If all the liba.so
library is dlopen
ed using RTLD_GLOBAL
, then you can use dlsym(RTLD_DEFAULT, "expA")
to find the symbol without needing to reopen the library.
If the liba.so
library is dlopen
ed using RTLD_LOCAL
then you will need to obtain a handle to the library using dlopen
again within your own libmine.so
. Note the following:
If the same library is loaded again with dlopen(), the same file handle is returned. The dl library maintains reference counts for library handles, so a dynamic library is not deallocated until dlclose() has been called on it as many times as dlopen() has succeeded on it. The _init() routine, if present, is only called once. But a subsequent call with RTLD_NOW may force symbol resolution for a library earlier loaded with RTLD_LAZY.
i.e. it's the same copy of the library.
the mechanism is (pseudo), assuming expA is a function: int expA(int value)
:
int (*fpointer)(int) = NULL;
void *handle = dlopen("liba.so", RTLD_LAZY | RTLD_LOCAL);
if (handle != NULL) {
void *symbol = dlsym(handle, "expA");
if (symbol != NULL) {
fpointer = (int (*)(int ))symbol;
}
}
That's pseudo code, with little to no error handling.