意外的故障地址0x0,在使用dll api时发生。

huangapple go评论79阅读模式
英文:

unexpected fault address 0x0 while using dll api

问题

我正在尝试在Go语言中使用一些第三方的dll文件,但是遇到了很大的困难。我更希望使用纯Go的实现,但这不是一个选择。

这是我的示例程序:

import (
	"fmt"
	"github.com/andlabs/dl"
)

type GetObject func(string, string) int

func main() {
	gro := new(GetObject)
	d, err := dl.Open("/home/vaishnavi/lib/libVsphere.so", dl.Lazy)
	if err != nil {
		panic(err)
	}
	fmt.Println("DLL Loaded:", d)

	s, err := d.Symbol("GetSum")
	if err != nil {
		panic(err)
	}

	gro = (*GetObject)(s)

	p := (*gro)("1", "2")
	fmt.Println("Got returned result:", p)
	fmt.Println("Converted:", gro)
	fmt.Println("Loaded at:", s)
	if s == nil {
		fmt.Println("Error loading from the dll")
	}
}

运行这个程序时,我得到以下错误:

DLL Loaded:: 15028848
unexpected fault address 0x0
fatal error: fault
[signal 0xb code=0x80 addr=0x0 pc=0x401923]

goroutine 1 [running]:
runtime.gothrow(0x4dcef0, 0x5)
	/usr/local/go/src/runtime/panic.go:503 +0x8e fp=0xc20805fda0 sp=0xc20805fd88
runtime.sigpanic()
	/usr/local/go/src/runtime/sigpanic_unix.go:29 +0x265 fp=0xc20805fdf0 sp=0xc20805fda0
main.main()
	/home/vaishnavi/ESXI_VHBS/src/main/main.go:25 +0x343 fp=0xc20805ff98 sp=0xc20805fdf0
runtime.main()
	/usr/local/go/src/runtime/proc.go:63 +0xf3 fp=0xc20805ffe0 sp=0xc20805ff98
runtime.goexit()
	/usr/local/go/src/runtime/asm_amd64.s:2232 +0x1 fp=0xc20805ffe8 sp=0xc20805ffe0

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
	/usr/local/go/src/runtime/asm_amd64.s:2232 +0x1
Error: process exited with code 2.

希望能得到帮助。

英文:

I am trying to use some third party dll in go language. But facing enormous difficulties. I would prefer pure go implementation but that is not a choice.

This my sample program

import (
"fmt"
"github.com/andlabs/dl"
)

type GetObject func(string, string) int

func main() {
    gro := new(GetObject)
    d, err := dl.Open("/home/vaishnavi/lib/libVsphere.so", dl.Lazy)
    if err!=nil{
        panic(err)
    }
    fmt.Println("DLL Loaded::", d)
   
    s, err := d.Symbol("GetSum")
    if err != nil {
    	panic(err)
    }

    gro = (*GetObject)(s)

    p := (*gro)("1","2")
    fmt.Println("Got returned result::", p)
    fmt.Println("Converted::", gro)
    fmt.Println("Loaded at::", s)
    if s == nil {
	    fmt.Println("Error loading from the dll")
    }

}

While running this program i am getting following error

DLL Loaded:: 15028848
unexpected fault address 0x0
fatal error: fault
[signal 0xb code=0x80 addr=0x0 pc=0x401923]

goroutine 1 [running]:
runtime.gothrow(0x4dcef0, 0x5)
    /usr/local/go/src/runtime/panic.go:503 +0x8e fp=0xc20805fda0   sp=0xc20805fd88
runtime.sigpanic()
/usr/local/go/src/runtime/sigpanic_unix.go:29 +0x265 fp=0xc20805fdf0 sp=0xc20805fda0
main.main()
 	    /home/vaishnavi/ESXI_VHBS/src/main/main.go:25 +0x343     fp=0xc20805ff98 sp=0xc20805fdf0
runtime.main()
        /usr/local/go/src/runtime/proc.go:63 +0xf3 fp=0xc20805ffe0     sp=0xc20805ff98
runtime.goexit()
    /usr/local/go/src/runtime/asm_amd64.s:2232 +0x1 fp=0xc20805ffe8 sp=0xc20805ffe0

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
    /usr/local/go/src/runtime/asm_amd64.s:2232 +0x1
Error: process exited with code 2.

Any help will be appreciated.

答案1

得分: 2

包文档中写道:

Symbol在模块中查找给定的命名符号。注意,Symbol的值可以为nil,因此检查symbol是否为nil不会表示错误;检查err是否为nil才会表示错误。

(强调添加)

包的源代码中看,Symbol和err都为nil的情况只有两种可能。要么该符号确实为空,要么它在库中不存在。确保该符号确实存在,并且您没有拼写错误。通常,您可以使用readelf -Wsnm -g列出.so文件中的所有符号。

英文:

The package documentation says:

>Symbol looks up the given named symbol in the Module. Note that the value of Symbol can be nil, so checking symbol for nil will not indicate an error; checking err for nil is.

(Emphasis added.)

From the package's source code it seems that there are only two reasons both Symbol and err can be nil. Either the symbol is really null, or it doesn't exist in the library. Make sure that the symbol is really there, and that you've not misspelled the name. Usually you can list all symbols in an .so file with readelf -Ws or nm -g.

huangapple
  • 本文由 发表于 2015年7月21日 23:29:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/31543415.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定