Index: lld/trunk/ELF/LinkerScript.cpp =================================================================== --- lld/trunk/ELF/LinkerScript.cpp +++ lld/trunk/ELF/LinkerScript.cpp @@ -51,8 +51,8 @@ } template static void addSynthetic(SymbolAssignment *Cmd) { - Symbol *Sym = Symtab::X->addSynthetic(Cmd->Name, nullptr, 0); - Sym->Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT; + Symbol *Sym = Symtab::X->addSynthetic( + Cmd->Name, nullptr, 0, Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT); Cmd->Sym = Sym->body(); } Index: lld/trunk/ELF/SymbolTable.h =================================================================== --- lld/trunk/ELF/SymbolTable.h +++ lld/trunk/ELF/SymbolTable.h @@ -65,7 +65,7 @@ InputSectionBase *Section); Symbol *addRegular(StringRef Name, uint8_t Binding, uint8_t StOther); Symbol *addSynthetic(StringRef N, OutputSectionBase *Section, - uintX_t Value); + uintX_t Value, uint8_t StOther); void addShared(SharedFile *F, StringRef Name, const Elf_Sym &Sym, const typename ELFT::Verdef *Verdef); Index: lld/trunk/ELF/SymbolTable.cpp =================================================================== --- lld/trunk/ELF/SymbolTable.cpp +++ lld/trunk/ELF/SymbolTable.cpp @@ -418,12 +418,12 @@ template Symbol *SymbolTable::addSynthetic(StringRef N, OutputSectionBase *Section, - uintX_t Value) { + uintX_t Value, uint8_t StOther) { Symbol *S; bool WasInserted; - std::tie(S, WasInserted) = - insert(N, STT_NOTYPE, STV_HIDDEN, /*CanOmitFromDynSym*/ false, - /*IsUsedInRegularObj*/ true, nullptr); + std::tie(S, WasInserted) = insert(N, STT_NOTYPE, /*Visibility*/ StOther & 0x3, + /*CanOmitFromDynSym*/ false, + /*IsUsedInRegularObj*/ true, nullptr); int Cmp = compareDefinedNonCommon(S, WasInserted, STB_GLOBAL); if (Cmp > 0) replaceBody>(S, N, Value, Section); Index: lld/trunk/ELF/Writer.cpp =================================================================== --- lld/trunk/ELF/Writer.cpp +++ lld/trunk/ELF/Writer.cpp @@ -519,7 +519,7 @@ return nullptr; if (!S->isUndefined() && !S->isShared()) return S->symbol(); - return Symtab::X->addSynthetic(Name, Sec, Val); + return Symtab::X->addSynthetic(Name, Sec, Val, STV_HIDDEN); } template @@ -527,7 +527,7 @@ typename ELFT::uint Val) { SymbolBody *S = Symtab::X->find(Name); if (!S || S->isUndefined() || S->isShared()) - Symtab::X->addSynthetic(Name, Sec, Val); + Symtab::X->addSynthetic(Name, Sec, Val, STV_HIDDEN); } // The beginning and the ending of .rel[a].plt section are marked // with __rel[a]_iplt_{start,end} symbols if it is a statically linked @@ -554,7 +554,8 @@ // so that it points to an absolute address which is relative to GOT. // See "Global Data Symbols" in Chapter 6 in the following document: // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf - Symtab::X->addSynthetic("_gp", Out::Got, MipsGPOffset); + Symtab::X->addSynthetic("_gp", Out::Got, MipsGPOffset, + STV_HIDDEN); // On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between // start of function and 'gp' pointer into GOT. @@ -701,7 +702,8 @@ // Even the author of gold doesn't remember why gold behaves that way. // https://sourceware.org/ml/binutils/2002-03/msg00360.html if (Out::DynSymTab) - Symtab::X->addSynthetic("_DYNAMIC", Out::Dynamic, 0); + Symtab::X->addSynthetic("_DYNAMIC", Out::Dynamic, 0, + STV_HIDDEN); // Define __rel[a]_iplt_{start,end} symbols if needed. addRelIpltSymbols(); @@ -893,11 +895,11 @@ StringRef Stop = Saver.save("__stop_" + S); if (SymbolBody *B = Symtab::X->find(Start)) if (B->isUndefined()) - Symtab::X->addSynthetic(Start, Sec, 0); + Symtab::X->addSynthetic(Start, Sec, 0, B->getVisibility()); if (SymbolBody *B = Symtab::X->find(Stop)) if (B->isUndefined()) - Symtab::X->addSynthetic(Stop, Sec, - DefinedSynthetic::SectionEnd); + Symtab::X->addSynthetic( + Stop, Sec, DefinedSynthetic::SectionEnd, B->getVisibility()); } template Index: lld/trunk/test/ELF/startstop-shared.s =================================================================== --- lld/trunk/test/ELF/startstop-shared.s +++ lld/trunk/test/ELF/startstop-shared.s @@ -6,21 +6,23 @@ .data .quad __start_foo .section foo,"aw" -// By default the symbol is hidden. -// CHECK: R_X86_64_RELATIVE - 0x[[ADDR1:.*]] .hidden __start_bar .quad __start_bar .section bar,"a" -// References do not affect the visibility. -// CHECK: R_X86_64_RELATIVE - 0x[[ADDR2:.*]] + +// Test that we are able to hide the symbol. +// CHECK: R_X86_64_RELATIVE - 0x[[ADDR:.*]] + +// By default the symbol is visible and we need a dynamic reloc. +// CHECK: R_X86_64_64 __start_foo 0x0 // CHECK: Name: __start_bar -// CHECK-NEXT: Value: 0x[[ADDR2]] +// CHECK-NEXT: Value: 0x[[ADDR]] // CHECK-NEXT: Size: // CHECK-NEXT: Binding: Local // CHECK: Name: __start_foo -// CHECK-NEXT: Value: 0x[[ADDR1]] +// CHECK-NEXT: Value: // CHECK-NEXT: Size: -// CHECK-NEXT: Binding: Local +// CHECK-NEXT: Binding: Global Index: lld/trunk/test/ELF/startstop.s =================================================================== --- lld/trunk/test/ELF/startstop.s +++ lld/trunk/test/ELF/startstop.s @@ -22,30 +22,46 @@ // SYMBOL: Relocations [ // SYMBOL-NEXT: Section ({{.*}}) .rela.dyn { -// SYMBOL-NEXT: 0x3000 R_X86_64_RELATIVE - 0x3020 -// SYMBOL-NEXT: 0x3008 R_X86_64_RELATIVE - 0x3021 -// SYMBOL-NEXT: 0x3010 R_X86_64_RELATIVE - 0x3010 -// SYMBOL-NEXT: 0x3018 R_X86_64_RELATIVE - 0x3011 +// SYMBOL-NEXT: 0x3010 R_X86_64_64 __stop_zed1 0x0 +// SYMBOL-NEXT: 0x3018 R_X86_64_64 __stop_zed1 0x1 +// SYMBOL-NEXT: 0x3000 R_X86_64_64 __stop_zed2 0x0 +// SYMBOL-NEXT: 0x3008 R_X86_64_64 __stop_zed2 0x1 // SYMBOL-NEXT: } // SYMBOL-NEXT: ] // SYMBOL: Symbol { // SYMBOL: Name: __start_bar // SYMBOL: Value: 0x1012 +// SYMBOL: STV_HIDDEN // SYMBOL: Section: bar // SYMBOL: } // SYMBOL-NOT: Section: __stop_bar // SYMBOL: Symbol { // SYMBOL: Name: __start_foo // SYMBOL: Value: 0x100F +// SYMBOL: STV_HIDDEN // SYMBOL: Section: foo // SYMBOL: } // SYMBOL: Symbol { // SYMBOL: Name: __stop_foo // SYMBOL: Value: 0x1012 +// STMBOL: STV_HIDDEN // SYMBOL: Section: foo // SYMBOL: } +// SYMBOL: Symbol { +// SYMBOL: Name: __stop_zed1 +// SYMBOL: Value: 0x3010 +// STMBOL: Other: 0 +// SYMBOL: Section: zed1 +// SYMBOL: } +// SYMBOL: Symbol { +// SYMBOL: Name: __stop_zed2 +// SYMBOL: Value: 0x3020 +// STMBOL: Other: 0 +// SYMBOL: Section: zed2 +// SYMBOL: } + .hidden __start_foo .hidden __stop_foo .hidden __start_bar