public inbox for libc-hacker@sourceware.org
 help / color / mirror / Atom feed
From: David Mosberger <davidm@napali.hpl.hp.com>
To: libc-hacker@sources.redhat.com
Cc: hjl@lucon.org, davidm@napali.hpl.hp.com
Subject: patch to make init_array work (2nd version; resend)
Date: Thu, 07 Nov 2002 11:43:00 -0000	[thread overview]
Message-ID: <15818.49752.477001.832523@napali.hpl.hp.com> (raw)

[I'm resending this patch since there has been no response to the original
 mail on October 28th.  I'd appreciate it if someone could let me know if
 something is wrong with the patch.  If not, please apply.  The patch will
 be helpful for any platform that needs/wants correct unwind info for
 constructors/destructors.  Thanks.]

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...).

Thanks,

	--david

2002-10-28  David Mosberger  <davidm@hpl.hp.com>

	* elf/dl-fini.c (_dl_fini): Invoke fini_array in _reverse_ order.
	Don't add l->l_addr to array entry values.

2002-03-12  H.J. Lu  <hjl@gnu.org>

	* 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/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.

Index: csu/init.c
===================================================================
RCS file: /cvs/glibc/libc/csu/init.c,v
retrieving revision 1.4
diff -u -r1.4 init.c
--- csu/csu/init.c	6 Jul 2001 04:54:45 -0000	1.4
+++ csu/csu/init.c	29 Oct 2002 05:35:15 -0000
@@ -25,3 +25,43 @@
 const int _IO_stdin_used = _G_IO_IO_FILE_VERSION;
 
 #endif
+
+#ifdef HAVE_INITFINI_ARRAY
+extern void (*__preinit_array_start []) (void);
+extern void (*__preinit_array_end []) (void);
+
+void
+__libc_preinit_array (void)
+{
+  unsigned int size, i;
+
+  size = __preinit_array_end - __preinit_array_start;
+  for (i = 0; i < size; i++)
+    __preinit_array_start [i] ();
+}
+
+extern void (*__init_array_start []) (void);
+extern void (*__init_array_end []) (void);
+
+void
+__libc_init_array (void)
+{
+  unsigned int size, i;
+
+  size =  __init_array_end - __init_array_start;
+  for (i = 0; i < size; i++)
+    __init_array_start [i] ();
+}
+
+extern void (*__fini_array_start []) (void);
+extern void (*__fini_array_end []) (void);
+
+void
+__libc_fini_array (void)
+{
+  int i;
+
+  for (i = __fini_array_end - __fini_array_start - 1; i >= 0; i--)
+    __fini_array_start [i] ();
+}
+#endif
Index: elf/Makefile
===================================================================
RCS file: /cvs/glibc/libc/elf/Makefile,v
retrieving revision 1.242
diff -u -r1.242 Makefile
--- elf/elf/Makefile	24 Oct 2002 00:20:38 -0000	1.242
+++ elf/elf/Makefile	29 Oct 2002 05:35:15 -0000
@@ -117,6 +117,9 @@
 endif
 
 tests = tst-tls1 tst-tls2 tst-tls9
+ifeq (yes,$(have-initfini-array))
+tests += tst-array1 tst-array2 tst-array3
+endif
 ifeq (yes,$(build-static))
 tests-static = tst-tls1-static tst-tls2-static
 ifeq (yesyesyes,$(build-static)$(build-shared)$(elf))
@@ -156,6 +159,9 @@
 		tst-tlsmod5 tst-tlsmod6 \
 		circlemod1 circlemod1a circlemod2 circlemod2a \
 		circlemod3 circlemod3a
+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
@@ -543,3 +549,22 @@
 $(objpfx)tst-tls9-static: $(common-objpfx)dlfcn/libdl.a
 $(objpfx)tst-tls9-static.out: $(objpfx)tst-tlsmod5.so $(objpfx)tst-tlsmod6.so
 endif
