NAME
elf - format of Executable and Linking Format (ELF) files
SYNOPSIS
#include <elf.h>
DESCRIPTION
The header file <elf.h> defines the format of ELF executable binary files. Amongst these files are normal executable files, relocatable object files, core files and shared libraries.
An executable file using the ELF file format consists of an ELF header, followed by a program header table or a section header table, or both. The ELF header is always at offset zero of the file. The program header table and the section header tables offset in the file are defined in the ELF header. The two tables describe the rest of the particularities of the file.
This header file describes the above mentioned headers as C structures and also includes structures for dynamic sections, relocation sections and symbol tables.
The following types are used for N-bit architectures (N=32,64, ElfN stands for Elf32 or Elf64, uintN_t stands for uint32_t or uint64_t):
ElfN_Addr Unsigned program address, uintN_t
ElfN_Off Unsigned file offset, uintN_t
ElfN_Section Unsigned section index, uint16_t
ElfN_Versym Unsigned version symbol information, uint16_t
Elf_Byte unsigned char
ElfN_Half uint16_t
ElfN_Sword int32_t
ElfN_Word uint32_t
ElfN_Sxword int64_t
ElfN_Xword uint64_t
(Note: The *BSD terminology is a bit different. There Elf64_Half is twice as large as Elf32_Half, and Elf64Quarter is used for uint16_t. In order to avoid confusion these types are replaced by explicit ones in the below.)
All data structures that the file format defines follow the "natural" size and alignment guidelines for the relevant class. If necessary, data structures contain explicit padding to ensure 4-byte alignment for 4-byte objects, to force structure sizes to a multiple of 4, etc.
The ELF header is described by the type Elf32_Ehdr or Elf64_Ehdr:
#define EI_NIDENT 16
typedef struct {
unsigned char e_ident[EI_NIDENT];
uint16_t e_type;
uint16_t e_machine;
uint32_t e_version;
ElfN_Addr e_entry;
ElfN_Off e_phoff;
ElfN_Off e_shoff;
uint32_t e_flags;
uint16_t e_ehsize;
uint16_t e_phentsize;
uint16_t e_phnum;
uint16_t e_shentsize;
uint16_t e_shnum;
uint16_t e_shstrndx;
} ElfN_Ehdr;
The fields have the following meanings:
e_ident | This array of bytes specifies to interpret the file,
independent of the processor or the files remaining contents.
Within this array everything is named by macros, which start with
the prefix EI_ and may contain values which start with the prefix
ELF. The following macros are defined:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
e_type | This member of the structure identifies the object file type:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
e_machine | This member specifies the required architecture for an individual file.
E.g.:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
e_version | This member identifies the file version:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
e_entry | This member gives the virtual address to which the system first transfers control, thus starting the process. If the file has no associated entry point, this member holds zero. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
e_phoff | This member holds the program header tables file offset in bytes. If the file has no program header table, this member holds zero. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
e_shoff | This member holds the section header tables file offset in bytes. If the file has no section header table this member holds zero. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
e_flags | This member holds processor-specific flags associated with the file. Flag names take the form EF_machine_flag. Currently no flags have been defined. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
e_ehsize | This member holds the ELF headers size in bytes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
e_phentsize | This member holds the size in bytes of one entry in the files program header table; all entries are the same size. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
e_phnum | This member holds the number of entries in the program header table. Thus the product of e_phentsize and e_phnum gives the tables size in bytes. If a file has no program header, e_phnum holds the value zero. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
e_shentsize | This member holds a sections headers size in bytes. A section header is one entry in the section header table; all entries are the same size. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
e_shnum | This member holds the number of entries in the section header table. Thus the product of e_shentsize and e_shnum gives the section header tables size in bytes. If a file has no section header table, e_shnum holds the value of zero. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
e_shstrndx | This member holds the section header table index of the entry associated
with the section name string table.
If the file has no section name string
table, this member holds the value
SHN_UNDEF.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
An executable or shared object files program header table is an array of
structures, each describing a segment or other information the system needs
to prepare the program for execution.
An object file
segment contains one or more
sections. Program headers are meaningful only for executable and shared object files.
A file specifies its own program header size with the ELF headers
e_phentsize and
e_phnum members.
The ELF program header is described by the type
Elf32_Phdr or
Elf64_Phdr depending on the architecture:
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The main difference between the 32-bit and the 64-bit program header lies in the location of the p_flags member in the total struct. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
p_type | This member of the Phdr struct tells what kind of segment this array
element describes or how to interpret the array elements information.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
p_offset | This member holds the offset from the beginning of the file at which the first byte of the segment resides. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
p_vaddr | This member holds the virtual address at which the first byte of the segment resides in memory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
p_paddr | On systems for which physical addressing is relevant, this member is reserved for the segments physical address. Under BSD this member is not used and must be zero. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
p_filesz | This member holds the number of bytes in the file image of the segment. It may be zero. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
p_memsz | This member holds the number of bytes in the memory image of the segment. It may be zero. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
p_flags | This member holds a bitmask of flags relevant to the segment:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
A text segment commonly has the flags PF_X and PF_R. A data segment commonly has PF_X, PF_W and PF_R. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
p_align | This member holds the value to which the segments are aligned in memory and in the file. Loadable process segments must have congruent values for p_vaddr and p_offset, modulo the page size. Values of zero and one mean no alignment is required. Otherwise, p_align should be a positive, integral power of two, and p_vaddr should equal p_offset, modulo p_align. |
A files section header table lets one locate all the files sections. The section header table is an array of Elf32_Shdr or Elf64_Shdr structures. The ELF headers e_shoff member gives the byte offset from the beginning of the file to the section header table. e_shnum holds the number of entries the section header table contains. e_shentsize holds the size in bytes of each entry.
A section header table index is a subscript into this array. Some section header table indices are reserved. An object file does not have sections for these special indices:
SHN_UNDEF | This value marks an undefined, missing, irrelevant or otherwise meaningless section reference. |
SHN_LORESERVE | This value specifies the lower bound of the range of reserved indices. |
SHN_LOPROC | Values greater than or equal to SHN_HIPROC are reserved for processor-specific semantics. |
SHN_HIPROC | Values less than or equal to SHN_LOPROC are reserved for processor-specific semantics. |
SHN_ABS | This value specifies the absolute value for the corresponding reference. For example, a symbol defined relative to section number SHN_ABS has an absolute value and is not affected by relocation. |
SHN_COMMON | Symbols defined relative to this section are common symbols, such as FORTRAN COMMON or unallocated C external variables. |
SHN_HIRESERVE | This value specifies the upper bound of the range of reserved indices. The system reserves indices between SHN_LORESERVE and SHN_HIRESERVE, inclusive. The section header table does not contain entries for the reserved indices. |
The section header has the following structure:
typedef struct {
uint32_t sh_name;
uint32_t sh_type;
uint32_t sh_flags;
Elf32_Addr sh_addr;
Elf32_Off sh_offset;
uint32_t sh_size;
uint32_t sh_link;
uint32_t sh_info;
uint32_t sh_addralign;
uint32_t sh_entsize;
} Elf32_Shdr;
typedef struct {
uint32_t sh_name;
uint32_t sh_type;
uint64_t sh_flags;
Elf64_Addr sh_addr;
Elf64_Off sh_offset;
uint64_t sh_size;
uint32_t sh_link;
uint32_t sh_info;
uint64_t sh_addralign;
uint64_t sh_entsize;
} Elf64_Shdr;
No real differences exist between the 32-bit and 64-bit section headers.
sh_name | This member specifies the name of the section. Its value is an index into the section header string table section, giving the location of a null-terminated string. | ||||||||||||||||||||||||||||||||
sh_type | This member categorizes the sections contents and semantics.
| ||||||||||||||||||||||||||||||||
sh_flags | Sections support one-bit flags that describe miscellaneous attributes.
If a flag bit is set in
sh_flags, the attribute is "on" for the section.
Otherwise, the attribute is "off" or does not apply.
Undefined attributes are set to zero.
| ||||||||||||||||||||||||||||||||
sh_addr | If this section appears in the memory image of a process, this member holds the address at which the sections first byte should reside. Otherwise, the member contains zero. | ||||||||||||||||||||||||||||||||
sh_offset | This members value holds the byte offset from the beginning of the file to the first byte in the section. One section type, SHT_NOBITS, occupies no space in the file, and its sh_offset member locates the conceptual placement in the file. | ||||||||||||||||||||||||||||||||
sh_size | This member holds the sections size in bytes. Unless the section type is SHT_NOBITS, the section occupies sh_size bytes in the file. A section of type SHT_NOBITS may have a non-zero size, but it occupies no space in the file. | ||||||||||||||||||||||||||||||||
sh_link | This member holds a section header table index link, whose interpretation depends on the section type. | ||||||||||||||||||||||||||||||||
sh_info | This member holds extra information, whose interpretation depends on the section type. | ||||||||||||||||||||||||||||||||
sh_addralign | Some sections have address alignment constraints. If a section holds a doubleword, the system must ensure doubleword alignment for the entire section. That is, the value of sh_addr must be congruent to zero, modulo the value of sh_addralign. Only zero and positive integral powers of two are allowed. Values of zero or one mean the section has no alignment constraints. | ||||||||||||||||||||||||||||||||
sh_entsize | Some sections hold a table of fixed-sized entries, such as a symbol table. For such a section, this member gives the size in bytes for each entry. This member contains zero if the section does not hold a table of fixed-size entries. | ||||||||||||||||||||||||||||||||
Various sections hold program and control information: | |||||||||||||||||||||||||||||||||
.bss | This section holds uninitialized data that contributes to the programs memory image. By definition, the system initializes the data with zeros when the program begins to run. This section is of type SHT_NOBITS. The attribute types are SHF_ALLOC and SHF_WRITE. | ||||||||||||||||||||||||||||||||
.comment | This section holds version control information. This section is of type SHT_PROGBITS. No attribute types are used. | ||||||||||||||||||||||||||||||||
.ctors | This section holds initialized pointers to the C++ constructor functions. This section is of type SHT_PROGBITS. The attribute types are SHF_ALLOC and SHF_WRITE. | ||||||||||||||||||||||||||||||||
.data | This section holds initialized data that contribute to the programs memory image. This section is of type SHT_PROGBITS. The attribute types are SHF_ALLOC and SHF_WRITE. | ||||||||||||||||||||||||||||||||
.data1 | This section holds initialized data that contribute to the programs memory image. This section is of type SHT_PROGBITS. The attribute types are SHF_ALLOC and SHF_WRITE. | ||||||||||||||||||||||||||||||||
.debug | This section holds information for symbolic debugging. The contents are unspecified. This section is of type SHT_PROGBITS. No attribute types are used. | ||||||||||||||||||||||||||||||||
.dtors | This section holds initialized pointers to the C++ destructor functions. This section is of type SHT_PROGBITS. The attribute types are SHF_ALLOC and SHF_WRITE. | ||||||||||||||||||||||||||||||||
.dynamic | This section holds dynamic linking information. The sections attributes will include the SHF_ALLOC bit. Whether the SHF_WRITE bit is set is processor-specific. This section is of type SHT_DYNAMIC. See the attributes above. | ||||||||||||||||||||||||||||||||
.dynstr | This section holds strings needed for dynamic linking, most commonly the strings that represent the names associated with symbol table entries. This section is of type SHT_STRTAB. The attribute type used is SHF_ALLOC. | ||||||||||||||||||||||||||||||||
.dynsym | This section holds the dynamic linking symbol table. This section is of type SHT_DYNSYM. The attribute used is SHF_ALLOC. | ||||||||||||||||||||||||||||||||
.fini | This section holds executable instructions that contribute to the process termination code. When a program exits normally the system arranges to execute the code in this section. This section is of type SHT_PROGBITS. The attributes used are SHF_ALLOC and SHF_EXECINSTR. | ||||||||||||||||||||||||||||||||
.gnu.version | |||||||||||||||||||||||||||||||||
This section holds the version symbol table, an array of ElfN_Half elements. This section is of type SHT_GNU_versym. The attribute type used is SHF_ALLOC. | |||||||||||||||||||||||||||||||||
.gnu.version_d | |||||||||||||||||||||||||||||||||
This section holds the version symbol definitions, a table of ElfN_Verdef structures. This section is of type SHT_GNU_verdef. The attribute type used is SHF_ALLOC. | |||||||||||||||||||||||||||||||||
.gnu.version_r | |||||||||||||||||||||||||||||||||
This section holds the version symbol needed elements, a table of ElfN_Verneed structures. This section is of type SHT_GNU_versym. The attribute type used is SHF_ALLOC. | |||||||||||||||||||||||||||||||||
.got | This section holds the global offset table. This section is of type SHT_PROGBITS. The attributes are processor specific. | ||||||||||||||||||||||||||||||||
.hash | This section holds a symbol hash table. This section is of type SHT_HASH. The attribute used is SHF_ALLOC. | ||||||||||||||||||||||||||||||||
.init | This section holds executable instructions that contribute to the process initialization code. When a program starts to run the system arranges to execute the code in this section before calling the main program entry point. This section is of type SHT_PROGBITS. The attributes used are SHF_ALLOC and SHF_EXECINSTR. | ||||||||||||||||||||||||||||||||
.interp | This section holds the pathname of a program interpreter. If the file has a loadable segment that includes the section, the sections attributes will include the SHF_ALLOC bit. Otherwise, that bit will be off. This section is of type SHT_PROGBITS. | ||||||||||||||||||||||||||||||||
.line | This section holds line number information for symbolic debugging, which describes the correspondence between the program source and the machine code. The contents are unspecified. This section is of type SHT_PROGBITS. No attribute types are used. | ||||||||||||||||||||||||||||||||
.note | This section holds information in the "Note Section" format described below. This section is of type SHT_NOTE. No attribute types are used. OpenBSD native executables usually contain a .note.openbsd.ident section to identify themselves, for the kernel to bypass any compatibility ELF binary emulation tests when loading the file. | ||||||||||||||||||||||||||||||||
.note.GNU-stack | This section is used in Linux object files for declaring stack attributes. This section is of type SHT_PROGBITS. The only attribute used is SHF_EXECINSTR. This indicates to the GNU linker that the object file requires an executable stack. | ||||||||||||||||||||||||||||||||
.plt | This section holds the procedure linkage table. This section is of type SHT_PROGBITS. The attributes are processor specific. | ||||||||||||||||||||||||||||||||
.relNAME | This section holds relocation information as described below. If the file has a loadable segment that includes relocation, the sections attributes will include the SHF_ALLOC bit. Otherwise the bit will be off. By convention, "NAME" is supplied by the section to which the relocations apply. Thus a relocation section for .text normally would have the name .rel.text. This section is of type SHT_REL. | ||||||||||||||||||||||||||||||||
.relaNAME | This section holds relocation information as described below. If the file has a loadable segment that includes relocation, the sections attributes will include the SHF_ALLOC bit. Otherwise the bit will be off. By convention, "NAME" is supplied by the section to which the relocations apply. Thus a relocation section for .text normally would have the name .rela.text. This section is of type SHT_RELA. | ||||||||||||||||||||||||||||||||
.rodata | This section holds read-only data that typically contributes to a non-writable segment in the process image. This section is of type SHT_PROGBITS. The attribute used is SHF_ALLOC. | ||||||||||||||||||||||||||||||||
.rodata1 | This section holds read-only data that typically contributes to a non-writable segment in the process image. This section is of type SHT_PROGBITS. The attribute used is SHF_ALLOC. | ||||||||||||||||||||||||||||||||
.shstrtab | This section holds section names. This section is of type SHT_STRTAB. No attribute types are used. | ||||||||||||||||||||||||||||||||
.strtab | This section holds strings, most commonly the strings that represent the names associated with symbol table entries. If the file has a loadable segment that includes the symbol string table, the sections attributes will include the SHF_ALLOC bit. Otherwise the bit will be off. This section is of type SHT_STRTAB. | ||||||||||||||||||||||||||||||||
.symtab | This section holds a symbol table. If the file has a loadable segment that includes the symbol table, the sections attributes will include the SHF_ALLOC bit. Otherwise the bit will be off. This section is of type SHT_SYMTAB. | ||||||||||||||||||||||||||||||||
.text | This section holds the "text", or executable instructions, of a program. This section is of type SHT_PROGBITS. The attributes used are SHF_ALLOC and SHF_EXECINSTR. | ||||||||||||||||||||||||||||||||
String table sections hold null-terminated character sequences, commonly called strings. The object file uses these strings to represent symbol and section names. One references a string as an index into the string table section. The first byte, which is index zero, is defined to hold a null byte ('\0'). Similarly, a string tables last byte is defined to hold a null byte, ensuring null termination for all strings. | |||||||||||||||||||||||||||||||||
An object files symbol table holds information needed to locate and
relocate a programs symbolic definitions and references.
A symbol table
index is a subscript into this array.
| |||||||||||||||||||||||||||||||||
The 32-bit and 64-bit versions have the same members, just in a different order. | |||||||||||||||||||||||||||||||||
st_name | This member holds an index into the object files symbol string table, which holds character representations of the symbol names. If the value is non-zero, it represents a string table index that gives the symbol name. Otherwise, the symbol table has no name. | ||||||||||||||||||||||||||||||||
st_value | This member gives the value of the associated symbol. | ||||||||||||||||||||||||||||||||
st_size | Many symbols have associated sizes. This member holds zero if the symbol has no size or an unknown size. | ||||||||||||||||||||||||||||||||
st_info | This member specifies the symbols type and binding attributes:
| ||||||||||||||||||||||||||||||||
st_other | This member defines the symbol visibility.
ELF32_ST_VISIBILITY(other) or ELF64_ST_VISIBILITY(other) | ||||||||||||||||||||||||||||||||
st_shndx | Every symbol table entry is "defined" in relation to some section. This member holds the relevant section header table index. |
Relocation is the process of connecting symbolic references with symbolic definitions. Relocatable files must have information that describes how to modify their section contents, thus allowing executable and shared object files to hold the right information for a processs program image. Relocation entries are these data.
Relocation structures that do not need an addend:
typedef struct {
Elf32_Addr r_offset;
uint32_t r_info;
} Elf32_Rel;
typedef struct {
Elf64_Addr r_offset;
uint64_t r_info;
} Elf64_Rel;
Relocation structures that need an addend:
typedef struct {
Elf32_Addr r_offset;
uint32_t r_info;
int32_t r_addend;
} Elf32_Rela;
typedef struct {
Elf64_Addr r_offset;
uint64_t r_info;
int64_t r_addend;
} Elf64_Rela;
r_offset | This member gives the location at which to apply the relocation action. For a relocatable file, the value is the byte offset from the beginning of the section to the storage unit affected by the relocation. For an executable file or shared object, the value is the virtual address of the storage unit affected by the relocation. |
r_info | This member gives both the symbol table index with respect to which the relocation must be made and the type of relocation to apply. Relocation types are processor specific. When the text refers to a relocation entrys relocation type or symbol table index, it means the result of applying ELF_[32|64]_R_TYPE or ELF[32|64]_R_SYM, respectively, to the entrys r_info member. |
r_addend | This member specifies a constant addend used to compute the value to be stored into the relocatable field. |
The .dynamic section contains a series of structures that hold relevant dynamic linking information. The d_tag member controls the interpretation of d_un.
typedef struct {
Elf32_Sword d_tag;
union {
Elf32_Word d_val;
Elf32_Addr d_ptr;
} d_un;
} Elf32_Dyn;
extern Elf32_Dyn _DYNAMIC[];
typedef struct {
Elf64_Sxword d_tag;
union {
Elf64_Xword d_val;
Elf64_Addr d_ptr;
} d_un;
} Elf64_Dyn;
extern Elf64_Dyn _DYNAMIC[];
d_tag | This member may have any of the following values:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
d_val | This member represents integer values with various interpretations. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
d_ptr | This member represents program virtual addresses. When interpreting these addresses, the actual address should be computed based on the original file value and memory base address. Files do not contain relocation entries to fixup these addresses. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_DYNAMIC | Array containing all the dynamic structures in the .dynamic section. This is automatically populated by the linker. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
NOTES
ELF first appeared in System V. The ELF format is an adopted standard.
SEE ALSO
as(1), gdb(1), ld(1), objdump(1), execve(2), core(5)
Hewlett-Packard, Elf-64 Object File Format.
Santa Cruz Operation, System V Application Binary Interface.
Unix System Laboratories, "Object Files", Executable and Linking Format (ELF).
COLOPHON
This page is part of release 3.23 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/.