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