Definition
An application binary interface (ABI) is the set of machine-level rules that separately compiled pieces of code must share to call one another and exchange data. It fixes the size and alignment §4.22 of each data type, structure layouts, which registers (the processor’s few built-in storage locations) or stack slots carry arguments and results, which registers a called function must preserve, and stack alignment. Native means code that the processor runs directly. An API (application programming interface) is the corresponding agreement at the level of source code.
The idea
A linker §12.4 connects a call to a function by name. In C, the name recorded in the object file, called the symbol, is the function’s name, on some platforms with an underscore added in front: make_pair carries no record of its parameters or of its result type. Suppose one source file declares the function wrongly. The linker finds make_pair, connects the call to it, and reports success. The caller and the called function, the callee, then disagree about where the argument is and where the result goes, and nothing stops the program from running with that disagreement.
A telephone call is a fair picture. Dialing the right number connects the two parties; it does not make them speak the same language. The symbol is the number, and the ABI is the language. The picture stops in one respect: two people who speak different languages notice at once, but two pieces of machine code that disagree about the ABI do not. Each reads registers and memory as its own rules say, and the result is a wrong value, corrupted memory or a crash, often far from the call.
How it works
The rules below are those of the base standard of the AAPCS (Procedure Call Standard for the Arm Architecture), the ABI for function calls on 32-bit Arm processors; every ABI must decide the same questions. The processor has 16 core registers of 32 bits, named r0 to r15. r13 is the stack pointer (SP), which holds the address of the top of the stack. r14 is the link register §4.45 (LR), which holds the return address. r15 is the program counter §2.9 (PC), which holds the address of the instruction being executed.
Arguments. The first four 32-bit words of arguments go in
r0tor3, further ones on the stack. A 64-bit argument, such as along long, takes the pairr0andr1or the pairr2andr3, skipping a register if necessary.Results. A result of up to 4 bytes is returned in
r0, and a 64-bit basic value, such as along long, inr0andr1. A structure larger than 4 bytes is returned in memory: the caller reserves space and passes its address inr0as a hidden first argument, so every visible argument moves up by one register.Caller-saved registers. The callee may overwrite
r0tor3andr12; a caller that needs their values afterwards saves them first.Callee-saved registers.
r4tor8,r10,r11and the stack pointer must hold on return the values they held on entry; a callee that uses them saves and restores them. Each platform defines the role ofr9; where it has no special role, it is callee-saved too.Stack. The stack grows toward lower addresses. The stack pointer is always a multiple of 4, and a multiple of 8 at every call to a function that separately compiled code may call.
Floating point. The base standard passes floating-point values in the core registers. The hard-float or VFP (vector floating point) variant, for processors with a floating-point unit, passes them in floating-point registers. Code built for one variant puts floating-point values where code built for the other does not look for them, so linkers such as
ldrefuse to combine the two.
Other ABIs choose differently. The System V ABI for x86-64, used by Linux, passes the first six integer arguments in rdi, rsi, rdx, rcx, r8 and r9; the Microsoft x64 convention used by Windows passes four, in rcx, rdx, r8 and r9.
Worked example
The callee is compiled from correct source for 32-bit Arm:
struct pair { int32_t a; int32_t b; };
struct pair make_pair(int32_t x) {
return (struct pair){ x, x + 1 };
}
The structure has two 4-byte fields, so it is 8 bytes long, which is more than 4. Under the AAPCS the caller must therefore pass in r0 the address of an 8-byte result buffer, and x arrives in r1. The callee stores x at that address and x + 1 at the address 4 bytes higher.
A second source file declares the function wrongly, as int32_t make_pair(int32_t x);, and calls make_pair(7). Its compiler follows the wrong declaration faithfully.
The caller puts 7 in
r0as the first argument and reserves no buffer.r1keeps whatever value it happens to hold, say 100.The linker finds the symbol
make_pair, connects the call, and reports success.The callee reads
r0as the buffer address andr1asx. It writes 100 into the 4 bytes at addresses 7 to 10, and into the 4 bytes at addresses 11 to 14.The caller reads
r0as the integer result. The callee was under no obligation to leave any particular value there.
Address 7 belongs to no buffer the caller reserved. Depending on what the memory map places there, the stores corrupt other data, cause a fault, or have no effect. The callee never received 7 as x, and no tool along the way reported an error.
Stated precisely
Two separately compiled pieces of code are ABI-compatible for a call when the caller’s view of the callee and the callee’s definition agree on four things.
The ABI and its variant: the same processor architecture and convention, such as the AAPCS base standard or its hard-float variant on 32-bit Arm, or System V or Microsoft on x86-64.
The function type: the number, order and types of the parameters, the result type, and whether the function takes a variable number of arguments.
The layout of every type passed by value or through a pointer: size, alignment, field offsets, structure padding §4.27 and byte order §4.21.
The choices the ABI leaves to the platform, such as the role of
r9on 32-bit Arm, the size ofwchar_tor the size of anenum.
When all four agree, the ABI guarantees that the callee finds each argument where the caller put it, that the caller finds the result where the callee left it, and that the callee-saved registers and the stack pointer hold, after the call, the values the caller left in them. Symbol resolution guarantees only that both sides use the same name.
Limits and common mistakes
A successful link is not evidence of ABI agreement. A C symbol carries no type. A C++ symbol encodes the parameter types, through a scheme called name mangling, but not the result type of an ordinary function and not the layout of the structures involved, so a changed structure still links.
Under the C standard, calling a function through a declaration whose type does not match its definition is undefined behavior §4.12. The register account above explains why such a program fails; it does not predict what an optimizing compiler will produce. Building with LTO §12.10 (link-time optimization), in which the compiler sees all files together, lets gcc warn about a declaration that does not match its definition.
An ABI does not cover meaning. It says where a pointer argument travels, not who must free the memory it points to, how long that memory stays valid, or which thread may use it. Those rules belong to the documentation of the API.
Changing the layout of a structure in a shared library breaks every program compiled against the old layout, although every name still resolves.
Calls from another language into native code go through an FFI §16.15 (foreign function interface), such as the ctypes module of Python §12.23. The FFI declaration plays the role of the C prototype, and the FFI cannot check it against the real function.
Questions answered
Why does resolving the expected function symbol not prove that caller and callee are ABI-compatible?
Resolving the symbol proves only that caller and callee use the same name, and the name does not record what ABI (application binary interface) compatibility depends on. In C, a symbol is the bare function name, with no parameter types, no result type and no structure layout. C++ name mangling adds parameter types, but still no result type for an ordinary function and no layout. Compatibility needs both sides to agree on the ABI variant, the exact function type and the layout of every type passed.
For example, on 32-bit Arm a function that returns an 8-byte structure expects the address of a result buffer in register r0 and its first visible argument in r1. A caller that wrongly declares it as returning an integer puts its argument, say 7, in r0. The link succeeds, and the callee writes its result to memory address 7. Compatibility comes from compiling both sides against the same header file, building both for the same ABI variant, and checking every hand-written declaration for a call from another language against the real prototype.
Related entries: Cross-compilation (§12.1), Toolchain (§12.2), Guest-host ABI (§2.37), Struct padding and packing (§4.27), ctypes and foreign function interface (FFI) (§16.15), Linker (§12.4), Link register (LR) (§4.45), Undefined behavior (UB) (§4.12). Further reading: [82].