summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJake Ehrlich <jakehehrlich@google.com>2017-10-10 23:02:43 +0000
committerJake Ehrlich <jakehehrlich@google.com>2017-10-10 23:02:43 +0000
commitc9dc2816f16b6e5162ca71fef71387a725a901fd (patch)
tree6ceeedd803d6c31505e4c928f9eaa21bc68e2f8c
parent02ce807db57d139864a316ba01e46955f8eab54f (diff)
[llvm-objcopy] Add ability to remove multiple sections by name
This change adds the ability to use the "-R"/"-remove-section" option multiple times. Differential Revision: https://reviews.llvm.org/D38332 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@315385 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--test/tools/llvm-objcopy/remove-multiple-sections.test130
-rw-r--r--tools/llvm-objcopy/llvm-objcopy.cpp15
2 files changed, 139 insertions, 6 deletions
diff --git a/test/tools/llvm-objcopy/remove-multiple-sections.test b/test/tools/llvm-objcopy/remove-multiple-sections.test
new file mode 100644
index 00000000000..5e5de97cd31
--- /dev/null
+++ b/test/tools/llvm-objcopy/remove-multiple-sections.test
@@ -0,0 +1,130 @@
+# RUN: yaml2obj %s > %t
+# RUN: llvm-objcopy -R .test2 -R .test3 -R .test5 %t %t2
+# RUN: llvm-readobj -sections %t2 | FileCheck %s
+
+!ELF
+FileHeader:
+ Class: ELFCLASS64
+ Data: ELFDATA2LSB
+ Type: ET_EXEC
+ Machine: EM_X86_64
+Sections:
+ - Name: .test1
+ Type: SHT_PROGBITS
+ Flags: [ ]
+ - Name: .test2
+ Type: SHT_PROGBITS
+ Flags: [ ]
+ - Name: .test3
+ Type: SHT_PROGBITS
+ Flags: [ ]
+ - Name: .test4
+ Type: SHT_PROGBITS
+ Flags: [ ]
+ - Name: .test5
+ Type: SHT_PROGBITS
+ Flags: [ ]
+ - Name: .test6
+ Type: SHT_PROGBITS
+ Flags: [ ]
+
+# CHECK: Sections [
+# CHECK: Section {
+# CHECK: Index: 0
+# CHECK: Name: (0)
+# CHECK: Type: SHT_NULL (0x0)
+# CHECK: Flags [ (0x0)
+# CHECK: ]
+# CHECK: Address: 0x0
+# CHECK: Offset:
+# CHECK: Size:
+# CHECK: Link: 0
+# CHECK: Info: 0
+# CHECK: AddressAlignment: 0
+# CHECK: EntrySize: 0
+# CHECK: }
+# CHECK: Section {
+# CHECK: Index: 1
+# CHECK: Name: .test1
+# CHECK: Type: SHT_PROGBITS (0x1)
+# CHECK: Flags [ (0x0)
+# CHECK: ]
+# CHECK: Address: 0x0
+# CHECK: Offset:
+# CHECK: Size:
+# CHECK: Link: 0
+# CHECK: Info: 0
+# CHECK: AddressAlignment: 0
+# CHECK: EntrySize: 0
+# CHECK: }
+# CHECK: Section {
+# CHECK: Index: 2
+# CHECK: Name: .test4
+# CHECK: Type: SHT_PROGBITS (0x1)
+# CHECK: Flags [ (0x0)
+# CHECK: ]
+# CHECK: Address: 0x0
+# CHECK: Offset:
+# CHECK: Size:
+# CHECK: Link: 0
+# CHECK: Info: 0
+# CHECK: AddressAlignment: 0
+# CHECK: EntrySize: 0
+# CHECK: }
+# CHECK: Section {
+# CHECK: Index: 3
+# CHECK: Name: .test6
+# CHECK: Type: SHT_PROGBITS (0x1)
+# CHECK: Flags [ (0x0)
+# CHECK: ]
+# CHECK: Address: 0x0
+# CHECK: Offset:
+# CHECK: Size:
+# CHECK: Link: 0
+# CHECK: Info: 0
+# CHECK: AddressAlignment: 0
+# CHECK: EntrySize: 0
+# CHECK: }
+# CHECK: Section {
+# CHECK: Index: 4
+# CHECK: Name: .symtab
+# CHECK: Type: SHT_SYMTAB (0x2)
+# CHECK: Flags [ (0x0)
+# CHECK: ]
+# CHECK: Address: 0x0
+# CHECK: Offset:
+# CHECK: Size:
+# CHECK: Link: 5
+# CHECK: Info: 1
+# CHECK: AddressAlignment: 8
+# CHECK: EntrySize: 24
+# CHECK: }
+# CHECK: Section {
+# CHECK: Index: 5
+# CHECK: Name: .strtab
+# CHECK: Type: SHT_STRTAB (0x3)
+# CHECK: Flags [ (0x0)
+# CHECK: ]
+# CHECK: Address: 0x0
+# CHECK: Offset:
+# CHECK: Size:
+# CHECK: Link: 0
+# CHECK: Info: 0
+# CHECK: AddressAlignment: 1
+# CHECK: EntrySize: 0
+# CHECK: }
+# CHECK: Section {
+# CHECK: Index: 6
+# CHECK: Name: .shstrtab
+# CHECK: Type: SHT_STRTAB (0x3)
+# CHECK: Flags [ (0x0)
+# CHECK: ]
+# CHECK: Address: 0x0
+# CHECK: Offset:
+# CHECK: Size:
+# CHECK: Link: 0
+# CHECK: Info: 0
+# CHECK: AddressAlignment: 1
+# CHECK: EntrySize: 0
+# CHECK: }
+# CHECK: ]
diff --git a/tools/llvm-objcopy/llvm-objcopy.cpp b/tools/llvm-objcopy/llvm-objcopy.cpp
index 775c5ae42b6..d76735482d6 100644
--- a/tools/llvm-objcopy/llvm-objcopy.cpp
+++ b/tools/llvm-objcopy/llvm-objcopy.cpp
@@ -56,10 +56,11 @@ cl::opt<std::string> OutputFilename(cl::Positional, cl::desc("<output>"),
cl::opt<std::string>
OutputFormat("O", cl::desc("set output format to one of the following:"
"\n\tbinary"));
-// TODO: make this a cl::list to support removing multiple sections
-cl::opt<std::string> ToRemove("remove-section",
- cl::desc("Remove a specific section"));
-cl::alias ToRemoveA("R", cl::desc("Alias for remove-section"), cl::aliasopt(ToRemove));
+
+cl::list<std::string> ToRemove("remove-section",
+ cl::desc("Remove a specific section"));
+cl::alias ToRemoveA("R", cl::desc("Alias for remove-section"),
+ cl::aliasopt(ToRemove));
void CopyBinary(const ELFObjectFile<ELF64LE> &ObjFile) {
std::unique_ptr<FileOutputBuffer> Buffer;
@@ -71,8 +72,10 @@ void CopyBinary(const ELFObjectFile<ELF64LE> &ObjFile) {
else
Obj = llvm::make_unique<ELFObject<ELF64LE>>(ObjFile);
if (!ToRemove.empty()) {
- Obj->removeSections(
- [&](const SectionBase &Sec) { return ToRemove == Sec.Name; });
+ Obj->removeSections([&](const SectionBase &Sec) {
+ return std::find(std::begin(ToRemove), std::end(ToRemove), Sec.Name) !=
+ std::end(ToRemove);
+ });
}
Obj->finalize();
ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =