将代码编译为原始二进制文件

时间:2014-12-30 16:15:30

标签: gcc command-line machine-code objcopy

我试图将我的代码编译成原始二进制文件并按照其他SO帖子的建议(例如thisthis)我尝试了objdump:

$ gcc -c foo.c
$ objcopy -O binary foo.o foo.bin

然后我试着确定这是否有效:

$ objdump -d foo.o

foo.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <main>:
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   48 83 ec 10             sub    $0x10,%rsp
   8:   89 7d fc                mov    %edi,-0x4(%rbp)
   b:   48 89 75 f0             mov    %rsi,-0x10(%rbp)
   f:   bf 00 00 00 00          mov    $0x0,%edi
  14:   b8 00 00 00 00          mov    $0x0,%eax
  19:   e8 00 00 00 00          callq  1e <main+0x1e>
  1e:   b8 00 00 00 00          mov    $0x0,%eax
  23:   c9                      leaveq 
  24:   c3                      ret

$ hexdump -C foo.bin

00000000  14 00 00 00 00 00 00 00  01 7a 52 00 01 78 10 01  |.........zR..x..|
00000010  1b 0c 07 08 90 01 00 00  1c 00 00 00 1c 00 00 00  |................|
00000020  00 00 00 00 25 00 00 00  00 41 0e 10 86 02 43 0d  |....%....A....C.|
00000030  06 60 0c 07 08 00 00 00                           |.`......|
00000038

显然有些事情是错的。我用gcc交叉编译的结果检查了这个,结果明显不正确。

1 个答案:

答案 0 :(得分:2)

您可以将-j .text传递给objcopy