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