Update.
[glibc.git] / elf / dl-open.c
blob2b3352b6743df1cef70346f8ce4299867abdd4be
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 <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 /* Undefine the following for debugging. */
46 /* #define SCOPE_DEBUG 1 */
47 #ifdef SCOPE_DEBUG
48 static void show_scope (struct link_map *new);
49 #endif
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;
80 unsigned int global_add;
82 /* Load the named object. */
83 args->map = new = _dl_map_object (NULL, file, 0, lt_loaded, 0);
84 if (new->l_searchlist.r_list)
85 /* It was already open. */
86 return;
88 /* Load that object's dependencies. */
89 global_add = _dl_map_object_deps (new, NULL, 0, 0, mode & RTLD_GLOBAL);
91 /* So far, so good. Now check the versions. */
92 (void) _dl_check_all_versions (new, 0);
94 #ifdef SCOPE_DEBUG
95 show_scope (new);
96 #endif
98 /* Relocate the objects loaded. We do this in reverse order so that copy
99 relocs of earlier objects overwrite the data written by later objects. */
101 l = new;
102 while (l->l_next)
103 l = l->l_next;
104 while (1)
106 if (! l->l_relocated)
108 #ifdef PIC
109 if (_dl_profile != NULL)
111 /* If this here is the shared object which we want to profile
112 make sure the profile is started. We can find out whether
113 this is necessary or not by observing the `_dl_profile_map'
114 variable. If was NULL but is not NULL afterwars we must
115 start the profiling. */
116 struct link_map *old_profile_map = _dl_profile_map;
118 _dl_relocate_object (l, l->l_scope, 1, 1);
120 if (old_profile_map == NULL && _dl_profile_map != NULL)
121 /* We must prepare the profiling. */
122 _dl_start_profile (_dl_profile_map, _dl_profile_output);
124 else
125 #endif
126 _dl_relocate_object (l, l->l_scope,
127 (mode & RTLD_BINDING_MASK) == RTLD_LAZY, 0);
130 if (l == new)
131 break;
132 l = l->l_prev;
135 /* Notify the debugger we have added some objects. We need to call
136 _dl_debug_initialize in a static program in case dynamic linking has
137 not been used before. */
138 r = _dl_debug_initialize (0);
139 r->r_state = RT_ADD;
140 _dl_debug_state ();
142 /* Run the initializer functions of new objects. */
143 while ((init = _dl_init_next (&new->l_searchlist)))
144 (*(void (*) (int, char **, char **)) init) (__libc_argc, __libc_argv,
145 __environ);
147 /* Now we can make the new map available in the global scope. */
148 while (global_add-- > 0)
149 _dl_main_searchlist->r_list[_dl_main_searchlist->r_nlist++]->l_global = 1;
151 if (_dl_sysdep_start == NULL)
152 /* We must be the static _dl_open in libc.a. A static program that
153 has loaded a dynamic object now has competition. */
154 __libc_multiple_libcs = 1;
158 struct link_map *
159 internal_function
160 _dl_open (const char *file, int mode)
162 struct dl_open_args args;
163 char *errstring;
164 int errcode;
166 if ((mode & RTLD_BINDING_MASK) == 0)
167 /* One of the flags must be set. */
168 _dl_signal_error (EINVAL, file, _("invalid mode for dlopen()"));
170 /* Make sure we are alone. */
171 __libc_lock_lock (_dl_load_lock);
173 args.file = file;
174 args.mode = mode;
175 args.map = NULL;
176 errcode = _dl_catch_error (&errstring, dl_open_worker, &args);
178 #ifndef MAP_COPY
179 /* We must munmap() the cache file. */
180 _dl_unload_cache ();
181 #endif
183 /* Release the lock. */
184 __libc_lock_unlock (_dl_load_lock);
186 if (errstring)
188 /* Some error occured during loading. */
189 char *local_errstring;
191 /* Remove the object from memory. It may be in an inconsistent
192 state if relocation failed, for example. */
193 if (args.map)
194 _dl_close (args.map);
196 /* Make a local copy of the error string so that we can release the
197 memory allocated for it. */
198 local_errstring = strdupa (errstring);
199 free (errstring);
201 /* Reraise the error. */
202 _dl_signal_error (errcode, NULL, local_errstring);
205 return args.map;
209 #ifdef SCOPE_DEBUG
210 #include <unistd.h>
212 static void
213 show_scope (struct link_map *new)
215 int scope_cnt;
217 for (scope_cnt = 0; new->l_scope[scope_cnt] != NULL; ++scope_cnt)
219 char numbuf[2];
220 unsigned int cnt;
222 numbuf[0] = '0' + scope_cnt;
223 numbuf[1] = '\0';
224 _dl_sysdep_message ("scope ", numbuf, ":", NULL);
226 for (cnt = 0; cnt < new->l_scope[scope_cnt]->r_nlist; ++cnt)
227 if (*new->l_scope[scope_cnt]->r_list[cnt]->l_name)
228 _dl_sysdep_message (" ",
229 new->l_scope[scope_cnt]->r_list[cnt]->l_name,
230 NULL);
231 else
232 _dl_sysdep_message (" <main>", NULL);
234 _dl_sysdep_message ("\n", NULL);
237 #endif