-->

扩展动态链接共享库?(Extend a dynamic linked shared library?

2019-08-07 18:02发布

我在C新型,我缺乏知识很抱歉(我这里C-书真的是巨大的:)

我想延长与闭源,但公共已知API共享库(libcustomer.so)。

是这样的可能吗?

  1. 重命名libcustomer.so到liboldcustomer.so
  2. 创建扩展共享库libcustomer.so(以便其他人隐式地使用扩展的一个)
  3. 链接liboldcustomer.so到我的扩展libcustomer.so通过-loldcustomer
  4. 直接转发任何额外的未实现的方法到老“liboldcustomer.so”

我不认为这会工作方式(姓名被编译成。所以,不是吗?)。 但是,什么是另类?

对于4:有没有做到这一点的一般方式,或者说我必须写一个名为像旧的方法和转发呼叫(如何?)?

因为原来libcustomer.so(= liboldcustomer.so)可以随时改变,所有的东西应该工作动态。

出于安全原因,我们的系统没有LD_PRELOAD(否则我会采取:()。

想想扩展验证的检查和一些更好的NPE-handlings。

在此先感谢您的帮助!

编辑:

我只是执行我的扩展如图所示的答案,但我目前所面对的一个未处理的情况下:

我如何“代理”了从扩展库结构?

例如,我有这样的:

customer.h:

struct customer;

customer.c:

struct customer {
    int children:1;
    int age;
    struct house *house_config;
};

现在,在我的客户extension.c我写的所有的公共方法形成customer.c,但我怎么“直通”的结构?

非常感谢您的时间和帮助!

Answer 1:

所以,你必须OldLib与

void func1();
int  func2();
... etc

步骤4可能看起来像一些静态初始化创建另一个库。

与内容创建NewLib:

void your_func1();

void (*old_func1_ptr)() = NULL;
int  (*old_func2_ptr)() = NULL;

void func1()
{
    // in case you don't have static initializers, implement lazy loading
    if(!old_func1_ptr)
    {
       void* lib = dlopen("OldLibFileName.so", RTLD_NOW);
       old_func1_ptr = dlsym(lib, "func1");
    }

    old_func1_ptr();
}

int func2()
{
    return old_func2_ptr();
}

// gcc extension, static initializer - will be called on .so's load
// If this is not supported, then you should call this function
// manually after loading the NewLib.so in your program.
// If the user of OldLib.so is not _your_ program,
// then implement lazy-loading in func1, func2 etc. - check function pointers for being NULL
// and do the dlopen/dlsym calls there.
__attribute__((constructor))
void static_global_init()
{
   // use dlfcn.h
   void* lib = dlopen("OldLibFileName.so", RTLD_NOW);

   old_func1_ptr = dlsym(lib, "func1");
   ...
}

static_global_init和所有的func_ptr的可以,如果你有旧的API的一些描述来自动生成。 创建NewLib后,你当然可以更换OldLib。



文章来源: Extend a dynamic linked shared library?