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