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