问题描述
我正在尝试使用LLVM构建代码分析工具的简单版本.
I am trying to build a simple version of a code analysis tool with LLVM.
我有一些.ll文件,其中包含某些程序的中间LLVM表示形式.
I have a few .ll files which contain the intermediate LLVM representation of certain programs.
如何从LLVM的中间表示中获取在程序的每个功能中执行的功能调用列表?
我拥有的输入参数是LLVM:代表程序的模块类的实例.然后,使用getFunctionList()函数获取程序中存在的函数列表.
The input parameter I have is an instance of the LLVM: Module class which represents the program. Then, I get the list of functions present in the program with the function getFunctionList ().
void getFunctionCalls(const Module *M)
{
// Iterate functions in program
for (auto curFref = M->getFunctionList().begin(), endFref = M->getFunctionList().end();
curFref != endFref; ++curFref) {
// For each function
// Get list of function calls
}
}
推荐答案
这是我们工作代码的一部分此处:
This is a fragment from our working code here:
for (auto &module : Ctx.getModules()) {
auto &functionList = module->getModule()->getFunctionList();
for (auto &function : functionList) {
for (auto &bb : function) {
for (auto &instruction : bb) {
if (CallInst *callInst = dyn_cast<CallInst>(&instruction)) {
if (Function *calledFunction = callInst->getCalledFunction()) {
if (calledFunction->getName().startswith("llvm.dbg.declare")) {
还请记住,还有一些调用指令InvokeInst
,它们可以通过类似的方式获得.
Also keep in mind that there are also invoke instructions InvokeInst
which may be obtained in a similar way.
Google CallInst vs InvokeInst
,还可以了解带有或不带有被调用函数的函数.如果一个函数没有被调用的函数,则这是间接调用.当源代码而不是直接调用函数而是调用函数指针时,间接调用会出现在LLVM IR中.在C ++中,当某些类通过抽象接口(多态)进行操作时,通常会发生这种情况.因此请记住,即使有调用指令,也不总是能够100%跟踪被调用的函数.
Google CallInst vs InvokeInst
and also learn about the functions with or without a called function. If a function does not have a called function this is indirect call. Indirect calls appear in LLVM IR when the source code instead of calling a function directly, calls a function pointer. In C++ this often happens when some class operates through an abstract interface (polymorphism). So keep in mind that it is not 100% always possible to trace a called function even though you have a call instruction in place.
这篇关于如何从LLVM的中间表示中获取在程序的每个功能中执行的功能调用列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!