[lld] r325849 - Make undefined symbol in DSO to pull out object files from archive files.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 22 17:16:57 PST 2018


Author: ruiu
Date: Thu Feb 22 17:16:57 2018
New Revision: 325849

URL: http://llvm.org/viewvc/llvm-project?rev=325849&view=rev
Log:
Make undefined symbol in DSO to pull out object files from archive files.

We have an internal program that does't link without this patch. I don't
know of any open-source program that needs this, but there might be.
Since this patch improves compatibility with GNU linkers with a few lines
of code, I think it's worth to be committed.

The problem is about undefined symbols in DSOs. Some programs depend on
the GNU linkers' behavior that they pull out object files from archive
files to resolve undefined symbols in DSOs. We already allow that kind of
"reverse" dependency (from DSOs to the main executable) for regular
symbols, in particular, for "__progname" symbol (which is usually in
crt0.o), but that doesn't work if the symbol is in an archive file.
This patch is to make it work.

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

Added:
    lld/trunk/test/ELF/shlib-undefined-archive.s
Modified:
    lld/trunk/ELF/SymbolTable.cpp

Modified: lld/trunk/ELF/SymbolTable.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SymbolTable.cpp?rev=325849&r1=325848&r2=325849&view=diff
==============================================================================
--- lld/trunk/ELF/SymbolTable.cpp (original)
+++ lld/trunk/ELF/SymbolTable.cpp Thu Feb 22 17:16:57 2018
@@ -599,9 +599,14 @@ template <class ELFT> void SymbolTable::
   for (InputFile *F : SharedFiles) {
     for (StringRef U : cast<SharedFile<ELFT>>(F)->getUndefinedSymbols()) {
       Symbol *Sym = find(U);
-      if (!Sym || !Sym->isDefined())
+      if (!Sym)
         continue;
-      Sym->ExportDynamic = true;
+      if (auto *L = dyn_cast<Lazy>(Sym))
+        if (InputFile *File = L->fetch())
+          addFile<ELFT>(File);
+
+      if (Sym->isDefined())
+        Sym->ExportDynamic = true;
     }
   }
 }

Added: lld/trunk/test/ELF/shlib-undefined-archive.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/shlib-undefined-archive.s?rev=325849&view=auto
==============================================================================
--- lld/trunk/test/ELF/shlib-undefined-archive.s (added)
+++ lld/trunk/test/ELF/shlib-undefined-archive.s Thu Feb 22 17:16:57 2018
@@ -0,0 +1,19 @@
+# REQUIRES: x86
+
+# Undefined symbols in a DSO should pull out object files from archives
+# to resolve them.
+
+# RUN: echo '.globl foo' | llvm-mc -filetype=obj -triple=x86_64-linux-gnu -o %t1.o -
+# RUN: ld.lld -shared -o %t.so %t1.o
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-linux-gnu -o %t2.o %s
+# RUN: rm -f %t.a
+# RUN: llvm-ar cru %t.a %t2.o
+# RUN: ld.lld -o %t.exe %t.so %t.a
+# RUN: llvm-nm -D %t.exe | FileCheck %s
+
+# CHECK: T foo
+
+.globl foo
+foo:
+  ret




More information about the llvm-commits mailing list