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