__uClibc_main.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. extern void __init_stdio(void);
  33. extern void __stdio_close_all(void);
  34. typedef void (*vfuncp) (void);
  35. vfuncp __uClibc_cleanup = __stdio_close_all;
  36. /*
  37. * Now for our main routine.
  38. */
  39. void __uClibc_main(int argc, char **argv, char **envp)
  40. {
  41. /*
  42. * Initialize the global variable __environ.
  43. */
  44. __environ = envp;
  45. /*
  46. * Initialize stdio here. In the static library case, this will
  47. * be bypassed if not needed because of the weak alias above.
  48. */
  49. __init_stdio();
  50. /*
  51. * Note: It is possible that any initialization done above could
  52. * have resulted in errno being set nonzero, so set it to 0 before
  53. * we call main.
  54. */
  55. __set_errno(0);
  56. /*
  57. * Finally, invoke application's main and then exit.
  58. */
  59. exit(main(argc, argv, envp));
  60. }
  61. /*
  62. * Declare the __environ global variable and create a weak alias environ.
  63. * Note: Apparently we must initialize __environ for the weak environ
  64. * symbol to be included.
  65. */
  66. char **__environ = 0;
  67. __asm__(".weak environ;environ = __environ");
  68. __asm__(".weak __init_stdio; __init_stdio = __uClibc_empty_func");
  69. __asm__(".weak __stdio_close_all; __stdio_close_all = __uClibc_empty_func");