2 * Copyright (C) Feb 2001 Manuel Novoa III
3 * Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
5 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
7 * __uClibc_main is the routine to be called by all the arch-specific
8 * versions of crt1.S in uClibc.
10 * It is meant to handle any special initialization needed by the library
11 * such as setting the global variable(s) __environ (environ) and
12 * initializing the stdio package. Using weak symbols, the latter is
13 * avoided in the static library case.
23 #include <bits/uClibc_page.h>
25 #include <asm/errno.h>
28 #include <sys/sysmacros.h>
32 void *__libc_stack_end = NULL;
34 # ifdef __UCLIBC_HAS_SSP__
35 # include <dl-osinfo.h>
36 # ifndef THREAD_SET_STACK_GUARD
37 /* Only exported for architectures that don't store the stack guard canary
38 * in thread local area. */
40 uintptr_t stack_chk_guard;
41 /* for gcc-4.1 non-TLS */
42 uintptr_t __stack_chk_guard attribute_relro;
43 /* for gcc-3.x + Etoh ssp */
44 # ifdef __UCLIBC_HAS_SSP_COMPAT__
45 # ifdef __HAVE_SHARED__
46 strong_alias(__stack_chk_guard,__guard)
48 uintptr_t __guard attribute_relro;
51 # elif defined __UCLIBC_HAS_SSP_COMPAT__
52 uintptr_t __guard attribute_relro;
57 * Needed to initialize _dl_phdr when statically linked
60 void internal_function _dl_aux_init (ElfW(auxv_t) *av);
66 extern int *weak_const_function __errno_location(void);
67 libc_hidden_proto(__errno_location)
68 extern int *weak_const_function __h_errno_location(void);
69 libc_hidden_proto(__h_errno_location)
71 extern void weak_function _stdio_init(void) attribute_hidden;
72 #ifdef __UCLIBC_HAS_LOCALE__
73 extern void weak_function _locale_init(void) attribute_hidden;
75 #ifdef __UCLIBC_HAS_THREADS__
76 extern void weak_function __pthread_initialize_minimal(void);
79 /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation and finalisation
80 * is handled by the routines passed to __uClibc_main(). */
81 #if defined (__UCLIBC_CTOR_DTOR__) && !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
82 extern void _dl_app_init_array(void);
83 extern void _dl_app_fini_array(void);
85 /* These magic symbols are provided by the linker. */
86 extern void (*__preinit_array_start []) (void) attribute_hidden;
87 extern void (*__preinit_array_end []) (void) attribute_hidden;
88 extern void (*__init_array_start []) (void) attribute_hidden;
89 extern void (*__init_array_end []) (void) attribute_hidden;
90 extern void (*__fini_array_start []) (void) attribute_hidden;
91 extern void (*__fini_array_end []) (void) attribute_hidden;
95 attribute_hidden const char *__uclibc_progname = "";
96 #ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
97 const char *program_invocation_short_name = "";
98 const char *program_invocation_name = "";
100 #ifdef __UCLIBC_HAS___PROGNAME__
101 weak_alias (program_invocation_short_name, __progname)
102 weak_alias (program_invocation_name, __progname_full)
106 * Declare the __environ global variable and create a weak alias environ.
107 * This must be initialized; we cannot have a weak alias into bss.
109 char **__environ = 0;
110 weak_alias(__environ, environ)
112 /* TODO: don't export __pagesize; we cant now because libpthread uses it */
113 size_t __pagesize = 0;
116 # define O_NOFOLLOW 0
119 #ifndef __ARCH_HAS_NO_LDSO__
120 static void __check_one_fd(int fd, int mode)
122 /* Check if the specified fd is already open */
123 if (fcntl(fd, F_GETFD) == -1)
125 /* The descriptor is probably not open, so try to use /dev/null */
126 int nullfd = open(_PATH_DEVNULL, mode);
127 /* /dev/null is major=1 minor=3. Make absolutely certain
128 * that is in fact the device that we have opened and not
129 * some other weird file... [removed in uclibc] */
137 static int __check_suid(void)
150 return 0; /* we are not suid */
154 /* __uClibc_init completely initialize uClibc so it is ready to use.
156 * On ELF systems (with a dynamic loader) this function must be called
157 * from the dynamic loader (see TIS and ELF Specification), so that
158 * constructors of shared libraries (which depend on libc) can use all
159 * the libc code without restriction. For this we link the shared
160 * version of the uClibc with -init __uClibc_init so DT_INIT for
161 * uClibc is the address of __uClibc_init
163 * In all other cases we call it from the main stub
167 extern void __uClibc_init(void);
168 libc_hidden_proto(__uClibc_init)
169 void __uClibc_init(void)
171 static smallint been_there_done_that;
173 if (been_there_done_that)
175 been_there_done_that++;
177 /* Setup an initial value. This may not be perfect, but is
178 * better than malloc using __pagesize=0 for atexit, ctors, etc. */
179 __pagesize = PAGE_SIZE;
181 #ifdef __UCLIBC_HAS_THREADS__
182 /* Before we start initializing uClibc we have to call
183 * __pthread_initialize_minimal so we can use pthread_locks
184 * whenever they are needed.
186 if (likely(__pthread_initialize_minimal!=NULL))
187 __pthread_initialize_minimal();
191 # ifdef __UCLIBC_HAS_SSP__
192 /* Set up the stack checker's canary. */
193 stack_chk_guard = _dl_setup_stack_chk_guard();
194 # ifdef THREAD_SET_STACK_GUARD
195 THREAD_SET_STACK_GUARD (stack_chk_guard);
196 # ifdef __UCLIBC_HAS_SSP_COMPAT__
197 __guard = stack_chk_guard;
200 __stack_chk_guard = stack_chk_guard;
201 # if !defined __HAVE_SHARED__ && defined __UCLIBC_HAS_SSP_COMPAT__
202 __guard = stack_chk_guard;
208 #ifdef __UCLIBC_HAS_LOCALE__
209 /* Initialize the global locale structure. */
210 if (likely(_locale_init!=NULL))
215 * Initialize stdio here. In the static library case, this will
216 * be bypassed if not needed because of the weak alias above.
217 * Thus we get a nice size savings because the stdio functions
218 * won't be pulled into the final static binary unless used.
220 if (likely(_stdio_init != NULL))
224 libc_hidden_def(__uClibc_init)
226 #ifdef __UCLIBC_CTOR_DTOR__
227 void attribute_hidden (*__app_fini)(void) = NULL;
230 void attribute_hidden (*__rtld_fini)(void) = NULL;
232 extern void __uClibc_fini(void);
233 libc_hidden_proto(__uClibc_fini)
234 void __uClibc_fini(void)
236 #ifdef __UCLIBC_CTOR_DTOR__
237 /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array finalisation is handled
240 _dl_app_fini_array();
241 # elif !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
242 size_t i = __fini_array_end - __fini_array_start;
244 (*__fini_array_start [i]) ();
246 if (__app_fini != NULL)
249 if (__rtld_fini != NULL)
252 libc_hidden_def(__uClibc_fini)
254 /* __uClibc_main is the new main stub for uClibc. This function is
255 * called from crt1 (version 0.9.28 or newer), after ALL shared libraries
256 * are initialized, just before we call the application's main function.
258 void __uClibc_main(int (*main)(int, char **, char **), int argc,
259 char **argv, void (*app_init)(void), void (*app_fini)(void),
260 void (*rtld_fini)(void), void *stack_end) attribute_noreturn;
261 void __uClibc_main(int (*main)(int, char **, char **), int argc,
262 char **argv, void (*app_init)(void), void (*app_fini)(void),
263 void (*rtld_fini)(void), void *stack_end)
265 #ifndef __ARCH_HAS_NO_LDSO__
266 unsigned long *aux_dat;
267 ElfW(auxv_t) auxvt[AT_EGID + 1];
271 __libc_stack_end = stack_end;
274 __rtld_fini = rtld_fini;
276 /* The environment begins right after argv. */
277 __environ = &argv[argc + 1];
279 /* If the first thing after argv is the arguments
280 * the the environment is empty. */
281 if ((char *) __environ == *argv) {
282 /* Make __environ point to the NULL at argv[argc] */
283 __environ = &argv[argc];
286 #ifndef __ARCH_HAS_NO_LDSO__
287 /* Pull stuff from the ELF header when possible */
288 memset(auxvt, 0x00, sizeof(auxvt));
289 aux_dat = (unsigned long*)__environ;
295 ElfW(auxv_t) *auxv_entry = (ElfW(auxv_t) *) aux_dat;
296 if (auxv_entry->a_type <= AT_EGID) {
297 memcpy(&(auxvt[auxv_entry->a_type]), auxv_entry, sizeof(ElfW(auxv_t)));
302 /* Get the program headers (_dl_phdr) from the aux vector
303 It will be used into __libc_setup_tls. */
305 _dl_aux_init (auxvt);
309 /* We need to initialize uClibc. If we are dynamically linked this
310 * may have already been completed by the shared lib loader. We call
311 * __uClibc_init() regardless, to be sure the right thing happens. */
314 #ifndef __ARCH_HAS_NO_LDSO__
315 /* Make certain getpagesize() gives the correct answer */
316 __pagesize = (auxvt[AT_PAGESZ].a_un.a_val)? auxvt[AT_PAGESZ].a_un.a_val : PAGE_SIZE;
318 /* Prevent starting SUID binaries where the stdin. stdout, and
319 * stderr file descriptors are not already opened. */
320 if ((auxvt[AT_UID].a_un.a_val == (size_t)-1 && __check_suid()) ||
321 (auxvt[AT_UID].a_un.a_val != (size_t)-1 &&
322 (auxvt[AT_UID].a_un.a_val != auxvt[AT_EUID].a_un.a_val ||
323 auxvt[AT_GID].a_un.a_val != auxvt[AT_EGID].a_un.a_val)))
325 __check_one_fd (STDIN_FILENO, O_RDONLY | O_NOFOLLOW);
326 __check_one_fd (STDOUT_FILENO, O_RDWR | O_NOFOLLOW);
327 __check_one_fd (STDERR_FILENO, O_RDWR | O_NOFOLLOW);
331 __uclibc_progname = *argv;
332 #ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
334 program_invocation_name = *argv;
335 program_invocation_short_name = strrchr(*argv, '/');
336 if (program_invocation_short_name != NULL)
337 ++program_invocation_short_name;
339 program_invocation_short_name = program_invocation_name;
343 #ifdef __UCLIBC_CTOR_DTOR__
344 /* Arrange for the application's dtors to run before we exit. */
345 __app_fini = app_fini;
347 /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation is handled
349 # if !defined (SHARED) && !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
350 /* For dynamically linked executables the preinit array is executed by
351 the dynamic linker (before initializing any shared object).
352 For static executables, preinit happens rights before init. */
354 const size_t size = __preinit_array_end - __preinit_array_start;
356 for (i = 0; i < size; i++)
357 (*__preinit_array_start [i]) ();
360 /* Run all the application's ctors now. */
361 if (app_init!=NULL) {
364 /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation is handled
367 _dl_app_init_array();
368 # elif !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
370 const size_t size = __init_array_end - __init_array_start;
372 for (i = 0; i < size; i++)
373 (*__init_array_start [i]) ();
378 /* Note: It is possible that any initialization done above could
379 * have resulted in errno being set nonzero, so set it to 0 before
382 if (likely(__errno_location!=NULL))
383 *(__errno_location()) = 0;
385 /* Set h_errno to 0 as well */
386 if (likely(__h_errno_location!=NULL))
387 *(__h_errno_location()) = 0;
390 * Finally, invoke application's main and then exit.
392 exit(main(argc, argv, __environ));
395 #if defined(__UCLIBC_HAS_THREADS__) && !defined(SHARED)
396 /* Weaks for internal library use only.
398 * We need to define weaks here to cover all the pthread functions that
399 * libc itself will use so that we aren't forced to link libc against
400 * libpthread. This file is only used in libc.a and since we have
401 * weaks here, they will be automatically overridden by libpthread.a
402 * if it gets linked in.
405 static int __pthread_return_0 (void) { return 0; }
406 static void __pthread_return_void (void) { return; }
408 weak_alias (__pthread_return_0, __pthread_mutex_init)
409 weak_alias (__pthread_return_0, __pthread_mutex_lock)
410 weak_alias (__pthread_return_0, __pthread_mutex_trylock)
411 weak_alias (__pthread_return_0, __pthread_mutex_unlock)
412 weak_alias (__pthread_return_void, _pthread_cleanup_push_defer)
413 weak_alias (__pthread_return_void, _pthread_cleanup_pop_restore)
414 # ifdef __UCLIBC_HAS_THREADS_NATIVE__
415 weak_alias (__pthread_return_0, __pthread_mutexattr_init)
416 weak_alias (__pthread_return_0, __pthread_mutexattr_destroy)
417 weak_alias (__pthread_return_0, __pthread_mutexattr_settype)