Repository navigation
5.3 Binary Format and Program Loading
Relevant source files
This page details the binary layout, packaging, and program loading subsystems of CadenzaOS. It covers the 16-byte CDZP flat binary format used by userland executables (src/kernel/binfmt.zig1-125), the CDZB program bundle format (src/kernel/bundlefmt.zig1-72), the build-time packaging script (tools/pack.sh1-77), and the kernel installation and loading pipeline (src/kernel/fs/binary_loader.zig1-130).
Sources: src/kernel/binfmt.zig1-125src/kernel/bundlefmt.zig1-72tools/pack.sh1-77src/kernel/fs/binary_loader.zig1-130
Every userland executable in CadenzaOS begins with a fixed 16-byte header emitted by the application linker script, defined as BinHeadersrc/kernel/binfmt.zig36-51
The header uses a magic value of 0x50_5A_44_43, which corresponds to the ASCII byte sequence 'C', 'D', 'Z', 'P' in little-endian representation src/kernel/binfmt.zig16-22
The layout of the executable header is defined as follows:
| Offset | Field | Type | Description |
|---|---|---|---|
| 0x00 | magic |
u32 |
Format magic (0x50_5A_44_43 / "CDZP") src/kernel/binfmt.zig37
|
| 0x04 | version |
u16 |
Binary format version (currently 1) src/kernel/binfmt.zig25src/kernel/binfmt.zig40
|
| 0x06 | flags |
u16 |
Reserved flags (must be zero for version 1) src/kernel/binfmt.zig42-44 |
| 0x08 | entry_offset |
u32 |
Byte offset from the image base to the entry point src/kernel/binfmt.zig46-47 |
| 0x0C | image_size |
u32 |
Total file-backed size emitted by the linker src/kernel/binfmt.zig49-50 |
The kernel provides two tiers of validation for binary images:
-
readHeader(bytes: []const u8): Validates the magic value, format version, and claimed image size using only the initial header bytes src/kernel/binfmt.zig79-99 -
parse(image: []const u8): Performs full verification on a fully loaded image buffer, ensuringimage_sizedoes not exceed the buffer length and thatentry_offsetpoints past the header and within the executable payload src/kernel/binfmt.zig101-125
Sources: src/kernel/binfmt.zig1-125
To stage multiple userland programs onto the disk image before the filesystem is initialized, CadenzaOS packs all binaries under userland/ into a single container known as a bundle src/kernel/bundlefmt.zig1-15
The bundle format uses the magic value 0x425A4443 ("CDZB") src/kernel/bundlefmt.zig18 Its on-disk layout consists of:
-
Sector 0: A 16-byte
BundleHeaderfollowed by up to 12BundleEntryrecords (40 bytes each) src/kernel/bundlefmt.zig12-13 - Sector 1+: Program images, each padded to align with sector boundaries src/kernel/bundlefmt.zig14
-
BundleHeader: Containsmagic,version(1),count(number of bundled programs), andtotal_sectorssrc/kernel/bundlefmt.zig24-30 -
BundleEntry: Contains a 32-byte NUL-padded filename,offset_sectorsfrom the start of the bundle, andsize_bytessrc/kernel/bundlefmt.zig32-36

Figure 1: Data flow bridging natural bundle bytes to bundlefmt.zig validation and entry functions.
Sources: src/kernel/bundlefmt.zig1-72
The packaging tool tools/pack.sh constructs the binary bundle from compiled .bin userland applications tools/pack.sh1-77
- Validates that the number of input programs does not exceed
MAXENT(12) tools/pack.sh27-29 - Computes each program's size via
stat, calculates sector counts, and appends a 40-byteBundleEntryrecord into a temporary table tools/pack.sh45-55 - Prepends the 16-byte
BundleHeader(0x425A4443, version1, entry count, and total sectors) to sector 0 tools/pack.sh59-67 - Concatenates all program binary payloads into the output file, padding each program with zeroes to the next sector boundary tools/pack.sh70-75
Sources: tools/pack.sh1-77
At first boot, the kernel installs embedded applications from the raw staging area (APP_LBA_START) into the CodaFS filesystem src/kernel/fs/binary_loader.zig13-51
-
installEmbeddedApps(allocator, fs): Reads sector 0 fromAPP_LBA_STARTusingata.AtaDevice.readBlocks()src/kernel/fs/binary_loader.zig29-30 parses the bundle header viabundle.parseHeader()src/kernel/fs/binary_loader.zig32 and iterates through each entry to invokeinstallOne()src/kernel/fs/binary_loader.zig38-50 -
installOne(allocator, fs, name, offset_sectors, size):
- Checks if the file already exists in the root directory via
fs.findFile(). If it exists, installation is skipped to preserve user changes src/kernel/fs/binary_loader.zig63-68 - Reads the program's sectors from raw disk storage (
APP_LBA_START + offset_sectors) src/kernel/fs/binary_loader.zig70-76 - Validates the program header using
binfmt.parse()src/kernel/fs/binary_loader.zig79 - Allocates filesystem space for metadata and data blocks using
fs.space_manager.allocate()src/kernel/fs/binary_loader.zig84-91 - Populates a
FileMetastructure and writes both the metadata block and payload blocks to disk src/kernel/fs/binary_loader.zig94-112 - Inserts the directory entry into CodaFS and flushes space manager states src/kernel/fs/binary_loader.zig115-130
Figure 2: Component interaction bridging disk staging areas to binary_loader.zig installation procedures.
Sources: src/kernel/fs/binary_loader.zig1-130src/kernel/drivers/ata.zig1-155
When a userland program is executed (spawned) from the shell, the kernel loads the file contents from CodaFS into a memory buffer and subjects it to strict validation before execution src/kernel/binfmt.zig101-125
-
Header Verification:
binfmt.readHeader()ensures the magic bytes match0x50_5A_44_43("CDZP") and version equals1src/kernel/binfmt.zig79-98 -
Size Bounds:
binfmt.parse()verifies thatimage_sizedoes not exceed the allocated buffer size src/kernel/binfmt.zig111-116 -
Entry Point Bounds: Ensures
entry_offsetis strictly greater than or equal to@sizeOf(BinHeader)(16 bytes) and strictly less thanimage_size, preventing execution flow from targeting header metadata or unmapped memory src/kernel/binfmt.zig118-122
Sources: src/kernel/binfmt.zig1-125src/kernel/fs/binary_loader.zig1-130
- Overview
- Getting Started and Build System
- Kernel Configuration and Global State
- Project Governance and Contributing
- Boot and Early Kernel Initialization
- Bootloader Stages and Linking
- kmain Boot Sequence
- Splash Screen and Branding
- Interrupts, Scheduling, and Syscalls
- IDT, PIC, APIC and IRQ Stubs
- Scheduler and Task Lifecycle
- System Call Interface
- Demonstration Tasks
- CodaFS Filesystem and Storage
- On-Disk Format and Operations
- Space Manager and Block Devices
- Binary Format and Program Loading
- Shell, Terminal, and Display
- Shell Command Pipeline
- Terminal, Input Devices and Prompts
- Display: VGA, Framebuffer, Console and Serial