1 /* Map in a shared object's segments from the file.
2 Copyright (C) 1995, 1996, 1997, 1998 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. */
26 #include <elf/ldsodefs.h>
28 #include <sys/param.h>
30 #include <sys/types.h>
31 #include "dynamic-link.h"
32 #include <stdio-common/_itoa.h>
35 /* On some systems, no flag bits are given to specify file mapping. */
40 /* The right way to map in the shared library files is MAP_COPY, which
41 makes a virtual copy of the data at the time of the mmap call; this
42 guarantees the mapped pages will be consistent even if the file is
43 overwritten. Some losing VM systems like Linux's lack MAP_COPY. All we
44 get is MAP_PRIVATE, which copies each page when it is modified; this
45 means if the file is overwritten, we may at some point get some pages
46 from the new version after starting with pages from the old version. */
48 #define MAP_COPY MAP_PRIVATE
51 /* Some systems link their relocatable objects for another base address
52 than 0. We want to know the base address for these such that we can
53 subtract this address from the segment addresses during mapping.
54 This results in a more efficient address space usage. Defaults to
55 zero for almost all systems. */
57 #define MAP_BASE_ADDR(l) 0
62 #if BYTE_ORDER == BIG_ENDIAN
63 #define byteorder ELFDATA2MSB
64 #define byteorder_name "big-endian"
65 #elif BYTE_ORDER == LITTLE_ENDIAN
66 #define byteorder ELFDATA2LSB
67 #define byteorder_name "little-endian"
69 #error "Unknown BYTE_ORDER " BYTE_ORDER
70 #define byteorder ELFDATANONE
73 #define STRING(x) __STRING (x)
76 /* The fd is not examined when using MAP_ANON. */
80 #define ANONFD _dl_zerofd
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;
88 #ifndef ELF_PREFERRED_ADDRESS
89 #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
91 #ifndef ELF_FIXED_ADDRESS
92 #define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
97 extern const char *_dl_platform;
98 extern size_t _dl_platformlen;
100 /* This is a fake list to store the RPATH information for static
102 static struct r_search_path_elem **fake_path_list;
104 /* List of the hardware capabilities we might end up using. */
105 static const struct r_strlenpair *capstr;
106 static size_t ncapstr;
107 static size_t max_capstrlen;
110 /* Local version of `strdup' function. */
112 local_strdup (const char *s)
114 size_t len = strlen (s) + 1;
115 void *new = malloc (len);
120 return (char *) memcpy (new, s, len);
123 /* Add `name' to the list of names for a particular shared object.
124 `name' is expected to have been allocated with malloc and will
125 be freed if the shared object already has this name.
126 Returns false if the object already had this name. */
129 add_name_to_object (struct link_map *l, char *name)
131 struct libname_list *lnp, *lastp;
132 struct libname_list *newname;
136 /* No more memory. */
137 _dl_signal_error (ENOMEM, NULL, "could not allocate name string");
142 for (lnp = l->l_libname; lnp != NULL; lastp = lnp, lnp = lnp->next)
143 if (strcmp (name, lnp->name) == 0)
149 newname = malloc (sizeof *newname);
152 /* No more memory. */
153 _dl_signal_error (ENOMEM, name, "cannot allocate name record");
157 /* The object should have a libname set from _dl_new_object. */
158 assert (lastp != NULL);
160 newname->name = name;
161 newname->next = NULL;
162 lastp->next = newname;
166 /* All known directories in sorted order. */
167 static struct r_search_path_elem *all_dirs;
169 /* Standard search directories. */
170 static struct r_search_path_elem **rtld_search_dirs;
172 static size_t max_dirnamelen;
174 static inline struct r_search_path_elem **
175 fillin_rpath (char *rpath, struct r_search_path_elem **result, const char *sep,
176 const char **trusted, const char *what, const char *where)
181 while ((cp = __strsep (&rpath, sep)) != NULL)
183 struct r_search_path_elem *dirp;
184 size_t len = strlen (cp);
186 /* `strsep' can pass an empty string. This has to be
187 interpreted as `use the current directory'. */
190 static char curwd[2];
191 cp = strcpy (curwd, ".");
194 /* Remove trailing slashes. */
195 while (len > 1 && cp[len - 1] == '/')
198 /* Make sure we don't use untrusted directories if we run SUID. */
201 const char **trun = trusted;
203 /* All trusted directory must be complete name. */
208 && (memcmp (*trun, cp, len) != 0
209 || ((*trun)[len] != '/' && (*trun)[len + 1] != '\0')))
213 /* It's no trusted directory, skip it. */
221 /* See if this directory is already known. */
222 for (dirp = all_dirs; dirp != NULL; dirp = dirp->next)
223 if (dirp->dirnamelen == len && memcmp (cp, dirp->dirname, len) == 0)
228 /* It is available, see whether it's on our own list. */
230 for (cnt = 0; cnt < nelems; ++cnt)
231 if (result[cnt] == dirp)
235 result[nelems++] = dirp;
241 /* It's a new directory. Create an entry and add it. */
242 dirp = (struct r_search_path_elem *)
243 malloc (sizeof (*dirp) + ncapstr * sizeof (enum r_dir_status));
245 _dl_signal_error (ENOMEM, NULL,
246 "cannot create cache for search path");
249 dirp->dirnamelen = len;
251 if (len > max_dirnamelen)
252 max_dirnamelen = len;
254 /* We have to make sure all the relative directories are never
255 ignored. The current directory might change and all our
256 saved information would be void. */
258 for (cnt = 0; cnt < ncapstr; ++cnt)
259 dirp->status[cnt] = existing;
261 for (cnt = 0; cnt < ncapstr; ++cnt)
262 dirp->status[cnt] = unknown;
267 dirp->next = all_dirs;
270 /* Put it in the result array. */
271 result[nelems++] = dirp;
275 /* Terminate the array. */
276 result[nelems] = NULL;
282 static struct r_search_path_elem **
284 decompose_rpath (const char *rpath, size_t additional_room,
285 const char *what, const char *where)
287 /* Make a copy we can work with. */
288 char *copy = local_strdup (rpath);
290 struct r_search_path_elem **result;
291 /* First count the number of necessary elements in the result array. */
294 for (cp = copy; *cp != '\0'; ++cp)
298 /* Allocate room for the result. NELEMS + 1 + ADDITIONAL_ROOM is an upper
299 limit for the number of necessary entries. */
300 result = (struct r_search_path_elem **) malloc ((nelems + 1
301 + additional_room + 1)
304 _dl_signal_error (ENOMEM, NULL, "cannot create cache for search path");
306 return fillin_rpath (copy, result, ":", NULL, what, where);
312 _dl_init_paths (const char *llp)
314 static const char *system_dirs[] =
316 #include "trusted-dirs.h"
320 struct r_search_path_elem *pelem, **aelem;
323 /* We have in `search_path' the information about the RPATH of the
324 dynamic loader. Now fill in the information about the applications
325 RPATH and the directories addressed by the LD_LIBRARY_PATH environment
329 /* Number of elements in the library path. */
332 /* First determine how many elements the LD_LIBRARY_PATH contents has. */
333 if (llp != NULL && *llp != '\0')
335 /* Simply count the number of colons. */
336 const char *cp = llp;
340 if (*cp == ':' || *cp == ';')
348 /* Get the capabilities. */
349 capstr = _dl_important_hwcaps (_dl_platform, _dl_platformlen,
350 &ncapstr, &max_capstrlen);
352 /* First set up the rest of the default search directory entries. */
353 aelem = rtld_search_dirs = (struct r_search_path_elem **)
354 malloc ((ncapstr + 1) * sizeof (struct r_search_path_elem *));
356 round_size = ((2 * sizeof (struct r_search_path_elem) - 1
357 + ncapstr * sizeof (enum r_dir_status))
358 / sizeof (struct r_search_path_elem));
360 rtld_search_dirs[0] = (struct r_search_path_elem *)
361 malloc ((sizeof (system_dirs) / sizeof (system_dirs[0]) - 1)
362 * round_size * sizeof (struct r_search_path_elem));
363 if (rtld_search_dirs[0] == NULL)
364 _dl_signal_error (ENOMEM, NULL, "cannot create cache for search path");
366 pelem = all_dirs= rtld_search_dirs[0];
367 for (strp = system_dirs; *strp != NULL; ++strp, pelem += round_size)
373 pelem->next = *(strp + 1) == NULL ? NULL : (pelem + round_size);
375 pelem->what = "system search path";
378 pelem->dirnamelen = strlen (pelem->dirname = *strp);
379 if (pelem->dirnamelen > max_dirnamelen)
380 max_dirnamelen = pelem->dirnamelen;
382 if (pelem->dirname[0] != '/')
383 for (cnt = 0; cnt < ncapstr; ++cnt)
384 pelem->status[cnt] = existing;
386 for (cnt = 0; cnt < ncapstr; ++cnt)
387 pelem->status[cnt] = unknown;
394 if (l->l_type != lt_loaded && l->l_info[DT_RPATH])
396 /* Allocate room for the search path and fill in information
399 decompose_rpath ((const char *)
400 (l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr
401 + l->l_info[DT_RPATH]->d_un.d_val),
402 nllp, "RPATH", l->l_name);
406 /* If we have no LD_LIBRARY_PATH and no RPATH we must tell
407 this somehow to prevent we look this up again and again. */
409 l->l_rpath_dirs = (struct r_search_path_elem **) -1l;
412 l->l_rpath_dirs = (struct r_search_path_elem **)
413 malloc ((nllp + 1) * sizeof (*l->l_rpath_dirs));
414 if (l->l_rpath_dirs == NULL)
415 _dl_signal_error (ENOMEM, NULL,
416 "cannot create cache for search path");
417 l->l_rpath_dirs[0] = NULL;
421 /* We don't need to search the list of fake entries which is searched
422 when no dynamic objects were loaded at this time. */
423 fake_path_list = NULL;
427 char *copy = local_strdup (llp);
429 /* Decompose the LD_LIBRARY_PATH and fill in the result.
430 First search for the next place to enter elements. */
431 struct r_search_path_elem **result = l->l_rpath_dirs;
432 while (*result != NULL)
435 /* We need to take care that the LD_LIBRARY_PATH environment
436 variable can contain a semicolon. */
437 (void) fillin_rpath (copy, result, ":;",
438 __libc_enable_secure ? system_dirs : NULL,
439 "LD_LIBRARY_PATH", NULL);
444 /* This is a statically linked program but we still have to
445 take care for the LD_LIBRARY_PATH environment variable. We
446 use a fake link_map entry. This will only contain the
447 l_rpath_dirs information. */
450 fake_path_list = NULL;
453 fake_path_list = (struct r_search_path_elem **)
454 malloc ((nllp + 1) * sizeof (struct r_search_path_elem *));
455 if (fake_path_list == NULL)
456 _dl_signal_error (ENOMEM, NULL,
457 "cannot create cache for search path");
459 (void) fillin_rpath (local_strdup (llp), fake_path_list, ":;",
460 __libc_enable_secure ? system_dirs : NULL,
461 "LD_LIBRARY_PATH", NULL);
467 /* Map in the shared object NAME, actually located in REALNAME, and already
471 _dl_map_object_from_fd (char *name, int fd, char *realname,
472 struct link_map *loader, int l_type)
474 struct link_map *l = NULL;
475 void *file_mapping = NULL;
476 size_t mapping_size = 0;
478 #define LOSE(s) lose (0, (s))
479 void lose (int code, const char *msg)
483 __munmap (file_mapping, mapping_size);
486 /* Remove the stillborn object from the list and free it. */
488 l->l_prev->l_next = l->l_next;
490 l->l_next->l_prev = l->l_prev;
494 _dl_signal_error (code, name, msg);
495 free (name); /* Hmmm. Can this leak memory? Better
496 than a segfault, anyway. */
499 inline caddr_t map_segment (ElfW(Addr) mapstart, size_t len,
500 int prot, int fixed, off_t offset)
502 caddr_t mapat = __mmap ((caddr_t) mapstart, len, prot,
503 fixed|MAP_COPY|MAP_FILE,
505 if (mapat == MAP_FAILED)
506 lose (errno, "failed to map segment from shared object");
510 /* Make sure LOCATION is mapped in. */
511 void *map (off_t location, size_t size)
513 if ((off_t) mapping_size <= location + (off_t) size)
517 __munmap (file_mapping, mapping_size);
518 mapping_size = (location + size + 1 + _dl_pagesize - 1);
519 mapping_size &= ~(_dl_pagesize - 1);
520 result = __mmap (file_mapping, mapping_size, PROT_READ,
521 MAP_COPY|MAP_FILE, fd, 0);
522 if (result == MAP_FAILED)
523 lose (errno, "cannot map file data");
524 file_mapping = result;
526 return file_mapping + location;
529 const ElfW(Ehdr) *header;
530 const ElfW(Phdr) *phdr;
531 const ElfW(Phdr) *ph;
535 /* Look again to see if the real name matched another already loaded. */
536 for (l = _dl_loaded; l; l = l->l_next)
537 if (! strcmp (realname, l->l_name))
539 /* The object is already loaded.
540 Just bump its reference count and return it. */
543 /* If the name is not in the list of names for this object add
546 add_name_to_object (l, name);
551 /* Print debugging message. */
553 _dl_debug_message (1, "file=", name, "; generating link map\n", NULL);
555 /* Map in the first page to read the header. */
556 header = map (0, sizeof *header);
558 /* Check the header for basic validity. */
559 if (*(Elf32_Word *) &header->e_ident !=
560 #if BYTE_ORDER == LITTLE_ENDIAN
561 ((ELFMAG0 << (EI_MAG0 * 8)) |
562 (ELFMAG1 << (EI_MAG1 * 8)) |
563 (ELFMAG2 << (EI_MAG2 * 8)) |
564 (ELFMAG3 << (EI_MAG3 * 8)))
566 ((ELFMAG0 << (EI_MAG3 * 8)) |
567 (ELFMAG1 << (EI_MAG2 * 8)) |
568 (ELFMAG2 << (EI_MAG1 * 8)) |
569 (ELFMAG3 << (EI_MAG0 * 8)))
572 LOSE ("invalid ELF header");
573 #define ELF32_CLASS ELFCLASS32
574 #define ELF64_CLASS ELFCLASS64
575 if (header->e_ident[EI_CLASS] != ELFW(CLASS))
576 LOSE ("ELF file class not " STRING(__ELF_NATIVE_CLASS) "-bit");
577 if (header->e_ident[EI_DATA] != byteorder)
578 LOSE ("ELF file data encoding not " byteorder_name);
579 if (header->e_ident[EI_VERSION] != EV_CURRENT)
580 LOSE ("ELF file version ident not " STRING(EV_CURRENT));
581 if (header->e_version != EV_CURRENT)
582 LOSE ("ELF file version not " STRING(EV_CURRENT));
583 if (! elf_machine_matches_host (header->e_machine))
584 LOSE ("ELF file machine architecture not " ELF_MACHINE_NAME);
585 if (header->e_phentsize != sizeof (ElfW(Phdr)))
586 LOSE ("ELF file's phentsize not the expected size");
590 if (_dl_zerofd == -1)
592 _dl_zerofd = _dl_sysdep_open_zero_fill ();
593 if (_dl_zerofd == -1)
596 _dl_signal_error (errno, NULL, "cannot open zero fill device");
601 /* Enter the new object in the list of loaded objects. */
602 l = _dl_new_object (realname, name, l_type);
604 lose (ENOMEM, "cannot create shared object descriptor");
606 l->l_loader = loader;
608 /* Extract the remaining details we need from the ELF header
609 and then map in the program header table. */
610 l->l_entry = header->e_entry;
611 type = header->e_type;
612 l->l_phnum = header->e_phnum;
613 phdr = map (header->e_phoff, l->l_phnum * sizeof (ElfW(Phdr)));
616 /* Scan the program header table, collecting its load commands. */
619 ElfW(Addr) mapstart, mapend, dataend, allocend;
622 } loadcmds[l->l_phnum], *c;
623 size_t nloadcmds = 0;
628 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
631 /* These entries tell us where to find things once the file's
632 segments are mapped in. We record the addresses it says
633 verbatim, and later correct for the run-time load address. */
635 l->l_ld = (void *) ph->p_vaddr;
638 l->l_phdr = (void *) ph->p_vaddr;
642 /* A load command tells us to map in part of the file.
643 We record the load commands and process them all later. */
644 if (ph->p_align % _dl_pagesize != 0)
645 LOSE ("ELF load command alignment not page-aligned");
646 if ((ph->p_vaddr - ph->p_offset) % ph->p_align)
647 LOSE ("ELF load command address/offset not properly aligned");
649 struct loadcmd *c = &loadcmds[nloadcmds++];
650 c->mapstart = ph->p_vaddr & ~(ph->p_align - 1);
651 c->mapend = ((ph->p_vaddr + ph->p_filesz + _dl_pagesize - 1)
652 & ~(_dl_pagesize - 1));
653 c->dataend = ph->p_vaddr + ph->p_filesz;
654 c->allocend = ph->p_vaddr + ph->p_memsz;
655 c->mapoff = ph->p_offset & ~(ph->p_align - 1);
657 if (ph->p_flags & PF_R)
658 c->prot |= PROT_READ;
659 if (ph->p_flags & PF_W)
660 c->prot |= PROT_WRITE;
661 if (ph->p_flags & PF_X)
662 c->prot |= PROT_EXEC;
667 /* We are done reading the file's headers now. Unmap them. */
668 __munmap (file_mapping, mapping_size);
670 /* Now process the load commands and map segments into memory. */
673 /* Length of the sections to be loaded. */
674 maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart;
676 if (type == ET_DYN || type == ET_REL)
678 /* This is a position-independent shared object. We can let the
679 kernel map it anywhere it likes, but we must have space for all
680 the segments in their specified positions relative to the first.
681 So we map the first segment without MAP_FIXED, but with its
682 extent increased to cover all the segments. Then we remove
683 access from excess portion, and there is known sufficient space
684 there to remap from the later segments.
686 As a refinement, sometimes we have an address that we would
687 prefer to map such objects at; but this is only a preference,
688 the OS can do whatever it likes. */
691 mappref = (ELF_PREFERRED_ADDRESS (loader, maplength, c->mapstart)
692 - MAP_BASE_ADDR (l));
693 mapat = map_segment (mappref, maplength, c->prot, 0, c->mapoff);
694 l->l_addr = (ElfW(Addr)) mapat - c->mapstart;
696 /* Change protection on the excess portion to disallow all access;
697 the portions we do not remap later will be inaccessible as if
698 unallocated. Then jump into the normal segment-mapping loop to
699 handle the portion of the segment past the end of the file
701 __mprotect ((caddr_t) (l->l_addr + c->mapend),
702 loadcmds[nloadcmds - 1].allocend - c->mapend,
708 /* Notify ELF_PREFERRED_ADDRESS that we have to load this one
710 ELF_FIXED_ADDRESS (loader, c->mapstart);
713 while (c < &loadcmds[nloadcmds])
715 if (c->mapend > c->mapstart)
716 /* Map the segment contents from the file. */
717 map_segment (l->l_addr + c->mapstart, c->mapend - c->mapstart,
718 c->prot, MAP_FIXED, c->mapoff);
721 if (c->allocend > c->dataend)
723 /* Extra zero pages should appear at the end of this segment,
724 after the data mapped from the file. */
725 ElfW(Addr) zero, zeroend, zeropage;
727 zero = l->l_addr + c->dataend;
728 zeroend = l->l_addr + c->allocend;
729 zeropage = (zero + _dl_pagesize - 1) & ~(_dl_pagesize - 1);
731 if (zeroend < zeropage)
732 /* All the extra data is in the last page of the segment.
733 We can just zero it. */
738 /* Zero the final part of the last page of the segment. */
739 if ((c->prot & PROT_WRITE) == 0)
742 if (__mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)),
743 _dl_pagesize, c->prot|PROT_WRITE) < 0)
744 lose (errno, "cannot change memory protections");
746 memset ((void *) zero, 0, zeropage - zero);
747 if ((c->prot & PROT_WRITE) == 0)
748 __mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)),
749 _dl_pagesize, c->prot);
752 if (zeroend > zeropage)
754 /* Map the remaining zero pages in from the zero fill FD. */
756 mapat = __mmap ((caddr_t) zeropage, zeroend - zeropage,
757 c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
759 if (mapat == MAP_FAILED)
760 lose (errno, "cannot map zero-fill pages");
769 /* There was no PT_PHDR specified. We need to find the phdr in the
770 load image ourselves. We assume it is in fact in the load image
771 somewhere, and that the first load command starts at the
772 beginning of the file and thus contains the ELF file header. */
773 ElfW(Addr) bof = l->l_addr + loadcmds[0].mapstart;
774 assert (loadcmds[0].mapoff == 0);
775 l->l_phdr = (void *) (bof + ((const ElfW(Ehdr) *) bof)->e_phoff);
778 /* Adjust the PT_PHDR value by the runtime load address. */
779 (ElfW(Addr)) l->l_phdr += l->l_addr;
782 /* We are done mapping in the file. We no longer need the descriptor. */
785 if (l->l_type == lt_library && type == ET_EXEC)
786 l->l_type = lt_executable;
791 LOSE ("object file has no dynamic section");
794 (ElfW(Addr)) l->l_ld += l->l_addr;
796 l->l_entry += l->l_addr;
800 const size_t nibbles = sizeof (void *) * 2;
801 char buf1[nibbles + 1];
802 char buf2[nibbles + 1];
803 char buf3[nibbles + 1];
805 buf1[nibbles] = '\0';
806 buf2[nibbles] = '\0';
807 buf3[nibbles] = '\0';
809 memset (buf1, '0', nibbles);
810 memset (buf2, '0', nibbles);
811 memset (buf3, '0', nibbles);
812 _itoa_word ((unsigned long int) l->l_ld, &buf1[nibbles], 16, 0);
813 _itoa_word ((unsigned long int) l->l_addr, &buf2[nibbles], 16, 0);
814 _itoa_word (maplength, &buf3[nibbles], 16, 0);
816 _dl_debug_message (1, " dynamic: 0x", buf1, " base: 0x", buf2,
817 " size: 0x", buf3, "\n", NULL);
818 memset (buf1, '0', nibbles);
819 memset (buf2, '0', nibbles);
820 memset (buf3, ' ', nibbles);
821 _itoa_word ((unsigned long int) l->l_entry, &buf1[nibbles], 16, 0);
822 _itoa_word ((unsigned long int) l->l_phdr, &buf2[nibbles], 16, 0);
823 _itoa_word (l->l_phnum, &buf3[nibbles], 10, 0);
824 _dl_debug_message (1, " entry: 0x", buf1, " phdr: 0x", buf2,
825 " phnum: ", buf3, "\n\n", NULL);
828 elf_get_dynamic_info (l->l_ld, l->l_info);
829 if (l->l_info[DT_HASH])
835 /* Print search path. */
837 print_search_path (struct r_search_path_elem **list,
838 const char *what, const char *name)
840 char buf[max_dirnamelen + max_capstrlen];
843 _dl_debug_message (1, " search path=", NULL);
845 while (*list != NULL && (*list)->what == what) /* Yes, ==. */
847 char *endp = __mempcpy (buf, (*list)->dirname, (*list)->dirnamelen);
850 for (cnt = 0; cnt < ncapstr; ++cnt)
851 if ((*list)->status[cnt] != nonexisting)
853 char *cp = __mempcpy (endp, capstr[cnt].str, capstr[cnt].len);
855 _dl_debug_message (0, first ? "" : ":", buf, NULL);
863 _dl_debug_message (0, "\t\t(", what, " from file ",
864 name[0] ? name : _dl_argv[0], ")\n", NULL);
866 _dl_debug_message (0, "\t\t(", what, ")\n", NULL);
869 /* Try to open NAME in one of the directories in DIRS.
870 Return the fd, or -1. If successful, fill in *REALNAME
871 with the malloc'd full directory name. */
874 open_path (const char *name, size_t namelen, int preloaded,
875 struct r_search_path_elem **dirs,
880 const char *current_what = NULL;
882 if (dirs == NULL || *dirs == NULL)
884 __set_errno (ENOENT);
888 buf = __alloca (max_dirnamelen + max_capstrlen + namelen + 1);
891 struct r_search_path_elem *this_dir = *dirs;
895 /* If we are debugging the search for libraries print the path
896 now if it hasn't happened now. */
897 if (_dl_debug_libs && current_what != this_dir->what)
899 current_what = this_dir->what;
900 print_search_path (dirs, current_what, this_dir->where);
903 for (cnt = 0; fd == -1 && cnt < ncapstr; ++cnt)
905 /* Skip this directory if we know it does not exist. */
906 if (this_dir->status[cnt] == nonexisting)
910 ((char *) __mempcpy (__mempcpy (__mempcpy (buf, this_dir->dirname,
911 this_dir->dirnamelen),
912 capstr[cnt].str, capstr[cnt].len),
916 /* Print name we try if this is wanted. */
918 _dl_debug_message (1, " trying file=", buf, "\n", NULL);
920 fd = __open (buf, O_RDONLY);
921 if (this_dir->status[cnt] == unknown)
923 this_dir->status[cnt] = existing;
926 /* We failed to open machine dependent library. Let's
927 test whether there is any directory at all. */
930 buf[this_dir->dirnamelen
931 + MAX (capstr[cnt].len - 1, 0)] = '\0';
933 if (__xstat (_STAT_VER, buf, &st) != 0
934 || ! S_ISDIR (st.st_mode))
935 /* The directory does not exist ot it is no directory. */
936 this_dir->status[cnt] = nonexisting;
938 this_dir->status[cnt] = existing;
941 if (fd != -1 && preloaded && __libc_enable_secure)
943 /* This is an extra security effort to make sure nobody can
944 preload broken shared objects which are in the trusted
945 directories and so exploit the bugs. */
948 if (__fxstat (_STAT_VER, fd, &st) != 0
949 || (st.st_mode & S_ISUID) == 0)
951 /* The shared object cannot be tested for being SUID
952 or this bit is not set. In this case we must not
956 /* We simply ignore the file, signal this by setting
957 the error value which would have been set by `open'. */
965 *realname = malloc (buflen);
966 if (*realname != NULL)
968 memcpy (*realname, buf, buflen);
973 /* No memory for the name, we certainly won't be able
974 to load and link it. */
979 if (errno != ENOENT && errno != EACCES)
980 /* The file exists and is readable, but something went wrong. */
983 while (*++dirs != NULL);
988 /* Map in the shared object file NAME. */
992 _dl_map_object (struct link_map *loader, const char *name, int preloaded,
993 int type, int trace_mode)
1000 /* Look for this name among those already loaded. */
1001 for (l = _dl_loaded; l; l = l->l_next)
1003 /* If the requested name matches the soname of a loaded object,
1004 use that object. Elide this check for names that have not
1006 if (l->l_opencount <= 0)
1008 if (!_dl_name_match_p (name, l))
1012 if (l->l_info[DT_SONAME] == NULL)
1015 soname = (const char *) (l->l_addr
1016 + l->l_info[DT_STRTAB]->d_un.d_ptr
1017 + l->l_info[DT_SONAME]->d_un.d_val);
1018 if (strcmp (name, soname) != 0)
1021 /* We have a match on a new name -- cache it. */
1022 add_name_to_object (l, local_strdup (soname));
1025 /* We have a match -- bump the reference count and return it. */
1030 /* Display information if we are debugging. */
1031 if (_dl_debug_files && loader != NULL)
1032 _dl_debug_message (1, "\nfile=", name, "; needed by ",
1033 loader->l_name[0] ? loader->l_name : _dl_argv[0],
1036 if (strchr (name, '/') == NULL)
1038 /* Search for NAME in several places. */
1040 size_t namelen = strlen (name) + 1;
1043 _dl_debug_message (1, "find library=", name, "; searching\n", NULL);
1047 /* First try the DT_RPATH of the dependent object that caused NAME
1048 to be loaded. Then that object's dependent, and on up. */
1049 for (l = loader; fd == -1 && l; l = l->l_loader)
1050 if (l && l->l_info[DT_RPATH])
1052 /* Make sure the cache information is available. */
1053 if (l->l_rpath_dirs == NULL)
1055 size_t ptrval = (l->l_addr
1056 + l->l_info[DT_STRTAB]->d_un.d_ptr
1057 + l->l_info[DT_RPATH]->d_un.d_val);
1059 decompose_rpath ((const char *) ptrval, 0,
1060 "RPATH", l->l_name);
1063 if (l->l_rpath_dirs != (struct r_search_path_elem **) -1l)
1064 fd = open_path (name, namelen, preloaded, l->l_rpath_dirs,
1068 /* If dynamically linked, try the DT_RPATH of the executable itself
1069 and the LD_LIBRARY_PATH environment variable. */
1071 if (fd == -1 && l && l->l_type != lt_loaded
1072 && l->l_rpath_dirs != (struct r_search_path_elem **) -1l)
1073 fd = open_path (name, namelen, preloaded, l->l_rpath_dirs, &realname);
1075 /* This is used if a static binary uses dynamic loading and there
1076 is a LD_LIBRARY_PATH given. */
1077 if (fd == -1 && fake_path_list != NULL)
1078 fd = open_path (name, namelen, preloaded, fake_path_list, &realname);
1082 /* Check the list of libraries in the file /etc/ld.so.cache,
1083 for compatibility with Linux's ldconfig program. */
1084 extern const char *_dl_load_cache_lookup (const char *name);
1085 const char *cached = _dl_load_cache_lookup (name);
1088 fd = __open (cached, O_RDONLY);
1091 realname = local_strdup (cached);
1092 if (realname == NULL)
1101 /* Finally, try the default path. */
1103 fd = open_path (name, namelen, preloaded, rtld_search_dirs, &realname);
1105 /* Add another newline when we a tracing the library loading. */
1107 _dl_debug_message (1, "\n", NULL);
1111 fd = __open (name, O_RDONLY);
1114 realname = local_strdup (name);
1115 if (realname == NULL)
1125 name_copy = local_strdup (name);
1126 if (name_copy == NULL)
1137 /* We haven't found an appropriate library. But since we
1138 are only interested in the list of libraries this isn't
1139 so severe. Fake an entry with all the information we
1141 static const ElfW(Symndx) dummy_bucket = STN_UNDEF;
1143 /* Enter the new object in the list of loaded objects. */
1144 if ((name_copy = local_strdup (name)) == NULL
1145 || (l = _dl_new_object (name_copy, name, type)) == NULL)
1146 _dl_signal_error (ENOMEM, name,
1147 "cannot create shared object descriptor");
1148 /* We use an opencount of 0 as a sign for the faked entry. */
1151 l->l_buckets = &dummy_bucket;
1158 _dl_signal_error (errno, name, "cannot open shared object file");
1161 return _dl_map_object_from_fd (name_copy, fd, realname, loader, type);