[lld] r329960 - Do not keep shared symbols created from garbage-collected eliminated DSOs.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 12 14:57:04 PDT 2018


Author: ruiu
Date: Thu Apr 12 14:57:04 2018
New Revision: 329960

URL: http://llvm.org/viewvc/llvm-project?rev=329960&view=rev
Log:
Do not keep shared symbols created from garbage-collected eliminated DSOs.

If all references to a DSO happen to be weak, and if the DSO is
specified with --as-needed, the DSO is not added to DT_NEEDED.
If that happens, we also need to eliminate shared symbols created
from the DSO. Otherwise, they become dangling references that point
to non-exsitent DSO.

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

Differential Revision: https://reviews.llvm.org/D45536

Added:
    lld/trunk/test/ELF/as-needed-weak.s
Modified:
    lld/trunk/ELF/Driver.cpp
    lld/trunk/test/ELF/gc-sections-shared.s

Modified: lld/trunk/ELF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.cpp?rev=329960&r1=329959&r2=329960&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.cpp (original)
+++ lld/trunk/ELF/Driver.cpp Thu Apr 12 14:57:04 2018
@@ -1059,6 +1059,18 @@ template <class ELFT> static void handle
     Symtab->fetchLazy<ELFT>(Sym);
 }
 
+// If all references to a DSO happen to be weak, the DSO is not added
+// to DT_NEEDED. If that happens, we need to eliminate shared symbols
+// created from the DSO. Otherwise, they become dangling references
+// that point to a non-existent DSO.
+template <class ELFT> static void demoteSharedSymbols() {
+  for (Symbol *Sym : Symtab->getSymbols())
+    if (auto *S = dyn_cast<SharedSymbol>(Sym))
+      if (!S->getFile<ELFT>().IsNeeded)
+        replaceSymbol<Undefined>(S, nullptr, S->getName(), STB_WEAK, S->StOther,
+                                 S->Type);
+}
+
 // Do actual linking. Note that when this function is called,
 // all linker scripts have already been parsed.
 template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
@@ -1219,6 +1231,7 @@ template <class ELFT> void LinkerDriver:
   // Do size optimizations: garbage collection, merging of SHF_MERGE sections
   // and identical code folding.
   markLive<ELFT>();
+  demoteSharedSymbols<ELFT>();
   decompressSections();
   mergeSections();
   if (Config->ICF)

Added: lld/trunk/test/ELF/as-needed-weak.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/as-needed-weak.s?rev=329960&view=auto
==============================================================================
--- lld/trunk/test/ELF/as-needed-weak.s (added)
+++ lld/trunk/test/ELF/as-needed-weak.s Thu Apr 12 14:57:04 2018
@@ -0,0 +1,22 @@
+# REQUIRES: x86
+
+# RUN: echo '.globl foo; .type foo, @function; foo:' | \
+# RUN:   llvm-mc -filetype=obj -triple=x86_64-unknown-linux - -o %t1.o
+# RUN: ld.lld -shared -o %t1.so -soname libfoo %t1.o
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t2.o
+# RUN: ld.lld -o %t.exe %t2.o --as-needed %t1.so
+# RUN: llvm-readelf -dynamic-table -dyn-symbols %t.exe | FileCheck %s
+
+# CHECK-NOT: libfoo
+
+# CHECK:      Symbol table of .hash for image:
+# CHECK-NEXT: Num Buc:    Value          Size   Type   Bind Vis      Ndx Name
+# CHECK-NEXT:   1   1: 0000000000000000     0 FUNC    WEAK   DEFAULT UND foo@
+
+.globl _start
+.weak foo
+
+_start:
+  mov $foo, %eax
+  callq foo

Modified: lld/trunk/test/ELF/gc-sections-shared.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/gc-sections-shared.s?rev=329960&r1=329959&r2=329960&view=diff
==============================================================================
--- lld/trunk/test/ELF/gc-sections-shared.s (original)
+++ lld/trunk/test/ELF/gc-sections-shared.s Thu Apr 12 14:57:04 2018
@@ -9,11 +9,6 @@
 # RUN: ld.lld --gc-sections --export-dynamic-symbol foo -o %t %t.o --as-needed %t2.so %t3.so %t4.so
 # RUN: llvm-readobj --dynamic-table --dyn-symbols %t | FileCheck %s
 
-# This test the property that we have a needed line for every undefined.
-# It would also be OK to keep bar2 and the need for %t2.so
-# At the same time, weak symbols should not cause adding DT_NEEDED;
-# this case is checked with symbol qux and %t4.so.
-
 # CHECK:      DynamicSymbols [
 # CHECK-NEXT:   Symbol {
 # CHECK-NEXT:     Name:
@@ -51,15 +46,6 @@
 # CHECK-NEXT:     Other:
 # CHECK-NEXT:     Section: .text
 # CHECK-NEXT:   }
-# CHECK-NEXT:   Symbol {
-# CHECK-NEXT:     Name: qux
-# CHECK-NEXT:     Value:
-# CHECK-NEXT:     Size:
-# CHECK-NEXT:     Binding: Weak
-# CHECK-NEXT:     Type:
-# CHECK-NEXT:     Other:
-# CHECK-NEXT:     Section: Undefined
-# CHECK-NEXT:   }
 # CHECK-NEXT: ]
 
 # CHECK-NOT: NEEDED
@@ -97,15 +83,6 @@
 # CHECK2-NEXT:     Type:
 # CHECK2-NEXT:     Other:
 # CHECK2-NEXT:     Section: Undefined
-# CHECK2-NEXT:   }
-# CHECK2-NEXT:   Symbol {
-# CHECK2-NEXT:     Name: qux
-# CHECK2-NEXT:     Value:
-# CHECK2-NEXT:     Size:
-# CHECK2-NEXT:     Binding: Weak
-# CHECK2-NEXT:     Type:
-# CHECK2-NEXT:     Other:
-# CHECK2-NEXT:     Section: Undefined
 # CHECK2-NEXT:   }
 # CHECK2-NEXT:   Symbol {
 # CHECK2-NEXT:     Name: foo




More information about the llvm-commits mailing list