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

查看:90
本文介绍了扩展动态链接共享库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new at C, so sorry for my lack of knowledge (my C-book here is really massive :)

我想延长与封闭源代码的共享库(libcustomer.so),但公众所知的API。

I would like to extend a shared library (libcustomer.so) with closed source, but public known api.

是这样的可能吗?


  1. 重命名为libcustomer.so liboldcustomer.so

  2. 创建一个扩展的共享库libcustomer.so(以便其他人含蓄地使用扩展的)

  3. 链接liboldcustomer.so到我的扩展libcustomer.so通过-loldcustomer

  4. 直接转发任何额外的未实现的方法到老liboldcustomer.so

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

I don't think it would work that way (the name is compiled into the .so, isn't it?). But what's the alternative?

有关#4:有没有做到这一点的一般方式,还是我写了一个名为像旧的方法,并调用转发(如何?)

For #4: is there a general way to do this, or do I have to write a method named like the old one and forward the call (how?)?

由于原libcustomer.so(= liboldcustomer.so)可随时更改,所有的东西应该动态地工作。

Because the original libcustomer.so (=liboldcustomer.so) can change from time to time, all that stuff should work dynamically.

有关安全方面的原因,我们的系统没有LD_ preLOAD(否则我会采取:()。

For security reasons, our system has no LD_PRELOAD (otherwise I would take that :( ).

想想扩展验证的检查和放大器;一些更好的NPE-装卸。

Think about extended validation-checks & some better NPE-handlings.

在此先感谢您的帮助!

编辑:

我只是执行我的扩展如图所示的答案,但我都不得不时刻与一个未处理的情况:

I'm just implementing my extension as shown in the answer, but I have one unhandled case at the moment:

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

How can I "proxy" the structs from the extended library?

例如我有这样的:

customer.h:

customer.h:

struct customer;

customer.c:

customer.c:

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

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

Now, in my customer-extension.c I am writing all the public methods form customer.c, but how do I "pass-thru" the structs?

您的时间和放大器非常感谢;帮助!

Many thanks for your time & help!

推荐答案

所以,你必须用OldLib

So you have OldLib with

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

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

The step 4 might look like creating another library with some static initialization.

与内容创建NewLib:

Create NewLib with contents:

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。

The static_global_init and all the func_ptr's can be autogenerated if you have some description of the old API. After the NewLib is created, you certainly can replace the OldLib.

这篇关于扩展动态链接共享库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