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