Files
patches/glibc/glibc-2.43-use-compiler-reported-libgcc.patch
2026-05-26 02:08:47 -05:00

316 lines
14 KiB
Diff

diff -ruN a/Makeconfig b/Makeconfig
--- a/Makeconfig 2026-01-23 14:54:00.000000000 -0600
+++ b/Makeconfig 2026-03-15 19:54:42.332792487 -0500
@@ -714,33 +714,109 @@
else
libunwind = -lunwind
endif
-libgcc_eh := -Wl,--as-needed -lgcc_s $(libunwind) -Wl,--no-as-needed
+# Preserve the configured target selection when asking the compiler for
+# support libraries and CRT objects. For Clang, prefer
+# compiler-rt builtins if available because libgcc lookups may resolve to a
+# host archive.
+cc-runtime-flags := $(filter-out -I%,$(CFLAGS)) $(sysdep-LDFLAGS) $(LDFLAGS)
+cc-runtime-target := $(shell $(CC) $(cc-runtime-flags) -print-target-triple 2>/dev/null)
+cc-clang-resource-dir := $(shell $(CC) $(cc-runtime-flags) --print-resource-dir 2>/dev/null)
+cc-clang-rtlib-flags := $(shell \
+ if test "$(have-test-clang)" = yes; then \
+ for candidate in i686-linux-gnu i686-unknown-linux-gnu i386-unknown-linux-gnu "$(cc-runtime-target)"; do \
+ if test -n "$$candidate"; then \
+ rtlib=$$($(CC) $(cc-runtime-flags) --target=$$candidate \
+ --rtlib=compiler-rt --print-libgcc-file-name 2>/dev/null); \
+ if test "x$$rtlib" != x && test "x$$rtlib" != xlibgcc.a && test -r "$$rtlib"; then \
+ printf '%s' "--target=$$candidate --rtlib=compiler-rt"; \
+ exit 0; \
+ fi; \
+ fi; \
+ done; \
+ fi)
+cc-clang-crtbegin := $(shell \
+ if test "$(have-test-clang)" = yes; then \
+ for path in \
+ "$(cc-clang-resource-dir)/lib/i686-linux-gnu/clang_rt.crtbegin-i386.o" \
+ "$(cc-clang-resource-dir)/lib/i686-unknown-linux-gnu/clang_rt.crtbegin-i386.o" \
+ "$(cc-clang-resource-dir)/lib/i386-unknown-linux-gnu/clang_rt.crtbegin-i386.o" \
+ "$(cc-clang-resource-dir)/lib/$(cc-runtime-target)/clang_rt.crtbegin-i386.o" \
+ "$(cc-clang-resource-dir)/lib/linux/clang_rt.crtbegin-i386.o"; do \
+ if test -r "$$path"; then \
+ printf '%s' "$$path"; \
+ exit 0; \
+ fi; \
+ done; \
+ fi)
+cc-clang-crtend := $(shell \
+ if test "$(have-test-clang)" = yes; then \
+ for path in \
+ "$(cc-clang-resource-dir)/lib/i686-linux-gnu/clang_rt.crtend-i386.o" \
+ "$(cc-clang-resource-dir)/lib/i686-unknown-linux-gnu/clang_rt.crtend-i386.o" \
+ "$(cc-clang-resource-dir)/lib/i386-unknown-linux-gnu/clang_rt.crtend-i386.o" \
+ "$(cc-clang-resource-dir)/lib/$(cc-runtime-target)/clang_rt.crtend-i386.o" \
+ "$(cc-clang-resource-dir)/lib/linux/clang_rt.crtend-i386.o"; do \
+ if test -r "$$path"; then \
+ printf '%s' "$$path"; \
+ exit 0; \
+ fi; \
+ done; \
+ fi)
+libgcc := $(shell \
+ lib=$$($(CC) $(cc-runtime-flags) --print-libgcc-file-name 2>/dev/null); \
+ if test "$(have-test-clang)" = yes; then \
+ rtlib=$$($(CC) $(cc-runtime-flags) $(cc-clang-rtlib-flags) \
+ --print-libgcc-file-name 2>/dev/null); \
+ if test "x$$rtlib" != x && test "x$$rtlib" != xlibgcc.a && test -r "$$rtlib"; then \
+ lib=$$rtlib; \
+ else \
+ for path in \
+ "$(cc-clang-resource-dir)/lib/i686-linux-gnu/libclang_rt.builtins.a" \
+ "$(cc-clang-resource-dir)/lib/i686-unknown-linux-gnu/libclang_rt.builtins.a" \
+ "$(cc-clang-resource-dir)/lib/i386-unknown-linux-gnu/libclang_rt.builtins.a" \
+ "$(cc-clang-resource-dir)/lib/$(cc-runtime-target)/libclang_rt.builtins.a" \
+ "$(cc-clang-resource-dir)/lib/linux/libclang_rt.builtins-i386.a"; do \
+ if test -r "$$path"; then \
+ lib=$$path; \
+ break; \
+ fi; \
+ done; \
+ fi; \
+ fi; \
+ printf '%s' "$$lib")
+# Do not mix Clang's compiler-rt builtins with GCC crtbegin/crtend
+# objects. If compiler-rt CRT objects are unavailable, leave these
+# empty and let the link proceed without the GCC startup fragments.
+cc-clang-use-rt := $(and $(filter yes,$(have-test-clang)),$(cc-clang-rtlib-flags))
+libgcc_eh_archive := $(shell $(CC) $(cc-runtime-flags) --print-file-name=libgcc_eh.a)
+libgcc_s := $(shell $(CC) $(cc-runtime-flags) --print-file-name=libgcc_s.so$(libgcc_s.so-version))
+libgcc_eh := -Wl,--as-needed $(libgcc_s) $(libunwind) -Wl,--no-as-needed
gnulib-arch =
-gnulib = -lgcc $(gnulib-arch)
-gnulib-tests := -lgcc $(libgcc_eh)
-gnulib-extralibdir = $(dir $(shell $(CC) -print-file-name=libgcc_s.so$(libgcc_s.so-version)))
+gnulib = $(libgcc) $(gnulib-arch)
+gnulib-tests := $(libgcc) $(libgcc_eh)
+gnulib-extralibdir = $(patsubst ./,,$(dir $(libgcc_s)))
static-gnulib-arch =
# By default, elf/static-stubs.o, instead of -lgcc_eh, is used to
# statically link programs. When --disable-shared is used, we use
# -lgcc_eh since elf/static-stubs.o isn't sufficient.
ifeq (yes,$(build-shared))
-static-gnulib = -lgcc $(static-gnulib-arch)
+static-gnulib = $(libgcc) $(static-gnulib-arch)
else
-static-gnulib = -lgcc -lgcc_eh $(static-gnulib-arch)
+static-gnulib = $(libgcc) $(libgcc_eh_archive) $(static-gnulib-arch)
endif
-static-gnulib-tests := -lgcc -lgcc_eh $(libunwind)
-libc.so-gnulib := -lgcc
+static-gnulib-tests := $(libgcc) $(libgcc_eh_archive) $(libunwind)
+libc.so-gnulib := $(libgcc)
endif
+preinit = $(addprefix $(csu-objpfx),crti.o)
+postinit = $(addprefix $(csu-objpfx),crtn.o)
-+prector = `$(CC) $(sysdep-LDFLAGS) --print-file-name=crtbegin.o`
-+postctor = `$(CC) $(sysdep-LDFLAGS) --print-file-name=crtend.o`
++prector = $(if $(cc-clang-use-rt),$(cc-clang-crtbegin),`$(CC) $(cc-runtime-flags) --print-file-name=crtbegin.o`)
++postctor = $(if $(cc-clang-use-rt),$(cc-clang-crtend),`$(CC) $(cc-runtime-flags) --print-file-name=crtend.o`)
# Variants of the two previous definitions for linking PIE programs.
-+prectorS = `$(CC) $(sysdep-LDFLAGS) --print-file-name=crtbeginS.o`
-+postctorS = `$(CC) $(sysdep-LDFLAGS) --print-file-name=crtendS.o`
++prectorS = $(if $(cc-clang-use-rt),$(cc-clang-crtbegin),`$(CC) $(cc-runtime-flags) --print-file-name=crtbeginS.o`)
++postctorS = $(if $(cc-clang-use-rt),$(cc-clang-crtend),`$(CC) $(cc-runtime-flags) --print-file-name=crtendS.o`)
# Variants of the two previous definitions for statically linking programs.
-static-prector = `$(CC) $(sysdep-LDFLAGS) --print-file-name=crtbeginT.o`
-static-postctor = `$(CC) $(sysdep-LDFLAGS) --print-file-name=crtend.o`
+static-prector = $(if $(cc-clang-use-rt),$(cc-clang-crtbegin),`$(CC) $(cc-runtime-flags) --print-file-name=crtbeginT.o`)
+static-postctor = $(if $(cc-clang-use-rt),$(cc-clang-crtend),`$(CC) $(cc-runtime-flags) --print-file-name=crtend.o`)
ifeq (yes,$(enable-static-pie))
# Static PIE must use PIE variants.
+prectorT = $(if $($(@F)-no-pie),$(static-prector),$(+prectorS))
diff -ruN a/Makerules b/Makerules
--- a/Makerules 2026-01-23 14:54:00.000000000 -0600
+++ b/Makerules 2026-03-15 19:54:16.435354197 -0500
@@ -543,7 +543,7 @@
$(call after-link,$@)
define build-shlib-helper
-$(LINK.o) -shared -static-libgcc -Wl,-O1 $(sysdep-LDFLAGS) \
+$(LINK.o) -shared $(if $(and $(filter yes,$(have-test-clang)),$(cc-clang-rtlib-flags)),-nostartfiles $(cc-clang-rtlib-flags),-static-libgcc) -Wl,-O1 $(sysdep-LDFLAGS) \
$(if $($(@F)-no-z-defs)$(no-z-defs),,-Wl,-z,defs) $(rtld-LDFLAGS) \
$(if $($(@F)-no-dt-relr),$(no-dt-relr-ldflag),$(dt-relr-ldflag)) \
$(extra-B-$(@F:lib%.so=%).so) -B$(csu-objpfx) \
@@ -555,11 +555,13 @@
define build-shlib
$(build-shlib-helper) -o $@ \
- $(csu-objpfx)abi-note.o $(build-shlib-objlist)
+ $(if $(and $(filter yes,$(have-test-clang)),$(cc-clang-crtbegin)),$(if $(filter c.so,$(@F)),,$(+preinit)) $(+prectorS)) \
+ $(csu-objpfx)abi-note.o $(build-shlib-objlist) \
+ $(if $(and $(filter yes,$(have-test-clang)),$(cc-clang-crtend)),$(+postctorS) $(if $(filter c.so,$(@F)),,$(+postinit)))
endef
define build-module-helper
-$(LINK.o) -shared -static-libgcc $(sysdep-LDFLAGS) $(rtld-LDFLAGS) \
+$(LINK.o) -shared $(if $(and $(filter yes,$(have-test-clang)),$(cc-clang-rtlib-flags)),-nostartfiles $(cc-clang-rtlib-flags),-static-libgcc) $(sysdep-LDFLAGS) $(rtld-LDFLAGS) \
$(if $($(@F)-no-z-defs)$(no-z-defs),,-Wl,-z,defs) \
$(if $($(@F)-no-dt-relr),$(no-dt-relr-ldflag),$(dt-relr-ldflag)) \
-B$(csu-objpfx) $(load-map-file) \
@@ -574,14 +576,18 @@
# not for shared objects
define build-module
$(build-module-helper) -o $@ \
- $(csu-objpfx)abi-note.o $(build-module-objlist) $(link-libc-args)
+ $(if $(and $(filter yes,$(have-test-clang)),$(cc-clang-crtbegin)),$(+preinit) $(+prectorS)) \
+ $(csu-objpfx)abi-note.o $(build-module-objlist) $(link-libc-args) \
+ $(if $(and $(filter yes,$(have-test-clang)),$(cc-clang-crtend)),$(+postctorS) $(+postinit))
$(call after-link,$@)
endef
define build-module-asneeded
$(build-module-helper) -o $@ \
+ $(if $(and $(filter yes,$(have-test-clang)),$(cc-clang-crtbegin)),$(+preinit) $(+prectorS)) \
$(csu-objpfx)abi-note.o \
-Wl,--as-needed $(build-module-objlist) -Wl,--no-as-needed \
- $(link-libc-args)
+ $(link-libc-args) \
+ $(if $(and $(filter yes,$(have-test-clang)),$(cc-clang-crtend)),$(+postctorS) $(+postinit))
$(call after-link,$@)
endef
diff -ruN a/configure b/configure
--- a/configure 2026-01-23 14:54:00.000000000 -0600
+++ b/configure 2026-03-15 19:54:16.436079577 -0500
@@ -9328,18 +9328,28 @@
config_vars="$config_vars
enable-static-pie = $libc_cv_static_pie"
-# Check if we can link support functionality against libgcc_s.
+# Check if the compiler reports usable support functionality from libgcc_s.
# Must not be used for linking installed binaries, to produce the
# same binaries for bootstrapped and bootstrapping builds (the latter
# with a GCC that does not build libgcc_s).
-{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking whether $CC can link against -lgcc_s" >&5
-printf %s "checking whether $CC can link against -lgcc_s... " >&6; }
+#
+# The compiler-reported runtime may be an absolute path rather than a bare
+# library name, for example when using llvm-libgcc. Include the configured
+# target-selection flags so multilib and compiler-rt builds query the
+# matching runtime.
+{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking whether $CC reports a usable libgcc_s" >&5
+printf %s "checking whether $CC reports a usable libgcc_s... " >&6; }
if test ${libc_cv_have_libgcc_s+y}
then :
printf %s "(cached) " >&6
else case e in #(
e) old_LIBS="$LIBS"
- LIBS="$LIBS -lgcc_s"
+ libc_cv_libgcc_s=`$CC $CFLAGS $LDFLAGS -print-file-name=libgcc_s.so 2>/dev/null`
+ if test "x$libc_cv_libgcc_s" = "xlibgcc_s.so"
+ then :
+ libc_cv_have_libgcc_s=no
+ else case e in #(
+ e) LIBS="$LIBS $libc_cv_libgcc_s"
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
@@ -9360,6 +9370,9 @@
fi
rm -f core conftest.err conftest.$ac_objext conftest.beam \
conftest$ac_exeext conftest.$ac_ext
+ ;;
+esac
+ fi
LIBS="$old_LIBS" ;;
esac
fi
@@ -11002,5 +11015,3 @@
{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: WARNING: unrecognized options: $ac_unrecognized_opts" >&5
printf "%s\n" "$as_me: WARNING: unrecognized options: $ac_unrecognized_opts" >&2;}
fi
-
-
diff -ruN a/configure.ac b/configure.ac
--- a/configure.ac 2026-01-23 14:54:00.000000000 -0600
+++ b/configure.ac 2026-03-15 19:54:16.436525491 -0500
@@ -2122,15 +2122,24 @@
fi
LIBC_CONFIG_VAR([enable-static-pie], [$libc_cv_static_pie])
-# Check if we can link support functionality against libgcc_s.
+# Check if the compiler reports usable support functionality from libgcc_s.
# Must not be used for linking installed binaries, to produce the
# same binaries for bootstrapped and bootstrapping builds (the latter
# with a GCC that does not build libgcc_s).
-AC_CACHE_CHECK([whether $CC can link against -lgcc_s], libc_cv_have_libgcc_s, [dnl
+# The compiler-reported runtime may be an absolute path rather than a bare
+# library name, for example when using llvm-libgcc. Include the configured
+# target-selection flags so multilib and compiler-rt builds query the
+# matching runtime.
+AC_CACHE_CHECK([whether $CC reports a usable libgcc_s], libc_cv_have_libgcc_s, [dnl
old_LIBS="$LIBS"
- LIBS="$LIBS -lgcc_s"
- AC_LINK_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
- [libc_cv_have_libgcc_s=yes], [libc_cv_have_libgcc_s=no])
+ libc_cv_libgcc_s=`$CC $CFLAGS $LDFLAGS -print-file-name=libgcc_s.so 2>/dev/null`
+ if test "x$libc_cv_libgcc_s" = "xlibgcc_s.so"; then
+ libc_cv_have_libgcc_s=no
+ else
+ LIBS="$LIBS $libc_cv_libgcc_s"
+ AC_LINK_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
+ [libc_cv_have_libgcc_s=yes], [libc_cv_have_libgcc_s=no])
+ fi
LIBS="$old_LIBS"])
LIBC_CONFIG_VAR([have-libgcc_s], [$libc_cv_have_libgcc_s])
diff -ruN a/dlfcn/Makefile b/dlfcn/Makefile
--- a/dlfcn/Makefile 2026-01-23 14:54:00.000000000 -0600
+++ b/dlfcn/Makefile 2026-03-15 19:54:16.436660818 -0500
@@ -187,7 +187,7 @@
$(objpfx)bug-atexit2.out: $(objpfx)bug-atexit2-lib.so
ifneq (,$(CXX))
-LDLIBS-bug-atexit3-lib.so = -lstdc++ -lgcc_eh
+LDLIBS-bug-atexit3-lib.so = -lstdc++ $(libgcc_eh_archive)
$(objpfx)bug-atexit3-lib.so: $(libsupport)
$(objpfx)bug-atexit3.out: $(objpfx)bug-atexit3-lib.so
endif
diff -ruN a/elf/Makefile b/elf/Makefile
--- a/elf/Makefile 2026-01-23 14:54:00.000000000 -0600
+++ b/elf/Makefile 2026-03-15 19:54:16.436784483 -0500
@@ -1514,7 +1514,7 @@
echo ".globl $$symbol"; \
echo "$$symbol:"; \
done | $(CC) -o $@T.o $(ASFLAGS) -c -x assembler -
- $(reloc-link) -o $@.o $@T.o '-Wl,-(' $^ -lgcc '-Wl,-)' -Wl,-Map,$@T
+ $(reloc-link) -o $@.o $@T.o '-Wl,-(' $^ $(libgcc) '-Wl,-)' -Wl,-Map,$@T
rm -f %@T.o $@.o
mv -f $@T $@
@@ -1544,7 +1544,7 @@
$(MAKE) -f $< -f rtld-Rules
$(objpfx)librtld.os: $(objpfx)dl-allobjs.os $(objpfx)rtld-libc.a
- $(LINK.o) -nostdlib -nostartfiles -r -o $@ '-Wl,-(' $^ -lgcc '-Wl,-)' \
+ $(LINK.o) -nostdlib -nostartfiles -r -o $@ '-Wl,-(' $^ $(libgcc) '-Wl,-)' \
-Wl,-Map,$@.map
generated += librtld.map librtld.mk rtld-libc.a librtld.os.map
diff -ruN a/support/Makefile b/support/Makefile
--- a/support/Makefile 2026-01-23 14:54:00.000000000 -0600
+++ b/support/Makefile 2026-03-15 19:54:16.436940579 -0500
@@ -284,14 +284,14 @@
ifeq (,$(CXX))
LINKS_DSO_PROGRAM = links-dso-program-c
-LDLIBS-links-dso-program-c = -lgcc
+LDLIBS-links-dso-program-c = $(libgcc)
ifeq ($(have-libgcc_s),yes)
CFLAGS-links-dso-program-c.c += -fexceptions
-LDLIBS-links-dso-program-c += -lgcc_s $(libunwind)
+LDLIBS-links-dso-program-c += $(libgcc_s) $(libunwind)
endif
else
LINKS_DSO_PROGRAM = links-dso-program
-LDLIBS-links-dso-program = -lstdc++ -lgcc -lgcc_s $(libunwind)
+LDLIBS-links-dso-program = -lstdc++ $(libgcc) $(libgcc_s) $(libunwind)
endif
ifeq (yes,$(have-selinux))