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-w30are their low 32 bits (writingw0clears the upper half ofx0, as on x86-64).spis the stack pointer,xzr/wzrread as zero,pcis 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/strand their pair formsldp/stp. - Calls:
bl f(branch with link) puts the return address inx30instead of on the stack;retjumps tox30. A function that calls others must savex30first, which is why non-leaf functions start withstp 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.
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]+:|>: