From ee5be798dae30d5f9414b01f76ff807edbc881aa Mon Sep 17 00:00:00 2001 From: Paul Semel Date: Fri, 27 Apr 2018 19:09:44 +0000 Subject: [PATCH] [llvm-objcopy] Add --globalize-symbol option llvm-svn: 331068 --- llvm/test/tools/llvm-objcopy/globalize.test | 75 +++++++++++++++++++++++++++++ llvm/tools/llvm-objcopy/Opts.td | 3 ++ llvm/tools/llvm-objcopy/llvm-objcopy.cpp | 11 +++-- 3 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 llvm/test/tools/llvm-objcopy/globalize.test diff --git a/llvm/test/tools/llvm-objcopy/globalize.test b/llvm/test/tools/llvm-objcopy/globalize.test new file mode 100644 index 0000000..5c9d62e --- /dev/null +++ b/llvm/test/tools/llvm-objcopy/globalize.test @@ -0,0 +1,75 @@ +# RUN: yaml2obj %s > %t +# RUN: llvm-objcopy --globalize-symbol Global --globalize-symbol Local --globalize-symbol 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 +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: Global +#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: Global +#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: Global +#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 6e71595..98a0856 100644 --- a/llvm/tools/llvm-objcopy/Opts.td +++ b/llvm/tools/llvm-objcopy/Opts.td @@ -64,3 +64,6 @@ defm localize_symbol : Eq<"localize-symbol">, HelpText<"Mark as local">; def L : JoinedOrSeparate<["-"], "L">, Alias; +defm globalize_symbol : Eq<"globalize-symbol">, + MetaVarName<"symbol">, + HelpText<"Mark as global">; diff --git a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp index a4b38cf..59b0782 100644 --- a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp +++ b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp @@ -119,6 +119,7 @@ struct CopyConfig { std::vector OnlyKeep; std::vector AddSection; std::vector SymbolsToLocalize; + std::vector SymbolsToGlobalize; StringMap SymbolsToRename; bool StripAll; bool StripAllGNU; @@ -320,11 +321,13 @@ void HandleArgs(const CopyConfig &Config, Object &Obj, const Reader &Reader, if ((Config.LocalizeHidden && (Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) || (!Config.SymbolsToLocalize.empty() && - std::find(std::begin(Config.SymbolsToLocalize), - std::end(Config.SymbolsToLocalize), - Sym.Name) != std::end(Config.SymbolsToLocalize))) + is_contained(Config.SymbolsToLocalize, Sym.Name))) Sym.Binding = STB_LOCAL; + if (!Config.SymbolsToGlobalize.empty() && + is_contained(Config.SymbolsToGlobalize, Sym.Name)) + Sym.Binding = STB_GLOBAL; + const auto I = Config.SymbolsToRename.find(Sym.Name); if (I != Config.SymbolsToRename.end()) Sym.Name = I->getValue(); @@ -416,6 +419,8 @@ CopyConfig ParseObjcopyOptions(ArrayRef ArgsArr) { Config.LocalizeHidden = InputArgs.hasArg(OBJCOPY_localize_hidden); for (auto Arg : InputArgs.filtered(OBJCOPY_localize_symbol)) Config.SymbolsToLocalize.push_back(Arg->getValue()); + for (auto Arg : InputArgs.filtered(OBJCOPY_globalize_symbol)) + Config.SymbolsToGlobalize.push_back(Arg->getValue()); return Config; } -- 2.7.4