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