如何从LLVM的中间表示中获取在程序的每个函数中执行的函数调用列表
How can I get the list of function calls that are performed in each function of a program, from the intermediate representation of LLVM?
我正在尝试使用LLVM构建代码分析工具的简单版本。
我有几个.ll文件,其中包含某些程序的中间LLVM表示。
如何从 LLVM 的中间表示中获取在程序的每个函数中执行的函数调用列表?
我拥有的输入参数是 LLVM:模块类的实例,它表示程序。然后,我使用函数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
}
}
这是我们工作代码的一个片段:
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
可以通过类似的方式获得。
谷歌CallInst vs InvokeInst
并了解带有或不带有调用函数的函数。如果函数没有被调用的函数,则这是间接调用。当源代码而不是直接调用函数而是调用函数指针时,间接调用会出现在 LLVM IR 中。在C++当某个类通过抽象接口(多态性(运行时,通常会发生这种情况。因此请记住,即使您有调用指令,也并非 100% 总是可以跟踪被调用的函数。
相关文章:
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 在C应用程序中运行C++(带有STL)函数
- 如何在c++程序中找到函数的地址
- 如何在信号处理程序和普通函数中对全局变量进行互斥读写操作
- WIN32:C++,为什么在WM_CLOSE上调用Messagebox函数程序正在冻结
- 具有外部"c"和程序集的未定义函数
- 是否可以依赖函数范围的静态变量来执行程序关闭期间调用的方法?
- 程序不会执行函数 c++
- C++调用具有 *this 属性的单个帮助程序函数
- 用于检查值是否为其任何参数的帮助程序函数
- 程序中的布尔函数返回输入的范围无论如何都是无效的
- 在 C++20 中是否不再允许在 std 中对程序定义类型的函数模板进行专用化?
- 程序不向函数返回值
- C++ 函数程序中的平均输出始终为 0.0
- 使用getline和字符串函数C++程序中的分段错误
- 如何理解哪个递归函数程序需要
- 3 函数加主函数 C 程序计算和显示阶乘
- 在结构中嵌入函数程序集代码
- c++的构造函数和析构函数程序集
- c++基本函数程序编译时不会出现满屏幕的错误