OSX上でセルフホスティングできるコンパイラを書こう

どうした

Mach-Oバイナリ調査

とりあえずgccさまを信じる

以下のCコードを作成(exit.c)

int main() {}

さくっとgcc -c exit.c && objdump -d exit.o

できあがったのがこちら


exit.o: file format Mach-O 64-bit x86-64

Disassembly of section __TEXT,__text:
_main:
       0:       55      pushq   %rbp
       1:       48 89 e5        movq    %rsp, %rbp
       4:       31 c0   xorl    %eax, %eax
       6:       5d      popq    %rbp
       7:       c3      retq

なるほど?アセンブリを抜き出すとこうなる。

.global _main

_main:
      pushq     %rbp
      movq      %rsp, %rbp
      xorl      %eax, %eax
      popq      %rbp
      retq

これにnopを16こつけたものも用意する

.global _main

_main:
      pushq %rbp
      movq  %rsp, %rbp
      xorl  %eax, %eax
      popq  %rbp
      retq

nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop
nop

で、それぞれコンパイルして.oを作成してhexdump -Cする

00000000  cf fa ed fe 07 00 00 01  03 00 00 00 01 00 00 00  |................|
00000010  04 00 00 00 10 01 00 00  00 00 00 00 00 00 00 00  |................|
00000020  19 00 00 00 98 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000040  08 00 00 00 00 00 00 00  30 01 00 00 00 00 00 00  |........0.......|
00000050  08 00 00 00 00 00 00 00  07 00 00 00 07 00 00 00  |................|
00000060  01 00 00 00 00 00 00 00  5f 5f 74 65 78 74 00 00  |........__text..|
00000070  00 00 00 00 00 00 00 00  5f 5f 54 45 58 54 00 00  |........__TEXT..|
00000080  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000090  08 00 00 00 00 00 00 00  30 01 00 00 00 00 00 00  |........0.......|
000000a0  00 00 00 00 00 00 00 00  00 04 00 80 00 00 00 00  |................|
000000b0  00 00 00 00 00 00 00 00  24 00 00 00 10 00 00 00  |........$.......|
000000c0  00 0c 0a 00 00 00 00 00  02 00 00 00 18 00 00 00  |................|
000000d0  38 01 00 00 01 00 00 00  48 01 00 00 08 00 00 00  |8.......H.......|
000000e0  0b 00 00 00 50 00 00 00  00 00 00 00 00 00 00 00  |....P...........|
000000f0  00 00 00 00 01 00 00 00  01 00 00 00 00 00 00 00  |................|
00000100  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000130  55 48 89 e5 31 c0 5d c3  01 00 00 00 0f 01 00 00  |UH..1.].........|
00000140  00 00 00 00 00 00 00 00  00 5f 6d 61 69 6e 00 00  |........._main..|
00000150
00000000  cf fa ed fe 07 00 00 01  03 00 00 00 01 00 00 00  |................|
00000010  04 00 00 00 10 01 00 00  00 00 00 00 00 00 00 00  |................|
00000020  19 00 00 00 98 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000040  18 00 00 00 00 00 00 00  30 01 00 00 00 00 00 00  |........0.......|
00000050  18 00 00 00 00 00 00 00  07 00 00 00 07 00 00 00  |................|
00000060  01 00 00 00 00 00 00 00  5f 5f 74 65 78 74 00 00  |........__text..|
00000070  00 00 00 00 00 00 00 00  5f 5f 54 45 58 54 00 00  |........__TEXT..|
00000080  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000090  18 00 00 00 00 00 00 00  30 01 00 00 00 00 00 00  |........0.......|
000000a0  00 00 00 00 00 00 00 00  00 04 00 80 00 00 00 00  |................|
000000b0  00 00 00 00 00 00 00 00  24 00 00 00 10 00 00 00  |........$.......|
000000c0  00 0c 0a 00 00 00 00 00  02 00 00 00 18 00 00 00  |................|
000000d0  48 01 00 00 01 00 00 00  58 01 00 00 08 00 00 00  |H.......X.......|
000000e0  0b 00 00 00 50 00 00 00  00 00 00 00 00 00 00 00  |....P...........|
000000f0  00 00 00 00 01 00 00 00  01 00 00 00 00 00 00 00  |................|
00000100  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000130  55 48 89 e5 31 c0 5d c3  90 90 90 90 90 90 90 90  |UH..1.].........|
00000140  90 90 90 90 90 90 90 90  01 00 00 00 0f 01 00 00  |................|
00000150  00 00 00 00 00 00 00 00  00 5f 6d 61 69 6e 00 00  |........._main..|
00000160