* sysdeps/m68k/fpu/bits/mathinline.h: Don't define log2 as inline.
[glibc.git] / elf / dl-open.c
blob43daf60d58c39cca8ec82c86f5a6b81f39ccf4f9
1 /* Load a shared object at runtime, relocate it, and run its initializer.
2 Copyright (C) 1996,1997,1998,1999,2000,2001 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <assert.h>
21 #include <dlfcn.h>
22 #include <errno.h>
23 #include <libintl.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/mman.h> /* Check whether MAP_COPY is defined. */
28 #include <sys/param.h>
29 #include <bits/libc-lock.h>
30 #include <ldsodefs.h>
31 #include <bp-sym.h>
33 #include <dl-dst.h>
34 #include <stdio-common/_itoa.h>
37 extern ElfW(Addr) _dl_sysdep_start (void **start_argptr,
38 void (*dl_main) (const ElfW(Phdr) *phdr,
39 ElfW(Word) phnum,
40 ElfW(Addr) *user_entry));
41 weak_extern (BP_SYM (_dl_sysdep_start))
43 /* This function is used to unload the cache file if necessary. */
44 extern void _dl_unload_cache (void);
46 extern int __libc_multiple_libcs; /* Defined in init-first.c. */
48 extern int __libc_argc;
49 extern char **__libc_argv;
51 extern char **__environ;
53 extern int _dl_lazy; /* Do we do lazy relocations? */
55 /* Undefine the following for debugging. */
56 /* #define SCOPE_DEBUG 1 */
57 #ifdef SCOPE_DEBUG
58 static void show_scope (struct link_map *new);
59 #endif
61 /* During the program run we must not modify the global data of
62 loaded shared object simultanously in two threads. Therefore we
63 protect `_dl_open' and `_dl_close' in dl-close.c.
65 This must be a recursive lock since the initializer function of
66 the loaded object might as well require a call to this function.
67 At this time it is not anymore a problem to modify the tables. */
68 __libc_lock_define (extern, _dl_load_lock)
70 extern size_t _dl_platformlen;
72 /* We must be carefull not to leave us in an inconsistent state. Thus we
73 catch any error and re-raise it after cleaning up. */
75 struct dl_open_args
77 const char *file;
78 int mode;
79 const void *caller;
80 struct link_map *map;
84 static int
85 add_to_global (struct link_map *new)
87 struct link_map **new_global;
88 unsigned int to_add = 0;
89 unsigned int cnt;
91 /* Count the objects we have to put in the global scope. */
92 for (cnt = 0; cnt < new->l_searchlist.r_nlist; ++cnt)
93 if (new->l_searchlist.r_list[cnt]->l_global == 0)
94 ++to_add;
96 /* The symbols of the new objects and its dependencies are to be
97 introduced into the global scope that will be used to resolve
98 references from other dynamically-loaded objects.
100 The global scope is the searchlist in the main link map. We
101 extend this list if necessary. There is one problem though:
102 since this structure was allocated very early (before the libc
103 is loaded) the memory it uses is allocated by the malloc()-stub
104 in the ld.so. When we come here these functions are not used
105 anymore. Instead the malloc() implementation of the libc is
106 used. But this means the block from the main map cannot be used
107 in an realloc() call. Therefore we allocate a completely new
108 array the first time we have to add something to the locale scope. */
110 if (_dl_global_scope_alloc == 0)
112 /* This is the first dynamic object given global scope. */
113 _dl_global_scope_alloc = _dl_main_searchlist->r_nlist + to_add + 8;
114 new_global = (struct link_map **)
115 malloc (_dl_global_scope_alloc * sizeof (struct link_map *));
116 if (new_global == NULL)
118 _dl_global_scope_alloc = 0;
119 nomem:
120 _dl_signal_error (ENOMEM, new->l_libname->name,
121 N_("cannot extend global scope"));
122 return 1;
125 /* Copy over the old entries. */
126 memcpy (new_global, _dl_main_searchlist->r_list,
127 (_dl_main_searchlist->r_nlist * sizeof (struct link_map *)));
129 _dl_main_searchlist->r_list = new_global;
131 else if (_dl_main_searchlist->r_nlist + to_add > _dl_global_scope_alloc)
133 /* We have to extend the existing array of link maps in the
134 main map. */
135 new_global = (struct link_map **)
136 realloc (_dl_main_searchlist->r_list,
137 ((_dl_global_scope_alloc + to_add + 8)
138 * sizeof (struct link_map *)));
139 if (new_global == NULL)
140 goto nomem;
142 _dl_global_scope_alloc += to_add + 8;
143 _dl_main_searchlist->r_list = new_global;
146 /* Now add the new entries. */
147 for (cnt = 0; cnt < new->l_searchlist.r_nlist; ++cnt)
149 struct link_map *map = new->l_searchlist.r_list[cnt];
151 if (map->l_global == 0)
153 map->l_global = 1;
154 _dl_main_searchlist->r_list[_dl_main_searchlist->r_nlist] = map;
155 ++_dl_main_searchlist->r_nlist;
159 /* XXX Do we have to add something to r_dupsearchlist??? --drepper */
160 return 0;
164 static void
165 dl_open_worker (void *a)
167 struct dl_open_args *args = a;
168 const char *file = args->file;
169 int mode = args->mode;
170 struct link_map *new, *l;
171 const char *dst;
172 int lazy;
173 unsigned int i;
175 /* Maybe we have to expand a DST. */
176 dst = strchr (file, '$');
177 if (dst != NULL)
179 const void *caller = args->caller;
180 size_t len = strlen (file);
181 size_t required;
182 struct link_map *call_map;
183 char *new_file;
185 /* DSTs must not appear in SUID/SGID programs. */
186 if (__libc_enable_secure)
187 /* This is an error. */
188 _dl_signal_error (0, "dlopen",
189 N_("DST not allowed in SUID/SGID programs"));
191 /* We have to find out from which object the caller is calling. */
192 call_map = NULL;
193 for (l = _dl_loaded; l; l = l->l_next)
194 if (caller >= (const void *) l->l_map_start
195 && caller < (const void *) l->l_map_end)
197 /* There must be exactly one DSO for the range of the virtual
198 memory. Otherwise something is really broken. */
199 call_map = l;
200 break;
203 if (call_map == NULL)
204 /* In this case we assume this is the main application. */
205 call_map = _dl_loaded;
207 /* Determine how much space we need. We have to allocate the
208 memory locally. */
209 required = DL_DST_REQUIRED (call_map, file, len, _dl_dst_count (dst, 0));
211 /* Get space for the new file name. */
212 new_file = (char *) alloca (required + 1);
214 /* Generate the new file name. */
215 DL_DST_SUBSTITUTE (call_map, file, new_file, 0);
217 /* If the substitution failed don't try to load. */
218 if (*new_file == '\0')
219 _dl_signal_error (0, "dlopen",
220 N_("empty dynamic string token substitution"));
222 /* Now we have a new file name. */
223 file = new_file;
226 /* Load the named object. */
227 args->map = new = _dl_map_object (NULL, file, 0, lt_loaded, 0,
228 mode);
230 /* If the pointer returned is NULL this means the RTLD_NOLOAD flag is
231 set and the object is not already loaded. */
232 if (new == NULL)
234 assert (mode & RTLD_NOLOAD);
235 return;
238 /* It was already open. */
239 if (new->l_searchlist.r_list != NULL)
241 /* Let the user know about the opencount. */
242 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_FILES, 0))
243 _dl_debug_printf ("opening file=%s; opencount == %u\n\n",
244 new->l_name, new->l_opencount);
246 /* If the user requested the object to be in the global namespace
247 but it is not so far, add it now. */
248 if ((mode & RTLD_GLOBAL) && new->l_global == 0)
249 (void) add_to_global (new);
251 /* Increment just the reference counter of the object. */
252 ++new->l_opencount;
254 return;
257 /* Load that object's dependencies. */
258 _dl_map_object_deps (new, NULL, 0, 0);
260 /* So far, so good. Now check the versions. */
261 for (i = 0; i < new->l_searchlist.r_nlist; ++i)
262 if (new->l_searchlist.r_list[i]->l_versions == NULL)
263 (void) _dl_check_map_versions (new->l_searchlist.r_list[i], 0, 0);
265 #ifdef SCOPE_DEBUG
266 show_scope (new);
267 #endif
269 /* Only do lazy relocation if `LD_BIND_NOW' is not set. */
270 lazy = (mode & RTLD_BINDING_MASK) == RTLD_LAZY && _dl_lazy;
272 /* Relocate the objects loaded. We do this in reverse order so that copy
273 relocs of earlier objects overwrite the data written by later objects. */
275 l = new;
276 while (l->l_next)
277 l = l->l_next;
278 while (1)
280 if (! l->l_relocated)
282 #ifdef SHARED
283 if (_dl_profile != NULL)
285 /* If this here is the shared object which we want to profile
286 make sure the profile is started. We can find out whether
287 this is necessary or not by observing the `_dl_profile_map'
288 variable. If was NULL but is not NULL afterwars we must
289 start the profiling. */
290 struct link_map *old_profile_map = _dl_profile_map;
292 _dl_relocate_object (l, l->l_scope, 1, 1);
294 if (old_profile_map == NULL && _dl_profile_map != NULL)
295 /* We must prepare the profiling. */
296 _dl_start_profile (_dl_profile_map, _dl_profile_output);
298 else
299 #endif
300 _dl_relocate_object (l, l->l_scope, lazy, 0);
303 if (l == new)
304 break;
305 l = l->l_prev;
308 /* Increment the open count for all dependencies. */
309 for (i = 0; i < new->l_searchlist.r_nlist; ++i)
310 ++new->l_searchlist.r_list[i]->l_opencount;
312 /* Run the initializer functions of new objects. */
313 _dl_init (new, __libc_argc, __libc_argv, __environ);
315 /* Now we can make the new map available in the global scope. */
316 if (mode & RTLD_GLOBAL)
317 /* Move the object in the global namespace. */
318 if (add_to_global (new) != 0)
319 /* It failed. */
320 return;
322 /* Mark the object as not deletable if the RTLD_NODELETE flags was
323 passed. */
324 if (__builtin_expect (mode & RTLD_NODELETE, 0))
325 new->l_flags_1 |= DF_1_NODELETE;
327 if (_dl_sysdep_start == NULL)
328 /* We must be the static _dl_open in libc.a. A static program that
329 has loaded a dynamic object now has competition. */
330 __libc_multiple_libcs = 1;
332 /* Let the user know about the opencount. */
333 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_FILES, 0))
334 _dl_debug_printf ("opening file=%s; opencount == %u\n\n",
335 new->l_name, new->l_opencount);
339 void *
340 internal_function
341 _dl_open (const char *file, int mode, const void *caller)
343 struct dl_open_args args;
344 const char *objname;
345 const char *errstring;
346 int errcode;
348 if ((mode & RTLD_BINDING_MASK) == 0)
349 /* One of the flags must be set. */
350 _dl_signal_error (EINVAL, file, N_("invalid mode for dlopen()"));
352 /* Make sure we are alone. */
353 __libc_lock_lock (_dl_load_lock);
355 args.file = file;
356 args.mode = mode;
357 args.caller = caller;
358 args.map = NULL;
359 errcode = _dl_catch_error (&objname, &errstring, dl_open_worker, &args);
361 #ifndef MAP_COPY
362 /* We must munmap() the cache file. */
363 _dl_unload_cache ();
364 #endif
366 /* Release the lock. */
367 __libc_lock_unlock (_dl_load_lock);
369 if (errstring)
371 /* Some error occurred during loading. */
372 char *local_errstring;
373 size_t len_errstring;
375 /* Remove the object from memory. It may be in an inconsistent
376 state if relocation failed, for example. */
377 if (args.map)
379 unsigned int i;
381 /* Increment open counters for all objects since this has
382 not happened yet. */
383 for (i = 0; i < args.map->l_searchlist.r_nlist; ++i)
384 ++args.map->l_searchlist.r_list[i]->l_opencount;
386 _dl_close (args.map);
389 /* Make a local copy of the error string so that we can release the
390 memory allocated for it. */
391 len_errstring = strlen (errstring) + 1;
392 if (objname == errstring + len_errstring)
394 size_t total_len = len_errstring + strlen (objname) + 1;
395 local_errstring = alloca (total_len);
396 memcpy (local_errstring, errstring, total_len);
397 objname = local_errstring + len_errstring;
399 else
401 local_errstring = alloca (len_errstring);
402 memcpy (local_errstring, errstring, len_errstring);
405 if (errstring != _dl_out_of_memory)
406 free ((char *) errstring);
408 /* Reraise the error. */
409 _dl_signal_error (errcode, objname, local_errstring);
412 #ifndef SHARED
413 DL_STATIC_INIT (args.map);
414 #endif
416 return args.map;
420 #ifdef SCOPE_DEBUG
421 #include <unistd.h>
423 static void
424 show_scope (struct link_map *new)
426 int scope_cnt;
428 for (scope_cnt = 0; new->l_scope[scope_cnt] != NULL; ++scope_cnt)
430 char numbuf[2];
431 unsigned int cnt;
433 numbuf[0] = '0' + scope_cnt;
434 numbuf[1] = '\0';
435 _dl_printf ("scope %s:", numbuf);
437 for (cnt = 0; cnt < new->l_scope[scope_cnt]->r_nlist; ++cnt)
438 if (*new->l_scope[scope_cnt]->r_list[cnt]->l_name)
439 _dl_printf (" %s", new->l_scope[scope_cnt]->r_list[cnt]->l_name)
440 else
441 _dl_printf (" <main>", NULL);
443 _dl_printf ("\n", NULL);
446 #endif