From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 11453 invoked by alias); 29 Oct 2002 06:27:50 -0000 Mailing-List: contact libc-hacker-help@sources.redhat.com; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-hacker-owner@sources.redhat.com Received: (qmail 11409 invoked from network); 29 Oct 2002 06:27:50 -0000 Received: from unknown (HELO rwcrmhc51.attbi.com) (204.127.198.38) by sources.redhat.com with SMTP; 29 Oct 2002 06:27:50 -0000 Received: from lucon.org ([12.234.88.146]) by rwcrmhc51.attbi.com (InterMail vM.4.01.03.27 201-229-121-127-20010626) with ESMTP id <20021029062749.NQPR24071.rwcrmhc51.attbi.com@lucon.org>; Tue, 29 Oct 2002 06:27:49 +0000 Received: by lucon.org (Postfix, from userid 1000) id E5ED12C4EC; Mon, 28 Oct 2002 22:27:48 -0800 (PST) Date: Mon, 28 Oct 2002 22:34:00 -0000 From: "H. J. Lu" To: davidm@hpl.hp.com Cc: libc-hacker@sources.redhat.com, davidm@napali.hpl.hp.com Subject: Re: patch to make init_array work (2nd version) Message-ID: <20021028222748.A17290@lucon.org> References: <15806.7806.501878.610194@napali.hpl.hp.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="r5Pyd7+fXNt84Ff3" Content-Disposition: inline User-Agent: Mutt/1.2.5.1i In-Reply-To: <15806.7806.501878.610194@napali.hpl.hp.com>; from davidm@napali.hpl.hp.com on Mon, Oct 28, 2002 at 09:37:02PM -0800 X-SW-Source: 2002-10/txt/msg00105.txt.bz2 --r5Pyd7+fXNt84Ff3 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-length: 622 On Mon, Oct 28, 2002 at 09:37:02PM -0800, David Mosberger wrote: > My previous patch (adopted from HJ's work) was missing a change to > dl-fini.c which broke the handling of .fini_array. The patch below is > updated to fix that and "make check" now passes as expected (I must > have made a mistake previously as I thought it worked with the > previous patch already). > > If it looks OK, please apply. (Yes, I know both Uli and Roland are > out this week, but I'll likely be out next week...). > I am sorry if I didn't send out my latest patch. David, could you please check out the one enclosed here. Thanks. H.J. --r5Pyd7+fXNt84Ff3 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="glibc-array.patch" Content-length: 12862 2002-03-12 H.J. Lu * config.h.in (HAVE_INITFINI_ARRAY): New. * config.make.in (have-initfini-array): New. * configure.in: Check .preinit_array/.init_array/.fini_array support in ld. * configure: Rebuild. * csu/init.c (__libc_preinit_array): New. Define if HAVE_INITFINI_ARRAY is defined. (__libc_init_array): Likewise. (__libc_fini_array): Likewise. * elf/Makefile (tests): Add tst-array1, tst-array2 and tst-array3 if $(have-initfini-array) is yes. (tests-static): Add tst-array3 if $(have-initfini-array) is yes. (modules-names): Add tst-array2dep if $(have-initfini-array) is yes. ($(objpfx)tst-array1.out): New target. ($(objpfx)tst-array2): Likewise. ($(objpfx)tst-array2.out): Likewise. ($(objpfx)tst-array3.out): Likewise. * elf/dl-fini.c (_dl_fini): Fix calling the fini array. * elf/dl-init.c (_dl_init): Fix calling the preinit array. * elf/tst-array1.c: New file. * elf/tst-array1.exp: Likewise. * elf/tst-array2.c: Likewise. * elf/tst-array2dep.c: Likewise. * elf/tst-array2.exp: Likewise. * elf/tst-array3.c: Likewise. * sysdeps/generic/libc-start.c (__libc_start_main): Process __libc_preinit_array, __libc_init_array and __libc_fini_array if HAVE_INITFINI_ARRAY is defined. --- libc/config.h.in.array Mon Mar 11 23:26:51 2002 +++ libc/config.h.in Tue Mar 12 10:47:15 2002 @@ -96,6 +96,10 @@ certain registers (CR0, MQ, CTR, LR) in asm statements. */ #undef BROKEN_PPC_ASM_CR0 +/* Define if the linker supports .preinit_array/.init_array/.fini_array + sections. */ +#undef HAVE_INITFINI_ARRAY + /* Define if the linker supports the -z combreloc option. */ #undef HAVE_Z_COMBRELOC --- libc/config.make.in.array Tue Mar 12 10:24:37 2002 +++ libc/config.make.in Tue Mar 12 17:15:31 2002 @@ -36,6 +36,7 @@ sysincludes = @SYSINCLUDES@ all-warnings = @all_warnings@ elf = @elf@ +have-initfini-array = @libc_cv_initfinit_array@ have-protected = @libc_cv_asm_protected_directive@ have-z-nodelete = @libc_cv_z_nodelete@ have-z-nodlopen = @libc_cv_z_nodlopen@ --- libc/configure.in.array Tue Mar 12 10:24:38 2002 +++ libc/configure.in Tue Mar 12 17:14:36 2002 @@ -1050,6 +1050,36 @@ EOF fi fi + AC_CACHE_CHECK(for .preinit_array/.init_array/.fini_array support, + libc_cv_initfinit_array, [dnl + cat > conftest.c <&AC_FD_CC]) + then + libc_cv_initfinit_array=yes + else + libc_cv_initfinit_array=no + fi + rm -f conftest*]) + AC_SUBST(libc_cv_initfinit_array) + if test $libc_cv_initfinit_array = yes; then + AC_DEFINE(HAVE_INITFINI_ARRAY) + fi + AC_CACHE_CHECK(for -z nodelete option, libc_cv_z_nodelete, [dnl cat > conftest.c <= 0; i--) + __fini_array_start [i] (); +} +#endif --- libc/elf/Makefile.array Tue Mar 12 10:24:37 2002 +++ libc/elf/Makefile Tue Mar 12 17:17:17 2002 @@ -120,6 +120,10 @@ tests = loadtest restest1 preloadtest lo neededtest3 neededtest4 unload2 lateglobal initfirst global \ restest2 next dblload dblunload reldep5 reldep6 tst-tls1 tst-tls2 \ tst-tls3 tst-tls4 tst-tls5 tst-tls6 tst-tls7 tst-tls8 +ifeq (yes,$(have-initfini-array)) +tests += tst-array1 tst-array2 tst-array3 +tests-static += tst-array3 +endif test-srcs = tst-pathopt tests-vis-yes = vismain tests-nodelete-yes = nodelete @@ -138,6 +142,9 @@ modules-names = testobj1 testobj2 testob dblloadmod1 dblloadmod2 dblloadmod3 reldepmod5 reldepmod6 \ reldep6mod0 reldep6mod1 reldep6mod2 reldep6mod3 reldep6mod4 \ tst-tlsmod1 tst-tlsmod2 tst-tlsmod3 tst-tlsmod4 +ifeq (yes,$(have-initfini-array)) +modules-names += tst-array2dep +endif modules-vis-yes = vismod1 vismod2 vismod3 modules-nodelete-yes = nodelmod1 nodelmod2 nodelmod3 nodelmod4 modules-nodlopen-yes = nodlopenmod nodlopenmod2 @@ -466,3 +473,20 @@ $(objpfx)tst-tls7.out: $(objpfx)tst-tlsm $(objpfx)tst-tls8: $(libdl) $(objpfx)tst-tls8.out: $(objpfx)tst-tlsmod3.so $(objpfx)tst-tlsmod4.so + +$(objpfx)tst-array1.out: $(objpfx)tst-array1 + $(elf-objpfx)$(rtld-installed-name) \ + --library-path $(rpath-link)$(patsubst %,:%,$(sysdep-library-path)) \ + $(objpfx)tst-array1 > $@ + cmp $@ tst-array1.exp > /dev/null + +$(objpfx)tst-array2: $(objpfx)tst-array2dep.so +$(objpfx)tst-array2.out: $(objpfx)tst-array2 + $(elf-objpfx)$(rtld-installed-name) \ + --library-path $(rpath-link)$(patsubst %,:%,$(sysdep-library-path)) \ + $(objpfx)tst-array2 > $@ + cmp $@ tst-array2.exp > /dev/null + +$(objpfx)tst-array3.out: $(objpfx)tst-array3 + $(objpfx)tst-array3 > $@ + cmp $@ tst-array1.exp > /dev/null --- libc/elf/dl-fini.c.array Fri Mar 1 22:57:50 2002 +++ libc/elf/dl-fini.c Tue Mar 12 12:21:27 2002 @@ -157,12 +157,11 @@ _dl_fini (void) ElfW(Addr) *array = (ElfW(Addr) *) (l->l_addr + l->l_info[DT_FINI_ARRAY]->d_un.d_ptr); - unsigned int sz = (l->l_info[DT_FINI_ARRAYSZ]->d_un.d_val - / sizeof (ElfW(Addr))); - unsigned int cnt; + int cnt = (l->l_info[DT_FINI_ARRAYSZ]->d_un.d_val + / sizeof (ElfW(Addr))) - 1; - for (cnt = 0; cnt < sz; ++cnt) - ((fini_t) (l->l_addr + array[cnt])) (); + for (; cnt >= 0; --cnt) + ((fini_t) array[cnt]) (); } /* Next try the old-style destructor. */ --- libc/elf/dl-init.c.array Fri Mar 1 22:57:50 2002 +++ libc/elf/dl-init.c Tue Mar 12 12:00:47 2002 @@ -101,7 +101,8 @@ _dl_init (struct link_map *main_map, int /* Don't do anything if there is no preinit array. */ if (__builtin_expect (preinit_array != NULL, 0) - && (i = preinit_array->d_un.d_val / sizeof (ElfW(Addr))) > 0) + && (i = main_map->l_info[DT_PREINIT_ARRAYSZ]->d_un.d_val + / sizeof (ElfW(Addr))) > 0) { ElfW(Addr) *addrs; unsigned int cnt; @@ -111,7 +112,7 @@ _dl_init (struct link_map *main_map, int main_map->l_name[0] ? main_map->l_name : rtld_progname); - addrs = (ElfW(Addr) *) (main_map->l_info[DT_PREINIT_ARRAY]->d_un.d_ptr + addrs = (ElfW(Addr) *) (preinit_array->d_un.d_ptr + main_map->l_addr); for (cnt = 0; cnt < i; ++cnt) ((init_t) addrs[cnt]) (argc, argv, env); --- libc/elf/tst-array1.c.array Tue Mar 12 13:46:09 2002 +++ libc/elf/tst-array1.c Tue Mar 12 15:14:39 2002 @@ -0,0 +1,101 @@ +#include + +static void init (void) __attribute__ ((constructor)); + +static void +init (void) +{ + write (STDOUT_FILENO, "init\n", 5); +} + +static void fini (void) __attribute__ ((destructor)); + +static void +fini (void) +{ + write (STDOUT_FILENO, "fini\n", 5); +} + +static void +preinit_0 (void) +{ + write (STDOUT_FILENO, "preinit array 0\n", 16); +} + +static void +preinit_1 (void) +{ + write (STDOUT_FILENO, "preinit array 1\n", 16); +} + +static void +preinit_2 (void) +{ + write (STDOUT_FILENO, "preinit array 2\n", 16); +} + +void +(*preinit_array []) (void) __attribute__ ((section (".preinit_array"))) = +{ + &preinit_0, + &preinit_1, + &preinit_2 +}; + +static void +init_0 (void) +{ + write (STDOUT_FILENO, "init array 0\n", 13); +} + +static void +init_1 (void) +{ + write (STDOUT_FILENO, "init array 1\n", 13); +} + +static void +init_2 (void) +{ + write (STDOUT_FILENO, "init array 2\n", 13); +} + +void +(*init_array []) (void) __attribute__ ((section (".init_array"))) = +{ + &init_0, + &init_1, + &init_2 +}; + +static void +fini_0 (void) +{ + write (STDOUT_FILENO, "fini array 0\n", 13); +} + +static void +fini_1 (void) +{ + write (STDOUT_FILENO, "fini array 1\n", 13); +} + +static void +fini_2 (void) +{ + write (STDOUT_FILENO, "fini array 2\n", 13); +} + +void +(*fini_array []) (void) __attribute__ ((section (".fini_array"))) = +{ + &fini_0, + &fini_1, + &fini_2 +}; + +int +main (void) +{ + return 0; +} --- libc/elf/tst-array1.exp.array Tue Mar 12 14:50:24 2002 +++ libc/elf/tst-array1.exp Tue Mar 12 15:15:01 2002 @@ -0,0 +1,11 @@ +preinit array 0 +preinit array 1 +preinit array 2 +init +init array 0 +init array 1 +init array 2 +fini array 2 +fini array 1 +fini array 0 +fini --- libc/elf/tst-array2.c.array Tue Mar 12 15:01:13 2002 +++ libc/elf/tst-array2.c Tue Mar 12 15:23:44 2002 @@ -0,0 +1 @@ +#include "tst-array1.c" --- libc/elf/tst-array2.exp.array Tue Mar 12 15:01:19 2002 +++ libc/elf/tst-array2.exp Tue Mar 12 15:16:45 2002 @@ -0,0 +1,19 @@ +preinit array 0 +preinit array 1 +preinit array 2 +DSO init +DSO init array 0 +DSO init array 1 +DSO init array 2 +init +init array 0 +init array 1 +init array 2 +fini array 2 +fini array 1 +fini array 0 +fini +DSO fini array 2 +DSO fini array 1 +DSO fini array 0 +DSO fini --- libc/elf/tst-array2dep.c.array Tue Mar 12 15:05:39 2002 +++ libc/elf/tst-array2dep.c Tue Mar 12 15:16:20 2002 @@ -0,0 +1,69 @@ +#include + +static void init (void) __attribute__ ((constructor)); + +static void +init (void) +{ + write (STDOUT_FILENO, "DSO init\n", 9); +} + +static void fini (void) __attribute__ ((destructor)); + +static void +fini (void) +{ + write (STDOUT_FILENO, "DSO fini\n", 9); +} + +static void +init_0 (void) +{ + write (STDOUT_FILENO, "DSO init array 0\n", 17); +} + +static void +init_1 (void) +{ + write (STDOUT_FILENO, "DSO init array 1\n", 17); +} + +static void +init_2 (void) +{ + write (STDOUT_FILENO, "DSO init array 2\n", 17); +} + +void +(*init_array []) (void) __attribute__ ((section (".init_array"))) = +{ + &init_0, + &init_1, + &init_2 +}; + +static void +fini_0 (void) +{ + write (STDOUT_FILENO, "DSO fini array 0\n", 17); +} + +static void +fini_1 (void) +{ + write (STDOUT_FILENO, "DSO fini array 1\n", 17); +} + +static void +fini_2 (void) +{ + write (STDOUT_FILENO, "DSO fini array 2\n", 17); +} + +void +(*fini_array []) (void) __attribute__ ((section (".fini_array"))) = +{ + &fini_0, + &fini_1, + &fini_2 +}; --- libc/elf/tst-array3.c.array Tue Mar 12 15:25:15 2002 +++ libc/elf/tst-array3.c Tue Mar 12 15:25:05 2002 @@ -0,0 +1 @@ +#include "tst-array1.c" --- libc/sysdeps/generic/libc-start.c.array Mon Feb 4 13:45:13 2002 +++ libc/sysdeps/generic/libc-start.c Tue Mar 12 14:30:57 2002 @@ -33,6 +33,16 @@ extern void *__libc_stack_end; extern void __pthread_initialize_minimal (void) __attribute__ ((weak)); #endif +#ifdef HAVE_INITFINI_ARRAY +# ifdef SHARED +extern void __libc_init_array (void) __attribute__ ((weak)); +extern void __libc_fini_array (void) __attribute__ ((weak)); +# else +extern void __libc_preinit_array (void); +extern void __libc_init_array (void); +extern void __libc_fini_array (void); +# endif +#endif extern int BP_SYM (__libc_start_main) (int (*main) (int, char **, char **), int argc, @@ -113,6 +123,17 @@ BP_SYM (__libc_start_main) (int (*main) if (fini) __cxa_atexit ((void (*) (void *)) fini, NULL, NULL); +#ifdef HAVE_INITFINI_ARRAY +# ifdef SHARED + if (__libc_fini_array) +# endif + __cxa_atexit ((void (*) (void *)) __libc_fini_array, NULL, NULL); + +# ifndef SHARED + __libc_preinit_array (); +# endif +#endif + /* Call the initializer of the program, if any. */ #ifdef SHARED if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_IMPCALLS, 0)) @@ -121,6 +142,13 @@ BP_SYM (__libc_start_main) (int (*main) if (init) (*init) (); +#ifdef HAVE_INITFINI_ARRAY +# ifdef SHARED + if (__libc_init_array) +# endif + __libc_init_array (); +#endif + #ifdef SHARED if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_IMPCALLS, 0)) _dl_debug_printf ("\ntransferring control: %s\n\n", argv[0]); --r5Pyd7+fXNt84Ff3--