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