Update.
[glibc.git] / elf / dl-load.c
blob2c506b55c1b8113cce21f99aa16f91f502bdb3e9
1 /* Map in a shared object's segments from the file.
2 Copyright (C) 1995,96,97,98,99,2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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. */
20 #include <elf.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <libintl.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <ldsodefs.h>
28 #include <sys/mman.h>
29 #include <sys/param.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include "dynamic-link.h"
33 #include <stdio-common/_itoa.h>
35 #include <dl-dst.h>
37 /* On some systems, no flag bits are given to specify file mapping. */
38 #ifndef MAP_FILE
39 # define MAP_FILE 0
40 #endif
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
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
63 #include <endian.h>
64 #if BYTE_ORDER == BIG_ENDIAN
65 # define byteorder ELFDATA2MSB
66 #elif BYTE_ORDER == LITTLE_ENDIAN
67 # define byteorder ELFDATA2LSB
68 #else
69 # error "Unknown BYTE_ORDER " BYTE_ORDER
70 # define byteorder ELFDATANONE
71 #endif
73 #define STRING(x) __STRING (x)
75 #ifdef MAP_ANON
76 /* The fd is not examined when using MAP_ANON. */
77 # define ANONFD -1
78 #else
79 int _dl_zerofd = -1;
80 # define ANONFD _dl_zerofd
81 #endif
83 /* Handle situations where we have a preferred location in memory for
84 the shared objects. */
85 #ifdef ELF_PREFERRED_ADDRESS_DATA
86 ELF_PREFERRED_ADDRESS_DATA;
87 #endif
88 #ifndef ELF_PREFERRED_ADDRESS
89 # define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
90 #endif
91 #ifndef ELF_FIXED_ADDRESS
92 # define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
93 #endif
95 size_t _dl_pagesize;
97 extern const char *_dl_platform;
98 extern size_t _dl_platformlen;
100 /* This is the decomposed LD_LIBRARY_PATH search path. */
101 static struct r_search_path_elem **env_path_list;
103 /* List of the hardware capabilities we might end up using. */
104 static const struct r_strlenpair *capstr;
105 static size_t ncapstr;
106 static size_t max_capstrlen;
108 const unsigned char _dl_pf_to_prot[8] =
110 [0] = PROT_NONE,
111 [PF_R] = PROT_READ,
112 [PF_W] = PROT_WRITE,
113 [PF_R | PF_W] = PROT_READ | PROT_WRITE,
114 [PF_X] = PROT_EXEC,
115 [PF_R | PF_X] = PROT_READ | PROT_EXEC,
116 [PF_W | PF_X] = PROT_WRITE | PROT_EXEC,
117 [PF_R | PF_W | PF_X] = PROT_READ | PROT_WRITE | PROT_EXEC
121 /* Get the generated information about the trusted directories. */
122 #include "trusted-dirs.h"
124 static const char system_dirs[] = SYSTEM_DIRS;
125 static const size_t system_dirs_len[] =
127 SYSTEM_DIRS_LEN
131 /* Local version of `strdup' function. */
132 static inline char *
133 local_strdup (const char *s)
135 size_t len = strlen (s) + 1;
136 void *new = malloc (len);
138 if (new == NULL)
139 return NULL;
141 return (char *) memcpy (new, s, len);
145 size_t
146 _dl_dst_count (const char *name, int is_path)
148 const char *const start = name;
149 size_t cnt = 0;
153 size_t len = 1;
155 /* $ORIGIN is not expanded for SUID/GUID programs.
157 Note that it is no bug that the strings in the first two `strncmp'
158 calls are longer than the sequence which is actually tested. */
159 if ((((strncmp (&name[1], "ORIGIN}", 6) == 0
160 && (!__libc_enable_secure
161 || ((name[7] == '\0' || (is_path && name[7] == ':'))
162 && (name == start || (is_path && name[-1] == ':'))))
163 && (len = 7) != 0)
164 || (strncmp (&name[1], "PLATFORM}", 8) == 0 && (len = 9) != 0))
165 && (name[len] == '\0' || name[len] == '/'
166 || (is_path && name[len] == ':')))
167 || (name[1] == '{'
168 && ((strncmp (&name[2], "ORIGIN}", 7) == 0
169 && (!__libc_enable_secure
170 || ((name[9] == '\0' || (is_path && name[9] == ':'))
171 && (name == start || (is_path && name[-1] == ':'))))
172 && (len = 9) != 0)
173 || (strncmp (&name[2], "PLATFORM}", 9) == 0
174 && (len = 11) != 0))))
175 ++cnt;
177 name = strchr (name + len, '$');
179 while (name != NULL);
181 return cnt;
185 char *
186 _dl_dst_substitute (struct link_map *l, const char *name, char *result,
187 int is_path)
189 const char *const start = name;
190 char *last_elem, *wp;
192 /* Now fill the result path. While copying over the string we keep
193 track of the start of the last path element. When we come accross
194 a DST we copy over the value or (if the value is not available)
195 leave the entire path element out. */
196 last_elem = wp = result;
200 if (*name == '$')
202 const char *repl;
203 size_t len;
205 /* Note that it is no bug that the strings in the first two `strncmp'
206 calls are longer than the sequence which is actually tested. */
207 if ((((strncmp (&name[1], "ORIGIN}", 6) == 0 && (len = 7) != 0)
208 || (strncmp (&name[1], "PLATFORM}", 8) == 0 && (len = 9) != 0))
209 && (name[len] == '\0' || name[len] == '/'
210 || (is_path && name[len] == ':')))
211 || (name[1] == '{'
212 && ((strncmp (&name[2], "ORIGIN}", 7) == 0 && (len = 9) != 0)
213 || (strncmp (&name[2], "PLATFORM}", 9) == 0
214 && (len = 11) != 0))))
216 repl = ((len == 7 || name[2] == 'O')
217 ? (__libc_enable_secure
218 && ((name[len] != '\0'
219 && (!is_path || name[len] != ':'))
220 || (name != start
221 && (!is_path || name[-1] != ':')))
222 ? NULL : l->l_origin)
223 : _dl_platform);
225 if (repl != NULL && repl != (const char *) -1)
227 wp = __stpcpy (wp, repl);
228 name += len;
230 else
232 /* We cannot use this path element, the value of the
233 replacement is unknown. */
234 wp = last_elem;
235 name += len;
236 while (*name != '\0' && (!is_path || *name != ':'))
237 ++name;
240 else
241 /* No DST we recognize. */
242 *wp++ = *name++;
244 else if (is_path && *name == ':')
246 *wp++ = *name++;
247 last_elem = wp;
249 else
250 *wp++ = *name++;
252 while (*name != '\0');
254 *wp = '\0';
256 return result;
260 /* Return copy of argument with all recognized dynamic string tokens
261 ($ORIGIN and $PLATFORM for now) replaced. On some platforms it
262 might not be possible to determine the path from which the object
263 belonging to the map is loaded. In this case the path element
264 containing $ORIGIN is left out. */
265 static char *
266 expand_dynamic_string_token (struct link_map *l, const char *s)
268 /* We make two runs over the string. First we determine how large the
269 resulting string is and then we copy it over. Since this is now
270 frequently executed operation we are looking here not for performance
271 but rather for code size. */
272 size_t cnt;
273 size_t total;
274 char *result;
276 /* Determine the number of DST elements. */
277 cnt = DL_DST_COUNT (s, 1);
279 /* If we do not have to replace anything simply copy the string. */
280 if (cnt == 0)
281 return local_strdup (s);
283 /* Determine the length of the substituted string. */
284 total = DL_DST_REQUIRED (l, s, strlen (s), cnt);
286 /* Allocate the necessary memory. */
287 result = (char *) malloc (total + 1);
288 if (result == NULL)
289 return NULL;
291 return DL_DST_SUBSTITUTE (l, s, result, 1);
295 /* Add `name' to the list of names for a particular shared object.
296 `name' is expected to have been allocated with malloc and will
297 be freed if the shared object already has this name.
298 Returns false if the object already had this name. */
299 static void
300 internal_function
301 add_name_to_object (struct link_map *l, const char *name)
303 struct libname_list *lnp, *lastp;
304 struct libname_list *newname;
305 size_t name_len;
307 lastp = NULL;
308 for (lnp = l->l_libname; lnp != NULL; lastp = lnp, lnp = lnp->next)
309 if (strcmp (name, lnp->name) == 0)
310 return;
312 name_len = strlen (name) + 1;
313 newname = (struct libname_list *) malloc (sizeof *newname + name_len);
314 if (newname == NULL)
316 /* No more memory. */
317 _dl_signal_error (ENOMEM, name, N_("cannot allocate name record"));
318 return;
320 /* The object should have a libname set from _dl_new_object. */
321 assert (lastp != NULL);
323 newname->name = memcpy (newname + 1, name, name_len);
324 newname->next = NULL;
325 lastp->next = newname;
328 /* All known directories in sorted order. */
329 static struct r_search_path_elem *all_dirs;
331 /* Standard search directories. */
332 static struct r_search_path_elem **rtld_search_dirs;
334 static size_t max_dirnamelen;
336 static inline struct r_search_path_elem **
337 fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
338 int check_trusted, const char *what, const char *where)
340 char *cp;
341 size_t nelems = 0;
343 while ((cp = __strsep (&rpath, sep)) != NULL)
345 struct r_search_path_elem *dirp;
346 size_t len = strlen (cp);
348 /* `strsep' can pass an empty string. This has to be
349 interpreted as `use the current directory'. */
350 if (len == 0)
352 static const char curwd[] = "./";
353 cp = (char *) curwd;
356 /* Remove trailing slashes (except for "/"). */
357 while (len > 1 && cp[len - 1] == '/')
358 --len;
360 /* Now add one if there is none so far. */
361 if (len > 0 && cp[len - 1] != '/')
362 cp[len++] = '/';
364 /* Make sure we don't use untrusted directories if we run SUID. */
365 if (check_trusted)
367 const char *trun = system_dirs;
368 size_t idx;
370 /* All trusted directories must be complete names. */
371 if (cp[0] != '/')
372 continue;
374 for (idx = 0;
375 idx < sizeof (system_dirs_len) / sizeof (system_dirs_len[0]);
376 ++idx)
378 if (len == system_dirs_len[idx] && memcmp (trun, cp, len) == 0)
379 /* Found it. */
380 break;
382 trun += system_dirs_len[idx] + 1;
385 if (idx == sizeof (system_dirs_len) / sizeof (system_dirs_len[0]))
386 /* It's no trusted directory, skip it. */
387 continue;
390 /* See if this directory is already known. */
391 for (dirp = all_dirs; dirp != NULL; dirp = dirp->next)
392 if (dirp->dirnamelen == len && memcmp (cp, dirp->dirname, len) == 0)
393 break;
395 if (dirp != NULL)
397 /* It is available, see whether it's on our own list. */
398 size_t cnt;
399 for (cnt = 0; cnt < nelems; ++cnt)
400 if (result[cnt] == dirp)
401 break;
403 if (cnt == nelems)
404 result[nelems++] = dirp;
406 else
408 size_t cnt;
409 enum r_dir_status init_val;
411 /* It's a new directory. Create an entry and add it. */
412 dirp = (struct r_search_path_elem *)
413 malloc (sizeof (*dirp) + ncapstr * sizeof (enum r_dir_status));
414 if (dirp == NULL)
415 _dl_signal_error (ENOMEM, NULL,
416 N_("cannot create cache for search path"));
418 dirp->dirname = cp;
419 dirp->dirnamelen = len;
421 if (len > max_dirnamelen)
422 max_dirnamelen = len;
424 /* We have to make sure all the relative directories are never
425 ignored. The current directory might change and all our
426 saved information would be void. */
427 init_val = cp[0] != '/' ? existing : unknown;
428 for (cnt = 0; cnt < ncapstr; ++cnt)
429 dirp->status[cnt] = init_val;
431 dirp->what = what;
432 dirp->where = where;
434 dirp->next = all_dirs;
435 all_dirs = dirp;
437 /* Put it in the result array. */
438 result[nelems++] = dirp;
442 /* Terminate the array. */
443 result[nelems] = NULL;
445 return result;
449 static struct r_search_path_elem **
450 internal_function
451 decompose_rpath (const char *rpath, struct link_map *l, const char *what)
453 /* Make a copy we can work with. */
454 const char *where = l->l_name;
455 char *copy;
456 char *cp;
457 struct r_search_path_elem **result;
458 size_t nelems;
460 /* First see whether we must forget the RUNPATH and RPATH from this
461 object. */
462 if (_dl_inhibit_rpath != NULL && !__libc_enable_secure)
464 const char *found = strstr (_dl_inhibit_rpath, where);
465 if (found != NULL)
467 size_t len = strlen (where);
468 if ((found == _dl_inhibit_rpath || found[-1] == ':')
469 && (found[len] == '\0' || found[len] == ':'))
471 /* This object is on the list of objects for which the
472 RUNPATH and RPATH must not be used. */
473 result = (struct r_search_path_elem **)
474 malloc (sizeof (*result));
475 if (result == NULL)
476 _dl_signal_error (ENOMEM, NULL,
477 N_("cannot create cache for search path"));
478 result[0] = NULL;
480 return result;
485 /* Make a writable copy. At the same time expand possible dynamic
486 string tokens. */
487 copy = expand_dynamic_string_token (l, rpath);
488 if (copy == NULL)
489 _dl_signal_error (ENOMEM, NULL, N_("cannot create RUNPATH/RPATH copy"));
491 /* Count the number of necessary elements in the result array. */
492 nelems = 0;
493 for (cp = copy; *cp != '\0'; ++cp)
494 if (*cp == ':')
495 ++nelems;
497 /* Allocate room for the result. NELEMS + 1 is an upper limit for the
498 number of necessary entries. */
499 result = (struct r_search_path_elem **) malloc ((nelems + 1 + 1)
500 * sizeof (*result));
501 if (result == NULL)
502 _dl_signal_error (ENOMEM, NULL, N_("cannot create cache for search path"));
504 return fillin_rpath (copy, result, ":", 0, what, where);
508 void
509 internal_function
510 _dl_init_paths (const char *llp)
512 size_t idx;
513 const char *strp;
514 struct r_search_path_elem *pelem, **aelem;
515 size_t round_size;
516 #ifdef SHARED
517 struct link_map *l;
518 #endif
520 /* Fill in the information about the application's RPATH and the
521 directories addressed by the LD_LIBRARY_PATH environment variable. */
523 /* Get the capabilities. */
524 capstr = _dl_important_hwcaps (_dl_platform, _dl_platformlen,
525 &ncapstr, &max_capstrlen);
527 /* First set up the rest of the default search directory entries. */
528 aelem = rtld_search_dirs = (struct r_search_path_elem **)
529 malloc ((sizeof (system_dirs_len) / sizeof (system_dirs_len[0]) + 1)
530 * sizeof (struct r_search_path_elem *));
531 if (rtld_search_dirs == NULL)
532 _dl_signal_error (ENOMEM, NULL, N_("cannot create search path array"));
534 round_size = ((2 * sizeof (struct r_search_path_elem) - 1
535 + ncapstr * sizeof (enum r_dir_status))
536 / sizeof (struct r_search_path_elem));
538 rtld_search_dirs[0] = (struct r_search_path_elem *)
539 malloc ((sizeof (system_dirs) / sizeof (system_dirs[0]))
540 * round_size * sizeof (struct r_search_path_elem));
541 if (rtld_search_dirs[0] == NULL)
542 _dl_signal_error (ENOMEM, NULL, N_("cannot create cache for search path"));
544 pelem = all_dirs = rtld_search_dirs[0];
545 strp = system_dirs;
546 idx = 0;
550 size_t cnt;
552 *aelem++ = pelem;
554 pelem->what = "system search path";
555 pelem->where = NULL;
557 pelem->dirname = strp;
558 pelem->dirnamelen = system_dirs_len[idx];
559 strp += system_dirs_len[idx] + 1;
561 if (pelem->dirname[0] != '/')
562 for (cnt = 0; cnt < ncapstr; ++cnt)
563 pelem->status[cnt] = existing;
564 else
565 for (cnt = 0; cnt < ncapstr; ++cnt)
566 pelem->status[cnt] = unknown;
568 pelem->next = (++idx == (sizeof (system_dirs_len)
569 / sizeof (system_dirs_len[0]))
570 ? NULL : (pelem + round_size));
572 pelem += round_size;
574 while (idx < sizeof (system_dirs_len) / sizeof (system_dirs_len[0]));
576 max_dirnamelen = SYSTEM_DIRS_MAX_LEN;
577 *aelem = NULL;
579 #ifdef SHARED
580 /* This points to the map of the main object. */
581 l = _dl_loaded;
582 if (l != NULL)
584 assert (l->l_type != lt_loaded);
586 if (l->l_info[DT_RUNPATH])
588 /* Allocate room for the search path and fill in information
589 from RUNPATH. */
590 l->l_runpath_dirs =
591 decompose_rpath ((const void *) (D_PTR (l, l_info[DT_STRTAB])
592 + l->l_info[DT_RUNPATH]->d_un.d_val),
593 l, "RUNPATH");
595 /* The RPATH is ignored. */
596 l->l_rpath_dirs = (void *) -1;
598 else
600 l->l_runpath_dirs = (void *) -1;
602 if (l->l_info[DT_RPATH])
603 /* Allocate room for the search path and fill in information
604 from RPATH. */
605 l->l_rpath_dirs =
606 decompose_rpath ((const void *) (D_PTR (l, l_info[DT_STRTAB])
607 + l->l_info[DT_RPATH]->d_un.d_val),
608 l, "RPATH");
609 else
610 l->l_rpath_dirs = (void *) -1;
613 #endif /* SHARED */
615 if (llp != NULL && *llp != '\0')
617 size_t nllp;
618 const char *cp = llp;
620 /* Decompose the LD_LIBRARY_PATH contents. First determine how many
621 elements it has. */
622 nllp = 1;
623 while (*cp)
625 if (*cp == ':' || *cp == ';')
626 ++nllp;
627 ++cp;
630 env_path_list = (struct r_search_path_elem **)
631 malloc ((nllp + 1) * sizeof (struct r_search_path_elem *));
632 if (env_path_list == NULL)
633 _dl_signal_error (ENOMEM, NULL,
634 N_("cannot create cache for search path"));
636 (void) fillin_rpath (local_strdup (llp), env_path_list, ":;",
637 __libc_enable_secure, "LD_LIBRARY_PATH", NULL);
639 if (env_path_list[0] == NULL)
641 free (env_path_list);
642 env_path_list = (void *) -1;
645 else
646 env_path_list = (void *) -1;
650 /* Think twice before changing anything in this function. It is placed
651 here and prepared using the `alloca' magic to prevent it from being
652 inlined. The function is only called in case of an error. But then
653 performance does not count. The function used to be "inlinable" and
654 the compiled did so all the time. This increased the code size for
655 absolutely no good reason. */
656 #define LOSE(code, s) lose (code, fd, name, realname, l, s)
657 static void
658 __attribute__ ((noreturn))
659 lose (int code, int fd, const char *name, char *realname, struct link_map *l,
660 const char *msg)
662 /* The use of `alloca' here looks ridiculous but it helps. The goal
663 is to avoid the function from being inlined. There is no official
664 way to do this so we use this trick. gcc never inlines functions
665 which use `alloca'. */
666 int *a = alloca (sizeof (int));
667 a[0] = fd;
668 (void) __close (a[0]);
669 if (l != NULL)
671 /* Remove the stillborn object from the list and free it. */
672 if (l->l_prev)
673 l->l_prev->l_next = l->l_next;
674 if (l->l_next)
675 l->l_next->l_prev = l->l_prev;
676 free (l);
678 free (realname);
679 _dl_signal_error (code, name, msg);
683 /* Map in the shared object NAME, actually located in REALNAME, and already
684 opened on FD. */
686 #ifndef EXTERNAL_MAP_FROM_FD
687 static
688 #endif
689 struct link_map *
690 _dl_map_object_from_fd (const char *name, int fd, char *realname,
691 struct link_map *loader, int l_type)
693 /* This is the expected ELF header. */
694 #define ELF32_CLASS ELFCLASS32
695 #define ELF64_CLASS ELFCLASS64
696 #ifndef VALID_ELF_HEADER
697 # define VALID_ELF_HEADER(hdr,exp,size) (memcmp (hdr, exp, size) == 0)
698 # define VALID_ELF_OSABI(osabi) (osabi == ELFOSABI_SYSV)
699 # define VALID_ELF_ABIVERSION(ver) (ver == 0)
700 #endif
701 static const unsigned char expected[EI_PAD] =
703 [EI_MAG0] = ELFMAG0,
704 [EI_MAG1] = ELFMAG1,
705 [EI_MAG2] = ELFMAG2,
706 [EI_MAG3] = ELFMAG3,
707 [EI_CLASS] = ELFW(CLASS),
708 [EI_DATA] = byteorder,
709 [EI_VERSION] = EV_CURRENT,
710 [EI_OSABI] = ELFOSABI_SYSV,
711 [EI_ABIVERSION] = 0
713 struct link_map *l = NULL;
715 inline caddr_t map_segment (ElfW(Addr) mapstart, size_t len,
716 int prot, int fixed, off_t offset)
718 caddr_t mapat = __mmap ((caddr_t) mapstart, len, prot,
719 fixed|MAP_COPY|MAP_FILE,
720 fd, offset);
721 if (mapat == MAP_FAILED)
722 LOSE (errno, N_("failed to map segment from shared object"));
723 return mapat;
726 const ElfW(Ehdr) *header;
727 const ElfW(Phdr) *phdr;
728 const ElfW(Phdr) *ph;
729 size_t maplength;
730 int type;
731 char *readbuf;
732 ssize_t readlength;
733 struct stat st;
735 /* Get file information. */
736 if (__fxstat (_STAT_VER, fd, &st) < 0)
737 LOSE (errno, N_("cannot stat shared object"));
739 /* Look again to see if the real name matched another already loaded. */
740 for (l = _dl_loaded; l; l = l->l_next)
741 if (l->l_ino == st.st_ino && l->l_dev == st.st_dev)
743 /* The object is already loaded.
744 Just bump its reference count and return it. */
745 __close (fd);
747 /* If the name is not in the list of names for this object add
748 it. */
749 free (realname);
750 add_name_to_object (l, name);
751 ++l->l_opencount;
752 return l;
755 /* Print debugging message. */
756 if (__builtin_expect (_dl_debug_files, 0))
757 _dl_debug_message (1, "file=", name, "; generating link map\n", NULL);
759 /* Read the header directly. */
760 readbuf = alloca (_dl_pagesize);
761 readlength = __libc_read (fd, readbuf, _dl_pagesize);
762 if (readlength < (ssize_t) sizeof (*header))
763 LOSE (errno, N_("cannot read file data"));
764 header = (void *) readbuf;
766 /* Check the header for basic validity. */
767 if (__builtin_expect (!VALID_ELF_HEADER (header->e_ident, expected, EI_PAD),
770 /* Something is wrong. */
771 if (*(Elf32_Word *) &header->e_ident !=
772 #if BYTE_ORDER == LITTLE_ENDIAN
773 ((ELFMAG0 << (EI_MAG0 * 8)) |
774 (ELFMAG1 << (EI_MAG1 * 8)) |
775 (ELFMAG2 << (EI_MAG2 * 8)) |
776 (ELFMAG3 << (EI_MAG3 * 8)))
777 #else
778 ((ELFMAG0 << (EI_MAG3 * 8)) |
779 (ELFMAG1 << (EI_MAG2 * 8)) |
780 (ELFMAG2 << (EI_MAG1 * 8)) |
781 (ELFMAG3 << (EI_MAG0 * 8)))
782 #endif
784 LOSE (0, N_("invalid ELF header"));
785 if (header->e_ident[EI_CLASS] != ELFW(CLASS))
787 if (__ELF_NATIVE_CLASS == 32)
788 LOSE (0, N_("ELF file class not 32-bit"));
789 else
790 LOSE (0, N_("ELF file class not 64-bit"));
792 if (header->e_ident[EI_DATA] != byteorder)
794 if (BYTE_ORDER == BIG_ENDIAN)
795 LOSE (0, "ELF file data encoding not big-endian");
796 else
797 LOSE (0, "ELF file data encoding not little-endian");
799 if (header->e_ident[EI_VERSION] != EV_CURRENT)
800 LOSE (0, N_("ELF file version ident does not match current one"));
801 /* XXX We should be able so set system specific versions which are
802 allowed here. */
803 if (!VALID_ELF_OSABI (header->e_ident[EI_OSABI]))
804 LOSE (0, N_("ELF file OS ABI invalid."));
805 if (!VALID_ELF_ABIVERSION (header->e_ident[EI_ABIVERSION]))
806 LOSE (0, N_("ELF file ABI version invalid."));
807 LOSE (0, N_("internal error"));
810 if (__builtin_expect (header->e_version, EV_CURRENT) != EV_CURRENT)
811 LOSE (0, N_("ELF file version does not not match current one"));
812 if (! __builtin_expect (elf_machine_matches_host (header->e_machine), 1))
813 LOSE (0, N_("ELF file machine architecture does not match"));
814 if (__builtin_expect (header->e_phentsize, sizeof (ElfW(Phdr)))
815 != sizeof (ElfW(Phdr)))
816 LOSE (0, N_("ELF file's phentsize not the expected size"));
818 #ifndef MAP_ANON
819 # define MAP_ANON 0
820 if (_dl_zerofd == -1)
822 _dl_zerofd = _dl_sysdep_open_zero_fill ();
823 if (_dl_zerofd == -1)
825 __close (fd);
826 _dl_signal_error (errno, NULL, N_("cannot open zero fill device"));
829 #endif
831 /* Enter the new object in the list of loaded objects. */
832 l = _dl_new_object (realname, name, l_type, loader);
833 if (__builtin_expect (! l, 0))
834 LOSE (ENOMEM, N_("cannot create shared object descriptor"));
835 l->l_opencount = 1;
837 /* Extract the remaining details we need from the ELF header
838 and then read in the program header table. */
839 l->l_entry = header->e_entry;
840 type = header->e_type;
841 l->l_phnum = header->e_phnum;
843 maplength = header->e_phnum * sizeof (ElfW(Phdr));
844 if (header->e_phoff + maplength <= readlength)
845 phdr = (void *) (readbuf + header->e_phoff);
846 else
848 phdr = alloca (maplength);
849 __lseek (fd, SEEK_SET, header->e_phoff);
850 if (__libc_read (fd, (void *) phdr, maplength) != maplength)
851 LOSE (errno, N_("cannot read file data"));
855 /* Scan the program header table, collecting its load commands. */
856 struct loadcmd
858 ElfW(Addr) mapstart, mapend, dataend, allocend;
859 off_t mapoff;
860 int prot;
861 } loadcmds[l->l_phnum], *c;
862 size_t nloadcmds = 0;
864 /* The struct is initialized to zero so this is not necessary:
865 l->l_ld = 0;
866 l->l_phdr = 0;
867 l->l_addr = 0; */
868 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
869 switch (ph->p_type)
871 /* These entries tell us where to find things once the file's
872 segments are mapped in. We record the addresses it says
873 verbatim, and later correct for the run-time load address. */
874 case PT_DYNAMIC:
875 l->l_ld = (void *) ph->p_vaddr;
876 l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn));
877 break;
878 case PT_PHDR:
879 l->l_phdr = (void *) ph->p_vaddr;
880 break;
882 case PT_LOAD:
883 /* A load command tells us to map in part of the file.
884 We record the load commands and process them all later. */
885 if (ph->p_align % _dl_pagesize != 0)
886 LOSE (0, N_("ELF load command alignment not page-aligned"));
887 if ((ph->p_vaddr - ph->p_offset) % ph->p_align)
888 LOSE (0,
889 N_("ELF load command address/offset not properly aligned"));
891 struct loadcmd *c = &loadcmds[nloadcmds++];
892 c->mapstart = ph->p_vaddr & ~(ph->p_align - 1);
893 c->mapend = ((ph->p_vaddr + ph->p_filesz + _dl_pagesize - 1)
894 & ~(_dl_pagesize - 1));
895 c->dataend = ph->p_vaddr + ph->p_filesz;
896 c->allocend = ph->p_vaddr + ph->p_memsz;
897 c->mapoff = ph->p_offset & ~(ph->p_align - 1);
899 /* Optimize a common case. */
900 if ((PF_R | PF_W | PF_X) == 7
901 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7)
902 c->prot = _dl_pf_to_prot[ph->p_flags & (PF_R | PF_W | PF_X)];
903 else
905 c->prot = 0;
906 if (ph->p_flags & PF_R)
907 c->prot |= PROT_READ;
908 if (ph->p_flags & PF_W)
909 c->prot |= PROT_WRITE;
910 if (ph->p_flags & PF_X)
911 c->prot |= PROT_EXEC;
913 break;
917 /* Now process the load commands and map segments into memory. */
918 c = loadcmds;
920 /* Length of the sections to be loaded. */
921 maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart;
923 if (type == ET_DYN || type == ET_REL)
925 /* This is a position-independent shared object. We can let the
926 kernel map it anywhere it likes, but we must have space for all
927 the segments in their specified positions relative to the first.
928 So we map the first segment without MAP_FIXED, but with its
929 extent increased to cover all the segments. Then we remove
930 access from excess portion, and there is known sufficient space
931 there to remap from the later segments.
933 As a refinement, sometimes we have an address that we would
934 prefer to map such objects at; but this is only a preference,
935 the OS can do whatever it likes. */
936 ElfW(Addr) mappref;
937 mappref = (ELF_PREFERRED_ADDRESS (loader, maplength, c->mapstart)
938 - MAP_BASE_ADDR (l));
940 /* Remember which part of the address space this object uses. */
941 l->l_map_start = (ElfW(Addr)) map_segment (mappref, maplength, c->prot,
942 0, c->mapoff);
943 l->l_map_end = l->l_map_start + maplength;
944 l->l_addr = l->l_map_start - c->mapstart;
946 /* Change protection on the excess portion to disallow all access;
947 the portions we do not remap later will be inaccessible as if
948 unallocated. Then jump into the normal segment-mapping loop to
949 handle the portion of the segment past the end of the file
950 mapping. */
951 __mprotect ((caddr_t) (l->l_addr + c->mapend),
952 loadcmds[nloadcmds - 1].allocend - c->mapend,
955 goto postmap;
957 else
959 /* Notify ELF_PREFERRED_ADDRESS that we have to load this one
960 fixed. */
961 ELF_FIXED_ADDRESS (loader, c->mapstart);
964 /* Remember which part of the address space this object uses. */
965 l->l_map_start = c->mapstart + l->l_addr;
966 l->l_map_end = l->l_map_start + maplength;
968 while (c < &loadcmds[nloadcmds])
970 if (c->mapend > c->mapstart)
971 /* Map the segment contents from the file. */
972 map_segment (l->l_addr + c->mapstart, c->mapend - c->mapstart,
973 c->prot, MAP_FIXED, c->mapoff);
975 postmap:
976 if (l->l_phdr == 0
977 && c->mapoff <= header->e_phoff
978 && (c->mapend - c->mapstart + c->mapoff
979 >= header->e_phoff + header->e_phnum * sizeof (ElfW(Phdr))))
980 /* Found the program header in this segment. */
981 l->l_phdr = (void *) (c->mapstart + header->e_phoff - c->mapoff);
983 if (c->allocend > c->dataend)
985 /* Extra zero pages should appear at the end of this segment,
986 after the data mapped from the file. */
987 ElfW(Addr) zero, zeroend, zeropage;
989 zero = l->l_addr + c->dataend;
990 zeroend = l->l_addr + c->allocend;
991 zeropage = (zero + _dl_pagesize - 1) & ~(_dl_pagesize - 1);
993 if (zeroend < zeropage)
994 /* All the extra data is in the last page of the segment.
995 We can just zero it. */
996 zeropage = zeroend;
998 if (zeropage > zero)
1000 /* Zero the final part of the last page of the segment. */
1001 if ((c->prot & PROT_WRITE) == 0)
1003 /* Dag nab it. */
1004 if (__mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)),
1005 _dl_pagesize, c->prot|PROT_WRITE) < 0)
1006 LOSE (errno, N_("cannot change memory protections"));
1008 memset ((void *) zero, 0, zeropage - zero);
1009 if ((c->prot & PROT_WRITE) == 0)
1010 __mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)),
1011 _dl_pagesize, c->prot);
1014 if (zeroend > zeropage)
1016 /* Map the remaining zero pages in from the zero fill FD. */
1017 caddr_t mapat;
1018 mapat = __mmap ((caddr_t) zeropage, zeroend - zeropage,
1019 c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
1020 ANONFD, 0);
1021 if (mapat == MAP_FAILED)
1022 LOSE (errno, N_("cannot map zero-fill pages"));
1026 ++c;
1029 if (l->l_phdr == NULL)
1031 /* The program header is not contained in any of the segmenst.
1032 We have to allocate memory ourself and copy it over from
1033 out temporary place. */
1034 ElfW(Phdr) *newp = (ElfW(Phdr) *) malloc (header->e_phnum
1035 * sizeof (ElfW(Phdr)));
1036 if (newp == NULL)
1037 LOSE (ENOMEM, N_("cannot allocate memory for program header"));
1039 l->l_phdr = memcpy (newp, phdr,
1040 (header->e_phnum * sizeof (ElfW(Phdr))));
1041 l->l_phdr_allocated = 1;
1043 else
1044 /* Adjust the PT_PHDR value by the runtime load address. */
1045 (ElfW(Addr)) l->l_phdr += l->l_addr;
1048 /* We are done mapping in the file. We no longer need the descriptor. */
1049 __close (fd);
1051 if (l->l_type == lt_library && type == ET_EXEC)
1052 l->l_type = lt_executable;
1054 if (l->l_ld == 0)
1056 if (type == ET_DYN)
1057 LOSE (0, N_("object file has no dynamic section"));
1059 else
1060 (ElfW(Addr)) l->l_ld += l->l_addr;
1062 l->l_entry += l->l_addr;
1064 if (__builtin_expect (_dl_debug_files, 0))
1066 const size_t nibbles = sizeof (void *) * 2;
1067 char buf1[nibbles + 1];
1068 char buf2[nibbles + 1];
1069 char buf3[nibbles + 1];
1071 buf1[nibbles] = '\0';
1072 buf2[nibbles] = '\0';
1073 buf3[nibbles] = '\0';
1075 memset (buf1, '0', nibbles);
1076 memset (buf2, '0', nibbles);
1077 memset (buf3, '0', nibbles);
1078 _itoa_word ((unsigned long int) l->l_ld, &buf1[nibbles], 16, 0);
1079 _itoa_word ((unsigned long int) l->l_addr, &buf2[nibbles], 16, 0);
1080 _itoa_word (maplength, &buf3[nibbles], 16, 0);
1082 _dl_debug_message (1, " dynamic: 0x", buf1, " base: 0x", buf2,
1083 " size: 0x", buf3, "\n", NULL);
1084 memset (buf1, '0', nibbles);
1085 memset (buf2, '0', nibbles);
1086 memset (buf3, ' ', nibbles);
1087 _itoa_word ((unsigned long int) l->l_entry, &buf1[nibbles], 16, 0);
1088 _itoa_word ((unsigned long int) l->l_phdr, &buf2[nibbles], 16, 0);
1089 _itoa_word (l->l_phnum, &buf3[nibbles], 10, 0);
1090 _dl_debug_message (1, " entry: 0x", buf1, " phdr: 0x", buf2,
1091 " phnum: ", buf3, "\n\n", NULL);
1094 elf_get_dynamic_info (l);
1095 if (l->l_info[DT_HASH])
1096 _dl_setup_hash (l);
1098 /* If this object has DT_SYMBOLIC set modify now its scope. We don't
1099 have to do this for the main map. */
1100 if (l->l_info[DT_SYMBOLIC] && &l->l_searchlist != l->l_scope[0])
1102 /* Create an appropriate searchlist. It contains only this map.
1104 XXX This is the definition of DT_SYMBOLIC in SysVr4. The old
1105 GNU ld.so implementation had a different interpretation which
1106 is more reasonable. We are prepared to add this possibility
1107 back as part of a GNU extension of the ELF format. */
1108 l->l_symbolic_searchlist.r_list =
1109 (struct link_map **) malloc (sizeof (struct link_map *));
1111 if (l->l_symbolic_searchlist.r_list == NULL)
1112 LOSE (ENOMEM, N_("cannot create searchlist"));
1114 l->l_symbolic_searchlist.r_list[0] = l;
1115 l->l_symbolic_searchlist.r_nlist = 1;
1116 l->l_symbolic_searchlist.r_duplist = l->l_symbolic_searchlist.r_list;
1117 l->l_symbolic_searchlist.r_nduplist = 1;
1119 /* Now move the existing entries one back. */
1120 memmove (&l->l_scope[1], &l->l_scope[0],
1121 sizeof (l->l_scope) - sizeof (l->l_scope[0]));
1123 /* Now add the new entry. */
1124 l->l_scope[0] = &l->l_symbolic_searchlist;
1127 /* Finally the file information. */
1128 l->l_dev = st.st_dev;
1129 l->l_ino = st.st_ino;
1131 return l;
1134 /* Print search path. */
1135 static void
1136 print_search_path (struct r_search_path_elem **list,
1137 const char *what, const char *name)
1139 char buf[max_dirnamelen + max_capstrlen];
1140 int first = 1;
1142 _dl_debug_message (1, " search path=", NULL);
1144 while (*list != NULL && (*list)->what == what) /* Yes, ==. */
1146 char *endp = __mempcpy (buf, (*list)->dirname, (*list)->dirnamelen);
1147 size_t cnt;
1149 for (cnt = 0; cnt < ncapstr; ++cnt)
1150 if ((*list)->status[cnt] != nonexisting)
1152 char *cp = __mempcpy (endp, capstr[cnt].str, capstr[cnt].len);
1153 if (cp == buf || (cp == buf + 1 && buf[0] == '/'))
1154 cp[0] = '\0';
1155 else
1156 cp[-1] = '\0';
1157 _dl_debug_message (0, first ? "" : ":", buf, NULL);
1158 first = 0;
1161 ++list;
1164 if (name != NULL)
1165 _dl_debug_message (0, "\t\t(", what, " from file ",
1166 name[0] ? name : _dl_argv[0], ")\n", NULL);
1167 else
1168 _dl_debug_message (0, "\t\t(", what, ")\n", NULL);
1171 /* Try to open NAME in one of the directories in DIRS.
1172 Return the fd, or -1. If successful, fill in *REALNAME
1173 with the malloc'd full directory name. */
1175 static int
1176 open_path (const char *name, size_t namelen, int preloaded,
1177 struct r_search_path_elem ***dirsp,
1178 char **realname)
1180 struct r_search_path_elem **dirs = *dirsp;
1181 char *buf;
1182 int fd = -1;
1183 const char *current_what = NULL;
1184 int any = 0;
1186 buf = alloca (max_dirnamelen + max_capstrlen + namelen);
1189 struct r_search_path_elem *this_dir = *dirs;
1190 size_t buflen = 0;
1191 size_t cnt;
1192 char *edp;
1194 /* If we are debugging the search for libraries print the path
1195 now if it hasn't happened now. */
1196 if (__builtin_expect (_dl_debug_libs, 0)
1197 && current_what != this_dir->what)
1199 current_what = this_dir->what;
1200 print_search_path (dirs, current_what, this_dir->where);
1203 edp = (char *) __mempcpy (buf, this_dir->dirname, this_dir->dirnamelen);
1204 for (cnt = 0; fd == -1 && cnt < ncapstr; ++cnt)
1206 /* Skip this directory if we know it does not exist. */
1207 if (this_dir->status[cnt] == nonexisting)
1208 continue;
1210 buflen =
1211 ((char *) __mempcpy (__mempcpy (edp,
1212 capstr[cnt].str, capstr[cnt].len),
1213 name, namelen)
1214 - buf);
1216 /* Print name we try if this is wanted. */
1217 if (__builtin_expect (_dl_debug_libs, 0))
1218 _dl_debug_message (1, " trying file=", buf, "\n", NULL);
1220 fd = __open (buf, O_RDONLY);
1221 if (this_dir->status[cnt] == unknown)
1223 if (fd != -1)
1224 this_dir->status[cnt] = existing;
1225 else
1227 /* We failed to open machine dependent library. Let's
1228 test whether there is any directory at all. */
1229 struct stat st;
1231 buf[buflen - namelen - 1] = '\0';
1233 if (__xstat (_STAT_VER, buf, &st) != 0
1234 || ! S_ISDIR (st.st_mode))
1235 /* The directory does not exist or it is no directory. */
1236 this_dir->status[cnt] = nonexisting;
1237 else
1238 this_dir->status[cnt] = existing;
1242 /* Remember whether we found any existing directory. */
1243 any |= this_dir->status[cnt] == existing;
1245 if (fd != -1 && preloaded && __libc_enable_secure)
1247 /* This is an extra security effort to make sure nobody can
1248 preload broken shared objects which are in the trusted
1249 directories and so exploit the bugs. */
1250 struct stat st;
1252 if (__fxstat (_STAT_VER, fd, &st) != 0
1253 || (st.st_mode & S_ISUID) == 0)
1255 /* The shared object cannot be tested for being SUID
1256 or this bit is not set. In this case we must not
1257 use this object. */
1258 __close (fd);
1259 fd = -1;
1260 /* We simply ignore the file, signal this by setting
1261 the error value which would have been set by `open'. */
1262 errno = ENOENT;
1267 if (fd != -1)
1269 *realname = malloc (buflen);
1270 if (*realname != NULL)
1272 memcpy (*realname, buf, buflen);
1273 return fd;
1275 else
1277 /* No memory for the name, we certainly won't be able
1278 to load and link it. */
1279 __close (fd);
1280 return -1;
1283 if (errno != ENOENT && errno != EACCES)
1284 /* The file exists and is readable, but something went wrong. */
1285 return -1;
1287 while (*++dirs != NULL);
1289 /* Remove the whole path if none of the directories exists. */
1290 if (! any)
1292 free (*dirsp);
1293 *dirsp = (void *) -1;
1296 return -1;
1299 /* Map in the shared object file NAME. */
1301 struct link_map *
1302 internal_function
1303 _dl_map_object (struct link_map *loader, const char *name, int preloaded,
1304 int type, int trace_mode)
1306 int fd;
1307 char *realname;
1308 char *name_copy;
1309 struct link_map *l;
1311 /* Look for this name among those already loaded. */
1312 for (l = _dl_loaded; l; l = l->l_next)
1314 /* If the requested name matches the soname of a loaded object,
1315 use that object. Elide this check for names that have not
1316 yet been opened. */
1317 if (l->l_opencount <= 0)
1318 continue;
1319 if (!_dl_name_match_p (name, l))
1321 const char *soname;
1323 if (l->l_info[DT_SONAME] == NULL)
1324 continue;
1326 soname = (const void *) (D_PTR (l, l_info[DT_STRTAB])
1327 + l->l_info[DT_SONAME]->d_un.d_val);
1328 if (strcmp (name, soname) != 0)
1329 continue;
1331 /* We have a match on a new name -- cache it. */
1332 add_name_to_object (l, soname);
1335 /* We have a match -- bump the reference count and return it. */
1336 ++l->l_opencount;
1337 return l;
1340 /* Display information if we are debugging. */
1341 if (__builtin_expect (_dl_debug_files, 0) && loader != NULL)
1342 _dl_debug_message (1, "\nfile=", name, "; needed by ",
1343 loader->l_name[0] ? loader->l_name : _dl_argv[0],
1344 "\n", NULL);
1346 if (strchr (name, '/') == NULL)
1348 /* Search for NAME in several places. */
1350 size_t namelen = strlen (name) + 1;
1352 if (__builtin_expect (_dl_debug_libs, 0))
1353 _dl_debug_message (1, "find library=", name, "; searching\n", NULL);
1355 fd = -1;
1357 /* When the object has the RUNPATH information we don't use any
1358 RPATHs. */
1359 if (loader == NULL || loader->l_info[DT_RUNPATH] == NULL)
1361 /* First try the DT_RPATH of the dependent object that caused NAME
1362 to be loaded. Then that object's dependent, and on up. */
1363 for (l = loader; fd == -1 && l; l = l->l_loader)
1365 if (l->l_rpath_dirs == NULL)
1367 if (l->l_info[DT_RPATH] == NULL)
1368 /* There is no path. */
1369 l->l_rpath_dirs = (void *) -1;
1370 else
1372 /* Make sure the cache information is available. */
1373 size_t ptrval = (D_PTR (l, l_info[DT_STRTAB])
1374 + l->l_info[DT_RPATH]->d_un.d_val);
1375 l->l_rpath_dirs =
1376 decompose_rpath ((const char *) ptrval, l,
1377 "RPATH");
1379 if (l->l_rpath_dirs != (void *) -1)
1380 fd = open_path (name, namelen, preloaded,
1381 &l->l_rpath_dirs, &realname);
1384 else if (l->l_rpath_dirs != (void *) -1)
1385 fd = open_path (name, namelen, preloaded, &l->l_rpath_dirs,
1386 &realname);
1389 /* If dynamically linked, try the DT_RPATH of the executable
1390 itself. */
1391 l = _dl_loaded;
1392 if (fd == -1 && l && l->l_type != lt_loaded && l != loader
1393 && l->l_rpath_dirs != (void *) -1)
1394 fd = open_path (name, namelen, preloaded, &l->l_rpath_dirs,
1395 &realname);
1398 /* Try the LD_LIBRARY_PATH environment variable. */
1399 if (fd == -1 && env_path_list != (void *) -1)
1400 fd = open_path (name, namelen, preloaded, &env_path_list, &realname);
1402 /* Look at the RUNPATH informaiton for this binary. */
1403 if (loader != NULL && loader->l_runpath_dirs != (void *) -1)
1405 if (loader->l_runpath_dirs == NULL)
1407 if (loader->l_info[DT_RUNPATH] == NULL)
1408 /* No RUNPATH. */
1409 loader->l_runpath_dirs = (void *) -1;
1410 else
1412 /* Make sure the cache information is available. */
1413 size_t ptrval = (D_PTR (loader, l_info[DT_STRTAB])
1414 + loader->l_info[DT_RUNPATH]->d_un.d_val);
1415 loader->l_runpath_dirs =
1416 decompose_rpath ((const char *) ptrval, loader, "RUNPATH");
1418 if (loader->l_runpath_dirs != (void *) -1)
1419 fd = open_path (name, namelen, preloaded,
1420 &loader->l_runpath_dirs, &realname);
1423 else if (loader->l_runpath_dirs != (void *) -1)
1424 fd = open_path (name, namelen, preloaded,
1425 &loader->l_runpath_dirs, &realname);
1428 if (fd == -1)
1430 /* Check the list of libraries in the file /etc/ld.so.cache,
1431 for compatibility with Linux's ldconfig program. */
1432 extern const char *_dl_load_cache_lookup (const char *name);
1433 const char *cached = _dl_load_cache_lookup (name);
1434 if (cached)
1436 fd = __open (cached, O_RDONLY);
1437 if (fd != -1)
1439 realname = local_strdup (cached);
1440 if (realname == NULL)
1442 __close (fd);
1443 fd = -1;
1449 /* Finally, try the default path. */
1450 if (fd == -1)
1451 fd = open_path (name, namelen, preloaded, &rtld_search_dirs,
1452 &realname);
1454 /* Add another newline when we a tracing the library loading. */
1455 if (__builtin_expect (_dl_debug_libs, 0))
1456 _dl_debug_message (1, "\n", NULL);
1458 else
1460 /* The path may contain dynamic string tokens. */
1461 realname = (loader
1462 ? expand_dynamic_string_token (loader, name)
1463 : local_strdup (name));
1464 if (realname == NULL)
1465 fd = -1;
1466 else
1468 fd = __open (realname, O_RDONLY);
1469 if (fd == -1)
1470 free (realname);
1474 if (fd == -1)
1476 if (trace_mode)
1478 /* We haven't found an appropriate library. But since we
1479 are only interested in the list of libraries this isn't
1480 so severe. Fake an entry with all the information we
1481 have. */
1482 static const Elf_Symndx dummy_bucket = STN_UNDEF;
1484 /* Enter the new object in the list of loaded objects. */
1485 if ((name_copy = local_strdup (name)) == NULL
1486 || (l = _dl_new_object (name_copy, name, type, loader)) == NULL)
1487 _dl_signal_error (ENOMEM, name,
1488 N_("cannot create shared object descriptor"));
1489 /* We use an opencount of 0 as a sign for the faked entry.
1490 Since the descriptor is initialized with zero we do not
1491 have do this here.
1492 l->l_opencount = 0;
1493 l->l_reserved = 0; */
1494 l->l_buckets = &dummy_bucket;
1495 l->l_nbuckets = 1;
1496 l->l_relocated = 1;
1498 return l;
1500 else
1501 _dl_signal_error (errno, name, N_("cannot open shared object file"));
1504 return _dl_map_object_from_fd (name, fd, realname, loader, type);