1 Upstream-Status: Backport
2 Signed-off-by: Khem Raj <raj.khem@gmail.com>
4 From 95f5a9a866695da4e038aa4e6ccbbfd5d9cf63b7 Mon Sep 17 00:00:00 2001
5 From: Joseph Myers <joseph@codesourcery.com>
6 Date: Tue, 3 Jul 2012 19:14:59 +0000
7 Subject: [PATCH] Avoid use of libgcc_s and libgcc_eh when building glibc.
10 ChangeLog | 47 ++++++++++++++++++++++++++++++
11 Makeconfig | 68 ++++++++++++++++++++++++++++++++++++++------
12 Rules | 45 ++++++++++++++++++++++-------
14 elf/static-stubs.c | 46 ++++++++++++++++++++++++++++++
15 ports/ChangeLog.arm | 7 +++++
16 ports/sysdeps/arm/Makefile | 8 ++++++
17 7 files changed, 206 insertions(+), 21 deletions(-)
18 create mode 100644 elf/static-stubs.c
20 Index: libc/Makeconfig
21 ===================================================================
22 --- libc.orig/Makeconfig 2012-07-04 18:26:22.000000000 -0700
23 +++ libc/Makeconfig 2012-08-14 20:16:10.197093639 -0700
25 LDFLAGS-rtld += $(hashstyle-LDFLAGS)
28 -# Command for linking programs with the C library.
29 +# Commands for linking programs with the C library.
31 -+link = $(CC) -nostdlib -nostartfiles -o $@ \
32 ++link-before-libc = $(CC) -nostdlib -nostartfiles -o $@ \
33 $(sysdep-LDFLAGS) $(config-LDFLAGS) $(LDFLAGS) $(LDFLAGS-$(@F)) \
34 $(combreloc-LDFLAGS) $(relro-LDFLAGS) $(hashstyle-LDFLAGS) \
35 $(addprefix $(csu-objpfx),$(start-installed-name)) \
37 $(start-installed-name))\
38 $(+preinit) $(link-extra-libs) \
39 $(common-objpfx)libc% $(+postinit),$^) \
40 - $(link-extra-libs) $(link-libc) $(+postctor) $(+postinit)
42 ++link-after-libc = $(+postctor) $(+postinit)
43 ++link = $(+link-before-libc) $(link-libc) $(+link-after-libc)
44 ++link-tests = $(+link-before-libc) $(link-libc-tests) $(+link-after-libc)
46 # Command for linking PIE programs with the C library.
50 # Command for statically linking programs with the C library.
52 -+link-static = $(CC) -nostdlib -nostartfiles -static -o $@ \
53 ++link-static-before-libc = $(CC) -nostdlib -nostartfiles -static -o $@ \
54 $(sysdep-LDFLAGS) $(LDFLAGS) $(LDFLAGS-$(@F)) \
55 $(addprefix $(csu-objpfx),$(static-start-installed-name)) \
56 $(+preinit) $(+prector) \
58 $(start-installed-name))\
59 $(+preinit) $(link-extra-libs-static) \
60 $(common-objpfx)libc% $(+postinit),$^) \
61 - $(link-extra-libs-static) $(link-libc-static) $(+postctor) $(+postinit)
62 + $(link-extra-libs-static) $(link-libc-static)
63 ++link-static-after-libc = $(+postctor) $(+postinit)
64 ++link-static = $(+link-static-before-libc) $(link-libc-static) \
65 + $(+link-static-after-libc)
66 ++link-static-tests = $(+link-static-before-libc) $(link-libc-static-tests) \
67 + $(+link-static-after-libc)
69 # Command for statically linking bounded-pointer programs with the C library.
72 # We need the versioned name of libc.so in the deps of $(others) et al
73 # so that the symlink to libc.so is created before anything tries to
74 # run the linked programs.
75 -link-libc = -Wl,-rpath-link=$(rpath-link) \
76 +link-libc-before-gnulib = -Wl,-rpath-link=$(rpath-link) \
77 $(common-objpfx)libc.so$(libc.so-version) \
78 $(common-objpfx)$(patsubst %,$(libtype.oS),c) \
79 - $(as-needed) $(common-objpfx)elf/ld.so $(no-as-needed) $(gnulib)
80 + $(as-needed) $(common-objpfx)elf/ld.so $(no-as-needed)
81 +link-libc = $(link-libc-before-gnulib) $(gnulib)
82 +link-libc-tests = $(link-libc-before-gnulib) $(gnulib-tests)
83 # This is how to find at build-time things that will be installed there.
84 rpath-dirs = math elf dlfcn nss nis rt resolv crypt
87 nssobjdir := $(patsubst ../$(subdir),.,$(common-objpfx)nss)
88 resolvobjdir := $(patsubst ../$(subdir),.,$(common-objpfx)resolv)
89 link-libc = $(common-objpfx)libc.a $(otherlibs) $(gnulib) $(common-objpfx)libc.a $(gnulib)
90 +link-libc-tests = $(common-objpfx)libc.a $(otherlibs) $(gnulib-tests) $(common-objpfx)libc.a $(gnulib-tests)
96 # The static libraries.
97 link-libc-static = -Wl,--start-group $(common-objpfx)libc.a $(static-gnulib) -Wl,--end-group
98 +link-libc-static-tests = -Wl,--start-group $(common-objpfx)libc.a $(static-gnulib-tests) -Wl,--end-group
99 link-libc-bounded = $(common-objpfx)libc_b.a $(gnulib) $(common-objpfx)libc_b.a
101 +# How to link against libgcc. Some libgcc functions, such as those
102 +# for "long long" arithmetic or software floating point, can always be
103 +# built without use of C library headers and do not have any global
104 +# state so can safely be linked statically into any executable or
105 +# shared library requiring them; these functions are in libgcc.a.
106 +# Other functions, relating to exception handling, may require C
107 +# library headers to build and it may not be safe to have more than
108 +# one copy of them in a process; these functions are only in
109 +# libgcc_s.so and libgcc_eh.a.
111 +# To avoid circular dependencies when bootstrapping, it is desirable
112 +# to avoid use of libgcc_s and libgcc_eh in building glibc. Where any
113 +# glibc functionality (in particular, thread cancellation) requires
114 +# exception handling, this is implemented through dlopen of libgcc_s
115 +# to avoid unnecessary dependencies on libgcc_s by programs not using
116 +# that functionality; executables built with glibc do not use
117 +# exception handling other than through thread cancellation.
119 +# Undefined references to functions from libgcc_eh or libgcc_s may
120 +# arise for code built with -fexceptions. In the case of statically
121 +# linked programs installed by glibc, unwinding will never actually
122 +# occur at runtime and the use of elf/static-stubs.c to resolve these
123 +# references is safe. In the case of statically linked test programs
124 +# and test programs built with -fexceptions, unwinding may occur in
125 +# some cases and it is preferable to link with libgcc_eh or libgcc_s
126 +# so that the testing is as similar as possible to how programs will
127 +# be built with the installed glibc.
129 +# Some architectures have architecture-specific systems for exception
130 +# handling that may involve undefined references to
131 +# architecture-specific functions. On those architectures,
132 +# gnulib-arch and static-gnulib-arch may be defined in sysdeps
133 +# makefiles to use additional libraries for linking executables and
134 +# shared libraries built by glibc.
136 ifneq ($(have-cc-with-libunwind),yes)
141 libgcc_eh := -Wl,--as-needed -lgcc_s $(libunwind) -Wl,--no-as-needed
142 -gnulib := -lgcc $(libgcc_eh)
143 -static-gnulib := -lgcc -lgcc_eh $(libunwind)
145 +gnulib = -lgcc $(gnulib-arch)
146 +gnulib-tests := -lgcc $(libgcc_eh)
147 +static-gnulib-arch =
148 +static-gnulib = -lgcc $(static-gnulib-arch)
149 +static-gnulib-tests := -lgcc -lgcc_eh $(libunwind)
150 libc.so-gnulib := -lgcc
152 +preinit = $(addprefix $(csu-objpfx),crti.o)
154 ===================================================================
155 --- libc.orig/Rules 2012-07-04 18:25:47.000000000 -0700
156 +++ libc/Rules 2012-08-14 20:16:10.197093639 -0700
158 -# Copyright (C) 1991-2000,2002,2003,2004,2005,2006,2011
159 -# Free Software Foundation, Inc.
160 +# Copyright (C) 1991-2012 Free Software Foundation, Inc.
161 # This file is part of the GNU C Library.
163 # The GNU C Library is free software; you can redistribute it and/or
164 @@ -103,29 +102,46 @@
167 ifeq ($(build-programs),yes)
168 -binaries-all = $(others) $(sysdep-others) $(tests) $(xtests) $(test-srcs)
169 -binaries-static = $(others-static) $(tests-static) $(xtests-static)
170 +binaries-all-notests = $(others) $(sysdep-others)
171 +binaries-all-tests = $(tests) $(xtests) $(test-srcs)
172 +binaries-all = $(binaries-all-notests) $(binaries-all-tests)
173 +binaries-static-notests = $(others-static)
174 +binaries-static-tests = $(tests-static) $(xtests-static)
175 +binaries-static = $(binaries-static-notests) $(binaries-static-tests)
176 ifeq (yesyes,$(have-fpie)$(build-shared))
177 binaries-pie = $(others-pie) $(tests-pie) $(xtests-pie)
182 -binaries-all = $(tests) $(xtests) $(test-srcs)
183 +binaries-all-notests =
184 +binaries-all-tests = $(tests) $(xtests) $(test-srcs)
185 +binaries-all = $(binaries-all-tests)
186 +binaries-static-notests =
187 +binaries-static-tests =
192 -binaries-shared = $(filter-out $(binaries-pie) $(binaries-static), \
194 +binaries-shared-tests = $(filter-out $(binaries-pie) $(binaries-static), \
195 + $(binaries-all-tests))
196 +binaries-shared-notests = $(filter-out $(binaries-pie) $(binaries-static), \
197 + $(binaries-all-notests))
199 -ifneq "$(strip $(binaries-shared))" ""
200 -$(addprefix $(objpfx),$(binaries-shared)): %: %.o \
201 +ifneq "$(strip $(binaries-shared-notests))" ""
202 +$(addprefix $(objpfx),$(binaries-shared-notests)): %: %.o \
203 $(sort $(filter $(common-objpfx)lib%,$(link-libc))) \
204 $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
208 +ifneq "$(strip $(binaries-shared-tests))" ""
209 +$(addprefix $(objpfx),$(binaries-shared-tests)): %: %.o \
210 + $(sort $(filter $(common-objpfx)lib%,$(link-libc))) \
211 + $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
215 ifneq "$(strip $(binaries-pie))" ""
216 $(addprefix $(objpfx),$(binaries-pie)): %: %.o \
217 $(sort $(filter $(common-objpfx)lib%,$(link-libc))) \
218 @@ -133,13 +149,20 @@
222 -ifneq "$(strip $(binaries-static))" ""
223 -$(addprefix $(objpfx),$(binaries-static)): %: %.o \
224 +ifneq "$(strip $(binaries-static-notests))" ""
225 +$(addprefix $(objpfx),$(binaries-static-notests)): %: %.o \
226 $(sort $(filter $(common-objpfx)lib%,$(link-libc-static))) \
227 $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
231 +ifneq "$(strip $(binaries-static-tests))" ""
232 +$(addprefix $(objpfx),$(binaries-static-tests)): %: %.o \
233 + $(sort $(filter $(common-objpfx)lib%,$(link-libc-static-tests))) \
234 + $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
235 + $(+link-static-tests)
238 ifeq ($(build-bounded),yes)
239 binaries-bounded = $(addsuffix -bp,$(tests) $(xtests) $(test-srcs))
240 $(addprefix $(objpfx),$(binaries-bounded)): %-bp: %.ob \
241 Index: libc/elf/Makefile
242 ===================================================================
243 --- libc.orig/elf/Makefile 2012-07-04 18:26:34.000000000 -0700
244 +++ libc/elf/Makefile 2012-08-14 20:16:10.197093639 -0700
246 install-bin = sprof pldd
248 install-rootsbin = sln
249 +sln-modules := static-stubs
250 +extra-objs += $(sln-modules:=.o)
252 ifeq (yes,$(use-ldconfig))
253 ifeq (yes,$(build-shared))
256 install-rootsbin += ldconfig
258 -ldconfig-modules := cache readlib xmalloc xstrdup chroot_canon
259 +ldconfig-modules := cache readlib xmalloc xstrdup chroot_canon static-stubs
260 extra-objs += $(ldconfig-modules:=.o)
265 $(objpfx)sprof: $(libdl)
267 +$(objpfx)sln: $(sln-modules:%=$(objpfx)%.o)
269 $(objpfx)ldconfig: $(ldconfig-modules:%=$(objpfx)%.o)
271 $(objpfx)pldd: $(pldd-modules:%=$(objpfx)%.o)
272 Index: libc/elf/static-stubs.c
273 ===================================================================
274 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
275 +++ libc/elf/static-stubs.c 2012-08-14 20:16:10.197093639 -0700
277 +/* Stub implementations of functions to link into statically linked
278 + programs without needing libgcc_eh.
279 + Copyright (C) 2012 Free Software Foundation, Inc.
280 + This file is part of the GNU C Library.
282 + The GNU C Library is free software; you can redistribute it and/or
283 + modify it under the terms of the GNU Lesser General Public
284 + License as published by the Free Software Foundation; either
285 + version 2.1 of the License, or (at your option) any later version.
287 + The GNU C Library is distributed in the hope that it will be useful,
288 + but WITHOUT ANY WARRANTY; without even the implied warranty of
289 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
290 + Lesser General Public License for more details.
292 + You should have received a copy of the GNU Lesser General Public
293 + License along with the GNU C Library; if not, see
294 + <http://www.gnu.org/licenses/>. */
296 +/* Avoid backtrace (and so _Unwind_Backtrace) dependencies from
297 + sysdeps/unix/sysv/linux/libc_fatal.c. */
298 +#include <sysdeps/posix/libc_fatal.c>
303 +/* These programs do not use thread cancellation, so _Unwind_Resume
304 + and the personality routine are never actually called. */
307 +_Unwind_Resume (struct _Unwind_Exception *exc __attribute__ ((unused)))
313 +__gcc_personality_v0 (int version __attribute__ ((unused)),
314 + _Unwind_Action actions __attribute__ ((unused)),
315 + _Unwind_Exception_Class exception_class
316 + __attribute__ ((unused)),
317 + struct _Unwind_Exception *ue_header
318 + __attribute__ ((unused)),
319 + struct _Unwind_Context *context __attribute__ ((unused)))
323 Index: libc/ports/sysdeps/arm/Makefile
324 ===================================================================
325 --- libc.orig/ports/sysdeps/arm/Makefile 2012-07-04 18:25:41.000000000 -0700
326 +++ libc/ports/sysdeps/arm/Makefile 2012-08-14 20:16:10.201093638 -0700
328 +gnulib-arch = $(elfobjdir)/libgcc-stubs.a
329 +static-gnulib-arch = $(elfobjdir)/libgcc-stubs.a
332 sysdep-dl-routines += tlsdesc dl-tlsdesc
333 sysdep_routines += aeabi_unwind_cpp_pr1 find_exidx tlsdesc dl-tlsdesc
334 sysdep-rtld-routines += aeabi_unwind_cpp_pr1 tlsdesc dl-tlsdesc
335 shared-only-routines += aeabi_unwind_cpp_pr1
337 +$(objpfx)libgcc-stubs.a: $(objpfx)aeabi_unwind_cpp_pr1.os
340 +lib-noranlib: $(objpfx)libgcc-stubs.a