使用Cgo时出现“动态符号的意外R_X86_64_64重定位”

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

"unexpected R_X86_64_64 relocation for dynamic symbol" when using Cgo

问题

我正在尝试使用Cgo为一个C库创建绑定。我有一个使用Cgo导入该库并进行一些调用的包。它可以成功编译和安装。但是当我尝试从Go程序中使用该包时,在链接时出现错误“unexpected R_X86_64_64 relocation for dynamic symbol”。

有什么想法吗?

英文:

I am trying to create binding for a C library using Cgo. I have package which uses Cgo to import the library and make some calls to it. It compiles and installs fine. But when trying to use that package from a Go program, I get the the error "unexpected R_X86_64_64 relocation for dynamic symbol" when linking.

Any ideas?

答案1

得分: 1

它出现在6g编译器的汇编生成例程中:

case 256 + R_X86_64_64:
        if(targ->dynimpname != nil && !targ->dynexport)
                diag("对于动态符号%s,出现了意外的R_X86_64_64重定位",
                     targ->name);
        r->type = D_ADDR;
        return;

R_X86_64_64是库中的一种符号类型。有关amd64架构中重定位的更多信息,请参考此处的第70页。

您是否可能将386编译的库与amd64代码混合使用?

编译器应该报告导致问题的确切符号。您可以尝试链接一个包含其他符号的最小库,并尝试找到一个失败的最小示例吗?

您是否成功使用cgo与任何库一起使用?

英文:

It appears in the assembly generation routines in the 6g compiler:

case 256 + R_X86_64_64:
        if(targ->dynimpname != nil && !targ->dynexport)
                diag("unexpected R_X86_64_64 relocation for dynamic symbol %s",
                     targ->name);
        r->type = D_ADDR;
        return;

The R_X86_64_64 is a type of a symbol in the library. For more information about relocation in the amd64 architecture consult page ~70 here.

Is it possible that you mix 386 compiled library with amd64 code?

The compiler should report the exact symbol which caused the problem. Can you try linking with a minimal library containing other symbols, and try to locate a minimal example where it fails?

Did you manage to use cgo with any library at all?

答案2

得分: 0

我同意Elazar的观点,混合使用32位和64位代码似乎是可能的。

你尝试过gccgo吗?

英文:

I agree with Elazar that it seems plausible that mixing of 32-bit and 64-bit code is involved.

Have you tried gccgo?

答案3

得分: 0

运行得很好:

root@Ubuntu-1304-raring-64-minimal:/etc# uname -a
Linux Ubuntu-1304-raring-64-minimal 3.8.13.4 #2 SMP Mon Jul 8 23:59:05 CEST 2013 x86_64 x86_64 x86_64 GNU/Linux

按照以下顺序执行:

cd /usr/local
mkdir /var/go
apt-get install mercurial
hg clone https://code.google.com/p/go/

之后创建一个 /etc/profile.d/go.sh 文件,并将以下内容添加到其中,并使其可执行:

export GOPATH=/var/go
export GOROOT=/usr/local/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN
export GOROOT_FINAL=/var/go
export GOHOSTARCH=amd64
export GOARCH=amd64
export CGO_ENABLED=1

然后重新启动你的shell。注意:source xxx 将无法正常工作 - 请注意!在新的shell中执行以下操作:

cd /usr/local/go/src
./make.bash

完成后,将所有内容从 /usr/local/go 复制到 /var/go - 或者可以通过符号链接的方式合并两个目录,根据你的喜好选择。之后,在上述go.sh脚本中注释掉GOROOT_FINAL,并再次重新启动你的shell。然后,你就可以使用最新的Go语言了!

root@Ubuntu-1304-raring-64-minimal:/usr/work/golang/go/src# go version
go version devel +35d5bae6aac8 Fri Oct 18 10:45:19 2013 +0400 linux/amd64

请注意:需要两次重新启动shell - 我自己通过艰难的方式发现的。

英文:

Works like a charm :

root@Ubuntu-1304-raring-64-minimal:/etc# uname -a
Linux Ubuntu-1304-raring-64-minimal 3.8.13.4 #2 SMP Mon Jul 8 23:59:05 CEST 2013 x86_64 x86_64 x86_64 GNU/Linux

do this sequence :

cd /usr/local
mkdir /var/go
apt-get install mercurial
hg clone https://code.google.com/p/go/

After that create a /etc/profile.d/go.sh with the follewing contents and make it executable:

export GOPATH=/var/go
export GOROOT=/usr/local/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN
export GOROOT_FINAL=/var/go
export GOHOSTARCH=amd64
export GOARCH=amd64
export CGO_ENABLED=1

And then restart your shell. NO source xxx will be working properly - be warned! In a new shell do this :

cd /usr/local/go/src
./make.bash

do the thing, and then copy all the things from /usr/local/go to /var/go - or there's a way to merge both directories by symlink, whatever you prefer. After that comment GOROOT_FINAL in go.sh script above AND restart your shell again. And you're ok with latest working Go language!

root@Ubuntu-1304-raring-64-minimal:/usr/work/golang/go/src# go version
go version devel +35d5bae6aac8 Fri Oct 18 10:45:19 2013 +0400 linux/amd64

Note bene : Two shell restarts are required - found it myself in a hard way.

huangapple
  • 本文由 发表于 2011年5月29日 01:05:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/6163096.html
匿名

发表评论

匿名网友

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

确定