1 /* Load a shared object at runtime, relocate it, and run its initializer.
2 Copyright (C) 1996-2001, 2002 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
27 #include <sys/mman.h> /* Check whether MAP_COPY is defined. */
28 #include <sys/param.h>
29 #include <bits/libc-lock.h>
36 extern ElfW(Addr) _dl_sysdep_start (void **start_argptr,
37 void (*dl_main) (const ElfW(Phdr) *phdr,
39 ElfW(Addr) *user_entry));
40 weak_extern (BP_SYM (_dl_sysdep_start))
42 extern int __libc_multiple_libcs; /* Defined in init-first.c. */
44 extern int __libc_argc;
45 extern char **__libc_argv;
47 extern char **__environ;
49 /* Undefine the following for debugging. */
50 /* #define SCOPE_DEBUG 1 */
52 static void show_scope (struct link_map *new);
55 /* We must be carefull not to leave us in an inconsistent state. Thus we
56 catch any error and re-raise it after cleaning up. */
68 add_to_global (struct link_map *new)
70 struct link_map **new_global;
71 unsigned int to_add = 0;
74 /* Count the objects we have to put in the global scope. */
75 for (cnt = 0; cnt < new->l_searchlist.r_nlist; ++cnt)
76 if (new->l_searchlist.r_list[cnt]->l_global == 0)
79 /* The symbols of the new objects and its dependencies are to be
80 introduced into the global scope that will be used to resolve
81 references from other dynamically-loaded objects.
83 The global scope is the searchlist in the main link map. We
84 extend this list if necessary. There is one problem though:
85 since this structure was allocated very early (before the libc
86 is loaded) the memory it uses is allocated by the malloc()-stub
87 in the ld.so. When we come here these functions are not used
88 anymore. Instead the malloc() implementation of the libc is
89 used. But this means the block from the main map cannot be used
90 in an realloc() call. Therefore we allocate a completely new
91 array the first time we have to add something to the locale scope. */
93 if (GL(dl_global_scope_alloc) == 0)
95 /* This is the first dynamic object given global scope. */
96 GL(dl_global_scope_alloc) = GL(dl_main_searchlist)->r_nlist + to_add + 8;
97 new_global = (struct link_map **)
98 malloc (GL(dl_global_scope_alloc) * sizeof (struct link_map *));
99 if (new_global == NULL)
101 GL(dl_global_scope_alloc) = 0;
103 _dl_signal_error (ENOMEM, new->l_libname->name, NULL,
104 N_("cannot extend global scope"));
108 /* Copy over the old entries. */
109 memcpy (new_global, GL(dl_main_searchlist)->r_list,
110 (GL(dl_main_searchlist)->r_nlist * sizeof (struct link_map *)));
112 GL(dl_main_searchlist)->r_list = new_global;
114 else if (GL(dl_main_searchlist)->r_nlist + to_add
115 > GL(dl_global_scope_alloc))
117 /* We have to extend the existing array of link maps in the
119 new_global = (struct link_map **)
120 realloc (GL(dl_main_searchlist)->r_list,
121 ((GL(dl_global_scope_alloc) + to_add + 8)
122 * sizeof (struct link_map *)));
123 if (new_global == NULL)
126 GL(dl_global_scope_alloc) += to_add + 8;
127 GL(dl_main_searchlist)->r_list = new_global;
130 /* Now add the new entries. */
131 for (cnt = 0; cnt < new->l_searchlist.r_nlist; ++cnt)
133 struct link_map *map = new->l_searchlist.r_list[cnt];
135 if (map->l_global == 0)
138 GL(dl_main_searchlist)->r_list[GL(dl_main_searchlist)->r_nlist]
140 ++GL(dl_main_searchlist)->r_nlist;
149 dl_open_worker (void *a)
151 struct dl_open_args *args = a;
152 const char *file = args->file;
153 int mode = args->mode;
154 struct link_map *new, *l;
159 /* Maybe we have to expand a DST. */
160 dst = strchr (file, '$');
161 if (__builtin_expect (dst != NULL, 0))
163 const void *caller = args->caller;
164 size_t len = strlen (file);
166 struct link_map *call_map;
169 /* DSTs must not appear in SUID/SGID programs. */
170 if (__libc_enable_secure)
171 /* This is an error. */
172 _dl_signal_error (0, "dlopen", NULL,
173 N_("DST not allowed in SUID/SGID programs"));
175 /* We have to find out from which object the caller is calling. */
177 for (l = GL(dl_loaded); l; l = l->l_next)
178 if (caller >= (const void *) l->l_map_start
179 && caller < (const void *) l->l_map_end)
181 /* There must be exactly one DSO for the range of the virtual
182 memory. Otherwise something is really broken. */
187 if (call_map == NULL)
188 /* In this case we assume this is the main application. */
189 call_map = GL(dl_loaded);
191 /* Determine how much space we need. We have to allocate the
193 required = DL_DST_REQUIRED (call_map, file, len, _dl_dst_count (dst, 0));
195 /* Get space for the new file name. */
196 new_file = (char *) alloca (required + 1);
198 /* Generate the new file name. */
199 _dl_dst_substitute (call_map, file, new_file, 0);
201 /* If the substitution failed don't try to load. */
202 if (*new_file == '\0')
203 _dl_signal_error (0, "dlopen", NULL,
204 N_("empty dynamic string token substitution"));
206 /* Now we have a new file name. */
210 /* Load the named object. */
211 args->map = new = _dl_map_object (NULL, file, 0, lt_loaded, 0, mode);
213 /* If the pointer returned is NULL this means the RTLD_NOLOAD flag is
214 set and the object is not already loaded. */
217 assert (mode & RTLD_NOLOAD);
221 if (__builtin_expect (mode & __RTLD_SPROF, 0))
222 /* This happens only if we load a DSO for 'sprof'. */
225 /* It was already open. */
226 if (new->l_searchlist.r_list != NULL)
228 /* Let the user know about the opencount. */
229 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_FILES, 0))
230 _dl_debug_printf ("opening file=%s; opencount == %u\n\n",
231 new->l_name, new->l_opencount);
233 /* If the user requested the object to be in the global namespace
234 but it is not so far, add it now. */
235 if ((mode & RTLD_GLOBAL) && new->l_global == 0)
236 (void) add_to_global (new);
238 /* Increment just the reference counter of the object. */
244 /* Load that object's dependencies. */
245 _dl_map_object_deps (new, NULL, 0, 0, mode & __RTLD_DLOPEN);
247 /* So far, so good. Now check the versions. */
248 for (i = 0; i < new->l_searchlist.r_nlist; ++i)
249 if (new->l_searchlist.r_list[i]->l_versions == NULL)
250 (void) _dl_check_map_versions (new->l_searchlist.r_list[i], 0, 0);
256 /* Only do lazy relocation if `LD_BIND_NOW' is not set. */
257 lazy = (mode & RTLD_BINDING_MASK) == RTLD_LAZY && GL(dl_lazy);
259 /* Relocate the objects loaded. We do this in reverse order so that copy
260 relocs of earlier objects overwrite the data written by later objects. */
267 if (! l->l_relocated)
270 if (GL(dl_profile) != NULL)
272 /* If this here is the shared object which we want to profile
273 make sure the profile is started. We can find out whether
274 this is necessary or not by observing the `_dl_profile_map'
275 variable. If was NULL but is not NULL afterwars we must
276 start the profiling. */
277 struct link_map *old_profile_map = GL(dl_profile_map);
279 _dl_relocate_object (l, l->l_scope, 1, 1);
281 if (old_profile_map == NULL && GL(dl_profile_map) != NULL)
282 /* We must prepare the profiling. */
283 _dl_start_profile (GL(dl_profile_map), GL(dl_profile_output));
287 _dl_relocate_object (l, l->l_scope, lazy, 0);
295 /* Increment the open count for all dependencies. If the file is
296 not loaded as a dependency here add the search list of the newly
297 loaded object to the scope. */
298 for (i = 0; i < new->l_searchlist.r_nlist; ++i)
299 if (++new->l_searchlist.r_list[i]->l_opencount > 1
300 && new->l_searchlist.r_list[i]->l_type == lt_loaded)
302 struct link_map *imap = new->l_searchlist.r_list[i];
303 struct r_scope_elem **runp = imap->l_scope;
306 while (*runp != NULL)
308 /* This can happen if imap was just loaded, but during
309 relocation had l_opencount bumped because of relocation
310 dependency. Avoid duplicates in l_scope. */
311 if (__builtin_expect (*runp == &new->l_searchlist, 0))
319 /* Avoid duplicates. */
322 if (__builtin_expect (cnt + 1 >= imap->l_scope_max, 0))
324 /* The 'r_scope' array is too small. Allocate a new one
326 struct r_scope_elem **newp;
327 size_t new_size = imap->l_scope_max * 2;
329 if (imap->l_scope == imap->l_scope_mem)
331 newp = (struct r_scope_elem **)
332 malloc (new_size * sizeof (struct r_scope_elem *));
334 _dl_signal_error (ENOMEM, "dlopen", NULL,
335 N_("cannot create scope list"));
336 imap->l_scope = memcpy (newp, imap->l_scope,
337 cnt * sizeof (imap->l_scope[0]));
341 newp = (struct r_scope_elem **)
342 realloc (imap->l_scope,
343 new_size * sizeof (struct r_scope_elem *));
345 _dl_signal_error (ENOMEM, "dlopen", NULL,
346 N_("cannot create scope list"));
347 imap->l_scope = newp;
350 imap->l_scope_max = new_size;
353 imap->l_scope[cnt++] = &new->l_searchlist;
354 imap->l_scope[cnt] = NULL;
357 else if (new->l_searchlist.r_list[i]->l_opencount == 1
358 /* Only if the module defines thread local data. */
359 && __builtin_expect (new->l_searchlist.r_list[i]->l_tls_blocksize
362 /* Now that we know the object is loaded successfully add
363 modules containing TLS data to the dtv info table. We
364 might have to increase its size. */
365 struct dtv_slotinfo_list *listp;
366 struct dtv_slotinfo_list *prevp;
367 size_t idx = new->l_searchlist.r_list[i]->l_tls_modid;
369 assert (new->l_searchlist.r_list[i]->l_type == lt_loaded);
371 /* Find the place in the stv slotinfo list. */
372 listp = GL(dl_tls_dtv_slotinfo_list);
373 prevp = NULL; /* Needed to shut up gcc. */
376 /* Does it fit in the array of this list element? */
377 if (idx <= listp->len)
381 while ((listp = listp->next) != NULL);
385 /* When we come here it means we have to add a new element
386 to the slotinfo list. And the new module must be in
390 listp = prevp->next = (struct dtv_slotinfo_list *)
391 malloc (sizeof (struct dtv_slotinfo_list)
392 + TLS_SLOTINFO_SURPLUS * sizeof (struct dtv_slotinfo));
395 /* We ran out of memory. We will simply fail this
396 call but don't undo anything we did so far. The
397 application will crash or be terminated anyway very
400 /* We have to do this since some entries in the dtv
401 slotinfo array might already point to this
403 ++GL(dl_tls_generation);
405 _dl_signal_error (ENOMEM, "dlopen", NULL,
406 N_("cannot create TLS data structures"));
409 listp->len = TLS_SLOTINFO_SURPLUS;
411 memset (listp->slotinfo, '\0',
412 TLS_SLOTINFO_SURPLUS * sizeof (struct dtv_slotinfo));
415 /* Add the information into the slotinfo data structure. */
416 listp->slotinfo[idx].map = new->l_searchlist.r_list[i];
417 listp->slotinfo[idx].gen = GL(dl_tls_generation) + 1;
420 /* Bump the generation number. */
421 ++GL(dl_tls_generation);
424 /* Run the initializer functions of new objects. */
425 _dl_init (new, __libc_argc, __libc_argv, __environ);
427 /* Now we can make the new map available in the global scope. */
428 if (mode & RTLD_GLOBAL)
429 /* Move the object in the global namespace. */
430 if (add_to_global (new) != 0)
434 /* Mark the object as not deletable if the RTLD_NODELETE flags was
436 if (__builtin_expect (mode & RTLD_NODELETE, 0))
437 new->l_flags_1 |= DF_1_NODELETE;
440 /* We must be the static _dl_open in libc.a. A static program that
441 has loaded a dynamic object now has competition. */
442 __libc_multiple_libcs = 1;
445 /* Let the user know about the opencount. */
446 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_FILES, 0))
447 _dl_debug_printf ("opening file=%s; opencount == %u\n\n",
448 new->l_name, new->l_opencount);
454 _dl_open (const char *file, int mode, const void *caller)
456 struct dl_open_args args;
458 const char *errstring;
461 if ((mode & RTLD_BINDING_MASK) == 0)
462 /* One of the flags must be set. */
463 _dl_signal_error (EINVAL, file, NULL, N_("invalid mode for dlopen()"));
465 /* Make sure we are alone. */
466 __libc_lock_lock_recursive (GL(dl_load_lock));
470 args.caller = caller;
472 errcode = _dl_catch_error (&objname, &errstring, dl_open_worker, &args);
475 /* We must munmap() the cache file. */
479 /* Release the lock. */
480 __libc_lock_unlock_recursive (GL(dl_load_lock));
482 if (__builtin_expect (errstring != NULL, 0))
484 /* Some error occurred during loading. */
485 char *local_errstring;
486 size_t len_errstring;
488 /* Remove the object from memory. It may be in an inconsistent
489 state if relocation failed, for example. */
494 /* Increment open counters for all objects since this
495 sometimes has not happened yet. */
496 if (args.map->l_searchlist.r_list[0]->l_opencount == 0)
497 for (i = 0; i < args.map->l_searchlist.r_nlist; ++i)
498 ++args.map->l_searchlist.r_list[i]->l_opencount;
501 /* Maybe some of the modules which were loaded uses TLS.
502 Since it will be removed in the folowing _dl_close call
503 we have to mark the dtv array as having gaps to fill
504 the holes. This is a pessimistic assumption which won't
506 GL(dl_tls_dtv_gaps) = true;
509 _dl_close (args.map);
512 /* Make a local copy of the error string so that we can release the
513 memory allocated for it. */
514 len_errstring = strlen (errstring) + 1;
515 if (objname == errstring + len_errstring)
517 size_t total_len = len_errstring + strlen (objname) + 1;
518 local_errstring = alloca (total_len);
519 memcpy (local_errstring, errstring, total_len);
520 objname = local_errstring + len_errstring;
524 local_errstring = alloca (len_errstring);
525 memcpy (local_errstring, errstring, len_errstring);
528 if (errstring != _dl_out_of_memory)
529 free ((char *) errstring);
531 /* Reraise the error. */
532 _dl_signal_error (errcode, objname, NULL, local_errstring);
536 DL_STATIC_INIT (args.map);
547 show_scope (struct link_map *new)
551 for (scope_cnt = 0; new->l_scope[scope_cnt] != NULL; ++scope_cnt)
556 numbuf[0] = '0' + scope_cnt;
558 _dl_printf ("scope %s:", numbuf);
560 for (cnt = 0; cnt < new->l_scope[scope_cnt]->r_nlist; ++cnt)
561 if (*new->l_scope[scope_cnt]->r_list[cnt]->l_name)
562 _dl_printf (" %s", new->l_scope[scope_cnt]->r_list[cnt]->l_name);
564 _dl_printf (" <main>");