[PATCH] D45536: Do not keep shared symbols to garbage-collected eliminated DSOs.

Rui Ueyama via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 11 14:30:24 PDT 2018


ruiu created this revision.
ruiu added reviewers: espindola, grimar.
Herald added subscribers: arichardson, emaste.

If all references to a DSO happen to be weak, --gc-sections eliminates
the DSO from DT_NEEDED. If that happens, we also need to eliminate
shared symbols created from the DSO. Otehrwise, they become dangling
references that point to non-exsitent DSO.

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


https://reviews.llvm.org/D45536

Files:
  lld/ELF/MarkLive.cpp
  lld/test/ELF/gc-sections-shared.s


Index: lld/test/ELF/gc-sections-shared.s
===================================================================
--- lld/test/ELF/gc-sections-shared.s
+++ lld/test/ELF/gc-sections-shared.s
@@ -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
@@ -99,15 +85,6 @@
 # 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
 # CHECK2-NEXT:     Value:
 # CHECK2-NEXT:     Size:
Index: lld/ELF/MarkLive.cpp
===================================================================
--- lld/ELF/MarkLive.cpp
+++ lld/ELF/MarkLive.cpp
@@ -301,6 +301,15 @@
   // Follow the graph to mark all live sections.
   doGcSections<ELFT>();
 
+  // If all references to a DSO happen to be weak, the DSO is removed from
+  // DT_NEEDED, which creates dangling shared symbols to non-existent DSO.
+  // We'll replace such symbols with undefined ones to fix it.
+  for (Symbol *Sym : Symtab->getSymbols())
+    if (auto *S = dyn_cast<SharedSymbol>(Sym))
+      if (S->isWeak() && !S->getFile<ELFT>().IsNeeded)
+        replaceSymbol<Undefined>(S, nullptr, S->getName(), STB_WEAK, S->StOther,
+                                 S->Type);
+
   // Report garbage-collected sections.
   if (Config->PrintGcSections)
     for (InputSectionBase *Sec : InputSections)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D45536.142074.patch
Type: text/x-patch
Size: 2401 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180411/42d449ed/attachment-0001.bin>


More information about the llvm-commits mailing list