__uClibc_main.c 14 KB

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