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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lld/ELF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, which will replace real's later in
// SymbolTable::wrap.
sym->binding = real->binding;
}

Symbol *real = symtab.addUnusedUndefined(realName);
v.push_back({sym, real, wrap});
Expand Down
23 changes: 23 additions & 0 deletions lld/test/ELF/wrap-weak.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# REQUIRES: x86
# RUN: llvm-mc -filetype=obj -triple=x86_64 %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: 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
Loading