【ELF形式】ELFヘッダ(1)

ELF形式について、徹底的に調べ尽くしてみます。

ELFヘッダの構造体の定義は、「/usr/include/elf.h」に、データ型の定義は「/usr/include/stdint.h」でそれぞれ定義されています。

/* /usr/include/elf.h */
  26 /* Standard ELF types.  */
  27 
  28 #include <stdint.h>
  29 
  30 /* Type for a 16-bit quantity.  */
  31 typedef uint16_t Elf32_Half;
  32 typedef uint16_t Elf64_Half;
  33 
  34 /* Types for signed and unsigned 32-bit quantities.  */
  35 typedef uint32_t Elf32_Word;
  36 typedef int32_t  Elf32_Sword;
  37 typedef uint32_t Elf64_Word;
  38 typedef int32_t  Elf64_Sword;
  39 
  40 /* Types for signed and unsigned 64-bit quantities.  */
  41 typedef uint64_t Elf32_Xword;
  42 typedef int64_t  Elf32_Sxword;
  43 typedef uint64_t Elf64_Xword;
  44 typedef int64_t  Elf64_Sxword;
  45 
  46 /* Type of addresses.  */
  47 typedef uint32_t Elf32_Addr;
  48 typedef uint64_t Elf64_Addr;
  49 
  50 /* Type of file offsets.  */
  51 typedef uint32_t Elf32_Off;
  52 typedef uint64_t Elf64_Off;
  53 
  54 /* Type for section indices, which are 16-bit quantities.  */
  55 typedef uint16_t Elf32_Section;
  56 typedef uint16_t Elf64_Section;
  57 
  58 /* Type for version symbol information.  */
  59 typedef Elf32_Half Elf32_Versym;
  60 typedef Elf64_Half Elf64_Versym;
  61 
  62 
  63 /* The ELF file header.  This appears at the start of every ELF file.  */
  64 
  65 #define EI_NIDENT (16)
  66
  67 typedef struct
  68 {
  69   unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
  70   Elf32_Half    e_type;         /* Object file type */
  71   Elf32_Half    e_machine;      /* Architecture */
  72   Elf32_Word    e_version;      /* Object file version */
  73   Elf32_Addr    e_entry;        /* Entry point virtual address */
  74   Elf32_Off e_phoff;        /* Program header table file offset */
  75   Elf32_Off e_shoff;        /* Section header table file offset */
  76   Elf32_Word    e_flags;        /* Processor-specific flags */
  77   Elf32_Half    e_ehsize;       /* ELF header size in bytes */
  78   Elf32_Half    e_phentsize;        /* Program header table entry size */
  79   Elf32_Half    e_phnum;        /* Program header table entry count */
  80   Elf32_Half    e_shentsize;        /* Section header table entry size */
  81   Elf32_Half    e_shnum;        /* Section header table entry count */
  82   Elf32_Half    e_shstrndx;     /* Section header string table index */
  83 } Elf32_Ehdr;
/* /usr/include/stdint.h */
 47 /* Unsigned.  */
 48 typedef unsigned char       uint8_t;
 49 typedef unsigned short int  uint16_t;
 50 #ifndef __uint32_t_defined
 51 typedef unsigned int        uint32_t;
 52 # define __uint32_t_defined
 53 #endif
 54 #if __WORDSIZE == 64
 55 typedef unsigned long int   uint64_t;
 56 #else
 57 __extension__
 58 typedef unsigned long long int  uint64_t;
 59 #endif

ELFヘッダのサイズは
sizeof(e_ident) + sizeof(Elf32_Ehdr) = 合計52バイトとなる。

offset symbol type len
0 e_ident unsigned char [16] 16
offset Elf32_Ehdr type typedef typedef len
16 e_type Elf32_Half uint16_t unsigned short int 2
18 e_machine Elf32_Half uint16_t unsigned short int 2
20 e_version Elf32_Word uint32_t unsigned int 4
24 e_entry Elf32_Addr uint32_t unsigned int 4
28 e_phoff Elf32_Off uint32_t unsigned int 4
32 e_shoff Elf32_Off uint32_t unsigned int 4
36 e_flags Elf32_Word uint32_t unsigned int 4
40 e_ehsize Elf32_Half uint16_t unsigned short int 2
42 e_phentsize Elf32_Half uint16_t unsigned short int 2
44 e_phnum Elf32_Half uint16_t unsigned short int 2
46 e_shentsize Elf32_Half uint16_t unsigned short int 2
48 e_shnum Elf32_Half uint16_t unsigned short int 2
50 e_shstrndx Elf32_Half uint16_t unsigned short int 2

次回から、ELFヘッダのバイナリを読んでいきたいと思います。