Use INTERNAL_SYSCALL.
[glibc.git] / elf / dl-open.c
blob65102fabce9ed3d38603f5efcff592cb0edf2682
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
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>
34 #include <dl-dst.h>
37 #ifdef SHARED
38 /* Giving this initialized value preallocates some surplus bytes in the
39 static TLS area, see __libc_setup_tls (libc-tls.c). */
40 size_t _dl_tls_static_size = 576;
41 #endif
43 extern ElfW(Addr) _dl_sysdep_start (void **start_argptr,
44 void (*dl_main) (const ElfW(Phdr) *phdr,
45 ElfW(Word) phnum,
46 ElfW(Addr) *user_entry));
47 weak_extern (BP_SYM (_dl_sysdep_start))
49 extern int __libc_multiple_libcs; /* Defined in init-first.c. */
51 extern int __libc_argc attribute_hidden;
52 extern char **__libc_argv attribute_hidden;
54 extern char **__environ;
56 /* Undefine the following for debugging. */
57 /* #define SCOPE_DEBUG 1 */
58 #ifdef SCOPE_DEBUG
59 static void show_scope (struct link_map *new);
60 #endif
62 /* We must be carefull not to leave us in an inconsistent state. Thus we
63 catch any error and re-raise it after cleaning up. */
65 struct dl_open_args
67 const char *file;
68 int mode;
69 const void *caller;
70 struct link_map *map;
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_global_scope_alloc) == 0)
102 /* This is the first dynamic object given global scope. */
103 GL(dl_global_scope_alloc) = GL(dl_main_searchlist)->r_nlist + to_add + 8;
104 new_global = (struct link_map **)
105 malloc (GL(dl_global_scope_alloc) * sizeof (struct link_map *));
106 if (new_global == NULL)
108 GL(dl_global_scope_alloc) = 0;
109 nomem:
110 _dl_signal_error (ENOMEM, new->l_libname->name, NULL,
111 N_("cannot extend global scope"));
112 return 1;
115 /* Copy over the old entries. */
116 memcpy (new_global, GL(dl_main_searchlist)->r_list,
117 (GL(dl_main_searchlist)->r_nlist * sizeof (struct link_map *)));
119 GL(dl_main_searchlist)->r_list = new_global;
121 else if (GL(dl_main_searchlist)->r_nlist + to_add
122 > GL(dl_global_scope_alloc))
124 /* We have to extend the existing array of link maps in the
125 main map. */
126 new_global = (struct link_map **)
127 realloc (GL(dl_main_searchlist)->r_list,
128 ((GL(dl_global_scope_alloc) + to_add + 8)
129 * sizeof (struct link_map *)));
130 if (new_global == NULL)
131 goto nomem;
133 GL(dl_global_scope_alloc) += to_add + 8;
134 GL(dl_main_searchlist)->r_list = new_global;
137 /* Now add the new entries. */
138 for (cnt = 0; cnt < new->l_searchlist.r_nlist; ++cnt)
140 struct link_map *map = new->l_searchlist.r_list[cnt];
142 if (map->l_global == 0)
144 map->l_global = 1;
145 GL(dl_main_searchlist)->r_list[GL(dl_main_searchlist)->r_nlist]
146 = map;
147 ++GL(dl_main_searchlist)->r_nlist;
151 return 0;
155 static void
156 dl_open_worker (void *a)
158 struct dl_open_args *args = a;
159 const char *file = args->file;
160 int mode = args->mode;
161 struct link_map *new, *l;
162 const char *dst;
163 int lazy;
164 unsigned int i;
165 #ifdef USE_TLS
166 bool any_tls;
167 #endif
169 /* Maybe we have to expand a DST. */
170 dst = strchr (file, '$');
171 if (__builtin_expect (dst != NULL, 0))
173 const void *caller = args->caller;
174 size_t len = strlen (file);
175 size_t required;
176 struct link_map *call_map;
177 char *new_file;
179 /* DSTs must not appear in SUID/SGID programs. */
180 if (__libc_enable_secure)
181 /* This is an error. */
182 _dl_signal_error (0, "dlopen", NULL,
183 N_("DST not allowed in SUID/SGID programs"));
185 /* We have to find out from which object the caller is calling. */
186 call_map = NULL;
187 for (l = GL(dl_loaded); l; l = l->l_next)
188 if (caller >= (const void *) l->l_map_start
189 && caller < (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. */
193 call_map = l;
194 break;
197 if (call_map == NULL)
198 /* In this case we assume this is the main application. */
199 call_map = GL(dl_loaded);
201 /* Determine how much space we need. We have to allocate the
202 memory locally. */
203 required = DL_DST_REQUIRED (call_map, file, len, _dl_dst_count (dst, 0));
205 /* Get space for the new file name. */
206 new_file = (char *) alloca (required + 1);
208 /* Generate the new file name. */
209 _dl_dst_substitute (call_map, file, new_file, 0);
211 /* If the substitution failed don't try to load. */
212 if (*new_file == '\0')
213 _dl_signal_error (0, "dlopen", NULL,
214 N_("empty dynamic string token substitution"));
216 /* Now we have a new file name. */
217 file = new_file;
220 /* Load the named object. */
221 args->map = new = _dl_map_object (NULL, file, 0, lt_loaded, 0, mode);
223 /* If the pointer returned is NULL this means the RTLD_NOLOAD flag is
224 set and the object is not already loaded. */
225 if (new == NULL)
227 assert (mode & RTLD_NOLOAD);
228 return;
231 if (__builtin_expect (mode & __RTLD_SPROF, 0))
232 /* This happens only if we load a DSO for 'sprof'. */
233 return;
235 /* It was already open. */
236 if (new->l_searchlist.r_list != NULL)
238 /* Let the user know about the opencount. */
239 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_FILES, 0))
240 _dl_debug_printf ("opening file=%s; opencount == %u\n\n",
241 new->l_name, new->l_opencount);
243 /* If the user requested the object to be in the global namespace
244 but it is not so far, add it now. */
245 if ((mode & RTLD_GLOBAL) && new->l_global == 0)
246 (void) add_to_global (new);
248 /* Increment just the reference counter of the object. */
249 ++new->l_opencount;
251 return;
254 /* Load that object's dependencies. */
255 _dl_map_object_deps (new, NULL, 0, 0, mode & __RTLD_DLOPEN);
257 /* So far, so good. Now check the versions. */
258 for (i = 0; i < new->l_searchlist.r_nlist; ++i)
259 if (new->l_searchlist.r_list[i]->l_versions == NULL)
260 (void) _dl_check_map_versions (new->l_searchlist.r_list[i], 0, 0);
262 #ifdef SCOPE_DEBUG
263 show_scope (new);
264 #endif
266 /* Only do lazy relocation if `LD_BIND_NOW' is not set. */
267 lazy = (mode & RTLD_BINDING_MASK) == RTLD_LAZY && GL(dl_lazy);
269 /* Relocate the objects loaded. We do this in reverse order so that copy
270 relocs of earlier objects overwrite the data written by later objects. */
272 l = new;
273 while (l->l_next)
274 l = l->l_next;
275 while (1)
277 if (! l->l_relocated)
279 #ifdef SHARED
280 if (GL(dl_profile) != NULL)
282 /* If this here is the shared object which we want to profile
283 make sure the profile is started. We can find out whether
284 this is necessary or not by observing the `_dl_profile_map'
285 variable. If was NULL but is not NULL afterwars we must
286 start the profiling. */
287 struct link_map *old_profile_map = GL(dl_profile_map);
289 _dl_relocate_object (l, l->l_scope, 1, 1);
291 if (old_profile_map == NULL && GL(dl_profile_map) != NULL)
292 /* We must prepare the profiling. */
293 _dl_start_profile (GL(dl_profile_map), GL(dl_profile_output));
295 else
296 #endif
297 _dl_relocate_object (l, l->l_scope, lazy, 0);
300 if (l == new)
301 break;
302 l = l->l_prev;
305 #ifdef USE_TLS
306 /* We normally don't bump the TLS generation counter. There must be
307 actually a need to do this. */
308 any_tls = false;
309 #endif
311 /* Increment the open count for all dependencies. If the file is
312 not loaded as a dependency here add the search list of the newly
313 loaded object to the scope. */
314 for (i = 0; i < new->l_searchlist.r_nlist; ++i)
315 if (++new->l_searchlist.r_list[i]->l_opencount > 1
316 && new->l_searchlist.r_list[i]->l_type == lt_loaded)
318 struct link_map *imap = new->l_searchlist.r_list[i];
319 struct r_scope_elem **runp = imap->l_scope;
320 size_t cnt = 0;
322 while (*runp != NULL)
324 /* This can happen if imap was just loaded, but during
325 relocation had l_opencount bumped because of relocation
326 dependency. Avoid duplicates in l_scope. */
327 if (__builtin_expect (*runp == &new->l_searchlist, 0))
328 break;
330 ++cnt;
331 ++runp;
334 if (*runp != NULL)
335 /* Avoid duplicates. */
336 continue;
338 if (__builtin_expect (cnt + 1 >= imap->l_scope_max, 0))
340 /* The 'r_scope' array is too small. Allocate a new one
341 dynamically. */
342 struct r_scope_elem **newp;
343 size_t new_size = imap->l_scope_max * 2;
345 if (imap->l_scope == imap->l_scope_mem)
347 newp = (struct r_scope_elem **)
348 malloc (new_size * sizeof (struct r_scope_elem *));
349 if (newp == NULL)
350 _dl_signal_error (ENOMEM, "dlopen", NULL,
351 N_("cannot create scope list"));
352 imap->l_scope = memcpy (newp, imap->l_scope,
353 cnt * sizeof (imap->l_scope[0]));
355 else
357 newp = (struct r_scope_elem **)
358 realloc (imap->l_scope,
359 new_size * sizeof (struct r_scope_elem *));
360 if (newp == NULL)
361 _dl_signal_error (ENOMEM, "dlopen", NULL,
362 N_("cannot create scope list"));
363 imap->l_scope = newp;
366 imap->l_scope_max = new_size;
369 imap->l_scope[cnt++] = &new->l_searchlist;
370 imap->l_scope[cnt] = NULL;
372 #if USE_TLS
373 else if (new->l_searchlist.r_list[i]->l_opencount == 1
374 /* Only if the module defines thread local data. */
375 && __builtin_expect (new->l_searchlist.r_list[i]->l_tls_blocksize
376 > 0, 0))
378 /* Now that we know the object is loaded successfully add
379 modules containing TLS data to the dtv info table. We
380 might have to increase its size. */
381 struct dtv_slotinfo_list *listp;
382 struct dtv_slotinfo_list *prevp;
383 size_t idx = new->l_searchlist.r_list[i]->l_tls_modid;
385 assert (new->l_searchlist.r_list[i]->l_type == lt_loaded);
387 /* Find the place in the dtv slotinfo list. */
388 listp = GL(dl_tls_dtv_slotinfo_list);
389 prevp = NULL; /* Needed to shut up gcc. */
392 /* Does it fit in the array of this list element? */
393 if (idx < listp->len)
394 break;
395 idx -= listp->len;
396 prevp = listp;
397 listp = listp->next;
399 while (listp != NULL);
401 if (listp == NULL)
403 /* When we come here it means we have to add a new element
404 to the slotinfo list. And the new module must be in
405 the first slot. */
406 assert (idx == 0);
408 listp = prevp->next = (struct dtv_slotinfo_list *)
409 malloc (sizeof (struct dtv_slotinfo_list)
410 + TLS_SLOTINFO_SURPLUS * sizeof (struct dtv_slotinfo));
411 if (listp == NULL)
413 /* We ran out of memory. We will simply fail this
414 call but don't undo anything we did so far. The
415 application will crash or be terminated anyway very
416 soon. */
418 /* We have to do this since some entries in the dtv
419 slotinfo array might already point to this
420 generation. */
421 ++GL(dl_tls_generation);
423 _dl_signal_error (ENOMEM, "dlopen", NULL,
424 N_("cannot create TLS data structures"));
427 listp->len = TLS_SLOTINFO_SURPLUS;
428 listp->next = NULL;
429 memset (listp->slotinfo, '\0',
430 TLS_SLOTINFO_SURPLUS * sizeof (struct dtv_slotinfo));
433 /* Add the information into the slotinfo data structure. */
434 listp->slotinfo[idx].map = new->l_searchlist.r_list[i];
435 listp->slotinfo[idx].gen = GL(dl_tls_generation) + 1;
437 /* We have to bump the generation counter. */
438 any_tls = true;
441 /* Bump the generation number if necessary. */
442 if (any_tls)
443 if (__builtin_expect (++GL(dl_tls_generation) == 0, 0))
444 __libc_fatal (_("TLS generation counter wrapped! Please send report with the 'glibcbug' script."));
445 #endif
447 /* Run the initializer functions of new objects. */
448 _dl_init (new, __libc_argc, __libc_argv, __environ);
450 /* Now we can make the new map available in the global scope. */
451 if (mode & RTLD_GLOBAL)
452 /* Move the object in the global namespace. */
453 if (add_to_global (new) != 0)
454 /* It failed. */
455 return;
457 /* Mark the object as not deletable if the RTLD_NODELETE flags was
458 passed. */
459 if (__builtin_expect (mode & RTLD_NODELETE, 0))
460 new->l_flags_1 |= DF_1_NODELETE;
462 #ifndef SHARED
463 /* We must be the static _dl_open in libc.a. A static program that
464 has loaded a dynamic object now has competition. */
465 __libc_multiple_libcs = 1;
466 #endif
468 /* Let the user know about the opencount. */
469 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_FILES, 0))
470 _dl_debug_printf ("opening file=%s; opencount == %u\n\n",
471 new->l_name, new->l_opencount);
475 void *
476 internal_function
477 _dl_open (const char *file, int mode, const void *caller)
479 struct dl_open_args args;
480 const char *objname;
481 const char *errstring;
482 int errcode;
484 if ((mode & RTLD_BINDING_MASK) == 0)
485 /* One of the flags must be set. */
486 _dl_signal_error (EINVAL, file, NULL, N_("invalid mode for dlopen()"));
488 /* Make sure we are alone. */
489 __rtld_lock_lock_recursive (GL(dl_load_lock));
491 args.file = file;
492 args.mode = mode;
493 args.caller = caller;
494 args.map = NULL;
495 errcode = _dl_catch_error (&objname, &errstring, dl_open_worker, &args);
497 #ifndef MAP_COPY
498 /* We must munmap() the cache file. */
499 _dl_unload_cache ();
500 #endif
502 /* Release the lock. */
503 __rtld_lock_unlock_recursive (GL(dl_load_lock));
505 if (__builtin_expect (errstring != NULL, 0))
507 /* Some error occurred during loading. */
508 char *local_errstring;
509 size_t len_errstring;
511 /* Remove the object from memory. It may be in an inconsistent
512 state if relocation failed, for example. */
513 if (args.map)
515 unsigned int i;
517 /* Increment open counters for all objects since this
518 sometimes has not happened yet. */
519 if (args.map->l_searchlist.r_list[0]->l_opencount == 0)
520 for (i = 0; i < args.map->l_searchlist.r_nlist; ++i)
521 ++args.map->l_searchlist.r_list[i]->l_opencount;
523 #ifdef USE_TLS
524 /* Maybe some of the modules which were loaded uses TLS.
525 Since it will be removed in the following _dl_close call
526 we have to mark the dtv array as having gaps to fill
527 the holes. This is a pessimistic assumption which won't
528 hurt if not true. */
529 GL(dl_tls_dtv_gaps) = true;
530 #endif
532 _dl_close (args.map);
535 /* Make a local copy of the error string so that we can release the
536 memory allocated for it. */
537 len_errstring = strlen (errstring) + 1;
538 if (objname == errstring + len_errstring)
540 size_t total_len = len_errstring + strlen (objname) + 1;
541 local_errstring = alloca (total_len);
542 memcpy (local_errstring, errstring, total_len);
543 objname = local_errstring + len_errstring;
545 else
547 local_errstring = alloca (len_errstring);
548 memcpy (local_errstring, errstring, len_errstring);
551 if (errstring != _dl_out_of_memory)
552 free ((char *) errstring);
554 /* Reraise the error. */
555 _dl_signal_error (errcode, objname, NULL, local_errstring);
558 #ifndef SHARED
559 DL_STATIC_INIT (args.map);
560 #endif
562 return args.map;
564 libc_hidden_def (_dl_open)
567 #ifdef SCOPE_DEBUG
568 #include <unistd.h>
570 static void
571 show_scope (struct link_map *new)
573 int scope_cnt;
575 for (scope_cnt = 0; new->l_scope[scope_cnt] != NULL; ++scope_cnt)
577 char numbuf[2];
578 unsigned int cnt;
580 numbuf[0] = '0' + scope_cnt;
581 numbuf[1] = '\0';
582 _dl_printf ("scope %s:", numbuf);
584 for (cnt = 0; cnt < new->l_scope[scope_cnt]->r_nlist; ++cnt)
585 if (*new->l_scope[scope_cnt]->r_list[cnt]->l_name)
586 _dl_printf (" %s", new->l_scope[scope_cnt]->r_list[cnt]->l_name);
587 else
588 _dl_printf (" <main>");
590 _dl_printf ("\n");
593 #endif