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