Update.
[glibc.git] / elf / dl-open.c
blob097fd372c003bb061bc8bb25f6932bc92f541d48
1 /* Load a shared object at runtime, relocate it, and run its initializer.
2 Copyright (C) 1996, 1997, 1998, 1999 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 <elf/ldsodefs.h>
32 #include <dl-dst.h>
35 extern ElfW(Addr) _dl_sysdep_start (void **start_argptr,
36 void (*dl_main) (const ElfW(Phdr) *phdr,
37 ElfW(Word) phnum,
38 ElfW(Addr) *user_entry));
39 weak_extern (_dl_sysdep_start)
41 /* This function is used to unload the cache file if necessary. */
42 extern void _dl_unload_cache (void);
44 extern int __libc_multiple_libcs; /* Defined in init-first.c. */
46 extern int __libc_argc;
47 extern char **__libc_argv;
49 extern char **__environ;
51 extern int _dl_lazy; /* Do we do lazy relocations? */
53 /* Undefine the following for debugging. */
54 /* #define SCOPE_DEBUG 1 */
55 #ifdef SCOPE_DEBUG
56 static void show_scope (struct link_map *new);
57 #endif
59 /* During the program run we must not modify the global data of
60 loaded shared object simultanously in two threads. Therefore we
61 protect `_dl_open' and `_dl_close' in dl-close.c.
63 This must be a recursive lock since the initializer function of
64 the loaded object might as well require a call to this function.
65 At this time it is not anymore a problem to modify the tables. */
66 __libc_lock_define_initialized_recursive (, _dl_load_lock)
68 extern size_t _dl_platformlen;
70 /* We must be carefull not to leave us in an inconsistent state. Thus we
71 catch any error and re-raise it after cleaning up. */
73 struct dl_open_args
75 const char *file;
76 int mode;
77 const void *caller;
78 struct link_map *map;
81 static void
82 dl_open_worker (void *a)
84 struct dl_open_args *args = a;
85 const char *file = args->file;
86 int mode = args->mode;
87 struct link_map *new, *l;
88 ElfW(Addr) init;
89 struct r_debug *r;
90 unsigned int global_add;
91 const char *dst;
92 int lazy;
94 /* Maybe we have to expand a DST. */
95 dst = strchr (file, '$');
96 if (dst != NULL)
98 const void *caller = args->caller;
99 size_t len = strlen (file);
100 size_t required;
101 struct link_map *call_map;
102 char *new_file;
104 /* DSTs must not appear in SUID/SGID programs. */
105 if (__libc_enable_secure)
106 /* This is an error. */
107 _dl_signal_error (0, "dlopen",
108 "DST not allowed in SUID/SGID programs");
110 /* We have to find out from which object the caller is calling.
111 Find the highest-addressed object that ADDRESS is not below. */
112 call_map = NULL;
113 for (l = _dl_loaded; l; l = l->l_next)
114 if (l->l_addr != 0 /* Make sure we do not currently set this map up
115 in this moment. */
116 && caller >= (const void *) l->l_addr
117 && (call_map == NULL || call_map->l_addr < l->l_addr))
118 call_map = l;
120 if (call_map == NULL)
121 /* In this case we assume this is the main application. */
122 call_map = _dl_loaded;
124 /* Determine how much space we need. We have to allocate the
125 memory locally. */
126 required = DL_DST_REQUIRED (call_map, file, len, _dl_dst_count (dst, 0));
128 /* Get space for the new file name. */
129 new_file = (char *) alloca (required + 1);
131 /* Generate the new file name. */
132 DL_DST_SUBSTITUTE (call_map, file, new_file, 0);
134 /* If the substitution failed don't try to load. */
135 if (*new_file == '\0')
136 _dl_signal_error (0, "dlopen",
137 "empty dynamic string token substitution");
139 /* Now we have a new file name. */
140 file = new_file;
143 /* Load the named object. */
144 args->map = new = _dl_map_object (NULL, file, 0, lt_loaded, 0);
145 if (new->l_searchlist.r_list)
146 /* It was already open. */
147 return;
149 /* Load that object's dependencies. */
150 global_add = _dl_map_object_deps (new, NULL, 0, 0, mode & RTLD_GLOBAL);
152 /* So far, so good. Now check the versions. */
153 (void) _dl_check_all_versions (new, 0);
155 #ifdef SCOPE_DEBUG
156 show_scope (new);
157 #endif
159 /* Only do lazy relocation if `LD_BIND_NOW' is not set. */
160 lazy = (mode & RTLD_BINDING_MASK) == RTLD_LAZY && _dl_lazy;
162 /* Relocate the objects loaded. We do this in reverse order so that copy
163 relocs of earlier objects overwrite the data written by later objects. */
165 l = new;
166 while (l->l_next)
167 l = l->l_next;
168 while (1)
170 if (! l->l_relocated)
172 #ifdef PIC
173 if (_dl_profile != NULL)
175 /* If this here is the shared object which we want to profile
176 make sure the profile is started. We can find out whether
177 this is necessary or not by observing the `_dl_profile_map'
178 variable. If was NULL but is not NULL afterwars we must
179 start the profiling. */
180 struct link_map *old_profile_map = _dl_profile_map;
182 _dl_relocate_object (l, l->l_scope, 1, 1);
184 if (old_profile_map == NULL && _dl_profile_map != NULL)
185 /* We must prepare the profiling. */
186 _dl_start_profile (_dl_profile_map, _dl_profile_output);
188 else
189 #endif
190 _dl_relocate_object (l, l->l_scope, lazy, 0);
193 if (l == new)
194 break;
195 l = l->l_prev;
198 /* Notify the debugger we have added some objects. We need to call
199 _dl_debug_initialize in a static program in case dynamic linking has
200 not been used before. */
201 r = _dl_debug_initialize (0);
202 r->r_state = RT_ADD;
203 _dl_debug_state ();
205 /* Run the initializer functions of new objects. */
206 while ((init = _dl_init_next (&new->l_searchlist)))
207 (*(void (*) (int, char **, char **)) init) (__libc_argc, __libc_argv,
208 __environ);
210 /* Now we can make the new map available in the global scope. */
211 while (global_add-- > 0)
212 _dl_main_searchlist->r_list[_dl_main_searchlist->r_nlist++]->l_global = 1;
214 if (_dl_sysdep_start == NULL)
215 /* We must be the static _dl_open in libc.a. A static program that
216 has loaded a dynamic object now has competition. */
217 __libc_multiple_libcs = 1;
221 void *
222 internal_function
223 _dl_open (const char *file, int mode, const void *caller)
225 struct dl_open_args args;
226 char *errstring;
227 int errcode;
229 if ((mode & RTLD_BINDING_MASK) == 0)
230 /* One of the flags must be set. */
231 _dl_signal_error (EINVAL, file, _("invalid mode for dlopen()"));
233 /* Make sure we are alone. */
234 __libc_lock_lock (_dl_load_lock);
236 args.file = file;
237 args.mode = mode;
238 args.caller = caller;
239 args.map = NULL;
240 errcode = _dl_catch_error (&errstring, dl_open_worker, &args);
242 #ifndef MAP_COPY
243 /* We must munmap() the cache file. */
244 _dl_unload_cache ();
245 #endif
247 /* Release the lock. */
248 __libc_lock_unlock (_dl_load_lock);
250 if (errstring)
252 /* Some error occured during loading. */
253 char *local_errstring;
255 /* Remove the object from memory. It may be in an inconsistent
256 state if relocation failed, for example. */
257 if (args.map)
258 _dl_close (args.map);
260 /* Make a local copy of the error string so that we can release the
261 memory allocated for it. */
262 local_errstring = strdupa (errstring);
263 free (errstring);
265 /* Reraise the error. */
266 _dl_signal_error (errcode, NULL, local_errstring);
269 return args.map;
273 #ifdef SCOPE_DEBUG
274 #include <unistd.h>
276 static void
277 show_scope (struct link_map *new)
279 int scope_cnt;
281 for (scope_cnt = 0; new->l_scope[scope_cnt] != NULL; ++scope_cnt)
283 char numbuf[2];
284 unsigned int cnt;
286 numbuf[0] = '0' + scope_cnt;
287 numbuf[1] = '\0';
288 _dl_sysdep_message ("scope ", numbuf, ":", NULL);
290 for (cnt = 0; cnt < new->l_scope[scope_cnt]->r_nlist; ++cnt)
291 if (*new->l_scope[scope_cnt]->r_list[cnt]->l_name)
292 _dl_sysdep_message (" ",
293 new->l_scope[scope_cnt]->r_list[cnt]->l_name,
294 NULL);
295 else
296 _dl_sysdep_message (" <main>", NULL);
298 _dl_sysdep_message ("\n", NULL);
301 #endif