I am trying to use llvm’s pass to insert the call instruction of the callback function in a program. This is the content of the callback function I defined.
/* definition of the callback function */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
void callmynumber(int(*number)(void)){
printf(“call my number: %d\n”,number());
}
int name(void)
{
return 919;
}
// call instruction:callmynumber(&name);
In the implementation process, everything else is normal, but I don’t know how to pass this function pointer as a parameter to createcall to implement a call instruction.
/* the pass is used to insert call instruction at the start of each basic block, main body */
for(Module::iterator F = M.begin(), E = M.end(); F!=E; ++F)
{
if(F->getName() == "callmynumber")
{
monitor1 = cast<Function>(F);
continue;
}
if(F->getName() == "name")
{
monitor2 = cast<Function>(F);
// p_func = &monitor2;
continue;
}
for(Function::iterator BB = F->begin(),BE = F->end(); BB!=BE; ++BB)
{
for(BasicBlock::iterator BIB = BB->begin(), BIE = BB->end(); BIB!=BIE; ++BIB)
{
if(BIB == BB->begin())
{
// well, Here is my question. The parameter I need is a function pointer, so how do I pass it as a parameter to callinst::create? Or how should I set this call instruction?//
ArrayRef< Value* > arguments(ConstantInt::get(Type::getInt32Ty(M.getContext()), p_func, true));
Instruction *newInst = CallInst::Create(monitor1, monitor2, "callbackinst");
BB->getInstList().insert(BIB, newInst);
instruct_num++;
errs() << "Inserted the call instruction!\n";
}
else
{
continue;
}
}
}
}