Does dlopen overload already loaded dependencies? If so, what are the implications?

I have a program codenamed foo . foo depends on common.so and is related to it in the usual way (sorry, I don’t know the technical way to say this). When foo works, it dynamically loads bar.so with dlopen() . So far so good.

But, bar.so also depends on common.so . Will dlopen() reload common.so (from what I read, it recursively loads any required dependencies), or will it detect that it is already loaded? If he reloads it, can this cause problems in my program? Both foo and bar.so should see the changes in common.so that they make for static variables.

Perhaps I need to change the design or use -rdynamic (which I still don't understand well enough)?

+5
source share
1 answer

The POSIX specification for dlopen () says:

Only one copy of the executable object file must be an address space, even if dlopen () is called several times in reference to the executable file of the object, and even if pathanames are used to reference the executable file of the object.

On Linux, this is implemented using a reference counter; until dlclose is called an equal number of times, the shared object will remain resident.

[update]

I understand that you are asking for shared objects implicitly loaded as dependencies, but the same principle applies. Otherwise, many things will break ... In particular, global constructors in a common object will be executed several times, which will lead to chaos.

+2
source

All Articles