Update.
[glibc.git] / elf / dl-open.c
blob8cf1084a94a779a2eda2490faee51eeae57c4393
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;
46 /* During the program run we must not modify the global data of
47 loaded shared object simultanously in two threads. Therefore we
48 protect `_dl_open' and `_dl_close' in dl-close.c.
50 This must be a recursive lock since the initializer function of
51 the loaded object might as well require a call to this function.
52 At this time it is not anymore a problem to modify the tables. */
53 __libc_lock_define_initialized_recursive (, _dl_load_lock)
56 /* We must be carefull not to leave us in an inconsistent state. Thus we
57 catch any error and re-raise it after cleaning up. */
59 struct dl_open_args
61 const char *file;
62 int mode;
63 struct link_map *map;
66 static void
67 dl_open_worker (void *a)
69 struct dl_open_args *args = a;
70 const char *file = args->file;
71 int mode = args->mode;
72 struct link_map *new, *l;
73 ElfW(Addr) init;
74 struct r_debug *r;
75 unsigned int global_add;
77 /* Load the named object. */
78 args->map = new = _dl_map_object (NULL, file, 0, lt_loaded, 0);
79 if (new->l_searchlist.r_list)
80 /* It was already open. */
81 return;
83 /* Load that object's dependencies. */
84 global_add = _dl_map_object_deps (new, NULL, 0, 0, mode & RTLD_GLOBAL);
86 /* So far, so good. Now check the versions. */
87 (void) _dl_check_all_versions (new, 0);
89 /* Relocate the objects loaded. We do this in reverse order so that copy
90 relocs of earlier objects overwrite the data written by later objects. */
92 l = new;
93 while (l->l_next)
94 l = l->l_next;
95 while (1)
97 if (! l->l_relocated)
99 #ifdef PIC
100 if (_dl_profile != NULL)
102 /* If this here is the shared object which we want to profile
103 make sure the profile is started. We can find out whether
104 this is necessary or not by observing the `_dl_profile_map'
105 variable. If was NULL but is not NULL afterwars we must
106 start the profiling. */
107 struct link_map *old_profile_map = _dl_profile_map;
109 _dl_relocate_object (l, l->l_scope, 1, 1);
111 if (old_profile_map == NULL && _dl_profile_map != NULL)
112 /* We must prepare the profiling. */
113 _dl_start_profile (_dl_profile_map, _dl_profile_output);
115 else
116 #endif
117 _dl_relocate_object (l, l->l_scope,
118 (mode & RTLD_BINDING_MASK) == RTLD_LAZY, 0);
121 if (l == new)
122 break;
123 l = l->l_prev;
126 /* Notify the debugger we have added some objects. We need to call
127 _dl_debug_initialize in a static program in case dynamic linking has
128 not been used before. */
129 r = _dl_debug_initialize (0);
130 r->r_state = RT_ADD;
131 _dl_debug_state ();
133 /* Run the initializer functions of new objects. */
134 while ((init = _dl_init_next (&new->l_searchlist)))
135 (*(void (*) (int, char **, char **)) init) (__libc_argc, __libc_argv,
136 __environ);
138 if (new->l_global)
139 /* Now we can make the new map available in the global scope. */
140 _dl_main_searchlist->r_nlist += global_add;
142 if (_dl_sysdep_start == NULL)
143 /* We must be the static _dl_open in libc.a. A static program that
144 has loaded a dynamic object now has competition. */
145 __libc_multiple_libcs = 1;
149 struct link_map *
150 internal_function
151 _dl_open (const char *file, int mode)
153 struct dl_open_args args;
154 char *errstring;
155 int errcode;
157 if ((mode & RTLD_BINDING_MASK) == 0)
158 /* One of the flags must be set. */
159 _dl_signal_error (EINVAL, file, _("invalid mode for dlopen()"));
161 /* Make sure we are alone. */
162 __libc_lock_lock (_dl_load_lock);
164 args.file = file;
165 args.mode = mode;
166 args.map = NULL;
167 errcode = _dl_catch_error (&errstring, dl_open_worker, &args);
169 #ifndef MAP_COPY
170 /* We must munmap() the cache file. */
171 _dl_unload_cache ();
172 #endif
174 /* Release the lock. */
175 __libc_lock_unlock (_dl_load_lock);
177 if (errstring)
179 /* Some error occured during loading. */
180 char *local_errstring;
182 /* Remove the object from memory. It may be in an inconsistent
183 state if relocation failed, for example. */
184 if (args.map)
185 _dl_close (args.map);
187 /* Make a local copy of the error string so that we can release the
188 memory allocated for it. */
189 local_errstring = strdupa (errstring);
190 free (errstring);
192 /* Reraise the error. */
193 _dl_signal_error (errcode, NULL, local_errstring);
196 return args.map;