__uClibc_main.c 2.7 KB

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