Skip to content

[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

Merged
merged 2 commits into from
Jul 11, 2024
Merged

[lld][ELF] Fix wrap when __real_X is weak to avoid undefined symbol #98297

merged 2 commits into from
Jul 11, 2024

Conversation

xdqi
Copy link
Contributor

@xdqi xdqi commented Jul 10, 2024

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.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

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
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

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.

@llvmbot
Copy link
Member

llvmbot commented Jul 10, 2024

@llvm/pr-subscribers-lld

@llvm/pr-subscribers-lld-elf

Author: Alexander Qi (xdqi)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/98297.diff

2 Files Affected:

  • (modified) lld/ELF/Driver.cpp (+5-1)
  • (added) lld/test/ELF/wrap-weak.s (+23)
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

@MaskRay
Copy link
Member

MaskRay commented Jul 10, 2024

title: #98294

Can you remove #98294 from the subject and add Fix #98294 to the body instead?

`[ELF] --wrap: Make X weak when __real_X is to avoid undefined symbol errors'

@xdqi xdqi changed the title [lld][ELF] Fix wrap when __real_X is weak to avoid undefined symbol #98294 [lld][ELF] Fix wrap when __real_X is weak to avoid undefined symbol Jul 11, 2024
@xdqi
Copy link
Contributor Author

xdqi commented Jul 11, 2024

Fixed. Thank you for your advice.

@MaskRay MaskRay merged commit 1b3e376 into llvm:main Jul 11, 2024
4 of 5 checks passed
Copy link

@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
by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as
the builds can include changes from many authors. It is not uncommon for your
change to be included in a build that fails due to someone else's changes, or
infrastructure issues.

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.
This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

aaryanshukla pushed a commit to aaryanshukla/llvm-project that referenced this pull request Jul 14, 2024
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[LLD][ELF] undefined reference to foo when using --wrap=foo when __real_foo is declared weak
3 participants