__uClibc_main.c 16 KB

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