xref: /llvm-project/lld/test/ELF/lto/wrap-unreferenced-before-codegen.test (revision d7cf7ab61cd0a257a4d9907c8005ee89c6cf6985)
1# REQUIRES: x86
2## Verify that we can correctly wrap symbols produced only during LTO codegen
3## and unreferenced before then.
4
5# RUN: rm -rf %t && split-file %s %t
6# RUN: llvm-mc -triple x86_64-elf --filetype=obj -o %t/unwind.o %t/unwind.s
7# RUN: ld.lld -shared -o %t/libunwind.so -soname libunwind.so %t/unwind.o
8# RUN: llvm-as -o %t/resume.bc %t/resume.ll
9# RUN: ld.lld -shared -o %t/libresume.so -soname libresume.so %t/resume.bc \
10# RUN:   %t/libunwind.so --wrap _Unwind_Resume
11# RUN: llvm-objdump --no-print-imm-hex --dynamic-reloc --disassemble %t/libresume.so | \
12# RUN:   FileCheck --check-prefix=UNWIND-DISASM %s
13# RUN: llvm-readelf --dyn-syms %t/libresume.so | \
14# RUN:   FileCheck --check-prefix=UNWIND-DYNSYM %s
15
16# UNWIND-DISASM:       [[#%x,RELOC:]] R_X86_64_JUMP_SLOT __wrap__Unwind_Resume
17# UNWIND-DISASM-LABEL: <_Z1fv>:
18# UNWIND-DISASM:       callq {{.*}}<__wrap__Unwind_Resume@plt>
19# UNWIND-DISASM-LABEL: <__wrap__Unwind_Resume@plt>:
20# UNWIND-DISASM-NEXT:  jmpq *[[#]](%rip) # [[#%#x,RELOC]]
21
22# UNWIND-DYNSYM:      Symbol table '.dynsym' contains 4 entries:
23# UNWIND-DYNSYM:      NOTYPE  LOCAL  DEFAULT   UND
24# UNWIND-DYNSYM-NEXT: NOTYPE  GLOBAL DEFAULT   UND throw
25# UNWIND-DYNSYM-NEXT: NOTYPE  GLOBAL DEFAULT   UND __wrap__Unwind_Resume
26# UNWIND-DYNSYM-NEXT: FUNC    GLOBAL DEFAULT     9 _Z1fv
27
28# RUN: llvm-mc -triple x86_64-elf -filetype=obj -o %t/malloc.o %t/malloc.s
29# RUN: ld.lld -shared -o %t/libmalloc.so -soname libmalloc.so %t/malloc.o
30# RUN: llvm-mc -triple x86_64-elf -filetype=obj -o %t/emutls.o %t/emutls.s
31# RUN: llvm-as -o %t/usetls.bc %t/usetls.ll
32# RUN: ld.lld -shared -o %t/libusetls.so \
33# RUN:   %t/usetls.bc %t/libmalloc.so \
34# RUN:   --start-lib %t/emutls.o -mllvm -emulated-tls --wrap malloc
35# RUN: llvm-objdump --no-print-imm-hex --dynamic-reloc --disassemble %t/libusetls.so | \
36# RUN:   FileCheck --check-prefix=USETLS-DISASM %s
37# RUN: llvm-readelf --dyn-syms %t/libusetls.so | \
38# RUN:   FileCheck --check-prefix=USETLS-DYNSYM %s
39
40# USETLS-DISASM:       [[#%x,RELOC:]] R_X86_64_JUMP_SLOT __wrap_malloc
41# USETLS-DISASM-LABEL: <__emutls_get_address>:
42# USETLS-DISASM-NEXT:  jmp{{.*}}<__wrap_malloc@plt>
43# USETLS-DISASM-LABEL: <__wrap_malloc@plt>:
44# USETLS-DISASM-NEXT:  jmpq *[[#]](%rip) # [[#%#x,RELOC]]
45
46# USETLS-DYNSYM:      Symbol table '.dynsym' contains 5 entries:
47# USETLS-DYNSYM:      NOTYPE  LOCAL  DEFAULT   UND
48# USETLS-DYNSYM-NEXT: NOTYPE  GLOBAL DEFAULT   UND __wrap_malloc
49# USETLS-DYNSYM-NEXT: FUNC    GLOBAL DEFAULT     6 f
50# USETLS-DYNSYM-NEXT: NOTYPE  GLOBAL DEFAULT     6 __emutls_get_address
51
52#--- unwind.s
53.globl _Unwind_Resume
54.globl __wrap__Unwind_Resume
55_Unwind_Resume:
56__wrap__Unwind_Resume:
57	retq
58
59#--- resume.ll
60target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
61target triple = "x86_64-unknown-linux-gnu"
62define dso_local void @_Z1fv() optnone noinline personality ptr @throw {
63  invoke void @throw()
64          to label %unreachable unwind label %lpad
65lpad:
66  %1 = landingpad { ptr, i32 }
67          cleanup
68  resume { ptr, i32 } %1
69unreachable:
70  unreachable
71}
72declare void @throw()
73
74#--- malloc.s
75.globl malloc
76malloc:
77	retq
78
79#--- emutls.s
80.globl __emutls_get_address
81__emutls_get_address:
82	jmp	malloc@plt
83
84#--- usetls.ll
85target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
86target triple = "x86_64-unknown-linux-gnu"
87@x = dso_local thread_local global i32 0, align 4
88define dso_local i32 @f() {
89  %loaded = load i32, ptr @x, align 4
90  ret i32 %loaded
91}
92