Update.
[platform/upstream/glibc.git] / elf / dl-open.c
1 /* Load a shared object at runtime, relocate it, and run its initializer.
2    Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
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.
9
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.
14
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.  */
19
20 #include <assert.h>
21 #include <dlfcn.h>
22 #include <errno.h>
23 #include <libintl.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/mman.h>           /* Check whether MAP_COPY is defined.  */
28 #include <sys/param.h>
29 #include <bits/libc-lock.h>
30 #include <ldsodefs.h>
31
32 #include <dl-dst.h>
33
34
35 extern ElfW(Addr) _dl_sysdep_start (void **start_argptr,
36                                     void (*dl_main) (const ElfW(Phdr) *phdr,
37                                                      ElfW(Word) phnum,
38                                                      ElfW(Addr) *user_entry));
39 weak_extern (_dl_sysdep_start)
40
41 /* This function is used to unload the cache file if necessary.  */
42 extern void _dl_unload_cache (void);
43
44 extern int __libc_multiple_libcs;       /* Defined in init-first.c.  */
45
46 extern int __libc_argc;
47 extern char **__libc_argv;
48
49 extern char **__environ;
50
51 extern int _dl_lazy;                    /* Do we do lazy relocations?  */
52
53 /* Undefine the following for debugging.  */
54 /* #define SCOPE_DEBUG 1 */
55 #ifdef SCOPE_DEBUG
56 static void show_scope (struct link_map *new);
57 #endif
58
59 /* During the program run we must not modify the global data of
60    loaded shared object simultanously in two threads.  Therefore we
61    protect `_dl_open' and `_dl_close' in dl-close.c.
62
63    This must be a recursive lock since the initializer function of
64    the loaded object might as well require a call to this function.
65    At this time it is not anymore a problem to modify the tables.  */
66 __libc_lock_define (extern, _dl_load_lock)
67
68 extern size_t _dl_platformlen;
69
70 /* We must be carefull not to leave us in an inconsistent state.  Thus we
71    catch any error and re-raise it after cleaning up.  */
72
73 struct dl_open_args
74 {
75   const char *file;
76   int mode;
77   const void *caller;
78   struct link_map *map;
79 };
80
81 static void
82 dl_open_worker (void *a)
83 {
84   struct dl_open_args *args = a;
85   const char *file = args->file;
86   int mode = args->mode;
87   struct link_map *new, *l;
88   const char *dst;
89   int lazy;
90
91   /* Maybe we have to expand a DST.  */
92   dst = strchr (file, '$');
93   if (dst != NULL)
94     {
95       const void *caller = args->caller;
96       size_t len = strlen (file);
97       size_t required;
98       struct link_map *call_map;
99       char *new_file;
100
101       /* DSTs must not appear in SUID/SGID programs.  */
102       if (__libc_enable_secure)
103         /* This is an error.  */
104         _dl_signal_error (0, "dlopen",
105                           N_("DST not allowed in SUID/SGID programs"));
106
107       /* We have to find out from which object the caller is calling.
108          Find the highest-addressed object that ADDRESS is not below.  */
109       call_map = NULL;
110       for (l = _dl_loaded; l; l = l->l_next)
111         if (l->l_addr != 0 /* Make sure we do not currently set this map up
112                               in this moment.  */
113             && caller >= (const void *) l->l_map_start
114             && caller < (const void *) l->l_map_end)
115           {
116             /* There must be exactly one DSO for the range of the virtual
117                memory.  Otherwise something is really broken.  */
118             call_map = l;
119             break;
120           }
121
122       if (call_map == NULL)
123         /* In this case we assume this is the main application.  */
124         call_map = _dl_loaded;
125
126       /* Determine how much space we need.  We have to allocate the
127          memory locally.  */
128       required = DL_DST_REQUIRED (call_map, file, len, _dl_dst_count (dst, 0));
129
130       /* Get space for the new file name.  */
131       new_file = (char *) alloca (required + 1);
132
133       /* Generate the new file name.  */
134       DL_DST_SUBSTITUTE (call_map, file, new_file, 0);
135
136       /* If the substitution failed don't try to load.  */
137       if (*new_file == '\0')
138         _dl_signal_error (0, "dlopen",
139                           N_("empty dynamic string token substitution"));
140
141       /* Now we have a new file name.  */
142       file = new_file;
143     }
144
145   /* Load the named object.  */
146   args->map = new = _dl_map_object (NULL, file, 0, lt_loaded, 0);
147   if (new->l_searchlist.r_list)
148     /* It was already open.  */
149     return;
150
151   /* Load that object's dependencies.  */
152   _dl_map_object_deps (new, NULL, 0, 0);
153
154   /* So far, so good.  Now check the versions.  */
155   (void) _dl_check_all_versions (new, 0, 0);
156
157 #ifdef SCOPE_DEBUG
158   show_scope (new);
159 #endif
160
161   /* Only do lazy relocation if `LD_BIND_NOW' is not set.  */
162   lazy = (mode & RTLD_BINDING_MASK) == RTLD_LAZY && _dl_lazy;
163
164   /* Relocate the objects loaded.  We do this in reverse order so that copy
165      relocs of earlier objects overwrite the data written by later objects.  */
166
167   l = new;
168   while (l->l_next)
169     l = l->l_next;
170   while (1)
171     {
172       if (! l->l_relocated)
173         {
174 #ifdef SHARED
175           if (_dl_profile != NULL)
176             {
177               /* If this here is the shared object which we want to profile
178                  make sure the profile is started.  We can find out whether
179                  this is necessary or not by observing the `_dl_profile_map'
180                  variable.  If was NULL but is not NULL afterwars we must
181                  start the profiling.  */
182               struct link_map *old_profile_map = _dl_profile_map;
183
184               _dl_relocate_object (l, l->l_scope, 1, 1);
185
186               if (old_profile_map == NULL && _dl_profile_map != NULL)
187                 /* We must prepare the profiling.  */
188                 _dl_start_profile (_dl_profile_map, _dl_profile_output);
189             }
190           else
191 #endif
192             _dl_relocate_object (l, l->l_scope, lazy, 0);
193         }
194
195       if (l == new)
196         break;
197       l = l->l_prev;
198     }
199
200   /* Run the initializer functions of new objects.  */
201   _dl_init (new, __libc_argc, __libc_argv, __environ);
202
203   /* Now we can make the new map available in the global scope.  */
204   if (mode & RTLD_GLOBAL)
205     {
206       struct link_map **new_global;
207       unsigned int to_add = 0;
208       unsigned int cnt;
209
210       /* Count the objects we have to put in the global scope.  */
211       for (cnt = 0; cnt < new->l_searchlist.r_nlist; ++cnt)
212         if (new->l_searchlist.r_list[cnt]->l_global == 0)
213           ++to_add;
214
215       /* The symbols of the new objects and its dependencies are to be
216          introduced into the global scope that will be used to resolve
217          references from other dynamically-loaded objects.
218
219          The global scope is the searchlist in the main link map.  We
220          extend this list if necessary.  There is one problem though:
221          since this structure was allocated very early (before the libc
222          is loaded) the memory it uses is allocated by the malloc()-stub
223          in the ld.so.  When we come here these functions are not used
224          anymore.  Instead the malloc() implementation of the libc is
225          used.  But this means the block from the main map cannot be used
226          in an realloc() call.  Therefore we allocate a completely new
227          array the first time we have to add something to the locale scope.  */
228
229       if (_dl_global_scope_alloc == 0)
230         {
231           /* This is the first dynamic object given global scope.  */
232           _dl_global_scope_alloc = _dl_main_searchlist->r_nlist + to_add + 8;
233           new_global = (struct link_map **)
234             malloc (_dl_global_scope_alloc * sizeof (struct link_map *));
235           if (new_global == NULL)
236             {
237               _dl_global_scope_alloc = 0;
238             nomem:
239               _dl_signal_error (ENOMEM, new->l_libname->name,
240                                 N_("cannot extend global scope"));
241               return;
242             }
243
244           /* Copy over the old entries.  */
245           memcpy (new_global, _dl_main_searchlist->r_list,
246                   (_dl_main_searchlist->r_nlist * sizeof (struct link_map *)));
247
248           _dl_main_searchlist->r_list = new_global;
249         }
250       else if (_dl_main_searchlist->r_nlist + to_add > _dl_global_scope_alloc)
251         {
252           /* We have to extend the existing array of link maps in the
253              main map.  */
254           new_global = (struct link_map **)
255             realloc (_dl_main_searchlist->r_list,
256                      ((_dl_global_scope_alloc + to_add + 8)
257                       * sizeof (struct link_map *)));
258           if (new_global == NULL)
259             goto nomem;
260
261           _dl_global_scope_alloc += to_add + 8;
262           _dl_main_searchlist->r_list = new_global;
263         }
264
265       /* Now add the new entries.  */
266       for (cnt = 0; cnt < new->l_searchlist.r_nlist; ++cnt)
267         {
268           struct link_map *map = new->l_searchlist.r_list[cnt];
269
270           if (map->l_global == 0)
271             {
272               map->l_global = 1;
273               _dl_main_searchlist->r_list[_dl_main_searchlist->r_nlist] = map;
274               ++_dl_main_searchlist->r_nlist;
275             }
276         }
277
278       /* XXX Do we have to add something to r_dupsearchlist???  --drepper */
279     }
280
281   if (_dl_sysdep_start == NULL)
282     /* We must be the static _dl_open in libc.a.  A static program that
283        has loaded a dynamic object now has competition.  */
284     __libc_multiple_libcs = 1;
285 }
286
287
288 void *
289 internal_function
290 _dl_open (const char *file, int mode, const void *caller)
291 {
292   struct dl_open_args args;
293   const char *objname;
294   const char *errstring;
295   int errcode;
296
297   if ((mode & RTLD_BINDING_MASK) == 0)
298     /* One of the flags must be set.  */
299     _dl_signal_error (EINVAL, file, N_("invalid mode for dlopen()"));
300
301   /* Make sure we are alone.  */
302   __libc_lock_lock (_dl_load_lock);
303
304   args.file = file;
305   args.mode = mode;
306   args.caller = caller;
307   args.map = NULL;
308   errcode = _dl_catch_error (&objname, &errstring, dl_open_worker, &args);
309
310 #ifndef MAP_COPY
311   /* We must munmap() the cache file.  */
312   _dl_unload_cache ();
313 #endif
314
315   /* Release the lock.  */
316   __libc_lock_unlock (_dl_load_lock);
317
318   if (errstring)
319     {
320       /* Some error occurred during loading.  */
321       char *local_errstring;
322
323       /* Remove the object from memory.  It may be in an inconsistent
324          state if relocation failed, for example.  */
325       if (args.map)
326         _dl_close (args.map);
327
328       /* Make a local copy of the error string so that we can release the
329          memory allocated for it.  */
330       local_errstring = strdupa (errstring);
331       free ((char *) errstring);
332
333       /* Reraise the error.  */
334       _dl_signal_error (errcode, objname, local_errstring);
335     }
336
337   return args.map;
338 }
339
340
341 #ifdef SCOPE_DEBUG
342 #include <unistd.h>
343
344 static void
345 show_scope (struct link_map *new)
346 {
347   int scope_cnt;
348
349   for (scope_cnt = 0; new->l_scope[scope_cnt] != NULL; ++scope_cnt)
350     {
351       char numbuf[2];
352       unsigned int cnt;
353
354       numbuf[0] = '0' + scope_cnt;
355       numbuf[1] = '\0';
356       _dl_sysdep_message ("scope ", numbuf, ":", NULL);
357
358       for (cnt = 0; cnt < new->l_scope[scope_cnt]->r_nlist; ++cnt)
359         if (*new->l_scope[scope_cnt]->r_list[cnt]->l_name)
360           _dl_sysdep_message (" ",
361                               new->l_scope[scope_cnt]->r_list[cnt]->l_name,
362                               NULL);
363         else
364           _dl_sysdep_message (" <main>", NULL);
365
366       _dl_sysdep_message ("\n", NULL);
367     }
368 }
369 #endif