Overview
ABI describes a system interface for compiled application programs. It is a set of specifications that detail:
- CPU instruction set, with details like register file structure, stack organization, memory access types
- Sizes, layouts, and alignments of data types
- Calling convention
- System calls
- Object files binary format
System V AMD64 ABI is the standard ABI used by Linux
Calling Convention
Calling convention describes the interface of the called code:
- The order in which parameters are allocated
- How parameters are passed (pushed on the stack, placed in registers, or a mix of both)
- Which registers (if any) are used for parameters
- Which register is used for the return value
- Which registers are caller-saved and which are callee-saved
- How the task of preparing the stack for, and restoring after, a function call is divided between the caller and the callee
The calling convention of a program’s language may differ from the calling convention of the underlying platform, OS, or library being linked to. The function declarations will include additional platform-specific keywords that indicate the calling convention to be used
Arguments Passing
The System V x86-64 ABI passes arguments in registers, which is more efficient than the System V x86 ABI’s stack-based argument passing. This method avoids the latency and extra instructions associated with storing arguments to the cache and then loading them back again in the callee
Caller-saved and Callee-saved Registers
Caller-saved registers can be overwritten by the called subroutine and it’s responsibility of the caller to preserve them
Callee-saved registers can’t be overwritten by the called subroutine and it’s responsibility of the subroutine to restore them to the state before the call