__uClibc_main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * Copyright (C) Feb 2001 Manuel Novoa III
  3. * Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
  4. *
  5. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  6. *
  7. * __uClibc_main is the routine to be called by all the arch-specific
  8. * versions of crt1.S in uClibc.
  9. *
  10. * It is meant to handle any special initialization needed by the library
  11. * such as setting the global variable(s) __environ (environ) and
  12. * initializing the stdio package. Using weak symbols, the latter is
  13. * avoided in the static library case.
  14. */
  15. #define _ERRNO_H
  16. #include <features.h>
  17. #include <unistd.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <elf.h>
  21. #include <link.h>
  22. #include <bits/uClibc_page.h>
  23. #include <paths.h>
  24. #include <unistd.h>
  25. #include <asm/errno.h>
  26. #include <fcntl.h>
  27. #include <sys/stat.h>
  28. #include <sys/sysmacros.h>
  29. libc_hidden_proto(exit)
  30. #ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
  31. libc_hidden_proto(strrchr)
  32. #endif
  33. #ifdef __ARCH_USE_MMU__
  34. libc_hidden_proto(memcpy)
  35. libc_hidden_proto(getgid)
  36. libc_hidden_proto(getuid)
  37. libc_hidden_proto(getegid)
  38. libc_hidden_proto(geteuid)
  39. libc_hidden_proto(fstat)
  40. libc_hidden_proto(abort)
  41. extern __typeof(open) __libc_open;
  42. libc_hidden_proto(__libc_open)
  43. extern __typeof(fcntl) __libc_fcntl;
  44. libc_hidden_proto(__libc_fcntl)
  45. #endif
  46. #ifndef SHARED
  47. void *__libc_stack_end=NULL;
  48. # ifdef __UCLIBC_HAS_SSP__
  49. # include <dl-osinfo.h>
  50. # ifndef THREAD_SET_STACK_GUARD
  51. /* Only exported for architectures that don't store the stack guard canary
  52. * in thread local area. */
  53. # include <stdint.h>
  54. uintptr_t stack_chk_guard;
  55. /* for gcc-4.1 non-TLS */
  56. uintptr_t __stack_chk_guard attribute_relro;
  57. /* for gcc-3.x + Etoh ssp */
  58. # ifdef __UCLIBC_HAS_SSP_COMPAT__
  59. # ifdef __HAVE_SHARED__
  60. strong_alias(__stack_chk_guard,__guard)
  61. # else
  62. uintptr_t __guard attribute_relro;
  63. # endif
  64. # endif
  65. # elif defined __UCLIBC_HAS_SSP_COMPAT__
  66. uintptr_t __guard attribute_relro;
  67. # endif
  68. # endif
  69. #endif /* !SHARED */
  70. /*
  71. * Prototypes.
  72. */
  73. extern void weak_function _stdio_init(void) attribute_hidden;
  74. extern int *weak_const_function __errno_location(void);
  75. extern int *weak_const_function __h_errno_location(void);
  76. #ifdef __UCLIBC_HAS_LOCALE__
  77. extern void weak_function _locale_init(void) attribute_hidden;
  78. #endif
  79. #ifdef __UCLIBC_HAS_THREADS__
  80. extern void weak_function __pthread_initialize_minimal(void);
  81. #endif
  82. #ifdef __UCLIBC_CTOR_DTOR__
  83. extern void _dl_app_init_array(void);
  84. extern void _dl_app_fini_array(void);
  85. # ifndef SHARED
  86. /* These magic symbols are provided by the linker. */
  87. extern void (*__preinit_array_start []) (void) attribute_hidden;
  88. extern void (*__preinit_array_end []) (void) attribute_hidden;
  89. extern void (*__init_array_start []) (void) attribute_hidden;
  90. extern void (*__init_array_end []) (void) attribute_hidden;
  91. extern void (*__fini_array_start []) (void) attribute_hidden;
  92. extern void (*__fini_array_end []) (void) attribute_hidden;
  93. # endif
  94. #endif
  95. attribute_hidden const char *__uclibc_progname = NULL;
  96. #ifdef __UCLIBC_HAS___PROGNAME__
  97. strong_alias (__uclibc_progname, __progname)
  98. #endif
  99. #ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
  100. attribute_hidden const char *__progname_full = NULL;
  101. strong_alias (__uclibc_progname, program_invocation_short_name)
  102. strong_alias (__progname_full, program_invocation_name)
  103. #endif
  104. /*
  105. * Declare the __environ global variable and create a weak alias environ.
  106. * Note: Apparently we must initialize __environ to ensure that the weak
  107. * environ symbol is also included.
  108. */
  109. libc_hidden_proto(__environ)
  110. char **__environ = 0;
  111. libc_hidden_data_def(__environ)
  112. #ifdef __USE_GNU
  113. /* psm: arm segfaults with strong_alias, although defined */
  114. weak_alias(__environ,environ)
  115. #endif
  116. /* TODO: don't export __pagesize; we cant now because libpthread uses it */
  117. extern size_t __pagesize;
  118. libc_hidden_proto(__pagesize)
  119. size_t __pagesize = 0;
  120. libc_hidden_data_def(__pagesize)
  121. #ifndef O_NOFOLLOW
  122. # define O_NOFOLLOW 0
  123. #endif
  124. #ifdef __ARCH_USE_MMU__
  125. static void __check_one_fd(int fd, int mode)
  126. {
  127. /* Check if the specified fd is already open */
  128. if (unlikely(__libc_fcntl(fd, F_GETFD)==-1 && *(__errno_location())==EBADF))
  129. {
  130. /* The descriptor is probably not open, so try to use /dev/null */
  131. struct stat st;
  132. int nullfd = __libc_open(_PATH_DEVNULL, mode);
  133. /* /dev/null is major=1 minor=3. Make absolutely certain
  134. * that is in fact the device that we have opened and not
  135. * some other wierd file... */
  136. if ( (nullfd!=fd) || fstat(fd, &st) || !S_ISCHR(st.st_mode) ||
  137. (st.st_rdev != makedev(1, 3)))
  138. {
  139. abort();
  140. }
  141. }
  142. }
  143. static int __check_suid(void)
  144. {
  145. uid_t uid, euid;
  146. gid_t gid, egid;
  147. uid = getuid();
  148. euid = geteuid();
  149. gid = getgid();
  150. egid = getegid();
  151. if(uid == euid && gid == egid) {
  152. return 0;
  153. }
  154. return 1;
  155. }
  156. #endif
  157. /* __uClibc_init completely initialize uClibc so it is ready to use.
  158. *
  159. * On ELF systems (with a dynamic loader) this function must be called
  160. * from the dynamic loader (see TIS and ELF Specification), so that
  161. * constructors of shared libraries (which depend on libc) can use all
  162. * the libc code without restriction. For this we link the shared
  163. * version of the uClibc with -init __uClibc_init so DT_INIT for
  164. * uClibc is the address of __uClibc_init
  165. *
  166. * In all other cases we call it from the main stub
  167. * __uClibc_main.
  168. */
  169. extern void __uClibc_init(void);
  170. libc_hidden_proto(__uClibc_init)
  171. void __uClibc_init(void)
  172. {
  173. static int been_there_done_that = 0;
  174. if (been_there_done_that)
  175. return;
  176. been_there_done_that++;
  177. /* Setup an initial value. This may not be perfect, but is
  178. * better than malloc using __pagesize=0 for atexit, ctors, etc. */
  179. __pagesize = PAGE_SIZE;
  180. #ifdef __UCLIBC_HAS_THREADS__
  181. /* Before we start initializing uClibc we have to call
  182. * __pthread_initialize_minimal so we can use pthread_locks
  183. * whenever they are needed.
  184. */
  185. if (likely(__pthread_initialize_minimal!=NULL))
  186. __pthread_initialize_minimal();
  187. #endif
  188. #ifndef SHARED
  189. # ifdef __UCLIBC_HAS_SSP__
  190. /* Set up the stack checker's canary. */
  191. stack_chk_guard = _dl_setup_stack_chk_guard();
  192. # ifdef THREAD_SET_STACK_GUARD
  193. THREAD_SET_STACK_GUARD (stack_chk_guard);
  194. # ifdef __UCLIBC_HAS_SSP_COMPAT__
  195. __guard = stack_chk_guard;
  196. # endif
  197. # else
  198. __stack_chk_guard = stack_chk_guard;
  199. # if !defined __HAVE_SHARED__ && defined __UCLIBC_HAS_SSP_COMPAT__
  200. __guard = stack_chk_guard;
  201. # endif
  202. # endif
  203. # endif
  204. #endif
  205. #ifdef __UCLIBC_HAS_LOCALE__
  206. /* Initialize the global locale structure. */
  207. if (likely(_locale_init!=NULL))
  208. _locale_init();
  209. #endif
  210. /*
  211. * Initialize stdio here. In the static library case, this will
  212. * be bypassed if not needed because of the weak alias above.
  213. */
  214. if (likely(_stdio_init != NULL))
  215. _stdio_init();
  216. }
  217. libc_hidden_def(__uClibc_init)
  218. #ifdef __UCLIBC_CTOR_DTOR__
  219. void attribute_hidden (*__app_fini)(void) = NULL;
  220. #endif
  221. void attribute_hidden (*__rtld_fini)(void) = NULL;
  222. extern void __uClibc_fini(void);
  223. libc_hidden_proto(__uClibc_fini)
  224. void __uClibc_fini(void)
  225. {
  226. #ifdef __UCLIBC_CTOR_DTOR__
  227. # ifdef SHARED
  228. _dl_app_fini_array();
  229. # else
  230. size_t i = __fini_array_end - __fini_array_start;
  231. while (i-- > 0)
  232. (*__fini_array_start [i]) ();
  233. # endif
  234. if (__app_fini != NULL)
  235. (__app_fini)();
  236. #endif
  237. if (__rtld_fini != NULL)
  238. (__rtld_fini)();
  239. }
  240. libc_hidden_def(__uClibc_fini)
  241. /* __uClibc_main is the new main stub for uClibc. This function is
  242. * called from crt1 (version 0.9.28 or newer), after ALL shared libraries
  243. * are initialized, just before we call the application's main function.
  244. */
  245. void __uClibc_main(int (*main)(int, char **, char **), int argc,
  246. char **argv, void (*app_init)(void), void (*app_fini)(void),
  247. void (*rtld_fini)(void), void *stack_end) attribute_noreturn;
  248. void __uClibc_main(int (*main)(int, char **, char **), int argc,
  249. char **argv, void (*app_init)(void), void (*app_fini)(void),
  250. void (*rtld_fini)(void), void *stack_end)
  251. {
  252. #ifdef __ARCH_USE_MMU__
  253. unsigned long *aux_dat;
  254. ElfW(auxv_t) auxvt[AT_EGID + 1];
  255. #endif
  256. #ifndef SHARED
  257. __libc_stack_end = stack_end;
  258. #endif
  259. __rtld_fini = rtld_fini;
  260. /* The environment begins right after argv. */
  261. __environ = &argv[argc + 1];
  262. /* If the first thing after argv is the arguments
  263. * the the environment is empty. */
  264. if ((char *) __environ == *argv) {
  265. /* Make __environ point to the NULL at argv[argc] */
  266. __environ = &argv[argc];
  267. }
  268. #ifdef __ARCH_USE_MMU__
  269. /* Pull stuff from the ELF header when possible */
  270. aux_dat = (unsigned long*)__environ;
  271. while (*aux_dat) {
  272. aux_dat++;
  273. }
  274. aux_dat++;
  275. while (*aux_dat) {
  276. ElfW(auxv_t) *auxv_entry = (ElfW(auxv_t) *) aux_dat;
  277. if (auxv_entry->a_type <= AT_EGID) {
  278. memcpy(&(auxvt[auxv_entry->a_type]), auxv_entry, sizeof(ElfW(auxv_t)));
  279. }
  280. aux_dat += 2;
  281. }
  282. #endif
  283. /* We need to initialize uClibc. If we are dynamically linked this
  284. * may have already been completed by the shared lib loader. We call
  285. * __uClibc_init() regardless, to be sure the right thing happens. */
  286. __uClibc_init();
  287. #ifdef __ARCH_USE_MMU__
  288. /* Make certain getpagesize() gives the correct answer */
  289. __pagesize = (auxvt[AT_PAGESZ].a_un.a_val)? auxvt[AT_PAGESZ].a_un.a_val : PAGE_SIZE;
  290. /* Prevent starting SUID binaries where the stdin. stdout, and
  291. * stderr file descriptors are not already opened. */
  292. if ((auxvt[AT_UID].a_un.a_val == (size_t)-1 && __check_suid()) ||
  293. (auxvt[AT_UID].a_un.a_val != (size_t)-1 &&
  294. (auxvt[AT_UID].a_un.a_val != auxvt[AT_EUID].a_un.a_val ||
  295. auxvt[AT_GID].a_un.a_val != auxvt[AT_EGID].a_un.a_val)))
  296. {
  297. __check_one_fd (STDIN_FILENO, O_RDONLY | O_NOFOLLOW);
  298. __check_one_fd (STDOUT_FILENO, O_RDWR | O_NOFOLLOW);
  299. __check_one_fd (STDERR_FILENO, O_RDWR | O_NOFOLLOW);
  300. }
  301. #endif
  302. #ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
  303. __progname_full = *argv;
  304. __progname = strrchr(*argv, '/');
  305. if (__progname != NULL)
  306. ++__progname;
  307. else
  308. __progname = __progname_full;
  309. #else
  310. __uclibc_progname = *argv;
  311. #endif
  312. #ifdef __UCLIBC_CTOR_DTOR__
  313. /* Arrange for the application's dtors to run before we exit. */
  314. __app_fini = app_fini;
  315. # ifndef SHARED
  316. /* For dynamically linked executables the preinit array is executed by
  317. the dynamic linker (before initializing any shared object).
  318. For static executables, preinit happens rights before init. */
  319. {
  320. const size_t size = __preinit_array_end - __preinit_array_start;
  321. size_t i;
  322. for (i = 0; i < size; i++)
  323. (*__preinit_array_start [i]) ();
  324. }
  325. # endif
  326. /* Run all the application's ctors now. */
  327. if (app_init!=NULL) {
  328. app_init();
  329. }
  330. # ifdef SHARED
  331. _dl_app_init_array();
  332. # else
  333. {
  334. const size_t size = __init_array_end - __init_array_start;
  335. size_t i;
  336. for (i = 0; i < size; i++)
  337. (*__init_array_start [i]) ();
  338. }
  339. # endif
  340. #endif
  341. /* Note: It is possible that any initialization done above could
  342. * have resulted in errno being set nonzero, so set it to 0 before
  343. * we call main.
  344. */
  345. if (likely(__errno_location!=NULL))
  346. *(__errno_location()) = 0;
  347. /* Set h_errno to 0 as well */
  348. if (likely(__h_errno_location!=NULL))
  349. *(__h_errno_location()) = 0;
  350. /*
  351. * Finally, invoke application's main and then exit.
  352. */
  353. exit(main(argc, argv, __environ));
  354. }