tomoyuki-nakabayashi / Rustemu86

Apache License 2.0
5 stars 0 forks source link

elf loader作ろう #49

Closed tomoyuki-nakabayashi closed 5 years ago

tomoyuki-nakabayashi commented 5 years ago

riscv-testsをロードしたいので、elf loaderを作ろう。

tomoyuki-nakabayashi commented 5 years ago
$ readelf -S ~/work/04.RISCV/riscv-tests/isa/rv32ui-p-simple
There are 6 section headers, starting at offset 0x21f4:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .text.init        PROGBITS        80000000 001000 000144 00  AX  0   0 64
  [ 2] .tohost           PROGBITS        80001000 002000 000048 00  WA  0   0 64
  [ 3] .symtab           SYMTAB          00000000 002048 0000f0 10      4   9  4
  [ 4] .strtab           STRTAB          00000000 002138 00008b 00      0   0  1
  [ 5] .shstrtab         STRTAB          00000000 0021c3 00002e 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)
tomoyuki-nakabayashi commented 5 years ago
$ riscv32-unknown-elf-readelf -h tests/data/elf/rv32ui-p-simple
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:                              EXEC (Executable file)
  Machine:                           RISC-V
  Version:                           0x1
  Entry point address:               0x80000000
  Start of program headers:          52 (bytes into file)
  Start of section headers:          8692 (bytes into file)
  Flags:                             0x0
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         2
  Size of section headers:           40 (bytes)
  Number of section headers:         6
  Section header string table index: 5
tomoyuki-nakabayashi commented 5 years ago
$ riscv32-unknown-elf-readelf tests/data/elf/rv32ui-p-simple -l

Elf file type is EXEC (Executable file)
Entry point 0x80000000
There are 2 program headers, starting at offset 52

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  LOAD           0x001000 0x80000000 0x80000000 0x00144 0x00144 R E 0x1000
  LOAD           0x002000 0x80001000 0x80001000 0x00048 0x00048 RW  0x1000

 Section to Segment mapping:
  Segment Sections...
   00     .text.init
   01     .tohost

52byte目からプログラムヘッダ情報があるのはわかったので、あとは、プログラムヘッダを読み出せば、ロードできるかな?

tomoyuki-nakabayashi commented 5 years ago

そろそろ外部から使うAPIを考えないとなぁ。 プログラムヘッダのイテレータを返してあげるか?

tomoyuki-nakabayashi commented 5 years ago

man elfで確認したプログラムヘッダ構造体。

           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;
tomoyuki-nakabayashi commented 5 years ago

作り終わったのでclose