summary refs log tree commit diff
path: root/sysdeps
diff options
context:
space:
mode:
Diffstat (limited to 'sysdeps')
-rw-r--r--sysdeps/generic/libc-start.c24
-rw-r--r--sysdeps/i386/elf/start.S51
-rw-r--r--sysdeps/unix/sysv/linux/i386/profil-counter.h4
-rw-r--r--sysdeps/unix/sysv/linux/libc-start.c33
4 files changed, 68 insertions, 44 deletions
diff --git a/sysdeps/generic/libc-start.c b/sysdeps/generic/libc-start.c
index 12419887a1..d6fd3d3581 100644
--- a/sysdeps/generic/libc-start.c
+++ b/sysdeps/generic/libc-start.c
@@ -16,9 +16,29 @@
    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.  */
 
+#include <stdlib.h>
+#include <unistd.h>
+
 int
 __libc_start_main (int (*main) (int, char **, char **), int argc,
-		   char **argv, char **envp)
+		   char **argv, void (*init) (void), void (*fini) (void),
+		   void (*rtld_fini) (void))
 {
-  return (*main) (argc, argv, envp);
+  /* Register the destructor of the dynamic linker if there is any.  */
+  if (rtld_fini != NULL)
+    atexit (rtld_fini);
+
+  /* Call the initializer of the libc.  */
+  __libc_init_first ();
+
+  /* Set the global _environ variable correctly.  */
+  __environ = &argv[argc + 1];
+
+  /* Call the initializer of the program.  */
+  (*init) ();
+
+  /* Register the destructor of the program.  */
+  atexit (fini);
+
+  exit ((*main) (argc, argv, envp));
 }
diff --git a/sysdeps/i386/elf/start.S b/sysdeps/i386/elf/start.S
index e8ed2043ff..95f74ed647 100644
--- a/sysdeps/i386/elf/start.S
+++ b/sysdeps/i386/elf/start.S
@@ -43,57 +43,32 @@ _start:
 	   the outermost frame obviously.  */
 	xorl %ebp, %ebp
 
-	/* %edx contains the address of the shared library termination
-	   function, which we will register with `atexit' to be called by
-	   `exit'.  I suspect that on some systems, and when statically
-	   linked, this will not be set by anything to any function
-	   pointer; hopefully it will be zero so we don't try to call
-	   random pointers.  */
-	testl %edx,%edx
-	jz .Lnofini
-	pushl %edx
-	call atexit
-	popl %eax		/* Pop value to unused register to remove
-				   argument from stack.  */
-.Lnofini:
-
-	/* Do essential libc initialization.  In statically linked
-	   programs under the GNU Hurd, this is what sets up the
-	   arguments on the stack for the code below.  */
-	call __libc_init_first
-
-	/* Extract the arguments and environment as encoded on the stack
-	   and set up the arguments for `main': argc, argv, envp.  */
+	/* Extract the arguments as encoded on the stack and set up
+	   the arguments for `main': argc, argv.  envp will be determined
+	   later in __libc_start_main.  */
 	popl %esi		/* Pop the argument count.  */
-	leal 4(%esp,%esi,4), %eax /* envp = &argv[argc + 1] */
-	movl %eax, _environ	/* Store it in the global variable.  */
-	movl %esp, %edx		/* argv starts just at the current stack top.*/
+	movl %esp, %ecx		/* argv starts just at the current stack top.*/
 
 	/* Before pushing the arguments align the stack to a double word
 	   boundary to avoid penalties from misaligned accesses.  Thanks
 	   to Edward Seidl <seidl@janed.com> for pointing this out.  */
 	andl $0xfffffff8, %esp
-	pushl %eax		/* Push garbage because we allocate
-				   twelve more bytes.  */
+	pushl %edx		/* Push address of the shared library
+				   termination function.  */
 
-	pushl %eax		/* Push third argument: envp.  */
-	pushl %edx		/* Push second argument: argv.  */
+	/* Push address of our own entry points to .fini and .init.  */
+	pushl $_fini
+	pushl $_init
+
+	pushl %ecx		/* Push second argument: argv.  */
 	pushl %esi		/* Push first argument: argc.  */
 
-	/* Call `_init', which is the entry point to our own `.init'
-	   section; and register with `atexit' to have `exit' call
-	   `_fini', which is the entry point to our own `.fini' section.  */
-	call _init
-	pushl $_fini
-	call atexit
-	popl %eax
+	pushl $main
 
 	/* Call the user's main function, and exit with its value.
 	   But let the libc call main.    */
-	movl $main, %eax
-	pushl %eax
 	call __libc_start_main
-	call exit
+
 	hlt			/* Crash if somehow `exit' does return.  */
 
 /* To fulfill the System V/i386 ABI we need this symbol.  Yuck, it's so
diff --git a/sysdeps/unix/sysv/linux/i386/profil-counter.h b/sysdeps/unix/sysv/linux/i386/profil-counter.h
index 615371ac35..a24ea19654 100644
--- a/sysdeps/unix/sysv/linux/i386/profil-counter.h
+++ b/sysdeps/unix/sysv/linux/i386/profil-counter.h
@@ -1,5 +1,5 @@
 /* Low-level statistical profiling support function.  Linux/i386 version.
-   Copyright (C) 1996, 1997 Free Software Foundation, Inc.
+   Copyright (C) 1996, 1997, 1998 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
@@ -19,7 +19,7 @@
 
 #include <signal.h>
 
-void
+static void
 profil_counter (int signo, struct sigcontext sc)
 {
   profil_count ((void *) sc.eip);
diff --git a/sysdeps/unix/sysv/linux/libc-start.c b/sysdeps/unix/sysv/linux/libc-start.c
index f142195fbc..e9d21a638b 100644
--- a/sysdeps/unix/sysv/linux/libc-start.c
+++ b/sysdeps/unix/sysv/linux/libc-start.c
@@ -17,15 +17,44 @@
    Boston, MA 02111-1307, USA.  */
 
 #include <link.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+extern void __libc_init_first (void);
 
 int
 __libc_start_main (int (*main) (int, char **, char **), int argc,
-		   char **argv, char **envp)
+		   char **argv, void (*init) (void), void (*fini) (void),
+		   void (*rtld_fini) (void))
 {
+  /* Register the destructor of the dynamic linker if there is any.  */
+  if (rtld_fini != NULL)
+    atexit (rtld_fini);
+
+  /* Call the initializer of the libc.  */
+#ifdef PIC
+  if (_dl_debug_impcalls)
+    _dl_debug_message ("\tinitialize libc\n\n", NULL);
+#endif
+  __libc_init_first ();
+
+  /* Set the global _environ variable correctly.  */
+  __environ = &argv[argc + 1];
+
+  /* Call the initializer of the program.  */
+#ifdef PIC
+  if (_dl_debug_impcalls)
+    _dl_debug_message ("\tinitialize program: ", argv[0], "\n\n", NULL);
+#endif
+  (*init) ();
+
+  /* Register the destructor of the program.  */
+  atexit (fini);
+
 #ifdef PIC
   if (_dl_debug_impcalls)
     _dl_debug_message ("\ttransferring control: ", argv[0], "\n\n", NULL);
 #endif
 
-  return (*main) (argc, argv, envp);
+  exit ((*main) (argc, argv, __environ));
 }