[PATCH] D118756: [ELF] Avoid wrapping unreferenced lazy symbols

Shoaib Meenai via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 2 00:10:47 PST 2022


smeenai created this revision.
smeenai added reviewers: MaskRay, psmith.
Herald added subscribers: arichardson, emaste.
smeenai requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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 a wrapped symbol that was otherwise unreferenced was actually referenced, and therefore proceed with the definition, which goes against my expectations. The new behavior also matches bfd and gold.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D118756

Files:
  lld/ELF/Driver.cpp
  lld/test/ELF/wrap-lazy.s


Index: lld/test/ELF/wrap-lazy.s
===================================================================
--- /dev/null
+++ lld/test/ELF/wrap-lazy.s
@@ -0,0 +1,37 @@
+# REQUIRES: x86
+
+# RUN: split-file %s %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64-linux-gnu %t/lazy.s -o %tlazy.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-linux-gnu %t/lib.s -o %tlib.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-linux-gnu %t/ref.s -o %tref.o
+# RUN: llvm-ar crs %tliblazy.a %tlazy.o
+# RUN: ld.lld -shared -o %t1.so %tlib.o %tliblazy.a --wrap lazy
+# RUN: llvm-nm -D %t1.so | FileCheck --check-prefix=NO-LAZY %s
+# RUN: ld.lld -shared -o %t2.so %tlib.o --start-lib %tlazy.o --wrap lazy
+# RUN: llvm-nm -D %t2.so | FileCheck --check-prefix=NO-LAZY %s
+# RUN: ld.lld -shared -o %t3.so %tlib.o %tliblazy.a -u lazy --wrap lazy
+# RUN: llvm-nm -D %t3.so | FileCheck --check-prefix=LAZY %s
+# RUN: ld.lld -shared -o %t4.so %tlib.o --start-lib %tlazy.o -u lazy --wrap lazy
+# RUN: llvm-nm -D %t4.so | FileCheck --check-prefix=LAZY %s
+# RUN: ld.lld -shared -o %t5.so %tlib.o %tlazy.o --wrap lazy
+# RUN: llvm-nm -D %t5.so | FileCheck --check-prefix=LAZY %s
+# RUN: ld.lld -shared -o %t6.so %tref.o %tliblazy.a --wrap lazy
+# RUN: llvm-nm -D %t6.so | FileCheck --check-prefix=LAZY %s
+# RUN: ld.lld -shared -o %t7.so %tref.o --start-lib %tlazy.o --wrap lazy
+# RUN: llvm-nm -D %t7.so | FileCheck --check-prefix=LAZY %s
+
+## NO-LAZY-NOT: lazy
+## LAZY: lazy
+
+#--- lazy.s
+.globl lazy
+lazy:
+	retq
+
+#--- lib.s
+.globl dummy
+dummy:
+	retq
+
+#--- ref.s
+	jmp	lazy
Index: lld/ELF/Driver.cpp
===================================================================
--- lld/ELF/Driver.cpp
+++ lld/ELF/Driver.cpp
@@ -2047,7 +2047,10 @@
       continue;
 
     Symbol *sym = symtab->find(name);
-    if (!sym)
+    // If a symbol is lazy at this point, it must have been unreferenced (and
+    // therefore never fetched), so avoid wrapping it (which would otherwise
+    // create an undefined reference).
+    if (!sym || sym->isLazy())
       continue;
 
     Symbol *real = addUnusedUndefined(saver().save("__real_" + name));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D118756.405157.patch
Type: text/x-patch
Size: 2105 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220202/8afcf0c7/attachment.bin>


More information about the llvm-commits mailing list