Go: runtime/cgo: cross compile with cgo for target ARM64/linux fails

Created on 27 Nov 2018  Â·  5Comments  Â·  Source: golang/go

What version of Go are you using (go version)?

$ go version
go version go1.10.1 linux/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output

$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/qbox/gopath"
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build660351216=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I want to cross compile a package for arm64/linux. But I have met some problems.
I can reproduce the problem with a short code blow.

test.go

package main

//int pt(){
// return 1;
//}
import "C"
import "fmt"

func main(){
    fmt.Println(C.pt());
}

I ran env CGO_ENABLED=1 GOOS=linux GOARCH=arm64 go build test.go
And then I got the blow output:

# runtime/cgo
gcc_arm64.S: Assembler messages:
gcc_arm64.S:27: Error: no such instruction: `stp x19,x20,[sp,'
gcc_arm64.S:28: Error: no such instruction: `stp x21,x22,[sp,'
gcc_arm64.S:29: Error: no such instruction: `stp x23,x24,[sp,'
gcc_arm64.S:30: Error: no such instruction: `stp x25,x26,[sp,'
gcc_arm64.S:31: Error: no such instruction: `stp x27,x28,[sp,'
gcc_arm64.S:32: Error: no such instruction: `stp x29,x30,[sp,'
gcc_arm64.S:33: Error: too many memory references for `mov'
gcc_arm64.S:35: Error: too many memory references for `mov'
gcc_arm64.S:36: Error: too many memory references for `mov'
gcc_arm64.S:37: Error: too many memory references for `mov'
gcc_arm64.S:39: Error: no such instruction: `blr x20'
gcc_arm64.S:40: Error: no such instruction: `blr x19'
gcc_arm64.S:42: Error: no such instruction: `ldp x29,x30,[sp],'
gcc_arm64.S:43: Error: no such instruction: `ldp x27,x28,[sp],'
gcc_arm64.S:44: Error: no such instruction: `ldp x25,x26,[sp],'
gcc_arm64.S:45: Error: no such instruction: `ldp x23,x24,[sp],'
gcc_arm64.S:46: Error: no such instruction: `ldp x21,x22,[sp],'
gcc_arm64.S:47: Error: no such instruction: `ldp x19,x20,[sp],'

And then I tried to install gcc-arm-linux-gnueabihf package.
Then I ran the command env CGO_ENABLED=1 GOOS=linux GOARCH=arm64 CC=arm-linux-gnueabihf-gcc go build test.go
And I got the output below:

# runtime/cgo
In file included from _cgo_export.c:3:0:
cgo-gcc-export-header-prolog:25:14: error: size of array '_check_for_64_bit_pointer_matching_GoInt' is negative

What did you expect to see?

I want to cross compile successfully for ARM64/linux

What did you see instead?

I have posted the details of the error output above~

FrozenDueToAge NeedsInvestigation

All 5 comments

@ianlancetaylor is there an arm-specific owner I should point this to?

If you want to cross-compile with CGO_ENABLED=1, you must have a cross-compiler from your system to arm64-linux-gnu, and you must set the CC_FOR_TARGET environment variable to that cross-compiler. You are trying to build arm64 code with an x86 assembler, which can't work.

Closing because there is nothing we can change in the Go tools to fix this.

Hi Ian - I'm having a similar issue except I am defining CC_FOR_TARGET (although I've probably got it wrong)

CGO_ENABLED=1 GOOS=linux GOARCH=arm64 CC_FOR_TARGET=gcc-aarch64-linux-gnu go build -buildmode=plugin -o out-linux-arm64/module/module.so module/module.go

And I checked the corss tools are installed:

➜  sudo apt install gcc-aarch64-linux-gnu
Reading package lists... Done
Building dependency tree       
Reading state information... Done
gcc-aarch64-linux-gnu is already the newest version (4:8.3.0-1ubuntu1.2).

However I still get:

eginning build for linux - arm64
# runtime/cgo
gcc_arm64.S: Assembler messages:
gcc_arm64.S:27: Error: no such instruction: `stp x19,x20,[sp,'
gcc_arm64.S:28: Error: no such instruction: `stp x21,x22,[sp,'
gcc_arm64.S:29: Error: no such instruction: `stp x23,x24,[sp,'
gcc_arm64.S:30: Error: no such instruction: `stp x25,x26,[sp,'
gcc_arm64.S:31: Error: no such instruction: `stp x27,x28,[sp,'
gcc_arm64.S:32: Error: no such instruction: `stp x29,x30,[sp,'
gcc_arm64.S:33: Error: too many memory references for `mov'
gcc_arm64.S:35: Error: too many memory references for `mov'
gcc_arm64.S:36: Error: too many memory references for `mov'
gcc_arm64.S:37: Error: too many memory references for `mov'
gcc_arm64.S:39: Error: no such instruction: `blr x20'
gcc_arm64.S:40: Error: no such instruction: `blr x19'
gcc_arm64.S:42: Error: no such instruction: `ldp x29,x30,[sp],'
gcc_arm64.S:43: Error: no such instruction: `ldp x27,x28,[sp],'
gcc_arm64.S:44: Error: no such instruction: `ldp x25,x26,[sp],'
gcc_arm64.S:45: Error: no such instruction: `ldp x23,x24,[sp],'
gcc_arm64.S:46: Error: no such instruction: `ldp x21,x22,[sp],'
gcc_arm64.S:47: Error: no such instruction: `ldp x19,x20,[sp],'

Any pointers would be much appreciated. Thanks

@Escalion This issue was closed almost a year ago. The best way to report a problem is to open a new issue, and the best way to ask for help is to use a forum. See https://golang.org/wiki/Questions. Thanks.

In your case my first guess would be that you didn't set the CC environment variable to the cross-compiler.

@ianlancetaylor - no problem I will remember this for next time. Adding CC=aarch64-linux-gnu-gcc resolved my issue. Thank you!

Was this page helpful?
0 / 5 - 0 ratings