为什么在我尝试调用 createTargetMachine() 时出现 LLVM 段错误?

Why is LLVM segfaulting when I try to call createTargetMachine()?

我想在编译器实现教程之后发出 object 代码。但是当我调用 createTargetMachine() 时它有段错误。

似乎是一个不同的问题

这是我没有 headers 的代码,它与上面的代码一样安静:

using namespace llvm;
using namespace llvm::sys;
static LLVMContext TheContext;
static IRBuilder<> Builder(TheContext);
static std::unique_ptr<Module> TheModule;
static std::map<std::string, AllocaInst *> NamedValues;
int main() {

TheModule = llvm::make_unique<llvm::Module>("module", TheContext);
    Function *f = Function::Create(
        FunctionType::get(llvm::Type::getVoidTy(TheContext), std::vector<llvm::Type*>(), false),
        Function::ExternalLinkage,
        "test",
        TheModule.get()
        );
BasicBlock *BB = BasicBlock::Create(TheContext, "entry", f);
Builder.SetInsertPoint(BB);
Builder.CreateRetVoid();

if( verifyFunction(*f, &errs()) )
    return 1;

InitializeAllTargetInfos();
InitializeAllTargets();
InitializeAllTargetMCs();
InitializeAllAsmParsers();
InitializeAllAsmPrinters();

auto TargetTriple = sys::getDefaultTargetTriple();
TheModule->setTargetTriple(TargetTriple);

std::string Error;
auto Target = TargetRegistry::lookupTarget(TargetTriple, Error);

if (!Target)
{
    errs() << Error;
    return 1;
}

auto CPU = "generic";
auto Features = "";

TargetOptions opt;
auto RM = Optional<Reloc::Model>();

/***********Crash Here**************/
auto TheTargetMachine =
  Target->createTargetMachine(TargetTriple, CPU, Features, opt, RM); //Crash
/***********************************/

TheModule->setDataLayout(TheTargetMachine->createDataLayout());

llvm::verifyModule(*TheModule, &errs());

auto Filename = "output.o";
std::error_code EC;
raw_fd_ostream dest(Filename, EC, sys::fs::F_None);

if (EC)
{
    errs() << "Could not open file: " << EC.message();
    return 1;
}

legacy::PassManager pass;
auto FileType = TargetMachine::CGFT_ObjectFile;

if (TheTargetMachine->addPassesToEmitFile(pass, dest, nullptr, FileType))
{
    errs() << "TheTargetMachine can't emit a file of this type";
    return 1;
}

pass.run(*TheModule);
dest.flush();

outs() << "Wrote " << Filename << "\n";

return 0;
}

这里是编译命令,clang++和LLVM版本都是7.0.1

clang++ -g  main.cpp `llvm-config --cxxflags --ldflags --system-libs --libs all` -Wall -fexceptions -o toy`

这是 lldb 信息:

Process 26460 stopped
* thread #1, name = 'toy', stop reason = step over
frame #0: 0x000055555555869b toy`main [inlined] llvm::Target::createTargetMachine(this=0x00007ffff7f9bcc0, TT=(Data = "x86_64-pc-linux-gnu", Length = 19), CPU=(Data = <no value available>, Length = 7), Features=(Data = <no value available>, Length = 0), Options=<unavailable>, RM=Optional<llvm::Reloc::Model> @ scalar, CM=Optional<llvm::CodeModel::Model> @ scalar, OL=Default, JIT=false) const at TargetRegistry.h:397
 394                                         bool JIT = false) const {
 395        if (!TargetMachineCtorFn)
 396          return nullptr;
-> 397      return TargetMachineCtorFn(*this, Triple(TT), CPU, Features, Options, RM,
 398                                   CM, OL, JIT);
 399      }
 400    
 (lldb) s
 Process 26460 stopped
* thread #1, name = 'toy', stop reason = signal SIGSEGV: invalid address (fault address: 0x4)
frame #0: 0x00007ffff691806a libLLVM-7.so`___lldb_unnamed_symbol27390$$libLLVM-7.so + 74
libLLVM-7.so`___lldb_unnamed_symbol27390$$libLLVM-7.so:
->  0x7ffff691806a <+74>: movzbl 0x4(%rcx), %eax
0x7ffff691806e <+78>: movb   %al, 0x1c(%rsp)
0x7ffff6918072 <+82>: testb  %al, %al
0x7ffff6918074 <+84>: je     0x7ffff691807c            ; <+92>
(lldb) bt
* thread #1, name = 'toy', stop reason = signal SIGSEGV: invalid address (fault address: 0x4)
* frame #0: 0x00007ffff691806a libLLVM-7.so`___lldb_unnamed_symbol27390$$libLLVM-7.so + 74
frame #1: 0x00005555555586e5 toy`main [inlined] llvm::Target::createTargetMachine(this=0x00007ffff7f9bcc0, TT=(Data = "x86_64-pc-linux-gnu", Length = 19), CPU=<unavailable>, Features=<unavailable>, Options=0x00000000ffff0000, RM=Optional<llvm::Reloc::Model> @ scalar, CM=Optional<llvm::CodeModel::Model> @ scalar, OL=Default, JIT=false) const at TargetRegistry.h:397
frame #2: 0x0000555555558673 toy`main at main.cpp:73
frame #3: 0x00007ffff3afe223 libc.so.6`__libc_start_main + 243
frame #4: 0x00005555555581ae toy`_start + 46

根据此 link. And fixed just few days ago according to link,似乎 ABI 不兼容。当我将 clang++ 更改为 g++ 时,它工作正常。 在 llvm 8.0 中,此错误已得到修复。 clang++ 和 g++ 都可以正常工作。