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. */
25 size_t _dl_global_scope_alloc
;
28 _dl_open (const char *file
, int mode
)
30 struct link_map
*new, *l
;
35 /* Load the named object. */
36 new = _dl_map_object (NULL
, file
, lt_loaded
);
37 if (new->l_searchlist
)
38 /* It was already open. */
41 /* Load that object's dependencies. */
42 _dl_map_object_deps (new);
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. */
55 _dl_relocate_object (l
, _dl_object_relocation_scope (l
),
56 (mode
& RTLD_BINDING_MASK
) == RTLD_LAZY
);
57 *_dl_global_scope_end
= NULL
;
65 new->l_global
= (mode
& RTLD_GLOBAL
);
68 /* The symbols of the new object and its dependencies are to be
69 introduced into the global scope that will be used to resolve
70 references from other dynamically-loaded objects. */
72 if (_dl_global_scope_alloc
== 0)
74 /* This is the first dynamic object given global scope. */
75 _dl_global_scope_alloc
= 8;
76 _dl_global_scope
= malloc (8 * sizeof (struct link_map
*));
77 if (! _dl_global_scope
)
79 _dl_global_scope
= _dl_default_scope
;
82 _dl_signal_error (ENOMEM
, file
, "cannot extend global scope");
84 _dl_global_scope
[2] = _dl_default_scope
[2];
85 _dl_global_scope
[3] = new;
86 _dl_global_scope
[4] = NULL
;
87 _dl_global_scope
[5] = NULL
;
91 if (_dl_global_scope_alloc
<
92 _dl_global_scope_end
- _dl_global_scope
+ 2)
94 /* Must extend the list. */
95 struct link_map
**new = realloc (_dl_global_scope
,
96 _dl_global_scope_alloc
* 2);
99 _dl_global_scope_end
= new + (_dl_global_scope_end
-
101 _dl_global_scope
= new;
102 _dl_global_scope_alloc
*= 2;
105 /* Append the new object and re-terminate the list. */
106 *_dl_global_scope_end
++ = new;
107 /* We keep the list double-terminated so the last element
108 can be filled in for symbol lookups. */
109 _dl_global_scope_end
[0] = NULL
;
110 _dl_global_scope_end
[1] = NULL
;
115 /* Notify the debugger we have added some objects. We need to call
116 _dl_debug_initialize in a static program in case dynamic linking has
117 not been used before. */
118 r
= _dl_debug_initialize (0);
122 /* Run the initializer functions of new objects. */
123 while (init
= _dl_init_next (new))
124 (*(void (*) (void)) init
) ();