Index: test/tools/llvm-objcopy/localize-hidden.test =================================================================== --- test/tools/llvm-objcopy/localize-hidden.test +++ test/tools/llvm-objcopy/localize-hidden.test @@ -6,7 +6,7 @@ FileHeader: Class: ELFCLASS64 Data: ELFDATA2LSB - Type: ET_REL + Type: ET_EXEC Machine: EM_X86_64 Sections: - Name: .text @@ -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 + Section: .data + 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 (0xF +#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 (0x{{[^F]}} +#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,12 +1,18 @@ # 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: -L GlobalSttCommon \ +# RUN: %t %t2 # RUN: llvm-readobj -symbols %t2 | FileCheck %s !ELF FileHeader: Class: ELFCLASS64 Data: ELFDATA2LSB - Type: ET_REL + Type: ET_EXEC Machine: EM_X86_64 Sections: - Name: .text @@ -40,6 +46,16 @@ Size: 8 Section: .text Value: 0x1010 + - Name: GlobalCommon + Type: STT_OBJECT + Index: SHN_COMMON + Value: 0x2006 + Size: 2 + - Name: GlobalSttCommon + Type: STT_COMMON + Section: .data + Value: 0x2006 + Size: 2 #CHECK: Symbols [ #CHECK-NEXT: Symbol { @@ -78,4 +94,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 (0xF +#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 (0x{{[^F]}} +#CHECK-NEXT: } #CHECK-NEXT:] Index: tools/llvm-objcopy/ELF/ELFObjcopy.cpp =================================================================== --- tools/llvm-objcopy/ELF/ELFObjcopy.cpp +++ tools/llvm-objcopy/ELF/ELFObjcopy.cpp @@ -213,10 +213,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 Index: tools/llvm-objcopy/ELF/Object.h =================================================================== --- tools/llvm-objcopy/ELF/Object.h +++ tools/llvm-objcopy/ELF/Object.h @@ -415,6 +415,7 @@ bool Referenced = false; uint16_t getShndx() const; + bool isCommon() const; }; class SectionIndexSection : public SectionBase { Index: tools/llvm-objcopy/ELF/Object.cpp =================================================================== --- tools/llvm-objcopy/ELF/Object.cpp +++ tools/llvm-objcopy/ELF/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)