From b0925a527b26c13272f2f14c94e4f365b25aa195 Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Fri, 25 Dec 2020 21:57:13 +0000 Subject: [PATCH] [Bootstrap] Add a sample shared library to used with ffi api --- bootstrap/examples/ffi/foo.srn | 16 +++++ bootstrap/examples/ffi/foo/Makefile | 5 ++ bootstrap/examples/ffi/foo/foo.c | 7 +++ bootstrap/examples/ffi/foo/foo.h | 6 ++ bootstrap/examples/ffi/foo/foo.o | Bin 0 -> 1544 bytes bootstrap/examples/ffi/foo/libfoo.so | Bin 0 -> 15464 bytes bootstrap/pkg/dl/dl.go | 86 +++++++++++++++++++++++++++ 7 files changed, 120 insertions(+) create mode 100644 bootstrap/examples/ffi/foo.srn create mode 100644 bootstrap/examples/ffi/foo/Makefile create mode 100644 bootstrap/examples/ffi/foo/foo.c create mode 100644 bootstrap/examples/ffi/foo/foo.h create mode 100644 bootstrap/examples/ffi/foo/foo.o create mode 100755 bootstrap/examples/ffi/foo/libfoo.so create mode 100644 bootstrap/pkg/dl/dl.go diff --git a/bootstrap/examples/ffi/foo.srn b/bootstrap/examples/ffi/foo.srn new file mode 100644 index 0000000..ca1d4fc --- /dev/null +++ b/bootstrap/examples/ffi/foo.srn @@ -0,0 +1,16 @@ +(ns examples.ffi.foo + :require '[adssa + [serene.ffi.libllvm]]) + +(defmacro deffi.....) + +(ffi/deffi some-function + :fn-name "some_function" + :args {:x :i32 + :y :char_ptr} + + :return :void) + + + +(some-function 12 "asdasd") diff --git a/bootstrap/examples/ffi/foo/Makefile b/bootstrap/examples/ffi/foo/Makefile new file mode 100644 index 0000000..7a4e56f --- /dev/null +++ b/bootstrap/examples/ffi/foo/Makefile @@ -0,0 +1,5 @@ +foo: + gcc -c -Wall -Werror -fpic foo.c + gcc -shared -o libfoo.so foo.o + +all: foo diff --git a/bootstrap/examples/ffi/foo/foo.c b/bootstrap/examples/ffi/foo/foo.c new file mode 100644 index 0000000..fdb4e9d --- /dev/null +++ b/bootstrap/examples/ffi/foo/foo.c @@ -0,0 +1,7 @@ +#include + + +void bar(void) +{ + puts("Hello, I am a shared library"); +} diff --git a/bootstrap/examples/ffi/foo/foo.h b/bootstrap/examples/ffi/foo/foo.h new file mode 100644 index 0000000..a50f948 --- /dev/null +++ b/bootstrap/examples/ffi/foo/foo.h @@ -0,0 +1,6 @@ +#ifndef foo_h__ +#define foo_h__ + +extern void baz(void); + +#endif // foo_h__ diff --git a/bootstrap/examples/ffi/foo/foo.o b/bootstrap/examples/ffi/foo/foo.o new file mode 100644 index 0000000000000000000000000000000000000000..327ce0edcf0a8b3f1740ff512d0e8620247734b4 GIT binary patch literal 1544 zcmb_bOKTKC5U$yYNsJp=6AytXbS|QhGzqzwfU*zvAuI{xfhQSzHxpLe$t*oBBwlh0 zco5>v|Kr7@e?su6cOh2obdk0*dlA2-r>efLue$oN=gn@b6a>Hs;5l4J%mREZUHjFs zTZMTj!(r#c_s+*>#QdPo<*P59csNW~#S5Vlp+q)NIzAG^H$AQN89=+y*cOl5@rg-O zu_ZU<`kLMlqs=FIKs5;Wa|6NqJqS)K!E$+K_7WR=fIJPtB7JIiD;K54{PYecP&@&& zrz!Y#RWuzI?ie7%UlBsARD=+2713B_W&&T_aR}2l@~Ogt)8}~a$lbwFxseU9GJ%KV zG?jhmDGgD(yIZeyquo|(zj+WH)au=41fzE*LoIhq*G{|oeU+n(K7mev)@ELyPoR9~ zsQ}3AEHSEw*67?0*wgV)NfVzMkVncWki9Gesna~@r%8gZ1msC-V%gp~T+58=zr_TS z#e?Wrt0W#T`X4?5YtS37la6h5!7g+fbP{%8ee(1i)fz0I5Np6{G< zf6mO@JJY%MgYLebs#J=Q(zJ_2I?$wj|^Gzkd3&|#5EoH?oRSjHPh1V;Vlv98I{22HyvvAIgi${_qi6hCGBl<@;z zHSGk}>SE}?-C$VSsgx$-xW?E4#a7Zm_LKUaG$DNbroKn@Z^pC}XhcH)M3#C@JF3x! zZ;zd;dt6(jewAQwgt;XNywsx-`1OzAdzfFHV1J0YB~(W>i?wnoY&`Gycz(hw27V#% zJo5Go?DdATh3xUsVvsEi?Cl)OPh5{0 zzXbRiFYN4K;oebh;k_ImZq{sPIdv5E0`I@a%0ukK4Vh_Rj~13M9lajLr6Lm2 zYMX2XYy@lsYy@lsYy@lsYy@lsYy@lsYy@lsp3Vrk*|D+wdbLOSk1G%Zn`{JZ1Z)Is1Z)Is1Z)Is1Z)Is1Z)Is1pZqD%sN!8JH?t(S<5P^UPF0d z>mnmJ8^7m_jPTM%qU2?0D7ly_}fk7-* zZZ>jx?Q2F8#*LuId#kK(mGrCzQq?*G{*);Q?YwO4cu{YdP~O9mZvSCp&tH?n{veM0 z8XL-8M*p{w%k4Df{a=7}?D6`x#?~G8c6Pp|T6?n-K|ZgxJ8jOU4TV?KiMDlGh`U$F zX<3I9xf@_A>7WN*VLdERg_@8$f7Vc8AQb!1=r-aVbn_U=36b-P|q zfBW8U&+Y1GiZ@Pp-TS&^M@4Vn{*LxOZ+}nE8{GrmKzm1DH?v$`@YvlJvVkI#iSC<_ zZ52t|HJ)HgMrA?d6i<%_{vcye(3nH+_{5}>^NTrhhEGqhnZ}?%j%ZG13&qj=M1|n7 zu8s6!5<$T;_CB(l+29_%NZ;d$r;LzkMnq&bQ1qPC-d-MJl9mg6&Zu_e_BbPG0$n1 zXf0=CZiOD>HW2fn%wuvd!yohF2IfQue~jSZiNZ@ICijB z=4SAiFMvwBhi#0u-_UfBGxA6Pf6OaD^dI_o|KBwJ(hf;@z5pUl;?JYYocLv#lXl_x zr##M>@g_c^n#ULR(CHXM)F1O%8I2tP!x-?Ln19A7faov$Ap_<7M(fA^Lg4psG>CdZ z%Xk^}$2=^P8f!Cxt@1>*kEY + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +package dl + +/* +#cgo linux LDFLAGS: -ldl +#cgo pkg-config: libffi +#include +#include +#include +#include + +#include + +static uintptr_t openShareLib(const char* path, char** err) { + void* h = dlopen(path, RTLD_NOW|RTLD_GLOBAL); + if (h == NULL) { + *err = (char*)dlerror(); + } + return (uintptr_t)h; +} + +static void* shareLibLookup(uintptr_t h, const char* name, char** err) { + void* r = dlsym((void*)h, name); + if (r == NULL) { + *err = (char*)dlerror(); + } + return r; +} +*/ +import "C" +import ( + "fmt" + "unsafe" +) + +type SharedLib struct{} + +func Open(libPath string) (*SharedLib, error) { + cPath := make([]byte, C.PATH_MAX+1) + cRelName := make([]byte, len(libPath)+1) + copy(cRelName, libPath) + + // If the given libPath exists, fill the cPath with the absolute path + // to the file (it follows symlinks). + if C.realpath( + (*C.char)(unsafe.Pointer(&cRelName[0])), + (*C.char)(unsafe.Pointer(&cPath[0]))) == nil { + return nil, fmt.Errorf("can't find the shared library '%s'", libPath) + } + + //filepath := C.GoString((*C.char)(unsafe.Pointer(&cPath[0]))) + + var cErr *C.char + h := C.openShareLib((*C.char)(unsafe.Pointer(&cPath[0])), &cErr) + + if h == 0 { + // lock.Unlock() + return nil, fmt.Errorf("failed to open shared library: '%s' due to: '%s'", libPath, C.GoString(cErr)) + } + + // if plugins == nil { + // plugins = make(map[string]*Plugin) + // } + initTask := C.shareLibLookup(h, C.CString("bar"), &cErr) + + fmt.Printf(">> %p \n", initTask) + + return &SharedLib{}, nil +}