1 /* Run time dynamic linker.
2 Copyright (C) 1995-1999, 2000, 2001 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 #include <sys/mman.h> /* Check if MAP_ANON is defined. */
27 #include <stdio-common/_itoa.h>
29 #include <fpu_control.h>
30 #include <hp-timing.h>
31 #include <bits/libc-lock.h>
32 #include "dynamic-link.h"
33 #include "dl-librecon.h"
34 #include <unsecvars.h>
38 /* Helper function to handle errors while resolving symbols. */
39 static void print_unresolved (int errcode
, const char *objname
,
40 const char *errsting
);
42 /* Helper function to handle errors when a version is missing. */
43 static void print_missing_version (int errcode
, const char *objname
,
44 const char *errsting
);
46 /* Print the various times we collected. */
47 static void print_statistics (void);
49 /* This is a list of all the modes the dynamic loader can be in. */
50 enum mode
{ normal
, list
, verify
, trace
};
52 /* Process all environments variables the dynamic linker must recognize.
53 Since all of them start with `LD_' we are a bit smarter while finding
55 static void process_envvars (enum mode
*modep
);
59 unsigned int _dl_skip_args
; /* Nonzero if we were run directly. */
61 const char *_dl_platform
;
62 size_t _dl_platformlen
;
63 unsigned long _dl_hwcap
;
64 fpu_control_t _dl_fpu_control
= _FPU_DEFAULT
;
65 struct r_search_path
*_dl_search_paths
;
66 const char *_dl_profile
;
67 const char *_dl_profile_output
;
68 struct link_map
*_dl_profile_map
;
70 /* XXX I know about at least one case where we depend on the old weak
71 behavior (it has to do with librt). Until we get DSO groups implemented
72 we have to make this the default. Bummer. --drepper */
76 int _dl_dynamic_weak
= 1;
79 const char *_dl_inhibit_rpath
; /* RPATH values which should be
81 const char *_dl_origin_path
;
84 /* This is a pointer to the map for the main object and through it to
85 all loaded objects. */
86 struct link_map
*_dl_loaded
;
87 /* Number of object in the _dl_loaded list. */
88 unsigned int _dl_nloaded
;
89 /* Pointer to the l_searchlist element of the link map of the main object. */
90 struct r_scope_elem
*_dl_main_searchlist
;
91 /* Copy of the content of `_dl_main_searchlist'. */
92 struct r_scope_elem _dl_initial_searchlist
;
93 /* Array which is used when looking up in the global scope. */
94 struct r_scope_elem
*_dl_global_scope
[2];
96 /* During the program run we must not modify the global data of
97 loaded shared object simultanously in two threads. Therefore we
98 protect `_dl_open' and `_dl_close' in dl-close.c.
100 This must be a recursive lock since the initializer function of
101 the loaded object might as well require a call to this function.
102 At this time it is not anymore a problem to modify the tables. */
103 __libc_lock_define_initialized_recursive (, _dl_load_lock
)
105 /* Set nonzero during loading and initialization of executable and
106 libraries, cleared before the executable's entry point runs. This
107 must not be initialized to nonzero, because the unused dynamic
108 linker loaded in for libc.so's "ld.so.1" dep will provide the
109 definition seen by libc.so's initializer; that value must be zero,
110 and will be since that dynamic linker's _dl_start and dl_main will
115 static void dl_main (const ElfW(Phdr
) *phdr
,
117 ElfW(Addr
) *user_entry
);
119 struct link_map _dl_rtld_map
;
120 struct libname_list _dl_rtld_libname
;
121 struct libname_list _dl_rtld_libname2
;
123 /* Variable for statistics. */
124 #ifndef HP_TIMING_NONAVAIL
125 static hp_timing_t rtld_total_time
;
126 static hp_timing_t relocate_time
;
127 static hp_timing_t load_time
;
129 extern unsigned long int _dl_num_relocations
; /* in dl-lookup.c */
131 static ElfW(Addr
) _dl_start_final (void *arg
, struct link_map
*bootstrap_map_p
,
132 hp_timing_t start_time
);
137 #error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
141 _dl_start (void *arg
)
143 struct link_map bootstrap_map
;
144 hp_timing_t start_time
;
147 /* This #define produces dynamic linking inline functions for
148 bootstrap relocation instead of general-purpose relocation. */
149 #define RTLD_BOOTSTRAP
150 #define RESOLVE_MAP(sym, version, flags) \
151 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : &bootstrap_map)
152 #define RESOLVE(sym, version, flags) \
153 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : bootstrap_map.l_addr)
154 #include "dynamic-link.h"
156 if (HP_TIMING_INLINE
&& HP_TIMING_AVAIL
)
157 HP_TIMING_NOW (start_time
);
159 /* Partly clean the `bootstrap_map' structure up. Don't use `memset'
160 since it might not be built in or inlined and we cannot make function
161 calls at this point. */
163 cnt
< sizeof (bootstrap_map
.l_info
) / sizeof (bootstrap_map
.l_info
[0]);
165 bootstrap_map
.l_info
[cnt
] = 0;
167 /* Figure out the run-time load address of the dynamic linker itself. */
168 bootstrap_map
.l_addr
= elf_machine_load_address ();
170 /* Read our own dynamic section and fill in the info array. */
171 bootstrap_map
.l_ld
= (void *) bootstrap_map
.l_addr
+ elf_machine_dynamic ();
172 elf_get_dynamic_info (&bootstrap_map
);
174 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
175 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map
.l_info
);
178 /* Relocate ourselves so we can do normal function calls and
179 data access using the global offset table. */
181 ELF_DYNAMIC_RELOCATE (&bootstrap_map
, 0, 0);
182 /* Please note that we don't allow profiling of this object and
183 therefore need not test whether we have to allocate the array
184 for the relocation results (as done in dl-reloc.c). */
186 /* Now life is sane; we can call functions and access global data.
187 Set up to use the operating system facilities, and find out from
188 the operating system's program loader where to find the program
189 header table in core. Put the rest of _dl_start into a separate
190 function, that way the compiler cannot put accesses to the GOT
191 before ELF_DYNAMIC_RELOCATE. */
193 ElfW(Addr
) entry
= _dl_start_final (arg
, &bootstrap_map
, start_time
);
195 #ifndef ELF_MACHINE_START_ADDRESS
196 # define ELF_MACHINE_START_ADDRESS(map, start) (start)
199 return ELF_MACHINE_START_ADDRESS (_dl_loaded
, entry
);
205 _dl_start_final (void *arg
, struct link_map
*bootstrap_map_p
,
206 hp_timing_t start_time
)
208 /* The use of `alloca' here looks ridiculous but it helps. The goal
209 is to avoid the function from being inlined. There is no official
210 way to do this so we use this trick. gcc never inlines functions
211 which use `alloca'. */
212 ElfW(Addr
) *start_addr
= alloca (sizeof (ElfW(Addr
)));
216 /* If it hasn't happen yet record the startup time. */
217 if (! HP_TIMING_INLINE
)
218 HP_TIMING_NOW (start_time
);
220 /* Initialize the timing functions. */
221 HP_TIMING_DIFF_INIT ();
224 /* Transfer data about ourselves to the permanent link_map structure. */
225 _dl_rtld_map
.l_addr
= bootstrap_map_p
->l_addr
;
226 _dl_rtld_map
.l_ld
= bootstrap_map_p
->l_ld
;
227 _dl_rtld_map
.l_opencount
= 1;
228 memcpy (_dl_rtld_map
.l_info
, bootstrap_map_p
->l_info
,
229 sizeof _dl_rtld_map
.l_info
);
230 _dl_setup_hash (&_dl_rtld_map
);
231 _dl_rtld_map
.l_mach
= bootstrap_map_p
->l_mach
;
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
);
242 #ifndef HP_TIMING_NONAVAIL
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
);
255 if (__builtin_expect (_dl_debug_mask
& DL_DEBUG_STATISTICS
, 0))
261 /* Now life is peachy; we can do all normal operations.
262 On to the real work. */
264 /* Some helper functions. */
266 /* Arguments to relocate_doit. */
275 /* Argument to map_doit. */
277 /* Return value of map_doit. */
278 struct link_map
*main_map
;
281 /* Arguments to version_check_doit. */
282 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, 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
->dotrace
) && 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_THISPROCNUM + 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;
379 #ifndef HP_TIMING_NONAVAIL
385 /* Process the environment variable which control the behaviour. */
386 process_envvars (&mode
);
388 /* Set up a flag which tells we are just starting. */
391 if (*user_entry
== (ElfW(Addr
)) ENTRY_POINT
)
393 /* Ho ho. We are not the program interpreter! We are the program
394 itself! This means someone ran ld.so as a command. Well, that
395 might be convenient to do sometimes. We support it by
396 interpreting the args like this:
398 ld.so PROGRAM ARGS...
400 The first argument is the name of a file containing an ELF
401 executable we will load and run with the following arguments.
402 To simplify life here, PROGRAM is searched for using the
403 normal rules for shared objects, rather than $PATH or anything
404 like that. We just load it and use its entry point; we don't
405 pay attention to its PT_INTERP command (we are the interpreter
406 ourselves). This is an easy way to test a new ld.so before
410 /* Note the place where the dynamic linker actually came from. */
411 _dl_rtld_map
.l_name
= _dl_argv
[0];
414 if (! strcmp (_dl_argv
[1], "--list"))
417 _dl_lazy
= -1; /* This means do no dependency analysis. */
423 else if (! strcmp (_dl_argv
[1], "--verify"))
431 else if (! strcmp (_dl_argv
[1], "--library-path") && _dl_argc
> 2)
433 library_path
= _dl_argv
[2];
439 else if (! strcmp (_dl_argv
[1], "--inhibit-rpath") && _dl_argc
> 2)
441 _dl_inhibit_rpath
= _dl_argv
[2];
450 /* If we have no further argument the program was called incorrectly.
451 Grant the user some education. */
454 Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
455 You have invoked `ld.so', the helper program for shared library executables.\n\
456 This program usually lives in the file `/lib/ld.so', and special directives\n\
457 in executable files using ELF shared libraries tell the system's program\n\
458 loader to load the helper program from this file. This helper program loads\n\
459 the shared libraries needed by the program executable, prepares the program\n\
460 to run, and runs it. You may invoke this helper program directly from the\n\
461 command line to load and run an ELF executable file; this is like executing\n\
462 that file itself, but always uses this helper program from the file you\n\
463 specified, instead of the helper program file specified in the executable\n\
464 file you run. This is mostly of use for maintainers to test new versions\n\
465 of this helper program; chances are you did not intend to run this program.\n\
467 --list list all dependencies and how they are resolved\n\
468 --verify verify that given object really is a dynamically linked\n\
469 object we can handle\n\
470 --library-path PATH use given PATH instead of content of the environment\n\
471 variable LD_LIBRARY_PATH\n\
472 --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\
479 /* Initialize the data structures for the search paths for shared
481 _dl_init_paths (library_path
);
483 if (__builtin_expect (mode
, normal
) == verify
)
486 const char *err_str
= NULL
;
487 struct map_args args
;
489 args
.str
= _dl_argv
[0];
490 (void) _dl_catch_error (&objname
, &err_str
, map_doit
, &args
);
491 if (__builtin_expect (err_str
!= NULL
, 0))
493 if (err_str
!= _dl_out_of_memory
)
494 free ((char *) err_str
);
495 _exit (EXIT_FAILURE
);
500 HP_TIMING_NOW (start
);
501 _dl_map_object (NULL
, _dl_argv
[0], 0, lt_library
, 0, 0);
502 HP_TIMING_NOW (stop
);
504 HP_TIMING_DIFF (load_time
, start
, stop
);
507 phdr
= _dl_loaded
->l_phdr
;
508 phnum
= _dl_loaded
->l_phnum
;
509 /* We overwrite here a pointer to a malloc()ed string. But since
510 the malloc() implementation used at this point is the dummy
511 implementations which has no real free() function it does not
512 makes sense to free the old string first. */
513 _dl_loaded
->l_name
= (char *) "";
514 *user_entry
= _dl_loaded
->l_entry
;
518 /* Create a link_map for the executable itself.
519 This will be what dlopen on "" returns. */
520 _dl_new_object ((char *) "", "", lt_executable
, NULL
);
521 if (_dl_loaded
== NULL
)
522 _dl_fatal_printf ("cannot allocate memory for link map\n");
523 _dl_loaded
->l_phdr
= phdr
;
524 _dl_loaded
->l_phnum
= phnum
;
525 _dl_loaded
->l_entry
= *user_entry
;
527 /* At this point we are in a bit of trouble. We would have to
528 fill in the values for l_dev and l_ino. But in general we
529 do not know where the file is. We also do not handle AT_EXECFD
530 even if it would be passed up.
532 We leave the values here defined to 0. This is normally no
533 problem as the program code itself is normally no shared
534 object and therefore cannot be loaded dynamically. Nothing
535 prevent the use of dynamic binaries and in these situations
536 we might get problems. We might not be able to find out
537 whether the object is already loaded. But since there is no
538 easy way out and because the dynamic binary must also not
539 have an SONAME we ignore this program for now. If it becomes
540 a problem we can force people using SONAMEs. */
542 /* We delay initializing the path structure until we got the dynamic
543 information for the program. */
546 _dl_loaded
->l_map_end
= 0;
547 /* Perhaps the executable has no PT_LOAD header entries at all. */
548 _dl_loaded
->l_map_start
= ~0;
550 /* Scan the program header table for the dynamic section. */
551 for (ph
= phdr
; ph
< &phdr
[phnum
]; ++ph
)
555 /* Find out the load address. */
556 _dl_loaded
->l_addr
= (ElfW(Addr
)) phdr
- ph
->p_vaddr
;
559 /* This tells us where to find the dynamic section,
560 which tells us everything we need to do. */
561 _dl_loaded
->l_ld
= (void *) _dl_loaded
->l_addr
+ ph
->p_vaddr
;
564 /* This "interpreter segment" was used by the program loader to
565 find the program interpreter, which is this program itself, the
566 dynamic linker. We note what name finds us, so that a future
567 dlopen call or DT_NEEDED entry, for something that wants to link
568 against the dynamic linker as a shared library, will know that
569 the shared object is already loaded. */
570 _dl_rtld_libname
.name
= ((const char *) _dl_loaded
->l_addr
572 /* _dl_rtld_libname.next = NULL; Already zero. */
573 _dl_rtld_map
.l_libname
= &_dl_rtld_libname
;
575 /* Ordinarilly, we would get additional names for the loader from
576 our DT_SONAME. This can't happen if we were actually linked as
577 a static executable (detect this case when we have no DYNAMIC).
578 If so, assume the filename component of the interpreter path to
579 be our SONAME, and add it to our name list. */
580 if (_dl_rtld_map
.l_ld
== NULL
)
582 char *p
= strrchr (_dl_rtld_libname
.name
, '/');
585 _dl_rtld_libname2
.name
= p
+1;
586 /* _dl_rtld_libname2.next = NULL; Already zero. */
587 _dl_rtld_libname
.next
= &_dl_rtld_libname2
;
598 /* Remember where the main program starts in memory. */
599 mapstart
= _dl_loaded
->l_addr
+ (ph
->p_vaddr
& ~(ph
->p_align
- 1));
600 if (_dl_loaded
->l_map_start
> mapstart
)
601 _dl_loaded
->l_map_start
= mapstart
;
603 /* Also where it ends. */
604 allocend
= _dl_loaded
->l_addr
+ ph
->p_vaddr
+ ph
->p_memsz
;
605 if (_dl_loaded
->l_map_end
< allocend
)
606 _dl_loaded
->l_map_end
= allocend
;
610 if (! _dl_loaded
->l_map_end
)
611 _dl_loaded
->l_map_end
= ~0;
612 if (! _dl_rtld_map
.l_libname
&& _dl_rtld_map
.l_name
)
614 /* We were invoked directly, so the program might not have a
616 _dl_rtld_libname
.name
= _dl_rtld_map
.l_name
;
617 /* _dl_rtld_libname.next = NULL; Alread zero. */
618 _dl_rtld_map
.l_libname
= &_dl_rtld_libname
;
621 assert (_dl_rtld_map
.l_libname
); /* How else did we get here? */
625 /* Extract the contents of the dynamic section for easy access. */
626 elf_get_dynamic_info (_dl_loaded
);
627 if (_dl_loaded
->l_info
[DT_HASH
])
628 /* Set up our cache of pointers into the hash table. */
629 _dl_setup_hash (_dl_loaded
);
632 if (__builtin_expect (mode
, normal
) == verify
)
634 /* We were called just to verify that this is a dynamic
635 executable using us as the program interpreter. Exit with an
636 error if we were not able to load the binary or no interpreter
637 is specified (i.e., this is no dynamically linked binary. */
638 if (_dl_loaded
->l_ld
== NULL
)
641 /* We allow here some platform specific code. */
642 #ifdef DISTINGUISH_LIB_VERSIONS
643 DISTINGUISH_LIB_VERSIONS
;
645 _exit (has_interp
? 0 : 2);
649 /* Initialize the data structures for the search paths for shared
651 _dl_init_paths (library_path
);
653 /* Put the link_map for ourselves on the chain so it can be found by
654 name. Note that at this point the global chain of link maps contains
655 exactly one element, which is pointed to by _dl_loaded. */
656 if (! _dl_rtld_map
.l_name
)
657 /* If not invoked directly, the dynamic linker shared object file was
658 found by the PT_INTERP name. */
659 _dl_rtld_map
.l_name
= (char *) _dl_rtld_map
.l_libname
->name
;
660 _dl_rtld_map
.l_type
= lt_library
;
661 _dl_loaded
->l_next
= &_dl_rtld_map
;
662 _dl_rtld_map
.l_prev
= _dl_loaded
;
665 /* We have two ways to specify objects to preload: via environment
666 variable and via the file /etc/ld.so.preload. The latter can also
667 be used when security is enabled. */
671 if (__builtin_expect (preloadlist
!= NULL
, 0))
673 /* The LD_PRELOAD environment variable gives list of libraries
674 separated by white space or colons that are loaded before the
675 executable's dependencies and prepended to the global scope
676 list. If the binary is running setuid all elements
677 containing a '/' are ignored since it is insecure. */
678 char *list
= strdupa (preloadlist
);
681 HP_TIMING_NOW (start
);
683 while ((p
= strsep (&list
, " :")) != NULL
)
685 && (__builtin_expect (! __libc_enable_secure
, 1)
686 || strchr (p
, '/') == NULL
))
688 struct link_map
*new_map
= _dl_map_object (_dl_loaded
, p
, 1,
690 if (++new_map
->l_opencount
== 1)
691 /* It is no duplicate. */
695 HP_TIMING_NOW (stop
);
696 HP_TIMING_DIFF (diff
, start
, stop
);
697 HP_TIMING_ACCUM_NT (load_time
, diff
);
700 /* Read the contents of the file. */
701 file
= _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size
,
702 PROT_READ
| PROT_WRITE
);
703 if (__builtin_expect (file
!= NULL
, 0))
705 /* Parse the file. It contains names of libraries to be loaded,
706 separated by white spaces or `:'. It may also contain
707 comments introduced by `#'. */
712 /* Eliminate comments. */
717 char *comment
= memchr (runp
, '#', rest
);
721 rest
-= comment
- runp
;
724 while (--rest
> 0 && *++comment
!= '\n');
727 /* We have one problematic case: if we have a name at the end of
728 the file without a trailing terminating characters, we cannot
729 place the \0. Handle the case separately. */
730 if (file
[file_size
- 1] != ' ' && file
[file_size
- 1] != '\t'
731 && file
[file_size
- 1] != '\n' && file
[file_size
- 1] != ':')
733 problem
= &file
[file_size
];
734 while (problem
> file
&& problem
[-1] != ' ' && problem
[-1] != '\t'
735 && problem
[-1] != '\n' && problem
[-1] != ':')
744 file
[file_size
- 1] = '\0';
747 HP_TIMING_NOW (start
);
753 while ((p
= strsep (&runp
, ": \t\n")) != NULL
)
756 struct link_map
*new_map
= _dl_map_object (_dl_loaded
, p
, 1,
758 if (++new_map
->l_opencount
== 1)
759 /* It is no duplicate. */
766 char *p
= strndupa (problem
, file_size
- (problem
- file
));
767 struct link_map
*new_map
= _dl_map_object (_dl_loaded
, p
, 1,
769 if (++new_map
->l_opencount
== 1)
770 /* It is no duplicate. */
774 HP_TIMING_NOW (stop
);
775 HP_TIMING_DIFF (diff
, start
, stop
);
776 HP_TIMING_ACCUM_NT (load_time
, diff
);
778 /* We don't need the file anymore. */
779 __munmap (file
, file_size
);
782 if (__builtin_expect (npreloads
, 0) != 0)
784 /* Set up PRELOADS with a vector of the preloaded libraries. */
786 preloads
= __alloca (npreloads
* sizeof preloads
[0]);
787 l
= _dl_rtld_map
.l_next
; /* End of the chain before preloads. */
794 assert (i
== npreloads
);
797 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
798 specified some libraries to load, these are inserted before the actual
799 dependencies in the executable's searchlist for symbol resolution. */
800 HP_TIMING_NOW (start
);
801 _dl_map_object_deps (_dl_loaded
, preloads
, npreloads
, mode
== trace
);
802 HP_TIMING_NOW (stop
);
803 HP_TIMING_DIFF (diff
, start
, stop
);
804 HP_TIMING_ACCUM_NT (load_time
, diff
);
806 /* Mark all objects as being in the global scope and set the open
808 for (i
= _dl_loaded
->l_searchlist
.r_nlist
; i
> 0; )
811 _dl_loaded
->l_searchlist
.r_list
[i
]->l_global
= 1;
812 ++_dl_loaded
->l_searchlist
.r_list
[i
]->l_opencount
;
816 /* We are done mapping things, so close the zero-fill descriptor. */
817 __close (_dl_zerofd
);
821 /* Remove _dl_rtld_map from the chain. */
822 _dl_rtld_map
.l_prev
->l_next
= _dl_rtld_map
.l_next
;
823 if (_dl_rtld_map
.l_next
)
824 _dl_rtld_map
.l_next
->l_prev
= _dl_rtld_map
.l_prev
;
826 if (__builtin_expect (_dl_rtld_map
.l_opencount
> 1, 1))
828 /* Some DT_NEEDED entry referred to the interpreter object itself, so
829 put it back in the list of visible objects. We insert it into the
830 chain in symbol search order because gdb uses the chain's order as
831 its symbol search order. */
833 while (_dl_loaded
->l_searchlist
.r_list
[i
] != &_dl_rtld_map
)
835 _dl_rtld_map
.l_prev
= _dl_loaded
->l_searchlist
.r_list
[i
- 1];
836 if (__builtin_expect (mode
, normal
) == normal
)
837 _dl_rtld_map
.l_next
= (i
+ 1 < _dl_loaded
->l_searchlist
.r_nlist
838 ? _dl_loaded
->l_searchlist
.r_list
[i
+ 1]
841 /* In trace mode there might be an invisible object (which we
842 could not find) after the previous one in the search list.
843 In this case it doesn't matter much where we put the
844 interpreter object, so we just initialize the list pointer so
845 that the assertion below holds. */
846 _dl_rtld_map
.l_next
= _dl_rtld_map
.l_prev
->l_next
;
848 assert (_dl_rtld_map
.l_prev
->l_next
== _dl_rtld_map
.l_next
);
849 _dl_rtld_map
.l_prev
->l_next
= &_dl_rtld_map
;
850 if (_dl_rtld_map
.l_next
)
852 assert (_dl_rtld_map
.l_next
->l_prev
== _dl_rtld_map
.l_prev
);
853 _dl_rtld_map
.l_next
->l_prev
= &_dl_rtld_map
;
857 /* Now let us see whether all libraries are available in the
860 struct version_check_args args
;
861 args
.doexit
= mode
== normal
;
862 args
.dotrace
= mode
== trace
;
863 _dl_receive_error (print_missing_version
, version_check_doit
, &args
);
866 if (__builtin_expect (mode
, normal
) != normal
)
868 /* We were run just to list the shared libraries. It is
869 important that we do this before real relocation, because the
870 functions we call below for output may no longer work properly
872 if (! _dl_loaded
->l_info
[DT_NEEDED
])
873 _dl_printf ("\tstatically linked\n");
878 for (l
= _dl_loaded
->l_next
; l
; l
= l
->l_next
)
880 /* The library was not found. */
881 _dl_printf ("\t%s => not found\n", l
->l_libname
->name
);
883 _dl_printf ("\t%s => %s (0x%0*Zx)\n", l
->l_libname
->name
,
884 l
->l_name
, (int) sizeof l
->l_addr
* 2, l
->l_addr
);
887 if (__builtin_expect (mode
, trace
) != trace
)
888 for (i
= 1; i
< _dl_argc
; ++i
)
890 const ElfW(Sym
) *ref
= NULL
;
894 result
= _dl_lookup_symbol (_dl_argv
[i
], _dl_loaded
,
895 &ref
, _dl_loaded
->l_scope
,
896 ELF_MACHINE_JMP_SLOT
, 1);
898 loadbase
= LOOKUP_VALUE_ADDRESS (result
);
900 _dl_printf ("%s found at 0x%0*Zd in object at 0x%0*Zd\n",
902 (int) sizeof ref
->st_value
* 2, ref
->st_value
,
903 (int) sizeof loadbase
* 2, loadbase
);
907 /* If LD_WARN is set warn about undefined symbols. */
908 if (_dl_lazy
>= 0 && _dl_verbose
)
910 /* We have to do symbol dependency testing. */
911 struct relocate_args args
;
914 args
.lazy
= _dl_lazy
;
921 if (l
!= &_dl_rtld_map
&& ! l
->l_faked
)
924 _dl_receive_error (print_unresolved
, relocate_doit
,
931 #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
934 /* Print more information. This means here, print information
935 about the versions needed. */
937 struct link_map
*map
= _dl_loaded
;
939 for (map
= _dl_loaded
; map
!= NULL
; map
= map
->l_next
)
942 ElfW(Dyn
) *dyn
= map
->l_info
[VERNEEDTAG
];
948 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
949 ent
= (ElfW(Verneed
) *) (map
->l_addr
+ dyn
->d_un
.d_ptr
);
953 _dl_printf ("\n\tVersion information:\n");
957 _dl_printf ("\t%s:\n",
958 map
->l_name
[0] ? map
->l_name
: _dl_argv
[0]);
963 struct link_map
*needed
;
965 needed
= find_needed (strtab
+ ent
->vn_file
);
966 aux
= (ElfW(Vernaux
) *) ((char *) ent
+ ent
->vn_aux
);
970 const char *fname
= NULL
;
973 && match_version (strtab
+ aux
->vna_name
,
975 fname
= needed
->l_name
;
977 _dl_printf ("\t\t%s (%s) %s=> %s\n",
978 strtab
+ ent
->vn_file
,
979 strtab
+ aux
->vna_name
,
980 aux
->vna_flags
& VER_FLG_WEAK
982 fname
?: "not found");
984 if (aux
->vna_next
== 0)
985 /* No more symbols. */
989 aux
= (ElfW(Vernaux
) *) ((char *) aux
993 if (ent
->vn_next
== 0)
994 /* No more dependencies. */
997 /* Next dependency. */
998 ent
= (ElfW(Verneed
) *) ((char *) ent
+ ent
->vn_next
);
1008 /* Now we have all the objects loaded. Relocate them all except for
1009 the dynamic linker itself. We do this in reverse order so that copy
1010 relocs of earlier objects overwrite the data written by later
1011 objects. We do not re-relocate the dynamic linker itself in this
1012 loop because that could result in the GOT entries for functions we
1013 call being changed, and that would break us. It is safe to relocate
1014 the dynamic linker out of order because it has no copy relocs (we
1015 know that because it is self-contained). */
1018 int consider_profiling
= _dl_profile
!= NULL
;
1019 #ifndef HP_TIMING_NONAVAIL
1025 /* If we are profiling we also must do lazy reloaction. */
1026 _dl_lazy
|= consider_profiling
;
1032 HP_TIMING_NOW (start
);
1035 /* While we are at it, help the memory handling a bit. We have to
1036 mark some data structures as allocated with the fake malloc()
1037 implementation in ld.so. */
1038 struct libname_list
*lnp
= l
->l_libname
->next
;
1040 while (__builtin_expect (lnp
!= NULL
, 0))
1046 if (l
!= &_dl_rtld_map
)
1047 _dl_relocate_object (l
, l
->l_scope
, _dl_lazy
, consider_profiling
);
1052 HP_TIMING_NOW (stop
);
1054 HP_TIMING_DIFF (relocate_time
, start
, stop
);
1056 /* Do any necessary cleanups for the startup OS interface code.
1057 We do these now so that no calls are made after rtld re-relocation
1058 which might be resolved to different functions than we expect.
1059 We cannot do this before relocating the other objects because
1060 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
1061 _dl_sysdep_start_cleanup ();
1063 /* Now enable profiling if needed. Like the previous call,
1064 this has to go here because the calls it makes should use the
1065 rtld versions of the functions (particularly calloc()), but it
1066 needs to have _dl_profile_map set up by the relocator. */
1067 if (__builtin_expect (_dl_profile_map
!= NULL
, 0))
1068 /* We must prepare the profiling. */
1069 _dl_start_profile (_dl_profile_map
, _dl_profile_output
);
1071 if (_dl_rtld_map
.l_opencount
> 1)
1073 /* There was an explicit ref to the dynamic linker as a shared lib.
1074 Re-relocate ourselves with user-controlled symbol definitions. */
1075 HP_TIMING_NOW (start
);
1076 _dl_relocate_object (&_dl_rtld_map
, _dl_loaded
->l_scope
, 0, 0);
1077 HP_TIMING_NOW (stop
);
1078 HP_TIMING_DIFF (add
, start
, stop
);
1079 HP_TIMING_ACCUM_NT (relocate_time
, add
);
1083 /* Now set up the variable which helps the assembler startup code. */
1084 _dl_main_searchlist
= &_dl_loaded
->l_searchlist
;
1085 _dl_global_scope
[0] = &_dl_loaded
->l_searchlist
;
1087 /* Safe the information about the original global scope list since
1088 we need it in the memory handling later. */
1089 _dl_initial_searchlist
= *_dl_main_searchlist
;
1092 /* Initialize _r_debug. */
1093 struct r_debug
*r
= _dl_debug_initialize (_dl_rtld_map
.l_addr
);
1098 #ifdef ELF_MACHINE_DEBUG_SETUP
1100 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
1102 ELF_MACHINE_DEBUG_SETUP (l
, r
);
1103 ELF_MACHINE_DEBUG_SETUP (&_dl_rtld_map
, r
);
1107 if (l
->l_info
[DT_DEBUG
])
1108 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
1109 with the run-time address of the r_debug structure */
1110 l
->l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
1112 /* Fill in the pointer in the dynamic linker's own dynamic section, in
1113 case you run gdb on the dynamic linker directly. */
1114 if (_dl_rtld_map
.l_info
[DT_DEBUG
])
1115 _dl_rtld_map
.l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
1119 /* Notify the debugger that all objects are now mapped in. */
1120 r
->r_state
= RT_ADD
;
1125 /* We must munmap() the cache file. */
1126 _dl_unload_cache ();
1129 /* Once we return, _dl_sysdep_start will invoke
1130 the DT_INIT functions and then *USER_ENTRY. */
1133 /* This is a little helper function for resolving symbols while
1134 tracing the binary. */
1136 print_unresolved (int errcode
__attribute__ ((unused
)), const char *objname
,
1137 const char *errstring
)
1139 if (objname
[0] == '\0')
1140 objname
= _dl_argv
[0] ?: "<main program>";
1141 _dl_error_printf ("%s (%s)\n", errstring
, objname
);
1144 /* This is a little helper function for resolving symbols while
1145 tracing the binary. */
1147 print_missing_version (int errcode
__attribute__ ((unused
)),
1148 const char *objname
, const char *errstring
)
1150 _dl_error_printf ("%s: %s: %s\n", _dl_argv
[0] ?: "<program name unknown>",
1151 objname
, errstring
);
1154 /* Nonzero if any of the debugging options is enabled. */
1155 static int any_debug
;
1157 /* Process the string given as the parameter which explains which debugging
1158 options are enabled. */
1160 process_dl_debug (const char *dl_debug
)
1163 #define separators " ,:"
1167 /* Skip separating white spaces and commas. */
1168 dl_debug
+= strspn (dl_debug
, separators
);
1169 if (*dl_debug
!= '\0')
1171 len
= strcspn (dl_debug
, separators
);
1176 /* This option is not documented since it is not generally
1178 if (memcmp (dl_debug
, "all", 3) == 0)
1180 _dl_debug_mask
= (DL_DEBUG_LIBS
| DL_DEBUG_IMPCALLS
1181 | DL_DEBUG_RELOC
| DL_DEBUG_FILES
1182 | DL_DEBUG_SYMBOLS
| DL_DEBUG_BINDINGS
1183 | DL_DEBUG_VERSIONS
);
1190 if (memcmp (dl_debug
, "help", 4) == 0)
1193 Valid options for the LD_DEBUG environment variable are:\n\
1195 bindings display information about symbol binding\n\
1196 files display processing of files and libraries\n\
1197 help display this help message and exit\n\
1198 libs display library search paths\n\
1199 reloc display relocation processing\n\
1200 statistics display relocation statistics\n\
1201 symbols display symbol table processing\n\
1202 versions display version dependencies\n\
1204 To direct the debugging output into a file instead of standard output\n\
1205 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n");
1209 if (memcmp (dl_debug
, "libs", 4) == 0)
1211 _dl_debug_mask
|= DL_DEBUG_LIBS
| DL_DEBUG_IMPCALLS
;
1218 if (memcmp (dl_debug
, "reloc", 5) == 0)
1220 _dl_debug_mask
|= DL_DEBUG_RELOC
| DL_DEBUG_IMPCALLS
;
1225 if (memcmp (dl_debug
, "files", 5) == 0)
1227 _dl_debug_mask
|= DL_DEBUG_FILES
| DL_DEBUG_IMPCALLS
;
1234 if (memcmp (dl_debug
, "symbols", 7) == 0)
1236 _dl_debug_mask
|= DL_DEBUG_SYMBOLS
| DL_DEBUG_IMPCALLS
;
1243 if (memcmp (dl_debug
, "bindings", 8) == 0)
1245 _dl_debug_mask
|= DL_DEBUG_BINDINGS
| DL_DEBUG_IMPCALLS
;
1250 if (memcmp (dl_debug
, "versions", 8) == 0)
1252 _dl_debug_mask
|= DL_DEBUG_VERSIONS
| DL_DEBUG_IMPCALLS
;
1259 if (memcmp (dl_debug
, "statistics", 10) == 0)
1261 _dl_debug_mask
|= DL_DEBUG_STATISTICS
;
1271 /* Display a warning and skip everything until next separator. */
1272 char *startp
= strndupa (dl_debug
, len
);
1273 _dl_error_printf ("\
1274 warning: debug option `%s' unknown; try LD_DEBUG=help\n", startp
);
1279 while (*(dl_debug
+= len
) != '\0');
1282 /* Process all environments variables the dynamic linker must recognize.
1283 Since all of them start with `LD_' we are a bit smarter while finding
1286 process_envvars (enum mode
*modep
)
1290 enum mode mode
= normal
;
1291 char *debug_output
= NULL
;
1293 /* This is the default place for profiling data file. */
1294 _dl_profile_output
= __libc_enable_secure
? "/var/profile" : "/var/tmp";
1296 while ((envline
= _dl_next_ld_env_entry (&runp
)) != NULL
)
1298 size_t len
= strcspn (envline
, "=");
1300 if (envline
[len
] != '=')
1301 /* This is a "LD_" variable at the end of the string without
1302 a '=' character. Ignore it since otherwise we will access
1303 invalid memory below. */
1309 /* Warning level, verbose or not. */
1310 if (memcmp (&envline
[3], "WARN", 4) == 0)
1311 _dl_verbose
= envline
[8] != '\0';
1315 /* Debugging of the dynamic linker? */
1316 if (memcmp (&envline
[3], "DEBUG", 5) == 0)
1317 process_dl_debug (&envline
[9]);
1321 /* Print information about versions. */
1322 if (memcmp (&envline
[3], "VERBOSE", 7) == 0)
1324 version_info
= envline
[11] != '\0';
1328 /* List of objects to be preloaded. */
1329 if (memcmp (&envline
[3], "PRELOAD", 7) == 0)
1331 preloadlist
= &envline
[11];
1335 /* Which shared object shall be profiled. */
1336 if (memcmp (&envline
[3], "PROFILE", 7) == 0)
1337 _dl_profile
= &envline
[11];
1341 /* Do we bind early? */
1342 if (memcmp (&envline
[3], "BIND_NOW", 8) == 0)
1344 _dl_lazy
= envline
[12] == '\0';
1347 if (memcmp (&envline
[3], "BIND_NOT", 8) == 0)
1348 _dl_bind_not
= envline
[12] != '\0';
1352 /* Test whether we want to see the content of the auxiliary
1353 array passed up from the kernel. */
1354 if (memcmp (&envline
[3], "SHOW_AUXV", 9) == 0)
1359 /* Mask for the important hardware capabilities. */
1360 if (memcmp (&envline
[3], "HWCAP_MASK", 10) == 0)
1361 _dl_hwcap_mask
= __strtoul_internal (&envline
[14], NULL
, 0, 0);
1365 /* Path where the binary is found. */
1366 if (!__libc_enable_secure
1367 && memcmp (&envline
[3], "ORIGIN_PATH", 11) == 0)
1368 _dl_origin_path
= &envline
[15];
1372 /* The library search path. */
1373 if (memcmp (&envline
[3], "LIBRARY_PATH", 12) == 0)
1375 library_path
= &envline
[16];
1379 /* Where to place the profiling data file. */
1380 if (memcmp (&envline
[3], "DEBUG_OUTPUT", 12) == 0)
1382 debug_output
= &envline
[16];
1386 if (memcmp (&envline
[3], "DYNAMIC_WEAK", 12) == 0)
1387 _dl_dynamic_weak
= 1;
1391 /* Where to place the profiling data file. */
1392 if (!__libc_enable_secure
1393 && memcmp (&envline
[3], "PROFILE_OUTPUT", 14) == 0)
1395 _dl_profile_output
= &envline
[18];
1396 if (*_dl_profile_output
== '\0')
1397 _dl_profile_output
= "/var/tmp";
1402 /* The mode of the dynamic linker can be set. */
1403 if (memcmp (&envline
[3], "TRACE_LOADED_OBJECTS", 20) == 0)
1407 /* We might have some extra environment variable to handle. This
1408 is tricky due to the pre-processing of the length of the name
1409 in the switch statement here. The code here assumes that added
1410 environment variables have a different length. */
1411 #ifdef EXTRA_LD_ENVVARS
1417 /* Extra security for SUID binaries. Remove all dangerous environment
1419 if (__builtin_expect (__libc_enable_secure
, 0))
1421 static const char *unsecure_envvars
[] =
1424 #ifdef EXTRA_UNSECURE_ENVVARS
1425 EXTRA_UNSECURE_ENVVARS
1430 if (preloadlist
!= NULL
)
1431 unsetenv ("LD_PRELOAD");
1432 if (library_path
!= NULL
)
1433 unsetenv ("LD_LIBRARY_PATH");
1434 if (_dl_origin_path
!= NULL
)
1435 unsetenv ("LD_ORIGIN_PATH");
1436 if (debug_output
!= NULL
)
1437 unsetenv ("LD_DEBUG_OUTPUT");
1438 if (_dl_profile
!= NULL
)
1439 unsetenv ("LD_PROFILE");
1442 cnt
< sizeof (unsecure_envvars
) / sizeof (unsecure_envvars
[0]);
1444 unsetenv (unsecure_envvars
[cnt
]);
1446 if (__access ("/etc/suid-debug", F_OK
) != 0)
1447 unsetenv ("MALLOC_CHECK_");
1450 /* The name of the object to profile cannot be empty. */
1451 if (_dl_profile
!= NULL
&& *_dl_profile
== '\0')
1454 /* If we have to run the dynamic linker in debugging mode and the
1455 LD_DEBUG_OUTPUT environment variable is given, we write the debug
1456 messages to this file. */
1457 if (any_debug
&& debug_output
!= NULL
&& !__libc_enable_secure
)
1460 const int flags
= O_WRONLY
| O_APPEND
| O_CREAT
| O_NOFOLLOW
;
1462 const int flags
= O_WRONLY
| O_APPEND
| O_CREAT
;
1464 size_t name_len
= strlen (debug_output
);
1465 char buf
[name_len
+ 12];
1468 buf
[name_len
+ 11] = '\0';
1469 startp
= _itoa_word (__getpid (), &buf
[name_len
+ 11], 10, 0);
1471 startp
= memcpy (startp
- name_len
, debug_output
, name_len
);
1473 _dl_debug_fd
= __open (startp
, flags
, DEFFILEMODE
);
1474 if (_dl_debug_fd
== -1)
1475 /* We use standard output if opening the file failed. */
1476 _dl_debug_fd
= STDOUT_FILENO
;
1483 /* Print the various times we collected. */
1485 print_statistics (void)
1487 #ifndef HP_TIMING_NONAVAIL
1492 /* Total time rtld used. */
1493 if (HP_TIMING_AVAIL
)
1495 HP_TIMING_PRINT (buf
, sizeof (buf
), rtld_total_time
);
1496 _dl_debug_printf ("\nruntime linker statistics:\n"
1497 " total startup time in dynamic loader: %s\n", buf
);
1500 /* Print relocation statistics. */
1501 if (HP_TIMING_AVAIL
)
1504 HP_TIMING_PRINT (buf
, sizeof (buf
), relocate_time
);
1505 cp
= _itoa_word ((1000 * relocate_time
) / rtld_total_time
,
1506 pbuf
+ sizeof (pbuf
), 10, 0);
1508 switch (pbuf
+ sizeof (pbuf
) - cp
)
1519 _dl_debug_printf (" time needed for relocation: %s (%s)\n",
1523 _dl_debug_printf (" number of relocations: %lu\n",
1524 _dl_num_relocations
);
1526 #ifndef HP_TIMING_NONAVAIL
1527 /* Time spend while loading the object and the dependencies. */
1528 if (HP_TIMING_AVAIL
)
1531 HP_TIMING_PRINT (buf
, sizeof (buf
), load_time
);
1532 cp
= _itoa_word ((1000 * load_time
) / rtld_total_time
,
1533 pbuf
+ sizeof (pbuf
), 10, 0);
1535 switch (pbuf
+ sizeof (pbuf
) - cp
)
1546 _dl_debug_printf (" time needed to load objects: %s (%s)\n",