From libtooling to LLVM IR?

Hi all,

I’m wondering how difficult it is to go from the libtooling interface to the LLVM IR. The idea is to then use the JIT facilities to execute some C++ code.
It seems Libtooling requires far less code and far less adaptations to go from a code snippet to enything than clang itself (to get all the pieces together, it seems that it requires almost 50 lines whereas libtooling more or less has only one statement).

Any pointers for this?

Cheers,

Matthieu

Hi all,

So I managed to get something together from different bits available of Internet:

llvm::LLVMContext context;

std::unique_ptrclang::CodeGenAction action = std::make_uniqueclang::EmitLLVMOnlyAction(&context);

clang::tooling::runToolOnCode/WithArgs/(action.get(), fullfile);

std::unique_ptrllvm::Module module = action->takeModule();

llvm::InitializeNativeTarget();

llvm::InitializeNativeTargetAsmPrinter();

llvm::InitializeNativeTargetAsmParser();

llvm::EngineBuilder builder(std::move(module));

builder.setMCJITMemoryManager(std::make_uniquellvm::SectionMemoryManager());

builder.setOptLevel(llvm::CodeGenOpt::Level::Aggressive);

EE.reset(builder.create());

return reinterpret_cast(EE->getFunctionAddress(function));

Unfortunately, when calling builder.create, there is an ASAN error:

==18530==ERROR: AddressSanitizer: SEGV on unknown address 0x0000000000d8 (pc 0x0001114da737 bp 0x7ffee62a2530 sp 0x7ffee62a2510 T0)

==18530==The signal is caused by a READ memory access.

==18530==Hint: address points to the zero page.

#0 0x1114da736 in std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator >::basic_string(std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > const&) (libc++.1.dylib:x86_64+0x3e736)

#1 0x10a43446c in llvm::Twine::str() const

#2 0x10a42ef9e in llvm::Triple::Triple(llvm::Twine const&)

#3 0x109f320cc in llvm::EngineBuilder::create(llvm::TargetMachine*)

#4 0x109a4b614 in llvm::EngineBuilder::create() ExecutionEngine.h:659

I suppose there is one bit missing in the native target initialization, but can’t find any relavant pointer online.
Does anyone have a clue?

Cheers,

Matthieu