__uClibc_main.c 15 KB

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