1 .\" $NetBSD: elf.5,v 1.13 2006/11/18 14:32:14 pooka Exp $
3 .\" Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
6 .\" This document is derived from work contributed to The NetBSD Foundation
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
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.
18 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE
22 .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 .\" POSSIBILITY OF SUCH DAMAGE.
35 .Nd executable and linking format
39 Because of the flexible nature of ELF, the structures describing it are
40 available both as 32bit and 64bit versions. This document uses the 32bit
43 for the corresponding 64bit versions.
45 The four main types of an ELF object file are:
46 .Bl -tag -width "relocatable"
48 A file suitable for execution. It contains the information required for
49 creating a new process image.
51 Contains the necessary information to be run through the link editor
53 to create an executable or a shared library.
55 The shared object contains necessary information which can be used by
56 either the link editor
58 at link time or by the dynamic loader
62 A file which describes the virtual address space and register state
63 of a process. Core files are typically used in conjunction with
68 ELF files have a dual nature. The toolchain, including tools such as the
72 treats them as a set of sections described by their section headers. The system
73 loader treats them as a set of segments described by the program headers.
75 The general format of an ELF file is the following: The file starts with an
76 ELF header. This is followed by a table of program headers (optional for
77 relocatable and shared files). After this come the sections/segments.
78 The file ends with a table of section headers (optional for executable
81 A segment can be considered to consist of several sections. For example,
82 all executable sections are typically packed into one loadable segment
83 which is read-only and executable (see
85 in the program header). This enables the system to map the entire file with
86 just a few operations, one for each loadable segment, instead of doing
87 numerous map operations for each section separately.
89 Each file is described by the ELF header:
90 .Bd -literal -offset indent
92 unsigned char e_ident[ELF_NIDENT];
101 Elf32_Half e_phentsize;
103 Elf32_Half e_shentsize;
105 Elf32_Half e_shstrndx;
109 .Bl -tag -width "e_phentsize"
111 The array contains the following information in the indicated locations:
112 .Bl -tag -width EI_ABIVERSION
114 The elements ranging from
118 contain the ELF magic number: \\0177ELF
120 Contains the address size of the binary, either 32 or 64bit.
124 Contains the ELF header version. This is currently always set to 1.
126 Contains the operating system ABI identification. Note that even though the
135 ABI does not deviate from the standard.
140 Contains the file type identification. It can be either
146 for relocatable, executable, shared, or core, respectively.
148 Contains the machine type, e.g. SPARC, Alpha, MIPS, ...
150 The program entry point if the file is executable.
152 The position of the program header table in the file or 0 if it doesn't exist.
154 The position of the section header table in the file or 0 if it doesn't exist.
156 Contains processor-specific flags. For example, the SPARC port uses this
157 space to specify what kind of memory store ordering is required.
159 The size of the ELF header.
161 The size of an entry in the program header table. All entries are the same
164 The number of entries in the program header table, or 0 if none exists.
166 The size of an entry in the section header table. All entries are the same
169 The number of entries in the section header table, or 0 if none exists.
171 Contains the index number of the section which contains the section
175 Each ELF section in turn is described by the section header:
176 .Bd -literal -offset indent
186 Elf32_Word sh_addralign;
187 Elf32_Word sh_entsize;
191 .Bl -tag -width "sh_addralign"
193 Contains an index to the position in the section header string section where
194 the name of the current section can be found.
196 Contains the section type indicator. The more important possible values are:
197 .Bl -tag -width "SHT_PROGBITS"
199 Section is inactive. The other fields contain undefined values.
201 Section contains program information. It can be for example code, data,
202 or debugger information.
204 Section contains a symbol table. This section usually contains all the
205 symbols and is intended for the regular link editor
208 Section contains a string table.
210 Section contains relocation information with an explicit addend.
212 Section contains a symbol hash table.
214 Section contains dynamic linking information.
216 Section contains some special information. The format can be e.g.
219 Sections contains information similar to
221 but takes up no space in the file. This can be used for e.g. bss.
223 Section contains relocation information without an explicit addend.
225 This section type is reserved but has unspecified semantics.
227 Section contains a symbol table. This symbol table is intended for the
228 dynamic linker, and is kept as small as possible to conserve space, since
229 it must be loaded to memory at run time.
232 Contains the section flags, which can have the following values or any
234 .Bl -tag -width SHF_EXECINSTR
236 Section is writable after it has been loaded.
238 Section will occupy memory at run time.
240 Section contains executable machine instructions.
243 Address to where the section will be loaded, or 0 if this section does not
244 reside in memory at run time.
246 The byte offset from the beginning of the file to the beginning of this
247 section. If the section is of type
249 this field specifies the conceptual placement in the file.
251 The size of the section in the file for all types except
253 For that type the value may differ from zero, but the section will still
254 always take up no space from the file.
256 Contains an index to the section header table. The interpretation depends
257 on the section type as follows:
259 .Bl -tag -compact -width SHT_DYNAMIC
262 Section index of the associated symbol table.
266 Section index of the associated string table.
269 Section index of the symbol table to which the hash table applies.
272 Section index of of the string table by which entries in this section are used.
275 Contains extra information. The interpretation depends on the type as
278 .Bl -tag -compact -width SHT_DYNSYM
281 Section index of the section to which the relocation information applies.
285 Contains a value one greater that the last local symbol table index.
288 Marks the section alignment requirement. If, for example, the section contains
289 a doubleword, the entire section must be doubleword aligned to ensure proper
290 alignment. Only 0 and integral powers of two are allowed. Values 0 and 1
291 denote that the section has no alignment.
293 Contains the entry size of an element for sections which are constructed
294 of a table of fixed-size entries. If the section does not hold a table of
295 fixed-size entries, this value is 0.
298 Every executable object must contain a program header. The program header
299 contains information necessary in constructing a process image.
300 .Bd -literal -offset indent
313 .Bl -tag -width p_offset
315 Contains the segment type indicator. The possible values are:
316 .Bl -tag -width PT_DYNAMIC
318 Segment is inactive. The other fields contain undefined values.
320 Segment is loadable. It is loaded to the address described by
326 the memory range from
334 is zero-filled when the segment is loaded.
336 can not be greater than
338 Segments of this type are sorted in the header table by
342 Segment contains dynamic linking information.
344 Segment contains a null-terminated path name to the interpreter. This segment
345 may be present only once in a file, and it must appear before any loadable
346 segments. This field will most likely contain the ELF dynamic loader:
347 .Pa /libexec/ld.elf_so
349 Segment contains some special information. Format can be e.g. vendor-specific.
351 This segment type is reserved but has unspecified semantics. Programs
352 which contain a segment of this type do not conform to the ABI, and must
353 indicate this by setting the appropriate ABI in the ELF header
357 The values in a program header of this type specify the characteristics
358 of the program header table itself. For example, the
360 field specifies the program header table location in memory once the
361 program is loaded. This field may not occur more than once, may occur only
362 if the program header table is part of the file memory image, and must
363 come before any loadable segments.
366 Contains the byte offset from the beginning of the file to the beginning
369 Contains the virtual memory address to which this segment is loaded.
371 Contains the physical address to which this segment is loaded. This value
372 is usually ignored, but may be used while bootstrapping or in embedded
375 Contains the number of bytes this segment occupies in the file image.
377 Contains the number of bytes this segment occupies in the memory image.
379 Contains the segment flags, which specify the permissions for the segment
380 after it has been loaded. The following values or any combination of them
386 Segment can be written.
388 Segment is executable.
391 Contains the segment alignment. Acceptable values are 0 and 1 for no alignment,
392 and integral powers of two.
411 The ELF object file format first appeared in