# callgraph_pass **Repository Path**: vaughnHuang/callgraph_pass ## Basic Information - **Project Name**: callgraph_pass - **Description**: Generate call graph (GraphViz DOT file) from LLVM IR file (.ll or .bc). Forked from https://github.com/bernardnongpoh/CallGraph - **Primary Language**: C++ - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-05-19 - **Last Updated**: 2023-05-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: graph, Llvm ## README # Static Call Graph Construction using LLVM ## A. **Summary** A call graph is a directed graph that depicts the calling relationships between functions. Each node denotes a function, and each edge (f,g) represents that function f calls function g. There are two common types of call graphs, static and dynamic call graphs. Static call graphs are generated statically, i.e., without running the program. However, a dynamic call graph is produced by running the program with a given input. This LLVM pass generates a **Static Call Graph** for C and C++ programs. The Call graph generated has the following properties - Flow-insensitive - Field Sensitive (No guarantee as for now) - Context-insensitive --- ## B. **Requirements** - LLVM / Clang 17 and above - CMake 3.0 and above Tested under Ubuntu 22.04 LTS. --- ## C. **Installing LLVM** Refer to [Ubuntu环境下LLVM 15.0 完全编译 附windows编译LLVM master](https://www.cnblogs.com/vaughnhuang/p/15817603.html) Environment variables and PATH: ```bash # vi ~/.bashrc export LLVM_CONFIG=/usr/local/bin/llvm-config export LLVM_DIR=/usr/local/bin export PATH="$PATH:$LLVM_DIR/bin" ``` ```bash clang -v Target: x86_64-unknown-linux-gnu Thread model: posix InstalledDir: /usr/local/bin Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/11 Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/11 Candidate multilib: .;@m64 Selected multilib: .;@m64 ``` If the output is similar to the above, you’re successfully installed LLVM and Clang. --- ## D. Building the CallGraph LLVM Pass ```bash git clone https://github.com/bernardnongpoh/CallGraph.git cd Callgraph CallGraph$ mkdir build Callgraph$ cd build $CallGraph/build$ cmake .. Consolidate compiler generated dependencies of target CallGraph [100%] Built target CallGraph ``` --- ## E. Running and Testing the Call Graph LLVM Pass Go back to the root directory of `CallGraph`: ```bash opt -O0 -load-pass-plugin lib/libCallGraph.so -S test/temp.ll -f -o /dev/null ``` --- ## F. LLVM IR 语法基本结构 1. 模块(Module):`LLVM IR`的最顶层结构是模块,表示整个程序或库。一个模块由一组全局变量和函数组成。 2. 全局变量(Global Variables):`LLVM IR`允许声明全局变量,它们是在整个程序中可见的。全局变量由其类型、名称和可选的初始值组成。全局变量的声明以`@`符号开头,后面跟着变量名称和类型信息。例如,`@myGlobal = global i32 0` 表示一个名为`myGlobal`的 32位整数 全局变量,初始值为 0。 3. 函数(Functions):`LLVM IR`中的函数由函数类型、名称、参数列表和函数体组成。函数类型包括返回类型和参数类型。函数的声明以`define`关键字开头,后面是返回类型、函数名称、参数列表和函数体。函数体由`基本块(Basic Block)`组成,每个基本块由一组指令组成。函数体的最后一条指令通常是返回指令。 4. 基本块(Basic Blocks):`基本块`是 `LLVM IR` 中的基本执行单元。它由一系列按顺序执行的指令组成,没有分支或跳转指令。基本块以标签(Label)开头,后面是一组指令。基本块可以包含控制流指令,如条件分支和无条件跳转,以便实现程序的控制流程。 5. 指令(Instructions):`LLVM IR` 包括各种指令来表示程序的操作。指令由操作码和操作数组成,用于执行特定的操作。`LLVM IR` 支持常见的指令类型,如赋值指令、算术运算指令、逻辑运算指令、条件分支指令、内存访问指令等。指令可以使用变量、常量和表达式作为操作数。 我们可以使用 `LLVM` 提供的工具(如 `llvm-as` 和 `llvm-dis`)将 `LLVM IR` 代码转换为二进制形式以及反向转换,以便更好地理解其结构和语法。 --- ## G. References ### 1. repo newPM_LLVM_pass [newPM_LLVM_pass](https://github.com/sepy97/newPM_LLVM_pass.git). Run command: ```bash opt -load-pass-plugin build/lib/libLEO_PASSES.so -passes=leo-test-pass -disable-output ./temp.ll -f -o /dev/null ``` ### 2. repo OOS-Pass CN [OOS-Pass](https://github.com/WYK15/OOS-Pass.git) Run command: ```bash opt -load-pass-plugin build/lib/libLEO_PASSES.so -passes=leo-test-pass -disable-output ./temp.ll ``` 该仓库使用的pass注册函数为: ```C++ PB.registerPipelineParsingCallback ``` ### 3. LLVM docs - [opt 使用手册](https://www.llvm.org/docs/CommandGuide/opt.html) - [LLVM Programmer’s Manual](https://www.llvm.org/docs/ProgrammersManual.html) - [LLVM User Guides](https://www.llvm.org/docs/UserGuides.html) ### 4. LLVM forum - [google groups](https://groups.google.com/g/llvm-dev)