[PATCH] D56873: [llvm-objcopy] [COFF] Implement --only-section

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 18 12:28:45 PST 2019


mstorsjo updated this revision to Diff 182582.
mstorsjo added a comment.

Updated to test how --only-section combined with --strip-debug behaves - matching GNU objcopy's behaviour.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D56873/new/

https://reviews.llvm.org/D56873

Files:
  test/tools/llvm-objcopy/COFF/only-section.test
  tools/llvm-objcopy/COFF/COFFObjcopy.cpp


Index: tools/llvm-objcopy/COFF/COFFObjcopy.cpp
===================================================================
--- tools/llvm-objcopy/COFF/COFFObjcopy.cpp
+++ tools/llvm-objcopy/COFF/COFFObjcopy.cpp
@@ -34,6 +34,12 @@
 static Error handleArgs(const CopyConfig &Config, Object &Obj) {
   // Perform the actual section removals.
   Obj.removeSections([&Config](const Section &Sec) {
+    // Contrary to --only-keep-debug, --only-section fully removes sections that
+    // aren't mentioned.
+    if (!Config.OnlySection.empty() &&
+        !is_contained(Config.OnlySection, Sec.Name))
+      return true;
+
     if (Config.StripDebug || Config.StripAll || Config.StripAllGNU ||
         Config.DiscardAll || Config.StripUnneeded) {
       if (isDebugSection(Sec) &&
Index: test/tools/llvm-objcopy/COFF/only-section.test
===================================================================
--- /dev/null
+++ test/tools/llvm-objcopy/COFF/only-section.test
@@ -0,0 +1,21 @@
+RUN: yaml2obj %p/Inputs/only-keep-sections.yaml > %t.in.exe
+
+RUN: llvm-objcopy --only-section .debug_discardable %t.in.exe %t.out.exe
+RUN: llvm-objdump --section-headers -t %t.out.exe | FileCheck %s --check-prefixes=SECTIONS,SECTIONS-DEBUG,SYMBOLS,SYMBOLS-DEBUG
+
+Adding another section stripping option makes it return the intersection of
+kept sections - in this case keeping only .text.
+
+RUN: llvm-objcopy --only-section .debug_discardable --only-section .text --strip-debug %t.in.exe %t.combination.exe
+RUN: llvm-objdump --section-headers -t %t.combination.exe | FileCheck %s --check-prefixes=SECTIONS,SECTIONS-TEXT,SYMBOLS,SYMBOLS-TEXT
+
+SECTIONS: Sections:
+SECTIONS-NEXT: Idx Name
+SECTIONS-DEBUG-NEXT: .debug_discardable
+SECTIONS-TEXT-NEXT: .text
+SECTIONS-NEXT-EMPTY:
+
+SYMBOLS: SYMBOL TABLE:
+SYMBOLS-DEBUG-NEXT: debug_discardable_sym
+SYMBOLS-TEXT-NEXT: main
+SYMBOLS-EMPTY:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D56873.182582.patch
Type: text/x-patch
Size: 1869 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190118/75b56c9e/attachment.bin>


More information about the llvm-commits mailing list