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"
33 #include <unsecvars.h>
37 /* System-specific function to do initial startup for the dynamic linker.
38 After this, file access calls and getenv must work. This is responsible
39 for setting __libc_enable_secure if we need to be secure (e.g. setuid),
40 and for setting _dl_argc and _dl_argv, and then calling _dl_main. */
41 extern ElfW(Addr
) _dl_sysdep_start (void **start_argptr
,
42 void (*dl_main
) (const ElfW(Phdr
) *phdr
,
44 ElfW(Addr
) *user_entry
));
45 extern void _dl_sysdep_start_cleanup (void);
47 /* This function is used to unload the cache file if necessary. */
48 extern void _dl_unload_cache (void);
50 /* System-dependent function to read a file's whole contents
51 in the most convenient manner available. */
52 extern void *_dl_sysdep_read_whole_file (const char *filename
,
56 /* Helper function to handle errors while resolving symbols. */
57 static void print_unresolved (int errcode
, const char *objname
,
58 const char *errsting
);
60 /* Helper function to handle errors when a version is missing. */
61 static void print_missing_version (int errcode
, const char *objname
,
62 const char *errsting
);
64 /* Print the various times we collected. */
65 static void print_statistics (void);
67 /* This is a list of all the modes the dynamic loader can be in. */
68 enum mode
{ normal
, list
, verify
, trace
};
70 /* Process all environments variables the dynamic linker must recognize.
71 Since all of them start with `LD_' we are a bit smarter while finding
73 static void process_envvars (enum mode
*modep
, int *lazyp
);
77 unsigned int _dl_skip_args
; /* Nonzero if we were run directly. */
79 const char *_dl_platform
;
80 size_t _dl_platformlen
;
81 unsigned long _dl_hwcap
;
82 fpu_control_t _dl_fpu_control
= _FPU_DEFAULT
;
83 struct r_search_path
*_dl_search_paths
;
84 const char *_dl_profile
;
85 const char *_dl_profile_output
;
86 struct link_map
*_dl_profile_map
;
88 /* XXX I know about at least one case where we depend on the old weak
89 behavior (it has to do with librt). Until we get DSO groups implemented
90 we have to make this the default. Bummer. --drepper */
94 int _dl_dynamic_weak
= 1;
97 int _dl_debug_impcalls
;
98 int _dl_debug_bindings
;
99 int _dl_debug_symbols
;
100 int _dl_debug_versions
;
103 int _dl_debug_statistics
;
104 const char *_dl_inhibit_rpath
; /* RPATH values which should be
106 const char *_dl_origin_path
;
109 /* This is a pointer to the map for the main object and through it to
110 all loaded objects. */
111 struct link_map
*_dl_loaded
;
112 /* Number of object in the _dl_loaded list. */
113 unsigned int _dl_nloaded
;
114 /* Pointer to the l_searchlist element of the link map of the main object. */
115 struct r_scope_elem
*_dl_main_searchlist
;
116 /* Copy of the content of `_dl_main_searchlist'. */
117 struct r_scope_elem _dl_initial_searchlist
;
118 /* Array which is used when looking up in the global scope. */
119 struct r_scope_elem
*_dl_global_scope
[2];
121 /* During the program run we must not modify the global data of
122 loaded shared object simultanously in two threads. Therefore we
123 protect `_dl_open' and `_dl_close' in dl-close.c.
125 This must be a recursive lock since the initializer function of
126 the loaded object might as well require a call to this function.
127 At this time it is not anymore a problem to modify the tables. */
128 __libc_lock_define_initialized_recursive (, _dl_load_lock
)
130 /* Set nonzero during loading and initialization of executable and
131 libraries, cleared before the executable's entry point runs. This
132 must not be initialized to nonzero, because the unused dynamic
133 linker loaded in for libc.so's "ld.so.1" dep will provide the
134 definition seen by libc.so's initializer; that value must be zero,
135 and will be since that dynamic linker's _dl_start and dl_main will
140 static void dl_main (const ElfW(Phdr
) *phdr
,
142 ElfW(Addr
) *user_entry
);
144 struct link_map _dl_rtld_map
;
145 struct libname_list _dl_rtld_libname
;
146 struct libname_list _dl_rtld_libname2
;
148 /* Variable for statistics. */
149 #ifndef HP_TIMING_NONAVAIL
150 static hp_timing_t rtld_total_time
;
151 static hp_timing_t relocate_time
;
152 static hp_timing_t load_time
;
154 extern unsigned long int _dl_num_relocations
; /* in dl-lookup.c */
156 static ElfW(Addr
) _dl_start_final (void *arg
, struct link_map
*bootstrap_map_p
,
157 hp_timing_t start_time
);
162 #error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
166 _dl_start (void *arg
)
168 struct link_map bootstrap_map
;
169 hp_timing_t start_time
;
172 /* This #define produces dynamic linking inline functions for
173 bootstrap relocation instead of general-purpose relocation. */
174 #define RTLD_BOOTSTRAP
175 #define RESOLVE_MAP(sym, version, flags) \
176 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : &bootstrap_map)
177 #define RESOLVE(sym, version, flags) \
178 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : bootstrap_map.l_addr)
179 #include "dynamic-link.h"
181 if (HP_TIMING_INLINE
&& HP_TIMING_AVAIL
)
182 HP_TIMING_NOW (start_time
);
184 /* Partly clean the `bootstrap_map' structure up. Don't use `memset'
185 since it might nor be built in or inlined and we cannot make function
186 calls at this point. */
188 cnt
< sizeof (bootstrap_map
.l_info
) / sizeof (bootstrap_map
.l_info
[0]);
190 bootstrap_map
.l_info
[cnt
] = 0;
192 /* Figure out the run-time load address of the dynamic linker itself. */
193 bootstrap_map
.l_addr
= elf_machine_load_address ();
195 /* Read our own dynamic section and fill in the info array. */
196 bootstrap_map
.l_ld
= (void *) bootstrap_map
.l_addr
+ elf_machine_dynamic ();
197 elf_get_dynamic_info (&bootstrap_map
);
199 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
200 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map
.l_info
);
203 /* Relocate ourselves so we can do normal function calls and
204 data access using the global offset table. */
206 ELF_DYNAMIC_RELOCATE (&bootstrap_map
, 0, 0);
207 /* Please note that we don't allow profiling of this object and
208 therefore need not test whether we have to allocate the array
209 for the relocation results (as done in dl-reloc.c). */
211 /* Now life is sane; we can call functions and access global data.
212 Set up to use the operating system facilities, and find out from
213 the operating system's program loader where to find the program
214 header table in core. Put the rest of _dl_start into a separate
215 function, that way the compiler cannot put accesses to the GOT
216 before ELF_DYNAMIC_RELOCATE. */
218 ElfW(Addr
) entry
= _dl_start_final (arg
, &bootstrap_map
, start_time
);
220 #ifndef ELF_MACHINE_START_ADDRESS
221 # define ELF_MACHINE_START_ADDRESS(map, start) (start)
224 return ELF_MACHINE_START_ADDRESS (_dl_loaded
, entry
);
230 _dl_start_final (void *arg
, struct link_map
*bootstrap_map_p
,
231 hp_timing_t start_time
)
233 /* The use of `alloca' here looks ridiculous but it helps. The goal
234 is to avoid the function from being inlined. There is no official
235 way to do this so we use this trick. gcc never inlines functions
236 which use `alloca'. */
237 ElfW(Addr
) *start_addr
= alloca (sizeof (ElfW(Addr
)));
241 /* If it hasn't happen yet record the startup time. */
242 if (! HP_TIMING_INLINE
)
243 HP_TIMING_NOW (start_time
);
245 /* Initialize the timing functions. */
246 HP_TIMING_DIFF_INIT ();
249 /* Transfer data about ourselves to the permanent link_map structure. */
250 _dl_rtld_map
.l_addr
= bootstrap_map_p
->l_addr
;
251 _dl_rtld_map
.l_ld
= bootstrap_map_p
->l_ld
;
252 _dl_rtld_map
.l_opencount
= 1;
253 memcpy (_dl_rtld_map
.l_info
, bootstrap_map_p
->l_info
,
254 sizeof _dl_rtld_map
.l_info
);
255 _dl_setup_hash (&_dl_rtld_map
);
257 /* Don't bother trying to work out how ld.so is mapped in memory. */
258 _dl_rtld_map
.l_map_start
= ~0;
259 _dl_rtld_map
.l_map_end
= ~0;
261 /* Call the OS-dependent function to set up life so we can do things like
262 file access. It will call `dl_main' (below) to do all the real work
263 of the dynamic linker, and then unwind our frame and run the user
264 entry point on the same stack we entered on. */
265 *start_addr
= _dl_sysdep_start (arg
, &dl_main
);
266 #ifndef HP_TIMING_NONAVAIL
269 hp_timing_t end_time
;
271 /* Get the current time. */
272 HP_TIMING_NOW (end_time
);
274 /* Compute the difference. */
275 HP_TIMING_DIFF (rtld_total_time
, start_time
, end_time
);
279 if (__builtin_expect (_dl_debug_statistics
, 0))
285 /* Now life is peachy; we can do all normal operations.
286 On to the real work. */
288 void ENTRY_POINT (void);
290 /* Some helper functions. */
292 /* Arguments to relocate_doit. */
301 /* Argument to map_doit. */
303 /* Return value of map_doit. */
304 struct link_map
*main_map
;
307 /* Arguments to version_check_doit. */
308 struct version_check_args
315 relocate_doit (void *a
)
317 struct relocate_args
*args
= (struct relocate_args
*) a
;
319 _dl_relocate_object (args
->l
, args
->l
->l_scope
,
326 struct map_args
*args
= (struct map_args
*) a
;
327 args
->main_map
= _dl_map_object (NULL
, args
->str
, 0, lt_library
, 0, 0);
331 version_check_doit (void *a
)
333 struct version_check_args
*args
= (struct version_check_args
*) a
;
334 if (_dl_check_all_versions (_dl_loaded
, 1, args
->dotrace
) && args
->doexit
)
335 /* We cannot start the application. Abort now. */
340 static inline struct link_map
*
341 find_needed (const char *name
)
343 unsigned int n
= _dl_loaded
->l_searchlist
.r_nlist
;
346 if (_dl_name_match_p (name
, _dl_loaded
->l_searchlist
.r_list
[n
]))
347 return _dl_loaded
->l_searchlist
.r_list
[n
];
349 /* Should never happen. */
354 match_version (const char *string
, struct link_map
*map
)
356 const char *strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
359 #define VERDEFTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
360 if (map
->l_info
[VERDEFTAG
] == NULL
)
361 /* The file has no symbol versioning. */
364 def
= (ElfW(Verdef
) *) ((char *) map
->l_addr
365 + map
->l_info
[VERDEFTAG
]->d_un
.d_ptr
);
368 ElfW(Verdaux
) *aux
= (ElfW(Verdaux
) *) ((char *) def
+ def
->vd_aux
);
370 /* Compare the version strings. */
371 if (strcmp (string
, strtab
+ aux
->vda_name
) == 0)
375 /* If no more definitions we failed to find what we want. */
376 if (def
->vd_next
== 0)
379 /* Next definition. */
380 def
= (ElfW(Verdef
) *) ((char *) def
+ def
->vd_next
);
386 static const char *library_path
; /* The library search path. */
387 static const char *preloadlist
; /* The list preloaded objects. */
388 static int version_info
; /* Nonzero if information about
389 versions has to be printed. */
392 dl_main (const ElfW(Phdr
) *phdr
,
394 ElfW(Addr
) *user_entry
)
396 const ElfW(Phdr
) *ph
;
398 struct link_map
**preloads
;
399 unsigned int npreloads
;
404 int rtld_is_main
= 0;
405 #ifndef HP_TIMING_NONAVAIL
411 /* Process the environment variable which control the behaviour. */
412 process_envvars (&mode
, &_dl_lazy
);
414 /* Set up a flag which tells we are just starting. */
417 if (*user_entry
== (ElfW(Addr
)) &ENTRY_POINT
)
419 /* Ho ho. We are not the program interpreter! We are the program
420 itself! This means someone ran ld.so as a command. Well, that
421 might be convenient to do sometimes. We support it by
422 interpreting the args like this:
424 ld.so PROGRAM ARGS...
426 The first argument is the name of a file containing an ELF
427 executable we will load and run with the following arguments.
428 To simplify life here, PROGRAM is searched for using the
429 normal rules for shared objects, rather than $PATH or anything
430 like that. We just load it and use its entry point; we don't
431 pay attention to its PT_INTERP command (we are the interpreter
432 ourselves). This is an easy way to test a new ld.so before
436 /* Note the place where the dynamic linker actually came from. */
437 _dl_rtld_map
.l_name
= _dl_argv
[0];
440 if (! strcmp (_dl_argv
[1], "--list"))
443 _dl_lazy
= -1; /* This means do no dependency analysis. */
449 else if (! strcmp (_dl_argv
[1], "--verify"))
457 else if (! strcmp (_dl_argv
[1], "--library-path") && _dl_argc
> 2)
459 library_path
= _dl_argv
[2];
465 else if (! strcmp (_dl_argv
[1], "--inhibit-rpath") && _dl_argc
> 2)
467 _dl_inhibit_rpath
= _dl_argv
[2];
476 /* If we have no further argument the program was called incorrectly.
477 Grant the user some education. */
480 Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
481 You have invoked `ld.so', the helper program for shared library executables.\n\
482 This program usually lives in the file `/lib/ld.so', and special directives\n\
483 in executable files using ELF shared libraries tell the system's program\n\
484 loader to load the helper program from this file. This helper program loads\n\
485 the shared libraries needed by the program executable, prepares the program\n\
486 to run, and runs it. You may invoke this helper program directly from the\n\
487 command line to load and run an ELF executable file; this is like executing\n\
488 that file itself, but always uses this helper program from the file you\n\
489 specified, instead of the helper program file specified in the executable\n\
490 file you run. This is mostly of use for maintainers to test new versions\n\
491 of this helper program; chances are you did not intend to run this program.\n\
493 --list list all dependencies and how they are resolved\n\
494 --verify verify that given object really is a dynamically linked\n\
495 object we can handle\n\
496 --library-path PATH use given PATH instead of content of the environment\n\
497 variable LD_LIBRARY_PATH\n\
498 --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\
506 /* Initialize the data structures for the search paths for shared
508 _dl_init_paths (library_path
);
510 if (__builtin_expect (mode
, normal
) == verify
)
513 const char *err_str
= NULL
;
514 struct map_args args
;
516 args
.str
= _dl_argv
[0];
517 (void) _dl_catch_error (&objname
, &err_str
, map_doit
, &args
);
518 if (__builtin_expect (err_str
!= NULL
, 0))
520 if (err_str
!= _dl_out_of_memory
)
521 free ((char *) err_str
);
522 _exit (EXIT_FAILURE
);
527 HP_TIMING_NOW (start
);
528 _dl_map_object (NULL
, _dl_argv
[0], 0, lt_library
, 0, 0);
529 HP_TIMING_NOW (stop
);
531 HP_TIMING_DIFF (load_time
, start
, stop
);
534 phdr
= _dl_loaded
->l_phdr
;
535 phent
= _dl_loaded
->l_phnum
;
536 /* We overwrite here a pointer to a malloc()ed string. But since
537 the malloc() implementation used at this point is the dummy
538 implementations which has no real free() function it does not
539 makes sense to free the old string first. */
540 _dl_loaded
->l_name
= (char *) "";
541 *user_entry
= _dl_loaded
->l_entry
;
545 /* Create a link_map for the executable itself.
546 This will be what dlopen on "" returns. */
547 _dl_new_object ((char *) "", "", lt_executable
, NULL
);
548 if (_dl_loaded
== NULL
)
549 _dl_sysdep_fatal ("cannot allocate memory for link map\n", NULL
);
550 _dl_loaded
->l_phdr
= phdr
;
551 _dl_loaded
->l_phnum
= phent
;
552 _dl_loaded
->l_entry
= *user_entry
;
553 _dl_loaded
->l_opencount
= 1;
555 /* At this point we are in a bit of trouble. We would have to
556 fill in the values for l_dev and l_ino. But in general we
557 do not know where the file is. We also do not handle AT_EXECFD
558 even if it would be passed up.
560 We leave the values here defined to 0. This is normally no
561 problem as the program code itself is normally no shared
562 object and therefore cannot be loaded dynamically. Nothing
563 prevent the use of dynamic binaries and in these situations
564 we might get problems. We might not be able to find out
565 whether the object is already loaded. But since there is no
566 easy way out and because the dynamic binary must also not
567 have an SONAME we ignore this program for now. If it becomes
568 a problem we can force people using SONAMEs. */
570 /* We delay initializing the path structure until we got the dynamic
571 information for the program. */
574 /* It is not safe to load stuff after the main program. */
575 _dl_loaded
->l_map_end
= ~0;
576 /* Perhaps the executable has no PT_LOAD header entries at all. */
577 _dl_loaded
->l_map_start
= ~0;
579 /* Scan the program header table for the dynamic section. */
580 for (ph
= phdr
; ph
< &phdr
[phent
]; ++ph
)
584 /* Find out the load address. */
585 _dl_loaded
->l_addr
= (ElfW(Addr
)) phdr
- ph
->p_vaddr
;
588 /* This tells us where to find the dynamic section,
589 which tells us everything we need to do. */
590 _dl_loaded
->l_ld
= (void *) _dl_loaded
->l_addr
+ ph
->p_vaddr
;
593 /* This "interpreter segment" was used by the program loader to
594 find the program interpreter, which is this program itself, the
595 dynamic linker. We note what name finds us, so that a future
596 dlopen call or DT_NEEDED entry, for something that wants to link
597 against the dynamic linker as a shared library, will know that
598 the shared object is already loaded. */
599 _dl_rtld_libname
.name
= ((const char *) _dl_loaded
->l_addr
601 /* _dl_rtld_libname.next = NULL; Already zero. */
602 _dl_rtld_map
.l_libname
= &_dl_rtld_libname
;
604 /* Ordinarilly, we would get additional names for the loader from
605 our DT_SONAME. This can't happen if we were actually linked as
606 a static executable (detect this case when we have no DYNAMIC).
607 If so, assume the filename component of the interpreter path to
608 be our SONAME, and add it to our name list. */
609 if (_dl_rtld_map
.l_ld
== NULL
)
611 char *p
= strrchr (_dl_rtld_libname
.name
, '/');
614 _dl_rtld_libname2
.name
= p
+1;
615 /* _dl_rtld_libname2.next = NULL; Already zero. */
616 _dl_rtld_libname
.next
= &_dl_rtld_libname2
;
623 /* Remember where the main program starts in memory. */
626 mapstart
= _dl_loaded
->l_addr
+ (ph
->p_vaddr
& ~(ph
->p_align
- 1));
627 if (_dl_loaded
->l_map_start
> mapstart
)
628 _dl_loaded
->l_map_start
= mapstart
;
632 if (! _dl_rtld_map
.l_libname
&& _dl_rtld_map
.l_name
)
634 /* We were invoked directly, so the program might not have a
636 _dl_rtld_libname
.name
= _dl_rtld_map
.l_name
;
637 /* _dl_rtld_libname.next = NULL; Alread zero. */
638 _dl_rtld_map
.l_libname
= &_dl_rtld_libname
;
641 assert (_dl_rtld_map
.l_libname
); /* How else did we get here? */
645 /* Extract the contents of the dynamic section for easy access. */
646 elf_get_dynamic_info (_dl_loaded
);
647 if (_dl_loaded
->l_info
[DT_HASH
])
648 /* Set up our cache of pointers into the hash table. */
649 _dl_setup_hash (_dl_loaded
);
652 if (__builtin_expect (mode
, normal
) == verify
)
654 /* We were called just to verify that this is a dynamic
655 executable using us as the program interpreter. Exit with an
656 error if we were not able to load the binary or no interpreter
657 is specified (i.e., this is no dynamically linked binary. */
658 if (_dl_loaded
->l_ld
== NULL
)
661 /* We allow here some platform specific code. */
662 #ifdef DISTINGUISH_LIB_VERSIONS
663 DISTINGUISH_LIB_VERSIONS
;
665 _exit (has_interp
? 0 : 2);
669 /* Initialize the data structures for the search paths for shared
671 _dl_init_paths (library_path
);
673 /* Put the link_map for ourselves on the chain so it can be found by
674 name. Note that at this point the global chain of link maps contains
675 exactly one element, which is pointed to by _dl_loaded. */
676 if (! _dl_rtld_map
.l_name
)
677 /* If not invoked directly, the dynamic linker shared object file was
678 found by the PT_INTERP name. */
679 _dl_rtld_map
.l_name
= (char *) _dl_rtld_map
.l_libname
->name
;
680 _dl_rtld_map
.l_type
= lt_library
;
681 _dl_loaded
->l_next
= &_dl_rtld_map
;
682 _dl_rtld_map
.l_prev
= _dl_loaded
;
685 /* We have two ways to specify objects to preload: via environment
686 variable and via the file /etc/ld.so.preload. The latter can also
687 be used when security is enabled. */
691 if (__builtin_expect (preloadlist
!= NULL
, 0))
693 /* The LD_PRELOAD environment variable gives list of libraries
694 separated by white space or colons that are loaded before the
695 executable's dependencies and prepended to the global scope
696 list. If the binary is running setuid all elements
697 containing a '/' are ignored since it is insecure. */
698 char *list
= strdupa (preloadlist
);
701 HP_TIMING_NOW (start
);
703 while ((p
= strsep (&list
, " :")) != NULL
)
705 && (__builtin_expect (! __libc_enable_secure
, 1)
706 || strchr (p
, '/') == NULL
))
708 struct link_map
*new_map
= _dl_map_object (_dl_loaded
, p
, 1,
710 if (new_map
->l_opencount
== 1)
711 /* It is no duplicate. */
715 HP_TIMING_NOW (stop
);
716 HP_TIMING_DIFF (diff
, start
, stop
);
717 HP_TIMING_ACCUM_NT (load_time
, diff
);
720 /* Read the contents of the file. */
721 file
= _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size
,
722 PROT_READ
| PROT_WRITE
);
723 if (__builtin_expect (file
!= NULL
, 0))
725 /* Parse the file. It contains names of libraries to be loaded,
726 separated by white spaces or `:'. It may also contain
727 comments introduced by `#'. */
732 /* Eliminate comments. */
737 char *comment
= memchr (runp
, '#', rest
);
741 rest
-= comment
- runp
;
744 while (--rest
> 0 && *++comment
!= '\n');
747 /* We have one problematic case: if we have a name at the end of
748 the file without a trailing terminating characters, we cannot
749 place the \0. Handle the case separately. */
750 if (file
[file_size
- 1] != ' ' && file
[file_size
- 1] != '\t'
751 && file
[file_size
- 1] != '\n' && file
[file_size
- 1] != ':')
753 problem
= &file
[file_size
];
754 while (problem
> file
&& problem
[-1] != ' ' && problem
[-1] != '\t'
755 && problem
[-1] != '\n' && problem
[-1] != ':')
764 file
[file_size
- 1] = '\0';
767 HP_TIMING_NOW (start
);
773 while ((p
= strsep (&runp
, ": \t\n")) != NULL
)
776 struct link_map
*new_map
= _dl_map_object (_dl_loaded
, p
, 1,
778 if (new_map
->l_opencount
== 1)
779 /* It is no duplicate. */
786 char *p
= strndupa (problem
, file_size
- (problem
- file
));
787 struct link_map
*new_map
= _dl_map_object (_dl_loaded
, p
, 1,
789 if (new_map
->l_opencount
== 1)
790 /* It is no duplicate. */
794 HP_TIMING_NOW (stop
);
795 HP_TIMING_DIFF (diff
, start
, stop
);
796 HP_TIMING_ACCUM_NT (load_time
, diff
);
798 /* We don't need the file anymore. */
799 __munmap (file
, file_size
);
802 if (__builtin_expect (npreloads
, 0) != 0)
804 /* Set up PRELOADS with a vector of the preloaded libraries. */
806 preloads
= __alloca (npreloads
* sizeof preloads
[0]);
807 l
= _dl_rtld_map
.l_next
; /* End of the chain before preloads. */
814 assert (i
== npreloads
);
817 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
818 specified some libraries to load, these are inserted before the actual
819 dependencies in the executable's searchlist for symbol resolution. */
820 HP_TIMING_NOW (start
);
821 _dl_map_object_deps (_dl_loaded
, preloads
, npreloads
, mode
== trace
);
822 HP_TIMING_NOW (stop
);
823 HP_TIMING_DIFF (diff
, start
, stop
);
824 HP_TIMING_ACCUM_NT (load_time
, diff
);
826 /* Mark all objects as being in the global scope. */
827 for (i
= _dl_loaded
->l_searchlist
.r_nlist
; i
> 0; )
828 _dl_loaded
->l_searchlist
.r_list
[--i
]->l_global
= 1;
831 /* We are done mapping things, so close the zero-fill descriptor. */
832 __close (_dl_zerofd
);
836 /* Remove _dl_rtld_map from the chain. */
837 _dl_rtld_map
.l_prev
->l_next
= _dl_rtld_map
.l_next
;
838 if (_dl_rtld_map
.l_next
)
839 _dl_rtld_map
.l_next
->l_prev
= _dl_rtld_map
.l_prev
;
841 if (__builtin_expect (_dl_rtld_map
.l_opencount
, 2) > 1)
843 /* Some DT_NEEDED entry referred to the interpreter object itself, so
844 put it back in the list of visible objects. We insert it into the
845 chain in symbol search order because gdb uses the chain's order as
846 its symbol search order. */
848 while (_dl_loaded
->l_searchlist
.r_list
[i
] != &_dl_rtld_map
)
850 _dl_rtld_map
.l_prev
= _dl_loaded
->l_searchlist
.r_list
[i
- 1];
851 if (__builtin_expect (mode
, normal
) == normal
)
852 _dl_rtld_map
.l_next
= (i
+ 1 < _dl_loaded
->l_searchlist
.r_nlist
853 ? _dl_loaded
->l_searchlist
.r_list
[i
+ 1]
856 /* In trace mode there might be an invisible object (which we
857 could not find) after the previous one in the search list.
858 In this case it doesn't matter much where we put the
859 interpreter object, so we just initialize the list pointer so
860 that the assertion below holds. */
861 _dl_rtld_map
.l_next
= _dl_rtld_map
.l_prev
->l_next
;
863 assert (_dl_rtld_map
.l_prev
->l_next
== _dl_rtld_map
.l_next
);
864 _dl_rtld_map
.l_prev
->l_next
= &_dl_rtld_map
;
865 if (_dl_rtld_map
.l_next
)
867 assert (_dl_rtld_map
.l_next
->l_prev
== _dl_rtld_map
.l_prev
);
868 _dl_rtld_map
.l_next
->l_prev
= &_dl_rtld_map
;
872 /* Now let us see whether all libraries are available in the
875 struct version_check_args args
;
876 args
.doexit
= mode
== normal
;
877 args
.dotrace
= mode
== trace
;
878 _dl_receive_error (print_missing_version
, version_check_doit
, &args
);
881 if (__builtin_expect (mode
, normal
) != normal
)
883 /* We were run just to list the shared libraries. It is
884 important that we do this before real relocation, because the
885 functions we call below for output may no longer work properly
887 if (! _dl_loaded
->l_info
[DT_NEEDED
])
888 _dl_sysdep_message ("\t", "statically linked\n", NULL
);
893 for (l
= _dl_loaded
->l_next
; l
; l
= l
->l_next
)
894 if (l
->l_opencount
== 0)
895 /* The library was not found. */
896 _dl_sysdep_message ("\t", l
->l_libname
->name
, " => not found\n",
901 buf
[sizeof buf
- 1] = '\0';
902 bp
= _itoa_word (l
->l_addr
, &buf
[sizeof buf
- 1], 16, 0);
903 while ((size_t) (&buf
[sizeof buf
- 1] - bp
)
904 < sizeof l
->l_addr
* 2)
906 _dl_sysdep_message ("\t", l
->l_libname
->name
, " => ",
907 l
->l_name
, " (0x", bp
, ")\n", NULL
);
911 if (__builtin_expect (mode
, trace
) != trace
)
912 for (i
= 1; i
< _dl_argc
; ++i
)
914 const ElfW(Sym
) *ref
= NULL
;
919 result
= _dl_lookup_symbol (_dl_argv
[i
], _dl_loaded
,
920 &ref
, _dl_loaded
->l_scope
,
921 ELF_MACHINE_JMP_SLOT
, 1);
923 loadbase
= LOOKUP_VALUE_ADDRESS (result
);
925 buf
[sizeof buf
- 1] = '\0';
926 bp
= _itoa_word (ref
->st_value
, &buf
[sizeof buf
- 1], 16, 0);
927 while ((size_t) (&buf
[sizeof buf
- 1] - bp
) < sizeof loadbase
* 2)
929 _dl_sysdep_message (_dl_argv
[i
], " found at 0x", bp
, NULL
);
930 buf
[sizeof buf
- 1] = '\0';
931 bp
= _itoa_word (loadbase
, &buf
[sizeof buf
- 1], 16, 0);
932 while ((size_t) (&buf
[sizeof buf
- 1] - bp
) < sizeof loadbase
* 2)
934 _dl_sysdep_message (" in object at 0x", bp
, "\n", NULL
);
940 /* We have to do symbol dependency testing. */
941 struct relocate_args args
;
944 args
.lazy
= _dl_lazy
;
951 if (l
!= &_dl_rtld_map
&& l
->l_opencount
> 0)
954 _dl_receive_error (print_unresolved
, relocate_doit
,
961 #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
964 /* Print more information. This means here, print information
965 about the versions needed. */
967 struct link_map
*map
= _dl_loaded
;
969 for (map
= _dl_loaded
; map
!= NULL
; map
= map
->l_next
)
972 ElfW(Dyn
) *dyn
= map
->l_info
[VERNEEDTAG
];
978 strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
979 ent
= (ElfW(Verneed
) *) (map
->l_addr
+ dyn
->d_un
.d_ptr
);
983 _dl_sysdep_message ("\n\tVersion information:\n", NULL
);
987 _dl_sysdep_message ("\t", (map
->l_name
[0]
988 ? map
->l_name
: _dl_argv
[0]),
994 struct link_map
*needed
;
996 needed
= find_needed (strtab
+ ent
->vn_file
);
997 aux
= (ElfW(Vernaux
) *) ((char *) ent
+ ent
->vn_aux
);
1001 const char *fname
= NULL
;
1003 _dl_sysdep_message ("\t\t",
1004 strtab
+ ent
->vn_file
,
1005 " (", strtab
+ aux
->vna_name
,
1013 && match_version (strtab
+aux
->vna_name
, needed
))
1014 fname
= needed
->l_name
;
1016 _dl_sysdep_message (fname
?: "not found", "\n",
1019 if (aux
->vna_next
== 0)
1020 /* No more symbols. */
1024 aux
= (ElfW(Vernaux
) *) ((char *) aux
1028 if (ent
->vn_next
== 0)
1029 /* No more dependencies. */
1032 /* Next dependency. */
1033 ent
= (ElfW(Verneed
) *) ((char *) ent
+ ent
->vn_next
);
1043 /* Now we have all the objects loaded. Relocate them all except for
1044 the dynamic linker itself. We do this in reverse order so that copy
1045 relocs of earlier objects overwrite the data written by later
1046 objects. We do not re-relocate the dynamic linker itself in this
1047 loop because that could result in the GOT entries for functions we
1048 call being changed, and that would break us. It is safe to relocate
1049 the dynamic linker out of order because it has no copy relocs (we
1050 know that because it is self-contained). */
1053 int consider_profiling
= _dl_profile
!= NULL
;
1054 #ifndef HP_TIMING_NONAVAIL
1060 /* If we are profiling we also must do lazy reloaction. */
1061 _dl_lazy
|= consider_profiling
;
1067 HP_TIMING_NOW (start
);
1070 /* While we are at it, help the memory handling a bit. We have to
1071 mark some data structures as allocated with the fake malloc()
1072 implementation in ld.so. */
1073 struct libname_list
*lnp
= l
->l_libname
->next
;
1075 while (__builtin_expect (lnp
!= NULL
, 0))
1081 if (l
!= &_dl_rtld_map
)
1082 _dl_relocate_object (l
, l
->l_scope
, _dl_lazy
, consider_profiling
);
1087 HP_TIMING_NOW (stop
);
1089 HP_TIMING_DIFF (relocate_time
, start
, stop
);
1091 /* Do any necessary cleanups for the startup OS interface code.
1092 We do these now so that no calls are made after rtld re-relocation
1093 which might be resolved to different functions than we expect.
1094 We cannot do this before relocating the other objects because
1095 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
1096 _dl_sysdep_start_cleanup ();
1098 /* Now enable profiling if needed. Like the previous call,
1099 this has to go here because the calls it makes should use the
1100 rtld versions of the functions (particularly calloc()), but it
1101 needs to have _dl_profile_map set up by the relocator. */
1102 if (__builtin_expect (_dl_profile_map
!= NULL
, 0))
1103 /* We must prepare the profiling. */
1104 _dl_start_profile (_dl_profile_map
, _dl_profile_output
);
1106 if (_dl_rtld_map
.l_opencount
> 1)
1108 /* There was an explicit ref to the dynamic linker as a shared lib.
1109 Re-relocate ourselves with user-controlled symbol definitions. */
1110 HP_TIMING_NOW (start
);
1111 _dl_relocate_object (&_dl_rtld_map
, _dl_loaded
->l_scope
, 0, 0);
1112 HP_TIMING_NOW (stop
);
1113 HP_TIMING_DIFF (add
, start
, stop
);
1114 HP_TIMING_ACCUM_NT (relocate_time
, add
);
1118 /* Now set up the variable which helps the assembler startup code. */
1119 _dl_main_searchlist
= &_dl_loaded
->l_searchlist
;
1120 _dl_global_scope
[0] = &_dl_loaded
->l_searchlist
;
1122 /* Safe the information about the original global scope list since
1123 we need it in the memory handling later. */
1124 _dl_initial_searchlist
= *_dl_main_searchlist
;
1127 /* Initialize _r_debug. */
1128 struct r_debug
*r
= _dl_debug_initialize (_dl_rtld_map
.l_addr
);
1133 #ifdef ELF_MACHINE_DEBUG_SETUP
1135 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
1137 ELF_MACHINE_DEBUG_SETUP (l
, r
);
1138 ELF_MACHINE_DEBUG_SETUP (&_dl_rtld_map
, r
);
1142 if (l
->l_info
[DT_DEBUG
])
1143 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
1144 with the run-time address of the r_debug structure */
1145 l
->l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
1147 /* Fill in the pointer in the dynamic linker's own dynamic section, in
1148 case you run gdb on the dynamic linker directly. */
1149 if (_dl_rtld_map
.l_info
[DT_DEBUG
])
1150 _dl_rtld_map
.l_info
[DT_DEBUG
]->d_un
.d_ptr
= (ElfW(Addr
)) r
;
1154 /* Notify the debugger that all objects are now mapped in. */
1155 r
->r_state
= RT_ADD
;
1160 /* We must munmap() the cache file. */
1161 _dl_unload_cache ();
1164 /* Once we return, _dl_sysdep_start will invoke
1165 the DT_INIT functions and then *USER_ENTRY. */
1168 /* This is a little helper function for resolving symbols while
1169 tracing the binary. */
1171 print_unresolved (int errcode
__attribute__ ((unused
)), const char *objname
,
1172 const char *errstring
)
1174 if (objname
[0] == '\0')
1175 objname
= _dl_argv
[0] ?: "<main program>";
1176 _dl_sysdep_error (errstring
, " (", objname
, ")\n", NULL
);
1179 /* This is a little helper function for resolving symbols while
1180 tracing the binary. */
1182 print_missing_version (int errcode
__attribute__ ((unused
)),
1183 const char *objname
, const char *errstring
)
1185 _dl_sysdep_error (_dl_argv
[0] ?: "<program name unknown>", ": ",
1186 objname
, ": ", errstring
, "\n", NULL
);
1189 /* Nonzero if any of the debugging options is enabled. */
1190 static int any_debug
;
1192 /* Process the string given as the parameter which explains which debugging
1193 options are enabled. */
1195 process_dl_debug (const char *dl_debug
)
1198 #define separators " ,:"
1202 /* Skip separating white spaces and commas. */
1203 dl_debug
+= strspn (dl_debug
, separators
);
1204 if (*dl_debug
!= '\0')
1206 len
= strcspn (dl_debug
, separators
);
1211 /* This option is not documented since it is not generally
1213 if (memcmp (dl_debug
, "all", 3) == 0)
1216 _dl_debug_impcalls
= 1;
1217 _dl_debug_reloc
= 1;
1218 _dl_debug_files
= 1;
1219 _dl_debug_symbols
= 1;
1220 _dl_debug_bindings
= 1;
1221 _dl_debug_versions
= 1;
1228 if (memcmp (dl_debug
, "help", 4) == 0)
1230 _dl_sysdep_message ("\
1231 Valid options for the LD_DEBUG environment variable are:\n\
1233 bindings display information about symbol binding\n\
1234 files display processing of files and libraries\n\
1235 help display this help message and exit\n\
1236 libs display library search paths\n\
1237 reloc display relocation processing\n\
1238 statistics display relocation statistics\n\
1239 symbols display symbol table processing\n\
1240 versions display version dependencies\n\
1242 To direct the debugging output into a file instead of standard output\n\
1243 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n",
1248 if (memcmp (dl_debug
, "libs", 4) == 0)
1251 _dl_debug_impcalls
= 1;
1258 if (memcmp (dl_debug
, "reloc", 5) == 0)
1260 _dl_debug_reloc
= 1;
1261 _dl_debug_impcalls
= 1;
1266 if (memcmp (dl_debug
, "files", 5) == 0)
1268 _dl_debug_files
= 1;
1269 _dl_debug_impcalls
= 1;
1276 if (memcmp (dl_debug
, "symbols", 7) == 0)
1278 _dl_debug_symbols
= 1;
1279 _dl_debug_impcalls
= 1;
1286 if (memcmp (dl_debug
, "bindings", 8) == 0)
1288 _dl_debug_bindings
= 1;
1289 _dl_debug_impcalls
= 1;
1294 if (memcmp (dl_debug
, "versions", 8) == 0)
1296 _dl_debug_versions
= 1;
1297 _dl_debug_impcalls
= 1;
1304 if (memcmp (dl_debug
, "statistics", 10) == 0)
1306 _dl_debug_statistics
= 1;
1316 /* Display a warning and skip everything until next separator. */
1317 char *startp
= strndupa (dl_debug
, len
);
1318 _dl_sysdep_error ("warning: debug option `", startp
,
1319 "' unknown; try LD_DEBUG=help\n", NULL
);
1324 while (*(dl_debug
+= len
) != '\0');
1327 /* Process all environments variables the dynamic linker must recognize.
1328 Since all of them start with `LD_' we are a bit smarter while finding
1331 process_envvars (enum mode
*modep
, int *lazyp
)
1335 enum mode mode
= normal
;
1337 char *debug_output
= NULL
;
1339 /* This is the default place for profiling data file. */
1340 _dl_profile_output
= "/var/tmp";
1342 while ((envline
= _dl_next_ld_env_entry (&runp
)) != NULL
)
1344 size_t len
= strcspn (envline
, "=");
1346 if (envline
[len
] != '=')
1347 /* This is a "LD_" variable at the end of the string without
1348 a '=' character. Ignore it since otherwise we will access
1349 invalid memory below. */
1355 /* Warning level, verbose or not. */
1356 if (memcmp (&envline
[3], "WARN", 4) == 0)
1357 _dl_verbose
= envline
[8] != '\0';
1361 /* Debugging of the dynamic linker? */
1362 if (memcmp (&envline
[3], "DEBUG", 5) == 0)
1363 process_dl_debug (&envline
[9]);
1367 /* Print information about versions. */
1368 if (memcmp (&envline
[3], "VERBOSE", 7) == 0)
1370 version_info
= envline
[11] != '\0';
1374 /* List of objects to be preloaded. */
1375 if (memcmp (&envline
[3], "PRELOAD", 7) == 0)
1377 preloadlist
= &envline
[11];
1381 /* Which shared object shall be profiled. */
1382 if (memcmp (&envline
[3], "PROFILE", 7) == 0)
1383 _dl_profile
= &envline
[11];
1387 /* Do we bind early? */
1388 if (memcmp (&envline
[3], "BIND_NOW", 8) == 0)
1390 bind_now
= envline
[12] != '\0';
1393 if (memcmp (&envline
[3], "BIND_NOT", 8) == 0)
1394 _dl_bind_not
= envline
[12] != '\0';
1398 /* Test whether we want to see the content of the auxiliary
1399 array passed up from the kernel. */
1400 if (memcmp (&envline
[3], "SHOW_AUXV", 9) == 0)
1405 /* Mask for the important hardware capabilities. */
1406 if (memcmp (&envline
[3], "HWCAP_MASK", 10) == 0)
1407 _dl_hwcap_mask
= strtoul (&envline
[14], NULL
, 0);
1411 /* Path where the binary is found. */
1412 if (!__libc_enable_secure
1413 && memcmp (&envline
[3], "ORIGIN_PATH", 11) == 0)
1414 _dl_origin_path
= &envline
[15];
1418 /* The library search path. */
1419 if (memcmp (&envline
[3], "LIBRARY_PATH", 12) == 0)
1421 library_path
= &envline
[16];
1425 /* Where to place the profiling data file. */
1426 if (memcmp (&envline
[3], "DEBUG_OUTPUT", 12) == 0)
1428 debug_output
= &envline
[16];
1432 if (memcmp (&envline
[3], "DYNAMIC_WEAK", 12) == 0)
1433 _dl_dynamic_weak
= 1;
1437 /* Where to place the profiling data file. */
1438 if (!__libc_enable_secure
1439 && memcmp (&envline
[3], "PROFILE_OUTPUT", 14) == 0)
1441 _dl_profile_output
= &envline
[18];
1442 if (*_dl_profile_output
== '\0')
1443 _dl_profile_output
= "/var/tmp";
1448 /* The mode of the dynamic linker can be set. */
1449 if (memcmp (&envline
[3], "TRACE_LOADED_OBJECTS", 20) == 0)
1453 /* We might have some extra environment variable to handle. This
1454 is tricky due to the pre-processing of the length of the name
1455 in the switch statement here. The code here assumes that added
1456 environment variables have a different length. */
1457 #ifdef EXTRA_LD_ENVVARS
1463 /* Extra security for SUID binaries. Remove all dangerous environment
1465 if (__libc_enable_secure
)
1467 static const char *unsecure_envvars
[] =
1470 #ifdef EXTRA_UNSECURE_ENVVARS
1471 EXTRA_UNSECURE_ENVVARS
1476 if (preloadlist
!= NULL
)
1477 unsetenv ("LD_PRELOAD");
1478 if (library_path
!= NULL
)
1479 unsetenv ("LD_LIBRARY_PATH");
1480 if (_dl_origin_path
!= NULL
)
1481 unsetenv ("LD_ORIGIN_PATH");
1482 if (debug_output
!= NULL
)
1483 unsetenv ("LD_DEBUG_OUTPUT");
1484 if (_dl_profile
!= NULL
)
1485 unsetenv ("LD_PROFILE");
1488 cnt
< sizeof (unsecure_envvars
) / sizeof (unsecure_envvars
[0]);
1490 unsetenv (unsecure_envvars
[cnt
]);
1492 if (__access ("/etc/suid-debug", F_OK
) != 0)
1493 unsetenv ("MALLOC_CHECK_");
1496 /* The name of the object to profile cannot be empty. */
1497 if (_dl_profile
!= NULL
&& *_dl_profile
== '\0')
1500 /* If we have to run the dynamic linker in debugging mode and the
1501 LD_DEBUG_OUTPUT environment variable is given, we write the debug
1502 messages to this file. */
1503 if (any_debug
&& debug_output
!= NULL
&& !__libc_enable_secure
)
1506 const int flags
= O_WRONLY
| O_APPEND
| O_CREAT
| O_NOFOLLOW
;
1508 const int flags
= O_WRONLY
| O_APPEND
| O_CREAT
;
1510 size_t name_len
= strlen (debug_output
);
1511 char buf
[name_len
+ 12];
1514 buf
[name_len
+ 11] = '\0';
1515 startp
= _itoa_word (__getpid (), &buf
[name_len
+ 11], 10, 0);
1517 startp
= memcpy (startp
- name_len
, debug_output
, name_len
);
1519 _dl_debug_fd
= __open (startp
, flags
, 0666);
1520 if (_dl_debug_fd
== -1)
1521 /* We use standard output if opening the file failed. */
1522 _dl_debug_fd
= STDOUT_FILENO
;
1525 /* LAZY is determined by the environment variable LD_WARN and
1526 LD_BIND_NOW if we trace the binary. */
1527 if (__builtin_expect (mode
, normal
) == trace
)
1528 *lazyp
= _dl_verbose
? !bind_now
: -1;
1536 /* Print the various times we collected. */
1538 print_statistics (void)
1541 #ifndef HP_TIMING_NONAVAIL
1545 /* Total time rtld used. */
1546 if (HP_TIMING_AVAIL
)
1548 HP_TIMING_PRINT (buf
, sizeof (buf
), rtld_total_time
);
1549 _dl_debug_message (1, "\nruntime linker statistics:\n"
1550 " total startup time in dynamic loader: ",
1554 /* Print relocation statistics. */
1555 if (HP_TIMING_AVAIL
)
1557 HP_TIMING_PRINT (buf
, sizeof (buf
), relocate_time
);
1558 _dl_debug_message (1, " time needed for relocation: ", buf
,
1560 cp
= _itoa_word ((1000 * relocate_time
) / rtld_total_time
,
1561 buf
+ sizeof (buf
), 10, 0);
1563 switch (buf
+ sizeof (buf
) - cp
)
1574 _dl_debug_message (0, " (", buf
, "%)\n", NULL
);
1577 buf
[sizeof (buf
) - 1] = '\0';
1578 _dl_debug_message (1, " number of relocations: ",
1579 _itoa_word (_dl_num_relocations
,
1580 buf
+ sizeof (buf
) - 1, 10, 0),
1583 #ifndef HP_TIMING_NONAVAIL
1584 /* Time spend while loading the object and the dependencies. */
1585 if (HP_TIMING_AVAIL
)
1587 HP_TIMING_PRINT (buf
, sizeof (buf
), load_time
);
1588 _dl_debug_message (1, " time needed to load objects: ", buf
,
1590 cp
= _itoa_word ((1000 * load_time
) / rtld_total_time
,
1591 buf
+ sizeof (buf
), 10, 0);
1593 switch (buf
+ sizeof (buf
) - cp
)
1604 _dl_debug_message (0, " (", buf
, "%)\n", NULL
);