Hayden Livingston
2015-Feb-18 05:34 UTC
[LLVMdev] Calling into non-linked function via a pointer
This is my module's dump, which is different than yours. I wonder how I can get the external thing for my function. define double @sum(double, double) { entry: call void @FooBar() %tmp = fadd double %0, %1 ret double %tmp } ; ModuleID = 'My_Module' define double @sum(double, double) { entry: call void @FooBar() %tmp = fadd double %0, %1 ret double %tmp } declare void @FooBar() On Tue, Feb 17, 2015 at 9:07 PM, Bruce Hoult <bruce at hoult.org> wrote:> Naturally you can, since C can. As usual, it's instructive to see what > Clang generates e.g. > > typedef int fn(int); > > int test(fn f, int val){ > return f(val); > } > > ----------- > > Compiled with: clang callfuncptr.c -O -S -emit-llvm -o callfuncptr.ll > > ; ModuleID = 'callfuncptr.c' > > target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" > > target triple = "x86_64-apple-macosx10.10.0" > > > ; Function Attrs: nounwind ssp uwtable > > define i32 @test(i32 (i32)* nocapture %f, i32 %val) #0 { > > %1 = tail call i32 %f(i32 %val) #1 > > ret i32 %1 > > } > > ------------- > > Or... > > > typedef int fn(int); > > extern fn *f; > > int test(int val){ > return f(val); > } > > ----------------------- > > ; ModuleID = 'callfuncptr.c' > > target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" > > target triple = "x86_64-apple-macosx10.10.0" > > > @f = external global i32 (i32)* > > > ; Function Attrs: nounwind ssp uwtable > > define i32 @test(i32 %val) #0 { > > %1 = load i32 (i32)** @f, align 8, !tbaa !1 > > %2 = tail call i32 %1(i32 %val) #1 > > ret i32 %2 > > } > > > On Wed, Feb 18, 2015 at 5:52 PM, Hayden Livingston <halivingston at gmail.com > > wrote: > >> I'm having a problem of being unable to call into an arbitrary function >> that is loaded into memory whose pointer address is known to me but was not >> linked into LLVM. >> >> I have added the function and called LLVMAddGlobalMapping with the >> pointer, and the program segfaults. >> >> I was wondering if it is a supported scenario that LLVM can generate a >> call into an arbitrary function that is not linked. >> >> >> >> _______________________________________________ >> LLVM Developers mailing list >> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu >> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >> >> >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150217/662e918d/attachment.html>
Hayden Livingston
2015-Feb-18 05:51 UTC
[LLVMdev] Calling into non-linked function via a pointer
I have got this, but still no luck, I'm getting a segfault. ; ModuleID = 'My_Module' @FooBar = external global void () define double @sum(double, double) { entry: call void @FooBar() %tmp = fadd double %0, %1 ret double %tmp } On Tue, Feb 17, 2015 at 9:34 PM, Hayden Livingston <halivingston at gmail.com> wrote:> This is my module's dump, which is different than yours. I wonder how I > can get the external thing for my function. > > define double @sum(double, double) { > entry: > call void @FooBar() > %tmp = fadd double %0, %1 > ret double %tmp > } > > ; ModuleID = 'My_Module' > > define double @sum(double, double) { > entry: > call void @FooBar() > %tmp = fadd double %0, %1 > ret double %tmp > } > > declare void @FooBar() > > > On Tue, Feb 17, 2015 at 9:07 PM, Bruce Hoult <bruce at hoult.org> wrote: > >> Naturally you can, since C can. As usual, it's instructive to see what >> Clang generates e.g. >> >> typedef int fn(int); >> >> int test(fn f, int val){ >> return f(val); >> } >> >> ----------- >> >> Compiled with: clang callfuncptr.c -O -S -emit-llvm -o callfuncptr.ll >> >> ; ModuleID = 'callfuncptr.c' >> >> target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" >> >> target triple = "x86_64-apple-macosx10.10.0" >> >> >> ; Function Attrs: nounwind ssp uwtable >> >> define i32 @test(i32 (i32)* nocapture %f, i32 %val) #0 { >> >> %1 = tail call i32 %f(i32 %val) #1 >> >> ret i32 %1 >> >> } >> >> ------------- >> >> Or... >> >> >> typedef int fn(int); >> >> extern fn *f; >> >> int test(int val){ >> return f(val); >> } >> >> ----------------------- >> >> ; ModuleID = 'callfuncptr.c' >> >> target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" >> >> target triple = "x86_64-apple-macosx10.10.0" >> >> >> @f = external global i32 (i32)* >> >> >> ; Function Attrs: nounwind ssp uwtable >> >> define i32 @test(i32 %val) #0 { >> >> %1 = load i32 (i32)** @f, align 8, !tbaa !1 >> >> %2 = tail call i32 %1(i32 %val) #1 >> >> ret i32 %2 >> >> } >> >> >> On Wed, Feb 18, 2015 at 5:52 PM, Hayden Livingston < >> halivingston at gmail.com> wrote: >> >>> I'm having a problem of being unable to call into an arbitrary function >>> that is loaded into memory whose pointer address is known to me but was not >>> linked into LLVM. >>> >>> I have added the function and called LLVMAddGlobalMapping with the >>> pointer, and the program segfaults. >>> >>> I was wondering if it is a supported scenario that LLVM can generate a >>> call into an arbitrary function that is not linked. >>> >>> >>> >>> _______________________________________________ >>> LLVM Developers mailing list >>> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu >>> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >>> >>> >> >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150217/4b7771f8/attachment.html>
Bruce Hoult
2015-Feb-18 06:17 UTC
[LLVMdev] Calling into non-linked function via a pointer
That's what you get if you don't have the * in 'extern fn *f;' and therefore in '@f = external global i32 (i32)*' Without the * llvm expects to link to a known and provided function. With the * it's a global variable that contains the address of the function. On Wed, Feb 18, 2015 at 6:34 PM, Hayden Livingston <halivingston at gmail.com> wrote:> This is my module's dump, which is different than yours. I wonder how I > can get the external thing for my function. > > define double @sum(double, double) { > entry: > call void @FooBar() > %tmp = fadd double %0, %1 > ret double %tmp > } > > ; ModuleID = 'My_Module' > > define double @sum(double, double) { > entry: > call void @FooBar() > %tmp = fadd double %0, %1 > ret double %tmp > } > > declare void @FooBar() > > > On Tue, Feb 17, 2015 at 9:07 PM, Bruce Hoult <bruce at hoult.org> wrote: > >> Naturally you can, since C can. As usual, it's instructive to see what >> Clang generates e.g. >> >> typedef int fn(int); >> >> int test(fn f, int val){ >> return f(val); >> } >> >> ----------- >> >> Compiled with: clang callfuncptr.c -O -S -emit-llvm -o callfuncptr.ll >> >> ; ModuleID = 'callfuncptr.c' >> >> target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" >> >> target triple = "x86_64-apple-macosx10.10.0" >> >> >> ; Function Attrs: nounwind ssp uwtable >> >> define i32 @test(i32 (i32)* nocapture %f, i32 %val) #0 { >> >> %1 = tail call i32 %f(i32 %val) #1 >> >> ret i32 %1 >> >> } >> >> ------------- >> >> Or... >> >> >> typedef int fn(int); >> >> extern fn *f; >> >> int test(int val){ >> return f(val); >> } >> >> ----------------------- >> >> ; ModuleID = 'callfuncptr.c' >> >> target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" >> >> target triple = "x86_64-apple-macosx10.10.0" >> >> >> @f = external global i32 (i32)* >> >> >> ; Function Attrs: nounwind ssp uwtable >> >> define i32 @test(i32 %val) #0 { >> >> %1 = load i32 (i32)** @f, align 8, !tbaa !1 >> >> %2 = tail call i32 %1(i32 %val) #1 >> >> ret i32 %2 >> >> } >> >> >> On Wed, Feb 18, 2015 at 5:52 PM, Hayden Livingston < >> halivingston at gmail.com> wrote: >> >>> I'm having a problem of being unable to call into an arbitrary function >>> that is loaded into memory whose pointer address is known to me but was not >>> linked into LLVM. >>> >>> I have added the function and called LLVMAddGlobalMapping with the >>> pointer, and the program segfaults. >>> >>> I was wondering if it is a supported scenario that LLVM can generate a >>> call into an arbitrary function that is not linked. >>> >>> >>> >>> _______________________________________________ >>> LLVM Developers mailing list >>> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu >>> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >>> >>> >> > > -- > This message has been scanned for viruses and > dangerous content by *MailScanner* <http://www.mailscanner.info/>, and is > believed to be clean.-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150218/54a4bfac/attachment.html>
Hayden Livingston
2015-Feb-18 07:43 UTC
[LLVMdev] Calling into non-linked function via a pointer
I think I understand what you're trying to say. I have to figure out how to generate that. I've tried a few thigns but failing at different places. I tried creating a load int32, and then trying to call through it, and it fails saying it's not a function type. Then I tried different globlmapping things, and they fail with segfaults. I also tried creating an constant int, and trying to make pointer to it, but I don't think I quite succeeded here either. I'd appreciate any pointers on how to go from a function pointer address to being able to emit call indirect. On Tue, Feb 17, 2015 at 10:17 PM, Bruce Hoult <bruce at hoult.org> wrote:> That's what you get if you don't have the * in 'extern fn *f;' and > therefore in '@f = external global i32 (i32)*' > > Without the * llvm expects to link to a known and provided function. With > the * it's a global variable that contains the address of the function. > > On Wed, Feb 18, 2015 at 6:34 PM, Hayden Livingston <halivingston at gmail.com > > wrote: > >> This is my module's dump, which is different than yours. I wonder how I >> can get the external thing for my function. >> >> define double @sum(double, double) { >> entry: >> call void @FooBar() >> %tmp = fadd double %0, %1 >> ret double %tmp >> } >> >> ; ModuleID = 'My_Module' >> >> define double @sum(double, double) { >> entry: >> call void @FooBar() >> %tmp = fadd double %0, %1 >> ret double %tmp >> } >> >> declare void @FooBar() >> >> >> On Tue, Feb 17, 2015 at 9:07 PM, Bruce Hoult <bruce at hoult.org> wrote: >> >>> Naturally you can, since C can. As usual, it's instructive to see what >>> Clang generates e.g. >>> >>> typedef int fn(int); >>> >>> int test(fn f, int val){ >>> return f(val); >>> } >>> >>> ----------- >>> >>> Compiled with: clang callfuncptr.c -O -S -emit-llvm -o callfuncptr.ll >>> >>> ; ModuleID = 'callfuncptr.c' >>> >>> target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" >>> >>> target triple = "x86_64-apple-macosx10.10.0" >>> >>> >>> ; Function Attrs: nounwind ssp uwtable >>> >>> define i32 @test(i32 (i32)* nocapture %f, i32 %val) #0 { >>> >>> %1 = tail call i32 %f(i32 %val) #1 >>> >>> ret i32 %1 >>> >>> } >>> >>> ------------- >>> >>> Or... >>> >>> >>> typedef int fn(int); >>> >>> extern fn *f; >>> >>> int test(int val){ >>> return f(val); >>> } >>> >>> ----------------------- >>> >>> ; ModuleID = 'callfuncptr.c' >>> >>> target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" >>> >>> target triple = "x86_64-apple-macosx10.10.0" >>> >>> >>> @f = external global i32 (i32)* >>> >>> >>> ; Function Attrs: nounwind ssp uwtable >>> >>> define i32 @test(i32 %val) #0 { >>> >>> %1 = load i32 (i32)** @f, align 8, !tbaa !1 >>> >>> %2 = tail call i32 %1(i32 %val) #1 >>> >>> ret i32 %2 >>> >>> } >>> >>> >>> On Wed, Feb 18, 2015 at 5:52 PM, Hayden Livingston < >>> halivingston at gmail.com> wrote: >>> >>>> I'm having a problem of being unable to call into an arbitrary function >>>> that is loaded into memory whose pointer address is known to me but was not >>>> linked into LLVM. >>>> >>>> I have added the function and called LLVMAddGlobalMapping with the >>>> pointer, and the program segfaults. >>>> >>>> I was wondering if it is a supported scenario that LLVM can generate a >>>> call into an arbitrary function that is not linked. >>>> >>>> >>>> >>>> _______________________________________________ >>>> LLVM Developers mailing list >>>> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu >>>> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >>>> >>>> >>> >> >> -- >> This message has been scanned for viruses and >> dangerous content by *MailScanner* <http://www.mailscanner.info/>, and >> is >> believed to be clean. > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20150217/9289c3b9/attachment.html>