__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_alias(__uClibc_empty_func, __init_stdio);
  25. weak_alias(__uClibc_empty_func, __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. __init_stdio();
  45. /*
  46. * Note: It is possible that any initialization done above could
  47. * have resulted in errno being set nonzero, so set it to 0 before
  48. * we call main.
  49. */
  50. __set_errno(0);
  51. /*
  52. * Finally, invoke application's main and then exit.
  53. */
  54. exit(main(argc, argv, envp));
  55. }
  56. /*
  57. * Declare the __environ global variable and create a weak alias environ.
  58. * Note: Apparently we must initialize __environ for the weak environ
  59. * symbol to be included.
  60. */
  61. char **__environ = 0;
  62. /*
  63. * Define an empty function and use it as a weak alias for the stdio
  64. * initialization routine. That way we don't pull in all the stdio
  65. * code unless we need to. Similarly, do the same for __stdio_close_all
  66. * so as not to include atexit unnecessarily.
  67. *
  68. * NOTE!!! This is only true for the _static_ case!!!
  69. */
  70. void __uClibc_empty_func(void)
  71. {
  72. }
  73. #ifndef HAVE_ELF
  74. weak_alias(__environ, environ);
  75. weak_alias(__uClibc_empty_func, __init_stdio);
  76. weak_alias(__uClibc_empty_func, __stdio_close_all);
  77. #endif