I'd like to use libdl to dynamically load C++ in general. The problem is identifying symbols at runtime that have been name mangled.
As described here, one solution is to remove name mangling by using extern "C".
http://www.tldp.org/HOWTO/C++-dlopen/theproblem.html
This solution has the drawback of limiting dynamically loaded resources to C style interfaces. Dynamically loaded functions cannot, for instance, be overloaded functions.
What is a good way to overcome this limitation?
One possible solution would be tools to name mangle the library source code with an accompanying function to get the mangled names when the library needs to be linked. Does llvm provide tools for this?
Maybe a clumsy solution would be a function that takes a function signature, creates dummy code with a function that has the signature, pipes into the compiler that was used with a flag for generating assembly, parses the output to retrieve the mangled name, and returns the mangled name as a string. The string could then be passed to dlsym().
To keep the problem concrete, here are two example programs that illustrate something the extern "C" solution can't dynamically load without modifying library code. The first dynamically links a library in traditional C++ fashion. The second uses dlopen. Linking an overloaded function in the first program is simple. There's no simple way to link the overloaded function in the second program.
Program 1: Loadtime Dynamic Linking
main.cpp
// forward declarations of functions that will be linked
void say(int);
void say(float);
int main() {
int myint = 3;
say(myint);
float myfloat = 5.0f;
say(myfloat);
}
say.cpp
#include <iostream>
//extern "C" function signatures would collide
//extern "C" void say(int a) {
void say(int a) {
std::cout << "The int value is " << a << ".\n";
}
//extern "C" void say(float a) {
void say(float r) {
std::cout << "The float value is " << r << ".\n";
}
output
$ ./main
The int value is 3.
The float value is 5.
Program 2: Runtime Dynamic Linking
main_with_dl.cpp
#include <iostream>
#include <dlfcn.h>
int main() {
// open library
void* handle = dlopen("./say_externC.so", RTLD_LAZY);
if (!handle) {
std::cerr << "dlopen error: " << dlerror() << '\n';
return 1;
}
// load symbol
typedef void (*say_t)(int);
// clear errors, find symbol, check errors
dlerror();
say_t say = (say_t) dlsym(handle, "say");
const char *dlsym_error = dlerror();
if (dlsym_error) {
std::cerr << "dlsym error: " << dlsym_error << '\n';
dlclose(handle);
return 1;
}
// use function
int myint = 3;
say(myint);
// can't load in void say(float)
// float myfloat = 5.0f;
// say(myfloat);
// close library
dlclose(handle);
}
output
$ ./main_with_dl
The int value is 3.
Compiling
Makefile
CXX = g++
all: main main_with_dl say_externC.so
main: main.cpp say.so
$(CXX) -o $@ $^
main_with_dl: main_with_dl.cpp
$(CXX) -o $@ $<
%.so : %.cpp
$(CXX) -shared -o $@ $<
.PHONY: clean
clean:
rm main main_with_dl say.so say_externC.so
dlsym
, but it occurs to me that if you're trying to call an overloaded function and a conversion will be necessary then that's not enough to cut it. Since there's no way (yet) in C++ to determine the signature of an overloaded function that would be called after conversions, Your only option I know of is to make wrappers that resolve the overloads in Program2 that can delegate to Program1. Unless...typeid
? – Coopnm
on your dynamic library. 2. Runc++filt
on the output ofnm
. 3. Voila! You have a table that maps between mangled and demangled form of names. 4. Use it. – Professionalize