0
votes

So this is what I am trying to do:

     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);
        }

where "hook" is the function. The error I get is:

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

And when I change ArrayRef <Value *> arguments('c')to ArrayRef <char> arguments('c') the error becomes:

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, ^

I lack the understanding to passing arguments to external functions I call in my LLVM pass as I am new to this stuff. Help will be appreciated!

1

1 Answers

3
votes

CallInst::Create needs ArrayRef < Value* > for arguments

so now when you initialized ArrayRef < Value * > arguments('c'), here as there is no inbuilt constructor to convert char 'c' to Value*

you can do

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

or you can directly pass single i8 type integer to CallInst::Create call

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

see http://llvm.org/docs/doxygen/html/classllvm_1_1CallInst.html and http://llvm.org/docs/doxygen/html/classllvm_1_1ArrayRef.html#details for more details.