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