最近在寫x64的hooking,
需要一小段machine code,
但是VC在x64平台上已經不允許寫inline assembly了,
這時候只好直接塞machine code進去.
但是又不可能直接寫machine,
還是需要從assembly轉成machine code.
還好MinGW64 gcc還是可以inline assembly,
就開個檔案,裡面直接inline asssembly,
例如:開個檔案叫做 asm.c 內容如下:
asm("movq $0x10000000000000,%rax\n\t"
"movq (%rax),%rax\n\t"
"ret \n\t"
);
然後 gcc -c asm.c 編譯出object檔後,
再用objdump -d asm.o 就可以得到如下結果:
asm.o: file format pe-x86-64
Disassembly of section .text:
0000000000000000 <.text>:
0: 48 b8 00 00 00 00 00 movabs $0x10000000000000,%rax
7: 00 10 00
a: 48 8b 00 mov (%rax),%rax
d: c3 retq
e: 90 nop
f: 90 nop
剩下就看怎麼用這些machine codes了~~~
再順便附上一個.pl檔用來format成c的格式
用法如下: objdump -d asm.o | perl dump_asm.pl
就可以得到以下結果:
/* 0 */ 0x48,0xb8,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00, // movabs $0x10000000000000,%rax
/* a */ 0x48,0x8b,0x00, // mov (%rax),%rax
/* d */ 0xc3, // retq
/* e */ 0x90, // nop
/* f */ 0x90, // nop