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>
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;
160 dl_open_worker (void *a)
162 struct dl_open_args *args = a;
163 const char *file = args->file;
164 int mode = args->mode;
165 struct link_map *new, *l;
171 struct link_map *call_map = NULL;
173 /* Check whether _dl_open() has been called from a valid DSO. */
174 if (__check_caller (args->caller_dl_open, allow_libc|allow_libdl) != 0)
175 GLRO(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 or when the file name has no path in which case we
179 need to look along the RUNPATH/RPATH of the caller. */
180 const char *dst = strchr (file, '$');
181 if (dst != NULL || strchr (file, '/') == NULL)
183 const void *caller_dlopen = args->caller_dlopen;
185 /* We have to find out from which object the caller is calling. */
187 for (l = GL(dl_loaded); l; l = l->l_next)
188 if (caller_dlopen >= (const void *) l->l_map_start
189 && caller_dlopen < (const void *) l->l_map_end)
191 /* There must be exactly one DSO for the range of the virtual
192 memory. Otherwise something is really broken. */
197 if (call_map == NULL)
198 /* In this case we assume this is the main application. */
199 call_map = GL(dl_loaded);
202 /* Maybe we have to expand a DST. */
203 if (__builtin_expect (dst != NULL, 0))
205 size_t len = strlen (file);
209 /* DSTs must not appear in SUID/SGID programs. */
210 if (__libc_enable_secure)
211 /* This is an error. */
212 GLRO(dl_signal_error) (0, "dlopen", NULL,
213 N_("DST not allowed in SUID/SGID programs"));
216 /* Determine how much space we need. We have to allocate the
218 required = DL_DST_REQUIRED (call_map, file, len, _dl_dst_count (dst, 0));
220 /* Get space for the new file name. */
221 new_file = (char *) alloca (required + 1);
223 /* Generate the new file name. */
224 _dl_dst_substitute (call_map, file, new_file, 0);
226 /* If the substitution failed don't try to load. */
227 if (*new_file == '\0')
228 GLRO(dl_signal_error) (0, "dlopen", NULL,
229 N_("empty dynamic string token substitution"));
231 /* Now we have a new file name. */
234 /* It does not matter whether call_map is set even if we
235 computed it only because of the DST. Since the path contains
236 a slash the value is not used. See dl-load.c. */
239 /* Load the named object. */
240 args->map = new = GLRO(dl_map_object) (call_map, file, 0, lt_loaded, 0,
241 mode | __RTLD_CALLMAP);
243 /* If the pointer returned is NULL this means the RTLD_NOLOAD flag is
244 set and the object is not already loaded. */
247 assert (mode & RTLD_NOLOAD);
251 if (__builtin_expect (mode & __RTLD_SPROF, 0))
252 /* This happens only if we load a DSO for 'sprof'. */
255 /* It was already open. */
256 if (__builtin_expect (new->l_searchlist.r_list != NULL, 0))
258 /* Let the user know about the opencount. */
259 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
260 GLRO(dl_debug_printf) ("opening file=%s; opencount == %u\n\n",
261 new->l_name, new->l_opencount);
263 /* If the user requested the object to be in the global namespace
264 but it is not so far, add it now. */
265 if ((mode & RTLD_GLOBAL) && new->l_global == 0)
266 (void) add_to_global (new);
268 /* Increment just the reference counter of the object. */
274 /* Load that object's dependencies. */
275 GLRO(dl_map_object_deps) (new, NULL, 0, 0,
276 mode & (__RTLD_DLOPEN | RTLD_DEEPBIND));
278 /* So far, so good. Now check the versions. */
279 for (i = 0; i < new->l_searchlist.r_nlist; ++i)
280 if (new->l_searchlist.r_list[i]->l_versions == NULL)
281 (void) GLRO(dl_check_map_versions) (new->l_searchlist.r_list[i], 0, 0);
287 /* Only do lazy relocation if `LD_BIND_NOW' is not set. */
288 lazy = (mode & RTLD_BINDING_MASK) == RTLD_LAZY && GLRO(dl_lazy);
290 /* Relocate the objects loaded. We do this in reverse order so that copy
291 relocs of earlier objects overwrite the data written by later objects. */
298 if (! l->l_relocated)
301 if (GLRO(dl_profile) != NULL)
303 /* If this here is the shared object which we want to profile
304 make sure the profile is started. We can find out whether
305 this is necessary or not by observing the `_dl_profile_map'
306 variable. If was NULL but is not NULL afterwars we must
307 start the profiling. */
308 struct link_map *old_profile_map = GL(dl_profile_map);
310 GLRO(dl_relocate_object) (l, l->l_scope, 1, 1);
312 if (old_profile_map == NULL && GL(dl_profile_map) != NULL)
314 /* We must prepare the profiling. */
315 GLRO(dl_start_profile) ();
317 /* Prevent unloading the object. */
318 GL(dl_profile_map)->l_flags_1 |= DF_1_NODELETE;
323 GLRO(dl_relocate_object) (l, l->l_scope, lazy, 0);
332 /* Do static TLS initialization now if it has been delayed because
333 the TLS template might not be fully relocated at _dl_allocate_static_tls
335 for (l = new; l; l = l->l_next)
336 if (l->l_need_tls_init)
338 l->l_need_tls_init = 0;
339 GL(dl_init_static_tls) (l);
342 /* We normally don't bump the TLS generation counter. There must be
343 actually a need to do this. */
347 /* Increment the open count for all dependencies. If the file is
348 not loaded as a dependency here add the search list of the newly
349 loaded object to the scope. */
350 for (i = 0; i < new->l_searchlist.r_nlist; ++i)
351 if (++new->l_searchlist.r_list[i]->l_opencount > 1
352 && new->l_searchlist.r_list[i]->l_type == lt_loaded)
354 struct link_map *imap = new->l_searchlist.r_list[i];
355 struct r_scope_elem **runp = imap->l_scope;
358 while (*runp != NULL)
360 /* This can happen if imap was just loaded, but during
361 relocation had l_opencount bumped because of relocation
362 dependency. Avoid duplicates in l_scope. */
363 if (__builtin_expect (*runp == &new->l_searchlist, 0))
371 /* Avoid duplicates. */
374 if (__builtin_expect (cnt + 1 >= imap->l_scope_max, 0))
376 /* The 'r_scope' array is too small. Allocate a new one
378 struct r_scope_elem **newp;
379 size_t new_size = imap->l_scope_max * 2;
381 if (imap->l_scope == imap->l_scope_mem)
383 newp = (struct r_scope_elem **)
384 malloc (new_size * sizeof (struct r_scope_elem *));
386 GLRO(dl_signal_error) (ENOMEM, "dlopen", NULL,
387 N_("cannot create scope list"));
388 imap->l_scope = memcpy (newp, imap->l_scope,
389 cnt * sizeof (imap->l_scope[0]));
393 newp = (struct r_scope_elem **)
394 realloc (imap->l_scope,
395 new_size * sizeof (struct r_scope_elem *));
397 GLRO(dl_signal_error) (ENOMEM, "dlopen", NULL,
398 N_("cannot create scope list"));
399 imap->l_scope = newp;
402 imap->l_scope_max = new_size;
405 imap->l_scope[cnt++] = &new->l_searchlist;
406 imap->l_scope[cnt] = NULL;
409 else if (new->l_searchlist.r_list[i]->l_opencount == 1
410 /* Only if the module defines thread local data. */
411 && __builtin_expect (new->l_searchlist.r_list[i]->l_tls_blocksize
414 /* Now that we know the object is loaded successfully add
415 modules containing TLS data to the dtv info table. We
416 might have to increase its size. */
417 struct dtv_slotinfo_list *listp;
418 struct dtv_slotinfo_list *prevp;
419 size_t idx = new->l_searchlist.r_list[i]->l_tls_modid;
421 assert (new->l_searchlist.r_list[i]->l_type == lt_loaded);
423 /* Find the place in the dtv slotinfo list. */
424 listp = GL(dl_tls_dtv_slotinfo_list);
425 prevp = NULL; /* Needed to shut up gcc. */
428 /* Does it fit in the array of this list element? */
429 if (idx < listp->len)
435 while (listp != NULL);
439 /* When we come here it means we have to add a new element
440 to the slotinfo list. And the new module must be in
444 listp = prevp->next = (struct dtv_slotinfo_list *)
445 malloc (sizeof (struct dtv_slotinfo_list)
446 + TLS_SLOTINFO_SURPLUS * sizeof (struct dtv_slotinfo));
449 /* We ran out of memory. We will simply fail this
450 call but don't undo anything we did so far. The
451 application will crash or be terminated anyway very
454 /* We have to do this since some entries in the dtv
455 slotinfo array might already point to this
457 ++GL(dl_tls_generation);
459 GLRO(dl_signal_error) (ENOMEM, "dlopen", NULL, N_("\
460 cannot create TLS data structures"));
463 listp->len = TLS_SLOTINFO_SURPLUS;
465 memset (listp->slotinfo, '\0',
466 TLS_SLOTINFO_SURPLUS * sizeof (struct dtv_slotinfo));
469 /* Add the information into the slotinfo data structure. */
470 listp->slotinfo[idx].map = new->l_searchlist.r_list[i];
471 listp->slotinfo[idx].gen = GL(dl_tls_generation) + 1;
473 /* We have to bump the generation counter. */
477 /* Bump the generation number if necessary. */
479 if (__builtin_expect (++GL(dl_tls_generation) == 0, 0))
480 __libc_fatal (_("TLS generation counter wrapped! Please send report with the 'glibcbug' script."));
483 /* Run the initializer functions of new objects. */
484 GLRO(dl_init) (new, __libc_argc, __libc_argv, __environ);
486 /* Now we can make the new map available in the global scope. */
487 if (mode & RTLD_GLOBAL)
488 /* Move the object in the global namespace. */
489 if (add_to_global (new) != 0)
493 /* Mark the object as not deletable if the RTLD_NODELETE flags was
495 if (__builtin_expect (mode & RTLD_NODELETE, 0))
496 new->l_flags_1 |= DF_1_NODELETE;
499 /* We must be the static _dl_open in libc.a. A static program that
500 has loaded a dynamic object now has competition. */
501 __libc_multiple_libcs = 1;
504 /* Let the user know about the opencount. */
505 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
506 GLRO(dl_debug_printf) ("opening file=%s; opencount == %u\n\n",
507 new->l_name, new->l_opencount);
513 _dl_open (const char *file, int mode, const void *caller_dlopen)
515 struct dl_open_args args;
517 const char *errstring;
520 if ((mode & RTLD_BINDING_MASK) == 0)
521 /* One of the flags must be set. */
522 GLRO(dl_signal_error) (EINVAL, file, NULL,
523 N_("invalid mode for dlopen()"));
525 /* Make sure we are alone. */
526 __rtld_lock_lock_recursive (GL(dl_load_lock));
530 args.caller_dlopen = caller_dlopen;
531 args.caller_dl_open = RETURN_ADDRESS (0);
533 errcode = GLRO(dl_catch_error) (&objname, &errstring, dl_open_worker, &args);
536 /* We must munmap() the cache file. */
537 GLRO(dl_unload_cache) ();
540 /* Release the lock. */
541 __rtld_lock_unlock_recursive (GL(dl_load_lock));
543 if (__builtin_expect (errstring != NULL, 0))
545 /* Some error occurred during loading. */
546 char *local_errstring;
547 size_t len_errstring;
549 /* Remove the object from memory. It may be in an inconsistent
550 state if relocation failed, for example. */
555 /* Increment open counters for all objects since this
556 sometimes has not happened yet. */
557 if (args.map->l_searchlist.r_list[0]->l_opencount == 0)
558 for (i = 0; i < args.map->l_searchlist.r_nlist; ++i)
559 ++args.map->l_searchlist.r_list[i]->l_opencount;
562 /* Maybe some of the modules which were loaded uses TLS.
563 Since it will be removed in the following _dl_close call
564 we have to mark the dtv array as having gaps to fill
565 the holes. This is a pessimistic assumption which won't
567 GL(dl_tls_dtv_gaps) = true;
570 _dl_close (args.map);
573 /* Make a local copy of the error string so that we can release the
574 memory allocated for it. */
575 len_errstring = strlen (errstring) + 1;
576 if (objname == errstring + len_errstring)
578 size_t total_len = len_errstring + strlen (objname) + 1;
579 local_errstring = alloca (total_len);
580 memcpy (local_errstring, errstring, total_len);
581 objname = local_errstring + len_errstring;
585 local_errstring = alloca (len_errstring);
586 memcpy (local_errstring, errstring, len_errstring);
589 if (errstring != _dl_out_of_memory)
590 free ((char *) errstring);
592 /* Reraise the error. */
593 GLRO(dl_signal_error) (errcode, objname, NULL, local_errstring);
597 DL_STATIC_INIT (args.map);
602 libc_hidden_def (_dl_open)
609 show_scope (struct link_map *new)
613 for (scope_cnt = 0; new->l_scope[scope_cnt] != NULL; ++scope_cnt)
618 numbuf[0] = '0' + scope_cnt;
620 _dl_printf ("scope %s:", numbuf);
622 for (cnt = 0; cnt < new->l_scope[scope_cnt]->r_nlist; ++cnt)
623 if (*new->l_scope[scope_cnt]->r_list[cnt]->l_name)
624 _dl_printf (" %s", new->l_scope[scope_cnt]->r_list[cnt]->l_name);
626 _dl_printf (" <main>");