1 /* Run time dynamic linker.
2 Copyright (C) 1995-1999, 2000, 2001, 2002 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
25 #include <sys/mman.h> /* Check if MAP_ANON is defined. */
26 #include <sys/param.h>
29 #include <stdio-common/_itoa.h>
31 #include <fpu_control.h>
32 #include <hp-timing.h>
33 #include <bits/libc-lock.h>
34 #include "dynamic-link.h"
35 #include "dl-librecon.h"
36 #include <unsecvars.h>
38 #include <dl-procinfo.h>
42 /* Helper function to handle errors while resolving symbols. */
43 static void print_unresolved (int errcode, const char *objname,
44 const char *errsting);
46 /* Helper function to handle errors when a version is missing. */
47 static void print_missing_version (int errcode, const char *objname,
48 const char *errsting);
50 /* Print the various times we collected. */
51 static void print_statistics (void);
53 /* This is a list of all the modes the dynamic loader can be in. */
54 enum mode { normal, list, verify, trace };
56 /* Process all environments variables the dynamic linker must recognize.
57 Since all of them start with `LD_' we are a bit smarter while finding
59 static void process_envvars (enum mode *modep);
61 int _dl_argc attribute_hidden;
62 char **_dl_argv = NULL;
65 /* Nonzero if we were run directly. */
66 unsigned int _dl_skip_args attribute_hidden;
68 /* Set nonzero during loading and initialization of executable and
69 libraries, cleared before the executable's entry point runs. This
70 must not be initialized to nonzero, because the unused dynamic
71 linker loaded in for libc.so's "ld.so.1" dep will provide the
72 definition seen by libc.so's initializer; that value must be zero,
73 and will be since that dynamic linker's _dl_start and dl_main will
75 int _dl_starting_up = 0;
76 INTVARDEF(_dl_starting_up)
78 /* This is the structure which defines all variables global to ld.so
79 (except those which cannot be added for some reason). */
80 struct rtld_global _rtld_global =
82 /* Get architecture specific initializer. */
83 #include <dl-procinfo.c>
84 ._dl_debug_fd = STDERR_FILENO,
86 /* XXX I know about at least one case where we depend on the old
87 weak behavior (it has to do with librt). Until we get DSO
88 groups implemented we have to make this the default.
90 ._dl_dynamic_weak = 1,
93 ._dl_fpu_control = _FPU_DEFAULT,
94 ._dl_correct_cache_id = _DL_CACHE_DEFAULT_ID,
95 ._dl_hwcap_mask = HWCAP_IMPORTANT,
96 ._dl_load_lock = _LIBC_LOCK_RECURSIVE_INITIALIZER
98 strong_alias (_rtld_global, _rtld_local);
100 static void dl_main (const ElfW(Phdr) *phdr, ElfW(Word) phnum,
101 ElfW(Addr) *user_entry);
103 static struct libname_list _dl_rtld_libname;
104 static struct libname_list _dl_rtld_libname2;
106 /* We expect less than a second for relocation. */
107 #ifdef HP_SMALL_TIMING_AVAIL
108 # undef HP_TIMING_AVAIL
109 # define HP_TIMING_AVAIL HP_SMALL_TIMING_AVAIL
112 /* Variable for statistics. */
113 #ifndef HP_TIMING_NONAVAIL
114 static hp_timing_t rtld_total_time;
115 static hp_timing_t relocate_time;
116 static hp_timing_t load_time;
119 static ElfW(Addr) _dl_start_final (void *arg, struct link_map *bootstrap_map_p,
120 hp_timing_t start_time);
125 # error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
128 static ElfW(Addr) __attribute_used__ internal_function
129 _dl_start (void *arg)
131 struct link_map bootstrap_map;
132 hp_timing_t start_time;
133 #if !defined HAVE_BUILTIN_MEMSET || defined USE_TLS
142 /* This #define produces dynamic linking inline functions for
143 bootstrap relocation instead of general-purpose relocation. */
144 #define RTLD_BOOTSTRAP
145 #define RESOLVE_MAP(sym, version, flags) \
146 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : &bootstrap_map)
147 #define RESOLVE(sym, version, flags) \
148 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : bootstrap_map.l_addr)
149 #include "dynamic-link.h"
151 if (HP_TIMING_INLINE && HP_TIMING_AVAIL)
152 HP_TIMING_NOW (start_time);
154 /* Partly clean the `bootstrap_map' structure up. Don't use
155 `memset' since it might not be built in or inlined and we cannot
156 make function calls at this point. Use '__builtin_memset' if we
157 know it is available. */
158 #ifdef HAVE_BUILTIN_MEMSET
159 __builtin_memset (bootstrap_map.l_info, '\0', sizeof (bootstrap_map.l_info));
162 cnt < sizeof (bootstrap_map.l_info) / sizeof (bootstrap_map.l_info[0]);
164 bootstrap_map.l_info[cnt] = 0;
167 /* Figure out the run-time load address of the dynamic linker itself. */
168 bootstrap_map.l_addr = elf_machine_load_address ();
170 /* Read our own dynamic section and fill in the info array. */
171 bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
172 elf_get_dynamic_info (&bootstrap_map);
175 # ifndef HAVE___THREAD
176 /* Signal that we have not found TLS data so far. */
177 bootstrap_map.l_tls_modid = 0;
180 /* Get the dynamic linkers program header. */
181 ehdr = (ElfW(Ehdr) *) bootstrap_map.l_addr;
182 phdr = (ElfW(Phdr) *) (bootstrap_map.l_addr + ehdr->e_phoff);
183 for (cnt = 0; cnt < ehdr->e_phnum; ++cnt)
184 if (phdr[cnt].p_type == PT_TLS)
187 size_t max_align = MAX (TLS_INIT_TCB_ALIGN, phdr[cnt].p_align);
189 bootstrap_map.l_tls_blocksize = phdr[cnt].p_memsz;
190 bootstrap_map.l_tls_align = phdr[cnt].p_align;
191 assert (bootstrap_map.l_tls_blocksize != 0);
192 bootstrap_map.l_tls_initimage_size = phdr[cnt].p_filesz;
193 bootstrap_map.l_tls_initimage = (void *) (bootstrap_map.l_addr
194 + phdr[cnt].p_offset);
196 /* We can now allocate the initial TLS block. This can happen
197 on the stack. We'll get the final memory later when we
198 know all about the various objects loaded at startup
201 tlsblock = alloca (roundup (bootstrap_map.l_tls_blocksize,
206 tlsblock = alloca (roundup (TLS_INIT_TCB_SIZE,
207 bootstrap_map.l_tls_align)
208 + bootstrap_map.l_tls_blocksize
211 /* In case a model with a different layout for the TCB and DTV
212 is defined add another #elif here and in the following #ifs. */
213 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
215 /* Align the TLS block. */
216 tlsblock = (void *) (((uintptr_t) tlsblock + max_align - 1)
219 /* Initialize the dtv. [0] is the length, [1] the generation
221 initdtv[0].counter = 1;
222 initdtv[1].counter = 0;
224 /* Initialize the TLS block. */
226 initdtv[2].pointer = tlsblock;
228 bootstrap_map.l_tls_offset = roundup (TLS_INIT_TCB_SIZE,
229 bootstrap_map.l_tls_align);
230 initdtv[2].pointer = (char *) tlsblock + bootstrap_map.l_tls_offset;
232 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
234 memset (__mempcpy (initdtv[2].pointer, bootstrap_map.l_tls_initimage,
235 bootstrap_map.l_tls_initimage_size),
236 '\0', (bootstrap_map.l_tls_blocksize
237 - bootstrap_map.l_tls_initimage_size));
239 /* Install the pointer to the dtv. */
241 /* Initialize the thread pointer. */
243 bootstrap_map.l_tls_offset
244 = roundup (bootstrap_map.l_tls_blocksize, TLS_INIT_TCB_ALIGN);
246 INSTALL_DTV ((char *) tlsblock + bootstrap_map.l_tls_offset,
249 TLS_INIT_TP ((char *) tlsblock + bootstrap_map.l_tls_offset);
251 INSTALL_DTV (tlsblock, initdtv);
252 TLS_INIT_TP (tlsblock);
254 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
257 /* So far this is module number one. */
258 bootstrap_map.l_tls_modid = 1;
260 /* There can only be one PT_TLS entry. */
265 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
266 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info);
269 if (bootstrap_map.l_addr || ! bootstrap_map.l_info[VALIDX(DT_GNU_PRELINKED)])
271 /* Relocate ourselves so we can do normal function calls and
272 data access using the global offset table. */
274 ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0, 0);
277 /* Please note that we don't allow profiling of this object and
278 therefore need not test whether we have to allocate the array
279 for the relocation results (as done in dl-reloc.c). */
281 /* Now life is sane; we can call functions and access global data.
282 Set up to use the operating system facilities, and find out from
283 the operating system's program loader where to find the program
284 header table in core. Put the rest of _dl_start into a separate
285 function, that way the compiler cannot put accesses to the GOT
286 before ELF_DYNAMIC_RELOCATE. */
288 ElfW(Addr) entry = _dl_start_final (arg, &bootstrap_map, start_time);
290 #ifndef ELF_MACHINE_START_ADDRESS
291 # define ELF_MACHINE_START_ADDRESS(map, start) (start)
294 return ELF_MACHINE_START_ADDRESS (GL(dl_loaded), entry);
300 # define VALIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
301 + DT_EXTRANUM + DT_VALTAGIDX (tag))
304 # define ADDRIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
305 + DT_EXTRANUM + DT_VALNUM + DT_ADDRTAGIDX (tag))
309 _dl_start_final (void *arg, struct link_map *bootstrap_map_p,
310 hp_timing_t start_time)
312 /* The use of `alloca' here looks ridiculous but it helps. The goal
313 is to avoid the function from being inlined. There is no official
314 way to do this so we use this trick. gcc never inlines functions
315 which use `alloca'. */
316 ElfW(Addr) *start_addr = alloca (sizeof (ElfW(Addr)));
317 extern char _begin[] attribute_hidden;
318 extern char _end[] attribute_hidden;
322 /* If it hasn't happen yet record the startup time. */
323 if (! HP_TIMING_INLINE)
324 HP_TIMING_NOW (start_time);
326 /* Initialize the timing functions. */
327 HP_TIMING_DIFF_INIT ();
330 /* Transfer data about ourselves to the permanent link_map structure. */
331 GL(dl_rtld_map).l_addr = bootstrap_map_p->l_addr;
332 GL(dl_rtld_map).l_ld = bootstrap_map_p->l_ld;
333 GL(dl_rtld_map).l_opencount = 1;
334 memcpy (GL(dl_rtld_map).l_info, bootstrap_map_p->l_info,
335 sizeof GL(dl_rtld_map).l_info);
336 _dl_setup_hash (&GL(dl_rtld_map));
337 GL(dl_rtld_map).l_mach = bootstrap_map_p->l_mach;
338 GL(dl_rtld_map).l_map_start = (ElfW(Addr)) _begin;
339 GL(dl_rtld_map).l_map_end = (ElfW(Addr)) _end;
340 /* Copy the TLS related data if necessary. */
342 # ifdef HAVE___THREAD
343 assert (bootstrap_map_p->l_tls_modid != 0);
345 if (bootstrap_map_p->l_tls_modid != 0)
348 GL(dl_rtld_map).l_tls_blocksize = bootstrap_map_p->l_tls_blocksize;
349 GL(dl_rtld_map).l_tls_align = bootstrap_map_p->l_tls_align;
350 GL(dl_rtld_map).l_tls_initimage_size
351 = bootstrap_map_p->l_tls_initimage_size;
352 GL(dl_rtld_map).l_tls_initimage = bootstrap_map_p->l_tls_initimage;
353 GL(dl_rtld_map).l_tls_offset = bootstrap_map_p->l_tls_offset;
354 GL(dl_rtld_map).l_tls_modid = 1;
359 HP_TIMING_NOW (GL(dl_cpuclock_offset));
362 /* Call the OS-dependent function to set up life so we can do things like
363 file access. It will call `dl_main' (below) to do all the real work
364 of the dynamic linker, and then unwind our frame and run the user
365 entry point on the same stack we entered on. */
366 *start_addr = _dl_sysdep_start (arg, &dl_main);
368 #ifndef HP_TIMING_NONAVAIL
371 hp_timing_t end_time;
373 /* Get the current time. */
374 HP_TIMING_NOW (end_time);
376 /* Compute the difference. */
377 HP_TIMING_DIFF (rtld_total_time, start_time, end_time);
381 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_STATISTICS, 0))
387 /* Now life is peachy; we can do all normal operations.
388 On to the real work. */
390 /* Some helper functions. */
392 /* Arguments to relocate_doit. */
401 /* Argument to map_doit. */
403 /* Return value of map_doit. */
404 struct link_map *main_map;
407 /* Arguments to version_check_doit. */
408 struct version_check_args
415 relocate_doit (void *a)
417 struct relocate_args *args = (struct relocate_args *) a;
419 INTUSE(_dl_relocate_object) (args->l, args->l->l_scope, args->lazy, 0);
425 struct map_args *args = (struct map_args *) a;
426 args->main_map = INTUSE(_dl_map_object) (NULL, args->str, 0, lt_library, 0, 0);
430 version_check_doit (void *a)
432 struct version_check_args *args = (struct version_check_args *) a;
433 if (_dl_check_all_versions (GL(dl_loaded), 1, args->dotrace) && args->doexit)
434 /* We cannot start the application. Abort now. */
439 static inline struct link_map *
440 find_needed (const char *name)
442 unsigned int n = GL(dl_loaded)->l_searchlist.r_nlist;
445 if (_dl_name_match_p (name, GL(dl_loaded)->l_searchlist.r_list[n]))
446 return GL(dl_loaded)->l_searchlist.r_list[n];
448 /* Should never happen. */
453 match_version (const char *string, struct link_map *map)
455 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
458 #define VERDEFTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
459 if (map->l_info[VERDEFTAG] == NULL)
460 /* The file has no symbol versioning. */
463 def = (ElfW(Verdef) *) ((char *) map->l_addr
464 + map->l_info[VERDEFTAG]->d_un.d_ptr);
467 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
469 /* Compare the version strings. */
470 if (strcmp (string, strtab + aux->vda_name) == 0)
474 /* If no more definitions we failed to find what we want. */
475 if (def->vd_next == 0)
478 /* Next definition. */
479 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
485 static const char *library_path; /* The library search path. */
486 static const char *preloadlist; /* The list preloaded objects. */
487 static int version_info; /* Nonzero if information about
488 versions has to be printed. */
491 dl_main (const ElfW(Phdr) *phdr,
493 ElfW(Addr) *user_entry)
495 const ElfW(Phdr) *ph;
497 struct link_map **preloads;
498 unsigned int npreloads;
501 bool has_interp = false;
503 bool prelinked = false;
504 bool rtld_is_main = false;
505 #ifndef HP_TIMING_NONAVAIL
514 /* Process the environment variable which control the behaviour. */
515 process_envvars (&mode);
517 /* Set up a flag which tells we are just starting. */
518 INTUSE(_dl_starting_up) = 1;
520 if (*user_entry == (ElfW(Addr)) ENTRY_POINT)
522 /* Ho ho. We are not the program interpreter! We are the program
523 itself! This means someone ran ld.so as a command. Well, that
524 might be convenient to do sometimes. We support it by
525 interpreting the args like this:
527 ld.so PROGRAM ARGS...
529 The first argument is the name of a file containing an ELF
530 executable we will load and run with the following arguments.
531 To simplify life here, PROGRAM is searched for using the
532 normal rules for shared objects, rather than $PATH or anything
533 like that. We just load it and use its entry point; we don't
534 pay attention to its PT_INTERP command (we are the interpreter
535 ourselves). This is an easy way to test a new ld.so before
539 /* Note the place where the dynamic linker actually came from. */
540 GL(dl_rtld_map).l_name = rtld_progname;
543 if (! strcmp (INTUSE(_dl_argv)[1], "--list"))
546 GL(dl_lazy) = -1; /* This means do no dependency analysis. */
552 else if (! strcmp (INTUSE(_dl_argv)[1], "--verify"))
560 else if (! strcmp (INTUSE(_dl_argv)[1], "--library-path")
563 library_path = INTUSE(_dl_argv)[2];
567 INTUSE(_dl_argv) += 2;
569 else if (! strcmp (INTUSE(_dl_argv)[1], "--inhibit-rpath")
572 GL(dl_inhibit_rpath) = INTUSE(_dl_argv)[2];
576 INTUSE(_dl_argv) += 2;
581 /* If we have no further argument the program was called incorrectly.
582 Grant the user some education. */
585 Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
586 You have invoked `ld.so', the helper program for shared library executables.\n\
587 This program usually lives in the file `/lib/ld.so', and special directives\n\
588 in executable files using ELF shared libraries tell the system's program\n\
589 loader to load the helper program from this file. This helper program loads\n\
590 the shared libraries needed by the program executable, prepares the program\n\
591 to run, and runs it. You may invoke this helper program directly from the\n\
592 command line to load and run an ELF executable file; this is like executing\n\
593 that file itself, but always uses this helper program from the file you\n\
594 specified, instead of the helper program file specified in the executable\n\
595 file you run. This is mostly of use for maintainers to test new versions\n\
596 of this helper program; chances are you did not intend to run this program.\n\
598 --list list all dependencies and how they are resolved\n\
599 --verify verify that given object really is a dynamically linked\n\
600 object we can handle\n\
601 --library-path PATH use given PATH instead of content of the environment\n\
602 variable LD_LIBRARY_PATH\n\
603 --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\
610 /* Initialize the data structures for the search paths for shared
612 _dl_init_paths (library_path);
614 if (__builtin_expect (mode, normal) == verify)
617 const char *err_str = NULL;
618 struct map_args args;
620 args.str = rtld_progname;
621 (void) INTUSE(_dl_catch_error) (&objname, &err_str, map_doit, &args);
622 if (__builtin_expect (err_str != NULL, 0))
623 /* We don't free the returned string, the programs stops
625 _exit (EXIT_FAILURE);
629 HP_TIMING_NOW (start);
630 INTUSE(_dl_map_object) (NULL, rtld_progname, 0, lt_library, 0, 0);
631 HP_TIMING_NOW (stop);
633 HP_TIMING_DIFF (load_time, start, stop);
636 phdr = GL(dl_loaded)->l_phdr;
637 phnum = GL(dl_loaded)->l_phnum;
638 /* We overwrite here a pointer to a malloc()ed string. But since
639 the malloc() implementation used at this point is the dummy
640 implementations which has no real free() function it does not
641 makes sense to free the old string first. */
642 GL(dl_loaded)->l_name = (char *) "";
643 *user_entry = GL(dl_loaded)->l_entry;
647 /* Create a link_map for the executable itself.
648 This will be what dlopen on "" returns. */
649 _dl_new_object ((char *) "", "", lt_executable, NULL);
650 if (GL(dl_loaded) == NULL)
651 _dl_fatal_printf ("cannot allocate memory for link map\n");
652 GL(dl_loaded)->l_phdr = phdr;
653 GL(dl_loaded)->l_phnum = phnum;
654 GL(dl_loaded)->l_entry = *user_entry;
656 /* At this point we are in a bit of trouble. We would have to
657 fill in the values for l_dev and l_ino. But in general we
658 do not know where the file is. We also do not handle AT_EXECFD
659 even if it would be passed up.
661 We leave the values here defined to 0. This is normally no
662 problem as the program code itself is normally no shared
663 object and therefore cannot be loaded dynamically. Nothing
664 prevent the use of dynamic binaries and in these situations
665 we might get problems. We might not be able to find out
666 whether the object is already loaded. But since there is no
667 easy way out and because the dynamic binary must also not
668 have an SONAME we ignore this program for now. If it becomes
669 a problem we can force people using SONAMEs. */
671 /* We delay initializing the path structure until we got the dynamic
672 information for the program. */
675 GL(dl_loaded)->l_map_end = 0;
676 /* Perhaps the executable has no PT_LOAD header entries at all. */
677 GL(dl_loaded)->l_map_start = ~0;
678 /* We opened the file, account for it. */
679 ++GL(dl_loaded)->l_opencount;
681 /* Scan the program header table for the dynamic section. */
682 for (ph = phdr; ph < &phdr[phnum]; ++ph)
686 /* Find out the load address. */
687 GL(dl_loaded)->l_addr = (ElfW(Addr)) phdr - ph->p_vaddr;
690 /* This tells us where to find the dynamic section,
691 which tells us everything we need to do. */
692 GL(dl_loaded)->l_ld = (void *) GL(dl_loaded)->l_addr + ph->p_vaddr;
695 /* This "interpreter segment" was used by the program loader to
696 find the program interpreter, which is this program itself, the
697 dynamic linker. We note what name finds us, so that a future
698 dlopen call or DT_NEEDED entry, for something that wants to link
699 against the dynamic linker as a shared library, will know that
700 the shared object is already loaded. */
701 _dl_rtld_libname.name = ((const char *) GL(dl_loaded)->l_addr
703 /* _dl_rtld_libname.next = NULL; Already zero. */
704 GL(dl_rtld_map).l_libname = &_dl_rtld_libname;
706 /* Ordinarilly, we would get additional names for the loader from
707 our DT_SONAME. This can't happen if we were actually linked as
708 a static executable (detect this case when we have no DYNAMIC).
709 If so, assume the filename component of the interpreter path to
710 be our SONAME, and add it to our name list. */
711 if (GL(dl_rtld_map).l_ld == NULL)
713 const char *p = NULL;
714 const char *cp = _dl_rtld_libname.name;
716 /* Find the filename part of the path. */
723 _dl_rtld_libname2.name = p;
724 /* _dl_rtld_libname2.next = NULL; Already zero. */
725 _dl_rtld_libname.next = &_dl_rtld_libname2;
736 /* Remember where the main program starts in memory. */
737 mapstart = (GL(dl_loaded)->l_addr
738 + (ph->p_vaddr & ~(ph->p_align - 1)));
739 if (GL(dl_loaded)->l_map_start > mapstart)
740 GL(dl_loaded)->l_map_start = mapstart;
742 /* Also where it ends. */
743 allocend = GL(dl_loaded)->l_addr + ph->p_vaddr + ph->p_memsz;
744 if (GL(dl_loaded)->l_map_end < allocend)
745 GL(dl_loaded)->l_map_end = allocend;
752 /* Note that in the case the dynamic linker we duplicate work
753 here since we read the PT_TLS entry already in
754 _dl_start_final. But the result is repeatable so do not
755 check for this special but unimportant case. */
756 GL(dl_loaded)->l_tls_blocksize = ph->p_memsz;
757 GL(dl_loaded)->l_tls_align = ph->p_align;
758 GL(dl_loaded)->l_tls_initimage_size = ph->p_filesz;
759 GL(dl_loaded)->l_tls_initimage = (void *) ph->p_vaddr;
761 /* This image gets the ID one. */
762 GL(dl_tls_max_dtv_idx) = GL(dl_loaded)->l_tls_modid = 1;
767 if (! GL(dl_loaded)->l_map_end)
768 GL(dl_loaded)->l_map_end = ~0;
769 if (! GL(dl_rtld_map).l_libname && GL(dl_rtld_map).l_name)
771 /* We were invoked directly, so the program might not have a
773 _dl_rtld_libname.name = GL(dl_rtld_map).l_name;
774 /* _dl_rtld_libname.next = NULL; Already zero. */
775 GL(dl_rtld_map).l_libname = &_dl_rtld_libname;
778 assert (GL(dl_rtld_map).l_libname); /* How else did we get here? */
782 /* Extract the contents of the dynamic section for easy access. */
783 elf_get_dynamic_info (GL(dl_loaded));
784 if (GL(dl_loaded)->l_info[DT_HASH])
785 /* Set up our cache of pointers into the hash table. */
786 _dl_setup_hash (GL(dl_loaded));
789 if (__builtin_expect (mode, normal) == verify)
791 /* We were called just to verify that this is a dynamic
792 executable using us as the program interpreter. Exit with an
793 error if we were not able to load the binary or no interpreter
794 is specified (i.e., this is no dynamically linked binary. */
795 if (GL(dl_loaded)->l_ld == NULL)
798 /* We allow here some platform specific code. */
799 #ifdef DISTINGUISH_LIB_VERSIONS
800 DISTINGUISH_LIB_VERSIONS;
802 _exit (has_interp ? 0 : 2);
806 /* Initialize the data structures for the search paths for shared
808 _dl_init_paths (library_path);
810 /* Put the link_map for ourselves on the chain so it can be found by
811 name. Note that at this point the global chain of link maps contains
812 exactly one element, which is pointed to by dl_loaded. */
813 if (! GL(dl_rtld_map).l_name)
814 /* If not invoked directly, the dynamic linker shared object file was
815 found by the PT_INTERP name. */
816 GL(dl_rtld_map).l_name = (char *) GL(dl_rtld_map).l_libname->name;
817 GL(dl_rtld_map).l_type = lt_library;
818 GL(dl_loaded)->l_next = &GL(dl_rtld_map);
819 GL(dl_rtld_map).l_prev = GL(dl_loaded);
822 /* We have two ways to specify objects to preload: via environment
823 variable and via the file /etc/ld.so.preload. The latter can also
824 be used when security is enabled. */
828 if (__builtin_expect (preloadlist != NULL, 0))
830 /* The LD_PRELOAD environment variable gives list of libraries
831 separated by white space or colons that are loaded before the
832 executable's dependencies and prepended to the global scope
833 list. If the binary is running setuid all elements
834 containing a '/' are ignored since it is insecure. */
835 char *list = strdupa (preloadlist);
838 HP_TIMING_NOW (start);
840 /* Prevent optimizing strsep. Speed is not important here. */
841 while ((p = (strsep) (&list, " :")) != NULL)
843 && (__builtin_expect (! INTUSE(__libc_enable_secure), 1)
844 || strchr (p, '/') == NULL))
846 struct link_map *new_map = INTUSE(_dl_map_object) (GL(dl_loaded),
850 if (++new_map->l_opencount == 1)
851 /* It is no duplicate. */
855 HP_TIMING_NOW (stop);
856 HP_TIMING_DIFF (diff, start, stop);
857 HP_TIMING_ACCUM_NT (load_time, diff);
860 /* Read the contents of the file. */
861 file = _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size,
862 PROT_READ | PROT_WRITE);
863 if (__builtin_expect (file != MAP_FAILED, 0))
865 /* Parse the file. It contains names of libraries to be loaded,
866 separated by white spaces or `:'. It may also contain
867 comments introduced by `#'. */
872 /* Eliminate comments. */
877 char *comment = memchr (runp, '#', rest);
881 rest -= comment - runp;
884 while (--rest > 0 && *++comment != '\n');
887 /* We have one problematic case: if we have a name at the end of
888 the file without a trailing terminating characters, we cannot
889 place the \0. Handle the case separately. */
890 if (file[file_size - 1] != ' ' && file[file_size - 1] != '\t'
891 && file[file_size - 1] != '\n' && file[file_size - 1] != ':')
893 problem = &file[file_size];
894 while (problem > file && problem[-1] != ' ' && problem[-1] != '\t'
895 && problem[-1] != '\n' && problem[-1] != ':')
904 file[file_size - 1] = '\0';
907 HP_TIMING_NOW (start);
913 while ((p = strsep (&runp, ": \t\n")) != NULL)
916 struct link_map *new_map = INTUSE(_dl_map_object) (GL(dl_loaded),
920 if (++new_map->l_opencount == 1)
921 /* It is no duplicate. */
928 char *p = strndupa (problem, file_size - (problem - file));
929 struct link_map *new_map = INTUSE(_dl_map_object) (GL(dl_loaded), p,
932 if (++new_map->l_opencount == 1)
933 /* It is no duplicate. */
937 HP_TIMING_NOW (stop);
938 HP_TIMING_DIFF (diff, start, stop);
939 HP_TIMING_ACCUM_NT (load_time, diff);
941 /* We don't need the file anymore. */
942 __munmap (file, file_size);
945 if (__builtin_expect (npreloads, 0) != 0)
947 /* Set up PRELOADS with a vector of the preloaded libraries. */
949 preloads = __alloca (npreloads * sizeof preloads[0]);
950 l = GL(dl_rtld_map).l_next; /* End of the chain before preloads. */
957 assert (i == npreloads);
960 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
961 specified some libraries to load, these are inserted before the actual
962 dependencies in the executable's searchlist for symbol resolution. */
963 HP_TIMING_NOW (start);
964 INTUSE(_dl_map_object_deps) (GL(dl_loaded), preloads, npreloads,
966 HP_TIMING_NOW (stop);
967 HP_TIMING_DIFF (diff, start, stop);
968 HP_TIMING_ACCUM_NT (load_time, diff);
970 /* Mark all objects as being in the global scope and set the open
972 for (i = GL(dl_loaded)->l_searchlist.r_nlist; i > 0; )
975 GL(dl_loaded)->l_searchlist.r_list[i]->l_global = 1;
976 ++GL(dl_loaded)->l_searchlist.r_list[i]->l_opencount;
980 /* We are done mapping things, so close the zero-fill descriptor. */
981 __close (_dl_zerofd);
985 /* Remove _dl_rtld_map from the chain. */
986 GL(dl_rtld_map).l_prev->l_next = GL(dl_rtld_map).l_next;
987 if (GL(dl_rtld_map).l_next)
988 GL(dl_rtld_map).l_next->l_prev = GL(dl_rtld_map).l_prev;
990 if (__builtin_expect (GL(dl_rtld_map).l_opencount > 1, 1))
992 /* Some DT_NEEDED entry referred to the interpreter object itself, so
993 put it back in the list of visible objects. We insert it into the
994 chain in symbol search order because gdb uses the chain's order as
995 its symbol search order. */
997 while (GL(dl_loaded)->l_searchlist.r_list[i] != &GL(dl_rtld_map))
999 GL(dl_rtld_map).l_prev = GL(dl_loaded)->l_searchlist.r_list[i - 1];
1000 if (__builtin_expect (mode, normal) == normal)
1001 GL(dl_rtld_map).l_next = (i + 1 < GL(dl_loaded)->l_searchlist.r_nlist
1002 ? GL(dl_loaded)->l_searchlist.r_list[i + 1]
1005 /* In trace mode there might be an invisible object (which we
1006 could not find) after the previous one in the search list.
1007 In this case it doesn't matter much where we put the
1008 interpreter object, so we just initialize the list pointer so
1009 that the assertion below holds. */
1010 GL(dl_rtld_map).l_next = GL(dl_rtld_map).l_prev->l_next;
1012 assert (GL(dl_rtld_map).l_prev->l_next == GL(dl_rtld_map).l_next);
1013 GL(dl_rtld_map).l_prev->l_next = &GL(dl_rtld_map);
1014 if (GL(dl_rtld_map).l_next != NULL)
1016 assert (GL(dl_rtld_map).l_next->l_prev == GL(dl_rtld_map).l_prev);
1017 GL(dl_rtld_map).l_next->l_prev = &GL(dl_rtld_map);
1021 /* Now let us see whether all libraries are available in the
1022 versions we need. */
1024 struct version_check_args args;
1025 args.doexit = mode == normal;
1026 args.dotrace = mode == trace;
1027 _dl_receive_error (print_missing_version, version_check_doit, &args);
1031 /* Now it is time to determine the layout of the static TLS block
1032 and allocate it for the initial thread. Note that we always
1033 allocate the static block, we never defer it even if no
1034 DF_STATIC_TLS bit is set. The reason is that we know glibc will
1035 use the static model. First add the dynamic linker to the list
1036 if it also uses TLS. */
1037 if (GL(dl_rtld_map).l_tls_blocksize != 0)
1038 /* Assign a module ID. */
1039 GL(dl_rtld_map).l_tls_modid = _dl_next_tls_modid ();
1042 /* If dynamic loading of modules with TLS is impossible we do not
1043 have to initialize any of the TLS functionality unless any of the
1044 initial modules uses TLS. */
1045 if (GL(dl_tls_max_dtv_idx) > 0)
1050 struct dtv_slotinfo *slotinfo;
1052 /* Number of elements in the static TLS block. */
1053 GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx);
1055 /* Allocate the array which contains the information about the
1056 dtv slots. We allocate a few entries more than needed to
1057 avoid the need for reallocation. */
1058 nelem = GL(dl_tls_max_dtv_idx) + 1 + TLS_SLOTINFO_SURPLUS;
1061 GL(dl_tls_dtv_slotinfo_list) = (struct dtv_slotinfo_list *)
1062 malloc (sizeof (struct dtv_slotinfo_list)
1063 + nelem * sizeof (struct dtv_slotinfo));
1064 /* No need to check the return value. If memory allocation failed
1065 the program would have been terminated. */
1067 slotinfo = memset (GL(dl_tls_dtv_slotinfo_list)->slotinfo, '\0',
1068 nelem * sizeof (struct dtv_slotinfo));
1069 GL(dl_tls_dtv_slotinfo_list)->len = nelem;
1070 GL(dl_tls_dtv_slotinfo_list)->next = NULL;
1072 /* Fill in the information from the loaded modules. */
1073 for (l = GL(dl_loaded), i = 0; l != NULL; l = l->l_next)
1074 if (l->l_tls_blocksize != 0)
1075 /* This is a module with TLS data. Store the map reference.
1076 The generation counter is zero. */
1077 slotinfo[++i].map = l;
1078 assert (i == GL(dl_tls_max_dtv_idx));
1080 /* Computer the TLS offsets for the various blocks. We call this
1081 function even if none of the modules available at startup time
1082 uses TLS to initialize some variables. */
1083 _dl_determine_tlsoffset ();
1085 /* Construct the static TLS block and the dtv for the initial
1086 thread. For some platforms this will include allocating memory
1087 for the thread descriptor. The memory for the TLS block will
1088 never be freed. It should be allocated accordingly. The dtv
1089 array can be changed if dynamic loading requires it. */
1090 tcbp = INTUSE(_dl_allocate_tls) ();
1092 _dl_fatal_printf ("\
1093 cannot allocate TLS data structures for inital thread");
1095 /* And finally install it for the main thread. */
1100 if (__builtin_expect (mode, normal) != normal)
1102 /* We were run just to list the shared libraries. It is
1103 important that we do this before real relocation, because the
1104 functions we call below for output may no longer work properly
1105 after relocation. */
1106 if (! GL(dl_loaded)->l_info[DT_NEEDED])
1107 _dl_printf ("\tstatically linked\n");
1112 if (GL(dl_debug_mask) & DL_DEBUG_PRELINK)
1114 struct r_scope_elem *scope = &GL(dl_loaded)->l_searchlist;
1116 for (i = 0; i < scope->r_nlist; i++)
1118 l = scope->r_list [i];
1121 _dl_printf ("\t%s => not found\n", l->l_libname->name);
1124 if (_dl_name_match_p (GL(dl_trace_prelink), l))
1125 GL(dl_trace_prelink_map) = l;
1126 _dl_printf ("\t%s => %s (0x%0*Zx, 0x%0*Zx)",
1127 l->l_libname->name[0] ? l->l_libname->name
1128 : rtld_progname ?: "<main program>",
1129 l->l_name[0] ? l->l_name
1130 : rtld_progname ?: "<main program>",
1131 (int) sizeof l->l_map_start * 2,
1133 (int) sizeof l->l_addr * 2,
1137 _dl_printf (" TLS(0x%Zx, 0x%0*Zx)\n", l->l_tls_modid,
1138 (int) sizeof l->l_tls_offset * 2,
1147 for (l = GL(dl_loaded)->l_next; l; l = l->l_next)
1149 /* The library was not found. */
1150 _dl_printf ("\t%s => not found\n", l->l_libname->name);
1152 _dl_printf ("\t%s => %s (0x%0*Zx)\n", l->l_libname->name,
1153 l->l_name, (int) sizeof l->l_map_start * 2,
1158 if (__builtin_expect (mode, trace) != trace)
1159 for (i = 1; i < _dl_argc; ++i)
1161 const ElfW(Sym) *ref = NULL;
1162 ElfW(Addr) loadbase;
1165 result = INTUSE(_dl_lookup_symbol) (INTUSE(_dl_argv)[i],
1167 &ref, GL(dl_loaded)->l_scope,
1168 ELF_RTYPE_CLASS_PLT, 1);
1170 loadbase = LOOKUP_VALUE_ADDRESS (result);
1172 _dl_printf ("%s found at 0x%0*Zd in object at 0x%0*Zd\n",
1173 INTUSE(_dl_argv)[i],
1174 (int) sizeof ref->st_value * 2, ref->st_value,
1175 (int) sizeof loadbase * 2, loadbase);
1179 /* If LD_WARN is set warn about undefined symbols. */
1180 if (GL(dl_lazy) >= 0 && GL(dl_verbose))
1182 /* We have to do symbol dependency testing. */
1183 struct relocate_args args;
1186 args.lazy = GL(dl_lazy);
1193 if (l != &GL(dl_rtld_map) && ! l->l_faked)
1196 _dl_receive_error (print_unresolved, relocate_doit,
1202 if ((GL(dl_debug_mask) & DL_DEBUG_PRELINK)
1203 && GL(dl_rtld_map).l_opencount > 1)
1204 INTUSE(_dl_relocate_object) (&GL(dl_rtld_map),
1205 GL(dl_loaded)->l_scope, 0, 0);
1208 #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
1211 /* Print more information. This means here, print information
1212 about the versions needed. */
1214 struct link_map *map = GL(dl_loaded);
1216 for (map = GL(dl_loaded); map != NULL; map = map->l_next)
1219 ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG];
1225 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
1226 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
1230 _dl_printf ("\n\tVersion information:\n");
1234 _dl_printf ("\t%s:\n",
1235 map->l_name[0] ? map->l_name : rtld_progname);
1240 struct link_map *needed;
1242 needed = find_needed (strtab + ent->vn_file);
1243 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
1247 const char *fname = NULL;
1250 && match_version (strtab + aux->vna_name,
1252 fname = needed->l_name;
1254 _dl_printf ("\t\t%s (%s) %s=> %s\n",
1255 strtab + ent->vn_file,
1256 strtab + aux->vna_name,
1257 aux->vna_flags & VER_FLG_WEAK
1259 fname ?: "not found");
1261 if (aux->vna_next == 0)
1262 /* No more symbols. */
1266 aux = (ElfW(Vernaux) *) ((char *) aux
1270 if (ent->vn_next == 0)
1271 /* No more dependencies. */
1274 /* Next dependency. */
1275 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
1284 if (GL(dl_loaded)->l_info [ADDRIDX (DT_GNU_LIBLIST)]
1285 && ! __builtin_expect (GL(dl_profile) != NULL, 0))
1287 ElfW(Lib) *liblist, *liblistend;
1288 struct link_map **r_list, **r_listend, *l;
1289 const char *strtab = (const void *) D_PTR (GL(dl_loaded),
1292 assert (GL(dl_loaded)->l_info [VALIDX (DT_GNU_LIBLISTSZ)] != NULL);
1293 liblist = (ElfW(Lib) *)
1294 GL(dl_loaded)->l_info [ADDRIDX (DT_GNU_LIBLIST)]->d_un.d_ptr;
1295 liblistend = (ElfW(Lib) *)
1297 + GL(dl_loaded)->l_info [VALIDX (DT_GNU_LIBLISTSZ)]->d_un.d_val);
1298 r_list = GL(dl_loaded)->l_searchlist.r_list;
1299 r_listend = r_list + GL(dl_loaded)->l_searchlist.r_nlist;
1301 for (; r_list < r_listend && liblist < liblistend; r_list++)
1305 if (l == GL(dl_loaded))
1308 /* If the library is not mapped where it should, fail. */
1312 /* Next, check if checksum matches. */
1313 if (l->l_info [VALIDX(DT_CHECKSUM)] == NULL
1314 || l->l_info [VALIDX(DT_CHECKSUM)]->d_un.d_val
1315 != liblist->l_checksum)
1318 if (l->l_info [VALIDX(DT_GNU_PRELINKED)] == NULL
1319 || l->l_info [VALIDX(DT_GNU_PRELINKED)]->d_un.d_val
1320 != liblist->l_time_stamp)
1323 if (! _dl_name_match_p (strtab + liblist->l_name, l))
1330 if (r_list == r_listend && liblist == liblistend)
1333 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_LIBS, 0))
1334 _dl_printf ("\nprelink checking: %s\n", prelinked ? "ok" : "failed");
1339 if (GL(dl_loaded)->l_info [ADDRIDX (DT_GNU_CONFLICT)] != NULL)
1341 ElfW(Rela) *conflict, *conflictend;
1342 #ifndef HP_TIMING_NONAVAIL
1347 HP_TIMING_NOW (start);
1348 assert (GL(dl_loaded)->l_info [VALIDX (DT_GNU_CONFLICTSZ)] != NULL);
1349 conflict = (ElfW(Rela) *)
1350 GL(dl_loaded)->l_info [ADDRIDX (DT_GNU_CONFLICT)]->d_un.d_ptr;
1351 conflictend = (ElfW(Rela) *)
1353 + GL(dl_loaded)->l_info [VALIDX (DT_GNU_CONFLICTSZ)]->d_un.d_val);
1354 _dl_resolve_conflicts (GL(dl_loaded), conflict, conflictend);
1355 HP_TIMING_NOW (stop);
1356 HP_TIMING_DIFF (relocate_time, start, stop);
1359 _dl_sysdep_start_cleanup ();
1363 /* Now we have all the objects loaded. Relocate them all except for
1364 the dynamic linker itself. We do this in reverse order so that copy
1365 relocs of earlier objects overwrite the data written by later
1366 objects. We do not re-relocate the dynamic linker itself in this
1367 loop because that could result in the GOT entries for functions we
1368 call being changed, and that would break us. It is safe to relocate
1369 the dynamic linker out of order because it has no copy relocs (we
1370 know that because it is self-contained). */
1373 int consider_profiling = GL(dl_profile) != NULL;
1374 #ifndef HP_TIMING_NONAVAIL
1380 /* If we are profiling we also must do lazy reloaction. */
1381 GL(dl_lazy) |= consider_profiling;
1387 HP_TIMING_NOW (start);
1390 /* While we are at it, help the memory handling a bit. We have to
1391 mark some data structures as allocated with the fake malloc()
1392 implementation in ld.so. */
1393 struct libname_list *lnp = l->l_libname->next;
1395 while (__builtin_expect (lnp != NULL, 0))
1401 if (l != &GL(dl_rtld_map))
1402 INTUSE(_dl_relocate_object) (l, l->l_scope, GL(dl_lazy),
1403 consider_profiling);
1408 HP_TIMING_NOW (stop);
1410 HP_TIMING_DIFF (relocate_time, start, stop);
1412 /* Do any necessary cleanups for the startup OS interface code.
1413 We do these now so that no calls are made after rtld re-relocation
1414 which might be resolved to different functions than we expect.
1415 We cannot do this before relocating the other objects because
1416 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
1417 _dl_sysdep_start_cleanup ();
1419 /* Now enable profiling if needed. Like the previous call,
1420 this has to go here because the calls it makes should use the
1421 rtld versions of the functions (particularly calloc()), but it
1422 needs to have _dl_profile_map set up by the relocator. */
1423 if (__builtin_expect (GL(dl_profile_map) != NULL, 0))
1424 /* We must prepare the profiling. */
1425 INTUSE(_dl_start_profile) (GL(dl_profile_map), GL(dl_profile_output));
1427 if (GL(dl_rtld_map).l_opencount > 1)
1429 /* There was an explicit ref to the dynamic linker as a shared lib.
1430 Re-relocate ourselves with user-controlled symbol definitions. */
1431 HP_TIMING_NOW (start);
1432 INTUSE(_dl_relocate_object) (&GL(dl_rtld_map), GL(dl_loaded)->l_scope,
1434 HP_TIMING_NOW (stop);
1435 HP_TIMING_DIFF (add, start, stop);
1436 HP_TIMING_ACCUM_NT (relocate_time, add);
1440 /* Now set up the variable which helps the assembler startup code. */
1441 GL(dl_main_searchlist) = &GL(dl_loaded)->l_searchlist;
1442 GL(dl_global_scope)[0] = &GL(dl_loaded)->l_searchlist;
1444 /* Save the information about the original global scope list since
1445 we need it in the memory handling later. */
1446 GL(dl_initial_searchlist) = *GL(dl_main_searchlist);
1449 /* Initialize _r_debug. */
1450 struct r_debug *r = _dl_debug_initialize (GL(dl_rtld_map).l_addr);
1455 #ifdef ELF_MACHINE_DEBUG_SETUP
1457 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
1459 ELF_MACHINE_DEBUG_SETUP (l, r);
1460 ELF_MACHINE_DEBUG_SETUP (&GL(dl_rtld_map), r);
1464 if (l->l_info[DT_DEBUG] != NULL)
1465 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
1466 with the run-time address of the r_debug structure */
1467 l->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1469 /* Fill in the pointer in the dynamic linker's own dynamic section, in
1470 case you run gdb on the dynamic linker directly. */
1471 if (GL(dl_rtld_map).l_info[DT_DEBUG] != NULL)
1472 GL(dl_rtld_map).l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1476 /* Notify the debugger that all objects are now mapped in. */
1477 r->r_state = RT_ADD;
1478 INTUSE(_dl_debug_state) ();
1482 /* We must munmap() the cache file. */
1483 INTUSE(_dl_unload_cache) ();
1486 /* Once we return, _dl_sysdep_start will invoke
1487 the DT_INIT functions and then *USER_ENTRY. */
1490 /* This is a little helper function for resolving symbols while
1491 tracing the binary. */
1493 print_unresolved (int errcode __attribute__ ((unused)), const char *objname,
1494 const char *errstring)
1496 if (objname[0] == '\0')
1497 objname = rtld_progname ?: "<main program>";
1498 _dl_error_printf ("%s (%s)\n", errstring, objname);
1501 /* This is a little helper function for resolving symbols while
1502 tracing the binary. */
1504 print_missing_version (int errcode __attribute__ ((unused)),
1505 const char *objname, const char *errstring)
1507 _dl_error_printf ("%s: %s: %s\n", rtld_progname ?: "<program name unknown>",
1508 objname, errstring);
1511 /* Nonzero if any of the debugging options is enabled. */
1512 static int any_debug;
1514 /* Process the string given as the parameter which explains which debugging
1515 options are enabled. */
1517 process_dl_debug (const char *dl_debug)
1519 /* When adding new entries make sure that the maximal length of a name
1520 is correctly handled in the LD_DEBUG_HELP code below. */
1524 const char name[10];
1525 const char helptext[41];
1526 unsigned short int mask;
1529 #define LEN_AND_STR(str) sizeof (str) - 1, str
1530 { LEN_AND_STR ("libs"), "display library search paths",
1531 DL_DEBUG_LIBS | DL_DEBUG_IMPCALLS },
1532 { LEN_AND_STR ("reloc"), "display relocation processing",
1533 DL_DEBUG_RELOC | DL_DEBUG_IMPCALLS },
1534 { LEN_AND_STR ("files"), "display progress for input file",
1535 DL_DEBUG_FILES | DL_DEBUG_IMPCALLS },
1536 { LEN_AND_STR ("symbols"), "display symbol table processing",
1537 DL_DEBUG_SYMBOLS | DL_DEBUG_IMPCALLS },
1538 { LEN_AND_STR ("bindings"), "display information about symbol binding",
1539 DL_DEBUG_BINDINGS | DL_DEBUG_IMPCALLS },
1540 { LEN_AND_STR ("versions"), "display version dependencies",
1541 DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS },
1542 { LEN_AND_STR ("all"), "all previous options combined",
1543 DL_DEBUG_LIBS | DL_DEBUG_RELOC | DL_DEBUG_FILES | DL_DEBUG_SYMBOLS
1544 | DL_DEBUG_BINDINGS | DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS },
1545 { LEN_AND_STR ("statistics"), "display relocation statistics",
1546 DL_DEBUG_STATISTICS },
1547 { LEN_AND_STR ("help"), "display this help message and exit",
1550 #define ndebopts (sizeof (debopts) / sizeof (debopts[0]))
1552 /* Skip separating white spaces and commas. */
1553 while (*dl_debug != '\0')
1555 if (*dl_debug != ' ' && *dl_debug != ',' && *dl_debug != ':')
1560 while (dl_debug[len] != '\0' && dl_debug[len] != ' '
1561 && dl_debug[len] != ',' && dl_debug[len] != ':')
1564 for (cnt = 0; cnt < ndebopts; ++cnt)
1565 if (debopts[cnt].len == len
1566 && memcmp (dl_debug, debopts[cnt].name, len) == 0)
1568 GL(dl_debug_mask) |= debopts[cnt].mask;
1573 if (cnt == ndebopts)
1575 /* Display a warning and skip everything until next
1577 char *copy = strndupa (dl_debug, len);
1578 _dl_error_printf ("\
1579 warning: debug option `%s' unknown; try LD_DEBUG=help\n", copy);
1589 if (GL(dl_debug_mask) & DL_DEBUG_HELP)
1594 Valid options for the LD_DEBUG environment variable are:\n\n");
1596 for (cnt = 0; cnt < ndebopts; ++cnt)
1597 _dl_printf (" %.*s%s%s\n", debopts[cnt].len, debopts[cnt].name,
1598 " " + debopts[cnt].len - 3,
1599 debopts[cnt].helptext);
1602 To direct the debugging output into a file instead of standard output\n\
1603 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n");
1608 /* Process all environments variables the dynamic linker must recognize.
1609 Since all of them start with `LD_' we are a bit smarter while finding
1611 extern char **_environ attribute_hidden;
1615 process_envvars (enum mode *modep)
1617 char **runp = _environ;
1619 enum mode mode = normal;
1620 char *debug_output = NULL;
1622 /* This is the default place for profiling data file. */
1623 GL(dl_profile_output)
1624 = &"/var/tmp\0/var/profile"[INTUSE(__libc_enable_secure) ? 9 : 0];
1626 while ((envline = _dl_next_ld_env_entry (&runp)) != NULL)
1630 while (envline[len] != '\0' && envline[len] != '=')
1633 if (envline[len] != '=')
1634 /* This is a "LD_" variable at the end of the string without
1635 a '=' character. Ignore it since otherwise we will access
1636 invalid memory below. */
1642 /* Warning level, verbose or not. */
1643 if (memcmp (envline, "WARN", 4) == 0)
1644 GL(dl_verbose) = envline[5] != '\0';
1648 /* Debugging of the dynamic linker? */
1649 if (memcmp (envline, "DEBUG", 5) == 0)
1650 process_dl_debug (&envline[6]);
1654 /* Print information about versions. */
1655 if (memcmp (envline, "VERBOSE", 7) == 0)
1657 version_info = envline[8] != '\0';
1661 /* List of objects to be preloaded. */
1662 if (memcmp (envline, "PRELOAD", 7) == 0)
1664 preloadlist = &envline[8];
1668 /* Which shared object shall be profiled. */
1669 if (memcmp (envline, "PROFILE", 7) == 0 && envline[8] != '\0')
1670 GL(dl_profile) = &envline[8];
1674 /* Do we bind early? */
1675 if (memcmp (envline, "BIND_NOW", 8) == 0)
1677 GL(dl_lazy) = envline[9] == '\0';
1680 if (memcmp (envline, "BIND_NOT", 8) == 0)
1681 GL(dl_bind_not) = envline[9] != '\0';
1685 /* Test whether we want to see the content of the auxiliary
1686 array passed up from the kernel. */
1687 if (memcmp (envline, "SHOW_AUXV", 9) == 0)
1692 /* Mask for the important hardware capabilities. */
1693 if (memcmp (envline, "HWCAP_MASK", 10) == 0)
1694 GL(dl_hwcap_mask) = __strtoul_internal (&envline[11], NULL, 0, 0);
1698 /* Path where the binary is found. */
1699 if (!INTUSE(__libc_enable_secure)
1700 && memcmp (envline, "ORIGIN_PATH", 11) == 0)
1701 GL(dl_origin_path) = &envline[12];
1705 /* The library search path. */
1706 if (memcmp (envline, "LIBRARY_PATH", 12) == 0)
1708 library_path = &envline[13];
1712 /* Where to place the profiling data file. */
1713 if (memcmp (envline, "DEBUG_OUTPUT", 12) == 0)
1715 debug_output = &envline[13];
1719 if (memcmp (envline, "DYNAMIC_WEAK", 12) == 0)
1720 GL(dl_dynamic_weak) = 1;
1724 /* Where to place the profiling data file. */
1725 if (!INTUSE(__libc_enable_secure)
1726 && memcmp (envline, "PROFILE_OUTPUT", 14) == 0
1727 && envline[15] != '\0')
1728 GL(dl_profile_output) = &envline[15];
1732 /* The mode of the dynamic linker can be set. */
1733 if (memcmp (envline, "TRACE_PRELINKING", 16) == 0)
1737 GL(dl_debug_mask) |= DL_DEBUG_PRELINK;
1738 GL(dl_trace_prelink) = &envline[17];
1743 /* The mode of the dynamic linker can be set. */
1744 if (memcmp (envline, "TRACE_LOADED_OBJECTS", 20) == 0)
1748 /* We might have some extra environment variable to handle. This
1749 is tricky due to the pre-processing of the length of the name
1750 in the switch statement here. The code here assumes that added
1751 environment variables have a different length. */
1752 #ifdef EXTRA_LD_ENVVARS
1758 /* The caller wants this information. */
1761 /* Extra security for SUID binaries. Remove all dangerous environment
1763 if (__builtin_expect (INTUSE(__libc_enable_secure), 0))
1765 static const char unsecure_envvars[] =
1766 #ifdef EXTRA_UNSECURE_ENVVARS
1767 EXTRA_UNSECURE_ENVVARS
1772 nextp = unsecure_envvars;
1776 /* We could use rawmemchr but this need not be fast. */
1777 nextp = (char *) (strchr) (nextp, '\0') + 1;
1779 while (*nextp != '\0');
1781 if (__access ("/etc/suid-debug", F_OK) != 0)
1782 unsetenv ("MALLOC_CHECK_");
1784 /* If we have to run the dynamic linker in debugging mode and the
1785 LD_DEBUG_OUTPUT environment variable is given, we write the debug
1786 messages to this file. */
1787 else if (any_debug && debug_output != NULL)
1790 const int flags = O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW;
1792 const int flags = O_WRONLY | O_APPEND | O_CREAT;
1794 size_t name_len = strlen (debug_output);
1795 char buf[name_len + 12];
1798 buf[name_len + 11] = '\0';
1799 startp = _itoa (__getpid (), &buf[name_len + 11], 10, 0);
1801 startp = memcpy (startp - name_len, debug_output, name_len);
1803 GL(dl_debug_fd) = __open (startp, flags, DEFFILEMODE);
1804 if (GL(dl_debug_fd) == -1)
1805 /* We use standard output if opening the file failed. */
1806 GL(dl_debug_fd) = STDOUT_FILENO;
1811 /* Print the various times we collected. */
1813 print_statistics (void)
1815 #ifndef HP_TIMING_NONAVAIL
1820 /* Total time rtld used. */
1821 if (HP_TIMING_AVAIL)
1823 HP_TIMING_PRINT (buf, sizeof (buf), rtld_total_time);
1824 INTUSE(_dl_debug_printf) ("\nruntime linker statistics:\n"
1825 " total startup time in dynamic loader: %s\n",
1829 /* Print relocation statistics. */
1830 if (HP_TIMING_AVAIL)
1833 HP_TIMING_PRINT (buf, sizeof (buf), relocate_time);
1834 cp = _itoa ((1000ULL * relocate_time) / rtld_total_time,
1835 pbuf + sizeof (pbuf), 10, 0);
1837 switch (pbuf + sizeof (pbuf) - cp)
1848 INTUSE(_dl_debug_printf) ("\
1849 time needed for relocation: %s (%s%%)\n",
1853 INTUSE(_dl_debug_printf) (" number of relocations: %lu\n",
1854 GL(dl_num_relocations));
1855 INTUSE(_dl_debug_printf) (" number of relocations from cache: %lu\n",
1856 GL(dl_num_cache_relocations));
1858 #ifndef HP_TIMING_NONAVAIL
1859 /* Time spend while loading the object and the dependencies. */
1860 if (HP_TIMING_AVAIL)
1863 HP_TIMING_PRINT (buf, sizeof (buf), load_time);
1864 cp = _itoa ((1000ULL * load_time) / rtld_total_time,
1865 pbuf + sizeof (pbuf), 10, 0);
1867 switch (pbuf + sizeof (pbuf) - cp)
1878 INTUSE(_dl_debug_printf) ("\
1879 time needed to load objects: %s (%s%%)\n",