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