]> git.ipfire.org Git - thirdparty/glibc.git/blame - csu/libc-start.c
elf: Remove __libc_init_secure
[thirdparty/glibc.git] / csu / libc-start.c
CommitLineData
035c012e 1/* Perform initialization and invoke main.
581c785b 2 Copyright (C) 1998-2022 Free Software Foundation, Inc.
7dea968e
UD
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
7dea968e
UD
9
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
41bdb6e2 13 Lesser General Public License for more details.
7dea968e 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
7dea968e 18
035c012e
FW
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. */
22
47618209
SN
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
035c012e 27
288f7d79 28#include <assert.h>
3db52d94 29#include <stdlib.h>
dde2652b 30#include <stdio.h>
3db52d94 31#include <unistd.h>
a42195db 32#include <ldsodefs.h>
2098d403 33#include <libc-diag.h>
67e58f39 34#include <libc-internal.h>
ec935dea 35#include <elf/libc-early-init.h>
03e187a4 36#include <stdbool.h>
035c012e
FW
37#include <elf-initfini.h>
38#include <shlib-compat.h>
67e58f39
SP
39
40#include <elf/dl-tunables.h>
3db52d94 41
31161268
UD
42extern void __libc_init_first (int argc, char **argv, char **envp);
43
7ae4abe9 44#include <tls.h>
4fb7a71f 45#ifndef SHARED
dde2652b 46# include <dl-osinfo.h>
35f1e827
UD
47# ifndef THREAD_SET_STACK_GUARD
48/* Only exported for architectures that don't store the stack guard canary
49 in thread local area. */
50uintptr_t __stack_chk_guard attribute_relro;
51# endif
c61b4d41
CD
52# ifndef THREAD_SET_POINTER_GUARD
53/* Only exported for architectures that don't store the pointer guard
54 value in thread local area. */
7c241325 55uintptr_t __pointer_chk_guard_local attribute_relro attribute_hidden;
c61b4d41 56# endif
4fb7a71f
AJ
57#endif
58
21ad0558
RM
59#ifndef SHARED
60# include <link.h>
61# include <dl-irel.h>
62
63# ifdef ELF_MACHINE_IRELA
64# define IREL_T ElfW(Rela)
65# define IPLT_START __rela_iplt_start
66# define IPLT_END __rela_iplt_end
67# define IREL elf_irela
68# elif defined ELF_MACHINE_IREL
69# define IREL_T ElfW(Rel)
70# define IPLT_START __rel_iplt_start
71# define IPLT_END __rel_iplt_end
72# define IREL elf_irel
73# endif
74
21ad0558
RM
75static void
76apply_irel (void)
77{
35a5b08b
RM
78# ifdef IREL
79 /* We use weak references for these so that we'll still work with a linker
80 that doesn't define them. Such a linker doesn't support IFUNC at all
81 and so uses won't work, but a statically-linked program that doesn't
82 use any IFUNC symbols won't have a problem. */
83 extern const IREL_T IPLT_START[] __attribute__ ((weak));
84 extern const IREL_T IPLT_END[] __attribute__ ((weak));
21ad0558
RM
85 for (const IREL_T *ipltent = IPLT_START; ipltent < IPLT_END; ++ipltent)
86 IREL (ipltent);
35a5b08b 87# endif
21ad0558
RM
88}
89#endif
90
91
2b089f21 92#ifdef LIBC_START_MAIN
11986c68
UD
93# ifdef LIBC_START_DISABLE_INLINE
94# define STATIC static
95# else
96# define STATIC static inline __attribute__ ((always_inline))
97# endif
035c012e 98# define DO_DEFINE_LIBC_START_MAIN_VERSION 0
2b089f21
RM
99#else
100# define STATIC
035c012e
FW
101# define LIBC_START_MAIN __libc_start_main_impl
102# define DO_DEFINE_LIBC_START_MAIN_VERSION 1
2b089f21
RM
103#endif
104
2b089f21 105#ifdef MAIN_AUXVEC_ARG
09d65ff3
UD
106/* main gets passed a pointer to the auxiliary. */
107# define MAIN_AUXVEC_DECL , void *
108# define MAIN_AUXVEC_PARAM , auxvec
109#else
110# define MAIN_AUXVEC_DECL
111# define MAIN_AUXVEC_PARAM
2b089f21
RM
112#endif
113
4158ba08
SP
114#ifndef ARCH_INIT_CPU_FEATURES
115# define ARCH_INIT_CPU_FEATURES()
116#endif
117
130fca17
FW
118/* Obtain the definition of __libc_start_call_main. */
119#include <libc_start_call_main.h>
120
035c012e
FW
121#ifdef SHARED
122/* Initialization for dynamic executables. Find the main executable
123 link map and run its init functions. */
124static void
125call_init (int argc, char **argv, char **env)
126{
127 /* Obtain the main map of the executable. */
128 struct link_map *l = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
129
130 /* DT_PREINIT_ARRAY is not processed here. It is already handled in
131 _dl_init in elf/dl-init.c. Also see the call_init function in
132 the same file. */
133
134 if (ELF_INITFINI && l->l_info[DT_INIT] != NULL)
135 DL_CALL_DT_INIT(l, l->l_addr + l->l_info[DT_INIT]->d_un.d_ptr,
136 argc, argv, env);
137
138 ElfW(Dyn) *init_array = l->l_info[DT_INIT_ARRAY];
139 if (init_array != NULL)
140 {
141 unsigned int jm
142 = l->l_info[DT_INIT_ARRAYSZ]->d_un.d_val / sizeof (ElfW(Addr));
143 ElfW(Addr) *addrs = (void *) (init_array->d_un.d_ptr + l->l_addr);
144 for (unsigned int j = 0; j < jm; ++j)
145 ((dl_init_t) addrs[j]) (argc, argv, env);
146 }
147}
148
149#else /* !SHARED */
150
151/* These magic symbols are provided by the linker. */
152extern void (*__preinit_array_start []) (int, char **, char **)
153 attribute_hidden;
154extern void (*__preinit_array_end []) (int, char **, char **)
155 attribute_hidden;
156extern void (*__init_array_start []) (int, char **, char **)
157 attribute_hidden;
158extern void (*__init_array_end []) (int, char **, char **)
159 attribute_hidden;
160extern void (*__fini_array_start []) (void) attribute_hidden;
161extern void (*__fini_array_end []) (void) attribute_hidden;
162
163# if ELF_INITFINI
164/* These function symbols are provided for the .init/.fini section entry
165 points automagically by the linker. */
166extern void _init (void);
167extern void _fini (void);
168# endif
169
170/* Initialization for static executables. There is no dynamic
171 segment, so we access the symbols directly. */
172static void
173call_init (int argc, char **argv, char **envp)
174{
175 /* For static executables, preinit happens right before init. */
176 {
177 const size_t size = __preinit_array_end - __preinit_array_start;
178 size_t i;
179 for (i = 0; i < size; i++)
180 (*__preinit_array_start [i]) (argc, argv, envp);
181 }
182
183# if ELF_INITFINI
184 _init ();
185# endif
186
187 const size_t size = __init_array_end - __init_array_start;
188 for (size_t i = 0; i < size; i++)
189 (*__init_array_start [i]) (argc, argv, envp);
190}
191
192/* Likewise for the destructor. */
193static void
194call_fini (void *unused)
195{
196 size_t i = __fini_array_end - __fini_array_start;
197 while (i-- > 0)
198 (*__fini_array_start [i]) ();
199
200# if ELF_INITFINI
201 _fini ();
202# endif
203}
204
205#endif /* !SHARED */
206
91ac3a7d
TMQMF
207#include <libc-start.h>
208
09d65ff3
UD
209STATIC int LIBC_START_MAIN (int (*main) (int, char **, char **
210 MAIN_AUXVEC_DECL),
2b089f21 211 int argc,
70d9946a 212 char **argv,
2b089f21 213#ifdef LIBC_START_MAIN_AUXVEC_ARG
70d9946a 214 ElfW(auxv_t) *auxvec,
2b089f21 215#endif
2b089f21 216 __typeof (main) init,
2b089f21
RM
217 void (*fini) (void),
218 void (*rtld_fini) (void),
70d9946a 219 void *stack_end)
17427edd 220 __attribute__ ((noreturn));
a828c2f5 221
9dcafc55 222
035c012e
FW
223/* Note: The init and fini parameters are no longer used. fini is
224 completely unused, init is still called if not NULL, but the
225 current startup code always passes NULL. (In the future, it would
226 be possible to use fini to pass a version code if init is NULL, to
227 indicate the link-time glibc without introducing a hard
228 incompatibility for new programs with older glibc versions.)
229
230 For dynamically linked executables, the dynamic segment is used to
231 locate constructors and destructors. For statically linked
232 executables, the relevant symbols are access directly. */
2b089f21 233STATIC int
09d65ff3 234LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL),
70d9946a 235 int argc, char **argv,
2b089f21 236#ifdef LIBC_START_MAIN_AUXVEC_ARG
70d9946a 237 ElfW(auxv_t) *auxvec,
2b089f21 238#endif
2b089f21 239 __typeof (main) init,
2b089f21 240 void (*fini) (void),
70d9946a 241 void (*rtld_fini) (void), void *stack_end)
7dea968e 242{
3fb2606a 243#ifndef SHARED
70d9946a 244 char **ev = &argv[argc + 1];
4f657581 245
70d9946a 246 __environ = ev;
ab95290c 247
ea4f25a7
UD
248 /* Store the lowest stack address. This is done in ld.so if this is
249 the code for the DSO. */
c0fb8a56 250 __libc_stack_end = stack_end;
31161268 251
8a30f00f
UD
252# ifdef HAVE_AUX_VECTOR
253 /* First process the auxiliary vector since we need to find the
254 program header to locate an eventually present PT_TLS entry. */
2b089f21 255# ifndef LIBC_START_MAIN_AUXVEC_ARG
70d9946a 256 ElfW(auxv_t) *auxvec;
2b089f21 257 {
70d9946a 258 char **evp = ev;
97026947
UD
259 while (*evp++ != NULL)
260 ;
70d9946a 261 auxvec = (ElfW(auxv_t) *) evp;
2b089f21
RM
262 }
263# endif
264 _dl_aux_init (auxvec);
288f7d79 265 if (GL(dl_phdr) == NULL)
8a30f00f 266# endif
288f7d79
RM
267 {
268 /* Starting from binutils-2.23, the linker will define the
269 magic symbol __ehdr_start to point to our own ELF header
270 if it is visible in a segment that also includes the phdrs.
271 So we can set up _dl_phdr and _dl_phnum even without any
272 information from auxv. */
273
ae9552cf 274 extern const ElfW(Ehdr) __ehdr_start
04af1eb3
SN
275# if BUILD_PIE_DEFAULT
276 __attribute__ ((visibility ("hidden")));
277# else
ae9552cf 278 __attribute__ ((weak, visibility ("hidden")));
288f7d79 279 if (&__ehdr_start != NULL)
04af1eb3 280# endif
288f7d79
RM
281 {
282 assert (__ehdr_start.e_phentsize == sizeof *GL(dl_phdr));
283 GL(dl_phdr) = (const void *) &__ehdr_start + __ehdr_start.e_phoff;
284 GL(dl_phnum) = __ehdr_start.e_phnum;
285 }
286 }
287
67e58f39
SP
288 __tunables_init (__environ);
289
4158ba08
SP
290 ARCH_INIT_CPU_FEATURES ();
291
86d439b0
SN
292 /* Do static pie self relocation after tunables and cpu features
293 are setup for ifunc resolvers. Before this point relocations
294 must be avoided. */
295 _dl_relocate_static_pie ();
296
21ad0558 297 /* Perform IREL{,A} relocations. */
91ac3a7d 298 ARCH_SETUP_IREL ();
1c3c269b 299
003a27e8 300 /* The stack guard goes into the TCB, so initialize it early. */
bcfa607b 301 ARCH_SETUP_TLS ();
a828c2f5 302
91ac3a7d
TMQMF
303 /* In some architectures, IREL{,A} relocations happen after TLS setup in
304 order to let IFUNC resolvers benefit from TCB information, e.g. powerpc's
305 hwcap and platform fields available in the TCB. */
306 ARCH_APPLY_IREL ();
307
35f1e827 308 /* Set up the stack checker's canary. */
5b656a0d 309 uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random);
75fb247e 310# ifdef THREAD_SET_STACK_GUARD
35f1e827 311 THREAD_SET_STACK_GUARD (stack_chk_guard);
75fb247e 312# else
35f1e827 313 __stack_chk_guard = stack_chk_guard;
75fb247e 314# endif
c61b4d41 315
003a27e8 316# ifdef DL_SYSDEP_OSCHECK
e7570f41
FW
317 {
318 /* This needs to run to initiliaze _dl_osversion before TLS
319 setup might check it. */
320 DL_SYSDEP_OSCHECK (__libc_fatal);
321 }
003a27e8
NA
322# endif
323
324 /* Initialize libpthread if linked in. */
325 if (__pthread_initialize_minimal != NULL)
326 __pthread_initialize_minimal ();
327
c61b4d41
CD
328 /* Set up the pointer guard value. */
329 uintptr_t pointer_chk_guard = _dl_setup_pointer_guard (_dl_random,
330 stack_chk_guard);
331# ifdef THREAD_SET_POINTER_GUARD
332 THREAD_SET_POINTER_GUARD (pointer_chk_guard);
333# else
334 __pointer_chk_guard_local = pointer_chk_guard;
335# endif
336
91ac3a7d 337#endif /* !SHARED */
75fb247e 338
3db52d94 339 /* Register the destructor of the dynamic linker if there is any. */
a1ffb40e 340 if (__glibc_likely (rtld_fini != NULL))
c08bc50a 341 __cxa_atexit ((void (*) (void *)) rtld_fini, NULL, NULL);
3db52d94 342
43c59a70 343#ifndef SHARED
ec935dea
FW
344 /* Perform early initialization. In the shared case, this function
345 is called from the dynamic loader as early as possible. */
03e187a4 346 __libc_early_init (true);
ec935dea 347
dacc8ffa
UD
348 /* Call the initializer of the libc. This is only needed here if we
349 are compiling for the static library in which case we haven't
350 run the constructors in `_dl_start_user'. */
31161268
UD
351 __libc_init_first (argc, argv, __environ);
352
035c012e
FW
353 /* Register the destructor of the statically-linked program. */
354 __cxa_atexit (call_fini, NULL, NULL);
43c59a70 355
9946f75a
UD
356 /* Some security at this point. Prevent starting a SUID binary where
357 the standard file descriptors are not opened. We have to do this
358 only for statically linked applications since otherwise the dynamic
359 loader did the work already. */
360 if (__builtin_expect (__libc_enable_secure, 0))
361 __libc_check_standard_fds ();
035c012e 362#endif /* !SHARED */
9946f75a 363
4194bc66 364 /* Call the initializer of the program, if any. */
b5567b2a 365#ifdef SHARED
afdca0f2 366 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_IMPCALLS, 0))
154d10bd 367 GLRO(dl_debug_printf) ("\ninitialize program: %s\n\n", argv[0]);
035c012e
FW
368
369 if (init != NULL)
370 /* This is a legacy program which supplied its own init
371 routine. */
04395c90 372 (*init) (argc, argv, __environ MAIN_AUXVEC_PARAM);
035c012e
FW
373 else
374 /* This is a current program. Use the dynamic segment to find
375 constructors. */
376 call_init (argc, argv, __environ);
3db52d94 377
9dcafc55 378 /* Auditing checkpoint: we have a new object. */
0b98a874 379 _dl_audit_preinit (GL(dl_ns)[LM_ID_BASE]._ns_loaded);
9dcafc55 380
a1ffb40e 381 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_IMPCALLS))
154d10bd 382 GLRO(dl_debug_printf) ("\ntransferring control: %s\n\n", argv[0]);
0b98a874
AZ
383#else /* !SHARED */
384 call_init (argc, argv, __environ);
31161268 385
f737dfd0
CD
386 _dl_debug_initialize (0, LM_ID_BASE);
387#endif
7ae4abe9 388
130fca17 389 __libc_start_call_main (main, argc, argv MAIN_AUXVEC_PARAM);
7dea968e 390}
035c012e
FW
391
392/* Starting with glibc 2.34, the init parameter is always NULL. Older
393 libcs are not prepared to handle that. The macro
394 DEFINE_LIBC_START_MAIN_VERSION creates GLIBC_2.34 alias, so that
395 newly linked binaries reflect that dependency. The macros below
396 expect that the exported function is called
397 __libc_start_main_impl. */
398#ifdef SHARED
399# define DEFINE_LIBC_START_MAIN_VERSION \
400 DEFINE_LIBC_START_MAIN_VERSION_1 \
401 strong_alias (__libc_start_main_impl, __libc_start_main_alias_2) \
402 versioned_symbol (libc, __libc_start_main_alias_2, __libc_start_main, \
403 GLIBC_2_34);
404
405# if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_34)
406# define DEFINE_LIBC_START_MAIN_VERSION_1 \
407 strong_alias (__libc_start_main_impl, __libc_start_main_alias_1) \
408 compat_symbol (libc, __libc_start_main_alias_1, __libc_start_main, GLIBC_2_0);
409# else
410# define DEFINE_LIBC_START_MAIN_VERSION_1
411# endif
412#else /* !SHARED */
413/* Enable calling the function under its exported name. */
414# define DEFINE_LIBC_START_MAIN_VERSION \
415 strong_alias (__libc_start_main_impl, __libc_start_main)
416#endif
417
418/* Only define the version information if LIBC_START_MAIN was not set.
419 If there is a wrapper file, it must expand
420 DEFINE_LIBC_START_MAIN_VERSION on its own. */
421#if DO_DEFINE_LIBC_START_MAIN_VERSION
422DEFINE_LIBC_START_MAIN_VERSION
423#endif