Sort objects before relocations
[platform/upstream/glibc.git] / elf / dl-open.c
1 /* Load a shared object at runtime, relocate it, and run its initializer.
2    Copyright (C) 1996-2007, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <assert.h>
21 #include <dlfcn.h>
22 #include <errno.h>
23 #include <libintl.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/mman.h>           /* Check whether MAP_COPY is defined.  */
29 #include <sys/param.h>
30 #include <bits/libc-lock.h>
31 #include <ldsodefs.h>
32 #include <bp-sym.h>
33 #include <caller.h>
34 #include <sysdep-cancel.h>
35 #include <tls.h>
36
37 #include <dl-dst.h>
38
39
40 extern ElfW(Addr) _dl_sysdep_start (void **start_argptr,
41                                     void (*dl_main) (const ElfW(Phdr) *phdr,
42                                                      ElfW(Word) phnum,
43                                                      ElfW(Addr) *user_entry,
44                                                      ElfW(auxv_t) *auxv));
45 weak_extern (BP_SYM (_dl_sysdep_start))
46
47 extern int __libc_multiple_libcs;       /* Defined in init-first.c.  */
48
49 /* We must be carefull not to leave us in an inconsistent state.  Thus we
50    catch any error and re-raise it after cleaning up.  */
51
52 struct dl_open_args
53 {
54   const char *file;
55   int mode;
56   /* This is the caller of the dlopen() function.  */
57   const void *caller_dlopen;
58   /* This is the caller if _dl_open().  */
59   const void *caller_dl_open;
60   struct link_map *map;
61   /* Namespace ID.  */
62   Lmid_t nsid;
63   /* Original parameters to the program and the current environment.  */
64   int argc;
65   char **argv;
66   char **env;
67 };
68
69
70 static int
71 add_to_global (struct link_map *new)
72 {
73   struct link_map **new_global;
74   unsigned int to_add = 0;
75   unsigned int cnt;
76
77   /* Count the objects we have to put in the global scope.  */
78   for (cnt = 0; cnt < new->l_searchlist.r_nlist; ++cnt)
79     if (new->l_searchlist.r_list[cnt]->l_global == 0)
80       ++to_add;
81
82   /* The symbols of the new objects and its dependencies are to be
83      introduced into the global scope that will be used to resolve
84      references from other dynamically-loaded objects.
85
86      The global scope is the searchlist in the main link map.  We
87      extend this list if necessary.  There is one problem though:
88      since this structure was allocated very early (before the libc
89      is loaded) the memory it uses is allocated by the malloc()-stub
90      in the ld.so.  When we come here these functions are not used
91      anymore.  Instead the malloc() implementation of the libc is
92      used.  But this means the block from the main map cannot be used
93      in an realloc() call.  Therefore we allocate a completely new
94      array the first time we have to add something to the locale scope.  */
95
96   struct link_namespaces *ns = &GL(dl_ns)[new->l_ns];
97   if (ns->_ns_global_scope_alloc == 0)
98     {
99       /* This is the first dynamic object given global scope.  */
100       ns->_ns_global_scope_alloc
101         = ns->_ns_main_searchlist->r_nlist + to_add + 8;
102       new_global = (struct link_map **)
103         malloc (ns->_ns_global_scope_alloc * sizeof (struct link_map *));
104       if (new_global == NULL)
105         {
106           ns->_ns_global_scope_alloc = 0;
107         nomem:
108           _dl_signal_error (ENOMEM, new->l_libname->name, NULL,
109                             N_("cannot extend global scope"));
110           return 1;
111         }
112
113       /* Copy over the old entries.  */
114       ns->_ns_main_searchlist->r_list
115         = memcpy (new_global, ns->_ns_main_searchlist->r_list,
116                   (ns->_ns_main_searchlist->r_nlist
117                    * sizeof (struct link_map *)));
118     }
119   else if (ns->_ns_main_searchlist->r_nlist + to_add
120            > ns->_ns_global_scope_alloc)
121     {
122       /* We have to extend the existing array of link maps in the
123          main map.  */
124       struct link_map **old_global
125         = GL(dl_ns)[new->l_ns]._ns_main_searchlist->r_list;
126       size_t new_nalloc = ((ns->_ns_global_scope_alloc + to_add) * 2);
127
128       new_global = (struct link_map **)
129         malloc (new_nalloc * sizeof (struct link_map *));
130       if (new_global == NULL)
131         goto nomem;
132
133       memcpy (new_global, old_global,
134               ns->_ns_global_scope_alloc * sizeof (struct link_map *));
135
136       ns->_ns_global_scope_alloc = new_nalloc;
137       ns->_ns_main_searchlist->r_list = new_global;
138
139       if (!RTLD_SINGLE_THREAD_P)
140         THREAD_GSCOPE_WAIT ();
141
142       free (old_global);
143     }
144
145   /* Now add the new entries.  */
146   unsigned int new_nlist = ns->_ns_main_searchlist->r_nlist;
147   for (cnt = 0; cnt < new->l_searchlist.r_nlist; ++cnt)
148     {
149       struct link_map *map = new->l_searchlist.r_list[cnt];
150
151       if (map->l_global == 0)
152         {
153           map->l_global = 1;
154           ns->_ns_main_searchlist->r_list[new_nlist++] = map;
155
156           /* We modify the global scope.  Report this.  */
157           if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_SCOPES, 0))
158             _dl_debug_printf ("\nadd %s [%lu] to global scope\n",
159                               map->l_name, map->l_ns);
160         }
161     }
162   atomic_write_barrier ();
163   ns->_ns_main_searchlist->r_nlist = new_nlist;
164
165   return 0;
166 }
167
168 static void
169 dl_open_worker (void *a)
170 {
171   struct dl_open_args *args = a;
172   const char *file = args->file;
173   int mode = args->mode;
174   struct link_map *call_map = NULL;
175
176   /* Check whether _dl_open() has been called from a valid DSO.  */
177   if (__check_caller (args->caller_dl_open,
178                       allow_libc|allow_libdl|allow_ldso) != 0)
179     _dl_signal_error (0, "dlopen", NULL, N_("invalid caller"));
180
181   /* Determine the caller's map if necessary.  This is needed in case
182      we have a DST, when we don't know the namespace ID we have to put
183      the new object in, or when the file name has no path in which
184      case we need to look along the RUNPATH/RPATH of the caller.  */
185   const char *dst = strchr (file, '$');
186   if (dst != NULL || args->nsid == __LM_ID_CALLER
187       || strchr (file, '/') == NULL)
188     {
189       const void *caller_dlopen = args->caller_dlopen;
190
191       /* We have to find out from which object the caller is calling.
192          By default we assume this is the main application.  */
193       call_map = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
194
195       struct link_map *l;
196       for (Lmid_t ns = 0; ns < GL(dl_nns); ++ns)
197         for (l = GL(dl_ns)[ns]._ns_loaded; l != NULL; l = l->l_next)
198           if (caller_dlopen >= (const void *) l->l_map_start
199               && caller_dlopen < (const void *) l->l_map_end
200               && (l->l_contiguous
201                   || _dl_addr_inside_object (l, (ElfW(Addr)) caller_dlopen)))
202             {
203               assert (ns == l->l_ns);
204               call_map = l;
205               goto found_caller;
206             }
207
208     found_caller:
209       if (args->nsid == __LM_ID_CALLER)
210         {
211 #ifndef SHARED
212           /* In statically linked apps there might be no loaded object.  */
213           if (call_map == NULL)
214             args->nsid = LM_ID_BASE;
215           else
216 #endif
217             args->nsid = call_map->l_ns;
218         }
219     }
220
221   assert (_dl_debug_initialize (0, args->nsid)->r_state == RT_CONSISTENT);
222
223   /* Load the named object.  */
224   struct link_map *new;
225   args->map = new = _dl_map_object (call_map, file, lt_loaded, 0,
226                                     mode | __RTLD_CALLMAP, args->nsid);
227
228   /* If the pointer returned is NULL this means the RTLD_NOLOAD flag is
229      set and the object is not already loaded.  */
230   if (new == NULL)
231     {
232       assert (mode & RTLD_NOLOAD);
233       return;
234     }
235
236   if (__builtin_expect (mode & __RTLD_SPROF, 0))
237     /* This happens only if we load a DSO for 'sprof'.  */
238     return;
239
240   /* This object is directly loaded.  */
241   ++new->l_direct_opencount;
242
243   /* It was already open.  */
244   if (__builtin_expect (new->l_searchlist.r_list != NULL, 0))
245     {
246       /* Let the user know about the opencount.  */
247       if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
248         _dl_debug_printf ("opening file=%s [%lu]; direct_opencount=%u\n\n",
249                           new->l_name, new->l_ns, new->l_direct_opencount);
250
251       /* If the user requested the object to be in the global namespace
252          but it is not so far, add it now.  */
253       if ((mode & RTLD_GLOBAL) && new->l_global == 0)
254         (void) add_to_global (new);
255
256       assert (_dl_debug_initialize (0, args->nsid)->r_state == RT_CONSISTENT);
257
258       return;
259     }
260
261   /* Load that object's dependencies.  */
262   _dl_map_object_deps (new, NULL, 0, 0,
263                        mode & (__RTLD_DLOPEN | RTLD_DEEPBIND | __RTLD_AUDIT));
264
265   /* So far, so good.  Now check the versions.  */
266   for (unsigned int i = 0; i < new->l_searchlist.r_nlist; ++i)
267     if (new->l_searchlist.r_list[i]->l_real->l_versions == NULL)
268       (void) _dl_check_map_versions (new->l_searchlist.r_list[i]->l_real,
269                                      0, 0);
270
271 #ifdef SHARED
272   /* Auditing checkpoint: we have added all objects.  */
273   if (__builtin_expect (GLRO(dl_naudit) > 0, 0))
274     {
275       struct link_map *head = GL(dl_ns)[new->l_ns]._ns_loaded;
276       /* Do not call the functions for any auditing object.  */
277       if (head->l_auditing == 0)
278         {
279           struct audit_ifaces *afct = GLRO(dl_audit);
280           for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
281             {
282               if (afct->activity != NULL)
283                 afct->activity (&head->l_audit[cnt].cookie, LA_ACT_CONSISTENT);
284
285               afct = afct->next;
286             }
287         }
288     }
289 #endif
290
291   /* Notify the debugger all new objects are now ready to go.  */
292   struct r_debug *r = _dl_debug_initialize (0, args->nsid);
293   r->r_state = RT_CONSISTENT;
294   _dl_debug_state ();
295
296   /* Print scope information.  */
297   if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_SCOPES, 0))
298     _dl_show_scope (new, 0);
299
300   /* Only do lazy relocation if `LD_BIND_NOW' is not set.  */
301   int reloc_mode = mode & __RTLD_AUDIT;
302   if (GLRO(dl_lazy))
303     reloc_mode |= mode & RTLD_LAZY;
304
305   /* Sort the objects by dependency for the relocation process.  This
306      allows IFUNC relocations to work and it also means copy
307      relocation of dependencies are if necessary overwritten.  */
308   size_t nmaps = 0;
309   struct link_map *l = new;
310   do
311     {
312       if (! l->l_real->l_relocated)
313         ++nmaps;
314       l = l->l_next;
315     }
316   while (l != NULL);
317   struct link_map *maps[nmaps];
318   nmaps = 0;
319   l = new;
320   do
321     {
322       if (! l->l_real->l_relocated)
323         maps[nmaps++] = l;
324       l = l->l_next;
325     }
326   while (l != NULL);
327   if (nmaps > 1)
328     {
329       char seen[nmaps];
330       memset (seen, '\0', nmaps);
331       size_t i = 0;
332       while (1)
333         {
334           ++seen[i];
335           struct link_map *thisp = maps[i];
336
337           /* Find the last object in the list for which the current one is
338              a dependency and move the current object behind the object
339              with the dependency.  */
340           size_t k = nmaps - 1;
341           while (k > i)
342             {
343               struct link_map **runp = maps[k]->l_initfini;
344               if (runp != NULL)
345                 /* Look through the dependencies of the object.  */
346                 while (*runp != NULL)
347                   if (__builtin_expect (*runp++ == thisp, 0))
348                     {
349                       /* Move the current object to the back past the last
350                          object with it as the dependency.  */
351                       memmove (&maps[i], &maps[i + 1],
352                                (k - i) * sizeof (maps[0]));
353                       maps[k] = thisp;
354
355                       if (seen[i + 1] > 1)
356                         {
357                           ++i;
358                           goto next_clear;
359                         }
360
361                       char this_seen = seen[i];
362                       memmove (&seen[i], &seen[i + 1],
363                                (k - i) * sizeof (seen[0]));
364                       seen[k] = this_seen;
365
366                       goto next;
367                     }
368
369               --k;
370             }
371
372           if (++i == nmaps)
373             break;
374         next_clear:
375           memset (&seen[i], 0, (nmaps - i) * sizeof (seen[0]));
376         next:;
377         }
378     }
379
380   for (size_t i = nmaps; i-- > 0; )
381     {
382       l = maps[i];
383
384 #ifdef SHARED
385       if (__builtin_expect (GLRO(dl_profile) != NULL, 0))
386         {
387           /* If this here is the shared object which we want to profile
388              make sure the profile is started.  We can find out whether
389              this is necessary or not by observing the `_dl_profile_map'
390              variable.  If it was NULL but is not NULL afterwars we must
391              start the profiling.  */
392           struct link_map *old_profile_map = GL(dl_profile_map);
393
394           _dl_relocate_object (l, l->l_scope, reloc_mode | RTLD_LAZY, 1);
395
396           if (old_profile_map == NULL && GL(dl_profile_map) != NULL)
397             {
398               /* We must prepare the profiling.  */
399               _dl_start_profile ();
400
401               /* Prevent unloading the object.  */
402               GL(dl_profile_map)->l_flags_1 |= DF_1_NODELETE;
403             }
404         }
405       else
406 #endif
407         _dl_relocate_object (l, l->l_scope, reloc_mode, 0);
408     }
409
410   /* If the file is not loaded now as a dependency, add the search
411      list of the newly loaded object to the scope.  */
412   bool any_tls = false;
413   unsigned int first_static_tls = new->l_searchlist.r_nlist;
414   for (unsigned int i = 0; i < new->l_searchlist.r_nlist; ++i)
415     {
416       struct link_map *imap = new->l_searchlist.r_list[i];
417       int from_scope = 0;
418
419       /* If the initializer has been called already, the object has
420          not been loaded here and now.  */
421       if (imap->l_init_called && imap->l_type == lt_loaded)
422         {
423           struct r_scope_elem **runp = imap->l_scope;
424           size_t cnt = 0;
425
426           while (*runp != NULL)
427             {
428               if (*runp == &new->l_searchlist)
429                 break;
430               ++cnt;
431               ++runp;
432             }
433
434           if (*runp != NULL)
435             /* Avoid duplicates.  */
436             continue;
437
438           if (__builtin_expect (cnt + 1 >= imap->l_scope_max, 0))
439             {
440               /* The 'r_scope' array is too small.  Allocate a new one
441                  dynamically.  */
442               size_t new_size;
443               struct r_scope_elem **newp;
444
445 #define SCOPE_ELEMS(imap) \
446   (sizeof (imap->l_scope_mem) / sizeof (imap->l_scope_mem[0]))
447
448               if (imap->l_scope != imap->l_scope_mem
449                   && imap->l_scope_max < SCOPE_ELEMS (imap))
450                 {
451                   new_size = SCOPE_ELEMS (imap);
452                   newp = imap->l_scope_mem;
453                 }
454               else
455                 {
456                   new_size = imap->l_scope_max * 2;
457                   newp = (struct r_scope_elem **)
458                     malloc (new_size * sizeof (struct r_scope_elem *));
459                   if (newp == NULL)
460                     _dl_signal_error (ENOMEM, "dlopen", NULL,
461                                       N_("cannot create scope list"));
462                 }
463
464               memcpy (newp, imap->l_scope, cnt * sizeof (imap->l_scope[0]));
465               struct r_scope_elem **old = imap->l_scope;
466
467               imap->l_scope = newp;
468
469               if (old != imap->l_scope_mem)
470                 _dl_scope_free (old);
471
472               imap->l_scope_max = new_size;
473             }
474
475           /* First terminate the extended list.  Otherwise a thread
476              might use the new last element and then use the garbage
477              at offset IDX+1.  */
478           imap->l_scope[cnt + 1] = NULL;
479           atomic_write_barrier ();
480           imap->l_scope[cnt] = &new->l_searchlist;
481
482           /* Print only new scope information.  */
483           from_scope = cnt;
484         }
485       /* Only add TLS memory if this object is loaded now and
486          therefore is not yet initialized.  */
487       else if (! imap->l_init_called
488                /* Only if the module defines thread local data.  */
489                && __builtin_expect (imap->l_tls_blocksize > 0, 0))
490         {
491           /* Now that we know the object is loaded successfully add
492              modules containing TLS data to the slot info table.  We
493              might have to increase its size.  */
494           _dl_add_to_slotinfo (imap);
495
496           if (imap->l_need_tls_init
497               && first_static_tls == new->l_searchlist.r_nlist)
498             first_static_tls = i;
499
500           /* We have to bump the generation counter.  */
501           any_tls = true;
502         }
503
504       /* Print scope information.  */
505       if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_SCOPES, 0))
506         _dl_show_scope (imap, from_scope);
507     }
508
509   /* Bump the generation number if necessary.  */
510   if (any_tls && __builtin_expect (++GL(dl_tls_generation) == 0, 0))
511     _dl_fatal_printf (N_("\
512 TLS generation counter wrapped!  Please report this."));
513
514   /* We need a second pass for static tls data, because _dl_update_slotinfo
515      must not be run while calls to _dl_add_to_slotinfo are still pending. */
516   for (unsigned int i = first_static_tls; i < new->l_searchlist.r_nlist; ++i)
517     {
518       struct link_map *imap = new->l_searchlist.r_list[i];
519
520       if (imap->l_need_tls_init
521           && ! imap->l_init_called
522           && imap->l_tls_blocksize > 0)
523         {
524           /* For static TLS we have to allocate the memory here and
525              now.  This includes allocating memory in the DTV.  But we
526              cannot change any DTV other than our own. So, if we
527              cannot guarantee that there is room in the DTV we don't
528              even try it and fail the load.
529
530              XXX We could track the minimum DTV slots allocated in
531              all threads.  */
532           if (! RTLD_SINGLE_THREAD_P && imap->l_tls_modid > DTV_SURPLUS)
533             _dl_signal_error (0, "dlopen", NULL, N_("\
534 cannot load any more object with static TLS"));
535
536           imap->l_need_tls_init = 0;
537 #ifdef SHARED
538           /* Update the slot information data for at least the
539              generation of the DSO we are allocating data for.  */
540           _dl_update_slotinfo (imap->l_tls_modid);
541 #endif
542
543           GL(dl_init_static_tls) (imap);
544           assert (imap->l_need_tls_init == 0);
545         }
546     }
547
548   /* Run the initializer functions of new objects.  */
549   _dl_init (new, args->argc, args->argv, args->env);
550
551   /* Now we can make the new map available in the global scope.  */
552   if (mode & RTLD_GLOBAL)
553     /* Move the object in the global namespace.  */
554     if (add_to_global (new) != 0)
555       /* It failed.  */
556       return;
557
558   /* Mark the object as not deletable if the RTLD_NODELETE flags was
559      passed.  */
560   if (__builtin_expect (mode & RTLD_NODELETE, 0))
561     new->l_flags_1 |= DF_1_NODELETE;
562
563 #ifndef SHARED
564   /* We must be the static _dl_open in libc.a.  A static program that
565      has loaded a dynamic object now has competition.  */
566   __libc_multiple_libcs = 1;
567 #endif
568
569   /* Let the user know about the opencount.  */
570   if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
571     _dl_debug_printf ("opening file=%s [%lu]; direct_opencount=%u\n\n",
572                       new->l_name, new->l_ns, new->l_direct_opencount);
573 }
574
575
576 void *
577 _dl_open (const char *file, int mode, const void *caller_dlopen, Lmid_t nsid,
578           int argc, char *argv[], char *env[])
579 {
580   if ((mode & RTLD_BINDING_MASK) == 0)
581     /* One of the flags must be set.  */
582     _dl_signal_error (EINVAL, file, NULL, N_("invalid mode for dlopen()"));
583
584   /* Make sure we are alone.  */
585   __rtld_lock_lock_recursive (GL(dl_load_lock));
586
587   if (__builtin_expect (nsid == LM_ID_NEWLM, 0))
588     {
589       /* Find a new namespace.  */
590       for (nsid = 1; DL_NNS > 1 && nsid < GL(dl_nns); ++nsid)
591         if (GL(dl_ns)[nsid]._ns_loaded == NULL)
592           break;
593
594       if (__builtin_expect (nsid == DL_NNS, 0))
595         {
596           /* No more namespace available.  */
597           __rtld_lock_unlock_recursive (GL(dl_load_lock));
598
599           _dl_signal_error (EINVAL, file, NULL, N_("\
600 no more namespaces available for dlmopen()"));
601         }
602       else if (nsid == GL(dl_nns))
603         {
604           __rtld_lock_initialize (GL(dl_ns)[nsid]._ns_unique_sym_table.lock);
605           ++GL(dl_nns);
606         }
607
608       _dl_debug_initialize (0, nsid)->r_state = RT_CONSISTENT;
609     }
610   /* Never allow loading a DSO in a namespace which is empty.  Such
611      direct placements is only causing problems.  Also don't allow
612      loading into a namespace used for auditing.  */
613   else if (__builtin_expect (nsid != LM_ID_BASE && nsid != __LM_ID_CALLER, 0)
614            && (GL(dl_ns)[nsid]._ns_nloaded == 0
615                || GL(dl_ns)[nsid]._ns_loaded->l_auditing))
616     _dl_signal_error (EINVAL, file, NULL,
617                       N_("invalid target namespace in dlmopen()"));
618 #ifndef SHARED
619   else if ((nsid == LM_ID_BASE || nsid == __LM_ID_CALLER)
620            && GL(dl_ns)[LM_ID_BASE]._ns_loaded == NULL
621            && GL(dl_nns) == 0)
622     GL(dl_nns) = 1;
623 #endif
624
625   struct dl_open_args args;
626   args.file = file;
627   args.mode = mode;
628   args.caller_dlopen = caller_dlopen;
629   args.caller_dl_open = RETURN_ADDRESS (0);
630   args.map = NULL;
631   args.nsid = nsid;
632   args.argc = argc;
633   args.argv = argv;
634   args.env = env;
635
636   const char *objname;
637   const char *errstring;
638   bool malloced;
639   int errcode = _dl_catch_error (&objname, &errstring, &malloced,
640                                  dl_open_worker, &args);
641
642 #ifndef MAP_COPY
643   /* We must munmap() the cache file.  */
644   _dl_unload_cache ();
645 #endif
646
647   /* See if an error occurred during loading.  */
648   if (__builtin_expect (errstring != NULL, 0))
649     {
650       /* Remove the object from memory.  It may be in an inconsistent
651          state if relocation failed, for example.  */
652       if (args.map)
653         {
654           /* Maybe some of the modules which were loaded use TLS.
655              Since it will be removed in the following _dl_close call
656              we have to mark the dtv array as having gaps to fill the
657              holes.  This is a pessimistic assumption which won't hurt
658              if not true.  There is no need to do this when we are
659              loading the auditing DSOs since TLS has not yet been set
660              up.  */
661           if ((mode & __RTLD_AUDIT) == 0)
662             GL(dl_tls_dtv_gaps) = true;
663
664           _dl_close_worker (args.map);
665         }
666
667       assert (_dl_debug_initialize (0, args.nsid)->r_state == RT_CONSISTENT);
668
669       /* Release the lock.  */
670       __rtld_lock_unlock_recursive (GL(dl_load_lock));
671
672       /* Make a local copy of the error string so that we can release the
673          memory allocated for it.  */
674       size_t len_errstring = strlen (errstring) + 1;
675       char *local_errstring;
676       if (objname == errstring + len_errstring)
677         {
678           size_t total_len = len_errstring + strlen (objname) + 1;
679           local_errstring = alloca (total_len);
680           memcpy (local_errstring, errstring, total_len);
681           objname = local_errstring + len_errstring;
682         }
683       else
684         {
685           local_errstring = alloca (len_errstring);
686           memcpy (local_errstring, errstring, len_errstring);
687         }
688
689       if (malloced)
690         free ((char *) errstring);
691
692       /* Reraise the error.  */
693       _dl_signal_error (errcode, objname, NULL, local_errstring);
694     }
695
696   assert (_dl_debug_initialize (0, args.nsid)->r_state == RT_CONSISTENT);
697
698   /* Release the lock.  */
699   __rtld_lock_unlock_recursive (GL(dl_load_lock));
700
701 #ifndef SHARED
702   DL_STATIC_INIT (args.map);
703 #endif
704
705   return args.map;
706 }
707
708
709 void
710 _dl_show_scope (struct link_map *l, int from)
711 {
712   _dl_debug_printf ("object=%s [%lu]\n",
713                     *l->l_name ? l->l_name : rtld_progname, l->l_ns);
714   if (l->l_scope != NULL)
715     for (int scope_cnt = from; l->l_scope[scope_cnt] != NULL; ++scope_cnt)
716       {
717         _dl_debug_printf (" scope %u:", scope_cnt);
718
719         for (unsigned int cnt = 0; cnt < l->l_scope[scope_cnt]->r_nlist; ++cnt)
720           if (*l->l_scope[scope_cnt]->r_list[cnt]->l_name)
721             _dl_debug_printf_c (" %s",
722                                 l->l_scope[scope_cnt]->r_list[cnt]->l_name);
723           else
724             _dl_debug_printf_c (" %s", rtld_progname);
725
726         _dl_debug_printf_c ("\n");
727       }
728   else
729     _dl_debug_printf (" no scope\n");
730   _dl_debug_printf ("\n");
731 }
732
733 #ifdef IS_IN_rtld
734 /* Return non-zero if ADDR lies within one of L's segments.  */
735 int
736 internal_function
737 _dl_addr_inside_object (struct link_map *l, const ElfW(Addr) addr)
738 {
739   int n = l->l_phnum;
740   const ElfW(Addr) reladdr = addr - l->l_addr;
741
742   while (--n >= 0)
743     if (l->l_phdr[n].p_type == PT_LOAD
744         && reladdr - l->l_phdr[n].p_vaddr >= 0
745         && reladdr - l->l_phdr[n].p_vaddr < l->l_phdr[n].p_memsz)
746       return 1;
747   return 0;
748 }
749 #endif