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