了解反汇编 - 看到两个 main()
以下 C 程序的转储:
int main() {
int i,j;
for(i=0; i<2; i++) {
j++;
}
return 0;
}
正在生成:
08048394 <main>:
int main() {
8048394: 8d 4c 24 04 lea 0x4(%esp),%ecx
8048398: 83 e4 f0 and $0xfffffff0,%esp
804839b: ff 71 fc pushl -0x4(%ecx)
804839e: 55 push %ebp
804839f: 89 e5 mov %esp,%ebp
80483a1: 51 push %ecx
80483a2: 83 ec 10 sub $0x10,%esp
int i,j;
for(i=0; i<2; i++) {
80483a5: c7 45 f8 00 00 00 00 movl $0x0,-0x8(%ebp)
80483ac: eb 08 jmp 80483b6 <main+0x22>
j++;
80483ae: 83 45 f4 01 addl $0x1,-0xc(%ebp)
int main() {
int i,j;
for(i=0; i<2; i++) {
80483b2: 83 45 f8 01 addl $0x1,-0x8(%ebp)
80483b6: 83 7d f8 01 cmpl $0x1,-0x8(%ebp)
80483ba: 7e f2 jle 80483ae <main+0x1a>
j++;
}
return 0;
80483bc: b8 00 00 00 00 mov $0x0,%eax
}
无论我放置 i<2
还是 i<10
,我都会看到两个 main() 具有相同的结构。有人能告诉我为什么会发生这种情况吗?
The dump of the following C program:
int main() {
int i,j;
for(i=0; i<2; i++) {
j++;
}
return 0;
}
is producing:
08048394 <main>:
int main() {
8048394: 8d 4c 24 04 lea 0x4(%esp),%ecx
8048398: 83 e4 f0 and $0xfffffff0,%esp
804839b: ff 71 fc pushl -0x4(%ecx)
804839e: 55 push %ebp
804839f: 89 e5 mov %esp,%ebp
80483a1: 51 push %ecx
80483a2: 83 ec 10 sub $0x10,%esp
int i,j;
for(i=0; i<2; i++) {
80483a5: c7 45 f8 00 00 00 00 movl $0x0,-0x8(%ebp)
80483ac: eb 08 jmp 80483b6 <main+0x22>
j++;
80483ae: 83 45 f4 01 addl $0x1,-0xc(%ebp)
int main() {
int i,j;
for(i=0; i<2; i++) {
80483b2: 83 45 f8 01 addl $0x1,-0x8(%ebp)
80483b6: 83 7d f8 01 cmpl $0x1,-0x8(%ebp)
80483ba: 7e f2 jle 80483ae <main+0x1a>
j++;
}
return 0;
80483bc: b8 00 00 00 00 mov $0x0,%eax
}
No matter whether I put i<2
or i<10
, I am seeing two main()
's with the same structure. Can someone tell me why this is happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有看到两个
main()
。您会看到反汇编器被for
循环彻底搞糊涂了。如果您从头到尾阅读它,实际的程序集只代表一个函数main()
,并且逻辑路径与 C 代码相同。简而言之:插入程序集中的 C 是错误。
You are not seeing two
main()
s. You are seeing a disassembler utterly confused out of its mind by afor
loop. The actual assembly, if you read it all the way through, represents exactly one function,main()
, and the logic path is identical to the C code.In short: the C interleaved into the assembly is wrong.
反汇编器会尽职尽责地交错源代码,完全按照编译器的输出调试信息所述。在 Linux 上,您可以使用 objdump -W 来查看这一点:
我的编译器显然与您的编译器略有不同,因为地址不同,但您可以看到它是如何工作的:输出程序集中的地址与输入源文件中的行不精确。
The disassembler is dutifully interleaving the source code exactly as the compiler's output debug information says. On Linux, you can see this with
objdump -W
:My compiler apparently differs a bit from yours, as the addresses are different, but you see how it works: the mapping between addresses in the output assembly and lines in the input source file is imprecise.