[lld] [lld][ELF] Fix wrap when __real_X is weak to avoid undefined symbol #98294 (PR #98297)
Alexander Qi via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 10 03:26:41 PDT 2024
https://github.com/xdqi created https://github.com/llvm/llvm-project/pull/98297
When you specify --wrap=foo, sometimes foo is undefined in any context. If you declare __real_foo as weak, GNU ld will not attempt to find the strong symbol foo, instead, it generates a weak undefined symbol.
This pull request imitates this behavior by copying the binding attribute from __real_foo to foo.
>From 6609380af2548804894c8a17fa8624d601c564fa Mon Sep 17 00:00:00 2001
From: Alexander Qi <xdqi at users.noreply.github.com>
Date: Wed, 10 Jul 2024 18:21:59 +0800
Subject: [PATCH] [lld][ELF] Fix wrap when __real_X is weak
---
lld/ELF/Driver.cpp | 6 +++++-
lld/test/ELF/wrap-weak.s | 23 +++++++++++++++++++++++
2 files changed, 28 insertions(+), 1 deletion(-)
create mode 100644 lld/test/ELF/wrap-weak.s
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index a4863d6717efb..8c9196fe4047b 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -2551,8 +2551,12 @@ static std::vector<WrappedSymbol> addWrappedSymbols(opt::InputArgList &args) {
// If __real_ is referenced, pull in the symbol if it is lazy. Do this after
// processing __wrap_ as that may have referenced __real_.
StringRef realName = saver().save("__real_" + name);
- if (symtab.find(realName))
+ if (Symbol *real = symtab.find(realName)) {
symtab.addUnusedUndefined(name, sym->binding);
+ // update sym's binding to __real_'s binding, as sym will replacing
+ // __real_ later in SymbolTable::wrap().
+ sym->binding = real->binding;
+ }
Symbol *real = symtab.addUnusedUndefined(realName);
v.push_back({sym, real, wrap});
diff --git a/lld/test/ELF/wrap-weak.s b/lld/test/ELF/wrap-weak.s
new file mode 100644
index 0000000000000..e06304f39daa8
--- /dev/null
+++ b/lld/test/ELF/wrap-weak.s
@@ -0,0 +1,23 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: ld.lld -shared -o %t.so %t.o -wrap foo
+
+# RUN: llvm-readelf --dyn-syms %t.so | FileCheck %s --check-prefix=CHECK
+
+# CHECK: Symbol table '.dynsym' contains 4 entries:
+# CHECK: NOTYPE LOCAL DEFAULT UND
+# CHECK-NEXT: NOTYPE WEAK DEFAULT UND foo
+# CHECK-NEXT: NOTYPE GLOBAL DEFAULT [[#]] __wrap_foo
+# CHECK-NEXT: NOTYPE GLOBAL DEFAULT [[#]] _start
+
+.global foo
+.weak __real_foo
+
+.global __wrap_foo
+__wrap_foo:
+ movq __real_foo at gotpcrel(%rip), %rax
+ call __real_foo at plt
+
+.global _start
+_start:
+ call foo at plt
More information about the llvm-commits
mailing list