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