[llvm-dev] creating a callinst to an external function

marwayusuf@feng.bu.edu.eg via llvm-dev
Wed, 02 Sep 2015 14:17:02 -0000

I tried to remove parameters from the called function "foo". So I got
a different error.

LLVM ERROR: Program used external function 'foo' which could not be
resolved!

Any help?


Regards,
Marwa Yusuf
Teaching Assistant - Computer Engineering Department
Faculty of Engineering - Benha University
E-JUST PhD Student
Computer Science & Engineering Dept.


________________________________
From: llvm-dev <llvm-dev-bounces at lists.llvm.org> on behalf of
marwayusuf at feng.bu.edu.eg via llvm-dev <llvm-dev at lists.llvm.org>
Sent: Thursday, August 20, 2015 12:42 AM
To: llvm-dev at lists.llvm.org
Subject: [llvm-dev] creating a callinst to an external function


Dear All

I'm making an instrumentation pass. The pass is supposed to modify the given
IR in a specefic way. One of the required modifications is to insert a call to a
function at a specific location.

This is the signature of the called function:

      void myclass::foo(Function *f, BasicBlock* b)

This function's prototype is in an foofile.h file in include/llvm

And the function definition is in foofile.cpp file in the MCJIT folder.

and running "make" at this folder works fine and the foofile.cpp is
compiled with MCJIT.cpp and another function in the same file works just fine as
expected.

Not back to the instrumentation pass. How can I insert a callinst to the foo
function in the given IR?

Here is the snippet that inserts the call:

           Type* retTy = Type::getInt32Ty(C);

            FunctionType* FuncTy = FunctionType::get(retTy, false);

            PointerType* PtrToFuncTy = PointerType::get(FuncTy, 0);

            Constant *fun = M->getOrInsertFunction("foo",
Type::getVoidTy(C), PtrToFuncTy, Type::getLabelTy(C), nullptr);

            Function *dofoo = cast<Function>(fun);

            Instruction* dofooCall = CallInst::Create(fun, Args2, "",
bb);


Note: Args2 is an arraylist containing 2 value pointers to a function and a
basicblock, bb is the basicblock to insert the call in.


When I run the pass using op on a given IR, it produces a declaration and a call
correctly like this:

 declaration:       declare void @foo(i32 ()*, label)

 call:                   call void @foo(i32 ()* @main, label %for.cond)

But when I try to run the resulting .ll file using lli, everything explodes!
This the first 2 lines before the stacktrace:

    Can't get register for value!

    UNREACHABLE executed at
/home/marwayusuf/llvm-env/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp:1158!



I concluded that the problem is it can't find the foo function. If this is
the problem, how can I create the callinst correctly?




Regards,
Marwa Yusuf
Teaching Assistant - Computer Engineering Department
Faculty of Engineering - Benha University
E-JUST PhD Student
Computer Science & Engineering Dept.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://lists.llvm.org/pipermail/llvm-dev/attachments/20150902/304d7534/attachment-0001.html>