[llvm-objcopy] Don't apply --localize flags to common symbols

Summary:
--localize-symbol and --localize-hidden will currently localize common symbols. GNU objcopy will not localize these symbols even when explicitly requested, which seems reasonable; common symbols should always be global so they can be merged during linking.

See PR39461

Reviewers: jakehehrlich, jhenderson, alexshap, MaskRay, espindola

Reviewed By: jakehehrlich, jhenderson, alexshap, MaskRay

Subscribers: emaste, arichardson, alexshap, MaskRay, llvm-commits

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

llvm-svn: 345856
This commit is contained in:
Jordan Rupprecht 2018-11-01 17:26:36 +00:00
parent ab205a31d4
commit b47475c058
5 changed files with 45 additions and 5 deletions

View File

@ -55,6 +55,12 @@ Symbols:
Value: 0x2006 Value: 0x2006
Size: 2 Size: 2
Visibility: STV_HIDDEN Visibility: STV_HIDDEN
- Name: hiddenGlobalCommon
Type: STT_OBJECT
Index: SHN_COMMON
Value: 0x2006
Size: 2
Visibility: STV_HIDDEN
- Name: undefGlobal - Name: undefGlobal
Type: STT_FUNC Type: STT_FUNC
Size: 8 Size: 8
@ -142,6 +148,17 @@ Symbols:
#CHECK-NEXT: Section: .text #CHECK-NEXT: Section: .text
#CHECK-NEXT: } #CHECK-NEXT: }
#CHECK-NEXT: Symbol { #CHECK-NEXT: Symbol {
#CHECK-NEXT: Name: hiddenGlobalCommon
#CHECK-NEXT: Value: 0x2006
#CHECK-NEXT: Size: 2
#CHECK-NEXT: Binding: Global
#CHECK-NEXT: Type: Object
#CHECK-NEXT: Other [
#CHECK-NEXT: STV_HIDDEN
#CHECK-NEXT: ]
#CHECK-NEXT: Section: Common (0xF
#CHECK-NEXT: }
#CHECK-NEXT: Symbol {
#CHECK-NEXT: Name: undefGlobal #CHECK-NEXT: Name: undefGlobal
#CHECK-NEXT: Value: 0x0 #CHECK-NEXT: Value: 0x0
#CHECK-NEXT: Size: 8 #CHECK-NEXT: Size: 8

View File

@ -1,5 +1,10 @@
# RUN: yaml2obj %s > %t # RUN: yaml2obj %s > %t
# RUN: llvm-objcopy --localize-symbol Global -L Local -L Weak %t %t2 # RUN: llvm-objcopy \
# RUN: --localize-symbol Global \
# RUN: -L Local \
# RUN: -L Weak \
# RUN: -L GlobalCommon \
# RUN: %t %t2
# RUN: llvm-readobj -symbols %t2 | FileCheck %s # RUN: llvm-readobj -symbols %t2 | FileCheck %s
!ELF !ELF
@ -40,6 +45,11 @@ Symbols:
Size: 8 Size: 8
Section: .text Section: .text
Value: 0x1010 Value: 0x1010
- Name: GlobalCommon
Type: STT_OBJECT
Index: SHN_COMMON
Value: 0x2006
Size: 2
#CHECK: Symbols [ #CHECK: Symbols [
#CHECK-NEXT: Symbol { #CHECK-NEXT: Symbol {
@ -78,4 +88,13 @@ Symbols:
#CHECK-NEXT: Other: 0 #CHECK-NEXT: Other: 0
#CHECK-NEXT: Section: .text #CHECK-NEXT: Section: .text
#CHECK-NEXT: } #CHECK-NEXT: }
#CHECK-NEXT: Symbol {
#CHECK-NEXT: Name: GlobalCommon
#CHECK-NEXT: Value: 0x2006
#CHECK-NEXT: Size: 2
#CHECK-NEXT: Binding: Global
#CHECK-NEXT: Type: Object
#CHECK-NEXT: Other: 0
#CHECK-NEXT: Section: Common (0xF
#CHECK-NEXT: }
#CHECK-NEXT:] #CHECK-NEXT:]

View File

@ -213,10 +213,11 @@ static void handleArgs(const CopyConfig &Config, Object &Obj,
// them. // them.
if (Obj.SymbolTable) { if (Obj.SymbolTable) {
Obj.SymbolTable->updateSymbols([&](Symbol &Sym) { Obj.SymbolTable->updateSymbols([&](Symbol &Sym) {
if ((Config.LocalizeHidden && if (!Sym.isCommon() &&
((Config.LocalizeHidden &&
(Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) || (Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) ||
(!Config.SymbolsToLocalize.empty() && (!Config.SymbolsToLocalize.empty() &&
is_contained(Config.SymbolsToLocalize, Sym.Name))) is_contained(Config.SymbolsToLocalize, Sym.Name))))
Sym.Binding = STB_LOCAL; Sym.Binding = STB_LOCAL;
// Note: these two globalize flags have very similar names but different // Note: these two globalize flags have very similar names but different

View File

@ -332,6 +332,8 @@ uint16_t Symbol::getShndx() const {
llvm_unreachable("Symbol with invalid ShndxType encountered"); llvm_unreachable("Symbol with invalid ShndxType encountered");
} }
bool Symbol::isCommon() const { return getShndx() == SHN_COMMON; }
void SymbolTableSection::assignIndices() { void SymbolTableSection::assignIndices() {
uint32_t Index = 0; uint32_t Index = 0;
for (auto &Sym : Symbols) for (auto &Sym : Symbols)

View File

@ -415,6 +415,7 @@ struct Symbol {
bool Referenced = false; bool Referenced = false;
uint16_t getShndx() const; uint16_t getShndx() const;
bool isCommon() const;
}; };
class SectionIndexSection : public SectionBase { class SectionIndexSection : public SectionBase {