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