161607Sbostic.\" Copyright (c) 1991, 1993 261607Sbostic.\" 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*67344Sah.\" @(#)a.out.5 8.2 (Berkeley) 06/01/94 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 99*67344Sah.ne 1i 10048831Scael.It Dv OMAGIC 10148831ScaelThe text and data segments immediately follow the header 10248831Scaeland are contiguous. 10348831ScaelThe kernel loads both text and data segments into writable memory. 10448831Scael.It Dv NMAGIC 10548831ScaelAs with 10648831Scael.Dv OMAGIC , 10748831Scaeltext and data segments immediately follow the header and are contiguous. 10848831ScaelHowever, the kernel loads the text into read-only memory 10948831Scaeland loads the data into writable memory at the next 11048831Scaelpage boundary after the text. 11148831Scael.It Dv ZMAGIC 11248831ScaelThe kernel loads individual pages on demand from the binary. 11348831ScaelThe header, text segment and data segment are all 11448831Scaelpadded by the link editor to a multiple of the page size. 11548831ScaelPages that the kernel loads from the text segment are read-only, 11648831Scaelwhile pages from the data segment are writable. 11748831Scael.El 11848831Scael.It Fa a_text 11948831ScaelContains the size of the text segment in bytes. 12048831Scael.It Fa a_data 12148831ScaelContains the size of the data segment in bytes. 12248831Scael.It Fa a_bss 12348831ScaelContains the number of bytes in the 12448831Scael.Sq bss segment 12548831Scaeland is used by the kernel to set the initial break 12648831Scael.Pq Xr brk 2 12748831Scaelafter the data segment. 12848831ScaelThe kernel loads the program so that this amount of writable memory 12948831Scaelappears to follow the data segment and initially reads as zeroes. 13048831Scael.It Fa a_syms 13148831ScaelContains the size in bytes of the symbol table section. 13248831Scael.It Fa a_entry 13348831ScaelContains the address in memory of the entry point 13448831Scaelof the program after the kernel has loaded it; 13548831Scaelthe kernel starts the execution of the program 13648831Scaelfrom the machine instruction at this address. 13748831Scael.It Fa a_trsize 13848831ScaelContains the size in bytes of the text relocation table. 13948831Scael.It Fa a_drsize 14048831ScaelContains the size in bytes of the data relocation table. 14148831Scael.El 14248831Scael.Pp 14348831ScaelThe 14448831Scael.Pa a.out.h 14548831Scaelinclude file defines several macros which use an 14648831Scael.Fa exec 14748831Scaelstructure to test consistency or to locate section offsets in the binary file. 14848831Scael.Bl -tag -width N_BADMAG(exec) 14948831Scael.It Fn N_BADMAG exec 15048831ScaelNonzero if the 15148831Scael.Fa a_magic 15248831Scaelfield does not contain a recognized value. 15348831Scael.It Fn N_TXTOFF exec 15448831ScaelThe byte offset in the binary file of the beginning of the text segment. 15548831Scael.It Fn N_SYMOFF exec 15648831ScaelThe byte offset of the beginning of the symbol table. 15748831Scael.It Fn N_STROFF exec 15848831ScaelThe byte offset of the beginning of the string table. 15948831Scael.El 16048831Scael.Pp 16148831ScaelRelocation records have a standard format which 16248831Scaelis described by the 16348831Scael.Fa relocation_info 16448831Scaelstructure: 16548831Scael.Bd -literal -offset indent 16648831Scaelstruct relocation_info { 16748831Scael int r_address; 16848831Scael unsigned int r_symbolnum : 24, 16948831Scael r_pcrel : 1, 17048831Scael r_length : 2, 17148831Scael r_extern : 1, 17248831Scael : 4; 17348831Scael}; 17448831Scael.Ed 17548831Scael.Pp 17648831ScaelThe 17748831Scael.Fa relocation_info 17848831Scaelfields are used as follows: 17948831Scael.Bl -tag -width r_symbolnum 18048831Scael.It Fa r_address 18148831ScaelContains the byte offset of a pointer that needs to be link-edited. 18248831ScaelText relocation offsets are reckoned from the start of the text segment, 18348831Scaeland data relocation offsets from the start of the data segment. 18448831ScaelThe link editor adds the value that is already stored at this offset 18548831Scaelinto the new value that it computes using this relocation record. 186*67344Sah.ne 1i 18748831Scael.It Fa r_symbolnum 18848831ScaelContains the ordinal number of a symbol structure 18948831Scaelin the symbol table (it is 19048831Scael.Em not 19148831Scaela byte offset). 19248831ScaelAfter the link editor resolves the absolute address for this symbol, 19348831Scaelit adds that address to the pointer that is undergoing relocation. 19448831Scael(If the 19548831Scael.Fa r_extern 19648831Scaelbit is clear, the situation is different; see below.) 19748831Scael.It Fa r_pcrel 19848831ScaelIf this is set, 19948831Scaelthe link editor assumes that it is updating a pointer 20048831Scaelthat is part of a machine code instruction using pc-relative addressing. 20148831ScaelThe address of the relocated pointer is implicitly added 20248831Scaelto its value when the running program uses it. 20348831Scael.It Fa r_length 20448831ScaelContains the log base 2 of the length of the pointer in bytes; 20548831Scael0 for 1-byte displacements, 1 for 2-byte displacements, 20648831Scael2 for 4-byte displacements. 20748831Scael.It Fa r_extern 20848831ScaelSet if this relocation requires an external reference; 20948831Scaelthe link editor must use a symbol address to update the pointer. 21048831ScaelWhen the 21148831Scael.Fa r_extern 21248831Scaelbit is clear, the relocation is 21348831Scael.Sq local ; 21448831Scaelthe link editor updates the pointer to reflect 21548831Scaelchanges in the load addresses of the various segments, 21648831Scaelrather than changes in the value of a symbol. 21748831ScaelIn this case, the content of the 21848831Scael.Fa r_symbolnum 21948831Scaelfield is an 22048831Scael.Fa n_type 22148831Scaelvalue (see below); 22248831Scaelthis type field tells the link editor 22348831Scaelwhat segment the relocated pointer points into. 22448831Scael.El 22548831Scael.Pp 22648831ScaelSymbols map names to addresses (or more generally, strings to values). 22748831ScaelSince the link-editor adjusts addresses, 22848831Scaela symbol's name must be used to stand for its address 22948831Scaeluntil an absolute value has been assigned. 23048831ScaelSymbols consist of a fixed-length record in the symbol table 23148831Scaeland a variable-length name in the string table. 23248831ScaelThe symbol table is an array of 23348831Scael.Fa nlist 23448831Scaelstructures: 23548831Scael.Bd -literal -offset indent 23620755Smckusickstruct nlist { 23720755Smckusick union { 23848831Scael char *n_name; 23948831Scael long n_strx; 24020755Smckusick } n_un; 24148831Scael unsigned char n_type; 24248831Scael char n_other; 24348831Scael short n_desc; 24448831Scael unsigned long n_value; 24520755Smckusick}; 24648831Scael.Ed 24748831Scael.Pp 24848831ScaelThe fields are used as follows: 24948831Scael.Bl -tag -width n_un.n_strx 25048831Scael.It Fa n_un.n_strx 25148831ScaelContains a byte offset into the string table 25248831Scaelfor the name of this symbol. 25348831ScaelWhen a program accesses a symbol table with the 25448831Scael.Xr nlist 3 25548831Scaelfunction, 25648831Scaelthis field is replaced with the 25748831Scael.Fa n_un.n_name 25848831Scaelfield, which is a pointer to the string in memory. 25948831Scael.It Fa n_type 26048831ScaelUsed by the link editor to determine 26148831Scaelhow to update the symbol's value. 26248831ScaelThe 26348831Scael.Fa n_type 26448831Scaelfield is broken down into three sub-fields using bitmasks. 26548831ScaelThe link editor treats symbols with the 26648831Scael.Dv N_EXT 26748831Scaeltype bit set as 26848831Scael.Sq external 26948831Scaelsymbols and permits references to them from other binary files. 27048831ScaelThe 27148831Scael.Dv N_TYPE 27248831Scaelmask selects bits of interest to the link editor: 27348831Scael.Bl -tag -width N_TEXT 27448831Scael.It Dv N_UNDF 27548831ScaelAn undefined symbol. 27648831ScaelThe link editor must locate an external symbol with the same name 27748831Scaelin another binary file to determine the absolute value of this symbol. 27848831ScaelAs a special case, if the 27948831Scael.Fa n_value 28048831Scaelfield is nonzero and no binary file in the link-edit defines this symbol, 28148831Scaelthe link-editor will resolve this symbol to an address 28248831Scaelin the bss segment, 28348831Scaelreserving an amount of bytes equal to 28448831Scael.Fa n_value . 28548831ScaelIf this symbol is undefined in more than one binary file 28648831Scaeland the binary files do not agree on the size, 28748831Scaelthe link editor chooses the greatest size found across all binaries. 28848831Scael.It Dv N_ABS 28948831ScaelAn absolute symbol. 29048831ScaelThe link editor does not update an absolute symbol. 29148831Scael.It Dv N_TEXT 29248831ScaelA text symbol. 29348831ScaelThis symbol's value is a text address and 29448831Scaelthe link editor will update it when it merges binary files. 29548831Scael.It Dv N_DATA 29648831ScaelA data symbol; similar to 29748831Scael.Dv N_TEXT 29848831Scaelbut for data addresses. 29948831ScaelThe values for text and data symbols are not file offsets but 30048831Scaeladdresses; to recover the file offsets, it is necessary 30148831Scaelto identify the loaded address of the beginning of the corresponding 30248831Scaelsection and subtract it, then add the offset of the section. 30348831Scael.It Dv N_BSS 30448831ScaelA bss symbol; like text or data symbols but 30548831Scaelhas no corresponding offset in the binary file. 30648831Scael.It Dv N_FN 30748831ScaelA filename symbol. 30848831ScaelThe link editor inserts this symbol before 30948831Scaelthe other symbols from a binary file when 31048831Scaelmerging binary files. 31148831ScaelThe name of the symbol is the filename given to the link editor, 31248831Scaeland its value is the first text address from that binary file. 31348831ScaelFilename symbols are not needed for link-editing or loading, 31448831Scaelbut are useful for debuggers. 31548831Scael.El 31648831Scael.Pp 31748831ScaelThe 31848831Scael.Dv N_STAB 31948831Scaelmask selects bits of interest to symbolic debuggers 32048831Scaelsuch as 32148831Scael.Xr gdb 1 ; 32248831Scaelthe values are described in 32348831Scael.Xr stab 5 . 32448831Scael.It Fa n_other 32548831ScaelThis field is currently unused. 32648831Scael.It Fa n_desc 32748831ScaelReserved for use by debuggers; passed untouched by the link editor. 32848831ScaelDifferent debuggers use this field for different purposes. 32948831Scael.It Fa n_value 33048831ScaelContains the value of the symbol. 33148831ScaelFor text, data and bss symbols, this is an address; 33248831Scaelfor other symbols (such as debugger symbols), 33348831Scaelthe value may be arbitrary. 33448831Scael.El 33548831Scael.Pp 33648831ScaelThe string table consists of an 33748831Scael.Em unsigned long 33848831Scaellength followed by null-terminated symbol strings. 33948831ScaelThe length represents the size of the entire table in bytes, 34048831Scaelso its minimum value (or the offset of the first string) 34148831Scaelis always 4 on 32-bit machines. 34248831Scael.Sh SEE ALSO 34348831Scael.Xr ld 1 , 34448831Scael.Xr execve 2 , 34548831Scael.Xr nlist 3 , 34648831Scael.Xr core 5 , 34748831Scael.Xr dbx 5 , 34848831Scael.Xr stab 5 34948831Scael.Sh HISTORY 35048831ScaelThe 35148831Scael.Pa a.out.h 35248831Scaelinclude file appeared in 35348831Scael.At v7 . 35448831Scael.Sh BUGS 35548831ScaelSince not all of the supported architectures use the 35648831Scael.Fa a_mid 35748831Scaelfield, 35848831Scaelit can be difficult to determine what 35948831Scaelarchitecture a binary will execute on 36048831Scaelwithout examining its actual machine code. 36148831ScaelEven with a machine identifier, 36248831Scaelthe byte order of the 36348831Scael.Fa exec 36448831Scaelheader is machine-dependent. 36548831Scael.Pp 36648831ScaelNobody seems to agree on what 36748831Scael.Em bss 36848831Scaelstands for. 36948831Scael.Pp 37048831ScaelNew binary file formats may be supported in the future, 37148831Scaeland they probably will not be compatible at any level 37248831Scaelwith this ancient format. 373