__uClibc_main.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 <unistd.h>
  15. #include <stdlib.h>
  16. /*
  17. * Prototypes.
  18. */
  19. extern int main(int argc, char **argv, char **envp);
  20. extern void weak_function _init(void);
  21. extern void weak_function __pthread_initialize_minimal(void);
  22. extern void weak_function _fini(void);
  23. extern void weak_function _stdio_init(void);
  24. extern int *weak_const_function __errno_location(void);
  25. extern int *weak_const_function __h_errno_location(void);
  26. extern int weak_function atexit(void (*function)(void));
  27. #ifdef __UCLIBC_HAS_LOCALE__
  28. extern void weak_function _locale_init(void);
  29. #endif
  30. /*
  31. * Declare the __environ global variable and create a weak alias environ.
  32. * Note: Apparently we must initialize __environ for the weak environ
  33. * symbol to be included.
  34. */
  35. char **__environ = 0;
  36. weak_alias(__environ, environ);
  37. void __attribute__ ((__noreturn__))
  38. __uClibc_main(int argc, char **argv, char **envp)
  39. {
  40. /* If we are dynamically linked the shared lib loader
  41. * already did this for us. But if we are statically
  42. * linked, we need to do this for ourselves. */
  43. if (__environ==NULL) {
  44. /* Statically linked. */
  45. __environ = envp;
  46. }
  47. #ifdef _LIBC_REENTRANT
  48. if (likely(__pthread_initialize_minimal!=NULL))
  49. __pthread_initialize_minimal();
  50. #endif
  51. #if 0
  52. /* Some security at this point. Prevent starting a SUID binary
  53. * where the standard file descriptors are not opened. We have
  54. * to do this only for statically linked applications since
  55. * otherwise the dynamic loader did the work already. */
  56. if (unlikely (__libc_enable_secure!=NULL))
  57. __libc_check_standard_fds ();
  58. #endif
  59. #ifdef __UCLIBC_HAS_LOCALE__
  60. /* Initialize the global locale structure. */
  61. if (likely(_locale_init!=NULL))
  62. _locale_init();
  63. #endif
  64. /*
  65. * Initialize stdio here. In the static library case, this will
  66. * be bypassed if not needed because of the weak alias above.
  67. */
  68. if (likely(_stdio_init != NULL))
  69. _stdio_init();
  70. /* Arrange for dtors to run at exit. */
  71. if (likely(_fini!=NULL && atexit)) {
  72. atexit (&_fini);
  73. }
  74. /* Run all ctors now. */
  75. if (likely(_init!=NULL))
  76. _init();
  77. /*
  78. * Note: It is possible that any initialization done above could
  79. * have resulted in errno being set nonzero, so set it to 0 before
  80. * we call main.
  81. */
  82. if (likely(__errno_location!=NULL))
  83. *(__errno_location()) = 0;
  84. /* Set h_errno to 0 as well */
  85. if (likely(__h_errno_location!=NULL))
  86. *(__h_errno_location()) = 0;
  87. /*
  88. * Finally, invoke application's main and then exit.
  89. */
  90. exit(main(argc, argv, envp));
  91. }