1 /* Map in a shared object's segments from the file.
2 Copyright (C) 1995, 1996, 1997 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. */
28 #include <sys/types.h>
29 #include "dynamic-link.h"
32 /* On some systems, no flag bits are given to specify file mapping. */
37 /* The right way to map in the shared library files is MAP_COPY, which
38 makes a virtual copy of the data at the time of the mmap call; this
39 guarantees the mapped pages will be consistent even if the file is
40 overwritten. Some losing VM systems like Linux's lack MAP_COPY. All we
41 get is MAP_PRIVATE, which copies each page when it is modified; this
42 means if the file is overwritten, we may at some point get some pages
43 from the new version after starting with pages from the old version. */
45 #define MAP_COPY MAP_PRIVATE
48 /* Some systems link their relocatable objects for another base address
49 than 0. We want to know the base address for these such that we can
50 subtract this address from the segment addresses during mapping.
51 This results in a more efficient address space usage. Defaults to
52 zero for almost all systems. */
54 #define MAP_BASE_ADDR(l) 0
59 #if BYTE_ORDER == BIG_ENDIAN
60 #define byteorder ELFDATA2MSB
61 #define byteorder_name "big-endian"
62 #elif BYTE_ORDER == LITTLE_ENDIAN
63 #define byteorder ELFDATA2LSB
64 #define byteorder_name "little-endian"
66 #error "Unknown BYTE_ORDER " BYTE_ORDER
67 #define byteorder ELFDATANONE
73 /* The fd is not examined when using MAP_ANON. */
77 #define ANONFD _dl_zerofd
80 /* Handle situations where we have a preferred location in memory for
81 the shared objects. */
82 #ifdef ELF_PREFERRED_ADDRESS_DATA
83 ELF_PREFERRED_ADDRESS_DATA
;
85 #ifndef ELF_PREFERRED_ADDRESS
86 #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
88 #ifndef ELF_FIXED_ADDRESS
89 #define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
94 extern const char *_dl_platform
;
95 extern size_t _dl_platformlen
;
97 /* This is a fake list to store the RPATH information for static
99 static struct r_search_path_elem
**fake_path_list
;
102 /* Local version of `strdup' function. */
104 local_strdup (const char *s
)
106 size_t len
= strlen (s
) + 1;
107 void *new = malloc (len
);
112 return (char *) memcpy (new, s
, len
);
115 /* Add `name' to the list of names for a particular shared object.
116 `name' is expected to have been allocated with malloc and will
117 be freed if the shared object already has this name.
118 Returns false if the object already had this name. */
120 add_name_to_object (struct link_map
*l
, char *name
)
122 struct libname_list
*lnp
, *lastp
;
123 struct libname_list
*newname
;
127 /* No more memory. */
128 _dl_signal_error (ENOMEM
, NULL
, _("could not allocate name string"));
133 for (lnp
= l
->l_libname
; lnp
!= NULL
; lastp
= lnp
, lnp
= lnp
->next
)
134 if (strcmp (name
, lnp
->name
) == 0)
140 newname
= malloc (sizeof *newname
);
143 /* No more memory. */
144 _dl_signal_error (ENOMEM
, name
, _("cannot allocate name record"));
148 /* The object should have a libname set from _dl_new_object. */
149 assert (lastp
!= NULL
);
151 newname
->name
= name
;
152 newname
->next
= NULL
;
153 lastp
->next
= newname
;
158 /* Implement cache for search path lookup. */
161 static size_t max_dirnamelen
;
163 static inline struct r_search_path_elem
**
164 fillin_rpath (char *rpath
, struct r_search_path_elem
**result
, const char *sep
,
165 const char **trusted
)
170 while ((cp
= __strsep (&rpath
, sep
)) != NULL
)
172 struct r_search_path_elem
*dirp
;
173 size_t len
= strlen (cp
);
174 /* Remove trailing slashes. */
175 while (len
> 1 && cp
[len
- 1] == '/')
178 /* Make sure we don't use untrusted directories if we run SUID. */
181 const char **trun
= trusted
;
183 /* All trusted directory must be complete name. */
188 && (memcmp (*trun
, cp
, len
) != 0 || (*trun
)[len
] != '\0'))
192 /* It's no trusted directory, skip it. */
200 /* See if this directory is already known. */
201 for (dirp
= all_dirs
; dirp
!= NULL
; dirp
= dirp
->next
)
202 if (dirp
->dirnamelen
== len
&& strcmp (cp
, dirp
->dirname
) == 0)
207 /* It is available, see whether it's in our own list. */
209 for (cnt
= 0; cnt
< nelems
; ++cnt
)
210 if (result
[cnt
] == dirp
)
214 result
[nelems
++] = dirp
;
218 /* It's a new directory. Create an entry and add it. */
219 dirp
= (struct r_search_path_elem
*) malloc (sizeof (*dirp
));
221 _dl_signal_error (ENOMEM
, NULL
,
222 "cannot create cache for search path");
224 dirp
->dirnamelen
= len
;
225 /* We have to make sure all the relative directories are never
226 ignored. The current directory might change and all our
227 saved information would be void. */
228 dirp
->dirstatus
= cp
[0] != '/' ? existing
: unknown
;
230 /* Add the name of the machine dependent directory if a machine
232 if (_dl_platform
!= NULL
)
236 dirp
->machdirnamelen
= len
+ _dl_platformlen
+ 1;
237 tmp
= (char *) malloc (len
+ _dl_platformlen
+ 2);
239 _dl_signal_error (ENOMEM
, NULL
,
240 "cannot create cache for search path");
241 memcpy (tmp
, cp
, len
);
242 memcpy (tmp
+ len
, _dl_platform
, _dl_platformlen
);
243 tmp
[len
+ _dl_platformlen
] = '/';
244 tmp
[len
+ _dl_platformlen
+ 1] = '\0';
247 dirp
->machdirstatus
= dirp
->dirstatus
;
249 if (max_dirnamelen
< dirp
->machdirnamelen
)
250 max_dirnamelen
= dirp
->machdirnamelen
;
256 dirp
->machdirnamelen
= len
;
257 dirp
->machdirstatus
= nonexisting
;
259 tmp
= (char *) malloc (len
+ 1);
261 _dl_signal_error (ENOMEM
, NULL
,
262 "cannot create cache for search path");
263 memcpy (tmp
, cp
, len
);
266 if (max_dirnamelen
< dirp
->dirnamelen
)
267 max_dirnamelen
= dirp
->dirnamelen
;
272 dirp
->next
= all_dirs
;
275 /* Put it in the result array. */
276 result
[nelems
++] = dirp
;
280 /* Terminate the array. */
281 result
[nelems
] = NULL
;
287 static struct r_search_path_elem
**
288 decompose_rpath (const char *rpath
, size_t additional_room
)
290 /* Make a copy we can work with. */
291 char *copy
= strdupa (rpath
);
293 struct r_search_path_elem
**result
;
294 /* First count the number of necessary elements in the result array. */
297 for (cp
= copy
; *cp
!= '\0'; ++cp
)
301 /* Allocate room for the result. NELEMS + 1 + ADDITIONAL_ROOM is an upper
302 limit for the number of necessary entries. */
303 result
= (struct r_search_path_elem
**) malloc ((nelems
+ 1
304 + additional_room
+ 1)
307 _dl_signal_error (ENOMEM
, NULL
, "cannot create cache for search path");
309 return fillin_rpath (copy
, result
, ":", NULL
);
314 _dl_init_paths (void)
316 static const char *trusted_dirs
[] =
318 #include "trusted-dirs.h"
322 struct r_search_path_elem
**pelem
;
324 /* We have in `search_path' the information about the RPATH of the
325 dynamic loader. Now fill in the information about the applications
326 RPATH and the directories addressed by the LD_LIBRARY_PATH environment
330 /* First determine how many elements the LD_LIBRARY_PATH contents has. */
331 const char *llp
= getenv ("LD_LIBRARY_PATH");
334 if (llp
!= NULL
&& *llp
!= '\0')
336 /* Simply count the number of colons. */
337 const char *cp
= llp
;
349 if (l
->l_type
!= lt_loaded
&& l
->l_info
[DT_RPATH
])
351 /* Allocate room for the search path and fill in information
354 decompose_rpath ((const char *)
355 (l
->l_addr
+ l
->l_info
[DT_STRTAB
]->d_un
.d_ptr
356 + l
->l_info
[DT_RPATH
]->d_un
.d_val
),
361 /* If we have no LD_LIBRARY_PATH and no RPATH we must tell
362 this somehow to prevent we look this up again and again. */
364 l
->l_rpath_dirs
= (struct r_search_path_elem
**) -1l;
367 l
->l_rpath_dirs
= (struct r_search_path_elem
**)
368 malloc ((nllp
+ 1) * sizeof (*l
->l_rpath_dirs
));
369 if (l
->l_rpath_dirs
== NULL
)
370 _dl_signal_error (ENOMEM
, NULL
,
371 "cannot create cache for search path");
372 l
->l_rpath_dirs
[0] = NULL
;
376 /* We don't need to search the list of fake entries which is searched
377 when no dynamic objects were loaded at this time. */
378 fake_path_list
= NULL
;
382 char *copy
= strdupa (llp
);
384 /* Decompose the LD_LIBRARY_PATH and fill in the result.
385 First search for the next place to enter elements. */
386 struct r_search_path_elem
**result
= l
->l_rpath_dirs
;
387 while (*result
!= NULL
)
390 /* We need to take care that the LD_LIBRARY_PATH environment
391 variable can contain a semicolon. */
392 (void) fillin_rpath (copy
, result
, ":;",
393 __libc_enable_secure
? trusted_dirs
: NULL
);
398 /* This is a statically linked program but we still have to
399 take care for the LD_LIBRARY_PATH environment variable. We
400 use a fake link_map entry. This will only contain the
401 l_rpath_dirs information. */
404 fake_path_list
= NULL
;
407 fake_path_list
= (struct r_search_path_elem
**)
408 malloc ((nllp
+ 1) * sizeof (struct r_search_path_elem
*));
410 (void) fillin_rpath (local_strdup (llp
), fake_path_list
, ":;",
411 __libc_enable_secure
? trusted_dirs
: NULL
);
415 /* Now set up the rest of the rtld_search_dirs. */
416 for (pelem
= rtld_search_dirs
; *pelem
!= NULL
; ++pelem
)
418 struct r_search_path_elem
*relem
= *pelem
;
420 if (_dl_platform
!= NULL
)
424 relem
->machdirnamelen
= relem
->dirnamelen
+ _dl_platformlen
+ 1;
425 tmp
= (char *) malloc (relem
->machdirnamelen
+ 1);
427 _dl_signal_error (ENOMEM
, NULL
,
428 "cannot create cache for search path");
430 memcpy (tmp
, relem
->dirname
, relem
->dirnamelen
);
431 memcpy (tmp
+ relem
->dirnamelen
, _dl_platform
, _dl_platformlen
);
432 tmp
[relem
->dirnamelen
+ _dl_platformlen
] = '/';
433 tmp
[relem
->dirnamelen
+ _dl_platformlen
+ 1] = '\0';
435 relem
->dirname
= tmp
;
437 relem
->machdirstatus
= unknown
;
439 if (max_dirnamelen
< relem
->machdirnamelen
)
440 max_dirnamelen
= relem
->machdirnamelen
;
444 relem
->machdirnamelen
= relem
->dirnamelen
;
445 relem
->machdirstatus
= nonexisting
;
447 if (max_dirnamelen
< relem
->dirnamelen
)
448 max_dirnamelen
= relem
->dirnamelen
;
454 /* Map in the shared object NAME, actually located in REALNAME, and already
458 _dl_map_object_from_fd (char *name
, int fd
, char *realname
,
459 struct link_map
*loader
, int l_type
)
461 struct link_map
*l
= NULL
;
462 void *file_mapping
= NULL
;
463 size_t mapping_size
= 0;
465 #define LOSE(s) lose (0, (s))
466 void lose (int code
, const char *msg
)
470 __munmap (file_mapping
, mapping_size
);
473 /* Remove the stillborn object from the list and free it. */
475 l
->l_prev
->l_next
= l
->l_next
;
477 l
->l_next
->l_prev
= l
->l_prev
;
481 _dl_signal_error (code
, name
, msg
);
482 free (name
); /* Hmmm. Can this leak memory? Better
483 than a segfault, anyway. */
486 inline caddr_t
map_segment (ElfW(Addr
) mapstart
, size_t len
,
487 int prot
, int fixed
, off_t offset
)
489 caddr_t mapat
= __mmap ((caddr_t
) mapstart
, len
, prot
,
490 fixed
|MAP_COPY
|MAP_FILE
,
492 if (mapat
== MAP_FAILED
)
493 lose (errno
, "failed to map segment from shared object");
497 /* Make sure LOCATION is mapped in. */
498 void *map (off_t location
, size_t size
)
500 if ((off_t
) mapping_size
<= location
+ (off_t
) size
)
504 __munmap (file_mapping
, mapping_size
);
505 mapping_size
= (location
+ size
+ 1 + _dl_pagesize
- 1);
506 mapping_size
&= ~(_dl_pagesize
- 1);
507 result
= __mmap (file_mapping
, mapping_size
, PROT_READ
,
508 MAP_COPY
|MAP_FILE
, fd
, 0);
509 if (result
== MAP_FAILED
)
510 lose (errno
, "cannot map file data");
511 file_mapping
= result
;
513 return file_mapping
+ location
;
516 const ElfW(Ehdr
) *header
;
517 const ElfW(Phdr
) *phdr
;
518 const ElfW(Phdr
) *ph
;
521 /* Look again to see if the real name matched another already loaded. */
522 for (l
= _dl_loaded
; l
; l
= l
->l_next
)
523 if (! strcmp (realname
, l
->l_name
))
525 /* The object is already loaded.
526 Just bump its reference count and return it. */
529 /* If the name is not in the list of names for this object add
532 add_name_to_object (l
, name
);
537 /* Map in the first page to read the header. */
538 header
= map (0, sizeof *header
);
540 /* Check the header for basic validity. */
541 if (*(Elf32_Word
*) &header
->e_ident
!=
542 #if BYTE_ORDER == LITTLE_ENDIAN
543 ((ELFMAG0
<< (EI_MAG0
* 8)) |
544 (ELFMAG1
<< (EI_MAG1
* 8)) |
545 (ELFMAG2
<< (EI_MAG2
* 8)) |
546 (ELFMAG3
<< (EI_MAG3
* 8)))
548 ((ELFMAG0
<< (EI_MAG3
* 8)) |
549 (ELFMAG1
<< (EI_MAG2
* 8)) |
550 (ELFMAG2
<< (EI_MAG1
* 8)) |
551 (ELFMAG3
<< (EI_MAG0
* 8)))
554 LOSE ("invalid ELF header");
555 #define ELF32_CLASS ELFCLASS32
556 #define ELF64_CLASS ELFCLASS64
557 if (header
->e_ident
[EI_CLASS
] != ELFW(CLASS
))
558 LOSE ("ELF file class not " STRING(__ELF_WORDSIZE
) "-bit");
559 if (header
->e_ident
[EI_DATA
] != byteorder
)
560 LOSE ("ELF file data encoding not " byteorder_name
);
561 if (header
->e_ident
[EI_VERSION
] != EV_CURRENT
)
562 LOSE ("ELF file version ident not " STRING(EV_CURRENT
));
563 if (header
->e_version
!= EV_CURRENT
)
564 LOSE ("ELF file version not " STRING(EV_CURRENT
));
565 if (! elf_machine_matches_host (header
->e_machine
))
566 LOSE ("ELF file machine architecture not " ELF_MACHINE_NAME
);
567 if (header
->e_phentsize
!= sizeof (ElfW(Phdr
)))
568 LOSE ("ELF file's phentsize not the expected size");
572 if (_dl_zerofd
== -1)
574 _dl_zerofd
= _dl_sysdep_open_zero_fill ();
575 if (_dl_zerofd
== -1)
578 _dl_signal_error (errno
, NULL
, "cannot open zero fill device");
583 /* Enter the new object in the list of loaded objects. */
584 l
= _dl_new_object (realname
, name
, l_type
);
586 lose (ENOMEM
, "cannot create shared object descriptor");
588 l
->l_loader
= loader
;
590 /* Extract the remaining details we need from the ELF header
591 and then map in the program header table. */
592 l
->l_entry
= header
->e_entry
;
593 type
= header
->e_type
;
594 l
->l_phnum
= header
->e_phnum
;
595 phdr
= map (header
->e_phoff
, l
->l_phnum
* sizeof (ElfW(Phdr
)));
598 /* Scan the program header table, collecting its load commands. */
601 ElfW(Addr
) mapstart
, mapend
, dataend
, allocend
;
604 } loadcmds
[l
->l_phnum
], *c
;
605 size_t nloadcmds
= 0;
610 for (ph
= phdr
; ph
< &phdr
[l
->l_phnum
]; ++ph
)
613 /* These entries tell us where to find things once the file's
614 segments are mapped in. We record the addresses it says
615 verbatim, and later correct for the run-time load address. */
617 l
->l_ld
= (void *) ph
->p_vaddr
;
620 l
->l_phdr
= (void *) ph
->p_vaddr
;
624 /* A load command tells us to map in part of the file.
625 We record the load commands and process them all later. */
626 if (ph
->p_align
% _dl_pagesize
!= 0)
627 LOSE ("ELF load command alignment not page-aligned");
628 if ((ph
->p_vaddr
- ph
->p_offset
) % ph
->p_align
)
629 LOSE ("ELF load command address/offset not properly aligned");
631 struct loadcmd
*c
= &loadcmds
[nloadcmds
++];
632 c
->mapstart
= ph
->p_vaddr
& ~(ph
->p_align
- 1);
633 c
->mapend
= ((ph
->p_vaddr
+ ph
->p_filesz
+ _dl_pagesize
- 1)
634 & ~(_dl_pagesize
- 1));
635 c
->dataend
= ph
->p_vaddr
+ ph
->p_filesz
;
636 c
->allocend
= ph
->p_vaddr
+ ph
->p_memsz
;
637 c
->mapoff
= ph
->p_offset
& ~(ph
->p_align
- 1);
639 if (ph
->p_flags
& PF_R
)
640 c
->prot
|= PROT_READ
;
641 if (ph
->p_flags
& PF_W
)
642 c
->prot
|= PROT_WRITE
;
643 if (ph
->p_flags
& PF_X
)
644 c
->prot
|= PROT_EXEC
;
649 /* We are done reading the file's headers now. Unmap them. */
650 __munmap (file_mapping
, mapping_size
);
652 /* Now process the load commands and map segments into memory. */
655 if (type
== ET_DYN
|| type
== ET_REL
)
657 /* This is a position-independent shared object. We can let the
658 kernel map it anywhere it likes, but we must have space for all
659 the segments in their specified positions relative to the first.
660 So we map the first segment without MAP_FIXED, but with its
661 extent increased to cover all the segments. Then we remove
662 access from excess portion, and there is known sufficient space
663 there to remap from the later segments.
665 As a refinement, sometimes we have an address that we would
666 prefer to map such objects at; but this is only a preference,
667 the OS can do whatever it likes. */
670 size_t maplength
= loadcmds
[nloadcmds
- 1].allocend
- c
->mapstart
;
671 mappref
= (ELF_PREFERRED_ADDRESS (loader
, maplength
, c
->mapstart
)
672 - MAP_BASE_ADDR (l
));
673 mapat
= map_segment (mappref
, maplength
, c
->prot
, 0, c
->mapoff
);
674 l
->l_addr
= (ElfW(Addr
)) mapat
- c
->mapstart
;
676 /* Change protection on the excess portion to disallow all access;
677 the portions we do not remap later will be inaccessible as if
678 unallocated. Then jump into the normal segment-mapping loop to
679 handle the portion of the segment past the end of the file
681 __mprotect ((caddr_t
) (l
->l_addr
+ c
->mapend
),
682 loadcmds
[nloadcmds
- 1].allocend
- c
->mapend
,
688 /* Notify ELF_PREFERRED_ADDRESS that we have to load this one
690 ELF_FIXED_ADDRESS (loader
, c
->mapstart
);
693 while (c
< &loadcmds
[nloadcmds
])
695 if (c
->mapend
> c
->mapstart
)
696 /* Map the segment contents from the file. */
697 map_segment (l
->l_addr
+ c
->mapstart
, c
->mapend
- c
->mapstart
,
698 c
->prot
, MAP_FIXED
, c
->mapoff
);
701 if (c
->allocend
> c
->dataend
)
703 /* Extra zero pages should appear at the end of this segment,
704 after the data mapped from the file. */
705 ElfW(Addr
) zero
, zeroend
, zeropage
;
707 zero
= l
->l_addr
+ c
->dataend
;
708 zeroend
= l
->l_addr
+ c
->allocend
;
709 zeropage
= (zero
+ _dl_pagesize
- 1) & ~(_dl_pagesize
- 1);
711 if (zeroend
< zeropage
)
712 /* All the extra data is in the last page of the segment.
713 We can just zero it. */
718 /* Zero the final part of the last page of the segment. */
719 if ((c
->prot
& PROT_WRITE
) == 0)
722 if (__mprotect ((caddr_t
) (zero
& ~(_dl_pagesize
- 1)),
723 _dl_pagesize
, c
->prot
|PROT_WRITE
) < 0)
724 lose (errno
, "cannot change memory protections");
726 memset ((void *) zero
, 0, zeropage
- zero
);
727 if ((c
->prot
& PROT_WRITE
) == 0)
728 __mprotect ((caddr_t
) (zero
& ~(_dl_pagesize
- 1)),
729 _dl_pagesize
, c
->prot
);
732 if (zeroend
> zeropage
)
734 /* Map the remaining zero pages in from the zero fill FD. */
736 mapat
= __mmap ((caddr_t
) zeropage
, zeroend
- zeropage
,
737 c
->prot
, MAP_ANON
|MAP_PRIVATE
|MAP_FIXED
,
739 if (mapat
== MAP_FAILED
)
740 lose (errno
, "cannot map zero-fill pages");
749 /* There was no PT_PHDR specified. We need to find the phdr in the
750 load image ourselves. We assume it is in fact in the load image
751 somewhere, and that the first load command starts at the
752 beginning of the file and thus contains the ELF file header. */
753 ElfW(Addr
) bof
= l
->l_addr
+ loadcmds
[0].mapstart
;
754 assert (loadcmds
[0].mapoff
== 0);
755 l
->l_phdr
= (void *) (bof
+ ((const ElfW(Ehdr
) *) bof
)->e_phoff
);
758 /* Adjust the PT_PHDR value by the runtime load address. */
759 (ElfW(Addr
)) l
->l_phdr
+= l
->l_addr
;
762 /* We are done mapping in the file. We no longer need the descriptor. */
765 if (l
->l_type
== lt_library
&& type
== ET_EXEC
)
766 l
->l_type
= lt_executable
;
771 LOSE ("object file has no dynamic section");
774 (ElfW(Addr
)) l
->l_ld
+= l
->l_addr
;
776 l
->l_entry
+= l
->l_addr
;
778 elf_get_dynamic_info (l
->l_ld
, l
->l_info
);
779 if (l
->l_info
[DT_HASH
])
785 /* Try to open NAME in one of the directories in DIRS.
786 Return the fd, or -1. If successful, fill in *REALNAME
787 with the malloc'd full directory name. */
790 open_path (const char *name
, size_t namelen
,
791 struct r_search_path_elem
**dirs
,
797 if (dirs
== NULL
|| *dirs
== NULL
)
799 __set_errno (ENOENT
);
803 buf
= __alloca (max_dirnamelen
+ namelen
);
806 struct r_search_path_elem
*this_dir
= *dirs
;
809 if (this_dir
->machdirstatus
!= nonexisting
)
811 /* Construct the pathname to try. */
812 (void) memcpy (buf
, this_dir
->dirname
, this_dir
->machdirnamelen
);
813 (void) memcpy (buf
+ this_dir
->machdirnamelen
, name
, namelen
);
814 buflen
= this_dir
->machdirnamelen
+ namelen
;
816 fd
= __open (buf
, O_RDONLY
);
817 if (this_dir
->machdirstatus
== unknown
)
819 this_dir
->machdirstatus
= existing
;
822 /* We failed to open machine dependent library. Let's
823 test whether there is any directory at all. */
826 buf
[this_dir
->machdirnamelen
- 1] = '\0';
828 if (stat (buf
, &st
) != 0 || ! S_ISDIR (st
.st_mode
))
829 /* The directory does not exist ot it is no directory. */
830 this_dir
->machdirstatus
= nonexisting
;
832 this_dir
->machdirstatus
= existing
;
836 if (fd
== -1 && this_dir
->dirstatus
!= nonexisting
)
838 /* Construct the pathname to try. */
839 (void) memcpy (buf
, this_dir
->dirname
, this_dir
->dirnamelen
);
840 (void) memcpy (buf
+ this_dir
->dirnamelen
, name
, namelen
);
841 buflen
= this_dir
->dirnamelen
+ namelen
;
843 fd
= __open (buf
, O_RDONLY
);
844 if (this_dir
->dirstatus
== unknown
)
846 this_dir
->dirstatus
= existing
;
848 /* We failed to open library. Let's test whether there
849 is any directory at all. */
850 if (this_dir
->dirnamelen
<= 1)
851 this_dir
->dirstatus
= existing
;
856 buf
[this_dir
->dirnamelen
- 1] = '\0';
858 if (stat (buf
, &st
) != 0 || ! S_ISDIR (st
.st_mode
))
859 /* The directory does not exist ot it is no directory. */
860 this_dir
->dirstatus
= nonexisting
;
862 this_dir
->dirstatus
= existing
;
868 *realname
= malloc (buflen
);
871 memcpy (*realname
, buf
, buflen
);
876 /* No memory for the name, we certainly won't be able
877 to load and link it. */
882 if (errno
!= ENOENT
&& errno
!= EACCES
)
883 /* The file exists and is readable, but something went wrong. */
886 while (*++dirs
!= NULL
);
891 /* Map in the shared object file NAME. */
894 _dl_map_object (struct link_map
*loader
, const char *name
, int type
,
902 /* Look for this name among those already loaded. */
903 for (l
= _dl_loaded
; l
; l
= l
->l_next
)
904 if (l
->l_opencount
> 0 && _dl_name_match_p (name
, l
) ||
905 /* If the requested name matches the soname of a loaded object,
907 (l
->l_info
[DT_SONAME
] &&
908 ! strcmp (name
, (const char *) (l
->l_addr
+
909 l
->l_info
[DT_STRTAB
]->d_un
.d_ptr
+
910 l
->l_info
[DT_SONAME
]->d_un
.d_val
))))
912 /* The object is already loaded.
913 Just bump its reference count and return it. */
914 const char *soname
= (const char *) (l
->l_addr
+
915 l
->l_info
[DT_STRTAB
]->d_un
.d_ptr
+
916 l
->l_info
[DT_SONAME
]->d_un
.d_val
);
917 add_name_to_object (l
, local_strdup (soname
));
922 if (strchr (name
, '/') == NULL
)
924 /* Search for NAME in several places. */
926 size_t namelen
= strlen (name
) + 1;
930 /* First try the DT_RPATH of the dependent object that caused NAME
931 to be loaded. Then that object's dependent, and on up. */
932 for (l
= loader
; fd
== -1 && l
; l
= l
->l_loader
)
933 if (l
&& l
->l_info
[DT_RPATH
])
935 /* Make sure the cache information is available. */
936 if (l
->l_rpath_dirs
== NULL
)
938 size_t ptrval
= (l
->l_addr
939 + l
->l_info
[DT_STRTAB
]->d_un
.d_ptr
940 + l
->l_info
[DT_RPATH
]->d_un
.d_val
);
942 decompose_rpath ((const char *) ptrval
, 0);
945 if (l
->l_rpath_dirs
!= (struct r_search_path_elem
**) -1l)
946 fd
= open_path (name
, namelen
, l
->l_rpath_dirs
, &realname
);
949 /* If dynamically linked, try the DT_RPATH of the executable itself
950 and the LD_LIBRARY_PATH environment variable. */
952 if (fd
== -1 && l
&& l
->l_type
!= lt_loaded
953 && l
->l_rpath_dirs
!= (struct r_search_path_elem
**) -1l)
954 fd
= open_path (name
, namelen
, l
->l_rpath_dirs
, &realname
);
956 /* This is used if a static binary uses dynamic loading and there
957 is a LD_LIBRARY_PATH given. */
958 if (fd
== -1 && fake_path_list
!= NULL
)
959 fd
= open_path (name
, namelen
, fake_path_list
, &realname
);
963 /* Check the list of libraries in the file /etc/ld.so.cache,
964 for compatibility with Linux's ldconfig program. */
965 extern const char *_dl_load_cache_lookup (const char *name
);
966 const char *cached
= _dl_load_cache_lookup (name
);
969 fd
= __open (cached
, O_RDONLY
);
972 realname
= local_strdup (cached
);
973 if (realname
== NULL
)
982 /* Finally, try the default path. */
984 fd
= open_path (name
, namelen
, rtld_search_dirs
, &realname
);
988 fd
= __open (name
, O_RDONLY
);
991 realname
= local_strdup (name
);
992 if (realname
== NULL
)
1002 name_copy
= local_strdup (name
);
1003 if (name_copy
== NULL
)
1014 /* We haven't found an appropriate library. But since we
1015 are only interested in the list of libraries this isn't
1016 so severe. Fake an entry with all the information we
1018 static const ElfW(Symndx
) dummy_bucket
= STN_UNDEF
;
1020 /* Enter the new object in the list of loaded objects. */
1021 if ((name_copy
= local_strdup (name
)) == NULL
1022 || (l
= _dl_new_object (name_copy
, name
, type
)) == NULL
)
1023 _dl_signal_error (ENOMEM
, name
,
1024 "cannot create shared object descriptor");
1025 /* We use an opencount of 0 as a sign for the faked entry. */
1028 l
->l_buckets
= &dummy_bucket
;
1035 _dl_signal_error (errno
, name
, "cannot open shared object file");
1038 return _dl_map_object_from_fd (name_copy
, fd
, realname
, loader
, type
);