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. */
24 #include <sys/mman.h> /* Check if MAP_ANON is defined. */
26 #include <stdio-common/_itoa.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"
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,
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,
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
72 static void process_envvars (enum mode *modep, int *lazyp);
76 unsigned int _dl_skip_args; /* Nonzero if we were run directly. */
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;
88 int _dl_debug_impcalls;
89 int _dl_debug_bindings;
90 int _dl_debug_symbols;
91 int _dl_debug_versions;
94 int _dl_debug_statistics;
95 const char *_dl_inhibit_rpath; /* RPATH values which should be
97 const char *_dl_origin_path;
99 /* This is a pointer to the map for the main object and through it to
100 all loaded objects. */
101 struct link_map *_dl_loaded;
102 /* Pointer to the l_searchlist element of the link map of the main object. */
103 struct r_scope_elem *_dl_main_searchlist;
104 /* Copy of the content of `_dl_main_searchlist'. */
105 struct r_scope_elem _dl_initial_searchlist;
106 /* Array which is used when looking up in the global scope. */
107 struct r_scope_elem *_dl_global_scope[2];
109 /* During the program run we must not modify the global data of
110 loaded shared object simultanously in two threads. Therefore we
111 protect `_dl_open' and `_dl_close' in dl-close.c.
113 This must be a recursive lock since the initializer function of
114 the loaded object might as well require a call to this function.
115 At this time it is not anymore a problem to modify the tables. */
116 __libc_lock_define_initialized_recursive (, _dl_load_lock)
118 /* Set nonzero during loading and initialization of executable and
119 libraries, cleared before the executable's entry point runs. This
120 must not be initialized to nonzero, because the unused dynamic
121 linker loaded in for libc.so's "ld.so.1" dep will provide the
122 definition seen by libc.so's initializer; that value must be zero,
123 and will be since that dynamic linker's _dl_start and dl_main will
128 static void dl_main (const ElfW(Phdr) *phdr,
130 ElfW(Addr) *user_entry);
132 struct link_map _dl_rtld_map;
133 struct libname_list _dl_rtld_libname;
134 struct libname_list _dl_rtld_libname2;
136 /* Variable for statistics. */
137 #ifndef HP_TIMING_NONAVAIL
138 static hp_timing_t rtld_total_time;
139 static hp_timing_t relocate_time;
140 static hp_timing_t load_time;
142 extern unsigned long int _dl_num_relocations; /* in dl-lookup.c */
144 static ElfW(Addr) _dl_start_final (void *arg, struct link_map *bootstrap_map_p,
145 hp_timing_t start_time);
150 #error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
154 _dl_start (void *arg)
156 struct link_map bootstrap_map;
157 hp_timing_t start_time;
160 /* This #define produces dynamic linking inline functions for
161 bootstrap relocation instead of general-purpose relocation. */
162 #define RTLD_BOOTSTRAP
163 #define RESOLVE(sym, version, flags) \
164 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : bootstrap_map.l_addr)
165 #include "dynamic-link.h"
167 if (HP_TIMING_INLINE && HP_TIMING_AVAIL)
168 HP_TIMING_NOW (start_time);
170 /* Partly clean the `bootstrap_map' structure up. Don't use `memset'
171 since it might nor be built in or inlined and we cannot make function
172 calls at this point. */
174 cnt < sizeof (bootstrap_map.l_info) / sizeof (bootstrap_map.l_info[0]);
176 bootstrap_map.l_info[cnt] = 0;
178 /* Figure out the run-time load address of the dynamic linker itself. */
179 bootstrap_map.l_addr = elf_machine_load_address ();
181 /* Read our own dynamic section and fill in the info array. */
182 bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
183 elf_get_dynamic_info (&bootstrap_map);
185 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
186 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info);
189 /* Relocate ourselves so we can do normal function calls and
190 data access using the global offset table. */
192 ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0, 0);
193 /* Please note that we don't allow profiling of this object and
194 therefore need not test whether we have to allocate the array
195 for the relocation results (as done in dl-reloc.c). */
197 /* Now life is sane; we can call functions and access global data.
198 Set up to use the operating system facilities, and find out from
199 the operating system's program loader where to find the program
200 header table in core. Put the rest of _dl_start into a separate
201 function, that way the compiler cannot put accesses to the GOT
202 before ELF_DYNAMIC_RELOCATE. */
203 return _dl_start_final (arg, &bootstrap_map, start_time);
208 _dl_start_final (void *arg, struct link_map *bootstrap_map_p,
209 hp_timing_t start_time)
211 /* The use of `alloca' here looks ridiculous but it helps. The goal
212 is to avoid the function from being inlined. There is no official
213 way to do this so we use this trick. gcc never inlines functions
214 which use `alloca'. */
215 ElfW(Addr) *start_addr = alloca (sizeof (ElfW(Addr)));
219 /* If it hasn't happen yet record the startup time. */
220 if (! HP_TIMING_INLINE)
221 HP_TIMING_NOW (start_time);
223 /* Initialize the timing functions. */
224 HP_TIMING_DIFF_INIT ();
227 /* Transfer data about ourselves to the permanent link_map structure. */
228 _dl_rtld_map.l_addr = bootstrap_map_p->l_addr;
229 _dl_rtld_map.l_ld = bootstrap_map_p->l_ld;
230 _dl_rtld_map.l_opencount = 1;
231 memcpy (_dl_rtld_map.l_info, bootstrap_map_p->l_info,
232 sizeof _dl_rtld_map.l_info);
233 _dl_setup_hash (&_dl_rtld_map);
235 /* Don't bother trying to work out how ld.so is mapped in memory. */
236 _dl_rtld_map.l_map_start = ~0;
237 _dl_rtld_map.l_map_end = ~0;
239 /* Call the OS-dependent function to set up life so we can do things like
240 file access. It will call `dl_main' (below) to do all the real work
241 of the dynamic linker, and then unwind our frame and run the user
242 entry point on the same stack we entered on. */
243 *start_addr = _dl_sysdep_start (arg, &dl_main);
244 #ifndef HP_TIMING_NONAVAIL
247 hp_timing_t end_time;
249 /* Get the current time. */
250 HP_TIMING_NOW (end_time);
252 /* Compute the difference. */
253 HP_TIMING_DIFF (rtld_total_time, start_time, end_time);
257 if (__builtin_expect (_dl_debug_statistics, 0))
263 /* Now life is peachy; we can do all normal operations.
264 On to the real work. */
266 void ENTRY_POINT (void);
268 /* Some helper functions. */
270 /* Arguments to relocate_doit. */
279 /* Argument to map_doit. */
281 /* Return value of map_doit. */
282 struct link_map *main_map;
285 /* Arguments to version_check_doit. */
286 struct version_check_args
293 relocate_doit (void *a)
295 struct relocate_args *args = (struct relocate_args *) a;
297 _dl_relocate_object (args->l, args->l->l_scope,
304 struct map_args *args = (struct map_args *) a;
305 args->main_map = _dl_map_object (NULL, args->str, 0, lt_library, 0);
309 version_check_doit (void *a)
311 struct version_check_args *args = (struct version_check_args *) a;
312 if (_dl_check_all_versions (_dl_loaded, 1, args->dotrace) && args->doexit)
313 /* We cannot start the application. Abort now. */
318 static inline struct link_map *
319 find_needed (const char *name)
321 unsigned int n = _dl_loaded->l_searchlist.r_nlist;
324 if (_dl_name_match_p (name, _dl_loaded->l_searchlist.r_list[n]))
325 return _dl_loaded->l_searchlist.r_list[n];
327 /* Should never happen. */
332 match_version (const char *string, struct link_map *map)
334 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
337 #define VERDEFTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
338 if (map->l_info[VERDEFTAG] == NULL)
339 /* The file has no symbol versioning. */
342 def = (ElfW(Verdef) *) ((char *) map->l_addr
343 + map->l_info[VERDEFTAG]->d_un.d_ptr);
346 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
348 /* Compare the version strings. */
349 if (strcmp (string, strtab + aux->vda_name) == 0)
353 /* If no more definitions we failed to find what we want. */
354 if (def->vd_next == 0)
357 /* Next definition. */
358 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
364 static const char *library_path; /* The library search path. */
365 static const char *preloadlist; /* The list preloaded objects. */
366 static int version_info; /* Nonzero if information about
367 versions has to be printed. */
370 dl_main (const ElfW(Phdr) *phdr,
372 ElfW(Addr) *user_entry)
374 const ElfW(Phdr) *ph;
376 struct link_map **preloads;
377 unsigned int npreloads;
382 int rtld_is_main = 0;
383 #ifndef HP_TIMING_NONAVAIL
389 /* Process the environment variable which control the behaviour. */
390 process_envvars (&mode, &_dl_lazy);
392 /* Set up a flag which tells we are just starting. */
395 if (*user_entry == (ElfW(Addr)) &ENTRY_POINT)
397 /* Ho ho. We are not the program interpreter! We are the program
398 itself! This means someone ran ld.so as a command. Well, that
399 might be convenient to do sometimes. We support it by
400 interpreting the args like this:
402 ld.so PROGRAM ARGS...
404 The first argument is the name of a file containing an ELF
405 executable we will load and run with the following arguments.
406 To simplify life here, PROGRAM is searched for using the
407 normal rules for shared objects, rather than $PATH or anything
408 like that. We just load it and use its entry point; we don't
409 pay attention to its PT_INTERP command (we are the interpreter
410 ourselves). This is an easy way to test a new ld.so before
414 /* Note the place where the dynamic linker actually came from. */
415 _dl_rtld_map.l_name = _dl_argv[0];
418 if (! strcmp (_dl_argv[1], "--list"))
421 _dl_lazy = -1; /* This means do no dependency analysis. */
427 else if (! strcmp (_dl_argv[1], "--verify"))
435 else if (! strcmp (_dl_argv[1], "--library-path") && _dl_argc > 2)
437 library_path = _dl_argv[2];
443 else if (! strcmp (_dl_argv[1], "--inhibit-rpath") && _dl_argc > 2)
445 _dl_inhibit_rpath = _dl_argv[2];
454 /* If we have no further argument the program was called incorrectly.
455 Grant the user some education. */
458 Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
459 You have invoked `ld.so', the helper program for shared library executables.\n\
460 This program usually lives in the file `/lib/ld.so', and special directives\n\
461 in executable files using ELF shared libraries tell the system's program\n\
462 loader to load the helper program from this file. This helper program loads\n\
463 the shared libraries needed by the program executable, prepares the program\n\
464 to run, and runs it. You may invoke this helper program directly from the\n\
465 command line to load and run an ELF executable file; this is like executing\n\
466 that file itself, but always uses this helper program from the file you\n\
467 specified, instead of the helper program file specified in the executable\n\
468 file you run. This is mostly of use for maintainers to test new versions\n\
469 of this helper program; chances are you did not intend to run this program.\n\
471 --list list all dependencies and how they are resolved\n\
472 --verify verify that given object really is a dynamically linked\n\
473 object we can handle\n\
474 --library-path PATH use given PATH instead of content of the environment\n\
475 variable LD_LIBRARY_PATH\n\
476 --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\
484 /* Initialize the data structures for the search paths for shared
486 _dl_init_paths (library_path);
488 if (__builtin_expect (mode, normal) == verify)
490 char *err_str = NULL;
491 struct map_args args;
493 args.str = _dl_argv[0];
494 (void) _dl_catch_error (&err_str, map_doit, &args);
498 _exit (EXIT_FAILURE);
503 HP_TIMING_NOW (start);
504 _dl_map_object (NULL, _dl_argv[0], 0, lt_library, 0);
505 HP_TIMING_NOW (stop);
507 HP_TIMING_DIFF (load_time, start, stop);
510 phdr = _dl_loaded->l_phdr;
511 phent = _dl_loaded->l_phnum;
512 /* We overwrite here a pointer to a malloc()ed string. But since
513 the malloc() implementation used at this point is the dummy
514 implementations which has no real free() function it does not
515 makes sense to free the old string first. */
516 _dl_loaded->l_name = (char *) "";
517 *user_entry = _dl_loaded->l_entry;
521 /* Create a link_map for the executable itself.
522 This will be what dlopen on "" returns. */
523 _dl_new_object ((char *) "", "", lt_executable, NULL);
524 if (_dl_loaded == NULL)
525 _dl_sysdep_fatal ("cannot allocate memory for link map\n", NULL);
526 _dl_loaded->l_phdr = phdr;
527 _dl_loaded->l_phnum = phent;
528 _dl_loaded->l_entry = *user_entry;
529 _dl_loaded->l_opencount = 1;
531 /* At this point we are in a bit of trouble. We would have to
532 fill in the values for l_dev and l_ino. But in general we
533 do not know where the file is. We also do not handle AT_EXECFD
534 even if it would be passed up.
536 We leave the values here defined to 0. This is normally no
537 problem as the program code itself is normally no shared
538 object and therefore cannot be loaded dynamically. Nothing
539 prevent the use of dynamic binaries and in these situations
540 we might get problems. We might not be able to find out
541 whether the object is already loaded. But since there is no
542 easy way out and because the dynamic binary must also not
543 have an SONAME we ignore this program for now. If it becomes
544 a problem we can force people using SONAMEs. */
546 /* We delay initializing the path structure until we got the dynamic
547 information for the program. */
550 /* It is not safe to load stuff after the main program. */
551 _dl_loaded->l_map_end = ~0;
552 /* Perhaps the executable has no PT_LOAD header entries at all. */
553 _dl_loaded->l_map_start = ~0;
555 /* Scan the program header table for the dynamic section. */
556 for (ph = phdr; ph < &phdr[phent]; ++ph)
560 /* Find out the load address. */
561 _dl_loaded->l_addr = (ElfW(Addr)) phdr - ph->p_vaddr;
564 /* This tells us where to find the dynamic section,
565 which tells us everything we need to do. */
566 _dl_loaded->l_ld = (void *) _dl_loaded->l_addr + ph->p_vaddr;
569 /* This "interpreter segment" was used by the program loader to
570 find the program interpreter, which is this program itself, the
571 dynamic linker. We note what name finds us, so that a future
572 dlopen call or DT_NEEDED entry, for something that wants to link
573 against the dynamic linker as a shared library, will know that
574 the shared object is already loaded. */
575 _dl_rtld_libname.name = ((const char *) _dl_loaded->l_addr
577 _dl_rtld_libname.next = NULL;
578 _dl_rtld_map.l_libname = &_dl_rtld_libname;
580 /* Ordinarilly, we would get additional names for the loader from
581 our DT_SONAME. This can't happen if we were actually linked as
582 a static executable (detect this case when we have no DYNAMIC).
583 If so, assume the filename component of the interpreter path to
584 be our SONAME, and add it to our name list. */
585 if (_dl_rtld_map.l_ld == NULL)
587 char *p = strrchr (_dl_rtld_libname.name, '/');
590 _dl_rtld_libname2.name = p+1;
591 _dl_rtld_libname2.next = NULL;
592 _dl_rtld_libname.next = &_dl_rtld_libname2;
599 /* Remember where the main program starts in memory. */
602 mapstart = _dl_loaded->l_addr + (ph->p_vaddr & ~(ph->p_align - 1));
603 if (_dl_loaded->l_map_start > mapstart)
604 _dl_loaded->l_map_start = mapstart;
608 if (! _dl_rtld_map.l_libname && _dl_rtld_map.l_name)
610 /* We were invoked directly, so the program might not have a
612 _dl_rtld_libname.name = _dl_rtld_map.l_name;
613 _dl_rtld_libname.next = NULL;
614 _dl_rtld_map.l_libname = &_dl_rtld_libname;
617 assert (_dl_rtld_map.l_libname); /* How else did we get here? */
621 /* Extract the contents of the dynamic section for easy access. */
622 elf_get_dynamic_info (_dl_loaded);
623 if (_dl_loaded->l_info[DT_HASH])
624 /* Set up our cache of pointers into the hash table. */
625 _dl_setup_hash (_dl_loaded);
628 if (__builtin_expect (mode, normal) == verify)
630 /* We were called just to verify that this is a dynamic
631 executable using us as the program interpreter. Exit with an
632 error if we were not able to load the binary or no interpreter
633 is specified (i.e., this is no dynamically linked binary. */
634 if (_dl_loaded->l_ld == NULL)
637 /* We allow here some platform specific code. */
638 #ifdef DISTINGUISH_LIB_VERSIONS
639 DISTINGUISH_LIB_VERSIONS;
641 _exit (has_interp ? 0 : 2);
645 /* Initialize the data structures for the search paths for shared
647 _dl_init_paths (library_path);
649 /* Put the link_map for ourselves on the chain so it can be found by
650 name. Note that at this point the global chain of link maps contains
651 exactly one element, which is pointed to by _dl_loaded. */
652 if (! _dl_rtld_map.l_name)
653 /* If not invoked directly, the dynamic linker shared object file was
654 found by the PT_INTERP name. */
655 _dl_rtld_map.l_name = (char *) _dl_rtld_map.l_libname->name;
656 _dl_rtld_map.l_type = lt_library;
657 _dl_loaded->l_next = &_dl_rtld_map;
658 _dl_rtld_map.l_prev = _dl_loaded;
660 /* We have two ways to specify objects to preload: via environment
661 variable and via the file /etc/ld.so.preload. The latter can also
662 be used when security is enabled. */
668 /* The LD_PRELOAD environment variable gives list of libraries
669 separated by white space or colons that are loaded before the
670 executable's dependencies and prepended to the global scope
671 list. If the binary is running setuid all elements
672 containing a '/' are ignored since it is insecure. */
673 char *list = strdupa (preloadlist);
676 HP_TIMING_NOW (start);
678 while ((p = strsep (&list, " :")) != NULL)
680 && (! __libc_enable_secure || strchr (p, '/') == NULL))
682 struct link_map *new_map = _dl_map_object (_dl_loaded, p, 1,
684 if (new_map->l_opencount == 1)
685 /* It is no duplicate. */
689 HP_TIMING_NOW (stop);
690 HP_TIMING_DIFF (diff, start, stop);
691 HP_TIMING_ACCUM_NT (load_time, diff);
694 /* Read the contents of the file. */
695 file = _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size,
696 PROT_READ | PROT_WRITE);
699 /* Parse the file. It contains names of libraries to be loaded,
700 separated by white spaces or `:'. It may also contain
701 comments introduced by `#'. */
706 /* Eliminate comments. */
711 char *comment = memchr (runp, '#', rest);
715 rest -= comment - runp;
718 while (--rest > 0 && *++comment != '\n');
721 /* We have one problematic case: if we have a name at the end of
722 the file without a trailing terminating characters, we cannot
723 place the \0. Handle the case separately. */
724 if (file[file_size - 1] != ' ' && file[file_size - 1] != '\t'
725 && file[file_size - 1] != '\n' && file[file_size - 1] != ':')
727 problem = &file[file_size];
728 while (problem > file && problem[-1] != ' ' && problem[-1] != '\t'
729 && problem[-1] != '\n' && problem[-1] != ':')
738 file[file_size - 1] = '\0';
741 HP_TIMING_NOW (start);
747 while ((p = strsep (&runp, ": \t\n")) != NULL)
750 struct link_map *new_map = _dl_map_object (_dl_loaded, p, 1,
752 if (new_map->l_opencount == 1)
753 /* It is no duplicate. */
760 char *p = strndupa (problem, file_size - (problem - file));
761 struct link_map *new_map = _dl_map_object (_dl_loaded, p, 1,
763 if (new_map->l_opencount == 1)
764 /* It is no duplicate. */
768 HP_TIMING_NOW (stop);
769 HP_TIMING_DIFF (diff, start, stop);
770 HP_TIMING_ACCUM_NT (load_time, diff);
772 /* We don't need the file anymore. */
773 __munmap (file, file_size);
778 /* Set up PRELOADS with a vector of the preloaded libraries. */
780 preloads = __alloca (npreloads * sizeof preloads[0]);
781 l = _dl_rtld_map.l_next; /* End of the chain before preloads. */
788 assert (i == npreloads);
791 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
792 specified some libraries to load, these are inserted before the actual
793 dependencies in the executable's searchlist for symbol resolution. */
794 HP_TIMING_NOW (start);
795 _dl_map_object_deps (_dl_loaded, preloads, npreloads, mode == trace);
796 HP_TIMING_NOW (stop);
797 HP_TIMING_DIFF (diff, start, stop);
798 HP_TIMING_ACCUM_NT (load_time, diff);
800 /* Mark all objects as being in the global scope. */
801 for (i = _dl_loaded->l_searchlist.r_nlist; i > 0; )
802 _dl_loaded->l_searchlist.r_list[--i]->l_global = 1;
805 /* We are done mapping things, so close the zero-fill descriptor. */
806 __close (_dl_zerofd);
810 /* Remove _dl_rtld_map from the chain. */
811 _dl_rtld_map.l_prev->l_next = _dl_rtld_map.l_next;
812 if (_dl_rtld_map.l_next)
813 _dl_rtld_map.l_next->l_prev = _dl_rtld_map.l_prev;
815 if (__builtin_expect (_dl_rtld_map.l_opencount, 2) > 1)
817 /* Some DT_NEEDED entry referred to the interpreter object itself, so
818 put it back in the list of visible objects. We insert it into the
819 chain in symbol search order because gdb uses the chain's order as
820 its symbol search order. */
822 while (_dl_loaded->l_searchlist.r_list[i] != &_dl_rtld_map)
824 _dl_rtld_map.l_prev = _dl_loaded->l_searchlist.r_list[i - 1];
825 if (__builtin_expect (mode, normal) == normal)
826 _dl_rtld_map.l_next = (i + 1 < _dl_loaded->l_searchlist.r_nlist
827 ? _dl_loaded->l_searchlist.r_list[i + 1]
830 /* In trace mode there might be an invisible object (which we
831 could not find) after the previous one in the search list.
832 In this case it doesn't matter much where we put the
833 interpreter object, so we just initialize the list pointer so
834 that the assertion below holds. */
835 _dl_rtld_map.l_next = _dl_rtld_map.l_prev->l_next;
837 assert (_dl_rtld_map.l_prev->l_next == _dl_rtld_map.l_next);
838 _dl_rtld_map.l_prev->l_next = &_dl_rtld_map;
839 if (_dl_rtld_map.l_next)
841 assert (_dl_rtld_map.l_next->l_prev == _dl_rtld_map.l_prev);
842 _dl_rtld_map.l_next->l_prev = &_dl_rtld_map;
846 /* Now let us see whether all libraries are available in the
849 struct version_check_args args;
850 args.doexit = mode == normal;
851 args.dotrace = mode == trace;
852 _dl_receive_error (print_missing_version, version_check_doit, &args);
855 if (__builtin_expect (mode, normal) != normal)
857 /* We were run just to list the shared libraries. It is
858 important that we do this before real relocation, because the
859 functions we call below for output may no longer work properly
861 if (! _dl_loaded->l_info[DT_NEEDED])
862 _dl_sysdep_message ("\t", "statically linked\n", NULL);
867 for (l = _dl_loaded->l_next; l; l = l->l_next)
868 if (l->l_opencount == 0)
869 /* The library was not found. */
870 _dl_sysdep_message ("\t", l->l_libname->name, " => not found\n",
875 buf[sizeof buf - 1] = '\0';
876 bp = _itoa_word (l->l_addr, &buf[sizeof buf - 1], 16, 0);
877 while ((size_t) (&buf[sizeof buf - 1] - bp)
878 < sizeof l->l_addr * 2)
880 _dl_sysdep_message ("\t", l->l_libname->name, " => ",
881 l->l_name, " (0x", bp, ")\n", NULL);
885 if (__builtin_expect (mode, trace) != trace)
886 for (i = 1; i < _dl_argc; ++i)
888 const ElfW(Sym) *ref = NULL;
889 ElfW(Addr) loadbase = _dl_lookup_symbol (_dl_argv[i], _dl_loaded,
890 &ref, _dl_loaded->l_scope,
891 ELF_MACHINE_JMP_SLOT);
893 buf[sizeof buf - 1] = '\0';
894 bp = _itoa_word (ref->st_value, &buf[sizeof buf - 1], 16, 0);
895 while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2)
897 _dl_sysdep_message (_dl_argv[i], " found at 0x", bp, NULL);
898 buf[sizeof buf - 1] = '\0';
899 bp = _itoa_word (loadbase, &buf[sizeof buf - 1], 16, 0);
900 while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2)
902 _dl_sysdep_message (" in object at 0x", bp, "\n", NULL);
908 /* We have to do symbol dependency testing. */
909 struct relocate_args args;
912 args.lazy = _dl_lazy;
919 if (l != &_dl_rtld_map && l->l_opencount > 0)
922 _dl_receive_error (print_unresolved, relocate_doit,
929 #define VERNEEDTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
932 /* Print more information. This means here, print information
933 about the versions needed. */
935 struct link_map *map = _dl_loaded;
937 for (map = _dl_loaded; map != NULL; map = map->l_next)
940 ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG];
946 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
947 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
951 _dl_sysdep_message ("\n\tVersion information:\n", NULL);
955 _dl_sysdep_message ("\t", (map->l_name[0]
956 ? map->l_name : _dl_argv[0]),
962 struct link_map *needed;
964 needed = find_needed (strtab + ent->vn_file);
965 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
969 const char *fname = NULL;
971 _dl_sysdep_message ("\t\t",
972 strtab + ent->vn_file,
973 " (", strtab + aux->vna_name,
981 && match_version (strtab+aux->vna_name, needed))
982 fname = needed->l_name;
984 _dl_sysdep_message (fname ?: "not found", "\n",
987 if (aux->vna_next == 0)
988 /* No more symbols. */
992 aux = (ElfW(Vernaux) *) ((char *) aux
996 if (ent->vn_next == 0)
997 /* No more dependencies. */
1000 /* Next dependency. */
1001 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
1011 /* Now we have all the objects loaded. Relocate them all except for
1012 the dynamic linker itself. We do this in reverse order so that copy
1013 relocs of earlier objects overwrite the data written by later
1014 objects. We do not re-relocate the dynamic linker itself in this
1015 loop because that could result in the GOT entries for functions we
1016 call being changed, and that would break us. It is safe to relocate
1017 the dynamic linker out of order because it has no copy relocs (we
1018 know that because it is self-contained). */
1021 int consider_profiling = _dl_profile != NULL;
1022 #ifndef HP_TIMING_NONAVAIL
1028 /* If we are profiling we also must do lazy reloaction. */
1029 _dl_lazy |= consider_profiling;
1035 HP_TIMING_NOW (start);
1038 if (l != &_dl_rtld_map)
1039 _dl_relocate_object (l, l->l_scope, _dl_lazy, consider_profiling);
1044 HP_TIMING_NOW (stop);
1046 HP_TIMING_DIFF (relocate_time, start, stop);
1048 /* Do any necessary cleanups for the startup OS interface code.
1049 We do these now so that no calls are made after rtld re-relocation
1050 which might be resolved to different functions than we expect.
1051 We cannot do this before relocating the other objects because
1052 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
1053 _dl_sysdep_start_cleanup ();
1055 /* Now enable profiling if needed. Like the previous call,
1056 this has to go here because the calls it makes should use the
1057 rtld versions of the functions (particularly calloc()), but it
1058 needs to have _dl_profile_map set up by the relocator. */
1059 if (_dl_profile_map != NULL)
1060 /* We must prepare the profiling. */
1061 _dl_start_profile (_dl_profile_map, _dl_profile_output);
1063 if (_dl_rtld_map.l_opencount > 1)
1065 /* There was an explicit ref to the dynamic linker as a shared lib.
1066 Re-relocate ourselves with user-controlled symbol definitions. */
1067 HP_TIMING_NOW (start);
1068 _dl_relocate_object (&_dl_rtld_map, _dl_loaded->l_scope, 0, 0);
1069 HP_TIMING_NOW (stop);
1070 HP_TIMING_DIFF (add, start, stop);
1071 HP_TIMING_ACCUM_NT (relocate_time, add);
1075 /* Now set up the variable which helps the assembler startup code. */
1076 _dl_main_searchlist = &_dl_loaded->l_searchlist;
1077 _dl_global_scope[0] = &_dl_loaded->l_searchlist;
1079 /* Safe the information about the original global scope list since
1080 we need it in the memory handling later. */
1081 _dl_initial_searchlist = *_dl_main_searchlist;
1084 /* Initialize _r_debug. */
1085 struct r_debug *r = _dl_debug_initialize (_dl_rtld_map.l_addr);
1090 #ifdef ELF_MACHINE_DEBUG_SETUP
1092 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
1094 ELF_MACHINE_DEBUG_SETUP (l, r);
1095 ELF_MACHINE_DEBUG_SETUP (&_dl_rtld_map, r);
1099 if (l->l_info[DT_DEBUG])
1100 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
1101 with the run-time address of the r_debug structure */
1102 l->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1104 /* Fill in the pointer in the dynamic linker's own dynamic section, in
1105 case you run gdb on the dynamic linker directly. */
1106 if (_dl_rtld_map.l_info[DT_DEBUG])
1107 _dl_rtld_map.l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1111 /* Notify the debugger that all objects are now mapped in. */
1112 r->r_state = RT_ADD;
1117 /* We must munmap() the cache file. */
1118 _dl_unload_cache ();
1121 /* Once we return, _dl_sysdep_start will invoke
1122 the DT_INIT functions and then *USER_ENTRY. */
1125 /* This is a little helper function for resolving symbols while
1126 tracing the binary. */
1128 print_unresolved (int errcode __attribute__ ((unused)), const char *objname,
1129 const char *errstring)
1131 if (objname[0] == '\0')
1132 objname = _dl_argv[0] ?: "<main program>";
1133 _dl_sysdep_error (errstring, " (", objname, ")\n", NULL);
1136 /* This is a little helper function for resolving symbols while
1137 tracing the binary. */
1139 print_missing_version (int errcode __attribute__ ((unused)),
1140 const char *objname, const char *errstring)
1142 _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>", ": ",
1143 objname, ": ", errstring, "\n", NULL);
1146 /* Nonzero if any of the debugging options is enabled. */
1147 static int any_debug;
1149 /* Process the string given as the parameter which explains which debugging
1150 options are enabled. */
1152 process_dl_debug (const char *dl_debug)
1155 #define separators " ,:"
1159 /* Skip separating white spaces and commas. */
1160 dl_debug += strspn (dl_debug, separators);
1161 if (*dl_debug != '\0')
1163 len = strcspn (dl_debug, separators);
1168 /* This option is not documented since it is not generally
1170 if (memcmp (dl_debug, "all", 3) == 0)
1173 _dl_debug_impcalls = 1;
1174 _dl_debug_reloc = 1;
1175 _dl_debug_files = 1;
1176 _dl_debug_symbols = 1;
1177 _dl_debug_bindings = 1;
1178 _dl_debug_versions = 1;
1185 if (memcmp (dl_debug, "help", 4) == 0)
1187 _dl_sysdep_message ("\
1188 Valid options for the LD_DEBUG environment variable are:\n\
1190 bindings display information about symbol binding\n\
1191 files display processing of files and libraries\n\
1192 help display this help message and exit\n\
1193 libs display library search paths\n\
1194 reloc display relocation processing\n\
1195 statistics display relocation statistics\n\
1196 symbols display symbol table processing\n\
1197 versions display version dependencies\n\
1199 To direct the debugging output into a file instead of standard output\n\
1200 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n",
1205 if (memcmp (dl_debug, "libs", 4) == 0)
1208 _dl_debug_impcalls = 1;
1215 if (memcmp (dl_debug, "reloc", 5) == 0)
1217 _dl_debug_reloc = 1;
1218 _dl_debug_impcalls = 1;
1223 if (memcmp (dl_debug, "files", 5) == 0)
1225 _dl_debug_files = 1;
1226 _dl_debug_impcalls = 1;
1233 if (memcmp (dl_debug, "symbols", 7) == 0)
1235 _dl_debug_symbols = 1;
1236 _dl_debug_impcalls = 1;
1243 if (memcmp (dl_debug, "bindings", 8) == 0)
1245 _dl_debug_bindings = 1;
1246 _dl_debug_impcalls = 1;
1251 if (memcmp (dl_debug, "versions", 8) == 0)
1253 _dl_debug_versions = 1;
1254 _dl_debug_impcalls = 1;
1261 if (memcmp (dl_debug, "statistics", 10) == 0)
1263 _dl_debug_statistics = 1;
1273 /* Display a warning and skip everything until next separator. */
1274 char *startp = strndupa (dl_debug, len);
1275 _dl_sysdep_error ("warning: debug option `", startp,
1276 "' unknown; try LD_DEBUG=help\n", NULL);
1281 while (*(dl_debug += len) != '\0');
1284 /* Process all environments variables the dynamic linker must recognize.
1285 Since all of them start with `LD_' we are a bit smarter while finding
1288 process_envvars (enum mode *modep, int *lazyp)
1292 enum mode mode = normal;
1294 char *debug_output = NULL;
1296 /* This is the default place for profiling data file. */
1297 _dl_profile_output = "/var/tmp";
1299 while ((envline = _dl_next_ld_env_entry (&runp)) != NULL)
1301 size_t len = strcspn (envline, "=") - 3;
1306 /* Warning level, verbose or not. */
1307 if (memcmp (&envline[3], "WARN", 4) == 0)
1308 _dl_verbose = envline[8] != '\0';
1312 /* Debugging of the dynamic linker? */
1313 if (memcmp (&envline[3], "DEBUG", 5) == 0)
1314 process_dl_debug (&envline[9]);
1318 /* Print information about versions. */
1319 if (memcmp (&envline[3], "VERBOSE", 7) == 0)
1321 version_info = envline[11] != '\0';
1325 /* List of objects to be preloaded. */
1326 if (memcmp (&envline[3], "PRELOAD", 7) == 0)
1328 preloadlist = &envline[11];
1332 /* Which shared object shall be profiled. */
1333 if (memcmp (&envline[3], "PROFILE", 7) == 0)
1334 _dl_profile = &envline[11];
1338 /* Do we bind early? */
1339 if (memcmp (&envline[3], "BIND_NOW", 8) == 0)
1340 bind_now = envline[12] != '\0';
1344 /* Test whether we want to see the content of the auxiliary
1345 array passed up from the kernel. */
1346 if (memcmp (&envline[3], "SHOW_AUXV", 9) == 0)
1351 /* Mask for the important hardware capabilities. */
1352 if (memcmp (&envline[3], "HWCAP_MASK", 10) == 0)
1353 _dl_hwcap_mask = strtoul (&envline[14], NULL, 0);
1357 /* Path where the binary is found. */
1358 if (!__libc_enable_secure
1359 && memcmp (&envline[3], "ORIGIN_PATH", 11) == 0)
1360 _dl_origin_path = &envline[15];
1364 /* Where to place the profiling data file. */
1365 if (memcmp (&envline[3], "DEBUG_OUTPUT", 12) == 0)
1367 debug_output = &envline[16];
1371 /* The library search path. */
1372 if (memcmp (&envline[3], "LIBRARY_PATH", 12) == 0)
1373 library_path = &envline[16];
1377 /* Where to place the profiling data file. */
1378 if (!__libc_enable_secure
1379 && memcmp (&envline[3], "PROFILE_OUTPUT", 14) == 0)
1381 _dl_profile_output = &envline[18];
1382 if (*_dl_profile_output == '\0')
1383 _dl_profile_output = "/var/tmp";
1388 /* The mode of the dynamic linker can be set. */
1389 if (memcmp (&envline[3], "TRACE_LOADED_OBJECTS", 20) == 0)
1393 /* We might have some extra environment variable to handle. This
1394 is tricky due to the pre-processing of the length of the name
1395 in the switch statement here. The code here assumes that added
1396 environment variables have a different length. */
1397 #ifdef EXTRA_LD_ENVVARS
1403 /* Extra security for SUID binaries. Remove all dangerous environment
1405 if (__libc_enable_secure)
1407 static const char *unsecure_envvars[] =
1409 #ifdef EXTRA_UNSECURE_ENVVARS
1410 EXTRA_UNSECURE_ENVVARS
1415 if (preloadlist != NULL)
1416 unsetenv ("LD_PRELOAD");
1417 if (library_path != NULL)
1418 unsetenv ("LD_LIBRARY_PATH");
1419 if (_dl_origin_path != NULL)
1420 unsetenv ("LD_ORIGIN_PATH");
1421 if (debug_output != NULL)
1422 unsetenv ("LD_DEBUG_OUTPUT");
1423 if (_dl_profile != NULL)
1424 unsetenv ("LD_PROFILE");
1427 cnt < sizeof (unsecure_envvars) / sizeof (unsecure_envvars[0]);
1429 unsetenv (unsecure_envvars[cnt]);
1432 /* The name of the object to profile cannot be empty. */
1433 if (_dl_profile != NULL && *_dl_profile == '\0')
1436 /* If we have to run the dynamic linker in debugging mode and the
1437 LD_DEBUG_OUTPUT environment variable is given, we write the debug
1438 messages to this file. */
1439 if (any_debug && debug_output != NULL && !__libc_enable_secure)
1441 size_t name_len = strlen (debug_output);
1442 char buf[name_len + 12];
1445 buf[name_len + 11] = '\0';
1446 startp = _itoa_word (__getpid (), &buf[name_len + 11], 10, 0);
1448 startp = memcpy (startp - name_len, debug_output, name_len);
1450 _dl_debug_fd = __open (startp, O_WRONLY | O_APPEND | O_CREAT, 0666);
1451 if (_dl_debug_fd == -1)
1452 /* We use standard output if opening the file failed. */
1453 _dl_debug_fd = STDOUT_FILENO;
1456 /* LAZY is determined by the environment variable LD_WARN and
1457 LD_BIND_NOW if we trace the binary. */
1458 if (__builtin_expect (mode, normal) == trace)
1459 *lazyp = _dl_verbose ? !bind_now : -1;
1467 /* Print the various times we collected. */
1469 print_statistics (void)
1472 #ifndef HP_TIMING_NONAVAIL
1476 /* Total time rtld used. */
1477 if (HP_TIMING_AVAIL)
1479 HP_TIMING_PRINT (buf, sizeof (buf), rtld_total_time);
1480 _dl_debug_message (1, "\nruntime linker statistics:\n"
1481 " total startup time in dynamic loader: ",
1485 /* Print relocation statistics. */
1486 if (HP_TIMING_AVAIL)
1488 HP_TIMING_PRINT (buf, sizeof (buf), relocate_time);
1489 _dl_debug_message (1, " time needed for relocation: ", buf,
1491 cp = _itoa_word ((1000 * relocate_time) / rtld_total_time,
1492 buf + sizeof (buf), 10, 0);
1494 switch (buf + sizeof (buf) - cp)
1505 _dl_debug_message (0, " (", buf, "%)\n", NULL);
1508 buf[sizeof (buf) - 1] = '\0';
1509 _dl_debug_message (1, " number of relocations: ",
1510 _itoa_word (_dl_num_relocations,
1511 buf + sizeof (buf) - 1, 10, 0),
1514 #ifndef HP_TIMING_NONAVAIL
1515 /* Time spend while loading the object and the dependencies. */
1516 if (HP_TIMING_AVAIL)
1518 HP_TIMING_PRINT (buf, sizeof (buf), load_time);
1519 _dl_debug_message (1, " time needed to load objects: ", buf,
1521 cp = _itoa_word ((1000 * load_time) / rtld_total_time,
1522 buf + sizeof (buf), 10, 0);
1524 switch (buf + sizeof (buf) - cp)
1535 _dl_debug_message (0, " (", buf, "%)\n", NULL);