]>
Commit | Line | Data |
---|---|---|
d66e34cd | 1 | /* Run time dynamic linker. |
581c785b | 2 | Copyright (C) 1995-2022 Free Software Foundation, Inc. |
afd4eb37 | 3 | This file is part of the GNU C Library. |
d66e34cd | 4 | |
afd4eb37 | 5 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
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. | |
d66e34cd | 9 | |
afd4eb37 UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
d66e34cd | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
d66e34cd | 18 | |
7d0b1164 | 19 | #include <errno.h> |
154d10bd | 20 | #include <dlfcn.h> |
7dea968e | 21 | #include <fcntl.h> |
164a7164 | 22 | #include <stdbool.h> |
d66e34cd | 23 | #include <stdlib.h> |
f51d1dfd | 24 | #include <string.h> |
d66e34cd | 25 | #include <unistd.h> |
20739e54 | 26 | #include <sys/mman.h> |
af8bf6bd | 27 | #include <sys/param.h> |
ba9fcb3f | 28 | #include <sys/stat.h> |
a42195db | 29 | #include <ldsodefs.h> |
eb96ffb0 | 30 | #include <_itoa.h> |
f21acc89 | 31 | #include <entry.h> |
c94a8080 | 32 | #include <fpu_control.h> |
db276fa1 | 33 | #include <hp-timing.h> |
ec999b8e | 34 | #include <libc-lock.h> |
8f480b4b | 35 | #include <dl-librecon.h> |
74955460 | 36 | #include <unsecvars.h> |
5688da55 | 37 | #include <dl-cache.h> |
2f4db0df | 38 | #include <dl-osinfo.h> |
5688da55 | 39 | #include <dl-procinfo.h> |
f753fa7d | 40 | #include <dl-prop.h> |
1bdda52f AZ |
41 | #include <dl-vdso.h> |
42 | #include <dl-vdso-setup.h> | |
5f5843e3 | 43 | #include <tls.h> |
815e6fa3 | 44 | #include <stap-probe.h> |
30950a5f | 45 | #include <stackinfo.h> |
329ea513 | 46 | #include <not-cancel.h> |
4c6e0415 | 47 | #include <array_length.h> |
ec935dea | 48 | #include <libc-early-init.h> |
2bf9e641 | 49 | #include <dl-main.h> |
4d098554 | 50 | #include <gnu/lib-names.h> |
86f65dff | 51 | #include <dl-tunables.h> |
d6d89608 | 52 | #include <get-dynamic-info.h> |
c1cb2dee | 53 | #include <dl-execve.h> |
5d28a896 | 54 | #include <dl-find_object.h> |
f5348425 | 55 | |
a853022c | 56 | #include <assert.h> |
f5348425 | 57 | |
d6d89608 AZ |
58 | /* This #define produces dynamic linking inline functions for |
59 | bootstrap relocation instead of general-purpose relocation. | |
60 | Since ld.so must not have any undefined symbols the result | |
61 | is trivial: always the map of ld.so itself. */ | |
62 | #define RTLD_BOOTSTRAP | |
63 | #define RESOLVE_MAP(map, scope, sym, version, flags) map | |
64 | #include "dynamic-link.h" | |
65 | ||
9288c92d L |
66 | /* Must include after <dl-machine.h> for DT_MIPS definition. */ |
67 | #include <dl-debug.h> | |
68 | ||
1e372ded AZ |
69 | /* Only enables rtld profiling for architectures which provides non generic |
70 | hp-timing support. The generic support requires either syscall | |
71 | (clock_gettime), which will incur in extra overhead on loading time. | |
72 | Using vDSO is also an option, but it will require extra support on loader | |
73 | to setup the vDSO pointer before its usage. */ | |
74 | #if HP_TIMING_INLINE | |
75 | # define RLTD_TIMING_DECLARE(var, classifier,...) \ | |
76 | classifier hp_timing_t var __VA_ARGS__ | |
77 | # define RTLD_TIMING_VAR(var) RLTD_TIMING_DECLARE (var, ) | |
78 | # define RTLD_TIMING_SET(var, value) (var) = (value) | |
79 | # define RTLD_TIMING_REF(var) &(var) | |
80 | ||
81 | static inline void | |
82 | rtld_timer_start (hp_timing_t *var) | |
83 | { | |
84 | HP_TIMING_NOW (*var); | |
85 | } | |
86 | ||
87 | static inline void | |
88 | rtld_timer_stop (hp_timing_t *var, hp_timing_t start) | |
89 | { | |
90 | hp_timing_t stop; | |
91 | HP_TIMING_NOW (stop); | |
92 | HP_TIMING_DIFF (*var, start, stop); | |
93 | } | |
94 | ||
95 | static inline void | |
96 | rtld_timer_accum (hp_timing_t *sum, hp_timing_t start) | |
97 | { | |
98 | hp_timing_t stop; | |
99 | rtld_timer_stop (&stop, start); | |
100 | HP_TIMING_ACCUM_NT(*sum, stop); | |
101 | } | |
102 | #else | |
103 | # define RLTD_TIMING_DECLARE(var, classifier...) | |
104 | # define RTLD_TIMING_SET(var, value) | |
105 | # define RTLD_TIMING_VAR(var) | |
106 | # define RTLD_TIMING_REF(var) 0 | |
107 | # define rtld_timer_start(var) | |
108 | # define rtld_timer_stop(var, start) | |
109 | # define rtld_timer_accum(sum, start) | |
110 | #endif | |
111 | ||
6ce3881d RM |
112 | /* Avoid PLT use for our local calls at startup. */ |
113 | extern __typeof (__mempcpy) __mempcpy attribute_hidden; | |
114 | ||
115 | /* GCC has mental blocks about _exit. */ | |
116 | extern __typeof (_exit) exit_internal asm ("_exit") attribute_hidden; | |
117 | #define _exit exit_internal | |
118 | ||
fd26970f | 119 | /* Helper function to handle errors while resolving symbols. */ |
c84142e8 UD |
120 | static void print_unresolved (int errcode, const char *objname, |
121 | const char *errsting); | |
122 | ||
123 | /* Helper function to handle errors when a version is missing. */ | |
124 | static void print_missing_version (int errcode, const char *objname, | |
125 | const char *errsting); | |
fd26970f | 126 | |
db276fa1 | 127 | /* Print the various times we collected. */ |
1e372ded | 128 | static void print_statistics (const hp_timing_t *total_timep); |
ea278354 | 129 | |
4c6e0415 FW |
130 | /* Creates an empty audit list. */ |
131 | static void audit_list_init (struct audit_list *); | |
132 | ||
133 | /* Add a string to the end of the audit list, for later parsing. Must | |
134 | not be called after audit_list_next. */ | |
135 | static void audit_list_add_string (struct audit_list *, const char *); | |
136 | ||
8f7a75d7 FW |
137 | /* Add the audit strings from the link map, found in the dynamic |
138 | segment at TG (either DT_AUDIT and DT_DEPAUDIT). Must be called | |
139 | before audit_list_next. */ | |
140 | static void audit_list_add_dynamic_tag (struct audit_list *, | |
141 | struct link_map *, | |
142 | unsigned int tag); | |
143 | ||
4c6e0415 FW |
144 | /* Extract the next audit module from the audit list. Only modules |
145 | for which dso_name_valid_for_suid is true are returned. Must be | |
8f7a75d7 FW |
146 | called after all the audit_list_add_string, |
147 | audit_list_add_dynamic_tags calls. */ | |
4c6e0415 | 148 | static const char *audit_list_next (struct audit_list *); |
74780cf6 | 149 | |
2bf9e641 FW |
150 | /* Initialize *STATE with the defaults. */ |
151 | static void dl_main_state_init (struct dl_main_state *state); | |
ea278354 UD |
152 | |
153 | /* Process all environments variables the dynamic linker must recognize. | |
154 | Since all of them start with `LD_' we are a bit smarter while finding | |
155 | all the entries. */ | |
851f32cf | 156 | extern char **_environ attribute_hidden; |
2bf9e641 | 157 | static void process_envvars (struct dl_main_state *state); |
ea278354 | 158 | |
11986c68 | 159 | #ifdef DL_ARGV_NOT_RELRO |
22aa06a5 | 160 | int _dl_argc attribute_hidden; |
11986c68 | 161 | char **_dl_argv = NULL; |
22aa06a5 RM |
162 | /* Nonzero if we were run directly. */ |
163 | unsigned int _dl_skip_args attribute_hidden; | |
11986c68 | 164 | #else |
22aa06a5 | 165 | int _dl_argc attribute_relro attribute_hidden; |
697afbe1 | 166 | char **_dl_argv attribute_relro = NULL; |
22aa06a5 | 167 | unsigned int _dl_skip_args attribute_relro attribute_hidden; |
11986c68 | 168 | #endif |
4243cbea | 169 | rtld_hidden_data_def (_dl_argv) |
5c82e15e | 170 | |
35f1e827 UD |
171 | #ifndef THREAD_SET_STACK_GUARD |
172 | /* Only exported for architectures that don't store the stack guard canary | |
173 | in thread local area. */ | |
174 | uintptr_t __stack_chk_guard attribute_relro; | |
175 | #endif | |
176 | ||
827b7087 UD |
177 | /* Only exported for architectures that don't store the pointer guard |
178 | value in thread local area. */ | |
7c241325 | 179 | uintptr_t __pointer_chk_guard_local attribute_relro attribute_hidden; |
827b7087 UD |
180 | #ifndef THREAD_SET_POINTER_GUARD |
181 | strong_alias (__pointer_chk_guard_local, __pointer_chk_guard) | |
182 | #endif | |
183 | ||
6d0ba622 FW |
184 | /* Check that AT_SECURE=0, or that the passed name does not contain |
185 | directories and is not overly long. Reject empty names | |
186 | unconditionally. */ | |
187 | static bool | |
188 | dso_name_valid_for_suid (const char *p) | |
189 | { | |
190 | if (__glibc_unlikely (__libc_enable_secure)) | |
191 | { | |
192 | /* Ignore pathnames with directories for AT_SECURE=1 | |
193 | programs, and also skip overlong names. */ | |
194 | size_t len = strlen (p); | |
195 | if (len >= SECURE_NAME_LIMIT || memchr (p, '/', len) != NULL) | |
196 | return false; | |
197 | } | |
198 | return *p != '\0'; | |
199 | } | |
827b7087 | 200 | |
4c6e0415 FW |
201 | static void |
202 | audit_list_init (struct audit_list *list) | |
9dcafc55 | 203 | { |
4c6e0415 FW |
204 | list->length = 0; |
205 | list->current_index = 0; | |
206 | list->current_tail = NULL; | |
207 | } | |
9dcafc55 | 208 | |
4c6e0415 FW |
209 | static void |
210 | audit_list_add_string (struct audit_list *list, const char *string) | |
81b82fb9 | 211 | { |
4c6e0415 FW |
212 | /* Empty strings do not load anything. */ |
213 | if (*string == '\0') | |
214 | return; | |
81b82fb9 | 215 | |
4c6e0415 FW |
216 | if (list->length == array_length (list->audit_strings)) |
217 | _dl_fatal_printf ("Fatal glibc error: Too many audit modules requested\n"); | |
81b82fb9 | 218 | |
4c6e0415 | 219 | list->audit_strings[list->length++] = string; |
81b82fb9 | 220 | |
4c6e0415 FW |
221 | /* Initialize processing of the first string for |
222 | audit_list_next. */ | |
223 | if (list->length == 1) | |
224 | list->current_tail = string; | |
81b82fb9 FW |
225 | } |
226 | ||
8f7a75d7 FW |
227 | static void |
228 | audit_list_add_dynamic_tag (struct audit_list *list, struct link_map *main_map, | |
229 | unsigned int tag) | |
230 | { | |
231 | ElfW(Dyn) *info = main_map->l_info[ADDRIDX (tag)]; | |
232 | const char *strtab = (const char *) D_PTR (main_map, l_info[DT_STRTAB]); | |
233 | if (info != NULL) | |
234 | audit_list_add_string (list, strtab + info->d_un.d_val); | |
235 | } | |
236 | ||
81b82fb9 | 237 | static const char * |
4c6e0415 | 238 | audit_list_next (struct audit_list *list) |
81b82fb9 | 239 | { |
4c6e0415 FW |
240 | if (list->current_tail == NULL) |
241 | return NULL; | |
242 | ||
243 | while (true) | |
81b82fb9 | 244 | { |
4c6e0415 FW |
245 | /* Advance to the next string in audit_strings if the current |
246 | string has been exhausted. */ | |
247 | while (*list->current_tail == '\0') | |
81b82fb9 | 248 | { |
4c6e0415 FW |
249 | ++list->current_index; |
250 | if (list->current_index == list->length) | |
81b82fb9 | 251 | { |
4c6e0415 FW |
252 | list->current_tail = NULL; |
253 | return NULL; | |
81b82fb9 | 254 | } |
4c6e0415 | 255 | list->current_tail = list->audit_strings[list->current_index]; |
81b82fb9 | 256 | } |
81b82fb9 | 257 | |
4c6e0415 FW |
258 | /* Split the in-string audit list at the next colon colon. */ |
259 | size_t len = strcspn (list->current_tail, ":"); | |
260 | if (len > 0 && len < sizeof (list->fname)) | |
261 | { | |
262 | memcpy (list->fname, list->current_tail, len); | |
263 | list->fname[len] = '\0'; | |
264 | } | |
265 | else | |
266 | /* Mark the name as unusable for dso_name_valid_for_suid. */ | |
267 | list->fname[0] = '\0'; | |
268 | ||
269 | /* Skip over the substring and the following delimiter. */ | |
270 | list->current_tail += len; | |
271 | if (*list->current_tail == ':') | |
272 | ++list->current_tail; | |
273 | ||
274 | /* If the name is valid, return it. */ | |
275 | if (dso_name_valid_for_suid (list->fname)) | |
276 | return list->fname; | |
277 | ||
278 | /* Otherwise wrap around to find the next list element. . */ | |
81b82fb9 | 279 | } |
81b82fb9 FW |
280 | } |
281 | ||
17796419 SN |
282 | /* Count audit modules before they are loaded so GLRO(dl_naudit) |
283 | is not yet usable. */ | |
284 | static size_t | |
285 | audit_list_count (struct audit_list *list) | |
286 | { | |
287 | /* Restore the audit_list iterator state at the end. */ | |
288 | const char *saved_tail = list->current_tail; | |
289 | size_t naudit = 0; | |
290 | ||
291 | assert (list->current_index == 0); | |
292 | while (audit_list_next (list) != NULL) | |
293 | naudit++; | |
294 | list->current_tail = saved_tail; | |
295 | list->current_index = 0; | |
296 | return naudit; | |
297 | } | |
298 | ||
2bf9e641 FW |
299 | static void |
300 | dl_main_state_init (struct dl_main_state *state) | |
301 | { | |
302 | audit_list_init (&state->audit_list); | |
303 | state->library_path = NULL; | |
27316f4a | 304 | state->library_path_source = NULL; |
2bf9e641 FW |
305 | state->preloadlist = NULL; |
306 | state->preloadarg = NULL; | |
dad90d52 FW |
307 | state->glibc_hwcaps_prepend = NULL; |
308 | state->glibc_hwcaps_mask = NULL; | |
2bf9e641 FW |
309 | state->mode = rtld_mode_normal; |
310 | state->any_debug = false; | |
311 | state->version_info = false; | |
312 | } | |
313 | ||
ce6e047f | 314 | #ifndef HAVE_INLINED_SYSCALLS |
39778c6c UD |
315 | /* Set nonzero during loading and initialization of executable and |
316 | libraries, cleared before the executable's entry point runs. This | |
317 | must not be initialized to nonzero, because the unused dynamic | |
318 | linker loaded in for libc.so's "ld.so.1" dep will provide the | |
319 | definition seen by libc.so's initializer; that value must be zero, | |
320 | and will be since that dynamic linker's _dl_start and dl_main will | |
321 | never be called. */ | |
e6caf4e1 | 322 | int _dl_starting_up = 0; |
9cf27b8d | 323 | rtld_hidden_def (_dl_starting_up) |
ce6e047f | 324 | #endif |
39778c6c | 325 | |
d6b5d570 UD |
326 | /* This is the structure which defines all variables global to ld.so |
327 | (except those which cannot be added for some reason). */ | |
5688da55 UD |
328 | struct rtld_global _rtld_global = |
329 | { | |
674ea882 L |
330 | /* Get architecture specific initializer. */ |
331 | #include <dl-procruntime.c> | |
30950a5f RA |
332 | /* Generally the default presumption without further information is an |
333 | * executable stack but this is not true for all platforms. */ | |
334 | ._dl_stack_flags = DEFAULT_STACK_PERMS, | |
ffa8d2a0 | 335 | #ifdef _LIBC_REENTRANT |
22c83193 | 336 | ._dl_load_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER, |
5a2a1d75 | 337 | ._dl_load_write_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER, |
83b53232 | 338 | ._dl_load_tls_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER, |
ffa8d2a0 | 339 | #endif |
415ac3df UD |
340 | ._dl_nns = 1, |
341 | ._dl_ns = | |
342 | { | |
bea9b193 | 343 | #ifdef _LIBC_REENTRANT |
415ac3df UD |
344 | [LM_ID_BASE] = { ._ns_unique_sym_table |
345 | = { .lock = _RTLD_LOCK_RECURSIVE_INITIALIZER } } | |
bea9b193 | 346 | #endif |
415ac3df | 347 | } |
5688da55 | 348 | }; |
27a754a9 UD |
349 | /* If we would use strong_alias here the compiler would see a |
350 | non-hidden definition. This would undo the effect of the previous | |
7ee881f1 | 351 | declaration. So spell out what strong_alias does plus add the |
27a754a9 UD |
352 | visibility attribute. */ |
353 | extern struct rtld_global _rtld_local | |
354 | __attribute__ ((alias ("_rtld_global"), visibility ("hidden"))); | |
c0fb8a56 | 355 | |
afdca0f2 UD |
356 | |
357 | /* This variable is similar to _rtld_local, but all values are | |
358 | read-only after relocation. */ | |
359 | struct rtld_global_ro _rtld_global_ro attribute_relro = | |
360 | { | |
c31e278f UD |
361 | /* Get architecture specific initializer. */ |
362 | #include <dl-procinfo.c> | |
afdca0f2 UD |
363 | #ifdef NEED_DL_SYSINFO |
364 | ._dl_sysinfo = DL_SYSINFO_DEFAULT, | |
365 | #endif | |
dd70526e | 366 | ._dl_debug_fd = STDERR_FILENO, |
afdca0f2 UD |
367 | ._dl_use_load_bias = -2, |
368 | ._dl_correct_cache_id = _DL_CACHE_DEFAULT_ID, | |
ff08fc59 | 369 | #if !HAVE_TUNABLES |
afdca0f2 | 370 | ._dl_hwcap_mask = HWCAP_IMPORTANT, |
ff08fc59 | 371 | #endif |
afdca0f2 UD |
372 | ._dl_lazy = 1, |
373 | ._dl_fpu_control = _FPU_DEFAULT, | |
02d46fc4 | 374 | ._dl_pagesize = EXEC_PAGESIZE, |
73d65cc3 | 375 | ._dl_inhibit_cache = 0, |
154d10bd UD |
376 | |
377 | /* Function pointers. */ | |
154d10bd | 378 | ._dl_debug_printf = _dl_debug_printf, |
ab97ee8f | 379 | ._dl_mcount = _dl_mcount, |
021723ab | 380 | ._dl_lookup_symbol_x = _dl_lookup_symbol_x, |
9dcafc55 | 381 | ._dl_open = _dl_open, |
93025f93 | 382 | ._dl_close = _dl_close, |
b2964eb1 | 383 | ._dl_catch_error = _rtld_catch_error, |
fada9018 | 384 | ._dl_error_free = _dl_error_free, |
7c22c7ec | 385 | ._dl_tls_get_addr_soft = _dl_tls_get_addr_soft, |
f1d333b5 | 386 | ._dl_libc_freeres = __rtld_libc_freeres, |
7c22c7ec UD |
387 | #ifdef HAVE_DL_DISCOVER_OSVERSION |
388 | ._dl_discover_osversion = _dl_discover_osversion | |
389 | #endif | |
afdca0f2 UD |
390 | }; |
391 | /* If we would use strong_alias here the compiler would see a | |
392 | non-hidden definition. This would undo the effect of the previous | |
393 | declaration. So spell out was strong_alias does plus add the | |
394 | visibility attribute. */ | |
395 | extern struct rtld_global_ro _rtld_local_ro | |
396 | __attribute__ ((alias ("_rtld_global_ro"), visibility ("hidden"))); | |
397 | ||
398 | ||
67ddea92 | 399 | static void dl_main (const ElfW(Phdr) *phdr, ElfW(Word) phnum, |
3a56ea26 | 400 | ElfW(Addr) *user_entry, ElfW(auxv_t) *auxv); |
d66e34cd | 401 | |
392a6b52 | 402 | /* These two variables cannot be moved into .data.rel.ro. */ |
d6b5d570 UD |
403 | static struct libname_list _dl_rtld_libname; |
404 | static struct libname_list _dl_rtld_libname2; | |
86d2c878 | 405 | |
db276fa1 | 406 | /* Variable for statistics. */ |
1e372ded AZ |
407 | RLTD_TIMING_DECLARE (relocate_time, static); |
408 | RLTD_TIMING_DECLARE (load_time, static, attribute_relro); | |
409 | RLTD_TIMING_DECLARE (start_time, static, attribute_relro); | |
db276fa1 | 410 | |
2a76f7ef UD |
411 | /* Additional definitions needed by TLS initialization. */ |
412 | #ifdef TLS_INIT_HELPER | |
413 | TLS_INIT_HELPER | |
5e289179 UD |
414 | #endif |
415 | ||
416 | /* Helper function for syscall implementation. */ | |
417 | #ifdef DL_SYSINFO_IMPLEMENTATION | |
418 | DL_SYSINFO_IMPLEMENTATION | |
2a76f7ef UD |
419 | #endif |
420 | ||
01d8e36d UD |
421 | /* Before ld.so is relocated we must not access variables which need |
422 | relocations. This means variables which are exported. Variables | |
423 | declared as static are fine. If we can mark a variable hidden this | |
27a754a9 | 424 | is fine, too. The latter is important here. We can avoid setting |
01d8e36d UD |
425 | up a temporary link map for ld.so if we can mark _rtld_global as |
426 | hidden. */ | |
11bf311e | 427 | #ifdef PI_STATIC_AND_HIDDEN |
01d8e36d UD |
428 | # define DONT_USE_BOOTSTRAP_MAP 1 |
429 | #endif | |
430 | ||
431 | #ifdef DONT_USE_BOOTSTRAP_MAP | |
432 | static ElfW(Addr) _dl_start_final (void *arg); | |
433 | #else | |
4874b009 RM |
434 | struct dl_start_final_info |
435 | { | |
436 | struct link_map l; | |
1e372ded | 437 | RTLD_TIMING_VAR (start_time); |
4874b009 | 438 | }; |
01d8e36d | 439 | static ElfW(Addr) _dl_start_final (void *arg, |
4874b009 | 440 | struct dl_start_final_info *info); |
01d8e36d | 441 | #endif |
6a1db4ff | 442 | |
65da9563 RM |
443 | /* These defined magically in the linker script. */ |
444 | extern char _begin[] attribute_hidden; | |
eec8b6ca | 445 | extern char _etext[] attribute_hidden; |
65da9563 RM |
446 | extern char _end[] attribute_hidden; |
447 | ||
448 | ||
b1dbbaa4 RM |
449 | #ifdef RTLD_START |
450 | RTLD_START | |
451 | #else | |
eaad82e0 | 452 | # error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START" |
b1dbbaa4 RM |
453 | #endif |
454 | ||
c2248c44 RM |
455 | /* This is the second half of _dl_start (below). It can be inlined safely |
456 | under DONT_USE_BOOTSTRAP_MAP, where it is careful not to make any GOT | |
457 | references. When the tools don't permit us to avoid using a GOT entry | |
458 | for _dl_rtld_global (no attribute_hidden support), we must make sure | |
459 | this function is not inlined (see below). */ | |
460 | ||
461 | #ifdef DONT_USE_BOOTSTRAP_MAP | |
462 | static inline ElfW(Addr) __attribute__ ((always_inline)) | |
463 | _dl_start_final (void *arg) | |
464 | #else | |
465 | static ElfW(Addr) __attribute__ ((noinline)) | |
4874b009 | 466 | _dl_start_final (void *arg, struct dl_start_final_info *info) |
c2248c44 RM |
467 | #endif |
468 | { | |
469 | ElfW(Addr) start_addr; | |
c2248c44 | 470 | |
1e372ded AZ |
471 | /* If it hasn't happen yet record the startup time. */ |
472 | rtld_timer_start (&start_time); | |
473 | #if !defined DONT_USE_BOOTSTRAP_MAP | |
474 | RTLD_TIMING_SET (start_time, info->start_time); | |
4874b009 | 475 | #endif |
c2248c44 RM |
476 | |
477 | /* Transfer data about ourselves to the permanent link_map structure. */ | |
478 | #ifndef DONT_USE_BOOTSTRAP_MAP | |
4874b009 RM |
479 | GL(dl_rtld_map).l_addr = info->l.l_addr; |
480 | GL(dl_rtld_map).l_ld = info->l.l_ld; | |
b413280c | 481 | GL(dl_rtld_map).l_ld_readonly = info->l.l_ld_readonly; |
4874b009 | 482 | memcpy (GL(dl_rtld_map).l_info, info->l.l_info, |
c2248c44 | 483 | sizeof GL(dl_rtld_map).l_info); |
4874b009 | 484 | GL(dl_rtld_map).l_mach = info->l.l_mach; |
82221992 | 485 | GL(dl_rtld_map).l_relocated = 1; |
c2248c44 RM |
486 | #endif |
487 | _dl_setup_hash (&GL(dl_rtld_map)); | |
c0f62c56 | 488 | GL(dl_rtld_map).l_real = &GL(dl_rtld_map); |
c2248c44 RM |
489 | GL(dl_rtld_map).l_map_start = (ElfW(Addr)) _begin; |
490 | GL(dl_rtld_map).l_map_end = (ElfW(Addr)) _end; | |
eec8b6ca | 491 | GL(dl_rtld_map).l_text_end = (ElfW(Addr)) _etext; |
c2248c44 | 492 | /* Copy the TLS related data if necessary. */ |
11bf311e | 493 | #ifndef DONT_USE_BOOTSTRAP_MAP |
3d8c8bff | 494 | # if NO_TLS_OFFSET != 0 |
299601a1 | 495 | GL(dl_rtld_map).l_tls_offset = NO_TLS_OFFSET; |
c2248c44 | 496 | # endif |
c2248c44 RM |
497 | #endif |
498 | ||
ea4f25a7 UD |
499 | /* Initialize the stack end variable. */ |
500 | __libc_stack_end = __builtin_frame_address (0); | |
501 | ||
c2248c44 RM |
502 | /* Call the OS-dependent function to set up life so we can do things like |
503 | file access. It will call `dl_main' (below) to do all the real work | |
504 | of the dynamic linker, and then unwind our frame and run the user | |
505 | entry point on the same stack we entered on. */ | |
ecdeaac0 | 506 | start_addr = _dl_sysdep_start (arg, &dl_main); |
c2248c44 | 507 | |
a1ffb40e | 508 | if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_STATISTICS)) |
39b04aa3 | 509 | { |
1e372ded AZ |
510 | RTLD_TIMING_VAR (rtld_total_time); |
511 | rtld_timer_stop (&rtld_total_time, start_time); | |
512 | print_statistics (RTLD_TIMING_REF(rtld_total_time)); | |
39b04aa3 | 513 | } |
c2248c44 RM |
514 | |
515 | return start_addr; | |
516 | } | |
517 | ||
01d8e36d UD |
518 | #ifdef DONT_USE_BOOTSTRAP_MAP |
519 | # define bootstrap_map GL(dl_rtld_map) | |
520 | #else | |
4874b009 | 521 | # define bootstrap_map info.l |
739d440d | 522 | #endif |
d66e34cd | 523 | |
490e6c62 FS |
524 | static ElfW(Addr) __attribute_used__ |
525 | _dl_start (void *arg) | |
526 | { | |
4874b009 | 527 | #ifdef DONT_USE_BOOTSTRAP_MAP |
1e372ded | 528 | rtld_timer_start (&start_time); |
4874b009 | 529 | #else |
490e6c62 | 530 | struct dl_start_final_info info; |
1e372ded | 531 | rtld_timer_start (&info.start_time); |
4874b009 | 532 | #endif |
db276fa1 | 533 | |
e66d0a4c UD |
534 | /* Partly clean the `bootstrap_map' structure up. Don't use |
535 | `memset' since it might not be built in or inlined and we cannot | |
536 | make function calls at this point. Use '__builtin_memset' if we | |
01d8e36d UD |
537 | know it is available. We do not have to clear the memory if we |
538 | do not have to use the temporary bootstrap_map. Global variables | |
539 | are initialized to zero by default. */ | |
540 | #ifndef DONT_USE_BOOTSTRAP_MAP | |
541 | # ifdef HAVE_BUILTIN_MEMSET | |
e66d0a4c | 542 | __builtin_memset (bootstrap_map.l_info, '\0', sizeof (bootstrap_map.l_info)); |
01d8e36d | 543 | # else |
ce460d04 | 544 | for (size_t cnt = 0; |
264ec183 UD |
545 | cnt < sizeof (bootstrap_map.l_info) / sizeof (bootstrap_map.l_info[0]); |
546 | ++cnt) | |
547 | bootstrap_map.l_info[cnt] = 0; | |
01d8e36d | 548 | # endif |
e66d0a4c | 549 | #endif |
264ec183 | 550 | |
d66e34cd | 551 | /* Figure out the run-time load address of the dynamic linker itself. */ |
86d2c878 | 552 | bootstrap_map.l_addr = elf_machine_load_address (); |
d66e34cd | 553 | |
47707456 UD |
554 | /* Read our own dynamic section and fill in the info array. */ |
555 | bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic (); | |
2ec99d8c | 556 | bootstrap_map.l_ld_readonly = DL_RO_DYN_SECTION; |
5118dcac | 557 | elf_get_dynamic_info (&bootstrap_map, true, false); |
d66e34cd | 558 | |
11bf311e | 559 | #if NO_TLS_OFFSET != 0 |
299601a1 UD |
560 | bootstrap_map.l_tls_offset = NO_TLS_OFFSET; |
561 | #endif | |
562 | ||
d66e34cd | 563 | #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC |
490e6c62 | 564 | ELF_MACHINE_BEFORE_RTLD_RELOC (&bootstrap_map, bootstrap_map.l_info); |
d66e34cd RM |
565 | #endif |
566 | ||
32e6df36 UD |
567 | if (bootstrap_map.l_addr || ! bootstrap_map.l_info[VALIDX(DT_GNU_PRELINKED)]) |
568 | { | |
569 | /* Relocate ourselves so we can do normal function calls and | |
570 | data access using the global offset table. */ | |
571 | ||
490e6c62 | 572 | ELF_DYNAMIC_RELOCATE (&bootstrap_map, NULL, 0, 0, 0); |
32e6df36 | 573 | } |
f85f3563 | 574 | bootstrap_map.l_relocated = 1; |
421f82e5 | 575 | |
ea7eb7e3 UD |
576 | /* Please note that we don't allow profiling of this object and |
577 | therefore need not test whether we have to allocate the array | |
578 | for the relocation results (as done in dl-reloc.c). */ | |
421f82e5 | 579 | |
d66e34cd RM |
580 | /* Now life is sane; we can call functions and access global data. |
581 | Set up to use the operating system facilities, and find out from | |
582 | the operating system's program loader where to find the program | |
6a1db4ff UD |
583 | header table in core. Put the rest of _dl_start into a separate |
584 | function, that way the compiler cannot put accesses to the GOT | |
585 | before ELF_DYNAMIC_RELOCATE. */ | |
3a0ecccb FW |
586 | |
587 | __rtld_malloc_init_stubs (); | |
588 | ||
5d28a896 FW |
589 | /* Do not use an initializer for these members because it would |
590 | intefere with __rtld_static_init. */ | |
591 | GLRO (dl_find_object) = &_dl_find_object; | |
592 | ||
c0282c06 | 593 | { |
01d8e36d UD |
594 | #ifdef DONT_USE_BOOTSTRAP_MAP |
595 | ElfW(Addr) entry = _dl_start_final (arg); | |
596 | #else | |
4874b009 | 597 | ElfW(Addr) entry = _dl_start_final (arg, &info); |
01d8e36d | 598 | #endif |
c0282c06 UD |
599 | |
600 | #ifndef ELF_MACHINE_START_ADDRESS | |
601 | # define ELF_MACHINE_START_ADDRESS(map, start) (start) | |
602 | #endif | |
603 | ||
7cb92a99 | 604 | return ELF_MACHINE_START_ADDRESS (GL(dl_ns)[LM_ID_BASE]._ns_loaded, entry); |
c0282c06 | 605 | } |
6a1db4ff UD |
606 | } |
607 | ||
608 | ||
d66e34cd | 609 | |
d66e34cd RM |
610 | /* Now life is peachy; we can do all normal operations. |
611 | On to the real work. */ | |
612 | ||
993b3242 UD |
613 | /* Some helper functions. */ |
614 | ||
615 | /* Arguments to relocate_doit. */ | |
616 | struct relocate_args | |
617 | { | |
618 | struct link_map *l; | |
2ca285b0 | 619 | int reloc_mode; |
993b3242 UD |
620 | }; |
621 | ||
622 | struct map_args | |
623 | { | |
624 | /* Argument to map_doit. */ | |
acf869f4 | 625 | const char *str; |
f04b9a68 | 626 | struct link_map *loader; |
f04b9a68 | 627 | int mode; |
993b3242 | 628 | /* Return value of map_doit. */ |
f04b9a68 | 629 | struct link_map *map; |
993b3242 UD |
630 | }; |
631 | ||
9dcafc55 UD |
632 | struct dlmopen_args |
633 | { | |
634 | const char *fname; | |
635 | struct link_map *map; | |
636 | }; | |
637 | ||
638 | struct lookup_args | |
639 | { | |
640 | const char *name; | |
641 | struct link_map *map; | |
642 | void *result; | |
643 | }; | |
644 | ||
993b3242 UD |
645 | /* Arguments to version_check_doit. */ |
646 | struct version_check_args | |
647 | { | |
993b3242 | 648 | int doexit; |
145b8413 | 649 | int dotrace; |
993b3242 UD |
650 | }; |
651 | ||
652 | static void | |
653 | relocate_doit (void *a) | |
654 | { | |
655 | struct relocate_args *args = (struct relocate_args *) a; | |
656 | ||
2ca285b0 | 657 | _dl_relocate_object (args->l, args->l->l_scope, args->reloc_mode, 0); |
993b3242 UD |
658 | } |
659 | ||
660 | static void | |
661 | map_doit (void *a) | |
662 | { | |
be935610 | 663 | struct map_args *args = (struct map_args *) a; |
798212a0 PP |
664 | int type = (args->mode == __RTLD_OPENEXEC) ? lt_executable : lt_library; |
665 | args->map = _dl_map_object (args->loader, args->str, type, 0, | |
8e9f92e9 | 666 | args->mode, LM_ID_BASE); |
993b3242 UD |
667 | } |
668 | ||
9dcafc55 UD |
669 | static void |
670 | dlmopen_doit (void *a) | |
671 | { | |
672 | struct dlmopen_args *args = (struct dlmopen_args *) a; | |
8e9f92e9 AS |
673 | args->map = _dl_open (args->fname, |
674 | (RTLD_LAZY | __RTLD_DLOPEN | __RTLD_AUDIT | |
675 | | __RTLD_SECURE), | |
4243cbea | 676 | dl_main, LM_ID_NEWLM, _dl_argc, _dl_argv, |
9dcafc55 UD |
677 | __environ); |
678 | } | |
679 | ||
680 | static void | |
681 | lookup_doit (void *a) | |
682 | { | |
683 | struct lookup_args *args = (struct lookup_args *) a; | |
684 | const ElfW(Sym) *ref = NULL; | |
685 | args->result = NULL; | |
686 | lookup_t l = _dl_lookup_symbol_x (args->name, args->map, &ref, | |
687 | args->map->l_local_scope, NULL, 0, | |
688 | DL_LOOKUP_RETURN_NEWEST, NULL); | |
689 | if (ref != NULL) | |
690 | args->result = DL_SYMBOL_ADDRESS (l, ref); | |
691 | } | |
692 | ||
993b3242 UD |
693 | static void |
694 | version_check_doit (void *a) | |
695 | { | |
be935610 | 696 | struct version_check_args *args = (struct version_check_args *) a; |
c0f62c56 UD |
697 | if (_dl_check_all_versions (GL(dl_ns)[LM_ID_BASE]._ns_loaded, 1, |
698 | args->dotrace) && args->doexit) | |
993b3242 UD |
699 | /* We cannot start the application. Abort now. */ |
700 | _exit (1); | |
701 | } | |
702 | ||
ce37fa88 UD |
703 | |
704 | static inline struct link_map * | |
705 | find_needed (const char *name) | |
706 | { | |
c0f62c56 UD |
707 | struct r_scope_elem *scope = &GL(dl_ns)[LM_ID_BASE]._ns_loaded->l_searchlist; |
708 | unsigned int n = scope->r_nlist; | |
ce37fa88 | 709 | |
be935610 | 710 | while (n-- > 0) |
c0f62c56 UD |
711 | if (_dl_name_match_p (name, scope->r_list[n])) |
712 | return scope->r_list[n]; | |
ce37fa88 UD |
713 | |
714 | /* Should never happen. */ | |
715 | return NULL; | |
716 | } | |
717 | ||
718 | static int | |
719 | match_version (const char *string, struct link_map *map) | |
720 | { | |
a42195db | 721 | const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]); |
ce37fa88 UD |
722 | ElfW(Verdef) *def; |
723 | ||
b0982c4a | 724 | #define VERDEFTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERDEF)) |
ce37fa88 UD |
725 | if (map->l_info[VERDEFTAG] == NULL) |
726 | /* The file has no symbol versioning. */ | |
727 | return 0; | |
728 | ||
729 | def = (ElfW(Verdef) *) ((char *) map->l_addr | |
730 | + map->l_info[VERDEFTAG]->d_un.d_ptr); | |
731 | while (1) | |
732 | { | |
733 | ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux); | |
734 | ||
735 | /* Compare the version strings. */ | |
736 | if (strcmp (string, strtab + aux->vda_name) == 0) | |
737 | /* Bingo! */ | |
738 | return 1; | |
739 | ||
740 | /* If no more definitions we failed to find what we want. */ | |
741 | if (def->vd_next == 0) | |
742 | break; | |
743 | ||
744 | /* Next definition. */ | |
745 | def = (ElfW(Verdef) *) ((char *) def + def->vd_next); | |
746 | } | |
747 | ||
748 | return 0; | |
749 | } | |
750 | ||
9dcafc55 UD |
751 | static bool tls_init_tp_called; |
752 | ||
753 | static void * | |
17796419 | 754 | init_tls (size_t naudit) |
9dcafc55 UD |
755 | { |
756 | /* Number of elements in the static TLS block. */ | |
757 | GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx); | |
758 | ||
759 | /* Do not do this twice. The audit interface might have required | |
760 | the DTV interfaces to be set up early. */ | |
761 | if (GL(dl_initial_dtv) != NULL) | |
762 | return NULL; | |
763 | ||
764 | /* Allocate the array which contains the information about the | |
765 | dtv slots. We allocate a few entries more than needed to | |
766 | avoid the need for reallocation. */ | |
767 | size_t nelem = GL(dl_tls_max_dtv_idx) + 1 + TLS_SLOTINFO_SURPLUS; | |
768 | ||
769 | /* Allocate. */ | |
770 | GL(dl_tls_dtv_slotinfo_list) = (struct dtv_slotinfo_list *) | |
771 | calloc (sizeof (struct dtv_slotinfo_list) | |
772 | + nelem * sizeof (struct dtv_slotinfo), 1); | |
773 | /* No need to check the return value. If memory allocation failed | |
774 | the program would have been terminated. */ | |
775 | ||
776 | struct dtv_slotinfo *slotinfo = GL(dl_tls_dtv_slotinfo_list)->slotinfo; | |
777 | GL(dl_tls_dtv_slotinfo_list)->len = nelem; | |
778 | GL(dl_tls_dtv_slotinfo_list)->next = NULL; | |
779 | ||
780 | /* Fill in the information from the loaded modules. No namespace | |
781 | but the base one can be filled at this time. */ | |
782 | assert (GL(dl_ns)[LM_ID_BASE + 1]._ns_loaded == NULL); | |
783 | int i = 0; | |
784 | for (struct link_map *l = GL(dl_ns)[LM_ID_BASE]._ns_loaded; l != NULL; | |
785 | l = l->l_next) | |
786 | if (l->l_tls_blocksize != 0) | |
787 | { | |
788 | /* This is a module with TLS data. Store the map reference. | |
789 | The generation counter is zero. */ | |
790 | slotinfo[i].map = l; | |
791 | /* slotinfo[i].gen = 0; */ | |
792 | ++i; | |
793 | } | |
794 | assert (i == GL(dl_tls_max_dtv_idx)); | |
795 | ||
0c7b002f | 796 | /* Calculate the size of the static TLS surplus. */ |
17796419 | 797 | _dl_tls_static_surplus_init (naudit); |
0c7b002f | 798 | |
9dcafc55 UD |
799 | /* Compute the TLS offsets for the various blocks. */ |
800 | _dl_determine_tlsoffset (); | |
801 | ||
802 | /* Construct the static TLS block and the dtv for the initial | |
803 | thread. For some platforms this will include allocating memory | |
804 | for the thread descriptor. The memory for the TLS block will | |
805 | never be freed. It should be allocated accordingly. The dtv | |
806 | array can be changed if dynamic loading requires it. */ | |
807 | void *tcbp = _dl_allocate_tls_storage (); | |
808 | if (tcbp == NULL) | |
809 | _dl_fatal_printf ("\ | |
f648728c | 810 | cannot allocate TLS data structures for initial thread\n"); |
9dcafc55 UD |
811 | |
812 | /* Store for detection of the special case by __tls_get_addr | |
813 | so it knows not to pass this dtv to the normal realloc. */ | |
814 | GL(dl_initial_dtv) = GET_DTV (tcbp); | |
815 | ||
3d8c8bff | 816 | /* And finally install it for the main thread. */ |
774f9285 | 817 | const char *lossage = TLS_INIT_TP (tcbp); |
a1ffb40e | 818 | if (__glibc_unlikely (lossage != NULL)) |
9dcafc55 | 819 | _dl_fatal_printf ("cannot set up thread-local storage: %s\n", lossage); |
90d7e7e5 | 820 | __tls_init_tp (); |
9dcafc55 UD |
821 | tls_init_tp_called = true; |
822 | ||
823 | return tcbp; | |
824 | } | |
9dcafc55 | 825 | |
20fe49b9 | 826 | static unsigned int |
acf869f4 | 827 | do_preload (const char *fname, struct link_map *main_map, const char *where) |
20fe49b9 UD |
828 | { |
829 | const char *objname; | |
830 | const char *err_str = NULL; | |
831 | struct map_args args; | |
74780cf6 | 832 | bool malloced; |
20fe49b9 UD |
833 | |
834 | args.str = fname; | |
835 | args.loader = main_map; | |
8e9f92e9 | 836 | args.mode = __RTLD_SECURE; |
20fe49b9 UD |
837 | |
838 | unsigned int old_nloaded = GL(dl_ns)[LM_ID_BASE]._ns_nloaded; | |
839 | ||
74780cf6 | 840 | (void) _dl_catch_error (&objname, &err_str, &malloced, map_doit, &args); |
a1ffb40e | 841 | if (__glibc_unlikely (err_str != NULL)) |
20fe49b9 UD |
842 | { |
843 | _dl_error_printf ("\ | |
4db5b08f MS |
844 | ERROR: ld.so: object '%s' from %s cannot be preloaded (%s): ignored.\n", |
845 | fname, where, err_str); | |
20fe49b9 UD |
846 | /* No need to call free, this is still before |
847 | the libc's malloc is used. */ | |
848 | } | |
849 | else if (GL(dl_ns)[LM_ID_BASE]._ns_nloaded != old_nloaded) | |
850 | /* It is no duplicate. */ | |
851 | return 1; | |
852 | ||
853 | /* Nothing loaded. */ | |
854 | return 0; | |
855 | } | |
856 | ||
4c48ef06 UD |
857 | static void |
858 | security_init (void) | |
859 | { | |
860 | /* Set up the stack checker's canary. */ | |
965cb60a | 861 | uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random); |
4c48ef06 UD |
862 | #ifdef THREAD_SET_STACK_GUARD |
863 | THREAD_SET_STACK_GUARD (stack_chk_guard); | |
864 | #else | |
865 | __stack_chk_guard = stack_chk_guard; | |
866 | #endif | |
867 | ||
868 | /* Set up the pointer guard as well, if necessary. */ | |
a014cecd FW |
869 | uintptr_t pointer_chk_guard |
870 | = _dl_setup_pointer_guard (_dl_random, stack_chk_guard); | |
4c48ef06 | 871 | #ifdef THREAD_SET_POINTER_GUARD |
a014cecd | 872 | THREAD_SET_POINTER_GUARD (pointer_chk_guard); |
4c48ef06 | 873 | #endif |
a014cecd | 874 | __pointer_chk_guard_local = pointer_chk_guard; |
965cb60a UD |
875 | |
876 | /* We do not need the _dl_random value anymore. The less | |
877 | information we leave behind, the better, so clear the | |
878 | variable. */ | |
879 | _dl_random = NULL; | |
4c48ef06 UD |
880 | } |
881 | ||
1bdda52f | 882 | #include <setup-vdso.h> |
4c48ef06 | 883 | |
6d0ba622 FW |
884 | /* The LD_PRELOAD environment variable gives list of libraries |
885 | separated by white space or colons that are loaded before the | |
886 | executable's dependencies and prepended to the global scope list. | |
887 | (If the binary is running setuid all elements containing a '/' are | |
888 | ignored since it is insecure.) Return the number of preloads | |
8692ebdb | 889 | performed. Ditto for --preload command argument. */ |
6d0ba622 | 890 | unsigned int |
8692ebdb DN |
891 | handle_preload_list (const char *preloadlist, struct link_map *main_map, |
892 | const char *where) | |
6d0ba622 FW |
893 | { |
894 | unsigned int npreloads = 0; | |
895 | const char *p = preloadlist; | |
896 | char fname[SECURE_PATH_LIMIT]; | |
897 | ||
898 | while (*p != '\0') | |
899 | { | |
900 | /* Split preload list at space/colon. */ | |
901 | size_t len = strcspn (p, " :"); | |
902 | if (len > 0 && len < sizeof (fname)) | |
903 | { | |
904 | memcpy (fname, p, len); | |
905 | fname[len] = '\0'; | |
906 | } | |
907 | else | |
908 | fname[0] = '\0'; | |
909 | ||
910 | /* Skip over the substring and the following delimiter. */ | |
911 | p += len; | |
912 | if (*p != '\0') | |
913 | ++p; | |
914 | ||
915 | if (dso_name_valid_for_suid (fname)) | |
8692ebdb | 916 | npreloads += do_preload (fname, main_map, where); |
6d0ba622 FW |
917 | } |
918 | return npreloads; | |
919 | } | |
920 | ||
3b856d09 FW |
921 | /* Called if the audit DSO cannot be used: if it does not have the |
922 | appropriate interfaces, or it expects a more recent version library | |
923 | version than what the dynamic linker provides. */ | |
924 | static void | |
925 | unload_audit_module (struct link_map *map, int original_tls_idx) | |
926 | { | |
927 | #ifndef NDEBUG | |
928 | Lmid_t ns = map->l_ns; | |
929 | #endif | |
930 | _dl_close (map); | |
931 | ||
932 | /* Make sure the namespace has been cleared entirely. */ | |
933 | assert (GL(dl_ns)[ns]._ns_loaded == NULL); | |
934 | assert (GL(dl_ns)[ns]._ns_nloaded == 0); | |
935 | ||
936 | GL(dl_tls_max_dtv_idx) = original_tls_idx; | |
937 | } | |
938 | ||
939 | /* Called to print an error message if loading of an audit module | |
940 | failed. */ | |
941 | static void | |
942 | report_audit_module_load_error (const char *name, const char *err_str, | |
943 | bool malloced) | |
944 | { | |
945 | _dl_error_printf ("\ | |
946 | ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n", | |
947 | name, err_str); | |
948 | if (malloced) | |
949 | free ((char *) err_str); | |
950 | } | |
951 | ||
952 | /* Load one audit module. */ | |
953 | static void | |
954 | load_audit_module (const char *name, struct audit_ifaces **last_audit) | |
955 | { | |
956 | int original_tls_idx = GL(dl_tls_max_dtv_idx); | |
957 | ||
958 | struct dlmopen_args dlmargs; | |
959 | dlmargs.fname = name; | |
960 | dlmargs.map = NULL; | |
961 | ||
962 | const char *objname; | |
963 | const char *err_str = NULL; | |
964 | bool malloced; | |
965 | _dl_catch_error (&objname, &err_str, &malloced, dlmopen_doit, &dlmargs); | |
966 | if (__glibc_unlikely (err_str != NULL)) | |
967 | { | |
968 | report_audit_module_load_error (name, err_str, malloced); | |
969 | return; | |
970 | } | |
971 | ||
972 | struct lookup_args largs; | |
973 | largs.name = "la_version"; | |
974 | largs.map = dlmargs.map; | |
975 | _dl_catch_error (&objname, &err_str, &malloced, lookup_doit, &largs); | |
976 | if (__glibc_likely (err_str != NULL)) | |
977 | { | |
978 | unload_audit_module (dlmargs.map, original_tls_idx); | |
979 | report_audit_module_load_error (name, err_str, malloced); | |
980 | return; | |
981 | } | |
982 | ||
983 | unsigned int (*laversion) (unsigned int) = largs.result; | |
984 | ||
985 | /* A null symbol indicates that something is very wrong with the | |
986 | loaded object because defined symbols are supposed to have a | |
987 | valid, non-null address. */ | |
988 | assert (laversion != NULL); | |
989 | ||
990 | unsigned int lav = laversion (LAV_CURRENT); | |
991 | if (lav == 0) | |
992 | { | |
993 | /* Only print an error message if debugging because this can | |
994 | happen deliberately. */ | |
995 | if (GLRO(dl_debug_mask) & DL_DEBUG_FILES) | |
996 | _dl_debug_printf ("\ | |
997 | file=%s [%lu]; audit interface function la_version returned zero; ignored.\n", | |
998 | dlmargs.map->l_name, dlmargs.map->l_ns); | |
999 | unload_audit_module (dlmargs.map, original_tls_idx); | |
1000 | return; | |
1001 | } | |
1002 | ||
1003 | if (lav > LAV_CURRENT) | |
1004 | { | |
1005 | _dl_debug_printf ("\ | |
1006 | ERROR: audit interface '%s' requires version %d (maximum supported version %d); ignored.\n", | |
1007 | name, lav, LAV_CURRENT); | |
1008 | unload_audit_module (dlmargs.map, original_tls_idx); | |
1009 | return; | |
1010 | } | |
1011 | ||
1012 | enum { naudit_ifaces = 8 }; | |
1013 | union | |
1014 | { | |
1015 | struct audit_ifaces ifaces; | |
1016 | void (*fptr[naudit_ifaces]) (void); | |
1017 | } *newp = malloc (sizeof (*newp)); | |
1018 | if (newp == NULL) | |
1019 | _dl_fatal_printf ("Out of memory while loading audit modules\n"); | |
1020 | ||
1021 | /* Names of the auditing interfaces. All in one | |
1022 | long string. */ | |
1023 | static const char audit_iface_names[] = | |
1024 | "la_activity\0" | |
1025 | "la_objsearch\0" | |
1026 | "la_objopen\0" | |
1027 | "la_preinit\0" | |
063f9ba2 | 1028 | LA_SYMBIND "\0" |
3b856d09 FW |
1029 | #define STRING(s) __STRING (s) |
1030 | "la_" STRING (ARCH_LA_PLTENTER) "\0" | |
1031 | "la_" STRING (ARCH_LA_PLTEXIT) "\0" | |
1032 | "la_objclose\0"; | |
1033 | unsigned int cnt = 0; | |
1034 | const char *cp = audit_iface_names; | |
1035 | do | |
1036 | { | |
1037 | largs.name = cp; | |
1038 | _dl_catch_error (&objname, &err_str, &malloced, lookup_doit, &largs); | |
1039 | ||
1040 | /* Store the pointer. */ | |
1041 | if (err_str == NULL && largs.result != NULL) | |
c7bf5cea | 1042 | newp->fptr[cnt] = largs.result; |
3b856d09 FW |
1043 | else |
1044 | newp->fptr[cnt] = NULL; | |
1045 | ++cnt; | |
1046 | ||
1047 | cp = rawmemchr (cp, '\0') + 1; | |
1048 | } | |
1049 | while (*cp != '\0'); | |
1050 | assert (cnt == naudit_ifaces); | |
1051 | ||
1052 | /* Now append the new auditing interface to the list. */ | |
1053 | newp->ifaces.next = NULL; | |
1054 | if (*last_audit == NULL) | |
1055 | *last_audit = GLRO(dl_audit) = &newp->ifaces; | |
1056 | else | |
1057 | *last_audit = (*last_audit)->next = &newp->ifaces; | |
c7bf5cea | 1058 | |
e1d559f3 FW |
1059 | /* The dynamic linker link map is statically allocated, so the |
1060 | cookie in _dl_new_object has not happened. */ | |
1061 | link_map_audit_state (&GL (dl_rtld_map), GLRO (dl_naudit))->cookie | |
c7bf5cea FW |
1062 | = (intptr_t) &GL (dl_rtld_map); |
1063 | ||
3b856d09 FW |
1064 | ++GLRO(dl_naudit); |
1065 | ||
1066 | /* Mark the DSO as being used for auditing. */ | |
1067 | dlmargs.map->l_auditing = 1; | |
1068 | } | |
1069 | ||
3b856d09 FW |
1070 | /* Load all audit modules. */ |
1071 | static void | |
4c6e0415 | 1072 | load_audit_modules (struct link_map *main_map, struct audit_list *audit_list) |
3b856d09 FW |
1073 | { |
1074 | struct audit_ifaces *last_audit = NULL; | |
3b856d09 FW |
1075 | |
1076 | while (true) | |
1077 | { | |
4c6e0415 | 1078 | const char *name = audit_list_next (audit_list); |
3b856d09 FW |
1079 | if (name == NULL) |
1080 | break; | |
1081 | load_audit_module (name, &last_audit); | |
1082 | } | |
1083 | ||
1084 | /* Notify audit modules of the initially loaded modules (the main | |
1085 | program and the dynamic linker itself). */ | |
1086 | if (GLRO(dl_naudit) > 0) | |
1087 | { | |
aee6e90f AZ |
1088 | _dl_audit_objopen (main_map, LM_ID_BASE); |
1089 | _dl_audit_objopen (&GL(dl_rtld_map), LM_ID_BASE); | |
3b856d09 FW |
1090 | } |
1091 | } | |
1092 | ||
c1cb2dee FW |
1093 | /* Check if the executable is not actualy dynamically linked, and |
1094 | invoke it directly in that case. */ | |
1095 | static void | |
1096 | rtld_chain_load (struct link_map *main_map, char *argv0) | |
1097 | { | |
1098 | /* The dynamic loader run against itself. */ | |
1099 | const char *rtld_soname | |
1100 | = ((const char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB]) | |
1101 | + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_val); | |
1102 | if (main_map->l_info[DT_SONAME] != NULL | |
1103 | && strcmp (rtld_soname, | |
1104 | ((const char *) D_PTR (main_map, l_info[DT_STRTAB]) | |
1105 | + main_map->l_info[DT_SONAME]->d_un.d_val)) == 0) | |
1106 | _dl_fatal_printf ("%s: loader cannot load itself\n", rtld_soname); | |
1107 | ||
1108 | /* With DT_NEEDED dependencies, the executable is dynamically | |
1109 | linked. */ | |
1110 | if (__glibc_unlikely (main_map->l_info[DT_NEEDED] != NULL)) | |
1111 | return; | |
1112 | ||
1113 | /* If the executable has program interpreter, it is dynamically | |
1114 | linked. */ | |
1115 | for (size_t i = 0; i < main_map->l_phnum; ++i) | |
1116 | if (main_map->l_phdr[i].p_type == PT_INTERP) | |
1117 | return; | |
1118 | ||
1119 | const char *pathname = _dl_argv[0]; | |
1120 | if (argv0 != NULL) | |
1121 | _dl_argv[0] = argv0; | |
1122 | int errcode = __rtld_execve (pathname, _dl_argv, _environ); | |
1123 | const char *errname = strerrorname_np (errcode); | |
1124 | if (errname != NULL) | |
1125 | _dl_fatal_printf("%s: cannot execute %s: %s\n", | |
1126 | rtld_soname, pathname, errname); | |
1127 | else | |
1128 | _dl_fatal_printf("%s: cannot execute %s: %d\n", | |
f1eeef94 | 1129 | rtld_soname, pathname, errcode); |
c1cb2dee FW |
1130 | } |
1131 | ||
b4d4ff89 FW |
1132 | /* Called to complete the initialization of the link map for the main |
1133 | executable. Returns true if there is a PT_INTERP segment. */ | |
1134 | static bool | |
1135 | rtld_setup_main_map (struct link_map *main_map) | |
1136 | { | |
1137 | /* This have already been filled in right after _dl_new_object, or | |
1138 | as part of _dl_map_object. */ | |
1139 | const ElfW(Phdr) *phdr = main_map->l_phdr; | |
1140 | ElfW(Word) phnum = main_map->l_phnum; | |
1141 | ||
1142 | bool has_interp = false; | |
1143 | ||
1144 | main_map->l_map_end = 0; | |
1145 | main_map->l_text_end = 0; | |
1146 | /* Perhaps the executable has no PT_LOAD header entries at all. */ | |
1147 | main_map->l_map_start = ~0; | |
1148 | /* And it was opened directly. */ | |
1149 | ++main_map->l_direct_opencount; | |
8eb2510d FW |
1150 | main_map->l_contiguous = 1; |
1151 | ||
1152 | /* A PT_LOAD segment at an unexpected address will clear the | |
1153 | l_contiguous flag. The ELF specification says that PT_LOAD | |
1154 | segments need to be sorted in in increasing order, but perhaps | |
1155 | not all executables follow this requirement. Having l_contiguous | |
1156 | equal to 1 is just an optimization, so the code below does not | |
1157 | try to sort the segments in case they are unordered. | |
1158 | ||
1159 | There is one corner case in which l_contiguous is not set to 1, | |
1160 | but where it could be set: If a PIE (ET_DYN) binary is loaded by | |
1161 | glibc itself (not the kernel), it is always contiguous due to the | |
1162 | way the glibc loader works. However, the kernel loader may still | |
1163 | create holes in this case, and the code here still uses 0 | |
1164 | conservatively for the glibc-loaded case, too. */ | |
1165 | ElfW(Addr) expected_load_address = 0; | |
b4d4ff89 FW |
1166 | |
1167 | /* Scan the program header table for the dynamic section. */ | |
1168 | for (const ElfW(Phdr) *ph = phdr; ph < &phdr[phnum]; ++ph) | |
1169 | switch (ph->p_type) | |
1170 | { | |
1171 | case PT_PHDR: | |
1172 | /* Find out the load address. */ | |
1173 | main_map->l_addr = (ElfW(Addr)) phdr - ph->p_vaddr; | |
1174 | break; | |
1175 | case PT_DYNAMIC: | |
1176 | /* This tells us where to find the dynamic section, | |
1177 | which tells us everything we need to do. */ | |
1178 | main_map->l_ld = (void *) main_map->l_addr + ph->p_vaddr; | |
1179 | main_map->l_ld_readonly = (ph->p_flags & PF_W) == 0; | |
1180 | break; | |
1181 | case PT_INTERP: | |
1182 | /* This "interpreter segment" was used by the program loader to | |
1183 | find the program interpreter, which is this program itself, the | |
1184 | dynamic linker. We note what name finds us, so that a future | |
1185 | dlopen call or DT_NEEDED entry, for something that wants to link | |
1186 | against the dynamic linker as a shared library, will know that | |
1187 | the shared object is already loaded. */ | |
1188 | _dl_rtld_libname.name = ((const char *) main_map->l_addr | |
1189 | + ph->p_vaddr); | |
1190 | /* _dl_rtld_libname.next = NULL; Already zero. */ | |
1191 | GL(dl_rtld_map).l_libname = &_dl_rtld_libname; | |
1192 | ||
1193 | /* Ordinarilly, we would get additional names for the loader from | |
1194 | our DT_SONAME. This can't happen if we were actually linked as | |
1195 | a static executable (detect this case when we have no DYNAMIC). | |
1196 | If so, assume the filename component of the interpreter path to | |
1197 | be our SONAME, and add it to our name list. */ | |
1198 | if (GL(dl_rtld_map).l_ld == NULL) | |
1199 | { | |
1200 | const char *p = NULL; | |
1201 | const char *cp = _dl_rtld_libname.name; | |
1202 | ||
1203 | /* Find the filename part of the path. */ | |
1204 | while (*cp != '\0') | |
1205 | if (*cp++ == '/') | |
1206 | p = cp; | |
1207 | ||
1208 | if (p != NULL) | |
1209 | { | |
1210 | _dl_rtld_libname2.name = p; | |
1211 | /* _dl_rtld_libname2.next = NULL; Already zero. */ | |
1212 | _dl_rtld_libname.next = &_dl_rtld_libname2; | |
1213 | } | |
1214 | } | |
1215 | ||
1216 | has_interp = true; | |
1217 | break; | |
1218 | case PT_LOAD: | |
1219 | { | |
1220 | ElfW(Addr) mapstart; | |
1221 | ElfW(Addr) allocend; | |
1222 | ||
1223 | /* Remember where the main program starts in memory. */ | |
1224 | mapstart = (main_map->l_addr | |
1225 | + (ph->p_vaddr & ~(GLRO(dl_pagesize) - 1))); | |
1226 | if (main_map->l_map_start > mapstart) | |
1227 | main_map->l_map_start = mapstart; | |
1228 | ||
8eb2510d FW |
1229 | if (main_map->l_contiguous && expected_load_address != 0 |
1230 | && expected_load_address != mapstart) | |
1231 | main_map->l_contiguous = 0; | |
1232 | ||
b4d4ff89 FW |
1233 | /* Also where it ends. */ |
1234 | allocend = main_map->l_addr + ph->p_vaddr + ph->p_memsz; | |
1235 | if (main_map->l_map_end < allocend) | |
1236 | main_map->l_map_end = allocend; | |
1237 | if ((ph->p_flags & PF_X) && allocend > main_map->l_text_end) | |
1238 | main_map->l_text_end = allocend; | |
8eb2510d FW |
1239 | |
1240 | /* The next expected address is the page following this load | |
1241 | segment. */ | |
1242 | expected_load_address = ((allocend + GLRO(dl_pagesize) - 1) | |
1243 | & ~(GLRO(dl_pagesize) - 1)); | |
b4d4ff89 FW |
1244 | } |
1245 | break; | |
1246 | ||
1247 | case PT_TLS: | |
1248 | if (ph->p_memsz > 0) | |
1249 | { | |
1250 | /* Note that in the case the dynamic linker we duplicate work | |
1251 | here since we read the PT_TLS entry already in | |
1252 | _dl_start_final. But the result is repeatable so do not | |
1253 | check for this special but unimportant case. */ | |
1254 | main_map->l_tls_blocksize = ph->p_memsz; | |
1255 | main_map->l_tls_align = ph->p_align; | |
1256 | if (ph->p_align == 0) | |
1257 | main_map->l_tls_firstbyte_offset = 0; | |
1258 | else | |
1259 | main_map->l_tls_firstbyte_offset = (ph->p_vaddr | |
1260 | & (ph->p_align - 1)); | |
1261 | main_map->l_tls_initimage_size = ph->p_filesz; | |
1262 | main_map->l_tls_initimage = (void *) ph->p_vaddr; | |
1263 | ||
1264 | /* This image gets the ID one. */ | |
1265 | GL(dl_tls_max_dtv_idx) = main_map->l_tls_modid = 1; | |
1266 | } | |
1267 | break; | |
1268 | ||
1269 | case PT_GNU_STACK: | |
1270 | GL(dl_stack_flags) = ph->p_flags; | |
1271 | break; | |
1272 | ||
1273 | case PT_GNU_RELRO: | |
1274 | main_map->l_relro_addr = ph->p_vaddr; | |
1275 | main_map->l_relro_size = ph->p_memsz; | |
1276 | break; | |
1277 | } | |
1278 | /* Process program headers again, but scan them backwards so | |
1279 | that PT_NOTE can be skipped if PT_GNU_PROPERTY exits. */ | |
1280 | for (const ElfW(Phdr) *ph = &phdr[phnum]; ph != phdr; --ph) | |
1281 | switch (ph[-1].p_type) | |
1282 | { | |
1283 | case PT_NOTE: | |
1284 | _dl_process_pt_note (main_map, -1, &ph[-1]); | |
1285 | break; | |
1286 | case PT_GNU_PROPERTY: | |
1287 | _dl_process_pt_gnu_property (main_map, -1, &ph[-1]); | |
1288 | break; | |
1289 | } | |
1290 | ||
1291 | /* Adjust the address of the TLS initialization image in case | |
1292 | the executable is actually an ET_DYN object. */ | |
1293 | if (main_map->l_tls_initimage != NULL) | |
1294 | main_map->l_tls_initimage | |
1295 | = (char *) main_map->l_tls_initimage + main_map->l_addr; | |
1296 | if (! main_map->l_map_end) | |
1297 | main_map->l_map_end = ~0; | |
1298 | if (! main_map->l_text_end) | |
1299 | main_map->l_text_end = ~0; | |
1300 | if (! GL(dl_rtld_map).l_libname && GL(dl_rtld_map).l_name) | |
1301 | { | |
1302 | /* We were invoked directly, so the program might not have a | |
1303 | PT_INTERP. */ | |
1304 | _dl_rtld_libname.name = GL(dl_rtld_map).l_name; | |
1305 | /* _dl_rtld_libname.next = NULL; Already zero. */ | |
1306 | GL(dl_rtld_map).l_libname = &_dl_rtld_libname; | |
1307 | } | |
1308 | else | |
1309 | assert (GL(dl_rtld_map).l_libname); /* How else did we get here? */ | |
1310 | ||
1311 | return has_interp; | |
1312 | } | |
1313 | ||
d66e34cd | 1314 | static void |
266180eb | 1315 | dl_main (const ElfW(Phdr) *phdr, |
72f70279 | 1316 | ElfW(Word) phnum, |
3a56ea26 AK |
1317 | ElfW(Addr) *user_entry, |
1318 | ElfW(auxv_t) *auxv) | |
d66e34cd | 1319 | { |
c0f62c56 | 1320 | struct link_map *main_map; |
14bab8de UD |
1321 | size_t file_size; |
1322 | char *file; | |
77aba05b | 1323 | unsigned int i; |
164a7164 UD |
1324 | bool prelinked = false; |
1325 | bool rtld_is_main = false; | |
9dcafc55 | 1326 | void *tcbp = NULL; |
d66e34cd | 1327 | |
2bf9e641 FW |
1328 | struct dl_main_state state; |
1329 | dl_main_state_init (&state); | |
4c6e0415 | 1330 | |
d017b0ab | 1331 | __tls_pre_init_tp (); |
334fcf2a | 1332 | |
2dd87703 | 1333 | #if !PTHREAD_IN_LIBC |
c70ba488 RM |
1334 | /* The explicit initialization here is cheaper than processing the reloc |
1335 | in the _rtld_local definition's initializer. */ | |
1336 | GL(dl_make_stack_executable_hook) = &_dl_make_stack_executable; | |
2dd87703 | 1337 | #endif |
c70ba488 | 1338 | |
ea278354 | 1339 | /* Process the environment variable which control the behaviour. */ |
2bf9e641 | 1340 | process_envvars (&state); |
3996f34b | 1341 | |
ce6e047f | 1342 | #ifndef HAVE_INLINED_SYSCALLS |
46ec036d | 1343 | /* Set up a flag which tells we are just starting. */ |
9cf27b8d | 1344 | _dl_starting_up = 1; |
ce6e047f | 1345 | #endif |
46ec036d | 1346 | |
e0f1a58f | 1347 | const char *ld_so_name = _dl_argv[0]; |
a16956f3 | 1348 | if (*user_entry == (ElfW(Addr)) ENTRY_POINT) |
0200214b RM |
1349 | { |
1350 | /* Ho ho. We are not the program interpreter! We are the program | |
1351 | itself! This means someone ran ld.so as a command. Well, that | |
1352 | might be convenient to do sometimes. We support it by | |
1353 | interpreting the args like this: | |
1354 | ||
1355 | ld.so PROGRAM ARGS... | |
1356 | ||
1357 | The first argument is the name of a file containing an ELF | |
1358 | executable we will load and run with the following arguments. | |
1359 | To simplify life here, PROGRAM is searched for using the | |
1360 | normal rules for shared objects, rather than $PATH or anything | |
1361 | like that. We just load it and use its entry point; we don't | |
1362 | pay attention to its PT_INTERP command (we are the interpreter | |
1363 | ourselves). This is an easy way to test a new ld.so before | |
1364 | installing it. */ | |
164a7164 | 1365 | rtld_is_main = true; |
421f82e5 | 1366 | |
c6702789 VM |
1367 | char *argv0 = NULL; |
1368 | ||
ffee1316 | 1369 | /* Note the place where the dynamic linker actually came from. */ |
e6caf4e1 | 1370 | GL(dl_rtld_map).l_name = rtld_progname; |
6a76c115 | 1371 | |
fd26970f | 1372 | while (_dl_argc > 1) |
4243cbea | 1373 | if (! strcmp (_dl_argv[1], "--list")) |
fd26970f | 1374 | { |
e0f1a58f FW |
1375 | if (state.mode != rtld_mode_help) |
1376 | { | |
1377 | state.mode = rtld_mode_list; | |
1378 | /* This means do no dependency analysis. */ | |
1379 | GLRO(dl_lazy) = -1; | |
1380 | } | |
61965e9b | 1381 | |
fd26970f UD |
1382 | ++_dl_skip_args; |
1383 | --_dl_argc; | |
4243cbea | 1384 | ++_dl_argv; |
fd26970f | 1385 | } |
4243cbea | 1386 | else if (! strcmp (_dl_argv[1], "--verify")) |
fd26970f | 1387 | { |
e0f1a58f FW |
1388 | if (state.mode != rtld_mode_help) |
1389 | state.mode = rtld_mode_verify; | |
6a76c115 | 1390 | |
73d65cc3 SP |
1391 | ++_dl_skip_args; |
1392 | --_dl_argc; | |
4243cbea | 1393 | ++_dl_argv; |
73d65cc3 | 1394 | } |
4243cbea | 1395 | else if (! strcmp (_dl_argv[1], "--inhibit-cache")) |
73d65cc3 SP |
1396 | { |
1397 | GLRO(dl_inhibit_cache) = 1; | |
fd26970f UD |
1398 | ++_dl_skip_args; |
1399 | --_dl_argc; | |
4243cbea | 1400 | ++_dl_argv; |
fd26970f | 1401 | } |
4243cbea | 1402 | else if (! strcmp (_dl_argv[1], "--library-path") |
e6caf4e1 | 1403 | && _dl_argc > 2) |
880f421f | 1404 | { |
2bf9e641 | 1405 | state.library_path = _dl_argv[2]; |
27316f4a | 1406 | state.library_path_source = "--library-path"; |
880f421f | 1407 | |
310930c1 UD |
1408 | _dl_skip_args += 2; |
1409 | _dl_argc -= 2; | |
4243cbea | 1410 | _dl_argv += 2; |
310930c1 | 1411 | } |
4243cbea | 1412 | else if (! strcmp (_dl_argv[1], "--inhibit-rpath") |
e6caf4e1 | 1413 | && _dl_argc > 2) |
310930c1 | 1414 | { |
4243cbea | 1415 | GLRO(dl_inhibit_rpath) = _dl_argv[2]; |
310930c1 | 1416 | |
74780cf6 UD |
1417 | _dl_skip_args += 2; |
1418 | _dl_argc -= 2; | |
4243cbea | 1419 | _dl_argv += 2; |
74780cf6 | 1420 | } |
4243cbea | 1421 | else if (! strcmp (_dl_argv[1], "--audit") && _dl_argc > 2) |
74780cf6 | 1422 | { |
2bf9e641 | 1423 | audit_list_add_string (&state.audit_list, _dl_argv[2]); |
74780cf6 | 1424 | |
8692ebdb DN |
1425 | _dl_skip_args += 2; |
1426 | _dl_argc -= 2; | |
1427 | _dl_argv += 2; | |
1428 | } | |
1429 | else if (! strcmp (_dl_argv[1], "--preload") && _dl_argc > 2) | |
1430 | { | |
2bf9e641 | 1431 | state.preloadarg = _dl_argv[2]; |
c6702789 VM |
1432 | _dl_skip_args += 2; |
1433 | _dl_argc -= 2; | |
1434 | _dl_argv += 2; | |
1435 | } | |
1436 | else if (! strcmp (_dl_argv[1], "--argv0") && _dl_argc > 2) | |
1437 | { | |
1438 | argv0 = _dl_argv[2]; | |
1439 | ||
dad90d52 FW |
1440 | _dl_skip_args += 2; |
1441 | _dl_argc -= 2; | |
1442 | _dl_argv += 2; | |
1443 | } | |
1444 | else if (strcmp (_dl_argv[1], "--glibc-hwcaps-prepend") == 0 | |
1445 | && _dl_argc > 2) | |
1446 | { | |
1447 | state.glibc_hwcaps_prepend = _dl_argv[2]; | |
1448 | _dl_skip_args += 2; | |
1449 | _dl_argc -= 2; | |
1450 | _dl_argv += 2; | |
1451 | } | |
1452 | else if (strcmp (_dl_argv[1], "--glibc-hwcaps-mask") == 0 | |
1453 | && _dl_argc > 2) | |
1454 | { | |
1455 | state.glibc_hwcaps_mask = _dl_argv[2]; | |
880f421f UD |
1456 | _dl_skip_args += 2; |
1457 | _dl_argc -= 2; | |
4243cbea | 1458 | _dl_argv += 2; |
880f421f | 1459 | } |
86f65dff L |
1460 | #if HAVE_TUNABLES |
1461 | else if (! strcmp (_dl_argv[1], "--list-tunables")) | |
1462 | { | |
1463 | state.mode = rtld_mode_list_tunables; | |
1464 | ||
1465 | ++_dl_skip_args; | |
1466 | --_dl_argc; | |
1467 | ++_dl_argv; | |
1468 | } | |
1469 | #endif | |
851f32cf FW |
1470 | else if (! strcmp (_dl_argv[1], "--list-diagnostics")) |
1471 | { | |
1472 | state.mode = rtld_mode_list_diagnostics; | |
1473 | ||
1474 | ++_dl_skip_args; | |
1475 | --_dl_argc; | |
1476 | ++_dl_argv; | |
1477 | } | |
e0f1a58f FW |
1478 | else if (strcmp (_dl_argv[1], "--help") == 0) |
1479 | { | |
1480 | state.mode = rtld_mode_help; | |
1481 | --_dl_argc; | |
1482 | ++_dl_argv; | |
1483 | } | |
542923d9 FW |
1484 | else if (strcmp (_dl_argv[1], "--version") == 0) |
1485 | _dl_version (); | |
e0f1a58f FW |
1486 | else if (_dl_argv[1][0] == '-' && _dl_argv[1][1] == '-') |
1487 | { | |
1488 | if (_dl_argv[1][1] == '\0') | |
1489 | /* End of option list. */ | |
1490 | break; | |
1491 | else | |
1492 | /* Unrecognized option. */ | |
1493 | _dl_usage (ld_so_name, _dl_argv[1]); | |
1494 | } | |
fd26970f UD |
1495 | else |
1496 | break; | |
d66e34cd | 1497 | |
86f65dff L |
1498 | #if HAVE_TUNABLES |
1499 | if (__glibc_unlikely (state.mode == rtld_mode_list_tunables)) | |
1500 | { | |
1501 | __tunables_print (); | |
1502 | _exit (0); | |
1503 | } | |
1504 | #endif | |
1505 | ||
851f32cf FW |
1506 | if (state.mode == rtld_mode_list_diagnostics) |
1507 | _dl_print_diagnostics (_environ); | |
1508 | ||
61eb22d3 UD |
1509 | /* If we have no further argument the program was called incorrectly. |
1510 | Grant the user some education. */ | |
1511 | if (_dl_argc < 2) | |
e0f1a58f FW |
1512 | { |
1513 | if (state.mode == rtld_mode_help) | |
1514 | /* --help without an executable is not an error. */ | |
1515 | _dl_help (ld_so_name, &state); | |
1516 | else | |
1517 | _dl_usage (ld_so_name, NULL); | |
1518 | } | |
61eb22d3 | 1519 | |
0200214b RM |
1520 | ++_dl_skip_args; |
1521 | --_dl_argc; | |
4243cbea | 1522 | ++_dl_argv; |
91f62ce6 | 1523 | |
c70ba488 RM |
1524 | /* The initialization of _dl_stack_flags done below assumes the |
1525 | executable's PT_GNU_STACK may have been honored by the kernel, and | |
1526 | so a PT_GNU_STACK with PF_X set means the stack started out with | |
1527 | execute permission. However, this is not really true if the | |
1528 | dynamic linker is the executable the kernel loaded. For this | |
1529 | case, we must reinitialize _dl_stack_flags to match the dynamic | |
1530 | linker itself. If the dynamic linker was built with a | |
1531 | PT_GNU_STACK, then the kernel may have loaded us with a | |
1532 | nonexecutable stack that we will have to make executable when we | |
1533 | load the program below unless it has a PT_GNU_STACK indicating | |
1534 | nonexecutable stack is ok. */ | |
1535 | ||
b4d4ff89 | 1536 | for (const ElfW(Phdr) *ph = phdr; ph < &phdr[phnum]; ++ph) |
c70ba488 RM |
1537 | if (ph->p_type == PT_GNU_STACK) |
1538 | { | |
1539 | GL(dl_stack_flags) = ph->p_flags; | |
1540 | break; | |
1541 | } | |
1542 | ||
e0f1a58f FW |
1543 | if (__glibc_unlikely (state.mode == rtld_mode_verify |
1544 | || state.mode == rtld_mode_help)) | |
2de99474 | 1545 | { |
8e17ea58 UD |
1546 | const char *objname; |
1547 | const char *err_str = NULL; | |
993b3242 | 1548 | struct map_args args; |
74780cf6 | 1549 | bool malloced; |
2de99474 | 1550 | |
e6caf4e1 | 1551 | args.str = rtld_progname; |
f04b9a68 | 1552 | args.loader = NULL; |
f04b9a68 | 1553 | args.mode = __RTLD_OPENEXEC; |
74780cf6 UD |
1554 | (void) _dl_catch_error (&objname, &err_str, &malloced, map_doit, |
1555 | &args); | |
a1ffb40e | 1556 | if (__glibc_unlikely (err_str != NULL)) |
e0f1a58f FW |
1557 | { |
1558 | /* We don't free the returned string, the programs stops | |
1559 | anyway. */ | |
1560 | if (state.mode == rtld_mode_help) | |
1561 | /* Mask the failure to load the main object. The help | |
1562 | message contains less information in this case. */ | |
1563 | _dl_help (ld_so_name, &state); | |
1564 | else | |
1565 | _exit (EXIT_FAILURE); | |
1566 | } | |
2de99474 UD |
1567 | } |
1568 | else | |
db276fa1 | 1569 | { |
1e372ded AZ |
1570 | RTLD_TIMING_VAR (start); |
1571 | rtld_timer_start (&start); | |
798212a0 | 1572 | _dl_map_object (NULL, rtld_progname, lt_executable, 0, |
c0f62c56 | 1573 | __RTLD_OPENEXEC, LM_ID_BASE); |
1e372ded | 1574 | rtld_timer_stop (&load_time, start); |
db276fa1 | 1575 | } |
2de99474 | 1576 | |
c0f62c56 UD |
1577 | /* Now the map for the main executable is available. */ |
1578 | main_map = GL(dl_ns)[LM_ID_BASE]._ns_loaded; | |
1579 | ||
c1cb2dee FW |
1580 | if (__glibc_likely (state.mode == rtld_mode_normal)) |
1581 | rtld_chain_load (main_map, argv0); | |
01f16ab0 | 1582 | |
c0f62c56 UD |
1583 | phdr = main_map->l_phdr; |
1584 | phnum = main_map->l_phnum; | |
143e2b96 UD |
1585 | /* We overwrite here a pointer to a malloc()ed string. But since |
1586 | the malloc() implementation used at this point is the dummy | |
1587 | implementations which has no real free() function it does not | |
1588 | makes sense to free the old string first. */ | |
c0f62c56 UD |
1589 | main_map->l_name = (char *) ""; |
1590 | *user_entry = main_map->l_entry; | |
3a56ea26 | 1591 | |
15a0c573 CLT |
1592 | /* Set bit indicating this is the main program map. */ |
1593 | main_map->l_main_map = 1; | |
1594 | ||
bc58236c | 1595 | #ifdef HAVE_AUX_VECTOR |
3a56ea26 AK |
1596 | /* Adjust the on-stack auxiliary vector so that it looks like the |
1597 | binary was executed directly. */ | |
bc58236c | 1598 | for (ElfW(auxv_t) *av = auxv; av->a_type != AT_NULL; av++) |
3a56ea26 AK |
1599 | switch (av->a_type) |
1600 | { | |
1601 | case AT_PHDR: | |
4dd019e3 | 1602 | av->a_un.a_val = (uintptr_t) phdr; |
3a56ea26 AK |
1603 | break; |
1604 | case AT_PHNUM: | |
1605 | av->a_un.a_val = phnum; | |
1606 | break; | |
1607 | case AT_ENTRY: | |
1608 | av->a_un.a_val = *user_entry; | |
1609 | break; | |
5c349950 PP |
1610 | case AT_EXECFN: |
1611 | av->a_un.a_val = (uintptr_t) _dl_argv[0]; | |
1612 | break; | |
3a56ea26 | 1613 | } |
bc58236c | 1614 | #endif |
c6702789 VM |
1615 | |
1616 | /* Set the argv[0] string now that we've processed the executable. */ | |
1617 | if (argv0 != NULL) | |
1618 | _dl_argv[0] = argv0; | |
0200214b RM |
1619 | } |
1620 | else | |
1621 | { | |
1622 | /* Create a link_map for the executable itself. | |
1623 | This will be what dlopen on "" returns. */ | |
9fbdeb41 UD |
1624 | main_map = _dl_new_object ((char *) "", "", lt_executable, NULL, |
1625 | __RTLD_OPENEXEC, LM_ID_BASE); | |
9dcafc55 | 1626 | assert (main_map != NULL); |
c0f62c56 UD |
1627 | main_map->l_phdr = phdr; |
1628 | main_map->l_phnum = phnum; | |
1629 | main_map->l_entry = *user_entry; | |
da832465 | 1630 | |
f0967738 AK |
1631 | /* Even though the link map is not yet fully initialized we can add |
1632 | it to the map list since there are no possible users running yet. */ | |
1633 | _dl_add_to_namespace_list (main_map, LM_ID_BASE); | |
fa41c84d | 1634 | assert (main_map == GL(dl_ns)[LM_ID_BASE]._ns_loaded); |
f0967738 | 1635 | |
61e0617a UD |
1636 | /* At this point we are in a bit of trouble. We would have to |
1637 | fill in the values for l_dev and l_ino. But in general we | |
1638 | do not know where the file is. We also do not handle AT_EXECFD | |
1639 | even if it would be passed up. | |
1640 | ||
1641 | We leave the values here defined to 0. This is normally no | |
1642 | problem as the program code itself is normally no shared | |
1643 | object and therefore cannot be loaded dynamically. Nothing | |
1644 | prevent the use of dynamic binaries and in these situations | |
1645 | we might get problems. We might not be able to find out | |
1646 | whether the object is already loaded. But since there is no | |
1647 | easy way out and because the dynamic binary must also not | |
1648 | have an SONAME we ignore this program for now. If it becomes | |
1649 | a problem we can force people using SONAMEs. */ | |
1650 | ||
97a51d8a UD |
1651 | /* We delay initializing the path structure until we got the dynamic |
1652 | information for the program. */ | |
0200214b RM |
1653 | } |
1654 | ||
b4d4ff89 | 1655 | bool has_interp = rtld_setup_main_map (main_map); |
0200214b | 1656 | |
9dcafc55 UD |
1657 | /* If the current libname is different from the SONAME, add the |
1658 | latter as well. */ | |
1659 | if (GL(dl_rtld_map).l_info[DT_SONAME] != NULL | |
1660 | && strcmp (GL(dl_rtld_map).l_libname->name, | |
1661 | (const char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB]) | |
1662 | + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_val) != 0) | |
1663 | { | |
1664 | static struct libname_list newname; | |
1665 | newname.name = ((char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB]) | |
1666 | + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_ptr); | |
1667 | newname.next = NULL; | |
1668 | newname.dont_free = 1; | |
1669 | ||
1670 | assert (GL(dl_rtld_map).l_libname->next == NULL); | |
1671 | GL(dl_rtld_map).l_libname->next = &newname; | |
1672 | } | |
1673 | /* The ld.so must be relocated since otherwise loading audit modules | |
1674 | will fail since they reuse the very same ld.so. */ | |
1675 | assert (GL(dl_rtld_map).l_relocated); | |
1676 | ||
9a51759b UD |
1677 | if (! rtld_is_main) |
1678 | { | |
1679 | /* Extract the contents of the dynamic section for easy access. */ | |
5118dcac | 1680 | elf_get_dynamic_info (main_map, false, false); |
4d098554 FW |
1681 | |
1682 | /* If the main map is libc.so, update the base namespace to | |
1683 | refer to this map. If libc.so is loaded later, this happens | |
1684 | in _dl_map_object_from_fd. */ | |
1685 | if (main_map->l_info[DT_SONAME] != NULL | |
1686 | && (strcmp (((const char *) D_PTR (main_map, l_info[DT_STRTAB]) | |
1687 | + main_map->l_info[DT_SONAME]->d_un.d_val), LIBC_SO) | |
1688 | == 0)) | |
1689 | GL(dl_ns)[LM_ID_BASE].libc_map = main_map; | |
1690 | ||
efec5079 | 1691 | /* Set up our cache of pointers into the hash table. */ |
c0f62c56 | 1692 | _dl_setup_hash (main_map); |
9a51759b | 1693 | } |
0200214b | 1694 | |
2bf9e641 | 1695 | if (__glibc_unlikely (state.mode == rtld_mode_verify)) |
e2102c14 UD |
1696 | { |
1697 | /* We were called just to verify that this is a dynamic | |
1698 | executable using us as the program interpreter. Exit with an | |
1699 | error if we were not able to load the binary or no interpreter | |
1700 | is specified (i.e., this is no dynamically linked binary. */ | |
c0f62c56 | 1701 | if (main_map->l_ld == NULL) |
e2102c14 | 1702 | _exit (1); |
e2102c14 UD |
1703 | |
1704 | /* We allow here some platform specific code. */ | |
1705 | #ifdef DISTINGUISH_LIB_VERSIONS | |
1706 | DISTINGUISH_LIB_VERSIONS; | |
1707 | #endif | |
eb406346 | 1708 | _exit (has_interp ? 0 : 2); |
e2102c14 UD |
1709 | } |
1710 | ||
ab1d521d | 1711 | struct link_map **first_preload = &GL(dl_rtld_map).l_next; |
ab1d521d RM |
1712 | /* Set up the data structures for the system-supplied DSO early, |
1713 | so they can influence _dl_init_paths. */ | |
9cee5585 | 1714 | setup_vdso (main_map, &first_preload); |
ab1d521d | 1715 | |
1bdda52f AZ |
1716 | /* With vDSO setup we can initialize the function pointers. */ |
1717 | setup_vdso_pointers (); | |
1718 | ||
ab1d521d | 1719 | #ifdef DL_SYSDEP_OSCHECK |
ceb809dc | 1720 | DL_SYSDEP_OSCHECK (_dl_fatal_printf); |
ab1d521d RM |
1721 | #endif |
1722 | ||
1723 | /* Initialize the data structures for the search paths for shared | |
1724 | objects. */ | |
2bf9e641 | 1725 | call_init_paths (&state); |
97a51d8a | 1726 | |
a93d9e03 | 1727 | /* Initialize _r_debug_extended. */ |
29f97654 UD |
1728 | struct r_debug *r = _dl_debug_initialize (GL(dl_rtld_map).l_addr, |
1729 | LM_ID_BASE); | |
9dcafc55 UD |
1730 | r->r_state = RT_CONSISTENT; |
1731 | ||
0200214b | 1732 | /* Put the link_map for ourselves on the chain so it can be found by |
ceb2d9aa | 1733 | name. Note that at this point the global chain of link maps contains |
d6b5d570 UD |
1734 | exactly one element, which is pointed to by dl_loaded. */ |
1735 | if (! GL(dl_rtld_map).l_name) | |
ffee1316 RM |
1736 | /* If not invoked directly, the dynamic linker shared object file was |
1737 | found by the PT_INTERP name. */ | |
d6b5d570 UD |
1738 | GL(dl_rtld_map).l_name = (char *) GL(dl_rtld_map).l_libname->name; |
1739 | GL(dl_rtld_map).l_type = lt_library; | |
c0f62c56 UD |
1740 | main_map->l_next = &GL(dl_rtld_map); |
1741 | GL(dl_rtld_map).l_prev = main_map; | |
1742 | ++GL(dl_ns)[LM_ID_BASE]._ns_nloaded; | |
e8ed861d | 1743 | ++GL(dl_load_adds); |
0200214b | 1744 | |
97fd3a30 UD |
1745 | /* If LD_USE_LOAD_BIAS env variable has not been seen, default |
1746 | to not using bias for non-prelinked PIEs and libraries | |
1747 | and using it for executables or prelinked PIEs or libraries. */ | |
afdca0f2 | 1748 | if (GLRO(dl_use_load_bias) == (ElfW(Addr)) -2) |
c0f62c56 | 1749 | GLRO(dl_use_load_bias) = main_map->l_addr == 0 ? -1 : 0; |
97fd3a30 | 1750 | |
44c4e5d5 RM |
1751 | /* Starting from binutils-2.23, the linker will define the magic symbol |
1752 | __ehdr_start to point to our own ELF header if it is visible in a | |
1753 | segment that also includes the phdrs. If that's not available, we use | |
1754 | the old method that assumes the beginning of the file is part of the | |
1755 | lowest-addressed PT_LOAD segment. */ | |
44c4e5d5 | 1756 | extern const ElfW(Ehdr) __ehdr_start __attribute__ ((visibility ("hidden"))); |
302247c8 FS |
1757 | |
1758 | /* Set up the program header information for the dynamic linker | |
1759 | itself. It is needed in the dl_iterate_phdr callbacks. */ | |
1760 | const ElfW(Ehdr) *rtld_ehdr = &__ehdr_start; | |
44c4e5d5 RM |
1761 | assert (rtld_ehdr->e_ehsize == sizeof *rtld_ehdr); |
1762 | assert (rtld_ehdr->e_phentsize == sizeof (ElfW(Phdr))); | |
1763 | ||
1764 | const ElfW(Phdr) *rtld_phdr = (const void *) rtld_ehdr + rtld_ehdr->e_phoff; | |
1765 | ||
e8ed861d | 1766 | GL(dl_rtld_map).l_phdr = rtld_phdr; |
553eca26 UD |
1767 | GL(dl_rtld_map).l_phnum = rtld_ehdr->e_phnum; |
1768 | ||
9dcafc55 | 1769 | |
e8ed861d UD |
1770 | /* PT_GNU_RELRO is usually the last phdr. */ |
1771 | size_t cnt = rtld_ehdr->e_phnum; | |
1772 | while (cnt-- > 0) | |
1773 | if (rtld_phdr[cnt].p_type == PT_GNU_RELRO) | |
1774 | { | |
1775 | GL(dl_rtld_map).l_relro_addr = rtld_phdr[cnt].p_vaddr; | |
1776 | GL(dl_rtld_map).l_relro_size = rtld_phdr[cnt].p_memsz; | |
1777 | break; | |
1778 | } | |
1779 | ||
9dcafc55 UD |
1780 | /* Add the dynamic linker to the TLS list if it also uses TLS. */ |
1781 | if (GL(dl_rtld_map).l_tls_blocksize != 0) | |
1782 | /* Assign a module ID. Do this before loading any audit modules. */ | |
ba33937b | 1783 | _dl_assign_tls_modid (&GL(dl_rtld_map)); |
9dcafc55 | 1784 | |
2bf9e641 FW |
1785 | audit_list_add_dynamic_tag (&state.audit_list, main_map, DT_AUDIT); |
1786 | audit_list_add_dynamic_tag (&state.audit_list, main_map, DT_DEPAUDIT); | |
8f7a75d7 | 1787 | |
e0f1a58f FW |
1788 | /* At this point, all data has been obtained that is included in the |
1789 | --help output. */ | |
1790 | if (__glibc_unlikely (state.mode == rtld_mode_help)) | |
1791 | _dl_help (ld_so_name, &state); | |
1792 | ||
9dcafc55 | 1793 | /* If we have auditing DSOs to load, do it now. */ |
81b82fb9 | 1794 | bool need_security_init = true; |
2bf9e641 | 1795 | if (state.audit_list.length > 0) |
9dcafc55 | 1796 | { |
2bf9e641 | 1797 | size_t naudit = audit_list_count (&state.audit_list); |
17796419 | 1798 | |
3abee0b7 UD |
1799 | /* Since we start using the auditing DSOs right away we need to |
1800 | initialize the data structures now. */ | |
17796419 | 1801 | tcbp = init_tls (naudit); |
3abee0b7 | 1802 | |
4c48ef06 UD |
1803 | /* Initialize security features. We need to do it this early |
1804 | since otherwise the constructors of the audit libraries will | |
1805 | use different values (especially the pointer guard) and will | |
1806 | fail later on. */ | |
1807 | security_init (); | |
81b82fb9 | 1808 | need_security_init = false; |
4c48ef06 | 1809 | |
2bf9e641 | 1810 | load_audit_modules (main_map, &state.audit_list); |
17796419 SN |
1811 | |
1812 | /* The count based on audit strings may overestimate the number | |
1813 | of audit modules that got loaded, but not underestimate. */ | |
1814 | assert (GLRO(dl_naudit) <= naudit); | |
9dcafc55 UD |
1815 | } |
1816 | ||
d0503676 CD |
1817 | /* Keep track of the currently loaded modules to count how many |
1818 | non-audit modules which use TLS are loaded. */ | |
1819 | size_t count_modids = _dl_count_modids (); | |
1820 | ||
c63d8f80 | 1821 | /* Set up debugging before the debugger is notified for the first time. */ |
9288c92d | 1822 | elf_setup_debug_entry (main_map, r); |
c63d8f80 | 1823 | |
9dcafc55 UD |
1824 | /* We start adding objects. */ |
1825 | r->r_state = RT_ADD; | |
1826 | _dl_debug_state (); | |
815e6fa3 | 1827 | LIBC_PROBE (init_start, 2, LM_ID_BASE, r); |
9dcafc55 UD |
1828 | |
1829 | /* Auditing checkpoint: we are ready to signal that the initial map | |
1830 | is being constructed. */ | |
3dac3959 | 1831 | _dl_audit_activity_map (main_map, LA_ACT_ADD); |
9dcafc55 | 1832 | |
14bab8de | 1833 | /* We have two ways to specify objects to preload: via environment |
49c091e5 | 1834 | variable and via the file /etc/ld.so.preload. The latter can also |
14bab8de | 1835 | be used when security is enabled. */ |
ab1d521d | 1836 | assert (*first_preload == NULL); |
20fe49b9 UD |
1837 | struct link_map **preloads = NULL; |
1838 | unsigned int npreloads = 0; | |
14bab8de | 1839 | |
2bf9e641 | 1840 | if (__glibc_unlikely (state.preloadlist != NULL)) |
c4029823 | 1841 | { |
1e372ded AZ |
1842 | RTLD_TIMING_VAR (start); |
1843 | rtld_timer_start (&start); | |
2bf9e641 FW |
1844 | npreloads += handle_preload_list (state.preloadlist, main_map, |
1845 | "LD_PRELOAD"); | |
1e372ded | 1846 | rtld_timer_accum (&load_time, start); |
8692ebdb DN |
1847 | } |
1848 | ||
2bf9e641 | 1849 | if (__glibc_unlikely (state.preloadarg != NULL)) |
8692ebdb | 1850 | { |
1e372ded AZ |
1851 | RTLD_TIMING_VAR (start); |
1852 | rtld_timer_start (&start); | |
2bf9e641 FW |
1853 | npreloads += handle_preload_list (state.preloadarg, main_map, |
1854 | "--preload"); | |
1e372ded | 1855 | rtld_timer_accum (&load_time, start); |
c4029823 UD |
1856 | } |
1857 | ||
761490a1 UD |
1858 | /* There usually is no ld.so.preload file, it should only be used |
1859 | for emergencies and testing. So the open call etc should usually | |
1860 | fail. Using access() on a non-existing file is faster than using | |
1861 | open(). So we do this first. If it succeeds we do almost twice | |
1862 | the work but this does not matter, since it is not for production | |
1863 | use. */ | |
1864 | static const char preload_file[] = "/etc/ld.so.preload"; | |
a1ffb40e | 1865 | if (__glibc_unlikely (__access (preload_file, R_OK) == 0)) |
14bab8de | 1866 | { |
761490a1 UD |
1867 | /* Read the contents of the file. */ |
1868 | file = _dl_sysdep_read_whole_file (preload_file, &file_size, | |
1869 | PROT_READ | PROT_WRITE); | |
a1ffb40e | 1870 | if (__glibc_unlikely (file != MAP_FAILED)) |
14bab8de | 1871 | { |
761490a1 UD |
1872 | /* Parse the file. It contains names of libraries to be loaded, |
1873 | separated by white spaces or `:'. It may also contain | |
1874 | comments introduced by `#'. */ | |
1875 | char *problem; | |
1876 | char *runp; | |
1877 | size_t rest; | |
1878 | ||
1879 | /* Eliminate comments. */ | |
e2102c14 | 1880 | runp = file; |
761490a1 UD |
1881 | rest = file_size; |
1882 | while (rest > 0) | |
1883 | { | |
1884 | char *comment = memchr (runp, '#', rest); | |
1885 | if (comment == NULL) | |
1886 | break; | |
1887 | ||
1888 | rest -= comment - runp; | |
1889 | do | |
1890 | *comment = ' '; | |
1891 | while (--rest > 0 && *++comment != '\n'); | |
1892 | } | |
1893 | ||
1894 | /* We have one problematic case: if we have a name at the end of | |
1895 | the file without a trailing terminating characters, we cannot | |
1896 | place the \0. Handle the case separately. */ | |
1897 | if (file[file_size - 1] != ' ' && file[file_size - 1] != '\t' | |
1898 | && file[file_size - 1] != '\n' && file[file_size - 1] != ':') | |
1899 | { | |
1900 | problem = &file[file_size]; | |
1901 | while (problem > file && problem[-1] != ' ' | |
1902 | && problem[-1] != '\t' | |
1903 | && problem[-1] != '\n' && problem[-1] != ':') | |
1904 | --problem; | |
1905 | ||
1906 | if (problem > file) | |
1907 | problem[-1] = '\0'; | |
1908 | } | |
1909 | else | |
1910 | { | |
1911 | problem = NULL; | |
1912 | file[file_size - 1] = '\0'; | |
1913 | } | |
f04b9a68 | 1914 | |
1e372ded AZ |
1915 | RTLD_TIMING_VAR (start); |
1916 | rtld_timer_start (&start); | |
f04b9a68 | 1917 | |
761490a1 UD |
1918 | if (file != problem) |
1919 | { | |
1920 | char *p; | |
1921 | runp = file; | |
1922 | while ((p = strsep (&runp, ": \t\n")) != NULL) | |
1923 | if (p[0] != '\0') | |
20fe49b9 | 1924 | npreloads += do_preload (p, main_map, preload_file); |
761490a1 UD |
1925 | } |
1926 | ||
1927 | if (problem != NULL) | |
1928 | { | |
1929 | char *p = strndupa (problem, file_size - (problem - file)); | |
20fe49b9 UD |
1930 | |
1931 | npreloads += do_preload (p, main_map, preload_file); | |
761490a1 | 1932 | } |
14bab8de | 1933 | |
1e372ded | 1934 | rtld_timer_accum (&load_time, start); |
db276fa1 | 1935 | |
761490a1 UD |
1936 | /* We don't need the file anymore. */ |
1937 | __munmap (file, file_size); | |
1938 | } | |
14bab8de UD |
1939 | } |
1940 | ||
a1ffb40e | 1941 | if (__glibc_unlikely (*first_preload != NULL)) |
14bab8de UD |
1942 | { |
1943 | /* Set up PRELOADS with a vector of the preloaded libraries. */ | |
ab1d521d | 1944 | struct link_map *l = *first_preload; |
14bab8de | 1945 | preloads = __alloca (npreloads * sizeof preloads[0]); |
14bab8de UD |
1946 | i = 0; |
1947 | do | |
1948 | { | |
1949 | preloads[i++] = l; | |
1950 | l = l->l_next; | |
1951 | } while (l); | |
1952 | assert (i == npreloads); | |
1953 | } | |
1954 | ||
f0e23d34 AZ |
1955 | #ifdef NEED_DL_SYSINFO_DSO |
1956 | /* Now that the audit modules are opened, call la_objopen for the vDSO. */ | |
1957 | if (GLRO(dl_sysinfo_map) != NULL) | |
1958 | _dl_audit_objopen (GLRO(dl_sysinfo_map), LM_ID_BASE); | |
1959 | #endif | |
1960 | ||
2064087b RM |
1961 | /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD |
1962 | specified some libraries to load, these are inserted before the actual | |
1963 | dependencies in the executable's searchlist for symbol resolution. */ | |
1e372ded AZ |
1964 | { |
1965 | RTLD_TIMING_VAR (start); | |
1966 | rtld_timer_start (&start); | |
2bf9e641 FW |
1967 | _dl_map_object_deps (main_map, preloads, npreloads, |
1968 | state.mode == rtld_mode_trace, 0); | |
1e372ded AZ |
1969 | rtld_timer_accum (&load_time, start); |
1970 | } | |
e3e35cfc | 1971 | |
20fe49b9 | 1972 | /* Mark all objects as being in the global scope. */ |
c0f62c56 | 1973 | for (i = main_map->l_searchlist.r_nlist; i > 0; ) |
20fe49b9 | 1974 | main_map->l_searchlist.r_list[--i]->l_global = 1; |
d66e34cd | 1975 | |
f9496a7b | 1976 | /* Remove _dl_rtld_map from the chain. */ |
d6b5d570 | 1977 | GL(dl_rtld_map).l_prev->l_next = GL(dl_rtld_map).l_next; |
20fe49b9 | 1978 | if (GL(dl_rtld_map).l_next != NULL) |
d6b5d570 | 1979 | GL(dl_rtld_map).l_next->l_prev = GL(dl_rtld_map).l_prev; |
f9496a7b | 1980 | |
20fe49b9 UD |
1981 | for (i = 1; i < main_map->l_searchlist.r_nlist; ++i) |
1982 | if (main_map->l_searchlist.r_list[i] == &GL(dl_rtld_map)) | |
1983 | break; | |
1984 | ||
1985 | bool rtld_multiple_ref = false; | |
a1ffb40e | 1986 | if (__glibc_likely (i < main_map->l_searchlist.r_nlist)) |
0200214b | 1987 | { |
f9496a7b RM |
1988 | /* Some DT_NEEDED entry referred to the interpreter object itself, so |
1989 | put it back in the list of visible objects. We insert it into the | |
1990 | chain in symbol search order because gdb uses the chain's order as | |
1991 | its symbol search order. */ | |
20fe49b9 UD |
1992 | rtld_multiple_ref = true; |
1993 | ||
c0f62c56 | 1994 | GL(dl_rtld_map).l_prev = main_map->l_searchlist.r_list[i - 1]; |
2bf9e641 | 1995 | if (__glibc_likely (state.mode == rtld_mode_normal)) |
3b3ddb4f | 1996 | { |
c0f62c56 UD |
1997 | GL(dl_rtld_map).l_next = (i + 1 < main_map->l_searchlist.r_nlist |
1998 | ? main_map->l_searchlist.r_list[i + 1] | |
3b3ddb4f | 1999 | : NULL); |
7775448e | 2000 | #ifdef NEED_DL_SYSINFO_DSO |
ab1d521d RM |
2001 | if (GLRO(dl_sysinfo_map) != NULL |
2002 | && GL(dl_rtld_map).l_prev->l_next == GLRO(dl_sysinfo_map) | |
2003 | && GL(dl_rtld_map).l_next != GLRO(dl_sysinfo_map)) | |
2004 | GL(dl_rtld_map).l_prev = GLRO(dl_sysinfo_map); | |
3b3ddb4f UD |
2005 | #endif |
2006 | } | |
b2bcd61a UD |
2007 | else |
2008 | /* In trace mode there might be an invisible object (which we | |
2009 | could not find) after the previous one in the search list. | |
2010 | In this case it doesn't matter much where we put the | |
2011 | interpreter object, so we just initialize the list pointer so | |
2012 | that the assertion below holds. */ | |
d6b5d570 | 2013 | GL(dl_rtld_map).l_next = GL(dl_rtld_map).l_prev->l_next; |
b2bcd61a | 2014 | |
d6b5d570 UD |
2015 | assert (GL(dl_rtld_map).l_prev->l_next == GL(dl_rtld_map).l_next); |
2016 | GL(dl_rtld_map).l_prev->l_next = &GL(dl_rtld_map); | |
3fb55878 | 2017 | if (GL(dl_rtld_map).l_next != NULL) |
f9496a7b | 2018 | { |
d6b5d570 UD |
2019 | assert (GL(dl_rtld_map).l_next->l_prev == GL(dl_rtld_map).l_prev); |
2020 | GL(dl_rtld_map).l_next->l_prev = &GL(dl_rtld_map); | |
f9496a7b | 2021 | } |
0200214b | 2022 | } |
d66e34cd | 2023 | |
c84142e8 UD |
2024 | /* Now let us see whether all libraries are available in the |
2025 | versions we need. */ | |
2026 | { | |
993b3242 | 2027 | struct version_check_args args; |
2bf9e641 FW |
2028 | args.doexit = state.mode == rtld_mode_normal; |
2029 | args.dotrace = state.mode == rtld_mode_trace; | |
993b3242 | 2030 | _dl_receive_error (print_missing_version, version_check_doit, &args); |
c84142e8 UD |
2031 | } |
2032 | ||
2d148689 RM |
2033 | /* We do not initialize any of the TLS functionality unless any of the |
2034 | initial modules uses TLS. This makes dynamic loading of modules with | |
2035 | TLS impossible, but to support it requires either eagerly doing setup | |
2036 | now or lazily doing it later. Doing it now makes us incompatible with | |
2037 | an old kernel that can't perform TLS_INIT_TP, even if no TLS is ever | |
2038 | used. Trying to do it lazily is too hairy to try when there could be | |
2039 | multiple threads (from a non-TLS-using libpthread). */ | |
9dcafc55 | 2040 | bool was_tls_init_tp_called = tls_init_tp_called; |
35f1e827 | 2041 | if (tcbp == NULL) |
17796419 | 2042 | tcbp = init_tls (0); |
0ecb606c | 2043 | |
81b82fb9 | 2044 | if (__glibc_likely (need_security_init)) |
4c48ef06 UD |
2045 | /* Initialize security features. But only if we have not done it |
2046 | earlier. */ | |
2047 | security_init (); | |
827b7087 | 2048 | |
2bf9e641 | 2049 | if (__glibc_unlikely (state.mode != rtld_mode_normal)) |
0200214b RM |
2050 | { |
2051 | /* We were run just to list the shared libraries. It is | |
2052 | important that we do this before real relocation, because the | |
2053 | functions we call below for output may no longer work properly | |
2054 | after relocation. */ | |
81f3ac4c UD |
2055 | struct link_map *l; |
2056 | ||
afdca0f2 | 2057 | if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK) |
ceb2d9aa | 2058 | { |
c0f62c56 | 2059 | struct r_scope_elem *scope = &main_map->l_searchlist; |
ceb2d9aa | 2060 | |
81f3ac4c | 2061 | for (i = 0; i < scope->r_nlist; i++) |
32e6df36 | 2062 | { |
81f3ac4c UD |
2063 | l = scope->r_list [i]; |
2064 | if (l->l_faked) | |
32e6df36 | 2065 | { |
81f3ac4c UD |
2066 | _dl_printf ("\t%s => not found\n", l->l_libname->name); |
2067 | continue; | |
2068 | } | |
afdca0f2 UD |
2069 | if (_dl_name_match_p (GLRO(dl_trace_prelink), l)) |
2070 | GLRO(dl_trace_prelink_map) = l; | |
81f3ac4c | 2071 | _dl_printf ("\t%s => %s (0x%0*Zx, 0x%0*Zx)", |
b9375348 SP |
2072 | DSO_FILENAME (l->l_libname->name), |
2073 | DSO_FILENAME (l->l_name), | |
d347a4ab UD |
2074 | (int) sizeof l->l_map_start * 2, |
2075 | (size_t) l->l_map_start, | |
2076 | (int) sizeof l->l_addr * 2, | |
2077 | (size_t) l->l_addr); | |
11bf311e | 2078 | |
81f3ac4c UD |
2079 | if (l->l_tls_modid) |
2080 | _dl_printf (" TLS(0x%Zx, 0x%0*Zx)\n", l->l_tls_modid, | |
2081 | (int) sizeof l->l_tls_offset * 2, | |
d347a4ab | 2082 | (size_t) l->l_tls_offset); |
81f3ac4c | 2083 | else |
81f3ac4c | 2084 | _dl_printf ("\n"); |
32e6df36 | 2085 | } |
ceb2d9aa | 2086 | } |
7a11603d UD |
2087 | else if (GLRO(dl_debug_mask) & DL_DEBUG_UNUSED) |
2088 | { | |
2089 | /* Look through the dependencies of the main executable | |
2090 | and determine which of them is not actually | |
2091 | required. */ | |
c0f62c56 | 2092 | struct link_map *l = main_map; |
7a11603d UD |
2093 | |
2094 | /* Relocate the main executable. */ | |
2ca285b0 | 2095 | struct relocate_args args = { .l = l, |
3a62d00d AS |
2096 | .reloc_mode = ((GLRO(dl_lazy) |
2097 | ? RTLD_LAZY : 0) | |
2098 | | __RTLD_NOIFUNC) }; | |
7a11603d UD |
2099 | _dl_receive_error (print_unresolved, relocate_doit, &args); |
2100 | ||
2101 | /* This loop depends on the dependencies of the executable to | |
2102 | correspond in number and order to the DT_NEEDED entries. */ | |
c0f62c56 | 2103 | ElfW(Dyn) *dyn = main_map->l_ld; |
7a11603d UD |
2104 | bool first = true; |
2105 | while (dyn->d_tag != DT_NULL) | |
2106 | { | |
2107 | if (dyn->d_tag == DT_NEEDED) | |
2108 | { | |
2109 | l = l->l_next; | |
7775448e | 2110 | #ifdef NEED_DL_SYSINFO_DSO |
ff9f1c5f DM |
2111 | /* Skip the VDSO since it's not part of the list |
2112 | of objects we brought in via DT_NEEDED entries. */ | |
2113 | if (l == GLRO(dl_sysinfo_map)) | |
2114 | l = l->l_next; | |
2115 | #endif | |
7a11603d UD |
2116 | if (!l->l_used) |
2117 | { | |
2118 | if (first) | |
2119 | { | |
2120 | _dl_printf ("Unused direct dependencies:\n"); | |
2121 | first = false; | |
2122 | } | |
2123 | ||
2124 | _dl_printf ("\t%s\n", l->l_name); | |
2125 | } | |
2126 | } | |
2127 | ||
2128 | ++dyn; | |
2129 | } | |
2130 | ||
2131 | _exit (first != true); | |
2132 | } | |
c0f62c56 | 2133 | else if (! main_map->l_info[DT_NEEDED]) |
81f3ac4c UD |
2134 | _dl_printf ("\tstatically linked\n"); |
2135 | else | |
2136 | { | |
c0f62c56 | 2137 | for (l = main_map->l_next; l; l = l->l_next) |
81f3ac4c UD |
2138 | if (l->l_faked) |
2139 | /* The library was not found. */ | |
2140 | _dl_printf ("\t%s => not found\n", l->l_libname->name); | |
75489693 | 2141 | else if (strcmp (l->l_libname->name, l->l_name) == 0) |
7a11603d UD |
2142 | _dl_printf ("\t%s (0x%0*Zx)\n", l->l_libname->name, |
2143 | (int) sizeof l->l_map_start * 2, | |
2144 | (size_t) l->l_map_start); | |
81f3ac4c UD |
2145 | else |
2146 | _dl_printf ("\t%s => %s (0x%0*Zx)\n", l->l_libname->name, | |
2147 | l->l_name, (int) sizeof l->l_map_start * 2, | |
d347a4ab | 2148 | (size_t) l->l_map_start); |
81f3ac4c | 2149 | } |
1a3a58fd | 2150 | |
2bf9e641 | 2151 | if (__glibc_unlikely (state.mode != rtld_mode_trace)) |
5a47e7f2 | 2152 | for (i = 1; i < (unsigned int) _dl_argc; ++i) |
cddcfecf RM |
2153 | { |
2154 | const ElfW(Sym) *ref = NULL; | |
c0282c06 UD |
2155 | ElfW(Addr) loadbase; |
2156 | lookup_t result; | |
c0282c06 | 2157 | |
4243cbea | 2158 | result = _dl_lookup_symbol_x (_dl_argv[i], main_map, |
11bf311e UD |
2159 | &ref, main_map->l_scope, |
2160 | NULL, ELF_RTYPE_CLASS_PLT, | |
021723ab | 2161 | DL_LOOKUP_ADD_DEPENDENCY, NULL); |
c0282c06 | 2162 | |
10a446dd | 2163 | loadbase = LOOKUP_VALUE_ADDRESS (result, false); |
c0282c06 | 2164 | |
35fc382a | 2165 | _dl_printf ("%s found at 0x%0*Zd in object at 0x%0*Zd\n", |
4243cbea | 2166 | _dl_argv[i], |
d347a4ab UD |
2167 | (int) sizeof ref->st_value * 2, |
2168 | (size_t) ref->st_value, | |
2169 | (int) sizeof loadbase * 2, (size_t) loadbase); | |
cddcfecf | 2170 | } |
ce37fa88 | 2171 | else |
fd26970f | 2172 | { |
20fe49b9 | 2173 | /* If LD_WARN is set, warn about undefined symbols. */ |
afdca0f2 | 2174 | if (GLRO(dl_lazy) >= 0 && GLRO(dl_verbose)) |
ce37fa88 UD |
2175 | { |
2176 | /* We have to do symbol dependency testing. */ | |
2177 | struct relocate_args args; | |
48b67d71 | 2178 | unsigned int i; |
993b3242 | 2179 | |
3a62d00d AS |
2180 | args.reloc_mode = ((GLRO(dl_lazy) ? RTLD_LAZY : 0) |
2181 | | __RTLD_NOIFUNC); | |
fd26970f | 2182 | |
48b67d71 AS |
2183 | i = main_map->l_searchlist.r_nlist; |
2184 | while (i-- > 0) | |
ce37fa88 | 2185 | { |
48b67d71 | 2186 | struct link_map *l = main_map->l_initfini[i]; |
d6b5d570 | 2187 | if (l != &GL(dl_rtld_map) && ! l->l_faked) |
ce37fa88 UD |
2188 | { |
2189 | args.l = l; | |
2190 | _dl_receive_error (print_unresolved, relocate_doit, | |
2191 | &args); | |
ce37fa88 | 2192 | } |
20fe49b9 | 2193 | } |
32e6df36 | 2194 | |
afdca0f2 | 2195 | if ((GLRO(dl_debug_mask) & DL_DEBUG_PRELINK) |
20fe49b9 | 2196 | && rtld_multiple_ref) |
e38c954b UD |
2197 | { |
2198 | /* Mark the link map as not yet relocated again. */ | |
2199 | GL(dl_rtld_map).l_relocated = 0; | |
11bf311e | 2200 | _dl_relocate_object (&GL(dl_rtld_map), |
3a62d00d | 2201 | main_map->l_scope, __RTLD_NOIFUNC, 0); |
e38c954b | 2202 | } |
3a56ea26 | 2203 | } |
b0982c4a | 2204 | #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED)) |
2bf9e641 | 2205 | if (state.version_info) |
fd26970f | 2206 | { |
ce37fa88 UD |
2207 | /* Print more information. This means here, print information |
2208 | about the versions needed. */ | |
2209 | int first = 1; | |
c0f62c56 | 2210 | struct link_map *map; |
ce37fa88 | 2211 | |
c0f62c56 | 2212 | for (map = main_map; map != NULL; map = map->l_next) |
fd26970f | 2213 | { |
f41c8091 | 2214 | const char *strtab; |
ce37fa88 | 2215 | ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG]; |
f41c8091 UD |
2216 | ElfW(Verneed) *ent; |
2217 | ||
2218 | if (dyn == NULL) | |
2219 | continue; | |
2220 | ||
a42195db | 2221 | strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]); |
f41c8091 | 2222 | ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr); |
ce37fa88 | 2223 | |
f41c8091 | 2224 | if (first) |
ce37fa88 | 2225 | { |
35fc382a | 2226 | _dl_printf ("\n\tVersion information:\n"); |
f41c8091 UD |
2227 | first = 0; |
2228 | } | |
ce37fa88 | 2229 | |
b9375348 | 2230 | _dl_printf ("\t%s:\n", DSO_FILENAME (map->l_name)); |
f41c8091 UD |
2231 | |
2232 | while (1) | |
2233 | { | |
2234 | ElfW(Vernaux) *aux; | |
2235 | struct link_map *needed; | |
ce37fa88 | 2236 | |
f41c8091 UD |
2237 | needed = find_needed (strtab + ent->vn_file); |
2238 | aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux); | |
ce37fa88 UD |
2239 | |
2240 | while (1) | |
2241 | { | |
f41c8091 UD |
2242 | const char *fname = NULL; |
2243 | ||
f41c8091 | 2244 | if (needed != NULL |
ba9fcb3f UD |
2245 | && match_version (strtab + aux->vna_name, |
2246 | needed)) | |
f41c8091 UD |
2247 | fname = needed->l_name; |
2248 | ||
35fc382a UD |
2249 | _dl_printf ("\t\t%s (%s) %s=> %s\n", |
2250 | strtab + ent->vn_file, | |
2251 | strtab + aux->vna_name, | |
2252 | aux->vna_flags & VER_FLG_WEAK | |
2253 | ? "[WEAK] " : "", | |
2254 | fname ?: "not found"); | |
ce37fa88 | 2255 | |
f41c8091 UD |
2256 | if (aux->vna_next == 0) |
2257 | /* No more symbols. */ | |
ce37fa88 UD |
2258 | break; |
2259 | ||
f41c8091 UD |
2260 | /* Next symbol. */ |
2261 | aux = (ElfW(Vernaux) *) ((char *) aux | |
2262 | + aux->vna_next); | |
ce37fa88 | 2263 | } |
f41c8091 UD |
2264 | |
2265 | if (ent->vn_next == 0) | |
2266 | /* No more dependencies. */ | |
2267 | break; | |
2268 | ||
2269 | /* Next dependency. */ | |
2270 | ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next); | |
ce37fa88 | 2271 | } |
fd26970f | 2272 | } |
ce37fa88 | 2273 | } |
fd26970f | 2274 | } |
d66e34cd | 2275 | |
0200214b RM |
2276 | _exit (0); |
2277 | } | |
86d2c878 | 2278 | |
c0f62c56 | 2279 | if (main_map->l_info[ADDRIDX (DT_GNU_LIBLIST)] |
768027a4 UD |
2280 | && ! __builtin_expect (GLRO(dl_profile) != NULL, 0) |
2281 | && ! __builtin_expect (GLRO(dl_dynamic_weak), 0)) | |
32e6df36 UD |
2282 | { |
2283 | ElfW(Lib) *liblist, *liblistend; | |
2284 | struct link_map **r_list, **r_listend, *l; | |
c0f62c56 | 2285 | const char *strtab = (const void *) D_PTR (main_map, l_info[DT_STRTAB]); |
32e6df36 | 2286 | |
c0f62c56 | 2287 | assert (main_map->l_info[VALIDX (DT_GNU_LIBLISTSZ)] != NULL); |
32e6df36 | 2288 | liblist = (ElfW(Lib) *) |
c0f62c56 | 2289 | main_map->l_info[ADDRIDX (DT_GNU_LIBLIST)]->d_un.d_ptr; |
32e6df36 | 2290 | liblistend = (ElfW(Lib) *) |
34a5a146 JM |
2291 | ((char *) liblist |
2292 | + main_map->l_info[VALIDX (DT_GNU_LIBLISTSZ)]->d_un.d_val); | |
c0f62c56 UD |
2293 | r_list = main_map->l_searchlist.r_list; |
2294 | r_listend = r_list + main_map->l_searchlist.r_nlist; | |
32e6df36 UD |
2295 | |
2296 | for (; r_list < r_listend && liblist < liblistend; r_list++) | |
2297 | { | |
2298 | l = *r_list; | |
2299 | ||
c0f62c56 | 2300 | if (l == main_map) |
32e6df36 UD |
2301 | continue; |
2302 | ||
2303 | /* If the library is not mapped where it should, fail. */ | |
2304 | if (l->l_addr) | |
2305 | break; | |
2306 | ||
2307 | /* Next, check if checksum matches. */ | |
2308 | if (l->l_info [VALIDX(DT_CHECKSUM)] == NULL | |
2309 | || l->l_info [VALIDX(DT_CHECKSUM)]->d_un.d_val | |
2310 | != liblist->l_checksum) | |
2311 | break; | |
2312 | ||
2313 | if (l->l_info [VALIDX(DT_GNU_PRELINKED)] == NULL | |
2314 | || l->l_info [VALIDX(DT_GNU_PRELINKED)]->d_un.d_val | |
2315 | != liblist->l_time_stamp) | |
2316 | break; | |
2317 | ||
2318 | if (! _dl_name_match_p (strtab + liblist->l_name, l)) | |
2319 | break; | |
2320 | ||
2321 | ++liblist; | |
2322 | } | |
2323 | ||
2324 | ||
2325 | if (r_list == r_listend && liblist == liblistend) | |
164a7164 | 2326 | prelinked = true; |
32e6df36 | 2327 | |
a1ffb40e | 2328 | if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS)) |
b85a0f39 UD |
2329 | _dl_debug_printf ("\nprelink checking: %s\n", |
2330 | prelinked ? "ok" : "failed"); | |
32e6df36 UD |
2331 | } |
2332 | ||
ed20b3d9 | 2333 | |
c31e278f | 2334 | /* Now set up the variable which helps the assembler startup code. */ |
c0f62c56 | 2335 | GL(dl_ns)[LM_ID_BASE]._ns_main_searchlist = &main_map->l_searchlist; |
c31e278f UD |
2336 | |
2337 | /* Save the information about the original global scope list since | |
2338 | we need it in the memory handling later. */ | |
c0f62c56 | 2339 | GLRO(dl_initial_searchlist) = *GL(dl_ns)[LM_ID_BASE]._ns_main_searchlist; |
c31e278f | 2340 | |
e23fe25b | 2341 | /* Remember the last search directory added at startup, now that |
8e1472d2 FW |
2342 | malloc will no longer be the one from dl-minimal.c. As a side |
2343 | effect, this marks ld.so as initialized, so that the rtld_active | |
2344 | function returns true from now on. */ | |
e23fe25b AS |
2345 | GLRO(dl_init_all_dirs) = GL(dl_all_dirs); |
2346 | ||
73d7af4f | 2347 | /* Print scope information. */ |
a1ffb40e | 2348 | if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_SCOPES)) |
73d7af4f UD |
2349 | { |
2350 | _dl_debug_printf ("\nInitial object scopes\n"); | |
2351 | ||
2352 | for (struct link_map *l = main_map; l != NULL; l = l->l_next) | |
174baab3 | 2353 | _dl_show_scope (l, 0); |
73d7af4f UD |
2354 | } |
2355 | ||
f753fa7d L |
2356 | _rtld_main_check (main_map, _dl_argv[0]); |
2357 | ||
32e6df36 UD |
2358 | if (prelinked) |
2359 | { | |
c0f62c56 | 2360 | if (main_map->l_info [ADDRIDX (DT_GNU_CONFLICT)] != NULL) |
32e6df36 UD |
2361 | { |
2362 | ElfW(Rela) *conflict, *conflictend; | |
32e6df36 | 2363 | |
1e372ded AZ |
2364 | RTLD_TIMING_VAR (start); |
2365 | rtld_timer_start (&start); | |
2366 | ||
c0f62c56 | 2367 | assert (main_map->l_info [VALIDX (DT_GNU_CONFLICTSZ)] != NULL); |
32e6df36 | 2368 | conflict = (ElfW(Rela) *) |
c0f62c56 | 2369 | main_map->l_info [ADDRIDX (DT_GNU_CONFLICT)]->d_un.d_ptr; |
32e6df36 | 2370 | conflictend = (ElfW(Rela) *) |
d89ae1d5 | 2371 | ((char *) conflict |
c0f62c56 UD |
2372 | + main_map->l_info [VALIDX (DT_GNU_CONFLICTSZ)]->d_un.d_val); |
2373 | _dl_resolve_conflicts (main_map, conflict, conflictend); | |
1e372ded AZ |
2374 | |
2375 | rtld_timer_stop (&relocate_time, start); | |
32e6df36 UD |
2376 | } |
2377 | ||
5d28a896 FW |
2378 | /* Set up the object lookup structures. */ |
2379 | _dl_find_object_init (); | |
2380 | ||
3a0ecccb FW |
2381 | /* The library defining malloc has already been relocated due to |
2382 | prelinking. Resolve the malloc symbols for the dynamic | |
2383 | loader. */ | |
2384 | __rtld_malloc_init_real (main_map); | |
d89ae1d5 | 2385 | |
d6163dfd FW |
2386 | /* Likewise for the locking implementation. */ |
2387 | __rtld_mutex_init (); | |
2388 | ||
d89ae1d5 | 2389 | /* Mark all the objects so we know they have been already relocated. */ |
9dcafc55 | 2390 | for (struct link_map *l = main_map; l != NULL; l = l->l_next) |
e8648a5a UD |
2391 | { |
2392 | l->l_relocated = 1; | |
2393 | if (l->l_relro_size) | |
2394 | _dl_protect_relro (l); | |
9dcafc55 UD |
2395 | |
2396 | /* Add object to slot information data if necessasy. */ | |
2397 | if (l->l_tls_blocksize != 0 && tls_init_tp_called) | |
a509eb11 | 2398 | _dl_add_to_slotinfo (l, true); |
e8648a5a | 2399 | } |
32e6df36 UD |
2400 | } |
2401 | else | |
164a7164 UD |
2402 | { |
2403 | /* Now we have all the objects loaded. Relocate them all except for | |
2404 | the dynamic linker itself. We do this in reverse order so that copy | |
2405 | relocs of earlier objects overwrite the data written by later | |
2406 | objects. We do not re-relocate the dynamic linker itself in this | |
2407 | loop because that could result in the GOT entries for functions we | |
2408 | call being changed, and that would break us. It is safe to relocate | |
2409 | the dynamic linker out of order because it has no copy relocs (we | |
2410 | know that because it is self-contained). */ | |
2411 | ||
afdca0f2 | 2412 | int consider_profiling = GLRO(dl_profile) != NULL; |
c0fb8a56 | 2413 | |
164a7164 | 2414 | /* If we are profiling we also must do lazy reloaction. */ |
afdca0f2 | 2415 | GLRO(dl_lazy) |= consider_profiling; |
c0fb8a56 | 2416 | |
1e372ded AZ |
2417 | RTLD_TIMING_VAR (start); |
2418 | rtld_timer_start (&start); | |
2bc17433 AS |
2419 | unsigned i = main_map->l_searchlist.r_nlist; |
2420 | while (i-- > 0) | |
164a7164 | 2421 | { |
2bc17433 AS |
2422 | struct link_map *l = main_map->l_initfini[i]; |
2423 | ||
164a7164 UD |
2424 | /* While we are at it, help the memory handling a bit. We have to |
2425 | mark some data structures as allocated with the fake malloc() | |
2426 | implementation in ld.so. */ | |
2427 | struct libname_list *lnp = l->l_libname->next; | |
752a2a50 | 2428 | |
164a7164 UD |
2429 | while (__builtin_expect (lnp != NULL, 0)) |
2430 | { | |
2431 | lnp->dont_free = 1; | |
2432 | lnp = lnp->next; | |
2433 | } | |
0479b305 AS |
2434 | /* Also allocated with the fake malloc(). */ |
2435 | l->l_free_initfini = 0; | |
752a2a50 | 2436 | |
164a7164 | 2437 | if (l != &GL(dl_rtld_map)) |
2ca285b0 | 2438 | _dl_relocate_object (l, l->l_scope, GLRO(dl_lazy) ? RTLD_LAZY : 0, |
154d10bd | 2439 | consider_profiling); |
be935610 | 2440 | |
9dcafc55 UD |
2441 | /* Add object to slot information data if necessasy. */ |
2442 | if (l->l_tls_blocksize != 0 && tls_init_tp_called) | |
a509eb11 | 2443 | _dl_add_to_slotinfo (l, true); |
164a7164 | 2444 | } |
1e372ded | 2445 | rtld_timer_stop (&relocate_time, start); |
164a7164 | 2446 | |
164a7164 UD |
2447 | /* Now enable profiling if needed. Like the previous call, |
2448 | this has to go here because the calls it makes should use the | |
2449 | rtld versions of the functions (particularly calloc()), but it | |
2450 | needs to have _dl_profile_map set up by the relocator. */ | |
a1ffb40e | 2451 | if (__glibc_unlikely (GL(dl_profile_map) != NULL)) |
164a7164 | 2452 | /* We must prepare the profiling. */ |
53bfdc1c | 2453 | _dl_start_profile (); |
164a7164 | 2454 | } |
ac16e905 | 2455 | |
d0503676 CD |
2456 | if ((!was_tls_init_tp_called && GL(dl_tls_max_dtv_idx) > 0) |
2457 | || count_modids != _dl_count_modids ()) | |
35f1e827 | 2458 | ++GL(dl_tls_generation); |
9dcafc55 | 2459 | |
35f1e827 UD |
2460 | /* Now that we have completed relocation, the initializer data |
2461 | for the TLS blocks has its final values and we can copy them | |
91ac3a7d TMQMF |
2462 | into the main thread's TLS area, which we allocated above. |
2463 | Note: thread-local variables must only be accessed after completing | |
2464 | the next step. */ | |
35f1e827 | 2465 | _dl_allocate_tls_init (tcbp); |
a334319f | 2466 | |
3d8c8bff | 2467 | /* And finally install it for the main thread. */ |
35f1e827 UD |
2468 | if (! tls_init_tp_called) |
2469 | { | |
774f9285 | 2470 | const char *lossage = TLS_INIT_TP (tcbp); |
a1ffb40e | 2471 | if (__glibc_unlikely (lossage != NULL)) |
35f1e827 UD |
2472 | _dl_fatal_printf ("cannot set up thread-local storage: %s\n", |
2473 | lossage); | |
90d7e7e5 | 2474 | __tls_init_tp (); |
0ecb606c | 2475 | } |
0ecb606c | 2476 | |
e23fe25b AS |
2477 | /* Make sure no new search directories have been added. */ |
2478 | assert (GLRO(dl_init_all_dirs) == GL(dl_all_dirs)); | |
bc5fb037 | 2479 | |
cafdfdb6 RM |
2480 | if (! prelinked && rtld_multiple_ref) |
2481 | { | |
2482 | /* There was an explicit ref to the dynamic linker as a shared lib. | |
2483 | Re-relocate ourselves with user-controlled symbol definitions. | |
2484 | ||
2485 | We must do this after TLS initialization in case after this | |
2486 | re-relocation, we might call a user-supplied function | |
2487 | (e.g. calloc from _dl_relocate_object) that uses TLS data. */ | |
2488 | ||
5d28a896 FW |
2489 | /* Set up the object lookup structures. */ |
2490 | _dl_find_object_init (); | |
2491 | ||
3a0ecccb FW |
2492 | /* The malloc implementation has been relocated, so resolving |
2493 | its symbols (and potentially calling IFUNC resolvers) is safe | |
2494 | at this point. */ | |
2495 | __rtld_malloc_init_real (main_map); | |
2496 | ||
d6163dfd FW |
2497 | /* Likewise for the locking implementation. */ |
2498 | __rtld_mutex_init (); | |
2499 | ||
1e372ded AZ |
2500 | RTLD_TIMING_VAR (start); |
2501 | rtld_timer_start (&start); | |
cafdfdb6 | 2502 | |
cafdfdb6 RM |
2503 | /* Mark the link map as not yet relocated again. */ |
2504 | GL(dl_rtld_map).l_relocated = 0; | |
c0a777e8 | 2505 | _dl_relocate_object (&GL(dl_rtld_map), main_map->l_scope, 0, 0); |
1e372ded AZ |
2506 | |
2507 | rtld_timer_accum (&relocate_time, start); | |
cafdfdb6 RM |
2508 | } |
2509 | ||
03e187a4 FW |
2510 | /* Relocation is complete. Perform early libc initialization. This |
2511 | is the initial libc, even if audit modules have been loaded with | |
2512 | other libcs. */ | |
2513 | _dl_call_libc_early_init (GL(dl_ns)[LM_ID_BASE].libc_map, true); | |
ec935dea | 2514 | |
bf8523c8 RM |
2515 | /* Do any necessary cleanups for the startup OS interface code. |
2516 | We do these now so that no calls are made after rtld re-relocation | |
2517 | which might be resolved to different functions than we expect. | |
2518 | We cannot do this before relocating the other objects because | |
2519 | _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */ | |
2520 | _dl_sysdep_start_cleanup (); | |
2521 | ||
9dcafc55 UD |
2522 | #ifdef SHARED |
2523 | /* Auditing checkpoint: we have added all objects. */ | |
3dac3959 | 2524 | _dl_audit_activity_nsid (LM_ID_BASE, LA_ACT_CONSISTENT); |
9dcafc55 UD |
2525 | #endif |
2526 | ||
2527 | /* Notify the debugger all new objects are now ready to go. We must re-get | |
2528 | the address since by now the variable might be in another object. */ | |
a93d9e03 | 2529 | r = _dl_debug_update (LM_ID_BASE); |
9dcafc55 | 2530 | r->r_state = RT_CONSISTENT; |
154d10bd | 2531 | _dl_debug_state (); |
815e6fa3 | 2532 | LIBC_PROBE (init_complete, 2, LM_ID_BASE, r); |
0200214b | 2533 | |
f57f8055 | 2534 | #if defined USE_LDCONFIG && !defined MAP_COPY |
08cac4ac | 2535 | /* We must munmap() the cache file. */ |
154d10bd | 2536 | _dl_unload_cache (); |
08cac4ac UD |
2537 | #endif |
2538 | ||
d66e34cd RM |
2539 | /* Once we return, _dl_sysdep_start will invoke |
2540 | the DT_INIT functions and then *USER_ENTRY. */ | |
2541 | } | |
fd26970f UD |
2542 | \f |
2543 | /* This is a little helper function for resolving symbols while | |
2544 | tracing the binary. */ | |
2545 | static void | |
c84142e8 UD |
2546 | print_unresolved (int errcode __attribute__ ((unused)), const char *objname, |
2547 | const char *errstring) | |
fd26970f | 2548 | { |
3996f34b | 2549 | if (objname[0] == '\0') |
b9375348 | 2550 | objname = RTLD_PROGNAME; |
35fc382a | 2551 | _dl_error_printf ("%s (%s)\n", errstring, objname); |
fd26970f | 2552 | } |
c84142e8 UD |
2553 | \f |
2554 | /* This is a little helper function for resolving symbols while | |
2555 | tracing the binary. */ | |
2556 | static void | |
2557 | print_missing_version (int errcode __attribute__ ((unused)), | |
2558 | const char *objname, const char *errstring) | |
2559 | { | |
b9375348 | 2560 | _dl_error_printf ("%s: %s: %s\n", RTLD_PROGNAME, |
35fc382a | 2561 | objname, errstring); |
c84142e8 | 2562 | } |
ea278354 | 2563 | \f |
b5efde2f UD |
2564 | /* Process the string given as the parameter which explains which debugging |
2565 | options are enabled. */ | |
2566 | static void | |
2bf9e641 | 2567 | process_dl_debug (struct dl_main_state *state, const char *dl_debug) |
b5efde2f | 2568 | { |
3e2040c8 UD |
2569 | /* When adding new entries make sure that the maximal length of a name |
2570 | is correctly handled in the LD_DEBUG_HELP code below. */ | |
2571 | static const struct | |
2572 | { | |
379d4ec4 UD |
2573 | unsigned char len; |
2574 | const char name[10]; | |
3e2040c8 UD |
2575 | const char helptext[41]; |
2576 | unsigned short int mask; | |
2577 | } debopts[] = | |
2578 | { | |
379d4ec4 UD |
2579 | #define LEN_AND_STR(str) sizeof (str) - 1, str |
2580 | { LEN_AND_STR ("libs"), "display library search paths", | |
3e2040c8 | 2581 | DL_DEBUG_LIBS | DL_DEBUG_IMPCALLS }, |
379d4ec4 | 2582 | { LEN_AND_STR ("reloc"), "display relocation processing", |
3e2040c8 | 2583 | DL_DEBUG_RELOC | DL_DEBUG_IMPCALLS }, |
379d4ec4 | 2584 | { LEN_AND_STR ("files"), "display progress for input file", |
3e2040c8 | 2585 | DL_DEBUG_FILES | DL_DEBUG_IMPCALLS }, |
379d4ec4 | 2586 | { LEN_AND_STR ("symbols"), "display symbol table processing", |
3e2040c8 | 2587 | DL_DEBUG_SYMBOLS | DL_DEBUG_IMPCALLS }, |
379d4ec4 | 2588 | { LEN_AND_STR ("bindings"), "display information about symbol binding", |
3e2040c8 | 2589 | DL_DEBUG_BINDINGS | DL_DEBUG_IMPCALLS }, |
379d4ec4 | 2590 | { LEN_AND_STR ("versions"), "display version dependencies", |
3e2040c8 | 2591 | DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS }, |
73d7af4f UD |
2592 | { LEN_AND_STR ("scopes"), "display scope information", |
2593 | DL_DEBUG_SCOPES }, | |
379d4ec4 | 2594 | { LEN_AND_STR ("all"), "all previous options combined", |
3e2040c8 | 2595 | DL_DEBUG_LIBS | DL_DEBUG_RELOC | DL_DEBUG_FILES | DL_DEBUG_SYMBOLS |
73d7af4f UD |
2596 | | DL_DEBUG_BINDINGS | DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS |
2597 | | DL_DEBUG_SCOPES }, | |
379d4ec4 | 2598 | { LEN_AND_STR ("statistics"), "display relocation statistics", |
3e2040c8 | 2599 | DL_DEBUG_STATISTICS }, |
7a11603d UD |
2600 | { LEN_AND_STR ("unused"), "determined unused DSOs", |
2601 | DL_DEBUG_UNUSED }, | |
379d4ec4 | 2602 | { LEN_AND_STR ("help"), "display this help message and exit", |
3e2040c8 UD |
2603 | DL_DEBUG_HELP }, |
2604 | }; | |
2605 | #define ndebopts (sizeof (debopts) / sizeof (debopts[0])) | |
3e2040c8 | 2606 | |
379d4ec4 UD |
2607 | /* Skip separating white spaces and commas. */ |
2608 | while (*dl_debug != '\0') | |
b5efde2f | 2609 | { |
379d4ec4 | 2610 | if (*dl_debug != ' ' && *dl_debug != ',' && *dl_debug != ':') |
b5efde2f | 2611 | { |
3e2040c8 | 2612 | size_t cnt; |
379d4ec4 | 2613 | size_t len = 1; |
77aba05b | 2614 | |
379d4ec4 UD |
2615 | while (dl_debug[len] != '\0' && dl_debug[len] != ' ' |
2616 | && dl_debug[len] != ',' && dl_debug[len] != ':') | |
2617 | ++len; | |
14c44e2e | 2618 | |
3e2040c8 | 2619 | for (cnt = 0; cnt < ndebopts; ++cnt) |
379d4ec4 UD |
2620 | if (debopts[cnt].len == len |
2621 | && memcmp (dl_debug, debopts[cnt].name, len) == 0) | |
3e2040c8 | 2622 | { |
afdca0f2 | 2623 | GLRO(dl_debug_mask) |= debopts[cnt].mask; |
2bf9e641 | 2624 | state->any_debug = true; |
3e2040c8 UD |
2625 | break; |
2626 | } | |
77aba05b | 2627 | |
3e2040c8 UD |
2628 | if (cnt == ndebopts) |
2629 | { | |
2630 | /* Display a warning and skip everything until next | |
2631 | separator. */ | |
2632 | char *copy = strndupa (dl_debug, len); | |
2633 | _dl_error_printf ("\ | |
2634 | warning: debug option `%s' unknown; try LD_DEBUG=help\n", copy); | |
379d4ec4 UD |
2635 | } |
2636 | ||
2637 | dl_debug += len; | |
2638 | continue; | |
3e2040c8 | 2639 | } |
379d4ec4 UD |
2640 | |
2641 | ++dl_debug; | |
3e2040c8 | 2642 | } |
77aba05b | 2643 | |
ff9f1c5f DM |
2644 | if (GLRO(dl_debug_mask) & DL_DEBUG_UNUSED) |
2645 | { | |
2646 | /* In order to get an accurate picture of whether a particular | |
2647 | DT_NEEDED entry is actually used we have to process both | |
2648 | the PLT and non-PLT relocation entries. */ | |
2649 | GLRO(dl_lazy) = 0; | |
2650 | } | |
2651 | ||
afdca0f2 | 2652 | if (GLRO(dl_debug_mask) & DL_DEBUG_HELP) |
3e2040c8 UD |
2653 | { |
2654 | size_t cnt; | |
14c44e2e | 2655 | |
3e2040c8 UD |
2656 | _dl_printf ("\ |
2657 | Valid options for the LD_DEBUG environment variable are:\n\n"); | |
db276fa1 | 2658 | |
3e2040c8 | 2659 | for (cnt = 0; cnt < ndebopts; ++cnt) |
37d8b778 UD |
2660 | _dl_printf (" %.*s%s%s\n", debopts[cnt].len, debopts[cnt].name, |
2661 | " " + debopts[cnt].len - 3, | |
3e2040c8 | 2662 | debopts[cnt].helptext); |
14c44e2e | 2663 | |
3e2040c8 UD |
2664 | _dl_printf ("\n\ |
2665 | To direct the debugging output into a file instead of standard output\n\ | |
2666 | a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n"); | |
2667 | _exit (0); | |
b5efde2f | 2668 | } |
b5efde2f UD |
2669 | } |
2670 | \f | |
ea278354 | 2671 | static void |
2bf9e641 | 2672 | process_envvars (struct dl_main_state *state) |
ea278354 | 2673 | { |
67c94753 | 2674 | char **runp = _environ; |
ea278354 | 2675 | char *envline; |
7dea968e | 2676 | char *debug_output = NULL; |
ea278354 UD |
2677 | |
2678 | /* This is the default place for profiling data file. */ | |
afdca0f2 | 2679 | GLRO(dl_profile_output) |
6bc6bd3b | 2680 | = &"/var/tmp\0/var/profile"[__libc_enable_secure ? 9 : 0]; |
ea278354 UD |
2681 | |
2682 | while ((envline = _dl_next_ld_env_entry (&runp)) != NULL) | |
2683 | { | |
379d4ec4 UD |
2684 | size_t len = 0; |
2685 | ||
2686 | while (envline[len] != '\0' && envline[len] != '=') | |
2687 | ++len; | |
ea278354 | 2688 | |
75e8d1f5 UD |
2689 | if (envline[len] != '=') |
2690 | /* This is a "LD_" variable at the end of the string without | |
2691 | a '=' character. Ignore it since otherwise we will access | |
2692 | invalid memory below. */ | |
67c94753 | 2693 | continue; |
75e8d1f5 | 2694 | |
67c94753 | 2695 | switch (len) |
ea278354 | 2696 | { |
14c44e2e UD |
2697 | case 4: |
2698 | /* Warning level, verbose or not. */ | |
67c94753 | 2699 | if (memcmp (envline, "WARN", 4) == 0) |
afdca0f2 | 2700 | GLRO(dl_verbose) = envline[5] != '\0'; |
14c44e2e | 2701 | break; |
ea278354 | 2702 | |
14c44e2e UD |
2703 | case 5: |
2704 | /* Debugging of the dynamic linker? */ | |
67c94753 | 2705 | if (memcmp (envline, "DEBUG", 5) == 0) |
9dcafc55 | 2706 | { |
2bf9e641 | 2707 | process_dl_debug (state, &envline[6]); |
9dcafc55 UD |
2708 | break; |
2709 | } | |
2710 | if (memcmp (envline, "AUDIT", 5) == 0) | |
2bf9e641 | 2711 | audit_list_add_string (&state->audit_list, &envline[6]); |
14c44e2e | 2712 | break; |
b5efde2f | 2713 | |
14c44e2e UD |
2714 | case 7: |
2715 | /* Print information about versions. */ | |
67c94753 | 2716 | if (memcmp (envline, "VERBOSE", 7) == 0) |
14c44e2e | 2717 | { |
2bf9e641 | 2718 | state->version_info = envline[8] != '\0'; |
14c44e2e UD |
2719 | break; |
2720 | } | |
7dea968e | 2721 | |
14c44e2e | 2722 | /* List of objects to be preloaded. */ |
67c94753 | 2723 | if (memcmp (envline, "PRELOAD", 7) == 0) |
14c44e2e | 2724 | { |
2bf9e641 | 2725 | state->preloadlist = &envline[8]; |
14c44e2e UD |
2726 | break; |
2727 | } | |
120b4c49 | 2728 | |
14c44e2e | 2729 | /* Which shared object shall be profiled. */ |
c95f3fd4 | 2730 | if (memcmp (envline, "PROFILE", 7) == 0 && envline[8] != '\0') |
afdca0f2 | 2731 | GLRO(dl_profile) = &envline[8]; |
14c44e2e | 2732 | break; |
120b4c49 | 2733 | |
14c44e2e UD |
2734 | case 8: |
2735 | /* Do we bind early? */ | |
67c94753 | 2736 | if (memcmp (envline, "BIND_NOW", 8) == 0) |
f53c03c2 | 2737 | { |
afdca0f2 | 2738 | GLRO(dl_lazy) = envline[9] == '\0'; |
f53c03c2 UD |
2739 | break; |
2740 | } | |
67c94753 | 2741 | if (memcmp (envline, "BIND_NOT", 8) == 0) |
afdca0f2 | 2742 | GLRO(dl_bind_not) = envline[9] != '\0'; |
14c44e2e | 2743 | break; |
ea278354 | 2744 | |
14c44e2e UD |
2745 | case 9: |
2746 | /* Test whether we want to see the content of the auxiliary | |
2747 | array passed up from the kernel. */ | |
6bc6bd3b | 2748 | if (!__libc_enable_secure |
00a12162 | 2749 | && memcmp (envline, "SHOW_AUXV", 9) == 0) |
14c44e2e UD |
2750 | _dl_show_auxv (); |
2751 | break; | |
ea278354 | 2752 | |
ff08fc59 | 2753 | #if !HAVE_TUNABLES |
12264bd7 | 2754 | case 10: |
3081378b | 2755 | /* Mask for the important hardware capabilities. */ |
1c1243b6 SP |
2756 | if (!__libc_enable_secure |
2757 | && memcmp (envline, "HWCAP_MASK", 10) == 0) | |
37b66c0b | 2758 | GLRO(dl_hwcap_mask) = _dl_strtoul (&envline[11], NULL); |
12264bd7 | 2759 | break; |
ff08fc59 | 2760 | #endif |
12264bd7 | 2761 | |
f787edde UD |
2762 | case 11: |
2763 | /* Path where the binary is found. */ | |
6bc6bd3b | 2764 | if (!__libc_enable_secure |
67c94753 | 2765 | && memcmp (envline, "ORIGIN_PATH", 11) == 0) |
afdca0f2 | 2766 | GLRO(dl_origin_path) = &envline[12]; |
f787edde UD |
2767 | break; |
2768 | ||
14c44e2e | 2769 | case 12: |
dec126b4 | 2770 | /* The library search path. */ |
f6110a8f FW |
2771 | if (!__libc_enable_secure |
2772 | && memcmp (envline, "LIBRARY_PATH", 12) == 0) | |
dec126b4 | 2773 | { |
2bf9e641 | 2774 | state->library_path = &envline[13]; |
27316f4a | 2775 | state->library_path_source = "LD_LIBRARY_PATH"; |
dec126b4 UD |
2776 | break; |
2777 | } | |
2778 | ||
14c44e2e | 2779 | /* Where to place the profiling data file. */ |
67c94753 | 2780 | if (memcmp (envline, "DEBUG_OUTPUT", 12) == 0) |
14c44e2e | 2781 | { |
67c94753 | 2782 | debug_output = &envline[13]; |
14c44e2e UD |
2783 | break; |
2784 | } | |
ea278354 | 2785 | |
6bc6bd3b | 2786 | if (!__libc_enable_secure |
00a12162 | 2787 | && memcmp (envline, "DYNAMIC_WEAK", 12) == 0) |
afdca0f2 | 2788 | GLRO(dl_dynamic_weak) = 1; |
14c44e2e | 2789 | break; |
ea278354 | 2790 | |
97fd3a30 UD |
2791 | case 13: |
2792 | /* We might have some extra environment variable with length 13 | |
2793 | to handle. */ | |
2794 | #ifdef EXTRA_LD_ENVVARS_13 | |
2795 | EXTRA_LD_ENVVARS_13 | |
2796 | #endif | |
6bc6bd3b | 2797 | if (!__libc_enable_secure |
97fd3a30 | 2798 | && memcmp (envline, "USE_LOAD_BIAS", 13) == 0) |
827b7087 UD |
2799 | { |
2800 | GLRO(dl_use_load_bias) = envline[14] == '1' ? -1 : 0; | |
2801 | break; | |
2802 | } | |
97fd3a30 UD |
2803 | break; |
2804 | ||
14c44e2e UD |
2805 | case 14: |
2806 | /* Where to place the profiling data file. */ | |
6bc6bd3b | 2807 | if (!__libc_enable_secure |
3e2040c8 UD |
2808 | && memcmp (envline, "PROFILE_OUTPUT", 14) == 0 |
2809 | && envline[15] != '\0') | |
afdca0f2 | 2810 | GLRO(dl_profile_output) = &envline[15]; |
14c44e2e | 2811 | break; |
120b4c49 | 2812 | |
32e6df36 UD |
2813 | case 16: |
2814 | /* The mode of the dynamic linker can be set. */ | |
2815 | if (memcmp (envline, "TRACE_PRELINKING", 16) == 0) | |
2816 | { | |
2bf9e641 | 2817 | state->mode = rtld_mode_trace; |
afdca0f2 UD |
2818 | GLRO(dl_verbose) = 1; |
2819 | GLRO(dl_debug_mask) |= DL_DEBUG_PRELINK; | |
2820 | GLRO(dl_trace_prelink) = &envline[17]; | |
32e6df36 UD |
2821 | } |
2822 | break; | |
2823 | ||
14c44e2e UD |
2824 | case 20: |
2825 | /* The mode of the dynamic linker can be set. */ | |
67c94753 | 2826 | if (memcmp (envline, "TRACE_LOADED_OBJECTS", 20) == 0) |
2bf9e641 | 2827 | state->mode = rtld_mode_trace; |
14c44e2e | 2828 | break; |
e2102c14 UD |
2829 | |
2830 | /* We might have some extra environment variable to handle. This | |
2831 | is tricky due to the pre-processing of the length of the name | |
2832 | in the switch statement here. The code here assumes that added | |
2833 | environment variables have a different length. */ | |
2834 | #ifdef EXTRA_LD_ENVVARS | |
2835 | EXTRA_LD_ENVVARS | |
2836 | #endif | |
ea278354 UD |
2837 | } |
2838 | } | |
2839 | ||
4bae5567 UD |
2840 | /* Extra security for SUID binaries. Remove all dangerous environment |
2841 | variables. */ | |
6bc6bd3b | 2842 | if (__builtin_expect (__libc_enable_secure, 0)) |
4bae5567 | 2843 | { |
c95f3fd4 | 2844 | static const char unsecure_envvars[] = |
4bae5567 UD |
2845 | #ifdef EXTRA_UNSECURE_ENVVARS |
2846 | EXTRA_UNSECURE_ENVVARS | |
2847 | #endif | |
c95f3fd4 UD |
2848 | UNSECURE_ENVVARS; |
2849 | const char *nextp; | |
2850 | ||
2851 | nextp = unsecure_envvars; | |
2852 | do | |
2853 | { | |
2854 | unsetenv (nextp); | |
9710f75d UD |
2855 | /* We could use rawmemchr but this need not be fast. */ |
2856 | nextp = (char *) (strchr) (nextp, '\0') + 1; | |
c95f3fd4 UD |
2857 | } |
2858 | while (*nextp != '\0'); | |
74955460 UD |
2859 | |
2860 | if (__access ("/etc/suid-debug", F_OK) != 0) | |
3a56ea26 | 2861 | { |
67e58f39 | 2862 | #if !HAVE_TUNABLES |
00a12162 | 2863 | unsetenv ("MALLOC_CHECK_"); |
67e58f39 | 2864 | #endif |
f57a3c94 | 2865 | GLRO(dl_debug_mask) = 0; |
3a56ea26 | 2866 | } |
f57a3c94 | 2867 | |
2bf9e641 | 2868 | if (state->mode != rtld_mode_normal) |
f57a3c94 | 2869 | _exit (5); |
4bae5567 | 2870 | } |
7dea968e UD |
2871 | /* If we have to run the dynamic linker in debugging mode and the |
2872 | LD_DEBUG_OUTPUT environment variable is given, we write the debug | |
2873 | messages to this file. */ | |
2bf9e641 | 2874 | else if (state->any_debug && debug_output != NULL) |
7dea968e | 2875 | { |
5f2de337 | 2876 | const int flags = O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW; |
7a2fd787 UD |
2877 | size_t name_len = strlen (debug_output); |
2878 | char buf[name_len + 12]; | |
2879 | char *startp; | |
2880 | ||
2881 | buf[name_len + 11] = '\0'; | |
9710f75d | 2882 | startp = _itoa (__getpid (), &buf[name_len + 11], 10, 0); |
7a2fd787 UD |
2883 | *--startp = '.'; |
2884 | startp = memcpy (startp - name_len, debug_output, name_len); | |
2885 | ||
329ea513 | 2886 | GLRO(dl_debug_fd) = __open64_nocancel (startp, flags, DEFFILEMODE); |
dd70526e | 2887 | if (GLRO(dl_debug_fd) == -1) |
7dea968e | 2888 | /* We use standard output if opening the file failed. */ |
dd70526e | 2889 | GLRO(dl_debug_fd) = STDOUT_FILENO; |
7dea968e | 2890 | } |
ea278354 | 2891 | } |
db276fa1 | 2892 | |
1e372ded AZ |
2893 | #if HP_TIMING_INLINE |
2894 | static void | |
2895 | print_statistics_item (const char *title, hp_timing_t time, | |
2896 | hp_timing_t total) | |
2897 | { | |
2898 | char cycles[HP_TIMING_PRINT_SIZE]; | |
2899 | HP_TIMING_PRINT (cycles, sizeof (cycles), time); | |
2900 | ||
2901 | char relative[3 * sizeof (hp_timing_t) + 2]; | |
2902 | char *cp = _itoa ((1000ULL * time) / total, relative + sizeof (relative), | |
2903 | 10, 0); | |
2904 | /* Sets the decimal point. */ | |
2905 | char *wp = relative; | |
2906 | switch (relative + sizeof (relative) - cp) | |
2907 | { | |
2908 | case 3: | |
2909 | *wp++ = *cp++; | |
2910 | /* Fall through. */ | |
2911 | case 2: | |
2912 | *wp++ = *cp++; | |
2913 | /* Fall through. */ | |
2914 | case 1: | |
2915 | *wp++ = '.'; | |
2916 | *wp++ = *cp++; | |
2917 | } | |
2918 | *wp = '\0'; | |
2919 | _dl_debug_printf ("%s: %s cycles (%s%%)\n", title, cycles, relative); | |
2920 | } | |
2921 | #endif | |
db276fa1 UD |
2922 | |
2923 | /* Print the various times we collected. */ | |
2924 | static void | |
ee600e3f | 2925 | __attribute ((noinline)) |
1e372ded | 2926 | print_statistics (const hp_timing_t *rtld_total_timep) |
db276fa1 | 2927 | { |
1e372ded AZ |
2928 | #if HP_TIMING_INLINE |
2929 | { | |
2930 | char cycles[HP_TIMING_PRINT_SIZE]; | |
2931 | HP_TIMING_PRINT (cycles, sizeof (cycles), *rtld_total_timep); | |
2932 | _dl_debug_printf ("\nruntime linker statistics:\n" | |
2933 | " total startup time in dynamic loader: %s cycles\n", | |
2934 | cycles); | |
2935 | print_statistics_item (" time needed for relocation", | |
2936 | relocate_time, *rtld_total_timep); | |
2937 | } | |
1531e094 | 2938 | #endif |
a21a20a3 UD |
2939 | |
2940 | unsigned long int num_relative_relocations = 0; | |
22c83193 | 2941 | for (Lmid_t ns = 0; ns < GL(dl_nns); ++ns) |
a21a20a3 | 2942 | { |
c120d94d UD |
2943 | if (GL(dl_ns)[ns]._ns_loaded == NULL) |
2944 | continue; | |
2945 | ||
c0f62c56 | 2946 | struct r_scope_elem *scope = &GL(dl_ns)[ns]._ns_loaded->l_searchlist; |
a21a20a3 | 2947 | |
c0f62c56 UD |
2948 | for (unsigned int i = 0; i < scope->r_nlist; i++) |
2949 | { | |
2950 | struct link_map *l = scope->r_list [i]; | |
2951 | ||
c120d94d | 2952 | if (l->l_addr != 0 && l->l_info[VERSYMIDX (DT_RELCOUNT)]) |
c0f62c56 UD |
2953 | num_relative_relocations |
2954 | += l->l_info[VERSYMIDX (DT_RELCOUNT)]->d_un.d_val; | |
c120d94d UD |
2955 | #ifndef ELF_MACHINE_REL_RELATIVE |
2956 | /* Relative relocations are processed on these architectures if | |
2957 | library is loaded to different address than p_vaddr or | |
2958 | if not prelinked. */ | |
2959 | if ((l->l_addr != 0 || !l->l_info[VALIDX(DT_GNU_PRELINKED)]) | |
2960 | && l->l_info[VERSYMIDX (DT_RELACOUNT)]) | |
2961 | #else | |
2962 | /* On e.g. IA-64 or Alpha, relative relocations are processed | |
2963 | only if library is loaded to different address than p_vaddr. */ | |
2964 | if (l->l_addr != 0 && l->l_info[VERSYMIDX (DT_RELACOUNT)]) | |
2965 | #endif | |
c0f62c56 UD |
2966 | num_relative_relocations |
2967 | += l->l_info[VERSYMIDX (DT_RELACOUNT)]->d_un.d_val; | |
2968 | } | |
a21a20a3 UD |
2969 | } |
2970 | ||
42af49f8 UD |
2971 | _dl_debug_printf (" number of relocations: %lu\n" |
2972 | " number of relocations from cache: %lu\n" | |
2973 | " number of relative relocations: %lu\n", | |
2974 | GL(dl_num_relocations), | |
2975 | GL(dl_num_cache_relocations), | |
154d10bd | 2976 | num_relative_relocations); |
db276fa1 | 2977 | |
1e372ded AZ |
2978 | #if HP_TIMING_INLINE |
2979 | print_statistics_item (" time needed to load objects", | |
2980 | load_time, *rtld_total_timep); | |
1531e094 | 2981 | #endif |
db276fa1 | 2982 | } |