]> git.saurik.com Git - apple/ld64.git/blame_incremental - unit-tests/test-cases/relocs-asm/relocs-asm.s
ld64-409.12.tar.gz
[apple/ld64.git] / unit-tests / test-cases / relocs-asm / relocs-asm.s
... / ...
CommitLineData
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
34L0:
35 ldr r0, [pc, r0]
36
37 @ PIC load of c
38 ldr r0, L100+4
39L1:
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
57L100:
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
961: 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
123L101:
124 .long _test_hidden_weak - L101
125
126
127 .text
128_pointer_diffs:
1291: .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
179L2: add r3, pc
180 ldr r3, L7
181L3: add r3, pc
182 ldr r3, L8
183L4: add r3, pc
184 ldr r3, L9
185L5: add r3, pc
186 pop {r7, pc}
187 .align 2
188L6: .long _thumb1-(L2+4)
189L7: .long _thumb2-(L3+4)
190L7a:.long _thumb3-(L3+4)
191L7b:.long _thumb4-(L3+4)
192L8: .long _arm1-(L4+4)
193L9: .long _arm2-(L5+4)
194L9a:.long _arm3-(L5+4)
195L9b:.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
203L12:add r3, pc
204 ldr r3, L17
205L13:add r3, pc
206 ldr r3, L18
207L14:add r3, pc
208 ldr r3, L19
209L15:add r3, pc
210 pop {r7, pc}
211 .align 2
212L16: .long _thumb1-(L12+8)
213L17: .long _thumb2-(L13+8)
214L17a: .long _thumb3-(L13+8)
215L17b: .long _thumb4-(L13+8)
216L18: .long _arm1-(L14+8)
217L19: .long _arm2-(L15+8)
218L19a: .long _arm3-(L15+8)
219L19b: .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 movw r0, :lower16:_thumbTarget
248 movt r0, :upper16:_thumbTarget
249 movw r0, :lower16:_externalTarget
250 movt r0, :upper16:_externalTarget
251 movw r0, :lower16:_externalTarget+61447
252 movt r0, :upper16:_externalTarget+61447
253Lpicbase:
254 movw r0, :lower16:_datahilo16 - Lpicbase
255 movt r0, :upper16:_datahilo16 - Lpicbase
256 movw r0, :lower16:_datahilo16+4 - Lpicbase
257 movt r0, :upper16:_datahilo16+4 - Lpicbase
258 movw r0, :lower16:_datahilo16alt - Lpicbase
259 movt r0, :upper16:_datahilo16alt - Lpicbase
260 movw r0, :lower16:_datahilo16alt+61440 - Lpicbase
261 movt r0, :upper16:_datahilo16alt+61440 - Lpicbase
262 movw r0, :lower16:_datahilo16alt+2048 - Lpicbase
263 movt r0, :upper16:_datahilo16alt+2048 - Lpicbase
264 movw r0, :lower16:_datahilo16alt+1792 - Lpicbase
265 movt r0, :upper16:_datahilo16alt+1792 - Lpicbase
266 movw r0, :lower16:_datahilo16alt+165 - Lpicbase
267 movt r0, :upper16:_datahilo16alt+165 - Lpicbase
268 movw r0, :lower16:_thumbTarget - Lpicbase
269 movt r0, :upper16:_thumbTarget - Lpicbase
270 bx lr
271
272 .code 16
273 .thumb_func _thumb16tests
274_thumb16tests:
275 movw r0, :lower16:_datahilo16
276 movt r0, :upper16:_datahilo16
277 movw r0, :lower16:_datahilo16+4
278 movt r0, :upper16:_datahilo16+4
279 movw r0, :lower16:_datahilo16alt
280 movt r0, :upper16:_datahilo16alt
281 movw r0, :lower16:_datahilo16alt+61440
282 movt r0, :upper16:_datahilo16alt+61440
283 movw r0, :lower16:_datahilo16alt+2048
284 movt r0, :upper16:_datahilo16alt+2048
285 movw r0, :lower16:_datahilo16alt+1792
286 movt r0, :upper16:_datahilo16alt+1792
287 movw r0, :lower16:_datahilo16alt+165
288 movt r0, :upper16:_datahilo16alt+165
289 movw r0, :lower16:_thumbTarget
290 movt r0, :upper16:_thumbTarget
291 movw r0, :lower16:_externalTarget
292 movt r0, :upper16:_externalTarget
293 movw r0, :lower16:_externalTarget+61447
294 movt r0, :upper16:_externalTarget+61447
295Lpicbase2:
296 movw r0, :lower16:_datahilo16 - Lpicbase2
297 movt r0, :upper16:_datahilo16 - Lpicbase2
298 movw r0, :lower16:_datahilo16+4 - Lpicbase2
299 movt r0, :upper16:_datahilo16+4 - Lpicbase2
300 movw r0, :lower16:_datahilo16alt - Lpicbase2
301 movt r0, :upper16:_datahilo16alt - Lpicbase2
302 movw r0, :lower16:_datahilo16alt+61440 - Lpicbase2
303 movt r0, :upper16:_datahilo16alt+61440 - Lpicbase2
304 movw r0, :lower16:_datahilo16alt+2048 - Lpicbase2
305 movt r0, :upper16:_datahilo16alt+2048 - Lpicbase2
306 movw r0, :lower16:_datahilo16alt+1792 - Lpicbase2
307 movt r0, :upper16:_datahilo16alt+1792 - Lpicbase2
308 movw r0, :lower16:_datahilo16alt+165 - Lpicbase2
309 movt r0, :upper16:_datahilo16alt+165 - Lpicbase2
310 movw r0, :lower16:_thumbTarget - Lpicbase2
311 movt r0, :upper16:_thumbTarget - Lpicbase2
312 bx lr
313
314 .code 16
315 .thumb_func _thumbTarget
316_thumbTarget:
317 nop
318 bx lr
319
320 .data
321_datahilo16: .long 0
322_datahilo16alt: .long 0
323
324
325
326#endif
327
328#endif
329
330#if __ppc__ || __ppc64__
331
332 .text
333 .align 2
334
335 .globl _test_loads
336_test_loads:
337 stmw r30,-8(r1)
338 stwu r1,-48(r1)
339Lpicbase:
340
341 ; PIC load of a
342 addis r2,r10,ha16(_a-Lpicbase)
343 lwz r2,lo16(_a-Lpicbase)(r2)
344
345 ; PIC load of c
346 addis r2,r10,ha16(_c-Lpicbase)
347 lwz r2,lo16(_c-Lpicbase)(r2)
348
349 ; absolute load of a
350 lis r2,ha16(_a)
351 lwz r2,lo16(_a)(r2)
352
353 ; absolute load of c
354 lis r2,ha16(_c)
355 lwz r2,lo16(_c)(r2)
356
357 ; absolute load of external
358 lis r2,ha16(_ax)
359 lwz r2,lo16(_ax)(r2)
360
361 ; absolute lea of external
362 lis r2,hi16(_ax)
363 ori r2,r2,lo16(_ax)
364
365
366 ; PIC load of a + addend
367 addis r2,r10,ha16(_a+0x19000-Lpicbase)
368 lwz r2,lo16(_a+0x19000-Lpicbase)(r2)
369
370 ; absolute load of a + addend
371 lis r2,ha16(_a+0x19000)
372 lwz r2,lo16(_a+0x19000)(r2)
373
374 ; lea of a + addend
375 lis r2,ha16(_a+0x19000)
376 addi r2,r2,lo16(_a+0x19000)
377
378 ; alt lea of a + addend
379 lis r2,hi16(_a+0x19000)
380 ori r2,r2,lo16(_a+0x19000)
381
382 ; absolute load of external + addend
383 lis r2,ha16(_ax+0x19000)
384 lwz r2,lo16(_ax+0x19000)(r2)
385
386 ; absolute lea of external + addend
387 lis r2,hi16(_ax+0x19000)
388 ori r2,r2,lo16(_ax+0x19000)
389
390
391 ; PIC load of a + addend
392 addis r2,r10,ha16(_a+0x09000-Lpicbase)
393 lwz r2,lo16(_a+0x09000-Lpicbase)(r2)
394
395 ; absolute load of a + addend
396 lis r2,ha16(_a+0x09000)
397 lwz r2,lo16(_a+0x09000)(r2)
398
399 ; lea of a + addend
400 lis r2,ha16(_a+0x09000)
401 addi r2,r2,lo16(_a+0x09000)
402
403 ; alt lea of a + addend
404 lis r2,hi16(_a+0x09000)
405 ori r2,r2,lo16(_a+0x09000)
406
407 ; absolute load of external + addend
408 lis r2,ha16(_ax+0x09000)
409 lwz r2,lo16(_ax+0x09000)(r2)
410
411 ; absolute lea of external + addend
412 lis r2,hi16(_ax+0x09000)
413 ori r2,r2,lo16(_ax+0x09000)
414
415 blr
416
417
418_test_calls:
419 ; call internal
420 bl _test_branches
421
422 ; call internal + addend
423 bl _test_branches+0x19000
424
425 ; call external
426 bl _external
427
428 ; call external + addend
429 bl _external+0x19000
430
431
432_test_branches:
433 ; call internal
434 bne _test_calls
435
436 ; call internal + addend
437 bne _test_calls+16
438
439 ; call external
440 bne _external
441
442 ; call external + addend
443 bne _external+16
444
445 .globl _test_weak
446 .weak_definition _test_weak
447_test_weak:
448 nop
449 nop
450
451_test_weak_call:
452 bl _test_weak
453 bl _test_weak+4
454
455#endif
456
457
458
459#if __i386__
460 .text
461 .align 2
462
463Ltest_data:
464 .long 1
465 .long 2
466 .long 3
467
468 .globl _test_loads
469_test_loads:
470 pushl %ebp
471Lpicbase:
472
473 # PIC load of a
474 movl _a-Lpicbase(%ebx), %eax
475
476 # absolute load of a
477 movl _a, %eax
478
479 # absolute load of external
480 movl _ax, %eax
481
482 # absolute lea of external
483 leal _ax, %eax
484
485
486 # PIC load of a + addend
487 movl _a-Lpicbase+0x19000(%ebx), %eax
488
489 # absolute load of a + addend
490 movl _a+0x19000(%ebx), %eax
491
492 # absolute load of external + addend
493 movl _ax+0x19000(%ebx), %eax
494
495 # absolute lea of external + addend
496 leal _ax+0x1900, %eax
497
498 # absolute load of _test_data with negative addend and local label
499 movl Ltest_data-16(%edi),%eax
500 movq Ltest_data-16(%edi),%mm4
501
502 ret
503
504
505_test_calls:
506 # call internal
507 call _test_branches
508
509 # call internal + addend
510 call _test_branches+0x19000
511
512 # 16-bit call internal
513 callw _test_branches
514
515 # 16-bit call internal + addend
516 callw _test_branches+13
517
518 # call external
519 call _external
520
521 # call external + addend
522 call _external+0x19000
523
524
525_test_branches:
526 # call internal
527 jne _test_calls
528
529 # call internal + addend
530 jne _test_calls+16
531
532 # call external
533 jne _external
534
535 # call external + addend
536 jne _external+16
537
538_pointer_diffs:
539 nop
540 call _get_ret_eax
5411: movl _foo-1b(%eax),%esi
542 movl _foo+10-1b(%eax),%esi
543 movl _test_branches-1b(%eax),%esi
544 movl _test_branches+3-1b(%eax),%esi
545 cmpl $(( (_test_branches - _test_loads) + -2097152 )),(%esp)
546 cmpl $(( (_test_calls - _test_loads) + -2097152 )),(%esp)
547
548
549_word_relocs:
550 callw _pointer_diffs
551
552_byte_relocs:
553 mov $100, %ecx
554c_1:
555 loop c_1
556 mov $100, %ecx
557c_2:
558 sub $(1), %ecx
559 jcxz c_2
560
561 .globl _test_weak
562 .weak_definition _test_weak
563_test_weak:
564 nop
565 nop
566
567_test_weak_call:
568 call _test_weak
569 call _test_weak+1
570
571#endif
572
573
574
575#if __x86_64__
576 .text
577 .align 2
578
579 .globl _test_loads
580_test_loads:
581
582 # PIC load of a
583 movl _a(%rip), %eax
584
585 # PIC load of a + addend
586 movl _a+0x1234(%rip), %eax
587
588 # PIC lea
589 leaq _a(%rip), %rax
590
591 # PIC lea through GOT
592 movq _a@GOTPCREL(%rip), %rax
593
594 # PIC access of GOT
595 pushq _a@GOTPCREL(%rip)
596
597 # PIC lea external through GOT
598 movq _ax@GOTPCREL(%rip), %rax
599
600 # PIC external access of GOT
601 pushq _ax@GOTPCREL(%rip)
602
603 # 1-byte store
604 movb $0x12, _a(%rip)
605 movb $0x12, _a+2(%rip)
606 movb $0x12, L0(%rip)
607
608 # 4-byte store
609 movl $0x12345678, _a(%rip)
610 movl $0x12345678, _a+4(%rip)
611 movl $0x12345678, L0(%rip)
612
613 # test local labels
614 lea L1(%rip), %rax
615 movl L0(%rip), %eax
616
617 ret
618
619
620_test_calls:
621 # call internal
622 call _test_branches
623
624 # call internal + addend
625 call _test_branches+0x19000
626
627 # call external
628 call _external
629
630 # call external + addend
631 call _external+0x19000
632
633
634_test_branches:
635 # call internal
636 jne _test_calls
637
638 # call internal + addend
639 jne _test_calls+16
640
641 # call external
642 jne _external
643
644 # call external + addend
645 jne _external+16
646
647_byte_relocs:
648 # nonsense loop that creates byte branch relocation
649 mov $100, %ecx
650c_1:
651 loop _byte_relocs
652 nop
653
654 .globl _test_weak
655 .weak_definition _test_weak
656_test_weak:
657 nop
658 nop
659
660_test_weak_call:
661 call _test_weak
662 call _test_weak+1
663
664#endif
665
666
667
668 # test that pointer-diff relocs are preserved
669 .text
670 .align 2
671_test_diffs:
672Llocal2:
673 .long 0
674 .long Llocal2-_test_branches
675 .long . - _test_branches
676 .long . - _test_branches + 8
677 .long _test_branches - .
678 .long _test_branches - . + 8
679 .long _test_branches - . - 8
680 .long 0
681 .long 0
682#if __ppc64__
683 .quad Llocal2-_test_branches
684#endif
685
686_foo: nop
687Lfoo: nop
688
689 .align 2
690_distance_from_foo:
691 .long 0
692 .long . - _foo
693 .long . - 8 - _foo
694
695
696_distance_to_foo:
697 .long _foo - .
698 .long _foo - . + 4
699
700
701_distance_to_here:
702 .long _foo - _distance_to_here
703 .long _foo - _distance_to_here - 4
704 .long _foo - _distance_to_here - 12
705 .long Lfoo - _distance_to_here
706Ltohere:
707 .long Lfoo - Ltohere
708 .long Lfoo - Ltohere - 4
709 .long 0
710
711
712#if __x86_64__
713 .data
714L0: .quad _test_branches
715_prev:
716 .quad _test_branches+4
717L1: .quad _test_branches - _test_diffs
718 .quad _test_branches - _test_diffs + 4
719 .long _test_branches - _test_diffs
720# .long LCL0-. ### assembler bug: should SUB/UNSIGNED with content= LCL0-24, or single pc-rel SIGNED reloc with content = LCL0-.+4
721 .quad L1
722 .quad L0
723 .quad _test_branches - .
724 .quad _test_branches - L1
725 .quad L1 - _prev
726 .quad _prev+100 - _test_branches
727 #tests support for 32-bit absolute pointers
728 .long _prev
729 .long L1
730
731# the following generates: _foo cannot be undefined in a subtraction expression
732# but it should be ok (it will be a linker error if _foo and _bar are not in same linkage unit)
733# .quad _foo - _bar ### assembler bug
734
735 .section __DATA,__data2
736LCL0: .long 2
737
738
739#endif
740
741
742 .data
743_a:
744 .long 0
745
746_b:
747#if __ppc__ || __i386__ || __arm__
748 .long _test_calls
749 .long _test_calls+16
750 .long _external
751 .long _external+16
752 .long _test_weak
753 .long _test_weak+16
754 .long Lother - . + 0x4000000
755Lother:
756 .long 0
757#elif __ppc64__ || __x86_64__
758 .quad _test_calls
759 .quad _test_calls+16
760 .quad _external
761 .quad _external+16
762 .quad _test_weak
763 .quad _test_weak+16
764 .quad Lother - . + 0x4000000
765Lother:
766 .quad 0
767#endif
768
769 # test that reloc sizes are the same
770Llocal3:
771 .long 0
772
773Llocal4:
774 .long 0
775
776 .long Llocal4-Llocal3
777
778Lfiller:
779 .space 0x9000
780_c:
781 .long 0
782