ELF Header

#define EI_NIDENT 16

typedef struct {
        unsigned char   e_ident[EI_NIDENT]; // 16 bytes
        Elf32_Half      e_type;       // 2 bytes
        Elf32_Half      e_machine;    // 2 bytes
        Elf32_Word      e_version;    // 4 bytes
        Elf32_Addr      e_entry;      // 4 bytes
        Elf32_Off       e_phoff;      // 4 bytes
        Elf32_Off       e_shoff;      // 4 bytes
        Elf32_Word      e_flags;      // 4 bytes
        Elf32_Half      e_ehsize;     // 2 bytes
        Elf32_Half      e_phentsize;  // 2 bytes
        Elf32_Half      e_phnum;      // 2 bytes
        Elf32_Half      e_shentsize;  // 2 bytes
        Elf32_Half      e_shnum;      // 2 bytes
        Elf32_Half      e_shstrndx;   // 2 bytes
} Elf32_Ehdr; // 52 bytes

typedef struct {
        unsigned char   e_ident[EI_NIDENT]; // 16 bytes
        Elf64_Half      e_type;      // 2 bytes
        Elf64_Half      e_machine;   // 2 bytes
        Elf64_Word      e_version;   // 4 bytes
        Elf64_Addr      e_entry;     // 8 bytes
        Elf64_Off       e_phoff;     // 8 bytes
        Elf64_Off       e_shoff;     // 8 bytes
        Elf64_Word      e_flags;     // 4 bytes
        Elf64_Half      e_ehsize;    // 2 bytes
        Elf64_Half      e_phentsize; // 2 bytes
        Elf64_Half      e_phnum;     // 2 bytes
        Elf64_Half      e_shentsize; // 2 bytes
        Elf64_Half      e_shnum;     // 2 bytes
        Elf64_Half      e_shstrndx;  // 2 bytes
} Elf64_Ehdr; // 64 bytes

ELF Identification / e_ident

The initial bytes mark the file as an object file and provide machine-independent data with which to decode and interpret the file's contents.

e_ident[] indexes and possible values:

Name           Value  Purpose              Possible e_ident[EI_*] value
EI_MAG0        0      File identification  ELFMAG0 / 0x7f
EI_MAG1        1      File identification  ELFMAG1 / 'E'
EI_MAG2        2      File identification  ELFMAG1 / 'L'
EI_MAG3        3      File identification  ELFMAG3 / 'F'
--------------------------------------------------------------------------------
EI_CLASS       4      File class [1]       ELFCLASSNONE / 0 / Invalid class
                                           ELFCLASS32   / 1 / 32-bit objects
                                           ELFCLASS64   / 2 / 64-bit objects

EI_DATA        5      Data encoding [2]    ELFDATANONE  / 0 / Invalid data
                                                              encoding
                                           ELFDATA2LSB  / 1 / Little endian
                                           ELFDATA2MSB  / 2 / Big endian

EI_VERSION     6      ELF header version   EV_CURRENT [3]

EI_OSABI       7      OS/ABI-specific ELF  ELFOSABI_NONE / 0 / No extensions or
                      extension identifi-                      unspecified
                      cation [4]           ELFOSABI_HPUX / 1 / Hewlett-Packard
                                                               HP-UX
                                           ...
                                           / 64 - 255 / Architecture-specific
                                                        value range
--------------------------------------------------------------------------------
EI_ABIVERSION  8      ABI version [5]
EI_PAD         9      Start index of pad-  0
                      ding bytes
               10-15  Indexes of padding   0
                      bytes
EI_NIDENT      16     Size of e_ident[]

1 File Class

2 Data Encoding

Primarily for the convenience of code that looks at the ELF file at runtime, the ELF data structures are intended to have the same byte order as that of the running program.

3 EV_CURRENT

Currently, e_ident[EI_VERSION] must be EV_CURRENT, as explained below for e_version.

4 OS/ABI-specific ELF Extension

Some fields in other ELF structures have flags and values that have operating system and/or ABI specific meanings; the interpretation of those fields is determined by the value of this byte.

5 ABI Version

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.

If no values are specified for the EI_OSABI field by the processor supplement or no version values are specified for the ABI determined by a particular value of the EI_OSABI byte, the value 0 shall be used for the EI_ABIVERSION byte; it indicates unspecified.

Other Members

Name         Size   Purpose           Possible value
e_type [1]   2 B    Object file type  ET_NONE   / 0 / No file type
                                      ET_REL    / 1 / Relocatable file
                                      ET_EXEC   / 2 / Executable file
                                      ET_DYN    / 3 / Shared object file
                                      ET_CORE   / 4 / Core file

                                      ET_LOOS   / 0xfe00 / OS-specific
                                      ET_HIOS   / 0xfeff / OS-specific
                                      ET_LOPROC / 0xff00 / Processor-specific
                                      ET_HIPROC / 0xffff / Processor-specific

e_machine    2 B    Specifies archi-  EM_NONE    / 0   / No machine
                    tecture           EM_386     / 3   / Intel 80386
                                      EM_ARM     / 40  / ARM 32-bit architecture
                                                         (AARCH32)
                                      EM_X86_64  / 62  / AMD x86-64 architecture
                                      EM_AARCH64 / 183 / ARM 64-bit architecture
                                                         (AARCH64)
                                      EM_CUDA    / 190 / NV CUDA architecture
                                      EM_AMDGPU  / 224 / AMD GPU architecture
                                      EM_RISCV   / 243 / RISC-V

e_version    4 B    Object file version  EV_NONE    / 0 / Invalid version
[2]                                      EV_CURRENT / 1 / Current version

e_entry      4/8 B  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      4/8 B  The program header table's file offset in bytes.
                    If the file has no program header table, this member holds
                    zero.

e_shoff      4/8 B  The section header table's file offset in bytes.
                    If the file has no section header table, this member holds
                    zero.

e_flags [5]  4 B    Processor-specific flags associated with the file.
                    Flag names take the form EF_machine_flag.

e_ehsize     2 B    ELF header's size in bytes  52 / 64 B

e_phentsize  2 B    The size in bytes of one entry in program header table
                    If a file has no program header table, it holds the value
                    zero.

e_phnum      2 B    The number of entries in the program header table
                    If a file has no program header table, e_phnum holds the
                    value zero.

e_shentsize  2 B    Similar to e_phentsize      40 / 64 B

e_shnum [3]  2 B    Similar to e_phnum

e_shstrndx   2 B    The section header table index of the entry associated with
[4]                 the section name string table.
                    If the file has no section name string table, this member
                    holds the value SHN_UNDEF.

1 e_type

2 e_version

The value 1 signifies the original file format; extensions will create new versions with higher numbers.

Although the value of EV_CURRENT is shown as 1 in the previous table, it will change as necessary to reflect the current version number.

3 e_shnum

4 e_shstrndx

5 e_flags for RISC-V