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