Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems enabling RTTI in LLVM JIT-ed code

Tags:

c++

jit

llvm

rtti

I'm JIT-ting C++ code in a Windows application. I'm using LLVM/CLang 5.0, and the application was compiled using MsVc 2015.

While DLLs and executable code created with the two toolchains mix and match very well, I'm facing problems with the JIT-ted code that doesn't include variables that LLVM should generate automatically, like ??_7type_info@@6B@ and associated stuff, like \01??_R0H@8. They are registered as external only, and any use of them will cause the application to crash with

LLVM ERROR: Program used external function '??_7type_info@@6B@' which could not be resolved!

While I have found examples of adding user-defined variables to JIT-ted code, I could not find effective solutions to the problem of making JIT code refer to these internal variables, that are generated behind the curtains.

I believe I could provide the missing variables via a DLL compiled with CLang, with the tweaking of .ll but I would prefer a cleaner solution, merely configuring the JIT-ter engine.

Can anybody help me, please ?

like image 347
Hilton Fernandes Avatar asked Oct 21 '25 05:10

Hilton Fernandes


1 Answers

??_7type_info@@6B@ is the mangled name of the vtable for the std::type_info class, which is provided by one or other of the MSVC static libraries that gets linked implicitly, for example:

c:/Program Files (x86)/Microsoft Visual Studio/2017/Professional/VC/Tools/MSVC/14.15.26726/lib/x64/msvcrt.lib

To fix the LLVM lookup error you can export this symbol from your own DLL or EXE, and you can actually do this from your C++ code like this:

#pragma comment(linker, "/export:??_7type_info@@6B@")

See also https://learn.microsoft.com/en-us/cpp/build/reference/export-exports-a-function?view=vs-2017

You may also have to call the utility function below to ensure all your symbols are visible to LLVM

llvm::sys::DynamicLibrary::LoadLibraryPermanently(nullptr);

Not sure if that's strictly necessary in this case, but it's recommended by the LLVM JIT compiler tutorial.

like image 65
Raoouul Avatar answered Oct 23 '25 19:10

Raoouul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!