__uClibc_main.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. atexit (&_fini);
  67. /* Run all ctors now. */
  68. _init();
  69. /*
  70. * Note: It is possible that any initialization done above could
  71. * have resulted in errno being set nonzero, so set it to 0 before
  72. * we call main.
  73. */
  74. if (likely(__errno_location))
  75. *(__errno_location()) = 0;
  76. /* Set h_errno to 0 as well */
  77. if (likely(__h_errno_location))
  78. *(__h_errno_location()) = 0;
  79. /*
  80. * Finally, invoke application's main and then exit.
  81. */
  82. exit(main(argc, argv, envp));
  83. }
  84. #ifdef __USE_WEAK_ALIASES
  85. /*
  86. * Define an empty function and use it as a weak alias for the stdio
  87. * initialization routine. That way we don't pull in all the stdio
  88. * code unless we need to. Similarly, do the same for _stdio_term
  89. * so as not to include atexit unnecessarily.
  90. *
  91. * NOTE!!! This is only true for the _static_ case!!!
  92. */
  93. weak_alias(__environ, environ);
  94. void __uClibc_empty_func(void)
  95. {
  96. }
  97. weak_alias(__uClibc_empty_func, atexit);
  98. weak_alias(__uClibc_empty_func, _init);
  99. weak_alias(__uClibc_empty_func, _fini);
  100. weak_alias(__uClibc_empty_func, __errno_location);
  101. weak_alias(__uClibc_empty_func, __h_errno_location);
  102. weak_alias(__uClibc_empty_func, _stdio_init);
  103. weak_alias(__uClibc_empty_func, _stdio_term);
  104. #endif