1/* Support for dynamic linking code in static libc.
2 Copyright (C) 1996-2024 Free Software Foundation, Inc.
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
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.
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
13 Lesser General Public License for more details.
14
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/>. */
18
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
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-procinfo.h>
39#include <unsecvars.h>
40#include <hp-timing.h>
41#include <stackinfo.h>
42#include <dl-vdso.h>
43#include <dl-vdso-setup.h>
44#include <dl-auxv.h>
45#include <dl-find_object.h>
46#include <array_length.h>
47#include <dl-symbol-redir-ifunc.h>
48
49extern char *__progname;
50char **_dl_argv = &__progname; /* This is checked for some error messages. */
51
52/* Name of the architecture. */
53const char *_dl_platform;
54size_t _dl_platformlen;
55
56int _dl_debug_mask;
57int _dl_lazy;
58int _dl_dynamic_weak;
59
60/* If nonzero print warnings about problematic situations. */
61int _dl_verbose;
62
63/* Names of shared object for which the RUNPATHs and RPATHs should be
64 ignored. */
65const char *_dl_inhibit_rpath;
66
67/* The map for the object we will profile. */
68struct link_map *_dl_profile_map;
69
70/* This is the address of the last stack address ever used. */
71void *__libc_stack_end;
72
73/* Path where the binary is found. */
74const char *_dl_origin_path;
75
76/* Nonzero if runtime lookup should not update the .got/.plt. */
77int _dl_bind_not;
78
79/* A dummy link map for the executable, used by dlopen to access the global
80 scope. We don't export any symbols ourselves, so this can be minimal. */
81static struct link_map _dl_main_map =
82 {
83 .l_name = (char *) "",
84 .l_real = &_dl_main_map,
85 .l_ns = LM_ID_BASE,
86 .l_libname = &(struct libname_list) { .name = "", .dont_free = 1 },
87 .l_searchlist =
88 {
89 .r_list = &(struct link_map *) { &_dl_main_map },
90 .r_nlist = 1,
91 },
92 .l_symbolic_searchlist = { .r_list = &(struct link_map *) { NULL } },
93 .l_type = lt_executable,
94 .l_scope_mem = { &_dl_main_map.l_searchlist },
95 .l_scope_max = (sizeof (_dl_main_map.l_scope_mem)
96 / sizeof (_dl_main_map.l_scope_mem[0])),
97 .l_scope = _dl_main_map.l_scope_mem,
98 .l_local_scope = { &_dl_main_map.l_searchlist },
99 .l_used = 1,
100 .l_tls_offset = NO_TLS_OFFSET,
101 .l_serial = 1,
102 };
103
104/* Namespace information. */
105struct link_namespaces _dl_ns[DL_NNS] =
106 {
107 [LM_ID_BASE] =
108 {
109 ._ns_loaded = &_dl_main_map,
110 ._ns_nloaded = 1,
111 ._ns_main_searchlist = &_dl_main_map.l_searchlist,
112 }
113 };
114size_t _dl_nns = 1;
115
116/* Incremented whenever something may have been added to dl_loaded. */
117unsigned long long _dl_load_adds = 1;
118
119/* Fake scope of the main application. */
120struct r_scope_elem _dl_initial_searchlist =
121 {
122 .r_list = &(struct link_map *) { &_dl_main_map },
123 .r_nlist = 1,
124 };
125
126#ifndef HAVE_INLINED_SYSCALLS
127/* Nonzero during startup. */
128int _dl_starting_up = 1;
129#endif
130
131/* Random data provided by the kernel. */
132void *_dl_random;
133
134/* Get architecture specific initializer. */
135#include <dl-procruntime.c>
136#include <dl-procinfo.c>
137
138size_t _dl_pagesize = EXEC_PAGESIZE;
139
140size_t _dl_minsigstacksize = CONSTANT_MINSIGSTKSZ;
141
142int _dl_inhibit_cache;
143
144/* All known directories in sorted order. */
145struct r_search_path_elem *_dl_all_dirs;
146
147/* All directories after startup. */
148struct r_search_path_elem *_dl_init_all_dirs;
149
150/* The object to be initialized first. */
151struct link_map *_dl_initfirst;
152
153/* Descriptor to write debug messages to. */
154int _dl_debug_fd = STDERR_FILENO;
155
156ElfW(auxv_t) *_dl_auxv;
157const ElfW(Phdr) *_dl_phdr;
158size_t _dl_phnum;
159uint64_t _dl_hwcap;
160uint64_t _dl_hwcap2;
161uint64_t _dl_hwcap3;
162uint64_t _dl_hwcap4;
163
164enum dso_sort_algorithm _dl_dso_sort_algo;
165
166/* The value of the FPU control word the kernel will preset in hardware. */
167fpu_control_t _dl_fpu_control = _FPU_DEFAULT;
168
169/* Prevailing state of the stack. Generally this includes PF_X, indicating it's
170 * executable but this isn't true for all platforms. */
171ElfW(Word) _dl_stack_flags = DEFAULT_STACK_PERMS;
172
173#if PTHREAD_IN_LIBC
174list_t _dl_stack_used;
175list_t _dl_stack_user;
176list_t _dl_stack_cache;
177size_t _dl_stack_cache_actsize;
178uintptr_t _dl_in_flight_stack;
179int _dl_stack_cache_lock;
180#else
181/* If loading a shared object requires that we make the stack executable
182 when it was not, we do it by calling this function.
183 It returns an errno code or zero on success. */
184int (*_dl_make_stack_executable_hook) (void **) = _dl_make_stack_executable;
185void (*_dl_init_static_tls) (struct link_map *) = &_dl_nothread_init_static_tls;
186#endif
187struct dl_scope_free_list *_dl_scope_free_list;
188
189#ifdef NEED_DL_SYSINFO
190/* Needed for improved syscall handling on at least x86/Linux. NB: Don't
191 initialize it here to avoid RELATIVE relocation in static PIE. */
192uintptr_t _dl_sysinfo;
193#endif
194#ifdef NEED_DL_SYSINFO_DSO
195/* Address of the ELF headers in the vsyscall page. */
196const ElfW(Ehdr) *_dl_sysinfo_dso;
197
198struct link_map *_dl_sysinfo_map;
199
200# include "get-dynamic-info.h"
201#endif
202#include "setup-vdso.h"
203/* Define the vDSO function pointers. */
204#include <dl-vdso-setup.c>
205
206/* During the program run we must not modify the global data of
207 loaded shared object simultaneously in two threads. Therefore we
208 protect `_dl_open' and `_dl_close' in dl-close.c.
209
210 This must be a recursive lock since the initializer function of
211 the loaded object might as well require a call to this function.
212 At this time it is not anymore a problem to modify the tables. */
213__rtld_lock_define_initialized_recursive (, _dl_load_lock)
214/* This lock is used to keep __dl_iterate_phdr from inspecting the
215 list of loaded objects while an object is added to or removed from
216 that list. */
217__rtld_lock_define_initialized_recursive (, _dl_load_write_lock)
218 /* This lock protects global and module specific TLS related data.
219 E.g. it is held in dlopen and dlclose when GL(dl_tls_generation),
220 GL(dl_tls_max_dtv_idx) or GL(dl_tls_dtv_slotinfo_list) are
221 accessed and when TLS related relocations are processed for a
222 module. It was introduced to keep pthread_create accessing TLS
223 state that is being set up. */
224__rtld_lock_define_initialized_recursive (, _dl_load_tls_lock)
225
226
227#ifdef HAVE_AUX_VECTOR
228#include <dl-parse_auxv.h>
229
230int _dl_clktck;
231
232void
233_dl_aux_init (ElfW(auxv_t) *av)
234{
235#ifdef NEED_DL_SYSINFO
236 /* NB: Avoid RELATIVE relocation in static PIE. */
237 GL(dl_sysinfo) = DL_SYSINFO_DEFAULT;
238#endif
239
240 _dl_auxv = av;
241 dl_parse_auxv_t auxv_values;
242 /* Use an explicit initialization loop here because memset may not
243 be available yet. */
244 for (int i = 0; i < array_length (auxv_values); ++i)
245 auxv_values[i] = 0;
246 _dl_parse_auxv (av, auxv_values);
247
248 _dl_phdr = (void*) auxv_values[AT_PHDR];
249 _dl_phnum = auxv_values[AT_PHNUM];
250
251 if (_dl_phdr == NULL)
252 {
253 /* Starting from binutils-2.23, the linker will define the
254 magic symbol __ehdr_start to point to our own ELF header
255 if it is visible in a segment that also includes the phdrs.
256 So we can set up _dl_phdr and _dl_phnum even without any
257 information from auxv. */
258
259 extern const ElfW(Ehdr) __ehdr_start attribute_hidden;
260 assert (__ehdr_start.e_phentsize == sizeof *GL(dl_phdr));
261 _dl_phdr = (const void *) &__ehdr_start + __ehdr_start.e_phoff;
262 _dl_phnum = __ehdr_start.e_phnum;
263 }
264
265 assert (_dl_phdr != NULL);
266}
267#endif
268
269
270void
271_dl_non_dynamic_init (void)
272{
273 _dl_main_map.l_origin = _dl_get_origin ();
274 _dl_main_map.l_phdr = GL(dl_phdr);
275 _dl_main_map.l_phnum = GL(dl_phnum);
276
277 /* Set up the data structures for the system-supplied DSO early,
278 so they can influence _dl_init_paths. */
279 setup_vdso (NULL, NULL);
280
281 /* With vDSO setup we can initialize the function pointers. */
282 setup_vdso_pointers ();
283
284 if (__libc_enable_secure)
285 {
286 static const char unsecure_envvars[] =
287 UNSECURE_ENVVARS
288 ;
289 const char *cp = unsecure_envvars;
290
291 while (cp < unsecure_envvars + sizeof (unsecure_envvars))
292 {
293 __unsetenv (name: cp);
294 cp = strchr (cp, '\0') + 1;
295 }
296 }
297
298 _dl_verbose = *(getenv ("LD_WARN") ?: "") == '\0' ? 0 : 1;
299
300 /* Initialize the data structures for the search paths for shared
301 objects. */
302 _dl_init_paths (library_path: getenv ("LD_LIBRARY_PATH"), source: "LD_LIBRARY_PATH",
303 /* No glibc-hwcaps selection support in statically
304 linked binaries. */
305 NULL, NULL);
306
307 /* Remember the last search directory added at startup. */
308 _dl_init_all_dirs = GL(dl_all_dirs);
309
310 _dl_lazy = *(getenv ("LD_BIND_NOW") ?: "") == '\0';
311
312 _dl_bind_not = *(getenv ("LD_BIND_NOT") ?: "") != '\0';
313
314 _dl_dynamic_weak = *(getenv ("LD_DYNAMIC_WEAK") ?: "") == '\0';
315
316#ifdef DL_PLATFORM_INIT
317 DL_PLATFORM_INIT;
318#endif
319
320 /* Now determine the length of the platform string. */
321 if (_dl_platform != NULL)
322 _dl_platformlen = strlen (_dl_platform);
323
324 for (const ElfW(Phdr) *ph = _dl_phdr; ph < &_dl_phdr[_dl_phnum]; ++ph)
325 switch (ph->p_type)
326 {
327 /* Check if the stack is nonexecutable. */
328 case PT_GNU_STACK:
329 _dl_stack_flags = ph->p_flags;
330 break;
331
332 case PT_GNU_RELRO:
333 _dl_main_map.l_relro_addr = ph->p_vaddr;
334 _dl_main_map.l_relro_size = ph->p_memsz;
335 break;
336 }
337
338 call_function_static_weak (_dl_find_object_init);
339
340 /* Setup relro on the binary itself. */
341 if (_dl_main_map.l_relro_size != 0)
342 _dl_protect_relro (map: &_dl_main_map);
343}
344
345#ifdef DL_SYSINFO_IMPLEMENTATION
346DL_SYSINFO_IMPLEMENTATION
347#endif
348
349/* Since relocation to hidden _dl_main_map causes relocation overflow on
350 aarch64, a function is used to get the address of _dl_main_map. */
351
352struct link_map *
353_dl_get_dl_main_map (void)
354{
355 return &_dl_main_map;
356}
357
358/* This is used by _dl_runtime_profile, not used on static code. */
359void
360DL_ARCH_FIXUP_ATTRIBUTE
361_dl_audit_pltexit (struct link_map *l, ElfW(Word) reloc_arg,
362 const void *inregs, void *outregs)
363{
364}
365

source code of glibc/elf/dl-support.c