17

如何从库本身中获取共享库的路径?

换句话说,假设库 X 是使用 加载的dlopen(),我如何才能访问用于从库本身中加载所述库的路径?

请注意,我不能让首先加载库的代理将这个参数交给我。

更新:这是使用静态变量的方法:

std::string wdir;

namespace {
    class dynamic_library_load_unload_handler {
         public:
              dynamic_library_load_unload_handler(){
                    Dl_info dl_info;
                    dladdr((void *) NP_Initialize, &dl_info);

                    std::string path(dl_info.dli_fname);
                    wdir = path.substr( 0, path.find_last_of( '/' ) +1 );
              }
              ~dynamic_library_load_unload_handler(){
                    // Code to execute when the library is unloaded
              }
    } dynamic_library_load_unload_handler_hook;
}
4

2 回答 2

21

动态链接器实际上搜索了几个地方来找到每个动态库。其中包括(来自 man ld.so):

  • 环境变量给出的路径LD_LIBRARY_PATH
  • 烘焙到二进制文件中的路径加载DT_RUNPATH条目下的库
  • 缓存文件 /etc/ld.so.cache
  • /lib 和 /usr/lib

如果您想获取特定共享库的路径,我会推荐该dladdr功能。从手册页:

函数 dladdr() 接受一个函数指针并尝试解析它所在的名称和文件。信息存储在 Dl_info结构中:

typedef struct {
    const char *dli_fname;  /* Pathname of shared object that
                               contains address */
    void       *dli_fbase;  /* Address at which shared object
                               is loaded */
    const char *dli_sname;  /* Name of nearest symbol with address
                               lower than addr */
    void       *dli_saddr;  /* Exact address of symbol named
                               in dli_sname */
} Dl_info;

如果找不到与 addr 匹配的符号,则dli_snamedli_saddr设置为NULL

dladdr()错误返回 0,成功返回非零。

所以你只要给它一个函数指针,它就会给你提供它的文件的名称和一堆其他信息。因此,例如,您可以在库中使用构造函数对其自身调用 this 以找出库的完整路径:

#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>

__attribute__((constructor))
void on_load(void) {
    Dl_info dl_info;
    dladdr((void *)on_load, &dl_info);
    fprintf(stderr, "module %s loaded\n", dl_info.dli_fname);
}

此函数也适用于具有相同语义的 OS X。

于 2009-11-05T15:24:44.893 回答
0

由于 dl_info.dli_fname 在 android 中并不总是包含完整路径(请参见此处),我们需要解析 /proc/self/maps 以获得完整路径。

string GetSelfPath()
{
    string selfPath;
    Dl_info di;
    dladdr((void*)GetSelfPath, &di);
    LOGC("GetSelfPath:%08X,dli_fbase:%08X,dli_saddr:%08X,dli_fname:%s,dli_sname:%s", GetSelfPath, di.dli_fbase, di.dli_saddr, di.dli_fname, di.dli_sname);
    //dl_info.dli_fname not always contain full path in android,see android.googlesource.com/platform/bionic/+/… line 141
    if (strrchr(di.dli_fname, '/') != NULL)
    {
        selfPath=di.dli_fname;
    }
    else selfPath= GetPathByFileName(di.dli_fname);
    LOGC("self path:%s", selfPath.c_str());
    return selfPath;
}

string GetPathByFileName(string targetFilename)
{
    FILE *fp = fopen("/proc/self/maps", "r");
    if (NULL == fp) {
        return "";
    }
    const size_t BUFFER_SIZE = 256;
    char buffer[BUFFER_SIZE] = "";
    char path[BUFFER_SIZE] = "";

    while (fgets(buffer, BUFFER_SIZE, fp)) {
        if (sscanf(buffer, "%*llx-%*llx %*s %*s %*s %*s %s", path) == 1) {
            char *bname = basename(path);
            LOGC("check basename[%s]", bname);
            if (strcasecmp(bname, targetFilename.c_str()) == 0) {
                fclose(fp);
                return path;
            }
        }
    }
    fclose(fp);
    return "";
}
于 2019-02-19T10:29:13.927 回答