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