[LLVMdev] Indirect function call

夏一民 ymxia at nudt.edu.cn
Mon May 22 00:33:41 PDT 2006


The follwing is a snippet of code to find some indirect calls in a module, which I
learned from TopDownClosure.cpp:

void FPS::repairCallGraph(Module &M) {
	CompleteBUDataStructures &DS = getAnalysis<CompleteBUDataStructures>();
	for (Module::iterator f = M.begin(); f != M.end(); ++f ) {
		if( f->isExternal() ) continue;
		for (Function::iterator I = f->begin(); I != f->end(); ++I)  {
			for(BasicBlock::iterator J = I->begin(); J != I->end(); ++J) {
				if(CallInst *cs = dyn_cast<CallInst>(J)) {
					Function *callee = cs->getCalledFunction();
					if(callee) continue;//not a function pointer.
					for(CompleteBUDataStructures::callee_iterator K = DS.callee_begin(J); K !=
DS.callee_end(J); ++K) {
						if(K->first != J) continue;

						CallGraphNode *cgn = getAnalysis<CallGraph>()[f]; // Find a indirect call!
						CallGraphNode *calleecgn = getAnalysis<CallGraph>()[K->second];
						cgn->addCalledFunction(calleecgn);
						std::cerr<<"\n indirect call in "<<f->getName()<<*J<<", callee:
"<<K->second->getName();
					}
				}
			}
		}
	}
}

But my code does not always works: if the arguments are not pointer,
CompleteBUDataStructures not records it. So, if you want to find all indirect
calls, you maybe have to repair CompleteBUDataStructures. :)

If you do not use BUDataStructures, you can do it yourself: find all load/store
instructions with its destination is function type.






More information about the llvm-dev mailing list