Index: test/tools/llvm-objcopy/keep-file-symbols-remove-section.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/keep-file-symbols-remove-section.test @@ -0,0 +1,34 @@ +# RUN: yaml2obj %s > %t +# RUN: llvm-objcopy --remove-section .text --keep-file-symbols %t %t2 +# RUN: llvm-readobj -symbols %t2 | FileCheck %s + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .text + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + Address: 0x1000 + AddressAlign: 0x0000000000000010 + Size: 64 +Symbols: + Local: + - Name: foo + Type: STT_FILE + Section: .text + +#CHECK: Symbols [ +#CHECK-NEXT: Symbol { +#CHECK-NEXT: Name: +#CHECK-NEXT: Value: 0x0 +#CHECK-NEXT: Size: 0 +#CHECK-NEXT: Binding: Local +#CHECK-NEXT: Type: None +#CHECK-NEXT: Other: 0 +#CHECK-NEXT: Section: Undefined +#CHECK-NEXT: } +#CHECK-NEXT:] Index: test/tools/llvm-objcopy/keep-file-symbols.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/keep-file-symbols.test @@ -0,0 +1,43 @@ +# RUN: yaml2obj %s > %t +# RUN: llvm-objcopy --strip-symbol foo --keep-file-symbols %t %t2 +# RUN: llvm-readobj -symbols %t2 | FileCheck %s + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .text + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + Address: 0x1000 + AddressAlign: 0x0000000000000010 + Size: 64 +Symbols: + Local: + - Name: foo + Type: STT_FILE + Section: .text + +#CHECK: Symbols [ +#CHECK-NEXT: Symbol { +#CHECK-NEXT: Name: +#CHECK-NEXT: Value: 0x0 +#CHECK-NEXT: Size: 0 +#CHECK-NEXT: Binding: Local +#CHECK-NEXT: Type: None +#CHECK-NEXT: Other: 0 +#CHECK-NEXT: Section: Undefined +#CHECK-NEXT: } +#CHECK-NEXT: Symbol { +#CHECK-NEXT: Name: foo +#CHECK-NEXT: Value: 0x0 +#CHECK-NEXT: Size: 0 +#CHECK-NEXT: Binding: Local +#CHECK-NEXT: Type: File +#CHECK-NEXT: Other: 0 +#CHECK-NEXT: Section: .text +#CHECK-NEXT: } +#CHECK-NEXT:] Index: tools/llvm-objcopy/ObjcopyOpts.td =================================================================== --- tools/llvm-objcopy/ObjcopyOpts.td +++ tools/llvm-objcopy/ObjcopyOpts.td @@ -88,3 +88,5 @@ HelpText<"Do not remove symbol ">; def K : JoinedOrSeparate<["-"], "K">, Alias; +def keep_file_symbols : Flag<["-", "--"], "keep-file-symbols">, + HelpText<"Do not remove file symbols">; Index: tools/llvm-objcopy/llvm-objcopy.cpp =================================================================== --- tools/llvm-objcopy/llvm-objcopy.cpp +++ tools/llvm-objcopy/llvm-objcopy.cpp @@ -160,6 +160,7 @@ bool LocalizeHidden = false; bool Weaken = false; bool DiscardAll = false; + bool KeepFileSymbols = false; }; using SectionPred = std::function; @@ -374,8 +375,9 @@ }); Obj.removeSymbols([&](const Symbol &Sym) { - if (!Config.SymbolsToKeep.empty() && - is_contained(Config.SymbolsToKeep, Sym.Name)) + if ((!Config.SymbolsToKeep.empty() && + is_contained(Config.SymbolsToKeep, Sym.Name)) || + (Config.KeepFileSymbols && Sym.Type == STT_FILE)) return false; if (Config.DiscardAll && Sym.Binding == STB_LOCAL && @@ -482,6 +484,7 @@ Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden); Config.Weaken = InputArgs.hasArg(OBJCOPY_weaken); Config.DiscardAll = InputArgs.hasArg(OBJCOPY_discard_all); + Config.KeepFileSymbols = InputArgs.hasArg(OBJCOPY_keep_file_symbols); for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol)) Config.SymbolsToLocalize.push_back(Arg->getValue()); for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol))