I'm trying to load an LLVM module defined in a .bc
file at runtime but have run into a snag.
The bitcode of interest has been generated from hello.cpp
:
// hello.cpp
// build with:
// clang-3.4 -c -emit-llvm hello.cpp -o hello.bc
#include <iostream>
void hello()
{
std::cout << "Hello, world!" << std::endl;
}
When the program below attempts to load it at runtime, it crashes inside llvm::BitstreamCursor::Read()
:
// main.cpp
// build with:
// g++ main.cpp `llvm-config-3.4 --cppflags --ldflags --libs` -ldl -lpthread -lcurses
#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;
}
// load bitcode
std::string ir((std::istreambuf_iterator<char>(stream)), (std::istreambuf_iterator<char>()));
// parse it
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;
} // end if
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'm building against LLVM v3.4 using the command lines mentioned in the comments.
Any idea what I'm doing wrong?