]> git.saurik.com Git - apple/ld64.git/blob - unit-tests/test-cases/relocs-asm/relocs-asm.s
ld64-123.2.tar.gz
[apple/ld64.git] / unit-tests / test-cases / relocs-asm / relocs-asm.s
1 /*
2 * Copyright (c) 2005-2010 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
25
26 #if __arm__
27 .text
28 .align 2
29
30 .globl _test_loads
31 _test_loads:
32 @ PIC load of a
33 ldr r0, L100
34 L0:
35 ldr r0, [pc, r0]
36
37 @ PIC load of c
38 ldr r0, L100+4
39 L1:
40 ldr r0, [pc, r0]
41
42 @ sorta-absolute load of a
43 ldr r0, L100+8
44 ldr r0, [r0, #0]
45
46 @ sorta-absolute load of c
47 ldr r0, L100+12
48 ldr r0, [r0, #0]
49
50 @ sorta-absolute load of external
51 ldr r0, L100+16
52 ldr r0, [r0, #0]
53
54 @ PIC load of a + addend ??
55 bx lr
56
57 L100:
58 .long _a-(L0+8)
59 .long _c-(L1+8)
60 .long _a
61 .long _c
62 .long _ax
63
64 _test_calls:
65 @ call internal
66 bl _test_branches
67
68 @ call internal + addend
69 bl _test_branches+0x19000
70
71 @ call external
72 bl _external
73
74 @ call external + addend
75 bl _external+0x19000
76
77
78 _test_branches:
79 @ call internal
80 bne _test_calls
81
82 @ call internal + addend
83 bne _test_calls+16
84
85 @ call internal - addend
86 bne _test_calls-16
87
88 @ call external
89 bne _external
90
91 @ call external + addend
92 bne _external+16
93
94 nop
95 bl 1f
96 1: nop
97
98
99 .globl _test_weak
100 .weak_definition _test_weak
101 _test_weak:
102 nop
103 nop
104
105 .globl _test_hidden_weak
106 .private_extern _test_hidden_weak
107 .weak_definition _test_hidden_weak
108 _test_hidden_weak:
109 nop
110 nop
111
112
113 _test_weak_call:
114 bl _test_weak
115 bl _test_weak+4
116
117
118 _test_weak_hidden_pointer_call:
119 ldr r12,L3
120 add r12, pc, r12
121 nop
122 bx r12
123 L101:
124 .long _test_hidden_weak - L101
125
126
127 .text
128 _pointer_diffs:
129 .long _foo-1b
130 .long _foo+10-1b
131 .long _test_branches-1b
132 .long _test_branches+3-1b
133 .long (_test_branches - _test_loads) + -2097152
134 .long (_test_calls - _test_loads) + -2097152
135
136
137 .text
138 .code 32
139 _arm1:
140 bx lr
141 _arm2:
142 bx lr
143 .weak_definition _arm3
144 .globl _arm3
145 .private_extern _arm3
146 _arm3:
147 bx lr
148 .weak_definition _arm4
149 .globl _arm4
150 .private_extern _arm4
151 _arm4:
152 bx lr
153
154 .code 16
155 .thumb_func _thumb1
156 _thumb1:
157 bx lr
158 .thumb_func _thumb2
159 _thumb2:
160 bx lr
161 .weak_definition _thumb3
162 .globl _thumb3
163 .private_extern _thumb3
164 .thumb_func _thumb3
165 _thumb3:
166 bx lr
167 .weak_definition _thumb4
168 .globl _thumb4
169 .private_extern _thumb4
170 .thumb_func _thumb4
171 _thumb4:
172 bx lr
173
174 .thumb_func _thumb_func_ref_test
175 _thumb_func_ref_test:
176 push {r7, lr}
177 add r7, sp, #0
178 ldr r3, L6
179 L2: add r3, pc
180 ldr r3, L7
181 L3: add r3, pc
182 ldr r3, L8
183 L4: add r3, pc
184 ldr r3, L9
185 L5: add r3, pc
186 pop {r7, pc}
187 .align 2
188 L6: .long _thumb1-(L2+4)
189 L7: .long _thumb2-(L3+4)
190 L7a:.long _thumb3-(L3+4)
191 L7b:.long _thumb4-(L3+4)
192 L8: .long _arm1-(L4+4)
193 L9: .long _arm2-(L5+4)
194 L9a:.long _arm3-(L5+4)
195 L9b:.long _arm4-(L5+4)
196
197 .code 32
198 .align 2
199 _arm_func_ref_test:
200 push {r7, lr}
201 add r7, sp, #0
202 ldr r3, L16
203 L12:add r3, pc
204 ldr r3, L17
205 L13:add r3, pc
206 ldr r3, L18
207 L14:add r3, pc
208 ldr r3, L19
209 L15:add r3, pc
210 pop {r7, pc}
211 .align 2
212 L16: .long _thumb1-(L12+8)
213 L17: .long _thumb2-(L13+8)
214 L17a: .long _thumb3-(L13+8)
215 L17b: .long _thumb4-(L13+8)
216 L18: .long _arm1-(L14+8)
217 L19: .long _arm2-(L15+8)
218 L19a: .long _arm3-(L15+8)
219 L19b: .long _arm4-(L15+8)
220
221 .section __DATA,__const
222 _myVTable:
223 .long _thumb1
224 .long _thumb2
225 .long _thumb3
226 .long _arm1
227 .long _arm2
228
229 #if __ARM_ARCH_7A__
230 .text
231 .align 2
232 _arm16tests:
233 movw r0, :lower16:_datahilo16
234 movt r0, :upper16:_datahilo16
235 movw r0, :lower16:_datahilo16+4
236 movt r0, :upper16:_datahilo16+4
237 movw r0, :lower16:_datahilo16alt
238 movt r0, :upper16:_datahilo16alt
239 movw r0, :lower16:_datahilo16alt+61440
240 movt r0, :upper16:_datahilo16alt+61440
241 movw r0, :lower16:_datahilo16alt+2048
242 movt r0, :upper16:_datahilo16alt+2048
243 movw r0, :lower16:_datahilo16alt+1792
244 movt r0, :upper16:_datahilo16alt+1792
245 movw r0, :lower16:_datahilo16alt+165
246 movt r0, :upper16:_datahilo16alt+165
247 Lpicbase:
248 movw r0, :lower16:_datahilo16 - Lpicbase
249 movt r0, :upper16:_datahilo16 - Lpicbase
250 movw r0, :lower16:_datahilo16+4 - Lpicbase
251 movt r0, :upper16:_datahilo16+4 - Lpicbase
252 movw r0, :lower16:_datahilo16alt - Lpicbase
253 movt r0, :upper16:_datahilo16alt - Lpicbase
254 movw r0, :lower16:_datahilo16alt+61440 - Lpicbase
255 movt r0, :upper16:_datahilo16alt+61440 - Lpicbase
256 movw r0, :lower16:_datahilo16alt+2048 - Lpicbase
257 movt r0, :upper16:_datahilo16alt+2048 - Lpicbase
258 movw r0, :lower16:_datahilo16alt+1792 - Lpicbase
259 movt r0, :upper16:_datahilo16alt+1792 - Lpicbase
260 movw r0, :lower16:_datahilo16alt+165 - Lpicbase
261 movt r0, :upper16:_datahilo16alt+165 - Lpicbase
262 bx lr
263
264 .code 16
265 .thumb_func _thumb16tests
266 _thumb16tests:
267 movw r0, :lower16:_datahilo16
268 movt r0, :upper16:_datahilo16
269 movw r0, :lower16:_datahilo16+4
270 movt r0, :upper16:_datahilo16+4
271 movw r0, :lower16:_datahilo16alt
272 movt r0, :upper16:_datahilo16alt
273 movw r0, :lower16:_datahilo16alt+61440
274 movt r0, :upper16:_datahilo16alt+61440
275 movw r0, :lower16:_datahilo16alt+2048
276 movt r0, :upper16:_datahilo16alt+2048
277 movw r0, :lower16:_datahilo16alt+1792
278 movt r0, :upper16:_datahilo16alt+1792
279 movw r0, :lower16:_datahilo16alt+165
280 movt r0, :upper16:_datahilo16alt+165
281 Lpicbase2:
282 movw r0, :lower16:_datahilo16 - Lpicbase2
283 movt r0, :upper16:_datahilo16 - Lpicbase2
284 movw r0, :lower16:_datahilo16+4 - Lpicbase2
285 movt r0, :upper16:_datahilo16+4 - Lpicbase2
286 movw r0, :lower16:_datahilo16alt - Lpicbase2
287 movt r0, :upper16:_datahilo16alt - Lpicbase2
288 movw r0, :lower16:_datahilo16alt+61440 - Lpicbase2
289 movt r0, :upper16:_datahilo16alt+61440 - Lpicbase2
290 movw r0, :lower16:_datahilo16alt+2048 - Lpicbase2
291 movt r0, :upper16:_datahilo16alt+2048 - Lpicbase2
292 movw r0, :lower16:_datahilo16alt+1792 - Lpicbase2
293 movt r0, :upper16:_datahilo16alt+1792 - Lpicbase2
294 movw r0, :lower16:_datahilo16alt+165 - Lpicbase2
295 movt r0, :upper16:_datahilo16alt+165 - Lpicbase2
296 bx lr
297
298 .data
299 _datahilo16: .long 0
300 _datahilo16alt: .long 0
301
302 #endif
303
304 #endif
305
306 #if __ppc__ || __ppc64__
307
308 .text
309 .align 2
310
311 .globl _test_loads
312 _test_loads:
313 stmw r30,-8(r1)
314 stwu r1,-48(r1)
315 Lpicbase:
316
317 ; PIC load of a
318 addis r2,r10,ha16(_a-Lpicbase)
319 lwz r2,lo16(_a-Lpicbase)(r2)
320
321 ; PIC load of c
322 addis r2,r10,ha16(_c-Lpicbase)
323 lwz r2,lo16(_c-Lpicbase)(r2)
324
325 ; absolute load of a
326 lis r2,ha16(_a)
327 lwz r2,lo16(_a)(r2)
328
329 ; absolute load of c
330 lis r2,ha16(_c)
331 lwz r2,lo16(_c)(r2)
332
333 ; absolute load of external
334 lis r2,ha16(_ax)
335 lwz r2,lo16(_ax)(r2)
336
337 ; absolute lea of external
338 lis r2,hi16(_ax)
339 ori r2,r2,lo16(_ax)
340
341
342 ; PIC load of a + addend
343 addis r2,r10,ha16(_a+0x19000-Lpicbase)
344 lwz r2,lo16(_a+0x19000-Lpicbase)(r2)
345
346 ; absolute load of a + addend
347 lis r2,ha16(_a+0x19000)
348 lwz r2,lo16(_a+0x19000)(r2)
349
350 ; lea of a + addend
351 lis r2,ha16(_a+0x19000)
352 addi r2,r2,lo16(_a+0x19000)
353
354 ; alt lea of a + addend
355 lis r2,hi16(_a+0x19000)
356 ori r2,r2,lo16(_a+0x19000)
357
358 ; absolute load of external + addend
359 lis r2,ha16(_ax+0x19000)
360 lwz r2,lo16(_ax+0x19000)(r2)
361
362 ; absolute lea of external + addend
363 lis r2,hi16(_ax+0x19000)
364 ori r2,r2,lo16(_ax+0x19000)
365
366
367 ; PIC load of a + addend
368 addis r2,r10,ha16(_a+0x09000-Lpicbase)
369 lwz r2,lo16(_a+0x09000-Lpicbase)(r2)
370
371 ; absolute load of a + addend
372 lis r2,ha16(_a+0x09000)
373 lwz r2,lo16(_a+0x09000)(r2)
374
375 ; lea of a + addend
376 lis r2,ha16(_a+0x09000)
377 addi r2,r2,lo16(_a+0x09000)
378
379 ; alt lea of a + addend
380 lis r2,hi16(_a+0x09000)
381 ori r2,r2,lo16(_a+0x09000)
382
383 ; absolute load of external + addend
384 lis r2,ha16(_ax+0x09000)
385 lwz r2,lo16(_ax+0x09000)(r2)
386
387 ; absolute lea of external + addend
388 lis r2,hi16(_ax+0x09000)
389 ori r2,r2,lo16(_ax+0x09000)
390
391 blr
392
393
394 _test_calls:
395 ; call internal
396 bl _test_branches
397
398 ; call internal + addend
399 bl _test_branches+0x19000
400
401 ; call external
402 bl _external
403
404 ; call external + addend
405 bl _external+0x19000
406
407
408 _test_branches:
409 ; call internal
410 bne _test_calls
411
412 ; call internal + addend
413 bne _test_calls+16
414
415 ; call external
416 bne _external
417
418 ; call external + addend
419 bne _external+16
420
421 .globl _test_weak
422 .weak_definition _test_weak
423 _test_weak:
424 nop
425 nop
426
427 _test_weak_call:
428 bl _test_weak
429 bl _test_weak+4
430
431 #endif
432
433
434
435 #if __i386__
436 .text
437 .align 2
438
439 Ltest_data:
440 .long 1
441 .long 2
442 .long 3
443
444 .globl _test_loads
445 _test_loads:
446 pushl %ebp
447 Lpicbase:
448
449 # PIC load of a
450 movl _a-Lpicbase(%ebx), %eax
451
452 # absolute load of a
453 movl _a, %eax
454
455 # absolute load of external
456 movl _ax, %eax
457
458 # absolute lea of external
459 leal _ax, %eax
460
461
462 # PIC load of a + addend
463 movl _a-Lpicbase+0x19000(%ebx), %eax
464
465 # absolute load of a + addend
466 movl _a+0x19000(%ebx), %eax
467
468 # absolute load of external + addend
469 movl _ax+0x19000(%ebx), %eax
470
471 # absolute lea of external + addend
472 leal _ax+0x1900, %eax
473
474 # absolute load of _test_data with negative addend and local label
475 movl Ltest_data-16(%edi),%eax
476 movq Ltest_data-16(%edi),%mm4
477
478 ret
479
480
481 _test_calls:
482 # call internal
483 call _test_branches
484
485 # call internal + addend
486 call _test_branches+0x19000
487
488 # 16-bit call internal
489 callw _test_branches
490
491 # 16-bit call internal + addend
492 callw _test_branches+13
493
494 # call external
495 call _external
496
497 # call external + addend
498 call _external+0x19000
499
500
501 _test_branches:
502 # call internal
503 jne _test_calls
504
505 # call internal + addend
506 jne _test_calls+16
507
508 # call external
509 jne _external
510
511 # call external + addend
512 jne _external+16
513
514 _pointer_diffs:
515 nop
516 call _get_ret_eax
517 1: movl _foo-1b(%eax),%esi
518 movl _foo+10-1b(%eax),%esi
519 movl _test_branches-1b(%eax),%esi
520 movl _test_branches+3-1b(%eax),%esi
521 cmpl $(( (_test_branches - _test_loads) + -2097152 )),(%esp)
522 cmpl $(( (_test_calls - _test_loads) + -2097152 )),(%esp)
523
524
525 _word_relocs:
526 callw _pointer_diffs
527
528 _byte_relocs:
529 mov $100, %ecx
530 c_1:
531 loop c_1
532 mov $100, %ecx
533 c_2:
534 sub $(1), %ecx
535 jcxz c_2
536
537 .globl _test_weak
538 .weak_definition _test_weak
539 _test_weak:
540 nop
541 nop
542
543 _test_weak_call:
544 call _test_weak
545 call _test_weak+1
546
547 #endif
548
549
550
551 #if __x86_64__
552 .text
553 .align 2
554
555 .globl _test_loads
556 _test_loads:
557
558 # PIC load of a
559 movl _a(%rip), %eax
560
561 # PIC load of a + addend
562 movl _a+0x1234(%rip), %eax
563
564 # PIC lea
565 leaq _a(%rip), %rax
566
567 # PIC lea through GOT
568 movq _a@GOTPCREL(%rip), %rax
569
570 # PIC access of GOT
571 pushq _a@GOTPCREL(%rip)
572
573 # PIC lea external through GOT
574 movq _ax@GOTPCREL(%rip), %rax
575
576 # PIC external access of GOT
577 pushq _ax@GOTPCREL(%rip)
578
579 # 1-byte store
580 movb $0x12, _a(%rip)
581 movb $0x12, _a+2(%rip)
582 movb $0x12, L0(%rip)
583
584 # 4-byte store
585 movl $0x12345678, _a(%rip)
586 movl $0x12345678, _a+4(%rip)
587 movl $0x12345678, L0(%rip)
588
589 # test local labels
590 lea L1(%rip), %rax
591 movl L0(%rip), %eax
592
593 ret
594
595
596 _test_calls:
597 # call internal
598 call _test_branches
599
600 # call internal + addend
601 call _test_branches+0x19000
602
603 # call external
604 call _external
605
606 # call external + addend
607 call _external+0x19000
608
609
610 _test_branches:
611 # call internal
612 jne _test_calls
613
614 # call internal + addend
615 jne _test_calls+16
616
617 # call external
618 jne _external
619
620 # call external + addend
621 jne _external+16
622
623 _byte_relocs:
624 # nonsense loop that creates byte branch relocation
625 mov $100, %ecx
626 c_1:
627 loop _byte_relocs
628 nop
629
630 .globl _test_weak
631 .weak_definition _test_weak
632 _test_weak:
633 nop
634 nop
635
636 _test_weak_call:
637 call _test_weak
638 call _test_weak+1
639
640 #endif
641
642
643
644 # test that pointer-diff relocs are preserved
645 .text
646 .align 2
647 _test_diffs:
648 Llocal2:
649 .long 0
650 .long Llocal2-_test_branches
651 .long . - _test_branches
652 .long . - _test_branches + 8
653 .long _test_branches - .
654 .long _test_branches - . + 8
655 .long _test_branches - . - 8
656 .long 0
657 .long 0
658 #if __ppc64__
659 .quad Llocal2-_test_branches
660 #endif
661
662 _foo: nop
663 Lfoo: nop
664
665 .align 2
666 _distance_from_foo:
667 .long 0
668 .long . - _foo
669 .long . - 8 - _foo
670
671
672 _distance_to_foo:
673 .long _foo - .
674 .long _foo - . + 4
675
676
677 _distance_to_here:
678 .long _foo - _distance_to_here
679 .long _foo - _distance_to_here - 4
680 .long _foo - _distance_to_here - 12
681 .long Lfoo - _distance_to_here
682 Ltohere:
683 .long Lfoo - Ltohere
684 .long Lfoo - Ltohere - 4
685 .long 0
686
687
688 #if __x86_64__
689 .data
690 L0: .quad _test_branches
691 _prev:
692 .quad _test_branches+4
693 L1: .quad _test_branches - _test_diffs
694 .quad _test_branches - _test_diffs + 4
695 .long _test_branches - _test_diffs
696 # .long LCL0-. ### assembler bug: should SUB/UNSIGNED with content= LCL0-24, or single pc-rel SIGNED reloc with content = LCL0-.+4
697 .quad L1
698 .quad L0
699 .quad _test_branches - .
700 .quad _test_branches - L1
701 .quad L1 - _prev
702 .quad _prev+100 - _test_branches
703 #tests support for 32-bit absolute pointers
704 .long _prev
705 .long L1
706
707 # the following generates: _foo cannot be undefined in a subtraction expression
708 # but it should be ok (it will be a linker error if _foo and _bar are not in same linkage unit)
709 # .quad _foo - _bar ### assembler bug
710
711 .section __DATA,__data2
712 LCL0: .long 2
713
714
715 #endif
716
717
718 .data
719 _a:
720 .long 0
721
722 _b:
723 #if __ppc__ || __i386__ || __arm__
724 .long _test_calls
725 .long _test_calls+16
726 .long _external
727 .long _external+16
728 .long _test_weak
729 .long _test_weak+16
730 #elif __ppc64__ || __x86_64__
731 .quad _test_calls
732 .quad _test_calls+16
733 .quad _external
734 .quad _external+16
735 .quad _test_weak
736 .quad _test_weak+16
737 #endif
738
739 # test that reloc sizes are the same
740 Llocal3:
741 .long 0
742
743 Llocal4:
744 .long 0
745
746 .long Llocal4-Llocal3
747
748 Lfiller:
749 .space 0x9000
750 _c:
751 .long 0
752