]>
Commit | Line | Data |
---|---|---|
266180eb | 1 | /* Load a shared object at runtime, relocate it, and run its initializer. |
d9cb1a7d | 2 | Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc. |
afd4eb37 UD |
3 | This file is part of the GNU C Library. |
4 | ||
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. | |
9 | ||
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. | |
14 | ||
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. */ | |
266180eb | 19 | |
dc5efe83 | 20 | #include <assert.h> |
266180eb | 21 | #include <dlfcn.h> |
ba79d61b | 22 | #include <errno.h> |
06535ae9 | 23 | #include <libintl.h> |
a853022c | 24 | #include <stdlib.h> |
7a68c94a | 25 | #include <string.h> |
06535ae9 | 26 | #include <unistd.h> |
08cac4ac | 27 | #include <sys/mman.h> /* Check whether MAP_COPY is defined. */ |
dc5efe83 | 28 | #include <sys/param.h> |
9a0a462c | 29 | #include <bits/libc-lock.h> |
a42195db | 30 | #include <ldsodefs.h> |
ebdf53a7 | 31 | #include <bp-sym.h> |
ba79d61b | 32 | |
dc5efe83 | 33 | #include <dl-dst.h> |
b35e21f4 | 34 | #include <stdio-common/_itoa.h> |
dc5efe83 | 35 | |
39778c6c | 36 | |
11336c16 UD |
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)); | |
ebdf53a7 | 41 | weak_extern (BP_SYM (_dl_sysdep_start)) |
39778c6c | 42 | |
08cac4ac UD |
43 | /* This function is used to unload the cache file if necessary. */ |
44 | extern void _dl_unload_cache (void); | |
45 | ||
39778c6c UD |
46 | extern int __libc_multiple_libcs; /* Defined in init-first.c. */ |
47 | ||
dcf0671d UD |
48 | extern int __libc_argc; |
49 | extern char **__libc_argv; | |
dcf0671d | 50 | |
57ba7bb4 | 51 | extern char **__environ; |
dcf0671d | 52 | |
12b5b6b7 UD |
53 | extern int _dl_lazy; /* Do we do lazy relocations? */ |
54 | ||
482eec0d UD |
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 | |
26b4d766 UD |
60 | |
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. | |
64 | ||
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. */ | |
cf197e41 | 68 | __libc_lock_define (extern, _dl_load_lock) |
26b4d766 | 69 | |
dc5efe83 | 70 | extern size_t _dl_platformlen; |
26b4d766 | 71 | |
7a68c94a UD |
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. */ | |
74 | ||
75 | struct dl_open_args | |
266180eb | 76 | { |
7a68c94a UD |
77 | const char *file; |
78 | int mode; | |
dc5efe83 | 79 | const void *caller; |
7a68c94a UD |
80 | struct link_map *map; |
81 | }; | |
82 | ||
d785c366 UD |
83 | |
84 | static int | |
85 | add_to_global (struct link_map *new) | |
86 | { | |
87 | struct link_map **new_global; | |
88 | unsigned int to_add = 0; | |
89 | unsigned int cnt; | |
90 | ||
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; | |
95 | ||
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. | |
99 | ||
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. */ | |
109 | ||
110 | if (_dl_global_scope_alloc == 0) | |
111 | { | |
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) | |
117 | { | |
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; | |
123 | } | |
124 | ||
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 *))); | |
128 | ||
129 | _dl_main_searchlist->r_list = new_global; | |
130 | } | |
131 | else if (_dl_main_searchlist->r_nlist + to_add > _dl_global_scope_alloc) | |
132 | { | |
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; | |
141 | ||
142 | _dl_global_scope_alloc += to_add + 8; | |
143 | _dl_main_searchlist->r_list = new_global; | |
144 | } | |
145 | ||
146 | /* Now add the new entries. */ | |
147 | for (cnt = 0; cnt < new->l_searchlist.r_nlist; ++cnt) | |
148 | { | |
149 | struct link_map *map = new->l_searchlist.r_list[cnt]; | |
150 | ||
151 | if (map->l_global == 0) | |
152 | { | |
153 | map->l_global = 1; | |
154 | _dl_main_searchlist->r_list[_dl_main_searchlist->r_nlist] = map; | |
155 | ++_dl_main_searchlist->r_nlist; | |
156 | } | |
157 | } | |
158 | ||
159 | /* XXX Do we have to add something to r_dupsearchlist??? --drepper */ | |
160 | return 0; | |
161 | } | |
162 | ||
163 | ||
7a68c94a UD |
164 | static void |
165 | dl_open_worker (void *a) | |
166 | { | |
167 | struct dl_open_args *args = a; | |
168 | const char *file = args->file; | |
169 | int mode = args->mode; | |
266180eb | 170 | struct link_map *new, *l; |
dc5efe83 | 171 | const char *dst; |
12b5b6b7 | 172 | int lazy; |
752a2a50 | 173 | unsigned int i; |
dc5efe83 UD |
174 | |
175 | /* Maybe we have to expand a DST. */ | |
176 | dst = strchr (file, '$'); | |
177 | if (dst != NULL) | |
178 | { | |
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; | |
184 | ||
06535ae9 UD |
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", | |
8e17ea58 | 189 | N_("DST not allowed in SUID/SGID programs")); |
06535ae9 | 190 | |
dc5efe83 UD |
191 | /* We have to find out from which object the caller is calling. |
192 | Find the highest-addressed object that ADDRESS is not below. */ | |
193 | call_map = NULL; | |
194 | for (l = _dl_loaded; l; l = l->l_next) | |
195 | if (l->l_addr != 0 /* Make sure we do not currently set this map up | |
196 | in this moment. */ | |
28760b3d UD |
197 | && caller >= (const void *) l->l_map_start |
198 | && caller < (const void *) l->l_map_end) | |
199 | { | |
200 | /* There must be exactly one DSO for the range of the virtual | |
201 | memory. Otherwise something is really broken. */ | |
202 | call_map = l; | |
203 | break; | |
204 | } | |
dc5efe83 UD |
205 | |
206 | if (call_map == NULL) | |
207 | /* In this case we assume this is the main application. */ | |
208 | call_map = _dl_loaded; | |
209 | ||
210 | /* Determine how much space we need. We have to allocate the | |
211 | memory locally. */ | |
212 | required = DL_DST_REQUIRED (call_map, file, len, _dl_dst_count (dst, 0)); | |
213 | ||
214 | /* Get space for the new file name. */ | |
215 | new_file = (char *) alloca (required + 1); | |
216 | ||
217 | /* Generate the new file name. */ | |
218 | DL_DST_SUBSTITUTE (call_map, file, new_file, 0); | |
219 | ||
220 | /* If the substitution failed don't try to load. */ | |
221 | if (*new_file == '\0') | |
222 | _dl_signal_error (0, "dlopen", | |
8e17ea58 | 223 | N_("empty dynamic string token substitution")); |
dc5efe83 UD |
224 | |
225 | /* Now we have a new file name. */ | |
226 | file = new_file; | |
227 | } | |
ba79d61b | 228 | |
266180eb | 229 | /* Load the named object. */ |
bf8b3e74 | 230 | args->map = new = _dl_map_object (NULL, file, 0, lt_loaded, 0, |
2f54c82d | 231 | mode); |
bf8b3e74 UD |
232 | |
233 | /* If the pointer returned is NULL this means the RTLD_NOLOAD flag is | |
234 | set and the object is not already loaded. */ | |
235 | if (new == NULL) | |
236 | { | |
237 | assert (mode & RTLD_NOLOAD); | |
238 | return; | |
239 | } | |
240 | ||
42c4f32a UD |
241 | /* It was already open. */ |
242 | if (new->l_searchlist.r_list != NULL) | |
b35e21f4 UD |
243 | { |
244 | /* Let the user know about the opencount. */ | |
245 | if (__builtin_expect (_dl_debug_files, 0)) | |
246 | { | |
247 | char buf[20]; | |
248 | ||
249 | buf[sizeof buf - 1] = '\0'; | |
250 | ||
f55727ca | 251 | _dl_debug_message (1, "opening file=", new->l_name, |
b35e21f4 UD |
252 | "; opencount == ", |
253 | _itoa_word (new->l_opencount, | |
254 | buf + sizeof buf - 1, 10, 0), | |
f55727ca | 255 | "\n\n", NULL); |
b35e21f4 | 256 | } |
d785c366 | 257 | |
e30f2b98 | 258 | /* If the user requested the object to be in the global namespace |
d785c366 UD |
259 | but it is not so far, add it now. */ |
260 | if ((mode & RTLD_GLOBAL) && new->l_global == 0) | |
261 | (void) add_to_global (new); | |
262 | ||
42c4f32a UD |
263 | /* Increment just the reference counter of the object. */ |
264 | ++new->l_opencount; | |
265 | ||
b35e21f4 UD |
266 | return; |
267 | } | |
266180eb RM |
268 | |
269 | /* Load that object's dependencies. */ | |
d9cb1a7d | 270 | _dl_map_object_deps (new, NULL, 0, 0); |
266180eb | 271 | |
c84142e8 | 272 | /* So far, so good. Now check the versions. */ |
752a2a50 UD |
273 | for (i = 0; i < new->l_searchlist.r_nlist; ++i) |
274 | if (new->l_searchlist.r_list[i]->l_versions == NULL) | |
275 | (void) _dl_check_map_versions (new->l_searchlist.r_list[i], 0, 0); | |
ba79d61b | 276 | |
482eec0d UD |
277 | #ifdef SCOPE_DEBUG |
278 | show_scope (new); | |
279 | #endif | |
280 | ||
12b5b6b7 UD |
281 | /* Only do lazy relocation if `LD_BIND_NOW' is not set. */ |
282 | lazy = (mode & RTLD_BINDING_MASK) == RTLD_LAZY && _dl_lazy; | |
283 | ||
ba79d61b RM |
284 | /* Relocate the objects loaded. We do this in reverse order so that copy |
285 | relocs of earlier objects overwrite the data written by later objects. */ | |
286 | ||
287 | l = new; | |
288 | while (l->l_next) | |
289 | l = l->l_next; | |
4d6acc61 | 290 | while (1) |
ba79d61b RM |
291 | { |
292 | if (! l->l_relocated) | |
293 | { | |
b5567b2a | 294 | #ifdef SHARED |
5ad49c07 UD |
295 | if (_dl_profile != NULL) |
296 | { | |
297 | /* If this here is the shared object which we want to profile | |
298 | make sure the profile is started. We can find out whether | |
299 | this is necessary or not by observing the `_dl_profile_map' | |
300 | variable. If was NULL but is not NULL afterwars we must | |
301 | start the profiling. */ | |
302 | struct link_map *old_profile_map = _dl_profile_map; | |
303 | ||
be935610 | 304 | _dl_relocate_object (l, l->l_scope, 1, 1); |
5ad49c07 UD |
305 | |
306 | if (old_profile_map == NULL && _dl_profile_map != NULL) | |
307 | /* We must prepare the profiling. */ | |
308 | _dl_start_profile (_dl_profile_map, _dl_profile_output); | |
309 | } | |
310 | else | |
311 | #endif | |
12b5b6b7 | 312 | _dl_relocate_object (l, l->l_scope, lazy, 0); |
ba79d61b RM |
313 | } |
314 | ||
4d6acc61 RM |
315 | if (l == new) |
316 | break; | |
ba79d61b | 317 | l = l->l_prev; |
4d6acc61 | 318 | } |
ba79d61b | 319 | |
09f5e163 UD |
320 | /* Increment the open count for all dependencies. */ |
321 | for (i = 0; i < new->l_searchlist.r_nlist; ++i) | |
322 | ++new->l_searchlist.r_list[i]->l_opencount; | |
323 | ||
266180eb | 324 | /* Run the initializer functions of new objects. */ |
dacc8ffa | 325 | _dl_init (new, __libc_argc, __libc_argv, __environ); |
266180eb | 326 | |
50b65db1 | 327 | /* Now we can make the new map available in the global scope. */ |
d9cb1a7d | 328 | if (mode & RTLD_GLOBAL) |
d785c366 UD |
329 | /* Move the object in the global namespace. */ |
330 | if (add_to_global (new) != 0) | |
331 | /* It failed. */ | |
332 | return; | |
be935610 | 333 | |
bf8b3e74 UD |
334 | /* Mark the object as not deletable if the RTLD_NODELETE flags was |
335 | passed. */ | |
336 | if (__builtin_expect (mode & RTLD_NODELETE, 0)) | |
337 | new->l_flags_1 |= DF_1_NODELETE; | |
338 | ||
11336c16 | 339 | if (_dl_sysdep_start == NULL) |
cccda09f UD |
340 | /* We must be the static _dl_open in libc.a. A static program that |
341 | has loaded a dynamic object now has competition. */ | |
39778c6c | 342 | __libc_multiple_libcs = 1; |
b35e21f4 UD |
343 | |
344 | /* Let the user know about the opencount. */ | |
345 | if (__builtin_expect (_dl_debug_files, 0)) | |
346 | { | |
347 | char buf[20]; | |
348 | ||
349 | buf[sizeof buf - 1] = '\0'; | |
350 | ||
f55727ca | 351 | _dl_debug_message (1, "opening file=", new->l_name, |
b35e21f4 UD |
352 | "; opencount == ", |
353 | _itoa_word (new->l_opencount, | |
354 | buf + sizeof buf - 1, 10, 0), | |
f55727ca | 355 | "\n\n", NULL); |
b35e21f4 | 356 | } |
7a68c94a UD |
357 | } |
358 | ||
359 | ||
94e365c6 | 360 | void * |
7a68c94a | 361 | internal_function |
dc5efe83 | 362 | _dl_open (const char *file, int mode, const void *caller) |
7a68c94a UD |
363 | { |
364 | struct dl_open_args args; | |
8e17ea58 UD |
365 | const char *objname; |
366 | const char *errstring; | |
7a68c94a UD |
367 | int errcode; |
368 | ||
e254df14 UD |
369 | if ((mode & RTLD_BINDING_MASK) == 0) |
370 | /* One of the flags must be set. */ | |
8e17ea58 | 371 | _dl_signal_error (EINVAL, file, N_("invalid mode for dlopen()")); |
e254df14 | 372 | |
7a68c94a UD |
373 | /* Make sure we are alone. */ |
374 | __libc_lock_lock (_dl_load_lock); | |
375 | ||
376 | args.file = file; | |
377 | args.mode = mode; | |
dc5efe83 | 378 | args.caller = caller; |
7a68c94a | 379 | args.map = NULL; |
8e17ea58 | 380 | errcode = _dl_catch_error (&objname, &errstring, dl_open_worker, &args); |
39778c6c | 381 | |
08cac4ac UD |
382 | #ifndef MAP_COPY |
383 | /* We must munmap() the cache file. */ | |
384 | _dl_unload_cache (); | |
385 | #endif | |
386 | ||
26b4d766 UD |
387 | /* Release the lock. */ |
388 | __libc_lock_unlock (_dl_load_lock); | |
389 | ||
7a68c94a UD |
390 | if (errstring) |
391 | { | |
49c091e5 | 392 | /* Some error occurred during loading. */ |
7a68c94a | 393 | char *local_errstring; |
7b70fef6 | 394 | size_t len_errstring; |
7a68c94a | 395 | |
7a68c94a UD |
396 | /* Remove the object from memory. It may be in an inconsistent |
397 | state if relocation failed, for example. */ | |
398 | if (args.map) | |
c77a4478 UD |
399 | { |
400 | int i; | |
401 | ||
09f5e163 UD |
402 | /* Increment open counters for all objects since this has |
403 | not happened yet. */ | |
c77a4478 | 404 | for (i = 0; i < args.map->l_searchlist.r_nlist; ++i) |
09f5e163 | 405 | ++args.map->l_searchlist.r_list[i]->l_opencount; |
c77a4478 UD |
406 | |
407 | _dl_close (args.map); | |
408 | } | |
7a68c94a UD |
409 | |
410 | /* Make a local copy of the error string so that we can release the | |
411 | memory allocated for it. */ | |
7b70fef6 UD |
412 | len_errstring = strlen (errstring) + 1; |
413 | if (objname == errstring + len_errstring) | |
414 | { | |
7231f6f9 UD |
415 | size_t total_len = len_errstring + strlen (objname) + 1; |
416 | local_errstring = alloca (total_len); | |
417 | memcpy (local_errstring, errstring, total_len); | |
7b70fef6 UD |
418 | objname = local_errstring + len_errstring; |
419 | } | |
420 | else | |
421 | { | |
422 | local_errstring = alloca (len_errstring); | |
423 | memcpy (local_errstring, errstring, len_errstring); | |
424 | } | |
425 | ||
ca3c0135 UD |
426 | if (errstring != _dl_out_of_memory) |
427 | free ((char *) errstring); | |
7a68c94a UD |
428 | |
429 | /* Reraise the error. */ | |
8e17ea58 | 430 | _dl_signal_error (errcode, objname, local_errstring); |
7a68c94a UD |
431 | } |
432 | ||
7e36861e UD |
433 | #ifndef SHARED |
434 | DL_STATIC_INIT (args.map); | |
435 | #endif | |
436 | ||
7a68c94a | 437 | return args.map; |
266180eb | 438 | } |
482eec0d UD |
439 | |
440 | ||
441 | #ifdef SCOPE_DEBUG | |
442 | #include <unistd.h> | |
443 | ||
444 | static void | |
445 | show_scope (struct link_map *new) | |
446 | { | |
447 | int scope_cnt; | |
448 | ||
449 | for (scope_cnt = 0; new->l_scope[scope_cnt] != NULL; ++scope_cnt) | |
450 | { | |
451 | char numbuf[2]; | |
452 | unsigned int cnt; | |
453 | ||
454 | numbuf[0] = '0' + scope_cnt; | |
455 | numbuf[1] = '\0'; | |
456 | _dl_sysdep_message ("scope ", numbuf, ":", NULL); | |
457 | ||
458 | for (cnt = 0; cnt < new->l_scope[scope_cnt]->r_nlist; ++cnt) | |
459 | if (*new->l_scope[scope_cnt]->r_list[cnt]->l_name) | |
460 | _dl_sysdep_message (" ", | |
461 | new->l_scope[scope_cnt]->r_list[cnt]->l_name, | |
462 | NULL); | |
463 | else | |
464 | _dl_sysdep_message (" <main>", NULL); | |
465 | ||
466 | _dl_sysdep_message ("\n", NULL); | |
467 | } | |
468 | } | |
469 | #endif |