2

I am trying to build a go code for "arm64" architecture from "amd64" using a arm64 based so. I get the below mentioned error when I build the go code with the shared library. Can someone please assist me on this?

Go Version: go1.10.3 linux/amd64

gcc version: gcc version 7.3.0 (Ubuntu 7.3.0-21ubuntu1~16.04)

go env:

GOARCH="arm64"
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
CGO_ENABLED="1"

Error: go build hello.go

# 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],'

Go Code:

package main
/*
#cgo CFLAGS: -I./cgolang/include
#cgo LDFLAGS: -L./cgolang/lib  -laxxxxxx
#include "axxxxxx.h"
*/
import "C"
import "fmt"

func main() {
fmt.Printf("hello, world\n")
}
adiN
  • 21
  • 1
  • 2
  • You need to show how you configured, and how the source files are being compiled. – jww Jul 23 '18 at 19:00

3 Answers3

3

I had a similar problem when I needed to build a static Go binary with cgo that would eventually run in an alpine container with arm64 architecture, but had to be built in a golang:alpine container with x86_64 architecture (I didn't have control over the CI/CD runner architecture).

I solved it like the answer from @jesse but wanted to include an example for completeness since I was confused about where to get the cross-compiler and which files in it to use. Here is an example of downloading/extracting the CC in the container user's home directory and using it in a go build based out of /usr/src/example that produces an executable named exampleApp.

wget -P ~ https://musl.cc/aarch64-linux-musl-cross.tgz
tar -xvf ~/aarch64-linux-musl-cross.tgz -C ~
cd /usr/src/example
GOOS=linux GOARCH=arm64 CGO_ENABLED=1 CC=~/aarch64-linux-musl-cross/bin/aarch64-linux-musl-gcc go build -o exampleApp -a -ldflags=-extldflags=-static . 

Running file on the exe output verified it was statically linked for aarch64. I was then able to run it in an arm64 alpine container elsewhere.

geniusburger
  • 408
  • 6
  • 17
0

I linked the math library like so #cgo LDFLAGS: -L./cgolang/lib -lpthread -lm

That worked for me. If your shared library is provided by a third party, please ask them to help you out

dirtyqwerty
  • 185
  • 2
  • 16
0
env CGO_ENABLED=1 GOOS=linux GOARCH=arm64 CC=/path/to/your/arm64/gcc go build hello.go

you could see this same issue:https://github.com/golang/go/issues/8161

Jesse
  • 43
  • 5