1 /* Load a shared object at runtime, relocate it, and run its initializer.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000 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. */
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 /* This function is used to unload the cache file if necessary. */
43 extern void _dl_unload_cache (void);
45 extern int __libc_multiple_libcs; /* Defined in init-first.c. */
47 extern int __libc_argc;
48 extern char **__libc_argv;
50 extern char **__environ;
52 extern int _dl_lazy; /* Do we do lazy relocations? */
54 /* Undefine the following for debugging. */
55 /* #define SCOPE_DEBUG 1 */
57 static void show_scope (struct link_map *new);
60 /* During the program run we must not modify the global data of
61 loaded shared object simultanously in two threads. Therefore we
62 protect `_dl_open' and `_dl_close' in dl-close.c.
64 This must be a recursive lock since the initializer function of
65 the loaded object might as well require a call to this function.
66 At this time it is not anymore a problem to modify the tables. */
67 __libc_lock_define (extern, _dl_load_lock)
69 extern size_t _dl_platformlen;
71 /* We must be carefull not to leave us in an inconsistent state. Thus we
72 catch any error and re-raise it after cleaning up. */
83 dl_open_worker (void *a)
85 struct dl_open_args *args = a;
86 const char *file = args->file;
87 int mode = args->mode;
88 struct link_map *new, *l;
92 /* Maybe we have to expand a DST. */
93 dst = strchr (file, '$');
96 const void *caller = args->caller;
97 size_t len = strlen (file);
99 struct link_map *call_map;
102 /* DSTs must not appear in SUID/SGID programs. */
103 if (__libc_enable_secure)
104 /* This is an error. */
105 _dl_signal_error (0, "dlopen",
106 N_("DST not allowed in SUID/SGID programs"));
108 /* We have to find out from which object the caller is calling.
109 Find the highest-addressed object that ADDRESS is not below. */
111 for (l = _dl_loaded; l; l = l->l_next)
112 if (l->l_addr != 0 /* Make sure we do not currently set this map up
114 && caller >= (const void *) l->l_map_start
115 && caller < (const void *) l->l_map_end)
117 /* There must be exactly one DSO for the range of the virtual
118 memory. Otherwise something is really broken. */
123 if (call_map == NULL)
124 /* In this case we assume this is the main application. */
125 call_map = _dl_loaded;
127 /* Determine how much space we need. We have to allocate the
129 required = DL_DST_REQUIRED (call_map, file, len, _dl_dst_count (dst, 0));
131 /* Get space for the new file name. */
132 new_file = (char *) alloca (required + 1);
134 /* Generate the new file name. */
135 DL_DST_SUBSTITUTE (call_map, file, new_file, 0);
137 /* If the substitution failed don't try to load. */
138 if (*new_file == '\0')
139 _dl_signal_error (0, "dlopen",
140 N_("empty dynamic string token substitution"));
142 /* Now we have a new file name. */
146 /* Load the named object. */
147 args->map = new = _dl_map_object (NULL, file, 0, lt_loaded, 0,
150 /* If the pointer returned is NULL this means the RTLD_NOLOAD flag is
151 set and the object is not already loaded. */
154 assert (mode & RTLD_NOLOAD);
158 if (new->l_searchlist.r_list)
159 /* It was already open. */
162 /* Load that object's dependencies. */
163 _dl_map_object_deps (new, NULL, 0, 0);
165 /* So far, so good. Now check the versions. */
166 (void) _dl_check_all_versions (new, 0, 0);
172 /* Only do lazy relocation if `LD_BIND_NOW' is not set. */
173 lazy = (mode & RTLD_BINDING_MASK) == RTLD_LAZY && _dl_lazy;
175 /* Relocate the objects loaded. We do this in reverse order so that copy
176 relocs of earlier objects overwrite the data written by later objects. */
183 if (! l->l_relocated)
186 if (_dl_profile != NULL)
188 /* If this here is the shared object which we want to profile
189 make sure the profile is started. We can find out whether
190 this is necessary or not by observing the `_dl_profile_map'
191 variable. If was NULL but is not NULL afterwars we must
192 start the profiling. */
193 struct link_map *old_profile_map = _dl_profile_map;
195 _dl_relocate_object (l, l->l_scope, 1, 1);
197 if (old_profile_map == NULL && _dl_profile_map != NULL)
198 /* We must prepare the profiling. */
199 _dl_start_profile (_dl_profile_map, _dl_profile_output);
203 _dl_relocate_object (l, l->l_scope, lazy, 0);
211 /* Run the initializer functions of new objects. */
212 _dl_init (new, __libc_argc, __libc_argv, __environ);
214 /* Now we can make the new map available in the global scope. */
215 if (mode & RTLD_GLOBAL)
217 struct link_map **new_global;
218 unsigned int to_add = 0;
221 /* Count the objects we have to put in the global scope. */
222 for (cnt = 0; cnt < new->l_searchlist.r_nlist; ++cnt)
223 if (new->l_searchlist.r_list[cnt]->l_global == 0)
226 /* The symbols of the new objects and its dependencies are to be
227 introduced into the global scope that will be used to resolve
228 references from other dynamically-loaded objects.
230 The global scope is the searchlist in the main link map. We
231 extend this list if necessary. There is one problem though:
232 since this structure was allocated very early (before the libc
233 is loaded) the memory it uses is allocated by the malloc()-stub
234 in the ld.so. When we come here these functions are not used
235 anymore. Instead the malloc() implementation of the libc is
236 used. But this means the block from the main map cannot be used
237 in an realloc() call. Therefore we allocate a completely new
238 array the first time we have to add something to the locale scope. */
240 if (_dl_global_scope_alloc == 0)
242 /* This is the first dynamic object given global scope. */
243 _dl_global_scope_alloc = _dl_main_searchlist->r_nlist + to_add + 8;
244 new_global = (struct link_map **)
245 malloc (_dl_global_scope_alloc * sizeof (struct link_map *));
246 if (new_global == NULL)
248 _dl_global_scope_alloc = 0;
250 _dl_signal_error (ENOMEM, new->l_libname->name,
251 N_("cannot extend global scope"));
255 /* Copy over the old entries. */
256 memcpy (new_global, _dl_main_searchlist->r_list,
257 (_dl_main_searchlist->r_nlist * sizeof (struct link_map *)));
259 _dl_main_searchlist->r_list = new_global;
261 else if (_dl_main_searchlist->r_nlist + to_add > _dl_global_scope_alloc)
263 /* We have to extend the existing array of link maps in the
265 new_global = (struct link_map **)
266 realloc (_dl_main_searchlist->r_list,
267 ((_dl_global_scope_alloc + to_add + 8)
268 * sizeof (struct link_map *)));
269 if (new_global == NULL)
272 _dl_global_scope_alloc += to_add + 8;
273 _dl_main_searchlist->r_list = new_global;
276 /* Now add the new entries. */
277 for (cnt = 0; cnt < new->l_searchlist.r_nlist; ++cnt)
279 struct link_map *map = new->l_searchlist.r_list[cnt];
281 if (map->l_global == 0)
284 _dl_main_searchlist->r_list[_dl_main_searchlist->r_nlist] = map;
285 ++_dl_main_searchlist->r_nlist;
289 /* XXX Do we have to add something to r_dupsearchlist??? --drepper */
292 /* Mark the object as not deletable if the RTLD_NODELETE flags was
294 if (__builtin_expect (mode & RTLD_NODELETE, 0))
295 new->l_flags_1 |= DF_1_NODELETE;
297 if (_dl_sysdep_start == NULL)
298 /* We must be the static _dl_open in libc.a. A static program that
299 has loaded a dynamic object now has competition. */
300 __libc_multiple_libcs = 1;
306 _dl_open (const char *file, int mode, const void *caller)
308 struct dl_open_args args;
310 const char *errstring;
313 if ((mode & RTLD_BINDING_MASK) == 0)
314 /* One of the flags must be set. */
315 _dl_signal_error (EINVAL, file, N_("invalid mode for dlopen()"));
317 /* Make sure we are alone. */
318 __libc_lock_lock (_dl_load_lock);
322 args.caller = caller;
324 errcode = _dl_catch_error (&objname, &errstring, dl_open_worker, &args);
327 /* We must munmap() the cache file. */
331 /* Release the lock. */
332 __libc_lock_unlock (_dl_load_lock);
336 /* Some error occurred during loading. */
337 char *local_errstring;
339 /* Remove the object from memory. It may be in an inconsistent
340 state if relocation failed, for example. */
342 _dl_close (args.map);
344 /* Make a local copy of the error string so that we can release the
345 memory allocated for it. */
346 local_errstring = strdupa (errstring);
347 if (errstring != _dl_out_of_memory)
348 free ((char *) errstring);
350 /* Reraise the error. */
351 _dl_signal_error (errcode, objname, local_errstring);
362 show_scope (struct link_map *new)
366 for (scope_cnt = 0; new->l_scope[scope_cnt] != NULL; ++scope_cnt)
371 numbuf[0] = '0' + scope_cnt;
373 _dl_sysdep_message ("scope ", numbuf, ":", NULL);
375 for (cnt = 0; cnt < new->l_scope[scope_cnt]->r_nlist; ++cnt)
376 if (*new->l_scope[scope_cnt]->r_list[cnt]->l_name)
377 _dl_sysdep_message (" ",
378 new->l_scope[scope_cnt]->r_list[cnt]->l_name,
381 _dl_sysdep_message (" <main>", NULL);
383 _dl_sysdep_message ("\n", NULL);