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