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