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>
26 #include <asm/errno.h>
29 #include <sys/sysmacros.h>
31 libc_hidden_proto(exit)
33 #ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
34 libc_hidden_proto(strrchr)
36 #ifdef __ARCH_USE_MMU__
37 libc_hidden_proto(memcpy)
38 libc_hidden_proto(getgid)
39 libc_hidden_proto(getuid)
40 libc_hidden_proto(getegid)
41 libc_hidden_proto(geteuid)
42 libc_hidden_proto(fstat)
43 libc_hidden_proto(abort)
45 extern __typeof(open) __libc_open;
46 libc_hidden_proto(__libc_open)
47 extern __typeof(fcntl) __libc_fcntl;
48 libc_hidden_proto(__libc_fcntl)
52 void *__libc_stack_end=NULL;
54 # ifdef __UCLIBC_HAS_SSP__
55 # include <dl-osinfo.h>
56 # ifndef THREAD_SET_STACK_GUARD
57 /* Only exported for architectures that don't store the stack guard canary
58 * in thread local area. */
60 uintptr_t stack_chk_guard;
61 /* for gcc-4.1 non-TLS */
62 uintptr_t __stack_chk_guard attribute_relro;
63 /* for gcc-3.x + Etoh ssp */
64 # ifdef __UCLIBC_HAS_SSP_COMPAT__
65 # ifdef __HAVE_SHARED__
66 strong_alias(__stack_chk_guard,__guard)
68 uintptr_t __guard attribute_relro;
71 # elif defined __UCLIBC_HAS_SSP_COMPAT__
72 uintptr_t __guard attribute_relro;
81 extern void weak_function _stdio_init(void) attribute_hidden;
82 extern int *weak_const_function __errno_location(void);
83 extern int *weak_const_function __h_errno_location(void);
84 #ifdef __UCLIBC_HAS_LOCALE__
85 extern void weak_function _locale_init(void) attribute_hidden;
87 #ifdef __UCLIBC_HAS_THREADS__
88 extern void weak_function __pthread_initialize_minimal(void);
91 /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation and finalisation
92 * is handled by the routines passed to __uClibc_main(). */
93 #if defined (__UCLIBC_CTOR_DTOR__) && !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
94 extern void _dl_app_init_array(void);
95 extern void _dl_app_fini_array(void);
97 /* These magic symbols are provided by the linker. */
98 extern void (*__preinit_array_start []) (void) attribute_hidden;
99 extern void (*__preinit_array_end []) (void) attribute_hidden;
100 extern void (*__init_array_start []) (void) attribute_hidden;
101 extern void (*__init_array_end []) (void) attribute_hidden;
102 extern void (*__fini_array_start []) (void) attribute_hidden;
103 extern void (*__fini_array_end []) (void) attribute_hidden;
107 attribute_hidden const char *__uclibc_progname = NULL;
108 #ifdef __UCLIBC_HAS___PROGNAME__
109 strong_alias (__uclibc_progname, __progname)
111 #ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
112 attribute_hidden const char *__progname_full = NULL;
113 strong_alias (__uclibc_progname, program_invocation_short_name)
114 strong_alias (__progname_full, program_invocation_name)
118 * Declare the __environ global variable and create a strong alias environ.
119 * Note: Apparently we must initialize __environ to ensure that the strong
120 * environ symbol is also included.
122 char **__environ = 0;
123 weak_alias(__environ, environ)
125 /* TODO: don't export __pagesize; we cant now because libpthread uses it */
126 size_t __pagesize = 0;
129 # define O_NOFOLLOW 0
132 #ifdef __ARCH_USE_MMU__
133 static void __check_one_fd(int fd, int mode)
135 /* Check if the specified fd is already open */
136 if (unlikely(__libc_fcntl(fd, F_GETFD)==-1 && *(__errno_location())==EBADF))
138 /* The descriptor is probably not open, so try to use /dev/null */
140 int nullfd = __libc_open(_PATH_DEVNULL, mode);
141 /* /dev/null is major=1 minor=3. Make absolutely certain
142 * that is in fact the device that we have opened and not
143 * some other wierd file... */
144 if ( (nullfd!=fd) || fstat(fd, &st) || !S_ISCHR(st.st_mode) ||
145 (st.st_rdev != makedev(1, 3)))
152 static int __check_suid(void)
162 if(uid == euid && gid == egid) {
169 /* __uClibc_init completely initialize uClibc so it is ready to use.
171 * On ELF systems (with a dynamic loader) this function must be called
172 * from the dynamic loader (see TIS and ELF Specification), so that
173 * constructors of shared libraries (which depend on libc) can use all
174 * the libc code without restriction. For this we link the shared
175 * version of the uClibc with -init __uClibc_init so DT_INIT for
176 * uClibc is the address of __uClibc_init
178 * In all other cases we call it from the main stub
182 extern void __uClibc_init(void);
183 libc_hidden_proto(__uClibc_init)
184 void __uClibc_init(void)
186 static int been_there_done_that = 0;
188 if (been_there_done_that)
190 been_there_done_that++;
192 /* Setup an initial value. This may not be perfect, but is
193 * better than malloc using __pagesize=0 for atexit, ctors, etc. */
194 __pagesize = PAGE_SIZE;
196 #ifdef __UCLIBC_HAS_THREADS__
197 /* Before we start initializing uClibc we have to call
198 * __pthread_initialize_minimal so we can use pthread_locks
199 * whenever they are needed.
201 if (likely(__pthread_initialize_minimal!=NULL))
202 __pthread_initialize_minimal();
206 # ifdef __UCLIBC_HAS_SSP__
207 /* Set up the stack checker's canary. */
208 stack_chk_guard = _dl_setup_stack_chk_guard();
209 # ifdef THREAD_SET_STACK_GUARD
210 THREAD_SET_STACK_GUARD (stack_chk_guard);
211 # ifdef __UCLIBC_HAS_SSP_COMPAT__
212 __guard = stack_chk_guard;
215 __stack_chk_guard = stack_chk_guard;
216 # if !defined __HAVE_SHARED__ && defined __UCLIBC_HAS_SSP_COMPAT__
217 __guard = stack_chk_guard;
223 #ifdef __UCLIBC_HAS_LOCALE__
224 /* Initialize the global locale structure. */
225 if (likely(_locale_init!=NULL))
230 * Initialize stdio here. In the static library case, this will
231 * be bypassed if not needed because of the weak alias above.
232 * Thus we get a nice size savings because the stdio functions
233 * won't be pulled into the final static binary unless used.
235 if (likely(_stdio_init != NULL))
239 libc_hidden_def(__uClibc_init)
241 #ifdef __UCLIBC_CTOR_DTOR__
242 void attribute_hidden (*__app_fini)(void) = NULL;
245 void attribute_hidden (*__rtld_fini)(void) = NULL;
247 extern void __uClibc_fini(void);
248 libc_hidden_proto(__uClibc_fini)
249 void __uClibc_fini(void)
251 #ifdef __UCLIBC_CTOR_DTOR__
252 /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array finalisation is handled
255 _dl_app_fini_array();
256 # elif !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
257 size_t i = __fini_array_end - __fini_array_start;
259 (*__fini_array_start [i]) ();
261 if (__app_fini != NULL)
264 if (__rtld_fini != NULL)
267 libc_hidden_def(__uClibc_fini)
269 /* __uClibc_main is the new main stub for uClibc. This function is
270 * called from crt1 (version 0.9.28 or newer), after ALL shared libraries
271 * are initialized, just before we call the application's main function.
273 void __uClibc_main(int (*main)(int, char **, char **), int argc,
274 char **argv, void (*app_init)(void), void (*app_fini)(void),
275 void (*rtld_fini)(void), void *stack_end) attribute_noreturn;
276 void __uClibc_main(int (*main)(int, char **, char **), int argc,
277 char **argv, void (*app_init)(void), void (*app_fini)(void),
278 void (*rtld_fini)(void), void *stack_end)
280 #ifdef __ARCH_USE_MMU__
281 unsigned long *aux_dat;
282 ElfW(auxv_t) auxvt[AT_EGID + 1];
286 __libc_stack_end = stack_end;
289 __rtld_fini = rtld_fini;
291 /* The environment begins right after argv. */
292 __environ = &argv[argc + 1];
294 /* If the first thing after argv is the arguments
295 * the the environment is empty. */
296 if ((char *) __environ == *argv) {
297 /* Make __environ point to the NULL at argv[argc] */
298 __environ = &argv[argc];
301 #ifdef __ARCH_USE_MMU__
302 /* Pull stuff from the ELF header when possible */
303 memset(auxvt, 0x00, sizeof(auxvt));
304 aux_dat = (unsigned long*)__environ;
310 ElfW(auxv_t) *auxv_entry = (ElfW(auxv_t) *) aux_dat;
311 if (auxv_entry->a_type <= AT_EGID) {
312 memcpy(&(auxvt[auxv_entry->a_type]), auxv_entry, sizeof(ElfW(auxv_t)));
318 /* We need to initialize uClibc. If we are dynamically linked this
319 * may have already been completed by the shared lib loader. We call
320 * __uClibc_init() regardless, to be sure the right thing happens. */
323 #ifdef __ARCH_USE_MMU__
324 /* Make certain getpagesize() gives the correct answer */
325 __pagesize = (auxvt[AT_PAGESZ].a_un.a_val)? auxvt[AT_PAGESZ].a_un.a_val : PAGE_SIZE;
327 /* Prevent starting SUID binaries where the stdin. stdout, and
328 * stderr file descriptors are not already opened. */
329 if ((auxvt[AT_UID].a_un.a_val == (size_t)-1 && __check_suid()) ||
330 (auxvt[AT_UID].a_un.a_val != (size_t)-1 &&
331 (auxvt[AT_UID].a_un.a_val != auxvt[AT_EUID].a_un.a_val ||
332 auxvt[AT_GID].a_un.a_val != auxvt[AT_EGID].a_un.a_val)))
334 __check_one_fd (STDIN_FILENO, O_RDONLY | O_NOFOLLOW);
335 __check_one_fd (STDOUT_FILENO, O_RDWR | O_NOFOLLOW);
336 __check_one_fd (STDERR_FILENO, O_RDWR | O_NOFOLLOW);
340 #ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
341 __progname_full = *argv;
342 __progname = strrchr(*argv, '/');
343 if (__progname != NULL)
346 __progname = __progname_full;
348 __uclibc_progname = *argv;
351 #ifdef __UCLIBC_CTOR_DTOR__
352 /* Arrange for the application's dtors to run before we exit. */
353 __app_fini = app_fini;
355 /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation is handled
357 # if !defined (SHARED) && !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
358 /* For dynamically linked executables the preinit array is executed by
359 the dynamic linker (before initializing any shared object).
360 For static executables, preinit happens rights before init. */
362 const size_t size = __preinit_array_end - __preinit_array_start;
364 for (i = 0; i < size; i++)
365 (*__preinit_array_start [i]) ();
368 /* Run all the application's ctors now. */
369 if (app_init!=NULL) {
372 /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation is handled
375 _dl_app_init_array();
376 # elif !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
378 const size_t size = __init_array_end - __init_array_start;
380 for (i = 0; i < size; i++)
381 (*__init_array_start [i]) ();
386 /* Note: It is possible that any initialization done above could
387 * have resulted in errno being set nonzero, so set it to 0 before
390 if (likely(__errno_location!=NULL))
391 *(__errno_location()) = 0;
393 /* Set h_errno to 0 as well */
394 if (likely(__h_errno_location!=NULL))
395 *(__h_errno_location()) = 0;
398 * Finally, invoke application's main and then exit.
400 exit(main(argc, argv, __environ));