【问题标题】:Implement missing symbol on a shared library在共享库上实现缺失符号
【发布时间】:2021-03-21 07:34:46
【问题描述】:

我有一个第三方库(比如说libfoobar.so),它依赖于另一个名为libutils.so 的第三方库。我只有libutils.so的老版本,他的老版本少了一个新版本才有的符号(导出函数)。

我可以在一个新的共享库中编写函数代码(比如说libwrapper.so):

extern "C" int missing_function() {
    return 123;
}

然后……现在??如何“告诉”libfoobar.so 使用此功能,已经尝试过:

  • libfoobar.so之前加载libwrapper.so
  • 使用带有patchelf --add-needed libwrapper.so libfoobar.so 的补丁工具。但我得到dlopen failed: empty/missing DT_HASH in "libfoobar.so" (built with --hash-style=gnu?)

由于兼容性原因,我无法使用“新版本”。有什么解决办法吗?

【问题讨论】:

    标签: c unix shared-libraries


    【解决方案1】:

    好的,我以一种奇怪的方式解决了这个问题,创建了一个“代理”库来包装所使用的函数。

    按照上一个示例进行操作:

    1. 在十六进制编辑器中打开 libfoobar.so 并修补(更改)指向新共享库的链接,将字符串 libutils.so 替换为 libutilx.so(注意字符数)
    2. 同时更改 SONAME,在本例中将 LIBSUTILS 更改为 LIBUTILX
    3. 编码包装器并编译为libutilx.so。下面是 C 代码示例
    4. libfoobar.so 和DONE 放在同一个文件夹中。
    #include <dlfcn.h>
    
    void *lib = dlopen("libutils.so", RTLD_LAZY);// original lib
    
    
    // delegates
    void *(*ORIGmalloc)(size_t size);
    int (*ORIGpthread_setspecific)(pthread_key_t key, const void *value);
    
    
    // functions
    extern "C" void *malloc(size_t size) {
        return (*ORIGmalloc)(size_t);
    }
    extern "C" int pthread_setspecific(pthread_key_t key, const void *value) {
        return (*ORIGpthread_setspecific)(pthread_key_t, value);
    }
    
    // implements missing functions in libutils.so required by libfoobar.so
    extern "C" const char *get_greeting_string() {
       return "Hello from libutilx.so !!";
    }
    
    extern "C" int some_number() {
       return 12345;
    }
    
    
    // assigns (just an example, you must check for errors)
    // with the attribute "constructor" this function is called after the lib is opened
    void  __attribute__((constructor)) assign_delegates() {
        *(void **) (&ORIGmalloc) = dlsym(lib, "malloc");
        *(void **) (&ORIGpthread_setspecific) = dlsym(lib, "pthread_setspecific");
    }
    

    要知道导入了什么函数,请使用命令行实用程序objdump。示例objdump -T libfoobar.so 输出将是:

    00000000      DF *UND*  00000000  LIBUTILS        pthread_setspecific
    00000000      DF *UND*  00000000  LIBUTILS        malloc
    00000000      DO *UND*  00000000  LIBUTILS        __sF
    

    干杯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-28
      相关资源
      最近更新 更多