__uClibc_main.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 <unistd.h>
  15. #if !defined HAVE_ELF
  16. /* This is a theoretical attempt to support old a.out compilers.
  17. * Dunno if this will work properly and I really don't much
  18. * care... Elf is the One True Path(tm). You will be assimilated */
  19. # define __USE_WEAK_ALIASES
  20. #endif
  21. /*
  22. * Prototypes.
  23. */
  24. extern int main(int argc, char **argv, char **envp);
  25. #ifndef __USE_WEAK_ALIASES
  26. #include <stdlib.h>
  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. #else
  35. /*
  36. * Define an empty function and use it as a weak alias for the stdio
  37. * initialization routine. That way we don't pull in all the stdio
  38. * code unless we need to. Similarly, do the same for _stdio_term
  39. * so as not to include atexit unnecessarily.
  40. *
  41. * NOTE!!! This is only true for the _static_ case!!!
  42. */
  43. weak_alias(__environ, environ);
  44. void __uClibc_empty_func(void)
  45. {
  46. }
  47. extern void exit (int status) __attribute__ ((__noreturn__));
  48. extern void _init(void);
  49. extern void _fini(void);
  50. extern void _stdio_init(void);
  51. weak_alias(__uClibc_empty_func, _init);
  52. weak_alias(__uClibc_empty_func, _fini);
  53. weak_alias(__uClibc_empty_func, _stdio_init);
  54. weak_alias(__uClibc_empty_func, _stdio_term);
  55. //weak_alias(__uClibc_empty_func, atexit);
  56. extern int atexit(void (*function)(void));
  57. //weak_alias(__uClibc_empty_func, __errno_location);
  58. extern int *__errno_location(void);
  59. //weak_alias(__uClibc_empty_func, __h_errno_location);
  60. extern int *__h_errno_location(void);
  61. #endif
  62. /*
  63. * Declare the __environ global variable and create a weak alias environ.
  64. * Note: Apparently we must initialize __environ for the weak environ
  65. * symbol to be included.
  66. */
  67. char **__environ = 0;
  68. weak_alias(__environ, environ);
  69. /*
  70. * Now for our main routine.
  71. */
  72. void __attribute__ ((__noreturn__))
  73. __uClibc_main(int argc, char **argv, char **envp)
  74. {
  75. /*
  76. * Initialize the global variable __environ.
  77. */
  78. __environ = envp;
  79. #if 0
  80. /* Some security at this point. Prevent starting a SUID binary
  81. * where the standard file descriptors are not opened. We have
  82. * to do this only for statically linked applications since
  83. * otherwise the dynamic loader did the work already. */
  84. if (unlikely (__libc_enable_secure))
  85. __libc_check_standard_fds ();
  86. #endif
  87. /*
  88. * Initialize stdio here. In the static library case, this will
  89. * be bypassed if not needed because of the weak alias above.
  90. */
  91. if (likely(_stdio_init))
  92. _stdio_init();
  93. /* Arrange for dtors to run at exit. */
  94. if (_fini && atexit) {
  95. atexit (&_fini);
  96. }
  97. /* Run all ctors now. */
  98. if (_init)
  99. _init();
  100. /*
  101. * Note: It is possible that any initialization done above could
  102. * have resulted in errno being set nonzero, so set it to 0 before
  103. * we call main.
  104. */
  105. if (likely(__errno_location))
  106. *(__errno_location()) = 0;
  107. /* Set h_errno to 0 as well */
  108. if (likely(__h_errno_location))
  109. *(__h_errno_location()) = 0;
  110. /*
  111. * Finally, invoke application's main and then exit.
  112. */
  113. exit(main(argc, argv, envp));
  114. }