]> sourceware.org Git - glibc.git/blob - elf/dl-open.c
update from main archive 960818
[glibc.git] / elf / dl-open.c
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.
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
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
19
20 #include <link.h>
21 #include <dlfcn.h>
22 #include <stdlib.h>
23 #include <errno.h>
24
25
26 extern void _dl_start (void); weak_extern (_dl_start)
27
28 extern int __libc_multiple_libcs; /* Defined in init-first.c. */
29
30 size_t _dl_global_scope_alloc;
31
32 struct link_map *
33 _dl_open (const char *file, int mode)
34 {
35 struct link_map *new, *l;
36 ElfW(Addr) init;
37 struct r_debug *r;
38 /* To decide whether we are the static libc or not. We must use
39 this variable since gcc would otherwise optimize the test away. */
40 void (*dl_start_ptr) (void) = &_dl_start;
41
42 /* Load the named object. */
43 new = _dl_map_object (NULL, file, lt_loaded);
44 if (new->l_searchlist)
45 /* It was already open. */
46 return new;
47
48 /* Load that object's dependencies. */
49 _dl_map_object_deps (new, NULL, 0);
50
51
52 /* Relocate the objects loaded. We do this in reverse order so that copy
53 relocs of earlier objects overwrite the data written by later objects. */
54
55 l = new;
56 while (l->l_next)
57 l = l->l_next;
58 while (1)
59 {
60 if (! l->l_relocated)
61 {
62 /* We use an indirect call call for _dl_relocate_object because
63 we must avoid using the PLT in the call. If our PLT entry for
64 _dl_relocate_object hasn't been used yet, then the dynamic
65 linker fixup routine will clobber _dl_global_scope during its
66 work. We must be sure that nothing will require a PLT fixup
67 between when _dl_object_relocation_scope returns and when we
68 enter the dynamic linker's code (_dl_relocate_object). */
69 __typeof (_dl_relocate_object) *reloc = &_dl_relocate_object;
70 (*reloc) (l, _dl_object_relocation_scope (l),
71 (mode & RTLD_BINDING_MASK) == RTLD_LAZY);
72 *_dl_global_scope_end = NULL;
73 }
74
75 if (l == new)
76 break;
77 l = l->l_prev;
78 }
79
80 new->l_global = (mode & RTLD_GLOBAL);
81 if (new->l_global)
82 {
83 /* The symbols of the new object and its dependencies are to be
84 introduced into the global scope that will be used to resolve
85 references from other dynamically-loaded objects. */
86
87 if (_dl_global_scope_alloc == 0)
88 {
89 /* This is the first dynamic object given global scope. */
90 _dl_global_scope_alloc = 8;
91 _dl_global_scope = malloc (8 * sizeof (struct link_map *));
92 if (! _dl_global_scope)
93 {
94 _dl_global_scope = _dl_default_scope;
95 nomem:
96 _dl_close (new);
97 _dl_signal_error (ENOMEM, file, "cannot extend global scope");
98 }
99 _dl_global_scope[2] = _dl_default_scope[2];
100 _dl_global_scope[3] = new;
101 _dl_global_scope[4] = NULL;
102 _dl_global_scope[5] = NULL;
103 }
104 else
105 {
106 if (_dl_global_scope_alloc <
107 (size_t) (_dl_global_scope_end - _dl_global_scope + 2))
108 {
109 /* Must extend the list. */
110 struct link_map **new = realloc (_dl_global_scope,
111 _dl_global_scope_alloc * 2);
112 if (! new)
113 goto nomem;
114 _dl_global_scope_end = new + (_dl_global_scope_end -
115 _dl_global_scope);
116 _dl_global_scope = new;
117 _dl_global_scope_alloc *= 2;
118 }
119
120 /* Append the new object and re-terminate the list. */
121 *_dl_global_scope_end++ = new;
122 /* We keep the list double-terminated so the last element
123 can be filled in for symbol lookups. */
124 _dl_global_scope_end[0] = NULL;
125 _dl_global_scope_end[1] = NULL;
126 }
127 }
128
129
130 /* Notify the debugger we have added some objects. We need to call
131 _dl_debug_initialize in a static program in case dynamic linking has
132 not been used before. */
133 r = _dl_debug_initialize (0);
134 r->r_state = RT_ADD;
135 _dl_debug_state ();
136
137 /* Run the initializer functions of new objects. */
138 while (init = _dl_init_next (new))
139 (*(void (*) (void)) init) ();
140
141 if (dl_start_ptr == NULL)
142 /* We must be the static _dl_open in libc.a because ld.so.1 is not
143 in scope. A static program that has loaded a dynamic object
144 now has competition. */
145 __libc_multiple_libcs = 1;
146
147 return new;
148 }
This page took 0.044771 seconds and 5 git commands to generate.