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