A.OUT(5)
π NAME π
a.out β format of executable binary files, bestie. This is the a.out(5) page from the File Formats Manual, and itβs literally the blueprint for how executable binaries are laid out. π
π SYNOPSIS π
#include <a.out.h>
β¨ DESCRIPTION β¨
Okay so the include file <a.out.h> declares three structures and several macros. Those structures describe the format of executable machine code files (βbinariesβ) on the system. No cap. π¦
A binary file consists of up to 7 sections. In order, these sections are:
- exec header β contains parameters used by the kernel to load a binary file into memory and execute it, and by the link editor
ld(1)to combine a binary file with other binary files. This section is the only mandatory one. - text segment β contains machine code and related data that are loaded into memory when a program executes. May be loaded read-only.
- data segment β contains initialized data; always loaded into writable memory.
- text relocations β contains records used by the link editor to update pointers in the text segment when combining binary files.
- data relocations β like the text relocation section, but for data segment pointers.
- symbol table β contains records used by the link editor to cross reference the addresses of named variables and functions (βsymbolsβ) between binary files.
- string table β contains the character strings corresponding to the symbol names.
Every binary file begins with an exec structure:
struct exec {
unsigned long a_midmag;
unsigned long a_text;
unsigned long a_data;
unsigned long a_bss;
unsigned long a_syms;
unsigned long a_entry;
unsigned long a_trsize;
unsigned long a_drsize;
};
The fields have the following functions:
a_midmagβ this field is stored in host byte-order. It has a number of sub-components accessed by the macrosN_GETFLAG(),N_GETMID(), andN_GETMAGIC(), and set by the macroN_SETMAGIC().- The macro
N_GETFLAG()returns a few flags: - If both
EX_DYNAMICandEX_PICare set, the object file is a position independent executable image (e.g. a shared library), which is to be loaded into the process address space by the run-time link editor. - The macro
N_GETMID()returns the machine-id. This indicates which machine(s) the binary is intended to run on. N_GETMAGIC()specifies the magic number, which uniquely identifies binary files and distinguishes different loading conventions. The field must contain one of the following values:OMAGICβ the text and data segments immediately follow the header and are contiguous. The kernel loads both text and data segments into writable memory.NMAGICβ as withOMAGIC, text and data segments immediately follow the header and are contiguous. However, the kernel loads the text into read-only memory and loads the data into writable memory at the next page boundary after the text.ZMAGICβ the kernel loads individual pages on demand from the binary. The header, text segment and data segment are all padded by the link editor to a multiple of the page size. Pages that the kernel loads from the text segment are read-only, while pages from the data segment are writable.
- The macro
a_textβ contains the size of the text segment in bytes.a_dataβ contains the size of the data segment in bytes.a_bssβ contains the number of bytes in the βbss segmentβ and is used by the kernel to set the initial break (brk(2)) after the data segment. The kernel loads the program so that this amount of writable memory appears to follow the data segment and initially reads as zeroes. (bss = block started by symbol)a_symsβ contains the size in bytes of the symbol table section.a_entryβ contains the address in memory of the entry point of the program after the kernel has loaded it; the kernel starts the execution of the program from the machine instruction at this address.a_trsizeβ contains the size in bytes of the text relocation table.a_drsizeβ contains the size in bytes of the data relocation table.
The <a.out.h> include file defines several macros which use an exec structure to test consistency or to locate section offsets in the binary file.
N_BADMAG(exec)β nonzero if thea_magicfield does not contain a recognized value.N_TXTOFF(exec)β the byte offset in the binary file of the beginning of the text segment.N_SYMOFF(exec)β the byte offset of the beginning of the symbol table.N_STROFF(exec)β the byte offset of the beginning of the string table.
Relocation records have a standard format which is described by the relocation_info structure:
struct relocation_info {
int r_address;
unsigned int r_symbolnum : 24,
r_pcrel : 1,
r_length : 2,
r_extern : 1,
r_baserel : 1,
r_jmptable : 1,
r_relative : 1,
r_copy : 1;
};
The relocation_info fields are used as follows:
r_addressβ contains the byte offset of a pointer that needs to be link-edited. Text relocation offsets are reckoned from the start of the text segment, and data relocation offsets from the start of the data segment. The link editor adds the value that is already stored at this offset into the new value that it computes using this relocation record.r_symbolnumβ contains the ordinal number of a symbol structure in the symbol table (it is not a byte offset). After the link editor resolves the absolute address for this symbol, it adds that address to the pointer that is undergoing relocation. (If ther_externbit is clear, the situation is different; see below.)r_pcrelβ if this is set, the link editor assumes that it is updating a pointer that is part of a machine code instruction using pc-relative addressing. The address of the relocated pointer is implicitly added to its value when the running program uses it.r_lengthβ contains the log base 2 of the length of the pointer in bytes;0for 1-byte displacements,1for 2-byte displacements,2for 4-byte displacements.r_externβ set if this relocation requires an external reference; the link editor must use a symbol address to update the pointer. When ther_externbit is clear, the relocation is βlocalβ; the link editor updates the pointer to reflect changes in the load addresses of the various segments, rather than changes in the value of a symbol (except whenr_baserelis also set (see below)). In this case, the content of ther_symbolnumfield is ann_typevalue (see below); this type field tells the link editor what segment the relocated pointer points into.r_baserelβ if set, the symbol, as identified by ther_symbolnumfield, is to be relocated to an offset into the Global Offset Table. At run-time, the entry in the Global Offset Table at this offset is set to be the address of the symbol.r_jmptableβ if set, the symbol, as identified by ther_symbolnumfield, is to be relocated to an offset into the Procedure Linkage Table.r_relativeβ if set, this relocation is relative to the (run-time) load address of the image this object file is going to be a part of. This type of relocation only occurs in shared objects.r_copyβ if set, this relocation record identifies a symbol whose contents should be copied to the location given inr_address. The copying is done by the run-time link-editor from a suitable data item in a shared object.
Symbols map names to addresses (or more generally, strings to values). Since the link-editor adjusts addresses, a symbolβs name must be used to stand for its address until an absolute value has been assigned. Symbols consist of a fixed-length record in the symbol table and a variable-length name in the string table. The symbol table is an array of nlist structures:
struct nlist {
union {
const char *n_name;
long n_strx;
} n_un;
unsigned char n_type;
char n_other;
short n_desc;
unsigned long n_value;
};
The fields are used as follows:
n_un.n_strxβ contains a byte offset into the string table for the name of this symbol. When a program accesses a symbol table with thenlist(3)function, this field is replaced with then_un.n_namefield, which is a pointer to the string in memory.n_typeβ used by the link editor to determine how to update the symbolβs value. Then_typefield is broken down into three sub-fields using bitmasks. The link editor treats symbols with theN_EXTtype bit set as βexternalβ symbols and permits references to them from other binary files. TheN_TYPEmask selects bits of interest to the link editor:N_UNDFβ the undefined symbol π. The link editor has to go hunt down an external symbol with the same name in some other binary file to figure out this symbol's absolute value. Special case alert π¨: if then_valuefield is nonzero and no binary file in the link-edit defines this symbol, the link-editor will resolve this symbol to an address in the bss segment, reserving an amount of bytes equal ton_value. And if this symbol is undefined in more than one binary file and those binary files do not agree on the size? The link editor picks the greatest size found across all binaries. queen behaviour honestly π.N_ABSβ an absolute symbol β¨. The link editor does not update an absolute symbol. it's giving "i'm perfect, don't touch me" π .N_TEXTβ a text symbol πΈ. Its value is a text address, and the link editor will update it when it merges binary files.N_DATAβ a data symbol π; basicallyN_TEXT's sibling but for data addresses. Ok so important vibe check: the values for text and data symbols are not file offsets, they're addresses π«Ά. To recover the file offsets you have to identify the loaded address of the beginning of the corresponding section, subtract it, then add the offset of the section. math era π.N_BSSβ a bss symbol π; like text or data symbols but it has no corresponding offset in the binary file. ghosting the file entirely, iconic.N_FNβ a filename symbol π. The link editor inserts this symbol before the other symbols from a binary file when merging binary files. The name of the symbol is the filename given to the link editor, and its value is the first text address from that binary file. Filename symbols are not needed for link-editing or loading, but they're super useful for debuggers. lowkey the debugger's bestie.
The N_STAB mask selects bits of interest to symbolic debuggers such as gdb(1) (ports/devel/gdb); the values are described in stab(5). iykyk π§.
n_other β this field gives you info on the nature of the symbol, independent of where the symbol lives segment-wise as determined by the n_type field π₯. Currently, the lower 4 bits of the n_other field hold one of two values: AUX_FUNC and AUX_OBJECT (see <link.h> for their definitions). AUX_FUNC associates the symbol with a callable function, while AUX_OBJECT associates the symbol with data, irrespective of their locations in either the text or the data segment. This field is intended to be used by ld(1) for the construction of dynamic executables. main character field honestly π.
n_desc β reserved for use by debuggers π; passed untouched by the link editor. Different debuggers use this field for different purposes. mysterious. enigmatic. we love her π.
n_value β contains the value of the symbol β. For text, data and bss symbols, this is an address; for other symbols (such as debugger symbols), the value may be arbitrary. chaos permitted π
.
The string table consists of an unsigned long length followed by null-terminated symbol strings π¦. The length represents the size of the entire table in bytes, so its minimum value (or the offset of the first string) is always 4 on 32-bit machines β no cap, that's just the rules β¨.
β¨ SEE ALSO β¨
as(1), gdb(1) (ports/devel/gdb), ld(1), brk(2), execve(2), nlist(3), core(5), elf(5), link(5), stab(5) πΈ
go read them, they're all eating π₯
π¦ HISTORY π¦
The <a.out.h> include file appeared in Version 7 AT&T UNIX π. a literal ancestor. we stan a historical icon β.
π BUGS π
Since not all of the supported architectures use the a_midmag field, it can be difficult to determine what architecture a binary will execute on without examining its actual machine code π. Even with a machine identifier, the byte order of the exec header is machine-dependent. so yeah β sometimes you just gotta look at the raw bytes and squint π.
okay bestie that's the whole page, you ate that π«Άπ thanks for reading all the way through, genuinely obsessed with you. this one's from the Debian crew, dated June 10, 2010, and the page is A.OUT(5) β go forth and debug β¨ππ
π¦ where this page comes from
- freebsd 15.1: base 15.1
- nixpkgs unstable: source
- Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms