[llvm-branch-commits] [lld] f96ff3c - [ELF] --wrap: Produce a dynamic symbol for undefined __wrap_

Fangrui Song via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Jan 19 21:28:05 PST 2021


Author: Fangrui Song
Date: 2021-01-19T21:23:57-08:00
New Revision: f96ff3c0f8ebd941b3f6b345164c3d858b781484

URL: https://github.com/llvm/llvm-project/commit/f96ff3c0f8ebd941b3f6b345164c3d858b781484
DIFF: https://github.com/llvm/llvm-project/commit/f96ff3c0f8ebd941b3f6b345164c3d858b781484.diff

LOG: [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`.

Added: 
    

Modified: 
    lld/ELF/Driver.cpp
    lld/ELF/Symbols.h
    lld/test/ELF/wrap-shlib-undefined.s

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index e1395a568ea2..de613b5c9d19 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -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;

diff  --git a/lld/ELF/Symbols.h b/lld/ELF/Symbols.h
index bb87ac75f9c6..38c20d55bb08 100644
--- a/lld/ELF/Symbols.h
+++ b/lld/ELF/Symbols.h
@@ -132,9 +132,11 @@ class Symbol {
   // 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.

diff  --git a/lld/test/ELF/wrap-shlib-undefined.s b/lld/test/ELF/wrap-shlib-undefined.s
index acc9fd8b943e..f46ebe36b779 100644
--- a/lld/test/ELF/wrap-shlib-undefined.s
+++ b/lld/test/ELF/wrap-shlib-undefined.s
@@ -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
 


        


More information about the llvm-branch-commits mailing list