徐保钰
徐保钰

Reputation: 792

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

I want to emit object code following tutorial on compiler implementation. But it has segfault when I call createTargetMachine(). It seems like a different problem with Why is LLVM segfaulting when I try to emit object code?

Here is my code without headers which is quiet same as the above one:

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;
}

Here is the compiling command and clang++ and LLVM version are both 7.0.1

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

Here is the lldb info:

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

Upvotes: 3

Views: 1042

Answers (1)

徐保钰
徐保钰

Reputation: 792

It seems like ABI incompatibility according to this link. And fixed just few days ago according to link.It works fine when I change clang++ with g++. In llvm 8.0, this bug has been fixed. Both clang++ and g++ work fine.

Upvotes: 2

Related Questions