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

cmd/link: cgo fails to link on macos 13 beta (arm64) #53372

Closed
ptxmac opened this issue Jun 14, 2022 · 9 comments
Closed

cmd/link: cgo fails to link on macos 13 beta (arm64) #53372

ptxmac opened this issue Jun 14, 2022 · 9 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Darwin

Comments

@ptxmac
Copy link

ptxmac commented Jun 14, 2022

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

$ go version
go version go1.18.3 darwin/arm64

Does this issue reproduce with the latest release?

Yes

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

tl;dr: 13.0 Beta (22A5266r) on Apple silicon (arm64)

go env Output
$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/ptx/Library/Caches/go-build"
GOENV="/Users/ptx/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/ptx/go/pkg/mod"
GONOPROXY=
GONOSUMDB=
GOOS="darwin"
GOPATH="/Users/ptx/go"
GOPRIVATE=
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.18.3"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
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 -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/xf/5whnbpls5d31z5dyrpdf_zh80000gn/T/go-build3396597166=/tmp/go-build -gno-record-gcc-switches -fno-common"
GOROOT/bin/go version: go version go1.18.3 darwin/arm64
GOROOT/bin/go tool compile -V: compile version go1.18.3
uname -v: Darwin Kernel Version 22.0.0: Tue May 24 20:30:32 PDT 2022; root:xnu-8792.0.50.111.3~5/RELEASE_ARM64_T8103
ProductName:	macOS
ProductVersion:	13.0
BuildVersion:	22A5266r
lldb --version: lldb-1400.0.17.202
Apple Swift version 5.7 (swiftlang-5.7.0.113.202 clang-1400.0.16.2)

What did you do?

Attempt to compile the following program:

package main

// #include <stdio.h>
import "C"

func main() {
	C.putchar('a')
}

What did you expect to see?

This should Just-Work™ and create a valid arm64 binary

What did you see instead?

A long list of linker warnings followed by an error:

$ go build -v main.go
# command-line-arguments
/usr/local/go/pkg/tool/darwin_arm64/link: running clang failed: exit status 1
ld: warning: pointer not aligned at address 0x100090467 ('_type.*' + 1127 from /var/folders/xf/5whnbpls5d31z5dyrpdf_zh80000gn/T/go-link-1884331981/go.o)
  ...snip around 2000 lines of minor variations of this warning
ld: warning: pointer not aligned at address 0x10009CA17 ('_type.*' + 51735 from /var/folders/xf/5whnbpls5d31z5dyrpdf_zh80000gn/T/go-link-1884331981/go.o)
ld: unaligned pointer(s) for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
@cherrymui
Copy link
Member

cherrymui commented Jun 14, 2022

Thanks for reporting. I'll see if I can get macOS 13 beta on my machine and try this.

Please note that this is the beta version of the OS and things may change before the final release. Thanks.

@ptxmac
Copy link
Author

ptxmac commented Jun 14, 2022

Fully aware of the pain of using beta software 😄

Happy to test nightly builds of go if there's any commits that might solve this problem

@cherrymui
Copy link
Member

cherrymui commented Jun 14, 2022

I couldn't find how to install macOS 13 beta despite that I have enrolled the Apple Beta Program. However, I'm able to reproduce the issue by using Xcode 14 beta on macOS 12.5 beta. I see two issues here:

  • According to the man page (even the one from Xcode 14 beta), the unaligned pointer check should be default to "suppress" (as we pass -arch arm64), but it is not. Even explicitly passing -unaligned_pointers suppress still couldn't suppress it. It looks to me this is broken in the system linker (ld64-816).

  • The address of the symbol _type.* is indeed weird. I see

0000000000070000 s _runtime.types
0000000000070001 s _type.*

for the go.o object file. It doesn't really affect program execution. But I think it is a good idea to just align that address.

I send a CL for the second part. With that the cgo program builds and runs fine.

@gopherbot
Copy link

gopherbot commented Jun 14, 2022

Change https://go.dev/cl/411912 mentions this issue: cmd/link: set alignment for carrier symbols

@ptxmac
Copy link
Author

ptxmac commented Jun 14, 2022

I'm able to reproduce the issue by using Xcode 14 beta on macOS 12.5 beta

Oh that's good news - then I should be able to use another version of xcode.

@seankhliao seankhliao changed the title cgo fails to link on macos 13 beta (arm64) cmd/link: cgo fails to link on macos 13 beta (arm64) Jun 15, 2022
@seankhliao seankhliao added OS-Darwin NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Jun 15, 2022
@cherrymui
Copy link
Member

cherrymui commented Jun 15, 2022

@ptxmac could you try if Go tip (which now includes the CL) works on the new OS with the new Xcode? Thanks!

@ptxmac
Copy link
Author

ptxmac commented Jun 16, 2022

Just tested with

$ gotip version
go version devel go1.19-ecc268a Thu Jun 16 01:03:59 2022 +0000 darwin/arm64

Just perfect! - Thanks!

@gopherbot
Copy link

gopherbot commented Jun 16, 2022

Change https://go.dev/cl/412734 mentions this issue: cmd/link: consider alignment in carrier symbol size calculation

gopherbot pushed a commit that referenced this issue Jun 16, 2022
Currently, when we calculate the size of a carrier symbol, we use
the previous symbol's end address as the start. But the symbol
actually starts after applying the alignment. Do this in the
size calculation.

Should fix AIX build.

Updates #53372.

Change-Id: I17942b1fe8027dce12b78c8e8c80ea6cebcee240
Reviewed-on: https://go-review.googlesource.com/c/go/+/412734
Run-TryBot: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
@nikitagriaznov
Copy link

nikitagriaznov commented Jul 7, 2022

It looks like beta 3 update solved this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Darwin
Projects
None yet
Development

No branches or pull requests

5 participants