【发布时间】:2017-01-10 23:42:10
【问题描述】:
我无法通过使用dladdr 的间接函数获得对elf 的gnu 扩展。
在下面的例子中fabs和sin是libm中的两个动态函数,其中sin也是一个间接函数。从指针查找fabs 效果很好,但找不到sin。我尝试了dlopen 和-rdynamic 的各种标志,但均未成功。
调试器显示如何从gnu-indirect-function variable 评估sin 到__sin_avx。
我在这里遗漏了什么还是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就可以工作。