From: Paul Semel Date: Thu, 26 Apr 2018 17:44:43 +0000 (+0000) Subject: [llvm-objcopy] Add --localize-symbol option X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b492494c0f1437e8fe7d9350a3ce4f7fad7072b7;p=platform%2Fupstream%2Fllvm.git [llvm-objcopy] Add --localize-symbol option llvm-svn: 330963 --- diff --git a/llvm/test/tools/llvm-objcopy/localize.test b/llvm/test/tools/llvm-objcopy/localize.test new file mode 100644 index 0000000..d52852a --- /dev/null +++ b/llvm/test/tools/llvm-objcopy/localize.test @@ -0,0 +1,81 @@ +# RUN: yaml2obj %s > %t +# RUN: llvm-objcopy --localize-symbol Global -L Local -L Weak %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 + - Name: .data + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC ] + Address: 0x2000 + AddressAlign: 0x0000000000000010 + Content: "0000000000000000" +Symbols: + Local: + - Name: Local + Type: STT_FUNC + Section: .text + Value: 0x1000 + Size: 8 + Weak: + - Name: Weak + Type: STT_FUNC + Size: 8 + Section: .text + Value: 0x1008 + Global: + - Name: Global + Type: STT_FUNC + Size: 8 + Section: .text + Value: 0x1010 + +#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: Local +#CHECK-NEXT: Value: 0x1000 +#CHECK-NEXT: Size: 8 +#CHECK-NEXT: Binding: Local +#CHECK-NEXT: Type: Function +#CHECK-NEXT: Other: 0 +#CHECK-NEXT: Section: .text +#CHECK-NEXT: } +#CHECK-NEXT: Symbol { +#CHECK-NEXT: Name: Global +#CHECK-NEXT: Value: 0x1010 +#CHECK-NEXT: Size: 8 +#CHECK-NEXT: Binding: Local +#CHECK-NEXT: Type: Function +#CHECK-NEXT: Other: 0 +#CHECK-NEXT: Section: .text +#CHECK-NEXT: } +#CHECK-NEXT: Symbol { +#CHECK-NEXT: Name: Weak +#CHECK-NEXT: Value: 0x1008 +#CHECK-NEXT: Size: 8 +#CHECK-NEXT: Binding: Local +#CHECK-NEXT: Type: Function +#CHECK-NEXT: Other: 0 +#CHECK-NEXT: Section: .text +#CHECK-NEXT: } +#CHECK-NEXT:] diff --git a/llvm/tools/llvm-objcopy/Opts.td b/llvm/tools/llvm-objcopy/Opts.td index 15723dc..a4d864a 100644 --- a/llvm/tools/llvm-objcopy/Opts.td +++ b/llvm/tools/llvm-objcopy/Opts.td @@ -56,3 +56,8 @@ def extract_dwo : Flag<["-", "--"], "extract-dwo">, HelpText<"Remove all sections that are not DWARF .dwo sections from file">; def localize_hidden : Flag<["-", "--"], "localize-hidden">, HelpText<"Mark all symbols that have hidden or internal visibility as local">; +defm localize_symbol : Eq<"localize-symbol">, + MetaVarName<"symbol">, + HelpText<"Mark as local">; +def L : JoinedOrSeparate<["-"], "L">, + Alias; diff --git a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp index 1fa8762..7158cee 100644 --- a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp +++ b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp @@ -118,6 +118,7 @@ struct CopyConfig { std::vector Keep; std::vector OnlyKeep; std::vector AddSection; + std::vector LocalizeSymbol; bool StripAll; bool StripAllGNU; bool StripDebug; @@ -129,6 +130,7 @@ struct CopyConfig { }; using SectionPred = std::function; +using SymbolPred = std::function; bool IsDWOSection(const SectionBase &Sec) { return Sec.Name.endswith(".dwo"); } @@ -188,14 +190,27 @@ void HandleArgs(const CopyConfig &Config, Object &Obj, const Reader &Reader, SplitDWOToFile(Config, Reader, Config.SplitDWO, OutputElfType); } + SymbolPred LocalizePred = [](const Symbol &) { return false; }; + // Localize: if (Config.LocalizeHidden) { - Obj.SymbolTable->localize([](const Symbol &Sym) { + LocalizePred = [](const Symbol &Sym) { return Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL; - }); + }; + } + + if (!Config.LocalizeSymbol.empty()) { + LocalizePred = [LocalizePred, &Config](const Symbol &Sym) { + return LocalizePred(Sym) || + std::find(std::begin(Config.LocalizeSymbol), + std::end(Config.LocalizeSymbol), + Sym.Name) != std::end(Config.LocalizeSymbol); + }; } + Obj.SymbolTable->localize(LocalizePred); + SectionPred RemovePred = [](const SectionBase &) { return false; }; // Removes: @@ -398,6 +413,8 @@ CopyConfig ParseObjcopyOptions(ArrayRef ArgsArr) { Config.StripNonAlloc = InputArgs.hasArg(OBJCOPY_strip_non_alloc); Config.ExtractDWO = InputArgs.hasArg(OBJCOPY_extract_dwo); Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden); + for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol)) + Config.LocalizeSymbol.push_back(Arg->getValue()); return Config; }