Export __progname even if a -dynamic-list is given.

BSD's __progname symbol is defined in crt1.o and linked against main
executables. The libc expects that main executables export __progname
symbol via .dynsym sections. In order to handle this case, we scan
undefined symbols in DSOs and exported them by setting Sym->ExportDynamic
to true.

But it turned out that setting that variable is not enough to make sure
that symbols are exported in all use cases. If a -dynamic-list option is
given, all symbols not explicitly mentioned in a version script are
hidden by default. That hides __progname symbol. This patch fixes the issue.

Fixes https://bugs.llvm.org/show_bug.cgi?id=32703

llvm-svn: 301282
This commit is contained in:
Rui Ueyama 2017-04-25 00:15:48 +00:00
parent bbebcb6c4d
commit 321b9cd072
2 changed files with 18 additions and 5 deletions

View File

@ -548,11 +548,20 @@ template <class ELFT> void SymbolTable<ELFT>::scanUndefinedFlags() {
// shared libraries can find them.
// Except this, we ignore undefined symbols in DSOs.
template <class ELFT> void SymbolTable<ELFT>::scanShlibUndefined() {
for (SharedFile<ELFT> *File : SharedFiles)
for (StringRef U : File->getUndefinedSymbols())
if (SymbolBody *Sym = find(U))
if (Sym->isDefined())
Sym->symbol()->ExportDynamic = true;
for (SharedFile<ELFT> *File : SharedFiles) {
for (StringRef U : File->getUndefinedSymbols()) {
SymbolBody *Sym = find(U);
if (!Sym || !Sym->isDefined())
continue;
Sym->symbol()->ExportDynamic = true;
// If -dynamic-list is given, the default version is set to
// VER_NDX_LOCAL, which prevents a symbol to be exported via .dynsym.
// Set to VER_NDX_GLOBAL so the symbol will be handled as if it were
// specified by -dynamic-list.
Sym->symbol()->VersionId = VER_NDX_GLOBAL;
}
}
}
// Initialize DemangledSyms with a map from demangled symbols to symbol

View File

@ -12,6 +12,10 @@
// RUN: ld.lld -o %t %t.o %t.so
// RUN: llvm-readobj -dyn-symbols %t | FileCheck %s
// RUN: echo "{_start;};" > %t.dynlist
// RUN: ld.lld -dynamic-list %t.dynlist -o %t %t.o %t.so
// RUN: llvm-readobj -dyn-symbols %t | FileCheck %s
// CHECK: Name: __progname@
// CHECK-NEXT: Value: 0x201000
// CHECK-NEXT: Size: 0