簡體   English   中英

在共享庫上實現缺失符號

[英]Implement missing symbol on a shared library

我有一個第三方庫(比如說libfoobar.so ),它依賴於另一個名為libutils.so的第三方庫。 我只有舊版本的libutils.so ,他的舊版本缺少一個僅存在於新版本中的符號(導出函數)。

我可以在一個新的共享庫中編碼 function(比如說libwrapper.so ):

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

現在?? 如何“告訴” libfoobar.so使用這個 function,已經嘗試過:

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

由於兼容性原因,我無法使用“新版本”。 有什么解決辦法嗎?

好的,我以一種奇怪的方式解決了這個問題,創建了一個包含所用函數的“代理”庫。

按照上一個示例進行操作:

  1. 在十六進制編輯器中打開libfoobar.so並修補(更改)到新共享庫的鏈接,用libutilx.so替換字符串libutils.so (尊重字符數)
  2. 還要更改 SONAME,在本例中,通過LIBUTILX更改LIBSUTILS
  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 output 將是:

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

干杯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM