xref: /freebsd/share/man/man5/elf.5 (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
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 25, 2022
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.Bd -literal -offset indent
138typedef struct {
139	unsigned char   e_ident[EI_NIDENT];
140	Elf64_Half      e_type;
141	Elf64_Half      e_machine;
142	Elf64_Word      e_version;
143	Elf64_Addr      e_entry;
144	Elf64_Off       e_phoff;
145	Elf64_Off       e_shoff;
146	Elf64_Word      e_flags;
147	Elf64_Half      e_ehsize;
148	Elf64_Half      e_phentsize;
149	Elf64_Half      e_phnum;
150	Elf64_Half      e_shentsize;
151	Elf64_Half      e_shnum;
152	Elf64_Half      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
235.Nx
236operating system ABI.
237.It Dv ELFOSABI_LINUX
238GNU/Linux operating system ABI.
239.It Dv ELFOSABI_HURD
240GNU/Hurd operating system ABI.
241.It Dv ELFOSABI_86OPEN
24286Open Common IA32 ABI.
243.It Dv ELFOSABI_SOLARIS
244Solaris operating system ABI.
245.It Dv ELFOSABI_MONTEREY
246Monterey project ABI.
247.It Dv ELFOSABI_IRIX
248IRIX operating system ABI.
249.It Dv ELFOSABI_FREEBSD
250.Fx
251operating system ABI.
252.It Dv ELFOSABI_TRU64
253TRU64 UNIX operating system ABI.
254.It Dv ELFOSABI_ARM
255ARM architecture ABI.
256.It Dv ELFOSABI_STANDALONE
257Standalone (embedded) ABI.
258.El
259.It Dv EI_ABIVERSION
260This byte identifies the version of the ABI
261to which the object is targeted.
262This field is used to distinguish among incompatible versions of an ABI.
263The interpretation of this version number
264is dependent on the ABI identified by the EI_OSABI field.
265Applications conforming to this specification use the value 0.
266.It Dv EI_PAD
267Start of padding.
268These bytes are reserved and set to zero.
269Programs
270which read them should ignore them.
271The value for EI_PAD will change in
272the future if currently unused bytes are given meanings.
273.It Dv EI_BRAND
274Start of architecture identification.
275.It Dv EI_NIDENT
276The size of the e_ident array.
277.El
278.Pp
279.It Dv e_type
280This member of the structure identifies the object file type:
281.Pp
282.Bl -tag -width "ET_NONE" -compact
283.It Dv ET_NONE
284An unknown type.
285.It Dv ET_REL
286A relocatable file.
287.It Dv ET_EXEC
288An executable file.
289.It Dv ET_DYN
290A shared object.
291.It Dv ET_CORE
292A core file.
293.El
294.Pp
295.It Dv e_machine
296This member specifies the required architecture for an individual file:
297.Pp
298.Bl -tag -width "EM_MIPS_RS4_BE" -compact
299.It Dv EM_NONE
300An unknown machine.
301.It Dv EM_M32
302AT&T WE 32100.
303.It Dv EM_SPARC
304Sun Microsystems SPARC.
305.It Dv EM_386
306Intel 80386.
307.It Dv EM_68K
308Motorola 68000.
309.It Dv EM_88K
310Motorola 88000.
311.It Dv EM_486
312Intel 80486.
313.It Dv EM_860
314Intel 80860.
315.It Dv EM_MIPS
316MIPS RS3000 (big-endian only).
317.It Dv EM_MIPS_RS4_BE
318MIPS RS4000 (big-endian only).
319.It Dv EM_SPARC64
320SPARC v9 64-bit unofficial.
321.It Dv EM_PARISC
322HPPA.
323.It Dv EM_PPC
324PowerPC.
325.It Dv EM_ALPHA
326Compaq [DEC] Alpha.
327.El
328.Pp
329.It Dv e_version
330This member identifies the file version:
331.Pp
332.Bl -tag -width "EV_CURRENT" -compact
333.It Dv EV_NONE
334Invalid version
335.It Dv EV_CURRENT
336Current version
337.El
338.It Dv e_entry
339This member gives the virtual address to which the system first transfers
340control, thus starting the process.
341If the file has no associated entry
342point, this member holds zero.
343.It Dv e_phoff
344This member holds the program header table's file offset in bytes.
345If
346the file has no program header table, this member holds zero.
347.It Dv e_shoff
348This member holds the section header table's file offset in bytes.
349If the
350file has no section header table this member holds zero.
351.It Dv e_flags
352This member holds processor-specific flags associated with the file.
353Flag
354names take the form EF_`machine_flag'.
355Currently no flags have been defined.
356.It Dv e_ehsize
357This member holds the ELF header's size in bytes.
358.It Dv e_phentsize
359This member holds the size in bytes of one entry in the file's program header
360table; all entries are the same size.
361.It Dv e_phnum
362This member holds the number of entries in the program header
363table.
364If the file is using extended program header numbering, then the
365.Sy e_phnum
366member will contain the value
367.Dv PN_XNUM
368and the actual number of program header table entries will be stored
369in the
370.Sy sh_info
371member of the section header at index
372.Dv SHN_UNDEF .
373The product of
374.Sy e_phentsize
375and the number of program header table entries gives the program
376header table's size in bytes.
377If a file has no program header,
378.Sy e_phnum
379holds the value zero.
380.It Dv e_shentsize
381This member holds a sections header's size in bytes.
382A section header is one
383entry in the section header table; all entries are the same size.
384.It Dv e_shnum
385This member holds the number of entries in the section header table.
386If the file is using extended section numbering, then the
387.Sy e_shnum
388member will be zero and the actual section number will be stored in the
389.Sy sh_size
390member of the section header at index
391.Dv SHN_UNDEF .
392If a file has no section header table, both the
393.Sy e_shnum
394and the
395.Sy e_shoff
396fields of the ELF header will be zero.
397The product of
398.Sy e_shentsize
399and the number of sections in the file gives the section header
400table's size in bytes.
401.It Dv e_shstrndx
402This member holds the section header table index of the entry associated
403with the section name string table.
404If extended section numbering is being used, this field will hold the
405value
406.Sy SHN_XINDEX ,
407and the actual section header table index will be present in the
408.Sy sh_link
409field of the section header entry at index
410.Dv SHN_UNDEF .
411If the file has no section name string
412table, this member holds the value
413.Sy SHN_UNDEF .
414.El
415.Pp
416An executable or shared object file's program header table is an array of
417structures, each describing a segment or other information the system needs
418to prepare the program for execution.
419An object file
420.Em segment
421contains one or more
422.Em sections .
423Program headers are meaningful only for executable and shared object files.
424A file specifies its own program header size with the ELF header's
425.Sy e_phentsize
426and
427.Sy e_phnum
428members.
429As with the Elf executable header, the program header
430also has different versions depending on the architecture:
431.Bd -literal -offset indent
432typedef struct {
433        Elf32_Word      p_type;
434        Elf32_Off       p_offset;
435        Elf32_Addr      p_vaddr;
436        Elf32_Addr      p_paddr;
437        Elf32_Word      p_filesz;
438        Elf32_Word      p_memsz;
439        Elf32_Word      p_flags;
440        Elf32_Word      p_align;
441} Elf32_Phdr;
442.Ed
443.Bd -literal -offset indent
444typedef struct {
445        Elf64_Word      p_type;
446        Elf64_Word      p_flags;
447        Elf64_Off       p_offset;
448        Elf64_Addr      p_vaddr;
449        Elf64_Addr      p_paddr;
450        Elf64_Xword     p_filesz;
451        Elf64_Xword     p_memsz;
452        Elf64_Xword     p_align;
453} Elf64_Phdr;
454.Ed
455.Pp
456The main difference between the 32-bit and the 64-bit program header lies
457only in the location of a
458.Sy p_flags
459member in the total struct.
460.Pp
461.Bl -tag -width "p_offset" -compact -offset indent
462.It Dv p_type
463This member of the Phdr struct tells what kind of segment this array
464element describes or how to interpret the array element's information.
465.Pp
466.Bl -tag -width "PT_DYNAMIC" -compact
467.It Dv PT_NULL
468The array element is unused and the other members' values are undefined.
469This lets the program header have ignored entries.
470.It Dv PT_LOAD
471The array element specifies a loadable segment, described by
472.Sy p_filesz
473and
474.Sy p_memsz .
475The bytes from the file are mapped to the beginning of the memory
476segment.
477If the segment's memory size
478.Pq Sy p_memsz
479is larger than the file size
480.Pq Sy p_filesz ,
481the
482.Dq extra
483bytes are defined to hold the value 0 and to follow the segment's
484initialized area.
485The file size may not be larger than the memory size.
486Loadable segment entries in the program header table appear in ascending
487order, sorted on the
488.Sy p_vaddr
489member.
490.It Dv PT_DYNAMIC
491The array element specifies dynamic linking information.
492.It Dv PT_INTERP
493The array element specifies the location and size of a null-terminated
494path name to invoke as an interpreter.
495This segment type is meaningful
496only for executable files (though it may occur for shared objects).
497However
498it may not occur more than once in a file.
499If it is present it must precede
500any loadable segment entry.
501.It Dv PT_NOTE
502The array element specifies the location and size for auxiliary information.
503.It Dv PT_SHLIB
504This segment type is reserved but has unspecified semantics.
505Programs that
506contain an array element of this type do not conform to the ABI.
507.It Dv PT_PHDR
508The array element, if present, specifies the location and size of the program
509header table itself, both in the file and in the memory image of the program.
510This segment type may not occur more than once in a file.
511Moreover, it may
512only occur if the program header table is part of the memory image of the
513program.
514If it is present it must precede any loadable segment entry.
515.It Dv PT_LOPROC
516This value up to and including
517.Sy PT_HIPROC
518are reserved for processor-specific semantics.
519.It Dv PT_HIPROC
520This value down to and including
521.Sy PT_LOPROC
522are reserved for processor-specific semantics.
523.El
524.Pp
525.It Dv p_offset
526This member holds the offset from the beginning of the file at which
527the first byte of the segment resides.
528.It Dv p_vaddr
529This member holds the virtual address at which the first byte of the
530segment resides in memory.
531.It Dv p_paddr
532On systems for which physical addressing is relevant, this member is
533reserved for the segment's physical address.
534Under
535.Bx
536this member is
537not used and must be zero.
538.It Dv p_filesz
539This member holds the number of bytes in the file image of the segment.
540It may be zero.
541.It Dv p_memsz
542This member holds the number of bytes in the memory image of the segment.
543It may be zero.
544.It Dv p_flags
545This member holds flags relevant to the segment:
546.Pp
547.Bl -tag -width "PF_X" -compact
548.It Dv PF_X
549An executable segment.
550.It Dv PF_W
551A writable segment.
552.It Dv PF_R
553A readable segment.
554.El
555.Pp
556A text segment commonly has the flags
557.Sy PF_X
558and
559.Sy PF_R .
560A data segment commonly has
561.Sy PF_X ,
562.Sy PF_W
563and
564.Sy PF_R .
565.It Dv p_align
566This member holds the value to which the segments are aligned in memory
567and in the file.
568Loadable process segments must have congruent values for
569.Sy p_vaddr
570and
571.Sy p_offset ,
572modulo the page size.
573Values of zero and one mean no alignment is required.
574Otherwise,
575.Sy p_align
576should be a positive, integral power of two, and
577.Sy p_vaddr
578should equal
579.Sy p_offset ,
580modulo
581.Sy p_align .
582.El
583.Pp
584An file's section header table lets one locate all the file's sections.
585The
586section header table is an array of Elf32_Shdr or Elf64_Shdr structures.
587The
588ELF header's
589.Sy e_shoff
590member gives the byte offset from the beginning of the file to the section
591header table.
592.Sy e_shnum
593holds the number of entries the section header table contains.
594.Sy e_shentsize
595holds the size in bytes of each entry.
596.Pp
597A section header table index is a subscript into this array.
598Some section
599header table indices are reserved.
600An object file does not have sections for
601these special indices:
602.Pp
603.Bl -tag -width "SHN_LORESERVE" -compact
604.It Dv SHN_UNDEF
605This value marks an undefined, missing, irrelevant, or otherwise meaningless
606section reference.
607For example, a symbol
608.Dq defined
609relative to section number
610.Sy SHN_UNDEF
611is an undefined symbol.
612.It Dv SHN_LORESERVE
613This value specifies the lower bound of the range of reserved indices.
614.It Dv SHN_LOPROC
615This value up to and including
616.Sy SHN_HIPROC
617are reserved for processor-specific semantics.
618.It Dv SHN_HIPROC
619This value down to and including
620.Sy SHN_LOPROC
621are reserved for processor-specific semantics.
622.It Dv SHN_ABS
623This value specifies absolute values for the corresponding reference.
624For
625example, symbols defined relative to section number
626.Sy SHN_ABS
627have absolute values and are not affected by relocation.
628.It Dv SHN_COMMON
629Symbols defined relative to this section are common symbols, such as FORTRAN
630COMMON or unallocated C external variables.
631.It Dv SHN_HIRESERVE
632This value specifies the upper bound of the range of reserved indices.
633The
634system reserves indices between
635.Sy SHN_LORESERVE
636and
637.Sy SHN_HIRESERVE ,
638inclusive.
639The section header table does not contain entries for the
640reserved indices.
641.El
642.Pp
643The section header has the following structure:
644.Bd -literal -offset indent
645typedef struct {
646	Elf32_Word      sh_name;
647	Elf32_Word      sh_type;
648	Elf32_Word      sh_flags;
649	Elf32_Addr      sh_addr;
650	Elf32_Off       sh_offset;
651	Elf32_Word      sh_size;
652	Elf32_Word      sh_link;
653	Elf32_Word      sh_info;
654	Elf32_Word      sh_addralign;
655	Elf32_Word      sh_entsize;
656} Elf32_Shdr;
657.Ed
658.Bd -literal -offset indent
659typedef struct {
660	Elf64_Word      sh_name;
661	Elf64_Word      sh_type;
662	Elf64_Xword     sh_flags;
663	Elf64_Addr      sh_addr;
664	Elf64_Off       sh_offset;
665	Elf64_Xword     sh_size;
666	Elf64_Word      sh_link;
667	Elf64_Word      sh_info;
668	Elf64_Xword     sh_addralign;
669	Elf64_Xword     sh_entsize;
670} Elf64_Shdr;
671.Ed
672.Pp
673.Bl -tag -width "sh_addralign" -compact
674.It Dv sh_name
675This member specifies the name of the section.
676Its value is an index
677into the section header string table section, giving the location of
678a null-terminated string.
679.It Dv sh_type
680This member categorizes the section's contents and semantics.
681.Pp
682.Bl -tag -width "SHT_PROGBITS" -compact
683.It Dv SHT_NULL
684This value marks the section header as inactive.
685It does not
686have an associated section.
687Other members of the section header
688have undefined values.
689.It Dv SHT_PROGBITS
690The section holds information defined by the program, whose
691format and meaning are determined solely by the program.
692.It Dv SHT_SYMTAB
693This section holds a symbol table.
694Typically,
695.Sy SHT_SYMTAB
696provides symbols for link editing, though it may also be used
697for dynamic linking.
698As a complete symbol table, it may contain
699many symbols unnecessary for dynamic linking.
700An object file can
701also contain a
702.Sy SHN_DYNSYM
703section.
704.It Dv SHT_STRTAB
705This section holds a string table.
706An object file may have multiple
707string table sections.
708.It Dv SHT_RELA
709This section holds relocation entries with explicit addends, such
710as type
711.Sy Elf32_Rela
712for the 32-bit class of object files.
713An object may have multiple
714relocation sections.
715.It Dv SHT_HASH
716This section holds a symbol hash table.
717All object participating in
718dynamic linking must contain a symbol hash table.
719An object file may
720have only one hash table.
721.It Dv SHT_DYNAMIC
722This section holds information for dynamic linking.
723An object file may
724have only one dynamic section.
725.It Dv SHT_NOTE
726This section holds information that marks the file in some way.
727.It Dv SHT_NOBITS
728A section of this type occupies no space in the file but otherwise
729resembles
730.Sy SHN_PROGBITS .
731Although this section contains no bytes, the
732.Sy sh_offset
733member contains the conceptual file offset.
734.It Dv SHT_REL
735This section holds relocation offsets without explicit addends, such
736as type
737.Sy Elf32_Rel
738for the 32-bit class of object files.
739An object file may have multiple
740relocation sections.
741.It Dv SHT_SHLIB
742This section is reserved but has unspecified semantics.
743.It Dv SHT_DYNSYM
744This section holds a minimal set of dynamic linking symbols.
745An
746object file can also contain a
747.Sy SHN_SYMTAB
748section.
749.It Dv SHT_LOPROC
750This value up to and including
751.Sy SHT_HIPROC
752are reserved for processor-specific semantics.
753.It Dv SHT_HIPROC
754This value down to and including
755.Sy SHT_LOPROC
756are reserved for processor-specific semantics.
757.It Dv SHT_LOUSER
758This value specifies the lower bound of the range of indices reserved for
759application programs.
760.It Dv SHT_HIUSER
761This value specifies the upper bound of the range of indices reserved for
762application programs.
763Section types between
764.Sy SHT_LOUSER
765and
766.Sy SHT_HIUSER
767may be used by the application, without conflicting with current or future
768system-defined section types.
769.El
770.Pp
771.It Dv sh_flags
772Sections support one-bit flags that describe miscellaneous attributes.
773If a flag bit is set in
774.Sy sh_flags ,
775the attribute is
776.Dq on
777for the section.
778Otherwise, the attribute is
779.Dq off
780or does not apply.
781Undefined attributes are set to zero.
782.Pp
783.Bl -tag -width "SHF_EXECINSTR" -compact
784.It Dv SHF_WRITE
785This section contains data that should be writable during process
786execution.
787.It Dv SHF_ALLOC
788The section occupies memory during process execution.
789Some control
790sections do not reside in the memory image of an object file.
791This
792attribute is off for those sections.
793.It Dv SHF_EXECINSTR
794The section contains executable machine instructions.
795.It Dv SHF_MASKPROC
796All bits included in this mask are reserved for processor-specific
797semantics.
798.It Dv SHF_COMPRESSED
799The section data is compressed.
800.El
801.Pp
802.It Dv sh_addr
803If the section will appear in the memory image of a process, this member
804holds the address at which the section's first byte should reside.
805Otherwise, the member contains zero.
806.It Dv sh_offset
807This member's value holds the byte offset from the beginning of the file
808to the first byte in the section.
809One section type,
810.Sy SHT_NOBITS ,
811occupies no space in the file, and its
812.Sy sh_offset
813member locates the conceptual placement in the file.
814.It Dv sh_size
815This member holds the section's size in bytes.
816Unless the section type
817is
818.Sy SHT_NOBITS ,
819the section occupies
820.Sy sh_size
821bytes in the file.
822A section of type
823.Sy SHT_NOBITS
824may have a non-zero size, but it occupies no space in the file.
825.It Dv sh_link
826This member holds a section header table index link, whose interpretation
827depends on the section type.
828.It Dv sh_info
829This member holds extra information, whose interpretation depends on the
830section type.
831.It Dv sh_addralign
832Some sections have address alignment constraints.
833If a section holds a
834doubleword, the system must ensure doubleword alignment for the entire
835section.
836That is, the value of
837.Sy sh_addr
838must be congruent to zero, modulo the value of
839.Sy sh_addralign .
840Only zero and positive integral powers of two are allowed.
841Values of zero
842or one mean the section has no alignment constraints.
843.It Dv sh_entsize
844Some sections hold a table of fixed-sized entries, such as a symbol table.
845For such a section, this member gives the size in bytes for each entry.
846This member contains zero if the section does not hold a table of
847fixed-size entries.
848.El
849.Pp
850Various sections hold program and control information:
851.Bl -tag -width ".shstrtab" -compact
852.It .bss
853(Block Started by Symbol)
854This section holds uninitialized data that contributes to the program's
855memory image.
856By definition, the system initializes the data with zeros
857when the program begins to run.
858This section is of type
859.Sy SHT_NOBITS .
860The attributes types are
861.Sy SHF_ALLOC
862and
863.Sy SHF_WRITE .
864.It .comment
865This section holds version control information.
866This section is of type
867.Sy SHT_PROGBITS .
868No attribute types are used.
869.It .data
870This section holds initialized data that contribute to the program's
871memory image.
872This section is of type
873.Sy SHT_PROGBITS .
874The attribute types are
875.Sy SHF_ALLOC
876and
877.Sy SHF_WRITE .
878.It .data1
879This section holds initialized data that contribute to the program's
880memory image.
881This section is of type
882.Sy SHT_PROGBITS .
883The attribute types are
884.Sy SHF_ALLOC
885and
886.Sy SHF_WRITE .
887.It .debug
888This section holds information for symbolic debugging.
889The contents
890are unspecified.
891This section is of type
892.Sy SHT_PROGBITS .
893No attribute types are used.
894.It .dynamic
895This section holds dynamic linking information.
896The section's attributes
897will include the
898.Sy SHF_ALLOC
899bit.
900Whether the
901.Sy SHF_WRITE
902bit is set is processor-specific.
903This section is of type
904.Sy SHT_DYNAMIC .
905See the attributes above.
906.It .dynstr
907This section holds strings needed for dynamic linking, most commonly
908the strings that represent the names associated with symbol table entries.
909This section is of type
910.Sy SHT_STRTAB .
911The attribute type used is
912.Sy SHF_ALLOC .
913.It .dynsym
914This section holds the dynamic linking symbol table.
915This section is of type
916.Sy SHT_DYNSYM .
917The attribute used is
918.Sy SHF_ALLOC .
919.It .fini
920This section holds executable instructions that contribute to the process
921termination code.
922When a program exits normally the system arranges to
923execute the code in this section.
924This section is of type
925.Sy SHT_PROGBITS .
926The attributes used are
927.Sy SHF_ALLOC
928and
929.Sy SHF_EXECINSTR .
930.It .got
931This section holds the global offset table.
932This section is of type
933.Sy SHT_PROGBITS .
934The attributes are processor-specific.
935.It .hash
936This section holds a symbol hash table.
937This section is of type
938.Sy SHT_HASH .
939The attribute used is
940.Sy SHF_ALLOC .
941.It .init
942This section holds executable instructions that contribute to the process
943initialization code.
944When a program starts to run the system arranges to
945execute the code in this section before calling the main program entry point.
946This section is of type
947.Sy SHT_PROGBITS .
948The attributes used are
949.Sy SHF_ALLOC
950and
951.Sy SHF_EXECINSTR .
952.It .interp
953This section holds the pathname of a program interpreter.
954If the file has
955a loadable segment that includes the section, the section's attributes will
956include the
957.Sy SHF_ALLOC
958bit.
959Otherwise, that bit will be off.
960This section is of type
961.Sy SHT_PROGBITS .
962.It .line
963This section holds line number information for symbolic debugging, which
964describes the correspondence between the program source and the machine code.
965The contents are unspecified.
966This section is of type
967.Sy SHT_PROGBITS .
968No attribute types are used.
969.It .note
970This section holds information in the
971.Dq Note Section
972format described below.
973This section is of type
974.Sy SHT_NOTE .
975No attribute types are used.
976.It .plt
977This section holds the procedure linkage table.
978This section is of type
979.Sy SHT_PROGBITS .
980The attributes are processor-specific.
981.It .relNAME
982This section holds relocation information as described below.
983If the file
984has a loadable segment that includes relocation, the section's attributes
985will include the
986.Sy SHF_ALLOC
987bit.
988Otherwise the bit will be off.
989By convention,
990.Dq NAME
991is supplied by the section to which the relocations apply.
992Thus a relocation
993section for
994.Sy .text
995normally would have the name
996.Sy .rel.text .
997This section is of type
998.Sy SHT_REL .
999.It .relaNAME
1000This section holds relocation information as described below.
1001If the file
1002has a loadable segment that includes relocation, the section's attributes
1003will include the
1004.Sy SHF_ALLOC
1005bit.
1006Otherwise the bit will be off.
1007By convention,
1008.Dq NAME
1009is supplied by the section to which the relocations apply.
1010Thus a relocation
1011section for
1012.Sy .text
1013normally would have the name
1014.Sy .rela.text .
1015This section is of type
1016.Sy SHT_RELA .
1017.It .rodata
1018This section holds read-only data that typically contributes to a
1019non-writable segment in the process image.
1020This section is of type
1021.Sy SHT_PROGBITS .
1022The attribute used is
1023.Sy SHF_ALLOC .
1024.It .rodata1
1025This section hold read-only data that typically contributes to a
1026non-writable segment in the process image.
1027This section is of type
1028.Sy SHT_PROGBITS .
1029The attribute used is
1030.Sy SHF_ALLOC .
1031.It .shstrtab
1032This section holds section names.
1033This section is of type
1034.Sy SHT_STRTAB .
1035No attribute types are used.
1036.It .strtab
1037This section holds strings, most commonly the strings that represent the
1038names associated with symbol table entries.
1039If the file has a loadable
1040segment that includes the symbol string table, the section's attributes
1041will include the
1042.Sy SHF_ALLOC
1043bit.
1044Otherwise the bit will be off.
1045This section is of type
1046.Sy SHT_STRTAB .
1047.It .symtab
1048This section holds a symbol table.
1049If the file has a loadable segment
1050that includes the symbol table, the section's attributes will include
1051the
1052.Sy SHF_ALLOC
1053bit.
1054Otherwise the bit will be off.
1055This section is of type
1056.Sy SHT_SYMTAB .
1057.It .text
1058This section holds the
1059.Dq text ,
1060or executable instructions, of a program.
1061This section is of type
1062.Sy SHT_PROGBITS .
1063The attributes used are
1064.Sy SHF_ALLOC
1065and
1066.Sy SHF_EXECINSTR .
1067.It .jcr
1068This section holds information about Java classes that must
1069be registered.
1070.It .eh_frame
1071This section holds information used for C++ exception-handling.
1072.El
1073.Pp
1074A section with the
1075.Dv SHF_COMPRESSED
1076flag set contains a compressed copy of the section data.
1077Compressed section data begins with an
1078.Vt Elf64_Chdr
1079or
1080.Vt Elf32_Chdr structure
1081which encodes the compression algorithm and some characteristics of the
1082uncompressed data.
1083.Bd -literal -offset indent
1084typedef struct {
1085	Elf32_Word    ch_type;
1086	Elf32_Word    ch_size;
1087	Elf32_Word    ch_addralign;
1088} Elf32_Chdr;
1089.Ed
1090.Bd -literal -offset indent
1091typedef struct {
1092	Elf64_Word    ch_type;
1093	Elf64_Word    ch_reserved;
1094	Elf64_Xword   ch_size;
1095	Elf64_Xword   ch_addralign;
1096} Elf64_Chdr;
1097.Ed
1098.Pp
1099.Bl -tag -width "ch_addralign" -compact
1100.It Dv ch_type
1101The compression algorithm used.
1102A value of
1103.Dv ELFCOMPRESS_ZLIB
1104indicates that the data is compressed using
1105.Xr zlib 3 .
1106A value of
1107.Dv ELFCOMPRESS_ZSTD
1108indicates that the data is compressed using
1109Zstandard.
1110.It Dv ch_size
1111The size, in bytes, of the uncompressed section data.
1112This corresponds to the
1113.Sy sh_size
1114field of a section header containing uncompressed data.
1115.It Dv ch_addralign
1116The address alignment of the uncompressed section data.
1117This corresponds to the
1118.Sy sh_addralign
1119field of a section header containing uncompressed data.
1120.El
1121.Pp
1122String table sections hold null-terminated character sequences, commonly
1123called strings.
1124The object file uses these strings to represent symbol
1125and section names.
1126One references a string as an index into the string
1127table section.
1128The first byte, which is index zero, is defined to hold
1129a null character.
1130Similarly, a string table's last byte is defined to
1131hold a null character, ensuring null termination for all strings.
1132.Pp
1133An object file's symbol table holds information needed to locate and
1134relocate a program's symbolic definitions and references.
1135A symbol table
1136index is a subscript into this array.
1137.Bd -literal -offset indent
1138typedef struct {
1139	Elf32_Word      st_name;
1140	Elf32_Addr      st_value;
1141	Elf32_Word      st_size;
1142	unsigned char   st_info;
1143	unsigned char   st_other;
1144	Elf32_Half      st_shndx;
1145} Elf32_Sym;
1146.Ed
1147.Bd -literal -offset indent
1148typedef struct {
1149	Elf64_Word      st_name;
1150	unsigned char   st_info;
1151	unsigned char   st_other;
1152	Elf64_Half      st_shndx;
1153	Elf64_Addr      st_value;
1154	Elf64_Xword     st_size;
1155} Elf64_Sym;
1156.Ed
1157.Pp
1158.Bl -tag -width "st_value" -compact
1159.It Dv st_name
1160This member holds an index into the object file's symbol string table,
1161which holds character representations of the symbol names.
1162If the value
1163is non-zero, it represents a string table index that gives the symbol
1164name.
1165Otherwise, the symbol table has no name.
1166.It Dv st_value
1167This member gives the value of the associated symbol.
1168.It Dv st_size
1169Many symbols have associated sizes.
1170This member holds zero if the symbol
1171has no size or an unknown size.
1172.It Dv st_info
1173This member specifies the symbol's type and binding attributes:
1174.Pp
1175.Bl -tag -width "STT_SECTION" -compact
1176.It Dv STT_NOTYPE
1177The symbol's type is not defined.
1178.It Dv STT_OBJECT
1179The symbol is associated with a data object.
1180.It Dv STT_FUNC
1181The symbol is associated with a function or other executable code.
1182.It Dv STT_SECTION
1183The symbol is associated with a section.
1184Symbol table entries of
1185this type exist primarily for relocation and normally have
1186.Sy STB_LOCAL
1187bindings.
1188.It Dv STT_FILE
1189By convention the symbol's name gives the name of the source file
1190associated with the object file.
1191A file symbol has
1192.Sy STB_LOCAL
1193bindings, its section index is
1194.Sy SHN_ABS ,
1195and it precedes the other
1196.Sy STB_LOCAL
1197symbols of the file, if it is present.
1198.It Dv STT_LOPROC
1199This value up to and including
1200.Sy STT_HIPROC
1201are reserved for processor-specific semantics.
1202.It Dv STT_HIPROC
1203This value down to and including
1204.Sy STT_LOPROC
1205are reserved for processor-specific semantics.
1206.El
1207.Pp
1208.Bl -tag -width "STB_GLOBAL" -compact
1209.It Dv STB_LOCAL
1210Local symbols are not visible outside the object file containing their
1211definition.
1212Local symbols of the same name may exist in multiple file
1213without interfering with each other.
1214.It Dv STB_GLOBAL
1215Global symbols are visible to all object files being combined.
1216One file's
1217definition of a global symbol will satisfy another file's undefined
1218reference to the same symbol.
1219.It Dv STB_WEAK
1220Weak symbols resemble global symbols, but their definitions have lower
1221precedence.
1222.It Dv STB_LOPROC
1223This value up to and including
1224.Sy STB_HIPROC
1225are reserved for processor-specific semantics.
1226.It Dv STB_HIPROC
1227This value down to and including
1228.Sy STB_LOPROC
1229are reserved for processor-specific semantics.
1230.Pp
1231There are macros for packing and unpacking the binding and type fields:
1232.Pp
1233.Bl -tag -width "ELF32_ST_INFO(bind, type)" -compact
1234.It Xo
1235.Fn ELF32_ST_BIND info
1236.Xc
1237or
1238.Fn ELF64_ST_BIND info
1239extract a binding from an st_info value.
1240.It Xo
1241.Fn ELF64_ST_TYPE info
1242.Xc
1243or
1244.Fn ELF32_ST_TYPE info
1245extract a type from an st_info value.
1246.It Xo
1247.Fn ELF32_ST_INFO bind type
1248.Xc
1249or
1250.Fn ELF64_ST_INFO bind type
1251convert a binding and a type into an st_info value.
1252.El
1253.El
1254.Pp
1255.It Dv st_other
1256This member currently holds zero and has no defined meaning.
1257.It Dv st_shndx
1258Every symbol table entry is
1259.Dq defined
1260in relation to some section.
1261This member holds the relevant section
1262header table index.
1263.El
1264.Pp
1265Relocation is the process of connecting symbolic references with
1266symbolic definitions.
1267Relocatable files must have information that
1268describes how to modify their section contents, thus allowing executable
1269and shared object files to hold the right information for a process'
1270program image.
1271Relocation entries are these data.
1272.Pp
1273Relocation structures that do not need an addend:
1274.Bd -literal -offset indent
1275typedef struct {
1276	Elf32_Addr      r_offset;
1277	Elf32_Word      r_info;
1278} Elf32_Rel;
1279.Ed
1280.Bd -literal -offset indent
1281typedef struct {
1282	Elf64_Addr      r_offset;
1283	Elf64_Xword     r_info;
1284} Elf64_Rel;
1285.Ed
1286.Pp
1287Relocation structures that need an addend:
1288.Bd -literal -offset indent
1289typedef struct {
1290	Elf32_Addr      r_offset;
1291	Elf32_Word      r_info;
1292	Elf32_Sword     r_addend;
1293} Elf32_Rela;
1294.Ed
1295.Bd -literal -offset indent
1296typedef struct {
1297	Elf64_Addr      r_offset;
1298	Elf64_Xword     r_info;
1299	Elf64_Sxword    r_addend;
1300} Elf64_Rela;
1301.Ed
1302.Pp
1303.Bl -tag -width "r_offset" -compact
1304.It Dv r_offset
1305This member gives the location at which to apply the relocation action.
1306For a relocatable file, the value is the byte offset from the beginning
1307of the section to the storage unit affected by the relocation.
1308For an
1309executable file or shared object, the value is the virtual address of
1310the storage unit affected by the relocation.
1311.It Dv r_info
1312This member gives both the symbol table index with respect to which the
1313relocation must be made and the type of relocation to apply.
1314Relocation
1315types are processor-specific.
1316When the text refers to a relocation
1317entry's relocation type or symbol table index, it means the result of
1318applying
1319.Sy ELF_[32|64]_R_TYPE
1320or
1321.Sy ELF[32|64]_R_SYM ,
1322respectively to the entry's
1323.Sy r_info
1324member.
1325.It Dv r_addend
1326This member specifies a constant addend used to compute the value to be
1327stored into the relocatable field.
1328.El
1329.Ss Note Section
1330ELF note sections consist of entries with the following format:
1331.Bl -column -offset indent "namesz" "32 bits" "Null-terminated originator name"
1332.Sy Field       Ta Sy Size    Ta Sy Description
1333.It Va namesz   Ta 32 bits    Ta Size of "name"
1334.It Va descsz   Ta 32 bits    Ta Size of "desc"
1335.It Va type     Ta 32 bits    Ta OS-dependent note type
1336.It Va name     Ta Va namesz  Ta Null-terminated originator name
1337.It Va desc     Ta Va descsz  Ta OS-dependent note data
1338.El
1339.Pp
1340The
1341.Va name
1342and
1343.Va desc
1344fields are padded to ensure 4-byte alignemnt.
1345.Va namesz
1346and
1347.Va descsz
1348specify the unpadded length.
1349.Pp
1350.Fx
1351defines the following ELF note types
1352.Po with corresponding interpretation of
1353.Va desc Pc :
1354.Bl -tag -width 4n
1355.It Dv NT_FREEBSD_ABI_TAG Pq Value: 1
1356Indicates the OS ABI version in a form of a 32-bit integer containing expected
1357ABI version
1358.Po i.e.,
1359.Dv __FreeBSD_version Pc .
1360.It Dv NT_FREEBSD_NOINIT_TAG Pq Value: 2
1361Indicates that the C startup does not call initialization routines, and thus
1362.Xr rtld 1
1363must do so.
1364.Va desc
1365is ignored.
1366.It Dv NT_FREEBSD_ARCH_TAG Pq Value: 3
1367Contains the MACHINE_ARCH that the executable was built for.
1368.It Dv NT_FREEBSD_FEATURE_CTL Pq Value: 4
1369Contains a bitmask of mitigations and features to enable:
1370.Bl -tag -width 4n
1371.It NT_FREEBSD_FCTL_ASLR_DISABLE Pq Value: 0x01
1372Request that address randomization (ASLR) not be performed.
1373See
1374.Xr security 7 .
1375.It NT_FREEBSD_FCTL_PROTMAX_DISABLE Pq Value: 0x02
1376Request that
1377.Xr mmap 2
1378calls not set PROT_MAX to the initial value of the
1379.Fa prot
1380argument.
1381.It NT_FREEBSD_FCTL_STKGAP_DISABLE Pq Value: 0x04
1382Disable stack gap.
1383.It NT_FREEBSD_FCTL_WXNEEDED Pq Value: 0x08
1384Indicate that the binary requires mappings that are simultaneously
1385writeable and executable.
1386.El
1387.El
1388.Sh SEE ALSO
1389.Xr as 1 ,
1390.Xr gdb 1 ,
1391.Xr ld 1 ,
1392.Xr objdump 1 ,
1393.Xr readelf 1 ,
1394.Xr execve 2 ,
1395.Xr zlib 3 ,
1396.Xr ar 5 ,
1397.Xr core 5
1398.Rs
1399.%A Hewlett Packard
1400.%B Elf-64 Object File Format
1401.Re
1402.Rs
1403.%A Santa Cruz Operation
1404.%B System V Application Binary Interface
1405.Re
1406.Rs
1407.%A Unix System Laboratories
1408.%T Object Files
1409.%B "Executable and Linking Format (ELF)"
1410.Re
1411.Sh HISTORY
1412The ELF header files made their appearance in
1413.Fx 2.2.6 .
1414ELF in itself first appeared in
1415.At V .
1416The ELF format is an adopted standard.
1417.Sh AUTHORS
1418This manual page was written by
1419.An Jeroen Ruigrok van der Werven Aq Mt asmodai@FreeBSD.org
1420with inspiration from BSDi's
1421.Bsx
1422.Nm
1423manpage.
1424