]> git.ipfire.org Git - thirdparty/glibc.git/blob - elf/rtld.c
* elf/rtld.c (_dl_start_final): Move _begin, _end decls outside the fn.
[thirdparty/glibc.git] / elf / rtld.c
1 /* Run time dynamic linker.
2 Copyright (C) 1995-1999, 2000, 2001, 2002 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <stdbool.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 <sys/param.h>
28 #include <sys/stat.h>
29 #include <ldsodefs.h>
30 #include <stdio-common/_itoa.h>
31 #include <entry.h>
32 #include <fpu_control.h>
33 #include <hp-timing.h>
34 #include <bits/libc-lock.h>
35 #include "dynamic-link.h"
36 #include "dl-librecon.h"
37 #include <unsecvars.h>
38 #include <dl-cache.h>
39 #include <dl-procinfo.h>
40
41 #include <assert.h>
42
43 /* Avoid PLT use for our local calls at startup. */
44 extern __typeof (__mempcpy) __mempcpy attribute_hidden;
45
46 /* GCC has mental blocks about _exit. */
47 extern __typeof (_exit) exit_internal asm ("_exit") attribute_hidden;
48 #define _exit exit_internal
49
50 /* Helper function to handle errors while resolving symbols. */
51 static void print_unresolved (int errcode, const char *objname,
52 const char *errsting);
53
54 /* Helper function to handle errors when a version is missing. */
55 static void print_missing_version (int errcode, const char *objname,
56 const char *errsting);
57
58 /* Print the various times we collected. */
59 static void print_statistics (void);
60
61 /* This is a list of all the modes the dynamic loader can be in. */
62 enum mode { normal, list, verify, trace };
63
64 /* Process all environments variables the dynamic linker must recognize.
65 Since all of them start with `LD_' we are a bit smarter while finding
66 all the entries. */
67 static void process_envvars (enum mode *modep);
68
69 int _dl_argc attribute_hidden;
70 char **_dl_argv = NULL;
71 INTDEF(_dl_argv)
72
73 /* Nonzero if we were run directly. */
74 unsigned int _dl_skip_args attribute_hidden;
75
76 /* Set nonzero during loading and initialization of executable and
77 libraries, cleared before the executable's entry point runs. This
78 must not be initialized to nonzero, because the unused dynamic
79 linker loaded in for libc.so's "ld.so.1" dep will provide the
80 definition seen by libc.so's initializer; that value must be zero,
81 and will be since that dynamic linker's _dl_start and dl_main will
82 never be called. */
83 int _dl_starting_up = 0;
84 INTVARDEF(_dl_starting_up)
85
86 /* This is the structure which defines all variables global to ld.so
87 (except those which cannot be added for some reason). */
88 struct rtld_global _rtld_global =
89 {
90 /* Get architecture specific initializer. */
91 #include <dl-procinfo.c>
92 ._dl_debug_fd = STDERR_FILENO,
93 #if 1
94 /* XXX I know about at least one case where we depend on the old
95 weak behavior (it has to do with librt). Until we get DSO
96 groups implemented we have to make this the default.
97 Bummer. --drepper */
98 ._dl_dynamic_weak = 1,
99 #endif
100 ._dl_lazy = 1,
101 ._dl_fpu_control = _FPU_DEFAULT,
102 ._dl_correct_cache_id = _DL_CACHE_DEFAULT_ID,
103 ._dl_hwcap_mask = HWCAP_IMPORTANT,
104 #ifdef _LIBC_REENTRANT
105 ._dl_load_lock = _LIBC_LOCK_RECURSIVE_INITIALIZER
106 #endif
107 };
108 strong_alias (_rtld_global, _rtld_local);
109
110 static void dl_main (const ElfW(Phdr) *phdr, ElfW(Word) phnum,
111 ElfW(Addr) *user_entry);
112
113 static struct libname_list _dl_rtld_libname;
114 static struct libname_list _dl_rtld_libname2;
115
116 /* We expect less than a second for relocation. */
117 #ifdef HP_SMALL_TIMING_AVAIL
118 # undef HP_TIMING_AVAIL
119 # define HP_TIMING_AVAIL HP_SMALL_TIMING_AVAIL
120 #endif
121
122 /* Variable for statistics. */
123 #ifndef HP_TIMING_NONAVAIL
124 static hp_timing_t rtld_total_time;
125 static hp_timing_t relocate_time;
126 static hp_timing_t load_time;
127 static hp_timing_t start_time;
128 #endif
129
130 /* Additional definitions needed by TLS initialization. */
131 #ifdef TLS_INIT_HELPER
132 TLS_INIT_HELPER
133 #endif
134
135 /* Before ld.so is relocated we must not access variables which need
136 relocations. This means variables which are exported. Variables
137 declared as static are fine. If we can mark a variable hidden this
138 is fine, too. The latter is impotant here. We can avoid setting
139 up a temporary link map for ld.so if we can mark _rtld_global as
140 hidden. */
141 #if defined PI_STATIC_AND_HIDDEN && defined HAVE_HIDDEN \
142 && defined HAVE_VISIBILITY_ATTRIBUTE
143 # define DONT_USE_BOOTSTRAP_MAP 1
144 #endif
145
146 #ifdef DONT_USE_BOOTSTRAP_MAP
147 static ElfW(Addr) _dl_start_final (void *arg);
148 #else
149 static ElfW(Addr) _dl_start_final (void *arg,
150 struct link_map *bootstrap_map_p);
151 #endif
152
153 /* These defined magically in the linker script. */
154 extern char _begin[] attribute_hidden;
155 extern char _end[] attribute_hidden;
156
157
158 #ifdef RTLD_START
159 RTLD_START
160 #else
161 # error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
162 #endif
163
164 #ifndef VALIDX
165 # define VALIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
166 + DT_EXTRANUM + DT_VALTAGIDX (tag))
167 #endif
168 #ifndef ADDRIDX
169 # define ADDRIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
170 + DT_EXTRANUM + DT_VALNUM + DT_ADDRTAGIDX (tag))
171 #endif
172
173 /* This is the second half of _dl_start (below). It can be inlined safely
174 under DONT_USE_BOOTSTRAP_MAP, where it is careful not to make any GOT
175 references. When the tools don't permit us to avoid using a GOT entry
176 for _dl_rtld_global (no attribute_hidden support), we must make sure
177 this function is not inlined (see below). */
178
179 #ifdef DONT_USE_BOOTSTRAP_MAP
180 static inline ElfW(Addr) __attribute__ ((always_inline))
181 _dl_start_final (void *arg)
182 #else
183 static ElfW(Addr) __attribute__ ((noinline))
184 _dl_start_final (void *arg, struct link_map *bootstrap_map_p)
185 #endif
186 {
187 ElfW(Addr) start_addr;
188
189 if (HP_TIMING_AVAIL)
190 {
191 /* If it hasn't happen yet record the startup time. */
192 if (! HP_TIMING_INLINE)
193 HP_TIMING_NOW (start_time);
194
195 /* Initialize the timing functions. */
196 HP_TIMING_DIFF_INIT ();
197 }
198
199 /* Transfer data about ourselves to the permanent link_map structure. */
200 #ifndef DONT_USE_BOOTSTRAP_MAP
201 GL(dl_rtld_map).l_addr = bootstrap_map_p->l_addr;
202 GL(dl_rtld_map).l_ld = bootstrap_map_p->l_ld;
203 memcpy (GL(dl_rtld_map).l_info, bootstrap_map_p->l_info,
204 sizeof GL(dl_rtld_map).l_info);
205 GL(dl_rtld_map).l_mach = bootstrap_map_p->l_mach;
206 #endif
207 _dl_setup_hash (&GL(dl_rtld_map));
208 GL(dl_rtld_map).l_opencount = 1;
209 GL(dl_rtld_map).l_map_start = (ElfW(Addr)) _begin;
210 GL(dl_rtld_map).l_map_end = (ElfW(Addr)) _end;
211 /* Copy the TLS related data if necessary. */
212 #if USE_TLS && !defined DONT_USE_BOOTSTRAP_MAP
213 # ifdef HAVE___THREAD
214 assert (bootstrap_map_p->l_tls_modid != 0);
215 # else
216 if (bootstrap_map_p->l_tls_modid != 0)
217 # endif
218 {
219 GL(dl_rtld_map).l_tls_blocksize = bootstrap_map_p->l_tls_blocksize;
220 GL(dl_rtld_map).l_tls_align = bootstrap_map_p->l_tls_align;
221 GL(dl_rtld_map).l_tls_initimage_size
222 = bootstrap_map_p->l_tls_initimage_size;
223 GL(dl_rtld_map).l_tls_initimage = bootstrap_map_p->l_tls_initimage;
224 GL(dl_rtld_map).l_tls_offset = bootstrap_map_p->l_tls_offset;
225 GL(dl_rtld_map).l_tls_modid = 1;
226 GL(dl_rtld_map).l_tls_tp_initialized
227 = bootstrap_map_p->l_tls_tp_initialized;
228 }
229 #endif
230
231 #if HP_TIMING_AVAIL
232 HP_TIMING_NOW (GL(dl_cpuclock_offset));
233 #endif
234
235 /* Call the OS-dependent function to set up life so we can do things like
236 file access. It will call `dl_main' (below) to do all the real work
237 of the dynamic linker, and then unwind our frame and run the user
238 entry point on the same stack we entered on. */
239 start_addr = _dl_sysdep_start (arg, &dl_main);
240
241 #ifndef HP_TIMING_NONAVAIL
242 if (HP_TIMING_AVAIL)
243 {
244 hp_timing_t end_time;
245
246 /* Get the current time. */
247 HP_TIMING_NOW (end_time);
248
249 /* Compute the difference. */
250 HP_TIMING_DIFF (rtld_total_time, start_time, end_time);
251 }
252 #endif
253
254 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_STATISTICS, 0))
255 print_statistics ();
256
257 return start_addr;
258 }
259
260 static ElfW(Addr) __attribute_used__ internal_function
261 _dl_start (void *arg)
262 {
263 #ifdef DONT_USE_BOOTSTRAP_MAP
264 # define bootstrap_map GL(dl_rtld_map)
265 #else
266 struct link_map bootstrap_map;
267 #endif
268 #if !defined HAVE_BUILTIN_MEMSET || defined USE_TLS
269 size_t cnt;
270 #endif
271 #ifdef USE_TLS
272 ElfW(Ehdr) *ehdr;
273 ElfW(Phdr) *phdr;
274 dtv_t initdtv[3];
275 #endif
276
277 /* This #define produces dynamic linking inline functions for
278 bootstrap relocation instead of general-purpose relocation. */
279 #define RTLD_BOOTSTRAP
280 #define RESOLVE_MAP(sym, version, flags) \
281 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : &bootstrap_map)
282 #define RESOLVE(sym, version, flags) \
283 ((*(sym))->st_shndx == SHN_UNDEF ? 0 : bootstrap_map.l_addr)
284 #include "dynamic-link.h"
285
286 if (HP_TIMING_INLINE && HP_TIMING_AVAIL)
287 HP_TIMING_NOW (start_time);
288
289 /* Partly clean the `bootstrap_map' structure up. Don't use
290 `memset' since it might not be built in or inlined and we cannot
291 make function calls at this point. Use '__builtin_memset' if we
292 know it is available. We do not have to clear the memory if we
293 do not have to use the temporary bootstrap_map. Global variables
294 are initialized to zero by default. */
295 #ifndef DONT_USE_BOOTSTRAP_MAP
296 # ifdef HAVE_BUILTIN_MEMSET
297 __builtin_memset (bootstrap_map.l_info, '\0', sizeof (bootstrap_map.l_info));
298 # else
299 for (cnt = 0;
300 cnt < sizeof (bootstrap_map.l_info) / sizeof (bootstrap_map.l_info[0]);
301 ++cnt)
302 bootstrap_map.l_info[cnt] = 0;
303 # endif
304 #endif
305
306 /* Figure out the run-time load address of the dynamic linker itself. */
307 bootstrap_map.l_addr = elf_machine_load_address ();
308
309 /* Read our own dynamic section and fill in the info array. */
310 bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
311 elf_get_dynamic_info (&bootstrap_map);
312
313 #if USE_TLS
314 # if !defined HAVE___THREAD && !defined DONT_USE_BOOTSTRAP_MAP
315 /* Signal that we have not found TLS data so far. */
316 bootstrap_map.l_tls_modid = 0;
317 # endif
318
319 /* Get the dynamic linker's own program header. First we need the ELF
320 file header. The `_begin' symbol created by the linker script points
321 to it. When we have something like GOTOFF relocs, we can use a plain
322 reference to find the runtime address. Without that, we have to rely
323 on the `l_addr' value, which is not the value we want when prelinked. */
324 #ifdef DONT_USE_BOOTSTRAP_MAP
325 ehdr = (ElfW(Ehdr) *) &_begin;
326 #else
327 ehdr = (ElfW(Ehdr) *) bootstrap_map.l_addr;
328 #endif
329 phdr = (ElfW(Phdr) *) ((ElfW(Addr)) ehdr + ehdr->e_phoff);
330 for (cnt = 0; cnt < ehdr->e_phnum; ++cnt)
331 if (phdr[cnt].p_type == PT_TLS)
332 {
333 void *tlsblock;
334 size_t max_align = MAX (TLS_INIT_TCB_ALIGN, phdr[cnt].p_align);
335 char *p;
336
337 bootstrap_map.l_tls_blocksize = phdr[cnt].p_memsz;
338 bootstrap_map.l_tls_align = phdr[cnt].p_align;
339 assert (bootstrap_map.l_tls_blocksize != 0);
340 bootstrap_map.l_tls_initimage_size = phdr[cnt].p_filesz;
341 bootstrap_map.l_tls_initimage = (void *) (bootstrap_map.l_addr
342 + phdr[cnt].p_vaddr);
343
344 /* We can now allocate the initial TLS block. This can happen
345 on the stack. We'll get the final memory later when we
346 know all about the various objects loaded at startup
347 time. */
348 # if TLS_TCB_AT_TP
349 tlsblock = alloca (roundup (bootstrap_map.l_tls_blocksize,
350 TLS_INIT_TCB_ALIGN)
351 + TLS_INIT_TCB_SIZE
352 + max_align);
353 # elif TLS_DTV_AT_TP
354 tlsblock = alloca (roundup (TLS_INIT_TCB_SIZE,
355 bootstrap_map.l_tls_align)
356 + bootstrap_map.l_tls_blocksize
357 + max_align);
358 # else
359 /* In case a model with a different layout for the TCB and DTV
360 is defined add another #elif here and in the following #ifs. */
361 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
362 # endif
363 /* Align the TLS block. */
364 tlsblock = (void *) (((uintptr_t) tlsblock + max_align - 1)
365 & ~(max_align - 1));
366
367 /* Initialize the dtv. [0] is the length, [1] the generation
368 counter. */
369 initdtv[0].counter = 1;
370 initdtv[1].counter = 0;
371
372 /* Initialize the TLS block. */
373 # if TLS_TCB_AT_TP
374 initdtv[2].pointer = tlsblock;
375 # elif TLS_DTV_AT_TP
376 bootstrap_map.l_tls_offset = roundup (TLS_INIT_TCB_SIZE,
377 bootstrap_map.l_tls_align);
378 initdtv[2].pointer = (char *) tlsblock + bootstrap_map.l_tls_offset;
379 # else
380 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
381 # endif
382 p = __mempcpy (initdtv[2].pointer, bootstrap_map.l_tls_initimage,
383 bootstrap_map.l_tls_initimage_size);
384 # ifdef HAVE_BUILTIN_MEMSET
385 __builtin_memset (p, '\0', (bootstrap_map.l_tls_blocksize
386 - bootstrap_map.l_tls_initimage_size));
387 # else
388 {
389 size_t remaining = (bootstrap_map.l_tls_blocksize
390 - bootstrap_map.l_tls_initimage_size);
391 while (remaining-- > 0)
392 *p++ = '\0';
393 }
394 #endif
395
396 /* Install the pointer to the dtv. */
397
398 /* Initialize the thread pointer. */
399 # if TLS_TCB_AT_TP
400 bootstrap_map.l_tls_offset
401 = roundup (bootstrap_map.l_tls_blocksize, TLS_INIT_TCB_ALIGN);
402
403 INSTALL_DTV ((char *) tlsblock + bootstrap_map.l_tls_offset,
404 initdtv);
405
406 if (TLS_INIT_TP ((char *) tlsblock + bootstrap_map.l_tls_offset, 0)
407 != 0)
408 _dl_fatal_printf ("cannot setup thread-local storage\n");
409 # elif TLS_DTV_AT_TP
410 INSTALL_DTV (tlsblock, initdtv);
411 if (TLS_INIT_TP (tlsblock, 0) != 0)
412 _dl_fatal_printf ("cannot setup thread-local storage\n");
413 # else
414 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
415 # endif
416
417 /* So far this is module number one. */
418 bootstrap_map.l_tls_modid = 1;
419 /* The TP got initialized. */
420 bootstrap_map.l_tls_tp_initialized = 1;
421
422 /* There can only be one PT_TLS entry. */
423 break;
424 }
425 #endif /* use TLS */
426
427 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
428 ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info);
429 #endif
430
431 if (bootstrap_map.l_addr || ! bootstrap_map.l_info[VALIDX(DT_GNU_PRELINKED)])
432 {
433 /* Relocate ourselves so we can do normal function calls and
434 data access using the global offset table. */
435
436 ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0, 0);
437 }
438
439 /* Please note that we don't allow profiling of this object and
440 therefore need not test whether we have to allocate the array
441 for the relocation results (as done in dl-reloc.c). */
442
443 /* Now life is sane; we can call functions and access global data.
444 Set up to use the operating system facilities, and find out from
445 the operating system's program loader where to find the program
446 header table in core. Put the rest of _dl_start into a separate
447 function, that way the compiler cannot put accesses to the GOT
448 before ELF_DYNAMIC_RELOCATE. */
449 {
450 #ifdef DONT_USE_BOOTSTRAP_MAP
451 ElfW(Addr) entry = _dl_start_final (arg);
452 #else
453 ElfW(Addr) entry = _dl_start_final (arg, &bootstrap_map);
454 #endif
455
456 #ifndef ELF_MACHINE_START_ADDRESS
457 # define ELF_MACHINE_START_ADDRESS(map, start) (start)
458 #endif
459
460 return ELF_MACHINE_START_ADDRESS (GL(dl_loaded), entry);
461 }
462 }
463
464
465
466 /* Now life is peachy; we can do all normal operations.
467 On to the real work. */
468
469 /* Some helper functions. */
470
471 /* Arguments to relocate_doit. */
472 struct relocate_args
473 {
474 struct link_map *l;
475 int lazy;
476 };
477
478 struct map_args
479 {
480 /* Argument to map_doit. */
481 char *str;
482 /* Return value of map_doit. */
483 struct link_map *main_map;
484 };
485
486 /* Arguments to version_check_doit. */
487 struct version_check_args
488 {
489 int doexit;
490 int dotrace;
491 };
492
493 static void
494 relocate_doit (void *a)
495 {
496 struct relocate_args *args = (struct relocate_args *) a;
497
498 INTUSE(_dl_relocate_object) (args->l, args->l->l_scope, args->lazy, 0);
499 }
500
501 static void
502 map_doit (void *a)
503 {
504 struct map_args *args = (struct map_args *) a;
505 args->main_map = INTUSE(_dl_map_object) (NULL, args->str, 0, lt_library, 0, 0);
506 }
507
508 static void
509 version_check_doit (void *a)
510 {
511 struct version_check_args *args = (struct version_check_args *) a;
512 if (_dl_check_all_versions (GL(dl_loaded), 1, args->dotrace) && args->doexit)
513 /* We cannot start the application. Abort now. */
514 _exit (1);
515 }
516
517
518 static inline struct link_map *
519 find_needed (const char *name)
520 {
521 unsigned int n = GL(dl_loaded)->l_searchlist.r_nlist;
522
523 while (n-- > 0)
524 if (_dl_name_match_p (name, GL(dl_loaded)->l_searchlist.r_list[n]))
525 return GL(dl_loaded)->l_searchlist.r_list[n];
526
527 /* Should never happen. */
528 return NULL;
529 }
530
531 static int
532 match_version (const char *string, struct link_map *map)
533 {
534 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
535 ElfW(Verdef) *def;
536
537 #define VERDEFTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
538 if (map->l_info[VERDEFTAG] == NULL)
539 /* The file has no symbol versioning. */
540 return 0;
541
542 def = (ElfW(Verdef) *) ((char *) map->l_addr
543 + map->l_info[VERDEFTAG]->d_un.d_ptr);
544 while (1)
545 {
546 ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
547
548 /* Compare the version strings. */
549 if (strcmp (string, strtab + aux->vda_name) == 0)
550 /* Bingo! */
551 return 1;
552
553 /* If no more definitions we failed to find what we want. */
554 if (def->vd_next == 0)
555 break;
556
557 /* Next definition. */
558 def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
559 }
560
561 return 0;
562 }
563
564 static const char *library_path; /* The library search path. */
565 static const char *preloadlist; /* The list preloaded objects. */
566 static int version_info; /* Nonzero if information about
567 versions has to be printed. */
568
569 static void
570 dl_main (const ElfW(Phdr) *phdr,
571 ElfW(Word) phnum,
572 ElfW(Addr) *user_entry)
573 {
574 const ElfW(Phdr) *ph;
575 enum mode mode;
576 struct link_map **preloads;
577 unsigned int npreloads;
578 size_t file_size;
579 char *file;
580 bool has_interp = false;
581 unsigned int i;
582 bool prelinked = false;
583 bool rtld_is_main = false;
584 #ifndef HP_TIMING_NONAVAIL
585 hp_timing_t start;
586 hp_timing_t stop;
587 hp_timing_t diff;
588 #endif
589 #ifdef USE_TLS
590 void *tcbp;
591 #endif
592
593 /* Process the environment variable which control the behaviour. */
594 process_envvars (&mode);
595
596 /* Set up a flag which tells we are just starting. */
597 INTUSE(_dl_starting_up) = 1;
598
599 if (*user_entry == (ElfW(Addr)) ENTRY_POINT)
600 {
601 /* Ho ho. We are not the program interpreter! We are the program
602 itself! This means someone ran ld.so as a command. Well, that
603 might be convenient to do sometimes. We support it by
604 interpreting the args like this:
605
606 ld.so PROGRAM ARGS...
607
608 The first argument is the name of a file containing an ELF
609 executable we will load and run with the following arguments.
610 To simplify life here, PROGRAM is searched for using the
611 normal rules for shared objects, rather than $PATH or anything
612 like that. We just load it and use its entry point; we don't
613 pay attention to its PT_INTERP command (we are the interpreter
614 ourselves). This is an easy way to test a new ld.so before
615 installing it. */
616 rtld_is_main = true;
617
618 /* Note the place where the dynamic linker actually came from. */
619 GL(dl_rtld_map).l_name = rtld_progname;
620
621 while (_dl_argc > 1)
622 if (! strcmp (INTUSE(_dl_argv)[1], "--list"))
623 {
624 mode = list;
625 GL(dl_lazy) = -1; /* This means do no dependency analysis. */
626
627 ++_dl_skip_args;
628 --_dl_argc;
629 ++INTUSE(_dl_argv);
630 }
631 else if (! strcmp (INTUSE(_dl_argv)[1], "--verify"))
632 {
633 mode = verify;
634
635 ++_dl_skip_args;
636 --_dl_argc;
637 ++INTUSE(_dl_argv);
638 }
639 else if (! strcmp (INTUSE(_dl_argv)[1], "--library-path")
640 && _dl_argc > 2)
641 {
642 library_path = INTUSE(_dl_argv)[2];
643
644 _dl_skip_args += 2;
645 _dl_argc -= 2;
646 INTUSE(_dl_argv) += 2;
647 }
648 else if (! strcmp (INTUSE(_dl_argv)[1], "--inhibit-rpath")
649 && _dl_argc > 2)
650 {
651 GL(dl_inhibit_rpath) = INTUSE(_dl_argv)[2];
652
653 _dl_skip_args += 2;
654 _dl_argc -= 2;
655 INTUSE(_dl_argv) += 2;
656 }
657 else
658 break;
659
660 /* If we have no further argument the program was called incorrectly.
661 Grant the user some education. */
662 if (_dl_argc < 2)
663 _dl_fatal_printf ("\
664 Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
665 You have invoked `ld.so', the helper program for shared library executables.\n\
666 This program usually lives in the file `/lib/ld.so', and special directives\n\
667 in executable files using ELF shared libraries tell the system's program\n\
668 loader to load the helper program from this file. This helper program loads\n\
669 the shared libraries needed by the program executable, prepares the program\n\
670 to run, and runs it. You may invoke this helper program directly from the\n\
671 command line to load and run an ELF executable file; this is like executing\n\
672 that file itself, but always uses this helper program from the file you\n\
673 specified, instead of the helper program file specified in the executable\n\
674 file you run. This is mostly of use for maintainers to test new versions\n\
675 of this helper program; chances are you did not intend to run this program.\n\
676 \n\
677 --list list all dependencies and how they are resolved\n\
678 --verify verify that given object really is a dynamically linked\n\
679 object we can handle\n\
680 --library-path PATH use given PATH instead of content of the environment\n\
681 variable LD_LIBRARY_PATH\n\
682 --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\
683 in LIST\n");
684
685 ++_dl_skip_args;
686 --_dl_argc;
687 ++INTUSE(_dl_argv);
688
689 /* Initialize the data structures for the search paths for shared
690 objects. */
691 _dl_init_paths (library_path);
692
693 if (__builtin_expect (mode, normal) == verify)
694 {
695 const char *objname;
696 const char *err_str = NULL;
697 struct map_args args;
698
699 args.str = rtld_progname;
700 (void) INTUSE(_dl_catch_error) (&objname, &err_str, map_doit, &args);
701 if (__builtin_expect (err_str != NULL, 0))
702 /* We don't free the returned string, the programs stops
703 anyway. */
704 _exit (EXIT_FAILURE);
705 }
706 else
707 {
708 HP_TIMING_NOW (start);
709 INTUSE(_dl_map_object) (NULL, rtld_progname, 0, lt_library, 0, 0);
710 HP_TIMING_NOW (stop);
711
712 HP_TIMING_DIFF (load_time, start, stop);
713 }
714
715 phdr = GL(dl_loaded)->l_phdr;
716 phnum = GL(dl_loaded)->l_phnum;
717 /* We overwrite here a pointer to a malloc()ed string. But since
718 the malloc() implementation used at this point is the dummy
719 implementations which has no real free() function it does not
720 makes sense to free the old string first. */
721 GL(dl_loaded)->l_name = (char *) "";
722 *user_entry = GL(dl_loaded)->l_entry;
723 }
724 else
725 {
726 /* Create a link_map for the executable itself.
727 This will be what dlopen on "" returns. */
728 _dl_new_object ((char *) "", "", lt_executable, NULL);
729 if (GL(dl_loaded) == NULL)
730 _dl_fatal_printf ("cannot allocate memory for link map\n");
731 GL(dl_loaded)->l_phdr = phdr;
732 GL(dl_loaded)->l_phnum = phnum;
733 GL(dl_loaded)->l_entry = *user_entry;
734
735 /* At this point we are in a bit of trouble. We would have to
736 fill in the values for l_dev and l_ino. But in general we
737 do not know where the file is. We also do not handle AT_EXECFD
738 even if it would be passed up.
739
740 We leave the values here defined to 0. This is normally no
741 problem as the program code itself is normally no shared
742 object and therefore cannot be loaded dynamically. Nothing
743 prevent the use of dynamic binaries and in these situations
744 we might get problems. We might not be able to find out
745 whether the object is already loaded. But since there is no
746 easy way out and because the dynamic binary must also not
747 have an SONAME we ignore this program for now. If it becomes
748 a problem we can force people using SONAMEs. */
749
750 /* We delay initializing the path structure until we got the dynamic
751 information for the program. */
752 }
753
754 GL(dl_loaded)->l_map_end = 0;
755 /* Perhaps the executable has no PT_LOAD header entries at all. */
756 GL(dl_loaded)->l_map_start = ~0;
757 /* We opened the file, account for it. */
758 ++GL(dl_loaded)->l_opencount;
759
760 /* Scan the program header table for the dynamic section. */
761 for (ph = phdr; ph < &phdr[phnum]; ++ph)
762 switch (ph->p_type)
763 {
764 case PT_PHDR:
765 /* Find out the load address. */
766 GL(dl_loaded)->l_addr = (ElfW(Addr)) phdr - ph->p_vaddr;
767 break;
768 case PT_DYNAMIC:
769 /* This tells us where to find the dynamic section,
770 which tells us everything we need to do. */
771 GL(dl_loaded)->l_ld = (void *) GL(dl_loaded)->l_addr + ph->p_vaddr;
772 break;
773 case PT_INTERP:
774 /* This "interpreter segment" was used by the program loader to
775 find the program interpreter, which is this program itself, the
776 dynamic linker. We note what name finds us, so that a future
777 dlopen call or DT_NEEDED entry, for something that wants to link
778 against the dynamic linker as a shared library, will know that
779 the shared object is already loaded. */
780 _dl_rtld_libname.name = ((const char *) GL(dl_loaded)->l_addr
781 + ph->p_vaddr);
782 /* _dl_rtld_libname.next = NULL; Already zero. */
783 GL(dl_rtld_map).l_libname = &_dl_rtld_libname;
784
785 /* Ordinarilly, we would get additional names for the loader from
786 our DT_SONAME. This can't happen if we were actually linked as
787 a static executable (detect this case when we have no DYNAMIC).
788 If so, assume the filename component of the interpreter path to
789 be our SONAME, and add it to our name list. */
790 if (GL(dl_rtld_map).l_ld == NULL)
791 {
792 const char *p = NULL;
793 const char *cp = _dl_rtld_libname.name;
794
795 /* Find the filename part of the path. */
796 while (*cp != '\0')
797 if (*cp++ == '/')
798 p = cp;
799
800 if (p != NULL)
801 {
802 _dl_rtld_libname2.name = p;
803 /* _dl_rtld_libname2.next = NULL; Already zero. */
804 _dl_rtld_libname.next = &_dl_rtld_libname2;
805 }
806 }
807
808 has_interp = true;
809 break;
810 case PT_LOAD:
811 {
812 ElfW(Addr) mapstart;
813 ElfW(Addr) allocend;
814
815 /* Remember where the main program starts in memory. */
816 mapstart = (GL(dl_loaded)->l_addr
817 + (ph->p_vaddr & ~(ph->p_align - 1)));
818 if (GL(dl_loaded)->l_map_start > mapstart)
819 GL(dl_loaded)->l_map_start = mapstart;
820
821 /* Also where it ends. */
822 allocend = GL(dl_loaded)->l_addr + ph->p_vaddr + ph->p_memsz;
823 if (GL(dl_loaded)->l_map_end < allocend)
824 GL(dl_loaded)->l_map_end = allocend;
825 }
826 break;
827 #ifdef USE_TLS
828 case PT_TLS:
829 if (ph->p_memsz > 0)
830 {
831 /* Note that in the case the dynamic linker we duplicate work
832 here since we read the PT_TLS entry already in
833 _dl_start_final. But the result is repeatable so do not
834 check for this special but unimportant case. */
835 GL(dl_loaded)->l_tls_blocksize = ph->p_memsz;
836 GL(dl_loaded)->l_tls_align = ph->p_align;
837 GL(dl_loaded)->l_tls_initimage_size = ph->p_filesz;
838 GL(dl_loaded)->l_tls_initimage = (void *) ph->p_vaddr;
839
840 /* This image gets the ID one. */
841 GL(dl_tls_max_dtv_idx) = GL(dl_loaded)->l_tls_modid = 1;
842 }
843 break;
844 #endif
845 }
846 #ifdef USE_TLS
847 /* Adjust the address of the TLS initialization image in case
848 the executable is actually an ET_DYN object. */
849 if (GL(dl_loaded)->l_tls_initimage != NULL)
850 GL(dl_loaded)->l_tls_initimage
851 = (char *) GL(dl_loaded)->l_tls_initimage + GL(dl_loaded)->l_addr;
852 #endif
853 if (! GL(dl_loaded)->l_map_end)
854 GL(dl_loaded)->l_map_end = ~0;
855 if (! GL(dl_rtld_map).l_libname && GL(dl_rtld_map).l_name)
856 {
857 /* We were invoked directly, so the program might not have a
858 PT_INTERP. */
859 _dl_rtld_libname.name = GL(dl_rtld_map).l_name;
860 /* _dl_rtld_libname.next = NULL; Already zero. */
861 GL(dl_rtld_map).l_libname = &_dl_rtld_libname;
862 }
863 else
864 assert (GL(dl_rtld_map).l_libname); /* How else did we get here? */
865
866 if (! rtld_is_main)
867 {
868 /* Extract the contents of the dynamic section for easy access. */
869 elf_get_dynamic_info (GL(dl_loaded));
870 if (GL(dl_loaded)->l_info[DT_HASH])
871 /* Set up our cache of pointers into the hash table. */
872 _dl_setup_hash (GL(dl_loaded));
873 }
874
875 if (__builtin_expect (mode, normal) == verify)
876 {
877 /* We were called just to verify that this is a dynamic
878 executable using us as the program interpreter. Exit with an
879 error if we were not able to load the binary or no interpreter
880 is specified (i.e., this is no dynamically linked binary. */
881 if (GL(dl_loaded)->l_ld == NULL)
882 _exit (1);
883
884 /* We allow here some platform specific code. */
885 #ifdef DISTINGUISH_LIB_VERSIONS
886 DISTINGUISH_LIB_VERSIONS;
887 #endif
888 _exit (has_interp ? 0 : 2);
889 }
890
891 if (! rtld_is_main)
892 /* Initialize the data structures for the search paths for shared
893 objects. */
894 _dl_init_paths (library_path);
895
896 /* Put the link_map for ourselves on the chain so it can be found by
897 name. Note that at this point the global chain of link maps contains
898 exactly one element, which is pointed to by dl_loaded. */
899 if (! GL(dl_rtld_map).l_name)
900 /* If not invoked directly, the dynamic linker shared object file was
901 found by the PT_INTERP name. */
902 GL(dl_rtld_map).l_name = (char *) GL(dl_rtld_map).l_libname->name;
903 GL(dl_rtld_map).l_type = lt_library;
904 GL(dl_loaded)->l_next = &GL(dl_rtld_map);
905 GL(dl_rtld_map).l_prev = GL(dl_loaded);
906 ++GL(dl_nloaded);
907
908 /* We have two ways to specify objects to preload: via environment
909 variable and via the file /etc/ld.so.preload. The latter can also
910 be used when security is enabled. */
911 preloads = NULL;
912 npreloads = 0;
913
914 if (__builtin_expect (preloadlist != NULL, 0))
915 {
916 /* The LD_PRELOAD environment variable gives list of libraries
917 separated by white space or colons that are loaded before the
918 executable's dependencies and prepended to the global scope
919 list. If the binary is running setuid all elements
920 containing a '/' are ignored since it is insecure. */
921 char *list = strdupa (preloadlist);
922 char *p;
923
924 HP_TIMING_NOW (start);
925
926 /* Prevent optimizing strsep. Speed is not important here. */
927 while ((p = (strsep) (&list, " :")) != NULL)
928 if (p[0] != '\0'
929 && (__builtin_expect (! INTUSE(__libc_enable_secure), 1)
930 || strchr (p, '/') == NULL))
931 {
932 struct link_map *new_map = INTUSE(_dl_map_object) (GL(dl_loaded),
933 p, 1,
934 lt_library,
935 0, 0);
936 if (++new_map->l_opencount == 1)
937 /* It is no duplicate. */
938 ++npreloads;
939 }
940
941 HP_TIMING_NOW (stop);
942 HP_TIMING_DIFF (diff, start, stop);
943 HP_TIMING_ACCUM_NT (load_time, diff);
944 }
945
946 /* Read the contents of the file. */
947 file = _dl_sysdep_read_whole_file ("/etc/ld.so.preload", &file_size,
948 PROT_READ | PROT_WRITE);
949 if (__builtin_expect (file != MAP_FAILED, 0))
950 {
951 /* Parse the file. It contains names of libraries to be loaded,
952 separated by white spaces or `:'. It may also contain
953 comments introduced by `#'. */
954 char *problem;
955 char *runp;
956 size_t rest;
957
958 /* Eliminate comments. */
959 runp = file;
960 rest = file_size;
961 while (rest > 0)
962 {
963 char *comment = memchr (runp, '#', rest);
964 if (comment == NULL)
965 break;
966
967 rest -= comment - runp;
968 do
969 *comment = ' ';
970 while (--rest > 0 && *++comment != '\n');
971 }
972
973 /* We have one problematic case: if we have a name at the end of
974 the file without a trailing terminating characters, we cannot
975 place the \0. Handle the case separately. */
976 if (file[file_size - 1] != ' ' && file[file_size - 1] != '\t'
977 && file[file_size - 1] != '\n' && file[file_size - 1] != ':')
978 {
979 problem = &file[file_size];
980 while (problem > file && problem[-1] != ' ' && problem[-1] != '\t'
981 && problem[-1] != '\n' && problem[-1] != ':')
982 --problem;
983
984 if (problem > file)
985 problem[-1] = '\0';
986 }
987 else
988 {
989 problem = NULL;
990 file[file_size - 1] = '\0';
991 }
992
993 HP_TIMING_NOW (start);
994
995 if (file != problem)
996 {
997 char *p;
998 runp = file;
999 while ((p = strsep (&runp, ": \t\n")) != NULL)
1000 if (p[0] != '\0')
1001 {
1002 struct link_map *new_map = INTUSE(_dl_map_object) (GL(dl_loaded),
1003 p, 1,
1004 lt_library,
1005 0, 0);
1006 if (++new_map->l_opencount == 1)
1007 /* It is no duplicate. */
1008 ++npreloads;
1009 }
1010 }
1011
1012 if (problem != NULL)
1013 {
1014 char *p = strndupa (problem, file_size - (problem - file));
1015 struct link_map *new_map = INTUSE(_dl_map_object) (GL(dl_loaded), p,
1016 1, lt_library,
1017 0, 0);
1018 if (++new_map->l_opencount == 1)
1019 /* It is no duplicate. */
1020 ++npreloads;
1021 }
1022
1023 HP_TIMING_NOW (stop);
1024 HP_TIMING_DIFF (diff, start, stop);
1025 HP_TIMING_ACCUM_NT (load_time, diff);
1026
1027 /* We don't need the file anymore. */
1028 __munmap (file, file_size);
1029 }
1030
1031 if (__builtin_expect (npreloads, 0) != 0)
1032 {
1033 /* Set up PRELOADS with a vector of the preloaded libraries. */
1034 struct link_map *l;
1035 preloads = __alloca (npreloads * sizeof preloads[0]);
1036 l = GL(dl_rtld_map).l_next; /* End of the chain before preloads. */
1037 i = 0;
1038 do
1039 {
1040 preloads[i++] = l;
1041 l = l->l_next;
1042 } while (l);
1043 assert (i == npreloads);
1044 }
1045
1046 /* Load all the libraries specified by DT_NEEDED entries. If LD_PRELOAD
1047 specified some libraries to load, these are inserted before the actual
1048 dependencies in the executable's searchlist for symbol resolution. */
1049 HP_TIMING_NOW (start);
1050 INTUSE(_dl_map_object_deps) (GL(dl_loaded), preloads, npreloads,
1051 mode == trace, 0);
1052 HP_TIMING_NOW (stop);
1053 HP_TIMING_DIFF (diff, start, stop);
1054 HP_TIMING_ACCUM_NT (load_time, diff);
1055
1056 /* Mark all objects as being in the global scope and set the open
1057 counter. */
1058 for (i = GL(dl_loaded)->l_searchlist.r_nlist; i > 0; )
1059 {
1060 --i;
1061 GL(dl_loaded)->l_searchlist.r_list[i]->l_global = 1;
1062 ++GL(dl_loaded)->l_searchlist.r_list[i]->l_opencount;
1063 }
1064
1065 #ifndef MAP_ANON
1066 /* We are done mapping things, so close the zero-fill descriptor. */
1067 __close (_dl_zerofd);
1068 _dl_zerofd = -1;
1069 #endif
1070
1071 /* Remove _dl_rtld_map from the chain. */
1072 GL(dl_rtld_map).l_prev->l_next = GL(dl_rtld_map).l_next;
1073 if (GL(dl_rtld_map).l_next)
1074 GL(dl_rtld_map).l_next->l_prev = GL(dl_rtld_map).l_prev;
1075
1076 if (__builtin_expect (GL(dl_rtld_map).l_opencount > 1, 1))
1077 {
1078 /* Some DT_NEEDED entry referred to the interpreter object itself, so
1079 put it back in the list of visible objects. We insert it into the
1080 chain in symbol search order because gdb uses the chain's order as
1081 its symbol search order. */
1082 i = 1;
1083 while (GL(dl_loaded)->l_searchlist.r_list[i] != &GL(dl_rtld_map))
1084 ++i;
1085 GL(dl_rtld_map).l_prev = GL(dl_loaded)->l_searchlist.r_list[i - 1];
1086 if (__builtin_expect (mode, normal) == normal)
1087 GL(dl_rtld_map).l_next = (i + 1 < GL(dl_loaded)->l_searchlist.r_nlist
1088 ? GL(dl_loaded)->l_searchlist.r_list[i + 1]
1089 : NULL);
1090 else
1091 /* In trace mode there might be an invisible object (which we
1092 could not find) after the previous one in the search list.
1093 In this case it doesn't matter much where we put the
1094 interpreter object, so we just initialize the list pointer so
1095 that the assertion below holds. */
1096 GL(dl_rtld_map).l_next = GL(dl_rtld_map).l_prev->l_next;
1097
1098 assert (GL(dl_rtld_map).l_prev->l_next == GL(dl_rtld_map).l_next);
1099 GL(dl_rtld_map).l_prev->l_next = &GL(dl_rtld_map);
1100 if (GL(dl_rtld_map).l_next != NULL)
1101 {
1102 assert (GL(dl_rtld_map).l_next->l_prev == GL(dl_rtld_map).l_prev);
1103 GL(dl_rtld_map).l_next->l_prev = &GL(dl_rtld_map);
1104 }
1105 }
1106
1107 /* Now let us see whether all libraries are available in the
1108 versions we need. */
1109 {
1110 struct version_check_args args;
1111 args.doexit = mode == normal;
1112 args.dotrace = mode == trace;
1113 _dl_receive_error (print_missing_version, version_check_doit, &args);
1114 }
1115
1116 #ifdef USE_TLS
1117 /* Now it is time to determine the layout of the static TLS block
1118 and allocate it for the initial thread. Note that we always
1119 allocate the static block, we never defer it even if no
1120 DF_STATIC_TLS bit is set. The reason is that we know glibc will
1121 use the static model. First add the dynamic linker to the list
1122 if it also uses TLS. */
1123 if (GL(dl_rtld_map).l_tls_blocksize != 0)
1124 /* Assign a module ID. */
1125 GL(dl_rtld_map).l_tls_modid = _dl_next_tls_modid ();
1126
1127 # ifndef SHARED
1128 /* If dynamic loading of modules with TLS is impossible we do not
1129 have to initialize any of the TLS functionality unless any of the
1130 initial modules uses TLS. */
1131 if (GL(dl_tls_max_dtv_idx) > 0)
1132 # endif
1133 {
1134 struct link_map *l;
1135 size_t nelem;
1136 struct dtv_slotinfo *slotinfo;
1137
1138 /* Number of elements in the static TLS block. */
1139 GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx);
1140
1141 /* Allocate the array which contains the information about the
1142 dtv slots. We allocate a few entries more than needed to
1143 avoid the need for reallocation. */
1144 nelem = GL(dl_tls_max_dtv_idx) + 1 + TLS_SLOTINFO_SURPLUS;
1145
1146 /* Allocate. */
1147 GL(dl_tls_dtv_slotinfo_list) = (struct dtv_slotinfo_list *)
1148 malloc (sizeof (struct dtv_slotinfo_list)
1149 + nelem * sizeof (struct dtv_slotinfo));
1150 /* No need to check the return value. If memory allocation failed
1151 the program would have been terminated. */
1152
1153 slotinfo = memset (GL(dl_tls_dtv_slotinfo_list)->slotinfo, '\0',
1154 nelem * sizeof (struct dtv_slotinfo));
1155 GL(dl_tls_dtv_slotinfo_list)->len = nelem;
1156 GL(dl_tls_dtv_slotinfo_list)->next = NULL;
1157
1158 /* Fill in the information from the loaded modules. */
1159 for (l = GL(dl_loaded), i = 0; l != NULL; l = l->l_next)
1160 if (l->l_tls_blocksize != 0)
1161 /* This is a module with TLS data. Store the map reference.
1162 The generation counter is zero. */
1163 slotinfo[++i].map = l;
1164 assert (i == GL(dl_tls_max_dtv_idx));
1165
1166 /* Compute the TLS offsets for the various blocks. We call this
1167 function even if none of the modules available at startup time
1168 uses TLS to initialize some variables. */
1169 _dl_determine_tlsoffset ();
1170
1171 /* Construct the static TLS block and the dtv for the initial
1172 thread. For some platforms this will include allocating memory
1173 for the thread descriptor. The memory for the TLS block will
1174 never be freed. It should be allocated accordingly. The dtv
1175 array can be changed if dynamic loading requires it. */
1176 tcbp = _dl_allocate_tls_storage ();
1177 if (tcbp == NULL)
1178 _dl_fatal_printf ("\
1179 cannot allocate TLS data structures for initial thread");
1180
1181 /* Store for detection of the special case by __tls_get_addr
1182 so it knows not to pass this dtv to the normal realloc. */
1183 GL(dl_initial_dtv) = GET_DTV (tcbp);
1184 }
1185 #endif
1186
1187 if (__builtin_expect (mode, normal) != normal)
1188 {
1189 /* We were run just to list the shared libraries. It is
1190 important that we do this before real relocation, because the
1191 functions we call below for output may no longer work properly
1192 after relocation. */
1193 if (! GL(dl_loaded)->l_info[DT_NEEDED])
1194 _dl_printf ("\tstatically linked\n");
1195 else
1196 {
1197 struct link_map *l;
1198
1199 if (GL(dl_debug_mask) & DL_DEBUG_PRELINK)
1200 {
1201 struct r_scope_elem *scope = &GL(dl_loaded)->l_searchlist;
1202
1203 for (i = 0; i < scope->r_nlist; i++)
1204 {
1205 l = scope->r_list [i];
1206 if (l->l_faked)
1207 {
1208 _dl_printf ("\t%s => not found\n", l->l_libname->name);
1209 continue;
1210 }
1211 if (_dl_name_match_p (GL(dl_trace_prelink), l))
1212 GL(dl_trace_prelink_map) = l;
1213 _dl_printf ("\t%s => %s (0x%0*Zx, 0x%0*Zx)",
1214 l->l_libname->name[0] ? l->l_libname->name
1215 : rtld_progname ?: "<main program>",
1216 l->l_name[0] ? l->l_name
1217 : rtld_progname ?: "<main program>",
1218 (int) sizeof l->l_map_start * 2,
1219 l->l_map_start,
1220 (int) sizeof l->l_addr * 2,
1221 l->l_addr);
1222 #ifdef USE_TLS
1223 if (l->l_tls_modid)
1224 _dl_printf (" TLS(0x%Zx, 0x%0*Zx)\n", l->l_tls_modid,
1225 (int) sizeof l->l_tls_offset * 2,
1226 l->l_tls_offset);
1227 else
1228 #endif
1229 _dl_printf ("\n");
1230 }
1231 }
1232 else
1233 {
1234 for (l = GL(dl_loaded)->l_next; l; l = l->l_next)
1235 if (l->l_faked)
1236 /* The library was not found. */
1237 _dl_printf ("\t%s => not found\n", l->l_libname->name);
1238 else
1239 _dl_printf ("\t%s => %s (0x%0*Zx)\n", l->l_libname->name,
1240 l->l_name, (int) sizeof l->l_map_start * 2,
1241 l->l_map_start);
1242 }
1243 }
1244
1245 if (__builtin_expect (mode, trace) != trace)
1246 for (i = 1; i < (unsigned int) _dl_argc; ++i)
1247 {
1248 const ElfW(Sym) *ref = NULL;
1249 ElfW(Addr) loadbase;
1250 lookup_t result;
1251
1252 result = INTUSE(_dl_lookup_symbol) (INTUSE(_dl_argv)[i],
1253 GL(dl_loaded),
1254 &ref, GL(dl_loaded)->l_scope,
1255 ELF_RTYPE_CLASS_PLT, 1);
1256
1257 loadbase = LOOKUP_VALUE_ADDRESS (result);
1258
1259 _dl_printf ("%s found at 0x%0*Zd in object at 0x%0*Zd\n",
1260 INTUSE(_dl_argv)[i],
1261 (int) sizeof ref->st_value * 2, ref->st_value,
1262 (int) sizeof loadbase * 2, loadbase);
1263 }
1264 else
1265 {
1266 /* If LD_WARN is set warn about undefined symbols. */
1267 if (GL(dl_lazy) >= 0 && GL(dl_verbose))
1268 {
1269 /* We have to do symbol dependency testing. */
1270 struct relocate_args args;
1271 struct link_map *l;
1272
1273 args.lazy = GL(dl_lazy);
1274
1275 l = GL(dl_loaded);
1276 while (l->l_next)
1277 l = l->l_next;
1278 do
1279 {
1280 if (l != &GL(dl_rtld_map) && ! l->l_faked)
1281 {
1282 args.l = l;
1283 _dl_receive_error (print_unresolved, relocate_doit,
1284 &args);
1285 }
1286 l = l->l_prev;
1287 } while (l);
1288
1289 if ((GL(dl_debug_mask) & DL_DEBUG_PRELINK)
1290 && GL(dl_rtld_map).l_opencount > 1)
1291 INTUSE(_dl_relocate_object) (&GL(dl_rtld_map),
1292 GL(dl_loaded)->l_scope, 0, 0);
1293 }
1294
1295 #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
1296 if (version_info)
1297 {
1298 /* Print more information. This means here, print information
1299 about the versions needed. */
1300 int first = 1;
1301 struct link_map *map = GL(dl_loaded);
1302
1303 for (map = GL(dl_loaded); map != NULL; map = map->l_next)
1304 {
1305 const char *strtab;
1306 ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG];
1307 ElfW(Verneed) *ent;
1308
1309 if (dyn == NULL)
1310 continue;
1311
1312 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
1313 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
1314
1315 if (first)
1316 {
1317 _dl_printf ("\n\tVersion information:\n");
1318 first = 0;
1319 }
1320
1321 _dl_printf ("\t%s:\n",
1322 map->l_name[0] ? map->l_name : rtld_progname);
1323
1324 while (1)
1325 {
1326 ElfW(Vernaux) *aux;
1327 struct link_map *needed;
1328
1329 needed = find_needed (strtab + ent->vn_file);
1330 aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
1331
1332 while (1)
1333 {
1334 const char *fname = NULL;
1335
1336 if (needed != NULL
1337 && match_version (strtab + aux->vna_name,
1338 needed))
1339 fname = needed->l_name;
1340
1341 _dl_printf ("\t\t%s (%s) %s=> %s\n",
1342 strtab + ent->vn_file,
1343 strtab + aux->vna_name,
1344 aux->vna_flags & VER_FLG_WEAK
1345 ? "[WEAK] " : "",
1346 fname ?: "not found");
1347
1348 if (aux->vna_next == 0)
1349 /* No more symbols. */
1350 break;
1351
1352 /* Next symbol. */
1353 aux = (ElfW(Vernaux) *) ((char *) aux
1354 + aux->vna_next);
1355 }
1356
1357 if (ent->vn_next == 0)
1358 /* No more dependencies. */
1359 break;
1360
1361 /* Next dependency. */
1362 ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
1363 }
1364 }
1365 }
1366 }
1367
1368 _exit (0);
1369 }
1370
1371 if (GL(dl_loaded)->l_info [ADDRIDX (DT_GNU_LIBLIST)]
1372 && ! __builtin_expect (GL(dl_profile) != NULL, 0))
1373 {
1374 ElfW(Lib) *liblist, *liblistend;
1375 struct link_map **r_list, **r_listend, *l;
1376 const char *strtab = (const void *) D_PTR (GL(dl_loaded),
1377 l_info[DT_STRTAB]);
1378
1379 assert (GL(dl_loaded)->l_info [VALIDX (DT_GNU_LIBLISTSZ)] != NULL);
1380 liblist = (ElfW(Lib) *)
1381 GL(dl_loaded)->l_info [ADDRIDX (DT_GNU_LIBLIST)]->d_un.d_ptr;
1382 liblistend = (ElfW(Lib) *)
1383 ((char *) liblist
1384 + GL(dl_loaded)->l_info [VALIDX (DT_GNU_LIBLISTSZ)]->d_un.d_val);
1385 r_list = GL(dl_loaded)->l_searchlist.r_list;
1386 r_listend = r_list + GL(dl_loaded)->l_searchlist.r_nlist;
1387
1388 for (; r_list < r_listend && liblist < liblistend; r_list++)
1389 {
1390 l = *r_list;
1391
1392 if (l == GL(dl_loaded))
1393 continue;
1394
1395 /* If the library is not mapped where it should, fail. */
1396 if (l->l_addr)
1397 break;
1398
1399 /* Next, check if checksum matches. */
1400 if (l->l_info [VALIDX(DT_CHECKSUM)] == NULL
1401 || l->l_info [VALIDX(DT_CHECKSUM)]->d_un.d_val
1402 != liblist->l_checksum)
1403 break;
1404
1405 if (l->l_info [VALIDX(DT_GNU_PRELINKED)] == NULL
1406 || l->l_info [VALIDX(DT_GNU_PRELINKED)]->d_un.d_val
1407 != liblist->l_time_stamp)
1408 break;
1409
1410 if (! _dl_name_match_p (strtab + liblist->l_name, l))
1411 break;
1412
1413 ++liblist;
1414 }
1415
1416
1417 if (r_list == r_listend && liblist == liblistend)
1418 prelinked = true;
1419
1420 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_LIBS, 0))
1421 _dl_printf ("\nprelink checking: %s\n", prelinked ? "ok" : "failed");
1422 }
1423
1424 if (prelinked)
1425 {
1426 struct link_map *l;
1427
1428 if (GL(dl_loaded)->l_info [ADDRIDX (DT_GNU_CONFLICT)] != NULL)
1429 {
1430 ElfW(Rela) *conflict, *conflictend;
1431 #ifndef HP_TIMING_NONAVAIL
1432 hp_timing_t start;
1433 hp_timing_t stop;
1434 #endif
1435
1436 HP_TIMING_NOW (start);
1437 assert (GL(dl_loaded)->l_info [VALIDX (DT_GNU_CONFLICTSZ)] != NULL);
1438 conflict = (ElfW(Rela) *)
1439 GL(dl_loaded)->l_info [ADDRIDX (DT_GNU_CONFLICT)]->d_un.d_ptr;
1440 conflictend = (ElfW(Rela) *)
1441 ((char *) conflict
1442 + GL(dl_loaded)->l_info [VALIDX (DT_GNU_CONFLICTSZ)]->d_un.d_val);
1443 _dl_resolve_conflicts (GL(dl_loaded), conflict, conflictend);
1444 HP_TIMING_NOW (stop);
1445 HP_TIMING_DIFF (relocate_time, start, stop);
1446 }
1447
1448
1449 /* Mark all the objects so we know they have been already relocated. */
1450 for (l = GL(dl_loaded); l != NULL; l = l->l_next)
1451 l->l_relocated = 1;
1452
1453 _dl_sysdep_start_cleanup ();
1454 }
1455 else
1456 {
1457 /* Now we have all the objects loaded. Relocate them all except for
1458 the dynamic linker itself. We do this in reverse order so that copy
1459 relocs of earlier objects overwrite the data written by later
1460 objects. We do not re-relocate the dynamic linker itself in this
1461 loop because that could result in the GOT entries for functions we
1462 call being changed, and that would break us. It is safe to relocate
1463 the dynamic linker out of order because it has no copy relocs (we
1464 know that because it is self-contained). */
1465
1466 struct link_map *l;
1467 int consider_profiling = GL(dl_profile) != NULL;
1468 #ifndef HP_TIMING_NONAVAIL
1469 hp_timing_t start;
1470 hp_timing_t stop;
1471 hp_timing_t add;
1472 #endif
1473
1474 /* If we are profiling we also must do lazy reloaction. */
1475 GL(dl_lazy) |= consider_profiling;
1476
1477 l = GL(dl_loaded);
1478 while (l->l_next)
1479 l = l->l_next;
1480
1481 HP_TIMING_NOW (start);
1482 do
1483 {
1484 /* While we are at it, help the memory handling a bit. We have to
1485 mark some data structures as allocated with the fake malloc()
1486 implementation in ld.so. */
1487 struct libname_list *lnp = l->l_libname->next;
1488
1489 while (__builtin_expect (lnp != NULL, 0))
1490 {
1491 lnp->dont_free = 1;
1492 lnp = lnp->next;
1493 }
1494
1495 if (l != &GL(dl_rtld_map))
1496 INTUSE(_dl_relocate_object) (l, l->l_scope, GL(dl_lazy),
1497 consider_profiling);
1498
1499 l = l->l_prev;
1500 }
1501 while (l);
1502 HP_TIMING_NOW (stop);
1503
1504 HP_TIMING_DIFF (relocate_time, start, stop);
1505
1506 /* Do any necessary cleanups for the startup OS interface code.
1507 We do these now so that no calls are made after rtld re-relocation
1508 which might be resolved to different functions than we expect.
1509 We cannot do this before relocating the other objects because
1510 _dl_relocate_object might need to call `mprotect' for DT_TEXTREL. */
1511 _dl_sysdep_start_cleanup ();
1512
1513 /* Now enable profiling if needed. Like the previous call,
1514 this has to go here because the calls it makes should use the
1515 rtld versions of the functions (particularly calloc()), but it
1516 needs to have _dl_profile_map set up by the relocator. */
1517 if (__builtin_expect (GL(dl_profile_map) != NULL, 0))
1518 /* We must prepare the profiling. */
1519 INTUSE(_dl_start_profile) (GL(dl_profile_map), GL(dl_profile_output));
1520
1521 if (GL(dl_rtld_map).l_opencount > 1)
1522 {
1523 /* There was an explicit ref to the dynamic linker as a shared lib.
1524 Re-relocate ourselves with user-controlled symbol definitions. */
1525 HP_TIMING_NOW (start);
1526 INTUSE(_dl_relocate_object) (&GL(dl_rtld_map), GL(dl_loaded)->l_scope,
1527 0, 0);
1528 HP_TIMING_NOW (stop);
1529 HP_TIMING_DIFF (add, start, stop);
1530 HP_TIMING_ACCUM_NT (relocate_time, add);
1531 }
1532 }
1533
1534 /* Now set up the variable which helps the assembler startup code. */
1535 GL(dl_main_searchlist) = &GL(dl_loaded)->l_searchlist;
1536 GL(dl_global_scope)[0] = &GL(dl_loaded)->l_searchlist;
1537
1538 /* Save the information about the original global scope list since
1539 we need it in the memory handling later. */
1540 GL(dl_initial_searchlist) = *GL(dl_main_searchlist);
1541
1542 #ifdef USE_TLS
1543 # ifndef SHARED
1544 if (GL(dl_tls_max_dtv_idx) > 0)
1545 # endif
1546 {
1547 /* Now that we have completed relocation, the initializer data
1548 for the TLS blocks has its final values and we can copy them
1549 into the main thread's TLS area, which we allocated above. */
1550 _dl_allocate_tls_init (tcbp);
1551
1552 /* And finally install it for the main thread. */
1553 # ifndef HAVE___THREAD
1554 TLS_INIT_TP (tcbp, GL(dl_rtld_map).l_tls_tp_initialized);
1555 # else
1556 /* If the compiler supports the __thread keyword we know that
1557 at least ld.so itself uses TLS and therefore the thread
1558 pointer was initialized earlier. */
1559 assert (GL(dl_rtld_map).l_tls_tp_initialized != 0);
1560 TLS_INIT_TP (tcbp, 1);
1561 # endif
1562 }
1563 #endif
1564
1565 {
1566 /* Initialize _r_debug. */
1567 struct r_debug *r = _dl_debug_initialize (GL(dl_rtld_map).l_addr);
1568 struct link_map *l;
1569
1570 l = GL(dl_loaded);
1571
1572 #ifdef ELF_MACHINE_DEBUG_SETUP
1573
1574 /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way. */
1575
1576 ELF_MACHINE_DEBUG_SETUP (l, r);
1577 ELF_MACHINE_DEBUG_SETUP (&GL(dl_rtld_map), r);
1578
1579 #else
1580
1581 if (l->l_info[DT_DEBUG] != NULL)
1582 /* There is a DT_DEBUG entry in the dynamic section. Fill it in
1583 with the run-time address of the r_debug structure */
1584 l->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1585
1586 /* Fill in the pointer in the dynamic linker's own dynamic section, in
1587 case you run gdb on the dynamic linker directly. */
1588 if (GL(dl_rtld_map).l_info[DT_DEBUG] != NULL)
1589 GL(dl_rtld_map).l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1590
1591 #endif
1592
1593 /* Notify the debugger that all objects are now mapped in. */
1594 r->r_state = RT_ADD;
1595 INTUSE(_dl_debug_state) ();
1596 }
1597
1598 #ifndef MAP_COPY
1599 /* We must munmap() the cache file. */
1600 INTUSE(_dl_unload_cache) ();
1601 #endif
1602
1603 /* Once we return, _dl_sysdep_start will invoke
1604 the DT_INIT functions and then *USER_ENTRY. */
1605 }
1606 \f
1607 /* This is a little helper function for resolving symbols while
1608 tracing the binary. */
1609 static void
1610 print_unresolved (int errcode __attribute__ ((unused)), const char *objname,
1611 const char *errstring)
1612 {
1613 if (objname[0] == '\0')
1614 objname = rtld_progname ?: "<main program>";
1615 _dl_error_printf ("%s (%s)\n", errstring, objname);
1616 }
1617 \f
1618 /* This is a little helper function for resolving symbols while
1619 tracing the binary. */
1620 static void
1621 print_missing_version (int errcode __attribute__ ((unused)),
1622 const char *objname, const char *errstring)
1623 {
1624 _dl_error_printf ("%s: %s: %s\n", rtld_progname ?: "<program name unknown>",
1625 objname, errstring);
1626 }
1627 \f
1628 /* Nonzero if any of the debugging options is enabled. */
1629 static int any_debug;
1630
1631 /* Process the string given as the parameter which explains which debugging
1632 options are enabled. */
1633 static void
1634 process_dl_debug (const char *dl_debug)
1635 {
1636 /* When adding new entries make sure that the maximal length of a name
1637 is correctly handled in the LD_DEBUG_HELP code below. */
1638 static const struct
1639 {
1640 unsigned char len;
1641 const char name[10];
1642 const char helptext[41];
1643 unsigned short int mask;
1644 } debopts[] =
1645 {
1646 #define LEN_AND_STR(str) sizeof (str) - 1, str
1647 { LEN_AND_STR ("libs"), "display library search paths",
1648 DL_DEBUG_LIBS | DL_DEBUG_IMPCALLS },
1649 { LEN_AND_STR ("reloc"), "display relocation processing",
1650 DL_DEBUG_RELOC | DL_DEBUG_IMPCALLS },
1651 { LEN_AND_STR ("files"), "display progress for input file",
1652 DL_DEBUG_FILES | DL_DEBUG_IMPCALLS },
1653 { LEN_AND_STR ("symbols"), "display symbol table processing",
1654 DL_DEBUG_SYMBOLS | DL_DEBUG_IMPCALLS },
1655 { LEN_AND_STR ("bindings"), "display information about symbol binding",
1656 DL_DEBUG_BINDINGS | DL_DEBUG_IMPCALLS },
1657 { LEN_AND_STR ("versions"), "display version dependencies",
1658 DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS },
1659 { LEN_AND_STR ("all"), "all previous options combined",
1660 DL_DEBUG_LIBS | DL_DEBUG_RELOC | DL_DEBUG_FILES | DL_DEBUG_SYMBOLS
1661 | DL_DEBUG_BINDINGS | DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS },
1662 { LEN_AND_STR ("statistics"), "display relocation statistics",
1663 DL_DEBUG_STATISTICS },
1664 { LEN_AND_STR ("help"), "display this help message and exit",
1665 DL_DEBUG_HELP },
1666 };
1667 #define ndebopts (sizeof (debopts) / sizeof (debopts[0]))
1668
1669 /* Skip separating white spaces and commas. */
1670 while (*dl_debug != '\0')
1671 {
1672 if (*dl_debug != ' ' && *dl_debug != ',' && *dl_debug != ':')
1673 {
1674 size_t cnt;
1675 size_t len = 1;
1676
1677 while (dl_debug[len] != '\0' && dl_debug[len] != ' '
1678 && dl_debug[len] != ',' && dl_debug[len] != ':')
1679 ++len;
1680
1681 for (cnt = 0; cnt < ndebopts; ++cnt)
1682 if (debopts[cnt].len == len
1683 && memcmp (dl_debug, debopts[cnt].name, len) == 0)
1684 {
1685 GL(dl_debug_mask) |= debopts[cnt].mask;
1686 any_debug = 1;
1687 break;
1688 }
1689
1690 if (cnt == ndebopts)
1691 {
1692 /* Display a warning and skip everything until next
1693 separator. */
1694 char *copy = strndupa (dl_debug, len);
1695 _dl_error_printf ("\
1696 warning: debug option `%s' unknown; try LD_DEBUG=help\n", copy);
1697 }
1698
1699 dl_debug += len;
1700 continue;
1701 }
1702
1703 ++dl_debug;
1704 }
1705
1706 if (GL(dl_debug_mask) & DL_DEBUG_HELP)
1707 {
1708 size_t cnt;
1709
1710 _dl_printf ("\
1711 Valid options for the LD_DEBUG environment variable are:\n\n");
1712
1713 for (cnt = 0; cnt < ndebopts; ++cnt)
1714 _dl_printf (" %.*s%s%s\n", debopts[cnt].len, debopts[cnt].name,
1715 " " + debopts[cnt].len - 3,
1716 debopts[cnt].helptext);
1717
1718 _dl_printf ("\n\
1719 To direct the debugging output into a file instead of standard output\n\
1720 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n");
1721 _exit (0);
1722 }
1723 }
1724 \f
1725 /* Process all environments variables the dynamic linker must recognize.
1726 Since all of them start with `LD_' we are a bit smarter while finding
1727 all the entries. */
1728 extern char **_environ attribute_hidden;
1729
1730
1731 static void
1732 process_envvars (enum mode *modep)
1733 {
1734 char **runp = _environ;
1735 char *envline;
1736 enum mode mode = normal;
1737 char *debug_output = NULL;
1738
1739 /* This is the default place for profiling data file. */
1740 GL(dl_profile_output)
1741 = &"/var/tmp\0/var/profile"[INTUSE(__libc_enable_secure) ? 9 : 0];
1742
1743 while ((envline = _dl_next_ld_env_entry (&runp)) != NULL)
1744 {
1745 size_t len = 0;
1746
1747 while (envline[len] != '\0' && envline[len] != '=')
1748 ++len;
1749
1750 if (envline[len] != '=')
1751 /* This is a "LD_" variable at the end of the string without
1752 a '=' character. Ignore it since otherwise we will access
1753 invalid memory below. */
1754 continue;
1755
1756 switch (len)
1757 {
1758 case 4:
1759 /* Warning level, verbose or not. */
1760 if (memcmp (envline, "WARN", 4) == 0)
1761 GL(dl_verbose) = envline[5] != '\0';
1762 break;
1763
1764 case 5:
1765 /* Debugging of the dynamic linker? */
1766 if (memcmp (envline, "DEBUG", 5) == 0)
1767 process_dl_debug (&envline[6]);
1768 break;
1769
1770 case 7:
1771 /* Print information about versions. */
1772 if (memcmp (envline, "VERBOSE", 7) == 0)
1773 {
1774 version_info = envline[8] != '\0';
1775 break;
1776 }
1777
1778 /* List of objects to be preloaded. */
1779 if (memcmp (envline, "PRELOAD", 7) == 0)
1780 {
1781 preloadlist = &envline[8];
1782 break;
1783 }
1784
1785 /* Which shared object shall be profiled. */
1786 if (memcmp (envline, "PROFILE", 7) == 0 && envline[8] != '\0')
1787 GL(dl_profile) = &envline[8];
1788 break;
1789
1790 case 8:
1791 /* Do we bind early? */
1792 if (memcmp (envline, "BIND_NOW", 8) == 0)
1793 {
1794 GL(dl_lazy) = envline[9] == '\0';
1795 break;
1796 }
1797 if (memcmp (envline, "BIND_NOT", 8) == 0)
1798 GL(dl_bind_not) = envline[9] != '\0';
1799 break;
1800
1801 case 9:
1802 /* Test whether we want to see the content of the auxiliary
1803 array passed up from the kernel. */
1804 if (memcmp (envline, "SHOW_AUXV", 9) == 0)
1805 _dl_show_auxv ();
1806 break;
1807
1808 case 10:
1809 /* Mask for the important hardware capabilities. */
1810 if (memcmp (envline, "HWCAP_MASK", 10) == 0)
1811 GL(dl_hwcap_mask) = __strtoul_internal (&envline[11], NULL, 0, 0);
1812 break;
1813
1814 case 11:
1815 /* Path where the binary is found. */
1816 if (!INTUSE(__libc_enable_secure)
1817 && memcmp (envline, "ORIGIN_PATH", 11) == 0)
1818 GL(dl_origin_path) = &envline[12];
1819 break;
1820
1821 case 12:
1822 /* The library search path. */
1823 if (memcmp (envline, "LIBRARY_PATH", 12) == 0)
1824 {
1825 library_path = &envline[13];
1826 break;
1827 }
1828
1829 /* Where to place the profiling data file. */
1830 if (memcmp (envline, "DEBUG_OUTPUT", 12) == 0)
1831 {
1832 debug_output = &envline[13];
1833 break;
1834 }
1835
1836 if (memcmp (envline, "DYNAMIC_WEAK", 12) == 0)
1837 GL(dl_dynamic_weak) = 1;
1838 break;
1839
1840 case 14:
1841 /* Where to place the profiling data file. */
1842 if (!INTUSE(__libc_enable_secure)
1843 && memcmp (envline, "PROFILE_OUTPUT", 14) == 0
1844 && envline[15] != '\0')
1845 GL(dl_profile_output) = &envline[15];
1846 break;
1847
1848 case 16:
1849 /* The mode of the dynamic linker can be set. */
1850 if (memcmp (envline, "TRACE_PRELINKING", 16) == 0)
1851 {
1852 mode = trace;
1853 GL(dl_verbose) = 1;
1854 GL(dl_debug_mask) |= DL_DEBUG_PRELINK;
1855 GL(dl_trace_prelink) = &envline[17];
1856 }
1857 break;
1858
1859 case 20:
1860 /* The mode of the dynamic linker can be set. */
1861 if (memcmp (envline, "TRACE_LOADED_OBJECTS", 20) == 0)
1862 mode = trace;
1863 break;
1864
1865 /* We might have some extra environment variable to handle. This
1866 is tricky due to the pre-processing of the length of the name
1867 in the switch statement here. The code here assumes that added
1868 environment variables have a different length. */
1869 #ifdef EXTRA_LD_ENVVARS
1870 EXTRA_LD_ENVVARS
1871 #endif
1872 }
1873 }
1874
1875 /* The caller wants this information. */
1876 *modep = mode;
1877
1878 /* Extra security for SUID binaries. Remove all dangerous environment
1879 variables. */
1880 if (__builtin_expect (INTUSE(__libc_enable_secure), 0))
1881 {
1882 static const char unsecure_envvars[] =
1883 #ifdef EXTRA_UNSECURE_ENVVARS
1884 EXTRA_UNSECURE_ENVVARS
1885 #endif
1886 UNSECURE_ENVVARS;
1887 const char *nextp;
1888
1889 nextp = unsecure_envvars;
1890 do
1891 {
1892 unsetenv (nextp);
1893 /* We could use rawmemchr but this need not be fast. */
1894 nextp = (char *) (strchr) (nextp, '\0') + 1;
1895 }
1896 while (*nextp != '\0');
1897
1898 if (__access ("/etc/suid-debug", F_OK) != 0)
1899 unsetenv ("MALLOC_CHECK_");
1900 }
1901 /* If we have to run the dynamic linker in debugging mode and the
1902 LD_DEBUG_OUTPUT environment variable is given, we write the debug
1903 messages to this file. */
1904 else if (any_debug && debug_output != NULL)
1905 {
1906 #ifdef O_NOFOLLOW
1907 const int flags = O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW;
1908 #else
1909 const int flags = O_WRONLY | O_APPEND | O_CREAT;
1910 #endif
1911 size_t name_len = strlen (debug_output);
1912 char buf[name_len + 12];
1913 char *startp;
1914
1915 buf[name_len + 11] = '\0';
1916 startp = _itoa (__getpid (), &buf[name_len + 11], 10, 0);
1917 *--startp = '.';
1918 startp = memcpy (startp - name_len, debug_output, name_len);
1919
1920 GL(dl_debug_fd) = __open (startp, flags, DEFFILEMODE);
1921 if (GL(dl_debug_fd) == -1)
1922 /* We use standard output if opening the file failed. */
1923 GL(dl_debug_fd) = STDOUT_FILENO;
1924 }
1925 }
1926
1927
1928 /* Print the various times we collected. */
1929 static void
1930 print_statistics (void)
1931 {
1932 #ifndef HP_TIMING_NONAVAIL
1933 char buf[200];
1934 char *cp;
1935 char *wp;
1936
1937 /* Total time rtld used. */
1938 if (HP_TIMING_AVAIL)
1939 {
1940 HP_TIMING_PRINT (buf, sizeof (buf), rtld_total_time);
1941 INTUSE(_dl_debug_printf) ("\nruntime linker statistics:\n"
1942 " total startup time in dynamic loader: %s\n",
1943 buf);
1944 }
1945
1946 /* Print relocation statistics. */
1947 if (HP_TIMING_AVAIL)
1948 {
1949 char pbuf[30];
1950 HP_TIMING_PRINT (buf, sizeof (buf), relocate_time);
1951 cp = _itoa ((1000ULL * relocate_time) / rtld_total_time,
1952 pbuf + sizeof (pbuf), 10, 0);
1953 wp = pbuf;
1954 switch (pbuf + sizeof (pbuf) - cp)
1955 {
1956 case 3:
1957 *wp++ = *cp++;
1958 case 2:
1959 *wp++ = *cp++;
1960 case 1:
1961 *wp++ = '.';
1962 *wp++ = *cp++;
1963 }
1964 *wp = '\0';
1965 INTUSE(_dl_debug_printf) ("\
1966 time needed for relocation: %s (%s%%)\n",
1967 buf, pbuf);
1968 }
1969 #endif
1970 INTUSE(_dl_debug_printf) (" number of relocations: %lu\n",
1971 GL(dl_num_relocations));
1972 INTUSE(_dl_debug_printf) (" number of relocations from cache: %lu\n",
1973 GL(dl_num_cache_relocations));
1974
1975 #ifndef HP_TIMING_NONAVAIL
1976 /* Time spend while loading the object and the dependencies. */
1977 if (HP_TIMING_AVAIL)
1978 {
1979 char pbuf[30];
1980 HP_TIMING_PRINT (buf, sizeof (buf), load_time);
1981 cp = _itoa ((1000ULL * load_time) / rtld_total_time,
1982 pbuf + sizeof (pbuf), 10, 0);
1983 wp = pbuf;
1984 switch (pbuf + sizeof (pbuf) - cp)
1985 {
1986 case 3:
1987 *wp++ = *cp++;
1988 case 2:
1989 *wp++ = *cp++;
1990 case 1:
1991 *wp++ = '.';
1992 *wp++ = *cp++;
1993 }
1994 *wp = '\0';
1995 INTUSE(_dl_debug_printf) ("\
1996 time needed to load objects: %s (%s%%)\n",
1997 buf, pbuf);
1998 }
1999 #endif
2000 }