Update INSTALL with package versions that are known to work
[glibc.git] / elf / dl-support.c
blob7abb65d8e393d9ee9c7ec3f1409a15f4643c5402
1 /* Support for dynamic linking code in static libc.
2 Copyright (C) 1996-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 /* 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. */
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
28 #include <errno.h>
29 #include <libintl.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <sys/param.h>
33 #include <stdint.h>
34 #include <ldsodefs.h>
35 #include <dl-machine.h>
36 #include <libc-lock.h>
37 #include <dl-cache.h>
38 #include <dl-librecon.h>
39 #include <dl-procinfo.h>
40 #include <unsecvars.h>
41 #include <hp-timing.h>
42 #include <stackinfo.h>
43 #include <dl-vdso.h>
44 #include <dl-vdso-setup.h>
45 #include <dl-auxv.h>
47 extern char *__progname;
48 char **_dl_argv = &__progname; /* This is checked for some error messages. */
50 /* Name of the architecture. */
51 const char *_dl_platform;
52 size_t _dl_platformlen;
54 int _dl_debug_mask;
55 int _dl_lazy;
56 ElfW(Addr) _dl_use_load_bias = -2;
57 int _dl_dynamic_weak;
59 /* If nonzero print warnings about problematic situations. */
60 int _dl_verbose;
62 /* We never do profiling. */
63 const char *_dl_profile;
64 const char *_dl_profile_output;
66 /* Names of shared object for which the RUNPATHs and RPATHs should be
67 ignored. */
68 const char *_dl_inhibit_rpath;
70 /* The map for the object we will profile. */
71 struct link_map *_dl_profile_map;
73 /* This is the address of the last stack address ever used. */
74 void *__libc_stack_end;
76 /* Path where the binary is found. */
77 const char *_dl_origin_path;
79 /* Nonzero if runtime lookup should not update the .got/.plt. */
80 int _dl_bind_not;
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. */
84 static struct link_map _dl_main_map =
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 =
92 .r_list = &(struct link_map *) { &_dl_main_map },
93 .r_nlist = 1,
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,
107 /* Namespace information. */
108 struct link_namespaces _dl_ns[DL_NNS] =
110 [LM_ID_BASE] =
112 ._ns_loaded = &_dl_main_map,
113 ._ns_nloaded = 1,
114 ._ns_main_searchlist = &_dl_main_map.l_searchlist,
117 size_t _dl_nns = 1;
119 /* Incremented whenever something may have been added to dl_loaded. */
120 unsigned long long _dl_load_adds = 1;
122 /* Fake scope of the main application. */
123 struct r_scope_elem _dl_initial_searchlist =
125 .r_list = &(struct link_map *) { &_dl_main_map },
126 .r_nlist = 1,
129 #ifndef HAVE_INLINED_SYSCALLS
130 /* Nonzero during startup. */
131 int _dl_starting_up = 1;
132 #endif
134 /* Random data provided by the kernel. */
135 void *_dl_random;
137 /* Get architecture specific initializer. */
138 #include <dl-procruntime.c>
139 #include <dl-procinfo.c>
141 void (*_dl_init_static_tls) (struct link_map *) = &_dl_nothread_init_static_tls;
143 size_t _dl_pagesize = EXEC_PAGESIZE;
145 int _dl_inhibit_cache;
147 unsigned int _dl_osversion;
149 /* All known directories in sorted order. */
150 struct r_search_path_elem *_dl_all_dirs;
152 /* All directories after startup. */
153 struct r_search_path_elem *_dl_init_all_dirs;
155 /* The object to be initialized first. */
156 struct link_map *_dl_initfirst;
158 /* Descriptor to write debug messages to. */
159 int _dl_debug_fd = STDERR_FILENO;
161 int _dl_correct_cache_id = _DL_CACHE_DEFAULT_ID;
163 ElfW(auxv_t) *_dl_auxv;
164 const ElfW(Phdr) *_dl_phdr;
165 size_t _dl_phnum;
166 uint64_t _dl_hwcap __attribute__ ((nocommon));
167 uint64_t _dl_hwcap2 __attribute__ ((nocommon));
169 /* The value of the FPU control word the kernel will preset in hardware. */
170 fpu_control_t _dl_fpu_control = _FPU_DEFAULT;
172 #if !HAVE_TUNABLES
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. */
179 uint64_t _dl_hwcap_mask __attribute__ ((nocommon));
180 #endif
182 /* Prevailing state of the stack. Generally this includes PF_X, indicating it's
183 * executable but this isn't true for all platforms. */
184 ElfW(Word) _dl_stack_flags = DEFAULT_STACK_PERMS;
186 /* If loading a shared object requires that we make the stack executable
187 when it was not, we do it by calling this function.
188 It returns an errno code or zero on success. */
189 int (*_dl_make_stack_executable_hook) (void **) = _dl_make_stack_executable;
192 #if THREAD_GSCOPE_IN_TCB
193 list_t _dl_stack_used;
194 list_t _dl_stack_user;
195 int _dl_stack_cache_lock;
196 #else
197 int _dl_thread_gscope_count;
198 #endif
199 struct dl_scope_free_list *_dl_scope_free_list;
201 #ifdef NEED_DL_SYSINFO
202 /* Needed for improved syscall handling on at least x86/Linux. NB: Don't
203 initialize it here to avoid RELATIVE relocation in static PIE. */
204 uintptr_t _dl_sysinfo;
205 #endif
206 #ifdef NEED_DL_SYSINFO_DSO
207 /* Address of the ELF headers in the vsyscall page. */
208 const ElfW(Ehdr) *_dl_sysinfo_dso;
210 struct link_map *_dl_sysinfo_map;
212 # include "get-dynamic-info.h"
213 #endif
214 #include "setup-vdso.h"
215 /* Define the vDSO function pointers. */
216 #include <dl-vdso-setup.c>
218 /* During the program run we must not modify the global data of
219 loaded shared object simultanously in two threads. Therefore we
220 protect `_dl_open' and `_dl_close' in dl-close.c.
222 This must be a recursive lock since the initializer function of
223 the loaded object might as well require a call to this function.
224 At this time it is not anymore a problem to modify the tables. */
225 __rtld_lock_define_initialized_recursive (, _dl_load_lock)
226 /* This lock is used to keep __dl_iterate_phdr from inspecting the
227 list of loaded objects while an object is added to or removed from
228 that list. */
229 __rtld_lock_define_initialized_recursive (, _dl_load_write_lock)
232 #ifdef HAVE_AUX_VECTOR
233 int _dl_clktck;
235 void
236 _dl_aux_init (ElfW(auxv_t) *av)
238 int seen = 0;
239 uid_t uid = 0;
240 gid_t gid = 0;
242 #ifdef NEED_DL_SYSINFO
243 /* NB: Avoid RELATIVE relocation in static PIE. */
244 GL(dl_sysinfo) = DL_SYSINFO_DEFAULT;
245 #endif
247 _dl_auxv = av;
248 for (; av->a_type != AT_NULL; ++av)
249 switch (av->a_type)
251 case AT_PAGESZ:
252 if (av->a_un.a_val != 0)
253 GLRO(dl_pagesize) = av->a_un.a_val;
254 break;
255 case AT_CLKTCK:
256 GLRO(dl_clktck) = av->a_un.a_val;
257 break;
258 case AT_PHDR:
259 GL(dl_phdr) = (const void *) av->a_un.a_val;
260 break;
261 case AT_PHNUM:
262 GL(dl_phnum) = av->a_un.a_val;
263 break;
264 case AT_PLATFORM:
265 GLRO(dl_platform) = (void *) av->a_un.a_val;
266 break;
267 case AT_HWCAP:
268 GLRO(dl_hwcap) = (unsigned long int) av->a_un.a_val;
269 break;
270 case AT_HWCAP2:
271 GLRO(dl_hwcap2) = (unsigned long int) av->a_un.a_val;
272 break;
273 case AT_FPUCW:
274 GLRO(dl_fpu_control) = av->a_un.a_val;
275 break;
276 #ifdef NEED_DL_SYSINFO
277 case AT_SYSINFO:
278 GL(dl_sysinfo) = av->a_un.a_val;
279 break;
280 #endif
281 #ifdef NEED_DL_SYSINFO_DSO
282 case AT_SYSINFO_EHDR:
283 GL(dl_sysinfo_dso) = (void *) av->a_un.a_val;
284 break;
285 #endif
286 case AT_UID:
287 uid ^= av->a_un.a_val;
288 seen |= 1;
289 break;
290 case AT_EUID:
291 uid ^= av->a_un.a_val;
292 seen |= 2;
293 break;
294 case AT_GID:
295 gid ^= av->a_un.a_val;
296 seen |= 4;
297 break;
298 case AT_EGID:
299 gid ^= av->a_un.a_val;
300 seen |= 8;
301 break;
302 case AT_SECURE:
303 seen = -1;
304 __libc_enable_secure = av->a_un.a_val;
305 __libc_enable_secure_decided = 1;
306 break;
307 case AT_RANDOM:
308 _dl_random = (void *) av->a_un.a_val;
309 break;
310 DL_PLATFORM_AUXV
312 if (seen == 0xf)
314 __libc_enable_secure = uid != 0 || gid != 0;
315 __libc_enable_secure_decided = 1;
318 #endif
321 void
322 _dl_non_dynamic_init (void)
324 _dl_main_map.l_origin = _dl_get_origin ();
325 _dl_main_map.l_phdr = GL(dl_phdr);
326 _dl_main_map.l_phnum = GL(dl_phnum);
328 _dl_verbose = *(getenv ("LD_WARN") ?: "") == '\0' ? 0 : 1;
330 /* Set up the data structures for the system-supplied DSO early,
331 so they can influence _dl_init_paths. */
332 setup_vdso (NULL, NULL);
334 /* With vDSO setup we can initialize the function pointers. */
335 setup_vdso_pointers ();
337 /* Initialize the data structures for the search paths for shared
338 objects. */
339 _dl_init_paths (getenv ("LD_LIBRARY_PATH"), "LD_LIBRARY_PATH",
340 /* No glibc-hwcaps selection support in statically
341 linked binaries. */
342 NULL, NULL);
344 /* Remember the last search directory added at startup. */
345 _dl_init_all_dirs = GL(dl_all_dirs);
347 _dl_lazy = *(getenv ("LD_BIND_NOW") ?: "") == '\0';
349 _dl_bind_not = *(getenv ("LD_BIND_NOT") ?: "") != '\0';
351 _dl_dynamic_weak = *(getenv ("LD_DYNAMIC_WEAK") ?: "") == '\0';
353 _dl_profile_output = getenv ("LD_PROFILE_OUTPUT");
354 if (_dl_profile_output == NULL || _dl_profile_output[0] == '\0')
355 _dl_profile_output
356 = &"/var/tmp\0/var/profile"[__libc_enable_secure ? 9 : 0];
358 if (__libc_enable_secure)
360 static const char unsecure_envvars[] =
361 UNSECURE_ENVVARS
362 #ifdef EXTRA_UNSECURE_ENVVARS
363 EXTRA_UNSECURE_ENVVARS
364 #endif
366 const char *cp = unsecure_envvars;
368 while (cp < unsecure_envvars + sizeof (unsecure_envvars))
370 __unsetenv (cp);
371 cp = (const char *) __rawmemchr (cp, '\0') + 1;
374 #if !HAVE_TUNABLES
375 if (__access ("/etc/suid-debug", F_OK) != 0)
376 __unsetenv ("MALLOC_CHECK_");
377 #endif
380 #ifdef DL_PLATFORM_INIT
381 DL_PLATFORM_INIT;
382 #endif
384 #ifdef DL_OSVERSION_INIT
385 DL_OSVERSION_INIT;
386 #endif
388 /* Now determine the length of the platform string. */
389 if (_dl_platform != NULL)
390 _dl_platformlen = strlen (_dl_platform);
392 if (_dl_phdr != NULL)
393 for (const ElfW(Phdr) *ph = _dl_phdr; ph < &_dl_phdr[_dl_phnum]; ++ph)
394 switch (ph->p_type)
396 /* Check if the stack is nonexecutable. */
397 case PT_GNU_STACK:
398 _dl_stack_flags = ph->p_flags;
399 break;
401 case PT_GNU_RELRO:
402 _dl_main_map.l_relro_addr = ph->p_vaddr;
403 _dl_main_map.l_relro_size = ph->p_memsz;
404 break;
407 /* Setup relro on the binary itself. */
408 if (_dl_main_map.l_relro_size != 0)
409 _dl_protect_relro (&_dl_main_map);
412 #ifdef DL_SYSINFO_IMPLEMENTATION
413 DL_SYSINFO_IMPLEMENTATION
414 #endif
416 #if ENABLE_STATIC_PIE
417 /* Since relocation to hidden _dl_main_map causes relocation overflow on
418 aarch64, a function is used to get the address of _dl_main_map. */
420 struct link_map *
421 _dl_get_dl_main_map (void)
423 return &_dl_main_map;
425 #endif