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