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 <http://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. */
29void
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. */
56struct 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 size_t libname_len = strlen (libname) + 1;
61 struct link_map *new;
62 struct libname_list *newname;
63#ifdef SHARED
64 /* We create the map for the executable before we know whether we have
65 auditing libraries and if yes, how many. Assume the worst. */
66 unsigned int naudit = GLRO(dl_naudit) ?: ((mode & __RTLD_OPENEXEC)
67 ? DL_NNS : 0);
68 size_t audit_space = naudit * sizeof (new->l_audit[0]);
69#else
70# define audit_space 0
71#endif
72
73 new = (struct link_map *) calloc (sizeof (*new) + audit_space
74 + sizeof (struct link_map *)
75 + sizeof (*newname) + libname_len, 1);
76 if (new == NULL)
77 return NULL;
78
79 new->l_real = new;
80 new->l_symbolic_searchlist.r_list = (struct link_map **) ((char *) (new + 1)
81 + audit_space);
82
83 new->l_libname = newname
84 = (struct libname_list *) (new->l_symbolic_searchlist.r_list + 1);
85 newname->name = (char *) memcpy (newname + 1, libname, libname_len);
86 /* newname->next = NULL; We use calloc therefore not necessary. */
87 newname->dont_free = 1;
88
89 /* When we create the executable link map, or a VDSO link map, we start
90 with "" for the l_name. In these cases "" points to ld.so rodata
91 and won't get dumped during core file generation. Therefore to assist
92 gdb and to create more self-contained core files we adjust l_name to
93 point at the newly allocated copy (which will get dumped) instead of
94 the ld.so rodata copy. */
95 new->l_name = *realname ? realname : (char *) newname->name + libname_len - 1;
96 new->l_type = type;
97 /* If we set the bit now since we know it is never used we avoid
98 dirtying the cache line later. */
99 if ((GLRO(dl_debug_mask) & DL_DEBUG_UNUSED) == 0)
100 new->l_used = 1;
101 new->l_loader = loader;
102#if NO_TLS_OFFSET != 0
103 new->l_tls_offset = NO_TLS_OFFSET;
104#endif
105 new->l_ns = nsid;
106
107#ifdef SHARED
108 for (unsigned int cnt = 0; cnt < naudit; ++cnt)
109 {
110 new->l_audit[cnt].cookie = (uintptr_t) new;
111 /* new->l_audit[cnt].bindflags = 0; */
112 }
113#endif
114
115 /* new->l_global = 0; We use calloc therefore not necessary. */
116
117 /* Use the 'l_scope_mem' array by default for the 'l_scope'
118 information. If we need more entries we will allocate a large
119 array dynamically. */
120 new->l_scope = new->l_scope_mem;
121 new->l_scope_max = sizeof (new->l_scope_mem) / sizeof (new->l_scope_mem[0]);
122
123 /* Counter for the scopes we have to handle. */
124 int idx = 0;
125
126 if (GL(dl_ns)[nsid]._ns_loaded != NULL)
127 /* Add the global scope. */
128 new->l_scope[idx++] = &GL(dl_ns)[nsid]._ns_loaded->l_searchlist;
129
130 /* If we have no loader the new object acts as it. */
131 if (loader == NULL)
132 loader = new;
133 else
134 /* Determine the local scope. */
135 while (loader->l_loader != NULL)
136 loader = loader->l_loader;
137
138 /* Insert the scope if it isn't the global scope we already added. */
139 if (idx == 0 || &loader->l_searchlist != new->l_scope[0])
140 {
141 if ((mode & RTLD_DEEPBIND) != 0 && idx != 0)
142 {
143 new->l_scope[1] = new->l_scope[0];
144 idx = 0;
145 }
146
147 new->l_scope[idx] = &loader->l_searchlist;
148 }
149
150 new->l_local_scope[0] = &new->l_searchlist;
151
152 /* Don't try to find the origin for the main map which has the name "". */
153 if (realname[0] != '\0')
154 {
155 size_t realname_len = strlen (realname) + 1;
156 char *origin;
157 char *cp;
158
159 if (realname[0] == '/')
160 {
161 /* It is an absolute path. Use it. But we have to make a
162 copy since we strip out the trailing slash. */
163 cp = origin = (char *) malloc (realname_len);
164 if (origin == NULL)
165 {
166 origin = (char *) -1;
167 goto out;
168 }
169 }
170 else
171 {
172 size_t len = realname_len;
173 char *result = NULL;
174
175 /* Get the current directory name. */
176 origin = NULL;
177 do
178 {
179 char *new_origin;
180
181 len += 128;
182 new_origin = (char *) realloc (origin, len);
183 if (new_origin == NULL)
184 /* We exit the loop. Note that result == NULL. */
185 break;
186 origin = new_origin;
187 }
188 while ((result = __getcwd (origin, len - realname_len)) == NULL
189 && errno == ERANGE);
190
191 if (result == NULL)
192 {
193 /* We were not able to determine the current directory.
194 Note that free(origin) is OK if origin == NULL. */
195 free (origin);
196 origin = (char *) -1;
197 goto out;
198 }
199
200 /* Find the end of the path and see whether we have to add a
201 slash. We could use rawmemchr but this need not be
202 fast. */
203 cp = (strchr) (origin, '\0');
204 if (cp[-1] != '/')
205 *cp++ = '/';
206 }
207
208 /* Add the real file name. */
209 cp = __mempcpy (cp, realname, realname_len);
210
211 /* Now remove the filename and the slash. Leave the slash if
212 the name is something like "/foo". */
213 do
214 --cp;
215 while (*cp != '/');
216
217 if (cp == origin)
218 /* Keep the only slash which is the first character. */
219 ++cp;
220 *cp = '\0';
221
222 out:
223 new->l_origin = origin;
224 }
225
226 return new;
227}
228