libc-start.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /* Perform initialization and invoke main.
  2. Copyright (C) 1998-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. /* Note: This code is only part of the startup code proper for
  16. statically linked binaries. For dynamically linked binaries, it
  17. resides in libc.so. */
  18. /* Mark symbols hidden in static PIE for early self relocation to work. */
  19. #if BUILD_PIE_DEFAULT
  20. # pragma GCC visibility push(hidden)
  21. #endif
  22. #include <assert.h>
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. #include <ldsodefs.h>
  27. #include <libc-diag.h>
  28. #include <libc-internal.h>
  29. #include <elf/libc-early-init.h>
  30. #include <stdbool.h>
  31. #include <elf-initfini.h>
  32. #include <shlib-compat.h>
  33. #include <elf/dl-tunables.h>
  34. extern void __libc_init_first (int argc, char **argv, char **envp);
  35. #include <tls.h>
  36. #ifndef SHARED
  37. # include <dl-osinfo.h>
  38. # ifndef THREAD_SET_STACK_GUARD
  39. /* Only exported for architectures that don't store the stack guard canary
  40. in thread local area. */
  41. uintptr_t __stack_chk_guard attribute_relro;
  42. # endif
  43. # ifndef THREAD_SET_POINTER_GUARD
  44. /* Only exported for architectures that don't store the pointer guard
  45. value in thread local area. */
  46. uintptr_t __pointer_chk_guard_local attribute_relro attribute_hidden;
  47. # endif
  48. #endif
  49. #ifndef SHARED
  50. # include <link.h>
  51. # include <dl-irel.h>
  52. # ifdef ELF_MACHINE_IRELA
  53. # define IREL_T ElfW(Rela)
  54. # define IPLT_START __rela_iplt_start
  55. # define IPLT_END __rela_iplt_end
  56. # define IREL elf_irela
  57. # elif defined ELF_MACHINE_IREL
  58. # define IREL_T ElfW(Rel)
  59. # define IPLT_START __rel_iplt_start
  60. # define IPLT_END __rel_iplt_end
  61. # define IREL elf_irel
  62. # endif
  63. static void
  64. apply_irel (void)
  65. {
  66. # ifdef IREL
  67. /* We use weak references for these so that we'll still work with a linker
  68. that doesn't define them. Such a linker doesn't support IFUNC at all
  69. and so uses won't work, but a statically-linked program that doesn't
  70. use any IFUNC symbols won't have a problem. */
  71. extern const IREL_T IPLT_START[] __attribute__ ((weak));
  72. extern const IREL_T IPLT_END[] __attribute__ ((weak));
  73. for (const IREL_T *ipltent = IPLT_START; ipltent < IPLT_END; ++ipltent)
  74. IREL (ipltent);
  75. # endif
  76. }
  77. #endif
  78. #ifdef LIBC_START_MAIN
  79. # ifdef LIBC_START_DISABLE_INLINE
  80. # define STATIC static
  81. # else
  82. # define STATIC static inline __attribute__ ((always_inline))
  83. # endif
  84. # define DO_DEFINE_LIBC_START_MAIN_VERSION 0
  85. #else
  86. # define STATIC
  87. # define LIBC_START_MAIN __libc_start_main_impl
  88. # define DO_DEFINE_LIBC_START_MAIN_VERSION 1
  89. #endif
  90. #ifdef MAIN_AUXVEC_ARG
  91. /* main gets passed a pointer to the auxiliary. */
  92. # define MAIN_AUXVEC_DECL , void *
  93. # define MAIN_AUXVEC_PARAM , auxvec
  94. #else
  95. # define MAIN_AUXVEC_DECL
  96. # define MAIN_AUXVEC_PARAM
  97. #endif
  98. #ifndef ARCH_INIT_CPU_FEATURES
  99. # define ARCH_INIT_CPU_FEATURES()
  100. #endif
  101. /* Obtain the definition of __libc_start_call_main. */
  102. #include <libc_start_call_main.h>
  103. #ifdef SHARED
  104. /* Initialization for dynamic executables. Find the main executable
  105. link map and run its init functions. */
  106. static void
  107. call_init (int argc, char **argv, char **env)
  108. {
  109. /* Obtain the main map of the executable. */
  110. struct link_map *l = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
  111. /* DT_PREINIT_ARRAY is not processed here. It is already handled in
  112. _dl_init in elf/dl-init.c. Also see the call_init function in
  113. the same file. */
  114. if (ELF_INITFINI && l->l_info[DT_INIT] != NULL)
  115. DL_CALL_DT_INIT(l, l->l_addr + l->l_info[DT_INIT]->d_un.d_ptr,
  116. argc, argv, env);
  117. ElfW(Dyn) *init_array = l->l_info[DT_INIT_ARRAY];
  118. if (init_array != NULL)
  119. {
  120. unsigned int jm
  121. = l->l_info[DT_INIT_ARRAYSZ]->d_un.d_val / sizeof (ElfW(Addr));
  122. ElfW(Addr) *addrs = (void *) (init_array->d_un.d_ptr + l->l_addr);
  123. for (unsigned int j = 0; j < jm; ++j)
  124. ((dl_init_t) addrs[j]) (argc, argv, env);
  125. }
  126. }
  127. #else /* !SHARED */
  128. /* These magic symbols are provided by the linker. */
  129. extern void (*__preinit_array_start []) (int, char **, char **)
  130. attribute_hidden;
  131. extern void (*__preinit_array_end []) (int, char **, char **)
  132. attribute_hidden;
  133. extern void (*__init_array_start []) (int, char **, char **)
  134. attribute_hidden;
  135. extern void (*__init_array_end []) (int, char **, char **)
  136. attribute_hidden;
  137. extern void (*__fini_array_start []) (void) attribute_hidden;
  138. extern void (*__fini_array_end []) (void) attribute_hidden;
  139. # if ELF_INITFINI
  140. /* These function symbols are provided for the .init/.fini section entry
  141. points automagically by the linker. */
  142. extern void _init (void);
  143. extern void _fini (void);
  144. # endif
  145. /* Initialization for static executables. There is no dynamic
  146. segment, so we access the symbols directly. */
  147. static void
  148. call_init (int argc, char **argv, char **envp)
  149. {
  150. /* For static executables, preinit happens right before init. */
  151. {
  152. const size_t size = __preinit_array_end - __preinit_array_start;
  153. size_t i;
  154. for (i = 0; i < size; i++)
  155. (*__preinit_array_start [i]) (argc, argv, envp);
  156. }
  157. # if ELF_INITFINI
  158. _init ();
  159. # endif
  160. const size_t size = __init_array_end - __init_array_start;
  161. for (size_t i = 0; i < size; i++)
  162. (*__init_array_start [i]) (argc, argv, envp);
  163. }
  164. /* Likewise for the destructor. */
  165. static void
  166. call_fini (void *unused)
  167. {
  168. size_t i = __fini_array_end - __fini_array_start;
  169. while (i-- > 0)
  170. (*__fini_array_start [i]) ();
  171. # if ELF_INITFINI
  172. _fini ();
  173. # endif
  174. }
  175. #endif /* !SHARED */
  176. #include <libc-start.h>
  177. STATIC int LIBC_START_MAIN (int (*main) (int, char **, char **
  178. MAIN_AUXVEC_DECL),
  179. int argc,
  180. char **argv,
  181. #ifdef LIBC_START_MAIN_AUXVEC_ARG
  182. ElfW(auxv_t) *auxvec,
  183. #endif
  184. __typeof (main) init,
  185. void (*fini) (void),
  186. void (*rtld_fini) (void),
  187. void *stack_end)
  188. __attribute__ ((noreturn));
  189. /* Note: The init and fini parameters are no longer used. fini is
  190. completely unused, init is still called if not NULL, but the
  191. current startup code always passes NULL. (In the future, it would
  192. be possible to use fini to pass a version code if init is NULL, to
  193. indicate the link-time glibc without introducing a hard
  194. incompatibility for new programs with older glibc versions.)
  195. For dynamically linked executables, the dynamic segment is used to
  196. locate constructors and destructors. For statically linked
  197. executables, the relevant symbols are access directly. */
  198. STATIC int
  199. LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
  200. int argc, char **argv,
  201. #ifdef LIBC_START_MAIN_AUXVEC_ARG
  202. ElfW(auxv_t) *auxvec,
  203. #endif
  204. __typeof (main) init,
  205. void (*fini) (void),
  206. void (*rtld_fini) (void), void *stack_end)
  207. {
  208. #ifndef SHARED
  209. char **ev = &argv[argc + 1];
  210. __environ = ev;
  211. /* Store the lowest stack address. This is done in ld.so if this is
  212. the code for the DSO. */
  213. __libc_stack_end = stack_end;
  214. # ifdef HAVE_AUX_VECTOR
  215. /* First process the auxiliary vector since we need to find the
  216. program header to locate an eventually present PT_TLS entry. */
  217. # ifndef LIBC_START_MAIN_AUXVEC_ARG
  218. ElfW(auxv_t) *auxvec;
  219. {
  220. char **evp = ev;
  221. while (*evp++ != NULL)
  222. ;
  223. auxvec = (ElfW(auxv_t) *) evp;
  224. }
  225. # endif
  226. _dl_aux_init (auxvec);
  227. # endif
  228. __tunables_init (__environ);
  229. ARCH_INIT_CPU_FEATURES ();
  230. /* Do static pie self relocation after tunables and cpu features
  231. are setup for ifunc resolvers. Before this point relocations
  232. must be avoided. */
  233. _dl_relocate_static_pie ();
  234. /* Perform IREL{,A} relocations. */
  235. ARCH_SETUP_IREL ();
  236. /* The stack guard goes into the TCB, so initialize it early. */
  237. ARCH_SETUP_TLS ();
  238. /* In some architectures, IREL{,A} relocations happen after TLS setup in
  239. order to let IFUNC resolvers benefit from TCB information, e.g. powerpc's
  240. hwcap and platform fields available in the TCB. */
  241. ARCH_APPLY_IREL ();
  242. /* Set up the stack checker's canary. */
  243. uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random);
  244. # ifdef THREAD_SET_STACK_GUARD
  245. THREAD_SET_STACK_GUARD (stack_chk_guard);
  246. # else
  247. __stack_chk_guard = stack_chk_guard;
  248. # endif
  249. /* Initialize libpthread if linked in. */
  250. if (__pthread_initialize_minimal != NULL)
  251. __pthread_initialize_minimal ();
  252. /* Set up the pointer guard value. */
  253. uintptr_t pointer_chk_guard = _dl_setup_pointer_guard (_dl_random,
  254. stack_chk_guard);
  255. # ifdef THREAD_SET_POINTER_GUARD
  256. THREAD_SET_POINTER_GUARD (pointer_chk_guard);
  257. # else
  258. __pointer_chk_guard_local = pointer_chk_guard;
  259. # endif
  260. #endif /* !SHARED */
  261. /* Register the destructor of the dynamic linker if there is any. */
  262. if (__glibc_likely (rtld_fini != NULL))
  263. __cxa_atexit ((void (*) (void *)) rtld_fini, NULL, NULL);
  264. #ifndef SHARED
  265. /* Perform early initialization. In the shared case, this function
  266. is called from the dynamic loader as early as possible. */
  267. __libc_early_init (true);
  268. /* Call the initializer of the libc. This is only needed here if we
  269. are compiling for the static library in which case we haven't
  270. run the constructors in `_dl_start_user'. */
  271. __libc_init_first (argc, argv, __environ);
  272. /* Register the destructor of the statically-linked program. */
  273. __cxa_atexit (call_fini, NULL, NULL);
  274. /* Some security at this point. Prevent starting a SUID binary where
  275. the standard file descriptors are not opened. We have to do this
  276. only for statically linked applications since otherwise the dynamic
  277. loader did the work already. */
  278. if (__builtin_expect (__libc_enable_secure, 0))
  279. __libc_check_standard_fds ();
  280. #endif /* !SHARED */
  281. /* Call the initializer of the program, if any. */
  282. #ifdef SHARED
  283. if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_IMPCALLS, 0))
  284. GLRO(dl_debug_printf) ("\ninitialize program: %s\n\n", argv[0]);
  285. if (init != NULL)
  286. /* This is a legacy program which supplied its own init
  287. routine. */
  288. (*init) (argc, argv, __environ MAIN_AUXVEC_PARAM);
  289. else
  290. /* This is a current program. Use the dynamic segment to find
  291. constructors. */
  292. call_init (argc, argv, __environ);
  293. /* Auditing checkpoint: we have a new object. */
  294. _dl_audit_preinit (GL(dl_ns)[LM_ID_BASE]._ns_loaded);
  295. if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_IMPCALLS))
  296. GLRO(dl_debug_printf) ("\ntransferring control: %s\n\n", argv[0]);
  297. #else /* !SHARED */
  298. call_init (argc, argv, __environ);
  299. _dl_debug_initialize (0, LM_ID_BASE);
  300. #endif
  301. __libc_start_call_main (main, argc, argv MAIN_AUXVEC_PARAM);
  302. }
  303. /* Starting with glibc 2.34, the init parameter is always NULL. Older
  304. libcs are not prepared to handle that. The macro
  305. DEFINE_LIBC_START_MAIN_VERSION creates GLIBC_2.34 alias, so that
  306. newly linked binaries reflect that dependency. The macros below
  307. expect that the exported function is called
  308. __libc_start_main_impl. */
  309. #ifdef SHARED
  310. # define DEFINE_LIBC_START_MAIN_VERSION \
  311. DEFINE_LIBC_START_MAIN_VERSION_1 \
  312. strong_alias (__libc_start_main_impl, __libc_start_main_alias_2) \
  313. versioned_symbol (libc, __libc_start_main_alias_2, __libc_start_main, \
  314. GLIBC_2_34);
  315. # if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_34)
  316. # define DEFINE_LIBC_START_MAIN_VERSION_1 \
  317. strong_alias (__libc_start_main_impl, __libc_start_main_alias_1) \
  318. compat_symbol (libc, __libc_start_main_alias_1, __libc_start_main, GLIBC_2_0);
  319. # else
  320. # define DEFINE_LIBC_START_MAIN_VERSION_1
  321. # endif
  322. #else /* !SHARED */
  323. /* Enable calling the function under its exported name. */
  324. # define DEFINE_LIBC_START_MAIN_VERSION \
  325. strong_alias (__libc_start_main_impl, __libc_start_main)
  326. #endif
  327. /* Only define the version information if LIBC_START_MAIN was not set.
  328. If there is a wrapper file, it must expand
  329. DEFINE_LIBC_START_MAIN_VERSION on its own. */
  330. #if DO_DEFINE_LIBC_START_MAIN_VERSION
  331. DEFINE_LIBC_START_MAIN_VERSION
  332. #endif