HobbyOSs / opennask

nask clone assembly, it can boot tiny OS with Linux
https://github.com/HobbyOSs/opennask/wiki/%E5%8B%95%E4%BD%9C%E3%83%81%E3%82%A7%E3%83%83%E3%82%AF
GNU General Public License v3.0
20 stars 3 forks source link

PE(Portable Executable)ヘッダの出力 #12

Closed hangingman closed 7 years ago

hangingman commented 7 years ago
[FORMAT "WCOFF"]

資料

その他

hangingman commented 7 years ago

naskの出力を見る。

アセンブラの記述量の割に出力が多い

; naskfunc
; TAB=4

[FORMAT "WCOFF"]                ; オブジェクトファイルを作るモード  
[BITS 32]                       ; 32ビットモード用の機械語を作らせる

; オブジェクトファイルのための情報

[FILE "naskfunc.nas"]           ; ソースファイル名情報

        GLOBAL  _io_hlt         ; このプログラムに含まれる関数名

; 以下は実際の関数

[SECTION .text]     ; オブジェクトファイルではこれを書いてからプログラムを書く

_io_hlt:    ; void io_hlt(void);
        HLT
        RET
00000000: 4c01 0300 0000 0000 8e00 0000 0900 0000  L...............

  (WORD)  0x4c01     => Machine               => Intel 386
  (WORD)  0x0300     => NumberOfSections      => 3つ?
  (DWORD) 0x00000000 => TimeDateStamp         => 入ってない
  (DWORD) 0x8e000000 => PointerToSymbolTable  =>
  (DWORD) 0x09000000 => NumberOfSymbols       =>

00000010: 0000 0000 2e74 6578 7400 0000 0000 0000  .....text.......

  (WORD)  0x0000     => SizeOfOptionalHeader  => オプショナルヘッダは無し
  (WORD)  0x0000     => Characteristics       => 何もセットされてない…?

  0x2e746578からセクションテーブル -->
  (BYTE[8]) 0x2e74657874000000 => Name        => ".text"
  (DWORD) 0x00000200   => VirtualSize => セクションのサイズ: 512byte

00000020: 0000 0000 0200 0000 8c00 0000 8e00 0000  ................

  (DWORD) 0x00008c00 => VirtualAddress        => セクションの RVA
  (DWORD) 0x00008e00 => PointerToRawData      => セクションが初期値となるデータを持つ場合に、ファイル内オフセットが格納されている

