> For the complete documentation index, see [llms.txt](https://davidjosearaujo.gitbook.io/notes-mcs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://davidjosearaujo.gitbook.io/notes-mcs/reverse-engineering/binary-analysis/calling-conventions.md).

# 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

<figure><img src="https://1103423335-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvyEajzuIz0PAtDiV6JcU%2Fuploads%2FZryajubwAfDpg4hJMhzJ%2FScreenshot%20from%202024-03-27%2022-22-04.png?alt=media&amp;token=27cdee14-7c30-481e-bb33-ac0bf9388e6b" alt=""><figcaption></figcaption></figure>

## 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.

<figure><img src="https://1103423335-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvyEajzuIz0PAtDiV6JcU%2Fuploads%2FpL3zHamLvvTiEHncLXKn%2FScreenshot%20from%202024-03-27%2022-24-54.png?alt=media&amp;token=5a90ee1b-35f0-4fb2-8c95-ee27767d86cf" alt=""><figcaption></figcaption></figure>

## 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.

<figure><img src="https://1103423335-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvyEajzuIz0PAtDiV6JcU%2Fuploads%2FvwXnx4v9U5AoqsjFBaKG%2FScreenshot%20from%202024-03-27%2022-29-35.png?alt=media&amp;token=cf49ce28-cdd2-48e7-946b-4b1f7cdadd5e" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1103423335-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvyEajzuIz0PAtDiV6JcU%2Fuploads%2FuDYeUbRZRLIcRZxG2F0q%2FScreenshot%20from%202024-03-27%2022-38-15.png?alt=media&amp;token=e8746a1c-df86-413e-b469-f6a42ca25758" alt=""><figcaption></figcaption></figure>

### 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).

<figure><img src="https://1103423335-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvyEajzuIz0PAtDiV6JcU%2Fuploads%2Fr5MWMCR7u7YJ0x4BVKl2%2FScreenshot%20from%202024-03-27%2022-40-49.png?alt=media&amp;token=04b29dda-3979-436e-aa8a-8c280be890c9" alt=""><figcaption></figcaption></figure>

## 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.

<figure><img src="https://1103423335-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvyEajzuIz0PAtDiV6JcU%2Fuploads%2FMR7yzMlghtE9sutcwZYH%2FScreenshot%20from%202024-03-27%2022-44-44.png?alt=media&amp;token=03dcb6c3-6cc0-4afe-901f-04aa8168bf8d" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1103423335-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvyEajzuIz0PAtDiV6JcU%2Fuploads%2FelKVZl4UJ0c7hL29DZli%2FScreenshot%20from%202024-03-27%2022-45-16.png?alt=media&amp;token=24065ecf-229c-4bab-9974-f30201ede4b2" alt=""><figcaption></figcaption></figure>
