]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/dl-load.c
1999-02-27 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
126b06f9
UD
570#define LOSE(code, s) lose (code, fd, name, realname, l, s)
571static void volatile
572__attribute__ ((noreturn))
573lose (int code, int fd, const char *name, char *realname, struct link_map *l,
574 const char *msg)
575{
576 /* The use of `alloca' here looks ridiculous but it helps. The goal
577 is to avoid the function from being inlined. There is no official
578 way to do this so we use this trick. gcc never inlines functions
579 which use `alloca'. */
580 int *a = alloca (sizeof (int));
581 a[0] = fd;
582 (void) __close (a[0]);
583 if (l != NULL)
584 {
585 /* Remove the stillborn object from the list and free it. */
586 if (l->l_prev)
587 l->l_prev->l_next = l->l_next;
588 if (l->l_next)
589 l->l_next->l_prev = l->l_prev;
590 free (l);
591 }
592 free (realname);
593 _dl_signal_error (code, name, msg);
594}
595
596
ea03559a
RM
597/* Map in the shared object NAME, actually located in REALNAME, and already
598 opened on FD. */
599
f787edde
UD
600#ifndef EXTERNAL_MAP_FROM_FD
601static
602#endif
ea03559a 603struct link_map *
143e2b96 604_dl_map_object_from_fd (const char *name, int fd, char *realname,
ba79d61b 605 struct link_map *loader, int l_type)
ea03559a 606{
126b06f9
UD
607 /* This is the expected ELF header. */
608#define ELF32_CLASS ELFCLASS32
609#define ELF64_CLASS ELFCLASS64
610 static const unsigned char expected[EI_PAD] =
611 {
612 [EI_MAG0] = ELFMAG0,
613 [EI_MAG1] = ELFMAG1,
614 [EI_MAG2] = ELFMAG2,
615 [EI_MAG3] = ELFMAG3,
616 [EI_CLASS] = ELFW(CLASS),
617 [EI_DATA] = byteorder,
618 [EI_VERSION] = EV_CURRENT,
619 [EI_OSABI] = ELFOSABI_SYSV,
620 [EI_ABIVERSION] = 0
621 };
622586fb 622 struct link_map *l = NULL;
ea03559a 623
266180eb 624 inline caddr_t map_segment (ElfW(Addr) mapstart, size_t len,
b122c703
RM
625 int prot, int fixed, off_t offset)
626 {
266180eb
RM
627 caddr_t mapat = __mmap ((caddr_t) mapstart, len, prot,
628 fixed|MAP_COPY|MAP_FILE,
629 fd, offset);
0413b54c 630 if (mapat == MAP_FAILED)
126b06f9 631 LOSE (errno, "failed to map segment from shared object");
b122c703
RM
632 return mapat;
633 }
634
266180eb
RM
635 const ElfW(Ehdr) *header;
636 const ElfW(Phdr) *phdr;
637 const ElfW(Phdr) *ph;
8193034b 638 size_t maplength;
b122c703 639 int type;
32c85e43
UD
640 char *readbuf;
641 ssize_t readlength;
61e0617a
UD
642 struct stat st;
643
644 /* Get file information. */
126b06f9
UD
645 if (__fxstat (_STAT_VER, fd, &st) < 0)
646 LOSE (errno, "cannot stat shared object");
d66e34cd
RM
647
648 /* Look again to see if the real name matched another already loaded. */
649 for (l = _dl_loaded; l; l = l->l_next)
61e0617a 650 if (l->l_ino == st.st_ino && l->l_dev == st.st_dev)
d66e34cd
RM
651 {
652 /* The object is already loaded.
653 Just bump its reference count and return it. */
266180eb 654 __close (fd);
c84142e8
UD
655
656 /* If the name is not in the list of names for this object add
657 it. */
ea03559a 658 free (realname);
0413b54c 659 add_name_to_object (l, name);
d66e34cd
RM
660 ++l->l_opencount;
661 return l;
662 }
663
8193034b
UD
664 /* Print debugging message. */
665 if (_dl_debug_files)
666 _dl_debug_message (1, "file=", name, "; generating link map\n", NULL);
667
32c85e43
UD
668 /* Read the header directly. */
669 readbuf = alloca (_dl_pagesize);
670 readlength = __libc_read (fd, readbuf, _dl_pagesize);
126b06f9
UD
671 if (readlength < (ssize_t) sizeof (*header))
672 LOSE (errno, "cannot read file data");
32c85e43 673 header = (void *) readbuf;
d66e34cd 674
d66e34cd 675 /* Check the header for basic validity. */
126b06f9
UD
676 if (memcmp (header->e_ident, expected, EI_PAD) != 0)
677 {
678 /* Something is wrong. */
679 if (*(Elf32_Word *) &header->e_ident !=
c4b72918 680#if BYTE_ORDER == LITTLE_ENDIAN
126b06f9
UD
681 ((ELFMAG0 << (EI_MAG0 * 8)) |
682 (ELFMAG1 << (EI_MAG1 * 8)) |
683 (ELFMAG2 << (EI_MAG2 * 8)) |
684 (ELFMAG3 << (EI_MAG3 * 8)))
c4b72918 685#else
126b06f9
UD
686 ((ELFMAG0 << (EI_MAG3 * 8)) |
687 (ELFMAG1 << (EI_MAG2 * 8)) |
688 (ELFMAG2 << (EI_MAG1 * 8)) |
689 (ELFMAG3 << (EI_MAG0 * 8)))
c4b72918 690#endif
126b06f9
UD
691 )
692 LOSE (0, "invalid ELF header");
693 if (header->e_ident[EI_CLASS] != ELFW(CLASS))
694 LOSE (0, "ELF file class not " STRING(__ELF_NATIVE_CLASS) "-bit");
695 if (header->e_ident[EI_DATA] != byteorder)
696 LOSE (0, "ELF file data encoding not " byteorder_name);
697 if (header->e_ident[EI_VERSION] != EV_CURRENT)
698 LOSE (0, "ELF file version ident not " STRING(EV_CURRENT));
699 /* XXX We should be able so set system specific versions which are
700 allowed here. */
701 if (header->e_ident[EI_OSABI] != ELFOSABI_SYSV)
702 LOSE (0, "ELF file OS ABI not " STRING(ELFOSABI_SYSV));
703 if (header->e_ident[EI_ABIVERSION] != 0)
704 LOSE (0, "ELF file ABI version not 0");
705 LOSE (0, "internal error");
706 }
707
d66e34cd 708 if (header->e_version != EV_CURRENT)
126b06f9 709 LOSE (0, "ELF file version not " STRING(EV_CURRENT));
d66e34cd 710 if (! elf_machine_matches_host (header->e_machine))
126b06f9 711 LOSE (0, "ELF file machine architecture not " ELF_MACHINE_NAME);
266180eb 712 if (header->e_phentsize != sizeof (ElfW(Phdr)))
126b06f9 713 LOSE (0, "ELF file's phentsize not the expected size");
d66e34cd 714
2064087b 715#ifndef MAP_ANON
126b06f9 716# define MAP_ANON 0
d66e34cd
RM
717 if (_dl_zerofd == -1)
718 {
719 _dl_zerofd = _dl_sysdep_open_zero_fill ();
720 if (_dl_zerofd == -1)
ba79d61b
RM
721 {
722 __close (fd);
723 _dl_signal_error (errno, NULL, "cannot open zero fill device");
724 }
d66e34cd 725 }
2064087b 726#endif
d66e34cd 727
ba79d61b 728 /* Enter the new object in the list of loaded objects. */
be935610 729 l = _dl_new_object (realname, name, l_type, loader);
ba79d61b 730 if (! l)
126b06f9 731 LOSE (ENOMEM, "cannot create shared object descriptor");
ba79d61b 732 l->l_opencount = 1;
ba79d61b 733
b122c703 734 /* Extract the remaining details we need from the ELF header
32c85e43 735 and then read in the program header table. */
b122c703
RM
736 l->l_entry = header->e_entry;
737 type = header->e_type;
738 l->l_phnum = header->e_phnum;
32c85e43
UD
739
740 maplength = header->e_phnum * sizeof (ElfW(Phdr));
741 if (header->e_phoff + maplength <= readlength)
742 phdr = (void *) (readbuf + header->e_phoff);
743 else
744 {
745 phdr = alloca (maplength);
746 __lseek (fd, SEEK_SET, header->e_phoff);
747 if (__libc_read (fd, (void *) phdr, maplength) != maplength)
126b06f9 748 LOSE (errno, "cannot read file data");
32c85e43 749 }
879bf2e6 750
b122c703
RM
751 {
752 /* Scan the program header table, collecting its load commands. */
753 struct loadcmd
754 {
266180eb 755 ElfW(Addr) mapstart, mapend, dataend, allocend;
b122c703
RM
756 off_t mapoff;
757 int prot;
758 } loadcmds[l->l_phnum], *c;
759 size_t nloadcmds = 0;
d66e34cd 760
126b06f9 761 /* The struct is initialized to zero so this is not necessary:
d66e34cd 762 l->l_ld = 0;
b122c703 763 l->l_phdr = 0;
126b06f9 764 l->l_addr = 0; */
d66e34cd
RM
765 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
766 switch (ph->p_type)
767 {
768 /* These entries tell us where to find things once the file's
769 segments are mapped in. We record the addresses it says
770 verbatim, and later correct for the run-time load address. */
771 case PT_DYNAMIC:
772 l->l_ld = (void *) ph->p_vaddr;
773 break;
774 case PT_PHDR:
775 l->l_phdr = (void *) ph->p_vaddr;
776 break;
777
778 case PT_LOAD:
b122c703
RM
779 /* A load command tells us to map in part of the file.
780 We record the load commands and process them all later. */
266180eb 781 if (ph->p_align % _dl_pagesize != 0)
126b06f9 782 LOSE (0, "ELF load command alignment not page-aligned");
d66e34cd 783 if ((ph->p_vaddr - ph->p_offset) % ph->p_align)
126b06f9 784 LOSE (0, "ELF load command address/offset not properly aligned");
d66e34cd 785 {
b122c703
RM
786 struct loadcmd *c = &loadcmds[nloadcmds++];
787 c->mapstart = ph->p_vaddr & ~(ph->p_align - 1);
266180eb
RM
788 c->mapend = ((ph->p_vaddr + ph->p_filesz + _dl_pagesize - 1)
789 & ~(_dl_pagesize - 1));
b122c703
RM
790 c->dataend = ph->p_vaddr + ph->p_filesz;
791 c->allocend = ph->p_vaddr + ph->p_memsz;
792 c->mapoff = ph->p_offset & ~(ph->p_align - 1);
126b06f9
UD
793
794 /* Optimize a common case. */
795 if ((PF_R | PF_W | PF_X) == 7
796 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7)
24d60840 797 c->prot = _dl_pf_to_prot[ph->p_flags & (PF_R | PF_W | PF_X)];
126b06f9
UD
798 else
799 {
800 c->prot = 0;
801 if (ph->p_flags & PF_R)
802 c->prot |= PROT_READ;
803 if (ph->p_flags & PF_W)
804 c->prot |= PROT_WRITE;
805 if (ph->p_flags & PF_X)
806 c->prot |= PROT_EXEC;
807 }
b122c703
RM
808 break;
809 }
810 }
d66e34cd 811
b122c703
RM
812 /* Now process the load commands and map segments into memory. */
813 c = loadcmds;
814
8193034b
UD
815 /* Length of the sections to be loaded. */
816 maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart;
817
b122c703
RM
818 if (type == ET_DYN || type == ET_REL)
819 {
820 /* This is a position-independent shared object. We can let the
821 kernel map it anywhere it likes, but we must have space for all
822 the segments in their specified positions relative to the first.
823 So we map the first segment without MAP_FIXED, but with its
22930c9b
RM
824 extent increased to cover all the segments. Then we remove
825 access from excess portion, and there is known sufficient space
4cca6b86
UD
826 there to remap from the later segments.
827
828 As a refinement, sometimes we have an address that we would
829 prefer to map such objects at; but this is only a preference,
830 the OS can do whatever it likes. */
b122c703 831 caddr_t mapat;
4cca6b86 832 ElfW(Addr) mappref;
f21acc89
UD
833 mappref = (ELF_PREFERRED_ADDRESS (loader, maplength, c->mapstart)
834 - MAP_BASE_ADDR (l));
4cca6b86 835 mapat = map_segment (mappref, maplength, c->prot, 0, c->mapoff);
266180eb 836 l->l_addr = (ElfW(Addr)) mapat - c->mapstart;
b122c703 837
22930c9b
RM
838 /* Change protection on the excess portion to disallow all access;
839 the portions we do not remap later will be inaccessible as if
840 unallocated. Then jump into the normal segment-mapping loop to
841 handle the portion of the segment past the end of the file
842 mapping. */
0d3726c3 843 __mprotect ((caddr_t) (l->l_addr + c->mapend),
266180eb
RM
844 loadcmds[nloadcmds - 1].allocend - c->mapend,
845 0);
052b6a6c
UD
846
847 /* Remember which part of the address space this object uses. */
848 l->l_map_start = c->mapstart + l->l_addr;
849 l->l_map_end = l->l_map_start + maplength;
850
b122c703
RM
851 goto postmap;
852 }
4cca6b86
UD
853 else
854 {
855 /* Notify ELF_PREFERRED_ADDRESS that we have to load this one
856 fixed. */
857 ELF_FIXED_ADDRESS (loader, c->mapstart);
858 }
b122c703 859
052b6a6c
UD
860 /* Remember which part of the address space this object uses. */
861 l->l_map_start = c->mapstart + l->l_addr;
862 l->l_map_end = l->l_map_start + maplength;
863
b122c703
RM
864 while (c < &loadcmds[nloadcmds])
865 {
866 if (c->mapend > c->mapstart)
867 /* Map the segment contents from the file. */
868 map_segment (l->l_addr + c->mapstart, c->mapend - c->mapstart,
869 c->prot, MAP_FIXED, c->mapoff);
870
871 postmap:
872 if (c->allocend > c->dataend)
873 {
874 /* Extra zero pages should appear at the end of this segment,
875 after the data mapped from the file. */
266180eb 876 ElfW(Addr) zero, zeroend, zeropage;
b122c703
RM
877
878 zero = l->l_addr + c->dataend;
879 zeroend = l->l_addr + c->allocend;
266180eb 880 zeropage = (zero + _dl_pagesize - 1) & ~(_dl_pagesize - 1);
d66e34cd 881
b122c703
RM
882 if (zeroend < zeropage)
883 /* All the extra data is in the last page of the segment.
884 We can just zero it. */
885 zeropage = zeroend;
886
887 if (zeropage > zero)
d66e34cd 888 {
b122c703
RM
889 /* Zero the final part of the last page of the segment. */
890 if ((c->prot & PROT_WRITE) == 0)
d66e34cd 891 {
b122c703 892 /* Dag nab it. */
266180eb
RM
893 if (__mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)),
894 _dl_pagesize, c->prot|PROT_WRITE) < 0)
126b06f9 895 LOSE (errno, "cannot change memory protections");
d66e34cd 896 }
b122c703
RM
897 memset ((void *) zero, 0, zeropage - zero);
898 if ((c->prot & PROT_WRITE) == 0)
266180eb
RM
899 __mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)),
900 _dl_pagesize, c->prot);
b122c703 901 }
d66e34cd 902
b122c703
RM
903 if (zeroend > zeropage)
904 {
905 /* Map the remaining zero pages in from the zero fill FD. */
906 caddr_t mapat;
266180eb
RM
907 mapat = __mmap ((caddr_t) zeropage, zeroend - zeropage,
908 c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
2064087b 909 ANONFD, 0);
0413b54c 910 if (mapat == MAP_FAILED)
126b06f9 911 LOSE (errno, "cannot map zero-fill pages");
d66e34cd
RM
912 }
913 }
d66e34cd 914
b122c703 915 ++c;
879bf2e6 916 }
0d3726c3
RM
917
918 if (l->l_phdr == 0)
919 {
920 /* There was no PT_PHDR specified. We need to find the phdr in the
921 load image ourselves. We assume it is in fact in the load image
82d0c099
UD
922 somewhere. */
923 for (c = loadcmds; c < &loadcmds[nloadcmds]; c++)
924 if (c->mapoff <= header->e_phoff
925 && (c->mapend - c->mapstart + c->mapoff
926 >= header->e_phoff + header->e_phnum * sizeof (ElfW(Phdr))))
927 {
928 ElfW(Addr) bof = l->l_addr + c->mapstart;
929 l->l_phdr = (void *) (bof + header->e_phoff - c->mapoff);
930 break;
931 }
932 if (l->l_phdr == 0)
126b06f9 933 LOSE (0, "program headers not contained in any loaded segment");
0d3726c3
RM
934 }
935 else
936 /* Adjust the PT_PHDR value by the runtime load address. */
937 (ElfW(Addr)) l->l_phdr += l->l_addr;
b122c703 938 }
d66e34cd 939
6d9756c9
RM
940 /* We are done mapping in the file. We no longer need the descriptor. */
941 __close (fd);
942
ba79d61b
RM
943 if (l->l_type == lt_library && type == ET_EXEC)
944 l->l_type = lt_executable;
9b8a44cd 945
b122c703
RM
946 if (l->l_ld == 0)
947 {
948 if (type == ET_DYN)
126b06f9 949 LOSE (0, "object file has no dynamic section");
b122c703
RM
950 }
951 else
266180eb 952 (ElfW(Addr)) l->l_ld += l->l_addr;
879bf2e6 953
463e148b
RM
954 l->l_entry += l->l_addr;
955
8193034b
UD
956 if (_dl_debug_files)
957 {
958 const size_t nibbles = sizeof (void *) * 2;
959 char buf1[nibbles + 1];
960 char buf2[nibbles + 1];
961 char buf3[nibbles + 1];
962
963 buf1[nibbles] = '\0';
964 buf2[nibbles] = '\0';
965 buf3[nibbles] = '\0';
966
967 memset (buf1, '0', nibbles);
968 memset (buf2, '0', nibbles);
969 memset (buf3, '0', nibbles);
970 _itoa_word ((unsigned long int) l->l_ld, &buf1[nibbles], 16, 0);
971 _itoa_word ((unsigned long int) l->l_addr, &buf2[nibbles], 16, 0);
972 _itoa_word (maplength, &buf3[nibbles], 16, 0);
973
974 _dl_debug_message (1, " dynamic: 0x", buf1, " base: 0x", buf2,
975 " size: 0x", buf3, "\n", NULL);
976 memset (buf1, '0', nibbles);
977 memset (buf2, '0', nibbles);
978 memset (buf3, ' ', nibbles);
979 _itoa_word ((unsigned long int) l->l_entry, &buf1[nibbles], 16, 0);
980 _itoa_word ((unsigned long int) l->l_phdr, &buf2[nibbles], 16, 0);
981 _itoa_word (l->l_phnum, &buf3[nibbles], 10, 0);
982 _dl_debug_message (1, " entry: 0x", buf1, " phdr: 0x", buf2,
983 " phnum: ", buf3, "\n\n", NULL);
984 }
985
f420344c 986 elf_get_dynamic_info (l->l_ld, l->l_addr, l->l_info);
d66e34cd
RM
987 if (l->l_info[DT_HASH])
988 _dl_setup_hash (l);
989
be935610
UD
990 /* If this object has DT_SYMBOLIC set modify now its scope. We don't
991 have to do this for the main map. */
992 if (l->l_info[DT_SYMBOLIC] && &l->l_searchlist != l->l_scope[0])
993 {
994 /* Create an appropriate searchlist. It contains only this map.
995
996 XXX This is the definition of DT_SYMBOLIC in SysVr4. The old
997 GNU ld.so implementation had a different interpretation which
998 is more reasonable. We are prepared to add this possibility
999 back as part of a GNU extension of the ELF format. */
1000 l->l_symbolic_searchlist.r_list =
1001 (struct link_map **) malloc (sizeof (struct link_map *));
1002
1003 if (l->l_symbolic_searchlist.r_list == NULL)
126b06f9 1004 LOSE (ENOMEM, "cannot create searchlist");
be935610
UD
1005
1006 l->l_symbolic_searchlist.r_list[0] = l;
1007 l->l_symbolic_searchlist.r_nlist = 1;
1008 l->l_symbolic_searchlist.r_duplist = l->l_symbolic_searchlist.r_list;
1009 l->l_symbolic_searchlist.r_nduplist = 1;
1010
1011 /* Now move the existing entries one back. */
1012 memmove (&l->l_scope[1], &l->l_scope[0],
82d0c099 1013 sizeof (l->l_scope) - sizeof (l->l_scope[0]));
be935610
UD
1014
1015 /* Now add the new entry. */
1016 l->l_scope[0] = &l->l_symbolic_searchlist;
1017 }
1018
61e0617a
UD
1019 /* Finally the file information. */
1020 l->l_dev = st.st_dev;
1021 l->l_ino = st.st_ino;
1022
d66e34cd
RM
1023 return l;
1024}
ba79d61b 1025\f
b5efde2f
UD
1026/* Print search path. */
1027static void
1028print_search_path (struct r_search_path_elem **list,
1029 const char *what, const char *name)
1030{
12264bd7 1031 char buf[max_dirnamelen + max_capstrlen];
b5efde2f
UD
1032 int first = 1;
1033
8193034b 1034 _dl_debug_message (1, " search path=", NULL);
b5efde2f
UD
1035
1036 while (*list != NULL && (*list)->what == what) /* Yes, ==. */
1037 {
12264bd7
UD
1038 char *endp = __mempcpy (buf, (*list)->dirname, (*list)->dirnamelen);
1039 size_t cnt;
1040
1041 for (cnt = 0; cnt < ncapstr; ++cnt)
1042 if ((*list)->status[cnt] != nonexisting)
1043 {
1044 char *cp = __mempcpy (endp, capstr[cnt].str, capstr[cnt].len);
143e2b96
UD
1045 if (cp == buf || (cp == buf + 1 && buf[0] == '/'))
1046 cp[0] = '\0';
1047 else
1048 cp[-1] = '\0';
12264bd7
UD
1049 _dl_debug_message (0, first ? "" : ":", buf, NULL);
1050 first = 0;
1051 }
b5efde2f 1052
b5efde2f
UD
1053 ++list;
1054 }
1055
1056 if (name != NULL)
8193034b 1057 _dl_debug_message (0, "\t\t(", what, " from file ",
b5efde2f
UD
1058 name[0] ? name : _dl_argv[0], ")\n", NULL);
1059 else
8193034b 1060 _dl_debug_message (0, "\t\t(", what, ")\n", NULL);
b5efde2f
UD
1061}
1062\f
0a54e401 1063/* Try to open NAME in one of the directories in DIRS.
ba79d61b
RM
1064 Return the fd, or -1. If successful, fill in *REALNAME
1065 with the malloc'd full directory name. */
1066
1067static int
c6222ab9 1068open_path (const char *name, size_t namelen, int preloaded,
0a54e401
UD
1069 struct r_search_path_elem **dirs,
1070 char **realname)
ba79d61b
RM
1071{
1072 char *buf;
0a54e401 1073 int fd = -1;
b5efde2f 1074 const char *current_what = NULL;
ba79d61b 1075
0a54e401 1076 if (dirs == NULL || *dirs == NULL)
ba79d61b 1077 {
c4029823 1078 __set_errno (ENOENT);
ba79d61b
RM
1079 return -1;
1080 }
1081
5431ece5 1082 buf = alloca (max_dirnamelen + max_capstrlen + namelen);
ba79d61b
RM
1083 do
1084 {
0a54e401
UD
1085 struct r_search_path_elem *this_dir = *dirs;
1086 size_t buflen = 0;
12264bd7 1087 size_t cnt;
b0b67c47 1088 char *edp;
ba79d61b 1089
b5efde2f
UD
1090 /* If we are debugging the search for libraries print the path
1091 now if it hasn't happened now. */
1092 if (_dl_debug_libs && current_what != this_dir->what)
1093 {
1094 current_what = this_dir->what;
1095 print_search_path (dirs, current_what, this_dir->where);
1096 }
1097
b0b67c47 1098 edp = (char *) __mempcpy (buf, this_dir->dirname, this_dir->dirnamelen);
12264bd7 1099 for (cnt = 0; fd == -1 && cnt < ncapstr; ++cnt)
fd26970f 1100 {
12264bd7
UD
1101 /* Skip this directory if we know it does not exist. */
1102 if (this_dir->status[cnt] == nonexisting)
1103 continue;
0a54e401 1104
12264bd7 1105 buflen =
b0b67c47 1106 ((char *) __mempcpy (__mempcpy (edp,
12264bd7
UD
1107 capstr[cnt].str, capstr[cnt].len),
1108 name, namelen)
1109 - buf);
1110
1111 /* Print name we try if this is wanted. */
b5efde2f 1112 if (_dl_debug_libs)
8193034b 1113 _dl_debug_message (1, " trying file=", buf, "\n", NULL);
b5efde2f 1114
0a54e401 1115 fd = __open (buf, O_RDONLY);
12264bd7 1116 if (this_dir->status[cnt] == unknown)
6e4c40ba
UD
1117 {
1118 if (fd != -1)
1119 this_dir->status[cnt] = existing;
1120 else
1121 {
1122 /* We failed to open machine dependent library. Let's
1123 test whether there is any directory at all. */
1124 struct stat st;
1125
1126 buf[buflen - namelen - 1] = '\0';
1127
1128 if (__xstat (_STAT_VER, buf, &st) != 0
1129 || ! S_ISDIR (st.st_mode))
1130 /* The directory does not exist or it is no directory. */
1131 this_dir->status[cnt] = nonexisting;
1132 else
1133 this_dir->status[cnt] = existing;
1134 }
1135 }
fd26970f 1136
c6222ab9
UD
1137 if (fd != -1 && preloaded && __libc_enable_secure)
1138 {
1139 /* This is an extra security effort to make sure nobody can
1140 preload broken shared objects which are in the trusted
1141 directories and so exploit the bugs. */
1142 struct stat st;
1143
1144 if (__fxstat (_STAT_VER, fd, &st) != 0
1145 || (st.st_mode & S_ISUID) == 0)
1146 {
1147 /* The shared object cannot be tested for being SUID
1148 or this bit is not set. In this case we must not
1149 use this object. */
1150 __close (fd);
1151 fd = -1;
1152 /* We simply ignore the file, signal this by setting
1153 the error value which would have been set by `open'. */
1154 errno = ENOENT;
1155 }
1156 }
ba79d61b
RM
1157 }
1158
ba79d61b
RM
1159 if (fd != -1)
1160 {
1161 *realname = malloc (buflen);
c6222ab9 1162 if (*realname != NULL)
ba79d61b
RM
1163 {
1164 memcpy (*realname, buf, buflen);
1165 return fd;
1166 }
1167 else
1168 {
1169 /* No memory for the name, we certainly won't be able
1170 to load and link it. */
1171 __close (fd);
1172 return -1;
1173 }
1174 }
1175 if (errno != ENOENT && errno != EACCES)
1176 /* The file exists and is readable, but something went wrong. */
1177 return -1;
1178 }
0a54e401 1179 while (*++dirs != NULL);
ba79d61b
RM
1180
1181 return -1;
1182}
1183
1184/* Map in the shared object file NAME. */
1185
1186struct link_map *
d0fc4041 1187internal_function
c6222ab9
UD
1188_dl_map_object (struct link_map *loader, const char *name, int preloaded,
1189 int type, int trace_mode)
ba79d61b
RM
1190{
1191 int fd;
1192 char *realname;
14bab8de 1193 char *name_copy;
ba79d61b
RM
1194 struct link_map *l;
1195
1196 /* Look for this name among those already loaded. */
1197 for (l = _dl_loaded; l; l = l->l_next)
f41c8091
UD
1198 {
1199 /* If the requested name matches the soname of a loaded object,
1200 use that object. Elide this check for names that have not
1201 yet been opened. */
1202 if (l->l_opencount <= 0)
1203 continue;
1204 if (!_dl_name_match_p (name, l))
1205 {
1206 const char *soname;
1207
1208 if (l->l_info[DT_SONAME] == NULL)
1209 continue;
1210
f420344c 1211 soname = (const void *) (l->l_info[DT_STRTAB]->d_un.d_ptr
f41c8091
UD
1212 + l->l_info[DT_SONAME]->d_un.d_val);
1213 if (strcmp (name, soname) != 0)
1214 continue;
1215
1216 /* We have a match on a new name -- cache it. */
76156ea1 1217 add_name_to_object (l, soname);
f41c8091
UD
1218 }
1219
1220 /* We have a match -- bump the reference count and return it. */
1221 ++l->l_opencount;
1222 return l;
1223 }
ba79d61b 1224
8193034b
UD
1225 /* Display information if we are debugging. */
1226 if (_dl_debug_files && loader != NULL)
1227 _dl_debug_message (1, "\nfile=", name, "; needed by ",
1228 loader->l_name[0] ? loader->l_name : _dl_argv[0],
1229 "\n", NULL);
1230
ba79d61b
RM
1231 if (strchr (name, '/') == NULL)
1232 {
1233 /* Search for NAME in several places. */
1234
1235 size_t namelen = strlen (name) + 1;
1236
b5efde2f 1237 if (_dl_debug_libs)
8193034b 1238 _dl_debug_message (1, "find library=", name, "; searching\n", NULL);
b5efde2f 1239
ba79d61b 1240 fd = -1;
a23db8e4
RM
1241
1242 /* First try the DT_RPATH of the dependent object that caused NAME
1243 to be loaded. Then that object's dependent, and on up. */
1244 for (l = loader; fd == -1 && l; l = l->l_loader)
ba79d61b 1245 if (l && l->l_info[DT_RPATH])
fd26970f 1246 {
0a54e401
UD
1247 /* Make sure the cache information is available. */
1248 if (l->l_rpath_dirs == NULL)
1249 {
f420344c 1250 size_t ptrval = (l->l_info[DT_STRTAB]->d_un.d_ptr
0a54e401
UD
1251 + l->l_info[DT_RPATH]->d_un.d_val);
1252 l->l_rpath_dirs =
7ef90c15 1253 decompose_rpath ((const char *) ptrval, l);
0a54e401
UD
1254 }
1255
7ef90c15 1256 if (l->l_rpath_dirs != NULL)
c6222ab9
UD
1257 fd = open_path (name, namelen, preloaded, l->l_rpath_dirs,
1258 &realname);
0a54e401
UD
1259 }
1260
7ef90c15 1261 /* If dynamically linked, try the DT_RPATH of the executable itself. */
0a54e401 1262 l = _dl_loaded;
143e2b96 1263 if (fd == -1 && l && l->l_type != lt_loaded && l != loader
7ef90c15 1264 && l->l_rpath_dirs != NULL)
c6222ab9 1265 fd = open_path (name, namelen, preloaded, l->l_rpath_dirs, &realname);
fd26970f 1266
7ef90c15
UD
1267 /* Try the LD_LIBRARY_PATH environment variable. */
1268 if (fd == -1 && env_path_list != NULL)
1269 fd = open_path (name, namelen, preloaded, env_path_list, &realname);
40a55d20 1270
f18edac3
RM
1271 if (fd == -1)
1272 {
1273 /* Check the list of libraries in the file /etc/ld.so.cache,
1274 for compatibility with Linux's ldconfig program. */
1275 extern const char *_dl_load_cache_lookup (const char *name);
1276 const char *cached = _dl_load_cache_lookup (name);
1277 if (cached)
1278 {
1279 fd = __open (cached, O_RDONLY);
1280 if (fd != -1)
1281 {
706074a5
UD
1282 realname = local_strdup (cached);
1283 if (realname == NULL)
f18edac3
RM
1284 {
1285 __close (fd);
1286 fd = -1;
1287 }
1288 }
1289 }
1290 }
0a54e401 1291
a23db8e4 1292 /* Finally, try the default path. */
ba79d61b 1293 if (fd == -1)
c6222ab9 1294 fd = open_path (name, namelen, preloaded, rtld_search_dirs, &realname);
b5efde2f
UD
1295
1296 /* Add another newline when we a tracing the library loading. */
1297 if (_dl_debug_libs)
8193034b 1298 _dl_debug_message (1, "\n", NULL);
ba79d61b
RM
1299 }
1300 else
1301 {
f787edde
UD
1302 /* The path may contain dynamic string tokens. */
1303 realname = (loader
1304 ? expand_dynamic_string_token (loader, name)
1305 : local_strdup (name));
1306 if (realname == NULL)
1307 fd = -1;
1308 else
ba79d61b 1309 {
f787edde
UD
1310 fd = __open (realname, O_RDONLY);
1311 if (fd == -1)
1312 free (realname);
ba79d61b
RM
1313 }
1314 }
1315
1316 if (fd == -1)
46ec036d
UD
1317 {
1318 if (trace_mode)
1319 {
1320 /* We haven't found an appropriate library. But since we
1321 are only interested in the list of libraries this isn't
1322 so severe. Fake an entry with all the information we
1ef32c3d 1323 have. */
fd26970f 1324 static const ElfW(Symndx) dummy_bucket = STN_UNDEF;
46ec036d
UD
1325
1326 /* Enter the new object in the list of loaded objects. */
1327 if ((name_copy = local_strdup (name)) == NULL
be935610 1328 || (l = _dl_new_object (name_copy, name, type, loader)) == NULL)
46ec036d
UD
1329 _dl_signal_error (ENOMEM, name,
1330 "cannot create shared object descriptor");
126b06f9
UD
1331 /* We use an opencount of 0 as a sign for the faked entry.
1332 Since the descriptor is initialized with zero we do not
1333 have do this here.
46ec036d 1334 l->l_opencount = 0;
126b06f9 1335 l->l_reserved = 0; */
fd26970f
UD
1336 l->l_buckets = &dummy_bucket;
1337 l->l_nbuckets = 1;
1338 l->l_relocated = 1;
1339
1340 return l;
46ec036d
UD
1341 }
1342 else
1343 _dl_signal_error (errno, name, "cannot open shared object file");
1344 }
ba79d61b 1345
76156ea1 1346 return _dl_map_object_from_fd (name, fd, realname, loader, type);
ba79d61b 1347}