Remove last use of USE___THREAD
[platform/upstream/glibc.git] / elf / rtld.c
1 /* Run time dynamic linker.
2    Copyright (C) 1995-2014 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, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <errno.h>
20 #include <dlfcn.h>
21 #include <fcntl.h>
22 #include <stdbool.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 #include <sys/param.h>
28 #include <sys/stat.h>
29 #include <ldsodefs.h>
30 #include <_itoa.h>
31 #include <entry.h>
32 #include <fpu_control.h>
33 #include <hp-timing.h>
34 #include <bits/libc-lock.h>
35 #include "dynamic-link.h"
36 #include <dl-librecon.h>
37 #include <unsecvars.h>
38 #include <dl-cache.h>
39 #include <dl-osinfo.h>
40 #include <dl-procinfo.h>
41 #include <tls.h>
42 #include <stap-probe.h>
43 #include <stackinfo.h>
44
45 #include <assert.h>
46
47 /* Avoid PLT use for our local calls at startup.  */
48 extern __typeof (__mempcpy) __mempcpy attribute_hidden;
49
50 /* GCC has mental blocks about _exit.  */
51 extern __typeof (_exit) exit_internal asm ("_exit") attribute_hidden;
52 #define _exit exit_internal
53
54 /* Helper function to handle errors while resolving symbols.  */
55 static void print_unresolved (int errcode, const char *objname,
56                               const char *errsting);
57
58 /* Helper function to handle errors when a version is missing.  */
59 static void print_missing_version (int errcode, const char *objname,
60                                    const char *errsting);
61
62 /* Print the various times we collected.  */
63 static void print_statistics (hp_timing_t *total_timep);
64
65 /* Add audit objects.  */
66 static void process_dl_audit (char *str);
67
68 /* This is a list of all the modes the dynamic loader can be in.  */
69 enum mode { normal, list, verify, trace };
70
71 /* Process all environments variables the dynamic linker must recognize.
72    Since all of them start with `LD_' we are a bit smarter while finding
73    all the entries.  */
74 static void process_envvars (enum mode *modep);
75
76 #ifdef DL_ARGV_NOT_RELRO
77 int _dl_argc attribute_hidden;
78 char **_dl_argv = NULL;
79 /* Nonzero if we were run directly.  */
80 unsigned int _dl_skip_args attribute_hidden;
81 #else
82 int _dl_argc attribute_relro attribute_hidden;
83 char **_dl_argv attribute_relro = NULL;
84 unsigned int _dl_skip_args attribute_relro attribute_hidden;
85 #endif
86 INTDEF(_dl_argv)
87
88 #ifndef THREAD_SET_STACK_GUARD
89 /* Only exported for architectures that don't store the stack guard canary
90    in thread local area.  */
91 uintptr_t __stack_chk_guard attribute_relro;
92 #endif
93
94 /* Only exported for architectures that don't store the pointer guard
95    value in thread local area.  */
96 uintptr_t __pointer_chk_guard_local
97      attribute_relro attribute_hidden __attribute__ ((nocommon));
98 #ifndef THREAD_SET_POINTER_GUARD
99 strong_alias (__pointer_chk_guard_local, __pointer_chk_guard)
100 #endif
101
102
103 /* List of auditing DSOs.  */
104 static struct audit_list
105 {
106   const char *name;
107   struct audit_list *next;
108 } *audit_list;
109
110 #ifndef HAVE_INLINED_SYSCALLS
111 /* Set nonzero during loading and initialization of executable and
112    libraries, cleared before the executable's entry point runs.  This
113    must not be initialized to nonzero, because the unused dynamic
114    linker loaded in for libc.so's "ld.so.1" dep will provide the
115    definition seen by libc.so's initializer; that value must be zero,
116    and will be since that dynamic linker's _dl_start and dl_main will
117    never be called.  */
118 int _dl_starting_up = 0;
119 INTVARDEF(_dl_starting_up)
120 #endif
121
122 /* This is the structure which defines all variables global to ld.so
123    (except those which cannot be added for some reason).  */
124 struct rtld_global _rtld_global =
125   {
126     /* Generally the default presumption without further information is an
127      * executable stack but this is not true for all platforms.  */
128     ._dl_stack_flags = DEFAULT_STACK_PERMS,
129 #ifdef _LIBC_REENTRANT
130     ._dl_load_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER,
131     ._dl_load_write_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER,
132 #endif
133     ._dl_nns = 1,
134     ._dl_ns =
135     {
136 #ifdef _LIBC_REENTRANT
137       [LM_ID_BASE] = { ._ns_unique_sym_table
138                        = { .lock = _RTLD_LOCK_RECURSIVE_INITIALIZER } }
139 #endif
140     }
141   };
142 /* If we would use strong_alias here the compiler would see a
143    non-hidden definition.  This would undo the effect of the previous
144    declaration.  So spell out was strong_alias does plus add the
145    visibility attribute.  */
146 extern struct rtld_global _rtld_local
147     __attribute__ ((alias ("_rtld_global"), visibility ("hidden")));
148
149
150 /* This variable is similar to _rtld_local, but all values are
151    read-only after relocation.  */
152 struct rtld_global_ro _rtld_global_ro attribute_relro =
153   {
154     /* Get architecture specific initializer.  */
155 #include <dl-procinfo.c>
156 #ifdef NEED_DL_SYSINFO
157     ._dl_sysinfo = DL_SYSINFO_DEFAULT,
158 #endif
159     ._dl_debug_fd = STDERR_FILENO,
160     ._dl_use_load_bias = -2,
161     ._dl_correct_cache_id = _DL_CACHE_DEFAULT_ID,
162     ._dl_hwcap_mask = HWCAP_IMPORTANT,
163     ._dl_lazy = 1,
164     ._dl_fpu_control = _FPU_DEFAULT,
165     ._dl_pointer_guard = 1,
166     ._dl_pagesize = EXEC_PAGESIZE,
167     ._dl_inhibit_cache = 0,
168
169     /* Function pointers.  */
170     ._dl_debug_printf = _dl_debug_printf,
171     ._dl_catch_error = _dl_catch_error,
172     ._dl_signal_error = _dl_signal_error,
173     ._dl_mcount = _dl_mcount_internal,
174     ._dl_lookup_symbol_x = _dl_lookup_symbol_x,
175     ._dl_check_caller = _dl_check_caller,
176     ._dl_open = _dl_open,
177     ._dl_close = _dl_close,
178     ._dl_tls_get_addr_soft = _dl_tls_get_addr_soft,
179 #ifdef HAVE_DL_DISCOVER_OSVERSION
180     ._dl_discover_osversion = _dl_discover_osversion
181 #endif
182   };
183 /* If we would use strong_alias here the compiler would see a
184    non-hidden definition.  This would undo the effect of the previous
185    declaration.  So spell out was strong_alias does plus add the
186    visibility attribute.  */
187 extern struct rtld_global_ro _rtld_local_ro
188     __attribute__ ((alias ("_rtld_global_ro"), visibility ("hidden")));
189
190
191 static void dl_main (const ElfW(Phdr) *phdr, ElfW(Word) phnum,
192                      ElfW(Addr) *user_entry, ElfW(auxv_t) *auxv);
193
194 /* These two variables cannot be moved into .data.rel.ro.  */
195 static struct libname_list _dl_rtld_libname;
196 static struct libname_list _dl_rtld_libname2;
197
198 /* We expect less than a second for relocation.  */
199 #ifdef HP_SMALL_TIMING_AVAIL
200 # undef HP_TIMING_AVAIL
201 # define HP_TIMING_AVAIL HP_SMALL_TIMING_AVAIL
202 #endif
203
204 /* Variable for statistics.  */
205 #ifndef HP_TIMING_NONAVAIL
206 static hp_timing_t relocate_time;
207 static hp_timing_t load_time attribute_relro;
208 static hp_timing_t start_time attribute_relro;
209 #endif
210
211 /* Additional definitions needed by TLS initialization.  */
212 #ifdef TLS_INIT_HELPER
213 TLS_INIT_HELPER
214 #endif
215
216 /* Helper function for syscall implementation.  */
217 #ifdef DL_SYSINFO_IMPLEMENTATION
218 DL_SYSINFO_IMPLEMENTATION
219 #endif
220
221 /* Before ld.so is relocated we must not access variables which need
222    relocations.  This means variables which are exported.  Variables
223    declared as static are fine.  If we can mark a variable hidden this
224    is fine, too.  The latter is important here.  We can avoid setting
225    up a temporary link map for ld.so if we can mark _rtld_global as
226    hidden.  */
227 #ifdef PI_STATIC_AND_HIDDEN
228 # define DONT_USE_BOOTSTRAP_MAP 1
229 #endif
230
231 #ifdef DONT_USE_BOOTSTRAP_MAP
232 static ElfW(Addr) _dl_start_final (void *arg);
233 #else
234 struct dl_start_final_info
235 {
236   struct link_map l;
237 #if !defined HP_TIMING_NONAVAIL && HP_TIMING_INLINE
238   hp_timing_t start_time;
239 #endif
240 };
241 static ElfW(Addr) _dl_start_final (void *arg,
242                                    struct dl_start_final_info *info);
243 #endif
244
245 /* These defined magically in the linker script.  */
246 extern char _begin[] attribute_hidden;
247 extern char _etext[] attribute_hidden;
248 extern char _end[] attribute_hidden;
249
250
251 #ifdef RTLD_START
252 RTLD_START
253 #else
254 # error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
255 #endif
256
257 /* This is the second half of _dl_start (below).  It can be inlined safely
258    under DONT_USE_BOOTSTRAP_MAP, where it is careful not to make any GOT
259    references.  When the tools don't permit us to avoid using a GOT entry
260    for _dl_rtld_global (no attribute_hidden support), we must make sure
261    this function is not inlined (see below).  */
262
263 #ifdef DONT_USE_BOOTSTRAP_MAP
264 static inline ElfW(Addr) __attribute__ ((always_inline))
265 _dl_start_final (void *arg)
266 #else
267 static ElfW(Addr) __attribute__ ((noinline))
268 _dl_start_final (void *arg, struct dl_start_final_info *info)
269 #endif
270 {
271   ElfW(Addr) start_addr;
272
273   if (HP_TIMING_AVAIL)
274     {
275       /* If it hasn't happen yet record the startup time.  */
276       if (! HP_TIMING_INLINE)
277         HP_TIMING_NOW (start_time);
278 #if !defined DONT_USE_BOOTSTRAP_MAP && !defined HP_TIMING_NONAVAIL
279       else
280         start_time = info->start_time;
281 #endif
282
283       /* Initialize the timing functions.  */
284       HP_TIMING_DIFF_INIT ();
285     }
286
287   /* Transfer data about ourselves to the permanent link_map structure.  */
288 #ifndef DONT_USE_BOOTSTRAP_MAP
289   GL(dl_rtld_map).l_addr = info->l.l_addr;
290   GL(dl_rtld_map).l_ld = info->l.l_ld;
291   memcpy (GL(dl_rtld_map).l_info, info->l.l_info,
292           sizeof GL(dl_rtld_map).l_info);
293   GL(dl_rtld_map).l_mach = info->l.l_mach;
294   GL(dl_rtld_map).l_relocated = 1;
295 #endif
296   _dl_setup_hash (&GL(dl_rtld_map));
297   GL(dl_rtld_map).l_real = &GL(dl_rtld_map);
298   GL(dl_rtld_map).l_map_start = (ElfW(Addr)) _begin;
299   GL(dl_rtld_map).l_map_end = (ElfW(Addr)) _end;
300   GL(dl_rtld_map).l_text_end = (ElfW(Addr)) _etext;
301   /* Copy the TLS related data if necessary.  */
302 #ifndef DONT_USE_BOOTSTRAP_MAP
303 # if NO_TLS_OFFSET != 0
304   GL(dl_rtld_map).l_tls_offset = NO_TLS_OFFSET;
305 # endif
306 #endif
307
308 #if HP_TIMING_AVAIL
309   HP_TIMING_NOW (GL(dl_cpuclock_offset));
310 #endif
311
312   /* Initialize the stack end variable.  */
313   __libc_stack_end = __builtin_frame_address (0);
314
315   /* Call the OS-dependent function to set up life so we can do things like
316      file access.  It will call `dl_main' (below) to do all the real work
317      of the dynamic linker, and then unwind our frame and run the user
318      entry point on the same stack we entered on.  */
319   start_addr = _dl_sysdep_start (arg, &dl_main);
320
321 #ifndef HP_TIMING_NONAVAIL
322   hp_timing_t rtld_total_time;
323   if (HP_TIMING_AVAIL)
324     {
325       hp_timing_t end_time;
326
327       /* Get the current time.  */
328       HP_TIMING_NOW (end_time);
329
330       /* Compute the difference.  */
331       HP_TIMING_DIFF (rtld_total_time, start_time, end_time);
332     }
333 #endif
334
335   if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_STATISTICS))
336     {
337 #ifndef HP_TIMING_NONAVAIL
338       print_statistics (&rtld_total_time);
339 #else
340       print_statistics (NULL);
341 #endif
342     }
343
344   return start_addr;
345 }
346
347 static ElfW(Addr) __attribute_used__ internal_function
348 _dl_start (void *arg)
349 {
350 #ifdef DONT_USE_BOOTSTRAP_MAP
351 # define bootstrap_map GL(dl_rtld_map)
352 #else
353   struct dl_start_final_info info;
354 # define bootstrap_map info.l
355 #endif
356
357   /* This #define produces dynamic linking inline functions for
358      bootstrap relocation instead of general-purpose relocation.
359      Since ld.so must not have any undefined symbols the result
360      is trivial: always the map of ld.so itself.  */
361 #define RTLD_BOOTSTRAP
362 #define RESOLVE_MAP(sym, version, flags) (&bootstrap_map)
363 #include "dynamic-link.h"
364
365   if (HP_TIMING_INLINE && HP_TIMING_AVAIL)
366 #ifdef DONT_USE_BOOTSTRAP_MAP
367     HP_TIMING_NOW (start_time);
368 #else
369     HP_TIMING_NOW (info.start_time);
370 #endif
371
372   /* Partly clean the `bootstrap_map' structure up.  Don't use
373      `memset' since it might not be built in or inlined and we cannot
374      make function calls at this point.  Use '__builtin_memset' if we
375      know it is available.  We do not have to clear the memory if we
376      do not have to use the temporary bootstrap_map.  Global variables
377      are initialized to zero by default.  */
378 #ifndef DONT_USE_BOOTSTRAP_MAP
379 # ifdef HAVE_BUILTIN_MEMSET
380   __builtin_memset (bootstrap_map.l_info, '\0', sizeof (bootstrap_map.l_info));
381 # else
382   for (size_t cnt = 0;
383        cnt < sizeof (bootstrap_map.l_info) / sizeof (bootstrap_map.l_info[0]);
384        ++cnt)
385     bootstrap_map.l_info[cnt] = 0;
386 # endif
387 #endif
388
389   /* Figure out the run-time load address of the dynamic linker itself.  */
390   bootstrap_map.l_addr = elf_machine_load_address ();
391
392   /* Read our own dynamic section and fill in the info array.  */
393   bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
394   elf_get_dynamic_info (&bootstrap_map, NULL);
395
396 #if NO_TLS_OFFSET != 0
397   bootstrap_map.l_tls_offset = NO_TLS_OFFSET;
398 #endif
399
400 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
401   ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info);
402 #endif
403
404   if (bootstrap_map.l_addr || ! bootstrap_map.l_info[VALIDX(DT_GNU_PRELINKED)])
405     {
406       /* Relocate ourselves so we can do normal function calls and
407          data access using the global offset table.  */
408
409       ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0, 0, 0);
410     }
411   bootstrap_map.l_relocated = 1;
412
413   /* Please note that we don't allow profiling of this object and
414      therefore need not test whether we have to allocate the array
415      for the relocation results (as done in dl-reloc.c).  */
416
417   /* Now life is sane; we can call functions and access global data.
418      Set up to use the operating system facilities, and find out from
419      the operating system's program loader where to find the program
420      header table in core.  Put the rest of _dl_start into a separate
421      function, that way the compiler cannot put accesses to the GOT
422      before ELF_DYNAMIC_RELOCATE.  */
423   {
424 #ifdef DONT_USE_BOOTSTRAP_MAP
425     ElfW(Addr) entry = _dl_start_final (arg);
426 #else
427     ElfW(Addr) entry = _dl_start_final (arg, &info);
428 #endif
429
430 #ifndef ELF_MACHINE_START_ADDRESS
431 # define ELF_MACHINE_START_ADDRESS(map, start) (start)
432 #endif
433
434     return ELF_MACHINE_START_ADDRESS (GL(dl_ns)[LM_ID_BASE]._ns_loaded, entry);
435   }
436 }
437
438
439
440 /* Now life is peachy; we can do all normal operations.
441    On to the real work.  */
442
443 /* Some helper functions.  */
444
445 /* Arguments to relocate_doit.  */
446 struct relocate_args
447 {
448   struct link_map *l;
449   int reloc_mode;
450 };
451
452 struct map_args
453 {
454   /* Argument to map_doit.  */
455   char *str;
456   struct link_map *loader;
457   int mode;
458   /* Return value of map_doit.  */
459   struct link_map *map;
460 };
461
462 struct dlmopen_args
463 {
464   const char *fname;
465   struct link_map *map;
466 };
467
468 struct lookup_args
469 {
470   const char *name;
471   struct link_map *map;
472   void *result;
473 };
474
475 /* Arguments to version_check_doit.  */
476 struct version_check_args
477 {
478   int doexit;
479   int dotrace;
480 };
481
482 static void
483 relocate_doit (void *a)
484 {
485   struct relocate_args *args = (struct relocate_args *) a;
486
487   _dl_relocate_object (args->l, args->l->l_scope, args->reloc_mode, 0);
488 }
489
490 static void
491 map_doit (void *a)
492 {
493   struct map_args *args = (struct map_args *) a;
494   int type = (args->mode == __RTLD_OPENEXEC) ? lt_executable : lt_library;
495   args->map = _dl_map_object (args->loader, args->str, type, 0,
496                               args->mode, LM_ID_BASE);
497 }
498
499 static void
500 dlmopen_doit (void *a)
501 {
502   struct dlmopen_args *args = (struct dlmopen_args *) a;
503   args->map = _dl_open (args->fname,
504                         (RTLD_LAZY | __RTLD_DLOPEN | __RTLD_AUDIT
505                          | __RTLD_SECURE),
506                         dl_main, LM_ID_NEWLM, _dl_argc, INTUSE(_dl_argv),
507                         __environ);
508 }
509
510 static void
511 lookup_doit (void *a)
512 {
513   struct lookup_args *args = (struct lookup_args *) a;
514   const ElfW(Sym) *ref = NULL;
515   args->result = NULL;
516   lookup_t l = _dl_lookup_symbol_x (args->name, args->map, &ref,
517                                     args->map->l_local_scope, NULL, 0,
518                                     DL_LOOKUP_RETURN_NEWEST, NULL);
519   if (ref != NULL)
520     args->result = DL_SYMBOL_ADDRESS (l, ref);
521 }
522
523 static void
524 version_check_doit (void *a)
525 {
526   struct version_check_args *args = (struct version_check_args *) a;
527   if (_dl_check_all_versions (GL(dl_ns)[LM_ID_BASE]._ns_loaded, 1,
528                               args->dotrace) && args->doexit)
529     /* We cannot start the application.  Abort now.  */
530     _exit (1);
531 }
532
533
534 static inline struct link_map *
535 find_needed (const char *name)
536 {
537   struct r_scope_elem *scope = &GL(dl_ns)[LM_ID_BASE]._ns_loaded->l_searchlist;
538   unsigned int n = scope->r_nlist;
539
540   while (n-- > 0)
541     if (_dl_name_match_p (name, scope->r_list[n]))
542       return scope->r_list[n];
543
544   /* Should never happen.  */
545   return NULL;
546 }
547
548 static int
549 match_version (const char *string, struct link_map *map)
550 {
551   const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
552   ElfW(Verdef) *def;
553
554 #define VERDEFTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
555   if (map->l_info[VERDEFTAG] == NULL)
556     /* The file has no symbol versioning.  */
557     return 0;
558
559   def = (ElfW(Verdef) *) ((char *) map->l_addr
560                           + map->l_info[VERDEFTAG]->d_un.d_ptr);
561   while (1)
562     {
563       ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
564
565       /* Compare the version strings.  */
566       if (strcmp (string, strtab + aux->vda_name) == 0)
567         /* Bingo!  */
568         return 1;
569
570       /* If no more definitions we failed to find what we want.  */
571       if (def->vd_next == 0)
572         break;
573
574       /* Next definition.  */
575       def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
576     }
577
578   return 0;
579 }
580
581 static bool tls_init_tp_called;
582
583 static void *
584 init_tls (void)
585 {
586   /* Number of elements in the static TLS block.  */
587   GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx);
588
589   /* Do not do this twice.  The audit interface might have required
590      the DTV interfaces to be set up early.  */
591   if (GL(dl_initial_dtv) != NULL)
592     return NULL;
593
594   /* Allocate the array which contains the information about the
595      dtv slots.  We allocate a few entries more than needed to
596      avoid the need for reallocation.  */
597   size_t nelem = GL(dl_tls_max_dtv_idx) + 1 + TLS_SLOTINFO_SURPLUS;
598
599   /* Allocate.  */
600   GL(dl_tls_dtv_slotinfo_list) = (struct dtv_slotinfo_list *)
601     calloc (sizeof (struct dtv_slotinfo_list)
602             + nelem * sizeof (struct dtv_slotinfo), 1);
603   /* No need to check the return value.  If memory allocation failed
604      the program would have been terminated.  */
605
606   struct dtv_slotinfo *slotinfo = GL(dl_tls_dtv_slotinfo_list)->slotinfo;
607   GL(dl_tls_dtv_slotinfo_list)->len = nelem;
608   GL(dl_tls_dtv_slotinfo_list)->next = NULL;
609
610   /* Fill in the information from the loaded modules.  No namespace
611      but the base one can be filled at this time.  */
612   assert (GL(dl_ns)[LM_ID_BASE + 1]._ns_loaded == NULL);
613   int i = 0;
614   for (struct link_map *l = GL(dl_ns)[LM_ID_BASE]._ns_loaded; l != NULL;
615        l = l->l_next)
616     if (l->l_tls_blocksize != 0)
617       {
618         /* This is a module with TLS data.  Store the map reference.
619            The generation counter is zero.  */
620         slotinfo[i].map = l;
621         /* slotinfo[i].gen = 0; */
622         ++i;
623       }
624   assert (i == GL(dl_tls_max_dtv_idx));
625
626   /* Compute the TLS offsets for the various blocks.  */
627   _dl_determine_tlsoffset ();
628
629   /* Construct the static TLS block and the dtv for the initial
630      thread.  For some platforms this will include allocating memory
631      for the thread descriptor.  The memory for the TLS block will
632      never be freed.  It should be allocated accordingly.  The dtv
633      array can be changed if dynamic loading requires it.  */
634   void *tcbp = _dl_allocate_tls_storage ();
635   if (tcbp == NULL)
636     _dl_fatal_printf ("\
637 cannot allocate TLS data structures for initial thread");
638
639   /* Store for detection of the special case by __tls_get_addr
640      so it knows not to pass this dtv to the normal realloc.  */
641   GL(dl_initial_dtv) = GET_DTV (tcbp);
642
643   /* And finally install it for the main thread.  */
644   const char *lossage = TLS_INIT_TP (tcbp, 0);
645   if (__glibc_unlikely (lossage != NULL))
646     _dl_fatal_printf ("cannot set up thread-local storage: %s\n", lossage);
647   tls_init_tp_called = true;
648
649   return tcbp;
650 }
651
652 #ifdef _LIBC_REENTRANT
653 /* _dl_error_catch_tsd points to this for the single-threaded case.
654    It's reset by the thread library for multithreaded programs.  */
655 void ** __attribute__ ((const))
656 _dl_initial_error_catch_tsd (void)
657 {
658   static void *data;
659   return &data;
660 }
661 #endif
662
663
664 static unsigned int
665 do_preload (char *fname, struct link_map *main_map, const char *where)
666 {
667   const char *objname;
668   const char *err_str = NULL;
669   struct map_args args;
670   bool malloced;
671
672   args.str = fname;
673   args.loader = main_map;
674   args.mode = __RTLD_SECURE;
675
676   unsigned int old_nloaded = GL(dl_ns)[LM_ID_BASE]._ns_nloaded;
677
678   (void) _dl_catch_error (&objname, &err_str, &malloced, map_doit, &args);
679   if (__glibc_unlikely (err_str != NULL))
680     {
681       _dl_error_printf ("\
682 ERROR: ld.so: object '%s' from %s cannot be preloaded (%s): ignored.\n",
683                         fname, where, err_str);
684       /* No need to call free, this is still before
685          the libc's malloc is used.  */
686     }
687   else if (GL(dl_ns)[LM_ID_BASE]._ns_nloaded != old_nloaded)
688     /* It is no duplicate.  */
689     return 1;
690
691   /* Nothing loaded.  */
692   return 0;
693 }
694
695 #if defined SHARED && defined _LIBC_REENTRANT \
696     && defined __rtld_lock_default_lock_recursive
697 static void
698 rtld_lock_default_lock_recursive (void *lock)
699 {
700   __rtld_lock_default_lock_recursive (lock);
701 }
702
703 static void
704 rtld_lock_default_unlock_recursive (void *lock)
705 {
706   __rtld_lock_default_unlock_recursive (lock);
707 }
708 #endif
709
710
711 static void
712 security_init (void)
713 {
714   /* Set up the stack checker's canary.  */
715   uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard (_dl_random);
716 #ifdef THREAD_SET_STACK_GUARD
717   THREAD_SET_STACK_GUARD (stack_chk_guard);
718 #else
719   __stack_chk_guard = stack_chk_guard;
720 #endif
721
722   /* Set up the pointer guard as well, if necessary.  */
723   if (GLRO(dl_pointer_guard))
724     {
725       uintptr_t pointer_chk_guard = _dl_setup_pointer_guard (_dl_random,
726                                                              stack_chk_guard);
727 #ifdef THREAD_SET_POINTER_GUARD
728       THREAD_SET_POINTER_GUARD (pointer_chk_guard);
729 #endif
730       __pointer_chk_guard_local = pointer_chk_guard;
731     }
732
733   /* We do not need the _dl_random value anymore.  The less
734      information we leave behind, the better, so clear the
735      variable.  */
736   _dl_random = NULL;
737 }
738
739 #include "setup-vdso.h"
740
741 /* The library search path.  */
742 static const char *library_path attribute_relro;
743 /* The list preloaded objects.  */
744 static const char *preloadlist attribute_relro;
745 /* Nonzero if information about versions has to be printed.  */
746 static int version_info attribute_relro;
747
748 static void
749 dl_main (const ElfW(Phdr) *phdr,
750          ElfW(Word) phnum,
751          ElfW(Addr) *user_entry,
752          ElfW(auxv_t) *auxv)
753 {
754   const ElfW(Phdr) *ph;
755   enum mode mode;
756   struct link_map *main_map;
757   size_t file_size;
758   char *file;
759   bool has_interp = false;
760   unsigned int i;
761   bool prelinked = false;
762   bool rtld_is_main = false;
763 #ifndef HP_TIMING_NONAVAIL
764   hp_timing_t start;
765   hp_timing_t stop;
766   hp_timing_t diff;
767 #endif
768   void *tcbp = NULL;
769
770 #ifdef _LIBC_REENTRANT
771   /* Explicit initialization since the reloc would just be more work.  */
772   GL(dl_error_catch_tsd) = &_dl_initial_error_catch_tsd;
773 #endif
774
775   GL(dl_init_static_tls) = &_dl_nothread_init_static_tls;
776
777 #if defined SHARED && defined _LIBC_REENTRANT \
778     && defined __rtld_lock_default_lock_recursive
779   GL(dl_rtld_lock_recursive) = rtld_lock_default_lock_recursive;
780   GL(dl_rtld_unlock_recursive) = rtld_lock_default_unlock_recursive;
781 #endif
782
783   /* The explicit initialization here is cheaper than processing the reloc
784      in the _rtld_local definition's initializer.  */
785   GL(dl_make_stack_executable_hook) = &_dl_make_stack_executable;
786
787   /* Process the environment variable which control the behaviour.  */
788   process_envvars (&mode);
789
790 #ifndef HAVE_INLINED_SYSCALLS
791   /* Set up a flag which tells we are just starting.  */
792   INTUSE(_dl_starting_up) = 1;
793 #endif
794
795   if (*user_entry == (ElfW(Addr)) ENTRY_POINT)
796     {
797       /* Ho ho.  We are not the program interpreter!  We are the program
798          itself!  This means someone ran ld.so as a command.  Well, that
799          might be convenient to do sometimes.  We support it by
800          interpreting the args like this:
801
802          ld.so PROGRAM ARGS...
803
804          The first argument is the name of a file containing an ELF
805          executable we will load and run with the following arguments.
806          To simplify life here, PROGRAM is searched for using the
807          normal rules for shared objects, rather than $PATH or anything
808          like that.  We just load it and use its entry point; we don't
809          pay attention to its PT_INTERP command (we are the interpreter
810          ourselves).  This is an easy way to test a new ld.so before
811          installing it.  */
812       rtld_is_main = true;
813
814       /* Note the place where the dynamic linker actually came from.  */
815       GL(dl_rtld_map).l_name = rtld_progname;
816
817       while (_dl_argc > 1)
818         if (! strcmp (INTUSE(_dl_argv)[1], "--list"))
819           {
820             mode = list;
821             GLRO(dl_lazy) = -1; /* This means do no dependency analysis.  */
822
823             ++_dl_skip_args;
824             --_dl_argc;
825             ++INTUSE(_dl_argv);
826           }
827         else if (! strcmp (INTUSE(_dl_argv)[1], "--verify"))
828           {
829             mode = verify;
830
831             ++_dl_skip_args;
832             --_dl_argc;
833             ++INTUSE(_dl_argv);
834           }
835         else if (! strcmp (INTUSE(_dl_argv)[1], "--inhibit-cache"))
836           {
837             GLRO(dl_inhibit_cache) = 1;
838             ++_dl_skip_args;
839             --_dl_argc;
840             ++INTUSE(_dl_argv);
841           }
842         else if (! strcmp (INTUSE(_dl_argv)[1], "--library-path")
843                  && _dl_argc > 2)
844           {
845             library_path = INTUSE(_dl_argv)[2];
846
847             _dl_skip_args += 2;
848             _dl_argc -= 2;
849             INTUSE(_dl_argv) += 2;
850           }
851         else if (! strcmp (INTUSE(_dl_argv)[1], "--inhibit-rpath")
852                  && _dl_argc > 2)
853           {
854             GLRO(dl_inhibit_rpath) = INTUSE(_dl_argv)[2];
855
856             _dl_skip_args += 2;
857             _dl_argc -= 2;
858             INTUSE(_dl_argv) += 2;
859           }
860         else if (! strcmp (INTUSE(_dl_argv)[1], "--audit") && _dl_argc > 2)
861           {
862             process_dl_audit (INTUSE(_dl_argv)[2]);
863
864             _dl_skip_args += 2;
865             _dl_argc -= 2;
866             INTUSE(_dl_argv) += 2;
867           }
868         else
869           break;
870
871       /* If we have no further argument the program was called incorrectly.
872          Grant the user some education.  */
873       if (_dl_argc < 2)
874         _dl_fatal_printf ("\
875 Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
876 You have invoked `ld.so', the helper program for shared library executables.\n\
877 This program usually lives in the file `/lib/ld.so', and special directives\n\
878 in executable files using ELF shared libraries tell the system's program\n\
879 loader to load the helper program from this file.  This helper program loads\n\
880 the shared libraries needed by the program executable, prepares the program\n\
881 to run, and runs it.  You may invoke this helper program directly from the\n\
882 command line to load and run an ELF executable file; this is like executing\n\
883 that file itself, but always uses this helper program from the file you\n\
884 specified, instead of the helper program file specified in the executable\n\
885 file you run.  This is mostly of use for maintainers to test new versions\n\
886 of this helper program; chances are you did not intend to run this program.\n\
887 \n\
888   --list                list all dependencies and how they are resolved\n\
889   --verify              verify that given object really is a dynamically linked\n\
890                         object we can handle\n\
891   --inhibit-cache       Do not use " LD_SO_CACHE "\n\
892   --library-path PATH   use given PATH instead of content of the environment\n\
893                         variable LD_LIBRARY_PATH\n\
894   --inhibit-rpath LIST  ignore RUNPATH and RPATH information in object names\n\
895                         in LIST\n\
896   --audit LIST          use objects named in LIST as auditors\n");
897
898       ++_dl_skip_args;
899       --_dl_argc;
900       ++INTUSE(_dl_argv);
901
902       /* The initialization of _dl_stack_flags done below assumes the
903          executable's PT_GNU_STACK may have been honored by the kernel, and
904          so a PT_GNU_STACK with PF_X set means the stack started out with
905          execute permission.  However, this is not really true if the
906          dynamic linker is the executable the kernel loaded.  For this
907          case, we must reinitialize _dl_stack_flags to match the dynamic
908          linker itself.  If the dynamic linker was built with a
909          PT_GNU_STACK, then the kernel may have loaded us with a
910          nonexecutable stack that we will have to make executable when we
911          load the program below unless it has a PT_GNU_STACK indicating
912          nonexecutable stack is ok.  */
913
914       for (ph = phdr; ph < &phdr[phnum]; ++ph)
915         if (ph->p_type == PT_GNU_STACK)
916           {
917             GL(dl_stack_flags) = ph->p_flags;
918             break;
919           }
920
921       if (__builtin_expect (mode, normal) == verify)
922         {
923           const char *objname;
924           const char *err_str = NULL;
925           struct map_args args;
926           bool malloced;
927
928           args.str = rtld_progname;
929           args.loader = NULL;
930           args.mode = __RTLD_OPENEXEC;
931           (void) _dl_catch_error (&objname, &err_str, &malloced, map_doit,
932                                   &args);
933           if (__glibc_unlikely (err_str != NULL))
934             /* We don't free the returned string, the programs stops
935                anyway.  */
936             _exit (EXIT_FAILURE);
937         }
938       else
939         {
940           HP_TIMING_NOW (start);
941           _dl_map_object (NULL, rtld_progname, lt_executable, 0,
942                           __RTLD_OPENEXEC, LM_ID_BASE);
943           HP_TIMING_NOW (stop);
944
945           HP_TIMING_DIFF (load_time, start, stop);
946         }
947
948       /* Now the map for the main executable is available.  */
949       main_map = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
950
951       if (__builtin_expect (mode, normal) == normal
952           && GL(dl_rtld_map).l_info[DT_SONAME] != NULL
953           && main_map->l_info[DT_SONAME] != NULL
954           && strcmp ((const char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB])
955                      + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_val,
956                      (const char *) D_PTR (main_map, l_info[DT_STRTAB])
957                      + main_map->l_info[DT_SONAME]->d_un.d_val) == 0)
958         _dl_fatal_printf ("loader cannot load itself\n");
959
960       phdr = main_map->l_phdr;
961       phnum = main_map->l_phnum;
962       /* We overwrite here a pointer to a malloc()ed string.  But since
963          the malloc() implementation used at this point is the dummy
964          implementations which has no real free() function it does not
965          makes sense to free the old string first.  */
966       main_map->l_name = (char *) "";
967       *user_entry = main_map->l_entry;
968
969 #ifdef HAVE_AUX_VECTOR
970       /* Adjust the on-stack auxiliary vector so that it looks like the
971          binary was executed directly.  */
972       for (ElfW(auxv_t) *av = auxv; av->a_type != AT_NULL; av++)
973         switch (av->a_type)
974           {
975           case AT_PHDR:
976             av->a_un.a_val = (uintptr_t) phdr;
977             break;
978           case AT_PHNUM:
979             av->a_un.a_val = phnum;
980             break;
981           case AT_ENTRY:
982             av->a_un.a_val = *user_entry;
983             break;
984           case AT_EXECFN:
985             av->a_un.a_val = (uintptr_t) _dl_argv[0];
986             break;
987           }
988 #endif
989     }
990   else
991     {
992       /* Create a link_map for the executable itself.
993          This will be what dlopen on "" returns.  */
994       main_map = _dl_new_object ((char *) "", "", lt_executable, NULL,
995                                  __RTLD_OPENEXEC, LM_ID_BASE);
996       assert (main_map != NULL);
997       main_map->l_phdr = phdr;
998       main_map->l_phnum = phnum;
999       main_map->l_entry = *user_entry;
1000
1001       /* Even though the link map is not yet fully initialized we can add
1002          it to the map list since there are no possible users running yet.  */
1003       _dl_add_to_namespace_list (main_map, LM_ID_BASE);
1004       assert (main_map == GL(dl_ns)[LM_ID_BASE]._ns_loaded);
1005
1006       /* At this point we are in a bit of trouble.  We would have to
1007          fill in the values for l_dev and l_ino.  But in general we
1008          do not know where the file is.  We also do not handle AT_EXECFD
1009          even if it would be passed up.
1010
1011          We leave the values here defined to 0.  This is normally no
1012          problem as the program code itself is normally no shared
1013          object and therefore cannot be loaded dynamically.  Nothing
1014          prevent the use of dynamic binaries and in these situations
1015          we might get problems.  We might not be able to find out
1016          whether the object is already loaded.  But since there is no
1017          easy way out and because the dynamic binary must also not
1018          have an SONAME we ignore this program for now.  If it becomes
1019          a problem we can force people using SONAMEs.  */
1020
1021       /* We delay initializing the path structure until we got the dynamic
1022          information for the program.  */
1023     }
1024
1025   main_map->l_map_end = 0;
1026   main_map->l_text_end = 0;
1027   /* Perhaps the executable has no PT_LOAD header entries at all.  */
1028   main_map->l_map_start = ~0;
1029   /* And it was opened directly.  */
1030   ++main_map->l_direct_opencount;
1031
1032   /* Scan the program header table for the dynamic section.  */
1033   for (ph = phdr; ph < &phdr[phnum]; ++ph)
1034     switch (ph->p_type)
1035       {
1036       case PT_PHDR:
1037         /* Find out the load address.  */
1038         main_map->l_addr = (ElfW(Addr)) phdr - ph->p_vaddr;
1039         break;
1040       case PT_DYNAMIC:
1041         /* This tells us where to find the dynamic section,
1042            which tells us everything we need to do.  */
1043         main_map->l_ld = (void *) main_map->l_addr + ph->p_vaddr;
1044         break;
1045       case PT_INTERP:
1046         /* This "interpreter segment" was used by the program loader to
1047            find the program interpreter, which is this program itself, the
1048            dynamic linker.  We note what name finds us, so that a future
1049            dlopen call or DT_NEEDED entry, for something that wants to link
1050            against the dynamic linker as a shared library, will know that
1051            the shared object is already loaded.  */
1052         _dl_rtld_libname.name = ((const char *) main_map->l_addr
1053                                  + ph->p_vaddr);
1054         /* _dl_rtld_libname.next = NULL;        Already zero.  */
1055         GL(dl_rtld_map).l_libname = &_dl_rtld_libname;
1056
1057         /* Ordinarilly, we would get additional names for the loader from
1058            our DT_SONAME.  This can't happen if we were actually linked as
1059            a static executable (detect this case when we have no DYNAMIC).
1060            If so, assume the filename component of the interpreter path to
1061            be our SONAME, and add it to our name list.  */
1062         if (GL(dl_rtld_map).l_ld == NULL)
1063           {
1064             const char *p = NULL;
1065             const char *cp = _dl_rtld_libname.name;
1066
1067             /* Find the filename part of the path.  */
1068             while (*cp != '\0')
1069               if (*cp++ == '/')
1070                 p = cp;
1071
1072             if (p != NULL)
1073               {
1074                 _dl_rtld_libname2.name = p;
1075                 /* _dl_rtld_libname2.next = NULL;  Already zero.  */
1076                 _dl_rtld_libname.next = &_dl_rtld_libname2;
1077               }
1078           }
1079
1080         has_interp = true;
1081         break;
1082       case PT_LOAD:
1083         {
1084           ElfW(Addr) mapstart;
1085           ElfW(Addr) allocend;
1086
1087           /* Remember where the main program starts in memory.  */
1088           mapstart = (main_map->l_addr
1089                       + (ph->p_vaddr & ~(GLRO(dl_pagesize) - 1)));
1090           if (main_map->l_map_start > mapstart)
1091             main_map->l_map_start = mapstart;
1092
1093           /* Also where it ends.  */
1094           allocend = main_map->l_addr + ph->p_vaddr + ph->p_memsz;
1095           if (main_map->l_map_end < allocend)
1096             main_map->l_map_end = allocend;
1097           if ((ph->p_flags & PF_X) && allocend > main_map->l_text_end)
1098             main_map->l_text_end = allocend;
1099         }
1100         break;
1101
1102       case PT_TLS:
1103         if (ph->p_memsz > 0)
1104           {
1105             /* Note that in the case the dynamic linker we duplicate work
1106                here since we read the PT_TLS entry already in
1107                _dl_start_final.  But the result is repeatable so do not
1108                check for this special but unimportant case.  */
1109             main_map->l_tls_blocksize = ph->p_memsz;
1110             main_map->l_tls_align = ph->p_align;
1111             if (ph->p_align == 0)
1112               main_map->l_tls_firstbyte_offset = 0;
1113             else
1114               main_map->l_tls_firstbyte_offset = (ph->p_vaddr
1115                                                   & (ph->p_align - 1));
1116             main_map->l_tls_initimage_size = ph->p_filesz;
1117             main_map->l_tls_initimage = (void *) ph->p_vaddr;
1118
1119             /* This image gets the ID one.  */
1120             GL(dl_tls_max_dtv_idx) = main_map->l_tls_modid = 1;
1121           }
1122         break;
1123
1124       case PT_GNU_STACK:
1125         GL(dl_stack_flags) = ph->p_flags;
1126         break;
1127
1128       case PT_GNU_RELRO:
1129         main_map->l_relro_addr = ph->p_vaddr;
1130         main_map->l_relro_size = ph->p_memsz;
1131         break;
1132       }
1133
1134   /* Adjust the address of the TLS initialization image in case
1135      the executable is actually an ET_DYN object.  */
1136   if (main_map->l_tls_initimage != NULL)
1137     main_map->l_tls_initimage
1138       = (char *) main_map->l_tls_initimage + main_map->l_addr;
1139   if (! main_map->l_map_end)
1140     main_map->l_map_end = ~0;
1141   if (! main_map->l_text_end)
1142     main_map->l_text_end = ~0;
1143   if (! GL(dl_rtld_map).l_libname && GL(dl_rtld_map).l_name)
1144     {
1145       /* We were invoked directly, so the program might not have a
1146          PT_INTERP.  */
1147       _dl_rtld_libname.name = GL(dl_rtld_map).l_name;
1148       /* _dl_rtld_libname.next = NULL;  Already zero.  */
1149       GL(dl_rtld_map).l_libname =  &_dl_rtld_libname;
1150     }
1151   else
1152     assert (GL(dl_rtld_map).l_libname); /* How else did we get here?  */
1153
1154   /* If the current libname is different from the SONAME, add the
1155      latter as well.  */
1156   if (GL(dl_rtld_map).l_info[DT_SONAME] != NULL
1157       && strcmp (GL(dl_rtld_map).l_libname->name,
1158                  (const char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB])
1159                  + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_val) != 0)
1160     {
1161       static struct libname_list newname;
1162       newname.name = ((char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB])
1163                       + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_ptr);
1164       newname.next = NULL;
1165       newname.dont_free = 1;
1166
1167       assert (GL(dl_rtld_map).l_libname->next == NULL);
1168       GL(dl_rtld_map).l_libname->next = &newname;
1169     }
1170   /* The ld.so must be relocated since otherwise loading audit modules
1171      will fail since they reuse the very same ld.so.  */
1172   assert (GL(dl_rtld_map).l_relocated);
1173
1174   if (! rtld_is_main)
1175     {
1176       /* Extract the contents of the dynamic section for easy access.  */
1177       elf_get_dynamic_info (main_map, NULL);
1178       /* Set up our cache of pointers into the hash table.  */
1179       _dl_setup_hash (main_map);
1180     }
1181
1182   if (__builtin_expect (mode, normal) == verify)
1183     {
1184       /* We were called just to verify that this is a dynamic
1185          executable using us as the program interpreter.  Exit with an
1186          error if we were not able to load the binary or no interpreter
1187          is specified (i.e., this is no dynamically linked binary.  */
1188       if (main_map->l_ld == NULL)
1189         _exit (1);
1190
1191       /* We allow here some platform specific code.  */
1192 #ifdef DISTINGUISH_LIB_VERSIONS
1193       DISTINGUISH_LIB_VERSIONS;
1194 #endif
1195       _exit (has_interp ? 0 : 2);
1196     }
1197
1198   struct link_map **first_preload = &GL(dl_rtld_map).l_next;
1199   /* Set up the data structures for the system-supplied DSO early,
1200      so they can influence _dl_init_paths.  */
1201   setup_vdso (main_map, &first_preload);
1202
1203 #ifdef DL_SYSDEP_OSCHECK
1204   DL_SYSDEP_OSCHECK (_dl_fatal_printf);
1205 #endif
1206
1207   /* Initialize the data structures for the search paths for shared
1208      objects.  */
1209   _dl_init_paths (library_path);
1210
1211   /* Initialize _r_debug.  */
1212   struct r_debug *r = _dl_debug_initialize (GL(dl_rtld_map).l_addr,
1213                                             LM_ID_BASE);
1214   r->r_state = RT_CONSISTENT;
1215
1216   /* Put the link_map for ourselves on the chain so it can be found by
1217      name.  Note that at this point the global chain of link maps contains
1218      exactly one element, which is pointed to by dl_loaded.  */
1219   if (! GL(dl_rtld_map).l_name)
1220     /* If not invoked directly, the dynamic linker shared object file was
1221        found by the PT_INTERP name.  */
1222     GL(dl_rtld_map).l_name = (char *) GL(dl_rtld_map).l_libname->name;
1223   GL(dl_rtld_map).l_type = lt_library;
1224   main_map->l_next = &GL(dl_rtld_map);
1225   GL(dl_rtld_map).l_prev = main_map;
1226   ++GL(dl_ns)[LM_ID_BASE]._ns_nloaded;
1227   ++GL(dl_load_adds);
1228
1229   /* If LD_USE_LOAD_BIAS env variable has not been seen, default
1230      to not using bias for non-prelinked PIEs and libraries
1231      and using it for executables or prelinked PIEs or libraries.  */
1232   if (GLRO(dl_use_load_bias) == (ElfW(Addr)) -2)
1233     GLRO(dl_use_load_bias) = main_map->l_addr == 0 ? -1 : 0;
1234
1235   /* Set up the program header information for the dynamic linker
1236      itself.  It is needed in the dl_iterate_phdr callbacks.  */
1237   const ElfW(Ehdr) *rtld_ehdr;
1238
1239   /* Starting from binutils-2.23, the linker will define the magic symbol
1240      __ehdr_start to point to our own ELF header if it is visible in a
1241      segment that also includes the phdrs.  If that's not available, we use
1242      the old method that assumes the beginning of the file is part of the
1243      lowest-addressed PT_LOAD segment.  */
1244 #ifdef HAVE_EHDR_START
1245   extern const ElfW(Ehdr) __ehdr_start __attribute__ ((visibility ("hidden")));
1246   rtld_ehdr = &__ehdr_start;
1247 #else
1248   rtld_ehdr = (void *) GL(dl_rtld_map).l_map_start;
1249 #endif
1250   assert (rtld_ehdr->e_ehsize == sizeof *rtld_ehdr);
1251   assert (rtld_ehdr->e_phentsize == sizeof (ElfW(Phdr)));
1252
1253   const ElfW(Phdr) *rtld_phdr = (const void *) rtld_ehdr + rtld_ehdr->e_phoff;
1254
1255   GL(dl_rtld_map).l_phdr = rtld_phdr;
1256   GL(dl_rtld_map).l_phnum = rtld_ehdr->e_phnum;
1257
1258
1259   /* PT_GNU_RELRO is usually the last phdr.  */
1260   size_t cnt = rtld_ehdr->e_phnum;
1261   while (cnt-- > 0)
1262     if (rtld_phdr[cnt].p_type == PT_GNU_RELRO)
1263       {
1264         GL(dl_rtld_map).l_relro_addr = rtld_phdr[cnt].p_vaddr;
1265         GL(dl_rtld_map).l_relro_size = rtld_phdr[cnt].p_memsz;
1266         break;
1267       }
1268
1269   /* Add the dynamic linker to the TLS list if it also uses TLS.  */
1270   if (GL(dl_rtld_map).l_tls_blocksize != 0)
1271     /* Assign a module ID.  Do this before loading any audit modules.  */
1272     GL(dl_rtld_map).l_tls_modid = _dl_next_tls_modid ();
1273
1274   /* If we have auditing DSOs to load, do it now.  */
1275   if (__glibc_unlikely (audit_list != NULL))
1276     {
1277       /* Iterate over all entries in the list.  The order is important.  */
1278       struct audit_ifaces *last_audit = NULL;
1279       struct audit_list *al = audit_list->next;
1280
1281       /* Since we start using the auditing DSOs right away we need to
1282          initialize the data structures now.  */
1283       tcbp = init_tls ();
1284
1285       /* Initialize security features.  We need to do it this early
1286          since otherwise the constructors of the audit libraries will
1287          use different values (especially the pointer guard) and will
1288          fail later on.  */
1289       security_init ();
1290
1291       do
1292         {
1293           int tls_idx = GL(dl_tls_max_dtv_idx);
1294
1295           /* Now it is time to determine the layout of the static TLS
1296              block and allocate it for the initial thread.  Note that we
1297              always allocate the static block, we never defer it even if
1298              no DF_STATIC_TLS bit is set.  The reason is that we know
1299              glibc will use the static model.  */
1300           struct dlmopen_args dlmargs;
1301           dlmargs.fname = al->name;
1302           dlmargs.map = NULL;
1303
1304           const char *objname;
1305           const char *err_str = NULL;
1306           bool malloced;
1307           (void) _dl_catch_error (&objname, &err_str, &malloced, dlmopen_doit,
1308                                   &dlmargs);
1309           if (__glibc_unlikely (err_str != NULL))
1310             {
1311             not_loaded:
1312               _dl_error_printf ("\
1313 ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n",
1314                                 al->name, err_str);
1315               if (malloced)
1316                 free ((char *) err_str);
1317             }
1318           else
1319             {
1320               struct lookup_args largs;
1321               largs.name = "la_version";
1322               largs.map = dlmargs.map;
1323
1324               /* Check whether the interface version matches.  */
1325               (void) _dl_catch_error (&objname, &err_str, &malloced,
1326                                       lookup_doit, &largs);
1327
1328               unsigned int (*laversion) (unsigned int);
1329               unsigned int lav;
1330               if  (err_str == NULL
1331                    && (laversion = largs.result) != NULL
1332                    && (lav = laversion (LAV_CURRENT)) > 0
1333                    && lav <= LAV_CURRENT)
1334                 {
1335                   /* Allocate structure for the callback function pointers.
1336                      This call can never fail.  */
1337                   union
1338                   {
1339                     struct audit_ifaces ifaces;
1340 #define naudit_ifaces 8
1341                     void (*fptr[naudit_ifaces]) (void);
1342                   } *newp = malloc (sizeof (*newp));
1343
1344                   /* Names of the auditing interfaces.  All in one
1345                      long string.  */
1346                   static const char audit_iface_names[] =
1347                     "la_activity\0"
1348                     "la_objsearch\0"
1349                     "la_objopen\0"
1350                     "la_preinit\0"
1351 #if __ELF_NATIVE_CLASS == 32
1352                     "la_symbind32\0"
1353 #elif __ELF_NATIVE_CLASS == 64
1354                     "la_symbind64\0"
1355 #else
1356 # error "__ELF_NATIVE_CLASS must be defined"
1357 #endif
1358 #define STRING(s) __STRING (s)
1359                     "la_" STRING (ARCH_LA_PLTENTER) "\0"
1360                     "la_" STRING (ARCH_LA_PLTEXIT) "\0"
1361                     "la_objclose\0";
1362                   unsigned int cnt = 0;
1363                   const char *cp = audit_iface_names;
1364                   do
1365                     {
1366                       largs.name = cp;
1367                       (void) _dl_catch_error (&objname, &err_str, &malloced,
1368                                               lookup_doit, &largs);
1369
1370                       /* Store the pointer.  */
1371                       if (err_str == NULL && largs.result != NULL)
1372                         {
1373                           newp->fptr[cnt] = largs.result;
1374
1375                           /* The dynamic linker link map is statically
1376                              allocated, initialize the data now.   */
1377                           GL(dl_rtld_map).l_audit[cnt].cookie
1378                             = (intptr_t) &GL(dl_rtld_map);
1379                         }
1380                       else
1381                         newp->fptr[cnt] = NULL;
1382                       ++cnt;
1383
1384                       cp = (char *) rawmemchr (cp, '\0') + 1;
1385                     }
1386                   while (*cp != '\0');
1387                   assert (cnt == naudit_ifaces);
1388
1389                   /* Now append the new auditing interface to the list.  */
1390                   newp->ifaces.next = NULL;
1391                   if (last_audit == NULL)
1392                     last_audit = GLRO(dl_audit) = &newp->ifaces;
1393                   else
1394                     last_audit = last_audit->next = &newp->ifaces;
1395                   ++GLRO(dl_naudit);
1396
1397                   /* Mark the DSO as being used for auditing.  */
1398                   dlmargs.map->l_auditing = 1;
1399                 }
1400               else
1401                 {
1402                   /* We cannot use the DSO, it does not have the
1403                      appropriate interfaces or it expects something
1404                      more recent.  */
1405 #ifndef NDEBUG
1406                   Lmid_t ns = dlmargs.map->l_ns;
1407 #endif
1408                   _dl_close (dlmargs.map);
1409
1410                   /* Make sure the namespace has been cleared entirely.  */
1411                   assert (GL(dl_ns)[ns]._ns_loaded == NULL);
1412                   assert (GL(dl_ns)[ns]._ns_nloaded == 0);
1413
1414                   GL(dl_tls_max_dtv_idx) = tls_idx;
1415                   goto not_loaded;
1416                 }
1417             }
1418
1419           al = al->next;
1420         }
1421       while (al != audit_list->next);
1422
1423       /* If we have any auditing modules, announce that we already
1424          have two objects loaded.  */
1425       if (__glibc_unlikely (GLRO(dl_naudit) > 0))
1426         {
1427           struct link_map *ls[2] = { main_map, &GL(dl_rtld_map) };
1428
1429           for (unsigned int outer = 0; outer < 2; ++outer)
1430             {
1431               struct audit_ifaces *afct = GLRO(dl_audit);
1432               for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1433                 {
1434                   if (afct->objopen != NULL)
1435                     {
1436                       ls[outer]->l_audit[cnt].bindflags
1437                         = afct->objopen (ls[outer], LM_ID_BASE,
1438                                          &ls[outer]->l_audit[cnt].cookie);
1439
1440                       ls[outer]->l_audit_any_plt
1441                         |= ls[outer]->l_audit[cnt].bindflags != 0;
1442                     }
1443
1444                   afct = afct->next;
1445                 }
1446             }
1447         }
1448     }
1449
1450   /* Keep track of the currently loaded modules to count how many
1451      non-audit modules which use TLS are loaded.  */
1452   size_t count_modids = _dl_count_modids ();
1453
1454   /* Set up debugging before the debugger is notified for the first time.  */
1455 #ifdef ELF_MACHINE_DEBUG_SETUP
1456   /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way.  */
1457   ELF_MACHINE_DEBUG_SETUP (main_map, r);
1458   ELF_MACHINE_DEBUG_SETUP (&GL(dl_rtld_map), r);
1459 #else
1460   if (main_map->l_info[DT_DEBUG] != NULL)
1461     /* There is a DT_DEBUG entry in the dynamic section.  Fill it in
1462        with the run-time address of the r_debug structure  */
1463     main_map->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1464
1465   /* Fill in the pointer in the dynamic linker's own dynamic section, in
1466      case you run gdb on the dynamic linker directly.  */
1467   if (GL(dl_rtld_map).l_info[DT_DEBUG] != NULL)
1468     GL(dl_rtld_map).l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
1469 #endif
1470
1471   /* We start adding objects.  */
1472   r->r_state = RT_ADD;
1473   _dl_debug_state ();
1474   LIBC_PROBE (init_start, 2, LM_ID_BASE, r);
1475
1476   /* Auditing checkpoint: we are ready to signal that the initial map
1477      is being constructed.  */
1478   if (__glibc_unlikely (GLRO(dl_naudit) > 0))
1479     {
1480       struct audit_ifaces *afct = GLRO(dl_audit);
1481       for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
1482         {
1483           if (afct->activity != NULL)
1484             afct->activity (&main_map->l_audit[cnt].cookie, LA_ACT_ADD);
1485
1486           afct = afct->next;
1487         }
1488     }
1489
1490   /* We have two ways to specify objects to preload: via environment
1491      variable and via the file /etc/ld.so.preload.  The latter can also
1492      be used when security is enabled.  */
1493   assert (*first_preload == NULL);
1494   struct link_map **preloads = NULL;
1495   unsigned int npreloads = 0;
1496
1497   if (__glibc_unlikely (preloadlist != NULL))
1498     {
1499       /* The LD_PRELOAD environment variable gives list of libraries
1500          separated by white space or colons that are loaded before the
1501          executable's dependencies and prepended to the global scope
1502          list.  If the binary is running setuid all elements
1503          containing a '/' are ignored since it is insecure.  */
1504       char *list = strdupa (preloadlist);
1505       char *p;
1506
1507       HP_TIMING_NOW (start);
1508
1509       /* Prevent optimizing strsep.  Speed is not important here.  */
1510       while ((p = (strsep) (&list, " :")) != NULL)
1511         if (p[0] != '\0'
1512             && (__builtin_expect (! INTUSE(__libc_enable_secure), 1)
1513                 || strchr (p, '/') == NULL))
1514           npreloads += do_preload (p, main_map, "LD_PRELOAD");
1515
1516       HP_TIMING_NOW (stop);
1517       HP_TIMING_DIFF (diff, start, stop);
1518       HP_TIMING_ACCUM_NT (load_time, diff);
1519     }
1520
1521   /* There usually is no ld.so.preload file, it should only be used
1522      for emergencies and testing.  So the open call etc should usually
1523      fail.  Using access() on a non-existing file is faster than using
1524      open().  So we do this first.  If it succeeds we do almost twice
1525      the work but this does not matter, since it is not for production
1526      use.  */
1527   static const char preload_file[] = "/etc/ld.so.preload";
1528   if (__glibc_unlikely (__access (preload_file, R_OK) == 0))
1529     {
1530       /* Read the contents of the file.  */
1531       file = _dl_sysdep_read_whole_file (preload_file, &file_size,
1532                                          PROT_READ | PROT_WRITE);
1533       if (__glibc_unlikely (file != MAP_FAILED))
1534         {
1535           /* Parse the file.  It contains names of libraries to be loaded,
1536              separated by white spaces or `:'.  It may also contain
1537              comments introduced by `#'.  */
1538           char *problem;
1539           char *runp;
1540           size_t rest;
1541
1542           /* Eliminate comments.  */
1543           runp = file;
1544           rest = file_size;
1545           while (rest > 0)
1546             {
1547               char *comment = memchr (runp, '#', rest);
1548               if (comment == NULL)
1549                 break;
1550
1551               rest -= comment - runp;
1552               do
1553                 *comment = ' ';
1554               while (--rest > 0 && *++comment != '\n');
1555             }
1556
1557           /* We have one problematic case: if we have a name at the end of
1558              the file without a trailing terminating characters, we cannot
1559              place the \0.  Handle the case separately.  */
1560           if (file[file_size - 1] != ' ' && file[file_size - 1] != '\t'
1561               && file[file_size - 1] != '\n' && file[file_size - 1] != ':')
1562             {
1563               problem = &file[file_size];
1564               while (problem > file && problem[-1] != ' '
1565                      && problem[-1] != '\t'
1566                      && problem[-1] != '\n' && problem[-1] != ':')
1567                 --problem;
1568
1569               if (problem > file)
1570                 problem[-1] = '\0';
1571             }
1572           else
1573             {
1574               problem = NULL;
1575               file[file_size - 1] = '\0';
1576             }
1577
1578           HP_TIMING_NOW (start);
1579
1580           if (file != problem)
1581             {
1582               char *p;
1583               runp = file;
1584               while ((p = strsep (&runp, ": \t\n")) != NULL)
1585                 if (p[0] != '\0')
1586                   npreloads += do_preload (p, main_map, preload_file);
1587             }
1588
1589           if (problem != NULL)
1590             {
1591               char *p = strndupa (problem, file_size - (problem - file));
1592
1593               npreloads += do_preload (p, main_map, preload_file);
1594             }
1595
1596           HP_TIMING_NOW (stop);
1597           HP_TIMING_DIFF (diff, start, stop);
1598           HP_TIMING_ACCUM_NT (load_time, diff);
1599
1600           /* We don't need the file anymore.  */
1601           __munmap (file, file_size);
1602         }
1603     }
1604
1605   if (__glibc_unlikely (*first_preload != NULL))
1606     {
1607       /* Set up PRELOADS with a vector of the preloaded libraries.  */
1608       struct link_map *l = *first_preload;
1609       preloads = __alloca (npreloads * sizeof preloads[0]);
1610       i = 0;
1611       do
1612         {
1613           preloads[i++] = l;
1614           l = l->l_next;
1615         } while (l);
1616       assert (i == npreloads);
1617     }
1618
1619   /* Load all the libraries specified by DT_NEEDED entries.  If LD_PRELOAD
1620      specified some libraries to load, these are inserted before the actual
1621      dependencies in the executable's searchlist for symbol resolution.  */
1622   HP_TIMING_NOW (start);
1623   _dl_map_object_deps (main_map, preloads, npreloads, mode == trace, 0);
1624   HP_TIMING_NOW (stop);
1625   HP_TIMING_DIFF (diff, start, stop);
1626   HP_TIMING_ACCUM_NT (load_time, diff);
1627
1628   /* Mark all objects as being in the global scope.  */
1629   for (i = main_map->l_searchlist.r_nlist; i > 0; )
1630     main_map->l_searchlist.r_list[--i]->l_global = 1;
1631
1632   /* Remove _dl_rtld_map from the chain.  */
1633   GL(dl_rtld_map).l_prev->l_next = GL(dl_rtld_map).l_next;
1634   if (GL(dl_rtld_map).l_next != NULL)
1635     GL(dl_rtld_map).l_next->l_prev = GL(dl_rtld_map).l_prev;
1636
1637   for (i = 1; i < main_map->l_searchlist.r_nlist; ++i)
1638     if (main_map->l_searchlist.r_list[i] == &GL(dl_rtld_map))
1639       break;
1640
1641   bool rtld_multiple_ref = false;
1642   if (__glibc_likely (i < main_map->l_searchlist.r_nlist))
1643     {
1644       /* Some DT_NEEDED entry referred to the interpreter object itself, so
1645          put it back in the list of visible objects.  We insert it into the
1646          chain in symbol search order because gdb uses the chain's order as
1647          its symbol search order.  */
1648       rtld_multiple_ref = true;
1649
1650       GL(dl_rtld_map).l_prev = main_map->l_searchlist.r_list[i - 1];
1651       if (__builtin_expect (mode, normal) == normal)
1652         {
1653           GL(dl_rtld_map).l_next = (i + 1 < main_map->l_searchlist.r_nlist
1654                                     ? main_map->l_searchlist.r_list[i + 1]
1655                                     : NULL);
1656 #ifdef NEED_DL_SYSINFO_DSO
1657           if (GLRO(dl_sysinfo_map) != NULL
1658               && GL(dl_rtld_map).l_prev->l_next == GLRO(dl_sysinfo_map)
1659               && GL(dl_rtld_map).l_next != GLRO(dl_sysinfo_map))
1660             GL(dl_rtld_map).l_prev = GLRO(dl_sysinfo_map);
1661 #endif
1662         }
1663       else
1664         /* In trace mode there might be an invisible object (which we
1665            could not find) after the previous one in the search list.
1666            In this case it doesn't matter much where we put the
1667            interpreter object, so we just initialize the list pointer so
1668            that the assertion below holds.  */
1669         GL(dl_rtld_map).l_next = GL(dl_rtld_map).l_prev->l_next;
1670
1671       assert (GL(dl_rtld_map).l_prev->l_next == GL(dl_rtld_map).l_next);
1672       GL(dl_rtld_map).l_prev->l_next = &GL(dl_rtld_map);
1673       if (GL(dl_rtld_map).l_next != NULL)
1674         {
1675           assert (GL(dl_rtld_map).l_next->l_prev == GL(dl_rtld_map).l_prev);
1676           GL(dl_rtld_map).l_next->l_prev = &GL(dl_rtld_map);
1677         }
1678     }
1679
1680   /* Now let us see whether all libraries are available in the
1681      versions we need.  */
1682   {
1683     struct version_check_args args;
1684     args.doexit = mode == normal;
1685     args.dotrace = mode == trace;
1686     _dl_receive_error (print_missing_version, version_check_doit, &args);
1687   }
1688
1689   /* We do not initialize any of the TLS functionality unless any of the
1690      initial modules uses TLS.  This makes dynamic loading of modules with
1691      TLS impossible, but to support it requires either eagerly doing setup
1692      now or lazily doing it later.  Doing it now makes us incompatible with
1693      an old kernel that can't perform TLS_INIT_TP, even if no TLS is ever
1694      used.  Trying to do it lazily is too hairy to try when there could be
1695      multiple threads (from a non-TLS-using libpthread).  */
1696   bool was_tls_init_tp_called = tls_init_tp_called;
1697   if (tcbp == NULL)
1698     tcbp = init_tls ();
1699
1700   if (__glibc_likely (audit_list == NULL))
1701     /* Initialize security features.  But only if we have not done it
1702        earlier.  */
1703     security_init ();
1704
1705   if (__builtin_expect (mode, normal) != normal)
1706     {
1707       /* We were run just to list the shared libraries.  It is
1708          important that we do this before real relocation, because the
1709          functions we call below for output may no longer work properly
1710          after relocation.  */
1711       struct link_map *l;
1712
1713       if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
1714         {
1715           struct r_scope_elem *scope = &main_map->l_searchlist;
1716
1717           for (i = 0; i < scope->r_nlist; i++)
1718             {
1719               l = scope->r_list [i];
1720               if (l->l_faked)
1721                 {
1722                   _dl_printf ("\t%s => not found\n", l->l_libname->name);
1723                   continue;
1724                 }
1725               if (_dl_name_match_p (GLRO(dl_trace_prelink), l))
1726                 GLRO(dl_trace_prelink_map) = l;
1727               _dl_printf ("\t%s => %s (0x%0*Zx, 0x%0*Zx)",
1728                           DSO_FILENAME (l->l_libname->name),
1729                           DSO_FILENAME (l->l_name),
1730                           (int) sizeof l->l_map_start * 2,
1731                           (size_t) l->l_map_start,
1732                           (int) sizeof l->l_addr * 2,
1733                           (size_t) l->l_addr);
1734
1735               if (l->l_tls_modid)
1736                 _dl_printf (" TLS(0x%Zx, 0x%0*Zx)\n", l->l_tls_modid,
1737                             (int) sizeof l->l_tls_offset * 2,
1738                             (size_t) l->l_tls_offset);
1739               else
1740                 _dl_printf ("\n");
1741             }
1742         }
1743       else if (GLRO(dl_debug_mask) & DL_DEBUG_UNUSED)
1744         {
1745           /* Look through the dependencies of the main executable
1746              and determine which of them is not actually
1747              required.  */
1748           struct link_map *l = main_map;
1749
1750           /* Relocate the main executable.  */
1751           struct relocate_args args = { .l = l,
1752                                         .reloc_mode = ((GLRO(dl_lazy)
1753                                                        ? RTLD_LAZY : 0)
1754                                                        | __RTLD_NOIFUNC) };
1755           _dl_receive_error (print_unresolved, relocate_doit, &args);
1756
1757           /* This loop depends on the dependencies of the executable to
1758              correspond in number and order to the DT_NEEDED entries.  */
1759           ElfW(Dyn) *dyn = main_map->l_ld;
1760           bool first = true;
1761           while (dyn->d_tag != DT_NULL)
1762             {
1763               if (dyn->d_tag == DT_NEEDED)
1764                 {
1765                   l = l->l_next;
1766 #ifdef NEED_DL_SYSINFO_DSO
1767                   /* Skip the VDSO since it's not part of the list
1768                      of objects we brought in via DT_NEEDED entries.  */
1769                   if (l == GLRO(dl_sysinfo_map))
1770                     l = l->l_next;
1771 #endif
1772                   if (!l->l_used)
1773                     {
1774                       if (first)
1775                         {
1776                           _dl_printf ("Unused direct dependencies:\n");
1777                           first = false;
1778                         }
1779
1780                       _dl_printf ("\t%s\n", l->l_name);
1781                     }
1782                 }
1783
1784               ++dyn;
1785             }
1786
1787           _exit (first != true);
1788         }
1789       else if (! main_map->l_info[DT_NEEDED])
1790         _dl_printf ("\tstatically linked\n");
1791       else
1792         {
1793           for (l = main_map->l_next; l; l = l->l_next)
1794             if (l->l_faked)
1795               /* The library was not found.  */
1796               _dl_printf ("\t%s => not found\n", l->l_libname->name);
1797             else if (strcmp (l->l_libname->name, l->l_name) == 0)
1798               _dl_printf ("\t%s (0x%0*Zx)\n", l->l_libname->name,
1799                           (int) sizeof l->l_map_start * 2,
1800                           (size_t) l->l_map_start);
1801             else
1802               _dl_printf ("\t%s => %s (0x%0*Zx)\n", l->l_libname->name,
1803                           l->l_name, (int) sizeof l->l_map_start * 2,
1804                           (size_t) l->l_map_start);
1805         }
1806
1807       if (__builtin_expect (mode, trace) != trace)
1808         for (i = 1; i < (unsigned int) _dl_argc; ++i)
1809           {
1810             const ElfW(Sym) *ref = NULL;
1811             ElfW(Addr) loadbase;
1812             lookup_t result;
1813
1814             result = _dl_lookup_symbol_x (INTUSE(_dl_argv)[i], main_map,
1815                                           &ref, main_map->l_scope,
1816                                           NULL, ELF_RTYPE_CLASS_PLT,
1817                                           DL_LOOKUP_ADD_DEPENDENCY, NULL);
1818
1819             loadbase = LOOKUP_VALUE_ADDRESS (result);
1820
1821             _dl_printf ("%s found at 0x%0*Zd in object at 0x%0*Zd\n",
1822                         INTUSE(_dl_argv)[i],
1823                         (int) sizeof ref->st_value * 2,
1824                         (size_t) ref->st_value,
1825                         (int) sizeof loadbase * 2, (size_t) loadbase);
1826           }
1827       else
1828         {
1829           /* If LD_WARN is set, warn about undefined symbols.  */
1830           if (GLRO(dl_lazy) >= 0 && GLRO(dl_verbose))
1831             {
1832               /* We have to do symbol dependency testing.  */
1833               struct relocate_args args;
1834               unsigned int i;
1835
1836               args.reloc_mode = ((GLRO(dl_lazy) ? RTLD_LAZY : 0)
1837                                  | __RTLD_NOIFUNC);
1838
1839               i = main_map->l_searchlist.r_nlist;
1840               while (i-- > 0)
1841                 {
1842                   struct link_map *l = main_map->l_initfini[i];
1843                   if (l != &GL(dl_rtld_map) && ! l->l_faked)
1844                     {
1845                       args.l = l;
1846                       _dl_receive_error (print_unresolved, relocate_doit,
1847                                          &args);
1848                     }
1849                 }
1850
1851               if ((GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
1852                   && rtld_multiple_ref)
1853                 {
1854                   /* Mark the link map as not yet relocated again.  */
1855                   GL(dl_rtld_map).l_relocated = 0;
1856                   _dl_relocate_object (&GL(dl_rtld_map),
1857                                        main_map->l_scope, __RTLD_NOIFUNC, 0);
1858                 }
1859             }
1860 #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
1861           if (version_info)
1862             {
1863               /* Print more information.  This means here, print information
1864                  about the versions needed.  */
1865               int first = 1;
1866               struct link_map *map;
1867
1868               for (map = main_map; map != NULL; map = map->l_next)
1869                 {
1870                   const char *strtab;
1871                   ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG];
1872                   ElfW(Verneed) *ent;
1873
1874                   if (dyn == NULL)
1875                     continue;
1876
1877                   strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
1878                   ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
1879
1880                   if (first)
1881                     {
1882                       _dl_printf ("\n\tVersion information:\n");
1883                       first = 0;
1884                     }
1885
1886                   _dl_printf ("\t%s:\n", DSO_FILENAME (map->l_name));
1887
1888                   while (1)
1889                     {
1890                       ElfW(Vernaux) *aux;
1891                       struct link_map *needed;
1892
1893                       needed = find_needed (strtab + ent->vn_file);
1894                       aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
1895
1896                       while (1)
1897                         {
1898                           const char *fname = NULL;
1899
1900                           if (needed != NULL
1901                               && match_version (strtab + aux->vna_name,
1902                                                 needed))
1903                             fname = needed->l_name;
1904
1905                           _dl_printf ("\t\t%s (%s) %s=> %s\n",
1906                                       strtab + ent->vn_file,
1907                                       strtab + aux->vna_name,
1908                                       aux->vna_flags & VER_FLG_WEAK
1909                                       ? "[WEAK] " : "",
1910                                       fname ?: "not found");
1911
1912                           if (aux->vna_next == 0)
1913                             /* No more symbols.  */
1914                             break;
1915
1916                           /* Next symbol.  */
1917                           aux = (ElfW(Vernaux) *) ((char *) aux
1918                                                    + aux->vna_next);
1919                         }
1920
1921                       if (ent->vn_next == 0)
1922                         /* No more dependencies.  */
1923                         break;
1924
1925                       /* Next dependency.  */
1926                       ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
1927                     }
1928                 }
1929             }
1930         }
1931
1932       _exit (0);
1933     }
1934
1935   if (main_map->l_info[ADDRIDX (DT_GNU_LIBLIST)]
1936       && ! __builtin_expect (GLRO(dl_profile) != NULL, 0)
1937       && ! __builtin_expect (GLRO(dl_dynamic_weak), 0))
1938     {
1939       ElfW(Lib) *liblist, *liblistend;
1940       struct link_map **r_list, **r_listend, *l;
1941       const char *strtab = (const void *) D_PTR (main_map, l_info[DT_STRTAB]);
1942
1943       assert (main_map->l_info[VALIDX (DT_GNU_LIBLISTSZ)] != NULL);
1944       liblist = (ElfW(Lib) *)
1945                 main_map->l_info[ADDRIDX (DT_GNU_LIBLIST)]->d_un.d_ptr;
1946       liblistend = (ElfW(Lib) *)
1947                    ((char *) liblist +
1948                     main_map->l_info[VALIDX (DT_GNU_LIBLISTSZ)]->d_un.d_val);
1949       r_list = main_map->l_searchlist.r_list;
1950       r_listend = r_list + main_map->l_searchlist.r_nlist;
1951
1952       for (; r_list < r_listend && liblist < liblistend; r_list++)
1953         {
1954           l = *r_list;
1955
1956           if (l == main_map)
1957             continue;
1958
1959           /* If the library is not mapped where it should, fail.  */
1960           if (l->l_addr)
1961             break;
1962
1963           /* Next, check if checksum matches.  */
1964           if (l->l_info [VALIDX(DT_CHECKSUM)] == NULL
1965               || l->l_info [VALIDX(DT_CHECKSUM)]->d_un.d_val
1966                  != liblist->l_checksum)
1967             break;
1968
1969           if (l->l_info [VALIDX(DT_GNU_PRELINKED)] == NULL
1970               || l->l_info [VALIDX(DT_GNU_PRELINKED)]->d_un.d_val
1971                  != liblist->l_time_stamp)
1972             break;
1973
1974           if (! _dl_name_match_p (strtab + liblist->l_name, l))
1975             break;
1976
1977           ++liblist;
1978         }
1979
1980
1981       if (r_list == r_listend && liblist == liblistend)
1982         prelinked = true;
1983
1984       if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS))
1985         _dl_debug_printf ("\nprelink checking: %s\n",
1986                           prelinked ? "ok" : "failed");
1987     }
1988
1989
1990   /* Now set up the variable which helps the assembler startup code.  */
1991   GL(dl_ns)[LM_ID_BASE]._ns_main_searchlist = &main_map->l_searchlist;
1992
1993   /* Save the information about the original global scope list since
1994      we need it in the memory handling later.  */
1995   GLRO(dl_initial_searchlist) = *GL(dl_ns)[LM_ID_BASE]._ns_main_searchlist;
1996
1997   /* Remember the last search directory added at startup, now that
1998      malloc will no longer be the one from dl-minimal.c.  */
1999   GLRO(dl_init_all_dirs) = GL(dl_all_dirs);
2000
2001   /* Print scope information.  */
2002   if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_SCOPES))
2003     {
2004       _dl_debug_printf ("\nInitial object scopes\n");
2005
2006       for (struct link_map *l = main_map; l != NULL; l = l->l_next)
2007         _dl_show_scope (l, 0);
2008     }
2009
2010   if (prelinked)
2011     {
2012       if (main_map->l_info [ADDRIDX (DT_GNU_CONFLICT)] != NULL)
2013         {
2014           ElfW(Rela) *conflict, *conflictend;
2015 #ifndef HP_TIMING_NONAVAIL
2016           hp_timing_t start;
2017           hp_timing_t stop;
2018 #endif
2019
2020           HP_TIMING_NOW (start);
2021           assert (main_map->l_info [VALIDX (DT_GNU_CONFLICTSZ)] != NULL);
2022           conflict = (ElfW(Rela) *)
2023             main_map->l_info [ADDRIDX (DT_GNU_CONFLICT)]->d_un.d_ptr;
2024           conflictend = (ElfW(Rela) *)
2025             ((char *) conflict
2026              + main_map->l_info [VALIDX (DT_GNU_CONFLICTSZ)]->d_un.d_val);
2027           _dl_resolve_conflicts (main_map, conflict, conflictend);
2028           HP_TIMING_NOW (stop);
2029           HP_TIMING_DIFF (relocate_time, start, stop);
2030         }
2031
2032
2033       /* Mark all the objects so we know they have been already relocated.  */
2034       for (struct link_map *l = main_map; l != NULL; l = l->l_next)
2035         {
2036           l->l_relocated = 1;
2037           if (l->l_relro_size)
2038             _dl_protect_relro (l);
2039
2040           /* Add object to slot information data if necessasy.  */
2041           if (l->l_tls_blocksize != 0 && tls_init_tp_called)
2042             _dl_add_to_slotinfo (l);
2043         }
2044     }
2045   else
2046     {
2047       /* Now we have all the objects loaded.  Relocate them all except for
2048          the dynamic linker itself.  We do this in reverse order so that copy
2049          relocs of earlier objects overwrite the data written by later
2050          objects.  We do not re-relocate the dynamic linker itself in this
2051          loop because that could result in the GOT entries for functions we
2052          call being changed, and that would break us.  It is safe to relocate
2053          the dynamic linker out of order because it has no copy relocs (we
2054          know that because it is self-contained).  */
2055
2056       int consider_profiling = GLRO(dl_profile) != NULL;
2057 #ifndef HP_TIMING_NONAVAIL
2058       hp_timing_t start;
2059       hp_timing_t stop;
2060 #endif
2061
2062       /* If we are profiling we also must do lazy reloaction.  */
2063       GLRO(dl_lazy) |= consider_profiling;
2064
2065       HP_TIMING_NOW (start);
2066       unsigned i = main_map->l_searchlist.r_nlist;
2067       while (i-- > 0)
2068         {
2069           struct link_map *l = main_map->l_initfini[i];
2070
2071           /* While we are at it, help the memory handling a bit.  We have to
2072              mark some data structures as allocated with the fake malloc()
2073              implementation in ld.so.  */
2074           struct libname_list *lnp = l->l_libname->next;
2075
2076           while (__builtin_expect (lnp != NULL, 0))
2077             {
2078               lnp->dont_free = 1;
2079               lnp = lnp->next;
2080             }
2081           /* Also allocated with the fake malloc().  */
2082           l->l_free_initfini = 0;
2083
2084           if (l != &GL(dl_rtld_map))
2085             _dl_relocate_object (l, l->l_scope, GLRO(dl_lazy) ? RTLD_LAZY : 0,
2086                                  consider_profiling);
2087
2088           /* Add object to slot information data if necessasy.  */
2089           if (l->l_tls_blocksize != 0 && tls_init_tp_called)
2090             _dl_add_to_slotinfo (l);
2091         }
2092       HP_TIMING_NOW (stop);
2093
2094       HP_TIMING_DIFF (relocate_time, start, stop);
2095
2096       /* Now enable profiling if needed.  Like the previous call,
2097          this has to go here because the calls it makes should use the
2098          rtld versions of the functions (particularly calloc()), but it
2099          needs to have _dl_profile_map set up by the relocator.  */
2100       if (__glibc_unlikely (GL(dl_profile_map) != NULL))
2101         /* We must prepare the profiling.  */
2102         _dl_start_profile ();
2103     }
2104
2105   if ((!was_tls_init_tp_called && GL(dl_tls_max_dtv_idx) > 0)
2106       || count_modids != _dl_count_modids ())
2107     ++GL(dl_tls_generation);
2108
2109   /* Now that we have completed relocation, the initializer data
2110      for the TLS blocks has its final values and we can copy them
2111      into the main thread's TLS area, which we allocated above.  */
2112   _dl_allocate_tls_init (tcbp);
2113
2114   /* And finally install it for the main thread.  */
2115   if (! tls_init_tp_called)
2116     {
2117       const char *lossage = TLS_INIT_TP (tcbp, 0);
2118       if (__glibc_unlikely (lossage != NULL))
2119         _dl_fatal_printf ("cannot set up thread-local storage: %s\n",
2120                           lossage);
2121     }
2122
2123   /* Make sure no new search directories have been added.  */
2124   assert (GLRO(dl_init_all_dirs) == GL(dl_all_dirs));
2125
2126   if (! prelinked && rtld_multiple_ref)
2127     {
2128       /* There was an explicit ref to the dynamic linker as a shared lib.
2129          Re-relocate ourselves with user-controlled symbol definitions.
2130
2131          We must do this after TLS initialization in case after this
2132          re-relocation, we might call a user-supplied function
2133          (e.g. calloc from _dl_relocate_object) that uses TLS data.  */
2134
2135 #ifndef HP_TIMING_NONAVAIL
2136       hp_timing_t start;
2137       hp_timing_t stop;
2138       hp_timing_t add;
2139 #endif
2140
2141       HP_TIMING_NOW (start);
2142       /* Mark the link map as not yet relocated again.  */
2143       GL(dl_rtld_map).l_relocated = 0;
2144       _dl_relocate_object (&GL(dl_rtld_map), main_map->l_scope, 0, 0);
2145       HP_TIMING_NOW (stop);
2146       HP_TIMING_DIFF (add, start, stop);
2147       HP_TIMING_ACCUM_NT (relocate_time, add);
2148     }
2149
2150   /* Do any necessary cleanups for the startup OS interface code.
2151      We do these now so that no calls are made after rtld re-relocation
2152      which might be resolved to different functions than we expect.
2153      We cannot do this before relocating the other objects because
2154      _dl_relocate_object might need to call `mprotect' for DT_TEXTREL.  */
2155   _dl_sysdep_start_cleanup ();
2156
2157 #ifdef SHARED
2158   /* Auditing checkpoint: we have added all objects.  */
2159   if (__glibc_unlikely (GLRO(dl_naudit) > 0))
2160     {
2161       struct link_map *head = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
2162       /* Do not call the functions for any auditing object.  */
2163       if (head->l_auditing == 0)
2164         {
2165           struct audit_ifaces *afct = GLRO(dl_audit);
2166           for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
2167             {
2168               if (afct->activity != NULL)
2169                 afct->activity (&head->l_audit[cnt].cookie, LA_ACT_CONSISTENT);
2170
2171               afct = afct->next;
2172             }
2173         }
2174     }
2175 #endif
2176
2177   /* Notify the debugger all new objects are now ready to go.  We must re-get
2178      the address since by now the variable might be in another object.  */
2179   r = _dl_debug_initialize (0, LM_ID_BASE);
2180   r->r_state = RT_CONSISTENT;
2181   _dl_debug_state ();
2182   LIBC_PROBE (init_complete, 2, LM_ID_BASE, r);
2183
2184 #if defined USE_LDCONFIG && !defined MAP_COPY
2185   /* We must munmap() the cache file.  */
2186   _dl_unload_cache ();
2187 #endif
2188
2189   /* Once we return, _dl_sysdep_start will invoke
2190      the DT_INIT functions and then *USER_ENTRY.  */
2191 }
2192 \f
2193 /* This is a little helper function for resolving symbols while
2194    tracing the binary.  */
2195 static void
2196 print_unresolved (int errcode __attribute__ ((unused)), const char *objname,
2197                   const char *errstring)
2198 {
2199   if (objname[0] == '\0')
2200     objname = RTLD_PROGNAME;
2201   _dl_error_printf ("%s (%s)\n", errstring, objname);
2202 }
2203 \f
2204 /* This is a little helper function for resolving symbols while
2205    tracing the binary.  */
2206 static void
2207 print_missing_version (int errcode __attribute__ ((unused)),
2208                        const char *objname, const char *errstring)
2209 {
2210   _dl_error_printf ("%s: %s: %s\n", RTLD_PROGNAME,
2211                     objname, errstring);
2212 }
2213 \f
2214 /* Nonzero if any of the debugging options is enabled.  */
2215 static int any_debug attribute_relro;
2216
2217 /* Process the string given as the parameter which explains which debugging
2218    options are enabled.  */
2219 static void
2220 process_dl_debug (const char *dl_debug)
2221 {
2222   /* When adding new entries make sure that the maximal length of a name
2223      is correctly handled in the LD_DEBUG_HELP code below.  */
2224   static const struct
2225   {
2226     unsigned char len;
2227     const char name[10];
2228     const char helptext[41];
2229     unsigned short int mask;
2230   } debopts[] =
2231     {
2232 #define LEN_AND_STR(str) sizeof (str) - 1, str
2233       { LEN_AND_STR ("libs"), "display library search paths",
2234         DL_DEBUG_LIBS | DL_DEBUG_IMPCALLS },
2235       { LEN_AND_STR ("reloc"), "display relocation processing",
2236         DL_DEBUG_RELOC | DL_DEBUG_IMPCALLS },
2237       { LEN_AND_STR ("files"), "display progress for input file",
2238         DL_DEBUG_FILES | DL_DEBUG_IMPCALLS },
2239       { LEN_AND_STR ("symbols"), "display symbol table processing",
2240         DL_DEBUG_SYMBOLS | DL_DEBUG_IMPCALLS },
2241       { LEN_AND_STR ("bindings"), "display information about symbol binding",
2242         DL_DEBUG_BINDINGS | DL_DEBUG_IMPCALLS },
2243       { LEN_AND_STR ("versions"), "display version dependencies",
2244         DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS },
2245       { LEN_AND_STR ("scopes"), "display scope information",
2246         DL_DEBUG_SCOPES },
2247       { LEN_AND_STR ("all"), "all previous options combined",
2248         DL_DEBUG_LIBS | DL_DEBUG_RELOC | DL_DEBUG_FILES | DL_DEBUG_SYMBOLS
2249         | DL_DEBUG_BINDINGS | DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS
2250         | DL_DEBUG_SCOPES },
2251       { LEN_AND_STR ("statistics"), "display relocation statistics",
2252         DL_DEBUG_STATISTICS },
2253       { LEN_AND_STR ("unused"), "determined unused DSOs",
2254         DL_DEBUG_UNUSED },
2255       { LEN_AND_STR ("help"), "display this help message and exit",
2256         DL_DEBUG_HELP },
2257     };
2258 #define ndebopts (sizeof (debopts) / sizeof (debopts[0]))
2259
2260   /* Skip separating white spaces and commas.  */
2261   while (*dl_debug != '\0')
2262     {
2263       if (*dl_debug != ' ' && *dl_debug != ',' && *dl_debug != ':')
2264         {
2265           size_t cnt;
2266           size_t len = 1;
2267
2268           while (dl_debug[len] != '\0' && dl_debug[len] != ' '
2269                  && dl_debug[len] != ',' && dl_debug[len] != ':')
2270             ++len;
2271
2272           for (cnt = 0; cnt < ndebopts; ++cnt)
2273             if (debopts[cnt].len == len
2274                 && memcmp (dl_debug, debopts[cnt].name, len) == 0)
2275               {
2276                 GLRO(dl_debug_mask) |= debopts[cnt].mask;
2277                 any_debug = 1;
2278                 break;
2279               }
2280
2281           if (cnt == ndebopts)
2282             {
2283               /* Display a warning and skip everything until next
2284                  separator.  */
2285               char *copy = strndupa (dl_debug, len);
2286               _dl_error_printf ("\
2287 warning: debug option `%s' unknown; try LD_DEBUG=help\n", copy);
2288             }
2289
2290           dl_debug += len;
2291           continue;
2292         }
2293
2294       ++dl_debug;
2295     }
2296
2297   if (GLRO(dl_debug_mask) & DL_DEBUG_UNUSED)
2298     {
2299       /* In order to get an accurate picture of whether a particular
2300          DT_NEEDED entry is actually used we have to process both
2301          the PLT and non-PLT relocation entries.  */
2302       GLRO(dl_lazy) = 0;
2303     }
2304
2305   if (GLRO(dl_debug_mask) & DL_DEBUG_HELP)
2306     {
2307       size_t cnt;
2308
2309       _dl_printf ("\
2310 Valid options for the LD_DEBUG environment variable are:\n\n");
2311
2312       for (cnt = 0; cnt < ndebopts; ++cnt)
2313         _dl_printf ("  %.*s%s%s\n", debopts[cnt].len, debopts[cnt].name,
2314                     "         " + debopts[cnt].len - 3,
2315                     debopts[cnt].helptext);
2316
2317       _dl_printf ("\n\
2318 To direct the debugging output into a file instead of standard output\n\
2319 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n");
2320       _exit (0);
2321     }
2322 }
2323 \f
2324 static void
2325 process_dl_audit (char *str)
2326 {
2327   /* The parameter is a colon separated list of DSO names.  */
2328   char *p;
2329
2330   while ((p = (strsep) (&str, ":")) != NULL)
2331     if (p[0] != '\0'
2332         && (__builtin_expect (! INTUSE(__libc_enable_secure), 1)
2333             || strchr (p, '/') == NULL))
2334       {
2335         /* This is using the local malloc, not the system malloc.  The
2336            memory can never be freed.  */
2337         struct audit_list *newp = malloc (sizeof (*newp));
2338         newp->name = p;
2339
2340         if (audit_list == NULL)
2341           audit_list = newp->next = newp;
2342         else
2343           {
2344             newp->next = audit_list->next;
2345             audit_list = audit_list->next = newp;
2346           }
2347       }
2348 }
2349 \f
2350 /* Process all environments variables the dynamic linker must recognize.
2351    Since all of them start with `LD_' we are a bit smarter while finding
2352    all the entries.  */
2353 extern char **_environ attribute_hidden;
2354
2355
2356 static void
2357 process_envvars (enum mode *modep)
2358 {
2359   char **runp = _environ;
2360   char *envline;
2361   enum mode mode = normal;
2362   char *debug_output = NULL;
2363
2364   /* This is the default place for profiling data file.  */
2365   GLRO(dl_profile_output)
2366     = &"/var/tmp\0/var/profile"[INTUSE(__libc_enable_secure) ? 9 : 0];
2367
2368   while ((envline = _dl_next_ld_env_entry (&runp)) != NULL)
2369     {
2370       size_t len = 0;
2371
2372       while (envline[len] != '\0' && envline[len] != '=')
2373         ++len;
2374
2375       if (envline[len] != '=')
2376         /* This is a "LD_" variable at the end of the string without
2377            a '=' character.  Ignore it since otherwise we will access
2378            invalid memory below.  */
2379         continue;
2380
2381       switch (len)
2382         {
2383         case 4:
2384           /* Warning level, verbose or not.  */
2385           if (memcmp (envline, "WARN", 4) == 0)
2386             GLRO(dl_verbose) = envline[5] != '\0';
2387           break;
2388
2389         case 5:
2390           /* Debugging of the dynamic linker?  */
2391           if (memcmp (envline, "DEBUG", 5) == 0)
2392             {
2393               process_dl_debug (&envline[6]);
2394               break;
2395             }
2396           if (memcmp (envline, "AUDIT", 5) == 0)
2397             process_dl_audit (&envline[6]);
2398           break;
2399
2400         case 7:
2401           /* Print information about versions.  */
2402           if (memcmp (envline, "VERBOSE", 7) == 0)
2403             {
2404               version_info = envline[8] != '\0';
2405               break;
2406             }
2407
2408           /* List of objects to be preloaded.  */
2409           if (memcmp (envline, "PRELOAD", 7) == 0)
2410             {
2411               preloadlist = &envline[8];
2412               break;
2413             }
2414
2415           /* Which shared object shall be profiled.  */
2416           if (memcmp (envline, "PROFILE", 7) == 0 && envline[8] != '\0')
2417             GLRO(dl_profile) = &envline[8];
2418           break;
2419
2420         case 8:
2421           /* Do we bind early?  */
2422           if (memcmp (envline, "BIND_NOW", 8) == 0)
2423             {
2424               GLRO(dl_lazy) = envline[9] == '\0';
2425               break;
2426             }
2427           if (memcmp (envline, "BIND_NOT", 8) == 0)
2428             GLRO(dl_bind_not) = envline[9] != '\0';
2429           break;
2430
2431         case 9:
2432           /* Test whether we want to see the content of the auxiliary
2433              array passed up from the kernel.  */
2434           if (!INTUSE(__libc_enable_secure)
2435               && memcmp (envline, "SHOW_AUXV", 9) == 0)
2436             _dl_show_auxv ();
2437           break;
2438
2439         case 10:
2440           /* Mask for the important hardware capabilities.  */
2441           if (memcmp (envline, "HWCAP_MASK", 10) == 0)
2442             GLRO(dl_hwcap_mask) = __strtoul_internal (&envline[11], NULL,
2443                                                       0, 0);
2444           break;
2445
2446         case 11:
2447           /* Path where the binary is found.  */
2448           if (!INTUSE(__libc_enable_secure)
2449               && memcmp (envline, "ORIGIN_PATH", 11) == 0)
2450             GLRO(dl_origin_path) = &envline[12];
2451           break;
2452
2453         case 12:
2454           /* The library search path.  */
2455           if (memcmp (envline, "LIBRARY_PATH", 12) == 0)
2456             {
2457               library_path = &envline[13];
2458               break;
2459             }
2460
2461           /* Where to place the profiling data file.  */
2462           if (memcmp (envline, "DEBUG_OUTPUT", 12) == 0)
2463             {
2464               debug_output = &envline[13];
2465               break;
2466             }
2467
2468           if (!INTUSE(__libc_enable_secure)
2469               && memcmp (envline, "DYNAMIC_WEAK", 12) == 0)
2470             GLRO(dl_dynamic_weak) = 1;
2471           break;
2472
2473         case 13:
2474           /* We might have some extra environment variable with length 13
2475              to handle.  */
2476 #ifdef EXTRA_LD_ENVVARS_13
2477           EXTRA_LD_ENVVARS_13
2478 #endif
2479           if (!INTUSE(__libc_enable_secure)
2480               && memcmp (envline, "USE_LOAD_BIAS", 13) == 0)
2481             {
2482               GLRO(dl_use_load_bias) = envline[14] == '1' ? -1 : 0;
2483               break;
2484             }
2485
2486           if (memcmp (envline, "POINTER_GUARD", 13) == 0)
2487             GLRO(dl_pointer_guard) = envline[14] != '0';
2488           break;
2489
2490         case 14:
2491           /* Where to place the profiling data file.  */
2492           if (!INTUSE(__libc_enable_secure)
2493               && memcmp (envline, "PROFILE_OUTPUT", 14) == 0
2494               && envline[15] != '\0')
2495             GLRO(dl_profile_output) = &envline[15];
2496           break;
2497
2498         case 16:
2499           /* The mode of the dynamic linker can be set.  */
2500           if (memcmp (envline, "TRACE_PRELINKING", 16) == 0)
2501             {
2502               mode = trace;
2503               GLRO(dl_verbose) = 1;
2504               GLRO(dl_debug_mask) |= DL_DEBUG_PRELINK;
2505               GLRO(dl_trace_prelink) = &envline[17];
2506             }
2507           break;
2508
2509         case 20:
2510           /* The mode of the dynamic linker can be set.  */
2511           if (memcmp (envline, "TRACE_LOADED_OBJECTS", 20) == 0)
2512             mode = trace;
2513           break;
2514
2515           /* We might have some extra environment variable to handle.  This
2516              is tricky due to the pre-processing of the length of the name
2517              in the switch statement here.  The code here assumes that added
2518              environment variables have a different length.  */
2519 #ifdef EXTRA_LD_ENVVARS
2520           EXTRA_LD_ENVVARS
2521 #endif
2522         }
2523     }
2524
2525   /* The caller wants this information.  */
2526   *modep = mode;
2527
2528   /* Extra security for SUID binaries.  Remove all dangerous environment
2529      variables.  */
2530   if (__builtin_expect (INTUSE(__libc_enable_secure), 0))
2531     {
2532       static const char unsecure_envvars[] =
2533 #ifdef EXTRA_UNSECURE_ENVVARS
2534         EXTRA_UNSECURE_ENVVARS
2535 #endif
2536         UNSECURE_ENVVARS;
2537       const char *nextp;
2538
2539       nextp = unsecure_envvars;
2540       do
2541         {
2542           unsetenv (nextp);
2543           /* We could use rawmemchr but this need not be fast.  */
2544           nextp = (char *) (strchr) (nextp, '\0') + 1;
2545         }
2546       while (*nextp != '\0');
2547
2548       if (__access ("/etc/suid-debug", F_OK) != 0)
2549         {
2550           unsetenv ("MALLOC_CHECK_");
2551           GLRO(dl_debug_mask) = 0;
2552         }
2553
2554       if (mode != normal)
2555         _exit (5);
2556     }
2557   /* If we have to run the dynamic linker in debugging mode and the
2558      LD_DEBUG_OUTPUT environment variable is given, we write the debug
2559      messages to this file.  */
2560   else if (any_debug && debug_output != NULL)
2561     {
2562 #ifdef O_NOFOLLOW
2563       const int flags = O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW;
2564 #else
2565       const int flags = O_WRONLY | O_APPEND | O_CREAT;
2566 #endif
2567       size_t name_len = strlen (debug_output);
2568       char buf[name_len + 12];
2569       char *startp;
2570
2571       buf[name_len + 11] = '\0';
2572       startp = _itoa (__getpid (), &buf[name_len + 11], 10, 0);
2573       *--startp = '.';
2574       startp = memcpy (startp - name_len, debug_output, name_len);
2575
2576       GLRO(dl_debug_fd) = __open (startp, flags, DEFFILEMODE);
2577       if (GLRO(dl_debug_fd) == -1)
2578         /* We use standard output if opening the file failed.  */
2579         GLRO(dl_debug_fd) = STDOUT_FILENO;
2580     }
2581 }
2582
2583
2584 /* Print the various times we collected.  */
2585 static void
2586 __attribute ((noinline))
2587 print_statistics (hp_timing_t *rtld_total_timep)
2588 {
2589 #ifndef HP_TIMING_NONAVAIL
2590   char buf[200];
2591   char *cp;
2592   char *wp;
2593
2594   /* Total time rtld used.  */
2595   if (HP_TIMING_AVAIL)
2596     {
2597       HP_TIMING_PRINT (buf, sizeof (buf), *rtld_total_timep);
2598       _dl_debug_printf ("\nruntime linker statistics:\n"
2599                         "  total startup time in dynamic loader: %s\n", buf);
2600
2601       /* Print relocation statistics.  */
2602       char pbuf[30];
2603       HP_TIMING_PRINT (buf, sizeof (buf), relocate_time);
2604       cp = _itoa ((1000ULL * relocate_time) / *rtld_total_timep,
2605                   pbuf + sizeof (pbuf), 10, 0);
2606       wp = pbuf;
2607       switch (pbuf + sizeof (pbuf) - cp)
2608         {
2609         case 3:
2610           *wp++ = *cp++;
2611         case 2:
2612           *wp++ = *cp++;
2613         case 1:
2614           *wp++ = '.';
2615           *wp++ = *cp++;
2616         }
2617       *wp = '\0';
2618       _dl_debug_printf ("\
2619             time needed for relocation: %s (%s%%)\n", buf, pbuf);
2620     }
2621 #endif
2622
2623   unsigned long int num_relative_relocations = 0;
2624   for (Lmid_t ns = 0; ns < GL(dl_nns); ++ns)
2625     {
2626       if (GL(dl_ns)[ns]._ns_loaded == NULL)
2627         continue;
2628
2629       struct r_scope_elem *scope = &GL(dl_ns)[ns]._ns_loaded->l_searchlist;
2630
2631       for (unsigned int i = 0; i < scope->r_nlist; i++)
2632         {
2633           struct link_map *l = scope->r_list [i];
2634
2635           if (l->l_addr != 0 && l->l_info[VERSYMIDX (DT_RELCOUNT)])
2636             num_relative_relocations
2637               += l->l_info[VERSYMIDX (DT_RELCOUNT)]->d_un.d_val;
2638 #ifndef ELF_MACHINE_REL_RELATIVE
2639           /* Relative relocations are processed on these architectures if
2640              library is loaded to different address than p_vaddr or
2641              if not prelinked.  */
2642           if ((l->l_addr != 0 || !l->l_info[VALIDX(DT_GNU_PRELINKED)])
2643               && l->l_info[VERSYMIDX (DT_RELACOUNT)])
2644 #else
2645           /* On e.g. IA-64 or Alpha, relative relocations are processed
2646              only if library is loaded to different address than p_vaddr.  */
2647           if (l->l_addr != 0 && l->l_info[VERSYMIDX (DT_RELACOUNT)])
2648 #endif
2649             num_relative_relocations
2650               += l->l_info[VERSYMIDX (DT_RELACOUNT)]->d_un.d_val;
2651         }
2652     }
2653
2654   _dl_debug_printf ("                 number of relocations: %lu\n"
2655                     "      number of relocations from cache: %lu\n"
2656                     "        number of relative relocations: %lu\n",
2657                     GL(dl_num_relocations),
2658                     GL(dl_num_cache_relocations),
2659                     num_relative_relocations);
2660
2661 #ifndef HP_TIMING_NONAVAIL
2662   /* Time spend while loading the object and the dependencies.  */
2663   if (HP_TIMING_AVAIL)
2664     {
2665       char pbuf[30];
2666       HP_TIMING_PRINT (buf, sizeof (buf), load_time);
2667       cp = _itoa ((1000ULL * load_time) / *rtld_total_timep,
2668                   pbuf + sizeof (pbuf), 10, 0);
2669       wp = pbuf;
2670       switch (pbuf + sizeof (pbuf) - cp)
2671         {
2672         case 3:
2673           *wp++ = *cp++;
2674         case 2:
2675           *wp++ = *cp++;
2676         case 1:
2677           *wp++ = '.';
2678           *wp++ = *cp++;
2679         }
2680       *wp = '\0';
2681       _dl_debug_printf ("\
2682            time needed to load objects: %s (%s%%)\n",
2683                                 buf, pbuf);
2684     }
2685 #endif
2686 }