[llvm-objcopy] Add --keep-file-symbols option

This option prevent from removing file symbols while removing symbols.

Differential Revision: https://reviews.llvm.org/D46830

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@333339 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Paul Semel 2018-05-26 08:10:37 +00:00
parent 2a70cfb61f
commit 5af80c626e
3 changed files with 79 additions and 3 deletions

View File

@ -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:]

View File

@ -92,3 +92,5 @@ def only_keep_debug : Flag<["-", "--"], "only-keep-debug">,
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">;

View File

@ -174,6 +174,7 @@ struct CopyConfig {
bool Weaken = false;
bool DiscardAll = false;
bool OnlyKeepDebug = false;
bool KeepFileSymbols = false;
};
using SectionPred = std::function<bool(const SectionBase &Sec)>;
@ -273,8 +274,9 @@ void HandleArgs(const CopyConfig &Config, Object &Obj, const Reader &Reader,
}
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 @@ void HandleArgs(const CopyConfig &Config, Object &Obj, const Reader &Reader,
// 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 @@ CopyConfig ParseObjcopyOptions(ArrayRef<const char *> ArgsArr) {
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))