__uClibc_main.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Manuel Novoa III Feb 2001
  3. *
  4. * __uClibc_main is the routine to be called by all the arch-specific
  5. * versions of crt0.S in uClibc.
  6. *
  7. * It is meant to handle any special initialization needed by the library
  8. * such as setting the global variable(s) __environ (environ) and
  9. * initializing the stdio package. Using weak symbols, the latter is
  10. * avoided in the static library case.
  11. */
  12. #define _ERRNO_H
  13. #include <stdlib.h>
  14. #include <unistd.h>
  15. /*
  16. * Prototypes.
  17. */
  18. extern int main(int argc, char **argv, char **envp);
  19. void __uClibc_main(int argc, char **argv, char **envp)
  20. __attribute__ ((__noreturn__));
  21. #ifdef HAVE_ELF
  22. weak_alias(__environ, environ);
  23. extern void weak_function _stdio_init(void);
  24. extern void weak_function _stdio_term(void);
  25. extern int *weak_const_function __errno_location (void);
  26. extern int *weak_const_function __h_errno_location (void);
  27. #else
  28. extern void _stdio_init(void);
  29. extern void _stdio_term(void);
  30. extern int *__errno_location (void);
  31. extern int *__h_errno_location (void);
  32. #endif
  33. /*
  34. * Declare the __environ global variable and create a weak alias environ.
  35. * Note: Apparently we must initialize __environ for the weak environ
  36. * symbol to be included.
  37. */
  38. char **__environ = 0;
  39. /*
  40. * Now for our main routine.
  41. */
  42. void __uClibc_main(int argc, char **argv, char **envp)
  43. {
  44. /*
  45. * Initialize the global variable __environ.
  46. */
  47. __environ = envp;
  48. #if 0
  49. /* Some security at this point. Prevent starting a SUID binary
  50. * where the standard file descriptors are not opened. We have
  51. * to do this only for statically linked applications since
  52. * otherwise the dynamic loader did the work already. */
  53. if (__builtin_expect (__libc_enable_secure, 0))
  54. __libc_check_standard_fds ();
  55. #endif
  56. /*
  57. * Initialize stdio here. In the static library case, this will
  58. * be bypassed if not needed because of the weak alias above.
  59. */
  60. if (_stdio_init)
  61. _stdio_init();
  62. /*
  63. * Note: It is possible that any initialization done above could
  64. * have resulted in errno being set nonzero, so set it to 0 before
  65. * we call main.
  66. */
  67. if (__errno_location)
  68. *(__errno_location()) = 0;
  69. /* Set h_errno to 0 as well */
  70. if (__h_errno_location)
  71. *(__h_errno_location()) = 0;
  72. /*
  73. * Finally, invoke application's main and then exit.
  74. */
  75. exit(main(argc, argv, envp));
  76. }
  77. #ifndef HAVE_ELF
  78. /*
  79. * Define an empty function and use it as a weak alias for the stdio
  80. * initialization routine. That way we don't pull in all the stdio
  81. * code unless we need to. Similarly, do the same for _stdio_term
  82. * so as not to include atexit unnecessarily.
  83. *
  84. * NOTE!!! This is only true for the _static_ case!!!
  85. */
  86. weak_alias(__environ, environ);
  87. #if 0
  88. void __uClibc_empty_func(void)
  89. {
  90. }
  91. weak_alias(__uClibc_empty_func, _stdio_init);
  92. weak_alias(__uClibc_empty_func, _stdio_term);
  93. #endif
  94. #endif