mipsel-elf-gdbのsimulatorでmipsのバイナリーを実行する

エミュレータとして知名度のあるqemu以外にも、gdbにはsimulatorが内蔵されており、簡単なバイナリーを実行できることを知ったので試してみる。

この方式の利点はbinutilsをクロスコンパイルするだけで、assemblerとsimulatorが手に入るという手軽さ。今後考えている独自CPU自作に役立ちそう。

ターゲットはRISCとして知名度のあるmipsアーキテクチャ。以下のページの手順とコードを大変参考にさせて頂いた。

[user@localhost mips]$ mips-elf-gdb -q a.out
Reading symbols from /home/user/project/mips/a.out...(no debugging symbols found)...done.
(gdb) target sim
Connected to the simulator.
(gdb) r
Starting program: /home/user/project/mips/a.out
warning: No program loaded.
[Inferior 1 (process 42000) exited with code 057]
(gdb) load

kozosのcross-gcc4でmipsアセンブリをコンパイルし、gdbのsimで実行する - ヾノ*>ㅅ<)ノシ帳
  1. 準備するもの
    1. binutils(mipselのクロスコンパイル用に作っておく)
    2. 上記ページのhello.s/mips.lds

流れはこんな感じ。上記のページとの違いはクロスコンパイル用のbinutilsのみで実行ファイルの生成とgdbによる実行を可能にしている点。

$ mipsel-elf-as hello.S -o hello.o -EB
$ mipsel-elf-ld hello.o -T mips.lds -o hello.out

無事にバイナリが出来たらgdbのtarget simでシミュレータにバイナリーを送り込んで実行させるだけ。最初"-EB"をつけていなかったのでendianの問題に嵌った。

$ mipsel-elf-gdb hello.out
GNU gdb (GDB) 7.8.50.20141223-cvs
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "--host=x86_64-unknown-linux-gnu --target=mipsel-elf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from hello.out...(no debugging symbols found)...done.
(gdb) target sim
Connected to the simulator.
(gdb) load
Loading section .text, size 0x78 vma 0xffffffff80000000
Loading section .MIPS.abiflags, size 0x18 vma 0xffffffff80000078
Loading section .data, size 0xd vma 0xffffffff80000090
Start address 0xffffffff80000000
Transfer rate: 1256 bits in <1 sec.
(gdb) r
Starting program: /tmp/hello.out
Hello World
sim_monitor(17): _exit(int reason) to be coded
[Inferior 1 (process 42000) exited normally]
(gdb) q

こんな感じ。ちゃんとHello Worldと出力されている。