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