cmd/link: cgo cross linking using the clang --target flag creates crashing programs #73406
Labels
BugReport
Issues describing a possible bug in the Go implementation.
compiler/runtime
Issues related to the Go compiler and/or runtime.
NeedsFix
The path to resolution is known, but the work has not been done.
Milestone
Go version
go version go1.24.2 linux/amd64
Output of
go env
in your module/workspace:What did you do?
Attempted to cross-compile a cgo binary from amd64 to arm64 using a private LLVM/Clang-based toolchain and sysroot. I was able to reproduce this from arm64 crossing to amd64 as well.
Minimal example (which doesn't run in the playground due to needing cgo): https://go.dev/play/p/a0qGj4Vakf-
Built with:
The output binary can be run on an ARM host, or via
qemu
with something like:What did you see happen?
The
go tool link
output from the abovego build
command contained a linker invocation like this:(The "host link" outputs below are particularly long, and is more readable in an editor)
If I use the same compiler targeting the host architecture, using a sysroot compiled for it (in this case amd64/x86_64), the output is different:
Both commands succeed, and produce a binary.
These flag differences are numerous and consequential: running the cross-compiled binary (in this particular case, using qemu) crashes at startup like this:
The binary fails similarly on an actual ARM host as well. When I investigated this, it seems that the critical difference is the
-no-pie
flag in the second (amd64, same as the build host) output, as adding-buildmode pie
to the cross-compilinggo build
prevents the binary from crashing like this.What did you expect to see?
The flags in the two "host link" invocations above should be more similar (aside from arch-specific flags), and the cross-compiled program should not crash as mentioned above when run in an emulator or a system with that architecture.
The text was updated successfully, but these errors were encountered: