This is the mail archive of the mailing list for the glibc project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Support installing headers for bootstrapping libgcc

On Fri, 11 May 2012, Roland McGrath wrote:

> Let's look a little deeper into what the particular references in
> linking ldconfig are and where they come from.  It seems likely that
> we can stub things out pretty simply so that we don't lose anything
> that matters.  I tried adding:
> /* Include our own copy of __libc_fatal that doesn't call backtrace
>    (as linux/libc_fatal.c does) and bring in all those dependencies.  */
> #include <sysdeps/posix/libc_fatal.c>
> at the end of ldconfig.c and after this the only things look to be
> stuff from libio that is for cancellation support, which ldconfig
> really does not need.  I haven't looked into how to stub those out,
> which might well depend on machine/compiler version.  But I think it's
> worth (someone) investigating how hairy it really is.

Stubbing things out for ldconfig and sln works.  I have a prototype patch 
(below) that passes build and test on x86_64, but there are some problems 
with it:

* Testcases need linking with the appropriate one of -lgcc_eh and -lgcc_s 
(the former needed by statically linked tests, the latter needed by 
various NPTL tests such as tst-cancelx*).  Certainly that seems better 
than linking them with stub files - tests should be built in a way as 
similar as possible to normal programs built with an installed glibc and 
compiler.  But I've needed to duplicate various makefile rules and 
variables to link tests differently from other programs; is there a better 
way of doing things?

* On ARM, any nontrivial unwind info involves a reference to the 
personality routine, and there are ARM-specific personality routines.  sln 
and ldconfig of course need them - I suppose a sysdeps version of 
static-stubs would deal with that.  But then zic turns out to need them, 
because of certain division functions in libgcc being built with unwind 
info, and nscd needs them because of the unwind info in inline syscalls - 
and if you build with unwind info by default, which can be useful for 
backtracing, then a lot more programs need the personality routines and 
would previously have used libgcc_s.  The stub versions that are linked 
into will always suffice[*], but what is the right way for the ARM 
makefiles to say "this extra object should be linked into all (non-test) 
executables being built by glibc"?

[*] The undefined symbols in object files are created by the assembler so 
that the relevant personality routine is linked in in the case of static 
linking.  If unwinding actually occurs through pthread_cancel, the 
personality routine that gets called is determined by a reference to the 
__aeabi_unwind_cpp_pr* symbol from libgcc_s, not the executable or libc or 
libpthread, using the unwind info to determine which of the standard 
personality routines is relevant.

diff --git a/Makeconfig b/Makeconfig
index f68a752..3cbf185 100644
--- a/Makeconfig
+++ b/Makeconfig
@@ -428,6 +428,19 @@ ifndef +link
 			   $(common-objpfx)libc% $(+postinit),$^) \
 	      $(link-extra-libs) $(link-libc) $(+postctor) $(+postinit)
+# Likewise, for test programs.
+ifndef +link-tests
++link-tests = $(CC) -nostdlib -nostartfiles -o $@ \
+	      $(sysdep-LDFLAGS) $(config-LDFLAGS) $(LDFLAGS) $(LDFLAGS-$(@F)) \
+	      $(combreloc-LDFLAGS) $(relro-LDFLAGS) $(hashstyle-LDFLAGS) \
+	      $(addprefix $(csu-objpfx),$(start-installed-name)) \
+	      $(+preinit) $(+prector) \
+	      $(filter-out $(addprefix $(csu-objpfx),start.o \
+						     $(start-installed-name))\
+			   $(+preinit) $(link-extra-libs) \
+			   $(common-objpfx)libc% $(+postinit),$^) \
+	      $(link-extra-libs) $(link-libc-tests) $(+postctor) $(+postinit)
 # Command for linking PIE programs with the C library.
 ifndef +link-pie
 +link-pie = $(CC) -pie -Wl,-O1 -nostdlib -nostartfiles -o $@ \
@@ -453,6 +466,18 @@ ifndef +link-static
 			   $(common-objpfx)libc% $(+postinit),$^) \
 	      $(link-extra-libs-static) $(link-libc-static) $(+postctor) $(+postinit)
+# Likewise, for test programs.
+ifndef +link-static-tests
++link-static-tests = $(CC) -nostdlib -nostartfiles -static -o $@ \
+	      $(sysdep-LDFLAGS) $(LDFLAGS) $(LDFLAGS-$(@F))  \
+	      $(addprefix $(csu-objpfx),$(static-start-installed-name)) \
+	      $(+preinit) $(+prector) \
+	      $(filter-out $(addprefix $(csu-objpfx),start.o \
+						     $(start-installed-name))\
+			   $(+preinit) $(link-extra-libs-static) \
+			   $(common-objpfx)libc% $(+postinit),$^) \
+	      $(link-extra-libs-static) $(link-libc-static-tests) $(+postctor) $(+postinit)
 # Command for statically linking bounded-pointer programs with the C library.
 ifndef +link-bounded
 +link-bounded = $(CC) -nostdlib -nostartfiles -static -fbounded-pointers -o $@ \
