[llvm] r351661 - [llvm-objcopy] [COFF] Implement --strip-debug

Martin Storsjo via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 19 11:42:41 PST 2019


Author: mstorsjo
Date: Sat Jan 19 11:42:41 2019
New Revision: 351661

URL: http://llvm.org/viewvc/llvm-project?rev=351661&view=rev
Log:
[llvm-objcopy] [COFF] Implement --strip-debug

Also remove sections similarly for --strip-all, --discard-all,
--strip-unneeded.

Differential Revision: https://reviews.llvm.org/D56839

Added:
    llvm/trunk/test/tools/llvm-objcopy/COFF/strip-debug.test
Modified:
    llvm/trunk/tools/llvm-objcopy/COFF/COFFObjcopy.cpp

Added: llvm/trunk/test/tools/llvm-objcopy/COFF/strip-debug.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-objcopy/COFF/strip-debug.test?rev=351661&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-objcopy/COFF/strip-debug.test (added)
+++ llvm/trunk/test/tools/llvm-objcopy/COFF/strip-debug.test Sat Jan 19 11:42:41 2019
@@ -0,0 +1,109 @@
+# RUN: yaml2obj %s > %t.in.o
+#
+# RUN: llvm-objdump --section-headers %t.in.o | FileCheck %s --check-prefixes=SECTIONS,SECTIONS-PRE
+# RUN: llvm-objdump -t %t.in.o | FileCheck %s --check-prefixes=SYMBOLS,SYMBOLS-PRE
+#
+# RUN: llvm-objcopy --strip-debug %t.in.o %t.out.o
+# RUN: llvm-objdump --section-headers %t.out.o | FileCheck %s --check-prefixes=SECTIONS
+# RUN: llvm-objdump -t %t.out.o | FileCheck %s --check-prefixes=SYMBOLS
+#
+# Test that --strip-all, --strip-all-gnu, --discard-all and --strip-unneeded,
+# plus llvm-strip without arguments all produce a similiar set of sections
+# (while they remove symbols differently).
+#
+# RUN: llvm-objcopy --strip-all %t.in.o %t.strip-all.o
+# RUN: llvm-objdump --section-headers %t.strip-all.o | FileCheck %s --check-prefixes=SECTIONS
+#
+# RUN: llvm-objcopy --strip-all-gnu %t.in.o %t.strip-all-gnu.o
+# RUN: llvm-objdump --section-headers %t.strip-all-gnu.o | FileCheck %s --check-prefixes=SECTIONS
+#
+# RUN: llvm-objcopy --discard-all %t.in.o %t.discard-all.o
+# RUN: llvm-objdump --section-headers %t.discard-all.o | FileCheck %s --check-prefixes=SECTIONS
+#
+# RUN: llvm-objcopy --discard-all %t.in.o %t.strip-unneeded.o
+# RUN: llvm-objdump --section-headers %t.strip-unneeded.o | FileCheck %s --check-prefixes=SECTIONS
+#
+# SECTIONS:        Sections:
+# SECTIONS-NEXT:   Idx Name
+# SECTIONS-NEXT:     0      .text
+# SECTIONS-NEXT:     1      .data
+# SECTIONS-NEXT:     2      .bss
+# SECTIONS-NEXT:     3      .xdata
+# SECTIONS-NEXT:     4      .reloc
+# SECTIONS-PRE-NEXT: 5      .debug_discardable
+# SECTIONS-NEXT:     {{.*}} .debug_undiscardable
+# SECTIONS-NEXT:     {{.*}} .llvm_addrsig
+# SECTIONS-EMPTY:
+#
+# Test that --strip-debug doesn't remove e.g. unreferenced local symbols.
+#
+# SYMBOLS:          SYMBOL TABLE:
+# SYMBOLS-NEXT:     external
+# SYMBOLS-NEXT:     local_unreferenced
+# SYMBOLS-PRE-NEXT: debug_discardable_sym
+# SYMBOLS-NEXT:     debug_undiscardable_sym
+# SYMBOLS-EMPTY:
+
+--- !COFF
+header:          
+  Machine:         IMAGE_FILE_MACHINE_AMD64
+  Characteristics: [  ]
+sections:        
+  - Name:            .text
+    Characteristics: [  ]
+    Alignment:       4
+    SectionData:     00000000
+  - Name:            .data
+    Characteristics: [  ]
+    Alignment:       4
+    SectionData:     00000000
+  - Name:            .bss
+    Characteristics: [  ]
+    Alignment:       4
+    SectionData:     00000000
+  - Name:            .xdata
+    Characteristics: [  ]
+    Alignment:       4
+    SectionData:     00000000
+  - Name:            .reloc
+    Characteristics: [ IMAGE_SCN_MEM_DISCARDABLE ]
+    Alignment:       4
+    SectionData:     00000000
+  - Name:            .debug_discardable
+    Characteristics: [ IMAGE_SCN_MEM_DISCARDABLE ]
+    Alignment:       4
+    SectionData:     00000000
+  - Name:            .debug_undiscardable
+    Characteristics: [  ]
+    Alignment:       4
+    SectionData:     00000000
+  - Name:            .llvm_addrsig
+    Characteristics: [ IMAGE_SCN_LNK_REMOVE ]
+    Alignment:       4
+    SectionData:     00000000
+symbols:         
+  - Name:            external
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            local_unreferenced
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+  - Name:            debug_discardable_sym
+    Value:           0
+    SectionNumber:   6
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            debug_undiscardable_sym
+    Value:           0
+    SectionNumber:   7
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...

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=351661&r1=351660&r2=351661&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objcopy/COFF/COFFObjcopy.cpp (original)
+++ llvm/trunk/tools/llvm-objcopy/COFF/COFFObjcopy.cpp Sat Jan 19 11:42:41 2019
@@ -26,9 +26,20 @@ namespace coff {
 using namespace object;
 using namespace COFF;
 
+static bool isDebugSection(const Section &Sec) {
+  return Sec.Name.startswith(".debug");
+}
+
 static Error handleArgs(const CopyConfig &Config, Object &Obj) {
   // Perform the actual section removals.
   Obj.removeSections([&Config](const Section &Sec) {
+    if (Config.StripDebug || Config.StripAll || Config.StripAllGNU ||
+        Config.DiscardAll || Config.StripUnneeded) {
+      if (isDebugSection(Sec) &&
+          (Sec.Header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE) != 0)
+        return true;
+    }
+
     if (is_contained(Config.ToRemove, Sec.Name))
       return true;
 




More information about the llvm-commits mailing list