Skip to content

LLJIT::addObjectFile cannot work. #57

Closed
@lanistor

Description

@lanistor

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionA question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions