|
@@ -65,26 +65,37 @@ static std::vector<uint8_t> createGnuDebugLinkSectionContents(StringRef File) {
|
|
|
return Data;
|
|
|
}
|
|
|
|
|
|
-static void addGnuDebugLink(Object &Obj, StringRef DebugLinkFile) {
|
|
|
- uint32_t StartRVA = getNextRVA(Obj);
|
|
|
+// Adds named section with given contents to the object.
|
|
|
+static void addSection(Object &Obj, StringRef Name, ArrayRef<uint8_t> Contents,
|
|
|
+ uint32_t Characteristics) {
|
|
|
+ bool NeedVA = Characteristics & (IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ |
|
|
|
+ IMAGE_SCN_MEM_WRITE);
|
|
|
|
|
|
- std::vector<Section> Sections;
|
|
|
Section Sec;
|
|
|
- Sec.setOwnedContents(createGnuDebugLinkSectionContents(DebugLinkFile));
|
|
|
- Sec.Name = ".gnu_debuglink";
|
|
|
- Sec.Header.VirtualSize = Sec.getContents().size();
|
|
|
- Sec.Header.VirtualAddress = StartRVA;
|
|
|
- Sec.Header.SizeOfRawData = alignTo(Sec.Header.VirtualSize,
|
|
|
- Obj.IsPE ? Obj.PeHeader.FileAlignment : 1);
|
|
|
+ Sec.setOwnedContents(Contents);
|
|
|
+ Sec.Name = Name;
|
|
|
+ Sec.Header.VirtualSize = NeedVA ? Sec.getContents().size() : 0u;
|
|
|
+ Sec.Header.VirtualAddress = NeedVA ? getNextRVA(Obj) : 0u;
|
|
|
+ Sec.Header.SizeOfRawData =
|
|
|
+ NeedVA ? alignTo(Sec.Header.VirtualSize,
|
|
|
+ Obj.IsPE ? Obj.PeHeader.FileAlignment : 1)
|
|
|
+ : Sec.getContents().size();
|
|
|
// Sec.Header.PointerToRawData is filled in by the writer.
|
|
|
Sec.Header.PointerToRelocations = 0;
|
|
|
Sec.Header.PointerToLinenumbers = 0;
|
|
|
// Sec.Header.NumberOfRelocations is filled in by the writer.
|
|
|
Sec.Header.NumberOfLinenumbers = 0;
|
|
|
- Sec.Header.Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA |
|
|
|
- IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_DISCARDABLE;
|
|
|
- Sections.push_back(Sec);
|
|
|
- Obj.addSections(Sections);
|
|
|
+ Sec.Header.Characteristics = Characteristics;
|
|
|
+
|
|
|
+ Obj.addSections(Sec);
|
|
|
+}
|
|
|
+
|
|
|
+static void addGnuDebugLink(Object &Obj, StringRef DebugLinkFile) {
|
|
|
+ std::vector<uint8_t> Contents =
|
|
|
+ createGnuDebugLinkSectionContents(DebugLinkFile);
|
|
|
+ addSection(Obj, ".gnu_debuglink", Contents,
|
|
|
+ IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ |
|
|
|
+ IMAGE_SCN_MEM_DISCARDABLE);
|
|
|
}
|
|
|
|
|
|
static Error handleArgs(const CopyConfig &Config, Object &Obj) {
|
|
@@ -171,21 +182,40 @@ static Error handleArgs(const CopyConfig &Config, Object &Obj) {
|
|
|
return false;
|
|
|
});
|
|
|
|
|
|
+ for (const auto &Flag : Config.AddSection) {
|
|
|
+ StringRef SecName, FileName;
|
|
|
+ std::tie(SecName, FileName) = Flag.split("=");
|
|
|
+
|
|
|
+ if (FileName.empty())
|
|
|
+ return createStringError(llvm::errc::invalid_argument,
|
|
|
+ "bad format for --add-section");
|
|
|
+ auto BufOrErr = MemoryBuffer::getFile(FileName);
|
|
|
+ if (!BufOrErr)
|
|
|
+ return createFileError(FileName, errorCodeToError(BufOrErr.getError()));
|
|
|
+ auto Buf = std::move(*BufOrErr);
|
|
|
+
|
|
|
+ addSection(
|
|
|
+ Obj, SecName,
|
|
|
+ makeArrayRef(reinterpret_cast<const uint8_t *>(Buf->getBufferStart()),
|
|
|
+ Buf->getBufferSize()),
|
|
|
+ IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_ALIGN_1BYTES);
|
|
|
+ }
|
|
|
+
|
|
|
if (!Config.AddGnuDebugLink.empty())
|
|
|
addGnuDebugLink(Obj, Config.AddGnuDebugLink);
|
|
|
|
|
|
if (Config.AllowBrokenLinks || !Config.BuildIdLinkDir.empty() ||
|
|
|
Config.BuildIdLinkInput || Config.BuildIdLinkOutput ||
|
|
|
!Config.SplitDWO.empty() || !Config.SymbolsPrefix.empty() ||
|
|
|
- !Config.AllocSectionsPrefix.empty() || !Config.AddSection.empty() ||
|
|
|
- !Config.DumpSection.empty() || !Config.KeepSection.empty() ||
|
|
|
- !Config.SymbolsToGlobalize.empty() || !Config.SymbolsToKeep.empty() ||
|
|
|
- !Config.SymbolsToLocalize.empty() || !Config.SymbolsToWeaken.empty() ||
|
|
|
- !Config.SymbolsToKeepGlobal.empty() || !Config.SectionsToRename.empty() ||
|
|
|
- !Config.SetSectionFlags.empty() || !Config.SymbolsToRename.empty() ||
|
|
|
- Config.ExtractDWO || Config.KeepFileSymbols || Config.LocalizeHidden ||
|
|
|
- Config.PreserveDates || Config.StripDWO || Config.StripNonAlloc ||
|
|
|
- Config.StripSections || Config.Weaken || Config.DecompressDebugSections ||
|
|
|
+ !Config.AllocSectionsPrefix.empty() || !Config.DumpSection.empty() ||
|
|
|
+ !Config.KeepSection.empty() || !Config.SymbolsToGlobalize.empty() ||
|
|
|
+ !Config.SymbolsToKeep.empty() || !Config.SymbolsToLocalize.empty() ||
|
|
|
+ !Config.SymbolsToWeaken.empty() || !Config.SymbolsToKeepGlobal.empty() ||
|
|
|
+ !Config.SectionsToRename.empty() || !Config.SetSectionFlags.empty() ||
|
|
|
+ !Config.SymbolsToRename.empty() || Config.ExtractDWO ||
|
|
|
+ Config.KeepFileSymbols || Config.LocalizeHidden || Config.PreserveDates ||
|
|
|
+ Config.StripDWO || Config.StripNonAlloc || Config.StripSections ||
|
|
|
+ Config.Weaken || Config.DecompressDebugSections ||
|
|
|
Config.DiscardMode == DiscardType::Locals ||
|
|
|
!Config.SymbolsToAdd.empty() || Config.EntryExpr) {
|
|
|
return createStringError(llvm::errc::invalid_argument,
|