libc: Fix some unused parameter warnings
[uclibc-ng.git] / libc / misc / internals / __uClibc_main.c
blob64a9c82143fa3dcfb9915ef1131d31a0405f6eca
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>
6 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
8 * __uClibc_main is the routine to be called by all the arch-specific
9 * versions of crt1.S in uClibc.
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.
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 <not-cancel.h>
33 #include <atomic.h>
34 #include <tls.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
43 /* Are we in a secure process environment or are we dealing
44 * with setuid stuff? If we are dynamically linked, then we
45 * already have _dl_secure, otherwise we need to re-examine
46 * auxvt[] below.
48 int _pe_secure = 0;
49 libc_hidden_data_def(_pe_secure)
51 #if !defined(SHARED) && defined(__FDPIC__)
52 struct funcdesc_value
54 void *entry_point;
55 void *got_value;
56 } __attribute__((__aligned__(8)));
59 /* Prevent compiler optimization that removes GOT assignment.
61 Due to optimization passes (block split and move), in the rare case
62 where use r9 is the single instruction in a block we can have the
63 following behaviour:
64 - this block is marked as a forward block since use is not
65 considered as an active instruction after reload pass.
67 - In this case a jump in this block can be moved to the start of the
68 next one and so remove use in this flow of instructions which can
69 lead to a removal of r9 restoration after a call. */
70 #define _dl_stabilize_funcdesc(val) \
71 ({ __asm__ ("" : "+m" (*(val))); (val); })
73 static void fdpic_init_array_jump(void *addr)
75 struct funcdesc_value *fm = (struct funcdesc_value *) fdpic_init_array_jump;
76 struct funcdesc_value fd = {addr, fm->got_value};
78 void (*pf)(void) = (void*) _dl_stabilize_funcdesc(&fd);
80 (*pf)();
82 #endif
84 #ifndef SHARED
85 void *__libc_stack_end = NULL;
87 # ifdef __UCLIBC_HAS_SSP__
88 # include <dl-osinfo.h>
89 static uintptr_t stack_chk_guard;
90 # ifndef THREAD_SET_STACK_GUARD
91 /* Only exported for architectures that don't store the stack guard canary
92 * in thread local area. */
93 /* for gcc-4.1 non-TLS */
94 uintptr_t __stack_chk_guard attribute_relro;
95 # endif
96 # endif
99 * Needed to initialize _dl_phdr when statically linked
102 void internal_function _dl_aux_init (ElfW(auxv_t) *av);
104 #ifdef __UCLIBC_HAS_THREADS__
106 * uClibc internal locking requires that we have weak aliases
107 * for dummy functions in case a single threaded application is linked.
108 * This needs to be in compilation unit that is pulled always
109 * in or linker will disregard these weaks.
112 static int __pthread_return_0 (pthread_mutex_t *unused) { (void)unused; return 0; }
113 weak_alias (__pthread_return_0, __pthread_mutex_lock)
114 weak_alias (__pthread_return_0, __pthread_mutex_trylock)
115 weak_alias (__pthread_return_0, __pthread_mutex_unlock)
117 int weak_function
118 __pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
120 (void)mutex; (void)attr;
121 return 0;
124 void weak_function
125 _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer *__buffer,
126 void (*__routine) (void *), void *__arg)
128 __buffer->__routine = __routine;
129 __buffer->__arg = __arg;
132 void weak_function
133 _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer *__buffer,
134 int __execute)
136 if (__execute)
137 __buffer->__routine(__buffer->__arg);
140 #endif /* __UCLIBC_HAS_THREADS__ */
142 #endif /* !SHARED */
144 /* Defeat compiler optimization which assumes function addresses are never NULL */
145 static __always_inline int not_null_ptr(const void *p)
147 const void *q;
148 __asm__ (""
149 : "=r" (q) /* output */
150 : "0" (p) /* input */
152 return q != 0;
156 * Prototypes.
158 #ifdef __UCLIBC_HAS_THREADS__
159 #if !defined (__UCLIBC_HAS_THREADS_NATIVE__) || defined (SHARED)
160 extern void weak_function __pthread_initialize_minimal(void);
161 #else
162 extern void __pthread_initialize_minimal(void);
163 #endif
164 #endif
166 #ifndef SHARED
167 extern void __libc_setup_tls (size_t tcbsize, size_t tcbalign);
168 #endif
170 /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation and finalisation
171 * is handled by the routines passed to __uClibc_main(). */
172 #if defined (__UCLIBC_CTOR_DTOR__) && !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
173 extern void _dl_app_init_array(void);
174 extern void _dl_app_fini_array(void);
175 # ifndef SHARED
176 /* These magic symbols are provided by the linker. */
177 extern void (*__preinit_array_start []) (void) attribute_hidden;
178 extern void (*__preinit_array_end []) (void) attribute_hidden;
179 extern void (*__init_array_start []) (void) attribute_hidden;
180 extern void (*__init_array_end []) (void) attribute_hidden;
181 extern void (*__fini_array_start []) (void) attribute_hidden;
182 extern void (*__fini_array_end []) (void) attribute_hidden;
183 # endif
184 #endif
186 #ifdef SHARED
187 extern int _dl_secure;
188 #endif
189 extern size_t _dl_pagesize;
191 const char *__uclibc_progname = "";
192 #if !defined __UCLIBC_HAS___PROGNAME__ && defined __USE_GNU && defined __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
193 # define __progname program_invocation_short_name
194 # define __progname_full program_invocation_name
195 #endif
196 #if defined __UCLIBC_HAS___PROGNAME__ || (defined __USE_GNU && defined __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__)
197 const char *__progname = "";
198 /* psm: why have a visible __progname_full? */
199 const char *__progname_full = "";
200 # if defined __UCLIBC_HAS___PROGNAME__ && defined __USE_GNU && defined __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
201 weak_alias (__progname, program_invocation_short_name)
202 weak_alias (__progname_full, program_invocation_name)
203 # endif
204 #endif
207 * Declare the __environ global variable and create a weak alias environ.
208 * This must be initialized; we cannot have a weak alias into bss.
210 char **__environ = 0;
211 weak_alias(__environ, environ)
213 size_t __pagesize = 0;
215 #ifndef O_NOFOLLOW
216 # define O_NOFOLLOW 0
217 #endif
219 #ifndef __ARCH_HAS_NO_LDSO__
220 static void __check_one_fd(int fd, int mode)
222 /* Check if the specified fd is already open */
223 if (fcntl(fd, F_GETFD) == -1)
225 /* The descriptor is probably not open, so try to use /dev/null */
226 int nullfd = open(_PATH_DEVNULL, mode);
227 /* /dev/null is major=1 minor=3. Make absolutely certain
228 * that is in fact the device that we have opened and not
229 * some other wierd file... [removed in uclibc] */
230 if (nullfd!=fd)
232 abort();
237 #ifndef SHARED
238 static int __check_suid(void)
240 uid_t uid, euid;
241 gid_t gid, egid;
243 uid = getuid();
244 euid = geteuid();
245 if (uid != euid)
246 return 1;
247 gid = getgid();
248 egid = getegid();
249 if (gid != egid)
250 return 1;
251 return 0; /* we are not suid */
253 #endif
254 #endif
256 /* __uClibc_init completely initialize uClibc so it is ready to use.
258 * On ELF systems (with a dynamic loader) this function must be called
259 * from the dynamic loader (see TIS and ELF Specification), so that
260 * constructors of shared libraries (which depend on libc) can use all
261 * the libc code without restriction. For this we link the shared
262 * version of the uClibc with -init __uClibc_init so DT_INIT for
263 * uClibc is the address of __uClibc_init
265 * In all other cases we call it from the main stub
266 * __uClibc_main.
269 extern void __uClibc_init(void) attribute_hidden;
270 void __uClibc_init(void)
272 /* Don't recurse */
273 if (__pagesize)
274 return;
276 /* Setup an initial value. This may not be perfect, but is
277 * better than malloc using __pagesize=0 for atexit, ctors, etc. */
278 __pagesize = PAGE_SIZE;
280 #ifdef __UCLIBC_HAS_THREADS__
282 #if defined (__UCLIBC_HAS_THREADS_NATIVE__) && !defined (SHARED)
283 /* Unlike in the dynamically linked case the dynamic linker has not
284 taken care of initializing the TLS data structures. */
285 __libc_setup_tls (TLS_TCB_SIZE, TLS_TCB_ALIGN);
286 #endif
288 /* Before we start initializing uClibc we have to call
289 * __pthread_initialize_minimal so we can use pthread_locks
290 * whenever they are needed.
292 #if !defined (__UCLIBC_HAS_THREADS_NATIVE__) || defined (SHARED)
293 if (likely(__pthread_initialize_minimal!=NULL))
294 #endif
295 __pthread_initialize_minimal();
296 #endif
298 #ifndef SHARED
299 # ifdef __UCLIBC_HAS_SSP__
300 /* Set up the stack checker's canary. */
301 stack_chk_guard = _dl_setup_stack_chk_guard();
302 # ifdef THREAD_SET_STACK_GUARD
303 THREAD_SET_STACK_GUARD (stack_chk_guard);
304 # else
305 __stack_chk_guard = stack_chk_guard;
306 # endif
307 # endif
308 #endif
310 #ifdef __UCLIBC_HAS_LOCALE__
311 /* Initialize the global locale structure. */
312 if (likely(not_null_ptr(_locale_init)))
313 _locale_init();
314 #endif
317 * Initialize stdio here. In the static library case, this will
318 * be bypassed if not needed because of the weak alias above.
319 * Thus we get a nice size savings because the stdio functions
320 * won't be pulled into the final static binary unless used.
322 if (likely(not_null_ptr(_stdio_init)))
323 _stdio_init();
327 #ifdef __UCLIBC_CTOR_DTOR__
328 void attribute_hidden (*__app_fini)(void) = NULL;
329 #endif
331 void attribute_hidden (*__rtld_fini)(void) = NULL;
333 extern void __uClibc_fini(void) attribute_hidden;
334 void __uClibc_fini(void)
336 #ifdef __UCLIBC_CTOR_DTOR__
337 /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array finalisation is handled
338 * by __app_fini. */
339 # ifdef SHARED
340 _dl_app_fini_array();
341 # elif !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
342 size_t i = __fini_array_end - __fini_array_start;
343 while (i-- > 0)
344 #if !defined(SHARED) && defined(__FDPIC__)
345 fdpic_init_array_jump(__fini_array_start[i]);
346 #else
347 (*__fini_array_start [i]) ();
348 #endif
349 # endif
350 if (__app_fini != NULL)
351 (__app_fini)();
352 #endif
353 if (__rtld_fini != NULL)
354 (__rtld_fini)();
357 #ifndef SHARED
358 extern void __nptl_deallocate_tsd (void) __attribute ((weak));
359 extern unsigned int __nptl_nthreads __attribute ((weak));
360 #endif
362 /* __uClibc_main is the new main stub for uClibc. This function is
363 * called from crt1 (version 0.9.28 or newer), after ALL shared libraries
364 * are initialized, just before we call the application's main function.
366 void __uClibc_main(int (*main)(int, char **, char **), int argc,
367 char **argv, void (*app_init)(void), void (*app_fini)(void),
368 void (*rtld_fini)(void),
369 void *stack_end attribute_unused) attribute_noreturn;
370 void __uClibc_main(int (*main)(int, char **, char **), int argc,
371 char **argv, void (*app_init)(void), void (*app_fini)(void),
372 void (*rtld_fini)(void), void *stack_end attribute_unused)
374 #ifndef SHARED
375 unsigned long *aux_dat;
376 ElfW(auxv_t) auxvt[AT_EGID + 1];
377 #endif
379 #ifdef __UCLIBC_HAS_THREADS_NATIVE__
380 /* Result of the 'main' function. */
381 int result;
382 #endif
384 #ifndef SHARED
385 __libc_stack_end = stack_end;
386 #endif
388 __rtld_fini = rtld_fini;
390 /* The environment begins right after argv. */
391 __environ = &argv[argc + 1];
393 /* If the first thing after argv is the arguments
394 * then the environment is empty. */
395 if ((char *) __environ == *argv) {
396 /* Make __environ point to the NULL at argv[argc] */
397 __environ = &argv[argc];
400 #ifndef SHARED
401 /* Pull stuff from the ELF header when possible */
402 memset(auxvt, 0x00, sizeof(auxvt));
403 aux_dat = (unsigned long*)__environ;
404 while (*aux_dat) {
405 aux_dat++;
407 aux_dat++;
408 while (*aux_dat) {
409 ElfW(auxv_t) *auxv_entry = (ElfW(auxv_t) *) aux_dat;
410 if (auxv_entry->a_type <= AT_EGID) {
411 memcpy(&(auxvt[auxv_entry->a_type]), auxv_entry, sizeof(ElfW(auxv_t)));
413 aux_dat += 2;
415 /* Get the program headers (_dl_phdr) from the aux vector
416 It will be used into __libc_setup_tls. */
418 _dl_aux_init (auxvt);
419 #endif
421 /* We need to initialize uClibc. If we are dynamically linked this
422 * may have already been completed by the shared lib loader. We call
423 * __uClibc_init() regardless, to be sure the right thing happens. */
424 __uClibc_init();
426 #ifndef __ARCH_HAS_NO_LDSO__
427 /* Make certain getpagesize() gives the correct answer.
428 * _dl_pagesize is defined into ld.so if SHARED or into libc.a otherwise. */
429 __pagesize = _dl_pagesize;
431 #ifndef SHARED
432 /* Prevent starting SUID binaries where the stdin. stdout, and
433 * stderr file descriptors are not already opened. */
434 if ((auxvt[AT_UID].a_un.a_val == (size_t)-1 && __check_suid()) ||
435 (auxvt[AT_UID].a_un.a_val != (size_t)-1 &&
436 (auxvt[AT_UID].a_un.a_val != auxvt[AT_EUID].a_un.a_val ||
437 auxvt[AT_GID].a_un.a_val != auxvt[AT_EGID].a_un.a_val)))
438 #else
439 if (_dl_secure)
440 #endif
442 __check_one_fd (STDIN_FILENO, O_RDONLY | O_NOFOLLOW);
443 __check_one_fd (STDOUT_FILENO, O_RDWR | O_NOFOLLOW);
444 __check_one_fd (STDERR_FILENO, O_RDWR | O_NOFOLLOW);
445 _pe_secure = 1 ;
447 else
448 _pe_secure = 0 ;
449 #endif
451 __uclibc_progname = *argv;
452 #if defined __UCLIBC_HAS___PROGNAME__ || (defined __USE_GNU && defined __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__)
453 if (*argv != NULL) {
454 __progname_full = *argv;
455 __progname = strrchr(*argv, '/');
456 if (__progname != NULL)
457 ++__progname;
458 else
459 __progname = *argv;
461 #endif
463 #ifdef __UCLIBC_CTOR_DTOR__
464 /* Arrange for the application's dtors to run before we exit. */
465 __app_fini = app_fini;
467 /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation is handled
468 * by __app_init. */
469 # if !defined (SHARED) && !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
470 /* For dynamically linked executables the preinit array is executed by
471 the dynamic linker (before initializing any shared object).
472 For static executables, preinit happens rights before init. */
474 const size_t size = __preinit_array_end - __preinit_array_start;
475 size_t i;
476 for (i = 0; i < size; i++)
477 #if !defined(SHARED) && defined(__FDPIC__)
478 fdpic_init_array_jump(__preinit_array_start[i]);
479 #else
480 (*__preinit_array_start [i]) ();
481 #endif
483 # endif
484 /* Run all the application's ctors now. */
485 if (app_init!=NULL) {
486 app_init();
488 /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation is handled
489 * by __app_init. */
490 # ifdef SHARED
491 _dl_app_init_array();
492 # elif !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
494 const size_t size = __init_array_end - __init_array_start;
495 size_t i;
496 for (i = 0; i < size; i++)
497 #if !defined(SHARED) && defined(__FDPIC__)
498 fdpic_init_array_jump(__init_array_start[i]);
499 #else
500 (*__init_array_start [i]) ();
501 #endif
503 # endif
504 #endif
506 /* Note: It is possible that any initialization done above could
507 * have resulted in errno being set nonzero, so set it to 0 before
508 * we call main.
510 if (likely(not_null_ptr(__errno_location)))
511 *(__errno_location()) = 0;
513 /* Set h_errno to 0 as well */
514 if (likely(not_null_ptr(__h_errno_location)))
515 *(__h_errno_location()) = 0;
517 #if defined HAVE_CLEANUP_JMP_BUF && defined __UCLIBC_HAS_THREADS_NATIVE__
518 /* Memory for the cancellation buffer. */
519 struct pthread_unwind_buf unwind_buf;
521 int not_first_call;
522 not_first_call =
523 setjmp ((struct __jmp_buf_tag *) unwind_buf.cancel_jmp_buf);
524 if (__builtin_expect (! not_first_call, 1))
526 struct pthread *self = THREAD_SELF;
528 /* Store old info. */
529 unwind_buf.priv.data.prev = THREAD_GETMEM (self, cleanup_jmp_buf);
530 unwind_buf.priv.data.cleanup = THREAD_GETMEM (self, cleanup);
532 /* Store the new cleanup handler info. */
533 THREAD_SETMEM (self, cleanup_jmp_buf, &unwind_buf);
535 /* Run the program. */
536 result = main (argc, argv, __environ);
538 else
540 /* Remove the thread-local data. */
541 __nptl_deallocate_tsd ();
543 /* One less thread. Decrement the counter. If it is zero we
544 terminate the entire process. */
545 result = 0;
546 unsigned int *const ptr = &__nptl_nthreads;
548 if (ptr && ! atomic_decrement_and_test (ptr))
549 /* Not much left to do but to exit the thread, not the process. */
550 __exit_thread_inline (0);
553 exit (result);
554 #else
556 * Finally, invoke application's main and then exit.
558 exit (main (argc, argv, __environ));
559 #endif