Update.
[glibc.git] / elf / rtld.c
blob1d144c536e952642a0b7292a7a7e9825d44db726
1 /* Run time dynamic linker.
2 Copyright (C) 1995-1999, 2000 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 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 <ldsodefs.h>
26 #include <stdio-common/_itoa.h>
27 #include <entry.h>
28 #include <fpu_control.h>
29 #include <hp-timing.h>
30 #include <bits/libc-lock.h>
31 #include "dynamic-link.h"
32 #include "dl-librecon.h"
34 #include <assert.h>
36 /* System-specific function to do initial startup for the dynamic linker.
37 After this, file access calls and getenv must work. This is responsible
38 for setting __libc_enable_secure if we need to be secure (e.g. setuid),
39 and for setting _dl_argc and _dl_argv, and then calling _dl_main. */
40 extern ElfW(Addr) _dl_sysdep_start (void **start_argptr,
41 void (*dl_main) (const ElfW(Phdr) *phdr,
42 ElfW(Half) phent,
43 ElfW(Addr) *user_entry));
44 extern void _dl_sysdep_start_cleanup (void);
46 /* This function is used to unload the cache file if necessary. */
47 extern void _dl_unload_cache (void);
49 /* System-dependent function to read a file's whole contents
50 in the most convenient manner available. */
51 extern void *_dl_sysdep_read_whole_file (const char *filename,
52 size_t *filesize_ptr,
53 int mmap_prot);
55 /* Helper function to handle errors while resolving symbols. */
56 static void print_unresolved (int errcode, const char *objname,
57 const char *errsting);
59 /* Helper function to handle errors when a version is missing. */
60 static void print_missing_version (int errcode, const char *objname,
61 const char *errsting);
63 /* Print the various times we collected. */
64 static void print_statistics (void);
66 /* This is a list of all the modes the dynamic loader can be in. */
67 enum mode { normal, list, verify, trace };
69 /* Process all environments variables the dynamic linker must recognize.
70 Since all of them start with `LD_' we are a bit smarter while finding
71 all the entries. */
72 static void process_envvars (enum mode *modep, int *lazyp);
74 int _dl_argc;
75 char **_dl_argv;
76 unsigned int _dl_skip_args; /* Nonzero if we were run directly. */
77 int _dl_verbose;
78 const char *_dl_platform;
79 size_t _dl_platformlen;
80 unsigned long _dl_hwcap;
81 fpu_control_t _dl_fpu_control = _FPU_DEFAULT;
82 struct r_search_path *_dl_search_paths;
83 const char *_dl_profile;
84 const char *_dl_profile_output;
85 struct link_map *_dl_profile_map;
86 int _dl_lazy;
87 int _dl_dynamic_weak;
88 int _dl_debug_libs;
89 int _dl_debug_impcalls;
90 int _dl_debug_bindings;
91 int _dl_debug_symbols;
92 int _dl_debug_versions;
93 int _dl_debug_reloc;
94 int _dl_debug_files;
95 int _dl_debug_statistics;
96 const char *_dl_inhibit_rpath; /* RPATH values which should be
97 ignored. */
98 const char *_dl_origin_path;
99 int _dl_bind_not;
101 /* This is a pointer to the map for the main object and through it to
102 all loaded objects. */
103 struct link_map *_dl_loaded;
104 /* Pointer to the l_searchlist element of the link map of the main object. */
105 struct r_scope_elem *_dl_main_searchlist;
106 /* Copy of the content of `_dl_main_searchlist'. */
107 struct r_scope_elem _dl_initial_searchlist;
108 /* Array which is used when looking up in the global scope. */
109 struct r_scope_elem *_dl_global_scope[2];
111 /* During the program run we must not modify the global data of
112 loaded shared object simultanously in two threads. Therefore we
113 protect `_dl_open' and `_dl_close' in dl-close.c.
115 This must be a recursive lock since the initializer function of
116 the loaded object might as well require a call to this function.
117 At this time it is not anymore a problem to modify the tables. */
118 __libc_lock_define_initialized_recursive (, _dl_load_lock)
120 /* Set nonzero during loading and initialization of executable and
121 libraries, cleared before the executable's entry point runs. This
122 must not be initialized to nonzero, because the unused dynamic
123 linker loaded in for libc.so's "ld.so.1" dep will provide the
124 definition seen by libc.so's initializer; that value must be zero,
125 and will be since that dynamic linker's _dl_start and dl_main will
126 never be called. */
127 int _dl_starting_up;
130 static void dl_main (const ElfW(Phdr) *phdr,
131 ElfW(Half) phent,
132 ElfW(Addr) *user_entry);
134 struct link_map _dl_rtld_map;
135 struct libname_list _dl_rtld_libname;
136 struct libname_list _dl_rtld_libname2;
138 /* Variable for statistics. */
139 #ifndef HP_TIMING_NONAVAIL
140 static hp_timing_t rtld_total_time;
141 static hp_timing_t relocate_time;
142 static hp_timing_t load_time;
143 #endif
144 extern unsigned long int _dl_num_relocations; /* in dl-lookup.c */
146 static ElfW(Addr) _dl_start_final (void *arg, struct link_map *bootstrap_map_p,
147 hp_timing_t start_time);
149 #ifdef RTLD_START
150 RTLD_START
151 #else
152 #error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
153 #endif
155 static ElfW(Addr)
156 _dl_start (void *arg)
158 struct link_map bootstrap_map;
159 hp_timing_t start_time;
160 size_t cnt;
162 /* This #define produces dynamic linking inline functions for
163 bootstrap relocation instead of general-purpose relocation. */
164 #define RTLD_BOOTSTRAP
165 #define RESOLVE_MAP(sym, version, flags) \
166 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : &bootstrap_map)
167 #define RESOLVE(sym, version, flags) \
168 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : bootstrap_map.l_addr)
169 #include "dynamic-link.h"
171 if (HP_TIMING_INLINE && HP_TIMING_AVAIL)
172 HP_TIMING_NOW (start_time);
174 /* Partly clean the `bootstrap_map' structure up. Don't use `memset'
175 since it might nor be built in or inlined and we cannot make function
176 calls at this point. */
177 for (cnt = 0;
178 cnt < sizeof (bootstrap_map.l_info) / sizeof (bootstrap_map.l_info[0]);
179 ++cnt)
180 bootstrap_map.l_info[cnt] = 0;
182 /* Figure out the run-time load address of the dynamic linker itself. */
183 bootstrap_map.l_addr = elf_machine_load_address ();
185 /* Read our own dynamic section and fill in the info array. */
186 bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
187 elf_get_dynamic_info (&bootstrap_map);
189 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
190 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info);
191 #endif
193 /* Relocate ourselves so we can do normal function calls and
194 data access using the global offset table. */
196 ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0, 0);
197 /* Please note that we don't allow profiling of this object and
198 therefore need not test whether we have to allocate the array
199 for the relocation results (as done in dl-reloc.c). */
201 /* Now life is sane; we can call functions and access global data.
202 Set up to use the operating system facilities, and find out from
203 the operating system's program loader where to find the program
204 header table in core. Put the rest of _dl_start into a separate
205 function, that way the compiler cannot put accesses to the GOT
206 before ELF_DYNAMIC_RELOCATE. */
208 ElfW(Addr) entry = _dl_start_final (arg, &bootstrap_map, start_time);
210 #ifndef ELF_MACHINE_START_ADDRESS
211 # define ELF_MACHINE_START_ADDRESS(map, start) (start)
212 #endif
214 return ELF_MACHINE_START_ADDRESS (_dl_loaded, entry);
219 static ElfW(Addr)
220 _dl_start_final (void *arg, struct link_map *bootstrap_map_p,
221 hp_timing_t start_time)
223 /* The use of `alloca' here looks ridiculous but it helps. The goal
224 is to avoid the function from being inlined. There is no official
225 way to do this so we use this trick. gcc never inlines functions
226 which use `alloca'. */
227 ElfW(Addr) *start_addr = alloca (sizeof (ElfW(Addr)));
229 if (HP_TIMING_AVAIL)
231 /* If it hasn't happen yet record the startup time. */
232 if (! HP_TIMING_INLINE)
233 HP_TIMING_NOW (start_time);
235 /* Initialize the timing functions. */
236 HP_TIMING_DIFF_INIT ();
239 /* Transfer data about ourselves to the permanent link_map structure. */
240 _dl_rtld_map.l_addr = bootstrap_map_p->l_addr;
241 _dl_rtld_map.l_ld = bootstrap_map_p->l_ld;
242 _dl_rtld_map.l_opencount = 1;
243 memcpy (_dl_rtld_map.l_info, bootstrap_map_p->l_info,
244 sizeof _dl_rtld_map.l_info);
245 _dl_setup_hash (&_dl_rtld_map);
247 /* Don't bother trying to work out how ld.so is mapped in memory. */
248 _dl_rtld_map.l_map_start = ~0;
249 _dl_rtld_map.l_map_end = ~0;
251 /* Call the OS-dependent function to set up life so we can do things like
252 file access. It will call `dl_main' (below) to do all the real work
253 of the dynamic linker, and then unwind our frame and run the user
254 entry point on the same stack we entered on. */
255 *start_addr = _dl_sysdep_start (arg, &dl_main);
256 #ifndef HP_TIMING_NONAVAIL
257 if (HP_TIMING_AVAIL)
259 hp_timing_t end_time;
261 /* Get the current time. */
262 HP_TIMING_NOW (end_time);
264 /* Compute the difference. */
265 HP_TIMING_DIFF (rtld_total_time, start_time, end_time);
267 #endif
269 if (__builtin_expect (_dl_debug_statistics, 0))
270 print_statistics ();
272 return *start_addr;
275 /* Now life is peachy; we can do all normal operations.
276 On to the real work. */
278 void ENTRY_POINT (void);
280 /* Some helper functions. */
282 /* Arguments to relocate_doit. */
283 struct relocate_args
285 struct link_map *l;
286 int lazy;
289 struct map_args
291 /* Argument to map_doit. */
292 char *str;
293 /* Return value of map_doit. */
294 struct link_map *main_map;
297 /* Arguments to version_check_doit. */
298 struct version_check_args
300 int doexit;
301 int dotrace;
304 static void
305 relocate_doit (void *a)
307 struct relocate_args *args = (struct relocate_args *) a;
309 _dl_relocate_object (args->l, args->l->l_scope,
310 args->lazy, 0);
313 static void
314 map_doit (void *a)
316 struct map_args *args = (struct map_args *) a;
317 args->main_map = _dl_map_object (NULL, args->str, 0, lt_library, 0, 0);
320 static void
321 version_check_doit (void *a)
323 struct version_check_args *args = (struct version_check_args *) a;
324 if (_dl_check_all_versions (_dl_loaded, 1, args->dotrace) && args->doexit)
325 /* We cannot start the application. Abort now. */
326 _exit (1);
330 static inline struct link_map *
331 find_needed (const char *name)
333 unsigned int n = _dl_loaded->l_searchlist.r_nlist;
335 while (n-- > 0)
336 if (_dl_name_match_p (name, _dl_loaded->l_searchlist.r_list[n]))
337 return _dl_loaded->l_searchlist.r_list[n];
339 /* Should never happen. */
340 return NULL;
343 static int
344 match_version (const char *string, struct link_map *map)
346 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
347 ElfW(Verdef) *def;
349 #define VERDEFTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
350 if (map->l_info[VERDEFTAG] == NULL)
351 /* The file has no symbol versioning. */
352 return 0;
354 def = (ElfW(Verdef) *) ((char *) map->l_addr
355 + map->l_info[VERDEFTAG]->d_un.d_ptr);
356 while (1)
358 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
360 /* Compare the version strings. */
361 if (strcmp (string, strtab + aux->vda_name) == 0)
362 /* Bingo! */
363 return 1;
365 /* If no more definitions we failed to find what we want. */
366 if (def->vd_next == 0)
367 break;
369 /* Next definition. */
370 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
373 return 0;
376 static const char *library_path; /* The library search path. */
377 static const char *preloadlist; /* The list preloaded objects. */
378 static int version_info; /* Nonzero if information about
379 versions has to be printed. */
381 static void
382 dl_main (const ElfW(Phdr) *phdr,
383 ElfW(Half) phent,
384 ElfW(Addr) *user_entry)
386 const ElfW(Phdr) *ph;
387 enum mode mode;
388 struct link_map **preloads;
389 unsigned int npreloads;
390 size_t file_size;
391 char *file;
392 int has_interp = 0;
393 unsigned int i;
394 int rtld_is_main = 0;
395 #ifndef HP_TIMING_NONAVAIL
396 hp_timing_t start;
397 hp_timing_t stop;
398 hp_timing_t diff;
399 #endif
401 /* Process the environment variable which control the behaviour. */
402 process_envvars (&mode, &_dl_lazy);
404 /* Set up a flag which tells we are just starting. */
405 _dl_starting_up = 1;
407 if (*user_entry == (ElfW(Addr)) &ENTRY_POINT)
409 /* Ho ho. We are not the program interpreter! We are the program
410 itself! This means someone ran ld.so as a command. Well, that
411 might be convenient to do sometimes. We support it by
412 interpreting the args like this:
414 ld.so PROGRAM ARGS...
416 The first argument is the name of a file containing an ELF
417 executable we will load and run with the following arguments.
418 To simplify life here, PROGRAM is searched for using the
419 normal rules for shared objects, rather than $PATH or anything
420 like that. We just load it and use its entry point; we don't
421 pay attention to its PT_INTERP command (we are the interpreter
422 ourselves). This is an easy way to test a new ld.so before
423 installing it. */
424 rtld_is_main = 1;
426 /* Note the place where the dynamic linker actually came from. */
427 _dl_rtld_map.l_name = _dl_argv[0];
429 while (_dl_argc > 1)
430 if (! strcmp (_dl_argv[1], "--list"))
432 mode = list;
433 _dl_lazy = -1; /* This means do no dependency analysis. */
435 ++_dl_skip_args;
436 --_dl_argc;
437 ++_dl_argv;
439 else if (! strcmp (_dl_argv[1], "--verify"))
441 mode = verify;
443 ++_dl_skip_args;
444 --_dl_argc;
445 ++_dl_argv;
447 else if (! strcmp (_dl_argv[1], "--library-path") && _dl_argc > 2)
449 library_path = _dl_argv[2];
451 _dl_skip_args += 2;
452 _dl_argc -= 2;
453 _dl_argv += 2;
455 else if (! strcmp (_dl_argv[1], "--inhibit-rpath") && _dl_argc > 2)
457 _dl_inhibit_rpath = _dl_argv[2];
459 _dl_skip_args += 2;
460 _dl_argc -= 2;
461 _dl_argv += 2;
463 else
464 break;
466 /* If we have no further argument the program was called incorrectly.
467 Grant the user some education. */
468 if (_dl_argc < 2)
469 _dl_sysdep_fatal ("\
470 Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
471 You have invoked `ld.so', the helper program for shared library executables.\n\
472 This program usually lives in the file `/lib/ld.so', and special directives\n\
473 in executable files using ELF shared libraries tell the system's program\n\
474 loader to load the helper program from this file. This helper program loads\n\
475 the shared libraries needed by the program executable, prepares the program\n\
476 to run, and runs it. You may invoke this helper program directly from the\n\
477 command line to load and run an ELF executable file; this is like executing\n\
478 that file itself, but always uses this helper program from the file you\n\
479 specified, instead of the helper program file specified in the executable\n\
480 file you run. This is mostly of use for maintainers to test new versions\n\
481 of this helper program; chances are you did not intend to run this program.\n\
483 --list list all dependencies and how they are resolved\n\
484 --verify verify that given object really is a dynamically linked\n\
485 object we can handle\n\
486 --library-path PATH use given PATH instead of content of the environment\n\
487 variable LD_LIBRARY_PATH\n\
488 --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\
489 in LIST\n",
490 NULL);
492 ++_dl_skip_args;
493 --_dl_argc;
494 ++_dl_argv;
496 /* Initialize the data structures for the search paths for shared
497 objects. */
498 _dl_init_paths (library_path);
500 if (__builtin_expect (mode, normal) == verify)
502 const char *objname;
503 const char *err_str = NULL;
504 struct map_args args;
506 args.str = _dl_argv[0];
507 (void) _dl_catch_error (&objname, &err_str, map_doit, &args);
508 if (__builtin_expect (err_str != NULL, 0))
510 if (err_str != _dl_out_of_memory)
511 free ((char *) err_str);
512 _exit (EXIT_FAILURE);
515 else
517 HP_TIMING_NOW (start);
518 _dl_map_object (NULL, _dl_argv[0], 0, lt_library, 0, 0);
519 HP_TIMING_NOW (stop);
521 HP_TIMING_DIFF (load_time, start, stop);
524 phdr = _dl_loaded->l_phdr;
525 phent = _dl_loaded->l_phnum;
526 /* We overwrite here a pointer to a malloc()ed string. But since
527 the malloc() implementation used at this point is the dummy
528 implementations which has no real free() function it does not
529 makes sense to free the old string first. */
530 _dl_loaded->l_name = (char *) "";
531 *user_entry = _dl_loaded->l_entry;
533 else
535 /* Create a link_map for the executable itself.
536 This will be what dlopen on "" returns. */
537 _dl_new_object ((char *) "", "", lt_executable, NULL);
538 if (_dl_loaded == NULL)
539 _dl_sysdep_fatal ("cannot allocate memory for link map\n", NULL);
540 _dl_loaded->l_phdr = phdr;
541 _dl_loaded->l_phnum = phent;
542 _dl_loaded->l_entry = *user_entry;
543 _dl_loaded->l_opencount = 1;
545 /* At this point we are in a bit of trouble. We would have to
546 fill in the values for l_dev and l_ino. But in general we
547 do not know where the file is. We also do not handle AT_EXECFD
548 even if it would be passed up.
550 We leave the values here defined to 0. This is normally no
551 problem as the program code itself is normally no shared
552 object and therefore cannot be loaded dynamically. Nothing
553 prevent the use of dynamic binaries and in these situations
554 we might get problems. We might not be able to find out
555 whether the object is already loaded. But since there is no
556 easy way out and because the dynamic binary must also not
557 have an SONAME we ignore this program for now. If it becomes
558 a problem we can force people using SONAMEs. */
560 /* We delay initializing the path structure until we got the dynamic
561 information for the program. */
564 /* It is not safe to load stuff after the main program. */
565 _dl_loaded->l_map_end = ~0;
566 /* Perhaps the executable has no PT_LOAD header entries at all. */
567 _dl_loaded->l_map_start = ~0;
569 /* Scan the program header table for the dynamic section. */
570 for (ph = phdr; ph < &phdr[phent]; ++ph)
571 switch (ph->p_type)
573 case PT_PHDR:
574 /* Find out the load address. */
575 _dl_loaded->l_addr = (ElfW(Addr)) phdr - ph->p_vaddr;
576 break;
577 case PT_DYNAMIC:
578 /* This tells us where to find the dynamic section,
579 which tells us everything we need to do. */
580 _dl_loaded->l_ld = (void *) _dl_loaded->l_addr + ph->p_vaddr;
581 break;
582 case PT_INTERP:
583 /* This "interpreter segment" was used by the program loader to
584 find the program interpreter, which is this program itself, the
585 dynamic linker. We note what name finds us, so that a future
586 dlopen call or DT_NEEDED entry, for something that wants to link
587 against the dynamic linker as a shared library, will know that
588 the shared object is already loaded. */
589 _dl_rtld_libname.name = ((const char *) _dl_loaded->l_addr
590 + ph->p_vaddr);
591 _dl_rtld_libname.next = NULL;
592 _dl_rtld_map.l_libname = &_dl_rtld_libname;
594 /* Ordinarilly, we would get additional names for the loader from
595 our DT_SONAME. This can't happen if we were actually linked as
596 a static executable (detect this case when we have no DYNAMIC).
597 If so, assume the filename component of the interpreter path to
598 be our SONAME, and add it to our name list. */
599 if (_dl_rtld_map.l_ld == NULL)
601 char *p = strrchr (_dl_rtld_libname.name, '/');
602 if (p)
604 _dl_rtld_libname2.name = p+1;
605 _dl_rtld_libname2.next = NULL;
606 _dl_rtld_libname.next = &_dl_rtld_libname2;
610 has_interp = 1;
611 break;
612 case PT_LOAD:
613 /* Remember where the main program starts in memory. */
615 ElfW(Addr) mapstart;
616 mapstart = _dl_loaded->l_addr + (ph->p_vaddr & ~(ph->p_align - 1));
617 if (_dl_loaded->l_map_start > mapstart)
618 _dl_loaded->l_map_start = mapstart;
620 break;
622 if (! _dl_rtld_map.l_libname && _dl_rtld_map.l_name)
624 /* We were invoked directly, so the program might not have a
625 PT_INTERP. */
626 _dl_rtld_libname.name = _dl_rtld_map.l_name;
627 _dl_rtld_libname.next = NULL;
628 _dl_rtld_map.l_libname = &_dl_rtld_libname;
630 else
631 assert (_dl_rtld_map.l_libname); /* How else did we get here? */
633 if (! rtld_is_main)
635 /* Extract the contents of the dynamic section for easy access. */
636 elf_get_dynamic_info (_dl_loaded);
637 if (_dl_loaded->l_info[DT_HASH])
638 /* Set up our cache of pointers into the hash table. */
639 _dl_setup_hash (_dl_loaded);
642 if (__builtin_expect (mode, normal) == verify)
644 /* We were called just to verify that this is a dynamic
645 executable using us as the program interpreter. Exit with an
646 error if we were not able to load the binary or no interpreter
647 is specified (i.e., this is no dynamically linked binary. */
648 if (_dl_loaded->l_ld == NULL)
649 _exit (1);
651 /* We allow here some platform specific code. */
652 #ifdef DISTINGUISH_LIB_VERSIONS
653 DISTINGUISH_LIB_VERSIONS;
654 #endif
655 _exit (has_interp ? 0 : 2);
658 if (! rtld_is_main)
659 /* Initialize the data structures for the search paths for shared
660 objects. */
661 _dl_init_paths (library_path);
663 /* Put the link_map for ourselves on the chain so it can be found by
664 name. Note that at this point the global chain of link maps contains
665 exactly one element, which is pointed to by _dl_loaded. */
666 if (! _dl_rtld_map.l_name)
667 /* If not invoked directly, the dynamic linker shared object file was
668 found by the PT_INTERP name. */
669 _dl_rtld_map.l_name = (char *) _dl_rtld_map.l_libname->name;
670 _dl_rtld_map.l_type = lt_library;
671 _dl_loaded->l_next = &_dl_rtld_map;
672 _dl_rtld_map.l_prev = _dl_loaded;
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 (_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 != NULL, 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 (_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 (_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 = _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 (_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. */
816 for (i = _dl_loaded->l_searchlist.r_nlist; i > 0; )
817 _dl_loaded->l_searchlist.r_list[--i]->l_global = 1;
819 #ifndef MAP_ANON
820 /* We are done mapping things, so close the zero-fill descriptor. */
821 __close (_dl_zerofd);
822 _dl_zerofd = -1;
823 #endif
825 /* Remove _dl_rtld_map from the chain. */
826 _dl_rtld_map.l_prev->l_next = _dl_rtld_map.l_next;
827 if (_dl_rtld_map.l_next)
828 _dl_rtld_map.l_next->l_prev = _dl_rtld_map.l_prev;
830 if (__builtin_expect (_dl_rtld_map.l_opencount, 2) > 1)
832 /* Some DT_NEEDED entry referred to the interpreter object itself, so
833 put it back in the list of visible objects. We insert it into the
834 chain in symbol search order because gdb uses the chain's order as
835 its symbol search order. */
836 i = 1;
837 while (_dl_loaded->l_searchlist.r_list[i] != &_dl_rtld_map)
838 ++i;
839 _dl_rtld_map.l_prev = _dl_loaded->l_searchlist.r_list[i - 1];
840 if (__builtin_expect (mode, normal) == normal)
841 _dl_rtld_map.l_next = (i + 1 < _dl_loaded->l_searchlist.r_nlist
842 ? _dl_loaded->l_searchlist.r_list[i + 1]
843 : NULL);
844 else
845 /* In trace mode there might be an invisible object (which we
846 could not find) after the previous one in the search list.
847 In this case it doesn't matter much where we put the
848 interpreter object, so we just initialize the list pointer so
849 that the assertion below holds. */
850 _dl_rtld_map.l_next = _dl_rtld_map.l_prev->l_next;
852 assert (_dl_rtld_map.l_prev->l_next == _dl_rtld_map.l_next);
853 _dl_rtld_map.l_prev->l_next = &_dl_rtld_map;
854 if (_dl_rtld_map.l_next)
856 assert (_dl_rtld_map.l_next->l_prev == _dl_rtld_map.l_prev);
857 _dl_rtld_map.l_next->l_prev = &_dl_rtld_map;
861 /* Now let us see whether all libraries are available in the
862 versions we need. */
864 struct version_check_args args;
865 args.doexit = mode == normal;
866 args.dotrace = mode == trace;
867 _dl_receive_error (print_missing_version, version_check_doit, &args);
870 if (__builtin_expect (mode, normal) != normal)
872 /* We were run just to list the shared libraries. It is
873 important that we do this before real relocation, because the
874 functions we call below for output may no longer work properly
875 after relocation. */
876 if (! _dl_loaded->l_info[DT_NEEDED])
877 _dl_sysdep_message ("\t", "statically linked\n", NULL);
878 else
880 struct link_map *l;
882 for (l = _dl_loaded->l_next; l; l = l->l_next)
883 if (l->l_opencount == 0)
884 /* The library was not found. */
885 _dl_sysdep_message ("\t", l->l_libname->name, " => not found\n",
886 NULL);
887 else
889 char buf[20], *bp;
890 buf[sizeof buf - 1] = '\0';
891 bp = _itoa_word (l->l_addr, &buf[sizeof buf - 1], 16, 0);
892 while ((size_t) (&buf[sizeof buf - 1] - bp)
893 < sizeof l->l_addr * 2)
894 *--bp = '0';
895 _dl_sysdep_message ("\t", l->l_libname->name, " => ",
896 l->l_name, " (0x", bp, ")\n", NULL);
900 if (__builtin_expect (mode, trace) != trace)
901 for (i = 1; i < _dl_argc; ++i)
903 const ElfW(Sym) *ref = NULL;
904 ElfW(Addr) loadbase;
905 lookup_t result;
906 char buf[20], *bp;
908 result = _dl_lookup_symbol (_dl_argv[i], _dl_loaded,
909 &ref, _dl_loaded->l_scope,
910 ELF_MACHINE_JMP_SLOT);
912 loadbase = LOOKUP_VALUE_ADDRESS (result);
914 buf[sizeof buf - 1] = '\0';
915 bp = _itoa_word (ref->st_value, &buf[sizeof buf - 1], 16, 0);
916 while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2)
917 *--bp = '0';
918 _dl_sysdep_message (_dl_argv[i], " found at 0x", bp, NULL);
919 buf[sizeof buf - 1] = '\0';
920 bp = _itoa_word (loadbase, &buf[sizeof buf - 1], 16, 0);
921 while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2)
922 *--bp = '0';
923 _dl_sysdep_message (" in object at 0x", bp, "\n", NULL);
925 else
927 if (_dl_lazy >= 0)
929 /* We have to do symbol dependency testing. */
930 struct relocate_args args;
931 struct link_map *l;
933 args.lazy = _dl_lazy;
935 l = _dl_loaded;
936 while (l->l_next)
937 l = l->l_next;
940 if (l != &_dl_rtld_map && l->l_opencount > 0)
942 args.l = l;
943 _dl_receive_error (print_unresolved, relocate_doit,
944 &args);
946 l = l->l_prev;
947 } while (l);
950 #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
951 if (version_info)
953 /* Print more information. This means here, print information
954 about the versions needed. */
955 int first = 1;
956 struct link_map *map = _dl_loaded;
958 for (map = _dl_loaded; map != NULL; map = map->l_next)
960 const char *strtab;
961 ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG];
962 ElfW(Verneed) *ent;
964 if (dyn == NULL)
965 continue;
967 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
968 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
970 if (first)
972 _dl_sysdep_message ("\n\tVersion information:\n", NULL);
973 first = 0;
976 _dl_sysdep_message ("\t", (map->l_name[0]
977 ? map->l_name : _dl_argv[0]),
978 ":\n", NULL);
980 while (1)
982 ElfW(Vernaux) *aux;
983 struct link_map *needed;
985 needed = find_needed (strtab + ent->vn_file);
986 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
988 while (1)
990 const char *fname = NULL;
992 _dl_sysdep_message ("\t\t",
993 strtab + ent->vn_file,
994 " (", strtab + aux->vna_name,
995 ") ",
996 (aux->vna_flags
997 & VER_FLG_WEAK
998 ? "[WEAK] " : ""),
999 "=> ", NULL);
1001 if (needed != NULL
1002 && match_version (strtab+aux->vna_name, needed))
1003 fname = needed->l_name;
1005 _dl_sysdep_message (fname ?: "not found", "\n",
1006 NULL);
1008 if (aux->vna_next == 0)
1009 /* No more symbols. */
1010 break;
1012 /* Next symbol. */
1013 aux = (ElfW(Vernaux) *) ((char *) aux
1014 + aux->vna_next);
1017 if (ent->vn_next == 0)
1018 /* No more dependencies. */
1019 break;
1021 /* Next dependency. */
1022 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
1028 _exit (0);
1032 /* Now we have all the objects loaded. Relocate them all except for
1033 the dynamic linker itself. We do this in reverse order so that copy
1034 relocs of earlier objects overwrite the data written by later
1035 objects. We do not re-relocate the dynamic linker itself in this
1036 loop because that could result in the GOT entries for functions we
1037 call being changed, and that would break us. It is safe to relocate
1038 the dynamic linker out of order because it has no copy relocs (we
1039 know that because it is self-contained). */
1041 struct link_map *l;
1042 int consider_profiling = _dl_profile != NULL;
1043 #ifndef HP_TIMING_NONAVAIL
1044 hp_timing_t start;
1045 hp_timing_t stop;
1046 hp_timing_t add;
1047 #endif
1049 /* If we are profiling we also must do lazy reloaction. */
1050 _dl_lazy |= consider_profiling;
1052 l = _dl_loaded;
1053 while (l->l_next)
1054 l = l->l_next;
1056 HP_TIMING_NOW (start);
1059 if (l != &_dl_rtld_map)
1060 _dl_relocate_object (l, l->l_scope, _dl_lazy, consider_profiling);
1062 l = l->l_prev;
1064 while (l);
1065 HP_TIMING_NOW (stop);
1067 HP_TIMING_DIFF (relocate_time, start, stop);
1069 /* Do any necessary cleanups for the startup OS interface code.
1070 We do these now so that no calls are made after rtld re-relocation
1071 which might be resolved to different functions than we expect.
1072 We cannot do this before relocating the other objects because
1073 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
1074 _dl_sysdep_start_cleanup ();
1076 /* Now enable profiling if needed. Like the previous call,
1077 this has to go here because the calls it makes should use the
1078 rtld versions of the functions (particularly calloc()), but it
1079 needs to have _dl_profile_map set up by the relocator. */
1080 if (__builtin_expect (_dl_profile_map != NULL, 0))
1081 /* We must prepare the profiling. */
1082 _dl_start_profile (_dl_profile_map, _dl_profile_output);
1084 if (_dl_rtld_map.l_opencount > 1)
1086 /* There was an explicit ref to the dynamic linker as a shared lib.
1087 Re-relocate ourselves with user-controlled symbol definitions. */
1088 HP_TIMING_NOW (start);
1089 _dl_relocate_object (&_dl_rtld_map, _dl_loaded->l_scope, 0, 0);
1090 HP_TIMING_NOW (stop);
1091 HP_TIMING_DIFF (add, start, stop);
1092 HP_TIMING_ACCUM_NT (relocate_time, add);
1096 /* Now set up the variable which helps the assembler startup code. */
1097 _dl_main_searchlist = &_dl_loaded->l_searchlist;
1098 _dl_global_scope[0] = &_dl_loaded->l_searchlist;
1100 /* Safe the information about the original global scope list since
1101 we need it in the memory handling later. */
1102 _dl_initial_searchlist = *_dl_main_searchlist;
1105 /* Initialize _r_debug. */
1106 struct r_debug *r = _dl_debug_initialize (_dl_rtld_map.l_addr);
1107 struct link_map *l;
1109 l = _dl_loaded;
1111 #ifdef ELF_MACHINE_DEBUG_SETUP
1113 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
1115 ELF_MACHINE_DEBUG_SETUP (l, r);
1116 ELF_MACHINE_DEBUG_SETUP (&_dl_rtld_map, r);
1118 #else
1120 if (l->l_info[DT_DEBUG])
1121 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
1122 with the run-time address of the r_debug structure */
1123 l->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1125 /* Fill in the pointer in the dynamic linker's own dynamic section, in
1126 case you run gdb on the dynamic linker directly. */
1127 if (_dl_rtld_map.l_info[DT_DEBUG])
1128 _dl_rtld_map.l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1130 #endif
1132 /* Notify the debugger that all objects are now mapped in. */
1133 r->r_state = RT_ADD;
1134 _dl_debug_state ();
1137 #ifndef MAP_COPY
1138 /* We must munmap() the cache file. */
1139 _dl_unload_cache ();
1140 #endif
1142 /* Once we return, _dl_sysdep_start will invoke
1143 the DT_INIT functions and then *USER_ENTRY. */
1146 /* This is a little helper function for resolving symbols while
1147 tracing the binary. */
1148 static void
1149 print_unresolved (int errcode __attribute__ ((unused)), const char *objname,
1150 const char *errstring)
1152 if (objname[0] == '\0')
1153 objname = _dl_argv[0] ?: "<main program>";
1154 _dl_sysdep_error (errstring, " (", objname, ")\n", NULL);
1157 /* This is a little helper function for resolving symbols while
1158 tracing the binary. */
1159 static void
1160 print_missing_version (int errcode __attribute__ ((unused)),
1161 const char *objname, const char *errstring)
1163 _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>", ": ",
1164 objname, ": ", errstring, "\n", NULL);
1167 /* Nonzero if any of the debugging options is enabled. */
1168 static int any_debug;
1170 /* Process the string given as the parameter which explains which debugging
1171 options are enabled. */
1172 static void
1173 process_dl_debug (const char *dl_debug)
1175 size_t len;
1176 #define separators " ,:"
1179 len = 0;
1180 /* Skip separating white spaces and commas. */
1181 dl_debug += strspn (dl_debug, separators);
1182 if (*dl_debug != '\0')
1184 len = strcspn (dl_debug, separators);
1186 switch (len)
1188 case 3:
1189 /* This option is not documented since it is not generally
1190 useful. */
1191 if (memcmp (dl_debug, "all", 3) == 0)
1193 _dl_debug_libs = 1;
1194 _dl_debug_impcalls = 1;
1195 _dl_debug_reloc = 1;
1196 _dl_debug_files = 1;
1197 _dl_debug_symbols = 1;
1198 _dl_debug_bindings = 1;
1199 _dl_debug_versions = 1;
1200 any_debug = 1;
1201 continue;
1203 break;
1205 case 4:
1206 if (memcmp (dl_debug, "help", 4) == 0)
1208 _dl_sysdep_message ("\
1209 Valid options for the LD_DEBUG environment variable are:\n\
1211 bindings display information about symbol binding\n\
1212 files display processing of files and libraries\n\
1213 help display this help message and exit\n\
1214 libs display library search paths\n\
1215 reloc display relocation processing\n\
1216 statistics display relocation statistics\n\
1217 symbols display symbol table processing\n\
1218 versions display version dependencies\n\
1220 To direct the debugging output into a file instead of standard output\n\
1221 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n",
1222 NULL);
1223 _exit (0);
1226 if (memcmp (dl_debug, "libs", 4) == 0)
1228 _dl_debug_libs = 1;
1229 _dl_debug_impcalls = 1;
1230 any_debug = 1;
1231 continue;
1233 break;
1235 case 5:
1236 if (memcmp (dl_debug, "reloc", 5) == 0)
1238 _dl_debug_reloc = 1;
1239 _dl_debug_impcalls = 1;
1240 any_debug = 1;
1241 continue;
1244 if (memcmp (dl_debug, "files", 5) == 0)
1246 _dl_debug_files = 1;
1247 _dl_debug_impcalls = 1;
1248 any_debug = 1;
1249 continue;
1251 break;
1253 case 7:
1254 if (memcmp (dl_debug, "symbols", 7) == 0)
1256 _dl_debug_symbols = 1;
1257 _dl_debug_impcalls = 1;
1258 any_debug = 1;
1259 continue;
1261 break;
1263 case 8:
1264 if (memcmp (dl_debug, "bindings", 8) == 0)
1266 _dl_debug_bindings = 1;
1267 _dl_debug_impcalls = 1;
1268 any_debug = 1;
1269 continue;
1272 if (memcmp (dl_debug, "versions", 8) == 0)
1274 _dl_debug_versions = 1;
1275 _dl_debug_impcalls = 1;
1276 any_debug = 1;
1277 continue;
1279 break;
1281 case 10:
1282 if (memcmp (dl_debug, "statistics", 10) == 0)
1284 _dl_debug_statistics = 1;
1285 continue;
1287 break;
1289 default:
1290 break;
1294 /* Display a warning and skip everything until next separator. */
1295 char *startp = strndupa (dl_debug, len);
1296 _dl_sysdep_error ("warning: debug option `", startp,
1297 "' unknown; try LD_DEBUG=help\n", NULL);
1298 break;
1302 while (*(dl_debug += len) != '\0');
1305 /* Process all environments variables the dynamic linker must recognize.
1306 Since all of them start with `LD_' we are a bit smarter while finding
1307 all the entries. */
1308 static void
1309 process_envvars (enum mode *modep, int *lazyp)
1311 char **runp = NULL;
1312 char *envline;
1313 enum mode mode = normal;
1314 int bind_now = 0;
1315 char *debug_output = NULL;
1317 /* This is the default place for profiling data file. */
1318 _dl_profile_output = "/var/tmp";
1320 while ((envline = _dl_next_ld_env_entry (&runp)) != NULL)
1322 size_t len = strcspn (envline, "=");
1324 if (envline[len] != '=')
1325 /* This is a "LD_" variable at the end of the string without
1326 a '=' character. Ignore it since otherwise we will access
1327 invalid memory below. */
1328 break;
1330 switch (len - 3)
1332 case 4:
1333 /* Warning level, verbose or not. */
1334 if (memcmp (&envline[3], "WARN", 4) == 0)
1335 _dl_verbose = envline[8] != '\0';
1336 break;
1338 case 5:
1339 /* Debugging of the dynamic linker? */
1340 if (memcmp (&envline[3], "DEBUG", 5) == 0)
1341 process_dl_debug (&envline[9]);
1342 break;
1344 case 7:
1345 /* Print information about versions. */
1346 if (memcmp (&envline[3], "VERBOSE", 7) == 0)
1348 version_info = envline[11] != '\0';
1349 break;
1352 /* List of objects to be preloaded. */
1353 if (memcmp (&envline[3], "PRELOAD", 7) == 0)
1355 preloadlist = &envline[11];
1356 break;
1359 /* Which shared object shall be profiled. */
1360 if (memcmp (&envline[3], "PROFILE", 7) == 0)
1361 _dl_profile = &envline[11];
1362 break;
1364 case 8:
1365 /* Do we bind early? */
1366 if (memcmp (&envline[3], "BIND_NOW", 8) == 0)
1368 bind_now = envline[12] != '\0';
1369 break;
1371 if (memcmp (&envline[3], "BIND_NOT", 8) == 0)
1372 _dl_bind_not = envline[12] != '\0';
1373 break;
1375 case 9:
1376 /* Test whether we want to see the content of the auxiliary
1377 array passed up from the kernel. */
1378 if (memcmp (&envline[3], "SHOW_AUXV", 9) == 0)
1379 _dl_show_auxv ();
1380 break;
1382 case 10:
1383 /* Mask for the important hardware capabilities. */
1384 if (memcmp (&envline[3], "HWCAP_MASK", 10) == 0)
1385 _dl_hwcap_mask = strtoul (&envline[14], NULL, 0);
1386 break;
1388 case 11:
1389 /* Path where the binary is found. */
1390 if (!__libc_enable_secure
1391 && memcmp (&envline[3], "ORIGIN_PATH", 11) == 0)
1392 _dl_origin_path = &envline[15];
1393 break;
1395 case 12:
1396 /* The library search path. */
1397 if (memcmp (&envline[3], "LIBRARY_PATH", 12) == 0)
1399 library_path = &envline[16];
1400 break;
1403 /* Where to place the profiling data file. */
1404 if (memcmp (&envline[3], "DEBUG_OUTPUT", 12) == 0)
1406 debug_output = &envline[16];
1407 break;
1410 if (memcmp (&envline[3], "DYNAMIC_WEAK", 12) == 0)
1411 _dl_dynamic_weak = 1;
1412 break;
1414 case 14:
1415 /* Where to place the profiling data file. */
1416 if (!__libc_enable_secure
1417 && memcmp (&envline[3], "PROFILE_OUTPUT", 14) == 0)
1419 _dl_profile_output = &envline[18];
1420 if (*_dl_profile_output == '\0')
1421 _dl_profile_output = "/var/tmp";
1423 break;
1425 case 20:
1426 /* The mode of the dynamic linker can be set. */
1427 if (memcmp (&envline[3], "TRACE_LOADED_OBJECTS", 20) == 0)
1428 mode = trace;
1429 break;
1431 /* We might have some extra environment variable to handle. This
1432 is tricky due to the pre-processing of the length of the name
1433 in the switch statement here. The code here assumes that added
1434 environment variables have a different length. */
1435 #ifdef EXTRA_LD_ENVVARS
1436 EXTRA_LD_ENVVARS
1437 #endif
1441 /* Extra security for SUID binaries. Remove all dangerous environment
1442 variables. */
1443 if (__libc_enable_secure)
1445 static const char *unsecure_envvars[] =
1447 #ifdef EXTRA_UNSECURE_ENVVARS
1448 EXTRA_UNSECURE_ENVVARS
1449 #endif
1451 size_t cnt;
1453 if (preloadlist != NULL)
1454 unsetenv ("LD_PRELOAD");
1455 if (library_path != NULL)
1456 unsetenv ("LD_LIBRARY_PATH");
1457 if (_dl_origin_path != NULL)
1458 unsetenv ("LD_ORIGIN_PATH");
1459 if (debug_output != NULL)
1460 unsetenv ("LD_DEBUG_OUTPUT");
1461 if (_dl_profile != NULL)
1462 unsetenv ("LD_PROFILE");
1464 for (cnt = 0;
1465 cnt < sizeof (unsecure_envvars) / sizeof (unsecure_envvars[0]);
1466 ++cnt)
1467 unsetenv (unsecure_envvars[cnt]);
1470 /* The name of the object to profile cannot be empty. */
1471 if (_dl_profile != NULL && *_dl_profile == '\0')
1472 _dl_profile = NULL;
1474 /* If we have to run the dynamic linker in debugging mode and the
1475 LD_DEBUG_OUTPUT environment variable is given, we write the debug
1476 messages to this file. */
1477 if (any_debug && debug_output != NULL && !__libc_enable_secure)
1479 size_t name_len = strlen (debug_output);
1480 char buf[name_len + 12];
1481 char *startp;
1483 buf[name_len + 11] = '\0';
1484 startp = _itoa_word (__getpid (), &buf[name_len + 11], 10, 0);
1485 *--startp = '.';
1486 startp = memcpy (startp - name_len, debug_output, name_len);
1488 _dl_debug_fd = __open (startp, O_WRONLY | O_APPEND | O_CREAT, 0666);
1489 if (_dl_debug_fd == -1)
1490 /* We use standard output if opening the file failed. */
1491 _dl_debug_fd = STDOUT_FILENO;
1494 /* LAZY is determined by the environment variable LD_WARN and
1495 LD_BIND_NOW if we trace the binary. */
1496 if (__builtin_expect (mode, normal) == trace)
1497 *lazyp = _dl_verbose ? !bind_now : -1;
1498 else
1499 *lazyp = !bind_now;
1501 *modep = mode;
1505 /* Print the various times we collected. */
1506 static void
1507 print_statistics (void)
1509 char buf[200];
1510 #ifndef HP_TIMING_NONAVAIL
1511 char *cp;
1512 char *wp;
1514 /* Total time rtld used. */
1515 if (HP_TIMING_AVAIL)
1517 HP_TIMING_PRINT (buf, sizeof (buf), rtld_total_time);
1518 _dl_debug_message (1, "\nruntime linker statistics:\n"
1519 " total startup time in dynamic loader: ",
1520 buf, "\n", NULL);
1523 /* Print relocation statistics. */
1524 if (HP_TIMING_AVAIL)
1526 HP_TIMING_PRINT (buf, sizeof (buf), relocate_time);
1527 _dl_debug_message (1, " time needed for relocation: ", buf,
1528 NULL);
1529 cp = _itoa_word ((1000 * relocate_time) / rtld_total_time,
1530 buf + sizeof (buf), 10, 0);
1531 wp = buf;
1532 switch (buf + sizeof (buf) - cp)
1534 case 3:
1535 *wp++ = *cp++;
1536 case 2:
1537 *wp++ = *cp++;
1538 case 1:
1539 *wp++ = '.';
1540 *wp++ = *cp++;
1542 *wp = '\0';
1543 _dl_debug_message (0, " (", buf, "%)\n", NULL);
1545 #endif
1546 buf[sizeof (buf) - 1] = '\0';
1547 _dl_debug_message (1, " number of relocations: ",
1548 _itoa_word (_dl_num_relocations,
1549 buf + sizeof (buf) - 1, 10, 0),
1550 "\n", NULL);
1552 #ifndef HP_TIMING_NONAVAIL
1553 /* Time spend while loading the object and the dependencies. */
1554 if (HP_TIMING_AVAIL)
1556 HP_TIMING_PRINT (buf, sizeof (buf), load_time);
1557 _dl_debug_message (1, " time needed to load objects: ", buf,
1558 NULL);
1559 cp = _itoa_word ((1000 * load_time) / rtld_total_time,
1560 buf + sizeof (buf), 10, 0);
1561 wp = buf;
1562 switch (buf + sizeof (buf) - cp)
1564 case 3:
1565 *wp++ = *cp++;
1566 case 2:
1567 *wp++ = *cp++;
1568 case 1:
1569 *wp++ = '.';
1570 *wp++ = *cp++;
1572 *wp = '\0';
1573 _dl_debug_message (0, " (", buf, "%)\n", NULL);
1575 #endif