Index: test/tools/llvm-objcopy/localize-hidden.test =================================================================== --- test/tools/llvm-objcopy/localize-hidden.test +++ test/tools/llvm-objcopy/localize-hidden.test @@ -55,6 +55,18 @@ Value: 0x2006 Size: 2 Visibility: STV_HIDDEN + - Name: hiddenGlobalCommon + Type: STT_OBJECT + Index: SHN_COMMON + Value: 0x2006 + Size: 2 + Visibility: STV_HIDDEN + - Name: hiddenGlobalSttCommon + Type: STT_COMMON + Index: SHN_COMMON + Value: 0x2006 + Size: 2 + Visibility: STV_HIDDEN - Name: undefGlobal Type: STT_FUNC Size: 8 @@ -142,6 +154,28 @@ #CHECK-NEXT: Section: .text #CHECK-NEXT: } #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 +#CHECK-NEXT: } +#CHECK-NEXT: Symbol { +#CHECK-NEXT: Name: hiddenGlobalSttCommon +#CHECK-NEXT: Value: 0x2006 +#CHECK-NEXT: Size: 2 +#CHECK-NEXT: Binding: Global +#CHECK-NEXT: Type: Common +#CHECK-NEXT: Other [ +#CHECK-NEXT: STV_HIDDEN +#CHECK-NEXT: ] +#CHECK-NEXT: Section: Common +#CHECK-NEXT: } +#CHECK-NEXT: Symbol { #CHECK-NEXT: Name: undefGlobal #CHECK-NEXT: Value: 0x0 #CHECK-NEXT: Size: 8 Index: test/tools/llvm-objcopy/localize.test =================================================================== --- test/tools/llvm-objcopy/localize.test +++ test/tools/llvm-objcopy/localize.test @@ -1,5 +1,5 @@ # RUN: yaml2obj %s > %t -# RUN: llvm-objcopy --localize-symbol Global -L Local -L Weak %t %t2 +# RUN: llvm-objcopy --localize-symbol Global -L Local -L Weak -L GlobalCommon %t %t2 # RUN: llvm-readobj -symbols %t2 | FileCheck %s !ELF @@ -40,6 +40,16 @@ Size: 8 Section: .text Value: 0x1010 + - Name: GlobalCommon + Type: STT_OBJECT + Index: SHN_COMMON + Value: 0x2006 + Size: 2 + - Name: GlobalSttCommon + Type: STT_COMMON + Index: SHN_COMMON + Value: 0x2006 + Size: 2 #CHECK: Symbols [ #CHECK-NEXT: Symbol { @@ -78,4 +88,22 @@ #CHECK-NEXT: Other: 0 #CHECK-NEXT: Section: .text #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 +#CHECK-NEXT: } +#CHECK-NEXT: Symbol { +#CHECK-NEXT: Name: GlobalSttCommon +#CHECK-NEXT: Value: 0x2006 +#CHECK-NEXT: Size: 2 +#CHECK-NEXT: Binding: Global +#CHECK-NEXT: Type: Common +#CHECK-NEXT: Other: 0 +#CHECK-NEXT: Section: Common +#CHECK-NEXT: } #CHECK-NEXT:] Index: tools/llvm-objcopy/Object.h =================================================================== --- tools/llvm-objcopy/Object.h +++ tools/llvm-objcopy/Object.h @@ -415,6 +415,7 @@ bool Referenced = false; uint16_t getShndx() const; + bool isCommon() const; }; class SectionIndexSection : public SectionBase { Index: tools/llvm-objcopy/Object.cpp =================================================================== --- tools/llvm-objcopy/Object.cpp +++ tools/llvm-objcopy/Object.cpp @@ -332,6 +332,10 @@ llvm_unreachable("Symbol with invalid ShndxType encountered"); } +bool Symbol::isCommon() const { + return getShndx() == SHN_COMMON || Type == STT_COMMON; +} + void SymbolTableSection::assignIndices() { uint32_t Index = 0; for (auto &Sym : Symbols) Index: tools/llvm-objcopy/llvm-objcopy.cpp =================================================================== --- tools/llvm-objcopy/llvm-objcopy.cpp +++ tools/llvm-objcopy/llvm-objcopy.cpp @@ -256,10 +256,11 @@ // them. if (Obj.SymbolTable) { Obj.SymbolTable->updateSymbols([&](Symbol &Sym) { - if ((Config.LocalizeHidden && - (Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) || - (!Config.SymbolsToLocalize.empty() && - is_contained(Config.SymbolsToLocalize, Sym.Name))) + if (!Sym.isCommon() && + ((Config.LocalizeHidden && + (Sym.Visibility == STV_HIDDEN || Sym.Visibility == STV_INTERNAL)) || + (!Config.SymbolsToLocalize.empty() && + is_contained(Config.SymbolsToLocalize, Sym.Name)))) Sym.Binding = STB_LOCAL; // Note: these two globalize flags have very similar names but different