*** empty log message ***
[glibc.git] / elf / rtld.c
blob4a086c2d32372f357314654c83e6582861067431
1 /* Run time dynamic linker.
2 Copyright (C) 1995-2002, 2003 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 <fcntl.h>
22 #include <stdbool.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/mman.h> /* Check if MAP_ANON is defined. */
27 #include <sys/param.h>
28 #include <sys/stat.h>
29 #include <ldsodefs.h>
30 #include <stdio-common/_itoa.h>
31 #include <entry.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>
38 #include <dl-cache.h>
39 #include <dl-procinfo.h>
40 #include <tls.h>
42 #include <assert.h>
44 /* Avoid PLT use for our local calls at startup. */
45 extern __typeof (__mempcpy) __mempcpy attribute_hidden;
47 /* GCC has mental blocks about _exit. */
48 extern __typeof (_exit) exit_internal asm ("_exit") attribute_hidden;
49 #define _exit exit_internal
51 /* Helper function to handle errors while resolving symbols. */
52 static void print_unresolved (int errcode, const char *objname,
53 const char *errsting);
55 /* Helper function to handle errors when a version is missing. */
56 static void print_missing_version (int errcode, const char *objname,
57 const char *errsting);
59 /* Print the various times we collected. */
60 static void print_statistics (void);
62 /* This is a list of all the modes the dynamic loader can be in. */
63 enum mode { normal, list, verify, trace };
65 /* Process all environments variables the dynamic linker must recognize.
66 Since all of them start with `LD_' we are a bit smarter while finding
67 all the entries. */
68 static void process_envvars (enum mode *modep);
70 int _dl_argc attribute_hidden;
71 char **_dl_argv = NULL;
72 INTDEF(_dl_argv)
74 /* Nonzero if we were run directly. */
75 unsigned int _dl_skip_args attribute_hidden;
77 /* Set nonzero during loading and initialization of executable and
78 libraries, cleared before the executable's entry point runs. This
79 must not be initialized to nonzero, because the unused dynamic
80 linker loaded in for libc.so's "ld.so.1" dep will provide the
81 definition seen by libc.so's initializer; that value must be zero,
82 and will be since that dynamic linker's _dl_start and dl_main will
83 never be called. */
84 int _dl_starting_up = 0;
85 INTVARDEF(_dl_starting_up)
87 /* This is the structure which defines all variables global to ld.so
88 (except those which cannot be added for some reason). */
89 struct rtld_global _rtld_global =
91 /* Get architecture specific initializer. */
92 #include <dl-procinfo.c>
93 ._dl_debug_fd = STDERR_FILENO,
94 #ifdef NEED_DL_SYSINFO
95 ._dl_sysinfo = DL_SYSINFO_DEFAULT,
96 #endif
97 ._dl_lazy = 1,
98 ._dl_fpu_control = _FPU_DEFAULT,
99 ._dl_correct_cache_id = _DL_CACHE_DEFAULT_ID,
100 ._dl_hwcap_mask = HWCAP_IMPORTANT,
101 #ifdef _LIBC_REENTRANT
102 ._dl_load_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER
103 #endif
105 strong_alias (_rtld_global, _rtld_local);
107 static void dl_main (const ElfW(Phdr) *phdr, ElfW(Word) phnum,
108 ElfW(Addr) *user_entry);
110 static struct libname_list _dl_rtld_libname;
111 static struct libname_list _dl_rtld_libname2;
113 /* We expect less than a second for relocation. */
114 #ifdef HP_SMALL_TIMING_AVAIL
115 # undef HP_TIMING_AVAIL
116 # define HP_TIMING_AVAIL HP_SMALL_TIMING_AVAIL
117 #endif
119 /* Variable for statistics. */
120 #ifndef HP_TIMING_NONAVAIL
121 static hp_timing_t rtld_total_time;
122 static hp_timing_t relocate_time;
123 static hp_timing_t load_time;
124 static hp_timing_t start_time;
125 #endif
127 /* Additional definitions needed by TLS initialization. */
128 #ifdef TLS_INIT_HELPER
129 TLS_INIT_HELPER
130 #endif
132 /* Helper function for syscall implementation. */
133 #ifdef DL_SYSINFO_IMPLEMENTATION
134 DL_SYSINFO_IMPLEMENTATION
135 #endif
137 /* Before ld.so is relocated we must not access variables which need
138 relocations. This means variables which are exported. Variables
139 declared as static are fine. If we can mark a variable hidden this
140 is fine, too. The latter is impotant here. We can avoid setting
141 up a temporary link map for ld.so if we can mark _rtld_global as
142 hidden. */
143 #if defined PI_STATIC_AND_HIDDEN && defined HAVE_HIDDEN \
144 && defined HAVE_VISIBILITY_ATTRIBUTE
145 # define DONT_USE_BOOTSTRAP_MAP 1
146 #endif
148 #ifdef DONT_USE_BOOTSTRAP_MAP
149 static ElfW(Addr) _dl_start_final (void *arg);
150 #else
151 struct dl_start_final_info
153 struct link_map l;
154 #if !defined HP_TIMING_NONAVAIL && HP_TIMING_INLINE
155 hp_timing_t start_time;
156 #endif
158 static ElfW(Addr) _dl_start_final (void *arg,
159 struct dl_start_final_info *info);
160 #endif
162 /* These defined magically in the linker script. */
163 extern char _begin[] attribute_hidden;
164 extern char _end[] attribute_hidden;
167 #ifdef RTLD_START
168 RTLD_START
169 #else
170 # error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
171 #endif
173 #ifndef VALIDX
174 # define VALIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
175 + DT_EXTRANUM + DT_VALTAGIDX (tag))
176 #endif
177 #ifndef ADDRIDX
178 # define ADDRIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
179 + DT_EXTRANUM + DT_VALNUM + DT_ADDRTAGIDX (tag))
180 #endif
182 /* This is the second half of _dl_start (below). It can be inlined safely
183 under DONT_USE_BOOTSTRAP_MAP, where it is careful not to make any GOT
184 references. When the tools don't permit us to avoid using a GOT entry
185 for _dl_rtld_global (no attribute_hidden support), we must make sure
186 this function is not inlined (see below). */
188 #ifdef DONT_USE_BOOTSTRAP_MAP
189 static inline ElfW(Addr) __attribute__ ((always_inline))
190 _dl_start_final (void *arg)
191 #else
192 static ElfW(Addr) __attribute__ ((noinline))
193 _dl_start_final (void *arg, struct dl_start_final_info *info)
194 #endif
196 ElfW(Addr) start_addr;
198 if (HP_TIMING_AVAIL)
200 /* If it hasn't happen yet record the startup time. */
201 if (! HP_TIMING_INLINE)
202 HP_TIMING_NOW (start_time);
203 #if !defined DONT_USE_BOOTSTRAP_MAP && !defined HP_TIMING_NONAVAIL
204 else
205 start_time = info->start_time;
206 #endif
208 /* Initialize the timing functions. */
209 HP_TIMING_DIFF_INIT ();
212 /* Transfer data about ourselves to the permanent link_map structure. */
213 #ifndef DONT_USE_BOOTSTRAP_MAP
214 GL(dl_rtld_map).l_addr = info->l.l_addr;
215 GL(dl_rtld_map).l_ld = info->l.l_ld;
216 memcpy (GL(dl_rtld_map).l_info, info->l.l_info,
217 sizeof GL(dl_rtld_map).l_info);
218 GL(dl_rtld_map).l_mach = info->l.l_mach;
219 #endif
220 _dl_setup_hash (&GL(dl_rtld_map));
221 GL(dl_rtld_map).l_opencount = 1;
222 GL(dl_rtld_map).l_map_start = (ElfW(Addr)) _begin;
223 GL(dl_rtld_map).l_map_end = (ElfW(Addr)) _end;
224 /* Copy the TLS related data if necessary. */
225 #if USE_TLS && !defined DONT_USE_BOOTSTRAP_MAP
226 # if USE___THREAD
227 assert (info->l.l_tls_modid != 0);
228 GL(dl_rtld_map).l_tls_blocksize = info->l.l_tls_blocksize;
229 GL(dl_rtld_map).l_tls_align = info->l.l_tls_align;
230 GL(dl_rtld_map).l_tls_initimage_size = info->l.l_tls_initimage_size;
231 GL(dl_rtld_map).l_tls_initimage = info->l.l_tls_initimage;
232 GL(dl_rtld_map).l_tls_offset = info->l.l_tls_offset;
233 GL(dl_rtld_map).l_tls_modid = 1;
234 # else
235 assert (info->l.l_tls_modid == 0);
236 # if NO_TLS_OFFSET != 0
237 GL(dl_rtld_map).l_tls_offset = NO_TLS_OFFSET;
238 # endif
239 # endif
241 #endif
243 #if HP_TIMING_AVAIL
244 HP_TIMING_NOW (GL(dl_cpuclock_offset));
245 #endif
247 /* Call the OS-dependent function to set up life so we can do things like
248 file access. It will call `dl_main' (below) to do all the real work
249 of the dynamic linker, and then unwind our frame and run the user
250 entry point on the same stack we entered on. */
251 start_addr = _dl_sysdep_start (arg, &dl_main);
253 #ifndef HP_TIMING_NONAVAIL
254 if (HP_TIMING_AVAIL)
256 hp_timing_t end_time;
258 /* Get the current time. */
259 HP_TIMING_NOW (end_time);
261 /* Compute the difference. */
262 HP_TIMING_DIFF (rtld_total_time, start_time, end_time);
264 #endif
266 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_STATISTICS, 0))
267 print_statistics ();
269 return start_addr;
272 static ElfW(Addr) __attribute_used__ internal_function
273 _dl_start (void *arg)
275 #ifdef DONT_USE_BOOTSTRAP_MAP
276 # define bootstrap_map GL(dl_rtld_map)
277 #else
278 struct dl_start_final_info info;
279 # define bootstrap_map info.l
280 #endif
282 /* This #define produces dynamic linking inline functions for
283 bootstrap relocation instead of general-purpose relocation. */
284 #define RTLD_BOOTSTRAP
285 #define RESOLVE_MAP(sym, version, flags) \
286 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : &bootstrap_map)
287 #define RESOLVE(sym, version, flags) \
288 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : bootstrap_map.l_addr)
289 #include "dynamic-link.h"
291 if (HP_TIMING_INLINE && HP_TIMING_AVAIL)
292 #ifdef DONT_USE_BOOTSTRAP_MAP
293 HP_TIMING_NOW (start_time);
294 #else
295 HP_TIMING_NOW (info.start_time);
296 #endif
298 /* Partly clean the `bootstrap_map' structure up. Don't use
299 `memset' since it might not be built in or inlined and we cannot
300 make function calls at this point. Use '__builtin_memset' if we
301 know it is available. We do not have to clear the memory if we
302 do not have to use the temporary bootstrap_map. Global variables
303 are initialized to zero by default. */
304 #ifndef DONT_USE_BOOTSTRAP_MAP
305 # ifdef HAVE_BUILTIN_MEMSET
306 __builtin_memset (bootstrap_map.l_info, '\0', sizeof (bootstrap_map.l_info));
307 # else
308 for (size_t cnt = 0;
309 cnt < sizeof (bootstrap_map.l_info) / sizeof (bootstrap_map.l_info[0]);
310 ++cnt)
311 bootstrap_map.l_info[cnt] = 0;
312 # endif
313 #endif
315 /* Figure out the run-time load address of the dynamic linker itself. */
316 bootstrap_map.l_addr = elf_machine_load_address ();
318 /* Read our own dynamic section and fill in the info array. */
319 bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
320 elf_get_dynamic_info (&bootstrap_map, NULL);
322 #if defined USE_TLS && NO_TLS_OFFSET != 0
323 bootstrap_map.l_tls_offset = NO_TLS_OFFSET;
324 #endif
326 #if USE___THREAD
327 /* Get the dynamic linker's own program header. First we need the ELF
328 file header. The `_begin' symbol created by the linker script points
329 to it. When we have something like GOTOFF relocs, we can use a plain
330 reference to find the runtime address. Without that, we have to rely
331 on the `l_addr' value, which is not the value we want when prelinked. */
332 dtv_t initdtv[3];
333 ElfW(Ehdr) *ehdr
334 # ifdef DONT_USE_BOOTSTRAP_MAP
335 = (ElfW(Ehdr) *) &_begin;
336 # else
337 = (ElfW(Ehdr) *) bootstrap_map.l_addr;
338 # endif
339 ElfW(Phdr) *phdr = (ElfW(Phdr) *) ((void *) ehdr + ehdr->e_phoff);
340 size_t cnt = ehdr->e_phnum; /* PT_TLS is usually the last phdr. */
341 while (cnt-- > 0)
342 if (phdr[cnt].p_type == PT_TLS)
344 void *tlsblock;
345 size_t max_align = MAX (TLS_INIT_TCB_ALIGN, phdr[cnt].p_align);
346 char *p;
348 bootstrap_map.l_tls_blocksize = phdr[cnt].p_memsz;
349 bootstrap_map.l_tls_align = phdr[cnt].p_align;
350 assert (bootstrap_map.l_tls_blocksize != 0);
351 bootstrap_map.l_tls_initimage_size = phdr[cnt].p_filesz;
352 bootstrap_map.l_tls_initimage = (void *) (bootstrap_map.l_addr
353 + phdr[cnt].p_vaddr);
355 /* We can now allocate the initial TLS block. This can happen
356 on the stack. We'll get the final memory later when we
357 know all about the various objects loaded at startup
358 time. */
359 # if TLS_TCB_AT_TP
360 tlsblock = alloca (roundup (bootstrap_map.l_tls_blocksize,
361 TLS_INIT_TCB_ALIGN)
362 + TLS_INIT_TCB_SIZE
363 + max_align);
364 # elif TLS_DTV_AT_TP
365 tlsblock = alloca (roundup (TLS_INIT_TCB_SIZE,
366 bootstrap_map.l_tls_align)
367 + bootstrap_map.l_tls_blocksize
368 + max_align);
369 # else
370 /* In case a model with a different layout for the TCB and DTV
371 is defined add another #elif here and in the following #ifs. */
372 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
373 # endif
374 /* Align the TLS block. */
375 tlsblock = (void *) (((uintptr_t) tlsblock + max_align - 1)
376 & ~(max_align - 1));
378 /* Initialize the dtv. [0] is the length, [1] the generation
379 counter. */
380 initdtv[0].counter = 1;
381 initdtv[1].counter = 0;
383 /* Initialize the TLS block. */
384 # if TLS_TCB_AT_TP
385 initdtv[2].pointer = tlsblock;
386 # elif TLS_DTV_AT_TP
387 bootstrap_map.l_tls_offset = roundup (TLS_INIT_TCB_SIZE,
388 bootstrap_map.l_tls_align);
389 initdtv[2].pointer = (char *) tlsblock + bootstrap_map.l_tls_offset;
390 # else
391 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
392 # endif
393 p = __mempcpy (initdtv[2].pointer, bootstrap_map.l_tls_initimage,
394 bootstrap_map.l_tls_initimage_size);
395 # ifdef HAVE_BUILTIN_MEMSET
396 __builtin_memset (p, '\0', (bootstrap_map.l_tls_blocksize
397 - bootstrap_map.l_tls_initimage_size));
398 # else
400 size_t remaining = (bootstrap_map.l_tls_blocksize
401 - bootstrap_map.l_tls_initimage_size);
402 while (remaining-- > 0)
403 *p++ = '\0';
405 #endif
407 /* Install the pointer to the dtv. */
409 /* Initialize the thread pointer. */
410 # if TLS_TCB_AT_TP
411 bootstrap_map.l_tls_offset
412 = roundup (bootstrap_map.l_tls_blocksize, TLS_INIT_TCB_ALIGN);
414 INSTALL_DTV ((char *) tlsblock + bootstrap_map.l_tls_offset,
415 initdtv);
417 const char *lossage = TLS_INIT_TP ((char *) tlsblock
418 + bootstrap_map.l_tls_offset, 0);
419 # elif TLS_DTV_AT_TP
420 INSTALL_DTV (tlsblock, initdtv);
421 const char *lossage = TLS_INIT_TP (tlsblock, 0);
422 # else
423 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
424 # endif
425 if (__builtin_expect (lossage != NULL, 0))
426 _dl_fatal_printf ("cannot set up thread-local storage: %s\n",
427 lossage);
429 /* So far this is module number one. */
430 bootstrap_map.l_tls_modid = 1;
432 /* There can only be one PT_TLS entry. */
433 break;
435 #endif /* USE___THREAD */
437 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
438 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info);
439 #endif
441 if (bootstrap_map.l_addr || ! bootstrap_map.l_info[VALIDX(DT_GNU_PRELINKED)])
443 /* Relocate ourselves so we can do normal function calls and
444 data access using the global offset table. */
446 ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0, 0);
449 /* Please note that we don't allow profiling of this object and
450 therefore need not test whether we have to allocate the array
451 for the relocation results (as done in dl-reloc.c). */
453 /* Now life is sane; we can call functions and access global data.
454 Set up to use the operating system facilities, and find out from
455 the operating system's program loader where to find the program
456 header table in core. Put the rest of _dl_start into a separate
457 function, that way the compiler cannot put accesses to the GOT
458 before ELF_DYNAMIC_RELOCATE. */
460 #ifdef DONT_USE_BOOTSTRAP_MAP
461 ElfW(Addr) entry = _dl_start_final (arg);
462 #else
463 ElfW(Addr) entry = _dl_start_final (arg, &info);
464 #endif
466 #ifndef ELF_MACHINE_START_ADDRESS
467 # define ELF_MACHINE_START_ADDRESS(map, start) (start)
468 #endif
470 return ELF_MACHINE_START_ADDRESS (GL(dl_loaded), entry);
476 /* Now life is peachy; we can do all normal operations.
477 On to the real work. */
479 /* Some helper functions. */
481 /* Arguments to relocate_doit. */
482 struct relocate_args
484 struct link_map *l;
485 int lazy;
488 struct map_args
490 /* Argument to map_doit. */
491 char *str;
492 /* Return value of map_doit. */
493 struct link_map *main_map;
496 /* Arguments to version_check_doit. */
497 struct version_check_args
499 int doexit;
500 int dotrace;
503 static void
504 relocate_doit (void *a)
506 struct relocate_args *args = (struct relocate_args *) a;
508 INTUSE(_dl_relocate_object) (args->l, args->l->l_scope, args->lazy, 0);
511 static void
512 map_doit (void *a)
514 struct map_args *args = (struct map_args *) a;
515 args->main_map = INTUSE(_dl_map_object) (NULL, args->str, 0, lt_library, 0, 0);
518 static void
519 version_check_doit (void *a)
521 struct version_check_args *args = (struct version_check_args *) a;
522 if (_dl_check_all_versions (GL(dl_loaded), 1, args->dotrace) && args->doexit)
523 /* We cannot start the application. Abort now. */
524 _exit (1);
528 static inline struct link_map *
529 find_needed (const char *name)
531 unsigned int n = GL(dl_loaded)->l_searchlist.r_nlist;
533 while (n-- > 0)
534 if (_dl_name_match_p (name, GL(dl_loaded)->l_searchlist.r_list[n]))
535 return GL(dl_loaded)->l_searchlist.r_list[n];
537 /* Should never happen. */
538 return NULL;
541 static int
542 match_version (const char *string, struct link_map *map)
544 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
545 ElfW(Verdef) *def;
547 #define VERDEFTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
548 if (map->l_info[VERDEFTAG] == NULL)
549 /* The file has no symbol versioning. */
550 return 0;
552 def = (ElfW(Verdef) *) ((char *) map->l_addr
553 + map->l_info[VERDEFTAG]->d_un.d_ptr);
554 while (1)
556 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
558 /* Compare the version strings. */
559 if (strcmp (string, strtab + aux->vda_name) == 0)
560 /* Bingo! */
561 return 1;
563 /* If no more definitions we failed to find what we want. */
564 if (def->vd_next == 0)
565 break;
567 /* Next definition. */
568 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
571 return 0;
574 #ifdef _LIBC_REENTRANT
575 /* _dl_error_catch_tsd points to this for the single-threaded case.
576 It's reset by the thread library for multithreaded programs. */
577 void ** __attribute__ ((const))
578 _dl_initial_error_catch_tsd (void)
580 static void *data;
581 return &data;
583 #endif
585 static const char *library_path; /* The library search path. */
586 static const char *preloadlist; /* The list preloaded objects. */
587 static int version_info; /* Nonzero if information about
588 versions has to be printed. */
590 static void
591 dl_main (const ElfW(Phdr) *phdr,
592 ElfW(Word) phnum,
593 ElfW(Addr) *user_entry)
595 const ElfW(Phdr) *ph;
596 enum mode mode;
597 struct link_map **preloads;
598 unsigned int npreloads;
599 size_t file_size;
600 char *file;
601 bool has_interp = false;
602 unsigned int i;
603 bool prelinked = false;
604 bool rtld_is_main = false;
605 #ifndef HP_TIMING_NONAVAIL
606 hp_timing_t start;
607 hp_timing_t stop;
608 hp_timing_t diff;
609 #endif
610 #ifdef USE_TLS
611 void *tcbp;
612 #endif
614 #ifdef _LIBC_REENTRANT
615 /* Explicit initialization since the reloc would just be more work. */
616 GL(dl_error_catch_tsd) = &_dl_initial_error_catch_tsd;
617 #endif
619 /* Process the environment variable which control the behaviour. */
620 process_envvars (&mode);
622 /* Set up a flag which tells we are just starting. */
623 INTUSE(_dl_starting_up) = 1;
625 if (*user_entry == (ElfW(Addr)) ENTRY_POINT)
627 /* Ho ho. We are not the program interpreter! We are the program
628 itself! This means someone ran ld.so as a command. Well, that
629 might be convenient to do sometimes. We support it by
630 interpreting the args like this:
632 ld.so PROGRAM ARGS...
634 The first argument is the name of a file containing an ELF
635 executable we will load and run with the following arguments.
636 To simplify life here, PROGRAM is searched for using the
637 normal rules for shared objects, rather than $PATH or anything
638 like that. We just load it and use its entry point; we don't
639 pay attention to its PT_INTERP command (we are the interpreter
640 ourselves). This is an easy way to test a new ld.so before
641 installing it. */
642 rtld_is_main = true;
644 /* Note the place where the dynamic linker actually came from. */
645 GL(dl_rtld_map).l_name = rtld_progname;
647 while (_dl_argc > 1)
648 if (! strcmp (INTUSE(_dl_argv)[1], "--list"))
650 mode = list;
651 GL(dl_lazy) = -1; /* This means do no dependency analysis. */
653 ++_dl_skip_args;
654 --_dl_argc;
655 ++INTUSE(_dl_argv);
657 else if (! strcmp (INTUSE(_dl_argv)[1], "--verify"))
659 mode = verify;
661 ++_dl_skip_args;
662 --_dl_argc;
663 ++INTUSE(_dl_argv);
665 else if (! strcmp (INTUSE(_dl_argv)[1], "--library-path")
666 && _dl_argc > 2)
668 library_path = INTUSE(_dl_argv)[2];
670 _dl_skip_args += 2;
671 _dl_argc -= 2;
672 INTUSE(_dl_argv) += 2;
674 else if (! strcmp (INTUSE(_dl_argv)[1], "--inhibit-rpath")
675 && _dl_argc > 2)
677 GL(dl_inhibit_rpath) = INTUSE(_dl_argv)[2];
679 _dl_skip_args += 2;
680 _dl_argc -= 2;
681 INTUSE(_dl_argv) += 2;
683 else
684 break;
686 /* If we have no further argument the program was called incorrectly.
687 Grant the user some education. */
688 if (_dl_argc < 2)
689 _dl_fatal_printf ("\
690 Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
691 You have invoked `ld.so', the helper program for shared library executables.\n\
692 This program usually lives in the file `/lib/ld.so', and special directives\n\
693 in executable files using ELF shared libraries tell the system's program\n\
694 loader to load the helper program from this file. This helper program loads\n\
695 the shared libraries needed by the program executable, prepares the program\n\
696 to run, and runs it. You may invoke this helper program directly from the\n\
697 command line to load and run an ELF executable file; this is like executing\n\
698 that file itself, but always uses this helper program from the file you\n\
699 specified, instead of the helper program file specified in the executable\n\
700 file you run. This is mostly of use for maintainers to test new versions\n\
701 of this helper program; chances are you did not intend to run this program.\n\
703 --list list all dependencies and how they are resolved\n\
704 --verify verify that given object really is a dynamically linked\n\
705 object we can handle\n\
706 --library-path PATH use given PATH instead of content of the environment\n\
707 variable LD_LIBRARY_PATH\n\
708 --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\
709 in LIST\n");
711 ++_dl_skip_args;
712 --_dl_argc;
713 ++INTUSE(_dl_argv);
715 /* Initialize the data structures for the search paths for shared
716 objects. */
717 _dl_init_paths (library_path);
719 if (__builtin_expect (mode, normal) == verify)
721 const char *objname;
722 const char *err_str = NULL;
723 struct map_args args;
725 args.str = rtld_progname;
726 (void) INTUSE(_dl_catch_error) (&objname, &err_str, map_doit, &args);
727 if (__builtin_expect (err_str != NULL, 0))
728 /* We don't free the returned string, the programs stops
729 anyway. */
730 _exit (EXIT_FAILURE);
732 else
734 HP_TIMING_NOW (start);
735 INTUSE(_dl_map_object) (NULL, rtld_progname, 0, lt_library, 0, 0);
736 HP_TIMING_NOW (stop);
738 HP_TIMING_DIFF (load_time, start, stop);
741 phdr = GL(dl_loaded)->l_phdr;
742 phnum = GL(dl_loaded)->l_phnum;
743 /* We overwrite here a pointer to a malloc()ed string. But since
744 the malloc() implementation used at this point is the dummy
745 implementations which has no real free() function it does not
746 makes sense to free the old string first. */
747 GL(dl_loaded)->l_name = (char *) "";
748 *user_entry = GL(dl_loaded)->l_entry;
750 else
752 /* Create a link_map for the executable itself.
753 This will be what dlopen on "" returns. */
754 _dl_new_object ((char *) "", "", lt_executable, NULL);
755 if (GL(dl_loaded) == NULL)
756 _dl_fatal_printf ("cannot allocate memory for link map\n");
757 GL(dl_loaded)->l_phdr = phdr;
758 GL(dl_loaded)->l_phnum = phnum;
759 GL(dl_loaded)->l_entry = *user_entry;
761 /* At this point we are in a bit of trouble. We would have to
762 fill in the values for l_dev and l_ino. But in general we
763 do not know where the file is. We also do not handle AT_EXECFD
764 even if it would be passed up.
766 We leave the values here defined to 0. This is normally no
767 problem as the program code itself is normally no shared
768 object and therefore cannot be loaded dynamically. Nothing
769 prevent the use of dynamic binaries and in these situations
770 we might get problems. We might not be able to find out
771 whether the object is already loaded. But since there is no
772 easy way out and because the dynamic binary must also not
773 have an SONAME we ignore this program for now. If it becomes
774 a problem we can force people using SONAMEs. */
776 /* We delay initializing the path structure until we got the dynamic
777 information for the program. */
780 GL(dl_loaded)->l_map_end = 0;
781 /* Perhaps the executable has no PT_LOAD header entries at all. */
782 GL(dl_loaded)->l_map_start = ~0;
783 /* We opened the file, account for it. */
784 ++GL(dl_loaded)->l_opencount;
786 /* Scan the program header table for the dynamic section. */
787 for (ph = phdr; ph < &phdr[phnum]; ++ph)
788 switch (ph->p_type)
790 case PT_PHDR:
791 /* Find out the load address. */
792 GL(dl_loaded)->l_addr = (ElfW(Addr)) phdr - ph->p_vaddr;
793 break;
794 case PT_DYNAMIC:
795 /* This tells us where to find the dynamic section,
796 which tells us everything we need to do. */
797 GL(dl_loaded)->l_ld = (void *) GL(dl_loaded)->l_addr + ph->p_vaddr;
798 break;
799 case PT_INTERP:
800 /* This "interpreter segment" was used by the program loader to
801 find the program interpreter, which is this program itself, the
802 dynamic linker. We note what name finds us, so that a future
803 dlopen call or DT_NEEDED entry, for something that wants to link
804 against the dynamic linker as a shared library, will know that
805 the shared object is already loaded. */
806 _dl_rtld_libname.name = ((const char *) GL(dl_loaded)->l_addr
807 + ph->p_vaddr);
808 /* _dl_rtld_libname.next = NULL; Already zero. */
809 GL(dl_rtld_map).l_libname = &_dl_rtld_libname;
811 /* Ordinarilly, we would get additional names for the loader from
812 our DT_SONAME. This can't happen if we were actually linked as
813 a static executable (detect this case when we have no DYNAMIC).
814 If so, assume the filename component of the interpreter path to
815 be our SONAME, and add it to our name list. */
816 if (GL(dl_rtld_map).l_ld == NULL)
818 const char *p = NULL;
819 const char *cp = _dl_rtld_libname.name;
821 /* Find the filename part of the path. */
822 while (*cp != '\0')
823 if (*cp++ == '/')
824 p = cp;
826 if (p != NULL)
828 _dl_rtld_libname2.name = p;
829 /* _dl_rtld_libname2.next = NULL; Already zero. */
830 _dl_rtld_libname.next = &_dl_rtld_libname2;
834 has_interp = true;
835 break;
836 case PT_LOAD:
838 ElfW(Addr) mapstart;
839 ElfW(Addr) allocend;
841 /* Remember where the main program starts in memory. */
842 mapstart = (GL(dl_loaded)->l_addr
843 + (ph->p_vaddr & ~(ph->p_align - 1)));
844 if (GL(dl_loaded)->l_map_start > mapstart)
845 GL(dl_loaded)->l_map_start = mapstart;
847 /* Also where it ends. */
848 allocend = GL(dl_loaded)->l_addr + ph->p_vaddr + ph->p_memsz;
849 if (GL(dl_loaded)->l_map_end < allocend)
850 GL(dl_loaded)->l_map_end = allocend;
852 break;
853 #ifdef USE_TLS
854 case PT_TLS:
855 if (ph->p_memsz > 0)
857 /* Note that in the case the dynamic linker we duplicate work
858 here since we read the PT_TLS entry already in
859 _dl_start_final. But the result is repeatable so do not
860 check for this special but unimportant case. */
861 GL(dl_loaded)->l_tls_blocksize = ph->p_memsz;
862 GL(dl_loaded)->l_tls_align = ph->p_align;
863 GL(dl_loaded)->l_tls_initimage_size = ph->p_filesz;
864 GL(dl_loaded)->l_tls_initimage = (void *) ph->p_vaddr;
866 /* This image gets the ID one. */
867 GL(dl_tls_max_dtv_idx) = GL(dl_loaded)->l_tls_modid = 1;
869 break;
870 #endif
872 #ifdef USE_TLS
873 /* Adjust the address of the TLS initialization image in case
874 the executable is actually an ET_DYN object. */
875 if (GL(dl_loaded)->l_tls_initimage != NULL)
876 GL(dl_loaded)->l_tls_initimage
877 = (char *) GL(dl_loaded)->l_tls_initimage + GL(dl_loaded)->l_addr;
878 #endif
879 if (! GL(dl_loaded)->l_map_end)
880 GL(dl_loaded)->l_map_end = ~0;
881 if (! GL(dl_rtld_map).l_libname && GL(dl_rtld_map).l_name)
883 /* We were invoked directly, so the program might not have a
884 PT_INTERP. */
885 _dl_rtld_libname.name = GL(dl_rtld_map).l_name;
886 /* _dl_rtld_libname.next = NULL; Already zero. */
887 GL(dl_rtld_map).l_libname = &_dl_rtld_libname;
889 else
890 assert (GL(dl_rtld_map).l_libname); /* How else did we get here? */
892 if (! rtld_is_main)
894 /* Extract the contents of the dynamic section for easy access. */
895 elf_get_dynamic_info (GL(dl_loaded), NULL);
896 if (GL(dl_loaded)->l_info[DT_HASH])
897 /* Set up our cache of pointers into the hash table. */
898 _dl_setup_hash (GL(dl_loaded));
901 if (__builtin_expect (mode, normal) == verify)
903 /* We were called just to verify that this is a dynamic
904 executable using us as the program interpreter. Exit with an
905 error if we were not able to load the binary or no interpreter
906 is specified (i.e., this is no dynamically linked binary. */
907 if (GL(dl_loaded)->l_ld == NULL)
908 _exit (1);
910 /* We allow here some platform specific code. */
911 #ifdef DISTINGUISH_LIB_VERSIONS
912 DISTINGUISH_LIB_VERSIONS;
913 #endif
914 _exit (has_interp ? 0 : 2);
917 if (! rtld_is_main)
918 /* Initialize the data structures for the search paths for shared
919 objects. */
920 _dl_init_paths (library_path);
922 /* Put the link_map for ourselves on the chain so it can be found by
923 name. Note that at this point the global chain of link maps contains
924 exactly one element, which is pointed to by dl_loaded. */
925 if (! GL(dl_rtld_map).l_name)
926 /* If not invoked directly, the dynamic linker shared object file was
927 found by the PT_INTERP name. */
928 GL(dl_rtld_map).l_name = (char *) GL(dl_rtld_map).l_libname->name;
929 GL(dl_rtld_map).l_type = lt_library;
930 GL(dl_loaded)->l_next = &GL(dl_rtld_map);
931 GL(dl_rtld_map).l_prev = GL(dl_loaded);
932 ++GL(dl_nloaded);
934 /* Set up the program header information for the dynamic linker
935 itself. It is needed in the dl_iterate_phdr() callbacks. */
936 ElfW(Ehdr) *rtld_ehdr = (ElfW(Ehdr) *) GL(dl_rtld_map).l_map_start;
937 GL(dl_rtld_map).l_phdr = (ElfW(Phdr) *) (GL(dl_rtld_map).l_map_start
938 + rtld_ehdr->e_phoff);
939 GL(dl_rtld_map).l_phnum = rtld_ehdr->e_phnum;
941 /* We have two ways to specify objects to preload: via environment
942 variable and via the file /etc/ld.so.preload. The latter can also
943 be used when security is enabled. */
944 preloads = NULL;
945 npreloads = 0;
947 if (__builtin_expect (preloadlist != NULL, 0))
949 /* The LD_PRELOAD environment variable gives list of libraries
950 separated by white space or colons that are loaded before the
951 executable's dependencies and prepended to the global scope
952 list. If the binary is running setuid all elements
953 containing a '/' are ignored since it is insecure. */
954 char *list = strdupa (preloadlist);
955 char *p;
957 HP_TIMING_NOW (start);
959 /* Prevent optimizing strsep. Speed is not important here. */
960 while ((p = (strsep) (&list, " :")) != NULL)
961 if (p[0] != '\0'
962 && (__builtin_expect (! INTUSE(__libc_enable_secure), 1)
963 || strchr (p, '/') == NULL))
965 struct link_map *new_map = INTUSE(_dl_map_object) (GL(dl_loaded),
966 p, 1,
967 lt_library,
968 0, 0);
969 if (++new_map->l_opencount == 1)
970 /* It is no duplicate. */
971 ++npreloads;
974 HP_TIMING_NOW (stop);
975 HP_TIMING_DIFF (diff, start, stop);
976 HP_TIMING_ACCUM_NT (load_time, diff);
979 /* Read the contents of the file. */
980 file = _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size,
981 PROT_READ | PROT_WRITE);
982 if (__builtin_expect (file != MAP_FAILED, 0))
984 /* Parse the file. It contains names of libraries to be loaded,
985 separated by white spaces or `:'. It may also contain
986 comments introduced by `#'. */
987 char *problem;
988 char *runp;
989 size_t rest;
991 /* Eliminate comments. */
992 runp = file;
993 rest = file_size;
994 while (rest > 0)
996 char *comment = memchr (runp, '#', rest);
997 if (comment == NULL)
998 break;
1000 rest -= comment - runp;
1002 *comment = ' ';
1003 while (--rest > 0 && *++comment != '\n');
1006 /* We have one problematic case: if we have a name at the end of
1007 the file without a trailing terminating characters, we cannot
1008 place the \0. Handle the case separately. */
1009 if (file[file_size - 1] != ' ' && file[file_size - 1] != '\t'
1010 && file[file_size - 1] != '\n' && file[file_size - 1] != ':')
1012 problem = &file[file_size];
1013 while (problem > file && problem[-1] != ' ' && problem[-1] != '\t'
1014 && problem[-1] != '\n' && problem[-1] != ':')
1015 --problem;
1017 if (problem > file)
1018 problem[-1] = '\0';
1020 else
1022 problem = NULL;
1023 file[file_size - 1] = '\0';
1026 HP_TIMING_NOW (start);
1028 if (file != problem)
1030 char *p;
1031 runp = file;
1032 while ((p = strsep (&runp, ": \t\n")) != NULL)
1033 if (p[0] != '\0')
1035 struct link_map *new_map = INTUSE(_dl_map_object) (GL(dl_loaded),
1036 p, 1,
1037 lt_library,
1038 0, 0);
1039 if (++new_map->l_opencount == 1)
1040 /* It is no duplicate. */
1041 ++npreloads;
1045 if (problem != NULL)
1047 char *p = strndupa (problem, file_size - (problem - file));
1048 struct link_map *new_map = INTUSE(_dl_map_object) (GL(dl_loaded), p,
1049 1, lt_library,
1050 0, 0);
1051 if (++new_map->l_opencount == 1)
1052 /* It is no duplicate. */
1053 ++npreloads;
1056 HP_TIMING_NOW (stop);
1057 HP_TIMING_DIFF (diff, start, stop);
1058 HP_TIMING_ACCUM_NT (load_time, diff);
1060 /* We don't need the file anymore. */
1061 __munmap (file, file_size);
1064 if (__builtin_expect (npreloads, 0) != 0)
1066 /* Set up PRELOADS with a vector of the preloaded libraries. */
1067 struct link_map *l;
1068 preloads = __alloca (npreloads * sizeof preloads[0]);
1069 l = GL(dl_rtld_map).l_next; /* End of the chain before preloads. */
1070 i = 0;
1073 preloads[i++] = l;
1074 l = l->l_next;
1075 } while (l);
1076 assert (i == npreloads);
1079 #ifdef NEED_DL_SYSINFO
1080 if (GL(dl_sysinfo_dso) != NULL)
1082 /* We have a prelinked DSO preloaded by the system. */
1083 GL(dl_sysinfo) = GL(dl_sysinfo_dso)->e_entry;
1085 /* Do an abridged version of the work _dl_map_object_from_fd would do
1086 to map in the object. It's already mapped and prelinked (and
1087 better be, since it's read-only and so we couldn't relocate it).
1088 We just want our data structures to describe it as if we had just
1089 mapped and relocated it normally. */
1090 struct link_map *l = _dl_new_object ((char *) "", "", lt_library, NULL);
1091 if (__builtin_expect (l != NULL, 1))
1093 static ElfW(Dyn) dyn_temp [DL_RO_DYN_TEMP_CNT];
1095 l->l_phdr = ((const void *) GL(dl_sysinfo_dso)
1096 + GL(dl_sysinfo_dso)->e_phoff);
1097 l->l_phnum = GL(dl_sysinfo_dso)->e_phnum;
1098 for (uint_fast16_t i = 0; i < l->l_phnum; ++i)
1100 const ElfW(Phdr) *const ph = &l->l_phdr[i];
1101 if (ph->p_type == PT_DYNAMIC)
1103 l->l_ld = (void *) ph->p_vaddr;
1104 l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn));
1105 break;
1107 if (ph->p_type == PT_LOAD)
1108 assert ((void *) ph->p_vaddr == GL(dl_sysinfo_dso));
1110 elf_get_dynamic_info (l, dyn_temp);
1111 _dl_setup_hash (l);
1112 l->l_relocated = 1;
1114 /* Now that we have the info handy, use the DSO image's soname
1115 so this object can be looked up by name. */
1116 if (l->l_info[DT_SONAME] != NULL)
1117 l->l_libname->name = ((char *) D_PTR (l, l_info[DT_STRTAB])
1118 + l->l_info[DT_SONAME]->d_un.d_val);
1121 #endif
1123 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
1124 specified some libraries to load, these are inserted before the actual
1125 dependencies in the executable's searchlist for symbol resolution. */
1126 HP_TIMING_NOW (start);
1127 INTUSE(_dl_map_object_deps) (GL(dl_loaded), preloads, npreloads,
1128 mode == trace, 0);
1129 HP_TIMING_NOW (stop);
1130 HP_TIMING_DIFF (diff, start, stop);
1131 HP_TIMING_ACCUM_NT (load_time, diff);
1133 /* Mark all objects as being in the global scope and set the open
1134 counter. */
1135 for (i = GL(dl_loaded)->l_searchlist.r_nlist; i > 0; )
1137 --i;
1138 GL(dl_loaded)->l_searchlist.r_list[i]->l_global = 1;
1139 ++GL(dl_loaded)->l_searchlist.r_list[i]->l_opencount;
1142 #ifndef MAP_ANON
1143 /* We are done mapping things, so close the zero-fill descriptor. */
1144 __close (_dl_zerofd);
1145 _dl_zerofd = -1;
1146 #endif
1148 /* Remove _dl_rtld_map from the chain. */
1149 GL(dl_rtld_map).l_prev->l_next = GL(dl_rtld_map).l_next;
1150 if (GL(dl_rtld_map).l_next)
1151 GL(dl_rtld_map).l_next->l_prev = GL(dl_rtld_map).l_prev;
1153 if (__builtin_expect (GL(dl_rtld_map).l_opencount > 1, 1))
1155 /* Some DT_NEEDED entry referred to the interpreter object itself, so
1156 put it back in the list of visible objects. We insert it into the
1157 chain in symbol search order because gdb uses the chain's order as
1158 its symbol search order. */
1159 i = 1;
1160 while (GL(dl_loaded)->l_searchlist.r_list[i] != &GL(dl_rtld_map))
1161 ++i;
1162 GL(dl_rtld_map).l_prev = GL(dl_loaded)->l_searchlist.r_list[i - 1];
1163 if (__builtin_expect (mode, normal) == normal)
1164 GL(dl_rtld_map).l_next = (i + 1 < GL(dl_loaded)->l_searchlist.r_nlist
1165 ? GL(dl_loaded)->l_searchlist.r_list[i + 1]
1166 : NULL);
1167 else
1168 /* In trace mode there might be an invisible object (which we
1169 could not find) after the previous one in the search list.
1170 In this case it doesn't matter much where we put the
1171 interpreter object, so we just initialize the list pointer so
1172 that the assertion below holds. */
1173 GL(dl_rtld_map).l_next = GL(dl_rtld_map).l_prev->l_next;
1175 assert (GL(dl_rtld_map).l_prev->l_next == GL(dl_rtld_map).l_next);
1176 GL(dl_rtld_map).l_prev->l_next = &GL(dl_rtld_map);
1177 if (GL(dl_rtld_map).l_next != NULL)
1179 assert (GL(dl_rtld_map).l_next->l_prev == GL(dl_rtld_map).l_prev);
1180 GL(dl_rtld_map).l_next->l_prev = &GL(dl_rtld_map);
1184 /* Now let us see whether all libraries are available in the
1185 versions we need. */
1187 struct version_check_args args;
1188 args.doexit = mode == normal;
1189 args.dotrace = mode == trace;
1190 _dl_receive_error (print_missing_version, version_check_doit, &args);
1193 #ifdef USE_TLS
1194 /* Now it is time to determine the layout of the static TLS block
1195 and allocate it for the initial thread. Note that we always
1196 allocate the static block, we never defer it even if no
1197 DF_STATIC_TLS bit is set. The reason is that we know glibc will
1198 use the static model. First add the dynamic linker to the list
1199 if it also uses TLS. */
1200 if (GL(dl_rtld_map).l_tls_blocksize != 0)
1201 /* Assign a module ID. */
1202 GL(dl_rtld_map).l_tls_modid = _dl_next_tls_modid ();
1204 # ifndef TLS_INIT_TP_EXPENSIVE
1205 # define TLS_INIT_TP_EXPENSIVE 0
1206 # endif
1208 /* We do not initialize any of the TLS functionality unless any of the
1209 initial modules uses TLS. This makes dynamic loading of modules with
1210 TLS impossible, but to support it requires either eagerly doing setup
1211 now or lazily doing it later. Doing it now makes us incompatible with
1212 an old kernel that can't perform TLS_INIT_TP, even if no TLS is ever
1213 used. Trying to do it lazily is too hairy to try when there could be
1214 multiple threads (from a non-TLS-using libpthread). */
1215 if (GL(dl_tls_max_dtv_idx) > 0 || !TLS_INIT_TP_EXPENSIVE)
1217 struct link_map *l;
1218 size_t nelem;
1219 struct dtv_slotinfo *slotinfo;
1221 /* Number of elements in the static TLS block. */
1222 GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx);
1224 /* Allocate the array which contains the information about the
1225 dtv slots. We allocate a few entries more than needed to
1226 avoid the need for reallocation. */
1227 nelem = GL(dl_tls_max_dtv_idx) + 1 + TLS_SLOTINFO_SURPLUS;
1229 /* Allocate. */
1230 GL(dl_tls_dtv_slotinfo_list) = (struct dtv_slotinfo_list *)
1231 malloc (sizeof (struct dtv_slotinfo_list)
1232 + nelem * sizeof (struct dtv_slotinfo));
1233 /* No need to check the return value. If memory allocation failed
1234 the program would have been terminated. */
1236 slotinfo = memset (GL(dl_tls_dtv_slotinfo_list)->slotinfo, '\0',
1237 nelem * sizeof (struct dtv_slotinfo));
1238 GL(dl_tls_dtv_slotinfo_list)->len = nelem;
1239 GL(dl_tls_dtv_slotinfo_list)->next = NULL;
1241 /* Fill in the information from the loaded modules. */
1242 for (l = GL(dl_loaded), i = 0; l != NULL; l = l->l_next)
1243 if (l->l_tls_blocksize != 0)
1244 /* This is a module with TLS data. Store the map reference.
1245 The generation counter is zero. */
1246 slotinfo[++i].map = l;
1247 assert (i == GL(dl_tls_max_dtv_idx));
1249 /* Compute the TLS offsets for the various blocks. */
1250 _dl_determine_tlsoffset ();
1252 /* Construct the static TLS block and the dtv for the initial
1253 thread. For some platforms this will include allocating memory
1254 for the thread descriptor. The memory for the TLS block will
1255 never be freed. It should be allocated accordingly. The dtv
1256 array can be changed if dynamic loading requires it. */
1257 tcbp = _dl_allocate_tls_storage ();
1258 if (tcbp == NULL)
1259 _dl_fatal_printf ("\
1260 cannot allocate TLS data structures for initial thread");
1262 /* Store for detection of the special case by __tls_get_addr
1263 so it knows not to pass this dtv to the normal realloc. */
1264 GL(dl_initial_dtv) = GET_DTV (tcbp);
1266 #endif
1268 if (__builtin_expect (mode, normal) != normal)
1270 /* We were run just to list the shared libraries. It is
1271 important that we do this before real relocation, because the
1272 functions we call below for output may no longer work properly
1273 after relocation. */
1274 struct link_map *l;
1276 if (GL(dl_debug_mask) & DL_DEBUG_PRELINK)
1278 struct r_scope_elem *scope = &GL(dl_loaded)->l_searchlist;
1280 for (i = 0; i < scope->r_nlist; i++)
1282 l = scope->r_list [i];
1283 if (l->l_faked)
1285 _dl_printf ("\t%s => not found\n", l->l_libname->name);
1286 continue;
1288 if (_dl_name_match_p (GL(dl_trace_prelink), l))
1289 GL(dl_trace_prelink_map) = l;
1290 _dl_printf ("\t%s => %s (0x%0*Zx, 0x%0*Zx)",
1291 l->l_libname->name[0] ? l->l_libname->name
1292 : rtld_progname ?: "<main program>",
1293 l->l_name[0] ? l->l_name
1294 : rtld_progname ?: "<main program>",
1295 (int) sizeof l->l_map_start * 2, l->l_map_start,
1296 (int) sizeof l->l_addr * 2, l->l_addr);
1297 #ifdef USE_TLS
1298 if (l->l_tls_modid)
1299 _dl_printf (" TLS(0x%Zx, 0x%0*Zx)\n", l->l_tls_modid,
1300 (int) sizeof l->l_tls_offset * 2,
1301 l->l_tls_offset);
1302 else
1303 #endif
1304 _dl_printf ("\n");
1307 else if (! GL(dl_loaded)->l_info[DT_NEEDED])
1308 _dl_printf ("\tstatically linked\n");
1309 else
1311 for (l = GL(dl_loaded)->l_next; l; l = l->l_next)
1312 if (l->l_faked)
1313 /* The library was not found. */
1314 _dl_printf ("\t%s => not found\n", l->l_libname->name);
1315 else
1316 _dl_printf ("\t%s => %s (0x%0*Zx)\n", l->l_libname->name,
1317 l->l_name, (int) sizeof l->l_map_start * 2,
1318 l->l_map_start);
1321 if (__builtin_expect (mode, trace) != trace)
1322 for (i = 1; i < (unsigned int) _dl_argc; ++i)
1324 const ElfW(Sym) *ref = NULL;
1325 ElfW(Addr) loadbase;
1326 lookup_t result;
1328 result = INTUSE(_dl_lookup_symbol) (INTUSE(_dl_argv)[i],
1329 GL(dl_loaded),
1330 &ref, GL(dl_loaded)->l_scope,
1331 ELF_RTYPE_CLASS_PLT, 1);
1333 loadbase = LOOKUP_VALUE_ADDRESS (result);
1335 _dl_printf ("%s found at 0x%0*Zd in object at 0x%0*Zd\n",
1336 INTUSE(_dl_argv)[i],
1337 (int) sizeof ref->st_value * 2, ref->st_value,
1338 (int) sizeof loadbase * 2, loadbase);
1340 else
1342 /* If LD_WARN is set warn about undefined symbols. */
1343 if (GL(dl_lazy) >= 0 && GL(dl_verbose))
1345 /* We have to do symbol dependency testing. */
1346 struct relocate_args args;
1347 struct link_map *l;
1349 args.lazy = GL(dl_lazy);
1351 l = GL(dl_loaded);
1352 while (l->l_next)
1353 l = l->l_next;
1356 if (l != &GL(dl_rtld_map) && ! l->l_faked)
1358 args.l = l;
1359 _dl_receive_error (print_unresolved, relocate_doit,
1360 &args);
1362 l = l->l_prev;
1363 } while (l);
1365 if ((GL(dl_debug_mask) & DL_DEBUG_PRELINK)
1366 && GL(dl_rtld_map).l_opencount > 1)
1367 INTUSE(_dl_relocate_object) (&GL(dl_rtld_map),
1368 GL(dl_loaded)->l_scope, 0, 0);
1371 #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
1372 if (version_info)
1374 /* Print more information. This means here, print information
1375 about the versions needed. */
1376 int first = 1;
1377 struct link_map *map = GL(dl_loaded);
1379 for (map = GL(dl_loaded); map != NULL; map = map->l_next)
1381 const char *strtab;
1382 ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG];
1383 ElfW(Verneed) *ent;
1385 if (dyn == NULL)
1386 continue;
1388 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
1389 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
1391 if (first)
1393 _dl_printf ("\n\tVersion information:\n");
1394 first = 0;
1397 _dl_printf ("\t%s:\n",
1398 map->l_name[0] ? map->l_name : rtld_progname);
1400 while (1)
1402 ElfW(Vernaux) *aux;
1403 struct link_map *needed;
1405 needed = find_needed (strtab + ent->vn_file);
1406 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
1408 while (1)
1410 const char *fname = NULL;
1412 if (needed != NULL
1413 && match_version (strtab + aux->vna_name,
1414 needed))
1415 fname = needed->l_name;
1417 _dl_printf ("\t\t%s (%s) %s=> %s\n",
1418 strtab + ent->vn_file,
1419 strtab + aux->vna_name,
1420 aux->vna_flags & VER_FLG_WEAK
1421 ? "[WEAK] " : "",
1422 fname ?: "not found");
1424 if (aux->vna_next == 0)
1425 /* No more symbols. */
1426 break;
1428 /* Next symbol. */
1429 aux = (ElfW(Vernaux) *) ((char *) aux
1430 + aux->vna_next);
1433 if (ent->vn_next == 0)
1434 /* No more dependencies. */
1435 break;
1437 /* Next dependency. */
1438 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
1444 _exit (0);
1447 if (GL(dl_loaded)->l_info [ADDRIDX (DT_GNU_LIBLIST)]
1448 && ! __builtin_expect (GL(dl_profile) != NULL, 0))
1450 ElfW(Lib) *liblist, *liblistend;
1451 struct link_map **r_list, **r_listend, *l;
1452 const char *strtab = (const void *) D_PTR (GL(dl_loaded),
1453 l_info[DT_STRTAB]);
1455 assert (GL(dl_loaded)->l_info [VALIDX (DT_GNU_LIBLISTSZ)] != NULL);
1456 liblist = (ElfW(Lib) *)
1457 GL(dl_loaded)->l_info [ADDRIDX (DT_GNU_LIBLIST)]->d_un.d_ptr;
1458 liblistend = (ElfW(Lib) *)
1459 ((char *) liblist
1460 + GL(dl_loaded)->l_info [VALIDX (DT_GNU_LIBLISTSZ)]->d_un.d_val);
1461 r_list = GL(dl_loaded)->l_searchlist.r_list;
1462 r_listend = r_list + GL(dl_loaded)->l_searchlist.r_nlist;
1464 for (; r_list < r_listend && liblist < liblistend; r_list++)
1466 l = *r_list;
1468 if (l == GL(dl_loaded))
1469 continue;
1471 /* If the library is not mapped where it should, fail. */
1472 if (l->l_addr)
1473 break;
1475 /* Next, check if checksum matches. */
1476 if (l->l_info [VALIDX(DT_CHECKSUM)] == NULL
1477 || l->l_info [VALIDX(DT_CHECKSUM)]->d_un.d_val
1478 != liblist->l_checksum)
1479 break;
1481 if (l->l_info [VALIDX(DT_GNU_PRELINKED)] == NULL
1482 || l->l_info [VALIDX(DT_GNU_PRELINKED)]->d_un.d_val
1483 != liblist->l_time_stamp)
1484 break;
1486 if (! _dl_name_match_p (strtab + liblist->l_name, l))
1487 break;
1489 ++liblist;
1493 if (r_list == r_listend && liblist == liblistend)
1494 prelinked = true;
1496 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_LIBS, 0))
1497 _dl_printf ("\nprelink checking: %s\n", prelinked ? "ok" : "failed");
1500 if (prelinked)
1502 struct link_map *l;
1504 if (GL(dl_loaded)->l_info [ADDRIDX (DT_GNU_CONFLICT)] != NULL)
1506 ElfW(Rela) *conflict, *conflictend;
1507 #ifndef HP_TIMING_NONAVAIL
1508 hp_timing_t start;
1509 hp_timing_t stop;
1510 #endif
1512 HP_TIMING_NOW (start);
1513 assert (GL(dl_loaded)->l_info [VALIDX (DT_GNU_CONFLICTSZ)] != NULL);
1514 conflict = (ElfW(Rela) *)
1515 GL(dl_loaded)->l_info [ADDRIDX (DT_GNU_CONFLICT)]->d_un.d_ptr;
1516 conflictend = (ElfW(Rela) *)
1517 ((char *) conflict
1518 + GL(dl_loaded)->l_info [VALIDX (DT_GNU_CONFLICTSZ)]->d_un.d_val);
1519 _dl_resolve_conflicts (GL(dl_loaded), conflict, conflictend);
1520 HP_TIMING_NOW (stop);
1521 HP_TIMING_DIFF (relocate_time, start, stop);
1525 /* Mark all the objects so we know they have been already relocated. */
1526 for (l = GL(dl_loaded); l != NULL; l = l->l_next)
1527 l->l_relocated = 1;
1529 _dl_sysdep_start_cleanup ();
1531 else
1533 /* Now we have all the objects loaded. Relocate them all except for
1534 the dynamic linker itself. We do this in reverse order so that copy
1535 relocs of earlier objects overwrite the data written by later
1536 objects. We do not re-relocate the dynamic linker itself in this
1537 loop because that could result in the GOT entries for functions we
1538 call being changed, and that would break us. It is safe to relocate
1539 the dynamic linker out of order because it has no copy relocs (we
1540 know that because it is self-contained). */
1542 struct link_map *l;
1543 int consider_profiling = GL(dl_profile) != NULL;
1544 #ifndef HP_TIMING_NONAVAIL
1545 hp_timing_t start;
1546 hp_timing_t stop;
1547 hp_timing_t add;
1548 #endif
1550 /* If we are profiling we also must do lazy reloaction. */
1551 GL(dl_lazy) |= consider_profiling;
1553 l = GL(dl_loaded);
1554 while (l->l_next)
1555 l = l->l_next;
1557 HP_TIMING_NOW (start);
1560 /* While we are at it, help the memory handling a bit. We have to
1561 mark some data structures as allocated with the fake malloc()
1562 implementation in ld.so. */
1563 struct libname_list *lnp = l->l_libname->next;
1565 while (__builtin_expect (lnp != NULL, 0))
1567 lnp->dont_free = 1;
1568 lnp = lnp->next;
1571 if (l != &GL(dl_rtld_map))
1572 INTUSE(_dl_relocate_object) (l, l->l_scope, GL(dl_lazy),
1573 consider_profiling);
1575 l = l->l_prev;
1577 while (l);
1578 HP_TIMING_NOW (stop);
1580 HP_TIMING_DIFF (relocate_time, start, stop);
1582 /* Do any necessary cleanups for the startup OS interface code.
1583 We do these now so that no calls are made after rtld re-relocation
1584 which might be resolved to different functions than we expect.
1585 We cannot do this before relocating the other objects because
1586 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
1587 _dl_sysdep_start_cleanup ();
1589 /* Now enable profiling if needed. Like the previous call,
1590 this has to go here because the calls it makes should use the
1591 rtld versions of the functions (particularly calloc()), but it
1592 needs to have _dl_profile_map set up by the relocator. */
1593 if (__builtin_expect (GL(dl_profile_map) != NULL, 0))
1594 /* We must prepare the profiling. */
1595 INTUSE(_dl_start_profile) (GL(dl_profile_map), GL(dl_profile_output));
1597 if (GL(dl_rtld_map).l_opencount > 1)
1599 /* There was an explicit ref to the dynamic linker as a shared lib.
1600 Re-relocate ourselves with user-controlled symbol definitions. */
1601 HP_TIMING_NOW (start);
1602 INTUSE(_dl_relocate_object) (&GL(dl_rtld_map), GL(dl_loaded)->l_scope,
1603 0, 0);
1604 HP_TIMING_NOW (stop);
1605 HP_TIMING_DIFF (add, start, stop);
1606 HP_TIMING_ACCUM_NT (relocate_time, add);
1610 /* Now set up the variable which helps the assembler startup code. */
1611 GL(dl_main_searchlist) = &GL(dl_loaded)->l_searchlist;
1612 GL(dl_global_scope)[0] = &GL(dl_loaded)->l_searchlist;
1614 /* Save the information about the original global scope list since
1615 we need it in the memory handling later. */
1616 GL(dl_initial_searchlist) = *GL(dl_main_searchlist);
1618 #ifndef NONTLS_INIT_TP
1619 # define NONTLS_INIT_TP do { } while (0)
1620 #endif
1622 #ifdef USE_TLS
1623 if (GL(dl_tls_max_dtv_idx) > 0 || USE___THREAD || !TLS_INIT_TP_EXPENSIVE)
1625 /* Now that we have completed relocation, the initializer data
1626 for the TLS blocks has its final values and we can copy them
1627 into the main thread's TLS area, which we allocated above. */
1628 _dl_allocate_tls_init (tcbp);
1630 /* And finally install it for the main thread. If ld.so itself uses
1631 TLS we know the thread pointer was initialized earlier. */
1632 const char *lossage = TLS_INIT_TP (tcbp, USE___THREAD);
1633 if (__builtin_expect (lossage != NULL, 0))
1634 _dl_fatal_printf ("cannot set up thread-local storage: %s\n", lossage);
1636 else
1637 #endif
1638 NONTLS_INIT_TP;
1641 /* Initialize _r_debug. */
1642 struct r_debug *r = _dl_debug_initialize (GL(dl_rtld_map).l_addr);
1643 struct link_map *l;
1645 l = GL(dl_loaded);
1647 #ifdef ELF_MACHINE_DEBUG_SETUP
1649 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
1651 ELF_MACHINE_DEBUG_SETUP (l, r);
1652 ELF_MACHINE_DEBUG_SETUP (&GL(dl_rtld_map), r);
1654 #else
1656 if (l->l_info[DT_DEBUG] != NULL)
1657 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
1658 with the run-time address of the r_debug structure */
1659 l->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1661 /* Fill in the pointer in the dynamic linker's own dynamic section, in
1662 case you run gdb on the dynamic linker directly. */
1663 if (GL(dl_rtld_map).l_info[DT_DEBUG] != NULL)
1664 GL(dl_rtld_map).l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1666 #endif
1668 /* Notify the debugger that all objects are now mapped in. */
1669 r->r_state = RT_ADD;
1670 INTUSE(_dl_debug_state) ();
1673 #ifndef MAP_COPY
1674 /* We must munmap() the cache file. */
1675 INTUSE(_dl_unload_cache) ();
1676 #endif
1678 /* Once we return, _dl_sysdep_start will invoke
1679 the DT_INIT functions and then *USER_ENTRY. */
1682 /* This is a little helper function for resolving symbols while
1683 tracing the binary. */
1684 static void
1685 print_unresolved (int errcode __attribute__ ((unused)), const char *objname,
1686 const char *errstring)
1688 if (objname[0] == '\0')
1689 objname = rtld_progname ?: "<main program>";
1690 _dl_error_printf ("%s (%s)\n", errstring, objname);
1693 /* This is a little helper function for resolving symbols while
1694 tracing the binary. */
1695 static void
1696 print_missing_version (int errcode __attribute__ ((unused)),
1697 const char *objname, const char *errstring)
1699 _dl_error_printf ("%s: %s: %s\n", rtld_progname ?: "<program name unknown>",
1700 objname, errstring);
1703 /* Nonzero if any of the debugging options is enabled. */
1704 static int any_debug;
1706 /* Process the string given as the parameter which explains which debugging
1707 options are enabled. */
1708 static void
1709 process_dl_debug (const char *dl_debug)
1711 /* When adding new entries make sure that the maximal length of a name
1712 is correctly handled in the LD_DEBUG_HELP code below. */
1713 static const struct
1715 unsigned char len;
1716 const char name[10];
1717 const char helptext[41];
1718 unsigned short int mask;
1719 } debopts[] =
1721 #define LEN_AND_STR(str) sizeof (str) - 1, str
1722 { LEN_AND_STR ("libs"), "display library search paths",
1723 DL_DEBUG_LIBS | DL_DEBUG_IMPCALLS },
1724 { LEN_AND_STR ("reloc"), "display relocation processing",
1725 DL_DEBUG_RELOC | DL_DEBUG_IMPCALLS },
1726 { LEN_AND_STR ("files"), "display progress for input file",
1727 DL_DEBUG_FILES | DL_DEBUG_IMPCALLS },
1728 { LEN_AND_STR ("symbols"), "display symbol table processing",
1729 DL_DEBUG_SYMBOLS | DL_DEBUG_IMPCALLS },
1730 { LEN_AND_STR ("bindings"), "display information about symbol binding",
1731 DL_DEBUG_BINDINGS | DL_DEBUG_IMPCALLS },
1732 { LEN_AND_STR ("versions"), "display version dependencies",
1733 DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS },
1734 { LEN_AND_STR ("all"), "all previous options combined",
1735 DL_DEBUG_LIBS | DL_DEBUG_RELOC | DL_DEBUG_FILES | DL_DEBUG_SYMBOLS
1736 | DL_DEBUG_BINDINGS | DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS },
1737 { LEN_AND_STR ("statistics"), "display relocation statistics",
1738 DL_DEBUG_STATISTICS },
1739 { LEN_AND_STR ("help"), "display this help message and exit",
1740 DL_DEBUG_HELP },
1742 #define ndebopts (sizeof (debopts) / sizeof (debopts[0]))
1744 /* Skip separating white spaces and commas. */
1745 while (*dl_debug != '\0')
1747 if (*dl_debug != ' ' && *dl_debug != ',' && *dl_debug != ':')
1749 size_t cnt;
1750 size_t len = 1;
1752 while (dl_debug[len] != '\0' && dl_debug[len] != ' '
1753 && dl_debug[len] != ',' && dl_debug[len] != ':')
1754 ++len;
1756 for (cnt = 0; cnt < ndebopts; ++cnt)
1757 if (debopts[cnt].len == len
1758 && memcmp (dl_debug, debopts[cnt].name, len) == 0)
1760 GL(dl_debug_mask) |= debopts[cnt].mask;
1761 any_debug = 1;
1762 break;
1765 if (cnt == ndebopts)
1767 /* Display a warning and skip everything until next
1768 separator. */
1769 char *copy = strndupa (dl_debug, len);
1770 _dl_error_printf ("\
1771 warning: debug option `%s' unknown; try LD_DEBUG=help\n", copy);
1774 dl_debug += len;
1775 continue;
1778 ++dl_debug;
1781 if (GL(dl_debug_mask) & DL_DEBUG_HELP)
1783 size_t cnt;
1785 _dl_printf ("\
1786 Valid options for the LD_DEBUG environment variable are:\n\n");
1788 for (cnt = 0; cnt < ndebopts; ++cnt)
1789 _dl_printf (" %.*s%s%s\n", debopts[cnt].len, debopts[cnt].name,
1790 " " + debopts[cnt].len - 3,
1791 debopts[cnt].helptext);
1793 _dl_printf ("\n\
1794 To direct the debugging output into a file instead of standard output\n\
1795 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n");
1796 _exit (0);
1800 /* Process all environments variables the dynamic linker must recognize.
1801 Since all of them start with `LD_' we are a bit smarter while finding
1802 all the entries. */
1803 extern char **_environ attribute_hidden;
1806 static void
1807 process_envvars (enum mode *modep)
1809 char **runp = _environ;
1810 char *envline;
1811 enum mode mode = normal;
1812 char *debug_output = NULL;
1814 /* This is the default place for profiling data file. */
1815 GL(dl_profile_output)
1816 = &"/var/tmp\0/var/profile"[INTUSE(__libc_enable_secure) ? 9 : 0];
1818 while ((envline = _dl_next_ld_env_entry (&runp)) != NULL)
1820 size_t len = 0;
1822 while (envline[len] != '\0' && envline[len] != '=')
1823 ++len;
1825 if (envline[len] != '=')
1826 /* This is a "LD_" variable at the end of the string without
1827 a '=' character. Ignore it since otherwise we will access
1828 invalid memory below. */
1829 continue;
1831 switch (len)
1833 case 4:
1834 /* Warning level, verbose or not. */
1835 if (memcmp (envline, "WARN", 4) == 0)
1836 GL(dl_verbose) = envline[5] != '\0';
1837 break;
1839 case 5:
1840 /* Debugging of the dynamic linker? */
1841 if (memcmp (envline, "DEBUG", 5) == 0)
1842 process_dl_debug (&envline[6]);
1843 break;
1845 case 7:
1846 /* Print information about versions. */
1847 if (memcmp (envline, "VERBOSE", 7) == 0)
1849 version_info = envline[8] != '\0';
1850 break;
1853 /* List of objects to be preloaded. */
1854 if (memcmp (envline, "PRELOAD", 7) == 0)
1856 preloadlist = &envline[8];
1857 break;
1860 /* Which shared object shall be profiled. */
1861 if (memcmp (envline, "PROFILE", 7) == 0 && envline[8] != '\0')
1862 GL(dl_profile) = &envline[8];
1863 break;
1865 case 8:
1866 /* Do we bind early? */
1867 if (memcmp (envline, "BIND_NOW", 8) == 0)
1869 GL(dl_lazy) = envline[9] == '\0';
1870 break;
1872 if (memcmp (envline, "BIND_NOT", 8) == 0)
1873 GL(dl_bind_not) = envline[9] != '\0';
1874 break;
1876 case 9:
1877 /* Test whether we want to see the content of the auxiliary
1878 array passed up from the kernel. */
1879 if (memcmp (envline, "SHOW_AUXV", 9) == 0)
1880 _dl_show_auxv ();
1881 break;
1883 case 10:
1884 /* Mask for the important hardware capabilities. */
1885 if (memcmp (envline, "HWCAP_MASK", 10) == 0)
1886 GL(dl_hwcap_mask) = __strtoul_internal (&envline[11], NULL, 0, 0);
1887 break;
1889 case 11:
1890 /* Path where the binary is found. */
1891 if (!INTUSE(__libc_enable_secure)
1892 && memcmp (envline, "ORIGIN_PATH", 11) == 0)
1893 GL(dl_origin_path) = &envline[12];
1894 break;
1896 case 12:
1897 /* The library search path. */
1898 if (memcmp (envline, "LIBRARY_PATH", 12) == 0)
1900 library_path = &envline[13];
1901 break;
1904 /* Where to place the profiling data file. */
1905 if (memcmp (envline, "DEBUG_OUTPUT", 12) == 0)
1907 debug_output = &envline[13];
1908 break;
1911 if (memcmp (envline, "DYNAMIC_WEAK", 12) == 0)
1912 GL(dl_dynamic_weak) = 1;
1913 break;
1915 case 14:
1916 /* Where to place the profiling data file. */
1917 if (!INTUSE(__libc_enable_secure)
1918 && memcmp (envline, "PROFILE_OUTPUT", 14) == 0
1919 && envline[15] != '\0')
1920 GL(dl_profile_output) = &envline[15];
1921 break;
1923 case 16:
1924 /* The mode of the dynamic linker can be set. */
1925 if (memcmp (envline, "TRACE_PRELINKING", 16) == 0)
1927 mode = trace;
1928 GL(dl_verbose) = 1;
1929 GL(dl_debug_mask) |= DL_DEBUG_PRELINK;
1930 GL(dl_trace_prelink) = &envline[17];
1932 break;
1934 case 20:
1935 /* The mode of the dynamic linker can be set. */
1936 if (memcmp (envline, "TRACE_LOADED_OBJECTS", 20) == 0)
1937 mode = trace;
1938 break;
1940 /* We might have some extra environment variable to handle. This
1941 is tricky due to the pre-processing of the length of the name
1942 in the switch statement here. The code here assumes that added
1943 environment variables have a different length. */
1944 #ifdef EXTRA_LD_ENVVARS
1945 EXTRA_LD_ENVVARS
1946 #endif
1950 /* The caller wants this information. */
1951 *modep = mode;
1953 /* Extra security for SUID binaries. Remove all dangerous environment
1954 variables. */
1955 if (__builtin_expect (INTUSE(__libc_enable_secure), 0))
1957 static const char unsecure_envvars[] =
1958 #ifdef EXTRA_UNSECURE_ENVVARS
1959 EXTRA_UNSECURE_ENVVARS
1960 #endif
1961 UNSECURE_ENVVARS;
1962 const char *nextp;
1964 nextp = unsecure_envvars;
1967 unsetenv (nextp);
1968 /* We could use rawmemchr but this need not be fast. */
1969 nextp = (char *) (strchr) (nextp, '\0') + 1;
1971 while (*nextp != '\0');
1973 if (__access ("/etc/suid-debug", F_OK) != 0)
1974 unsetenv ("MALLOC_CHECK_");
1976 /* If we have to run the dynamic linker in debugging mode and the
1977 LD_DEBUG_OUTPUT environment variable is given, we write the debug
1978 messages to this file. */
1979 else if (any_debug && debug_output != NULL)
1981 #ifdef O_NOFOLLOW
1982 const int flags = O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW;
1983 #else
1984 const int flags = O_WRONLY | O_APPEND | O_CREAT;
1985 #endif
1986 size_t name_len = strlen (debug_output);
1987 char buf[name_len + 12];
1988 char *startp;
1990 buf[name_len + 11] = '\0';
1991 startp = _itoa (__getpid (), &buf[name_len + 11], 10, 0);
1992 *--startp = '.';
1993 startp = memcpy (startp - name_len, debug_output, name_len);
1995 GL(dl_debug_fd) = __open (startp, flags, DEFFILEMODE);
1996 if (GL(dl_debug_fd) == -1)
1997 /* We use standard output if opening the file failed. */
1998 GL(dl_debug_fd) = STDOUT_FILENO;
2003 /* Print the various times we collected. */
2004 static void
2005 print_statistics (void)
2007 #ifndef HP_TIMING_NONAVAIL
2008 char buf[200];
2009 char *cp;
2010 char *wp;
2012 /* Total time rtld used. */
2013 if (HP_TIMING_AVAIL)
2015 HP_TIMING_PRINT (buf, sizeof (buf), rtld_total_time);
2016 INTUSE(_dl_debug_printf) ("\nruntime linker statistics:\n"
2017 " total startup time in dynamic loader: %s\n",
2018 buf);
2021 /* Print relocation statistics. */
2022 if (HP_TIMING_AVAIL)
2024 char pbuf[30];
2025 HP_TIMING_PRINT (buf, sizeof (buf), relocate_time);
2026 cp = _itoa ((1000ULL * relocate_time) / rtld_total_time,
2027 pbuf + sizeof (pbuf), 10, 0);
2028 wp = pbuf;
2029 switch (pbuf + sizeof (pbuf) - cp)
2031 case 3:
2032 *wp++ = *cp++;
2033 case 2:
2034 *wp++ = *cp++;
2035 case 1:
2036 *wp++ = '.';
2037 *wp++ = *cp++;
2039 *wp = '\0';
2040 INTUSE(_dl_debug_printf) ("\
2041 time needed for relocation: %s (%s%%)\n",
2042 buf, pbuf);
2044 #endif
2045 INTUSE(_dl_debug_printf) (" number of relocations: %lu\n",
2046 GL(dl_num_relocations));
2047 INTUSE(_dl_debug_printf) (" number of relocations from cache: %lu\n",
2048 GL(dl_num_cache_relocations));
2050 #ifndef HP_TIMING_NONAVAIL
2051 /* Time spend while loading the object and the dependencies. */
2052 if (HP_TIMING_AVAIL)
2054 char pbuf[30];
2055 HP_TIMING_PRINT (buf, sizeof (buf), load_time);
2056 cp = _itoa ((1000ULL * load_time) / rtld_total_time,
2057 pbuf + sizeof (pbuf), 10, 0);
2058 wp = pbuf;
2059 switch (pbuf + sizeof (pbuf) - cp)
2061 case 3:
2062 *wp++ = *cp++;
2063 case 2:
2064 *wp++ = *cp++;
2065 case 1:
2066 *wp++ = '.';
2067 *wp++ = *cp++;
2069 *wp = '\0';
2070 INTUSE(_dl_debug_printf) ("\
2071 time needed to load objects: %s (%s%%)\n",
2072 buf, pbuf);
2074 #endif