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