Update.
[glibc.git] / elf / rtld.c
blobe4c2c6e0ada3a3e84176d76bbb81422185e8e441
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
18 02111-1307 USA. */
20 #include <fcntl.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/mman.h> /* Check if MAP_ANON is defined. */
25 #include <sys/param.h>
26 #include <sys/stat.h>
27 #include <ldsodefs.h>
28 #include <stdio-common/_itoa.h>
29 #include <entry.h>
30 #include <fpu_control.h>
31 #include <hp-timing.h>
32 #include <bits/libc-lock.h>
33 #include "dynamic-link.h"
34 #include "dl-librecon.h"
35 #include <unsecvars.h>
36 #include <dl-cache.h>
37 #include <dl-procinfo.h>
39 #include <assert.h>
41 /* Helper function to handle errors while resolving symbols. */
42 static void print_unresolved (int errcode, const char *objname,
43 const char *errsting);
45 /* Helper function to handle errors when a version is missing. */
46 static void print_missing_version (int errcode, const char *objname,
47 const char *errsting);
49 /* Print the various times we collected. */
50 static void print_statistics (void);
52 /* This is a list of all the modes the dynamic loader can be in. */
53 enum mode { normal, list, verify, trace };
55 /* Process all environments variables the dynamic linker must recognize.
56 Since all of them start with `LD_' we are a bit smarter while finding
57 all the entries. */
58 static void process_envvars (enum mode *modep);
60 int _dl_argc;
61 char **_dl_argv;
62 unsigned int _dl_skip_args; /* Nonzero if we were run directly. */
64 /* Set nonzero during loading and initialization of executable and
65 libraries, cleared before the executable's entry point runs. This
66 must not be initialized to nonzero, because the unused dynamic
67 linker loaded in for libc.so's "ld.so.1" dep will provide the
68 definition seen by libc.so's initializer; that value must be zero,
69 and will be since that dynamic linker's _dl_start and dl_main will
70 never be called. */
71 int _dl_starting_up;
73 /* This is the structure which defines all variables global to ld.so
74 (except those which cannot be added for some reason). */
75 struct rtld_global _rtld_global =
77 ._dl_debug_fd = STDERR_FILENO,
78 #if 1
79 /* XXX I know about at least one case where we depend on the old
80 weak behavior (it has to do with librt). Until we get DSO
81 groups implemented we have to make this the default.
82 Bummer. --drepper */
83 ._dl_dynamic_weak = 1,
84 #endif
85 ._dl_lazy = 1,
86 ._dl_fpu_control = _FPU_DEFAULT,
87 ._dl_correct_cache_id = _DL_CACHE_DEFAULT_ID,
88 ._dl_hwcap_mask = HWCAP_IMPORTANT,
89 ._dl_load_lock = _LIBC_LOCK_RECURSIVE_INITIALIZER
91 /* There must only be the definition in ld.so itself. */
92 #ifdef HAVE_PROTECTED
93 asm (".protected _rtld_global");
94 #endif
97 static void dl_main (const ElfW(Phdr) *phdr,
98 ElfW(Word) phnum,
99 ElfW(Addr) *user_entry);
101 static struct libname_list _dl_rtld_libname;
102 static struct libname_list _dl_rtld_libname2;
104 /* We expect less than a second for relocation. */
105 #ifdef HP_SMALL_TIMING_AVAIL
106 # undef HP_TIMING_AVAIL
107 # define HP_TIMING_AVAIL HP_SMALL_TIMING_AVAIL
108 #endif
110 /* Variable for statistics. */
111 #ifndef HP_TIMING_NONAVAIL
112 static hp_timing_t rtld_total_time;
113 static hp_timing_t relocate_time;
114 static hp_timing_t load_time;
115 #endif
117 static ElfW(Addr) _dl_start_final (void *arg, struct link_map *bootstrap_map_p,
118 hp_timing_t start_time);
120 #ifdef RTLD_START
121 RTLD_START
122 #else
123 # error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
124 #endif
126 static ElfW(Addr) __attribute_used__
127 _dl_start (void *arg)
129 struct link_map bootstrap_map;
130 hp_timing_t start_time;
131 #if !__GNUC_PREREQ (2, 96)
132 size_t cnt;
133 #endif
135 /* This #define produces dynamic linking inline functions for
136 bootstrap relocation instead of general-purpose relocation. */
137 #define RTLD_BOOTSTRAP
138 #define RESOLVE_MAP(sym, version, flags) \
139 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : &bootstrap_map)
140 #define RESOLVE(sym, version, flags) \
141 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : bootstrap_map.l_addr)
142 #include "dynamic-link.h"
144 if (HP_TIMING_INLINE && HP_TIMING_AVAIL)
145 HP_TIMING_NOW (start_time);
147 /* Partly clean the `bootstrap_map' structure up. Don't use
148 `memset' since it might not be built in or inlined and we cannot
149 make function calls at this point. Use '__builtin_memset' if we
150 know it is available. */
151 #if __GNUC_PREREQ (2, 96)
152 __builtin_memset (bootstrap_map.l_info, '\0', sizeof (bootstrap_map.l_info));
153 #else
154 for (cnt = 0;
155 cnt < sizeof (bootstrap_map.l_info) / sizeof (bootstrap_map.l_info[0]);
156 ++cnt)
157 bootstrap_map.l_info[cnt] = 0;
158 #endif
160 /* Figure out the run-time load address of the dynamic linker itself. */
161 bootstrap_map.l_addr = elf_machine_load_address ();
163 /* Read our own dynamic section and fill in the info array. */
164 bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
165 elf_get_dynamic_info (&bootstrap_map);
167 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
168 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info);
169 #endif
171 if (bootstrap_map.l_addr || ! bootstrap_map.l_info[VALIDX(DT_GNU_PRELINKED)])
173 /* Relocate ourselves so we can do normal function calls and
174 data access using the global offset table. */
176 ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0, 0);
179 /* Please note that we don't allow profiling of this object and
180 therefore need not test whether we have to allocate the array
181 for the relocation results (as done in dl-reloc.c). */
183 /* Now life is sane; we can call functions and access global data.
184 Set up to use the operating system facilities, and find out from
185 the operating system's program loader where to find the program
186 header table in core. Put the rest of _dl_start into a separate
187 function, that way the compiler cannot put accesses to the GOT
188 before ELF_DYNAMIC_RELOCATE. */
190 ElfW(Addr) entry = _dl_start_final (arg, &bootstrap_map, start_time);
192 #ifndef ELF_MACHINE_START_ADDRESS
193 # define ELF_MACHINE_START_ADDRESS(map, start) (start)
194 #endif
196 return ELF_MACHINE_START_ADDRESS (GL(dl_loaded), entry);
201 #ifndef VALIDX
202 # define VALIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
203 + DT_EXTRANUM + DT_VALTAGIDX (tag))
204 #endif
205 #ifndef ADDRIDX
206 # define ADDRIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
207 + DT_EXTRANUM + DT_VALNUM + DT_ADDRTAGIDX (tag))
208 #endif
210 static ElfW(Addr)
211 _dl_start_final (void *arg, struct link_map *bootstrap_map_p,
212 hp_timing_t start_time)
214 /* The use of `alloca' here looks ridiculous but it helps. The goal
215 is to avoid the function from being inlined. There is no official
216 way to do this so we use this trick. gcc never inlines functions
217 which use `alloca'. */
218 ElfW(Addr) *start_addr = alloca (sizeof (ElfW(Addr)));
219 extern char _begin[], _end[];
221 if (HP_TIMING_AVAIL)
223 /* If it hasn't happen yet record the startup time. */
224 if (! HP_TIMING_INLINE)
225 HP_TIMING_NOW (start_time);
227 /* Initialize the timing functions. */
228 HP_TIMING_DIFF_INIT ();
231 /* Transfer data about ourselves to the permanent link_map structure. */
232 GL(dl_rtld_map).l_addr = bootstrap_map_p->l_addr;
233 GL(dl_rtld_map).l_ld = bootstrap_map_p->l_ld;
234 GL(dl_rtld_map).l_opencount = 1;
235 memcpy (GL(dl_rtld_map).l_info, bootstrap_map_p->l_info,
236 sizeof GL(dl_rtld_map).l_info);
237 _dl_setup_hash (&GL(dl_rtld_map));
238 GL(dl_rtld_map).l_mach = bootstrap_map_p->l_mach;
239 GL(dl_rtld_map).l_map_start = (ElfW(Addr)) _begin;
240 GL(dl_rtld_map).l_map_end = (ElfW(Addr)) _end;
242 /* Call the OS-dependent function to set up life so we can do things like
243 file access. It will call `dl_main' (below) to do all the real work
244 of the dynamic linker, and then unwind our frame and run the user
245 entry point on the same stack we entered on. */
246 *start_addr = _dl_sysdep_start (arg, &dl_main);
247 #ifndef HP_TIMING_NONAVAIL
248 if (HP_TIMING_AVAIL)
250 hp_timing_t end_time;
252 /* Get the current time. */
253 HP_TIMING_NOW (end_time);
255 /* Compute the difference. */
256 HP_TIMING_DIFF (rtld_total_time, start_time, end_time);
258 #endif
260 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_STATISTICS, 0))
261 print_statistics ();
263 return *start_addr;
266 /* Now life is peachy; we can do all normal operations.
267 On to the real work. */
269 /* Some helper functions. */
271 /* Arguments to relocate_doit. */
272 struct relocate_args
274 struct link_map *l;
275 int lazy;
278 struct map_args
280 /* Argument to map_doit. */
281 char *str;
282 /* Return value of map_doit. */
283 struct link_map *main_map;
286 /* Arguments to version_check_doit. */
287 struct version_check_args
289 int doexit;
290 int dotrace;
293 static void
294 relocate_doit (void *a)
296 struct relocate_args *args = (struct relocate_args *) a;
298 _dl_relocate_object (args->l, args->l->l_scope,
299 args->lazy, 0);
302 static void
303 map_doit (void *a)
305 struct map_args *args = (struct map_args *) a;
306 args->main_map = _dl_map_object (NULL, args->str, 0, lt_library, 0, 0);
309 static void
310 version_check_doit (void *a)
312 struct version_check_args *args = (struct version_check_args *) a;
313 if (_dl_check_all_versions (GL(dl_loaded), 1, args->dotrace) && args->doexit)
314 /* We cannot start the application. Abort now. */
315 _exit (1);
319 static inline struct link_map *
320 find_needed (const char *name)
322 unsigned int n = GL(dl_loaded)->l_searchlist.r_nlist;
324 while (n-- > 0)
325 if (_dl_name_match_p (name, GL(dl_loaded)->l_searchlist.r_list[n]))
326 return GL(dl_loaded)->l_searchlist.r_list[n];
328 /* Should never happen. */
329 return NULL;
332 static int
333 match_version (const char *string, struct link_map *map)
335 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
336 ElfW(Verdef) *def;
338 #define VERDEFTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
339 if (map->l_info[VERDEFTAG] == NULL)
340 /* The file has no symbol versioning. */
341 return 0;
343 def = (ElfW(Verdef) *) ((char *) map->l_addr
344 + map->l_info[VERDEFTAG]->d_un.d_ptr);
345 while (1)
347 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
349 /* Compare the version strings. */
350 if (strcmp (string, strtab + aux->vda_name) == 0)
351 /* Bingo! */
352 return 1;
354 /* If no more definitions we failed to find what we want. */
355 if (def->vd_next == 0)
356 break;
358 /* Next definition. */
359 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
362 return 0;
365 static const char *library_path; /* The library search path. */
366 static const char *preloadlist; /* The list preloaded objects. */
367 static int version_info; /* Nonzero if information about
368 versions has to be printed. */
370 static void
371 dl_main (const ElfW(Phdr) *phdr,
372 ElfW(Word) phnum,
373 ElfW(Addr) *user_entry)
375 const ElfW(Phdr) *ph;
376 enum mode mode;
377 struct link_map **preloads;
378 unsigned int npreloads;
379 size_t file_size;
380 char *file;
381 int has_interp = 0;
382 unsigned int i;
383 int prelinked = 0;
384 int rtld_is_main = 0;
385 #ifndef HP_TIMING_NONAVAIL
386 hp_timing_t start;
387 hp_timing_t stop;
388 hp_timing_t diff;
389 #endif
391 /* Process the environment variable which control the behaviour. */
392 process_envvars (&mode);
394 /* Set up a flag which tells we are just starting. */
395 _dl_starting_up = 1;
397 if (*user_entry == (ElfW(Addr)) ENTRY_POINT)
399 /* Ho ho. We are not the program interpreter! We are the program
400 itself! This means someone ran ld.so as a command. Well, that
401 might be convenient to do sometimes. We support it by
402 interpreting the args like this:
404 ld.so PROGRAM ARGS...
406 The first argument is the name of a file containing an ELF
407 executable we will load and run with the following arguments.
408 To simplify life here, PROGRAM is searched for using the
409 normal rules for shared objects, rather than $PATH or anything
410 like that. We just load it and use its entry point; we don't
411 pay attention to its PT_INTERP command (we are the interpreter
412 ourselves). This is an easy way to test a new ld.so before
413 installing it. */
414 rtld_is_main = 1;
416 /* Note the place where the dynamic linker actually came from. */
417 GL(dl_rtld_map).l_name = _dl_argv[0];
419 while (_dl_argc > 1)
420 if (! strcmp (_dl_argv[1], "--list"))
422 mode = list;
423 GL(dl_lazy) = -1; /* This means do no dependency analysis. */
425 ++_dl_skip_args;
426 --_dl_argc;
427 ++_dl_argv;
429 else if (! strcmp (_dl_argv[1], "--verify"))
431 mode = verify;
433 ++_dl_skip_args;
434 --_dl_argc;
435 ++_dl_argv;
437 else if (! strcmp (_dl_argv[1], "--library-path") && _dl_argc > 2)
439 library_path = _dl_argv[2];
441 _dl_skip_args += 2;
442 _dl_argc -= 2;
443 _dl_argv += 2;
445 else if (! strcmp (_dl_argv[1], "--inhibit-rpath") && _dl_argc > 2)
447 GL(dl_inhibit_rpath) = _dl_argv[2];
449 _dl_skip_args += 2;
450 _dl_argc -= 2;
451 _dl_argv += 2;
453 else
454 break;
456 /* If we have no further argument the program was called incorrectly.
457 Grant the user some education. */
458 if (_dl_argc < 2)
459 _dl_fatal_printf ("\
460 Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
461 You have invoked `ld.so', the helper program for shared library executables.\n\
462 This program usually lives in the file `/lib/ld.so', and special directives\n\
463 in executable files using ELF shared libraries tell the system's program\n\
464 loader to load the helper program from this file. This helper program loads\n\
465 the shared libraries needed by the program executable, prepares the program\n\
466 to run, and runs it. You may invoke this helper program directly from the\n\
467 command line to load and run an ELF executable file; this is like executing\n\
468 that file itself, but always uses this helper program from the file you\n\
469 specified, instead of the helper program file specified in the executable\n\
470 file you run. This is mostly of use for maintainers to test new versions\n\
471 of this helper program; chances are you did not intend to run this program.\n\
473 --list list all dependencies and how they are resolved\n\
474 --verify verify that given object really is a dynamically linked\n\
475 object we can handle\n\
476 --library-path PATH use given PATH instead of content of the environment\n\
477 variable LD_LIBRARY_PATH\n\
478 --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\
479 in LIST\n");
481 ++_dl_skip_args;
482 --_dl_argc;
483 ++_dl_argv;
485 /* Initialize the data structures for the search paths for shared
486 objects. */
487 _dl_init_paths (library_path);
489 if (__builtin_expect (mode, normal) == verify)
491 const char *objname;
492 const char *err_str = NULL;
493 struct map_args args;
495 args.str = _dl_argv[0];
496 (void) _dl_catch_error (&objname, &err_str, map_doit, &args);
497 if (__builtin_expect (err_str != NULL, 0))
499 if (err_str != _dl_out_of_memory)
500 free ((char *) err_str);
501 _exit (EXIT_FAILURE);
504 else
506 HP_TIMING_NOW (start);
507 _dl_map_object (NULL, _dl_argv[0], 0, lt_library, 0, 0);
508 HP_TIMING_NOW (stop);
510 HP_TIMING_DIFF (load_time, start, stop);
513 phdr = GL(dl_loaded)->l_phdr;
514 phnum = GL(dl_loaded)->l_phnum;
515 /* We overwrite here a pointer to a malloc()ed string. But since
516 the malloc() implementation used at this point is the dummy
517 implementations which has no real free() function it does not
518 makes sense to free the old string first. */
519 GL(dl_loaded)->l_name = (char *) "";
520 *user_entry = GL(dl_loaded)->l_entry;
522 else
524 /* Create a link_map for the executable itself.
525 This will be what dlopen on "" returns. */
526 _dl_new_object ((char *) "", "", lt_executable, NULL);
527 if (GL(dl_loaded) == NULL)
528 _dl_fatal_printf ("cannot allocate memory for link map\n");
529 GL(dl_loaded)->l_phdr = phdr;
530 GL(dl_loaded)->l_phnum = phnum;
531 GL(dl_loaded)->l_entry = *user_entry;
533 /* At this point we are in a bit of trouble. We would have to
534 fill in the values for l_dev and l_ino. But in general we
535 do not know where the file is. We also do not handle AT_EXECFD
536 even if it would be passed up.
538 We leave the values here defined to 0. This is normally no
539 problem as the program code itself is normally no shared
540 object and therefore cannot be loaded dynamically. Nothing
541 prevent the use of dynamic binaries and in these situations
542 we might get problems. We might not be able to find out
543 whether the object is already loaded. But since there is no
544 easy way out and because the dynamic binary must also not
545 have an SONAME we ignore this program for now. If it becomes
546 a problem we can force people using SONAMEs. */
548 /* We delay initializing the path structure until we got the dynamic
549 information for the program. */
552 GL(dl_loaded)->l_map_end = 0;
553 /* Perhaps the executable has no PT_LOAD header entries at all. */
554 GL(dl_loaded)->l_map_start = ~0;
555 /* We opened the file, account for it. */
556 ++GL(dl_loaded)->l_opencount;
558 /* Scan the program header table for the dynamic section. */
559 for (ph = phdr; ph < &phdr[phnum]; ++ph)
560 switch (ph->p_type)
562 case PT_PHDR:
563 /* Find out the load address. */
564 GL(dl_loaded)->l_addr = (ElfW(Addr)) phdr - ph->p_vaddr;
565 break;
566 case PT_DYNAMIC:
567 /* This tells us where to find the dynamic section,
568 which tells us everything we need to do. */
569 GL(dl_loaded)->l_ld = (void *) GL(dl_loaded)->l_addr + ph->p_vaddr;
570 break;
571 case PT_INTERP:
572 /* This "interpreter segment" was used by the program loader to
573 find the program interpreter, which is this program itself, the
574 dynamic linker. We note what name finds us, so that a future
575 dlopen call or DT_NEEDED entry, for something that wants to link
576 against the dynamic linker as a shared library, will know that
577 the shared object is already loaded. */
578 _dl_rtld_libname.name = ((const char *) GL(dl_loaded)->l_addr
579 + ph->p_vaddr);
580 /* _dl_rtld_libname.next = NULL; Already zero. */
581 GL(dl_rtld_map).l_libname = &_dl_rtld_libname;
583 /* Ordinarilly, we would get additional names for the loader from
584 our DT_SONAME. This can't happen if we were actually linked as
585 a static executable (detect this case when we have no DYNAMIC).
586 If so, assume the filename component of the interpreter path to
587 be our SONAME, and add it to our name list. */
588 if (GL(dl_rtld_map).l_ld == NULL)
590 char *p = strrchr (_dl_rtld_libname.name, '/');
591 if (p)
593 _dl_rtld_libname2.name = p+1;
594 /* _dl_rtld_libname2.next = NULL; Already zero. */
595 _dl_rtld_libname.next = &_dl_rtld_libname2;
599 has_interp = 1;
600 break;
601 case PT_LOAD:
603 ElfW(Addr) mapstart;
604 ElfW(Addr) allocend;
606 /* Remember where the main program starts in memory. */
607 mapstart = (GL(dl_loaded)->l_addr
608 + (ph->p_vaddr & ~(ph->p_align - 1)));
609 if (GL(dl_loaded)->l_map_start > mapstart)
610 GL(dl_loaded)->l_map_start = mapstart;
612 /* Also where it ends. */
613 allocend = GL(dl_loaded)->l_addr + ph->p_vaddr + ph->p_memsz;
614 if (GL(dl_loaded)->l_map_end < allocend)
615 GL(dl_loaded)->l_map_end = allocend;
617 break;
619 if (! GL(dl_loaded)->l_map_end)
620 GL(dl_loaded)->l_map_end = ~0;
621 if (! GL(dl_rtld_map).l_libname && GL(dl_rtld_map).l_name)
623 /* We were invoked directly, so the program might not have a
624 PT_INTERP. */
625 _dl_rtld_libname.name = GL(dl_rtld_map).l_name;
626 /* _dl_rtld_libname.next = NULL; Alread zero. */
627 GL(dl_rtld_map).l_libname = &_dl_rtld_libname;
629 else
630 assert (GL(dl_rtld_map).l_libname); /* How else did we get here? */
632 if (! rtld_is_main)
634 /* Extract the contents of the dynamic section for easy access. */
635 elf_get_dynamic_info (GL(dl_loaded));
636 if (GL(dl_loaded)->l_info[DT_HASH])
637 /* Set up our cache of pointers into the hash table. */
638 _dl_setup_hash (GL(dl_loaded));
641 if (__builtin_expect (mode, normal) == verify)
643 /* We were called just to verify that this is a dynamic
644 executable using us as the program interpreter. Exit with an
645 error if we were not able to load the binary or no interpreter
646 is specified (i.e., this is no dynamically linked binary. */
647 if (GL(dl_loaded)->l_ld == NULL)
648 _exit (1);
650 /* We allow here some platform specific code. */
651 #ifdef DISTINGUISH_LIB_VERSIONS
652 DISTINGUISH_LIB_VERSIONS;
653 #endif
654 _exit (has_interp ? 0 : 2);
657 if (! rtld_is_main)
658 /* Initialize the data structures for the search paths for shared
659 objects. */
660 _dl_init_paths (library_path);
662 /* Put the link_map for ourselves on the chain so it can be found by
663 name. Note that at this point the global chain of link maps contains
664 exactly one element, which is pointed to by dl_loaded. */
665 if (! GL(dl_rtld_map).l_name)
666 /* If not invoked directly, the dynamic linker shared object file was
667 found by the PT_INTERP name. */
668 GL(dl_rtld_map).l_name = (char *) GL(dl_rtld_map).l_libname->name;
669 GL(dl_rtld_map).l_type = lt_library;
670 GL(dl_loaded)->l_next = &GL(dl_rtld_map);
671 GL(dl_rtld_map).l_prev = GL(dl_loaded);
672 ++GL(dl_nloaded);
674 /* We have two ways to specify objects to preload: via environment
675 variable and via the file /etc/ld.so.preload. The latter can also
676 be used when security is enabled. */
677 preloads = NULL;
678 npreloads = 0;
680 if (__builtin_expect (preloadlist != NULL, 0))
682 /* The LD_PRELOAD environment variable gives list of libraries
683 separated by white space or colons that are loaded before the
684 executable's dependencies and prepended to the global scope
685 list. If the binary is running setuid all elements
686 containing a '/' are ignored since it is insecure. */
687 char *list = strdupa (preloadlist);
688 char *p;
690 HP_TIMING_NOW (start);
692 while ((p = strsep (&list, " :")) != NULL)
693 if (p[0] != '\0'
694 && (__builtin_expect (! __libc_enable_secure, 1)
695 || strchr (p, '/') == NULL))
697 struct link_map *new_map = _dl_map_object (GL(dl_loaded), p, 1,
698 lt_library, 0, 0);
699 if (++new_map->l_opencount == 1)
700 /* It is no duplicate. */
701 ++npreloads;
704 HP_TIMING_NOW (stop);
705 HP_TIMING_DIFF (diff, start, stop);
706 HP_TIMING_ACCUM_NT (load_time, diff);
709 /* Read the contents of the file. */
710 file = _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size,
711 PROT_READ | PROT_WRITE);
712 if (__builtin_expect (file != MAP_FAILED, 0))
714 /* Parse the file. It contains names of libraries to be loaded,
715 separated by white spaces or `:'. It may also contain
716 comments introduced by `#'. */
717 char *problem;
718 char *runp;
719 size_t rest;
721 /* Eliminate comments. */
722 runp = file;
723 rest = file_size;
724 while (rest > 0)
726 char *comment = memchr (runp, '#', rest);
727 if (comment == NULL)
728 break;
730 rest -= comment - runp;
732 *comment = ' ';
733 while (--rest > 0 && *++comment != '\n');
736 /* We have one problematic case: if we have a name at the end of
737 the file without a trailing terminating characters, we cannot
738 place the \0. Handle the case separately. */
739 if (file[file_size - 1] != ' ' && file[file_size - 1] != '\t'
740 && file[file_size - 1] != '\n' && file[file_size - 1] != ':')
742 problem = &file[file_size];
743 while (problem > file && problem[-1] != ' ' && problem[-1] != '\t'
744 && problem[-1] != '\n' && problem[-1] != ':')
745 --problem;
747 if (problem > file)
748 problem[-1] = '\0';
750 else
752 problem = NULL;
753 file[file_size - 1] = '\0';
756 HP_TIMING_NOW (start);
758 if (file != problem)
760 char *p;
761 runp = file;
762 while ((p = strsep (&runp, ": \t\n")) != NULL)
763 if (p[0] != '\0')
765 struct link_map *new_map = _dl_map_object (GL(dl_loaded), p, 1,
766 lt_library, 0, 0);
767 if (++new_map->l_opencount == 1)
768 /* It is no duplicate. */
769 ++npreloads;
773 if (problem != NULL)
775 char *p = strndupa (problem, file_size - (problem - file));
776 struct link_map *new_map = _dl_map_object (GL(dl_loaded), p, 1,
777 lt_library, 0, 0);
778 if (++new_map->l_opencount == 1)
779 /* It is no duplicate. */
780 ++npreloads;
783 HP_TIMING_NOW (stop);
784 HP_TIMING_DIFF (diff, start, stop);
785 HP_TIMING_ACCUM_NT (load_time, diff);
787 /* We don't need the file anymore. */
788 __munmap (file, file_size);
791 if (__builtin_expect (npreloads, 0) != 0)
793 /* Set up PRELOADS with a vector of the preloaded libraries. */
794 struct link_map *l;
795 preloads = __alloca (npreloads * sizeof preloads[0]);
796 l = GL(dl_rtld_map).l_next; /* End of the chain before preloads. */
797 i = 0;
800 preloads[i++] = l;
801 l = l->l_next;
802 } while (l);
803 assert (i == npreloads);
806 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
807 specified some libraries to load, these are inserted before the actual
808 dependencies in the executable's searchlist for symbol resolution. */
809 HP_TIMING_NOW (start);
810 _dl_map_object_deps (GL(dl_loaded), preloads, npreloads, mode == trace);
811 HP_TIMING_NOW (stop);
812 HP_TIMING_DIFF (diff, start, stop);
813 HP_TIMING_ACCUM_NT (load_time, diff);
815 /* Mark all objects as being in the global scope and set the open
816 counter. */
817 for (i = GL(dl_loaded)->l_searchlist.r_nlist; i > 0; )
819 --i;
820 GL(dl_loaded)->l_searchlist.r_list[i]->l_global = 1;
821 ++GL(dl_loaded)->l_searchlist.r_list[i]->l_opencount;
824 #ifndef MAP_ANON
825 /* We are done mapping things, so close the zero-fill descriptor. */
826 __close (_dl_zerofd);
827 _dl_zerofd = -1;
828 #endif
830 /* Remove _dl_rtld_map from the chain. */
831 GL(dl_rtld_map).l_prev->l_next = GL(dl_rtld_map).l_next;
832 if (GL(dl_rtld_map).l_next)
833 GL(dl_rtld_map).l_next->l_prev = GL(dl_rtld_map).l_prev;
835 if (__builtin_expect (GL(dl_rtld_map).l_opencount > 1, 1))
837 /* Some DT_NEEDED entry referred to the interpreter object itself, so
838 put it back in the list of visible objects. We insert it into the
839 chain in symbol search order because gdb uses the chain's order as
840 its symbol search order. */
841 i = 1;
842 while (GL(dl_loaded)->l_searchlist.r_list[i] != &GL(dl_rtld_map))
843 ++i;
844 GL(dl_rtld_map).l_prev = GL(dl_loaded)->l_searchlist.r_list[i - 1];
845 if (__builtin_expect (mode, normal) == normal)
846 GL(dl_rtld_map).l_next = (i + 1 < GL(dl_loaded)->l_searchlist.r_nlist
847 ? GL(dl_loaded)->l_searchlist.r_list[i + 1]
848 : NULL);
849 else
850 /* In trace mode there might be an invisible object (which we
851 could not find) after the previous one in the search list.
852 In this case it doesn't matter much where we put the
853 interpreter object, so we just initialize the list pointer so
854 that the assertion below holds. */
855 GL(dl_rtld_map).l_next = GL(dl_rtld_map).l_prev->l_next;
857 assert (GL(dl_rtld_map).l_prev->l_next == GL(dl_rtld_map).l_next);
858 GL(dl_rtld_map).l_prev->l_next = &GL(dl_rtld_map);
859 if (GL(dl_rtld_map).l_next)
861 assert (GL(dl_rtld_map).l_next->l_prev == GL(dl_rtld_map).l_prev);
862 GL(dl_rtld_map).l_next->l_prev = &GL(dl_rtld_map);
866 /* Now let us see whether all libraries are available in the
867 versions we need. */
869 struct version_check_args args;
870 args.doexit = mode == normal;
871 args.dotrace = mode == trace;
872 _dl_receive_error (print_missing_version, version_check_doit, &args);
875 if (__builtin_expect (mode, normal) != normal)
877 /* We were run just to list the shared libraries. It is
878 important that we do this before real relocation, because the
879 functions we call below for output may no longer work properly
880 after relocation. */
881 if (! GL(dl_loaded)->l_info[DT_NEEDED])
882 _dl_printf ("\tstatically linked\n");
883 else
885 struct link_map *l;
887 if (GL(dl_debug_mask) & DL_DEBUG_PRELINK)
889 struct r_scope_elem *scope = &GL(dl_loaded)->l_searchlist;
891 for (i = 0; i < scope->r_nlist; i++)
893 l = scope->r_list [i];
894 if (l->l_faked)
896 _dl_printf ("\t%s => not found\n", l->l_libname->name);
897 continue;
899 if (_dl_name_match_p (GL(dl_trace_prelink), l))
900 GL(dl_trace_prelink_map) = l;
901 _dl_printf ("\t%s => %s (0x%0*Zx, 0x%0*Zx)\n",
902 l->l_libname->name[0] ? l->l_libname->name
903 : _dl_argv[0] ?: "<main program>",
904 l->l_name[0] ? l->l_name
905 : _dl_argv[0] ?: "<main program>",
906 (int) sizeof l->l_map_start * 2,
907 l->l_map_start,
908 (int) sizeof l->l_addr * 2,
909 l->l_addr);
912 else
914 for (l = GL(dl_loaded)->l_next; l; l = l->l_next)
915 if (l->l_faked)
916 /* The library was not found. */
917 _dl_printf ("\t%s => not found\n", l->l_libname->name);
918 else
919 _dl_printf ("\t%s => %s (0x%0*Zx)\n", l->l_libname->name,
920 l->l_name, (int) sizeof l->l_map_start * 2,
921 l->l_map_start);
925 if (__builtin_expect (mode, trace) != trace)
926 for (i = 1; i < _dl_argc; ++i)
928 const ElfW(Sym) *ref = NULL;
929 ElfW(Addr) loadbase;
930 lookup_t result;
932 result = _dl_lookup_symbol (_dl_argv[i], GL(dl_loaded),
933 &ref, GL(dl_loaded)->l_scope,
934 ELF_RTYPE_CLASS_PLT, 1);
936 loadbase = LOOKUP_VALUE_ADDRESS (result);
938 _dl_printf ("%s found at 0x%0*Zd in object at 0x%0*Zd\n",
939 _dl_argv[i],
940 (int) sizeof ref->st_value * 2, ref->st_value,
941 (int) sizeof loadbase * 2, loadbase);
943 else
945 /* If LD_WARN is set warn about undefined symbols. */
946 if (GL(dl_lazy) >= 0 && GL(dl_verbose))
948 /* We have to do symbol dependency testing. */
949 struct relocate_args args;
950 struct link_map *l;
952 args.lazy = GL(dl_lazy);
954 l = GL(dl_loaded);
955 while (l->l_next)
956 l = l->l_next;
959 if (l != &GL(dl_rtld_map) && ! l->l_faked)
961 args.l = l;
962 _dl_receive_error (print_unresolved, relocate_doit,
963 &args);
965 l = l->l_prev;
966 } while (l);
968 if ((GL(dl_debug_mask) & DL_DEBUG_PRELINK)
969 && GL(dl_rtld_map).l_opencount > 1)
970 _dl_relocate_object (&GL(dl_rtld_map), GL(dl_loaded)->l_scope,
971 0, 0);
974 #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
975 if (version_info)
977 /* Print more information. This means here, print information
978 about the versions needed. */
979 int first = 1;
980 struct link_map *map = GL(dl_loaded);
982 for (map = GL(dl_loaded); map != NULL; map = map->l_next)
984 const char *strtab;
985 ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG];
986 ElfW(Verneed) *ent;
988 if (dyn == NULL)
989 continue;
991 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
992 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
994 if (first)
996 _dl_printf ("\n\tVersion information:\n");
997 first = 0;
1000 _dl_printf ("\t%s:\n",
1001 map->l_name[0] ? map->l_name : _dl_argv[0]);
1003 while (1)
1005 ElfW(Vernaux) *aux;
1006 struct link_map *needed;
1008 needed = find_needed (strtab + ent->vn_file);
1009 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
1011 while (1)
1013 const char *fname = NULL;
1015 if (needed != NULL
1016 && match_version (strtab + aux->vna_name,
1017 needed))
1018 fname = needed->l_name;
1020 _dl_printf ("\t\t%s (%s) %s=> %s\n",
1021 strtab + ent->vn_file,
1022 strtab + aux->vna_name,
1023 aux->vna_flags & VER_FLG_WEAK
1024 ? "[WEAK] " : "",
1025 fname ?: "not found");
1027 if (aux->vna_next == 0)
1028 /* No more symbols. */
1029 break;
1031 /* Next symbol. */
1032 aux = (ElfW(Vernaux) *) ((char *) aux
1033 + aux->vna_next);
1036 if (ent->vn_next == 0)
1037 /* No more dependencies. */
1038 break;
1040 /* Next dependency. */
1041 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
1047 _exit (0);
1050 if (GL(dl_loaded)->l_info [ADDRIDX (DT_GNU_LIBLIST)]
1051 && ! __builtin_expect (GL(dl_profile) != NULL, 0))
1053 ElfW(Lib) *liblist, *liblistend;
1054 struct link_map **r_list, **r_listend, *l;
1055 const char *strtab = (const void *)
1056 D_PTR (GL(dl_loaded), l_info[DT_STRTAB]);
1058 assert (GL(dl_loaded)->l_info [VALIDX (DT_GNU_LIBLISTSZ)] != NULL);
1059 liblist = (ElfW(Lib) *)
1060 GL(dl_loaded)->l_info [ADDRIDX (DT_GNU_LIBLIST)]->d_un.d_ptr;
1061 liblistend = (ElfW(Lib) *)
1062 ((char *) liblist
1063 + GL(dl_loaded)->l_info [VALIDX (DT_GNU_LIBLISTSZ)]->d_un.d_val);
1064 r_list = GL(dl_loaded)->l_searchlist.r_list;
1065 r_listend = r_list + GL(dl_loaded)->l_searchlist.r_nlist;
1067 for (; r_list < r_listend && liblist < liblistend; r_list++)
1069 l = *r_list;
1071 if (l == GL(dl_loaded))
1072 continue;
1074 /* If the library is not mapped where it should, fail. */
1075 if (l->l_addr)
1076 break;
1078 /* Next, check if checksum matches. */
1079 if (l->l_info [VALIDX(DT_CHECKSUM)] == NULL
1080 || l->l_info [VALIDX(DT_CHECKSUM)]->d_un.d_val
1081 != liblist->l_checksum)
1082 break;
1084 if (l->l_info [VALIDX(DT_GNU_PRELINKED)] == NULL
1085 || l->l_info [VALIDX(DT_GNU_PRELINKED)]->d_un.d_val
1086 != liblist->l_time_stamp)
1087 break;
1089 if (! _dl_name_match_p (strtab + liblist->l_name, l))
1090 break;
1092 ++liblist;
1096 if (r_list == r_listend && liblist == liblistend)
1097 prelinked = 1;
1099 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_LIBS, 0))
1100 _dl_printf ("\nprelink checking: %s\n", prelinked ? "ok" : "failed");
1103 if (prelinked)
1105 if (GL(dl_loaded)->l_info [ADDRIDX (DT_GNU_CONFLICT)] != NULL)
1107 ElfW(Rela) *conflict, *conflictend;
1108 #ifndef HP_TIMING_NONAVAIL
1109 hp_timing_t start;
1110 hp_timing_t stop;
1111 #endif
1113 HP_TIMING_NOW (start);
1114 assert (GL(dl_loaded)->l_info [VALIDX (DT_GNU_CONFLICTSZ)] != NULL);
1115 conflict = (ElfW(Rela) *)
1116 GL(dl_loaded)->l_info [ADDRIDX (DT_GNU_CONFLICT)]->d_un.d_ptr;
1117 conflictend = (ElfW(Rela) *)
1118 ((char *) conflict
1119 + GL(dl_loaded)->l_info [VALIDX (DT_GNU_CONFLICTSZ)]->d_un.d_val);
1120 _dl_resolve_conflicts (GL(dl_loaded), conflict, conflictend);
1121 HP_TIMING_NOW (stop);
1122 HP_TIMING_DIFF (relocate_time, start, stop);
1125 _dl_sysdep_start_cleanup ();
1127 else
1129 /* Now we have all the objects loaded. Relocate them all except for
1130 the dynamic linker itself. We do this in reverse order so that copy
1131 relocs of earlier objects overwrite the data written by later
1132 objects. We do not re-relocate the dynamic linker itself in this
1133 loop because that could result in the GOT entries for functions we
1134 call being changed, and that would break us. It is safe to relocate
1135 the dynamic linker out of order because it has no copy relocs (we
1136 know that because it is self-contained). */
1138 struct link_map *l;
1139 int consider_profiling = GL(dl_profile) != NULL;
1140 #ifndef HP_TIMING_NONAVAIL
1141 hp_timing_t start;
1142 hp_timing_t stop;
1143 hp_timing_t add;
1144 #endif
1146 /* If we are profiling we also must do lazy reloaction. */
1147 GL(dl_lazy) |= consider_profiling;
1149 l = GL(dl_loaded);
1150 while (l->l_next)
1151 l = l->l_next;
1153 HP_TIMING_NOW (start);
1156 /* While we are at it, help the memory handling a bit. We have to
1157 mark some data structures as allocated with the fake malloc()
1158 implementation in ld.so. */
1159 struct libname_list *lnp = l->l_libname->next;
1161 while (__builtin_expect (lnp != NULL, 0))
1163 lnp->dont_free = 1;
1164 lnp = lnp->next;
1167 if (l != &GL(dl_rtld_map))
1168 _dl_relocate_object (l, l->l_scope, GL(dl_lazy), consider_profiling);
1170 l = l->l_prev;
1172 while (l);
1173 HP_TIMING_NOW (stop);
1175 HP_TIMING_DIFF (relocate_time, start, stop);
1177 /* Do any necessary cleanups for the startup OS interface code.
1178 We do these now so that no calls are made after rtld re-relocation
1179 which might be resolved to different functions than we expect.
1180 We cannot do this before relocating the other objects because
1181 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
1182 _dl_sysdep_start_cleanup ();
1184 /* Now enable profiling if needed. Like the previous call,
1185 this has to go here because the calls it makes should use the
1186 rtld versions of the functions (particularly calloc()), but it
1187 needs to have _dl_profile_map set up by the relocator. */
1188 if (__builtin_expect (GL(dl_profile_map) != NULL, 0))
1189 /* We must prepare the profiling. */
1190 _dl_start_profile (GL(dl_profile_map), GL(dl_profile_output));
1192 if (GL(dl_rtld_map).l_opencount > 1)
1194 /* There was an explicit ref to the dynamic linker as a shared lib.
1195 Re-relocate ourselves with user-controlled symbol definitions. */
1196 HP_TIMING_NOW (start);
1197 _dl_relocate_object (&GL(dl_rtld_map), GL(dl_loaded)->l_scope, 0, 0);
1198 HP_TIMING_NOW (stop);
1199 HP_TIMING_DIFF (add, start, stop);
1200 HP_TIMING_ACCUM_NT (relocate_time, add);
1204 /* Now set up the variable which helps the assembler startup code. */
1205 GL(dl_main_searchlist) = &GL(dl_loaded)->l_searchlist;
1206 GL(dl_global_scope)[0] = &GL(dl_loaded)->l_searchlist;
1208 /* Save the information about the original global scope list since
1209 we need it in the memory handling later. */
1210 GL(dl_initial_searchlist) = *GL(dl_main_searchlist);
1213 /* Initialize _r_debug. */
1214 struct r_debug *r = _dl_debug_initialize (GL(dl_rtld_map).l_addr);
1215 struct link_map *l;
1217 l = GL(dl_loaded);
1219 #ifdef ELF_MACHINE_DEBUG_SETUP
1221 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
1223 ELF_MACHINE_DEBUG_SETUP (l, r);
1224 ELF_MACHINE_DEBUG_SETUP (&GL(dl_rtld_map), r);
1226 #else
1228 if (l->l_info[DT_DEBUG])
1229 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
1230 with the run-time address of the r_debug structure */
1231 l->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1233 /* Fill in the pointer in the dynamic linker's own dynamic section, in
1234 case you run gdb on the dynamic linker directly. */
1235 if (GL(dl_rtld_map).l_info[DT_DEBUG])
1236 GL(dl_rtld_map).l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1238 #endif
1240 /* Notify the debugger that all objects are now mapped in. */
1241 r->r_state = RT_ADD;
1242 _dl_debug_state ();
1245 #ifndef MAP_COPY
1246 /* We must munmap() the cache file. */
1247 _dl_unload_cache ();
1248 #endif
1250 /* Once we return, _dl_sysdep_start will invoke
1251 the DT_INIT functions and then *USER_ENTRY. */
1254 /* This is a little helper function for resolving symbols while
1255 tracing the binary. */
1256 static void
1257 print_unresolved (int errcode __attribute__ ((unused)), const char *objname,
1258 const char *errstring)
1260 if (objname[0] == '\0')
1261 objname = _dl_argv[0] ?: "<main program>";
1262 _dl_error_printf ("%s (%s)\n", errstring, objname);
1265 /* This is a little helper function for resolving symbols while
1266 tracing the binary. */
1267 static void
1268 print_missing_version (int errcode __attribute__ ((unused)),
1269 const char *objname, const char *errstring)
1271 _dl_error_printf ("%s: %s: %s\n", _dl_argv[0] ?: "<program name unknown>",
1272 objname, errstring);
1275 /* Nonzero if any of the debugging options is enabled. */
1276 static int any_debug;
1278 /* Process the string given as the parameter which explains which debugging
1279 options are enabled. */
1280 static void
1281 process_dl_debug (const char *dl_debug)
1283 /* When adding new entries make sure that the maximal length of a name
1284 is correctly handled in the LD_DEBUG_HELP code below. */
1285 static const struct
1287 const char name[11];
1288 const char helptext[41];
1289 unsigned short int mask;
1290 } debopts[] =
1292 { "libs", "display library search paths",
1293 DL_DEBUG_LIBS | DL_DEBUG_IMPCALLS },
1294 { "reloc", "display relocation processing",
1295 DL_DEBUG_RELOC | DL_DEBUG_IMPCALLS },
1296 { "files", "display progress for input file",
1297 DL_DEBUG_FILES | DL_DEBUG_IMPCALLS },
1298 { "symbols", "display symbol table processing",
1299 DL_DEBUG_SYMBOLS | DL_DEBUG_IMPCALLS },
1300 { "bindings", "display information about symbol binding",
1301 DL_DEBUG_BINDINGS | DL_DEBUG_IMPCALLS },
1302 { "versions", "display version dependencies",
1303 DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS },
1304 { "all", "all previous options combined",
1305 DL_DEBUG_LIBS | DL_DEBUG_RELOC | DL_DEBUG_FILES | DL_DEBUG_SYMBOLS
1306 | DL_DEBUG_BINDINGS | DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS },
1307 { "statistics", "display relocation statistics",
1308 DL_DEBUG_STATISTICS },
1309 { "help", "display this help message and exit",
1310 DL_DEBUG_HELP },
1312 #define ndebopts (sizeof (debopts) / sizeof (debopts[0]))
1313 size_t len;
1315 #define separators " ,:"
1318 len = 0;
1319 /* Skip separating white spaces and commas. */
1320 dl_debug += strspn (dl_debug, separators);
1321 if (*dl_debug != '\0')
1323 size_t cnt;
1325 len = strcspn (dl_debug, separators);
1327 for (cnt = 0; cnt < ndebopts; ++cnt)
1328 if (strncmp (dl_debug, debopts[cnt].name, len) == 0
1329 && debopts[cnt].name[len] == '\0')
1331 GL(dl_debug_mask) |= debopts[cnt].mask;
1332 any_debug = 1;
1333 break;
1336 if (cnt == ndebopts)
1338 /* Display a warning and skip everything until next
1339 separator. */
1340 char *copy = strndupa (dl_debug, len);
1341 _dl_error_printf ("\
1342 warning: debug option `%s' unknown; try LD_DEBUG=help\n", copy);
1343 break;
1347 while (*(dl_debug += len) != '\0');
1349 if (GL(dl_debug_mask) & DL_DEBUG_HELP)
1351 size_t cnt;
1353 _dl_printf ("\
1354 Valid options for the LD_DEBUG environment variable are:\n\n");
1356 for (cnt = 0; cnt < ndebopts; ++cnt)
1357 _dl_printf (" %s%s %s\n", debopts[cnt].name,
1358 " " + strlen (debopts[cnt].name) - 3,
1359 debopts[cnt].helptext);
1361 _dl_printf ("\n\
1362 To direct the debugging output into a file instead of standard output\n\
1363 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n");
1364 _exit (0);
1368 /* Process all environments variables the dynamic linker must recognize.
1369 Since all of them start with `LD_' we are a bit smarter while finding
1370 all the entries. */
1371 extern char **_environ;
1374 static void
1375 process_envvars (enum mode *modep)
1377 char **runp = _environ;
1378 char *envline;
1379 enum mode mode = normal;
1380 char *debug_output = NULL;
1382 /* This is the default place for profiling data file. */
1383 GL(dl_profile_output) = &"/var/tmp\0/var/profile"[__libc_enable_secure
1384 ? 9 : 0];
1386 while ((envline = _dl_next_ld_env_entry (&runp)) != NULL)
1388 size_t len = strcspn (envline, "=");
1390 if (envline[len] != '=')
1391 /* This is a "LD_" variable at the end of the string without
1392 a '=' character. Ignore it since otherwise we will access
1393 invalid memory below. */
1394 continue;
1396 switch (len)
1398 case 4:
1399 /* Warning level, verbose or not. */
1400 if (memcmp (envline, "WARN", 4) == 0)
1401 GL(dl_verbose) = envline[5] != '\0';
1402 break;
1404 case 5:
1405 /* Debugging of the dynamic linker? */
1406 if (memcmp (envline, "DEBUG", 5) == 0)
1407 process_dl_debug (&envline[6]);
1408 break;
1410 case 7:
1411 /* Print information about versions. */
1412 if (memcmp (envline, "VERBOSE", 7) == 0)
1414 version_info = envline[8] != '\0';
1415 break;
1418 /* List of objects to be preloaded. */
1419 if (memcmp (envline, "PRELOAD", 7) == 0)
1421 preloadlist = &envline[8];
1422 break;
1425 /* Which shared object shall be profiled. */
1426 if (memcmp (envline, "PROFILE", 7) == 0 && envline[8] != '\0')
1427 GL(dl_profile) = &envline[8];
1428 break;
1430 case 8:
1431 /* Do we bind early? */
1432 if (memcmp (envline, "BIND_NOW", 8) == 0)
1434 GL(dl_lazy) = envline[9] == '\0';
1435 break;
1437 if (memcmp (envline, "BIND_NOT", 8) == 0)
1438 GL(dl_bind_not) = envline[9] != '\0';
1439 break;
1441 case 9:
1442 /* Test whether we want to see the content of the auxiliary
1443 array passed up from the kernel. */
1444 if (memcmp (envline, "SHOW_AUXV", 9) == 0)
1445 _dl_show_auxv ();
1446 break;
1448 case 10:
1449 /* Mask for the important hardware capabilities. */
1450 if (memcmp (envline, "HWCAP_MASK", 10) == 0)
1451 GL(dl_hwcap_mask) = __strtoul_internal (&envline[11], NULL, 0, 0);
1452 break;
1454 case 11:
1455 /* Path where the binary is found. */
1456 if (!__libc_enable_secure
1457 && memcmp (envline, "ORIGIN_PATH", 11) == 0)
1458 GL(dl_origin_path) = &envline[12];
1459 break;
1461 case 12:
1462 /* The library search path. */
1463 if (memcmp (envline, "LIBRARY_PATH", 12) == 0)
1465 library_path = &envline[13];
1466 break;
1469 /* Where to place the profiling data file. */
1470 if (memcmp (envline, "DEBUG_OUTPUT", 12) == 0)
1472 debug_output = &envline[13];
1473 break;
1476 if (memcmp (envline, "DYNAMIC_WEAK", 12) == 0)
1477 GL(dl_dynamic_weak) = 1;
1478 break;
1480 case 14:
1481 /* Where to place the profiling data file. */
1482 if (!__libc_enable_secure
1483 && memcmp (envline, "PROFILE_OUTPUT", 14) == 0
1484 && envline[15] != '\0')
1485 GL(dl_profile_output) = &envline[15];
1486 break;
1488 case 16:
1489 /* The mode of the dynamic linker can be set. */
1490 if (memcmp (envline, "TRACE_PRELINKING", 16) == 0)
1492 mode = trace;
1493 GL(dl_verbose) = 1;
1494 GL(dl_debug_mask) |= DL_DEBUG_PRELINK;
1495 GL(dl_trace_prelink) = &envline[17];
1497 break;
1499 case 20:
1500 /* The mode of the dynamic linker can be set. */
1501 if (memcmp (envline, "TRACE_LOADED_OBJECTS", 20) == 0)
1502 mode = trace;
1503 break;
1505 /* We might have some extra environment variable to handle. This
1506 is tricky due to the pre-processing of the length of the name
1507 in the switch statement here. The code here assumes that added
1508 environment variables have a different length. */
1509 #ifdef EXTRA_LD_ENVVARS
1510 EXTRA_LD_ENVVARS
1511 #endif
1515 /* The caller wants this information. */
1516 *modep = mode;
1518 /* Extra security for SUID binaries. Remove all dangerous environment
1519 variables. */
1520 if (__builtin_expect (__libc_enable_secure, 0))
1522 static const char unsecure_envvars[] =
1523 #ifdef EXTRA_UNSECURE_ENVVARS
1524 EXTRA_UNSECURE_ENVVARS
1525 #endif
1526 UNSECURE_ENVVARS;
1527 const char *nextp;
1529 nextp = unsecure_envvars;
1532 unsetenv (nextp);
1533 nextp = (char *) rawmemchr (nextp, '\0') + 1;
1535 while (*nextp != '\0');
1537 if (__access ("/etc/suid-debug", F_OK) != 0)
1538 unsetenv ("MALLOC_CHECK_");
1540 /* If we have to run the dynamic linker in debugging mode and the
1541 LD_DEBUG_OUTPUT environment variable is given, we write the debug
1542 messages to this file. */
1543 else if (any_debug && debug_output != NULL)
1545 #ifdef O_NOFOLLOW
1546 const int flags = O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW;
1547 #else
1548 const int flags = O_WRONLY | O_APPEND | O_CREAT;
1549 #endif
1550 size_t name_len = strlen (debug_output);
1551 char buf[name_len + 12];
1552 char *startp;
1554 buf[name_len + 11] = '\0';
1555 startp = _itoa_word (__getpid (), &buf[name_len + 11], 10, 0);
1556 *--startp = '.';
1557 startp = memcpy (startp - name_len, debug_output, name_len);
1559 GL(dl_debug_fd) = __open (startp, flags, DEFFILEMODE);
1560 if (GL(dl_debug_fd) == -1)
1561 /* We use standard output if opening the file failed. */
1562 GL(dl_debug_fd) = STDOUT_FILENO;
1567 /* Print the various times we collected. */
1568 static void
1569 print_statistics (void)
1571 #ifndef HP_TIMING_NONAVAIL
1572 char buf[200];
1573 char *cp;
1574 char *wp;
1576 /* Total time rtld used. */
1577 if (HP_TIMING_AVAIL)
1579 HP_TIMING_PRINT (buf, sizeof (buf), rtld_total_time);
1580 _dl_debug_printf ("\nruntime linker statistics:\n"
1581 " total startup time in dynamic loader: %s\n", buf);
1584 /* Print relocation statistics. */
1585 if (HP_TIMING_AVAIL)
1587 char pbuf[30];
1588 HP_TIMING_PRINT (buf, sizeof (buf), relocate_time);
1589 cp = _itoa_word ((1000ULL * relocate_time) / rtld_total_time,
1590 pbuf + sizeof (pbuf), 10, 0);
1591 wp = pbuf;
1592 switch (pbuf + sizeof (pbuf) - cp)
1594 case 3:
1595 *wp++ = *cp++;
1596 case 2:
1597 *wp++ = *cp++;
1598 case 1:
1599 *wp++ = '.';
1600 *wp++ = *cp++;
1602 *wp = '\0';
1603 _dl_debug_printf (" time needed for relocation: %s (%s%%)\n",
1604 buf, pbuf);
1606 #endif
1607 _dl_debug_printf (" number of relocations: %lu\n",
1608 GL(dl_num_relocations));
1609 _dl_debug_printf (" number of relocations from cache: %lu\n",
1610 GL(dl_num_cache_relocations));
1612 #ifndef HP_TIMING_NONAVAIL
1613 /* Time spend while loading the object and the dependencies. */
1614 if (HP_TIMING_AVAIL)
1616 char pbuf[30];
1617 HP_TIMING_PRINT (buf, sizeof (buf), load_time);
1618 cp = _itoa_word ((1000ULL * load_time) / rtld_total_time,
1619 pbuf + sizeof (pbuf), 10, 0);
1620 wp = pbuf;
1621 switch (pbuf + sizeof (pbuf) - cp)
1623 case 3:
1624 *wp++ = *cp++;
1625 case 2:
1626 *wp++ = *cp++;
1627 case 1:
1628 *wp++ = '.';
1629 *wp++ = *cp++;
1631 *wp = '\0';
1632 _dl_debug_printf (" time needed to load objects: %s (%s%%)\n",
1633 buf, pbuf);
1635 #endif