オブジェクト修行

BINARY HACKS のオブジェクトファイル Hacks 確認します。

型について

man 5 elf にて出力される情報を以下に引用。

       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

上二つの ElfN_Addr および ElfN_Off がアーキテクチャによって異なるのがわかります。で、次に出てくるのが ELF ヘッダ。

       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]; /* Magic
                                                  * Class, Data, Version,
                                                  * OS/ABI, API Version */
               uint16_t      e_type;             /* Type */
               uint16_t      e_machine;          /* Machine */
               uint32_t      e_version;          /* Version */
               ElfN_Addr     e_entry;            /* Entry point address */
               ElfN_Off      e_phoff;            /* Start of program headers */
               ElfN_Off      e_shoff;            /* Start of section headers */
               uint32_t      e_flags;            /* Flags */
               uint16_t      e_ehsize;           /* ELF header's size */
               uint16_t      e_phentsize;        /* Size of program headers */
               uint16_t      e_phnum;            /* Number of program headers */
               uint16_t      e_shentsize;        /* Size of section headers */
               uint16_t      e_shnum;            /* Number of section headers */
               uint16_t      e_shstrndx;         /* Section header string table index */
           } ElfN_Ehdr;

ええと、BINARY HACKS では readelf -h の出力を例示しています。arm の /sbin/init を確認してみます。コメントは下の出力そのまんまですねorz

$ ./arm-none-linux-gnueabi-readelf /sbin/init -h
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              DYN (Shared object file)
  Machine:                           ARM
  Version:                           0x1
  Entry point address:               0x48e5
  Start of program headers:          52 (bytes into file)
  Start of section headers:          119756 (bytes into file)
  Flags:                             0x5000002, has entry point, Version5 EABI
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         9
  Size of section headers:           40 (bytes)
  Number of section headers:         29
  Section header string table index: 28
$

readelf の -l と Number of program headers の値とか -S と Number of Section headers の値は合致していることを確認してます。

明日朝

起きれたら KOZOS 本にある ELF ヘッダの解析なソレを使ってプログラムヘッダを手繰って云々が書けるかどうか。あるいは手元にある kozos なナニを、になるのかどうか。
とりあえず今日はクタバります。