__uClibc_main.c 15 KB

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