是否有一组命令行选项可以说服 gcc 从独立的源文件生成平面二进制文件?例如,假设 foo.c 的内容是
static int f(int x)
{
int y = x*x;
return y+2;
}
没有外部引用,没有任何内容可以导出到链接器。我想要一个小文件,其中仅包含此功能的机器指令,没有任何其他装饰。有点像 (DOS) .COM 文件,但 32 位保护模式除外。
试试这个:
$ gcc -c test.c
$ objcopy -O binary -j .text test.o binfile
您可以确保它是正确的objdump
:
$ objdump -d test.o
test.o: file format pe-i386
Disassembly of section .text:
00000000 <_f>:
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 83 ec 04 sub $0x4,%esp
6: 8b 45 08 mov 0x8(%ebp),%eax
9: 0f af 45 08 imul 0x8(%ebp),%eax
d: 89 45 fc mov %eax,-0x4(%ebp)
10: 8b 45 fc mov -0x4(%ebp),%eax
13: 83 c0 02 add $0x2,%eax
16: c9 leave
17: c3 ret
并将其与二进制文件进行比较:
$ hexdump -C binfile
00000000 55 89 e5 83 ec 04 8b 45 08 0f af 45 08 89 45 fc |U......E...E..E.|
00000010 8b 45 fc 83 c0 02 c9 c3 |.E......|
00000018
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)