英文:
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 -Ws
或nm -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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论