TExcelTAD
Home › Reverse Engineering & Binary Analysis › ARM64 Basics and AAPCS64

ARM64 Basics and AAPCS64

A large share of the binaries an analyst meets today are not x86-64 at all: Android apps' native libraries, iPhone and Apple-silicon Mac applications, routers, cameras and other IoT firmware, cloud servers on Graviton or Ampere chips. They run AArch64, the 64-bit ARM architecture (usually just "ARM64"). The good news is that ARM64 is more regular than x86-64: fixed-size instructions, many registers, a load/store design. One lesson is enough to read compiler output for it, using the same C you already read as x86-64.

Definitions

  • Registers: 31 general-purpose 64-bit registers x0-x30; w0-w30 are their low 32 bits (writing w0 clears the upper half of x0, as on x86-64). sp is the stack pointer, xzr/wzr read as zero, pc is not a general register.
  • Special roles: x29 = frame pointer (FP), x30 = link register (LR, holds the return address), x16/x17 = scratch for the linker's veneers and PLT stubs, x18 = platform register (reserved on Apple platforms and Windows).
  • Fixed 4-byte instructions, always aligned: a disassembler never desynchronises, and every instruction's address is a multiple of 4.
  • Load/store architecture: arithmetic works only on registers; memory is touched only by ldr/str and their pair forms ldp/stp.
  • Calls: bl f (branch with link) puts the return address in x30 instead of on the stack; ret jumps to x30. A function that calls others must save x30 first, which is why non-leaf functions start with stp x29, x30, [sp, #-16]!.
  • Flags (NZCV) are set only by instructions that say so (cmp, subs, adds, tst); conditions are suffixes: b.eq, b.lt (signed), b.lo (unsigned lower), csel x0, x1, x2, gt.

AAPCS64, the calling convention

AAPCS64 (Linux, Android; Apple and Windows variants differ in details)
integer / pointer arguments x0-x7, then the stack
floating-point arguments v0-v7 (d0, s0... for doubles, floats)
return value x0 (and x1), v0 for floats
callee-saved x19-x28, x29 (FP), the low 64 bits of v8-v15
indirect result (a returned struct in memory) its address in x8
stack 16-byte aligned at all times

Apple's arm64 variant passes variadic arguments (printf's ...) on the stack instead of in registers and reserves x18; Windows on ARM also reserves x18.

The same C on ARM64

The session compiles a few small functions with clang 18 for aarch64-linux-gnu (an object file is enough; nothing is linked or run) and disassembles them with the raw encodings visible, so you can see that every instruction is 4 bytes.

bash · try it yourself
cat > arm.c <<'SRC'
long mix(long a, long b, long c, long d, long e, long f) { return a * b + c - d + e * f; }
long elem(const long *a, long i) { return a[i]; }
int  max(int a, int b)           { return a > b ? a : b; }
long helper(long x);
long outer(long x)               { return helper(x) + helper(x + 1); }
SRC
clang --target=aarch64-linux-gnu -O2 -c arm.c -o arm.o
llvm-objdump -d arm.o | grep -E '^\s+[0-9a-f]+:|>:
  

 | sed 's/^ *//'
