* elf/dl-load.c (_dl_dst_count, _dl_dst_substitute): Handle $LIB
[glibc.git] / sysdeps / generic / libc-start.c
blob3c7f2fa6398bea01450734943d2fe4555c0018e3
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 #include <tls.h>
33 #ifndef SHARED
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 /* Result of the 'main' function. */
67 int result;
69 #ifndef SHARED
70 # ifdef HAVE_AUX_VECTOR
71 void *__unbounded *__unbounded auxvec;
72 # endif
74 __libc_multiple_libcs = &_dl_starting_up && !_dl_starting_up;
75 #endif
77 INIT_ARGV_and_ENVIRON;
79 /* Store the lowest stack address. */
80 __libc_stack_end = stack_end;
82 #ifndef SHARED
83 # ifdef HAVE_AUX_VECTOR
84 /* First process the auxiliary vector since we need to find the
85 program header to locate an eventually present PT_TLS entry. */
86 for (auxvec = (void *__unbounded *__unbounded) ubp_ev;
87 *auxvec != NULL; ++auxvec);
88 ++auxvec;
89 _dl_aux_init ((ElfW(auxv_t) *) auxvec);
90 # endif
92 /* Initialize the thread library at least a bit since the libgcc
93 functions are using thread functions if these are available and
94 we need to setup errno. If there is no thread library and we
95 handle TLS the function is defined in the libc to initialized the
96 TLS handling. */
97 # if !(USE_TLS - 0)
98 if (__pthread_initialize_minimal)
99 # endif
100 __pthread_initialize_minimal ();
102 /* Some security at this point. Prevent starting a SUID binary where
103 the standard file descriptors are not opened. We have to do this
104 only for statically linked applications since otherwise the dynamic
105 loader did the work already. */
106 if (__builtin_expect (__libc_enable_secure, 0))
107 __libc_check_standard_fds ();
108 #endif
110 /* Register the destructor of the dynamic linker if there is any. */
111 if (__builtin_expect (rtld_fini != NULL, 1))
112 __cxa_atexit ((void (*) (void *)) rtld_fini, NULL, NULL);
114 /* Call the initializer of the libc. This is only needed here if we
115 are compiling for the static library in which case we haven't
116 run the constructors in `_dl_start_user'. */
117 #ifndef SHARED
118 __libc_init_first (argc, argv, __environ);
119 #endif
121 /* Register the destructor of the program, if any. */
122 if (fini)
123 __cxa_atexit ((void (*) (void *)) fini, NULL, NULL);
125 /* Call the initializer of the program, if any. */
126 #ifdef SHARED
127 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_IMPCALLS, 0))
128 _dl_debug_printf ("\ninitialize program: %s\n\n", argv[0]);
129 #endif
130 if (init)
131 (*init) ();
133 #ifdef SHARED
134 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_IMPCALLS, 0))
135 _dl_debug_printf ("\ntransferring control: %s\n\n", argv[0]);
136 #endif
138 #ifdef HAVE_CANCELBUF
139 if (setjmp (THREAD_SELF->cancelbuf) == 0)
140 #endif
142 /* XXX This is where the try/finally handling must be used. */
144 result = main (argc, argv, __environ);
146 #ifdef HAVE_CANCELBUF
147 else
148 /* Not much left to do but to exit the thread, not the process. */
149 __exit_thread (0);
150 #endif
152 exit (result);