]> git.saurik.com Git - apple/ld64.git/blame - 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
CommitLineData
d696c285 1/*
a645023d 2 * Copyright (c) 2005-2010 Apple Inc. All rights reserved.
d696c285
A
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
55e3d2f6
A
24
25
2f2f92e4
A
26#if __arm__
27 .text
28 .align 2
29
30 .globl _test_loads
31_test_loads:
32 @ PIC load of a
d9246299 33 ldr r0, L100
2f2f92e4
A
34L0:
35 ldr r0, [pc, r0]
36
37 @ PIC load of c
d9246299 38 ldr r0, L100+4
2f2f92e4
A
39L1:
40 ldr r0, [pc, r0]
41
42 @ sorta-absolute load of a
d9246299 43 ldr r0, L100+8
2f2f92e4
A
44 ldr r0, [r0, #0]
45
46 @ sorta-absolute load of c
d9246299 47 ldr r0, L100+12
2f2f92e4
A
48 ldr r0, [r0, #0]
49
50 @ sorta-absolute load of external
d9246299 51 ldr r0, L100+16
2f2f92e4
A
52 ldr r0, [r0, #0]
53
54 @ PIC load of a + addend ??
55 bx lr
56
d9246299 57L100:
2f2f92e4
A
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
a645023d
A
85 @ call internal - addend
86 bne _test_calls-16
87
2f2f92e4
A
88 @ call external
89 bne _external
90
91 @ call external + addend
92 bne _external+16
55e3d2f6 93
55e3d2f6
A
94 nop
95 bl 1f
961: nop
d9246299 97
a645023d
A
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
d9246299
A
127 .text
128_pointer_diffs:
55e3d2f6
A
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
a645023d 136
d9246299
A
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)
a645023d
A
214L17a: .long _thumb3-(L13+8)
215L17b: .long _thumb4-(L13+8)
d9246299
A
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
a645023d
A
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
247Lpicbase:
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
281Lpicbase2:
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
d9246299 303
2f2f92e4
A
304#endif
305
d696c285
A
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)
315Lpicbase:
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
2f2f92e4
A
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
d696c285
A
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
2f2f92e4
A
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
d696c285
A
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
a645023d
A
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
d696c285
A
431#endif
432
433
434
435#if __i386__
436 .text
437 .align 2
a645023d
A
438
439Ltest_data:
440 .long 1
441 .long 2
442 .long 3
443
d696c285
A
444 .globl _test_loads
445_test_loads:
446 pushl %ebp
447Lpicbase:
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
a645023d
A
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
d696c285
A
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
55e3d2f6
A
488 # 16-bit call internal
489 callw _test_branches
490
491 # 16-bit call internal + addend
492 callw _test_branches+13
493
d696c285
A
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
a61fdf0a
A
513
514_pointer_diffs:
515 nop
516 call _get_ret_eax
5171: 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
55e3d2f6
A
521 cmpl $(( (_test_branches - _test_loads) + -2097152 )),(%esp)
522 cmpl $(( (_test_calls - _test_loads) + -2097152 )),(%esp)
523
a61fdf0a
A
524
525_word_relocs:
526 callw _pointer_diffs
527
2f2f92e4
A
528_byte_relocs:
529 mov $100, %ecx
530c_1:
531 loop c_1
532 mov $100, %ecx
533c_2:
534 sub $(1), %ecx
535 jcxz c_2
536
a645023d
A
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
d696c285
A
547#endif
548
549
550
69a49097
A
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)
74cfe461
A
581 movb $0x12, _a+2(%rip)
582 movb $0x12, L0(%rip)
69a49097
A
583
584 # 4-byte store
585 movl $0x12345678, _a(%rip)
74cfe461
A
586 movl $0x12345678, _a+4(%rip)
587 movl $0x12345678, L0(%rip)
69a49097
A
588
589 # test local labels
74cfe461
A
590 lea L1(%rip), %rax
591 movl L0(%rip), %eax
69a49097
A
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
2f2f92e4
A
622
623_byte_relocs:
a645023d 624 # nonsense loop that creates byte branch relocation
2f2f92e4 625 mov $100, %ecx
a645023d 626c_1:
2f2f92e4
A
627 loop _byte_relocs
628 nop
629
a645023d
A
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
69a49097
A
640#endif
641
642
643
d696c285
A
644 # test that pointer-diff relocs are preserved
645 .text
d696c285 646 .align 2
a645023d 647_test_diffs:
d696c285
A
648Llocal2:
649 .long 0
650 .long Llocal2-_test_branches
a61fdf0a
A
651 .long . - _test_branches
652 .long . - _test_branches + 8
653 .long _test_branches - .
654 .long _test_branches - . + 8
655 .long _test_branches - . - 8
d9246299
A
656 .long 0
657 .long 0
d696c285
A
658#if __ppc64__
659 .quad Llocal2-_test_branches
660#endif
661
a61fdf0a 662_foo: nop
a645023d 663Lfoo: nop
a61fdf0a
A
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
a645023d
A
681 .long Lfoo - _distance_to_here
682Ltohere:
683 .long Lfoo - Ltohere
684 .long Lfoo - Ltohere - 4
a61fdf0a
A
685 .long 0
686
d696c285 687
69a49097
A
688#if __x86_64__
689 .data
690L0: .quad _test_branches
691_prev:
692 .quad _test_branches+4
693L1: .quad _test_branches - _test_diffs
694 .quad _test_branches - _test_diffs + 4
695 .long _test_branches - _test_diffs
74cfe461 696# .long LCL0-. ### assembler bug: should SUB/UNSIGNED with content= LCL0-24, or single pc-rel SIGNED reloc with content = LCL0-.+4
69a49097 697 .quad L1
74cfe461 698 .quad L0
69a49097
A
699 .quad _test_branches - .
700 .quad _test_branches - L1
701 .quad L1 - _prev
a645023d 702 .quad _prev+100 - _test_branches
55e3d2f6
A
703 #tests support for 32-bit absolute pointers
704 .long _prev
705 .long L1
69a49097
A
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
712LCL0: .long 2
713
714
715#endif
716
717
d696c285
A
718 .data
719_a:
720 .long 0
721
722_b:
2f2f92e4 723#if __ppc__ || __i386__ || __arm__
d696c285
A
724 .long _test_calls
725 .long _test_calls+16
726 .long _external
727 .long _external+16
a645023d
A
728 .long _test_weak
729 .long _test_weak+16
69a49097 730#elif __ppc64__ || __x86_64__
d696c285
A
731 .quad _test_calls
732 .quad _test_calls+16
733 .quad _external
734 .quad _external+16
a645023d
A
735 .quad _test_weak
736 .quad _test_weak+16
d696c285
A
737#endif
738
739 # test that reloc sizes are the same
740Llocal3:
741 .long 0
742
743Llocal4:
744 .long 0
745
746 .long Llocal4-Llocal3
747
748Lfiller:
749 .space 0x9000
750_c:
751 .long 0
752