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