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