I am trying to load the LLVM module defined in a file .bcat runtime, but am confused.
The bit code of interest is generated from hello.cpp:
#include <iostream>
void hello()
{
std::cout << "Hello, world!" << std::endl;
}
When the program below tries to load it at run time, it crashes inside llvm::BitstreamCursor::Read():
#include <llvm/IR/Module.h>
#include <llvm/IRReader/IRReader.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/Support/SourceMgr.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/raw_ostream.h>
#include <fstream>
#include <iostream>
llvm::Module *load_module(std::ifstream &stream)
{
if(!stream)
{
std::cerr << "error after open stream" << std::endl;
return 0;
}
std::string ir((std::istreambuf_iterator<char>(stream)), (std::istreambuf_iterator<char>()));
using namespace llvm;
LLVMContext context;
SMDiagnostic error;
Module *module = ParseIR(MemoryBuffer::getMemBuffer(StringRef(ir.c_str())), error, context);
if(!module)
{
std::string what;
llvm::raw_string_ostream os(what);
error.print("error after ParseIR()", os);
std::cerr << what;
}
return module;
}
int main()
{
std::ifstream stream("hello.bc", std::ios_base::binary);
llvm::Module *m = load_module(stream);
if(m)
{
m->dump();
}
return 0;
}
I am creating against LLVM v3.4 using the command lines mentioned in the comments.
Any idea what I'm doing wrong?
source
share