Update.
[glibc.git] / elf / rtld.c
blob7c2e054d88a0a46a5bc2a4164c71614ad1cefc8a
1 /* Run time dynamic linker.
2 Copyright (C) 1995, 1996, 1997, 1998 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. */
20 #include <fcntl.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/mman.h> /* Check if MAP_ANON is defined. */
25 #include <elf/ldsodefs.h>
26 #include <stdio-common/_itoa.h>
27 #include <entry.h>
28 #include "dynamic-link.h"
29 #include "dl-librecon.h"
31 #include <assert.h>
33 /* System-specific function to do initial startup for the dynamic linker.
34 After this, file access calls and getenv must work. This is responsible
35 for setting __libc_enable_secure if we need to be secure (e.g. setuid),
36 and for setting _dl_argc and _dl_argv, and then calling _dl_main. */
37 extern ElfW(Addr) _dl_sysdep_start (void **start_argptr,
38 void (*dl_main) (const ElfW(Phdr) *phdr,
39 ElfW(Half) phent,
40 ElfW(Addr) *user_entry));
41 extern void _dl_sysdep_start_cleanup (void);
43 /* This function is used to unload the cache file if necessary. */
44 extern void _dl_unload_cache (void);
46 /* System-dependent function to read a file's whole contents
47 in the most convenient manner available. */
48 extern void *_dl_sysdep_read_whole_file (const char *filename,
49 size_t *filesize_ptr,
50 int mmap_prot);
52 /* Helper function to handle errors while resolving symbols. */
53 static void print_unresolved (int errcode, const char *objname,
54 const char *errsting);
56 /* Helper function to handle errors when a version is missing. */
57 static void print_missing_version (int errcode, const char *objname,
58 const char *errsting);
61 /* This is a list of all the modes the dynamic loader can be in. */
62 enum mode { normal, list, verify, trace };
64 /* Process all environments variables the dynamic linker must recognize.
65 Since all of them start with `LD_' we are a bit smarter while finding
66 all the entries. */
67 static void process_envvars (enum mode *modep, int *lazyp);
69 int _dl_argc;
70 char **_dl_argv;
71 unsigned int _dl_skip_args; /* Nonzero if we were run directly. */
72 int _dl_verbose;
73 const char *_dl_platform;
74 size_t _dl_platformlen;
75 unsigned long _dl_hwcap;
76 struct r_search_path *_dl_search_paths;
77 const char *_dl_profile;
78 const char *_dl_profile_output;
79 struct link_map *_dl_profile_map;
80 int _dl_debug_libs;
81 int _dl_debug_impcalls;
82 int _dl_debug_bindings;
83 int _dl_debug_symbols;
84 int _dl_debug_versions;
85 int _dl_debug_reloc;
86 int _dl_debug_files;
87 const char *_dl_inhibit_rpath; /* RPATH values which should be
88 ignored. */
89 const char *_dl_origin_path;
91 /* This is a pointer to the map for the main object and through it to
92 all loaded objects. */
93 struct link_map *_dl_loaded;
94 /* Pointer to the l_searchlist element of the link map of the main object. */
95 struct r_scope_elem *_dl_main_searchlist;
96 /* Array which is used when looking up in the global scope. */
97 struct r_scope_elem *_dl_global_scope[2];
99 /* Set nonzero during loading and initialization of executable and
100 libraries, cleared before the executable's entry point runs. This
101 must not be initialized to nonzero, because the unused dynamic
102 linker loaded in for libc.so's "ld.so.1" dep will provide the
103 definition seen by libc.so's initializer; that value must be zero,
104 and will be since that dynamic linker's _dl_start and dl_main will
105 never be called. */
106 int _dl_starting_up;
109 static void dl_main (const ElfW(Phdr) *phdr,
110 ElfW(Half) phent,
111 ElfW(Addr) *user_entry);
113 struct link_map _dl_rtld_map;
114 struct libname_list _dl_rtld_libname;
115 struct libname_list _dl_rtld_libname2;
117 #ifdef RTLD_START
118 RTLD_START
119 #else
120 #error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
121 #endif
123 static ElfW(Addr)
124 _dl_start (void *arg)
126 struct link_map bootstrap_map;
128 /* This #define produces dynamic linking inline functions for
129 bootstrap relocation instead of general-purpose relocation. */
130 #define RTLD_BOOTSTRAP
131 #define RESOLVE(sym, version, flags) bootstrap_map.l_addr
132 #include "dynamic-link.h"
134 /* Figure out the run-time load address of the dynamic linker itself. */
135 bootstrap_map.l_addr = elf_machine_load_address ();
137 /* Read our own dynamic section and fill in the info array. */
138 bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
139 elf_get_dynamic_info (bootstrap_map.l_ld, bootstrap_map.l_info);
141 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
142 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info);
143 #endif
145 /* Relocate ourselves so we can do normal function calls and
146 data access using the global offset table. */
148 ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0, 0);
149 /* Please note that we don't allow profiling of this object and
150 therefore need not test whether we have to allocate the array
151 for the relocation results (as done in dl-reloc.c). */
153 /* Now life is sane; we can call functions and access global data.
154 Set up to use the operating system facilities, and find out from
155 the operating system's program loader where to find the program
156 header table in core. */
158 /* Transfer data about ourselves to the permanent link_map structure. */
159 _dl_rtld_map.l_addr = bootstrap_map.l_addr;
160 _dl_rtld_map.l_ld = bootstrap_map.l_ld;
161 _dl_rtld_map.l_opencount = 1;
162 memcpy (_dl_rtld_map.l_info, bootstrap_map.l_info,
163 sizeof _dl_rtld_map.l_info);
164 _dl_setup_hash (&_dl_rtld_map);
166 /* Don't bother trying to work out how ld.so is mapped in memory. */
167 _dl_rtld_map.l_map_start = ~0;
168 _dl_rtld_map.l_map_end = ~0;
170 /* Call the OS-dependent function to set up life so we can do things like
171 file access. It will call `dl_main' (below) to do all the real work
172 of the dynamic linker, and then unwind our frame and run the user
173 entry point on the same stack we entered on. */
174 return _dl_sysdep_start (arg, &dl_main);
177 /* Now life is peachy; we can do all normal operations.
178 On to the real work. */
180 void ENTRY_POINT (void);
182 /* Some helper functions. */
184 /* Arguments to relocate_doit. */
185 struct relocate_args
187 struct link_map *l;
188 int lazy;
191 struct map_args
193 /* Argument to map_doit. */
194 char *str;
195 /* Return value of map_doit. */
196 struct link_map *main_map;
199 /* Arguments to version_check_doit. */
200 struct version_check_args
202 int doexit;
205 static void
206 relocate_doit (void *a)
208 struct relocate_args *args = (struct relocate_args *) a;
210 _dl_relocate_object (args->l, args->l->l_scope,
211 args->lazy, 0);
214 static void
215 map_doit (void *a)
217 struct map_args *args = (struct map_args *) a;
218 args->main_map = _dl_map_object (NULL, args->str, 0, lt_library, 0);
221 static void
222 version_check_doit (void *a)
224 struct version_check_args *args = (struct version_check_args *) a;
225 if (_dl_check_all_versions (_dl_loaded, 1) && args->doexit)
226 /* We cannot start the application. Abort now. */
227 _exit (1);
231 static inline struct link_map *
232 find_needed (const char *name)
234 unsigned int n = _dl_loaded->l_searchlist.r_nlist;
236 while (n-- > 0)
237 if (_dl_name_match_p (name, _dl_loaded->l_searchlist.r_list[n]))
238 return _dl_loaded->l_searchlist.r_list[n];
240 /* Should never happen. */
241 return NULL;
244 static int
245 match_version (const char *string, struct link_map *map)
247 const char *strtab = (const char *) (map->l_addr
248 + map->l_info[DT_STRTAB]->d_un.d_ptr);
249 ElfW(Verdef) *def;
251 #define VERDEFTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
252 if (map->l_info[VERDEFTAG] == NULL)
253 /* The file has no symbol versioning. */
254 return 0;
256 def = (ElfW(Verdef) *) ((char *) map->l_addr
257 + map->l_info[VERDEFTAG]->d_un.d_ptr);
258 while (1)
260 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
262 /* Compare the version strings. */
263 if (strcmp (string, strtab + aux->vda_name) == 0)
264 /* Bingo! */
265 return 1;
267 /* If no more definitions we failed to find what we want. */
268 if (def->vd_next == 0)
269 break;
271 /* Next definition. */
272 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
275 return 0;
278 static const char *library_path; /* The library search path. */
279 static const char *preloadlist; /* The list preloaded objects. */
280 static int version_info; /* Nonzero if information about
281 versions has to be printed. */
283 static void
284 dl_main (const ElfW(Phdr) *phdr,
285 ElfW(Half) phent,
286 ElfW(Addr) *user_entry)
288 const ElfW(Phdr) *ph;
289 int lazy;
290 enum mode mode;
291 struct link_map **preloads;
292 unsigned int npreloads;
293 size_t file_size;
294 char *file;
295 int has_interp = 0;
296 unsigned int i;
297 int paths_initialized = 0;
299 /* Process the environment variable which control the behaviour. */
300 process_envvars (&mode, &lazy);
302 /* Set up a flag which tells we are just starting. */
303 _dl_starting_up = 1;
305 if (*user_entry == (ElfW(Addr)) &ENTRY_POINT)
307 /* Ho ho. We are not the program interpreter! We are the program
308 itself! This means someone ran ld.so as a command. Well, that
309 might be convenient to do sometimes. We support it by
310 interpreting the args like this:
312 ld.so PROGRAM ARGS...
314 The first argument is the name of a file containing an ELF
315 executable we will load and run with the following arguments.
316 To simplify life here, PROGRAM is searched for using the
317 normal rules for shared objects, rather than $PATH or anything
318 like that. We just load it and use its entry point; we don't
319 pay attention to its PT_INTERP command (we are the interpreter
320 ourselves). This is an easy way to test a new ld.so before
321 installing it. */
323 /* Note the place where the dynamic linker actually came from. */
324 _dl_rtld_map.l_name = _dl_argv[0];
326 while (_dl_argc > 1)
327 if (! strcmp (_dl_argv[1], "--list"))
329 mode = list;
330 lazy = -1; /* This means do no dependency analysis. */
332 ++_dl_skip_args;
333 --_dl_argc;
334 ++_dl_argv;
336 else if (! strcmp (_dl_argv[1], "--verify"))
338 mode = verify;
340 ++_dl_skip_args;
341 --_dl_argc;
342 ++_dl_argv;
344 else if (! strcmp (_dl_argv[1], "--library-path") && _dl_argc > 2)
346 library_path = _dl_argv[2];
348 _dl_skip_args += 2;
349 _dl_argc -= 2;
350 _dl_argv += 2;
352 else if (! strcmp (_dl_argv[1], "--inhibit-rpath") && _dl_argc > 2)
354 _dl_inhibit_rpath = _dl_argv[2];
356 _dl_skip_args += 2;
357 _dl_argc -= 2;
358 _dl_argv += 2;
360 else
361 break;
363 /* If we have no further argument the program was called incorrectly.
364 Grant the user some education. */
365 if (_dl_argc < 2)
366 _dl_sysdep_fatal ("\
367 Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
368 You have invoked `ld.so', the helper program for shared library executables.\n\
369 This program usually lives in the file `/lib/ld.so', and special directives\n\
370 in executable files using ELF shared libraries tell the system's program\n\
371 loader to load the helper program from this file. This helper program loads\n\
372 the shared libraries needed by the program executable, prepares the program\n\
373 to run, and runs it. You may invoke this helper program directly from the\n\
374 command line to load and run an ELF executable file; this is like executing\n\
375 that file itself, but always uses this helper program from the file you\n\
376 specified, instead of the helper program file specified in the executable\n\
377 file you run. This is mostly of use for maintainers to test new versions\n\
378 of this helper program; chances are you did not intend to run this program.\n\
380 --list list all dependencies and how they are resolved\n\
381 --verify verify that given object really is a dynamically linked\n\
382 object we get handle\n\
383 --library-path PATH use given PATH instead of content of the environment\n\
384 variable LD_LIBRARY_PATH\n\
385 --inhibit-rpath LIST ignore RPATH information in object names in LIST\n",
386 NULL);
388 ++_dl_skip_args;
389 --_dl_argc;
390 ++_dl_argv;
392 /* Initialize the data structures for the search paths for shared
393 objects. */
394 _dl_init_paths (library_path);
395 paths_initialized = 1;
397 if (mode == verify)
399 char *err_str = NULL;
400 struct map_args args;
402 args.str = _dl_argv[0];
403 (void) _dl_catch_error (&err_str, map_doit, &args);
404 if (err_str != NULL)
406 free (err_str);
407 _exit (EXIT_FAILURE);
410 else
411 _dl_map_object (NULL, _dl_argv[0], 0, lt_library, 0);
413 phdr = _dl_loaded->l_phdr;
414 phent = _dl_loaded->l_phnum;
415 /* We overwrite here a pointer to a malloc()ed string. But since
416 the malloc() implementation used at this point is the dummy
417 implementations which has no real free() function it does not
418 makes sense to free the old string first. */
419 _dl_loaded->l_name = (char *) "";
420 *user_entry = _dl_loaded->l_entry;
422 else
424 /* Create a link_map for the executable itself.
425 This will be what dlopen on "" returns. */
426 _dl_new_object ((char *) "", "", lt_executable, NULL);
427 if (_dl_loaded == NULL)
428 _dl_sysdep_fatal ("cannot allocate memory for link map\n", NULL);
429 _dl_loaded->l_phdr = phdr;
430 _dl_loaded->l_phnum = phent;
431 _dl_loaded->l_entry = *user_entry;
432 _dl_loaded->l_opencount = 1;
434 /* We delay initializing the path structure until we got the dynamic
435 information for the program. */
438 /* It is not safe to load stuff after the main program. */
439 _dl_loaded->l_map_end = ~0;
440 /* Perhaps the executable has no PT_LOAD header entries at all. */
441 _dl_loaded->l_map_start = ~0;
443 /* Scan the program header table for the dynamic section. */
444 for (ph = phdr; ph < &phdr[phent]; ++ph)
445 switch (ph->p_type)
447 case PT_PHDR:
448 /* Find out the load address. */
449 _dl_loaded->l_addr = (ElfW(Addr)) phdr - ph->p_vaddr;
450 break;
451 case PT_DYNAMIC:
452 /* This tells us where to find the dynamic section,
453 which tells us everything we need to do. */
454 _dl_loaded->l_ld = (void *) _dl_loaded->l_addr + ph->p_vaddr;
455 break;
456 case PT_INTERP:
457 /* This "interpreter segment" was used by the program loader to
458 find the program interpreter, which is this program itself, the
459 dynamic linker. We note what name finds us, so that a future
460 dlopen call or DT_NEEDED entry, for something that wants to link
461 against the dynamic linker as a shared library, will know that
462 the shared object is already loaded. */
463 _dl_rtld_libname.name = ((const char *) _dl_loaded->l_addr
464 + ph->p_vaddr);
465 _dl_rtld_libname.next = NULL;
466 _dl_rtld_map.l_libname = &_dl_rtld_libname;
468 /* Ordinarilly, we would get additional names for the loader from
469 our DT_SONAME. This can't happen if we were actually linked as
470 a static executable (detect this case when we have no DYNAMIC).
471 If so, assume the filename component of the interpreter path to
472 be our SONAME, and add it to our name list. */
473 if (_dl_rtld_map.l_ld == NULL)
475 char *p = strrchr (_dl_rtld_libname.name, '/');
476 if (p)
478 _dl_rtld_libname2.name = p+1;
479 _dl_rtld_libname2.next = NULL;
480 _dl_rtld_libname.next = &_dl_rtld_libname2;
484 has_interp = 1;
485 break;
486 case PT_LOAD:
487 /* Remember where the main program starts in memory. */
489 ElfW(Addr) mapstart;
490 mapstart = _dl_loaded->l_addr + (ph->p_vaddr & ~(ph->p_align - 1));
491 if (_dl_loaded->l_map_start > mapstart)
492 _dl_loaded->l_map_start = mapstart;
494 break;
496 if (! _dl_rtld_map.l_libname && _dl_rtld_map.l_name)
498 /* We were invoked directly, so the program might not have a
499 PT_INTERP. */
500 _dl_rtld_libname.name = _dl_rtld_map.l_name;
501 _dl_rtld_libname.next = NULL;
502 _dl_rtld_map.l_libname = &_dl_rtld_libname;
504 else
505 assert (_dl_rtld_map.l_libname); /* How else did we get here? */
507 /* Extract the contents of the dynamic section for easy access. */
508 elf_get_dynamic_info (_dl_loaded->l_ld, _dl_loaded->l_info);
509 if (_dl_loaded->l_info[DT_HASH])
510 /* Set up our cache of pointers into the hash table. */
511 _dl_setup_hash (_dl_loaded);
513 if (mode == verify)
515 /* We were called just to verify that this is a dynamic
516 executable using us as the program interpreter. Exit with an
517 error if we were not able to load the binary or no interpreter
518 is specified (i.e., this is no dynamically linked binary. */
519 if (_dl_loaded->l_ld == NULL)
520 _exit (1);
522 /* We allow here some platform specific code. */
523 #ifdef DISTINGUISH_LIB_VERSIONS
524 DISTINGUISH_LIB_VERSIONS;
525 #endif
526 _exit (has_interp ? 0 : 2);
529 if (! paths_initialized)
530 /* Initialize the data structures for the search paths for shared
531 objects. */
532 _dl_init_paths (library_path);
534 /* Put the link_map for ourselves on the chain so it can be found by
535 name. Note that at this point the global chain of link maps contains
536 exactly one element, which is pointed to by _dl_loaded. */
537 if (! _dl_rtld_map.l_name)
538 /* If not invoked directly, the dynamic linker shared object file was
539 found by the PT_INTERP name. */
540 _dl_rtld_map.l_name = (char *) _dl_rtld_map.l_libname->name;
541 _dl_rtld_map.l_type = lt_library;
542 _dl_loaded->l_next = &_dl_rtld_map;
543 _dl_rtld_map.l_prev = _dl_loaded;
545 /* We have two ways to specify objects to preload: via environment
546 variable and via the file /etc/ld.so.preload. The later can also
547 be used when security is enabled. */
548 preloads = NULL;
549 npreloads = 0;
551 if (preloadlist)
553 /* The LD_PRELOAD environment variable gives list of libraries
554 separated by white space or colons that are loaded before the
555 executable's dependencies and prepended to the global scope
556 list. If the binary is running setuid all elements
557 containing a '/' are ignored since it is insecure. */
558 char *list = strdupa (preloadlist);
559 char *p;
560 while ((p = strsep (&list, " :")) != NULL)
561 if (p[0] != '\0'
562 && (! __libc_enable_secure || strchr (p, '/') == NULL))
564 struct link_map *new_map = _dl_map_object (_dl_loaded, p, 1,
565 lt_library, 0);
566 if (new_map->l_opencount == 1)
567 /* It is no duplicate. */
568 ++npreloads;
572 /* Read the contents of the file. */
573 file = _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size,
574 PROT_READ | PROT_WRITE);
575 if (file)
577 /* Parse the file. It contains names of libraries to be loaded,
578 separated by white spaces or `:'. It may also contain
579 comments introduced by `#'. */
580 char *problem;
581 char *runp;
582 size_t rest;
584 /* Eliminate comments. */
585 runp = file;
586 rest = file_size;
587 while (rest > 0)
589 char *comment = memchr (runp, '#', rest);
590 if (comment == NULL)
591 break;
593 rest -= comment - runp;
595 *comment = ' ';
596 while (--rest > 0 && *++comment != '\n');
599 /* We have one problematic case: if we have a name at the end of
600 the file without a trailing terminating characters, we cannot
601 place the \0. Handle the case separately. */
602 if (file[file_size - 1] != ' ' && file[file_size - 1] != '\t'
603 && file[file_size - 1] != '\n' && file[file_size - 1] != ':')
605 problem = &file[file_size];
606 while (problem > file && problem[-1] != ' ' && problem[-1] != '\t'
607 && problem[-1] != '\n' && problem[-1] != ':')
608 --problem;
610 if (problem > file)
611 problem[-1] = '\0';
613 else
615 problem = NULL;
616 file[file_size - 1] = '\0';
619 if (file != problem)
621 char *p;
622 runp = file;
623 while ((p = strsep (&runp, ": \t\n")) != NULL)
624 if (p[0] != '\0')
626 struct link_map *new_map = _dl_map_object (_dl_loaded, p, 1,
627 lt_library, 0);
628 if (new_map->l_opencount == 1)
629 /* It is no duplicate. */
630 ++npreloads;
634 if (problem != NULL)
636 char *p = strndupa (problem, file_size - (problem - file));
637 struct link_map *new_map = _dl_map_object (_dl_loaded, p, 1,
638 lt_library, 0);
639 if (new_map->l_opencount == 1)
640 /* It is no duplicate. */
641 ++npreloads;
644 /* We don't need the file anymore. */
645 __munmap (file, file_size);
648 if (npreloads != 0)
650 /* Set up PRELOADS with a vector of the preloaded libraries. */
651 struct link_map *l;
652 preloads = __alloca (npreloads * sizeof preloads[0]);
653 l = _dl_rtld_map.l_next; /* End of the chain before preloads. */
654 i = 0;
657 preloads[i++] = l;
658 l = l->l_next;
659 } while (l);
660 assert (i == npreloads);
663 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
664 specified some libraries to load, these are inserted before the actual
665 dependencies in the executable's searchlist for symbol resolution. */
666 _dl_map_object_deps (_dl_loaded, preloads, npreloads, mode == trace);
668 #ifndef MAP_ANON
669 /* We are done mapping things, so close the zero-fill descriptor. */
670 __close (_dl_zerofd);
671 _dl_zerofd = -1;
672 #endif
674 /* Remove _dl_rtld_map from the chain. */
675 _dl_rtld_map.l_prev->l_next = _dl_rtld_map.l_next;
676 if (_dl_rtld_map.l_next)
677 _dl_rtld_map.l_next->l_prev = _dl_rtld_map.l_prev;
679 if (_dl_rtld_map.l_opencount > 1)
681 /* Some DT_NEEDED entry referred to the interpreter object itself, so
682 put it back in the list of visible objects. We insert it into the
683 chain in symbol search order because gdb uses the chain's order as
684 its symbol search order. */
685 i = 1;
686 while (_dl_loaded->l_searchlist.r_list[i] != &_dl_rtld_map)
687 ++i;
688 _dl_rtld_map.l_prev = _dl_loaded->l_searchlist.r_list[i - 1];
689 _dl_rtld_map.l_next = (i + 1 < _dl_loaded->l_searchlist.r_nlist
690 ? _dl_loaded->l_searchlist.r_list[i + 1]
691 : NULL);
692 assert (_dl_rtld_map.l_prev->l_next == _dl_rtld_map.l_next);
693 _dl_rtld_map.l_prev->l_next = &_dl_rtld_map;
694 if (_dl_rtld_map.l_next)
696 assert (_dl_rtld_map.l_next->l_prev == _dl_rtld_map.l_prev);
697 _dl_rtld_map.l_next->l_prev = &_dl_rtld_map;
701 /* Now let us see whether all libraries are available in the
702 versions we need. */
704 struct version_check_args args;
705 args.doexit = mode == normal;
706 _dl_receive_error (print_missing_version, version_check_doit, &args);
709 if (mode != normal)
711 /* We were run just to list the shared libraries. It is
712 important that we do this before real relocation, because the
713 functions we call below for output may no longer work properly
714 after relocation. */
715 if (! _dl_loaded->l_info[DT_NEEDED])
716 _dl_sysdep_message ("\t", "statically linked\n", NULL);
717 else
719 struct link_map *l;
721 for (l = _dl_loaded->l_next; l; l = l->l_next)
722 if (l->l_opencount == 0)
723 /* The library was not found. */
724 _dl_sysdep_message ("\t", l->l_libname->name, " => not found\n",
725 NULL);
726 else
728 char buf[20], *bp;
729 buf[sizeof buf - 1] = '\0';
730 bp = _itoa_word (l->l_addr, &buf[sizeof buf - 1], 16, 0);
731 while ((size_t) (&buf[sizeof buf - 1] - bp)
732 < sizeof l->l_addr * 2)
733 *--bp = '0';
734 _dl_sysdep_message ("\t", l->l_libname->name, " => ",
735 l->l_name, " (0x", bp, ")\n", NULL);
739 if (mode != trace)
740 for (i = 1; i < _dl_argc; ++i)
742 const ElfW(Sym) *ref = NULL;
743 ElfW(Addr) loadbase = _dl_lookup_symbol (_dl_argv[i], &ref,
744 _dl_loaded->l_scope,
745 "argument",
746 ELF_MACHINE_JMP_SLOT);
747 char buf[20], *bp;
748 buf[sizeof buf - 1] = '\0';
749 bp = _itoa_word (ref->st_value, &buf[sizeof buf - 1], 16, 0);
750 while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2)
751 *--bp = '0';
752 _dl_sysdep_message (_dl_argv[i], " found at 0x", bp, NULL);
753 buf[sizeof buf - 1] = '\0';
754 bp = _itoa_word (loadbase, &buf[sizeof buf - 1], 16, 0);
755 while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2)
756 *--bp = '0';
757 _dl_sysdep_message (" in object at 0x", bp, "\n", NULL);
759 else
761 if (lazy >= 0)
763 /* We have to do symbol dependency testing. */
764 struct relocate_args args;
765 struct link_map *l;
767 args.lazy = lazy;
769 l = _dl_loaded;
770 while (l->l_next)
771 l = l->l_next;
774 if (l != &_dl_rtld_map && l->l_opencount > 0)
776 args.l = l;
777 _dl_receive_error (print_unresolved, relocate_doit,
778 &args);
780 l = l->l_prev;
781 } while (l);
784 #define VERNEEDTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
785 if (version_info)
787 /* Print more information. This means here, print information
788 about the versions needed. */
789 int first = 1;
790 struct link_map *map = _dl_loaded;
792 for (map = _dl_loaded; map != NULL; map = map->l_next)
794 const char *strtab;
795 ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG];
796 ElfW(Verneed) *ent;
798 if (dyn == NULL)
799 continue;
801 strtab = (const char *)
802 (map->l_addr + map->l_info[DT_STRTAB]->d_un.d_ptr);
803 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
805 if (first)
807 _dl_sysdep_message ("\n\tVersion information:\n", NULL);
808 first = 0;
811 _dl_sysdep_message ("\t", (map->l_name[0]
812 ? map->l_name : _dl_argv[0]),
813 ":\n", NULL);
815 while (1)
817 ElfW(Vernaux) *aux;
818 struct link_map *needed;
820 needed = find_needed (strtab + ent->vn_file);
821 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
823 while (1)
825 const char *fname = NULL;
827 _dl_sysdep_message ("\t\t",
828 strtab + ent->vn_file,
829 " (", strtab + aux->vna_name,
830 ") ",
831 (aux->vna_flags
832 & VER_FLG_WEAK
833 ? "[WEAK] " : ""),
834 "=> ", NULL);
836 if (needed != NULL
837 && match_version (strtab+aux->vna_name, needed))
838 fname = needed->l_name;
840 _dl_sysdep_message (fname ?: "not found", "\n",
841 NULL);
843 if (aux->vna_next == 0)
844 /* No more symbols. */
845 break;
847 /* Next symbol. */
848 aux = (ElfW(Vernaux) *) ((char *) aux
849 + aux->vna_next);
852 if (ent->vn_next == 0)
853 /* No more dependencies. */
854 break;
856 /* Next dependency. */
857 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
863 _exit (0);
867 /* Now we have all the objects loaded. Relocate them all except for
868 the dynamic linker itself. We do this in reverse order so that copy
869 relocs of earlier objects overwrite the data written by later
870 objects. We do not re-relocate the dynamic linker itself in this
871 loop because that could result in the GOT entries for functions we
872 call being changed, and that would break us. It is safe to relocate
873 the dynamic linker out of order because it has no copy relocs (we
874 know that because it is self-contained). */
876 struct link_map *l;
877 int consider_profiling = _dl_profile != NULL;
879 /* If we are profiling we also must do lazy reloaction. */
880 lazy |= consider_profiling;
882 l = _dl_loaded;
883 while (l->l_next)
884 l = l->l_next;
887 if (l != &_dl_rtld_map)
888 _dl_relocate_object (l, l->l_scope, lazy, consider_profiling);
890 l = l->l_prev;
891 } while (l);
893 /* Do any necessary cleanups for the startup OS interface code.
894 We do these now so that no calls are made after rtld re-relocation
895 which might be resolved to different functions than we expect.
896 We cannot do this before relocating the other objects because
897 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
898 _dl_sysdep_start_cleanup ();
900 if (_dl_rtld_map.l_opencount > 0)
901 /* There was an explicit ref to the dynamic linker as a shared lib.
902 Re-relocate ourselves with user-controlled symbol definitions. */
903 _dl_relocate_object (&_dl_rtld_map, _dl_loaded->l_scope, 0, 0);
906 /* Now set up the variable which helps the assembler startup code. */
907 _dl_main_searchlist = &_dl_loaded->l_searchlist;
908 _dl_global_scope[0] = &_dl_loaded->l_searchlist;
911 /* Initialize _r_debug. */
912 struct r_debug *r = _dl_debug_initialize (_dl_rtld_map.l_addr);
913 struct link_map *l;
915 l = _dl_loaded;
917 #ifdef ELF_MACHINE_DEBUG_SETUP
919 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
921 ELF_MACHINE_DEBUG_SETUP (l, r);
922 ELF_MACHINE_DEBUG_SETUP (&_dl_rtld_map, r);
924 #else
926 if (l->l_info[DT_DEBUG])
927 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
928 with the run-time address of the r_debug structure */
929 l->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
931 /* Fill in the pointer in the dynamic linker's own dynamic section, in
932 case you run gdb on the dynamic linker directly. */
933 if (_dl_rtld_map.l_info[DT_DEBUG])
934 _dl_rtld_map.l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
936 #endif
938 /* Notify the debugger that all objects are now mapped in. */
939 r->r_state = RT_ADD;
940 _dl_debug_state ();
943 #ifndef MAP_COPY
944 /* We must munmap() the cache file. */
945 _dl_unload_cache ();
946 #endif
948 /* Now enable profiling if needed. */
949 if (_dl_profile_map != NULL)
950 /* We must prepare the profiling. */
951 _dl_start_profile (_dl_profile_map, _dl_profile_output);
953 /* Once we return, _dl_sysdep_start will invoke
954 the DT_INIT functions and then *USER_ENTRY. */
957 /* This is a little helper function for resolving symbols while
958 tracing the binary. */
959 static void
960 print_unresolved (int errcode __attribute__ ((unused)), const char *objname,
961 const char *errstring)
963 if (objname[0] == '\0')
964 objname = _dl_argv[0] ?: "<main program>";
965 _dl_sysdep_error (errstring, " (", objname, ")\n", NULL);
968 /* This is a little helper function for resolving symbols while
969 tracing the binary. */
970 static void
971 print_missing_version (int errcode __attribute__ ((unused)),
972 const char *objname, const char *errstring)
974 _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>", ": ",
975 objname, ": ", errstring, "\n", NULL);
978 /* Nonzero if any of the debugging options is enabled. */
979 static int any_debug;
981 /* Process the string given as the parameter which explains which debugging
982 options are enabled. */
983 static void
984 process_dl_debug (const char *dl_debug)
986 size_t len;
987 #define separators " ,:"
990 len = 0;
991 /* Skip separating white spaces and commas. */
992 dl_debug += strspn (dl_debug, separators);
993 if (*dl_debug != '\0')
995 len = strcspn (dl_debug, separators);
997 switch (len)
999 case 3:
1000 /* This option is not documented since it is not generally
1001 useful. */
1002 if (memcmp (dl_debug, "all", 3) == 0)
1004 _dl_debug_libs = 1;
1005 _dl_debug_impcalls = 1;
1006 _dl_debug_reloc = 1;
1007 _dl_debug_files = 1;
1008 _dl_debug_symbols = 1;
1009 _dl_debug_bindings = 1;
1010 _dl_debug_versions = 1;
1011 any_debug = 1;
1012 continue;
1014 break;
1016 case 4:
1017 if (memcmp (dl_debug, "help", 4) == 0)
1019 _dl_sysdep_message ("\
1020 Valid options for the LD_DEBUG environment variable are:\n\
1022 bindings display information about symbol binding\n\
1023 files display processing of files and libraries\n\
1024 help display this help message and exit\n\
1025 libs display library search paths\n\
1026 reloc display relocation processing\n\
1027 symbols display symbol table processing\n\
1028 versions display version dependencies\n\
1030 To direct the debugging output into a file instead of standard output\n\
1031 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n",
1032 NULL);
1033 _exit (0);
1036 if (memcmp (dl_debug, "libs", 4) == 0)
1038 _dl_debug_libs = 1;
1039 _dl_debug_impcalls = 1;
1040 any_debug = 1;
1041 continue;
1043 break;
1045 case 5:
1046 if (memcmp (dl_debug, "reloc", 5) == 0)
1048 _dl_debug_reloc = 1;
1049 _dl_debug_impcalls = 1;
1050 any_debug = 1;
1051 continue;
1054 if (memcmp (dl_debug, "files", 5) == 0)
1056 _dl_debug_files = 1;
1057 _dl_debug_impcalls = 1;
1058 any_debug = 1;
1059 continue;
1061 break;
1063 case 7:
1064 if (memcmp (dl_debug, "symbols", 7) == 0)
1066 _dl_debug_symbols = 1;
1067 _dl_debug_impcalls = 1;
1068 any_debug = 1;
1069 continue;
1071 break;
1073 case 8:
1074 if (memcmp (dl_debug, "bindings", 8) == 0)
1076 _dl_debug_bindings = 1;
1077 _dl_debug_impcalls = 1;
1078 any_debug = 1;
1079 continue;
1082 if (memcmp (dl_debug, "versions", 8) == 0)
1084 _dl_debug_versions = 1;
1085 _dl_debug_impcalls = 1;
1086 any_debug = 1;
1087 continue;
1089 break;
1091 default:
1092 break;
1096 /* Display a warning and skip everything until next separator. */
1097 char *startp = strndupa (dl_debug, len);
1098 _dl_sysdep_error ("warning: debug option `", startp,
1099 "' unknown; try LD_DEBUG=help\n", NULL);
1103 while (*(dl_debug += len) != '\0');
1106 /* Process all environments variables the dynamic linker must recognize.
1107 Since all of them start with `LD_' we are a bit smarter while finding
1108 all the entries. */
1109 static void
1110 process_envvars (enum mode *modep, int *lazyp)
1112 char **runp = NULL;
1113 char *envline;
1114 enum mode mode = normal;
1115 int bind_now = 0;
1116 char *debug_output = NULL;
1118 /* This is the default place for profiling data file. */
1119 _dl_profile_output = "/var/tmp";
1121 while ((envline = _dl_next_ld_env_entry (&runp)) != NULL)
1123 size_t len = strcspn (envline, "=") - 3;
1125 switch (len)
1127 case 4:
1128 /* Warning level, verbose or not. */
1129 if (memcmp (&envline[3], "WARN", 4) == 0)
1130 _dl_verbose = envline[8] != '\0';
1131 break;
1133 case 5:
1134 /* Debugging of the dynamic linker? */
1135 if (memcmp (&envline[3], "DEBUG", 5) == 0)
1136 process_dl_debug (&envline[9]);
1137 break;
1139 case 7:
1140 /* Print information about versions. */
1141 if (memcmp (&envline[3], "VERBOSE", 7) == 0)
1143 version_info = envline[11] != '\0';
1144 break;
1147 /* List of objects to be preloaded. */
1148 if (memcmp (&envline[3], "PRELOAD", 7) == 0)
1150 preloadlist = &envline[11];
1151 break;
1154 /* Which shared object shall be profiled. */
1155 if (memcmp (&envline[3], "PROFILE", 7) == 0)
1157 _dl_profile = &envline[11];
1158 if (*_dl_profile == '\0')
1159 _dl_profile = NULL;
1161 break;
1163 case 8:
1164 /* Do we bind early? */
1165 if (memcmp (&envline[3], "BIND_NOW", 8) == 0
1166 && (envline[12] == '1' || envline[12] == 'y'
1167 || envline[12] == 'Y'
1168 || ((envline[12] == 'o' || envline[12] == 'O')
1169 && (envline[13] == 'n' || envline[13] == 'N'))))
1170 bind_now = 1;
1171 break;
1173 case 9:
1174 /* Test whether we want to see the content of the auxiliary
1175 array passed up from the kernel. */
1176 if (memcmp (&envline[3], "SHOW_AUXV", 9) == 0)
1177 _dl_show_auxv ();
1178 break;
1180 case 10:
1181 /* Mask for the important hardware capabilities. */
1182 if (memcmp (&envline[3], "HWCAP_MASK", 10) == 0)
1183 _dl_hwcap_mask = strtoul (&envline[14], NULL, 0);
1184 break;
1186 case 11:
1187 /* Path where the binary is found. */
1188 if (!__libc_enable_secure
1189 && memcmp (&envline[3], "ORIGIN_PATH", 11) == 0)
1190 _dl_origin_path = &envline[15];
1191 break;
1193 case 12:
1194 /* Where to place the profiling data file. */
1195 if (memcmp (&envline[3], "DEBUG_OUTPUT", 12) == 0)
1197 debug_output = &envline[16];
1198 break;
1201 /* The library search path. */
1202 if (memcmp (&envline[3], "LIBRARY_PATH", 12) == 0)
1203 library_path = &envline[16];
1204 break;
1206 case 14:
1207 /* Where to place the profiling data file. */
1208 if (!__libc_enable_secure
1209 && memcmp (&envline[3], "PROFILE_OUTPUT", 14) == 0)
1211 _dl_profile_output = &envline[18];
1212 if (*_dl_profile_output == '\0')
1213 _dl_profile_output = "/var/tmp";
1215 break;
1217 case 20:
1218 /* The mode of the dynamic linker can be set. */
1219 if (memcmp (&envline[3], "TRACE_LOADED_OBJECTS", 20) == 0)
1220 mode = trace;
1221 break;
1223 /* We might have some extra environment variable to handle. This
1224 is tricky due to the pre-processing of the length of the name
1225 in the switch statement here. The code here assumes that added
1226 environment variables have a different length. */
1227 #ifdef EXTRA_LD_ENVVARS
1228 EXTRA_LD_ENVVARS
1229 #endif
1233 /* Extra security for SUID binaries. Remove all dangerous environment
1234 variables. */
1235 if (__libc_enable_secure)
1237 static const char *unsecure_envvars[] =
1239 #ifdef EXTRA_UNSECURE_ENVVARS
1240 EXTRA_UNSECURE_ENVVARS
1241 #endif
1243 size_t cnt;
1245 if (preloadlist != NULL)
1246 unsetenv ("LD_PRELOAD");
1247 if (library_path != NULL)
1248 unsetenv ("LD_LIBRARY_PATH");
1250 for (cnt = 0;
1251 cnt < sizeof (unsecure_envvars) / sizeof (unsecure_envvars[0]);
1252 ++cnt)
1253 unsetenv (unsecure_envvars[cnt]);
1256 /* If we have to run the dynamic linker in debugging mode and the
1257 LD_DEBUG_OUTPUT environment variable is given, we write the debug
1258 messages to this file. */
1259 if (any_debug && debug_output != NULL && !__libc_enable_secure)
1261 size_t name_len = strlen (debug_output);
1262 char buf[name_len + 12];
1263 char *startp;
1265 buf[name_len + 11] = '\0';
1266 startp = _itoa_word (__getpid (), &buf[name_len + 11], 10, 0);
1267 *--startp = '.';
1268 startp = memcpy (startp - name_len, debug_output, name_len);
1270 _dl_debug_fd = __open (startp, O_WRONLY | O_APPEND | O_CREAT, 0666);
1271 if (_dl_debug_fd == -1)
1272 /* We use standard output if opening the file failed. */
1273 _dl_debug_fd = STDOUT_FILENO;
1276 /* LAZY is determined by the environment variable LD_WARN and
1277 LD_BIND_NOW if we trace the binary. */
1278 if (mode == trace)
1279 *lazyp = _dl_verbose ? !bind_now : -1;
1280 else
1281 *lazyp = !__libc_enable_secure && !bind_now;
1283 *modep = mode;