From 5d3fbceff0267c3951523bc9e58c50fed20e1a76 Mon Sep 17 00:00:00 2001 From: Cory Snider Date: Thu, 29 Aug 2024 16:52:43 -0400 Subject: [PATCH] hack/make/.binary: set CGO_LDFLAGS=-latomic for arm/v5 cross-compiling for arm/v5 was failing; #56 84.12 /usr/bin/arm-linux-gnueabi-clang -marm -o $WORK/b001/exe/a.out -Wl,--export-dynamic-symbol=_cgo_panic -Wl,--export-dynamic-symbol=_cgo_topofstack -Wl,--export-dynamic-symbol=crosscall2 -Qunused-arguments -Wl,--compress-debug-sections=zlib /tmp/go-link-759578347/go.o /tmp/go-link-759578347/000000.o /tmp/go-link-759578347/000001.o /tmp/go-link-759578347/000002.o /tmp/go-link-759578347/000003.o /tmp/go-link-759578347/000004.o /tmp/go-link-759578347/000005.o /tmp/go-link-759578347/000006.o /tmp/go-link-759578347/000007.o /tmp/go-link-759578347/000008.o /tmp/go-link-759578347/000009.o /tmp/go-link-759578347/000010.o /tmp/go-link-759578347/000011.o /tmp/go-link-759578347/000012.o /tmp/go-link-759578347/000013.o /tmp/go-link-759578347/000014.o /tmp/go-link-759578347/000015.o /tmp/go-link-759578347/000016.o /tmp/go-link-759578347/000017.o /tmp/go-link-759578347/000018.o -O2 -g -O2 -g -O2 -g -lpthread -O2 -g -no-pie -static #56 84.12 ld.lld: error: undefined symbol: __atomic_load_4 #56 84.12 >>> referenced by gcc_libinit.c #56 84.12 >>> /tmp/go-link-759578347/000009.o:(_cgo_wait_runtime_init_done) #56 84.12 >>> referenced by gcc_libinit.c #56 84.12 >>> /tmp/go-link-759578347/000009.o:(_cgo_wait_runtime_init_done) #56 84.12 >>> referenced by gcc_libinit.c #56 84.12 >>> /tmp/go-link-759578347/000009.o:(_cgo_wait_runtime_init_done) #56 84.12 >>> referenced 2 more times #56 84.12 #56 84.12 ld.lld: error: undefined symbol: __atomic_store_4 #56 84.12 >>> referenced by gcc_libinit.c #56 84.12 >>> /tmp/go-link-759578347/000009.o:(_cgo_wait_runtime_init_done) #56 84.12 >>> referenced by gcc_libinit.c #56 84.12 >>> /tmp/go-link-759578347/000009.o:(x_cgo_notify_runtime_init_done) #56 84.12 >>> referenced by gcc_libinit.c #56 84.12 >>> /tmp/go-link-759578347/000009.o:(x_cgo_set_context_function) #56 84.12 clang: error: linker command failed with exit code 1 (use -v to see invocation) From discussion on GitHub; https://github.com/moby/moby/pull/46982#issuecomment-2206992611 The arm/v5 build failure looks to be due to libatomic not being included in the link. For reasons probably buried in mailing list archives, [gcc](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81358) and clang don't bother to implicitly auto-link libatomic. This is not a big deal on many modern platforms with atomic intrinsics as the compiler generates inline instruction sequences, avoiding any libcalls into libatomic. ARMv5 is not one of those platforms: all atomic operations require a libcall. In theory, adding `CGO_LDFLAGS=-latomic` should fix arm/v5 builds. While it could be argued that cgo should automatically link against libatomic in the same way that it automatically links against libpthread, the Go maintainers would have a valid counter-argument that it should be the C toolchain's responsibility to link against libatomic automatically, just like it does with libgcc or compiler-rt. Co-authored-by: Sebastiaan van Stijn Signed-off-by: Cory Snider (cherry picked from commit 4cd5c2b6435a4cc1afd01c01b85f01dfe9aa4da3) Signed-off-by: Cory Snider --- hack/make/.binary | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/hack/make/.binary b/hack/make/.binary index d1dbccc51b..d79a18eada 100644 --- a/hack/make/.binary +++ b/hack/make/.binary @@ -80,6 +80,10 @@ source "${MAKEDIR}/.go-autogen" # we can set the correct option manually. CGO_CFLAGS+=" -Wno-atomic-alignment" export CGO_CFLAGS + + # Make sure libatomic is included on arm/v5, because clang does not auto-link it. + # see https://github.com/moby/moby/pull/46982#issuecomment-2206992611 + export CGO_LDFLAGS="-latomic" fi echo "Building $([ "$DOCKER_STATIC" = "1" ] && echo "static" || echo "dynamic") $DEST/$BINARY_FULLNAME ($PLATFORM_NAME)..."