[llvm] r367130 - [llvm-objcopy] Add support for --add-section for COFF
Sergey Dmitriev via llvm-commits
llvm-commits at lists.llvm.org
Fri Jul 26 10:06:42 PDT 2019
Author: sdmitriev
Date: Fri Jul 26 10:06:41 2019
New Revision: 367130
URL: http://llvm.org/viewvc/llvm-project?rev=367130&view=rev
Log:
[llvm-objcopy] Add support for --add-section for COFF
This patch enables support for --add-section=... option for COFF objects.
Differential Revision: https://reviews.llvm.org/D65040
Added:
llvm/trunk/test/tools/llvm-objcopy/COFF/add-section.test
Modified:
llvm/trunk/docs/CommandGuide/llvm-objcopy.rst
llvm/trunk/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
llvm/trunk/tools/llvm-objcopy/COFF/Writer.cpp
Modified: llvm/trunk/docs/CommandGuide/llvm-objcopy.rst
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/CommandGuide/llvm-objcopy.rst?rev=367130&r1=367129&r2=367130&view=diff
==============================================================================
--- llvm/trunk/docs/CommandGuide/llvm-objcopy.rst (original)
+++ llvm/trunk/docs/CommandGuide/llvm-objcopy.rst Fri Jul 26 10:06:41 2019
@@ -36,6 +36,13 @@ multiple file formats.
Add a .gnu_debuglink section for ``<debug-file>`` to the output.
+.. option:: --add-section <section=file>
+
+ Add a section named ``<section>`` with the contents of ``<file>`` to the
+ output. For ELF objects the section will be of type `SHT_NOTE`, if the name
+ starts with ".note". Otherwise, it will have type `SHT_PROGBITS`. Can be
+ specified multiple times to add multiple sections.
+
.. option:: --disable-deterministic-archives, -U
Use real values for UIDs, GIDs and timestamps when updating archive member
@@ -141,13 +148,6 @@ The following options are implemented on
objects, :program:`llvm-objcopy` will either emit an error or silently ignore
them.
-.. option:: --add-section <section=file>
-
- Add a section named ``<section>`` with the contents of ``<file>`` to the
- output. The section will be of type `SHT_NOTE`, if the name starts with
- ".note". Otherwise, it will have type `SHT_PROGBITS`. Can be specified multiple
- times to add multiple sections.
-
.. option:: --add-symbol <name>=[<section>:]<value>[,<flags>]
Add a new symbol called ``<name>`` to the output symbol table, in the section
Added: llvm/trunk/test/tools/llvm-objcopy/COFF/add-section.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/COFF/add-section.test?rev=367130&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/COFF/add-section.test (added)
+++ llvm/trunk/test/tools/llvm-objcopy/COFF/add-section.test Fri Jul 26 10:06:41 2019
@@ -0,0 +1,56 @@
+# RUN: yaml2obj %s > %t
+
+## Test that llvm-objcopy adds a section to the given object with expected
+## contents.
+# RUN: echo DEADBEEF > %t.sec
+# RUN: llvm-objcopy --add-section=.test.section=%t.sec %t %t1
+# RUN: llvm-readobj --file-headers --sections --section-data %t1 | FileCheck %s --check-prefixes=CHECK-ADD
+
+# CHECK-ADD: SectionCount: 2
+# CHECK-ADD: Name: .text
+# CHECK-ADD: Name: .test.section
+# CHECK-ADD: Characteristics [
+# CHECK-ADD-NEXT: IMAGE_SCN_ALIGN_1BYTES
+# CHECK-ADD-NEXT: IMAGE_SCN_CNT_INITIALIZED_DATA
+# CHECK-ADD-NEXT: ]
+# CHECK-ADD: SectionData (
+# CHECK-ADD-NEXT: 0000: {{.+}}|DEADBEEF{{.+}}|
+# CHECK-ADD-NEXT: )
+
+## Test that llvm-objcopy can add a section with an empty name.
+# RUN: llvm-objcopy --add-section==%t.sec %t %t1.empty.name
+# RUN: llvm-readobj --file-headers --sections --section-data %t1.empty.name | FileCheck %s --check-prefixes=CHECK-ADD-EMPTY-NAME
+
+# CHECK-ADD-EMPTY-NAME: SectionCount: 2
+# CHECK-ADD-EMPTY-NAME: Name: .text
+# CHECK-ADD-EMPTY-NAME: Name: (00 00 00 00 00 00 00 00)
+# CHECK-ADD-EMPTY-NAME: Characteristics [
+# CHECK-ADD-EMPTY-NAME-NEXT: IMAGE_SCN_ALIGN_1BYTES
+# CHECK-ADD-EMPTY-NAME-NEXT: IMAGE_SCN_CNT_INITIALIZED_DATA
+# CHECK-ADD-EMPTY-NAME-NEXT: ]
+# CHECK-ADD-EMPTY-NAME: SectionData (
+# CHECK-ADD-EMPTY-NAME-NEXT: 0000: {{.+}}|DEADBEEF{{.+}}|
+# CHECK-ADD-EMPTY-NAME-NEXT: )
+
+## Test that llvm-objcopy produces an error if the file with section contents
+## to be added does not exist.
+# RUN: not llvm-objcopy --add-section=.another.section=%t2 %t %t3 2>&1 | FileCheck -DFILE=%t -DFILE1=%t2 %s --check-prefixes=CHECK-ERR1
+
+# CHECK-ERR1: llvm-objcopy{{(.exe)?}}: error: '[[FILE]]': '[[FILE1]]': {{[Nn]}}o such file or directory
+
+## Another negative test for invalid --add-sections command line argument.
+# RUN: not llvm-objcopy --add-section=.another.section %t %t3 2>&1 | FileCheck -DFILE=%t %s --check-prefixes=CHECK-ERR2
+
+# CHECK-ERR2: llvm-objcopy{{(.exe)?}}: error: '[[FILE]]': bad format for --add-section
+
+--- !COFF
+header:
+ Machine: IMAGE_FILE_MACHINE_AMD64
+ Characteristics: [ ]
+sections:
+ - Name: .text
+ Characteristics: [ ]
+ Alignment: 4
+ SectionData: 488B0500000000C3
+symbols:
+...
Modified: llvm/trunk/tools/llvm-objcopy/COFF/COFFObjcopy.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/COFF/COFFObjcopy.cpp?rev=367130&r1=367129&r2=367130&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/COFF/COFFObjcopy.cpp (original)
+++ llvm/trunk/tools/llvm-objcopy/COFF/COFFObjcopy.cpp Fri Jul 26 10:06:41 2019
@@ -65,26 +65,37 @@ static std::vector<uint8_t> createGnuDeb
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
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,
Modified: llvm/trunk/tools/llvm-objcopy/COFF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objcopy/COFF/Writer.cpp?rev=367130&r1=367129&r2=367130&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/COFF/Writer.cpp (original)
+++ llvm/trunk/tools/llvm-objcopy/COFF/Writer.cpp Fri Jul 26 10:06:41 2019
@@ -120,12 +120,12 @@ size_t COFFWriter::finalizeStringTable()
StrTabBuilder.finalize();
for (auto &S : Obj.getMutableSections()) {
+ memset(S.Header.Name, 0, sizeof(S.Header.Name));
if (S.Name.size() > COFF::NameSize) {
- memset(S.Header.Name, 0, sizeof(S.Header.Name));
snprintf(S.Header.Name, sizeof(S.Header.Name), "/%d",
(int)StrTabBuilder.getOffset(S.Name));
} else {
- strncpy(S.Header.Name, S.Name.data(), COFF::NameSize);
+ memcpy(S.Header.Name, S.Name.data(), S.Name.size());
}
}
for (auto &S : Obj.getMutableSymbols()) {
More information about the llvm-commits
mailing list