使用dlopen查找dylib版本

adk*_*adk 10 dylib dlopen

有没有办法使用其路径找到dylib的版本?我正在寻找接受与dlopen相同的参数的东西.我查看了NSVersionOfRunTimeLibrary,但是从我对文档的阅读看起来它看起来像是获取当前dylib的版本,而不是路径中指定的版本.

谢谢

F'x*_*F'x 14

运行otool -L它,它将显示其实际版本.我选择了libSystem.B,因为它在10.4和10.5 SDK中有不同的版本:

$ otool -L /Developer/SDKs/MacOSX10.4u.sdk/usr/lib/libSystem.B.dylib
/Developer/SDKs/MacOSX10.4u.sdk/usr/lib/libSystem.B.dylib:
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 88.3.11)
    /usr/lib/system/libmathCommon.A.dylib (compatibility version 1.0.0, current version 220.0.0)
$ otool -L /Developer/SDKs/MacOSX10.5.sdk/usr/lib/libSystem.B.dylib 
/Developer/SDKs/MacOSX10.5.sdk/usr/lib/libSystem.B.dylib:
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 111.1.4)
    /usr/lib/system/libmathCommon.A.dylib (compatibility version 1.0.0, current version 292.4.0)
Run Code Online (Sandbox Code Playgroud)

(看第一个如何有88.3.11版本,而第二个有111.1.4版本).此示例还显示并非所有库都是指向其中包含版本号的文件的符号链接:

$ ll /Developer/SDKs/MacOSX10.*.sdk/usr/lib/libSystem.B.dylib
-rwxr-xr-x  1 root  wheel   749K May 15  2009 /Developer/SDKs/MacOSX10.4u.sdk/usr/lib/libSystem.B.dylib
-rwxr-xr-x  1 root  wheel   670K May 15  2009 /Developer/SDKs/MacOSX10.5.sdk/usr/lib/libSystem.B.dylib
-rwxr-xr-x  1 root  wheel   901K Sep 25 00:21 /Developer/SDKs/MacOSX10.6.sdk/usr/lib/libSystem.B.dylib
Run Code Online (Sandbox Code Playgroud)

这里,文件的名称中没有版本号.

编辑:第二个解决方案是NSVersionOfRunTimeLibrary在测试程序中使用,在该程序中强制加载要检查的库.libversion从以下C源创建程序:

#include <stdio.h>
#include <mach-o/dyld.h>

int main (int argc, char **argv)
{
  printf ("%x\n", NSVersionOfRunTimeLibrary (argv[1]));
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

然后,你这样称呼它:

$ DYLD_INSERT_LIBRARIES=/usr/lib/libpam.2.dylib ./a.out libpam.2.dylib
30000
Run Code Online (Sandbox Code Playgroud)

(此处,版本号打印为十六进制,但您可以根据自己的需要进行调整.)

  • 更好的答案,删除我的. (2认同)

mfa*_*kas 5

你可以在这里查看NSVersionOfRunTimeLibrary的源代码:http: //www.opensource.apple.com/source/dyld/dyld-132.13/src/dyldAPIsInLibSystem.cpp

基于此,您可以创建自己的替换版本if(names_match(install_name, libraryName) == TRUE).if(strcmp(_dyld_get_image_name(i), libraryName) == 0) 这将解决原始期望没有完整路径的库名称的问题,编辑后的版本需要完整路径,但它仍将在加载的dylib中搜索.

#include <mach-o/dyld.h>
int32_t
library_version(const char* libraryName)
{
    unsigned long i, j, n;
    struct load_command *load_commands, *lc;
    struct dylib_command *dl;
    const struct mach_header *mh;

    n = _dyld_image_count();
    for(i = 0; i < n; i++){
        mh = _dyld_get_image_header(i);
        if(mh->filetype != MH_DYLIB)
        continue;
        load_commands = (struct load_command *)
#if __LP64__
                ((char *)mh + sizeof(struct mach_header_64));
#else
                ((char *)mh + sizeof(struct mach_header));
#endif
        lc = load_commands;
        for(j = 0; j < mh->ncmds; j++){
        if(lc->cmd == LC_ID_DYLIB){
            dl = (struct dylib_command *)lc;
            if(strcmp(_dyld_get_image_name(i), libraryName) == 0)
            return(dl->dylib.current_version);
        }
        lc = (struct load_command *)((char *)lc + lc->cmdsize);
        }
    }
    return(-1);
}
Run Code Online (Sandbox Code Playgroud)