Update.
[glibc.git] / elf / rtld.c
blob7189ca6b2b13aae9cc52d3a03e5a6b94930b77fb
1 /* Run time dynamic linker.
2 Copyright (C) 1995, 1996, 1997 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);
57 int _dl_argc;
58 char **_dl_argv;
59 const char *_dl_rpath;
60 int _dl_verbose;
61 const char *_dl_platform;
62 size_t _dl_platformlen;
63 struct r_search_path *_dl_search_paths;
65 /* Set nonzero during loading and initialization of executable and
66 libraries, cleared before the executable's entry point runs. This
67 must not be initialized to nonzero, because the unused dynamic
68 linker loaded in for libc.so's "ld.so.1" dep will provide the
69 definition seen by libc.so's initializer; that value must be zero,
70 and will be since that dynamic linker's _dl_start and dl_main will
71 never be called. */
72 int _dl_starting_up;
74 static void dl_main (const ElfW(Phdr) *phdr,
75 ElfW(Half) phent,
76 ElfW(Addr) *user_entry);
78 struct link_map _dl_rtld_map;
79 struct libname_list _dl_rtld_libname;
81 #ifdef RTLD_START
82 RTLD_START
83 #else
84 #error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
85 #endif
87 static ElfW(Addr)
88 _dl_start (void *arg)
90 struct link_map bootstrap_map;
92 /* This #define produces dynamic linking inline functions for
93 bootstrap relocation instead of general-purpose relocation. */
94 #define RTLD_BOOTSTRAP
95 #define RESOLVE(sym, version, flags) bootstrap_map.l_addr
96 #include "dynamic-link.h"
98 /* Figure out the run-time load address of the dynamic linker itself. */
99 bootstrap_map.l_addr = elf_machine_load_address ();
101 /* Read our own dynamic section and fill in the info array. */
102 bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
103 elf_get_dynamic_info (bootstrap_map.l_ld, bootstrap_map.l_info);
105 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
106 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info);
107 #endif
109 /* Relocate ourselves so we can do normal function calls and
110 data access using the global offset table. */
112 ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0);
114 /* Now life is sane; we can call functions and access global data.
115 Set up to use the operating system facilities, and find out from
116 the operating system's program loader where to find the program
117 header table in core. */
120 /* Transfer data about ourselves to the permanent link_map structure. */
121 _dl_rtld_map.l_addr = bootstrap_map.l_addr;
122 _dl_rtld_map.l_ld = bootstrap_map.l_ld;
123 memcpy (_dl_rtld_map.l_info, bootstrap_map.l_info,
124 sizeof _dl_rtld_map.l_info);
125 _dl_setup_hash (&_dl_rtld_map);
127 /* Cache the DT_RPATH stored in ld.so itself; this will be
128 the default search path. */
129 _dl_rpath = (void *) (_dl_rtld_map.l_addr +
130 _dl_rtld_map.l_info[DT_STRTAB]->d_un.d_ptr +
131 _dl_rtld_map.l_info[DT_RPATH]->d_un.d_val);
133 /* Call the OS-dependent function to set up life so we can do things like
134 file access. It will call `dl_main' (below) to do all the real work
135 of the dynamic linker, and then unwind our frame and run the user
136 entry point on the same stack we entered on. */
137 return _dl_sysdep_start (arg, &dl_main);
141 /* Now life is peachy; we can do all normal operations.
142 On to the real work. */
144 void ENTRY_POINT (void);
146 /* Some helper functions. */
148 /* Arguments to relocate_doit. */
149 struct relocate_args
151 struct link_map *l;
152 int lazy;
155 struct map_args
157 /* Argument to map_doit. */
158 char *str;
159 /* Return value of map_doit. */
160 struct link_map *main_map;
163 /* Arguments to version_check_doit. */
164 struct version_check_args
166 struct link_map *main_map;
167 int doexit;
170 static void
171 relocate_doit (void *a)
173 struct relocate_args *args = (struct relocate_args *) a;
175 _dl_relocate_object (args->l, _dl_object_relocation_scope (args->l),
176 args->lazy);
179 static void
180 map_doit (void *a)
182 struct map_args *args = (struct map_args *)a;
183 args->main_map = _dl_map_object (NULL, args->str, lt_library, 0);
186 static void
187 version_check_doit (void *a)
189 struct version_check_args *args = (struct version_check_args *)a;
190 if (_dl_check_all_versions (args->main_map, 1) && args->doexit)
191 /* We cannot start the application. Abort now. */
192 _exit (1);
196 static inline struct link_map *
197 find_needed (const char *name)
199 unsigned int n;
201 for (n = 0; n < _dl_loaded->l_nsearchlist; ++n)
202 if (_dl_name_match_p (name, _dl_loaded->l_searchlist[n]))
203 return _dl_loaded->l_searchlist[n];
205 /* Should never happen. */
206 return NULL;
209 static int
210 match_version (const char *string, struct link_map *map)
212 const char *strtab = (const char *) (map->l_addr
213 + map->l_info[DT_STRTAB]->d_un.d_ptr);
214 ElfW(Verdef) *def;
216 #define VERDEFTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
217 if (map->l_info[VERDEFTAG] == NULL)
218 /* The file has no symbol versioning. */
219 return 0;
221 def = (ElfW(Verdef) *) ((char *) map->l_addr
222 + map->l_info[VERDEFTAG]->d_un.d_ptr);
223 while (1)
225 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
227 /* Compare the version strings. */
228 if (strcmp (string, strtab + aux->vda_name) == 0)
229 /* Bingo! */
230 return 1;
232 /* If no more definitions we failed to find what we want. */
233 if (def->vd_next == 0)
234 break;
236 /* Next definition. */
237 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
240 return 0;
243 unsigned int _dl_skip_args; /* Nonzero if we were run directly. */
245 static void
246 dl_main (const ElfW(Phdr) *phdr,
247 ElfW(Half) phent,
248 ElfW(Addr) *user_entry)
250 const ElfW(Phdr) *ph;
251 struct link_map *main_map;
252 int lazy;
253 enum { normal, list, verify, trace } mode;
254 struct link_map **preloads;
255 unsigned int npreloads;
256 const char *preloadlist;
257 size_t file_size;
258 char *file;
259 int has_interp = 0;
261 mode = getenv ("LD_TRACE_LOADED_OBJECTS") != NULL ? trace : normal;
262 _dl_verbose = *(getenv ("LD_WARN") ?: "") == '\0' ? 0 : 1;
264 /* LAZY is determined by the environment variable LD_WARN and
265 LD_BIND_NOW if we trace the binary. */
266 if (mode == trace)
267 lazy = (_dl_verbose
268 ? (*(getenv ("LD_BIND_NOW") ?: "") == '\0' ? 1 : 0) : -1);
269 else
270 lazy = !__libc_enable_secure && *(getenv ("LD_BIND_NOW") ?: "") == '\0';
272 /* Set up a flag which tells we are just starting. */
273 _dl_starting_up = 1;
275 if (*user_entry == (ElfW(Addr)) &ENTRY_POINT)
277 /* Ho ho. We are not the program interpreter! We are the program
278 itself! This means someone ran ld.so as a command. Well, that
279 might be convenient to do sometimes. We support it by
280 interpreting the args like this:
282 ld.so PROGRAM ARGS...
284 The first argument is the name of a file containing an ELF
285 executable we will load and run with the following arguments.
286 To simplify life here, PROGRAM is searched for using the
287 normal rules for shared objects, rather than $PATH or anything
288 like that. We just load it and use its entry point; we don't
289 pay attention to its PT_INTERP command (we are the interpreter
290 ourselves). This is an easy way to test a new ld.so before
291 installing it. */
292 if (_dl_argc < 2)
293 _dl_sysdep_fatal ("\
294 Usage: ld.so [--list|--verify] EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
295 You have invoked `ld.so', the helper program for shared library executables.\n\
296 This program usually lives in the file `/lib/ld.so', and special directives\n\
297 in executable files using ELF shared libraries tell the system's program\n\
298 loader to load the helper program from this file. This helper program loads\n\
299 the shared libraries needed by the program executable, prepares the program\n\
300 to run, and runs it. You may invoke this helper program directly from the\n\
301 command line to load and run an ELF executable file; this is like executing\n\
302 that file itself, but always uses this helper program from the file you\n\
303 specified, instead of the helper program file specified in the executable\n\
304 file you run. This is mostly of use for maintainers to test new versions\n\
305 of this helper program; chances are you did not intend to run this program.\n",
306 NULL);
308 /* Note the place where the dynamic linker actually came from. */
309 _dl_rtld_map.l_name = _dl_argv[0];
311 while (_dl_argc > 1)
312 if (! strcmp (_dl_argv[1], "--list"))
314 mode = list;
315 lazy = -1; /* This means do no dependency analysis. */
317 ++_dl_skip_args;
318 --_dl_argc;
319 ++_dl_argv;
321 else if (! strcmp (_dl_argv[1], "--verify"))
323 mode = verify;
325 ++_dl_skip_args;
326 --_dl_argc;
327 ++_dl_argv;
329 else
330 break;
332 ++_dl_skip_args;
333 --_dl_argc;
334 ++_dl_argv;
336 if (mode == verify)
338 char *err_str = NULL;
339 const char *obj_name __attribute__ ((unused));
340 struct map_args args;
342 args.str = _dl_argv[0];
343 (void) _dl_catch_error (&err_str, &obj_name, map_doit, &args);
344 main_map = args.main_map;
345 if (err_str != NULL)
347 free (err_str);
348 _exit (EXIT_FAILURE);
351 else
352 main_map = _dl_map_object (NULL, _dl_argv[0], lt_library, 0);
354 phdr = main_map->l_phdr;
355 phent = main_map->l_phnum;
356 main_map->l_name = (char *) "";
357 *user_entry = main_map->l_entry;
359 else
361 /* Create a link_map for the executable itself.
362 This will be what dlopen on "" returns. */
363 main_map = _dl_new_object ((char *) "", "", lt_executable);
364 if (main_map == NULL)
365 _dl_sysdep_fatal ("cannot allocate memory for link map\n", NULL);
366 main_map->l_phdr = phdr;
367 main_map->l_phnum = phent;
368 main_map->l_entry = *user_entry;
369 main_map->l_opencount = 1;
372 /* Scan the program header table for the dynamic section. */
373 for (ph = phdr; ph < &phdr[phent]; ++ph)
374 switch (ph->p_type)
376 case PT_DYNAMIC:
377 /* This tells us where to find the dynamic section,
378 which tells us everything we need to do. */
379 main_map->l_ld = (void *) main_map->l_addr + ph->p_vaddr;
380 break;
381 case PT_INTERP:
382 /* This "interpreter segment" was used by the program loader to
383 find the program interpreter, which is this program itself, the
384 dynamic linker. We note what name finds us, so that a future
385 dlopen call or DT_NEEDED entry, for something that wants to link
386 against the dynamic linker as a shared library, will know that
387 the shared object is already loaded. */
388 _dl_rtld_libname.name = (const char *) main_map->l_addr + ph->p_vaddr;
389 _dl_rtld_libname.next = NULL;
390 _dl_rtld_map.l_libname = &_dl_rtld_libname;
391 has_interp = 1;
392 break;
394 if (! _dl_rtld_map.l_libname && _dl_rtld_map.l_name)
396 /* We were invoked directly, so the program might not have a
397 PT_INTERP. */
398 _dl_rtld_libname.name = _dl_rtld_map.l_name;
399 _dl_rtld_libname.next = NULL;
400 _dl_rtld_map.l_libname = &_dl_rtld_libname;
402 else
403 assert (_dl_rtld_map.l_libname); /* How else did we get here? */
405 if (mode == verify)
406 /* We were called just to verify that this is a dynamic executable
407 using us as the program interpreter. */
408 _exit (main_map->l_ld == NULL ? 1 : has_interp ? 0 : 2);
410 /* Extract the contents of the dynamic section for easy access. */
411 elf_get_dynamic_info (main_map->l_ld, main_map->l_info);
412 if (main_map->l_info[DT_HASH])
413 /* Set up our cache of pointers into the hash table. */
414 _dl_setup_hash (main_map);
416 /* Put the link_map for ourselves on the chain so it can be found by
417 name. Note that at this point the global chain of link maps contains
418 exactly one element, which is pointed to by main_map. */
419 if (! _dl_rtld_map.l_name)
420 /* If not invoked directly, the dynamic linker shared object file was
421 found by the PT_INTERP name. */
422 _dl_rtld_map.l_name = (char *) _dl_rtld_map.l_libname->name;
423 _dl_rtld_map.l_type = lt_library;
424 main_map->l_next = &_dl_rtld_map;
425 _dl_rtld_map.l_prev = main_map;
427 /* We have two ways to specify objects to preload: via environment
428 variable and via the file /etc/ld.so.preload. The later can also
429 be used when security is enabled. */
430 preloads = NULL;
431 npreloads = 0;
433 preloadlist = getenv ("LD_PRELOAD");
434 if (preloadlist)
436 /* The LD_PRELOAD environment variable gives a white space
437 separated list of libraries that are loaded before the
438 executable's dependencies and prepended to the global scope
439 list. If the binary is running setuid all elements
440 containing a '/' are ignored since it is insecure. */
441 char *list = strdupa (preloadlist);
442 char *p;
443 while ((p = strsep (&list, " ")) != NULL)
444 if (! __libc_enable_secure || strchr (p, '/') == NULL)
446 (void) _dl_map_object (NULL, p, lt_library, 0);
447 ++npreloads;
451 /* Read the contents of the file. */
452 file = _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size,
453 PROT_READ | PROT_WRITE);
454 if (file)
456 /* Parse the file. It contains names of libraries to be loaded,
457 separated by white spaces or `:'. It may also contain
458 comments introduced by `#'. */
459 char *problem;
460 char *runp;
461 size_t rest;
463 /* Eliminate comments. */
464 runp = file;
465 rest = file_size;
466 while (rest > 0)
468 char *comment = memchr (runp, '#', rest);
469 if (comment == NULL)
470 break;
472 rest -= comment - runp;
474 *comment = ' ';
475 while (--rest > 0 && *++comment != '\n');
478 /* We have one problematic case: if we have a name at the end of
479 the file without a trailing terminating characters, we cannot
480 place the \0. Handle the case separately. */
481 if (file[file_size - 1] != ' ' && file[file_size] != '\t'
482 && file[file_size] != '\n')
484 problem = &file[file_size];
485 while (problem > file && problem[-1] != ' ' && problem[-1] != '\t'
486 && problem[-1] != '\n')
487 --problem;
489 if (problem > file)
490 problem[-1] = '\0';
492 else
493 problem = NULL;
495 if (file != problem)
497 char *p;
498 runp = file;
499 while ((p = strsep (&runp, ": \t\n")) != NULL)
501 (void) _dl_map_object (NULL, p, lt_library, 0);
502 ++npreloads;
506 if (problem != NULL)
508 char *p = strndupa (problem, file_size - (problem - file));
509 (void) _dl_map_object (NULL, p, lt_library, 0);
512 /* We don't need the file anymore. */
513 __munmap (file, file_size);
516 if (npreloads != 0)
518 /* Set up PRELOADS with a vector of the preloaded libraries. */
519 struct link_map *l;
520 unsigned int i;
521 preloads = __alloca (npreloads * sizeof preloads[0]);
522 l = _dl_rtld_map.l_next; /* End of the chain before preloads. */
523 i = 0;
526 preloads[i++] = l;
527 l = l->l_next;
528 } while (l);
529 assert (i == npreloads);
532 /* Initialize the data structures for the search paths for shared
533 objects. */
534 _dl_init_paths ();
536 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
537 specified some libraries to load, these are inserted before the actual
538 dependencies in the executable's searchlist for symbol resolution. */
539 _dl_map_object_deps (main_map, preloads, npreloads, mode == trace);
541 #ifndef MAP_ANON
542 /* We are done mapping things, so close the zero-fill descriptor. */
543 __close (_dl_zerofd);
544 _dl_zerofd = -1;
545 #endif
547 /* Remove _dl_rtld_map from the chain. */
548 _dl_rtld_map.l_prev->l_next = _dl_rtld_map.l_next;
549 if (_dl_rtld_map.l_next)
550 _dl_rtld_map.l_next->l_prev = _dl_rtld_map.l_prev;
552 if (_dl_rtld_map.l_opencount)
554 /* Some DT_NEEDED entry referred to the interpreter object itself, so
555 put it back in the list of visible objects. We insert it into the
556 chain in symbol search order because gdb uses the chain's order as
557 its symbol search order. */
558 unsigned int i = 1;
559 while (main_map->l_searchlist[i] != &_dl_rtld_map)
560 ++i;
561 _dl_rtld_map.l_prev = main_map->l_searchlist[i - 1];
562 _dl_rtld_map.l_next = (i + 1 < main_map->l_nsearchlist ?
563 main_map->l_searchlist[i + 1] : NULL);
564 assert (_dl_rtld_map.l_prev->l_next == _dl_rtld_map.l_next);
565 _dl_rtld_map.l_prev->l_next = &_dl_rtld_map;
566 if (_dl_rtld_map.l_next)
568 assert (_dl_rtld_map.l_next->l_prev == _dl_rtld_map.l_prev);
569 _dl_rtld_map.l_next->l_prev = &_dl_rtld_map;
573 /* Now let us see whether all libraries are available in the
574 versions we need. */
576 struct version_check_args args;
577 args.doexit = mode == normal;
578 args.main_map = main_map;
579 _dl_receive_error (print_missing_version, version_check_doit, &args);
582 if (mode != normal)
584 /* We were run just to list the shared libraries. It is
585 important that we do this before real relocation, because the
586 functions we call below for output may no longer work properly
587 after relocation. */
589 int i;
591 if (! _dl_loaded->l_info[DT_NEEDED])
592 _dl_sysdep_message ("\t", "statically linked\n", NULL);
593 else
595 struct link_map *l;
597 for (l = _dl_loaded->l_next; l; l = l->l_next)
598 if (l->l_opencount == 0)
599 /* The library was not found. */
600 _dl_sysdep_message ("\t", l->l_libname->name, " => not found\n",
601 NULL);
602 else
604 char buf[20], *bp;
605 buf[sizeof buf - 1] = '\0';
606 bp = _itoa (l->l_addr, &buf[sizeof buf - 1], 16, 0);
607 while ((size_t) (&buf[sizeof buf - 1] - bp)
608 < sizeof l->l_addr * 2)
609 *--bp = '0';
610 _dl_sysdep_message ("\t", l->l_libname->name, " => ",
611 l->l_name, " (0x", bp, ")\n", NULL);
615 if (mode != trace)
616 for (i = 1; i < _dl_argc; ++i)
618 const ElfW(Sym) *ref = NULL;
619 ElfW(Addr) loadbase = _dl_lookup_symbol (_dl_argv[i], &ref,
620 &_dl_default_scope[2],
621 "argument",
622 ELF_MACHINE_RELOC_NOPLT);
623 char buf[20], *bp;
624 buf[sizeof buf - 1] = '\0';
625 bp = _itoa (ref->st_value, &buf[sizeof buf - 1], 16, 0);
626 while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2)
627 *--bp = '0';
628 _dl_sysdep_message (_dl_argv[i], " found at 0x", bp, NULL);
629 buf[sizeof buf - 1] = '\0';
630 bp = _itoa (loadbase, &buf[sizeof buf - 1], 16, 0);
631 while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2)
632 *--bp = '0';
633 _dl_sysdep_message (" in object at 0x", bp, "\n", NULL);
635 else
637 if (lazy >= 0)
639 /* We have to do symbol dependency testing. */
640 struct relocate_args args;
641 struct link_map *l;
643 args.lazy = lazy;
645 l = _dl_loaded;
646 while (l->l_next)
647 l = l->l_next;
650 if (l != &_dl_rtld_map && l->l_opencount > 0)
652 args.l = l;
653 _dl_receive_error (print_unresolved, relocate_doit,
654 &args);
655 *_dl_global_scope_end = NULL;
657 l = l->l_prev;
658 } while (l);
661 #define VERNEEDTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
662 if (*(getenv ("LD_VERBOSE") ?: "") != '\0')
664 /* Print more information. This means here, print information
665 about the versions needed. */
666 int first = 1;
667 struct link_map *map = _dl_loaded;
669 for (map = _dl_loaded; map != NULL; map = map->l_next)
671 const char *strtab =
672 (const char *) (map->l_addr
673 + map->l_info[DT_STRTAB]->d_un.d_ptr);
674 ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG];
676 if (dyn != NULL)
678 ElfW(Verneed) *ent =
679 (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
681 if (first)
683 _dl_sysdep_message ("\n\tVersion information:\n",
684 NULL);
685 first = 0;
688 _dl_sysdep_message ("\t", (map->l_name[0]
689 ? map->l_name
690 : _dl_argv[0]), ":\n",
691 NULL);
693 while (1)
695 ElfW(Vernaux) *aux;
696 struct link_map *needed;
698 needed = find_needed (strtab + ent->vn_file);
699 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
701 while (1)
703 const char *fname = NULL;
705 _dl_sysdep_message ("\t\t",
706 strtab + ent->vn_file,
707 " (", strtab + aux->vna_name,
708 ") ",
709 (aux->vna_flags
710 & VER_FLG_WEAK
711 ? "[WEAK] " : ""),
712 "=> ", NULL);
714 if (needed != NULL
715 && match_version (strtab + aux->vna_name,
716 needed))
717 fname = needed->l_name;
719 _dl_sysdep_message (fname ?: "not found", "\n",
720 NULL);
722 if (aux->vna_next == 0)
723 /* No more symbols. */
724 break;
726 /* Next symbol. */
727 aux = (ElfW(Vernaux) *) ((char *) aux
728 + aux->vna_next);
731 if (ent->vn_next == 0)
732 /* No more dependencies. */
733 break;
735 /* Next dependency. */
736 ent = (ElfW(Verneed) *) ((char *) ent
737 + ent->vn_next);
744 _exit (0);
748 /* Now we have all the objects loaded. Relocate them all except for
749 the dynamic linker itself. We do this in reverse order so that copy
750 relocs of earlier objects overwrite the data written by later
751 objects. We do not re-relocate the dynamic linker itself in this
752 loop because that could result in the GOT entries for functions we
753 call being changed, and that would break us. It is safe to relocate
754 the dynamic linker out of order because it has no copy relocs (we
755 know that because it is self-contained). */
757 struct link_map *l;
758 l = _dl_loaded;
759 while (l->l_next)
760 l = l->l_next;
763 if (l != &_dl_rtld_map)
765 _dl_relocate_object (l, _dl_object_relocation_scope (l), lazy);
766 *_dl_global_scope_end = NULL;
768 l = l->l_prev;
769 } while (l);
771 /* Do any necessary cleanups for the startup OS interface code.
772 We do these now so that no calls are made after rtld re-relocation
773 which might be resolved to different functions than we expect.
774 We cannot do this before relocating the other objects because
775 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
776 _dl_sysdep_start_cleanup ();
778 if (_dl_rtld_map.l_opencount > 0)
779 /* There was an explicit ref to the dynamic linker as a shared lib.
780 Re-relocate ourselves with user-controlled symbol definitions. */
781 _dl_relocate_object (&_dl_rtld_map, &_dl_default_scope[2], 0);
785 /* Initialize _r_debug. */
786 struct r_debug *r = _dl_debug_initialize (_dl_rtld_map.l_addr);
787 struct link_map *l;
789 l = _dl_loaded;
791 #ifdef ELF_MACHINE_DEBUG_SETUP
793 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
795 ELF_MACHINE_DEBUG_SETUP (l, r);
796 ELF_MACHINE_DEBUG_SETUP (&_dl_rtld_map, r);
798 #else
800 if (l->l_info[DT_DEBUG])
801 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
802 with the run-time address of the r_debug structure */
803 l->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
805 /* Fill in the pointer in the dynamic linker's own dynamic section, in
806 case you run gdb on the dynamic linker directly. */
807 if (_dl_rtld_map.l_info[DT_DEBUG])
808 _dl_rtld_map.l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
810 #endif
812 /* Notify the debugger that all objects are now mapped in. */
813 r->r_state = RT_ADD;
814 _dl_debug_state ();
817 /* Once we return, _dl_sysdep_start will invoke
818 the DT_INIT functions and then *USER_ENTRY. */
821 /* This is a little helper function for resolving symbols while
822 tracing the binary. */
823 static void
824 print_unresolved (int errcode __attribute__ ((unused)), const char *objname,
825 const char *errstring)
827 _dl_sysdep_error (errstring, " (", objname, ")\n", NULL);
830 /* This is a little helper function for resolving symbols while
831 tracing the binary. */
832 static void
833 print_missing_version (int errcode __attribute__ ((unused)),
834 const char *objname, const char *errstring)
836 _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>", ": ",
837 objname, ": ", errstring, "\n", NULL);