__uClibc_main.c 2.9 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. #include <stdlib.h>
  13. #include <unistd.h>
  14. #include <errno.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 __init_stdio(void);
  24. extern void weak_function __stdio_close_all(void);
  25. extern void weak_function __pthread_initialize_minimal (void);
  26. #else
  27. extern void __init_stdio(void);
  28. extern void __stdio_close_all(void);
  29. extern void __pthread_initialize_minimal (void);
  30. #endif
  31. typedef void (*vfuncp) (void);
  32. vfuncp __uClibc_cleanup = __stdio_close_all;
  33. /*
  34. * Now for our main routine.
  35. */
  36. void __uClibc_main(int argc, char **argv, char **envp)
  37. {
  38. /*
  39. * Initialize the global variable __environ.
  40. */
  41. __environ = envp;
  42. /* Initialize the thread library at least a bit so at least
  43. * errno will be properly setup */
  44. if (__pthread_initialize_minimal)
  45. __pthread_initialize_minimal ();
  46. #if 0
  47. /* Some security at this point. Prevent starting a SUID binary
  48. * where the standard file descriptors are not opened. We have
  49. * to do this only for statically linked applications since
  50. * otherwise the dynamic loader did the work already. */
  51. if (__builtin_expect (__libc_enable_secure, 0))
  52. __libc_check_standard_fds ();
  53. #endif
  54. /*
  55. * Initialize stdio here. In the static library case, this will
  56. * be bypassed if not needed because of the weak alias above.
  57. */
  58. if (__init_stdio)
  59. __init_stdio();
  60. /*
  61. * Note: It is possible that any initialization done above could
  62. * have resulted in errno being set nonzero, so set it to 0 before
  63. * we call main.
  64. */
  65. __set_errno(0);
  66. /*
  67. * Finally, invoke application's main and then exit.
  68. */
  69. exit(main(argc, argv, envp));
  70. }
  71. /*
  72. * Declare the __environ global variable and create a weak alias environ.
  73. * Note: Apparently we must initialize __environ for the weak environ
  74. * symbol to be included.
  75. */
  76. char **__environ = 0;
  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_close_all
  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, __init_stdio);
  92. weak_alias(__uClibc_empty_func, __stdio_close_all);
  93. #endif
  94. #endif