当我尝试调用createTargetMachine()时,为什么LLVM会崩溃?

3

我想按照编译器实现教程来生成目标代码。但是当我调用createTargetMachine()时,出现了段错误。这似乎是与为什么LLVM在尝试生成目标代码时会出现段错误?不同的问题。

这是我的代码,没有头文件,与上面的代码基本相同:

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

我以前的代码也遇到过类似的问题(https://dev59.com/3LDla4cB1Zd3GeqP3SCQ),原因是即使我手动设置了三元组,它也没有被正确地设置。我想可能还有其他需要创建的东西,但我从未找到它们 :/ - Matthieu Brucher
根据此链接(https://groups.google.com/forum/#!topic/llvm-dev/8Tcz49kTNCw),似乎是ABI不兼容的问题。而根据此链接(https://bugs.llvm.org/show_bug.cgi?id=35978),该问题在几天前已经得到解决。当我使用g++替换clang++时,它可以正常工作。 - 徐保钰
1个回答

2
根据这个链接,似乎是ABI不兼容问题。根据链接,这个问题在几天前已经被解决了。当我将clang++更改为g++时,它可以正常工作。 在llvm 8.0中,这个bug已经被修复了。clang++和g++都可以正常工作。原始答案:最初的回答。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接