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>
29 #include <sys/types.h>
30 #include "dynamic-link.h"
31 #include <stdio-common/_itoa.h>
34 /* On some systems, no flag bits are given to specify file mapping. */
39 /* The right way to map in the shared library files is MAP_COPY, which
40 makes a virtual copy of the data at the time of the mmap call; this
41 guarantees the mapped pages will be consistent even if the file is
42 overwritten. Some losing VM systems like Linux's lack MAP_COPY. All we
43 get is MAP_PRIVATE, which copies each page when it is modified; this
44 means if the file is overwritten, we may at some point get some pages
45 from the new version after starting with pages from the old version. */
47 #define MAP_COPY MAP_PRIVATE
50 /* Some systems link their relocatable objects for another base address
51 than 0. We want to know the base address for these such that we can
52 subtract this address from the segment addresses during mapping.
53 This results in a more efficient address space usage. Defaults to
54 zero for almost all systems. */
56 #define MAP_BASE_ADDR(l) 0
61 #if BYTE_ORDER == BIG_ENDIAN
62 #define byteorder ELFDATA2MSB
63 #define byteorder_name "big-endian"
64 #elif BYTE_ORDER == LITTLE_ENDIAN
65 #define byteorder ELFDATA2LSB
66 #define byteorder_name "little-endian"
68 #error "Unknown BYTE_ORDER " BYTE_ORDER
69 #define byteorder ELFDATANONE
72 #define STRING(x) __STRING (x)
75 /* The fd is not examined when using MAP_ANON. */
79 #define ANONFD _dl_zerofd
82 /* Handle situations where we have a preferred location in memory for
83 the shared objects. */
84 #ifdef ELF_PREFERRED_ADDRESS_DATA
85 ELF_PREFERRED_ADDRESS_DATA
;
87 #ifndef ELF_PREFERRED_ADDRESS
88 #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
90 #ifndef ELF_FIXED_ADDRESS
91 #define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
96 extern const char *_dl_platform
;
97 extern size_t _dl_platformlen
;
99 /* This is a fake list to store the RPATH information for static
101 static struct r_search_path_elem
**fake_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
;
109 /* Local version of `strdup' function. */
111 local_strdup (const char *s
)
113 size_t len
= strlen (s
) + 1;
114 void *new = malloc (len
);
119 return (char *) memcpy (new, s
, len
);
122 /* Add `name' to the list of names for a particular shared object.
123 `name' is expected to have been allocated with malloc and will
124 be freed if the shared object already has this name.
125 Returns false if the object already had this name. */
128 add_name_to_object (struct link_map
*l
, char *name
)
130 struct libname_list
*lnp
, *lastp
;
131 struct libname_list
*newname
;
135 /* No more memory. */
136 _dl_signal_error (ENOMEM
, NULL
, "could not allocate name string");
141 for (lnp
= l
->l_libname
; lnp
!= NULL
; lastp
= lnp
, lnp
= lnp
->next
)
142 if (strcmp (name
, lnp
->name
) == 0)
148 newname
= malloc (sizeof *newname
);
151 /* No more memory. */
152 _dl_signal_error (ENOMEM
, name
, "cannot allocate name record");
156 /* The object should have a libname set from _dl_new_object. */
157 assert (lastp
!= NULL
);
159 newname
->name
= name
;
160 newname
->next
= NULL
;
161 lastp
->next
= newname
;
165 /* All known directories in sorted order. */
166 static struct r_search_path_elem
*all_dirs
;
168 /* Standard search directories. */
169 static struct r_search_path_elem
**rtld_search_dirs
;
171 static size_t max_dirnamelen
;
173 static inline struct r_search_path_elem
**
174 fillin_rpath (char *rpath
, struct r_search_path_elem
**result
, const char *sep
,
175 const char **trusted
, const char *what
, const char *where
)
180 while ((cp
= __strsep (&rpath
, sep
)) != NULL
)
182 struct r_search_path_elem
*dirp
;
183 size_t len
= strlen (cp
);
185 /* `strsep' can pass an empty string. */
189 /* Remove trailing slashes. */
190 while (len
> 1 && cp
[len
- 1] == '/')
193 /* Make sure we don't use untrusted directories if we run SUID. */
196 const char **trun
= trusted
;
198 /* All trusted directory must be complete name. */
203 && (memcmp (*trun
, cp
, len
) != 0
204 || ((*trun
)[len
] != '/' && (*trun
)[len
+ 1] != '\0')))
208 /* It's no trusted directory, skip it. */
216 /* See if this directory is already known. */
217 for (dirp
= all_dirs
; dirp
!= NULL
; dirp
= dirp
->next
)
218 if (dirp
->dirnamelen
== len
&& memcmp (cp
, dirp
->dirname
, len
) == 0)
223 /* It is available, see whether it's on our own list. */
225 for (cnt
= 0; cnt
< nelems
; ++cnt
)
226 if (result
[cnt
] == dirp
)
230 result
[nelems
++] = dirp
;
236 /* It's a new directory. Create an entry and add it. */
237 dirp
= (struct r_search_path_elem
*)
238 malloc (sizeof (*dirp
) + ncapstr
* sizeof (enum r_dir_status
));
240 _dl_signal_error (ENOMEM
, NULL
,
241 "cannot create cache for search path");
244 dirp
->dirnamelen
= len
;
246 if (len
> max_dirnamelen
)
247 max_dirnamelen
= len
;
249 /* We have to make sure all the relative directories are never
250 ignored. The current directory might change and all our
251 saved information would be void. */
253 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
254 dirp
->status
[cnt
] = existing
;
256 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
257 dirp
->status
[cnt
] = unknown
;
262 dirp
->next
= all_dirs
;
265 /* Put it in the result array. */
266 result
[nelems
++] = dirp
;
270 /* Terminate the array. */
271 result
[nelems
] = NULL
;
277 static struct r_search_path_elem
**
279 decompose_rpath (const char *rpath
, size_t additional_room
,
280 const char *what
, const char *where
)
282 /* Make a copy we can work with. */
283 char *copy
= local_strdup (rpath
);
285 struct r_search_path_elem
**result
;
286 /* First count the number of necessary elements in the result array. */
289 for (cp
= copy
; *cp
!= '\0'; ++cp
)
293 /* Allocate room for the result. NELEMS + 1 + ADDITIONAL_ROOM is an upper
294 limit for the number of necessary entries. */
295 result
= (struct r_search_path_elem
**) malloc ((nelems
+ 1
296 + additional_room
+ 1)
299 _dl_signal_error (ENOMEM
, NULL
, "cannot create cache for search path");
301 return fillin_rpath (copy
, result
, ":", NULL
, what
, where
);
307 _dl_init_paths (const char *llp
)
309 static const char *system_dirs
[] =
311 #include "trusted-dirs.h"
315 struct r_search_path_elem
*pelem
, **aelem
;
318 /* We have in `search_path' the information about the RPATH of the
319 dynamic loader. Now fill in the information about the applications
320 RPATH and the directories addressed by the LD_LIBRARY_PATH environment
324 /* Number of elements in the library path. */
327 /* First determine how many elements the LD_LIBRARY_PATH contents has. */
328 if (llp
!= NULL
&& *llp
!= '\0')
330 /* Simply count the number of colons. */
331 const char *cp
= llp
;
340 /* Get the capabilities. */
341 capstr
= _dl_important_hwcaps (_dl_platform
, _dl_platformlen
,
342 &ncapstr
, &max_capstrlen
);
344 /* First set up the rest of the default search directory entries. */
345 aelem
= rtld_search_dirs
= (struct r_search_path_elem
**)
346 malloc ((ncapstr
+ 1) * sizeof (struct r_search_path_elem
*));
348 round_size
= ((2 * sizeof (struct r_search_path_elem
) - 1
349 + ncapstr
* sizeof (enum r_dir_status
))
350 / sizeof (struct r_search_path_elem
));
352 rtld_search_dirs
[0] = (struct r_search_path_elem
*)
353 malloc ((sizeof (system_dirs
) / sizeof (system_dirs
[0]) - 1)
354 * round_size
* sizeof (struct r_search_path_elem
));
355 if (rtld_search_dirs
[0] == NULL
)
356 _dl_signal_error (ENOMEM
, NULL
, "cannot create cache for search path");
358 pelem
= all_dirs
= rtld_search_dirs
[0];
359 for (strp
= system_dirs
; *strp
!= NULL
; ++strp
, pelem
+= round_size
)
365 pelem
->next
= *(strp
+ 1) == NULL
? NULL
: (pelem
+ round_size
);
367 pelem
->what
= "system search path";
370 pelem
->dirnamelen
= strlen (pelem
->dirname
= *strp
);
371 if (pelem
->dirnamelen
> max_dirnamelen
)
372 max_dirnamelen
= pelem
->dirnamelen
;
374 if (pelem
->dirname
[0] != '/')
375 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
376 pelem
->status
[cnt
] = existing
;
378 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
379 pelem
->status
[cnt
] = unknown
;
386 if (l
->l_type
!= lt_loaded
&& l
->l_info
[DT_RPATH
])
388 /* Allocate room for the search path and fill in information
391 decompose_rpath ((const char *)
392 (l
->l_addr
+ l
->l_info
[DT_STRTAB
]->d_un
.d_ptr
393 + l
->l_info
[DT_RPATH
]->d_un
.d_val
),
394 nllp
, "RPATH", l
->l_name
);
398 /* If we have no LD_LIBRARY_PATH and no RPATH we must tell
399 this somehow to prevent we look this up again and again. */
401 l
->l_rpath_dirs
= (struct r_search_path_elem
**) -1l;
404 l
->l_rpath_dirs
= (struct r_search_path_elem
**)
405 malloc ((nllp
+ 1) * sizeof (*l
->l_rpath_dirs
));
406 if (l
->l_rpath_dirs
== NULL
)
407 _dl_signal_error (ENOMEM
, NULL
,
408 "cannot create cache for search path");
409 l
->l_rpath_dirs
[0] = NULL
;
413 /* We don't need to search the list of fake entries which is searched
414 when no dynamic objects were loaded at this time. */
415 fake_path_list
= NULL
;
419 char *copy
= local_strdup (llp
);
421 /* Decompose the LD_LIBRARY_PATH and fill in the result.
422 First search for the next place to enter elements. */
423 struct r_search_path_elem
**result
= l
->l_rpath_dirs
;
424 while (*result
!= NULL
)
427 /* We need to take care that the LD_LIBRARY_PATH environment
428 variable can contain a semicolon. */
429 (void) fillin_rpath (copy
, result
, ":;",
430 __libc_enable_secure
? system_dirs
: NULL
,
431 "LD_LIBRARY_PATH", NULL
);
436 /* This is a statically linked program but we still have to
437 take care for the LD_LIBRARY_PATH environment variable. We
438 use a fake link_map entry. This will only contain the
439 l_rpath_dirs information. */
442 fake_path_list
= NULL
;
445 fake_path_list
= (struct r_search_path_elem
**)
446 malloc ((nllp
+ 1) * sizeof (struct r_search_path_elem
*));
447 if (fake_path_list
== NULL
)
448 _dl_signal_error (ENOMEM
, NULL
,
449 "cannot create cache for search path");
451 (void) fillin_rpath (local_strdup (llp
), fake_path_list
, ":;",
452 __libc_enable_secure
? system_dirs
: NULL
,
453 "LD_LIBRARY_PATH", NULL
);
459 /* Map in the shared object NAME, actually located in REALNAME, and already
463 _dl_map_object_from_fd (char *name
, int fd
, char *realname
,
464 struct link_map
*loader
, int l_type
)
466 struct link_map
*l
= NULL
;
467 void *file_mapping
= NULL
;
468 size_t mapping_size
= 0;
470 #define LOSE(s) lose (0, (s))
471 void lose (int code
, const char *msg
)
475 __munmap (file_mapping
, mapping_size
);
478 /* Remove the stillborn object from the list and free it. */
480 l
->l_prev
->l_next
= l
->l_next
;
482 l
->l_next
->l_prev
= l
->l_prev
;
486 _dl_signal_error (code
, name
, msg
);
487 free (name
); /* Hmmm. Can this leak memory? Better
488 than a segfault, anyway. */
491 inline caddr_t
map_segment (ElfW(Addr
) mapstart
, size_t len
,
492 int prot
, int fixed
, off_t offset
)
494 caddr_t mapat
= __mmap ((caddr_t
) mapstart
, len
, prot
,
495 fixed
|MAP_COPY
|MAP_FILE
,
497 if (mapat
== MAP_FAILED
)
498 lose (errno
, "failed to map segment from shared object");
502 /* Make sure LOCATION is mapped in. */
503 void *map (off_t location
, size_t size
)
505 if ((off_t
) mapping_size
<= location
+ (off_t
) size
)
509 __munmap (file_mapping
, mapping_size
);
510 mapping_size
= (location
+ size
+ 1 + _dl_pagesize
- 1);
511 mapping_size
&= ~(_dl_pagesize
- 1);
512 result
= __mmap (file_mapping
, mapping_size
, PROT_READ
,
513 MAP_COPY
|MAP_FILE
, fd
, 0);
514 if (result
== MAP_FAILED
)
515 lose (errno
, "cannot map file data");
516 file_mapping
= result
;
518 return file_mapping
+ location
;
521 const ElfW(Ehdr
) *header
;
522 const ElfW(Phdr
) *phdr
;
523 const ElfW(Phdr
) *ph
;
527 /* Look again to see if the real name matched another already loaded. */
528 for (l
= _dl_loaded
; l
; l
= l
->l_next
)
529 if (! strcmp (realname
, l
->l_name
))
531 /* The object is already loaded.
532 Just bump its reference count and return it. */
535 /* If the name is not in the list of names for this object add
538 add_name_to_object (l
, name
);
543 /* Print debugging message. */
545 _dl_debug_message (1, "file=", name
, "; generating link map\n", NULL
);
547 /* Map in the first page to read the header. */
548 header
= map (0, sizeof *header
);
550 /* Check the header for basic validity. */
551 if (*(Elf32_Word
*) &header
->e_ident
!=
552 #if BYTE_ORDER == LITTLE_ENDIAN
553 ((ELFMAG0
<< (EI_MAG0
* 8)) |
554 (ELFMAG1
<< (EI_MAG1
* 8)) |
555 (ELFMAG2
<< (EI_MAG2
* 8)) |
556 (ELFMAG3
<< (EI_MAG3
* 8)))
558 ((ELFMAG0
<< (EI_MAG3
* 8)) |
559 (ELFMAG1
<< (EI_MAG2
* 8)) |
560 (ELFMAG2
<< (EI_MAG1
* 8)) |
561 (ELFMAG3
<< (EI_MAG0
* 8)))
564 LOSE ("invalid ELF header");
565 #define ELF32_CLASS ELFCLASS32
566 #define ELF64_CLASS ELFCLASS64
567 if (header
->e_ident
[EI_CLASS
] != ELFW(CLASS
))
568 LOSE ("ELF file class not " STRING(__ELF_NATIVE_CLASS
) "-bit");
569 if (header
->e_ident
[EI_DATA
] != byteorder
)
570 LOSE ("ELF file data encoding not " byteorder_name
);
571 if (header
->e_ident
[EI_VERSION
] != EV_CURRENT
)
572 LOSE ("ELF file version ident not " STRING(EV_CURRENT
));
573 if (header
->e_version
!= EV_CURRENT
)
574 LOSE ("ELF file version not " STRING(EV_CURRENT
));
575 if (! elf_machine_matches_host (header
->e_machine
))
576 LOSE ("ELF file machine architecture not " ELF_MACHINE_NAME
);
577 if (header
->e_phentsize
!= sizeof (ElfW(Phdr
)))
578 LOSE ("ELF file's phentsize not the expected size");
582 if (_dl_zerofd
== -1)
584 _dl_zerofd
= _dl_sysdep_open_zero_fill ();
585 if (_dl_zerofd
== -1)
588 _dl_signal_error (errno
, NULL
, "cannot open zero fill device");
593 /* Enter the new object in the list of loaded objects. */
594 l
= _dl_new_object (realname
, name
, l_type
);
596 lose (ENOMEM
, "cannot create shared object descriptor");
598 l
->l_loader
= loader
;
600 /* Extract the remaining details we need from the ELF header
601 and then map in the program header table. */
602 l
->l_entry
= header
->e_entry
;
603 type
= header
->e_type
;
604 l
->l_phnum
= header
->e_phnum
;
605 phdr
= map (header
->e_phoff
, l
->l_phnum
* sizeof (ElfW(Phdr
)));
608 /* Scan the program header table, collecting its load commands. */
611 ElfW(Addr
) mapstart
, mapend
, dataend
, allocend
;
614 } loadcmds
[l
->l_phnum
], *c
;
615 size_t nloadcmds
= 0;
620 for (ph
= phdr
; ph
< &phdr
[l
->l_phnum
]; ++ph
)
623 /* These entries tell us where to find things once the file's
624 segments are mapped in. We record the addresses it says
625 verbatim, and later correct for the run-time load address. */
627 l
->l_ld
= (void *) ph
->p_vaddr
;
630 l
->l_phdr
= (void *) ph
->p_vaddr
;
634 /* A load command tells us to map in part of the file.
635 We record the load commands and process them all later. */
636 if (ph
->p_align
% _dl_pagesize
!= 0)
637 LOSE ("ELF load command alignment not page-aligned");
638 if ((ph
->p_vaddr
- ph
->p_offset
) % ph
->p_align
)
639 LOSE ("ELF load command address/offset not properly aligned");
641 struct loadcmd
*c
= &loadcmds
[nloadcmds
++];
642 c
->mapstart
= ph
->p_vaddr
& ~(ph
->p_align
- 1);
643 c
->mapend
= ((ph
->p_vaddr
+ ph
->p_filesz
+ _dl_pagesize
- 1)
644 & ~(_dl_pagesize
- 1));
645 c
->dataend
= ph
->p_vaddr
+ ph
->p_filesz
;
646 c
->allocend
= ph
->p_vaddr
+ ph
->p_memsz
;
647 c
->mapoff
= ph
->p_offset
& ~(ph
->p_align
- 1);
649 if (ph
->p_flags
& PF_R
)
650 c
->prot
|= PROT_READ
;
651 if (ph
->p_flags
& PF_W
)
652 c
->prot
|= PROT_WRITE
;
653 if (ph
->p_flags
& PF_X
)
654 c
->prot
|= PROT_EXEC
;
659 /* We are done reading the file's headers now. Unmap them. */
660 __munmap (file_mapping
, mapping_size
);
662 /* Now process the load commands and map segments into memory. */
665 /* Length of the sections to be loaded. */
666 maplength
= loadcmds
[nloadcmds
- 1].allocend
- c
->mapstart
;
668 if (type
== ET_DYN
|| type
== ET_REL
)
670 /* This is a position-independent shared object. We can let the
671 kernel map it anywhere it likes, but we must have space for all
672 the segments in their specified positions relative to the first.
673 So we map the first segment without MAP_FIXED, but with its
674 extent increased to cover all the segments. Then we remove
675 access from excess portion, and there is known sufficient space
676 there to remap from the later segments.
678 As a refinement, sometimes we have an address that we would
679 prefer to map such objects at; but this is only a preference,
680 the OS can do whatever it likes. */
683 mappref
= (ELF_PREFERRED_ADDRESS (loader
, maplength
, c
->mapstart
)
684 - MAP_BASE_ADDR (l
));
685 mapat
= map_segment (mappref
, maplength
, c
->prot
, 0, c
->mapoff
);
686 l
->l_addr
= (ElfW(Addr
)) mapat
- c
->mapstart
;
688 /* Change protection on the excess portion to disallow all access;
689 the portions we do not remap later will be inaccessible as if
690 unallocated. Then jump into the normal segment-mapping loop to
691 handle the portion of the segment past the end of the file
693 __mprotect ((caddr_t
) (l
->l_addr
+ c
->mapend
),
694 loadcmds
[nloadcmds
- 1].allocend
- c
->mapend
,
700 /* Notify ELF_PREFERRED_ADDRESS that we have to load this one
702 ELF_FIXED_ADDRESS (loader
, c
->mapstart
);
705 while (c
< &loadcmds
[nloadcmds
])
707 if (c
->mapend
> c
->mapstart
)
708 /* Map the segment contents from the file. */
709 map_segment (l
->l_addr
+ c
->mapstart
, c
->mapend
- c
->mapstart
,
710 c
->prot
, MAP_FIXED
, c
->mapoff
);
713 if (c
->allocend
> c
->dataend
)
715 /* Extra zero pages should appear at the end of this segment,
716 after the data mapped from the file. */
717 ElfW(Addr
) zero
, zeroend
, zeropage
;
719 zero
= l
->l_addr
+ c
->dataend
;
720 zeroend
= l
->l_addr
+ c
->allocend
;
721 zeropage
= (zero
+ _dl_pagesize
- 1) & ~(_dl_pagesize
- 1);
723 if (zeroend
< zeropage
)
724 /* All the extra data is in the last page of the segment.
725 We can just zero it. */
730 /* Zero the final part of the last page of the segment. */
731 if ((c
->prot
& PROT_WRITE
) == 0)
734 if (__mprotect ((caddr_t
) (zero
& ~(_dl_pagesize
- 1)),
735 _dl_pagesize
, c
->prot
|PROT_WRITE
) < 0)
736 lose (errno
, "cannot change memory protections");
738 memset ((void *) zero
, 0, zeropage
- zero
);
739 if ((c
->prot
& PROT_WRITE
) == 0)
740 __mprotect ((caddr_t
) (zero
& ~(_dl_pagesize
- 1)),
741 _dl_pagesize
, c
->prot
);
744 if (zeroend
> zeropage
)
746 /* Map the remaining zero pages in from the zero fill FD. */
748 mapat
= __mmap ((caddr_t
) zeropage
, zeroend
- zeropage
,
749 c
->prot
, MAP_ANON
|MAP_PRIVATE
|MAP_FIXED
,
751 if (mapat
== MAP_FAILED
)
752 lose (errno
, "cannot map zero-fill pages");
761 /* There was no PT_PHDR specified. We need to find the phdr in the
762 load image ourselves. We assume it is in fact in the load image
763 somewhere, and that the first load command starts at the
764 beginning of the file and thus contains the ELF file header. */
765 ElfW(Addr
) bof
= l
->l_addr
+ loadcmds
[0].mapstart
;
766 assert (loadcmds
[0].mapoff
== 0);
767 l
->l_phdr
= (void *) (bof
+ ((const ElfW(Ehdr
) *) bof
)->e_phoff
);
770 /* Adjust the PT_PHDR value by the runtime load address. */
771 (ElfW(Addr
)) l
->l_phdr
+= l
->l_addr
;
774 /* We are done mapping in the file. We no longer need the descriptor. */
777 if (l
->l_type
== lt_library
&& type
== ET_EXEC
)
778 l
->l_type
= lt_executable
;
783 LOSE ("object file has no dynamic section");
786 (ElfW(Addr
)) l
->l_ld
+= l
->l_addr
;
788 l
->l_entry
+= l
->l_addr
;
792 const size_t nibbles
= sizeof (void *) * 2;
793 char buf1
[nibbles
+ 1];
794 char buf2
[nibbles
+ 1];
795 char buf3
[nibbles
+ 1];
797 buf1
[nibbles
] = '\0';
798 buf2
[nibbles
] = '\0';
799 buf3
[nibbles
] = '\0';
801 memset (buf1
, '0', nibbles
);
802 memset (buf2
, '0', nibbles
);
803 memset (buf3
, '0', nibbles
);
804 _itoa_word ((unsigned long int) l
->l_ld
, &buf1
[nibbles
], 16, 0);
805 _itoa_word ((unsigned long int) l
->l_addr
, &buf2
[nibbles
], 16, 0);
806 _itoa_word (maplength
, &buf3
[nibbles
], 16, 0);
808 _dl_debug_message (1, " dynamic: 0x", buf1
, " base: 0x", buf2
,
809 " size: 0x", buf3
, "\n", NULL
);
810 memset (buf1
, '0', nibbles
);
811 memset (buf2
, '0', nibbles
);
812 memset (buf3
, ' ', nibbles
);
813 _itoa_word ((unsigned long int) l
->l_entry
, &buf1
[nibbles
], 16, 0);
814 _itoa_word ((unsigned long int) l
->l_phdr
, &buf2
[nibbles
], 16, 0);
815 _itoa_word (l
->l_phnum
, &buf3
[nibbles
], 10, 0);
816 _dl_debug_message (1, " entry: 0x", buf1
, " phdr: 0x", buf2
,
817 " phnum: ", buf3
, "\n\n", NULL
);
820 elf_get_dynamic_info (l
->l_ld
, l
->l_info
);
821 if (l
->l_info
[DT_HASH
])
827 /* Print search path. */
829 print_search_path (struct r_search_path_elem
**list
,
830 const char *what
, const char *name
)
832 char buf
[max_dirnamelen
+ max_capstrlen
];
836 _dl_debug_message (1, " search path=", NULL
);
838 while (*list
!= NULL
&& (*list
)->what
== what
) /* Yes, ==. */
840 char *endp
= __mempcpy (buf
, (*list
)->dirname
, (*list
)->dirnamelen
);
843 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
844 if ((*list
)->status
[cnt
] != nonexisting
)
846 char *cp
= __mempcpy (endp
, capstr
[cnt
].str
, capstr
[cnt
].len
);
848 _dl_debug_message (0, first
? "" : ":", buf
, NULL
);
856 _dl_debug_message (0, "\t\t(", what
, " from file ",
857 name
[0] ? name
: _dl_argv
[0], ")\n", NULL
);
859 _dl_debug_message (0, "\t\t(", what
, ")\n", NULL
);
862 /* Try to open NAME in one of the directories in DIRS.
863 Return the fd, or -1. If successful, fill in *REALNAME
864 with the malloc'd full directory name. */
867 open_path (const char *name
, size_t namelen
, int preloaded
,
868 struct r_search_path_elem
**dirs
,
873 const char *current_what
= NULL
;
875 if (dirs
== NULL
|| *dirs
== NULL
)
877 __set_errno (ENOENT
);
881 buf
= __alloca (max_dirnamelen
+ max_capstrlen
+ namelen
+ 1);
884 struct r_search_path_elem
*this_dir
= *dirs
;
888 /* If we are debugging the search for libraries print the path
889 now if it hasn't happened now. */
890 if (_dl_debug_libs
&& current_what
!= this_dir
->what
)
892 current_what
= this_dir
->what
;
893 print_search_path (dirs
, current_what
, this_dir
->where
);
896 for (cnt
= 0; fd
== -1 && cnt
< ncapstr
; ++cnt
)
898 /* Skip this directory if we know it does not exist. */
899 if (this_dir
->status
[cnt
] == nonexisting
)
903 ((char *) __mempcpy (__mempcpy (__mempcpy (buf
, this_dir
->dirname
,
904 this_dir
->dirnamelen
),
905 capstr
[cnt
].str
, capstr
[cnt
].len
),
909 /* Print name we try if this is wanted. */
911 _dl_debug_message (1, " trying file=", buf
, "\n", NULL
);
913 fd
= __open (buf
, O_RDONLY
);
914 if (this_dir
->status
[cnt
] == unknown
)
916 this_dir
->status
[cnt
] = existing
;
919 /* We failed to open machine dependent library. Let's
920 test whether there is any directory at all. */
923 buf
[this_dir
->dirnamelen
+ capstr
[cnt
].len
] = '\0';
925 if (__xstat (_STAT_VER
, buf
, &st
) != 0
926 || ! S_ISDIR (st
.st_mode
))
927 /* The directory does not exist ot it is no directory. */
928 this_dir
->status
[cnt
] = nonexisting
;
930 this_dir
->status
[cnt
] = existing
;
933 if (fd
!= -1 && preloaded
&& __libc_enable_secure
)
935 /* This is an extra security effort to make sure nobody can
936 preload broken shared objects which are in the trusted
937 directories and so exploit the bugs. */
940 if (__fxstat (_STAT_VER
, fd
, &st
) != 0
941 || (st
.st_mode
& S_ISUID
) == 0)
943 /* The shared object cannot be tested for being SUID
944 or this bit is not set. In this case we must not
948 /* We simply ignore the file, signal this by setting
949 the error value which would have been set by `open'. */
957 *realname
= malloc (buflen
);
958 if (*realname
!= NULL
)
960 memcpy (*realname
, buf
, buflen
);
965 /* No memory for the name, we certainly won't be able
966 to load and link it. */
971 if (errno
!= ENOENT
&& errno
!= EACCES
)
972 /* The file exists and is readable, but something went wrong. */
975 while (*++dirs
!= NULL
);
980 /* Map in the shared object file NAME. */
984 _dl_map_object (struct link_map
*loader
, const char *name
, int preloaded
,
985 int type
, int trace_mode
)
992 /* Look for this name among those already loaded. */
993 for (l
= _dl_loaded
; l
; l
= l
->l_next
)
995 /* If the requested name matches the soname of a loaded object,
996 use that object. Elide this check for names that have not
998 if (l
->l_opencount
<= 0)
1000 if (!_dl_name_match_p (name
, l
))
1004 if (l
->l_info
[DT_SONAME
] == NULL
)
1007 soname
= (const char *) (l
->l_addr
1008 + l
->l_info
[DT_STRTAB
]->d_un
.d_ptr
1009 + l
->l_info
[DT_SONAME
]->d_un
.d_val
);
1010 if (strcmp (name
, soname
) != 0)
1013 /* We have a match on a new name -- cache it. */
1014 add_name_to_object (l
, local_strdup (soname
));
1017 /* We have a match -- bump the reference count and return it. */
1022 /* Display information if we are debugging. */
1023 if (_dl_debug_files
&& loader
!= NULL
)
1024 _dl_debug_message (1, "\nfile=", name
, "; needed by ",
1025 loader
->l_name
[0] ? loader
->l_name
: _dl_argv
[0],
1028 if (strchr (name
, '/') == NULL
)
1030 /* Search for NAME in several places. */
1032 size_t namelen
= strlen (name
) + 1;
1035 _dl_debug_message (1, "find library=", name
, "; searching\n", NULL
);
1039 /* First try the DT_RPATH of the dependent object that caused NAME
1040 to be loaded. Then that object's dependent, and on up. */
1041 for (l
= loader
; fd
== -1 && l
; l
= l
->l_loader
)
1042 if (l
&& l
->l_info
[DT_RPATH
])
1044 /* Make sure the cache information is available. */
1045 if (l
->l_rpath_dirs
== NULL
)
1047 size_t ptrval
= (l
->l_addr
1048 + l
->l_info
[DT_STRTAB
]->d_un
.d_ptr
1049 + l
->l_info
[DT_RPATH
]->d_un
.d_val
);
1051 decompose_rpath ((const char *) ptrval
, 0,
1052 "RPATH", l
->l_name
);
1055 if (l
->l_rpath_dirs
!= (struct r_search_path_elem
**) -1l)
1056 fd
= open_path (name
, namelen
, preloaded
, l
->l_rpath_dirs
,
1060 /* If dynamically linked, try the DT_RPATH of the executable itself
1061 and the LD_LIBRARY_PATH environment variable. */
1063 if (fd
== -1 && l
&& l
->l_type
!= lt_loaded
1064 && l
->l_rpath_dirs
!= (struct r_search_path_elem
**) -1l)
1065 fd
= open_path (name
, namelen
, preloaded
, l
->l_rpath_dirs
, &realname
);
1067 /* This is used if a static binary uses dynamic loading and there
1068 is a LD_LIBRARY_PATH given. */
1069 if (fd
== -1 && fake_path_list
!= NULL
)
1070 fd
= open_path (name
, namelen
, preloaded
, fake_path_list
, &realname
);
1074 /* Check the list of libraries in the file /etc/ld.so.cache,
1075 for compatibility with Linux's ldconfig program. */
1076 extern const char *_dl_load_cache_lookup (const char *name
);
1077 const char *cached
= _dl_load_cache_lookup (name
);
1080 fd
= __open (cached
, O_RDONLY
);
1083 realname
= local_strdup (cached
);
1084 if (realname
== NULL
)
1093 /* Finally, try the default path. */
1095 fd
= open_path (name
, namelen
, preloaded
, rtld_search_dirs
, &realname
);
1097 /* Add another newline when we a tracing the library loading. */
1099 _dl_debug_message (1, "\n", NULL
);
1103 fd
= __open (name
, O_RDONLY
);
1106 realname
= local_strdup (name
);
1107 if (realname
== NULL
)
1117 name_copy
= local_strdup (name
);
1118 if (name_copy
== NULL
)
1129 /* We haven't found an appropriate library. But since we
1130 are only interested in the list of libraries this isn't
1131 so severe. Fake an entry with all the information we
1133 static const ElfW(Symndx
) dummy_bucket
= STN_UNDEF
;
1135 /* Enter the new object in the list of loaded objects. */
1136 if ((name_copy
= local_strdup (name
)) == NULL
1137 || (l
= _dl_new_object (name_copy
, name
, type
)) == NULL
)
1138 _dl_signal_error (ENOMEM
, name
,
1139 "cannot create shared object descriptor");
1140 /* We use an opencount of 0 as a sign for the faked entry. */
1143 l
->l_buckets
= &dummy_bucket
;
1150 _dl_signal_error (errno
, name
, "cannot open shared object file");
1153 return _dl_map_object_from_fd (name_copy
, fd
, realname
, loader
, type
);