Linux shared library depends on symbols in another shared library opened by dlopen with RTLD_LOCAL

做~自己de王妃 提交于 2019-12-23 05:13:32

问题


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?

  1. Change RTLD_LOCAL to RTLD_GLOBAL is not an option due to permission reasons.

  2. 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.

EDIT: the original problem I want to solve is:

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.


回答1:


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.



来源:https://stackoverflow.com/questions/56373416/linux-shared-library-depends-on-symbols-in-another-shared-library-opened-by-dlop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!