* csu/elf-init.c (__libc_csu_fini): Enable if LIBC_NONSHARED
[glibc.git] / elf / rtld.c
blobfbb4e4c3f2f90d225e016193585d60f61981dcb0
1 /* Run time dynamic linker.
2 Copyright (C) 1995-2002, 2003, 2004, 2005 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <dlfcn.h>
22 #include <fcntl.h>
23 #include <stdbool.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/mman.h> /* Check if MAP_ANON is defined. */
28 #include <sys/param.h>
29 #include <sys/stat.h>
30 #include <ldsodefs.h>
31 #include <stdio-common/_itoa.h>
32 #include <entry.h>
33 #include <fpu_control.h>
34 #include <hp-timing.h>
35 #include <bits/libc-lock.h>
36 #include "dynamic-link.h"
37 #include "dl-librecon.h"
38 #include <unsecvars.h>
39 #include <dl-cache.h>
40 #include <dl-procinfo.h>
41 #include <tls.h>
43 #include <assert.h>
45 /* Avoid PLT use for our local calls at startup. */
46 extern __typeof (__mempcpy) __mempcpy attribute_hidden;
48 /* GCC has mental blocks about _exit. */
49 extern __typeof (_exit) exit_internal asm ("_exit") attribute_hidden;
50 #define _exit exit_internal
52 /* Helper function to handle errors while resolving symbols. */
53 static void print_unresolved (int errcode, const char *objname,
54 const char *errsting);
56 /* Helper function to handle errors when a version is missing. */
57 static void print_missing_version (int errcode, const char *objname,
58 const char *errsting);
60 /* Print the various times we collected. */
61 static void print_statistics (hp_timing_t *total_timep);
63 /* This is a list of all the modes the dynamic loader can be in. */
64 enum mode { normal, list, verify, trace };
66 /* Process all environments variables the dynamic linker must recognize.
67 Since all of them start with `LD_' we are a bit smarter while finding
68 all the entries. */
69 static void process_envvars (enum mode *modep);
71 int _dl_argc attribute_relro attribute_hidden;
72 #ifdef DL_ARGV_NOT_RELRO
73 char **_dl_argv = NULL;
74 #else
75 char **_dl_argv attribute_relro = NULL;
76 #endif
77 INTDEF(_dl_argv)
79 /* Nonzero if we were run directly. */
80 unsigned int _dl_skip_args attribute_relro attribute_hidden;
82 /* List of auditing DSOs. */
83 static struct audit_list
85 const char *name;
86 struct audit_list *next;
87 } *audit_list;
89 #ifndef HAVE_INLINED_SYSCALLS
90 /* Set nonzero during loading and initialization of executable and
91 libraries, cleared before the executable's entry point runs. This
92 must not be initialized to nonzero, because the unused dynamic
93 linker loaded in for libc.so's "ld.so.1" dep will provide the
94 definition seen by libc.so's initializer; that value must be zero,
95 and will be since that dynamic linker's _dl_start and dl_main will
96 never be called. */
97 int _dl_starting_up = 0;
98 INTVARDEF(_dl_starting_up)
99 #endif
101 /* This is the structure which defines all variables global to ld.so
102 (except those which cannot be added for some reason). */
103 struct rtld_global _rtld_global =
105 /* Default presumption without further information is executable stack. */
106 ._dl_stack_flags = PF_R|PF_W|PF_X,
107 #ifdef _LIBC_REENTRANT
108 ._dl_load_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER
109 #endif
111 /* If we would use strong_alias here the compiler would see a
112 non-hidden definition. This would undo the effect of the previous
113 declaration. So spell out was strong_alias does plus add the
114 visibility attribute. */
115 extern struct rtld_global _rtld_local
116 __attribute__ ((alias ("_rtld_global"), visibility ("hidden")));
119 /* This variable is similar to _rtld_local, but all values are
120 read-only after relocation. */
121 struct rtld_global_ro _rtld_global_ro attribute_relro =
123 /* Get architecture specific initializer. */
124 #include <dl-procinfo.c>
125 #ifdef NEED_DL_SYSINFO
126 ._dl_sysinfo = DL_SYSINFO_DEFAULT,
127 #endif
128 ._dl_debug_fd = STDERR_FILENO,
129 ._dl_use_load_bias = -2,
130 ._dl_correct_cache_id = _DL_CACHE_DEFAULT_ID,
131 ._dl_hwcap_mask = HWCAP_IMPORTANT,
132 ._dl_lazy = 1,
133 ._dl_fpu_control = _FPU_DEFAULT,
135 /* Function pointers. */
136 ._dl_debug_printf = _dl_debug_printf,
137 ._dl_catch_error = _dl_catch_error,
138 ._dl_signal_error = _dl_signal_error,
139 ._dl_mcount = _dl_mcount_internal,
140 ._dl_lookup_symbol_x = _dl_lookup_symbol_x,
141 ._dl_check_caller = _dl_check_caller,
142 ._dl_open = _dl_open,
143 ._dl_close = _dl_close
145 /* If we would use strong_alias here the compiler would see a
146 non-hidden definition. This would undo the effect of the previous
147 declaration. So spell out was strong_alias does plus add the
148 visibility attribute. */
149 extern struct rtld_global_ro _rtld_local_ro
150 __attribute__ ((alias ("_rtld_global_ro"), visibility ("hidden")));
153 static void dl_main (const ElfW(Phdr) *phdr, ElfW(Word) phnum,
154 ElfW(Addr) *user_entry);
156 /* These two variables cannot be moved into .data.rel.ro. */
157 static struct libname_list _dl_rtld_libname;
158 static struct libname_list _dl_rtld_libname2;
160 /* We expect less than a second for relocation. */
161 #ifdef HP_SMALL_TIMING_AVAIL
162 # undef HP_TIMING_AVAIL
163 # define HP_TIMING_AVAIL HP_SMALL_TIMING_AVAIL
164 #endif
166 /* Variable for statistics. */
167 #ifndef HP_TIMING_NONAVAIL
168 static hp_timing_t relocate_time;
169 static hp_timing_t load_time attribute_relro;
170 static hp_timing_t start_time attribute_relro;
171 #endif
173 /* Additional definitions needed by TLS initialization. */
174 #ifdef TLS_INIT_HELPER
175 TLS_INIT_HELPER
176 #endif
178 /* Helper function for syscall implementation. */
179 #ifdef DL_SYSINFO_IMPLEMENTATION
180 DL_SYSINFO_IMPLEMENTATION
181 #endif
183 /* Before ld.so is relocated we must not access variables which need
184 relocations. This means variables which are exported. Variables
185 declared as static are fine. If we can mark a variable hidden this
186 is fine, too. The latter is important here. We can avoid setting
187 up a temporary link map for ld.so if we can mark _rtld_global as
188 hidden. */
189 #if defined PI_STATIC_AND_HIDDEN && defined HAVE_HIDDEN \
190 && defined HAVE_VISIBILITY_ATTRIBUTE
191 # define DONT_USE_BOOTSTRAP_MAP 1
192 #endif
194 #ifdef DONT_USE_BOOTSTRAP_MAP
195 static ElfW(Addr) _dl_start_final (void *arg);
196 #else
197 struct dl_start_final_info
199 struct link_map l;
200 #if !defined HP_TIMING_NONAVAIL && HP_TIMING_INLINE
201 hp_timing_t start_time;
202 #endif
204 static ElfW(Addr) _dl_start_final (void *arg,
205 struct dl_start_final_info *info);
206 #endif
208 /* These defined magically in the linker script. */
209 extern char _begin[] attribute_hidden;
210 extern char _etext[] attribute_hidden;
211 extern char _end[] attribute_hidden;
214 #ifdef RTLD_START
215 RTLD_START
216 #else
217 # error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
218 #endif
220 #ifndef VALIDX
221 # define VALIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
222 + DT_EXTRANUM + DT_VALTAGIDX (tag))
223 #endif
224 #ifndef ADDRIDX
225 # define ADDRIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
226 + DT_EXTRANUM + DT_VALNUM + DT_ADDRTAGIDX (tag))
227 #endif
229 /* This is the second half of _dl_start (below). It can be inlined safely
230 under DONT_USE_BOOTSTRAP_MAP, where it is careful not to make any GOT
231 references. When the tools don't permit us to avoid using a GOT entry
232 for _dl_rtld_global (no attribute_hidden support), we must make sure
233 this function is not inlined (see below). */
235 #ifdef DONT_USE_BOOTSTRAP_MAP
236 static inline ElfW(Addr) __attribute__ ((always_inline))
237 _dl_start_final (void *arg)
238 #else
239 static ElfW(Addr) __attribute__ ((noinline))
240 _dl_start_final (void *arg, struct dl_start_final_info *info)
241 #endif
243 ElfW(Addr) start_addr;
245 if (HP_TIMING_AVAIL)
247 /* If it hasn't happen yet record the startup time. */
248 if (! HP_TIMING_INLINE)
249 HP_TIMING_NOW (start_time);
250 #if !defined DONT_USE_BOOTSTRAP_MAP && !defined HP_TIMING_NONAVAIL
251 else
252 start_time = info->start_time;
253 #endif
255 /* Initialize the timing functions. */
256 HP_TIMING_DIFF_INIT ();
259 /* Transfer data about ourselves to the permanent link_map structure. */
260 #ifndef DONT_USE_BOOTSTRAP_MAP
261 GL(dl_rtld_map).l_addr = info->l.l_addr;
262 GL(dl_rtld_map).l_ld = info->l.l_ld;
263 memcpy (GL(dl_rtld_map).l_info, info->l.l_info,
264 sizeof GL(dl_rtld_map).l_info);
265 GL(dl_rtld_map).l_mach = info->l.l_mach;
266 GL(dl_rtld_map).l_relocated = 1;
267 #endif
268 _dl_setup_hash (&GL(dl_rtld_map));
269 GL(dl_rtld_map).l_real = &GL(dl_rtld_map);
270 GL(dl_rtld_map).l_opencount = 1;
271 GL(dl_rtld_map).l_map_start = (ElfW(Addr)) _begin;
272 GL(dl_rtld_map).l_map_end = (ElfW(Addr)) _end;
273 GL(dl_rtld_map).l_text_end = (ElfW(Addr)) _etext;
274 /* Copy the TLS related data if necessary. */
275 #if USE_TLS && !defined DONT_USE_BOOTSTRAP_MAP
276 # if USE___THREAD
277 assert (info->l.l_tls_modid != 0);
278 GL(dl_rtld_map).l_tls_blocksize = info->l.l_tls_blocksize;
279 GL(dl_rtld_map).l_tls_align = info->l.l_tls_align;
280 GL(dl_rtld_map).l_tls_firstbyte_offset = info->l.l_tls_firstbyte_offset;
281 GL(dl_rtld_map).l_tls_initimage_size = info->l.l_tls_initimage_size;
282 GL(dl_rtld_map).l_tls_initimage = info->l.l_tls_initimage;
283 GL(dl_rtld_map).l_tls_offset = info->l.l_tls_offset;
284 GL(dl_rtld_map).l_tls_modid = 1;
285 # else
286 assert (info->l.l_tls_modid == 0);
287 # if NO_TLS_OFFSET != 0
288 GL(dl_rtld_map).l_tls_offset = NO_TLS_OFFSET;
289 # endif
290 # endif
292 #endif
294 #if HP_TIMING_AVAIL
295 HP_TIMING_NOW (GL(dl_cpuclock_offset));
296 #endif
298 /* Initialize the stack end variable. */
299 __libc_stack_end = __builtin_frame_address (0);
301 /* Call the OS-dependent function to set up life so we can do things like
302 file access. It will call `dl_main' (below) to do all the real work
303 of the dynamic linker, and then unwind our frame and run the user
304 entry point on the same stack we entered on. */
305 start_addr = _dl_sysdep_start (arg, &dl_main);
307 #ifndef HP_TIMING_NONAVAIL
308 hp_timing_t rtld_total_time;
309 if (HP_TIMING_AVAIL)
311 hp_timing_t end_time;
313 /* Get the current time. */
314 HP_TIMING_NOW (end_time);
316 /* Compute the difference. */
317 HP_TIMING_DIFF (rtld_total_time, start_time, end_time);
319 #endif
321 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_STATISTICS, 0))
323 #ifndef HP_TIMING_NONAVAIL
324 print_statistics (&rtld_total_time);
325 #else
326 print_statistics (NULL);
327 #endif
330 return start_addr;
333 static ElfW(Addr) __attribute_used__ internal_function
334 _dl_start (void *arg)
336 #ifdef DONT_USE_BOOTSTRAP_MAP
337 # define bootstrap_map GL(dl_rtld_map)
338 #else
339 struct dl_start_final_info info;
340 # define bootstrap_map info.l
341 #endif
343 /* This #define produces dynamic linking inline functions for
344 bootstrap relocation instead of general-purpose relocation. */
345 #define RTLD_BOOTSTRAP
346 #define RESOLVE_MAP(sym, version, flags) \
347 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : &bootstrap_map)
348 #include "dynamic-link.h"
350 if (HP_TIMING_INLINE && HP_TIMING_AVAIL)
351 #ifdef DONT_USE_BOOTSTRAP_MAP
352 HP_TIMING_NOW (start_time);
353 #else
354 HP_TIMING_NOW (info.start_time);
355 #endif
357 /* Partly clean the `bootstrap_map' structure up. Don't use
358 `memset' since it might not be built in or inlined and we cannot
359 make function calls at this point. Use '__builtin_memset' if we
360 know it is available. We do not have to clear the memory if we
361 do not have to use the temporary bootstrap_map. Global variables
362 are initialized to zero by default. */
363 #ifndef DONT_USE_BOOTSTRAP_MAP
364 # ifdef HAVE_BUILTIN_MEMSET
365 __builtin_memset (bootstrap_map.l_info, '\0', sizeof (bootstrap_map.l_info));
366 # else
367 for (size_t cnt = 0;
368 cnt < sizeof (bootstrap_map.l_info) / sizeof (bootstrap_map.l_info[0]);
369 ++cnt)
370 bootstrap_map.l_info[cnt] = 0;
371 # endif
372 #endif
374 /* Figure out the run-time load address of the dynamic linker itself. */
375 bootstrap_map.l_addr = elf_machine_load_address ();
377 /* Read our own dynamic section and fill in the info array. */
378 bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
379 elf_get_dynamic_info (&bootstrap_map, NULL);
381 #if defined USE_TLS && NO_TLS_OFFSET != 0
382 bootstrap_map.l_tls_offset = NO_TLS_OFFSET;
383 #endif
385 /* Get the dynamic linker's own program header. First we need the ELF
386 file header. The `_begin' symbol created by the linker script points
387 to it. When we have something like GOTOFF relocs, we can use a plain
388 reference to find the runtime address. Without that, we have to rely
389 on the `l_addr' value, which is not the value we want when prelinked. */
390 #if USE___THREAD
391 dtv_t initdtv[3];
392 ElfW(Ehdr) *ehdr
393 # ifdef DONT_USE_BOOTSTRAP_MAP
394 = (ElfW(Ehdr) *) &_begin;
395 # else
396 # error This will not work with prelink.
397 = (ElfW(Ehdr) *) bootstrap_map.l_addr;
398 # endif
399 ElfW(Phdr) *phdr = (ElfW(Phdr) *) ((void *) ehdr + ehdr->e_phoff);
400 size_t cnt = ehdr->e_phnum; /* PT_TLS is usually the last phdr. */
401 while (cnt-- > 0)
402 if (phdr[cnt].p_type == PT_TLS)
404 void *tlsblock;
405 size_t max_align = MAX (TLS_INIT_TCB_ALIGN, phdr[cnt].p_align);
406 char *p;
408 bootstrap_map.l_tls_blocksize = phdr[cnt].p_memsz;
409 bootstrap_map.l_tls_align = phdr[cnt].p_align;
410 if (phdr[cnt].p_align == 0)
411 bootstrap_map.l_tls_firstbyte_offset = 0;
412 else
413 bootstrap_map.l_tls_firstbyte_offset = (phdr[cnt].p_vaddr
414 & (phdr[cnt].p_align - 1));
415 assert (bootstrap_map.l_tls_blocksize != 0);
416 bootstrap_map.l_tls_initimage_size = phdr[cnt].p_filesz;
417 bootstrap_map.l_tls_initimage = (void *) (bootstrap_map.l_addr
418 + phdr[cnt].p_vaddr);
420 /* We can now allocate the initial TLS block. This can happen
421 on the stack. We'll get the final memory later when we
422 know all about the various objects loaded at startup
423 time. */
424 # if TLS_TCB_AT_TP
425 tlsblock = alloca (roundup (bootstrap_map.l_tls_blocksize,
426 TLS_INIT_TCB_ALIGN)
427 + TLS_INIT_TCB_SIZE
428 + max_align);
429 # elif TLS_DTV_AT_TP
430 tlsblock = alloca (roundup (TLS_INIT_TCB_SIZE,
431 bootstrap_map.l_tls_align)
432 + bootstrap_map.l_tls_blocksize
433 + max_align);
434 # else
435 /* In case a model with a different layout for the TCB and DTV
436 is defined add another #elif here and in the following #ifs. */
437 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
438 # endif
439 /* Align the TLS block. */
440 tlsblock = (void *) (((uintptr_t) tlsblock + max_align - 1)
441 & ~(max_align - 1));
443 /* Initialize the dtv. [0] is the length, [1] the generation
444 counter. */
445 initdtv[0].counter = 1;
446 initdtv[1].counter = 0;
448 /* Initialize the TLS block. */
449 # if TLS_TCB_AT_TP
450 initdtv[2].pointer = tlsblock;
451 # elif TLS_DTV_AT_TP
452 bootstrap_map.l_tls_offset = roundup (TLS_INIT_TCB_SIZE,
453 bootstrap_map.l_tls_align);
454 initdtv[2].pointer = (char *) tlsblock + bootstrap_map.l_tls_offset;
455 # else
456 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
457 # endif
458 p = __mempcpy (initdtv[2].pointer, bootstrap_map.l_tls_initimage,
459 bootstrap_map.l_tls_initimage_size);
460 # ifdef HAVE_BUILTIN_MEMSET
461 __builtin_memset (p, '\0', (bootstrap_map.l_tls_blocksize
462 - bootstrap_map.l_tls_initimage_size));
463 # else
465 size_t remaining = (bootstrap_map.l_tls_blocksize
466 - bootstrap_map.l_tls_initimage_size);
467 while (remaining-- > 0)
468 *p++ = '\0';
470 # endif
472 /* Install the pointer to the dtv. */
474 /* Initialize the thread pointer. */
475 # if TLS_TCB_AT_TP
476 bootstrap_map.l_tls_offset
477 = roundup (bootstrap_map.l_tls_blocksize, TLS_INIT_TCB_ALIGN);
479 INSTALL_DTV ((char *) tlsblock + bootstrap_map.l_tls_offset,
480 initdtv);
482 const char *lossage = TLS_INIT_TP ((char *) tlsblock
483 + bootstrap_map.l_tls_offset, 0);
484 # elif TLS_DTV_AT_TP
485 INSTALL_DTV (tlsblock, initdtv);
486 const char *lossage = TLS_INIT_TP (tlsblock, 0);
487 # else
488 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
489 # endif
490 if (__builtin_expect (lossage != NULL, 0))
491 _dl_fatal_printf ("cannot set up thread-local storage: %s\n",
492 lossage);
494 /* So far this is module number one. */
495 bootstrap_map.l_tls_modid = 1;
497 /* There can only be one PT_TLS entry. */
498 break;
500 #endif /* USE___THREAD */
502 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
503 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info);
504 #endif
506 if (bootstrap_map.l_addr || ! bootstrap_map.l_info[VALIDX(DT_GNU_PRELINKED)])
508 /* Relocate ourselves so we can do normal function calls and
509 data access using the global offset table. */
511 ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0, 0);
513 bootstrap_map.l_relocated = 1;
515 /* Please note that we don't allow profiling of this object and
516 therefore need not test whether we have to allocate the array
517 for the relocation results (as done in dl-reloc.c). */
519 /* Now life is sane; we can call functions and access global data.
520 Set up to use the operating system facilities, and find out from
521 the operating system's program loader where to find the program
522 header table in core. Put the rest of _dl_start into a separate
523 function, that way the compiler cannot put accesses to the GOT
524 before ELF_DYNAMIC_RELOCATE. */
526 #ifdef DONT_USE_BOOTSTRAP_MAP
527 ElfW(Addr) entry = _dl_start_final (arg);
528 #else
529 ElfW(Addr) entry = _dl_start_final (arg, &info);
530 #endif
532 #ifndef ELF_MACHINE_START_ADDRESS
533 # define ELF_MACHINE_START_ADDRESS(map, start) (start)
534 #endif
536 return ELF_MACHINE_START_ADDRESS (GL(dl_ns)[LM_ID_BASE]._ns_loaded, entry);
542 /* Now life is peachy; we can do all normal operations.
543 On to the real work. */
545 /* Some helper functions. */
547 /* Arguments to relocate_doit. */
548 struct relocate_args
550 struct link_map *l;
551 int lazy;
554 struct map_args
556 /* Argument to map_doit. */
557 char *str;
558 struct link_map *loader;
559 int is_preloaded;
560 int mode;
561 /* Return value of map_doit. */
562 struct link_map *map;
565 struct dlmopen_args
567 const char *fname;
568 struct link_map *map;
571 struct lookup_args
573 const char *name;
574 struct link_map *map;
575 void *result;
578 /* Arguments to version_check_doit. */
579 struct version_check_args
581 int doexit;
582 int dotrace;
585 static void
586 relocate_doit (void *a)
588 struct relocate_args *args = (struct relocate_args *) a;
590 _dl_relocate_object (args->l, args->l->l_scope, args->lazy, 0);
593 static void
594 map_doit (void *a)
596 struct map_args *args = (struct map_args *) a;
597 args->map = _dl_map_object (args->loader, args->str,
598 args->is_preloaded, lt_library, 0, args->mode,
599 LM_ID_BASE);
602 static void
603 dlmopen_doit (void *a)
605 struct dlmopen_args *args = (struct dlmopen_args *) a;
606 args->map = _dl_open (args->fname, RTLD_LAZY | __RTLD_DLOPEN | __RTLD_AUDIT,
607 dl_main, LM_ID_NEWLM, _dl_argc, INTUSE(_dl_argv),
608 __environ);
611 static void
612 lookup_doit (void *a)
614 struct lookup_args *args = (struct lookup_args *) a;
615 const ElfW(Sym) *ref = NULL;
616 args->result = NULL;
617 lookup_t l = _dl_lookup_symbol_x (args->name, args->map, &ref,
618 args->map->l_local_scope, NULL, 0,
619 DL_LOOKUP_RETURN_NEWEST, NULL);
620 if (ref != NULL)
621 args->result = DL_SYMBOL_ADDRESS (l, ref);
624 static void
625 version_check_doit (void *a)
627 struct version_check_args *args = (struct version_check_args *) a;
628 if (_dl_check_all_versions (GL(dl_ns)[LM_ID_BASE]._ns_loaded, 1,
629 args->dotrace) && args->doexit)
630 /* We cannot start the application. Abort now. */
631 _exit (1);
635 static inline struct link_map *
636 find_needed (const char *name)
638 struct r_scope_elem *scope = &GL(dl_ns)[LM_ID_BASE]._ns_loaded->l_searchlist;
639 unsigned int n = scope->r_nlist;
641 while (n-- > 0)
642 if (_dl_name_match_p (name, scope->r_list[n]))
643 return scope->r_list[n];
645 /* Should never happen. */
646 return NULL;
649 static int
650 match_version (const char *string, struct link_map *map)
652 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
653 ElfW(Verdef) *def;
655 #define VERDEFTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
656 if (map->l_info[VERDEFTAG] == NULL)
657 /* The file has no symbol versioning. */
658 return 0;
660 def = (ElfW(Verdef) *) ((char *) map->l_addr
661 + map->l_info[VERDEFTAG]->d_un.d_ptr);
662 while (1)
664 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
666 /* Compare the version strings. */
667 if (strcmp (string, strtab + aux->vda_name) == 0)
668 /* Bingo! */
669 return 1;
671 /* If no more definitions we failed to find what we want. */
672 if (def->vd_next == 0)
673 break;
675 /* Next definition. */
676 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
679 return 0;
682 #ifdef USE_TLS
683 static bool tls_init_tp_called;
685 static void *
686 init_tls (void)
688 /* Number of elements in the static TLS block. */
689 GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx);
691 /* Do not do this twice. The audit interface might have required
692 the DTV interfaces to be set up early. */
693 if (GL(dl_initial_dtv) != NULL)
694 return NULL;
696 /* Allocate the array which contains the information about the
697 dtv slots. We allocate a few entries more than needed to
698 avoid the need for reallocation. */
699 size_t nelem = GL(dl_tls_max_dtv_idx) + 1 + TLS_SLOTINFO_SURPLUS;
701 /* Allocate. */
702 GL(dl_tls_dtv_slotinfo_list) = (struct dtv_slotinfo_list *)
703 calloc (sizeof (struct dtv_slotinfo_list)
704 + nelem * sizeof (struct dtv_slotinfo), 1);
705 /* No need to check the return value. If memory allocation failed
706 the program would have been terminated. */
708 struct dtv_slotinfo *slotinfo = GL(dl_tls_dtv_slotinfo_list)->slotinfo;
709 GL(dl_tls_dtv_slotinfo_list)->len = nelem;
710 GL(dl_tls_dtv_slotinfo_list)->next = NULL;
712 /* Fill in the information from the loaded modules. No namespace
713 but the base one can be filled at this time. */
714 assert (GL(dl_ns)[LM_ID_BASE + 1]._ns_loaded == NULL);
715 int i = 0;
716 for (struct link_map *l = GL(dl_ns)[LM_ID_BASE]._ns_loaded; l != NULL;
717 l = l->l_next)
718 if (l->l_tls_blocksize != 0)
720 /* This is a module with TLS data. Store the map reference.
721 The generation counter is zero. */
722 slotinfo[i].map = l;
723 /* slotinfo[i].gen = 0; */
724 ++i;
726 assert (i == GL(dl_tls_max_dtv_idx));
728 /* Compute the TLS offsets for the various blocks. */
729 _dl_determine_tlsoffset ();
731 /* Construct the static TLS block and the dtv for the initial
732 thread. For some platforms this will include allocating memory
733 for the thread descriptor. The memory for the TLS block will
734 never be freed. It should be allocated accordingly. The dtv
735 array can be changed if dynamic loading requires it. */
736 void *tcbp = _dl_allocate_tls_storage ();
737 if (tcbp == NULL)
738 _dl_fatal_printf ("\
739 cannot allocate TLS data structures for initial thread");
741 /* Store for detection of the special case by __tls_get_addr
742 so it knows not to pass this dtv to the normal realloc. */
743 GL(dl_initial_dtv) = GET_DTV (tcbp);
745 /* And finally install it for the main thread. If ld.so itself uses
746 TLS we know the thread pointer was initialized earlier. */
747 const char *lossage = TLS_INIT_TP (tcbp, USE___THREAD);
748 if (__builtin_expect (lossage != NULL, 0))
749 _dl_fatal_printf ("cannot set up thread-local storage: %s\n", lossage);
750 tls_init_tp_called = true;
752 return tcbp;
754 #endif
756 #ifdef _LIBC_REENTRANT
757 /* _dl_error_catch_tsd points to this for the single-threaded case.
758 It's reset by the thread library for multithreaded programs. */
759 void ** __attribute__ ((const))
760 _dl_initial_error_catch_tsd (void)
762 static void *data;
763 return &data;
765 #endif
767 #if defined SHARED && defined _LIBC_REENTRANT \
768 && defined __rtld_lock_default_lock_recursive
769 static void rtld_lock_default_lock_recursive (void *lock)
771 __rtld_lock_default_lock_recursive (lock);
774 static void rtld_lock_default_unlock_recursive (void *lock)
776 __rtld_lock_default_unlock_recursive (lock);
778 #endif
781 /* The library search path. */
782 static const char *library_path attribute_relro;
783 /* The list preloaded objects. */
784 static const char *preloadlist attribute_relro;
785 /* Nonzero if information about versions has to be printed. */
786 static int version_info attribute_relro;
788 static void
789 dl_main (const ElfW(Phdr) *phdr,
790 ElfW(Word) phnum,
791 ElfW(Addr) *user_entry)
793 const ElfW(Phdr) *ph;
794 enum mode mode;
795 struct link_map **preloads;
796 unsigned int npreloads;
797 struct link_map *main_map;
798 size_t file_size;
799 char *file;
800 bool has_interp = false;
801 unsigned int i;
802 bool prelinked = false;
803 bool rtld_is_main = false;
804 #ifndef HP_TIMING_NONAVAIL
805 hp_timing_t start;
806 hp_timing_t stop;
807 hp_timing_t diff;
808 #endif
809 #ifdef USE_TLS
810 void *tcbp = NULL;
811 #endif
813 #ifdef _LIBC_REENTRANT
814 /* Explicit initialization since the reloc would just be more work. */
815 GL(dl_error_catch_tsd) = &_dl_initial_error_catch_tsd;
816 #endif
818 #ifdef USE_TLS
819 GL(dl_init_static_tls) = &_dl_nothread_init_static_tls;
820 #endif
822 #if defined SHARED && defined _LIBC_REENTRANT \
823 && defined __rtld_lock_default_lock_recursive
824 GL(dl_rtld_lock_recursive) = rtld_lock_default_lock_recursive;
825 GL(dl_rtld_unlock_recursive) = rtld_lock_default_unlock_recursive;
826 #endif
828 /* The explicit initialization here is cheaper than processing the reloc
829 in the _rtld_local definition's initializer. */
830 GL(dl_make_stack_executable_hook) = &_dl_make_stack_executable;
832 /* Process the environment variable which control the behaviour. */
833 process_envvars (&mode);
835 #ifndef HAVE_INLINED_SYSCALLS
836 /* Set up a flag which tells we are just starting. */
837 INTUSE(_dl_starting_up) = 1;
838 #endif
840 if (*user_entry == (ElfW(Addr)) ENTRY_POINT)
842 /* Ho ho. We are not the program interpreter! We are the program
843 itself! This means someone ran ld.so as a command. Well, that
844 might be convenient to do sometimes. We support it by
845 interpreting the args like this:
847 ld.so PROGRAM ARGS...
849 The first argument is the name of a file containing an ELF
850 executable we will load and run with the following arguments.
851 To simplify life here, PROGRAM is searched for using the
852 normal rules for shared objects, rather than $PATH or anything
853 like that. We just load it and use its entry point; we don't
854 pay attention to its PT_INTERP command (we are the interpreter
855 ourselves). This is an easy way to test a new ld.so before
856 installing it. */
857 rtld_is_main = true;
859 /* Note the place where the dynamic linker actually came from. */
860 GL(dl_rtld_map).l_name = rtld_progname;
862 while (_dl_argc > 1)
863 if (! strcmp (INTUSE(_dl_argv)[1], "--list"))
865 mode = list;
866 GLRO(dl_lazy) = -1; /* This means do no dependency analysis. */
868 ++_dl_skip_args;
869 --_dl_argc;
870 ++INTUSE(_dl_argv);
872 else if (! strcmp (INTUSE(_dl_argv)[1], "--verify"))
874 mode = verify;
876 ++_dl_skip_args;
877 --_dl_argc;
878 ++INTUSE(_dl_argv);
880 else if (! strcmp (INTUSE(_dl_argv)[1], "--library-path")
881 && _dl_argc > 2)
883 library_path = INTUSE(_dl_argv)[2];
885 _dl_skip_args += 2;
886 _dl_argc -= 2;
887 INTUSE(_dl_argv) += 2;
889 else if (! strcmp (INTUSE(_dl_argv)[1], "--inhibit-rpath")
890 && _dl_argc > 2)
892 GLRO(dl_inhibit_rpath) = INTUSE(_dl_argv)[2];
894 _dl_skip_args += 2;
895 _dl_argc -= 2;
896 INTUSE(_dl_argv) += 2;
898 else
899 break;
901 /* If we have no further argument the program was called incorrectly.
902 Grant the user some education. */
903 if (_dl_argc < 2)
904 _dl_fatal_printf ("\
905 Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
906 You have invoked `ld.so', the helper program for shared library executables.\n\
907 This program usually lives in the file `/lib/ld.so', and special directives\n\
908 in executable files using ELF shared libraries tell the system's program\n\
909 loader to load the helper program from this file. This helper program loads\n\
910 the shared libraries needed by the program executable, prepares the program\n\
911 to run, and runs it. You may invoke this helper program directly from the\n\
912 command line to load and run an ELF executable file; this is like executing\n\
913 that file itself, but always uses this helper program from the file you\n\
914 specified, instead of the helper program file specified in the executable\n\
915 file you run. This is mostly of use for maintainers to test new versions\n\
916 of this helper program; chances are you did not intend to run this program.\n\
918 --list list all dependencies and how they are resolved\n\
919 --verify verify that given object really is a dynamically linked\n\
920 object we can handle\n\
921 --library-path PATH use given PATH instead of content of the environment\n\
922 variable LD_LIBRARY_PATH\n\
923 --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\
924 in LIST\n");
926 ++_dl_skip_args;
927 --_dl_argc;
928 ++INTUSE(_dl_argv);
930 /* Initialize the data structures for the search paths for shared
931 objects. */
932 _dl_init_paths (library_path);
935 /* The initialization of _dl_stack_flags done below assumes the
936 executable's PT_GNU_STACK may have been honored by the kernel, and
937 so a PT_GNU_STACK with PF_X set means the stack started out with
938 execute permission. However, this is not really true if the
939 dynamic linker is the executable the kernel loaded. For this
940 case, we must reinitialize _dl_stack_flags to match the dynamic
941 linker itself. If the dynamic linker was built with a
942 PT_GNU_STACK, then the kernel may have loaded us with a
943 nonexecutable stack that we will have to make executable when we
944 load the program below unless it has a PT_GNU_STACK indicating
945 nonexecutable stack is ok. */
947 for (ph = phdr; ph < &phdr[phnum]; ++ph)
948 if (ph->p_type == PT_GNU_STACK)
950 GL(dl_stack_flags) = ph->p_flags;
951 break;
954 if (__builtin_expect (mode, normal) == verify)
956 const char *objname;
957 const char *err_str = NULL;
958 struct map_args args;
960 args.str = rtld_progname;
961 args.loader = NULL;
962 args.is_preloaded = 0;
963 args.mode = __RTLD_OPENEXEC;
964 (void) _dl_catch_error (&objname, &err_str, map_doit, &args);
965 if (__builtin_expect (err_str != NULL, 0))
966 /* We don't free the returned string, the programs stops
967 anyway. */
968 _exit (EXIT_FAILURE);
970 else
972 HP_TIMING_NOW (start);
973 _dl_map_object (NULL, rtld_progname, 0, lt_library, 0,
974 __RTLD_OPENEXEC, LM_ID_BASE);
975 HP_TIMING_NOW (stop);
977 HP_TIMING_DIFF (load_time, start, stop);
980 /* Now the map for the main executable is available. */
981 main_map = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
983 phdr = main_map->l_phdr;
984 phnum = main_map->l_phnum;
985 /* We overwrite here a pointer to a malloc()ed string. But since
986 the malloc() implementation used at this point is the dummy
987 implementations which has no real free() function it does not
988 makes sense to free the old string first. */
989 main_map->l_name = (char *) "";
990 *user_entry = main_map->l_entry;
992 else
994 /* Create a link_map for the executable itself.
995 This will be what dlopen on "" returns. */
996 main_map = _dl_new_object ((char *) "", "", lt_executable, NULL,
997 __RTLD_OPENEXEC, LM_ID_BASE);
998 assert (main_map != NULL);
999 assert (main_map == GL(dl_ns)[LM_ID_BASE]._ns_loaded);
1000 main_map->l_phdr = phdr;
1001 main_map->l_phnum = phnum;
1002 main_map->l_entry = *user_entry;
1004 /* At this point we are in a bit of trouble. We would have to
1005 fill in the values for l_dev and l_ino. But in general we
1006 do not know where the file is. We also do not handle AT_EXECFD
1007 even if it would be passed up.
1009 We leave the values here defined to 0. This is normally no
1010 problem as the program code itself is normally no shared
1011 object and therefore cannot be loaded dynamically. Nothing
1012 prevent the use of dynamic binaries and in these situations
1013 we might get problems. We might not be able to find out
1014 whether the object is already loaded. But since there is no
1015 easy way out and because the dynamic binary must also not
1016 have an SONAME we ignore this program for now. If it becomes
1017 a problem we can force people using SONAMEs. */
1019 /* We delay initializing the path structure until we got the dynamic
1020 information for the program. */
1023 main_map->l_map_end = 0;
1024 main_map->l_text_end = 0;
1025 /* Perhaps the executable has no PT_LOAD header entries at all. */
1026 main_map->l_map_start = ~0;
1027 /* We opened the file, account for it. */
1028 ++main_map->l_opencount;
1029 /* And it was opened directly. */
1030 ++main_map->l_direct_opencount;
1032 /* Scan the program header table for the dynamic section. */
1033 for (ph = phdr; ph < &phdr[phnum]; ++ph)
1034 switch (ph->p_type)
1036 case PT_PHDR:
1037 /* Find out the load address. */
1038 main_map->l_addr = (ElfW(Addr)) phdr - ph->p_vaddr;
1039 break;
1040 case PT_DYNAMIC:
1041 /* This tells us where to find the dynamic section,
1042 which tells us everything we need to do. */
1043 main_map->l_ld = (void *) main_map->l_addr + ph->p_vaddr;
1044 break;
1045 case PT_INTERP:
1046 /* This "interpreter segment" was used by the program loader to
1047 find the program interpreter, which is this program itself, the
1048 dynamic linker. We note what name finds us, so that a future
1049 dlopen call or DT_NEEDED entry, for something that wants to link
1050 against the dynamic linker as a shared library, will know that
1051 the shared object is already loaded. */
1052 _dl_rtld_libname.name = ((const char *) main_map->l_addr
1053 + ph->p_vaddr);
1054 /* _dl_rtld_libname.next = NULL; Already zero. */
1055 GL(dl_rtld_map).l_libname = &_dl_rtld_libname;
1057 /* Ordinarilly, we would get additional names for the loader from
1058 our DT_SONAME. This can't happen if we were actually linked as
1059 a static executable (detect this case when we have no DYNAMIC).
1060 If so, assume the filename component of the interpreter path to
1061 be our SONAME, and add it to our name list. */
1062 if (GL(dl_rtld_map).l_ld == NULL)
1064 const char *p = NULL;
1065 const char *cp = _dl_rtld_libname.name;
1067 /* Find the filename part of the path. */
1068 while (*cp != '\0')
1069 if (*cp++ == '/')
1070 p = cp;
1072 if (p != NULL)
1074 _dl_rtld_libname2.name = p;
1075 /* _dl_rtld_libname2.next = NULL; Already zero. */
1076 _dl_rtld_libname.next = &_dl_rtld_libname2;
1080 has_interp = true;
1081 break;
1082 case PT_LOAD:
1084 ElfW(Addr) mapstart;
1085 ElfW(Addr) allocend;
1087 /* Remember where the main program starts in memory. */
1088 mapstart = (main_map->l_addr + (ph->p_vaddr & ~(ph->p_align - 1)));
1089 if (main_map->l_map_start > mapstart)
1090 main_map->l_map_start = mapstart;
1092 /* Also where it ends. */
1093 allocend = main_map->l_addr + ph->p_vaddr + ph->p_memsz;
1094 if (main_map->l_map_end < allocend)
1095 main_map->l_map_end = allocend;
1096 if ((ph->p_flags & PF_X) && allocend > main_map->l_text_end)
1097 main_map->l_text_end = allocend;
1099 break;
1101 case PT_TLS:
1102 #ifdef USE_TLS
1103 if (ph->p_memsz > 0)
1105 /* Note that in the case the dynamic linker we duplicate work
1106 here since we read the PT_TLS entry already in
1107 _dl_start_final. But the result is repeatable so do not
1108 check for this special but unimportant case. */
1109 main_map->l_tls_blocksize = ph->p_memsz;
1110 main_map->l_tls_align = ph->p_align;
1111 if (ph->p_align == 0)
1112 main_map->l_tls_firstbyte_offset = 0;
1113 else
1114 main_map->l_tls_firstbyte_offset = (ph->p_vaddr
1115 & (ph->p_align - 1));
1116 main_map->l_tls_initimage_size = ph->p_filesz;
1117 main_map->l_tls_initimage = (void *) ph->p_vaddr;
1119 /* This image gets the ID one. */
1120 GL(dl_tls_max_dtv_idx) = main_map->l_tls_modid = 1;
1122 #else
1123 _dl_fatal_printf ("\
1124 ld.so does not support TLS, but program uses it!\n");
1125 #endif
1126 break;
1128 case PT_GNU_STACK:
1129 GL(dl_stack_flags) = ph->p_flags;
1130 break;
1132 case PT_GNU_RELRO:
1133 main_map->l_relro_addr = ph->p_vaddr;
1134 main_map->l_relro_size = ph->p_memsz;
1135 break;
1137 #ifdef USE_TLS
1138 /* Adjust the address of the TLS initialization image in case
1139 the executable is actually an ET_DYN object. */
1140 if (main_map->l_tls_initimage != NULL)
1141 main_map->l_tls_initimage
1142 = (char *) main_map->l_tls_initimage + main_map->l_addr;
1143 #endif
1144 if (! main_map->l_map_end)
1145 main_map->l_map_end = ~0;
1146 if (! main_map->l_text_end)
1147 main_map->l_text_end = ~0;
1148 if (! GL(dl_rtld_map).l_libname && GL(dl_rtld_map).l_name)
1150 /* We were invoked directly, so the program might not have a
1151 PT_INTERP. */
1152 _dl_rtld_libname.name = GL(dl_rtld_map).l_name;
1153 /* _dl_rtld_libname.next = NULL; Already zero. */
1154 GL(dl_rtld_map).l_libname = &_dl_rtld_libname;
1156 else
1157 assert (GL(dl_rtld_map).l_libname); /* How else did we get here? */
1159 /* If the current libname is different from the SONAME, add the
1160 latter as well. */
1161 if (GL(dl_rtld_map).l_info[DT_SONAME] != NULL
1162 && strcmp (GL(dl_rtld_map).l_libname->name,
1163 (const char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB])
1164 + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_val) != 0)
1166 static struct libname_list newname;
1167 newname.name = ((char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB])
1168 + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_ptr);
1169 newname.next = NULL;
1170 newname.dont_free = 1;
1172 assert (GL(dl_rtld_map).l_libname->next == NULL);
1173 GL(dl_rtld_map).l_libname->next = &newname;
1175 /* The ld.so must be relocated since otherwise loading audit modules
1176 will fail since they reuse the very same ld.so. */
1177 assert (GL(dl_rtld_map).l_relocated);
1179 if (! rtld_is_main)
1181 /* Extract the contents of the dynamic section for easy access. */
1182 elf_get_dynamic_info (main_map, NULL);
1183 /* Set up our cache of pointers into the hash table. */
1184 _dl_setup_hash (main_map);
1187 if (__builtin_expect (mode, normal) == verify)
1189 /* We were called just to verify that this is a dynamic
1190 executable using us as the program interpreter. Exit with an
1191 error if we were not able to load the binary or no interpreter
1192 is specified (i.e., this is no dynamically linked binary. */
1193 if (main_map->l_ld == NULL)
1194 _exit (1);
1196 /* We allow here some platform specific code. */
1197 #ifdef DISTINGUISH_LIB_VERSIONS
1198 DISTINGUISH_LIB_VERSIONS;
1199 #endif
1200 _exit (has_interp ? 0 : 2);
1203 if (! rtld_is_main)
1204 /* Initialize the data structures for the search paths for shared
1205 objects. */
1206 _dl_init_paths (library_path);
1208 /* Initialize _r_debug. */
1209 struct r_debug *r = _dl_debug_initialize (GL(dl_rtld_map).l_addr,
1210 LM_ID_BASE);
1211 r->r_state = RT_CONSISTENT;
1213 /* Put the link_map for ourselves on the chain so it can be found by
1214 name. Note that at this point the global chain of link maps contains
1215 exactly one element, which is pointed to by dl_loaded. */
1216 if (! GL(dl_rtld_map).l_name)
1217 /* If not invoked directly, the dynamic linker shared object file was
1218 found by the PT_INTERP name. */
1219 GL(dl_rtld_map).l_name = (char *) GL(dl_rtld_map).l_libname->name;
1220 GL(dl_rtld_map).l_type = lt_library;
1221 main_map->l_next = &GL(dl_rtld_map);
1222 GL(dl_rtld_map).l_prev = main_map;
1223 ++GL(dl_ns)[LM_ID_BASE]._ns_nloaded;
1224 ++GL(dl_load_adds);
1226 /* If LD_USE_LOAD_BIAS env variable has not been seen, default
1227 to not using bias for non-prelinked PIEs and libraries
1228 and using it for executables or prelinked PIEs or libraries. */
1229 if (GLRO(dl_use_load_bias) == (ElfW(Addr)) -2)
1230 GLRO(dl_use_load_bias) = main_map->l_addr == 0 ? -1 : 0;
1232 /* Set up the program header information for the dynamic linker
1233 itself. It is needed in the dl_iterate_phdr() callbacks. */
1234 ElfW(Ehdr) *rtld_ehdr = (ElfW(Ehdr) *) GL(dl_rtld_map).l_map_start;
1235 ElfW(Phdr) *rtld_phdr = (ElfW(Phdr) *) (GL(dl_rtld_map).l_map_start
1236 + rtld_ehdr->e_phoff);
1237 GL(dl_rtld_map).l_phdr = rtld_phdr;
1238 GL(dl_rtld_map).l_phnum = rtld_ehdr->e_phnum;
1241 /* PT_GNU_RELRO is usually the last phdr. */
1242 size_t cnt = rtld_ehdr->e_phnum;
1243 while (cnt-- > 0)
1244 if (rtld_phdr[cnt].p_type == PT_GNU_RELRO)
1246 GL(dl_rtld_map).l_relro_addr = rtld_phdr[cnt].p_vaddr;
1247 GL(dl_rtld_map).l_relro_size = rtld_phdr[cnt].p_memsz;
1248 break;
1251 #ifdef USE_TLS
1252 /* Add the dynamic linker to the TLS list if it also uses TLS. */
1253 if (GL(dl_rtld_map).l_tls_blocksize != 0)
1254 /* Assign a module ID. Do this before loading any audit modules. */
1255 GL(dl_rtld_map).l_tls_modid = _dl_next_tls_modid ();
1256 #endif
1258 /* If we have auditing DSOs to load, do it now. */
1259 if (__builtin_expect (audit_list != NULL, 0))
1261 /* Iterate over all entries in the list. The order is important. */
1262 struct audit_ifaces *last_audit = NULL;
1263 struct audit_list *al = audit_list->next;
1266 #ifdef USE_TLS
1267 int tls_idx = GL(dl_tls_max_dtv_idx);
1269 /* Now it is time to determine the layout of the static TLS
1270 block and allocate it for the initial thread. Note that we
1271 always allocate the static block, we never defer it even if
1272 no DF_STATIC_TLS bit is set. The reason is that we know
1273 glibc will use the static model. */
1274 # ifndef TLS_INIT_TP_EXPENSIVE
1275 # define TLS_INIT_TP_EXPENSIVE 0
1276 # endif
1278 /* Since we start using the auditing DSOs right away we need to
1279 initialize the data structures now. */
1280 if (!TLS_INIT_TP_EXPENSIVE)
1281 tcbp = init_tls ();
1282 #endif
1283 struct dlmopen_args dlmargs;
1284 dlmargs.fname = al->name;
1285 dlmargs.map = NULL;
1287 const char *objname;
1288 const char *err_str = NULL;
1289 (void) _dl_catch_error (&objname, &err_str, dlmopen_doit, &dlmargs);
1290 if (__builtin_expect (err_str != NULL, 0))
1292 not_loaded:
1293 _dl_error_printf ("\
1294 ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n",
1295 al->name, err_str);
1296 free ((char *) err_str);
1298 else
1300 struct lookup_args largs;
1301 largs.name = "la_version";
1302 largs.map = dlmargs.map;
1304 /* Check whether the interface version matches. */
1305 (void) _dl_catch_error (&objname, &err_str, lookup_doit, &largs);
1307 unsigned int (*laversion) (unsigned int);
1308 unsigned int lav;
1309 if (err_str == NULL
1310 && (laversion = largs.result) != NULL
1311 && (lav = laversion (LAV_CURRENT)) > 0
1312 && lav <= LAV_CURRENT)
1314 /* Allocate structure for the callback function pointers.
1315 This call can never fail. */
1316 union
1318 struct audit_ifaces ifaces;
1319 #define naudit_ifaces 8
1320 void (*fptr[naudit_ifaces]) (void);
1321 } *newp = malloc (sizeof (*newp));
1323 /* Names of the auditing interfaces. All in one
1324 long string. */
1325 static const char audit_iface_names[] =
1326 "la_activity\0"
1327 "la_objsearch\0"
1328 "la_objopen\0"
1329 "la_preinit\0"
1330 #if __ELF_NATIVE_CLASS == 32
1331 "la_symbind32\0"
1332 #elif __ELF_NATIVE_CLASS == 64
1333 "la_symbind64\0"
1334 #else
1335 # error "__ELF_NATIVE_CLASS must be defined"
1336 #endif
1337 #define STRING(s) __STRING (s)
1338 "la_" STRING (ARCH_LA_PLTENTER) "\0"
1339 "la_" STRING (ARCH_LA_PLTEXIT) "\0"
1340 "la_objclose\0";
1341 unsigned int cnt = 0;
1342 const char *cp = audit_iface_names;
1345 largs.name = cp;
1346 (void) _dl_catch_error (&objname, &err_str, lookup_doit,
1347 &largs);
1349 /* Store the pointer. */
1350 if (err_str == NULL && largs.result != NULL)
1352 newp->fptr[cnt] = largs.result;
1354 /* The dynamic linker link map is statically
1355 allocated, initialize the data now. */
1356 GL(dl_rtld_map).l_audit[cnt].cookie
1357 = (intptr_t) &GL(dl_rtld_map);
1359 else
1360 newp->fptr[cnt] = NULL;
1361 ++cnt;
1363 cp = (char *) rawmemchr (cp, '\0') + 1;
1365 while (*cp != '\0');
1366 assert (cnt == naudit_ifaces);
1368 /* Now append the new auditing interface to the list. */
1369 newp->ifaces.next = NULL;
1370 if (last_audit == NULL)
1371 last_audit = GLRO(dl_audit) = &newp->ifaces;
1372 else
1373 last_audit = last_audit->next = &newp->ifaces;
1374 ++GLRO(dl_naudit);
1376 /* Mark the DSO as being used for auditing. */
1377 dlmargs.map->l_auditing = 1;
1379 else
1381 /* We cannot use the DSO, it does not have the
1382 appropriate interfaces or it expects something
1383 more recent. */
1384 #ifndef NDEBUG
1385 Lmid_t ns = dlmargs.map->l_ns;
1386 #endif
1387 _dl_close (dlmargs.map);
1389 /* Make sure the namespace has been cleared entirely. */
1390 assert (GL(dl_ns)[ns]._ns_loaded == NULL);
1391 assert (GL(dl_ns)[ns]._ns_nloaded == 0);
1393 #ifdef USE_TLS
1394 GL(dl_tls_max_dtv_idx) = tls_idx;
1395 #endif
1396 goto not_loaded;
1400 al = al->next;
1402 while (al != audit_list->next);
1404 /* If we have any auditing modules, announce that we already
1405 have two objects loaded. */
1406 if (__builtin_expect (GLRO(dl_naudit) > 0, 0))
1408 struct link_map *ls[2] = { main_map, &GL(dl_rtld_map) };
1410 for (unsigned int outer = 0; outer < 2; ++outer)
1412 struct audit_ifaces *afct = GLRO(dl_audit);
1413 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1415 if (afct->objopen != NULL)
1417 ls[outer]->l_audit[cnt].bindflags
1418 = afct->objopen (ls[outer], LM_ID_BASE,
1419 &ls[outer]->l_audit[cnt].cookie);
1421 ls[outer]->l_audit_any_plt
1422 |= ls[outer]->l_audit[cnt].bindflags != 0;
1425 afct = afct->next;
1431 /* We start adding objects. */
1432 r->r_state = RT_ADD;
1433 _dl_debug_state ();
1435 /* Auditing checkpoint: we are ready to signal that the initial map
1436 is being constructed. */
1437 if (__builtin_expect (GLRO(dl_naudit) > 0, 0))
1439 struct audit_ifaces *afct = GLRO(dl_audit);
1440 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1442 if (afct->activity != NULL)
1443 afct->activity (&main_map->l_audit[cnt].cookie, LA_ACT_ADD);
1445 afct = afct->next;
1449 /* We have two ways to specify objects to preload: via environment
1450 variable and via the file /etc/ld.so.preload. The latter can also
1451 be used when security is enabled. */
1452 preloads = NULL;
1453 npreloads = 0;
1455 if (__builtin_expect (preloadlist != NULL, 0))
1457 /* The LD_PRELOAD environment variable gives list of libraries
1458 separated by white space or colons that are loaded before the
1459 executable's dependencies and prepended to the global scope
1460 list. If the binary is running setuid all elements
1461 containing a '/' are ignored since it is insecure. */
1462 char *list = strdupa (preloadlist);
1463 char *p;
1465 HP_TIMING_NOW (start);
1467 /* Prevent optimizing strsep. Speed is not important here. */
1468 while ((p = (strsep) (&list, " :")) != NULL)
1469 if (p[0] != '\0'
1470 && (__builtin_expect (! INTUSE(__libc_enable_secure), 1)
1471 || strchr (p, '/') == NULL))
1473 struct link_map *new_map = _dl_map_object (main_map, p, 1,
1474 lt_library, 0, 0,
1475 LM_ID_BASE);
1476 if (++new_map->l_opencount == 1)
1477 /* It is no duplicate. */
1478 ++npreloads;
1481 HP_TIMING_NOW (stop);
1482 HP_TIMING_DIFF (diff, start, stop);
1483 HP_TIMING_ACCUM_NT (load_time, diff);
1486 /* There usually is no ld.so.preload file, it should only be used
1487 for emergencies and testing. So the open call etc should usually
1488 fail. Using access() on a non-existing file is faster than using
1489 open(). So we do this first. If it succeeds we do almost twice
1490 the work but this does not matter, since it is not for production
1491 use. */
1492 static const char preload_file[] = "/etc/ld.so.preload";
1493 if (__builtin_expect (__access (preload_file, R_OK) == 0, 0))
1495 /* Read the contents of the file. */
1496 file = _dl_sysdep_read_whole_file (preload_file, &file_size,
1497 PROT_READ | PROT_WRITE);
1498 if (__builtin_expect (file != MAP_FAILED, 0))
1500 /* Parse the file. It contains names of libraries to be loaded,
1501 separated by white spaces or `:'. It may also contain
1502 comments introduced by `#'. */
1503 char *problem;
1504 char *runp;
1505 size_t rest;
1507 /* Eliminate comments. */
1508 runp = file;
1509 rest = file_size;
1510 while (rest > 0)
1512 char *comment = memchr (runp, '#', rest);
1513 if (comment == NULL)
1514 break;
1516 rest -= comment - runp;
1518 *comment = ' ';
1519 while (--rest > 0 && *++comment != '\n');
1522 /* We have one problematic case: if we have a name at the end of
1523 the file without a trailing terminating characters, we cannot
1524 place the \0. Handle the case separately. */
1525 if (file[file_size - 1] != ' ' && file[file_size - 1] != '\t'
1526 && file[file_size - 1] != '\n' && file[file_size - 1] != ':')
1528 problem = &file[file_size];
1529 while (problem > file && problem[-1] != ' '
1530 && problem[-1] != '\t'
1531 && problem[-1] != '\n' && problem[-1] != ':')
1532 --problem;
1534 if (problem > file)
1535 problem[-1] = '\0';
1537 else
1539 problem = NULL;
1540 file[file_size - 1] = '\0';
1543 HP_TIMING_NOW (start);
1545 if (file != problem)
1547 char *p;
1548 runp = file;
1549 while ((p = strsep (&runp, ": \t\n")) != NULL)
1550 if (p[0] != '\0')
1552 const char *objname;
1553 const char *err_str = NULL;
1554 struct map_args args;
1556 args.str = p;
1557 args.loader = main_map;
1558 args.is_preloaded = 1;
1559 args.mode = 0;
1561 (void) _dl_catch_error (&objname, &err_str, map_doit,
1562 &args);
1563 if (__builtin_expect (err_str != NULL, 0))
1565 _dl_error_printf ("\
1566 ERROR: ld.so: object '%s' from %s cannot be preloaded: ignored.\n",
1567 p, preload_file);
1568 /* No need to call free, this is still before
1569 the libc's malloc is used. */
1571 else if (++args.map->l_opencount == 1)
1572 /* It is no duplicate. */
1573 ++npreloads;
1577 if (problem != NULL)
1579 char *p = strndupa (problem, file_size - (problem - file));
1580 struct link_map *new_map = _dl_map_object (main_map, p, 1,
1581 lt_library, 0, 0,
1582 LM_ID_BASE);
1583 if (++new_map->l_opencount == 1)
1584 /* It is no duplicate. */
1585 ++npreloads;
1588 HP_TIMING_NOW (stop);
1589 HP_TIMING_DIFF (diff, start, stop);
1590 HP_TIMING_ACCUM_NT (load_time, diff);
1592 /* We don't need the file anymore. */
1593 __munmap (file, file_size);
1597 if (__builtin_expect (npreloads, 0) != 0)
1599 /* Set up PRELOADS with a vector of the preloaded libraries. */
1600 struct link_map *l;
1601 preloads = __alloca (npreloads * sizeof preloads[0]);
1602 l = GL(dl_rtld_map).l_next; /* End of the chain before preloads. */
1603 i = 0;
1606 preloads[i++] = l;
1607 l = l->l_next;
1608 } while (l);
1609 assert (i == npreloads);
1612 #if defined NEED_DL_SYSINFO || defined NEED_DL_SYSINFO_DSO
1613 struct link_map *sysinfo_map = NULL;
1614 if (GLRO(dl_sysinfo_dso) != NULL)
1616 /* Do an abridged version of the work _dl_map_object_from_fd would do
1617 to map in the object. It's already mapped and prelinked (and
1618 better be, since it's read-only and so we couldn't relocate it).
1619 We just want our data structures to describe it as if we had just
1620 mapped and relocated it normally. */
1621 struct link_map *l = _dl_new_object ((char *) "", "", lt_library, NULL,
1622 0, LM_ID_BASE);
1623 if (__builtin_expect (l != NULL, 1))
1625 static ElfW(Dyn) dyn_temp[DL_RO_DYN_TEMP_CNT] attribute_relro;
1627 l->l_phdr = ((const void *) GLRO(dl_sysinfo_dso)
1628 + GLRO(dl_sysinfo_dso)->e_phoff);
1629 l->l_phnum = GLRO(dl_sysinfo_dso)->e_phnum;
1630 for (uint_fast16_t i = 0; i < l->l_phnum; ++i)
1632 const ElfW(Phdr) *const ph = &l->l_phdr[i];
1633 if (ph->p_type == PT_DYNAMIC)
1635 l->l_ld = (void *) ph->p_vaddr;
1636 l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn));
1638 else if (ph->p_type == PT_LOAD)
1640 if (! l->l_addr)
1641 l->l_addr = ph->p_vaddr;
1642 if (ph->p_vaddr + ph->p_memsz >= l->l_map_end)
1643 l->l_map_end = ph->p_vaddr + ph->p_memsz;
1644 if ((ph->p_flags & PF_X)
1645 && ph->p_vaddr + ph->p_memsz >= l->l_text_end)
1646 l->l_text_end = ph->p_vaddr + ph->p_memsz;
1648 else
1649 /* There must be no TLS segment. */
1650 assert (ph->p_type != PT_TLS);
1652 l->l_map_start = (ElfW(Addr)) GLRO(dl_sysinfo_dso);
1653 l->l_addr = l->l_map_start - l->l_addr;
1654 l->l_map_end += l->l_addr;
1655 l->l_text_end += l->l_addr;
1656 l->l_ld = (void *) ((ElfW(Addr)) l->l_ld + l->l_addr);
1657 elf_get_dynamic_info (l, dyn_temp);
1658 _dl_setup_hash (l);
1659 l->l_relocated = 1;
1661 /* Now that we have the info handy, use the DSO image's soname
1662 so this object can be looked up by name. Note that we do not
1663 set l_name here. That field gives the file name of the DSO,
1664 and this DSO is not associated with any file. */
1665 if (l->l_info[DT_SONAME] != NULL)
1667 /* Work around a kernel problem. The kernel cannot handle
1668 addresses in the vsyscall DSO pages in writev() calls. */
1669 const char *dsoname = ((char *) D_PTR (l, l_info[DT_STRTAB])
1670 + l->l_info[DT_SONAME]->d_un.d_val);
1671 size_t len = strlen (dsoname);
1672 char *copy = malloc (len);
1673 if (copy == NULL)
1674 _dl_fatal_printf ("out of memory\n");
1675 l->l_libname->name = memcpy (copy, dsoname, len);
1678 /* We have a prelinked DSO preloaded by the system. */
1679 sysinfo_map = l;
1680 # ifdef NEED_DL_SYSINFO
1681 if (GLRO(dl_sysinfo) == DL_SYSINFO_DEFAULT)
1682 GLRO(dl_sysinfo) = GLRO(dl_sysinfo_dso)->e_entry + l->l_addr;
1683 # endif
1686 #endif
1688 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
1689 specified some libraries to load, these are inserted before the actual
1690 dependencies in the executable's searchlist for symbol resolution. */
1691 HP_TIMING_NOW (start);
1692 _dl_map_object_deps (main_map, preloads, npreloads, mode == trace, 0);
1693 HP_TIMING_NOW (stop);
1694 HP_TIMING_DIFF (diff, start, stop);
1695 HP_TIMING_ACCUM_NT (load_time, diff);
1697 /* Mark all objects as being in the global scope and set the open
1698 counter. */
1699 for (i = main_map->l_searchlist.r_nlist; i > 0; )
1701 --i;
1702 main_map->l_searchlist.r_list[i]->l_global = 1;
1703 ++main_map->l_searchlist.r_list[i]->l_opencount;
1706 #ifndef MAP_ANON
1707 /* We are done mapping things, so close the zero-fill descriptor. */
1708 __close (_dl_zerofd);
1709 _dl_zerofd = -1;
1710 #endif
1712 /* Remove _dl_rtld_map from the chain. */
1713 GL(dl_rtld_map).l_prev->l_next = GL(dl_rtld_map).l_next;
1714 if (GL(dl_rtld_map).l_next)
1715 GL(dl_rtld_map).l_next->l_prev = GL(dl_rtld_map).l_prev;
1717 if (__builtin_expect (GL(dl_rtld_map).l_opencount > 1, 1))
1719 /* Some DT_NEEDED entry referred to the interpreter object itself, so
1720 put it back in the list of visible objects. We insert it into the
1721 chain in symbol search order because gdb uses the chain's order as
1722 its symbol search order. */
1723 i = 1;
1724 while (main_map->l_searchlist.r_list[i] != &GL(dl_rtld_map))
1725 ++i;
1726 GL(dl_rtld_map).l_prev = main_map->l_searchlist.r_list[i - 1];
1727 if (__builtin_expect (mode, normal) == normal)
1729 GL(dl_rtld_map).l_next = (i + 1 < main_map->l_searchlist.r_nlist
1730 ? main_map->l_searchlist.r_list[i + 1]
1731 : NULL);
1732 #if defined NEED_DL_SYSINFO || defined NEED_DL_SYSINFO_DSO
1733 if (sysinfo_map != NULL
1734 && GL(dl_rtld_map).l_prev->l_next == sysinfo_map
1735 && GL(dl_rtld_map).l_next != sysinfo_map)
1736 GL(dl_rtld_map).l_prev = sysinfo_map;
1737 #endif
1739 else
1740 /* In trace mode there might be an invisible object (which we
1741 could not find) after the previous one in the search list.
1742 In this case it doesn't matter much where we put the
1743 interpreter object, so we just initialize the list pointer so
1744 that the assertion below holds. */
1745 GL(dl_rtld_map).l_next = GL(dl_rtld_map).l_prev->l_next;
1747 assert (GL(dl_rtld_map).l_prev->l_next == GL(dl_rtld_map).l_next);
1748 GL(dl_rtld_map).l_prev->l_next = &GL(dl_rtld_map);
1749 if (GL(dl_rtld_map).l_next != NULL)
1751 assert (GL(dl_rtld_map).l_next->l_prev == GL(dl_rtld_map).l_prev);
1752 GL(dl_rtld_map).l_next->l_prev = &GL(dl_rtld_map);
1756 /* Now let us see whether all libraries are available in the
1757 versions we need. */
1759 struct version_check_args args;
1760 args.doexit = mode == normal;
1761 args.dotrace = mode == trace;
1762 _dl_receive_error (print_missing_version, version_check_doit, &args);
1765 #ifdef USE_TLS
1766 /* We do not initialize any of the TLS functionality unless any of the
1767 initial modules uses TLS. This makes dynamic loading of modules with
1768 TLS impossible, but to support it requires either eagerly doing setup
1769 now or lazily doing it later. Doing it now makes us incompatible with
1770 an old kernel that can't perform TLS_INIT_TP, even if no TLS is ever
1771 used. Trying to do it lazily is too hairy to try when there could be
1772 multiple threads (from a non-TLS-using libpthread). */
1773 bool was_tls_init_tp_called = tls_init_tp_called;
1774 if (tcbp == NULL && (!TLS_INIT_TP_EXPENSIVE || GL(dl_tls_max_dtv_idx) > 0))
1775 tcbp = init_tls ();
1776 #endif
1778 if (__builtin_expect (mode, normal) != normal)
1780 /* We were run just to list the shared libraries. It is
1781 important that we do this before real relocation, because the
1782 functions we call below for output may no longer work properly
1783 after relocation. */
1784 struct link_map *l;
1786 if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
1788 struct r_scope_elem *scope = &main_map->l_searchlist;
1790 for (i = 0; i < scope->r_nlist; i++)
1792 l = scope->r_list [i];
1793 if (l->l_faked)
1795 _dl_printf ("\t%s => not found\n", l->l_libname->name);
1796 continue;
1798 if (_dl_name_match_p (GLRO(dl_trace_prelink), l))
1799 GLRO(dl_trace_prelink_map) = l;
1800 _dl_printf ("\t%s => %s (0x%0*Zx, 0x%0*Zx)",
1801 l->l_libname->name[0] ? l->l_libname->name
1802 : rtld_progname ?: "<main program>",
1803 l->l_name[0] ? l->l_name
1804 : rtld_progname ?: "<main program>",
1805 (int) sizeof l->l_map_start * 2,
1806 (size_t) l->l_map_start,
1807 (int) sizeof l->l_addr * 2,
1808 (size_t) l->l_addr);
1809 #ifdef USE_TLS
1810 if (l->l_tls_modid)
1811 _dl_printf (" TLS(0x%Zx, 0x%0*Zx)\n", l->l_tls_modid,
1812 (int) sizeof l->l_tls_offset * 2,
1813 (size_t) l->l_tls_offset);
1814 else
1815 #endif
1816 _dl_printf ("\n");
1819 else if (GLRO(dl_debug_mask) & DL_DEBUG_UNUSED)
1821 /* Look through the dependencies of the main executable
1822 and determine which of them is not actually
1823 required. */
1824 struct link_map *l = main_map;
1826 /* Relocate the main executable. */
1827 struct relocate_args args = { .l = l, .lazy = GLRO(dl_lazy) };
1828 _dl_receive_error (print_unresolved, relocate_doit, &args);
1830 /* This loop depends on the dependencies of the executable to
1831 correspond in number and order to the DT_NEEDED entries. */
1832 ElfW(Dyn) *dyn = main_map->l_ld;
1833 bool first = true;
1834 while (dyn->d_tag != DT_NULL)
1836 if (dyn->d_tag == DT_NEEDED)
1838 l = l->l_next;
1840 if (!l->l_used)
1842 if (first)
1844 _dl_printf ("Unused direct dependencies:\n");
1845 first = false;
1848 _dl_printf ("\t%s\n", l->l_name);
1852 ++dyn;
1855 _exit (first != true);
1857 else if (! main_map->l_info[DT_NEEDED])
1858 _dl_printf ("\tstatically linked\n");
1859 else
1861 for (l = main_map->l_next; l; l = l->l_next)
1862 if (l->l_faked)
1863 /* The library was not found. */
1864 _dl_printf ("\t%s => not found\n", l->l_libname->name);
1865 else if (strcmp (l->l_libname->name, l->l_name) == 0)
1866 _dl_printf ("\t%s (0x%0*Zx)\n", l->l_libname->name,
1867 (int) sizeof l->l_map_start * 2,
1868 (size_t) l->l_map_start);
1869 else
1870 _dl_printf ("\t%s => %s (0x%0*Zx)\n", l->l_libname->name,
1871 l->l_name, (int) sizeof l->l_map_start * 2,
1872 (size_t) l->l_map_start);
1875 if (__builtin_expect (mode, trace) != trace)
1876 for (i = 1; i < (unsigned int) _dl_argc; ++i)
1878 const ElfW(Sym) *ref = NULL;
1879 ElfW(Addr) loadbase;
1880 lookup_t result;
1882 result = _dl_lookup_symbol_x (INTUSE(_dl_argv)[i], main_map,
1883 &ref, main_map->l_scope, NULL,
1884 ELF_RTYPE_CLASS_PLT,
1885 DL_LOOKUP_ADD_DEPENDENCY, NULL);
1887 loadbase = LOOKUP_VALUE_ADDRESS (result);
1889 _dl_printf ("%s found at 0x%0*Zd in object at 0x%0*Zd\n",
1890 INTUSE(_dl_argv)[i],
1891 (int) sizeof ref->st_value * 2,
1892 (size_t) ref->st_value,
1893 (int) sizeof loadbase * 2, (size_t) loadbase);
1895 else
1897 /* If LD_WARN is set warn about undefined symbols. */
1898 if (GLRO(dl_lazy) >= 0 && GLRO(dl_verbose))
1900 /* We have to do symbol dependency testing. */
1901 struct relocate_args args;
1902 struct link_map *l;
1904 args.lazy = GLRO(dl_lazy);
1906 l = main_map;
1907 while (l->l_next)
1908 l = l->l_next;
1911 if (l != &GL(dl_rtld_map) && ! l->l_faked)
1913 args.l = l;
1914 _dl_receive_error (print_unresolved, relocate_doit,
1915 &args);
1917 l = l->l_prev;
1918 } while (l);
1920 if ((GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
1921 && GL(dl_rtld_map).l_opencount > 1)
1923 /* Mark the link map as not yet relocated again. */
1924 GL(dl_rtld_map).l_relocated = 0;
1925 _dl_relocate_object (&GL(dl_rtld_map), main_map->l_scope,
1926 0, 0);
1929 #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
1930 if (version_info)
1932 /* Print more information. This means here, print information
1933 about the versions needed. */
1934 int first = 1;
1935 struct link_map *map;
1937 for (map = main_map; map != NULL; map = map->l_next)
1939 const char *strtab;
1940 ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG];
1941 ElfW(Verneed) *ent;
1943 if (dyn == NULL)
1944 continue;
1946 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
1947 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
1949 if (first)
1951 _dl_printf ("\n\tVersion information:\n");
1952 first = 0;
1955 _dl_printf ("\t%s:\n",
1956 map->l_name[0] ? map->l_name : rtld_progname);
1958 while (1)
1960 ElfW(Vernaux) *aux;
1961 struct link_map *needed;
1963 needed = find_needed (strtab + ent->vn_file);
1964 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
1966 while (1)
1968 const char *fname = NULL;
1970 if (needed != NULL
1971 && match_version (strtab + aux->vna_name,
1972 needed))
1973 fname = needed->l_name;
1975 _dl_printf ("\t\t%s (%s) %s=> %s\n",
1976 strtab + ent->vn_file,
1977 strtab + aux->vna_name,
1978 aux->vna_flags & VER_FLG_WEAK
1979 ? "[WEAK] " : "",
1980 fname ?: "not found");
1982 if (aux->vna_next == 0)
1983 /* No more symbols. */
1984 break;
1986 /* Next symbol. */
1987 aux = (ElfW(Vernaux) *) ((char *) aux
1988 + aux->vna_next);
1991 if (ent->vn_next == 0)
1992 /* No more dependencies. */
1993 break;
1995 /* Next dependency. */
1996 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
2002 _exit (0);
2005 if (main_map->l_info[ADDRIDX (DT_GNU_LIBLIST)]
2006 && ! __builtin_expect (GLRO(dl_profile) != NULL, 0))
2008 ElfW(Lib) *liblist, *liblistend;
2009 struct link_map **r_list, **r_listend, *l;
2010 const char *strtab = (const void *) D_PTR (main_map, l_info[DT_STRTAB]);
2012 assert (main_map->l_info[VALIDX (DT_GNU_LIBLISTSZ)] != NULL);
2013 liblist = (ElfW(Lib) *)
2014 main_map->l_info[ADDRIDX (DT_GNU_LIBLIST)]->d_un.d_ptr;
2015 liblistend = (ElfW(Lib) *)
2016 ((char *) liblist +
2017 main_map->l_info[VALIDX (DT_GNU_LIBLISTSZ)]->d_un.d_val);
2018 r_list = main_map->l_searchlist.r_list;
2019 r_listend = r_list + main_map->l_searchlist.r_nlist;
2021 for (; r_list < r_listend && liblist < liblistend; r_list++)
2023 l = *r_list;
2025 if (l == main_map)
2026 continue;
2028 /* If the library is not mapped where it should, fail. */
2029 if (l->l_addr)
2030 break;
2032 /* Next, check if checksum matches. */
2033 if (l->l_info [VALIDX(DT_CHECKSUM)] == NULL
2034 || l->l_info [VALIDX(DT_CHECKSUM)]->d_un.d_val
2035 != liblist->l_checksum)
2036 break;
2038 if (l->l_info [VALIDX(DT_GNU_PRELINKED)] == NULL
2039 || l->l_info [VALIDX(DT_GNU_PRELINKED)]->d_un.d_val
2040 != liblist->l_time_stamp)
2041 break;
2043 if (! _dl_name_match_p (strtab + liblist->l_name, l))
2044 break;
2046 ++liblist;
2050 if (r_list == r_listend && liblist == liblistend)
2051 prelinked = true;
2053 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_LIBS, 0))
2054 _dl_debug_printf ("\nprelink checking: %s\n",
2055 prelinked ? "ok" : "failed");
2060 struct link_map *l = main_map;
2062 #ifdef ELF_MACHINE_DEBUG_SETUP
2064 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
2066 ELF_MACHINE_DEBUG_SETUP (l, r);
2067 ELF_MACHINE_DEBUG_SETUP (&GL(dl_rtld_map), r);
2069 #else
2071 if (l->l_info[DT_DEBUG] != NULL)
2072 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
2073 with the run-time address of the r_debug structure */
2074 l->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
2076 /* Fill in the pointer in the dynamic linker's own dynamic section, in
2077 case you run gdb on the dynamic linker directly. */
2078 if (GL(dl_rtld_map).l_info[DT_DEBUG] != NULL)
2079 GL(dl_rtld_map).l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
2080 #endif
2083 /* Now set up the variable which helps the assembler startup code. */
2084 GL(dl_ns)[LM_ID_BASE]._ns_main_searchlist = &main_map->l_searchlist;
2085 GL(dl_ns)[LM_ID_BASE]._ns_global_scope[0] = &main_map->l_searchlist;
2087 /* Save the information about the original global scope list since
2088 we need it in the memory handling later. */
2089 GLRO(dl_initial_searchlist) = *GL(dl_ns)[LM_ID_BASE]._ns_main_searchlist;
2091 if (prelinked)
2093 if (main_map->l_info [ADDRIDX (DT_GNU_CONFLICT)] != NULL)
2095 ElfW(Rela) *conflict, *conflictend;
2096 #ifndef HP_TIMING_NONAVAIL
2097 hp_timing_t start;
2098 hp_timing_t stop;
2099 #endif
2101 HP_TIMING_NOW (start);
2102 assert (main_map->l_info [VALIDX (DT_GNU_CONFLICTSZ)] != NULL);
2103 conflict = (ElfW(Rela) *)
2104 main_map->l_info [ADDRIDX (DT_GNU_CONFLICT)]->d_un.d_ptr;
2105 conflictend = (ElfW(Rela) *)
2106 ((char *) conflict
2107 + main_map->l_info [VALIDX (DT_GNU_CONFLICTSZ)]->d_un.d_val);
2108 _dl_resolve_conflicts (main_map, conflict, conflictend);
2109 HP_TIMING_NOW (stop);
2110 HP_TIMING_DIFF (relocate_time, start, stop);
2114 /* Mark all the objects so we know they have been already relocated. */
2115 for (struct link_map *l = main_map; l != NULL; l = l->l_next)
2117 l->l_relocated = 1;
2118 if (l->l_relro_size)
2119 _dl_protect_relro (l);
2121 #ifdef USE_TLS
2122 /* Add object to slot information data if necessasy. */
2123 if (l->l_tls_blocksize != 0 && tls_init_tp_called)
2124 _dl_add_to_slotinfo (l);
2125 #endif
2128 _dl_sysdep_start_cleanup ();
2130 else
2132 /* Now we have all the objects loaded. Relocate them all except for
2133 the dynamic linker itself. We do this in reverse order so that copy
2134 relocs of earlier objects overwrite the data written by later
2135 objects. We do not re-relocate the dynamic linker itself in this
2136 loop because that could result in the GOT entries for functions we
2137 call being changed, and that would break us. It is safe to relocate
2138 the dynamic linker out of order because it has no copy relocs (we
2139 know that because it is self-contained). */
2141 int consider_profiling = GLRO(dl_profile) != NULL;
2142 #ifndef HP_TIMING_NONAVAIL
2143 hp_timing_t start;
2144 hp_timing_t stop;
2145 hp_timing_t add;
2146 #endif
2148 /* If we are profiling we also must do lazy reloaction. */
2149 GLRO(dl_lazy) |= consider_profiling;
2151 struct link_map *l = main_map;
2152 while (l->l_next)
2153 l = l->l_next;
2155 HP_TIMING_NOW (start);
2158 /* While we are at it, help the memory handling a bit. We have to
2159 mark some data structures as allocated with the fake malloc()
2160 implementation in ld.so. */
2161 struct libname_list *lnp = l->l_libname->next;
2163 while (__builtin_expect (lnp != NULL, 0))
2165 lnp->dont_free = 1;
2166 lnp = lnp->next;
2169 if (l != &GL(dl_rtld_map))
2170 _dl_relocate_object (l, l->l_scope, GLRO(dl_lazy),
2171 consider_profiling);
2173 #ifdef USE_TLS
2174 /* Add object to slot information data if necessasy. */
2175 if (l->l_tls_blocksize != 0 && tls_init_tp_called)
2176 _dl_add_to_slotinfo (l);
2177 #endif
2179 l = l->l_prev;
2181 while (l);
2182 HP_TIMING_NOW (stop);
2184 HP_TIMING_DIFF (relocate_time, start, stop);
2186 /* Do any necessary cleanups for the startup OS interface code.
2187 We do these now so that no calls are made after rtld re-relocation
2188 which might be resolved to different functions than we expect.
2189 We cannot do this before relocating the other objects because
2190 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
2191 _dl_sysdep_start_cleanup ();
2193 /* Now enable profiling if needed. Like the previous call,
2194 this has to go here because the calls it makes should use the
2195 rtld versions of the functions (particularly calloc()), but it
2196 needs to have _dl_profile_map set up by the relocator. */
2197 if (__builtin_expect (GL(dl_profile_map) != NULL, 0))
2198 /* We must prepare the profiling. */
2199 _dl_start_profile ();
2201 if (GL(dl_rtld_map).l_opencount > 1)
2203 /* There was an explicit ref to the dynamic linker as a shared lib.
2204 Re-relocate ourselves with user-controlled symbol definitions. */
2205 HP_TIMING_NOW (start);
2206 /* Mark the link map as not yet relocated again. */
2207 GL(dl_rtld_map).l_relocated = 0;
2208 _dl_relocate_object (&GL(dl_rtld_map), main_map->l_scope, 0, 0);
2209 HP_TIMING_NOW (stop);
2210 HP_TIMING_DIFF (add, start, stop);
2211 HP_TIMING_ACCUM_NT (relocate_time, add);
2215 #ifndef NONTLS_INIT_TP
2216 # define NONTLS_INIT_TP do { } while (0)
2217 #endif
2219 #ifdef USE_TLS
2220 if (GL(dl_tls_max_dtv_idx) > 0 || USE___THREAD || !TLS_INIT_TP_EXPENSIVE)
2222 if (!was_tls_init_tp_called && GL(dl_tls_max_dtv_idx) > 0)
2223 ++GL(dl_tls_generation);
2225 /* Now that we have completed relocation, the initializer data
2226 for the TLS blocks has its final values and we can copy them
2227 into the main thread's TLS area, which we allocated above. */
2228 _dl_allocate_tls_init (tcbp);
2230 /* And finally install it for the main thread. If ld.so itself uses
2231 TLS we know the thread pointer was initialized earlier. */
2232 if (! tls_init_tp_called)
2234 const char *lossage = TLS_INIT_TP (tcbp, USE___THREAD);
2235 if (__builtin_expect (lossage != NULL, 0))
2236 _dl_fatal_printf ("cannot set up thread-local storage: %s\n",
2237 lossage);
2240 else
2241 #endif
2242 NONTLS_INIT_TP;
2244 #ifdef SHARED
2245 /* Auditing checkpoint: we have added all objects. */
2246 if (__builtin_expect (GLRO(dl_naudit) > 0, 0))
2248 struct link_map *head = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
2249 /* Do not call the functions for any auditing object. */
2250 if (head->l_auditing == 0)
2252 struct audit_ifaces *afct = GLRO(dl_audit);
2253 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
2255 if (afct->activity != NULL)
2256 afct->activity (&head->l_audit[cnt].cookie, LA_ACT_CONSISTENT);
2258 afct = afct->next;
2262 #endif
2264 /* Notify the debugger all new objects are now ready to go. We must re-get
2265 the address since by now the variable might be in another object. */
2266 r = _dl_debug_initialize (0, LM_ID_BASE);
2267 r->r_state = RT_CONSISTENT;
2268 _dl_debug_state ();
2270 #ifndef MAP_COPY
2271 /* We must munmap() the cache file. */
2272 _dl_unload_cache ();
2273 #endif
2275 /* Once we return, _dl_sysdep_start will invoke
2276 the DT_INIT functions and then *USER_ENTRY. */
2279 /* This is a little helper function for resolving symbols while
2280 tracing the binary. */
2281 static void
2282 print_unresolved (int errcode __attribute__ ((unused)), const char *objname,
2283 const char *errstring)
2285 if (objname[0] == '\0')
2286 objname = rtld_progname ?: "<main program>";
2287 _dl_error_printf ("%s (%s)\n", errstring, objname);
2290 /* This is a little helper function for resolving symbols while
2291 tracing the binary. */
2292 static void
2293 print_missing_version (int errcode __attribute__ ((unused)),
2294 const char *objname, const char *errstring)
2296 _dl_error_printf ("%s: %s: %s\n", rtld_progname ?: "<program name unknown>",
2297 objname, errstring);
2300 /* Nonzero if any of the debugging options is enabled. */
2301 static int any_debug attribute_relro;
2303 /* Process the string given as the parameter which explains which debugging
2304 options are enabled. */
2305 static void
2306 process_dl_debug (const char *dl_debug)
2308 /* When adding new entries make sure that the maximal length of a name
2309 is correctly handled in the LD_DEBUG_HELP code below. */
2310 static const struct
2312 unsigned char len;
2313 const char name[10];
2314 const char helptext[41];
2315 unsigned short int mask;
2316 } debopts[] =
2318 #define LEN_AND_STR(str) sizeof (str) - 1, str
2319 { LEN_AND_STR ("libs"), "display library search paths",
2320 DL_DEBUG_LIBS | DL_DEBUG_IMPCALLS },
2321 { LEN_AND_STR ("reloc"), "display relocation processing",
2322 DL_DEBUG_RELOC | DL_DEBUG_IMPCALLS },
2323 { LEN_AND_STR ("files"), "display progress for input file",
2324 DL_DEBUG_FILES | DL_DEBUG_IMPCALLS },
2325 { LEN_AND_STR ("symbols"), "display symbol table processing",
2326 DL_DEBUG_SYMBOLS | DL_DEBUG_IMPCALLS },
2327 { LEN_AND_STR ("bindings"), "display information about symbol binding",
2328 DL_DEBUG_BINDINGS | DL_DEBUG_IMPCALLS },
2329 { LEN_AND_STR ("versions"), "display version dependencies",
2330 DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS },
2331 { LEN_AND_STR ("all"), "all previous options combined",
2332 DL_DEBUG_LIBS | DL_DEBUG_RELOC | DL_DEBUG_FILES | DL_DEBUG_SYMBOLS
2333 | DL_DEBUG_BINDINGS | DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS },
2334 { LEN_AND_STR ("statistics"), "display relocation statistics",
2335 DL_DEBUG_STATISTICS },
2336 { LEN_AND_STR ("unused"), "determined unused DSOs",
2337 DL_DEBUG_UNUSED },
2338 { LEN_AND_STR ("help"), "display this help message and exit",
2339 DL_DEBUG_HELP },
2341 #define ndebopts (sizeof (debopts) / sizeof (debopts[0]))
2343 /* Skip separating white spaces and commas. */
2344 while (*dl_debug != '\0')
2346 if (*dl_debug != ' ' && *dl_debug != ',' && *dl_debug != ':')
2348 size_t cnt;
2349 size_t len = 1;
2351 while (dl_debug[len] != '\0' && dl_debug[len] != ' '
2352 && dl_debug[len] != ',' && dl_debug[len] != ':')
2353 ++len;
2355 for (cnt = 0; cnt < ndebopts; ++cnt)
2356 if (debopts[cnt].len == len
2357 && memcmp (dl_debug, debopts[cnt].name, len) == 0)
2359 GLRO(dl_debug_mask) |= debopts[cnt].mask;
2360 any_debug = 1;
2361 break;
2364 if (cnt == ndebopts)
2366 /* Display a warning and skip everything until next
2367 separator. */
2368 char *copy = strndupa (dl_debug, len);
2369 _dl_error_printf ("\
2370 warning: debug option `%s' unknown; try LD_DEBUG=help\n", copy);
2373 dl_debug += len;
2374 continue;
2377 ++dl_debug;
2380 if (GLRO(dl_debug_mask) & DL_DEBUG_HELP)
2382 size_t cnt;
2384 _dl_printf ("\
2385 Valid options for the LD_DEBUG environment variable are:\n\n");
2387 for (cnt = 0; cnt < ndebopts; ++cnt)
2388 _dl_printf (" %.*s%s%s\n", debopts[cnt].len, debopts[cnt].name,
2389 " " + debopts[cnt].len - 3,
2390 debopts[cnt].helptext);
2392 _dl_printf ("\n\
2393 To direct the debugging output into a file instead of standard output\n\
2394 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n");
2395 _exit (0);
2399 static void
2400 process_dl_audit (char *str)
2402 /* The parameter is a colon separated list of DSO names. */
2403 char *p;
2405 while ((p = (strsep) (&str, ":")) != NULL)
2406 if (p[0] != '\0'
2407 && (__builtin_expect (! INTUSE(__libc_enable_secure), 1)
2408 || strchr (p, '/') == NULL))
2410 /* This is using the local malloc, not the system malloc. The
2411 memory can never be freed. */
2412 struct audit_list *newp = malloc (sizeof (*newp));
2413 newp->name = p;
2415 if (audit_list == NULL)
2416 audit_list = newp->next = newp;
2417 else
2419 newp->next = audit_list->next;
2420 audit_list = audit_list->next = newp;
2425 /* Process all environments variables the dynamic linker must recognize.
2426 Since all of them start with `LD_' we are a bit smarter while finding
2427 all the entries. */
2428 extern char **_environ attribute_hidden;
2431 static void
2432 process_envvars (enum mode *modep)
2434 char **runp = _environ;
2435 char *envline;
2436 enum mode mode = normal;
2437 char *debug_output = NULL;
2439 /* This is the default place for profiling data file. */
2440 GLRO(dl_profile_output)
2441 = &"/var/tmp\0/var/profile"[INTUSE(__libc_enable_secure) ? 9 : 0];
2443 while ((envline = _dl_next_ld_env_entry (&runp)) != NULL)
2445 size_t len = 0;
2447 while (envline[len] != '\0' && envline[len] != '=')
2448 ++len;
2450 if (envline[len] != '=')
2451 /* This is a "LD_" variable at the end of the string without
2452 a '=' character. Ignore it since otherwise we will access
2453 invalid memory below. */
2454 continue;
2456 switch (len)
2458 case 4:
2459 /* Warning level, verbose or not. */
2460 if (memcmp (envline, "WARN", 4) == 0)
2461 GLRO(dl_verbose) = envline[5] != '\0';
2462 break;
2464 case 5:
2465 /* Debugging of the dynamic linker? */
2466 if (memcmp (envline, "DEBUG", 5) == 0)
2468 process_dl_debug (&envline[6]);
2469 break;
2471 if (memcmp (envline, "AUDIT", 5) == 0)
2472 process_dl_audit (&envline[6]);
2473 break;
2475 case 7:
2476 /* Print information about versions. */
2477 if (memcmp (envline, "VERBOSE", 7) == 0)
2479 version_info = envline[8] != '\0';
2480 break;
2483 /* List of objects to be preloaded. */
2484 if (memcmp (envline, "PRELOAD", 7) == 0)
2486 preloadlist = &envline[8];
2487 break;
2490 /* Which shared object shall be profiled. */
2491 if (memcmp (envline, "PROFILE", 7) == 0 && envline[8] != '\0')
2492 GLRO(dl_profile) = &envline[8];
2493 break;
2495 case 8:
2496 /* Do we bind early? */
2497 if (memcmp (envline, "BIND_NOW", 8) == 0)
2499 GLRO(dl_lazy) = envline[9] == '\0';
2500 break;
2502 if (memcmp (envline, "BIND_NOT", 8) == 0)
2503 GLRO(dl_bind_not) = envline[9] != '\0';
2504 break;
2506 case 9:
2507 /* Test whether we want to see the content of the auxiliary
2508 array passed up from the kernel. */
2509 if (!INTUSE(__libc_enable_secure)
2510 && memcmp (envline, "SHOW_AUXV", 9) == 0)
2511 _dl_show_auxv ();
2512 break;
2514 case 10:
2515 /* Mask for the important hardware capabilities. */
2516 if (memcmp (envline, "HWCAP_MASK", 10) == 0)
2517 GLRO(dl_hwcap_mask) = __strtoul_internal (&envline[11], NULL,
2518 0, 0);
2519 break;
2521 case 11:
2522 /* Path where the binary is found. */
2523 if (!INTUSE(__libc_enable_secure)
2524 && memcmp (envline, "ORIGIN_PATH", 11) == 0)
2525 GLRO(dl_origin_path) = &envline[12];
2526 break;
2528 case 12:
2529 /* The library search path. */
2530 if (memcmp (envline, "LIBRARY_PATH", 12) == 0)
2532 library_path = &envline[13];
2533 break;
2536 /* Where to place the profiling data file. */
2537 if (memcmp (envline, "DEBUG_OUTPUT", 12) == 0)
2539 debug_output = &envline[13];
2540 break;
2543 if (!INTUSE(__libc_enable_secure)
2544 && memcmp (envline, "DYNAMIC_WEAK", 12) == 0)
2545 GLRO(dl_dynamic_weak) = 1;
2546 break;
2548 case 13:
2549 /* We might have some extra environment variable with length 13
2550 to handle. */
2551 #ifdef EXTRA_LD_ENVVARS_13
2552 EXTRA_LD_ENVVARS_13
2553 #endif
2554 if (!INTUSE(__libc_enable_secure)
2555 && memcmp (envline, "USE_LOAD_BIAS", 13) == 0)
2556 GLRO(dl_use_load_bias) = envline[14] == '1' ? -1 : 0;
2557 break;
2559 case 14:
2560 /* Where to place the profiling data file. */
2561 if (!INTUSE(__libc_enable_secure)
2562 && memcmp (envline, "PROFILE_OUTPUT", 14) == 0
2563 && envline[15] != '\0')
2564 GLRO(dl_profile_output) = &envline[15];
2565 break;
2567 case 16:
2568 /* The mode of the dynamic linker can be set. */
2569 if (memcmp (envline, "TRACE_PRELINKING", 16) == 0)
2571 mode = trace;
2572 GLRO(dl_verbose) = 1;
2573 GLRO(dl_debug_mask) |= DL_DEBUG_PRELINK;
2574 GLRO(dl_trace_prelink) = &envline[17];
2576 break;
2578 case 20:
2579 /* The mode of the dynamic linker can be set. */
2580 if (memcmp (envline, "TRACE_LOADED_OBJECTS", 20) == 0)
2581 mode = trace;
2582 break;
2584 /* We might have some extra environment variable to handle. This
2585 is tricky due to the pre-processing of the length of the name
2586 in the switch statement here. The code here assumes that added
2587 environment variables have a different length. */
2588 #ifdef EXTRA_LD_ENVVARS
2589 EXTRA_LD_ENVVARS
2590 #endif
2594 /* The caller wants this information. */
2595 *modep = mode;
2597 /* Extra security for SUID binaries. Remove all dangerous environment
2598 variables. */
2599 if (__builtin_expect (INTUSE(__libc_enable_secure), 0))
2601 static const char unsecure_envvars[] =
2602 #ifdef EXTRA_UNSECURE_ENVVARS
2603 EXTRA_UNSECURE_ENVVARS
2604 #endif
2605 UNSECURE_ENVVARS;
2606 const char *nextp;
2608 nextp = unsecure_envvars;
2611 unsetenv (nextp);
2612 /* We could use rawmemchr but this need not be fast. */
2613 nextp = (char *) (strchr) (nextp, '\0') + 1;
2615 while (*nextp != '\0');
2617 if (__access ("/etc/suid-debug", F_OK) != 0)
2619 unsetenv ("MALLOC_CHECK_");
2620 GLRO(dl_debug_mask) = 0;
2623 if (mode != normal)
2624 _exit (5);
2626 /* If we have to run the dynamic linker in debugging mode and the
2627 LD_DEBUG_OUTPUT environment variable is given, we write the debug
2628 messages to this file. */
2629 else if (any_debug && debug_output != NULL)
2631 #ifdef O_NOFOLLOW
2632 const int flags = O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW;
2633 #else
2634 const int flags = O_WRONLY | O_APPEND | O_CREAT;
2635 #endif
2636 size_t name_len = strlen (debug_output);
2637 char buf[name_len + 12];
2638 char *startp;
2640 buf[name_len + 11] = '\0';
2641 startp = _itoa (__getpid (), &buf[name_len + 11], 10, 0);
2642 *--startp = '.';
2643 startp = memcpy (startp - name_len, debug_output, name_len);
2645 GLRO(dl_debug_fd) = __open (startp, flags, DEFFILEMODE);
2646 if (GLRO(dl_debug_fd) == -1)
2647 /* We use standard output if opening the file failed. */
2648 GLRO(dl_debug_fd) = STDOUT_FILENO;
2653 /* Print the various times we collected. */
2654 static void
2655 __attribute ((noinline))
2656 print_statistics (hp_timing_t *rtld_total_timep)
2658 #ifndef HP_TIMING_NONAVAIL
2659 char buf[200];
2660 char *cp;
2661 char *wp;
2663 /* Total time rtld used. */
2664 if (HP_TIMING_AVAIL)
2666 HP_TIMING_PRINT (buf, sizeof (buf), *rtld_total_timep);
2667 _dl_debug_printf ("\nruntime linker statistics:\n"
2668 " total startup time in dynamic loader: %s\n", buf);
2670 /* Print relocation statistics. */
2671 char pbuf[30];
2672 HP_TIMING_PRINT (buf, sizeof (buf), relocate_time);
2673 cp = _itoa ((1000ULL * relocate_time) / *rtld_total_timep,
2674 pbuf + sizeof (pbuf), 10, 0);
2675 wp = pbuf;
2676 switch (pbuf + sizeof (pbuf) - cp)
2678 case 3:
2679 *wp++ = *cp++;
2680 case 2:
2681 *wp++ = *cp++;
2682 case 1:
2683 *wp++ = '.';
2684 *wp++ = *cp++;
2686 *wp = '\0';
2687 _dl_debug_printf ("\
2688 time needed for relocation: %s (%s%%)\n", buf, pbuf);
2690 #endif
2692 unsigned long int num_relative_relocations = 0;
2693 for (Lmid_t ns = 0; ns < DL_NNS; ++ns)
2695 if (GL(dl_ns)[ns]._ns_loaded == NULL)
2696 continue;
2698 struct r_scope_elem *scope = &GL(dl_ns)[ns]._ns_loaded->l_searchlist;
2700 for (unsigned int i = 0; i < scope->r_nlist; i++)
2702 struct link_map *l = scope->r_list [i];
2704 if (l->l_addr != 0 && l->l_info[VERSYMIDX (DT_RELCOUNT)])
2705 num_relative_relocations
2706 += l->l_info[VERSYMIDX (DT_RELCOUNT)]->d_un.d_val;
2707 #ifndef ELF_MACHINE_REL_RELATIVE
2708 /* Relative relocations are processed on these architectures if
2709 library is loaded to different address than p_vaddr or
2710 if not prelinked. */
2711 if ((l->l_addr != 0 || !l->l_info[VALIDX(DT_GNU_PRELINKED)])
2712 && l->l_info[VERSYMIDX (DT_RELACOUNT)])
2713 #else
2714 /* On e.g. IA-64 or Alpha, relative relocations are processed
2715 only if library is loaded to different address than p_vaddr. */
2716 if (l->l_addr != 0 && l->l_info[VERSYMIDX (DT_RELACOUNT)])
2717 #endif
2718 num_relative_relocations
2719 += l->l_info[VERSYMIDX (DT_RELACOUNT)]->d_un.d_val;
2723 _dl_debug_printf (" number of relocations: %lu\n"
2724 " number of relocations from cache: %lu\n"
2725 " number of relative relocations: %lu\n",
2726 GL(dl_num_relocations),
2727 GL(dl_num_cache_relocations),
2728 num_relative_relocations);
2730 #ifndef HP_TIMING_NONAVAIL
2731 /* Time spend while loading the object and the dependencies. */
2732 if (HP_TIMING_AVAIL)
2734 char pbuf[30];
2735 HP_TIMING_PRINT (buf, sizeof (buf), load_time);
2736 cp = _itoa ((1000ULL * load_time) / *rtld_total_timep,
2737 pbuf + sizeof (pbuf), 10, 0);
2738 wp = pbuf;
2739 switch (pbuf + sizeof (pbuf) - cp)
2741 case 3:
2742 *wp++ = *cp++;
2743 case 2:
2744 *wp++ = *cp++;
2745 case 1:
2746 *wp++ = '.';
2747 *wp++ = *cp++;
2749 *wp = '\0';
2750 _dl_debug_printf ("\
2751 time needed to load objects: %s (%s%%)\n",
2752 buf, pbuf);
2754 #endif