C Mastery / ABI and Calling Conventions
Part 5 — Compilation, Linking, and Building

ABI and Calling Conventions

This chapter explains the ABI (application binary interface) and the calling conventions it includes. The ABI is what makes separately compiled code — and code from different languages — interoperate.

Why This Matters

The C *language* is standardized; the *binary* interface is not. The ABI determines how arguments are passed, how structs are returned, how names are mangled, and how the stack is aligned. Getting the ABI wrong corrupts data or crashes, even when the source looks correct.

Prerequisites

Core Concept

ABI

The ABI is the complete contract between compiled units. It includes:

The C standard leaves these to the implementation; the ABI fixes them for a specific platform.

Calling convention

A calling convention specifies:

Examples

x86-64 System V (Linux/macOS, simplified)

Windows x64

These are platform-specific facts, not ISO C. PLATFORM-SPECIFIC

How It Works

The compiler generates code that obeys the ABI: it moves arguments into the right registers/stack slots, saves callee-saved registers, and reads the return value from the right register. The linker and loader assume all objects follow the same ABI.

Variations

Calling conventions within one platform

x86 (32-bit) had several conventions (cdecl, stdcall, fastcall); x86-64 mostly unified to System V (Unix) and Microsoft x64 (Windows).

Struct returns

Small structs may be returned in registers; large structs may be returned via a hidden pointer argument (sret). This is ABI-defined.

Common Mistakes

Undefined Behavior

violate the ABI at the machine level).

Portability

depends critically on matching the ABI.

Under the Hood

The ABI is realized in the generated assembly: argument moves, stack adjustments, register saves, and return-value placement. c.cpu.6 shows concrete examples.

Practical Usage

Exercises

1. Write a tiny function and compile to assembly; identify where each argument is placed. 2. Write a function that returns a large struct and observe the sret pattern. 3. Compare the assembly of a simple call on Linux vs. Windows (or two architectures) if available.

Deep Challenge

Explain the full sequence of a function call on x86-64 System V: argument placement, stack alignment, prologue, and return. Then explain how a variadic function's va_list interacts with the register save area.

References

Verification

verified.`

Progress

Concept checkboxes

Mastery levels

ConceptCurrent level (0–8)Target level
c.build.abi07
c.build.calling-convention06