I been reading and trying to understand how symbols get resolved within a shared library in Linux. So here is a description of what I am faced with.
I am using an application (APP) that can load user created shared libraries to add features. I have two such libraries, LIB_A.so and LIB_B.so that perform separate things and do not depend on another to work. They are independently compiled and based on the compiler arguments (-fPIC
), will seem it will make the symbols interposable (from my research on the topic). So most symbols will be exported by default.
Now, there is this common code that both LIB_A and LIB_B uses that is compiled and statically linked with each library. The common code does not use any namespaces or static functions, so I am assuming they will be exported as well. Both LIB_A and LIB_B load and work as intended in the APP.
But what if there was a bug found inside the common code but only LIB_A can be recompiled because it needs the fixed code. My question is, when LIB_A is recompiled to pick up this change in the common code and get loaded in the APP, would there be separate copies of the common code for both LIB_A (that has the bug fix) and LIB_B (that does not have the bug fix) and each would use their respective copies or would both link and share one of the versions of the common code in both? Is there a way for me to discover the source of the symbol using a debugger perhaps?
To just to get answer questions ahead of time, I do not know which order the libraries will be loaded, I can not recompiled LIB_B to pick up the changes, only LIB_A. I do not have the source code of the APP to know how it's dynamically loading the libraries.
I know there is a great deal with the compiler flags but assume it's just -fPIC
, no -fvisibility-hidden
, -Wl,-Bsymbolic
, -fno-semantic-interposition
flags are set. Would these solve this issue if there was a conflict?
I looked at using the nm -D
command and I see some of the symbols are W, does that mean it will use an existing symbol if it already exists before using the one that was statically built with the library?
I been reading articles and search, but this one things I am not sure I 100% nailed down.
EDIT For further information, I am loading these libraries at runtime on demand. Would this change anything by using dlopen
with RTLD_LOCAL
vs RTLD_GLOBAL
? From the description, RTLD_LOCAL seems to prevent symbols from being loaded globally and therefore will not conflict or link to other symbols outside of the library?