Update.
[glibc.git] / elf / rtld.c
blobdd79a81124d7ff3ebbd08d4f7af96b406cd05476
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 <link.h>
21 #include <stddef.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/mman.h> /* Check if MAP_ANON is defined. */
26 #include <stdio-common/_itoa.h>
27 #include <assert.h>
28 #include <entry.h>
29 #include "dynamic-link.h"
32 /* System-specific function to do initial startup for the dynamic linker.
33 After this, file access calls and getenv must work. This is responsible
34 for setting __libc_enable_secure if we need to be secure (e.g. setuid),
35 and for setting _dl_argc and _dl_argv, and then calling _dl_main. */
36 extern ElfW(Addr) _dl_sysdep_start (void **start_argptr,
37 void (*dl_main) (const ElfW(Phdr) *phdr,
38 ElfW(Half) phent,
39 ElfW(Addr) *user_entry));
40 extern void _dl_sysdep_start_cleanup (void);
42 /* System-dependent function to read a file's whole contents
43 in the most convenient manner available. */
44 extern void *_dl_sysdep_read_whole_file (const char *filename,
45 size_t *filesize_ptr,
46 int mmap_prot);
48 /* Helper function to handle errors while resolving symbols. */
49 static void print_unresolved (int errcode, const char *objname,
50 const char *errsting);
52 /* Helper function to handle errors when a version is missing. */
53 static void print_missing_version (int errcode, const char *objname,
54 const char *errsting);
56 int _dl_argc;
57 char **_dl_argv;
58 const char *_dl_rpath;
59 int _dl_verbose;
60 const char *_dl_platform;
61 size_t _dl_platformlen;
62 unsigned long _dl_hwcap;
63 struct r_search_path *_dl_search_paths;
64 const char *_dl_profile;
65 const char *_dl_profile_output;
66 struct link_map *_dl_profile_map;
68 /* Set nonzero during loading and initialization of executable and
69 libraries, cleared before the executable's entry point runs. This
70 must not be initialized to nonzero, because the unused dynamic
71 linker loaded in for libc.so's "ld.so.1" dep will provide the
72 definition seen by libc.so's initializer; that value must be zero,
73 and will be since that dynamic linker's _dl_start and dl_main will
74 never be called. */
75 int _dl_starting_up;
77 static void dl_main (const ElfW(Phdr) *phdr,
78 ElfW(Half) phent,
79 ElfW(Addr) *user_entry);
81 struct link_map _dl_rtld_map;
82 struct libname_list _dl_rtld_libname;
83 struct libname_list _dl_rtld_libname2;
85 #ifdef RTLD_START
86 RTLD_START
87 #else
88 #error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
89 #endif
91 static ElfW(Addr)
92 _dl_start (void *arg)
94 struct link_map bootstrap_map;
96 /* This #define produces dynamic linking inline functions for
97 bootstrap relocation instead of general-purpose relocation. */
98 #define RTLD_BOOTSTRAP
99 #define RESOLVE(sym, version, flags) bootstrap_map.l_addr
100 #include "dynamic-link.h"
102 /* Figure out the run-time load address of the dynamic linker itself. */
103 bootstrap_map.l_addr = elf_machine_load_address ();
105 /* Read our own dynamic section and fill in the info array. */
106 bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
107 elf_get_dynamic_info (bootstrap_map.l_ld, bootstrap_map.l_info);
109 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
110 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info);
111 #endif
113 /* Relocate ourselves so we can do normal function calls and
114 data access using the global offset table. */
116 ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0, 0);
118 /* Now life is sane; we can call functions and access global data.
119 Set up to use the operating system facilities, and find out from
120 the operating system's program loader where to find the program
121 header table in core. */
123 /* Transfer data about ourselves to the permanent link_map structure. */
124 _dl_rtld_map.l_addr = bootstrap_map.l_addr;
125 _dl_rtld_map.l_ld = bootstrap_map.l_ld;
126 _dl_rtld_map.l_opencount = 1;
127 memcpy (_dl_rtld_map.l_info, bootstrap_map.l_info,
128 sizeof _dl_rtld_map.l_info);
129 _dl_setup_hash (&_dl_rtld_map);
131 /* Cache the DT_RPATH stored in ld.so itself; this will be
132 the default search path. */
133 if (_dl_rtld_map.l_info[DT_STRTAB] && _dl_rtld_map.l_info[DT_RPATH])
135 _dl_rpath = (void *) (_dl_rtld_map.l_addr +
136 _dl_rtld_map.l_info[DT_STRTAB]->d_un.d_ptr +
137 _dl_rtld_map.l_info[DT_RPATH]->d_un.d_val);
140 /* Call the OS-dependent function to set up life so we can do things like
141 file access. It will call `dl_main' (below) to do all the real work
142 of the dynamic linker, and then unwind our frame and run the user
143 entry point on the same stack we entered on. */
144 return _dl_sysdep_start (arg, &dl_main);
148 /* Now life is peachy; we can do all normal operations.
149 On to the real work. */
151 void ENTRY_POINT (void);
153 /* Some helper functions. */
155 /* Arguments to relocate_doit. */
156 struct relocate_args
158 struct link_map *l;
159 int lazy;
162 struct map_args
164 /* Argument to map_doit. */
165 char *str;
166 /* Return value of map_doit. */
167 struct link_map *main_map;
170 /* Arguments to version_check_doit. */
171 struct version_check_args
173 struct link_map *main_map;
174 int doexit;
177 static void
178 relocate_doit (void *a)
180 struct relocate_args *args = (struct relocate_args *) a;
182 _dl_relocate_object (args->l, _dl_object_relocation_scope (args->l),
183 args->lazy);
186 static void
187 map_doit (void *a)
189 struct map_args *args = (struct map_args *)a;
190 args->main_map = _dl_map_object (NULL, args->str, 0, lt_library, 0);
193 static void
194 version_check_doit (void *a)
196 struct version_check_args *args = (struct version_check_args *)a;
197 if (_dl_check_all_versions (args->main_map, 1) && args->doexit)
198 /* We cannot start the application. Abort now. */
199 _exit (1);
203 static inline struct link_map *
204 find_needed (const char *name)
206 unsigned int n;
208 for (n = 0; n < _dl_loaded->l_nsearchlist; ++n)
209 if (_dl_name_match_p (name, _dl_loaded->l_searchlist[n]))
210 return _dl_loaded->l_searchlist[n];
212 /* Should never happen. */
213 return NULL;
216 static int
217 match_version (const char *string, struct link_map *map)
219 const char *strtab = (const char *) (map->l_addr
220 + map->l_info[DT_STRTAB]->d_un.d_ptr);
221 ElfW(Verdef) *def;
223 #define VERDEFTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
224 if (map->l_info[VERDEFTAG] == NULL)
225 /* The file has no symbol versioning. */
226 return 0;
228 def = (ElfW(Verdef) *) ((char *) map->l_addr
229 + map->l_info[VERDEFTAG]->d_un.d_ptr);
230 while (1)
232 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
234 /* Compare the version strings. */
235 if (strcmp (string, strtab + aux->vda_name) == 0)
236 /* Bingo! */
237 return 1;
239 /* If no more definitions we failed to find what we want. */
240 if (def->vd_next == 0)
241 break;
243 /* Next definition. */
244 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
247 return 0;
250 unsigned int _dl_skip_args; /* Nonzero if we were run directly. */
252 static void
253 dl_main (const ElfW(Phdr) *phdr,
254 ElfW(Half) phent,
255 ElfW(Addr) *user_entry)
257 const ElfW(Phdr) *ph;
258 struct link_map *main_map;
259 int lazy;
260 enum { normal, list, verify, trace } mode;
261 struct link_map **preloads;
262 unsigned int npreloads;
263 const char *preloadlist;
264 size_t file_size;
265 char *file;
266 int has_interp = 0;
268 /* Test whether we want to see the content of the auxiliary array passed
269 up from the kernel. */
270 if (getenv ("LD_SHOW_AUXV") != NULL)
271 _dl_show_auxv ();
273 mode = getenv ("LD_TRACE_LOADED_OBJECTS") != NULL ? trace : normal;
274 _dl_verbose = *(getenv ("LD_WARN") ?: "") == '\0' ? 0 : 1;
276 /* LAZY is determined by the environment variable LD_WARN and
277 LD_BIND_NOW if we trace the binary. */
278 if (mode == trace)
279 lazy = (_dl_verbose
280 ? (*(getenv ("LD_BIND_NOW") ?: "") == '\0' ? 1 : 0) : -1);
281 else
282 lazy = !__libc_enable_secure && *(getenv ("LD_BIND_NOW") ?: "") == '\0';
284 /* See whether we want to use profiling. */
285 _dl_profile = getenv ("LD_PROFILE");
286 if (_dl_profile != NULL)
287 if (_dl_profile[0] == '\0')
288 /* An empty string is of not much help. Disable profiling. */
289 _dl_profile = NULL;
290 else
292 /* OK, we have the name of a shared object we want to
293 profile. It's up to the user to provide a good name, it
294 must match the file name or soname of one of the loaded
295 objects. Now let's see where we are supposed to place the
296 result. */
297 _dl_profile_output = getenv ("LD_PROFILE_OUTPUT");
299 if (_dl_profile_output == NULL || _dl_profile_output[0] == '\0')
300 /* This is the default place. */
301 _dl_profile_output = "/var/tmp";
304 /* Set up a flag which tells we are just starting. */
305 _dl_starting_up = 1;
307 if (*user_entry == (ElfW(Addr)) &ENTRY_POINT)
309 /* Ho ho. We are not the program interpreter! We are the program
310 itself! This means someone ran ld.so as a command. Well, that
311 might be convenient to do sometimes. We support it by
312 interpreting the args like this:
314 ld.so PROGRAM ARGS...
316 The first argument is the name of a file containing an ELF
317 executable we will load and run with the following arguments.
318 To simplify life here, PROGRAM is searched for using the
319 normal rules for shared objects, rather than $PATH or anything
320 like that. We just load it and use its entry point; we don't
321 pay attention to its PT_INTERP command (we are the interpreter
322 ourselves). This is an easy way to test a new ld.so before
323 installing it. */
325 /* Overwrites LD_LIBRARY_PATH if given. */
326 const char *library_path = NULL;
328 /* Note the place where the dynamic linker actually came from. */
329 _dl_rtld_map.l_name = _dl_argv[0];
331 while (_dl_argc > 1)
332 if (! strcmp (_dl_argv[1], "--list"))
334 mode = list;
335 lazy = -1; /* This means do no dependency analysis. */
337 ++_dl_skip_args;
338 --_dl_argc;
339 ++_dl_argv;
341 else if (! strcmp (_dl_argv[1], "--verify"))
343 mode = verify;
345 ++_dl_skip_args;
346 --_dl_argc;
347 ++_dl_argv;
349 else if (! strcmp (_dl_argv[1], "--library-path")
350 && _dl_argc > 2)
352 library_path = _dl_argv[2];
354 _dl_skip_args += 2;
355 _dl_argc -= 2;
356 _dl_argv += 2;
358 else
359 break;
361 /* If we have no further argument the program was called incorrectly.
362 Grant the user some education. */
363 if (_dl_argc < 2)
364 _dl_sysdep_fatal ("\
365 Usage: ld.so [--list|--verify] EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
366 You have invoked `ld.so', the helper program for shared library executables.\n\
367 This program usually lives in the file `/lib/ld.so', and special directives\n\
368 in executable files using ELF shared libraries tell the system's program\n\
369 loader to load the helper program from this file. This helper program loads\n\
370 the shared libraries needed by the program executable, prepares the program\n\
371 to run, and runs it. You may invoke this helper program directly from the\n\
372 command line to load and run an ELF executable file; this is like executing\n\
373 that file itself, but always uses this helper program from the file you\n\
374 specified, instead of the helper program file specified in the executable\n\
375 file you run. This is mostly of use for maintainers to test new versions\n\
376 of this helper program; chances are you did not intend to run this program.\n",
377 NULL);
379 ++_dl_skip_args;
380 --_dl_argc;
381 ++_dl_argv;
383 /* Initialize the data structures for the search paths for shared
384 objects. */
385 _dl_init_paths (library_path);
387 if (mode == verify)
389 char *err_str = NULL;
390 const char *obj_name __attribute__ ((unused));
391 struct map_args args;
393 args.str = _dl_argv[0];
394 (void) _dl_catch_error (&err_str, &obj_name, map_doit, &args);
395 main_map = args.main_map;
396 if (err_str != NULL)
398 free (err_str);
399 _exit (EXIT_FAILURE);
402 else
403 main_map = _dl_map_object (NULL, _dl_argv[0], 0, lt_library, 0);
405 phdr = main_map->l_phdr;
406 phent = main_map->l_phnum;
407 main_map->l_name = (char *) "";
408 *user_entry = main_map->l_entry;
410 else
412 /* Create a link_map for the executable itself.
413 This will be what dlopen on "" returns. */
414 main_map = _dl_new_object ((char *) "", "", lt_executable);
415 if (main_map == NULL)
416 _dl_sysdep_fatal ("cannot allocate memory for link map\n", NULL);
417 main_map->l_phdr = phdr;
418 main_map->l_phnum = phent;
419 main_map->l_entry = *user_entry;
420 main_map->l_opencount = 1;
422 /* Initialize the data structures for the search paths for shared
423 objects. */
424 _dl_init_paths (NULL);
427 /* Scan the program header table for the dynamic section. */
428 for (ph = phdr; ph < &phdr[phent]; ++ph)
429 switch (ph->p_type)
431 case PT_PHDR:
432 /* Find out the load address. */
433 main_map->l_addr = (ElfW(Addr)) phdr - ph->p_vaddr;
434 break;
435 case PT_DYNAMIC:
436 /* This tells us where to find the dynamic section,
437 which tells us everything we need to do. */
438 main_map->l_ld = (void *) main_map->l_addr + ph->p_vaddr;
439 break;
440 case PT_INTERP:
441 /* This "interpreter segment" was used by the program loader to
442 find the program interpreter, which is this program itself, the
443 dynamic linker. We note what name finds us, so that a future
444 dlopen call or DT_NEEDED entry, for something that wants to link
445 against the dynamic linker as a shared library, will know that
446 the shared object is already loaded. */
447 _dl_rtld_libname.name = (const char *) main_map->l_addr + ph->p_vaddr;
448 _dl_rtld_libname.next = NULL;
449 _dl_rtld_map.l_libname = &_dl_rtld_libname;
451 /* Ordinarilly, we would get additional names for the loader from
452 our DT_SONAME. This can't happen if we were actually linked as
453 a static executable (detect this case when we have no DYNAMIC).
454 If so, assume the filename component of the interpreter path to
455 be our SONAME, and add it to our name list. */
456 if (_dl_rtld_map.l_ld == NULL)
458 char *p = strrchr (_dl_rtld_libname.name, '/');
459 if (p)
461 _dl_rtld_libname2.name = p+1;
462 _dl_rtld_libname2.next = NULL;
463 _dl_rtld_libname.next = &_dl_rtld_libname2;
467 has_interp = 1;
468 break;
470 if (! _dl_rtld_map.l_libname && _dl_rtld_map.l_name)
472 /* We were invoked directly, so the program might not have a
473 PT_INTERP. */
474 _dl_rtld_libname.name = _dl_rtld_map.l_name;
475 _dl_rtld_libname.next = NULL;
476 _dl_rtld_map.l_libname = &_dl_rtld_libname;
478 else
479 assert (_dl_rtld_map.l_libname); /* How else did we get here? */
481 if (mode == verify)
482 /* We were called just to verify that this is a dynamic executable
483 using us as the program interpreter. */
484 _exit (main_map->l_ld == NULL ? 1 : has_interp ? 0 : 2);
486 /* Extract the contents of the dynamic section for easy access. */
487 elf_get_dynamic_info (main_map->l_ld, main_map->l_info);
488 if (main_map->l_info[DT_HASH])
489 /* Set up our cache of pointers into the hash table. */
490 _dl_setup_hash (main_map);
492 /* Put the link_map for ourselves on the chain so it can be found by
493 name. Note that at this point the global chain of link maps contains
494 exactly one element, which is pointed to by main_map. */
495 if (! _dl_rtld_map.l_name)
496 /* If not invoked directly, the dynamic linker shared object file was
497 found by the PT_INTERP name. */
498 _dl_rtld_map.l_name = (char *) _dl_rtld_map.l_libname->name;
499 _dl_rtld_map.l_type = lt_library;
500 main_map->l_next = &_dl_rtld_map;
501 _dl_rtld_map.l_prev = main_map;
503 /* We have two ways to specify objects to preload: via environment
504 variable and via the file /etc/ld.so.preload. The later can also
505 be used when security is enabled. */
506 preloads = NULL;
507 npreloads = 0;
509 preloadlist = getenv ("LD_PRELOAD");
510 if (preloadlist)
512 /* The LD_PRELOAD environment variable gives list of libraries
513 separated by white space or colons that are loaded before the
514 executable's dependencies and prepended to the global scope
515 list. If the binary is running setuid all elements
516 containing a '/' are ignored since it is insecure. */
517 char *list = strdupa (preloadlist);
518 char *p;
519 while ((p = strsep (&list, " :")) != NULL)
520 if (! __libc_enable_secure || strchr (p, '/') == NULL)
522 struct link_map *new_map = _dl_map_object (NULL, p, 1,
523 lt_library, 0);
524 if (new_map->l_opencount == 1)
525 /* It is no duplicate. */
526 ++npreloads;
530 /* Read the contents of the file. */
531 file = _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size,
532 PROT_READ | PROT_WRITE);
533 if (file)
535 /* Parse the file. It contains names of libraries to be loaded,
536 separated by white spaces or `:'. It may also contain
537 comments introduced by `#'. */
538 char *problem;
539 char *runp;
540 size_t rest;
542 /* Eliminate comments. */
543 runp = file;
544 rest = file_size;
545 while (rest > 0)
547 char *comment = memchr (runp, '#', rest);
548 if (comment == NULL)
549 break;
551 rest -= comment - runp;
553 *comment = ' ';
554 while (--rest > 0 && *++comment != '\n');
557 /* We have one problematic case: if we have a name at the end of
558 the file without a trailing terminating characters, we cannot
559 place the \0. Handle the case separately. */
560 if (file[file_size - 1] != ' ' && file[file_size - 1] != '\t'
561 && file[file_size - 1] != '\n' && file[file_size - 1] != ':')
563 problem = &file[file_size];
564 while (problem > file && problem[-1] != ' ' && problem[-1] != '\t'
565 && problem[-1] != '\n' && problem[-1] != ':')
566 --problem;
568 if (problem > file)
569 problem[-1] = '\0';
571 else
573 problem = NULL;
574 file[file_size - 1] = '\0';
577 if (file != problem)
579 char *p;
580 runp = file + strspn (file, ": \t\n");
581 while ((p = strsep (&runp, ": \t\n")) != NULL)
583 struct link_map *new_map = _dl_map_object (NULL, p, 1,
584 lt_library, 0);
585 if (new_map->l_opencount == 1)
586 /* It is no duplicate. */
587 ++npreloads;
589 if (runp != NULL)
590 runp += strspn (runp, ": \t\n");
594 if (problem != NULL)
596 char *p = strndupa (problem, file_size - (problem - file));
597 struct link_map *new_map = _dl_map_object (NULL, p, 1,
598 lt_library, 0);
599 if (new_map->l_opencount == 1)
600 /* It is no duplicate. */
601 ++npreloads;
604 /* We don't need the file anymore. */
605 __munmap (file, file_size);
608 if (npreloads != 0)
610 /* Set up PRELOADS with a vector of the preloaded libraries. */
611 struct link_map *l;
612 unsigned int i;
613 preloads = __alloca (npreloads * sizeof preloads[0]);
614 l = _dl_rtld_map.l_next; /* End of the chain before preloads. */
615 i = 0;
618 preloads[i++] = l;
619 l = l->l_next;
620 } while (l);
621 assert (i == npreloads);
624 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
625 specified some libraries to load, these are inserted before the actual
626 dependencies in the executable's searchlist for symbol resolution. */
627 _dl_map_object_deps (main_map, preloads, npreloads, mode == trace);
629 #ifndef MAP_ANON
630 /* We are done mapping things, so close the zero-fill descriptor. */
631 __close (_dl_zerofd);
632 _dl_zerofd = -1;
633 #endif
635 /* Remove _dl_rtld_map from the chain. */
636 _dl_rtld_map.l_prev->l_next = _dl_rtld_map.l_next;
637 if (_dl_rtld_map.l_next)
638 _dl_rtld_map.l_next->l_prev = _dl_rtld_map.l_prev;
640 if (_dl_rtld_map.l_opencount)
642 /* Some DT_NEEDED entry referred to the interpreter object itself, so
643 put it back in the list of visible objects. We insert it into the
644 chain in symbol search order because gdb uses the chain's order as
645 its symbol search order. */
646 unsigned int i = 1;
647 while (main_map->l_searchlist[i] != &_dl_rtld_map)
648 ++i;
649 _dl_rtld_map.l_prev = main_map->l_searchlist[i - 1];
650 _dl_rtld_map.l_next = (i + 1 < main_map->l_nsearchlist ?
651 main_map->l_searchlist[i + 1] : NULL);
652 assert (_dl_rtld_map.l_prev->l_next == _dl_rtld_map.l_next);
653 _dl_rtld_map.l_prev->l_next = &_dl_rtld_map;
654 if (_dl_rtld_map.l_next)
656 assert (_dl_rtld_map.l_next->l_prev == _dl_rtld_map.l_prev);
657 _dl_rtld_map.l_next->l_prev = &_dl_rtld_map;
661 /* Now let us see whether all libraries are available in the
662 versions we need. */
664 struct version_check_args args;
665 args.doexit = mode == normal;
666 args.main_map = main_map;
667 _dl_receive_error (print_missing_version, version_check_doit, &args);
670 if (mode != normal)
672 /* We were run just to list the shared libraries. It is
673 important that we do this before real relocation, because the
674 functions we call below for output may no longer work properly
675 after relocation. */
677 int i;
679 if (! _dl_loaded->l_info[DT_NEEDED])
680 _dl_sysdep_message ("\t", "statically linked\n", NULL);
681 else
683 struct link_map *l;
685 for (l = _dl_loaded->l_next; l; l = l->l_next)
686 if (l->l_opencount == 0)
687 /* The library was not found. */
688 _dl_sysdep_message ("\t", l->l_libname->name, " => not found\n",
689 NULL);
690 else
692 char buf[20], *bp;
693 buf[sizeof buf - 1] = '\0';
694 bp = _itoa_word (l->l_addr, &buf[sizeof buf - 1], 16, 0);
695 while ((size_t) (&buf[sizeof buf - 1] - bp)
696 < sizeof l->l_addr * 2)
697 *--bp = '0';
698 _dl_sysdep_message ("\t", l->l_libname->name, " => ",
699 l->l_name, " (0x", bp, ")\n", NULL);
703 if (mode != trace)
704 for (i = 1; i < _dl_argc; ++i)
706 const ElfW(Sym) *ref = NULL;
707 ElfW(Addr) loadbase = _dl_lookup_symbol (_dl_argv[i], &ref,
708 &_dl_default_scope[2],
709 "argument",
710 ELF_MACHINE_JMP_SLOT);
711 char buf[20], *bp;
712 buf[sizeof buf - 1] = '\0';
713 bp = _itoa_word (ref->st_value, &buf[sizeof buf - 1], 16, 0);
714 while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2)
715 *--bp = '0';
716 _dl_sysdep_message (_dl_argv[i], " found at 0x", bp, NULL);
717 buf[sizeof buf - 1] = '\0';
718 bp = _itoa_word (loadbase, &buf[sizeof buf - 1], 16, 0);
719 while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2)
720 *--bp = '0';
721 _dl_sysdep_message (" in object at 0x", bp, "\n", NULL);
723 else
725 if (lazy >= 0)
727 /* We have to do symbol dependency testing. */
728 struct relocate_args args;
729 struct link_map *l;
731 args.lazy = lazy;
733 l = _dl_loaded;
734 while (l->l_next)
735 l = l->l_next;
738 if (l != &_dl_rtld_map && l->l_opencount > 0)
740 args.l = l;
741 _dl_receive_error (print_unresolved, relocate_doit,
742 &args);
743 *_dl_global_scope_end = NULL;
745 l = l->l_prev;
746 } while (l);
749 #define VERNEEDTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
750 if (*(getenv ("LD_VERBOSE") ?: "") != '\0')
752 /* Print more information. This means here, print information
753 about the versions needed. */
754 int first = 1;
755 struct link_map *map = _dl_loaded;
757 for (map = _dl_loaded; map != NULL; map = map->l_next)
759 const char *strtab;
760 ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG];
761 ElfW(Verneed) *ent;
763 if (dyn == NULL)
764 continue;
766 strtab = (const char *)
767 (map->l_addr + map->l_info[DT_STRTAB]->d_un.d_ptr);
768 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
770 if (first)
772 _dl_sysdep_message ("\n\tVersion information:\n", NULL);
773 first = 0;
776 _dl_sysdep_message ("\t", (map->l_name[0]
777 ? map->l_name : _dl_argv[0]),
778 ":\n", NULL);
780 while (1)
782 ElfW(Vernaux) *aux;
783 struct link_map *needed;
785 needed = find_needed (strtab + ent->vn_file);
786 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
788 while (1)
790 const char *fname = NULL;
792 _dl_sysdep_message ("\t\t",
793 strtab + ent->vn_file,
794 " (", strtab + aux->vna_name,
795 ") ",
796 (aux->vna_flags
797 & VER_FLG_WEAK
798 ? "[WEAK] " : ""),
799 "=> ", NULL);
801 if (needed != NULL
802 && match_version (strtab+aux->vna_name, needed))
803 fname = needed->l_name;
805 _dl_sysdep_message (fname ?: "not found", "\n",
806 NULL);
808 if (aux->vna_next == 0)
809 /* No more symbols. */
810 break;
812 /* Next symbol. */
813 aux = (ElfW(Vernaux) *) ((char *) aux
814 + aux->vna_next);
817 if (ent->vn_next == 0)
818 /* No more dependencies. */
819 break;
821 /* Next dependency. */
822 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
828 _exit (0);
832 /* Now we have all the objects loaded. Relocate them all except for
833 the dynamic linker itself. We do this in reverse order so that copy
834 relocs of earlier objects overwrite the data written by later
835 objects. We do not re-relocate the dynamic linker itself in this
836 loop because that could result in the GOT entries for functions we
837 call being changed, and that would break us. It is safe to relocate
838 the dynamic linker out of order because it has no copy relocs (we
839 know that because it is self-contained). */
841 struct link_map *l;
842 l = _dl_loaded;
843 while (l->l_next)
844 l = l->l_next;
847 if (l != &_dl_rtld_map)
849 _dl_relocate_object (l, _dl_object_relocation_scope (l), lazy);
850 *_dl_global_scope_end = NULL;
852 l = l->l_prev;
853 } while (l);
855 /* Do any necessary cleanups for the startup OS interface code.
856 We do these now so that no calls are made after rtld re-relocation
857 which might be resolved to different functions than we expect.
858 We cannot do this before relocating the other objects because
859 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
860 _dl_sysdep_start_cleanup ();
862 if (_dl_rtld_map.l_opencount > 0)
863 /* There was an explicit ref to the dynamic linker as a shared lib.
864 Re-relocate ourselves with user-controlled symbol definitions. */
865 _dl_relocate_object (&_dl_rtld_map, &_dl_default_scope[2], 0);
869 /* Initialize _r_debug. */
870 struct r_debug *r = _dl_debug_initialize (_dl_rtld_map.l_addr);
871 struct link_map *l;
873 l = _dl_loaded;
875 #ifdef ELF_MACHINE_DEBUG_SETUP
877 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
879 ELF_MACHINE_DEBUG_SETUP (l, r);
880 ELF_MACHINE_DEBUG_SETUP (&_dl_rtld_map, r);
882 #else
884 if (l->l_info[DT_DEBUG])
885 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
886 with the run-time address of the r_debug structure */
887 l->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
889 /* Fill in the pointer in the dynamic linker's own dynamic section, in
890 case you run gdb on the dynamic linker directly. */
891 if (_dl_rtld_map.l_info[DT_DEBUG])
892 _dl_rtld_map.l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
894 #endif
896 /* Notify the debugger that all objects are now mapped in. */
897 r->r_state = RT_ADD;
898 _dl_debug_state ();
901 /* Now enable profiling if needed. */
902 if (_dl_profile_map != NULL)
903 /* We must prepare the profiling. */
904 _dl_start_profile (_dl_profile_map, _dl_profile_output);
906 /* Once we return, _dl_sysdep_start will invoke
907 the DT_INIT functions and then *USER_ENTRY. */
910 /* This is a little helper function for resolving symbols while
911 tracing the binary. */
912 static void
913 print_unresolved (int errcode __attribute__ ((unused)), const char *objname,
914 const char *errstring)
916 if (objname[0] == '\0')
917 objname = _dl_argv[0] ?: "<main program>";
918 _dl_sysdep_error (errstring, " (", objname, ")\n", NULL);
921 /* This is a little helper function for resolving symbols while
922 tracing the binary. */
923 static void
924 print_missing_version (int errcode __attribute__ ((unused)),
925 const char *objname, const char *errstring)
927 _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>", ": ",
928 objname, ": ", errstring, "\n", NULL);