Test for hton/ntoh functions.
[glibc/history.git] / elf / rtld.c
bloba025757e0500f2dc5b7469f9127e46dd1ade16d1
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;
62 /* Set nonzero during loading and initialization of executable and
63 libraries, cleared before the executable's entry point runs. This
64 must not be initialized to nonzero, because the unused dynamic
65 linker loaded in for libc.so's "ld.so.1" dep will provide the
66 definition seen by libc.so's initializer; that value must be zero,
67 and will be since that dynamic linker's _dl_start and dl_main will
68 never be called. */
69 int _dl_starting_up;
71 static void dl_main (const ElfW(Phdr) *phdr,
72 ElfW(Half) phent,
73 ElfW(Addr) *user_entry);
75 struct link_map _dl_rtld_map;
76 struct libname_list _dl_rtld_libname;
78 #ifdef RTLD_START
79 RTLD_START
80 #else
81 #error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
82 #endif
84 static ElfW(Addr)
85 _dl_start (void *arg)
87 struct link_map bootstrap_map;
89 /* This #define produces dynamic linking inline functions for
90 bootstrap relocation instead of general-purpose relocation. */
91 #define RTLD_BOOTSTRAP
92 #define RESOLVE(sym, version, flags) bootstrap_map.l_addr
93 #include "dynamic-link.h"
95 /* Figure out the run-time load address of the dynamic linker itself. */
96 bootstrap_map.l_addr = elf_machine_load_address ();
98 /* Read our own dynamic section and fill in the info array. */
99 bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
100 elf_get_dynamic_info (bootstrap_map.l_ld, bootstrap_map.l_info);
102 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
103 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info);
104 #endif
106 /* Relocate ourselves so we can do normal function calls and
107 data access using the global offset table. */
109 ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0);
111 /* Now life is sane; we can call functions and access global data.
112 Set up to use the operating system facilities, and find out from
113 the operating system's program loader where to find the program
114 header table in core. */
117 /* Transfer data about ourselves to the permanent link_map structure. */
118 _dl_rtld_map.l_addr = bootstrap_map.l_addr;
119 _dl_rtld_map.l_ld = bootstrap_map.l_ld;
120 memcpy (_dl_rtld_map.l_info, bootstrap_map.l_info,
121 sizeof _dl_rtld_map.l_info);
122 _dl_setup_hash (&_dl_rtld_map);
124 /* Cache the DT_RPATH stored in ld.so itself; this will be
125 the default search path. */
126 _dl_rpath = (void *) (_dl_rtld_map.l_addr +
127 _dl_rtld_map.l_info[DT_STRTAB]->d_un.d_ptr +
128 _dl_rtld_map.l_info[DT_RPATH]->d_un.d_val);
130 /* Call the OS-dependent function to set up life so we can do things like
131 file access. It will call `dl_main' (below) to do all the real work
132 of the dynamic linker, and then unwind our frame and run the user
133 entry point on the same stack we entered on. */
134 return _dl_sysdep_start (arg, &dl_main);
138 /* Now life is peachy; we can do all normal operations.
139 On to the real work. */
141 void ENTRY_POINT (void);
143 /* Some helper functions. */
145 /* Arguments to relocate_doit. */
146 struct relocate_args
148 struct link_map *l;
149 int lazy;
152 struct map_args
154 /* Argument to map_doit. */
155 char *str;
156 /* Return value of map_doit. */
157 struct link_map *main_map;
160 /* Arguments to version_check_doit. */
161 struct version_check_args
163 struct link_map *main_map;
164 int doexit;
167 static void
168 relocate_doit (void *a)
170 struct relocate_args *args = (struct relocate_args *) a;
172 _dl_relocate_object (args->l, _dl_object_relocation_scope (args->l),
173 args->lazy);
176 static void
177 map_doit (void *a)
179 struct map_args *args = (struct map_args *)a;
180 args->main_map = _dl_map_object (NULL, args->str, lt_library, 0);
183 static void
184 version_check_doit (void *a)
186 struct version_check_args *args = (struct version_check_args *)a;
187 if (_dl_check_all_versions (args->main_map, 1) && args->doexit)
188 /* We cannot start the application. Abort now. */
189 _exit (1);
193 static inline struct link_map *
194 find_needed (const char *name)
196 unsigned int n;
198 for (n = 0; n < _dl_loaded->l_nsearchlist; ++n)
199 if (_dl_name_match_p (name, _dl_loaded->l_searchlist[n]))
200 return _dl_loaded->l_searchlist[n];
202 /* Should never happen. */
203 return NULL;
206 static int
207 match_version (const char *string, struct link_map *map)
209 const char *strtab = (const char *) (map->l_addr
210 + map->l_info[DT_STRTAB]->d_un.d_ptr);
211 ElfW(Verdef) *def;
213 #define VERDEFTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
214 if (map->l_info[VERDEFTAG] == NULL)
215 /* The file has no symbol versioning. */
216 return 0;
218 def = (ElfW(Verdef) *) ((char *) map->l_addr
219 + map->l_info[VERDEFTAG]->d_un.d_ptr);
220 while (1)
222 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
224 /* Compare the version strings. */
225 if (strcmp (string, strtab + aux->vda_name) == 0)
226 /* Bingo! */
227 return 1;
229 /* If no more definitions we failed to find what we want. */
230 if (def->vd_next == 0)
231 break;
233 /* Next definition. */
234 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
237 return 0;
240 unsigned int _dl_skip_args; /* Nonzero if we were run directly. */
242 static void
243 dl_main (const ElfW(Phdr) *phdr,
244 ElfW(Half) phent,
245 ElfW(Addr) *user_entry)
247 const ElfW(Phdr) *ph;
248 struct link_map *main_map;
249 int lazy;
250 enum { normal, list, verify, trace } mode;
251 struct link_map **preloads;
252 unsigned int npreloads;
253 const char *preloadlist;
254 size_t file_size;
255 char *file;
256 int has_interp = 0;
258 mode = getenv ("LD_TRACE_LOADED_OBJECTS") != NULL ? trace : normal;
259 _dl_verbose = *(getenv ("LD_WARN") ?: "") == '\0' ? 0 : 1;
261 /* LAZY is determined by the environment variable LD_WARN and
262 LD_BIND_NOW if we trace the binary. */
263 if (mode == trace)
264 lazy = (_dl_verbose
265 ? (*(getenv ("LD_BIND_NOW") ?: "") == '\0' ? 1 : 0) : -1);
266 else
267 lazy = !__libc_enable_secure && *(getenv ("LD_BIND_NOW") ?: "") == '\0';
269 /* Set up a flag which tells we are just starting. */
270 _dl_starting_up = 1;
272 if (*user_entry == (ElfW(Addr)) &ENTRY_POINT)
274 /* Ho ho. We are not the program interpreter! We are the program
275 itself! This means someone ran ld.so as a command. Well, that
276 might be convenient to do sometimes. We support it by
277 interpreting the args like this:
279 ld.so PROGRAM ARGS...
281 The first argument is the name of a file containing an ELF
282 executable we will load and run with the following arguments.
283 To simplify life here, PROGRAM is searched for using the
284 normal rules for shared objects, rather than $PATH or anything
285 like that. We just load it and use its entry point; we don't
286 pay attention to its PT_INTERP command (we are the interpreter
287 ourselves). This is an easy way to test a new ld.so before
288 installing it. */
289 if (_dl_argc < 2)
290 _dl_sysdep_fatal ("\
291 Usage: ld.so [--list|--verify] EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
292 You have invoked `ld.so', the helper program for shared library executables.\n\
293 This program usually lives in the file `/lib/ld.so', and special directives\n\
294 in executable files using ELF shared libraries tell the system's program\n\
295 loader to load the helper program from this file. This helper program loads\n\
296 the shared libraries needed by the program executable, prepares the program\n\
297 to run, and runs it. You may invoke this helper program directly from the\n\
298 command line to load and run an ELF executable file; this is like executing\n\
299 that file itself, but always uses this helper program from the file you\n\
300 specified, instead of the helper program file specified in the executable\n\
301 file you run. This is mostly of use for maintainers to test new versions\n\
302 of this helper program; chances are you did not intend to run this program.\n",
303 NULL);
305 /* Note the place where the dynamic linker actually came from. */
306 _dl_rtld_map.l_name = _dl_argv[0];
308 while (_dl_argc > 1)
309 if (! strcmp (_dl_argv[1], "--list"))
311 mode = list;
312 lazy = -1; /* This means do no dependency analysis. */
314 ++_dl_skip_args;
315 --_dl_argc;
316 ++_dl_argv;
318 else if (! strcmp (_dl_argv[1], "--verify"))
320 mode = verify;
322 ++_dl_skip_args;
323 --_dl_argc;
324 ++_dl_argv;
326 else
327 break;
329 ++_dl_skip_args;
330 --_dl_argc;
331 ++_dl_argv;
333 if (mode == verify)
335 char *err_str = NULL;
336 const char *obj_name __attribute__ ((unused));
337 struct map_args args;
339 args.str = _dl_argv[0];
340 (void) _dl_catch_error (&err_str, &obj_name, map_doit, &args);
341 main_map = args.main_map;
342 if (err_str != NULL)
344 free (err_str);
345 _exit (EXIT_FAILURE);
348 else
349 main_map = _dl_map_object (NULL, _dl_argv[0], lt_library, 0);
351 phdr = main_map->l_phdr;
352 phent = main_map->l_phnum;
353 main_map->l_name = (char *) "";
354 *user_entry = main_map->l_entry;
356 else
358 /* Create a link_map for the executable itself.
359 This will be what dlopen on "" returns. */
360 main_map = _dl_new_object ((char *) "", "", lt_executable);
361 if (main_map == NULL)
362 _dl_sysdep_fatal ("cannot allocate memory for link map\n", NULL);
363 main_map->l_phdr = phdr;
364 main_map->l_phnum = phent;
365 main_map->l_entry = *user_entry;
366 main_map->l_opencount = 1;
369 /* Scan the program header table for the dynamic section. */
370 for (ph = phdr; ph < &phdr[phent]; ++ph)
371 switch (ph->p_type)
373 case PT_DYNAMIC:
374 /* This tells us where to find the dynamic section,
375 which tells us everything we need to do. */
376 main_map->l_ld = (void *) main_map->l_addr + ph->p_vaddr;
377 break;
378 case PT_INTERP:
379 /* This "interpreter segment" was used by the program loader to
380 find the program interpreter, which is this program itself, the
381 dynamic linker. We note what name finds us, so that a future
382 dlopen call or DT_NEEDED entry, for something that wants to link
383 against the dynamic linker as a shared library, will know that
384 the shared object is already loaded. */
385 _dl_rtld_libname.name = (const char *) main_map->l_addr + ph->p_vaddr;
386 _dl_rtld_libname.next = NULL;
387 _dl_rtld_map.l_libname = &_dl_rtld_libname;
388 has_interp = 1;
389 break;
391 if (! _dl_rtld_map.l_libname && _dl_rtld_map.l_name)
393 /* We were invoked directly, so the program might not have a
394 PT_INTERP. */
395 _dl_rtld_libname.name = _dl_rtld_map.l_name;
396 _dl_rtld_libname.next = NULL;
397 _dl_rtld_map.l_libname = &_dl_rtld_libname;
399 else
400 assert (_dl_rtld_map.l_libname); /* How else did we get here? */
402 if (mode == verify)
403 /* We were called just to verify that this is a dynamic executable
404 using us as the program interpreter. */
405 _exit (main_map->l_ld == NULL ? 1 : has_interp ? 0 : 2);
407 /* Extract the contents of the dynamic section for easy access. */
408 elf_get_dynamic_info (main_map->l_ld, main_map->l_info);
409 if (main_map->l_info[DT_HASH])
410 /* Set up our cache of pointers into the hash table. */
411 _dl_setup_hash (main_map);
413 /* Put the link_map for ourselves on the chain so it can be found by
414 name. Note that at this point the global chain of link maps contains
415 exactly one element, which is pointed to by main_map. */
416 if (! _dl_rtld_map.l_name)
417 /* If not invoked directly, the dynamic linker shared object file was
418 found by the PT_INTERP name. */
419 _dl_rtld_map.l_name = (char *) _dl_rtld_map.l_libname->name;
420 _dl_rtld_map.l_type = lt_library;
421 main_map->l_next = &_dl_rtld_map;
422 _dl_rtld_map.l_prev = main_map;
424 /* We have two ways to specify objects to preload: via environment
425 variable and via the file /etc/ld.so.preload. The later can also
426 be used when security is enabled. */
427 preloads = NULL;
428 npreloads = 0;
430 preloadlist = getenv ("LD_PRELOAD");
431 if (preloadlist)
433 /* The LD_PRELOAD environment variable gives a white space
434 separated list of libraries that are loaded before the
435 executable's dependencies and prepended to the global scope
436 list. If the binary is running setuid all elements
437 containing a '/' are ignored since it is insecure. */
438 char *list = strdupa (preloadlist);
439 char *p;
440 while ((p = strsep (&list, " ")) != NULL)
441 if (! __libc_enable_secure || strchr (p, '/') == NULL)
443 (void) _dl_map_object (NULL, p, lt_library, 0);
444 ++npreloads;
448 /* Read the contents of the file. */
449 file = _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size,
450 PROT_READ | PROT_WRITE);
451 if (file)
453 /* Parse the file. It contains names of libraries to be loaded,
454 separated by white spaces or `:'. It may also contain
455 comments introduced by `#'. */
456 char *problem;
457 char *runp;
458 size_t rest;
460 /* Eliminate comments. */
461 runp = file;
462 rest = file_size;
463 while (rest > 0)
465 char *comment = memchr (runp, '#', rest);
466 if (comment == NULL)
467 break;
469 rest -= comment - runp;
471 *comment = ' ';
472 while (--rest > 0 && *++comment != '\n');
475 /* We have one problematic case: if we have a name at the end of
476 the file without a trailing terminating characters, we cannot
477 place the \0. Handle the case separately. */
478 if (file[file_size - 1] != ' ' && file[file_size] != '\t'
479 && file[file_size] != '\n')
481 problem = &file[file_size];
482 while (problem > file && problem[-1] != ' ' && problem[-1] != '\t'
483 && problem[-1] != '\n')
484 --problem;
486 if (problem > file)
487 problem[-1] = '\0';
489 else
490 problem = NULL;
492 if (file != problem)
494 char *p;
495 runp = file;
496 while ((p = strsep (&runp, ": \t\n")) != NULL)
498 (void) _dl_map_object (NULL, p, lt_library, 0);
499 ++npreloads;
503 if (problem != NULL)
505 char *p = strndupa (problem, file_size - (problem - file));
506 (void) _dl_map_object (NULL, p, lt_library, 0);
509 /* We don't need the file anymore. */
510 __munmap (file, file_size);
513 if (npreloads != 0)
515 /* Set up PRELOADS with a vector of the preloaded libraries. */
516 struct link_map *l;
517 unsigned int i;
518 preloads = __alloca (npreloads * sizeof preloads[0]);
519 l = _dl_rtld_map.l_next; /* End of the chain before preloads. */
520 i = 0;
523 preloads[i++] = l;
524 l = l->l_next;
525 } while (l);
526 assert (i == npreloads);
529 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
530 specified some libraries to load, these are inserted before the actual
531 dependencies in the executable's searchlist for symbol resolution. */
532 _dl_map_object_deps (main_map, preloads, npreloads, mode == trace);
534 #ifndef MAP_ANON
535 /* We are done mapping things, so close the zero-fill descriptor. */
536 __close (_dl_zerofd);
537 _dl_zerofd = -1;
538 #endif
540 /* Remove _dl_rtld_map from the chain. */
541 _dl_rtld_map.l_prev->l_next = _dl_rtld_map.l_next;
542 if (_dl_rtld_map.l_next)
543 _dl_rtld_map.l_next->l_prev = _dl_rtld_map.l_prev;
545 if (_dl_rtld_map.l_opencount)
547 /* Some DT_NEEDED entry referred to the interpreter object itself, so
548 put it back in the list of visible objects. We insert it into the
549 chain in symbol search order because gdb uses the chain's order as
550 its symbol search order. */
551 unsigned int i = 1;
552 while (main_map->l_searchlist[i] != &_dl_rtld_map)
553 ++i;
554 _dl_rtld_map.l_prev = main_map->l_searchlist[i - 1];
555 _dl_rtld_map.l_next = (i + 1 < main_map->l_nsearchlist ?
556 main_map->l_searchlist[i + 1] : NULL);
557 assert (_dl_rtld_map.l_prev->l_next == _dl_rtld_map.l_next);
558 _dl_rtld_map.l_prev->l_next = &_dl_rtld_map;
559 if (_dl_rtld_map.l_next)
561 assert (_dl_rtld_map.l_next->l_prev == _dl_rtld_map.l_prev);
562 _dl_rtld_map.l_next->l_prev = &_dl_rtld_map;
566 /* Now let us see whether all libraries are available in the
567 versions we need. */
569 struct version_check_args args;
570 args.doexit = mode == normal;
571 args.main_map = main_map;
572 _dl_receive_error (print_missing_version, version_check_doit, &args);
575 if (mode != normal)
577 /* We were run just to list the shared libraries. It is
578 important that we do this before real relocation, because the
579 functions we call below for output may no longer work properly
580 after relocation. */
582 int i;
584 if (! _dl_loaded->l_info[DT_NEEDED])
585 _dl_sysdep_message ("\t", "statically linked\n", NULL);
586 else
588 struct link_map *l;
590 for (l = _dl_loaded->l_next; l; l = l->l_next)
591 if (l->l_opencount == 0)
592 /* The library was not found. */
593 _dl_sysdep_message ("\t", l->l_libname->name, " => not found\n",
594 NULL);
595 else
597 char buf[20], *bp;
598 buf[sizeof buf - 1] = '\0';
599 bp = _itoa (l->l_addr, &buf[sizeof buf - 1], 16, 0);
600 while ((size_t) (&buf[sizeof buf - 1] - bp)
601 < sizeof l->l_addr * 2)
602 *--bp = '0';
603 _dl_sysdep_message ("\t", l->l_libname->name, " => ",
604 l->l_name, " (0x", bp, ")\n", NULL);
608 if (mode != trace)
609 for (i = 1; i < _dl_argc; ++i)
611 const ElfW(Sym) *ref = NULL;
612 ElfW(Addr) loadbase = _dl_lookup_symbol (_dl_argv[i], &ref,
613 &_dl_default_scope[2],
614 "argument",
615 ELF_MACHINE_RELOC_NOPLT);
616 char buf[20], *bp;
617 buf[sizeof buf - 1] = '\0';
618 bp = _itoa (ref->st_value, &buf[sizeof buf - 1], 16, 0);
619 while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2)
620 *--bp = '0';
621 _dl_sysdep_message (_dl_argv[i], " found at 0x", bp, NULL);
622 buf[sizeof buf - 1] = '\0';
623 bp = _itoa (loadbase, &buf[sizeof buf - 1], 16, 0);
624 while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2)
625 *--bp = '0';
626 _dl_sysdep_message (" in object at 0x", bp, "\n", NULL);
628 else
630 if (lazy >= 0)
632 /* We have to do symbol dependency testing. */
633 struct relocate_args args;
634 struct link_map *l;
636 args.lazy = lazy;
638 l = _dl_loaded;
639 while (l->l_next)
640 l = l->l_next;
643 if (l != &_dl_rtld_map && l->l_opencount > 0)
645 args.l = l;
646 _dl_receive_error (print_unresolved, relocate_doit,
647 &args);
648 *_dl_global_scope_end = NULL;
650 l = l->l_prev;
651 } while (l);
654 #define VERNEEDTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
655 if (*(getenv ("LD_VERBOSE") ?: "") != '\0')
657 /* Print more information. This means here, print information
658 about the versions needed. */
659 int first = 1;
660 struct link_map *map = _dl_loaded;
662 for (map = _dl_loaded; map != NULL; map = map->l_next)
664 const char *strtab =
665 (const char *) (map->l_addr
666 + map->l_info[DT_STRTAB]->d_un.d_ptr);
667 ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG];
669 if (dyn != NULL)
671 ElfW(Verneed) *ent =
672 (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
674 if (first)
676 _dl_sysdep_message ("\n\tVersion information:\n",
677 NULL);
678 first = 0;
681 _dl_sysdep_message ("\t", (map->l_name[0]
682 ? map->l_name
683 : _dl_argv[0]), ":\n",
684 NULL);
686 while (1)
688 ElfW(Vernaux) *aux;
689 struct link_map *needed;
691 needed = find_needed (strtab + ent->vn_file);
692 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
694 while (1)
696 const char *fname = NULL;
698 _dl_sysdep_message ("\t\t",
699 strtab + ent->vn_file,
700 " (", strtab + aux->vna_name,
701 ") ",
702 (aux->vna_flags
703 & VER_FLG_WEAK
704 ? "[WEAK] " : ""),
705 "=> ", NULL);
707 if (needed != NULL
708 && match_version (strtab + aux->vna_name,
709 needed))
710 fname = needed->l_name;
712 _dl_sysdep_message (fname ?: "not found", "\n",
713 NULL);
715 if (aux->vna_next == 0)
716 /* No more symbols. */
717 break;
719 /* Next symbol. */
720 aux = (ElfW(Vernaux) *) ((char *) aux
721 + aux->vna_next);
724 if (ent->vn_next == 0)
725 /* No more dependencies. */
726 break;
728 /* Next dependency. */
729 ent = (ElfW(Verneed) *) ((char *) ent
730 + ent->vn_next);
737 _exit (0);
741 /* Now we have all the objects loaded. Relocate them all except for
742 the dynamic linker itself. We do this in reverse order so that copy
743 relocs of earlier objects overwrite the data written by later
744 objects. We do not re-relocate the dynamic linker itself in this
745 loop because that could result in the GOT entries for functions we
746 call being changed, and that would break us. It is safe to relocate
747 the dynamic linker out of order because it has no copy relocs (we
748 know that because it is self-contained). */
750 struct link_map *l;
751 l = _dl_loaded;
752 while (l->l_next)
753 l = l->l_next;
756 if (l != &_dl_rtld_map)
758 _dl_relocate_object (l, _dl_object_relocation_scope (l), lazy);
759 *_dl_global_scope_end = NULL;
761 l = l->l_prev;
762 } while (l);
764 /* Do any necessary cleanups for the startup OS interface code.
765 We do these now so that no calls are made after rtld re-relocation
766 which might be resolved to different functions than we expect.
767 We cannot do this before relocating the other objects because
768 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
769 _dl_sysdep_start_cleanup ();
771 if (_dl_rtld_map.l_opencount > 0)
772 /* There was an explicit ref to the dynamic linker as a shared lib.
773 Re-relocate ourselves with user-controlled symbol definitions. */
774 _dl_relocate_object (&_dl_rtld_map, &_dl_default_scope[2], 0);
778 /* Initialize _r_debug. */
779 struct r_debug *r = _dl_debug_initialize (_dl_rtld_map.l_addr);
780 struct link_map *l;
782 l = _dl_loaded;
784 #ifdef ELF_MACHINE_DEBUG_SETUP
786 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
788 ELF_MACHINE_DEBUG_SETUP (l, r);
789 ELF_MACHINE_DEBUG_SETUP (&_dl_rtld_map, r);
791 #else
793 if (l->l_info[DT_DEBUG])
794 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
795 with the run-time address of the r_debug structure */
796 l->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
798 /* Fill in the pointer in the dynamic linker's own dynamic section, in
799 case you run gdb on the dynamic linker directly. */
800 if (_dl_rtld_map.l_info[DT_DEBUG])
801 _dl_rtld_map.l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
803 #endif
805 /* Notify the debugger that all objects are now mapped in. */
806 r->r_state = RT_ADD;
807 _dl_debug_state ();
810 /* Once we return, _dl_sysdep_start will invoke
811 the DT_INIT functions and then *USER_ENTRY. */
814 /* This is a little helper function for resolving symbols while
815 tracing the binary. */
816 static void
817 print_unresolved (int errcode __attribute__ ((unused)), const char *objname,
818 const char *errstring)
820 _dl_sysdep_error (errstring, " (", objname, ")\n", NULL);
823 /* This is a little helper function for resolving symbols while
824 tracing the binary. */
825 static void
826 print_missing_version (int errcode __attribute__ ((unused)),
827 const char *objname, const char *errstring)
829 _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>", ": ",
830 objname, ": ", errstring, "\n", NULL);