__uClibc_main.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Manuel Novoa III Feb 2001
  3. * Erik Andersen Mar 2002
  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. #ifdef __UCLIBC_PROPOLICE__
  18. extern void __guard_setup(void);
  19. #endif
  20. /*
  21. * Prototypes.
  22. */
  23. extern int main(int argc, char **argv, char **envp);
  24. extern void weak_function _stdio_init(void);
  25. extern int *weak_const_function __errno_location(void);
  26. extern int *weak_const_function __h_errno_location(void);
  27. #ifdef __UCLIBC_HAS_LOCALE__
  28. extern void weak_function _locale_init(void);
  29. #endif
  30. #ifdef __UCLIBC_HAS_THREADS__
  31. extern void weak_function __pthread_initialize_minimal(void);
  32. #endif
  33. /*
  34. * Declare the __environ global variable and create a weak alias environ.
  35. * Note: Apparently we must initialize __environ to ensure that the weak
  36. * environ symbol is also included.
  37. */
  38. char **__environ = 0;
  39. const char *__progname = 0;
  40. weak_alias(__environ, environ);
  41. /* __uClibc_init completely initialize uClibc so it is ready to use.
  42. *
  43. * On ELF systems (with a dynamic loader) this function must be called
  44. * from the dynamic loader (see TIS and ELF Specification), so that
  45. * constructors of shared libraries (which depend on libc) can use all
  46. * the libc code without restriction. For this we link the shared
  47. * version of the uClibc with -init __uClibc_init so DT_INIT for
  48. * uClibc is the address of __uClibc_init
  49. *
  50. * In all other cases we call it from the main stub
  51. * __uClibc_start_main.
  52. */
  53. void __uClibc_init(void)
  54. {
  55. static int been_there_done_that = 0;
  56. if (been_there_done_that)
  57. return;
  58. been_there_done_that++;
  59. #ifdef __UCLIBC_HAS_THREADS__
  60. /* Before we start initialzing uClibc we have to call
  61. * __pthread_initialize_minimal so we can use pthread_locks
  62. * whenever they are needed.
  63. */
  64. if (likely(__pthread_initialize_minimal!=NULL))
  65. __pthread_initialize_minimal();
  66. #endif
  67. #if 0
  68. /* Some security at this point. Prevent starting a SUID binary
  69. * where the standard file descriptors are not opened. We have
  70. * to do this only for statically linked applications since
  71. * otherwise the dynamic loader did the work already. */
  72. if (unlikely (__libc_enable_secure!=NULL))
  73. __libc_check_standard_fds ();
  74. #endif
  75. #ifdef __UCLIBC_HAS_LOCALE__
  76. /* Initialize the global locale structure. */
  77. if (likely(_locale_init!=NULL))
  78. _locale_init();
  79. #endif
  80. /*
  81. * Initialize stdio here. In the static library case, this will
  82. * be bypassed if not needed because of the weak alias above.
  83. */
  84. if (likely(_stdio_init != NULL))
  85. _stdio_init();
  86. }
  87. #ifdef __UCLIBC_CTOR_DTOR__
  88. void (*__app_fini)(void) = NULL;
  89. #endif
  90. /* __uClibc_start_main is the new main stub for uClibc. This function is
  91. * called from crt0 (version 0.9.16 or newer), after ALL shared libraries
  92. * are initialized, just before we call the application's main function.
  93. */
  94. void __attribute__ ((__noreturn__))
  95. __uClibc_start_main(int argc, char **argv, char **envp,
  96. void (*app_init)(void), void (*app_fini)(void))
  97. {
  98. /* If we are dynamically linked the shared lib loader already
  99. * did this for us. But if we are statically linked, we need
  100. * to do this for ourselves. */
  101. if (__environ==NULL) {
  102. /* Statically linked. */
  103. __environ = envp;
  104. }
  105. /* We need to initialize uClibc. If we are dynamically linked this
  106. * may have already been completed by the shared lib loader. We call
  107. * __uClibc_init() regardless, to be sure the right thing happens. */
  108. __uClibc_init();
  109. __progname = *argv;
  110. #ifdef __UCLIBC_CTOR_DTOR__
  111. /* Arrange for the application's dtors to run before we exit. */
  112. __app_fini = app_fini;
  113. /* Run all the application's ctors now. */
  114. if (app_init!=NULL) {
  115. app_init();
  116. }
  117. #endif
  118. #ifdef __UCLIBC_PROPOLICE__
  119. __guard_setup ();
  120. #endif
  121. /* Note: It is possible that any initialization done above could
  122. * have resulted in errno being set nonzero, so set it to 0 before
  123. * we call main.
  124. */
  125. if (likely(__errno_location!=NULL))
  126. *(__errno_location()) = 0;
  127. /* Set h_errno to 0 as well */
  128. if (likely(__h_errno_location!=NULL))
  129. *(__h_errno_location()) = 0;
  130. /*
  131. * Finally, invoke application's main and then exit.
  132. */
  133. exit(main(argc, argv, envp));
  134. }
  135. /* __uClibc_main is the old main stub of the uClibc. This
  136. * function is called from crt0 (uClibc 0.9.15 and older) after
  137. * ALL shared libraries are initialized, and just before we call
  138. * the application's main() function.
  139. *
  140. * Attention: This stub does not call the .init/.fini sections of
  141. * the application. If you need this, please fix your uClibc port
  142. * so that __uClibc_start_main is called by your crt0.S with
  143. * _init and _fini properly set.
  144. */
  145. void __attribute__ ((__noreturn__))
  146. __uClibc_main(int argc, char **argv, char ** envp)
  147. {
  148. __uClibc_start_main(argc, argv, envp, NULL, NULL);
  149. }