[BZ #406]
[glibc.git] / elf / dl-object.c
blob91b1fa2187a459582b54a9abea4c863ce6aeda3a
1 /* Storage management for the chain of loaded shared objects.
2 Copyright (C) 1995-2002, 2004 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 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.
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.
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. */
20 #include <errno.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <ldsodefs.h>
26 #include <assert.h>
29 /* Allocate a `struct link_map' for a new object being loaded,
30 and enter it into the _dl_loaded list. */
32 struct link_map *
33 internal_function
34 _dl_new_object (char *realname, const char *libname, int type,
35 struct link_map *loader, int mode)
37 struct link_map *l;
38 int idx;
39 size_t libname_len = strlen (libname) + 1;
40 struct link_map *new;
41 struct libname_list *newname;
43 new = (struct link_map *) calloc (sizeof (*new) + sizeof (*newname)
44 + libname_len, 1);
45 if (new == NULL)
46 return NULL;
48 new->l_libname = newname = (struct libname_list *) (new + 1);
49 newname->name = (char *) memcpy (newname + 1, libname, libname_len);
50 /* newname->next = NULL; We use calloc therefore not necessary. */
51 newname->dont_free = 1;
53 new->l_name = realname;
54 new->l_type = type;
55 new->l_loader = loader;
56 #if defined USE_TLS && NO_TLS_OFFSET != 0
57 new->l_tls_offset = NO_TLS_OFFSET;
58 #endif
60 /* new->l_global = 0; We use calloc therefore not necessary. */
62 /* Use the 'l_scope_mem' array by default for the the 'l_scope'
63 information. If we need more entries we will allocate a large
64 array dynamically. */
65 new->l_scope = new->l_scope_mem;
66 new->l_scope_max = sizeof (new->l_scope_mem) / sizeof (new->l_scope_mem[0]);
68 /* Counter for the scopes we have to handle. */
69 idx = 0;
71 if (GL(dl_loaded) != NULL)
73 l = GL(dl_loaded);
74 while (l->l_next != NULL)
75 l = l->l_next;
76 new->l_prev = l;
77 /* new->l_next = NULL; Would be necessary but we use calloc. */
78 l->l_next = new;
80 /* Add the global scope. */
81 new->l_scope[idx++] = &GL(dl_loaded)->l_searchlist;
83 else
84 GL(dl_loaded) = new;
85 ++GL(dl_nloaded);
86 ++GL(dl_load_adds);
88 /* If we have no loader the new object acts as it. */
89 if (loader == NULL)
90 loader = new;
91 else
92 /* Determine the local scope. */
93 while (loader->l_loader != NULL)
94 loader = loader->l_loader;
96 /* Insert the scope if it isn't the global scope we already added. */
97 if (idx == 0 || &loader->l_searchlist != new->l_scope[0])
99 if ((mode & RTLD_DEEPBIND) != 0 && idx != 0)
101 new->l_scope[1] = new->l_scope[0];
102 idx = 0;
105 new->l_scope[idx] = &loader->l_searchlist;
108 new->l_local_scope[0] = &new->l_searchlist;
110 /* Don't try to find the origin for the main map which has the name "". */
111 if (realname[0] != '\0')
113 size_t realname_len = strlen (realname) + 1;
114 char *origin;
115 char *cp;
117 if (realname[0] == '/')
119 /* It is an absolute path. Use it. But we have to make a
120 copy since we strip out the trailing slash. */
121 cp = origin = (char *) malloc (realname_len);
122 if (origin == NULL)
124 origin = (char *) -1;
125 goto out;
128 else
130 size_t len = realname_len;
131 char *result = NULL;
133 /* Get the current directory name. */
134 origin = NULL;
137 char *new_origin;
139 len += 128;
140 new_origin = (char *) realloc (origin, len);
141 if (new_origin == NULL)
142 /* We exit the loop. Note that result == NULL. */
143 break;
144 origin = new_origin;
146 while ((result = __getcwd (origin, len - realname_len)) == NULL
147 && errno == ERANGE);
149 if (result == NULL)
151 /* We were not able to determine the current directory.
152 Note that free(origin) is OK if origin == NULL. */
153 free (origin);
154 origin = (char *) -1;
155 goto out;
158 /* Find the end of the path and see whether we have to add a
159 slash. We could use rawmemchr but this need not be
160 fast. */
161 cp = (strchr) (origin, '\0');
162 if (cp[-1] != '/')
163 *cp++ = '/';
166 /* Add the real file name. */
167 cp = __mempcpy (cp, realname, realname_len);
169 /* Now remove the filename and the slash. Leave the slash if
170 the name is something like "/foo". */
172 --cp;
173 while (*cp != '/');
175 if (cp == origin)
176 /* Keep the only slash which is the first character. */
177 ++cp;
178 *cp = '\0';
180 out:
181 new->l_origin = origin;
184 return new;