axxによる、x86_64linuxシステムの、hello worldのテストです。
試験環境
Archlinux terminal
AXXファイル
hello.axx
MOV EAX,e :: 0xb8,e,e>>8,e>>16,e>>24
MOV EDI,e :: 0xbf,e,e>>8,e>>16,e>>24
MOVQ ESI,e:: 0x48,0xbe,e,e>>8,e>>16,e>>24,e>>32,e>>40,e>>48,e>>56
MOV EDX,e :: 0xba,e,e>>8,e>>16,e>>24
SYSCALL :: 0xf,0x5
DB e :: e
hello world 本体
hello.s
section .text
.export _start,len
.org 0x401000
_start:
mov edx, 0
mov eax, 1 ; sys_write (01)
mov edi, 1 ; stdout (01)
mov edx, len: ; length (13)
movq esi, msg ; address
syscall
mov edi, 0 ; return 0
mov eax, 60 ; sys_exit
syscall
msg: .ascii "hello, world\n"
len: .equ $$ - msg
_end:
アセンブル
$ axx.py hello.axx hello.s -o hello.bin
hello.s 5 mov edx, 0 0xba 0x00 0x00 0x00 0x00
hello.s 6 mov eax, 1 ; sys_write (01) 0xb8 0x01 0x00 0x00 0x00
hello.s 7 mov edi, 1 ; stdout (01) 0xbf 0x01 0x00 0x00 0x00
hello.s 8 mov edx, len: ; length (13) 0xba 0x0d 0x00 0x00 0x00
hello.s 9 movq esi, msg ; address 0x48 0xbe 0x2c 0x10 0x40 0x00 0x00 0x00 0x00 0x00
hello.s 10 syscall 0x0f 0x05
hello.s 11 mov edi, 0 ; return 0 0xbf 0x00 0x00 0x00 0x00
hello.s 12 mov eax, 60 ; sys_exit 0xb8 0x3c 0x00 0x00 0x00
hello.s 13 syscall 0x0f 0x05
0x68 0x65 0x6c 0x6c 0x6f 0x2c 0x20 0x77 0x6f 0x72 0x6c 0x64 0x0a
rawバイナリファイルから、実行形式ELFへの変換
これが役に立ちました。
bin2elf
・https://gist.github.com/tangrs/4030336
bin2elf.sh hello.bin hello.exe 0x401000
プログラム実行のデフォルトエントリは0x401000なので。
実行
$ ./hello.exe
hello, world
$
動きました。
axxのリンカ(ldとのインターフェース)を作ろうとしていますが、ELFの構造は複雑で、リロケータブルELFを作るのが難しいです。