# Understanding a.out and QMAGIC

`a.out` is an early Unix format for executable files, object files, and shared
libraries. Its name comes from “assembler output,” the default filename
produced by early Unix assemblers and linkers. The name later referred to the
binary format itself, even when the file had a different name.

BSD systems used several a.out variants before adopting ELF. They share a
small fixed header and a mostly predetermined memory layout. QMAGIC is the
compact demand-paged variant used by binaries such as the FreeBSD/i386 release
of Netscape Navigator 4.08.

## The a.out header

In FreeBSD, an a.out executable begins with this eight-field header:

```c
struct exec {
	uint32_t a_midmag;  /* flags, machine ID, and magic value */
	uint32_t a_text;    /* text size */
	uint32_t a_data;    /* initialized data size */
	uint32_t a_bss;     /* zero-initialized data size */
	uint32_t a_syms;    /* symbol-table size */
	uint32_t a_entry;   /* initial instruction address */
	uint32_t a_trsize;  /* text-relocation size */
	uint32_t a_drsize;  /* data-relocation size */
};
```

Each field is 32 bits, so the complete header is 32 bytes. The header tells the
kernel how much code and data to map and where execution begins.

The first word, `a_midmag`, packs three values together:

- a **magic value**, which identifies the a.out layout variant;
- a **machine ID**, such as `MID_I386` for an i386 BSD binary;
- flags such as `EX_PIC` or `EX_DYNAMIC`.

A magic value is a fixed signature that lets the kernel select the appropriate
executable loader. It is not encryption or a checksum.

## Text, data, and BSS

The main size fields describe three conventional process regions:

- **text** contains machine code and usually read-only constants;
- **data** contains initialized writable global and static variables;
- **BSS** reserves zero-filled memory for uninitialized variables. BSS takes
  space in memory but does not need an equivalent block of zero bytes in the
  executable file.

The kernel derives file offsets and virtual addresses from these sizes, the
magic value, and fixed alignment rules. This is one of a.out's defining traits:
much of the layout is implicit rather than described by a list of independent
segments.

## The main a.out variants

FreeBSD recognizes four traditional magic values:

| Variant | Magic | Main property |
|---|---:|---|
| OMAGIC | `0407` | Old “impure” layout; text is not separated as read-only |
| NMAGIC | `0410` | Read-only text separated from writable data |
| ZMAGIC | `0413` | Demand-paged layout with text at a page-aligned file offset |
| QMAGIC | `0314` | Compact demand-paged layout with text starting at file offset zero |

The values are traditionally written in octal. QMAGIC's `0314` is `0xcc` in
hexadecimal.

“Demand-paged” means the kernel maps executable pages and loads each page when
the process accesses it. Earlier layouts could require more copying or loading
at execution time. Demand paging also allows clean, read-only code pages to be
shared by multiple processes running the same program.

## How QMAGIC is laid out

QMAGIC removes the unused file padding found in other demand-paged layouts. In
FreeBSD's format rules:

- the text mapping begins at virtual address `0x1000`;
- the text comes from file offset `0`;
- the 32-byte a.out header is therefore part of the mapped text;
- initialized data follows the text at a page-aligned address and file offset;
- BSS follows the initialized data in memory.

`0x1000` is hexadecimal for 4096, the size of one 4 KiB page. Starting the
mapping there leaves the lowest virtual page unmapped and gives the binary a
page-aligned base.

Because the header occupies the first `0x20` bytes of that mapping, a QMAGIC
program commonly begins execution at `0x1020`:

```text
file offset 0x0000  -> virtual address 0x1000: 32-byte a.out header
file offset 0x0020  -> virtual address 0x1020: first machine instruction
```

The entry address is stored explicitly in `a_entry`. The kernel does not assume
that every QMAGIC binary starts at `0x1020`, but that value follows naturally
when code begins immediately after the 32-byte header.

The recovered Netscape 4.08 binary is a concrete example:

```text
text:   0x001000 - 0x625000
data:   0x625000 - 0x70e000
BSS:    begins at 0x70e000
entry:  0x1020
```

Its text mapping includes the header at `0x1000`; the CPU starts on the first
instruction at `0x1020`.

## Loading and dynamic linking

An a.out-aware kernel image activator validates the magic and machine ID,
calculates the fixed text and data locations, maps the file, creates BSS and
the process stack, and transfers control to `a_entry`.

Static a.out executables contain everything required to start running. Dynamic
a.out executables can set `EX_DYNAMIC` and include runtime link-edit data. On
historical FreeBSD, startup code and `ld.so` then locate shared libraries,
resolve symbols, and apply the format's relocation conventions.

These conventions are closely tied to the operating system, architecture, and
toolchain generation. Two files both described as “a.out” may still use
different byte ordering, machine IDs, paging rules, or dynamic-linker details.
The format name describes a family of related layouts rather than one fully
portable specification.

## How ELF differs

ELF—the Executable and Linkable Format—also stores code, data, symbols, and
relocations, but it describes executable memory through a table of program
headers. Each program header gives a segment's file offset, virtual address,
file and memory sizes, alignment, and read/write/execute permissions.

That is the central difference:

- a.out derives a small number of conventional regions from one fixed header
  and magic-specific formulas;
- ELF provides an explicit list of segments that the loader maps according to
  the file's metadata.

ELF also separates the loader's view from the linker's view. Program headers
describe what is needed at runtime, while section headers describe objects such
as code sections, symbol tables, relocation records, and debugging data for
linkers and analysis tools. A developer does not need to understand every ELF
section to run a program; the kernel primarily follows the program headers.

Dynamic ELF files can identify their runtime interpreter and carry standardized
dynamic-linking records for needed libraries, symbol tables, relocations, and
version information. ELF also has defined representations for different word
sizes, byte orders, and machine architectures.

## Why ELF became the standard

ELF replaced a.out across most Unix-like systems because it scales better as
hardware and software become more varied:

- **Explicit layouts:** loaders do not need a growing set of magic-specific
  formulas for every executable arrangement.
- **Extensibility:** new segment, section, note, relocation, and processor
  types can be added without redesigning the base header.
- **Portable tooling:** compilers, linkers, debuggers, disassemblers, and core
  dump tools can share a structured format across many architectures.
- **Dynamic linking:** shared libraries, position-independent code, symbol
  lookup, relocations, and versioning have standardized metadata.
- **Memory permissions:** each loadable segment declares its own read, write,
  and execute flags instead of inheriting behavior from an old layout variant.
- **32-bit and 64-bit support:** ELF defines related formats for both sizes and
  records byte order and target architecture directly.

ELF is not newer merely because its header is larger. It replaced implicit,
platform-specific conventions with tables that can describe more layouts while
remaining readable by a common ecosystem of tools.

## Why QMAGIC still matters

Old QMAGIC binaries encode assumptions that extend beyond parsing the 32-byte
header. They may depend on fixed low addresses, historical page permissions,
an older system-call entry mechanism, and a particular a.out dynamic linker.

Running one on a current system therefore requires compatibility at several
layers: the kernel must recognize and map the image correctly, create the
expected process address space, provide its historical ABI, and load matching
runtime libraries. QMAGIC explains the initial file and memory layout, but it
is only the first part of preserving the complete execution environment.
