math: Remove slow paths from atan2 [BZ #15267]
[glibc.git] / csu / libc-start.c
blob05ff7afddf52f8efab57512f596f1ceb47185b66
1 /* Perform initialization and invoke main.
2 Copyright (C) 1998-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 /* Note: This code is only part of the startup code proper for
20 statically linked binaries. For dynamically linked binaries, it
21 resides in libc.so. */
23 /* Mark symbols hidden in static PIE for early self relocation to work. */
24 #if BUILD_PIE_DEFAULT
25 # pragma GCC visibility push(hidden)
26 #endif
28 #include <assert.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <ldsodefs.h>
33 #include <exit-thread.h>
34 #include <libc-diag.h>
35 #include <libc-internal.h>
36 #include <elf/libc-early-init.h>
37 #include <stdbool.h>
38 #include <elf-initfini.h>
39 #include <shlib-compat.h>
41 #include <elf/dl-tunables.h>
43 extern void __libc_init_first (int argc, char **argv, char **envp);
45 #include <tls.h>
46 #ifndef SHARED
47 # include <dl-osinfo.h>
48 # ifndef THREAD_SET_STACK_GUARD
49 /* Only exported for architectures that don't store the stack guard canary
50 in thread local area. */
51 uintptr_t __stack_chk_guard attribute_relro;
52 # endif
53 # ifndef THREAD_SET_POINTER_GUARD
54 /* Only exported for architectures that don't store the pointer guard
55 value in thread local area. */
56 uintptr_t __pointer_chk_guard_local
57 attribute_relro attribute_hidden __attribute__ ((nocommon));
58 # endif
59 #endif
61 #ifdef HAVE_PTR_NTHREADS
62 /* We need atomic operations. */
63 # include <atomic.h>
64 #endif
67 #ifndef SHARED
68 # include <link.h>
69 # include <dl-irel.h>
71 # ifdef ELF_MACHINE_IRELA
72 # define IREL_T ElfW(Rela)
73 # define IPLT_START __rela_iplt_start
74 # define IPLT_END __rela_iplt_end
75 # define IREL elf_irela
76 # elif defined ELF_MACHINE_IREL
77 # define IREL_T ElfW(Rel)
78 # define IPLT_START __rel_iplt_start
79 # define IPLT_END __rel_iplt_end
80 # define IREL elf_irel
81 # endif
83 static void
84 apply_irel (void)
86 # ifdef IREL
87 /* We use weak references for these so that we'll still work with a linker
88 that doesn't define them. Such a linker doesn't support IFUNC at all
89 and so uses won't work, but a statically-linked program that doesn't
90 use any IFUNC symbols won't have a problem. */
91 extern const IREL_T IPLT_START[] __attribute__ ((weak));
92 extern const IREL_T IPLT_END[] __attribute__ ((weak));
93 for (const IREL_T *ipltent = IPLT_START; ipltent < IPLT_END; ++ipltent)
94 IREL (ipltent);
95 # endif
97 #endif
100 #ifdef LIBC_START_MAIN
101 # ifdef LIBC_START_DISABLE_INLINE
102 # define STATIC static
103 # else
104 # define STATIC static inline __attribute__ ((always_inline))
105 # endif
106 # define DO_DEFINE_LIBC_START_MAIN_VERSION 0
107 #else
108 # define STATIC
109 # define LIBC_START_MAIN __libc_start_main_impl
110 # define DO_DEFINE_LIBC_START_MAIN_VERSION 1
111 #endif
113 #ifdef MAIN_AUXVEC_ARG
114 /* main gets passed a pointer to the auxiliary. */
115 # define MAIN_AUXVEC_DECL , void *
116 # define MAIN_AUXVEC_PARAM , auxvec
117 #else
118 # define MAIN_AUXVEC_DECL
119 # define MAIN_AUXVEC_PARAM
120 #endif
122 #ifndef ARCH_INIT_CPU_FEATURES
123 # define ARCH_INIT_CPU_FEATURES()
124 #endif
126 #ifdef SHARED
127 /* Initialization for dynamic executables. Find the main executable
128 link map and run its init functions. */
129 static void
130 call_init (int argc, char **argv, char **env)
132 /* Obtain the main map of the executable. */
133 struct link_map *l = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
135 /* DT_PREINIT_ARRAY is not processed here. It is already handled in
136 _dl_init in elf/dl-init.c. Also see the call_init function in
137 the same file. */
139 if (ELF_INITFINI && l->l_info[DT_INIT] != NULL)
140 DL_CALL_DT_INIT(l, l->l_addr + l->l_info[DT_INIT]->d_un.d_ptr,
141 argc, argv, env);
143 ElfW(Dyn) *init_array = l->l_info[DT_INIT_ARRAY];
144 if (init_array != NULL)
146 unsigned int jm
147 = l->l_info[DT_INIT_ARRAYSZ]->d_un.d_val / sizeof (ElfW(Addr));
148 ElfW(Addr) *addrs = (void *) (init_array->d_un.d_ptr + l->l_addr);
149 for (unsigned int j = 0; j < jm; ++j)
150 ((dl_init_t) addrs[j]) (argc, argv, env);
154 #else /* !SHARED */
156 /* These magic symbols are provided by the linker. */
157 extern void (*__preinit_array_start []) (int, char **, char **)
158 attribute_hidden;
159 extern void (*__preinit_array_end []) (int, char **, char **)
160 attribute_hidden;
161 extern void (*__init_array_start []) (int, char **, char **)
162 attribute_hidden;
163 extern void (*__init_array_end []) (int, char **, char **)
164 attribute_hidden;
165 extern void (*__fini_array_start []) (void) attribute_hidden;
166 extern void (*__fini_array_end []) (void) attribute_hidden;
168 # if ELF_INITFINI
169 /* These function symbols are provided for the .init/.fini section entry
170 points automagically by the linker. */
171 extern void _init (void);
172 extern void _fini (void);
173 # endif
175 /* Initialization for static executables. There is no dynamic
176 segment, so we access the symbols directly. */
177 static void
178 call_init (int argc, char **argv, char **envp)
180 /* For static executables, preinit happens right before init. */
182 const size_t size = __preinit_array_end - __preinit_array_start;
183 size_t i;
184 for (i = 0; i < size; i++)
185 (*__preinit_array_start [i]) (argc, argv, envp);
188 # if ELF_INITFINI
189 _init ();
190 # endif
192 const size_t size = __init_array_end - __init_array_start;
193 for (size_t i = 0; i < size; i++)
194 (*__init_array_start [i]) (argc, argv, envp);
197 /* Likewise for the destructor. */
198 static void
199 call_fini (void *unused)
201 size_t i = __fini_array_end - __fini_array_start;
202 while (i-- > 0)
203 (*__fini_array_start [i]) ();
205 # if ELF_INITFINI
206 _fini ();
207 # endif
210 #endif /* !SHARED */
212 #include <libc-start.h>
214 STATIC int LIBC_START_MAIN (int (*main) (int, char **, char **
215 MAIN_AUXVEC_DECL),
216 int argc,
217 char **argv,
218 #ifdef LIBC_START_MAIN_AUXVEC_ARG
219 ElfW(auxv_t) *auxvec,
220 #endif
221 __typeof (main) init,
222 void (*fini) (void),
223 void (*rtld_fini) (void),
224 void *stack_end)
225 __attribute__ ((noreturn));
228 /* Note: The init and fini parameters are no longer used. fini is
229 completely unused, init is still called if not NULL, but the
230 current startup code always passes NULL. (In the future, it would
231 be possible to use fini to pass a version code if init is NULL, to
232 indicate the link-time glibc without introducing a hard
233 incompatibility for new programs with older glibc versions.)
235 For dynamically linked executables, the dynamic segment is used to
236 locate constructors and destructors. For statically linked
237 executables, the relevant symbols are access directly. */
238 STATIC int
239 LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
240 int argc, char **argv,
241 #ifdef LIBC_START_MAIN_AUXVEC_ARG
242 ElfW(auxv_t) *auxvec,
243 #endif
244 __typeof (main) init,
245 void (*fini) (void),
246 void (*rtld_fini) (void), void *stack_end)
248 /* Result of the 'main' function. */
249 int result;
251 #ifndef SHARED
252 char **ev = &argv[argc + 1];
254 __environ = ev;
256 /* Store the lowest stack address. This is done in ld.so if this is
257 the code for the DSO. */
258 __libc_stack_end = stack_end;
260 # ifdef HAVE_AUX_VECTOR
261 /* First process the auxiliary vector since we need to find the
262 program header to locate an eventually present PT_TLS entry. */
263 # ifndef LIBC_START_MAIN_AUXVEC_ARG
264 ElfW(auxv_t) *auxvec;
266 char **evp = ev;
267 while (*evp++ != NULL)
269 auxvec = (ElfW(auxv_t) *) evp;
271 # endif
272 _dl_aux_init (auxvec);
273 if (GL(dl_phdr) == NULL)
274 # endif
276 /* Starting from binutils-2.23, the linker will define the
277 magic symbol __ehdr_start to point to our own ELF header
278 if it is visible in a segment that also includes the phdrs.
279 So we can set up _dl_phdr and _dl_phnum even without any
280 information from auxv. */
282 extern const ElfW(Ehdr) __ehdr_start
283 # if BUILD_PIE_DEFAULT
284 __attribute__ ((visibility ("hidden")));
285 # else
286 __attribute__ ((weak, visibility ("hidden")));
287 if (&__ehdr_start != NULL)
288 # endif
290 assert (__ehdr_start.e_phentsize == sizeof *GL(dl_phdr));
291 GL(dl_phdr) = (const void *) &__ehdr_start + __ehdr_start.e_phoff;
292 GL(dl_phnum) = __ehdr_start.e_phnum;
296 /* Initialize very early so that tunables can use it. */
297 __libc_init_secure ();
299 __tunables_init (__environ);
301 ARCH_INIT_CPU_FEATURES ();
303 /* Do static pie self relocation after tunables and cpu features
304 are setup for ifunc resolvers. Before this point relocations
305 must be avoided. */
306 _dl_relocate_static_pie ();
308 /* Perform IREL{,A} relocations. */
309 ARCH_SETUP_IREL ();
311 /* The stack guard goes into the TCB, so initialize it early. */
312 ARCH_SETUP_TLS ();
314 /* In some architectures, IREL{,A} relocations happen after TLS setup in
315 order to let IFUNC resolvers benefit from TCB information, e.g. powerpc's
316 hwcap and platform fields available in the TCB. */
317 ARCH_APPLY_IREL ();
319 /* Set up the stack checker's canary. */
320 uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random);
321 # ifdef THREAD_SET_STACK_GUARD
322 THREAD_SET_STACK_GUARD (stack_chk_guard);
323 # else
324 __stack_chk_guard = stack_chk_guard;
325 # endif
327 # ifdef DL_SYSDEP_OSCHECK
329 /* This needs to run to initiliaze _dl_osversion before TLS
330 setup might check it. */
331 DL_SYSDEP_OSCHECK (__libc_fatal);
333 # endif
335 /* Initialize libpthread if linked in. */
336 if (__pthread_initialize_minimal != NULL)
337 __pthread_initialize_minimal ();
339 /* Set up the pointer guard value. */
340 uintptr_t pointer_chk_guard = _dl_setup_pointer_guard (_dl_random,
341 stack_chk_guard);
342 # ifdef THREAD_SET_POINTER_GUARD
343 THREAD_SET_POINTER_GUARD (pointer_chk_guard);
344 # else
345 __pointer_chk_guard_local = pointer_chk_guard;
346 # endif
348 #endif /* !SHARED */
350 /* Register the destructor of the dynamic linker if there is any. */
351 if (__glibc_likely (rtld_fini != NULL))
352 __cxa_atexit ((void (*) (void *)) rtld_fini, NULL, NULL);
354 #ifndef SHARED
355 /* Perform early initialization. In the shared case, this function
356 is called from the dynamic loader as early as possible. */
357 __libc_early_init (true);
359 /* Call the initializer of the libc. This is only needed here if we
360 are compiling for the static library in which case we haven't
361 run the constructors in `_dl_start_user'. */
362 __libc_init_first (argc, argv, __environ);
364 /* Register the destructor of the statically-linked program. */
365 __cxa_atexit (call_fini, NULL, NULL);
367 /* Some security at this point. Prevent starting a SUID binary where
368 the standard file descriptors are not opened. We have to do this
369 only for statically linked applications since otherwise the dynamic
370 loader did the work already. */
371 if (__builtin_expect (__libc_enable_secure, 0))
372 __libc_check_standard_fds ();
373 #endif /* !SHARED */
375 /* Call the initializer of the program, if any. */
376 #ifdef SHARED
377 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_IMPCALLS, 0))
378 GLRO(dl_debug_printf) ("\ninitialize program: %s\n\n", argv[0]);
380 if (init != NULL)
381 /* This is a legacy program which supplied its own init
382 routine. */
383 (*init) (argc, argv, __environ MAIN_AUXVEC_PARAM);
384 else
385 /* This is a current program. Use the dynamic segment to find
386 constructors. */
387 call_init (argc, argv, __environ);
388 #else /* !SHARED */
389 call_init (argc, argv, __environ);
390 #endif /* SHARED */
392 #ifdef SHARED
393 /* Auditing checkpoint: we have a new object. */
394 if (__glibc_unlikely (GLRO(dl_naudit) > 0))
396 struct audit_ifaces *afct = GLRO(dl_audit);
397 struct link_map *head = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
398 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
400 if (afct->preinit != NULL)
401 afct->preinit (&link_map_audit_state (head, cnt)->cookie);
403 afct = afct->next;
406 #endif
408 #ifdef SHARED
409 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_IMPCALLS))
410 GLRO(dl_debug_printf) ("\ntransferring control: %s\n\n", argv[0]);
411 #endif
413 #ifndef SHARED
414 _dl_debug_initialize (0, LM_ID_BASE);
415 #endif
416 #ifdef HAVE_CLEANUP_JMP_BUF
417 /* Memory for the cancellation buffer. */
418 struct pthread_unwind_buf unwind_buf;
420 int not_first_call;
421 DIAG_PUSH_NEEDS_COMMENT;
422 #if __GNUC_PREREQ (7, 0)
423 /* This call results in a -Wstringop-overflow warning because struct
424 pthread_unwind_buf is smaller than jmp_buf. setjmp and longjmp
425 do not use anything beyond the common prefix (they never access
426 the saved signal mask), so that is a false positive. */
427 DIAG_IGNORE_NEEDS_COMMENT (11, "-Wstringop-overflow=");
428 #endif
429 not_first_call = setjmp ((struct __jmp_buf_tag *) unwind_buf.cancel_jmp_buf);
430 DIAG_POP_NEEDS_COMMENT;
431 if (__glibc_likely (! not_first_call))
433 struct pthread *self = THREAD_SELF;
435 /* Store old info. */
436 unwind_buf.priv.data.prev = THREAD_GETMEM (self, cleanup_jmp_buf);
437 unwind_buf.priv.data.cleanup = THREAD_GETMEM (self, cleanup);
439 /* Store the new cleanup handler info. */
440 THREAD_SETMEM (self, cleanup_jmp_buf, &unwind_buf);
442 /* Run the program. */
443 result = main (argc, argv, __environ MAIN_AUXVEC_PARAM);
445 else
447 /* Remove the thread-local data. */
448 # ifdef SHARED
449 PTHFCT_CALL (ptr__nptl_deallocate_tsd, ());
450 # else
451 extern void __nptl_deallocate_tsd (void) __attribute ((weak));
452 __nptl_deallocate_tsd ();
453 # endif
455 /* One less thread. Decrement the counter. If it is zero we
456 terminate the entire process. */
457 result = 0;
458 # ifdef SHARED
459 unsigned int *ptr = __libc_pthread_functions.ptr_nthreads;
460 # ifdef PTR_DEMANGLE
461 PTR_DEMANGLE (ptr);
462 # endif
463 # else
464 extern unsigned int __nptl_nthreads __attribute ((weak));
465 unsigned int *const ptr = &__nptl_nthreads;
466 # endif
468 if (! atomic_decrement_and_test (ptr))
469 /* Not much left to do but to exit the thread, not the process. */
470 __exit_thread ();
472 #else
473 /* Nothing fancy, just call the function. */
474 result = main (argc, argv, __environ MAIN_AUXVEC_PARAM);
475 #endif
477 exit (result);
480 /* Starting with glibc 2.34, the init parameter is always NULL. Older
481 libcs are not prepared to handle that. The macro
482 DEFINE_LIBC_START_MAIN_VERSION creates GLIBC_2.34 alias, so that
483 newly linked binaries reflect that dependency. The macros below
484 expect that the exported function is called
485 __libc_start_main_impl. */
486 #ifdef SHARED
487 # define DEFINE_LIBC_START_MAIN_VERSION \
488 DEFINE_LIBC_START_MAIN_VERSION_1 \
489 strong_alias (__libc_start_main_impl, __libc_start_main_alias_2) \
490 versioned_symbol (libc, __libc_start_main_alias_2, __libc_start_main, \
491 GLIBC_2_34);
493 # if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_34)
494 # define DEFINE_LIBC_START_MAIN_VERSION_1 \
495 strong_alias (__libc_start_main_impl, __libc_start_main_alias_1) \
496 compat_symbol (libc, __libc_start_main_alias_1, __libc_start_main, GLIBC_2_0);
497 # else
498 # define DEFINE_LIBC_START_MAIN_VERSION_1
499 # endif
500 #else /* !SHARED */
501 /* Enable calling the function under its exported name. */
502 # define DEFINE_LIBC_START_MAIN_VERSION \
503 strong_alias (__libc_start_main_impl, __libc_start_main)
504 #endif
506 /* Only define the version information if LIBC_START_MAIN was not set.
507 If there is a wrapper file, it must expand
508 DEFINE_LIBC_START_MAIN_VERSION on its own. */
509 #if DO_DEFINE_LIBC_START_MAIN_VERSION
510 DEFINE_LIBC_START_MAIN_VERSION
511 #endif