]>
Commit | Line | Data |
---|---|---|
d66e34cd | 1 | /* Storage management for the chain of loaded shared objects. |
f7a9f785 | 2 | Copyright (C) 1995-2016 Free Software Foundation, Inc. |
afd4eb37 UD |
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 | |
41bdb6e2 AJ |
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. | |
afd4eb37 UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
afd4eb37 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
d66e34cd | 18 | |
a853022c | 19 | #include <errno.h> |
d66e34cd RM |
20 | #include <string.h> |
21 | #include <stdlib.h> | |
f787edde | 22 | #include <unistd.h> |
a42195db | 23 | #include <ldsodefs.h> |
d66e34cd | 24 | |
a853022c | 25 | #include <assert.h> |
d66e34cd | 26 | |
d66e34cd | 27 | |
f0967738 AK |
28 | /* Add the new link_map NEW to the end of the namespace list. */ |
29 | void | |
30 | internal_function | |
31 | _dl_add_to_namespace_list (struct link_map *new, Lmid_t nsid) | |
32 | { | |
33 | /* We modify the list of loaded objects. */ | |
34 | __rtld_lock_lock_recursive (GL(dl_load_write_lock)); | |
35 | ||
36 | if (GL(dl_ns)[nsid]._ns_loaded != NULL) | |
37 | { | |
38 | struct link_map *l = GL(dl_ns)[nsid]._ns_loaded; | |
39 | while (l->l_next != NULL) | |
40 | l = l->l_next; | |
41 | new->l_prev = l; | |
42 | /* new->l_next = NULL; Would be necessary but we use calloc. */ | |
43 | l->l_next = new; | |
44 | } | |
45 | else | |
46 | GL(dl_ns)[nsid]._ns_loaded = new; | |
47 | ++GL(dl_ns)[nsid]._ns_nloaded; | |
48 | new->l_serial = GL(dl_load_adds); | |
49 | ++GL(dl_load_adds); | |
50 | ||
51 | __rtld_lock_unlock_recursive (GL(dl_load_write_lock)); | |
52 | } | |
53 | ||
54 | ||
d66e34cd RM |
55 | /* Allocate a `struct link_map' for a new object being loaded, |
56 | and enter it into the _dl_loaded list. */ | |
d66e34cd | 57 | struct link_map * |
d0fc4041 | 58 | internal_function |
be935610 | 59 | _dl_new_object (char *realname, const char *libname, int type, |
c0f62c56 | 60 | struct link_map *loader, int mode, Lmid_t nsid) |
d66e34cd | 61 | { |
76156ea1 | 62 | size_t libname_len = strlen (libname) + 1; |
c4bb124a UD |
63 | struct link_map *new; |
64 | struct libname_list *newname; | |
9dcafc55 UD |
65 | #ifdef SHARED |
66 | /* We create the map for the executable before we know whether we have | |
67 | auditing libraries and if yes, how many. Assume the worst. */ | |
68 | unsigned int naudit = GLRO(dl_naudit) ?: ((mode & __RTLD_OPENEXEC) | |
69 | ? DL_NNS : 0); | |
70 | size_t audit_space = naudit * sizeof (new->l_audit[0]); | |
71 | #else | |
72 | # define audit_space 0 | |
73 | #endif | |
c4bb124a | 74 | |
9dcafc55 | 75 | new = (struct link_map *) calloc (sizeof (*new) + audit_space |
7b91359b | 76 | + sizeof (struct link_map *) |
9dcafc55 | 77 | + sizeof (*newname) + libname_len, 1); |
11810621 | 78 | if (new == NULL) |
ba79d61b | 79 | return NULL; |
d66e34cd | 80 | |
c0f62c56 | 81 | new->l_real = new; |
ff0bf753 UD |
82 | new->l_symbolic_searchlist.r_list = (struct link_map **) ((char *) (new + 1) |
83 | + audit_space); | |
84 | ||
85 | new->l_libname = newname | |
86 | = (struct libname_list *) (new->l_symbolic_searchlist.r_list + 1); | |
c4bb124a | 87 | newname->name = (char *) memcpy (newname + 1, libname, libname_len); |
455e8060 | 88 | /* newname->next = NULL; We use calloc therefore not necessary. */ |
11810621 UD |
89 | newname->dont_free = 1; |
90 | ||
9182aa67 JK |
91 | /* When we create the executable link map, or a VDSO link map, we start |
92 | with "" for the l_name. In these cases "" points to ld.so rodata | |
93 | and won't get dumped during core file generation. Therefore to assist | |
94 | gdb and to create more self-contained core files we adjust l_name to | |
95 | point at the newly allocated copy (which will get dumped) instead of | |
96 | the ld.so rodata copy. */ | |
97 | new->l_name = *realname ? realname : (char *) newname->name + libname_len - 1; | |
d66e34cd | 98 | new->l_type = type; |
2af63968 UD |
99 | /* If we set the bit now since we know it is never used we avoid |
100 | dirtying the cache line later. */ | |
101 | if ((GLRO(dl_debug_mask) & DL_DEBUG_UNUSED) == 0) | |
102 | new->l_used = 1; | |
be935610 | 103 | new->l_loader = loader; |
11bf311e | 104 | #if NO_TLS_OFFSET != 0 |
299601a1 UD |
105 | new->l_tls_offset = NO_TLS_OFFSET; |
106 | #endif | |
c0f62c56 | 107 | new->l_ns = nsid; |
299601a1 | 108 | |
9dcafc55 UD |
109 | #ifdef SHARED |
110 | for (unsigned int cnt = 0; cnt < naudit; ++cnt) | |
111 | { | |
112 | new->l_audit[cnt].cookie = (uintptr_t) new; | |
113 | /* new->l_audit[cnt].bindflags = 0; */ | |
114 | } | |
115 | #endif | |
116 | ||
b25d4ff0 | 117 | /* new->l_global = 0; We use calloc therefore not necessary. */ |
d66e34cd | 118 | |
ded5b9b7 | 119 | /* Use the 'l_scope_mem' array by default for the 'l_scope' |
5a21d307 UD |
120 | information. If we need more entries we will allocate a large |
121 | array dynamically. */ | |
c0a777e8 UD |
122 | new->l_scope = new->l_scope_mem; |
123 | new->l_scope_max = sizeof (new->l_scope_mem) / sizeof (new->l_scope_mem[0]); | |
1100f849 | 124 | |
be935610 | 125 | /* Counter for the scopes we have to handle. */ |
f0967738 | 126 | int idx = 0; |
be935610 | 127 | |
c0f62c56 | 128 | if (GL(dl_ns)[nsid]._ns_loaded != NULL) |
f0967738 AK |
129 | /* Add the global scope. */ |
130 | new->l_scope[idx++] = &GL(dl_ns)[nsid]._ns_loaded->l_searchlist; | |
5a2a1d75 | 131 | |
7bccbc05 UD |
132 | /* If we have no loader the new object acts as it. */ |
133 | if (loader == NULL) | |
134 | loader = new; | |
135 | else | |
136 | /* Determine the local scope. */ | |
137 | while (loader->l_loader != NULL) | |
138 | loader = loader->l_loader; | |
139 | ||
140 | /* Insert the scope if it isn't the global scope we already added. */ | |
c0a777e8 | 141 | if (idx == 0 || &loader->l_searchlist != new->l_scope[0]) |
1fc07491 UD |
142 | { |
143 | if ((mode & RTLD_DEEPBIND) != 0 && idx != 0) | |
144 | { | |
c0a777e8 | 145 | new->l_scope[1] = new->l_scope[0]; |
1fc07491 UD |
146 | idx = 0; |
147 | } | |
148 | ||
c0a777e8 | 149 | new->l_scope[idx] = &loader->l_searchlist; |
1fc07491 | 150 | } |
be935610 | 151 | |
df4d2898 | 152 | new->l_local_scope[0] = &new->l_searchlist; |
be935610 UD |
153 | |
154 | /* Don't try to find the origin for the main map which has the name "". */ | |
155 | if (realname[0] != '\0') | |
f787edde | 156 | { |
7bccbc05 | 157 | size_t realname_len = strlen (realname) + 1; |
f787edde | 158 | char *origin; |
7bccbc05 | 159 | char *cp; |
f787edde UD |
160 | |
161 | if (realname[0] == '/') | |
162 | { | |
7bccbc05 UD |
163 | /* It is an absolute path. Use it. But we have to make a |
164 | copy since we strip out the trailing slash. */ | |
165 | cp = origin = (char *) malloc (realname_len); | |
f787edde | 166 | if (origin == NULL) |
7bccbc05 UD |
167 | { |
168 | origin = (char *) -1; | |
169 | goto out; | |
170 | } | |
f787edde UD |
171 | } |
172 | else | |
173 | { | |
7bccbc05 | 174 | size_t len = realname_len; |
f787edde UD |
175 | char *result = NULL; |
176 | ||
177 | /* Get the current directory name. */ | |
7bccbc05 UD |
178 | origin = NULL; |
179 | do | |
f787edde | 180 | { |
d1dddedf UD |
181 | char *new_origin; |
182 | ||
f787edde | 183 | len += 128; |
d1dddedf UD |
184 | new_origin = (char *) realloc (origin, len); |
185 | if (new_origin == NULL) | |
186 | /* We exit the loop. Note that result == NULL. */ | |
187 | break; | |
188 | origin = new_origin; | |
f787edde | 189 | } |
d1dddedf | 190 | while ((result = __getcwd (origin, len - realname_len)) == NULL |
7bccbc05 | 191 | && errno == ERANGE); |
f787edde UD |
192 | |
193 | if (result == NULL) | |
194 | { | |
7bccbc05 | 195 | /* We were not able to determine the current directory. |
2af63968 | 196 | Note that free(origin) is OK if origin == NULL. */ |
903244ac | 197 | free (origin); |
f787edde | 198 | origin = (char *) -1; |
7bccbc05 | 199 | goto out; |
f787edde | 200 | } |
f787edde | 201 | |
9710f75d UD |
202 | /* Find the end of the path and see whether we have to add a |
203 | slash. We could use rawmemchr but this need not be | |
204 | fast. */ | |
205 | cp = (strchr) (origin, '\0'); | |
7bccbc05 UD |
206 | if (cp[-1] != '/') |
207 | *cp++ = '/'; | |
f787edde UD |
208 | } |
209 | ||
7bccbc05 | 210 | /* Add the real file name. */ |
88794e30 | 211 | cp = __mempcpy (cp, realname, realname_len); |
7bccbc05 | 212 | |
88794e30 | 213 | /* Now remove the filename and the slash. Leave the slash if |
7bccbc05 | 214 | the name is something like "/foo". */ |
88794e30 UD |
215 | do |
216 | --cp; | |
217 | while (*cp != '/'); | |
218 | ||
7bccbc05 | 219 | if (cp == origin) |
88794e30 UD |
220 | /* Keep the only slash which is the first character. */ |
221 | ++cp; | |
222 | *cp = '\0'; | |
f787edde | 223 | |
7bccbc05 | 224 | out: |
f787edde UD |
225 | new->l_origin = origin; |
226 | } | |
227 | ||
d66e34cd RM |
228 | return new; |
229 | } |