]> sourceware.org Git - glibc.git/blob - elf/rtld.c
Update.
[glibc.git] / elf / rtld.c
1 /* Run time dynamic linker.
2 Copyright (C) 1995-1999, 2000 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 <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/mman.h> /* Check if MAP_ANON is defined. */
25 #include <ldsodefs.h>
26 #include <stdio-common/_itoa.h>
27 #include <entry.h>
28 #include <fpu_control.h>
29 #include <hp-timing.h>
30 #include <bits/libc-lock.h>
31 #include "dynamic-link.h"
32 #include "dl-librecon.h"
33 #include <unsecvars.h>
34
35 #include <assert.h>
36
37 /* System-specific function to do initial startup for the dynamic linker.
38 After this, file access calls and getenv must work. This is responsible
39 for setting __libc_enable_secure if we need to be secure (e.g. setuid),
40 and for setting _dl_argc and _dl_argv, and then calling _dl_main. */
41 extern ElfW(Addr) _dl_sysdep_start (void **start_argptr,
42 void (*dl_main) (const ElfW(Phdr) *phdr,
43 ElfW(Half) phent,
44 ElfW(Addr) *user_entry));
45 extern void _dl_sysdep_start_cleanup (void);
46
47 /* This function is used to unload the cache file if necessary. */
48 extern void _dl_unload_cache (void);
49
50 /* System-dependent function to read a file's whole contents
51 in the most convenient manner available. */
52 extern void *_dl_sysdep_read_whole_file (const char *filename,
53 size_t *filesize_ptr,
54 int mmap_prot);
55
56 /* Helper function to handle errors while resolving symbols. */
57 static void print_unresolved (int errcode, const char *objname,
58 const char *errsting);
59
60 /* Helper function to handle errors when a version is missing. */
61 static void print_missing_version (int errcode, const char *objname,
62 const char *errsting);
63
64 /* Print the various times we collected. */
65 static void print_statistics (void);
66
67 /* This is a list of all the modes the dynamic loader can be in. */
68 enum mode { normal, list, verify, trace };
69
70 /* Process all environments variables the dynamic linker must recognize.
71 Since all of them start with `LD_' we are a bit smarter while finding
72 all the entries. */
73 static void process_envvars (enum mode *modep, int *lazyp);
74
75 int _dl_argc;
76 char **_dl_argv;
77 unsigned int _dl_skip_args; /* Nonzero if we were run directly. */
78 int _dl_verbose;
79 const char *_dl_platform;
80 size_t _dl_platformlen;
81 unsigned long _dl_hwcap;
82 fpu_control_t _dl_fpu_control = _FPU_DEFAULT;
83 struct r_search_path *_dl_search_paths;
84 const char *_dl_profile;
85 const char *_dl_profile_output;
86 struct link_map *_dl_profile_map;
87 int _dl_lazy;
88 /* XXX I know about at least one case where we depend on the old weak
89 behavior (it has to do with librt). Until we get DSO groups implemented
90 we have to make this the default. Bummer. --drepper */
91 #if 0
92 int _dl_dynamic_weak;
93 #else
94 int _dl_dynamic_weak = 1;
95 #endif
96 int _dl_debug_libs;
97 int _dl_debug_impcalls;
98 int _dl_debug_bindings;
99 int _dl_debug_symbols;
100 int _dl_debug_versions;
101 int _dl_debug_reloc;
102 int _dl_debug_files;
103 int _dl_debug_statistics;
104 const char *_dl_inhibit_rpath; /* RPATH values which should be
105 ignored. */
106 const char *_dl_origin_path;
107 int _dl_bind_not;
108
109 /* This is a pointer to the map for the main object and through it to
110 all loaded objects. */
111 struct link_map *_dl_loaded;
112 /* Number of object in the _dl_loaded list. */
113 unsigned int _dl_nloaded;
114 /* Pointer to the l_searchlist element of the link map of the main object. */
115 struct r_scope_elem *_dl_main_searchlist;
116 /* Copy of the content of `_dl_main_searchlist'. */
117 struct r_scope_elem _dl_initial_searchlist;
118 /* Array which is used when looking up in the global scope. */
119 struct r_scope_elem *_dl_global_scope[2];
120
121 /* During the program run we must not modify the global data of
122 loaded shared object simultanously in two threads. Therefore we
123 protect `_dl_open' and `_dl_close' in dl-close.c.
124
125 This must be a recursive lock since the initializer function of
126 the loaded object might as well require a call to this function.
127 At this time it is not anymore a problem to modify the tables. */
128 __libc_lock_define_initialized_recursive (, _dl_load_lock)
129
130 /* Set nonzero during loading and initialization of executable and
131 libraries, cleared before the executable's entry point runs. This
132 must not be initialized to nonzero, because the unused dynamic
133 linker loaded in for libc.so's "ld.so.1" dep will provide the
134 definition seen by libc.so's initializer; that value must be zero,
135 and will be since that dynamic linker's _dl_start and dl_main will
136 never be called. */
137 int _dl_starting_up;
138
139
140 static void dl_main (const ElfW(Phdr) *phdr,
141 ElfW(Half) phent,
142 ElfW(Addr) *user_entry);
143
144 struct link_map _dl_rtld_map;
145 struct libname_list _dl_rtld_libname;
146 struct libname_list _dl_rtld_libname2;
147
148 /* Variable for statistics. */
149 #ifndef HP_TIMING_NONAVAIL
150 static hp_timing_t rtld_total_time;
151 static hp_timing_t relocate_time;
152 static hp_timing_t load_time;
153 #endif
154 extern unsigned long int _dl_num_relocations; /* in dl-lookup.c */
155
156 static ElfW(Addr) _dl_start_final (void *arg, struct link_map *bootstrap_map_p,
157 hp_timing_t start_time);
158
159 #ifdef RTLD_START
160 RTLD_START
161 #else
162 #error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
163 #endif
164
165 static ElfW(Addr)
166 _dl_start (void *arg)
167 {
168 struct link_map bootstrap_map;
169 hp_timing_t start_time;
170 size_t cnt;
171
172 /* This #define produces dynamic linking inline functions for
173 bootstrap relocation instead of general-purpose relocation. */
174 #define RTLD_BOOTSTRAP
175 #define RESOLVE_MAP(sym, version, flags) \
176 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : &bootstrap_map)
177 #define RESOLVE(sym, version, flags) \
178 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : bootstrap_map.l_addr)
179 #include "dynamic-link.h"
180
181 if (HP_TIMING_INLINE && HP_TIMING_AVAIL)
182 HP_TIMING_NOW (start_time);
183
184 /* Partly clean the `bootstrap_map' structure up. Don't use `memset'
185 since it might nor be built in or inlined and we cannot make function
186 calls at this point. */
187 for (cnt = 0;
188 cnt < sizeof (bootstrap_map.l_info) / sizeof (bootstrap_map.l_info[0]);
189 ++cnt)
190 bootstrap_map.l_info[cnt] = 0;
191
192 /* Figure out the run-time load address of the dynamic linker itself. */
193 bootstrap_map.l_addr = elf_machine_load_address ();
194
195 /* Read our own dynamic section and fill in the info array. */
196 bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
197 elf_get_dynamic_info (&bootstrap_map);
198
199 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
200 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info);
201 #endif
202
203 /* Relocate ourselves so we can do normal function calls and
204 data access using the global offset table. */
205
206 ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0, 0);
207 /* Please note that we don't allow profiling of this object and
208 therefore need not test whether we have to allocate the array
209 for the relocation results (as done in dl-reloc.c). */
210
211 /* Now life is sane; we can call functions and access global data.
212 Set up to use the operating system facilities, and find out from
213 the operating system's program loader where to find the program
214 header table in core. Put the rest of _dl_start into a separate
215 function, that way the compiler cannot put accesses to the GOT
216 before ELF_DYNAMIC_RELOCATE. */
217 {
218 ElfW(Addr) entry = _dl_start_final (arg, &bootstrap_map, start_time);
219
220 #ifndef ELF_MACHINE_START_ADDRESS
221 # define ELF_MACHINE_START_ADDRESS(map, start) (start)
222 #endif
223
224 return ELF_MACHINE_START_ADDRESS (_dl_loaded, entry);
225 }
226 }
227
228
229 static ElfW(Addr)
230 _dl_start_final (void *arg, struct link_map *bootstrap_map_p,
231 hp_timing_t start_time)
232 {
233 /* The use of `alloca' here looks ridiculous but it helps. The goal
234 is to avoid the function from being inlined. There is no official
235 way to do this so we use this trick. gcc never inlines functions
236 which use `alloca'. */
237 ElfW(Addr) *start_addr = alloca (sizeof (ElfW(Addr)));
238
239 if (HP_TIMING_AVAIL)
240 {
241 /* If it hasn't happen yet record the startup time. */
242 if (! HP_TIMING_INLINE)
243 HP_TIMING_NOW (start_time);
244
245 /* Initialize the timing functions. */
246 HP_TIMING_DIFF_INIT ();
247 }
248
249 /* Transfer data about ourselves to the permanent link_map structure. */
250 _dl_rtld_map.l_addr = bootstrap_map_p->l_addr;
251 _dl_rtld_map.l_ld = bootstrap_map_p->l_ld;
252 _dl_rtld_map.l_opencount = 1;
253 memcpy (_dl_rtld_map.l_info, bootstrap_map_p->l_info,
254 sizeof _dl_rtld_map.l_info);
255 _dl_setup_hash (&_dl_rtld_map);
256
257 /* Don't bother trying to work out how ld.so is mapped in memory. */
258 _dl_rtld_map.l_map_start = ~0;
259 _dl_rtld_map.l_map_end = ~0;
260
261 /* Call the OS-dependent function to set up life so we can do things like
262 file access. It will call `dl_main' (below) to do all the real work
263 of the dynamic linker, and then unwind our frame and run the user
264 entry point on the same stack we entered on. */
265 *start_addr = _dl_sysdep_start (arg, &dl_main);
266 #ifndef HP_TIMING_NONAVAIL
267 if (HP_TIMING_AVAIL)
268 {
269 hp_timing_t end_time;
270
271 /* Get the current time. */
272 HP_TIMING_NOW (end_time);
273
274 /* Compute the difference. */
275 HP_TIMING_DIFF (rtld_total_time, start_time, end_time);
276 }
277 #endif
278
279 if (__builtin_expect (_dl_debug_statistics, 0))
280 print_statistics ();
281
282 return *start_addr;
283 }
284
285 /* Now life is peachy; we can do all normal operations.
286 On to the real work. */
287
288 void ENTRY_POINT (void);
289
290 /* Some helper functions. */
291
292 /* Arguments to relocate_doit. */
293 struct relocate_args
294 {
295 struct link_map *l;
296 int lazy;
297 };
298
299 struct map_args
300 {
301 /* Argument to map_doit. */
302 char *str;
303 /* Return value of map_doit. */
304 struct link_map *main_map;
305 };
306
307 /* Arguments to version_check_doit. */
308 struct version_check_args
309 {
310 int doexit;
311 int dotrace;
312 };
313
314 static void
315 relocate_doit (void *a)
316 {
317 struct relocate_args *args = (struct relocate_args *) a;
318
319 _dl_relocate_object (args->l, args->l->l_scope,
320 args->lazy, 0);
321 }
322
323 static void
324 map_doit (void *a)
325 {
326 struct map_args *args = (struct map_args *) a;
327 args->main_map = _dl_map_object (NULL, args->str, 0, lt_library, 0, 0);
328 }
329
330 static void
331 version_check_doit (void *a)
332 {
333 struct version_check_args *args = (struct version_check_args *) a;
334 if (_dl_check_all_versions (_dl_loaded, 1, args->dotrace) && args->doexit)
335 /* We cannot start the application. Abort now. */
336 _exit (1);
337 }
338
339
340 static inline struct link_map *
341 find_needed (const char *name)
342 {
343 unsigned int n = _dl_loaded->l_searchlist.r_nlist;
344
345 while (n-- > 0)
346 if (_dl_name_match_p (name, _dl_loaded->l_searchlist.r_list[n]))
347 return _dl_loaded->l_searchlist.r_list[n];
348
349 /* Should never happen. */
350 return NULL;
351 }
352
353 static int
354 match_version (const char *string, struct link_map *map)
355 {
356 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
357 ElfW(Verdef) *def;
358
359 #define VERDEFTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
360 if (map->l_info[VERDEFTAG] == NULL)
361 /* The file has no symbol versioning. */
362 return 0;
363
364 def = (ElfW(Verdef) *) ((char *) map->l_addr
365 + map->l_info[VERDEFTAG]->d_un.d_ptr);
366 while (1)
367 {
368 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
369
370 /* Compare the version strings. */
371 if (strcmp (string, strtab + aux->vda_name) == 0)
372 /* Bingo! */
373 return 1;
374
375 /* If no more definitions we failed to find what we want. */
376 if (def->vd_next == 0)
377 break;
378
379 /* Next definition. */
380 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
381 }
382
383 return 0;
384 }
385
386 static const char *library_path; /* The library search path. */
387 static const char *preloadlist; /* The list preloaded objects. */
388 static int version_info; /* Nonzero if information about
389 versions has to be printed. */
390
391 static void
392 dl_main (const ElfW(Phdr) *phdr,
393 ElfW(Half) phent,
394 ElfW(Addr) *user_entry)
395 {
396 const ElfW(Phdr) *ph;
397 enum mode mode;
398 struct link_map **preloads;
399 unsigned int npreloads;
400 size_t file_size;
401 char *file;
402 int has_interp = 0;
403 unsigned int i;
404 int rtld_is_main = 0;
405 #ifndef HP_TIMING_NONAVAIL
406 hp_timing_t start;
407 hp_timing_t stop;
408 hp_timing_t diff;
409 #endif
410
411 /* Process the environment variable which control the behaviour. */
412 process_envvars (&mode, &_dl_lazy);
413
414 /* Set up a flag which tells we are just starting. */
415 _dl_starting_up = 1;
416
417 if (*user_entry == (ElfW(Addr)) &ENTRY_POINT)
418 {
419 /* Ho ho. We are not the program interpreter! We are the program
420 itself! This means someone ran ld.so as a command. Well, that
421 might be convenient to do sometimes. We support it by
422 interpreting the args like this:
423
424 ld.so PROGRAM ARGS...
425
426 The first argument is the name of a file containing an ELF
427 executable we will load and run with the following arguments.
428 To simplify life here, PROGRAM is searched for using the
429 normal rules for shared objects, rather than $PATH or anything
430 like that. We just load it and use its entry point; we don't
431 pay attention to its PT_INTERP command (we are the interpreter
432 ourselves). This is an easy way to test a new ld.so before
433 installing it. */
434 rtld_is_main = 1;
435
436 /* Note the place where the dynamic linker actually came from. */
437 _dl_rtld_map.l_name = _dl_argv[0];
438
439 while (_dl_argc > 1)
440 if (! strcmp (_dl_argv[1], "--list"))
441 {
442 mode = list;
443 _dl_lazy = -1; /* This means do no dependency analysis. */
444
445 ++_dl_skip_args;
446 --_dl_argc;
447 ++_dl_argv;
448 }
449 else if (! strcmp (_dl_argv[1], "--verify"))
450 {
451 mode = verify;
452
453 ++_dl_skip_args;
454 --_dl_argc;
455 ++_dl_argv;
456 }
457 else if (! strcmp (_dl_argv[1], "--library-path") && _dl_argc > 2)
458 {
459 library_path = _dl_argv[2];
460
461 _dl_skip_args += 2;
462 _dl_argc -= 2;
463 _dl_argv += 2;
464 }
465 else if (! strcmp (_dl_argv[1], "--inhibit-rpath") && _dl_argc > 2)
466 {
467 _dl_inhibit_rpath = _dl_argv[2];
468
469 _dl_skip_args += 2;
470 _dl_argc -= 2;
471 _dl_argv += 2;
472 }
473 else
474 break;
475
476 /* If we have no further argument the program was called incorrectly.
477 Grant the user some education. */
478 if (_dl_argc < 2)
479 _dl_sysdep_fatal ("\
480 Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
481 You have invoked `ld.so', the helper program for shared library executables.\n\
482 This program usually lives in the file `/lib/ld.so', and special directives\n\
483 in executable files using ELF shared libraries tell the system's program\n\
484 loader to load the helper program from this file. This helper program loads\n\
485 the shared libraries needed by the program executable, prepares the program\n\
486 to run, and runs it. You may invoke this helper program directly from the\n\
487 command line to load and run an ELF executable file; this is like executing\n\
488 that file itself, but always uses this helper program from the file you\n\
489 specified, instead of the helper program file specified in the executable\n\
490 file you run. This is mostly of use for maintainers to test new versions\n\
491 of this helper program; chances are you did not intend to run this program.\n\
492 \n\
493 --list list all dependencies and how they are resolved\n\
494 --verify verify that given object really is a dynamically linked\n\
495 object we can handle\n\
496 --library-path PATH use given PATH instead of content of the environment\n\
497 variable LD_LIBRARY_PATH\n\
498 --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\
499 in LIST\n",
500 NULL);
501
502 ++_dl_skip_args;
503 --_dl_argc;
504 ++_dl_argv;
505
506 /* Initialize the data structures for the search paths for shared
507 objects. */
508 _dl_init_paths (library_path);
509
510 if (__builtin_expect (mode, normal) == verify)
511 {
512 const char *objname;
513 const char *err_str = NULL;
514 struct map_args args;
515
516 args.str = _dl_argv[0];
517 (void) _dl_catch_error (&objname, &err_str, map_doit, &args);
518 if (__builtin_expect (err_str != NULL, 0))
519 {
520 if (err_str != _dl_out_of_memory)
521 free ((char *) err_str);
522 _exit (EXIT_FAILURE);
523 }
524 }
525 else
526 {
527 HP_TIMING_NOW (start);
528 _dl_map_object (NULL, _dl_argv[0], 0, lt_library, 0, 0);
529 HP_TIMING_NOW (stop);
530
531 HP_TIMING_DIFF (load_time, start, stop);
532 }
533
534 phdr = _dl_loaded->l_phdr;
535 phent = _dl_loaded->l_phnum;
536 /* We overwrite here a pointer to a malloc()ed string. But since
537 the malloc() implementation used at this point is the dummy
538 implementations which has no real free() function it does not
539 makes sense to free the old string first. */
540 _dl_loaded->l_name = (char *) "";
541 *user_entry = _dl_loaded->l_entry;
542 }
543 else
544 {
545 /* Create a link_map for the executable itself.
546 This will be what dlopen on "" returns. */
547 _dl_new_object ((char *) "", "", lt_executable, NULL);
548 if (_dl_loaded == NULL)
549 _dl_sysdep_fatal ("cannot allocate memory for link map\n", NULL);
550 _dl_loaded->l_phdr = phdr;
551 _dl_loaded->l_phnum = phent;
552 _dl_loaded->l_entry = *user_entry;
553 _dl_loaded->l_opencount = 1;
554
555 /* At this point we are in a bit of trouble. We would have to
556 fill in the values for l_dev and l_ino. But in general we
557 do not know where the file is. We also do not handle AT_EXECFD
558 even if it would be passed up.
559
560 We leave the values here defined to 0. This is normally no
561 problem as the program code itself is normally no shared
562 object and therefore cannot be loaded dynamically. Nothing
563 prevent the use of dynamic binaries and in these situations
564 we might get problems. We might not be able to find out
565 whether the object is already loaded. But since there is no
566 easy way out and because the dynamic binary must also not
567 have an SONAME we ignore this program for now. If it becomes
568 a problem we can force people using SONAMEs. */
569
570 /* We delay initializing the path structure until we got the dynamic
571 information for the program. */
572 }
573
574 /* It is not safe to load stuff after the main program. */
575 _dl_loaded->l_map_end = ~0;
576 /* Perhaps the executable has no PT_LOAD header entries at all. */
577 _dl_loaded->l_map_start = ~0;
578
579 /* Scan the program header table for the dynamic section. */
580 for (ph = phdr; ph < &phdr[phent]; ++ph)
581 switch (ph->p_type)
582 {
583 case PT_PHDR:
584 /* Find out the load address. */
585 _dl_loaded->l_addr = (ElfW(Addr)) phdr - ph->p_vaddr;
586 break;
587 case PT_DYNAMIC:
588 /* This tells us where to find the dynamic section,
589 which tells us everything we need to do. */
590 _dl_loaded->l_ld = (void *) _dl_loaded->l_addr + ph->p_vaddr;
591 break;
592 case PT_INTERP:
593 /* This "interpreter segment" was used by the program loader to
594 find the program interpreter, which is this program itself, the
595 dynamic linker. We note what name finds us, so that a future
596 dlopen call or DT_NEEDED entry, for something that wants to link
597 against the dynamic linker as a shared library, will know that
598 the shared object is already loaded. */
599 _dl_rtld_libname.name = ((const char *) _dl_loaded->l_addr
600 + ph->p_vaddr);
601 /* _dl_rtld_libname.next = NULL; Already zero. */
602 _dl_rtld_map.l_libname = &_dl_rtld_libname;
603
604 /* Ordinarilly, we would get additional names for the loader from
605 our DT_SONAME. This can't happen if we were actually linked as
606 a static executable (detect this case when we have no DYNAMIC).
607 If so, assume the filename component of the interpreter path to
608 be our SONAME, and add it to our name list. */
609 if (_dl_rtld_map.l_ld == NULL)
610 {
611 char *p = strrchr (_dl_rtld_libname.name, '/');
612 if (p)
613 {
614 _dl_rtld_libname2.name = p+1;
615 /* _dl_rtld_libname2.next = NULL; Already zero. */
616 _dl_rtld_libname.next = &_dl_rtld_libname2;
617 }
618 }
619
620 has_interp = 1;
621 break;
622 case PT_LOAD:
623 /* Remember where the main program starts in memory. */
624 {
625 ElfW(Addr) mapstart;
626 mapstart = _dl_loaded->l_addr + (ph->p_vaddr & ~(ph->p_align - 1));
627 if (_dl_loaded->l_map_start > mapstart)
628 _dl_loaded->l_map_start = mapstart;
629 }
630 break;
631 }
632 if (! _dl_rtld_map.l_libname && _dl_rtld_map.l_name)
633 {
634 /* We were invoked directly, so the program might not have a
635 PT_INTERP. */
636 _dl_rtld_libname.name = _dl_rtld_map.l_name;
637 /* _dl_rtld_libname.next = NULL; Alread zero. */
638 _dl_rtld_map.l_libname = &_dl_rtld_libname;
639 }
640 else
641 assert (_dl_rtld_map.l_libname); /* How else did we get here? */
642
643 if (! rtld_is_main)
644 {
645 /* Extract the contents of the dynamic section for easy access. */
646 elf_get_dynamic_info (_dl_loaded);
647 if (_dl_loaded->l_info[DT_HASH])
648 /* Set up our cache of pointers into the hash table. */
649 _dl_setup_hash (_dl_loaded);
650 }
651
652 if (__builtin_expect (mode, normal) == verify)
653 {
654 /* We were called just to verify that this is a dynamic
655 executable using us as the program interpreter. Exit with an
656 error if we were not able to load the binary or no interpreter
657 is specified (i.e., this is no dynamically linked binary. */
658 if (_dl_loaded->l_ld == NULL)
659 _exit (1);
660
661 /* We allow here some platform specific code. */
662 #ifdef DISTINGUISH_LIB_VERSIONS
663 DISTINGUISH_LIB_VERSIONS;
664 #endif
665 _exit (has_interp ? 0 : 2);
666 }
667
668 if (! rtld_is_main)
669 /* Initialize the data structures for the search paths for shared
670 objects. */
671 _dl_init_paths (library_path);
672
673 /* Put the link_map for ourselves on the chain so it can be found by
674 name. Note that at this point the global chain of link maps contains
675 exactly one element, which is pointed to by _dl_loaded. */
676 if (! _dl_rtld_map.l_name)
677 /* If not invoked directly, the dynamic linker shared object file was
678 found by the PT_INTERP name. */
679 _dl_rtld_map.l_name = (char *) _dl_rtld_map.l_libname->name;
680 _dl_rtld_map.l_type = lt_library;
681 _dl_loaded->l_next = &_dl_rtld_map;
682 _dl_rtld_map.l_prev = _dl_loaded;
683 ++_dl_nloaded;
684
685 /* We have two ways to specify objects to preload: via environment
686 variable and via the file /etc/ld.so.preload. The latter can also
687 be used when security is enabled. */
688 preloads = NULL;
689 npreloads = 0;
690
691 if (__builtin_expect (preloadlist != NULL, 0))
692 {
693 /* The LD_PRELOAD environment variable gives list of libraries
694 separated by white space or colons that are loaded before the
695 executable's dependencies and prepended to the global scope
696 list. If the binary is running setuid all elements
697 containing a '/' are ignored since it is insecure. */
698 char *list = strdupa (preloadlist);
699 char *p;
700
701 HP_TIMING_NOW (start);
702
703 while ((p = strsep (&list, " :")) != NULL)
704 if (p[0] != '\0'
705 && (__builtin_expect (! __libc_enable_secure, 1)
706 || strchr (p, '/') == NULL))
707 {
708 struct link_map *new_map = _dl_map_object (_dl_loaded, p, 1,
709 lt_library, 0, 0);
710 if (new_map->l_opencount == 1)
711 /* It is no duplicate. */
712 ++npreloads;
713 }
714
715 HP_TIMING_NOW (stop);
716 HP_TIMING_DIFF (diff, start, stop);
717 HP_TIMING_ACCUM_NT (load_time, diff);
718 }
719
720 /* Read the contents of the file. */
721 file = _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size,
722 PROT_READ | PROT_WRITE);
723 if (__builtin_expect (file != NULL, 0))
724 {
725 /* Parse the file. It contains names of libraries to be loaded,
726 separated by white spaces or `:'. It may also contain
727 comments introduced by `#'. */
728 char *problem;
729 char *runp;
730 size_t rest;
731
732 /* Eliminate comments. */
733 runp = file;
734 rest = file_size;
735 while (rest > 0)
736 {
737 char *comment = memchr (runp, '#', rest);
738 if (comment == NULL)
739 break;
740
741 rest -= comment - runp;
742 do
743 *comment = ' ';
744 while (--rest > 0 && *++comment != '\n');
745 }
746
747 /* We have one problematic case: if we have a name at the end of
748 the file without a trailing terminating characters, we cannot
749 place the \0. Handle the case separately. */
750 if (file[file_size - 1] != ' ' && file[file_size - 1] != '\t'
751 && file[file_size - 1] != '\n' && file[file_size - 1] != ':')
752 {
753 problem = &file[file_size];
754 while (problem > file && problem[-1] != ' ' && problem[-1] != '\t'
755 && problem[-1] != '\n' && problem[-1] != ':')
756 --problem;
757
758 if (problem > file)
759 problem[-1] = '\0';
760 }
761 else
762 {
763 problem = NULL;
764 file[file_size - 1] = '\0';
765 }
766
767 HP_TIMING_NOW (start);
768
769 if (file != problem)
770 {
771 char *p;
772 runp = file;
773 while ((p = strsep (&runp, ": \t\n")) != NULL)
774 if (p[0] != '\0')
775 {
776 struct link_map *new_map = _dl_map_object (_dl_loaded, p, 1,
777 lt_library, 0, 0);
778 if (new_map->l_opencount == 1)
779 /* It is no duplicate. */
780 ++npreloads;
781 }
782 }
783
784 if (problem != NULL)
785 {
786 char *p = strndupa (problem, file_size - (problem - file));
787 struct link_map *new_map = _dl_map_object (_dl_loaded, p, 1,
788 lt_library, 0, 0);
789 if (new_map->l_opencount == 1)
790 /* It is no duplicate. */
791 ++npreloads;
792 }
793
794 HP_TIMING_NOW (stop);
795 HP_TIMING_DIFF (diff, start, stop);
796 HP_TIMING_ACCUM_NT (load_time, diff);
797
798 /* We don't need the file anymore. */
799 __munmap (file, file_size);
800 }
801
802 if (__builtin_expect (npreloads, 0) != 0)
803 {
804 /* Set up PRELOADS with a vector of the preloaded libraries. */
805 struct link_map *l;
806 preloads = __alloca (npreloads * sizeof preloads[0]);
807 l = _dl_rtld_map.l_next; /* End of the chain before preloads. */
808 i = 0;
809 do
810 {
811 preloads[i++] = l;
812 l = l->l_next;
813 } while (l);
814 assert (i == npreloads);
815 }
816
817 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
818 specified some libraries to load, these are inserted before the actual
819 dependencies in the executable's searchlist for symbol resolution. */
820 HP_TIMING_NOW (start);
821 _dl_map_object_deps (_dl_loaded, preloads, npreloads, mode == trace);
822 HP_TIMING_NOW (stop);
823 HP_TIMING_DIFF (diff, start, stop);
824 HP_TIMING_ACCUM_NT (load_time, diff);
825
826 /* Mark all objects as being in the global scope. */
827 for (i = _dl_loaded->l_searchlist.r_nlist; i > 0; )
828 _dl_loaded->l_searchlist.r_list[--i]->l_global = 1;
829
830 #ifndef MAP_ANON
831 /* We are done mapping things, so close the zero-fill descriptor. */
832 __close (_dl_zerofd);
833 _dl_zerofd = -1;
834 #endif
835
836 /* Remove _dl_rtld_map from the chain. */
837 _dl_rtld_map.l_prev->l_next = _dl_rtld_map.l_next;
838 if (_dl_rtld_map.l_next)
839 _dl_rtld_map.l_next->l_prev = _dl_rtld_map.l_prev;
840
841 if (__builtin_expect (_dl_rtld_map.l_opencount, 2) > 1)
842 {
843 /* Some DT_NEEDED entry referred to the interpreter object itself, so
844 put it back in the list of visible objects. We insert it into the
845 chain in symbol search order because gdb uses the chain's order as
846 its symbol search order. */
847 i = 1;
848 while (_dl_loaded->l_searchlist.r_list[i] != &_dl_rtld_map)
849 ++i;
850 _dl_rtld_map.l_prev = _dl_loaded->l_searchlist.r_list[i - 1];
851 if (__builtin_expect (mode, normal) == normal)
852 _dl_rtld_map.l_next = (i + 1 < _dl_loaded->l_searchlist.r_nlist
853 ? _dl_loaded->l_searchlist.r_list[i + 1]
854 : NULL);
855 else
856 /* In trace mode there might be an invisible object (which we
857 could not find) after the previous one in the search list.
858 In this case it doesn't matter much where we put the
859 interpreter object, so we just initialize the list pointer so
860 that the assertion below holds. */
861 _dl_rtld_map.l_next = _dl_rtld_map.l_prev->l_next;
862
863 assert (_dl_rtld_map.l_prev->l_next == _dl_rtld_map.l_next);
864 _dl_rtld_map.l_prev->l_next = &_dl_rtld_map;
865 if (_dl_rtld_map.l_next)
866 {
867 assert (_dl_rtld_map.l_next->l_prev == _dl_rtld_map.l_prev);
868 _dl_rtld_map.l_next->l_prev = &_dl_rtld_map;
869 }
870 }
871
872 /* Now let us see whether all libraries are available in the
873 versions we need. */
874 {
875 struct version_check_args args;
876 args.doexit = mode == normal;
877 args.dotrace = mode == trace;
878 _dl_receive_error (print_missing_version, version_check_doit, &args);
879 }
880
881 if (__builtin_expect (mode, normal) != normal)
882 {
883 /* We were run just to list the shared libraries. It is
884 important that we do this before real relocation, because the
885 functions we call below for output may no longer work properly
886 after relocation. */
887 if (! _dl_loaded->l_info[DT_NEEDED])
888 _dl_sysdep_message ("\t", "statically linked\n", NULL);
889 else
890 {
891 struct link_map *l;
892
893 for (l = _dl_loaded->l_next; l; l = l->l_next)
894 if (l->l_opencount == 0)
895 /* The library was not found. */
896 _dl_sysdep_message ("\t", l->l_libname->name, " => not found\n",
897 NULL);
898 else
899 {
900 char buf[20], *bp;
901 buf[sizeof buf - 1] = '\0';
902 bp = _itoa_word (l->l_addr, &buf[sizeof buf - 1], 16, 0);
903 while ((size_t) (&buf[sizeof buf - 1] - bp)
904 < sizeof l->l_addr * 2)
905 *--bp = '0';
906 _dl_sysdep_message ("\t", l->l_libname->name, " => ",
907 l->l_name, " (0x", bp, ")\n", NULL);
908 }
909 }
910
911 if (__builtin_expect (mode, trace) != trace)
912 for (i = 1; i < _dl_argc; ++i)
913 {
914 const ElfW(Sym) *ref = NULL;
915 ElfW(Addr) loadbase;
916 lookup_t result;
917 char buf[20], *bp;
918
919 result = _dl_lookup_symbol (_dl_argv[i], _dl_loaded,
920 &ref, _dl_loaded->l_scope,
921 ELF_MACHINE_JMP_SLOT, 1);
922
923 loadbase = LOOKUP_VALUE_ADDRESS (result);
924
925 buf[sizeof buf - 1] = '\0';
926 bp = _itoa_word (ref->st_value, &buf[sizeof buf - 1], 16, 0);
927 while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2)
928 *--bp = '0';
929 _dl_sysdep_message (_dl_argv[i], " found at 0x", bp, NULL);
930 buf[sizeof buf - 1] = '\0';
931 bp = _itoa_word (loadbase, &buf[sizeof buf - 1], 16, 0);
932 while ((size_t) (&buf[sizeof buf - 1] - bp) < sizeof loadbase * 2)
933 *--bp = '0';
934 _dl_sysdep_message (" in object at 0x", bp, "\n", NULL);
935 }
936 else
937 {
938 if (_dl_lazy >= 0)
939 {
940 /* We have to do symbol dependency testing. */
941 struct relocate_args args;
942 struct link_map *l;
943
944 args.lazy = _dl_lazy;
945
946 l = _dl_loaded;
947 while (l->l_next)
948 l = l->l_next;
949 do
950 {
951 if (l != &_dl_rtld_map && l->l_opencount > 0)
952 {
953 args.l = l;
954 _dl_receive_error (print_unresolved, relocate_doit,
955 &args);
956 }
957 l = l->l_prev;
958 } while (l);
959 }
960
961 #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
962 if (version_info)
963 {
964 /* Print more information. This means here, print information
965 about the versions needed. */
966 int first = 1;
967 struct link_map *map = _dl_loaded;
968
969 for (map = _dl_loaded; map != NULL; map = map->l_next)
970 {
971 const char *strtab;
972 ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG];
973 ElfW(Verneed) *ent;
974
975 if (dyn == NULL)
976 continue;
977
978 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
979 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
980
981 if (first)
982 {
983 _dl_sysdep_message ("\n\tVersion information:\n", NULL);
984 first = 0;
985 }
986
987 _dl_sysdep_message ("\t", (map->l_name[0]
988 ? map->l_name : _dl_argv[0]),
989 ":\n", NULL);
990
991 while (1)
992 {
993 ElfW(Vernaux) *aux;
994 struct link_map *needed;
995
996 needed = find_needed (strtab + ent->vn_file);
997 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
998
999 while (1)
1000 {
1001 const char *fname = NULL;
1002
1003 _dl_sysdep_message ("\t\t",
1004 strtab + ent->vn_file,
1005 " (", strtab + aux->vna_name,
1006 ") ",
1007 (aux->vna_flags
1008 & VER_FLG_WEAK
1009 ? "[WEAK] " : ""),
1010 "=> ", NULL);
1011
1012 if (needed != NULL
1013 && match_version (strtab+aux->vna_name, needed))
1014 fname = needed->l_name;
1015
1016 _dl_sysdep_message (fname ?: "not found", "\n",
1017 NULL);
1018
1019 if (aux->vna_next == 0)
1020 /* No more symbols. */
1021 break;
1022
1023 /* Next symbol. */
1024 aux = (ElfW(Vernaux) *) ((char *) aux
1025 + aux->vna_next);
1026 }
1027
1028 if (ent->vn_next == 0)
1029 /* No more dependencies. */
1030 break;
1031
1032 /* Next dependency. */
1033 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
1034 }
1035 }
1036 }
1037 }
1038
1039 _exit (0);
1040 }
1041
1042 {
1043 /* Now we have all the objects loaded. Relocate them all except for
1044 the dynamic linker itself. We do this in reverse order so that copy
1045 relocs of earlier objects overwrite the data written by later
1046 objects. We do not re-relocate the dynamic linker itself in this
1047 loop because that could result in the GOT entries for functions we
1048 call being changed, and that would break us. It is safe to relocate
1049 the dynamic linker out of order because it has no copy relocs (we
1050 know that because it is self-contained). */
1051
1052 struct link_map *l;
1053 int consider_profiling = _dl_profile != NULL;
1054 #ifndef HP_TIMING_NONAVAIL
1055 hp_timing_t start;
1056 hp_timing_t stop;
1057 hp_timing_t add;
1058 #endif
1059
1060 /* If we are profiling we also must do lazy reloaction. */
1061 _dl_lazy |= consider_profiling;
1062
1063 l = _dl_loaded;
1064 while (l->l_next)
1065 l = l->l_next;
1066
1067 HP_TIMING_NOW (start);
1068 do
1069 {
1070 /* While we are at it, help the memory handling a bit. We have to
1071 mark some data structures as allocated with the fake malloc()
1072 implementation in ld.so. */
1073 struct libname_list *lnp = l->l_libname->next;
1074
1075 while (__builtin_expect (lnp != NULL, 0))
1076 {
1077 lnp->dont_free = 1;
1078 lnp = lnp->next;
1079 }
1080
1081 if (l != &_dl_rtld_map)
1082 _dl_relocate_object (l, l->l_scope, _dl_lazy, consider_profiling);
1083
1084 l = l->l_prev;
1085 }
1086 while (l);
1087 HP_TIMING_NOW (stop);
1088
1089 HP_TIMING_DIFF (relocate_time, start, stop);
1090
1091 /* Do any necessary cleanups for the startup OS interface code.
1092 We do these now so that no calls are made after rtld re-relocation
1093 which might be resolved to different functions than we expect.
1094 We cannot do this before relocating the other objects because
1095 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
1096 _dl_sysdep_start_cleanup ();
1097
1098 /* Now enable profiling if needed. Like the previous call,
1099 this has to go here because the calls it makes should use the
1100 rtld versions of the functions (particularly calloc()), but it
1101 needs to have _dl_profile_map set up by the relocator. */
1102 if (__builtin_expect (_dl_profile_map != NULL, 0))
1103 /* We must prepare the profiling. */
1104 _dl_start_profile (_dl_profile_map, _dl_profile_output);
1105
1106 if (_dl_rtld_map.l_opencount > 1)
1107 {
1108 /* There was an explicit ref to the dynamic linker as a shared lib.
1109 Re-relocate ourselves with user-controlled symbol definitions. */
1110 HP_TIMING_NOW (start);
1111 _dl_relocate_object (&_dl_rtld_map, _dl_loaded->l_scope, 0, 0);
1112 HP_TIMING_NOW (stop);
1113 HP_TIMING_DIFF (add, start, stop);
1114 HP_TIMING_ACCUM_NT (relocate_time, add);
1115 }
1116 }
1117
1118 /* Now set up the variable which helps the assembler startup code. */
1119 _dl_main_searchlist = &_dl_loaded->l_searchlist;
1120 _dl_global_scope[0] = &_dl_loaded->l_searchlist;
1121
1122 /* Safe the information about the original global scope list since
1123 we need it in the memory handling later. */
1124 _dl_initial_searchlist = *_dl_main_searchlist;
1125
1126 {
1127 /* Initialize _r_debug. */
1128 struct r_debug *r = _dl_debug_initialize (_dl_rtld_map.l_addr);
1129 struct link_map *l;
1130
1131 l = _dl_loaded;
1132
1133 #ifdef ELF_MACHINE_DEBUG_SETUP
1134
1135 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
1136
1137 ELF_MACHINE_DEBUG_SETUP (l, r);
1138 ELF_MACHINE_DEBUG_SETUP (&_dl_rtld_map, r);
1139
1140 #else
1141
1142 if (l->l_info[DT_DEBUG])
1143 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
1144 with the run-time address of the r_debug structure */
1145 l->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1146
1147 /* Fill in the pointer in the dynamic linker's own dynamic section, in
1148 case you run gdb on the dynamic linker directly. */
1149 if (_dl_rtld_map.l_info[DT_DEBUG])
1150 _dl_rtld_map.l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1151
1152 #endif
1153
1154 /* Notify the debugger that all objects are now mapped in. */
1155 r->r_state = RT_ADD;
1156 _dl_debug_state ();
1157 }
1158
1159 #ifndef MAP_COPY
1160 /* We must munmap() the cache file. */
1161 _dl_unload_cache ();
1162 #endif
1163
1164 /* Once we return, _dl_sysdep_start will invoke
1165 the DT_INIT functions and then *USER_ENTRY. */
1166 }
1167 \f
1168 /* This is a little helper function for resolving symbols while
1169 tracing the binary. */
1170 static void
1171 print_unresolved (int errcode __attribute__ ((unused)), const char *objname,
1172 const char *errstring)
1173 {
1174 if (objname[0] == '\0')
1175 objname = _dl_argv[0] ?: "<main program>";
1176 _dl_sysdep_error (errstring, " (", objname, ")\n", NULL);
1177 }
1178 \f
1179 /* This is a little helper function for resolving symbols while
1180 tracing the binary. */
1181 static void
1182 print_missing_version (int errcode __attribute__ ((unused)),
1183 const char *objname, const char *errstring)
1184 {
1185 _dl_sysdep_error (_dl_argv[0] ?: "<program name unknown>", ": ",
1186 objname, ": ", errstring, "\n", NULL);
1187 }
1188 \f
1189 /* Nonzero if any of the debugging options is enabled. */
1190 static int any_debug;
1191
1192 /* Process the string given as the parameter which explains which debugging
1193 options are enabled. */
1194 static void
1195 process_dl_debug (const char *dl_debug)
1196 {
1197 size_t len;
1198 #define separators " ,:"
1199 do
1200 {
1201 len = 0;
1202 /* Skip separating white spaces and commas. */
1203 dl_debug += strspn (dl_debug, separators);
1204 if (*dl_debug != '\0')
1205 {
1206 len = strcspn (dl_debug, separators);
1207
1208 switch (len)
1209 {
1210 case 3:
1211 /* This option is not documented since it is not generally
1212 useful. */
1213 if (memcmp (dl_debug, "all", 3) == 0)
1214 {
1215 _dl_debug_libs = 1;
1216 _dl_debug_impcalls = 1;
1217 _dl_debug_reloc = 1;
1218 _dl_debug_files = 1;
1219 _dl_debug_symbols = 1;
1220 _dl_debug_bindings = 1;
1221 _dl_debug_versions = 1;
1222 any_debug = 1;
1223 continue;
1224 }
1225 break;
1226
1227 case 4:
1228 if (memcmp (dl_debug, "help", 4) == 0)
1229 {
1230 _dl_sysdep_message ("\
1231 Valid options for the LD_DEBUG environment variable are:\n\
1232 \n\
1233 bindings display information about symbol binding\n\
1234 files display processing of files and libraries\n\
1235 help display this help message and exit\n\
1236 libs display library search paths\n\
1237 reloc display relocation processing\n\
1238 statistics display relocation statistics\n\
1239 symbols display symbol table processing\n\
1240 versions display version dependencies\n\
1241 \n\
1242 To direct the debugging output into a file instead of standard output\n\
1243 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n",
1244 NULL);
1245 _exit (0);
1246 }
1247
1248 if (memcmp (dl_debug, "libs", 4) == 0)
1249 {
1250 _dl_debug_libs = 1;
1251 _dl_debug_impcalls = 1;
1252 any_debug = 1;
1253 continue;
1254 }
1255 break;
1256
1257 case 5:
1258 if (memcmp (dl_debug, "reloc", 5) == 0)
1259 {
1260 _dl_debug_reloc = 1;
1261 _dl_debug_impcalls = 1;
1262 any_debug = 1;
1263 continue;
1264 }
1265
1266 if (memcmp (dl_debug, "files", 5) == 0)
1267 {
1268 _dl_debug_files = 1;
1269 _dl_debug_impcalls = 1;
1270 any_debug = 1;
1271 continue;
1272 }
1273 break;
1274
1275 case 7:
1276 if (memcmp (dl_debug, "symbols", 7) == 0)
1277 {
1278 _dl_debug_symbols = 1;
1279 _dl_debug_impcalls = 1;
1280 any_debug = 1;
1281 continue;
1282 }
1283 break;
1284
1285 case 8:
1286 if (memcmp (dl_debug, "bindings", 8) == 0)
1287 {
1288 _dl_debug_bindings = 1;
1289 _dl_debug_impcalls = 1;
1290 any_debug = 1;
1291 continue;
1292 }
1293
1294 if (memcmp (dl_debug, "versions", 8) == 0)
1295 {
1296 _dl_debug_versions = 1;
1297 _dl_debug_impcalls = 1;
1298 any_debug = 1;
1299 continue;
1300 }
1301 break;
1302
1303 case 10:
1304 if (memcmp (dl_debug, "statistics", 10) == 0)
1305 {
1306 _dl_debug_statistics = 1;
1307 continue;
1308 }
1309 break;
1310
1311 default:
1312 break;
1313 }
1314
1315 {
1316 /* Display a warning and skip everything until next separator. */
1317 char *startp = strndupa (dl_debug, len);
1318 _dl_sysdep_error ("warning: debug option `", startp,
1319 "' unknown; try LD_DEBUG=help\n", NULL);
1320 break;
1321 }
1322 }
1323 }
1324 while (*(dl_debug += len) != '\0');
1325 }
1326 \f
1327 /* Process all environments variables the dynamic linker must recognize.
1328 Since all of them start with `LD_' we are a bit smarter while finding
1329 all the entries. */
1330 static void
1331 process_envvars (enum mode *modep, int *lazyp)
1332 {
1333 char **runp = NULL;
1334 char *envline;
1335 enum mode mode = normal;
1336 int bind_now = 0;
1337 char *debug_output = NULL;
1338
1339 /* This is the default place for profiling data file. */
1340 _dl_profile_output = "/var/tmp";
1341
1342 while ((envline = _dl_next_ld_env_entry (&runp)) != NULL)
1343 {
1344 size_t len = strcspn (envline, "=");
1345
1346 if (envline[len] != '=')
1347 /* This is a "LD_" variable at the end of the string without
1348 a '=' character. Ignore it since otherwise we will access
1349 invalid memory below. */
1350 break;
1351
1352 switch (len - 3)
1353 {
1354 case 4:
1355 /* Warning level, verbose or not. */
1356 if (memcmp (&envline[3], "WARN", 4) == 0)
1357 _dl_verbose = envline[8] != '\0';
1358 break;
1359
1360 case 5:
1361 /* Debugging of the dynamic linker? */
1362 if (memcmp (&envline[3], "DEBUG", 5) == 0)
1363 process_dl_debug (&envline[9]);
1364 break;
1365
1366 case 7:
1367 /* Print information about versions. */
1368 if (memcmp (&envline[3], "VERBOSE", 7) == 0)
1369 {
1370 version_info = envline[11] != '\0';
1371 break;
1372 }
1373
1374 /* List of objects to be preloaded. */
1375 if (memcmp (&envline[3], "PRELOAD", 7) == 0)
1376 {
1377 preloadlist = &envline[11];
1378 break;
1379 }
1380
1381 /* Which shared object shall be profiled. */
1382 if (memcmp (&envline[3], "PROFILE", 7) == 0)
1383 _dl_profile = &envline[11];
1384 break;
1385
1386 case 8:
1387 /* Do we bind early? */
1388 if (memcmp (&envline[3], "BIND_NOW", 8) == 0)
1389 {
1390 bind_now = envline[12] != '\0';
1391 break;
1392 }
1393 if (memcmp (&envline[3], "BIND_NOT", 8) == 0)
1394 _dl_bind_not = envline[12] != '\0';
1395 break;
1396
1397 case 9:
1398 /* Test whether we want to see the content of the auxiliary
1399 array passed up from the kernel. */
1400 if (memcmp (&envline[3], "SHOW_AUXV", 9) == 0)
1401 _dl_show_auxv ();
1402 break;
1403
1404 case 10:
1405 /* Mask for the important hardware capabilities. */
1406 if (memcmp (&envline[3], "HWCAP_MASK", 10) == 0)
1407 _dl_hwcap_mask = strtoul (&envline[14], NULL, 0);
1408 break;
1409
1410 case 11:
1411 /* Path where the binary is found. */
1412 if (!__libc_enable_secure
1413 && memcmp (&envline[3], "ORIGIN_PATH", 11) == 0)
1414 _dl_origin_path = &envline[15];
1415 break;
1416
1417 case 12:
1418 /* The library search path. */
1419 if (memcmp (&envline[3], "LIBRARY_PATH", 12) == 0)
1420 {
1421 library_path = &envline[16];
1422 break;
1423 }
1424
1425 /* Where to place the profiling data file. */
1426 if (memcmp (&envline[3], "DEBUG_OUTPUT", 12) == 0)
1427 {
1428 debug_output = &envline[16];
1429 break;
1430 }
1431
1432 if (memcmp (&envline[3], "DYNAMIC_WEAK", 12) == 0)
1433 _dl_dynamic_weak = 1;
1434 break;
1435
1436 case 14:
1437 /* Where to place the profiling data file. */
1438 if (!__libc_enable_secure
1439 && memcmp (&envline[3], "PROFILE_OUTPUT", 14) == 0)
1440 {
1441 _dl_profile_output = &envline[18];
1442 if (*_dl_profile_output == '\0')
1443 _dl_profile_output = "/var/tmp";
1444 }
1445 break;
1446
1447 case 20:
1448 /* The mode of the dynamic linker can be set. */
1449 if (memcmp (&envline[3], "TRACE_LOADED_OBJECTS", 20) == 0)
1450 mode = trace;
1451 break;
1452
1453 /* We might have some extra environment variable to handle. This
1454 is tricky due to the pre-processing of the length of the name
1455 in the switch statement here. The code here assumes that added
1456 environment variables have a different length. */
1457 #ifdef EXTRA_LD_ENVVARS
1458 EXTRA_LD_ENVVARS
1459 #endif
1460 }
1461 }
1462
1463 /* Extra security for SUID binaries. Remove all dangerous environment
1464 variables. */
1465 if (__libc_enable_secure)
1466 {
1467 static const char *unsecure_envvars[] =
1468 {
1469 UNSECURE_ENVVARS,
1470 #ifdef EXTRA_UNSECURE_ENVVARS
1471 EXTRA_UNSECURE_ENVVARS
1472 #endif
1473 };
1474 size_t cnt;
1475
1476 if (preloadlist != NULL)
1477 unsetenv ("LD_PRELOAD");
1478 if (library_path != NULL)
1479 unsetenv ("LD_LIBRARY_PATH");
1480 if (_dl_origin_path != NULL)
1481 unsetenv ("LD_ORIGIN_PATH");
1482 if (debug_output != NULL)
1483 unsetenv ("LD_DEBUG_OUTPUT");
1484 if (_dl_profile != NULL)
1485 unsetenv ("LD_PROFILE");
1486
1487 for (cnt = 0;
1488 cnt < sizeof (unsecure_envvars) / sizeof (unsecure_envvars[0]);
1489 ++cnt)
1490 unsetenv (unsecure_envvars[cnt]);
1491
1492 if (__access ("/etc/suid-debug", F_OK) != 0)
1493 unsetenv ("MALLOC_CHECK_");
1494 }
1495
1496 /* The name of the object to profile cannot be empty. */
1497 if (_dl_profile != NULL && *_dl_profile == '\0')
1498 _dl_profile = NULL;
1499
1500 /* If we have to run the dynamic linker in debugging mode and the
1501 LD_DEBUG_OUTPUT environment variable is given, we write the debug
1502 messages to this file. */
1503 if (any_debug && debug_output != NULL && !__libc_enable_secure)
1504 {
1505 #ifdef O_NOFOLLOW
1506 const int flags = O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW;
1507 #else
1508 const int flags = O_WRONLY | O_APPEND | O_CREAT;
1509 #endif
1510 size_t name_len = strlen (debug_output);
1511 char buf[name_len + 12];
1512 char *startp;
1513
1514 buf[name_len + 11] = '\0';
1515 startp = _itoa_word (__getpid (), &buf[name_len + 11], 10, 0);
1516 *--startp = '.';
1517 startp = memcpy (startp - name_len, debug_output, name_len);
1518
1519 _dl_debug_fd = __open (startp, flags, 0666);
1520 if (_dl_debug_fd == -1)
1521 /* We use standard output if opening the file failed. */
1522 _dl_debug_fd = STDOUT_FILENO;
1523 }
1524
1525 /* LAZY is determined by the environment variable LD_WARN and
1526 LD_BIND_NOW if we trace the binary. */
1527 if (__builtin_expect (mode, normal) == trace)
1528 *lazyp = _dl_verbose ? !bind_now : -1;
1529 else
1530 *lazyp = !bind_now;
1531
1532 *modep = mode;
1533 }
1534
1535
1536 /* Print the various times we collected. */
1537 static void
1538 print_statistics (void)
1539 {
1540 char buf[200];
1541 #ifndef HP_TIMING_NONAVAIL
1542 char *cp;
1543 char *wp;
1544
1545 /* Total time rtld used. */
1546 if (HP_TIMING_AVAIL)
1547 {
1548 HP_TIMING_PRINT (buf, sizeof (buf), rtld_total_time);
1549 _dl_debug_message (1, "\nruntime linker statistics:\n"
1550 " total startup time in dynamic loader: ",
1551 buf, "\n", NULL);
1552 }
1553
1554 /* Print relocation statistics. */
1555 if (HP_TIMING_AVAIL)
1556 {
1557 HP_TIMING_PRINT (buf, sizeof (buf), relocate_time);
1558 _dl_debug_message (1, " time needed for relocation: ", buf,
1559 NULL);
1560 cp = _itoa_word ((1000 * relocate_time) / rtld_total_time,
1561 buf + sizeof (buf), 10, 0);
1562 wp = buf;
1563 switch (buf + sizeof (buf) - cp)
1564 {
1565 case 3:
1566 *wp++ = *cp++;
1567 case 2:
1568 *wp++ = *cp++;
1569 case 1:
1570 *wp++ = '.';
1571 *wp++ = *cp++;
1572 }
1573 *wp = '\0';
1574 _dl_debug_message (0, " (", buf, "%)\n", NULL);
1575 }
1576 #endif
1577 buf[sizeof (buf) - 1] = '\0';
1578 _dl_debug_message (1, " number of relocations: ",
1579 _itoa_word (_dl_num_relocations,
1580 buf + sizeof (buf) - 1, 10, 0),
1581 "\n", NULL);
1582
1583 #ifndef HP_TIMING_NONAVAIL
1584 /* Time spend while loading the object and the dependencies. */
1585 if (HP_TIMING_AVAIL)
1586 {
1587 HP_TIMING_PRINT (buf, sizeof (buf), load_time);
1588 _dl_debug_message (1, " time needed to load objects: ", buf,
1589 NULL);
1590 cp = _itoa_word ((1000 * load_time) / rtld_total_time,
1591 buf + sizeof (buf), 10, 0);
1592 wp = buf;
1593 switch (buf + sizeof (buf) - cp)
1594 {
1595 case 3:
1596 *wp++ = *cp++;
1597 case 2:
1598 *wp++ = *cp++;
1599 case 1:
1600 *wp++ = '.';
1601 *wp++ = *cp++;
1602 }
1603 *wp = '\0';
1604 _dl_debug_message (0, " (", buf, "%)\n", NULL);
1605 }
1606 #endif
1607 }
This page took 0.112608 seconds and 5 git commands to generate.