LLVM JIT编译的程序找不到外部函数 [英] LLVM JIT-compiled program cannot find external functions
问题描述
我的程序JIT编译LLVM IR模块并调用定义的函数 foo
在运行时如果 foo
使用外部定义的函数:
My program which JIT compiles a LLVM IR module and calls a function foo
defined therein fails at runtime if foo
uses an externally-defined function:
LLVM ERROR: Program used external function 'glutInit' which could not be resolved!
我的程序:
// foo1.cpp
#include <GL/glut.h>
extern "C" void foo()
{
glutInit(0,0);
}
// foo2.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <llvm/Support/raw_ostream.h>
#include <llvm/LLVMContext.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Support/IRReader.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/ExecutionEngine/JIT.h>
#include <llvm/ExecutionEngine/RuntimeDyld.h>
int main(int argc, char **argv)
{
using namespace llvm;
InitializeNativeTarget();
LLVMContext context;
SMDiagnostic error;
std::ifstream ir_file("foo1.s");
std::string ir((std::istreambuf_iterator<char>(ir_file)),
(std::istreambuf_iterator<char>()));
Module *m = ParseIR(MemoryBuffer::getMemBuffer(StringRef(ir)), error, context);
if(!m)
{
error.print(argv[0], errs());
}
ExecutionEngine *ee = ExecutionEngine::create(m);
Function *func = ee->FindFunctionNamed("foo");
if(func == 0)
{
std::cerr << "Couldn't find Function foo" << std::endl;
std::exit(-1);
}
typedef void (*fcn_ptr)();
fcn_ptr foo = reinterpret_cast<fcn_ptr>(ee->getPointerToFunction(func));
foo();
delete ee;
return 0;
}
这是我如何构建我的程序:
Here's how I build my program:
$ clang -S -emit-llvm foo1.cpp
$ g++ -rdynamic foo2.cpp `llvm-config --cxxflags` `llvm-config --libs` `llvm-config --ldflags` -lglut
输出:
$ ./a.out
LLVM ERROR: Program used external function 'glutInit' which could not be resolved!
无论何时我尝试使用外部定义的函数, C ++标准库(例如 printf
, malloc
,& free
没有问题)。我做错了什么?
It fails with a similar error any time I try to use an externally-defined function which is not in the C++ standard library (e.g., printf
, malloc
, & free
are no problem). What am I doing wrong?
推荐答案
确保 glutInit
到 a.out
。如果你的主机代码(执行JIT的东西)没有调用它,它可能已经由链接器混合。如果是这样,你必须添加一个虚拟引用或使用链接器脚本/标志。
Make sure that glutInit
was linked into a.out
. If your host code (the thing executing the JIT) didn't call it, it could have been nixed by the linker. If that's the case, you have to add a dummy reference to it or use linker scripts / flags.
这篇关于LLVM JIT编译的程序找不到外部函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!