__uClibc_main.c 5.0 KB

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