f@3052: Upstream-Status: Backport f@3052: Signed-off-by: Khem Raj f@3052: f@3052: From 95f5a9a866695da4e038aa4e6ccbbfd5d9cf63b7 Mon Sep 17 00:00:00 2001 f@3052: From: Joseph Myers f@3052: Date: Tue, 3 Jul 2012 19:14:59 +0000 f@3052: Subject: [PATCH] Avoid use of libgcc_s and libgcc_eh when building glibc. f@3052: f@3052: --- f@3052: ChangeLog | 47 ++++++++++++++++++++++++++++++ f@3052: Makeconfig | 68 ++++++++++++++++++++++++++++++++++++++------ f@3052: Rules | 45 ++++++++++++++++++++++------- f@3052: elf/Makefile | 6 +++- f@3052: elf/static-stubs.c | 46 ++++++++++++++++++++++++++++++ f@3052: ports/ChangeLog.arm | 7 +++++ f@3052: ports/sysdeps/arm/Makefile | 8 ++++++ f@3052: 7 files changed, 206 insertions(+), 21 deletions(-) f@3052: create mode 100644 elf/static-stubs.c f@3052: f@3052: Index: a/libc/Makeconfig f@3052: =================================================================== f@3052: --- a/libc/Makeconfig 2012-07-04 18:26:22.000000000 -0700 f@3052: +++ b/libc/Makeconfig 2012-08-14 20:16:10.197093639 -0700 f@3052: @@ -415,9 +415,9 @@ f@3052: LDFLAGS-rtld += $(hashstyle-LDFLAGS) f@3052: endif f@3052: f@3052: -# Command for linking programs with the C library. f@3052: +# Commands for linking programs with the C library. f@3052: ifndef +link f@3052: -+link = $(CC) -nostdlib -nostartfiles -o $@ \ f@3052: ++link-before-libc = $(CC) -nostdlib -nostartfiles -o $@ \ f@3052: $(sysdep-LDFLAGS) $(config-LDFLAGS) $(LDFLAGS) $(LDFLAGS-$(@F)) \ f@3052: $(combreloc-LDFLAGS) $(relro-LDFLAGS) $(hashstyle-LDFLAGS) \ f@3052: $(addprefix $(csu-objpfx),$(start-installed-name)) \ f@3052: @@ -426,7 +426,10 @@ f@3052: $(start-installed-name))\ f@3052: $(+preinit) $(link-extra-libs) \ f@3052: $(common-objpfx)libc% $(+postinit),$^) \ f@3052: - $(link-extra-libs) $(link-libc) $(+postctor) $(+postinit) f@3052: + $(link-extra-libs) f@3052: ++link-after-libc = $(+postctor) $(+postinit) f@3052: ++link = $(+link-before-libc) $(link-libc) $(+link-after-libc) f@3052: ++link-tests = $(+link-before-libc) $(link-libc-tests) $(+link-after-libc) f@3052: endif f@3052: # Command for linking PIE programs with the C library. f@3052: ifndef +link-pie f@3052: @@ -443,7 +446,7 @@ f@3052: endif f@3052: # Command for statically linking programs with the C library. f@3052: ifndef +link-static f@3052: -+link-static = $(CC) -nostdlib -nostartfiles -static -o $@ \ f@3052: ++link-static-before-libc = $(CC) -nostdlib -nostartfiles -static -o $@ \ f@3052: $(sysdep-LDFLAGS) $(LDFLAGS) $(LDFLAGS-$(@F)) \ f@3052: $(addprefix $(csu-objpfx),$(static-start-installed-name)) \ f@3052: $(+preinit) $(+prector) \ f@3052: @@ -451,7 +454,12 @@ f@3052: $(start-installed-name))\ f@3052: $(+preinit) $(link-extra-libs-static) \ f@3052: $(common-objpfx)libc% $(+postinit),$^) \ f@3052: - $(link-extra-libs-static) $(link-libc-static) $(+postctor) $(+postinit) f@3052: + $(link-extra-libs-static) $(link-libc-static) f@3052: ++link-static-after-libc = $(+postctor) $(+postinit) f@3052: ++link-static = $(+link-static-before-libc) $(link-libc-static) \ f@3052: + $(+link-static-after-libc) f@3052: ++link-static-tests = $(+link-static-before-libc) $(link-libc-static-tests) \ f@3052: + $(+link-static-after-libc) f@3052: endif f@3052: # Command for statically linking bounded-pointer programs with the C library. f@3052: ifndef +link-bounded f@3052: @@ -475,10 +483,12 @@ f@3052: # We need the versioned name of libc.so in the deps of $(others) et al f@3052: # so that the symlink to libc.so is created before anything tries to f@3052: # run the linked programs. f@3052: -link-libc = -Wl,-rpath-link=$(rpath-link) \ f@3052: +link-libc-before-gnulib = -Wl,-rpath-link=$(rpath-link) \ f@3052: $(common-objpfx)libc.so$(libc.so-version) \ f@3052: $(common-objpfx)$(patsubst %,$(libtype.oS),c) \ f@3052: - $(as-needed) $(common-objpfx)elf/ld.so $(no-as-needed) $(gnulib) f@3052: + $(as-needed) $(common-objpfx)elf/ld.so $(no-as-needed) f@3052: +link-libc = $(link-libc-before-gnulib) $(gnulib) f@3052: +link-libc-tests = $(link-libc-before-gnulib) $(gnulib-tests) f@3052: # This is how to find at build-time things that will be installed there. f@3052: rpath-dirs = math elf dlfcn nss nis rt resolv crypt f@3052: rpath-link = \ f@3052: @@ -488,6 +498,7 @@ f@3052: nssobjdir := $(patsubst ../$(subdir),.,$(common-objpfx)nss) f@3052: resolvobjdir := $(patsubst ../$(subdir),.,$(common-objpfx)resolv) f@3052: link-libc = $(common-objpfx)libc.a $(otherlibs) $(gnulib) $(common-objpfx)libc.a $(gnulib) f@3052: +link-libc-tests = $(common-objpfx)libc.a $(otherlibs) $(gnulib-tests) $(common-objpfx)libc.a $(gnulib-tests) f@3052: endif f@3052: endif f@3052: f@3052: @@ -513,8 +524,43 @@ f@3052: f@3052: # The static libraries. f@3052: link-libc-static = -Wl,--start-group $(common-objpfx)libc.a $(static-gnulib) -Wl,--end-group f@3052: +link-libc-static-tests = -Wl,--start-group $(common-objpfx)libc.a $(static-gnulib-tests) -Wl,--end-group f@3052: link-libc-bounded = $(common-objpfx)libc_b.a $(gnulib) $(common-objpfx)libc_b.a f@3052: f@3052: +# How to link against libgcc. Some libgcc functions, such as those f@3052: +# for "long long" arithmetic or software floating point, can always be f@3052: +# built without use of C library headers and do not have any global f@3052: +# state so can safely be linked statically into any executable or f@3052: +# shared library requiring them; these functions are in libgcc.a. f@3052: +# Other functions, relating to exception handling, may require C f@3052: +# library headers to build and it may not be safe to have more than f@3052: +# one copy of them in a process; these functions are only in f@3052: +# libgcc_s.so and libgcc_eh.a. f@3052: +# f@3052: +# To avoid circular dependencies when bootstrapping, it is desirable f@3052: +# to avoid use of libgcc_s and libgcc_eh in building glibc. Where any f@3052: +# glibc functionality (in particular, thread cancellation) requires f@3052: +# exception handling, this is implemented through dlopen of libgcc_s f@3052: +# to avoid unnecessary dependencies on libgcc_s by programs not using f@3052: +# that functionality; executables built with glibc do not use f@3052: +# exception handling other than through thread cancellation. f@3052: +# f@3052: +# Undefined references to functions from libgcc_eh or libgcc_s may f@3052: +# arise for code built with -fexceptions. In the case of statically f@3052: +# linked programs installed by glibc, unwinding will never actually f@3052: +# occur at runtime and the use of elf/static-stubs.c to resolve these f@3052: +# references is safe. In the case of statically linked test programs f@3052: +# and test programs built with -fexceptions, unwinding may occur in f@3052: +# some cases and it is preferable to link with libgcc_eh or libgcc_s f@3052: +# so that the testing is as similar as possible to how programs will f@3052: +# be built with the installed glibc. f@3052: +# f@3052: +# Some architectures have architecture-specific systems for exception f@3052: +# handling that may involve undefined references to f@3052: +# architecture-specific functions. On those architectures, f@3052: +# gnulib-arch and static-gnulib-arch may be defined in sysdeps f@3052: +# makefiles to use additional libraries for linking executables and f@3052: +# shared libraries built by glibc. f@3052: ifndef gnulib f@3052: ifneq ($(have-cc-with-libunwind),yes) f@3052: libunwind = f@3052: @@ -522,8 +568,12 @@ f@3052: libunwind = -lunwind f@3052: endif f@3052: libgcc_eh := -Wl,--as-needed -lgcc_s $(libunwind) -Wl,--no-as-needed f@3052: -gnulib := -lgcc $(libgcc_eh) f@3052: -static-gnulib := -lgcc -lgcc_eh $(libunwind) f@3052: +gnulib-arch = f@3052: +gnulib = -lgcc $(gnulib-arch) f@3052: +gnulib-tests := -lgcc $(libgcc_eh) f@3052: +static-gnulib-arch = f@3052: +static-gnulib = -lgcc $(static-gnulib-arch) f@3052: +static-gnulib-tests := -lgcc -lgcc_eh $(libunwind) f@3052: libc.so-gnulib := -lgcc f@3052: endif f@3052: +preinit = $(addprefix $(csu-objpfx),crti.o) f@3052: Index: a/libc/Rules f@3052: =================================================================== f@3052: --- a/libc/Rules 2012-07-04 18:25:47.000000000 -0700 f@3052: +++ b/libc/Rules 2012-08-14 20:16:10.197093639 -0700 f@3052: @@ -1,5 +1,4 @@ f@3052: -# Copyright (C) 1991-2000,2002,2003,2004,2005,2006,2011 f@3052: -# Free Software Foundation, Inc. f@3052: +# Copyright (C) 1991-2012 Free Software Foundation, Inc. f@3052: # This file is part of the GNU C Library. f@3052: f@3052: # The GNU C Library is free software; you can redistribute it and/or f@3052: @@ -103,29 +102,46 @@ f@3052: # eglibc: endif f@3052: f@3052: ifeq ($(build-programs),yes) f@3052: -binaries-all = $(others) $(sysdep-others) $(tests) $(xtests) $(test-srcs) f@3052: -binaries-static = $(others-static) $(tests-static) $(xtests-static) f@3052: +binaries-all-notests = $(others) $(sysdep-others) f@3052: +binaries-all-tests = $(tests) $(xtests) $(test-srcs) f@3052: +binaries-all = $(binaries-all-notests) $(binaries-all-tests) f@3052: +binaries-static-notests = $(others-static) f@3052: +binaries-static-tests = $(tests-static) $(xtests-static) f@3052: +binaries-static = $(binaries-static-notests) $(binaries-static-tests) f@3052: ifeq (yesyes,$(have-fpie)$(build-shared)) f@3052: binaries-pie = $(others-pie) $(tests-pie) $(xtests-pie) f@3052: else f@3052: binaries-pie = f@3052: endif f@3052: else f@3052: -binaries-all = $(tests) $(xtests) $(test-srcs) f@3052: +binaries-all-notests = f@3052: +binaries-all-tests = $(tests) $(xtests) $(test-srcs) f@3052: +binaries-all = $(binaries-all-tests) f@3052: +binaries-static-notests = f@3052: +binaries-static-tests = f@3052: binaries-static = f@3052: binaries-pie = f@3052: endif f@3052: f@3052: -binaries-shared = $(filter-out $(binaries-pie) $(binaries-static), \ f@3052: - $(binaries-all)) f@3052: +binaries-shared-tests = $(filter-out $(binaries-pie) $(binaries-static), \ f@3052: + $(binaries-all-tests)) f@3052: +binaries-shared-notests = $(filter-out $(binaries-pie) $(binaries-static), \ f@3052: + $(binaries-all-notests)) f@3052: f@3052: -ifneq "$(strip $(binaries-shared))" "" f@3052: -$(addprefix $(objpfx),$(binaries-shared)): %: %.o \ f@3052: +ifneq "$(strip $(binaries-shared-notests))" "" f@3052: +$(addprefix $(objpfx),$(binaries-shared-notests)): %: %.o \ f@3052: $(sort $(filter $(common-objpfx)lib%,$(link-libc))) \ f@3052: $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit) f@3052: $(+link) f@3052: endif f@3052: f@3052: +ifneq "$(strip $(binaries-shared-tests))" "" f@3052: +$(addprefix $(objpfx),$(binaries-shared-tests)): %: %.o \ f@3052: + $(sort $(filter $(common-objpfx)lib%,$(link-libc))) \ f@3052: + $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit) f@3052: + $(+link-tests) f@3052: +endif f@3052: + f@3052: ifneq "$(strip $(binaries-pie))" "" f@3052: $(addprefix $(objpfx),$(binaries-pie)): %: %.o \ f@3052: $(sort $(filter $(common-objpfx)lib%,$(link-libc))) \ f@3052: @@ -133,13 +149,20 @@ f@3052: $(+link-pie) f@3052: endif f@3052: f@3052: -ifneq "$(strip $(binaries-static))" "" f@3052: -$(addprefix $(objpfx),$(binaries-static)): %: %.o \ f@3052: +ifneq "$(strip $(binaries-static-notests))" "" f@3052: +$(addprefix $(objpfx),$(binaries-static-notests)): %: %.o \ f@3052: $(sort $(filter $(common-objpfx)lib%,$(link-libc-static))) \ f@3052: $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit) f@3052: $(+link-static) f@3052: endif f@3052: f@3052: +ifneq "$(strip $(binaries-static-tests))" "" f@3052: +$(addprefix $(objpfx),$(binaries-static-tests)): %: %.o \ f@3052: + $(sort $(filter $(common-objpfx)lib%,$(link-libc-static-tests))) \ f@3052: + $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit) f@3052: + $(+link-static-tests) f@3052: +endif f@3052: + f@3052: ifeq ($(build-bounded),yes) f@3052: binaries-bounded = $(addsuffix -bp,$(tests) $(xtests) $(test-srcs)) f@3052: $(addprefix $(objpfx),$(binaries-bounded)): %-bp: %.ob \ f@3052: Index: a/libc/elf/Makefile f@3052: =================================================================== f@3052: --- a/libc/elf/Makefile 2012-07-04 18:26:34.000000000 -0700 f@3052: +++ b/libc/elf/Makefile 2012-08-14 20:16:10.197093639 -0700 f@3052: @@ -71,6 +71,8 @@ f@3052: install-bin = sprof pldd f@3052: others-static = sln f@3052: install-rootsbin = sln f@3052: +sln-modules := static-stubs f@3052: +extra-objs += $(sln-modules:=.o) f@3052: f@3052: ifeq (yes,$(use-ldconfig)) f@3052: ifeq (yes,$(build-shared)) f@3052: @@ -78,7 +80,7 @@ f@3052: others += ldconfig f@3052: install-rootsbin += ldconfig f@3052: f@3052: -ldconfig-modules := cache readlib xmalloc xstrdup chroot_canon f@3052: +ldconfig-modules := cache readlib xmalloc xstrdup chroot_canon static-stubs f@3052: extra-objs += $(ldconfig-modules:=.o) f@3052: endif f@3052: endif f@3052: @@ -417,6 +419,8 @@ f@3052: f@3052: $(objpfx)sprof: $(libdl) f@3052: f@3052: +$(objpfx)sln: $(sln-modules:%=$(objpfx)%.o) f@3052: + f@3052: $(objpfx)ldconfig: $(ldconfig-modules:%=$(objpfx)%.o) f@3052: f@3052: $(objpfx)pldd: $(pldd-modules:%=$(objpfx)%.o) f@3052: Index: a/libc/elf/static-stubs.c f@3052: =================================================================== f@3052: --- /dev/null 1970-01-01 00:00:00.000000000 +0000 f@3052: +++ a/libc/elf/static-stubs.c 2012-08-14 20:16:10.197093639 -0700 f@3052: @@ -0,0 +1,46 @@ f@3052: +/* Stub implementations of functions to link into statically linked f@3052: + programs without needing libgcc_eh. f@3052: + Copyright (C) 2012 Free Software Foundation, Inc. f@3052: + This file is part of the GNU C Library. f@3052: + f@3052: + The GNU C Library is free software; you can redistribute it and/or f@3052: + modify it under the terms of the GNU Lesser General Public f@3052: + License as published by the Free Software Foundation; either f@3052: + version 2.1 of the License, or (at your option) any later version. f@3052: + f@3052: + The GNU C Library is distributed in the hope that it will be useful, f@3052: + but WITHOUT ANY WARRANTY; without even the implied warranty of f@3052: + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU f@3052: + Lesser General Public License for more details. f@3052: + f@3052: + You should have received a copy of the GNU Lesser General Public f@3052: + License along with the GNU C Library; if not, see f@3052: + . */ f@3052: + f@3052: +/* Avoid backtrace (and so _Unwind_Backtrace) dependencies from f@3052: + sysdeps/unix/sysv/linux/libc_fatal.c. */ f@3052: +#include f@3052: + f@3052: +#include f@3052: +#include f@3052: + f@3052: +/* These programs do not use thread cancellation, so _Unwind_Resume f@3052: + and the personality routine are never actually called. */ f@3052: + f@3052: +void f@3052: +_Unwind_Resume (struct _Unwind_Exception *exc __attribute__ ((unused))) f@3052: +{ f@3052: + abort (); f@3052: +} f@3052: + f@3052: +_Unwind_Reason_Code f@3052: +__gcc_personality_v0 (int version __attribute__ ((unused)), f@3052: + _Unwind_Action actions __attribute__ ((unused)), f@3052: + _Unwind_Exception_Class exception_class f@3052: + __attribute__ ((unused)), f@3052: + struct _Unwind_Exception *ue_header f@3052: + __attribute__ ((unused)), f@3052: + struct _Unwind_Context *context __attribute__ ((unused))) f@3052: +{ f@3052: + abort (); f@3052: +} f@3052: Index: a/ports/sysdeps/arm/Makefile f@3052: =================================================================== f@3052: --- a/ports/sysdeps/arm/Makefile 2012-07-04 18:25:41.000000000 -0700 f@3052: +++ b/ports/sysdeps/arm/Makefile 2012-08-14 20:16:10.201093638 -0700 f@3052: @@ -1,8 +1,16 @@ f@3052: +gnulib-arch = $(elfobjdir)/libgcc-stubs.a f@3052: +static-gnulib-arch = $(elfobjdir)/libgcc-stubs.a f@3052: + f@3052: ifeq ($(subdir),elf) f@3052: sysdep-dl-routines += tlsdesc dl-tlsdesc f@3052: sysdep_routines += aeabi_unwind_cpp_pr1 find_exidx tlsdesc dl-tlsdesc f@3052: sysdep-rtld-routines += aeabi_unwind_cpp_pr1 tlsdesc dl-tlsdesc f@3052: shared-only-routines += aeabi_unwind_cpp_pr1 f@3052: + f@3052: +$(objpfx)libgcc-stubs.a: $(objpfx)aeabi_unwind_cpp_pr1.os f@3052: + $(build-extra-lib) f@3052: + f@3052: +lib-noranlib: $(objpfx)libgcc-stubs.a f@3052: endif f@3052: f@3052: ifeq ($(subdir),csu)