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)
175 /* Setup an initial value. This may not be perfect, but is
176 * better than malloc using __pagesize=0 for atexit, ctors, etc. */
177 __pagesize = PAGE_SIZE;
179 #ifdef __UCLIBC_HAS_THREADS__
180 /* Before we start initializing uClibc we have to call
181 * __pthread_initialize_minimal so we can use pthread_locks
182 * whenever they are needed.
184 if (likely(__pthread_initialize_minimal!=NULL))
185 __pthread_initialize_minimal();
189 # ifdef __UCLIBC_HAS_SSP__
190 /* Set up the stack checker's canary. */
191 stack_chk_guard = _dl_setup_stack_chk_guard();
192 # ifdef THREAD_SET_STACK_GUARD
193 THREAD_SET_STACK_GUARD (stack_chk_guard);
194 # ifdef __UCLIBC_HAS_SSP_COMPAT__
195 __guard = stack_chk_guard;
198 __stack_chk_guard = stack_chk_guard;
199 # if !defined __HAVE_SHARED__ && defined __UCLIBC_HAS_SSP_COMPAT__
200 __guard = stack_chk_guard;
206 #ifdef __UCLIBC_HAS_LOCALE__
207 /* Initialize the global locale structure. */
208 if (likely(_locale_init!=NULL))
213 * Initialize stdio here. In the static library case, this will
214 * be bypassed if not needed because of the weak alias above.
215 * Thus we get a nice size savings because the stdio functions
216 * won't be pulled into the final static binary unless used.
218 if (likely(_stdio_init != NULL))
222 libc_hidden_def(__uClibc_init)
224 #ifdef __UCLIBC_CTOR_DTOR__
225 void attribute_hidden (*__app_fini)(void) = NULL;
228 void attribute_hidden (*__rtld_fini)(void) = NULL;
230 extern void __uClibc_fini(void);
231 libc_hidden_proto(__uClibc_fini)
232 void __uClibc_fini(void)
234 #ifdef __UCLIBC_CTOR_DTOR__
235 /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array finalisation is handled
238 _dl_app_fini_array();
239 # elif !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
240 size_t i = __fini_array_end - __fini_array_start;
242 (*__fini_array_start [i]) ();
244 if (__app_fini != NULL)
247 if (__rtld_fini != NULL)
250 libc_hidden_def(__uClibc_fini)
252 /* __uClibc_main is the new main stub for uClibc. This function is
253 * called from crt1 (version 0.9.28 or newer), after ALL shared libraries
254 * are initialized, just before we call the application's main function.
256 void __uClibc_main(int (*main)(int, char **, char **), int argc,
257 char **argv, void (*app_init)(void), void (*app_fini)(void),
258 void (*rtld_fini)(void),
259 void *stack_end attribute_unused) attribute_noreturn;
260 void __uClibc_main(int (*main)(int, char **, char **), int argc,
261 char **argv, void (*app_init)(void), void (*app_fini)(void),
262 void (*rtld_fini)(void), void *stack_end attribute_unused)
264 #ifndef __ARCH_HAS_NO_LDSO__
265 unsigned long *aux_dat;
266 ElfW(auxv_t) auxvt[AT_EGID + 1];
270 __libc_stack_end = stack_end;
273 __rtld_fini = rtld_fini;
275 /* The environment begins right after argv. */
276 __environ = &argv[argc + 1];
278 /* If the first thing after argv is the arguments
279 * the the environment is empty. */
280 if ((char *) __environ == *argv) {
281 /* Make __environ point to the NULL at argv[argc] */
282 __environ = &argv[argc];
285 #ifndef __ARCH_HAS_NO_LDSO__
286 /* Pull stuff from the ELF header when possible */
287 memset(auxvt, 0x00, sizeof(auxvt));
288 aux_dat = (unsigned long*)__environ;
294 ElfW(auxv_t) *auxv_entry = (ElfW(auxv_t) *) aux_dat;
295 if (auxv_entry->a_type <= AT_EGID) {
296 memcpy(&(auxvt[auxv_entry->a_type]), auxv_entry, sizeof(ElfW(auxv_t)));
301 /* Get the program headers (_dl_phdr) from the aux vector
302 It will be used into __libc_setup_tls. */
304 _dl_aux_init (auxvt);
308 /* We need to initialize uClibc. If we are dynamically linked this
309 * may have already been completed by the shared lib loader. We call
310 * __uClibc_init() regardless, to be sure the right thing happens. */
313 #ifndef __ARCH_HAS_NO_LDSO__
314 /* Make certain getpagesize() gives the correct answer */
315 __pagesize = (auxvt[AT_PAGESZ].a_un.a_val)? auxvt[AT_PAGESZ].a_un.a_val : PAGE_SIZE;
317 /* Prevent starting SUID binaries where the stdin. stdout, and
318 * stderr file descriptors are not already opened. */
319 if ((auxvt[AT_UID].a_un.a_val == (size_t)-1 && __check_suid()) ||
320 (auxvt[AT_UID].a_un.a_val != (size_t)-1 &&
321 (auxvt[AT_UID].a_un.a_val != auxvt[AT_EUID].a_un.a_val ||
322 auxvt[AT_GID].a_un.a_val != auxvt[AT_EGID].a_un.a_val)))
324 __check_one_fd (STDIN_FILENO, O_RDONLY | O_NOFOLLOW);
325 __check_one_fd (STDOUT_FILENO, O_RDWR | O_NOFOLLOW);
326 __check_one_fd (STDERR_FILENO, O_RDWR | O_NOFOLLOW);
330 __uclibc_progname = *argv;
331 #ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
333 program_invocation_name = *argv;
334 program_invocation_short_name = strrchr(*argv, '/');
335 if (program_invocation_short_name != NULL)
336 ++program_invocation_short_name;
338 program_invocation_short_name = program_invocation_name;
342 #ifdef __UCLIBC_CTOR_DTOR__
343 /* Arrange for the application's dtors to run before we exit. */
344 __app_fini = app_fini;
346 /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation is handled
348 # if !defined (SHARED) && !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
349 /* For dynamically linked executables the preinit array is executed by
350 the dynamic linker (before initializing any shared object).
351 For static executables, preinit happens rights before init. */
353 const size_t size = __preinit_array_end - __preinit_array_start;
355 for (i = 0; i < size; i++)
356 (*__preinit_array_start [i]) ();
359 /* Run all the application's ctors now. */
360 if (app_init!=NULL) {
363 /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation is handled
366 _dl_app_init_array();
367 # elif !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
369 const size_t size = __init_array_end - __init_array_start;
371 for (i = 0; i < size; i++)
372 (*__init_array_start [i]) ();
377 /* Note: It is possible that any initialization done above could
378 * have resulted in errno being set nonzero, so set it to 0 before
381 if (likely(__errno_location!=NULL))
382 *(__errno_location()) = 0;
384 /* Set h_errno to 0 as well */
385 if (likely(__h_errno_location!=NULL))
386 *(__h_errno_location()) = 0;
389 * Finally, invoke application's main and then exit.
391 exit(main(argc, argv, __environ));
394 #if defined(__UCLIBC_HAS_THREADS__) && !defined(SHARED)
395 /* Weaks for internal library use only.
397 * We need to define weaks here to cover all the pthread functions that
398 * libc itself will use so that we aren't forced to link libc against
399 * libpthread. This file is only used in libc.a and since we have
400 * weaks here, they will be automatically overridden by libpthread.a
401 * if it gets linked in.
404 static int __pthread_return_0 (void) { return 0; }
405 static void __pthread_return_void (void) { return; }
407 weak_alias (__pthread_return_0, __pthread_mutex_init)
408 weak_alias (__pthread_return_0, __pthread_mutex_lock)
409 weak_alias (__pthread_return_0, __pthread_mutex_trylock)
410 weak_alias (__pthread_return_0, __pthread_mutex_unlock)
411 weak_alias (__pthread_return_void, _pthread_cleanup_push_defer)
412 weak_alias (__pthread_return_void, _pthread_cleanup_pop_restore)
413 # ifdef __UCLIBC_HAS_THREADS_NATIVE__
414 weak_alias (__pthread_return_0, __pthread_mutexattr_init)
415 weak_alias (__pthread_return_0, __pthread_mutexattr_destroy)
416 weak_alias (__pthread_return_0, __pthread_mutexattr_settype)