@@ -479,6 +504,11 @@ link-libc = -Wl,-rpath-link=$(rpath-link) \
 	    $(common-objpfx)$( \
 	    $(common-objpfx)$(patsubst %,$(libtype.oS),c) \
 	    $(as-needed) $(common-objpfx)elf/ $(no-as-needed) $(gnulib)
+link-libc-tests = -Wl,-rpath-link=$(rpath-link) \
+	    $(common-objpfx)$( \
+	    $(common-objpfx)$(patsubst %,$(libtype.oS),c) \
+	    $(as-needed) $(common-objpfx)elf/ $(no-as-needed) \
+	    $(gnulib-tests)
 # This is how to find at build-time things that will be installed there.
 rpath-dirs = math elf dlfcn nss nis rt resolv crypt
 rpath-link = \
@@ -488,6 +518,7 @@ else
 nssobjdir := $(patsubst ../$(subdir),.,$(common-objpfx)nss)
 resolvobjdir := $(patsubst ../$(subdir),.,$(common-objpfx)resolv)
 link-libc = $(common-objpfx)libc.a $(otherlibs) $(gnulib) $(common-objpfx)libc.a $(gnulib)
+link-libc-tests = $(common-objpfx)libc.a $(otherlibs) $(gnulib) $(common-objpfx)libc.a $(gnulib-tests)
@@ -513,6 +544,7 @@ endif
 # The static libraries.
 link-libc-static = -Wl,--start-group $(common-objpfx)libc.a $(static-gnulib) -Wl,--end-group
+link-libc-static-tests = -Wl,--start-group $(common-objpfx)libc.a $(static-gnulib-tests) -Wl,--end-group
 link-libc-bounded = $(common-objpfx)libc_b.a $(gnulib) $(common-objpfx)libc_b.a
 ifndef gnulib
@@ -522,8 +554,10 @@ else
   libunwind = -lunwind
 libgcc_eh := -Wl,--as-needed -lgcc_s $(libunwind) -Wl,--no-as-needed
-gnulib := -lgcc $(libgcc_eh)
-static-gnulib := -lgcc -lgcc_eh $(libunwind)
+gnulib := -lgcc
+gnulib-tests := -lgcc $(libgcc_eh)
+static-gnulib := -lgcc
+static-gnulib-tests := -lgcc -lgcc_eh $(libunwind) := -lgcc
 +preinit = $(addprefix $(csu-objpfx),crti.o)
diff --git a/Rules b/Rules
index 3c61a29..17d938e 100644
--- a/Rules
+++ b/Rules
@@ -1,5 +1,4 @@
-# Copyright (C) 1991-2000,2002,2003,2004,2005,2006,2011
-#	Free Software Foundation, Inc.
+# Copyright (C) 1991-2012 Free Software Foundation, Inc.
 # This file is part of the GNU C Library.
 # The GNU C Library is free software; you can redistribute it and/or
@@ -103,29 +102,46 @@ xtests: tests $(xtests:%=$(objpfx)%.out) $(xtests-bp.out)
 ifeq ($(build-programs),yes)
-binaries-all = $(others) $(sysdep-others) $(tests) $(xtests) $(test-srcs)
-binaries-static = $(others-static) $(tests-static) $(xtests-static)
+binaries-all-notests = $(others) $(sysdep-others)
+binaries-all-tests = $(tests) $(xtests) $(test-srcs)
+binaries-all = $(binaries-all-notests) $(binaries-all-tests)
+binaries-static-notests = $(others-static)
+binaries-static-tests = $(tests-static) $(xtests-static)
+binaries-static = $(binaries-static-notests) $(binaries-static-tests)
 ifeq (yesyes,$(have-fpie)$(build-shared))
 binaries-pie = $(others-pie) $(tests-pie) $(xtests-pie)
 binaries-pie =
-binaries-all = $(tests) $(xtests) $(test-srcs)
+binaries-all-notests =
+binaries-all-tests = $(tests) $(xtests) $(test-srcs)
+binaries-all = $(binaries-all-tests)
+binaries-static-notests =
+binaries-static-tests =
 binaries-static =
 binaries-pie =
-binaries-shared = $(filter-out $(binaries-pie) $(binaries-static), \
-			       $(binaries-all))
+binaries-shared-tests = $(filter-out $(binaries-pie) $(binaries-static), \
+				     $(binaries-all-tests))
+binaries-shared-notests = $(filter-out $(binaries-pie) $(binaries-static), \
+				       $(binaries-all-notests))
-ifneq "$(strip $(binaries-shared))" ""
-$(addprefix $(objpfx),$(binaries-shared)): %: %.o \
+ifneq "$(strip $(binaries-shared-notests))" ""
+$(addprefix $(objpfx),$(binaries-shared-notests)): %: %.o \
   $(sort $(filter $(common-objpfx)lib%,$(link-libc))) \
   $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
+ifneq "$(strip $(binaries-shared-tests))" ""
+$(addprefix $(objpfx),$(binaries-shared-tests)): %: %.o \
+  $(sort $(filter $(common-objpfx)lib%,$(link-libc))) \
+  $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
+	$(+link-tests)
 ifneq "$(strip $(binaries-pie))" ""
 $(addprefix $(objpfx),$(binaries-pie)): %: %.o \
   $(sort $(filter $(common-objpfx)lib%,$(link-libc))) \
@@ -133,13 +149,20 @@ $(addprefix $(objpfx),$(binaries-pie)): %: %.o \
-ifneq "$(strip $(binaries-static))" ""
-$(addprefix $(objpfx),$(binaries-static)): %: %.o \
+ifneq "$(strip $(binaries-static-notests))" ""
+$(addprefix $(objpfx),$(binaries-static-notests)): %: %.o \
   $(sort $(filter $(common-objpfx)lib%,$(link-libc-static))) \
   $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
+ifneq "$(strip $(binaries-static-tests))" ""
+$(addprefix $(objpfx),$(binaries-static-tests)): %: %.o \
+  $(sort $(filter $(common-objpfx)lib%,$(link-libc-static-tests))) \
+  $(addprefix $(csu-objpfx),start.o) $(+preinit) $(+postinit)
+	$(+link-static-tests)
 ifeq ($(build-bounded),yes)
 binaries-bounded = $(addsuffix -bp,$(tests) $(xtests) $(test-srcs))
 $(addprefix $(objpfx),$(binaries-bounded)): %-bp: %.ob \
diff --git a/elf/Makefile b/elf/Makefile
index f6c6fb4..99e6edc 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -71,6 +71,8 @@ others		= sprof sln pldd
 install-bin	= sprof pldd
 others-static   = sln
 install-rootsbin = sln
+sln-modules	:= static-stubs
+extra-objs	+= $(sln-modules:=.o)
 ifeq (yes,$(use-ldconfig))
 ifeq (yes,$(build-shared))
@@ -78,7 +80,7 @@ others-static	+= ldconfig
 others		+= ldconfig
 install-rootsbin += ldconfig
-ldconfig-modules := cache readlib xmalloc xstrdup chroot_canon
+ldconfig-modules := cache readlib xmalloc xstrdup chroot_canon static-stubs
 extra-objs	+= $(ldconfig-modules:=.o)
@@ -411,6 +413,8 @@ $(objpfx)ldd: $(common-objpfx) \
 $(objpfx)sprof: $(libdl)
+$(objpfx)sln: $(sln-modules:%=$(objpfx)%.o)
 $(objpfx)ldconfig: $(ldconfig-modules:%=$(objpfx)%.o)
 $(objpfx)pldd: $(pldd-modules:%=$(objpfx)%.o)
diff --git a/elf/static-stubs.c b/elf/static-stubs.c
new file mode 100644
index 0000000..6c5eebc
--- /dev/null
+++ b/elf/static-stubs.c
@@ -0,0 +1,46 @@
+/* Stub implementations of functions to link into statically linked
+   programs without needing libgcc_eh.
+   Copyright (C) 2012 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   Lesser General Public License for more details.
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <>.  */
+/* Avoid backtrace (and so _Unwind_Backtrace) dependencies from
+   sysdeps/unix/sysv/linux/libc_fatal.c.  */
+#include <sysdeps/posix/libc_fatal.c>
+#include <stdlib.h>
+#include <unwind.h>
+/* These programs do not use thread cancellation, so _Unwind_Resume
+   and the personality routine are never actually called.  */
+_Unwind_Resume (struct _Unwind_Exception *exc __attribute__ ((unused)))
+  abort ();
+__gcc_personality_v0 (int version __attribute__ ((unused)),
+		      _Unwind_Action actions __attribute__ ((unused)),
+		      _Unwind_Exception_Class exception_class
+		      __attribute__ ((unused)),
+		      struct _Unwind_Exception *ue_header
+		      __attribute__ ((unused)),
+		      struct _Unwind_Context *context __attribute__ ((unused)))
+  abort ();

Joseph S. Myers

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]