00000030: 0000 0000 0000 0000 2000 1060 2e64 6174  ........ ..`.dat

  (DWORD) 0x00000000 => PointerToRelocations  => PE ファイルでは使用しない
  (DWORD) 0x00000000 => PointerToLinenumbers  => 意味なし, 0x00
  (WORD)  0x0000     => NumberOfRelocations   => 再配置情報の数, PE ファイルでは常に 0
  (WORD)  0x0000     => NumberOfLinenumbers   => 行番号情報の数
  (DWORD) 0x20001060 => Characteristics       => セクションの特性を表すフラグの組み合わせ

00000040: 6100 0000 0000 0000 0000 0000 0000 0000  a...............

  0x2e64617461からセクションテーブル -->
  (BYTE[8]) 0x2e64617461000000 => Name        => ".data"
  (DWORD) 0x00000000 => VirtualAddress
  (DWORD) 0x00000000 => PointerToRawData
  (DWORD) 0x00000000 => PointerToRelocations

00000050: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000060: 4000 10c0 2e62 7373 0000 0000 0000 0000  @....bss........

  (DWORD) 0x00000000 => PointerToLinenumbers
  (WORD)  0x0000     => NumberOfRelocations
  (WORD)  0x0000     => NumberOfLinenumbers
  (DWORD) 0x400010c0 => Characteristics

  0x2e627373からセクションテーブル -->
  (BYTE[8]) 0x2e62737300000000 => Name        => ".bss"

00000070: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000080: 0000 0000 0000 0000 8000 10c0 f4c3 2e66  ...............f

  (DWORD) 0x00000000 => VirtualAddress
  (DWORD) 0x00000000 => PointerToRawData
  (DWORD) 0x00000000 => PointerToRelocations
  (DWORD) 0x00008000 => PointerToLinenumbers
  (WORD)  0x10c0     => NumberOfRelocations
  (WORD)  0xf4c3     => NumberOfLinenumbers

00000090: 696c 6500 0000 0000 0000 feff 0000 6701  ile...........g.

  0x2e66696c65からセクションテーブル -->
  (BYTE[8]) 0x2e66696c6500000000 => Name      => ".file"
  この辺よくわからなかった

000000a0: 6e61 736b 6675 6e63 2e6e 6173 0000 0000  naskfunc.nas....
000000b0: 0000 2e74 6578 7400 0000 0000 0000 0100  ...text.........
000000c0: 0000 0301 0200 0000 0000 0000 0000 0000  ................
000000d0: 0000 0000 0000 2e64 6174 6100 0000 0000  .......data.....
000000e0: 0000 0200 0000 0301 0000 0000 0000 0000  ................
000000f0: 0000 0000 0000 0000 0000 2e62 7373 0000  ...........bss..
00000100: 0000 0000 0000 0300 0000 0301 0000 0000  ................
00000110: 0000 0000 0000 0000 0000 0000 0000 5f69  .............._i
00000120: 6f5f 686c 7400 0000 0000 0100 0000 0200  o_hlt...........
00000130: 0400 0000
hangingman commented 7 years ago

資料追加

hangingman commented 7 years ago

naskの中に定義発見

     static constexpr UCHAR header[140] = {
    /* file header */
      0x4c, 0x01, /* signature */
      0x03, 0x00, /* sections == 3 */
      0, 0, 0, 0, /* time & date */
      0, 0, 0, 0, /* +0x08: symboltable */
      0, 0, 0, 0, /* +0x0c: sizeof (symboltable) / 18 */
      0x00, 0x00, /* no optional header */
      0x00, 0x00, /* flags */

    /* section header (.text) */
      '.', 't', 'e', 'x', 't', 0, 0, 0, /* name */
      0, 0, 0, 0, /* paddr (section_text - section_text) */
      0, 0, 0, 0, /* vaddr == 0 */
      0, 0, 0, 0, /* +0x24: sizeof (section_text) */
      0, 0, 0, 0, /* +0x28: section_text */
      0, 0, 0, 0, /* +0x2c: reloctab_text */
      0, 0, 0, 0, /* line number == 0 */
      0, 0, /* +0x34: sizeof (reloctab_text) / 10 */
      0, 0, /* sizeof (line_number) == 0 */
      0x20, 0x00, 0x10, 0x60, /* +0x38: flags, default_align = 1 */

    /* section header (.data) */
      '.', 'd', 'a', 't', 'a', 0, 0, 0, /* name */
      0, 0, 0, 0, /* paddr (section_data - section_text) */
      0, 0, 0, 0, /* vaddr == 0 */
      0, 0, 0, 0, /* +0x4c: sizeof (section_data) */
      0, 0, 0, 0, /* +0x50: section_data */
      0, 0, 0, 0, /* +0x54: reloctab_data */
      0, 0, 0, 0, /* line number == 0 */
      0, 0, /* +0x5c: sizeof (reloctab_data) / 10 */
      0, 0, /* sizeof (line_number) == 0 */
      0x40, 0x00, 0x10, 0xc0, /* +0x60: flags, default_align = 1 */

    /* section header (.bss) */
      '.', 'b', 's', 's', 0, 0, 0, 0, /* name */
      0, 0, 0, 0, /* paddr (section_bss - section_text) */
      0, 0, 0, 0, /* vaddr == 0 */
      0, 0, 0, 0, /* +0x74: sizeof (section_bss) */
      0, 0, 0, 0, /* section_bss == 0 */
      0, 0, 0, 0, /* reloctab_bss == 0 */
      0, 0, 0, 0, /* line number == 0 */
      0, 0, /* sizeof (reloctab_data) / 10 == 0 */
      0, 0, /* sizeof (line_number) == 0 */
      0x80, 0x00, 0x10, 0xc0 /* +0x88: flags, default_align = 1 */
     };
hangingman commented 7 years ago

.file 以後の補助フォーマット(Auxiliary Symbol Table)情報。前述のpdfにしか記述がないのでつらい。

Offset Size Field Description
0 8 Name (*) Name of the symbol, represented by union of three structures. An array of eight bytes is used if the name is not more than eight bytes long. See Section 5.4.1, “Symbol Name Representation, ” for more information.
8 4 Value Value associated with the symbol. The interpretation of this field depends on Section Number and Storage Class. A typical meaning is the relocatable address.
12 2 SectionNumber Signed integer identifying the section, using a one-based index into the Section Table. Some values have special meaning defined in “Section Number Values.”
14 2 Type A number representing type. Microsoft tools set this field to 0x20 (function) or 0x0 (not a function). See Section 5.4.3, “Type Representation,” for more information.
16 1 StorageClass Enumerated value representing storage class. See Section 5.4.4, “Storage Class,” for more information.
17 1 NumberOfAuxSymbols Number of auxiliary symbol table entries that follow this record.

".file" Auxiliary Symbol Table

Binary Size Field Description
2e66 696c 6500 0000 8 Name (*) ".file" を指定
0000 0000 4 Value
feff 2 SectionNumber
0000 2 Type
67 1 StorageClass
01 1 NumberOfAuxSymbols
6e61 736b 6675 6e63 2e6e 6173 0000 0000 0000 18 Auxiliary Symbol Table 上で指定されたファイル名で18byte
                                             |--> ここから
00000080: 0000 0000 0000 0000 8000 10c0 f4c3 2e66  ...............f
00000090: 696c 6500 0000 0000 0000 feff 0000 6701  ile...........g.
000000a0: 6e61 736b 6675 6e63 2e6e 6173 0000 0000  naskfunc.nas....
000000b0: 0000 2e74 6578 7400 0000 0000 0000 0100  ...text.........

".text" Auxiliary Symbol Table

Binary Size Field Description
2e74 6578 7400 0000 8 Name (*) ".text" を指定
0000 0000 4 Value
0100 2 SectionNumber
0000 2 Type
03 1 StorageClass
01 1 NumberOfAuxSymbols
0000 0000 0000 0000 0000 0000 0000 0000 0000 18 Auxiliary Symbol Table 謎の18byte

".data" Auxiliary Symbol Table

Binary Size Field Description
2e64 6174 6100 0000 8 Name (*) ".data" を指定
0000 0000 4 Value
0200 2 SectionNumber
0000 2 Type
03 1 StorageClass
01 1 NumberOfAuxSymbols
0000 0000 0000 0000 0000 0000 0000 0000 0000 18 Auxiliary Symbol Table 謎の18byte

".bss" Auxiliary Symbol Table

Binary Size Field Description
2e62 7373 0000 0000 8 Name (*) ".data" を指定
0000 0000 4 Value
0300 2 SectionNumber
0000 2 Type
03 1 StorageClass
01 1 NumberOfAuxSymbols
0000 0000 0000 0000 0000 0000 0000 0000 0000 18 Auxiliary Symbol Table 謎の18byte
hangingman commented 7 years ago

59f44ea535fb75caa6890b948787eda2f13b44fe でほぼ実装完了