6

I want to insert a function all before a certain instruction but the function call is defined in another file. I tried

IRBuilder<> Builder(pi);
CallInst *callOne = Builder.CreateCall(func_ins, "foo");

where func_ins is Function*(or Value* to be more general) and foo is the variable name prefix of calling function got assigned. Since this function is defined in another file I've no idea where the pointer func_ins should point to so I just set it to NULL but it didn't work.

Can anyone give me some hints on how to resolve this problem?

One more issue is can I use WriteBitcodeToFile to dump the instrumented code which has external function call to file because I‘m wondering it may report Referencing function in another module or Broken Module while performing module checking?

1 Answer 1

11

You may only call a function from the same Module, and you may not use NULL as the callee.

If the function is defined in another module, you need to first declare it in the module in which you want to make the call, then make the call using the declaration.

To declare it, create an identical function in the new module (via Function::Create) and just don't assign it a body.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks and here's part of sample code I'm using, I assume that Int1 represents for bool.FunctionType *FT = FunctionType::get(Type::getInt1Ty(getGlobalContext()), false); Value* func_ins = Function::Create(FT, Function::ExternalLinkage, "ExternalFunc", Mod); remaining builder part is the same as in the question
Could you please give me some hints on how to pass function parameters as well? By FunctionType::get method there is one argument called ArrayRef<Type *> and I guess it is used to pass arguments. What should I do if I want to pass three parameters(say int, float* and user defined struct*) into it?
@MinGao just check out ArrayRef's constructors, there's a variety of ways to create one - including from an array and from an std::vector.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.