[LLVMdev] How to call native functions from bytecode run in JIT?

Jan Rehders cmdkeen at gmx.de
Wed Jun 27 12:56:31 PDT 2007


Hello,

finally I tried a bit more but could not get the native calls to work.

I checked how dlsym works on Linux and OS X. On the latter I can  
dlsym any function in a dynamically loaded library as well as in the  
application. On Linux only functions in loaded dlls can be retrieved  
using dlsym. Adding -rdynamic as Nicolas suggested allows me to load  
functions from the exe, too. The following code works fine on Linux  
and OS X:

void* dllHandle = dlopen( "./codegen1.so", RTLD_LAZY );
void* exeHandle = dlopen( NULL, RTLD_LAZY );

// int get5() is contained in the dll, get6 in the app (both using  
extern "C")
getAndCall( dllHandle, "get5" );
getAndCall( exeHandle, "get6" );

void getAndCall(void* handle, char* name) {
   int (*get)() = (int(*)()) dlsym( handle, name );

   if( dlerror() == NULL && get != NULL )
     printf( "Calling %s() = %d\n", name, get() );
}

However if I use LLVM like described in my previous mails it only  
works on OS X and cannot find the functions on Linux. This is both  
true for functions in the exe as well as in a loaded dll (passing - 
lfoo or foo.so to the linker). Using addGlobalMapping does not help,  
neither.

Until now the dlsym behavior is the only relevant difference I could  
spot between the two systems. However -dynamic should remove this  
difference. Still LLVM does not find any functions apart from printf  
in the system libs. My code is identical on both systems.

> In gcc for Linux, you have the -rdynamic option that allows an
> executable to dlsym its symbols. Since llvm use dlsym to find the
> symbols, you could try with this option. That's what I did. And don't
> forget to use the C++ name if you compile with C++.

Do you have a working code sample (with LLVM) which I could compare  
to my own code?

Currently I'm pretty much out of ideas. The next thing to try would  
probably to install a debug version of LLVM and dive into the source  
using a debugger. But I fear I don't have enough time for this. I had  
a short look at some of the functions to identify libraries but could  
not make too much sense of it, yet.

greetings,
Jan




More information about the llvm-dev mailing list