xref: /dragonfly/share/man/man5/a.out.5 (revision 32efd857)
1.\" Copyright (c) 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This man page is derived from documentation contributed to Berkeley by
5.\" Donn Seeley at UUNET Technologies, Inc.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. Neither the name of the University nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\"	@(#)a.out.5	8.1 (Berkeley) 6/5/93
32.\" $FreeBSD: src/share/man/man5/a.out.5,v 1.10.2.4 2002/04/16 14:50:18 trhodes Exp $
33.\"
34.Dd December 16, 2021
35.Dt A.OUT 5
36.Os
37.Sh NAME
38.Nm a.out
39.Nd format of executable binary files
40.Sh SYNOPSIS
41.In a.out.h
42.Sh DESCRIPTION
43.Bf -emphasis
44This manual page describes an obsolete executable format.
45See
46.Xr elf 5
47for more information on
48.Dx Ap s
49current executable format.
50.Ef
51.Pp
52The include file
53.In a.out.h
54declares three structures and several macros.
55The structures describe the format of
56executable machine code files
57.Pq Sq binaries
58on the system.
59.Pp
60A binary file consists of up to 7 sections.
61In order, these sections are:
62.Bl -tag -width "text relocations"
63.It exec header
64Contains parameters used by the kernel
65to load a binary file into memory and execute it,
66and by the link editor
67.Xr ld 1
68to combine a binary file with other binary files.
69This section is the only mandatory one.
70.It text segment
71Contains machine code and related data
72that are loaded into memory when a program executes.
73May be loaded read-only.
74.It data segment
75Contains initialized data; always loaded into writable memory.
76.It text relocations
77Contains records used by the link editor
78to update pointers in the text segment when combining binary files.
79.It data relocations
80Like the text relocation section, but for data segment pointers.
81.It symbol table
82Contains records used by the link editor
83to cross reference the addresses of named variables and functions
84.Pq Sq symbols
85between binary files.
86.It string table
87Contains the character strings corresponding to the symbol names.
88.El
89.Pp
90Every binary file begins with an
91.Fa exec
92structure:
93.Bd -literal -offset indent
94struct exec {
95	unsigned long	a_midmag;
96	unsigned long	a_text;
97	unsigned long	a_data;
98	unsigned long	a_bss;
99	unsigned long	a_syms;
100	unsigned long	a_entry;
101	unsigned long	a_trsize;
102	unsigned long	a_drsize;
103};
104.Ed
105.Pp
106The fields have the following functions:
107.Bl -tag -width a_trsize
108.It Fa a_midmag
109This field is stored in host byte-order.
110It has a number of sub-components accessed by the macros
111.Fn N_GETFLAG ,
112.Fn N_GETMID ,
113and
114.Fn N_GETMAGIC ,
115and set by the macro
116.Fn N_SETMAGIC .
117.Pp
118The macro
119.Fn N_GETFLAG
120returns a few flags:
121.Bl -tag -width EX_DYNAMIC
122.It Dv EX_DYNAMIC
123indicates that the executable requires the services of the run-time link editor.
124.It Dv EX_PIC
125indicates that the object contains position independent code.
126This flag is
127set by
128.Xr as 1
129when given the
130.Sq -k
131flag and is preserved by
132.Xr ld 1
133if necessary.
134.El
135.Pp
136If both EX_DYNAMIC and EX_PIC are set, the object file is a position independent
137executable image (eg. a shared library), which is to be loaded into the
138process address space by the run-time link editor.
139.Pp
140The macro
141.Fn N_GETMID
142returns the machine-id.
143This indicates which machine(s) the binary is intended to run on.
144.Pp
145.Fn N_GETMAGIC
146specifies the magic number, which uniquely identifies binary files
147and distinguishes different loading conventions.
148The field must contain one of the following values:
149.Bl -tag -width ZMAGIC
150.It Dv OMAGIC
151The text and data segments immediately follow the header
152and are contiguous.
153The kernel loads both text and data segments into writable memory.
154.It Dv NMAGIC
155As with
156.Dv OMAGIC ,
157text and data segments immediately follow the header and are contiguous.
158However, the kernel loads the text into read-only memory
159and loads the data into writable memory at the next
160page boundary after the text.
161.It Dv ZMAGIC
162The kernel loads individual pages on demand from the binary.
163The header, text segment and data segment are all
164padded by the link editor to a multiple of the page size.
165Pages that the kernel loads from the text segment are read-only,
166while pages from the data segment are writable.
167.El
168.It Fa a_text
169Contains the size of the text segment in bytes.
170.It Fa a_data
171Contains the size of the data segment in bytes.
172.It Fa a_bss
173Contains the number of bytes in the
174.Sq bss segment .
175The kernel loads the program so that this amount of writable memory
176appears to follow the data segment and initially reads as zeroes.
177.Po
178.Em bss
179= block started by symbol
180.Pc
181.It Fa a_syms
182Contains the size in bytes of the symbol table section.
183.It Fa a_entry
184Contains the address in memory of the entry point
185of the program after the kernel has loaded it;
186the kernel starts the execution of the program
187from the machine instruction at this address.
188.It Fa a_trsize
189Contains the size in bytes of the text relocation table.
190.It Fa a_drsize
191Contains the size in bytes of the data relocation table.
192.El
193.Pp
194The
195.In a.out.h
196include file defines several macros which use an
197.Fa exec
198structure to test consistency or to locate section offsets in the binary file.
199.Bl -tag -width N_BADMAG(exec)
200.It Fn N_BADMAG exec
201Nonzero if the
202.Fa a_magic
203field does not contain a recognized value.
204.It Fn N_TXTOFF exec
205The byte offset in the binary file of the beginning of the text segment.
206.It Fn N_SYMOFF exec
207The byte offset of the beginning of the symbol table.
208.It Fn N_STROFF exec
209The byte offset of the beginning of the string table.
210.El
211.Pp
212Relocation records have a standard format which
213is described by the
214.Fa relocation_info
215structure:
216.Bd -literal -offset indent
217struct relocation_info {
218	int		r_address;
219	unsigned int	r_symbolnum : 24,
220			r_pcrel : 1,
221			r_length : 2,
222			r_extern : 1,
223			r_baserel : 1,
224			r_jmptable : 1,
225			r_relative : 1,
226			r_copy : 1;
227};
228.Ed
229.Pp
230The
231.Fa relocation_info
232fields are used as follows:
233.Bl -tag -width r_symbolnum
234.It Fa r_address
235Contains the byte offset of a pointer that needs to be link-edited.
236Text relocation offsets are reckoned from the start of the text segment,
237and data relocation offsets from the start of the data segment.
238The link editor adds the value that is already stored at this offset
239into the new value that it computes using this relocation record.
240.It Fa r_symbolnum
241Contains the ordinal number of a symbol structure
242in the symbol table (it is
243.Em not
244a byte offset).
245After the link editor resolves the absolute address for this symbol,
246it adds that address to the pointer that is undergoing relocation.
247(If the
248.Fa r_extern
249bit is clear, the situation is different; see below.)
250.It Fa r_pcrel
251If this is set,
252the link editor assumes that it is updating a pointer
253that is part of a machine code instruction using pc-relative addressing.
254The address of the relocated pointer is implicitly added
255to its value when the running program uses it.
256.It Fa r_length
257Contains the log base 2 of the length of the pointer in bytes;
2580 for 1-byte displacements, 1 for 2-byte displacements,
2592 for 4-byte displacements.
260.It Fa r_extern
261Set if this relocation requires an external reference;
262the link editor must use a symbol address to update the pointer.
263When the
264.Fa r_extern
265bit is clear, the relocation is
266.Sq local ;
267the link editor updates the pointer to reflect
268changes in the load addresses of the various segments,
269rather than changes in the value of a symbol (except when
270.Fa r_baserel
271is also set (see below).
272In this case, the content of the
273.Fa r_symbolnum
274field is an
275.Fa n_type
276value (see below);
277this type field tells the link editor
278what segment the relocated pointer points into.
279.It Fa r_baserel
280If set, the symbol, as identified by the
281.Fa r_symbolnum
282field, is to be relocated to an offset into the Global Offset Table.
283At run-time, the entry in the Global Offset Table at this offset is set to
284be the address of the symbol.
285.It Fa r_jmptable
286If set, the symbol, as identified by the
287.Fa r_symbolnum
288field, is to be relocated to an offset into the Procedure Linkage Table.
289.It Fa r_relative
290If set, this relocation is relative to the (run-time) load address of the
291image this object file is going to be a part of.
292This type of relocation
293only occurs in shared objects.
294.It Fa r_copy
295If set, this relocation record identifies a symbol whose contents should
296be copied to the location given in
297.Fa r_address .
298The copying is done by the run-time link-editor from a suitable data
299item in a shared object.
300.El
301.Pp
302Symbols map names to addresses (or more generally, strings to values).
303Since the link-editor adjusts addresses,
304a symbol's name must be used to stand for its address
305until an absolute value has been assigned.
306Symbols consist of a fixed-length record in the symbol table
307and a variable-length name in the string table.
308The symbol table is an array of
309.Fa nlist
310structures:
311.Bd -literal -offset indent
312struct nlist {
313	union {
314		char	*n_name;
315		long	n_strx;
316	} n_un;
317	unsigned char	n_type;
318	char		n_other;
319	short		n_desc;
320	unsigned long	n_value;
321};
322.Ed
323.Pp
324The fields are used as follows:
325.Bl -tag -width n_un.n_strx
326.It Fa n_un.n_strx
327Contains a byte offset into the string table
328for the name of this symbol.
329When a program accesses a symbol table with the
330.Xr nlist 3
331function,
332this field is replaced with the
333.Fa n_un.n_name
334field, which is a pointer to the string in memory.
335.It Fa n_type
336Used by the link editor to determine
337how to update the symbol's value.
338The
339.Fa n_type
340field is broken down into three sub-fields using bitmasks.
341The link editor treats symbols with the
342.Dv N_EXT
343type bit set as
344.Sq external
345symbols and permits references to them from other binary files.
346The
347.Dv N_TYPE
348mask selects bits of interest to the link editor:
349.Bl -tag -width N_TEXT
350.It Dv N_UNDF
351An undefined symbol.
352The link editor must locate an external symbol with the same name
353in another binary file to determine the absolute value of this symbol.
354As a special case, if the
355.Fa n_value
356field is nonzero and no binary file in the link-edit defines this symbol,
357the link-editor will resolve this symbol to an address
358in the bss segment,
359reserving an amount of bytes equal to
360.Fa n_value .
361If this symbol is undefined in more than one binary file
362and the binary files do not agree on the size,
363the link editor chooses the greatest size found across all binaries.
364.It Dv N_ABS
365An absolute symbol.
366The link editor does not update an absolute symbol.
367.It Dv N_TEXT
368A text symbol.
369This symbol's value is a text address and
370the link editor will update it when it merges binary files.
371.It Dv N_DATA
372A data symbol; similar to
373.Dv N_TEXT
374but for data addresses.
375The values for text and data symbols are not file offsets but
376addresses; to recover the file offsets, it is necessary
377to identify the loaded address of the beginning of the corresponding
378section and subtract it, then add the offset of the section.
379.It Dv N_BSS
380A bss symbol; like text or data symbols but
381has no corresponding offset in the binary file.
382.It Dv N_FN
383A filename symbol.
384The link editor inserts this symbol before
385the other symbols from a binary file when
386merging binary files.
387The name of the symbol is the filename given to the link editor,
388and its value is the first text address from that binary file.
389Filename symbols are not needed for link-editing or loading,
390but are useful for debuggers.
391.El
392.Pp
393The
394.Dv N_STAB
395mask selects bits of interest to symbolic debuggers
396such as
397.Xr gdb 1 ;
398the values are described in
399.Xr stab 5 .
400.It Fa n_other
401This field provides information on the nature of the symbol independent of
402the symbol's location in terms of segments as determined by the
403.Fa n_type
404field.
405Currently, the lower 4 bits of the
406.Fa n_other
407field hold one of two values:
408.Dv AUX_FUNC
409and
410.Dv AUX_OBJECT
411(see
412.In link.h
413for their definitions).
414.Dv AUX_FUNC
415associates the symbol with a callable function, while
416.Dv AUX_OBJECT
417associates the symbol with data, irrespective of their locations in
418either the text or the data segment.
419This field is intended to be used by
420.Xr ld 1
421for the construction of dynamic executables.
422.It Fa n_desc
423Reserved for use by debuggers; passed untouched by the link editor.
424Different debuggers use this field for different purposes.
425.It Fa n_value
426Contains the value of the symbol.
427For text, data and bss symbols, this is an address;
428for other symbols (such as debugger symbols),
429the value may be arbitrary.
430.El
431.Pp
432The string table consists of an
433.Em unsigned long
434length followed by null-terminated symbol strings.
435The length represents the size of the entire table in bytes,
436so its minimum value (or the offset of the first string)
437is always 4 on 32-bit machines.
438.Sh SEE ALSO
439.Xr as 1 ,
440.Xr gdb 1 ,
441.Xr ld 1 ,
442.Xr execve 2 ,
443.Xr nlist 3 ,
444.Xr core 5 ,
445.Xr elf 5 ,
446.Xr link 5 ,
447.Xr stab 5
448.Sh HISTORY
449The
450.In a.out.h
451include file appeared in
452.At v7 .
453.Sh BUGS
454Since not all of the supported architectures use the
455.Fa a_midmag
456field,
457it can be difficult to determine what
458architecture a binary will execute on
459without examining its actual machine code.
460Even with a machine identifier,
461the byte order of the
462.Fa exec
463header is machine-dependent.
464