Detecting duplicate characters in dlopen

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 .

+4
source share
1 answer

I would like to detect duplicate characters in dlopen and warn about them.

I do not think dlopen can do this.

Even if possible, detecting this issue at runtime is probably too late. You should detect this problem during assembly, and it is trivial to do this as a step after assembly:

 nm -D your_plugin_dir/*.so | egrep ' [TD] ' | cut -d ' ' -f3 | sort | uniq -c | grep -v ' 1 ' 

If you get any output, you have duplicate characters (some duplicate characters may be fine, you will have to filter out the “known good” duplicates).

+1
source

Source: https://habr.com/ru/post/1392645/


All Articles