【问题标题】:pass a local variable to a function llvm将局部变量传递给函数 llvm
【发布时间】:2018-05-16 05:45:40
【问题描述】:

我使用的是 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)

【问题讨论】:

  • 您需要以某种方式获取该变量的引用。你能显示定义%10的代码吗?
  • %10 = getelementptr inbounds [10000 x i8], [10000 x i8]* %line, i32 0, i32 0 %11 = 加载 %struct._IO_FILE*, %struct._IO_FILE** % fp, align 8 %12 = call i8* @fgets(i8* %10, i32 10000, %struct._IO_FILE* %11)

标签: llvm llvm-ir


【解决方案1】:

感谢您的回复。我得到了它。代码是 %10 = getelementptr 入站 [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)

【讨论】:

  • 您介意展示一下您的 hook1 函数的样子吗?很抱歉在同样的问题上苦苦挣扎,除了我不断收到我的调用参数类型与函数签名错误不匹配。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多