xref: /netbsd-src/share/man/man5/elf.5 (revision 5e4c038a45edbc7d63b7c2daa76e29f88b64a4e3)
1.\"	$NetBSD: elf.5,v 1.7 2002/02/13 08:18:11 ross Exp $
2.\"
3.\" Copyright (c) 2001 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This document is derived from work contributed to The NetBSD Foundation
7.\" by Antti Kantee
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
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.
17.\" 3. All advertising materials mentioning features or use of this software
18.\"    must display the following acknowledgement:
19.\"        This product includes software developed by the NetBSD
20.\"        Foundation, Inc. and its contributors.
21.\" 4. Neither the name of The NetBSD Foundation nor the names of its
22.\"    contributors may be used to endorse or promote products derived
23.\"    from this software without specific prior written permission.
24.\"
25.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE
29.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35.\" POSSIBILITY OF SUCH DAMAGE.
36.\"
37.Dd April 14, 2001
38.Dt ELF 5
39.Os
40.Sh NAME
41.Nm ELF
42.Nd executable and linking format
43.Sh SYNOPSIS
44.Fd #include \*[Lt]elf.h\*[Gt]
45.Sh DESCRIPTION
46Because of the flexible nature of ELF, the structures describing it are
47available both as 32bit and 64bit versions. This document uses the 32bit
48versions, refer to
49.Aq Pa elf.h
50for the corresponding 64bit versions.
51.Pp
52The three main types of an ELF object file are:
53.Bl -tag -width "relocatable"
54.It executable
55A file suitable for execution. It contains the information required for
56creating a new process image.
57.It relocatable
58Contains the necessary information to be run through the link editor
59.Xr ld 1
60to create an executable or a shared library.
61.It shared
62The shared object contains necessary information which can be used by
63either the link editor
64.Xr ld 1
65at link time or by the dynamic loader
66.Xr ld.elf_so 1
67at run time.
68.El
69.Pp
70ELF files have a dual nature. The toolchain, including tools such as the
71.Xr as 1
72and linker
73.Xr ld 1 ,
74treats them as a set of sections described by their section headers. The system
75loader treats them as a set of segments described by the program headers.
76.Pp
77The general format of an ELF file is the following: The file starts with an
78ELF header. This is followed by a table of program headers (optional for
79relocatable and shared files). After this come the sections/segments.
80The file ends with a table of section headers (optional for executable
81files).
82.Pp
83A segment can be considered to consist of several sections. For example,
84all executable sections are typically packed into one loadable segment
85which is read-only and executable (see
86.Fa p_flags
87in the program header). This enables the system to map the entire file with
88just a few operations, one for each loadable segment, instead of doing
89numerous map operations for each section separately.
90.Pp
91Each file is described by the ELF header:
92.Bd -literal -offset indent
93typedef struct {
94	unsigned char	e_ident[ELF_NIDENT];
95	Elf32_Half	e_type;
96	Elf32_Half	e_machine;
97	Elf32_Word	e_version;
98	Elf32_Addr	e_entry;
99	Elf32_Off	e_phoff;
100	Elf32_Off	e_shoff;
101	Elf32_Word	e_flags;
102	Elf32_Half	e_ehsize;
103	Elf32_Half	e_phentsize;
104	Elf32_Half	e_phnum;
105	Elf32_Half	e_shentsize;
106	Elf32_Half	e_shnum;
107	Elf32_Half	e_shstrndx;
108} Elf32_Ehdr;
109.Ed
110.Pp
111.Bl -tag -width "e_phentsize"
112.It Fa e_ident[]
113The array contains the following information in the indicated locations:
114.Bl -tag -width EI_ABIVERSION
115.It Dv ELFMAG0
116The elements ranging from
117.Dv ELFMAG0
118to
119.Dv ELFMAG3
120contain the ELF magic number: \\0177ELF
121.It Dv EI_CLASS
122Contains the address size of the binary, either 32 or 64bit.
123.It Dv EI_DATA
124byte order
125.It Dv EI_VERSION
126Contains the ELF header version. This is currently always set to 1.
127.It Dv EI_OSABI
128Contains the operating system ABI identification. Note that even though the
129definition
130.Dv ELFOSABI_NETBSD
131exists,
132.Nx
133uses
134.Dv ELFOSABI_SYSV
135here, since the
136.Nx
137ABI does not deviate from the standard.
138.It Dv EI_ABIVERSION
139ABI version.
140.El
141.It Fa e_type
142Contains the file type identification. It can be either
143.Dv ET_REL ,
144.Dv ET_EXEC ,
145.Dv ET_DYN ,
146or
147.Dv ET_CORE
148for relocatable, executable, shared, or core, respectively.
149.It Fa e_machine
150Contains the machine type, e.g. SPARC, Alpha, MIPS, ...
151.It Fa e_entry
152The program entry point if the file is executable.
153.It Fa e_phoff
154The position of the program header table in the file or 0 if it doesn't exist.
155.It Fa e_shoff
156The position of the section header table in the file or 0 if it doesn't exist.
157.It Fa e_flags
158Contains processor-specific flags. For example, the SPARC port uses this
159space to specify what kind of memory store ordering is required.
160.It Fa e_ehsize
161The size of the ELF header.
162.It Fa e_phentsize
163The size of an entry in the program header table. All entries are the same
164size.
165.It Fa e_phnum
166The number of entries in the program header table, or 0 if none exists.
167.It Fa e_shentsize
168The size of an entry in the section header table. All entries are the same
169size.
170.It Fa e_shnum
171The number of entries in the section header table, or 0 if none exists.
172.It Fa e_shstrndx
173Contains the index number of the section which contains the section
174name strings.
175.El
176.Pp
177Each ELF section in turn is described by the section header:
178.Bd -literal -offset indent
179typedef struct {
180	Elf32_Word	sh_name;
181	Elf32_Word	sh_type;
182	Elf32_Word	sh_flags;
183	Elf32_Addr	sh_addr;
184	Elf32_Off	sh_offset;
185	Elf32_Word	sh_size;
186	Elf32_Word	sh_link;
187	Elf32_Word	sh_info;
188	Elf32_Word	sh_addralign;
189	Elf32_Word	sh_entsize;
190} Elf32_Shdr;
191.Ed
192.Pp
193.Bl -tag -width "sh_addralign"
194.It Fa sh_name
195Contains an index to the position in the section header string section where
196the name of the current section can be found.
197.It Fa sh_type
198Contains the section type indicator. The more important possible values are:
199.Bl -tag -width "SHT_PROGBITS"
200.It Dv SHT_NULL
201Section is inactive. The other fields contain undefined values.
202.It Dv SHT_PROGBITS
203Section contains program information. It can be for example code, data,
204or debugger information.
205.It Dv SHT_SYMTAB
206Section contains a symbol table. This section usually contains all the
207symbols and is intended for the regular link editor
208.Xr ld 1 .
209.It Dv SHT_STRTAB
210Section contains a string table.
211.It Dv SHT_RELA
212Section contains relocation information with an explicit addend.
213.It Dv SHT_HASH
214Section contains a symbol hash table.
215.It Dv SHT_DYNAMIC
216Section contains dynamic linking information.
217.It Dv SHT_NOTE
218Section contains some special information. The format can be e.g.
219vendor-specific.
220.It Dv SHT_NOBITS
221Sections contains information similar to
222.Dv SHT_PROGBITS ,
223but takes up no space in the file. This can be used for e.g. bss.
224.It Dv SHT_REL
225Section contains relocation information without an explicit addend.
226.It Dv SHT_SHLIB
227This section type is reserved but has unspecified semantics.
228.It Dv SHT_DYNSYM
229Section contains a symbol table. This symbol table is intended for the
230dynamic linker, and is kept as small as possible to conserve space, since
231it must be loaded to memory at run time.
232.El
233.It Fa sh_flags
234Contains the section flags, which can have the following values or any
235combination of them:
236.Bl -tag -width SHF_EXECINSTR
237.It Dv SHF_WRITE
238Section is writable after it has been loaded.
239.It Dv SHF_ALLOC
240Section will occupy memory at run time.
241.It Dv SHF_EXECINSTR
242Section contains executable machine instructions.
243.El
244.It Fa sh_addr
245Address to where the section will be loaded, or 0 if this section does not
246reside in memory at run time.
247.It Fa sh_offset
248The byte offset from the beginning of the file to the beginning of this
249section. If the section is of type
250.Dv SHT_NOBITS ,
251this field specifies the conceptual placement in the file.
252.It Fa sh_size
253The size of the section in the file for all types except
254.Dv SHT_NOBITS .
255For that type the value may differ from zero, but the section will still
256always take up no space from the file.
257.It Fa sh_link
258Contains an index to the section header table. The interpretation depends
259on the section type as follows:
260.Pp
261.Bl -tag -compact -width SHT_DYNAMIC
262.It Dv SHT_REL
263.It Dv SHT_RELA
264Section index of the associated symbol table.
265.Pp
266.It Dv SHT_SYMTAB
267.It Dv SHT_DYNSYM
268Section index of the associated string table.
269.Pp
270.It Dv SHT_HASH
271Section index of the symbol table to which the hash table applies.
272.Pp
273.It Dv SHT_DYNAMIC
274Section index of of the string table by which entries in this section are used.
275.El
276.It Fa sh_info
277Contains extra information. The interpretation depends on the type as
278follows:
279.Pp
280.Bl -tag -compact -width SHT_DYNSYM
281.It Dv SHT_REL
282.It Dv SHT_RELA
283Section index of the section to which the relocation information applies.
284.Pp
285.It Dv SHT_SYMTAB
286.It Dv SHT_DYNSYM
287Contains a value one greater that the last local symbol table index.
288.El
289.It Fa sh_addralign
290Marks the section alignment requirement. If, for example, the section contains
291a doubleword, the entire section must be doubleword aligned to ensure proper
292alignment. Only 0 and integral powers of two are allowed. Values 0 and 1
293denote that the section has no alignment.
294.It Fa sh_entsize
295Contains the entry size of a element for sections which are constructed
296of a table of fixed-size entries. If the section does not hold a table of
297fixed-size entries, this value is 0.
298.El
299.Pp
300Every executable object must contain a program header. The program header
301contains information necessary in constructing a process image.
302.Bd -literal -offset indent
303typedef struct {
304	Elf32_Word	p_type;
305	Elf32_Off	p_offset;
306	Elf32_Addr	p_vaddr;
307	Elf32_Addr	p_paddr;
308	Elf32_Word	p_filesz;
309	Elf32_Word	p_memsz;
310	Elf32_Word	p_flags;
311	Elf32_Word	p_align;
312} Elf32_Phdr;
313.Ed
314.Pp
315.Bl -tag -width p_offset
316.It Fa p_type
317Contains the segment type indicator. The possible values are:
318.Bl -tag -width PT_DYNAMIC
319.It Dv PT_NULL
320Segment is inactive. The other fields contain undefined values.
321.It Dv PT_LOAD
322Segment is loadable. It is loaded to the address described by
323.Fa p_vaddr .
324If
325.Fa p_memsz
326is greater than
327.Fa p_filesz ,
328the memory range from
329.Po Fa p_vaddr
330+
331.Fa p_filesz Pc
332to
333.Po Fa p_vaddr
334+
335.Fa p_memsz Pc
336is zero-filled when the segment is loaded.
337.Fa p_filesz
338can not be greater than
339.Fa p_memsz .
340Segments of this type are sorted in the header table by
341.Fa p_vaddr
342in ascending order.
343.It Dv PT_DYNAMIC
344Segment contains dynamic linking information.
345.It Dv PT_INTERP
346Segment contains a null-terminated path name to the interpreter. This segment
347may be present only once in a file, and it must appear before any loadable
348segments. This field will most likely contain the ELF dynamic loader:
349.Pa /usr/libexec/ld.so_elf
350.It Dv PT_NOTE
351Segment contains some special information. Format can be e.g. vendor-specific.
352.It Dv PT_SHLIB
353This segment type is reserved but has unspecified semantics. Programs
354which contain a segment of this type do not conform to the ABI, and must
355indicate this by setting the appropriate ABI in the ELF header
356.Dv EI_OSABI
357field.
358.It Dv PT_PHDR
359The values in a program header of this type specify the characteristics
360of the program header table itself. For example, the
361.Fa p_vaddr
362field specifies the program header table location in memory once the
363program is loaded. This field may not occur more than once, may occur only
364if the program header table is part of the file memory image, and must
365come before any loadable segments.
366.El
367.It Fa p_offset
368Contains the byte offset from the beginning of the file to the beginning
369of this segment.
370.It Fa p_vaddr
371Contains the virtual memory address to which this segment is loaded.
372.It Fa p_paddr
373Contains the physical address to which this segment is loaded. This value
374is usually ignored, but may be used while bootstrapping or in embedded
375systems.
376.It Fa p_filesz
377Contains the number of bytes this segment occupies in the file image.
378.It Fa p_memsz
379Contains the number of bytes this segment occupies in the memory image.
380.It Fa p_flags
381Contains the segment flags, which specify the permissions for the segment
382after it has been loaded. The following values or any combination of them
383is acceptable:
384.Bl -tag -width PF_R
385.It Dv PF_R
386Segment can be read.
387.It Dv PF_R
388Segment can be written.
389.It Dv PF_X
390Segment is executable.
391.El
392.It Fa p_align
393Contains the segment alignment. Acceptable values are 0 and 1 for no alignment,
394and integral powers of two.
395.Fa p_vaddr
396should equal
397.Fa p_offset
398modulo
399.Fa p_align .
400.El
401.Sh SEE ALSO
402.Xr as 1 ,
403.Xr gdb 1 ,
404.Xr ld 1 ,
405.Xr ld.elf_so 1 ,
406.Xr execve 2 ,
407.Xr nlist 3 ,
408.Xr a.out 5 ,
409.Xr core 5 ,
410.Xr link 5 ,
411.Xr stab 5
412.Sh HISTORY
413.Pa ELF
414first appeared in
415.At V .
416