c++ - 函数调用在反汇编代码中似乎无法正常工作

标签 c++

<分区>

我写了一个简单的程序,然后编译组装。

tfc.cpp

int i = 0;

void f(int a)
{
  i += a;
};

int main()
{
  f(9);
  return 0;
};

我通过运行获得了 tfc.o

$ g++ -c -O1 tfc.cpp

然后我使用gobjdump (objdump) 反汇编二进制文件。

$ gobjdump -d tfc.o

然后我得到了

0000000000000000 <__Z1fi>:
   0: 55                    push   %rbp
   1: 48 89 e5              mov    %rsp,%rbp
   4: 01 3d 00 00 00 00     add    %edi,0x0(%rip)        # a <__Z1fi+0xa>
   a: 5d                    pop    %rbp
   b: c3                    retq   
   c: 0f 1f 40 00           nopl   0x0(%rax)

0000000000000010 <_main>:
  10: 55                    push   %rbp
  11: 48 89 e5              mov    %rsp,%rbp
  14: bf 09 00 00 00        mov    $0x9,%edi
  19: e8 00 00 00 00        callq  1e <_main+0xe>
  1e: 31 c0                 xor    %eax,%eax
  20: 5d                    pop    %rbp
  21: c3                    retq  

奇怪的事情发生了,callq指令后跟1e <_main+0xe> .不应该是<__Z1fi>的地址吗? ?如果不是,main 是如何实现的?函数调用 f功能?

编辑 仅供引用:

$ g++ -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.1.0
Thread model: posix

最佳答案

它调用地址0,这是f函数的地址。

e8是x86中的调用指令,按照这个:

http://www.cs.cmu.edu/~fp/courses/15213-s07/misc/asm64-handout.pdf

call 使用相对于内存位置 1e 的下一条指令的位移。这变成了内存位置 0。所以它是 callq 1e,而实际上它正在调用地址 0。

关于c++ - 函数调用在反汇编代码中似乎无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23072494/

相关文章:

c++ - 对于 C++17 中的非标准布局类, `offsetof` 为 "conditionally-supported"是什么意思?

c++ - 传递 C++ void 指针

c++ - 内存泄漏,来源 : float** binsRowPtrs = new float *[_nbins];

c++ - Visual Studio 调试器在内联代码中速度变慢

c++ - 按位移位将 unsigned char 提升为 int

c# - 将字符串从非托管 c++ dll 返回到 c#

c++ - 如何从 C++ 中的函数返回对象引用以及如何从客户端调用该函数?

c++ - 需要维护秩序的多线程作业

c++ - 如何使用已存在的程序实例自定义右键单击?

c++ - 用于将容器累积到字符串中的标准算法,其中分隔符分隔条目?