]> git.ipfire.org Git - thirdparty/glibc.git/blob - elf/dl-object.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / elf / dl-object.c
1 /* Storage management for the chain of loaded shared objects.
2 Copyright (C) 1995-2019 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 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.
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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <errno.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <ldsodefs.h>
24
25 #include <assert.h>
26
27
28 /* Add the new link_map NEW to the end of the namespace list. */
29 void
30 _dl_add_to_namespace_list (struct link_map *new, Lmid_t nsid)
31 {
32 /* We modify the list of loaded objects. */
33 __rtld_lock_lock_recursive (GL(dl_load_write_lock));
34
35 if (GL(dl_ns)[nsid]._ns_loaded != NULL)
36 {
37 struct link_map *l = GL(dl_ns)[nsid]._ns_loaded;
38 while (l->l_next != NULL)
39 l = l->l_next;
40 new->l_prev = l;
41 /* new->l_next = NULL; Would be necessary but we use calloc. */
42 l->l_next = new;
43 }
44 else
45 GL(dl_ns)[nsid]._ns_loaded = new;
46 ++GL(dl_ns)[nsid]._ns_nloaded;
47 new->l_serial = GL(dl_load_adds);
48 ++GL(dl_load_adds);
49
50 __rtld_lock_unlock_recursive (GL(dl_load_write_lock));
51 }
52
53
54 /* Allocate a `struct link_map' for a new object being loaded,
55 and enter it into the _dl_loaded list. */
56 struct link_map *
57 _dl_new_object (char *realname, const char *libname, int type,
58 struct link_map *loader, int mode, Lmid_t nsid)
59 {
60 #ifdef SHARED
61 unsigned int naudit;
62 if (__glibc_unlikely ((mode & __RTLD_OPENEXEC) != 0))
63 {
64 assert (type == lt_executable);
65 assert (nsid == LM_ID_BASE);
66
67 /* Ignore the specified libname for the main executable. It is
68 only known with an explicit loader invocation. */
69 libname = "";
70
71 /* We create the map for the executable before we know whether
72 we have auditing libraries and if yes, how many. Assume the
73 worst. */
74 naudit = DL_NNS;
75 }
76 else
77 naudit = GLRO (dl_naudit);
78 #endif
79
80 size_t libname_len = strlen (libname) + 1;
81 struct link_map *new;
82 struct libname_list *newname;
83 #ifdef SHARED
84 size_t audit_space = naudit * sizeof (new->l_audit[0]);
85 #else
86 # define audit_space 0
87 #endif
88
89 new = (struct link_map *) calloc (sizeof (*new) + audit_space
90 + sizeof (struct link_map *)
91 + sizeof (*newname) + libname_len, 1);
92 if (new == NULL)
93 return NULL;
94
95 new->l_real = new;
96 new->l_symbolic_searchlist.r_list = (struct link_map **) ((char *) (new + 1)
97 + audit_space);
98
99 new->l_libname = newname
100 = (struct libname_list *) (new->l_symbolic_searchlist.r_list + 1);
101 newname->name = (char *) memcpy (newname + 1, libname, libname_len);
102 /* newname->next = NULL; We use calloc therefore not necessary. */
103 newname->dont_free = 1;
104
105 /* When we create the executable link map, or a VDSO link map, we start
106 with "" for the l_name. In these cases "" points to ld.so rodata
107 and won't get dumped during core file generation. Therefore to assist
108 gdb and to create more self-contained core files we adjust l_name to
109 point at the newly allocated copy (which will get dumped) instead of
110 the ld.so rodata copy.
111
112 Furthermore, in case of explicit loader invocation, discard the
113 name of the main executable, to match the regular behavior, where
114 name of the executable is not known. */
115 #ifdef SHARED
116 if (*realname != '\0' && (mode & __RTLD_OPENEXEC) == 0)
117 #else
118 if (*realname != '\0')
119 #endif
120 new->l_name = realname;
121 else
122 new->l_name = (char *) newname->name + libname_len - 1;
123
124 new->l_type = type;
125 /* If we set the bit now since we know it is never used we avoid
126 dirtying the cache line later. */
127 if ((GLRO(dl_debug_mask) & DL_DEBUG_UNUSED) == 0)
128 new->l_used = 1;
129 new->l_loader = loader;
130 #if NO_TLS_OFFSET != 0
131 new->l_tls_offset = NO_TLS_OFFSET;
132 #endif
133 new->l_ns = nsid;
134
135 #ifdef SHARED
136 for (unsigned int cnt = 0; cnt < naudit; ++cnt)
137 {
138 new->l_audit[cnt].cookie = (uintptr_t) new;
139 /* new->l_audit[cnt].bindflags = 0; */
140 }
141 #endif
142
143 /* new->l_global = 0; We use calloc therefore not necessary. */
144
145 /* Use the 'l_scope_mem' array by default for the 'l_scope'
146 information. If we need more entries we will allocate a large
147 array dynamically. */
148 new->l_scope = new->l_scope_mem;
149 new->l_scope_max = sizeof (new->l_scope_mem) / sizeof (new->l_scope_mem[0]);
150
151 /* Counter for the scopes we have to handle. */
152 int idx = 0;
153
154 if (GL(dl_ns)[nsid]._ns_loaded != NULL)
155 /* Add the global scope. */
156 new->l_scope[idx++] = &GL(dl_ns)[nsid]._ns_loaded->l_searchlist;
157
158 /* If we have no loader the new object acts as it. */
159 if (loader == NULL)
160 loader = new;
161 else
162 /* Determine the local scope. */
163 while (loader->l_loader != NULL)
164 loader = loader->l_loader;
165
166 /* Insert the scope if it isn't the global scope we already added. */
167 if (idx == 0 || &loader->l_searchlist != new->l_scope[0])
168 {
169 if ((mode & RTLD_DEEPBIND) != 0 && idx != 0)
170 {
171 new->l_scope[1] = new->l_scope[0];
172 idx = 0;
173 }
174
175 new->l_scope[idx] = &loader->l_searchlist;
176 }
177
178 new->l_local_scope[0] = &new->l_searchlist;
179
180 /* Determine the origin. If allocating the link map for the main
181 executable, the realname is not known and "". In this case, the
182 origin needs to be determined by other means. However, in case
183 of an explicit loader invocation, the pathname of the main
184 executable is known and needs to be processed here: From the
185 point of view of the kernel, the main executable is the
186 dynamic loader, and this would lead to a computation of the wrong
187 origin. */
188 if (realname[0] != '\0')
189 {
190 size_t realname_len = strlen (realname) + 1;
191 char *origin;
192 char *cp;
193
194 if (realname[0] == '/')
195 {
196 /* It is an absolute path. Use it. But we have to make a
197 copy since we strip out the trailing slash. */
198 cp = origin = (char *) malloc (realname_len);
199 if (origin == NULL)
200 {
201 origin = (char *) -1;
202 goto out;
203 }
204 }
205 else
206 {
207 size_t len = realname_len;
208 char *result = NULL;
209
210 /* Get the current directory name. */
211 origin = NULL;
212 do
213 {
214 char *new_origin;
215
216 len += 128;
217 new_origin = (char *) realloc (origin, len);
218 if (new_origin == NULL)
219 /* We exit the loop. Note that result == NULL. */
220 break;
221 origin = new_origin;
222 }
223 while ((result = __getcwd (origin, len - realname_len)) == NULL
224 && errno == ERANGE);
225
226 if (result == NULL)
227 {
228 /* We were not able to determine the current directory.
229 Note that free(origin) is OK if origin == NULL. */
230 free (origin);
231 origin = (char *) -1;
232 goto out;
233 }
234
235 /* Find the end of the path and see whether we have to add a
236 slash. We could use rawmemchr but this need not be
237 fast. */
238 cp = (strchr) (origin, '\0');
239 if (cp[-1] != '/')
240 *cp++ = '/';
241 }
242
243 /* Add the real file name. */
244 cp = __mempcpy (cp, realname, realname_len);
245
246 /* Now remove the filename and the slash. Leave the slash if
247 the name is something like "/foo". */
248 do
249 --cp;
250 while (*cp != '/');
251
252 if (cp == origin)
253 /* Keep the only slash which is the first character. */
254 ++cp;
255 *cp = '\0';
256
257 out:
258 new->l_origin = origin;
259 }
260
261 return new;
262 }