+
+$(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
+	$(elf-objpfx)$(rtld-installed-name) \
+	  --library-path $(rpath-link)$(patsubst %,:%,$(sysdep-library-path)) \
+	  $(objpfx)tst-array3 > $@
+	cmp $@ tst-array1.exp > /dev/null
Index: elf/dl-fini.c
===================================================================
RCS file: /cvs/glibc/libc/elf/dl-fini.c,v
retrieving revision 1.27
diff -u -r1.27 dl-fini.c
--- elf/elf/dl-fini.c	1 Mar 2002 09:11:58 -0000	1.27
+++ elf/elf/dl-fini.c	29 Oct 2002 05:35:15 -0000
@@ -157,12 +157,11 @@
 	      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 i = (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 (; i >= 0; --i)
+		((fini_t) array[i]) ();
 	    }
 
 	  /* Next try the old-style destructor.  */
Index: elf/tst-array1.c
===================================================================
RCS file: elf/tst-array1.c
diff -N elf/tst-array1.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ elf/elf/tst-array1.c	29 Oct 2002 05:35:15 -0000
@@ -0,0 +1,101 @@
+#include <unistd.h>
+
+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;
+}
Index: elf/tst-array1.exp
===================================================================
RCS file: elf/tst-array1.exp
diff -N elf/tst-array1.exp
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ elf/elf/tst-array1.exp	29 Oct 2002 05:35:15 -0000
@@ -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
Index: elf/tst-array2.c
===================================================================
RCS file: elf/tst-array2.c
diff -N elf/tst-array2.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ elf/elf/tst-array2.c	29 Oct 2002 05:35:15 -0000
@@ -0,0 +1 @@
+#include "tst-array1.c"
Index: elf/tst-array2.exp
===================================================================
RCS file: elf/tst-array2.exp
diff -N elf/tst-array2.exp
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ elf/elf/tst-array2.exp	29 Oct 2002 05:35:15 -0000
@@ -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
Index: elf/tst-array2dep.c
===================================================================
RCS file: elf/tst-array2dep.c
diff -N elf/tst-array2dep.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ elf/elf/tst-array2dep.c	29 Oct 2002 05:35:15 -0000
@@ -0,0 +1,69 @@
+#include <unistd.h>
+
+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
+};
Index: elf/tst-array3.c
===================================================================
RCS file: elf/tst-array3.c
diff -N elf/tst-array3.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ elf/elf/tst-array3.c	29 Oct 2002 05:35:15 -0000
@@ -0,0 +1 @@
+#include "tst-array1.c"
Index: sysdeps/generic/libc-start.c
===================================================================
RCS file: /cvs/glibc/libc/sysdeps/generic/libc-start.c,v
retrieving revision 1.33
diff -u -r1.33 libc-start.c
--- sysdeps/generic/sysdeps/generic/libc-start.c	25 Oct 2002 19:41:24 -0000	1.33
+++ sysdeps/generic/sysdeps/generic/libc-start.c	29 Oct 2002 05:35:16 -0000
@@ -36,6 +36,16 @@
      ;
 #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,
@@ -115,6 +125,17 @@
   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))
@@ -122,6 +143,13 @@
 #endif
   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))

             reply	other threads:[~2002-11-07 19:43 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-11-07 11:43 David Mosberger [this message]
2002-11-07 13:31 ` Roland McGrath
2002-11-07 13:51   ` David Mosberger
2002-11-07 13:58     ` Roland McGrath
2002-11-07 14:10       ` David Mosberger
2002-11-07 14:29         ` Roland McGrath
2002-11-07 14:38           ` David Mosberger
2002-11-07 18:10       ` Richard Henderson
2002-11-07 18:20         ` Roland McGrath
2002-11-07 18:30           ` David Mosberger
2002-11-07 18:45             ` Roland McGrath
2002-11-08 11:34 Roland McGrath
2002-11-08 11:38 ` David Mosberger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=15818.49752.477001.832523@napali.hpl.hp.com \
    --to=davidm@napali.hpl.hp.com \
    --cc=davidm@hpl.hp.com \
    --cc=hjl@lucon.org \
    --cc=libc-hacker@sources.redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).