Problem linking and JITing code through C++-API

I have a frontend that generates some LLVM bitcode that needs to be linked with other bitcode (its runtime library), which I generate from C++ source using Clang.

If I write the output of my program to disk, link it with llvm-link, and then run it with lli, everything works perfectly. But if I try to perform the linking and running steps in my main program, I get this error during llvm::ExecutionEngine::getPointerToFunction:

Stack dump:

  1. Running pass ‘X86 Machine Code Emitter’ on function ‘@.step.myappend’

  2. Running pass ‘X86 Machine Code Emitter’ on function ‘@.step.myappend’

Segmentation fault (core dumped)

There are no other messages. Any idea what I’m doing wrong? I’ll copy the source of my main C++ file and the bitcode for .step.myappend below. I can send the full bitcode file, too, if someone asks for it, but it is around 800 lines.

Have you tried running this under gdb and looking at where the segmentation fault occurs?

Yes. It appears that a bad reference to DataLayout was passed to MachineJumpTableInfo::getEntrySize. I'm using LLVM as a pre-compiled Ubuntu package for this work, so I can't do much more in GDB without building from source.

Program received signal SIGSEGV, Segmentation fault.
0x00000000007565f0 in llvm::MachineJumpTableInfo::getEntrySize(llvm::DataLayout const&) const ()
(gdb) where
#0 0x00000000007565f0 in llvm::MachineJumpTableInfo::getEntrySize(llvm::DataLayout const&) const ()
#1 0x0000000000704313 in (anonymous namespace)::JITEmitter::getJumpTableEntryAddress(unsigned int) const ()
#2 0x000000000070bce1 in (anonymous namespace)::JITEmitter::finishFunction(llvm::MachineFunction&) ()
#3 0x0000000000474430 in (anonymous namespace)::Emitter<llvm::JITCodeEmitter>::runOnMachineFunction(llvm::MachineFunction&) ()
#4 0x0000000000b214dc in llvm::FPPassManager::runOnFunction(llvm::Function&) ()
#5 0x0000000000b21610 in llvm::FunctionPassManagerImpl::run(llvm::Function&) ()
#6 0x0000000000b216f4 in llvm::FunctionPassManager::run(llvm::Function&) ()
#7 0x00000000006fe26e in llvm::JIT::jitTheFunction(llvm::Function*, llvm::MutexGuard const&) ()
#8 0x00000000006fe8cf in llvm::JIT::runJITOnFunctionUnlocked(llvm::Function*, llvm::MutexGuard const&) ()
#9 0x00000000006fead2 in llvm::JIT::getPointerToFunction(llvm::Function*) ()
#10 0x00000000006fd63f in llvm::JIT::getPointerToBasicBlock(llvm::BasicBlock*) ()
#11 0x00000000009e14ee in llvm::ExecutionEngine::getConstantValue(llvm::Constant const*) ()
#12 0x00000000009e265f in llvm::ExecutionEngine::InitializeMemory(llvm::Constant const*, void*) ()
#13 0x00000000009e27e3 in llvm::ExecutionEngine::InitializeMemory(llvm::Constant const*, void*) ()
#14 0x00000000009e3628 in llvm::ExecutionEngine::EmitGlobalVariable(llvm::GlobalVariable const*) ()
#15 0x00000000006fd559 in llvm::JIT::getOrEmitGlobalVariable(llvm::GlobalVariable const*) ()
#16 0x000000000070b8cd in (anonymous namespace)::JITEmitter::getPointerToGlobal(llvm::GlobalValue*, void*, bool) [clone .isra.439] ()
#17 0x000000000070c505 in (anonymous namespace)::JITEmitter::finishFunction(llvm::MachineFunction&) ()
#18 0x0000000000474430 in (anonymous namespace)::Emitter<llvm::JITCodeEmitter>::runOnMachineFunction(llvm::MachineFunction&) ()
#19 0x0000000000b214dc in llvm::FPPassManager::runOnFunction(llvm::Function&) ()
#20 0x0000000000b21610 in llvm::FunctionPassManagerImpl::run(llvm::Function&) ()
#21 0x0000000000b216f4 in llvm::FunctionPassManager::run(llvm::Function&) ()
#22 0x00000000006fe26e in llvm::JIT::jitTheFunction(llvm::Function*, llvm::MutexGuard const&) ()
#23 0x00000000006fe8fa in llvm::JIT::runJITOnFunctionUnlocked(llvm::Function*, llvm::MutexGuard const&) ()
#24 0x00000000006fead2 in llvm::JIT::getPointerToFunction(llvm::Function*) ()
#25 0x00000000004285e7 in main () at sprite.cpp:172
(gdb) x/10i $pc
=> 0x7565f0 <_ZNK4llvm20MachineJumpTableInfo12getEntrySizeERKNS_10DataLayoutE>: cmpl $0x5,(%rdi)
   0x7565f3 <_ZNK4llvm20MachineJumpTableInfo12getEntrySizeERKNS_10DataLayoutE+3>: mov (%rdi),%edx
   0x7565f5 <_ZNK4llvm20MachineJumpTableInfo12getEntrySizeERKNS_10DataLayoutE+5>: lea 0x4ccc5c(%rip),%rax # 0xc23258
   0x7565fc <_ZNK4llvm20MachineJumpTableInfo12getEntrySizeERKNS_10DataLayoutE+12>: movslq (%rax,%rdx,4),%rdx
   0x756600 <_ZNK4llvm20MachineJumpTableInfo12getEntrySizeERKNS_10DataLayoutE+16>: add %rdx,%rax
   0x756603 <_ZNK4llvm20MachineJumpTableInfo12getEntrySizeERKNS_10DataLayoutE+19>: jmpq *%rax
   0x756605 <_ZNK4llvm20MachineJumpTableInfo12getEntrySizeERKNS_10DataLayoutE+21>: nopl (%rax)
   0x756608 <_ZNK4llvm20MachineJumpTableInfo12getEntrySizeERKNS_10DataLayoutE+24>: mov $0x4,%eax
   0x75660d <_ZNK4llvm20MachineJumpTableInfo12getEntrySizeERKNS_10DataLayoutE+29>: retq
   0x75660e <_ZNK4llvm20MachineJumpTableInfo12getEntrySizeERKNS_10DataLayoutE+30>: xchg %ax,%ax
