libc-start.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /* Copyright (C) 1998-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <assert.h>
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <ldsodefs.h>
  19. #include <exit-thread.h>
  20. #include <libc-internal.h>
  21. #include <elf/dl-tunables.h>
  22. extern void __libc_init_first (int argc, char **argv, char **envp);
  23. #include <tls.h>
  24. #ifndef SHARED
  25. # include <dl-osinfo.h>
  26. # ifndef THREAD_SET_STACK_GUARD
  27. /* Only exported for architectures that don't store the stack guard canary
  28. in thread local area. */
  29. uintptr_t __stack_chk_guard attribute_relro;
  30. # endif
  31. # ifndef THREAD_SET_POINTER_GUARD
  32. /* Only exported for architectures that don't store the pointer guard
  33. value in thread local area. */
  34. uintptr_t __pointer_chk_guard_local
  35. attribute_relro attribute_hidden __attribute__ ((nocommon));
  36. # endif
  37. #endif
  38. #ifdef HAVE_PTR_NTHREADS
  39. /* We need atomic operations. */
  40. # include <atomic.h>
  41. #endif
  42. #ifndef SHARED
  43. # include <link.h>
  44. # include <dl-irel.h>
  45. # ifdef ELF_MACHINE_IRELA
  46. # define IREL_T ElfW(Rela)
  47. # define IPLT_START __rela_iplt_start
  48. # define IPLT_END __rela_iplt_end
  49. # define IREL elf_irela
  50. # elif defined ELF_MACHINE_IREL
  51. # define IREL_T ElfW(Rel)
  52. # define IPLT_START __rel_iplt_start
  53. # define IPLT_END __rel_iplt_end
  54. # define IREL elf_irel
  55. # endif
  56. static void
  57. apply_irel (void)
  58. {
  59. # ifdef IREL
  60. /* We use weak references for these so that we'll still work with a linker
  61. that doesn't define them. Such a linker doesn't support IFUNC at all
  62. and so uses won't work, but a statically-linked program that doesn't
  63. use any IFUNC symbols won't have a problem. */
  64. extern const IREL_T IPLT_START[] __attribute__ ((weak));
  65. extern const IREL_T IPLT_END[] __attribute__ ((weak));
  66. for (const IREL_T *ipltent = IPLT_START; ipltent < IPLT_END; ++ipltent)
  67. IREL (ipltent);
  68. # endif
  69. }
  70. #endif
  71. #ifdef LIBC_START_MAIN
  72. # ifdef LIBC_START_DISABLE_INLINE
  73. # define STATIC static
  74. # else
  75. # define STATIC static inline __attribute__ ((always_inline))
  76. # endif
  77. #else
  78. # define STATIC
  79. # define LIBC_START_MAIN __libc_start_main
  80. #endif
  81. #ifdef MAIN_AUXVEC_ARG
  82. /* main gets passed a pointer to the auxiliary. */
  83. # define MAIN_AUXVEC_DECL , void *
  84. # define MAIN_AUXVEC_PARAM , auxvec
  85. #else
  86. # define MAIN_AUXVEC_DECL
  87. # define MAIN_AUXVEC_PARAM
  88. #endif
  89. #ifndef ARCH_INIT_CPU_FEATURES
  90. # define ARCH_INIT_CPU_FEATURES()
  91. #endif
  92. #include <libc-start.h>
  93. STATIC int LIBC_START_MAIN (int (*main) (int, char **, char **
  94. MAIN_AUXVEC_DECL),
  95. int argc,
  96. char **argv,
  97. #ifdef LIBC_START_MAIN_AUXVEC_ARG
  98. ElfW(auxv_t) *auxvec,
  99. #endif
  100. __typeof (main) init,
  101. void (*fini) (void),
  102. void (*rtld_fini) (void),
  103. void *stack_end)
  104. __attribute__ ((noreturn));
  105. /* Note: the fini parameter is ignored here for shared library. It
  106. is registered with __cxa_atexit. This had the disadvantage that
  107. finalizers were called in more than one place. */
  108. STATIC int
  109. LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
  110. int argc, char **argv,
  111. #ifdef LIBC_START_MAIN_AUXVEC_ARG
  112. ElfW(auxv_t) *auxvec,
  113. #endif
  114. __typeof (main) init,
  115. void (*fini) (void),
  116. void (*rtld_fini) (void), void *stack_end)
  117. {
  118. /* Result of the 'main' function. */
  119. int result;
  120. __libc_multiple_libcs = &_dl_starting_up && !_dl_starting_up;
  121. #ifndef SHARED
  122. _dl_relocate_static_pie ();
  123. char **ev = &argv[argc + 1];
  124. __environ = ev;
  125. /* Store the lowest stack address. This is done in ld.so if this is
  126. the code for the DSO. */
  127. __libc_stack_end = stack_end;
  128. # ifdef HAVE_AUX_VECTOR
  129. /* First process the auxiliary vector since we need to find the
  130. program header to locate an eventually present PT_TLS entry. */
  131. # ifndef LIBC_START_MAIN_AUXVEC_ARG
  132. ElfW(auxv_t) *auxvec;
  133. {
  134. char **evp = ev;
  135. while (*evp++ != NULL)
  136. ;
  137. auxvec = (ElfW(auxv_t) *) evp;
  138. }
  139. # endif
  140. _dl_aux_init (auxvec);
  141. if (GL(dl_phdr) == NULL)
  142. # endif
  143. {
  144. /* Starting from binutils-2.23, the linker will define the
  145. magic symbol __ehdr_start to point to our own ELF header
  146. if it is visible in a segment that also includes the phdrs.
  147. So we can set up _dl_phdr and _dl_phnum even without any
  148. information from auxv. */
  149. extern const ElfW(Ehdr) __ehdr_start
  150. __attribute__ ((weak, visibility ("hidden")));
  151. if (&__ehdr_start != NULL)
  152. {
  153. assert (__ehdr_start.e_phentsize == sizeof *GL(dl_phdr));
  154. GL(dl_phdr) = (const void *) &__ehdr_start + __ehdr_start.e_phoff;
  155. GL(dl_phnum) = __ehdr_start.e_phnum;
  156. }
  157. }
  158. /* Initialize very early so that tunables can use it. */
  159. __libc_init_secure ();
  160. __tunables_init (__environ);
  161. ARCH_INIT_CPU_FEATURES ();
  162. /* Perform IREL{,A} relocations. */
  163. ARCH_SETUP_IREL ();
  164. /* The stack guard goes into the TCB, so initialize it early. */
  165. ARCH_SETUP_TLS ();
  166. /* In some architectures, IREL{,A} relocations happen after TLS setup in
  167. order to let IFUNC resolvers benefit from TCB information, e.g. powerpc's
  168. hwcap and platform fields available in the TCB. */
  169. ARCH_APPLY_IREL ();
  170. /* Set up the stack checker's canary. */
  171. uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random);
  172. # ifdef THREAD_SET_STACK_GUARD
  173. THREAD_SET_STACK_GUARD (stack_chk_guard);
  174. # else
  175. __stack_chk_guard = stack_chk_guard;
  176. # endif
  177. # ifdef DL_SYSDEP_OSCHECK
  178. if (!__libc_multiple_libcs)
  179. {
  180. /* This needs to run to initiliaze _dl_osversion before TLS
  181. setup might check it. */
  182. DL_SYSDEP_OSCHECK (__libc_fatal);
  183. }
  184. # endif
  185. /* Initialize libpthread if linked in. */
  186. if (__pthread_initialize_minimal != NULL)
  187. __pthread_initialize_minimal ();
  188. /* Set up the pointer guard value. */
  189. uintptr_t pointer_chk_guard = _dl_setup_pointer_guard (_dl_random,
  190. stack_chk_guard);
  191. # ifdef THREAD_SET_POINTER_GUARD
  192. THREAD_SET_POINTER_GUARD (pointer_chk_guard);
  193. # else
  194. __pointer_chk_guard_local = pointer_chk_guard;
  195. # endif
  196. #endif /* !SHARED */
  197. /* Register the destructor of the dynamic linker if there is any. */
  198. if (__glibc_likely (rtld_fini != NULL))
  199. __cxa_atexit ((void (*) (void *)) rtld_fini, NULL, NULL);
  200. #ifndef SHARED
  201. /* Call the initializer of the libc. This is only needed here if we
  202. are compiling for the static library in which case we haven't
  203. run the constructors in `_dl_start_user'. */
  204. __libc_init_first (argc, argv, __environ);
  205. /* Register the destructor of the program, if any. */
  206. if (fini)
  207. __cxa_atexit ((void (*) (void *)) fini, NULL, NULL);
  208. /* Some security at this point. Prevent starting a SUID binary where
  209. the standard file descriptors are not opened. We have to do this
  210. only for statically linked applications since otherwise the dynamic
  211. loader did the work already. */
  212. if (__builtin_expect (__libc_enable_secure, 0))
  213. __libc_check_standard_fds ();
  214. #endif
  215. /* Call the initializer of the program, if any. */
  216. #ifdef SHARED
  217. if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_IMPCALLS, 0))
  218. GLRO(dl_debug_printf) ("\ninitialize program: %s\n\n", argv[0]);
  219. #endif
  220. if (init)
  221. (*init) (argc, argv, __environ MAIN_AUXVEC_PARAM);
  222. #ifdef SHARED
  223. /* Auditing checkpoint: we have a new object. */
  224. if (__glibc_unlikely (GLRO(dl_naudit) > 0))
  225. {
  226. struct audit_ifaces *afct = GLRO(dl_audit);
  227. struct link_map *head = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
  228. for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
  229. {
  230. if (afct->preinit != NULL)
  231. afct->preinit (&head->l_audit[cnt].cookie);
  232. afct = afct->next;
  233. }
  234. }
  235. #endif
  236. #ifdef SHARED
  237. if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_IMPCALLS))
  238. GLRO(dl_debug_printf) ("\ntransferring control: %s\n\n", argv[0]);
  239. #endif
  240. #ifndef SHARED
  241. _dl_debug_initialize (0, LM_ID_BASE);
  242. #endif
  243. #ifdef HAVE_CLEANUP_JMP_BUF
  244. /* Memory for the cancellation buffer. */
  245. struct pthread_unwind_buf unwind_buf;
  246. int not_first_call;
  247. not_first_call = setjmp ((struct __jmp_buf_tag *) unwind_buf.cancel_jmp_buf);
  248. if (__glibc_likely (! not_first_call))
  249. {
  250. struct pthread *self = THREAD_SELF;
  251. /* Store old info. */
  252. unwind_buf.priv.data.prev = THREAD_GETMEM (self, cleanup_jmp_buf);
  253. unwind_buf.priv.data.cleanup = THREAD_GETMEM (self, cleanup);
  254. /* Store the new cleanup handler info. */
  255. THREAD_SETMEM (self, cleanup_jmp_buf, &unwind_buf);
  256. /* Run the program. */
  257. result = main (argc, argv, __environ MAIN_AUXVEC_PARAM);
  258. }
  259. else
  260. {
  261. /* Remove the thread-local data. */
  262. # ifdef SHARED
  263. PTHFCT_CALL (ptr__nptl_deallocate_tsd, ());
  264. # else
  265. extern void __nptl_deallocate_tsd (void) __attribute ((weak));
  266. __nptl_deallocate_tsd ();
  267. # endif
  268. /* One less thread. Decrement the counter. If it is zero we
  269. terminate the entire process. */
  270. result = 0;
  271. # ifdef SHARED
  272. unsigned int *ptr = __libc_pthread_functions.ptr_nthreads;
  273. # ifdef PTR_DEMANGLE
  274. PTR_DEMANGLE (ptr);
  275. # endif
  276. # else
  277. extern unsigned int __nptl_nthreads __attribute ((weak));
  278. unsigned int *const ptr = &__nptl_nthreads;
  279. # endif
  280. if (! atomic_decrement_and_test (ptr))
  281. /* Not much left to do but to exit the thread, not the process. */
  282. __exit_thread ();
  283. }
  284. #else
  285. /* Nothing fancy, just call the function. */
  286. result = main (argc, argv, __environ MAIN_AUXVEC_PARAM);
  287. #endif
  288. exit (result);
  289. }