首先这篇文章总结得不错
https://blog.csdn.net/stpeace/article/details/73302833
然后依然会碰到问题,这时候就要看具体的函数签名了
Name Mangling in C++
参考
http://blog.51cto.com/hipercomer/855223
nm工具的 –demangle 选项 可以让函数名可读
name demangling
C++的name mangling技术一般使得函数变得面目全非,而很多情况下我们在查看这些符号的时候并不需要看到这些函数name mangling之后的效果,而是想看看是否定义了某个函数,或者是否引用了某个函数,这对于我们调试程序是非常有帮助的。
所以需要一种方法从name mangling之后的符号变换为name mangling之前的符号,这个过程称之为name demangling.事实上有很多工具提供这些功能,最常用的就是c++file命令,c++filt命令接受一个name mangling之后的符号作为输入并输出demangling之后的符号。例如:
[lichao@sg01 name_mangling]$ c++filt _Z9test_funcRiPKcdSsf
test_func(int&, char const*, double, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, float)
一般更常用的方法为:
[lichao@sg01 name_mangling]$ nm func.o | c++filt
0000000000000060 t global constructors keyed to _Z9test_funcRiPKcdSsf
U _Unwind_Resume
0000000000000022 t __static_initialization_and_destruction_0(int, int)
0000000000000000 T test_func(int&, char const*, double, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, float)
U std::allocator<char>::allocator()
U std::allocator<char>::~allocator()
U std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)
U std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
U std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()
U std::ios_base::Init::Init()
U std::ios_base::Init::~Init()
0000000000000000 b std::__ioinit
U __cxa_atexit
U __dso_handle
U __gxx_personality_v0
0000000000000076 t __tcf_0
000000000000008e T main
另外使用nm命令也可以demangle符号,使用选项-C即可,例如:
[lichao@sg01 name_mangling]$ nm -C func.o
0000000000000060 t global constructors keyed to _Z9test_funcRiPKcdSsf
U _Unwind_Resume
0000000000000022 t __static_initialization_and_destruction_0(int, int)
0000000000000000 T test_func(int&, char const*, double, std::string, float)
U std::allocator<char>::allocator()
U std::allocator<char>::~allocator()
U std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)
U std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&)
U std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()
U std::ios_base::Init::Init()
U std::ios_base::Init::~Init()
0000000000000000 b std::__ioinit
U __cxa_atexit
U __dso_handle
U __gxx_personality_v0
0000000000000076 t __tcf_0
000000000000008e T main
又到了Last but not least important的时候了,还有一个特别重要的接口函数就是__cxa_demangle(),此函数的原型为:
namespace abi {
extern "C" char* __cxa_demangle (const char* mangled_name,
char* buf,
size_t* n,
int* status);
}
用于将mangled_name所指向的mangled进行demangle并将结果存放在buf中,n为buf的大小。status存放函数执行的结果,返回值为0表示执行成功。
下面是使用这个接口函数进行demangle的例子:
/* * Author: Chaos Lee * Description: Employ __cxa_demangle to demangle a mangling function name. * Date:2012/05/06 * */
#include<iostream>
#include<cxxabi.h>
using namespace std;
using namespace abi;
int main(int argc,char *argv[])
{
const char * mangled_string = "_Z9test_funcRiPKcdSsf";
char buffer[100];
int status;
size_t n=100;
__cxa_demangle(mangled_string,buffer,&n,&status);
cout<<buffer<<endl;
cout<<status<<endl;
return 0;
}
测试结果:
[lichao@sg01 name_mangling]$ g++ cxa_demangle.cpp -o cxa_demangle
[lichao@sg01 name_mangling]$ ./cxa_demangle
test_func(int&, char const*, double, std::string, float)
0