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