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