Index: test/tools/llvm-objcopy/keep-file-symbols.test =================================================================== --- /dev/null +++ test/tools/llvm-objcopy/keep-file-symbols.test @@ -0,0 +1,45 @@ +# RUN: yaml2obj %s > %t +# RUN: llvm-objcopy --strip-all --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 + - Name: bar + Type: STT_FUNC + Section: .text + Weak: + - Name: baz + Type: STT_FUNC + Section: .text + Global: + - Name: foobar + Type: STT_FUNC + Section: .text + +#CHECK: Symbols [ +#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 @@ -90,3 +90,5 @@ Alias; def only_keep_debug : Flag<["-", "--"], "only-keep-debug">, HelpText<"Currently ignored. Only for compaitability with GNU objcopy.">; +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 @@ -173,6 +173,7 @@ bool Weaken = false; bool DiscardAll = false; bool OnlyKeepDebug = false; + bool KeepFileSymbols = false; }; using SectionPred = std::function; @@ -264,8 +265,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 && @@ -394,7 +396,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; @@ -514,6 +517,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))