Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ggeh opened this issue Nov 27, 2018 · 5 comments
Closed

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

ggeh opened this issue Nov 27, 2018 · 5 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@ggeh
Copy link

ggeh commented Nov 27, 2018

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~

@andybons andybons changed the title cross compile with cgo for target ARM64/linux fails cgo: cross compile with cgo for target ARM64/linux fails Nov 27, 2018
@andybons andybons changed the title cgo: cross compile with cgo for target ARM64/linux fails runtime/cgo: cross compile with cgo for target ARM64/linux fails Nov 27, 2018
@andybons andybons added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 27, 2018
@andybons
Copy link
Member

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

@andybons andybons added this to the Unplanned milestone Nov 27, 2018
@ianlancetaylor
Copy link
Contributor

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.

@Escalion
Copy link

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

@ianlancetaylor
Copy link
Contributor

@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.

@Escalion
Copy link

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

@golang golang locked and limited conversation to collaborators Oct 28, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants