TExcelTAD
Home › Reverse Engineering & Binary Analysis › Calling Conventions: System V and Microsoft x64

Calling Conventions: System V and Microsoft x64

When you land in the middle of an unknown function, the first questions are "what are its arguments?" and "what does it return?". The answers are not in the function; they are in the calling convention, the contract between caller and callee about which registers carry arguments, who saves what, and how the stack is laid out at the call. x86-64 has two conventions in daily use: the System V AMD64 ABI (Linux, the BSDs, macOS) and the Microsoft x64 convention (Windows). Mixing them up means misreading every function. System V Calling Convention in the assembly course teaches the Linux side by writing it; this lesson puts both side by side as an analyst reads them.

The two conventions

System V AMD64 (Linux, macOS, BSD) Microsoft x64 (Windows)
integer / pointer arguments rdi, rsi, rdx, rcx, r8, r9, then the stack rcx, rdx, r8, r9, then the stack
floating-point arguments xmm0-xmm7 xmm0-xmm3, sharing the four slots with the integer arguments (argument 2 is rdx or xmm1)
return value rax (and rdx for 128-bit), xmm0 for floats rax, xmm0 for floats
callee-saved (must survive a call) rbx, rbp, r12-r15 rbx, rbp, rdi, rsi, r12-r15, xmm6-xmm15
stack at the call 16-byte aligned 16-byte aligned
space the caller reserves none 32 bytes of shadow space (home space) above the return address, for the callee to spill rcx, rdx, r8, r9
red zone 128 bytes below rsp that a leaf function may use without moving rsp none
stack arguments start at (in the callee, before any push) [rsp+8] [rsp+0x28] (8 for the return address + 32 shadow)

Two consequences for reading code. On Windows, rdi and rsi are callee-saved, so a Windows function that uses them pushes them first; on Linux they are arguments and are never saved. And a Windows caller that calls anything at all has sub rsp, 0x28 or more in its prologue, because of the shadow space.

The same C, two binaries

The session compiles one callee and one caller with clang 18 twice: once for Linux (an ELF object) and once for Windows (a COFF object, --target=x86_64-pc-windows-msvc). Only the target changes. mix takes six integer arguments, more than Windows passes in registers.

bash · try it yourself
cat > mix.c <<'SRC'
long mix(long a, long b, long c, long d, long e, long f) {
    return a * b + c - d + e * f;
}
SRC
cat > caller.c <<'SRC'
long mix(long, long, long, long, long, long);
long call_mix(long x) { return mix(x, 2, 3, 4, 5, 6) + 1; }
SRC
show() { llvm-objdump -d --no-show-raw-insn -M intel "$1" | grep -E '^\s+[0-9a-f]+:' | sed 's/^ *//'; }
for t in x86_64-linux-gnu x86_64-pc-windows-msvc; do
  clang --target=$t -O2 -c mix.c -o mix-$t.o
  clang --target=$t -O2 -c caller.c -o caller-$t.o
  echo "== $t: mix";      show mix-$t.o
  echo "== $t: call_mix"; show caller-$t.o
done
== x86_64-linux-gnu: mix
0:      	imul	rdi, rsi
4:      	lea	rax, [rdi + rdx]
8:      	sub	rax, rcx
b:      	imul	r8, r9
f:      	add	rax, r8
12:      	ret
== x86_64-linux-gnu: call_mix
0:      	push	rax
1:      	mov	esi, 0x2
6:      	mov	edx, 0x3
b:      	mov	ecx, 0x4
10:      	mov	r8d, 0x5
16:      	mov	r9d, 0x6
1c:      	call	0x21 <call_mix+0x21>
21:      	inc	rax
24:      	pop	rcx
25:      	ret
== x86_64-pc-windows-msvc: mix
0:      	mov	eax, dword ptr [rsp + 0x30]
4:      	imul	ecx, edx
7:      	add	ecx, r8d
a:      	sub	ecx, r9d
d:      	imul	eax, dword ptr [rsp + 0x28]
12:      	add	eax, ecx
14:      	ret
== x86_64-pc-windows-msvc: call_mix
0:      	sub	rsp, 0x38
4:      	mov	dword ptr [rsp + 0x28], 0x6
c:      	mov	dword ptr [rsp + 0x20], 0x5
14:      	mov	edx, 0x2
19:      	mov	r8d, 0x3
1f:      	mov	r9d, 0x4
25:      	call	0x2a <call_mix+0x2a>
2a:      	inc	eax
2c:      	add	rsp, 0x38
30:      	ret

