__uClibc_main.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #define _ERRNO_H
  13. #include <stdlib.h>
  14. #include <unistd.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_flush_buffers(void);
  25. extern int *weak_function __errno_location (void);
  26. #else
  27. extern void __init_stdio(void);
  28. extern void __stdio_flush_buffers(void);
  29. extern int *__errno_location (void);
  30. #endif
  31. /*
  32. * Declare the __environ global variable and create a weak alias environ.
  33. * Note: Apparently we must initialize __environ for the weak environ
  34. * symbol to be included.
  35. */
  36. char **__environ = 0;
  37. /*
  38. * Now for our main routine.
  39. */
  40. void __uClibc_main(int argc, char **argv, char **envp)
  41. {
  42. /*
  43. * Initialize the global variable __environ.
  44. */
  45. __environ = envp;
  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. if (__errno_location)
  66. *(__errno_location()) = 0;
  67. /*
  68. * Finally, invoke application's main and then exit.
  69. */
  70. exit(main(argc, argv, envp));
  71. }
  72. #ifndef HAVE_ELF
  73. /*
  74. * Define an empty function and use it as a weak alias for the stdio
  75. * initialization routine. That way we don't pull in all the stdio
  76. * code unless we need to. Similarly, do the same for __stdio_flush_buffers
  77. * so as not to include atexit unnecessarily.
  78. *
  79. * NOTE!!! This is only true for the _static_ case!!!
  80. */
  81. weak_alias(__environ, environ);
  82. #if 0
  83. void __uClibc_empty_func(void)
  84. {
  85. }
  86. weak_alias(__uClibc_empty_func, __init_stdio);
  87. weak_alias(__uClibc_empty_func, __stdio_flush_buffers);
  88. #endif
  89. #endif