__uClibc_main.c 15 KB

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