1*5e015e5eSjoerg.\" $NetBSD: a.out.5,v 1.20 2010/03/22 18:58:32 joerg Exp $ 2b5930afcSjtc.\" 3b5930afcSjtc.\" Copyright (c) 1991, 1993 4b5930afcSjtc.\" The Regents of the University of California. All rights reserved. 561f28255Scgd.\" 661f28255Scgd.\" This man page is derived from documentation contributed to Berkeley by 761f28255Scgd.\" Donn Seeley at UUNET Technologies, Inc. 861f28255Scgd.\" 961f28255Scgd.\" Redistribution and use in source and binary forms, with or without 1061f28255Scgd.\" modification, are permitted provided that the following conditions 1161f28255Scgd.\" are met: 1261f28255Scgd.\" 1. Redistributions of source code must retain the above copyright 1361f28255Scgd.\" notice, this list of conditions and the following disclaimer. 1461f28255Scgd.\" 2. Redistributions in binary form must reproduce the above copyright 1561f28255Scgd.\" notice, this list of conditions and the following disclaimer in the 1661f28255Scgd.\" documentation and/or other materials provided with the distribution. 17075022b3Sagc.\" 3. Neither the name of the University nor the names of its contributors 1861f28255Scgd.\" may be used to endorse or promote products derived from this software 1961f28255Scgd.\" without specific prior written permission. 2061f28255Scgd.\" 2161f28255Scgd.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2261f28255Scgd.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2361f28255Scgd.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2461f28255Scgd.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2561f28255Scgd.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2661f28255Scgd.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2761f28255Scgd.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2861f28255Scgd.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2961f28255Scgd.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3061f28255Scgd.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3161f28255Scgd.\" SUCH DAMAGE. 3261f28255Scgd.\" 33b5930afcSjtc.\" @(#)a.out.5 8.1 (Berkeley) 6/5/93 3461f28255Scgd.\" 35b5930afcSjtc.Dd June 5, 1993 3661f28255Scgd.Dt A.OUT 5 3761f28255Scgd.Os 3861f28255Scgd.Sh NAME 3961f28255Scgd.Nm a.out 4061f28255Scgd.Nd format of executable binary files 4161f28255Scgd.Sh SYNOPSIS 42472351e1Swiz.In sys/types.h 43472351e1Swiz.In a.out.h 4461f28255Scgd.Sh DESCRIPTION 4561f28255ScgdThe include file 46*5e015e5eSjoerg.In a.out.h 4761f28255Scgddeclares three structures and several macros. 4861f28255ScgdThe structures describe the format of 4961f28255Scgdexecutable machine code files 5061f28255Scgd.Pq Sq binaries 5161f28255Scgdon the system. 5261f28255Scgd.Pp 5361f28255ScgdA binary file consists of up to 7 sections. 5461f28255ScgdIn order, these sections are: 5561f28255Scgd.Bl -tag -width "text relocations" 5661f28255Scgd.It exec header 5761f28255ScgdContains parameters used by the kernel 5861f28255Scgdto load a binary file into memory and execute it, 5961f28255Scgdand by the link editor 6061f28255Scgd.Xr ld 1 6161f28255Scgdto combine a binary file with other binary files. 6261f28255ScgdThis section is the only mandatory one. 6361f28255Scgd.It text segment 6461f28255ScgdContains machine code and related data 6561f28255Scgdthat are loaded into memory when a program executes. 6661f28255ScgdMay be loaded read-only. 6761f28255Scgd.It data segment 6861f28255ScgdContains initialized data; always loaded into writable memory. 6961f28255Scgd.It text relocations 7061f28255ScgdContains records used by the link editor 7161f28255Scgdto update pointers in the text segment when combining binary files. 7261f28255Scgd.It data relocations 7361f28255ScgdLike the text relocation section, but for data segment pointers. 7461f28255Scgd.It symbol table 7561f28255ScgdContains records used by the link editor 7661f28255Scgdto cross reference the addresses of named variables and functions 7761f28255Scgd.Pq Sq symbols 7861f28255Scgdbetween binary files. 7961f28255Scgd.It string table 8061f28255ScgdContains the character strings corresponding to the symbol names. 8161f28255Scgd.El 8261f28255Scgd.Pp 8361f28255ScgdEvery binary file begins with an 8461f28255Scgd.Fa exec 8561f28255Scgdstructure: 8661f28255Scgd.Bd -literal -offset indent 8761f28255Scgdstruct exec { 8879bf0f83Sderaadt unsigned long a_midmag; 8961f28255Scgd unsigned long a_text; 9061f28255Scgd unsigned long a_data; 9161f28255Scgd unsigned long a_bss; 9261f28255Scgd unsigned long a_syms; 9361f28255Scgd unsigned long a_entry; 9461f28255Scgd unsigned long a_trsize; 9561f28255Scgd unsigned long a_drsize; 9661f28255Scgd}; 9761f28255Scgd.Ed 9861f28255Scgd.Pp 9961f28255ScgdThe fields have the following functions: 10061f28255Scgd.Bl -tag -width a_trsize 10179bf0f83Sderaadt.It Fa a_midmag 10279bf0f83SderaadtThis field is stored in network byte-order so that binaries for 103aae59958Srumblemachines with alternative byte orders can be distinguished. 10479bf0f83SderaadtIt has a number of sub-components accessed by the macros 10579bf0f83Sderaadt.Dv N_GETFLAG() , 10679bf0f83Sderaadt.Dv N_GETMID() , and 10779bf0f83Sderaadt.Dv N_GETMAGIC() , 10879bf0f83Sderaadtand set by the macro 10979bf0f83Sderaadt.Dv N_SETMAGIC() . 11079bf0f83Sderaadt.Pp 11179bf0f83SderaadtThe macro 11279bf0f83Sderaadt.Dv N_GETFLAG() 11379bf0f83Sderaadtreturns a few flags: 11479bf0f83Sderaadt.Bl -tag -width EX_DYNAMIC 11579bf0f83Sderaadt.It Dv EX_DYNAMIC 1165a944a14Spkindicates that the executable requires the services of the run-time link editor. 1175a944a14Spk.It Dv EX_PIC 1185a944a14Spkindicates that the object contains position independent code. This flag is 1195a944a14Spkset by 120a47fc068Smycroft.Xr as 1 1215a944a14Spkwhen given the 1225a944a14Spk.Sq -k 1235a944a14Spkflag and is preserved by 124a47fc068Smycroft.Xr ld 1 1255a944a14Spkif necessary. 12679bf0f83Sderaadt.El 12779bf0f83Sderaadt.Pp 128ed7ff6e2SkristerwIf both EX_DYNAMIC and EX_PIC are set, the object file is a position independent 1296abe6d18Swizexecutable image (e.g. a shared library), which is to be loaded into the 1305a944a14Spkprocess address space by the run-time link editor. 1315a944a14Spk.Pp 13279bf0f83SderaadtThe macro 13379bf0f83Sderaadt.Dv N_GETMID() 13479bf0f83Sderaadtreturns the machine-id. 13579bf0f83SderaadtThis indicates which machine(s) the binary is intended to run on. 13679bf0f83Sderaadt.Pp 13779bf0f83Sderaadt.Dv N_GETMAGIC() 13879bf0f83Sderaadtspecifies the magic number, which uniquely identifies binary files 13961f28255Scgdand distinguishes different loading conventions. 14061f28255ScgdThe field must contain one of the following values: 14161f28255Scgd.Bl -tag -width ZMAGIC 14261f28255Scgd.It Dv OMAGIC 14361f28255ScgdThe text and data segments immediately follow the header 14461f28255Scgdand are contiguous. 14561f28255ScgdThe kernel loads both text and data segments into writable memory. 14661f28255Scgd.It Dv NMAGIC 14761f28255ScgdAs with 14861f28255Scgd.Dv OMAGIC , 14961f28255Scgdtext and data segments immediately follow the header and are contiguous. 15061f28255ScgdHowever, the kernel loads the text into read-only memory 15161f28255Scgdand loads the data into writable memory at the next 15261f28255Scgdpage boundary after the text. 15361f28255Scgd.It Dv ZMAGIC 15461f28255ScgdThe kernel loads individual pages on demand from the binary. 15561f28255ScgdThe header, text segment and data segment are all 15661f28255Scgdpadded by the link editor to a multiple of the page size. 15761f28255ScgdPages that the kernel loads from the text segment are read-only, 15861f28255Scgdwhile pages from the data segment are writable. 15961f28255Scgd.El 16061f28255Scgd.It Fa a_text 16161f28255ScgdContains the size of the text segment in bytes. 16261f28255Scgd.It Fa a_data 16361f28255ScgdContains the size of the data segment in bytes. 16461f28255Scgd.It Fa a_bss 16561f28255ScgdContains the number of bytes in the 16661f28255Scgd.Sq bss segment 16761f28255Scgdand is used by the kernel to set the initial break 16861f28255Scgd.Pq Xr brk 2 16961f28255Scgdafter the data segment. 17061f28255ScgdThe kernel loads the program so that this amount of writable memory 17161f28255Scgdappears to follow the data segment and initially reads as zeroes. 17261f28255Scgd.It Fa a_syms 17361f28255ScgdContains the size in bytes of the symbol table section. 17461f28255Scgd.It Fa a_entry 17561f28255ScgdContains the address in memory of the entry point 17661f28255Scgdof the program after the kernel has loaded it; 17761f28255Scgdthe kernel starts the execution of the program 17861f28255Scgdfrom the machine instruction at this address. 17961f28255Scgd.It Fa a_trsize 18061f28255ScgdContains the size in bytes of the text relocation table. 18161f28255Scgd.It Fa a_drsize 18261f28255ScgdContains the size in bytes of the data relocation table. 18361f28255Scgd.El 18461f28255Scgd.Pp 18561f28255ScgdThe 18661f28255Scgd.Pa a.out.h 18761f28255Scgdinclude file defines several macros which use an 18861f28255Scgd.Fa exec 18961f28255Scgdstructure to test consistency or to locate section offsets in the binary file. 19061f28255Scgd.Bl -tag -width N_BADMAG(exec) 19161f28255Scgd.It Fn N_BADMAG exec 19261f28255ScgdNonzero if the 19361f28255Scgd.Fa a_magic 19461f28255Scgdfield does not contain a recognized value. 19561f28255Scgd.It Fn N_TXTOFF exec 19661f28255ScgdThe byte offset in the binary file of the beginning of the text segment. 19761f28255Scgd.It Fn N_SYMOFF exec 19861f28255ScgdThe byte offset of the beginning of the symbol table. 19961f28255Scgd.It Fn N_STROFF exec 20061f28255ScgdThe byte offset of the beginning of the string table. 20161f28255Scgd.El 20261f28255Scgd.Pp 20361f28255ScgdRelocation records have a standard format which 20461f28255Scgdis described by the 20561f28255Scgd.Fa relocation_info 20661f28255Scgdstructure: 20761f28255Scgd.Bd -literal -offset indent 20861f28255Scgdstruct relocation_info { 20961f28255Scgd int r_address; 21061f28255Scgd unsigned int r_symbolnum : 24, 21161f28255Scgd r_pcrel : 1, 21261f28255Scgd r_length : 2, 21361f28255Scgd r_extern : 1, 21473ad053aSpk r_baserel : 1, 21573ad053aSpk r_jmptable : 1, 21673ad053aSpk r_relative : 1, 21773ad053aSpk r_copy : 1; 21861f28255Scgd}; 21961f28255Scgd.Ed 22061f28255Scgd.Pp 22161f28255ScgdThe 22261f28255Scgd.Fa relocation_info 22361f28255Scgdfields are used as follows: 22461f28255Scgd.Bl -tag -width r_symbolnum 22561f28255Scgd.It Fa r_address 22661f28255ScgdContains the byte offset of a pointer that needs to be link-edited. 22761f28255ScgdText relocation offsets are reckoned from the start of the text segment, 22861f28255Scgdand data relocation offsets from the start of the data segment. 22961f28255ScgdThe link editor adds the value that is already stored at this offset 23061f28255Scgdinto the new value that it computes using this relocation record. 23161f28255Scgd.It Fa r_symbolnum 23261f28255ScgdContains the ordinal number of a symbol structure 23361f28255Scgdin the symbol table (it is 23461f28255Scgd.Em not 23561f28255Scgda byte offset). 23661f28255ScgdAfter the link editor resolves the absolute address for this symbol, 23761f28255Scgdit adds that address to the pointer that is undergoing relocation. 23861f28255Scgd(If the 23961f28255Scgd.Fa r_extern 24061f28255Scgdbit is clear, the situation is different; see below.) 24161f28255Scgd.It Fa r_pcrel 24261f28255ScgdIf this is set, 24361f28255Scgdthe link editor assumes that it is updating a pointer 24461f28255Scgdthat is part of a machine code instruction using pc-relative addressing. 24561f28255ScgdThe address of the relocated pointer is implicitly added 24661f28255Scgdto its value when the running program uses it. 24761f28255Scgd.It Fa r_length 24861f28255ScgdContains the log base 2 of the length of the pointer in bytes; 24961f28255Scgd0 for 1-byte displacements, 1 for 2-byte displacements, 25061f28255Scgd2 for 4-byte displacements. 25161f28255Scgd.It Fa r_extern 25261f28255ScgdSet if this relocation requires an external reference; 25361f28255Scgdthe link editor must use a symbol address to update the pointer. 25461f28255ScgdWhen the 25561f28255Scgd.Fa r_extern 25661f28255Scgdbit is clear, the relocation is 25761f28255Scgd.Sq local ; 25861f28255Scgdthe link editor updates the pointer to reflect 25961f28255Scgdchanges in the load addresses of the various segments, 26073ad053aSpkrather than changes in the value of a symbol (except when 26173ad053aSpk.Fa r_baserel 262ed7ff6e2Skristerwis also set, see below). 26361f28255ScgdIn this case, the content of the 26461f28255Scgd.Fa r_symbolnum 26561f28255Scgdfield is an 26661f28255Scgd.Fa n_type 26761f28255Scgdvalue (see below); 26861f28255Scgdthis type field tells the link editor 26961f28255Scgdwhat segment the relocated pointer points into. 27073ad053aSpk.It Fa r_baserel 27173ad053aSpkIf set, the symbol, as identified by the 27273ad053aSpk.Fa r_symbolnum 27373ad053aSpkfield, is to be relocated to an offset into the Global Offset Table. 27473ad053aSpkAt run-time, the entry in the Global Offset Table at this offset is set to 27573ad053aSpkbe the address of the symbol. 27673ad053aSpk.It Fa r_jmptable 27773ad053aSpkIf set, the symbol, as identified by the 27873ad053aSpk.Fa r_symbolnum 27973ad053aSpkfield, is to be relocated to an offset into the Procedure Linkage Table. 28073ad053aSpk.It Fa r_relative 28173ad053aSpkIf set, this relocation is relative to the (run-time) load address of the 28273ad053aSpkimage this object file is going to be a part of. This type of relocation 28373ad053aSpkonly occurs in shared objects. 28473ad053aSpk.It Fa r_copy 28573ad053aSpkIf set, this relocation record identifies a symbol whose contents should 28673ad053aSpkbe copied to the location given in 28773ad053aSpk.Fa r_address . 28873ad053aSpkThe copying is done by the run-time link-editor from a suitable data 28973ad053aSpkitem in a shared object. 29061f28255Scgd.El 29161f28255Scgd.Pp 29261f28255ScgdSymbols map names to addresses (or more generally, strings to values). 29361f28255ScgdSince the link-editor adjusts addresses, 29461f28255Scgda symbol's name must be used to stand for its address 29561f28255Scgduntil an absolute value has been assigned. 29661f28255ScgdSymbols consist of a fixed-length record in the symbol table 29761f28255Scgdand a variable-length name in the string table. 29861f28255ScgdThe symbol table is an array of 29961f28255Scgd.Fa nlist 30061f28255Scgdstructures: 30161f28255Scgd.Bd -literal -offset indent 30261f28255Scgdstruct nlist { 30361f28255Scgd union { 30461f28255Scgd char *n_name; 30561f28255Scgd long n_strx; 30661f28255Scgd } n_un; 30761f28255Scgd unsigned char n_type; 30861f28255Scgd char n_other; 30961f28255Scgd short n_desc; 31061f28255Scgd unsigned long n_value; 31161f28255Scgd}; 31261f28255Scgd.Ed 31361f28255Scgd.Pp 31461f28255ScgdThe fields are used as follows: 31561f28255Scgd.Bl -tag -width n_un.n_strx 31661f28255Scgd.It Fa n_un.n_strx 31761f28255ScgdContains a byte offset into the string table 31861f28255Scgdfor the name of this symbol. 31961f28255ScgdWhen a program accesses a symbol table with the 32061f28255Scgd.Xr nlist 3 32161f28255Scgdfunction, 32261f28255Scgdthis field is replaced with the 32361f28255Scgd.Fa n_un.n_name 32461f28255Scgdfield, which is a pointer to the string in memory. 32561f28255Scgd.It Fa n_type 32661f28255ScgdUsed by the link editor to determine 32761f28255Scgdhow to update the symbol's value. 32861f28255ScgdThe 32961f28255Scgd.Fa n_type 33061f28255Scgdfield is broken down into three sub-fields using bitmasks. 33161f28255ScgdThe link editor treats symbols with the 33261f28255Scgd.Dv N_EXT 33361f28255Scgdtype bit set as 33461f28255Scgd.Sq external 33561f28255Scgdsymbols and permits references to them from other binary files. 33661f28255ScgdThe 33761f28255Scgd.Dv N_TYPE 33861f28255Scgdmask selects bits of interest to the link editor: 33961f28255Scgd.Bl -tag -width N_TEXT 34061f28255Scgd.It Dv N_UNDF 34161f28255ScgdAn undefined symbol. 34261f28255ScgdThe link editor must locate an external symbol with the same name 34361f28255Scgdin another binary file to determine the absolute value of this symbol. 34461f28255ScgdAs a special case, if the 34561f28255Scgd.Fa n_value 34661f28255Scgdfield is nonzero and no binary file in the link-edit defines this symbol, 34761f28255Scgdthe link-editor will resolve this symbol to an address 34861f28255Scgdin the bss segment, 34961f28255Scgdreserving an amount of bytes equal to 35061f28255Scgd.Fa n_value . 35161f28255ScgdIf this symbol is undefined in more than one binary file 35261f28255Scgdand the binary files do not agree on the size, 35361f28255Scgdthe link editor chooses the greatest size found across all binaries. 35461f28255Scgd.It Dv N_ABS 35561f28255ScgdAn absolute symbol. 35661f28255ScgdThe link editor does not update an absolute symbol. 35761f28255Scgd.It Dv N_TEXT 35861f28255ScgdA text symbol. 35961f28255ScgdThis symbol's value is a text address and 36061f28255Scgdthe link editor will update it when it merges binary files. 36161f28255Scgd.It Dv N_DATA 36261f28255ScgdA data symbol; similar to 36361f28255Scgd.Dv N_TEXT 36461f28255Scgdbut for data addresses. 36561f28255ScgdThe values for text and data symbols are not file offsets but 36661f28255Scgdaddresses; to recover the file offsets, it is necessary 36761f28255Scgdto identify the loaded address of the beginning of the corresponding 36861f28255Scgdsection and subtract it, then add the offset of the section. 36961f28255Scgd.It Dv N_BSS 37061f28255ScgdA bss symbol; like text or data symbols but 37161f28255Scgdhas no corresponding offset in the binary file. 37261f28255Scgd.It Dv N_FN 37361f28255ScgdA filename symbol. 37461f28255ScgdThe link editor inserts this symbol before 37561f28255Scgdthe other symbols from a binary file when 37661f28255Scgdmerging binary files. 37761f28255ScgdThe name of the symbol is the filename given to the link editor, 37861f28255Scgdand its value is the first text address from that binary file. 37961f28255ScgdFilename symbols are not needed for link-editing or loading, 38061f28255Scgdbut are useful for debuggers. 38161f28255Scgd.El 38261f28255Scgd.Pp 38361f28255ScgdThe 38461f28255Scgd.Dv N_STAB 38561f28255Scgdmask selects bits of interest to symbolic debuggers 38661f28255Scgdsuch as 38761f28255Scgd.Xr gdb 1 ; 38861f28255Scgdthe values are described in 38961f28255Scgd.Xr stab 5 . 39061f28255Scgd.It Fa n_other 39103c92d6eSpkThis field provides information on the nature of the symbol independent of 39203c92d6eSpkthe symbol's location in terms of segments as determined by the 39303c92d6eSpk.Fa n_type 39403c92d6eSpkfield. Currently, the lower 4 bits of the 39503c92d6eSpk.Fa n_other 39603c92d6eSpkfield hold one of two values: 39703c92d6eSpk.Dv AUX_FUNC 39803c92d6eSpkand 39903c92d6eSpk.Dv AUX_OBJECT 40003c92d6eSpk.Po 40103c92d6eSpksee 402*5e015e5eSjoerg.In link.h 40303c92d6eSpkfor their definitions 40403c92d6eSpk.Pc . 40503c92d6eSpk.Dv AUX_FUNC 40603c92d6eSpkassociates the symbol with a callable function, while 40703c92d6eSpk.Dv AUX_OBJECT 40803c92d6eSpkassociates the symbol with data, irrespective of their locations in 40903c92d6eSpkeither the text or the data segment. 41003c92d6eSpkThis field is intended to be used by 41103c92d6eSpk.Xr ld 1 41203c92d6eSpkfor the construction of dynamic executables. 41361f28255Scgd.It Fa n_desc 41461f28255ScgdReserved for use by debuggers; passed untouched by the link editor. 41561f28255ScgdDifferent debuggers use this field for different purposes. 41661f28255Scgd.It Fa n_value 41761f28255ScgdContains the value of the symbol. 41861f28255ScgdFor text, data and bss symbols, this is an address; 41961f28255Scgdfor other symbols (such as debugger symbols), 42061f28255Scgdthe value may be arbitrary. 42161f28255Scgd.El 42261f28255Scgd.Pp 42361f28255ScgdThe string table consists of an 42461f28255Scgd.Em unsigned long 42561f28255Scgdlength followed by null-terminated symbol strings. 42661f28255ScgdThe length represents the size of the entire table in bytes, 42761f28255Scgdso its minimum value (or the offset of the first string) 42861f28255Scgdis always 4 on 32-bit machines. 42961f28255Scgd.Sh SEE ALSO 430a47fc068Smycroft.Xr as 1 , 431a47fc068Smycroft.Xr gdb 1 , 43261f28255Scgd.Xr ld 1 , 433a47fc068Smycroft.Xr brk 2 , 43461f28255Scgd.Xr execve 2 , 43561f28255Scgd.Xr nlist 3 , 43661f28255Scgd.Xr core 5 , 4372bb6da2dSpooka.Xr elf 5 , 438809a0176Smikel.Xr link 5 , 439809a0176Smikel.Xr stab 5 44061f28255Scgd.Sh HISTORY 44161f28255ScgdThe 44261f28255Scgd.Pa a.out.h 44361f28255Scgdinclude file appeared in 44461f28255Scgd.At v7 . 44561f28255Scgd.Sh BUGS 44661f28255ScgdNobody seems to agree on what 44761f28255Scgd.Em bss 44861f28255Scgdstands for. 44961f28255Scgd.Pp 45061f28255ScgdNew binary file formats may be supported in the future, 45161f28255Scgdand they probably will not be compatible at any level 45261f28255Scgdwith this ancient format. 453