]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/dl-support.c
Add MADV_POPULATE_READ and MADV_POPULATE_WRITE from Linux 5.14 to bits/mman-linux.h
[thirdparty/glibc.git] / elf / dl-support.c
CommitLineData
266180eb 1/* Support for dynamic linking code in static libc.
2b778ceb 2 Copyright (C) 1996-2021 Free Software Foundation, Inc.
afd4eb37 3 This file is part of the GNU C Library.
266180eb 4
afd4eb37 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.
266180eb 9
afd4eb37
UD
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.
266180eb 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/>. */
266180eb 18
266180eb
RM
19/* This file defines some things that for the dynamic linker are defined in
20 rtld.c and dl-sysdep.c in ways appropriate to bootstrap dynamic linking. */
21
47618209
SN
22#include <string.h>
23/* Mark symbols hidden in static PIE for early self relocation to work.
24 Note: string.h may have ifuncs which cannot be hidden on i686. */
25#if BUILD_PIE_DEFAULT
26# pragma GCC visibility push(hidden)
27#endif
3c720987 28#include <errno.h>
8e17ea58 29#include <libintl.h>
b13927da 30#include <stdlib.h>
0a54e401 31#include <unistd.h>
32b4c839 32#include <sys/param.h>
e054f494 33#include <stdint.h>
a42195db 34#include <ldsodefs.h>
0a54e401 35#include <dl-machine.h>
ec999b8e 36#include <libc-lock.h>
5688da55 37#include <dl-cache.h>
74955460 38#include <dl-librecon.h>
594d423a 39#include <dl-procinfo.h>
74955460 40#include <unsecvars.h>
3b5c1b57 41#include <hp-timing.h>
30950a5f 42#include <stackinfo.h>
1bdda52f
AZ
43#include <dl-vdso.h>
44#include <dl-vdso-setup.h>
18363b4f 45#include <dl-auxv.h>
b13927da 46
266180eb
RM
47extern char *__progname;
48char **_dl_argv = &__progname; /* This is checked for some error messages. */
49
0a54e401
UD
50/* Name of the architecture. */
51const char *_dl_platform;
52size_t _dl_platformlen;
3c720987 53
62dcee57 54int _dl_debug_mask;
12b5b6b7 55int _dl_lazy;
97fd3a30 56ElfW(Addr) _dl_use_load_bias = -2;
dec126b4 57int _dl_dynamic_weak;
0a54e401 58
b13927da
UD
59/* If nonzero print warnings about problematic situations. */
60int _dl_verbose;
61
3996f34b
UD
62/* We never do profiling. */
63const char *_dl_profile;
53bfdc1c 64const char *_dl_profile_output;
3996f34b 65
fcf70d41
UD
66/* Names of shared object for which the RUNPATHs and RPATHs should be
67 ignored. */
b0a01055 68const char *_dl_inhibit_rpath;
310930c1 69
a3d6fb9b
UD
70/* The map for the object we will profile. */
71struct link_map *_dl_profile_map;
72
c0fb8a56
UD
73/* This is the address of the last stack address ever used. */
74void *__libc_stack_end;
75
f787edde
UD
76/* Path where the binary is found. */
77const char *_dl_origin_path;
78
f53c03c2
UD
79/* Nonzero if runtime lookup should not update the .got/.plt. */
80int _dl_bind_not;
81
f91f1c0f
MR
82/* A dummy link map for the executable, used by dlopen to access the global
83 scope. We don't export any symbols ourselves, so this can be minimal. */
84static struct link_map _dl_main_map =
85 {
86 .l_name = (char *) "",
87 .l_real = &_dl_main_map,
88 .l_ns = LM_ID_BASE,
89 .l_libname = &(struct libname_list) { .name = "", .dont_free = 1 },
90 .l_searchlist =
91 {
92 .r_list = &(struct link_map *) { &_dl_main_map },
93 .r_nlist = 1,
94 },
95 .l_symbolic_searchlist = { .r_list = &(struct link_map *) { NULL } },
96 .l_type = lt_executable,
97 .l_scope_mem = { &_dl_main_map.l_searchlist },
98 .l_scope_max = (sizeof (_dl_main_map.l_scope_mem)
99 / sizeof (_dl_main_map.l_scope_mem[0])),
100 .l_scope = _dl_main_map.l_scope_mem,
101 .l_local_scope = { &_dl_main_map.l_searchlist },
102 .l_used = 1,
103 .l_tls_offset = NO_TLS_OFFSET,
104 .l_serial = 1,
105 };
106
c0f62c56 107/* Namespace information. */
f91f1c0f
MR
108struct link_namespaces _dl_ns[DL_NNS] =
109 {
110 [LM_ID_BASE] =
111 {
112 ._ns_loaded = &_dl_main_map,
113 ._ns_nloaded = 1,
114 ._ns_main_searchlist = &_dl_main_map.l_searchlist,
115 }
116 };
117size_t _dl_nns = 1;
be935610 118
bed12f78 119/* Incremented whenever something may have been added to dl_loaded. */
f91f1c0f 120unsigned long long _dl_load_adds = 1;
bed12f78 121
f91f1c0f
MR
122/* Fake scope of the main application. */
123struct r_scope_elem _dl_initial_searchlist =
124 {
125 .r_list = &(struct link_map *) { &_dl_main_map },
126 .r_nlist = 1,
127 };
be935610 128
ce6e047f 129#ifndef HAVE_INLINED_SYSCALLS
9ad04ff7
UD
130/* Nonzero during startup. */
131int _dl_starting_up = 1;
ce6e047f 132#endif
9ad04ff7 133
965cb60a
UD
134/* Random data provided by the kernel. */
135void *_dl_random;
136
ccdf0cab 137/* Get architecture specific initializer. */
4a306ef1 138#include <dl-procruntime.c>
ccdf0cab
UD
139#include <dl-procinfo.c>
140
32b4c839 141size_t _dl_pagesize = EXEC_PAGESIZE;
d6b5d570 142
6c57d320
L
143size_t _dl_minsigstacksize = CONSTANT_MINSIGSTKSZ;
144
73d65cc3
SP
145int _dl_inhibit_cache;
146
d6b5d570
UD
147unsigned int _dl_osversion;
148
149/* All known directories in sorted order. */
150struct r_search_path_elem *_dl_all_dirs;
151
152/* All directories after startup. */
153struct r_search_path_elem *_dl_init_all_dirs;
154
155/* The object to be initialized first. */
156struct link_map *_dl_initfirst;
157
5688da55
UD
158/* Descriptor to write debug messages to. */
159int _dl_debug_fd = STDERR_FILENO;
160
161int _dl_correct_cache_id = _DL_CACHE_DEFAULT_ID;
162
c7683a6d 163ElfW(auxv_t) *_dl_auxv;
dc0a0263 164const ElfW(Phdr) *_dl_phdr;
fcda29e2 165size_t _dl_phnum;
7c241325
FW
166uint64_t _dl_hwcap;
167uint64_t _dl_hwcap2;
172ce013 168
95e7cf29
MR
169/* The value of the FPU control word the kernel will preset in hardware. */
170fpu_control_t _dl_fpu_control = _FPU_DEFAULT;
171
ff08fc59 172#if !HAVE_TUNABLES
7bfa311f
RM
173/* This is not initialized to HWCAP_IMPORTANT, matching the definition
174 of _dl_important_hwcaps, below, where no hwcap strings are ever
175 used. This mask is still used to mediate the lookups in the cache
176 file. Since there is no way to set this nonzero (we don't grok the
177 LD_HWCAP_MASK environment variable here), there is no real point in
178 setting _dl_hwcap nonzero below, but we do anyway. */
7c241325 179uint64_t _dl_hwcap_mask;
ff08fc59 180#endif
7bfa311f 181
30950a5f
RA
182/* Prevailing state of the stack. Generally this includes PF_X, indicating it's
183 * executable but this isn't true for all platforms. */
184ElfW(Word) _dl_stack_flags = DEFAULT_STACK_PERMS;
ecdeaac0 185
1daccf40
FW
186#if THREAD_GSCOPE_IN_TCB
187list_t _dl_stack_used;
188list_t _dl_stack_user;
9d124d81
FW
189list_t _dl_stack_cache;
190size_t _dl_stack_cache_actsize;
191uintptr_t _dl_in_flight_stack;
1daccf40
FW
192int _dl_stack_cache_lock;
193#else
2dd87703
FW
194/* If loading a shared object requires that we make the stack executable
195 when it was not, we do it by calling this function.
196 It returns an errno code or zero on success. */
197int (*_dl_make_stack_executable_hook) (void **) = _dl_make_stack_executable;
a5df0318 198int _dl_thread_gscope_count;
7cbf1c84 199void (*_dl_init_static_tls) (struct link_map *) = &_dl_nothread_init_static_tls;
a5df0318 200#endif
e4eb675d
UD
201struct dl_scope_free_list *_dl_scope_free_list;
202
5e289179 203#ifdef NEED_DL_SYSINFO
cabc8a29
L
204/* Needed for improved syscall handling on at least x86/Linux. NB: Don't
205 initialize it here to avoid RELATIVE relocation in static PIE. */
206uintptr_t _dl_sysinfo;
30e32d23 207#endif
7775448e 208#ifdef NEED_DL_SYSINFO_DSO
f866314b
UD
209/* Address of the ELF headers in the vsyscall page. */
210const ElfW(Ehdr) *_dl_sysinfo_dso;
9cee5585
L
211
212struct link_map *_dl_sysinfo_map;
213
214# include "get-dynamic-info.h"
5e289179 215#endif
d0d4f868 216#include "setup-vdso.h"
1bdda52f
AZ
217/* Define the vDSO function pointers. */
218#include <dl-vdso-setup.c>
5e289179 219
cf197e41
UD
220/* During the program run we must not modify the global data of
221 loaded shared object simultanously in two threads. Therefore we
222 protect `_dl_open' and `_dl_close' in dl-close.c.
223
224 This must be a recursive lock since the initializer function of
225 the loaded object might as well require a call to this function.
226 At this time it is not anymore a problem to modify the tables. */
d3c9f895 227__rtld_lock_define_initialized_recursive (, _dl_load_lock)
5a2a1d75
AS
228/* This lock is used to keep __dl_iterate_phdr from inspecting the
229 list of loaded objects while an object is added to or removed from
230 that list. */
231__rtld_lock_define_initialized_recursive (, _dl_load_write_lock)
cf197e41 232
f8f900ec 233
e4a5f77d 234#ifdef HAVE_AUX_VECTOR
d6b5d570 235int _dl_clktck;
0a54e401 236
e4a5f77d 237void
e4a5f77d 238_dl_aux_init (ElfW(auxv_t) *av)
b13927da 239{
be4d8038
UD
240 int seen = 0;
241 uid_t uid = 0;
242 gid_t gid = 0;
243
cabc8a29
L
244#ifdef NEED_DL_SYSINFO
245 /* NB: Avoid RELATIVE relocation in static PIE. */
246 GL(dl_sysinfo) = DL_SYSINFO_DEFAULT;
247#endif
248
c7683a6d 249 _dl_auxv = av;
f8f900ec
UD
250 for (; av->a_type != AT_NULL; ++av)
251 switch (av->a_type)
252 {
253 case AT_PAGESZ:
aefc9b8c
RM
254 if (av->a_un.a_val != 0)
255 GLRO(dl_pagesize) = av->a_un.a_val;
f8f900ec 256 break;
f8f900ec 257 case AT_CLKTCK:
afdca0f2 258 GLRO(dl_clktck) = av->a_un.a_val;
f8f900ec 259 break;
fcda29e2 260 case AT_PHDR:
dc0a0263 261 GL(dl_phdr) = (const void *) av->a_un.a_val;
fcda29e2
UD
262 break;
263 case AT_PHNUM:
264 GL(dl_phnum) = av->a_un.a_val;
265 break;
76c5ae00
CES
266 case AT_PLATFORM:
267 GLRO(dl_platform) = (void *) av->a_un.a_val;
268 break;
11bf8ce1 269 case AT_HWCAP:
ab1d521d 270 GLRO(dl_hwcap) = (unsigned long int) av->a_un.a_val;
11bf8ce1 271 break;
1ae8bfe0
RA
272 case AT_HWCAP2:
273 GLRO(dl_hwcap2) = (unsigned long int) av->a_un.a_val;
274 break;
95e7cf29
MR
275 case AT_FPUCW:
276 GLRO(dl_fpu_control) = av->a_un.a_val;
277 break;
5df8349b
UD
278#ifdef NEED_DL_SYSINFO
279 case AT_SYSINFO:
280 GL(dl_sysinfo) = av->a_un.a_val;
281 break;
30e32d23 282#endif
7775448e 283#ifdef NEED_DL_SYSINFO_DSO
30e32d23 284 case AT_SYSINFO_EHDR:
39fb308f 285 GL(dl_sysinfo_dso) = (void *) av->a_un.a_val;
30e32d23 286 break;
5df8349b 287#endif
be4d8038
UD
288 case AT_UID:
289 uid ^= av->a_un.a_val;
290 seen |= 1;
291 break;
292 case AT_EUID:
293 uid ^= av->a_un.a_val;
294 seen |= 2;
295 break;
296 case AT_GID:
297 gid ^= av->a_un.a_val;
298 seen |= 4;
299 break;
300 case AT_EGID:
301 gid ^= av->a_un.a_val;
302 seen |= 8;
303 break;
c801e765
RM
304 case AT_SECURE:
305 seen = -1;
306 __libc_enable_secure = av->a_un.a_val;
307 __libc_enable_secure_decided = 1;
308 break;
965cb60a
UD
309 case AT_RANDOM:
310 _dl_random = (void *) av->a_un.a_val;
311 break;
6c57d320
L
312 case AT_MINSIGSTKSZ:
313 _dl_minsigstacksize = av->a_un.a_val;
314 break;
47dbe62b 315 DL_PLATFORM_AUXV
f8f900ec 316 }
be4d8038
UD
317 if (seen == 0xf)
318 {
319 __libc_enable_secure = uid != 0 || gid != 0;
320 __libc_enable_secure_decided = 1;
321 }
e4a5f77d 322}
155fd00c 323#endif
f8f900ec 324
e4a5f77d 325
d417e0ff 326void
d417e0ff 327_dl_non_dynamic_init (void)
e4a5f77d 328{
f91f1c0f 329 _dl_main_map.l_origin = _dl_get_origin ();
0d23a5c1
MR
330 _dl_main_map.l_phdr = GL(dl_phdr);
331 _dl_main_map.l_phnum = GL(dl_phnum);
f91f1c0f 332
f8f900ec 333 _dl_verbose = *(getenv ("LD_WARN") ?: "") == '\0' ? 0 : 1;
e34b0f29 334
9cee5585
L
335 /* Set up the data structures for the system-supplied DSO early,
336 so they can influence _dl_init_paths. */
337 setup_vdso (NULL, NULL);
338
1bdda52f
AZ
339 /* With vDSO setup we can initialize the function pointers. */
340 setup_vdso_pointers ();
341
0a54e401
UD
342 /* Initialize the data structures for the search paths for shared
343 objects. */
dad90d52
FW
344 _dl_init_paths (getenv ("LD_LIBRARY_PATH"), "LD_LIBRARY_PATH",
345 /* No glibc-hwcaps selection support in statically
346 linked binaries. */
347 NULL, NULL);
0a54e401 348
bc5fb037
AS
349 /* Remember the last search directory added at startup. */
350 _dl_init_all_dirs = GL(dl_all_dirs);
351
12b5b6b7
UD
352 _dl_lazy = *(getenv ("LD_BIND_NOW") ?: "") == '\0';
353
f53c03c2
UD
354 _dl_bind_not = *(getenv ("LD_BIND_NOT") ?: "") != '\0';
355
dec126b4
UD
356 _dl_dynamic_weak = *(getenv ("LD_DYNAMIC_WEAK") ?: "") == '\0';
357
53bfdc1c
UD
358 _dl_profile_output = getenv ("LD_PROFILE_OUTPUT");
359 if (_dl_profile_output == NULL || _dl_profile_output[0] == '\0')
360 _dl_profile_output
361 = &"/var/tmp\0/var/profile"[__libc_enable_secure ? 9 : 0];
362
74955460
UD
363 if (__libc_enable_secure)
364 {
c4e328a1
UD
365 static const char unsecure_envvars[] =
366 UNSECURE_ENVVARS
74955460
UD
367#ifdef EXTRA_UNSECURE_ENVVARS
368 EXTRA_UNSECURE_ENVVARS
369#endif
c4e328a1
UD
370 ;
371 const char *cp = unsecure_envvars;
74955460 372
c4e328a1
UD
373 while (cp < unsecure_envvars + sizeof (unsecure_envvars))
374 {
375 __unsetenv (cp);
376 cp = (const char *) __rawmemchr (cp, '\0') + 1;
377 }
74955460 378
67e58f39 379#if !HAVE_TUNABLES
74955460 380 if (__access ("/etc/suid-debug", F_OK) != 0)
c4e328a1 381 __unsetenv ("MALLOC_CHECK_");
67e58f39 382#endif
74955460
UD
383 }
384
0a54e401
UD
385#ifdef DL_PLATFORM_INIT
386 DL_PLATFORM_INIT;
387#endif
388
2402cdf3
RM
389#ifdef DL_OSVERSION_INIT
390 DL_OSVERSION_INIT;
391#endif
392
0a54e401
UD
393 /* Now determine the length of the platform string. */
394 if (_dl_platform != NULL)
395 _dl_platformlen = strlen (_dl_platform);
ecdeaac0 396
ecdeaac0 397 if (_dl_phdr != NULL)
57013650
AZ
398 for (const ElfW(Phdr) *ph = _dl_phdr; ph < &_dl_phdr[_dl_phnum]; ++ph)
399 switch (ph->p_type)
ecdeaac0 400 {
57013650
AZ
401 /* Check if the stack is nonexecutable. */
402 case PT_GNU_STACK:
403 _dl_stack_flags = ph->p_flags;
404 break;
405
406 case PT_GNU_RELRO:
407 _dl_main_map.l_relro_addr = ph->p_vaddr;
408 _dl_main_map.l_relro_size = ph->p_memsz;
ecdeaac0
RM
409 break;
410 }
57013650
AZ
411
412 /* Setup relro on the binary itself. */
413 if (_dl_main_map.l_relro_size != 0)
414 _dl_protect_relro (&_dl_main_map);
b13927da 415}
d417e0ff 416
5e289179
UD
417#ifdef DL_SYSINFO_IMPLEMENTATION
418DL_SYSINFO_IMPLEMENTATION
419#endif
9d7a3741
L
420
421#if ENABLE_STATIC_PIE
422/* Since relocation to hidden _dl_main_map causes relocation overflow on
423 aarch64, a function is used to get the address of _dl_main_map. */
424
425struct link_map *
426_dl_get_dl_main_map (void)
427{
428 return &_dl_main_map;
429}
430#endif