问题
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?
Change
RTLD_LOCALtoRTLD_GLOBALis not an option due to permission reasons.There is a question quite similar to this one, the solution to that problem is to make
libpatch.soa dependency oflibmain.so, so it will be loaded whenlibmain.sois loaded, but mylibpatch.soshould be loaded conditionally,libpatch.somight not be there whenlibmain.sois 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