Update.
[glibc.git] / elf / dl-open.c
blob83e6424c843307c763d9ef0861a7f830f029e1a3
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 /* This is zero at program start to signal that the global scope map is
46 allocated by rtld. Later it keeps the size of the map. It might be
47 reset if in _dl_close if the last global object is removed. */
48 size_t _dl_global_scope_alloc;
51 /* During the program run we must not modify the global data of
52 loaded shared object simultanously in two threads. Therefore we
53 protect `_dl_open' and `_dl_close' in dl-close.c.
55 This must be a recursive lock since the initializer function of
56 the loaded object might as well require a call to this function.
57 At this time it is not anymore a problem to modify the tables. */
58 __libc_lock_define_initialized_recursive (, _dl_load_lock)
61 /* We must be carefull not to leave us in an inconsistent state. Thus we
62 catch any error and re-raise it after cleaning up. */
64 struct dl_open_args
66 const char *file;
67 int mode;
68 struct link_map *map;
71 static void
72 dl_open_worker (void *a)
74 struct dl_open_args *args = a;
75 const char *file = args->file;
76 int mode = args->mode;
77 struct link_map *new, *l;
78 ElfW(Addr) init;
79 struct r_debug *r;
81 /* Load the named object. */
82 args->map = new = _dl_map_object (NULL, file, 0, lt_loaded, 0);
83 if (new->l_searchlist.r_list)
84 /* It was already open. */
85 return;
87 /* Load that object's dependencies. */
88 _dl_map_object_deps (new, NULL, 0, 0);
90 /* So far, so good. Now check the versions. */
91 (void) _dl_check_all_versions (new, 0);
93 /* Relocate the objects loaded. We do this in reverse order so that copy
94 relocs of earlier objects overwrite the data written by later objects. */
96 l = new;
97 while (l->l_next)
98 l = l->l_next;
99 while (1)
101 if (! l->l_relocated)
103 #ifdef PIC
104 if (_dl_profile != NULL)
106 /* If this here is the shared object which we want to profile
107 make sure the profile is started. We can find out whether
108 this is necessary or not by observing the `_dl_profile_map'
109 variable. If was NULL but is not NULL afterwars we must
110 start the profiling. */
111 struct link_map *old_profile_map = _dl_profile_map;
113 _dl_relocate_object (l, l->l_scope, 1, 1);
115 if (old_profile_map == NULL && _dl_profile_map != NULL)
116 /* We must prepare the profiling. */
117 _dl_start_profile (_dl_profile_map, _dl_profile_output);
119 else
120 #endif
121 _dl_relocate_object (l, l->l_scope,
122 (mode & RTLD_BINDING_MASK) == RTLD_LAZY, 0);
125 if (l == new)
126 break;
127 l = l->l_prev;
130 new->l_global = (mode & RTLD_GLOBAL) ? 1 : 0;
131 if (new->l_global)
133 struct link_map **new_global;
135 /* The symbols of the new object and its dependencies are to be
136 introduced into the global scope that will be used to resolve
137 references from other dynamically-loaded objects.
139 The global scope is the searchlist in the main link map. We
140 extend this list if necessary. There is one problem though:
141 since this structure was allocated very early (before the libc
142 is loaded) the memory it uses is allocated by the malloc()-stub
143 in the ld.so. When we come here these functions are not used
144 anymore. Instead the malloc() implementation of the libc is
145 used. But this means the block from the main map cannot be used
146 in an realloc() call. Therefore we allocate a completely new
147 array the first time we have to add something to the locale scope. */
148 if (_dl_global_scope_alloc == 0)
150 /* This is the first dynamic object given global scope. */
151 _dl_global_scope_alloc = _dl_main_searchlist->r_nlist + 8;
152 new_global = (struct link_map **)
153 malloc (_dl_global_scope_alloc * sizeof (struct link_map *));
154 if (new_global == NULL)
156 _dl_global_scope_alloc = 0;
157 nomem:
158 new->l_global = 0;
159 _dl_signal_error (ENOMEM, file, "cannot extend global scope");
162 /* Copy over the old entries. */
163 memcpy (new_global, _dl_main_searchlist->r_list,
164 (_dl_main_searchlist->r_nlist * sizeof (struct link_map *)));
166 _dl_main_searchlist->r_list = new_global;
168 else if (_dl_main_searchlist->r_nlist == _dl_global_scope_alloc)
170 /* We have to extend the existing array of link maps in the
171 main map. */
172 new_global = (struct link_map **)
173 realloc (_dl_main_searchlist->r_list,
174 ((_dl_global_scope_alloc + 8)
175 * sizeof (struct link_map *)));
176 if (new_global == NULL)
177 goto nomem;
179 _dl_global_scope_alloc += 8;
180 _dl_main_searchlist->r_list = new_global;
183 /* Now add the new entry. */
184 _dl_main_searchlist->r_list[_dl_main_searchlist->r_nlist] = new;
186 /* XXX Do we have to add something to r_dupsearchlist??? --drepper */
190 /* Notify the debugger we have added some objects. We need to call
191 _dl_debug_initialize in a static program in case dynamic linking has
192 not been used before. */
193 r = _dl_debug_initialize (0);
194 r->r_state = RT_ADD;
195 _dl_debug_state ();
197 /* Run the initializer functions of new objects. */
198 while ((init = _dl_init_next (&new->l_searchlist)))
199 (*(void (*) (int, char **, char **)) init) (__libc_argc, __libc_argv,
200 __environ);
202 if (new->l_global)
203 /* Now we can make the new map available in the global scope. */
204 ++_dl_main_searchlist->r_nlist;
206 if (_dl_sysdep_start == NULL)
207 /* We must be the static _dl_open in libc.a. A static program that
208 has loaded a dynamic object now has competition. */
209 __libc_multiple_libcs = 1;
213 struct link_map *
214 internal_function
215 _dl_open (const char *file, int mode)
217 struct dl_open_args args;
218 char *errstring;
219 int errcode;
221 /* Make sure we are alone. */
222 __libc_lock_lock (_dl_load_lock);
224 args.file = file;
225 args.mode = mode;
226 args.map = NULL;
227 errcode = _dl_catch_error (&errstring, dl_open_worker, &args);
229 #ifndef MAP_COPY
230 /* We must munmap() the cache file. */
231 _dl_unload_cache ();
232 #endif
234 /* Release the lock. */
235 __libc_lock_unlock (_dl_load_lock);
237 if (errstring)
239 /* Some error occured during loading. */
240 char *local_errstring;
242 /* Remove the object from memory. It may be in an inconsistent
243 state if relocation failed, for example. */
244 if (args.map)
245 _dl_close (args.map);
247 /* Make a local copy of the error string so that we can release the
248 memory allocated for it. */
249 local_errstring = strdupa (errstring);
250 free (errstring);
252 /* Reraise the error. */
253 _dl_signal_error (errcode, NULL, local_errstring);
256 return args.map;