Back to index

glibc  2.9
rtld.c
Go to the documentation of this file.
00001 /* Run time dynamic linker.
00002    Copyright (C) 1995-2006, 2007, 2008 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <dlfcn.h>
00022 #include <fcntl.h>
00023 #include <stdbool.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <unistd.h>
00027 #include <sys/mman.h>              /* Check if MAP_ANON is defined.  */
00028 #include <sys/param.h>
00029 #include <sys/stat.h>
00030 #include <ldsodefs.h>
00031 #include <stdio-common/_itoa.h>
00032 #include <entry.h>
00033 #include <fpu_control.h>
00034 #include <hp-timing.h>
00035 #include <bits/libc-lock.h>
00036 #include "dynamic-link.h"
00037 #include <dl-librecon.h>
00038 #include <unsecvars.h>
00039 #include <dl-cache.h>
00040 #include <dl-osinfo.h>
00041 #include <dl-procinfo.h>
00042 #include <tls.h>
00043 
00044 #include <assert.h>
00045 
00046 /* Avoid PLT use for our local calls at startup.  */
00047 extern __typeof (__mempcpy) __mempcpy attribute_hidden;
00048 
00049 /* GCC has mental blocks about _exit.  */
00050 extern __typeof (_exit) exit_internal asm ("_exit") attribute_hidden;
00051 #define _exit exit_internal
00052 
00053 /* Helper function to handle errors while resolving symbols.  */
00054 static void print_unresolved (int errcode, const char *objname,
00055                            const char *errsting);
00056 
00057 /* Helper function to handle errors when a version is missing.  */
00058 static void print_missing_version (int errcode, const char *objname,
00059                                const char *errsting);
00060 
00061 /* Print the various times we collected.  */
00062 static void print_statistics (hp_timing_t *total_timep);
00063 
00064 /* Add audit objects.  */
00065 static void process_dl_audit (char *str);
00066 
00067 /* This is a list of all the modes the dynamic loader can be in.  */
00068 enum mode { normal, list, verify, trace };
00069 
00070 /* Process all environments variables the dynamic linker must recognize.
00071    Since all of them start with `LD_' we are a bit smarter while finding
00072    all the entries.  */
00073 static void process_envvars (enum mode *modep);
00074 
00075 #ifdef DL_ARGV_NOT_RELRO
00076 int _dl_argc attribute_hidden;
00077 char **_dl_argv = NULL;
00078 /* Nonzero if we were run directly.  */
00079 unsigned int _dl_skip_args attribute_hidden;
00080 #else
00081 int _dl_argc attribute_relro attribute_hidden;
00082 char **_dl_argv attribute_relro = NULL;
00083 unsigned int _dl_skip_args attribute_relro attribute_hidden;
00084 #endif
00085 INTDEF(_dl_argv)
00086 
00087 #ifndef THREAD_SET_STACK_GUARD
00088 /* Only exported for architectures that don't store the stack guard canary
00089    in thread local area.  */
00090 uintptr_t __stack_chk_guard attribute_relro;
00091 #endif
00092 
00093 /* Only exported for architectures that don't store the pointer guard
00094    value in thread local area.  */
00095 uintptr_t __pointer_chk_guard_local
00096      attribute_relro attribute_hidden __attribute__ ((nocommon));
00097 #ifndef THREAD_SET_POINTER_GUARD
00098 strong_alias (__pointer_chk_guard_local, __pointer_chk_guard)
00099 #endif
00100 
00101 
00102 /* List of auditing DSOs.  */
00103 static struct audit_list
00104 {
00105   const char *name;
00106   struct audit_list *next;
00107 } *audit_list;
00108 
00109 #ifndef HAVE_INLINED_SYSCALLS
00110 /* Set nonzero during loading and initialization of executable and
00111    libraries, cleared before the executable's entry point runs.  This
00112    must not be initialized to nonzero, because the unused dynamic
00113    linker loaded in for libc.so's "ld.so.1" dep will provide the
00114    definition seen by libc.so's initializer; that value must be zero,
00115    and will be since that dynamic linker's _dl_start and dl_main will
00116    never be called.  */
00117 int _dl_starting_up = 0;
00118 INTVARDEF(_dl_starting_up)
00119 #endif
00120 
00121 /* This is the structure which defines all variables global to ld.so
00122    (except those which cannot be added for some reason).  */
00123 struct rtld_global _rtld_global =
00124   {
00125     /* Default presumption without further information is executable stack.  */
00126     ._dl_stack_flags = PF_R|PF_W|PF_X,
00127 #ifdef _LIBC_REENTRANT
00128     ._dl_load_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER
00129 #endif
00130   };
00131 /* If we would use strong_alias here the compiler would see a
00132    non-hidden definition.  This would undo the effect of the previous
00133    declaration.  So spell out was strong_alias does plus add the
00134    visibility attribute.  */
00135 extern struct rtld_global _rtld_local
00136     __attribute__ ((alias ("_rtld_global"), visibility ("hidden")));
00137 
00138 
00139 /* This variable is similar to _rtld_local, but all values are
00140    read-only after relocation.  */
00141 struct rtld_global_ro _rtld_global_ro attribute_relro =
00142   {
00143     /* Get architecture specific initializer.  */
00144 #include <dl-procinfo.c>
00145 #ifdef NEED_DL_SYSINFO
00146     ._dl_sysinfo = DL_SYSINFO_DEFAULT,
00147 #endif
00148     ._dl_debug_fd = STDERR_FILENO,
00149     ._dl_use_load_bias = -2,
00150     ._dl_correct_cache_id = _DL_CACHE_DEFAULT_ID,
00151     ._dl_hwcap_mask = HWCAP_IMPORTANT,
00152     ._dl_lazy = 1,
00153     ._dl_fpu_control = _FPU_DEFAULT,
00154     ._dl_pointer_guard = 1,
00155 
00156     /* Function pointers.  */
00157     ._dl_debug_printf = _dl_debug_printf,
00158     ._dl_catch_error = _dl_catch_error,
00159     ._dl_signal_error = _dl_signal_error,
00160     ._dl_mcount = _dl_mcount_internal,
00161     ._dl_lookup_symbol_x = _dl_lookup_symbol_x,
00162     ._dl_check_caller = _dl_check_caller,
00163     ._dl_open = _dl_open,
00164     ._dl_close = _dl_close,
00165     ._dl_tls_get_addr_soft = _dl_tls_get_addr_soft,
00166 #ifdef HAVE_DL_DISCOVER_OSVERSION
00167     ._dl_discover_osversion = _dl_discover_osversion
00168 #endif
00169   };
00170 /* If we would use strong_alias here the compiler would see a
00171    non-hidden definition.  This would undo the effect of the previous
00172    declaration.  So spell out was strong_alias does plus add the
00173    visibility attribute.  */
00174 extern struct rtld_global_ro _rtld_local_ro
00175     __attribute__ ((alias ("_rtld_global_ro"), visibility ("hidden")));
00176 
00177 
00178 static void dl_main (const ElfW(Phdr) *phdr, ElfW(Word) phnum,
00179                    ElfW(Addr) *user_entry);
00180 
00181 /* These two variables cannot be moved into .data.rel.ro.  */
00182 static struct libname_list _dl_rtld_libname;
00183 static struct libname_list _dl_rtld_libname2;
00184 
00185 /* We expect less than a second for relocation.  */
00186 #ifdef HP_SMALL_TIMING_AVAIL
00187 # undef HP_TIMING_AVAIL
00188 # define HP_TIMING_AVAIL HP_SMALL_TIMING_AVAIL
00189 #endif
00190 
00191 /* Variable for statistics.  */
00192 #ifndef HP_TIMING_NONAVAIL
00193 static hp_timing_t relocate_time;
00194 static hp_timing_t load_time attribute_relro;
00195 static hp_timing_t start_time attribute_relro;
00196 #endif
00197 
00198 /* Additional definitions needed by TLS initialization.  */
00199 #ifdef TLS_INIT_HELPER
00200 TLS_INIT_HELPER
00201 #endif
00202 
00203 /* Helper function for syscall implementation.  */
00204 #ifdef DL_SYSINFO_IMPLEMENTATION
00205 DL_SYSINFO_IMPLEMENTATION
00206 #endif
00207 
00208 /* Before ld.so is relocated we must not access variables which need
00209    relocations.  This means variables which are exported.  Variables
00210    declared as static are fine.  If we can mark a variable hidden this
00211    is fine, too.  The latter is important here.  We can avoid setting
00212    up a temporary link map for ld.so if we can mark _rtld_global as
00213    hidden.  */
00214 #ifdef PI_STATIC_AND_HIDDEN
00215 # define DONT_USE_BOOTSTRAP_MAP    1
00216 #endif
00217 
00218 #ifdef DONT_USE_BOOTSTRAP_MAP
00219 static ElfW(Addr) _dl_start_final (void *arg);
00220 #else
00221 struct dl_start_final_info
00222 {
00223   struct link_map l;
00224 #if !defined HP_TIMING_NONAVAIL && HP_TIMING_INLINE
00225   hp_timing_t start_time;
00226 #endif
00227 };
00228 static ElfW(Addr) _dl_start_final (void *arg,
00229                                struct dl_start_final_info *info);
00230 #endif
00231 
00232 /* These defined magically in the linker script.  */
00233 extern char _begin[] attribute_hidden;
00234 extern char _etext[] attribute_hidden;
00235 extern char _end[] attribute_hidden;
00236 
00237 
00238 #ifdef RTLD_START
00239 RTLD_START
00240 #else
00241 # error "sysdeps/MACHINE/dl-machine.h fails to define RTLD_START"
00242 #endif
00243 
00244 #ifndef VALIDX
00245 # define VALIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
00246                     + DT_EXTRANUM + DT_VALTAGIDX (tag))
00247 #endif
00248 #ifndef ADDRIDX
00249 # define ADDRIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
00250                      + DT_EXTRANUM + DT_VALNUM + DT_ADDRTAGIDX (tag))
00251 #endif
00252 
00253 /* This is the second half of _dl_start (below).  It can be inlined safely
00254    under DONT_USE_BOOTSTRAP_MAP, where it is careful not to make any GOT
00255    references.  When the tools don't permit us to avoid using a GOT entry
00256    for _dl_rtld_global (no attribute_hidden support), we must make sure
00257    this function is not inlined (see below).  */
00258 
00259 #ifdef DONT_USE_BOOTSTRAP_MAP
00260 static inline ElfW(Addr) __attribute__ ((always_inline))
00261 _dl_start_final (void *arg)
00262 #else
00263 static ElfW(Addr) __attribute__ ((noinline))
00264 _dl_start_final (void *arg, struct dl_start_final_info *info)
00265 #endif
00266 {
00267   ElfW(Addr) start_addr;
00268 
00269   if (HP_TIMING_AVAIL)
00270     {
00271       /* If it hasn't happen yet record the startup time.  */
00272       if (! HP_TIMING_INLINE)
00273        HP_TIMING_NOW (start_time);
00274 #if !defined DONT_USE_BOOTSTRAP_MAP && !defined HP_TIMING_NONAVAIL
00275       else
00276        start_time = info->start_time;
00277 #endif
00278 
00279       /* Initialize the timing functions.  */
00280       HP_TIMING_DIFF_INIT ();
00281     }
00282 
00283   /* Transfer data about ourselves to the permanent link_map structure.  */
00284 #ifndef DONT_USE_BOOTSTRAP_MAP
00285   GL(dl_rtld_map).l_addr = info->l.l_addr;
00286   GL(dl_rtld_map).l_ld = info->l.l_ld;
00287   memcpy (GL(dl_rtld_map).l_info, info->l.l_info,
00288          sizeof GL(dl_rtld_map).l_info);
00289   GL(dl_rtld_map).l_mach = info->l.l_mach;
00290   GL(dl_rtld_map).l_relocated = 1;
00291 #endif
00292   _dl_setup_hash (&GL(dl_rtld_map));
00293   GL(dl_rtld_map).l_real = &GL(dl_rtld_map);
00294   GL(dl_rtld_map).l_map_start = (ElfW(Addr)) _begin;
00295   GL(dl_rtld_map).l_map_end = (ElfW(Addr)) _end;
00296   GL(dl_rtld_map).l_text_end = (ElfW(Addr)) _etext;
00297   /* Copy the TLS related data if necessary.  */
00298 #ifndef DONT_USE_BOOTSTRAP_MAP
00299 # if USE___THREAD
00300   assert (info->l.l_tls_modid != 0);
00301   GL(dl_rtld_map).l_tls_blocksize = info->l.l_tls_blocksize;
00302   GL(dl_rtld_map).l_tls_align = info->l.l_tls_align;
00303   GL(dl_rtld_map).l_tls_firstbyte_offset = info->l.l_tls_firstbyte_offset;
00304   GL(dl_rtld_map).l_tls_initimage_size = info->l.l_tls_initimage_size;
00305   GL(dl_rtld_map).l_tls_initimage = info->l.l_tls_initimage;
00306   GL(dl_rtld_map).l_tls_offset = info->l.l_tls_offset;
00307   GL(dl_rtld_map).l_tls_modid = 1;
00308 # else
00309 #  if NO_TLS_OFFSET != 0
00310   GL(dl_rtld_map).l_tls_offset = NO_TLS_OFFSET;
00311 #  endif
00312 # endif
00313 
00314 #endif
00315 
00316 #if HP_TIMING_AVAIL
00317   HP_TIMING_NOW (GL(dl_cpuclock_offset));
00318 #endif
00319 
00320   /* Initialize the stack end variable.  */
00321   __libc_stack_end = __builtin_frame_address (0);
00322 
00323   /* Call the OS-dependent function to set up life so we can do things like
00324      file access.  It will call `dl_main' (below) to do all the real work
00325      of the dynamic linker, and then unwind our frame and run the user
00326      entry point on the same stack we entered on.  */
00327   start_addr = _dl_sysdep_start (arg, &dl_main);
00328 
00329 #ifndef HP_TIMING_NONAVAIL
00330   hp_timing_t rtld_total_time;
00331   if (HP_TIMING_AVAIL)
00332     {
00333       hp_timing_t end_time;
00334 
00335       /* Get the current time.  */
00336       HP_TIMING_NOW (end_time);
00337 
00338       /* Compute the difference.  */
00339       HP_TIMING_DIFF (rtld_total_time, start_time, end_time);
00340     }
00341 #endif
00342 
00343   if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_STATISTICS, 0))
00344     {
00345 #ifndef HP_TIMING_NONAVAIL
00346       print_statistics (&rtld_total_time);
00347 #else
00348       print_statistics (NULL);
00349 #endif
00350     }
00351 
00352   return start_addr;
00353 }
00354 
00355 static ElfW(Addr) __attribute_used__ internal_function
00356 _dl_start (void *arg)
00357 {
00358 #ifdef DONT_USE_BOOTSTRAP_MAP
00359 # define bootstrap_map GL(dl_rtld_map)
00360 #else
00361   struct dl_start_final_info info;
00362 # define bootstrap_map info.l
00363 #endif
00364 
00365   /* This #define produces dynamic linking inline functions for
00366      bootstrap relocation instead of general-purpose relocation.
00367      Since ld.so must not have any undefined symbols the result
00368      is trivial: always the map of ld.so itself.  */
00369 #define RTLD_BOOTSTRAP
00370 #define RESOLVE_MAP(sym, version, flags) (&bootstrap_map)
00371 #include "dynamic-link.h"
00372 
00373   if (HP_TIMING_INLINE && HP_TIMING_AVAIL)
00374 #ifdef DONT_USE_BOOTSTRAP_MAP
00375     HP_TIMING_NOW (start_time);
00376 #else
00377     HP_TIMING_NOW (info.start_time);
00378 #endif
00379 
00380   /* Partly clean the `bootstrap_map' structure up.  Don't use
00381      `memset' since it might not be built in or inlined and we cannot
00382      make function calls at this point.  Use '__builtin_memset' if we
00383      know it is available.  We do not have to clear the memory if we
00384      do not have to use the temporary bootstrap_map.  Global variables
00385      are initialized to zero by default.  */
00386 #ifndef DONT_USE_BOOTSTRAP_MAP
00387 # ifdef HAVE_BUILTIN_MEMSET
00388   __builtin_memset (bootstrap_map.l_info, '\0', sizeof (bootstrap_map.l_info));
00389 # else
00390   for (size_t cnt = 0;
00391        cnt < sizeof (bootstrap_map.l_info) / sizeof (bootstrap_map.l_info[0]);
00392        ++cnt)
00393     bootstrap_map.l_info[cnt] = 0;
00394 # endif
00395 # if USE___THREAD
00396   bootstrap_map.l_tls_modid = 0;
00397 # endif
00398 #endif
00399 
00400   /* Figure out the run-time load address of the dynamic linker itself.  */
00401   bootstrap_map.l_addr = elf_machine_load_address ();
00402 
00403   /* Read our own dynamic section and fill in the info array.  */
00404   bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
00405   elf_get_dynamic_info (&bootstrap_map, NULL);
00406 
00407 #if NO_TLS_OFFSET != 0
00408   bootstrap_map.l_tls_offset = NO_TLS_OFFSET;
00409 #endif
00410 
00411   /* Get the dynamic linker's own program header.  First we need the ELF
00412      file header.  The `_begin' symbol created by the linker script points
00413      to it.  When we have something like GOTOFF relocs, we can use a plain
00414      reference to find the runtime address.  Without that, we have to rely
00415      on the `l_addr' value, which is not the value we want when prelinked.  */
00416 #if USE___THREAD
00417   dtv_t initdtv[3];
00418   ElfW(Ehdr) *ehdr
00419 # ifdef DONT_USE_BOOTSTRAP_MAP
00420     = (ElfW(Ehdr) *) &_begin;
00421 # else
00422 #  error This will not work with prelink.
00423     = (ElfW(Ehdr) *) bootstrap_map.l_addr;
00424 # endif
00425   ElfW(Phdr) *phdr = (ElfW(Phdr) *) ((void *) ehdr + ehdr->e_phoff);
00426   size_t cnt = ehdr->e_phnum;      /* PT_TLS is usually the last phdr.  */
00427   while (cnt-- > 0)
00428     if (phdr[cnt].p_type == PT_TLS)
00429       {
00430        void *tlsblock;
00431        size_t max_align = MAX (TLS_INIT_TCB_ALIGN, phdr[cnt].p_align);
00432        char *p;
00433 
00434        bootstrap_map.l_tls_blocksize = phdr[cnt].p_memsz;
00435        bootstrap_map.l_tls_align = phdr[cnt].p_align;
00436        if (phdr[cnt].p_align == 0)
00437          bootstrap_map.l_tls_firstbyte_offset = 0;
00438        else
00439          bootstrap_map.l_tls_firstbyte_offset = (phdr[cnt].p_vaddr
00440                                             & (phdr[cnt].p_align - 1));
00441        assert (bootstrap_map.l_tls_blocksize != 0);
00442        bootstrap_map.l_tls_initimage_size = phdr[cnt].p_filesz;
00443        bootstrap_map.l_tls_initimage = (void *) (bootstrap_map.l_addr
00444                                             + phdr[cnt].p_vaddr);
00445 
00446        /* We can now allocate the initial TLS block.  This can happen
00447           on the stack.  We'll get the final memory later when we
00448           know all about the various objects loaded at startup
00449           time.  */
00450 # if TLS_TCB_AT_TP
00451        tlsblock = alloca (roundup (bootstrap_map.l_tls_blocksize,
00452                                 TLS_INIT_TCB_ALIGN)
00453                         + TLS_INIT_TCB_SIZE
00454                         + max_align);
00455 # elif TLS_DTV_AT_TP
00456        tlsblock = alloca (roundup (TLS_INIT_TCB_SIZE,
00457                                 bootstrap_map.l_tls_align)
00458                         + bootstrap_map.l_tls_blocksize
00459                         + max_align);
00460 # else
00461        /* In case a model with a different layout for the TCB and DTV
00462           is defined add another #elif here and in the following #ifs.  */
00463 #  error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
00464 # endif
00465        /* Align the TLS block.  */
00466        tlsblock = (void *) (((uintptr_t) tlsblock + max_align - 1)
00467                           & ~(max_align - 1));
00468 
00469        /* Initialize the dtv.  [0] is the length, [1] the generation
00470           counter.  */
00471        initdtv[0].counter = 1;
00472        initdtv[1].counter = 0;
00473 
00474        /* Initialize the TLS block.  */
00475 # if TLS_TCB_AT_TP
00476        initdtv[2].pointer = tlsblock;
00477 # elif TLS_DTV_AT_TP
00478        bootstrap_map.l_tls_offset = roundup (TLS_INIT_TCB_SIZE,
00479                                          bootstrap_map.l_tls_align);
00480        initdtv[2].pointer = (char *) tlsblock + bootstrap_map.l_tls_offset;
00481 # else
00482 #  error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
00483 # endif
00484        p = __mempcpy (initdtv[2].pointer, bootstrap_map.l_tls_initimage,
00485                      bootstrap_map.l_tls_initimage_size);
00486 # ifdef HAVE_BUILTIN_MEMSET
00487        __builtin_memset (p, '\0', (bootstrap_map.l_tls_blocksize
00488                                 - bootstrap_map.l_tls_initimage_size));
00489 # else
00490        {
00491          size_t remaining = (bootstrap_map.l_tls_blocksize
00492                            - bootstrap_map.l_tls_initimage_size);
00493          while (remaining-- > 0)
00494            *p++ = '\0';
00495        }
00496 # endif
00497 
00498        /* Install the pointer to the dtv.  */
00499 
00500        /* Initialize the thread pointer.  */
00501 # if TLS_TCB_AT_TP
00502        bootstrap_map.l_tls_offset
00503          = roundup (bootstrap_map.l_tls_blocksize, TLS_INIT_TCB_ALIGN);
00504 
00505        INSTALL_DTV ((char *) tlsblock + bootstrap_map.l_tls_offset,
00506                    initdtv);
00507 
00508        const char *lossage = TLS_INIT_TP ((char *) tlsblock
00509                                       + bootstrap_map.l_tls_offset, 0);
00510 # elif TLS_DTV_AT_TP
00511        INSTALL_DTV (tlsblock, initdtv);
00512        const char *lossage = TLS_INIT_TP (tlsblock, 0);
00513 # else
00514 #  error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
00515 # endif
00516        if (__builtin_expect (lossage != NULL, 0))
00517          _dl_fatal_printf ("cannot set up thread-local storage: %s\n",
00518                          lossage);
00519 
00520        /* So far this is module number one.  */
00521        bootstrap_map.l_tls_modid = 1;
00522 
00523        /* There can only be one PT_TLS entry.  */
00524        break;
00525       }
00526 #endif /* USE___THREAD */
00527 
00528 #ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
00529   ELF_MACHINE_BEFORE_RTLD_RELOC (bootstrap_map.l_info);
00530 #endif
00531 
00532   if (bootstrap_map.l_addr || ! bootstrap_map.l_info[VALIDX(DT_GNU_PRELINKED)])
00533     {
00534       /* Relocate ourselves so we can do normal function calls and
00535         data access using the global offset table.  */
00536 
00537       ELF_DYNAMIC_RELOCATE (&bootstrap_map, 0, 0);
00538     }
00539   bootstrap_map.l_relocated = 1;
00540 
00541   /* Please note that we don't allow profiling of this object and
00542      therefore need not test whether we have to allocate the array
00543      for the relocation results (as done in dl-reloc.c).  */
00544 
00545   /* Now life is sane; we can call functions and access global data.
00546      Set up to use the operating system facilities, and find out from
00547      the operating system's program loader where to find the program
00548      header table in core.  Put the rest of _dl_start into a separate
00549      function, that way the compiler cannot put accesses to the GOT
00550      before ELF_DYNAMIC_RELOCATE.  */
00551   {
00552 #ifdef DONT_USE_BOOTSTRAP_MAP
00553     ElfW(Addr) entry = _dl_start_final (arg);
00554 #else
00555     ElfW(Addr) entry = _dl_start_final (arg, &info);
00556 #endif
00557 
00558 #ifndef ELF_MACHINE_START_ADDRESS
00559 # define ELF_MACHINE_START_ADDRESS(map, start) (start)
00560 #endif
00561 
00562     return ELF_MACHINE_START_ADDRESS (GL(dl_ns)[LM_ID_BASE]._ns_loaded, entry);
00563   }
00564 }
00565 
00566 
00567 
00568 /* Now life is peachy; we can do all normal operations.
00569    On to the real work.  */
00570 
00571 /* Some helper functions.  */
00572 
00573 /* Arguments to relocate_doit.  */
00574 struct relocate_args
00575 {
00576   struct link_map *l;
00577   int lazy;
00578 };
00579 
00580 struct map_args
00581 {
00582   /* Argument to map_doit.  */
00583   char *str;
00584   struct link_map *loader;
00585   int is_preloaded;
00586   int mode;
00587   /* Return value of map_doit.  */
00588   struct link_map *map;
00589 };
00590 
00591 struct dlmopen_args
00592 {
00593   const char *fname;
00594   struct link_map *map;
00595 };
00596 
00597 struct lookup_args
00598 {
00599   const char *name;
00600   struct link_map *map;
00601   void *result;
00602 };
00603 
00604 /* Arguments to version_check_doit.  */
00605 struct version_check_args
00606 {
00607   int doexit;
00608   int dotrace;
00609 };
00610 
00611 static void
00612 relocate_doit (void *a)
00613 {
00614   struct relocate_args *args = (struct relocate_args *) a;
00615 
00616   _dl_relocate_object (args->l, args->l->l_scope, args->lazy, 0);
00617 }
00618 
00619 static void
00620 map_doit (void *a)
00621 {
00622   struct map_args *args = (struct map_args *) a;
00623   args->map = _dl_map_object (args->loader, args->str,
00624                            args->is_preloaded, lt_library, 0, args->mode,
00625                            LM_ID_BASE);
00626 }
00627 
00628 static void
00629 dlmopen_doit (void *a)
00630 {
00631   struct dlmopen_args *args = (struct dlmopen_args *) a;
00632   args->map = _dl_open (args->fname, RTLD_LAZY | __RTLD_DLOPEN | __RTLD_AUDIT,
00633                      dl_main, LM_ID_NEWLM, _dl_argc, INTUSE(_dl_argv),
00634                      __environ);
00635 }
00636 
00637 static void
00638 lookup_doit (void *a)
00639 {
00640   struct lookup_args *args = (struct lookup_args *) a;
00641   const ElfW(Sym) *ref = NULL;
00642   args->result = NULL;
00643   lookup_t l = _dl_lookup_symbol_x (args->name, args->map, &ref,
00644                                 args->map->l_local_scope, NULL, 0,
00645                                 DL_LOOKUP_RETURN_NEWEST, NULL);
00646   if (ref != NULL)
00647     args->result = DL_SYMBOL_ADDRESS (l, ref);
00648 }
00649 
00650 static void
00651 version_check_doit (void *a)
00652 {
00653   struct version_check_args *args = (struct version_check_args *) a;
00654   if (_dl_check_all_versions (GL(dl_ns)[LM_ID_BASE]._ns_loaded, 1,
00655                            args->dotrace) && args->doexit)
00656     /* We cannot start the application.  Abort now.  */
00657     _exit (1);
00658 }
00659 
00660 
00661 static inline struct link_map *
00662 find_needed (const char *name)
00663 {
00664   struct r_scope_elem *scope = &GL(dl_ns)[LM_ID_BASE]._ns_loaded->l_searchlist;
00665   unsigned int n = scope->r_nlist;
00666 
00667   while (n-- > 0)
00668     if (_dl_name_match_p (name, scope->r_list[n]))
00669       return scope->r_list[n];
00670 
00671   /* Should never happen.  */
00672   return NULL;
00673 }
00674 
00675 static int
00676 match_version (const char *string, struct link_map *map)
00677 {
00678   const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
00679   ElfW(Verdef) *def;
00680 
00681 #define VERDEFTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERDEF))
00682   if (map->l_info[VERDEFTAG] == NULL)
00683     /* The file has no symbol versioning.  */
00684     return 0;
00685 
00686   def = (ElfW(Verdef) *) ((char *) map->l_addr
00687                        + map->l_info[VERDEFTAG]->d_un.d_ptr);
00688   while (1)
00689     {
00690       ElfW(Verdaux) *aux = (ElfW(Verdaux) *) ((char *) def + def->vd_aux);
00691 
00692       /* Compare the version strings.  */
00693       if (strcmp (string, strtab + aux->vda_name) == 0)
00694        /* Bingo!  */
00695        return 1;
00696 
00697       /* If no more definitions we failed to find what we want.  */
00698       if (def->vd_next == 0)
00699        break;
00700 
00701       /* Next definition.  */
00702       def = (ElfW(Verdef) *) ((char *) def + def->vd_next);
00703     }
00704 
00705   return 0;
00706 }
00707 
00708 static bool tls_init_tp_called;
00709 
00710 static void *
00711 init_tls (void)
00712 {
00713   /* Number of elements in the static TLS block.  */
00714   GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx);
00715 
00716   /* Do not do this twice.  The audit interface might have required
00717      the DTV interfaces to be set up early.  */
00718   if (GL(dl_initial_dtv) != NULL)
00719     return NULL;
00720 
00721   /* Allocate the array which contains the information about the
00722      dtv slots.  We allocate a few entries more than needed to
00723      avoid the need for reallocation.  */
00724   size_t nelem = GL(dl_tls_max_dtv_idx) + 1 + TLS_SLOTINFO_SURPLUS;
00725 
00726   /* Allocate.  */
00727   GL(dl_tls_dtv_slotinfo_list) = (struct dtv_slotinfo_list *)
00728     calloc (sizeof (struct dtv_slotinfo_list)
00729            + nelem * sizeof (struct dtv_slotinfo), 1);
00730   /* No need to check the return value.  If memory allocation failed
00731      the program would have been terminated.  */
00732 
00733   struct dtv_slotinfo *slotinfo = GL(dl_tls_dtv_slotinfo_list)->slotinfo;
00734   GL(dl_tls_dtv_slotinfo_list)->len = nelem;
00735   GL(dl_tls_dtv_slotinfo_list)->next = NULL;
00736 
00737   /* Fill in the information from the loaded modules.  No namespace
00738      but the base one can be filled at this time.  */
00739   assert (GL(dl_ns)[LM_ID_BASE + 1]._ns_loaded == NULL);
00740   int i = 0;
00741   for (struct link_map *l = GL(dl_ns)[LM_ID_BASE]._ns_loaded; l != NULL;
00742        l = l->l_next)
00743     if (l->l_tls_blocksize != 0)
00744       {
00745        /* This is a module with TLS data.  Store the map reference.
00746           The generation counter is zero.  */
00747        slotinfo[i].map = l;
00748        /* slotinfo[i].gen = 0; */
00749        ++i;
00750       }
00751   assert (i == GL(dl_tls_max_dtv_idx));
00752 
00753   /* Compute the TLS offsets for the various blocks.  */
00754   _dl_determine_tlsoffset ();
00755 
00756   /* Construct the static TLS block and the dtv for the initial
00757      thread.  For some platforms this will include allocating memory
00758      for the thread descriptor.  The memory for the TLS block will
00759      never be freed.  It should be allocated accordingly.  The dtv
00760      array can be changed if dynamic loading requires it.  */
00761   void *tcbp = _dl_allocate_tls_storage ();
00762   if (tcbp == NULL)
00763     _dl_fatal_printf ("\
00764 cannot allocate TLS data structures for initial thread");
00765 
00766   /* Store for detection of the special case by __tls_get_addr
00767      so it knows not to pass this dtv to the normal realloc.  */
00768   GL(dl_initial_dtv) = GET_DTV (tcbp);
00769 
00770   /* And finally install it for the main thread.  If ld.so itself uses
00771      TLS we know the thread pointer was initialized earlier.  */
00772   const char *lossage = TLS_INIT_TP (tcbp, USE___THREAD);
00773   if (__builtin_expect (lossage != NULL, 0))
00774     _dl_fatal_printf ("cannot set up thread-local storage: %s\n", lossage);
00775   tls_init_tp_called = true;
00776 
00777   return tcbp;
00778 }
00779 
00780 #ifdef _LIBC_REENTRANT
00781 /* _dl_error_catch_tsd points to this for the single-threaded case.
00782    It's reset by the thread library for multithreaded programs.  */
00783 void ** __attribute__ ((const))
00784 _dl_initial_error_catch_tsd (void)
00785 {
00786   static void *data;
00787   return &data;
00788 }
00789 #endif
00790 
00791 
00792 static unsigned int
00793 do_preload (char *fname, struct link_map *main_map, const char *where)
00794 {
00795   const char *objname;
00796   const char *err_str = NULL;
00797   struct map_args args;
00798   bool malloced;
00799 
00800   args.str = fname;
00801   args.loader = main_map;
00802   args.is_preloaded = 1;
00803   args.mode = 0;
00804 
00805   unsigned int old_nloaded = GL(dl_ns)[LM_ID_BASE]._ns_nloaded;
00806 
00807   (void) _dl_catch_error (&objname, &err_str, &malloced, map_doit, &args);
00808   if (__builtin_expect (err_str != NULL, 0))
00809     {
00810       _dl_error_printf ("\
00811 ERROR: ld.so: object '%s' from %s cannot be preloaded: ignored.\n",
00812                      fname, where);
00813       /* No need to call free, this is still before
00814         the libc's malloc is used.  */
00815     }
00816   else if (GL(dl_ns)[LM_ID_BASE]._ns_nloaded != old_nloaded)
00817     /* It is no duplicate.  */
00818     return 1;
00819 
00820   /* Nothing loaded.  */
00821   return 0;
00822 }
00823 
00824 #if defined SHARED && defined _LIBC_REENTRANT \
00825     && defined __rtld_lock_default_lock_recursive
00826 static void
00827 rtld_lock_default_lock_recursive (void *lock)
00828 {
00829   __rtld_lock_default_lock_recursive (lock);
00830 }
00831 
00832 static void
00833 rtld_lock_default_unlock_recursive (void *lock)
00834 {
00835   __rtld_lock_default_unlock_recursive (lock);
00836 }
00837 #endif
00838 
00839 
00840 static void
00841 security_init (void)
00842 {
00843   /* Set up the stack checker's canary.  */
00844   uintptr_t stack_chk_guard = _dl_setup_stack_chk_guard ();
00845 #ifdef THREAD_SET_STACK_GUARD
00846   THREAD_SET_STACK_GUARD (stack_chk_guard);
00847 #else
00848   __stack_chk_guard = stack_chk_guard;
00849 #endif
00850 
00851   /* Set up the pointer guard as well, if necessary.  */
00852   if (GLRO(dl_pointer_guard))
00853     {
00854       // XXX If it is cheap, we should use a separate value.
00855       uintptr_t pointer_chk_guard = stack_chk_guard;
00856 #ifndef HP_TIMING_NONAVAIL
00857       hp_timing_t now;
00858       HP_TIMING_NOW (now);
00859       pointer_chk_guard ^= now;
00860 #endif
00861 #ifdef THREAD_SET_POINTER_GUARD
00862       THREAD_SET_POINTER_GUARD (pointer_chk_guard);
00863 #endif
00864       __pointer_chk_guard_local = pointer_chk_guard;
00865     }
00866 }
00867 
00868 
00869 /* The library search path.  */
00870 static const char *library_path attribute_relro;
00871 /* The list preloaded objects.  */
00872 static const char *preloadlist attribute_relro;
00873 /* Nonzero if information about versions has to be printed.  */
00874 static int version_info attribute_relro;
00875 
00876 static void
00877 dl_main (const ElfW(Phdr) *phdr,
00878         ElfW(Word) phnum,
00879         ElfW(Addr) *user_entry)
00880 {
00881   const ElfW(Phdr) *ph;
00882   enum mode mode;
00883   struct link_map *main_map;
00884   size_t file_size;
00885   char *file;
00886   bool has_interp = false;
00887   unsigned int i;
00888   bool prelinked = false;
00889   bool rtld_is_main = false;
00890 #ifndef HP_TIMING_NONAVAIL
00891   hp_timing_t start;
00892   hp_timing_t stop;
00893   hp_timing_t diff;
00894 #endif
00895   void *tcbp = NULL;
00896 
00897 #ifdef _LIBC_REENTRANT
00898   /* Explicit initialization since the reloc would just be more work.  */
00899   GL(dl_error_catch_tsd) = &_dl_initial_error_catch_tsd;
00900 #endif
00901 
00902   GL(dl_init_static_tls) = &_dl_nothread_init_static_tls;
00903 
00904 #if defined SHARED && defined _LIBC_REENTRANT \
00905     && defined __rtld_lock_default_lock_recursive
00906   GL(dl_rtld_lock_recursive) = rtld_lock_default_lock_recursive;
00907   GL(dl_rtld_unlock_recursive) = rtld_lock_default_unlock_recursive;
00908 #endif
00909 
00910   /* The explicit initialization here is cheaper than processing the reloc
00911      in the _rtld_local definition's initializer.  */
00912   GL(dl_make_stack_executable_hook) = &_dl_make_stack_executable;
00913 
00914   /* Process the environment variable which control the behaviour.  */
00915   process_envvars (&mode);
00916 
00917 #ifndef HAVE_INLINED_SYSCALLS
00918   /* Set up a flag which tells we are just starting.  */
00919   INTUSE(_dl_starting_up) = 1;
00920 #endif
00921 
00922   if (*user_entry == (ElfW(Addr)) ENTRY_POINT)
00923     {
00924       /* Ho ho.  We are not the program interpreter!  We are the program
00925         itself!  This means someone ran ld.so as a command.  Well, that
00926         might be convenient to do sometimes.  We support it by
00927         interpreting the args like this:
00928 
00929         ld.so PROGRAM ARGS...
00930 
00931         The first argument is the name of a file containing an ELF
00932         executable we will load and run with the following arguments.
00933         To simplify life here, PROGRAM is searched for using the
00934         normal rules for shared objects, rather than $PATH or anything
00935         like that.  We just load it and use its entry point; we don't
00936         pay attention to its PT_INTERP command (we are the interpreter
00937         ourselves).  This is an easy way to test a new ld.so before
00938         installing it.  */
00939       rtld_is_main = true;
00940 
00941       /* Note the place where the dynamic linker actually came from.  */
00942       GL(dl_rtld_map).l_name = rtld_progname;
00943 
00944       while (_dl_argc > 1)
00945        if (! strcmp (INTUSE(_dl_argv)[1], "--list"))
00946          {
00947            mode = list;
00948            GLRO(dl_lazy) = -1;     /* This means do no dependency analysis.  */
00949 
00950            ++_dl_skip_args;
00951            --_dl_argc;
00952            ++INTUSE(_dl_argv);
00953          }
00954        else if (! strcmp (INTUSE(_dl_argv)[1], "--verify"))
00955          {
00956            mode = verify;
00957 
00958            ++_dl_skip_args;
00959            --_dl_argc;
00960            ++INTUSE(_dl_argv);
00961          }
00962        else if (! strcmp (INTUSE(_dl_argv)[1], "--library-path")
00963                && _dl_argc > 2)
00964          {
00965            library_path = INTUSE(_dl_argv)[2];
00966 
00967            _dl_skip_args += 2;
00968            _dl_argc -= 2;
00969            INTUSE(_dl_argv) += 2;
00970          }
00971        else if (! strcmp (INTUSE(_dl_argv)[1], "--inhibit-rpath")
00972                && _dl_argc > 2)
00973          {
00974            GLRO(dl_inhibit_rpath) = INTUSE(_dl_argv)[2];
00975 
00976            _dl_skip_args += 2;
00977            _dl_argc -= 2;
00978            INTUSE(_dl_argv) += 2;
00979          }
00980        else if (! strcmp (INTUSE(_dl_argv)[1], "--audit") && _dl_argc > 2)
00981          {
00982            process_dl_audit (INTUSE(_dl_argv)[2]);
00983 
00984            _dl_skip_args += 2;
00985            _dl_argc -= 2;
00986            INTUSE(_dl_argv) += 2;
00987          }
00988        else
00989          break;
00990 
00991       /* If we have no further argument the program was called incorrectly.
00992         Grant the user some education.  */
00993       if (_dl_argc < 2)
00994        _dl_fatal_printf ("\
00995 Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
00996 You have invoked `ld.so', the helper program for shared library executables.\n\
00997 This program usually lives in the file `/lib/ld.so', and special directives\n\
00998 in executable files using ELF shared libraries tell the system's program\n\
00999 loader to load the helper program from this file.  This helper program loads\n\
01000 the shared libraries needed by the program executable, prepares the program\n\
01001 to run, and runs it.  You may invoke this helper program directly from the\n\
01002 command line to load and run an ELF executable file; this is like executing\n\
01003 that file itself, but always uses this helper program from the file you\n\
01004 specified, instead of the helper program file specified in the executable\n\
01005 file you run.  This is mostly of use for maintainers to test new versions\n\
01006 of this helper program; chances are you did not intend to run this program.\n\
01007 \n\
01008   --list                list all dependencies and how they are resolved\n\
01009   --verify              verify that given object really is a dynamically linked\n\
01010                         object we can handle\n\
01011   --library-path PATH   use given PATH instead of content of the environment\n\
01012                         variable LD_LIBRARY_PATH\n\
01013   --inhibit-rpath LIST  ignore RUNPATH and RPATH information in object names\n\
01014                         in LIST\n");
01015 
01016       ++_dl_skip_args;
01017       --_dl_argc;
01018       ++INTUSE(_dl_argv);
01019 
01020       /* The initialization of _dl_stack_flags done below assumes the
01021         executable's PT_GNU_STACK may have been honored by the kernel, and
01022         so a PT_GNU_STACK with PF_X set means the stack started out with
01023         execute permission.  However, this is not really true if the
01024         dynamic linker is the executable the kernel loaded.  For this
01025         case, we must reinitialize _dl_stack_flags to match the dynamic
01026         linker itself.  If the dynamic linker was built with a
01027         PT_GNU_STACK, then the kernel may have loaded us with a
01028         nonexecutable stack that we will have to make executable when we
01029         load the program below unless it has a PT_GNU_STACK indicating
01030         nonexecutable stack is ok.  */
01031 
01032       for (ph = phdr; ph < &phdr[phnum]; ++ph)
01033        if (ph->p_type == PT_GNU_STACK)
01034          {
01035            GL(dl_stack_flags) = ph->p_flags;
01036            break;
01037          }
01038 
01039       if (__builtin_expect (mode, normal) == verify)
01040        {
01041          const char *objname;
01042          const char *err_str = NULL;
01043          struct map_args args;
01044          bool malloced;
01045 
01046          args.str = rtld_progname;
01047          args.loader = NULL;
01048          args.is_preloaded = 0;
01049          args.mode = __RTLD_OPENEXEC;
01050          (void) _dl_catch_error (&objname, &err_str, &malloced, map_doit,
01051                               &args);
01052          if (__builtin_expect (err_str != NULL, 0))
01053            /* We don't free the returned string, the programs stops
01054               anyway.  */
01055            _exit (EXIT_FAILURE);
01056        }
01057       else
01058        {
01059          HP_TIMING_NOW (start);
01060          _dl_map_object (NULL, rtld_progname, 0, lt_library, 0,
01061                        __RTLD_OPENEXEC, LM_ID_BASE);
01062          HP_TIMING_NOW (stop);
01063 
01064          HP_TIMING_DIFF (load_time, start, stop);
01065        }
01066 
01067       /* Now the map for the main executable is available.  */
01068       main_map = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
01069 
01070       phdr = main_map->l_phdr;
01071       phnum = main_map->l_phnum;
01072       /* We overwrite here a pointer to a malloc()ed string.  But since
01073         the malloc() implementation used at this point is the dummy
01074         implementations which has no real free() function it does not
01075         makes sense to free the old string first.  */
01076       main_map->l_name = (char *) "";
01077       *user_entry = main_map->l_entry;
01078     }
01079   else
01080     {
01081       /* Create a link_map for the executable itself.
01082         This will be what dlopen on "" returns.  */
01083       main_map = _dl_new_object ((char *) "", "", lt_executable, NULL,
01084                              __RTLD_OPENEXEC, LM_ID_BASE);
01085       assert (main_map != NULL);
01086       assert (main_map == GL(dl_ns)[LM_ID_BASE]._ns_loaded);
01087       main_map->l_phdr = phdr;
01088       main_map->l_phnum = phnum;
01089       main_map->l_entry = *user_entry;
01090 
01091       /* At this point we are in a bit of trouble.  We would have to
01092         fill in the values for l_dev and l_ino.  But in general we
01093         do not know where the file is.  We also do not handle AT_EXECFD
01094         even if it would be passed up.
01095 
01096         We leave the values here defined to 0.  This is normally no
01097         problem as the program code itself is normally no shared
01098         object and therefore cannot be loaded dynamically.  Nothing
01099         prevent the use of dynamic binaries and in these situations
01100         we might get problems.  We might not be able to find out
01101         whether the object is already loaded.  But since there is no
01102         easy way out and because the dynamic binary must also not
01103         have an SONAME we ignore this program for now.  If it becomes
01104         a problem we can force people using SONAMEs.  */
01105 
01106       /* We delay initializing the path structure until we got the dynamic
01107         information for the program.  */
01108     }
01109 
01110   main_map->l_map_end = 0;
01111   main_map->l_text_end = 0;
01112   /* Perhaps the executable has no PT_LOAD header entries at all.  */
01113   main_map->l_map_start = ~0;
01114   /* And it was opened directly.  */
01115   ++main_map->l_direct_opencount;
01116 
01117   /* Scan the program header table for the dynamic section.  */
01118   for (ph = phdr; ph < &phdr[phnum]; ++ph)
01119     switch (ph->p_type)
01120       {
01121       case PT_PHDR:
01122        /* Find out the load address.  */
01123        main_map->l_addr = (ElfW(Addr)) phdr - ph->p_vaddr;
01124        break;
01125       case PT_DYNAMIC:
01126        /* This tells us where to find the dynamic section,
01127           which tells us everything we need to do.  */
01128        main_map->l_ld = (void *) main_map->l_addr + ph->p_vaddr;
01129        break;
01130       case PT_INTERP:
01131        /* This "interpreter segment" was used by the program loader to
01132           find the program interpreter, which is this program itself, the
01133           dynamic linker.  We note what name finds us, so that a future
01134           dlopen call or DT_NEEDED entry, for something that wants to link
01135           against the dynamic linker as a shared library, will know that
01136           the shared object is already loaded.  */
01137        _dl_rtld_libname.name = ((const char *) main_map->l_addr
01138                              + ph->p_vaddr);
01139        /* _dl_rtld_libname.next = NULL;   Already zero.  */
01140        GL(dl_rtld_map).l_libname = &_dl_rtld_libname;
01141 
01142        /* Ordinarilly, we would get additional names for the loader from
01143           our DT_SONAME.  This can't happen if we were actually linked as
01144           a static executable (detect this case when we have no DYNAMIC).
01145           If so, assume the filename component of the interpreter path to
01146           be our SONAME, and add it to our name list.  */
01147        if (GL(dl_rtld_map).l_ld == NULL)
01148          {
01149            const char *p = NULL;
01150            const char *cp = _dl_rtld_libname.name;
01151 
01152            /* Find the filename part of the path.  */
01153            while (*cp != '\0')
01154              if (*cp++ == '/')
01155               p = cp;
01156 
01157            if (p != NULL)
01158              {
01159               _dl_rtld_libname2.name = p;
01160               /* _dl_rtld_libname2.next = NULL;  Already zero.  */
01161               _dl_rtld_libname.next = &_dl_rtld_libname2;
01162              }
01163          }
01164 
01165        has_interp = true;
01166        break;
01167       case PT_LOAD:
01168        {
01169          ElfW(Addr) mapstart;
01170          ElfW(Addr) allocend;
01171 
01172          /* Remember where the main program starts in memory.  */
01173          mapstart = (main_map->l_addr
01174                     + (ph->p_vaddr & ~(GLRO(dl_pagesize) - 1)));
01175          if (main_map->l_map_start > mapstart)
01176            main_map->l_map_start = mapstart;
01177 
01178          /* Also where it ends.  */
01179          allocend = main_map->l_addr + ph->p_vaddr + ph->p_memsz;
01180          if (main_map->l_map_end < allocend)
01181            main_map->l_map_end = allocend;
01182          if ((ph->p_flags & PF_X) && allocend > main_map->l_text_end)
01183            main_map->l_text_end = allocend;
01184        }
01185        break;
01186 
01187       case PT_TLS:
01188        if (ph->p_memsz > 0)
01189          {
01190            /* Note that in the case the dynamic linker we duplicate work
01191               here since we read the PT_TLS entry already in
01192               _dl_start_final.  But the result is repeatable so do not
01193               check for this special but unimportant case.  */
01194            main_map->l_tls_blocksize = ph->p_memsz;
01195            main_map->l_tls_align = ph->p_align;
01196            if (ph->p_align == 0)
01197              main_map->l_tls_firstbyte_offset = 0;
01198            else
01199              main_map->l_tls_firstbyte_offset = (ph->p_vaddr
01200                                             & (ph->p_align - 1));
01201            main_map->l_tls_initimage_size = ph->p_filesz;
01202            main_map->l_tls_initimage = (void *) ph->p_vaddr;
01203 
01204            /* This image gets the ID one.  */
01205            GL(dl_tls_max_dtv_idx) = main_map->l_tls_modid = 1;
01206          }
01207        break;
01208 
01209       case PT_GNU_STACK:
01210        GL(dl_stack_flags) = ph->p_flags;
01211        break;
01212 
01213       case PT_GNU_RELRO:
01214        main_map->l_relro_addr = ph->p_vaddr;
01215        main_map->l_relro_size = ph->p_memsz;
01216        break;
01217       }
01218 
01219   /* Adjust the address of the TLS initialization image in case
01220      the executable is actually an ET_DYN object.  */
01221   if (main_map->l_tls_initimage != NULL)
01222     main_map->l_tls_initimage
01223       = (char *) main_map->l_tls_initimage + main_map->l_addr;
01224   if (! main_map->l_map_end)
01225     main_map->l_map_end = ~0;
01226   if (! main_map->l_text_end)
01227     main_map->l_text_end = ~0;
01228   if (! GL(dl_rtld_map).l_libname && GL(dl_rtld_map).l_name)
01229     {
01230       /* We were invoked directly, so the program might not have a
01231         PT_INTERP.  */
01232       _dl_rtld_libname.name = GL(dl_rtld_map).l_name;
01233       /* _dl_rtld_libname.next = NULL;    Already zero.  */
01234       GL(dl_rtld_map).l_libname =  &_dl_rtld_libname;
01235     }
01236   else
01237     assert (GL(dl_rtld_map).l_libname); /* How else did we get here?  */
01238 
01239   /* If the current libname is different from the SONAME, add the
01240      latter as well.  */
01241   if (GL(dl_rtld_map).l_info[DT_SONAME] != NULL
01242       && strcmp (GL(dl_rtld_map).l_libname->name,
01243                (const char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB])
01244                + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_val) != 0)
01245     {
01246       static struct libname_list newname;
01247       newname.name = ((char *) D_PTR (&GL(dl_rtld_map), l_info[DT_STRTAB])
01248                     + GL(dl_rtld_map).l_info[DT_SONAME]->d_un.d_ptr);
01249       newname.next = NULL;
01250       newname.dont_free = 1;
01251 
01252       assert (GL(dl_rtld_map).l_libname->next == NULL);
01253       GL(dl_rtld_map).l_libname->next = &newname;
01254     }
01255   /* The ld.so must be relocated since otherwise loading audit modules
01256      will fail since they reuse the very same ld.so.  */
01257   assert (GL(dl_rtld_map).l_relocated);
01258 
01259   if (! rtld_is_main)
01260     {
01261       /* Extract the contents of the dynamic section for easy access.  */
01262       elf_get_dynamic_info (main_map, NULL);
01263       /* Set up our cache of pointers into the hash table.  */
01264       _dl_setup_hash (main_map);
01265     }
01266 
01267   if (__builtin_expect (mode, normal) == verify)
01268     {
01269       /* We were called just to verify that this is a dynamic
01270         executable using us as the program interpreter.  Exit with an
01271         error if we were not able to load the binary or no interpreter
01272         is specified (i.e., this is no dynamically linked binary.  */
01273       if (main_map->l_ld == NULL)
01274        _exit (1);
01275 
01276       /* We allow here some platform specific code.  */
01277 #ifdef DISTINGUISH_LIB_VERSIONS
01278       DISTINGUISH_LIB_VERSIONS;
01279 #endif
01280       _exit (has_interp ? 0 : 2);
01281     }
01282 
01283   struct link_map **first_preload = &GL(dl_rtld_map).l_next;
01284 #if defined NEED_DL_SYSINFO || defined NEED_DL_SYSINFO_DSO
01285   /* Set up the data structures for the system-supplied DSO early,
01286      so they can influence _dl_init_paths.  */
01287   if (GLRO(dl_sysinfo_dso) != NULL)
01288     {
01289       /* Do an abridged version of the work _dl_map_object_from_fd would do
01290         to map in the object.  It's already mapped and prelinked (and
01291         better be, since it's read-only and so we couldn't relocate it).
01292         We just want our data structures to describe it as if we had just
01293         mapped and relocated it normally.  */
01294       struct link_map *l = _dl_new_object ((char *) "", "", lt_library, NULL,
01295                                       0, LM_ID_BASE);
01296       if (__builtin_expect (l != NULL, 1))
01297        {
01298          static ElfW(Dyn) dyn_temp[DL_RO_DYN_TEMP_CNT] attribute_relro;
01299 
01300          l->l_phdr = ((const void *) GLRO(dl_sysinfo_dso)
01301                      + GLRO(dl_sysinfo_dso)->e_phoff);
01302          l->l_phnum = GLRO(dl_sysinfo_dso)->e_phnum;
01303          for (uint_fast16_t i = 0; i < l->l_phnum; ++i)
01304            {
01305              const ElfW(Phdr) *const ph = &l->l_phdr[i];
01306              if (ph->p_type == PT_DYNAMIC)
01307               {
01308                 l->l_ld = (void *) ph->p_vaddr;
01309                 l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn));
01310               }
01311              else if (ph->p_type == PT_LOAD)
01312               {
01313                 if (! l->l_addr)
01314                   l->l_addr = ph->p_vaddr;
01315                 if (ph->p_vaddr + ph->p_memsz >= l->l_map_end)
01316                   l->l_map_end = ph->p_vaddr + ph->p_memsz;
01317                 if ((ph->p_flags & PF_X)
01318                         && ph->p_vaddr + ph->p_memsz >= l->l_text_end)
01319                   l->l_text_end = ph->p_vaddr + ph->p_memsz;
01320               }
01321              else
01322               /* There must be no TLS segment.  */
01323               assert (ph->p_type != PT_TLS);
01324            }
01325          l->l_map_start = (ElfW(Addr)) GLRO(dl_sysinfo_dso);
01326          l->l_addr = l->l_map_start - l->l_addr;
01327          l->l_map_end += l->l_addr;
01328          l->l_text_end += l->l_addr;
01329          l->l_ld = (void *) ((ElfW(Addr)) l->l_ld + l->l_addr);
01330          elf_get_dynamic_info (l, dyn_temp);
01331          _dl_setup_hash (l);
01332          l->l_relocated = 1;
01333 
01334          /* Initialize l_local_scope to contain just this map.  This allows
01335             the use of dl_lookup_symbol_x to resolve symbols within the vdso.
01336             So we create a single entry list pointing to l_real as its only
01337             element */
01338          l->l_local_scope[0]->r_nlist = 1;
01339          l->l_local_scope[0]->r_list = &l->l_real;
01340 
01341          /* Now that we have the info handy, use the DSO image's soname
01342             so this object can be looked up by name.  Note that we do not
01343             set l_name here.  That field gives the file name of the DSO,
01344             and this DSO is not associated with any file.  */
01345          if (l->l_info[DT_SONAME] != NULL)
01346            {
01347              /* Work around a kernel problem.  The kernel cannot handle
01348                addresses in the vsyscall DSO pages in writev() calls.  */
01349              const char *dsoname = ((char *) D_PTR (l, l_info[DT_STRTAB])
01350                                  + l->l_info[DT_SONAME]->d_un.d_val);
01351              size_t len = strlen (dsoname);
01352              char *copy = malloc (len);
01353              if (copy == NULL)
01354               _dl_fatal_printf ("out of memory\n");
01355              l->l_libname->name = memcpy (copy, dsoname, len);
01356            }
01357 
01358          /* Rearrange the list so this DSO appears after rtld_map.  */
01359          assert (l->l_next == NULL);
01360          assert (l->l_prev == main_map);
01361          GL(dl_rtld_map).l_next = l;
01362          l->l_prev = &GL(dl_rtld_map);
01363          first_preload = &l->l_next;
01364 
01365          /* We have a prelinked DSO preloaded by the system.  */
01366          GLRO(dl_sysinfo_map) = l;
01367 # ifdef NEED_DL_SYSINFO
01368          if (GLRO(dl_sysinfo) == DL_SYSINFO_DEFAULT)
01369            GLRO(dl_sysinfo) = GLRO(dl_sysinfo_dso)->e_entry + l->l_addr;
01370 # endif
01371        }
01372     }
01373 #endif
01374 
01375 #ifdef DL_SYSDEP_OSCHECK
01376   DL_SYSDEP_OSCHECK (dl_fatal);
01377 #endif
01378 
01379   /* Initialize the data structures for the search paths for shared
01380      objects.  */
01381   _dl_init_paths (library_path);
01382 
01383   /* Initialize _r_debug.  */
01384   struct r_debug *r = _dl_debug_initialize (GL(dl_rtld_map).l_addr,
01385                                        LM_ID_BASE);
01386   r->r_state = RT_CONSISTENT;
01387 
01388   /* Put the link_map for ourselves on the chain so it can be found by
01389      name.  Note that at this point the global chain of link maps contains
01390      exactly one element, which is pointed to by dl_loaded.  */
01391   if (! GL(dl_rtld_map).l_name)
01392     /* If not invoked directly, the dynamic linker shared object file was
01393        found by the PT_INTERP name.  */
01394     GL(dl_rtld_map).l_name = (char *) GL(dl_rtld_map).l_libname->name;
01395   GL(dl_rtld_map).l_type = lt_library;
01396   main_map->l_next = &GL(dl_rtld_map);
01397   GL(dl_rtld_map).l_prev = main_map;
01398   ++GL(dl_ns)[LM_ID_BASE]._ns_nloaded;
01399   ++GL(dl_load_adds);
01400 
01401   /* If LD_USE_LOAD_BIAS env variable has not been seen, default
01402      to not using bias for non-prelinked PIEs and libraries
01403      and using it for executables or prelinked PIEs or libraries.  */
01404   if (GLRO(dl_use_load_bias) == (ElfW(Addr)) -2)
01405     GLRO(dl_use_load_bias) = main_map->l_addr == 0 ? -1 : 0;
01406 
01407   /* Set up the program header information for the dynamic linker
01408      itself.  It is needed in the dl_iterate_phdr() callbacks.  */
01409   ElfW(Ehdr) *rtld_ehdr = (ElfW(Ehdr) *) GL(dl_rtld_map).l_map_start;
01410   ElfW(Phdr) *rtld_phdr = (ElfW(Phdr) *) (GL(dl_rtld_map).l_map_start
01411                                      + rtld_ehdr->e_phoff);
01412   GL(dl_rtld_map).l_phdr = rtld_phdr;
01413   GL(dl_rtld_map).l_phnum = rtld_ehdr->e_phnum;
01414 
01415 
01416   /* PT_GNU_RELRO is usually the last phdr.  */
01417   size_t cnt = rtld_ehdr->e_phnum;
01418   while (cnt-- > 0)
01419     if (rtld_phdr[cnt].p_type == PT_GNU_RELRO)
01420       {
01421        GL(dl_rtld_map).l_relro_addr = rtld_phdr[cnt].p_vaddr;
01422        GL(dl_rtld_map).l_relro_size = rtld_phdr[cnt].p_memsz;
01423        break;
01424       }
01425 
01426   /* Add the dynamic linker to the TLS list if it also uses TLS.  */
01427   if (GL(dl_rtld_map).l_tls_blocksize != 0)
01428     /* Assign a module ID.  Do this before loading any audit modules.  */
01429     GL(dl_rtld_map).l_tls_modid = _dl_next_tls_modid ();
01430 
01431   /* If we have auditing DSOs to load, do it now.  */
01432   if (__builtin_expect (audit_list != NULL, 0))
01433     {
01434       /* Iterate over all entries in the list.  The order is important.  */
01435       struct audit_ifaces *last_audit = NULL;
01436       struct audit_list *al = audit_list->next;
01437 
01438       /* Since we start using the auditing DSOs right away we need to
01439         initialize the data structures now.  */
01440       tcbp = init_tls ();
01441 
01442       /* Initialize security features.  We need to do it this early
01443         since otherwise the constructors of the audit libraries will
01444         use different values (especially the pointer guard) and will
01445         fail later on.  */
01446       security_init ();
01447 
01448       do
01449        {
01450          int tls_idx = GL(dl_tls_max_dtv_idx);
01451 
01452          /* Now it is time to determine the layout of the static TLS
01453             block and allocate it for the initial thread.  Note that we
01454             always allocate the static block, we never defer it even if
01455             no DF_STATIC_TLS bit is set.  The reason is that we know
01456             glibc will use the static model.  */
01457          struct dlmopen_args dlmargs;
01458          dlmargs.fname = al->name;
01459          dlmargs.map = NULL;
01460 
01461          const char *objname;
01462          const char *err_str = NULL;
01463          bool malloced;
01464          (void) _dl_catch_error (&objname, &err_str, &malloced, dlmopen_doit,
01465                               &dlmargs);
01466          if (__builtin_expect (err_str != NULL, 0))
01467            {
01468            not_loaded:
01469              _dl_error_printf ("\
01470 ERROR: ld.so: object '%s' cannot be loaded as audit interface: %s; ignored.\n",
01471                             al->name, err_str);
01472              if (malloced)
01473               free ((char *) err_str);
01474            }
01475          else
01476            {
01477              struct lookup_args largs;
01478              largs.name = "la_version";
01479              largs.map = dlmargs.map;
01480 
01481              /* Check whether the interface version matches.  */
01482              (void) _dl_catch_error (&objname, &err_str, &malloced,
01483                                   lookup_doit, &largs);
01484 
01485              unsigned int (*laversion) (unsigned int);
01486              unsigned int lav;
01487              if  (err_str == NULL
01488                  && (laversion = largs.result) != NULL
01489                  && (lav = laversion (LAV_CURRENT)) > 0
01490                  && lav <= LAV_CURRENT)
01491               {
01492                 /* Allocate structure for the callback function pointers.
01493                    This call can never fail.  */
01494                 union
01495                 {
01496                   struct audit_ifaces ifaces;
01497 #define naudit_ifaces 8
01498                   void (*fptr[naudit_ifaces]) (void);
01499                 } *newp = malloc (sizeof (*newp));
01500 
01501                 /* Names of the auditing interfaces.  All in one
01502                    long string.  */
01503                 static const char audit_iface_names[] =
01504                   "la_activity\0"
01505                   "la_objsearch\0"
01506                   "la_objopen\0"
01507                   "la_preinit\0"
01508 #if __ELF_NATIVE_CLASS == 32
01509                   "la_symbind32\0"
01510 #elif __ELF_NATIVE_CLASS == 64
01511                   "la_symbind64\0"
01512 #else
01513 # error "__ELF_NATIVE_CLASS must be defined"
01514 #endif
01515 #define STRING(s) __STRING (s)
01516                   "la_" STRING (ARCH_LA_PLTENTER) "\0"
01517                   "la_" STRING (ARCH_LA_PLTEXIT) "\0"
01518                   "la_objclose\0";
01519                 unsigned int cnt = 0;
01520                 const char *cp = audit_iface_names;
01521                 do
01522                   {
01523                     largs.name = cp;
01524                     (void) _dl_catch_error (&objname, &err_str, &malloced,
01525                                          lookup_doit, &largs);
01526 
01527                     /* Store the pointer.  */
01528                     if (err_str == NULL && largs.result != NULL)
01529                      {
01530                        newp->fptr[cnt] = largs.result;
01531 
01532                        /* The dynamic linker link map is statically
01533                           allocated, initialize the data now.   */
01534                        GL(dl_rtld_map).l_audit[cnt].cookie
01535                          = (intptr_t) &GL(dl_rtld_map);
01536                      }
01537                     else
01538                      newp->fptr[cnt] = NULL;
01539                     ++cnt;
01540 
01541                     cp = (char *) rawmemchr (cp, '\0') + 1;
01542                   }
01543                 while (*cp != '\0');
01544                 assert (cnt == naudit_ifaces);
01545 
01546                 /* Now append the new auditing interface to the list.  */
01547                 newp->ifaces.next = NULL;
01548                 if (last_audit == NULL)
01549                   last_audit = GLRO(dl_audit) = &newp->ifaces;
01550                 else
01551                   last_audit = last_audit->next = &newp->ifaces;
01552                 ++GLRO(dl_naudit);
01553 
01554                 /* Mark the DSO as being used for auditing.  */
01555                 dlmargs.map->l_auditing = 1;
01556               }
01557              else
01558               {
01559                 /* We cannot use the DSO, it does not have the
01560                    appropriate interfaces or it expects something
01561                    more recent.  */
01562 #ifndef NDEBUG
01563                 Lmid_t ns = dlmargs.map->l_ns;
01564 #endif
01565                 _dl_close (dlmargs.map);
01566 
01567                 /* Make sure the namespace has been cleared entirely.  */
01568                 assert (GL(dl_ns)[ns]._ns_loaded == NULL);
01569                 assert (GL(dl_ns)[ns]._ns_nloaded == 0);
01570 
01571                 GL(dl_tls_max_dtv_idx) = tls_idx;
01572                 goto not_loaded;
01573               }
01574            }
01575 
01576          al = al->next;
01577        }
01578       while (al != audit_list->next);
01579 
01580       /* If we have any auditing modules, announce that we already
01581         have two objects loaded.  */
01582       if (__builtin_expect (GLRO(dl_naudit) > 0, 0))
01583        {
01584          struct link_map *ls[2] = { main_map, &GL(dl_rtld_map) };
01585 
01586          for (unsigned int outer = 0; outer < 2; ++outer)
01587            {
01588              struct audit_ifaces *afct = GLRO(dl_audit);
01589              for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
01590               {
01591                 if (afct->objopen != NULL)
01592                   {
01593                     ls[outer]->l_audit[cnt].bindflags
01594                      = afct->objopen (ls[outer], LM_ID_BASE,
01595                                     &ls[outer]->l_audit[cnt].cookie);
01596 
01597                     ls[outer]->l_audit_any_plt
01598                      |= ls[outer]->l_audit[cnt].bindflags != 0;
01599                   }
01600 
01601                 afct = afct->next;
01602               }
01603            }
01604        }
01605     }
01606 
01607   /* Set up debugging before the debugger is notified for the first time.  */
01608 #ifdef ELF_MACHINE_DEBUG_SETUP
01609   /* Some machines (e.g. MIPS) don't use DT_DEBUG in this way.  */
01610   ELF_MACHINE_DEBUG_SETUP (main_map, r);
01611   ELF_MACHINE_DEBUG_SETUP (&GL(dl_rtld_map), r);
01612 #else
01613   if (main_map->l_info[DT_DEBUG] != NULL)
01614     /* There is a DT_DEBUG entry in the dynamic section.  Fill it in
01615        with the run-time address of the r_debug structure  */
01616     main_map->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
01617 
01618   /* Fill in the pointer in the dynamic linker's own dynamic section, in
01619      case you run gdb on the dynamic linker directly.  */
01620   if (GL(dl_rtld_map).l_info[DT_DEBUG] != NULL)
01621     GL(dl_rtld_map).l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) r;
01622 #endif
01623 
01624   /* We start adding objects.  */
01625   r->r_state = RT_ADD;
01626   _dl_debug_state ();
01627 
01628   /* Auditing checkpoint: we are ready to signal that the initial map
01629      is being constructed.  */
01630   if (__builtin_expect (GLRO(dl_naudit) > 0, 0))
01631     {
01632       struct audit_ifaces *afct = GLRO(dl_audit);
01633       for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
01634        {
01635          if (afct->activity != NULL)
01636            afct->activity (&main_map->l_audit[cnt].cookie, LA_ACT_ADD);
01637 
01638          afct = afct->next;
01639        }
01640     }
01641 
01642   /* We have two ways to specify objects to preload: via environment
01643      variable and via the file /etc/ld.so.preload.  The latter can also
01644      be used when security is enabled.  */
01645   assert (*first_preload == NULL);
01646   struct link_map **preloads = NULL;
01647   unsigned int npreloads = 0;
01648 
01649   if (__builtin_expect (preloadlist != NULL, 0))
01650     {
01651       /* The LD_PRELOAD environment variable gives list of libraries
01652         separated by white space or colons that are loaded before the
01653         executable's dependencies and prepended to the global scope
01654         list.  If the binary is running setuid all elements
01655         containing a '/' are ignored since it is insecure.  */
01656       char *list = strdupa (preloadlist);
01657       char *p;
01658 
01659       HP_TIMING_NOW (start);
01660 
01661       /* Prevent optimizing strsep.  Speed is not important here.  */
01662       while ((p = (strsep) (&list, " :")) != NULL)
01663        if (p[0] != '\0'
01664            && (__builtin_expect (! INTUSE(__libc_enable_secure), 1)
01665               || strchr (p, '/') == NULL))
01666          npreloads += do_preload (p, main_map, "LD_PRELOAD");
01667 
01668       HP_TIMING_NOW (stop);
01669       HP_TIMING_DIFF (diff, start, stop);
01670       HP_TIMING_ACCUM_NT (load_time, diff);
01671     }
01672 
01673   /* There usually is no ld.so.preload file, it should only be used
01674      for emergencies and testing.  So the open call etc should usually
01675      fail.  Using access() on a non-existing file is faster than using
01676      open().  So we do this first.  If it succeeds we do almost twice
01677      the work but this does not matter, since it is not for production
01678      use.  */
01679   static const char preload_file[] = "/etc/ld.so.preload";
01680   if (__builtin_expect (__access (preload_file, R_OK) == 0, 0))
01681     {
01682       /* Read the contents of the file.  */
01683       file = _dl_sysdep_read_whole_file (preload_file, &file_size,
01684                                     PROT_READ | PROT_WRITE);
01685       if (__builtin_expect (file != MAP_FAILED, 0))
01686        {
01687          /* Parse the file.  It contains names of libraries to be loaded,
01688             separated by white spaces or `:'.  It may also contain
01689             comments introduced by `#'.  */
01690          char *problem;
01691          char *runp;
01692          size_t rest;
01693 
01694          /* Eliminate comments.  */
01695          runp = file;
01696          rest = file_size;
01697          while (rest > 0)
01698            {
01699              char *comment = memchr (runp, '#', rest);
01700              if (comment == NULL)
01701               break;
01702 
01703              rest -= comment - runp;
01704              do
01705               *comment = ' ';
01706              while (--rest > 0 && *++comment != '\n');
01707            }
01708 
01709          /* We have one problematic case: if we have a name at the end of
01710             the file without a trailing terminating characters, we cannot
01711             place the \0.  Handle the case separately.  */
01712          if (file[file_size - 1] != ' ' && file[file_size - 1] != '\t'
01713              && file[file_size - 1] != '\n' && file[file_size - 1] != ':')
01714            {
01715              problem = &file[file_size];
01716              while (problem > file && problem[-1] != ' '
01717                    && problem[-1] != '\t'
01718                    && problem[-1] != '\n' && problem[-1] != ':')
01719               --problem;
01720 
01721              if (problem > file)
01722               problem[-1] = '\0';
01723            }
01724          else
01725            {
01726              problem = NULL;
01727              file[file_size - 1] = '\0';
01728            }
01729 
01730          HP_TIMING_NOW (start);
01731 
01732          if (file != problem)
01733            {
01734              char *p;
01735              runp = file;
01736              while ((p = strsep (&runp, ": \t\n")) != NULL)
01737               if (p[0] != '\0')
01738                 npreloads += do_preload (p, main_map, preload_file);
01739            }
01740 
01741          if (problem != NULL)
01742            {
01743              char *p = strndupa (problem, file_size - (problem - file));
01744 
01745              npreloads += do_preload (p, main_map, preload_file);
01746            }
01747 
01748          HP_TIMING_NOW (stop);
01749          HP_TIMING_DIFF (diff, start, stop);
01750          HP_TIMING_ACCUM_NT (load_time, diff);
01751 
01752          /* We don't need the file anymore.  */
01753          __munmap (file, file_size);
01754        }
01755     }
01756 
01757   if (__builtin_expect (*first_preload != NULL, 0))
01758     {
01759       /* Set up PRELOADS with a vector of the preloaded libraries.  */
01760       struct link_map *l = *first_preload;
01761       preloads = __alloca (npreloads * sizeof preloads[0]);
01762       i = 0;
01763       do
01764        {
01765          preloads[i++] = l;
01766          l = l->l_next;
01767        } while (l);
01768       assert (i == npreloads);
01769     }
01770 
01771   /* Load all the libraries specified by DT_NEEDED entries.  If LD_PRELOAD
01772      specified some libraries to load, these are inserted before the actual
01773      dependencies in the executable's searchlist for symbol resolution.  */
01774   HP_TIMING_NOW (start);
01775   _dl_map_object_deps (main_map, preloads, npreloads, mode == trace, 0);
01776   HP_TIMING_NOW (stop);
01777   HP_TIMING_DIFF (diff, start, stop);
01778   HP_TIMING_ACCUM_NT (load_time, diff);
01779 
01780   /* Mark all objects as being in the global scope.  */
01781   for (i = main_map->l_searchlist.r_nlist; i > 0; )
01782     main_map->l_searchlist.r_list[--i]->l_global = 1;
01783 
01784 #ifndef MAP_ANON
01785   /* We are done mapping things, so close the zero-fill descriptor.  */
01786   __close (_dl_zerofd);
01787   _dl_zerofd = -1;
01788 #endif
01789 
01790   /* Remove _dl_rtld_map from the chain.  */
01791   GL(dl_rtld_map).l_prev->l_next = GL(dl_rtld_map).l_next;
01792   if (GL(dl_rtld_map).l_next != NULL)
01793     GL(dl_rtld_map).l_next->l_prev = GL(dl_rtld_map).l_prev;
01794 
01795   for (i = 1; i < main_map->l_searchlist.r_nlist; ++i)
01796     if (main_map->l_searchlist.r_list[i] == &GL(dl_rtld_map))
01797       break;
01798 
01799   bool rtld_multiple_ref = false;
01800   if (__builtin_expect (i < main_map->l_searchlist.r_nlist, 1))
01801     {
01802       /* Some DT_NEEDED entry referred to the interpreter object itself, so
01803         put it back in the list of visible objects.  We insert it into the
01804         chain in symbol search order because gdb uses the chain's order as
01805         its symbol search order.  */
01806       rtld_multiple_ref = true;
01807 
01808       GL(dl_rtld_map).l_prev = main_map->l_searchlist.r_list[i - 1];
01809       if (__builtin_expect (mode, normal) == normal)
01810        {
01811          GL(dl_rtld_map).l_next = (i + 1 < main_map->l_searchlist.r_nlist
01812                                 ? main_map->l_searchlist.r_list[i + 1]
01813                                 : NULL);
01814 #if defined NEED_DL_SYSINFO || defined NEED_DL_SYSINFO_DSO
01815          if (GLRO(dl_sysinfo_map) != NULL
01816              && GL(dl_rtld_map).l_prev->l_next == GLRO(dl_sysinfo_map)
01817              && GL(dl_rtld_map).l_next != GLRO(dl_sysinfo_map))
01818            GL(dl_rtld_map).l_prev = GLRO(dl_sysinfo_map);
01819 #endif
01820        }
01821       else
01822        /* In trace mode there might be an invisible object (which we
01823           could not find) after the previous one in the search list.
01824           In this case it doesn't matter much where we put the
01825           interpreter object, so we just initialize the list pointer so
01826           that the assertion below holds.  */
01827        GL(dl_rtld_map).l_next = GL(dl_rtld_map).l_prev->l_next;
01828 
01829       assert (GL(dl_rtld_map).l_prev->l_next == GL(dl_rtld_map).l_next);
01830       GL(dl_rtld_map).l_prev->l_next = &GL(dl_rtld_map);
01831       if (GL(dl_rtld_map).l_next != NULL)
01832        {
01833          assert (GL(dl_rtld_map).l_next->l_prev == GL(dl_rtld_map).l_prev);
01834          GL(dl_rtld_map).l_next->l_prev = &GL(dl_rtld_map);
01835        }
01836     }
01837 
01838   /* Now let us see whether all libraries are available in the
01839      versions we need.  */
01840   {
01841     struct version_check_args args;
01842     args.doexit = mode == normal;
01843     args.dotrace = mode == trace;
01844     _dl_receive_error (print_missing_version, version_check_doit, &args);
01845   }
01846 
01847   /* We do not initialize any of the TLS functionality unless any of the
01848      initial modules uses TLS.  This makes dynamic loading of modules with
01849      TLS impossible, but to support it requires either eagerly doing setup
01850      now or lazily doing it later.  Doing it now makes us incompatible with
01851      an old kernel that can't perform TLS_INIT_TP, even if no TLS is ever
01852      used.  Trying to do it lazily is too hairy to try when there could be
01853      multiple threads (from a non-TLS-using libpthread).  */
01854   bool was_tls_init_tp_called = tls_init_tp_called;
01855   if (tcbp == NULL)
01856     tcbp = init_tls ();
01857 
01858   if (__builtin_expect (audit_list == NULL, 1))
01859     /* Initialize security features.  But only if we have not done it
01860        earlier.  */
01861     security_init ();
01862 
01863   if (__builtin_expect (mode, normal) != normal)
01864     {
01865       /* We were run just to list the shared libraries.  It is
01866         important that we do this before real relocation, because the
01867         functions we call below for output may no longer work properly
01868         after relocation.  */
01869       struct link_map *l;
01870 
01871       if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
01872        {
01873          struct r_scope_elem *scope = &main_map->l_searchlist;
01874 
01875          for (i = 0; i < scope->r_nlist; i++)
01876            {
01877              l = scope->r_list [i];
01878              if (l->l_faked)
01879               {
01880                 _dl_printf ("\t%s => not found\n", l->l_libname->name);
01881                 continue;
01882               }
01883              if (_dl_name_match_p (GLRO(dl_trace_prelink), l))
01884               GLRO(dl_trace_prelink_map) = l;
01885              _dl_printf ("\t%s => %s (0x%0*Zx, 0x%0*Zx)",
01886                        l->l_libname->name[0] ? l->l_libname->name
01887                        : rtld_progname ?: "<main program>",
01888                        l->l_name[0] ? l->l_name
01889                        : rtld_progname ?: "<main program>",
01890                        (int) sizeof l->l_map_start * 2,
01891                        (size_t) l->l_map_start,
01892                        (int) sizeof l->l_addr * 2,
01893                        (size_t) l->l_addr);
01894 
01895              if (l->l_tls_modid)
01896               _dl_printf (" TLS(0x%Zx, 0x%0*Zx)\n", l->l_tls_modid,
01897                          (int) sizeof l->l_tls_offset * 2,
01898                          (size_t) l->l_tls_offset);
01899              else
01900               _dl_printf ("\n");
01901            }
01902        }
01903       else if (GLRO(dl_debug_mask) & DL_DEBUG_UNUSED)
01904        {
01905          /* Look through the dependencies of the main executable
01906             and determine which of them is not actually
01907             required.  */
01908          struct link_map *l = main_map;
01909 
01910          /* Relocate the main executable.  */
01911          struct relocate_args args = { .l = l, .lazy = GLRO(dl_lazy) };
01912          _dl_receive_error (print_unresolved, relocate_doit, &args);
01913 
01914          /* This loop depends on the dependencies of the executable to
01915             correspond in number and order to the DT_NEEDED entries.  */
01916          ElfW(Dyn) *dyn = main_map->l_ld;
01917          bool first = true;
01918          while (dyn->d_tag != DT_NULL)
01919            {
01920              if (dyn->d_tag == DT_NEEDED)
01921               {
01922                 l = l->l_next;
01923 
01924                 if (!l->l_used)
01925                   {
01926                     if (first)
01927                      {
01928                        _dl_printf ("Unused direct dependencies:\n");
01929                        first = false;
01930                      }
01931 
01932                     _dl_printf ("\t%s\n", l->l_name);
01933                   }
01934               }
01935 
01936              ++dyn;
01937            }
01938 
01939          _exit (first != true);
01940        }
01941       else if (! main_map->l_info[DT_NEEDED])
01942        _dl_printf ("\tstatically linked\n");
01943       else
01944        {
01945          for (l = main_map->l_next; l; l = l->l_next)
01946            if (l->l_faked)
01947              /* The library was not found.  */
01948              _dl_printf ("\t%s => not found\n", l->l_libname->name);
01949            else if (strcmp (l->l_libname->name, l->l_name) == 0)
01950              _dl_printf ("\t%s (0x%0*Zx)\n", l->l_libname->name,
01951                        (int) sizeof l->l_map_start * 2,
01952                        (size_t) l->l_map_start);
01953            else
01954              _dl_printf ("\t%s => %s (0x%0*Zx)\n", l->l_libname->name,
01955                        l->l_name, (int) sizeof l->l_map_start * 2,
01956                        (size_t) l->l_map_start);
01957        }
01958 
01959       if (__builtin_expect (mode, trace) != trace)
01960        for (i = 1; i < (unsigned int) _dl_argc; ++i)
01961          {
01962            const ElfW(Sym) *ref = NULL;
01963            ElfW(Addr) loadbase;
01964            lookup_t result;
01965 
01966            result = _dl_lookup_symbol_x (INTUSE(_dl_argv)[i], main_map,
01967                                      &ref, main_map->l_scope,
01968                                      NULL, ELF_RTYPE_CLASS_PLT,
01969                                      DL_LOOKUP_ADD_DEPENDENCY, NULL);
01970 
01971            loadbase = LOOKUP_VALUE_ADDRESS (result);
01972 
01973            _dl_printf ("%s found at 0x%0*Zd in object at 0x%0*Zd\n",
01974                      INTUSE(_dl_argv)[i],
01975                      (int) sizeof ref->st_value * 2,
01976                      (size_t) ref->st_value,
01977                      (int) sizeof loadbase * 2, (size_t) loadbase);
01978          }
01979       else
01980        {
01981          /* If LD_WARN is set, warn about undefined symbols.  */
01982          if (GLRO(dl_lazy) >= 0 && GLRO(dl_verbose))
01983            {
01984              /* We have to do symbol dependency testing.  */
01985              struct relocate_args args;
01986              struct link_map *l;
01987 
01988              args.lazy = GLRO(dl_lazy);
01989 
01990              l = main_map;
01991              while (l->l_next != NULL)
01992               l = l->l_next;
01993              do
01994               {
01995                 if (l != &GL(dl_rtld_map) && ! l->l_faked)
01996                   {
01997                     args.l = l;
01998                     _dl_receive_error (print_unresolved, relocate_doit,
01999                                     &args);
02000                   }
02001                 l = l->l_prev;
02002               }
02003              while (l != NULL);
02004 
02005              if ((GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
02006                 && rtld_multiple_ref)
02007               {
02008                 /* Mark the link map as not yet relocated again.  */
02009                 GL(dl_rtld_map).l_relocated = 0;
02010                 _dl_relocate_object (&GL(dl_rtld_map),
02011                                    main_map->l_scope, 0, 0);
02012               }
02013             }
02014 #define VERNEEDTAG (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (DT_VERNEED))
02015          if (version_info)
02016            {
02017              /* Print more information.  This means here, print information
02018                about the versions needed.  */
02019              int first = 1;
02020              struct link_map *map;
02021 
02022              for (map = main_map; map != NULL; map = map->l_next)
02023               {
02024                 const char *strtab;
02025                 ElfW(Dyn) *dyn = map->l_info[VERNEEDTAG];
02026                 ElfW(Verneed) *ent;
02027 
02028                 if (dyn == NULL)
02029                   continue;
02030 
02031                 strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
02032                 ent = (ElfW(Verneed) *) (map->l_addr + dyn->d_un.d_ptr);
02033 
02034                 if (first)
02035                   {
02036                     _dl_printf ("\n\tVersion information:\n");
02037                     first = 0;
02038                   }
02039 
02040                 _dl_printf ("\t%s:\n",
02041                            map->l_name[0] ? map->l_name : rtld_progname);
02042 
02043                 while (1)
02044                   {
02045                     ElfW(Vernaux) *aux;
02046                     struct link_map *needed;
02047 
02048                     needed = find_needed (strtab + ent->vn_file);
02049                     aux = (ElfW(Vernaux) *) ((char *) ent + ent->vn_aux);
02050 
02051                     while (1)
02052                      {
02053                        const char *fname = NULL;
02054 
02055                        if (needed != NULL
02056                            && match_version (strtab + aux->vna_name,
02057                                           needed))
02058                          fname = needed->l_name;
02059 
02060                        _dl_printf ("\t\t%s (%s) %s=> %s\n",
02061                                   strtab + ent->vn_file,
02062                                   strtab + aux->vna_name,
02063                                   aux->vna_flags & VER_FLG_WEAK
02064                                   ? "[WEAK] " : "",
02065                                   fname ?: "not found");
02066 
02067                        if (aux->vna_next == 0)
02068                          /* No more symbols.  */
02069                          break;
02070 
02071                        /* Next symbol.  */
02072                        aux = (ElfW(Vernaux) *) ((char *) aux
02073                                              + aux->vna_next);
02074                      }
02075 
02076                     if (ent->vn_next == 0)
02077                      /* No more dependencies.  */
02078                      break;
02079 
02080                     /* Next dependency.  */
02081                     ent = (ElfW(Verneed) *) ((char *) ent + ent->vn_next);
02082                   }
02083               }
02084            }
02085        }
02086 
02087       _exit (0);
02088     }
02089 
02090   if (main_map->l_info[ADDRIDX (DT_GNU_LIBLIST)]
02091       && ! __builtin_expect (GLRO(dl_profile) != NULL, 0)
02092       && ! __builtin_expect (GLRO(dl_dynamic_weak), 0))
02093     {
02094       ElfW(Lib) *liblist, *liblistend;
02095       struct link_map **r_list, **r_listend, *l;
02096       const char *strtab = (const void *) D_PTR (main_map, l_info[DT_STRTAB]);
02097 
02098       assert (main_map->l_info[VALIDX (DT_GNU_LIBLISTSZ)] != NULL);
02099       liblist = (ElfW(Lib) *)
02100               main_map->l_info[ADDRIDX (DT_GNU_LIBLIST)]->d_un.d_ptr;
02101       liblistend = (ElfW(Lib) *)
02102                  ((char *) liblist +
02103                   main_map->l_info[VALIDX (DT_GNU_LIBLISTSZ)]->d_un.d_val);
02104       r_list = main_map->l_searchlist.r_list;
02105       r_listend = r_list + main_map->l_searchlist.r_nlist;
02106 
02107       for (; r_list < r_listend && liblist < liblistend; r_list++)
02108        {
02109          l = *r_list;
02110 
02111          if (l == main_map)
02112            continue;
02113 
02114          /* If the library is not mapped where it should, fail.  */
02115          if (l->l_addr)
02116            break;
02117 
02118          /* Next, check if checksum matches.  */
02119          if (l->l_info [VALIDX(DT_CHECKSUM)] == NULL
02120              || l->l_info [VALIDX(DT_CHECKSUM)]->d_un.d_val
02121                != liblist->l_checksum)
02122            break;
02123 
02124          if (l->l_info [VALIDX(DT_GNU_PRELINKED)] == NULL
02125              || l->l_info [VALIDX(DT_GNU_PRELINKED)]->d_un.d_val
02126                != liblist->l_time_stamp)
02127            break;
02128 
02129          if (! _dl_name_match_p (strtab + liblist->l_name, l))
02130            break;
02131 
02132          ++liblist;
02133        }
02134 
02135 
02136       if (r_list == r_listend && liblist == liblistend)
02137        prelinked = true;
02138 
02139       if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_LIBS, 0))
02140        _dl_debug_printf ("\nprelink checking: %s\n",
02141                        prelinked ? "ok" : "failed");
02142     }
02143 
02144 
02145   /* Now set up the variable which helps the assembler startup code.  */
02146   GL(dl_ns)[LM_ID_BASE]._ns_main_searchlist = &main_map->l_searchlist;
02147 
02148   /* Save the information about the original global scope list since
02149      we need it in the memory handling later.  */
02150   GLRO(dl_initial_searchlist) = *GL(dl_ns)[LM_ID_BASE]._ns_main_searchlist;
02151 
02152   if (prelinked)
02153     {
02154       if (main_map->l_info [ADDRIDX (DT_GNU_CONFLICT)] != NULL)
02155        {
02156          ElfW(Rela) *conflict, *conflictend;
02157 #ifndef HP_TIMING_NONAVAIL
02158          hp_timing_t start;
02159          hp_timing_t stop;
02160 #endif
02161 
02162          HP_TIMING_NOW (start);
02163          assert (main_map->l_info [VALIDX (DT_GNU_CONFLICTSZ)] != NULL);
02164          conflict = (ElfW(Rela) *)
02165            main_map->l_info [ADDRIDX (DT_GNU_CONFLICT)]->d_un.d_ptr;
02166          conflictend = (ElfW(Rela) *)
02167            ((char *) conflict
02168             + main_map->l_info [VALIDX (DT_GNU_CONFLICTSZ)]->d_un.d_val);
02169          _dl_resolve_conflicts (main_map, conflict, conflictend);
02170          HP_TIMING_NOW (stop);
02171          HP_TIMING_DIFF (relocate_time, start, stop);
02172        }
02173 
02174 
02175       /* Mark all the objects so we know they have been already relocated.  */
02176       for (struct link_map *l = main_map; l != NULL; l = l->l_next)
02177        {
02178          l->l_relocated = 1;
02179          if (l->l_relro_size)
02180            _dl_protect_relro (l);
02181 
02182          /* Add object to slot information data if necessasy.  */
02183          if (l->l_tls_blocksize != 0 && tls_init_tp_called)
02184            _dl_add_to_slotinfo (l);
02185        }
02186 
02187       _dl_sysdep_start_cleanup ();
02188     }
02189   else
02190     {
02191       /* Now we have all the objects loaded.  Relocate them all except for
02192         the dynamic linker itself.  We do this in reverse order so that copy
02193         relocs of earlier objects overwrite the data written by later
02194         objects.  We do not re-relocate the dynamic linker itself in this
02195         loop because that could result in the GOT entries for functions we
02196         call being changed, and that would break us.  It is safe to relocate
02197         the dynamic linker out of order because it has no copy relocs (we
02198         know that because it is self-contained).  */
02199 
02200       int consider_profiling = GLRO(dl_profile) != NULL;
02201 #ifndef HP_TIMING_NONAVAIL
02202       hp_timing_t start;
02203       hp_timing_t stop;
02204 #endif
02205 
02206       /* If we are profiling we also must do lazy reloaction.  */
02207       GLRO(dl_lazy) |= consider_profiling;
02208 
02209       struct link_map *l = main_map;
02210       while (l->l_next)
02211        l = l->l_next;
02212 
02213       HP_TIMING_NOW (start);
02214       do
02215        {
02216          /* While we are at it, help the memory handling a bit.  We have to
02217             mark some data structures as allocated with the fake malloc()
02218             implementation in ld.so.  */
02219          struct libname_list *lnp = l->l_libname->next;
02220 
02221          while (__builtin_expect (lnp != NULL, 0))
02222            {
02223              lnp->dont_free = 1;
02224              lnp = lnp->next;
02225            }
02226 
02227          if (l != &GL(dl_rtld_map))
02228            _dl_relocate_object (l, l->l_scope, GLRO(dl_lazy),
02229                              consider_profiling);
02230 
02231          /* Add object to slot information data if necessasy.  */
02232          if (l->l_tls_blocksize != 0 && tls_init_tp_called)
02233            _dl_add_to_slotinfo (l);
02234 
02235          l = l->l_prev;
02236        }
02237       while (l);
02238       HP_TIMING_NOW (stop);
02239 
02240       HP_TIMING_DIFF (relocate_time, start, stop);
02241 
02242       /* Do any necessary cleanups for the startup OS interface code.
02243         We do these now so that no calls are made after rtld re-relocation
02244         which might be resolved to different functions than we expect.
02245         We cannot do this before relocating the other objects because
02246         _dl_relocate_object might need to call `mprotect' for DT_TEXTREL.  */
02247       _dl_sysdep_start_cleanup ();
02248 
02249       /* Now enable profiling if needed.  Like the previous call,
02250         this has to go here because the calls it makes should use the
02251         rtld versions of the functions (particularly calloc()), but it
02252         needs to have _dl_profile_map set up by the relocator.  */
02253       if (__builtin_expect (GL(dl_profile_map) != NULL, 0))
02254        /* We must prepare the profiling.  */
02255        _dl_start_profile ();
02256     }
02257 
02258 #ifndef NONTLS_INIT_TP
02259 # define NONTLS_INIT_TP do { } while (0)
02260 #endif
02261 
02262   if (!was_tls_init_tp_called && GL(dl_tls_max_dtv_idx) > 0)
02263     ++GL(dl_tls_generation);
02264 
02265   /* Now that we have completed relocation, the initializer data
02266      for the TLS blocks has its final values and we can copy them
02267      into the main thread's TLS area, which we allocated above.  */
02268   _dl_allocate_tls_init (tcbp);
02269 
02270   /* And finally install it for the main thread.  If ld.so itself uses
02271      TLS we know the thread pointer was initialized earlier.  */
02272   if (! tls_init_tp_called)
02273     {
02274       const char *lossage = TLS_INIT_TP (tcbp, USE___THREAD);
02275       if (__builtin_expect (lossage != NULL, 0))
02276        _dl_fatal_printf ("cannot set up thread-local storage: %s\n",
02277                        lossage);
02278     }
02279 
02280   if (! prelinked && rtld_multiple_ref)
02281     {
02282       /* There was an explicit ref to the dynamic linker as a shared lib.
02283         Re-relocate ourselves with user-controlled symbol definitions.
02284 
02285         We must do this after TLS initialization in case after this
02286         re-relocation, we might call a user-supplied function
02287         (e.g. calloc from _dl_relocate_object) that uses TLS data.  */
02288 
02289 #ifndef HP_TIMING_NONAVAIL
02290       hp_timing_t start;
02291       hp_timing_t stop;
02292       hp_timing_t add;
02293 #endif
02294 
02295       HP_TIMING_NOW (start);
02296       /* Mark the link map as not yet relocated again.  */
02297       GL(dl_rtld_map).l_relocated = 0;
02298       _dl_relocate_object (&GL(dl_rtld_map), main_map->l_scope, 0, 0);
02299       HP_TIMING_NOW (stop);
02300       HP_TIMING_DIFF (add, start, stop);
02301       HP_TIMING_ACCUM_NT (relocate_time, add);
02302     }
02303 
02304 #ifdef SHARED
02305   /* Auditing checkpoint: we have added all objects.  */
02306   if (__builtin_expect (GLRO(dl_naudit) > 0, 0))
02307     {
02308       struct link_map *head = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
02309       /* Do not call the functions for any auditing object.  */
02310       if (head->l_auditing == 0)
02311        {
02312          struct audit_ifaces *afct = GLRO(dl_audit);
02313          for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
02314            {
02315              if (afct->activity != NULL)
02316               afct->activity (&head->l_audit[cnt].cookie, LA_ACT_CONSISTENT);
02317 
02318              afct = afct->next;
02319            }
02320        }
02321     }
02322 #endif
02323 
02324   /* Notify the debugger all new objects are now ready to go.  We must re-get
02325      the address since by now the variable might be in another object.  */
02326   r = _dl_debug_initialize (0, LM_ID_BASE);
02327   r->r_state = RT_CONSISTENT;
02328   _dl_debug_state ();
02329 
02330 #ifndef MAP_COPY
02331   /* We must munmap() the cache file.  */
02332   _dl_unload_cache ();
02333 #endif
02334 
02335   /* Once we return, _dl_sysdep_start will invoke
02336      the DT_INIT functions and then *USER_ENTRY.  */
02337 }
02338 
02339 /* This is a little helper function for resolving symbols while
02340    tracing the binary.  */
02341 static void
02342 print_unresolved (int errcode __attribute__ ((unused)), const char *objname,
02343                 const char *errstring)
02344 {
02345   if (objname[0] == '\0')
02346     objname = rtld_progname ?: "<main program>";
02347   _dl_error_printf ("%s     (%s)\n", errstring, objname);
02348 }
02349 
02350 /* This is a little helper function for resolving symbols while
02351    tracing the binary.  */
02352 static void
02353 print_missing_version (int errcode __attribute__ ((unused)),
02354                      const char *objname, const char *errstring)
02355 {
02356   _dl_error_printf ("%s: %s: %s\n", rtld_progname ?: "<program name unknown>",
02357                   objname, errstring);
02358 }
02359 
02360 /* Nonzero if any of the debugging options is enabled.  */
02361 static int any_debug attribute_relro;
02362 
02363 /* Process the string given as the parameter which explains which debugging
02364    options are enabled.  */
02365 static void
02366 process_dl_debug (const char *dl_debug)
02367 {
02368   /* When adding new entries make sure that the maximal length of a name
02369      is correctly handled in the LD_DEBUG_HELP code below.  */
02370   static const struct
02371   {
02372     unsigned char len;
02373     const char name[10];
02374     const char helptext[41];
02375     unsigned short int mask;
02376   } debopts[] =
02377     {
02378 #define LEN_AND_STR(str) sizeof (str) - 1, str
02379       { LEN_AND_STR ("libs"), "display library search paths",
02380        DL_DEBUG_LIBS | DL_DEBUG_IMPCALLS },
02381       { LEN_AND_STR ("reloc"), "display relocation processing",
02382        DL_DEBUG_RELOC | DL_DEBUG_IMPCALLS },
02383       { LEN_AND_STR ("files"), "display progress for input file",
02384        DL_DEBUG_FILES | DL_DEBUG_IMPCALLS },
02385       { LEN_AND_STR ("symbols"), "display symbol table processing",
02386        DL_DEBUG_SYMBOLS | DL_DEBUG_IMPCALLS },
02387       { LEN_AND_STR ("bindings"), "display information about symbol binding",
02388        DL_DEBUG_BINDINGS | DL_DEBUG_IMPCALLS },
02389       { LEN_AND_STR ("versions"), "display version dependencies",
02390        DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS },
02391       { LEN_AND_STR ("all"), "all previous options combined",
02392        DL_DEBUG_LIBS | DL_DEBUG_RELOC | DL_DEBUG_FILES | DL_DEBUG_SYMBOLS
02393        | DL_DEBUG_BINDINGS | DL_DEBUG_VERSIONS | DL_DEBUG_IMPCALLS },
02394       { LEN_AND_STR ("statistics"), "display relocation statistics",
02395        DL_DEBUG_STATISTICS },
02396       { LEN_AND_STR ("unused"), "determined unused DSOs",
02397        DL_DEBUG_UNUSED },
02398       { LEN_AND_STR ("help"), "display this help message and exit",
02399        DL_DEBUG_HELP },
02400     };
02401 #define ndebopts (sizeof (debopts) / sizeof (debopts[0]))
02402 
02403   /* Skip separating white spaces and commas.  */
02404   while (*dl_debug != '\0')
02405     {
02406       if (*dl_debug != ' ' && *dl_debug != ',' && *dl_debug != ':')
02407        {
02408          size_t cnt;
02409          size_t len = 1;
02410 
02411          while (dl_debug[len] != '\0' && dl_debug[len] != ' '
02412                && dl_debug[len] != ',' && dl_debug[len] != ':')
02413            ++len;
02414 
02415          for (cnt = 0; cnt < ndebopts; ++cnt)
02416            if (debopts[cnt].len == len
02417               && memcmp (dl_debug, debopts[cnt].name, len) == 0)
02418              {
02419               GLRO(dl_debug_mask) |= debopts[cnt].mask;
02420               any_debug = 1;
02421               break;
02422              }
02423 
02424          if (cnt == ndebopts)
02425            {
02426              /* Display a warning and skip everything until next
02427                separator.  */
02428              char *copy = strndupa (dl_debug, len);
02429              _dl_error_printf ("\
02430 warning: debug option `%s' unknown; try LD_DEBUG=help\n", copy);
02431            }
02432 
02433          dl_debug += len;
02434          continue;
02435        }
02436 
02437       ++dl_debug;
02438     }
02439 
02440   if (GLRO(dl_debug_mask) & DL_DEBUG_HELP)
02441     {
02442       size_t cnt;
02443 
02444       _dl_printf ("\
02445 Valid options for the LD_DEBUG environment variable are:\n\n");
02446 
02447       for (cnt = 0; cnt < ndebopts; ++cnt)
02448        _dl_printf ("  %.*s%s%s\n", debopts[cnt].len, debopts[cnt].name,
02449                   "         " + debopts[cnt].len - 3,
02450                   debopts[cnt].helptext);
02451 
02452       _dl_printf ("\n\
02453 To direct the debugging output into a file instead of standard output\n\
02454 a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n");
02455       _exit (0);
02456     }
02457 }
02458 
02459 static void
02460 process_dl_audit (char *str)
02461 {
02462   /* The parameter is a colon separated list of DSO names.  */
02463   char *p;
02464 
02465   while ((p = (strsep) (&str, ":")) != NULL)
02466     if (p[0] != '\0'
02467        && (__builtin_expect (! INTUSE(__libc_enable_secure), 1)
02468            || strchr (p, '/') == NULL))
02469       {
02470        /* This is using the local malloc, not the system malloc.  The
02471           memory can never be freed.  */
02472        struct audit_list *newp = malloc (sizeof (*newp));
02473        newp->name = p;
02474 
02475        if (audit_list == NULL)
02476          audit_list = newp->next = newp;
02477        else
02478          {
02479            newp->next = audit_list->next;
02480            audit_list = audit_list->next = newp;
02481          }
02482       }
02483 }
02484 
02485 /* Process all environments variables the dynamic linker must recognize.
02486    Since all of them start with `LD_' we are a bit smarter while finding
02487    all the entries.  */
02488 extern char **_environ attribute_hidden;
02489 
02490 
02491 static void
02492 process_envvars (enum mode *modep)
02493 {
02494   char **runp = _environ;
02495   char *envline;
02496   enum mode mode = normal;
02497   char *debug_output = NULL;
02498 
02499   /* This is the default place for profiling data file.  */
02500   GLRO(dl_profile_output)
02501     = &"/var/tmp\0/var/profile"[INTUSE(__libc_enable_secure) ? 9 : 0];
02502 
02503   while ((envline = _dl_next_ld_env_entry (&runp)) != NULL)
02504     {
02505       size_t len = 0;
02506 
02507       while (envline[len] != '\0' && envline[len] != '=')
02508        ++len;
02509 
02510       if (envline[len] != '=')
02511        /* This is a "LD_" variable at the end of the string without
02512           a '=' character.  Ignore it since otherwise we will access
02513           invalid memory below.  */
02514        continue;
02515 
02516       switch (len)
02517        {
02518        case 4:
02519          /* Warning level, verbose or not.  */
02520          if (memcmp (envline, "WARN", 4) == 0)
02521            GLRO(dl_verbose) = envline[5] != '\0';
02522          break;
02523 
02524        case 5:
02525          /* Debugging of the dynamic linker?  */
02526          if (memcmp (envline, "DEBUG", 5) == 0)
02527            {
02528              process_dl_debug (&envline[6]);
02529              break;
02530            }
02531          if (memcmp (envline, "AUDIT", 5) == 0)
02532            process_dl_audit (&envline[6]);
02533          break;
02534 
02535        case 7:
02536          /* Print information about versions.  */
02537          if (memcmp (envline, "VERBOSE", 7) == 0)
02538            {
02539              version_info = envline[8] != '\0';
02540              break;
02541            }
02542 
02543          /* List of objects to be preloaded.  */
02544          if (memcmp (envline, "PRELOAD", 7) == 0)
02545            {
02546              preloadlist = &envline[8];
02547              break;
02548            }
02549 
02550          /* Which shared object shall be profiled.  */
02551          if (memcmp (envline, "PROFILE", 7) == 0 && envline[8] != '\0')
02552            GLRO(dl_profile) = &envline[8];
02553          break;
02554 
02555        case 8:
02556          /* Do we bind early?  */
02557          if (memcmp (envline, "BIND_NOW", 8) == 0)
02558            {
02559              GLRO(dl_lazy) = envline[9] == '\0';
02560              break;
02561            }
02562          if (memcmp (envline, "BIND_NOT", 8) == 0)
02563            GLRO(dl_bind_not) = envline[9] != '\0';
02564          break;
02565 
02566        case 9:
02567          /* Test whether we want to see the content of the auxiliary
02568             array passed up from the kernel.  */
02569          if (!INTUSE(__libc_enable_secure)
02570              && memcmp (envline, "SHOW_AUXV", 9) == 0)
02571            _dl_show_auxv ();
02572          break;
02573 
02574        case 10:
02575          /* Mask for the important hardware capabilities.  */
02576          if (memcmp (envline, "HWCAP_MASK", 10) == 0)
02577            GLRO(dl_hwcap_mask) = __strtoul_internal (&envline[11], NULL,
02578                                                 0, 0);
02579          break;
02580 
02581        case 11:
02582          /* Path where the binary is found.  */
02583          if (!INTUSE(__libc_enable_secure)
02584              && memcmp (envline, "ORIGIN_PATH", 11) == 0)
02585            GLRO(dl_origin_path) = &envline[12];
02586          break;
02587 
02588        case 12:
02589          /* The library search path.  */
02590          if (memcmp (envline, "LIBRARY_PATH", 12) == 0)
02591            {
02592              library_path = &envline[13];
02593              break;
02594            }
02595 
02596          /* Where to place the profiling data file.  */
02597          if (memcmp (envline, "DEBUG_OUTPUT", 12) == 0)
02598            {
02599              debug_output = &envline[13];
02600              break;
02601            }
02602 
02603          if (!INTUSE(__libc_enable_secure)
02604              && memcmp (envline, "DYNAMIC_WEAK", 12) == 0)
02605            GLRO(dl_dynamic_weak) = 1;
02606          break;
02607 
02608        case 13:
02609          /* We might have some extra environment variable with length 13
02610             to handle.  */
02611 #ifdef EXTRA_LD_ENVVARS_13
02612          EXTRA_LD_ENVVARS_13
02613 #endif
02614          if (!INTUSE(__libc_enable_secure)
02615              && memcmp (envline, "USE_LOAD_BIAS", 13) == 0)
02616            {
02617              GLRO(dl_use_load_bias) = envline[14] == '1' ? -1 : 0;
02618              break;
02619            }
02620 
02621          if (memcmp (envline, "POINTER_GUARD", 13) == 0)
02622            GLRO(dl_pointer_guard) = envline[14] != '0';
02623          break;
02624 
02625        case 14:
02626          /* Where to place the profiling data file.  */
02627          if (!INTUSE(__libc_enable_secure)
02628              && memcmp (envline, "PROFILE_OUTPUT", 14) == 0
02629              && envline[15] != '\0')
02630            GLRO(dl_profile_output) = &envline[15];
02631          break;
02632 
02633        case 16:
02634          /* The mode of the dynamic linker can be set.  */
02635          if (memcmp (envline, "TRACE_PRELINKING", 16) == 0)
02636            {
02637              mode = trace;
02638              GLRO(dl_verbose) = 1;
02639              GLRO(dl_debug_mask) |= DL_DEBUG_PRELINK;
02640              GLRO(dl_trace_prelink) = &envline[17];
02641            }
02642          break;
02643 
02644        case 20:
02645          /* The mode of the dynamic linker can be set.  */
02646          if (memcmp (envline, "TRACE_LOADED_OBJECTS", 20) == 0)
02647            mode = trace;
02648          break;
02649 
02650          /* We might have some extra environment variable to handle.  This
02651             is tricky due to the pre-processing of the length of the name
02652             in the switch statement here.  The code here assumes that added
02653             environment variables have a different length.  */
02654 #ifdef EXTRA_LD_ENVVARS
02655          EXTRA_LD_ENVVARS
02656 #endif
02657        }
02658     }
02659 
02660   /* The caller wants this information.  */
02661   *modep = mode;
02662 
02663   /* Extra security for SUID binaries.  Remove all dangerous environment
02664      variables.  */
02665   if (__builtin_expect (INTUSE(__libc_enable_secure), 0))
02666     {
02667       static const char unsecure_envvars[] =
02668 #ifdef EXTRA_UNSECURE_ENVVARS
02669        EXTRA_UNSECURE_ENVVARS
02670 #endif
02671        UNSECURE_ENVVARS;
02672       const char *nextp;
02673 
02674       nextp = unsecure_envvars;
02675       do
02676        {
02677          unsetenv (nextp);
02678          /* We could use rawmemchr but this need not be fast.  */
02679          nextp = (char *) (strchr) (nextp, '\0') + 1;
02680        }
02681       while (*nextp != '\0');
02682 
02683       if (__access ("/etc/suid-debug", F_OK) != 0)
02684         {
02685          unsetenv ("MALLOC_CHECK_");
02686          GLRO(dl_debug_mask) = 0;
02687         }
02688 
02689       if (mode != normal)
02690        _exit (5);
02691     }
02692   /* If we have to run the dynamic linker in debugging mode and the
02693      LD_DEBUG_OUTPUT environment variable is given, we write the debug
02694      messages to this file.  */
02695   else if (any_debug && debug_output != NULL)
02696     {
02697 #ifdef O_NOFOLLOW
02698       const int flags = O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW;
02699 #else
02700       const int flags = O_WRONLY | O_APPEND | O_CREAT;
02701 #endif
02702       size_t name_len = strlen (debug_output);
02703       char buf[name_len + 12];
02704       char *startp;
02705 
02706       buf[name_len + 11] = '\0';
02707       startp = _itoa (__getpid (), &buf[name_len + 11], 10, 0);
02708       *--startp = '.';
02709       startp = memcpy (startp - name_len, debug_output, name_len);
02710 
02711       GLRO(dl_debug_fd) = __open (startp, flags, DEFFILEMODE);
02712       if (GLRO(dl_debug_fd) == -1)
02713        /* We use standard output if opening the file failed.  */
02714        GLRO(dl_debug_fd) = STDOUT_FILENO;
02715     }
02716 }
02717 
02718 
02719 /* Print the various times we collected.  */
02720 static void
02721 __attribute ((noinline))
02722 print_statistics (hp_timing_t *rtld_total_timep)
02723 {
02724 #ifndef HP_TIMING_NONAVAIL
02725   char buf[200];
02726   char *cp;
02727   char *wp;
02728 
02729   /* Total time rtld used.  */
02730   if (HP_TIMING_AVAIL)
02731     {
02732       HP_TIMING_PRINT (buf, sizeof (buf), *rtld_total_timep);
02733       _dl_debug_printf ("\nruntime linker statistics:\n"
02734                      "  total startup time in dynamic loader: %s\n", buf);
02735 
02736       /* Print relocation statistics.  */
02737       char pbuf[30];
02738       HP_TIMING_PRINT (buf, sizeof (buf), relocate_time);
02739       cp = _itoa ((1000ULL * relocate_time) / *rtld_total_timep,
02740                 pbuf + sizeof (pbuf), 10, 0);
02741       wp = pbuf;
02742       switch (pbuf + sizeof (pbuf) - cp)
02743        {
02744        case 3:
02745          *wp++ = *cp++;
02746        case 2:
02747          *wp++ = *cp++;
02748        case 1:
02749          *wp++ = '.';
02750          *wp++ = *cp++;
02751        }
02752       *wp = '\0';
02753       _dl_debug_printf ("\
02754             time needed for relocation: %s (%s%%)\n", buf, pbuf);
02755     }
02756 #endif
02757 
02758   unsigned long int num_relative_relocations = 0;
02759   for (Lmid_t ns = 0; ns < DL_NNS; ++ns)
02760     {
02761       if (GL(dl_ns)[ns]._ns_loaded == NULL)
02762        continue;
02763 
02764       struct r_scope_elem *scope = &GL(dl_ns)[ns]._ns_loaded->l_searchlist;
02765 
02766       for (unsigned int i = 0; i < scope->r_nlist; i++)
02767        {
02768          struct link_map *l = scope->r_list [i];
02769 
02770          if (l->l_addr != 0 && l->l_info[VERSYMIDX (DT_RELCOUNT)])
02771            num_relative_relocations
02772              += l->l_info[VERSYMIDX (DT_RELCOUNT)]->d_un.d_val;
02773 #ifndef ELF_MACHINE_REL_RELATIVE
02774          /* Relative relocations are processed on these architectures if
02775             library is loaded to different address than p_vaddr or
02776             if not prelinked.  */
02777          if ((l->l_addr != 0 || !l->l_info[VALIDX(DT_GNU_PRELINKED)])
02778              && l->l_info[VERSYMIDX (DT_RELACOUNT)])
02779 #else
02780          /* On e.g. IA-64 or Alpha, relative relocations are processed
02781             only if library is loaded to different address than p_vaddr.  */
02782          if (l->l_addr != 0 && l->l_info[VERSYMIDX (DT_RELACOUNT)])
02783 #endif
02784            num_relative_relocations
02785              += l->l_info[VERSYMIDX (DT_RELACOUNT)]->d_un.d_val;
02786        }
02787     }
02788 
02789   _dl_debug_printf ("                 number of relocations: %lu\n"
02790                   "      number of relocations from cache: %lu\n"
02791                   "        number of relative relocations: %lu\n",
02792                   GL(dl_num_relocations),
02793                   GL(dl_num_cache_relocations),
02794                   num_relative_relocations);
02795 
02796 #ifndef HP_TIMING_NONAVAIL
02797   /* Time spend while loading the object and the dependencies.  */
02798   if (HP_TIMING_AVAIL)
02799     {
02800       char pbuf[30];
02801       HP_TIMING_PRINT (buf, sizeof (buf), load_time);
02802       cp = _itoa ((1000ULL * load_time) / *rtld_total_timep,
02803                 pbuf + sizeof (pbuf), 10, 0);
02804       wp = pbuf;
02805       switch (pbuf + sizeof (pbuf) - cp)
02806        {
02807        case 3:
02808          *wp++ = *cp++;
02809        case 2:
02810          *wp++ = *cp++;
02811        case 1:
02812          *wp++ = '.';
02813          *wp++ = *cp++;
02814        }
02815       *wp = '\0';
02816       _dl_debug_printf ("\
02817            time needed to load objects: %s (%s%%)\n",
02818                             buf, pbuf);
02819     }
02820 #endif
02821 }