[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

llvm-svn: 359781
diff --git a/llvm/test/tools/llvm-objcopy/ELF/basic-only-keep-debug.test b/llvm/test/tools/llvm-objcopy/ELF/basic-only-keep-debug.test
index c5a794a..95cd07d 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/basic-only-keep-debug.test
+++ b/llvm/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/llvm/test/tools/llvm-objcopy/ELF/dynsym-error-remove-strtab.test b/llvm/test/tools/llvm-objcopy/ELF/dynsym-error-remove-strtab.test
index 6e63c23..bab78ea 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/dynsym-error-remove-strtab.test
+++ b/llvm/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/llvm/test/tools/llvm-objcopy/ELF/no-strip-all.test b/llvm/test/tools/llvm-objcopy/ELF/no-strip-all.test
new file mode 100644
index 0000000..a0158d4
--- /dev/null
+++ b/llvm/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/llvm/test/tools/llvm-objcopy/ELF/reloc-error-remove-symtab.test b/llvm/test/tools/llvm-objcopy/ELF/reloc-error-remove-symtab.test
index 9b6d611..272e325 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/reloc-error-remove-symtab.test
+++ b/llvm/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 @@
 # 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/llvm/test/tools/llvm-objcopy/ELF/remove-linked-section.test b/llvm/test/tools/llvm-objcopy/ELF/remove-linked-section.test
index 63ba9d1..80939af 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/remove-linked-section.test
+++ b/llvm/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 @@
 # 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/llvm/test/tools/llvm-objcopy/ELF/symtab-error-on-remove-strtab.test b/llvm/test/tools/llvm-objcopy/ELF/symtab-error-on-remove-strtab.test
index 2115358..d0916c8 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/symtab-error-on-remove-strtab.test
+++ b/llvm/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 @@
 # 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/llvm/test/tools/llvm-objcopy/ELF/symtab-link.test b/llvm/test/tools/llvm-objcopy/ELF/symtab-link.test
index 7693bfc..68b8f78 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/symtab-link.test
+++ b/llvm/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