*** empty log message ***
[glibc.git] / elf / dl-open.c
blob9dda31e7616ef23381d380e4f5aca180afb969f5
1 /* Load a shared object at runtime, relocate it, and run its initializer.
2 Copyright (C) 1996 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
20 #include <link.h>
21 #include <dlfcn.h>
22 #include <stdlib.h>
23 #include <errno.h>
25 size_t _dl_global_scope_alloc;
27 struct link_map *
28 _dl_open (const char *file, int mode)
30 struct link_map *new, *l;
31 ElfW(Addr) init;
32 struct r_debug *r;
35 /* Load the named object. */
36 new = _dl_map_object (NULL, file, lt_loaded);
37 if (new->l_searchlist)
38 /* It was already open. */
39 return new;
41 /* Load that object's dependencies. */
42 _dl_map_object_deps (new, NULL, 0);
45 /* Relocate the objects loaded. We do this in reverse order so that copy
46 relocs of earlier objects overwrite the data written by later objects. */
48 l = new;
49 while (l->l_next)
50 l = l->l_next;
51 while (1)
53 if (! l->l_relocated)
55 /* We use an indirect call call for _dl_relocate_object because
56 we must avoid using the PLT in the call. If our PLT entry for
57 _dl_relocate_object hasn't been used yet, then the dynamic
58 linker fixup routine will clobber _dl_global_scope during its
59 work. We must be sure that nothing will require a PLT fixup
60 between when _dl_object_relocation_scope returns and when we
61 enter the dynamic linker's code (_dl_relocate_object). */
62 __typeof (_dl_relocate_object) *reloc = &_dl_relocate_object;
63 (*reloc) (l, _dl_object_relocation_scope (l),
64 (mode & RTLD_BINDING_MASK) == RTLD_LAZY);
65 *_dl_global_scope_end = NULL;
68 if (l == new)
69 break;
70 l = l->l_prev;
73 new->l_global = (mode & RTLD_GLOBAL);
74 if (new->l_global)
76 /* The symbols of the new object and its dependencies are to be
77 introduced into the global scope that will be used to resolve
78 references from other dynamically-loaded objects. */
80 if (_dl_global_scope_alloc == 0)
82 /* This is the first dynamic object given global scope. */
83 _dl_global_scope_alloc = 8;
84 _dl_global_scope = malloc (8 * sizeof (struct link_map *));
85 if (! _dl_global_scope)
87 _dl_global_scope = _dl_default_scope;
88 nomem:
89 _dl_close (new);
90 _dl_signal_error (ENOMEM, file, "cannot extend global scope");
92 _dl_global_scope[2] = _dl_default_scope[2];
93 _dl_global_scope[3] = new;
94 _dl_global_scope[4] = NULL;
95 _dl_global_scope[5] = NULL;
97 else
99 if (_dl_global_scope_alloc <
100 (size_t) (_dl_global_scope_end - _dl_global_scope + 2))
102 /* Must extend the list. */
103 struct link_map **new = realloc (_dl_global_scope,
104 _dl_global_scope_alloc * 2);
105 if (! new)
106 goto nomem;
107 _dl_global_scope_end = new + (_dl_global_scope_end -
108 _dl_global_scope);
109 _dl_global_scope = new;
110 _dl_global_scope_alloc *= 2;
113 /* Append the new object and re-terminate the list. */
114 *_dl_global_scope_end++ = new;
115 /* We keep the list double-terminated so the last element
116 can be filled in for symbol lookups. */
117 _dl_global_scope_end[0] = NULL;
118 _dl_global_scope_end[1] = NULL;
123 /* Notify the debugger we have added some objects. We need to call
124 _dl_debug_initialize in a static program in case dynamic linking has
125 not been used before. */
126 r = _dl_debug_initialize (0);
127 r->r_state = RT_ADD;
128 _dl_debug_state ();
130 /* Run the initializer functions of new objects. */
131 while (init = _dl_init_next (new))
132 (*(void (*) (void)) init) ();
134 return new;