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