How can I get the list of function calls that are performed in each function of a program, from the intermediate representation of LLVM?

时间秒杀一切 提交于 2019-12-22 18:29:32

问题


I am trying to build a simple version of a code analysis tool with LLVM.

I have a few .ll files which contain the intermediate LLVM representation of certain programs.

How can I get the list of function calls that are performed in each function of a program, from the intermediate representation of LLVM?

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

  }

}

回答1:


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")) {

Also keep in mind that there are also invoke instructions InvokeInst which may be obtained in a similar way.

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.



来源:https://stackoverflow.com/questions/43160566/how-can-i-get-the-list-of-function-calls-that-are-performed-in-each-function-of

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!