* string/basename.c (basename): Cast FILENAME to char * for return.
[glibc.git] / elf / dl-open.c
blob9389303b90536590e5c15c010fb35d07559f654d
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 (struct link_map *parent, const char *file, int mode)
30 struct link_map *new, *l;
31 ElfW(Addr) init;
34 /* Load the named object. */
35 new = _dl_map_object (parent, file, lt_loaded);
36 if (new->l_searchlist)
37 /* It was already open. */
38 return new;
40 /* Load that object's dependencies. */
41 _dl_map_object_deps (new);
44 /* Relocate the objects loaded. We do this in reverse order so that copy
45 relocs of earlier objects overwrite the data written by later objects. */
47 l = new;
48 while (l->l_next)
49 l = l->l_next;
52 if (! l->l_relocated)
54 _dl_relocate_object (l, _dl_object_relocation_scope (l),
55 (mode & RTLD_BINDING_MASK) == RTLD_LAZY);
56 *_dl_global_scope_end = NULL;
59 l = l->l_prev;
60 } while (l != new);
62 new->l_global = (mode & RTLD_GLOBAL);
63 if (new->l_global)
65 /* The symbols of the new object and its dependencies are to be
66 introduced into the global scope that will be used to resolve
67 references from other dynamically-loaded objects. */
69 if (_dl_global_scope_alloc == 0)
71 /* This is the first dynamic object given global scope. */
72 _dl_global_scope_alloc = 8;
73 _dl_global_scope = malloc (8 * sizeof (struct link_map *));
74 if (! _dl_global_scope)
76 _dl_global_scope = _dl_default_scope;
77 nomem:
78 _dl_close (new);
79 _dl_signal_error (ENOMEM, file, "cannot extend global scope");
81 _dl_global_scope[2] = _dl_default_scope[2];
82 _dl_global_scope[3] = new;
83 _dl_global_scope[4] = NULL;
84 _dl_global_scope[5] = NULL;
86 else
88 if (_dl_global_scope_alloc <
89 _dl_global_scope_end - _dl_global_scope + 2)
91 /* Must extend the list. */
92 struct link_map **new = realloc (_dl_global_scope,
93 _dl_global_scope_alloc * 2);
94 if (! new)
95 goto nomem;
96 _dl_global_scope_end = new + (_dl_global_scope_end -
97 _dl_global_scope);
98 _dl_global_scope = new;
99 _dl_global_scope_alloc *= 2;
102 /* Append the new object and re-terminate the list. */
103 *_dl_global_scope_end++ = new;
104 /* We keep the list double-terminated so the last element
105 can be filled in for symbol lookups. */
106 _dl_global_scope_end[0] = NULL;
107 _dl_global_scope_end[1] = NULL;
111 /* Run the initializer functions of new objects. */
112 while (init = _dl_init_next (new))
113 (*(void (*) (void)) init) ();
115 return new;