在抽象类中使用shared_ptr时,如何在nm或objdump中找到函数符号

How to find the function symbol in nm or objdump when using shared_ptr for abstract class?

本文关键字:nm objdump 符号 函数 抽象类 shared ptr      更新时间:2023-10-16

我使用shared_ptr作为抽象类ABC。ABCImpl类是ABC的实现。Abc_ptr是shared_ptr<指向一个ABCImpl对象。在调用函数中,abc_ptr将调用ABC类中的成员函数之一(func_in_ABC)。编译成功。但是当我使用nm或objdump时,我只能看到abc_ptr的符号。调用函数中没有显示func_in_ABC()的符号。>

有谁知道为什么,或者我如何在调用函数中获得func_in_ABC()符号的输出?

代码如下:在ABC.h:

#include <boost/shared_ptr.hpp>
class ABC
{
    public:
        virtual void func_in_ABC(const int param) = 0;
};
typedef boost::shared_ptr<ABC> ABCPtr;
ABCPtr get_ABC_ptr();
在ABCImpl.h:

#include "ABC.h"
class ABCImpl : public 
{
    public:
        ABCImpl() {}
        void func_in_ABC(const int param);
    private:
        int data;
};
在ABCImpl.cpp:

#include "ABCImpl.h"
ABCPtr get_ABC_ptr()
{
        return ABCPtr(new ABCImpl());
}
void ABCImpl::func_in_ABC(const int param)
{
    data = param;
}

调用者函数d.p p:

#include "D.h"
#include "ABC.h"
void D::call_ABC()
{
    ABCPtr abc_ptr = get_ABC_ptr();
    abc_ptr->func_in_ABC(100);
}

D.o从nm的输出:

         U _Unwind_Resume
         U get_ABC_ptr()
0000000000000000 T D::call_ABC()
0000000000000000 W boost::shared_ptr<ABC>::operator->() const
0000000000000000 r boost::shared_ptr<ABC>::operator->() const::__PRETTY_FUNCTION__
         U __assert_fail
         U __gxx_personality_v0

如果我改变了ABC.h中func_in_ABC的定义,d.p p的编译将会失败。我认为在编译D.o时会检查类ABC的定义,但是为什么我找不到调用者映射到ABC定义的符号?

由于func_in_ABC是一个虚函数,因此实际上不需要符号名称来调用它。您只需要将该特定虚函数的偏移量放入虚表。

如果您使func_in_ABC非虚拟,您应该看到nm输出中出现符号。