Update.
[glibc.git] / elf / dl-open.c
blobb595f2df73b966c6e94d8137c173b9ccaf608b8b
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 <bits/libc-lock.h>
25 #include <elf/ldsodefs.h>
28 extern ElfW(Addr) _dl_sysdep_start (void **start_argptr,
29 void (*dl_main) (const ElfW(Phdr) *phdr,
30 ElfW(Word) phnum,
31 ElfW(Addr) *user_entry));
32 weak_extern (_dl_sysdep_start)
34 extern int __libc_multiple_libcs; /* Defined in init-first.c. */
36 extern int __libc_argc;
37 extern char **__libc_argv;
39 extern char **__environ;
41 static size_t _dl_global_scope_alloc;
44 /* During the program run we must not modify the global data of
45 loaded shared object simultanously in two threads. Therefore we
46 protect `_dl_open' and `_dl_close' in dl-close.c.
48 This must be a recursive lock since the initializer function of
49 the loaded object might as well require a call to this function.
50 At this time it is not anymore a problem to modify the tables. */
51 __libc_lock_define_initialized_recursive (, _dl_load_lock)
54 /* We must be carefull not to leave us in an inconsistent state. Thus we
55 catch any error and re-raise it after cleaning up. */
57 struct dl_open_args
59 const char *file;
60 int mode;
61 struct link_map *map;
64 static void
65 dl_open_worker (void *a)
67 struct dl_open_args *args = a;
68 const char *file = args->file;
69 int mode = args->mode;
70 struct link_map *new, *l;
71 ElfW(Addr) init;
72 struct r_debug *r;
74 /* Load the named object. */
75 args->map = new = _dl_map_object (NULL, file, 0, lt_loaded, 0);
76 if (new->l_searchlist)
77 /* It was already open. */
78 return;
80 /* Load that object's dependencies. */
81 _dl_map_object_deps (new, NULL, 0, 0);
83 /* So far, so good. Now check the versions. */
84 (void) _dl_check_all_versions (new, 0);
86 /* Relocate the objects loaded. We do this in reverse order so that copy
87 relocs of earlier objects overwrite the data written by later objects. */
89 l = new;
90 while (l->l_next)
91 l = l->l_next;
92 while (1)
94 if (! l->l_relocated)
96 /* We use an indirect call call for _dl_relocate_object because
97 we must avoid using the PLT in the call. If our PLT entry for
98 _dl_relocate_object hasn't been used yet, then the dynamic
99 linker fixup routine will clobber _dl_global_scope during its
100 work. We must be sure that nothing will require a PLT fixup
101 between when _dl_object_relocation_scope returns and when we
102 enter the dynamic linker's code (_dl_relocate_object). */
103 __typeof (_dl_relocate_object) *reloc = &_dl_relocate_object;
105 /* GCC is very clever. If we wouldn't add some magic it would
106 simply optimize away our nice little variable `reloc' and we
107 would result in a not working binary. So let's swing the
108 magic ward. */
109 asm ("" : "=r" (reloc) : "0" (reloc));
111 #ifdef PIC
112 if (_dl_profile != NULL)
114 /* If this here is the shared object which we want to profile
115 make sure the profile is started. We can find out whether
116 this is necessary or not by observing the `_dl_profile_map'
117 variable. If was NULL but is not NULL afterwars we must
118 start the profiling. */
119 struct link_map *old_profile_map = _dl_profile_map;
121 (*reloc) (l, _dl_object_relocation_scope (l), 1, 1);
123 if (old_profile_map == NULL && _dl_profile_map != NULL)
124 /* We must prepare the profiling. */
125 _dl_start_profile (_dl_profile_map, _dl_profile_output);
127 else
128 #endif
129 (*reloc) (l, _dl_object_relocation_scope (l),
130 (mode & RTLD_BINDING_MASK) == RTLD_LAZY, 0);
132 *_dl_global_scope_end = NULL;
135 if (l == new)
136 break;
137 l = l->l_prev;
140 new->l_global = (mode & RTLD_GLOBAL) ? 1 : 0;
141 if (new->l_global)
143 /* The symbols of the new object and its dependencies are to be
144 introduced into the global scope that will be used to resolve
145 references from other dynamically-loaded objects. */
147 if (_dl_global_scope_alloc == 0)
149 /* This is the first dynamic object given global scope. */
150 _dl_global_scope_alloc = 8;
151 _dl_global_scope = malloc (_dl_global_scope_alloc
152 * sizeof (struct link_map *));
153 if (! _dl_global_scope)
155 _dl_global_scope = _dl_default_scope;
156 nomem:
157 new->l_global = 0;
158 _dl_signal_error (ENOMEM, file, "cannot extend global scope");
160 _dl_global_scope[2] = _dl_default_scope[2];
161 _dl_global_scope[3] = new;
162 _dl_global_scope[4] = NULL;
163 _dl_global_scope[5] = NULL;
164 _dl_global_scope_end = &_dl_global_scope [4];
166 else
168 if (_dl_global_scope_end + 2
169 == _dl_global_scope + _dl_global_scope_alloc)
171 /* Must extend the list. */
172 struct link_map **new = realloc (_dl_global_scope,
173 _dl_global_scope_alloc * 2
174 * sizeof (struct link_map *));
175 if (! new)
176 goto nomem;
177 _dl_global_scope = new;
178 _dl_global_scope_end = new + _dl_global_scope_alloc - 2;
179 _dl_global_scope_alloc *= 2;
182 /* Append the new object and re-terminate the list. */
183 *_dl_global_scope_end++ = new;
184 /* We keep the list double-terminated so the last element
185 can be filled in for symbol lookups. */
186 _dl_global_scope_end[0] = NULL;
187 _dl_global_scope_end[1] = NULL;
192 /* Notify the debugger we have added some objects. We need to call
193 _dl_debug_initialize in a static program in case dynamic linking has
194 not been used before. */
195 r = _dl_debug_initialize (0);
196 r->r_state = RT_ADD;
197 _dl_debug_state ();
199 /* Run the initializer functions of new objects. */
200 while (init = _dl_init_next (new))
201 (*(void (*) (int, char **, char **)) init) (__libc_argc, __libc_argv,
202 __environ);
204 if (_dl_sysdep_start == NULL)
205 /* We must be the static _dl_open in libc.a. A static program that
206 has loaded a dynamic object now has competition. */
207 __libc_multiple_libcs = 1;
211 struct link_map *
212 internal_function
213 _dl_open (const char *file, int mode)
215 struct dl_open_args args;
216 char *errstring;
217 int errcode;
219 /* Make sure we are alone. */
220 __libc_lock_lock (_dl_load_lock);
222 args.file = file;
223 args.mode = mode;
224 args.map = NULL;
225 errcode = _dl_catch_error (&errstring, dl_open_worker, &args);
227 /* Release the lock. */
228 __libc_lock_unlock (_dl_load_lock);
230 if (errstring)
232 /* Some error occured during loading. */
233 char *local_errstring;
235 /* Reset the global scope. */
236 *_dl_global_scope_end = NULL;
238 /* Remove the object from memory. It may be in an inconsistent
239 state if relocation failed, for example. */
240 if (args.map)
241 _dl_close (args.map);
243 /* Make a local copy of the error string so that we can release the
244 memory allocated for it. */
245 local_errstring = strdupa (errstring);
246 free (errstring);
248 /* Reraise the error. */
249 _dl_signal_error (errcode, NULL, local_errstring);
252 return args.map;