将局部变量传递给函数llvm

时间:2018-05-16 05:45:40

标签: llvm llvm-ir

我正在使用llvm 4.0。我需要挂钩一个函数,其中参数是IR中的局部变量之一,例如我想将%10值传递给函数。 我知道如何挂钩一个函数。以下是我的代码:

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();
builder1.SetInsertPoint(&(*BC));
Value* args = builder1.CreateGlobalStringPtr(%10); //this is wrong.what should be here instead of %10? I want %10 value.
builder1.CreateCall(hook1, args)

1 个答案:

答案 0 :(得分:0)

感谢您的回复。我知道了。代码是        %10 = getelementptr inbounds [10000 x i8],[10000 x i8] *%line,i32 0,i32 0        %11 =加载%struct._IO_FILE *,%struct._IO_FILE **%fp,对齐8        %12 =调用i8 * @fgets(i8 *%10,i32 10000,%struct._IO_FILE *%11)

我可以通过使用

获取fgets 1参数来获得%10
   CallInst abc = cast<CallInst>(&(*i)); //where i is the instruction pointer where fgets is called.
   Value* args = abc->getOperand(0);
   builder1.CreateCall(hook1, args)