Golang 在交叉编译时无法在 x86_64 机器上 link 一个 aarch64/arm64 二进制文件

Golang fails to link an aarch64/arm64 binary on an x86_64 machine while cross compiling

我正尝试在我的 x86_64 桌面上为 aarch64 机器交叉编译 https://github.com/joohoi/acme-dns

$ CC=aarch64-linux-gnu-gcc GOOS=linux GOARCH=arm64 CGO_ENABLED=1 go build -v -ldflags="-extld=$CC"
# github.com/mattn/go-sqlite3
sqlite3-binding.c: In function ‘sqlite3SelectNew’:
sqlite3-binding.c:125322:10: warning: function may return address of local variable [-Wreturn-local-addr]
125322 |   return pNew;
       |          ^~~~
sqlite3-binding.c:125282:10: note: declared here
125282 |   Select standin;
       |          ^~~~~~~
# github.com/joohoi/acme-dns
/usr/lib/go-1.15/pkg/tool/linux_amd64/link: running gcc failed: exit status 1
/usr/bin/ld: /tmp/go-link-266874795/go.o: Relocations in generic ELF (EM: 183)
/usr/bin/ld: /tmp/go-link-266874795/go.o: Relocations in generic ELF (EM: 183)
/usr/bin/ld: /tmp/go-link-266874795/go.o: Relocations in generic ELF (EM: 183)
/usr/bin/ld: /tmp/go-link-266874795/go.o: Relocations in generic ELF (EM: 183)
/usr/bin/ld: /tmp/go-link-266874795/go.o: Relocations in generic ELF (EM: 183)
/usr/bin/ld: /tmp/go-link-266874795/go.o: Relocations in generic ELF (EM: 183)
/usr/bin/ld: /tmp/go-link-266874795/go.o: Relocations in generic ELF (EM: 183)
/usr/bin/ld: /tmp/go-link-266874795/go.o: Relocations in generic ELF (EM: 183)
/usr/bin/ld: /tmp/go-link-266874795/go.o: Relocations in generic ELF (EM: 183)
/usr/bin/ld: /tmp/go-link-266874795/go.o: Relocations in generic ELF (EM: 183)
/usr/bin/ld: /tmp/go-link-266874795/go.o: Relocations in generic ELF (EM: 183)
/usr/bin/ld: /tmp/go-link-266874795/go.o: Relocations in generic ELF (EM: 183)
/usr/bin/ld: /tmp/go-link-266874795/go.o: Relocations in generic ELF (EM: 183)
/usr/bin/ld: /tmp/go-link-266874795/go.o: Relocations in generic ELF (EM: 183)
/usr/bin/ld: /tmp/go-link-266874795/go.o: Relocations in generic ELF (EM: 183)
/usr/bin/ld: /tmp/go-link-266874795/go.o: Relocations in generic ELF (EM: 183)
/usr/bin/ld: /tmp/go-link-266874795/go.o: Relocations in generic ELF (EM: 183)
/usr/bin/ld: /tmp/go-link-266874795/go.o: Relocations in generic ELF (EM: 183)
/usr/bin/ld: /tmp/go-link-266874795/go.o: error adding symbols: file in wrong format
collect2: error: ld returned 1 exit status
CC=aarch64-linux-gnu-gcc GOOS=linux GOARCH=arm64 CGO_ENABLED=1 go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/home/voltagex/.cache/go-build"
GOENV="/home/voltagex/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/voltagex/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/voltagex/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go-1.15"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.15/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="aarch64-linux-gnu-gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/btrfs/src/acme-dns/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build156138713=/tmp/go-build -gno-record-gcc-switches"

go env 看起来是正确的,除了 GOTOOLDIR - 我知道这是一个计算字段。

go-sqlite3 本身似乎可以正确交叉编译。

我已经用 golang 1.15 和 1.17.1 试过了。

主机 OS 是 Debian 11,gcc 10.2.1

两个问题:

  1. 如何获得正确的 arm64 链接器?我想我也需要交叉编译这个?
  2. 为什么 GOTOOLPATH 在交叉编译时指向错误的位置,我该如何解决这个问题?

问题重现,并通过将 -ldflags="-extld=$CC" 替换为 -ldflags="-extld=aarch64-linux-gnu-gcc" 来解决。

或者,您也可以预先 export CC 变量。

错误输出是由不匹配的链接器引起的(对于您的原始构建命令,仍然调用了 x86-64 链接器)。

在我的两台主机上测试:一台Ubuntu 20.04 + go1.13,另一台Ubuntu 18.04 + go1.16.


更多解释:

似乎内联 CC env 变量设置已传递给 go 工具,但未用于 shell 的参数替换。以下输出 (Bash 5.0) 证明了这一点:

anna@LAPTOP-KV4759EJ:~/git/github.com/joohoi/acme-dns$ CC=123 echo $CC

anna@LAPTOP-KV4759EJ:~/git/github.com/joohoi/acme-dns$ export CC=123; echo $CC
123

请注意第一个 echo 没有产生任何输出。

Inspired by Relocations in generic ELF (EM: 40)