1 /* Load a shared object at runtime, relocate it, and run its initializer.
2 Copyright (C) 1996-2001, 2002, 2003, 2004 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>
33 #include <gnu/lib-names.h>
39 /* Giving this initialized value preallocates some surplus bytes in the
40 static TLS area, see __libc_setup_tls (libc-tls.c). */
41 size_t _dl_tls_static_size = 2048;
44 extern ElfW(Addr) _dl_sysdep_start (void **start_argptr,
45 void (*dl_main) (const ElfW(Phdr) *phdr,
47 ElfW(Addr) *user_entry));
48 weak_extern (BP_SYM (_dl_sysdep_start))
50 extern int __libc_multiple_libcs; /* Defined in init-first.c. */
52 extern int __libc_argc attribute_hidden;
53 extern char **__libc_argv attribute_hidden;
55 extern char **__environ;
57 /* Undefine the following for debugging. */
58 /* #define SCOPE_DEBUG 1 */
60 static void show_scope (struct link_map *new);
63 /* We must be carefull not to leave us in an inconsistent state. Thus we
64 catch any error and re-raise it after cleaning up. */
70 /* This is the caller of the dlopen() function. */
71 const void *caller_dlopen;
72 /* This is the caller if _dl_open(). */
73 const void *caller_dl_open;
79 add_to_global (struct link_map *new)
81 struct link_map **new_global;
82 unsigned int to_add = 0;
85 /* Count the objects we have to put in the global scope. */
86 for (cnt = 0; cnt < new->l_searchlist.r_nlist; ++cnt)
87 if (new->l_searchlist.r_list[cnt]->l_global == 0)
90 /* The symbols of the new objects and its dependencies are to be
91 introduced into the global scope that will be used to resolve
92 references from other dynamically-loaded objects.
94 The global scope is the searchlist in the main link map. We
95 extend this list if necessary. There is one problem though:
96 since this structure was allocated very early (before the libc
97 is loaded) the memory it uses is allocated by the malloc()-stub
98 in the ld.so. When we come here these functions are not used
99 anymore. Instead the malloc() implementation of the libc is
100 used. But this means the block from the main map cannot be used
101 in an realloc() call. Therefore we allocate a completely new
102 array the first time we have to add something to the locale scope. */
104 if (GL(dl_global_scope_alloc) == 0)
106 /* This is the first dynamic object given global scope. */
107 GL(dl_global_scope_alloc) = GL(dl_main_searchlist)->r_nlist + to_add + 8;
108 new_global = (struct link_map **)
109 malloc (GL(dl_global_scope_alloc) * sizeof (struct link_map *));
110 if (new_global == NULL)
112 GL(dl_global_scope_alloc) = 0;
114 GLRO(dl_signal_error) (ENOMEM, new->l_libname->name, NULL,
115 N_("cannot extend global scope"));
119 /* Copy over the old entries. */
120 memcpy (new_global, GL(dl_main_searchlist)->r_list,
121 (GL(dl_main_searchlist)->r_nlist * sizeof (struct link_map *)));
123 GL(dl_main_searchlist)->r_list = new_global;
125 else if (GL(dl_main_searchlist)->r_nlist + to_add
126 > GL(dl_global_scope_alloc))
128 /* We have to extend the existing array of link maps in the
130 new_global = (struct link_map **)
131 realloc (GL(dl_main_searchlist)->r_list,
132 ((GL(dl_global_scope_alloc) + to_add + 8)
133 * sizeof (struct link_map *)));
134 if (new_global == NULL)
137 GL(dl_global_scope_alloc) += to_add + 8;
138 GL(dl_main_searchlist)->r_list = new_global;
141 /* Now add the new entries. */
142 for (cnt = 0; cnt < new->l_searchlist.r_nlist; ++cnt)
144 struct link_map *map = new->l_searchlist.r_list[cnt];
146 if (map->l_global == 0)
149 GL(dl_main_searchlist)->r_list[GL(dl_main_searchlist)->r_nlist]
151 ++GL(dl_main_searchlist)->r_nlist;
162 check_libc_caller (const void *caller)
164 static const char expected1[] = LIBC_SO;
165 static const char expected2[] = LIBDL_SO;
167 /* If we already know the address ranges, just test. */
168 static const void *expected1_from;
169 static const void *expected1_to;
170 static const void *expected2_from;
171 static const void *expected2_to;
173 if (expected1_from == NULL)
175 /* The only other DSO which is allowed to call these functions is
176 libdl. Find the address range containing the caller. */
179 for (l = GL(dl_loaded); l != NULL; l = l->l_next)
180 if (strcmp (expected1, l->l_name) == 0)
183 expected1_from = (const void *) l->l_map_start;
184 expected1_to = (const void *) l->l_map_end;
186 else if (strcmp (expected1, l->l_name) == 0)
189 expected2_from = (const void *) l->l_map_start;
190 expected2_to = (const void *) l->l_map_end;
194 struct libname_list *runp = l->l_libname;
198 if (strcmp (expected1, runp->name) == 0)
200 else if (strcmp (expected2, runp->name) == 0)
207 assert (expected1_from != NULL);
210 /* When there would be more than two expected caller we could use an
211 array for the values but for now this is cheaper. */
212 if ((caller >= expected1_from && caller < expected1_to)
213 || (caller >= expected2_from && caller < expected2_to))
222 dl_open_worker (void *a)
224 struct dl_open_args *args = a;
225 const char *file = args->file;
226 int mode = args->mode;
227 struct link_map *new, *l;
236 /* Check whether _dl_open() has been called from a valid DSO. */
237 if (check_libc_caller (args->caller_dl_open) != 0)
238 GLRO(dl_signal_error) (0, "dlopen", NULL, N_("invalid caller"));
241 /* Maybe we have to expand a DST. */
242 dst = strchr (file, '$');
243 if (__builtin_expect (dst != NULL, 0))
245 const void *caller_dlopen = args->caller_dlopen;
246 size_t len = strlen (file);
248 struct link_map *call_map;
251 /* DSTs must not appear in SUID/SGID programs. */
252 if (__libc_enable_secure)
253 /* This is an error. */
254 GLRO(dl_signal_error) (0, "dlopen", NULL,
255 N_("DST not allowed in SUID/SGID programs"));
257 /* We have to find out from which object the caller is calling. */
259 for (l = GL(dl_loaded); l; l = l->l_next)
260 if (caller_dlopen >= (const void *) l->l_map_start
261 && caller_dlopen < (const void *) l->l_map_end)
263 /* There must be exactly one DSO for the range of the virtual
264 memory. Otherwise something is really broken. */
269 if (call_map == NULL)
270 /* In this case we assume this is the main application. */
271 call_map = GL(dl_loaded);
273 /* Determine how much space we need. We have to allocate the
275 required = DL_DST_REQUIRED (call_map, file, len, _dl_dst_count (dst, 0));
277 /* Get space for the new file name. */
278 new_file = (char *) alloca (required + 1);
280 /* Generate the new file name. */
281 _dl_dst_substitute (call_map, file, new_file, 0);
283 /* If the substitution failed don't try to load. */
284 if (*new_file == '\0')
285 GLRO(dl_signal_error) (0, "dlopen", NULL,
286 N_("empty dynamic string token substitution"));
288 /* Now we have a new file name. */
292 /* Load the named object. */
293 args->map = new = GLRO(dl_map_object) (NULL, file, 0, lt_loaded, 0, mode);
295 /* If the pointer returned is NULL this means the RTLD_NOLOAD flag is
296 set and the object is not already loaded. */
299 assert (mode & RTLD_NOLOAD);
303 if (__builtin_expect (mode & __RTLD_SPROF, 0))
304 /* This happens only if we load a DSO for 'sprof'. */
307 /* It was already open. */
308 if (__builtin_expect (new->l_searchlist.r_list != NULL, 0))
310 /* Let the user know about the opencount. */
311 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
312 GLRO(dl_debug_printf) ("opening file=%s; opencount == %u\n\n",
313 new->l_name, new->l_opencount);
315 /* If the user requested the object to be in the global namespace
316 but it is not so far, add it now. */
317 if ((mode & RTLD_GLOBAL) && new->l_global == 0)
318 (void) add_to_global (new);
320 /* Increment just the reference counter of the object. */
326 /* Load that object's dependencies. */
327 GLRO(dl_map_object_deps) (new, NULL, 0, 0, mode & __RTLD_DLOPEN);
329 /* So far, so good. Now check the versions. */
330 for (i = 0; i < new->l_searchlist.r_nlist; ++i)
331 if (new->l_searchlist.r_list[i]->l_versions == NULL)
332 (void) GLRO(dl_check_map_versions) (new->l_searchlist.r_list[i], 0, 0);
338 /* Only do lazy relocation if `LD_BIND_NOW' is not set. */
339 lazy = (mode & RTLD_BINDING_MASK) == RTLD_LAZY && GLRO(dl_lazy);
341 /* Relocate the objects loaded. We do this in reverse order so that copy
342 relocs of earlier objects overwrite the data written by later objects. */
349 if (! l->l_relocated)
352 if (GLRO(dl_profile) != NULL)
354 /* If this here is the shared object which we want to profile
355 make sure the profile is started. We can find out whether
356 this is necessary or not by observing the `_dl_profile_map'
357 variable. If was NULL but is not NULL afterwars we must
358 start the profiling. */
359 struct link_map *old_profile_map = GL(dl_profile_map);
361 GLRO(dl_relocate_object) (l, l->l_scope, 1, 1);
363 if (old_profile_map == NULL && GL(dl_profile_map) != NULL)
364 /* We must prepare the profiling. */
365 GLRO(dl_start_profile) ();
369 GLRO(dl_relocate_object) (l, l->l_scope, lazy, 0);
378 /* Do static TLS initialization now if it has been delayed because
379 the TLS template might not be fully relocated at _dl_allocate_static_tls
381 for (l = new; l; l = l->l_next)
382 if (l->l_need_tls_init)
384 l->l_need_tls_init = 0;
385 GL(dl_init_static_tls) (l);
388 /* We normally don't bump the TLS generation counter. There must be
389 actually a need to do this. */
393 /* Increment the open count for all dependencies. If the file is
394 not loaded as a dependency here add the search list of the newly
395 loaded object to the scope. */
396 for (i = 0; i < new->l_searchlist.r_nlist; ++i)
397 if (++new->l_searchlist.r_list[i]->l_opencount > 1
398 && new->l_searchlist.r_list[i]->l_type == lt_loaded)
400 struct link_map *imap = new->l_searchlist.r_list[i];
401 struct r_scope_elem **runp = imap->l_scope;
404 while (*runp != NULL)
406 /* This can happen if imap was just loaded, but during
407 relocation had l_opencount bumped because of relocation
408 dependency. Avoid duplicates in l_scope. */
409 if (__builtin_expect (*runp == &new->l_searchlist, 0))
417 /* Avoid duplicates. */
420 if (__builtin_expect (cnt + 1 >= imap->l_scope_max, 0))
422 /* The 'r_scope' array is too small. Allocate a new one
424 struct r_scope_elem **newp;
425 size_t new_size = imap->l_scope_max * 2;
427 if (imap->l_scope == imap->l_scope_mem)
429 newp = (struct r_scope_elem **)
430 malloc (new_size * sizeof (struct r_scope_elem *));
432 GLRO(dl_signal_error) (ENOMEM, "dlopen", NULL,
433 N_("cannot create scope list"));
434 imap->l_scope = memcpy (newp, imap->l_scope,
435 cnt * sizeof (imap->l_scope[0]));
439 newp = (struct r_scope_elem **)
440 realloc (imap->l_scope,
441 new_size * sizeof (struct r_scope_elem *));
443 GLRO(dl_signal_error) (ENOMEM, "dlopen", NULL,
444 N_("cannot create scope list"));
445 imap->l_scope = newp;
448 imap->l_scope_max = new_size;
451 imap->l_scope[cnt++] = &new->l_searchlist;
452 imap->l_scope[cnt] = NULL;
455 else if (new->l_searchlist.r_list[i]->l_opencount == 1
456 /* Only if the module defines thread local data. */
457 && __builtin_expect (new->l_searchlist.r_list[i]->l_tls_blocksize
460 /* Now that we know the object is loaded successfully add
461 modules containing TLS data to the dtv info table. We
462 might have to increase its size. */
463 struct dtv_slotinfo_list *listp;
464 struct dtv_slotinfo_list *prevp;
465 size_t idx = new->l_searchlist.r_list[i]->l_tls_modid;
467 assert (new->l_searchlist.r_list[i]->l_type == lt_loaded);
469 /* Find the place in the dtv slotinfo list. */
470 listp = GL(dl_tls_dtv_slotinfo_list);
471 prevp = NULL; /* Needed to shut up gcc. */
474 /* Does it fit in the array of this list element? */
475 if (idx < listp->len)
481 while (listp != NULL);
485 /* When we come here it means we have to add a new element
486 to the slotinfo list. And the new module must be in
490 listp = prevp->next = (struct dtv_slotinfo_list *)
491 malloc (sizeof (struct dtv_slotinfo_list)
492 + TLS_SLOTINFO_SURPLUS * sizeof (struct dtv_slotinfo));
495 /* We ran out of memory. We will simply fail this
496 call but don't undo anything we did so far. The
497 application will crash or be terminated anyway very
500 /* We have to do this since some entries in the dtv
501 slotinfo array might already point to this
503 ++GL(dl_tls_generation);
505 GLRO(dl_signal_error) (ENOMEM, "dlopen", NULL, N_("\
506 cannot create TLS data structures"));
509 listp->len = TLS_SLOTINFO_SURPLUS;
511 memset (listp->slotinfo, '\0',
512 TLS_SLOTINFO_SURPLUS * sizeof (struct dtv_slotinfo));
515 /* Add the information into the slotinfo data structure. */
516 listp->slotinfo[idx].map = new->l_searchlist.r_list[i];
517 listp->slotinfo[idx].gen = GL(dl_tls_generation) + 1;
519 /* We have to bump the generation counter. */
523 /* Bump the generation number if necessary. */
525 if (__builtin_expect (++GL(dl_tls_generation) == 0, 0))
526 __libc_fatal (_("TLS generation counter wrapped! Please send report with the 'glibcbug' script."));
529 /* Run the initializer functions of new objects. */
530 GLRO(dl_init) (new, __libc_argc, __libc_argv, __environ);
532 /* Now we can make the new map available in the global scope. */
533 if (mode & RTLD_GLOBAL)
534 /* Move the object in the global namespace. */
535 if (add_to_global (new) != 0)
539 /* Mark the object as not deletable if the RTLD_NODELETE flags was
541 if (__builtin_expect (mode & RTLD_NODELETE, 0))
542 new->l_flags_1 |= DF_1_NODELETE;
545 /* We must be the static _dl_open in libc.a. A static program that
546 has loaded a dynamic object now has competition. */
547 __libc_multiple_libcs = 1;
550 /* Let the user know about the opencount. */
551 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
552 GLRO(dl_debug_printf) ("opening file=%s; opencount == %u\n\n",
553 new->l_name, new->l_opencount);
559 _dl_open (const char *file, int mode, const void *caller_dlopen)
561 struct dl_open_args args;
563 const char *errstring;
566 if ((mode & RTLD_BINDING_MASK) == 0)
567 /* One of the flags must be set. */
568 GLRO(dl_signal_error) (EINVAL, file, NULL,
569 N_("invalid mode for dlopen()"));
571 /* Make sure we are alone. */
572 __rtld_lock_lock_recursive (GL(dl_load_lock));
576 args.caller_dlopen = caller_dlopen;
577 args.caller_dl_open = RETURN_ADDRESS (0);
579 errcode = GLRO(dl_catch_error) (&objname, &errstring, dl_open_worker, &args);
582 /* We must munmap() the cache file. */
583 GLRO(dl_unload_cache) ();
586 /* Release the lock. */
587 __rtld_lock_unlock_recursive (GL(dl_load_lock));
589 if (__builtin_expect (errstring != NULL, 0))
591 /* Some error occurred during loading. */
592 char *local_errstring;
593 size_t len_errstring;
595 /* Remove the object from memory. It may be in an inconsistent
596 state if relocation failed, for example. */
601 /* Increment open counters for all objects since this
602 sometimes has not happened yet. */
603 if (args.map->l_searchlist.r_list[0]->l_opencount == 0)
604 for (i = 0; i < args.map->l_searchlist.r_nlist; ++i)
605 ++args.map->l_searchlist.r_list[i]->l_opencount;
608 /* Maybe some of the modules which were loaded uses TLS.
609 Since it will be removed in the following _dl_close call
610 we have to mark the dtv array as having gaps to fill
611 the holes. This is a pessimistic assumption which won't
613 GL(dl_tls_dtv_gaps) = true;
616 _dl_close (args.map);
619 /* Make a local copy of the error string so that we can release the
620 memory allocated for it. */
621 len_errstring = strlen (errstring) + 1;
622 if (objname == errstring + len_errstring)
624 size_t total_len = len_errstring + strlen (objname) + 1;
625 local_errstring = alloca (total_len);
626 memcpy (local_errstring, errstring, total_len);
627 objname = local_errstring + len_errstring;
631 local_errstring = alloca (len_errstring);
632 memcpy (local_errstring, errstring, len_errstring);
635 if (errstring != _dl_out_of_memory)
636 free ((char *) errstring);
638 /* Reraise the error. */
639 GLRO(dl_signal_error) (errcode, objname, NULL, local_errstring);
643 DL_STATIC_INIT (args.map);
648 libc_hidden_def (_dl_open)
655 show_scope (struct link_map *new)
659 for (scope_cnt = 0; new->l_scope[scope_cnt] != NULL; ++scope_cnt)
664 numbuf[0] = '0' + scope_cnt;
666 _dl_printf ("scope %s:", numbuf);
668 for (cnt = 0; cnt < new->l_scope[scope_cnt]->r_nlist; ++cnt)
669 if (*new->l_scope[scope_cnt]->r_list[cnt]->l_name)
670 _dl_printf (" %s", new->l_scope[scope_cnt]->r_list[cnt]->l_name);
672 _dl_printf (" <main>");