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