__uClibc_main.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. //#include <errno.h>
  16. #undef errno
  17. #define __set_errno(val) (*__errno_location ()) = (val)
  18. /*
  19. * Prototypes.
  20. */
  21. extern int main(int argc, char **argv, char **envp);
  22. void __uClibc_main(int argc, char **argv, char **envp)
  23. __attribute__ ((__noreturn__));
  24. #ifdef HAVE_ELF
  25. weak_alias(__environ, environ);
  26. extern void weak_function __init_stdio(void);
  27. extern void weak_function __stdio_flush_buffers(void);
  28. extern int *weak_function __errno_location (void);
  29. #else
  30. extern void __init_stdio(void);
  31. extern void __stdio_flush_buffers(void);
  32. extern int *__errno_location (void);
  33. #endif
  34. /*
  35. * Declare the __environ global variable and create a weak alias environ.
  36. * Note: Apparently we must initialize __environ for the weak environ
  37. * symbol to be included.
  38. */
  39. char **__environ = 0;
  40. /*
  41. * Now for our main routine.
  42. */
  43. void __uClibc_main(int argc, char **argv, char **envp)
  44. {
  45. /*
  46. * Initialize the global variable __environ.
  47. */
  48. __environ = envp;
  49. #if 0
  50. /* Some security at this point. Prevent starting a SUID binary
  51. * where the standard file descriptors are not opened. We have
  52. * to do this only for statically linked applications since
  53. * otherwise the dynamic loader did the work already. */
  54. if (__builtin_expect (__libc_enable_secure, 0))
  55. __libc_check_standard_fds ();
  56. #endif
  57. /*
  58. * Initialize stdio here. In the static library case, this will
  59. * be bypassed if not needed because of the weak alias above.
  60. */
  61. if (__init_stdio)
  62. __init_stdio();
  63. /*
  64. * Note: It is possible that any initialization done above could
  65. * have resulted in errno being set nonzero, so set it to 0 before
  66. * we call main.
  67. */
  68. __set_errno(0);
  69. /*
  70. * Finally, invoke application's main and then exit.
  71. */
  72. exit(main(argc, argv, envp));
  73. }
  74. #ifndef HAVE_ELF
  75. /*
  76. * Define an empty function and use it as a weak alias for the stdio
  77. * initialization routine. That way we don't pull in all the stdio
  78. * code unless we need to. Similarly, do the same for __stdio_flush_buffers
  79. * so as not to include atexit unnecessarily.
  80. *
  81. * NOTE!!! This is only true for the _static_ case!!!
  82. */
  83. weak_alias(__environ, environ);
  84. #if 0
  85. void __uClibc_empty_func(void)
  86. {
  87. }
  88. weak_alias(__uClibc_empty_func, __init_stdio);
  89. weak_alias(__uClibc_empty_func, __stdio_flush_buffers);
  90. #endif
  91. #endif