1/* Support for dynamic linking code in static libc.
2 Copyright (C) 1996-2023 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/* We never do profiling. */
64const char *_dl_profile;
65const char *_dl_profile_output;
66
67/* Names of shared object for which the RUNPATHs and RPATHs should be
68 ignored. */
69const char *_dl_inhibit_rpath;
70
71/* The map for the object we will profile. */
72struct link_map *_dl_profile_map;
73
74/* This is the address of the last stack address ever used. */
75void *__libc_stack_end;
76
77/* Path where the binary is found. */
78const char *_dl_origin_path;
79
80/* Nonzero if runtime lookup should not update the .got/.plt. */
81int _dl_bind_not;
82
83/* A dummy link map for the executable, used by dlopen to access the global
84 scope. We don't export any symbols ourselves, so this can be minimal. */
85static struct link_map _dl_main_map =
86 {
87 .l_name = (char *) "",
88 .l_real = &_dl_main_map,
89 .l_ns = LM_ID_BASE,
90 .l_libname = &(struct libname_list) { .name = "", .dont_free = 1 },
91 .l_searchlist =
92 {
93 .r_list = &(struct link_map *) { &_dl_main_map },
94 .r_nlist = 1,
95 },
96 .l_symbolic_searchlist = { .r_list = &(struct link_map *) { NULL } },
97 .l_type = lt_executable,
98 .l_scope_mem = { &_dl_main_map.l_searchlist },
99 .l_scope_max = (sizeof (_dl_main_map.l_scope_mem)
100 / sizeof (_dl_main_map.l_scope_mem[0])),
101 .l_scope = _dl_main_map.l_scope_mem,
102 .l_local_scope = { &_dl_main_map.l_searchlist },
103 .l_used = 1,
104 .l_tls_offset = NO_TLS_OFFSET,
105 .l_serial = 1,
106 };
107
108/* Namespace information. */
109struct link_namespaces _dl_ns[DL_NNS] =
110 {
111 [LM_ID_BASE] =
112 {
113 ._ns_loaded = &_dl_main_map,
114 ._ns_nloaded = 1,
115 ._ns_main_searchlist = &_dl_main_map.l_searchlist,
116 }
117 };
118size_t _dl_nns = 1;
119
120/* Incremented whenever something may have been added to dl_loaded. */
121unsigned long long _dl_load_adds = 1;
122
123/* Fake scope of the main application. */
124struct r_scope_elem _dl_initial_searchlist =
125 {
126 .r_list = &(struct link_map *) { &_dl_main_map },
127 .r_nlist = 1,
128 };
129
130#ifndef HAVE_INLINED_SYSCALLS
131/* Nonzero during startup. */
132int _dl_starting_up = 1;
133#endif
134
135/* Random data provided by the kernel. */
136void *_dl_random;
137
138/* Get architecture specific initializer. */
139#include <dl-procruntime.c>
140#include <dl-procinfo.c>
141
142size_t _dl_pagesize = EXEC_PAGESIZE;
143
144size_t _dl_minsigstacksize = CONSTANT_MINSIGSTKSZ;
145
146int _dl_inhibit_cache;
147
148/* All known directories in sorted order. */
149struct r_search_path_elem *_dl_all_dirs;
150
151/* All directories after startup. */
152struct r_search_path_elem *_dl_init_all_dirs;
153
154/* The object to be initialized first. */
155struct link_map *_dl_initfirst;
156
157/* Descriptor to write debug messages to. */
158int _dl_debug_fd = STDERR_FILENO;
159
160ElfW(auxv_t) *_dl_auxv;
161const ElfW(Phdr) *_dl_phdr;
162size_t _dl_phnum;
163uint64_t _dl_hwcap;
164uint64_t _dl_hwcap2;
165
166enum dso_sort_algorithm _dl_dso_sort_algo;
167
168/* The value of the FPU control word the kernel will preset in hardware. */
169fpu_control_t _dl_fpu_control = _FPU_DEFAULT;
170
171#if !HAVE_TUNABLES
172/* This is not initialized to HWCAP_IMPORTANT, matching the definition
173 of _dl_important_hwcaps, below, where no hwcap strings are ever
174 used. This mask is still used to mediate the lookups in the cache
175 file. Since there is no way to set this nonzero (we don't grok the
176 LD_HWCAP_MASK environment variable here), there is no real point in
177 setting _dl_hwcap nonzero below, but we do anyway. */
178uint64_t _dl_hwcap_mask;
179#endif
180
181/* Prevailing state of the stack. Generally this includes PF_X, indicating it's
182 * executable but this isn't true for all platforms. */
183ElfW(Word) _dl_stack_flags = DEFAULT_STACK_PERMS;
184
185#if PTHREAD_IN_LIBC
186list_t _dl_stack_used;
187list_t _dl_stack_user;
188list_t _dl_stack_cache;
189size_t _dl_stack_cache_actsize;
190uintptr_t _dl_in_flight_stack;
191int _dl_stack_cache_lock;
192#else
193/* If loading a shared object requires that we make the stack executable
194 when it was not, we do it by calling this function.
195 It returns an errno code or zero on success. */
196int (*_dl_make_stack_executable_hook) (void **) = _dl_make_stack_executable;
197void (*_dl_init_static_tls) (struct link_map *) = &_dl_nothread_init_static_tls;
198#endif
199struct dl_scope_free_list *_dl_scope_free_list;
200
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. */
204uintptr_t _dl_sysinfo;
205#endif
206#ifdef NEED_DL_SYSINFO_DSO
207/* Address of the ELF headers in the vsyscall page. */
208const ElfW(Ehdr) *_dl_sysinfo_dso;
209
210struct link_map *_dl_sysinfo_map;
211
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>
217
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.
221
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)
230 /* This lock protects global and module specific TLS related data.
231 E.g. it is held in dlopen and dlclose when GL(dl_tls_generation),
232 GL(dl_tls_max_dtv_idx) or GL(dl_tls_dtv_slotinfo_list) are
233 accessed and when TLS related relocations are processed for a
234 module. It was introduced to keep pthread_create accessing TLS
235 state that is being set up. */
236__rtld_lock_define_initialized_recursive (, _dl_load_tls_lock)
237
238
239#ifdef HAVE_AUX_VECTOR
240#include <dl-parse_auxv.h>
241
242int _dl_clktck;
243
244void
245_dl_aux_init (ElfW(auxv_t) *av)
246{
247#ifdef NEED_DL_SYSINFO
248 /* NB: Avoid RELATIVE relocation in static PIE. */
249 GL(dl_sysinfo) = DL_SYSINFO_DEFAULT;
250#endif
251
252 _dl_auxv = av;
253 dl_parse_auxv_t auxv_values;
254 /* Use an explicit initialization loop here because memset may not
255 be available yet. */
256 for (int i = 0; i < array_length (auxv_values); ++i)
257 auxv_values[i] = 0;
258 _dl_parse_auxv (av, auxv_values);
259
260 _dl_phdr = (void*) auxv_values[AT_PHDR];
261 _dl_phnum = auxv_values[AT_PHNUM];
262
263 if (_dl_phdr == NULL)
264 {
265 /* Starting from binutils-2.23, the linker will define the
266 magic symbol __ehdr_start to point to our own ELF header
267 if it is visible in a segment that also includes the phdrs.
268 So we can set up _dl_phdr and _dl_phnum even without any
269 information from auxv. */
270
271 extern const ElfW(Ehdr) __ehdr_start attribute_hidden;
272 assert (__ehdr_start.e_phentsize == sizeof *GL(dl_phdr));
273 _dl_phdr = (const void *) &__ehdr_start + __ehdr_start.e_phoff;
274 _dl_phnum = __ehdr_start.e_phnum;
275 }
276
277 assert (_dl_phdr != NULL);
278}
279#endif
280
281
282void
283_dl_non_dynamic_init (void)
284{
285 _dl_main_map.l_origin = _dl_get_origin ();
286 _dl_main_map.l_phdr = GL(dl_phdr);
287 _dl_main_map.l_phnum = GL(dl_phnum);
288
289 _dl_verbose = *(getenv ("LD_WARN") ?: "") == '\0' ? 0 : 1;
290
291 /* Set up the data structures for the system-supplied DSO early,
292 so they can influence _dl_init_paths. */
293 setup_vdso (NULL, NULL);
294
295 /* With vDSO setup we can initialize the function pointers. */
296 setup_vdso_pointers ();
297
298 /* Initialize the data structures for the search paths for shared
299 objects. */
300 _dl_init_paths (getenv ("LD_LIBRARY_PATH"), "LD_LIBRARY_PATH",
301 /* No glibc-hwcaps selection support in statically
302 linked binaries. */
303 NULL, NULL);
304
305 /* Remember the last search directory added at startup. */
306 _dl_init_all_dirs = GL(dl_all_dirs);
307
308 _dl_lazy = *(getenv ("LD_BIND_NOW") ?: "") == '\0';
309
310 _dl_bind_not = *(getenv ("LD_BIND_NOT") ?: "") != '\0';
311
312 _dl_dynamic_weak = *(getenv ("LD_DYNAMIC_WEAK") ?: "") == '\0';
313
314 _dl_profile_output = getenv ("LD_PROFILE_OUTPUT");
315 if (_dl_profile_output == NULL || _dl_profile_output[0] == '\0')
316 _dl_profile_output
317 = &"/var/tmp\0/var/profile"[__libc_enable_secure ? 9 : 0];
318
319 if (__libc_enable_secure)
320 {
321 static const char unsecure_envvars[] =
322 UNSECURE_ENVVARS
323 ;
324 const char *cp = unsecure_envvars;
325
326 while (cp < unsecure_envvars + sizeof (unsecure_envvars))
327 {
328 __unsetenv (cp);
329 cp = (const char *) __rawmemchr (cp, '\0') + 1;
330 }
331
332#if !HAVE_TUNABLES
333 if (__access ("/etc/suid-debug", F_OK) != 0)
334 __unsetenv ("MALLOC_CHECK_");
335#endif
336 }
337
338#ifdef DL_PLATFORM_INIT
339 DL_PLATFORM_INIT;
340#endif
341
342 /* Now determine the length of the platform string. */
343 if (_dl_platform != NULL)
344 _dl_platformlen = strlen (_dl_platform);
345
346 for (const ElfW(Phdr) *ph = _dl_phdr; ph < &_dl_phdr[_dl_phnum]; ++ph)
347 switch (ph->p_type)
348 {
349 /* Check if the stack is nonexecutable. */
350 case PT_GNU_STACK:
351 _dl_stack_flags = ph->p_flags;
352 break;
353
354 case PT_GNU_RELRO:
355 _dl_main_map.l_relro_addr = ph->p_vaddr;
356 _dl_main_map.l_relro_size = ph->p_memsz;
357 break;
358 }
359
360 call_function_static_weak (_dl_find_object_init);
361
362 /* Setup relro on the binary itself. */
363 if (_dl_main_map.l_relro_size != 0)
364 _dl_protect_relro (&_dl_main_map);
365}
366
367#ifdef DL_SYSINFO_IMPLEMENTATION
368DL_SYSINFO_IMPLEMENTATION
369#endif
370
371#if ENABLE_STATIC_PIE
372/* Since relocation to hidden _dl_main_map causes relocation overflow on
373 aarch64, a function is used to get the address of _dl_main_map. */
374
375struct link_map *
376_dl_get_dl_main_map (void)
377{
378 return &_dl_main_map;
379}
380#endif
381
382/* This is used by _dl_runtime_profile, not used on static code. */
383void
384DL_ARCH_FIXUP_ATTRIBUTE
385_dl_audit_pltexit (struct link_map *l, ElfW(Word) reloc_arg,
386 const void *inregs, void *outregs)
387{
388}
389