Bas*_*ard 321
如果使用调试符号进行编译,则可以使用它objdump来生成更易读的反汇编.
>objdump --help
[...]
-S, --source Intermix source code with disassembly
-l, --line-numbers Include line numbers and filenames in output
Run Code Online (Sandbox Code Playgroud)
objdump -drwC -Mintel 很好:
-r在重定位上显示符号名称(因此您将puts在call下面的说明中看到)-R 显示动态链接重定位/符号名称(对共享库有用)-C demangles C++符号名称-w 是"宽"模式:它不包装机器码字节-Mintel:使用GAS/binutils类MASM .intel_syntax noprefix语法而不是AT&T-S:使用反汇编交错源代码行.你可以把类似的东西alias disas="objdump -drwCS -Mintel"放在你的~/.bashrc
例:
> gcc -g -c test.c
> objdump -d -M intel -S test.o
test.o: file format elf32-i386
Disassembly of section .text:
00000000 <main>:
#include <stdio.h>
int main(void)
{
0: 55 push ebp
1: 89 e5 mov ebp,esp
3: 83 e4 f0 and esp,0xfffffff0
6: 83 ec 10 sub esp,0x10
puts("test");
9: c7 04 24 00 00 00 00 mov DWORD PTR [esp],0x0
10: e8 fc ff ff ff call 11 <main+0x11>
return 0;
15: b8 00 00 00 00 mov eax,0x0
}
1a: c9 leave
1b: c3 ret
Run Code Online (Sandbox Code Playgroud)
kmm*_*kmm 97
我想补充一下这些答案,如果你给gcc一个标志-fverbose-asm,它发出的汇编程序将会更加清晰.
And*_*ton 74
使用-S(注意:大写S)切换到GCC,它会将汇编代码发送到扩展名为.s的文件.例如,以下命令:
gcc -O2 -S foo.c将生成的汇编代码保留在文件foo.s.
直接从http://www.delorie.com/djgpp/v2faq/faq8_20.html中删除(但删除错误-c)
小智 49
-S在基于x86的系统上使用切换到GCC会产生AT&T语法转储,默认情况下可以使用-masm=att交换机指定,如下所示:
gcc -S -masm=att code.c
Run Code Online (Sandbox Code Playgroud)
如果您想以英特尔语法生成转储,则可以使用该-masm=intel开关,如下所示:
gcc -S -masm=intel code.c
Run Code Online (Sandbox Code Playgroud)
(两者分别生成code.c转换为各种语法的转储code.s)
为了使用objdump产生类似的效果,您需要使用--disassembler-options= intel/ attswitch,一个示例(使用代码转储来说明语法上的差异):
$ objdump -d --disassembler-options=att code.c
Run Code Online (Sandbox Code Playgroud)
080483c4 <main>:
80483c4: 8d 4c 24 04 lea 0x4(%esp),%ecx
80483c8: 83 e4 f0 and $0xfffffff0,%esp
80483cb: ff 71 fc pushl -0x4(%ecx)
80483ce: 55 push %ebp
80483cf: 89 e5 mov %esp,%ebp
80483d1: 51 push %ecx
80483d2: 83 ec 04 sub $0x4,%esp
80483d5: c7 04 24 b0 84 04 08 movl $0x80484b0,(%esp)
80483dc: e8 13 ff ff ff call 80482f4 <puts@plt>
80483e1: b8 00 00 00 00 mov $0x0,%eax
80483e6: 83 c4 04 add $0x4,%esp
80483e9: 59 pop %ecx
80483ea: 5d pop %ebp
80483eb: 8d 61 fc lea -0x4(%ecx),%esp
80483ee: c3 ret
80483ef: 90 nop
Run Code Online (Sandbox Code Playgroud)
和
$ objdump -d --disassembler-options=intel code.c
Run Code Online (Sandbox Code Playgroud)
080483c4 <main>:
80483c4: 8d 4c 24 04 lea ecx,[esp+0x4]
80483c8: 83 e4 f0 and esp,0xfffffff0
80483cb: ff 71 fc push DWORD PTR [ecx-0x4]
80483ce: 55 push ebp
80483cf: 89 e5 mov ebp,esp
80483d1: 51 push ecx
80483d2: 83 ec 04 sub esp,0x4
80483d5: c7 04 24 b0 84 04 08 mov DWORD PTR [esp],0x80484b0
80483dc: e8 13 ff ff ff call 80482f4 <puts@plt>
80483e1: b8 00 00 00 00 mov eax,0x0
80483e6: 83 c4 04 add esp,0x4
80483e9: 59 pop ecx
80483ea: 5d pop ebp
80483eb: 8d 61 fc lea esp,[ecx-0x4]
80483ee: c3 ret
80483ef: 90 nop
Run Code Online (Sandbox Code Playgroud)
Sha*_*our 34
godbolt是一个非常有用的工具,他们列出的只有C++编译器,但是你可以使用-x cflag来让它将代码视为C.然后它将为你的代码并排生成一个汇编列表,你可以使用该Colourise选项来生成彩色条可视地指示哪个源代码映射到生成的程序集.例如,以下代码:
#include <stdio.h>
void func()
{
printf( "hello world\n" ) ;
}
Run Code Online (Sandbox Code Playgroud)
使用以下命令行:
-x c -std=c99 -O3
Run Code Online (Sandbox Code Playgroud)
并Colourise会生成以下:

Bas*_*tch 22
您是否尝试过gcc -S -fverbose-asm -O source.c查看生成的source.s汇编程序文件?
生成的汇编代码进入source.s(您可以使用-o assembler-filename覆盖它); 该-fverbose-asm选项要求编译器发出一些汇编语句"解释"生成的汇编代码.该-O选项要求编译器优化一点(它可以用-O2或优化更多-O3).
如果你想了解gcc正在做什么尝试传递-fdump-tree-all但要小心:你将获得数百个转储文件.
顺便说一句,GCC可通过插件或MELT(一种高级域特定语言扩展GCC;我在2017年放弃)扩展
Vis*_*gar 19
您可以像使用objdump一样使用gdb.
摘录摘自http://sources.redhat.com/gdb/current/onlinedocs/gdb_9.html#SEC64
以下是显示Intel x86的混合源+程序集的示例:
(gdb) disas /m main
Dump of assembler code for function main:
5 {
0x08048330 : push %ebp
0x08048331 : mov %esp,%ebp
0x08048333 : sub $0x8,%esp
0x08048336 : and $0xfffffff0,%esp
0x08048339 : sub $0x10,%esp
6 printf ("Hello.\n");
0x0804833c : movl $0x8048440,(%esp)
0x08048343 : call 0x8048284
7 return 0;
8 }
0x08048348 : mov $0x0,%eax
0x0804834d : leave
0x0804834e : ret
End of assembler dump.
我还没有尝试过gcc,但如果是的话g++,下面的命令对我有用。
-g用于调试构建-Wa,-adhln传递给汇编器以与源代码一起列出g++ -g -Wa,-adhln src.cpp
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
154195 次 |
| 最近记录: |