问题描述
我有一个共享库 libmain.so
,由宿主程序使用 dlopen("libmain.so",RTLD_LOCAL)
加载,在某些情况下, libmain.so
将同时使用 dlopen
加载另一个共享库 libpatch.so
.问题是 libpatch.so取决于libmain.so中的符号,那么我该如何解决呢?
I have a shared library libmain.so
, loaded by the host program with dlopen("libmain.so", RTLD_LOCAL)
, and under some conditions, libmain.so
will load another shared library, libpatch.so
, also with dlopen
. The problem is, libpatch.so depends on symbols inside libmain.so, so how can I solve this?
-
由于权限原因,不能将
RTLD_LOCAL
更改为RTLD_GLOBAL
.
那里是一个与此非常相似的问题首先,解决该问题的方法是使 libpatch.so
成为 libmain.so
的依赖项,以便在 libmain.so
时将其加载已加载,但我的 libpatch.so
应该有条件地加载,当链接 libmain.so
时, libpatch.so
可能不存在.
There is a question quite similar to this one, the solution to that problem is to make libpatch.so
a dependency of libmain.so
, so it will be loaded when libmain.so
is loaded, but my libpatch.so
should be loaded conditionally, libpatch.so
might not be there when libmain.so
is linked.
我要解决的原始问题是:
the original problem I want to solve is:
进程运行时,我们可能会发现 libmain.so
中的函数 SomeFunction
中存在错误,,但是该进程无法重新启动,并且 libmain.so
无法重新加载,因此我们必须为 libpatch.so
提供错误修复功能 SomeFunction
,然后发送一个向进程发出信号,使其加载 libpatch.so
,并在 libpatch.so
中使用 SomeFunction
,而不是在 libmain中的越野车之一.so
.但是, SomeFunction
依赖于全局变量 GlobalVar
,并且它可能已在 libmain.so
中更改,因此我们要链接 SomeFunction
到 libmain.so
里面,但是 libmain.so
是用 RTLD_LOCAL
加载的,GlobalVar
不能加载 libpatch.so
时被引用.
When the process is running, we may find that there is a bug in function SomeFunction
inside libmain.so
, but the process cannot be restarted and libmain.so
cannot be reloaded, so we have to provide a libpatch.so
with bug-fixed function SomeFunction
, and send a signal to the process, make it to load libpatch.so
, and use SomeFunction
in libpatch.so
instead the buggy one in libmain.so
. However, SomeFunction
depends on a global variable GlobalVar
, and it might have changed in libmain.so
, so we want to link SomeFunction
to it inside libmain.so
, but libmain.so
is loaded with RTLD_LOCAL
, GlobalVar
cannot be referenced when libpatch.so
is loading.
推荐答案
从libmain.so编译libpatch.so所需的符号列表.建立一个包含这些符号地址的数据结构.并非针对libmain.so而是针对此数据结构构建libpatch.so.将其实例传递给libpatch.so初始化函数.
Compile a list of symbols from libmain.so needed by libpatch.so. Build a data structure that contains addresses of these symbols. Build libpatch.so not against libmain.so but against this data structure. Pass an instance of it to libpatch.so initialisation function.
这篇关于Linux共享库取决于dlopen使用RTLD_LOCAL打开的另一个共享库中的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!