__uClibc_main.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. extern void __uClibc_empty_func(void);
  20. void __uClibc_main(int argc, char **argv, char **envp)
  21. __attribute__ ((__noreturn__));
  22. #ifdef HAVE_ELF
  23. weak_alias(__environ, environ);
  24. weak_symbol(__init_stdio);
  25. weak_symbol(__stdio_close_all);
  26. #endif
  27. extern void __init_stdio(void);
  28. extern void __stdio_close_all(void);
  29. typedef void (*vfuncp) (void);
  30. vfuncp __uClibc_cleanup = __stdio_close_all;
  31. /*
  32. * Now for our main routine.
  33. */
  34. void __uClibc_main(int argc, char **argv, char **envp)
  35. {
  36. /*
  37. * Initialize the global variable __environ.
  38. */
  39. __environ = envp;
  40. /*
  41. * Initialize stdio here. In the static library case, this will
  42. * be bypassed if not needed because of the weak alias above.
  43. */
  44. if (__init_stdio)
  45. __init_stdio();
  46. /*
  47. * Note: It is possible that any initialization done above could
  48. * have resulted in errno being set nonzero, so set it to 0 before
  49. * we call main.
  50. */
  51. __set_errno(0);
  52. /*
  53. * Finally, invoke application's main and then exit.
  54. */
  55. exit(main(argc, argv, envp));
  56. }
  57. /*
  58. * Declare the __environ global variable and create a weak alias environ.
  59. * Note: Apparently we must initialize __environ for the weak environ
  60. * symbol to be included.
  61. */
  62. char **__environ = 0;
  63. #ifndef HAVE_ELF
  64. /*
  65. * Define an empty function and use it as a weak alias for the stdio
  66. * initialization routine. That way we don't pull in all the stdio
  67. * code unless we need to. Similarly, do the same for __stdio_close_all
  68. * so as not to include atexit unnecessarily.
  69. *
  70. * NOTE!!! This is only true for the _static_ case!!!
  71. */
  72. void __uClibc_empty_func(void)
  73. {
  74. }
  75. weak_alias(__environ, environ);
  76. weak_alias(__uClibc_empty_func, __init_stdio);
  77. weak_alias(__uClibc_empty_func, __stdio_close_all);
  78. #endif