Skip to content

Commit f96ff3c

Browse files
committed
[ELF] --wrap: Produce a dynamic symbol for undefined __wrap_
``` // a.s jmp fcntl // b.s .globl fcntl fcntl: ret ``` `ld.lld -shared --wrap=fcntl a.o b.o` has an `R_X86_64_JUMP_SLOT` referencing the index 0 undefined symbol, which will cause a glibc `symbol lookup error` at runtime. This is because `__wrap_fcntl` is not in .dynsym We use an approximation `!wrap->isUndefined()`, which doesn't set `isUsedInRegularObj` of `__wrap_fcntl` when `fcntl` is referenced and `__wrap_fcntl` is undefined. Fix this by using `sym->referenced`.
1 parent 8031785 commit f96ff3c

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

lld/ELF/Driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1922,7 +1922,7 @@ static std::vector<WrappedSymbol> addWrappedSymbols(opt::InputArgList &args) {
19221922

19231923
// Tell LTO not to eliminate these symbols.
19241924
sym->isUsedInRegularObj = true;
1925-
if (!wrap->isUndefined())
1925+
if (sym->referenced)
19261926
wrap->isUsedInRegularObj = true;
19271927
}
19281928
return v;

lld/ELF/Symbols.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,11 @@ class Symbol {
132132
// doesn't know the final contents of the symbol.
133133
uint8_t canInline : 1;
134134

135-
// Used by Undefined and SharedSymbol to track if there has been at least one
136-
// undefined reference to the symbol. The binding may change to STB_WEAK if
137-
// the first undefined reference from a non-shared object is weak.
135+
// Used to track if there has been at least one undefined reference to the
136+
// symbol. For Undefined and SharedSymbol, the binding may change to STB_WEAK
137+
// if the first undefined reference from a non-shared object is weak.
138+
//
139+
// This is also used to retain __wrap_foo when foo is referenced.
138140
uint8_t referenced : 1;
139141

140142
// True if this symbol is specified by --trace-symbol option.

lld/test/ELF/wrap-shlib-undefined.s

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/wrap.s -o %t/wrap.o
99
# RUN: ld.lld -shared --soname=fixed %t/wrap.o -o %t/wrap.so
1010

11+
## foo is defined, then referenced in another object file.
1112
# RUN: ld.lld -shared %t/main.o %t/call-foo.o --wrap foo -o %t1.so
1213
# RUN: llvm-readelf -r %t1.so | FileCheck %s --check-prefix=CHECK1
1314

1415
# CHECK1: R_X86_64_JUMP_SLOT 0000000000000000 bar + 0
15-
# CHECK1-NEXT: R_X86_64_JUMP_SLOT 0{{$}}
16+
# CHECK1-NEXT: R_X86_64_JUMP_SLOT 0000000000000000 __wrap_foo + 0
1617

1718
## --no-allow-shlib-undefined errors because __real_foo is not defined.
1819
# RUN: not ld.lld %t/main.o %t/bar.so -o /dev/null 2>&1 | FileCheck --check-prefix=ERR %s
@@ -32,9 +33,10 @@
3233
## __wrap_bar is undefined.
3334
# RUN: ld.lld -shared %t.o --wrap=bar -o %t3.so
3435
# RUN: llvm-readelf -r --dyn-syms %t3.so | FileCheck %s --check-prefix=CHECK3
35-
# CHECK3: R_X86_64_JUMP_SLOT 0{{$}}
36-
# CHECK3: Symbol table '.dynsym' contains 3 entries:
36+
# CHECK3: R_X86_64_JUMP_SLOT 0000000000000000 __wrap_bar + 0
37+
# CHECK3: Symbol table '.dynsym' contains 4 entries:
3738
# CHECK3: NOTYPE LOCAL DEFAULT UND
39+
# CHECK3-NEXT: NOTYPE GLOBAL DEFAULT UND __wrap_bar
3840
# CHECK3-NEXT: NOTYPE GLOBAL DEFAULT 6 _start
3941
# CHECK3-NEXT: NOTYPE GLOBAL DEFAULT 6 foo
4042

0 commit comments

Comments
 (0)