我无法通过间接函数获得gnu扩展到elf,与dladdr
一起使用。如何使用dladdr获取gnu间接函数的名称?
在下面的例子中,fabs
和sin
是libm
中的两个动态函数,其中sin
也是一个间接函数。从它的指针查找fabs
效果不错,但找不到sin
。我已尝试各种标志dlopen
和-rdynamic
没有成功。
调试器显示如何从gnu-indirect-function variable
到__sin_avx
评估sin
。
我在这里丢失了些什么或者是间接功能dladdr
不支持?
/*
compiled with g++-5 -fPIC -ldl
*/
#include <cmath>
#include <dlfcn.h>
#include <iostream>
char const * name (void * arg)
{
void * h = dlopen (NULL, RTLD_LAZY);
if (h)
{
Dl_info res;
int status = dladdr (arg, & res);
dlclose (h);
if (status && res.dli_sname && arg == res.dli_saddr)
return res.dli_sname;
}
return "";
}
int main()
{
std::cout << fabs (0.0) << " " << name ((void *) fabs) << std::endl; // found
std::cout << sin (0.0) << " " << name ((void *) sin) << std::endl; // not found
}
有趣的,如果你删除'-fPIC',管理工作。 – yugr