NAME=ELF: aarch64 ET_REL CALL26 patches the branch immediate
FILE=bins/elf/random_39855/random_39855.oo
ARGS=-e bin.relocs.apply=true
CMDS=<<EOF
pd 2 @ 0x0800007c
EOF
EXPECT=<<EOF
            0x0800007c      7f490094       bl reloc.strcmp
        ,=< 0x08000080      a0000035       cbnz w0, 0x8000094
EOF
RUN

NAME=ELF: aarch64 ET_REL CALL26 resolves the analysis jump target
FILE=bins/elf/random_39855/random_39855.oo
ARGS=-e bin.relocs.apply=true
CMDS=<<EOF
p8 4 @ 0x0800007c
ao 1 @ 0x0800007c~jump
EOF
EXPECT=<<EOF
7f490094
jump: 0x08012678
EOF
RUN

NAME=ELF: aarch64 ET_REL PREL32 keeps the next word intact
FILE=bins/elf/random_39855/random_39855.oo
ARGS=-e bin.relocs.apply=true
CMDS=<<EOF
pxw 8 @ 0x080101c4
EOF
EXPECT=<<EOF
0x080101c4  0xfffefe7c 0x00000088                        |.......
EOF
RUN

NAME=ELF: aarch64 ET_REL branch relocs resolve to their targets
FILE=bins/elf/analysis/arm64-relocs
ARGS=-e bin.relocs.apply=true
CMDS=<<EOF
pd 6 @ 0x08000044
EOF
EXPECT=<<EOF
            0x08000044      06000094       bl sym.local_target         ; RELOC 32 local_target @ 0x0800005c
            0x08000048      b6000094       bl reloc.extern_target
        ,=< 0x0800004c      80001836       tbz w0, 3, sym.local_target ; RELOC 32 local_target @ 0x0800005c
       ,==< 0x08000050      60000054       b.eq sym.local_target       ; RELOC 32 local_target @ 0x0800005c
       ||   0x08000054      fd7bc1a8       ldp x29, x30, [sp], 0x10
      ,===< 0x08000058      b4000014       b reloc.extern_tail
EOF
RUN

NAME=ELF: aarch64 ET_REL JUMP26 import gets its own got slot
FILE=bins/elf/analysis/arm64-relocs
ARGS=-e bin.relocs.apply=true
CMDS=<<EOF
ir
EOF
EXPECT=<<EOF
vaddr      paddr      type   ntype name
---------------------------------------
0x08000044 0x00000044 ADD_32 283   local_target
0x0800004c 0x0000004c ADD_32 279   local_target
0x08000050 0x00000050 ADD_32 280   local_target
0x08000068 0x00000068 ADD_32 261   extern_target + 0x08000000
0x0800006c 0x0000006c ADD_32 258   local_target
0x08000070 0x00000070 ADD_64 257   local_target
0x08000320 0x00000048 ADD_32 283   extern_target
0x08000328 0x00000058 ADD_32 282   extern_tail
EOF
RUN

NAME=ELF: aarch64 ET_REL PREL32 and ABS relocs keep their field widths
FILE=bins/elf/analysis/arm64-relocs
ARGS=-e bin.relocs.apply=true
CMDS=<<EOF
pxw 16 @ 0x08000068
EOF
EXPECT=<<EOF
0x08000068  0x000002b8 0x0800005c 0x0800005c 0x00000000  ....\...\.......
EOF
RUN

NAME=ELF: aarch64 ET_REL relocated call creates a real xref
FILE=bins/elf/analysis/arm64-relocs
ARGS=-e bin.relocs.apply=true
CMDS=<<EOF
aa
axt @ sym.local_target
EOF
EXPECT=<<EOF
sym.branch_relocs 0x8000044 [CALL:--x] bl sym.local_target
EOF
RUN

NAME=ELF: aarch64 ET_REL GOT relocs patch an import slot but not a defined symbol
FILE=bins/elf/aarch64-got-etrel.o
ARGS=-e bin.relocs.apply=true
CMDS=<<EOF
ir
p8 20 @ 0x08000040
EOF
EXPECT=<<EOF
vaddr      paddr      type   ntype name
---------------------------------------
0x08000040 0x00000040 ADD_32 311   gdef
0x08000044 0x00000044 ADD_32 312   gdef
0x08000188 0x00000048 ADD_32 311   gundef
0x08000188 0x0000004c ADD_32 312   gundef
00000090000040f90100009021c440f9c0035fd6
EOF
RUN

NAME=ELF: aarch64 ET_REL ADR_GOT_PAGE encodes the page of the import slot
FILE=bins/elf/ptx/ptx_ops.o
ARGS=-e bin.relocs.apply=true
CMDS=<<EOF
ir~__stack_chk_guard:0
p8 8 @ 0x08000080
pd 2 @ 0x08000080
EOF
EXPECT=<<EOF
0x08003550 0x00000080 ADD_32 311   __stack_chk_guard
000000f000a842f9
            0x08000080      000000f0       adrp x0, 0x8003000
            0x08000084      00a842f9       ldr x0, [x0, 0x550]
EOF
RUN
