]>
Commit | Line | Data |
---|---|---|
0a54e401 | 1 | /* Map in a shared object's segments from the file. |
880f421f | 2 | Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc. |
afd4eb37 | 3 | This file is part of the GNU C Library. |
d66e34cd | 4 | |
afd4eb37 UD |
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. | |
d66e34cd | 9 | |
afd4eb37 UD |
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. | |
d66e34cd | 14 | |
afd4eb37 UD |
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 not, | |
17 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
18 | Boston, MA 02111-1307, USA. */ | |
d66e34cd | 19 | |
14e9dd67 | 20 | #include <elf.h> |
0a54e401 UD |
21 | #include <errno.h> |
22 | #include <fcntl.h> | |
d66e34cd | 23 | #include <link.h> |
0a54e401 | 24 | #include <stdlib.h> |
d66e34cd | 25 | #include <string.h> |
d66e34cd | 26 | #include <unistd.h> |
0a54e401 UD |
27 | #include <sys/mman.h> |
28 | #include <sys/stat.h> | |
29 | #include <sys/types.h> | |
d66e34cd | 30 | #include "dynamic-link.h" |
8193034b | 31 | #include <stdio-common/_itoa.h> |
d66e34cd RM |
32 | |
33 | ||
9b8a44cd RM |
34 | /* On some systems, no flag bits are given to specify file mapping. */ |
35 | #ifndef MAP_FILE | |
36 | #define MAP_FILE 0 | |
37 | #endif | |
38 | ||
39 | /* The right way to map in the shared library files is MAP_COPY, which | |
40 | makes a virtual copy of the data at the time of the mmap call; this | |
41 | guarantees the mapped pages will be consistent even if the file is | |
42 | overwritten. Some losing VM systems like Linux's lack MAP_COPY. All we | |
43 | get is MAP_PRIVATE, which copies each page when it is modified; this | |
44 | means if the file is overwritten, we may at some point get some pages | |
45 | from the new version after starting with pages from the old version. */ | |
46 | #ifndef MAP_COPY | |
47 | #define MAP_COPY MAP_PRIVATE | |
48 | #endif | |
49 | ||
f21acc89 UD |
50 | /* Some systems link their relocatable objects for another base address |
51 | than 0. We want to know the base address for these such that we can | |
52 | subtract this address from the segment addresses during mapping. | |
53 | This results in a more efficient address space usage. Defaults to | |
54 | zero for almost all systems. */ | |
55 | #ifndef MAP_BASE_ADDR | |
56 | #define MAP_BASE_ADDR(l) 0 | |
57 | #endif | |
58 | ||
9b8a44cd | 59 | |
d66e34cd RM |
60 | #include <endian.h> |
61 | #if BYTE_ORDER == BIG_ENDIAN | |
62 | #define byteorder ELFDATA2MSB | |
63 | #define byteorder_name "big-endian" | |
64 | #elif BYTE_ORDER == LITTLE_ENDIAN | |
65 | #define byteorder ELFDATA2LSB | |
66 | #define byteorder_name "little-endian" | |
67 | #else | |
68 | #error "Unknown BYTE_ORDER " BYTE_ORDER | |
69 | #define byteorder ELFDATANONE | |
70 | #endif | |
71 | ||
14e9dd67 | 72 | #define STRING(x) __STRING (x) |
d66e34cd | 73 | |
2064087b RM |
74 | #ifdef MAP_ANON |
75 | /* The fd is not examined when using MAP_ANON. */ | |
76 | #define ANONFD -1 | |
77 | #else | |
d66e34cd | 78 | int _dl_zerofd = -1; |
2064087b RM |
79 | #define ANONFD _dl_zerofd |
80 | #endif | |
81 | ||
4cca6b86 UD |
82 | /* Handle situations where we have a preferred location in memory for |
83 | the shared objects. */ | |
84 | #ifdef ELF_PREFERRED_ADDRESS_DATA | |
85 | ELF_PREFERRED_ADDRESS_DATA; | |
86 | #endif | |
87 | #ifndef ELF_PREFERRED_ADDRESS | |
88 | #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref) | |
89 | #endif | |
90 | #ifndef ELF_FIXED_ADDRESS | |
91 | #define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0) | |
92 | #endif | |
93 | ||
266180eb | 94 | size_t _dl_pagesize; |
d66e34cd | 95 | |
0a54e401 UD |
96 | extern const char *_dl_platform; |
97 | extern size_t _dl_platformlen; | |
d66e34cd | 98 | |
40a55d20 UD |
99 | /* This is a fake list to store the RPATH information for static |
100 | binaries. */ | |
101 | static struct r_search_path_elem **fake_path_list; | |
102 | ||
103 | ||
706074a5 UD |
104 | /* Local version of `strdup' function. */ |
105 | static inline char * | |
106 | local_strdup (const char *s) | |
107 | { | |
108 | size_t len = strlen (s) + 1; | |
109 | void *new = malloc (len); | |
110 | ||
111 | if (new == NULL) | |
112 | return NULL; | |
113 | ||
114 | return (char *) memcpy (new, s, len); | |
115 | } | |
116 | ||
0413b54c UD |
117 | /* Add `name' to the list of names for a particular shared object. |
118 | `name' is expected to have been allocated with malloc and will | |
119 | be freed if the shared object already has this name. | |
120 | Returns false if the object already had this name. */ | |
121 | static int | |
122 | add_name_to_object (struct link_map *l, char *name) | |
0a54e401 | 123 | { |
0413b54c UD |
124 | struct libname_list *lnp, *lastp; |
125 | struct libname_list *newname; | |
0a54e401 | 126 | |
0413b54c | 127 | if (name == NULL) |
da832465 UD |
128 | { |
129 | /* No more memory. */ | |
130 | _dl_signal_error (ENOMEM, NULL, "could not allocate name string"); | |
131 | return 0; | |
132 | } | |
0413b54c UD |
133 | |
134 | lastp = NULL; | |
135 | for (lnp = l->l_libname; lnp != NULL; lastp = lnp, lnp = lnp->next) | |
136 | if (strcmp (name, lnp->name) == 0) | |
137 | { | |
138 | free (name); | |
139 | return 0; | |
140 | } | |
141 | ||
142 | newname = malloc (sizeof *newname); | |
143 | if (newname == NULL) | |
da832465 UD |
144 | { |
145 | /* No more memory. */ | |
146 | _dl_signal_error (ENOMEM, name, "cannot allocate name record"); | |
147 | free (name); | |
148 | return 0; | |
149 | } | |
0413b54c UD |
150 | /* The object should have a libname set from _dl_new_object. */ |
151 | assert (lastp != NULL); | |
152 | ||
153 | newname->name = name; | |
154 | newname->next = NULL; | |
155 | lastp->next = newname; | |
156 | return 1; | |
157 | } | |
158 | ||
159 | ||
160 | /* Implement cache for search path lookup. */ | |
161 | #include "rtldtbl.h" | |
0a54e401 UD |
162 | |
163 | static size_t max_dirnamelen; | |
164 | ||
165 | static inline struct r_search_path_elem ** | |
166 | fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep, | |
b5efde2f | 167 | const char **trusted, const char *what, const char *where) |
0a54e401 UD |
168 | { |
169 | char *cp; | |
170 | size_t nelems = 0; | |
171 | ||
172 | while ((cp = __strsep (&rpath, sep)) != NULL) | |
173 | { | |
174 | struct r_search_path_elem *dirp; | |
175 | size_t len = strlen (cp); | |
176 | /* Remove trailing slashes. */ | |
177 | while (len > 1 && cp[len - 1] == '/') | |
178 | --len; | |
179 | ||
180 | /* Make sure we don't use untrusted directories if we run SUID. */ | |
181 | if (trusted != NULL) | |
182 | { | |
183 | const char **trun = trusted; | |
184 | ||
185 | /* All trusted directory must be complete name. */ | |
186 | if (cp[0] != '/') | |
187 | continue; | |
188 | ||
189 | while (*trun != NULL | |
190 | && (memcmp (*trun, cp, len) != 0 || (*trun)[len] != '\0')) | |
191 | ++trun; | |
192 | ||
193 | if (*trun == NULL) | |
194 | /* It's no trusted directory, skip it. */ | |
195 | continue; | |
196 | } | |
197 | ||
198 | /* Now add one. */ | |
199 | if (len > 0) | |
200 | cp[len++] = '/'; | |
201 | ||
202 | /* See if this directory is already known. */ | |
203 | for (dirp = all_dirs; dirp != NULL; dirp = dirp->next) | |
204 | if (dirp->dirnamelen == len && strcmp (cp, dirp->dirname) == 0) | |
205 | break; | |
206 | ||
207 | if (dirp != NULL) | |
208 | { | |
209 | /* It is available, see whether it's in our own list. */ | |
210 | size_t cnt; | |
211 | for (cnt = 0; cnt < nelems; ++cnt) | |
212 | if (result[cnt] == dirp) | |
213 | break; | |
214 | ||
215 | if (cnt == nelems) | |
216 | result[nelems++] = dirp; | |
217 | } | |
218 | else | |
219 | { | |
220 | /* It's a new directory. Create an entry and add it. */ | |
221 | dirp = (struct r_search_path_elem *) malloc (sizeof (*dirp)); | |
222 | if (dirp == NULL) | |
223 | _dl_signal_error (ENOMEM, NULL, | |
224 | "cannot create cache for search path"); | |
225 | ||
226 | dirp->dirnamelen = len; | |
3996f34b UD |
227 | /* We have to make sure all the relative directories are never |
228 | ignored. The current directory might change and all our | |
229 | saved information would be void. */ | |
230 | dirp->dirstatus = cp[0] != '/' ? existing : unknown; | |
0a54e401 UD |
231 | |
232 | /* Add the name of the machine dependent directory if a machine | |
233 | is defined. */ | |
234 | if (_dl_platform != NULL) | |
235 | { | |
236 | char *tmp; | |
237 | ||
238 | dirp->machdirnamelen = len + _dl_platformlen + 1; | |
239 | tmp = (char *) malloc (len + _dl_platformlen + 2); | |
240 | if (tmp == NULL) | |
241 | _dl_signal_error (ENOMEM, NULL, | |
242 | "cannot create cache for search path"); | |
0a54e401 | 243 | dirp->dirname = tmp; |
86187531 UD |
244 | tmp = __mempcpy (tmp, cp, len); |
245 | tmp = __mempcpy (tmp, _dl_platform, _dl_platformlen); | |
246 | *tmp++ = '/'; | |
247 | *tmp = '\0'; | |
248 | ||
3996f34b | 249 | dirp->machdirstatus = dirp->dirstatus; |
0a54e401 UD |
250 | |
251 | if (max_dirnamelen < dirp->machdirnamelen) | |
252 | max_dirnamelen = dirp->machdirnamelen; | |
253 | } | |
254 | else | |
255 | { | |
256 | char *tmp; | |
257 | ||
258 | dirp->machdirnamelen = len; | |
259 | dirp->machdirstatus = nonexisting; | |
260 | ||
261 | tmp = (char *) malloc (len + 1); | |
262 | if (tmp == NULL) | |
263 | _dl_signal_error (ENOMEM, NULL, | |
264 | "cannot create cache for search path"); | |
86187531 UD |
265 | dirp->dirname = tmp; |
266 | *((char *) __mempcpy (tmp, cp, len)) = '\0'; | |
0a54e401 UD |
267 | |
268 | if (max_dirnamelen < dirp->dirnamelen) | |
269 | max_dirnamelen = dirp->dirnamelen; | |
0a54e401 UD |
270 | } |
271 | ||
b5efde2f UD |
272 | dirp->what = what; |
273 | dirp->where = where; | |
274 | ||
0a54e401 UD |
275 | dirp->next = all_dirs; |
276 | all_dirs = dirp; | |
277 | ||
278 | /* Put it in the result array. */ | |
279 | result[nelems++] = dirp; | |
280 | } | |
281 | } | |
282 | ||
283 | /* Terminate the array. */ | |
284 | result[nelems] = NULL; | |
285 | ||
286 | return result; | |
287 | } | |
288 | ||
289 | ||
290 | static struct r_search_path_elem ** | |
b5efde2f UD |
291 | decompose_rpath (const char *rpath, size_t additional_room, |
292 | const char *what, const char *where) | |
0a54e401 UD |
293 | { |
294 | /* Make a copy we can work with. */ | |
295 | char *copy = strdupa (rpath); | |
296 | char *cp; | |
297 | struct r_search_path_elem **result; | |
298 | /* First count the number of necessary elements in the result array. */ | |
299 | size_t nelems = 0; | |
300 | ||
301 | for (cp = copy; *cp != '\0'; ++cp) | |
302 | if (*cp == ':') | |
303 | ++nelems; | |
304 | ||
305 | /* Allocate room for the result. NELEMS + 1 + ADDITIONAL_ROOM is an upper | |
306 | limit for the number of necessary entries. */ | |
307 | result = (struct r_search_path_elem **) malloc ((nelems + 1 | |
308 | + additional_room + 1) | |
309 | * sizeof (*result)); | |
310 | if (result == NULL) | |
311 | _dl_signal_error (ENOMEM, NULL, "cannot create cache for search path"); | |
312 | ||
b5efde2f | 313 | return fillin_rpath (copy, result, ":", NULL, what, where); |
0a54e401 UD |
314 | } |
315 | ||
316 | ||
317 | void | |
880f421f | 318 | _dl_init_paths (const char *llp) |
0a54e401 | 319 | { |
40a55d20 UD |
320 | static const char *trusted_dirs[] = |
321 | { | |
322 | #include "trusted-dirs.h" | |
323 | NULL | |
324 | }; | |
325 | ||
0a54e401 UD |
326 | struct r_search_path_elem **pelem; |
327 | ||
328 | /* We have in `search_path' the information about the RPATH of the | |
329 | dynamic loader. Now fill in the information about the applications | |
330 | RPATH and the directories addressed by the LD_LIBRARY_PATH environment | |
331 | variable. */ | |
332 | struct link_map *l; | |
333 | ||
880f421f | 334 | /* Number of elements in the library path. */ |
0a54e401 UD |
335 | size_t nllp; |
336 | ||
880f421f | 337 | /* First determine how many elements the LD_LIBRARY_PATH contents has. */ |
0a54e401 UD |
338 | if (llp != NULL && *llp != '\0') |
339 | { | |
340 | /* Simply count the number of colons. */ | |
341 | const char *cp = llp; | |
342 | nllp = 1; | |
343 | while (*cp) | |
344 | if (*cp++ == ':') | |
345 | ++nllp; | |
346 | } | |
347 | else | |
348 | nllp = 0; | |
349 | ||
350 | l = _dl_loaded; | |
40a55d20 | 351 | if (l != NULL) |
0a54e401 | 352 | { |
40a55d20 UD |
353 | if (l->l_type != lt_loaded && l->l_info[DT_RPATH]) |
354 | { | |
355 | /* Allocate room for the search path and fill in information | |
356 | from RPATH. */ | |
357 | l->l_rpath_dirs = | |
358 | decompose_rpath ((const char *) | |
359 | (l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr | |
360 | + l->l_info[DT_RPATH]->d_un.d_val), | |
b5efde2f | 361 | nllp, "RPATH", l->l_name); |
40a55d20 UD |
362 | } |
363 | else | |
364 | { | |
365 | /* If we have no LD_LIBRARY_PATH and no RPATH we must tell | |
366 | this somehow to prevent we look this up again and again. */ | |
367 | if (nllp == 0) | |
368 | l->l_rpath_dirs = (struct r_search_path_elem **) -1l; | |
369 | else | |
370 | { | |
371 | l->l_rpath_dirs = (struct r_search_path_elem **) | |
372 | malloc ((nllp + 1) * sizeof (*l->l_rpath_dirs)); | |
373 | if (l->l_rpath_dirs == NULL) | |
374 | _dl_signal_error (ENOMEM, NULL, | |
375 | "cannot create cache for search path"); | |
376 | l->l_rpath_dirs[0] = NULL; | |
377 | } | |
378 | } | |
379 | ||
380 | /* We don't need to search the list of fake entries which is searched | |
381 | when no dynamic objects were loaded at this time. */ | |
382 | fake_path_list = NULL; | |
383 | ||
384 | if (nllp > 0) | |
385 | { | |
386 | char *copy = strdupa (llp); | |
387 | ||
388 | /* Decompose the LD_LIBRARY_PATH and fill in the result. | |
389 | First search for the next place to enter elements. */ | |
390 | struct r_search_path_elem **result = l->l_rpath_dirs; | |
391 | while (*result != NULL) | |
392 | ++result; | |
393 | ||
394 | /* We need to take care that the LD_LIBRARY_PATH environment | |
395 | variable can contain a semicolon. */ | |
396 | (void) fillin_rpath (copy, result, ":;", | |
b5efde2f UD |
397 | __libc_enable_secure ? trusted_dirs : NULL, |
398 | "LD_LIBRARY_PATH", NULL); | |
40a55d20 | 399 | } |
0a54e401 UD |
400 | } |
401 | else | |
402 | { | |
40a55d20 UD |
403 | /* This is a statically linked program but we still have to |
404 | take care for the LD_LIBRARY_PATH environment variable. We | |
405 | use a fake link_map entry. This will only contain the | |
406 | l_rpath_dirs information. */ | |
407 | ||
0a54e401 | 408 | if (nllp == 0) |
40a55d20 | 409 | fake_path_list = NULL; |
0a54e401 UD |
410 | else |
411 | { | |
40a55d20 UD |
412 | fake_path_list = (struct r_search_path_elem **) |
413 | malloc ((nllp + 1) * sizeof (struct r_search_path_elem *)); | |
f41c8091 UD |
414 | if (fake_path_list == NULL) |
415 | _dl_signal_error (ENOMEM, NULL, | |
416 | "cannot create cache for search path"); | |
0a54e401 | 417 | |
40a55d20 | 418 | (void) fillin_rpath (local_strdup (llp), fake_path_list, ":;", |
b5efde2f UD |
419 | __libc_enable_secure ? trusted_dirs : NULL, |
420 | "LD_LIBRARY_PATH", NULL); | |
40a55d20 | 421 | } |
0a54e401 UD |
422 | } |
423 | ||
424 | /* Now set up the rest of the rtld_search_dirs. */ | |
425 | for (pelem = rtld_search_dirs; *pelem != NULL; ++pelem) | |
426 | { | |
427 | struct r_search_path_elem *relem = *pelem; | |
428 | ||
429 | if (_dl_platform != NULL) | |
430 | { | |
f43ce637 | 431 | char *tmp, *orig; |
0a54e401 UD |
432 | |
433 | relem->machdirnamelen = relem->dirnamelen + _dl_platformlen + 1; | |
434 | tmp = (char *) malloc (relem->machdirnamelen + 1); | |
435 | if (tmp == NULL) | |
436 | _dl_signal_error (ENOMEM, NULL, | |
437 | "cannot create cache for search path"); | |
438 | ||
f43ce637 | 439 | orig = tmp; |
86187531 UD |
440 | tmp = __mempcpy (tmp, relem->dirname, relem->dirnamelen); |
441 | tmp = __mempcpy (tmp, _dl_platform, _dl_platformlen); | |
442 | *tmp++ = '/'; | |
443 | *tmp = '\0'; | |
f43ce637 | 444 | relem->dirname = orig; |
0a54e401 UD |
445 | |
446 | relem->machdirstatus = unknown; | |
447 | ||
448 | if (max_dirnamelen < relem->machdirnamelen) | |
449 | max_dirnamelen = relem->machdirnamelen; | |
450 | } | |
451 | else | |
452 | { | |
453 | relem->machdirnamelen = relem->dirnamelen; | |
454 | relem->machdirstatus = nonexisting; | |
455 | ||
456 | if (max_dirnamelen < relem->dirnamelen) | |
457 | max_dirnamelen = relem->dirnamelen; | |
458 | } | |
b5efde2f UD |
459 | |
460 | relem->what = "system search path"; | |
461 | relem->where = NULL; | |
0a54e401 UD |
462 | } |
463 | } | |
464 | ||
465 | ||
ea03559a RM |
466 | /* Map in the shared object NAME, actually located in REALNAME, and already |
467 | opened on FD. */ | |
468 | ||
469 | struct link_map * | |
706074a5 | 470 | _dl_map_object_from_fd (char *name, int fd, char *realname, |
ba79d61b | 471 | struct link_map *loader, int l_type) |
ea03559a | 472 | { |
622586fb | 473 | struct link_map *l = NULL; |
ea03559a RM |
474 | void *file_mapping = NULL; |
475 | size_t mapping_size = 0; | |
476 | ||
b122c703 | 477 | #define LOSE(s) lose (0, (s)) |
ea03559a RM |
478 | void lose (int code, const char *msg) |
479 | { | |
266180eb | 480 | (void) __close (fd); |
ea03559a | 481 | if (file_mapping) |
266180eb | 482 | __munmap (file_mapping, mapping_size); |
ba79d61b RM |
483 | if (l) |
484 | { | |
485 | /* Remove the stillborn object from the list and free it. */ | |
486 | if (l->l_prev) | |
487 | l->l_prev->l_next = l->l_next; | |
488 | if (l->l_next) | |
489 | l->l_next->l_prev = l->l_prev; | |
490 | free (l); | |
491 | } | |
492 | free (realname); | |
493 | _dl_signal_error (code, name, msg); | |
0413b54c UD |
494 | free (name); /* Hmmm. Can this leak memory? Better |
495 | than a segfault, anyway. */ | |
ea03559a RM |
496 | } |
497 | ||
266180eb | 498 | inline caddr_t map_segment (ElfW(Addr) mapstart, size_t len, |
b122c703 RM |
499 | int prot, int fixed, off_t offset) |
500 | { | |
266180eb RM |
501 | caddr_t mapat = __mmap ((caddr_t) mapstart, len, prot, |
502 | fixed|MAP_COPY|MAP_FILE, | |
503 | fd, offset); | |
0413b54c | 504 | if (mapat == MAP_FAILED) |
b122c703 RM |
505 | lose (errno, "failed to map segment from shared object"); |
506 | return mapat; | |
507 | } | |
508 | ||
ea03559a RM |
509 | /* Make sure LOCATION is mapped in. */ |
510 | void *map (off_t location, size_t size) | |
511 | { | |
512 | if ((off_t) mapping_size <= location + (off_t) size) | |
513 | { | |
514 | void *result; | |
515 | if (file_mapping) | |
266180eb RM |
516 | __munmap (file_mapping, mapping_size); |
517 | mapping_size = (location + size + 1 + _dl_pagesize - 1); | |
518 | mapping_size &= ~(_dl_pagesize - 1); | |
519 | result = __mmap (file_mapping, mapping_size, PROT_READ, | |
520 | MAP_COPY|MAP_FILE, fd, 0); | |
0413b54c | 521 | if (result == MAP_FAILED) |
ea03559a RM |
522 | lose (errno, "cannot map file data"); |
523 | file_mapping = result; | |
524 | } | |
525 | return file_mapping + location; | |
526 | } | |
527 | ||
266180eb RM |
528 | const ElfW(Ehdr) *header; |
529 | const ElfW(Phdr) *phdr; | |
530 | const ElfW(Phdr) *ph; | |
8193034b | 531 | size_t maplength; |
b122c703 | 532 | int type; |
d66e34cd RM |
533 | |
534 | /* Look again to see if the real name matched another already loaded. */ | |
535 | for (l = _dl_loaded; l; l = l->l_next) | |
536 | if (! strcmp (realname, l->l_name)) | |
537 | { | |
538 | /* The object is already loaded. | |
539 | Just bump its reference count and return it. */ | |
266180eb | 540 | __close (fd); |
c84142e8 UD |
541 | |
542 | /* If the name is not in the list of names for this object add | |
543 | it. */ | |
ea03559a | 544 | free (realname); |
0413b54c | 545 | add_name_to_object (l, name); |
d66e34cd RM |
546 | ++l->l_opencount; |
547 | return l; | |
548 | } | |
549 | ||
8193034b UD |
550 | /* Print debugging message. */ |
551 | if (_dl_debug_files) | |
552 | _dl_debug_message (1, "file=", name, "; generating link map\n", NULL); | |
553 | ||
d66e34cd RM |
554 | /* Map in the first page to read the header. */ |
555 | header = map (0, sizeof *header); | |
d66e34cd | 556 | |
d66e34cd | 557 | /* Check the header for basic validity. */ |
c4b72918 RM |
558 | if (*(Elf32_Word *) &header->e_ident != |
559 | #if BYTE_ORDER == LITTLE_ENDIAN | |
560 | ((ELFMAG0 << (EI_MAG0 * 8)) | | |
561 | (ELFMAG1 << (EI_MAG1 * 8)) | | |
562 | (ELFMAG2 << (EI_MAG2 * 8)) | | |
563 | (ELFMAG3 << (EI_MAG3 * 8))) | |
564 | #else | |
565 | ((ELFMAG0 << (EI_MAG3 * 8)) | | |
566 | (ELFMAG1 << (EI_MAG2 * 8)) | | |
567 | (ELFMAG2 << (EI_MAG1 * 8)) | | |
568 | (ELFMAG3 << (EI_MAG0 * 8))) | |
569 | #endif | |
570 | ) | |
d66e34cd | 571 | LOSE ("invalid ELF header"); |
266180eb RM |
572 | #define ELF32_CLASS ELFCLASS32 |
573 | #define ELF64_CLASS ELFCLASS64 | |
574 | if (header->e_ident[EI_CLASS] != ELFW(CLASS)) | |
14e9dd67 | 575 | LOSE ("ELF file class not " STRING(__ELF_NATIVE_CLASS) "-bit"); |
d66e34cd RM |
576 | if (header->e_ident[EI_DATA] != byteorder) |
577 | LOSE ("ELF file data encoding not " byteorder_name); | |
578 | if (header->e_ident[EI_VERSION] != EV_CURRENT) | |
579 | LOSE ("ELF file version ident not " STRING(EV_CURRENT)); | |
580 | if (header->e_version != EV_CURRENT) | |
581 | LOSE ("ELF file version not " STRING(EV_CURRENT)); | |
582 | if (! elf_machine_matches_host (header->e_machine)) | |
583 | LOSE ("ELF file machine architecture not " ELF_MACHINE_NAME); | |
266180eb | 584 | if (header->e_phentsize != sizeof (ElfW(Phdr))) |
d66e34cd RM |
585 | LOSE ("ELF file's phentsize not the expected size"); |
586 | ||
2064087b RM |
587 | #ifndef MAP_ANON |
588 | #define MAP_ANON 0 | |
d66e34cd RM |
589 | if (_dl_zerofd == -1) |
590 | { | |
591 | _dl_zerofd = _dl_sysdep_open_zero_fill (); | |
592 | if (_dl_zerofd == -1) | |
ba79d61b RM |
593 | { |
594 | __close (fd); | |
595 | _dl_signal_error (errno, NULL, "cannot open zero fill device"); | |
596 | } | |
d66e34cd | 597 | } |
2064087b | 598 | #endif |
d66e34cd | 599 | |
ba79d61b RM |
600 | /* Enter the new object in the list of loaded objects. */ |
601 | l = _dl_new_object (realname, name, l_type); | |
602 | if (! l) | |
603 | lose (ENOMEM, "cannot create shared object descriptor"); | |
604 | l->l_opencount = 1; | |
605 | l->l_loader = loader; | |
606 | ||
b122c703 RM |
607 | /* Extract the remaining details we need from the ELF header |
608 | and then map in the program header table. */ | |
609 | l->l_entry = header->e_entry; | |
610 | type = header->e_type; | |
611 | l->l_phnum = header->e_phnum; | |
266180eb | 612 | phdr = map (header->e_phoff, l->l_phnum * sizeof (ElfW(Phdr))); |
879bf2e6 | 613 | |
b122c703 RM |
614 | { |
615 | /* Scan the program header table, collecting its load commands. */ | |
616 | struct loadcmd | |
617 | { | |
266180eb | 618 | ElfW(Addr) mapstart, mapend, dataend, allocend; |
b122c703 RM |
619 | off_t mapoff; |
620 | int prot; | |
621 | } loadcmds[l->l_phnum], *c; | |
622 | size_t nloadcmds = 0; | |
d66e34cd | 623 | |
d66e34cd | 624 | l->l_ld = 0; |
b122c703 RM |
625 | l->l_phdr = 0; |
626 | l->l_addr = 0; | |
d66e34cd RM |
627 | for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph) |
628 | switch (ph->p_type) | |
629 | { | |
630 | /* These entries tell us where to find things once the file's | |
631 | segments are mapped in. We record the addresses it says | |
632 | verbatim, and later correct for the run-time load address. */ | |
633 | case PT_DYNAMIC: | |
634 | l->l_ld = (void *) ph->p_vaddr; | |
635 | break; | |
636 | case PT_PHDR: | |
637 | l->l_phdr = (void *) ph->p_vaddr; | |
638 | break; | |
639 | ||
640 | case PT_LOAD: | |
b122c703 RM |
641 | /* A load command tells us to map in part of the file. |
642 | We record the load commands and process them all later. */ | |
266180eb | 643 | if (ph->p_align % _dl_pagesize != 0) |
d66e34cd RM |
644 | LOSE ("ELF load command alignment not page-aligned"); |
645 | if ((ph->p_vaddr - ph->p_offset) % ph->p_align) | |
646 | LOSE ("ELF load command address/offset not properly aligned"); | |
647 | { | |
b122c703 RM |
648 | struct loadcmd *c = &loadcmds[nloadcmds++]; |
649 | c->mapstart = ph->p_vaddr & ~(ph->p_align - 1); | |
266180eb RM |
650 | c->mapend = ((ph->p_vaddr + ph->p_filesz + _dl_pagesize - 1) |
651 | & ~(_dl_pagesize - 1)); | |
b122c703 RM |
652 | c->dataend = ph->p_vaddr + ph->p_filesz; |
653 | c->allocend = ph->p_vaddr + ph->p_memsz; | |
654 | c->mapoff = ph->p_offset & ~(ph->p_align - 1); | |
655 | c->prot = 0; | |
d66e34cd | 656 | if (ph->p_flags & PF_R) |
b122c703 | 657 | c->prot |= PROT_READ; |
d66e34cd | 658 | if (ph->p_flags & PF_W) |
b122c703 | 659 | c->prot |= PROT_WRITE; |
d66e34cd | 660 | if (ph->p_flags & PF_X) |
b122c703 RM |
661 | c->prot |= PROT_EXEC; |
662 | break; | |
663 | } | |
664 | } | |
d66e34cd | 665 | |
b122c703 | 666 | /* We are done reading the file's headers now. Unmap them. */ |
266180eb | 667 | __munmap (file_mapping, mapping_size); |
b122c703 RM |
668 | |
669 | /* Now process the load commands and map segments into memory. */ | |
670 | c = loadcmds; | |
671 | ||
8193034b UD |
672 | /* Length of the sections to be loaded. */ |
673 | maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart; | |
674 | ||
b122c703 RM |
675 | if (type == ET_DYN || type == ET_REL) |
676 | { | |
677 | /* This is a position-independent shared object. We can let the | |
678 | kernel map it anywhere it likes, but we must have space for all | |
679 | the segments in their specified positions relative to the first. | |
680 | So we map the first segment without MAP_FIXED, but with its | |
22930c9b RM |
681 | extent increased to cover all the segments. Then we remove |
682 | access from excess portion, and there is known sufficient space | |
4cca6b86 UD |
683 | there to remap from the later segments. |
684 | ||
685 | As a refinement, sometimes we have an address that we would | |
686 | prefer to map such objects at; but this is only a preference, | |
687 | the OS can do whatever it likes. */ | |
b122c703 | 688 | caddr_t mapat; |
4cca6b86 | 689 | ElfW(Addr) mappref; |
f21acc89 UD |
690 | mappref = (ELF_PREFERRED_ADDRESS (loader, maplength, c->mapstart) |
691 | - MAP_BASE_ADDR (l)); | |
4cca6b86 | 692 | mapat = map_segment (mappref, maplength, c->prot, 0, c->mapoff); |
266180eb | 693 | l->l_addr = (ElfW(Addr)) mapat - c->mapstart; |
b122c703 | 694 | |
22930c9b RM |
695 | /* Change protection on the excess portion to disallow all access; |
696 | the portions we do not remap later will be inaccessible as if | |
697 | unallocated. Then jump into the normal segment-mapping loop to | |
698 | handle the portion of the segment past the end of the file | |
699 | mapping. */ | |
0d3726c3 | 700 | __mprotect ((caddr_t) (l->l_addr + c->mapend), |
266180eb RM |
701 | loadcmds[nloadcmds - 1].allocend - c->mapend, |
702 | 0); | |
b122c703 RM |
703 | goto postmap; |
704 | } | |
4cca6b86 UD |
705 | else |
706 | { | |
707 | /* Notify ELF_PREFERRED_ADDRESS that we have to load this one | |
708 | fixed. */ | |
709 | ELF_FIXED_ADDRESS (loader, c->mapstart); | |
710 | } | |
b122c703 RM |
711 | |
712 | while (c < &loadcmds[nloadcmds]) | |
713 | { | |
714 | if (c->mapend > c->mapstart) | |
715 | /* Map the segment contents from the file. */ | |
716 | map_segment (l->l_addr + c->mapstart, c->mapend - c->mapstart, | |
717 | c->prot, MAP_FIXED, c->mapoff); | |
718 | ||
719 | postmap: | |
720 | if (c->allocend > c->dataend) | |
721 | { | |
722 | /* Extra zero pages should appear at the end of this segment, | |
723 | after the data mapped from the file. */ | |
266180eb | 724 | ElfW(Addr) zero, zeroend, zeropage; |
b122c703 RM |
725 | |
726 | zero = l->l_addr + c->dataend; | |
727 | zeroend = l->l_addr + c->allocend; | |
266180eb | 728 | zeropage = (zero + _dl_pagesize - 1) & ~(_dl_pagesize - 1); |
d66e34cd | 729 | |
b122c703 RM |
730 | if (zeroend < zeropage) |
731 | /* All the extra data is in the last page of the segment. | |
732 | We can just zero it. */ | |
733 | zeropage = zeroend; | |
734 | ||
735 | if (zeropage > zero) | |
d66e34cd | 736 | { |
b122c703 RM |
737 | /* Zero the final part of the last page of the segment. */ |
738 | if ((c->prot & PROT_WRITE) == 0) | |
d66e34cd | 739 | { |
b122c703 | 740 | /* Dag nab it. */ |
266180eb RM |
741 | if (__mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)), |
742 | _dl_pagesize, c->prot|PROT_WRITE) < 0) | |
b122c703 | 743 | lose (errno, "cannot change memory protections"); |
d66e34cd | 744 | } |
b122c703 RM |
745 | memset ((void *) zero, 0, zeropage - zero); |
746 | if ((c->prot & PROT_WRITE) == 0) | |
266180eb RM |
747 | __mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)), |
748 | _dl_pagesize, c->prot); | |
b122c703 | 749 | } |
d66e34cd | 750 | |
b122c703 RM |
751 | if (zeroend > zeropage) |
752 | { | |
753 | /* Map the remaining zero pages in from the zero fill FD. */ | |
754 | caddr_t mapat; | |
266180eb RM |
755 | mapat = __mmap ((caddr_t) zeropage, zeroend - zeropage, |
756 | c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED, | |
2064087b | 757 | ANONFD, 0); |
0413b54c | 758 | if (mapat == MAP_FAILED) |
9b8a44cd | 759 | lose (errno, "cannot map zero-fill pages"); |
d66e34cd RM |
760 | } |
761 | } | |
d66e34cd | 762 | |
b122c703 | 763 | ++c; |
879bf2e6 | 764 | } |
0d3726c3 RM |
765 | |
766 | if (l->l_phdr == 0) | |
767 | { | |
768 | /* There was no PT_PHDR specified. We need to find the phdr in the | |
769 | load image ourselves. We assume it is in fact in the load image | |
770 | somewhere, and that the first load command starts at the | |
771 | beginning of the file and thus contains the ELF file header. */ | |
772 | ElfW(Addr) bof = l->l_addr + loadcmds[0].mapstart; | |
773 | assert (loadcmds[0].mapoff == 0); | |
774 | l->l_phdr = (void *) (bof + ((const ElfW(Ehdr) *) bof)->e_phoff); | |
775 | } | |
776 | else | |
777 | /* Adjust the PT_PHDR value by the runtime load address. */ | |
778 | (ElfW(Addr)) l->l_phdr += l->l_addr; | |
b122c703 | 779 | } |
d66e34cd | 780 | |
6d9756c9 RM |
781 | /* We are done mapping in the file. We no longer need the descriptor. */ |
782 | __close (fd); | |
783 | ||
ba79d61b RM |
784 | if (l->l_type == lt_library && type == ET_EXEC) |
785 | l->l_type = lt_executable; | |
9b8a44cd | 786 | |
b122c703 RM |
787 | if (l->l_ld == 0) |
788 | { | |
789 | if (type == ET_DYN) | |
790 | LOSE ("object file has no dynamic section"); | |
791 | } | |
792 | else | |
266180eb | 793 | (ElfW(Addr)) l->l_ld += l->l_addr; |
879bf2e6 | 794 | |
463e148b RM |
795 | l->l_entry += l->l_addr; |
796 | ||
8193034b UD |
797 | if (_dl_debug_files) |
798 | { | |
799 | const size_t nibbles = sizeof (void *) * 2; | |
800 | char buf1[nibbles + 1]; | |
801 | char buf2[nibbles + 1]; | |
802 | char buf3[nibbles + 1]; | |
803 | ||
804 | buf1[nibbles] = '\0'; | |
805 | buf2[nibbles] = '\0'; | |
806 | buf3[nibbles] = '\0'; | |
807 | ||
808 | memset (buf1, '0', nibbles); | |
809 | memset (buf2, '0', nibbles); | |
810 | memset (buf3, '0', nibbles); | |
811 | _itoa_word ((unsigned long int) l->l_ld, &buf1[nibbles], 16, 0); | |
812 | _itoa_word ((unsigned long int) l->l_addr, &buf2[nibbles], 16, 0); | |
813 | _itoa_word (maplength, &buf3[nibbles], 16, 0); | |
814 | ||
815 | _dl_debug_message (1, " dynamic: 0x", buf1, " base: 0x", buf2, | |
816 | " size: 0x", buf3, "\n", NULL); | |
817 | memset (buf1, '0', nibbles); | |
818 | memset (buf2, '0', nibbles); | |
819 | memset (buf3, ' ', nibbles); | |
820 | _itoa_word ((unsigned long int) l->l_entry, &buf1[nibbles], 16, 0); | |
821 | _itoa_word ((unsigned long int) l->l_phdr, &buf2[nibbles], 16, 0); | |
822 | _itoa_word (l->l_phnum, &buf3[nibbles], 10, 0); | |
823 | _dl_debug_message (1, " entry: 0x", buf1, " phdr: 0x", buf2, | |
824 | " phnum: ", buf3, "\n\n", NULL); | |
825 | } | |
826 | ||
d66e34cd RM |
827 | elf_get_dynamic_info (l->l_ld, l->l_info); |
828 | if (l->l_info[DT_HASH]) | |
829 | _dl_setup_hash (l); | |
830 | ||
831 | return l; | |
832 | } | |
ba79d61b | 833 | \f |
b5efde2f UD |
834 | /* Print search path. */ |
835 | static void | |
836 | print_search_path (struct r_search_path_elem **list, | |
837 | const char *what, const char *name) | |
838 | { | |
839 | int first = 1; | |
840 | ||
8193034b | 841 | _dl_debug_message (1, " search path=", NULL); |
b5efde2f UD |
842 | |
843 | while (*list != NULL && (*list)->what == what) /* Yes, ==. */ | |
844 | { | |
845 | char *buf = strdupa ((*list)->dirname); | |
846 | ||
847 | if ((*list)->machdirstatus != nonexisting) | |
848 | { | |
849 | buf[(*list)->machdirnamelen - 1] = '\0'; | |
8193034b | 850 | _dl_debug_message (0, first ? "" : ":", buf, NULL); |
b5efde2f UD |
851 | first = 0; |
852 | } | |
853 | if ((*list)->dirstatus != nonexisting) | |
854 | { | |
855 | buf[(*list)->dirnamelen - 1] = '\0'; | |
8193034b | 856 | _dl_debug_message (0, first ? "" : ":", buf, NULL); |
b5efde2f UD |
857 | first = 0; |
858 | } | |
859 | ++list; | |
860 | } | |
861 | ||
862 | if (name != NULL) | |
8193034b | 863 | _dl_debug_message (0, "\t\t(", what, " from file ", |
b5efde2f UD |
864 | name[0] ? name : _dl_argv[0], ")\n", NULL); |
865 | else | |
8193034b | 866 | _dl_debug_message (0, "\t\t(", what, ")\n", NULL); |
b5efde2f UD |
867 | } |
868 | \f | |
0a54e401 | 869 | /* Try to open NAME in one of the directories in DIRS. |
ba79d61b RM |
870 | Return the fd, or -1. If successful, fill in *REALNAME |
871 | with the malloc'd full directory name. */ | |
872 | ||
873 | static int | |
c6222ab9 | 874 | open_path (const char *name, size_t namelen, int preloaded, |
0a54e401 UD |
875 | struct r_search_path_elem **dirs, |
876 | char **realname) | |
ba79d61b RM |
877 | { |
878 | char *buf; | |
0a54e401 | 879 | int fd = -1; |
b5efde2f | 880 | const char *current_what = NULL; |
ba79d61b | 881 | |
0a54e401 | 882 | if (dirs == NULL || *dirs == NULL) |
ba79d61b | 883 | { |
c4029823 | 884 | __set_errno (ENOENT); |
ba79d61b RM |
885 | return -1; |
886 | } | |
887 | ||
0a54e401 | 888 | buf = __alloca (max_dirnamelen + namelen); |
ba79d61b RM |
889 | do |
890 | { | |
0a54e401 UD |
891 | struct r_search_path_elem *this_dir = *dirs; |
892 | size_t buflen = 0; | |
ba79d61b | 893 | |
b5efde2f UD |
894 | /* If we are debugging the search for libraries print the path |
895 | now if it hasn't happened now. */ | |
896 | if (_dl_debug_libs && current_what != this_dir->what) | |
897 | { | |
898 | current_what = this_dir->what; | |
899 | print_search_path (dirs, current_what, this_dir->where); | |
900 | } | |
901 | ||
0a54e401 | 902 | if (this_dir->machdirstatus != nonexisting) |
fd26970f | 903 | { |
0a54e401 | 904 | /* Construct the pathname to try. */ |
86187531 UD |
905 | buflen = ((char *) __mempcpy (__mempcpy (buf, this_dir->dirname, |
906 | this_dir->machdirnamelen), | |
907 | name, namelen) | |
908 | - buf); | |
0a54e401 | 909 | |
b5efde2f UD |
910 | /* Print name we try if this is wanted. */ |
911 | if (_dl_debug_libs) | |
8193034b | 912 | _dl_debug_message (1, " trying file=", buf, "\n", NULL); |
b5efde2f | 913 | |
0a54e401 UD |
914 | fd = __open (buf, O_RDONLY); |
915 | if (this_dir->machdirstatus == unknown) | |
916 | if (fd != -1) | |
917 | this_dir->machdirstatus = existing; | |
918 | else | |
919 | { | |
920 | /* We failed to open machine dependent library. Let's | |
921 | test whether there is any directory at all. */ | |
922 | struct stat st; | |
fd26970f | 923 | |
0a54e401 | 924 | buf[this_dir->machdirnamelen - 1] = '\0'; |
fd26970f | 925 | |
0a614877 UD |
926 | if (__xstat (_STAT_VER, buf, &st) != 0 |
927 | || ! S_ISDIR (st.st_mode)) | |
0a54e401 UD |
928 | /* The directory does not exist ot it is no directory. */ |
929 | this_dir->machdirstatus = nonexisting; | |
fd26970f | 930 | else |
0a54e401 UD |
931 | this_dir->machdirstatus = existing; |
932 | } | |
c6222ab9 UD |
933 | if (fd != -1 && preloaded && __libc_enable_secure) |
934 | { | |
935 | /* This is an extra security effort to make sure nobody can | |
936 | preload broken shared objects which are in the trusted | |
937 | directories and so exploit the bugs. */ | |
938 | struct stat st; | |
939 | ||
940 | if (__fxstat (_STAT_VER, fd, &st) != 0 | |
941 | || (st.st_mode & S_ISUID) == 0) | |
942 | { | |
943 | /* The shared object cannot be tested for being SUID | |
944 | or this bit is not set. In this case we must not | |
945 | use this object. */ | |
946 | __close (fd); | |
947 | fd = -1; | |
948 | /* We simply ignore the file, signal this by setting | |
949 | the error value which would have been set by `open'. */ | |
950 | errno = ENOENT; | |
951 | } | |
952 | } | |
fd26970f | 953 | } |
c6222ab9 UD |
954 | else |
955 | errno = ENOENT; | |
fd26970f | 956 | |
c6222ab9 | 957 | if (fd == -1 && errno == ENOENT && this_dir->dirstatus != nonexisting) |
ba79d61b RM |
958 | { |
959 | /* Construct the pathname to try. */ | |
86187531 UD |
960 | buflen = ((char *) __mempcpy (__mempcpy (buf, this_dir->dirname, |
961 | this_dir->dirnamelen), | |
962 | name, namelen) | |
963 | - buf); | |
0a54e401 | 964 | |
b5efde2f UD |
965 | /* Print name we try if this is wanted. */ |
966 | if (_dl_debug_libs) | |
8193034b | 967 | _dl_debug_message (1, " trying file=", buf, "\n", NULL); |
b5efde2f | 968 | |
0a54e401 UD |
969 | fd = __open (buf, O_RDONLY); |
970 | if (this_dir->dirstatus == unknown) | |
971 | if (fd != -1) | |
972 | this_dir->dirstatus = existing; | |
973 | else | |
974 | /* We failed to open library. Let's test whether there | |
975 | is any directory at all. */ | |
976 | if (this_dir->dirnamelen <= 1) | |
977 | this_dir->dirstatus = existing; | |
978 | else | |
979 | { | |
980 | struct stat st; | |
981 | ||
982 | buf[this_dir->dirnamelen - 1] = '\0'; | |
983 | ||
0a614877 UD |
984 | if (__xstat (_STAT_VER, buf, &st) != 0 |
985 | || ! S_ISDIR (st.st_mode)) | |
0a54e401 UD |
986 | /* The directory does not exist ot it is no directory. */ |
987 | this_dir->dirstatus = nonexisting; | |
988 | else | |
989 | this_dir->dirstatus = existing; | |
990 | } | |
c6222ab9 UD |
991 | if (fd != -1 && preloaded && __libc_enable_secure) |
992 | { | |
993 | /* This is an extra security effort to make sure nobody can | |
994 | preload broken shared objects which are in the trusted | |
995 | directories and so exploit the bugs. */ | |
996 | struct stat st; | |
997 | ||
998 | if (__fxstat (_STAT_VER, fd, &st) != 0 | |
999 | || (st.st_mode & S_ISUID) == 0) | |
1000 | { | |
1001 | /* The shared object cannot be tested for being SUID | |
1002 | or this bit is not set. In this case we must not | |
1003 | use this object. */ | |
1004 | __close (fd); | |
1005 | fd = -1; | |
1006 | /* We simply ignore the file, signal this by setting | |
1007 | the error value which would have been set by `open'. */ | |
1008 | errno = ENOENT; | |
1009 | } | |
1010 | } | |
ba79d61b RM |
1011 | } |
1012 | ||
ba79d61b RM |
1013 | if (fd != -1) |
1014 | { | |
1015 | *realname = malloc (buflen); | |
c6222ab9 | 1016 | if (*realname != NULL) |
ba79d61b RM |
1017 | { |
1018 | memcpy (*realname, buf, buflen); | |
1019 | return fd; | |
1020 | } | |
1021 | else | |
1022 | { | |
1023 | /* No memory for the name, we certainly won't be able | |
1024 | to load and link it. */ | |
1025 | __close (fd); | |
1026 | return -1; | |
1027 | } | |
1028 | } | |
1029 | if (errno != ENOENT && errno != EACCES) | |
1030 | /* The file exists and is readable, but something went wrong. */ | |
1031 | return -1; | |
1032 | } | |
0a54e401 | 1033 | while (*++dirs != NULL); |
ba79d61b RM |
1034 | |
1035 | return -1; | |
1036 | } | |
1037 | ||
1038 | /* Map in the shared object file NAME. */ | |
1039 | ||
1040 | struct link_map * | |
c6222ab9 UD |
1041 | _dl_map_object (struct link_map *loader, const char *name, int preloaded, |
1042 | int type, int trace_mode) | |
ba79d61b RM |
1043 | { |
1044 | int fd; | |
1045 | char *realname; | |
14bab8de | 1046 | char *name_copy; |
ba79d61b RM |
1047 | struct link_map *l; |
1048 | ||
1049 | /* Look for this name among those already loaded. */ | |
1050 | for (l = _dl_loaded; l; l = l->l_next) | |
f41c8091 UD |
1051 | { |
1052 | /* If the requested name matches the soname of a loaded object, | |
1053 | use that object. Elide this check for names that have not | |
1054 | yet been opened. */ | |
1055 | if (l->l_opencount <= 0) | |
1056 | continue; | |
1057 | if (!_dl_name_match_p (name, l)) | |
1058 | { | |
1059 | const char *soname; | |
1060 | ||
1061 | if (l->l_info[DT_SONAME] == NULL) | |
1062 | continue; | |
1063 | ||
1064 | soname = (const char *) (l->l_addr | |
1065 | + l->l_info[DT_STRTAB]->d_un.d_ptr | |
1066 | + l->l_info[DT_SONAME]->d_un.d_val); | |
1067 | if (strcmp (name, soname) != 0) | |
1068 | continue; | |
1069 | ||
1070 | /* We have a match on a new name -- cache it. */ | |
1071 | add_name_to_object (l, local_strdup (soname)); | |
1072 | } | |
1073 | ||
1074 | /* We have a match -- bump the reference count and return it. */ | |
1075 | ++l->l_opencount; | |
1076 | return l; | |
1077 | } | |
ba79d61b | 1078 | |
8193034b UD |
1079 | /* Display information if we are debugging. */ |
1080 | if (_dl_debug_files && loader != NULL) | |
1081 | _dl_debug_message (1, "\nfile=", name, "; needed by ", | |
1082 | loader->l_name[0] ? loader->l_name : _dl_argv[0], | |
1083 | "\n", NULL); | |
1084 | ||
ba79d61b RM |
1085 | if (strchr (name, '/') == NULL) |
1086 | { | |
1087 | /* Search for NAME in several places. */ | |
1088 | ||
1089 | size_t namelen = strlen (name) + 1; | |
1090 | ||
b5efde2f | 1091 | if (_dl_debug_libs) |
8193034b | 1092 | _dl_debug_message (1, "find library=", name, "; searching\n", NULL); |
b5efde2f | 1093 | |
ba79d61b | 1094 | fd = -1; |
a23db8e4 RM |
1095 | |
1096 | /* First try the DT_RPATH of the dependent object that caused NAME | |
1097 | to be loaded. Then that object's dependent, and on up. */ | |
1098 | for (l = loader; fd == -1 && l; l = l->l_loader) | |
ba79d61b | 1099 | if (l && l->l_info[DT_RPATH]) |
fd26970f | 1100 | { |
0a54e401 UD |
1101 | /* Make sure the cache information is available. */ |
1102 | if (l->l_rpath_dirs == NULL) | |
1103 | { | |
1104 | size_t ptrval = (l->l_addr | |
1105 | + l->l_info[DT_STRTAB]->d_un.d_ptr | |
1106 | + l->l_info[DT_RPATH]->d_un.d_val); | |
1107 | l->l_rpath_dirs = | |
b5efde2f UD |
1108 | decompose_rpath ((const char *) ptrval, 0, |
1109 | "RPATH", l->l_name); | |
0a54e401 UD |
1110 | } |
1111 | ||
1112 | if (l->l_rpath_dirs != (struct r_search_path_elem **) -1l) | |
c6222ab9 UD |
1113 | fd = open_path (name, namelen, preloaded, l->l_rpath_dirs, |
1114 | &realname); | |
0a54e401 UD |
1115 | } |
1116 | ||
1117 | /* If dynamically linked, try the DT_RPATH of the executable itself | |
1118 | and the LD_LIBRARY_PATH environment variable. */ | |
1119 | l = _dl_loaded; | |
1120 | if (fd == -1 && l && l->l_type != lt_loaded | |
1121 | && l->l_rpath_dirs != (struct r_search_path_elem **) -1l) | |
c6222ab9 | 1122 | fd = open_path (name, namelen, preloaded, l->l_rpath_dirs, &realname); |
fd26970f | 1123 | |
40a55d20 UD |
1124 | /* This is used if a static binary uses dynamic loading and there |
1125 | is a LD_LIBRARY_PATH given. */ | |
1126 | if (fd == -1 && fake_path_list != NULL) | |
c6222ab9 | 1127 | fd = open_path (name, namelen, preloaded, fake_path_list, &realname); |
40a55d20 | 1128 | |
f18edac3 RM |
1129 | if (fd == -1) |
1130 | { | |
1131 | /* Check the list of libraries in the file /etc/ld.so.cache, | |
1132 | for compatibility with Linux's ldconfig program. */ | |
1133 | extern const char *_dl_load_cache_lookup (const char *name); | |
1134 | const char *cached = _dl_load_cache_lookup (name); | |
1135 | if (cached) | |
1136 | { | |
1137 | fd = __open (cached, O_RDONLY); | |
1138 | if (fd != -1) | |
1139 | { | |
706074a5 UD |
1140 | realname = local_strdup (cached); |
1141 | if (realname == NULL) | |
f18edac3 RM |
1142 | { |
1143 | __close (fd); | |
1144 | fd = -1; | |
1145 | } | |
1146 | } | |
1147 | } | |
1148 | } | |
0a54e401 | 1149 | |
a23db8e4 | 1150 | /* Finally, try the default path. */ |
ba79d61b | 1151 | if (fd == -1) |
c6222ab9 | 1152 | fd = open_path (name, namelen, preloaded, rtld_search_dirs, &realname); |
b5efde2f UD |
1153 | |
1154 | /* Add another newline when we a tracing the library loading. */ | |
1155 | if (_dl_debug_libs) | |
8193034b | 1156 | _dl_debug_message (1, "\n", NULL); |
ba79d61b RM |
1157 | } |
1158 | else | |
1159 | { | |
1160 | fd = __open (name, O_RDONLY); | |
1161 | if (fd != -1) | |
1162 | { | |
706074a5 UD |
1163 | realname = local_strdup (name); |
1164 | if (realname == NULL) | |
ba79d61b RM |
1165 | { |
1166 | __close (fd); | |
1167 | fd = -1; | |
1168 | } | |
1169 | } | |
1170 | } | |
1171 | ||
706074a5 UD |
1172 | if (fd != -1) |
1173 | { | |
14bab8de UD |
1174 | name_copy = local_strdup (name); |
1175 | if (name_copy == NULL) | |
706074a5 UD |
1176 | { |
1177 | __close (fd); | |
1178 | fd = -1; | |
1179 | } | |
1180 | } | |
1181 | ||
ba79d61b | 1182 | if (fd == -1) |
46ec036d UD |
1183 | { |
1184 | if (trace_mode) | |
1185 | { | |
1186 | /* We haven't found an appropriate library. But since we | |
1187 | are only interested in the list of libraries this isn't | |
1188 | so severe. Fake an entry with all the information we | |
1ef32c3d | 1189 | have. */ |
fd26970f | 1190 | static const ElfW(Symndx) dummy_bucket = STN_UNDEF; |
46ec036d UD |
1191 | |
1192 | /* Enter the new object in the list of loaded objects. */ | |
1193 | if ((name_copy = local_strdup (name)) == NULL | |
1194 | || (l = _dl_new_object (name_copy, name, type)) == NULL) | |
1195 | _dl_signal_error (ENOMEM, name, | |
1196 | "cannot create shared object descriptor"); | |
1197 | /* We use an opencount of 0 as a sign for the faked entry. */ | |
1198 | l->l_opencount = 0; | |
1199 | l->l_reserved = 0; | |
fd26970f UD |
1200 | l->l_buckets = &dummy_bucket; |
1201 | l->l_nbuckets = 1; | |
1202 | l->l_relocated = 1; | |
1203 | ||
1204 | return l; | |
46ec036d UD |
1205 | } |
1206 | else | |
1207 | _dl_signal_error (errno, name, "cannot open shared object file"); | |
1208 | } | |
ba79d61b | 1209 | |
14bab8de | 1210 | return _dl_map_object_from_fd (name_copy, fd, realname, loader, type); |
ba79d61b | 1211 | } |