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