__uClibc_main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * Copyright (C) Feb 2001 Manuel Novoa III
  3. * Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
  4. *
  5. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  6. *
  7. * __uClibc_main is the routine to be called by all the arch-specific
  8. * versions of crt1.S in uClibc.
  9. *
  10. * It is meant to handle any special initialization needed by the library
  11. * such as setting the global variable(s) __environ (environ) and
  12. * initializing the stdio package. Using weak symbols, the latter is
  13. * avoided in the static library case.
  14. */
  15. #define _ERRNO_H
  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 <asm/errno.h>
  25. #include <fcntl.h>
  26. #include <sys/stat.h>
  27. #include <sys/sysmacros.h>
  28. /* libc_hidden_proto(exit) */
  29. #ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
  30. /* Experimentally off - libc_hidden_proto(strrchr) */
  31. #endif
  32. #ifndef __ARCH_HAS_NO_LDSO__
  33. /* Experimentally off - libc_hidden_proto(memcpy) */
  34. /* libc_hidden_proto(getgid) */
  35. /* libc_hidden_proto(getuid) */
  36. /* libc_hidden_proto(getegid) */
  37. /* libc_hidden_proto(geteuid) */
  38. /* libc_hidden_proto(fstat) */
  39. /* libc_hidden_proto(abort) */
  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. #endif /* !SHARED */
  69. /*
  70. * Prototypes.
  71. */
  72. extern int *weak_const_function __errno_location(void);
  73. libc_hidden_proto(__errno_location)
  74. extern int *weak_const_function __h_errno_location(void);
  75. libc_hidden_proto(__h_errno_location)
  76. extern void weak_function _stdio_init(void) attribute_hidden;
  77. #ifdef __UCLIBC_HAS_LOCALE__
  78. extern void weak_function _locale_init(void) attribute_hidden;
  79. #endif
  80. #ifdef __UCLIBC_HAS_THREADS__
  81. extern void weak_function __pthread_initialize_minimal(void);
  82. #endif
  83. /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation and finalisation
  84. * is handled by the routines passed to __uClibc_main(). */
  85. #if defined (__UCLIBC_CTOR_DTOR__) && !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
  86. extern void _dl_app_init_array(void);
  87. extern void _dl_app_fini_array(void);
  88. # ifndef SHARED
  89. /* These magic symbols are provided by the linker. */
  90. extern void (*__preinit_array_start []) (void) attribute_hidden;
  91. extern void (*__preinit_array_end []) (void) attribute_hidden;
  92. extern void (*__init_array_start []) (void) attribute_hidden;
  93. extern void (*__init_array_end []) (void) attribute_hidden;
  94. extern void (*__fini_array_start []) (void) attribute_hidden;
  95. extern void (*__fini_array_end []) (void) attribute_hidden;
  96. # endif
  97. #endif
  98. attribute_hidden const char *__uclibc_progname = "";
  99. #ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
  100. const char *program_invocation_short_name = "";
  101. const char *program_invocation_name = "";
  102. #endif
  103. #ifdef __UCLIBC_HAS___PROGNAME__
  104. weak_alias (program_invocation_short_name, __progname)
  105. weak_alias (program_invocation_name, __progname_full)
  106. #endif
  107. /*
  108. * Declare the __environ global variable and create a weak alias environ.
  109. * This must be initialized; we cannot have a weak alias into bss.
  110. */
  111. char **__environ = 0;
  112. weak_alias(__environ, environ)
  113. /* TODO: don't export __pagesize; we cant now because libpthread uses it */
  114. size_t __pagesize = 0;
  115. #ifndef O_NOFOLLOW
  116. # define O_NOFOLLOW 0
  117. #endif
  118. #ifndef __ARCH_HAS_NO_LDSO__
  119. static void __check_one_fd(int fd, int mode)
  120. {
  121. /* Check if the specified fd is already open */
  122. if (fcntl(fd, F_GETFD) == -1)
  123. {
  124. /* The descriptor is probably not open, so try to use /dev/null */
  125. int nullfd = open(_PATH_DEVNULL, mode);
  126. /* /dev/null is major=1 minor=3. Make absolutely certain
  127. * that is in fact the device that we have opened and not
  128. * some other weird file... [removed in uclibc] */
  129. if (nullfd!=fd)
  130. {
  131. abort();
  132. }
  133. }
  134. }
  135. static int __check_suid(void)
  136. {
  137. uid_t uid, euid;
  138. gid_t gid, egid;
  139. uid = getuid();
  140. euid = geteuid();
  141. if (uid != euid)
  142. return 1;
  143. gid = getgid();
  144. egid = getegid();
  145. if (gid != egid)
  146. return 1;
  147. return 0; /* we are not suid */
  148. }
  149. #endif
  150. /* __uClibc_init completely initialize uClibc so it is ready to use.
  151. *
  152. * On ELF systems (with a dynamic loader) this function must be called
  153. * from the dynamic loader (see TIS and ELF Specification), so that
  154. * constructors of shared libraries (which depend on libc) can use all
  155. * the libc code without restriction. For this we link the shared
  156. * version of the uClibc with -init __uClibc_init so DT_INIT for
  157. * uClibc is the address of __uClibc_init
  158. *
  159. * In all other cases we call it from the main stub
  160. * __uClibc_main.
  161. */
  162. extern void __uClibc_init(void);
  163. libc_hidden_proto(__uClibc_init)
  164. void __uClibc_init(void)
  165. {
  166. static smallint been_there_done_that;
  167. if (been_there_done_that)
  168. return;
  169. been_there_done_that++;
  170. /* Setup an initial value. This may not be perfect, but is
  171. * better than malloc using __pagesize=0 for atexit, ctors, etc. */
  172. __pagesize = PAGE_SIZE;
  173. #ifdef __UCLIBC_HAS_THREADS__
  174. /* Before we start initializing uClibc we have to call
  175. * __pthread_initialize_minimal so we can use pthread_locks
  176. * whenever they are needed.
  177. */
  178. if (likely(__pthread_initialize_minimal!=NULL))
  179. __pthread_initialize_minimal();
  180. #endif
  181. #ifndef SHARED
  182. # ifdef __UCLIBC_HAS_SSP__
  183. /* Set up the stack checker's canary. */
  184. stack_chk_guard = _dl_setup_stack_chk_guard();
  185. # ifdef THREAD_SET_STACK_GUARD
  186. THREAD_SET_STACK_GUARD (stack_chk_guard);
  187. # ifdef __UCLIBC_HAS_SSP_COMPAT__
  188. __guard = stack_chk_guard;
  189. # endif
  190. # else
  191. __stack_chk_guard = stack_chk_guard;
  192. # if !defined __HAVE_SHARED__ && defined __UCLIBC_HAS_SSP_COMPAT__
  193. __guard = stack_chk_guard;
  194. # endif
  195. # endif
  196. # endif
  197. #endif
  198. #ifdef __UCLIBC_HAS_LOCALE__
  199. /* Initialize the global locale structure. */
  200. if (likely(_locale_init!=NULL))
  201. _locale_init();
  202. #endif
  203. /*
  204. * Initialize stdio here. In the static library case, this will
  205. * be bypassed if not needed because of the weak alias above.
  206. * Thus we get a nice size savings because the stdio functions
  207. * won't be pulled into the final static binary unless used.
  208. */
  209. if (likely(_stdio_init != NULL))
  210. _stdio_init();
  211. }
  212. libc_hidden_def(__uClibc_init)
  213. #ifdef __UCLIBC_CTOR_DTOR__
  214. void attribute_hidden (*__app_fini)(void) = NULL;
  215. #endif
  216. void attribute_hidden (*__rtld_fini)(void) = NULL;
  217. extern void __uClibc_fini(void);
  218. libc_hidden_proto(__uClibc_fini)
  219. void __uClibc_fini(void)
  220. {
  221. #ifdef __UCLIBC_CTOR_DTOR__
  222. /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array finalisation is handled
  223. * by __app_fini. */
  224. # ifdef SHARED
  225. _dl_app_fini_array();
  226. # elif !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
  227. size_t i = __fini_array_end - __fini_array_start;
  228. while (i-- > 0)
  229. (*__fini_array_start [i]) ();
  230. # endif
  231. if (__app_fini != NULL)
  232. (__app_fini)();
  233. #endif
  234. if (__rtld_fini != NULL)
  235. (__rtld_fini)();
  236. }
  237. libc_hidden_def(__uClibc_fini)
  238. /* __uClibc_main is the new main stub for uClibc. This function is
  239. * called from crt1 (version 0.9.28 or newer), after ALL shared libraries
  240. * are initialized, just before we call the application's main function.
  241. */
  242. void __uClibc_main(int (*main)(int, char **, char **), int argc,
  243. char **argv, void (*app_init)(void), void (*app_fini)(void),
  244. void (*rtld_fini)(void), void *stack_end) attribute_noreturn;
  245. void __uClibc_main(int (*main)(int, char **, char **), int argc,
  246. char **argv, void (*app_init)(void), void (*app_fini)(void),
  247. void (*rtld_fini)(void), void *stack_end)
  248. {
  249. #ifndef __ARCH_HAS_NO_LDSO__
  250. unsigned long *aux_dat;
  251. ElfW(auxv_t) auxvt[AT_EGID + 1];
  252. #endif
  253. #ifndef SHARED
  254. __libc_stack_end = stack_end;
  255. #endif
  256. __rtld_fini = rtld_fini;
  257. /* The environment begins right after argv. */
  258. __environ = &argv[argc + 1];
  259. /* If the first thing after argv is the arguments
  260. * the the environment is empty. */
  261. if ((char *) __environ == *argv) {
  262. /* Make __environ point to the NULL at argv[argc] */
  263. __environ = &argv[argc];
  264. }
  265. #ifndef __ARCH_HAS_NO_LDSO__
  266. /* Pull stuff from the ELF header when possible */
  267. memset(auxvt, 0x00, sizeof(auxvt));
  268. aux_dat = (unsigned long*)__environ;
  269. while (*aux_dat) {
  270. aux_dat++;
  271. }
  272. aux_dat++;
  273. while (*aux_dat) {
  274. ElfW(auxv_t) *auxv_entry = (ElfW(auxv_t) *) aux_dat;
  275. if (auxv_entry->a_type <= AT_EGID) {
  276. memcpy(&(auxvt[auxv_entry->a_type]), auxv_entry, sizeof(ElfW(auxv_t)));
  277. }
  278. aux_dat += 2;
  279. }
  280. #ifndef SHARED
  281. /* Get the program headers (_dl_phdr) from the aux vector
  282. It will be used into __libc_setup_tls. */
  283. _dl_aux_init (auxvt);
  284. #endif
  285. #endif
  286. /* We need to initialize uClibc. If we are dynamically linked this
  287. * may have already been completed by the shared lib loader. We call
  288. * __uClibc_init() regardless, to be sure the right thing happens. */
  289. __uClibc_init();
  290. #ifndef __ARCH_HAS_NO_LDSO__
  291. /* Make certain getpagesize() gives the correct answer */
  292. __pagesize = (auxvt[AT_PAGESZ].a_un.a_val)? auxvt[AT_PAGESZ].a_un.a_val : PAGE_SIZE;
  293. /* Prevent starting SUID binaries where the stdin. stdout, and
  294. * stderr file descriptors are not already opened. */
  295. if ((auxvt[AT_UID].a_un.a_val == (size_t)-1 && __check_suid()) ||
  296. (auxvt[AT_UID].a_un.a_val != (size_t)-1 &&
  297. (auxvt[AT_UID].a_un.a_val != auxvt[AT_EUID].a_un.a_val ||
  298. auxvt[AT_GID].a_un.a_val != auxvt[AT_EGID].a_un.a_val)))
  299. {
  300. __check_one_fd (STDIN_FILENO, O_RDONLY | O_NOFOLLOW);
  301. __check_one_fd (STDOUT_FILENO, O_RDWR | O_NOFOLLOW);
  302. __check_one_fd (STDERR_FILENO, O_RDWR | O_NOFOLLOW);
  303. }
  304. #endif
  305. __uclibc_progname = *argv;
  306. #ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
  307. if (*argv != NULL) {
  308. program_invocation_name = *argv;
  309. program_invocation_short_name = strrchr(*argv, '/');
  310. if (program_invocation_short_name != NULL)
  311. ++program_invocation_short_name;
  312. else
  313. program_invocation_short_name = program_invocation_name;
  314. }
  315. #endif
  316. #ifdef __UCLIBC_CTOR_DTOR__
  317. /* Arrange for the application's dtors to run before we exit. */
  318. __app_fini = app_fini;
  319. /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation is handled
  320. * by __app_init. */
  321. # if !defined (SHARED) && !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
  322. /* For dynamically linked executables the preinit array is executed by
  323. the dynamic linker (before initializing any shared object).
  324. For static executables, preinit happens rights before init. */
  325. {
  326. const size_t size = __preinit_array_end - __preinit_array_start;
  327. size_t i;
  328. for (i = 0; i < size; i++)
  329. (*__preinit_array_start [i]) ();
  330. }
  331. # endif
  332. /* Run all the application's ctors now. */
  333. if (app_init!=NULL) {
  334. app_init();
  335. }
  336. /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation is handled
  337. * by __app_init. */
  338. # ifdef SHARED
  339. _dl_app_init_array();
  340. # elif !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
  341. {
  342. const size_t size = __init_array_end - __init_array_start;
  343. size_t i;
  344. for (i = 0; i < size; i++)
  345. (*__init_array_start [i]) ();
  346. }
  347. # endif
  348. #endif
  349. /* Note: It is possible that any initialization done above could
  350. * have resulted in errno being set nonzero, so set it to 0 before
  351. * we call main.
  352. */
  353. if (likely(__errno_location!=NULL))
  354. *(__errno_location()) = 0;
  355. /* Set h_errno to 0 as well */
  356. if (likely(__h_errno_location!=NULL))
  357. *(__h_errno_location()) = 0;
  358. /*
  359. * Finally, invoke application's main and then exit.
  360. */
  361. exit(main(argc, argv, __environ));
  362. }
  363. #if defined(__UCLIBC_HAS_THREADS__) && !defined(SHARED)
  364. /* Weaks for internal library use only.
  365. *
  366. * We need to define weaks here to cover all the pthread functions that
  367. * libc itself will use so that we aren't forced to link libc against
  368. * libpthread. This file is only used in libc.a and since we have
  369. * weaks here, they will be automatically overridden by libpthread.a
  370. * if it gets linked in.
  371. */
  372. static int __pthread_return_0 (void) { return 0; }
  373. static void __pthread_return_void (void) { return; }
  374. weak_alias (__pthread_return_0, __pthread_mutex_init)
  375. weak_alias (__pthread_return_0, __pthread_mutex_lock)
  376. weak_alias (__pthread_return_0, __pthread_mutex_trylock)
  377. weak_alias (__pthread_return_0, __pthread_mutex_unlock)
  378. weak_alias (__pthread_return_void, _pthread_cleanup_push_defer)
  379. weak_alias (__pthread_return_void, _pthread_cleanup_pop_restore)
  380. # ifdef __UCLIBC_HAS_THREADS_NATIVE__
  381. weak_alias (__pthread_return_0, __pthread_mutexattr_init)
  382. weak_alias (__pthread_return_0, __pthread_mutexattr_destroy)
  383. weak_alias (__pthread_return_0, __pthread_mutexattr_settype)
  384. # endif
  385. #endif