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