链接c ++标准库时,LLJIT运行错误“细分错误:11”

时间:2019-12-30 11:35:05

标签: c++ llvm jit llvm-clang llvm-ir

我使用的是自定义c ++函数,其中包含c++ standard libraries。 当我使用c++ standard libraries时,发生了错误:make:*** [start] Segmentation fault:11

这是我的代码文件:

main.cpp

#include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h"
#include "llvm/ExecutionEngine/JITSymbol.h"
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
#include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/TargetSelect.h"
#include <fstream>
#include <iostream>

using namespace std;
using namespace llvm;
using namespace llvm::orc;

int main(int argc, char *argv[]) {

  InitLLVM X(argc, argv);
  InitializeNativeTarget();
  InitializeNativeTargetAsmPrinter();
  ThreadSafeContext context(std::make_unique<LLVMContext>());

  ExitOnError ExitOnErr;

  auto JTMB = ExitOnErr(JITTargetMachineBuilder::detectHost());
  JTMB.setCodeModel(CodeModel::Small);

  auto jit =
      ExitOnErr(LLJITBuilder()
                    .setJITTargetMachineBuilder(std::move(JTMB))
                    .create());

  jit->getMainJITDylib().addGenerator(
      ExitOnErr(orc::DynamicLibrarySearchGenerator::GetForCurrentProcess(
          jit->getDataLayout().getGlobalPrefix())));

  char        ffi_file[] = "build/ffi";
  llvm::Error err =
      jit->addObjectFile(ExitOnErr(errorOrToExpected(MemoryBuffer::getFileAsStream(ffi_file))));

  if (err) {
    cout << "error addObjectFile" << endl;
    return 1;
  };

  char func_name[]      = "add";
  auto func_add         = ExitOnErr(jit->lookup(func_name));
  int (*func)(int, int) = (int (*)(int, int))func_add.getAddress();
  int result            = func(111, 222);

  cout << "result: " << result << endl;

  return 0;
}

ffi.cpp ,它将生成并输出build/ffi文件。

#include <vector>

extern "C" int add(int a, int b) {
  std::vector<int> vc;
  vc.push_back(1);
  return a + b;
}

vc.push_back(1);中包含ffi.cpp时,发生错误,删除此行时,它运行正常。

这是我的环境

OS: MacOSX 10.15.2
LLVM: 10
Clang: 11

这是我的cmake文件

cmake_minimum_required(VERSION 3.15.0)

project(jitdemo)

SET (CMAKE_C_COMPILER /usr/bin/clang)
SET (CMAKE_CXX_COMPILER /usr/bin/clang++)

SET ( CMAKE_BUILD_TYPE Debug )

find_package(LLVM REQUIRED CONFIG)

message(STATUS ">>Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS ">>Using LLVMConfig.cmake in: ${LLVM_DIR}")

add_compile_options(-std=c++17)
add_compile_options(-stdlib=libc++)
add_definitions(${LLVM_DEFINITIONS})

include_directories(${LLVM_INCLUDE_DIRS})
include_directories(${PROJECT_SOURCE_DIR})

add_subdirectory(link)

llvm_map_components_to_libnames(llvm_libs support core orcjit irreader nativecodegen)

add_executable(main main.cpp)
target_link_libraries(main link ${llvm_libs})

0 个答案:

没有答案