1*61607Sbostic.\" Copyright (c) 1991, 1993 2*61607Sbostic.\" The Regents of the University of California. All rights reserved. 320755Smckusick.\" 448831Scael.\" This man page is derived from documentation contributed to Berkeley by 548831Scael.\" Donn Seeley at UUNET Technologies, Inc. 620755Smckusick.\" 748831Scael.\" %sccs.include.redist.roff% 848831Scael.\" 9*61607Sbostic.\" @(#)a.out.5 8.1 (Berkeley) 06/05/93 1048831Scael.\" 1148831Scael.Dd 1248831Scael.Dt A.OUT 5 1348831Scael.Os 1448831Scael.Sh NAME 1548831Scael.Nm a.out 1648831Scael.Nd format of executable binary files 1748831Scael.Sh SYNOPSIS 1848831Scael.Fd #include <a.out.h> 1948831Scael.Sh DESCRIPTION 2048831ScaelThe include file 2148831Scael.Aq Pa a.out.h 2248831Scaeldeclares three structures and several macros. 2348831ScaelThe structures describe the format of 2448831Scaelexecutable machine code files 2548831Scael.Pq Sq binaries 2648831Scaelon the system. 2748831Scael.Pp 2848831ScaelA binary file consists of up to 7 sections. 2948831ScaelIn order, these sections are: 3048831Scael.Bl -tag -width "text relocations" 3148831Scael.It exec header 3248831ScaelContains parameters used by the kernel 3348831Scaelto load a binary file into memory and execute it, 3448831Scaeland by the link editor 3548831Scael.Xr ld 1 3648831Scaelto combine a binary file with other binary files. 3748831ScaelThis section is the only mandatory one. 3848831Scael.It text segment 3948831ScaelContains machine code and related data 4048831Scaelthat are loaded into memory when a program executes. 4148831ScaelMay be loaded read-only. 4248831Scael.It data segment 4348831ScaelContains initialized data; always loaded into writable memory. 4448831Scael.It text relocations 4548831ScaelContains records used by the link editor 4648831Scaelto update pointers in the text segment when combining binary files. 4748831Scael.It data relocations 4848831ScaelLike the text relocation section, but for data segment pointers. 4948831Scael.It symbol table 5048831ScaelContains records used by the link editor 5148831Scaelto cross reference the addresses of named variables and functions 5248831Scael.Pq Sq symbols 5348831Scaelbetween binary files. 5448831Scael.It string table 5548831ScaelContains the character strings corresponding to the symbol names. 5648831Scael.El 5748831Scael.Pp 5848831ScaelEvery binary file begins with an 5948831Scael.Fa exec 6048831Scaelstructure: 6148831Scael.Bd -literal -offset indent 6220755Smckusickstruct exec { 6348831Scael unsigned short a_mid; 6448831Scael unsigned short a_magic; 6548831Scael unsigned long a_text; 6648831Scael unsigned long a_data; 6748831Scael unsigned long a_bss; 6848831Scael unsigned long a_syms; 6948831Scael unsigned long a_entry; 7048831Scael unsigned long a_trsize; 7148831Scael unsigned long a_drsize; 7220755Smckusick}; 7348831Scael.Ed 7448831Scael.Pp 7548831ScaelThe fields have the following functions: 7648831Scael.Bl -tag -width a_trsize 7748831Scael.It Fa a_mid 7848831ScaelContains a bit pattern that 7948831Scaelidentifies binaries that were built for 8048831Scaelcertain sub-classes of an architecture 8148831Scael.Pq Sq machine IDs 8248831Scaelor variants of the operating system on a given architecture. 8348831ScaelThe kernel may not support all machine IDs 8448831Scaelon a given architecture. 8548831ScaelThe 8648831Scael.Fa a_mid 8748831Scaelfield is not present on some architectures; 8848831Scaelin this case, the 8948831Scael.Fa a_magic 9048831Scaelfield has type 9148831Scael.Em unsigned long . 9248831Scael.It Fa a_magic 9348831ScaelContains a bit pattern 9448831Scael.Pq Sq magic number 9548831Scaelthat uniquely identifies binary files 9648831Scaeland distinguishes different loading conventions. 9748831ScaelThe field must contain one of the following values: 9848831Scael.Bl -tag -width ZMAGIC 9948831Scael.It Dv OMAGIC 10048831ScaelThe text and data segments immediately follow the header 10148831Scaeland are contiguous. 10248831ScaelThe kernel loads both text and data segments into writable memory. 10348831Scael.It Dv NMAGIC 10448831ScaelAs with 10548831Scael.Dv OMAGIC , 10648831Scaeltext and data segments immediately follow the header and are contiguous. 10748831ScaelHowever, the kernel loads the text into read-only memory 10848831Scaeland loads the data into writable memory at the next 10948831Scaelpage boundary after the text. 11048831Scael.It Dv ZMAGIC 11148831ScaelThe kernel loads individual pages on demand from the binary. 11248831ScaelThe header, text segment and data segment are all 11348831Scaelpadded by the link editor to a multiple of the page size. 11448831ScaelPages that the kernel loads from the text segment are read-only, 11548831Scaelwhile pages from the data segment are writable. 11648831Scael.El 11748831Scael.It Fa a_text 11848831ScaelContains the size of the text segment in bytes. 11948831Scael.It Fa a_data 12048831ScaelContains the size of the data segment in bytes. 12148831Scael.It Fa a_bss 12248831ScaelContains the number of bytes in the 12348831Scael.Sq bss segment 12448831Scaeland is used by the kernel to set the initial break 12548831Scael.Pq Xr brk 2 12648831Scaelafter the data segment. 12748831ScaelThe kernel loads the program so that this amount of writable memory 12848831Scaelappears to follow the data segment and initially reads as zeroes. 12948831Scael.It Fa a_syms 13048831ScaelContains the size in bytes of the symbol table section. 13148831Scael.It Fa a_entry 13248831ScaelContains the address in memory of the entry point 13348831Scaelof the program after the kernel has loaded it; 13448831Scaelthe kernel starts the execution of the program 13548831Scaelfrom the machine instruction at this address. 13648831Scael.It Fa a_trsize 13748831ScaelContains the size in bytes of the text relocation table. 13848831Scael.It Fa a_drsize 13948831ScaelContains the size in bytes of the data relocation table. 14048831Scael.El 14148831Scael.Pp 14248831ScaelThe 14348831Scael.Pa a.out.h 14448831Scaelinclude file defines several macros which use an 14548831Scael.Fa exec 14648831Scaelstructure to test consistency or to locate section offsets in the binary file. 14748831Scael.Bl -tag -width N_BADMAG(exec) 14848831Scael.It Fn N_BADMAG exec 14948831ScaelNonzero if the 15048831Scael.Fa a_magic 15148831Scaelfield does not contain a recognized value. 15248831Scael.It Fn N_TXTOFF exec 15348831ScaelThe byte offset in the binary file of the beginning of the text segment. 15448831Scael.It Fn N_SYMOFF exec 15548831ScaelThe byte offset of the beginning of the symbol table. 15648831Scael.It Fn N_STROFF exec 15748831ScaelThe byte offset of the beginning of the string table. 15848831Scael.El 15948831Scael.Pp 16048831ScaelRelocation records have a standard format which 16148831Scaelis described by the 16248831Scael.Fa relocation_info 16348831Scaelstructure: 16448831Scael.Bd -literal -offset indent 16548831Scaelstruct relocation_info { 16648831Scael int r_address; 16748831Scael unsigned int r_symbolnum : 24, 16848831Scael r_pcrel : 1, 16948831Scael r_length : 2, 17048831Scael r_extern : 1, 17148831Scael : 4; 17248831Scael}; 17348831Scael.Ed 17448831Scael.Pp 17548831ScaelThe 17648831Scael.Fa relocation_info 17748831Scaelfields are used as follows: 17848831Scael.Bl -tag -width r_symbolnum 17948831Scael.It Fa r_address 18048831ScaelContains the byte offset of a pointer that needs to be link-edited. 18148831ScaelText relocation offsets are reckoned from the start of the text segment, 18248831Scaeland data relocation offsets from the start of the data segment. 18348831ScaelThe link editor adds the value that is already stored at this offset 18448831Scaelinto the new value that it computes using this relocation record. 18548831Scael.It Fa r_symbolnum 18648831ScaelContains the ordinal number of a symbol structure 18748831Scaelin the symbol table (it is 18848831Scael.Em not 18948831Scaela byte offset). 19048831ScaelAfter the link editor resolves the absolute address for this symbol, 19148831Scaelit adds that address to the pointer that is undergoing relocation. 19248831Scael(If the 19348831Scael.Fa r_extern 19448831Scaelbit is clear, the situation is different; see below.) 19548831Scael.It Fa r_pcrel 19648831ScaelIf this is set, 19748831Scaelthe link editor assumes that it is updating a pointer 19848831Scaelthat is part of a machine code instruction using pc-relative addressing. 19948831ScaelThe address of the relocated pointer is implicitly added 20048831Scaelto its value when the running program uses it. 20148831Scael.It Fa r_length 20248831ScaelContains the log base 2 of the length of the pointer in bytes; 20348831Scael0 for 1-byte displacements, 1 for 2-byte displacements, 20448831Scael2 for 4-byte displacements. 20548831Scael.It Fa r_extern 20648831ScaelSet if this relocation requires an external reference; 20748831Scaelthe link editor must use a symbol address to update the pointer. 20848831ScaelWhen the 20948831Scael.Fa r_extern 21048831Scaelbit is clear, the relocation is 21148831Scael.Sq local ; 21248831Scaelthe link editor updates the pointer to reflect 21348831Scaelchanges in the load addresses of the various segments, 21448831Scaelrather than changes in the value of a symbol. 21548831ScaelIn this case, the content of the 21648831Scael.Fa r_symbolnum 21748831Scaelfield is an 21848831Scael.Fa n_type 21948831Scaelvalue (see below); 22048831Scaelthis type field tells the link editor 22148831Scaelwhat segment the relocated pointer points into. 22248831Scael.El 22348831Scael.Pp 22448831ScaelSymbols map names to addresses (or more generally, strings to values). 22548831ScaelSince the link-editor adjusts addresses, 22648831Scaela symbol's name must be used to stand for its address 22748831Scaeluntil an absolute value has been assigned. 22848831ScaelSymbols consist of a fixed-length record in the symbol table 22948831Scaeland a variable-length name in the string table. 23048831ScaelThe symbol table is an array of 23148831Scael.Fa nlist 23248831Scaelstructures: 23348831Scael.Bd -literal -offset indent 23420755Smckusickstruct nlist { 23520755Smckusick union { 23648831Scael char *n_name; 23748831Scael long n_strx; 23820755Smckusick } n_un; 23948831Scael unsigned char n_type; 24048831Scael char n_other; 24148831Scael short n_desc; 24248831Scael unsigned long n_value; 24320755Smckusick}; 24448831Scael.Ed 24548831Scael.Pp 24648831ScaelThe fields are used as follows: 24748831Scael.Bl -tag -width n_un.n_strx 24848831Scael.It Fa n_un.n_strx 24948831ScaelContains a byte offset into the string table 25048831Scaelfor the name of this symbol. 25148831ScaelWhen a program accesses a symbol table with the 25248831Scael.Xr nlist 3 25348831Scaelfunction, 25448831Scaelthis field is replaced with the 25548831Scael.Fa n_un.n_name 25648831Scaelfield, which is a pointer to the string in memory. 25748831Scael.It Fa n_type 25848831ScaelUsed by the link editor to determine 25948831Scaelhow to update the symbol's value. 26048831ScaelThe 26148831Scael.Fa n_type 26248831Scaelfield is broken down into three sub-fields using bitmasks. 26348831ScaelThe link editor treats symbols with the 26448831Scael.Dv N_EXT 26548831Scaeltype bit set as 26648831Scael.Sq external 26748831Scaelsymbols and permits references to them from other binary files. 26848831ScaelThe 26948831Scael.Dv N_TYPE 27048831Scaelmask selects bits of interest to the link editor: 27148831Scael.Bl -tag -width N_TEXT 27248831Scael.It Dv N_UNDF 27348831ScaelAn undefined symbol. 27448831ScaelThe link editor must locate an external symbol with the same name 27548831Scaelin another binary file to determine the absolute value of this symbol. 27648831ScaelAs a special case, if the 27748831Scael.Fa n_value 27848831Scaelfield is nonzero and no binary file in the link-edit defines this symbol, 27948831Scaelthe link-editor will resolve this symbol to an address 28048831Scaelin the bss segment, 28148831Scaelreserving an amount of bytes equal to 28248831Scael.Fa n_value . 28348831ScaelIf this symbol is undefined in more than one binary file 28448831Scaeland the binary files do not agree on the size, 28548831Scaelthe link editor chooses the greatest size found across all binaries. 28648831Scael.It Dv N_ABS 28748831ScaelAn absolute symbol. 28848831ScaelThe link editor does not update an absolute symbol. 28948831Scael.It Dv N_TEXT 29048831ScaelA text symbol. 29148831ScaelThis symbol's value is a text address and 29248831Scaelthe link editor will update it when it merges binary files. 29348831Scael.It Dv N_DATA 29448831ScaelA data symbol; similar to 29548831Scael.Dv N_TEXT 29648831Scaelbut for data addresses. 29748831ScaelThe values for text and data symbols are not file offsets but 29848831Scaeladdresses; to recover the file offsets, it is necessary 29948831Scaelto identify the loaded address of the beginning of the corresponding 30048831Scaelsection and subtract it, then add the offset of the section. 30148831Scael.It Dv N_BSS 30248831ScaelA bss symbol; like text or data symbols but 30348831Scaelhas no corresponding offset in the binary file. 30448831Scael.It Dv N_FN 30548831ScaelA filename symbol. 30648831ScaelThe link editor inserts this symbol before 30748831Scaelthe other symbols from a binary file when 30848831Scaelmerging binary files. 30948831ScaelThe name of the symbol is the filename given to the link editor, 31048831Scaeland its value is the first text address from that binary file. 31148831ScaelFilename symbols are not needed for link-editing or loading, 31248831Scaelbut are useful for debuggers. 31348831Scael.El 31448831Scael.Pp 31548831ScaelThe 31648831Scael.Dv N_STAB 31748831Scaelmask selects bits of interest to symbolic debuggers 31848831Scaelsuch as 31948831Scael.Xr gdb 1 ; 32048831Scaelthe values are described in 32148831Scael.Xr stab 5 . 32248831Scael.It Fa n_other 32348831ScaelThis field is currently unused. 32448831Scael.It Fa n_desc 32548831ScaelReserved for use by debuggers; passed untouched by the link editor. 32648831ScaelDifferent debuggers use this field for different purposes. 32748831Scael.It Fa n_value 32848831ScaelContains the value of the symbol. 32948831ScaelFor text, data and bss symbols, this is an address; 33048831Scaelfor other symbols (such as debugger symbols), 33148831Scaelthe value may be arbitrary. 33248831Scael.El 33348831Scael.Pp 33448831ScaelThe string table consists of an 33548831Scael.Em unsigned long 33648831Scaellength followed by null-terminated symbol strings. 33748831ScaelThe length represents the size of the entire table in bytes, 33848831Scaelso its minimum value (or the offset of the first string) 33948831Scaelis always 4 on 32-bit machines. 34048831Scael.Sh SEE ALSO 34148831Scael.Xr ld 1 , 34248831Scael.Xr execve 2 , 34348831Scael.Xr nlist 3 , 34448831Scael.Xr core 5 , 34548831Scael.Xr dbx 5 , 34648831Scael.Xr stab 5 34748831Scael.Sh HISTORY 34848831ScaelThe 34948831Scael.Pa a.out.h 35048831Scaelinclude file appeared in 35148831Scael.At v7 . 35248831Scael.Sh BUGS 35348831ScaelSince not all of the supported architectures use the 35448831Scael.Fa a_mid 35548831Scaelfield, 35648831Scaelit can be difficult to determine what 35748831Scaelarchitecture a binary will execute on 35848831Scaelwithout examining its actual machine code. 35948831ScaelEven with a machine identifier, 36048831Scaelthe byte order of the 36148831Scael.Fa exec 36248831Scaelheader is machine-dependent. 36348831Scael.Pp 36448831ScaelNobody seems to agree on what 36548831Scael.Em bss 36648831Scaelstands for. 36748831Scael.Pp 36848831ScaelNew binary file formats may be supported in the future, 36948831Scaeland they probably will not be compatible at any level 37048831Scaelwith this ancient format. 371