<div dir="ltr">Hi List,<div><br></div><div>I'm trying to upgrade our llvm-c based compiler from JIT to MCJIT.</div><div>While trying to do so I encountered several problems. Looks like C</div><div>API does not have proper functions to intialize LLVM with MCJIT.</div>
<div>I ended up wrapping the following functions in my own init routine.</div><div><br></div><div><div><span class="" style="white-space:pre">      </span>LLVMInitializeX86TargetInfo();<br></div><div><span class="" style="white-space:pre">   </span>LLVMInitializeX86Target();</div>
<div><span class="" style="white-space:pre">    </span>LLVMInitializeX86TargetMC();</div><div><span class="" style="white-space:pre">       </span>LLVMInitializeX86AsmPrinter();</div><div><span class="" style="white-space:pre">     </span>LLVMInitializeX86AsmParser();</div>
<div><br></div><div>With this set of functions it attempts to JIT something at least.</div><div>I run into a segfault, valgrind reports the following:</div><div><br></div><div><div>==27130== Process terminating with default action of signal 11 (SIGSEGV)</div>
<div>==27130==  Bad permissions for mapped region at address 0xEAF02F7</div><div>==27130==    at 0xEAF031F: llvm::RuntimeDyldELF::resolveX86_64Relocation(llvm::SectionEntry const&, unsigned long, unsigned long, unsigned int, long, unsigned long) (RuntimeDyldELF.cpp:213)</div>
<div>==27130==    by 0xEAF260F: llvm::RuntimeDyldELF::resolveRelocation(llvm::SectionEntry const&, unsigned long, unsigned long, unsigned int, long, unsigned long) (RuntimeDyldELF.cpp:798)</div><div>==27130==    by 0xEAF258F: llvm::RuntimeDyldELF::resolveRelocation(llvm::RelocationEntry const&, unsigned long) (RuntimeDyldELF.cpp:787)</div>
<div>==27130==    by 0xEAE7C67: llvm::RuntimeDyldImpl::resolveRelocationList(llvm::SmallVector<llvm::RelocationEntry, 64u> const&, unsigned long) (RuntimeDyld.cpp:462)</div><div>==27130==    by 0xEAE543C: llvm::RuntimeDyldImpl::resolveRelocations() (RuntimeDyld.cpp:51)</div>
<div>==27130==    by 0xEAE8236: llvm::RuntimeDyld::resolveRelocations() (RuntimeDyld.cpp:561)</div><div>==27130==    by 0xA25EA99: llvm::MCJIT::finalizeObject() (MCJIT.cpp:173)</div><div>==27130==    by 0xAB57918: LLVMGetPointerToGlobal (ExecutionEngineBindings.cpp:332)</div>
<div>==27130==    by 0x4E4813D: compile_query (ql.c:2286)</div><div>==27130==    by 0x4E4836A: tusto_ql_compile (ql.c:2322)</div><div>==27130==    by 0x402D19: process_query (ts_util.c:167)</div><div>==27130==    by 0x403205: run_shell (ts_util.c:296)</div>
</div><div><br></div><div>I have source tree checked out at f46e5eadc307beaef6e8dd0602bb4c63ca41fd50.</div><div>To create MCJIT i use provided llvm-c apis:</div><div><br></div><div><div><span class="" style="white-space:pre">       </span>LLVMInitializeMCJITCompilerOptions(&options, sizeof(options));</div>
<div><span class="" style="white-space:pre">    </span>options.OptLevel = MODULE_OPT_LEVEL; /* 3 of 0 */</div><div><span class="" style="white-space:pre">  </span>options.CodeModel = LLVMCodeModelJITDefault;</div><div><br></div>
<div><span class="" style="white-space:pre">    </span>if (LLVMCreateMCJITCompilerForModule(&qry->llvm.engine, qry->llvm.mod,</div><div><span class="" style="white-space:pre">                           </span>&options, sizeof(options), &error) != 0) {</div>
<div><span class="" style="white-space:pre">            </span>fprintf(stderr, "%s\n", error);</div><div><span class="" style="white-space:pre">          </span>LLVMDisposeMessage(error);</div><div><span class="" style="white-space:pre">         </span>abort();</div>
<div><span class="" style="white-space:pre">    </span>}</div><div><span class="" style="white-space:pre">  </span>LLVMRunStaticConstructors(qry->llvm.engine);</div></div><div><br></div><div>Then I do the IR building and get the function pointer i need:</div>
<div><br></div><div>iter->run = LLVMGetPointerToGlobal(qry->llvm.engine, func_ref);<br></div><div><br></div><div><br></div><div>Am i doing something wrong?</div><div><br></div><div>Thanks</div>-- <br><div>Regards,</div>
<div>Konstantin<br></div>
</div></div>