Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / lld / test / ELF / riscv-undefined-weak.s
blob8682fe645e9a8e2d69c448d4e4628e6ce35f2eda
1 # REQUIRES: riscv
2 # RUN: llvm-mc -filetype=obj -triple=riscv64 -riscv-asm-relax-branches=0 %s -o %t.o
3 # RUN: llvm-readobj -r %t.o | FileCheck --check-prefix=RELOC %s
5 # RUN: ld.lld -e absolute %t.o -o %t
6 # RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck --check-prefixes=CHECK,PC %s
7 # RUN: llvm-readelf -x .data %t | FileCheck --check-prefixes=HEX,HEX-WITHOUT-PLT %s
9 # RUN: ld.lld -e absolute %t.o -o %t --export-dynamic
10 # RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck --check-prefixes=CHECK,PLT %s
11 # RUN: llvm-readelf -x .data %t | FileCheck --check-prefixes=HEX,HEX-WITH-PLT %s
13 .weak target
14 .global absolute, relative, branch
16 ## Absolute relocations are resolved to 0.
17 # RELOC: 0x0 R_RISCV_HI20 target 0x1
18 # RELOC-NEXT: 0x4 R_RISCV_LO12_I target 0x1
20 # CHECK-LABEL: <absolute>:
21 # CHECK-NEXT: lui t0, 0
22 # CHECK-NEXT: addi t0, t0, 1
23 absolute:
24 lui t0, %hi(target+1)
25 addi t0, t0, %lo(target+1)
27 ## Currently, PC-relative relocations are resolved to 0.
28 # RELOC-NEXT: 0x8 R_RISCV_PCREL_HI20 target 0x0
29 # RELOC-NEXT: 0xC R_RISCV_PCREL_LO12_I .Lpcrel_hi0 0x0
30 # RELOC-NEXT: 0x10 R_RISCV_PCREL_HI20 target 0x2
31 # RELOC-NEXT: 0x14 R_RISCV_PCREL_LO12_S .Lpcrel_hi1 0x0
33 ## 1048559 should be -0x11.
34 # CHECK-LABEL: <relative>:
35 # CHECK-NEXT: 11{{...}}: auipc a1, 1048559
36 # PC-NEXT: addi a1, a1, -352
37 # PLT-NEXT: addi a1, a1, -792
38 # CHECK-LABEL: <.Lpcrel_hi1>:
39 # CHECK-NEXT: 11{{...}}: auipc t1, 1048559
40 # PC-NEXT: sd a2, -358(t1)
41 # PLT-NEXT: sd a2, -798(t1)
42 relative:
43 la a1, target
44 sd a2, target+2, t1
46 ## Branch relocations
47 ## If .dynsym does not exist, an undefined weak symbol is non-preemptible.
48 ## Treat them as PC relative relocations.
49 # RELOC: 0x18 R_RISCV_CALL_PLT target 0x0
50 # RELOC-NEXT: 0x20 R_RISCV_JAL target 0x0
51 # RELOC-NEXT: 0x24 R_RISCV_BRANCH target 0x0
53 # PC-LABEL: <branch>:
54 # PC-NEXT: auipc ra, 0
55 # PC-NEXT: jalr ra
56 # PC-NEXT: [[#%x,ADDR:]]:
57 # PC-SAME: j 0x[[#ADDR]]
58 # PC-NEXT: [[#%x,ADDR:]]:
59 # PC-SAME: beqz zero, 0x[[#ADDR]]
61 ## If .dynsym exists, an undefined weak symbol is preemptible.
62 ## We create a PLT entry and redirect the reference to it.
63 # PLT-LABEL: <branch>:
64 # PLT-NEXT: auipc ra, 0
65 # PLT-NEXT: jalr 56(ra)
66 # PLT-NEXT: [[#%x,ADDR:]]:
67 # PLT-SAME: j 0x[[#ADDR]]
68 # PLT-NEXT: [[#%x,ADDR:]]:
69 # PLT-SAME: beqz zero, 0x[[#ADDR]]
70 branch:
71 call target
72 jal x0, target
73 beq x0, x0, target
75 ## Absolute relocations are resolved to 0.
76 # RELOC: 0x0 R_RISCV_64 target 0x3
77 # RELOC-NEXT: 0x8 R_RISCV_32 target 0x4
78 # RELOC-NEXT: 0xC R_RISCV_PLT32 target 0x0
80 # HEX: section '.data':
81 # HEX-NEXT: 03000000 00000000 04000000
82 # HEX-WITHOUT-PLT-SAME: 00000000
84 ## A plt entry is created for target, so this is the offset between the
85 ## plt entry and this address.
87 ## S = 0x11360 (the address of the plt entry for target)
88 ## A = 0
89 ## P = 0x1343c (the address of `.`)
91 ## S - A + P = -0x0x20dc = 0xffffdf24
92 # HEX-WITH-PLT-SAME: 24dfffff
94 .data
95 .p2align 3
96 .quad target+3
97 .long target+4
99 # PC-NOT: .plt:
100 # PLT: .plt:
102 .word target@plt - .