2 * Manuel Novoa III Feb 2001
3 * Erik Andersen 2002-2004
5 * __uClibc_main is the routine to be called by all the arch-specific
6 * versions of crt0.S in uClibc.
8 * It is meant to handle any special initialization needed by the library
9 * such as setting the global variable(s) __environ (environ) and
10 * initializing the stdio package. Using weak symbols, the latter is
11 * avoided in the static library case.
21 #include <bits/uClibc_page.h>
24 #include <asm/errno.h>
27 #include <sys/sysmacros.h>
28 #ifdef __UCLIBC_HAS_SSP__
29 extern void __guard_setup(void);
36 extern void *__libc_stack_end;
37 extern void weak_function _stdio_init(void);
38 extern int *weak_const_function __errno_location(void);
39 extern int *weak_const_function __h_errno_location(void);
40 #ifdef __UCLIBC_HAS_LOCALE__
41 extern void weak_function _locale_init(void);
43 #ifdef __UCLIBC_HAS_THREADS__
44 extern void weak_function __pthread_initialize_minimal(void);
52 * Declare the __environ global variable and create a weak alias environ.
53 * Note: Apparently we must initialize __environ to ensure that the weak
54 * environ symbol is also included.
57 weak_alias(__environ, environ);
59 size_t __pagesize = 0;
60 const char *__progname = 0;
67 extern int __libc_fcntl(int fd, int cmd, ...);
68 extern int __libc_open(const char *file, int flags, ...);
70 static void __check_one_fd(int fd, int mode)
72 /* Check if the specified fd is already open */
73 if (unlikely(__libc_fcntl(fd, F_GETFD)==-1 && *(__errno_location())==EBADF))
75 /* The descriptor is probably not open, so try to use /dev/null */
77 int nullfd = __libc_open(_PATH_DEVNULL, mode);
78 /* /dev/null is major=1 minor=3. Make absolutely certain
79 * that is in fact the device that we have opened and not
80 * some other wierd file... */
81 if ( (nullfd!=fd) || fstat(fd, &st) || !S_ISCHR(st.st_mode) ||
82 (st.st_rdev != makedev(1, 3)))
84 /* Somebody is trying some trickery here... */
92 static int __check_suid(void)
102 if(uid == euid && gid == egid) {
109 /* __uClibc_init completely initialize uClibc so it is ready to use.
111 * On ELF systems (with a dynamic loader) this function must be called
112 * from the dynamic loader (see TIS and ELF Specification), so that
113 * constructors of shared libraries (which depend on libc) can use all
114 * the libc code without restriction. For this we link the shared
115 * version of the uClibc with -init __uClibc_init so DT_INIT for
116 * uClibc is the address of __uClibc_init
118 * In all other cases we call it from the main stub
119 * __uClibc_start_main.
122 void __uClibc_init(void)
124 static int been_there_done_that = 0;
126 if (been_there_done_that)
128 been_there_done_that++;
130 /* Setup an initial value. This may not be perfect, but is
131 * better than malloc using __pagesize=0 for atexit, ctors, etc. */
132 __pagesize = PAGE_SIZE;
134 #ifdef __UCLIBC_HAS_THREADS__
135 /* Before we start initialzing uClibc we have to call
136 * __pthread_initialize_minimal so we can use pthread_locks
137 * whenever they are needed.
139 if (likely(__pthread_initialize_minimal!=NULL))
140 __pthread_initialize_minimal();
143 #ifdef __UCLIBC_HAS_LOCALE__
144 /* Initialize the global locale structure. */
145 if (likely(_locale_init!=NULL))
150 * Initialize stdio here. In the static library case, this will
151 * be bypassed if not needed because of the weak alias above.
153 if (likely(_stdio_init != NULL))
158 #ifdef __UCLIBC_CTOR_DTOR__
159 void attribute_hidden (*__app_fini)(void) = NULL;
162 void attribute_hidden (*__rtld_fini)(void) = NULL;
164 #ifdef _DL_FINI_CRT_COMPAT
165 void attribute_hidden (*__dl_fini)(void) = NULL;
167 void _set__dl_fini(void *fini_func)
169 if (fini_func != NULL)
170 __dl_fini = fini_func;
174 /* __uClibc_start_main is the new main stub for uClibc. This function is
175 * called from crt0 (version 0.9.16 or newer), after ALL shared libraries
176 * are initialized, just before we call the application's main function.
178 void __attribute__ ((__noreturn__))
179 __uClibc_main(int (*main)(int, char **, char **), int argc,
180 char **argv, void (*app_init)(void), void (*app_fini)(void),
181 void (*rtld_fini)(void), void *stack_end)
183 #ifdef __ARCH_HAS_MMU__
184 unsigned long *aux_dat;
185 ElfW(auxv_t) auxvt[AT_EGID + 1];
187 __libc_stack_end = stack_end;
188 /* We need to initialize uClibc. If we are dynamically linked this
189 * may have already been completed by the shared lib loader. We call
190 * __uClibc_init() regardless, to be sure the right thing happens. */
193 __rtld_fini = rtld_fini;
195 /* The environment begins right after argv. */
196 __environ = &argv[argc + 1];
198 /* If the first thing after argv is the arguments
199 * the the environment is empty. */
200 if ((char *) __environ == *argv) {
201 /* Make __environ point to the NULL at argv[argc] */
202 __environ = &argv[argc];
205 /* Pull stuff from the ELF header when possible */
206 #ifdef __ARCH_HAS_MMU__
207 aux_dat = (unsigned long*)__environ;
213 ElfW(auxv_t) *auxv_entry = (ElfW(auxv_t) *) aux_dat;
214 if (auxv_entry->a_type <= AT_EGID) {
215 memcpy(&(auxvt[auxv_entry->a_type]), auxv_entry, sizeof(ElfW(auxv_t)));
220 /* Make certain getpagesize() gives the correct answer */
221 __pagesize = (auxvt[AT_PAGESZ].a_un.a_val)? auxvt[AT_PAGESZ].a_un.a_val : PAGE_SIZE;
223 /* Prevent starting SUID binaries where the stdin. stdout, and
224 * stderr file descriptors are not already opened. */
225 if ((auxvt[AT_UID].a_un.a_val==-1 && __check_suid()) ||
226 (auxvt[AT_UID].a_un.a_val != -1 &&
227 (auxvt[AT_UID].a_un.a_val != auxvt[AT_EUID].a_un.a_val ||
228 auxvt[AT_GID].a_un.a_val != auxvt[AT_EGID].a_un.a_val)))
230 __check_one_fd (STDIN_FILENO, O_RDONLY | O_NOFOLLOW);
231 __check_one_fd (STDOUT_FILENO, O_RDWR | O_NOFOLLOW);
232 __check_one_fd (STDERR_FILENO, O_RDWR | O_NOFOLLOW);
238 #ifdef __UCLIBC_CTOR_DTOR__
239 /* Arrange for the application's dtors to run before we exit. */
240 __app_fini = app_fini;
242 /* Run all the application's ctors now. */
243 if (app_init!=NULL) {
248 #ifdef __UCLIBC_HAS_SSP__
252 /* Note: It is possible that any initialization done above could
253 * have resulted in errno being set nonzero, so set it to 0 before
256 if (likely(__errno_location!=NULL))
257 *(__errno_location()) = 0;
259 /* Set h_errno to 0 as well */
260 if (likely(__h_errno_location!=NULL))
261 *(__h_errno_location()) = 0;
264 * Finally, invoke application's main and then exit.
266 exit(main(argc, argv, __environ));
269 #ifdef _DL_FINI_CRT_COMPAT
270 extern int weak_function main(int argc, char **argv, char **envp);
271 void __attribute__ ((__noreturn__))
272 __uClibc_start_main(int argc, char **argv, char **envp,
273 void (*app_fini)(void), void (*app_init)(void))
275 __uClibc_main(main, argc, argv, app_init, app_fini, NULL, NULL);