From 9c93f41cc7c698fc369a93ec6c9f1bbb0992d024 Mon Sep 17 00:00:00 2001 From: Martin Storsjo Date: Sat, 19 Jan 2019 19:42:41 +0000 Subject: [PATCH] [llvm-objcopy] [COFF] Implement --strip-debug Also remove sections similarly for --strip-all, --discard-all, --strip-unneeded. Differential Revision: https://reviews.llvm.org/D56839 llvm-svn: 351661 --- test/tools/llvm-objcopy/COFF/strip-debug.test | 109 ++++++++++++++++++ tools/llvm-objcopy/COFF/COFFObjcopy.cpp | 11 ++ 2 files changed, 120 insertions(+) create mode 100644 test/tools/llvm-objcopy/COFF/strip-debug.test diff --git a/test/tools/llvm-objcopy/COFF/strip-debug.test b/test/tools/llvm-objcopy/COFF/strip-debug.test new file mode 100644 index 00000000000..97fa96aac70 --- /dev/null +++ b/test/tools/llvm-objcopy/COFF/strip-debug.test @@ -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 +... diff --git a/tools/llvm-objcopy/COFF/COFFObjcopy.cpp b/tools/llvm-objcopy/COFF/COFFObjcopy.cpp index dd2e4829218..13d8efde37c 100644 --- a/tools/llvm-objcopy/COFF/COFFObjcopy.cpp +++ b/tools/llvm-objcopy/COFF/COFFObjcopy.cpp @@ -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;