Calling Conventions: Difference between revisions

Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content added Content deleted
(Add table of calling conventions)
Line 7: Line 7:


== Cheat Sheets ==
== Cheat Sheets ==

Here is a quick overview of common calling conventions. Note that the calling conventions are usually more complex than represented here (for instance, how is a large struct returned? How about a struct that fits in two registers? How about va_list's?). Look up the specifications if you want to be certain. It may be useful to write a test function and use gcc -S to see how the compiler generates code, which may give a hint of how the calling convention specification should be interpreted.


{| {{wikitable}}
{| {{wikitable}}
Line 16: Line 18:
! Scratch Registers
! Scratch Registers
! Preserved Registers
! Preserved Registers
! Call List
|-
|-
| System V i386 || eax, edx || none || stack || || eax, ecx, edx || ebx, esi, edi, ebp, esp
| System V i386 || eax, edx || none || stack (right to left) || || eax, ecx, edx || ebx, esi, edi, ebp, esp || ebp
|-
|-
| System V X86_64 || rax, rdx || rdi, rsi, rdx, rcx, r8, r9 || stack || 16-byte at call<sup>[[#Note1|1]]</sup> || rax, rdi, rsi, rdx, rcx, r8, r9, r10, r11 || rbx, rsp, rbp, r12, r13, r14, r15
| System V X86_64<sup>[[#Note1|1]]</sup> || rax, rdx || rdi, rsi, rdx, rcx, r8, r9 || stack (right to left) || 16-byte at call<sup>[[#Note2|2]]</sup> || rax, rdi, rsi, rdx, rcx, r8, r9, r10, r11 || rbx, rsp, rbp, r12, r13, r14, r15 || rbp
|-
|-
| ARM || r0, r1 || r0, r1, r2, r3 || stack || 8 byte<sup>[[#Note2|2]]</sup> || r0, r1, r2, r3, r12 || r4, r5, r6, r7, r8, r9, r10, r11, r13, r14
| ARM || r0, r1 || r0, r1, r2, r3 || stack || 8 byte<sup>[[#Note3|3]]</sup> || r0, r1, r2, r3, r12 || r4, r5, r6, r7, r8, r9, r10, r11, r13, r14 ||
|}
|}


<small id="Note1">Note 1: There is a 128 byte area below the stack called the 'red zone', which may be used by leaf functions without increasing %rsp. This requires the kernel to increase %rsp by an additional 128 bytes upon signals in user-space. This is <em>not</em> done by the CPU - if interrupts use the current stack (as with kernel code), and the red zone is enabled (default), then interrupts will silently corrupt the stack. Always pass -mno-red-zone to kernel code (even support libraries such as libc's embedded in the kernel) if interrupts don't respect the red zone.</small>
<small id="Note1">Note 1: Stack is 16 byte aligned at time of call. The call pushes %rip, so the stack is 16-byte aligned again if the callee pushes %rbp.</small>

<small id="Note2">Note 2: Stack is 16 byte aligned at time of call. The call pushes %rip, so the stack is 16-byte aligned again if the callee pushes %rbp.</small>


<small id="Note2">Note 2: Stack is 8 byte aligned at all times outside of prologue/epilogue of function.</small>
<small id="Note3">Note 3: Stack is 8 byte aligned at all times outside of prologue/epilogue of function.</small>


==External References==
==External References==