如何在LLVM中间接调用时从CallInst获取FunctionType

Met*_*est 7 c++ llvm

如果函数调用是直接的,则可以通过以下代码获取Function类型.

Function  * fun  = callInst->getCalledFunction();
Function  * funType = fun->getFunctionType();
Run Code Online (Sandbox Code Playgroud)

但是,如果调用是间接调用,即通过函数指针,则getCalledFunction 返回NULL.所以我的问题是当通过函数指针调用函数时如何获取Function类型.

Oak*_*Oak 9

要从间接调用中获取类型,请使用getCalledValue而不是getCalledFunction,如下所示:

Type* t = callInst->getCalledValue()->getType();
Run Code Online (Sandbox Code Playgroud)

那会得到传递给调用指令的指针类型; 要获取实际的函数类型,请继续:

FunctionType* ft = cast<FunctionType>(cast<PointerType>(t)->getElementType());
Run Code Online (Sandbox Code Playgroud)