Index: test/tools/llvm-objcopy/COFF/strip-debug.yaml =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/COFF/strip-debug.yaml @@ -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 +... Index: tools/llvm-objcopy/COFF/COFFObjcopy.cpp =================================================================== --- tools/llvm-objcopy/COFF/COFFObjcopy.cpp +++ tools/llvm-objcopy/COFF/COFFObjcopy.cpp @@ -27,9 +27,20 @@ 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;