Read the four listings against the table:

  • Linux mix: the six arguments are rdi, rsi, rdx, rcx, r8, r9, all registers, no stack access, 64-bit operations throughout.
  • Windows mix: the first four are rcx, rdx, r8, r9; arguments 5 and 6 are loaded from [rsp+0x28] and [rsp+0x30], just above the return address (8 bytes) and the 32-byte shadow space. And every operation is 32-bit (ecx, dword ptr): on Windows long is 32 bits (the LLP64 data model), on Linux 64 bits (LP64). The same source declares different types on the two platforms, a classic source of truncation bugs in ported code.
  • Linux call_mix: x is already in rdi as the first argument of both functions, so only the constants are loaded into the other five registers. push rax only realigns the stack to 16 bytes for the call. The call target shows as the next instruction's address because this is an unlinked object whose displacement is still 0 (the linker fills it in, see Relocations).
  • Windows call_mix: x is already in rcx. sub rsp, 0x38 reserves the shadow space plus room for two stack arguments (and keeps alignment); the constants 5 and 6 go to [rsp+0x20] and [rsp+0x28], which are [rsp+0x28] and [rsp+0x30] in the callee once call has pushed the return address.

Recognising the convention in an unknown binary

  1. The file format usually decides: PE means Microsoft x64, ELF and Mach-O mean System V. Exceptions exist (Wine, whose Unix-side code calls Windows code with the Microsoft convention; functions marked __attribute__((ms_abi)) or sysv_abi; UEFI firmware, which uses the Microsoft convention inside PE files even when built on Linux).
  2. Which registers are read before they are written at the start of a function. A function that reads rdi and rsi right away is System V; one that reads rcx and rdx and spills them to [rsp+8], [rsp+0x10] is Microsoft x64 (a debug build spilling into its home space).
  3. The caller's setup just before a call: which registers it fills, and whether it reserves 32 bytes.

Counting the argument registers the caller fills gives a function's arity, which is the first thing to fix in a decompiler that guessed wrong.

How this matters for detection

  • Hooking and API monitoring (EDR, API monitors, Frida scripts) read arguments from the convention's registers at function entry. A tool configured for the wrong convention logs garbage.
  • Malware that switches conventions (calling Windows APIs from hand-written code, or position-independent shellcode resolving functions by hash) shows up as calls whose setup doesn't match the compiler's usual pattern: unusual, and worth a note.
  • Decompiler errors around a call (arguments missing or extra) often mean the function's prototype is wrong; set it explicitly.

How mistakes are prevented

  • For authors: declare every external function with a full prototype; on Windows, use the platform headers instead of hand-written declarations, and never call across conventions without the attribute (__attribute__((ms_abi)), sysv_abi).
  • For analysts: set the right compiler and convention in the disassembler's project settings before analysing, and correct function signatures as soon as you know them, so the decompiler propagates types.

Pitfalls

  • rcx in a Linux function is the fourth argument, not the first.
  • [rsp+0x28] on Windows is the fifth argument at entry; after the callee pushes registers or subtracts from rsp, the offset changes. Track rsp as you read.
  • Variadic functions (printf): System V sets al to the number of vector registers used (you'll see xor eax, eax before call printf); Windows passes floating-point varargs in both an integer and an xmm register.
  • 32-bit x86 has several more conventions (cdecl, stdcall, fastcall, thiscall); this course is 64-bit only.

Practice

1. A Windows function starts with mov [rsp+0x10], rdx and mov [rsp+8], rcx. What is happening?

It spills its first two arguments into the shadow (home) space the caller reserved, typical of an unoptimised build.

2. On Linux, a caller does mov edx, 16, mov rsi, rbx, mov rdi, rax, call memcpy. What are the arguments?

memcpy(dest = rax, src = rbx, n = 16).

3. Where would a seventh integer argument be in the Linux callee, at entry?

At [rsp+8] (above the return address); the first six travel in registers.

Test yourself

Which registers carry the first four integer arguments in the Microsoft x64 convention?

  1. rdi, rsi, rdx, rcx
  2. rcx, rdx, r8, r9
  3. rax, rbx, rcx, rdx
  4. r8, r9, r10, r11

In the Windows mix, why is the fifth argument at [rsp+0x28]?

  1. Because of the red zone
  2. 8 bytes of return address plus the 32-byte shadow space the caller reserves
  3. It is a local variable
  4. Because the function is optimised

Which register is callee-saved on Windows but an argument register on Linux?

  1. rbx
  2. rdi
  3. rax
  4. r12

A leaf function on Linux writes to [rsp-0x10] without moving rsp. Is that a bug?

  1. Yes, always
  2. No: System V gives leaf functions a 128-byte red zone below rsp
  3. Only on Windows
  4. Only with -O0