__uClibc_main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 <asm/errno.h>
  25. #include <fcntl.h>
  26. #include <sys/stat.h>
  27. #include <sys/sysmacros.h>
  28. /* libc_hidden_proto(exit) */
  29. #ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
  30. /* Experimentally off - libc_hidden_proto(strrchr) */
  31. #endif
  32. #ifndef __ARCH_HAS_NO_LDSO__
  33. /* Experimentally off - libc_hidden_proto(memcpy) */
  34. /* libc_hidden_proto(getgid) */
  35. /* libc_hidden_proto(getuid) */
  36. /* libc_hidden_proto(getegid) */
  37. /* libc_hidden_proto(geteuid) */
  38. /* libc_hidden_proto(fstat) */
  39. /* libc_hidden_proto(abort) */
  40. extern __typeof(open) __libc_open;
  41. libc_hidden_proto(__libc_open)
  42. extern __typeof(fcntl) __libc_fcntl;
  43. libc_hidden_proto(__libc_fcntl)
  44. #endif
  45. #ifndef SHARED
  46. void *__libc_stack_end=NULL;
  47. # ifdef __UCLIBC_HAS_SSP__
  48. # include <dl-osinfo.h>
  49. # ifndef THREAD_SET_STACK_GUARD
  50. /* Only exported for architectures that don't store the stack guard canary
  51. * in thread local area. */
  52. # include <stdint.h>
  53. uintptr_t stack_chk_guard;
  54. /* for gcc-4.1 non-TLS */
  55. uintptr_t __stack_chk_guard attribute_relro;
  56. /* for gcc-3.x + Etoh ssp */
  57. # ifdef __UCLIBC_HAS_SSP_COMPAT__
  58. # ifdef __HAVE_SHARED__
  59. strong_alias(__stack_chk_guard,__guard)
  60. # else
  61. uintptr_t __guard attribute_relro;
  62. # endif
  63. # endif
  64. # elif defined __UCLIBC_HAS_SSP_COMPAT__
  65. uintptr_t __guard attribute_relro;
  66. # endif
  67. # endif
  68. /*
  69. * Needed to initialize _dl_phdr when statically linked
  70. */
  71. void internal_function _dl_aux_init (ElfW(auxv_t) *av);
  72. #endif /* !SHARED */
  73. /*
  74. * Prototypes.
  75. */
  76. extern int *weak_const_function __errno_location(void);
  77. libc_hidden_proto(__errno_location)
  78. extern int *weak_const_function __h_errno_location(void);
  79. libc_hidden_proto(__h_errno_location)
  80. extern void weak_function _stdio_init(void) attribute_hidden;
  81. #ifdef __UCLIBC_HAS_LOCALE__
  82. extern void weak_function _locale_init(void) attribute_hidden;
  83. #endif
  84. #ifdef __UCLIBC_HAS_THREADS__
  85. extern void weak_function __pthread_initialize_minimal(void);
  86. #endif
  87. /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation and finalisation
  88. * is handled by the routines passed to __uClibc_main(). */
  89. #if defined (__UCLIBC_CTOR_DTOR__) && !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
  90. extern void _dl_app_init_array(void);
  91. extern void _dl_app_fini_array(void);
  92. # ifndef SHARED
  93. /* These magic symbols are provided by the linker. */
  94. extern void (*__preinit_array_start []) (void) attribute_hidden;
  95. extern void (*__preinit_array_end []) (void) attribute_hidden;
  96. extern void (*__init_array_start []) (void) attribute_hidden;
  97. extern void (*__init_array_end []) (void) attribute_hidden;
  98. extern void (*__fini_array_start []) (void) attribute_hidden;
  99. extern void (*__fini_array_end []) (void) attribute_hidden;
  100. # endif
  101. #endif
  102. attribute_hidden const char *__uclibc_progname = "";
  103. #ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
  104. const char *program_invocation_short_name = "";
  105. const char *program_invocation_name = "";
  106. #endif
  107. #ifdef __UCLIBC_HAS___PROGNAME__
  108. weak_alias (program_invocation_short_name, __progname)
  109. weak_alias (program_invocation_name, __progname_full)
  110. #endif
  111. /*
  112. * Declare the __environ global variable and create a weak alias environ.
  113. * This must be initialized; we cannot have a weak alias into bss.
  114. */
  115. char **__environ = 0;
  116. weak_alias(__environ, environ)
  117. /* TODO: don't export __pagesize; we cant now because libpthread uses it */
  118. size_t __pagesize = 0;
  119. #ifndef O_NOFOLLOW
  120. # define O_NOFOLLOW 0
  121. #endif
  122. #ifndef __ARCH_HAS_NO_LDSO__
  123. static void __check_one_fd(int fd, int mode)
  124. {
  125. /* Check if the specified fd is already open */
  126. if (__libc_fcntl(fd, F_GETFD) == -1)
  127. {
  128. /* The descriptor is probably not open, so try to use /dev/null */
  129. int nullfd = __libc_open(_PATH_DEVNULL, mode);
  130. /* /dev/null is major=1 minor=3. Make absolutely certain
  131. * that is in fact the device that we have opened and not
  132. * some other wierd file... */
  133. if (nullfd!=fd)
  134. {
  135. abort();
  136. }
  137. }
  138. }
  139. static int __check_suid(void)
  140. {
  141. uid_t uid, euid;
  142. gid_t gid, egid;
  143. uid = getuid();
  144. euid = geteuid();
  145. if (uid != euid)
  146. return 1;
  147. gid = getgid();
  148. egid = getegid();
  149. if (gid != egid)
  150. return 1;
  151. return 0; /* we are not suid */
  152. }
  153. #endif
  154. /* __uClibc_init completely initialize uClibc so it is ready to use.
  155. *
  156. * On ELF systems (with a dynamic loader) this function must be called
  157. * from the dynamic loader (see TIS and ELF Specification), so that
  158. * constructors of shared libraries (which depend on libc) can use all
  159. * the libc code without restriction. For this we link the shared
  160. * version of the uClibc with -init __uClibc_init so DT_INIT for
  161. * uClibc is the address of __uClibc_init
  162. *
  163. * In all other cases we call it from the main stub
  164. * __uClibc_main.
  165. */
  166. extern void __uClibc_init(void);
  167. libc_hidden_proto(__uClibc_init)
  168. void __uClibc_init(void)
  169. {
  170. static smallint been_there_done_that;
  171. if (been_there_done_that)
  172. return;
  173. been_there_done_that++;
  174. /* Setup an initial value. This may not be perfect, but is
  175. * better than malloc using __pagesize=0 for atexit, ctors, etc. */
  176. __pagesize = PAGE_SIZE;
  177. #ifdef __UCLIBC_HAS_THREADS__
  178. /* Before we start initializing uClibc we have to call
  179. * __pthread_initialize_minimal so we can use pthread_locks
  180. * whenever they are needed.
  181. */
  182. if (likely(__pthread_initialize_minimal!=NULL))
  183. __pthread_initialize_minimal();
  184. #endif
  185. #ifndef SHARED
  186. # ifdef __UCLIBC_HAS_SSP__
  187. /* Set up the stack checker's canary. */
  188. stack_chk_guard = _dl_setup_stack_chk_guard();
  189. # ifdef THREAD_SET_STACK_GUARD
  190. THREAD_SET_STACK_GUARD (stack_chk_guard);
  191. # ifdef __UCLIBC_HAS_SSP_COMPAT__
  192. __guard = stack_chk_guard;
  193. # endif
  194. # else
  195. __stack_chk_guard = stack_chk_guard;
  196. # if !defined __HAVE_SHARED__ && defined __UCLIBC_HAS_SSP_COMPAT__
  197. __guard = stack_chk_guard;
  198. # endif
  199. # endif
  200. # endif
  201. #endif
  202. #ifdef __UCLIBC_HAS_LOCALE__
  203. /* Initialize the global locale structure. */
  204. if (likely(_locale_init!=NULL))
  205. _locale_init();
  206. #endif
  207. /*
  208. * Initialize stdio here. In the static library case, this will
  209. * be bypassed if not needed because of the weak alias above.
  210. * Thus we get a nice size savings because the stdio functions
  211. * won't be pulled into the final static binary unless used.
  212. */
  213. if (likely(_stdio_init != NULL))
  214. _stdio_init();
  215. }
  216. libc_hidden_def(__uClibc_init)
  217. #ifdef __UCLIBC_CTOR_DTOR__
  218. void attribute_hidden (*__app_fini)(void) = NULL;
  219. #endif
  220. void attribute_hidden (*__rtld_fini)(void) = NULL;
  221. extern void __uClibc_fini(void);
  222. libc_hidden_proto(__uClibc_fini)
  223. void __uClibc_fini(void)
  224. {
  225. #ifdef __UCLIBC_CTOR_DTOR__
  226. /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array finalisation is handled
  227. * by __app_fini. */
  228. # ifdef SHARED
  229. _dl_app_fini_array();
  230. # elif !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
  231. size_t i = __fini_array_end - __fini_array_start;
  232. while (i-- > 0)
  233. (*__fini_array_start [i]) ();
  234. # endif
  235. if (__app_fini != NULL)
  236. (__app_fini)();
  237. #endif
  238. if (__rtld_fini != NULL)
  239. (__rtld_fini)();
  240. }
  241. libc_hidden_def(__uClibc_fini)
  242. /* __uClibc_main is the new main stub for uClibc. This function is
  243. * called from crt1 (version 0.9.28 or newer), after ALL shared libraries
  244. * are initialized, just before we call the application's main function.
  245. */
  246. void __uClibc_main(int (*main)(int, char **, char **), int argc,
  247. char **argv, void (*app_init)(void), void (*app_fini)(void),
  248. void (*rtld_fini)(void), void *stack_end) attribute_noreturn;
  249. void __uClibc_main(int (*main)(int, char **, char **), int argc,
  250. char **argv, void (*app_init)(void), void (*app_fini)(void),
  251. void (*rtld_fini)(void), void *stack_end)
  252. {
  253. #ifndef __ARCH_HAS_NO_LDSO__
  254. unsigned long *aux_dat;
  255. ElfW(auxv_t) auxvt[AT_EGID + 1];
  256. #endif
  257. #ifndef SHARED
  258. __libc_stack_end = stack_end;
  259. #endif
  260. __rtld_fini = rtld_fini;
  261. /* The environment begins right after argv. */
  262. __environ = &argv[argc + 1];
  263. /* If the first thing after argv is the arguments
  264. * the the environment is empty. */
  265. if ((char *) __environ == *argv) {
  266. /* Make __environ point to the NULL at argv[argc] */
  267. __environ = &argv[argc];
  268. }
  269. #ifndef __ARCH_HAS_NO_LDSO__
  270. /* Pull stuff from the ELF header when possible */
  271. memset(auxvt, 0x00, sizeof(auxvt));
  272. aux_dat = (unsigned long*)__environ;
  273. while (*aux_dat) {
  274. aux_dat++;
  275. }
  276. aux_dat++;
  277. while (*aux_dat) {
  278. ElfW(auxv_t) *auxv_entry = (ElfW(auxv_t) *) aux_dat;
  279. if (auxv_entry->a_type <= AT_EGID) {
  280. memcpy(&(auxvt[auxv_entry->a_type]), auxv_entry, sizeof(ElfW(auxv_t)));
  281. }
  282. aux_dat += 2;
  283. }
  284. #ifndef SHARED
  285. /* Get the program headers (_dl_phdr) from the aux vector
  286. It will be used into __libc_setup_tls. */
  287. _dl_aux_init (auxvt);
  288. #endif
  289. #endif
  290. /* We need to initialize uClibc. If we are dynamically linked this
  291. * may have already been completed by the shared lib loader. We call
  292. * __uClibc_init() regardless, to be sure the right thing happens. */
  293. __uClibc_init();
  294. #ifndef __ARCH_HAS_NO_LDSO__
  295. /* Make certain getpagesize() gives the correct answer */
  296. __pagesize = (auxvt[AT_PAGESZ].a_un.a_val)? auxvt[AT_PAGESZ].a_un.a_val : PAGE_SIZE;
  297. /* Prevent starting SUID binaries where the stdin. stdout, and
  298. * stderr file descriptors are not already opened. */
  299. if ((auxvt[AT_UID].a_un.a_val == (size_t)-1 && __check_suid()) ||
  300. (auxvt[AT_UID].a_un.a_val != (size_t)-1 &&
  301. (auxvt[AT_UID].a_un.a_val != auxvt[AT_EUID].a_un.a_val ||
  302. auxvt[AT_GID].a_un.a_val != auxvt[AT_EGID].a_un.a_val)))
  303. {
  304. __check_one_fd (STDIN_FILENO, O_RDONLY | O_NOFOLLOW);
  305. __check_one_fd (STDOUT_FILENO, O_RDWR | O_NOFOLLOW);
  306. __check_one_fd (STDERR_FILENO, O_RDWR | O_NOFOLLOW);
  307. }
  308. #endif
  309. __uclibc_progname = *argv;
  310. #ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
  311. if (*argv != NULL) {
  312. program_invocation_name = *argv;
  313. program_invocation_short_name = strrchr(*argv, '/');
  314. if (program_invocation_short_name != NULL)
  315. ++program_invocation_short_name;
  316. else
  317. program_invocation_short_name = program_invocation_name;
  318. }
  319. #endif
  320. #ifdef __UCLIBC_CTOR_DTOR__
  321. /* Arrange for the application's dtors to run before we exit. */
  322. __app_fini = app_fini;
  323. /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation is handled
  324. * by __app_init. */
  325. # if !defined (SHARED) && !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
  326. /* For dynamically linked executables the preinit array is executed by
  327. the dynamic linker (before initializing any shared object).
  328. For static executables, preinit happens rights before init. */
  329. {
  330. const size_t size = __preinit_array_end - __preinit_array_start;
  331. size_t i;
  332. for (i = 0; i < size; i++)
  333. (*__preinit_array_start [i]) ();
  334. }
  335. # endif
  336. /* Run all the application's ctors now. */
  337. if (app_init!=NULL) {
  338. app_init();
  339. }
  340. /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation is handled
  341. * by __app_init. */
  342. # ifdef SHARED
  343. _dl_app_init_array();
  344. # elif !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
  345. {
  346. const size_t size = __init_array_end - __init_array_start;
  347. size_t i;
  348. for (i = 0; i < size; i++)
  349. (*__init_array_start [i]) ();
  350. }
  351. # endif
  352. #endif
  353. /* Note: It is possible that any initialization done above could
  354. * have resulted in errno being set nonzero, so set it to 0 before
  355. * we call main.
  356. */
  357. if (likely(__errno_location!=NULL))
  358. *(__errno_location()) = 0;
  359. /* Set h_errno to 0 as well */
  360. if (likely(__h_errno_location!=NULL))
  361. *(__h_errno_location()) = 0;
  362. /*
  363. * Finally, invoke application's main and then exit.
  364. */
  365. exit(main(argc, argv, __environ));
  366. }
  367. #if defined(__UCLIBC_HAS_THREADS__) && !defined(SHARED)
  368. /* Weaks for internal library use only.
  369. *
  370. * We need to define weaks here to cover all the pthread functions that
  371. * libc itself will use so that we aren't forced to link libc against
  372. * libpthread. This file is only used in libc.a and since we have
  373. * weaks here, they will be automatically overridden by libpthread.a
  374. * if it gets linked in.
  375. */
  376. static int __pthread_return_0 (void) { return 0; }
  377. static void __pthread_return_void (void) { return; }
  378. weak_alias (__pthread_return_0, __pthread_mutex_init)
  379. weak_alias (__pthread_return_0, __pthread_mutex_lock)
  380. weak_alias (__pthread_return_0, __pthread_mutex_trylock)
  381. weak_alias (__pthread_return_0, __pthread_mutex_unlock)
  382. weak_alias (__pthread_return_void, _pthread_cleanup_push_defer)
  383. weak_alias (__pthread_return_void, _pthread_cleanup_pop_restore)
  384. # ifdef __UCLIBC_HAS_THREADS_NATIVE__
  385. weak_alias (__pthread_return_0, __pthread_mutexattr_init)
  386. weak_alias (__pthread_return_0, __pthread_mutexattr_destroy)
  387. weak_alias (__pthread_return_0, __pthread_mutexattr_settype)
  388. # endif
  389. #endif