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 0000000..327ce0e Binary files /dev/null and b/bootstrap/examples/ffi/foo/foo.o differ diff --git a/bootstrap/examples/ffi/foo/libfoo.so b/bootstrap/examples/ffi/foo/libfoo.so new file mode 100755 index 0000000..5b47227 Binary files /dev/null and b/bootstrap/examples/ffi/foo/libfoo.so differ diff --git a/bootstrap/pkg/dl/dl.go b/bootstrap/pkg/dl/dl.go new file mode 100644 index 0000000..f0b1f84 --- /dev/null +++ b/bootstrap/pkg/dl/dl.go @@ -0,0 +1,86 @@ +/* + Serene --- Yet an other Lisp + +Copyright (c) 2020 Sameer Rahmani + +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 +}