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