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

marwayusuf@feng.bu.edu.eg via llvm-dev llvm-dev at lists.llvm.org
Wed Sep 9 07:21:15 PDT 2015


Thanks a million for your help.
foo function is properly called now, if I pass no parameters. However, it stops if I try to pass an llvm Function as a prameter.
The problem arises if I try to perform any operation (like getName) on this passed function. It gives me a segmentation fault. I checked and found it's not NULL.
I understood from your answer that I should convert the function pointer into an integer that represents its address and then convert it back into a pointer in the instrumented IR. However, I could not get how exactly to do that. I tried some code but with no luck. If you please explain the idea more, I'll appreciate. Thanks in advance.

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

________________________________________
From: Tim Northover <t.p.northover at gmail.com>
Sent: Wednesday, September 2, 2015 4:37 PM
To: marwayusuf at feng.bu.edu.eg
Cc: llvm-dev at lists.llvm.org
Subject: Re: [llvm-dev] creating a callinst to an external function

On 19 August 2015 at 23:42, marwayusuf at feng.bu.edu.eg via llvm-dev
<llvm-dev at lists.llvm.org> wrote:
>       void myclass::foo(Function *f, BasicBlock* b)
> [...]
>  call:                   call void @foo(i32 ()* @main, label %for.cond)

The problem here is that a "Function *" isn't simply a function
pointer, or even a function pointer at all. It's a compile-time
representation of some function. Similar comments apply to BasicBlock.
I'm quite surprised it even reached SelectionDAGBuilder before blowing
up.

That information is not normally available at runtime, though in
principle you could get one in there if you were JITing. If you know
where the Function lives when you're generating the IR (and can
guarantee it won't move) you could aim for something like:

   call void @foo(i8* inttoptr(i64 0x123450 to i8*), inttoptr(i64 ...))

Something like:

    Function *Main = ...;
    Value *Arg = Constant::CreateIntToPtr(reinterpret_cast<uint64_t>(Main));

I.e. instead of using Main as a Value in the call expression,
completely hide the fact that it's an LLVM object from the compiler
and pass it in as an opaque integer. Also, don't forget the "this"
pointer if "foo" is not a static method.

Cheers.

Tim.


More information about the llvm-dev mailing list