__uClibc_main.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Manuel Novoa III Feb 2001
  3. * Erik Anderseni 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 <stdlib.h>
  15. #include <unistd.h>
  16. #if !defined HAVE_ELF || !defined __UCLIBC_HAS_MMU__
  17. # undef weak_function
  18. # undef weak_const_function
  19. # define weak_function
  20. # define weak_const_function
  21. # define __USE_WEAK_ALIASES
  22. #endif
  23. /*
  24. * Prototypes.
  25. */
  26. extern int main(int argc, char **argv, char **envp);
  27. extern int weak_function atexit(void (*function)(void));
  28. extern void weak_function _init(void);
  29. extern void weak_function _fini(void);
  30. extern void weak_function _stdio_init(void);
  31. extern void weak_function _stdio_term(void);
  32. extern int *weak_const_function __errno_location(void);
  33. extern int *weak_const_function __h_errno_location(void);
  34. /*
  35. * Declare the __environ global variable and create a weak alias environ.
  36. * Note: Apparently we must initialize __environ for the weak environ
  37. * symbol to be included.
  38. */
  39. char **__environ = 0;
  40. weak_alias(__environ, environ);
  41. /*
  42. * Now for our main routine.
  43. */
  44. void __attribute__ ((__noreturn__))
  45. __uClibc_main(int argc, char **argv, char **envp)
  46. {
  47. /*
  48. * Initialize the global variable __environ.
  49. */
  50. __environ = envp;
  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))
  57. __libc_check_standard_fds ();
  58. #endif
  59. /*
  60. * Initialize stdio here. In the static library case, this will
  61. * be bypassed if not needed because of the weak alias above.
  62. */
  63. if (likely(_stdio_init))
  64. _stdio_init();
  65. /* Arrange for dtors to run at exit. */
  66. if (_fini && atexit) {
  67. atexit (&_fini);
  68. }
  69. /* Run all ctors now. */
  70. if (_init)
  71. _init();
  72. /*
  73. * Note: It is possible that any initialization done above could
  74. * have resulted in errno being set nonzero, so set it to 0 before
  75. * we call main.
  76. */
  77. if (likely(__errno_location))
  78. *(__errno_location()) = 0;
  79. /* Set h_errno to 0 as well */
  80. if (likely(__h_errno_location))
  81. *(__h_errno_location()) = 0;
  82. /*
  83. * Finally, invoke application's main and then exit.
  84. */
  85. exit(main(argc, argv, envp));
  86. }
  87. #ifdef __USE_WEAK_ALIASES
  88. /*
  89. * Define an empty function and use it as a weak alias for the stdio
  90. * initialization routine. That way we don't pull in all the stdio
  91. * code unless we need to. Similarly, do the same for _stdio_term
  92. * so as not to include atexit unnecessarily.
  93. *
  94. * NOTE!!! This is only true for the _static_ case!!!
  95. */
  96. weak_alias(__environ, environ);
  97. void __uClibc_empty_func(void)
  98. {
  99. }
  100. weak_alias(__uClibc_empty_func, atexit);
  101. weak_alias(__uClibc_empty_func, _init);
  102. weak_alias(__uClibc_empty_func, _fini);
  103. weak_alias(__uClibc_empty_func, __errno_location);
  104. weak_alias(__uClibc_empty_func, __h_errno_location);
  105. weak_alias(__uClibc_empty_func, _stdio_init);
  106. weak_alias(__uClibc_empty_func, _stdio_term);
  107. #endif