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