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