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