__uClibc_main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * Copyright (C) 2006 by Steven J. Hill <sjhill@realitydiluted.com>
  3. * Copyright (C) 2001 by Manuel Novoa III <mjn3@uclibc.org>
  4. * Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
  5. *
  6. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  7. *
  8. * __uClibc_main is the routine to be called by all the arch-specific
  9. * versions of crt1.S in uClibc.
  10. *
  11. * It is meant to handle any special initialization needed by the library
  12. * such as setting the global variable(s) __environ (environ) and
  13. * initializing the stdio package. Using weak symbols, the latter is
  14. * avoided in the static library case.
  15. */
  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 <errno.h>
  25. #include <netdb.h>
  26. #include <stdio.h>
  27. #ifndef __ARCH_HAS_NO_LDSO__
  28. #include <fcntl.h>
  29. #endif
  30. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  31. #include <not-cancel.h>
  32. #include <atomic.h>
  33. #include <tls.h>
  34. #endif
  35. #ifdef __UCLIBC_HAS_THREADS__
  36. #include <pthread.h>
  37. #endif
  38. #ifdef __UCLIBC_HAS_LOCALE__
  39. #include <locale.h>
  40. #endif
  41. /* Are we in a secure process environment or are we dealing
  42. * with setuid stuff? If we are dynamically linked, then we
  43. * already have _dl_secure, otherwise we need to re-examine
  44. * auxvt[] below.
  45. */
  46. int _pe_secure = 0;
  47. libc_hidden_data_def(_pe_secure)
  48. #ifndef SHARED
  49. void *__libc_stack_end = NULL;
  50. # ifdef __UCLIBC_HAS_SSP__
  51. # include <dl-osinfo.h>
  52. static uintptr_t stack_chk_guard;
  53. # ifndef THREAD_SET_STACK_GUARD
  54. /* Only exported for architectures that don't store the stack guard canary
  55. * in thread local area. */
  56. /* for gcc-4.1 non-TLS */
  57. uintptr_t __stack_chk_guard attribute_relro;
  58. # endif
  59. # endif
  60. /*
  61. * Needed to initialize _dl_phdr when statically linked
  62. */
  63. void internal_function _dl_aux_init (ElfW(auxv_t) *av);
  64. #endif /* !SHARED */
  65. /* Defeat compiler optimization which assumes function addresses are never NULL */
  66. static __always_inline int not_null_ptr(const void *p)
  67. {
  68. const void *q;
  69. __asm__ (""
  70. : "=r" (q) /* output */
  71. : "0" (p) /* input */
  72. );
  73. return q != 0;
  74. }
  75. /*
  76. * Prototypes.
  77. */
  78. #ifdef __UCLIBC_HAS_THREADS__
  79. #if !defined (__UCLIBC_HAS_THREADS_NATIVE__) || defined (SHARED)
  80. extern void weak_function __pthread_initialize_minimal(void);
  81. #else
  82. extern void __pthread_initialize_minimal(void);
  83. #endif
  84. #endif
  85. #ifndef SHARED
  86. extern void __libc_setup_tls (size_t tcbsize, size_t tcbalign);
  87. #endif
  88. /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation and finalisation
  89. * is handled by the routines passed to __uClibc_main(). */
  90. #if defined (__UCLIBC_CTOR_DTOR__) && !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
  91. extern void _dl_app_init_array(void);
  92. extern void _dl_app_fini_array(void);
  93. # ifndef SHARED
  94. /* These magic symbols are provided by the linker. */
  95. extern void (*__preinit_array_start []) (void) attribute_hidden;
  96. extern void (*__preinit_array_end []) (void) attribute_hidden;
  97. extern void (*__init_array_start []) (void) attribute_hidden;
  98. extern void (*__init_array_end []) (void) attribute_hidden;
  99. extern void (*__fini_array_start []) (void) attribute_hidden;
  100. extern void (*__fini_array_end []) (void) attribute_hidden;
  101. # endif
  102. #endif
  103. #ifdef SHARED
  104. extern int _dl_secure;
  105. #endif
  106. extern size_t _dl_pagesize;
  107. const char *__uclibc_progname = "";
  108. #if !defined __UCLIBC_HAS___PROGNAME__ && defined __USE_GNU && defined __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
  109. # define __progname program_invocation_short_name
  110. # define __progname_full program_invocation_name
  111. #endif
  112. #if defined __UCLIBC_HAS___PROGNAME__ || (defined __USE_GNU && defined __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__)
  113. const char *__progname = "";
  114. /* psm: why have a visible __progname_full? */
  115. const char *__progname_full = "";
  116. # if defined __UCLIBC_HAS___PROGNAME__ && defined __USE_GNU && defined __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
  117. weak_alias (__progname, program_invocation_short_name)
  118. weak_alias (__progname_full, program_invocation_name)
  119. # endif
  120. #endif
  121. /*
  122. * Declare the __environ global variable and create a weak alias environ.
  123. * This must be initialized; we cannot have a weak alias into bss.
  124. */
  125. char **__environ = 0;
  126. weak_alias(__environ, environ)
  127. size_t __pagesize = 0;
  128. #ifndef O_NOFOLLOW
  129. # define O_NOFOLLOW 0
  130. #endif
  131. #ifndef __ARCH_HAS_NO_LDSO__
  132. static void __check_one_fd(int fd, int mode)
  133. {
  134. /* Check if the specified fd is already open */
  135. if (fcntl(fd, F_GETFD) == -1)
  136. {
  137. /* The descriptor is probably not open, so try to use /dev/null */
  138. int nullfd = open(_PATH_DEVNULL, mode);
  139. /* /dev/null is major=1 minor=3. Make absolutely certain
  140. * that is in fact the device that we have opened and not
  141. * some other wierd file... [removed in uclibc] */
  142. if (nullfd!=fd)
  143. {
  144. abort();
  145. }
  146. }
  147. }
  148. #ifndef SHARED
  149. static int __check_suid(void)
  150. {
  151. uid_t uid, euid;
  152. gid_t gid, egid;
  153. uid = getuid();
  154. euid = geteuid();
  155. if (uid != euid)
  156. return 1;
  157. gid = getgid();
  158. egid = getegid();
  159. if (gid != egid)
  160. return 1;
  161. return 0; /* we are not suid */
  162. }
  163. #endif
  164. #endif
  165. /* __uClibc_init completely initialize uClibc so it is ready to use.
  166. *
  167. * On ELF systems (with a dynamic loader) this function must be called
  168. * from the dynamic loader (see TIS and ELF Specification), so that
  169. * constructors of shared libraries (which depend on libc) can use all
  170. * the libc code without restriction. For this we link the shared
  171. * version of the uClibc with -init __uClibc_init so DT_INIT for
  172. * uClibc is the address of __uClibc_init
  173. *
  174. * In all other cases we call it from the main stub
  175. * __uClibc_main.
  176. */
  177. extern void __uClibc_init(void) attribute_hidden;
  178. void __uClibc_init(void)
  179. {
  180. /* Don't recurse */
  181. if (__pagesize)
  182. return;
  183. /* Setup an initial value. This may not be perfect, but is
  184. * better than malloc using __pagesize=0 for atexit, ctors, etc. */
  185. __pagesize = PAGE_SIZE;
  186. #ifdef __UCLIBC_HAS_THREADS__
  187. #if defined (__UCLIBC_HAS_THREADS_NATIVE__) && !defined (SHARED)
  188. /* Unlike in the dynamically linked case the dynamic linker has not
  189. taken care of initializing the TLS data structures. */
  190. __libc_setup_tls (TLS_TCB_SIZE, TLS_TCB_ALIGN);
  191. #endif
  192. /* Before we start initializing uClibc we have to call
  193. * __pthread_initialize_minimal so we can use pthread_locks
  194. * whenever they are needed.
  195. */
  196. #if !defined (__UCLIBC_HAS_THREADS_NATIVE__) || defined (SHARED)
  197. if (likely(__pthread_initialize_minimal!=NULL))
  198. #endif
  199. __pthread_initialize_minimal();
  200. #endif
  201. #ifndef SHARED
  202. # ifdef __UCLIBC_HAS_SSP__
  203. /* Set up the stack checker's canary. */
  204. stack_chk_guard = _dl_setup_stack_chk_guard();
  205. # ifdef THREAD_SET_STACK_GUARD
  206. THREAD_SET_STACK_GUARD (stack_chk_guard);
  207. # else
  208. __stack_chk_guard = stack_chk_guard;
  209. # endif
  210. # endif
  211. #endif
  212. #ifdef __UCLIBC_HAS_LOCALE__
  213. /* Initialize the global locale structure. */
  214. if (likely(not_null_ptr(_locale_init)))
  215. _locale_init();
  216. #endif
  217. /*
  218. * Initialize stdio here. In the static library case, this will
  219. * be bypassed if not needed because of the weak alias above.
  220. * Thus we get a nice size savings because the stdio functions
  221. * won't be pulled into the final static binary unless used.
  222. */
  223. if (likely(not_null_ptr(_stdio_init)))
  224. _stdio_init();
  225. }
  226. #ifdef __UCLIBC_CTOR_DTOR__
  227. void attribute_hidden (*__app_fini)(void) = NULL;
  228. #endif
  229. void attribute_hidden (*__rtld_fini)(void) = NULL;
  230. extern void __uClibc_fini(void) attribute_hidden;
  231. void __uClibc_fini(void)
  232. {
  233. #ifdef __UCLIBC_CTOR_DTOR__
  234. /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array finalisation is handled
  235. * by __app_fini. */
  236. # ifdef SHARED
  237. _dl_app_fini_array();
  238. # elif !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
  239. size_t i = __fini_array_end - __fini_array_start;
  240. while (i-- > 0)
  241. (*__fini_array_start [i]) ();
  242. # endif
  243. if (__app_fini != NULL)
  244. (__app_fini)();
  245. #endif
  246. if (__rtld_fini != NULL)
  247. (__rtld_fini)();
  248. }
  249. #ifndef SHARED
  250. extern void __nptl_deallocate_tsd (void) __attribute ((weak));
  251. extern unsigned int __nptl_nthreads __attribute ((weak));
  252. #endif
  253. /* __uClibc_main is the new main stub for uClibc. This function is
  254. * called from crt1 (version 0.9.28 or newer), after ALL shared libraries
  255. * are initialized, just before we call the application's main function.
  256. */
  257. void __uClibc_main(int (*main)(int, char **, char **), int argc,
  258. char **argv, void (*app_init)(void), void (*app_fini)(void),
  259. void (*rtld_fini)(void),
  260. void *stack_end attribute_unused) attribute_noreturn;
  261. void __uClibc_main(int (*main)(int, char **, char **), int argc,
  262. char **argv, void (*app_init)(void), void (*app_fini)(void),
  263. void (*rtld_fini)(void), void *stack_end attribute_unused)
  264. {
  265. #if !defined __ARCH_HAS_NO_LDSO__ && !defined SHARED
  266. unsigned long *aux_dat;
  267. ElfW(auxv_t) auxvt[AT_EGID + 1];
  268. #endif
  269. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  270. /* Result of the 'main' function. */
  271. int result;
  272. #endif
  273. #ifndef SHARED
  274. __libc_stack_end = stack_end;
  275. #endif
  276. __rtld_fini = rtld_fini;
  277. /* The environment begins right after argv. */
  278. __environ = &argv[argc + 1];
  279. /* If the first thing after argv is the arguments
  280. * then the environment is empty. */
  281. if ((char *) __environ == *argv) {
  282. /* Make __environ point to the NULL at argv[argc] */
  283. __environ = &argv[argc];
  284. }
  285. #if !defined __ARCH_HAS_NO_LDSO__ && !defined SHARED
  286. /* Pull stuff from the ELF header when possible */
  287. memset(auxvt, 0x00, sizeof(auxvt));
  288. aux_dat = (unsigned long*)__environ;
  289. while (*aux_dat) {
  290. aux_dat++;
  291. }
  292. aux_dat++;
  293. while (*aux_dat) {
  294. ElfW(auxv_t) *auxv_entry = (ElfW(auxv_t) *) aux_dat;
  295. if (auxv_entry->a_type <= AT_EGID) {
  296. memcpy(&(auxvt[auxv_entry->a_type]), auxv_entry, sizeof(ElfW(auxv_t)));
  297. }
  298. aux_dat += 2;
  299. }
  300. /* Get the program headers (_dl_phdr) from the aux vector
  301. It will be used into __libc_setup_tls. */
  302. _dl_aux_init (auxvt);
  303. #endif
  304. /* We need to initialize uClibc. If we are dynamically linked this
  305. * may have already been completed by the shared lib loader. We call
  306. * __uClibc_init() regardless, to be sure the right thing happens. */
  307. __uClibc_init();
  308. #ifndef __ARCH_HAS_NO_LDSO__
  309. /* Make certain getpagesize() gives the correct answer.
  310. * _dl_pagesize is defined into ld.so if SHARED or into libc.a otherwise. */
  311. __pagesize = _dl_pagesize;
  312. #ifndef SHARED
  313. /* Prevent starting SUID binaries where the stdin. stdout, and
  314. * stderr file descriptors are not already opened. */
  315. if ((auxvt[AT_UID].a_un.a_val == (size_t)-1 && __check_suid()) ||
  316. (auxvt[AT_UID].a_un.a_val != (size_t)-1 &&
  317. (auxvt[AT_UID].a_un.a_val != auxvt[AT_EUID].a_un.a_val ||
  318. auxvt[AT_GID].a_un.a_val != auxvt[AT_EGID].a_un.a_val)))
  319. #else
  320. if (_dl_secure)
  321. #endif
  322. {
  323. __check_one_fd (STDIN_FILENO, O_RDONLY | O_NOFOLLOW);
  324. __check_one_fd (STDOUT_FILENO, O_RDWR | O_NOFOLLOW);
  325. __check_one_fd (STDERR_FILENO, O_RDWR | O_NOFOLLOW);
  326. _pe_secure = 1 ;
  327. }
  328. else
  329. _pe_secure = 0 ;
  330. #endif
  331. __uclibc_progname = *argv;
  332. #if defined __UCLIBC_HAS___PROGNAME__ || (defined __USE_GNU && defined __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__)
  333. if (*argv != NULL) {
  334. __progname_full = *argv;
  335. __progname = strrchr(*argv, '/');
  336. if (__progname != NULL)
  337. ++__progname;
  338. else
  339. __progname = *argv;
  340. }
  341. #endif
  342. #ifdef __UCLIBC_CTOR_DTOR__
  343. /* Arrange for the application's dtors to run before we exit. */
  344. __app_fini = app_fini;
  345. /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation is handled
  346. * by __app_init. */
  347. # if !defined (SHARED) && !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
  348. /* For dynamically linked executables the preinit array is executed by
  349. the dynamic linker (before initializing any shared object).
  350. For static executables, preinit happens rights before init. */
  351. {
  352. const size_t size = __preinit_array_end - __preinit_array_start;
  353. size_t i;
  354. for (i = 0; i < size; i++)
  355. (*__preinit_array_start [i]) ();
  356. }
  357. # endif
  358. /* Run all the application's ctors now. */
  359. if (app_init!=NULL) {
  360. app_init();
  361. }
  362. /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation is handled
  363. * by __app_init. */
  364. # ifdef SHARED
  365. _dl_app_init_array();
  366. # elif !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
  367. {
  368. const size_t size = __init_array_end - __init_array_start;
  369. size_t i;
  370. for (i = 0; i < size; i++)
  371. (*__init_array_start [i]) ();
  372. }
  373. # endif
  374. #endif
  375. /* Note: It is possible that any initialization done above could
  376. * have resulted in errno being set nonzero, so set it to 0 before
  377. * we call main.
  378. */
  379. if (likely(not_null_ptr(__errno_location)))
  380. *(__errno_location()) = 0;
  381. /* Set h_errno to 0 as well */
  382. if (likely(not_null_ptr(__h_errno_location)))
  383. *(__h_errno_location()) = 0;
  384. #if defined HAVE_CLEANUP_JMP_BUF && defined __UCLIBC_HAS_THREADS_NATIVE__
  385. /* Memory for the cancellation buffer. */
  386. struct pthread_unwind_buf unwind_buf;
  387. int not_first_call;
  388. not_first_call =
  389. setjmp ((struct __jmp_buf_tag *) unwind_buf.cancel_jmp_buf);
  390. if (__builtin_expect (! not_first_call, 1))
  391. {
  392. struct pthread *self = THREAD_SELF;
  393. /* Store old info. */
  394. unwind_buf.priv.data.prev = THREAD_GETMEM (self, cleanup_jmp_buf);
  395. unwind_buf.priv.data.cleanup = THREAD_GETMEM (self, cleanup);
  396. /* Store the new cleanup handler info. */
  397. THREAD_SETMEM (self, cleanup_jmp_buf, &unwind_buf);
  398. /* Run the program. */
  399. result = main (argc, argv, __environ);
  400. }
  401. else
  402. {
  403. /* Remove the thread-local data. */
  404. __nptl_deallocate_tsd ();
  405. /* One less thread. Decrement the counter. If it is zero we
  406. terminate the entire process. */
  407. result = 0;
  408. unsigned int *const ptr = &__nptl_nthreads;
  409. if (! atomic_decrement_and_test (ptr))
  410. /* Not much left to do but to exit the thread, not the process. */
  411. __exit_thread_inline (0);
  412. }
  413. exit (result);
  414. #else
  415. /*
  416. * Finally, invoke application's main and then exit.
  417. */
  418. exit (main (argc, argv, __environ));
  419. #endif
  420. }