Calling Conventions

Compilers handle the function calling processes differently, and we have several conventions.

  • Adapted to how programmers use the languages (number of arguments).

  • Adapted to several registers and other architectural details.

These dictate:

  • How arguments are passed to the callee.

  • How return codes are passed to the caller.

  • Who allocates the stack?

  • Who stores important registers such as the Program Counter.

cdecl

Created by Microsoft compilers, widely used in x86, including GCC.

  • The standard method for most code in x86 environments.

Arguments: passed in the stack, in inverted order (right to left).

  • First argument is pushed last.

Registers: Mixed

  • Caller saves RIP, A, C, D.

  • Callee saves BP, and others and restores RIP

stdcall

Official call convention for the Win32API (32 bits).

Arguments: passed in the stack from right to left.

  • Additional arguments are passed in the stack.

Registers: Callee saves.

  • Except EAX, ECX and EDX which can be freely used.

Stack Red Zone: Leaf functions have a 128-byte area kept safe which doesn’t need to be allocated.

  • Can be used for local variables, and avoids the use of two operations (sub rsp, add rsp).

  • Leaf functions are functions that do not call others.

fastcall

Official call convention for Win32API 64bits.

Arguments: left to right, first as registers.

  • Additional arguments are passed in the stack.

Registers: Caller saves.

Stack Shadow Zone: Leaf functions have a 32 byte area kept safe which doesn’t need to be allocated.

  • Can be used for local variables, and avoids the use of two operations (sub rsp, add rsp).

  • Leaf functions are functions that do not call others.

Fastcall for 64bits (Windows)

Official convention for x86_64 architectures with MSVC (Windows).

  • Mandatory if compiling for x86_64 in Windows.

Arguments: passed as RDX, RCX, R8, R9.

  • Additional arguments are passed in the stack (right to left).

Registers: Mixed.

  • Caller save: RAX, RCX, RDX, R8, R9, R10, R11.

  • Callee save: RBX, RBP, RDI, RSI, RSP, R12, R13, R14, and R15.

Stack Red Zone: Leaf functions have a 32 byte area kept safe, allocated by the callee.

  • Can be used to store RDX, RCX, R8, R9.

  • (Leaf functions are functions that do not call others).

System V AMD64 ABI

Official convention for x64 architectures using Linux, BSD, Unix, and Windows.

Arguments: passed as RDI, RSI, RDX, RCX, R8, R9.

  • Additional arguments are passed in the stack.

Registers: Caller saves.

  • Except for RBX, RSP, RBP, R12-R15 which callee must save if they are used.

Stack Red Zone: Leaf functions have a 128-byte area kept safe which doesn’t need to be allocated.

  • Can be used for local variables, and avoids the use of two operations (sub rsp, add rsp).

  • Leaf functions are functions that do not call others.

Last updated