1# REQUIRES: x86
2
3# RUN: rm -rf %t && split-file %s %t
4# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/main.s -o %t/main.o
5# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/call-foo.s -o %t/call-foo.o
6# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/bar.s -o %t/bar.o
7# RUN: ld.lld -shared -soname=t.so %t/bar.o -o %t/bar.so
8# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/wrap.s -o %t/wrap.o
9# RUN: ld.lld -shared --soname=fixed %t/wrap.o -o %t/wrap.so
10
11## foo is defined, then referenced in another object file.
12# RUN: ld.lld -shared %t/main.o %t/call-foo.o --wrap foo -o %t1.so
13# RUN: llvm-readelf -r %t1.so | FileCheck %s --check-prefix=CHECK1
14
15# CHECK1:      R_X86_64_JUMP_SLOT 0000000000000000 bar + 0
16# CHECK1-NEXT: R_X86_64_JUMP_SLOT 0000000000000000 __wrap_foo + 0
17
18## --no-allow-shlib-undefined errors because __real_foo is not defined.
19# RUN: not ld.lld %t/main.o %t/bar.so -o /dev/null 2>&1 | FileCheck --check-prefix=ERR %s
20# ERR: error: undefined reference due to --no-allow-shlib-undefined: __real_foo
21
22## --wrap=foo defines __real_foo.
23# RUN: ld.lld %t/main.o %t/bar.so --wrap=foo -o %t2
24# RUN: llvm-readelf --dyn-syms %t2 | FileCheck %s --check-prefix=CHECK2
25
26## See wrap-plt2.s why __wrap_foo is retained.
27# CHECK2:      Symbol table '.dynsym' contains 3 entries:
28# CHECK2:      NOTYPE  LOCAL  DEFAULT  UND
29# CHECK2-NEXT: NOTYPE  GLOBAL DEFAULT  UND bar
30# CHECK2-NEXT: NOTYPE  GLOBAL DEFAULT  UND __wrap_foo
31
32## __wrap_bar is undefined.
33# RUN: ld.lld -shared %t/main.o --wrap=bar -o %t3.so
34# RUN: llvm-readelf -r --dyn-syms %t3.so | FileCheck %s --check-prefix=CHECK3
35# CHECK3:      R_X86_64_JUMP_SLOT 0000000000000000 __wrap_bar + 0
36# CHECK3:      Symbol table '.dynsym' contains 4 entries:
37# CHECK3:      NOTYPE  LOCAL  DEFAULT  UND
38# CHECK3-NEXT: NOTYPE  GLOBAL DEFAULT  UND __wrap_bar
39# CHECK3-NEXT: NOTYPE  GLOBAL DEFAULT    6 _start
40# CHECK3-NEXT: NOTYPE  GLOBAL DEFAULT    6 foo
41
42## __wrap_bar is defined in %t/wrap.so.
43# RUN: ld.lld -shared %t/main.o %t/wrap.so --wrap=bar -o %t4.so
44# RUN: llvm-readelf -r --dyn-syms %t4.so | FileCheck %s --check-prefix=CHECK4
45# CHECK4:      R_X86_64_JUMP_SLOT {{.*}} __wrap_bar + 0
46# CHECK4:      Symbol table '.dynsym' contains 4 entries:
47# CHECK4:      NOTYPE  LOCAL  DEFAULT  UND
48# CHECK4-NEXT: NOTYPE  GLOBAL DEFAULT  UND __wrap_bar
49# CHECK4-NEXT: NOTYPE  GLOBAL DEFAULT    6 _start
50# CHECK4-NEXT: NOTYPE  GLOBAL DEFAULT    6 foo
51
52# RUN: ld.lld %t/main.o %t/wrap.so --wrap bar -o %t1
53# RUN: llvm-readelf --dyn-syms %t1 | FileCheck %s --check-prefix=DYNSYM
54# RUN: llvm-objdump -d %t1 | FileCheck %s --check-prefix=ASM
55
56# DYNSYM:      Symbol table '.dynsym' contains 2 entries:
57# DYNSYM:      NOTYPE  LOCAL  DEFAULT  UND
58# DYNSYM-NEXT: NOTYPE  GLOBAL DEFAULT  UND __wrap_bar
59
60# ASM:      <_start>:
61# ASM-NEXT:   callq {{.*}} <__wrap_bar@plt>
62
63#--- main.s
64.globl _start, foo
65_start:
66  call bar
67foo:
68
69#--- call-foo.s
70  call foo
71
72#--- bar.s
73.globl bar
74bar:
75  call __real_foo
76
77#--- wrap.s
78.globl __wrap_bar
79__wrap_bar:
80  retq
81