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 /* Protec SUID program against misuse of file descriptors. */
56 extern void __libc_check_standard_fds (void);
58 /* Helper function to handle errors while resolving symbols. */
59 static void print_unresolved (int errcode
, const char *objname
,
60 const char *errsting
);
62 /* Helper function to handle errors when a version is missing. */
63 static void print_missing_version (int errcode
, const char *objname
,
64 const char *errsting
);
66 /* Print the various times we collected. */
67 static void print_statistics (void);
69 /* This is a list of all the modes the dynamic loader can be in. */
70 enum mode
{ normal
, list
, verify
, trace
};
72 /* Process all environments variables the dynamic linker must recognize.
73 Since all of them start with `LD_' we are a bit smarter while finding
75 static void process_envvars (enum mode
*modep
, int *lazyp
);
79 unsigned int _dl_skip_args
; /* Nonzero if we were run directly. */
81 const char *_dl_platform
;
82 size_t _dl_platformlen
;
83 unsigned long _dl_hwcap
;
84 fpu_control_t _dl_fpu_control
= _FPU_DEFAULT
;
85 struct r_search_path
*_dl_search_paths
;
86 const char *_dl_profile
;
87 const char *_dl_profile_output
;
88 struct link_map
*_dl_profile_map
;
92 int _dl_debug_impcalls
;
93 int _dl_debug_bindings
;
94 int _dl_debug_symbols
;
95 int _dl_debug_versions
;
98 int _dl_debug_statistics
;
99 const char *_dl_inhibit_rpath
; /* RPATH values which should be
101 const char *_dl_origin_path
;
103 /* This is a pointer to the map for the main object and through it to
104 all loaded objects. */
105 struct link_map
*_dl_loaded
;
106 /* Pointer to the l_searchlist element of the link map of the main object. */
107 struct r_scope_elem
*_dl_main_searchlist
;
108 /* Copy of the content of `_dl_main_searchlist'. */
109 struct r_scope_elem _dl_initial_searchlist
;
110 /* Array which is used when looking up in the global scope. */
111 struct r_scope_elem
*_dl_global_scope
[2];
113 /* During the program run we must not modify the global data of
114 loaded shared object simultanously in two threads. Therefore we
115 protect `_dl_open' and `_dl_close' in dl-close.c.
117 This must be a recursive lock since the initializer function of
118 the loaded object might as well require a call to this function.
119 At this time it is not anymore a problem to modify the tables. */
120 __libc_lock_define_initialized_recursive (, _dl_load_lock
)
122 /* Set nonzero during loading and initialization of executable and
123 libraries, cleared before the executable's entry point runs. This
124 must not be initialized to nonzero, because the unused dynamic
125 linker loaded in for libc.so's "ld.so.1" dep will provide the
126 definition seen by libc.so's initializer; that value must be zero,
127 and will be since that dynamic linker's _dl_start and dl_main will
132 static void dl_main (const ElfW(Phdr
) *phdr
,
134 ElfW(Addr
) *user_entry
);
136 struct link_map _dl_rtld_map
;
137 struct libname_list _dl_rtld_libname
;
138 struct libname_list _dl_rtld_libname2
;
140 /* Variable for statistics. */
141 #ifndef HP_TIMING_NONAVAIL
142 static hp_timing_t rtld_total_time
;
143 static hp_timing_t relocate_time
;
144 static hp_timing_t load_time
;
146 extern unsigned long int _dl_num_relocations
; /* in dl-lookup.c */
148 static ElfW(Addr
) _dl_start_final (void *arg
, struct link_map
*bootstrap_map_p
,
149 hp_timing_t start_time
);
154 #error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
158 _dl_start (void *arg
)
160 struct link_map bootstrap_map
;
161 hp_timing_t start_time
;
164 /* This #define produces dynamic linking inline functions for
165 bootstrap relocation instead of general-purpose relocation. */
166 #define RTLD_BOOTSTRAP
167 #define RESOLVE_MAP(sym, version, flags) \
168 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : &bootstrap_map)
169 #define RESOLVE(sym, version, flags) \
170 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : bootstrap_map.l_addr)
171 #include "dynamic-link.h"
173 if (HP_TIMING_INLINE
&& HP_TIMING_AVAIL
)
174 HP_TIMING_NOW (start_time
);
176 /* Partly clean the `bootstrap_map' structure up. Don't use `memset'
177 since it might nor be built in or inlined and we cannot make function
178 calls at this point. */
180 cnt
< sizeof (bootstrap_map
.l_info
) / sizeof (bootstrap_map
.l_info
[0]);
182 bootstrap_map
.l_info
[cnt
] = 0;
184 /* Figure out the run-time load address of the dynamic linker itself. */
185 bootstrap_map
.l_addr
= elf_machine_load_address ();
187 /* Read our own dynamic section and fill in the info array. */
188 bootstrap_map
.l_ld
= (void *) bootstrap_map
.l_addr
+ elf_machine_dynamic ();
189 elf_get_dynamic_info (&bootstrap_map
);
191 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
192 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map
.l_info
);
195 /* Relocate ourselves so we can do normal function calls and
196 data access using the global offset table. */
198 ELF_DYNAMIC_RELOCATE (&bootstrap_map
, 0, 0);
199 /* Please note that we don't allow profiling of this object and
200 therefore need not test whether we have to allocate the array
201 for the relocation results (as done in dl-reloc.c). */
203 /* Now life is sane; we can call functions and access global data.
204 Set up to use the operating system facilities, and find out from
205 the operating system's program loader where to find the program
206 header table in core. Put the rest of _dl_start into a separate
207 function, that way the compiler cannot put accesses to the GOT
208 before ELF_DYNAMIC_RELOCATE. */
210 ElfW(Addr
) entry
= _dl_start_final (arg
, &bootstrap_map
, start_time
);
212 #ifndef ELF_MACHINE_START_ADDRESS
213 # define ELF_MACHINE_START_ADDRESS(map, start) (start)
216 return ELF_MACHINE_START_ADDRESS (_dl_loaded
, entry
);
222 _dl_start_final (void *arg
, struct link_map
*bootstrap_map_p
,
223 hp_timing_t start_time
)
225 /* The use of `alloca' here looks ridiculous but it helps. The goal
226 is to avoid the function from being inlined. There is no official
227 way to do this so we use this trick. gcc never inlines functions
228 which use `alloca'. */
229 ElfW(Addr
) *start_addr
= alloca (sizeof (ElfW(Addr
)));
233 /* If it hasn't happen yet record the startup time. */
234 if (! HP_TIMING_INLINE
)
235 HP_TIMING_NOW (start_time
);
237 /* Initialize the timing functions. */
238 HP_TIMING_DIFF_INIT ();
241 /* Transfer data about ourselves to the permanent link_map structure. */
242 _dl_rtld_map
.l_addr
= bootstrap_map_p
->l_addr
;
243 _dl_rtld_map
.l_ld
= bootstrap_map_p
->l_ld
;
244 _dl_rtld_map
.l_opencount
= 1;
245 memcpy (_dl_rtld_map
.l_info
, bootstrap_map_p
->l_info
,
246 sizeof _dl_rtld_map
.l_info
);
247 _dl_setup_hash (&_dl_rtld_map
);
249 /* Don't bother trying to work out how ld.so is mapped in memory. */
250 _dl_rtld_map
.l_map_start
= ~0;
251 _dl_rtld_map
.l_map_end
= ~0;
253 /* Call the OS-dependent function to set up life so we can do things like
254 file access. It will call `dl_main' (below) to do all the real work
255 of the dynamic linker, and then unwind our frame and run the user
256 entry point on the same stack we entered on. */
257 *start_addr
= _dl_sysdep_start (arg
, &dl_main
);
258 #ifndef HP_TIMING_NONAVAIL
261 hp_timing_t end_time
;
263 /* Get the current time. */
264 HP_TIMING_NOW (end_time
);
266 /* Compute the difference. */
267 HP_TIMING_DIFF (rtld_total_time
, start_time
, end_time
);
271 if (__builtin_expect (_dl_debug_statistics
, 0))
277 /* Now life is peachy; we can do all normal operations.
278 On to the real work. */
280 void ENTRY_POINT (void);
282 /* Some helper functions. */
284 /* Arguments to relocate_doit. */
293 /* Argument to map_doit. */
295 /* Return value of map_doit. */
296 struct link_map
*main_map
;
299 /* Arguments to version_check_doit. */
300 struct version_check_args
307 relocate_doit (void *a
)
309 struct relocate_args
*args
= (struct relocate_args
*) a
;
311 _dl_relocate_object (args
->l
, args
->l
->l_scope
,
318 struct map_args
*args
= (struct map_args
*) a
;
319 args
->main_map
= _dl_map_object (NULL
, args
->str
, 0, lt_library
, 0);
323 version_check_doit (void *a
)
325 struct version_check_args
*args
= (struct version_check_args
*) a
;
326 if (_dl_check_all_versions (_dl_loaded
, 1, args
->dotrace
) && args
->doexit
)
327 /* We cannot start the application. Abort now. */
332 static inline struct link_map
*
333 find_needed (const char *name
)
335 unsigned int n
= _dl_loaded
->l_searchlist
.r_nlist
;
338 if (_dl_name_match_p (name
, _dl_loaded
->l_searchlist
.r_list
[n
]))
339 return _dl_loaded
->l_searchlist
.r_list
[n
];
341 /* Should never happen. */
346 match_version (const char *string
, struct link_map
*map
)
348 const char *strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
351 #define VERDEFTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
352 if (map
->l_info
[VERDEFTAG
] == NULL
)
353 /* The file has no symbol versioning. */
356 def
= (ElfW(Verdef
) *) ((char *) map
->l_addr
357 + map
->l_info
[VERDEFTAG
]->d_un
.d_ptr
);
360 ElfW(Verdaux
) *aux
= (ElfW(Verdaux
) *) ((char *) def
+ def
->vd_aux
);
362 /* Compare the version strings. */
363 if (strcmp (string
, strtab
+ aux
->vda_name
) == 0)
367 /* If no more definitions we failed to find what we want. */
368 if (def
->vd_next
== 0)
371 /* Next definition. */
372 def
= (ElfW(Verdef
) *) ((char *) def
+ def
->vd_next
);
378 static const char *library_path
; /* The library search path. */
379 static const char *preloadlist
; /* The list preloaded objects. */
380 static int version_info
; /* Nonzero if information about
381 versions has to be printed. */
384 dl_main (const ElfW(Phdr
) *phdr
,
386 ElfW(Addr
) *user_entry
)
388 const ElfW(Phdr
) *ph
;
390 struct link_map
**preloads
;
391 unsigned int npreloads
;
396 int rtld_is_main
= 0;
397 #ifndef HP_TIMING_NONAVAIL
403 /* First thing, if this is a SUID program we make sure that FDs 0,
404 1, and 2 are allocated. If necessary we are doing it ourself.
405 If it is not possible we stop the program. */
406 if (__builtin_expect (__libc_enable_secure
, 0))
407 __libc_check_standard_fds ();
409 /* Process the environment variable which control the behaviour. */
410 process_envvars (&mode
, &_dl_lazy
);
412 /* Set up a flag which tells we are just starting. */
415 if (*user_entry
== (ElfW(Addr
)) &ENTRY_POINT
)
417 /* Ho ho. We are not the program interpreter! We are the program
418 itself! This means someone ran ld.so as a command. Well, that
419 might be convenient to do sometimes. We support it by
420 interpreting the args like this:
422 ld.so PROGRAM ARGS...
424 The first argument is the name of a file containing an ELF
425 executable we will load and run with the following arguments.
426 To simplify life here, PROGRAM is searched for using the
427 normal rules for shared objects, rather than $PATH or anything
428 like that. We just load it and use its entry point; we don't
429 pay attention to its PT_INTERP command (we are the interpreter
430 ourselves). This is an easy way to test a new ld.so before
434 /* Note the place where the dynamic linker actually came from. */
435 _dl_rtld_map
.l_name
= _dl_argv
[0];
438 if (! strcmp (_dl_argv
[1], "--list"))
441 _dl_lazy
= -1; /* This means do no dependency analysis. */
447 else if (! strcmp (_dl_argv
[1], "--verify"))
455 else if (! strcmp (_dl_argv
[1], "--library-path") && _dl_argc
> 2)
457 library_path
= _dl_argv
[2];
463 else if (! strcmp (_dl_argv
[1], "--inhibit-rpath") && _dl_argc
> 2)
465 _dl_inhibit_rpath
= _dl_argv
[2];
474 /* If we have no further argument the program was called incorrectly.
475 Grant the user some education. */
478 Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
479 You have invoked `ld.so', the helper program for shared library executables.\n\
480 This program usually lives in the file `/lib/ld.so', and special directives\n\
481 in executable files using ELF shared libraries tell the system's program\n\
482 loader to load the helper program from this file. This helper program loads\n\
483 the shared libraries needed by the program executable, prepares the program\n\
484 to run, and runs it. You may invoke this helper program directly from the\n\
485 command line to load and run an ELF executable file; this is like executing\n\
486 that file itself, but always uses this helper program from the file you\n\
487 specified, instead of the helper program file specified in the executable\n\
488 file you run. This is mostly of use for maintainers to test new versions\n\
489 of this helper program; chances are you did not intend to run this program.\n\
491 --list list all dependencies and how they are resolved\n\
492 --verify verify that given object really is a dynamically linked\n\
493 object we can handle\n\
494 --library-path PATH use given PATH instead of content of the environment\n\
495 variable LD_LIBRARY_PATH\n\
496 --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\
504 /* Initialize the data structures for the search paths for shared
506 _dl_init_paths (library_path
);
508 if (__builtin_expect (mode
, normal
) == verify
)
511 const char *err_str
= NULL
;
512 struct map_args args
;
514 args
.str
= _dl_argv
[0];
515 (void) _dl_catch_error (&objname
, &err_str
, map_doit
, &args
);
516 if (__builtin_expect (err_str
!= NULL
, 0))
518 free ((char *) err_str
);
519 _exit (EXIT_FAILURE
);
524 HP_TIMING_NOW (start
);
525 _dl_map_object (NULL
, _dl_argv
[0], 0, lt_library
, 0);
526 HP_TIMING_NOW (stop
);
528 HP_TIMING_DIFF (load_time
, start
, stop
);
531 phdr
= _dl_loaded
->l_phdr
;
532 phent
= _dl_loaded
->l_phnum
;
533 /* We overwrite here a pointer to a malloc()ed string. But since
534 the malloc() implementation used at this point is the dummy
535 implementations which has no real free() function it does not
536 makes sense to free the old string first. */
537 _dl_loaded
->l_name
= (char *) "";
538 *user_entry
= _dl_loaded
->l_entry
;
542 /* Create a link_map for the executable itself.
543 This will be what dlopen on "" returns. */
544 _dl_new_object ((char *) "", "", lt_executable
, NULL
);
545 if (_dl_loaded
== NULL
)
546 _dl_sysdep_fatal ("cannot allocate memory for link map\n", NULL
);
547 _dl_loaded
->l_phdr
= phdr
;
548 _dl_loaded
->l_phnum
= phent
;
549 _dl_loaded
->l_entry
= *user_entry
;
550 _dl_loaded
->l_opencount
= 1;
552 /* At this point we are in a bit of trouble. We would have to
553 fill in the values for l_dev and l_ino. But in general we
554 do not know where the file is. We also do not handle AT_EXECFD
555 even if it would be passed up.
557 We leave the values here defined to 0. This is normally no
558 problem as the program code itself is normally no shared
559 object and therefore cannot be loaded dynamically. Nothing
560 prevent the use of dynamic binaries and in these situations
561 we might get problems. We might not be able to find out
562 whether the object is already loaded. But since there is no
563 easy way out and because the dynamic binary must also not
564 have an SONAME we ignore this program for now. If it becomes
565 a problem we can force people using SONAMEs. */
567 /* We delay initializing the path structure until we got the dynamic
568 information for the program. */
571 /* It is not safe to load stuff after the main program. */
572 _dl_loaded
->l_map_end
= ~0;
573 /* Perhaps the executable has no PT_LOAD header entries at all. */
574 _dl_loaded
->l_map_start
= ~0;
576 /* Scan the program header table for the dynamic section. */
577 for (ph
= phdr
; ph
< &phdr
[phent
]; ++ph
)
581 /* Find out the load address. */
582 _dl_loaded
->l_addr
= (ElfW(Addr
)) phdr
- ph
->p_vaddr
;
585 /* This tells us where to find the dynamic section,
586 which tells us everything we need to do. */
587 _dl_loaded
->l_ld
= (void *) _dl_loaded
->l_addr
+ ph
->p_vaddr
;
590 /* This "interpreter segment" was used by the program loader to
591 find the program interpreter, which is this program itself, the
592 dynamic linker. We note what name finds us, so that a future
593 dlopen call or DT_NEEDED entry, for something that wants to link
594 against the dynamic linker as a shared library, will know that
595 the shared object is already loaded. */
596 _dl_rtld_libname
.name
= ((const char *) _dl_loaded
->l_addr
598 _dl_rtld_libname
.next
= NULL
;
599 _dl_rtld_map
.l_libname
= &_dl_rtld_libname
;
601 /* Ordinarilly, we would get additional names for the loader from
602 our DT_SONAME. This can't happen if we were actually linked as
603 a static executable (detect this case when we have no DYNAMIC).
604 If so, assume the filename component of the interpreter path to
605 be our SONAME, and add it to our name list. */
606 if (_dl_rtld_map
.l_ld
== NULL
)
608 char *p
= strrchr (_dl_rtld_libname
.name
, '/');
611 _dl_rtld_libname2
.name
= p
+1;
612 _dl_rtld_libname2
.next
= NULL
;
613 _dl_rtld_libname
.next
= &_dl_rtld_libname2
;
620 /* Remember where the main program starts in memory. */
623 mapstart
= _dl_loaded
->l_addr
+ (ph
->p_vaddr
& ~(ph
->p_align
- 1));
624 if (_dl_loaded
->l_map_start
> mapstart
)
625 _dl_loaded
->l_map_start
= mapstart
;
629 if (! _dl_rtld_map
.l_libname
&& _dl_rtld_map
.l_name
)
631 /* We were invoked directly, so the program might not have a
633 _dl_rtld_libname
.name
= _dl_rtld_map
.l_name
;
634 _dl_rtld_libname
.next
= NULL
;
635 _dl_rtld_map
.l_libname
= &_dl_rtld_libname
;
638 assert (_dl_rtld_map
.l_libname
); /* How else did we get here? */
642 /* Extract the contents of the dynamic section for easy access. */
643 elf_get_dynamic_info (_dl_loaded
);
644 if (_dl_loaded
->l_info
[DT_HASH
])
645 /* Set up our cache of pointers into the hash table. */
646 _dl_setup_hash (_dl_loaded
);
649 if (__builtin_expect (mode
, normal
) == verify
)
651 /* We were called just to verify that this is a dynamic
652 executable using us as the program interpreter. Exit with an
653 error if we were not able to load the binary or no interpreter
654 is specified (i.e., this is no dynamically linked binary. */
655 if (_dl_loaded
->l_ld
== NULL
)
658 /* We allow here some platform specific code. */
659 #ifdef DISTINGUISH_LIB_VERSIONS
660 DISTINGUISH_LIB_VERSIONS
;
662 _exit (has_interp
? 0 : 2);
666 /* Initialize the data structures for the search paths for shared
668 _dl_init_paths (library_path
);
670 /* Put the link_map for ourselves on the chain so it can be found by
671 name. Note that at this point the global chain of link maps contains
672 exactly one element, which is pointed to by _dl_loaded. */
673 if (! _dl_rtld_map
.l_name
)
674 /* If not invoked directly, the dynamic linker shared object file was
675 found by the PT_INTERP name. */
676 _dl_rtld_map
.l_name
= (char *) _dl_rtld_map
.l_libname
->name
;
677 _dl_rtld_map
.l_type
= lt_library
;
678 _dl_loaded
->l_next
= &_dl_rtld_map
;
679 _dl_rtld_map
.l_prev
= _dl_loaded
;
681 /* We have two ways to specify objects to preload: via environment
682 variable and via the file /etc/ld.so.preload. The latter can also
683 be used when security is enabled. */
687 if (__builtin_expect (preloadlist
!= NULL
, 0))
689 /* The LD_PRELOAD environment variable gives list of libraries
690 separated by white space or colons that are loaded before the
691 executable's dependencies and prepended to the global scope
692 list. If the binary is running setuid all elements
693 containing a '/' are ignored since it is insecure. */
694 char *list
= strdupa (preloadlist
);
697 HP_TIMING_NOW (start
);
699 while ((p
= strsep (&list
, " :")) != NULL
)
701 && (__builtin_expect (! __libc_enable_secure
, 1)
702 || strchr (p
, '/') == NULL
))
704 struct link_map
*new_map
= _dl_map_object (_dl_loaded
, p
, 1,
706 if (new_map
->l_opencount
== 1)
707 /* It is no duplicate. */
711 HP_TIMING_NOW (stop
);
712 HP_TIMING_DIFF (diff
, start
, stop
);
713 HP_TIMING_ACCUM_NT (load_time
, diff
);
716 /* Read the contents of the file. */
717 file
= _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size
,
718 PROT_READ
| PROT_WRITE
);
719 if (__builtin_expect (file
!= NULL
, 0))
721 /* Parse the file. It contains names of libraries to be loaded,
722 separated by white spaces or `:'. It may also contain
723 comments introduced by `#'. */
728 /* Eliminate comments. */
733 char *comment
= memchr (runp
, '#', rest
);
737 rest
-= comment
- runp
;
740 while (--rest
> 0 && *++comment
!= '\n');
743 /* We have one problematic case: if we have a name at the end of
744 the file without a trailing terminating characters, we cannot
745 place the \0. Handle the case separately. */
746 if (file
[file_size
- 1] != ' ' && file
[file_size
- 1] != '\t'
747 && file
[file_size
- 1] != '\n' && file
[file_size
- 1] != ':')
749 problem
= &file
[file_size
];
750 while (problem
> file
&& problem
[-1] != ' ' && problem
[-1] != '\t'
751 && problem
[-1] != '\n' && problem
[-1] != ':')
760 file
[file_size
- 1] = '\0';
763 HP_TIMING_NOW (start
);
769 while ((p
= strsep (&runp
, ": \t\n")) != NULL
)
772 struct link_map
*new_map
= _dl_map_object (_dl_loaded
, p
, 1,
774 if (new_map
->l_opencount
== 1)
775 /* It is no duplicate. */
782 char *p
= strndupa (problem
, file_size
- (problem
- file
));
783 struct link_map
*new_map
= _dl_map_object (_dl_loaded
, p
, 1,
785 if (new_map
->l_opencount
== 1)
786 /* It is no duplicate. */
790 HP_TIMING_NOW (stop
);
791 HP_TIMING_DIFF (diff
, start
, stop
);
792 HP_TIMING_ACCUM_NT (load_time
, diff
);
794 /* We don't need the file anymore. */
795 __munmap (file
, file_size
);
798 if (__builtin_expect (npreloads
, 0) != 0)
800 /* Set up PRELOADS with a vector of the preloaded libraries. */
802 preloads
= __alloca (npreloads
* sizeof preloads
[0]);
803 l
= _dl_rtld_map
.l_next
; /* End of the chain before preloads. */
810 assert (i
== npreloads
);
813 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
814 specified some libraries to load, these are inserted before the actual
815 dependencies in the executable's searchlist for symbol resolution. */
816 HP_TIMING_NOW (start
);
817 _dl_map_object_deps (_dl_loaded
, preloads
, npreloads
, mode
== trace
);
818 HP_TIMING_NOW (stop
);
819 HP_TIMING_DIFF (diff
, start
, stop
);
820 HP_TIMING_ACCUM_NT (load_time
, diff
);
822 /* Mark all objects as being in the global scope. */
823 for (i
= _dl_loaded
->l_searchlist
.r_nlist
; i
> 0; )
824 _dl_loaded
->l_searchlist
.r_list
[--i
]->l_global
= 1;
827 /* We are done mapping things, so close the zero-fill descriptor. */
828 __close (_dl_zerofd
);
832 /* Remove _dl_rtld_map from the chain. */
833 _dl_rtld_map
.l_prev
->l_next
= _dl_rtld_map
.l_next
;
834 if (_dl_rtld_map
.l_next
)
835 _dl_rtld_map
.l_next
->l_prev
= _dl_rtld_map
.l_prev
;
837 if (__builtin_expect (_dl_rtld_map
.l_opencount
, 2) > 1)
839 /* Some DT_NEEDED entry referred to the interpreter object itself, so
840 put it back in the list of visible objects. We insert it into the
841 chain in symbol search order because gdb uses the chain's order as
842 its symbol search order. */
844 while (_dl_loaded
->l_searchlist
.r_list
[i
] != &_dl_rtld_map
)
846 _dl_rtld_map
.l_prev
= _dl_loaded
->l_searchlist
.r_list
[i
- 1];
847 if (__builtin_expect (mode
, normal
) == normal
)
848 _dl_rtld_map
.l_next
= (i
+ 1 < _dl_loaded
->l_searchlist
.r_nlist
849 ? _dl_loaded
->l_searchlist
.r_list
[i
+ 1]
852 /* In trace mode there might be an invisible object (which we
853 could not find) after the previous one in the search list.
854 In this case it doesn't matter much where we put the
855 interpreter object, so we just initialize the list pointer so
856 that the assertion below holds. */
857 _dl_rtld_map
.l_next
= _dl_rtld_map
.l_prev
->l_next
;
859 assert (_dl_rtld_map
.l_prev
->l_next
== _dl_rtld_map
.l_next
);
860 _dl_rtld_map
.l_prev
->l_next
= &_dl_rtld_map
;
861 if (_dl_rtld_map
.l_next
)
863 assert (_dl_rtld_map
.l_next
->l_prev
== _dl_rtld_map
.l_prev
);
864 _dl_rtld_map
.l_next
->l_prev
= &_dl_rtld_map
;
868 /* Now let us see whether all libraries are available in the
871 struct version_check_args args
;
872 args
.doexit
= mode
== normal
;
873 args
.dotrace
= mode
== trace
;
874 _dl_receive_error (print_missing_version
, version_check_doit
, &args
);
877 if (__builtin_expect (mode
, normal
) != normal
)
879 /* We were run just to list the shared libraries. It is
880 important that we do this before real relocation, because the
881 functions we call below for output may no longer work properly
883 if (! _dl_loaded
->l_info
[DT_NEEDED
])
884 _dl_sysdep_message ("\t", "statically linked\n", NULL
);
889 for (l
= _dl_loaded
->l_next
; l
; l
= l
->l_next
)
890 if (l
->l_opencount
== 0)
891 /* The library was not found. */
892 _dl_sysdep_message ("\t", l
->l_libname
->name
, " => not found\n",
897 buf
[sizeof buf
- 1] = '\0';
898 bp
= _itoa_word (l
->l_addr
, &buf
[sizeof buf
- 1], 16, 0);
899 while ((size_t) (&buf
[sizeof buf
- 1] - bp
)
900 < sizeof l
->l_addr
* 2)
902 _dl_sysdep_message ("\t", l
->l_libname
->name
, " => ",
903 l
->l_name
, " (0x", bp
, ")\n", NULL
);
907 if (__builtin_expect (mode
, trace
) != trace
)
908 for (i
= 1; i
< _dl_argc
; ++i
)
910 const ElfW(Sym
) *ref
= NULL
;
915 result
= _dl_lookup_symbol (_dl_argv
[i
], _dl_loaded
,
916 &ref
, _dl_loaded
->l_scope
,
917 ELF_MACHINE_JMP_SLOT
);
919 loadbase
= LOOKUP_VALUE_ADDRESS (result
);
921 buf
[sizeof buf
- 1] = '\0';
922 bp
= _itoa_word (ref
->st_value
, &buf
[sizeof buf
- 1], 16, 0);
923 while ((size_t) (&buf
[sizeof buf
- 1] - bp
) < sizeof loadbase
* 2)
925 _dl_sysdep_message (_dl_argv
[i
], " found at 0x", bp
, NULL
);
926 buf
[sizeof buf
- 1] = '\0';
927 bp
= _itoa_word (loadbase
, &buf
[sizeof buf
- 1], 16, 0);
928 while ((size_t) (&buf
[sizeof buf
- 1] - bp
) < sizeof loadbase
* 2)
930 _dl_sysdep_message (" in object at 0x", bp
, "\n", NULL
);
936 /* We have to do symbol dependency testing. */
937 struct relocate_args args
;
940 args
.lazy
= _dl_lazy
;
947 if (l
!= &_dl_rtld_map
&& l
->l_opencount
> 0)
950 _dl_receive_error (print_unresolved
, relocate_doit
,
957 #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
960 /* Print more information. This means here, print information
961 about the versions needed. */
963 struct link_map
*map
= _dl_loaded
;
965 for (map
= _dl_loaded
; map
!= NULL
; map
= map
->l_next
)
968 ElfW(Dyn
) *dyn
= map
->l_info
[VERNEEDTAG
];
974 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
975 ent
= (ElfW(Verneed
) *) (map
->l_addr
+ dyn
->d_un
.d_ptr
);
979 _dl_sysdep_message ("\n\tVersion information:\n", NULL
);
983 _dl_sysdep_message ("\t", (map
->l_name
[0]
984 ? map
->l_name
: _dl_argv
[0]),
990 struct link_map
*needed
;
992 needed
= find_needed (strtab
+ ent
->vn_file
);
993 aux
= (ElfW(Vernaux
) *) ((char *) ent
+ ent
->vn_aux
);
997 const char *fname
= NULL
;
999 _dl_sysdep_message ("\t\t",
1000 strtab
+ ent
->vn_file
,
1001 " (", strtab
+ aux
->vna_name
,
1009 && match_version (strtab
+aux
->vna_name
, needed
))
1010 fname
= needed
->l_name
;
1012 _dl_sysdep_message (fname
?: "not found", "\n",
1015 if (aux
->vna_next
== 0)
1016 /* No more symbols. */
1020 aux
= (ElfW(Vernaux
) *) ((char *) aux
1024 if (ent
->vn_next
== 0)
1025 /* No more dependencies. */
1028 /* Next dependency. */
1029 ent
= (ElfW(Verneed
) *) ((char *) ent
+ ent
->vn_next
);
1039 /* Now we have all the objects loaded. Relocate them all except for
1040 the dynamic linker itself. We do this in reverse order so that copy
1041 relocs of earlier objects overwrite the data written by later
1042 objects. We do not re-relocate the dynamic linker itself in this
1043 loop because that could result in the GOT entries for functions we
1044 call being changed, and that would break us. It is safe to relocate
1045 the dynamic linker out of order because it has no copy relocs (we
1046 know that because it is self-contained). */
1049 int consider_profiling
= _dl_profile
!= NULL
;
1050 #ifndef HP_TIMING_NONAVAIL
1056 /* If we are profiling we also must do lazy reloaction. */
1057 _dl_lazy
|= consider_profiling
;
1063 HP_TIMING_NOW (start
);
1066 if (l
!= &_dl_rtld_map
)
1067 _dl_relocate_object (l
, l
->l_scope
, _dl_lazy
, consider_profiling
);
1072 HP_TIMING_NOW (stop
);
1074 HP_TIMING_DIFF (relocate_time
, start
, stop
);
1076 /* Do any necessary cleanups for the startup OS interface code.
1077 We do these now so that no calls are made after rtld re-relocation
1078 which might be resolved to different functions than we expect.
1079 We cannot do this before relocating the other objects because
1080 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
1081 _dl_sysdep_start_cleanup ();
1083 /* Now enable profiling if needed. Like the previous call,
1084 this has to go here because the calls it makes should use the
1085 rtld versions of the functions (particularly calloc()), but it
1086 needs to have _dl_profile_map set up by the relocator. */
1087 if (__builtin_expect (_dl_profile_map
!= NULL
, 0))
1088 /* We must prepare the profiling. */
1089 _dl_start_profile (_dl_profile_map
, _dl_profile_output
);
1091 if (_dl_rtld_map
.l_opencount
> 1)
1093 /* There was an explicit ref to the dynamic linker as a shared lib.
1094 Re-relocate ourselves with user-controlled symbol definitions. */
1095 HP_TIMING_NOW (start
);
1096 _dl_relocate_object (&_dl_rtld_map
, _dl_loaded
->l_scope
, 0, 0);
1097 HP_TIMING_NOW (stop
);
1098 HP_TIMING_DIFF (add
, start
, stop
);
1099 HP_TIMING_ACCUM_NT (relocate_time
, add
);
1103 /* Now set up the variable which helps the assembler startup code. */
1104 _dl_main_searchlist
= &_dl_loaded
->l_searchlist
;
1105 _dl_global_scope
[0] = &_dl_loaded
->l_searchlist
;
1107 /* Safe the information about the original global scope list since
1108 we need it in the memory handling later. */
1109 _dl_initial_searchlist
= *_dl_main_searchlist
;
1112 /* Initialize _r_debug. */
1113 struct r_debug
*r
= _dl_debug_initialize (_dl_rtld_map
.l_addr
);
1118 #ifdef ELF_MACHINE_DEBUG_SETUP
1120 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
1122 ELF_MACHINE_DEBUG_SETUP (l
, r
);
1123 ELF_MACHINE_DEBUG_SETUP (&_dl_rtld_map
, r
);
1127 if (l
->l_info
[DT_DEBUG
])
1128 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
1129 with the run-time address of the r_debug structure */
1130 l
->l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
1132 /* Fill in the pointer in the dynamic linker's own dynamic section, in
1133 case you run gdb on the dynamic linker directly. */
1134 if (_dl_rtld_map
.l_info
[DT_DEBUG
])
1135 _dl_rtld_map
.l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
1139 /* Notify the debugger that all objects are now mapped in. */
1140 r
->r_state
= RT_ADD
;
1145 /* We must munmap() the cache file. */
1146 _dl_unload_cache ();
1149 /* Once we return, _dl_sysdep_start will invoke
1150 the DT_INIT functions and then *USER_ENTRY. */
1153 /* This is a little helper function for resolving symbols while
1154 tracing the binary. */
1156 print_unresolved (int errcode
__attribute__ ((unused
)), const char *objname
,
1157 const char *errstring
)
1159 if (objname
[0] == '\0')
1160 objname
= _dl_argv
[0] ?: "<main program>";
1161 _dl_sysdep_error (errstring
, " (", objname
, ")\n", NULL
);
1164 /* This is a little helper function for resolving symbols while
1165 tracing the binary. */
1167 print_missing_version (int errcode
__attribute__ ((unused
)),
1168 const char *objname
, const char *errstring
)
1170 _dl_sysdep_error (_dl_argv
[0] ?: "<program name unknown>", ": ",
1171 objname
, ": ", errstring
, "\n", NULL
);
1174 /* Nonzero if any of the debugging options is enabled. */
1175 static int any_debug
;
1177 /* Process the string given as the parameter which explains which debugging
1178 options are enabled. */
1180 process_dl_debug (const char *dl_debug
)
1183 #define separators " ,:"
1187 /* Skip separating white spaces and commas. */
1188 dl_debug
+= strspn (dl_debug
, separators
);
1189 if (*dl_debug
!= '\0')
1191 len
= strcspn (dl_debug
, separators
);
1196 /* This option is not documented since it is not generally
1198 if (memcmp (dl_debug
, "all", 3) == 0)
1201 _dl_debug_impcalls
= 1;
1202 _dl_debug_reloc
= 1;
1203 _dl_debug_files
= 1;
1204 _dl_debug_symbols
= 1;
1205 _dl_debug_bindings
= 1;
1206 _dl_debug_versions
= 1;
1213 if (memcmp (dl_debug
, "help", 4) == 0)
1215 _dl_sysdep_message ("\
1216 Valid options for the LD_DEBUG environment variable are:\n\
1218 bindings display information about symbol binding\n\
1219 files display processing of files and libraries\n\
1220 help display this help message and exit\n\
1221 libs display library search paths\n\
1222 reloc display relocation processing\n\
1223 statistics display relocation statistics\n\
1224 symbols display symbol table processing\n\
1225 versions display version dependencies\n\
1227 To direct the debugging output into a file instead of standard output\n\
1228 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n",
1233 if (memcmp (dl_debug
, "libs", 4) == 0)
1236 _dl_debug_impcalls
= 1;
1243 if (memcmp (dl_debug
, "reloc", 5) == 0)
1245 _dl_debug_reloc
= 1;
1246 _dl_debug_impcalls
= 1;
1251 if (memcmp (dl_debug
, "files", 5) == 0)
1253 _dl_debug_files
= 1;
1254 _dl_debug_impcalls
= 1;
1261 if (memcmp (dl_debug
, "symbols", 7) == 0)
1263 _dl_debug_symbols
= 1;
1264 _dl_debug_impcalls
= 1;
1271 if (memcmp (dl_debug
, "bindings", 8) == 0)
1273 _dl_debug_bindings
= 1;
1274 _dl_debug_impcalls
= 1;
1279 if (memcmp (dl_debug
, "versions", 8) == 0)
1281 _dl_debug_versions
= 1;
1282 _dl_debug_impcalls
= 1;
1289 if (memcmp (dl_debug
, "statistics", 10) == 0)
1291 _dl_debug_statistics
= 1;
1301 /* Display a warning and skip everything until next separator. */
1302 char *startp
= strndupa (dl_debug
, len
);
1303 _dl_sysdep_error ("warning: debug option `", startp
,
1304 "' unknown; try LD_DEBUG=help\n", NULL
);
1309 while (*(dl_debug
+= len
) != '\0');
1312 /* Process all environments variables the dynamic linker must recognize.
1313 Since all of them start with `LD_' we are a bit smarter while finding
1316 process_envvars (enum mode
*modep
, int *lazyp
)
1320 enum mode mode
= normal
;
1322 char *debug_output
= NULL
;
1324 /* This is the default place for profiling data file. */
1325 _dl_profile_output
= "/var/tmp";
1327 while ((envline
= _dl_next_ld_env_entry (&runp
)) != NULL
)
1329 size_t len
= strcspn (envline
, "=");
1331 if (envline
[len
] != '=')
1332 /* This is a "LD_" variable at the end of the string without
1333 a '=' character. Ignore it since otherwise we will access
1334 invalid memory below. */
1340 /* Warning level, verbose or not. */
1341 if (memcmp (&envline
[3], "WARN", 4) == 0)
1342 _dl_verbose
= envline
[8] != '\0';
1346 /* Debugging of the dynamic linker? */
1347 if (memcmp (&envline
[3], "DEBUG", 5) == 0)
1348 process_dl_debug (&envline
[9]);
1352 /* Print information about versions. */
1353 if (memcmp (&envline
[3], "VERBOSE", 7) == 0)
1355 version_info
= envline
[11] != '\0';
1359 /* List of objects to be preloaded. */
1360 if (memcmp (&envline
[3], "PRELOAD", 7) == 0)
1362 preloadlist
= &envline
[11];
1366 /* Which shared object shall be profiled. */
1367 if (memcmp (&envline
[3], "PROFILE", 7) == 0)
1368 _dl_profile
= &envline
[11];
1372 /* Do we bind early? */
1373 if (memcmp (&envline
[3], "BIND_NOW", 8) == 0)
1374 bind_now
= envline
[12] != '\0';
1378 /* Test whether we want to see the content of the auxiliary
1379 array passed up from the kernel. */
1380 if (memcmp (&envline
[3], "SHOW_AUXV", 9) == 0)
1385 /* Mask for the important hardware capabilities. */
1386 if (memcmp (&envline
[3], "HWCAP_MASK", 10) == 0)
1387 _dl_hwcap_mask
= strtoul (&envline
[14], NULL
, 0);
1391 /* Path where the binary is found. */
1392 if (!__libc_enable_secure
1393 && memcmp (&envline
[3], "ORIGIN_PATH", 11) == 0)
1394 _dl_origin_path
= &envline
[15];
1398 /* The library search path. */
1399 if (memcmp (&envline
[3], "LIBRARY_PATH", 12) == 0)
1401 library_path
= &envline
[16];
1405 /* Where to place the profiling data file. */
1406 if (memcmp (&envline
[3], "DEBUG_OUTPUT", 12) == 0)
1408 debug_output
= &envline
[16];
1412 if (memcmp (&envline
[3], "DYNAMIC_WEAK", 12) == 0)
1413 _dl_dynamic_weak
= 1;
1417 /* Where to place the profiling data file. */
1418 if (!__libc_enable_secure
1419 && memcmp (&envline
[3], "PROFILE_OUTPUT", 14) == 0)
1421 _dl_profile_output
= &envline
[18];
1422 if (*_dl_profile_output
== '\0')
1423 _dl_profile_output
= "/var/tmp";
1428 /* The mode of the dynamic linker can be set. */
1429 if (memcmp (&envline
[3], "TRACE_LOADED_OBJECTS", 20) == 0)
1433 /* We might have some extra environment variable to handle. This
1434 is tricky due to the pre-processing of the length of the name
1435 in the switch statement here. The code here assumes that added
1436 environment variables have a different length. */
1437 #ifdef EXTRA_LD_ENVVARS
1443 /* Extra security for SUID binaries. Remove all dangerous environment
1445 if (__libc_enable_secure
)
1447 static const char *unsecure_envvars
[] =
1449 #ifdef EXTRA_UNSECURE_ENVVARS
1450 EXTRA_UNSECURE_ENVVARS
1455 if (preloadlist
!= NULL
)
1456 unsetenv ("LD_PRELOAD");
1457 if (library_path
!= NULL
)
1458 unsetenv ("LD_LIBRARY_PATH");
1459 if (_dl_origin_path
!= NULL
)
1460 unsetenv ("LD_ORIGIN_PATH");
1461 if (debug_output
!= NULL
)
1462 unsetenv ("LD_DEBUG_OUTPUT");
1463 if (_dl_profile
!= NULL
)
1464 unsetenv ("LD_PROFILE");
1467 cnt
< sizeof (unsecure_envvars
) / sizeof (unsecure_envvars
[0]);
1469 unsetenv (unsecure_envvars
[cnt
]);
1472 /* The name of the object to profile cannot be empty. */
1473 if (_dl_profile
!= NULL
&& *_dl_profile
== '\0')
1476 /* If we have to run the dynamic linker in debugging mode and the
1477 LD_DEBUG_OUTPUT environment variable is given, we write the debug
1478 messages to this file. */
1479 if (any_debug
&& debug_output
!= NULL
&& !__libc_enable_secure
)
1481 size_t name_len
= strlen (debug_output
);
1482 char buf
[name_len
+ 12];
1485 buf
[name_len
+ 11] = '\0';
1486 startp
= _itoa_word (__getpid (), &buf
[name_len
+ 11], 10, 0);
1488 startp
= memcpy (startp
- name_len
, debug_output
, name_len
);
1490 _dl_debug_fd
= __open (startp
, O_WRONLY
| O_APPEND
| O_CREAT
, 0666);
1491 if (_dl_debug_fd
== -1)
1492 /* We use standard output if opening the file failed. */
1493 _dl_debug_fd
= STDOUT_FILENO
;
1496 /* LAZY is determined by the environment variable LD_WARN and
1497 LD_BIND_NOW if we trace the binary. */
1498 if (__builtin_expect (mode
, normal
) == trace
)
1499 *lazyp
= _dl_verbose
? !bind_now
: -1;
1507 /* Print the various times we collected. */
1509 print_statistics (void)
1512 #ifndef HP_TIMING_NONAVAIL
1516 /* Total time rtld used. */
1517 if (HP_TIMING_AVAIL
)
1519 HP_TIMING_PRINT (buf
, sizeof (buf
), rtld_total_time
);
1520 _dl_debug_message (1, "\nruntime linker statistics:\n"
1521 " total startup time in dynamic loader: ",
1525 /* Print relocation statistics. */
1526 if (HP_TIMING_AVAIL
)
1528 HP_TIMING_PRINT (buf
, sizeof (buf
), relocate_time
);
1529 _dl_debug_message (1, " time needed for relocation: ", buf
,
1531 cp
= _itoa_word ((1000 * relocate_time
) / rtld_total_time
,
1532 buf
+ sizeof (buf
), 10, 0);
1534 switch (buf
+ sizeof (buf
) - cp
)
1545 _dl_debug_message (0, " (", buf
, "%)\n", NULL
);
1548 buf
[sizeof (buf
) - 1] = '\0';
1549 _dl_debug_message (1, " number of relocations: ",
1550 _itoa_word (_dl_num_relocations
,
1551 buf
+ sizeof (buf
) - 1, 10, 0),
1554 #ifndef HP_TIMING_NONAVAIL
1555 /* Time spend while loading the object and the dependencies. */
1556 if (HP_TIMING_AVAIL
)
1558 HP_TIMING_PRINT (buf
, sizeof (buf
), load_time
);
1559 _dl_debug_message (1, " time needed to load objects: ", buf
,
1561 cp
= _itoa_word ((1000 * load_time
) / rtld_total_time
,
1562 buf
+ sizeof (buf
), 10, 0);
1564 switch (buf
+ sizeof (buf
) - cp
)
1575 _dl_debug_message (0, " (", buf
, "%)\n", NULL
);