0000000000000000 <mix>:
0: 9b000828     	madd	x8, x1, x0, x2
4: cb030108     	sub	x8, x8, x3
8: 9b0420a0     	madd	x0, x5, x4, x8
c: d65f03c0     	ret
0000000000000010 <elem>:
10: f8617800     	ldr	x0, [x0, x1, lsl #3]
14: d65f03c0     	ret
0000000000000018 <max>:
18: 6b01001f     	cmp	w0, w1
1c: 1a81c000     	csel	w0, w0, w1, gt
20: d65f03c0     	ret
0000000000000024 <outer>:
24: a9be7bfd     	stp	x29, x30, [sp, #-0x20]!
28: a9014ff4     	stp	x20, x19, [sp, #0x10]
2c: 910003fd     	mov	x29, sp
30: aa0003f3     	mov	x19, x0
34: 94000000     	bl	0x34 <outer+0x10>
38: aa0003f4     	mov	x20, x0
3c: 91000660     	add	x0, x19, #0x1
40: 94000000     	bl	0x40 <outer+0x1c>
44: 8b140000     	add	x0, x0, x20
48: a9414ff4     	ldp	x20, x19, [sp, #0x10]
4c: a8c27bfd     	ldp	x29, x30, [sp], #0x20
50: d65f03c0     	ret

What to notice:

  • mix: arguments arrive in x0-x5. madd x8, x1, x0, x2 computes a*b + c in one instruction (multiply-add), sub subtracts d, and the second madd adds e*f straight into x0, the return register. Three-operand instructions mean no extra moves; x8 is just a scratch register here.
  • elem: ldr x0, [x0, x1, lsl #3] is the load of a[i], the index shifted by 3 (times 8), the same idea as x86-64's [rdi+rsi*8].
  • max: cmp then csel (conditional select), the ARM counterpart of cmov.
  • outer: a non-leaf function. It saves x29 and x30 with stp x29, x30, [sp, #-0x20]! (pre-index: decrement sp by 32, then store), saves the callee-saved x19 and x20 because it needs x and the first result to survive the calls, calls with bl, restores with ldp (post-index [sp], #0x20 adds the 32 back) and returns with ret. Each bl shows its own address as target because the object is unlinked and the offset is still 0.

Pointer authentication and BTI

Newer ARM cores add two hardware defences that show up as extra instructions. Pointer authentication (PAC) signs the return address in x30 with a secret key before it is stored on the stack (paciasp) and checks it before returning (autiasp); a corrupted return address fails the check. Branch Target Identification (BTI) marks valid targets of indirect branches with bti c, like Intel's endbr64. Apple's arm64e and recent Android platform builds use PAC; on Linux it is the compiler flag -mbranch-protection=standard.

bash · try it yourself
printf 'long helper(long x);\nlong outer(long x) { return helper(x) + 1; }\n' > pac.c
clang --target=aarch64-linux-gnu -O2 -mbranch-protection=standard -c pac.c -o pac.o
llvm-objdump -d pac.o | grep -E '^\s+[0-9a-f]+:' | sed 's/^ *//'
0: d503233f     	paciasp
4: a9bf7bfd     	stp	x29, x30, [sp, #-0x10]!
8: 910003fd     	mov	x29, sp
c: 94000000     	bl	0xc <outer+0xc>
10: 91000400     	add	x0, x0, #0x1
14: a8c17bfd     	ldp	x29, x30, [sp], #0x10
18: d50323bf     	autiasp
1c: d65f03c0     	ret

paciasp comes first and signs the return address before stp saves it; autiasp checks it after ldp restores it. There is no separate bti c: paciasp itself counts as a valid landing pad for an indirect call when BTI is on, so the compiler leaves it out (a function without paciasp would start with bti c instead). When you see these in a binary, the developer opted into the hardening; when you see a hand-written routine without them in a PAC-enabled binary, that routine is worth a closer look.

How this matters for detection

  • Firmware and mobile analysis: file and the ELF header's e_machine (183 = AArch64) tell you the architecture; set the disassembler accordingly, or it will decode ARM bytes as x86 garbage.
  • Hooks and patches on ARM64 replace whole 4-byte instructions; a branch (b) or a ldr x16, ...; br x16 sequence at a function's start where the compiler would have put stp x29, x30 or bti c/paciasp is a sign of a hook (legitimate instrumentation or not).
  • Missing PAC/BTI in one library of an otherwise hardened app suggests it was built differently, for instance a third-party component.

How mistakes are prevented

  • Build with -mbranch-protection=standard on ARM64 Linux targets that support it, and keep stack canaries and PIE as on x86-64.
  • For analysts: confirm the architecture and the ABI variant (Linux, Apple, Windows) before reading arguments, because x18 and variadic arguments differ.

Pitfalls

  • ret uses x30, not the stack: in a leaf function the return address may never touch memory.
  • Condition codes: b.lo/b.hs are unsigned (lower, higher-or-same), b.lt/b.ge signed, as jb/jae vs jl/jge on x86-64.
  • adrp + add pairs build addresses of globals and strings in 4 KiB pages; follow both instructions to find the target.
  • 32-bit ARM (A32/Thumb) in older firmware is a different instruction set with 2- and 4-byte instructions.

Practice

1. A function begins with stp x29, x30, [sp, #-48]!. What does it tell you?

It is a non-leaf function (it saves the link register because it calls others) with a 48-byte frame, and x29 will be set as the frame pointer next (mov x29, sp).

2. Which register holds the third integer argument under AAPCS64, and which holds the return value?

x2; the return value is in x0.

3. Why can a linear-sweep disassembler never get out of step on ARM64, unlike on x86-64?

Every instruction is exactly 4 bytes and 4-byte aligned, so instruction boundaries are known; data in code can still be mis-decoded, but it can't shift the boundaries of the code after it.

Test yourself

Where does bl put the return address?

  1. On the stack
  2. In x30, the link register
  3. In x29
  4. In sp

Which registers carry the first eight integer arguments in AAPCS64?

  1. x0-x7
  2. x19-x26
  3. rdi, rsi, rdx, rcx, r8, r9
  4. w8-w15

What is the ARM64 counterpart of x86-64's cmov?

  1. madd
  2. csel
  3. ldp
  4. adrp

What do paciasp and autiasp protect?

  1. The stack canary
  2. The return address in x30, signed before saving and checked before returning
  3. The heap
  4. System calls