1.\" Copyright (c) 1991 The Regents of the University of California. 2.\" All rights reserved. 3.\" 4.\" This man page is derived from documentation contributed to Berkeley by 5.\" Donn Seeley at UUNET Technologies, Inc. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. All advertising materials mentioning features or use of this software 16.\" must display the following acknowledgement: 17.\" This product includes software developed by the University of 18.\" California, Berkeley and its contributors. 19.\" 4. Neither the name of the University nor the names of its contributors 20.\" may be used to endorse or promote products derived from this software 21.\" without specific prior written permission. 22.\" 23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" SUCH DAMAGE. 34.\" 35.\" from: @(#)a.out.5 6.3 (Berkeley) 4/29/91 36.\" $Id: a.out.5,v 1.3 1993/11/09 00:25:03 pk Exp $ 37.\" 38.Dd April 29, 1991 39.Dt A.OUT 5 40.Os 41.Sh NAME 42.Nm a.out 43.Nd format of executable binary files 44.Sh SYNOPSIS 45.Fd #include <a.out.h> 46.Sh DESCRIPTION 47The include file 48.Aq Pa a.out.h 49declares three structures and several macros. 50The structures describe the format of 51executable machine code files 52.Pq Sq binaries 53on the system. 54.Pp 55A binary file consists of up to 7 sections. 56In order, these sections are: 57.Bl -tag -width "text relocations" 58.It exec header 59Contains parameters used by the kernel 60to load a binary file into memory and execute it, 61and by the link editor 62.Xr ld 1 63to combine a binary file with other binary files. 64This section is the only mandatory one. 65.It text segment 66Contains machine code and related data 67that are loaded into memory when a program executes. 68May be loaded read-only. 69.It data segment 70Contains initialized data; always loaded into writable memory. 71.It text relocations 72Contains records used by the link editor 73to update pointers in the text segment when combining binary files. 74.It data relocations 75Like the text relocation section, but for data segment pointers. 76.It symbol table 77Contains records used by the link editor 78to cross reference the addresses of named variables and functions 79.Pq Sq symbols 80between binary files. 81.It string table 82Contains the character strings corresponding to the symbol names. 83.El 84.Pp 85Every binary file begins with an 86.Fa exec 87structure: 88.Bd -literal -offset indent 89struct exec { 90 unsigned short a_mid; 91 unsigned short a_magic; 92 unsigned long a_text; 93 unsigned long a_data; 94 unsigned long a_bss; 95 unsigned long a_syms; 96 unsigned long a_entry; 97 unsigned long a_trsize; 98 unsigned long a_drsize; 99}; 100.Ed 101.Pp 102The fields have the following functions: 103.Bl -tag -width a_trsize 104.It Fa a_mid 105Contains a bit pattern that 106identifies binaries that were built for 107certain sub-classes of an architecture 108.Pq Sq machine IDs 109or variants of the operating system on a given architecture. 110The kernel may not support all machine IDs 111on a given architecture. 112The 113.Fa a_mid 114field is not present on some architectures; 115in this case, the 116.Fa a_magic 117field has type 118.Em unsigned long . 119.It Fa a_magic 120Contains a bit pattern 121.Pq Sq magic number 122that uniquely identifies binary files 123and distinguishes different loading conventions. 124The field must contain one of the following values: 125.Bl -tag -width ZMAGIC 126.It Dv OMAGIC 127The text and data segments immediately follow the header 128and are contiguous. 129The kernel loads both text and data segments into writable memory. 130.It Dv NMAGIC 131As with 132.Dv OMAGIC , 133text and data segments immediately follow the header and are contiguous. 134However, the kernel loads the text into read-only memory 135and loads the data into writable memory at the next 136page boundary after the text. 137.It Dv ZMAGIC 138The kernel loads individual pages on demand from the binary. 139The header, text segment and data segment are all 140padded by the link editor to a multiple of the page size. 141Pages that the kernel loads from the text segment are read-only, 142while pages from the data segment are writable. 143.El 144.It Fa a_text 145Contains the size of the text segment in bytes. 146.It Fa a_data 147Contains the size of the data segment in bytes. 148.It Fa a_bss 149Contains the number of bytes in the 150.Sq bss segment 151and is used by the kernel to set the initial break 152.Pq Xr brk 2 153after the data segment. 154The kernel loads the program so that this amount of writable memory 155appears to follow the data segment and initially reads as zeroes. 156.It Fa a_syms 157Contains the size in bytes of the symbol table section. 158.It Fa a_entry 159Contains the address in memory of the entry point 160of the program after the kernel has loaded it; 161the kernel starts the execution of the program 162from the machine instruction at this address. 163.It Fa a_trsize 164Contains the size in bytes of the text relocation table. 165.It Fa a_drsize 166Contains the size in bytes of the data relocation table. 167.El 168.Pp 169The 170.Pa a.out.h 171include file defines several macros which use an 172.Fa exec 173structure to test consistency or to locate section offsets in the binary file. 174.Bl -tag -width N_BADMAG(exec) 175.It Fn N_BADMAG exec 176Nonzero if the 177.Fa a_magic 178field does not contain a recognized value. 179.It Fn N_TXTOFF exec 180The byte offset in the binary file of the beginning of the text segment. 181.It Fn N_SYMOFF exec 182The byte offset of the beginning of the symbol table. 183.It Fn N_STROFF exec 184The byte offset of the beginning of the string table. 185.El 186.Pp 187Relocation records have a standard format which 188is described by the 189.Fa relocation_info 190structure: 191.Bd -literal -offset indent 192struct relocation_info { 193 int r_address; 194 unsigned int r_symbolnum : 24, 195 r_pcrel : 1, 196 r_length : 2, 197 r_extern : 1, 198 r_baserel : 1, 199 r_jmptable : 1, 200 r_relative : 1, 201 r_copy : 1; 202}; 203.Ed 204.Pp 205The 206.Fa relocation_info 207fields are used as follows: 208.Bl -tag -width r_symbolnum 209.It Fa r_address 210Contains the byte offset of a pointer that needs to be link-edited. 211Text relocation offsets are reckoned from the start of the text segment, 212and data relocation offsets from the start of the data segment. 213The link editor adds the value that is already stored at this offset 214into the new value that it computes using this relocation record. 215.It Fa r_symbolnum 216Contains the ordinal number of a symbol structure 217in the symbol table (it is 218.Em not 219a byte offset). 220After the link editor resolves the absolute address for this symbol, 221it adds that address to the pointer that is undergoing relocation. 222(If the 223.Fa r_extern 224bit is clear, the situation is different; see below.) 225.It Fa r_pcrel 226If this is set, 227the link editor assumes that it is updating a pointer 228that is part of a machine code instruction using pc-relative addressing. 229The address of the relocated pointer is implicitly added 230to its value when the running program uses it. 231.It Fa r_length 232Contains the log base 2 of the length of the pointer in bytes; 2330 for 1-byte displacements, 1 for 2-byte displacements, 2342 for 4-byte displacements. 235.It Fa r_extern 236Set if this relocation requires an external reference; 237the link editor must use a symbol address to update the pointer. 238When the 239.Fa r_extern 240bit is clear, the relocation is 241.Sq local ; 242the link editor updates the pointer to reflect 243changes in the load addresses of the various segments, 244rather than changes in the value of a symbol (except when 245.Fa r_baserel 246is also set (see below). 247In this case, the content of the 248.Fa r_symbolnum 249field is an 250.Fa n_type 251value (see below); 252this type field tells the link editor 253what segment the relocated pointer points into. 254.It Fa r_baserel 255If set, the symbol, as identified by the 256.Fa r_symbolnum 257field, is to be relocated to an offset into the Global Offset Table. 258At run-time, the entry in the Global Offset Table at this offset is set to 259be the address of the symbol. 260.It Fa r_jmptable 261If set, the symbol, as identified by the 262.Fa r_symbolnum 263field, is to be relocated to an offset into the Procedure Linkage Table. 264.It Fa r_relative 265If set, this relocation is relative to the (run-time) load address of the 266image this object file is going to be a part of. This type of relocation 267only occurs in shared objects. 268.It Fa r_copy 269If set, this relocation record identifies a symbol whose contents should 270be copied to the location given in 271.Fa r_address. 272The copying is done by the run-time link-editor from a suitable data 273item in a shared object. 274.El 275.Pp 276Symbols map names to addresses (or more generally, strings to values). 277Since the link-editor adjusts addresses, 278a symbol's name must be used to stand for its address 279until an absolute value has been assigned. 280Symbols consist of a fixed-length record in the symbol table 281and a variable-length name in the string table. 282The symbol table is an array of 283.Fa nlist 284structures: 285.Bd -literal -offset indent 286struct nlist { 287 union { 288 char *n_name; 289 long n_strx; 290 } n_un; 291 unsigned char n_type; 292 char n_other; 293 short n_desc; 294 unsigned long n_value; 295}; 296.Ed 297.Pp 298The fields are used as follows: 299.Bl -tag -width n_un.n_strx 300.It Fa n_un.n_strx 301Contains a byte offset into the string table 302for the name of this symbol. 303When a program accesses a symbol table with the 304.Xr nlist 3 305function, 306this field is replaced with the 307.Fa n_un.n_name 308field, which is a pointer to the string in memory. 309.It Fa n_type 310Used by the link editor to determine 311how to update the symbol's value. 312The 313.Fa n_type 314field is broken down into three sub-fields using bitmasks. 315The link editor treats symbols with the 316.Dv N_EXT 317type bit set as 318.Sq external 319symbols and permits references to them from other binary files. 320The 321.Dv N_TYPE 322mask selects bits of interest to the link editor: 323.Bl -tag -width N_TEXT 324.It Dv N_UNDF 325An undefined symbol. 326The link editor must locate an external symbol with the same name 327in another binary file to determine the absolute value of this symbol. 328As a special case, if the 329.Fa n_value 330field is nonzero and no binary file in the link-edit defines this symbol, 331the link-editor will resolve this symbol to an address 332in the bss segment, 333reserving an amount of bytes equal to 334.Fa n_value . 335If this symbol is undefined in more than one binary file 336and the binary files do not agree on the size, 337the link editor chooses the greatest size found across all binaries. 338.It Dv N_ABS 339An absolute symbol. 340The link editor does not update an absolute symbol. 341.It Dv N_TEXT 342A text symbol. 343This symbol's value is a text address and 344the link editor will update it when it merges binary files. 345.It Dv N_DATA 346A data symbol; similar to 347.Dv N_TEXT 348but for data addresses. 349The values for text and data symbols are not file offsets but 350addresses; to recover the file offsets, it is necessary 351to identify the loaded address of the beginning of the corresponding 352section and subtract it, then add the offset of the section. 353.It Dv N_BSS 354A bss symbol; like text or data symbols but 355has no corresponding offset in the binary file. 356.It Dv N_FN 357A filename symbol. 358The link editor inserts this symbol before 359the other symbols from a binary file when 360merging binary files. 361The name of the symbol is the filename given to the link editor, 362and its value is the first text address from that binary file. 363Filename symbols are not needed for link-editing or loading, 364but are useful for debuggers. 365.El 366.Pp 367The 368.Dv N_STAB 369mask selects bits of interest to symbolic debuggers 370such as 371.Xr gdb 1 ; 372the values are described in 373.Xr stab 5 . 374.It Fa n_other 375This field is currently unused. 376.It Fa n_desc 377Reserved for use by debuggers; passed untouched by the link editor. 378Different debuggers use this field for different purposes. 379.It Fa n_value 380Contains the value of the symbol. 381For text, data and bss symbols, this is an address; 382for other symbols (such as debugger symbols), 383the value may be arbitrary. 384.El 385.Pp 386The string table consists of an 387.Em unsigned long 388length followed by null-terminated symbol strings. 389The length represents the size of the entire table in bytes, 390so its minimum value (or the offset of the first string) 391is always 4 on 32-bit machines. 392.Sh SEE ALSO 393.Xr ld 1 , 394.Xr execve 2 , 395.Xr nlist 3 , 396.Xr core 5 , 397.Xr dbx 5 , 398.Xr stab 5 , 399.Xr link 5 400.Sh HISTORY 401The 402.Pa a.out.h 403include file appeared in 404.At v7 . 405.Sh BUGS 406Since not all of the supported architectures use the 407.Fa a_mid 408field, 409it can be difficult to determine what 410architecture a binary will execute on 411without examining its actual machine code. 412Even with a machine identifier, 413the byte order of the 414.Fa exec 415header is machine-dependent. 416.Pp 417Nobody seems to agree on what 418.Em bss 419stands for. 420.Pp 421New binary file formats may be supported in the future, 422and they probably will not be compatible at any level 423with this ancient format. 424