-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[lld][ELF] Fix wrap when __real_X is weak to avoid undefined symbol #98297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-lld @llvm/pr-subscribers-lld-elf Author: Alexander Qi (xdqi) ChangesWhen 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. Full diff: https://github.com/llvm/llvm-project/pull/98297.diff 2 Files Affected:
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@gotpcrel(%rip), %rax
+ call __real_foo@plt
+
+.global _start
+_start:
+ call foo@plt
|
Can you remove `[ELF] --wrap: Make X weak when __real_X is to avoid undefined symbol errors' |
Fixed. Thank you for your advice. |
@xdqi Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested Please check whether problems have been caused by your change specifically, as How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
…mbol errors (llvm#98297) Fix llvm#98294. 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.
Fix #98294.
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.