[PATCH] D135737: [LLD][ELF] restore behaviour of __real_foo being a weak_reference_to_a_lazy_foo

ben via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 11 18:38:32 PDT 2022


bd1976llvm created this revision.
bd1976llvm added reviewers: MaskRay, smeenai, llvm-commits.
Herald added subscribers: StephenFan, arichardson, emaste.
Herald added a project: All.
bd1976llvm requested review of this revision.

A use-case we have is to be able to supply a __wrap_foo function and a --wrap=foo command line argument to the linker but for these to only have an effect if foo is included in the link. If foo is not included in the link then references to __real_foo should be considered a weak reference.

This was the behaviour previous to https://github.com/llvm/llvm-project/commit/8b01b638d0145473d27a0dd99ded48cc5a8b85a1.

Restore this behaviour.


https://reviews.llvm.org/D135737

Files:
  lld/ELF/Driver.cpp
  lld/test/ELF/wrap-extract-real.ll
  lld/test/ELF/wrap-extract-real.s


Index: lld/test/ELF/wrap-extract-real.s
===================================================================
--- /dev/null
+++ lld/test/ELF/wrap-extract-real.s
@@ -0,0 +1,14 @@
+# REQUIRES: x86
+## Show that the link succeeds if wrapped symbol foo is not included in the link.
+
+# RUN: rm -rf %t && split-file %s %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/a.s -o %t/a.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/b.s -o %t/b.o
+# RUN: ld.lld %t/a.o --start-lib %t/b.o --end-lib -o %t/a --wrap foo -o /dev/null --trace 2>&1 | FileCheck %s
+
+CHECK-NOT: t.a
+#--- a.s
+.globl _start; _start:; call __real_foo
+
+#--- b.s
+.global foo; foo:; ret
Index: lld/test/ELF/wrap-extract-real.ll
===================================================================
--- /dev/null
+++ lld/test/ELF/wrap-extract-real.ll
@@ -0,0 +1,26 @@
+# REQUIRES: x86
+## Show that the link succeeds if wrapped symbol foo is not included in the link.
+
+# RUN: rm -rf %t && split-file %s %t
+# RUN: llvm-as %t/a.ll -o %t/a.o
+# RUN: llvm-as %t/b.ll -o %t/b.o
+# RUN: ld.lld %t/a.o --start-lib %t/b.o --end-lib --wrap foo -o /dev/null --trace 2>&1 | FileCheck %s
+
+CHECK-NOT: t.a
+
+#--- a.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 @_start() {
+  call void @__real_foo()
+  ret void
+}
+
+declare void @__real_foo()
+
+#--- b.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 @foo() {
+  ret void
+}
Index: lld/ELF/Driver.cpp
===================================================================
--- lld/ELF/Driver.cpp
+++ lld/ELF/Driver.cpp
@@ -2246,6 +2246,14 @@
     Symbol *real = addUnusedUndefined(saver().save("__real_" + name));
     Symbol *wrap =
         addUnusedUndefined(saver().save("__wrap_" + name), sym->binding);
+
+    // If sym is lazy change the binding to weak. This allows a
+    // wrap function to be supplied which references sym via real
+    // and a --wrap sym option, and for these to only have an effect
+    // if sym has been included in the link.
+    if (sym->isLazy())
+        sym->binding = STB_WEAK;
+
     v.push_back({sym, real, wrap});
 
     // We want to tell LTO not to inline symbols to be overwritten


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135737.466990.patch
Type: text/x-patch
Size: 2318 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221012/07646de6/attachment.bin>


More information about the llvm-commits mailing list