1 /* Run time dynamic linker.
2 Copyright (C) 1995-2012 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/>. */
27 #include <sys/param.h>
32 #include <fpu_control.h>
33 #include <hp-timing.h>
34 #include <bits/libc-lock.h>
35 #include "dynamic-link.h"
36 #include <dl-librecon.h>
37 #include <unsecvars.h>
39 #include <dl-osinfo.h>
40 #include <dl-procinfo.h>
42 #include <stap-probe.h>
43 #include <stackinfo.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
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
;
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
;
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
;
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
)
103 /* List of auditing DSOs. */
104 static struct audit_list
107 struct audit_list
*next
;
110 #ifndef HAVE_INLINED_SYSCALLS
111 /* Set nonzero during loading and initialization of executable and
112 libraries, cleared before the executable's entry point runs. This
113 must not be initialized to nonzero, because the unused dynamic
114 linker loaded in for libc.so's "ld.so.1" dep will provide the
115 definition seen by libc.so's initializer; that value must be zero,
116 and will be since that dynamic linker's _dl_start and dl_main will
118 int _dl_starting_up
= 0;
119 INTVARDEF(_dl_starting_up
)
122 /* This is the structure which defines all variables global to ld.so
123 (except those which cannot be added for some reason). */
124 struct rtld_global _rtld_global
=
126 /* Generally the default presumption without further information is an
127 * executable stack but this is not true for all platforms. */
128 ._dl_stack_flags
= DEFAULT_STACK_PERMS
,
129 #ifdef _LIBC_REENTRANT
130 ._dl_load_lock
= _RTLD_LOCK_RECURSIVE_INITIALIZER
,
131 ._dl_load_write_lock
= _RTLD_LOCK_RECURSIVE_INITIALIZER
,
136 #ifdef _LIBC_REENTRANT
137 [LM_ID_BASE
] = { ._ns_unique_sym_table
138 = { .lock
= _RTLD_LOCK_RECURSIVE_INITIALIZER
} }
142 /* If we would use strong_alias here the compiler would see a
143 non-hidden definition. This would undo the effect of the previous
144 declaration. So spell out was strong_alias does plus add the
145 visibility attribute. */
146 extern struct rtld_global _rtld_local
147 __attribute__ ((alias ("_rtld_global"), visibility ("hidden")));
150 /* This variable is similar to _rtld_local, but all values are
151 read-only after relocation. */
152 struct rtld_global_ro _rtld_global_ro attribute_relro
=
154 /* Get architecture specific initializer. */
155 #include <dl-procinfo.c>
156 #ifdef NEED_DL_SYSINFO
157 ._dl_sysinfo
= DL_SYSINFO_DEFAULT
,
159 ._dl_debug_fd
= STDERR_FILENO
,
160 ._dl_use_load_bias
= -2,
161 ._dl_correct_cache_id
= _DL_CACHE_DEFAULT_ID
,
162 ._dl_hwcap_mask
= HWCAP_IMPORTANT
,
164 ._dl_fpu_control
= _FPU_DEFAULT
,
165 ._dl_pointer_guard
= 1,
166 ._dl_pagesize
= EXEC_PAGESIZE
,
167 ._dl_inhibit_cache
= 0,
169 /* Function pointers. */
170 ._dl_debug_printf
= _dl_debug_printf
,
171 ._dl_catch_error
= _dl_catch_error
,
172 ._dl_signal_error
= _dl_signal_error
,
173 ._dl_mcount
= _dl_mcount_internal
,
174 ._dl_lookup_symbol_x
= _dl_lookup_symbol_x
,
175 ._dl_check_caller
= _dl_check_caller
,
176 ._dl_open
= _dl_open
,
177 ._dl_close
= _dl_close
,
178 ._dl_tls_get_addr_soft
= _dl_tls_get_addr_soft
,
179 #ifdef HAVE_DL_DISCOVER_OSVERSION
180 ._dl_discover_osversion
= _dl_discover_osversion
183 /* If we would use strong_alias here the compiler would see a
184 non-hidden definition. This would undo the effect of the previous
185 declaration. So spell out was strong_alias does plus add the
186 visibility attribute. */
187 extern struct rtld_global_ro _rtld_local_ro
188 __attribute__ ((alias ("_rtld_global_ro"), visibility ("hidden")));
191 static void dl_main (const ElfW(Phdr
) *phdr
, ElfW(Word
) phnum
,
192 ElfW(Addr
) *user_entry
, ElfW(auxv_t
) *auxv
);
194 /* These two variables cannot be moved into .data.rel.ro. */
195 static struct libname_list _dl_rtld_libname
;
196 static struct libname_list _dl_rtld_libname2
;
198 /* We expect less than a second for relocation. */
199 #ifdef HP_SMALL_TIMING_AVAIL
200 # undef HP_TIMING_AVAIL
201 # define HP_TIMING_AVAIL HP_SMALL_TIMING_AVAIL
204 /* Variable for statistics. */
205 #ifndef HP_TIMING_NONAVAIL
206 static hp_timing_t relocate_time
;
207 static hp_timing_t load_time attribute_relro
;
208 static hp_timing_t start_time attribute_relro
;
211 /* Additional definitions needed by TLS initialization. */
212 #ifdef TLS_INIT_HELPER
216 /* Helper function for syscall implementation. */
217 #ifdef DL_SYSINFO_IMPLEMENTATION
218 DL_SYSINFO_IMPLEMENTATION
221 /* Before ld.so is relocated we must not access variables which need
222 relocations. This means variables which are exported. Variables
223 declared as static are fine. If we can mark a variable hidden this
224 is fine, too. The latter is important here. We can avoid setting
225 up a temporary link map for ld.so if we can mark _rtld_global as
227 #ifdef PI_STATIC_AND_HIDDEN
228 # define DONT_USE_BOOTSTRAP_MAP 1
231 #ifdef DONT_USE_BOOTSTRAP_MAP
232 static ElfW(Addr
) _dl_start_final (void *arg
);
234 struct dl_start_final_info
237 #if !defined HP_TIMING_NONAVAIL && HP_TIMING_INLINE
238 hp_timing_t start_time
;
241 static ElfW(Addr
) _dl_start_final (void *arg
,
242 struct dl_start_final_info
*info
);
245 /* These defined magically in the linker script. */
246 extern char _begin
[] attribute_hidden
;
247 extern char _etext
[] attribute_hidden
;
248 extern char _end
[] attribute_hidden
;
254 # error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
257 /* This is the second half of _dl_start (below). It can be inlined safely
258 under DONT_USE_BOOTSTRAP_MAP, where it is careful not to make any GOT
259 references. When the tools don't permit us to avoid using a GOT entry
260 for _dl_rtld_global (no attribute_hidden support), we must make sure
261 this function is not inlined (see below). */
263 #ifdef DONT_USE_BOOTSTRAP_MAP
264 static inline ElfW(Addr
) __attribute__ ((always_inline
))
265 _dl_start_final (void *arg
)
267 static ElfW(Addr
) __attribute__ ((noinline
))
268 _dl_start_final (void *arg
, struct dl_start_final_info
*info
)
271 ElfW(Addr
) start_addr
;
275 /* If it hasn't happen yet record the startup time. */
276 if (! HP_TIMING_INLINE
)
277 HP_TIMING_NOW (start_time
);
278 #if !defined DONT_USE_BOOTSTRAP_MAP && !defined HP_TIMING_NONAVAIL
280 start_time
= info
->start_time
;
283 /* Initialize the timing functions. */
284 HP_TIMING_DIFF_INIT ();
287 /* Transfer data about ourselves to the permanent link_map structure. */
288 #ifndef DONT_USE_BOOTSTRAP_MAP
289 GL(dl_rtld_map
).l_addr
= info
->l
.l_addr
;
290 GL(dl_rtld_map
).l_ld
= info
->l
.l_ld
;
291 memcpy (GL(dl_rtld_map
).l_info
, info
->l
.l_info
,
292 sizeof GL(dl_rtld_map
).l_info
);
293 GL(dl_rtld_map
).l_mach
= info
->l
.l_mach
;
294 GL(dl_rtld_map
).l_relocated
= 1;
296 _dl_setup_hash (&GL(dl_rtld_map
));
297 GL(dl_rtld_map
).l_real
= &GL(dl_rtld_map
);
298 GL(dl_rtld_map
).l_map_start
= (ElfW(Addr
)) _begin
;
299 GL(dl_rtld_map
).l_map_end
= (ElfW(Addr
)) _end
;
300 GL(dl_rtld_map
).l_text_end
= (ElfW(Addr
)) _etext
;
301 /* Copy the TLS related data if necessary. */
302 #ifndef DONT_USE_BOOTSTRAP_MAP
304 assert (info
->l
.l_tls_modid
!= 0);
305 GL(dl_rtld_map
).l_tls_blocksize
= info
->l
.l_tls_blocksize
;
306 GL(dl_rtld_map
).l_tls_align
= info
->l
.l_tls_align
;
307 GL(dl_rtld_map
).l_tls_firstbyte_offset
= info
->l
.l_tls_firstbyte_offset
;
308 GL(dl_rtld_map
).l_tls_initimage_size
= info
->l
.l_tls_initimage_size
;
309 GL(dl_rtld_map
).l_tls_initimage
= info
->l
.l_tls_initimage
;
310 GL(dl_rtld_map
).l_tls_offset
= info
->l
.l_tls_offset
;
311 GL(dl_rtld_map
).l_tls_modid
= 1;
313 # if NO_TLS_OFFSET != 0
314 GL(dl_rtld_map
).l_tls_offset
= NO_TLS_OFFSET
;
321 HP_TIMING_NOW (GL(dl_cpuclock_offset
));
324 /* Initialize the stack end variable. */
325 __libc_stack_end
= __builtin_frame_address (0);
327 /* Call the OS-dependent function to set up life so we can do things like
328 file access. It will call `dl_main' (below) to do all the real work
329 of the dynamic linker, and then unwind our frame and run the user
330 entry point on the same stack we entered on. */
331 start_addr
= _dl_sysdep_start (arg
, &dl_main
);
333 #ifndef HP_TIMING_NONAVAIL
334 hp_timing_t rtld_total_time
;
337 hp_timing_t end_time
;
339 /* Get the current time. */
340 HP_TIMING_NOW (end_time
);
342 /* Compute the difference. */
343 HP_TIMING_DIFF (rtld_total_time
, start_time
, end_time
);
347 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_STATISTICS
, 0))
349 #ifndef HP_TIMING_NONAVAIL
350 print_statistics (&rtld_total_time
);
352 print_statistics (NULL
);
359 static ElfW(Addr
) __attribute_used__ internal_function
360 _dl_start (void *arg
)
362 #ifdef DONT_USE_BOOTSTRAP_MAP
363 # define bootstrap_map GL(dl_rtld_map)
365 struct dl_start_final_info info
;
366 # define bootstrap_map info.l
369 /* This #define produces dynamic linking inline functions for
370 bootstrap relocation instead of general-purpose relocation.
371 Since ld.so must not have any undefined symbols the result
372 is trivial: always the map of ld.so itself. */
373 #define RTLD_BOOTSTRAP
374 #define RESOLVE_MAP(sym, version, flags) (&bootstrap_map)
375 #include "dynamic-link.h"
377 if (HP_TIMING_INLINE
&& HP_TIMING_AVAIL
)
378 #ifdef DONT_USE_BOOTSTRAP_MAP
379 HP_TIMING_NOW (start_time
);
381 HP_TIMING_NOW (info
.start_time
);
384 /* Partly clean the `bootstrap_map' structure up. Don't use
385 `memset' since it might not be built in or inlined and we cannot
386 make function calls at this point. Use '__builtin_memset' if we
387 know it is available. We do not have to clear the memory if we
388 do not have to use the temporary bootstrap_map. Global variables
389 are initialized to zero by default. */
390 #ifndef DONT_USE_BOOTSTRAP_MAP
391 # ifdef HAVE_BUILTIN_MEMSET
392 __builtin_memset (bootstrap_map
.l_info
, '\0', sizeof (bootstrap_map
.l_info
));
395 cnt
< sizeof (bootstrap_map
.l_info
) / sizeof (bootstrap_map
.l_info
[0]);
397 bootstrap_map
.l_info
[cnt
] = 0;
400 bootstrap_map
.l_tls_modid
= 0;
404 /* Figure out the run-time load address of the dynamic linker itself. */
405 bootstrap_map
.l_addr
= elf_machine_load_address ();
407 /* Read our own dynamic section and fill in the info array. */
408 bootstrap_map
.l_ld
= (void *) bootstrap_map
.l_addr
+ elf_machine_dynamic ();
409 elf_get_dynamic_info (&bootstrap_map
, NULL
);
411 #if NO_TLS_OFFSET != 0
412 bootstrap_map
.l_tls_offset
= NO_TLS_OFFSET
;
415 /* Get the dynamic linker's own program header. First we need the ELF
416 file header. The `_begin' symbol created by the linker script points
417 to it. When we have something like GOTOFF relocs, we can use a plain
418 reference to find the runtime address. Without that, we have to rely
419 on the `l_addr' value, which is not the value we want when prelinked. */
423 # ifdef DONT_USE_BOOTSTRAP_MAP
424 = (ElfW(Ehdr
) *) &_begin
;
426 # error This will not work with prelink.
427 = (ElfW(Ehdr
) *) bootstrap_map
.l_addr
;
429 ElfW(Phdr
) *phdr
= (ElfW(Phdr
) *) ((void *) ehdr
+ ehdr
->e_phoff
);
430 size_t cnt
= ehdr
->e_phnum
; /* PT_TLS is usually the last phdr. */
432 if (phdr
[cnt
].p_type
== PT_TLS
)
435 size_t max_align
= MAX (TLS_INIT_TCB_ALIGN
, phdr
[cnt
].p_align
);
438 bootstrap_map
.l_tls_blocksize
= phdr
[cnt
].p_memsz
;
439 bootstrap_map
.l_tls_align
= phdr
[cnt
].p_align
;
440 if (phdr
[cnt
].p_align
== 0)
441 bootstrap_map
.l_tls_firstbyte_offset
= 0;
443 bootstrap_map
.l_tls_firstbyte_offset
= (phdr
[cnt
].p_vaddr
444 & (phdr
[cnt
].p_align
- 1));
445 assert (bootstrap_map
.l_tls_blocksize
!= 0);
446 bootstrap_map
.l_tls_initimage_size
= phdr
[cnt
].p_filesz
;
447 bootstrap_map
.l_tls_initimage
= (void *) (bootstrap_map
.l_addr
448 + phdr
[cnt
].p_vaddr
);
450 /* We can now allocate the initial TLS block. This can happen
451 on the stack. We'll get the final memory later when we
452 know all about the various objects loaded at startup
455 tlsblock
= alloca (roundup (bootstrap_map
.l_tls_blocksize
,
460 tlsblock
= alloca (roundup (TLS_INIT_TCB_SIZE
,
461 bootstrap_map
.l_tls_align
)
462 + bootstrap_map
.l_tls_blocksize
465 /* In case a model with a different layout for the TCB and DTV
466 is defined add another #elif here and in the following #ifs. */
467 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
469 /* Align the TLS block. */
470 tlsblock
= (void *) (((uintptr_t) tlsblock
+ max_align
- 1)
473 /* Initialize the dtv. [0] is the length, [1] the generation
475 initdtv
[0].counter
= 1;
476 initdtv
[1].counter
= 0;
478 /* Initialize the TLS block. */
480 initdtv
[2].pointer
= tlsblock
;
482 bootstrap_map
.l_tls_offset
= roundup (TLS_INIT_TCB_SIZE
,
483 bootstrap_map
.l_tls_align
);
484 initdtv
[2].pointer
= (char *) tlsblock
+ bootstrap_map
.l_tls_offset
;
486 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
488 p
= __mempcpy (initdtv
[2].pointer
, bootstrap_map
.l_tls_initimage
,
489 bootstrap_map
.l_tls_initimage_size
);
490 # ifdef HAVE_BUILTIN_MEMSET
491 __builtin_memset (p
, '\0', (bootstrap_map
.l_tls_blocksize
492 - bootstrap_map
.l_tls_initimage_size
));
495 size_t remaining
= (bootstrap_map
.l_tls_blocksize
496 - bootstrap_map
.l_tls_initimage_size
);
497 while (remaining
-- > 0)
502 /* Install the pointer to the dtv. */
504 /* Initialize the thread pointer. */
506 bootstrap_map
.l_tls_offset
507 = roundup (bootstrap_map
.l_tls_blocksize
, TLS_INIT_TCB_ALIGN
);
509 INSTALL_DTV ((char *) tlsblock
+ bootstrap_map
.l_tls_offset
,
512 const char *lossage
= TLS_INIT_TP ((char *) tlsblock
513 + bootstrap_map
.l_tls_offset
, 0);
515 INSTALL_DTV (tlsblock
, initdtv
);
516 const char *lossage
= TLS_INIT_TP (tlsblock
, 0);
518 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
520 if (__builtin_expect (lossage
!= NULL
, 0))
521 _dl_fatal_printf ("cannot set up thread-local storage: %s\n",
524 /* So far this is module number one. */
525 bootstrap_map
.l_tls_modid
= 1;
527 /* There can only be one PT_TLS entry. */
530 #endif /* USE___THREAD */
532 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
533 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map
.l_info
);
536 if (bootstrap_map
.l_addr
|| ! bootstrap_map
.l_info
[VALIDX(DT_GNU_PRELINKED
)])
538 /* Relocate ourselves so we can do normal function calls and
539 data access using the global offset table. */
541 ELF_DYNAMIC_RELOCATE (&bootstrap_map
, 0, 0, 0);
543 bootstrap_map
.l_relocated
= 1;
545 /* Please note that we don't allow profiling of this object and
546 therefore need not test whether we have to allocate the array
547 for the relocation results (as done in dl-reloc.c). */
549 /* Now life is sane; we can call functions and access global data.
550 Set up to use the operating system facilities, and find out from
551 the operating system's program loader where to find the program
552 header table in core. Put the rest of _dl_start into a separate
553 function, that way the compiler cannot put accesses to the GOT
554 before ELF_DYNAMIC_RELOCATE. */
556 #ifdef DONT_USE_BOOTSTRAP_MAP
557 ElfW(Addr
) entry
= _dl_start_final (arg
);
559 ElfW(Addr
) entry
= _dl_start_final (arg
, &info
);
562 #ifndef ELF_MACHINE_START_ADDRESS
563 # define ELF_MACHINE_START_ADDRESS(map, start) (start)
566 return ELF_MACHINE_START_ADDRESS (GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
, entry
);
572 /* Now life is peachy; we can do all normal operations.
573 On to the real work. */
575 /* Some helper functions. */
577 /* Arguments to relocate_doit. */
586 /* Argument to map_doit. */
588 struct link_map
*loader
;
590 /* Return value of map_doit. */
591 struct link_map
*map
;
597 struct link_map
*map
;
603 struct link_map
*map
;
607 /* Arguments to version_check_doit. */
608 struct version_check_args
615 relocate_doit (void *a
)
617 struct relocate_args
*args
= (struct relocate_args
*) a
;
619 _dl_relocate_object (args
->l
, args
->l
->l_scope
, args
->reloc_mode
, 0);
625 struct map_args
*args
= (struct map_args
*) a
;
626 args
->map
= _dl_map_object (args
->loader
, args
->str
, lt_library
, 0,
627 args
->mode
, LM_ID_BASE
);
631 dlmopen_doit (void *a
)
633 struct dlmopen_args
*args
= (struct dlmopen_args
*) a
;
634 args
->map
= _dl_open (args
->fname
,
635 (RTLD_LAZY
| __RTLD_DLOPEN
| __RTLD_AUDIT
637 dl_main
, LM_ID_NEWLM
, _dl_argc
, INTUSE(_dl_argv
),
642 lookup_doit (void *a
)
644 struct lookup_args
*args
= (struct lookup_args
*) a
;
645 const ElfW(Sym
) *ref
= NULL
;
647 lookup_t l
= _dl_lookup_symbol_x (args
->name
, args
->map
, &ref
,
648 args
->map
->l_local_scope
, NULL
, 0,
649 DL_LOOKUP_RETURN_NEWEST
, NULL
);
651 args
->result
= DL_SYMBOL_ADDRESS (l
, ref
);
655 version_check_doit (void *a
)
657 struct version_check_args
*args
= (struct version_check_args
*) a
;
658 if (_dl_check_all_versions (GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
, 1,
659 args
->dotrace
) && args
->doexit
)
660 /* We cannot start the application. Abort now. */
665 static inline struct link_map
*
666 find_needed (const char *name
)
668 struct r_scope_elem
*scope
= &GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
->l_searchlist
;
669 unsigned int n
= scope
->r_nlist
;
672 if (_dl_name_match_p (name
, scope
->r_list
[n
]))
673 return scope
->r_list
[n
];
675 /* Should never happen. */
680 match_version (const char *string
, struct link_map
*map
)
682 const char *strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
685 #define VERDEFTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
686 if (map
->l_info
[VERDEFTAG
] == NULL
)
687 /* The file has no symbol versioning. */
690 def
= (ElfW(Verdef
) *) ((char *) map
->l_addr
691 + map
->l_info
[VERDEFTAG
]->d_un
.d_ptr
);
694 ElfW(Verdaux
) *aux
= (ElfW(Verdaux
) *) ((char *) def
+ def
->vd_aux
);
696 /* Compare the version strings. */
697 if (strcmp (string
, strtab
+ aux
->vda_name
) == 0)
701 /* If no more definitions we failed to find what we want. */
702 if (def
->vd_next
== 0)
705 /* Next definition. */
706 def
= (ElfW(Verdef
) *) ((char *) def
+ def
->vd_next
);
712 static bool tls_init_tp_called
;
717 /* Number of elements in the static TLS block. */
718 GL(dl_tls_static_nelem
) = GL(dl_tls_max_dtv_idx
);
720 /* Do not do this twice. The audit interface might have required
721 the DTV interfaces to be set up early. */
722 if (GL(dl_initial_dtv
) != NULL
)
725 /* Allocate the array which contains the information about the
726 dtv slots. We allocate a few entries more than needed to
727 avoid the need for reallocation. */
728 size_t nelem
= GL(dl_tls_max_dtv_idx
) + 1 + TLS_SLOTINFO_SURPLUS
;
731 GL(dl_tls_dtv_slotinfo_list
) = (struct dtv_slotinfo_list
*)
732 calloc (sizeof (struct dtv_slotinfo_list
)
733 + nelem
* sizeof (struct dtv_slotinfo
), 1);
734 /* No need to check the return value. If memory allocation failed
735 the program would have been terminated. */
737 struct dtv_slotinfo
*slotinfo
= GL(dl_tls_dtv_slotinfo_list
)->slotinfo
;
738 GL(dl_tls_dtv_slotinfo_list
)->len
= nelem
;
739 GL(dl_tls_dtv_slotinfo_list
)->next
= NULL
;
741 /* Fill in the information from the loaded modules. No namespace
742 but the base one can be filled at this time. */
743 assert (GL(dl_ns
)[LM_ID_BASE
+ 1]._ns_loaded
== NULL
);
745 for (struct link_map
*l
= GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
; l
!= NULL
;
747 if (l
->l_tls_blocksize
!= 0)
749 /* This is a module with TLS data. Store the map reference.
750 The generation counter is zero. */
752 /* slotinfo[i].gen = 0; */
755 assert (i
== GL(dl_tls_max_dtv_idx
));
757 /* Compute the TLS offsets for the various blocks. */
758 _dl_determine_tlsoffset ();
760 /* Construct the static TLS block and the dtv for the initial
761 thread. For some platforms this will include allocating memory
762 for the thread descriptor. The memory for the TLS block will
763 never be freed. It should be allocated accordingly. The dtv
764 array can be changed if dynamic loading requires it. */
765 void *tcbp
= _dl_allocate_tls_storage ();
768 cannot allocate TLS data structures for initial thread");
770 /* Store for detection of the special case by __tls_get_addr
771 so it knows not to pass this dtv to the normal realloc. */
772 GL(dl_initial_dtv
) = GET_DTV (tcbp
);
774 /* And finally install it for the main thread. If ld.so itself uses
775 TLS we know the thread pointer was initialized earlier. */
778 = TLS_INIT_TP (tcbp
, USE___THREAD
);
780 = TLS_INIT_TP (tcbp
, 0);
782 if (__builtin_expect (lossage
!= NULL
, 0))
783 _dl_fatal_printf ("cannot set up thread-local storage: %s\n", lossage
);
784 tls_init_tp_called
= true;
789 #ifdef _LIBC_REENTRANT
790 /* _dl_error_catch_tsd points to this for the single-threaded case.
791 It's reset by the thread library for multithreaded programs. */
792 void ** __attribute__ ((const))
793 _dl_initial_error_catch_tsd (void)
802 do_preload (char *fname
, struct link_map
*main_map
, const char *where
)
805 const char *err_str
= NULL
;
806 struct map_args args
;
810 args
.loader
= main_map
;
811 args
.mode
= __RTLD_SECURE
;
813 unsigned int old_nloaded
= GL(dl_ns
)[LM_ID_BASE
]._ns_nloaded
;
815 (void) _dl_catch_error (&objname
, &err_str
, &malloced
, map_doit
, &args
);
816 if (__builtin_expect (err_str
!= NULL
, 0))
819 ERROR: ld.so: object '%s' from %s cannot be preloaded: ignored.\n",
821 /* No need to call free, this is still before
822 the libc's malloc is used. */
824 else if (GL(dl_ns
)[LM_ID_BASE
]._ns_nloaded
!= old_nloaded
)
825 /* It is no duplicate. */
828 /* Nothing loaded. */
832 #if defined SHARED && defined _LIBC_REENTRANT \
833 && defined __rtld_lock_default_lock_recursive
835 rtld_lock_default_lock_recursive (void *lock
)
837 __rtld_lock_default_lock_recursive (lock
);
841 rtld_lock_default_unlock_recursive (void *lock
)
843 __rtld_lock_default_unlock_recursive (lock
);
851 /* Set up the stack checker's canary. */
852 uintptr_t stack_chk_guard
= _dl_setup_stack_chk_guard (_dl_random
);
853 #ifdef THREAD_SET_STACK_GUARD
854 THREAD_SET_STACK_GUARD (stack_chk_guard
);
856 __stack_chk_guard
= stack_chk_guard
;
859 /* Set up the pointer guard as well, if necessary. */
860 if (GLRO(dl_pointer_guard
))
862 uintptr_t pointer_chk_guard
= _dl_setup_pointer_guard (_dl_random
,
864 #ifdef THREAD_SET_POINTER_GUARD
865 THREAD_SET_POINTER_GUARD (pointer_chk_guard
);
867 __pointer_chk_guard_local
= pointer_chk_guard
;
870 /* We do not need the _dl_random value anymore. The less
871 information we leave behind, the better, so clear the
877 /* The library search path. */
878 static const char *library_path attribute_relro
;
879 /* The list preloaded objects. */
880 static const char *preloadlist attribute_relro
;
881 /* Nonzero if information about versions has to be printed. */
882 static int version_info attribute_relro
;
885 dl_main (const ElfW(Phdr
) *phdr
,
887 ElfW(Addr
) *user_entry
,
890 const ElfW(Phdr
) *ph
;
892 struct link_map
*main_map
;
895 bool has_interp
= false;
897 bool prelinked
= false;
898 bool rtld_is_main
= false;
899 #ifndef HP_TIMING_NONAVAIL
906 #ifdef _LIBC_REENTRANT
907 /* Explicit initialization since the reloc would just be more work. */
908 GL(dl_error_catch_tsd
) = &_dl_initial_error_catch_tsd
;
911 GL(dl_init_static_tls
) = &_dl_nothread_init_static_tls
;
913 #if defined SHARED && defined _LIBC_REENTRANT \
914 && defined __rtld_lock_default_lock_recursive
915 GL(dl_rtld_lock_recursive
) = rtld_lock_default_lock_recursive
;
916 GL(dl_rtld_unlock_recursive
) = rtld_lock_default_unlock_recursive
;
919 /* The explicit initialization here is cheaper than processing the reloc
920 in the _rtld_local definition's initializer. */
921 GL(dl_make_stack_executable_hook
) = &_dl_make_stack_executable
;
923 /* Process the environment variable which control the behaviour. */
924 process_envvars (&mode
);
926 #ifndef HAVE_INLINED_SYSCALLS
927 /* Set up a flag which tells we are just starting. */
928 INTUSE(_dl_starting_up
) = 1;
931 if (*user_entry
== (ElfW(Addr
)) ENTRY_POINT
)
933 /* Ho ho. We are not the program interpreter! We are the program
934 itself! This means someone ran ld.so as a command. Well, that
935 might be convenient to do sometimes. We support it by
936 interpreting the args like this:
938 ld.so PROGRAM ARGS...
940 The first argument is the name of a file containing an ELF
941 executable we will load and run with the following arguments.
942 To simplify life here, PROGRAM is searched for using the
943 normal rules for shared objects, rather than $PATH or anything
944 like that. We just load it and use its entry point; we don't
945 pay attention to its PT_INTERP command (we are the interpreter
946 ourselves). This is an easy way to test a new ld.so before
950 /* Note the place where the dynamic linker actually came from. */
951 GL(dl_rtld_map
).l_name
= rtld_progname
;
954 if (! strcmp (INTUSE(_dl_argv
)[1], "--list"))
957 GLRO(dl_lazy
) = -1; /* This means do no dependency analysis. */
963 else if (! strcmp (INTUSE(_dl_argv
)[1], "--verify"))
971 else if (! strcmp (INTUSE(_dl_argv
)[1], "--inhibit-cache"))
973 GLRO(dl_inhibit_cache
) = 1;
978 else if (! strcmp (INTUSE(_dl_argv
)[1], "--library-path")
981 library_path
= INTUSE(_dl_argv
)[2];
985 INTUSE(_dl_argv
) += 2;
987 else if (! strcmp (INTUSE(_dl_argv
)[1], "--inhibit-rpath")
990 GLRO(dl_inhibit_rpath
) = INTUSE(_dl_argv
)[2];
994 INTUSE(_dl_argv
) += 2;
996 else if (! strcmp (INTUSE(_dl_argv
)[1], "--audit") && _dl_argc
> 2)
998 process_dl_audit (INTUSE(_dl_argv
)[2]);
1002 INTUSE(_dl_argv
) += 2;
1007 /* If we have no further argument the program was called incorrectly.
1008 Grant the user some education. */
1010 _dl_fatal_printf ("\
1011 Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
1012 You have invoked `ld.so', the helper program for shared library executables.\n\
1013 This program usually lives in the file `/lib/ld.so', and special directives\n\
1014 in executable files using ELF shared libraries tell the system's program\n\
1015 loader to load the helper program from this file. This helper program loads\n\
1016 the shared libraries needed by the program executable, prepares the program\n\
1017 to run, and runs it. You may invoke this helper program directly from the\n\
1018 command line to load and run an ELF executable file; this is like executing\n\
1019 that file itself, but always uses this helper program from the file you\n\
1020 specified, instead of the helper program file specified in the executable\n\
1021 file you run. This is mostly of use for maintainers to test new versions\n\
1022 of this helper program; chances are you did not intend to run this program.\n\
1024 --list list all dependencies and how they are resolved\n\
1025 --verify verify that given object really is a dynamically linked\n\
1026 object we can handle\n\
1027 --inhibit-cache Do not use " LD_SO_CACHE
"\n\
1028 --library-path PATH use given PATH instead of content of the environment\n\
1029 variable LD_LIBRARY_PATH\n\
1030 --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\
1032 --audit LIST use objects named in LIST as auditors\n");
1038 /* The initialization of _dl_stack_flags done below assumes the
1039 executable's PT_GNU_STACK may have been honored by the kernel, and
1040 so a PT_GNU_STACK with PF_X set means the stack started out with
1041 execute permission. However, this is not really true if the
1042 dynamic linker is the executable the kernel loaded. For this
1043 case, we must reinitialize _dl_stack_flags to match the dynamic
1044 linker itself. If the dynamic linker was built with a
1045 PT_GNU_STACK, then the kernel may have loaded us with a
1046 nonexecutable stack that we will have to make executable when we
1047 load the program below unless it has a PT_GNU_STACK indicating
1048 nonexecutable stack is ok. */
1050 for (ph
= phdr
; ph
< &phdr
[phnum
]; ++ph
)
1051 if (ph
->p_type
== PT_GNU_STACK
)
1053 GL(dl_stack_flags
) = ph
->p_flags
;
1057 if (__builtin_expect (mode
, normal
) == verify
)
1059 const char *objname
;
1060 const char *err_str
= NULL
;
1061 struct map_args args
;
1064 args
.str
= rtld_progname
;
1066 args
.mode
= __RTLD_OPENEXEC
;
1067 (void) _dl_catch_error (&objname
, &err_str
, &malloced
, map_doit
,
1069 if (__builtin_expect (err_str
!= NULL
, 0))
1070 /* We don't free the returned string, the programs stops
1072 _exit (EXIT_FAILURE
);
1076 HP_TIMING_NOW (start
);
1077 _dl_map_object (NULL
, rtld_progname
, lt_library
, 0,
1078 __RTLD_OPENEXEC
, LM_ID_BASE
);
1079 HP_TIMING_NOW (stop
);
1081 HP_TIMING_DIFF (load_time
, start
, stop
);
1084 /* Now the map for the main executable is available. */
1085 main_map
= GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
;
1087 if (__builtin_expect (mode
, normal
) == normal
1088 && GL(dl_rtld_map
).l_info
[DT_SONAME
] != NULL
1089 && main_map
->l_info
[DT_SONAME
] != NULL
1090 && strcmp ((const char *) D_PTR (&GL(dl_rtld_map
), l_info
[DT_STRTAB
])
1091 + GL(dl_rtld_map
).l_info
[DT_SONAME
]->d_un
.d_val
,
1092 (const char *) D_PTR (main_map
, l_info
[DT_STRTAB
])
1093 + main_map
->l_info
[DT_SONAME
]->d_un
.d_val
) == 0)
1094 _dl_fatal_printf ("loader cannot load itself\n");
1096 phdr
= main_map
->l_phdr
;
1097 phnum
= main_map
->l_phnum
;
1098 /* We overwrite here a pointer to a malloc()ed string. But since
1099 the malloc() implementation used at this point is the dummy
1100 implementations which has no real free() function it does not
1101 makes sense to free the old string first. */
1102 main_map
->l_name
= (char *) "";
1103 *user_entry
= main_map
->l_entry
;
1105 #ifdef HAVE_AUX_VECTOR
1106 /* Adjust the on-stack auxiliary vector so that it looks like the
1107 binary was executed directly. */
1108 for (ElfW(auxv_t
) *av
= auxv
; av
->a_type
!= AT_NULL
; av
++)
1112 av
->a_un
.a_val
= (uintptr_t) phdr
;
1115 av
->a_un
.a_val
= phnum
;
1118 av
->a_un
.a_val
= *user_entry
;
1125 /* Create a link_map for the executable itself.
1126 This will be what dlopen on "" returns. */
1127 main_map
= _dl_new_object ((char *) "", "", lt_executable
, NULL
,
1128 __RTLD_OPENEXEC
, LM_ID_BASE
);
1129 assert (main_map
!= NULL
);
1130 main_map
->l_phdr
= phdr
;
1131 main_map
->l_phnum
= phnum
;
1132 main_map
->l_entry
= *user_entry
;
1134 /* Even though the link map is not yet fully initialized we can add
1135 it to the map list since there are no possible users running yet. */
1136 _dl_add_to_namespace_list (main_map
, LM_ID_BASE
);
1137 assert (main_map
== GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
);
1139 /* At this point we are in a bit of trouble. We would have to
1140 fill in the values for l_dev and l_ino. But in general we
1141 do not know where the file is. We also do not handle AT_EXECFD
1142 even if it would be passed up.
1144 We leave the values here defined to 0. This is normally no
1145 problem as the program code itself is normally no shared
1146 object and therefore cannot be loaded dynamically. Nothing
1147 prevent the use of dynamic binaries and in these situations
1148 we might get problems. We might not be able to find out
1149 whether the object is already loaded. But since there is no
1150 easy way out and because the dynamic binary must also not
1151 have an SONAME we ignore this program for now. If it becomes
1152 a problem we can force people using SONAMEs. */
1154 /* We delay initializing the path structure until we got the dynamic
1155 information for the program. */
1158 main_map
->l_map_end
= 0;
1159 main_map
->l_text_end
= 0;
1160 /* Perhaps the executable has no PT_LOAD header entries at all. */
1161 main_map
->l_map_start
= ~0;
1162 /* And it was opened directly. */
1163 ++main_map
->l_direct_opencount
;
1165 /* Scan the program header table for the dynamic section. */
1166 for (ph
= phdr
; ph
< &phdr
[phnum
]; ++ph
)
1170 /* Find out the load address. */
1171 main_map
->l_addr
= (ElfW(Addr
)) phdr
- ph
->p_vaddr
;
1174 /* This tells us where to find the dynamic section,
1175 which tells us everything we need to do. */
1176 main_map
->l_ld
= (void *) main_map
->l_addr
+ ph
->p_vaddr
;
1179 /* This "interpreter segment" was used by the program loader to
1180 find the program interpreter, which is this program itself, the
1181 dynamic linker. We note what name finds us, so that a future
1182 dlopen call or DT_NEEDED entry, for something that wants to link
1183 against the dynamic linker as a shared library, will know that
1184 the shared object is already loaded. */
1185 _dl_rtld_libname
.name
= ((const char *) main_map
->l_addr
1187 /* _dl_rtld_libname.next = NULL; Already zero. */
1188 GL(dl_rtld_map
).l_libname
= &_dl_rtld_libname
;
1190 /* Ordinarilly, we would get additional names for the loader from
1191 our DT_SONAME. This can't happen if we were actually linked as
1192 a static executable (detect this case when we have no DYNAMIC).
1193 If so, assume the filename component of the interpreter path to
1194 be our SONAME, and add it to our name list. */
1195 if (GL(dl_rtld_map
).l_ld
== NULL
)
1197 const char *p
= NULL
;
1198 const char *cp
= _dl_rtld_libname
.name
;
1200 /* Find the filename part of the path. */
1207 _dl_rtld_libname2
.name
= p
;
1208 /* _dl_rtld_libname2.next = NULL; Already zero. */
1209 _dl_rtld_libname
.next
= &_dl_rtld_libname2
;
1217 ElfW(Addr
) mapstart
;
1218 ElfW(Addr
) allocend
;
1220 /* Remember where the main program starts in memory. */
1221 mapstart
= (main_map
->l_addr
1222 + (ph
->p_vaddr
& ~(GLRO(dl_pagesize
) - 1)));
1223 if (main_map
->l_map_start
> mapstart
)
1224 main_map
->l_map_start
= mapstart
;
1226 /* Also where it ends. */
1227 allocend
= main_map
->l_addr
+ ph
->p_vaddr
+ ph
->p_memsz
;
1228 if (main_map
->l_map_end
< allocend
)
1229 main_map
->l_map_end
= allocend
;
1230 if ((ph
->p_flags
& PF_X
) && allocend
> main_map
->l_text_end
)
1231 main_map
->l_text_end
= allocend
;
1236 if (ph
->p_memsz
> 0)
1238 /* Note that in the case the dynamic linker we duplicate work
1239 here since we read the PT_TLS entry already in
1240 _dl_start_final. But the result is repeatable so do not
1241 check for this special but unimportant case. */
1242 main_map
->l_tls_blocksize
= ph
->p_memsz
;
1243 main_map
->l_tls_align
= ph
->p_align
;
1244 if (ph
->p_align
== 0)
1245 main_map
->l_tls_firstbyte_offset
= 0;
1247 main_map
->l_tls_firstbyte_offset
= (ph
->p_vaddr
1248 & (ph
->p_align
- 1));
1249 main_map
->l_tls_initimage_size
= ph
->p_filesz
;
1250 main_map
->l_tls_initimage
= (void *) ph
->p_vaddr
;
1252 /* This image gets the ID one. */
1253 GL(dl_tls_max_dtv_idx
) = main_map
->l_tls_modid
= 1;
1258 GL(dl_stack_flags
) = ph
->p_flags
;
1262 main_map
->l_relro_addr
= ph
->p_vaddr
;
1263 main_map
->l_relro_size
= ph
->p_memsz
;
1267 /* Adjust the address of the TLS initialization image in case
1268 the executable is actually an ET_DYN object. */
1269 if (main_map
->l_tls_initimage
!= NULL
)
1270 main_map
->l_tls_initimage
1271 = (char *) main_map
->l_tls_initimage
+ main_map
->l_addr
;
1272 if (! main_map
->l_map_end
)
1273 main_map
->l_map_end
= ~0;
1274 if (! main_map
->l_text_end
)
1275 main_map
->l_text_end
= ~0;
1276 if (! GL(dl_rtld_map
).l_libname
&& GL(dl_rtld_map
).l_name
)
1278 /* We were invoked directly, so the program might not have a
1280 _dl_rtld_libname
.name
= GL(dl_rtld_map
).l_name
;
1281 /* _dl_rtld_libname.next = NULL; Already zero. */
1282 GL(dl_rtld_map
).l_libname
= &_dl_rtld_libname
;
1285 assert (GL(dl_rtld_map
).l_libname
); /* How else did we get here? */
1287 /* If the current libname is different from the SONAME, add the
1289 if (GL(dl_rtld_map
).l_info
[DT_SONAME
] != NULL
1290 && strcmp (GL(dl_rtld_map
).l_libname
->name
,
1291 (const char *) D_PTR (&GL(dl_rtld_map
), l_info
[DT_STRTAB
])
1292 + GL(dl_rtld_map
).l_info
[DT_SONAME
]->d_un
.d_val
) != 0)
1294 static struct libname_list newname
;
1295 newname
.name
= ((char *) D_PTR (&GL(dl_rtld_map
), l_info
[DT_STRTAB
])
1296 + GL(dl_rtld_map
).l_info
[DT_SONAME
]->d_un
.d_ptr
);
1297 newname
.next
= NULL
;
1298 newname
.dont_free
= 1;
1300 assert (GL(dl_rtld_map
).l_libname
->next
== NULL
);
1301 GL(dl_rtld_map
).l_libname
->next
= &newname
;
1303 /* The ld.so must be relocated since otherwise loading audit modules
1304 will fail since they reuse the very same ld.so. */
1305 assert (GL(dl_rtld_map
).l_relocated
);
1309 /* Extract the contents of the dynamic section for easy access. */
1310 elf_get_dynamic_info (main_map
, NULL
);
1311 /* Set up our cache of pointers into the hash table. */
1312 _dl_setup_hash (main_map
);
1315 if (__builtin_expect (mode
, normal
) == verify
)
1317 /* We were called just to verify that this is a dynamic
1318 executable using us as the program interpreter. Exit with an
1319 error if we were not able to load the binary or no interpreter
1320 is specified (i.e., this is no dynamically linked binary. */
1321 if (main_map
->l_ld
== NULL
)
1324 /* We allow here some platform specific code. */
1325 #ifdef DISTINGUISH_LIB_VERSIONS
1326 DISTINGUISH_LIB_VERSIONS
;
1328 _exit (has_interp
? 0 : 2);
1331 struct link_map
**first_preload
= &GL(dl_rtld_map
).l_next
;
1332 #if defined NEED_DL_SYSINFO || defined NEED_DL_SYSINFO_DSO
1333 /* Set up the data structures for the system-supplied DSO early,
1334 so they can influence _dl_init_paths. */
1335 if (GLRO(dl_sysinfo_dso
) != NULL
)
1337 /* Do an abridged version of the work _dl_map_object_from_fd would do
1338 to map in the object. It's already mapped and prelinked (and
1339 better be, since it's read-only and so we couldn't relocate it).
1340 We just want our data structures to describe it as if we had just
1341 mapped and relocated it normally. */
1342 struct link_map
*l
= _dl_new_object ((char *) "", "", lt_library
, NULL
,
1344 if (__builtin_expect (l
!= NULL
, 1))
1346 static ElfW(Dyn
) dyn_temp
[DL_RO_DYN_TEMP_CNT
] attribute_relro
;
1348 l
->l_phdr
= ((const void *) GLRO(dl_sysinfo_dso
)
1349 + GLRO(dl_sysinfo_dso
)->e_phoff
);
1350 l
->l_phnum
= GLRO(dl_sysinfo_dso
)->e_phnum
;
1351 for (uint_fast16_t i
= 0; i
< l
->l_phnum
; ++i
)
1353 const ElfW(Phdr
) *const ph
= &l
->l_phdr
[i
];
1354 if (ph
->p_type
== PT_DYNAMIC
)
1356 l
->l_ld
= (void *) ph
->p_vaddr
;
1357 l
->l_ldnum
= ph
->p_memsz
/ sizeof (ElfW(Dyn
));
1359 else if (ph
->p_type
== PT_LOAD
)
1362 l
->l_addr
= ph
->p_vaddr
;
1363 if (ph
->p_vaddr
+ ph
->p_memsz
>= l
->l_map_end
)
1364 l
->l_map_end
= ph
->p_vaddr
+ ph
->p_memsz
;
1365 if ((ph
->p_flags
& PF_X
)
1366 && ph
->p_vaddr
+ ph
->p_memsz
>= l
->l_text_end
)
1367 l
->l_text_end
= ph
->p_vaddr
+ ph
->p_memsz
;
1370 /* There must be no TLS segment. */
1371 assert (ph
->p_type
!= PT_TLS
);
1373 l
->l_map_start
= (ElfW(Addr
)) GLRO(dl_sysinfo_dso
);
1374 l
->l_addr
= l
->l_map_start
- l
->l_addr
;
1375 l
->l_map_end
+= l
->l_addr
;
1376 l
->l_text_end
+= l
->l_addr
;
1377 l
->l_ld
= (void *) ((ElfW(Addr
)) l
->l_ld
+ l
->l_addr
);
1378 elf_get_dynamic_info (l
, dyn_temp
);
1382 /* The vDSO is always used. */
1385 /* Initialize l_local_scope to contain just this map. This allows
1386 the use of dl_lookup_symbol_x to resolve symbols within the vdso.
1387 So we create a single entry list pointing to l_real as its only
1389 l
->l_local_scope
[0]->r_nlist
= 1;
1390 l
->l_local_scope
[0]->r_list
= &l
->l_real
;
1392 /* Now that we have the info handy, use the DSO image's soname
1393 so this object can be looked up by name. Note that we do not
1394 set l_name here. That field gives the file name of the DSO,
1395 and this DSO is not associated with any file. */
1396 if (l
->l_info
[DT_SONAME
] != NULL
)
1398 /* Work around a kernel problem. The kernel cannot handle
1399 addresses in the vsyscall DSO pages in writev() calls. */
1400 const char *dsoname
= ((char *) D_PTR (l
, l_info
[DT_STRTAB
])
1401 + l
->l_info
[DT_SONAME
]->d_un
.d_val
);
1402 size_t len
= strlen (dsoname
);
1403 char *copy
= malloc (len
);
1405 _dl_fatal_printf ("out of memory\n");
1406 l
->l_libname
->name
= l
->l_name
= memcpy (copy
, dsoname
, len
);
1409 /* Add the vDSO to the object list. */
1410 _dl_add_to_namespace_list (l
, LM_ID_BASE
);
1412 /* Rearrange the list so this DSO appears after rtld_map. */
1413 assert (l
->l_next
== NULL
);
1414 assert (l
->l_prev
== main_map
);
1415 GL(dl_rtld_map
).l_next
= l
;
1416 l
->l_prev
= &GL(dl_rtld_map
);
1417 first_preload
= &l
->l_next
;
1419 /* We have a prelinked DSO preloaded by the system. */
1420 GLRO(dl_sysinfo_map
) = l
;
1421 # ifdef NEED_DL_SYSINFO
1422 if (GLRO(dl_sysinfo
) == DL_SYSINFO_DEFAULT
)
1423 GLRO(dl_sysinfo
) = GLRO(dl_sysinfo_dso
)->e_entry
+ l
->l_addr
;
1429 #ifdef DL_SYSDEP_OSCHECK
1430 DL_SYSDEP_OSCHECK (_dl_fatal_printf
);
1433 /* Initialize the data structures for the search paths for shared
1435 _dl_init_paths (library_path
);
1437 /* Initialize _r_debug. */
1438 struct r_debug
*r
= _dl_debug_initialize (GL(dl_rtld_map
).l_addr
,
1440 r
->r_state
= RT_CONSISTENT
;
1442 /* Put the link_map for ourselves on the chain so it can be found by
1443 name. Note that at this point the global chain of link maps contains
1444 exactly one element, which is pointed to by dl_loaded. */
1445 if (! GL(dl_rtld_map
).l_name
)
1446 /* If not invoked directly, the dynamic linker shared object file was
1447 found by the PT_INTERP name. */
1448 GL(dl_rtld_map
).l_name
= (char *) GL(dl_rtld_map
).l_libname
->name
;
1449 GL(dl_rtld_map
).l_type
= lt_library
;
1450 main_map
->l_next
= &GL(dl_rtld_map
);
1451 GL(dl_rtld_map
).l_prev
= main_map
;
1452 ++GL(dl_ns
)[LM_ID_BASE
]._ns_nloaded
;
1455 /* If LD_USE_LOAD_BIAS env variable has not been seen, default
1456 to not using bias for non-prelinked PIEs and libraries
1457 and using it for executables or prelinked PIEs or libraries. */
1458 if (GLRO(dl_use_load_bias
) == (ElfW(Addr
)) -2)
1459 GLRO(dl_use_load_bias
) = main_map
->l_addr
== 0 ? -1 : 0;
1461 /* Set up the program header information for the dynamic linker
1462 itself. It is needed in the dl_iterate_phdr() callbacks. */
1463 ElfW(Ehdr
) *rtld_ehdr
= (ElfW(Ehdr
) *) GL(dl_rtld_map
).l_map_start
;
1464 ElfW(Phdr
) *rtld_phdr
= (ElfW(Phdr
) *) (GL(dl_rtld_map
).l_map_start
1465 + rtld_ehdr
->e_phoff
);
1466 GL(dl_rtld_map
).l_phdr
= rtld_phdr
;
1467 GL(dl_rtld_map
).l_phnum
= rtld_ehdr
->e_phnum
;
1470 /* PT_GNU_RELRO is usually the last phdr. */
1471 size_t cnt
= rtld_ehdr
->e_phnum
;
1473 if (rtld_phdr
[cnt
].p_type
== PT_GNU_RELRO
)
1475 GL(dl_rtld_map
).l_relro_addr
= rtld_phdr
[cnt
].p_vaddr
;
1476 GL(dl_rtld_map
).l_relro_size
= rtld_phdr
[cnt
].p_memsz
;
1480 /* Add the dynamic linker to the TLS list if it also uses TLS. */
1481 if (GL(dl_rtld_map
).l_tls_blocksize
!= 0)
1482 /* Assign a module ID. Do this before loading any audit modules. */
1483 GL(dl_rtld_map
).l_tls_modid
= _dl_next_tls_modid ();
1485 /* If we have auditing DSOs to load, do it now. */
1486 if (__builtin_expect (audit_list
!= NULL
, 0))
1488 /* Iterate over all entries in the list. The order is important. */
1489 struct audit_ifaces
*last_audit
= NULL
;
1490 struct audit_list
*al
= audit_list
->next
;
1492 /* Since we start using the auditing DSOs right away we need to
1493 initialize the data structures now. */
1496 /* Initialize security features. We need to do it this early
1497 since otherwise the constructors of the audit libraries will
1498 use different values (especially the pointer guard) and will
1504 int tls_idx
= GL(dl_tls_max_dtv_idx
);
1506 /* Now it is time to determine the layout of the static TLS
1507 block and allocate it for the initial thread. Note that we
1508 always allocate the static block, we never defer it even if
1509 no DF_STATIC_TLS bit is set. The reason is that we know
1510 glibc will use the static model. */
1511 struct dlmopen_args dlmargs
;
1512 dlmargs
.fname
= al
->name
;
1515 const char *objname
;
1516 const char *err_str
= NULL
;
1518 (void) _dl_catch_error (&objname
, &err_str
, &malloced
, dlmopen_doit
,
1520 if (__builtin_expect (err_str
!= NULL
, 0))
1523 _dl_error_printf ("\
1524 ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n",
1527 free ((char *) err_str
);
1531 struct lookup_args largs
;
1532 largs
.name
= "la_version";
1533 largs
.map
= dlmargs
.map
;
1535 /* Check whether the interface version matches. */
1536 (void) _dl_catch_error (&objname
, &err_str
, &malloced
,
1537 lookup_doit
, &largs
);
1539 unsigned int (*laversion
) (unsigned int);
1542 && (laversion
= largs
.result
) != NULL
1543 && (lav
= laversion (LAV_CURRENT
)) > 0
1544 && lav
<= LAV_CURRENT
)
1546 /* Allocate structure for the callback function pointers.
1547 This call can never fail. */
1550 struct audit_ifaces ifaces
;
1551 #define naudit_ifaces 8
1552 void (*fptr
[naudit_ifaces
]) (void);
1553 } *newp
= malloc (sizeof (*newp
));
1555 /* Names of the auditing interfaces. All in one
1557 static const char audit_iface_names
[] =
1562 #if __ELF_NATIVE_CLASS == 32
1564 #elif __ELF_NATIVE_CLASS == 64
1567 # error "__ELF_NATIVE_CLASS must be defined"
1569 #define STRING(s) __STRING (s)
1570 "la_" STRING (ARCH_LA_PLTENTER
) "\0"
1571 "la_" STRING (ARCH_LA_PLTEXIT
) "\0"
1573 unsigned int cnt
= 0;
1574 const char *cp
= audit_iface_names
;
1578 (void) _dl_catch_error (&objname
, &err_str
, &malloced
,
1579 lookup_doit
, &largs
);
1581 /* Store the pointer. */
1582 if (err_str
== NULL
&& largs
.result
!= NULL
)
1584 newp
->fptr
[cnt
] = largs
.result
;
1586 /* The dynamic linker link map is statically
1587 allocated, initialize the data now. */
1588 GL(dl_rtld_map
).l_audit
[cnt
].cookie
1589 = (intptr_t) &GL(dl_rtld_map
);
1592 newp
->fptr
[cnt
] = NULL
;
1595 cp
= (char *) rawmemchr (cp
, '\0') + 1;
1597 while (*cp
!= '\0');
1598 assert (cnt
== naudit_ifaces
);
1600 /* Now append the new auditing interface to the list. */
1601 newp
->ifaces
.next
= NULL
;
1602 if (last_audit
== NULL
)
1603 last_audit
= GLRO(dl_audit
) = &newp
->ifaces
;
1605 last_audit
= last_audit
->next
= &newp
->ifaces
;
1608 /* Mark the DSO as being used for auditing. */
1609 dlmargs
.map
->l_auditing
= 1;
1613 /* We cannot use the DSO, it does not have the
1614 appropriate interfaces or it expects something
1617 Lmid_t ns
= dlmargs
.map
->l_ns
;
1619 _dl_close (dlmargs
.map
);
1621 /* Make sure the namespace has been cleared entirely. */
1622 assert (GL(dl_ns
)[ns
]._ns_loaded
== NULL
);
1623 assert (GL(dl_ns
)[ns
]._ns_nloaded
== 0);
1625 GL(dl_tls_max_dtv_idx
) = tls_idx
;
1632 while (al
!= audit_list
->next
);
1634 /* If we have any auditing modules, announce that we already
1635 have two objects loaded. */
1636 if (__builtin_expect (GLRO(dl_naudit
) > 0, 0))
1638 struct link_map
*ls
[2] = { main_map
, &GL(dl_rtld_map
) };
1640 for (unsigned int outer
= 0; outer
< 2; ++outer
)
1642 struct audit_ifaces
*afct
= GLRO(dl_audit
);
1643 for (unsigned int cnt
= 0; cnt
< GLRO(dl_naudit
); ++cnt
)
1645 if (afct
->objopen
!= NULL
)
1647 ls
[outer
]->l_audit
[cnt
].bindflags
1648 = afct
->objopen (ls
[outer
], LM_ID_BASE
,
1649 &ls
[outer
]->l_audit
[cnt
].cookie
);
1651 ls
[outer
]->l_audit_any_plt
1652 |= ls
[outer
]->l_audit
[cnt
].bindflags
!= 0;
1661 /* Set up debugging before the debugger is notified for the first time. */
1662 #ifdef ELF_MACHINE_DEBUG_SETUP
1663 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
1664 ELF_MACHINE_DEBUG_SETUP (main_map
, r
);
1665 ELF_MACHINE_DEBUG_SETUP (&GL(dl_rtld_map
), r
);
1667 if (main_map
->l_info
[DT_DEBUG
] != NULL
)
1668 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
1669 with the run-time address of the r_debug structure */
1670 main_map
->l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
1672 /* Fill in the pointer in the dynamic linker's own dynamic section, in
1673 case you run gdb on the dynamic linker directly. */
1674 if (GL(dl_rtld_map
).l_info
[DT_DEBUG
] != NULL
)
1675 GL(dl_rtld_map
).l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
1678 /* We start adding objects. */
1679 r
->r_state
= RT_ADD
;
1681 LIBC_PROBE (init_start
, 2, LM_ID_BASE
, r
);
1683 /* Auditing checkpoint: we are ready to signal that the initial map
1684 is being constructed. */
1685 if (__builtin_expect (GLRO(dl_naudit
) > 0, 0))
1687 struct audit_ifaces
*afct
= GLRO(dl_audit
);
1688 for (unsigned int cnt
= 0; cnt
< GLRO(dl_naudit
); ++cnt
)
1690 if (afct
->activity
!= NULL
)
1691 afct
->activity (&main_map
->l_audit
[cnt
].cookie
, LA_ACT_ADD
);
1697 /* We have two ways to specify objects to preload: via environment
1698 variable and via the file /etc/ld.so.preload. The latter can also
1699 be used when security is enabled. */
1700 assert (*first_preload
== NULL
);
1701 struct link_map
**preloads
= NULL
;
1702 unsigned int npreloads
= 0;
1704 if (__builtin_expect (preloadlist
!= NULL
, 0))
1706 /* The LD_PRELOAD environment variable gives list of libraries
1707 separated by white space or colons that are loaded before the
1708 executable's dependencies and prepended to the global scope
1709 list. If the binary is running setuid all elements
1710 containing a '/' are ignored since it is insecure. */
1711 char *list
= strdupa (preloadlist
);
1714 HP_TIMING_NOW (start
);
1716 /* Prevent optimizing strsep. Speed is not important here. */
1717 while ((p
= (strsep
) (&list
, " :")) != NULL
)
1719 && (__builtin_expect (! INTUSE(__libc_enable_secure
), 1)
1720 || strchr (p
, '/') == NULL
))
1721 npreloads
+= do_preload (p
, main_map
, "LD_PRELOAD");
1723 HP_TIMING_NOW (stop
);
1724 HP_TIMING_DIFF (diff
, start
, stop
);
1725 HP_TIMING_ACCUM_NT (load_time
, diff
);
1728 /* There usually is no ld.so.preload file, it should only be used
1729 for emergencies and testing. So the open call etc should usually
1730 fail. Using access() on a non-existing file is faster than using
1731 open(). So we do this first. If it succeeds we do almost twice
1732 the work but this does not matter, since it is not for production
1734 static const char preload_file
[] = "/etc/ld.so.preload";
1735 if (__builtin_expect (__access (preload_file
, R_OK
) == 0, 0))
1737 /* Read the contents of the file. */
1738 file
= _dl_sysdep_read_whole_file (preload_file
, &file_size
,
1739 PROT_READ
| PROT_WRITE
);
1740 if (__builtin_expect (file
!= MAP_FAILED
, 0))
1742 /* Parse the file. It contains names of libraries to be loaded,
1743 separated by white spaces or `:'. It may also contain
1744 comments introduced by `#'. */
1749 /* Eliminate comments. */
1754 char *comment
= memchr (runp
, '#', rest
);
1755 if (comment
== NULL
)
1758 rest
-= comment
- runp
;
1761 while (--rest
> 0 && *++comment
!= '\n');
1764 /* We have one problematic case: if we have a name at the end of
1765 the file without a trailing terminating characters, we cannot
1766 place the \0. Handle the case separately. */
1767 if (file
[file_size
- 1] != ' ' && file
[file_size
- 1] != '\t'
1768 && file
[file_size
- 1] != '\n' && file
[file_size
- 1] != ':')
1770 problem
= &file
[file_size
];
1771 while (problem
> file
&& problem
[-1] != ' '
1772 && problem
[-1] != '\t'
1773 && problem
[-1] != '\n' && problem
[-1] != ':')
1782 file
[file_size
- 1] = '\0';
1785 HP_TIMING_NOW (start
);
1787 if (file
!= problem
)
1791 while ((p
= strsep (&runp
, ": \t\n")) != NULL
)
1793 npreloads
+= do_preload (p
, main_map
, preload_file
);
1796 if (problem
!= NULL
)
1798 char *p
= strndupa (problem
, file_size
- (problem
- file
));
1800 npreloads
+= do_preload (p
, main_map
, preload_file
);
1803 HP_TIMING_NOW (stop
);
1804 HP_TIMING_DIFF (diff
, start
, stop
);
1805 HP_TIMING_ACCUM_NT (load_time
, diff
);
1807 /* We don't need the file anymore. */
1808 __munmap (file
, file_size
);
1812 if (__builtin_expect (*first_preload
!= NULL
, 0))
1814 /* Set up PRELOADS with a vector of the preloaded libraries. */
1815 struct link_map
*l
= *first_preload
;
1816 preloads
= __alloca (npreloads
* sizeof preloads
[0]);
1823 assert (i
== npreloads
);
1826 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
1827 specified some libraries to load, these are inserted before the actual
1828 dependencies in the executable's searchlist for symbol resolution. */
1829 HP_TIMING_NOW (start
);
1830 _dl_map_object_deps (main_map
, preloads
, npreloads
, mode
== trace
, 0);
1831 HP_TIMING_NOW (stop
);
1832 HP_TIMING_DIFF (diff
, start
, stop
);
1833 HP_TIMING_ACCUM_NT (load_time
, diff
);
1835 /* Mark all objects as being in the global scope. */
1836 for (i
= main_map
->l_searchlist
.r_nlist
; i
> 0; )
1837 main_map
->l_searchlist
.r_list
[--i
]->l_global
= 1;
1839 /* Remove _dl_rtld_map from the chain. */
1840 GL(dl_rtld_map
).l_prev
->l_next
= GL(dl_rtld_map
).l_next
;
1841 if (GL(dl_rtld_map
).l_next
!= NULL
)
1842 GL(dl_rtld_map
).l_next
->l_prev
= GL(dl_rtld_map
).l_prev
;
1844 for (i
= 1; i
< main_map
->l_searchlist
.r_nlist
; ++i
)
1845 if (main_map
->l_searchlist
.r_list
[i
] == &GL(dl_rtld_map
))
1848 bool rtld_multiple_ref
= false;
1849 if (__builtin_expect (i
< main_map
->l_searchlist
.r_nlist
, 1))
1851 /* Some DT_NEEDED entry referred to the interpreter object itself, so
1852 put it back in the list of visible objects. We insert it into the
1853 chain in symbol search order because gdb uses the chain's order as
1854 its symbol search order. */
1855 rtld_multiple_ref
= true;
1857 GL(dl_rtld_map
).l_prev
= main_map
->l_searchlist
.r_list
[i
- 1];
1858 if (__builtin_expect (mode
, normal
) == normal
)
1860 GL(dl_rtld_map
).l_next
= (i
+ 1 < main_map
->l_searchlist
.r_nlist
1861 ? main_map
->l_searchlist
.r_list
[i
+ 1]
1863 #if defined NEED_DL_SYSINFO || defined NEED_DL_SYSINFO_DSO
1864 if (GLRO(dl_sysinfo_map
) != NULL
1865 && GL(dl_rtld_map
).l_prev
->l_next
== GLRO(dl_sysinfo_map
)
1866 && GL(dl_rtld_map
).l_next
!= GLRO(dl_sysinfo_map
))
1867 GL(dl_rtld_map
).l_prev
= GLRO(dl_sysinfo_map
);
1871 /* In trace mode there might be an invisible object (which we
1872 could not find) after the previous one in the search list.
1873 In this case it doesn't matter much where we put the
1874 interpreter object, so we just initialize the list pointer so
1875 that the assertion below holds. */
1876 GL(dl_rtld_map
).l_next
= GL(dl_rtld_map
).l_prev
->l_next
;
1878 assert (GL(dl_rtld_map
).l_prev
->l_next
== GL(dl_rtld_map
).l_next
);
1879 GL(dl_rtld_map
).l_prev
->l_next
= &GL(dl_rtld_map
);
1880 if (GL(dl_rtld_map
).l_next
!= NULL
)
1882 assert (GL(dl_rtld_map
).l_next
->l_prev
== GL(dl_rtld_map
).l_prev
);
1883 GL(dl_rtld_map
).l_next
->l_prev
= &GL(dl_rtld_map
);
1887 /* Now let us see whether all libraries are available in the
1888 versions we need. */
1890 struct version_check_args args
;
1891 args
.doexit
= mode
== normal
;
1892 args
.dotrace
= mode
== trace
;
1893 _dl_receive_error (print_missing_version
, version_check_doit
, &args
);
1896 /* We do not initialize any of the TLS functionality unless any of the
1897 initial modules uses TLS. This makes dynamic loading of modules with
1898 TLS impossible, but to support it requires either eagerly doing setup
1899 now or lazily doing it later. Doing it now makes us incompatible with
1900 an old kernel that can't perform TLS_INIT_TP, even if no TLS is ever
1901 used. Trying to do it lazily is too hairy to try when there could be
1902 multiple threads (from a non-TLS-using libpthread). */
1903 bool was_tls_init_tp_called
= tls_init_tp_called
;
1907 if (__builtin_expect (audit_list
== NULL
, 1))
1908 /* Initialize security features. But only if we have not done it
1912 if (__builtin_expect (mode
, normal
) != normal
)
1914 /* We were run just to list the shared libraries. It is
1915 important that we do this before real relocation, because the
1916 functions we call below for output may no longer work properly
1917 after relocation. */
1920 if (GLRO(dl_debug_mask
) & DL_DEBUG_PRELINK
)
1922 struct r_scope_elem
*scope
= &main_map
->l_searchlist
;
1924 for (i
= 0; i
< scope
->r_nlist
; i
++)
1926 l
= scope
->r_list
[i
];
1929 _dl_printf ("\t%s => not found\n", l
->l_libname
->name
);
1932 if (_dl_name_match_p (GLRO(dl_trace_prelink
), l
))
1933 GLRO(dl_trace_prelink_map
) = l
;
1934 _dl_printf ("\t%s => %s (0x%0*Zx, 0x%0*Zx)",
1935 l
->l_libname
->name
[0] ? l
->l_libname
->name
1936 : rtld_progname
?: "<main program>",
1937 l
->l_name
[0] ? l
->l_name
1938 : rtld_progname
?: "<main program>",
1939 (int) sizeof l
->l_map_start
* 2,
1940 (size_t) l
->l_map_start
,
1941 (int) sizeof l
->l_addr
* 2,
1942 (size_t) l
->l_addr
);
1945 _dl_printf (" TLS(0x%Zx, 0x%0*Zx)\n", l
->l_tls_modid
,
1946 (int) sizeof l
->l_tls_offset
* 2,
1947 (size_t) l
->l_tls_offset
);
1952 else if (GLRO(dl_debug_mask
) & DL_DEBUG_UNUSED
)
1954 /* Look through the dependencies of the main executable
1955 and determine which of them is not actually
1957 struct link_map
*l
= main_map
;
1959 /* Relocate the main executable. */
1960 struct relocate_args args
= { .l
= l
,
1961 .reloc_mode
= ((GLRO(dl_lazy
)
1963 | __RTLD_NOIFUNC
) };
1964 _dl_receive_error (print_unresolved
, relocate_doit
, &args
);
1966 /* This loop depends on the dependencies of the executable to
1967 correspond in number and order to the DT_NEEDED entries. */
1968 ElfW(Dyn
) *dyn
= main_map
->l_ld
;
1970 while (dyn
->d_tag
!= DT_NULL
)
1972 if (dyn
->d_tag
== DT_NEEDED
)
1975 #if defined NEED_DL_SYSINFO || defined NEED_DL_SYSINFO_DSO
1976 /* Skip the VDSO since it's not part of the list
1977 of objects we brought in via DT_NEEDED entries. */
1978 if (l
== GLRO(dl_sysinfo_map
))
1985 _dl_printf ("Unused direct dependencies:\n");
1989 _dl_printf ("\t%s\n", l
->l_name
);
1996 _exit (first
!= true);
1998 else if (! main_map
->l_info
[DT_NEEDED
])
1999 _dl_printf ("\tstatically linked\n");
2002 for (l
= main_map
->l_next
; l
; l
= l
->l_next
)
2004 /* The library was not found. */
2005 _dl_printf ("\t%s => not found\n", l
->l_libname
->name
);
2006 else if (strcmp (l
->l_libname
->name
, l
->l_name
) == 0)
2007 _dl_printf ("\t%s (0x%0*Zx)\n", l
->l_libname
->name
,
2008 (int) sizeof l
->l_map_start
* 2,
2009 (size_t) l
->l_map_start
);
2011 _dl_printf ("\t%s => %s (0x%0*Zx)\n", l
->l_libname
->name
,
2012 l
->l_name
, (int) sizeof l
->l_map_start
* 2,
2013 (size_t) l
->l_map_start
);
2016 if (__builtin_expect (mode
, trace
) != trace
)
2017 for (i
= 1; i
< (unsigned int) _dl_argc
; ++i
)
2019 const ElfW(Sym
) *ref
= NULL
;
2020 ElfW(Addr
) loadbase
;
2023 result
= _dl_lookup_symbol_x (INTUSE(_dl_argv
)[i
], main_map
,
2024 &ref
, main_map
->l_scope
,
2025 NULL
, ELF_RTYPE_CLASS_PLT
,
2026 DL_LOOKUP_ADD_DEPENDENCY
, NULL
);
2028 loadbase
= LOOKUP_VALUE_ADDRESS (result
);
2030 _dl_printf ("%s found at 0x%0*Zd in object at 0x%0*Zd\n",
2031 INTUSE(_dl_argv
)[i
],
2032 (int) sizeof ref
->st_value
* 2,
2033 (size_t) ref
->st_value
,
2034 (int) sizeof loadbase
* 2, (size_t) loadbase
);
2038 /* If LD_WARN is set, warn about undefined symbols. */
2039 if (GLRO(dl_lazy
) >= 0 && GLRO(dl_verbose
))
2041 /* We have to do symbol dependency testing. */
2042 struct relocate_args args
;
2045 args
.reloc_mode
= ((GLRO(dl_lazy
) ? RTLD_LAZY
: 0)
2048 i
= main_map
->l_searchlist
.r_nlist
;
2051 struct link_map
*l
= main_map
->l_initfini
[i
];
2052 if (l
!= &GL(dl_rtld_map
) && ! l
->l_faked
)
2055 _dl_receive_error (print_unresolved
, relocate_doit
,
2060 if ((GLRO(dl_debug_mask
) & DL_DEBUG_PRELINK
)
2061 && rtld_multiple_ref
)
2063 /* Mark the link map as not yet relocated again. */
2064 GL(dl_rtld_map
).l_relocated
= 0;
2065 _dl_relocate_object (&GL(dl_rtld_map
),
2066 main_map
->l_scope
, __RTLD_NOIFUNC
, 0);
2069 #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
2072 /* Print more information. This means here, print information
2073 about the versions needed. */
2075 struct link_map
*map
;
2077 for (map
= main_map
; map
!= NULL
; map
= map
->l_next
)
2080 ElfW(Dyn
) *dyn
= map
->l_info
[VERNEEDTAG
];
2086 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
2087 ent
= (ElfW(Verneed
) *) (map
->l_addr
+ dyn
->d_un
.d_ptr
);
2091 _dl_printf ("\n\tVersion information:\n");
2095 _dl_printf ("\t%s:\n",
2096 map
->l_name
[0] ? map
->l_name
: rtld_progname
);
2101 struct link_map
*needed
;
2103 needed
= find_needed (strtab
+ ent
->vn_file
);
2104 aux
= (ElfW(Vernaux
) *) ((char *) ent
+ ent
->vn_aux
);
2108 const char *fname
= NULL
;
2111 && match_version (strtab
+ aux
->vna_name
,
2113 fname
= needed
->l_name
;
2115 _dl_printf ("\t\t%s (%s) %s=> %s\n",
2116 strtab
+ ent
->vn_file
,
2117 strtab
+ aux
->vna_name
,
2118 aux
->vna_flags
& VER_FLG_WEAK
2120 fname
?: "not found");
2122 if (aux
->vna_next
== 0)
2123 /* No more symbols. */
2127 aux
= (ElfW(Vernaux
) *) ((char *) aux
2131 if (ent
->vn_next
== 0)
2132 /* No more dependencies. */
2135 /* Next dependency. */
2136 ent
= (ElfW(Verneed
) *) ((char *) ent
+ ent
->vn_next
);
2145 if (main_map
->l_info
[ADDRIDX (DT_GNU_LIBLIST
)]
2146 && ! __builtin_expect (GLRO(dl_profile
) != NULL
, 0)
2147 && ! __builtin_expect (GLRO(dl_dynamic_weak
), 0))
2149 ElfW(Lib
) *liblist
, *liblistend
;
2150 struct link_map
**r_list
, **r_listend
, *l
;
2151 const char *strtab
= (const void *) D_PTR (main_map
, l_info
[DT_STRTAB
]);
2153 assert (main_map
->l_info
[VALIDX (DT_GNU_LIBLISTSZ
)] != NULL
);
2154 liblist
= (ElfW(Lib
) *)
2155 main_map
->l_info
[ADDRIDX (DT_GNU_LIBLIST
)]->d_un
.d_ptr
;
2156 liblistend
= (ElfW(Lib
) *)
2158 main_map
->l_info
[VALIDX (DT_GNU_LIBLISTSZ
)]->d_un
.d_val
);
2159 r_list
= main_map
->l_searchlist
.r_list
;
2160 r_listend
= r_list
+ main_map
->l_searchlist
.r_nlist
;
2162 for (; r_list
< r_listend
&& liblist
< liblistend
; r_list
++)
2169 /* If the library is not mapped where it should, fail. */
2173 /* Next, check if checksum matches. */
2174 if (l
->l_info
[VALIDX(DT_CHECKSUM
)] == NULL
2175 || l
->l_info
[VALIDX(DT_CHECKSUM
)]->d_un
.d_val
2176 != liblist
->l_checksum
)
2179 if (l
->l_info
[VALIDX(DT_GNU_PRELINKED
)] == NULL
2180 || l
->l_info
[VALIDX(DT_GNU_PRELINKED
)]->d_un
.d_val
2181 != liblist
->l_time_stamp
)
2184 if (! _dl_name_match_p (strtab
+ liblist
->l_name
, l
))
2191 if (r_list
== r_listend
&& liblist
== liblistend
)
2194 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_LIBS
, 0))
2195 _dl_debug_printf ("\nprelink checking: %s\n",
2196 prelinked
? "ok" : "failed");
2200 /* Now set up the variable which helps the assembler startup code. */
2201 GL(dl_ns
)[LM_ID_BASE
]._ns_main_searchlist
= &main_map
->l_searchlist
;
2203 /* Save the information about the original global scope list since
2204 we need it in the memory handling later. */
2205 GLRO(dl_initial_searchlist
) = *GL(dl_ns
)[LM_ID_BASE
]._ns_main_searchlist
;
2207 /* Remember the last search directory added at startup, now that
2208 malloc will no longer be the one from dl-minimal.c. */
2209 GLRO(dl_init_all_dirs
) = GL(dl_all_dirs
);
2211 /* Print scope information. */
2212 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_SCOPES
, 0))
2214 _dl_debug_printf ("\nInitial object scopes\n");
2216 for (struct link_map
*l
= main_map
; l
!= NULL
; l
= l
->l_next
)
2217 _dl_show_scope (l
, 0);
2222 if (main_map
->l_info
[ADDRIDX (DT_GNU_CONFLICT
)] != NULL
)
2224 ElfW(Rela
) *conflict
, *conflictend
;
2225 #ifndef HP_TIMING_NONAVAIL
2230 HP_TIMING_NOW (start
);
2231 assert (main_map
->l_info
[VALIDX (DT_GNU_CONFLICTSZ
)] != NULL
);
2232 conflict
= (ElfW(Rela
) *)
2233 main_map
->l_info
[ADDRIDX (DT_GNU_CONFLICT
)]->d_un
.d_ptr
;
2234 conflictend
= (ElfW(Rela
) *)
2236 + main_map
->l_info
[VALIDX (DT_GNU_CONFLICTSZ
)]->d_un
.d_val
);
2237 _dl_resolve_conflicts (main_map
, conflict
, conflictend
);
2238 HP_TIMING_NOW (stop
);
2239 HP_TIMING_DIFF (relocate_time
, start
, stop
);
2243 /* Mark all the objects so we know they have been already relocated. */
2244 for (struct link_map
*l
= main_map
; l
!= NULL
; l
= l
->l_next
)
2247 if (l
->l_relro_size
)
2248 _dl_protect_relro (l
);
2250 /* Add object to slot information data if necessasy. */
2251 if (l
->l_tls_blocksize
!= 0 && tls_init_tp_called
)
2252 _dl_add_to_slotinfo (l
);
2257 /* Now we have all the objects loaded. Relocate them all except for
2258 the dynamic linker itself. We do this in reverse order so that copy
2259 relocs of earlier objects overwrite the data written by later
2260 objects. We do not re-relocate the dynamic linker itself in this
2261 loop because that could result in the GOT entries for functions we
2262 call being changed, and that would break us. It is safe to relocate
2263 the dynamic linker out of order because it has no copy relocs (we
2264 know that because it is self-contained). */
2266 int consider_profiling
= GLRO(dl_profile
) != NULL
;
2267 #ifndef HP_TIMING_NONAVAIL
2272 /* If we are profiling we also must do lazy reloaction. */
2273 GLRO(dl_lazy
) |= consider_profiling
;
2275 HP_TIMING_NOW (start
);
2276 unsigned i
= main_map
->l_searchlist
.r_nlist
;
2279 struct link_map
*l
= main_map
->l_initfini
[i
];
2281 /* While we are at it, help the memory handling a bit. We have to
2282 mark some data structures as allocated with the fake malloc()
2283 implementation in ld.so. */
2284 struct libname_list
*lnp
= l
->l_libname
->next
;
2286 while (__builtin_expect (lnp
!= NULL
, 0))
2291 /* Also allocated with the fake malloc(). */
2292 l
->l_free_initfini
= 0;
2294 if (l
!= &GL(dl_rtld_map
))
2295 _dl_relocate_object (l
, l
->l_scope
, GLRO(dl_lazy
) ? RTLD_LAZY
: 0,
2296 consider_profiling
);
2298 /* Add object to slot information data if necessasy. */
2299 if (l
->l_tls_blocksize
!= 0 && tls_init_tp_called
)
2300 _dl_add_to_slotinfo (l
);
2302 HP_TIMING_NOW (stop
);
2304 HP_TIMING_DIFF (relocate_time
, start
, stop
);
2306 /* Now enable profiling if needed. Like the previous call,
2307 this has to go here because the calls it makes should use the
2308 rtld versions of the functions (particularly calloc()), but it
2309 needs to have _dl_profile_map set up by the relocator. */
2310 if (__builtin_expect (GL(dl_profile_map
) != NULL
, 0))
2311 /* We must prepare the profiling. */
2312 _dl_start_profile ();
2315 #ifndef NONTLS_INIT_TP
2316 # define NONTLS_INIT_TP do { } while (0)
2319 if (!was_tls_init_tp_called
&& GL(dl_tls_max_dtv_idx
) > 0)
2320 ++GL(dl_tls_generation
);
2322 /* Now that we have completed relocation, the initializer data
2323 for the TLS blocks has its final values and we can copy them
2324 into the main thread's TLS area, which we allocated above. */
2325 _dl_allocate_tls_init (tcbp
);
2327 /* And finally install it for the main thread. If ld.so itself uses
2328 TLS we know the thread pointer was initialized earlier. */
2329 if (! tls_init_tp_called
)
2333 = TLS_INIT_TP (tcbp
, USE___THREAD
);
2335 = TLS_INIT_TP (tcbp
, 0);
2337 if (__builtin_expect (lossage
!= NULL
, 0))
2338 _dl_fatal_printf ("cannot set up thread-local storage: %s\n",
2342 /* Make sure no new search directories have been added. */
2343 assert (GLRO(dl_init_all_dirs
) == GL(dl_all_dirs
));
2345 if (! prelinked
&& rtld_multiple_ref
)
2347 /* There was an explicit ref to the dynamic linker as a shared lib.
2348 Re-relocate ourselves with user-controlled symbol definitions.
2350 We must do this after TLS initialization in case after this
2351 re-relocation, we might call a user-supplied function
2352 (e.g. calloc from _dl_relocate_object) that uses TLS data. */
2354 #ifndef HP_TIMING_NONAVAIL
2360 HP_TIMING_NOW (start
);
2361 /* Mark the link map as not yet relocated again. */
2362 GL(dl_rtld_map
).l_relocated
= 0;
2363 _dl_relocate_object (&GL(dl_rtld_map
), main_map
->l_scope
, 0, 0);
2364 HP_TIMING_NOW (stop
);
2365 HP_TIMING_DIFF (add
, start
, stop
);
2366 HP_TIMING_ACCUM_NT (relocate_time
, add
);
2369 /* Do any necessary cleanups for the startup OS interface code.
2370 We do these now so that no calls are made after rtld re-relocation
2371 which might be resolved to different functions than we expect.
2372 We cannot do this before relocating the other objects because
2373 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
2374 _dl_sysdep_start_cleanup ();
2377 /* Auditing checkpoint: we have added all objects. */
2378 if (__builtin_expect (GLRO(dl_naudit
) > 0, 0))
2380 struct link_map
*head
= GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
;
2381 /* Do not call the functions for any auditing object. */
2382 if (head
->l_auditing
== 0)
2384 struct audit_ifaces
*afct
= GLRO(dl_audit
);
2385 for (unsigned int cnt
= 0; cnt
< GLRO(dl_naudit
); ++cnt
)
2387 if (afct
->activity
!= NULL
)
2388 afct
->activity (&head
->l_audit
[cnt
].cookie
, LA_ACT_CONSISTENT
);
2396 /* Notify the debugger all new objects are now ready to go. We must re-get
2397 the address since by now the variable might be in another object. */
2398 r
= _dl_debug_initialize (0, LM_ID_BASE
);
2399 r
->r_state
= RT_CONSISTENT
;
2401 LIBC_PROBE (init_complete
, 2, LM_ID_BASE
, r
);
2404 /* We must munmap() the cache file. */
2405 _dl_unload_cache ();
2408 /* Once we return, _dl_sysdep_start will invoke
2409 the DT_INIT functions and then *USER_ENTRY. */
2412 /* This is a little helper function for resolving symbols while
2413 tracing the binary. */
2415 print_unresolved (int errcode
__attribute__ ((unused
)), const char *objname
,
2416 const char *errstring
)
2418 if (objname
[0] == '\0')
2419 objname
= rtld_progname
?: "<main program>";
2420 _dl_error_printf ("%s (%s)\n", errstring
, objname
);
2423 /* This is a little helper function for resolving symbols while
2424 tracing the binary. */
2426 print_missing_version (int errcode
__attribute__ ((unused
)),
2427 const char *objname
, const char *errstring
)
2429 _dl_error_printf ("%s: %s: %s\n", rtld_progname
?: "<program name unknown>",
2430 objname
, errstring
);
2433 /* Nonzero if any of the debugging options is enabled. */
2434 static int any_debug attribute_relro
;
2436 /* Process the string given as the parameter which explains which debugging
2437 options are enabled. */
2439 process_dl_debug (const char *dl_debug
)
2441 /* When adding new entries make sure that the maximal length of a name
2442 is correctly handled in the LD_DEBUG_HELP code below. */
2446 const char name
[10];
2447 const char helptext
[41];
2448 unsigned short int mask
;
2451 #define LEN_AND_STR(str) sizeof (str) - 1, str
2452 { LEN_AND_STR ("libs"), "display library search paths",
2453 DL_DEBUG_LIBS
| DL_DEBUG_IMPCALLS
},
2454 { LEN_AND_STR ("reloc"), "display relocation processing",
2455 DL_DEBUG_RELOC
| DL_DEBUG_IMPCALLS
},
2456 { LEN_AND_STR ("files"), "display progress for input file",
2457 DL_DEBUG_FILES
| DL_DEBUG_IMPCALLS
},
2458 { LEN_AND_STR ("symbols"), "display symbol table processing",
2459 DL_DEBUG_SYMBOLS
| DL_DEBUG_IMPCALLS
},
2460 { LEN_AND_STR ("bindings"), "display information about symbol binding",
2461 DL_DEBUG_BINDINGS
| DL_DEBUG_IMPCALLS
},
2462 { LEN_AND_STR ("versions"), "display version dependencies",
2463 DL_DEBUG_VERSIONS
| DL_DEBUG_IMPCALLS
},
2464 { LEN_AND_STR ("scopes"), "display scope information",
2466 { LEN_AND_STR ("all"), "all previous options combined",
2467 DL_DEBUG_LIBS
| DL_DEBUG_RELOC
| DL_DEBUG_FILES
| DL_DEBUG_SYMBOLS
2468 | DL_DEBUG_BINDINGS
| DL_DEBUG_VERSIONS
| DL_DEBUG_IMPCALLS
2469 | DL_DEBUG_SCOPES
},
2470 { LEN_AND_STR ("statistics"), "display relocation statistics",
2471 DL_DEBUG_STATISTICS
},
2472 { LEN_AND_STR ("unused"), "determined unused DSOs",
2474 { LEN_AND_STR ("help"), "display this help message and exit",
2477 #define ndebopts (sizeof (debopts) / sizeof (debopts[0]))
2479 /* Skip separating white spaces and commas. */
2480 while (*dl_debug
!= '\0')
2482 if (*dl_debug
!= ' ' && *dl_debug
!= ',' && *dl_debug
!= ':')
2487 while (dl_debug
[len
] != '\0' && dl_debug
[len
] != ' '
2488 && dl_debug
[len
] != ',' && dl_debug
[len
] != ':')
2491 for (cnt
= 0; cnt
< ndebopts
; ++cnt
)
2492 if (debopts
[cnt
].len
== len
2493 && memcmp (dl_debug
, debopts
[cnt
].name
, len
) == 0)
2495 GLRO(dl_debug_mask
) |= debopts
[cnt
].mask
;
2500 if (cnt
== ndebopts
)
2502 /* Display a warning and skip everything until next
2504 char *copy
= strndupa (dl_debug
, len
);
2505 _dl_error_printf ("\
2506 warning: debug option `%s' unknown; try LD_DEBUG=help\n", copy
);
2516 if (GLRO(dl_debug_mask
) & DL_DEBUG_UNUSED
)
2518 /* In order to get an accurate picture of whether a particular
2519 DT_NEEDED entry is actually used we have to process both
2520 the PLT and non-PLT relocation entries. */
2524 if (GLRO(dl_debug_mask
) & DL_DEBUG_HELP
)
2529 Valid options for the LD_DEBUG environment variable are:\n\n");
2531 for (cnt
= 0; cnt
< ndebopts
; ++cnt
)
2532 _dl_printf (" %.*s%s%s\n", debopts
[cnt
].len
, debopts
[cnt
].name
,
2533 " " + debopts
[cnt
].len
- 3,
2534 debopts
[cnt
].helptext
);
2537 To direct the debugging output into a file instead of standard output\n\
2538 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n");
2544 process_dl_audit (char *str
)
2546 /* The parameter is a colon separated list of DSO names. */
2549 while ((p
= (strsep
) (&str
, ":")) != NULL
)
2551 && (__builtin_expect (! INTUSE(__libc_enable_secure
), 1)
2552 || strchr (p
, '/') == NULL
))
2554 /* This is using the local malloc, not the system malloc. The
2555 memory can never be freed. */
2556 struct audit_list
*newp
= malloc (sizeof (*newp
));
2559 if (audit_list
== NULL
)
2560 audit_list
= newp
->next
= newp
;
2563 newp
->next
= audit_list
->next
;
2564 audit_list
= audit_list
->next
= newp
;
2569 /* Process all environments variables the dynamic linker must recognize.
2570 Since all of them start with `LD_' we are a bit smarter while finding
2572 extern char **_environ attribute_hidden
;
2576 process_envvars (enum mode
*modep
)
2578 char **runp
= _environ
;
2580 enum mode mode
= normal
;
2581 char *debug_output
= NULL
;
2583 /* This is the default place for profiling data file. */
2584 GLRO(dl_profile_output
)
2585 = &"/var/tmp\0/var/profile"[INTUSE(__libc_enable_secure
) ? 9 : 0];
2587 while ((envline
= _dl_next_ld_env_entry (&runp
)) != NULL
)
2591 while (envline
[len
] != '\0' && envline
[len
] != '=')
2594 if (envline
[len
] != '=')
2595 /* This is a "LD_" variable at the end of the string without
2596 a '=' character. Ignore it since otherwise we will access
2597 invalid memory below. */
2603 /* Warning level, verbose or not. */
2604 if (memcmp (envline
, "WARN", 4) == 0)
2605 GLRO(dl_verbose
) = envline
[5] != '\0';
2609 /* Debugging of the dynamic linker? */
2610 if (memcmp (envline
, "DEBUG", 5) == 0)
2612 process_dl_debug (&envline
[6]);
2615 if (memcmp (envline
, "AUDIT", 5) == 0)
2616 process_dl_audit (&envline
[6]);
2620 /* Print information about versions. */
2621 if (memcmp (envline
, "VERBOSE", 7) == 0)
2623 version_info
= envline
[8] != '\0';
2627 /* List of objects to be preloaded. */
2628 if (memcmp (envline
, "PRELOAD", 7) == 0)
2630 preloadlist
= &envline
[8];
2634 /* Which shared object shall be profiled. */
2635 if (memcmp (envline
, "PROFILE", 7) == 0 && envline
[8] != '\0')
2636 GLRO(dl_profile
) = &envline
[8];
2640 /* Do we bind early? */
2641 if (memcmp (envline
, "BIND_NOW", 8) == 0)
2643 GLRO(dl_lazy
) = envline
[9] == '\0';
2646 if (memcmp (envline
, "BIND_NOT", 8) == 0)
2647 GLRO(dl_bind_not
) = envline
[9] != '\0';
2651 /* Test whether we want to see the content of the auxiliary
2652 array passed up from the kernel. */
2653 if (!INTUSE(__libc_enable_secure
)
2654 && memcmp (envline
, "SHOW_AUXV", 9) == 0)
2659 /* Mask for the important hardware capabilities. */
2660 if (memcmp (envline
, "HWCAP_MASK", 10) == 0)
2661 GLRO(dl_hwcap_mask
) = __strtoul_internal (&envline
[11], NULL
,
2666 /* Path where the binary is found. */
2667 if (!INTUSE(__libc_enable_secure
)
2668 && memcmp (envline
, "ORIGIN_PATH", 11) == 0)
2669 GLRO(dl_origin_path
) = &envline
[12];
2673 /* The library search path. */
2674 if (memcmp (envline
, "LIBRARY_PATH", 12) == 0)
2676 library_path
= &envline
[13];
2680 /* Where to place the profiling data file. */
2681 if (memcmp (envline
, "DEBUG_OUTPUT", 12) == 0)
2683 debug_output
= &envline
[13];
2687 if (!INTUSE(__libc_enable_secure
)
2688 && memcmp (envline
, "DYNAMIC_WEAK", 12) == 0)
2689 GLRO(dl_dynamic_weak
) = 1;
2693 /* We might have some extra environment variable with length 13
2695 #ifdef EXTRA_LD_ENVVARS_13
2698 if (!INTUSE(__libc_enable_secure
)
2699 && memcmp (envline
, "USE_LOAD_BIAS", 13) == 0)
2701 GLRO(dl_use_load_bias
) = envline
[14] == '1' ? -1 : 0;
2705 if (memcmp (envline
, "POINTER_GUARD", 13) == 0)
2706 GLRO(dl_pointer_guard
) = envline
[14] != '0';
2710 /* Where to place the profiling data file. */
2711 if (!INTUSE(__libc_enable_secure
)
2712 && memcmp (envline
, "PROFILE_OUTPUT", 14) == 0
2713 && envline
[15] != '\0')
2714 GLRO(dl_profile_output
) = &envline
[15];
2718 /* The mode of the dynamic linker can be set. */
2719 if (memcmp (envline
, "TRACE_PRELINKING", 16) == 0)
2722 GLRO(dl_verbose
) = 1;
2723 GLRO(dl_debug_mask
) |= DL_DEBUG_PRELINK
;
2724 GLRO(dl_trace_prelink
) = &envline
[17];
2729 /* The mode of the dynamic linker can be set. */
2730 if (memcmp (envline
, "TRACE_LOADED_OBJECTS", 20) == 0)
2734 /* We might have some extra environment variable to handle. This
2735 is tricky due to the pre-processing of the length of the name
2736 in the switch statement here. The code here assumes that added
2737 environment variables have a different length. */
2738 #ifdef EXTRA_LD_ENVVARS
2744 /* The caller wants this information. */
2747 /* Extra security for SUID binaries. Remove all dangerous environment
2749 if (__builtin_expect (INTUSE(__libc_enable_secure
), 0))
2751 static const char unsecure_envvars
[] =
2752 #ifdef EXTRA_UNSECURE_ENVVARS
2753 EXTRA_UNSECURE_ENVVARS
2758 nextp
= unsecure_envvars
;
2762 /* We could use rawmemchr but this need not be fast. */
2763 nextp
= (char *) (strchr
) (nextp
, '\0') + 1;
2765 while (*nextp
!= '\0');
2767 if (__access ("/etc/suid-debug", F_OK
) != 0)
2769 unsetenv ("MALLOC_CHECK_");
2770 GLRO(dl_debug_mask
) = 0;
2776 /* If we have to run the dynamic linker in debugging mode and the
2777 LD_DEBUG_OUTPUT environment variable is given, we write the debug
2778 messages to this file. */
2779 else if (any_debug
&& debug_output
!= NULL
)
2782 const int flags
= O_WRONLY
| O_APPEND
| O_CREAT
| O_NOFOLLOW
;
2784 const int flags
= O_WRONLY
| O_APPEND
| O_CREAT
;
2786 size_t name_len
= strlen (debug_output
);
2787 char buf
[name_len
+ 12];
2790 buf
[name_len
+ 11] = '\0';
2791 startp
= _itoa (__getpid (), &buf
[name_len
+ 11], 10, 0);
2793 startp
= memcpy (startp
- name_len
, debug_output
, name_len
);
2795 GLRO(dl_debug_fd
) = __open (startp
, flags
, DEFFILEMODE
);
2796 if (GLRO(dl_debug_fd
) == -1)
2797 /* We use standard output if opening the file failed. */
2798 GLRO(dl_debug_fd
) = STDOUT_FILENO
;
2803 /* Print the various times we collected. */
2805 __attribute ((noinline
))
2806 print_statistics (hp_timing_t
*rtld_total_timep
)
2808 #ifndef HP_TIMING_NONAVAIL
2813 /* Total time rtld used. */
2814 if (HP_TIMING_AVAIL
)
2816 HP_TIMING_PRINT (buf
, sizeof (buf
), *rtld_total_timep
);
2817 _dl_debug_printf ("\nruntime linker statistics:\n"
2818 " total startup time in dynamic loader: %s\n", buf
);
2820 /* Print relocation statistics. */
2822 HP_TIMING_PRINT (buf
, sizeof (buf
), relocate_time
);
2823 cp
= _itoa ((1000ULL * relocate_time
) / *rtld_total_timep
,
2824 pbuf
+ sizeof (pbuf
), 10, 0);
2826 switch (pbuf
+ sizeof (pbuf
) - cp
)
2837 _dl_debug_printf ("\
2838 time needed for relocation: %s (%s%%)\n", buf
, pbuf
);
2842 unsigned long int num_relative_relocations
= 0;
2843 for (Lmid_t ns
= 0; ns
< GL(dl_nns
); ++ns
)
2845 if (GL(dl_ns
)[ns
]._ns_loaded
== NULL
)
2848 struct r_scope_elem
*scope
= &GL(dl_ns
)[ns
]._ns_loaded
->l_searchlist
;
2850 for (unsigned int i
= 0; i
< scope
->r_nlist
; i
++)
2852 struct link_map
*l
= scope
->r_list
[i
];
2854 if (l
->l_addr
!= 0 && l
->l_info
[VERSYMIDX (DT_RELCOUNT
)])
2855 num_relative_relocations
2856 += l
->l_info
[VERSYMIDX (DT_RELCOUNT
)]->d_un
.d_val
;
2857 #ifndef ELF_MACHINE_REL_RELATIVE
2858 /* Relative relocations are processed on these architectures if
2859 library is loaded to different address than p_vaddr or
2860 if not prelinked. */
2861 if ((l
->l_addr
!= 0 || !l
->l_info
[VALIDX(DT_GNU_PRELINKED
)])
2862 && l
->l_info
[VERSYMIDX (DT_RELACOUNT
)])
2864 /* On e.g. IA-64 or Alpha, relative relocations are processed
2865 only if library is loaded to different address than p_vaddr. */
2866 if (l
->l_addr
!= 0 && l
->l_info
[VERSYMIDX (DT_RELACOUNT
)])
2868 num_relative_relocations
2869 += l
->l_info
[VERSYMIDX (DT_RELACOUNT
)]->d_un
.d_val
;
2873 _dl_debug_printf (" number of relocations: %lu\n"
2874 " number of relocations from cache: %lu\n"
2875 " number of relative relocations: %lu\n",
2876 GL(dl_num_relocations
),
2877 GL(dl_num_cache_relocations
),
2878 num_relative_relocations
);
2880 #ifndef HP_TIMING_NONAVAIL
2881 /* Time spend while loading the object and the dependencies. */
2882 if (HP_TIMING_AVAIL
)
2885 HP_TIMING_PRINT (buf
, sizeof (buf
), load_time
);
2886 cp
= _itoa ((1000ULL * load_time
) / *rtld_total_timep
,
2887 pbuf
+ sizeof (pbuf
), 10, 0);
2889 switch (pbuf
+ sizeof (pbuf
) - cp
)
2900 _dl_debug_printf ("\
2901 time needed to load objects: %s (%s%%)\n",