I am seeing the following error when cross-compiling go source on ARM with 
-buildmode=pie. Despite the error (or is it a warning?), the compilation 
succeeds
(i.e. go build exits with 0 and the binary is created). Adding CGO_ENABLED=1
(since this is cross-compilation, cgo is disabled by default) eliminates 
the warning.

Please see a complete repro below. This happens on the following linux 
platforms:
 - armel aka arm v6;
 - armhf aka arm v7;
 - riscv64;
 - s390x.

It does NOT happen
 - natively (linux/amd64);
 - on arm64.
- on ppc64le.

I haven't tried anything else (e.g. mips or 386), nor have I checked that 
the resulting binaries work.

My questions are:
1. If "loadinternal: cannot find runtime/cgo" is an error, why go build 
creates the binary and exits with code of 0? This looks like a bug.
2. Does -buildmode=pie require enabling cgo?

Here's a repro using docker or podman (I guess you can do the same without 
Docker on a Debian machine).

--------
mkdir hello
cd hello
# Create the Dockerfile.
cat << _EOF_ > Dockerfile
FROM    golang:1.18.0-bullseye
RUN    apt-get -qq update \
    && apt-get -qq -y install gcc-arm-linux-gnueabi libc-dev-armel-cross
ENV    HOST=arm-linux-gnueabi GOARCH=arm GOARM=6 CC=arm-linux-gnueabi-gcc
EOF

# Create the source.
cat << _EOF_ > hello.go
package main

func main() {
    println("hello")
}
_EOF_

# Create go.mod.
go mod init hello && go mod tidy

# Build a docker image.
docker build --tag hello .
--------

Now, this is what I see when building normally:

$ docker run -it --rm -v .:/hello hello
root@42d4fe21de73:/go# cd /hello
root@42d4fe21de73:/hello# go build .
root@42d4fe21de73:/hello# echo $?
0
root@42d4fe21de73:/hello# ls -l hello
-rwxr-xr-x. 1 root root 1218776 Apr  1 20:25 hello

Now let's add -buildmode=pie:

root@42d4fe21de73:/hello# rm hello  
root@42d4fe21de73:/hello# go build -buildmode=pie .
# hello
loadinternal: cannot find runtime/cgo
root@42d4fe21de73:/hello# echo $?
0
root@42d4fe21de73:/hello# ls -l hello
-rwxr-xr-x. 1 root root 1121328 Apr  1 20:26 hello

Now, let's enable CGO (since it's disabled by default when cross-compiling):

root@42d4fe21de73:/hello# CGO_ENABLED=1 go build -buildmode=pie .
root@42d4fe21de73:/hello# ls -l hello
-rwxr-xr-x. 1 root root 1143380 Apr  1 20:27 hello

System info:

root@42d4fe21de73:/hello# env
HOSTNAME=42d4fe21de73
PWD=/hello
GOARCH=arm
container=podman
GOARM=6
HOME=/root
GOLANG_VERSION=1.18
TERM=xterm
HOST=arm-linux-gnueabi
SHLVL=1
PATH=/go/bin:/usr/local/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
CC=arm-linux-gnueabi-gcc
OLDPWD=/go
GOPATH=/go
_=/usr/bin/env
root@42d4fe21de73:/hello# go env
GO111MODULE=""
GOARCH="arm"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct";
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18"
GCCGO="gccgo"
GOARM="6"
AR="ar"
CC="arm-linux-gnueabi-gcc"
CXX="g++"
CGO_ENABLED="0"
GOMOD="/hello/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -marm -fmessage-length=0 
-fdebug-prefix-map=/tmp/go-build3607723978=/tmp/go-build 
-gno-record-gcc-switches"
root@42d4fe21de73:/hello# $CC --version
arm-linux-gnueabi-gcc (Debian 10.2.1-6) 10.2.1 20210110
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

root@42d4fe21de73:/hello# go version
go version go1.18 linux/amd64
root@42d4fe21de73:/hello# 

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/58608533-0196-41e8-bb45-369402194943n%40googlegroups.com.

Reply via email to