【发布时间】:2022-11-14 16:20:21
【问题描述】:
我正在解决一个问题,以从 LLVM IR 中的 CallInst 中提取间接调用的函数名称。有没有办法获得间接调用的函数名?基本上,我需要找到所有被调用的函数,包括通过函数指针调用的函数。
C代码:
#include<stdio.h>
void (*fun_ptr)(int);
void fun(int a)
{
printf("Value of a: %d\n", a);
}
int func_3(int a, int b)
{
(*fun_ptr)(a+b);
}
int main ()
{
fun_ptr = fun;
func_3(4, 5);
}
LLVM 红外:
30 %retval = alloca i32, align 4
31 %a.addr = alloca i32, align 4
32 %b.addr = alloca i32, align 4
33 store i32 %a, i32* %a.addr, align 4
34 store i32 %b, i32* %b.addr, align 4
35 %3 = load void (i32)*, void (i32)** @fun_ptr, align 8 ;Can we retrieve the value of @fun_ptr
36 %4 = load i32, i32* %a.addr, align 4
37 %5 = load i32, i32* %b.addr, align 4
38 %add = add nsw i32 %4, %5
39 call void %3(i32 %add) ;Indirect Call
40 %6 = load i32, i32* %retval, align 4
41 ret i32 %6
LLVM 通行证:
for(Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
{
for(BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE; ++BI){
if (CallInst *CI = dyn_cast<CallInst>(BI)) {
CF = CI->getCalledFunction();
if (CF) {
// Direct function call
std::string fName = CF->getName().str(); //Name of callsite function
} else {
//Indirect function call (Via Function pointer)
//Can we get the function name? or Get the address of function pointer and resolve it using symbol table offline?
}
}
}
}
我能够找到直接调用的函数名称并检测到间接函数调用。
【问题讨论】:
标签: llvm