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_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. 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 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.

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 by p_filesz and p_memsz. The bytes from the file get mapped to the beginning of the memory segment. If the segment's memory size p_memsz is larger than the file size p_filesz, those "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, periodt. 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 โ€” a 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, 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 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. 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_SYMTAB provides 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 a SHT_DYNSYM section. The index of the associated string table section can be found in the sh_link member. ๐Ÿ”—
  • 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 type Elf32_Rela for 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 like SHT_PROGBITS. Like, no bytes at all, but the sh_offset member 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 type Elf32_Rel for 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 a SHT_SYMTAB section 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 between SHT_LOUSER and SHT_HIUSER are 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 ๐Ÿ‘ป

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 type SHT_NOBITS. The attribute types are SHF_ALLOC and SHF_WRITE. Zeroed at the start, every single time ๐Ÿ’œ
  • .comment โ€” holds version control information. This section is of type SHT_PROGBITS. No attribute types are used.
  • .ctors โ€” holds initialized pointers to the C++ constructor functions. This section is of type SHT_PROGBITS. The attribute types are SHF_ALLOC and SHF_WRITE. Construction era ๐Ÿ—๏ธ
  • .data โ€” holds initialized data that contribute to the program's memory image. This section is of type SHT_PROGBITS. The attribute types are SHF_ALLOC and SHF_WRITE.
  • .data1 โ€” also holds initialized data that contribute to the program's memory image. This section is of type SHT_PROGBITS. The attribute types are SHF_ALLOC and SHF_WRITE. Same energy, different name ๐Ÿ’…
  • .debug โ€” holds information for symbolic debugging. The contents are unspecified. This section is of type SHT_PROGBITS. No attribute types are used. Mysterious debugger fuel ๐Ÿ›
  • .dtors โ€” holds initialized pointers to the C++ destructor functions. This section is of type SHT_PROGBITS. The attribute types are SHF_ALLOC and SHF_WRITE. The end-of-era cleanup crew ๐Ÿงน
  • .dynamic โ€” holds dynamic linking information. The section's 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 โ€” 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 โ€” holds the dynamic linking symbol table. This section is of type SHT_DYNSYM. The attribute used is SHF_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 type SHT_PROGBITS. The attributes used are SHF_ALLOC and SHF_EXECINSTR. The farewell tour ๐ŸŽ€
  • .gnu.version โ€” 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 โ€” 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 โ€” 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 โ€” holds the global offset table. This section is of type SHT_PROGBITS. The attributes are processor-specific.
  • .hash โ€” holds a symbol hash table. This section is of type SHT_HASH. The attribute used is SHF_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 type SHT_PROGBITS. The attributes used are SHF_ALLOC and SHF_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 the SHF_ALLOC bit. Otherwise, that bit will be off. This section is of type SHT_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 type SHT_PROGBITS. No attribute types are used.
  • .note โ€” holds various notes. This section is of type SHT_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 type SHT_NOTE. The only attribute used is SHF_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-id option to the GNU linker (ld(1)) for more details. This section is of type SHT_NOTE. The only attribute used is SHF_ALLOC. A fingerprint for your binary ๐Ÿซถ
  • .note.GNU-stack โ€” 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.
โš ๏ธ 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 type SHT_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 the SHF_ALLOC bit. Otherwise, the bit will be off. By convention, "NAME" is supplied by the section to which the relocations apply. So a relocation section for .text would normally have the name .rel.text. This section is of type SHT_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 the SHF_ALLOC bit. Otherwise, the bit will be off. By convention, "NAME" is supplied by the section to which the relocations apply. So a relocation section for .text would normally have the name .rela.text. This section is of type SHT_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 .rodata fr: 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 the SHF_ALLOC bit. 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 the SHF_ALLOC bit. Otherwise, off. Type: SHT_SYMTAB.
  • .text โ€” the "text", aka the actual executable instructions of a program. Type: SHT_PROGBITS. Attributes used: SHF_ALLOC and SHF_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 have STB_LOCAL bindings.
    • STT_FILE โ€” by convention, the symbol's name gives the name of the source file associated with the object file. A file symbol has STB_LOCAL bindings, its section index is SHN_ABS, and it precedes the other STB_LOCAL symbols 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 an st_info value.
  • ELF32_ST_TYPE(info), ELF64_ST_TYPE(info) โ€” extract a type from an st_info value.
  • ELF32_ST_INFO(bind, type), ELF64_ST_INFO(bind, type) โ€” convert a binding and a type into an st_info value.
  • 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) 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 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 applying ELF[32|64]_R_TYPE or ELF[32|64]_R_SYM, respectively, to the entry's r_info member.
  • 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 section
    • DT_NEEDED โ€” string table offset to name of a needed library
    • DT_PLTRELSZ โ€” size in bytes of PLT relocation entries
    • DT_PLTGOT โ€” address of PLT and/or GOT
    • DT_HASH โ€” address of symbol hash table
    • DT_STRTAB โ€” address of string table
    • DT_SYMTAB โ€” address of symbol table
    • DT_RELA โ€” address of Rela relocation table
    • DT_RELASZ โ€” size in bytes of the Rela relocation table
    • DT_RELAENT โ€” size in bytes of a Rela relocation table entry
    • DT_STRSZ โ€” size in bytes of string table
    • DT_SYMENT โ€” size in bytes of a symbol table entry
    • DT_INIT โ€” address of the initialization function
    • DT_FINI โ€” address of the termination function
    • DT_SONAME โ€” string table offset to name of shared object
    • DT_RPATH โ€” string table offset to search path for direct and indirect library dependencies
    • DT_SYMBOLIC โ€” alert linker to search this shared object before the executable for symbols
    • DT_REL โ€” address of Rel relocation table
    • DT_RELSZ โ€” size in bytes of Rel relocation table
    • DT_RELENT โ€” size in bytes of a Rel table entry
    • DT_PLTREL โ€” type of relocation entry to which the PLT refers (Rela or Rel)
    • DT_DEBUG โ€” undefined use for debugging
    • DT_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 .dynamic section. 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", then n_namesz gets set to 4. (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. the desc field 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. the desc field 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 GNU ld(1) --build-id option. the desc consists of any nonzero number of bytes โœจ
  • NT_GNU_GOLD_VERSION โ€” the desc contains 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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms