动态加载时的库路径?

时间:2009-11-05 14:55:07

标签: linux shared-libraries dlopen

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

换句话说,假设库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;
}

2 个答案:

答案 0 :(得分:17)

动态链接器实际上搜索多个位置以查找每个动态库。这些包括(来自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_sname和   dli_saddr设置为NULL

     

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

所以你只需给它一个函数指针,它会给你提供它的文件的名称和一堆其他信息。因此,例如,你可以让一个库中的构造函数自己调用它来找出库的完整路径:

#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,具有相同的语义。

答案 1 :(得分:0)

由于dl_info.dli_fname在android中并不总是包含完整路径(请参见here),因此我们需要解析/ 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 "";
}