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