golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.01k stars 17.54k forks source link

cgo-gcc-export-header-prolog:31:14: error: size of array ‘_check_for_64_bit_pointer_matching_GoInt’ is negative #64190

Closed shaneHowearth closed 10 months ago

shaneHowearth commented 10 months ago

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

$ go version
go version go1.21.4 linux/arm64

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
GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/home/shane/.cache/go-build'
GOENV='/home/shane/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/shane/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/shane/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_arm64'
GOVCS=''
GOVERSION='go1.21.4'
GCCGO='gccgo'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'

Note: I am running this on a Raspberry Pi 4 - aarch64

$ uname -rms
Linux 6.1.21-v8+ aarch64

What did you do?

I first noticed this when trying to install linters and such with vim-go (:GoInstallBinaries).

Looking through older issues I found: https://github.com/golang/go/issues/43842 Which advised something to the effect of

 CGO_CFLAGS="-arch aarch64" CGO_LDFLAGS="-arch aarch64" vi cmd/main.go
or
CGO_CFLAGS="-arch arm64" CGO_LDFLAGS="-arch arm64" vi cmd/main.go
or
CGO_CFLAGS="-march=arm64" CGO_LDFLAGS="-march=arm64" vi cmd/main.go

A troubleshooting step was

go build -x cmd/main.go
WORK=/tmp/go-build2011245217
mkdir -p $WORK/b104/
cd /usr/local/go/src/runtime/cgo
TERM='dumb' CGO_LDFLAGS='"-O2" "-g" "-lpthread"' /usr/local/go/pkg/tool/linux_arm64/cgo -objdir $WORK/b104/ -importpath runtime/cgo -import_runtime_cgo=false -import_syscall=false -- -I $WORK/b104/ -O2 -g -Wall -Werror -fno-stack-protector ./cgo.go
cd $WORK
gcc -I /usr/local/go/src/runtime/cgo -fPIC -pthread -fmessage-length=0 -frandom-seed=1 -O2 -g -c -x c - -o /dev/null || true
cd $WORK/b104
TERM='dumb' gcc -I /usr/local/go/src/runtime/cgo -fPIC -pthread -fmessage-length=0 -I ./ -O2 -g -Wall -Werror -fno-stack-protector -frandom-seed=SkxYEAb1VH-1TE0Vqhtg -o ./_x001.o -c _cgo_export.c
# runtime/cgo
In file included from _cgo_export.c:4:
cgo-gcc-export-header-prolog:31:14: error: size of array ‘_check_for_64_bit_pointer_matching_GoInt’ is negative

or

CGO_CFLAGS="-march=arm64" CGO_LDFLAGS="-march=arm64" go build -x cmd/main.go
WORK=/tmp/go-build2000993112
mkdir -p $WORK/b104/
cd /usr/local/go/src/runtime/cgo
TERM='dumb' CGO_LDFLAGS='"-march=arm64" "-lpthread"' /usr/local/go/pkg/tool/linux_arm64/cgo -objdir $WORK/b104/ -importpath runtime/cgo -import_runtime_cgo=false -import_syscall=false -- -I $WORK/b104/ -march=arm64 -Wall -Werror -fno-stack-protector ./cgo.go
# runtime/cgo
gcc: error: unrecognized -march target: arm64
gcc: note: valid arguments are: armv4 armv4t armv5t armv5te armv5tej armv6 armv6j armv6k armv6z armv6kz armv6zk armv6t2 armv6-m armv6s-m armv7 armv7-a armv7ve armv7-r armv7-m armv7e-m armv8-a armv8.1-a armv8.2-a armv8.3-a armv8.4-a armv8.5-a armv8.6-a armv8-m.base armv8-m.main armv8-r armv8.1-m.main iwmmxt iwmmxt2 native; did you mean 'armv4'?
gcc: error: missing argument to '-march='

or

CGO_CFLAGS="-march=armv4" CGO_LDFLAGS="-march
 armv4" go build -x cmd/main.go
WORK=/tmp/go-build861798144
mkdir -p $WORK/b104/
cd /usr/local/go/src/runtime/cgo
TERM='dumb' CGO_LDFLAGS='"-march" "armv4" "-lpthread"' /usr/local/go/pkg/tool/linux_arm64/cgo -objdir $WORK/b104/ -importpath runtime/cgo -import_runtime_cgo=false -import_syscall=false -- -I $WORK/b104/ -march=armv4 -Wall -Werror -fno-stack-protector ./cgo.go
cd $WORK/b104
TERM='dumb' gcc -I /usr/local/go/src/runtime/cgo -fPIC -pthread -fmessage-length=0 -I ./ -march=armv4 -Wall -Werror -fno-stack-protector -frandom-seed=sUu01rIiyDxBEvjqwcuA -o ./_x001.o -c _cgo_export.c
# runtime/cgo
In file included from _cgo_export.c:4:
cgo-gcc-export-header-prolog:31:14: error: size of array ‘_check_for_64_bit_pointer_matching_GoInt’ is negative

For a very simple piece of code

$ cat foo/main.go
package main

import "fmt"

func main() {
        fmt.Println("vim-go")
}

Things are fine:

$ go run foo/main.go
vim-go

But for

$ cat cmd/main.go
package main

import "net/http"

func main() {

        r := chi.NewRouter()
        r.Use(middleware.Logger)
        r.Get("/", func(w http.ResponseWriter, r *http.Request) {
                w.Write([]byte("welcome"))

        })
        http.ListenAndServe(":3000", r)
}

I get

$ go run cmd/main.go
# runtime/cgo
In file included from _cgo_export.c:4:
cgo-gcc-export-header-prolog:31:14: error: size of array ‘_check_for_64_bit_pointer_matching_GoInt’ is negative
shaneHowearth commented 10 months ago

I appreciate that me using a Raspberry Pi might be the core issue here, but would love to be able to use the Pi as my dev machine (albeit with limitations), rather than ssh'ing from my Pi into a desktop to do the work.

mauri870 commented 10 months ago

Can you try with CGO_CFLAGS="-arch arm64" CGO_LDFLAGS="-arch arm64" go build main.go?

Might be a duplicate of https://github.com/golang/go/issues/43842

Edit: NVM, just read you tried that, my bad

mauri870 commented 10 months ago

Also https://github.com/golang/go/issues/38693

shaneHowearth commented 10 months ago

That appears to be the fix (I had missed this in one of the comments)

GOHOSTARCH=arm GOARCH=arm go run cmd/main.go
# command-line-arguments
cmd/main.go:7:7: undefined: chi
cmd/main.go:8:8: undefined: middleware

Hmm, even those are specified in the go env output... hmmm

Ok, so the issue is that in go env those variables are set:

GOARCH='arm64'
GOHOSTARCH='arm64'

And the fix is to have them as

GOARCH='arm'
GOHOSTARCH='arm'

Should I raise a bug for that?