Ubuntu Manpage - Elf - Format of Executable and Linking Format (ELF) Files PDF
Ubuntu Manpage - Elf - Format of Executable and Linking Format (ELF) Files PDF
Ubuntu Manpage - Elf - Format of Executable and Linking Format (ELF) Files PDF
Menu
name
synopsis
description
notes
see also
colophon
NAME
SYNOPSIS
#include <elf.h>
DESCRIPTION
The header file <elf.h> defines the format of ELF executable binary fi
files are normal executable files, relocatable object files, core
objects.
Basic types
The following types are used for N-bit architectures (N=32,64, ElfN sta
manpages.ubuntu.com/manpages/cosmic/man5/elf.5.html 1/26
4/13/2020 Ubuntu Manpage: elf - format of Executable and Linking Format (ELF) files
All data structures that the file format defines follow the "natural
guidelines for the relevant class. If necessary, data structures conta
to ensure 4-byte alignment for 4-byte objects, to force structure siz
4, and so on.
#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;
manpages.ubuntu.com/manpages/cosmic/man5/elf.5.html 2/26
4/13/2020 Ubuntu Manpage: elf - format of Executable and Linking Format (ELF) files
EI_VERSION
The seventh byte is the version number of the ELF sp
manpages.ubuntu.com/manpages/cosmic/man5/elf.5.html 3/26
4/13/2020 Ubuntu Manpage: elf - format of Executable and Linking Format (ELF) files
EI_ABIVERSION
The ninth byte identifies the version of the ABI to
targeted. This field is used to distinguish
versions of an ABI. The interpretation of thi
dependent on the ABI identified by the EI_OSABI fi
conforming to this specification use the value 0.
EI_NIDENT
The size of the e_ident array.
e_type This member of the structure identifies the object file type
manpages.ubuntu.com/manpages/cosmic/man5/elf.5.html 4/26
4/13/2020 Ubuntu Manpage: elf - format of Executable and Linking Format (ELF) files
e_entry This member gives the virtual address to which the syste
control, thus starting the process. If the file has no asso
this member holds zero.
e_phoff This member holds the program header table's file offset in b
has no program header table, this member holds zero.
e_shoff This member holds the section header table's file offset in
has no section header table, this member holds zero.
e_phentsize
This member holds the size in bytes of one entry in the fi
table; all entries are the same size.
e_phnum This member holds the number of entries in the program heade
product of e_phentsize and e_phnum gives the table's size i
manpages.ubuntu.com/manpages/cosmic/man5/elf.5.html 5/26
4/13/2020 Ubuntu Manpage: elf - format of Executable and Linking Format (ELF) files
e_shentsize
This member holds a sections header's size in bytes. A se
entry in the section header table; all entries are the same s
e_shnum This member holds the number of entries in the section heade
product of e_shentsize and e_shnum gives the section hea
bytes. If a file has no section header table, e_shnum holds
e_shstrndx
This member holds the section header table index of the ent
the section name string table. If the file has no section
this member holds the value SHN_UNDEF.
manpages.ubuntu.com/manpages/cosmic/man5/elf.5.html 6/26
4/13/2020 Ubuntu Manpage: elf - format of Executable and Linking Format (ELF) files
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 l
of the p_flags member in the total struct.
manpages.ubuntu.com/manpages/cosmic/man5/elf.5.html 7/26
4/13/2020 Ubuntu Manpage: elf - format of Executable and Linking Format (ELF) files
PT_LOPROC, PT_HIPROC
Values in the inclusive range [PT_LOPROC, PT_HIP
for processor-specific semantics.
PT_GNU_STACK
GNU extension which is used by the Linux kernel t
of the stack via the flags set in the p_flags mem
p_offset This member holds the offset from the beginning of the file a
byte of the segment resides.
p_vaddr This member holds the virtual address at which the first
resides in memory.
p_filesz This member holds the number of bytes in the file image of th
be zero.
p_memsz This member holds the number of bytes in the memory image
may be zero.
manpages.ubuntu.com/manpages/cosmic/man5/elf.5.html 8/26
4/13/2020 Ubuntu Manpage: elf - format of Executable and Linking Format (ELF) files
p_flags This member holds a bit mask of flags relevant to the segment
A text segment commonly has the flags PF_X and PF_R. A data
has PF_X, PF_W, and PF_R.
p_align This member holds the value to which the segments are align
the file. Loadable process segments must have congruent valu
p_offset, modulo the page size. Values of zero and one m
required. Otherwise, p_align should be a positive, integral
p_vaddr should equal p_offset, modulo p_align.
SHN_UNDEF
This value marks an undefined, missing, irrelevant, or ot
section reference.
SHN_LORESERVE
This value specifies the lower bound of the range of reserved in
SHN_LOPROC, SHN_HIPROC
Values greater in the inclusive range [SHN_LOPROC, SHN_HIPROC]
processor-specific semantics.
SHN_ABS
This value specifies the absolute value for the correspond
example, a symbol defined relative to section number SHN_ABS has
and is not affected by relocation.
manpages.ubuntu.com/manpages/cosmic/man5/elf.5.html 9/26
4/13/2020 Ubuntu Manpage: elf - format of Executable and Linking Format (ELF) files
SHN_COMMON
Symbols defined relative to this section are common symbols, suc
or unallocated C external variables.
SHN_HIRESERVE
This value specifies the upper bound of the range of reserved in
reserves indices between SHN_LORESERVE and SHN_HIRESERVE, incl
header table does not contain entries for the reserved indices.
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
section header string table section, giving the location o
string.
manpages.ubuntu.com/manpages/cosmic/man5/elf.5.html 10/26
4/13/2020 Ubuntu Manpage: elf - format of Executable and Linking Format (ELF) files
manpages.ubuntu.com/manpages/cosmic/man5/elf.5.html 11/26
4/13/2020 Ubuntu Manpage: elf - format of Executable and Linking Format (ELF) files
SHT_LOPROC, SHT_HIPROC
Values in the inclusive range [SHT_LOPROC
reserved for processor-specific semantics.
sh_offset This member's value holds the byte offset from the beginning
first byte in the section. One section type, SHT_NOBITS, o
the file, and its sh_offset member locates the conceptual pla
sh_size This member holds the section's size in bytes. Unless the
SHT_NOBITS, the section occupies sh_size bytes in the file
SHT_NOBITS may have a nonzero size, but it occupies no space
manpages.ubuntu.com/manpages/cosmic/man5/elf.5.html 12/26
4/13/2020 Ubuntu Manpage: elf - format of Executable and Linking Format (ELF) files
sh_addralign
Some sections have address alignment constraints. If a
doubleword, the system must ensure doubleword alignment for
That is, the value of sh_addr must be congruent to zero, mod
sh_addralign. Only zero and positive integral powers of tw
value 0 or 1 means that the section has no alignment constrai
sh_entsize
Some sections hold a table of fixed-sized entries, such as a
such a section, this member gives the size in bytes for each
contains zero if the section does not hold a table of fixed-s
manpages.ubuntu.com/manpages/cosmic/man5/elf.5.html 13/26
4/13/2020 Ubuntu Manpage: elf - format of Executable and Linking Format (ELF) files
.dynstr This section holds strings needed for dynamic linking, most c
that represent the names associated with symbol table entrie
of type SHT_STRTAB. The attribute type used is SHF_ALLOC.
.dynsym This section holds the dynamic linking symbol table. This se
SHT_DYNSYM. The attribute used is SHF_ALLOC.
.gnu.version
This section holds the version symbol table, an array of E
This section is of type SHT_GNU_versym. The attribute type u
.gnu.version_d
This section holds the version symbol definitions, a t
structures. This section is of type SHT_GNU_verdef. The att
SHF_ALLOC.
.gnu.version_r
This section holds the version symbol needed elements, a ta
structures. This section is of type SHT_GNU_versym. The att
SHF_ALLOC.
manpages.ubuntu.com/manpages/cosmic/man5/elf.5.html 14/26
4/13/2020 Ubuntu Manpage: elf - format of Executable and Linking Format (ELF) files
.note.ABI-tag
This section is used to declare the expected run-time ABI of
may include the operating system name and its run-time vers
is of type SHT_NOTE. The only attribute used is SHF_ALLOC.
.note.gnu.build-id
This section is used to hold an ID that uniquely identifies t
ELF image. Different files with the same build ID shou
executable content. See the --build-id option to the GNU lin
more details. This section is of type SHT_NOTE. The onl
SHF_ALLOC.
.note.GNU-stack
This section is used in Linux object files for declaring stac
section is of type SHT_PROGBITS. The only attribute use
This indicates to the GNU linker that the object file requi
stack.
.note.openbsd.ident
OpenBSD native executables usually contain this section to
so the kernel can bypass any compatibility ELF binary emu
loading the file.
manpages.ubuntu.com/manpages/cosmic/man5/elf.5.html 15/26
4/13/2020 Ubuntu Manpage: elf - format of Executable and Linking Format (ELF) files
.strtab This section holds strings, most commonly the strings that
associated with symbol table entries. If the file has a load
includes the symbol string table, the section's attribut
SHF_ALLOC bit. Otherwise, the bit will be off. This se
SHT_STRTAB.
manpages.ubuntu.com/manpages/cosmic/man5/elf.5.html 16/26
4/13/2020 Ubuntu Manpage: elf - format of Executable and Linking Format (ELF) files
typedef struct {
uint32_t st_name;
Elf32_Addr st_value;
uint32_t st_size;
unsigned char st_info;
unsigned char st_other;
uint16_t st_shndx;
} Elf32_Sym;
typedef struct {
uint32_t st_name;
unsigned char st_info;
unsigned char st_other;
uint16_t st_shndx;
Elf64_Addr st_value;
uint64_t st_size;
} Elf64_Sym;
The 32-bit and 64-bit versions have the same members, just in a differe
st_name This member holds an index into the object file's symbol st
holds character representations of the symbol names. If th
it represents a string table index that gives the symbol name
symbol has no name.
st_size Many symbols have associated sizes. This member holds zero i
size or an unknown size.
st_info This member specifies the symbol's type and binding attribute
manpages.ubuntu.com/manpages/cosmic/man5/elf.5.html 17/26
4/13/2020 Ubuntu Manpage: elf - format of Executable and Linking Format (ELF) files
STT_LOPROC, STT_HIPROC
Values in the inclusive range [STT_LOPROC, STT_H
for processor-specific semantics.
STB_LOPROC, STB_HIPROC
Values in the inclusive range [STB_LOPROC, STB_H
for processor-specific semantics.
There are macros for packing and unpacking the binding and ty
ELF32_ST_BIND(info), ELF64_ST_BIND(info)
Extract a binding from an st_info value.
ELF32_ST_TYPE(info), ELF64_ST_TYPE(info)
Extract a type from an st_info value.
manpages.ubuntu.com/manpages/cosmic/man5/elf.5.html 18/26
4/13/2020 Ubuntu Manpage: elf - format of Executable and Linking Format (ELF) files
ELF32_ST_VISIBILITY(other) or ELF64_ST_VISIBILITY(other)
typedef struct {
Elf32_Addr r_offset;
uint32_t r_info;
} Elf32_Rel;
typedef struct {
Elf64_Addr r_offset;
uint64_t r_info;
} Elf64_Rel;
typedef struct {
Elf32_Addr r_offset;
uint32_t r_info;
int32_t r_addend;
} Elf32_Rela;
typedef struct {
manpages.ubuntu.com/manpages/cosmic/man5/elf.5.html 19/26
4/13/2020 Ubuntu Manpage: elf - format of Executable and Linking Format (ELF) files
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 reloca
relocatable file, the value is the byte offset from the begin
to the storage unit affected by the relocation. For an
shared object, the value is the virtual address of the storag
the relocation.
r_info This member gives both the symbol table index with
relocation must be made and the type of relocation to apply.
are processor-specific. When the text refers to a relocation
type or symbol table index, it means the result of applying E
ELF[32|64]_R_SYM, respectively, to the entry's r_info member
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[];
manpages.ubuntu.com/manpages/cosmic/man5/elf.5.html 20/26
4/13/2020 Ubuntu Manpage: elf - format of Executable and Linking Format (ELF) files
manpages.ubuntu.com/manpages/cosmic/man5/elf.5.html 21/26
4/13/2020 Ubuntu Manpage: elf - format of Executable and Linking Format (ELF) files
DT_LOPROC, DT_HIPROC
Values in the inclusive range [DT_LOPROC, DT_HIP
for processor-specific semantics
Notes (Nhdr)
ELF notes allow for appending arbitrary information for the syste
largely used by core files (e_type of ET_CORE), but many projects defin
extensions. For example, the GNU tool chain uses ELF notes to pass i
linker to the C library.
Note sections contain a series of notes (see the struct definitions bel
followed by the name field (whose length is defined in n_name
descriptor field (whose length is defined in n_descsz) and whose starti
byte alignment. Neither field is defined in the note struct due
lengths.
An example for parsing out two consecutive notes should clarify their l
manpages.ubuntu.com/manpages/cosmic/man5/elf.5.html 22/26
4/13/2020 Ubuntu Manpage: elf - format of Executable and Linking Format (ELF) files
typedef struct {
Elf32_Word n_namesz;
Elf32_Word n_descsz;
Elf32_Word n_type;
} Elf32_Nhdr;
typedef struct {
Elf64_Word n_namesz;
Elf64_Word n_descsz;
Elf64_Word n_type;
} Elf64_Nhdr;
n_namesz The length of the name field in bytes. The contents will
this note in memory. The name is null terminated. For examp
"GNU", then n_namesz will be set to 4.
n_type Depending on the value of the name field, this member may
manpages.ubuntu.com/manpages/cosmic/man5/elf.5.html 23/26
4/13/2020 Ubuntu Manpage: elf - format of Executable and Linking Format (ELF) files
following values:
manpages.ubuntu.com/manpages/cosmic/man5/elf.5.html 24/26
4/13/2020 Ubuntu Manpage: elf - format of Executable and Linking Format (ELF) files
n_name = GNU
Extensions used by the GNU tool chain.
NT_GNU_ABI_TAG
Operating system (OS) ABI information. The d
words:
NT_GNU_HWCAP
Synthetic hwcap information. The desc field begi
NT_GNU_BUILD_ID
Unique build ID as generated by the GNU ld(1
The desc consists of any nonzero number of bytes
NT_GNU_GOLD_VERSION
The desc contains the GNU Gold linker version use
manpages.ubuntu.com/manpages/cosmic/man5/elf.5.html 25/26
4/13/2020 Ubuntu Manpage: elf - format of Executable and Linking Format (ELF) files
NOTES
The extensions for e_phnum, e_shnum and e_strndx respectively are Linu
BSD and AMD64 also support them; for further information, look under SE
SEE ALSO
COLOPHON
manpages.ubuntu.com/manpages/cosmic/man5/elf.5.html 26/26