xref: /llvm-project/lld/test/ELF/riscv-relax-call.s (revision c532ba4edd7ad7675ba450ba43268aa9e7bda46b)
1# REQUIRES: riscv
2## Relax R_RISCV_CALL and R_RISCV_CALL_PLT.
3
4# RUN: rm -rf %t && split-file %s %t && cd %t
5
6## Without RVC
7# RUN: llvm-mc -filetype=obj -triple=riscv32 -mattr=+relax a.s -o a.32.o
8# RUN: llvm-mc -filetype=obj -triple=riscv32 -mattr=+relax b.s -o b.32.o
9# RUN: ld.lld -shared -soname=b.so b.32.o -o b.32.so
10# RUN: ld.lld -T lds a.32.o b.32.so -o 32
11# RUN: llvm-objdump -td --no-show-raw-insn -M no-aliases 32 | FileCheck %s --check-prefix=NORVC
12
13# RUN: llvm-mc -filetype=obj -triple=riscv64 -mattr=+relax a.s -o a.64.o
14# RUN: llvm-mc -filetype=obj -triple=riscv64 -mattr=+relax b.s -o b.64.o
15# RUN: ld.lld -shared -soname=b.so b.64.o -o b.64.so
16# RUN: ld.lld -T lds a.64.o b.64.so -o 64
17# RUN: llvm-objdump -td --no-show-raw-insn -M no-aliases 64 | FileCheck %s --check-prefix=NORVC
18
19## RVC
20# RUN: llvm-mc -filetype=obj -triple=riscv32 -mattr=+c,+relax a.s -o a.32c.o
21# RUN: llvm-mc -filetype=obj -triple=riscv32 -mattr=+c,+relax b.s -o b.32c.o
22# RUN: ld.lld -shared -soname=b.so b.32c.o -o b.32c.so
23# RUN: ld.lld -T lds a.32c.o b.32c.so -o 32c
24# RUN: llvm-objdump -td --no-show-raw-insn -M no-aliases 32c | FileCheck %s --check-prefixes=RVC,RVC32
25
26# RUN: llvm-mc -filetype=obj -triple=riscv64 -mattr=+c,+relax a.s -o a.64c.o
27# RUN: llvm-mc -filetype=obj -triple=riscv64 -mattr=+c,+relax b.s -o b.64c.o
28# RUN: ld.lld -shared -soname=b.so b.64c.o -o b.64c.so
29# RUN: ld.lld -T lds a.64c.o b.64c.so -o 64c
30# RUN: llvm-objdump -td --no-show-raw-insn -M no-aliases 64c | FileCheck %s --check-prefixes=RVC,RVC64
31
32## --no-relax disables relaxation.
33# RUN: ld.lld -T lds a.64.o b.64.so --no-relax -o 64.norelax
34# RUN: llvm-objdump -td --no-show-raw-insn -M no-aliases 64.norelax | FileCheck %s --check-prefixes=NORELAX
35
36# NORVC:       00010000 g       .text  {{0*}}0000001c _start
37# NORVC:       0001001c g       .text  {{0*}}00000000 _start_end
38# NORVC:       00010808 g       .mid   {{0*}}00000000 mid_end
39# NORVC:       00110016 g       .high  {{0*}}00000000 high_end
40
41# NORVC-LABEL: <_start>:
42# NORVC-NEXT:    10000:  jal    zero, {{.*}} <a>
43# NORVC-NEXT:            jal    zero, {{.*}} <a>
44# NORVC-NEXT:            addi   zero, zero, 0x0
45# NORVC-NEXT:            addi   zero, zero, 0x0
46# NORVC-NEXT:    10010:  jal    ra, {{.*}} <a>
47# NORVC-NEXT:            jal    ra, 0x10420
48# NORVC-EMPTY:
49
50# NORVC-LABEL: <.mid>:
51# NORVC-NEXT:    10800:  jal    ra, {{.*}} <_start>
52# NORVC-NEXT:            jal    ra, {{.*}} <_start>
53# NORVC-EMPTY:
54
55# NORVC-LABEL: <.mid2>:
56# NORVC-NEXT:    1080c:  jal    ra, {{.*}} <_start>
57# NORVC-EMPTY:
58
59# NORVC-LABEL: <.high>:
60# NORVC-NEXT:   110006:  auipc  ra, 0xfff00
61# NORVC-NEXT:            jalr   ra, -0x6(ra)
62# NORVC-NEXT:            auipc  ra, 0xfff00
63# NORVC-NEXT:            jalr   ra, -0xe(ra)
64# NORVC-EMPTY:
65
66# RVC32:       00010000 g       .text  00000016 _start
67# RVC32:       00010016 g       .text  00000000 _start_end
68# RVC32:       00010806 g       .mid   00000000 mid_end
69# RVC32:       0011000c g       .high  00000000 high_end
70# RVC64:       0000000000010000 g       .text  000000000000001a _start
71# RVC64:       000000000001001a g       .text  0000000000000000 _start_end
72# RVC64:       0000000000010808 g       .mid   0000000000000000 mid_end
73# RVC64:       0000000000110014 g       .high  0000000000000000 high_end
74
75# RVC-LABEL:   <_start>:
76# RVC-NEXT:      10000:  c.j    {{.*}} <a>
77# RVC-NEXT:              c.j    {{.*}} <a>
78# RVC-NEXT:              addi   zero, zero, 0x0
79# RVC-NEXT:              addi   zero, zero, 0x0
80# RVC-NEXT:              addi   zero, zero, 0x0
81# RVC32-NEXT:    10010:  c.jal  {{.*}} <a>
82# RVC32-NEXT:            c.jal  0x10420
83# RVC64-NEXT:    10010:  jal    ra, {{.*}} <a>
84# RVC64-NEXT:            jal    ra, 0x10420
85# RVC-EMPTY:
86# RVC-NEXT:    <a>:
87# RVC-NEXT:              c.jr   ra
88# RVC-EMPTY:
89
90# RVC-LABEL:   <.mid>:
91# RVC32-NEXT:    10800:  c.jal  {{.*}} <_start>
92# RVC64-NEXT:    10800:  jal    ra, {{.*}} <_start>
93# RVC-NEXT:              jal    ra, {{.*}} <_start>
94# RVC-EMPTY:
95
96# RVC-LABEL:   <.mid2>:
97# RVC32-NEXT:    1080a:  jal    ra, {{.*}} <_start>
98# RVC64-NEXT:    1080c:  jal    ra, {{.*}} <_start>
99# RVC-EMPTY:
100
101# RVC-LABEL:   <.high>:
102# RVC32-NEXT:   110000:  jal    ra, 0x10000 <_start>
103# RVC32-NEXT:            auipc  ra, 0xfff00
104# RVC32-NEXT:            jalr   ra, -0x4(ra)
105# RVC64-NEXT:   110004:  auipc  ra, 0xfff00
106# RVC64-NEXT:            jalr   ra, -0x4(ra)
107# RVC64-NEXT:            auipc  ra, 0xfff00
108# RVC64-NEXT:            jalr   ra, -0xc(ra)
109# RVC-EMPTY:
110
111# NORELAX-LABEL: <_start>:
112# NORELAX-NEXT:    10000:  auipc  t1, 0x0
113# NORELAX-NEXT:            jalr   zero, 0x20(t1)
114# NORELAX-NEXT:            auipc  t0, 0x0
115# NORELAX-NEXT:            jalr   zero, 0x18(t0)
116# NORELAX-NEXT:    10010:  auipc  ra, 0x0
117# NORELAX-NEXT:            jalr   ra, 0x10(ra)
118# NORELAX-NEXT:            auipc  ra, 0x0
119# NORELAX-NEXT:            jalr   ra, 0x408(ra)
120# NORELAX-EMPTY:
121
122#--- a.s
123.global _start, _start_end
124_start:
125  tail a@plt
126  jump a, t0
127.balign 16
128  call a          # rv32c: c.jal; rv64c: jal
129  call bar        # PLT call can be relaxed. rv32c: c.jal; rv64c: jal
130
131a:
132  ret
133.size _start, . - _start
134_start_end:
135
136.section .mid,"ax",@progbits
137  call _start@plt # rv32c: c.jal; rv64c: jal
138  call _start@plt
139
140.section .mid2,"ax",@progbits
141  call _start@plt
142
143.section .high,"ax",@progbits
144  call _start@plt # relaxable for %t/32c
145  call _start@plt # not relaxed
146
147#--- b.s
148.globl bar
149bar:
150  ret
151
152#--- lds
153SECTIONS {
154  .text 0x10000 : { *(.text) }
155  .plt 0x10400 : { *(.plt) }
156  .mid 0x10800 : { *(.mid); mid_end = .; }
157  .mid2 mid_end+4 : { *(.mid2) }
158  # 22 is the size of _start in %t/32c (RVC32).
159  .high 0x110000+(_start_end-_start)-22 : { *(.high); high_end = .; }
160}
161