我正在尝试读取并调用从 LLVM 2.8 中的 LLVM 位码解析的函数。除了使程序崩溃的实际调用之外,我的一切都在工作。
首先我有这个C代码:
void hello() {}
我已经编译了这个:
llvm-gcc -c -emit-llvm hello.c -o hello.bc
这是应该阅读的代码的精简版本:
using namespace std;
using namespace llvm;
void callFunction(string file, string function) {
InitializeNativeTarget();
LLVMContext context;
string error;
MemoryBuffer* buff = MemoryBuffer::getFile(file);
Module* m = getLazyBitcodeModule(buff, context, &error);
// Check the module parsed here.
// ...
ExecutionEngine* engine = ExecutionEngine::create(m);
// Check the engine started up correctly here.
// ...
Function* func = m->getFunction(function);
// Check the function was found here.
// ..
vector<GenericValue> args(0);
// This is what crashes.
engine->runFunction(func, args);
}
我已经包含了大量 LLVM 标头,包括 ExecutionEngine/JIT.h,并且在每个步骤中都会检查代码以确保值不为 NULL。它解析位码,我检查了它找到的函数以确认它是否符合预期。
我也尝试过自己构建一个模块和函数,它按预期工作,所以问题肯定是由于函数是由位码产生的。