简体   繁体   中英

Problems enabling RTTI in LLVM JIT-ed code

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 ?

??_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://docs.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM