Skip to content

Commit

Permalink
[ELF] - Give automatically generated __start_* and __stop_* symbols d…
Browse files Browse the repository at this point in the history
…efault visibility.

This patch is opposite to D19024, which made this symbols to be hidden by default.

Unfortunately FreeBSD loader wants to see
start_set_modmetadata_set/stop_set_modmetadata_set in the dynamic symbol table. 
They were not placed there because had hidden visibility.

Patch makes them to have default visibility again.

Differential revision: https://reviews.llvm.org/D23552

llvm-svn: 279262
  • Loading branch information
George Rimar committed Aug 19, 2016
1 parent 9989f80 commit e1937bb
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 25 deletions.
4 changes: 2 additions & 2 deletions lld/ELF/LinkerScript.cpp
Expand Up @@ -51,8 +51,8 @@ static void addRegular(SymbolAssignment *Cmd) {
}

template <class ELFT> static void addSynthetic(SymbolAssignment *Cmd) {
Symbol *Sym = Symtab<ELFT>::X->addSynthetic(Cmd->Name, nullptr, 0);
Sym->Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
Symbol *Sym = Symtab<ELFT>::X->addSynthetic(
Cmd->Name, nullptr, 0, Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT);
Cmd->Sym = Sym->body();
}

Expand Down
8 changes: 4 additions & 4 deletions lld/ELF/SymbolTable.cpp
Expand Up @@ -418,12 +418,12 @@ Symbol *SymbolTable<ELFT>::addRegular(StringRef Name, uint8_t Binding,
template <typename ELFT>
Symbol *SymbolTable<ELFT>::addSynthetic(StringRef N,
OutputSectionBase<ELFT> *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<ELFT>(S, WasInserted, STB_GLOBAL);
if (Cmp > 0)
replaceBody<DefinedSynthetic<ELFT>>(S, N, Value, Section);
Expand Down
2 changes: 1 addition & 1 deletion lld/ELF/SymbolTable.h
Expand Up @@ -65,7 +65,7 @@ template <class ELFT> class SymbolTable {
InputSectionBase<ELFT> *Section);
Symbol *addRegular(StringRef Name, uint8_t Binding, uint8_t StOther);
Symbol *addSynthetic(StringRef N, OutputSectionBase<ELFT> *Section,
uintX_t Value);
uintX_t Value, uint8_t StOther);
void addShared(SharedFile<ELFT> *F, StringRef Name, const Elf_Sym &Sym,
const typename ELFT::Verdef *Verdef);

Expand Down
16 changes: 9 additions & 7 deletions lld/ELF/Writer.cpp
Expand Up @@ -519,15 +519,15 @@ static Symbol *addOptionalSynthetic(StringRef Name,
return nullptr;
if (!S->isUndefined() && !S->isShared())
return S->symbol();
return Symtab<ELFT>::X->addSynthetic(Name, Sec, Val);
return Symtab<ELFT>::X->addSynthetic(Name, Sec, Val, STV_HIDDEN);
}

template <class ELFT>
static void addSynthetic(StringRef Name, OutputSectionBase<ELFT> *Sec,
typename ELFT::uint Val) {
SymbolBody *S = Symtab<ELFT>::X->find(Name);
if (!S || S->isUndefined() || S->isShared())
Symtab<ELFT>::X->addSynthetic(Name, Sec, Val);
Symtab<ELFT>::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
Expand All @@ -554,7 +554,8 @@ template <class ELFT> void Writer<ELFT>::addReservedSymbols() {
// 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<ELFT>::X->addSynthetic("_gp", Out<ELFT>::Got, MipsGPOffset);
Symtab<ELFT>::X->addSynthetic("_gp", Out<ELFT>::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.
Expand Down Expand Up @@ -701,7 +702,8 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
// Even the author of gold doesn't remember why gold behaves that way.
// https://sourceware.org/ml/binutils/2002-03/msg00360.html
if (Out<ELFT>::DynSymTab)
Symtab<ELFT>::X->addSynthetic("_DYNAMIC", Out<ELFT>::Dynamic, 0);
Symtab<ELFT>::X->addSynthetic("_DYNAMIC", Out<ELFT>::Dynamic, 0,
STV_HIDDEN);

// Define __rel[a]_iplt_{start,end} symbols if needed.
addRelIpltSymbols();
Expand Down Expand Up @@ -893,11 +895,11 @@ void Writer<ELFT>::addStartStopSymbols(OutputSectionBase<ELFT> *Sec) {
StringRef Stop = Saver.save("__stop_" + S);
if (SymbolBody *B = Symtab<ELFT>::X->find(Start))
if (B->isUndefined())
Symtab<ELFT>::X->addSynthetic(Start, Sec, 0);
Symtab<ELFT>::X->addSynthetic(Start, Sec, 0, B->getVisibility());
if (SymbolBody *B = Symtab<ELFT>::X->find(Stop))
if (B->isUndefined())
Symtab<ELFT>::X->addSynthetic(Stop, Sec,
DefinedSynthetic<ELFT>::SectionEnd);
Symtab<ELFT>::X->addSynthetic(
Stop, Sec, DefinedSynthetic<ELFT>::SectionEnd, B->getVisibility());
}

template <class ELFT>
Expand Down
16 changes: 9 additions & 7 deletions lld/test/ELF/startstop-shared.s
Expand Up @@ -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
24 changes: 20 additions & 4 deletions lld/test/ELF/startstop.s
Expand Up @@ -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
Expand Down

0 comments on commit e1937bb

Please sign in to comment.