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 #ifdef __UCLIBC_CTOR_DTOR__
92 extern void _dl_app_init_array(void);
93 extern void _dl_app_fini_array(void);
95 /* These magic symbols are provided by the linker. */
96 extern void (*__preinit_array_start []) (void) attribute_hidden;
97 extern void (*__preinit_array_end []) (void) attribute_hidden;
98 extern void (*__init_array_start []) (void) attribute_hidden;
99 extern void (*__init_array_end []) (void) attribute_hidden;
100 extern void (*__fini_array_start []) (void) attribute_hidden;
101 extern void (*__fini_array_end []) (void) attribute_hidden;
105 attribute_hidden const char *__uclibc_progname = NULL;
106 #ifdef __UCLIBC_HAS___PROGNAME__
107 strong_alias (__uclibc_progname, __progname)
109 #ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
110 attribute_hidden const char *__progname_full = NULL;
111 strong_alias (__uclibc_progname, program_invocation_short_name)
112 strong_alias (__progname_full, program_invocation_name)
116 * Declare the __environ global variable and create a strong alias environ.
117 * Note: Apparently we must initialize __environ to ensure that the strong
118 * environ symbol is also included.
120 char **__environ = 0;
121 weak_alias(__environ, environ)
123 /* TODO: don't export __pagesize; we cant now because libpthread uses it */
124 size_t __pagesize = 0;
127 # define O_NOFOLLOW 0
130 #ifdef __ARCH_USE_MMU__
131 static void __check_one_fd(int fd, int mode)
133 /* Check if the specified fd is already open */
134 if (unlikely(__libc_fcntl(fd, F_GETFD)==-1 && *(__errno_location())==EBADF))
136 /* The descriptor is probably not open, so try to use /dev/null */
138 int nullfd = __libc_open(_PATH_DEVNULL, mode);
139 /* /dev/null is major=1 minor=3. Make absolutely certain
140 * that is in fact the device that we have opened and not
141 * some other wierd file... */
142 if ( (nullfd!=fd) || fstat(fd, &st) || !S_ISCHR(st.st_mode) ||
143 (st.st_rdev != makedev(1, 3)))
150 static int __check_suid(void)
160 if(uid == euid && gid == egid) {
167 /* __uClibc_init completely initialize uClibc so it is ready to use.
169 * On ELF systems (with a dynamic loader) this function must be called
170 * from the dynamic loader (see TIS and ELF Specification), so that
171 * constructors of shared libraries (which depend on libc) can use all
172 * the libc code without restriction. For this we link the shared
173 * version of the uClibc with -init __uClibc_init so DT_INIT for
174 * uClibc is the address of __uClibc_init
176 * In all other cases we call it from the main stub
180 extern void __uClibc_init(void);
181 libc_hidden_proto(__uClibc_init)
182 void __uClibc_init(void)
184 static int been_there_done_that = 0;
186 if (been_there_done_that)
188 been_there_done_that++;
190 /* Setup an initial value. This may not be perfect, but is
191 * better than malloc using __pagesize=0 for atexit, ctors, etc. */
192 __pagesize = PAGE_SIZE;
194 #ifdef __UCLIBC_HAS_THREADS__
195 /* Before we start initializing uClibc we have to call
196 * __pthread_initialize_minimal so we can use pthread_locks
197 * whenever they are needed.
199 if (likely(__pthread_initialize_minimal!=NULL))
200 __pthread_initialize_minimal();
204 # ifdef __UCLIBC_HAS_SSP__
205 /* Set up the stack checker's canary. */
206 stack_chk_guard = _dl_setup_stack_chk_guard();
207 # ifdef THREAD_SET_STACK_GUARD
208 THREAD_SET_STACK_GUARD (stack_chk_guard);
209 # ifdef __UCLIBC_HAS_SSP_COMPAT__
210 __guard = stack_chk_guard;
213 __stack_chk_guard = stack_chk_guard;
214 # if !defined __HAVE_SHARED__ && defined __UCLIBC_HAS_SSP_COMPAT__
215 __guard = stack_chk_guard;
221 #ifdef __UCLIBC_HAS_LOCALE__
222 /* Initialize the global locale structure. */
223 if (likely(_locale_init!=NULL))
228 * Initialize stdio here. In the static library case, this will
229 * be bypassed if not needed because of the weak alias above.
230 * Thus we get a nice size savings because the stdio functions
231 * won't be pulled into the final static binary unless used.
233 if (likely(_stdio_init != NULL))
237 libc_hidden_def(__uClibc_init)
239 #ifdef __UCLIBC_CTOR_DTOR__
240 void attribute_hidden (*__app_fini)(void) = NULL;
243 void attribute_hidden (*__rtld_fini)(void) = NULL;
245 extern void __uClibc_fini(void);
246 libc_hidden_proto(__uClibc_fini)
247 void __uClibc_fini(void)
249 #ifdef __UCLIBC_CTOR_DTOR__
251 _dl_app_fini_array();
253 size_t i = __fini_array_end - __fini_array_start;
255 (*__fini_array_start [i]) ();
257 if (__app_fini != NULL)
260 if (__rtld_fini != NULL)
263 libc_hidden_def(__uClibc_fini)
265 /* __uClibc_main is the new main stub for uClibc. This function is
266 * called from crt1 (version 0.9.28 or newer), after ALL shared libraries
267 * are initialized, just before we call the application's main function.
269 void __uClibc_main(int (*main)(int, char **, char **), int argc,
270 char **argv, void (*app_init)(void), void (*app_fini)(void),
271 void (*rtld_fini)(void), void *stack_end) attribute_noreturn;
272 void __uClibc_main(int (*main)(int, char **, char **), int argc,
273 char **argv, void (*app_init)(void), void (*app_fini)(void),
274 void (*rtld_fini)(void), void *stack_end)
276 #ifdef __ARCH_USE_MMU__
277 unsigned long *aux_dat;
278 ElfW(auxv_t) auxvt[AT_EGID + 1];
282 __libc_stack_end = stack_end;
285 __rtld_fini = rtld_fini;
287 /* The environment begins right after argv. */
288 __environ = &argv[argc + 1];
290 /* If the first thing after argv is the arguments
291 * the the environment is empty. */
292 if ((char *) __environ == *argv) {
293 /* Make __environ point to the NULL at argv[argc] */
294 __environ = &argv[argc];
297 #ifdef __ARCH_USE_MMU__
298 /* Pull stuff from the ELF header when possible */
299 memset(auxvt, 0x00, sizeof(auxvt));
300 aux_dat = (unsigned long*)__environ;
306 ElfW(auxv_t) *auxv_entry = (ElfW(auxv_t) *) aux_dat;
307 if (auxv_entry->a_type <= AT_EGID) {
308 memcpy(&(auxvt[auxv_entry->a_type]), auxv_entry, sizeof(ElfW(auxv_t)));
314 /* We need to initialize uClibc. If we are dynamically linked this
315 * may have already been completed by the shared lib loader. We call
316 * __uClibc_init() regardless, to be sure the right thing happens. */
319 #ifdef __ARCH_USE_MMU__
320 /* Make certain getpagesize() gives the correct answer */
321 __pagesize = (auxvt[AT_PAGESZ].a_un.a_val)? auxvt[AT_PAGESZ].a_un.a_val : PAGE_SIZE;
323 /* Prevent starting SUID binaries where the stdin. stdout, and
324 * stderr file descriptors are not already opened. */
325 if ((auxvt[AT_UID].a_un.a_val == (size_t)-1 && __check_suid()) ||
326 (auxvt[AT_UID].a_un.a_val != (size_t)-1 &&
327 (auxvt[AT_UID].a_un.a_val != auxvt[AT_EUID].a_un.a_val ||
328 auxvt[AT_GID].a_un.a_val != auxvt[AT_EGID].a_un.a_val)))
330 __check_one_fd (STDIN_FILENO, O_RDONLY | O_NOFOLLOW);
331 __check_one_fd (STDOUT_FILENO, O_RDWR | O_NOFOLLOW);
332 __check_one_fd (STDERR_FILENO, O_RDWR | O_NOFOLLOW);
336 #ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
337 __progname_full = *argv;
338 __progname = strrchr(*argv, '/');
339 if (__progname != NULL)
342 __progname = __progname_full;
344 __uclibc_progname = *argv;
347 #ifdef __UCLIBC_CTOR_DTOR__
348 /* Arrange for the application's dtors to run before we exit. */
349 __app_fini = app_fini;
352 /* For dynamically linked executables the preinit array is executed by
353 the dynamic linker (before initializing any shared object).
354 For static executables, preinit happens rights before init. */
356 const size_t size = __preinit_array_end - __preinit_array_start;
358 for (i = 0; i < size; i++)
359 (*__preinit_array_start [i]) ();
362 /* Run all the application's ctors now. */
363 if (app_init!=NULL) {
367 _dl_app_init_array();
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));