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