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