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. */
29 #include <sys/types.h>
30 #include "dynamic-link.h"
33 /* On some systems, no flag bits are given to specify file mapping. */
38 /* The right way to map in the shared library files is MAP_COPY, which
39 makes a virtual copy of the data at the time of the mmap call; this
40 guarantees the mapped pages will be consistent even if the file is
41 overwritten. Some losing VM systems like Linux's lack MAP_COPY. All we
42 get is MAP_PRIVATE, which copies each page when it is modified; this
43 means if the file is overwritten, we may at some point get some pages
44 from the new version after starting with pages from the old version. */
46 #define MAP_COPY MAP_PRIVATE
49 /* Some systems link their relocatable objects for another base address
50 than 0. We want to know the base address for these such that we can
51 subtract this address from the segment addresses during mapping.
52 This results in a more efficient address space usage. Defaults to
53 zero for almost all systems. */
55 #define MAP_BASE_ADDR(l) 0
60 #if BYTE_ORDER == BIG_ENDIAN
61 #define byteorder ELFDATA2MSB
62 #define byteorder_name "big-endian"
63 #elif BYTE_ORDER == LITTLE_ENDIAN
64 #define byteorder ELFDATA2LSB
65 #define byteorder_name "little-endian"
67 #error "Unknown BYTE_ORDER " BYTE_ORDER
68 #define byteorder ELFDATANONE
71 #define STRING(x) __STRING (x)
74 /* The fd is not examined when using MAP_ANON. */
78 #define ANONFD _dl_zerofd
81 /* Handle situations where we have a preferred location in memory for
82 the shared objects. */
83 #ifdef ELF_PREFERRED_ADDRESS_DATA
84 ELF_PREFERRED_ADDRESS_DATA
;
86 #ifndef ELF_PREFERRED_ADDRESS
87 #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
89 #ifndef ELF_FIXED_ADDRESS
90 #define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
95 extern const char *_dl_platform
;
96 extern size_t _dl_platformlen
;
98 /* This is a fake list to store the RPATH information for static
100 static struct r_search_path_elem
**fake_path_list
;
103 /* Local version of `strdup' function. */
105 local_strdup (const char *s
)
107 size_t len
= strlen (s
) + 1;
108 void *new = malloc (len
);
113 return (char *) memcpy (new, s
, len
);
116 /* Add `name' to the list of names for a particular shared object.
117 `name' is expected to have been allocated with malloc and will
118 be freed if the shared object already has this name.
119 Returns false if the object already had this name. */
121 add_name_to_object (struct link_map
*l
, char *name
)
123 struct libname_list
*lnp
, *lastp
;
124 struct libname_list
*newname
;
128 /* No more memory. */
129 _dl_signal_error (ENOMEM
, NULL
, "could not allocate name string");
134 for (lnp
= l
->l_libname
; lnp
!= NULL
; lastp
= lnp
, lnp
= lnp
->next
)
135 if (strcmp (name
, lnp
->name
) == 0)
141 newname
= malloc (sizeof *newname
);
144 /* No more memory. */
145 _dl_signal_error (ENOMEM
, name
, "cannot allocate name record");
149 /* The object should have a libname set from _dl_new_object. */
150 assert (lastp
!= NULL
);
152 newname
->name
= name
;
153 newname
->next
= NULL
;
154 lastp
->next
= newname
;
159 /* Implement cache for search path lookup. */
162 static size_t max_dirnamelen
;
164 static inline struct r_search_path_elem
**
165 fillin_rpath (char *rpath
, struct r_search_path_elem
**result
, const char *sep
,
166 const char **trusted
, const char *what
, const char *where
)
171 while ((cp
= __strsep (&rpath
, sep
)) != NULL
)
173 struct r_search_path_elem
*dirp
;
174 size_t len
= strlen (cp
);
175 /* Remove trailing slashes. */
176 while (len
> 1 && cp
[len
- 1] == '/')
179 /* Make sure we don't use untrusted directories if we run SUID. */
182 const char **trun
= trusted
;
184 /* All trusted directory must be complete name. */
189 && (memcmp (*trun
, cp
, len
) != 0 || (*trun
)[len
] != '\0'))
193 /* It's no trusted directory, skip it. */
201 /* See if this directory is already known. */
202 for (dirp
= all_dirs
; dirp
!= NULL
; dirp
= dirp
->next
)
203 if (dirp
->dirnamelen
== len
&& strcmp (cp
, dirp
->dirname
) == 0)
208 /* It is available, see whether it's in our own list. */
210 for (cnt
= 0; cnt
< nelems
; ++cnt
)
211 if (result
[cnt
] == dirp
)
215 result
[nelems
++] = dirp
;
219 /* It's a new directory. Create an entry and add it. */
220 dirp
= (struct r_search_path_elem
*) malloc (sizeof (*dirp
));
222 _dl_signal_error (ENOMEM
, NULL
,
223 "cannot create cache for search path");
225 dirp
->dirnamelen
= len
;
226 /* We have to make sure all the relative directories are never
227 ignored. The current directory might change and all our
228 saved information would be void. */
229 dirp
->dirstatus
= cp
[0] != '/' ? existing
: unknown
;
231 /* Add the name of the machine dependent directory if a machine
233 if (_dl_platform
!= NULL
)
237 dirp
->machdirnamelen
= len
+ _dl_platformlen
+ 1;
238 tmp
= (char *) malloc (len
+ _dl_platformlen
+ 2);
240 _dl_signal_error (ENOMEM
, NULL
,
241 "cannot create cache for search path");
243 tmp
= __mempcpy (tmp
, cp
, len
);
244 tmp
= __mempcpy (tmp
, _dl_platform
, _dl_platformlen
);
248 dirp
->machdirstatus
= dirp
->dirstatus
;
250 if (max_dirnamelen
< dirp
->machdirnamelen
)
251 max_dirnamelen
= dirp
->machdirnamelen
;
257 dirp
->machdirnamelen
= len
;
258 dirp
->machdirstatus
= nonexisting
;
260 tmp
= (char *) malloc (len
+ 1);
262 _dl_signal_error (ENOMEM
, NULL
,
263 "cannot create cache for search path");
265 *((char *) __mempcpy (tmp
, cp
, len
)) = '\0';
267 if (max_dirnamelen
< dirp
->dirnamelen
)
268 max_dirnamelen
= dirp
->dirnamelen
;
274 dirp
->next
= all_dirs
;
277 /* Put it in the result array. */
278 result
[nelems
++] = dirp
;
282 /* Terminate the array. */
283 result
[nelems
] = NULL
;
289 static struct r_search_path_elem
**
290 decompose_rpath (const char *rpath
, size_t additional_room
,
291 const char *what
, const char *where
)
293 /* Make a copy we can work with. */
294 char *copy
= strdupa (rpath
);
296 struct r_search_path_elem
**result
;
297 /* First count the number of necessary elements in the result array. */
300 for (cp
= copy
; *cp
!= '\0'; ++cp
)
304 /* Allocate room for the result. NELEMS + 1 + ADDITIONAL_ROOM is an upper
305 limit for the number of necessary entries. */
306 result
= (struct r_search_path_elem
**) malloc ((nelems
+ 1
307 + additional_room
+ 1)
310 _dl_signal_error (ENOMEM
, NULL
, "cannot create cache for search path");
312 return fillin_rpath (copy
, result
, ":", NULL
, what
, where
);
317 _dl_init_paths (const char *llp
)
319 static const char *trusted_dirs
[] =
321 #include "trusted-dirs.h"
325 struct r_search_path_elem
**pelem
;
327 /* We have in `search_path' the information about the RPATH of the
328 dynamic loader. Now fill in the information about the applications
329 RPATH and the directories addressed by the LD_LIBRARY_PATH environment
333 /* Number of elements in the library path. */
336 /* First determine how many elements the LD_LIBRARY_PATH contents has. */
337 if (llp
!= NULL
&& *llp
!= '\0')
339 /* Simply count the number of colons. */
340 const char *cp
= llp
;
352 if (l
->l_type
!= lt_loaded
&& l
->l_info
[DT_RPATH
])
354 /* Allocate room for the search path and fill in information
357 decompose_rpath ((const char *)
358 (l
->l_addr
+ l
->l_info
[DT_STRTAB
]->d_un
.d_ptr
359 + l
->l_info
[DT_RPATH
]->d_un
.d_val
),
360 nllp
, "RPATH", l
->l_name
);
364 /* If we have no LD_LIBRARY_PATH and no RPATH we must tell
365 this somehow to prevent we look this up again and again. */
367 l
->l_rpath_dirs
= (struct r_search_path_elem
**) -1l;
370 l
->l_rpath_dirs
= (struct r_search_path_elem
**)
371 malloc ((nllp
+ 1) * sizeof (*l
->l_rpath_dirs
));
372 if (l
->l_rpath_dirs
== NULL
)
373 _dl_signal_error (ENOMEM
, NULL
,
374 "cannot create cache for search path");
375 l
->l_rpath_dirs
[0] = NULL
;
379 /* We don't need to search the list of fake entries which is searched
380 when no dynamic objects were loaded at this time. */
381 fake_path_list
= NULL
;
385 char *copy
= strdupa (llp
);
387 /* Decompose the LD_LIBRARY_PATH and fill in the result.
388 First search for the next place to enter elements. */
389 struct r_search_path_elem
**result
= l
->l_rpath_dirs
;
390 while (*result
!= NULL
)
393 /* We need to take care that the LD_LIBRARY_PATH environment
394 variable can contain a semicolon. */
395 (void) fillin_rpath (copy
, result
, ":;",
396 __libc_enable_secure
? trusted_dirs
: NULL
,
397 "LD_LIBRARY_PATH", NULL
);
402 /* This is a statically linked program but we still have to
403 take care for the LD_LIBRARY_PATH environment variable. We
404 use a fake link_map entry. This will only contain the
405 l_rpath_dirs information. */
408 fake_path_list
= NULL
;
411 fake_path_list
= (struct r_search_path_elem
**)
412 malloc ((nllp
+ 1) * sizeof (struct r_search_path_elem
*));
413 if (fake_path_list
== NULL
)
414 _dl_signal_error (ENOMEM
, NULL
,
415 "cannot create cache for search path");
417 (void) fillin_rpath (local_strdup (llp
), fake_path_list
, ":;",
418 __libc_enable_secure
? trusted_dirs
: NULL
,
419 "LD_LIBRARY_PATH", NULL
);
423 /* Now set up the rest of the rtld_search_dirs. */
424 for (pelem
= rtld_search_dirs
; *pelem
!= NULL
; ++pelem
)
426 struct r_search_path_elem
*relem
= *pelem
;
428 if (_dl_platform
!= NULL
)
432 relem
->machdirnamelen
= relem
->dirnamelen
+ _dl_platformlen
+ 1;
433 tmp
= (char *) malloc (relem
->machdirnamelen
+ 1);
435 _dl_signal_error (ENOMEM
, NULL
,
436 "cannot create cache for search path");
439 tmp
= __mempcpy (tmp
, relem
->dirname
, relem
->dirnamelen
);
440 tmp
= __mempcpy (tmp
, _dl_platform
, _dl_platformlen
);
443 relem
->dirname
= orig
;
445 relem
->machdirstatus
= unknown
;
447 if (max_dirnamelen
< relem
->machdirnamelen
)
448 max_dirnamelen
= relem
->machdirnamelen
;
452 relem
->machdirnamelen
= relem
->dirnamelen
;
453 relem
->machdirstatus
= nonexisting
;
455 if (max_dirnamelen
< relem
->dirnamelen
)
456 max_dirnamelen
= relem
->dirnamelen
;
459 relem
->what
= "system search path";
465 /* Map in the shared object NAME, actually located in REALNAME, and already
469 _dl_map_object_from_fd (char *name
, int fd
, char *realname
,
470 struct link_map
*loader
, int l_type
)
472 struct link_map
*l
= NULL
;
473 void *file_mapping
= NULL
;
474 size_t mapping_size
= 0;
476 #define LOSE(s) lose (0, (s))
477 void lose (int code
, const char *msg
)
481 __munmap (file_mapping
, mapping_size
);
484 /* Remove the stillborn object from the list and free it. */
486 l
->l_prev
->l_next
= l
->l_next
;
488 l
->l_next
->l_prev
= l
->l_prev
;
492 _dl_signal_error (code
, name
, msg
);
493 free (name
); /* Hmmm. Can this leak memory? Better
494 than a segfault, anyway. */
497 inline caddr_t
map_segment (ElfW(Addr
) mapstart
, size_t len
,
498 int prot
, int fixed
, off_t offset
)
500 caddr_t mapat
= __mmap ((caddr_t
) mapstart
, len
, prot
,
501 fixed
|MAP_COPY
|MAP_FILE
,
503 if (mapat
== MAP_FAILED
)
504 lose (errno
, "failed to map segment from shared object");
508 /* Make sure LOCATION is mapped in. */
509 void *map (off_t location
, size_t size
)
511 if ((off_t
) mapping_size
<= location
+ (off_t
) size
)
515 __munmap (file_mapping
, mapping_size
);
516 mapping_size
= (location
+ size
+ 1 + _dl_pagesize
- 1);
517 mapping_size
&= ~(_dl_pagesize
- 1);
518 result
= __mmap (file_mapping
, mapping_size
, PROT_READ
,
519 MAP_COPY
|MAP_FILE
, fd
, 0);
520 if (result
== MAP_FAILED
)
521 lose (errno
, "cannot map file data");
522 file_mapping
= result
;
524 return file_mapping
+ location
;
527 const ElfW(Ehdr
) *header
;
528 const ElfW(Phdr
) *phdr
;
529 const ElfW(Phdr
) *ph
;
532 /* Look again to see if the real name matched another already loaded. */
533 for (l
= _dl_loaded
; l
; l
= l
->l_next
)
534 if (! strcmp (realname
, l
->l_name
))
536 /* The object is already loaded.
537 Just bump its reference count and return it. */
540 /* If the name is not in the list of names for this object add
543 add_name_to_object (l
, name
);
548 /* Map in the first page to read the header. */
549 header
= map (0, sizeof *header
);
551 /* Check the header for basic validity. */
552 if (*(Elf32_Word
*) &header
->e_ident
!=
553 #if BYTE_ORDER == LITTLE_ENDIAN
554 ((ELFMAG0
<< (EI_MAG0
* 8)) |
555 (ELFMAG1
<< (EI_MAG1
* 8)) |
556 (ELFMAG2
<< (EI_MAG2
* 8)) |
557 (ELFMAG3
<< (EI_MAG3
* 8)))
559 ((ELFMAG0
<< (EI_MAG3
* 8)) |
560 (ELFMAG1
<< (EI_MAG2
* 8)) |
561 (ELFMAG2
<< (EI_MAG1
* 8)) |
562 (ELFMAG3
<< (EI_MAG0
* 8)))
565 LOSE ("invalid ELF header");
566 #define ELF32_CLASS ELFCLASS32
567 #define ELF64_CLASS ELFCLASS64
568 if (header
->e_ident
[EI_CLASS
] != ELFW(CLASS
))
569 LOSE ("ELF file class not " STRING(__ELF_NATIVE_CLASS
) "-bit");
570 if (header
->e_ident
[EI_DATA
] != byteorder
)
571 LOSE ("ELF file data encoding not " byteorder_name
);
572 if (header
->e_ident
[EI_VERSION
] != EV_CURRENT
)
573 LOSE ("ELF file version ident not " STRING(EV_CURRENT
));
574 if (header
->e_version
!= EV_CURRENT
)
575 LOSE ("ELF file version not " STRING(EV_CURRENT
));
576 if (! elf_machine_matches_host (header
->e_machine
))
577 LOSE ("ELF file machine architecture not " ELF_MACHINE_NAME
);
578 if (header
->e_phentsize
!= sizeof (ElfW(Phdr
)))
579 LOSE ("ELF file's phentsize not the expected size");
583 if (_dl_zerofd
== -1)
585 _dl_zerofd
= _dl_sysdep_open_zero_fill ();
586 if (_dl_zerofd
== -1)
589 _dl_signal_error (errno
, NULL
, "cannot open zero fill device");
594 /* Enter the new object in the list of loaded objects. */
595 l
= _dl_new_object (realname
, name
, l_type
);
597 lose (ENOMEM
, "cannot create shared object descriptor");
599 l
->l_loader
= loader
;
601 /* Extract the remaining details we need from the ELF header
602 and then map in the program header table. */
603 l
->l_entry
= header
->e_entry
;
604 type
= header
->e_type
;
605 l
->l_phnum
= header
->e_phnum
;
606 phdr
= map (header
->e_phoff
, l
->l_phnum
* sizeof (ElfW(Phdr
)));
609 /* Scan the program header table, collecting its load commands. */
612 ElfW(Addr
) mapstart
, mapend
, dataend
, allocend
;
615 } loadcmds
[l
->l_phnum
], *c
;
616 size_t nloadcmds
= 0;
621 for (ph
= phdr
; ph
< &phdr
[l
->l_phnum
]; ++ph
)
624 /* These entries tell us where to find things once the file's
625 segments are mapped in. We record the addresses it says
626 verbatim, and later correct for the run-time load address. */
628 l
->l_ld
= (void *) ph
->p_vaddr
;
631 l
->l_phdr
= (void *) ph
->p_vaddr
;
635 /* A load command tells us to map in part of the file.
636 We record the load commands and process them all later. */
637 if (ph
->p_align
% _dl_pagesize
!= 0)
638 LOSE ("ELF load command alignment not page-aligned");
639 if ((ph
->p_vaddr
- ph
->p_offset
) % ph
->p_align
)
640 LOSE ("ELF load command address/offset not properly aligned");
642 struct loadcmd
*c
= &loadcmds
[nloadcmds
++];
643 c
->mapstart
= ph
->p_vaddr
& ~(ph
->p_align
- 1);
644 c
->mapend
= ((ph
->p_vaddr
+ ph
->p_filesz
+ _dl_pagesize
- 1)
645 & ~(_dl_pagesize
- 1));
646 c
->dataend
= ph
->p_vaddr
+ ph
->p_filesz
;
647 c
->allocend
= ph
->p_vaddr
+ ph
->p_memsz
;
648 c
->mapoff
= ph
->p_offset
& ~(ph
->p_align
- 1);
650 if (ph
->p_flags
& PF_R
)
651 c
->prot
|= PROT_READ
;
652 if (ph
->p_flags
& PF_W
)
653 c
->prot
|= PROT_WRITE
;
654 if (ph
->p_flags
& PF_X
)
655 c
->prot
|= PROT_EXEC
;
660 /* We are done reading the file's headers now. Unmap them. */
661 __munmap (file_mapping
, mapping_size
);
663 /* Now process the load commands and map segments into memory. */
666 if (type
== ET_DYN
|| type
== ET_REL
)
668 /* This is a position-independent shared object. We can let the
669 kernel map it anywhere it likes, but we must have space for all
670 the segments in their specified positions relative to the first.
671 So we map the first segment without MAP_FIXED, but with its
672 extent increased to cover all the segments. Then we remove
673 access from excess portion, and there is known sufficient space
674 there to remap from the later segments.
676 As a refinement, sometimes we have an address that we would
677 prefer to map such objects at; but this is only a preference,
678 the OS can do whatever it likes. */
681 size_t maplength
= loadcmds
[nloadcmds
- 1].allocend
- c
->mapstart
;
682 mappref
= (ELF_PREFERRED_ADDRESS (loader
, maplength
, c
->mapstart
)
683 - MAP_BASE_ADDR (l
));
684 mapat
= map_segment (mappref
, maplength
, c
->prot
, 0, c
->mapoff
);
685 l
->l_addr
= (ElfW(Addr
)) mapat
- c
->mapstart
;
687 /* Change protection on the excess portion to disallow all access;
688 the portions we do not remap later will be inaccessible as if
689 unallocated. Then jump into the normal segment-mapping loop to
690 handle the portion of the segment past the end of the file
692 __mprotect ((caddr_t
) (l
->l_addr
+ c
->mapend
),
693 loadcmds
[nloadcmds
- 1].allocend
- c
->mapend
,
699 /* Notify ELF_PREFERRED_ADDRESS that we have to load this one
701 ELF_FIXED_ADDRESS (loader
, c
->mapstart
);
704 while (c
< &loadcmds
[nloadcmds
])
706 if (c
->mapend
> c
->mapstart
)
707 /* Map the segment contents from the file. */
708 map_segment (l
->l_addr
+ c
->mapstart
, c
->mapend
- c
->mapstart
,
709 c
->prot
, MAP_FIXED
, c
->mapoff
);
712 if (c
->allocend
> c
->dataend
)
714 /* Extra zero pages should appear at the end of this segment,
715 after the data mapped from the file. */
716 ElfW(Addr
) zero
, zeroend
, zeropage
;
718 zero
= l
->l_addr
+ c
->dataend
;
719 zeroend
= l
->l_addr
+ c
->allocend
;
720 zeropage
= (zero
+ _dl_pagesize
- 1) & ~(_dl_pagesize
- 1);
722 if (zeroend
< zeropage
)
723 /* All the extra data is in the last page of the segment.
724 We can just zero it. */
729 /* Zero the final part of the last page of the segment. */
730 if ((c
->prot
& PROT_WRITE
) == 0)
733 if (__mprotect ((caddr_t
) (zero
& ~(_dl_pagesize
- 1)),
734 _dl_pagesize
, c
->prot
|PROT_WRITE
) < 0)
735 lose (errno
, "cannot change memory protections");
737 memset ((void *) zero
, 0, zeropage
- zero
);
738 if ((c
->prot
& PROT_WRITE
) == 0)
739 __mprotect ((caddr_t
) (zero
& ~(_dl_pagesize
- 1)),
740 _dl_pagesize
, c
->prot
);
743 if (zeroend
> zeropage
)
745 /* Map the remaining zero pages in from the zero fill FD. */
747 mapat
= __mmap ((caddr_t
) zeropage
, zeroend
- zeropage
,
748 c
->prot
, MAP_ANON
|MAP_PRIVATE
|MAP_FIXED
,
750 if (mapat
== MAP_FAILED
)
751 lose (errno
, "cannot map zero-fill pages");
760 /* There was no PT_PHDR specified. We need to find the phdr in the
761 load image ourselves. We assume it is in fact in the load image
762 somewhere, and that the first load command starts at the
763 beginning of the file and thus contains the ELF file header. */
764 ElfW(Addr
) bof
= l
->l_addr
+ loadcmds
[0].mapstart
;
765 assert (loadcmds
[0].mapoff
== 0);
766 l
->l_phdr
= (void *) (bof
+ ((const ElfW(Ehdr
) *) bof
)->e_phoff
);
769 /* Adjust the PT_PHDR value by the runtime load address. */
770 (ElfW(Addr
)) l
->l_phdr
+= l
->l_addr
;
773 /* We are done mapping in the file. We no longer need the descriptor. */
776 if (l
->l_type
== lt_library
&& type
== ET_EXEC
)
777 l
->l_type
= lt_executable
;
782 LOSE ("object file has no dynamic section");
785 (ElfW(Addr
)) l
->l_ld
+= l
->l_addr
;
787 l
->l_entry
+= l
->l_addr
;
789 elf_get_dynamic_info (l
->l_ld
, l
->l_info
);
790 if (l
->l_info
[DT_HASH
])
796 /* Print search path. */
798 print_search_path (struct r_search_path_elem
**list
,
799 const char *what
, const char *name
)
803 _dl_debug_message ("\t search path=", NULL
);
805 while (*list
!= NULL
&& (*list
)->what
== what
) /* Yes, ==. */
807 char *buf
= strdupa ((*list
)->dirname
);
809 if ((*list
)->machdirstatus
!= nonexisting
)
811 buf
[(*list
)->machdirnamelen
- 1] = '\0';
812 _dl_debug_message (first
? "" : ":", buf
, NULL
);
815 if ((*list
)->dirstatus
!= nonexisting
)
817 buf
[(*list
)->dirnamelen
- 1] = '\0';
818 _dl_debug_message (first
? "" : ":", buf
, NULL
);
825 _dl_debug_message ("\t\t(", what
, " from file ",
826 name
[0] ? name
: _dl_argv
[0], ")\n", NULL
);
828 _dl_debug_message ("\t\t(", what
, ")\n", NULL
);
831 /* Try to open NAME in one of the directories in DIRS.
832 Return the fd, or -1. If successful, fill in *REALNAME
833 with the malloc'd full directory name. */
836 open_path (const char *name
, size_t namelen
, int preloaded
,
837 struct r_search_path_elem
**dirs
,
842 const char *current_what
= NULL
;
844 if (dirs
== NULL
|| *dirs
== NULL
)
846 __set_errno (ENOENT
);
850 buf
= __alloca (max_dirnamelen
+ namelen
);
853 struct r_search_path_elem
*this_dir
= *dirs
;
856 /* If we are debugging the search for libraries print the path
857 now if it hasn't happened now. */
858 if (_dl_debug_libs
&& current_what
!= this_dir
->what
)
860 current_what
= this_dir
->what
;
861 print_search_path (dirs
, current_what
, this_dir
->where
);
864 if (this_dir
->machdirstatus
!= nonexisting
)
866 /* Construct the pathname to try. */
867 buflen
= ((char *) __mempcpy (__mempcpy (buf
, this_dir
->dirname
,
868 this_dir
->machdirnamelen
),
872 /* Print name we try if this is wanted. */
874 _dl_debug_message ("\t trying file=", buf
, "\n", NULL
);
876 fd
= __open (buf
, O_RDONLY
);
877 if (this_dir
->machdirstatus
== unknown
)
879 this_dir
->machdirstatus
= existing
;
882 /* We failed to open machine dependent library. Let's
883 test whether there is any directory at all. */
886 buf
[this_dir
->machdirnamelen
- 1] = '\0';
888 if (__xstat (_STAT_VER
, buf
, &st
) != 0
889 || ! S_ISDIR (st
.st_mode
))
890 /* The directory does not exist ot it is no directory. */
891 this_dir
->machdirstatus
= nonexisting
;
893 this_dir
->machdirstatus
= existing
;
895 if (fd
!= -1 && preloaded
&& __libc_enable_secure
)
897 /* This is an extra security effort to make sure nobody can
898 preload broken shared objects which are in the trusted
899 directories and so exploit the bugs. */
902 if (__fxstat (_STAT_VER
, fd
, &st
) != 0
903 || (st
.st_mode
& S_ISUID
) == 0)
905 /* The shared object cannot be tested for being SUID
906 or this bit is not set. In this case we must not
910 /* We simply ignore the file, signal this by setting
911 the error value which would have been set by `open'. */
919 if (fd
== -1 && errno
== ENOENT
&& this_dir
->dirstatus
!= nonexisting
)
921 /* Construct the pathname to try. */
922 buflen
= ((char *) __mempcpy (__mempcpy (buf
, this_dir
->dirname
,
923 this_dir
->dirnamelen
),
927 /* Print name we try if this is wanted. */
929 _dl_debug_message ("\t trying file=", buf
, "\n", NULL
);
931 fd
= __open (buf
, O_RDONLY
);
932 if (this_dir
->dirstatus
== unknown
)
934 this_dir
->dirstatus
= existing
;
936 /* We failed to open library. Let's test whether there
937 is any directory at all. */
938 if (this_dir
->dirnamelen
<= 1)
939 this_dir
->dirstatus
= existing
;
944 buf
[this_dir
->dirnamelen
- 1] = '\0';
946 if (__xstat (_STAT_VER
, buf
, &st
) != 0
947 || ! S_ISDIR (st
.st_mode
))
948 /* The directory does not exist ot it is no directory. */
949 this_dir
->dirstatus
= nonexisting
;
951 this_dir
->dirstatus
= existing
;
953 if (fd
!= -1 && preloaded
&& __libc_enable_secure
)
955 /* This is an extra security effort to make sure nobody can
956 preload broken shared objects which are in the trusted
957 directories and so exploit the bugs. */
960 if (__fxstat (_STAT_VER
, fd
, &st
) != 0
961 || (st
.st_mode
& S_ISUID
) == 0)
963 /* The shared object cannot be tested for being SUID
964 or this bit is not set. In this case we must not
968 /* We simply ignore the file, signal this by setting
969 the error value which would have been set by `open'. */
977 *realname
= malloc (buflen
);
978 if (*realname
!= NULL
)
980 memcpy (*realname
, buf
, buflen
);
985 /* No memory for the name, we certainly won't be able
986 to load and link it. */
991 if (errno
!= ENOENT
&& errno
!= EACCES
)
992 /* The file exists and is readable, but something went wrong. */
995 while (*++dirs
!= NULL
);
1000 /* Map in the shared object file NAME. */
1003 _dl_map_object (struct link_map
*loader
, const char *name
, int preloaded
,
1004 int type
, int trace_mode
)
1011 /* Look for this name among those already loaded. */
1012 for (l
= _dl_loaded
; l
; l
= l
->l_next
)
1014 /* If the requested name matches the soname of a loaded object,
1015 use that object. Elide this check for names that have not
1017 if (l
->l_opencount
<= 0)
1019 if (!_dl_name_match_p (name
, l
))
1023 if (l
->l_info
[DT_SONAME
] == NULL
)
1026 soname
= (const char *) (l
->l_addr
1027 + l
->l_info
[DT_STRTAB
]->d_un
.d_ptr
1028 + l
->l_info
[DT_SONAME
]->d_un
.d_val
);
1029 if (strcmp (name
, soname
) != 0)
1032 /* We have a match on a new name -- cache it. */
1033 add_name_to_object (l
, local_strdup (soname
));
1036 /* We have a match -- bump the reference count and return it. */
1041 if (strchr (name
, '/') == NULL
)
1043 /* Search for NAME in several places. */
1045 size_t namelen
= strlen (name
) + 1;
1048 _dl_debug_message ("\tfind library=", name
, "; searching\n", NULL
);
1052 /* First try the DT_RPATH of the dependent object that caused NAME
1053 to be loaded. Then that object's dependent, and on up. */
1054 for (l
= loader
; fd
== -1 && l
; l
= l
->l_loader
)
1055 if (l
&& l
->l_info
[DT_RPATH
])
1057 /* Make sure the cache information is available. */
1058 if (l
->l_rpath_dirs
== NULL
)
1060 size_t ptrval
= (l
->l_addr
1061 + l
->l_info
[DT_STRTAB
]->d_un
.d_ptr
1062 + l
->l_info
[DT_RPATH
]->d_un
.d_val
);
1064 decompose_rpath ((const char *) ptrval
, 0,
1065 "RPATH", l
->l_name
);
1068 if (l
->l_rpath_dirs
!= (struct r_search_path_elem
**) -1l)
1069 fd
= open_path (name
, namelen
, preloaded
, l
->l_rpath_dirs
,
1073 /* If dynamically linked, try the DT_RPATH of the executable itself
1074 and the LD_LIBRARY_PATH environment variable. */
1076 if (fd
== -1 && l
&& l
->l_type
!= lt_loaded
1077 && l
->l_rpath_dirs
!= (struct r_search_path_elem
**) -1l)
1078 fd
= open_path (name
, namelen
, preloaded
, l
->l_rpath_dirs
, &realname
);
1080 /* This is used if a static binary uses dynamic loading and there
1081 is a LD_LIBRARY_PATH given. */
1082 if (fd
== -1 && fake_path_list
!= NULL
)
1083 fd
= open_path (name
, namelen
, preloaded
, fake_path_list
, &realname
);
1087 /* Check the list of libraries in the file /etc/ld.so.cache,
1088 for compatibility with Linux's ldconfig program. */
1089 extern const char *_dl_load_cache_lookup (const char *name
);
1090 const char *cached
= _dl_load_cache_lookup (name
);
1093 fd
= __open (cached
, O_RDONLY
);
1096 realname
= local_strdup (cached
);
1097 if (realname
== NULL
)
1106 /* Finally, try the default path. */
1108 fd
= open_path (name
, namelen
, preloaded
, rtld_search_dirs
, &realname
);
1110 /* Add another newline when we a tracing the library loading. */
1112 _dl_debug_message ("\n", NULL
);
1116 fd
= __open (name
, O_RDONLY
);
1119 realname
= local_strdup (name
);
1120 if (realname
== NULL
)
1130 name_copy
= local_strdup (name
);
1131 if (name_copy
== NULL
)
1142 /* We haven't found an appropriate library. But since we
1143 are only interested in the list of libraries this isn't
1144 so severe. Fake an entry with all the information we
1146 static const ElfW(Symndx
) dummy_bucket
= STN_UNDEF
;
1148 /* Enter the new object in the list of loaded objects. */
1149 if ((name_copy
= local_strdup (name
)) == NULL
1150 || (l
= _dl_new_object (name_copy
, name
, type
)) == NULL
)
1151 _dl_signal_error (ENOMEM
, name
,
1152 "cannot create shared object descriptor");
1153 /* We use an opencount of 0 as a sign for the faked entry. */
1156 l
->l_buckets
= &dummy_bucket
;
1163 _dl_signal_error (errno
, name
, "cannot open shared object file");
1166 return _dl_map_object_from_fd (name_copy
, fd
, realname
, loader
, type
);