Skip to content
New issue

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

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

Already on GitHub? # to your account

Error building a simple program with GOOS=darwin GOARCH=arm64 environment variables #39033

Closed
ghost opened this issue May 12, 2020 · 1 comment

Comments

@ghost
Copy link

ghost commented May 12, 2020

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

david@iMac example % go version
go version go1.14.2 darwin/amd64
david@iMac example %

Does this issue reproduce with the latest release?

Yes

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

go env Output
david@iMac example % go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/david/Library/Caches/go-build"
GOENV="/Users/david/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/david/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.14.2_1/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.14.2_1/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/vr/c1zlgjd13fndm8vp_nqrtlf00000gn/T/go-build964299654=/tmp/go-build -gno-record-gcc-switches -fno-common"
david@iMac example %

What did you do?

I've tried to build a go program for different os/architectures

This is the program:

david@iMac example % cat main.go
package main

import "fmt"

func main() {
	fmt.Println("Hello world")
}
david@iMac example %

This is a build for linux os and arm64 arch

david@iMac example % GOOS=linux GOARCH=arm64 go build -ldflags=-v main.go
# command-line-arguments
HEADER = -H5 -T0x11000 -R0x10000
deadcode
symsize = 0
pclntab=453982 bytes, funcdata total 109667 bytes
symsize = 2352
symsize = 62088
49010 symbols
67772 liveness data
david@iMac example % file main
main: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, Go BuildID=1EuGVMlN4cDnr8BlGzcV/X7K-Pkf9r2YwkiBrKP6J/8u7GPK0eoIKFAW43GX6K/pHAjSmKoxybfPCmtW00z, not stripped
david@iMac example %

Everything went fine.

Same build, but trying different OS (darwin) with same cpu set (arm64)

david@iMac example % GOOS=darwin GOARCH=arm64 go build -ldflags=-v main.go
# command-line-arguments
HEADER = -H1 -T0x2000 -R0x1000
deadcode
symsize = 0
pclntab=487711 bytes, funcdata total 119215 bytes
symsize = 0
symsize = 0
host link: "clang" "-o" "/var/folders/vr/c1zlgjd13fndm8vp_nqrtlf00000gn/T/go-build581065135/b001/exe/a.out" "-Qunused-arguments" "/var/folders/vr/c1zlgjd13fndm8vp_nqrtlf00000gn/T/go-link-251820392/go.o" "-no-pie"
/usr/local/Cellar/go/1.14.2_1/libexec/pkg/tool/darwin_amd64/link: running clang failed: exit status 1
ld: warning: ignoring file /var/folders/vr/c1zlgjd13fndm8vp_nqrtlf00000gn/T/go-link-251820392/go.o, building for macOS-x86_64 but attempting to link with file built for unknown-arm64
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

david@iMac example %

What did you expect to see?

A successfully built

What did you see instead?

A linker error

host link: "clang" "-o" "/var/folders/vr/c1zlgjd13fndm8vp_nqrtlf00000gn/T/go-build581065135/b001/exe/a.out" "-Qunused-arguments" "/var/folders/vr/c1zlgjd13fndm8vp_nqrtlf00000gn/T/go-link-251820392/go.o" "-no-pie"
/usr/local/Cellar/go/1.14.2_1/libexec/pkg/tool/darwin_amd64/link: running clang failed: exit status 1
ld: warning: ignoring file /var/folders/vr/c1zlgjd13fndm8vp_nqrtlf00000gn/T/go-link-251820392/go.o, building for macOS-x86_64 but attempting to link with file built for unknown-arm64
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
@ianlancetaylor
Copy link
Member

The problem is that darwin-arm64 does not support internal linking, which means that Go has to invoke the C linker, which means that you need to have a cross-linker from your host system to darwin-arm64. The Go linker is invoking the C linker, but it's not a cross-linker, so it fails.

At present we assume that darwin-arm64 is an iPhone, so in general it only works for building for a mobile device. See #38485.

I'm going to close this issue since there isn't much we can do at present, but if you want to repurpose this in some way that would be OK.

@golang golang locked and limited conversation to collaborators May 12, 2021
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Projects
None yet
Development

No branches or pull requests

2 participants