Index: test/tools/llvm-objcopy/keep-file-symbols.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/keep-file-symbols.test @@ -0,0 +1,70 @@ +# RUN: yaml2obj %s > %t +# RUN: llvm-objcopy --strip-all --keep-file-symbols %t %t2 +# RUN: llvm-readobj -symbols %t2 | FileCheck %s --check-prefix=STRIPALL +# RUN: llvm-objcopy --keep-file-symbols --strip-symbol foo %t %t2 +# RUN: llvm-readobj -symbols %t2 | FileCheck %s --check-prefix=STRIP + +!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 + Global: + - Name: bar + Type: STT_FUNC + Section: .text + +#STRIPALL: Symbols [ +#STRIPALL-NEXT: Symbol { +#STRIPALL-NEXT: Name: foo +#STRIPALL-NEXT: Value: 0x0 +#STRIPALL-NEXT: Size: 0 +#STRIPALL-NEXT: Binding: Local +#STRIPALL-NEXT: Type: File +#STRIPALL-NEXT: Other: 0 +#STRIPALL-NEXT: Section: .text +#STRIPALL-NEXT: } +#STRIPALL-NEXT:] + +#STRIP: Symbols [ +#STRIP-NEXT: Symbol { +#STRIP-NEXT: Name: +#STRIP-NEXT: Value: 0x0 +#STRIP-NEXT: Size: 0 +#STRIP-NEXT: Binding: Local +#STRIP-NEXT: Type: None +#STRIP-NEXT: Other: 0 +#STRIP-NEXT: Section: Undefined +#STRIP-NEXT: } +#STRIP-NEXT: Symbol { +#STRIP-NEXT: Name: foo +#STRIP-NEXT: Value: 0x0 +#STRIP-NEXT: Size: 0 +#STRIP-NEXT: Binding: Local +#STRIP-NEXT: Type: File +#STRIP-NEXT: Other: 0 +#STRIP-NEXT: Section: .text +#STRIP-NEXT: } +#STRIP-NEXT: Symbol { +#STRIP-NEXT: Name: bar +#STRIP-NEXT: Value: 0x0 +#STRIP-NEXT: Size: 0 +#STRIP-NEXT: Binding: Global +#STRIP-NEXT: Type: Function +#STRIP-NEXT: Other: 0 +#STRIP-NEXT: Section: .text +#STRIP-NEXT: } +#STRIP-NEXT:] Index: tools/llvm-objcopy/ObjcopyOpts.td =================================================================== --- tools/llvm-objcopy/ObjcopyOpts.td +++ tools/llvm-objcopy/ObjcopyOpts.td @@ -92,3 +92,5 @@ HelpText<"Currently ignored. Only for compaitability with GNU objcopy.">; def strip_unneeded : Flag<["-", "--"], "strip-unneeded">, HelpText<"Remove all symbols not needed by relocations">; +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 @@ -174,6 +174,7 @@ bool Weaken = false; bool DiscardAll = false; bool OnlyKeepDebug = false; + bool KeepFileSymbols = false; }; using SectionPred = std::function; @@ -273,8 +274,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 && @@ -410,7 +412,8 @@ // and at least one of those symbols is present // (equivalently, the updated symbol table is not empty) // the symbol table and the string table should not be removed. - if (!Config.SymbolsToKeep.empty() && !Obj.SymbolTable->empty()) { + if ((!Config.SymbolsToKeep.empty() || Config.KeepFileSymbols) && + !Obj.SymbolTable->empty()) { RemovePred = [&Obj, RemovePred](const SectionBase &Sec) { if (&Sec == Obj.SymbolTable || &Sec == Obj.SymbolTable->getStrTab()) return false; @@ -531,6 +534,7 @@ Config.Weaken = InputArgs.hasArg(OBJCOPY_weaken); Config.DiscardAll = InputArgs.hasArg(OBJCOPY_discard_all); Config.OnlyKeepDebug = InputArgs.hasArg(OBJCOPY_only_keep_debug); + 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))