pass a local variable to a function llvm

I am using llvm 4.0. I need to hook a function in which the argument is one of the local variable in IR e.g. I want to pass %10 value to a function. I know how to hook a function. Following is my code:

hookFunc2 = M.getOrInsertFunction("funcName", Type::getVoidTy(M.getContext()),Type::getInt8PtrTy(M.getContext()),NULL);    
hook1= cast<Function>(hookFunc2);
IRBuilder <> builder1(a); //where a is the basic block pointer
BasicBlock::iterator BC = a->begin();
Value* args = builder1.CreateGlobalStringPtr(%10); //this is wrong.what should be here instead of %10? I want %10 value.
builder1.CreateCall(hook1, args)

1 answer

  • answered 2018-05-16 15:41 Aatira Anum

    Thanks for reply. I got it. The code is %10 = getelementptr inbounds [10000 x i8], [10000 x i8]* %line, i32 0, i32 0 %11 = load %struct._IO_FILE*, %struct._IO_FILE** %fp, align 8 %12 = call i8* @fgets(i8* %10, i32 10000, %struct._IO_FILE* %11)

    I can get %10 by getting fgets 1 argument using

       CallInst abc = cast<CallInst>(&(*i)); //where i is the instruction pointer where fgets is called.
       Value* args = abc->getOperand(0);
       builder1.CreateCall(hook1, args)