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