Closed
Description
When i try to run linked c++ function, error occured:
JIT session error: Unsupported file format
Failed to materialize symbols: { (0x7fdfb440b810, { _add, __mh_execute_header, _main }) }
Here are my code:
main.cpp
InitLLVM X(argc, argv);
InitializeNativeTarget();
InitializeNativeTargetAsmPrinter();
ThreadSafeContext context(std::make_unique<LLVMContext>());
ExitOnError ExitOnErr;
jitlink::InProcessMemoryManager MemMgr;
auto JTMB = ExitOnErr(JITTargetMachineBuilder::detectHost());
JTMB.setCodeModel(CodeModel::Small);
auto jit =
ExitOnErr(LLJITBuilder()
.setJITTargetMachineBuilder(std::move(JTMB))
.setObjectLinkingLayerCreator([&](ExecutionSession &ES, const Triple &TT) {
return std::make_unique<ObjectLinkingLayer>(ES, MemMgr);
})
.create());
auto ffi = ExitOnErr(errorOrToExpected(MemoryBuffer::getFileAsStream("build/ffi")));
jit->addObjectFile(std::move(ffi));
char func_name[] = "add";
// char file_path[] = "link.ll";
auto add = ExitOnErr(jit->lookup(func_name));
int (*func)(int, int) = (int (*)(int, int))add.getAddress();
int result = func(111, 222);
ffi.cpp
extern "C" int add(int a, int b) {
return a + b;
}
int main() {
return 0;
}
(build/ffi is output object file of ffi.cpp)
When i run jit->lookup("add"), error occoured.