1 # RUN: rm -rf %t && mkdir -p %t
2 # RUN: llvm-mc -triple=aarch64-unknown-linux-gnu -relax-relocations=false \
3 # RUN: -position-independent -filetype=obj -o %t/elf_reloc.o %s
4 # RUN: llvm-jitlink -noexec \
5 # RUN: -abs external_data=0xdeadbeef \
6 # RUN: -abs external_func=0xcafef00d \
7 # RUN: -check %s %t/elf_reloc.o
19 # Check R_AARCH64_CALL26 / R_AARCH64_JUMP26 relocation of a local function call
21 # jitlink-check: decode_operand(local_func_call26, 0)[25:0] = (local_func - local_func_call26)[27:2]
22 # jitlink-check: decode_operand(local_func_jump26, 0)[25:0] = (local_func - local_func_jump26)[27:2]
25 .type local_func,@function
28 .size local_func, .-local_func
30 .globl local_func_call26
34 .size local_func_call26, .-local_func_call26
36 .globl local_func_jump26
40 .size local_func_jump26, .-local_func_jump26
42 # Check R_AARCH64_ADR_PREL_LO21 relocation of a local symbol
44 # jitlink-check: decode_operand(test_adr_prel_lo21, 1) = (adr_data - test_adr_prel_lo21)[20:0]
45 .globl test_adr_prel_lo21, adr_data
49 .size test_adr_prel_lo21, .-test_adr_prel_lo21
50 ## ADR encoding is a bit tricky so use an offset with an irregular bit pattern
51 ## to test this bit better
52 adr_data
= test_adr_prel_lo21
+ 0xe46f2
54 # Check R_AARCH64_ADR_PREL_PG_HI21 / R_AARCH64_ADD_ABS_LO12_NC relocation of a local symbol
56 # For the ADR_PREL_PG_HI21/ADRP instruction we have the 21-bit delta to the 4k page
57 # containing the global.
59 # jitlink-check: decode_operand(test_adr_prel, 1) = (named_data - test_adr_prel)[32:12]
60 # jitlink-check: decode_operand(test_add_abs_lo12, 2) = (named_data + 0)[11:0]
65 .size test_adr_prel, .-test_adr_prel
67 .globl test_add_abs_lo12
70 add x0
, x0
, :lo12
:named_data
71 .size test_add_abs_lo12, .-test_add_abs_lo12
73 # Check that calls/jumps to external functions trigger the generation of stubs and GOT
76 # jitlink-check: decode_operand(test_external_call, 0) = (stub_addr(elf_reloc.o, external_func) - test_external_call)[27:2]
77 # jitlink-check: decode_operand(test_external_jump, 0) = (stub_addr(elf_reloc.o, external_func) - test_external_jump)[27:2]
78 # jitlink-check: *{8}(got_addr(elf_reloc.o, external_func)) = external_func
79 .globl test_external_call
83 .size test_external_call, .-test_external_call
85 .globl test_external_jump
89 .size test_external_jump, .-test_external_jump
91 # Check R_AARCH64_LDST*_ABS_LO12_NC relocation of a local symbol
93 # The immediate value should be the symbol address right shifted according to its instruction bitwidth.
95 # jitlink-check: decode_operand(test_ldrb, 2) = named_data[11:0]
96 # jitlink-check: decode_operand(test_ldrsb, 2) = (named_data + 0)[11:0]
97 # jitlink-check: decode_operand(test_ldrh, 2) = (named_data + 0)[11:1]
98 # jitlink-check: decode_operand(test_ldrsh, 2) = (named_data + 0)[11:1]
99 # jitlink-check: decode_operand(test_ldr_32bit, 2) = (named_data + 0)[11:2]
100 # jitlink-check: decode_operand(test_ldr_64bit, 2) = (named_data + 0)[11:3]
101 # jitlink-check: decode_operand(test_strb, 2) = named_data[11:0]
102 # jitlink-check: decode_operand(test_strh, 2) = (named_data + 0)[11:1]
103 # jitlink-check: decode_operand(test_str_32bit, 2) = (named_data + 0)[11:2]
104 # jitlink-check: decode_operand(test_str_64bit, 2) = (named_data + 0)[11:3]
108 ldrb w0
, [x1
, :lo12
:named_data
]
109 .size test_ldrb, .-test_ldrb
113 ldrsb w0
, [x1
, :lo12
:named_data
]
114 .size test_ldrsb, .-test_ldrsb
118 ldrh w0
, [x1
, :lo12
:named_data
]
119 .size test_ldrh, .-test_ldrh
123 ldrsh w0
, [x1
, :lo12
:named_data
]
124 .size test_ldrsh, .-test_ldrsh
126 .globl test_ldr_32bit
128 ldr w0
, [x1
, :lo12
:named_data
]
129 .size test_ldr_32bit, .-test_ldr_32bit
131 .globl test_ldr_64bit
133 ldr x0
, [x1
, :lo12
:named_data
]
134 .size test_ldr_64bit, .-test_ldr_64bit
138 strb w0
, [x1
, :lo12
:named_data
]
139 .size test_strb, .-test_strb
143 strh w0
, [x1
, :lo12
:named_data
]
144 .size test_strh, .-test_strh
146 .globl test_str_32bit
148 str w0
, [x1
, :lo12
:named_data
]
149 .size test_str_32bit, .-test_str_32bit
151 .globl test_str_64bit
153 str x0
, [x1
, :lo12
:named_data
]
154 .size test_str_64bit, .-test_str_64bit
157 # Check R_AARCH64_MOVW_UABS_G*_NC relocation of a local symbol
159 # The immediate value should be the symbol address right shifted according to LSL value
161 # jitlink-check: decode_operand(test_movz_g0_nc, 1) = named_data[15:0]
162 # jitlink-check: decode_operand(test_movk_g0_nc, 2) = named_data[15:0]
163 # jitlink-check: decode_operand(test_movz_g1_nc, 1) = named_data[31:16]
164 # jitlink-check: decode_operand(test_movk_g1_nc, 2) = named_data[31:16]
165 # jitlink-check: decode_operand(test_movz_g2_nc, 1) = named_data[47:32]
166 # jitlink-check: decode_operand(test_movk_g2_nc, 2) = named_data[47:32]
167 # jitlink-check: decode_operand(test_movz_g3, 1) = named_data[63:48]
168 # jitlink-check: decode_operand(test_movk_g3, 2) = named_data[63:48]
170 .globl test_movz_g0_nc
172 movz x0
, #:abs_g0_nc:named_data
173 .size test_movz_g0_nc, .-test_movz_g0_nc
175 .globl test_movk_g0_nc
177 movk x0
, #:abs_g0_nc:named_data
178 .size test_movk_g0_nc, .-test_movk_g0_nc
180 .globl test_movz_g1_nc
182 movz x0
, #:abs_g1_nc:named_data
183 .size test_movz_g1_nc, .-test_movz_g1_nc
185 .globl test_movk_g1_nc
187 movk x0
, #:abs_g1_nc:named_data
188 .size test_movk_g1_nc, .-test_movk_g1_nc
190 .globl test_movz_g2_nc
192 movz x0
, #:abs_g2_nc:named_data
193 .size test_movz_g2_nc, .-test_movz_g2_nc
195 .globl test_movk_g2_nc
197 movk x0
, #:abs_g2_nc:named_data
198 .size test_movk_g2_nc, .-test_movk_g2_nc
202 movk x0
, #:abs_g3:named_data
203 .size test_movk_g3, .-test_movk_g3
207 movz x0
, #:abs_g3:named_data
208 .size test_movz_g3, .-test_movz_g3
210 # Check R_AARCH64_ABS64 relocation of a function pointer to local symbol
212 # jitlink-check: *{8}local_func_addr_quad = named_func
213 .globl local_func_addr_quad
215 local_func_addr_quad
:
217 .size local_func_addr_quad, 8
219 # Check R_AARCH64_ABS64 relocation of a function pointer to external symbol
221 # jitlink-check: *{8}external_func_addr_quad = external_func
222 .globl external_func_addr_quad
224 external_func_addr_quad
:
226 .size external_func_addr_quad, 8
228 # Check R_AARCH64_ADR_GOT_PAGE / R_AARCH64_LD64_GOT_LO12_NC handling with a
229 # reference to an external symbol. Validate both the reference to the GOT entry,
230 # and also the content of the GOT entry.
232 # For the ADRP :got: instruction we have the 21-bit delta to the 4k page
233 # containing the GOT entry for external_data.
235 # For the LDR :got_lo12: instruction we have the 12-bit offset of the entry
238 # jitlink-check: *{8}(got_addr(elf_reloc.o, external_data)) = external_data
239 # jitlink-check: decode_operand(test_adr_gotpage_external, 1) = \
240 # jitlink-check: (got_addr(elf_reloc.o, external_data)[32:12] - \
241 # jitlink-check: test_adr_gotpage_external[32:12])
242 # jitlink-check: decode_operand(test_ld64_gotlo12_external, 2) = \
243 # jitlink-check: got_addr(elf_reloc.o, external_data)[11:3]
244 .globl test_adr_gotpage_external
246 test_adr_gotpage_external
:
247 adrp x0
, :got
:external_data
248 .size test_adr_gotpage_external, .-test_adr_gotpage_external
250 .globl test_ld64_gotlo12_external
252 test_ld64_gotlo12_external
:
253 ldr x0
, [x0
, :got_lo12
:external_data
]
254 .size test_ld64_gotlo12_external, .-test_ld64_gotlo12_external
256 # Check R_AARCH64_TSTBR14 for tbz
258 # jitlink-check: decode_operand(test_tstbr14_tbz, 2) = \
259 # jitlink-check: (test_tstbr14_tbz_target - test_tstbr14_tbz)[16:2]
260 .globl test_tstbr14_tbz, test_tstbr14_tbz_target
263 tbz x0
, 0, test_tstbr14_tbz_target
265 test_tstbr14_tbz_target
:
266 .size test_tstbr14_tbz, .-test_tstbr14_tbz
268 # Check R_AARCH64_TSTBR14 for tbnz
270 # jitlink-check: decode_operand(test_tstbr14_tbnz, 2) = \
271 # jitlink-check: (test_tstbr14_tbnz_target - test_tstbr14_tbnz)[16:2]
272 .globl test_tstbr14_tbnz, test_tstbr14_tbnz_target
275 tbnz x0
, 0, test_tstbr14_tbnz_target
277 test_tstbr14_tbnz_target
:
278 .size test_tstbr14_tbnz, .-test_tstbr14_tbnz
280 # Check R_AARCH64_CONDBR19 for compare and branch instructions
282 # jitlink-check: decode_operand(test_condbr19_cbz, 1) = \
283 # jitlink-check: (test_condbr19_cbz_target - test_condbr19_cbz)[21:2]
284 .globl test_condbr19_cbz, test_condbr19_cbz_target
287 cbz x0
, test_condbr19_cbz_target
289 test_condbr19_cbz_target
:
290 .size test_condbr19_cbz, .-test_condbr19_cbz
292 # Check R_AARCH64_CONDBR19 for conditional branch instructions
294 # jitlink-check: decode_operand(test_condbr19_bc, 1) = \
295 # jitlink-check: (test_condbr19_bc_target - test_condbr19_bc)[21:2]
296 .globl test_condbr19_bc, test_condbr19_bc_target
299 b.eq test_condbr19_bc_target
301 test_condbr19_bc_target
:
302 .size test_condbr19_bc, .-test_condbr19_bc
306 .type named_data,@object
308 .quad 0x2222222222222222
309 .quad 0x3333333333333333
310 .size named_data, .-named_data
314 .type named_func,@function
317 .size named_func, .-named_func