__uClibc_main.c 13 KB

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