golang / go

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

cmd/link: ld: B/BL out of range -134840372 (max +/-128MB) from 0x10809F954 #67854

Open d-bytebase opened 1 month ago

d-bytebase commented 1 month ago

Go version

1.22.4

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/danny/Library/Caches/go-build'
GOENV='/Users/danny/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/danny/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/danny/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.22.4/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.22.4/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.22.4'
GCCGO='gccgo'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/Users/danny/src/bytebase/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/2m/s9rrpxps5rl00_wnvy3b2jd40000gn/T/go-build3442076162=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

https://github.com/bytebase/bytebase

build command: CGO_ENABLED=1 go build -o ./.air/bytebase ./backend/bin/server/main.go

What did you see happen?

/opt/homebrew/Cellar/go/1.22.4/libexec/pkg/tool/darwin_arm64/link: running cc failed: exit status 1 ld: warning: -bind_at_load is deprecated on macOS ld: B/BL out of range -134840372 (max +/-128MB) from 0x10809F954 ('_runtime.cmpstring.island' from 'branch-islands-file') to 0x100007920 ('_runtime.cmpstring' from '/private/var/folders/2m/s9rrpxps5rl00_wnvy3b2jd40000gn/T/go-link-1336181017/go.o') final section layout: PAGEZERO addr=0x00000000, size=0x100000000, fileOffset=0x00000000 TEXT addr=0x100000000, size=0x11640000, fileOffset=0x00000000 text addr=0x100002ff0, size=0x082b982c, fileOffset=0x00002ff0 stubs addr=0x1082bc81c, size=0x00000810, fileOffset=0x082bc81c rodata addr=0x1082bd040, size=0x0929ddf0, fileOffset=0x082bd040 cstring addr=0x11155ae30, size=0x000363a7, fileOffset=0x1155ae30 const addr=0x111591200, size=0x000a71b8, fileOffset=0x11591200 unwind_info addr=0x1116383b8, size=0x00003d40, fileOffset=0x116383b8 eh_frame addr=0x11163c0f8, size=0x000000b8, fileOffset=0x1163c0f8 __DATA_CONST addr=0x111640000, size=0x0f640000, fileOffset=0x11640000 got addr=0x111640000, size=0x00000948, fileOffset=0x11640000 const addr=0x111640948, size=0x00039678, fileOffset=0x11640948 cfstring addr=0x111679fc0, size=0x000000e0, fileOffset=0x11679fc0 rodata addr=0x11167a0a0, size=0x027eb4d0, fileOffset=0x1167a0a0 typelink addr=0x113e65580, size=0x00050d44, fileOffset=0x13e65580 itablink addr=0x113eb62e0, size=0x0003a7c8, fileOffset=0x13eb62e0 gopclntab addr=0x113ef0ac0, size=0x0cd8c250, fileOffset=0x13ef0ac0 DATA addr=0x120c80000, size=0x011f8000, fileOffset=0x20c80000 data addr=0x120c80000, size=0x006ad908, fileOffset=0x20c80000 go_buildinfo addr=0x12132d910, size=0x000069c0, fileOffset=0x2132d910 noptrdata addr=0x1213342e0, size=0x009145c0, fileOffset=0x213342e0 thread_vars addr=0x121c488a0, size=0x00000630, fileOffset=0x21c488a0 thread_ptrs addr=0x121c48ed0, size=0x00000138, fileOffset=0x21c48ed0 thread_data addr=0x121c49008, size=0x00000108, fileOffset=0x21c49008 __thread_bss addr=0x121c49110, size=0x00000618, fileOffset=0x21c49110 bss addr=0x121c49740, size=0x0020c74c, fileOffset=0x00000000 noptrbss addr=0x121e55ea0, size=0x000201b0, fileOffset=0x00000000 common addr=0x121e76050, size=0x00000070, fileOffset=0x00000000 __LINKEDIT addr=0x121e78000, size=0x0db24000, fileOffset=0x21c4c000 clang: error: linker command failed with exit code 1 (use -v to see invocation)

What did you expect to see?

build success. I think this is MacOS only issue related to linker and starting since 1.22.

related issue: https://github.com/golang/go/issues/66993

d-bytebase commented 1 month ago

Hi @cherrymui @randall77 would you help take a look? Thanks!

I feel like it's a bug in MacOS linker. We can workaround the issue with -ldflags="-extldflags='-ld64'", however, not sure what the long-term solution is.

randall77 commented 1 month ago

It does not fail for me.

What does /usr/bin/xcodebuild -version print? For me it is

Xcode 15.2
Build version 15C500b

The error printed is strange. arm64 has a branch limit of +/- 32MB, not +/- 128MB. Also, the branch is between two locations in the text section, which has presumably already been resolved by the Go linker. The external linker shouldn't have to process any relocations for such a branch.

d-bytebase commented 1 month ago

Thanks. The link failures were not consistent with low chance to succeed. We could run 'go clean -cache' and reboot the laptop to get a more consistent failure.

% /usr/bin/xcodebuild -version Xcode 15.4 Build version 15F31d

% clang --version Apple clang version 15.0.0 (clang-1500.3.9.4) Target: arm64-apple-darwin23.5.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

randall77 commented 1 month ago

So the failure doesn't happen consistently? That is even more strange. The Go tools are carefully designed to generate the exact same artifacts each time.

d-bytebase commented 1 month ago

Yup, +1 for Go tool consistency. We have different Xcode version though. I wonder if it's using some cached objects or it's a bug in Xcode toolkit instead of Go, but Go triggers it somehow. The behavior is quite consistent if we clean all the cache and reboot the laptop.

cherrymui commented 1 month ago

I cannot reproduce this with

Xcode 15.4
Build version 15F31d

It builds successfully for me.

We can workaround the issue with -ldflags="-extldflags='-ld64'"

As it works with the old Apple linker, it might be a bug in the new Apple linker. The .island symbol looks like a trampoline generated by the Apple linker. It is unclear to me why it wants to generate such a symbol at that address.

(ARM64 B/BL instruction branch limit is actually +/- 128MB. It takes 26 bits off offset, adding two 0s at the end, as the an instruction address is always 4-byte aligned.)

gabyhelp commented 1 month ago

Similar Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

gopherbot commented 2 weeks ago

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

vektah commented 2 weeks ago

I'm also seeing this issue with go 1.22.4 and 1.22.5, xcode 15.3.0.0.1.1708646388, M2

ld: B/BL out of range -154140372 (max +/-128MB) from 0x1093089D4 ('_runtime.memequal.island' from 'branch-islands-file') to 0x100008B00 ('_runtime.memequal' from '/private/var/folders/bx/qq1wvp2n1ms609wzzgg0rm5m0000gn/T/go-link-274490021/go.o')
cherrymui commented 2 weeks ago

@vektah could you share what program you are building? Thanks.

alexBoldt95 commented 1 week ago

@d-bytebase Does the issue clear if you rebase your working branch or check out another branch?

vektah commented 1 week ago

@vektah could you share what program you are building? Thanks.

Its when running the test case for an internal service, sorry.

strideynet commented 1 week ago

I encounter this when compiling https://github.com/gravitational/teleport . The behaviour seems a little odd, it'll be fine sometimes but once I've hit the failure, it'll repeat until I reboot. At least a few members of the team here are encountering this.