From efbb95e52b273791f0fce9a1ebd16dbec55b4233 Mon Sep 17 00:00:00 2001 From: James Henderson Date: Thu, 2 May 2019 11:53:02 +0000 Subject: [PATCH] [llvm-strip]Add --no-strip-all to disable --strip-all behaviour (including default stripping) If certain switches are not specified, llvm-strip behaves as if --strip-all were specified. This means that for testing, when we don't want the stripping behaviour, we have to specify one of these switches, which can be confusing. This change adds --no-strip-all to allow an alternative way of suppressing the default stripping, in a less confusing manner. Reviewed by: jakehehrlich, MaskRay Differential Revision: https://reviews.llvm.org/D61377 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@359781 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../ELF/basic-only-keep-debug.test | 4 +- .../ELF/dynsym-error-remove-strtab.test | 6 +-- test/tools/llvm-objcopy/ELF/no-strip-all.test | 49 +++++++++++++++++++ .../ELF/reloc-error-remove-symtab.test | 6 +-- .../ELF/remove-linked-section.test | 6 +-- .../ELF/symtab-error-on-remove-strtab.test | 6 +-- test/tools/llvm-objcopy/ELF/symtab-link.test | 2 +- tools/llvm-objcopy/CopyConfig.cpp | 8 +-- tools/llvm-objcopy/StripOpts.td | 2 + 9 files changed, 66 insertions(+), 23 deletions(-) create mode 100644 test/tools/llvm-objcopy/ELF/no-strip-all.test diff --git a/test/tools/llvm-objcopy/ELF/basic-only-keep-debug.test b/test/tools/llvm-objcopy/ELF/basic-only-keep-debug.test index c5a794a0858..95cd07d0c26 100644 --- a/test/tools/llvm-objcopy/ELF/basic-only-keep-debug.test +++ b/test/tools/llvm-objcopy/ELF/basic-only-keep-debug.test @@ -6,9 +6,7 @@ # RUN: llvm-objcopy %t %t2 # RUN: llvm-objcopy --only-keep-debug %t %t3 # RUN: cmp %t2 %t3 -# Run llvm-strip with --strip-symbol on a non-existent symbol to prevent -# defaulting to --strip-all. -# RUN: llvm-strip --only-keep-debug --strip-symbol foo %t -o %t4 +# RUN: llvm-strip --only-keep-debug --no-strip-all %t -o %t4 # RUN: cmp %t2 %t4 !ELF diff --git a/test/tools/llvm-objcopy/ELF/dynsym-error-remove-strtab.test b/test/tools/llvm-objcopy/ELF/dynsym-error-remove-strtab.test index 6e63c23acbf..bab78ea3dd3 100644 --- a/test/tools/llvm-objcopy/ELF/dynsym-error-remove-strtab.test +++ b/test/tools/llvm-objcopy/ELF/dynsym-error-remove-strtab.test @@ -1,15 +1,13 @@ # RUN: not llvm-objcopy -R .dynstr %p/Inputs/dynsym.so %t 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR # RUN: cp %p/Inputs/dynsym.so %t2 -## Use --strip-debug to suppress the default --strip-all behavior of llvm-strip. -## TODO: Implement a better way to suppress --strip-all behavior. -# RUN: not llvm-strip --strip-debug -R .dynstr %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR +# RUN: not llvm-strip --no-strip-all -R .dynstr %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR # ERR: Section .dynstr cannot be removed because it is referenced by the section .dynsym # RUN: llvm-objcopy --allow-broken-links -R .dynstr %p/Inputs/dynsym.so %t3 # RUN: llvm-objdump --section-headers %t3 | FileCheck %s --check-prefix=SECTIONS --implicit-check-not=.dynstr # RUN: cp %p/Inputs/dynsym.so %t4 -# RUN: llvm-strip --strip-debug --allow-broken-links -R .dynstr %t4 +# RUN: llvm-strip --no-strip-all --allow-broken-links -R .dynstr %t4 # RUN: llvm-objdump --section-headers %t4 | FileCheck %s --check-prefix=SECTIONS --implicit-check-not=.dynstr # SECTIONS: .dynsym diff --git a/test/tools/llvm-objcopy/ELF/no-strip-all.test b/test/tools/llvm-objcopy/ELF/no-strip-all.test new file mode 100644 index 00000000000..a0158d4b834 --- /dev/null +++ b/test/tools/llvm-objcopy/ELF/no-strip-all.test @@ -0,0 +1,49 @@ +## --no-strip-all disables the --strip-all llvm-strip switch. +## It also disables the default --strip-all behaviour. + +# RUN: yaml2obj %s -o %t.o + +## Base case: no switches, should strip. Used as a sanity check for later test cases. +# RUN: llvm-strip %t.o -o %t1.o +# RUN: llvm-readobj --file-headers --sections %t1.o | FileCheck %s --check-prefix=ALL + +## --no-strip-all alone disables stripping. +# RUN: llvm-strip --no-strip-all %t.o -o %t2.o +# RUN: llvm-readobj --file-headers --sections %t2.o | FileCheck %s --check-prefix=NO-STRIP + +## --no-strip-all wins if last. +# RUN: llvm-strip --strip-all --no-strip-all %t.o -o %t3.o +# RUN: cmp %t2.o %t3.o + +## --strip-all wins if last. +# RUN: llvm-strip --no-strip-all --strip-all %t.o -o %t4.o +# RUN: cmp %t1.o %t4.o + +## The last instance of --no-strip-all is used in the comparison. +# RUN: llvm-strip --no-strip-all --strip-all --no-strip-all %t.o -o %t5.o +# RUN: cmp %t2.o %t5.o + +## The last instance of --strip-all is used in the comparison. +# RUN: llvm-strip --strip-all --no-strip-all --strip-all %t.o -o %t6.o +# RUN: cmp %t1.o %t6.o + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .alloc + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC ] + +# ALL: SectionHeaderCount: 3 +# ALL: Name: .alloc +# ALL: Name: .shstrtab + +# NO-STRIP: SectionHeaderCount: 5 +# NO-STRIP: Name: .alloc +# NO-STRIP: Name: .symtab +# NO-STRIP: Name: .strtab +# NO-STRIP: Name: .shstrtab diff --git a/test/tools/llvm-objcopy/ELF/reloc-error-remove-symtab.test b/test/tools/llvm-objcopy/ELF/reloc-error-remove-symtab.test index 9b6d611d67e..272e325e97c 100644 --- a/test/tools/llvm-objcopy/ELF/reloc-error-remove-symtab.test +++ b/test/tools/llvm-objcopy/ELF/reloc-error-remove-symtab.test @@ -1,9 +1,7 @@ # RUN: yaml2obj %s > %t # RUN: not llvm-objcopy -R .symtab %t %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR # RUN: cp %t %t3 -## Use --strip-debug to suppress the default --strip-all behavior of llvm-strip. -## TODO: Implement a better way to suppress --strip-all behavior. -# RUN: not llvm-strip --strip-debug -R .symtab %t3 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR +# RUN: not llvm-strip --no-strip-all -R .symtab %t3 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR !ELF FileHeader: @@ -38,7 +36,7 @@ Symbols: # RUN: llvm-objcopy --allow-broken-links -R .symtab %t %t4 # RUN: llvm-readobj --sections %t4 | FileCheck %s --check-prefix=SECTIONS --implicit-check-not=.symtab # RUN: cp %t %t5 -# RUN: llvm-strip --strip-debug --allow-broken-links -R .symtab %t5 +# RUN: llvm-strip --no-strip-all --allow-broken-links -R .symtab %t5 # RUN: llvm-readobj --sections %t5 | FileCheck %s --check-prefix=SECTIONS --implicit-check-not=.symtab # SECTIONS: Name: .rel.text diff --git a/test/tools/llvm-objcopy/ELF/remove-linked-section.test b/test/tools/llvm-objcopy/ELF/remove-linked-section.test index 63ba9d158b5..80939af918d 100644 --- a/test/tools/llvm-objcopy/ELF/remove-linked-section.test +++ b/test/tools/llvm-objcopy/ELF/remove-linked-section.test @@ -1,9 +1,7 @@ # RUN: yaml2obj %s -o %t.o # RUN: not llvm-objcopy -R .foo %t.o %t1 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR # RUN: cp %t.o %t2 -## Use --strip-debug to suppress the default --strip-all behavior of llvm-strip. -## TODO: Implement a better way to suppress --strip-all behavior. -# RUN: not llvm-strip --strip-debug -R .foo %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR +# RUN: not llvm-strip --no-strip-all -R .foo %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR --- !ELF FileHeader: @@ -23,7 +21,7 @@ Sections: # RUN: llvm-objcopy --allow-broken-links -R .foo %t.o %t3 # RUN: llvm-readobj --sections %t3 | FileCheck %s --check-prefix=SECTIONS --implicit-check-not=.foo # RUN: cp %t.o %t4 -# RUN: llvm-strip --strip-debug --allow-broken-links -R .foo %t4 +# RUN: llvm-strip --no-strip-all --allow-broken-links -R .foo %t4 # RUN: llvm-readobj --sections %t4 | FileCheck %s --check-prefix=SECTIONS --implicit-check-not=.foo # SECTIONS: Name: .bar diff --git a/test/tools/llvm-objcopy/ELF/symtab-error-on-remove-strtab.test b/test/tools/llvm-objcopy/ELF/symtab-error-on-remove-strtab.test index 211535830d0..d0916c8e510 100644 --- a/test/tools/llvm-objcopy/ELF/symtab-error-on-remove-strtab.test +++ b/test/tools/llvm-objcopy/ELF/symtab-error-on-remove-strtab.test @@ -1,9 +1,7 @@ # RUN: yaml2obj %s > %t # RUN: not llvm-objcopy -R .strtab %t %t2 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR # RUN: cp %t %t3 -## Use --strip-debug to suppress the default --strip-all behavior of llvm-strip. -## TODO: Implement a better way to suppress --strip-all behavior. -# RUN: not llvm-strip --strip-debug -R .strtab %t3 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR +# RUN: not llvm-strip --no-strip-all -R .strtab %t3 2>&1 >/dev/null | FileCheck %s --check-prefix=ERR !ELF FileHeader: @@ -17,7 +15,7 @@ FileHeader: # RUN: llvm-objcopy --allow-broken-links -R .strtab %t %t4 # RUN: llvm-objdump --section-headers %t4 | FileCheck %s --check-prefix=SECTIONS --implicit-check-not=.strtab # RUN: cp %t %t5 -# RUN: llvm-strip --strip-debug --allow-broken-links -R .strtab %t %t5 +# RUN: llvm-strip --no-strip-all --allow-broken-links -R .strtab %t %t5 # RUN: llvm-objdump --section-headers %t5 | FileCheck %s --check-prefix=SECTIONS --implicit-check-not=.strtab # SECTIONS: .symtab diff --git a/test/tools/llvm-objcopy/ELF/symtab-link.test b/test/tools/llvm-objcopy/ELF/symtab-link.test index 7693bfc37ec..68b8f78dde9 100644 --- a/test/tools/llvm-objcopy/ELF/symtab-link.test +++ b/test/tools/llvm-objcopy/ELF/symtab-link.test @@ -2,7 +2,7 @@ # RUN: llvm-objcopy %t %t2 # RUN: llvm-readobj --sections %t2 | FileCheck %s # RUN: cp %t %t3 -# RUN: llvm-strip --strip-debug %t3 +# RUN: llvm-strip --no-strip-all %t3 # RUN: llvm-readobj --sections %t3 | FileCheck %s !ELF diff --git a/tools/llvm-objcopy/CopyConfig.cpp b/tools/llvm-objcopy/CopyConfig.cpp index ddb8d4d1d18..bece6735166 100644 --- a/tools/llvm-objcopy/CopyConfig.cpp +++ b/tools/llvm-objcopy/CopyConfig.cpp @@ -730,7 +730,8 @@ Expected parseStripOptions(ArrayRef ArgsArr) { ? DiscardType::All : DiscardType::Locals; Config.StripUnneeded = InputArgs.hasArg(STRIP_strip_unneeded); - Config.StripAll = InputArgs.hasArg(STRIP_strip_all); + if (auto Arg = InputArgs.getLastArg(STRIP_strip_all, STRIP_no_strip_all)) + Config.StripAll = Arg->getOption().getID() == STRIP_strip_all; Config.StripAllGNU = InputArgs.hasArg(STRIP_strip_all_gnu); Config.OnlyKeepDebug = InputArgs.hasArg(STRIP_only_keep_debug); Config.KeepFileSymbols = InputArgs.hasArg(STRIP_keep_file_symbols); @@ -747,8 +748,9 @@ Expected parseStripOptions(ArrayRef ArgsArr) { for (auto Arg : InputArgs.filtered(STRIP_keep_symbol)) Config.SymbolsToKeep.emplace_back(Arg->getValue(), UseRegexp); - if (!Config.StripDebug && !Config.StripUnneeded && - Config.DiscardMode == DiscardType::None && !Config.StripAllGNU && Config.SymbolsToRemove.empty()) + if (!InputArgs.hasArg(STRIP_no_strip_all) && !Config.StripDebug && + !Config.StripUnneeded && Config.DiscardMode == DiscardType::None && + !Config.StripAllGNU && Config.SymbolsToRemove.empty()) Config.StripAll = true; Config.DeterministicArchives = diff --git a/tools/llvm-objcopy/StripOpts.td b/tools/llvm-objcopy/StripOpts.td index 99ce3c25a3c..40fe5d072c1 100644 --- a/tools/llvm-objcopy/StripOpts.td +++ b/tools/llvm-objcopy/StripOpts.td @@ -42,6 +42,8 @@ def strip_all : Flag<["--"], "strip-all">, HelpText<"Remove non-allocated sections outside segments. " ".gnu.warning* sections are not removed">; def s : Flag<["-"], "s">, Alias; +def no_strip_all : Flag<["--"], "no-strip-all">, + HelpText<"Disable --strip-all">; def strip_all_gnu : Flag<["--"], "strip-all-gnu">, HelpText<"Compatible with GNU strip's --strip-all">; -- 2.50.1