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