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