linux环境 dladdr可以获取到依赖库的绝对路径吗?
时间: 2023-12-14 16:39:22 浏览: 63
可以的,可以使用dladdr函数获取当前函数所在模块的信息,其中包括模块的绝对路径。具体方法如下:
```c
#include <dlfcn.h>
#include <stdio.h>
void foo() {
void* addr = (void*)foo;
Dl_info info;
if (dladdr(addr, &info)) {
printf("module path: %s\n", info.dli_fname);
}
}
int main() {
foo();
return 0;
}
```
在这个例子中,通过调用`dladdr`函数获取`foo`函数的地址,并将其传递给`dladdr`函数,再通过`Dl_info`结构体接收返回值。如果`dladdr`函数返回成功,那么`info.dli_fname`就是模块的绝对路径。
相关问题
linux环境 dladdr函数获取不到动态库的绝对路径
在 Linux 环境下,`dladdr` 函数可以用来获取某个函数或变量所在的动态库的信息,包括动态库的绝对路径。但是有时候我们会发现,`dladdr` 函数返回的 `Dl_info` 结构体中的 `dli_fname` 成员并不是动态库的绝对路径,而只是动态库的文件名。
这个问题通常是由于动态库的加载方式导致的。在 Linux 中,动态库可以通过 `LD_LIBRARY_PATH` 环境变量来指定搜索路径。如果动态库被加载时,其路径在 `LD_LIBRARY_PATH` 中,那么 `dli_fname` 中就只包含动态库的文件名,而不是绝对路径。
为了解决这个问题,可以使用 `dl_iterate_phdr` 函数遍历当前进程中加载的所有动态库,然后通过比较动态库的基地址和 `dli_fbase` 成员来找到相应的动态库,从而获取其绝对路径。下面是一个示例代码:
```c
#include <dlfcn.h>
#include <link.h>
#include <stdio.h>
#include <string.h>
void* func;
int callback(struct dl_phdr_info* info, size_t size, void* data)
{
if (func >= (void*)info->dlpi_addr && func < (void*)(info->dlpi_addr + info->dlpi_phdr[0].p_vaddr + info->dlpi_phdr[0].p_memsz))
{
printf("Library path: %s\n", info->dlpi_name);
return 1;
}
return 0;
}
int main()
{
func = dlsym(RTLD_DEFAULT, "some_function");
struct Dl_info info;
if (dladdr(func, &info))
{
if (info.dli_fname[0] != '/')
{
dl_iterate_phdr(callback, NULL);
}
else
{
printf("Library path: %s\n", info.dli_fname);
}
}
return 0;
}
```
这个示例代码中,我们首先使用 `dlsym` 函数获取某个函数的地址,然后使用 `dladdr` 函数获取该函数所在的动态库的信息。如果 `dli_fname` 成员中包含了绝对路径,那么直接打印即可。否则,我们就使用 `dl_iterate_phdr` 函数遍历动态库,找到包含该函数的动态库,最后打印其绝对路径。
需要注意的是,使用 `dl_iterate_phdr` 函数需要链接 `libdl` 库。在编译时需要加上 `-ldl` 选项。
linux环境 dladdr获取到的是相对路径
是的,`dladdr()` 函数获取的是共享库的相对路径,而不是绝对路径。如果你想要获取绝对路径,可以使用 `realpath()` 函数将相对路径转换为绝对路径,例如:
```c
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
int main() {
void *handle = dlopen("libtest.so", RTLD_NOW);
if (handle == NULL) {
printf("dlopen error: %s\n", dlerror());
return 1;
}
Dl_info info;
if (dladdr(handle, &info) == 0) {
printf("dladdr error: %s\n", dlerror());
return 1;
}
char *abs_path = realpath(info.dli_fname, NULL);
if (abs_path == NULL) {
printf("realpath error\n");
return 1;
}
printf("The absolute path of the shared library is %s\n", abs_path);
dlclose(handle);
return 0;
}
```
这段代码打印出来的结果会是类似于 `/home/user/libtest.so` 的绝对路径。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)