ELF(5)
๐ธ NAME ๐ธ
okay bestie, welcome to the ELF(5) page from the File Formats Manual. This is all about elf โ the format of Executable and Linking Format (ELF) files. periodt. ๐
โจ 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_Halfis twice as large asElf32_Half, andElf64Quarteris used foruint16_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 prefixEI_and may contain values which start with the prefixELF. The following macros are defined:EI_MAG0โ The first byte of the magic number. It must be filled withELFMAG0. (0:0x7f)EI_MAG1โ The second byte of the magic number. It must be filled withELFMAG1. (1:'E')EI_MAG2โ The third byte of the magic number. It must be filled withELFMAG2. (2:'L')EI_MAG3โ The fourth byte of the magic number. It must be filled withELFMAG3. (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 asELFOSABI_SYSVELFOSABI_SYSVโ UNIX System V ABIELFOSABI_HPUXโ HP-UX ABIELFOSABI_NETBSDโ NetBSD ABIELFOSABI_LINUXโ Linux ABIELFOSABI_SOLARISโ Solaris ABIELFOSABI_IRIXโ IRIX ABIELFOSABI_FREEBSDโ FreeBSD ABIELFOSABI_TRU64โ TRU64 UNIX ABIELFOSABI_ARMโ ARM architecture ABIELFOSABI_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 theEI_OSABIfield. 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 forEI_PADwill change in the future if currently unused bytes are given meanings.EI_NIDENTโ The size of thee_identarray.
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 machineEM_M32โ AT&T WE 32100EM_SPARCโ Sun Microsystems SPARCEM_386โ Intel 80386EM_68Kโ Motorola 68000EM_88Kโ Motorola 88000EM_860โ Intel 80860EM_MIPSโ MIPS RS3000 (big-endian only)EM_PARISCโ HP/PAEM_SPARC32PLUSโ SPARC with enhanced instruction setEM_PPCโ PowerPCEM_PPC64โ PowerPC 64-bitEM_S390โ IBM S/390EM_ARMโ Advanced RISC MachinesEM_SHโ Renesas SuperHEM_SPARCV9โ SPARC v9 64-bitEM_IA_64โ Intel ItaniumEM_X86_64โ AMD x86-64EM_VAXโ DEC Vax
e_versionโ This member identifies the file version:EV_NONEโ Invalid versionEV_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 formEF_machine_flag. The defined flags and their meanings depend on the target architecture and its ABI.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 ofe_phentsizeande_phnumgives the table's size in bytes. If a file has no program header,e_phnumholds 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 as0xffff, the largest numbere_phnumcan 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 ofe_shentsizeande_shnumgives the section header table's size in bytes. If a file has no section header table,e_shnumholds 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 valueSHN_UNDEF.
Okay so real talk about that last member, bestie, because it's the plot twist: if the index of section name string table section is larger than or equal to SHN_LORESERVE (0xff00), then this member just holds SHN_XINDEX (0xffff) instead, and the actual real-deal index of the section name string table section is hiding out in the sh_link member of the initial entry in section header table. ๐
Otherwise โ the boring timeline โ the sh_link member of the initial entry in section header table contains the value zero. That's it. That's the whole bit. โจ
๐ฆ Program header (Phdr) ๐ฆ
An executable or shared object file's program header table is literally just an array of structures, and each one describes a segment (or some other info the system needs to get the program ready to execute ๐). An object file segment contains one or more sections. And here's the vibe check: program headers are only meaningful for executable and shared object files, fr fr. A file specifies its own program header size with the ELF header's e_phentsize and e_phnum members. The ELF program header gets 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? It's giving "where does p_flags even live" ๐ โ the location of the p_flags member in the total struct. That's the whole diff. Slay.
โญ p_type โญ
This member of the structure indicates what kind of segment this array element describes, or how you're supposed to interpret the array element's information. Choose your fighter:
PT_NULLโ the array element is unused, and all the other members' values are undefined. It's the "don't perceive me" option ๐ and it lets the program header have ignored entries.PT_LOADโ the array element specifies a loadable segment, described byp_fileszandp_memsz. The bytes from the file get mapped to the beginning of the memory segment. If the segment's memory sizep_memszis larger than the file sizep_filesz, those "extra" bytes are defined to hold the value0and to follow the segment's initialized area. The file size may not be larger than the memory size, periodt. Loadable segment entries in the program header table appear in ascending order, sorted on thep_vaddrmember.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โ a GNU extension which is used by the Linux kernel to control the state of the stack via the flags set in thep_flagsmember. ๐ง
๐ 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, though? This member is not used and must be zero. Not optional, bestie. ๐
๐ 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. That's just their brand. ๐
๐ 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. It's 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, and e_shentsize holds the size in bytes of each entry. Three little numbers running the whole map, iconic honestly. ๐บ๏ธ
A section header table index is a subscript into this array. Some of those indices are reserved: the initial entry, and the indices between SHN_LORESERVE and SHN_HIRESERVE. The initial entry gets 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. And an object file doesn't have sections for these special indices:
SHN_UNDEFโ this value marks an undefined, missing, irrelevant, or otherwise meaningless section reference. It's the "no thoughts, head empty" reference. ๐SHN_LORESERVEโ this value specifies the lower bound of the range of reserved indices.SHN_LOPROC,SHN_HIPROCโ values 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 numberSHN_ABShas 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 betweenSHN_LORESERVEandSHN_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. Finally, some peace and quiet. ๐๏ธ
๐ฆ 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 section's contents and semantics. The lineup:
SHT_NULLโ this value marks the section header as inactive. It does not have an associated section, and the other members of the section header have undefined values. ๐SHT_PROGBITSโ this section holds information defined by the program, whose format and meaning are determined solely by the program.SHT_SYMTABโ this section holds a symbol table. Typically,SHT_SYMTABprovides symbols for link editing, though it may also be used for dynamic linking. As a complete symbol table, it may contain many symbols unnecessary for dynamic linking. An object file can also contain aSHT_DYNSYMsection. The index of the associated string table section can be found in thesh_linkmember. ๐SHT_STRTABโ this section holds a string table. An object file may have multiple string table sections.SHT_RELAโ this section holds relocation entries with explicit addends, such as typeElf32_Relafor the 32-bit class of object files. An object may have multiple relocation sections.SHT_HASHโ this section holds a symbol hash table. An object participating in dynamic linking must contain a symbol hash table. An object file may have only one hash table.SHT_DYNAMICโ this section holds information for dynamic linking. An object file may have only one dynamic section.SHT_NOTEโ this section holds notes (ElfN_Nhdr).SHT_NOBITSโ bestie this one is so iconic, it's giving illusionist โจ it takes up literally no space in the file but otherwise it's just likeSHT_PROGBITS. Like, no bytes at all, but thesh_offsetmember still holds the conceptual file offset. The way that it's a whole vibe with zero content ๐SHT_RELโ this one holds relocation offsets with no explicit addends, like the typeElf32_Relfor the 32-bit class of object files. And listen, one object file can absolutely have multiple relocation sections. Not me, not you, plural periodt.SHT_SHLIBโ reserved. Semantics unspecified. It's the mysterious quiet kid of section types ๐SHT_DYNSYMโ holds a minimal set of dynamic linking symbols (small but mighty ๐). And your object file can also contain aSHT_SYMTABsection at the same time, no cap.SHT_LOPROC<br>SHT_HIPROCโ every value in the inclusive range[SHT_LOPROC, SHT_HIPROC]is reserved for processor-specific semantics. That's the processor's little kingdom ๐ don't touch.SHT_LOUSERโ the lower bound of the range of indices reserved for application programs.SHT_HIUSERโ the upper bound of the range of indices reserved for application programs. Section types betweenSHT_LOUSERandSHT_HIUSERare yours to use, app dev ๐, and you won't conflict with any current or future system-defined section types. Slay.
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 just doesn't apply. Undefined attributes are set to zero. Simple as that, fr fr.
SHF_WRITEโ this section contains data that should be writable during process execution.SHF_ALLOCโ this section occupies memory during process execution. Some control sections do not reside in the memory image of an object file, and for those sections this attribute is off. Lowkey exclusive ๐ฆSHF_EXECINSTRโ this section contains executable machine instructions. Code o'clock.SHF_MASKPROCโ every bit included in this mask is reserved for processor-specific semantics. Processor business only ๐
sh_addr ๐
If this section appears in the memory image of a process, this member holds the address where the section's first byte should live. Otherwise, the member is just zero. Nothing there, no address, no drama.
sh_offset ๐
This member's value holds the byte offset from the beginning of the file to the first byte in the section. BUT โ and this is the plot twist โ one section type, SHT_NOBITS, occupies no space in the file at all, and its sh_offset member just locates the conceptual placement in the file. Conceptual ๐ it's giving theoretical.
sh_size ๐
This member holds the section's size in bytes. Unless the section type is SHT_NOBITS, the section occupies sh_size bytes in the file. And a section of type SHT_NOBITS may have a nonzero size, but it still occupies no space in the file. Make it make sense โ it's the ghost of sections ๐ป
sh_link ๐
This member holds a section header table index link, and how you interpret it depends on the section type. Situation-based, iykyk.
sh_info โน๏ธ
This member holds extra information, and again โ interpretation depends on the section type. Read the room bestie.
sh_addralign ๐งฒ
Some sections have address alignment constraints. If a section holds a doubleword, the system must make sure there's doubleword alignment for the whole entire section. That means 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. And the value 0 or 1 means the section has no alignment constraints. Freedom. โจ
sh_entsize ๐งฎ
Some sections hold a table of fixed-sized entries, like a symbol table. For a section like that, this member gives the size in bytes for each entry. And if the section does not hold a table of fixed-size entries, this member contains zero. Periodt.
Various sections hold program and control information: ๐๏ธ collect them all bestie
.bssโ this section holds uninitialized data that contributes to the program's memory image. By definition, the system initializes the data with zeros when the program begins to run. This section is of typeSHT_NOBITS. The attribute types areSHF_ALLOCandSHF_WRITE. Zeroed at the start, every single time ๐.commentโ holds version control information. This section is of typeSHT_PROGBITS. No attribute types are used..ctorsโ holds initialized pointers to the C++ constructor functions. This section is of typeSHT_PROGBITS. The attribute types areSHF_ALLOCandSHF_WRITE. Construction era ๐๏ธ.dataโ holds initialized data that contribute to the program's memory image. This section is of typeSHT_PROGBITS. The attribute types areSHF_ALLOCandSHF_WRITE..data1โ also holds initialized data that contribute to the program's memory image. This section is of typeSHT_PROGBITS. The attribute types areSHF_ALLOCandSHF_WRITE. Same energy, different name ๐.debugโ holds information for symbolic debugging. The contents are unspecified. This section is of typeSHT_PROGBITS. No attribute types are used. Mysterious debugger fuel ๐.dtorsโ holds initialized pointers to the C++ destructor functions. This section is of typeSHT_PROGBITS. The attribute types areSHF_ALLOCandSHF_WRITE. The end-of-era cleanup crew ๐งน.dynamicโ holds dynamic linking information. The section's attributes will include theSHF_ALLOCbit. Whether theSHF_WRITEbit is set is processor-specific. This section is of typeSHT_DYNAMIC. See the attributes above ๐.dynstrโ holds strings needed for dynamic linking, most commonly the strings that represent the names associated with symbol table entries. This section is of typeSHT_STRTAB. The attribute type used isSHF_ALLOC..dynsymโ holds the dynamic linking symbol table. This section is of typeSHT_DYNSYM. The attribute used isSHF_ALLOC..finiโ 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 typeSHT_PROGBITS. The attributes used areSHF_ALLOCandSHF_EXECINSTR. The farewell tour ๐.gnu.versionโ holds the version symbol table, an array ofElfN_Halfelements. This section is of typeSHT_GNU_versym. The attribute type used isSHF_ALLOC..gnu.version_dโ holds the version symbol definitions, a table ofElfN_Verdefstructures. This section is of typeSHT_GNU_verdef. The attribute type used isSHF_ALLOC..gnu.version_rโ holds the version symbol needed elements, a table ofElfN_Verneedstructures. This section is of typeSHT_GNU_versym. The attribute type used isSHF_ALLOC..gotโ holds the global offset table. This section is of typeSHT_PROGBITS. The attributes are processor-specific..hashโ holds a symbol hash table. This section is of typeSHT_HASH. The attribute used isSHF_ALLOC..initโ 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 typeSHT_PROGBITS. The attributes used areSHF_ALLOCandSHF_EXECINSTR. Opening act before the main character walks in ๐.interpโ holds the pathname of a program interpreter. If the file has a loadable segment that includes the section, the section's attributes will include theSHF_ALLOCbit. Otherwise, that bit will be off. This section is of typeSHT_PROGBITS..lineโ 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 typeSHT_PROGBITS. No attribute types are used..noteโ holds various notes. This section is of typeSHT_NOTE. No attribute types are used. Little sticky notes everywhere ๐๏ธ.note.ABI-tagโ used to declare the expected run-time ABI of the ELF image. It may include the operating system name and its run-time versions. This section is of typeSHT_NOTE. The only attribute used isSHF_ALLOC..note.gnu.build-idโ used to hold an ID that uniquely identifies the contents of the ELF image. Different files with the same build ID should contain the same executable content. See the--build-idoption to the GNU linker (ld(1)) for more details. This section is of typeSHT_NOTE. The only attribute used isSHF_ALLOC. A fingerprint for your binary ๐ซถ.note.GNU-stackโ used in Linux object files for declaring stack attributes. This section is of typeSHT_PROGBITS. The only attribute used isSHF_EXECINSTR. This indicates to the GNU linker that the object file requires an executable stack.
โ ๏ธ okay so heads up bestie โ an executable stack is a legit security concern, so know what you're doing before you ship that.
.note.openbsd.identโ OpenBSD native executables usually contain this section to identify themselves, so the kernel can bypass any compatibility ELF binary emulation tests when loading the file. An ID card for the kernel ๐.pltโ holds the procedure linkage table. This section is of typeSHT_PROGBITS. The attributes are processor-specific..relNAMEโ holds relocation information as described below. If the file has a loadable segment that includes relocation, the section's attributes will include theSHF_ALLOCbit. Otherwise, the bit will be off. By convention, "NAME" is supplied by the section to which the relocations apply. So a relocation section for.textwould normally have the name.rel.text. This section is of typeSHT_REL. Names that name names ๐ธ.relaNAMEโ holds relocation information as described below. If the file has a loadable segment that includes relocation, the section's attributes will include theSHF_ALLOCbit. Otherwise, the bit will be off. By convention, "NAME" is supplied by the section to which the relocations apply. So a relocation section for.textwould normally have the name.rela.text. This section is of typeSHT_RELA..rodataโ read-only data that's usually feeding into a nonwritable segment in the process image. Type:SHT_PROGBITS. Attribute:SHF_ALLOC. it's giving "look but don't touch" โจ.rodata1โ same energy as.rodatafr: read-only data, typically contributing to a nonwritable segment in the process image. Type:SHT_PROGBITS. Attribute:SHF_ALLOC..shstrtabโ bestie this one holds the section names themselves. Type:SHT_STRTAB. No attribute types used at all, zero, nada..strtabโ holds strings, most commonly the strings that are the names attached to symbol table entries. Here's the tea tho: if the file has a loadable segment that includes the symbol string table, then the section's attributes will include theSHF_ALLOCbit. Otherwise the bit is off. Type:SHT_STRTAB..symtabโ holds a symbol table. Same rules as above: if the file has a loadable segment that includes the symbol table, the section's attributes will include theSHF_ALLOCbit. Otherwise, off. Type:SHT_SYMTAB..textโ the "text", aka the actual executable instructions of a program. Type:SHT_PROGBITS. Attributes used:SHF_ALLOCandSHF_EXECINSTR. this is literally where the code lives ๐
๐ธ String and symbol tables ๐ธ
String table sections hold null-terminated character sequences, commonly called strings. The object file uses these strings to represent symbol and section names, periodt. You reference a string as an index into the string table section. The first byte โ that's index zero btw โ is defined to hold a null byte ('\0'). And similarly, a string table's last byte is defined to hold a null byte too, which guarantees null termination for all strings. iconic behavior, honestly. ๐
An object file's symbol table holds the info needed to locate and relocate a program's symbolic definitions and references. A symbol table index is a subscript into this array.
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 different order. that's it, that's the whole difference ๐ญ
st_nameโ holds an index into the object file's symbol string table, which holds the character representations of the symbol names. If the value is nonzero, it's a string table index that gives you the symbol name. Otherwise, the symbol has no name. nameless. mysterious. ๐ฆst_valueโ 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โ specifies the symbol's type and binding attributes:STT_NOTYPEโ the symbol's type is not defined.STT_OBJECTโ the symbol is associated with a data object.STT_FUNCโ the symbol is associated with a function or other executable code.STT_SECTIONโ the symbol is associated with a section. Symbol table entries of this type exist primarily for relocation, and normally haveSTB_LOCALbindings.STT_FILEโ by convention, the symbol's name gives the name of the source file associated with the object file. A file symbol hasSTB_LOCALbindings, its section index isSHN_ABS, and it precedes the otherSTB_LOCALsymbols of the file, if it is present. it goes first, it's the main character ๐STT_LOPROC,STT_HIPROCโ values in the inclusive range[STT_LOPROC, STT_HIPROC]are reserved for processor-specific semantics.STB_LOCALโ local symbols are not visible outside the object file containing their definition. Local symbols of the same name may exist in multiple files without interfering with each other. no drama, no overlap, we love that ๐ซถSTB_GLOBALโ global symbols are visible to all object files being combined. One file's definition of a global symbol will satisfy another file's undefined reference to the same symbol.STB_WEAKโ weak symbols resemble global symbols, but their definitions have lower precedence.STB_LOPROC,STB_HIPROCโ values in the inclusive range[STB_LOPROC, STB_HIPROC]are reserved for processor-specific semantics.
There are macros for packing and unpacking the binding and type fields:
ELF32_ST_BIND(info),ELF64_ST_BIND(info)โ extract a binding from anst_infovalue.ELF32_ST_TYPE(info),ELF64_ST_TYPE(info)โ extract a type from anst_infovalue.ELF32_ST_INFO(bind, type),ELF64_ST_INFO(bind, type)โ convert a binding and a type into anst_infovalue.
st_otherโ defines the symbol visibility:STV_DEFAULTโ default symbol visibility rules. Global and weak symbols are available to other modules; references in the local module can be interposed by definitions in other modules.STV_INTERNALโ processor-specific hidden class.STV_HIDDENโ symbol is unavailable to other modules; references in the local module always resolve to the local symbol (i.e., the symbol can't be interposed by definitions in other modules). it said no and meant it ๐STV_PROTECTEDโ symbol is available to other modules, but references in the local module always resolve to the local symbol.
There are macros for extracting the visibility type:
ELF32_ST_VISIBILITY(other)orELF64_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 entries (Rel & Rela) ๐ฅ
Relocation is the process of connecting symbolic references with symbolic definitions. Relocatable files have to carry information describing how to modify their section contents โ that's what lets executable and shared object files hold the right info for a process's program image. Relocation entries are exactly that data. it's the matchmaker of the ELF world ngl ๐
Relocation structures that don't 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 do 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โ 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โ 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 entry's relocation type or symbol table index, it means the result of applyingELF[32|64]_R_TYPEorELF[32|64]_R_SYM, respectively, to the entry'sr_infomember.r_addendโ specifies a constant addend used to compute the value to be stored into the relocatable field.
โจ Dynamic tags (Dyn) โจ
The .dynamic section contains a series of structures holding relevant dynamic linking information. The d_tag member controls the interpretation of d_un. the way one field decides the โจvibeโจ of the other ๐
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:DT_NULLโ marks end of dynamic sectionDT_NEEDEDโ string table offset to name of a needed libraryDT_PLTRELSZโ size in bytes of PLT relocation entriesDT_PLTGOTโ address of PLT and/or GOTDT_HASHโ address of symbol hash tableDT_STRTABโ address of string tableDT_SYMTABโ address of symbol tableDT_RELAโ address of Rela relocation tableDT_RELASZโ size in bytes of the Rela relocation tableDT_RELAENTโ size in bytes of a Rela relocation table entryDT_STRSZโ size in bytes of string tableDT_SYMENTโ size in bytes of a symbol table entryDT_INITโ address of the initialization functionDT_FINIโ address of the termination functionDT_SONAMEโ string table offset to name of shared objectDT_RPATHโ string table offset to search path for direct and indirect library dependenciesDT_SYMBOLICโ alert linker to search this shared object before the executable for symbolsDT_RELโ address of Rel relocation tableDT_RELSZโ size in bytes of Rel relocation tableDT_RELENTโ size in bytes of a Rel table entryDT_PLTRELโ type of relocation entry to which the PLT refers (Rela or Rel)DT_DEBUGโ undefined use for debuggingDT_TEXTRELโ absence of this entry indicates that no relocation entries should apply to a nonwritable segment
DT_JMPRELโ the address of relocation entries that are solely about the PLT, nothing else ๐DT_BIND_NOWโ tells the dynamic linker to process all relocations before handing control over to the executable. no procrastinating bestie โจDT_RUNPATHโ a string table offset pointing at the search path for direct library dependencies ๐ฆDT_LOPROC,DT_HIPROCโ every value in the inclusive range[DT_LOPROC, DT_HIPROC]is reserved for processor-specific semantics. not for us, that's their lane ๐d_valโ this member holds integer values, and those values get interpreted in various ways depending on context. it's giving multitudes ๐d_ptrโ this member holds program virtual addresses. when you interpret these bad boys, the actual address has to be computed from the original file value plus the memory base address. and listen: files do not contain relocation entries to fix up these addresses. so like, they stay as they are, no glow-up pass ๐ญ_DYNAMICโ an array holding all the dynamic structures in the.dynamicsection. the linker populates this automatically, you don't have to do a thing ๐ซถ
Notes (Nhdr) ๐
ELF notes let you append arbitrary information for the system to use. they're mostly a core-file thing (e_type of ET_CORE), but tons of projects define their own extension sets. for instance, the GNU tool chain uses ELF notes to pass info from the linker to the C library. adorable of them ๐
A note section holds a series of notes (struct definitions are right below, don't worry). each note is followed by the name field โ its length lives in n_namesz โ and then by the descriptor field โ length lives in n_descsz โ and that descriptor's starting address has a 4 byte alignment. neither field is declared in the note struct itself because their lengths are, like, whatever they feel like. arbitrary. iconic. โจ
okay here's an example that parses out two consecutive notes, so you can see how they sit in memory:
void *memory, *name, *desc;
Elf64_Nhdr *note, *next_note;
/* The buffer is pointing to the start of the section/segment. */
note = memory;
/* If the name is defined, it follows the note. */
name = note->n_namesz == 0 ? NULL : memory + sizeof(*note);
/* If the descriptor is defined, it follows the name
(with alignment). */
desc = note->n_descsz == 0 ? NULL :
memory + sizeof(*note) + ALIGN_UP(note->n_namesz, 4);
/* The next note follows both (with alignment). */
next_note = memory + sizeof(*note) +
ALIGN_UP(note->n_namesz, 4) +
ALIGN_UP(note->n_descsz, 4);
โ ๏ธ keep in mind that how you interpret n_type depends on the namespace defined by the n_namesz field. if n_namesz is not set (say it's 0), then there are two sets of notes in play: one for core files, and one for all other ELF types. and if the namespace is unknown, tools will usually fall back to those same sets of notes anyway. blessed fallback behaviour ๐
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 sit immediately after this note in memory. the name is null terminated. for example, if the name is"GNU", thenn_nameszgets set to4. (yes, 4, because of the null terminator bestie, count it) ๐n_descszโ the length of the descriptor field, in bytes. its contents sit immediately after the name field in memory ๐n_typeโ depending on the value of the name field, this member can take any of the following values:
Core files (e_type = ET_CORE) ๐
Notes used by all core files. these are highly operating system or architecture specific, and they often need close coordination with kernels, C libraries, and debuggers. they're used when the namespace is the default (i.e., n_namesz will be set to 0), or as a fallback when the namespace is unknown.
NT_PRSTATUS prstatus struct
NT_FPREGSET fpregset struct
NT_PRPSINFO prpsinfo struct
NT_PRXREG prxregset struct
NT_TASKSTRUCT task structure
NT_PLATFORM String from sysinfo(SI_PLATFORM)
NT_AUXV auxv array
NT_GWINDOWS gwindows struct
NT_ASRS asrset struct
NT_PSTATUS pstatus struct
NT_PSINFO psinfo struct
NT_PRCRED prcred struct
NT_UTSNAME utsname struct
NT_LWPSTATUS lwpstatus struct
NT_LWPSINFO lwpinfo struct
NT_PRFPXREG fprxregset struct
NT_SIGINFO siginfo_t (size might increase over
time)
NT_FILE Contains information about mapped files
NT_PRXFPREG user_fxsr_struct
NT_PPC_VMX PowerPC Altivec/VMX registers
NT_PPC_SPE PowerPC SPE/EVR registers
NT_PPC_VSX PowerPC VSX registers
NT_386_TLS i386 TLS slots (struct user_desc)
NT_386_IOPERM x86 io permission bitmap (1=deny)
NT_X86_XSTATE x86 extended state using xsave
NT_S390_HIGH_GPRS s390 upper register halves
NT_S390_TIMER s390 timer register
NT_S390_TODCMP s390 time-of-day (TOD) clock comparator
register
NT_S390_TODPREG s390 time-of-day (TOD) programmable
register
NT_S390_CTRS s390 control registers
NT_S390_PREFIX s390 prefix register
NT_S390_LAST_BREAK s390 breaking event address
NT_S390_SYSTEM_CALL s390 system call restart data
NT_S390_TDB s390 transaction diagnostic block
NT_ARM_VFP ARM VFP/NEON registers
NT_ARM_TLS ARM TLS register
NT_ARM_HW_BREAK ARM hardware breakpoint registers
NT_ARM_HW_WATCH ARM hardware watchpoint registers
NT_ARM_SYSTEM_CALL ARM system call number
n_name = GNU ๐
Extensions used by the GNU tool chain.
NT_GNU_ABI_TAGโ operating system (OS) ABI information. thedescfield will be 4 words:[0]โ OS descriptor (ELF_NOTE_OS_LINUX,ELF_NOTE_OS_GNU, and so on)[1]โ major version of the ABI[2]โ minor version of the ABI[3]โ subminor version of the ABI
NT_GNU_HWCAPโ synthetic hwcap information. thedescfield begins with two words:[0]โ number of entries[1]โ bit mask of enabled entries
Then follow variable-length entries, one byte followed by a null-terminated hwcap name string. that byte gives the bit number to test if enabled, (1U << bit) & bit mask. the way that math works is honestly so satisfying ๐
NT_GNU_BUILD_IDโ unique build ID, as generated by the GNUld(1)--build-idoption. thedescconsists of any nonzero number of bytes โจNT_GNU_GOLD_VERSIONโ thedesccontains the GNU Gold linker version that was used.
Default/unknown namespace (e_type != ET_CORE) ๐
These get used when the namespace is the default (i.e., n_namesz will be set to 0), or as a fallback when the namespace is unknown.
NT_VERSIONโ a version string of some sort.NT_ARCHโ architecture information.
NOTES ๐ง
ELF first showed up in System V. the ELF format is an adopted standard, no cap.
The extensions for e_phnum, e_shnum, and e_shstrndx respectively are Linux extensions. Sun, BSD, and AMD64 also support them; for further info, look under SEE ALSO bestie ๐
SEE ALSO ๐ธ
as(1), elfedit(1), gdb(1), ld(1), nm(1), objcopy(1), objdump(1), patchelf(1), readelf(1), size(1), strings(1), strip(1), execve(2), dl_iterate_phdr(3), core(5), ld.so(8)
- 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).
- Sun Microsystems, Linker and Libraries Guide.
- AMD64 ABI Draft, System V Application Binary Interface AMD64 Architecture Processor Supplement.
and that's a wrap on ELF(5) ๐ this whole page came from Linux man-pages 6.20, dated 2026-08-07 โ go thank the man-pages crew, they ate and left no crumbs ๐โจ bye bestie, go read some section headers ๐
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- Copyright 1999, Jeroen Ruigrok van der Werven
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms