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
)
510 char *err_str
= NULL
;
511 struct map_args args
;
513 args
.str
= _dl_argv
[0];
514 (void) _dl_catch_error (&err_str
, map_doit
, &args
);
518 _exit (EXIT_FAILURE
);
523 HP_TIMING_NOW (start
);
524 _dl_map_object (NULL
, _dl_argv
[0], 0, lt_library
, 0);
525 HP_TIMING_NOW (stop
);
527 HP_TIMING_DIFF (load_time
, start
, stop
);
530 phdr
= _dl_loaded
->l_phdr
;
531 phent
= _dl_loaded
->l_phnum
;
532 /* We overwrite here a pointer to a malloc()ed string. But since
533 the malloc() implementation used at this point is the dummy
534 implementations which has no real free() function it does not
535 makes sense to free the old string first. */
536 _dl_loaded
->l_name
= (char *) "";
537 *user_entry
= _dl_loaded
->l_entry
;
541 /* Create a link_map for the executable itself.
542 This will be what dlopen on "" returns. */
543 _dl_new_object ((char *) "", "", lt_executable
, NULL
);
544 if (_dl_loaded
== NULL
)
545 _dl_sysdep_fatal ("cannot allocate memory for link map\n", NULL
);
546 _dl_loaded
->l_phdr
= phdr
;
547 _dl_loaded
->l_phnum
= phent
;
548 _dl_loaded
->l_entry
= *user_entry
;
549 _dl_loaded
->l_opencount
= 1;
551 /* At this point we are in a bit of trouble. We would have to
552 fill in the values for l_dev and l_ino. But in general we
553 do not know where the file is. We also do not handle AT_EXECFD
554 even if it would be passed up.
556 We leave the values here defined to 0. This is normally no
557 problem as the program code itself is normally no shared
558 object and therefore cannot be loaded dynamically. Nothing
559 prevent the use of dynamic binaries and in these situations
560 we might get problems. We might not be able to find out
561 whether the object is already loaded. But since there is no
562 easy way out and because the dynamic binary must also not
563 have an SONAME we ignore this program for now. If it becomes
564 a problem we can force people using SONAMEs. */
566 /* We delay initializing the path structure until we got the dynamic
567 information for the program. */
570 /* It is not safe to load stuff after the main program. */
571 _dl_loaded
->l_map_end
= ~0;
572 /* Perhaps the executable has no PT_LOAD header entries at all. */
573 _dl_loaded
->l_map_start
= ~0;
575 /* Scan the program header table for the dynamic section. */
576 for (ph
= phdr
; ph
< &phdr
[phent
]; ++ph
)
580 /* Find out the load address. */
581 _dl_loaded
->l_addr
= (ElfW(Addr
)) phdr
- ph
->p_vaddr
;
584 /* This tells us where to find the dynamic section,
585 which tells us everything we need to do. */
586 _dl_loaded
->l_ld
= (void *) _dl_loaded
->l_addr
+ ph
->p_vaddr
;
589 /* This "interpreter segment" was used by the program loader to
590 find the program interpreter, which is this program itself, the
591 dynamic linker. We note what name finds us, so that a future
592 dlopen call or DT_NEEDED entry, for something that wants to link
593 against the dynamic linker as a shared library, will know that
594 the shared object is already loaded. */
595 _dl_rtld_libname
.name
= ((const char *) _dl_loaded
->l_addr
597 _dl_rtld_libname
.next
= NULL
;
598 _dl_rtld_map
.l_libname
= &_dl_rtld_libname
;
600 /* Ordinarilly, we would get additional names for the loader from
601 our DT_SONAME. This can't happen if we were actually linked as
602 a static executable (detect this case when we have no DYNAMIC).
603 If so, assume the filename component of the interpreter path to
604 be our SONAME, and add it to our name list. */
605 if (_dl_rtld_map
.l_ld
== NULL
)
607 char *p
= strrchr (_dl_rtld_libname
.name
, '/');
610 _dl_rtld_libname2
.name
= p
+1;
611 _dl_rtld_libname2
.next
= NULL
;
612 _dl_rtld_libname
.next
= &_dl_rtld_libname2
;
619 /* Remember where the main program starts in memory. */
622 mapstart
= _dl_loaded
->l_addr
+ (ph
->p_vaddr
& ~(ph
->p_align
- 1));
623 if (_dl_loaded
->l_map_start
> mapstart
)
624 _dl_loaded
->l_map_start
= mapstart
;
628 if (! _dl_rtld_map
.l_libname
&& _dl_rtld_map
.l_name
)
630 /* We were invoked directly, so the program might not have a
632 _dl_rtld_libname
.name
= _dl_rtld_map
.l_name
;
633 _dl_rtld_libname
.next
= NULL
;
634 _dl_rtld_map
.l_libname
= &_dl_rtld_libname
;
637 assert (_dl_rtld_map
.l_libname
); /* How else did we get here? */
641 /* Extract the contents of the dynamic section for easy access. */
642 elf_get_dynamic_info (_dl_loaded
);
643 if (_dl_loaded
->l_info
[DT_HASH
])
644 /* Set up our cache of pointers into the hash table. */
645 _dl_setup_hash (_dl_loaded
);
648 if (__builtin_expect (mode
, normal
) == verify
)
650 /* We were called just to verify that this is a dynamic
651 executable using us as the program interpreter. Exit with an
652 error if we were not able to load the binary or no interpreter
653 is specified (i.e., this is no dynamically linked binary. */
654 if (_dl_loaded
->l_ld
== NULL
)
657 /* We allow here some platform specific code. */
658 #ifdef DISTINGUISH_LIB_VERSIONS
659 DISTINGUISH_LIB_VERSIONS
;
661 _exit (has_interp
? 0 : 2);
665 /* Initialize the data structures for the search paths for shared
667 _dl_init_paths (library_path
);
669 /* Put the link_map for ourselves on the chain so it can be found by
670 name. Note that at this point the global chain of link maps contains
671 exactly one element, which is pointed to by _dl_loaded. */
672 if (! _dl_rtld_map
.l_name
)
673 /* If not invoked directly, the dynamic linker shared object file was
674 found by the PT_INTERP name. */
675 _dl_rtld_map
.l_name
= (char *) _dl_rtld_map
.l_libname
->name
;
676 _dl_rtld_map
.l_type
= lt_library
;
677 _dl_loaded
->l_next
= &_dl_rtld_map
;
678 _dl_rtld_map
.l_prev
= _dl_loaded
;
680 /* We have two ways to specify objects to preload: via environment
681 variable and via the file /etc/ld.so.preload. The latter can also
682 be used when security is enabled. */
686 if (__builtin_expect (preloadlist
!= NULL
, 0))
688 /* The LD_PRELOAD environment variable gives list of libraries
689 separated by white space or colons that are loaded before the
690 executable's dependencies and prepended to the global scope
691 list. If the binary is running setuid all elements
692 containing a '/' are ignored since it is insecure. */
693 char *list
= strdupa (preloadlist
);
696 HP_TIMING_NOW (start
);
698 while ((p
= strsep (&list
, " :")) != NULL
)
700 && (__builtin_expect (! __libc_enable_secure
, 1)
701 || strchr (p
, '/') == NULL
))
703 struct link_map
*new_map
= _dl_map_object (_dl_loaded
, p
, 1,
705 if (new_map
->l_opencount
== 1)
706 /* It is no duplicate. */
710 HP_TIMING_NOW (stop
);
711 HP_TIMING_DIFF (diff
, start
, stop
);
712 HP_TIMING_ACCUM_NT (load_time
, diff
);
715 /* Read the contents of the file. */
716 file
= _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size
,
717 PROT_READ
| PROT_WRITE
);
718 if (__builtin_expect (file
!= NULL
, 0))
720 /* Parse the file. It contains names of libraries to be loaded,
721 separated by white spaces or `:'. It may also contain
722 comments introduced by `#'. */
727 /* Eliminate comments. */
732 char *comment
= memchr (runp
, '#', rest
);
736 rest
-= comment
- runp
;
739 while (--rest
> 0 && *++comment
!= '\n');
742 /* We have one problematic case: if we have a name at the end of
743 the file without a trailing terminating characters, we cannot
744 place the \0. Handle the case separately. */
745 if (file
[file_size
- 1] != ' ' && file
[file_size
- 1] != '\t'
746 && file
[file_size
- 1] != '\n' && file
[file_size
- 1] != ':')
748 problem
= &file
[file_size
];
749 while (problem
> file
&& problem
[-1] != ' ' && problem
[-1] != '\t'
750 && problem
[-1] != '\n' && problem
[-1] != ':')
759 file
[file_size
- 1] = '\0';
762 HP_TIMING_NOW (start
);
768 while ((p
= strsep (&runp
, ": \t\n")) != NULL
)
771 struct link_map
*new_map
= _dl_map_object (_dl_loaded
, p
, 1,
773 if (new_map
->l_opencount
== 1)
774 /* It is no duplicate. */
781 char *p
= strndupa (problem
, file_size
- (problem
- file
));
782 struct link_map
*new_map
= _dl_map_object (_dl_loaded
, p
, 1,
784 if (new_map
->l_opencount
== 1)
785 /* It is no duplicate. */
789 HP_TIMING_NOW (stop
);
790 HP_TIMING_DIFF (diff
, start
, stop
);
791 HP_TIMING_ACCUM_NT (load_time
, diff
);
793 /* We don't need the file anymore. */
794 __munmap (file
, file_size
);
797 if (__builtin_expect (npreloads
, 0) != 0)
799 /* Set up PRELOADS with a vector of the preloaded libraries. */
801 preloads
= __alloca (npreloads
* sizeof preloads
[0]);
802 l
= _dl_rtld_map
.l_next
; /* End of the chain before preloads. */
809 assert (i
== npreloads
);
812 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
813 specified some libraries to load, these are inserted before the actual
814 dependencies in the executable's searchlist for symbol resolution. */
815 HP_TIMING_NOW (start
);
816 _dl_map_object_deps (_dl_loaded
, preloads
, npreloads
, mode
== trace
);
817 HP_TIMING_NOW (stop
);
818 HP_TIMING_DIFF (diff
, start
, stop
);
819 HP_TIMING_ACCUM_NT (load_time
, diff
);
821 /* Mark all objects as being in the global scope. */
822 for (i
= _dl_loaded
->l_searchlist
.r_nlist
; i
> 0; )
823 _dl_loaded
->l_searchlist
.r_list
[--i
]->l_global
= 1;
826 /* We are done mapping things, so close the zero-fill descriptor. */
827 __close (_dl_zerofd
);
831 /* Remove _dl_rtld_map from the chain. */
832 _dl_rtld_map
.l_prev
->l_next
= _dl_rtld_map
.l_next
;
833 if (_dl_rtld_map
.l_next
)
834 _dl_rtld_map
.l_next
->l_prev
= _dl_rtld_map
.l_prev
;
836 if (__builtin_expect (_dl_rtld_map
.l_opencount
, 2) > 1)
838 /* Some DT_NEEDED entry referred to the interpreter object itself, so
839 put it back in the list of visible objects. We insert it into the
840 chain in symbol search order because gdb uses the chain's order as
841 its symbol search order. */
843 while (_dl_loaded
->l_searchlist
.r_list
[i
] != &_dl_rtld_map
)
845 _dl_rtld_map
.l_prev
= _dl_loaded
->l_searchlist
.r_list
[i
- 1];
846 if (__builtin_expect (mode
, normal
) == normal
)
847 _dl_rtld_map
.l_next
= (i
+ 1 < _dl_loaded
->l_searchlist
.r_nlist
848 ? _dl_loaded
->l_searchlist
.r_list
[i
+ 1]
851 /* In trace mode there might be an invisible object (which we
852 could not find) after the previous one in the search list.
853 In this case it doesn't matter much where we put the
854 interpreter object, so we just initialize the list pointer so
855 that the assertion below holds. */
856 _dl_rtld_map
.l_next
= _dl_rtld_map
.l_prev
->l_next
;
858 assert (_dl_rtld_map
.l_prev
->l_next
== _dl_rtld_map
.l_next
);
859 _dl_rtld_map
.l_prev
->l_next
= &_dl_rtld_map
;
860 if (_dl_rtld_map
.l_next
)
862 assert (_dl_rtld_map
.l_next
->l_prev
== _dl_rtld_map
.l_prev
);
863 _dl_rtld_map
.l_next
->l_prev
= &_dl_rtld_map
;
867 /* Now let us see whether all libraries are available in the
870 struct version_check_args args
;
871 args
.doexit
= mode
== normal
;
872 args
.dotrace
= mode
== trace
;
873 _dl_receive_error (print_missing_version
, version_check_doit
, &args
);
876 if (__builtin_expect (mode
, normal
) != normal
)
878 /* We were run just to list the shared libraries. It is
879 important that we do this before real relocation, because the
880 functions we call below for output may no longer work properly
882 if (! _dl_loaded
->l_info
[DT_NEEDED
])
883 _dl_sysdep_message ("\t", "statically linked\n", NULL
);
888 for (l
= _dl_loaded
->l_next
; l
; l
= l
->l_next
)
889 if (l
->l_opencount
== 0)
890 /* The library was not found. */
891 _dl_sysdep_message ("\t", l
->l_libname
->name
, " => not found\n",
896 buf
[sizeof buf
- 1] = '\0';
897 bp
= _itoa_word (l
->l_addr
, &buf
[sizeof buf
- 1], 16, 0);
898 while ((size_t) (&buf
[sizeof buf
- 1] - bp
)
899 < sizeof l
->l_addr
* 2)
901 _dl_sysdep_message ("\t", l
->l_libname
->name
, " => ",
902 l
->l_name
, " (0x", bp
, ")\n", NULL
);
906 if (__builtin_expect (mode
, trace
) != trace
)
907 for (i
= 1; i
< _dl_argc
; ++i
)
909 const ElfW(Sym
) *ref
= NULL
;
914 result
= _dl_lookup_symbol (_dl_argv
[i
], _dl_loaded
,
915 &ref
, _dl_loaded
->l_scope
,
916 ELF_MACHINE_JMP_SLOT
);
918 loadbase
= LOOKUP_VALUE_ADDRESS (result
);
920 buf
[sizeof buf
- 1] = '\0';
921 bp
= _itoa_word (ref
->st_value
, &buf
[sizeof buf
- 1], 16, 0);
922 while ((size_t) (&buf
[sizeof buf
- 1] - bp
) < sizeof loadbase
* 2)
924 _dl_sysdep_message (_dl_argv
[i
], " found at 0x", bp
, NULL
);
925 buf
[sizeof buf
- 1] = '\0';
926 bp
= _itoa_word (loadbase
, &buf
[sizeof buf
- 1], 16, 0);
927 while ((size_t) (&buf
[sizeof buf
- 1] - bp
) < sizeof loadbase
* 2)
929 _dl_sysdep_message (" in object at 0x", bp
, "\n", NULL
);
935 /* We have to do symbol dependency testing. */
936 struct relocate_args args
;
939 args
.lazy
= _dl_lazy
;
946 if (l
!= &_dl_rtld_map
&& l
->l_opencount
> 0)
949 _dl_receive_error (print_unresolved
, relocate_doit
,
956 #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
959 /* Print more information. This means here, print information
960 about the versions needed. */
962 struct link_map
*map
= _dl_loaded
;
964 for (map
= _dl_loaded
; map
!= NULL
; map
= map
->l_next
)
967 ElfW(Dyn
) *dyn
= map
->l_info
[VERNEEDTAG
];
973 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
974 ent
= (ElfW(Verneed
) *) (map
->l_addr
+ dyn
->d_un
.d_ptr
);
978 _dl_sysdep_message ("\n\tVersion information:\n", NULL
);
982 _dl_sysdep_message ("\t", (map
->l_name
[0]
983 ? map
->l_name
: _dl_argv
[0]),
989 struct link_map
*needed
;
991 needed
= find_needed (strtab
+ ent
->vn_file
);
992 aux
= (ElfW(Vernaux
) *) ((char *) ent
+ ent
->vn_aux
);
996 const char *fname
= NULL
;
998 _dl_sysdep_message ("\t\t",
999 strtab
+ ent
->vn_file
,
1000 " (", strtab
+ aux
->vna_name
,
1008 && match_version (strtab
+aux
->vna_name
, needed
))
1009 fname
= needed
->l_name
;
1011 _dl_sysdep_message (fname
?: "not found", "\n",
1014 if (aux
->vna_next
== 0)
1015 /* No more symbols. */
1019 aux
= (ElfW(Vernaux
) *) ((char *) aux
1023 if (ent
->vn_next
== 0)
1024 /* No more dependencies. */
1027 /* Next dependency. */
1028 ent
= (ElfW(Verneed
) *) ((char *) ent
+ ent
->vn_next
);
1038 /* Now we have all the objects loaded. Relocate them all except for
1039 the dynamic linker itself. We do this in reverse order so that copy
1040 relocs of earlier objects overwrite the data written by later
1041 objects. We do not re-relocate the dynamic linker itself in this
1042 loop because that could result in the GOT entries for functions we
1043 call being changed, and that would break us. It is safe to relocate
1044 the dynamic linker out of order because it has no copy relocs (we
1045 know that because it is self-contained). */
1048 int consider_profiling
= _dl_profile
!= NULL
;
1049 #ifndef HP_TIMING_NONAVAIL
1055 /* If we are profiling we also must do lazy reloaction. */
1056 _dl_lazy
|= consider_profiling
;
1062 HP_TIMING_NOW (start
);
1065 if (l
!= &_dl_rtld_map
)
1066 _dl_relocate_object (l
, l
->l_scope
, _dl_lazy
, consider_profiling
);
1071 HP_TIMING_NOW (stop
);
1073 HP_TIMING_DIFF (relocate_time
, start
, stop
);
1075 /* Do any necessary cleanups for the startup OS interface code.
1076 We do these now so that no calls are made after rtld re-relocation
1077 which might be resolved to different functions than we expect.
1078 We cannot do this before relocating the other objects because
1079 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
1080 _dl_sysdep_start_cleanup ();
1082 /* Now enable profiling if needed. Like the previous call,
1083 this has to go here because the calls it makes should use the
1084 rtld versions of the functions (particularly calloc()), but it
1085 needs to have _dl_profile_map set up by the relocator. */
1086 if (__builtin_expect (_dl_profile_map
!= NULL
, 0))
1087 /* We must prepare the profiling. */
1088 _dl_start_profile (_dl_profile_map
, _dl_profile_output
);
1090 if (_dl_rtld_map
.l_opencount
> 1)
1092 /* There was an explicit ref to the dynamic linker as a shared lib.
1093 Re-relocate ourselves with user-controlled symbol definitions. */
1094 HP_TIMING_NOW (start
);
1095 _dl_relocate_object (&_dl_rtld_map
, _dl_loaded
->l_scope
, 0, 0);
1096 HP_TIMING_NOW (stop
);
1097 HP_TIMING_DIFF (add
, start
, stop
);
1098 HP_TIMING_ACCUM_NT (relocate_time
, add
);
1102 /* Now set up the variable which helps the assembler startup code. */
1103 _dl_main_searchlist
= &_dl_loaded
->l_searchlist
;
1104 _dl_global_scope
[0] = &_dl_loaded
->l_searchlist
;
1106 /* Safe the information about the original global scope list since
1107 we need it in the memory handling later. */
1108 _dl_initial_searchlist
= *_dl_main_searchlist
;
1111 /* Initialize _r_debug. */
1112 struct r_debug
*r
= _dl_debug_initialize (_dl_rtld_map
.l_addr
);
1117 #ifdef ELF_MACHINE_DEBUG_SETUP
1119 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
1121 ELF_MACHINE_DEBUG_SETUP (l
, r
);
1122 ELF_MACHINE_DEBUG_SETUP (&_dl_rtld_map
, r
);
1126 if (l
->l_info
[DT_DEBUG
])
1127 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
1128 with the run-time address of the r_debug structure */
1129 l
->l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
1131 /* Fill in the pointer in the dynamic linker's own dynamic section, in
1132 case you run gdb on the dynamic linker directly. */
1133 if (_dl_rtld_map
.l_info
[DT_DEBUG
])
1134 _dl_rtld_map
.l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
1138 /* Notify the debugger that all objects are now mapped in. */
1139 r
->r_state
= RT_ADD
;
1144 /* We must munmap() the cache file. */
1145 _dl_unload_cache ();
1148 /* Once we return, _dl_sysdep_start will invoke
1149 the DT_INIT functions and then *USER_ENTRY. */
1152 /* This is a little helper function for resolving symbols while
1153 tracing the binary. */
1155 print_unresolved (int errcode
__attribute__ ((unused
)), const char *objname
,
1156 const char *errstring
)
1158 if (objname
[0] == '\0')
1159 objname
= _dl_argv
[0] ?: "<main program>";
1160 _dl_sysdep_error (errstring
, " (", objname
, ")\n", NULL
);
1163 /* This is a little helper function for resolving symbols while
1164 tracing the binary. */
1166 print_missing_version (int errcode
__attribute__ ((unused
)),
1167 const char *objname
, const char *errstring
)
1169 _dl_sysdep_error (_dl_argv
[0] ?: "<program name unknown>", ": ",
1170 objname
, ": ", errstring
, "\n", NULL
);
1173 /* Nonzero if any of the debugging options is enabled. */
1174 static int any_debug
;
1176 /* Process the string given as the parameter which explains which debugging
1177 options are enabled. */
1179 process_dl_debug (const char *dl_debug
)
1182 #define separators " ,:"
1186 /* Skip separating white spaces and commas. */
1187 dl_debug
+= strspn (dl_debug
, separators
);
1188 if (*dl_debug
!= '\0')
1190 len
= strcspn (dl_debug
, separators
);
1195 /* This option is not documented since it is not generally
1197 if (memcmp (dl_debug
, "all", 3) == 0)
1200 _dl_debug_impcalls
= 1;
1201 _dl_debug_reloc
= 1;
1202 _dl_debug_files
= 1;
1203 _dl_debug_symbols
= 1;
1204 _dl_debug_bindings
= 1;
1205 _dl_debug_versions
= 1;
1212 if (memcmp (dl_debug
, "help", 4) == 0)
1214 _dl_sysdep_message ("\
1215 Valid options for the LD_DEBUG environment variable are:\n\
1217 bindings display information about symbol binding\n\
1218 files display processing of files and libraries\n\
1219 help display this help message and exit\n\
1220 libs display library search paths\n\
1221 reloc display relocation processing\n\
1222 statistics display relocation statistics\n\
1223 symbols display symbol table processing\n\
1224 versions display version dependencies\n\
1226 To direct the debugging output into a file instead of standard output\n\
1227 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n",
1232 if (memcmp (dl_debug
, "libs", 4) == 0)
1235 _dl_debug_impcalls
= 1;
1242 if (memcmp (dl_debug
, "reloc", 5) == 0)
1244 _dl_debug_reloc
= 1;
1245 _dl_debug_impcalls
= 1;
1250 if (memcmp (dl_debug
, "files", 5) == 0)
1252 _dl_debug_files
= 1;
1253 _dl_debug_impcalls
= 1;
1260 if (memcmp (dl_debug
, "symbols", 7) == 0)
1262 _dl_debug_symbols
= 1;
1263 _dl_debug_impcalls
= 1;
1270 if (memcmp (dl_debug
, "bindings", 8) == 0)
1272 _dl_debug_bindings
= 1;
1273 _dl_debug_impcalls
= 1;
1278 if (memcmp (dl_debug
, "versions", 8) == 0)
1280 _dl_debug_versions
= 1;
1281 _dl_debug_impcalls
= 1;
1288 if (memcmp (dl_debug
, "statistics", 10) == 0)
1290 _dl_debug_statistics
= 1;
1300 /* Display a warning and skip everything until next separator. */
1301 char *startp
= strndupa (dl_debug
, len
);
1302 _dl_sysdep_error ("warning: debug option `", startp
,
1303 "' unknown; try LD_DEBUG=help\n", NULL
);
1308 while (*(dl_debug
+= len
) != '\0');
1311 /* Process all environments variables the dynamic linker must recognize.
1312 Since all of them start with `LD_' we are a bit smarter while finding
1315 process_envvars (enum mode
*modep
, int *lazyp
)
1319 enum mode mode
= normal
;
1321 char *debug_output
= NULL
;
1323 /* This is the default place for profiling data file. */
1324 _dl_profile_output
= "/var/tmp";
1326 while ((envline
= _dl_next_ld_env_entry (&runp
)) != NULL
)
1328 size_t len
= strcspn (envline
, "=");
1330 if (envline
[len
] != '=')
1331 /* This is a "LD_" variable at the end of the string without
1332 a '=' character. Ignore it since otherwise we will access
1333 invalid memory below. */
1339 /* Warning level, verbose or not. */
1340 if (memcmp (&envline
[3], "WARN", 4) == 0)
1341 _dl_verbose
= envline
[8] != '\0';
1345 /* Debugging of the dynamic linker? */
1346 if (memcmp (&envline
[3], "DEBUG", 5) == 0)
1347 process_dl_debug (&envline
[9]);
1351 /* Print information about versions. */
1352 if (memcmp (&envline
[3], "VERBOSE", 7) == 0)
1354 version_info
= envline
[11] != '\0';
1358 /* List of objects to be preloaded. */
1359 if (memcmp (&envline
[3], "PRELOAD", 7) == 0)
1361 preloadlist
= &envline
[11];
1365 /* Which shared object shall be profiled. */
1366 if (memcmp (&envline
[3], "PROFILE", 7) == 0)
1367 _dl_profile
= &envline
[11];
1371 /* Do we bind early? */
1372 if (memcmp (&envline
[3], "BIND_NOW", 8) == 0)
1373 bind_now
= envline
[12] != '\0';
1377 /* Test whether we want to see the content of the auxiliary
1378 array passed up from the kernel. */
1379 if (memcmp (&envline
[3], "SHOW_AUXV", 9) == 0)
1384 /* Mask for the important hardware capabilities. */
1385 if (memcmp (&envline
[3], "HWCAP_MASK", 10) == 0)
1386 _dl_hwcap_mask
= strtoul (&envline
[14], NULL
, 0);
1390 /* Path where the binary is found. */
1391 if (!__libc_enable_secure
1392 && memcmp (&envline
[3], "ORIGIN_PATH", 11) == 0)
1393 _dl_origin_path
= &envline
[15];
1397 /* The library search path. */
1398 if (memcmp (&envline
[3], "LIBRARY_PATH", 12) == 0)
1400 library_path
= &envline
[16];
1404 /* Where to place the profiling data file. */
1405 if (memcmp (&envline
[3], "DEBUG_OUTPUT", 12) == 0)
1407 debug_output
= &envline
[16];
1411 if (memcmp (&envline
[3], "DYNAMIC_WEAK", 12) == 0)
1412 _dl_dynamic_weak
= 1;
1416 /* Where to place the profiling data file. */
1417 if (!__libc_enable_secure
1418 && memcmp (&envline
[3], "PROFILE_OUTPUT", 14) == 0)
1420 _dl_profile_output
= &envline
[18];
1421 if (*_dl_profile_output
== '\0')
1422 _dl_profile_output
= "/var/tmp";
1427 /* The mode of the dynamic linker can be set. */
1428 if (memcmp (&envline
[3], "TRACE_LOADED_OBJECTS", 20) == 0)
1432 /* We might have some extra environment variable to handle. This
1433 is tricky due to the pre-processing of the length of the name
1434 in the switch statement here. The code here assumes that added
1435 environment variables have a different length. */
1436 #ifdef EXTRA_LD_ENVVARS
1442 /* Extra security for SUID binaries. Remove all dangerous environment
1444 if (__libc_enable_secure
)
1446 static const char *unsecure_envvars
[] =
1448 #ifdef EXTRA_UNSECURE_ENVVARS
1449 EXTRA_UNSECURE_ENVVARS
1454 if (preloadlist
!= NULL
)
1455 unsetenv ("LD_PRELOAD");
1456 if (library_path
!= NULL
)
1457 unsetenv ("LD_LIBRARY_PATH");
1458 if (_dl_origin_path
!= NULL
)
1459 unsetenv ("LD_ORIGIN_PATH");
1460 if (debug_output
!= NULL
)
1461 unsetenv ("LD_DEBUG_OUTPUT");
1462 if (_dl_profile
!= NULL
)
1463 unsetenv ("LD_PROFILE");
1466 cnt
< sizeof (unsecure_envvars
) / sizeof (unsecure_envvars
[0]);
1468 unsetenv (unsecure_envvars
[cnt
]);
1471 /* The name of the object to profile cannot be empty. */
1472 if (_dl_profile
!= NULL
&& *_dl_profile
== '\0')
1475 /* If we have to run the dynamic linker in debugging mode and the
1476 LD_DEBUG_OUTPUT environment variable is given, we write the debug
1477 messages to this file. */
1478 if (any_debug
&& debug_output
!= NULL
&& !__libc_enable_secure
)
1480 size_t name_len
= strlen (debug_output
);
1481 char buf
[name_len
+ 12];
1484 buf
[name_len
+ 11] = '\0';
1485 startp
= _itoa_word (__getpid (), &buf
[name_len
+ 11], 10, 0);
1487 startp
= memcpy (startp
- name_len
, debug_output
, name_len
);
1489 _dl_debug_fd
= __open (startp
, O_WRONLY
| O_APPEND
| O_CREAT
, 0666);
1490 if (_dl_debug_fd
== -1)
1491 /* We use standard output if opening the file failed. */
1492 _dl_debug_fd
= STDOUT_FILENO
;
1495 /* LAZY is determined by the environment variable LD_WARN and
1496 LD_BIND_NOW if we trace the binary. */
1497 if (__builtin_expect (mode
, normal
) == trace
)
1498 *lazyp
= _dl_verbose
? !bind_now
: -1;
1506 /* Print the various times we collected. */
1508 print_statistics (void)
1511 #ifndef HP_TIMING_NONAVAIL
1515 /* Total time rtld used. */
1516 if (HP_TIMING_AVAIL
)
1518 HP_TIMING_PRINT (buf
, sizeof (buf
), rtld_total_time
);
1519 _dl_debug_message (1, "\nruntime linker statistics:\n"
1520 " total startup time in dynamic loader: ",
1524 /* Print relocation statistics. */
1525 if (HP_TIMING_AVAIL
)
1527 HP_TIMING_PRINT (buf
, sizeof (buf
), relocate_time
);
1528 _dl_debug_message (1, " time needed for relocation: ", buf
,
1530 cp
= _itoa_word ((1000 * relocate_time
) / rtld_total_time
,
1531 buf
+ sizeof (buf
), 10, 0);
1533 switch (buf
+ sizeof (buf
) - cp
)
1544 _dl_debug_message (0, " (", buf
, "%)\n", NULL
);
1547 buf
[sizeof (buf
) - 1] = '\0';
1548 _dl_debug_message (1, " number of relocations: ",
1549 _itoa_word (_dl_num_relocations
,
1550 buf
+ sizeof (buf
) - 1, 10, 0),
1553 #ifndef HP_TIMING_NONAVAIL
1554 /* Time spend while loading the object and the dependencies. */
1555 if (HP_TIMING_AVAIL
)
1557 HP_TIMING_PRINT (buf
, sizeof (buf
), load_time
);
1558 _dl_debug_message (1, " time needed to load objects: ", buf
,
1560 cp
= _itoa_word ((1000 * load_time
) / rtld_total_time
,
1561 buf
+ sizeof (buf
), 10, 0);
1563 switch (buf
+ sizeof (buf
) - cp
)
1574 _dl_debug_message (0, " (", buf
, "%)\n", NULL
);