(main): Call setlocale and textdomain.
[glibc/history.git] / elf / dl-open.c
blobc99752112661e08ab8b4c0d7612c4cf4961c20a3
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
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <dlfcn.h>
22 #include <errno.h>
23 #include <libintl.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/mman.h> /* Check whether MAP_COPY is defined. */
29 #include <sys/param.h>
30 #include <bits/libc-lock.h>
31 #include <ldsodefs.h>
32 #include <bp-sym.h>
33 #include <caller.h>
34 #include <sysdep-cancel.h>
36 #include <dl-dst.h>
39 extern ElfW(Addr) _dl_sysdep_start (void **start_argptr,
40 void (*dl_main) (const ElfW(Phdr) *phdr,
41 ElfW(Word) phnum,
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 */
49 #ifdef SCOPE_DEBUG
50 static void show_scope (struct link_map *new);
51 #endif
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. */
56 struct dl_open_args
58 const char *file;
59 int mode;
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;
64 struct link_map *map;
65 /* Namespace ID. */
66 Lmid_t nsid;
67 /* Original parameters to the program and the current environment. */
68 int argc;
69 char **argv;
70 char **env;
74 static int
75 add_to_global (struct link_map *new)
77 struct link_map **new_global;
78 unsigned int to_add = 0;
79 unsigned int cnt;
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)
84 ++to_add;
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;
111 nomem:
112 _dl_signal_error (ENOMEM, new->l_libname->name, NULL,
113 N_("cannot extend global scope"));
114 return 1;
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
128 main map. */
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)
134 goto nomem;
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)
147 map->l_global = 1;
148 GL(dl_ns)[new->l_ns]._ns_main_searchlist->r_list[GL(dl_ns)[new->l_ns]._ns_main_searchlist->r_nlist]
149 = map;
150 ++GL(dl_ns)[new->l_ns]._ns_main_searchlist->r_nlist;
154 return 0;
158 static void
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;
165 int lazy;
166 unsigned int i;
167 bool any_tls = false;
168 struct link_map *call_map = NULL;
170 /* Check whether _dl_open() has been called from a valid DSO. */
171 if (__check_caller (args->caller_dl_open,
172 allow_libc|allow_libdl|allow_ldso) != 0)
173 _dl_signal_error (0, "dlopen", NULL, N_("invalid caller"));
175 /* Determine the caller's map if necessary. This is needed in case
176 we have a DST, when we don't know the namespace ID we have to put
177 the new object in, or when the file name has no path in which
178 case we need to look along the RUNPATH/RPATH of the caller. */
179 const char *dst = strchr (file, '$');
180 if (dst != NULL || args->nsid == __LM_ID_CALLER
181 || strchr (file, '/') == NULL)
183 const void *caller_dlopen = args->caller_dlopen;
185 /* We have to find out from which object the caller is calling.
186 By default we assume this is the main application. */
187 call_map = GL(dl_ns)[LM_ID_BASE]._ns_loaded;
189 for (Lmid_t ns = 0; ns < DL_NNS; ++ns)
190 for (l = GL(dl_ns)[ns]._ns_loaded; l != NULL; l = l->l_next)
191 if (caller_dlopen >= (const void *) l->l_map_start
192 && caller_dlopen < (const void *) l->l_map_end)
194 /* There must be exactly one DSO for the range of the virtual
195 memory. Otherwise something is really broken. */
196 assert (ns == l->l_ns);
197 call_map = l;
198 goto found_caller;
201 found_caller:
202 if (args->nsid == __LM_ID_CALLER)
204 #ifndef SHARED
205 /* In statically linked apps there might be no loaded object. */
206 if (call_map == NULL)
207 args->nsid = LM_ID_BASE;
208 else
209 #endif
210 args->nsid = call_map->l_ns;
214 assert (_dl_debug_initialize (0, args->nsid)->r_state == RT_CONSISTENT);
216 /* Maybe we have to expand a DST. */
217 if (__builtin_expect (dst != NULL, 0))
219 size_t len = strlen (file);
220 size_t required;
221 char *new_file;
223 /* Determine how much space we need. We have to allocate the
224 memory locally. */
225 required = DL_DST_REQUIRED (call_map, file, len, _dl_dst_count (dst, 0));
227 /* Get space for the new file name. */
228 new_file = (char *) alloca (required + 1);
230 /* Generate the new file name. */
231 _dl_dst_substitute (call_map, file, new_file, 0);
233 /* If the substitution failed don't try to load. */
234 if (*new_file == '\0')
235 _dl_signal_error (0, "dlopen", NULL,
236 N_("empty dynamic string token substitution"));
238 /* Now we have a new file name. */
239 file = new_file;
241 /* It does not matter whether call_map is set even if we
242 computed it only because of the DST. Since the path contains
243 a slash the value is not used. See dl-load.c. */
246 /* Load the named object. */
247 args->map = new = _dl_map_object (call_map, file, 0, lt_loaded, 0,
248 mode | __RTLD_CALLMAP, args->nsid);
250 /* If the pointer returned is NULL this means the RTLD_NOLOAD flag is
251 set and the object is not already loaded. */
252 if (new == NULL)
254 assert (mode & RTLD_NOLOAD);
255 return;
258 if (__builtin_expect (mode & __RTLD_SPROF, 0))
259 /* This happens only if we load a DSO for 'sprof'. */
260 return;
262 /* This object is directly loaded. */
263 ++new->l_direct_opencount;
265 /* It was already open. */
266 if (__builtin_expect (new->l_searchlist.r_list != NULL, 0))
268 /* Let the user know about the opencount. */
269 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
270 _dl_debug_printf ("opening file=%s [%lu]; direct_opencount=%u\n\n",
271 new->l_name, new->l_ns, new->l_direct_opencount);
273 /* If the user requested the object to be in the global namespace
274 but it is not so far, add it now. */
275 if ((mode & RTLD_GLOBAL) && new->l_global == 0)
276 (void) add_to_global (new);
278 assert (_dl_debug_initialize (0, args->nsid)->r_state == RT_CONSISTENT);
280 return;
283 /* Load that object's dependencies. */
284 _dl_map_object_deps (new, NULL, 0, 0,
285 mode & (__RTLD_DLOPEN | RTLD_DEEPBIND | __RTLD_AUDIT));
287 /* So far, so good. Now check the versions. */
288 for (i = 0; i < new->l_searchlist.r_nlist; ++i)
289 if (new->l_searchlist.r_list[i]->l_real->l_versions == NULL)
290 (void) _dl_check_map_versions (new->l_searchlist.r_list[i]->l_real,
291 0, 0);
293 #ifdef SCOPE_DEBUG
294 show_scope (new);
295 #endif
297 #ifdef SHARED
298 /* Auditing checkpoint: we have added all objects. */
299 if (__builtin_expect (GLRO(dl_naudit) > 0, 0))
301 struct link_map *head = GL(dl_ns)[new->l_ns]._ns_loaded;
302 /* Do not call the functions for any auditing object. */
303 if (head->l_auditing == 0)
305 struct audit_ifaces *afct = GLRO(dl_audit);
306 for (unsigned int cnt = 0; cnt < GLRO(dl_naudit); ++cnt)
308 if (afct->activity != NULL)
309 afct->activity (&head->l_audit[cnt].cookie, LA_ACT_CONSISTENT);
311 afct = afct->next;
315 #endif
317 /* Notify the debugger all new objects are now ready to go. */
318 struct r_debug *r = _dl_debug_initialize (0, args->nsid);
319 r->r_state = RT_CONSISTENT;
320 _dl_debug_state ();
322 /* Only do lazy relocation if `LD_BIND_NOW' is not set. */
323 lazy = (mode & RTLD_BINDING_MASK) == RTLD_LAZY && GLRO(dl_lazy);
325 /* Relocate the objects loaded. We do this in reverse order so that copy
326 relocs of earlier objects overwrite the data written by later objects. */
328 l = new;
329 while (l->l_next)
330 l = l->l_next;
331 while (1)
333 if (! l->l_real->l_relocated)
335 #ifdef SHARED
336 if (__builtin_expect (GLRO(dl_profile) != NULL, 0))
338 /* If this here is the shared object which we want to profile
339 make sure the profile is started. We can find out whether
340 this is necessary or not by observing the `_dl_profile_map'
341 variable. If was NULL but is not NULL afterwars we must
342 start the profiling. */
343 struct link_map *old_profile_map = GL(dl_profile_map);
345 _dl_relocate_object (l, l->l_scope, 1, 1);
347 if (old_profile_map == NULL && GL(dl_profile_map) != NULL)
349 /* We must prepare the profiling. */
350 _dl_start_profile ();
352 /* Prevent unloading the object. */
353 GL(dl_profile_map)->l_flags_1 |= DF_1_NODELETE;
356 else
357 #endif
358 _dl_relocate_object (l, l->l_scope, lazy, 0);
361 if (l == new)
362 break;
363 l = l->l_prev;
366 /* If the file is not loaded now as a dependency, add the search
367 list of the newly loaded object to the scope. */
368 for (i = 0; i < new->l_searchlist.r_nlist; ++i)
370 struct link_map *imap = new->l_searchlist.r_list[i];
372 /* If the initializer has been called already, the object has
373 not been loaded here and now. */
374 if (imap->l_init_called && imap->l_type == lt_loaded)
376 struct r_scope_elem **runp = imap->l_scope;
377 size_t cnt = 0;
379 while (*runp != NULL)
381 if (*runp == &new->l_searchlist)
382 break;
383 ++cnt;
384 ++runp;
387 if (*runp != NULL)
388 /* Avoid duplicates. */
389 continue;
391 if (__builtin_expect (cnt + 1 >= imap->l_scope_max, 0))
393 /* The 'r_scope' array is too small. Allocate a new one
394 dynamically. */
395 size_t new_size;
396 struct r_scope_elem **newp;
398 #define SCOPE_ELEMS(imap) \
399 (sizeof (imap->l_scope_mem) / sizeof (imap->l_scope_mem[0]))
401 if (imap->l_scope != imap->l_scope_mem
402 && imap->l_scope_max < SCOPE_ELEMS (imap))
404 new_size = SCOPE_ELEMS (imap);
405 newp = imap->l_scope_mem;
407 else
409 new_size = imap->l_scope_max * 2;
410 newp = (struct r_scope_elem **)
411 malloc (new_size * sizeof (struct r_scope_elem *));
412 if (newp == NULL)
413 _dl_signal_error (ENOMEM, "dlopen", NULL,
414 N_("cannot create scope list"));
417 memcpy (newp, imap->l_scope, cnt * sizeof (imap->l_scope[0]));
418 struct r_scope_elem **old = imap->l_scope;
420 if (RTLD_SINGLE_THREAD_P)
421 imap->l_scope = newp;
422 else
424 __rtld_mrlock_change (imap->l_scope_lock);
425 imap->l_scope = newp;
426 __rtld_mrlock_done (imap->l_scope_lock);
429 if (old != imap->l_scope_mem)
430 free (old);
432 imap->l_scope_max = new_size;
435 /* First terminate the extended list. Otherwise a thread
436 might use the new last element and then use the garbage
437 at offset IDX+1. */
438 imap->l_scope[cnt + 1] = NULL;
439 atomic_write_barrier ();
440 imap->l_scope[cnt] = &new->l_searchlist;
442 /* Only add TLS memory if this object is loaded now and
443 therefore is not yet initialized. */
444 else if (! imap->l_init_called
445 /* Only if the module defines thread local data. */
446 && __builtin_expect (imap->l_tls_blocksize > 0, 0))
448 /* Now that we know the object is loaded successfully add
449 modules containing TLS data to the slot info table. We
450 might have to increase its size. */
451 _dl_add_to_slotinfo (imap);
453 if (imap->l_need_tls_init)
455 imap->l_need_tls_init = 0;
456 #ifdef SHARED
457 /* Update the slot information data for at least the
458 generation of the DSO we are allocating data for. */
459 _dl_update_slotinfo (imap->l_tls_modid);
460 #endif
462 GL(dl_init_static_tls) (imap);
463 assert (imap->l_need_tls_init == 0);
466 /* We have to bump the generation counter. */
467 any_tls = true;
471 /* Bump the generation number if necessary. */
472 if (any_tls && __builtin_expect (++GL(dl_tls_generation) == 0, 0))
473 _dl_fatal_printf (N_("\
474 TLS generation counter wrapped! Please report this."));
476 /* Run the initializer functions of new objects. */
477 _dl_init (new, args->argc, args->argv, args->env);
479 /* Now we can make the new map available in the global scope. */
480 if (mode & RTLD_GLOBAL)
481 /* Move the object in the global namespace. */
482 if (add_to_global (new) != 0)
483 /* It failed. */
484 return;
486 /* Mark the object as not deletable if the RTLD_NODELETE flags was
487 passed. */
488 if (__builtin_expect (mode & RTLD_NODELETE, 0))
489 new->l_flags_1 |= DF_1_NODELETE;
491 #ifndef SHARED
492 /* We must be the static _dl_open in libc.a. A static program that
493 has loaded a dynamic object now has competition. */
494 __libc_multiple_libcs = 1;
495 #endif
497 /* Let the user know about the opencount. */
498 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
499 _dl_debug_printf ("opening file=%s [%lu]; direct_opencount=%u\n\n",
500 new->l_name, new->l_ns, new->l_direct_opencount);
504 void *
505 _dl_open (const char *file, int mode, const void *caller_dlopen, Lmid_t nsid,
506 int argc, char *argv[], char *env[])
508 if ((mode & RTLD_BINDING_MASK) == 0)
509 /* One of the flags must be set. */
510 _dl_signal_error (EINVAL, file, NULL, N_("invalid mode for dlopen()"));
512 /* Make sure we are alone. */
513 __rtld_lock_lock_recursive (GL(dl_load_lock));
515 if (nsid == LM_ID_NEWLM)
517 /* Find a new namespace. */
518 for (nsid = 1; nsid < DL_NNS; ++nsid)
519 if (GL(dl_ns)[nsid]._ns_loaded == NULL)
520 break;
522 if (nsid == DL_NNS)
524 /* No more namespace available. */
525 __rtld_lock_unlock_recursive (GL(dl_load_lock));
527 _dl_signal_error (EINVAL, file, NULL, N_("\
528 no more namespaces available for dlmopen()"));
531 _dl_debug_initialize (0, nsid)->r_state = RT_CONSISTENT;
533 /* Never allow loading a DSO in a namespace which is empty. Such
534 direct placements is only causing problems. Also don't allow
535 loading into a namespace used for auditing. */
536 else if (nsid != LM_ID_BASE && nsid != __LM_ID_CALLER
537 && (GL(dl_ns)[nsid]._ns_nloaded == 0
538 || GL(dl_ns)[nsid]._ns_loaded->l_auditing))
539 _dl_signal_error (EINVAL, file, NULL,
540 N_("invalid target namespace in dlmopen()"));
542 struct dl_open_args args;
543 args.file = file;
544 args.mode = mode;
545 args.caller_dlopen = caller_dlopen;
546 args.caller_dl_open = RETURN_ADDRESS (0);
547 args.map = NULL;
548 args.nsid = nsid;
549 args.argc = argc;
550 args.argv = argv;
551 args.env = env;
553 const char *objname;
554 const char *errstring;
555 bool malloced;
556 int errcode = _dl_catch_error (&objname, &errstring, &malloced,
557 dl_open_worker, &args);
559 #ifndef MAP_COPY
560 /* We must munmap() the cache file. */
561 _dl_unload_cache ();
562 #endif
564 /* See if an error occurred during loading. */
565 if (__builtin_expect (errstring != NULL, 0))
567 /* Remove the object from memory. It may be in an inconsistent
568 state if relocation failed, for example. */
569 if (args.map)
571 /* Maybe some of the modules which were loaded use TLS.
572 Since it will be removed in the following _dl_close call
573 we have to mark the dtv array as having gaps to fill the
574 holes. This is a pessimistic assumption which won't hurt
575 if not true. There is no need to do this when we are
576 loading the auditing DSOs since TLS has not yet been set
577 up. */
578 if ((mode & __RTLD_AUDIT) == 0)
579 GL(dl_tls_dtv_gaps) = true;
581 _dl_close_worker (args.map);
584 assert (_dl_debug_initialize (0, args.nsid)->r_state == RT_CONSISTENT);
586 /* Release the lock. */
587 __rtld_lock_unlock_recursive (GL(dl_load_lock));
589 /* Make a local copy of the error string so that we can release the
590 memory allocated for it. */
591 size_t len_errstring = strlen (errstring) + 1;
592 char *local_errstring;
593 if (objname == errstring + len_errstring)
595 size_t total_len = len_errstring + strlen (objname) + 1;
596 local_errstring = alloca (total_len);
597 memcpy (local_errstring, errstring, total_len);
598 objname = local_errstring + len_errstring;
600 else
602 local_errstring = alloca (len_errstring);
603 memcpy (local_errstring, errstring, len_errstring);
606 if (malloced)
607 free ((char *) errstring);
609 /* Reraise the error. */
610 _dl_signal_error (errcode, objname, NULL, local_errstring);
613 assert (_dl_debug_initialize (0, args.nsid)->r_state == RT_CONSISTENT);
615 /* Release the lock. */
616 __rtld_lock_unlock_recursive (GL(dl_load_lock));
618 #ifndef SHARED
619 DL_STATIC_INIT (args.map);
620 #endif
622 return args.map;
626 #ifdef SCOPE_DEBUG
627 #include <unistd.h>
629 static void
630 show_scope (struct link_map *new)
632 int scope_cnt;
634 for (scope_cnt = 0; new->l_scope[scope_cnt] != NULL; ++scope_cnt)
636 char numbuf[2];
637 unsigned int cnt;
639 numbuf[0] = '0' + scope_cnt;
640 numbuf[1] = '\0';
641 _dl_printf ("scope %s:", numbuf);
643 for (cnt = 0; cnt < new->l_scope[scope_cnt]->r_nlist; ++cnt)
644 if (*new->l_scope[scope_cnt]->r_list[cnt]->l_name)
645 _dl_printf (" %s", new->l_scope[scope_cnt]->r_list[cnt]->l_name);
646 else
647 _dl_printf (" <main>");
649 _dl_printf ("\n");
652 #endif