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