1 /*
2 * Copyright (C) 2006 by Steven J. Hill <sjhill@realitydiluted.com>
3 * Copyright (C) 2001 by Manuel Novoa III <mjn3@uclibc.org>
4 * Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
5 *
6 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
7 *
8 * __uClibc_main is the routine to be called by all the arch-specific
9 * versions of crt1.S in uClibc.
10 *
11 * It is meant to handle any special initialization needed by the library
12 * such as setting the global variable(s) __environ (environ) and
13 * initializing the stdio package. Using weak symbols, the latter is
14 * avoided in the static library case.
15 */
16
17 #include <features.h>
18 #include <unistd.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <elf.h>
22 #include <link.h>
23 #include <bits/uClibc_page.h>
24 #include <paths.h>
25 #include <errno.h>
26 #include <netdb.h>
27 #include <stdio.h>
28 #ifndef __ARCH_HAS_NO_LDSO__
29 #include <fcntl.h>
30 #endif
31 #ifdef __UCLIBC_HAS_THREADS_NATIVE__
32 #include <pthread-functions.h>
33 #include <not-cancel.h>
34 #include <atomic.h>
35 #endif
36 #ifdef __UCLIBC_HAS_THREADS__
37 #include <pthread.h>
38 #endif
39 #ifdef __UCLIBC_HAS_LOCALE__
40 #include <locale.h>
41 #endif
42
43 #ifndef SHARED
44 void *__libc_stack_end = NULL;
45
46 # ifdef __UCLIBC_HAS_SSP__
47 # ifndef __NOT_FOR_L4__ // in L4 we have libssp which implements _dl_setup_stack_chk_guard
48 extern uintptr_t _dl_setup_stack_chk_guard(void);
49 # include <features.h>
50 # else
51 # include <dl-osinfo.h>
52 # endif // __NOT_FOR_L4__
53 static uintptr_t stack_chk_guard;
54 # ifndef THREAD_SET_STACK_GUARD
55 /* Only exported for architectures that don't store the stack guard canary
56 * in thread local area. */
57 /* for gcc-4.1 non-TLS */
58 uintptr_t __stack_chk_guard attribute_relro;
59 # endif
60 /* for gcc-3.x + Etoh ssp */
61 # ifdef __UCLIBC_HAS_SSP_COMPAT__
62 uintptr_t __guard attribute_relro;
63 # endif
64 # endif
65
66 /*
67 * Needed to initialize _dl_phdr when statically linked
68 */
69
70 void internal_function _dl_aux_init (ElfW(auxv_t) *av);
71
72 #ifdef __UCLIBC_HAS_THREADS__
73 /*
74 * uClibc internal locking requires that we have weak aliases
75 * for dummy functions in case libpthread.a is not linked in.
76 * This needs to be in compilation unit that is pulled always
77 * in or linker will disregard these weaks.
78 */
79
__pthread_return_0(pthread_mutex_t * unused)80 static int __pthread_return_0 (pthread_mutex_t *unused) { return 0; }
weak_alias(__pthread_return_0,__pthread_mutex_lock)81 weak_alias (__pthread_return_0, __pthread_mutex_lock)
82 weak_alias (__pthread_return_0, __pthread_mutex_trylock)
83 weak_alias (__pthread_return_0, __pthread_mutex_unlock)
84
85 int weak_function
86 __pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
87 {
88 return 0;
89 }
90
91 void weak_function
_pthread_cleanup_push_defer(struct _pthread_cleanup_buffer * __buffer,void (* __routine)(void *),void * __arg)92 _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer *__buffer,
93 void (*__routine) (void *), void *__arg)
94 {
95 __buffer->__routine = __routine;
96 __buffer->__arg = __arg;
97 }
98
99 void weak_function
_pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer * __buffer,int __execute)100 _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer *__buffer,
101 int __execute)
102 {
103 if (__execute)
104 __buffer->__routine(__buffer->__arg);
105 }
106 #endif /* __UCLIBC_HAS_THREADS__ */
107
108 #endif /* !SHARED */
109
110 /* Defeat compiler optimization which assumes function addresses are never NULL */
not_null_ptr(const void * p)111 static __always_inline int not_null_ptr(const void *p)
112 {
113 const void *q;
114 __asm__ (""
115 : "=r" (q) /* output */
116 : "0" (p) /* input */
117 );
118 return q != 0;
119 }
120
121 /*
122 * Prototypes.
123 */
124 #ifdef __UCLIBC_HAS_THREADS__
125 #if !defined (__UCLIBC_HAS_THREADS_NATIVE__) || defined (SHARED)
126 extern void weak_function __pthread_initialize_minimal(void);
127 #else
128 extern void __pthread_initialize_minimal(void);
129 #endif
130 #endif
131
132 /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation and finalisation
133 * is handled by the routines passed to __uClibc_main(). */
134 #if defined (__UCLIBC_CTOR_DTOR__) && !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
135 extern void _dl_app_init_array(void);
136 extern void _dl_app_fini_array(void);
137 # ifndef SHARED
138 /* These magic symbols are provided by the linker. */
139 extern void (*__preinit_array_start []) (void) attribute_hidden;
140 extern void (*__preinit_array_end []) (void) attribute_hidden;
141 extern void (*__init_array_start []) (void) attribute_hidden;
142 extern void (*__init_array_end []) (void) attribute_hidden;
143 extern void (*__fini_array_start []) (void) attribute_hidden;
144 extern void (*__fini_array_end []) (void) attribute_hidden;
145 # endif
146 #endif
147
148 #ifdef SHARED
149 extern int _dl_secure;
150 #endif
151 extern size_t _dl_pagesize;
152
153 const char *__uclibc_progname = "";
154 #if !defined __UCLIBC_HAS___PROGNAME__ && defined __USE_GNU && defined __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
155 # define __progname program_invocation_short_name
156 # define __progname_full program_invocation_name
157 #endif
158 #if defined __UCLIBC_HAS___PROGNAME__ || (defined __USE_GNU && defined __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__)
159 const char *__progname = "";
160 /* psm: why have a visible __progname_full? */
161 const char *__progname_full = "";
162 # if defined __UCLIBC_HAS___PROGNAME__ && defined __USE_GNU && defined __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
163 weak_alias (__progname, program_invocation_short_name)
164 weak_alias (__progname_full, program_invocation_name)
165 # endif
166 #endif
167
168 /*
169 * Declare the __environ global variable and create a weak alias environ.
170 * This must be initialized; we cannot have a weak alias into bss.
171 */
172 char **__environ = 0;
173 weak_alias(__environ, environ)
174
175 size_t __pagesize = 0;
176
177 #ifndef O_NOFOLLOW
178 # define O_NOFOLLOW 0
179 #endif
180
181 #ifndef __ARCH_HAS_NO_LDSO__
__check_one_fd(int fd,int mode)182 static void __check_one_fd(int fd, int mode)
183 {
184 #ifdef __NOT_FOR_L4__
185 /* Check if the specified fd is already open */
186 if (fcntl(fd, F_GETFD) == -1)
187 {
188 /* The descriptor is probably not open, so try to use /dev/null */
189 int nullfd = open(_PATH_DEVNULL, mode);
190 /* /dev/null is major=1 minor=3. Make absolutely certain
191 * that is in fact the device that we have opened and not
192 * some other wierd file... [removed in uclibc] */
193 if (nullfd!=fd)
194 {
195 abort();
196 }
197 }
198 #endif
199 }
200
201 #ifndef SHARED
__check_suid(void)202 static int __check_suid(void)
203 {
204 #ifdef __NOT_FOR_L4__
205 uid_t uid, euid;
206 gid_t gid, egid;
207
208 uid = getuid();
209 euid = geteuid();
210 if (uid != euid)
211 return 1;
212 gid = getgid();
213 egid = getegid();
214 if (gid != egid)
215 return 1;
216 #endif
217 return 0; /* we are not suid */
218 }
219 #endif
220 #endif
221
222 /* __uClibc_init completely initialize uClibc so it is ready to use.
223 *
224 * On ELF systems (with a dynamic loader) this function must be called
225 * from the dynamic loader (see TIS and ELF Specification), so that
226 * constructors of shared libraries (which depend on libc) can use all
227 * the libc code without restriction. For this we link the shared
228 * version of the uClibc with -init __uClibc_init so DT_INIT for
229 * uClibc is the address of __uClibc_init
230 *
231 * In all other cases we call it from the main stub
232 * __uClibc_main.
233 */
234
235 extern void __uClibc_init(void) attribute_hidden;
__uClibc_init(void)236 void __uClibc_init(void)
237 {
238 /* Don't recurse */
239 if (__pagesize)
240 return;
241
242 /* Setup an initial value. This may not be perfect, but is
243 * better than malloc using __pagesize=0 for atexit, ctors, etc. */
244 __pagesize = PAGE_SIZE;
245
246 #ifdef __UCLIBC_HAS_THREADS__
247 /* Before we start initializing uClibc we have to call
248 * __pthread_initialize_minimal so we can use pthread_locks
249 * whenever they are needed.
250 */
251 #if !defined (__UCLIBC_HAS_THREADS_NATIVE__) || defined (SHARED)
252 if (likely(__pthread_initialize_minimal!=NULL))
253 #endif
254 __pthread_initialize_minimal();
255 #endif
256
257 #ifndef SHARED
258 # ifdef __UCLIBC_HAS_SSP__
259 /* Set up the stack checker's canary. */
260 stack_chk_guard = _dl_setup_stack_chk_guard();
261 # ifdef THREAD_SET_STACK_GUARD
262 THREAD_SET_STACK_GUARD (stack_chk_guard);
263 # else
264 __stack_chk_guard = stack_chk_guard;
265 # endif
266 # ifdef __UCLIBC_HAS_SSP_COMPAT__
267 __guard = stack_chk_guard;
268 # endif
269 # endif
270 #endif
271
272 #ifdef __UCLIBC_HAS_LOCALE__
273 /* Initialize the global locale structure. */
274 if (likely(not_null_ptr(_locale_init)))
275 _locale_init();
276 #endif
277
278 /*
279 * Initialize stdio here. In the static library case, this will
280 * be bypassed if not needed because of the weak alias above.
281 * Thus we get a nice size savings because the stdio functions
282 * won't be pulled into the final static binary unless used.
283 */
284 if (likely(not_null_ptr(_stdio_init)))
285 _stdio_init();
286
287 }
288
289 #ifdef __UCLIBC_CTOR_DTOR__
290 void attribute_hidden (*__app_fini)(void) = NULL;
291 #endif
292
293 void attribute_hidden (*__rtld_fini)(void) = NULL;
294
295 #ifndef L4_MINIMAL_LIBC
296 extern int attribute_hidden __libc_argc;
297 extern char attribute_hidden **__libc_argv;
298 #endif
299
300 extern void __uClibc_fini(void) attribute_hidden;
__uClibc_fini(void)301 void __uClibc_fini(void)
302 {
303 #ifdef __UCLIBC_CTOR_DTOR__
304 /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array finalisation is handled
305 * by __app_fini. */
306 # ifdef SHARED
307 _dl_app_fini_array();
308 # elif !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
309 size_t i = __fini_array_end - __fini_array_start;
310 while (i-- > 0)
311 (*__fini_array_start [i]) ();
312 # endif
313 if (__app_fini != NULL)
314 (__app_fini)();
315 #endif
316 if (__rtld_fini != NULL)
317 (__rtld_fini)();
318 }
319
320 #ifndef __NOT_FOR_L4__
321 extern void *l4re_global_env __attribute__((weak));
322 #endif
323 #ifndef SHARED
324 extern void __nptl_deallocate_tsd (void) __attribute ((weak));
325 extern unsigned int __nptl_nthreads __attribute ((weak));
326 #endif
327
328 /* __uClibc_main is the new main stub for uClibc. This function is
329 * called from crt1 (version 0.9.28 or newer), after ALL shared libraries
330 * are initialized, just before we call the application's main function.
331 */
332 void __uClibc_main(int (*main)(int, char **, char **), int argc,
333 char **argv, void (*app_init)(void), void (*app_fini)(void),
334 void (*rtld_fini)(void),
335 void *stack_end attribute_unused) attribute_noreturn;
__uClibc_main(int (* main)(int,char **,char **),int argc,char ** argv,void (* app_init)(void),void (* app_fini)(void),void (* rtld_fini)(void),void * stack_end attribute_unused)336 void __uClibc_main(int (*main)(int, char **, char **), int argc,
337 char **argv, void (*app_init)(void), void (*app_fini)(void),
338 void (*rtld_fini)(void), void *stack_end attribute_unused)
339 {
340 #if !defined __ARCH_HAS_NO_LDSO__ && !defined SHARED
341 unsigned long *aux_dat;
342 ElfW(auxv_t) auxvt[AT_EGID + 1];
343 #endif
344
345 #ifdef __UCLIBC_HAS_THREADS_NATIVE__
346 /* Result of the 'main' function. */
347 int result;
348 #endif
349
350 #ifndef SHARED
351 __libc_stack_end = stack_end;
352 #endif
353
354 __rtld_fini = rtld_fini;
355
356 #ifndef L4_MINIMAL_LIBC
357 // the availability of __libc_argc/__libc_argv seems to be
358 // a side effect of importing external functionality to uclibc
359 // by upstream. In our case this is only available in full builds.
360 __libc_argc = argc;
361 __libc_argv = argv;
362 #endif
363
364 /* The environment begins right after argv. */
365 __environ = &argv[argc + 1];
366
367 /* If the first thing after argv is the arguments
368 * then the environment is empty. */
369 if ((char *) __environ == *argv) {
370 /* Make __environ point to the NULL at argv[argc] */
371 __environ = &argv[argc];
372 }
373
374 #if !defined __ARCH_HAS_NO_LDSO__ && !defined SHARED
375 /* Pull stuff from the ELF header when possible */
376 memset(auxvt, 0x00, sizeof(auxvt));
377 aux_dat = (unsigned long*)__environ;
378 while (*aux_dat) {
379 aux_dat++;
380 }
381 aux_dat++;
382 while (*aux_dat) {
383 ElfW(auxv_t) *auxv_entry = (ElfW(auxv_t) *) aux_dat;
384 if (auxv_entry->a_type <= AT_EGID) {
385 memcpy(&(auxvt[auxv_entry->a_type]), auxv_entry, sizeof(ElfW(auxv_t)));
386 }
387 #ifndef __NOT_FOR_L4__
388 if (auxv_entry->a_type == 0xf1 && &l4re_global_env)
389 {
390 l4re_global_env = (void*)auxv_entry->a_un.a_val;
391 }
392 #endif
393 aux_dat += 2;
394 }
395 /* Get the program headers (_dl_phdr) from the aux vector
396 It will be used into __libc_setup_tls. */
397
398 _dl_aux_init (auxvt);
399 #endif
400
401 /* We need to initialize uClibc. If we are dynamically linked this
402 * may have already been completed by the shared lib loader. We call
403 * __uClibc_init() regardless, to be sure the right thing happens. */
404 __uClibc_init();
405
406 #ifndef __ARCH_HAS_NO_LDSO__
407 /* Make certain getpagesize() gives the correct answer.
408 * _dl_pagesize is defined into ld.so if SHARED or into libc.a otherwise. */
409 __pagesize = _dl_pagesize;
410
411 #ifndef SHARED
412 /* Prevent starting SUID binaries where the stdin. stdout, and
413 * stderr file descriptors are not already opened. */
414 if ((auxvt[AT_UID].a_un.a_val == (size_t)-1 && __check_suid()) ||
415 (auxvt[AT_UID].a_un.a_val != (size_t)-1 &&
416 (auxvt[AT_UID].a_un.a_val != auxvt[AT_EUID].a_un.a_val ||
417 auxvt[AT_GID].a_un.a_val != auxvt[AT_EGID].a_un.a_val)))
418 #else
419 if (_dl_secure)
420 #endif
421 {
422 __check_one_fd (STDIN_FILENO, O_RDONLY | O_NOFOLLOW);
423 __check_one_fd (STDOUT_FILENO, O_RDWR | O_NOFOLLOW);
424 __check_one_fd (STDERR_FILENO, O_RDWR | O_NOFOLLOW);
425 }
426 #endif
427
428 __uclibc_progname = *argv;
429 #if defined __UCLIBC_HAS___PROGNAME__ || (defined __USE_GNU && defined __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__)
430 if (*argv != NULL) {
431 __progname_full = *argv;
432 __progname = strrchr(*argv, '/');
433 if (__progname != NULL)
434 ++__progname;
435 else
436 __progname = *argv;
437 }
438 #endif
439
440 #ifdef __UCLIBC_CTOR_DTOR__
441 /* Arrange for the application's dtors to run before we exit. */
442 __app_fini = app_fini;
443
444 /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation is handled
445 * by __app_init. */
446 # if !defined (SHARED) && !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
447 /* For dynamically linked executables the preinit array is executed by
448 the dynamic linker (before initializing any shared object).
449 For static executables, preinit happens rights before init. */
450 {
451 const size_t size = __preinit_array_end - __preinit_array_start;
452 size_t i;
453 for (i = 0; i < size; i++)
454 (*__preinit_array_start [i]) ();
455 }
456 # endif
457 /* Run all the application's ctors now. */
458 if (app_init!=NULL) {
459 app_init();
460 }
461 /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation is handled
462 * by __app_init. */
463 # ifdef SHARED
464 _dl_app_init_array();
465 # elif !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
466 {
467 const size_t size = __init_array_end - __init_array_start;
468 size_t i;
469 for (i = 0; i < size; i++)
470 (*__init_array_start [i]) ();
471 }
472 # endif
473 #endif
474
475 /* Note: It is possible that any initialization done above could
476 * have resulted in errno being set nonzero, so set it to 0 before
477 * we call main.
478 */
479 if (likely(not_null_ptr(__errno_location)))
480 *(__errno_location()) = 0;
481
482 /* Set h_errno to 0 as well */
483 if (likely(not_null_ptr(__h_errno_location)))
484 *(__h_errno_location()) = 0;
485
486 #if defined HAVE_CLEANUP_JMP_BUF && defined __UCLIBC_HAS_THREADS_NATIVE__
487 /* Memory for the cancellation buffer. */
488 struct pthread_unwind_buf unwind_buf;
489
490 int not_first_call;
491 not_first_call =
492 setjmp ((struct __jmp_buf_tag *) unwind_buf.cancel_jmp_buf);
493 if (__builtin_expect (! not_first_call, 1))
494 {
495 struct pthread *self = THREAD_SELF;
496
497 /* Store old info. */
498 unwind_buf.priv.data.prev = THREAD_GETMEM (self, cleanup_jmp_buf);
499 unwind_buf.priv.data.cleanup = THREAD_GETMEM (self, cleanup);
500
501 /* Store the new cleanup handler info. */
502 THREAD_SETMEM (self, cleanup_jmp_buf, &unwind_buf);
503
504 /* Run the program. */
505 result = main (argc, argv, __environ);
506 }
507 else
508 {
509 /* Remove the thread-local data. */
510 # ifdef SHARED
511 __libc_pthread_functions.ptr__nptl_deallocate_tsd ();
512 # else
513 __nptl_deallocate_tsd ();
514 # endif
515
516 /* One less thread. Decrement the counter. If it is zero we
517 terminate the entire process. */
518 result = 0;
519 # ifdef SHARED
520 unsigned int *const ptr = __libc_pthread_functions.ptr_nthreads;
521 # else
522 unsigned int *const ptr = &__nptl_nthreads;
523 # endif
524
525 if (! atomic_decrement_and_test (ptr))
526 /* Not much left to do but to exit the thread, not the process. */
527 __exit_thread_inline (0);
528 }
529
530 exit (result);
531 #else
532 /*
533 * Finally, invoke application's main and then exit.
534 */
535 exit (main (argc, argv, __environ));
536 #endif
537 }
538