如何从 LLVM 的中间表示中获取程序每个函数中执行的函数调用列表?

2024-04-18

我正在尝试使用 LLVM 构建一个简单版本的代码分析工具。

我有一些 .ll 文件,其中包含某些程序的中间 LLVM 表示。

如何从 LLVM 的中间表示中获取程序每个函数中执行的函数调用列表?

我的输入参数是 LLVM: Module 类的一个实例,它代表程序。然后,我使用函数 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

  }

}

这是我们工作代码的一个片段here https://github.com/mull-project/mull/blob/35f83655b04341b6260c5358168c4ac2da7bd86d/lib/Rust/RustTestFinder.cpp#L108:

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可以通过类似的方式获得。

Google CallInst vs InvokeInst并了解有或没有被调用函数的函数。如果函数没有被调用函数,则这是间接调用。当源代码不直接调用函数,而是调用函数指针时,间接调用出现在 LLVM IR 中。在 C++ 中,当某些类通过抽象接口(多态性)进行操作时,通常会发生这种情况。因此请记住,即使您有调用指令,也不是 100% 总是可以跟踪被调用的函数。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 LLVM 的中间表示中获取程序每个函数中执行的函数调用列表? 的相关文章

随机推荐