In my Linux application, I use the plugin architecture through dlopen . Shared objects are opened with
dlopen (path, RTLD_GLOBAL | RTLD_LAZY) `
The RTLD_GLOBAL option RTLD_GLOBAL necessary because plugins must access general RTTI information. Sometimes it happens that some plugins export the same symbols. This usually should not happen, but when it does, it leads to random segfaults, and it's hard to debug. Therefore, I would like to detect duplicate characters in dlopen and warn about them.
Is there any way to do this?
Here is a simple example to illustrate this. The code of the main executable file
#include <string> #include <dlfcn.h> #include <iostream> #include <cassert> typedef void (*Function)(); void open(const std::string& soname) { void* so = dlopen(soname.c_str(), RTLD_LAZY | RTLD_GLOBAL); if (!so) { std::cout << dlerror() << std::endl; } else { Function function = reinterpret_cast<Function>(dlsym(so, "f")); assert(function); function(); } } int main() { open("./a.so"); open("./b.so"); return 0; }
And it is built by g++ main.cpp -o main -ldl
a.so and b.so are built from
#include <iostream> void g() { std::cout << "a.cpp" << std::endl; } extern "C" { void f() { g(); } }
and
#include <iostream> void g() { std::cout << "b.cpp" << std::endl; } extern "C" { void f() { g(); } }
g++ -fPIC a.cpp -share -o a.so and g++ -fPIC b.cpp -share -o b.so respectively. Now, if I execute ./main , I get
a.cpp a.cpp
With RTLD_LOCAL I get
a.cpp b.cpp
but, as I already explained, I do not want RTLD_LOCAL .