How can I get the function name of an indirect call from CallInst in LLVM

Function *fun = call->getCalledFunction();

getCalledFunction();returns null if it is an indirect call. How can I get function name or pointer name?

I found that all the questions in Stack Overflow related to this issue concerned the name of the direct call function or the type of pointer.

I just want to keep track of cases like this:

void foo(){}
void goo(){}
void main(){
  int x = 1;
  void (*p)();
  if(x)
    p = &foo;
  else
    p = &goo;
  p(); // print the called function name
}
+4
source share
3 answers

My suggestion would be to use visitor clang AST and not use LLVM IR.

An example AST visitor can be found here:

http://clang.llvm.org/docs/RAVFrontendAction.html

AST ( ++ - C, , func() func(...)):

$ clang++ -Xclang -ast-dump -fno-color-diagnostics -c funcptr.cpp

TranslationUnitDecl 0x50973b0 <<invalid sloc>> <invalid sloc>
|-TypedefDecl 0x50978f0 <<invalid sloc>> <invalid sloc> implicit __int128_t '__int128'
|-TypedefDecl 0x5097950 <<invalid sloc>> <invalid sloc> implicit __uint128_t 'unsigned __int128'
|-TypedefDecl 0x5097d50 <<invalid sloc>> <invalid sloc> implicit __builtin_va_list '__va_list_tag [1]'
|-FunctionDecl 0x5097df0 <funcptr.cpp:1:1, col:12> col:6 used foo 'void (void)'
| `-CompoundStmt 0x5097e90 <col:11, col:12>
|-FunctionDecl 0x5097ed0 <line:2:1, col:12> col:6 used goo 'void (void)'
| `-CompoundStmt 0x5097f70 <col:11, col:12>
`-FunctionDecl 0x5097fe0 <line:3:1, line:14:1> line:3:5 main 'int (void)'
  `-CompoundStmt 0x50d98b0 <col:11, line:14:1>
    |-DeclStmt 0x50d9548 <line:5:1, col:9>
    | `-VarDecl 0x50d94d0 <col:1, col:8> col:5 used x 'int' cinit
    |   `-IntegerLiteral 0x50d9528 <col:8> 'int' 1
    |-DeclStmt 0x50d9678 <line:6:1, col:12>
    | `-VarDecl 0x50d9620 <col:1, col:11> col:8 used p 'void (*)(void)'
    |-IfStmt 0x50d9818 <line:7:1, line:10:7>
    | |-<<<NULL>>>
    | |-ImplicitCastExpr 0x50d96d0 <line:7:4> '_Bool' <IntegralToBoolean>
    | | `-ImplicitCastExpr 0x50d96b8 <col:4> 'int' <LValueToRValue>
    | |   `-DeclRefExpr 0x50d9690 <col:4> 'int' lvalue Var 0x50d94d0 'x' 'int'
    | |-BinaryOperator 0x50d9758 <line:8:2, col:7> 'void (*)(void)' lvalue '='
    | | |-DeclRefExpr 0x50d96e8 <col:2> 'void (*)(void)' lvalue Var 0x50d9620 'p' 'void (*)(void)'
    | | `-UnaryOperator 0x50d9738 <col:6, col:7> 'void (*)(void)' prefix '&'
    | |   `-DeclRefExpr 0x50d9710 <col:7> 'void (void)' lvalue Function 0x5097df0 'foo' 'void (void)'
    | `-BinaryOperator 0x50d97f0 <line:10:2, col:7> 'void (*)(void)' lvalue '='
    |   |-DeclRefExpr 0x50d9780 <col:2> 'void (*)(void)' lvalue Var 0x50d9620 'p' 'void (*)(void)'
    |   `-UnaryOperator 0x50d97d0 <col:6, col:7> 'void (*)(void)' prefix '&'
    |     `-DeclRefExpr 0x50d97a8 <col:7> 'void (void)' lvalue Function 0x5097ed0 'goo' 'void (void)'
    `-CallExpr 0x50d9888 <line:12:1, col:3> 'void'
      `-ImplicitCastExpr 0x50d9870 <col:1> 'void (*)(void)' <LValueToRValue>
        `-DeclRefExpr 0x50d9848 <col:1> 'void (*)(void)' lvalue Var 0x50d9620 'p' 'void (*)(void)'

, p CallExprImplicitCastExprDeclRefExpr. , p. , , , p - , , x , - " foo goo x". , - p, . ( , getPointeeType, isa<FunctionType> - ).

llvm, , - , if (x) p = &foo; else p = &goo; p = &foo;, p , [ ]. LLVM IR , . , , , , , .

, LLVM.

http://llvm.org/docs/WritingAnLLVMPass.html

, , CallGraphSCCPass , Callee, callrraph, . , , , - .

, , , -, , (, , , - , , , , !)

+1

. llvm:

Function* fp = CI->getCalledFunction();
if (fp==NULL) {
    Value* v=CI->getCalledValue();
    Value* sv = v->stripPointerCasts();
    StringRef fname = sv->getName();
    errs()<<fname<<"\n";
 }
0

: . . , LLVM .

, , : LLVM , .

x , ( foo goo), , . LLVM .

0

All Articles