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