(gdb) x $rdi
   0x3eb000003ec: Cannot access memory at address 0x3eb000003ec

It's really easy to build LLVM from source on any linux environment.

Hi Andy,

It looks like you’re using LLVM’s old JIT, rather than MCJIT? The old JIT has been removed from the mainline, and is no longer supported. I’d recommend building your own copy of LLVM from the development branch (as Reed suggested) where MCJIT is used by default - this may fix your issue.

If you want to stick with the precompiled binaries, then you should change:

#include “llvm/ExecutionEngine/JIT.h”

to

#include “llvm/ExecutionEngine/MCJIT.h”

and add:

.setUseMCJIT(true)

to your EngineBuilder invocation.

And finally change:

jit->getPointerToFunction(rtlib->getFunction(“main”));

to

jit->getFunctionAddress(“main”);

Cheers,
Lang.

Thank you for the suggestions, Lang. I was able to make each change except the last one. There is no getFunctionAddress is the version of LLVM I have (3.3).

If I run without that last change, I get (on x86_64):

LLVM ERROR: Target does not support MC emission!

I suppose there’s no reason to debug the original issue any further, since the old JIT has been removed. When I have the time, I’ll see if my project works with a newer version of LLVM.

-Andy

Hi Andy,

Sure thing.

Regarding the error you saw, I think you’ll also need to add a call to InitializeNativeTargetAsmPrinter() after your call to InitializeNativeTarget(). This is required on newer versions of LLVM as well.

Cheers,
Lang.