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