]> git.saurik.com Git - apple/ld64.git/blob - unit-tests/test-cases/relocs-asm/relocs-asm.s
06e10e8b428e6c597130a0faaf95acc46df21424
[apple/ld64.git] / unit-tests / test-cases / relocs-asm / relocs-asm.s
1 /*
2 * Copyright (c) 2005-2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #if __arm__
25 .text
26 .align 2
27
28 .globl _test_loads
29 _test_loads:
30 @ PIC load of a
31 ldr r0, L6
32 L0:
33 ldr r0, [pc, r0]
34
35 @ PIC load of c
36 ldr r0, L6+4
37 L1:
38 ldr r0, [pc, r0]
39
40 @ sorta-absolute load of a
41 ldr r0, L6+8
42 ldr r0, [r0, #0]
43
44 @ sorta-absolute load of c
45 ldr r0, L6+12
46 ldr r0, [r0, #0]
47
48 @ sorta-absolute load of external
49 ldr r0, L6+16
50 ldr r0, [r0, #0]
51
52 @ PIC load of a + addend ??
53 bx lr
54
55 L6:
56 .long _a-(L0+8)
57 .long _c-(L1+8)
58 .long _a
59 .long _c
60 .long _ax
61
62 _test_calls:
63 @ call internal
64 bl _test_branches
65
66 @ call internal + addend
67 bl _test_branches+0x19000
68
69 @ call external
70 bl _external
71
72 @ call external + addend
73 bl _external+0x19000
74
75
76 _test_branches:
77 @ call internal
78 bne _test_calls
79
80 @ call internal + addend
81 bne _test_calls+16
82
83 @ call external
84 bne _external
85
86 @ call external + addend
87 bne _external+16
88 #endif
89
90 #if __ppc__ || __ppc64__
91
92 .text
93 .align 2
94
95 .globl _test_loads
96 _test_loads:
97 stmw r30,-8(r1)
98 stwu r1,-48(r1)
99 Lpicbase:
100
101 ; PIC load of a
102 addis r2,r10,ha16(_a-Lpicbase)
103 lwz r2,lo16(_a-Lpicbase)(r2)
104
105 ; PIC load of c
106 addis r2,r10,ha16(_c-Lpicbase)
107 lwz r2,lo16(_c-Lpicbase)(r2)
108
109 ; absolute load of a
110 lis r2,ha16(_a)
111 lwz r2,lo16(_a)(r2)
112
113 ; absolute load of c
114 lis r2,ha16(_c)
115 lwz r2,lo16(_c)(r2)
116
117 ; absolute load of external
118 lis r2,ha16(_ax)
119 lwz r2,lo16(_ax)(r2)
120
121 ; absolute lea of external
122 lis r2,hi16(_ax)
123 ori r2,r2,lo16(_ax)
124
125
126 ; PIC load of a + addend
127 addis r2,r10,ha16(_a+0x19000-Lpicbase)
128 lwz r2,lo16(_a+0x19000-Lpicbase)(r2)
129
130 ; absolute load of a + addend
131 lis r2,ha16(_a+0x19000)
132 lwz r2,lo16(_a+0x19000)(r2)
133
134 ; lea of a + addend
135 lis r2,ha16(_a+0x19000)
136 addi r2,r2,lo16(_a+0x19000)
137
138 ; alt lea of a + addend
139 lis r2,hi16(_a+0x19000)
140 ori r2,r2,lo16(_a+0x19000)
141
142 ; absolute load of external + addend
143 lis r2,ha16(_ax+0x19000)
144 lwz r2,lo16(_ax+0x19000)(r2)
145
146 ; absolute lea of external + addend
147 lis r2,hi16(_ax+0x19000)
148 ori r2,r2,lo16(_ax+0x19000)
149
150
151 ; PIC load of a + addend
152 addis r2,r10,ha16(_a+0x09000-Lpicbase)
153 lwz r2,lo16(_a+0x09000-Lpicbase)(r2)
154
155 ; absolute load of a + addend
156 lis r2,ha16(_a+0x09000)
157 lwz r2,lo16(_a+0x09000)(r2)
158
159 ; lea of a + addend
160 lis r2,ha16(_a+0x09000)
161 addi r2,r2,lo16(_a+0x09000)
162
163 ; alt lea of a + addend
164 lis r2,hi16(_a+0x09000)
165 ori r2,r2,lo16(_a+0x09000)
166
167 ; absolute load of external + addend
168 lis r2,ha16(_ax+0x09000)
169 lwz r2,lo16(_ax+0x09000)(r2)
170
171 ; absolute lea of external + addend
172 lis r2,hi16(_ax+0x09000)
173 ori r2,r2,lo16(_ax+0x09000)
174
175 blr
176
177
178 _test_calls:
179 ; call internal
180 bl _test_branches
181
182 ; call internal + addend
183 bl _test_branches+0x19000
184
185 ; call external
186 bl _external
187
188 ; call external + addend
189 bl _external+0x19000
190
191
192 _test_branches:
193 ; call internal
194 bne _test_calls
195
196 ; call internal + addend
197 bne _test_calls+16
198
199 ; call external
200 bne _external
201
202 ; call external + addend
203 bne _external+16
204 #endif
205
206
207
208 #if __i386__
209 .text
210 .align 2
211
212 .globl _test_loads
213 _test_loads:
214 pushl %ebp
215 Lpicbase:
216
217 # PIC load of a
218 movl _a-Lpicbase(%ebx), %eax
219
220 # absolute load of a
221 movl _a, %eax
222
223 # absolute load of external
224 movl _ax, %eax
225
226 # absolute lea of external
227 leal _ax, %eax
228
229
230 # PIC load of a + addend
231 movl _a-Lpicbase+0x19000(%ebx), %eax
232
233 # absolute load of a + addend
234 movl _a+0x19000(%ebx), %eax
235
236 # absolute load of external + addend
237 movl _ax+0x19000(%ebx), %eax
238
239 # absolute lea of external + addend
240 leal _ax+0x1900, %eax
241
242 ret
243
244
245 _test_calls:
246 # call internal
247 call _test_branches
248
249 # call internal + addend
250 call _test_branches+0x19000
251
252 # call external
253 call _external
254
255 # call external + addend
256 call _external+0x19000
257
258
259 _test_branches:
260 # call internal
261 jne _test_calls
262
263 # call internal + addend
264 jne _test_calls+16
265
266 # call external
267 jne _external
268
269 # call external + addend
270 jne _external+16
271
272 _pointer_diffs:
273 nop
274 call _get_ret_eax
275 1: movl _foo-1b(%eax),%esi
276 movl _foo+10-1b(%eax),%esi
277 movl _test_branches-1b(%eax),%esi
278 movl _test_branches+3-1b(%eax),%esi
279
280 _word_relocs:
281 callw _pointer_diffs
282
283 _byte_relocs:
284 mov $100, %ecx
285 c_1:
286 loop c_1
287 mov $100, %ecx
288 c_2:
289 sub $(1), %ecx
290 jcxz c_2
291
292 #endif
293
294
295
296 #if __x86_64__
297 .text
298 .align 2
299
300 .globl _test_loads
301 _test_loads:
302
303 # PIC load of a
304 movl _a(%rip), %eax
305
306 # PIC load of a + addend
307 movl _a+0x1234(%rip), %eax
308
309 # PIC lea
310 leaq _a(%rip), %rax
311
312 # PIC lea through GOT
313 movq _a@GOTPCREL(%rip), %rax
314
315 # PIC access of GOT
316 pushq _a@GOTPCREL(%rip)
317
318 # PIC lea external through GOT
319 movq _ax@GOTPCREL(%rip), %rax
320
321 # PIC external access of GOT
322 pushq _ax@GOTPCREL(%rip)
323
324 # 1-byte store
325 movb $0x12, _a(%rip)
326 movb $0x12, _a+2(%rip)
327 movb $0x12, L0(%rip)
328
329 # 4-byte store
330 movl $0x12345678, _a(%rip)
331 movl $0x12345678, _a+4(%rip)
332 movl $0x12345678, L0(%rip)
333
334 # test local labels
335 lea L1(%rip), %rax
336 movl L0(%rip), %eax
337
338 ret
339
340
341 _test_calls:
342 # call internal
343 call _test_branches
344
345 # call internal + addend
346 call _test_branches+0x19000
347
348 # call external
349 call _external
350
351 # call external + addend
352 call _external+0x19000
353
354
355 _test_branches:
356 # call internal
357 jne _test_calls
358
359 # call internal + addend
360 jne _test_calls+16
361
362 # call external
363 jne _external
364
365 # call external + addend
366 jne _external+16
367
368 _byte_relocs:
369 mov $100, %ecx
370 c_1:
371 loop _byte_relocs
372 nop
373
374 #endif
375
376
377
378 # test that pointer-diff relocs are preserved
379 .text
380 _test_diffs:
381 .align 2
382 Llocal2:
383 .long 0
384 .long Llocal2-_test_branches
385 .long . - _test_branches
386 .long . - _test_branches + 8
387 .long _test_branches - .
388 .long _test_branches - . + 8
389 .long _test_branches - . - 8
390 #if __ppc64__
391 .quad Llocal2-_test_branches
392 #endif
393
394 _foo: nop
395
396 .align 2
397 _distance_from_foo:
398 .long 0
399 .long . - _foo
400 .long . - 8 - _foo
401
402
403 _distance_to_foo:
404 .long _foo - .
405 .long _foo - . + 4
406
407
408 _distance_to_here:
409 .long _foo - _distance_to_here
410 .long _foo - _distance_to_here - 4
411 .long _foo - _distance_to_here - 12
412 .long 0
413
414
415 #if __x86_64__
416 .data
417 L0: .quad _test_branches
418 _prev:
419 .quad _test_branches+4
420 L1: .quad _test_branches - _test_diffs
421 .quad _test_branches - _test_diffs + 4
422 .long _test_branches - _test_diffs
423 # .long LCL0-. ### assembler bug: should SUB/UNSIGNED with content= LCL0-24, or single pc-rel SIGNED reloc with content = LCL0-.+4
424 .quad L1
425 .quad L0
426 .quad _test_branches - .
427 .quad _test_branches - L1
428 .quad L1 - _prev
429
430 # the following generates: _foo cannot be undefined in a subtraction expression
431 # but it should be ok (it will be a linker error if _foo and _bar are not in same linkage unit)
432 # .quad _foo - _bar ### assembler bug
433
434 .section __DATA,__data2
435 LCL0: .long 2
436
437
438 #endif
439
440
441 .data
442 _a:
443 .long 0
444
445 _b:
446 #if __ppc__ || __i386__ || __arm__
447 .long _test_calls
448 .long _test_calls+16
449 .long _external
450 .long _external+16
451 #elif __ppc64__ || __x86_64__
452 .quad _test_calls
453 .quad _test_calls+16
454 .quad _external
455 .quad _external+16
456 #endif
457
458 # test that reloc sizes are the same
459 Llocal3:
460 .long 0
461
462 Llocal4:
463 .long 0
464
465 .long Llocal4-Llocal3
466
467 Lfiller:
468 .space 0x9000
469 _c:
470 .long 0
471