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-2001, 2002 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 <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/mman.h>           /* Check whether MAP_COPY is defined.  */
29 #include <sys/param.h>
30 #include <bits/libc-lock.h>
31 #include <ldsodefs.h>
32 #include <bp-sym.h>
33
34 #include <dl-dst.h>
35
36
37 #ifdef SHARED
38 /* Giving this initialized value preallocates some surplus bytes in the
39    static TLS area, see __libc_setup_tls (libc-tls.c).  */
40 size_t _dl_tls_static_size = 576;
41 #endif
42
43 extern ElfW(Addr) _dl_sysdep_start (void **start_argptr,
44                                     void (*dl_main) (const ElfW(Phdr) *phdr,
45                                                      ElfW(Word) phnum,
46                                                      ElfW(Addr) *user_entry));
47 weak_extern (BP_SYM (_dl_sysdep_start))
48
49 extern int __libc_multiple_libcs;       /* Defined in init-first.c.  */
50
51 extern int __libc_argc attribute_hidden;
52 extern char **__libc_argv attribute_hidden;
53
54 extern char **__environ;
55
56 /* Undefine the following for debugging.  */
57 /* #define SCOPE_DEBUG 1 */
58 #ifdef SCOPE_DEBUG
59 static void show_scope (struct link_map *new);
60 #endif
61
62 /* We must be carefull not to leave us in an inconsistent state.  Thus we
63    catch any error and re-raise it after cleaning up.  */
64
65 struct dl_open_args
66 {
67   const char *file;
68   int mode;
69   const void *caller;
70   struct link_map *map;
71 };
72
73
74 static int
75 add_to_global (struct link_map *new)
76 {
77   struct link_map **new_global;
78   unsigned int to_add = 0;
79   unsigned int cnt;
80
81   /* Count the objects we have to put in the global scope.  */
82   for (cnt = 0; cnt < new->l_searchlist.r_nlist; ++cnt)
83     if (new->l_searchlist.r_list[cnt]->l_global == 0)
84       ++to_add;
85
86   /* The symbols of the new objects and its dependencies are to be
87      introduced into the global scope that will be used to resolve
88      references from other dynamically-loaded objects.
89
90      The global scope is the searchlist in the main link map.  We
91      extend this list if necessary.  There is one problem though:
92      since this structure was allocated very early (before the libc
93      is loaded) the memory it uses is allocated by the malloc()-stub
94      in the ld.so.  When we come here these functions are not used
95      anymore.  Instead the malloc() implementation of the libc is
96      used.  But this means the block from the main map cannot be used
97      in an realloc() call.  Therefore we allocate a completely new
98      array the first time we have to add something to the locale scope.  */
99
100   if (GL(dl_global_scope_alloc) == 0)
101     {
102       /* This is the first dynamic object given global scope.  */
103       GL(dl_global_scope_alloc) = GL(dl_main_searchlist)->r_nlist + to_add + 8;
104       new_global = (struct link_map **)
105         malloc (GL(dl_global_scope_alloc) * sizeof (struct link_map *));
106       if (new_global == NULL)
107         {
108           GL(dl_global_scope_alloc) = 0;
109         nomem:
110           _dl_signal_error (ENOMEM, new->l_libname->name, NULL,
111                             N_("cannot extend global scope"));
112           return 1;
113         }
114
115       /* Copy over the old entries.  */
116       memcpy (new_global, GL(dl_main_searchlist)->r_list,
117               (GL(dl_main_searchlist)->r_nlist * sizeof (struct link_map *)));
118
119       GL(dl_main_searchlist)->r_list = new_global;
120     }
121   else if (GL(dl_main_searchlist)->r_nlist + to_add
122            > GL(dl_global_scope_alloc))
123     {
124       /* We have to extend the existing array of link maps in the
125          main map.  */
126       new_global = (struct link_map **)
127         realloc (GL(dl_main_searchlist)->r_list,
128                  ((GL(dl_global_scope_alloc) + to_add + 8)
129                   * sizeof (struct link_map *)));
130       if (new_global == NULL)
131         goto nomem;
132
133       GL(dl_global_scope_alloc) += to_add + 8;
134       GL(dl_main_searchlist)->r_list = new_global;
135     }
136
137   /* Now add the new entries.  */
138   for (cnt = 0; cnt < new->l_searchlist.r_nlist; ++cnt)
139     {
140       struct link_map *map = new->l_searchlist.r_list[cnt];
141
142       if (map->l_global == 0)
143         {
144           map->l_global = 1;
145           GL(dl_main_searchlist)->r_list[GL(dl_main_searchlist)->r_nlist]
146             = map;
147           ++GL(dl_main_searchlist)->r_nlist;
148         }
149     }
150
151   return 0;
152 }
153
154
155 static void
156 dl_open_worker (void *a)
157 {
158   struct dl_open_args *args = a;
159   const char *file = args->file;
160   int mode = args->mode;
161   struct link_map *new, *l;
162   const char *dst;
163   int lazy;
164   unsigned int i;
165 #ifdef USE_TLS
166   bool any_tls;
167 #endif
168
169   /* Maybe we have to expand a DST.  */
170   dst = strchr (file, '$');
171   if (__builtin_expect (dst != NULL, 0))
172     {
173       const void *caller = args->caller;
174       size_t len = strlen (file);
175       size_t required;
176       struct link_map *call_map;
177       char *new_file;
178
179       /* DSTs must not appear in SUID/SGID programs.  */
180       if (__libc_enable_secure)
181         /* This is an error.  */
182         _dl_signal_error (0, "dlopen", NULL,
183                           N_("DST not allowed in SUID/SGID programs"));
184
185       /* We have to find out from which object the caller is calling.  */
186       call_map = NULL;
187       for (l = GL(dl_loaded); l; l = l->l_next)
188         if (caller >= (const void *) l->l_map_start
189             && caller < (const void *) l->l_map_end)
190           {
191             /* There must be exactly one DSO for the range of the virtual
192                memory.  Otherwise something is really broken.  */
193             call_map = l;
194             break;
195           }
196
197       if (call_map == NULL)
198         /* In this case we assume this is the main application.  */
199         call_map = GL(dl_loaded);
200
201       /* Determine how much space we need.  We have to allocate the
202          memory locally.  */
203       required = DL_DST_REQUIRED (call_map, file, len, _dl_dst_count (dst, 0));
204
205       /* Get space for the new file name.  */
206       new_file = (char *) alloca (required + 1);
207
208       /* Generate the new file name.  */
209       _dl_dst_substitute (call_map, file, new_file, 0);
210
211       /* If the substitution failed don't try to load.  */
212       if (*new_file == '\0')
213         _dl_signal_error (0, "dlopen", NULL,
214                           N_("empty dynamic string token substitution"));
215
216       /* Now we have a new file name.  */
217       file = new_file;
218     }
219
220   /* Load the named object.  */
221   args->map = new = _dl_map_object (NULL, file, 0, lt_loaded, 0, mode);
222
223   /* If the pointer returned is NULL this means the RTLD_NOLOAD flag is
224      set and the object is not already loaded.  */
225   if (new == NULL)
226     {
227       assert (mode & RTLD_NOLOAD);
228       return;
229     }
230
231   if (__builtin_expect (mode & __RTLD_SPROF, 0))
232     /* This happens only if we load a DSO for 'sprof'.  */
233     return;
234
235   /* It was already open.  */
236   if (new->l_searchlist.r_list != NULL)
237     {
238       /* Let the user know about the opencount.  */
239       if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_FILES, 0))
240         _dl_debug_printf ("opening file=%s; opencount == %u\n\n",
241                           new->l_name, new->l_opencount);
242
243       /* If the user requested the object to be in the global namespace
244          but it is not so far, add it now.  */
245       if ((mode & RTLD_GLOBAL) && new->l_global == 0)
246         (void) add_to_global (new);
247
248       /* Increment just the reference counter of the object.  */
249       ++new->l_opencount;
250
251       return;
252     }
253
254   /* Load that object's dependencies.  */
255   _dl_map_object_deps (new, NULL, 0, 0, mode & __RTLD_DLOPEN);
256
257   /* So far, so good.  Now check the versions.  */
258   for (i = 0; i < new->l_searchlist.r_nlist; ++i)
259     if (new->l_searchlist.r_list[i]->l_versions == NULL)
260       (void) _dl_check_map_versions (new->l_searchlist.r_list[i], 0, 0);
261
262 #ifdef SCOPE_DEBUG
263   show_scope (new);
264 #endif
265
266   /* Only do lazy relocation if `LD_BIND_NOW' is not set.  */
267   lazy = (mode & RTLD_BINDING_MASK) == RTLD_LAZY && GL(dl_lazy);
268
269   /* Relocate the objects loaded.  We do this in reverse order so that copy
270      relocs of earlier objects overwrite the data written by later objects.  */
271
272   l = new;
273   while (l->l_next)
274     l = l->l_next;
275   while (1)
276     {
277       if (! l->l_relocated)
278         {
279 #ifdef SHARED
280           if (GL(dl_profile) != NULL)
281             {
282               /* If this here is the shared object which we want to profile
283                  make sure the profile is started.  We can find out whether
284                  this is necessary or not by observing the `_dl_profile_map'
285                  variable.  If was NULL but is not NULL afterwars we must
286                  start the profiling.  */
287               struct link_map *old_profile_map = GL(dl_profile_map);
288
289               _dl_relocate_object (l, l->l_scope, 1, 1);
290
291               if (old_profile_map == NULL && GL(dl_profile_map) != NULL)
292                 /* We must prepare the profiling.  */
293                 _dl_start_profile (GL(dl_profile_map), GL(dl_profile_output));
294             }
295           else
296 #endif
297             _dl_relocate_object (l, l->l_scope, lazy, 0);
298         }
299
300       if (l == new)
301         break;
302       l = l->l_prev;
303     }
304
305 #ifdef USE_TLS
306   /* We normally don't bump the TLS generation counter.  There must be
307      actually a need to do this.  */
308   any_tls = false;
309 #endif
310
311   /* Increment the open count for all dependencies.  If the file is
312      not loaded as a dependency here add the search list of the newly
313      loaded object to the scope.  */
314   for (i = 0; i < new->l_searchlist.r_nlist; ++i)
315     if (++new->l_searchlist.r_list[i]->l_opencount > 1
316         && new->l_searchlist.r_list[i]->l_type == lt_loaded)
317       {
318         struct link_map *imap = new->l_searchlist.r_list[i];
319         struct r_scope_elem **runp = imap->l_scope;
320         size_t cnt = 0;
321
322         while (*runp != NULL)
323           {
324             /* This can happen if imap was just loaded, but during
325                relocation had l_opencount bumped because of relocation
326                dependency.  Avoid duplicates in l_scope.  */
327             if (__builtin_expect (*runp == &new->l_searchlist, 0))
328               break;
329
330             ++cnt;
331             ++runp;
332           }
333
334         if (*runp != NULL)
335           /* Avoid duplicates.  */
336           continue;
337
338         if (__builtin_expect (cnt + 1 >= imap->l_scope_max, 0))
339           {
340             /* The 'r_scope' array is too small.  Allocate a new one
341                dynamically.  */
342             struct r_scope_elem **newp;
343             size_t new_size = imap->l_scope_max * 2;
344
345             if (imap->l_scope == imap->l_scope_mem)
346               {
347                 newp = (struct r_scope_elem **)
348                   malloc (new_size * sizeof (struct r_scope_elem *));
349                 if (newp == NULL)
350                   _dl_signal_error (ENOMEM, "dlopen", NULL,
351                                     N_("cannot create scope list"));
352                 imap->l_scope = memcpy (newp, imap->l_scope,
353                                         cnt * sizeof (imap->l_scope[0]));
354               }
355             else
356               {
357                 newp = (struct r_scope_elem **)
358                   realloc (imap->l_scope,
359                            new_size * sizeof (struct r_scope_elem *));
360                 if (newp == NULL)
361                   _dl_signal_error (ENOMEM, "dlopen", NULL,
362                                     N_("cannot create scope list"));
363                 imap->l_scope = newp;
364               }
365
366             imap->l_scope_max = new_size;
367           }
368
369         imap->l_scope[cnt++] = &new->l_searchlist;
370         imap->l_scope[cnt] = NULL;
371       }
372 #if USE_TLS
373     else if (new->l_searchlist.r_list[i]->l_opencount == 1
374              /* Only if the module defines thread local data.  */
375              && __builtin_expect (new->l_searchlist.r_list[i]->l_tls_blocksize
376                                   > 0, 0))
377       {
378         /* Now that we know the object is loaded successfully add
379            modules containing TLS data to the dtv info table.  We
380            might have to increase its size.  */
381         struct dtv_slotinfo_list *listp;
382         struct dtv_slotinfo_list *prevp;
383         size_t idx = new->l_searchlist.r_list[i]->l_tls_modid;
384
385         assert (new->l_searchlist.r_list[i]->l_type == lt_loaded);
386
387         /* Find the place in the dtv slotinfo list.  */
388         listp = GL(dl_tls_dtv_slotinfo_list);
389         prevp = NULL;           /* Needed to shut up gcc.  */
390         do
391           {
392             /* Does it fit in the array of this list element?  */
393             if (idx < listp->len)
394               break;
395             idx -= listp->len;
396             prevp = listp;
397             listp = listp->next;
398           }
399         while (listp != NULL);
400
401         if (listp == NULL)
402           {
403             /* When we come here it means we have to add a new element
404                to the slotinfo list.  And the new module must be in
405                the first slot.  */
406             assert (idx == 0);
407
408             listp = prevp->next = (struct dtv_slotinfo_list *)
409               malloc (sizeof (struct dtv_slotinfo_list)
410                       + TLS_SLOTINFO_SURPLUS * sizeof (struct dtv_slotinfo));
411             if (listp == NULL)
412               {
413                 /* We ran out of memory.  We will simply fail this
414                    call but don't undo anything we did so far.  The
415                    application will crash or be terminated anyway very
416                    soon.  */
417
418                 /* We have to do this since some entries in the dtv
419                    slotinfo array might already point to this
420                    generation.  */
421                 ++GL(dl_tls_generation);
422
423                 _dl_signal_error (ENOMEM, "dlopen", NULL,
424                                   N_("cannot create TLS data structures"));
425               }
426
427             listp->len = TLS_SLOTINFO_SURPLUS;
428             listp->next = NULL;
429             memset (listp->slotinfo, '\0',
430                     TLS_SLOTINFO_SURPLUS * sizeof (struct dtv_slotinfo));
431           }
432
433         /* Add the information into the slotinfo data structure.  */
434         listp->slotinfo[idx].map = new->l_searchlist.r_list[i];
435         listp->slotinfo[idx].gen = GL(dl_tls_generation) + 1;
436
437         /* We have to bump the generation counter.  */
438         any_tls = true;
439       }
440
441   /* Bump the generation number if necessary.  */
442   if (any_tls)
443     if (__builtin_expect (++GL(dl_tls_generation) == 0, 0))
444       __libc_fatal (_("TLS generation counter wrapped!  Please send report with the 'glibcbug' script."));
445 #endif
446
447   /* Run the initializer functions of new objects.  */
448   _dl_init (new, __libc_argc, __libc_argv, __environ);
449
450   /* Now we can make the new map available in the global scope.  */
451   if (mode & RTLD_GLOBAL)
452     /* Move the object in the global namespace.  */
453     if (add_to_global (new) != 0)
454       /* It failed.  */
455       return;
456
457   /* Mark the object as not deletable if the RTLD_NODELETE flags was
458      passed.  */
459   if (__builtin_expect (mode & RTLD_NODELETE, 0))
460     new->l_flags_1 |= DF_1_NODELETE;
461
462 #ifndef SHARED
463   /* We must be the static _dl_open in libc.a.  A static program that
464      has loaded a dynamic object now has competition.  */
465   __libc_multiple_libcs = 1;
466 #endif
467
468   /* Let the user know about the opencount.  */
469   if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_FILES, 0))
470     _dl_debug_printf ("opening file=%s; opencount == %u\n\n",
471                       new->l_name, new->l_opencount);
472 }
473
474
475 void *
476 internal_function
477 _dl_open (const char *file, int mode, const void *caller)
478 {
479   struct dl_open_args args;
480   const char *objname;
481   const char *errstring;
482   int errcode;
483
484   if ((mode & RTLD_BINDING_MASK) == 0)
485     /* One of the flags must be set.  */
486     _dl_signal_error (EINVAL, file, NULL, N_("invalid mode for dlopen()"));
487
488   /* Make sure we are alone.  */
489   __rtld_lock_lock_recursive (GL(dl_load_lock));
490
491   args.file = file;
492   args.mode = mode;
493   args.caller = caller;
494   args.map = NULL;
495   errcode = _dl_catch_error (&objname, &errstring, dl_open_worker, &args);
496
497 #ifndef MAP_COPY
498   /* We must munmap() the cache file.  */
499   _dl_unload_cache ();
500 #endif
501
502   /* Release the lock.  */
503   __rtld_lock_unlock_recursive (GL(dl_load_lock));
504
505   if (__builtin_expect (errstring != NULL, 0))
506     {
507       /* Some error occurred during loading.  */
508       char *local_errstring;
509       size_t len_errstring;
510
511       /* Remove the object from memory.  It may be in an inconsistent
512          state if relocation failed, for example.  */
513       if (args.map)
514         {
515           unsigned int i;
516
517           /* Increment open counters for all objects since this
518              sometimes has not happened yet.  */
519           if (args.map->l_searchlist.r_list[0]->l_opencount == 0)
520             for (i = 0; i < args.map->l_searchlist.r_nlist; ++i)
521               ++args.map->l_searchlist.r_list[i]->l_opencount;
522
523 #ifdef USE_TLS
524           /* Maybe some of the modules which were loaded uses TLS.
525              Since it will be removed in the following _dl_close call
526              we have to mark the dtv array as having gaps to fill
527              the holes.  This is a pessimistic assumption which won't
528              hurt if not true.  */
529           GL(dl_tls_dtv_gaps) = true;
530 #endif
531
532           _dl_close (args.map);
533         }
534
535       /* Make a local copy of the error string so that we can release the
536          memory allocated for it.  */
537       len_errstring = strlen (errstring) + 1;
538       if (objname == errstring + len_errstring)
539         {
540           size_t total_len = len_errstring + strlen (objname) + 1;
541           local_errstring = alloca (total_len);
542           memcpy (local_errstring, errstring, total_len);
543           objname = local_errstring + len_errstring;
544         }
545       else
546         {
547           local_errstring = alloca (len_errstring);
548           memcpy (local_errstring, errstring, len_errstring);
549         }
550
551       if (errstring != _dl_out_of_memory)
552         free ((char *) errstring);
553
554       /* Reraise the error.  */
555       _dl_signal_error (errcode, objname, NULL, local_errstring);
556     }
557
558 #ifndef SHARED
559   DL_STATIC_INIT (args.map);
560 #endif
561
562   return args.map;
563 }
564 libc_hidden_def (_dl_open)
565
566
567 #ifdef SCOPE_DEBUG
568 #include <unistd.h>
569
570 static void
571 show_scope (struct link_map *new)
572 {
573   int scope_cnt;
574
575   for (scope_cnt = 0; new->l_scope[scope_cnt] != NULL; ++scope_cnt)
576     {
577       char numbuf[2];
578       unsigned int cnt;
579
580       numbuf[0] = '0' + scope_cnt;
581       numbuf[1] = '\0';
582       _dl_printf ("scope %s:", numbuf);
583
584       for (cnt = 0; cnt < new->l_scope[scope_cnt]->r_nlist; ++cnt)
585         if (*new->l_scope[scope_cnt]->r_list[cnt]->l_name)
586           _dl_printf (" %s", new->l_scope[scope_cnt]->r_list[cnt]->l_name);
587         else
588           _dl_printf (" <main>");
589
590       _dl_printf ("\n");
591     }
592 }
593 #endif