如何将参数传递给我在 LLVM 传递中使用 CallInst::Create(...) 调用的外部函数?

How to pass arguments to an external function that I am calling using CallInst::Create(...) in my LLVM pass?

这就是我想要做的:

     if(isa<CallInst>(&(*BI)) )
                {
        ArrayRef <Value *> arguments('c');
        StringRef fname = cast<CallInst>(&(*BI))->getCalledFunction()->getName();
        errs()<<fname + " was called\n";
        //CallInst *CI = dyn_cast<CallInst>(BI);
        if(fname=="pthread_mutex_lock"){
            Instruction *newInst = CallInst::Create(hook, arguments, "");
            BB->getInstList().insert(BI, newInst);
        }

其中 "hook" 是函数。我得到的错误是:

no matching constructor for initialization of 'ArrayRef' ArrayRef arguments('c');

当我将 ArrayRef <Value *> arguments('c') 更改为 ArrayRef <char> arguments('c') 时,错误变为:

no matching function for call to 'Create' Instruction *newInst = CallInst::Create(hook, argume... ^~~~~~~~~~~~~~~~ /.../llvm/IR/Instructions.h:1187:20: note: candidate function not viable: no known conversion from 'ArrayRef' to 'ArrayRef' for 2nd argument static CallInst *Create(Value *Func, ^ /.../llvm/llvm-3.4/include/llvm/IR/Instructions.h:1200:20: note: candidate function not viable: no known conversion from 'ArrayRef' to 'const llvm::Twine' for 2nd argument static CallInst *Create(Value *F, const Twine &NameStr = "", ^ /.../llvm/llvm-3.4/include/llvm/IR/Instructions.h:1204:20: note: candidate function not viable: no known conversion from 'ArrayRef' to 'const llvm::Twine' for 2nd argument static CallInst *Create(Value *F, const Twine &NameStr, ^ /.../llvm/llvm-3.4/include/llvm/IR/Instructions.h:1194:20: note: candidate function not viable: requires 4 arguments, but 3 were provided static CallInst *Create(Value *Func, ^

我不了解如何将参数传递给我在 LLVM 传递中调用的外部函数,因为我是新手。帮助将不胜感激!

CallInst::Create 需要 ArrayRef < Value* > 作为参数

所以现在当你初始化 ArrayRef < Value * > arguments('c') 时,这里没有内置构造函数来将 char 'c' 转换为 Value*

你可以做到

ArrayRef< Value* > arguments(ConstantInt::get(Type::getInt8Ty(llvmContext), 'c'));

或 您可以直接将单个 i8 类型整数传递给 CallInst::Create 调用

Instruction *newInst = CallInst::Create(hook, ArrayRef< Value* >{ConstantInt::get(Type::getInt8Ty(llvmContext), 'c')}, "");

有关详细信息,请参阅 http://llvm.org/docs/doxygen/html/classllvm_1_1CallInst.html and http://llvm.org/docs/doxygen/html/classllvm_1_1ArrayRef.html#details