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>
30 /* libc_hidden_proto(exit) */
32 #ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
33 /* Experimentally off - libc_hidden_proto(strrchr) */
35 #ifndef __ARCH_HAS_NO_LDSO__
36 /* Experimentally off - libc_hidden_proto(memcpy) */
37 libc_hidden_proto(getgid)
38 libc_hidden_proto(getuid)
39 libc_hidden_proto(getegid)
40 libc_hidden_proto(geteuid)
41 libc_hidden_proto(fstat)
42 /* libc_hidden_proto(abort) */
44 extern __typeof(open) __libc_open;
45 libc_hidden_proto(__libc_open)
46 extern __typeof(fcntl) __libc_fcntl;
47 libc_hidden_proto(__libc_fcntl)
51 void *__libc_stack_end=NULL;
53 # ifdef __UCLIBC_HAS_SSP__
54 # include <dl-osinfo.h>
55 # ifndef THREAD_SET_STACK_GUARD
56 /* Only exported for architectures that don't store the stack guard canary
57 * in thread local area. */
59 uintptr_t stack_chk_guard;
60 /* for gcc-4.1 non-TLS */
61 uintptr_t __stack_chk_guard attribute_relro;
62 /* for gcc-3.x + Etoh ssp */
63 # ifdef __UCLIBC_HAS_SSP_COMPAT__
64 # ifdef __HAVE_SHARED__
65 strong_alias(__stack_chk_guard,__guard)
67 uintptr_t __guard attribute_relro;
70 # elif defined __UCLIBC_HAS_SSP_COMPAT__
71 uintptr_t __guard attribute_relro;
76 * Needed to initialize _dl_phdr when statically linked
79 void internal_function _dl_aux_init (ElfW(auxv_t) *av);
85 extern void weak_function _stdio_init(void) attribute_hidden;
86 extern int *weak_const_function __errno_location(void);
87 extern int *weak_const_function __h_errno_location(void);
88 #ifdef __UCLIBC_HAS_LOCALE__
89 extern void weak_function _locale_init(void) attribute_hidden;
91 #ifdef __UCLIBC_HAS_THREADS__
92 extern void weak_function __pthread_initialize_minimal(void);
95 /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation and finalisation
96 * is handled by the routines passed to __uClibc_main(). */
97 #if defined (__UCLIBC_CTOR_DTOR__) && !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
98 extern void _dl_app_init_array(void);
99 extern void _dl_app_fini_array(void);
101 /* These magic symbols are provided by the linker. */
102 extern void (*__preinit_array_start []) (void) attribute_hidden;
103 extern void (*__preinit_array_end []) (void) attribute_hidden;
104 extern void (*__init_array_start []) (void) attribute_hidden;
105 extern void (*__init_array_end []) (void) attribute_hidden;
106 extern void (*__fini_array_start []) (void) attribute_hidden;
107 extern void (*__fini_array_end []) (void) attribute_hidden;
111 attribute_hidden const char *__uclibc_progname = "";
112 #ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
113 const char *program_invocation_short_name = "";
114 const char *program_invocation_name = "";
116 #ifdef __UCLIBC_HAS___PROGNAME__
117 weak_alias (program_invocation_short_name, __progname)
118 weak_alias (program_invocation_name, __progname_full)
122 * Declare the __environ global variable and create a weak alias environ.
123 * This must be initialized; we cannot have a weak alias into bss.
125 char **__environ = 0;
126 weak_alias(__environ, environ)
128 /* TODO: don't export __pagesize; we cant now because libpthread uses it */
129 size_t __pagesize = 0;
132 # define O_NOFOLLOW 0
135 #ifndef __ARCH_HAS_NO_LDSO__
136 static void __check_one_fd(int fd, int mode)
138 /* Check if the specified fd is already open */
139 if (__libc_fcntl(fd, F_GETFD) == -1)
141 /* The descriptor is probably not open, so try to use /dev/null */
142 int nullfd = __libc_open(_PATH_DEVNULL, mode);
143 /* /dev/null is major=1 minor=3. Make absolutely certain
144 * that is in fact the device that we have opened and not
145 * some other wierd file... */
153 static int __check_suid(void)
166 return 0; /* we are not suid */
170 /* __uClibc_init completely initialize uClibc so it is ready to use.
172 * On ELF systems (with a dynamic loader) this function must be called
173 * from the dynamic loader (see TIS and ELF Specification), so that
174 * constructors of shared libraries (which depend on libc) can use all
175 * the libc code without restriction. For this we link the shared
176 * version of the uClibc with -init __uClibc_init so DT_INIT for
177 * uClibc is the address of __uClibc_init
179 * In all other cases we call it from the main stub
183 extern void __uClibc_init(void);
184 libc_hidden_proto(__uClibc_init)
185 void __uClibc_init(void)
187 static smallint been_there_done_that;
189 if (been_there_done_that)
191 been_there_done_that++;
193 /* Setup an initial value. This may not be perfect, but is
194 * better than malloc using __pagesize=0 for atexit, ctors, etc. */
195 __pagesize = PAGE_SIZE;
197 #ifdef __UCLIBC_HAS_THREADS__
198 /* Before we start initializing uClibc we have to call
199 * __pthread_initialize_minimal so we can use pthread_locks
200 * whenever they are needed.
202 if (likely(__pthread_initialize_minimal!=NULL))
203 __pthread_initialize_minimal();
207 # ifdef __UCLIBC_HAS_SSP__
208 /* Set up the stack checker's canary. */
209 stack_chk_guard = _dl_setup_stack_chk_guard();
210 # ifdef THREAD_SET_STACK_GUARD
211 THREAD_SET_STACK_GUARD (stack_chk_guard);
212 # ifdef __UCLIBC_HAS_SSP_COMPAT__
213 __guard = stack_chk_guard;
216 __stack_chk_guard = stack_chk_guard;
217 # if !defined __HAVE_SHARED__ && defined __UCLIBC_HAS_SSP_COMPAT__
218 __guard = stack_chk_guard;
224 #ifdef __UCLIBC_HAS_LOCALE__
225 /* Initialize the global locale structure. */
226 if (likely(_locale_init!=NULL))
231 * Initialize stdio here. In the static library case, this will
232 * be bypassed if not needed because of the weak alias above.
233 * Thus we get a nice size savings because the stdio functions
234 * won't be pulled into the final static binary unless used.
236 if (likely(_stdio_init != NULL))
240 libc_hidden_def(__uClibc_init)
242 #ifdef __UCLIBC_CTOR_DTOR__
243 void attribute_hidden (*__app_fini)(void) = NULL;
246 void attribute_hidden (*__rtld_fini)(void) = NULL;
248 extern void __uClibc_fini(void);
249 libc_hidden_proto(__uClibc_fini)
250 void __uClibc_fini(void)
252 #ifdef __UCLIBC_CTOR_DTOR__
253 /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array finalisation is handled
256 _dl_app_fini_array();
257 # elif !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
258 size_t i = __fini_array_end - __fini_array_start;
260 (*__fini_array_start [i]) ();
262 if (__app_fini != NULL)
265 if (__rtld_fini != NULL)
268 libc_hidden_def(__uClibc_fini)
270 /* __uClibc_main is the new main stub for uClibc. This function is
271 * called from crt1 (version 0.9.28 or newer), after ALL shared libraries
272 * are initialized, just before we call the application's main function.
274 void __uClibc_main(int (*main)(int, char **, char **), int argc,
275 char **argv, void (*app_init)(void), void (*app_fini)(void),
276 void (*rtld_fini)(void), void *stack_end) attribute_noreturn;
277 void __uClibc_main(int (*main)(int, char **, char **), int argc,
278 char **argv, void (*app_init)(void), void (*app_fini)(void),
279 void (*rtld_fini)(void), void *stack_end)
281 #ifndef __ARCH_HAS_NO_LDSO__
282 unsigned long *aux_dat;
283 ElfW(auxv_t) auxvt[AT_EGID + 1];
287 __libc_stack_end = stack_end;
290 __rtld_fini = rtld_fini;
292 /* The environment begins right after argv. */
293 __environ = &argv[argc + 1];
295 /* If the first thing after argv is the arguments
296 * the the environment is empty. */
297 if ((char *) __environ == *argv) {
298 /* Make __environ point to the NULL at argv[argc] */
299 __environ = &argv[argc];
302 #ifndef __ARCH_HAS_NO_LDSO__
303 /* Pull stuff from the ELF header when possible */
304 memset(auxvt, 0x00, sizeof(auxvt));
305 aux_dat = (unsigned long*)__environ;
311 ElfW(auxv_t) *auxv_entry = (ElfW(auxv_t) *) aux_dat;
312 if (auxv_entry->a_type <= AT_EGID) {
313 memcpy(&(auxvt[auxv_entry->a_type]), auxv_entry, sizeof(ElfW(auxv_t)));
318 /* Get the program headers (_dl_phdr) from the aux vector
319 It will be used into __libc_setup_tls. */
321 _dl_aux_init (auxvt);
325 /* We need to initialize uClibc. If we are dynamically linked this
326 * may have already been completed by the shared lib loader. We call
327 * __uClibc_init() regardless, to be sure the right thing happens. */
330 #ifndef __ARCH_HAS_NO_LDSO__
331 /* Make certain getpagesize() gives the correct answer */
332 __pagesize = (auxvt[AT_PAGESZ].a_un.a_val)? auxvt[AT_PAGESZ].a_un.a_val : PAGE_SIZE;
334 /* Prevent starting SUID binaries where the stdin. stdout, and
335 * stderr file descriptors are not already opened. */
336 if ((auxvt[AT_UID].a_un.a_val == (size_t)-1 && __check_suid()) ||
337 (auxvt[AT_UID].a_un.a_val != (size_t)-1 &&
338 (auxvt[AT_UID].a_un.a_val != auxvt[AT_EUID].a_un.a_val ||
339 auxvt[AT_GID].a_un.a_val != auxvt[AT_EGID].a_un.a_val)))
341 __check_one_fd (STDIN_FILENO, O_RDONLY | O_NOFOLLOW);
342 __check_one_fd (STDOUT_FILENO, O_RDWR | O_NOFOLLOW);
343 __check_one_fd (STDERR_FILENO, O_RDWR | O_NOFOLLOW);
347 __uclibc_progname = *argv;
348 #ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
350 program_invocation_name = *argv;
351 program_invocation_short_name = strrchr(*argv, '/');
352 if (program_invocation_short_name != NULL)
353 ++program_invocation_short_name;
355 program_invocation_short_name = program_invocation_name;
359 #ifdef __UCLIBC_CTOR_DTOR__
360 /* Arrange for the application's dtors to run before we exit. */
361 __app_fini = app_fini;
363 /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation is handled
365 # if !defined (SHARED) && !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
366 /* For dynamically linked executables the preinit array is executed by
367 the dynamic linker (before initializing any shared object).
368 For static executables, preinit happens rights before init. */
370 const size_t size = __preinit_array_end - __preinit_array_start;
372 for (i = 0; i < size; i++)
373 (*__preinit_array_start [i]) ();
376 /* Run all the application's ctors now. */
377 if (app_init!=NULL) {
380 /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation is handled
383 _dl_app_init_array();
384 # elif !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
386 const size_t size = __init_array_end - __init_array_start;
388 for (i = 0; i < size; i++)
389 (*__init_array_start [i]) ();
394 /* Note: It is possible that any initialization done above could
395 * have resulted in errno being set nonzero, so set it to 0 before
398 if (likely(__errno_location!=NULL))
399 *(__errno_location()) = 0;
401 /* Set h_errno to 0 as well */
402 if (likely(__h_errno_location!=NULL))
403 *(__h_errno_location()) = 0;
406 * Finally, invoke application's main and then exit.
408 exit(main(argc, argv, __environ));
411 #if defined(__UCLIBC_HAS_THREADS__) && !defined(SHARED)
412 /* Weaks for internal library use only.
414 * We need to define weaks here to cover all the pthread functions that
415 * libc itself will use so that we aren't forced to link libc against
416 * libpthread. This file is only used in libc.a and since we have
417 * weaks here, they will be automatically overridden by libpthread.a
418 * if it gets linked in.
421 static int __pthread_return_0 (void) { return 0; }
422 static void __pthread_return_void (void) { return; }
424 weak_alias (__pthread_return_0, __pthread_mutex_init)
425 weak_alias (__pthread_return_0, __pthread_mutex_lock)
426 weak_alias (__pthread_return_0, __pthread_mutex_trylock)
427 weak_alias (__pthread_return_0, __pthread_mutex_unlock)
428 weak_alias (__pthread_return_void, _pthread_cleanup_push_defer)
429 weak_alias (__pthread_return_void, _pthread_cleanup_pop_restore)
430 # ifdef __UCLIBC_HAS_THREADS_NATIVE__
431 weak_alias (__pthread_return_0, __pthread_mutexattr_init)
432 weak_alias (__pthread_return_0, __pthread_mutexattr_destroy)
433 weak_alias (__pthread_return_0, __pthread_mutexattr_settype)