1 /* Load a shared object at runtime, relocate it, and run its initializer.
2 Copyright (C) 1996-2004, 2005, 2006 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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
28 #include <sys/mman.h> /* Check whether MAP_COPY is defined. */
29 #include <sys/param.h>
30 #include <bits/libc-lock.h>
34 #include <sysdep-cancel.h>
39 extern ElfW(Addr) _dl_sysdep_start (void **start_argptr,
40 void (*dl_main) (const ElfW(Phdr) *phdr,
42 ElfW(Addr) *user_entry));
43 weak_extern (BP_SYM (_dl_sysdep_start))
45 extern int __libc_multiple_libcs; /* Defined in init-first.c. */
47 /* Undefine the following for debugging. */
48 /* #define SCOPE_DEBUG 1 */
50 static void show_scope (struct link_map *new);
53 /* We must be carefull not to leave us in an inconsistent state. Thus we
54 catch any error and re-raise it after cleaning up. */
60 /* This is the caller of the dlopen() function. */
61 const void *caller_dlopen;
62 /* This is the caller if _dl_open(). */
63 const void *caller_dl_open;
67 /* Original parameters to the program and the current environment. */
75 add_to_global (struct link_map *new)
77 struct link_map **new_global;
78 unsigned int to_add = 0;
81 /* Count the objects we have to put in the global scope. */
82 for (cnt = 0; cnt < new->l_searchlist.r_nlist; ++cnt)
83 if (new->l_searchlist.r_list[cnt]->l_global == 0)
86 /* The symbols of the new objects and its dependencies are to be
87 introduced into the global scope that will be used to resolve
88 references from other dynamically-loaded objects.
90 The global scope is the searchlist in the main link map. We
91 extend this list if necessary. There is one problem though:
92 since this structure was allocated very early (before the libc
93 is loaded) the memory it uses is allocated by the malloc()-stub
94 in the ld.so. When we come here these functions are not used
95 anymore. Instead the malloc() implementation of the libc is
96 used. But this means the block from the main map cannot be used
97 in an realloc() call. Therefore we allocate a completely new
98 array the first time we have to add something to the locale scope. */
100 if (GL(dl_ns)[new->l_ns]._ns_global_scope_alloc == 0)
102 /* This is the first dynamic object given global scope. */
103 GL(dl_ns)[new->l_ns]._ns_global_scope_alloc
104 = GL(dl_ns)[new->l_ns]._ns_main_searchlist->r_nlist + to_add + 8;
105 new_global = (struct link_map **)
106 malloc (GL(dl_ns)[new->l_ns]._ns_global_scope_alloc
107 * sizeof (struct link_map *));
108 if (new_global == NULL)
110 GL(dl_ns)[new->l_ns]._ns_global_scope_alloc = 0;
112 _dl_signal_error (ENOMEM, new->l_libname->name, NULL,
113 N_("cannot extend global scope"));
117 /* Copy over the old entries. */
118 GL(dl_ns)[new->l_ns]._ns_main_searchlist->r_list
119 = memcpy (new_global,
120 GL(dl_ns)[new->l_ns]._ns_main_searchlist->r_list,
121 (GL(dl_ns)[new->l_ns]._ns_main_searchlist->r_nlist
122 * sizeof (struct link_map *)));
124 else if (GL(dl_ns)[new->l_ns]._ns_main_searchlist->r_nlist + to_add
125 > GL(dl_ns)[new->l_ns]._ns_global_scope_alloc)
127 /* We have to extend the existing array of link maps in the
129 new_global = (struct link_map **)
130 realloc (GL(dl_ns)[new->l_ns]._ns_main_searchlist->r_list,
131 ((GL(dl_ns)[new->l_ns]._ns_global_scope_alloc + to_add + 8)
132 * sizeof (struct link_map *)));
133 if (new_global == NULL)
136 GL(dl_ns)[new->l_ns]._ns_global_scope_alloc += to_add + 8;
137 GL(dl_ns)[new->l_ns]._ns_main_searchlist->r_list = new_global;
140 /* Now add the new entries. */
141 for (cnt = 0; cnt < new->l_searchlist.r_nlist; ++cnt)
143 struct link_map *map = new->l_searchlist.r_list[cnt];
145 if (map->l_global == 0)
148 GL(dl_ns)[new->l_ns]._ns_main_searchlist->r_list[GL(dl_ns)[new->l_ns]._ns_main_searchlist->r_nlist]
150 ++GL(dl_ns)[new->l_ns]._ns_main_searchlist->r_nlist;
159 dl_open_worker (void *a)
161 struct dl_open_args *args = a;
162 const char *file = args->file;
163 int mode = args->mode;
164 struct link_map *new, *l;
168 bool any_tls = false;
170 struct link_map *call_map = NULL;
172 /* Check whether _dl_open() has been called from a valid DSO. */
173 if (__check_caller (args->caller_dl_open,
174 allow_libc|allow_libdl|allow_ldso) != 0)
175 _dl_signal_error (0, "dlopen", NULL, N_("invalid caller"));
177 /* Determine the caller's map if necessary. This is needed in case
178 we have a DST, when we don't know the namespace ID we have to put
179 the new object in, or when the file name has no path in which
180 case we need to look along the RUNPATH/RPATH of the caller. */
181 const char *dst = strchr (file, '$');
182 if (dst != NULL || args->nsid == __LM_ID_CALLER
183 || strchr (file, '/') == NULL)
185 const void *caller_dlopen = args->caller_dlopen;
187 /* We have to find out from which object the caller is calling.
188 By default we assume this is the main application. */
189 call_map = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
191 for (Lmid_t ns = 0; ns < DL_NNS; ++ns)
192 for (l = GL(dl_ns)[ns]._ns_loaded; l != NULL; l = l->l_next)
193 if (caller_dlopen >= (const void *) l->l_map_start
194 && caller_dlopen < (const void *) l->l_map_end)
196 /* There must be exactly one DSO for the range of the virtual
197 memory. Otherwise something is really broken. */
198 assert (ns == l->l_ns);
204 if (args->nsid == __LM_ID_CALLER)
207 /* In statically linked apps there might be no loaded object. */
208 if (call_map == NULL)
209 args->nsid = LM_ID_BASE;
212 args->nsid = call_map->l_ns;
216 assert (_dl_debug_initialize (0, args->nsid)->r_state == RT_CONSISTENT);
218 /* Maybe we have to expand a DST. */
219 if (__builtin_expect (dst != NULL, 0))
221 size_t len = strlen (file);
225 /* Determine how much space we need. We have to allocate the
227 required = DL_DST_REQUIRED (call_map, file, len, _dl_dst_count (dst, 0));
229 /* Get space for the new file name. */
230 new_file = (char *) alloca (required + 1);
232 /* Generate the new file name. */
233 _dl_dst_substitute (call_map, file, new_file, 0);
235 /* If the substitution failed don't try to load. */
236 if (*new_file == '\0')
237 _dl_signal_error (0, "dlopen", NULL,
238 N_("empty dynamic string token substitution"));
240 /* Now we have a new file name. */
243 /* It does not matter whether call_map is set even if we
244 computed it only because of the DST. Since the path contains
245 a slash the value is not used. See dl-load.c. */
248 /* Load the named object. */
249 args->map = new = _dl_map_object (call_map, file, 0, lt_loaded, 0,
250 mode | __RTLD_CALLMAP, args->nsid);
252 /* If the pointer returned is NULL this means the RTLD_NOLOAD flag is
253 set and the object is not already loaded. */
256 assert (mode & RTLD_NOLOAD);
260 if (__builtin_expect (mode & __RTLD_SPROF, 0))
261 /* This happens only if we load a DSO for 'sprof'. */
264 /* This object is directly loaded. */
265 ++new->l_direct_opencount;
267 /* It was already open. */
268 if (__builtin_expect (new->l_searchlist.r_list != NULL, 0))
270 /* Let the user know about the opencount. */
271 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
272 _dl_debug_printf ("opening file=%s [%lu]; direct_opencount=%u\n\n",
273 new->l_name, new->l_ns, new->l_direct_opencount);
275 /* If the user requested the object to be in the global namespace
276 but it is not so far, add it now. */
277 if ((mode & RTLD_GLOBAL) && new->l_global == 0)
278 (void) add_to_global (new);
280 assert (_dl_debug_initialize (0, args->nsid)->r_state == RT_CONSISTENT);
285 /* Load that object's dependencies. */
286 _dl_map_object_deps (new, NULL, 0, 0,
287 mode & (__RTLD_DLOPEN | RTLD_DEEPBIND | __RTLD_AUDIT));
289 /* So far, so good. Now check the versions. */
290 for (i = 0; i < new->l_searchlist.r_nlist; ++i)
291 if (new->l_searchlist.r_list[i]->l_real->l_versions == NULL)
292 (void) _dl_check_map_versions (new->l_searchlist.r_list[i]->l_real,
300 /* Auditing checkpoint: we have added all objects. */
301 if (__builtin_expect (GLRO(dl_naudit) > 0, 0))
303 struct link_map *head = GL(dl_ns)[new->l_ns]._ns_loaded;
304 /* Do not call the functions for any auditing object. */
305 if (head->l_auditing == 0)
307 struct audit_ifaces *afct = GLRO(dl_audit);
308 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
310 if (afct->activity != NULL)
311 afct->activity (&head->l_audit[cnt].cookie, LA_ACT_CONSISTENT);
319 /* Notify the debugger all new objects are now ready to go. */
320 struct r_debug *r = _dl_debug_initialize (0, args->nsid);
321 r->r_state = RT_CONSISTENT;
324 /* Only do lazy relocation if `LD_BIND_NOW' is not set. */
325 lazy = (mode & RTLD_BINDING_MASK) == RTLD_LAZY && GLRO(dl_lazy);
327 /* Relocate the objects loaded. We do this in reverse order so that copy
328 relocs of earlier objects overwrite the data written by later objects. */
335 if (! l->l_real->l_relocated)
338 if (__builtin_expect (GLRO(dl_profile) != NULL, 0))
340 /* If this here is the shared object which we want to profile
341 make sure the profile is started. We can find out whether
342 this is necessary or not by observing the `_dl_profile_map'
343 variable. If was NULL but is not NULL afterwars we must
344 start the profiling. */
345 struct link_map *old_profile_map = GL(dl_profile_map);
347 _dl_relocate_object (l, l->l_scope, 1, 1);
349 if (old_profile_map == NULL && GL(dl_profile_map) != NULL)
351 /* We must prepare the profiling. */
352 _dl_start_profile ();
354 /* Prevent unloading the object. */
355 GL(dl_profile_map)->l_flags_1 |= DF_1_NODELETE;
360 _dl_relocate_object (l, l->l_scope, lazy, 0);
368 /* If the file is not loaded now as a dependency, add the search
369 list of the newly loaded object to the scope. */
370 for (i = 0; i < new->l_searchlist.r_nlist; ++i)
372 struct link_map *imap = new->l_searchlist.r_list[i];
374 /* If the initializer has been called already, the object has
375 not been loaded here and now. */
376 if (imap->l_init_called && imap->l_type == lt_loaded)
378 struct r_scope_elem **runp = imap->l_scope;
381 while (*runp != NULL)
383 if (*runp == &new->l_searchlist)
390 /* Avoid duplicates. */
393 if (__builtin_expect (cnt + 1 >= imap->l_scope_max, 0))
395 /* The 'r_scope' array is too small. Allocate a new one
398 struct r_scope_elem **newp;
400 #define SCOPE_ELEMS(imap) \
401 (sizeof (imap->l_scope_mem) / sizeof (imap->l_scope_mem[0]))
403 if (imap->l_scope != imap->l_scope_mem
404 && imap->l_scope_max < SCOPE_ELEMS (imap))
406 new_size = SCOPE_ELEMS (imap);
407 newp = imap->l_scope_mem;
411 new_size = imap->l_scope_max * 2;
412 newp = (struct r_scope_elem **)
413 malloc (new_size * sizeof (struct r_scope_elem *));
415 _dl_signal_error (ENOMEM, "dlopen", NULL,
416 N_("cannot create scope list"));
419 memcpy (newp, imap->l_scope, cnt * sizeof (imap->l_scope[0]));
420 struct r_scope_elem **old = imap->l_scope;
423 imap->l_scope = newp;
426 __rtld_mrlock_change (imap->l_scope_lock);
427 imap->l_scope = newp;
428 __rtld_mrlock_done (imap->l_scope_lock);
431 if (old != imap->l_scope_mem)
434 imap->l_scope_max = new_size;
437 /* First terminate the extended list. Otherwise a thread
438 might use the new last element and then use the garbage
440 imap->l_scope[cnt + 1] = NULL;
441 atomic_write_barrier ();
442 imap->l_scope[cnt] = &new->l_searchlist;
445 /* Only add TLS memory if this object is loaded now and
446 therefore is not yet initialized. */
447 else if (! imap->l_init_called
448 /* Only if the module defines thread local data. */
449 && __builtin_expect (imap->l_tls_blocksize > 0, 0))
451 /* Now that we know the object is loaded successfully add
452 modules containing TLS data to the slot info table. We
453 might have to increase its size. */
454 _dl_add_to_slotinfo (imap);
456 if (imap->l_need_tls_init)
458 imap->l_need_tls_init = 0;
460 /* Update the slot information data for at least the
461 generation of the DSO we are allocating data for. */
462 _dl_update_slotinfo (imap->l_tls_modid);
465 GL(dl_init_static_tls) (imap);
466 assert (imap->l_need_tls_init == 0);
469 /* We have to bump the generation counter. */
476 /* Bump the generation number if necessary. */
477 if (any_tls && __builtin_expect (++GL(dl_tls_generation) == 0, 0))
478 _dl_fatal_printf (N_("\
479 TLS generation counter wrapped! Please report this."));
482 /* Run the initializer functions of new objects. */
483 _dl_init (new, args->argc, args->argv, args->env);
485 /* Now we can make the new map available in the global scope. */
486 if (mode & RTLD_GLOBAL)
487 /* Move the object in the global namespace. */
488 if (add_to_global (new) != 0)
492 /* Mark the object as not deletable if the RTLD_NODELETE flags was
494 if (__builtin_expect (mode & RTLD_NODELETE, 0))
495 new->l_flags_1 |= DF_1_NODELETE;
498 /* We must be the static _dl_open in libc.a. A static program that
499 has loaded a dynamic object now has competition. */
500 __libc_multiple_libcs = 1;
503 /* Let the user know about the opencount. */
504 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
505 _dl_debug_printf ("opening file=%s [%lu]; direct_opencount=%u\n\n",
506 new->l_name, new->l_ns, new->l_direct_opencount);
511 _dl_open (const char *file, int mode, const void *caller_dlopen, Lmid_t nsid,
512 int argc, char *argv[], char *env[])
514 if ((mode & RTLD_BINDING_MASK) == 0)
515 /* One of the flags must be set. */
516 _dl_signal_error (EINVAL, file, NULL, N_("invalid mode for dlopen()"));
518 /* Make sure we are alone. */
519 __rtld_lock_lock_recursive (GL(dl_load_lock));
521 if (nsid == LM_ID_NEWLM)
523 /* Find a new namespace. */
524 for (nsid = 1; nsid < DL_NNS; ++nsid)
525 if (GL(dl_ns)[nsid]._ns_loaded == NULL)
530 /* No more namespace available. */
531 __rtld_lock_unlock_recursive (GL(dl_load_lock));
533 _dl_signal_error (EINVAL, file, NULL, N_("\
534 no more namespaces available for dlmopen()"));
537 _dl_debug_initialize (0, nsid)->r_state = RT_CONSISTENT;
539 /* Never allow loading a DSO in a namespace which is empty. Such
540 direct placements is only causing problems. Also don't allow
541 loading into a namespace used for auditing. */
542 else if (nsid != LM_ID_BASE && nsid != __LM_ID_CALLER
543 && (GL(dl_ns)[nsid]._ns_nloaded == 0
544 || GL(dl_ns)[nsid]._ns_loaded->l_auditing))
545 _dl_signal_error (EINVAL, file, NULL,
546 N_("invalid target namespace in dlmopen()"));
548 struct dl_open_args args;
551 args.caller_dlopen = caller_dlopen;
552 args.caller_dl_open = RETURN_ADDRESS (0);
560 const char *errstring;
562 int errcode = _dl_catch_error (&objname, &errstring, &malloced,
563 dl_open_worker, &args);
566 /* We must munmap() the cache file. */
570 /* See if an error occurred during loading. */
571 if (__builtin_expect (errstring != NULL, 0))
573 /* Remove the object from memory. It may be in an inconsistent
574 state if relocation failed, for example. */
578 /* Maybe some of the modules which were loaded use TLS.
579 Since it will be removed in the following _dl_close call
580 we have to mark the dtv array as having gaps to fill the
581 holes. This is a pessimistic assumption which won't hurt
582 if not true. There is no need to do this when we are
583 loading the auditing DSOs since TLS has not yet been set
585 if ((mode & __RTLD_AUDIT) == 0)
586 GL(dl_tls_dtv_gaps) = true;
589 _dl_close (args.map);
592 assert (_dl_debug_initialize (0, args.nsid)->r_state == RT_CONSISTENT);
594 /* Release the lock. */
595 __rtld_lock_unlock_recursive (GL(dl_load_lock));
597 /* Make a local copy of the error string so that we can release the
598 memory allocated for it. */
599 size_t len_errstring = strlen (errstring) + 1;
600 char *local_errstring;
601 if (objname == errstring + len_errstring)
603 size_t total_len = len_errstring + strlen (objname) + 1;
604 local_errstring = alloca (total_len);
605 memcpy (local_errstring, errstring, total_len);
606 objname = local_errstring + len_errstring;
610 local_errstring = alloca (len_errstring);
611 memcpy (local_errstring, errstring, len_errstring);
615 free ((char *) errstring);
617 /* Reraise the error. */
618 _dl_signal_error (errcode, objname, NULL, local_errstring);
621 assert (_dl_debug_initialize (0, args.nsid)->r_state == RT_CONSISTENT);
623 /* Release the lock. */
624 __rtld_lock_unlock_recursive (GL(dl_load_lock));
627 DL_STATIC_INIT (args.map);
638 show_scope (struct link_map *new)
642 for (scope_cnt = 0; new->l_scope[scope_cnt] != NULL; ++scope_cnt)
647 numbuf[0] = '0' + scope_cnt;
649 _dl_printf ("scope %s:", numbuf);
651 for (cnt = 0; cnt < new->l_scope[scope_cnt]->r_nlist; ++cnt)
652 if (*new->l_scope[scope_cnt]->r_list[cnt]->l_name)
653 _dl_printf (" %s", new->l_scope[scope_cnt]->r_list[cnt]->l_name);
655 _dl_printf (" <main>");