xref: /freebsd/share/man/man5/elf.5 (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1.\" Copyright (c) 1999 Jeroen Ruigrok van der Werven
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\"
13.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23.\" SUCH DAMAGE.
24.\"
25.\" $FreeBSD$
26.\"
27.Dd December 18, 2005
28.Dt ELF 5
29.Os
30.Sh NAME
31.Nm elf
32.Nd format of ELF executable binary files
33.Sh SYNOPSIS
34.In elf.h
35.Sh DESCRIPTION
36The header file
37.In elf.h
38defines the format of ELF executable binary files.
39Amongst these files are
40normal executable files, relocatable object files, core files and shared
41libraries.
42.Pp
43An executable file using the ELF file format consists of an ELF header,
44followed by a program header table or a section header table, or both.
45The ELF header is always at offset zero of the file.
46The program header
47table and the section header table's offset in the file are defined in the
48ELF header.
49The two tables describe the rest of the particularities of
50the file.
51.Pp
52Applications which wish to process ELF binary files for their native
53architecture only should include
54.In elf.h
55in their source code.
56These applications should need to refer to
57all the types and structures by their generic names
58.Dq Elf_xxx
59and to the macros by
60.Dq ELF_xxx .
61Applications written this way can be compiled on any architecture,
62regardless whether the host is 32-bit or 64-bit.
63.Pp
64Should an application need to process ELF files of an unknown
65architecture then the application needs to include both
66.In sys/elf32.h
67and
68.In sys/elf64.h
69instead of
70.In elf.h .
71Furthermore, all types and structures need to be identified by either
72.Dq Elf32_xxx
73or
74.Dq Elf64_xxx .
75The macros need to be identified by
76.Dq ELF32_xxx
77or
78.Dq ELF64_xxx .
79.Pp
80Whatever the system's architecture is, it will always include
81.In sys/elf_common.h
82as well as
83.In sys/elf_generic.h .
84.Pp
85These header files describe the above mentioned headers as C structures
86and also include structures for dynamic sections, relocation sections and
87symbol tables.
88.Pp
89The following types are being used for 32-bit architectures:
90.Bd -literal -offset indent
91Elf32_Addr	Unsigned 32-bit program address
92Elf32_Half	Unsigned 16-bit field
93Elf32_Lword	Unsigned 64-bit field
94Elf32_Off	Unsigned 32-bit file offset
95Elf32_Sword	Signed 32-bit field or integer
96Elf32_Word	Unsigned 32-bit field or integer
97.Ed
98.Pp
99For 64-bit architectures we have the following types:
100.Bd -literal -offset indent
101Elf64_Addr	Unsigned 64-bit program address
102Elf64_Half	Unsigned 16-bit field
103Elf64_Lword	Unsigned 64-bit field
104Elf64_Off	Unsigned 64-bit file offset
105Elf64_Sword	Signed 32-bit field
106Elf64_Sxword	Signed 64-bit field or integer
107Elf64_Word	Unsigned 32-bit field
108Elf64_Xword	Unsigned 64-bit field or integer
109.Ed
110.Pp
111All data structures that the file format defines follow the
112.Dq natural
113size and alignment guidelines for the relevant class.
114If necessary,
115data structures contain explicit padding to ensure 4-byte alignment
116for 4-byte objects, to force structure sizes to a multiple of 4, etc.
117.Pp
118The ELF header is described by the type Elf32_Ehdr or Elf64_Ehdr:
119.Bd -literal -offset indent
120typedef struct {
121        unsigned char   e_ident[EI_NIDENT];
122        Elf32_Half      e_type;
123        Elf32_Half      e_machine;
124        Elf32_Word      e_version;
125        Elf32_Addr      e_entry;
126        Elf32_Off       e_phoff;
127        Elf32_Off       e_shoff;
128        Elf32_Word      e_flags;
129        Elf32_Half      e_ehsize;
130        Elf32_Half      e_phentsize;
131        Elf32_Half      e_phnum;
132        Elf32_Half      e_shentsize;
133        Elf32_Half      e_shnum;
134        Elf32_Half      e_shstrndx;
135} Elf32_Ehdr;
136.Ed
137.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.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 hold 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
1072String table sections hold null-terminated character sequences, commonly
1073called strings.
1074The object file uses these strings to represent symbol
1075and section names.
1076One references a string as an index into the string
1077table section.
1078The first byte, which is index zero, is defined to hold
1079a null character.
1080Similarly, a string table's last byte is defined to
1081hold a null character, ensuring null termination for all strings.
1082.Pp
1083An object file's symbol table holds information needed to locate and
1084relocate a program's symbolic definitions and references.
1085A symbol table
1086index is a subscript into this array.
1087.Bd -literal -offset indent
1088typedef struct {
1089	Elf32_Word      st_name;
1090	Elf32_Addr      st_value;
1091	Elf32_Word      st_size;
1092	unsigned char   st_info;
1093	unsigned char   st_other;
1094	Elf32_Half      st_shndx;
1095} Elf32_Sym;
1096.Ed
1097.Bd -literal -offset indent
1098typedef struct {
1099	Elf64_Word      st_name;
1100	unsigned char   st_info;
1101	unsigned char   st_other;
1102	Elf64_Half      st_shndx;
1103	Elf64_Addr      st_value;
1104	Elf64_Xword     st_size;
1105} Elf64_Sym;
1106.Ed
1107.Pp
1108.Bl -tag -width "st_value" -compact
1109.It Dv st_name
1110This member holds an index into the object file's symbol string table,
1111which holds character representations of the symbol names.
1112If the value
1113is non-zero, it represents a string table index that gives the symbol
1114name.
1115Otherwise, the symbol table has no name.
1116.It Dv st_value
1117This member gives the value of the associated symbol.
1118.It Dv st_size
1119Many symbols have associated sizes.
1120This member holds zero if the symbol
1121has no size or an unknown size.
1122.It Dv st_info
1123This member specifies the symbol's type and binding attributes:
1124.Pp
1125.Bl -tag -width "STT_SECTION" -compact
1126.It Dv STT_NOTYPE
1127The symbol's type is not defined.
1128.It Dv STT_OBJECT
1129The symbol is associated with a data object.
1130.It Dv STT_FUNC
1131The symbol is associated with a function or other executable code.
1132.It Dv STT_SECTION
1133The symbol is associated with a section.
1134Symbol table entries of
1135this type exist primarily for relocation and normally have
1136.Sy STB_LOCAL
1137bindings.
1138.It Dv STT_FILE
1139By convention the symbol's name gives the name of the source file
1140associated with the object file.
1141A file symbol has
1142.Sy STB_LOCAL
1143bindings, its section index is
1144.Sy SHN_ABS ,
1145and it precedes the other
1146.Sy STB_LOCAL
1147symbols of the file, if it is present.
1148.It Dv STT_LOPROC
1149This value up to and including
1150.Sy STT_HIPROC
1151are reserved for processor-specific semantics.
1152.It Dv STT_HIPROC
1153This value down to and including
1154.Sy STT_LOPROC
1155are reserved for processor-specific semantics.
1156.El
1157.Pp
1158.Bl -tag -width "STB_GLOBAL" -compact
1159.It Dv STB_LOCAL
1160Local symbols are not visible outside the object file containing their
1161definition.
1162Local symbols of the same name may exist in multiple file
1163without interfering with each other.
1164.It Dv STB_GLOBAL
1165Global symbols are visible to all object files being combined.
1166One file's
1167definition of a global symbol will satisfy another file's undefined
1168reference to the same symbol.
1169.It Dv STB_WEAK
1170Weak symbols resemble global symbols, but their definitions have lower
1171precedence.
1172.It Dv STB_LOPROC
1173This value up to and including
1174.Sy STB_HIPROC
1175are reserved for processor-specific semantics.
1176.It Dv STB_HIPROC
1177This value down to and including
1178.Sy STB_LOPROC
1179are reserved for processor-specific semantics.
1180.Pp
1181There are macros for packing and unpacking the binding and type fields:
1182.Pp
1183.Bl -tag -width "ELF32_ST_INFO(bind, type)" -compact
1184.It Xo
1185.Fn ELF32_ST_BIND info
1186.Xc
1187or
1188.Fn ELF64_ST_BIND info
1189extract a binding from an st_info value.
1190.It Xo
1191.Fn ELF64_ST_TYPE info
1192.Xc
1193or
1194.Fn ELF32_ST_TYPE info
1195extract a type from an st_info value.
1196.It Xo
1197.Fn ELF32_ST_INFO bind type
1198.Xc
1199or
1200.Fn ELF64_ST_INFO bind type
1201convert a binding and a type into an st_info value.
1202.El
1203.El
1204.Pp
1205.It Dv st_other
1206This member currently holds zero and has no defined meaning.
1207.It Dv st_shndx
1208Every symbol table entry is
1209.Dq defined
1210in relation to some section.
1211This member holds the relevant section
1212header table index.
1213.El
1214.Pp
1215Relocation is the process of connecting symbolic references with
1216symbolic definitions.
1217Relocatable files must have information that
1218describes how to modify their section contents, thus allowing executable
1219and shared object files to hold the right information for a process'
1220program image.
1221Relocation entries are these data.
1222.Pp
1223Relocation structures that do not need an addend:
1224.Bd -literal -offset indent
1225typedef struct {
1226	Elf32_Addr      r_offset;
1227	Elf32_Word      r_info;
1228} Elf32_Rel;
1229.Ed
1230.Bd -literal -offset indent
1231typedef struct {
1232	Elf64_Addr      r_offset;
1233	Elf64_Xword     r_info;
1234} Elf64_Rel;
1235.Ed
1236.Pp
1237Relocation structures that need an addend:
1238.Bd -literal -offset indent
1239typedef struct {
1240	Elf32_Addr      r_offset;
1241	Elf32_Word      r_info;
1242	Elf32_Sword     r_addend;
1243} Elf32_Rela;
1244.Ed
1245.Bd -literal -offset indent
1246typedef struct {
1247	Elf64_Addr      r_offset;
1248	Elf64_Xword     r_info;
1249	Elf64_Sxword    r_addend;
1250} Elf64_Rela;
1251.Ed
1252.Pp
1253.Bl -tag -width "r_offset" -compact
1254.It Dv r_offset
1255This member gives the location at which to apply the relocation action.
1256For a relocatable file, the value is the byte offset from the beginning
1257of the section to the storage unit affected by the relocation.
1258For an
1259executable file or shared object, the value is the virtual address of
1260the storage unit affected by the relocation.
1261.It Dv r_info
1262This member gives both the symbol table index with respect to which the
1263relocation must be made and the type of relocation to apply.
1264Relocation
1265types are processor-specific.
1266When the text refers to a relocation
1267entry's relocation type or symbol table index, it means the result of
1268applying
1269.Sy ELF_[32|64]_R_TYPE
1270or
1271.Sy ELF[32|64]_R_SYM ,
1272respectively to the entry's
1273.Sy r_info
1274member.
1275.It Dv r_addend
1276This member specifies a constant addend used to compute the value to be
1277stored into the relocatable field.
1278.El
1279.Sh SEE ALSO
1280.Xr as 1 ,
1281.Xr gdb 1 ,
1282.Xr ld 1 ,
1283.Xr objdump 1 ,
1284.Xr execve 2 ,
1285.Xr ar 5 ,
1286.Xr core 5
1287.Rs
1288.%A Hewlett Packard
1289.%B Elf-64 Object File Format
1290.Re
1291.Rs
1292.%A Santa Cruz Operation
1293.%B System V Application Binary Interface
1294.Re
1295.Rs
1296.%A Unix System Laboratories
1297.%T Object Files
1298.%B "Executable and Linking Format (ELF)"
1299.Re
1300.Sh HISTORY
1301The ELF header files made their appearance in
1302.Fx 2.2.6 .
1303ELF in itself first appeared in
1304.At V .
1305The ELF format is an adopted standard.
1306.Sh AUTHORS
1307This manual page was written by
1308.An Jeroen Ruigrok van der Werven Aq Mt asmodai@FreeBSD.org
1309with inspiration from BSDi's
1310.Bsx
1311.Xr elf 5
1312manpage.
1313