1 /* Run time dynamic linker.
2 Copyright (C) 1995, 1996, 1997, 1998, 1999 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. */
25 #include <elf/ldsodefs.h>
26 #include <stdio-common/_itoa.h>
28 #include <fpu_control.h>
29 #include <hp-timing.h>
30 #include "dynamic-link.h"
31 #include "dl-librecon.h"
35 /* System-specific function to do initial startup for the dynamic linker.
36 After this, file access calls and getenv must work. This is responsible
37 for setting __libc_enable_secure if we need to be secure (e.g. setuid),
38 and for setting _dl_argc and _dl_argv, and then calling _dl_main. */
39 extern ElfW(Addr
) _dl_sysdep_start (void **start_argptr
,
40 void (*dl_main
) (const ElfW(Phdr
) *phdr
,
42 ElfW(Addr
) *user_entry
));
43 extern void _dl_sysdep_start_cleanup (void);
45 /* This function is used to unload the cache file if necessary. */
46 extern void _dl_unload_cache (void);
48 /* System-dependent function to read a file's whole contents
49 in the most convenient manner available. */
50 extern void *_dl_sysdep_read_whole_file (const char *filename
,
54 /* Helper function to handle errors while resolving symbols. */
55 static void print_unresolved (int errcode
, const char *objname
,
56 const char *errsting
);
58 /* Helper function to handle errors when a version is missing. */
59 static void print_missing_version (int errcode
, const char *objname
,
60 const char *errsting
);
62 /* Print the various times we collected. */
63 static void print_statistics (void);
65 /* This is a list of all the modes the dynamic loader can be in. */
66 enum mode
{ normal
, list
, verify
, trace
};
68 /* Process all environments variables the dynamic linker must recognize.
69 Since all of them start with `LD_' we are a bit smarter while finding
71 static void process_envvars (enum mode
*modep
, int *lazyp
);
75 unsigned int _dl_skip_args
; /* Nonzero if we were run directly. */
77 const char *_dl_platform
;
78 size_t _dl_platformlen
;
79 unsigned long _dl_hwcap
;
80 fpu_control_t _dl_fpu_control
= _FPU_DEFAULT
;
81 struct r_search_path
*_dl_search_paths
;
82 const char *_dl_profile
;
83 const char *_dl_profile_output
;
84 struct link_map
*_dl_profile_map
;
87 int _dl_debug_impcalls
;
88 int _dl_debug_bindings
;
89 int _dl_debug_symbols
;
90 int _dl_debug_versions
;
93 int _dl_debug_statistics
;
94 const char *_dl_inhibit_rpath
; /* RPATH values which should be
96 const char *_dl_origin_path
;
98 /* This is a pointer to the map for the main object and through it to
99 all loaded objects. */
100 struct link_map
*_dl_loaded
;
101 /* Pointer to the l_searchlist element of the link map of the main object. */
102 struct r_scope_elem
*_dl_main_searchlist
;
103 /* Copy of the content of `_dl_main_searchlist'. */
104 struct r_scope_elem _dl_initial_searchlist
;
105 /* Array which is used when looking up in the global scope. */
106 struct r_scope_elem
*_dl_global_scope
[2];
108 /* Set nonzero during loading and initialization of executable and
109 libraries, cleared before the executable's entry point runs. This
110 must not be initialized to nonzero, because the unused dynamic
111 linker loaded in for libc.so's "ld.so.1" dep will provide the
112 definition seen by libc.so's initializer; that value must be zero,
113 and will be since that dynamic linker's _dl_start and dl_main will
118 static void dl_main (const ElfW(Phdr
) *phdr
,
120 ElfW(Addr
) *user_entry
);
122 struct link_map _dl_rtld_map
;
123 struct libname_list _dl_rtld_libname
;
124 struct libname_list _dl_rtld_libname2
;
126 /* Variable for statistics. */
127 static hp_timing_t rtld_total_time
;
128 static hp_timing_t relocate_time
;
129 static hp_timing_t load_time
;
130 extern unsigned long int _dl_num_relocations
; /* in dl-lookup.c */
132 static ElfW(Addr
) _dl_start_final (void *arg
, struct link_map
*bootstrap_map_p
,
133 hp_timing_t start_time
);
138 #error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
142 _dl_start (void *arg
)
144 struct link_map bootstrap_map
;
145 hp_timing_t start_time
;
148 /* This #define produces dynamic linking inline functions for
149 bootstrap relocation instead of general-purpose relocation. */
150 #define RTLD_BOOTSTRAP
151 #define RESOLVE(sym, version, flags) \
152 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : bootstrap_map.l_addr)
153 #include "dynamic-link.h"
155 if (HP_TIMING_INLINE
&& HP_TIMING_AVAIL
)
156 HP_TIMING_NOW (start_time
);
158 /* Partly clean the `bootstrap_map' structure up. Don't use `memset'
159 since it might nor be built in or inlined and we cannot make function
160 calls at this point. */
162 cnt
< sizeof (bootstrap_map
.l_info
) / sizeof (bootstrap_map
.l_info
[0]);
164 bootstrap_map
.l_info
[cnt
] = 0;
166 /* Figure out the run-time load address of the dynamic linker itself. */
167 bootstrap_map
.l_addr
= elf_machine_load_address ();
169 /* Read our own dynamic section and fill in the info array. */
170 bootstrap_map
.l_ld
= (void *) bootstrap_map
.l_addr
+ elf_machine_dynamic ();
171 elf_get_dynamic_info (&bootstrap_map
);
173 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
174 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map
.l_info
);
177 /* Relocate ourselves so we can do normal function calls and
178 data access using the global offset table. */
180 ELF_DYNAMIC_RELOCATE (&bootstrap_map
, 0, 0);
181 /* Please note that we don't allow profiling of this object and
182 therefore need not test whether we have to allocate the array
183 for the relocation results (as done in dl-reloc.c). */
185 /* Now life is sane; we can call functions and access global data.
186 Set up to use the operating system facilities, and find out from
187 the operating system's program loader where to find the program
188 header table in core. Put the rest of _dl_start into a separate
189 function, that way the compiler cannot put accesses to the GOT
190 before ELF_DYNAMIC_RELOCATE. */
191 return _dl_start_final (arg
, &bootstrap_map
, start_time
);
196 _dl_start_final (void *arg
, struct link_map
*bootstrap_map_p
,
197 hp_timing_t start_time
)
199 /* The use of `alloca' here looks ridiculous but it helps. The goal
200 is to avoid the function from being inlined. There is no official
201 way to do this so we use this trick. gcc never inlines functions
202 which use `alloca'. */
203 ElfW(Addr
) *start_addr
= alloca (sizeof (ElfW(Addr
)));
207 /* If it hasn't happen yet record the startup time. */
208 if (! HP_TIMING_INLINE
)
209 HP_TIMING_NOW (start_time
);
211 /* Initialize the timing functions. */
212 HP_TIMING_DIFF_INIT ();
215 /* Transfer data about ourselves to the permanent link_map structure. */
216 _dl_rtld_map
.l_addr
= bootstrap_map_p
->l_addr
;
217 _dl_rtld_map
.l_ld
= bootstrap_map_p
->l_ld
;
218 _dl_rtld_map
.l_opencount
= 1;
219 memcpy (_dl_rtld_map
.l_info
, bootstrap_map_p
->l_info
,
220 sizeof _dl_rtld_map
.l_info
);
221 _dl_setup_hash (&_dl_rtld_map
);
223 /* Don't bother trying to work out how ld.so is mapped in memory. */
224 _dl_rtld_map
.l_map_start
= ~0;
225 _dl_rtld_map
.l_map_end
= ~0;
227 /* Call the OS-dependent function to set up life so we can do things like
228 file access. It will call `dl_main' (below) to do all the real work
229 of the dynamic linker, and then unwind our frame and run the user
230 entry point on the same stack we entered on. */
231 *start_addr
= _dl_sysdep_start (arg
, &dl_main
);
235 hp_timing_t end_time
;
237 /* Get the current time. */
238 HP_TIMING_NOW (end_time
);
240 /* Compute the difference. */
241 HP_TIMING_DIFF (rtld_total_time
, start_time
, end_time
);
244 if (_dl_debug_statistics
)
250 /* Now life is peachy; we can do all normal operations.
251 On to the real work. */
253 void ENTRY_POINT (void);
255 /* Some helper functions. */
257 /* Arguments to relocate_doit. */
266 /* Argument to map_doit. */
268 /* Return value of map_doit. */
269 struct link_map
*main_map
;
272 /* Arguments to version_check_doit. */
273 struct version_check_args
279 relocate_doit (void *a
)
281 struct relocate_args
*args
= (struct relocate_args
*) a
;
283 _dl_relocate_object (args
->l
, args
->l
->l_scope
,
290 struct map_args
*args
= (struct map_args
*) a
;
291 args
->main_map
= _dl_map_object (NULL
, args
->str
, 0, lt_library
, 0);
295 version_check_doit (void *a
)
297 struct version_check_args
*args
= (struct version_check_args
*) a
;
298 if (_dl_check_all_versions (_dl_loaded
, 1) && args
->doexit
)
299 /* We cannot start the application. Abort now. */
304 static inline struct link_map
*
305 find_needed (const char *name
)
307 unsigned int n
= _dl_loaded
->l_searchlist
.r_nlist
;
310 if (_dl_name_match_p (name
, _dl_loaded
->l_searchlist
.r_list
[n
]))
311 return _dl_loaded
->l_searchlist
.r_list
[n
];
313 /* Should never happen. */
318 match_version (const char *string
, struct link_map
*map
)
320 const char *strtab
= (const void *) map
->l_info
[DT_STRTAB
]->d_un
.d_ptr
;
323 #define VERDEFTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
324 if (map
->l_info
[VERDEFTAG
] == NULL
)
325 /* The file has no symbol versioning. */
328 def
= (ElfW(Verdef
) *) ((char *) map
->l_addr
329 + map
->l_info
[VERDEFTAG
]->d_un
.d_ptr
);
332 ElfW(Verdaux
) *aux
= (ElfW(Verdaux
) *) ((char *) def
+ def
->vd_aux
);
334 /* Compare the version strings. */
335 if (strcmp (string
, strtab
+ aux
->vda_name
) == 0)
339 /* If no more definitions we failed to find what we want. */
340 if (def
->vd_next
== 0)
343 /* Next definition. */
344 def
= (ElfW(Verdef
) *) ((char *) def
+ def
->vd_next
);
350 static const char *library_path
; /* The library search path. */
351 static const char *preloadlist
; /* The list preloaded objects. */
352 static int version_info
; /* Nonzero if information about
353 versions has to be printed. */
356 dl_main (const ElfW(Phdr
) *phdr
,
358 ElfW(Addr
) *user_entry
)
360 const ElfW(Phdr
) *ph
;
362 struct link_map
**preloads
;
363 unsigned int npreloads
;
368 int rtld_is_main
= 0;
373 /* Process the environment variable which control the behaviour. */
374 process_envvars (&mode
, &_dl_lazy
);
376 /* Set up a flag which tells we are just starting. */
379 if (*user_entry
== (ElfW(Addr
)) &ENTRY_POINT
)
381 /* Ho ho. We are not the program interpreter! We are the program
382 itself! This means someone ran ld.so as a command. Well, that
383 might be convenient to do sometimes. We support it by
384 interpreting the args like this:
386 ld.so PROGRAM ARGS...
388 The first argument is the name of a file containing an ELF
389 executable we will load and run with the following arguments.
390 To simplify life here, PROGRAM is searched for using the
391 normal rules for shared objects, rather than $PATH or anything
392 like that. We just load it and use its entry point; we don't
393 pay attention to its PT_INTERP command (we are the interpreter
394 ourselves). This is an easy way to test a new ld.so before
398 /* Note the place where the dynamic linker actually came from. */
399 _dl_rtld_map
.l_name
= _dl_argv
[0];
402 if (! strcmp (_dl_argv
[1], "--list"))
405 _dl_lazy
= -1; /* This means do no dependency analysis. */
411 else if (! strcmp (_dl_argv
[1], "--verify"))
419 else if (! strcmp (_dl_argv
[1], "--library-path") && _dl_argc
> 2)
421 library_path
= _dl_argv
[2];
427 else if (! strcmp (_dl_argv
[1], "--inhibit-rpath") && _dl_argc
> 2)
429 _dl_inhibit_rpath
= _dl_argv
[2];
438 /* If we have no further argument the program was called incorrectly.
439 Grant the user some education. */
442 Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
443 You have invoked `ld.so', the helper program for shared library executables.\n\
444 This program usually lives in the file `/lib/ld.so', and special directives\n\
445 in executable files using ELF shared libraries tell the system's program\n\
446 loader to load the helper program from this file. This helper program loads\n\
447 the shared libraries needed by the program executable, prepares the program\n\
448 to run, and runs it. You may invoke this helper program directly from the\n\
449 command line to load and run an ELF executable file; this is like executing\n\
450 that file itself, but always uses this helper program from the file you\n\
451 specified, instead of the helper program file specified in the executable\n\
452 file you run. This is mostly of use for maintainers to test new versions\n\
453 of this helper program; chances are you did not intend to run this program.\n\
455 --list list all dependencies and how they are resolved\n\
456 --verify verify that given object really is a dynamically linked\n\
457 object we get handle\n\
458 --library-path PATH use given PATH instead of content of the environment\n\
459 variable LD_LIBRARY_PATH\n\
460 --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\
468 /* Initialize the data structures for the search paths for shared
470 _dl_init_paths (library_path
);
472 if (__builtin_expect (mode
, normal
) == verify
)
474 char *err_str
= NULL
;
475 struct map_args args
;
477 args
.str
= _dl_argv
[0];
478 (void) _dl_catch_error (&err_str
, map_doit
, &args
);
482 _exit (EXIT_FAILURE
);
487 HP_TIMING_NOW (start
);
488 _dl_map_object (NULL
, _dl_argv
[0], 0, lt_library
, 0);
489 HP_TIMING_NOW (stop
);
491 HP_TIMING_DIFF (load_time
, start
, stop
);
494 phdr
= _dl_loaded
->l_phdr
;
495 phent
= _dl_loaded
->l_phnum
;
496 /* We overwrite here a pointer to a malloc()ed string. But since
497 the malloc() implementation used at this point is the dummy
498 implementations which has no real free() function it does not
499 makes sense to free the old string first. */
500 _dl_loaded
->l_name
= (char *) "";
501 *user_entry
= _dl_loaded
->l_entry
;
505 /* Create a link_map for the executable itself.
506 This will be what dlopen on "" returns. */
507 _dl_new_object ((char *) "", "", lt_executable
, NULL
);
508 if (_dl_loaded
== NULL
)
509 _dl_sysdep_fatal ("cannot allocate memory for link map\n", NULL
);
510 _dl_loaded
->l_phdr
= phdr
;
511 _dl_loaded
->l_phnum
= phent
;
512 _dl_loaded
->l_entry
= *user_entry
;
513 _dl_loaded
->l_opencount
= 1;
515 /* At this point we are in a bit of trouble. We would have to
516 fill in the values for l_dev and l_ino. But in general we
517 do not know where the file is. We also do not handle AT_EXECFD
518 even if it would be passed up.
520 We leave the values here defined to 0. This is normally no
521 problem as the program code itself is normally no shared
522 object and therefore cannot be loaded dynamically. Nothing
523 prevent the use of dynamic binaries and in these situations
524 we might get problems. We might not be able to find out
525 whether the object is already loaded. But since there is no
526 easy way out and because the dynamic binary must also not
527 have an SONAME we ignore this program for now. If it becomes
528 a problem we can force people using SONAMEs. */
530 /* We delay initializing the path structure until we got the dynamic
531 information for the program. */
534 /* It is not safe to load stuff after the main program. */
535 _dl_loaded
->l_map_end
= ~0;
536 /* Perhaps the executable has no PT_LOAD header entries at all. */
537 _dl_loaded
->l_map_start
= ~0;
539 /* Scan the program header table for the dynamic section. */
540 for (ph
= phdr
; ph
< &phdr
[phent
]; ++ph
)
544 /* Find out the load address. */
545 _dl_loaded
->l_addr
= (ElfW(Addr
)) phdr
- ph
->p_vaddr
;
548 /* This tells us where to find the dynamic section,
549 which tells us everything we need to do. */
550 _dl_loaded
->l_ld
= (void *) _dl_loaded
->l_addr
+ ph
->p_vaddr
;
553 /* This "interpreter segment" was used by the program loader to
554 find the program interpreter, which is this program itself, the
555 dynamic linker. We note what name finds us, so that a future
556 dlopen call or DT_NEEDED entry, for something that wants to link
557 against the dynamic linker as a shared library, will know that
558 the shared object is already loaded. */
559 _dl_rtld_libname
.name
= ((const char *) _dl_loaded
->l_addr
561 _dl_rtld_libname
.next
= NULL
;
562 _dl_rtld_map
.l_libname
= &_dl_rtld_libname
;
564 /* Ordinarilly, we would get additional names for the loader from
565 our DT_SONAME. This can't happen if we were actually linked as
566 a static executable (detect this case when we have no DYNAMIC).
567 If so, assume the filename component of the interpreter path to
568 be our SONAME, and add it to our name list. */
569 if (_dl_rtld_map
.l_ld
== NULL
)
571 char *p
= strrchr (_dl_rtld_libname
.name
, '/');
574 _dl_rtld_libname2
.name
= p
+1;
575 _dl_rtld_libname2
.next
= NULL
;
576 _dl_rtld_libname
.next
= &_dl_rtld_libname2
;
583 /* Remember where the main program starts in memory. */
586 mapstart
= _dl_loaded
->l_addr
+ (ph
->p_vaddr
& ~(ph
->p_align
- 1));
587 if (_dl_loaded
->l_map_start
> mapstart
)
588 _dl_loaded
->l_map_start
= mapstart
;
592 if (! _dl_rtld_map
.l_libname
&& _dl_rtld_map
.l_name
)
594 /* We were invoked directly, so the program might not have a
596 _dl_rtld_libname
.name
= _dl_rtld_map
.l_name
;
597 _dl_rtld_libname
.next
= NULL
;
598 _dl_rtld_map
.l_libname
= &_dl_rtld_libname
;
601 assert (_dl_rtld_map
.l_libname
); /* How else did we get here? */
605 /* Extract the contents of the dynamic section for easy access. */
606 elf_get_dynamic_info (_dl_loaded
);
607 if (_dl_loaded
->l_info
[DT_HASH
])
608 /* Set up our cache of pointers into the hash table. */
609 _dl_setup_hash (_dl_loaded
);
612 if (__builtin_expect (mode
, normal
) == verify
)
614 /* We were called just to verify that this is a dynamic
615 executable using us as the program interpreter. Exit with an
616 error if we were not able to load the binary or no interpreter
617 is specified (i.e., this is no dynamically linked binary. */
618 if (_dl_loaded
->l_ld
== NULL
)
621 /* We allow here some platform specific code. */
622 #ifdef DISTINGUISH_LIB_VERSIONS
623 DISTINGUISH_LIB_VERSIONS
;
625 _exit (has_interp
? 0 : 2);
629 /* Initialize the data structures for the search paths for shared
631 _dl_init_paths (library_path
);
633 /* Put the link_map for ourselves on the chain so it can be found by
634 name. Note that at this point the global chain of link maps contains
635 exactly one element, which is pointed to by _dl_loaded. */
636 if (! _dl_rtld_map
.l_name
)
637 /* If not invoked directly, the dynamic linker shared object file was
638 found by the PT_INTERP name. */
639 _dl_rtld_map
.l_name
= (char *) _dl_rtld_map
.l_libname
->name
;
640 _dl_rtld_map
.l_type
= lt_library
;
641 _dl_loaded
->l_next
= &_dl_rtld_map
;
642 _dl_rtld_map
.l_prev
= _dl_loaded
;
644 /* We have two ways to specify objects to preload: via environment
645 variable and via the file /etc/ld.so.preload. The later can also
646 be used when security is enabled. */
652 /* The LD_PRELOAD environment variable gives list of libraries
653 separated by white space or colons that are loaded before the
654 executable's dependencies and prepended to the global scope
655 list. If the binary is running setuid all elements
656 containing a '/' are ignored since it is insecure. */
657 char *list
= strdupa (preloadlist
);
660 HP_TIMING_NOW (start
);
662 while ((p
= strsep (&list
, " :")) != NULL
)
664 && (! __libc_enable_secure
|| strchr (p
, '/') == NULL
))
666 struct link_map
*new_map
= _dl_map_object (_dl_loaded
, p
, 1,
668 if (new_map
->l_opencount
== 1)
669 /* It is no duplicate. */
673 HP_TIMING_NOW (stop
);
674 HP_TIMING_DIFF (diff
, start
, stop
);
675 HP_TIMING_ACCUM_NT (load_time
, diff
);
678 /* Read the contents of the file. */
679 file
= _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size
,
680 PROT_READ
| PROT_WRITE
);
683 /* Parse the file. It contains names of libraries to be loaded,
684 separated by white spaces or `:'. It may also contain
685 comments introduced by `#'. */
690 /* Eliminate comments. */
695 char *comment
= memchr (runp
, '#', rest
);
699 rest
-= comment
- runp
;
702 while (--rest
> 0 && *++comment
!= '\n');
705 /* We have one problematic case: if we have a name at the end of
706 the file without a trailing terminating characters, we cannot
707 place the \0. Handle the case separately. */
708 if (file
[file_size
- 1] != ' ' && file
[file_size
- 1] != '\t'
709 && file
[file_size
- 1] != '\n' && file
[file_size
- 1] != ':')
711 problem
= &file
[file_size
];
712 while (problem
> file
&& problem
[-1] != ' ' && problem
[-1] != '\t'
713 && problem
[-1] != '\n' && problem
[-1] != ':')
722 file
[file_size
- 1] = '\0';
725 HP_TIMING_NOW (start
);
731 while ((p
= strsep (&runp
, ": \t\n")) != NULL
)
734 struct link_map
*new_map
= _dl_map_object (_dl_loaded
, p
, 1,
736 if (new_map
->l_opencount
== 1)
737 /* It is no duplicate. */
744 char *p
= strndupa (problem
, file_size
- (problem
- file
));
745 struct link_map
*new_map
= _dl_map_object (_dl_loaded
, p
, 1,
747 if (new_map
->l_opencount
== 1)
748 /* It is no duplicate. */
752 HP_TIMING_NOW (stop
);
753 HP_TIMING_DIFF (diff
, start
, stop
);
754 HP_TIMING_ACCUM_NT (load_time
, diff
);
756 /* We don't need the file anymore. */
757 __munmap (file
, file_size
);
762 /* Set up PRELOADS with a vector of the preloaded libraries. */
764 preloads
= __alloca (npreloads
* sizeof preloads
[0]);
765 l
= _dl_rtld_map
.l_next
; /* End of the chain before preloads. */
772 assert (i
== npreloads
);
775 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
776 specified some libraries to load, these are inserted before the actual
777 dependencies in the executable's searchlist for symbol resolution. */
778 HP_TIMING_NOW (start
);
779 _dl_map_object_deps (_dl_loaded
, preloads
, npreloads
, mode
== trace
, 0);
780 HP_TIMING_NOW (stop
);
781 HP_TIMING_DIFF (diff
, start
, stop
);
782 HP_TIMING_ACCUM_NT (load_time
, diff
);
784 /* Mark all objects as being in the global scope. */
785 for (i
= _dl_loaded
->l_searchlist
.r_nlist
; i
> 0; )
786 _dl_loaded
->l_searchlist
.r_list
[--i
]->l_global
= 1;
789 /* We are done mapping things, so close the zero-fill descriptor. */
790 __close (_dl_zerofd
);
794 /* Remove _dl_rtld_map from the chain. */
795 _dl_rtld_map
.l_prev
->l_next
= _dl_rtld_map
.l_next
;
796 if (_dl_rtld_map
.l_next
)
797 _dl_rtld_map
.l_next
->l_prev
= _dl_rtld_map
.l_prev
;
799 if (__builtin_expect (_dl_rtld_map
.l_opencount
, 2) > 1)
801 /* Some DT_NEEDED entry referred to the interpreter object itself, so
802 put it back in the list of visible objects. We insert it into the
803 chain in symbol search order because gdb uses the chain's order as
804 its symbol search order. */
806 while (_dl_loaded
->l_searchlist
.r_list
[i
] != &_dl_rtld_map
)
808 _dl_rtld_map
.l_prev
= _dl_loaded
->l_searchlist
.r_list
[i
- 1];
809 if (__builtin_expect (mode
, normal
) == normal
)
810 _dl_rtld_map
.l_next
= (i
+ 1 < _dl_loaded
->l_searchlist
.r_nlist
811 ? _dl_loaded
->l_searchlist
.r_list
[i
+ 1]
814 /* In trace mode there might be an invisible object (which we
815 could not find) after the previous one in the search list.
816 In this case it doesn't matter much where we put the
817 interpreter object, so we just initialize the list pointer so
818 that the assertion below holds. */
819 _dl_rtld_map
.l_next
= _dl_rtld_map
.l_prev
->l_next
;
821 assert (_dl_rtld_map
.l_prev
->l_next
== _dl_rtld_map
.l_next
);
822 _dl_rtld_map
.l_prev
->l_next
= &_dl_rtld_map
;
823 if (_dl_rtld_map
.l_next
)
825 assert (_dl_rtld_map
.l_next
->l_prev
== _dl_rtld_map
.l_prev
);
826 _dl_rtld_map
.l_next
->l_prev
= &_dl_rtld_map
;
830 /* Now let us see whether all libraries are available in the
833 struct version_check_args args
;
834 args
.doexit
= mode
== normal
;
835 _dl_receive_error (print_missing_version
, version_check_doit
, &args
);
838 if (__builtin_expect (mode
, normal
) != normal
)
840 /* We were run just to list the shared libraries. It is
841 important that we do this before real relocation, because the
842 functions we call below for output may no longer work properly
844 if (! _dl_loaded
->l_info
[DT_NEEDED
])
845 _dl_sysdep_message ("\t", "statically linked\n", NULL
);
850 for (l
= _dl_loaded
->l_next
; l
; l
= l
->l_next
)
851 if (l
->l_opencount
== 0)
852 /* The library was not found. */
853 _dl_sysdep_message ("\t", l
->l_libname
->name
, " => not found\n",
858 buf
[sizeof buf
- 1] = '\0';
859 bp
= _itoa_word (l
->l_addr
, &buf
[sizeof buf
- 1], 16, 0);
860 while ((size_t) (&buf
[sizeof buf
- 1] - bp
)
861 < sizeof l
->l_addr
* 2)
863 _dl_sysdep_message ("\t", l
->l_libname
->name
, " => ",
864 l
->l_name
, " (0x", bp
, ")\n", NULL
);
868 if (__builtin_expect (mode
, trace
) != trace
)
869 for (i
= 1; i
< _dl_argc
; ++i
)
871 const ElfW(Sym
) *ref
= NULL
;
872 ElfW(Addr
) loadbase
= _dl_lookup_symbol (_dl_argv
[i
], _dl_loaded
,
873 &ref
, _dl_loaded
->l_scope
,
874 ELF_MACHINE_JMP_SLOT
);
876 buf
[sizeof buf
- 1] = '\0';
877 bp
= _itoa_word (ref
->st_value
, &buf
[sizeof buf
- 1], 16, 0);
878 while ((size_t) (&buf
[sizeof buf
- 1] - bp
) < sizeof loadbase
* 2)
880 _dl_sysdep_message (_dl_argv
[i
], " found at 0x", bp
, NULL
);
881 buf
[sizeof buf
- 1] = '\0';
882 bp
= _itoa_word (loadbase
, &buf
[sizeof buf
- 1], 16, 0);
883 while ((size_t) (&buf
[sizeof buf
- 1] - bp
) < sizeof loadbase
* 2)
885 _dl_sysdep_message (" in object at 0x", bp
, "\n", NULL
);
891 /* We have to do symbol dependency testing. */
892 struct relocate_args args
;
895 args
.lazy
= _dl_lazy
;
902 if (l
!= &_dl_rtld_map
&& l
->l_opencount
> 0)
905 _dl_receive_error (print_unresolved
, relocate_doit
,
912 #define VERNEEDTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
915 /* Print more information. This means here, print information
916 about the versions needed. */
918 struct link_map
*map
= _dl_loaded
;
920 for (map
= _dl_loaded
; map
!= NULL
; map
= map
->l_next
)
923 ElfW(Dyn
) *dyn
= map
->l_info
[VERNEEDTAG
];
929 strtab
= (const void *) map
->l_info
[DT_STRTAB
]->d_un
.d_ptr
;
930 ent
= (ElfW(Verneed
) *) (map
->l_addr
+ dyn
->d_un
.d_ptr
);
934 _dl_sysdep_message ("\n\tVersion information:\n", NULL
);
938 _dl_sysdep_message ("\t", (map
->l_name
[0]
939 ? map
->l_name
: _dl_argv
[0]),
945 struct link_map
*needed
;
947 needed
= find_needed (strtab
+ ent
->vn_file
);
948 aux
= (ElfW(Vernaux
) *) ((char *) ent
+ ent
->vn_aux
);
952 const char *fname
= NULL
;
954 _dl_sysdep_message ("\t\t",
955 strtab
+ ent
->vn_file
,
956 " (", strtab
+ aux
->vna_name
,
964 && match_version (strtab
+aux
->vna_name
, needed
))
965 fname
= needed
->l_name
;
967 _dl_sysdep_message (fname
?: "not found", "\n",
970 if (aux
->vna_next
== 0)
971 /* No more symbols. */
975 aux
= (ElfW(Vernaux
) *) ((char *) aux
979 if (ent
->vn_next
== 0)
980 /* No more dependencies. */
983 /* Next dependency. */
984 ent
= (ElfW(Verneed
) *) ((char *) ent
+ ent
->vn_next
);
994 /* Now we have all the objects loaded. Relocate them all except for
995 the dynamic linker itself. We do this in reverse order so that copy
996 relocs of earlier objects overwrite the data written by later
997 objects. We do not re-relocate the dynamic linker itself in this
998 loop because that could result in the GOT entries for functions we
999 call being changed, and that would break us. It is safe to relocate
1000 the dynamic linker out of order because it has no copy relocs (we
1001 know that because it is self-contained). */
1004 int consider_profiling
= _dl_profile
!= NULL
;
1009 /* If we are profiling we also must do lazy reloaction. */
1010 _dl_lazy
|= consider_profiling
;
1016 HP_TIMING_NOW (start
);
1019 if (l
!= &_dl_rtld_map
)
1020 _dl_relocate_object (l
, l
->l_scope
, _dl_lazy
, consider_profiling
);
1025 HP_TIMING_NOW (stop
);
1027 HP_TIMING_DIFF (relocate_time
, start
, stop
);
1029 /* Do any necessary cleanups for the startup OS interface code.
1030 We do these now so that no calls are made after rtld re-relocation
1031 which might be resolved to different functions than we expect.
1032 We cannot do this before relocating the other objects because
1033 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
1034 _dl_sysdep_start_cleanup ();
1036 /* Now enable profiling if needed. Like the previous call,
1037 this has to go here because the calls it makes should use the
1038 rtld versions of the functions (particularly calloc()), but it
1039 needs to have _dl_profile_map set up by the relocator. */
1040 if (_dl_profile_map
!= NULL
)
1041 /* We must prepare the profiling. */
1042 _dl_start_profile (_dl_profile_map
, _dl_profile_output
);
1044 if (_dl_rtld_map
.l_opencount
> 0)
1046 /* There was an explicit ref to the dynamic linker as a shared lib.
1047 Re-relocate ourselves with user-controlled symbol definitions. */
1048 HP_TIMING_NOW (start
);
1049 _dl_relocate_object (&_dl_rtld_map
, _dl_loaded
->l_scope
, 0, 0);
1050 HP_TIMING_NOW (stop
);
1051 HP_TIMING_DIFF (add
, start
, stop
);
1052 HP_TIMING_ACCUM_NT (relocate_time
, add
);
1056 /* Now set up the variable which helps the assembler startup code. */
1057 _dl_main_searchlist
= &_dl_loaded
->l_searchlist
;
1058 _dl_global_scope
[0] = &_dl_loaded
->l_searchlist
;
1060 /* Safe the information about the original global scope list since
1061 we need it in the memory handling later. */
1062 _dl_initial_searchlist
= *_dl_main_searchlist
;
1065 /* Initialize _r_debug. */
1066 struct r_debug
*r
= _dl_debug_initialize (_dl_rtld_map
.l_addr
);
1071 #ifdef ELF_MACHINE_DEBUG_SETUP
1073 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
1075 ELF_MACHINE_DEBUG_SETUP (l
, r
);
1076 ELF_MACHINE_DEBUG_SETUP (&_dl_rtld_map
, r
);
1080 if (l
->l_info
[DT_DEBUG
])
1081 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
1082 with the run-time address of the r_debug structure */
1083 l
->l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
1085 /* Fill in the pointer in the dynamic linker's own dynamic section, in
1086 case you run gdb on the dynamic linker directly. */
1087 if (_dl_rtld_map
.l_info
[DT_DEBUG
])
1088 _dl_rtld_map
.l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
1092 /* Notify the debugger that all objects are now mapped in. */
1093 r
->r_state
= RT_ADD
;
1098 /* We must munmap() the cache file. */
1099 _dl_unload_cache ();
1102 /* Once we return, _dl_sysdep_start will invoke
1103 the DT_INIT functions and then *USER_ENTRY. */
1106 /* This is a little helper function for resolving symbols while
1107 tracing the binary. */
1109 print_unresolved (int errcode
__attribute__ ((unused
)), const char *objname
,
1110 const char *errstring
)
1112 if (objname
[0] == '\0')
1113 objname
= _dl_argv
[0] ?: "<main program>";
1114 _dl_sysdep_error (errstring
, " (", objname
, ")\n", NULL
);
1117 /* This is a little helper function for resolving symbols while
1118 tracing the binary. */
1120 print_missing_version (int errcode
__attribute__ ((unused
)),
1121 const char *objname
, const char *errstring
)
1123 _dl_sysdep_error (_dl_argv
[0] ?: "<program name unknown>", ": ",
1124 objname
, ": ", errstring
, "\n", NULL
);
1127 /* Nonzero if any of the debugging options is enabled. */
1128 static int any_debug
;
1130 /* Process the string given as the parameter which explains which debugging
1131 options are enabled. */
1133 process_dl_debug (const char *dl_debug
)
1136 #define separators " ,:"
1140 /* Skip separating white spaces and commas. */
1141 dl_debug
+= strspn (dl_debug
, separators
);
1142 if (*dl_debug
!= '\0')
1144 len
= strcspn (dl_debug
, separators
);
1149 /* This option is not documented since it is not generally
1151 if (memcmp (dl_debug
, "all", 3) == 0)
1154 _dl_debug_impcalls
= 1;
1155 _dl_debug_reloc
= 1;
1156 _dl_debug_files
= 1;
1157 _dl_debug_symbols
= 1;
1158 _dl_debug_bindings
= 1;
1159 _dl_debug_versions
= 1;
1166 if (memcmp (dl_debug
, "help", 4) == 0)
1168 _dl_sysdep_message ("\
1169 Valid options for the LD_DEBUG environment variable are:\n\
1171 bindings display information about symbol binding\n\
1172 files display processing of files and libraries\n\
1173 help display this help message and exit\n\
1174 libs display library search paths\n\
1175 reloc display relocation processing\n\
1176 symbols display symbol table processing\n\
1177 versions display version dependencies\n\
1179 To direct the debugging output into a file instead of standard output\n\
1180 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n",
1185 if (memcmp (dl_debug
, "libs", 4) == 0)
1188 _dl_debug_impcalls
= 1;
1195 if (memcmp (dl_debug
, "reloc", 5) == 0)
1197 _dl_debug_reloc
= 1;
1198 _dl_debug_impcalls
= 1;
1203 if (memcmp (dl_debug
, "files", 5) == 0)
1205 _dl_debug_files
= 1;
1206 _dl_debug_impcalls
= 1;
1213 if (memcmp (dl_debug
, "symbols", 7) == 0)
1215 _dl_debug_symbols
= 1;
1216 _dl_debug_impcalls
= 1;
1223 if (memcmp (dl_debug
, "bindings", 8) == 0)
1225 _dl_debug_bindings
= 1;
1226 _dl_debug_impcalls
= 1;
1231 if (memcmp (dl_debug
, "versions", 8) == 0)
1233 _dl_debug_versions
= 1;
1234 _dl_debug_impcalls
= 1;
1241 if (memcmp (dl_debug
, "statistics", 10) == 0)
1243 _dl_debug_statistics
= 1;
1253 /* Display a warning and skip everything until next separator. */
1254 char *startp
= strndupa (dl_debug
, len
);
1255 _dl_sysdep_error ("warning: debug option `", startp
,
1256 "' unknown; try LD_DEBUG=help\n", NULL
);
1261 while (*(dl_debug
+= len
) != '\0');
1264 /* Process all environments variables the dynamic linker must recognize.
1265 Since all of them start with `LD_' we are a bit smarter while finding
1268 process_envvars (enum mode
*modep
, int *lazyp
)
1272 enum mode mode
= normal
;
1274 char *debug_output
= NULL
;
1276 /* This is the default place for profiling data file. */
1277 _dl_profile_output
= "/var/tmp";
1279 while ((envline
= _dl_next_ld_env_entry (&runp
)) != NULL
)
1281 size_t len
= strcspn (envline
, "=") - 3;
1286 /* Warning level, verbose or not. */
1287 if (memcmp (&envline
[3], "WARN", 4) == 0)
1288 _dl_verbose
= envline
[8] != '\0';
1292 /* Debugging of the dynamic linker? */
1293 if (memcmp (&envline
[3], "DEBUG", 5) == 0)
1294 process_dl_debug (&envline
[9]);
1298 /* Print information about versions. */
1299 if (memcmp (&envline
[3], "VERBOSE", 7) == 0)
1301 version_info
= envline
[11] != '\0';
1305 /* List of objects to be preloaded. */
1306 if (memcmp (&envline
[3], "PRELOAD", 7) == 0)
1308 preloadlist
= &envline
[11];
1312 /* Which shared object shall be profiled. */
1313 if (memcmp (&envline
[3], "PROFILE", 7) == 0)
1314 _dl_profile
= &envline
[11];
1318 /* Do we bind early? */
1319 if (memcmp (&envline
[3], "BIND_NOW", 8) == 0)
1320 bind_now
= envline
[12] != '\0';
1324 /* Test whether we want to see the content of the auxiliary
1325 array passed up from the kernel. */
1326 if (memcmp (&envline
[3], "SHOW_AUXV", 9) == 0)
1331 /* Mask for the important hardware capabilities. */
1332 if (memcmp (&envline
[3], "HWCAP_MASK", 10) == 0)
1333 _dl_hwcap_mask
= strtoul (&envline
[14], NULL
, 0);
1337 /* Path where the binary is found. */
1338 if (!__libc_enable_secure
1339 && memcmp (&envline
[3], "ORIGIN_PATH", 11) == 0)
1340 _dl_origin_path
= &envline
[15];
1344 /* Where to place the profiling data file. */
1345 if (memcmp (&envline
[3], "DEBUG_OUTPUT", 12) == 0)
1347 debug_output
= &envline
[16];
1351 /* The library search path. */
1352 if (memcmp (&envline
[3], "LIBRARY_PATH", 12) == 0)
1353 library_path
= &envline
[16];
1357 /* Where to place the profiling data file. */
1358 if (!__libc_enable_secure
1359 && memcmp (&envline
[3], "PROFILE_OUTPUT", 14) == 0)
1361 _dl_profile_output
= &envline
[18];
1362 if (*_dl_profile_output
== '\0')
1363 _dl_profile_output
= "/var/tmp";
1368 /* The mode of the dynamic linker can be set. */
1369 if (memcmp (&envline
[3], "TRACE_LOADED_OBJECTS", 20) == 0)
1373 /* We might have some extra environment variable to handle. This
1374 is tricky due to the pre-processing of the length of the name
1375 in the switch statement here. The code here assumes that added
1376 environment variables have a different length. */
1377 #ifdef EXTRA_LD_ENVVARS
1383 /* Extra security for SUID binaries. Remove all dangerous environment
1385 if (__libc_enable_secure
)
1387 static const char *unsecure_envvars
[] =
1389 #ifdef EXTRA_UNSECURE_ENVVARS
1390 EXTRA_UNSECURE_ENVVARS
1395 if (preloadlist
!= NULL
)
1396 unsetenv ("LD_PRELOAD");
1397 if (library_path
!= NULL
)
1398 unsetenv ("LD_LIBRARY_PATH");
1399 if (_dl_origin_path
!= NULL
)
1400 unsetenv ("LD_ORIGIN_PATH");
1401 if (debug_output
!= NULL
)
1402 unsetenv ("LD_DEBUG_OUTPUT");
1403 if (_dl_profile
!= NULL
)
1404 unsetenv ("LD_PROFILE");
1407 cnt
< sizeof (unsecure_envvars
) / sizeof (unsecure_envvars
[0]);
1409 unsetenv (unsecure_envvars
[cnt
]);
1412 /* The name of the object to profile cannot be empty. */
1413 if (_dl_profile
!= NULL
&& *_dl_profile
== '\0')
1416 /* If we have to run the dynamic linker in debugging mode and the
1417 LD_DEBUG_OUTPUT environment variable is given, we write the debug
1418 messages to this file. */
1419 if (any_debug
&& debug_output
!= NULL
&& !__libc_enable_secure
)
1421 size_t name_len
= strlen (debug_output
);
1422 char buf
[name_len
+ 12];
1425 buf
[name_len
+ 11] = '\0';
1426 startp
= _itoa_word (__getpid (), &buf
[name_len
+ 11], 10, 0);
1428 startp
= memcpy (startp
- name_len
, debug_output
, name_len
);
1430 _dl_debug_fd
= __open (startp
, O_WRONLY
| O_APPEND
| O_CREAT
, 0666);
1431 if (_dl_debug_fd
== -1)
1432 /* We use standard output if opening the file failed. */
1433 _dl_debug_fd
= STDOUT_FILENO
;
1436 /* LAZY is determined by the environment variable LD_WARN and
1437 LD_BIND_NOW if we trace the binary. */
1438 if (__builtin_expect (mode
, normal
) == trace
)
1439 *lazyp
= _dl_verbose
? !bind_now
: -1;
1447 /* Print the various times we collected. */
1449 print_statistics (void)
1455 /* Total time rtld used. */
1456 if (HP_TIMING_AVAIL
)
1458 HP_TIMING_PRINT (buf
, sizeof (buf
), rtld_total_time
);
1459 _dl_debug_message (1, "\nruntime linker statistics:\n"
1460 " total startup time in dynamic loader: ",
1464 /* Print relocation statistics. */
1465 if (HP_TIMING_AVAIL
)
1467 HP_TIMING_PRINT (buf
, sizeof (buf
), relocate_time
);
1468 _dl_debug_message (1, " time needed for relocation: ", buf
,
1470 cp
= _itoa_word ((1000 * relocate_time
) / rtld_total_time
,
1471 buf
+ sizeof (buf
), 10, 0);
1473 switch (buf
+ sizeof (buf
) - cp
)
1484 _dl_debug_message (0, " (", buf
, "%)\n", NULL
);
1487 buf
[sizeof (buf
) - 1] = '\0';
1488 _dl_debug_message (1, " number of relocations: ",
1489 _itoa_word (_dl_num_relocations
,
1490 buf
+ sizeof (buf
) - 1, 10, 0),
1493 /* Time spend while loading the object and the dependencies. */
1494 if (HP_TIMING_AVAIL
)
1496 HP_TIMING_PRINT (buf
, sizeof (buf
), load_time
);
1497 _dl_debug_message (1, " time needed to load objects: ", buf
,
1499 cp
= _itoa_word ((1000 * load_time
) / rtld_total_time
,
1500 buf
+ sizeof (buf
), 10, 0);
1502 switch (buf
+ sizeof (buf
) - cp
)
1513 _dl_debug_message (0, " (", buf
, "%)\n", NULL
);