xref: /openbsd-src/share/man/man5/elf.5 (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1.\"	$OpenBSD: elf.5,v 1.15 2007/05/31 19:19:58 jmc Exp $
2.\"Copyright (c) 1999 Jeroen Ruigrok van der Werven
3.\"All rights reserved.
4.\"
5.\"Redistribution and use in source and binary forms, with or without
6.\"modification, are permitted provided that the following conditions
7.\"are met:
8.\"1. Redistributions of source code must retain the above copyright
9.\"   notice, this list of conditions and the following disclaimer.
10.\"2. Redistributions in binary form must reproduce the above copyright
11.\"   notice, this list of conditions and the following disclaimer in the
12.\"   documentation and/or other materials provided with the distribution.
13.\"
14.\"THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15.\"ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16.\"IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17.\"ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18.\"FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19.\"DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20.\"OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21.\"HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22.\"LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23.\"OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24.\"SUCH DAMAGE.
25.\"
26.\"	$FreeBSD: src/share/man/man5/elf.5,v 1.21 2001/10/01 16:09:23 ru Exp $
27.\"
28.Dd $Mdocdate: May 31 2007 $
29.Dt ELF 5
30.Os
31.Sh NAME
32.Nm elf
33.Nd format of ELF executable binary files
34.Sh SYNOPSIS
35.Fd #include <elf_abi.h>
36.Sh DESCRIPTION
37The header file
38.Aq Pa elf_abi.h
39defines the format of ELF executable binary files.
40Amongst these files are
41normal executable files, relocatable object files, core files and shared
42libraries.
43.Pp
44An executable file using the ELF file format consists of an ELF header,
45followed by a program header table or a section header table, or both.
46The ELF header is always at offset zero of the file.
47The program header
48table and the section header table's offset in the file are defined in the
49ELF header.
50The two tables describe the rest of the particularities of
51the file.
52.Pp
53Applications which wish to process ELF binary files for their native
54architecture only should include
55.Aq Pa elf_abi.h
56in their source code.
57These applications should need to refer to
58all the types and structures by their generic names
59.Dq Elf_xxx
60and to the macros by
61.Dq ELF_xxx .
62Applications written this way can be compiled on any architecture,
63regardless of whether the host is 32-bit or 64-bit.
64.Pp
65Should an application need to process ELF files of an unknown
66architecture, then the application needs to explicitly use either
67.Dq Elf32_xxx
68or
69.Dq Elf64_xxx
70type and structure names.
71Likewise, the macros need to be identified by
72.Dq ELF32_xxx
73or
74.Dq ELF64_xxx .
75.Pp
76This header file describes the above mentioned headers as C structures
77and also includes structures for dynamic sections, relocation sections and
78symbol tables.
79.Pp
80The following types are used for 32-bit architectures:
81.Bd -literal -offset indent
82Elf32_Addr	Unsigned program address
83Elf32_Half	Unsigned halfword field
84Elf32_Off	Unsigned file offset
85Elf32_Sword	Signed large integer
86Elf32_Word	Field or unsigned large integer
87.\" Elf32_Size	Unsigned object size
88.Ed
89.Pp
90And the following types are used for 64-bit architectures:
91.Bd -literal -offset indent
92Elf64_Addr	Unsigned program address
93Elf64_Shalf	Signed halfword field
94Elf64_Half	Unsigned halfword field
95Elf64_Off	Unsigned file offset
96Elf64_Sword	Signed large integer
97Elf64_Word	Field or unsigned large integer
98.\" Elf64_Size	Unsigned object size
99Elf64_Xword	Unsigned object size or alignment
100Elf64_Sxword	Signed object size or alignment
101Elf64_Quarter	Unsigned quarterword field
102.Ed
103.Pp
104All data structures that the file format defines follow the
105.Dq natural
106size and alignment guidelines for the relevant class.
107If necessary,
108data structures contain explicit padding to ensure 4-byte alignment
109for 4-byte objects, to force structure sizes to a multiple of 4, etc.
110.Pp
111The ELF header is described by the type Elf32_Ehdr or Elf64_Ehdr:
112.Bd -literal -offset indent
113typedef struct {
114        unsigned char   e_ident[EI_NIDENT];
115        Elf32_Half      e_type;
116        Elf32_Half      e_machine;
117        Elf32_Word      e_version;
118        Elf32_Addr      e_entry;
119        Elf32_Off       e_phoff;
120        Elf32_Off       e_shoff;
121        Elf32_Word      e_flags;
122        Elf32_Half      e_ehsize;
123        Elf32_Half      e_phentsize;
124        Elf32_Half      e_phnum;
125        Elf32_Half      e_shentsize;
126        Elf32_Half      e_shnum;
127        Elf32_Half      e_shstrndx;
128} Elf32_Ehdr;
129.Ed
130.Bd -literal -offset indent
131typedef struct {
132	unsigned char   e_ident[EI_NIDENT];
133	Elf64_Quarter   e_type;
134	Elf64_Quarter   e_machine;
135	Elf64_Half      e_version;
136	Elf64_Addr      e_entry;
137	Elf64_Off       e_phoff;
138	Elf64_Off       e_shoff;
139	Elf64_Half      e_flags;
140	Elf64_Quarter   e_ehsize;
141	Elf64_Quarter   e_phentsize;
142	Elf64_Quarter   e_phnum;
143	Elf64_Quarter   e_shentsize;
144	Elf64_Quarter   e_shnum;
145	Elf64_Quarter   e_shstrndx;
146} Elf64_Ehdr;
147.Ed
148.Pp
149The fields have the following meanings:
150.Bl -tag -width "e_phentsize" -offset indent
151.It Dv e_ident
152This array of bytes specifies to interpret the file,
153independent of the processor or the file's remaining contents.
154Within this array everything is named by macros, which start with
155the prefix
156.Sy EI_
157and may contain values which start with the prefix
158.Sy ELF .
159The following macros are defined:
160.Bl -tag -width "EI_VERSION" \" EI_ABIVERSION
161.It Dv EI_MAG0
162The first byte of the magic number.
163It must be filled with
164.Sy ELFMAG0 .
165.It Dv EI_MAG1
166The second byte of the magic number.
167It must be filled with
168.Sy ELFMAG1 .
169.It Dv EI_MAG2
170The third byte of the magic number.
171It must be filled with
172.Sy ELFMAG2 .
173.It Dv EI_MAG3
174The fourth byte of the magic number.
175It must be filled with
176.Sy ELFMAG3 .
177.It Dv EI_CLASS
178The fifth byte identifies the architecture for this binary:
179.Pp
180.Bl -tag -width "ELFCLASSNONE" -compact
181.It Dv ELFCLASSNONE
182This class is invalid.
183.It Dv ELFCLASS32
184This defines the 32-bit architecture.
185It supports machines with files
186and virtual address spaces up to 4 Gigabytes.
187.It Dv ELFCLASS64
188This defines the 64-bit architecture.
189.El
190.It Dv EI_DATA
191The sixth byte specifies the data encoding of the processor-specific
192data in the file.
193Currently these encodings are supported:
194.Pp
195.Bl -tag -width "ELFDATA2LSB" -compact
196.It Dv ELFDATANONE
197Unknown data format.
198.It Dv ELFDATA2LSB
199Two's complement, little-endian.
200.It Dv ELFDATA2MSB
201Two's complement, big-endian.
202.El
203.It Dv EI_VERSION
204The version number of the ELF specification:
205.Pp
206.Bl -tag -width "EV_CURRENT" -compact
207.It Dv EV_NONE
208Invalid version.
209.It Dv EV_CURRENT
210Current version.
211.El
212.\" .It Dv EI_OSABI
213.\" This byte identifies the operating system
214.\" and ABI to which the object is targeted.
215.\" Some fields in other ELF structures have flags
216.\" and values that have platform specific meanings;
217.\" the interpretation of those fields is determined by the value of this byte.
218.\" The following values are currently defined:
219.\" .Pp
220.\" .Bl -tag -width "ELFOSABI_STANDALONE" -compact
221.\" .It Dv ELFOSABI_SYSV
222.\" UNIX System V ABI.
223.\" .It Dv ELFOSABI_HPUX
224.\" HP-UX operating system ABI.
225.\" .It Dv ELFOSABI_NETBSD
226.\" .Nx
227.\" operating system ABI.
228.\" .It Dv ELFOSABI_LINUX
229.\" GNU/Linux operating system ABI.
230.\" .It Dv ELFOSABI_HURD
231.\" GNU/Hurd operating system ABI.
232.\" .It Dv ELFOSABI_86OPEN
233.\" 86Open Common IA32 ABI.
234.\" .It Dv ELFOSABI_SOLARIS
235.\" Solaris operating system ABI.
236.\" .It Dv ELFOSABI_MONTEREY
237.\" Monterey project ABI.
238.\" .It Dv ELFOSABI_IRIX
239.\" IRIX operating system ABI.
240.\" .It Dv ELFOSABI_FREEBSD
241.\" .Fx
242.\" operating system ABI.
243.\" .It Dv ELFOSABI_TRU64
244.\" TRU64 UNIX operating system ABI.
245.\" .It Dv ELFOSABI_ARM
246.\" ARM architecture ABI.
247.\" .It Dv ELFOSABI_STANDALONE
248.\" Stand-alone (embedded) ABI.
249.\" .El
250.\" .It Dv EI_ABIVERSION
251.\" This byte identifies the version of the ABI
252.\" to which the object is targeted.
253.\" This field is used to distinguish among incompatible versions of an ABI.
254.\" The interpretation of this version number
255.\" is dependent on the ABI identified by the EI_OSABI field.
256.\" Applications conforming to this specification use the value 0.
257.It Dv EI_PAD
258Start of padding.
259These bytes are reserved and set to zero.
260Programs
261which read them should ignore them.
262The value for EI_PAD will change in
263the future if currently unused bytes are given meanings.
264.It Dv EI_BRAND
265Start of architecture identification.
266.It Dv EI_NIDENT
267The size of the e_ident array.
268.El
269.Pp
270.It Dv e_type
271This member of the structure identifies the object file type:
272.Pp
273.Bl -tag -width "ET_NONE" -compact
274.It Dv ET_NONE
275An unknown type.
276.It Dv ET_REL
277A relocatable file.
278.It Dv ET_EXEC
279An executable file.
280.It Dv ET_DYN
281A shared object.
282.It Dv ET_CORE
283A core file.
284.El
285.Pp
286.It Dv e_machine
287This member specifies the required architecture for an individual file:
288.Pp
289.Bl -tag -width "EM_MIPS_RS4_BE" -compact
290.It Dv EM_NONE
291An unknown machine.
292.It Dv EM_M32
293AT&T WE 32100.
294.It Dv EM_SPARC
295Sun Microsystems SPARC.
296.It Dv EM_386
297Intel 80386.
298.It Dv EM_68K
299Motorola 68000.
300.It Dv EM_88K
301Motorola 88000.
302.It Dv EM_486
303Intel 80486.
304.It Dv EM_860
305Intel 80860.
306.It Dv EM_MIPS
307MIPS RS3000 (big-endian only).
308.It Dv EM_MIPS_RS4_BE
309MIPS RS4000 (big-endian only).
310.It Dv EM_SPARC64
311SPARC v9 64-bit (unofficial).
312.It Dv EM_PARISC
313HPPA.
314.It Dv EM_SPARC32PLUS
315SPARC with enhanced instruction set.
316.It Dv EM_PPC
317PowerPC.
318.It Dv EM_ALPHA
319Compaq [DEC] Alpha.
320.It Dv EM_SPARCV9
321SPARC v9 64-bit.
322.It Dv EM_ALPHA_EXP
323Compaq [DEC] Alpha with enhanced instruction set.
324.It Dv EM_VAX
325DEC Vax.
326.El
327.Pp
328.It Dv e_version
329This member identifies the file version:
330.Pp
331.Bl -tag -width "EV_CURRENT" -compact
332.It Dv EV_NONE
333Invalid version.
334.It Dv EV_CURRENT
335Current version.
336.El
337.It Dv e_entry
338This member gives the virtual address to which the system first transfers
339control, thus starting the process.
340If the file has no associated entry
341point, this member holds zero.
342.It Dv e_phoff
343This member holds the program header table's file offset in bytes.
344If
345the file has no program header table, this member holds zero.
346.It Dv e_shoff
347This member holds the section header table's file offset in bytes.
348If the
349file has no section header table this member holds zero.
350.It Dv e_flags
351This member holds processor-specific flags associated with the file.
352Flag names take the form EF_`machine_flag'.
353Currently no flags have been defined.
354.It Dv e_ehsize
355This member holds the ELF header's size in bytes.
356.It Dv e_phentsize
357This member holds the size in bytes of one entry in the file's program header
358table; all entries are the same size.
359.It Dv e_phnum
360This member holds the number of entries in the program header
361table.
362Thus the product of
363.Sy e_phentsize
364and
365.Sy e_phnum
366gives the table's size
367in bytes.
368If a file has no program header,
369.Sy e_phnum
370holds the value zero.
371.It Dv e_shentsize
372This member holds a sections header's size in bytes.
373A section header is one
374entry in the section header table; all entries are the same size.
375.It Dv e_shnum
376This member holds the number of entries in the section header table.
377Thus
378the product of
379.Sy e_shentsize
380and
381.Sy e_shnum
382gives the section header table's size in bytes.
383If a file has no section
384header table,
385.Sy e_shnum
386holds the value of zero.
387.It Dv e_shstrndx
388This member holds the section header table index of the entry associated
389with the section name string table.
390If the file has no section name string
391table, this member holds the value
392.Sy SHN_UNDEF .
393.Bl -tag -width "SHN_LORESERVE"
394.It Dv SHN_UNDEF
395This value marks an undefined, missing, irrelevant, or otherwise meaningless
396section reference.
397For example, a symbol
398.Dq defined
399relative to section number
400.Sy SHN_UNDEF
401is an undefined symbol.
402.It Dv SHN_LORESERVE
403This value specifies the lower bound of the range of reserved indices.
404.It Dv SHN_LOPROC
405This value up to and including
406.Sy SHN_HIPROC
407is reserved for processor-specific semantics.
408.It Dv SHN_HIPROC
409This value down to and including
410.Sy SHN_LOPROC
411is reserved for processor-specific semantics.
412.It Dv SHN_ABS
413This value specifies absolute values for the corresponding reference.
414For
415example, symbols defined relative to section number
416.Sy SHN_ABS
417have absolute values and are not affected by relocation.
418.It Dv SHN_COMMON
419Symbols defined relative to this section are common symbols, such as Fortran
420COMMON or unallocated C external variables.
421.It Dv SHN_HIRESERVE
422This value specifies the upper bound of the range of reserved
423indices between
424.Sy SHN_LORESERVE
425and
426.Sy SHN_HIRESERVE ,
427inclusive; the values do
428not reference the section header table.
429That is, the section header table
430does
431.Em not
432contain entries for the reserved indices.
433.El
434.El
435.Pp
436An executable or shared object file's program header table is an array of
437structures, each describing a segment or other information the system needs
438to prepare the program for execution.
439An object file
440.Em segment
441contains one or more
442.Em sections .
443Program headers are meaningful only for executable and shared object files.
444A file specifies its own program header size with the ELF header's
445.Sy e_phentsize
446and
447.Sy e_phnum
448members.
449As with the ELF executable header, the program header
450also has different versions depending on the architecture:
451.Bd -literal -offset indent
452typedef struct {
453        Elf32_Word      p_type;
454        Elf32_Off       p_offset;
455        Elf32_Addr      p_vaddr;
456        Elf32_Addr      p_paddr;
457        Elf32_Word      p_filesz;
458        Elf32_Word      p_memsz;
459        Elf32_Word      p_flags;
460        Elf32_Word      p_align;
461} Elf32_Phdr;
462.Ed
463.Bd -literal -offset indent
464typedef struct {
465        Elf64_Half      p_type;
466        Elf64_Half      p_flags;
467        Elf64_Off       p_offset;
468        Elf64_Addr      p_vaddr;
469        Elf64_Addr      p_paddr;
470        Elf64_Xword     p_filesz;
471        Elf64_Xword     p_memsz;
472        Elf64_Xword     p_align;
473} Elf64_Phdr;
474.Ed
475.Pp
476The main difference between the 32-bit and the 64-bit program header lies
477only in the location of a
478.Sy p_flags
479member in the total struct.
480.Bl -tag -width "p_offset" -offset indent
481.It Dv p_type
482This member of the Phdr struct tells what kind of segment this array
483element describes or how to interpret the array element's information.
484.Bl -tag -width "PT_DYNAMIC"
485.It Dv PT_NULL
486The array element is unused and the other members' values are undefined.
487This lets the program header have ignored entries.
488.It Dv PT_LOAD
489The array element specifies a loadable segment, described by
490.Sy p_filesz
491and
492.Sy p_memsz .
493The bytes from the file are mapped to the beginning of the memory
494segment.
495If the segment's memory size
496.Pq Sy p_memsz
497is larger than the file size
498.Pq Sy p_filesz ,
499the
500.Dq extra
501bytes are defined to hold the value 0 and to follow the segment's
502initialized area.
503The file size may not be larger than the memory size.
504Loadable segment entries in the program header table appear in ascending
505order, sorted on the
506.Sy p_vaddr
507member.
508.It Dv PT_DYNAMIC
509The array element specifies dynamic linking information.
510.It Dv PT_INTERP
511The array element specifies the location and size of a null-terminated
512path name to invoke as an interpreter.
513This segment type is meaningful
514only for executable files (though it may occur for shared objects).
515However it may not occur more than once in a file.
516If it is present, it must precede any loadable segment entry.
517.It Dv PT_NOTE
518The array element specifies the location and size for auxiliary information.
519.It Dv PT_SHLIB
520This segment type is reserved but has unspecified semantics.
521Programs that
522contain an array element of this type do not conform to the ABI.
523.It Dv PT_PHDR
524The array element, if present, specifies the location and size of the program
525header table itself, both in the file and in the memory image of the program.
526This segment type may not occur more than once in a file.
527Moreover, it may
528only occur if the program header table is part of the memory image of the
529program.
530If it is present, it must precede any loadable segment entry.
531.It Dv PT_LOOS
532This value up to and including
533.Sy PT_HIOS
534is reserverd for operating system-specific semantics.
535.It Dv PT_HIOS
536This value down to and including
537.Sy PT_LOOS
538is reserved for operating system-specific semantics.
539.It Dv PT_LOPROC
540This value up to and including
541.Sy PT_HIPROC
542is reserved for processor-specific semantics.
543.It Dv PT_HIPROC
544This value down to and including
545.Sy PT_LOPROC
546is reserved for processor-specific semantics.
547.El
548.Pp
549.It Dv p_offset
550This member holds the offset from the beginning of the file at which
551the first byte of the segment resides.
552.It Dv p_vaddr
553This member holds the virtual address at which the first byte of the
554segment resides in memory.
555.It Dv p_paddr
556On systems for which physical addressing is relevant, this member is
557reserved for the segment's physical address.
558Under
559.Bx
560this member is
561not used and must be zero.
562.It Dv p_filesz
563This member holds the number of bytes in the file image of the segment.
564It may be zero.
565.It Dv p_memsz
566This member holds the number of bytes in the memory image of the segment.
567It may be zero.
568.It Dv p_flags
569This member holds flags relevant to the segment:
570.Pp
571.Bl -tag -width "PF_X" -compact
572.It Dv PF_X
573An executable segment.
574.It Dv PF_W
575A writable segment.
576.It Dv PF_R
577A readable segment.
578.El
579.Pp
580A text segment commonly has the flags
581.Sy PF_X
582and
583.Sy PF_R .
584A data segment commonly has
585.Sy PF_X ,
586.Sy PF_W
587and
588.Sy PF_R .
589.It Dv p_align
590This member holds the value to which the segments are aligned in memory
591and in the file.
592Loadable process segments must have congruent values for
593.Sy p_vaddr
594and
595.Sy p_offset ,
596modulo the page size.
597Values of zero and one mean no alignment is required.
598Otherwise,
599.Sy p_align
600should be a positive, integral power of two, and
601.Sy p_vaddr
602should equal
603.Sy p_offset ,
604modulo
605.Sy p_align .
606.El
607.Pp
608A file's section header table lets one locate all the file's sections.
609The
610section header table is an array of Elf32_Shdr or Elf64_Shdr structures.
611The
612ELF header's
613.Sy e_shoff
614member gives the byte offset from the beginning of the file to the section
615header table.
616.Sy e_shnum
617holds the number of entries the section header table contains.
618.Sy e_shentsize
619holds the size in bytes of each entry.
620.Pp
621A section header table index is a subscript into this array.
622Some section
623header table indices are reserved.
624An object file does not have sections for
625these special indices:
626.Bl -tag -width "SHN_LORESERVE"
627.It Dv SHN_UNDEF
628This value marks an undefined, missing, irrelevant or otherwise meaningless
629section reference.
630For example, a symbol
631.Dq defined
632relative to section number
633.Sy SHN_UNDEF
634is an undefined symbol.
635.It Dv SHN_LORESERVE
636This value specifies the lower bound of the range of reserved indices.
637.It Dv SHN_LOPROC
638This value up to and including
639.Sy SHN_HIPROC
640is reserved for processor-specific semantics.
641.It Dv SHN_HIPROC
642This value down to and including
643.Sy SHN_LOPROC
644is reserved for processor-specific semantics.
645.It Dv SHN_ABS
646This value specifies the absolute value for the corresponding reference.
647For
648example, a symbol defined relative to section number
649.Sy SHN_ABS
650has an absolute value and is not affected by relocation.
651.It Dv SHN_COMMON
652Symbols defined relative to this section are common symbols, such as FORTRAN
653COMMON or unallocated C external variables.
654.It Dv SHN_HIRESERVE
655This value specifies the upper bound of the range of reserved indices.
656The
657system reserves indices between
658.Sy SHN_LORESERVE
659and
660.Sy SHN_HIRESERVE ,
661inclusive.
662The section header table does not contain entries for the
663reserved indices.
664.El
665.Pp
666The section header has the following structure:
667.Bd -literal -offset indent
668typedef struct {
669	Elf32_Word      sh_name;
670	Elf32_Word      sh_type;
671	Elf32_Word      sh_flags;
672	Elf32_Addr      sh_addr;
673	Elf32_Off       sh_offset;
674	Elf32_Word      sh_size;
675	Elf32_Word      sh_link;
676	Elf32_Word      sh_info;
677	Elf32_Word      sh_addralign;
678	Elf32_Word      sh_entsize;
679} Elf32_Shdr;
680.Ed
681.Bd -literal -offset indent
682typedef struct {
683	Elf64_Half      sh_name;
684	Elf64_Half      sh_type;
685	Elf64_Xword     sh_flags;
686	Elf64_Addr      sh_addr;
687	Elf64_Off       sh_offset;
688	Elf64_Xword     sh_size;
689	Elf64_Half      sh_link;
690	Elf64_Half      sh_info;
691	Elf64_Xword     sh_addralign;
692	Elf64_Xword     sh_entsize;
693} Elf64_Shdr;
694.Ed
695.Bl -tag -width "sh_addralign"
696.It Dv sh_name
697This member specifies the name of the section.
698Its value is an index
699into the section header string table section, giving the location of
700a null-terminated string.
701.It Dv sh_type
702This member categorizes the section's contents and semantics.
703.Bl -tag -width "SHT_PROGBITS"
704.It Dv SHT_NULL
705This value marks the section header as inactive.
706It does not
707have an associated section.
708Other members of the section header
709have undefined values.
710.It Dv SHT_PROGBITS
711This section holds information defined by the program, whose
712format and meaning are determined solely by the program.
713.It Dv SHT_SYMTAB
714This section holds a symbol table.
715Typically,
716.Sy SHT_SYMTAB
717provides symbols for link editing, though it may also be used
718for dynamic linking.
719As a complete symbol table, it may contain
720many symbols unnecessary for dynamic linking.
721An object file can
722also contain a
723.Sy SHN_DYNSYM
724section.
725.It Dv SHT_STRTAB
726This section holds a string table.
727An object file may have multiple
728string table sections.
729.It Dv SHT_RELA
730This section holds relocation entries with explicit addends, such
731as type
732.Sy Elf32_Rela
733for the 32-bit class of object files.
734An object may have multiple
735relocation sections.
736.It Dv SHT_HASH
737This section holds a symbol hash table.
738An object participating in
739dynamic linking must contain a symbol hash table.
740An object file may
741have only one hash table.
742.It Dv SHT_DYNAMIC
743This section holds information for dynamic linking.
744An object file may
745have only one dynamic section.
746.It Dv SHT_NOTE
747This section holds information that marks the file in some way.
748.It Dv SHT_NOBITS
749A section of this type occupies no space in the file but otherwise
750resembles
751.Sy SHN_PROGBITS .
752Although this section contains no bytes, the
753.Sy sh_offset
754member contains the conceptual file offset.
755.It Dv SHT_REL
756This section holds relocation offsets without explicit addends, such
757as type
758.Sy Elf32_Rel
759for the 32-bit class of object files.
760An object file may have multiple
761relocation sections.
762.It Dv SHT_SHLIB
763This section is reserved but has unspecified semantics.
764.It Dv SHT_DYNSYM
765This section holds a minimal set of dynamic linking symbols.
766An
767object file can also contain a
768.Sy SHN_SYMTAB
769section.
770.It Dv SHT_LOPROC
771This value up to and including
772.Sy SHT_HIPROC
773is reserved for processor-specific semantics.
774.It Dv SHT_HIPROC
775This value down to and including
776.Sy SHT_LOPROC
777is reserved for processor-specific semantics.
778.It Dv SHT_LOUSER
779This value specifies the lower bound of the range of indices reserved for
780application programs.
781.It Dv SHT_HIUSER
782This value specifies the upper bound of the range of indices reserved for
783application programs.
784Section types between
785.Sy SHT_LOUSER
786and
787.Sy SHT_HIUSER
788may be used by the application, without conflicting with current or future
789system-defined section types.
790.El
791.Pp
792.It Dv sh_flags
793Sections support one-bit flags that describe miscellaneous attributes.
794If a flag bit is set in
795.Sy sh_flags ,
796the attribute is
797.Dq on
798for the section.
799Otherwise, the attribute is
800.Dq off
801or does not apply.
802Undefined attributes are set to zero.
803.Pp
804.Bl -tag -width "SHF_EXECINSTR" -compact
805.It Dv SHF_WRITE
806This section contains data that should be writable during process
807execution.
808.It Dv SHF_ALLOC
809This section occupies memory during process execution.
810Some control
811sections do not reside in the memory image of an object file.
812This
813attribute is off for those sections.
814.It Dv SHF_EXECINSTR
815This section contains executable machine instructions.
816.It Dv SHF_MASKPROC
817All bits included in this mask are reserved for processor-specific
818semantics.
819.El
820.Pp
821.It Dv sh_addr
822If this section appears in the memory image of a process, this member
823holds the address at which the section's first byte should reside.
824Otherwise, the member contains zero.
825.It Dv sh_offset
826This member's value holds the byte offset from the beginning of the file
827to the first byte in the section.
828One section type,
829.Sy SHT_NOBITS ,
830occupies no space in the file, and its
831.Sy sh_offset
832member locates the conceptual placement in the file.
833.It Dv sh_size
834This member holds the section's size in bytes.
835Unless the section type
836is
837.Sy SHT_NOBITS ,
838the section occupies
839.Sy sh_size
840bytes in the file.
841A section of type
842.Sy SHT_NOBITS
843may have a non-zero size, but it occupies no space in the file.
844.It Dv sh_link
845This member holds a section header table index link, whose interpretation
846depends on the section type.
847.It Dv sh_info
848This member holds extra information, whose interpretation depends on the
849section type.
850.It Dv sh_addralign
851Some sections have address alignment constraints.
852If a section holds a
853doubleword, the system must ensure doubleword alignment for the entire
854section.
855That is, the value of
856.Sy sh_addr
857must be congruent to zero, modulo the value of
858.Sy sh_addralign .
859Only zero and positive integral powers of two are allowed.
860Values of zero
861or one mean the section has no alignment constraints.
862.It Dv sh_entsize
863Some sections hold a table of fixed-sized entries, such as a symbol table.
864For such a section, this member gives the size in bytes for each entry.
865This member contains zero if the section does not hold a table of
866fixed-size entries.
867.El
868.Pp
869Various sections hold program and control information:
870.Bl -tag -width ".shstrtab"
871.It .bss
872This section holds uninitialized data that contributes to the program's
873memory image.
874By definition, the system initializes the data with zeros
875when the program begins to run.
876This section is of type
877.Sy SHT_NOBITS .
878The attribute types are
879.Sy SHF_ALLOC
880and
881.Sy SHF_WRITE .
882.It .comment
883This section holds version control information.
884This section is of type
885.Sy SHT_PROGBITS .
886No attribute types are used.
887.It .ctors
888This section holds initialized pointers to the C++ constructor functions.
889This section is of type
890.Sy SHT_PROGBITS .
891The attribute types are
892.Sy SHF_ALLOC
893and
894.Sy SHF_WRITE .
895.It .data
896This section holds initialized data that contribute to the program's
897memory image.
898This section is of type
899.Sy SHT_PROGBITS .
900The attribute types are
901.Sy SHF_ALLOC
902and
903.Sy SHF_WRITE .
904.It .data1
905This section holds initialized data that contribute to the program's
906memory image.
907This section is of type
908.Sy SHT_PROGBITS .
909The attribute types are
910.Sy SHF_ALLOC
911and
912.Sy SHF_WRITE .
913.It .debug
914This section holds information for symbolic debugging.
915The contents
916are unspecified.
917This section is of type
918.Sy SHT_PROGBITS .
919No attribute types are used.
920.It .dtors
921This section holds initialized pointers to the C++ destructor functions.
922This section is of type
923.Sy SHT_PROGBITS .
924The attribute types are
925.Sy SHF_ALLOC
926and
927.Sy SHF_WRITE .
928.It .dynamic
929This section holds dynamic linking information.
930The section's attributes
931will include the
932.Sy SHF_ALLOC
933bit.
934Whether the
935.Sy SHF_WRITE
936bit is set is processor-specific.
937This section is of type
938.Sy SHT_DYNAMIC .
939See the attributes above.
940.It .dynstr
941This section holds strings needed for dynamic linking, most commonly
942the strings that represent the names associated with symbol table entries.
943This section is of type
944.Sy SHT_STRTAB .
945The attribute type used is
946.Sy SHF_ALLOC .
947.It .dynsym
948This section holds the dynamic linking symbol table.
949This section is of type
950.Sy SHT_DYNSYM .
951The attribute used is
952.Sy SHF_ALLOC .
953.It .fini
954This section holds executable instructions that contribute to the process
955termination code.
956When a program exits normally the system arranges to
957execute the code in this section.
958This section is of type
959.Sy SHT_PROGBITS .
960The attributes used are
961.Sy SHF_ALLOC
962and
963.Sy SHF_EXECINSTR .
964.It .got
965This section holds the global offset table.
966This section is of type
967.Sy SHT_PROGBITS .
968The attributes are processor-specific.
969.It .hash
970This section holds a symbol hash table.
971This section is of type
972.Sy SHT_HASH .
973The attribute used is
974.Sy SHF_ALLOC .
975.It .init
976This section holds executable instructions that contribute to the process
977initialization code.
978When a program starts to run the system arranges to
979execute the code in this section before calling the main program entry point.
980This section is of type
981.Sy SHT_PROGBITS .
982The attributes used are
983.Sy SHF_ALLOC
984and
985.Sy SHF_EXECINSTR .
986.It .interp
987This section holds the pathname of a program interpreter.
988If the file has
989a loadable segment that includes the section, the section's attributes will
990include the
991.Sy SHF_ALLOC
992bit.
993Otherwise, that bit will be off.
994This section is of type
995.Sy SHT_PROGBITS .
996.It .line
997This section holds line number information for symbolic debugging, which
998describes the correspondence between the program source and the machine code.
999The contents are unspecified.
1000This section is of type
1001.Sy SHT_PROGBITS .
1002No attribute types are used.
1003.It .note
1004This section holds information in the
1005.Dq Note Section
1006format described below.
1007This section is of type
1008.Sy SHT_NOTE .
1009No attribute types are used.
1010.Ox
1011native executables usually contain a
1012.Sy .note.openbsd.ident
1013section to identify themselves, for the kernel to bypass any compatibility
1014ELF binary emulation tests when loading the file.
1015.It .plt
1016This section holds the procedure linkage table.
1017This section is of type
1018.Sy SHT_PROGBITS .
1019The attributes are processor-specific.
1020.It .relNAME
1021This section holds relocation information as described below.
1022If the file
1023has a loadable segment that includes relocation, the section's attributes
1024will include the
1025.Sy SHF_ALLOC
1026bit.
1027Otherwise the bit will be off.
1028By convention,
1029.Dq NAME
1030is supplied by the section to which the relocations apply.
1031Thus a relocation
1032section for
1033.Sy .text
1034normally would have the name
1035.Sy .rel.text .
1036This section is of type
1037.Sy SHT_REL .
1038.It .relaNAME
1039This section holds relocation information as described below.
1040If the file
1041has a loadable segment that includes relocation, the section's attributes
1042will include the
1043.Sy SHF_ALLOC
1044bit.
1045Otherwise the bit will be off.
1046By convention,
1047.Dq NAME
1048is supplied by the section to which the relocations apply.
1049Thus a relocation
1050section for
1051.Sy .text
1052normally would have the name
1053.Sy .rela.text .
1054This section is of type
1055.Sy SHT_RELA .
1056.It .rodata
1057This section holds read-only data that typically contributes to a
1058non-writable segment in the process image.
1059This section is of type
1060.Sy SHT_PROGBITS .
1061The attribute used is
1062.Sy SHF_ALLOC .
1063.It .rodata1
1064This section holds read-only data that typically contributes to a
1065non-writable segment in the process image.
1066This section is of type
1067.Sy SHT_PROGBITS .
1068The attribute used is
1069.Sy SHF_ALLOC .
1070.It .shstrtab
1071This section holds section names.
1072This section is of type
1073.Sy SHT_STRTAB .
1074No attribute types are used.
1075.It .strtab
1076This section holds strings, most commonly the strings that represent the
1077names associated with symbol table entries.
1078If the file has a loadable
1079segment that includes the symbol string table, the section's attributes
1080will include the
1081.Sy SHF_ALLOC
1082bit.
1083Otherwise the bit will be off.
1084This section is of type
1085.Sy SHT_STRTAB .
1086.It .symtab
1087This section holds a symbol table.
1088If the file has a loadable segment
1089that includes the symbol table, the section's attributes will include
1090the
1091.Sy SHF_ALLOC
1092bit.
1093Otherwise the bit will be off.
1094This section is of type
1095.Sy SHT_SYMTAB .
1096.It .text
1097This section holds the
1098.Dq text ,
1099or executable instructions, of a program.
1100This section is of type
1101.Sy SHT_PROGBITS .
1102The attributes used are
1103.Sy SHF_ALLOC
1104and
1105.Sy SHF_EXECINSTR .
1106.El
1107.Pp
1108String table sections hold null-terminated character sequences, commonly
1109called strings.
1110The object file uses these strings to represent symbol
1111and section names.
1112One references a string as an index into the string
1113table section.
1114The first byte, which is index zero, is defined to hold
1115a null character.
1116Similarly, a string table's last byte is defined to
1117hold a null character, ensuring null termination for all strings.
1118.Pp
1119An object file's symbol table holds information needed to locate and
1120relocate a program's symbolic definitions and references.
1121A symbol table
1122index is a subscript into this array.
1123.Bd -literal -offset indent
1124typedef struct {
1125	Elf32_Word      st_name;
1126	Elf32_Addr      st_value;
1127	Elf32_Word      st_size;
1128	unsigned char   st_info;
1129	unsigned char   st_other;
1130	Elf32_Half      st_shndx;
1131} Elf32_Sym;
1132.Ed
1133.Bd -literal -offset indent
1134typedef struct {
1135	Elf64_Half      st_name;
1136	Elf_Byte        st_info;
1137	Elf_Byte        st_other;
1138	Elf64_Quarter   st_shndx;
1139	Elf64_Xword     st_value;
1140	Elf64_Xword     st_size;
1141} Elf64_Sym;
1142.Ed
1143.Bl -tag -width "st_value"
1144.It Dv st_name
1145This member holds an index into the object file's symbol string table,
1146which holds character representations of the symbol names.
1147If the value
1148is non-zero, it represents a string table index that gives the symbol
1149name.
1150Otherwise, the symbol table has no name.
1151.It Dv st_value
1152This member gives the value of the associated symbol.
1153.It Dv st_size
1154Many symbols have associated sizes.
1155This member holds zero if the symbol
1156has no size or an unknown size.
1157.It Dv st_info
1158This member specifies the symbol's type and binding attributes:
1159.Bl -tag -width "STT_SECTION"
1160.It Dv STT_NOTYPE
1161The symbol's type is not defined.
1162.It Dv STT_OBJECT
1163The symbol is associated with a data object.
1164.It Dv STT_FUNC
1165The symbol is associated with a function or other executable code.
1166.It Dv STT_SECTION
1167The symbol is associated with a section.
1168Symbol table entries of
1169this type exist primarily for relocation and normally have
1170.Sy STB_LOCAL
1171bindings.
1172.It Dv STT_FILE
1173By convention, the symbol's name gives the name of the source file
1174associated with the object file.
1175A file symbol has
1176.Sy STB_LOCAL
1177bindings, its section index is
1178.Sy SHN_ABS ,
1179and it precedes the other
1180.Sy STB_LOCAL
1181symbols of the file, if it is present.
1182.It Dv STT_LOPROC
1183This value up to and including
1184.Sy STT_HIPROC
1185is reserved for processor-specific semantics.
1186.It Dv STT_HIPROC
1187This value down to and including
1188.Sy STT_LOPROC
1189is reserved for processor-specific semantics.
1190.El
1191.Bl -tag -width "STB_GLOBAL"
1192.It Dv STB_LOCAL
1193Local symbols are not visible outside the object file containing their
1194definition.
1195Local symbols of the same name may exist in multiple files
1196without interfering with each other.
1197.It Dv STB_GLOBAL
1198Global symbols are visible to all object files being combined.
1199One file's
1200definition of a global symbol will satisfy another file's undefined
1201reference to the same symbol.
1202.It Dv STB_WEAK
1203Weak symbols resemble global symbols, but their definitions have lower
1204precedence.
1205.It Dv STB_LOPROC
1206This value up to and including
1207.Sy STB_HIPROC
1208is reserved for processor-specific semantics.
1209.It Dv STB_HIPROC
1210This value down to and including
1211.Sy STB_LOPROC
1212is reserved for processor-specific semantics.
1213.Pp
1214There are macros for packing and unpacking the binding and type fields:
1215.Pp
1216.Bl -tag -width "ELF32_ST_INFO(bind, type)" -compact
1217.It Xo
1218.Fn ELF32_ST_BIND info
1219.Xc
1220or
1221.Fn ELF64_ST_BIND info
1222extract a binding from an st_info value.
1223.It Xo
1224.Fn ELF64_ST_TYPE info
1225.Xc
1226or
1227.Fn ELF32_ST_TYPE info
1228extract a type from an st_info value.
1229.It Xo
1230.Fn ELF32_ST_INFO bind type
1231.Xc
1232or
1233.Fn ELF64_ST_INFO bind type
1234convert a binding and a type into an st_info value.
1235.El
1236.El
1237.Pp
1238.It Dv st_other
1239This member currently holds zero and has no defined meaning.
1240.It Dv st_shndx
1241Every symbol table entry is
1242.Dq defined
1243in relation to some section.
1244This member holds the relevant section
1245header table index.
1246.El
1247.Pp
1248Relocation is the process of connecting symbolic references with
1249symbolic definitions.
1250Relocatable files must have information that
1251describes how to modify their section contents, thus allowing executable
1252and shared object files to hold the right information for a process'
1253program image.
1254Relocation entries are these data.
1255.Pp
1256Relocation structures that do not need an addend:
1257.Bd -literal -offset indent
1258typedef struct {
1259	Elf32_Addr      r_offset;
1260	Elf32_Word      r_info;
1261} Elf32_Rel;
1262.Ed
1263.Bd -literal -offset indent
1264typedef struct {
1265	Elf64_Xword     r_offset;
1266	Elf64_Xword     r_info;
1267} Elf64_Rel;
1268.Ed
1269.Pp
1270Relocation structures that need an addend:
1271.Bd -literal -offset indent
1272typedef struct {
1273	Elf32_Addr      r_offset;
1274	Elf32_Word      r_info;
1275	Elf32_Sword     r_addend;
1276} Elf32_Rela;
1277.Ed
1278.Bd -literal -offset indent
1279typedef struct {
1280	Elf64_Xword     r_offset;
1281	Elf64_Xword     r_info;
1282	Elf64_Sxword    r_addend;
1283} Elf64_Rela;
1284.Ed
1285.Bl -tag -width "r_offset"
1286.It Dv r_offset
1287This member gives the location at which to apply the relocation action.
1288For a relocatable file, the value is the byte offset from the beginning
1289of the section to the storage unit affected by the relocation.
1290For an
1291executable file or shared object, the value is the virtual address of
1292the storage unit affected by the relocation.
1293.It Dv r_info
1294This member gives both the symbol table index with respect to which the
1295relocation must be made and the type of relocation to apply.
1296Relocation
1297types are processor-specific.
1298When the text refers to a relocation
1299entry's relocation type or symbol table index, it means the result of
1300applying
1301.Sy ELF_[32|64]_R_TYPE
1302or
1303.Sy ELF[32|64]_R_SYM ,
1304respectively, to the entry's
1305.Sy r_info
1306member.
1307.It Dv r_addend
1308This member specifies a constant addend used to compute the value to be
1309stored into the relocatable field.
1310.El
1311.Sh SEE ALSO
1312.Xr as 1 ,
1313.Xr gdb 1 ,
1314.Xr ld 1 ,
1315.Xr objdump 1 ,
1316.Xr execve 2 ,
1317.Xr core 5
1318.Rs
1319.%A Hewlett-Packard
1320.%B Elf-64 Object File Format
1321.Re
1322.Rs
1323.%A Santa Cruz Operation
1324.%B System V Application Binary Interface
1325.Re
1326.Rs
1327.%A Unix System Laboratories
1328.%T Object Files
1329.%B "Executable and Linking Format (ELF)"
1330.Re
1331.Sh HISTORY
1332.Ox
1333ELF support first appeared in
1334.Ox 1.2 ,
1335although not all supported platforms use it as the native
1336binary file format.
1337ELF in itself first appeared in
1338.At V .
1339The ELF format is an adopted standard.
1340.Sh AUTHORS
1341This manual page was written by
1342.An Jeroen Ruigrok van der Werven
1343.Aq asmodai@FreeBSD.org
1344with inspiration from BSDi's
1345.Bsx
1346.Nm elf
1347manpage.
1348