[PATCH] D39926: [llvm-objcopy] Add --only-keep-alloc option to remove all non-allocated sections

Jake Ehrlich via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 14 10:50:42 PST 2017


This revision was automatically updated to reflect the committed changes.
Closed by commit rL318168: [llvm-objcopy] Add -strip-non-alloc option to remove all non-allocated sections (authored by jakehehrlich).

Changed prior to commit:
  https://reviews.llvm.org/D39926?vs=122767&id=122876#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D39926

Files:
  llvm/trunk/test/tools/llvm-objcopy/strip-non-alloc.test
  llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp


Index: llvm/trunk/test/tools/llvm-objcopy/strip-non-alloc.test
===================================================================
--- llvm/trunk/test/tools/llvm-objcopy/strip-non-alloc.test
+++ llvm/trunk/test/tools/llvm-objcopy/strip-non-alloc.test
@@ -0,0 +1,26 @@
+# RUN: yaml2obj %s > %t
+# RUN: llvm-objcopy --strip-non-alloc %t %t2
+# RUN: llvm-readobj -file-headers -sections %t2 | FileCheck %s
+
+!ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_REL
+  Machine:         EM_X86_64
+Sections:
+  - Name:            .bss
+    Type:            SHT_NOBITS
+    Flags:           [ SHF_ALLOC ]
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+  - Name:            .blarg
+    Type:            SHT_PROGBITS
+    Flags:           [ ]
+
+# CHECK: SectionHeaderCount: 4
+
+# CHECK: Name: .bss
+# CHECK: Name: .text
+# CHECK: Name: .shstrtab
Index: llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp
===================================================================
--- llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp
+++ llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp
@@ -87,6 +87,8 @@
                                 cl::desc("Removes all debug information"));
 static cl::opt<bool> StripSections("strip-sections",
                                    cl::desc("Remove all section headers"));
+static cl::opt<bool> StripNonAlloc("strip-non-alloc",
+                                   cl::desc("Remove all non-allocated sections"));
 static cl::opt<bool>
     StripDWO("strip-dwo", cl::desc("remove all DWARF .dwo sections from file"));
 static cl::opt<bool> ExtractDWO(
@@ -206,6 +208,15 @@
     };
   }
 
+  if (StripNonAlloc)
+    RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
+      if (RemovePred(Sec))
+        return true;
+      if (&Sec == Obj->getSectionHeaderStrTab())
+        return false;
+      return (Sec.Flags & SHF_ALLOC) == 0;
+    };
+
   Obj->removeSections(RemovePred);
   Obj->finalize();
   WriteObjectFile(*Obj, OutputFilename.getValue());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D39926.122876.patch
Type: text/x-patch
Size: 2094 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171114/7a88f164/attachment.bin>


More information about the llvm-commits mailing list