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

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