xref: /freebsd/share/man/man5/elf.5 (revision c98323078dede7579020518ec84cdcb478e5c142)
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
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_Size      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_Half      st_name;
1085	unsigned char   st_info;
1086	unsigned char   st_other;
1087	Elf64_Quarter   st_shndx;
1088	Elf64_Addr      st_value;
1089	Elf64_Size      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_Size      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_Size      r_info;
1236	Elf64_Off       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