[cfe-dev] From libtooling to LLVM IR?

Matthieu Brucher via cfe-dev cfe-dev at lists.llvm.org
Sun Jul 15 02:28:58 PDT 2018


Hi all,

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

    llvm::LLVMContext context;



    std::unique_ptr<clang::CodeGenAction> action = std::make_unique<clang::
EmitLLVMOnlyAction>(&context);

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



    std::unique_ptr<llvm::Module> module = action->takeModule();

    llvm::InitializeNativeTarget();

    llvm::InitializeNativeTargetAsmPrinter();

    llvm::InitializeNativeTargetAsmParser();



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

    builder.setMCJITMemoryManager(std::make_unique<llvm::
SectionMemoryManager>());

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

    EE.reset(builder.create());



    return reinterpret_cast<Function>(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<char>, std::__1::allocator<char>
>::basic_string(std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> > 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

Le sam. 14 juil. 2018 à 09:35, Matthieu Brucher <matthieu.brucher at gmail.com>
a écrit :

> 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
> --
> Quantitative analyst, Ph.D.
> Blog: http://blog.audio-tk.com/
> LinkedIn: http://www.linkedin.com/in/matthieubrucher
>


-- 
Quantitative analyst, Ph.D.
Blog: http://blog.audio-tk.com/
LinkedIn: http://www.linkedin.com/in/matthieubrucher
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180715/242a19fb/attachment.html>


More information about the cfe-dev mailing list