我無法通過間接函數獲得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