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