]> sourceware.org Git - glibc.git/blame - elf/rtld.c
Update.
[glibc.git] / elf / rtld.c
CommitLineData
d66e34cd 1/* Run time dynamic linker.
0c5ecdc4 2 Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
afd4eb37 3 This file is part of the GNU C Library.
d66e34cd 4
afd4eb37
UD
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.
d66e34cd 9
afd4eb37
UD
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.
d66e34cd 14
afd4eb37
UD
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. */
d66e34cd
RM
19
20#include <link.h>
d66e34cd
RM
21#include <stddef.h>
22#include <stdlib.h>
f51d1dfd 23#include <string.h>
d66e34cd 24#include <unistd.h>
2064087b 25#include <sys/mman.h> /* Check if MAP_ANON is defined. */
ce37fa88 26#include <stdio-common/_itoa.h>
b1dbbaa4 27#include <assert.h>
f21acc89 28#include <entry.h>
f5348425
RM
29#include "dynamic-link.h"
30
31
d66e34cd
RM
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
cddcfecf 34 for setting __libc_enable_secure if we need to be secure (e.g. setuid),
d66e34cd 35 and for setting _dl_argc and _dl_argv, and then calling _dl_main. */
266180eb
RM
36extern 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));
4cb20290 40extern void _dl_sysdep_start_cleanup (void);
d66e34cd 41
14bab8de
UD
42/* System-dependent function to read a file's whole contents
43 in the most convenient manner available. */
44extern void *_dl_sysdep_read_whole_file (const char *filename,
45 size_t *filesize_ptr,
46 int mmap_prot);
47
fd26970f 48/* Helper function to handle errors while resolving symbols. */
c84142e8
UD
49static void print_unresolved (int errcode, const char *objname,
50 const char *errsting);
51
52/* Helper function to handle errors when a version is missing. */
53static void print_missing_version (int errcode, const char *objname,
54 const char *errsting);
fd26970f 55
d66e34cd
RM
56int _dl_argc;
57char **_dl_argv;
4cb20290 58const char *_dl_rpath;
cf29ffbe 59int _dl_verbose;
0a54e401
UD
60const char *_dl_platform;
61size_t _dl_platformlen;
f41c8091 62unsigned long _dl_hwcap;
0a54e401 63struct r_search_path *_dl_search_paths;
3996f34b
UD
64const char *_dl_profile;
65const char *_dl_profile_output;
66struct link_map *_dl_profile_map;
d66e34cd 67
39778c6c
UD
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. */
75int _dl_starting_up;
76
266180eb
RM
77static void dl_main (const ElfW(Phdr) *phdr,
78 ElfW(Half) phent,
79 ElfW(Addr) *user_entry);
d66e34cd 80
ee188d55 81struct link_map _dl_rtld_map;
c84142e8 82struct libname_list _dl_rtld_libname;
f41c8091 83struct libname_list _dl_rtld_libname2;
86d2c878 84
b1dbbaa4
RM
85#ifdef RTLD_START
86RTLD_START
87#else
88#error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
89#endif
90
ceb2d9aa 91static ElfW(Addr)
d66e34cd
RM
92_dl_start (void *arg)
93{
86d2c878 94 struct link_map bootstrap_map;
d66e34cd 95
b1dbbaa4
RM
96 /* This #define produces dynamic linking inline functions for
97 bootstrap relocation instead of general-purpose relocation. */
98#define RTLD_BOOTSTRAP
c84142e8 99#define RESOLVE(sym, version, flags) bootstrap_map.l_addr
b1dbbaa4
RM
100#include "dynamic-link.h"
101
d66e34cd 102 /* Figure out the run-time load address of the dynamic linker itself. */
86d2c878 103 bootstrap_map.l_addr = elf_machine_load_address ();
d66e34cd 104
47707456
UD
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 ();
86d2c878 107 elf_get_dynamic_info (bootstrap_map.l_ld, bootstrap_map.l_info);
d66e34cd
RM
108
109#ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
86d2c878 110 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info);
d66e34cd
RM
111#endif
112
113 /* Relocate ourselves so we can do normal function calls and
114 data access using the global offset table. */
421f82e5 115
3996f34b 116 ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0, 0);
421f82e5 117
d66e34cd
RM
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. */
122
86d2c878 123 /* Transfer data about ourselves to the permanent link_map structure. */
ee188d55
RM
124 _dl_rtld_map.l_addr = bootstrap_map.l_addr;
125 _dl_rtld_map.l_ld = bootstrap_map.l_ld;
f41c8091 126 _dl_rtld_map.l_opencount = 1;
ee188d55
RM
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);
86d2c878 130
4cb20290
RM
131 /* Cache the DT_RPATH stored in ld.so itself; this will be
132 the default search path. */
f41c8091
UD
133 if (_dl_rtld_map.l_info[DT_STRTAB] && _dl_rtld_map.l_info[DT_RPATH])
134 {
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);
138 }
d66e34cd
RM
139
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. */
8d6468d0 144 return _dl_sysdep_start (arg, &dl_main);
d66e34cd
RM
145}
146
147
148/* Now life is peachy; we can do all normal operations.
149 On to the real work. */
150
f21acc89 151void ENTRY_POINT (void);
d66e34cd 152
993b3242
UD
153/* Some helper functions. */
154
155/* Arguments to relocate_doit. */
156struct relocate_args
157{
158 struct link_map *l;
159 int lazy;
160};
161
162struct map_args
163{
164 /* Argument to map_doit. */
165 char *str;
166 /* Return value of map_doit. */
167 struct link_map *main_map;
168};
169
170/* Arguments to version_check_doit. */
171struct version_check_args
172{
173 struct link_map *main_map;
174 int doexit;
175};
176
177static void
178relocate_doit (void *a)
179{
180 struct relocate_args *args = (struct relocate_args *) a;
181
182 _dl_relocate_object (args->l, _dl_object_relocation_scope (args->l),
183 args->lazy);
184}
185
186static void
187map_doit (void *a)
188{
189 struct map_args *args = (struct map_args *)a;
190 args->main_map = _dl_map_object (NULL, args->str, lt_library, 0);
191}
192
193static void
194version_check_doit (void *a)
195{
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);
200}
201
ce37fa88
UD
202
203static inline struct link_map *
204find_needed (const char *name)
205{
206 unsigned int n;
207
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];
211
212 /* Should never happen. */
213 return NULL;
214}
215
216static int
217match_version (const char *string, struct link_map *map)
218{
219 const char *strtab = (const char *) (map->l_addr
220 + map->l_info[DT_STRTAB]->d_un.d_ptr);
221 ElfW(Verdef) *def;
222
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;
227
228 def = (ElfW(Verdef) *) ((char *) map->l_addr
229 + map->l_info[VERDEFTAG]->d_un.d_ptr);
230 while (1)
231 {
232 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
233
234 /* Compare the version strings. */
235 if (strcmp (string, strtab + aux->vda_name) == 0)
236 /* Bingo! */
237 return 1;
238
239 /* If no more definitions we failed to find what we want. */
240 if (def->vd_next == 0)
241 break;
242
243 /* Next definition. */
244 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
245 }
246
247 return 0;
248}
249
91f62ce6 250unsigned int _dl_skip_args; /* Nonzero if we were run directly. */
a1a9d215 251
d66e34cd 252static void
266180eb
RM
253dl_main (const ElfW(Phdr) *phdr,
254 ElfW(Half) phent,
255 ElfW(Addr) *user_entry)
d66e34cd 256{
266180eb 257 const ElfW(Phdr) *ph;
ceb2d9aa 258 struct link_map *main_map;
0200214b 259 int lazy;
2de99474 260 enum { normal, list, verify, trace } mode;
2064087b
RM
261 struct link_map **preloads;
262 unsigned int npreloads;
fd26970f 263 const char *preloadlist;
14bab8de
UD
264 size_t file_size;
265 char *file;
2f6d1f1b 266 int has_interp = 0;
d66e34cd 267
ca34d7a7
UD
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 ();
272
2de99474 273 mode = getenv ("LD_TRACE_LOADED_OBJECTS") != NULL ? trace : normal;
cf29ffbe 274 _dl_verbose = *(getenv ("LD_WARN") ?: "") == '\0' ? 0 : 1;
2de99474 275
cf29ffbe
UD
276 /* LAZY is determined by the environment variable LD_WARN and
277 LD_BIND_NOW if we trace the binary. */
fd26970f 278 if (mode == trace)
cf29ffbe
UD
279 lazy = (_dl_verbose
280 ? (*(getenv ("LD_BIND_NOW") ?: "") == '\0' ? 1 : 0) : -1);
fd26970f
UD
281 else
282 lazy = !__libc_enable_secure && *(getenv ("LD_BIND_NOW") ?: "") == '\0';
283
3996f34b
UD
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
291 {
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");
298
299 if (_dl_profile_output == NULL || _dl_profile_output[0] == '\0')
300 /* This is the default place. */
301 _dl_profile_output = "/var/tmp";
302 }
303
46ec036d
UD
304 /* Set up a flag which tells we are just starting. */
305 _dl_starting_up = 1;
306
f21acc89 307 if (*user_entry == (ElfW(Addr)) &ENTRY_POINT)
0200214b
RM
308 {
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:
313
314 ld.so PROGRAM ARGS...
315
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. */
421f82e5 324
ffee1316
RM
325 /* Note the place where the dynamic linker actually came from. */
326 _dl_rtld_map.l_name = _dl_argv[0];
6a76c115 327
fd26970f
UD
328 while (_dl_argc > 1)
329 if (! strcmp (_dl_argv[1], "--list"))
330 {
331 mode = list;
332 lazy = -1; /* This means do no dependency analysis. */
61965e9b 333
fd26970f
UD
334 ++_dl_skip_args;
335 --_dl_argc;
336 ++_dl_argv;
337 }
338 else if (! strcmp (_dl_argv[1], "--verify"))
339 {
340 mode = verify;
6a76c115 341
fd26970f
UD
342 ++_dl_skip_args;
343 --_dl_argc;
344 ++_dl_argv;
345 }
346 else
347 break;
d66e34cd 348
61eb22d3
UD
349 /* If we have no further argument the program was called incorrectly.
350 Grant the user some education. */
351 if (_dl_argc < 2)
352 _dl_sysdep_fatal ("\
353Usage: ld.so [--list|--verify] EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
354You have invoked `ld.so', the helper program for shared library executables.\n\
355This program usually lives in the file `/lib/ld.so', and special directives\n\
356in executable files using ELF shared libraries tell the system's program\n\
357loader to load the helper program from this file. This helper program loads\n\
358the shared libraries needed by the program executable, prepares the program\n\
359to run, and runs it. You may invoke this helper program directly from the\n\
360command line to load and run an ELF executable file; this is like executing\n\
361that file itself, but always uses this helper program from the file you\n\
362specified, instead of the helper program file specified in the executable\n\
363file you run. This is mostly of use for maintainers to test new versions\n\
364of this helper program; chances are you did not intend to run this program.\n",
365 NULL);
366
0200214b
RM
367 ++_dl_skip_args;
368 --_dl_argc;
369 ++_dl_argv;
91f62ce6 370
2de99474
UD
371 if (mode == verify)
372 {
dcf0671d 373 char *err_str = NULL;
2de99474 374 const char *obj_name __attribute__ ((unused));
993b3242 375 struct map_args args;
2de99474 376
993b3242
UD
377 args.str = _dl_argv[0];
378 (void) _dl_catch_error (&err_str, &obj_name, map_doit, &args);
379 main_map = args.main_map;
2de99474 380 if (err_str != NULL)
dcf0671d
UD
381 {
382 free (err_str);
383 _exit (EXIT_FAILURE);
384 }
2de99474
UD
385 }
386 else
ceb2d9aa 387 main_map = _dl_map_object (NULL, _dl_argv[0], lt_library, 0);
2de99474 388
ceb2d9aa
UD
389 phdr = main_map->l_phdr;
390 phent = main_map->l_phnum;
391 main_map->l_name = (char *) "";
392 *user_entry = main_map->l_entry;
0200214b
RM
393 }
394 else
395 {
396 /* Create a link_map for the executable itself.
397 This will be what dlopen on "" returns. */
f2ea0f5b
UD
398 main_map = _dl_new_object (_dl_argv[0] ?: (char *) "<main program>",
399 "", lt_executable);
ceb2d9aa 400 if (main_map == NULL)
762a2918 401 _dl_sysdep_fatal ("cannot allocate memory for link map\n", NULL);
ceb2d9aa
UD
402 main_map->l_phdr = phdr;
403 main_map->l_phnum = phent;
404 main_map->l_entry = *user_entry;
3e5f5557 405 main_map->l_opencount = 1;
0200214b
RM
406 }
407
408 /* Scan the program header table for the dynamic section. */
409 for (ph = phdr; ph < &phdr[phent]; ++ph)
410 switch (ph->p_type)
411 {
412 case PT_DYNAMIC:
413 /* This tells us where to find the dynamic section,
414 which tells us everything we need to do. */
ceb2d9aa 415 main_map->l_ld = (void *) main_map->l_addr + ph->p_vaddr;
0200214b
RM
416 break;
417 case PT_INTERP:
418 /* This "interpreter segment" was used by the program loader to
419 find the program interpreter, which is this program itself, the
420 dynamic linker. We note what name finds us, so that a future
421 dlopen call or DT_NEEDED entry, for something that wants to link
422 against the dynamic linker as a shared library, will know that
423 the shared object is already loaded. */
ceb2d9aa 424 _dl_rtld_libname.name = (const char *) main_map->l_addr + ph->p_vaddr;
c84142e8
UD
425 _dl_rtld_libname.next = NULL;
426 _dl_rtld_map.l_libname = &_dl_rtld_libname;
f41c8091
UD
427
428 /* Ordinarilly, we would get additional names for the loader from
429 our DT_SONAME. This can't happen if we were actually linked as
430 a static executable (detect this case when we have no DYNAMIC).
431 If so, assume the filename component of the interpreter path to
432 be our SONAME, and add it to our name list. */
433 if (_dl_rtld_map.l_ld == NULL)
434 {
435 char *p = strrchr (_dl_rtld_libname.name, '/');
436 if (p)
437 {
438 _dl_rtld_libname2.name = p+1;
439 _dl_rtld_libname2.next = NULL;
440 _dl_rtld_libname.next = &_dl_rtld_libname2;
441 }
442 }
443
2f6d1f1b 444 has_interp = 1;
0200214b
RM
445 break;
446 }
ffee1316 447 if (! _dl_rtld_map.l_libname && _dl_rtld_map.l_name)
c84142e8
UD
448 {
449 /* We were invoked directly, so the program might not have a
450 PT_INTERP. */
451 _dl_rtld_libname.name = _dl_rtld_map.l_name;
452 _dl_rtld_libname.next = NULL;
453 _dl_rtld_map.l_libname = &_dl_rtld_libname;
454 }
ffee1316
RM
455 else
456 assert (_dl_rtld_map.l_libname); /* How else did we get here? */
0200214b 457
61965e9b
RM
458 if (mode == verify)
459 /* We were called just to verify that this is a dynamic executable
460 using us as the program interpreter. */
ceb2d9aa 461 _exit (main_map->l_ld == NULL ? 1 : has_interp ? 0 : 2);
61965e9b 462
0200214b 463 /* Extract the contents of the dynamic section for easy access. */
ceb2d9aa
UD
464 elf_get_dynamic_info (main_map->l_ld, main_map->l_info);
465 if (main_map->l_info[DT_HASH])
0200214b 466 /* Set up our cache of pointers into the hash table. */
ceb2d9aa 467 _dl_setup_hash (main_map);
0200214b 468
0200214b 469 /* Put the link_map for ourselves on the chain so it can be found by
ceb2d9aa
UD
470 name. Note that at this point the global chain of link maps contains
471 exactly one element, which is pointed to by main_map. */
ffee1316
RM
472 if (! _dl_rtld_map.l_name)
473 /* If not invoked directly, the dynamic linker shared object file was
474 found by the PT_INTERP name. */
c84142e8 475 _dl_rtld_map.l_name = (char *) _dl_rtld_map.l_libname->name;
ba79d61b 476 _dl_rtld_map.l_type = lt_library;
ceb2d9aa
UD
477 main_map->l_next = &_dl_rtld_map;
478 _dl_rtld_map.l_prev = main_map;
0200214b 479
14bab8de
UD
480 /* We have two ways to specify objects to preload: via environment
481 variable and via the file /etc/ld.so.preload. The later can also
482 be used when security is enabled. */
2064087b
RM
483 preloads = NULL;
484 npreloads = 0;
14bab8de 485
fd26970f
UD
486 preloadlist = getenv ("LD_PRELOAD");
487 if (preloadlist)
c4029823 488 {
fd26970f
UD
489 /* The LD_PRELOAD environment variable gives a white space
490 separated list of libraries that are loaded before the
491 executable's dependencies and prepended to the global scope
492 list. If the binary is running setuid all elements
493 containing a '/' are ignored since it is insecure. */
494 char *list = strdupa (preloadlist);
495 char *p;
496 while ((p = strsep (&list, " ")) != NULL)
497 if (! __libc_enable_secure || strchr (p, '/') == NULL)
498 {
499 (void) _dl_map_object (NULL, p, lt_library, 0);
500 ++npreloads;
501 }
c4029823
UD
502 }
503
14bab8de
UD
504 /* Read the contents of the file. */
505 file = _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size,
506 PROT_READ | PROT_WRITE);
507 if (file)
508 {
509 /* Parse the file. It contains names of libraries to be loaded,
510 separated by white spaces or `:'. It may also contain
511 comments introduced by `#'. */
512 char *problem;
513 char *runp;
514 size_t rest;
515
516 /* Eliminate comments. */
517 runp = file;
518 rest = file_size;
519 while (rest > 0)
520 {
521 char *comment = memchr (runp, '#', rest);
522 if (comment == NULL)
523 break;
524
525 rest -= comment - runp;
526 do
527 *comment = ' ';
528 while (--rest > 0 && *++comment != '\n');
529 }
530
531 /* We have one problematic case: if we have a name at the end of
532 the file without a trailing terminating characters, we cannot
533 place the \0. Handle the case separately. */
534 if (file[file_size - 1] != ' ' && file[file_size] != '\t'
535 && file[file_size] != '\n')
536 {
537 problem = &file[file_size];
538 while (problem > file && problem[-1] != ' ' && problem[-1] != '\t'
539 && problem[-1] != '\n')
540 --problem;
541
542 if (problem > file)
543 problem[-1] = '\0';
544 }
545 else
546 problem = NULL;
547
548 if (file != problem)
549 {
550 char *p;
551 runp = file;
552 while ((p = strsep (&runp, ": \t\n")) != NULL)
553 {
46ec036d 554 (void) _dl_map_object (NULL, p, lt_library, 0);
14bab8de
UD
555 ++npreloads;
556 }
557 }
558
559 if (problem != NULL)
560 {
561 char *p = strndupa (problem, file_size - (problem - file));
46ec036d 562 (void) _dl_map_object (NULL, p, lt_library, 0);
14bab8de
UD
563 }
564
565 /* We don't need the file anymore. */
566 __munmap (file, file_size);
567 }
568
14bab8de
UD
569 if (npreloads != 0)
570 {
571 /* Set up PRELOADS with a vector of the preloaded libraries. */
572 struct link_map *l;
573 unsigned int i;
574 preloads = __alloca (npreloads * sizeof preloads[0]);
575 l = _dl_rtld_map.l_next; /* End of the chain before preloads. */
576 i = 0;
577 do
578 {
579 preloads[i++] = l;
580 l = l->l_next;
581 } while (l);
582 assert (i == npreloads);
583 }
584
0a54e401
UD
585 /* Initialize the data structures for the search paths for shared
586 objects. */
587 _dl_init_paths ();
588
2064087b
RM
589 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
590 specified some libraries to load, these are inserted before the actual
591 dependencies in the executable's searchlist for symbol resolution. */
ceb2d9aa 592 _dl_map_object_deps (main_map, preloads, npreloads, mode == trace);
d66e34cd 593
2064087b 594#ifndef MAP_ANON
f332db02
RM
595 /* We are done mapping things, so close the zero-fill descriptor. */
596 __close (_dl_zerofd);
597 _dl_zerofd = -1;
2064087b 598#endif
f332db02 599
f9496a7b
RM
600 /* Remove _dl_rtld_map from the chain. */
601 _dl_rtld_map.l_prev->l_next = _dl_rtld_map.l_next;
602 if (_dl_rtld_map.l_next)
603 _dl_rtld_map.l_next->l_prev = _dl_rtld_map.l_prev;
604
605 if (_dl_rtld_map.l_opencount)
0200214b 606 {
f9496a7b
RM
607 /* Some DT_NEEDED entry referred to the interpreter object itself, so
608 put it back in the list of visible objects. We insert it into the
609 chain in symbol search order because gdb uses the chain's order as
610 its symbol search order. */
611 unsigned int i = 1;
ceb2d9aa 612 while (main_map->l_searchlist[i] != &_dl_rtld_map)
f9496a7b 613 ++i;
ceb2d9aa
UD
614 _dl_rtld_map.l_prev = main_map->l_searchlist[i - 1];
615 _dl_rtld_map.l_next = (i + 1 < main_map->l_nsearchlist ?
616 main_map->l_searchlist[i + 1] : NULL);
f9496a7b
RM
617 assert (_dl_rtld_map.l_prev->l_next == _dl_rtld_map.l_next);
618 _dl_rtld_map.l_prev->l_next = &_dl_rtld_map;
4d02a5b1 619 if (_dl_rtld_map.l_next)
f9496a7b
RM
620 {
621 assert (_dl_rtld_map.l_next->l_prev == _dl_rtld_map.l_prev);
622 _dl_rtld_map.l_next->l_prev = &_dl_rtld_map;
623 }
0200214b 624 }
d66e34cd 625
c84142e8
UD
626 /* Now let us see whether all libraries are available in the
627 versions we need. */
628 {
993b3242
UD
629 struct version_check_args args;
630 args.doexit = mode == normal;
631 args.main_map = main_map;
632 _dl_receive_error (print_missing_version, version_check_doit, &args);
c84142e8
UD
633 }
634
2de99474 635 if (mode != normal)
0200214b
RM
636 {
637 /* We were run just to list the shared libraries. It is
638 important that we do this before real relocation, because the
639 functions we call below for output may no longer work properly
640 after relocation. */
1a3a58fd 641
0200214b 642 int i;
fd861379 643
0200214b
RM
644 if (! _dl_loaded->l_info[DT_NEEDED])
645 _dl_sysdep_message ("\t", "statically linked\n", NULL);
646 else
ceb2d9aa
UD
647 {
648 struct link_map *l;
649
650 for (l = _dl_loaded->l_next; l; l = l->l_next)
651 if (l->l_opencount == 0)
652 /* The library was not found. */
653 _dl_sysdep_message ("\t", l->l_libname->name, " => not found\n",
654 NULL);
655 else
656 {
657 char buf[20], *bp;
658 buf[sizeof buf - 1] = '\0';
659 bp = _itoa (l->l_addr, &buf[sizeof buf - 1], 16, 0);
660 while ((size_t) (&buf[sizeof buf - 1] - bp)
661 < sizeof l->l_addr * 2)
662 *--bp = '0';
663 _dl_sysdep_message ("\t", l->l_libname->name, " => ",
664 l->l_name, " (0x", bp, ")\n", NULL);
665 }
666 }
1a3a58fd 667
2de99474 668 if (mode != trace)
cddcfecf
RM
669 for (i = 1; i < _dl_argc; ++i)
670 {
671 const ElfW(Sym) *ref = NULL;
672 ElfW(Addr) loadbase = _dl_lookup_symbol (_dl_argv[i], &ref,
673 &_dl_default_scope[2],
dcf0671d 674 "argument",
a2b08ee5 675 ELF_MACHINE_JMP_SLOT);
cddcfecf
RM
676 char buf[20], *bp;
677 buf[sizeof buf - 1] = '\0';
678 bp = _itoa (ref->st_value, &buf[sizeof buf - 1], 16, 0);
14bab8de 679 while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2)
cddcfecf
RM
680 *--bp = '0';
681 _dl_sysdep_message (_dl_argv[i], " found at 0x", bp, NULL);
682 buf[sizeof buf - 1] = '\0';
683 bp = _itoa (loadbase, &buf[sizeof buf - 1], 16, 0);
14bab8de 684 while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2)
cddcfecf
RM
685 *--bp = '0';
686 _dl_sysdep_message (" in object at 0x", bp, "\n", NULL);
687 }
ce37fa88 688 else
fd26970f 689 {
ce37fa88
UD
690 if (lazy >= 0)
691 {
692 /* We have to do symbol dependency testing. */
693 struct relocate_args args;
694 struct link_map *l;
993b3242 695
ce37fa88 696 args.lazy = lazy;
fd26970f 697
ce37fa88
UD
698 l = _dl_loaded;
699 while (l->l_next)
700 l = l->l_next;
701 do
702 {
703 if (l != &_dl_rtld_map && l->l_opencount > 0)
704 {
705 args.l = l;
706 _dl_receive_error (print_unresolved, relocate_doit,
707 &args);
708 *_dl_global_scope_end = NULL;
709 }
710 l = l->l_prev;
711 } while (l);
712 }
713
714#define VERNEEDTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
715 if (*(getenv ("LD_VERBOSE") ?: "") != '\0')
fd26970f 716 {
ce37fa88
UD
717 /* Print more information. This means here, print information
718 about the versions needed. */
719 int first = 1;
720 struct link_map *map = _dl_loaded;
721
722 for (map = _dl_loaded; map != NULL; map = map->l_next)
fd26970f 723 {
f41c8091 724 const char *strtab;
ce37fa88 725 ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG];
f41c8091
UD
726 ElfW(Verneed) *ent;
727
728 if (dyn == NULL)
729 continue;
730
731 strtab = (const char *)
732 (map->l_addr + map->l_info[DT_STRTAB]->d_un.d_ptr);
733 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
ce37fa88 734
f41c8091 735 if (first)
ce37fa88 736 {
f41c8091
UD
737 _dl_sysdep_message ("\n\tVersion information:\n", NULL);
738 first = 0;
739 }
ce37fa88 740
f41c8091
UD
741 _dl_sysdep_message ("\t", (map->l_name[0]
742 ? map->l_name : _dl_argv[0]),
743 ":\n", NULL);
744
745 while (1)
746 {
747 ElfW(Vernaux) *aux;
748 struct link_map *needed;
ce37fa88 749
f41c8091
UD
750 needed = find_needed (strtab + ent->vn_file);
751 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
ce37fa88
UD
752
753 while (1)
754 {
f41c8091
UD
755 const char *fname = NULL;
756
757 _dl_sysdep_message ("\t\t",
758 strtab + ent->vn_file,
759 " (", strtab + aux->vna_name,
760 ") ",
761 (aux->vna_flags
762 & VER_FLG_WEAK
763 ? "[WEAK] " : ""),
764 "=> ", NULL);
765
766 if (needed != NULL
767 && match_version (strtab+aux->vna_name, needed))
768 fname = needed->l_name;
769
770 _dl_sysdep_message (fname ?: "not found", "\n",
771 NULL);
ce37fa88 772
f41c8091
UD
773 if (aux->vna_next == 0)
774 /* No more symbols. */
ce37fa88
UD
775 break;
776
f41c8091
UD
777 /* Next symbol. */
778 aux = (ElfW(Vernaux) *) ((char *) aux
779 + aux->vna_next);
ce37fa88 780 }
f41c8091
UD
781
782 if (ent->vn_next == 0)
783 /* No more dependencies. */
784 break;
785
786 /* Next dependency. */
787 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
ce37fa88 788 }
fd26970f 789 }
ce37fa88 790 }
fd26970f 791 }
d66e34cd 792
0200214b
RM
793 _exit (0);
794 }
86d2c878 795
ba79d61b
RM
796 {
797 /* Now we have all the objects loaded. Relocate them all except for
798 the dynamic linker itself. We do this in reverse order so that copy
799 relocs of earlier objects overwrite the data written by later
800 objects. We do not re-relocate the dynamic linker itself in this
801 loop because that could result in the GOT entries for functions we
802 call being changed, and that would break us. It is safe to relocate
803 the dynamic linker out of order because it has no copy relocs (we
804 know that because it is self-contained). */
805
ceb2d9aa 806 struct link_map *l;
ba79d61b
RM
807 l = _dl_loaded;
808 while (l->l_next)
809 l = l->l_next;
810 do
811 {
812 if (l != &_dl_rtld_map)
813 {
814 _dl_relocate_object (l, _dl_object_relocation_scope (l), lazy);
815 *_dl_global_scope_end = NULL;
816 }
817 l = l->l_prev;
818 } while (l);
819
820 /* Do any necessary cleanups for the startup OS interface code.
821 We do these now so that no calls are made after rtld re-relocation
822 which might be resolved to different functions than we expect.
823 We cannot do this before relocating the other objects because
824 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
825 _dl_sysdep_start_cleanup ();
826
827 if (_dl_rtld_map.l_opencount > 0)
828 /* There was an explicit ref to the dynamic linker as a shared lib.
829 Re-relocate ourselves with user-controlled symbol definitions. */
830 _dl_relocate_object (&_dl_rtld_map, &_dl_default_scope[2], 0);
831 }
0200214b 832
4d6acc61
RM
833 {
834 /* Initialize _r_debug. */
835 struct r_debug *r = _dl_debug_initialize (_dl_rtld_map.l_addr);
ceb2d9aa 836 struct link_map *l;
4d6acc61
RM
837
838 l = _dl_loaded;
ec42724d
RM
839
840#ifdef ELF_MACHINE_DEBUG_SETUP
841
842 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
843
844 ELF_MACHINE_DEBUG_SETUP (l, r);
845 ELF_MACHINE_DEBUG_SETUP (&_dl_rtld_map, r);
846
847#else
848
4d6acc61
RM
849 if (l->l_info[DT_DEBUG])
850 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
851 with the run-time address of the r_debug structure */
852 l->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
853
d746b89c
RM
854 /* Fill in the pointer in the dynamic linker's own dynamic section, in
855 case you run gdb on the dynamic linker directly. */
856 if (_dl_rtld_map.l_info[DT_DEBUG])
857 _dl_rtld_map.l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
858
ec42724d
RM
859#endif
860
4d6acc61
RM
861 /* Notify the debugger that all objects are now mapped in. */
862 r->r_state = RT_ADD;
863 _dl_debug_state ();
864 }
0200214b 865
3996f34b
UD
866 /* Now enable profiling if needed. */
867 if (_dl_profile_map != NULL)
868 /* We must prepare the profiling. */
869 _dl_start_profile (_dl_profile_map, _dl_profile_output);
870
d66e34cd
RM
871 /* Once we return, _dl_sysdep_start will invoke
872 the DT_INIT functions and then *USER_ENTRY. */
873}
fd26970f
UD
874\f
875/* This is a little helper function for resolving symbols while
876 tracing the binary. */
877static void
c84142e8
UD
878print_unresolved (int errcode __attribute__ ((unused)), const char *objname,
879 const char *errstring)
fd26970f 880{
3996f34b
UD
881 if (objname[0] == '\0')
882 objname = _dl_argv[0] ?: "<main program>";
fd26970f
UD
883 _dl_sysdep_error (errstring, " (", objname, ")\n", NULL);
884}
c84142e8
UD
885\f
886/* This is a little helper function for resolving symbols while
887 tracing the binary. */
888static void
889print_missing_version (int errcode __attribute__ ((unused)),
890 const char *objname, const char *errstring)
891{
892 _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>", ": ",
893 objname, ": ", errstring, "\n", NULL);
894}
This page took 0.156299 seconds and 5 git commands to generate.