[lld] 997f2a5 - [ELF] Avoid wrapping unreferenced lazy symbols

Shoaib Meenai via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 4 18:14:18 PST 2022


Author: Shoaib Meenai
Date: 2022-02-04T18:09:37-08:00
New Revision: 997f2a56dea16a8b0a8827ca00cbaa21fbcd2338

URL: https://github.com/llvm/llvm-project/commit/997f2a56dea16a8b0a8827ca00cbaa21fbcd2338
DIFF: https://github.com/llvm/llvm-project/commit/997f2a56dea16a8b0a8827ca00cbaa21fbcd2338.diff

LOG: [ELF] Avoid wrapping unreferenced lazy symbols

There's a couple of motivations here:
* LLD 12 (which I was originally testing with) was adding an undefined
  symbol to the symbol table if you attempted to wrap an unreferenced
  lazy symbol, which would later break `--no-allow-shlib-undefined`. LLD
  on main actually produces a weak undefined symbol, so this doesn't
  break anyway, but it's cleaner to not have the weak undefined symbol
  as well. The new behavior also matches bfd and gold.
* PROVIDE in a linker script referencing a wrapped symbol would think
  that an otherwise-unreferenced lazy symbol which was wrapped was
  actually referenced, and therefore proceed with the definition, which
  goes against expectations. The new behavior also matches bfd and gold.

Reviewed By: MaskRay

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

Added: 
    lld/test/ELF/wrap-lazy.test

Modified: 
    lld/ELF/Driver.cpp

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 406571e4e9fd5..f32f952008645 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -2047,7 +2047,11 @@ static std::vector<WrappedSymbol> addWrappedSymbols(opt::InputArgList &args) {
       continue;
 
     Symbol *sym = symtab->find(name);
-    if (!sym)
+    // Avoid wrapping symbols that are lazy and unreferenced at this point, to
+    // not create undefined references. The isUsedInRegularObj check handles the
+    // case of a weak reference, which we still want to wrap even though it
+    // doesn't cause lazy symbols to be extracted.
+    if (!sym || (sym->isLazy() && !sym->isUsedInRegularObj))
       continue;
 
     Symbol *real = addUnusedUndefined(saver().save("__real_" + name));

diff  --git a/lld/test/ELF/wrap-lazy.test b/lld/test/ELF/wrap-lazy.test
new file mode 100644
index 0000000000000..8b85535517ab5
--- /dev/null
+++ b/lld/test/ELF/wrap-lazy.test
@@ -0,0 +1,58 @@
+# REQUIRES: x86
+
+# RUN: split-file %s %t
+# RUN: llvm-as %t/lazy.ll -o %tlazybitcode.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-elf %t/dummy.s -o %tdummy.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-elf %t/lazy.s -o %tlazy.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-elf %t/ref.s -o %tref.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-elf %t/weakref.s -o %tweakref.o
+# RUN: ld.lld -shared -o %t1.so %tdummy.o --start-lib %tlazy.o --wrap lazy
+# RUN: llvm-readelf -s %t1.so | FileCheck --check-prefix=NO-LAZY %s
+# RUN: ld.lld -shared -o %t2.so %tdummy.o --start-lib %tlazybitcode.o --wrap lazy
+# RUN: llvm-readelf -s %t2.so | FileCheck --check-prefix=NO-LAZY %s
+# RUN: ld.lld -shared -o %t3.so %tdummy.o --start-lib %tlazy.o -u lazy --wrap lazy
+# RUN: llvm-readelf -s %t3.so | FileCheck --check-prefix=LAZY-DEF %s
+# RUN: ld.lld -shared -o %t4.so %tdummy.o %tlazy.o --wrap lazy
+# RUN: llvm-readelf -s %t4.so | FileCheck --check-prefix=LAZY-DEF %s
+# RUN: ld.lld -shared -o %t5.so %tref.o --start-lib %tlazy.o --wrap lazy
+# RUN: llvm-readelf -s %t5.so | FileCheck --check-prefix=LAZY-DEF %s
+# RUN: ld.lld -shared -o %t6.so %tweakref.o --start-lib %tlazy.o --wrap lazy
+# RUN: llvm-readelf -s %t6.so | FileCheck --check-prefix=LAZY-REF %s
+# RUN: ld.lld -shared -o %t7.so %tweakref.o --start-lib %tlazybitcode.o --wrap lazy
+# RUN: llvm-readelf -s %t7.so | FileCheck --check-prefix=LAZY-REF %s
+# RUN: ld.lld -shared -o %tweakref.so %tweakref.o -soname libweakref.so
+# RUN: ld.lld -shared -o %t8.so %tdummy.o %tweakref.so --start-lib %tlazy.o --wrap lazy
+# RUN: llvm-readelf -s %t8.so | FileCheck --check-prefix=NO-LAZY %s
+
+# NO-LAZY-NOT: lazy
+# NO-LAZY-NOT: __wrap_lazy
+
+# LAZY-DEF-DAG: [[#]] lazy
+# LAZY-DEF-DAG: UND __wrap_lazy
+
+# LAZY-REF-DAG: UND lazy
+# LAZY-REF-DAG: UND __wrap_lazy
+
+#--- dummy.s
+.globl dummy
+dummy:
+	retq
+
+#--- lazy.ll
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-elf"
+define void @lazy() {
+  ret void
+}
+
+#--- lazy.s
+.globl lazy
+lazy:
+	retq
+
+#--- ref.s
+	jmp	lazy
+
+#--- weakref.s
+.weak lazy
+	jmp	lazy


        


More information about the llvm-commits mailing list