elf (5) - Linux Manuals
elf: format of Executable and Linking Format (ELF) files
Command to display elf
manual in Linux: $ man 5 elf
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
objects.
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 table's 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.
Basic types
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, and so on.
ELF header (Ehdr)
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 how to interpret the file,
independent of the processor or the file's 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:
-
- EI_MAG0
-
The first byte of the magic number.
It must be filled with
ELFMAG0.
(0: 0x7f)
- EI_MAG1
-
The second byte of the magic number.
It must be filled with
ELFMAG1.
(1: 'E')
- EI_MAG2
-
The third byte of the magic number.
It must be filled with
ELFMAG2.
(2: 'L')
- EI_MAG3
-
The fourth byte of the magic number.
It must be filled with
ELFMAG3.
(3: 'F')
- EI_CLASS
-
The fifth byte identifies the architecture for this binary:
-
- ELFCLASSNONE
-
This class is invalid.
- ELFCLASS32
-
This defines the 32-bit architecture.
It supports machines with files
and virtual address spaces up to 4 Gigabytes.
- ELFCLASS64
-
This defines the 64-bit architecture.
- EI_DATA
-
The sixth byte specifies the data encoding of the processor-specific
data in the file.
Currently, these encodings are supported:
-
- ELFDATANONE
-
Unknown data format.
- ELFDATA2LSB
-
Two's complement, little-endian.
- ELFDATA2MSB
-
Two's complement, big-endian.
- EI_VERSION
-
The seventh byte is the version number of the ELF specification:
-
-
- EV_NONE
-
Invalid version.
- EV_CURRENT
-
Current version.
- EI_OSABI
-
The eighth byte identifies the operating system
and ABI to which the object is targeted.
Some fields in other ELF structures have flags
and values that have platform-specific meanings;
the interpretation of those fields is determined by the value of this byte.
For example:
-
- ELFOSABI_NONE
-
Same as ELFOSABI_SYSV
- ELFOSABI_SYSV
-
UNIX System V ABI
- ELFOSABI_HPUX
-
HP-UX ABI
- ELFOSABI_NETBSD
-
NetBSD ABI
- ELFOSABI_LINUX
-
Linux ABI
- ELFOSABI_SOLARIS
-
Solaris ABI
- ELFOSABI_IRIX
-
IRIX ABI
- ELFOSABI_FREEBSD
-
FreeBSD ABI
- ELFOSABI_TRU64
-
TRU64 UNIX ABI
- ELFOSABI_ARM
-
ARM architecture ABI
- ELFOSABI_STANDALONE
-
Stand-alone (embedded) ABI
- EI_ABIVERSION
-
The ninth byte identifies the version of the ABI
to which the object is targeted.
This field is used to distinguish among incompatible versions of an ABI.
The interpretation of this version number
is dependent on the ABI identified by the
EI_OSABI
field.
Applications conforming to this specification use the value 0.
- EI_PAD
-
Start of padding.
These bytes are reserved and set to zero.
Programs
which read them should ignore them.
The value for
EI_PAD
will change in
the future if currently unused bytes are given meanings.
- EI_NIDENT
-
The size of the
e_ident
array.
- e_type
-
This member of the structure identifies the object file type:
-
- ET_NONE
-
An unknown type.
- ET_REL
-
A relocatable file.
- ET_EXEC
-
An executable file.
- ET_DYN
-
A shared object.
- ET_CORE
-
A core file.
- e_machine
-
This member specifies the required architecture for an individual file.
For example:
-
- EM_NONE
-
An unknown machine
- EM_M32
-
AT&T WE 32100
- EM_SPARC
-
Sun Microsystems SPARC
- EM_386
-
Intel 80386
- EM_68K
-
Motorola 68000
- EM_88K
-
Motorola 88000
- EM_860
-
Intel 80860
- EM_MIPS
-
MIPS RS3000 (big-endian only)
- EM_PARISC
-
HP/PA
- EM_SPARC32PLUS
-
SPARC with enhanced instruction set
- EM_PPC
-
PowerPC
- EM_PPC64
-
PowerPC 64-bit
- EM_S390
-
IBM S/390
- EM_ARM
-
Advanced RISC Machines
- EM_SH
-
Renesas SuperH
- EM_SPARCV9
-
SPARC v9 64-bit
- EM_IA_64
-
Intel Itanium
- EM_X86_64
-
AMD x86-64
- EM_VAX
-
DEC Vax
- e_version
-
This member identifies the file version:
-
- EV_NONE
-
Invalid version
- EV_CURRENT
-
Current 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 table's file offset in bytes.
If
the file has no program header table, this member holds zero.
- e_shoff
-
This member holds the section header table's 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 header's size in bytes.
- e_phentsize
-
This member holds the size in bytes of one entry in the file's
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 table's size
in bytes.
If a file has no program header,
e_phnum
holds the value zero.
-
If the number of entries in the program header table is
larger than or equal to
PN_XNUM
(0xffff), this member holds
PN_XNUM
(0xffff) and the real number of entries in the program header table is held
in the
sh_info
member of the initial entry in section header table.
Otherwise, the
sh_info
member of the initial entry contains the value zero.
-
- PN_XNUM
-
This is defined as 0xffff, the largest number
e_phnum
can have, specifying where the actual number of program headers is assigned.
- e_shentsize
-
This member holds a sections header's 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 table's size in bytes.
If a file has no section
header table,
e_shnum
holds the value of zero.
-
If the number of entries in the section header table is
larger than or equal to
SHN_LORESERVE
(0xff00),
e_shnum
holds the value zero and the real number of entries in the section header
table is held in the
sh_size
member of the initial entry in section header table.
Otherwise, the
sh_size
member of the initial entry in the section header table holds
the value 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.
-
If the index of section name string table section is
larger than or equal to
SHN_LORESERVE
(0xff00), this member holds
SHN_XINDEX
(0xffff) and the real index of the section name string table section
is held in the
sh_link
member of the initial entry in section header table.
Otherwise, the
sh_link
member of the initial entry in section header table contains the value zero.
Program header (Phdr)
An executable or shared object file's 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 header's
e_phentsize
and
e_phnum
members.
The ELF program header is described by the type
Elf32_Phdr
or
Elf64_Phdr
depending on the architecture:
typedef struct {
uint32_t p_type;
Elf32_Off p_offset;
Elf32_Addr p_vaddr;
Elf32_Addr p_paddr;
uint32_t p_filesz;
uint32_t p_memsz;
uint32_t p_flags;
uint32_t p_align;
} Elf32_Phdr;
typedef struct {
uint32_t p_type;
uint32_t p_flags;
Elf64_Off p_offset;
Elf64_Addr p_vaddr;
Elf64_Addr p_paddr;
uint64_t p_filesz;
uint64_t p_memsz;
uint64_t p_align;
} Elf64_Phdr;
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 structure indicates what kind of segment this array
element describes or how to interpret the array element's information.
-
- PT_NULL
-
The array element is unused and the other members' values are undefined.
This lets the program header have ignored entries.
- PT_LOAD
-
The array element specifies a loadable segment, described by
p_filesz
and
p_memsz.
The bytes from the file are mapped to the beginning of the memory
segment.
If the segment's memory size
p_memsz
is larger than the file size
p_filesz,
the
"extra"
bytes are defined to hold the value 0 and to follow the segment's
initialized area.
The file size may not be larger than the memory size.
Loadable segment entries in the program header table appear in ascending
order, sorted on the
p_vaddr
member.
- PT_DYNAMIC
-
The array element specifies dynamic linking information.
- PT_INTERP
-
The array element specifies the location and size of a null-terminated
pathname to invoke as an interpreter.
This segment type is meaningful
only for executable files (though it may occur for shared objects).
However it may not occur more than once in a file.
If it is present, it must precede any loadable segment entry.
- PT_NOTE
-
The array element specifies the location of notes (ElfN_Nhdr).
- PT_SHLIB
-
This segment type is reserved but has unspecified semantics.
Programs that
contain an array element of this type do not conform to the ABI.
- PT_PHDR
-
The array element, if present,
specifies the location and size of the program header table itself,
both in the file and in the memory image of the program.
This segment type may not occur more than once in a file.
Moreover, it may
occur only if the program header table is part of the memory image of the
program.
If it is present, it must precede any loadable segment entry.
- PT_LOPROC, PT_HIPROC
-
Values in the inclusive range
[PT_LOPROC, PT_HIPROC]
are reserved for processor-specific semantics.
- PT_GNU_STACK
-
GNU extension which is used by the Linux kernel to control the state of the
stack via the flags set in the
p_flags
member.
- 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 segment's 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 bit mask of flags relevant to the segment:
-
- PF_X
-
An executable segment.
- PF_W
-
A writable segment.
- PF_R
-
A readable segment.
-
A text segment commonly has the flags
PF_X
and
PF_R.
A data segment commonly has
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.
Section header (Shdr)
A file's section header table lets one locate all the file's sections.
The
section header table is an array of
Elf32_Shdr
or
Elf64_Shdr
structures.
The
ELF header's
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:
the initial entry and the indices between
SHN_LORESERVE
and
SHN_HIRESERVE.
The initial entry is used in ELF extensions for
e_phnum,
e_shnum,
and
e_shstrndx;
in other cases, each field in the initial entry is set to zero.
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, SHN_HIPROC
-
Values greater in the inclusive range
[SHN_LOPROC, SHN_HIPROC]
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