Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LLVM Insert function call defined from another file

Tags:

llvm

llvm-ir

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?

like image 614
Min Gao Avatar asked Mar 19 '23 06:03

Min Gao


1 Answers

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.

like image 117
Oak Avatar answered Mar 26 '23 00:03

Oak