在LLVM 2.8中调用LLVM位码函数
我正在尝试读取并调用从 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 的步骤。它解析位码,我检查了它找到的函数以确认它符合预期。
我还尝试自己构建一个模块和函数,它按预期工作,因此问题肯定是由于该函数是由位码生成的这一事实引起的。
I'm trying to read and call a function parsed from LLVM bitcode in LLVM 2.8. I have everything working apart from the actual call, which crashes the program.
First I have this C code:
void hello() {}
I've compiled this with:
llvm-gcc -c -emit-llvm hello.c -o hello.bc
Here's a trimmed down version of the code that's supposed to read it:
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);
}
I've included plenty of LLVM headers, including ExecutionEngine/JIT.h, and the code checks at each step to make sure values aren't NULL. It parses the bitcode, and I have examined the function it finds to confirm it was as expected.
I've also tried building a module and function myself, which works as expected, so the problem definitely arises from the fact that the function is produced by the bitcode.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经设法让它按预期运行。我很好奇问题是否出在上面的过程中,但显然不是这样的。我运行此程序的系统导致了崩溃,而上面的代码确实可以自行运行。
I've managed to get this running as expected. I was curious if the problem lay in the above process, but this is obviously not the case. The system I was running this as a part of was causing the crash, and the code above does work on its own.