diff --git a/llvm/test/tools/llvm-objcopy/COFF/only-keep-debug.test b/llvm/test/tools/llvm-objcopy/COFF/only-keep-debug.test index 5518d4000fcfa..c2a6afef7ce99 100644 --- a/llvm/test/tools/llvm-objcopy/COFF/only-keep-debug.test +++ b/llvm/test/tools/llvm-objcopy/COFF/only-keep-debug.test @@ -3,6 +3,10 @@ RUN: yaml2obj %p/Inputs/only-keep-sections.yaml > %t.in.exe RUN: llvm-objcopy --only-keep-debug %t.in.exe %t.out.exe RUN: llvm-readobj --sections %t.out.exe | FileCheck %s --check-prefix=SECTIONS RUN: llvm-objdump -t %t.out.exe | FileCheck %s --check-prefix=SYMBOLS +# 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.in.exe -o %t-strip.out.exe +RUN: cmp %t.out.exe %t-strip.out.exe Check that all non-debug/buildid sections with IMAGE_SCN_CNT_CODE or IMAGE_SCN_CNT_INITIALIZED_DATA are truncated, and no others. 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 c35c4a5c60af1..c5a794a08583b 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,6 +6,10 @@ # 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: cmp %t2 %t4 !ELF FileHeader: diff --git a/llvm/tools/llvm-objcopy/CopyConfig.cpp b/llvm/tools/llvm-objcopy/CopyConfig.cpp index 47867826ec203..dd3fa3e80e730 100644 --- a/llvm/tools/llvm-objcopy/CopyConfig.cpp +++ b/llvm/tools/llvm-objcopy/CopyConfig.cpp @@ -725,6 +725,7 @@ Expected parseStripOptions(ArrayRef ArgsArr) { Config.StripUnneeded = InputArgs.hasArg(STRIP_strip_unneeded); Config.StripAll = InputArgs.hasArg(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); for (auto Arg : InputArgs.filtered(STRIP_keep_section)) diff --git a/llvm/tools/llvm-objcopy/ObjcopyOpts.td b/llvm/tools/llvm-objcopy/ObjcopyOpts.td index a5f7119e3284e..8cd803ba3c98d 100644 --- a/llvm/tools/llvm-objcopy/ObjcopyOpts.td +++ b/llvm/tools/llvm-objcopy/ObjcopyOpts.td @@ -206,9 +206,12 @@ def N : JoinedOrSeparate<["-"], "N">, Alias; defm keep_symbol : Eq<"keep-symbol", "Do not remove symbol ">, MetaVarName<"symbol">; def K : JoinedOrSeparate<["-"], "K">, Alias; + def only_keep_debug : Flag<["-", "--"], "only-keep-debug">, - HelpText<"Currently ignored. Only for compatibility with GNU objcopy.">; + HelpText<"Clear sections that would not be stripped by --strip-debug. " + "Currently only implemented for COFF.">; + def keep_file_symbols : Flag<["-", "--"], "keep-file-symbols">, HelpText<"Do not remove file symbols">; defm dump_section diff --git a/llvm/tools/llvm-objcopy/StripOpts.td b/llvm/tools/llvm-objcopy/StripOpts.td index 40b043a7630e8..1bf18eb76b37c 100644 --- a/llvm/tools/llvm-objcopy/StripOpts.td +++ b/llvm/tools/llvm-objcopy/StripOpts.td @@ -63,6 +63,11 @@ def keep_file_symbols : Flag<["-", "--"], "keep-file-symbols">, def K : JoinedOrSeparate<["-"], "K">, Alias; +def only_keep_debug + : Flag<["-", "--"], "only-keep-debug">, + HelpText<"Clear sections that would not be stripped by --strip-debug. " + "Currently only implemented for COFF.">; + def discard_locals : Flag<["-", "--"], "discard-locals">, HelpText<"Remove compiler-generated local symbols, (e.g. " "symbols starting with .L)">;