__uClibc_main.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Manuel Novoa III Feb 2001
  3. * Erik Andersen 2002-2004
  4. *
  5. * __uClibc_main is the routine to be called by all the arch-specific
  6. * versions of crt0.S in uClibc.
  7. *
  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.
  12. */
  13. #define _ERRNO_H
  14. #include <features.h>
  15. #include <unistd.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <elf.h>
  19. #include <bits/uClibc_page.h>
  20. #ifdef __UCLIBC_PROPOLICE__
  21. extern void __guard_setup(void);
  22. #endif
  23. /*
  24. * Prototypes.
  25. */
  26. extern int main(int argc, char **argv, char **envp);
  27. extern void weak_function _stdio_init(void);
  28. extern int *weak_const_function __errno_location(void);
  29. extern int *weak_const_function __h_errno_location(void);
  30. #ifdef __UCLIBC_HAS_LOCALE__
  31. extern void weak_function _locale_init(void);
  32. #endif
  33. #ifdef __UCLIBC_HAS_THREADS__
  34. extern void weak_function __pthread_initialize_minimal(void);
  35. #endif
  36. /*
  37. * Declare the __environ global variable and create a weak alias environ.
  38. * Note: Apparently we must initialize __environ to ensure that the weak
  39. * environ symbol is also included.
  40. */
  41. size_t __pagesize = 0;
  42. char **__environ = 0;
  43. const char *__progname = 0;
  44. weak_alias(__environ, environ);
  45. /* FIXME */
  46. #if 0
  47. extern int _dl_secure;
  48. int __secure = 0;
  49. weak_alias(__secure, _dl_secure);
  50. #endif
  51. /* __uClibc_init completely initialize uClibc so it is ready to use.
  52. *
  53. * On ELF systems (with a dynamic loader) this function must be called
  54. * from the dynamic loader (see TIS and ELF Specification), so that
  55. * constructors of shared libraries (which depend on libc) can use all
  56. * the libc code without restriction. For this we link the shared
  57. * version of the uClibc with -init __uClibc_init so DT_INIT for
  58. * uClibc is the address of __uClibc_init
  59. *
  60. * In all other cases we call it from the main stub
  61. * __uClibc_start_main.
  62. */
  63. void __uClibc_init(void)
  64. {
  65. static int been_there_done_that = 0;
  66. if (been_there_done_that)
  67. return;
  68. been_there_done_that++;
  69. /* Setup an initial value. This may not be perfect, but is
  70. * better than malloc using __pagesize=0 for atexit, ctors, etc. */
  71. __pagesize = PAGE_SIZE;
  72. #ifdef __UCLIBC_HAS_THREADS__
  73. /* Before we start initialzing uClibc we have to call
  74. * __pthread_initialize_minimal so we can use pthread_locks
  75. * whenever they are needed.
  76. */
  77. if (likely(__pthread_initialize_minimal!=NULL))
  78. __pthread_initialize_minimal();
  79. #endif
  80. /* FIXME */
  81. #if 0
  82. /* Some security at this point. Prevent starting a SUID binary
  83. * where the standard file descriptors are not opened. We have
  84. * to do this only for statically linked applications since
  85. * otherwise the dynamic loader did the work already. */
  86. if (unlikely (__secure!=NULL))
  87. __libc_check_standard_fds ();
  88. #endif
  89. #ifdef __UCLIBC_HAS_LOCALE__
  90. /* Initialize the global locale structure. */
  91. if (likely(_locale_init!=NULL))
  92. _locale_init();
  93. #endif
  94. /*
  95. * Initialize stdio here. In the static library case, this will
  96. * be bypassed if not needed because of the weak alias above.
  97. */
  98. if (likely(_stdio_init != NULL))
  99. _stdio_init();
  100. }
  101. #ifdef __UCLIBC_CTOR_DTOR__
  102. void (*__app_fini)(void) = NULL;
  103. #endif
  104. /* __uClibc_start_main is the new main stub for uClibc. This function is
  105. * called from crt0 (version 0.9.16 or newer), after ALL shared libraries
  106. * are initialized, just before we call the application's main function.
  107. */
  108. void __attribute__ ((__noreturn__))
  109. __uClibc_start_main(int argc, char **argv, char **envp,
  110. void (*app_init)(void), void (*app_fini)(void))
  111. {
  112. #ifdef __ARCH_HAS_MMU__
  113. unsigned long *aux_dat;
  114. Elf32_auxv_t auxvt[AT_EGID + 1];
  115. #endif
  116. /* We need to initialize uClibc. If we are dynamically linked this
  117. * may have already been completed by the shared lib loader. We call
  118. * __uClibc_init() regardless, to be sure the right thing happens. */
  119. __uClibc_init();
  120. /* If we are dynamically linked, then ldso already did this for us. */
  121. if (__environ==NULL) {
  122. /* Statically linked. */
  123. __environ = envp;
  124. }
  125. /* Pull stuff from the ELF header when possible */
  126. #ifdef __ARCH_HAS_MMU__
  127. aux_dat = (unsigned long*)envp;
  128. while (*aux_dat) {
  129. aux_dat++;
  130. }
  131. aux_dat++;
  132. while (*aux_dat) {
  133. Elf32_auxv_t *auxv_entry = (Elf32_auxv_t *) aux_dat;
  134. if (auxv_entry->a_type <= AT_EGID) {
  135. memcpy(&(auxvt[auxv_entry->a_type]), auxv_entry, sizeof(Elf32_auxv_t));
  136. }
  137. aux_dat += 2;
  138. }
  139. __pagesize = (auxvt[AT_PAGESZ].a_un.a_val)? auxvt[AT_PAGESZ].a_un.a_val : PAGE_SIZE;
  140. #endif
  141. __progname = *argv;
  142. #ifdef __UCLIBC_CTOR_DTOR__
  143. /* Arrange for the application's dtors to run before we exit. */
  144. __app_fini = app_fini;
  145. /* Run all the application's ctors now. */
  146. if (app_init!=NULL) {
  147. app_init();
  148. }
  149. #endif
  150. #ifdef __UCLIBC_PROPOLICE__
  151. __guard_setup ();
  152. #endif
  153. /* Note: It is possible that any initialization done above could
  154. * have resulted in errno being set nonzero, so set it to 0 before
  155. * we call main.
  156. */
  157. if (likely(__errno_location!=NULL))
  158. *(__errno_location()) = 0;
  159. /* Set h_errno to 0 as well */
  160. if (likely(__h_errno_location!=NULL))
  161. *(__h_errno_location()) = 0;
  162. /*
  163. * Finally, invoke application's main and then exit.
  164. */
  165. exit(main(argc, argv, envp));
  166. }
  167. /* __uClibc_main is the old main stub of the uClibc. This
  168. * function is called from crt0 (uClibc 0.9.15 and older) after
  169. * ALL shared libraries are initialized, and just before we call
  170. * the application's main() function.
  171. *
  172. * Attention: This stub does not call the .init/.fini sections of
  173. * the application. If you need this, please fix your uClibc port
  174. * so that __uClibc_start_main is called by your crt0.S with
  175. * _init and _fini properly set.
  176. */
  177. void __attribute__ ((__noreturn__))
  178. __uClibc_main(int argc, char **argv, char ** envp)
  179. {
  180. __uClibc_start_main(argc, argv, envp, NULL, NULL);
  181. }