了解反汇编 - 看到两个 main()

发布于 2024-10-02 00:38:58 字数 1279 浏览 5 评论 0原文

以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦言归人 2024-10-09 00:38:58

您没有看到两个 main()。您会看到反汇编器被 for 循环彻底搞糊涂了。如果您从头到尾阅读它,实际的程序集只代表一个函数 main(),并且逻辑路径与 C 代码相同。

简而言之:插入程序集中的 C 是错误

You are not seeing two main()s. You are seeing a disassembler utterly confused out of its mind by a for 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.

若相惜即相离 2024-10-09 00:38:58

反汇编器会尽职尽责地交错源代码,完全按照编译器的输出调试信息所述。在 Linux 上,您可以使用 objdump -W 来查看这一点:

…
 Line Number Statements:
  Extended opcode 2: set Address to 0x80483e4
  Copy
  Special opcode 91: advance Address by 6 to 0x80483ea and Line by 2 to 3
  Special opcode 132: advance Address by 9 to 0x80483f3 and Line by 1 to 4
  Special opcode 60: advance Address by 4 to 0x80483f7 and Line by -1 to 3
  Special opcode 148: advance Address by 10 to 0x8048401 and Line by 3 to 6
  Special opcode 76: advance Address by 5 to 0x8048406 and Line by 1 to 7
  Advance PC by 2 to 0x8048408
  Extended opcode 1: End of Sequence
…

我的编译器显然与您的编译器略有不同,因为地址不同,但您可以看到它是如何工作的:输出程序集中的地址与输入源文件中的行不精确。

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:

…
 Line Number Statements:
  Extended opcode 2: set Address to 0x80483e4
  Copy
  Special opcode 91: advance Address by 6 to 0x80483ea and Line by 2 to 3
  Special opcode 132: advance Address by 9 to 0x80483f3 and Line by 1 to 4
  Special opcode 60: advance Address by 4 to 0x80483f7 and Line by -1 to 3
  Special opcode 148: advance Address by 10 to 0x8048401 and Line by 3 to 6
  Special opcode 76: advance Address by 5 to 0x8048406 and Line by 1 to 7
  Advance PC by 2 to 0x8048408
  Extended opcode 1: End of Sequence
…

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文