Update.
[glibc.git] / elf / dl-open.c
blob3670657379c91432e1a2aa0b0ede2949bfb2e6e2
1 /* Load a shared object at runtime, relocate it, and run its initializer.
2 Copyright (C) 1996, 1997, 1998 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 <dlfcn.h>
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/mman.h> /* Check whether MAP_COPY is defined. */
25 #include <bits/libc-lock.h>
26 #include <elf/ldsodefs.h>
29 extern ElfW(Addr) _dl_sysdep_start (void **start_argptr,
30 void (*dl_main) (const ElfW(Phdr) *phdr,
31 ElfW(Word) phnum,
32 ElfW(Addr) *user_entry));
33 weak_extern (_dl_sysdep_start)
35 /* This function is used to unload the cache file if necessary. */
36 extern void _dl_unload_cache (void);
38 extern int __libc_multiple_libcs; /* Defined in init-first.c. */
40 extern int __libc_argc;
41 extern char **__libc_argv;
43 extern char **__environ;
45 static size_t _dl_global_scope_alloc;
48 /* During the program run we must not modify the global data of
49 loaded shared object simultanously in two threads. Therefore we
50 protect `_dl_open' and `_dl_close' in dl-close.c.
52 This must be a recursive lock since the initializer function of
53 the loaded object might as well require a call to this function.
54 At this time it is not anymore a problem to modify the tables. */
55 __libc_lock_define_initialized_recursive (, _dl_load_lock)
58 /* We must be carefull not to leave us in an inconsistent state. Thus we
59 catch any error and re-raise it after cleaning up. */
61 struct dl_open_args
63 const char *file;
64 int mode;
65 struct link_map *map;
68 static void
69 dl_open_worker (void *a)
71 struct dl_open_args *args = a;
72 const char *file = args->file;
73 int mode = args->mode;
74 struct link_map *new, *l;
75 ElfW(Addr) init;
76 struct r_debug *r;
78 /* Load the named object. */
79 args->map = new = _dl_map_object (NULL, file, 0, lt_loaded, 0);
80 if (new->l_searchlist)
81 /* It was already open. */
82 return;
84 /* Load that object's dependencies. */
85 _dl_map_object_deps (new, NULL, 0, 0);
87 /* So far, so good. Now check the versions. */
88 (void) _dl_check_all_versions (new, 0);
90 /* Relocate the objects loaded. We do this in reverse order so that copy
91 relocs of earlier objects overwrite the data written by later objects. */
93 l = new;
94 while (l->l_next)
95 l = l->l_next;
96 while (1)
98 if (! l->l_relocated)
100 /* We use an indirect call call for _dl_relocate_object because
101 we must avoid using the PLT in the call. If our PLT entry for
102 _dl_relocate_object hasn't been used yet, then the dynamic
103 linker fixup routine will clobber _dl_global_scope during its
104 work. We must be sure that nothing will require a PLT fixup
105 between when _dl_object_relocation_scope returns and when we
106 enter the dynamic linker's code (_dl_relocate_object). */
107 __typeof (_dl_relocate_object) *reloc = &_dl_relocate_object;
109 /* GCC is very clever. If we wouldn't add some magic it would
110 simply optimize away our nice little variable `reloc' and we
111 would result in a not working binary. So let's swing the
112 magic ward. */
113 asm ("" : "=r" (reloc) : "0" (reloc));
115 #ifdef PIC
116 if (_dl_profile != NULL)
118 /* If this here is the shared object which we want to profile
119 make sure the profile is started. We can find out whether
120 this is necessary or not by observing the `_dl_profile_map'
121 variable. If was NULL but is not NULL afterwars we must
122 start the profiling. */
123 struct link_map *old_profile_map = _dl_profile_map;
125 (*reloc) (l, _dl_object_relocation_scope (l), 1, 1);
127 if (old_profile_map == NULL && _dl_profile_map != NULL)
128 /* We must prepare the profiling. */
129 _dl_start_profile (_dl_profile_map, _dl_profile_output);
131 else
132 #endif
133 (*reloc) (l, _dl_object_relocation_scope (l),
134 (mode & RTLD_BINDING_MASK) == RTLD_LAZY, 0);
136 *_dl_global_scope_end = NULL;
139 if (l == new)
140 break;
141 l = l->l_prev;
144 new->l_global = (mode & RTLD_GLOBAL) ? 1 : 0;
145 if (new->l_global)
147 /* The symbols of the new object and its dependencies are to be
148 introduced into the global scope that will be used to resolve
149 references from other dynamically-loaded objects. */
151 if (_dl_global_scope_alloc == 0)
153 /* This is the first dynamic object given global scope. */
154 _dl_global_scope_alloc = 8;
155 _dl_global_scope = malloc (_dl_global_scope_alloc
156 * sizeof (struct link_map *));
157 if (! _dl_global_scope)
159 _dl_global_scope = _dl_default_scope;
160 nomem:
161 new->l_global = 0;
162 _dl_signal_error (ENOMEM, file, "cannot extend global scope");
164 _dl_global_scope[2] = _dl_default_scope[2];
165 _dl_global_scope[3] = new;
166 _dl_global_scope[4] = NULL;
167 _dl_global_scope[5] = NULL;
168 _dl_global_scope_end = &_dl_global_scope [4];
170 else
172 if (_dl_global_scope_end + 2
173 == _dl_global_scope + _dl_global_scope_alloc)
175 /* Must extend the list. */
176 struct link_map **new = realloc (_dl_global_scope,
177 _dl_global_scope_alloc * 2
178 * sizeof (struct link_map *));
179 if (! new)
180 goto nomem;
181 _dl_global_scope = new;
182 _dl_global_scope_end = new + _dl_global_scope_alloc - 2;
183 _dl_global_scope_alloc *= 2;
186 /* Append the new object and re-terminate the list. */
187 *_dl_global_scope_end++ = new;
188 /* We keep the list double-terminated so the last element
189 can be filled in for symbol lookups. */
190 _dl_global_scope_end[0] = NULL;
191 _dl_global_scope_end[1] = NULL;
196 /* Notify the debugger we have added some objects. We need to call
197 _dl_debug_initialize in a static program in case dynamic linking has
198 not been used before. */
199 r = _dl_debug_initialize (0);
200 r->r_state = RT_ADD;
201 _dl_debug_state ();
203 /* Run the initializer functions of new objects. */
204 while (init = _dl_init_next (new))
205 (*(void (*) (int, char **, char **)) init) (__libc_argc, __libc_argv,
206 __environ);
208 if (_dl_sysdep_start == NULL)
209 /* We must be the static _dl_open in libc.a. A static program that
210 has loaded a dynamic object now has competition. */
211 __libc_multiple_libcs = 1;
215 struct link_map *
216 internal_function
217 _dl_open (const char *file, int mode)
219 struct dl_open_args args;
220 char *errstring;
221 int errcode;
223 /* Make sure we are alone. */
224 __libc_lock_lock (_dl_load_lock);
226 args.file = file;
227 args.mode = mode;
228 args.map = NULL;
229 errcode = _dl_catch_error (&errstring, dl_open_worker, &args);
231 #ifndef MAP_COPY
232 /* We must munmap() the cache file. */
233 _dl_unload_cache ();
234 #endif
236 /* Release the lock. */
237 __libc_lock_unlock (_dl_load_lock);
239 if (errstring)
241 /* Some error occured during loading. */
242 char *local_errstring;
244 /* Reset the global scope. */
245 *_dl_global_scope_end = NULL;
247 /* Remove the object from memory. It may be in an inconsistent
248 state if relocation failed, for example. */
249 if (args.map)
250 _dl_close (args.map);
252 /* Make a local copy of the error string so that we can release the
253 memory allocated for it. */
254 local_errstring = strdupa (errstring);
255 free (errstring);
257 /* Reraise the error. */
258 _dl_signal_error (errcode, NULL, local_errstring);
261 return args.map;