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