A.OUT(5) A.OUT(5) NAME a.out - object file format SYNOPSIS #include <a.out.h> DESCRIPTION A.out is the default name of the output file of the assem- bler as(1) or the link editor ld(1). Both programs make a.out executable if there were no errors and no unresolved external references. An object file has five sections: a header, the program text and data, relocation information, a symbol table and a string table (in that order). The last three may be absent; see strip(1) and option -s of ld(1). The header format, given in <a.out.h>, is struct exec { long a_magic; /* magic number */ unsigned a_text; /* size of text segment */ unsigned a_data; /* size of initialized data */ unsigned a_bss; /* size of uninitialized data */ unsigned a_syms; /* size of symbol table */ unsigned a_entry; /* entry point */ unsigned a_trsize; /* size of text relocation */ unsigned a_drsize; /* size of data relocation */ }; #define OMAGIC 0407 /* old impure format */ #define NMAGIC 0410 /* read-only text */ #define ZMAGIC 0413 /* demand load format */ Macros which take `exec' structures as arguments and tell whether the file has a reasonable magic number or return offsets: #define N_BADMAG(x) (((x).a_magic)!=OMAGIC && \ ((x).a_magic)!=NMAGIC && ((x).a_magic)!=ZMAGIC) #define N_TXTOFF(x) \ ((x).a_magic==ZMAGIC ? 1024 : sizeof (struct exec)) #define N_SYMOFF(x) (N_TXTOFF(x) + (x).a_text+(x).a_data + \ (x).a_trsize+(x).a_drsize) #define N_STROFF(x) (N_SYMOFF(x) + (x).a_syms) Sizes are expressed in bytes. The size of the header is not included in any of the other sizes. When an file is executed, a memory image of three segments is set up: the text segment, the data segment, and a stack. The text segment begins at virtual address 0. Following the text segment is the data segment, in which explicitly ini- tialized data come first, then other data, initialized to 0. The stack occupies the highest possible locations in the A.OUT(5) A.OUT(5) core image, automatically growing downwards from 0x7ffff400 as needed. The data segment may be extended by brk(2). If the magic number in the header is OMAGIC, the text seg- ment is neither write-protected nor shared and the data seg- ment is immediately contiguous with the text segment. This kind of executable program is rarely used. If the magic number is NMAGIC or ZMAGIC, the data segment is loaded at the first 0 mod 1024 address following the text segment, and the text segment is write-protected and shared among all processes that are executing the same file. ZMAGIC format, which ld(1) produces by default, supports demand paging: the text and data segments are multiples of 1024 bytes long and begin at byte offsets of 0 mod 1024 in the file. Macros are provided to compute the absolute offset of vari- ous parts of the file: N_TXTOFF Text segment. N_SYMOFF Symbol table. N_STROFF String table. The offsets of the data segment, text relocation informa- tion, and data relocation information are obtained by suc- cessively adding to N_TXTOFF the size fields a_text, a_data, and a_trsize. The first 4 bytes of the string table contain it size, including the 4 bytes. The layout of a symbol table entry, as given in <a.out.h>, is struct nlist { union { char *n_name;/* for use when in-core */ long n_strx; /* index into file string table */ } n_un; unsigned char n_type; /* type flag; see below */ char n_other; short n_desc; /* see stab(5) */ unsigned n_value;/* value of this symbol (or struct offset) */ }; Basic values for n_type: #define N_UNDF 0x0 /* undefined */ #define N_ABS 0x2 /* absolute */ #define N_TEXT 0x4 /* text */ #define N_DATA 0x6 /* data */ #define N_BSS 0x8 /* bss */ #define N_COMM 0x12 /* common (internal to ld) */ #define N_FN 0x1f /* file name symbol */ #define N_EXT 01 /* external bit, or'ed in */ A.OUT(5) A.OUT(5) #define N_TYPE 0x1e /* mask for all the type bits */ Other permanent symbol table entries have some N_STAB bits set. These are given in `<stab.h>': #define N_STAB 0xe0 /* if any of these bits set, keep */ The field n_un.n_strx gives an index into the string table; 0 indicates that no name is associated with the entry. The field n_un.n_name can be used to refer to the symbol name only if the program sets this up using n_strx and appropri- ate data from the string table. A symbol of type undefined external with nonzero value field names a common region; the value specifies its size. Relocation information occupies eight bytes per relocatable datum: struct relocation_info { int r_address; /* address of datum to be relocated */ unsigned r_symbolnum:24, /* local symbol ordinal */ r_pcrel:1, /* is referenced relative to pc */ r_length:2, /* 0=byte, 1=word, 2=long */ r_extern:1, /* symbol value unknown */ :4; /* nothing, yet */ }; If r_extern is 1, the datum designated by r_address and r_length will be relocated by adding to it the value of the associated external symbol. If r_extern is 0, r_symbolnum is encoded in the style of n_type and the value will be relocated by adding the relocated base of the designated area (text, initialized data, or common data). SEE ALSO adb(1), as(1), ld(1), nm(1), stab(5), strip(1)