c++ - !编译器只留下一个 stub ! ,C++, 在.o文件中找到这些 "stubs"

标签 c++ assembly compiler-construction linker

我想达到的目的

At the time of compilation, the compiler knew the function call was valid because you included the iostream header file, but since that function was not part of the cpp file, the compiler just leaves a stub at the call site. The linker goes through the object file, and for each stub, it finds the correct function address and replaces the stub with the correct address from one of the other object files being linked. by ALEX ALLAN , Jumping to C++

在“ stub ”之上,我想在真实的目标文件中看到。

我做了如下,

代码 // main.cpp

#include "f.h"

using namespace std;

int main()
{

   myfunc();

    return 0;
}


    //f.cpp
    #include "f.h"
    void myfunc()
    {
    }


 //f.h
#ifndef F_H_INCLUDED
#define F_H_INCLUDED

 void myfunc();

#endif // F_H_INCLUDED

以上 3 个在单独的文件中。

我在 main.o 上使用的 objdump 命令

C:\Users\User\Downloads\binutils-2.22-1-mingw32-bin.tar\binutils-2.22-1-mingw32-
bin\bin>objdump.exe -S -C C:\Users\User\Documents\myC++\testFuncstabs\obj\Debug\
main.o

enter image description here

但现在我无法在此输出中找到 myFunc 的“ stub ”?

谁能帮我解决这个问题?或建议我实现此目标的另一种策略?

最佳答案

在此上下文中的“ stub ”并不意味着一个小函数。相反,它指的是一个小的占位符值。请注意十六进制偏移 109120125 等如何将 4 个字节作为零?那是 stub 。当链接器处理目标文件时,它将查看存储在目标文件其他位置的重定位表。该表包括对其他符号的引用以及代码/数据的哪些部分使用这些符号。一旦链接器找到该符号的定义位置,它将替换(也称为重新定位)那些“ stub ”值以指向正确的地址。如果您要从实际的可执行文件中查看 main 的反汇编,您会看到所有这些零都被替换为其他值(也就是您的函数、变量等的地址(而不是偏移量))代码使用)。

关于c++ - !编译器只留下一个 stub ! ,C++, 在.o文件中找到这些 "stubs",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17623576/

相关文章:

assembly - 重新整理我的申请?

c++ - FMOD 编译问题

c++ - "g++"和 "c++"编译器

css - 优化 css 编译器

c++ - 是否有一种自动合并 C++ 实现(.cpp)和头文件(.h)文件的方法

c++ - 具有唯一指针的右值和移动语义:对象0x7ffee5c7b670错误:未分配正在释放的指针

c++ - 在 Linux 上用 C++ 读取硬盘扇区

c++ - 在Visual Studio中使用cygwin编译的dll的库函数出错

linux - 汇编语言中的指针

gcc - 了解 GCC 的 alloca() 对齐和看似错过的优化