Export __progname even if a -dynamic-list is given.
authorRui Ueyama <ruiu@google.com>
Tue, 25 Apr 2017 00:15:48 +0000 (00:15 +0000)
committerRui Ueyama <ruiu@google.com>
Tue, 25 Apr 2017 00:15:48 +0000 (00:15 +0000)
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

lld/ELF/SymbolTable.cpp
lld/test/ELF/progname.s

index a22f35e..874e4ef 100644 (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
index a8837da..ac2f918 100644 (file)
 // 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