Golang C(.so)在调用时导入分段违例

时间:2016-12-26 09:23:18

标签: c linux go shared-libraries

您好我将在linux环境中使用golang与第三方库(.so文件)一起工作。所以我尝试用一​​些微不足道的东西来练习,比如从linux native libs导入函数。并坚持导入和调用sqrt函数。这是我的代码:

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)

        }

当我运行它时,我总是会遇到分段违规:

/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

有什么问题? 提前谢谢。

P.S。 当我重新定义本机Go函数的函数指针(比如这里Go: convert unsafe.Pointer to function pointer and vice versa)时,一切正常。但导入失败。

2 个答案:

答案 0 :(得分:1)

这是解决方案。我不得不使用桥C功能:

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))

}

答案 1 :(得分:0)

您的情况是否可以让cgo为您链接图书馆?例如:

  ButtonNextLevel: function() {
    this.levelindex++;
    this.state.start('levelstate'); // just restart the same state
  }

对于第三方库/some/lib/dir/libxxx.so:

package main

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

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