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 static hp_timing_t rtld_total_time
;
138 static hp_timing_t relocate_time
;
139 static hp_timing_t load_time
;
140 extern unsigned long int _dl_num_relocations
; /* in dl-lookup.c */
142 static ElfW(Addr
) _dl_start_final (void *arg
, struct link_map
*bootstrap_map_p
,
143 hp_timing_t start_time
);
148 #error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
152 _dl_start (void *arg
)
154 struct link_map bootstrap_map
;
155 hp_timing_t start_time
;
158 /* This #define produces dynamic linking inline functions for
159 bootstrap relocation instead of general-purpose relocation. */
160 #define RTLD_BOOTSTRAP
161 #define RESOLVE(sym, version, flags) \
162 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : bootstrap_map.l_addr)
163 #include "dynamic-link.h"
165 if (HP_TIMING_INLINE
&& HP_TIMING_AVAIL
)
166 HP_TIMING_NOW (start_time
);
168 /* Partly clean the `bootstrap_map' structure up. Don't use `memset'
169 since it might nor be built in or inlined and we cannot make function
170 calls at this point. */
172 cnt
< sizeof (bootstrap_map
.l_info
) / sizeof (bootstrap_map
.l_info
[0]);
174 bootstrap_map
.l_info
[cnt
] = 0;
176 /* Figure out the run-time load address of the dynamic linker itself. */
177 bootstrap_map
.l_addr
= elf_machine_load_address ();
179 /* Read our own dynamic section and fill in the info array. */
180 bootstrap_map
.l_ld
= (void *) bootstrap_map
.l_addr
+ elf_machine_dynamic ();
181 elf_get_dynamic_info (&bootstrap_map
);
183 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
184 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map
.l_info
);
187 /* Relocate ourselves so we can do normal function calls and
188 data access using the global offset table. */
190 ELF_DYNAMIC_RELOCATE (&bootstrap_map
, 0, 0);
191 /* Please note that we don't allow profiling of this object and
192 therefore need not test whether we have to allocate the array
193 for the relocation results (as done in dl-reloc.c). */
195 /* Now life is sane; we can call functions and access global data.
196 Set up to use the operating system facilities, and find out from
197 the operating system's program loader where to find the program
198 header table in core. Put the rest of _dl_start into a separate
199 function, that way the compiler cannot put accesses to the GOT
200 before ELF_DYNAMIC_RELOCATE. */
201 return _dl_start_final (arg
, &bootstrap_map
, start_time
);
206 _dl_start_final (void *arg
, struct link_map
*bootstrap_map_p
,
207 hp_timing_t start_time
)
209 /* The use of `alloca' here looks ridiculous but it helps. The goal
210 is to avoid the function from being inlined. There is no official
211 way to do this so we use this trick. gcc never inlines functions
212 which use `alloca'. */
213 ElfW(Addr
) *start_addr
= alloca (sizeof (ElfW(Addr
)));
217 /* If it hasn't happen yet record the startup time. */
218 if (! HP_TIMING_INLINE
)
219 HP_TIMING_NOW (start_time
);
221 /* Initialize the timing functions. */
222 HP_TIMING_DIFF_INIT ();
225 /* Transfer data about ourselves to the permanent link_map structure. */
226 _dl_rtld_map
.l_addr
= bootstrap_map_p
->l_addr
;
227 _dl_rtld_map
.l_ld
= bootstrap_map_p
->l_ld
;
228 _dl_rtld_map
.l_opencount
= 1;
229 memcpy (_dl_rtld_map
.l_info
, bootstrap_map_p
->l_info
,
230 sizeof _dl_rtld_map
.l_info
);
231 _dl_setup_hash (&_dl_rtld_map
);
233 /* Don't bother trying to work out how ld.so is mapped in memory. */
234 _dl_rtld_map
.l_map_start
= ~0;
235 _dl_rtld_map
.l_map_end
= ~0;
237 /* Call the OS-dependent function to set up life so we can do things like
238 file access. It will call `dl_main' (below) to do all the real work
239 of the dynamic linker, and then unwind our frame and run the user
240 entry point on the same stack we entered on. */
241 *start_addr
= _dl_sysdep_start (arg
, &dl_main
);
245 hp_timing_t end_time
;
247 /* Get the current time. */
248 HP_TIMING_NOW (end_time
);
250 /* Compute the difference. */
251 HP_TIMING_DIFF (rtld_total_time
, start_time
, end_time
);
254 if (_dl_debug_statistics
)
260 /* Now life is peachy; we can do all normal operations.
261 On to the real work. */
263 void ENTRY_POINT (void);
265 /* Some helper functions. */
267 /* Arguments to relocate_doit. */
276 /* Argument to map_doit. */
278 /* Return value of map_doit. */
279 struct link_map
*main_map
;
282 /* Arguments to version_check_doit. */
283 struct version_check_args
289 relocate_doit (void *a
)
291 struct relocate_args
*args
= (struct relocate_args
*) a
;
293 _dl_relocate_object (args
->l
, args
->l
->l_scope
,
300 struct map_args
*args
= (struct map_args
*) a
;
301 args
->main_map
= _dl_map_object (NULL
, args
->str
, 0, lt_library
, 0);
305 version_check_doit (void *a
)
307 struct version_check_args
*args
= (struct version_check_args
*) a
;
308 if (_dl_check_all_versions (_dl_loaded
, 1) && args
->doexit
)
309 /* We cannot start the application. Abort now. */
314 static inline struct link_map
*
315 find_needed (const char *name
)
317 unsigned int n
= _dl_loaded
->l_searchlist
.r_nlist
;
320 if (_dl_name_match_p (name
, _dl_loaded
->l_searchlist
.r_list
[n
]))
321 return _dl_loaded
->l_searchlist
.r_list
[n
];
323 /* Should never happen. */
328 match_version (const char *string
, struct link_map
*map
)
330 const char *strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
333 #define VERDEFTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
334 if (map
->l_info
[VERDEFTAG
] == NULL
)
335 /* The file has no symbol versioning. */
338 def
= (ElfW(Verdef
) *) ((char *) map
->l_addr
339 + map
->l_info
[VERDEFTAG
]->d_un
.d_ptr
);
342 ElfW(Verdaux
) *aux
= (ElfW(Verdaux
) *) ((char *) def
+ def
->vd_aux
);
344 /* Compare the version strings. */
345 if (strcmp (string
, strtab
+ aux
->vda_name
) == 0)
349 /* If no more definitions we failed to find what we want. */
350 if (def
->vd_next
== 0)
353 /* Next definition. */
354 def
= (ElfW(Verdef
) *) ((char *) def
+ def
->vd_next
);
360 static const char *library_path
; /* The library search path. */
361 static const char *preloadlist
; /* The list preloaded objects. */
362 static int version_info
; /* Nonzero if information about
363 versions has to be printed. */
366 dl_main (const ElfW(Phdr
) *phdr
,
368 ElfW(Addr
) *user_entry
)
370 const ElfW(Phdr
) *ph
;
372 struct link_map
**preloads
;
373 unsigned int npreloads
;
378 int rtld_is_main
= 0;
383 /* Process the environment variable which control the behaviour. */
384 process_envvars (&mode
, &_dl_lazy
);
386 /* Set up a flag which tells we are just starting. */
389 if (*user_entry
== (ElfW(Addr
)) &ENTRY_POINT
)
391 /* Ho ho. We are not the program interpreter! We are the program
392 itself! This means someone ran ld.so as a command. Well, that
393 might be convenient to do sometimes. We support it by
394 interpreting the args like this:
396 ld.so PROGRAM ARGS...
398 The first argument is the name of a file containing an ELF
399 executable we will load and run with the following arguments.
400 To simplify life here, PROGRAM is searched for using the
401 normal rules for shared objects, rather than $PATH or anything
402 like that. We just load it and use its entry point; we don't
403 pay attention to its PT_INTERP command (we are the interpreter
404 ourselves). This is an easy way to test a new ld.so before
408 /* Note the place where the dynamic linker actually came from. */
409 _dl_rtld_map
.l_name
= _dl_argv
[0];
412 if (! strcmp (_dl_argv
[1], "--list"))
415 _dl_lazy
= -1; /* This means do no dependency analysis. */
421 else if (! strcmp (_dl_argv
[1], "--verify"))
429 else if (! strcmp (_dl_argv
[1], "--library-path") && _dl_argc
> 2)
431 library_path
= _dl_argv
[2];
437 else if (! strcmp (_dl_argv
[1], "--inhibit-rpath") && _dl_argc
> 2)
439 _dl_inhibit_rpath
= _dl_argv
[2];
448 /* If we have no further argument the program was called incorrectly.
449 Grant the user some education. */
452 Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
453 You have invoked `ld.so', the helper program for shared library executables.\n\
454 This program usually lives in the file `/lib/ld.so', and special directives\n\
455 in executable files using ELF shared libraries tell the system's program\n\
456 loader to load the helper program from this file. This helper program loads\n\
457 the shared libraries needed by the program executable, prepares the program\n\
458 to run, and runs it. You may invoke this helper program directly from the\n\
459 command line to load and run an ELF executable file; this is like executing\n\
460 that file itself, but always uses this helper program from the file you\n\
461 specified, instead of the helper program file specified in the executable\n\
462 file you run. This is mostly of use for maintainers to test new versions\n\
463 of this helper program; chances are you did not intend to run this program.\n\
465 --list list all dependencies and how they are resolved\n\
466 --verify verify that given object really is a dynamically linked\n\
467 object we can handle\n\
468 --library-path PATH use given PATH instead of content of the environment\n\
469 variable LD_LIBRARY_PATH\n\
470 --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\
478 /* Initialize the data structures for the search paths for shared
480 _dl_init_paths (library_path
);
482 if (__builtin_expect (mode
, normal
) == verify
)
484 char *err_str
= NULL
;
485 struct map_args args
;
487 args
.str
= _dl_argv
[0];
488 (void) _dl_catch_error (&err_str
, map_doit
, &args
);
492 _exit (EXIT_FAILURE
);
497 HP_TIMING_NOW (start
);
498 _dl_map_object (NULL
, _dl_argv
[0], 0, lt_library
, 0);
499 HP_TIMING_NOW (stop
);
501 HP_TIMING_DIFF (load_time
, start
, stop
);
504 phdr
= _dl_loaded
->l_phdr
;
505 phent
= _dl_loaded
->l_phnum
;
506 /* We overwrite here a pointer to a malloc()ed string. But since
507 the malloc() implementation used at this point is the dummy
508 implementations which has no real free() function it does not
509 makes sense to free the old string first. */
510 _dl_loaded
->l_name
= (char *) "";
511 *user_entry
= _dl_loaded
->l_entry
;
515 /* Create a link_map for the executable itself.
516 This will be what dlopen on "" returns. */
517 _dl_new_object ((char *) "", "", lt_executable
, NULL
);
518 if (_dl_loaded
== NULL
)
519 _dl_sysdep_fatal ("cannot allocate memory for link map\n", NULL
);
520 _dl_loaded
->l_phdr
= phdr
;
521 _dl_loaded
->l_phnum
= phent
;
522 _dl_loaded
->l_entry
= *user_entry
;
523 _dl_loaded
->l_opencount
= 1;
525 /* At this point we are in a bit of trouble. We would have to
526 fill in the values for l_dev and l_ino. But in general we
527 do not know where the file is. We also do not handle AT_EXECFD
528 even if it would be passed up.
530 We leave the values here defined to 0. This is normally no
531 problem as the program code itself is normally no shared
532 object and therefore cannot be loaded dynamically. Nothing
533 prevent the use of dynamic binaries and in these situations
534 we might get problems. We might not be able to find out
535 whether the object is already loaded. But since there is no
536 easy way out and because the dynamic binary must also not
537 have an SONAME we ignore this program for now. If it becomes
538 a problem we can force people using SONAMEs. */
540 /* We delay initializing the path structure until we got the dynamic
541 information for the program. */
544 /* It is not safe to load stuff after the main program. */
545 _dl_loaded
->l_map_end
= ~0;
546 /* Perhaps the executable has no PT_LOAD header entries at all. */
547 _dl_loaded
->l_map_start
= ~0;
549 /* Scan the program header table for the dynamic section. */
550 for (ph
= phdr
; ph
< &phdr
[phent
]; ++ph
)
554 /* Find out the load address. */
555 _dl_loaded
->l_addr
= (ElfW(Addr
)) phdr
- ph
->p_vaddr
;
558 /* This tells us where to find the dynamic section,
559 which tells us everything we need to do. */
560 _dl_loaded
->l_ld
= (void *) _dl_loaded
->l_addr
+ ph
->p_vaddr
;
563 /* This "interpreter segment" was used by the program loader to
564 find the program interpreter, which is this program itself, the
565 dynamic linker. We note what name finds us, so that a future
566 dlopen call or DT_NEEDED entry, for something that wants to link
567 against the dynamic linker as a shared library, will know that
568 the shared object is already loaded. */
569 _dl_rtld_libname
.name
= ((const char *) _dl_loaded
->l_addr
571 _dl_rtld_libname
.next
= NULL
;
572 _dl_rtld_map
.l_libname
= &_dl_rtld_libname
;
574 /* Ordinarilly, we would get additional names for the loader from
575 our DT_SONAME. This can't happen if we were actually linked as
576 a static executable (detect this case when we have no DYNAMIC).
577 If so, assume the filename component of the interpreter path to
578 be our SONAME, and add it to our name list. */
579 if (_dl_rtld_map
.l_ld
== NULL
)
581 char *p
= strrchr (_dl_rtld_libname
.name
, '/');
584 _dl_rtld_libname2
.name
= p
+1;
585 _dl_rtld_libname2
.next
= NULL
;
586 _dl_rtld_libname
.next
= &_dl_rtld_libname2
;
593 /* Remember where the main program starts in memory. */
596 mapstart
= _dl_loaded
->l_addr
+ (ph
->p_vaddr
& ~(ph
->p_align
- 1));
597 if (_dl_loaded
->l_map_start
> mapstart
)
598 _dl_loaded
->l_map_start
= mapstart
;
602 if (! _dl_rtld_map
.l_libname
&& _dl_rtld_map
.l_name
)
604 /* We were invoked directly, so the program might not have a
606 _dl_rtld_libname
.name
= _dl_rtld_map
.l_name
;
607 _dl_rtld_libname
.next
= NULL
;
608 _dl_rtld_map
.l_libname
= &_dl_rtld_libname
;
611 assert (_dl_rtld_map
.l_libname
); /* How else did we get here? */
615 /* Extract the contents of the dynamic section for easy access. */
616 elf_get_dynamic_info (_dl_loaded
);
617 if (_dl_loaded
->l_info
[DT_HASH
])
618 /* Set up our cache of pointers into the hash table. */
619 _dl_setup_hash (_dl_loaded
);
622 if (__builtin_expect (mode
, normal
) == verify
)
624 /* We were called just to verify that this is a dynamic
625 executable using us as the program interpreter. Exit with an
626 error if we were not able to load the binary or no interpreter
627 is specified (i.e., this is no dynamically linked binary. */
628 if (_dl_loaded
->l_ld
== NULL
)
631 /* We allow here some platform specific code. */
632 #ifdef DISTINGUISH_LIB_VERSIONS
633 DISTINGUISH_LIB_VERSIONS
;
635 _exit (has_interp
? 0 : 2);
639 /* Initialize the data structures for the search paths for shared
641 _dl_init_paths (library_path
);
643 /* Put the link_map for ourselves on the chain so it can be found by
644 name. Note that at this point the global chain of link maps contains
645 exactly one element, which is pointed to by _dl_loaded. */
646 if (! _dl_rtld_map
.l_name
)
647 /* If not invoked directly, the dynamic linker shared object file was
648 found by the PT_INTERP name. */
649 _dl_rtld_map
.l_name
= (char *) _dl_rtld_map
.l_libname
->name
;
650 _dl_rtld_map
.l_type
= lt_library
;
651 _dl_loaded
->l_next
= &_dl_rtld_map
;
652 _dl_rtld_map
.l_prev
= _dl_loaded
;
654 /* We have two ways to specify objects to preload: via environment
655 variable and via the file /etc/ld.so.preload. The latter can also
656 be used when security is enabled. */
662 /* The LD_PRELOAD environment variable gives list of libraries
663 separated by white space or colons that are loaded before the
664 executable's dependencies and prepended to the global scope
665 list. If the binary is running setuid all elements
666 containing a '/' are ignored since it is insecure. */
667 char *list
= strdupa (preloadlist
);
670 HP_TIMING_NOW (start
);
672 while ((p
= strsep (&list
, " :")) != NULL
)
674 && (! __libc_enable_secure
|| strchr (p
, '/') == NULL
))
676 struct link_map
*new_map
= _dl_map_object (_dl_loaded
, p
, 1,
678 if (new_map
->l_opencount
== 1)
679 /* It is no duplicate. */
683 HP_TIMING_NOW (stop
);
684 HP_TIMING_DIFF (diff
, start
, stop
);
685 HP_TIMING_ACCUM_NT (load_time
, diff
);
688 /* Read the contents of the file. */
689 file
= _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size
,
690 PROT_READ
| PROT_WRITE
);
693 /* Parse the file. It contains names of libraries to be loaded,
694 separated by white spaces or `:'. It may also contain
695 comments introduced by `#'. */
700 /* Eliminate comments. */
705 char *comment
= memchr (runp
, '#', rest
);
709 rest
-= comment
- runp
;
712 while (--rest
> 0 && *++comment
!= '\n');
715 /* We have one problematic case: if we have a name at the end of
716 the file without a trailing terminating characters, we cannot
717 place the \0. Handle the case separately. */
718 if (file
[file_size
- 1] != ' ' && file
[file_size
- 1] != '\t'
719 && file
[file_size
- 1] != '\n' && file
[file_size
- 1] != ':')
721 problem
= &file
[file_size
];
722 while (problem
> file
&& problem
[-1] != ' ' && problem
[-1] != '\t'
723 && problem
[-1] != '\n' && problem
[-1] != ':')
732 file
[file_size
- 1] = '\0';
735 HP_TIMING_NOW (start
);
741 while ((p
= strsep (&runp
, ": \t\n")) != NULL
)
744 struct link_map
*new_map
= _dl_map_object (_dl_loaded
, p
, 1,
746 if (new_map
->l_opencount
== 1)
747 /* It is no duplicate. */
754 char *p
= strndupa (problem
, file_size
- (problem
- file
));
755 struct link_map
*new_map
= _dl_map_object (_dl_loaded
, p
, 1,
757 if (new_map
->l_opencount
== 1)
758 /* It is no duplicate. */
762 HP_TIMING_NOW (stop
);
763 HP_TIMING_DIFF (diff
, start
, stop
);
764 HP_TIMING_ACCUM_NT (load_time
, diff
);
766 /* We don't need the file anymore. */
767 __munmap (file
, file_size
);
772 /* Set up PRELOADS with a vector of the preloaded libraries. */
774 preloads
= __alloca (npreloads
* sizeof preloads
[0]);
775 l
= _dl_rtld_map
.l_next
; /* End of the chain before preloads. */
782 assert (i
== npreloads
);
785 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
786 specified some libraries to load, these are inserted before the actual
787 dependencies in the executable's searchlist for symbol resolution. */
788 HP_TIMING_NOW (start
);
789 _dl_map_object_deps (_dl_loaded
, preloads
, npreloads
, mode
== trace
);
790 HP_TIMING_NOW (stop
);
791 HP_TIMING_DIFF (diff
, start
, stop
);
792 HP_TIMING_ACCUM_NT (load_time
, diff
);
794 /* Mark all objects as being in the global scope. */
795 for (i
= _dl_loaded
->l_searchlist
.r_nlist
; i
> 0; )
796 _dl_loaded
->l_searchlist
.r_list
[--i
]->l_global
= 1;
799 /* We are done mapping things, so close the zero-fill descriptor. */
800 __close (_dl_zerofd
);
804 /* Remove _dl_rtld_map from the chain. */
805 _dl_rtld_map
.l_prev
->l_next
= _dl_rtld_map
.l_next
;
806 if (_dl_rtld_map
.l_next
)
807 _dl_rtld_map
.l_next
->l_prev
= _dl_rtld_map
.l_prev
;
809 if (__builtin_expect (_dl_rtld_map
.l_opencount
, 2) > 1)
811 /* Some DT_NEEDED entry referred to the interpreter object itself, so
812 put it back in the list of visible objects. We insert it into the
813 chain in symbol search order because gdb uses the chain's order as
814 its symbol search order. */
816 while (_dl_loaded
->l_searchlist
.r_list
[i
] != &_dl_rtld_map
)
818 _dl_rtld_map
.l_prev
= _dl_loaded
->l_searchlist
.r_list
[i
- 1];
819 if (__builtin_expect (mode
, normal
) == normal
)
820 _dl_rtld_map
.l_next
= (i
+ 1 < _dl_loaded
->l_searchlist
.r_nlist
821 ? _dl_loaded
->l_searchlist
.r_list
[i
+ 1]
824 /* In trace mode there might be an invisible object (which we
825 could not find) after the previous one in the search list.
826 In this case it doesn't matter much where we put the
827 interpreter object, so we just initialize the list pointer so
828 that the assertion below holds. */
829 _dl_rtld_map
.l_next
= _dl_rtld_map
.l_prev
->l_next
;
831 assert (_dl_rtld_map
.l_prev
->l_next
== _dl_rtld_map
.l_next
);
832 _dl_rtld_map
.l_prev
->l_next
= &_dl_rtld_map
;
833 if (_dl_rtld_map
.l_next
)
835 assert (_dl_rtld_map
.l_next
->l_prev
== _dl_rtld_map
.l_prev
);
836 _dl_rtld_map
.l_next
->l_prev
= &_dl_rtld_map
;
840 /* Now let us see whether all libraries are available in the
843 struct version_check_args args
;
844 args
.doexit
= mode
== normal
;
845 _dl_receive_error (print_missing_version
, version_check_doit
, &args
);
848 if (__builtin_expect (mode
, normal
) != normal
)
850 /* We were run just to list the shared libraries. It is
851 important that we do this before real relocation, because the
852 functions we call below for output may no longer work properly
854 if (! _dl_loaded
->l_info
[DT_NEEDED
])
855 _dl_sysdep_message ("\t", "statically linked\n", NULL
);
860 for (l
= _dl_loaded
->l_next
; l
; l
= l
->l_next
)
861 if (l
->l_opencount
== 0)
862 /* The library was not found. */
863 _dl_sysdep_message ("\t", l
->l_libname
->name
, " => not found\n",
868 buf
[sizeof buf
- 1] = '\0';
869 bp
= _itoa_word (l
->l_addr
, &buf
[sizeof buf
- 1], 16, 0);
870 while ((size_t) (&buf
[sizeof buf
- 1] - bp
)
871 < sizeof l
->l_addr
* 2)
873 _dl_sysdep_message ("\t", l
->l_libname
->name
, " => ",
874 l
->l_name
, " (0x", bp
, ")\n", NULL
);
878 if (__builtin_expect (mode
, trace
) != trace
)
879 for (i
= 1; i
< _dl_argc
; ++i
)
881 const ElfW(Sym
) *ref
= NULL
;
882 ElfW(Addr
) loadbase
= _dl_lookup_symbol (_dl_argv
[i
], _dl_loaded
,
883 &ref
, _dl_loaded
->l_scope
,
884 ELF_MACHINE_JMP_SLOT
);
886 buf
[sizeof buf
- 1] = '\0';
887 bp
= _itoa_word (ref
->st_value
, &buf
[sizeof buf
- 1], 16, 0);
888 while ((size_t) (&buf
[sizeof buf
- 1] - bp
) < sizeof loadbase
* 2)
890 _dl_sysdep_message (_dl_argv
[i
], " found at 0x", bp
, NULL
);
891 buf
[sizeof buf
- 1] = '\0';
892 bp
= _itoa_word (loadbase
, &buf
[sizeof buf
- 1], 16, 0);
893 while ((size_t) (&buf
[sizeof buf
- 1] - bp
) < sizeof loadbase
* 2)
895 _dl_sysdep_message (" in object at 0x", bp
, "\n", NULL
);
901 /* We have to do symbol dependency testing. */
902 struct relocate_args args
;
905 args
.lazy
= _dl_lazy
;
912 if (l
!= &_dl_rtld_map
&& l
->l_opencount
> 0)
915 _dl_receive_error (print_unresolved
, relocate_doit
,
922 #define VERNEEDTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
925 /* Print more information. This means here, print information
926 about the versions needed. */
928 struct link_map
*map
= _dl_loaded
;
930 for (map
= _dl_loaded
; map
!= NULL
; map
= map
->l_next
)
933 ElfW(Dyn
) *dyn
= map
->l_info
[VERNEEDTAG
];
939 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
940 ent
= (ElfW(Verneed
) *) (map
->l_addr
+ dyn
->d_un
.d_ptr
);
944 _dl_sysdep_message ("\n\tVersion information:\n", NULL
);
948 _dl_sysdep_message ("\t", (map
->l_name
[0]
949 ? map
->l_name
: _dl_argv
[0]),
955 struct link_map
*needed
;
957 needed
= find_needed (strtab
+ ent
->vn_file
);
958 aux
= (ElfW(Vernaux
) *) ((char *) ent
+ ent
->vn_aux
);
962 const char *fname
= NULL
;
964 _dl_sysdep_message ("\t\t",
965 strtab
+ ent
->vn_file
,
966 " (", strtab
+ aux
->vna_name
,
974 && match_version (strtab
+aux
->vna_name
, needed
))
975 fname
= needed
->l_name
;
977 _dl_sysdep_message (fname
?: "not found", "\n",
980 if (aux
->vna_next
== 0)
981 /* No more symbols. */
985 aux
= (ElfW(Vernaux
) *) ((char *) aux
989 if (ent
->vn_next
== 0)
990 /* No more dependencies. */
993 /* Next dependency. */
994 ent
= (ElfW(Verneed
) *) ((char *) ent
+ ent
->vn_next
);
1004 /* Now we have all the objects loaded. Relocate them all except for
1005 the dynamic linker itself. We do this in reverse order so that copy
1006 relocs of earlier objects overwrite the data written by later
1007 objects. We do not re-relocate the dynamic linker itself in this
1008 loop because that could result in the GOT entries for functions we
1009 call being changed, and that would break us. It is safe to relocate
1010 the dynamic linker out of order because it has no copy relocs (we
1011 know that because it is self-contained). */
1014 int consider_profiling
= _dl_profile
!= NULL
;
1019 /* If we are profiling we also must do lazy reloaction. */
1020 _dl_lazy
|= consider_profiling
;
1026 HP_TIMING_NOW (start
);
1029 if (l
!= &_dl_rtld_map
)
1030 _dl_relocate_object (l
, l
->l_scope
, _dl_lazy
, consider_profiling
);
1035 HP_TIMING_NOW (stop
);
1037 HP_TIMING_DIFF (relocate_time
, start
, stop
);
1039 /* Do any necessary cleanups for the startup OS interface code.
1040 We do these now so that no calls are made after rtld re-relocation
1041 which might be resolved to different functions than we expect.
1042 We cannot do this before relocating the other objects because
1043 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
1044 _dl_sysdep_start_cleanup ();
1046 /* Now enable profiling if needed. Like the previous call,
1047 this has to go here because the calls it makes should use the
1048 rtld versions of the functions (particularly calloc()), but it
1049 needs to have _dl_profile_map set up by the relocator. */
1050 if (_dl_profile_map
!= NULL
)
1051 /* We must prepare the profiling. */
1052 _dl_start_profile (_dl_profile_map
, _dl_profile_output
);
1054 if (_dl_rtld_map
.l_opencount
> 1)
1056 /* There was an explicit ref to the dynamic linker as a shared lib.
1057 Re-relocate ourselves with user-controlled symbol definitions. */
1058 HP_TIMING_NOW (start
);
1059 _dl_relocate_object (&_dl_rtld_map
, _dl_loaded
->l_scope
, 0, 0);
1060 HP_TIMING_NOW (stop
);
1061 HP_TIMING_DIFF (add
, start
, stop
);
1062 HP_TIMING_ACCUM_NT (relocate_time
, add
);
1066 /* Now set up the variable which helps the assembler startup code. */
1067 _dl_main_searchlist
= &_dl_loaded
->l_searchlist
;
1068 _dl_global_scope
[0] = &_dl_loaded
->l_searchlist
;
1070 /* Safe the information about the original global scope list since
1071 we need it in the memory handling later. */
1072 _dl_initial_searchlist
= *_dl_main_searchlist
;
1075 /* Initialize _r_debug. */
1076 struct r_debug
*r
= _dl_debug_initialize (_dl_rtld_map
.l_addr
);
1081 #ifdef ELF_MACHINE_DEBUG_SETUP
1083 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
1085 ELF_MACHINE_DEBUG_SETUP (l
, r
);
1086 ELF_MACHINE_DEBUG_SETUP (&_dl_rtld_map
, r
);
1090 if (l
->l_info
[DT_DEBUG
])
1091 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
1092 with the run-time address of the r_debug structure */
1093 l
->l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
1095 /* Fill in the pointer in the dynamic linker's own dynamic section, in
1096 case you run gdb on the dynamic linker directly. */
1097 if (_dl_rtld_map
.l_info
[DT_DEBUG
])
1098 _dl_rtld_map
.l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
1102 /* Notify the debugger that all objects are now mapped in. */
1103 r
->r_state
= RT_ADD
;
1108 /* We must munmap() the cache file. */
1109 _dl_unload_cache ();
1112 /* Once we return, _dl_sysdep_start will invoke
1113 the DT_INIT functions and then *USER_ENTRY. */
1116 /* This is a little helper function for resolving symbols while
1117 tracing the binary. */
1119 print_unresolved (int errcode
__attribute__ ((unused
)), const char *objname
,
1120 const char *errstring
)
1122 if (objname
[0] == '\0')
1123 objname
= _dl_argv
[0] ?: "<main program>";
1124 _dl_sysdep_error (errstring
, " (", objname
, ")\n", NULL
);
1127 /* This is a little helper function for resolving symbols while
1128 tracing the binary. */
1130 print_missing_version (int errcode
__attribute__ ((unused
)),
1131 const char *objname
, const char *errstring
)
1133 _dl_sysdep_error (_dl_argv
[0] ?: "<program name unknown>", ": ",
1134 objname
, ": ", errstring
, "\n", NULL
);
1137 /* Nonzero if any of the debugging options is enabled. */
1138 static int any_debug
;
1140 /* Process the string given as the parameter which explains which debugging
1141 options are enabled. */
1143 process_dl_debug (const char *dl_debug
)
1146 #define separators " ,:"
1150 /* Skip separating white spaces and commas. */
1151 dl_debug
+= strspn (dl_debug
, separators
);
1152 if (*dl_debug
!= '\0')
1154 len
= strcspn (dl_debug
, separators
);
1159 /* This option is not documented since it is not generally
1161 if (memcmp (dl_debug
, "all", 3) == 0)
1164 _dl_debug_impcalls
= 1;
1165 _dl_debug_reloc
= 1;
1166 _dl_debug_files
= 1;
1167 _dl_debug_symbols
= 1;
1168 _dl_debug_bindings
= 1;
1169 _dl_debug_versions
= 1;
1176 if (memcmp (dl_debug
, "help", 4) == 0)
1178 _dl_sysdep_message ("\
1179 Valid options for the LD_DEBUG environment variable are:\n\
1181 bindings display information about symbol binding\n\
1182 files display processing of files and libraries\n\
1183 help display this help message and exit\n\
1184 libs display library search paths\n\
1185 reloc display relocation processing\n\
1186 symbols display symbol table processing\n\
1187 versions display version dependencies\n\
1189 To direct the debugging output into a file instead of standard output\n\
1190 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n",
1195 if (memcmp (dl_debug
, "libs", 4) == 0)
1198 _dl_debug_impcalls
= 1;
1205 if (memcmp (dl_debug
, "reloc", 5) == 0)
1207 _dl_debug_reloc
= 1;
1208 _dl_debug_impcalls
= 1;
1213 if (memcmp (dl_debug
, "files", 5) == 0)
1215 _dl_debug_files
= 1;
1216 _dl_debug_impcalls
= 1;
1223 if (memcmp (dl_debug
, "symbols", 7) == 0)
1225 _dl_debug_symbols
= 1;
1226 _dl_debug_impcalls
= 1;
1233 if (memcmp (dl_debug
, "bindings", 8) == 0)
1235 _dl_debug_bindings
= 1;
1236 _dl_debug_impcalls
= 1;
1241 if (memcmp (dl_debug
, "versions", 8) == 0)
1243 _dl_debug_versions
= 1;
1244 _dl_debug_impcalls
= 1;
1251 if (memcmp (dl_debug
, "statistics", 10) == 0)
1253 _dl_debug_statistics
= 1;
1263 /* Display a warning and skip everything until next separator. */
1264 char *startp
= strndupa (dl_debug
, len
);
1265 _dl_sysdep_error ("warning: debug option `", startp
,
1266 "' unknown; try LD_DEBUG=help\n", NULL
);
1271 while (*(dl_debug
+= len
) != '\0');
1274 /* Process all environments variables the dynamic linker must recognize.
1275 Since all of them start with `LD_' we are a bit smarter while finding
1278 process_envvars (enum mode
*modep
, int *lazyp
)
1282 enum mode mode
= normal
;
1284 char *debug_output
= NULL
;
1286 /* This is the default place for profiling data file. */
1287 _dl_profile_output
= "/var/tmp";
1289 while ((envline
= _dl_next_ld_env_entry (&runp
)) != NULL
)
1291 size_t len
= strcspn (envline
, "=") - 3;
1296 /* Warning level, verbose or not. */
1297 if (memcmp (&envline
[3], "WARN", 4) == 0)
1298 _dl_verbose
= envline
[8] != '\0';
1302 /* Debugging of the dynamic linker? */
1303 if (memcmp (&envline
[3], "DEBUG", 5) == 0)
1304 process_dl_debug (&envline
[9]);
1308 /* Print information about versions. */
1309 if (memcmp (&envline
[3], "VERBOSE", 7) == 0)
1311 version_info
= envline
[11] != '\0';
1315 /* List of objects to be preloaded. */
1316 if (memcmp (&envline
[3], "PRELOAD", 7) == 0)
1318 preloadlist
= &envline
[11];
1322 /* Which shared object shall be profiled. */
1323 if (memcmp (&envline
[3], "PROFILE", 7) == 0)
1324 _dl_profile
= &envline
[11];
1328 /* Do we bind early? */
1329 if (memcmp (&envline
[3], "BIND_NOW", 8) == 0)
1330 bind_now
= envline
[12] != '\0';
1334 /* Test whether we want to see the content of the auxiliary
1335 array passed up from the kernel. */
1336 if (memcmp (&envline
[3], "SHOW_AUXV", 9) == 0)
1341 /* Mask for the important hardware capabilities. */
1342 if (memcmp (&envline
[3], "HWCAP_MASK", 10) == 0)
1343 _dl_hwcap_mask
= strtoul (&envline
[14], NULL
, 0);
1347 /* Path where the binary is found. */
1348 if (!__libc_enable_secure
1349 && memcmp (&envline
[3], "ORIGIN_PATH", 11) == 0)
1350 _dl_origin_path
= &envline
[15];
1354 /* Where to place the profiling data file. */
1355 if (memcmp (&envline
[3], "DEBUG_OUTPUT", 12) == 0)
1357 debug_output
= &envline
[16];
1361 /* The library search path. */
1362 if (memcmp (&envline
[3], "LIBRARY_PATH", 12) == 0)
1363 library_path
= &envline
[16];
1367 /* Where to place the profiling data file. */
1368 if (!__libc_enable_secure
1369 && memcmp (&envline
[3], "PROFILE_OUTPUT", 14) == 0)
1371 _dl_profile_output
= &envline
[18];
1372 if (*_dl_profile_output
== '\0')
1373 _dl_profile_output
= "/var/tmp";
1378 /* The mode of the dynamic linker can be set. */
1379 if (memcmp (&envline
[3], "TRACE_LOADED_OBJECTS", 20) == 0)
1383 /* We might have some extra environment variable to handle. This
1384 is tricky due to the pre-processing of the length of the name
1385 in the switch statement here. The code here assumes that added
1386 environment variables have a different length. */
1387 #ifdef EXTRA_LD_ENVVARS
1393 /* Extra security for SUID binaries. Remove all dangerous environment
1395 if (__libc_enable_secure
)
1397 static const char *unsecure_envvars
[] =
1399 #ifdef EXTRA_UNSECURE_ENVVARS
1400 EXTRA_UNSECURE_ENVVARS
1405 if (preloadlist
!= NULL
)
1406 unsetenv ("LD_PRELOAD");
1407 if (library_path
!= NULL
)
1408 unsetenv ("LD_LIBRARY_PATH");
1409 if (_dl_origin_path
!= NULL
)
1410 unsetenv ("LD_ORIGIN_PATH");
1411 if (debug_output
!= NULL
)
1412 unsetenv ("LD_DEBUG_OUTPUT");
1413 if (_dl_profile
!= NULL
)
1414 unsetenv ("LD_PROFILE");
1417 cnt
< sizeof (unsecure_envvars
) / sizeof (unsecure_envvars
[0]);
1419 unsetenv (unsecure_envvars
[cnt
]);
1422 /* The name of the object to profile cannot be empty. */
1423 if (_dl_profile
!= NULL
&& *_dl_profile
== '\0')
1426 /* If we have to run the dynamic linker in debugging mode and the
1427 LD_DEBUG_OUTPUT environment variable is given, we write the debug
1428 messages to this file. */
1429 if (any_debug
&& debug_output
!= NULL
&& !__libc_enable_secure
)
1431 size_t name_len
= strlen (debug_output
);
1432 char buf
[name_len
+ 12];
1435 buf
[name_len
+ 11] = '\0';
1436 startp
= _itoa_word (__getpid (), &buf
[name_len
+ 11], 10, 0);
1438 startp
= memcpy (startp
- name_len
, debug_output
, name_len
);
1440 _dl_debug_fd
= __open (startp
, O_WRONLY
| O_APPEND
| O_CREAT
, 0666);
1441 if (_dl_debug_fd
== -1)
1442 /* We use standard output if opening the file failed. */
1443 _dl_debug_fd
= STDOUT_FILENO
;
1446 /* LAZY is determined by the environment variable LD_WARN and
1447 LD_BIND_NOW if we trace the binary. */
1448 if (__builtin_expect (mode
, normal
) == trace
)
1449 *lazyp
= _dl_verbose
? !bind_now
: -1;
1457 /* Print the various times we collected. */
1459 print_statistics (void)
1465 /* Total time rtld used. */
1466 if (HP_TIMING_AVAIL
)
1468 HP_TIMING_PRINT (buf
, sizeof (buf
), rtld_total_time
);
1469 _dl_debug_message (1, "\nruntime linker statistics:\n"
1470 " total startup time in dynamic loader: ",
1474 /* Print relocation statistics. */
1475 if (HP_TIMING_AVAIL
)
1477 HP_TIMING_PRINT (buf
, sizeof (buf
), relocate_time
);
1478 _dl_debug_message (1, " time needed for relocation: ", buf
,
1480 cp
= _itoa_word ((1000 * relocate_time
) / rtld_total_time
,
1481 buf
+ sizeof (buf
), 10, 0);
1483 switch (buf
+ sizeof (buf
) - cp
)
1494 _dl_debug_message (0, " (", buf
, "%)\n", NULL
);
1497 buf
[sizeof (buf
) - 1] = '\0';
1498 _dl_debug_message (1, " number of relocations: ",
1499 _itoa_word (_dl_num_relocations
,
1500 buf
+ sizeof (buf
) - 1, 10, 0),
1503 /* Time spend while loading the object and the dependencies. */
1504 if (HP_TIMING_AVAIL
)
1506 HP_TIMING_PRINT (buf
, sizeof (buf
), load_time
);
1507 _dl_debug_message (1, " time needed to load objects: ", buf
,
1509 cp
= _itoa_word ((1000 * load_time
) / rtld_total_time
,
1510 buf
+ sizeof (buf
), 10, 0);
1512 switch (buf
+ sizeof (buf
) - cp
)
1523 _dl_debug_message (0, " (", buf
, "%)\n", NULL
);