liveBook · Manning

Application binary interface

As explained in Systems Programming with Zig

An application binary interface specifies how compiled components interact at the binary level, including calling conventions and data representation.

An application binary interface (ABI) is a set of rules that lets compiled components interact. It answers questions such as how to call a function, how data is laid out in memory, and how the stack works. Shared rules let code from different languages and compiler toolchains fit together. section 5.2.1

Linking wires a program to library code. Static linking incorporates the library into the executable; dynamic linking records a dependency that the operating system can load. Either way, the components need compatible expectations at their binary boundary. section 5.2.1

Passing a recipe across an ABI boundary

A Zig recipe interface returns a whole recipe rather than exposing a separate function for every field. It uses nul-terminated byte pointers for strings and a pointer plus length for the ingredient sequence. The ingredient array is built in a caller-provided buffer with std.heap.FixedBufferAllocator; insufficient space produces a would_overflow result. section 5.4.2

Listing 5.18 Extern recipes
const RetrievedIngredient = extern struct { #1
    quantity: [*:0]const u8,
    name: [*:0]const u8,
};

const RetrievedRecipe = extern struct { #1
    name: [*:0]const u8,
    author: [*:0]const u8,
    instructions: [*:0]const u8,
    ingredients: [*]const RetrievedIngredient, #2
    ingredient_count: usize, #2
};

Inspect the extern structs that lay out recipe fields and represent the ingredient sequence as a pointer and count. section 5.4.2

Listing 5.19 Retrieving recipes with Benito
export fn recipe_get(
    book: *const RecipeBook,
    recipe_id: RecipeId,
    buf_ptr: [*]u8, #1
    buf_len: usize, #1
    result: *RetrievedRecipe,
) Success {
    const buffer = buf_ptr[0..buf_len]; #2
    var fba: std.heap.FixedBufferAllocator = .init(buffer); #3
    const buf_ally = fba.allocator(); #3

    const recipe = book.getRecipe(recipe_id) catch |e| {#4
        return translateErr(e);
    };

    var ingredients_copy: std.ArrayList(RetrievedIngredient) = .empty; #5
    var ingredients = book.iterateIngredients(recipe_id);
    while (ingredients.next()) |ing| {
        ingredients_copy.append(buf_ally, .{ #6
            .quantity = ing.quantity.ptr,
            .name = ing.name.ptr,
        }) catch return .would_overflow; #7
    }

    result.* = .{ #8
        .name = recipe.name.ptr,
        .author = recipe.author.ptr,
        .instructions = recipe.instructions.ptr,
        .ingredients = ingredients_copy.items.ptr,
        .ingredient_count = ingredients_copy.items.len,
    };
    return .ok; #9
}

Follow recipe_get as it turns a caller-provided buffer into an allocator and copies ingredient entries into it. section 5.4.2

Calls, returns, and conventions

In a CHIP-8 interpreter, a call saves the current position on a stack and jumps to a function; a return restores that position. Registers can carry data between functions. Standardizing how that data is passed is a step toward an ABI. section 6.5.6

How Systems Programming with Zig covers it

How deep it goes
Defines ABI responsibilities and works through C-compatible data transfer, without specifying a complete calling convention.
Examples
Uses recipe retrieval with caller-provided buffers and CHIP-8 subroutine calls to illustrate binary interfaces.
What you can do after
Apply it via Zig to expose C-compatible recipe data using extern structs and a caller-provided buffer.
How it connects
Connects foreign-function interfaces, linking, data layout, call stacks, and register-based parameter passing.

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 Systems Programming with Zig. The section links point to the parts of the book it draws on.