【问题标题】:"call" instruction that seemingly jumps into itself看似跳入自身的“调用”指令
【发布时间】:2010-03-31 06:04:02
【问题描述】:

我有一些 C++ 代码

#include <cstdio>
#include <boost/bind.hpp>
#include <boost/function.hpp>

class A {
public:
    void do_it() { std::printf("aaa"); }
};

void
call_it(const boost::function<void()> &f)
{
    f();
}

void
func()
{
    A *a = new A;
    call_it(boost::bind(&A::do_it, a));
}

哪个 gcc 4 编译成以下程序集(来自objdump):

00000030 <func()>:
  30:   55                      push   %ebp
  31:   89 e5                   mov    %esp,%ebp
  33:   56                      push   %esi
  34:   31 f6                   xor    %esi,%esi
  36:   53                      push   %ebx
  37:   bb 00 00 00 00          mov    $0x0,%ebx
  3c:   83 ec 40                sub    $0x40,%esp
  3f:   c7 04 24 01 00 00 00    movl   $0x1,(%esp)
  46:   e8 fc ff ff ff          call   47 <func()+0x17>
  4b:   8d 55 ec                lea    0xffffffec(%ebp),%edx
  4e:   89 14 24                mov    %edx,(%esp)
  51:   89 5c 24 04             mov    %ebx,0x4(%esp)
  55:   89 74 24 08             mov    %esi,0x8(%esp)
  59:   89 44 24 0c             mov    %eax,0xc(%esp)
  ; the rest of the function is omitted

这里看不懂call指令的操作数,为什么它自己调用了,却少了一个字节?

【问题讨论】:

  • 我们也可以看看 C++ 代码吗?

标签: c++ gcc assembly g++


【解决方案1】:

调用可能是对外部函数的调用,而您看到的地址 (FFFFFFFC) 只是真实地址的占位符,链接器和/或加载器稍后会处理。

【讨论】:

  • 是的,这是正确的答案。如果你 objdump 一个未链接的 .o 文件,你会看到很多这样的内容。
  • 是的,就是这样。我确实在 objdumping 一个 *.o 文件。
猜你喜欢
  • 2011-09-04
  • 2011-10-10
  • 1970-01-01
  • 2023-03-16
  • 2021-05-18
  • 2017-07-30
  • 1970-01-01
  • 1970-01-01
  • 2014-09-17
相关资源
最近更新 更多