1

Hello I'm going to work with thirdparty library (.so file) with golang in linux environment. So I tried to practice a bit with something trivial, like importing functions from linux native libs. And got stuck on importing and calling sqrt function. Here is my code:

package main
        // #cgo LDFLAGS: -ldl
        // #include <dlfcn.h>
        // #include <stdio.h>
        import "C"
        import "fmt"

        func main() {


            export_name := "sqrt"
            lib_path := "/lib/libm.so.6"

            //Loading .so
            handle := C.dlopen(C.CString(lib_path), C.RTLD_LAZY)
            if handle == nil {
                fmt.Println(lib_path+":\tNOT FOUND")
                return
            } else {
                fmt.Println(lib_path+":\tSUCCESS")
                }

            //looking for function address
            func_pointer := C.dlsym(handle, C.CString(export_name ))
            if func_pointer == nil {
                fmt.Println(export_name+":\tNOT FOUND")
                return
            } else {
                fmt.Println(export_name+":\t", func_pointer)
                }

            //negotiating datatypes
            //From c lib description: double sqrt(double x);
            sqrt  := *(*(func(float64)float64))(func_pointer)

            //Calling function
            sqrt(4)

        }

when I run it, I always get segmentation violation:

/lib/libm.so.6: SUCCESS
sqrt:    0x7f37117ea270
unexpected fault address 0x0
fatal error: fault
[signal SIGSEGV: segmentation violation code=0x80 addr=0x0 pc=0x4019fa]

goroutine 1 [running]:
runtime.throw(0x4a6643, 0x5)
        /usr/lib/go/src/runtime/panic.go:566 +0x95 fp=0xc42004be00 sp=0xc42004bde0
runtime.sigpanic()
        /usr/lib/go/src/runtime/sigpanic_unix.go:27 +0x288 fp=0xc42004be58 sp=0xc42004be00
main.main()
        /home/afx/goc/so.go:37 +0x2ba fp=0xc42004bf48 sp=0xc42004be58
runtime.main()
        /usr/lib/go/src/runtime/proc.go:183 +0x1f4 fp=0xc42004bfa0 sp=0xc42004bf48
runtime.goexit()
        /usr/lib/go/src/runtime/asm_amd64.s:2086 +0x1 fp=0xc42004bfa8 sp=0xc42004bfa0

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
        /usr/lib/go/src/runtime/asm_amd64.s:2086 +0x1
exit status 2

What is the problem ? Thank you in advance.

P.S. When I'm redefining function pointers of native Go functions (like here Go: convert unsafe.Pointer to function pointer and vice versa) everything works fine. But import fails.

Community
  • 1
  • 1
alex
  • 131
  • 4
  • I don't think you can do it that way. look at this example, you can do it with a static bridge function https://github.com/coreos/pkg/blob/master/dlopen/dlopen_example.go – Not_a_Golfer Dec 26 '16 at 09:41
  • Thank you! Bridge function was the missing link. – alex Dec 28 '16 at 03:14

2 Answers2

2

Here is solution. I had to use bridge C function:

package main

// #cgo LDFLAGS: -ldl
// #include <dlfcn.h>
// #include <stdio.h>
//
// double
// my_sqrt_bridge(void *f, double x)
// {
//  //description: ((return_data_type (*)(input_data_type))bridge_input_function_pointer) (bridge_input_value)
//  return ((double (*)(double))f)(x);
// }
import "C"
import "fmt"

func main() {

    export_name := "sqrt"
    lib_path := "/lib/libm.so.6"

    //Loading .so
    handle := C.dlopen(C.CString(lib_path), C.RTLD_LAZY)
    if handle == nil {
        fmt.Println(lib_path + ":\tNOT FOUND")
        return
    } else {
        fmt.Println(lib_path + ":\tSUCCESS")
    }

    //looking for function address
    func_pointer := C.dlsym(handle, C.CString(export_name))
    if func_pointer == nil {
        fmt.Println(export_name + ":\tNOT FOUND")
        return
    } else {
        fmt.Println(export_name+":\t", func_pointer)
    }

    fmt.Printf("%f", C.my_sqrt_bridge(func_pointer, 2))

}
alex
  • 131
  • 4
0

Would it be possible in your case to let cgo link the library for you? Eg:

package main

/*
#cgo LDFLAGS: -lm
#include <math.h>
*/
import "C"
import "fmt"

func main() {
    fmt.Printf("%g\n", C.sqrt(2))  //prints 1.4142135623730951
}

For a third party library /some/lib/dir/libxxx.so:

LDFLAGS: -L/some/lib/dir -lxxx
Mark
  • 6,731
  • 1
  • 40
  • 38