]> sourceware.org Git - glibc.git/blob - elf/rtld.c
Update.
[glibc.git] / elf / rtld.c
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.
4
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.
9
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.
14
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. */
19
20 #include <fcntl.h>
21 #include <link.h>
22 #include <stddef.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/mman.h> /* Check if MAP_ANON is defined. */
27 #include <stdio-common/_itoa.h>
28 #include <assert.h>
29 #include <entry.h>
30 #include "dynamic-link.h"
31
32
33 /* System-specific function to do initial startup for the dynamic linker.
34 After this, file access calls and getenv must work. This is responsible
35 for setting __libc_enable_secure if we need to be secure (e.g. setuid),
36 and for setting _dl_argc and _dl_argv, and then calling _dl_main. */
37 extern ElfW(Addr) _dl_sysdep_start (void **start_argptr,
38 void (*dl_main) (const ElfW(Phdr) *phdr,
39 ElfW(Half) phent,
40 ElfW(Addr) *user_entry));
41 extern void _dl_sysdep_start_cleanup (void);
42
43 /* System-dependent function to read a file's whole contents
44 in the most convenient manner available. */
45 extern void *_dl_sysdep_read_whole_file (const char *filename,
46 size_t *filesize_ptr,
47 int mmap_prot);
48
49 /* Helper function to handle errors while resolving symbols. */
50 static void print_unresolved (int errcode, const char *objname,
51 const char *errsting);
52
53 /* Helper function to handle errors when a version is missing. */
54 static void print_missing_version (int errcode, const char *objname,
55 const char *errsting);
56
57
58 /* This is a list of all the modes the dynamic loader can be in. */
59 enum mode { normal, list, verify, trace };
60
61 /* Process all environments variables the dynamic linker must recognize.
62 Since all of them start with `LD_' we are a bit smarter while finding
63 all the entries. */
64 static void process_envvars (enum mode *modep, int *lazyp);
65
66 int _dl_argc;
67 char **_dl_argv;
68 const char *_dl_rpath;
69 int _dl_verbose;
70 const char *_dl_platform;
71 size_t _dl_platformlen;
72 unsigned long _dl_hwcap;
73 struct r_search_path *_dl_search_paths;
74 const char *_dl_profile;
75 const char *_dl_profile_output;
76 struct link_map *_dl_profile_map;
77 int _dl_debug_libs;
78 int _dl_debug_impcalls;
79 int _dl_debug_bindings;
80 int _dl_debug_symbols;
81 int _dl_debug_versions;
82 int _dl_debug_reloc;
83 int _dl_debug_files;
84
85 /* Set nonzero during loading and initialization of executable and
86 libraries, cleared before the executable's entry point runs. This
87 must not be initialized to nonzero, because the unused dynamic
88 linker loaded in for libc.so's "ld.so.1" dep will provide the
89 definition seen by libc.so's initializer; that value must be zero,
90 and will be since that dynamic linker's _dl_start and dl_main will
91 never be called. */
92 int _dl_starting_up;
93
94 static void dl_main (const ElfW(Phdr) *phdr,
95 ElfW(Half) phent,
96 ElfW(Addr) *user_entry);
97
98 struct link_map _dl_rtld_map;
99 struct libname_list _dl_rtld_libname;
100 struct libname_list _dl_rtld_libname2;
101
102 #ifdef RTLD_START
103 RTLD_START
104 #else
105 #error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
106 #endif
107
108 static ElfW(Addr)
109 _dl_start (void *arg)
110 {
111 struct link_map bootstrap_map;
112
113 /* This #define produces dynamic linking inline functions for
114 bootstrap relocation instead of general-purpose relocation. */
115 #define RTLD_BOOTSTRAP
116 #define RESOLVE(sym, version, flags) bootstrap_map.l_addr
117 #include "dynamic-link.h"
118
119 /* Figure out the run-time load address of the dynamic linker itself. */
120 bootstrap_map.l_addr = elf_machine_load_address ();
121
122 /* Read our own dynamic section and fill in the info array. */
123 bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
124 elf_get_dynamic_info (bootstrap_map.l_ld, bootstrap_map.l_info);
125
126 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
127 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info);
128 #endif
129
130 /* Relocate ourselves so we can do normal function calls and
131 data access using the global offset table. */
132
133 ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0, 0);
134 /* Please note that we don't allow profiling of this object and
135 therefore need not test whether we have to allocate the array
136 for the relocation results (as done in dl-reloc.c). */
137
138 /* Now life is sane; we can call functions and access global data.
139 Set up to use the operating system facilities, and find out from
140 the operating system's program loader where to find the program
141 header table in core. */
142
143 /* Transfer data about ourselves to the permanent link_map structure. */
144 _dl_rtld_map.l_addr = bootstrap_map.l_addr;
145 _dl_rtld_map.l_ld = bootstrap_map.l_ld;
146 _dl_rtld_map.l_opencount = 1;
147 memcpy (_dl_rtld_map.l_info, bootstrap_map.l_info,
148 sizeof _dl_rtld_map.l_info);
149 _dl_setup_hash (&_dl_rtld_map);
150
151 /* Cache the DT_RPATH stored in ld.so itself; this will be
152 the default search path. */
153 if (_dl_rtld_map.l_info[DT_STRTAB] && _dl_rtld_map.l_info[DT_RPATH])
154 {
155 _dl_rpath = (void *) (_dl_rtld_map.l_addr +
156 _dl_rtld_map.l_info[DT_STRTAB]->d_un.d_ptr +
157 _dl_rtld_map.l_info[DT_RPATH]->d_un.d_val);
158 }
159
160 /* Call the OS-dependent function to set up life so we can do things like
161 file access. It will call `dl_main' (below) to do all the real work
162 of the dynamic linker, and then unwind our frame and run the user
163 entry point on the same stack we entered on. */
164 return _dl_sysdep_start (arg, &dl_main);
165 }
166
167 /* Now life is peachy; we can do all normal operations.
168 On to the real work. */
169
170 void ENTRY_POINT (void);
171
172 /* Some helper functions. */
173
174 /* Arguments to relocate_doit. */
175 struct relocate_args
176 {
177 struct link_map *l;
178 int lazy;
179 };
180
181 struct map_args
182 {
183 /* Argument to map_doit. */
184 char *str;
185 /* Return value of map_doit. */
186 struct link_map *main_map;
187 };
188
189 /* Arguments to version_check_doit. */
190 struct version_check_args
191 {
192 struct link_map *main_map;
193 int doexit;
194 };
195
196 static void
197 relocate_doit (void *a)
198 {
199 struct relocate_args *args = (struct relocate_args *) a;
200
201 _dl_relocate_object (args->l, _dl_object_relocation_scope (args->l),
202 args->lazy);
203 }
204
205 static void
206 map_doit (void *a)
207 {
208 struct map_args *args = (struct map_args *)a;
209 args->main_map = _dl_map_object (NULL, args->str, 0, lt_library, 0);
210 }
211
212 static void
213 version_check_doit (void *a)
214 {
215 struct version_check_args *args = (struct version_check_args *)a;
216 if (_dl_check_all_versions (args->main_map, 1) && args->doexit)
217 /* We cannot start the application. Abort now. */
218 _exit (1);
219 }
220
221
222 static inline struct link_map *
223 find_needed (const char *name)
224 {
225 unsigned int n;
226
227 for (n = 0; n < _dl_loaded->l_nsearchlist; ++n)
228 if (_dl_name_match_p (name, _dl_loaded->l_searchlist[n]))
229 return _dl_loaded->l_searchlist[n];
230
231 /* Should never happen. */
232 return NULL;
233 }
234
235 static int
236 match_version (const char *string, struct link_map *map)
237 {
238 const char *strtab = (const char *) (map->l_addr
239 + map->l_info[DT_STRTAB]->d_un.d_ptr);
240 ElfW(Verdef) *def;
241
242 #define VERDEFTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
243 if (map->l_info[VERDEFTAG] == NULL)
244 /* The file has no symbol versioning. */
245 return 0;
246
247 def = (ElfW(Verdef) *) ((char *) map->l_addr
248 + map->l_info[VERDEFTAG]->d_un.d_ptr);
249 while (1)
250 {
251 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
252
253 /* Compare the version strings. */
254 if (strcmp (string, strtab + aux->vda_name) == 0)
255 /* Bingo! */
256 return 1;
257
258 /* If no more definitions we failed to find what we want. */
259 if (def->vd_next == 0)
260 break;
261
262 /* Next definition. */
263 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
264 }
265
266 return 0;
267 }
268
269 static unsigned int _dl_skip_args; /* Nonzero if we were run directly. */
270 static const char *library_path; /* The library search path. */
271 static const char *preloadlist; /* The list preloaded objects. */
272 static int version_info; /* Nonzero if information about
273 versions has to be printed. */
274
275 static void
276 dl_main (const ElfW(Phdr) *phdr,
277 ElfW(Half) phent,
278 ElfW(Addr) *user_entry)
279 {
280 const ElfW(Phdr) *ph;
281 struct link_map *main_map;
282 int lazy;
283 enum mode mode;
284 struct link_map **preloads;
285 unsigned int npreloads;
286 size_t file_size;
287 char *file;
288 int has_interp = 0;
289 unsigned int i;
290
291 /* Process the environment variable which control the behaviour. */
292 process_envvars (&mode, &lazy);
293
294 /* Set up a flag which tells we are just starting. */
295 _dl_starting_up = 1;
296
297 if (*user_entry == (ElfW(Addr)) &ENTRY_POINT)
298 {
299 /* Ho ho. We are not the program interpreter! We are the program
300 itself! This means someone ran ld.so as a command. Well, that
301 might be convenient to do sometimes. We support it by
302 interpreting the args like this:
303
304 ld.so PROGRAM ARGS...
305
306 The first argument is the name of a file containing an ELF
307 executable we will load and run with the following arguments.
308 To simplify life here, PROGRAM is searched for using the
309 normal rules for shared objects, rather than $PATH or anything
310 like that. We just load it and use its entry point; we don't
311 pay attention to its PT_INTERP command (we are the interpreter
312 ourselves). This is an easy way to test a new ld.so before
313 installing it. */
314
315 /* Note the place where the dynamic linker actually came from. */
316 _dl_rtld_map.l_name = _dl_argv[0];
317
318 while (_dl_argc > 1)
319 if (! strcmp (_dl_argv[1], "--list"))
320 {
321 mode = list;
322 lazy = -1; /* This means do no dependency analysis. */
323
324 ++_dl_skip_args;
325 --_dl_argc;
326 ++_dl_argv;
327 }
328 else if (! strcmp (_dl_argv[1], "--verify"))
329 {
330 mode = verify;
331
332 ++_dl_skip_args;
333 --_dl_argc;
334 ++_dl_argv;
335 }
336 else if (! strcmp (_dl_argv[1], "--library-path")
337 && _dl_argc > 2)
338 {
339 library_path = _dl_argv[2];
340
341 _dl_skip_args += 2;
342 _dl_argc -= 2;
343 _dl_argv += 2;
344 }
345 else
346 break;
347
348 /* If we have no further argument the program was called incorrectly.
349 Grant the user some education. */
350 if (_dl_argc < 2)
351 _dl_sysdep_fatal ("\
352 Usage: ld.so [--list|--verify] EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
353 You have invoked `ld.so', the helper program for shared library executables.\n\
354 This program usually lives in the file `/lib/ld.so', and special directives\n\
355 in executable files using ELF shared libraries tell the system's program\n\
356 loader to load the helper program from this file. This helper program loads\n\
357 the shared libraries needed by the program executable, prepares the program\n\
358 to run, and runs it. You may invoke this helper program directly from the\n\
359 command line to load and run an ELF executable file; this is like executing\n\
360 that file itself, but always uses this helper program from the file you\n\
361 specified, instead of the helper program file specified in the executable\n\
362 file you run. This is mostly of use for maintainers to test new versions\n\
363 of this helper program; chances are you did not intend to run this program.\n",
364 NULL);
365
366 ++_dl_skip_args;
367 --_dl_argc;
368 ++_dl_argv;
369
370 /* Initialize the data structures for the search paths for shared
371 objects. */
372 _dl_init_paths (library_path);
373
374 if (mode == verify)
375 {
376 char *err_str = NULL;
377 const char *obj_name __attribute__ ((unused));
378 struct map_args args;
379
380 args.str = _dl_argv[0];
381 (void) _dl_catch_error (&err_str, &obj_name, map_doit, &args);
382 main_map = args.main_map;
383 if (err_str != NULL)
384 {
385 free (err_str);
386 _exit (EXIT_FAILURE);
387 }
388 }
389 else
390 main_map = _dl_map_object (NULL, _dl_argv[0], 0, lt_library, 0);
391
392 phdr = main_map->l_phdr;
393 phent = main_map->l_phnum;
394 main_map->l_name = (char *) "";
395 *user_entry = main_map->l_entry;
396 }
397 else
398 {
399 /* Create a link_map for the executable itself.
400 This will be what dlopen on "" returns. */
401 main_map = _dl_new_object ((char *) "", "", lt_executable);
402 if (main_map == NULL)
403 _dl_sysdep_fatal ("cannot allocate memory for link map\n", NULL);
404 main_map->l_phdr = phdr;
405 main_map->l_phnum = phent;
406 main_map->l_entry = *user_entry;
407 main_map->l_opencount = 1;
408
409 /* We delay initializing the path structure until we got the dynamic
410 information for the program. */
411 }
412
413 /* Scan the program header table for the dynamic section. */
414 for (ph = phdr; ph < &phdr[phent]; ++ph)
415 switch (ph->p_type)
416 {
417 case PT_PHDR:
418 /* Find out the load address. */
419 main_map->l_addr = (ElfW(Addr)) phdr - ph->p_vaddr;
420 break;
421 case PT_DYNAMIC:
422 /* This tells us where to find the dynamic section,
423 which tells us everything we need to do. */
424 main_map->l_ld = (void *) main_map->l_addr + ph->p_vaddr;
425 break;
426 case PT_INTERP:
427 /* This "interpreter segment" was used by the program loader to
428 find the program interpreter, which is this program itself, the
429 dynamic linker. We note what name finds us, so that a future
430 dlopen call or DT_NEEDED entry, for something that wants to link
431 against the dynamic linker as a shared library, will know that
432 the shared object is already loaded. */
433 _dl_rtld_libname.name = (const char *) main_map->l_addr + ph->p_vaddr;
434 _dl_rtld_libname.next = NULL;
435 _dl_rtld_map.l_libname = &_dl_rtld_libname;
436
437 /* Ordinarilly, we would get additional names for the loader from
438 our DT_SONAME. This can't happen if we were actually linked as
439 a static executable (detect this case when we have no DYNAMIC).
440 If so, assume the filename component of the interpreter path to
441 be our SONAME, and add it to our name list. */
442 if (_dl_rtld_map.l_ld == NULL)
443 {
444 char *p = strrchr (_dl_rtld_libname.name, '/');
445 if (p)
446 {
447 _dl_rtld_libname2.name = p+1;
448 _dl_rtld_libname2.next = NULL;
449 _dl_rtld_libname.next = &_dl_rtld_libname2;
450 }
451 }
452
453 has_interp = 1;
454 break;
455 }
456 if (! _dl_rtld_map.l_libname && _dl_rtld_map.l_name)
457 {
458 /* We were invoked directly, so the program might not have a
459 PT_INTERP. */
460 _dl_rtld_libname.name = _dl_rtld_map.l_name;
461 _dl_rtld_libname.next = NULL;
462 _dl_rtld_map.l_libname = &_dl_rtld_libname;
463 }
464 else
465 assert (_dl_rtld_map.l_libname); /* How else did we get here? */
466
467 if (mode == verify)
468 /* We were called just to verify that this is a dynamic executable
469 using us as the program interpreter. */
470 _exit (main_map->l_ld == NULL ? 1 : has_interp ? 0 : 2);
471
472 /* Extract the contents of the dynamic section for easy access. */
473 elf_get_dynamic_info (main_map->l_ld, main_map->l_info);
474 if (main_map->l_info[DT_HASH])
475 /* Set up our cache of pointers into the hash table. */
476 _dl_setup_hash (main_map);
477
478 if (*user_entry != (ElfW(Addr)) &ENTRY_POINT)
479 /* Initialize the data structures for the search paths for shared
480 objects. */
481 _dl_init_paths (library_path);
482
483 /* Put the link_map for ourselves on the chain so it can be found by
484 name. Note that at this point the global chain of link maps contains
485 exactly one element, which is pointed to by main_map. */
486 if (! _dl_rtld_map.l_name)
487 /* If not invoked directly, the dynamic linker shared object file was
488 found by the PT_INTERP name. */
489 _dl_rtld_map.l_name = (char *) _dl_rtld_map.l_libname->name;
490 _dl_rtld_map.l_type = lt_library;
491 main_map->l_next = &_dl_rtld_map;
492 _dl_rtld_map.l_prev = main_map;
493
494 /* We have two ways to specify objects to preload: via environment
495 variable and via the file /etc/ld.so.preload. The later can also
496 be used when security is enabled. */
497 preloads = NULL;
498 npreloads = 0;
499
500 if (preloadlist)
501 {
502 /* The LD_PRELOAD environment variable gives list of libraries
503 separated by white space or colons that are loaded before the
504 executable's dependencies and prepended to the global scope
505 list. If the binary is running setuid all elements
506 containing a '/' are ignored since it is insecure. */
507 char *list = strdupa (preloadlist);
508 char *p;
509 while ((p = strsep (&list, " :")) != NULL)
510 if (! __libc_enable_secure || strchr (p, '/') == NULL)
511 {
512 struct link_map *new_map = _dl_map_object (NULL, p, 1,
513 lt_library, 0);
514 if (new_map->l_opencount == 1)
515 /* It is no duplicate. */
516 ++npreloads;
517 }
518 }
519
520 /* Read the contents of the file. */
521 file = _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size,
522 PROT_READ | PROT_WRITE);
523 if (file)
524 {
525 /* Parse the file. It contains names of libraries to be loaded,
526 separated by white spaces or `:'. It may also contain
527 comments introduced by `#'. */
528 char *problem;
529 char *runp;
530 size_t rest;
531
532 /* Eliminate comments. */
533 runp = file;
534 rest = file_size;
535 while (rest > 0)
536 {
537 char *comment = memchr (runp, '#', rest);
538 if (comment == NULL)
539 break;
540
541 rest -= comment - runp;
542 do
543 *comment = ' ';
544 while (--rest > 0 && *++comment != '\n');
545 }
546
547 /* We have one problematic case: if we have a name at the end of
548 the file without a trailing terminating characters, we cannot
549 place the \0. Handle the case separately. */
550 if (file[file_size - 1] != ' ' && file[file_size - 1] != '\t'
551 && file[file_size - 1] != '\n' && file[file_size - 1] != ':')
552 {
553 problem = &file[file_size];
554 while (problem > file && problem[-1] != ' ' && problem[-1] != '\t'
555 && problem[-1] != '\n' && problem[-1] != ':')
556 --problem;
557
558 if (problem > file)
559 problem[-1] = '\0';
560 }
561 else
562 {
563 problem = NULL;
564 file[file_size - 1] = '\0';
565 }
566
567 if (file != problem)
568 {
569 char *p;
570 runp = file + strspn (file, ": \t\n");
571 while ((p = strsep (&runp, ": \t\n")) != NULL)
572 {
573 struct link_map *new_map = _dl_map_object (NULL, p, 1,
574 lt_library, 0);
575 if (new_map->l_opencount == 1)
576 /* It is no duplicate. */
577 ++npreloads;
578
579 if (runp != NULL)
580 runp += strspn (runp, ": \t\n");
581 }
582 }
583
584 if (problem != NULL)
585 {
586 char *p = strndupa (problem, file_size - (problem - file));
587 struct link_map *new_map = _dl_map_object (NULL, p, 1,
588 lt_library, 0);
589 if (new_map->l_opencount == 1)
590 /* It is no duplicate. */
591 ++npreloads;
592 }
593
594 /* We don't need the file anymore. */
595 __munmap (file, file_size);
596 }
597
598 if (npreloads != 0)
599 {
600 /* Set up PRELOADS with a vector of the preloaded libraries. */
601 struct link_map *l;
602 preloads = __alloca (npreloads * sizeof preloads[0]);
603 l = _dl_rtld_map.l_next; /* End of the chain before preloads. */
604 i = 0;
605 do
606 {
607 preloads[i++] = l;
608 l = l->l_next;
609 } while (l);
610 assert (i == npreloads);
611 }
612
613 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
614 specified some libraries to load, these are inserted before the actual
615 dependencies in the executable's searchlist for symbol resolution. */
616 _dl_map_object_deps (main_map, preloads, npreloads, mode == trace);
617
618 #ifndef MAP_ANON
619 /* We are done mapping things, so close the zero-fill descriptor. */
620 __close (_dl_zerofd);
621 _dl_zerofd = -1;
622 #endif
623
624 /* Remove _dl_rtld_map from the chain. */
625 _dl_rtld_map.l_prev->l_next = _dl_rtld_map.l_next;
626 if (_dl_rtld_map.l_next)
627 _dl_rtld_map.l_next->l_prev = _dl_rtld_map.l_prev;
628
629 if (_dl_rtld_map.l_opencount)
630 {
631 /* Some DT_NEEDED entry referred to the interpreter object itself, so
632 put it back in the list of visible objects. We insert it into the
633 chain in symbol search order because gdb uses the chain's order as
634 its symbol search order. */
635 i = 1;
636 while (main_map->l_searchlist[i] != &_dl_rtld_map)
637 ++i;
638 _dl_rtld_map.l_prev = main_map->l_searchlist[i - 1];
639 _dl_rtld_map.l_next = (i + 1 < main_map->l_nsearchlist ?
640 main_map->l_searchlist[i + 1] : NULL);
641 assert (_dl_rtld_map.l_prev->l_next == _dl_rtld_map.l_next);
642 _dl_rtld_map.l_prev->l_next = &_dl_rtld_map;
643 if (_dl_rtld_map.l_next)
644 {
645 assert (_dl_rtld_map.l_next->l_prev == _dl_rtld_map.l_prev);
646 _dl_rtld_map.l_next->l_prev = &_dl_rtld_map;
647 }
648 }
649
650 /* Now let us see whether all libraries are available in the
651 versions we need. */
652 {
653 struct version_check_args args;
654 args.doexit = mode == normal;
655 args.main_map = main_map;
656 _dl_receive_error (print_missing_version, version_check_doit, &args);
657 }
658
659 if (mode != normal)
660 {
661 /* We were run just to list the shared libraries. It is
662 important that we do this before real relocation, because the
663 functions we call below for output may no longer work properly
664 after relocation. */
665 if (! _dl_loaded->l_info[DT_NEEDED])
666 _dl_sysdep_message ("\t", "statically linked\n", NULL);
667 else
668 {
669 struct link_map *l;
670
671 for (l = _dl_loaded->l_next; l; l = l->l_next)
672 if (l->l_opencount == 0)
673 /* The library was not found. */
674 _dl_sysdep_message ("\t", l->l_libname->name, " => not found\n",
675 NULL);
676 else
677 {
678 char buf[20], *bp;
679 buf[sizeof buf - 1] = '\0';
680 bp = _itoa_word (l->l_addr, &buf[sizeof buf - 1], 16, 0);
681 while ((size_t) (&buf[sizeof buf - 1] - bp)
682 < sizeof l->l_addr * 2)
683 *--bp = '0';
684 _dl_sysdep_message ("\t", l->l_libname->name, " => ",
685 l->l_name, " (0x", bp, ")\n", NULL);
686 }
687 }
688
689 if (mode != trace)
690 for (i = 1; i < _dl_argc; ++i)
691 {
692 const ElfW(Sym) *ref = NULL;
693 ElfW(Addr) loadbase = _dl_lookup_symbol (_dl_argv[i], &ref,
694 &_dl_default_scope[2],
695 "argument",
696 ELF_MACHINE_JMP_SLOT);
697 char buf[20], *bp;
698 buf[sizeof buf - 1] = '\0';
699 bp = _itoa_word (ref->st_value, &buf[sizeof buf - 1], 16, 0);
700 while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2)
701 *--bp = '0';
702 _dl_sysdep_message (_dl_argv[i], " found at 0x", bp, NULL);
703 buf[sizeof buf - 1] = '\0';
704 bp = _itoa_word (loadbase, &buf[sizeof buf - 1], 16, 0);
705 while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2)
706 *--bp = '0';
707 _dl_sysdep_message (" in object at 0x", bp, "\n", NULL);
708 }
709 else
710 {
711 if (lazy >= 0)
712 {
713 /* We have to do symbol dependency testing. */
714 struct relocate_args args;
715 struct link_map *l;
716
717 args.lazy = lazy;
718
719 l = _dl_loaded;
720 while (l->l_next)
721 l = l->l_next;
722 do
723 {
724 if (l != &_dl_rtld_map && l->l_opencount > 0)
725 {
726 args.l = l;
727 _dl_receive_error (print_unresolved, relocate_doit,
728 &args);
729 *_dl_global_scope_end = NULL;
730 }
731 l = l->l_prev;
732 } while (l);
733 }
734
735 #define VERNEEDTAG (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
736 if (version_info)
737 {
738 /* Print more information. This means here, print information
739 about the versions needed. */
740 int first = 1;
741 struct link_map *map = _dl_loaded;
742
743 for (map = _dl_loaded; map != NULL; map = map->l_next)
744 {
745 const char *strtab;
746 ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG];
747 ElfW(Verneed) *ent;
748
749 if (dyn == NULL)
750 continue;
751
752 strtab = (const char *)
753 (map->l_addr + map->l_info[DT_STRTAB]->d_un.d_ptr);
754 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
755
756 if (first)
757 {
758 _dl_sysdep_message ("\n\tVersion information:\n", NULL);
759 first = 0;
760 }
761
762 _dl_sysdep_message ("\t", (map->l_name[0]
763 ? map->l_name : _dl_argv[0]),
764 ":\n", NULL);
765
766 while (1)
767 {
768 ElfW(Vernaux) *aux;
769 struct link_map *needed;
770
771 needed = find_needed (strtab + ent->vn_file);
772 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
773
774 while (1)
775 {
776 const char *fname = NULL;
777
778 _dl_sysdep_message ("\t\t",
779 strtab + ent->vn_file,
780 " (", strtab + aux->vna_name,
781 ") ",
782 (aux->vna_flags
783 & VER_FLG_WEAK
784 ? "[WEAK] " : ""),
785 "=> ", NULL);
786
787 if (needed != NULL
788 && match_version (strtab+aux->vna_name, needed))
789 fname = needed->l_name;
790
791 _dl_sysdep_message (fname ?: "not found", "\n",
792 NULL);
793
794 if (aux->vna_next == 0)
795 /* No more symbols. */
796 break;
797
798 /* Next symbol. */
799 aux = (ElfW(Vernaux) *) ((char *) aux
800 + aux->vna_next);
801 }
802
803 if (ent->vn_next == 0)
804 /* No more dependencies. */
805 break;
806
807 /* Next dependency. */
808 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
809 }
810 }
811 }
812 }
813
814 _exit (0);
815 }
816
817 {
818 /* Now we have all the objects loaded. Relocate them all except for
819 the dynamic linker itself. We do this in reverse order so that copy
820 relocs of earlier objects overwrite the data written by later
821 objects. We do not re-relocate the dynamic linker itself in this
822 loop because that could result in the GOT entries for functions we
823 call being changed, and that would break us. It is safe to relocate
824 the dynamic linker out of order because it has no copy relocs (we
825 know that because it is self-contained). */
826
827 struct link_map *l;
828 l = _dl_loaded;
829 while (l->l_next)
830 l = l->l_next;
831 do
832 {
833 if (l != &_dl_rtld_map)
834 {
835 _dl_relocate_object (l, _dl_object_relocation_scope (l), lazy);
836 *_dl_global_scope_end = NULL;
837 }
838 l = l->l_prev;
839 } while (l);
840
841 /* Do any necessary cleanups for the startup OS interface code.
842 We do these now so that no calls are made after rtld re-relocation
843 which might be resolved to different functions than we expect.
844 We cannot do this before relocating the other objects because
845 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
846 _dl_sysdep_start_cleanup ();
847
848 if (_dl_rtld_map.l_opencount > 0)
849 /* There was an explicit ref to the dynamic linker as a shared lib.
850 Re-relocate ourselves with user-controlled symbol definitions. */
851 _dl_relocate_object (&_dl_rtld_map, &_dl_default_scope[2], 0);
852 }
853
854 {
855 /* Initialize _r_debug. */
856 struct r_debug *r = _dl_debug_initialize (_dl_rtld_map.l_addr);
857 struct link_map *l;
858
859 l = _dl_loaded;
860
861 #ifdef ELF_MACHINE_DEBUG_SETUP
862
863 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
864
865 ELF_MACHINE_DEBUG_SETUP (l, r);
866 ELF_MACHINE_DEBUG_SETUP (&_dl_rtld_map, r);
867
868 #else
869
870 if (l->l_info[DT_DEBUG])
871 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
872 with the run-time address of the r_debug structure */
873 l->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
874
875 /* Fill in the pointer in the dynamic linker's own dynamic section, in
876 case you run gdb on the dynamic linker directly. */
877 if (_dl_rtld_map.l_info[DT_DEBUG])
878 _dl_rtld_map.l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
879
880 #endif
881
882 /* Notify the debugger that all objects are now mapped in. */
883 r->r_state = RT_ADD;
884 _dl_debug_state ();
885 }
886
887 /* Now enable profiling if needed. */
888 if (_dl_profile_map != NULL)
889 /* We must prepare the profiling. */
890 _dl_start_profile (_dl_profile_map, _dl_profile_output);
891
892 /* Once we return, _dl_sysdep_start will invoke
893 the DT_INIT functions and then *USER_ENTRY. */
894 }
895 \f
896 /* This is a little helper function for resolving symbols while
897 tracing the binary. */
898 static void
899 print_unresolved (int errcode __attribute__ ((unused)), const char *objname,
900 const char *errstring)
901 {
902 if (objname[0] == '\0')
903 objname = _dl_argv[0] ?: "<main program>";
904 _dl_sysdep_error (errstring, " (", objname, ")\n", NULL);
905 }
906 \f
907 /* This is a little helper function for resolving symbols while
908 tracing the binary. */
909 static void
910 print_missing_version (int errcode __attribute__ ((unused)),
911 const char *objname, const char *errstring)
912 {
913 _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>", ": ",
914 objname, ": ", errstring, "\n", NULL);
915 }
916 \f
917 /* Nonzero if any of the debugging options is enabled. */
918 static int any_debug;
919
920 /* Process the string given as the parameter which explains which debugging
921 options are enabled. */
922 static void
923 process_dl_debug (char *dl_debug)
924 {
925 do
926 {
927 #define issep(Ch) ((Ch) == ' ' || (Ch) == ',')
928 /* Skip separating white spaces and commas. */
929 while (issep (*dl_debug))
930 ++dl_debug;
931 if (*dl_debug != '\0')
932 {
933 if (strncmp (dl_debug, "files", 5) == 0
934 && (issep (dl_debug[5]) || dl_debug[5] == '\0'))
935 {
936 _dl_debug_files = 1;
937 _dl_debug_impcalls = 1;
938 any_debug = 1;
939 dl_debug += 5;
940 }
941 else if (strncmp (dl_debug, "bindings", 8) == 0
942 && (issep (dl_debug[8]) || dl_debug[8] == '\0'))
943 {
944 _dl_debug_bindings = 1;
945 _dl_debug_impcalls = 1;
946 any_debug = 1;
947 dl_debug += 8;
948 }
949 else if (strncmp (dl_debug, "help", 4) == 0
950 && (issep (dl_debug[4]) || dl_debug[4] == '\0'))
951 {
952 _dl_sysdep_message ("\
953 Valid options for the LD_DEBUG environment variable are:\n\
954 \n\
955 bindings display information about symbol binding\n\
956 files display processing of files and libraries\n\
957 help display this help message and exit\n\
958 libs display library search paths\n\
959 reloc display relocation processing\n\
960 symbols display symbol table processing\n\
961 versions display version dependencies\n\
962 \n\
963 To direct the debugging output into a file instead of standard output\n\
964 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n",
965 NULL);
966 _exit (0);
967 }
968 else if (strncmp (dl_debug, "libs", 4) == 0
969 && (issep (dl_debug[4]) || dl_debug[4] == '\0'))
970 {
971 _dl_debug_libs = 1;
972 _dl_debug_impcalls = 1;
973 any_debug = 1;
974 dl_debug += 4;
975 }
976 else if (strncmp (dl_debug, "reloc", 4) == 0
977 && (issep (dl_debug[5]) || dl_debug[5] == '\0'))
978 {
979 _dl_debug_reloc = 1;
980 _dl_debug_impcalls = 1;
981 any_debug = 1;
982 dl_debug += 5;
983 }
984 else if (strncmp (dl_debug, "symbols", 7) == 0
985 && (issep (dl_debug[7]) || dl_debug[7] == '\0'))
986 {
987 _dl_debug_symbols = 1;
988 _dl_debug_impcalls = 1;
989 any_debug = 1;
990 dl_debug += 7;
991 }
992 else if (strncmp (dl_debug, "versions", 8) == 0
993 && (issep (dl_debug[8]) || dl_debug[8] == '\0'))
994 {
995 _dl_debug_versions = 1;
996 _dl_debug_impcalls = 1;
997 any_debug = 1;
998 dl_debug += 8;
999 }
1000 else
1001 {
1002 /* Display a warning and skip everything until next
1003 separator. */
1004 char *startp = dl_debug;
1005
1006 do
1007 ++dl_debug;
1008 while (*dl_debug != '\0' && !issep (*dl_debug));
1009
1010 startp = strndupa (startp, dl_debug - startp);
1011 _dl_sysdep_error ("warning: debug option `", startp,
1012 "' unknown; try LD_DEBUG=help\n", NULL);
1013
1014 }
1015 }
1016 }
1017 while (*dl_debug != '\0');
1018 }
1019 \f
1020 /* Process all environments variables the dynamic linker must recognize.
1021 Since all of them start with `LD_' we are a bit smarter while finding
1022 all the entries. */
1023 static void
1024 process_envvars (enum mode *modep, int *lazyp)
1025 {
1026 char **runp = NULL;
1027 char *envline;
1028 enum mode mode = normal;
1029 int bind_now = 0;
1030 char *debug_output = NULL;
1031
1032 /* This is the default place for profiling data file. */
1033 _dl_profile_output = "/var/tmp";
1034
1035 while ((envline = _dl_next_ld_env_entry (&runp)) != NULL)
1036 {
1037 int result;
1038
1039 /* Do we bind early? */
1040 result = strncmp (&envline[3], "BIND_NOW=", 9);
1041 if (result == 0)
1042 {
1043 bind_now = 1;
1044 continue;
1045 }
1046 if (result < 0)
1047 continue;
1048
1049 /* Debugging of the dynamic linker? */
1050 result = strncmp (&envline[3], "DEBUG=", 6);
1051 if (result == 0)
1052 {
1053 process_dl_debug (&envline[9]);
1054 continue;
1055 }
1056 if (result < 0)
1057 continue;
1058
1059 /* Where to place the profiling data file. */
1060 result = strncmp (&envline[3], "DEBUG_OUTPUT=", 13);
1061 if (result == 0)
1062 {
1063 debug_output = &envline[16];
1064 continue;
1065 }
1066 if (result < 0)
1067 continue;
1068
1069 /* The library search path. */
1070 result = strncmp (&envline[3], "LIBRARY_PATH=", 13);
1071 if (result == 0)
1072 {
1073 library_path = &envline[16];
1074 continue;
1075 }
1076 if (result < 0)
1077 continue;
1078
1079 /* List of objects to be preloaded. */
1080 result = strncmp (&envline[3], "PRELOAD=", 8);
1081 if (result == 0)
1082 {
1083 preloadlist = &envline[11];
1084 continue;
1085 }
1086 if (result < 0)
1087 continue;
1088
1089 /* Which shared object shall be profiled. */
1090 result = strncmp (&envline[3], "PROFILE=", 8);
1091 if (result == 0)
1092 {
1093 _dl_profile = &envline[11];
1094 if (*_dl_profile == '\0')
1095 _dl_profile = NULL;
1096 continue;
1097 }
1098 if (result < 0)
1099 continue;
1100
1101 /* Where to place the profiling data file. */
1102 result = strncmp (&envline[3], "PROFILE_OUTPUT=", 15);
1103 if (result == 0)
1104 {
1105 _dl_profile_output = &envline[18];
1106 if (*_dl_profile_output == '\0')
1107 _dl_profile_output = "/var/tmp";
1108 continue;
1109 }
1110 if (result < 0)
1111 continue;
1112
1113 /* Test whether we want to see the content of the auxiliary
1114 array passed up from the kernel. */
1115 result = strncmp (&envline[3], "SHOW_AUXV=", 10);
1116 if (result == 0)
1117 {
1118 _dl_show_auxv ();
1119 continue;
1120 }
1121 if (result < 0)
1122 continue;
1123
1124 /* The mode of the dynamic linker can be set. */
1125 result = strncmp (&envline[3], "TRACE_LOADED_OBJECTS=", 21);
1126 if (result == 0)
1127 {
1128 mode = trace;
1129 continue;
1130 }
1131 if (result < 0)
1132 continue;
1133
1134 /* Print information about versions. */
1135 result = strncmp (&envline[3], "VERBOSE=", 8);
1136 if (result == 0)
1137 {
1138 version_info = envline[11] != '\0';
1139 continue;
1140 }
1141 if (result < 0)
1142 continue;
1143
1144 /* Warning level, verbose or not. */
1145 result = strncmp (&envline[3], "WARN=", 5);
1146 if (result == 0)
1147 {
1148 _dl_verbose = envline[8] != '\0';
1149 continue;
1150 }
1151 }
1152
1153 /* If we have to run the dynamic linker in debugging mode and the
1154 LD_DEBUG_OUTPUT environment variable is given, we write the debug
1155 messages to this file. */
1156 if (any_debug && debug_output != NULL)
1157 {
1158 _dl_debug_fd = __open (debug_output, O_WRONLY | O_APPEND | O_CREAT,
1159 0666);
1160 if (_dl_debug_fd == -1)
1161 /* We use standard output if opening the file failed. */
1162 _dl_debug_fd = STDOUT_FILENO;
1163 }
1164
1165 /* LAZY is determined by the environment variable LD_WARN and
1166 LD_BIND_NOW if we trace the binary. */
1167 if (mode == trace)
1168 *lazyp = _dl_verbose ? !bind_now : -1;
1169 else
1170 *lazyp = !__libc_enable_secure && !bind_now;
1171
1172 *modep = mode;
1173 }
This page took 0.091387 seconds and 5 git commands to generate.