137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines# RUN: llvm-mc -triple=arm64-apple-ios7.0.0 -code-model=small -relocation-model=pic -filetype=obj -o %T/foo.o %s
237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines# RUN: llvm-rtdyld -triple=arm64-apple-ios7.0.0 -map-section foo.o,__text=0x10bc0 -verify -check=%s %/T/foo.o
337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    .section  __TEXT,__text,regular,pure_instructions
537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    .ios_version_min 7, 0
637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    .globl  _foo
737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    .align  2
837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines_foo:
937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    movz  w0, #0
1037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    ret
1137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
1237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    .globl  _test_branch_reloc
1337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    .align  2
1437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
1537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
1637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines# Test ARM64_RELOC_BRANCH26 relocation. The branch instruction only encodes 26
1737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines# bits of the 28-bit possible branch range. The lower two bits are always zero
1837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines# and therefore ignored.
1937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines# rtdyld-check:  decode_operand(br1, 0)[25:0] = (_foo - br1)[27:2]
2037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines_test_branch_reloc:
2137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesbr1:
2237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    b _foo
2337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    ret
2437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
2537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
2637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines# Test ARM64_RELOC_PAGE21 and ARM64_RELOC_PAGEOFF12 relocation. adrp encodes
2737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines# the PC-relative page (4 KiB) difference between the adrp instruction and the
2837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines# variable ptr. ldr encodes the offset of the variable within the page. The ldr
2937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines# instruction perfroms an implicit shift on the encoded immediate (imm<<3).
3037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines# rtdyld-check:  decode_operand(adrp1, 1) = (_ptr[32:12] - adrp1[32:12])
3137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines# rtdyld-check:  decode_operand(ldr1, 2) = _ptr[11:3]
3237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    .globl  _test_adrp_ldr
3337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    .align  2
3437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines_test_adrp_ldr:
3537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesadrp1:
3637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    adrp x0, _ptr@PAGE
3737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesldr1:
3837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    ldr  x0, [x0, _ptr@PAGEOFF]
3937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    ret
4037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
4137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines# Test ARM64_RELOC_GOT_LOAD_PAGE21 and ARM64_RELOC_GOT_LOAD_PAGEOFF12
4237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines# relocation. adrp encodes the PC-relative page (4 KiB) difference between the
4337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines# adrp instruction and the GOT entry for ptr. ldr encodes the offset of the GOT
4437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines# entry within the page. The ldr instruction perfroms an implicit shift on the
4537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines# encoded immediate (imm<<3).
4637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines# rtdyld-check:  *{8}(stub_addr(foo.o, __text, _ptr)) = _ptr
4737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines# rtdyld-check:  decode_operand(adrp2, 1) = (stub_addr(foo.o, __text, _ptr)[32:12] - adrp2[32:12])
4837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines# rtdyld-check:  decode_operand(ldr2, 2) = stub_addr(foo.o, __text, _ptr)[11:3]
4937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    .globl  _test_adrp_ldr
5037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    .align  2
5137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines_test_got_adrp_ldr:
5237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesadrp2:
5337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    adrp x0, _ptr@GOTPAGE
5437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesldr2:
5537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    ldr  x0, [x0, _ptr@GOTPAGEOFF]
5637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    ret
5737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
58cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar# rtdyld-check: decode_operand(add1, 2) = (tgt+8)[11:2] << 2
59cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    .globl  _test_explicit_addend_reloc
60cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    .align  4
61cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar_test_explicit_addend_reloc:
62cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainaradd1:
63cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    add x0, x0, tgt@PAGEOFF+8
64cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
65cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    .align  3
66cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainartgt:
67cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    .long 0
68cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    .long 0
69cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    .long 7
7037ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines
7137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines# Test ARM64_RELOC_UNSIGNED relocation. The absolute 64-bit address of the
7237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines# function should be stored at the 8-byte memory location.
7337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines# rtdyld-check: *{8}_ptr = _foo
7437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    .section  __DATA,__data
7537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    .globl  _ptr
7637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    .align  3
7737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    .fill 4096, 1, 0
7837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines_ptr:
7937ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    .quad _foo
80