Include <sys/types.h> instead of <stddef.h> since ssize_t is needed as well. Replace...
[glibc.git] / sysdeps / generic / libc-start.c
blob8d523e6836d604721cce128ef51bb92e063a31fe
1 /* Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <ldsodefs.h>
22 #include <bp-start.h>
23 #include <bp-sym.h>
25 extern void __libc_init_first (int argc, char **argv, char **envp);
27 extern int _dl_starting_up;
28 weak_extern (_dl_starting_up)
29 extern int __libc_multiple_libcs;
30 extern void *__libc_stack_end;
32 #ifndef SHARED
33 # include <tls.h>
34 extern void __pthread_initialize_minimal (void)
35 # if !(USE_TLS - 0)
36 __attribute__ ((weak))
37 # endif
39 #endif
42 extern int BP_SYM (__libc_start_main) (int (*main) (int, char **, char **),
43 int argc,
44 char *__unbounded *__unbounded ubp_av,
45 void (*init) (void),
46 void (*fini) (void),
47 void (*rtld_fini) (void),
48 void *__unbounded stack_end)
49 __attribute__ ((noreturn));
51 int
52 /* GKM FIXME: GCC: this should get __BP_ prefix by virtue of the
53 BPs in the arglist of startup_info.main and startup_info.init. */
54 BP_SYM (__libc_start_main) (int (*main) (int, char **, char **),
55 int argc, char *__unbounded *__unbounded ubp_av,
56 void (*init) (void), void (*fini) (void),
57 void (*rtld_fini) (void), void *__unbounded stack_end)
59 char *__unbounded *__unbounded ubp_ev = &ubp_av[argc + 1];
60 #if __BOUNDED_POINTERS__
61 char **argv;
62 #else
63 # define argv ubp_av
64 #endif
66 #ifndef SHARED
67 # ifdef HAVE_AUX_VECTOR
68 void *__unbounded *__unbounded auxvec;
69 # endif
71 /* The next variable is only here to work around a bug in gcc <= 2.7.2.2.
72 If the address would be taken inside the expression the optimizer
73 would try to be too smart and throws it away. Grrr. */
74 int *dummy_addr = &_dl_starting_up;
76 __libc_multiple_libcs = dummy_addr && !_dl_starting_up;
77 #endif
79 INIT_ARGV_and_ENVIRON;
81 /* Store the lowest stack address. */
82 __libc_stack_end = stack_end;
84 #ifndef SHARED
85 # ifdef HAVE_AUX_VECTOR
86 /* First process the auxiliary vector since we need to find the
87 program header to locate an eventually present PT_TLS entry. */
88 for (auxvec = (void *__unbounded *__unbounded) ubp_ev;
89 *auxvec != NULL; ++auxvec);
90 ++auxvec;
91 _dl_aux_init ((ElfW(auxv_t) *) auxvec);
92 # endif
94 /* Initialize the thread library at least a bit since the libgcc
95 functions are using thread functions if these are available and
96 we need to setup errno. If there is no thread library and we
97 handle TLS the function is defined in the libc to initialized the
98 TLS handling. */
99 # if !(USE_TLS - 0)
100 if (__pthread_initialize_minimal)
101 # endif
102 __pthread_initialize_minimal ();
104 /* Some security at this point. Prevent starting a SUID binary where
105 the standard file descriptors are not opened. We have to do this
106 only for statically linked applications since otherwise the dynamic
107 loader did the work already. */
108 if (__builtin_expect (__libc_enable_secure, 0))
109 __libc_check_standard_fds ();
110 #endif
112 /* Register the destructor of the dynamic linker if there is any. */
113 if (__builtin_expect (rtld_fini != NULL, 1))
114 __cxa_atexit ((void (*) (void *)) rtld_fini, NULL, NULL);
116 /* Call the initializer of the libc. This is only needed here if we
117 are compiling for the static library in which case we haven't
118 run the constructors in `_dl_start_user'. */
119 #ifndef SHARED
120 __libc_init_first (argc, argv, __environ);
121 #endif
123 /* Register the destructor of the program, if any. */
124 if (fini)
125 __cxa_atexit ((void (*) (void *)) fini, NULL, NULL);
127 /* Call the initializer of the program, if any. */
128 #ifdef SHARED
129 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_IMPCALLS, 0))
130 _dl_debug_printf ("\ninitialize program: %s\n\n", argv[0]);
131 #endif
132 if (init)
133 (*init) ();
135 #ifdef SHARED
136 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_IMPCALLS, 0))
137 _dl_debug_printf ("\ntransferring control: %s\n\n", argv[0]);
138 #endif
140 exit ((*main) (argc, argv, __environ));