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 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>
34 #include <stdio-common/_itoa.h>
37 extern ElfW(Addr) _dl_sysdep_start (void **start_argptr,
38 void (*dl_main) (const ElfW(Phdr) *phdr,
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 */
58 static void show_scope (struct link_map *new);
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. */
85 add_to_global (struct link_map *new)
87 struct link_map **new_global;
88 unsigned int to_add = 0;
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)
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;
120 _dl_signal_error (ENOMEM, new->l_libname->name,
121 N_("cannot extend global scope"));
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
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)
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)
154 _dl_main_searchlist->r_list[_dl_main_searchlist->r_nlist] = map;
155 ++_dl_main_searchlist->r_nlist;
164 dl_open_worker (void *a)
166 struct dl_open_args *args = a;
167 const char *file = args->file;
168 int mode = args->mode;
169 struct link_map *new, *l;
174 /* Maybe we have to expand a DST. */
175 dst = strchr (file, '$');
178 const void *caller = args->caller;
179 size_t len = strlen (file);
181 struct link_map *call_map;
184 /* DSTs must not appear in SUID/SGID programs. */
185 if (__libc_enable_secure)
186 /* This is an error. */
187 _dl_signal_error (0, "dlopen",
188 N_("DST not allowed in SUID/SGID programs"));
190 /* We have to find out from which object the caller is calling. */
192 for (l = _dl_loaded; l; l = l->l_next)
193 if (caller >= (const void *) l->l_map_start
194 && caller < (const void *) l->l_map_end)
196 /* There must be exactly one DSO for the range of the virtual
197 memory. Otherwise something is really broken. */
202 if (call_map == NULL)
203 /* In this case we assume this is the main application. */
204 call_map = _dl_loaded;
206 /* Determine how much space we need. We have to allocate the
208 required = DL_DST_REQUIRED (call_map, file, len, _dl_dst_count (dst, 0));
210 /* Get space for the new file name. */
211 new_file = (char *) alloca (required + 1);
213 /* Generate the new file name. */
214 DL_DST_SUBSTITUTE (call_map, file, new_file, 0);
216 /* If the substitution failed don't try to load. */
217 if (*new_file == '\0')
218 _dl_signal_error (0, "dlopen",
219 N_("empty dynamic string token substitution"));
221 /* Now we have a new file name. */
225 /* Load the named object. */
226 args->map = new = _dl_map_object (NULL, file, 0, lt_loaded, 0,
229 /* If the pointer returned is NULL this means the RTLD_NOLOAD flag is
230 set and the object is not already loaded. */
233 assert (mode & RTLD_NOLOAD);
237 /* It was already open. */
238 if (new->l_searchlist.r_list != NULL)
240 /* Let the user know about the opencount. */
241 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_FILES, 0))
242 _dl_debug_printf ("opening file=%s; opencount == %u\n\n",
243 new->l_name, new->l_opencount);
245 /* If the user requested the object to be in the global namespace
246 but it is not so far, add it now. */
247 if ((mode & RTLD_GLOBAL) && new->l_global == 0)
248 (void) add_to_global (new);
250 /* Increment just the reference counter of the object. */
256 /* Load that object's dependencies. */
257 _dl_map_object_deps (new, NULL, 0, 0);
259 /* So far, so good. Now check the versions. */
260 for (i = 0; i < new->l_searchlist.r_nlist; ++i)
261 if (new->l_searchlist.r_list[i]->l_versions == NULL)
262 (void) _dl_check_map_versions (new->l_searchlist.r_list[i], 0, 0);
268 /* Only do lazy relocation if `LD_BIND_NOW' is not set. */
269 lazy = (mode & RTLD_BINDING_MASK) == RTLD_LAZY && _dl_lazy;
271 /* Relocate the objects loaded. We do this in reverse order so that copy
272 relocs of earlier objects overwrite the data written by later objects. */
279 if (! l->l_relocated)
282 if (_dl_profile != NULL)
284 /* If this here is the shared object which we want to profile
285 make sure the profile is started. We can find out whether
286 this is necessary or not by observing the `_dl_profile_map'
287 variable. If was NULL but is not NULL afterwars we must
288 start the profiling. */
289 struct link_map *old_profile_map = _dl_profile_map;
291 _dl_relocate_object (l, l->l_scope, 1, 1);
293 if (old_profile_map == NULL && _dl_profile_map != NULL)
294 /* We must prepare the profiling. */
295 _dl_start_profile (_dl_profile_map, _dl_profile_output);
299 _dl_relocate_object (l, l->l_scope, lazy, 0);
307 /* Increment the open count for all dependencies. */
308 for (i = 0; i < new->l_searchlist.r_nlist; ++i)
309 ++new->l_searchlist.r_list[i]->l_opencount;
311 /* Run the initializer functions of new objects. */
312 _dl_init (new, __libc_argc, __libc_argv, __environ);
314 /* Now we can make the new map available in the global scope. */
315 if (mode & RTLD_GLOBAL)
316 /* Move the object in the global namespace. */
317 if (add_to_global (new) != 0)
321 /* Mark the object as not deletable if the RTLD_NODELETE flags was
323 if (__builtin_expect (mode & RTLD_NODELETE, 0))
324 new->l_flags_1 |= DF_1_NODELETE;
326 if (_dl_sysdep_start == NULL)
327 /* We must be the static _dl_open in libc.a. A static program that
328 has loaded a dynamic object now has competition. */
329 __libc_multiple_libcs = 1;
331 /* Let the user know about the opencount. */
332 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_FILES, 0))
333 _dl_debug_printf ("opening file=%s; opencount == %u\n\n",
334 new->l_name, new->l_opencount);
340 _dl_open (const char *file, int mode, const void *caller)
342 struct dl_open_args args;
344 const char *errstring;
347 if ((mode & RTLD_BINDING_MASK) == 0)
348 /* One of the flags must be set. */
349 _dl_signal_error (EINVAL, file, N_("invalid mode for dlopen()"));
351 /* Make sure we are alone. */
352 __libc_lock_lock (_dl_load_lock);
356 args.caller = caller;
358 errcode = _dl_catch_error (&objname, &errstring, dl_open_worker, &args);
361 /* We must munmap() the cache file. */
365 /* Release the lock. */
366 __libc_lock_unlock (_dl_load_lock);
370 /* Some error occurred during loading. */
371 char *local_errstring;
372 size_t len_errstring;
374 /* Remove the object from memory. It may be in an inconsistent
375 state if relocation failed, for example. */
380 /* Increment open counters for all objects since this has
382 for (i = 0; i < args.map->l_searchlist.r_nlist; ++i)
383 ++args.map->l_searchlist.r_list[i]->l_opencount;
385 _dl_close (args.map);
388 /* Make a local copy of the error string so that we can release the
389 memory allocated for it. */
390 len_errstring = strlen (errstring) + 1;
391 if (objname == errstring + len_errstring)
393 size_t total_len = len_errstring + strlen (objname) + 1;
394 local_errstring = alloca (total_len);
395 memcpy (local_errstring, errstring, total_len);
396 objname = local_errstring + len_errstring;
400 local_errstring = alloca (len_errstring);
401 memcpy (local_errstring, errstring, len_errstring);
404 if (errstring != _dl_out_of_memory)
405 free ((char *) errstring);
407 /* Reraise the error. */
408 _dl_signal_error (errcode, objname, local_errstring);
412 DL_STATIC_INIT (args.map);
423 show_scope (struct link_map *new)
427 for (scope_cnt = 0; new->l_scope[scope_cnt] != NULL; ++scope_cnt)
432 numbuf[0] = '0' + scope_cnt;
434 _dl_printf ("scope %s:", numbuf);
436 for (cnt = 0; cnt < new->l_scope[scope_cnt]->r_nlist; ++cnt)
437 if (*new->l_scope[scope_cnt]->r_list[cnt]->l_name)
438 _dl_printf (" %s", new->l_scope[scope_cnt]->r_list[cnt]->l_name)
440 _dl_printf (" <main>", NULL);
442 _dl_printf ("\n", NULL);