在OSX上从dlopen句柄中查找路径名

sta*_*oat 12 c macos dyld

我已经dlopen()编写了一个库,我想从它传递给我的句柄反转回到共享库的完整路径名.在Linux和朋友们,我知道我可以dlinfo()用来获取链接图并迭代这些结构,但我似乎无法在OSX上找到模拟.我能做的最接近的事情是:

  • 使用dyld_image_count()dyld_get_image_name()迭代所有当前打开的库,希望我能猜出哪一个对应于我的句柄

  • 不知何故找到一个符号,它生活在我拥有的手柄里面,然后传递给它dladdr().

如果我对我刚刚打开的库中的符号名称有先验知识,我就可以dlsym()然后使用dladdr().这很好.但是在一般情况下,我不知道这个共享库中有什么内容,我需要能够枚举符号才能做到这一点,我不知道该怎么做.

因此,dlopen非常感谢有关如何从其句柄查找库的路径名的任何提示.谢谢!

0xc*_*ced 16

以下是如何获取返回的句柄的绝对路径dlopen.

  1. 为了获得绝对路径,您需要调用该dladdr函数并检索该Dl_info.dli_fname字段.
  2. 要调用该dladdr函数,您需要为其指定一个地址.
  3. 为了获得给定句柄的地址,您必须dlsym使用符号调用该函数.
  4. 为了从加载的库中获取符号,您必须解析库以查找其符号表并迭代符号.您需要找到外部符号,因为dlsym只搜索外部符号.

把它们放在一起就可以了:

#import <dlfcn.h>
#import <mach-o/dyld.h>
#import <mach-o/nlist.h>
#import <stdio.h>
#import <string.h>

#ifdef __LP64__
typedef struct mach_header_64 mach_header_t;
typedef struct segment_command_64 segment_command_t;
typedef struct nlist_64 nlist_t;
#else
typedef struct mach_header mach_header_t;
typedef struct segment_command segment_command_t;
typedef struct nlist nlist_t;
#endif

static const char * first_external_symbol_for_image(const mach_header_t *header)
{
    Dl_info info;
    if (dladdr(header, &info) == 0)
        return NULL;

    segment_command_t *seg_linkedit = NULL;
    segment_command_t *seg_text = NULL;
    struct symtab_command *symtab = NULL;

    struct load_command *cmd = (struct load_command *)((intptr_t)header + sizeof(mach_header_t));
    for (uint32_t i = 0; i < header->ncmds; i++, cmd = (struct load_command *)((intptr_t)cmd + cmd->cmdsize))
    {
        switch(cmd->cmd)
        {
            case LC_SEGMENT:
            case LC_SEGMENT_64:
                if (!strcmp(((segment_command_t *)cmd)->segname, SEG_TEXT))
                    seg_text = (segment_command_t *)cmd;
                else if (!strcmp(((segment_command_t *)cmd)->segname, SEG_LINKEDIT))
                    seg_linkedit = (segment_command_t *)cmd;
                break;

            case LC_SYMTAB:
                symtab = (struct symtab_command *)cmd;
                break;
        }
    }

    if ((seg_text == NULL) || (seg_linkedit == NULL) || (symtab == NULL))
        return NULL;

    intptr_t file_slide = ((intptr_t)seg_linkedit->vmaddr - (intptr_t)seg_text->vmaddr) - seg_linkedit->fileoff;
    intptr_t strings = (intptr_t)header + (symtab->stroff + file_slide);
    nlist_t *sym = (nlist_t *)((intptr_t)header + (symtab->symoff + file_slide));

    for (uint32_t i = 0; i < symtab->nsyms; i++, sym++)
    {
        if ((sym->n_type & N_EXT) != N_EXT || !sym->n_value)
            continue;

        return (const char *)strings + sym->n_un.n_strx;
    }

    return NULL;
}

const char * pathname_for_handle(void *handle)
{
    for (int32_t i = _dyld_image_count(); i >= 0 ; i--)
    {
        const char *first_symbol = first_external_symbol_for_image((const mach_header_t *)_dyld_get_image_header(i));
        if (first_symbol && strlen(first_symbol) > 1)
        {
            handle = (void *)((intptr_t)handle | 1); // in order to trigger findExportedSymbol instead of findExportedSymbolInImageOrDependentImages. See `dlsym` implementation at http://opensource.apple.com/source/dyld/dyld-239.3/src/dyldAPIs.cpp
            first_symbol++; // in order to remove the leading underscore
            void *address = dlsym(handle, first_symbol);
            Dl_info info;
            if (dladdr(address, &info))
                return info.dli_fname;
        }
    }
    return NULL;
}

int main(int argc, const char * argv[])
{
    void *libxml2 = dlopen("libxml2.dylib", RTLD_LAZY);
    printf("libxml2 path: %s\n", pathname_for_handle(libxml2));
    dlclose(libxml2);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果您运行此代码,它将产生预期的结果: libxml2 path: /usr/lib/libxml2.2.dylib


sta*_*oat 7

在使用0xced提供的解决方案大约一年后,我们发现了一种更简单的替代方法,避免了一种(相当罕见的)故障模式; 具体来说,因为0xced的代码片段遍历当前加载的每个dylib,找到第一个导出的符号,尝试在当前正在搜索的dylib中解析它,如果在该特定dylib中找到该符号则返回正数,如果从任意库中导出的第一个符号恰好出现在您当前正在搜索的dylib中.

我的解决方案是用来_dyld_get_image_name(i)获取每个图像加载的绝对路径,dlopen()该图像,并比较句柄(在屏蔽掉dlopen()由于使用的东西而设置的任何模式位之后RTLD_FIRST)以确保这个dylib实际上是与句柄传递给我的函数.

这里可以看到完整的功能,作为Julia语言的一部分,相关部分复制如下:

// Iterate through all images currently in memory
for (int32_t i = _dyld_image_count(); i >= 0 ; i--) {
    // dlopen() each image, check handle
    const char *image_name = _dyld_get_image_name(i);
    uv_lib_t *probe_lib = jl_load_dynamic_library(image_name, JL_RTLD_DEFAULT);
    void *probe_handle = probe_lib->handle;
    uv_dlclose(probe_lib);

    // If the handle is the same as what was passed in (modulo mode bits), return this image name
    if (((intptr_t)handle & (-4)) == ((intptr_t)probe_handle & (-4)))
        return image_name;
}
Run Code Online (Sandbox Code Playgroud)

请注意,诸如返回类型的jl_load_dynamic_library()包装器之类的函数,但代码的精神保持不变.dlopen()libuv