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.
23 #include <bits/uClibc_page.h>
28 #ifndef __ARCH_HAS_NO_LDSO__
31 #ifdef __UCLIBC_HAS_THREADS_NATIVE__
32 #include <not-cancel.h>
36 #ifdef __UCLIBC_HAS_THREADS__
39 #ifdef __UCLIBC_HAS_LOCALE__
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
49 libc_hidden_data_def(_pe_secure
)
51 #if !defined(SHARED) && defined(__FDPIC__)
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
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
);
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
;
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
)
118 __pthread_mutex_init(pthread_mutex_t
*mutex
, const pthread_mutexattr_t
*attr
)
120 (void)mutex
; (void)attr
;
125 _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer
*__buffer
,
126 void (*__routine
) (void *), void *__arg
)
128 __buffer
->__routine
= __routine
;
129 __buffer
->__arg
= __arg
;
133 _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer
*__buffer
,
137 __buffer
->__routine(__buffer
->__arg
);
140 #endif /* __UCLIBC_HAS_THREADS__ */
144 /* Defeat compiler optimization which assumes function addresses are never NULL */
145 static __always_inline
int not_null_ptr(const void *p
)
149 : "=r" (q
) /* output */
150 : "0" (p
) /* input */
158 #ifdef __UCLIBC_HAS_THREADS__
159 #if !defined (__UCLIBC_HAS_THREADS_NATIVE__) || defined (SHARED)
160 extern void weak_function
__pthread_initialize_minimal(void);
162 extern void __pthread_initialize_minimal(void);
167 extern void __libc_setup_tls (size_t tcbsize
, size_t tcbalign
);
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);
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
;
187 extern int _dl_secure
;
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
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
)
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;
216 # define O_NOFOLLOW 0
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] */
238 static int __check_suid(void)
251 return 0; /* we are not suid */
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
269 extern void __uClibc_init(void) attribute_hidden
;
270 void __uClibc_init(void)
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
);
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
))
295 __pthread_initialize_minimal();
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
);
305 __stack_chk_guard
= stack_chk_guard
;
310 #ifdef __UCLIBC_HAS_LOCALE__
311 /* Initialize the global locale structure. */
312 if (likely(not_null_ptr(_locale_init
)))
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
)))
327 #ifdef __UCLIBC_CTOR_DTOR__
328 void attribute_hidden (*__app_fini
)(void) = NULL
;
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
340 _dl_app_fini_array();
341 # elif !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
342 size_t i
= __fini_array_end
- __fini_array_start
;
344 #if !defined(SHARED) && defined(__FDPIC__)
345 fdpic_init_array_jump(__fini_array_start
[i
]);
347 (*__fini_array_start
[i
]) ();
350 if (__app_fini
!= NULL
)
353 if (__rtld_fini
!= NULL
)
358 extern void __nptl_deallocate_tsd (void) __attribute ((weak
));
359 extern unsigned int __nptl_nthreads
__attribute ((weak
));
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
)
375 unsigned long *aux_dat
;
376 ElfW(auxv_t
) auxvt
[AT_EGID
+ 1];
379 #ifdef __UCLIBC_HAS_THREADS_NATIVE__
380 /* Result of the 'main' function. */
385 __libc_stack_end
= stack_end
;
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
];
401 /* Pull stuff from the ELF header when possible */
402 memset(auxvt
, 0x00, sizeof(auxvt
));
403 aux_dat
= (unsigned long*)__environ
;
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
)));
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
);
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. */
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
;
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
)))
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
);
451 __uclibc_progname
= *argv
;
452 #if defined __UCLIBC_HAS___PROGNAME__ || (defined __USE_GNU && defined __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__)
454 __progname_full
= *argv
;
455 __progname
= strrchr(*argv
, '/');
456 if (__progname
!= NULL
)
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
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
;
476 for (i
= 0; i
< size
; i
++)
477 #if !defined(SHARED) && defined(__FDPIC__)
478 fdpic_init_array_jump(__preinit_array_start
[i
]);
480 (*__preinit_array_start
[i
]) ();
484 /* Run all the application's ctors now. */
485 if (app_init
!=NULL
) {
488 /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation is handled
491 _dl_app_init_array();
492 # elif !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
494 const size_t size
= __init_array_end
- __init_array_start
;
496 for (i
= 0; i
< size
; i
++)
497 #if !defined(SHARED) && defined(__FDPIC__)
498 fdpic_init_array_jump(__init_array_start
[i
]);
500 (*__init_array_start
[i
]) ();
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
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
;
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
);
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. */
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);
556 * Finally, invoke application's main and then exit.
558 exit (main (argc
, argv
, __environ
));