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