__uClibc_main.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. weak_alias(__uClibc_empty_func, _init);
  49. weak_alias(__uClibc_empty_func, _fini);
  50. weak_alias(__uClibc_empty_func, _stdio_init);
  51. weak_alias(__uClibc_empty_func, _stdio_term);
  52. //weak_alias(__uClibc_empty_func, atexit);
  53. extern int atexit(void (*function)(void));
  54. //weak_alias(__uClibc_empty_func, __errno_location);
  55. extern int *__errno_location(void);
  56. //weak_alias(__uClibc_empty_func, __h_errno_location);
  57. extern int *__h_errno_location(void);
  58. #endif
  59. /*
  60. * Declare the __environ global variable and create a weak alias environ.
  61. * Note: Apparently we must initialize __environ for the weak environ
  62. * symbol to be included.
  63. */
  64. char **__environ = 0;
  65. weak_alias(__environ, environ);
  66. /*
  67. * Now for our main routine.
  68. */
  69. void __attribute__ ((__noreturn__))
  70. __uClibc_main(int argc, char **argv, char **envp)
  71. {
  72. /*
  73. * Initialize the global variable __environ.
  74. */
  75. __environ = envp;
  76. #if 0
  77. /* Some security at this point. Prevent starting a SUID binary
  78. * where the standard file descriptors are not opened. We have
  79. * to do this only for statically linked applications since
  80. * otherwise the dynamic loader did the work already. */
  81. if (unlikely (__libc_enable_secure))
  82. __libc_check_standard_fds ();
  83. #endif
  84. /*
  85. * Initialize stdio here. In the static library case, this will
  86. * be bypassed if not needed because of the weak alias above.
  87. */
  88. if (likely(_stdio_init))
  89. _stdio_init();
  90. /* Arrange for dtors to run at exit. */
  91. if (_fini && atexit) {
  92. atexit (&_fini);
  93. }
  94. /* Run all ctors now. */
  95. if (_init)
  96. _init();
  97. /*
  98. * Note: It is possible that any initialization done above could
  99. * have resulted in errno being set nonzero, so set it to 0 before
  100. * we call main.
  101. */
  102. if (likely(__errno_location))
  103. *(__errno_location()) = 0;
  104. /* Set h_errno to 0 as well */
  105. if (likely(__h_errno_location))
  106. *(__h_errno_location()) = 0;
  107. /*
  108. * Finally, invoke application's main and then exit.
  109. */
  110. exit(main(argc, argv, envp));
  111. }