LLVM如何检测和忽略库(内置)函数?

问题描述 投票:1回答:1

我正在尝试编写具有以下目标的简单LLVM通过:

  • 查找所有call指令。
  • 将我编写的外部函数插入被调用函数中。

作为示例,请考虑我有以下示例程序:

#include <stdio.h>
#include <stdlib.h>

int times_two(int val);

int main(int argc, char** argv) {
    int arg_1 = atoi(argv[1]);
    char test_str[] = "Test String";
    int res = times_two(arg_1);
    printf("%d", res);
    return 0;
}

int times_two(int val) {
    // ----> INSERT SOME EXTERNAL FUNCTION HERE <---- 
    return val * 2;     
}

这是我的LLVM工具的一部分:

/* Begin instrumentation 
   --------------------- */
for (auto &F : M)
{       
    for (auto &B : F)
    {
        for (auto &I : B)
        {
             IRBuilder<> builder(&I);
             // Now we have reached a 'call' instruction
             if (auto *CallInstr = dyn_cast<CallInst>(&I))
             {
                // Cast into Function pointer
                Function *called_func = CallInstr->getCalledFunction();
                errs() << called_func->getName() << "\n";  

                // loop into the function, getFirstInstruction, do stuff and break.
                for (auto &bb_in_func : *called_func)
                    {
                        // Set an Insert point at the first
                        // line of the external function
                        BasicBlock::iterator insert_point = bb_in_func.getFirstInsertionPt();
                        builder.SetInsertPoint(&bb_in_func, insert_point);
                        // Make an external call
                        builder.CreateCall(extern1);
                        break;
                    }
               }
          }
    }
}        

但是,当我遍历模块中的所有功能时,此功能列表似乎也包含内置功能。在上述情况下,我得到以下信息:

atoi
llvm.memcpy.p0i8.p0i8.i64
times_two
printf

我如何忽略这些内置函数,仅考虑times_two?

c++ c llvm llvm-clang
1个回答
0
投票

我想我已经知道了。我们要使用getLibFunc()getLibFunc()示例执行了非常相似的操作。

就我而言,我必须如下更新LLVM工具:

This
© www.soinside.com 2019 - 2024. All rights reserved.