[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`.
This commit is contained in:
Fangrui Song 2021-01-19 21:23:57 -08:00
parent 8031785f4a
commit f96ff3c0f8
3 changed files with 11 additions and 7 deletions

View File

@ -1922,7 +1922,7 @@ static std::vector<WrappedSymbol> addWrappedSymbols(opt::InputArgList &args) {
// Tell LTO not to eliminate these symbols.
sym->isUsedInRegularObj = true;
if (!wrap->isUndefined())
if (sym->referenced)
wrap->isUsedInRegularObj = true;
}
return v;

View File

@ -132,9 +132,11 @@ public:
// doesn't know the final contents of the symbol.
uint8_t canInline : 1;
// Used by Undefined and SharedSymbol to track if there has been at least one
// undefined reference to the symbol. The binding may change to STB_WEAK if
// the first undefined reference from a non-shared object is weak.
// Used to track if there has been at least one undefined reference to the
// symbol. For Undefined and SharedSymbol, the binding may change to STB_WEAK
// if the first undefined reference from a non-shared object is weak.
//
// This is also used to retain __wrap_foo when foo is referenced.
uint8_t referenced : 1;
// True if this symbol is specified by --trace-symbol option.

View File

@ -8,11 +8,12 @@
# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/wrap.s -o %t/wrap.o
# RUN: ld.lld -shared --soname=fixed %t/wrap.o -o %t/wrap.so
## foo is defined, then referenced in another object file.
# RUN: ld.lld -shared %t/main.o %t/call-foo.o --wrap foo -o %t1.so
# RUN: llvm-readelf -r %t1.so | FileCheck %s --check-prefix=CHECK1
# CHECK1: R_X86_64_JUMP_SLOT 0000000000000000 bar + 0
# CHECK1-NEXT: R_X86_64_JUMP_SLOT 0{{$}}
# CHECK1-NEXT: R_X86_64_JUMP_SLOT 0000000000000000 __wrap_foo + 0
## --no-allow-shlib-undefined errors because __real_foo is not defined.
# RUN: not ld.lld %t/main.o %t/bar.so -o /dev/null 2>&1 | FileCheck --check-prefix=ERR %s
@ -32,9 +33,10 @@
## __wrap_bar is undefined.
# RUN: ld.lld -shared %t.o --wrap=bar -o %t3.so
# RUN: llvm-readelf -r --dyn-syms %t3.so | FileCheck %s --check-prefix=CHECK3
# CHECK3: R_X86_64_JUMP_SLOT 0{{$}}
# CHECK3: Symbol table '.dynsym' contains 3 entries:
# CHECK3: R_X86_64_JUMP_SLOT 0000000000000000 __wrap_bar + 0
# CHECK3: Symbol table '.dynsym' contains 4 entries:
# CHECK3: NOTYPE LOCAL DEFAULT UND
# CHECK3-NEXT: NOTYPE GLOBAL DEFAULT UND __wrap_bar
# CHECK3-NEXT: NOTYPE GLOBAL DEFAULT 6 _start
# CHECK3-NEXT: NOTYPE GLOBAL DEFAULT 6 foo