1

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.

Kelvin Hu
  • 1,299
  • 16
  • 31
  • `LD_PRELOAD` the shared object `libpatch.so` when needed. – jww May 30 '19 at 07:47
  • @jww AFAIK, `LD_PRELOAD` trick loads a `so` before the program launches, however, when my host program launches, `libpatch.so` may be not ready, and I need to load it during the host program running, without restart the host program, can `LD_PRELOAD` achieve this? – Kelvin Hu May 30 '19 at 09:21
  • What problem are you *actually* trying to solve? See http://xyproblem.info/ – Employed Russian May 31 '19 at 03:12
  • @EmployedRussian Sorry for the unclear description, I have edited the question. – Kelvin Hu May 31 '19 at 04:05

2 Answers2

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.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
0

There are two way:

  1. compile libpatch.so with libmain.so
  2. dlopen libmain.so again in libpatch.so

detail ref: https://docs.oracle.com/cd/E19120-01/open.solaris/819-0690/chapter3-14/index.html