liveBook · Manning

Application binary interface

As explained in Embedded Software with Rust

An application binary interface specifies binary-level conventions for calling functions, representing data, and interoperating between compiled software components.

An application binary interface (ABI) specifies the binary-level conventions that let compiled components work together. At a Rust–C boundary, both sides must agree on how to call functions and interpret shared memory; Rust’s memory-safety guarantees do not automatically extend across that boundary. section 8.1.2 section 8.0

An ABI includes several kinds of agreement:

  • Calling convention: where arguments and return values are placed, such as registers or the stack. section 8.1.2
  • Data layout: how fields and array elements are arranged in memory. section 8.1.2
  • Symbol naming: the names functions and variables have in compiled output so the linker can find them. section 8.1.2

The procedure call standard (PCS) is the part of the ABI that governs function calls. On Cortex-M, a useful simplified model puts ordinary arguments in R0–R3 and a simple return value in R0, or R0 and R1 if needed. A 64-bit argument uses an aligned register pair; arguments that do not fit in registers go on the stack. With the hard-float ABI variant, f32 and f64 arguments instead use floating-point registers. section 8.1.2

// C function prototype:
uint32_t c_function(uint32_t a, uint32_t b, uint32_t c, uint32_t d, uint32_t e);

// On call:
// R0 = a
// R1 = b
// R2 = c
// R3 = d
// [e] is pushed on stack

// Return value in R0

This call places four arguments in registers and the fifth on the stack, with the return value in R0. section 8.1.2

At a public interface between independently compiled components, the Arm PCS requires the stack to be aligned to 8 bytes. Incorrect alignment can cause faults when accessing 64-bit values. In a linker script, ALIGN(8) can align a data section such as .bss; the Rust compiler normally gives individual variables their natural alignment within the section. section 8.1.2 section 4.1.3

Choose a target whose architecture and ABI match the microcontroller. For example, select eabihf for an Arm microcontroller with hardware floating-point support and eabi otherwise. Check the device reference manual or datasheet for the architecture and features before choosing. section 2.1.5

How Embedded Software with Rust covers it

How deep it goes
Defines ABI responsibilities and explains a simplified Cortex-M calling convention, including floating-point and stack-alignment exceptions.
Examples
Uses a five-argument C function to illustrate register and stack placement, and a linker directive to illustrate data-section alignment.
What you can do after
Evaluate/understand ABI compatibility when choosing an embedded target or sharing calls and memory between Rust and C.
How it connects
Connects calling conventions, data layout, symbol naming, linking, stack alignment, and floating-point support.

Also explained in

Compare how 3 books explain application binary interface →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Embedded Software with Rust. The section links point to the parts of the book it draws on.