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"
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
;
104 /* Local version of `strdup' function. */
106 local_strdup (const char *s
)
108 size_t len
= strlen (s
) + 1;
109 void *new = malloc (len
);
114 return (char *) memcpy (new, s
, len
);
117 /* Add `name' to the list of names for a particular shared object.
118 `name' is expected to have been allocated with malloc and will
119 be freed if the shared object already has this name.
120 Returns false if the object already had this name. */
122 add_name_to_object (struct link_map
*l
, char *name
)
124 struct libname_list
*lnp
, *lastp
;
125 struct libname_list
*newname
;
129 /* No more memory. */
130 _dl_signal_error (ENOMEM
, NULL
, "could not allocate name string");
135 for (lnp
= l
->l_libname
; lnp
!= NULL
; lastp
= lnp
, lnp
= lnp
->next
)
136 if (strcmp (name
, lnp
->name
) == 0)
142 newname
= malloc (sizeof *newname
);
145 /* No more memory. */
146 _dl_signal_error (ENOMEM
, name
, "cannot allocate name record");
150 /* The object should have a libname set from _dl_new_object. */
151 assert (lastp
!= NULL
);
153 newname
->name
= name
;
154 newname
->next
= NULL
;
155 lastp
->next
= newname
;
160 /* Implement cache for search path lookup. */
163 static size_t max_dirnamelen
;
165 static inline struct r_search_path_elem
**
166 fillin_rpath (char *rpath
, struct r_search_path_elem
**result
, const char *sep
,
167 const char **trusted
, const char *what
, const char *where
)
172 while ((cp
= __strsep (&rpath
, sep
)) != NULL
)
174 struct r_search_path_elem
*dirp
;
175 size_t len
= strlen (cp
);
176 /* Remove trailing slashes. */
177 while (len
> 1 && cp
[len
- 1] == '/')
180 /* Make sure we don't use untrusted directories if we run SUID. */
183 const char **trun
= trusted
;
185 /* All trusted directory must be complete name. */
190 && (memcmp (*trun
, cp
, len
) != 0 || (*trun
)[len
] != '\0'))
194 /* It's no trusted directory, skip it. */
202 /* See if this directory is already known. */
203 for (dirp
= all_dirs
; dirp
!= NULL
; dirp
= dirp
->next
)
204 if (dirp
->dirnamelen
== len
&& strcmp (cp
, dirp
->dirname
) == 0)
209 /* It is available, see whether it's in our own list. */
211 for (cnt
= 0; cnt
< nelems
; ++cnt
)
212 if (result
[cnt
] == dirp
)
216 result
[nelems
++] = dirp
;
220 /* It's a new directory. Create an entry and add it. */
221 dirp
= (struct r_search_path_elem
*) malloc (sizeof (*dirp
));
223 _dl_signal_error (ENOMEM
, NULL
,
224 "cannot create cache for search path");
226 dirp
->dirnamelen
= len
;
227 /* We have to make sure all the relative directories are never
228 ignored. The current directory might change and all our
229 saved information would be void. */
230 dirp
->dirstatus
= cp
[0] != '/' ? existing
: unknown
;
232 /* Add the name of the machine dependent directory if a machine
234 if (_dl_platform
!= NULL
)
238 dirp
->machdirnamelen
= len
+ _dl_platformlen
+ 1;
239 tmp
= (char *) malloc (len
+ _dl_platformlen
+ 2);
241 _dl_signal_error (ENOMEM
, NULL
,
242 "cannot create cache for search path");
244 tmp
= __mempcpy (tmp
, cp
, len
);
245 tmp
= __mempcpy (tmp
, _dl_platform
, _dl_platformlen
);
249 dirp
->machdirstatus
= dirp
->dirstatus
;
251 if (max_dirnamelen
< dirp
->machdirnamelen
)
252 max_dirnamelen
= dirp
->machdirnamelen
;
258 dirp
->machdirnamelen
= len
;
259 dirp
->machdirstatus
= nonexisting
;
261 tmp
= (char *) malloc (len
+ 1);
263 _dl_signal_error (ENOMEM
, NULL
,
264 "cannot create cache for search path");
266 *((char *) __mempcpy (tmp
, cp
, len
)) = '\0';
268 if (max_dirnamelen
< dirp
->dirnamelen
)
269 max_dirnamelen
= dirp
->dirnamelen
;
275 dirp
->next
= all_dirs
;
278 /* Put it in the result array. */
279 result
[nelems
++] = dirp
;
283 /* Terminate the array. */
284 result
[nelems
] = NULL
;
290 static struct r_search_path_elem
**
291 decompose_rpath (const char *rpath
, size_t additional_room
,
292 const char *what
, const char *where
)
294 /* Make a copy we can work with. */
295 char *copy
= strdupa (rpath
);
297 struct r_search_path_elem
**result
;
298 /* First count the number of necessary elements in the result array. */
301 for (cp
= copy
; *cp
!= '\0'; ++cp
)
305 /* Allocate room for the result. NELEMS + 1 + ADDITIONAL_ROOM is an upper
306 limit for the number of necessary entries. */
307 result
= (struct r_search_path_elem
**) malloc ((nelems
+ 1
308 + additional_room
+ 1)
311 _dl_signal_error (ENOMEM
, NULL
, "cannot create cache for search path");
313 return fillin_rpath (copy
, result
, ":", NULL
, what
, where
);
318 _dl_init_paths (const char *llp
)
320 static const char *trusted_dirs
[] =
322 #include "trusted-dirs.h"
326 struct r_search_path_elem
**pelem
;
328 /* We have in `search_path' the information about the RPATH of the
329 dynamic loader. Now fill in the information about the applications
330 RPATH and the directories addressed by the LD_LIBRARY_PATH environment
334 /* Number of elements in the library path. */
337 /* First determine how many elements the LD_LIBRARY_PATH contents has. */
338 if (llp
!= NULL
&& *llp
!= '\0')
340 /* Simply count the number of colons. */
341 const char *cp
= llp
;
353 if (l
->l_type
!= lt_loaded
&& l
->l_info
[DT_RPATH
])
355 /* Allocate room for the search path and fill in information
358 decompose_rpath ((const char *)
359 (l
->l_addr
+ l
->l_info
[DT_STRTAB
]->d_un
.d_ptr
360 + l
->l_info
[DT_RPATH
]->d_un
.d_val
),
361 nllp
, "RPATH", l
->l_name
);
365 /* If we have no LD_LIBRARY_PATH and no RPATH we must tell
366 this somehow to prevent we look this up again and again. */
368 l
->l_rpath_dirs
= (struct r_search_path_elem
**) -1l;
371 l
->l_rpath_dirs
= (struct r_search_path_elem
**)
372 malloc ((nllp
+ 1) * sizeof (*l
->l_rpath_dirs
));
373 if (l
->l_rpath_dirs
== NULL
)
374 _dl_signal_error (ENOMEM
, NULL
,
375 "cannot create cache for search path");
376 l
->l_rpath_dirs
[0] = NULL
;
380 /* We don't need to search the list of fake entries which is searched
381 when no dynamic objects were loaded at this time. */
382 fake_path_list
= NULL
;
386 char *copy
= strdupa (llp
);
388 /* Decompose the LD_LIBRARY_PATH and fill in the result.
389 First search for the next place to enter elements. */
390 struct r_search_path_elem
**result
= l
->l_rpath_dirs
;
391 while (*result
!= NULL
)
394 /* We need to take care that the LD_LIBRARY_PATH environment
395 variable can contain a semicolon. */
396 (void) fillin_rpath (copy
, result
, ":;",
397 __libc_enable_secure
? trusted_dirs
: NULL
,
398 "LD_LIBRARY_PATH", NULL
);
403 /* This is a statically linked program but we still have to
404 take care for the LD_LIBRARY_PATH environment variable. We
405 use a fake link_map entry. This will only contain the
406 l_rpath_dirs information. */
409 fake_path_list
= NULL
;
412 fake_path_list
= (struct r_search_path_elem
**)
413 malloc ((nllp
+ 1) * sizeof (struct r_search_path_elem
*));
414 if (fake_path_list
== NULL
)
415 _dl_signal_error (ENOMEM
, NULL
,
416 "cannot create cache for search path");
418 (void) fillin_rpath (local_strdup (llp
), fake_path_list
, ":;",
419 __libc_enable_secure
? trusted_dirs
: NULL
,
420 "LD_LIBRARY_PATH", NULL
);
424 /* Now set up the rest of the rtld_search_dirs. */
425 for (pelem
= rtld_search_dirs
; *pelem
!= NULL
; ++pelem
)
427 struct r_search_path_elem
*relem
= *pelem
;
429 if (_dl_platform
!= NULL
)
433 relem
->machdirnamelen
= relem
->dirnamelen
+ _dl_platformlen
+ 1;
434 tmp
= (char *) malloc (relem
->machdirnamelen
+ 1);
436 _dl_signal_error (ENOMEM
, NULL
,
437 "cannot create cache for search path");
440 tmp
= __mempcpy (tmp
, relem
->dirname
, relem
->dirnamelen
);
441 tmp
= __mempcpy (tmp
, _dl_platform
, _dl_platformlen
);
444 relem
->dirname
= orig
;
446 relem
->machdirstatus
= unknown
;
448 if (max_dirnamelen
< relem
->machdirnamelen
)
449 max_dirnamelen
= relem
->machdirnamelen
;
453 relem
->machdirnamelen
= relem
->dirnamelen
;
454 relem
->machdirstatus
= nonexisting
;
456 if (max_dirnamelen
< relem
->dirnamelen
)
457 max_dirnamelen
= relem
->dirnamelen
;
460 relem
->what
= "system search path";
466 /* Map in the shared object NAME, actually located in REALNAME, and already
470 _dl_map_object_from_fd (char *name
, int fd
, char *realname
,
471 struct link_map
*loader
, int l_type
)
473 struct link_map
*l
= NULL
;
474 void *file_mapping
= NULL
;
475 size_t mapping_size
= 0;
477 #define LOSE(s) lose (0, (s))
478 void lose (int code
, const char *msg
)
482 __munmap (file_mapping
, mapping_size
);
485 /* Remove the stillborn object from the list and free it. */
487 l
->l_prev
->l_next
= l
->l_next
;
489 l
->l_next
->l_prev
= l
->l_prev
;
493 _dl_signal_error (code
, name
, msg
);
494 free (name
); /* Hmmm. Can this leak memory? Better
495 than a segfault, anyway. */
498 inline caddr_t
map_segment (ElfW(Addr
) mapstart
, size_t len
,
499 int prot
, int fixed
, off_t offset
)
501 caddr_t mapat
= __mmap ((caddr_t
) mapstart
, len
, prot
,
502 fixed
|MAP_COPY
|MAP_FILE
,
504 if (mapat
== MAP_FAILED
)
505 lose (errno
, "failed to map segment from shared object");
509 /* Make sure LOCATION is mapped in. */
510 void *map (off_t location
, size_t size
)
512 if ((off_t
) mapping_size
<= location
+ (off_t
) size
)
516 __munmap (file_mapping
, mapping_size
);
517 mapping_size
= (location
+ size
+ 1 + _dl_pagesize
- 1);
518 mapping_size
&= ~(_dl_pagesize
- 1);
519 result
= __mmap (file_mapping
, mapping_size
, PROT_READ
,
520 MAP_COPY
|MAP_FILE
, fd
, 0);
521 if (result
== MAP_FAILED
)
522 lose (errno
, "cannot map file data");
523 file_mapping
= result
;
525 return file_mapping
+ location
;
528 const ElfW(Ehdr
) *header
;
529 const ElfW(Phdr
) *phdr
;
530 const ElfW(Phdr
) *ph
;
534 /* Look again to see if the real name matched another already loaded. */
535 for (l
= _dl_loaded
; l
; l
= l
->l_next
)
536 if (! strcmp (realname
, l
->l_name
))
538 /* The object is already loaded.
539 Just bump its reference count and return it. */
542 /* If the name is not in the list of names for this object add
545 add_name_to_object (l
, name
);
550 /* Print debugging message. */
552 _dl_debug_message (1, "file=", name
, "; generating link map\n", NULL
);
554 /* Map in the first page to read the header. */
555 header
= map (0, sizeof *header
);
557 /* Check the header for basic validity. */
558 if (*(Elf32_Word
*) &header
->e_ident
!=
559 #if BYTE_ORDER == LITTLE_ENDIAN
560 ((ELFMAG0
<< (EI_MAG0
* 8)) |
561 (ELFMAG1
<< (EI_MAG1
* 8)) |
562 (ELFMAG2
<< (EI_MAG2
* 8)) |
563 (ELFMAG3
<< (EI_MAG3
* 8)))
565 ((ELFMAG0
<< (EI_MAG3
* 8)) |
566 (ELFMAG1
<< (EI_MAG2
* 8)) |
567 (ELFMAG2
<< (EI_MAG1
* 8)) |
568 (ELFMAG3
<< (EI_MAG0
* 8)))
571 LOSE ("invalid ELF header");
572 #define ELF32_CLASS ELFCLASS32
573 #define ELF64_CLASS ELFCLASS64
574 if (header
->e_ident
[EI_CLASS
] != ELFW(CLASS
))
575 LOSE ("ELF file class not " STRING(__ELF_NATIVE_CLASS
) "-bit");
576 if (header
->e_ident
[EI_DATA
] != byteorder
)
577 LOSE ("ELF file data encoding not " byteorder_name
);
578 if (header
->e_ident
[EI_VERSION
] != EV_CURRENT
)
579 LOSE ("ELF file version ident not " STRING(EV_CURRENT
));
580 if (header
->e_version
!= EV_CURRENT
)
581 LOSE ("ELF file version not " STRING(EV_CURRENT
));
582 if (! elf_machine_matches_host (header
->e_machine
))
583 LOSE ("ELF file machine architecture not " ELF_MACHINE_NAME
);
584 if (header
->e_phentsize
!= sizeof (ElfW(Phdr
)))
585 LOSE ("ELF file's phentsize not the expected size");
589 if (_dl_zerofd
== -1)
591 _dl_zerofd
= _dl_sysdep_open_zero_fill ();
592 if (_dl_zerofd
== -1)
595 _dl_signal_error (errno
, NULL
, "cannot open zero fill device");
600 /* Enter the new object in the list of loaded objects. */
601 l
= _dl_new_object (realname
, name
, l_type
);
603 lose (ENOMEM
, "cannot create shared object descriptor");
605 l
->l_loader
= loader
;
607 /* Extract the remaining details we need from the ELF header
608 and then map in the program header table. */
609 l
->l_entry
= header
->e_entry
;
610 type
= header
->e_type
;
611 l
->l_phnum
= header
->e_phnum
;
612 phdr
= map (header
->e_phoff
, l
->l_phnum
* sizeof (ElfW(Phdr
)));
615 /* Scan the program header table, collecting its load commands. */
618 ElfW(Addr
) mapstart
, mapend
, dataend
, allocend
;
621 } loadcmds
[l
->l_phnum
], *c
;
622 size_t nloadcmds
= 0;
627 for (ph
= phdr
; ph
< &phdr
[l
->l_phnum
]; ++ph
)
630 /* These entries tell us where to find things once the file's
631 segments are mapped in. We record the addresses it says
632 verbatim, and later correct for the run-time load address. */
634 l
->l_ld
= (void *) ph
->p_vaddr
;
637 l
->l_phdr
= (void *) ph
->p_vaddr
;
641 /* A load command tells us to map in part of the file.
642 We record the load commands and process them all later. */
643 if (ph
->p_align
% _dl_pagesize
!= 0)
644 LOSE ("ELF load command alignment not page-aligned");
645 if ((ph
->p_vaddr
- ph
->p_offset
) % ph
->p_align
)
646 LOSE ("ELF load command address/offset not properly aligned");
648 struct loadcmd
*c
= &loadcmds
[nloadcmds
++];
649 c
->mapstart
= ph
->p_vaddr
& ~(ph
->p_align
- 1);
650 c
->mapend
= ((ph
->p_vaddr
+ ph
->p_filesz
+ _dl_pagesize
- 1)
651 & ~(_dl_pagesize
- 1));
652 c
->dataend
= ph
->p_vaddr
+ ph
->p_filesz
;
653 c
->allocend
= ph
->p_vaddr
+ ph
->p_memsz
;
654 c
->mapoff
= ph
->p_offset
& ~(ph
->p_align
- 1);
656 if (ph
->p_flags
& PF_R
)
657 c
->prot
|= PROT_READ
;
658 if (ph
->p_flags
& PF_W
)
659 c
->prot
|= PROT_WRITE
;
660 if (ph
->p_flags
& PF_X
)
661 c
->prot
|= PROT_EXEC
;
666 /* We are done reading the file's headers now. Unmap them. */
667 __munmap (file_mapping
, mapping_size
);
669 /* Now process the load commands and map segments into memory. */
672 /* Length of the sections to be loaded. */
673 maplength
= loadcmds
[nloadcmds
- 1].allocend
- c
->mapstart
;
675 if (type
== ET_DYN
|| type
== ET_REL
)
677 /* This is a position-independent shared object. We can let the
678 kernel map it anywhere it likes, but we must have space for all
679 the segments in their specified positions relative to the first.
680 So we map the first segment without MAP_FIXED, but with its
681 extent increased to cover all the segments. Then we remove
682 access from excess portion, and there is known sufficient space
683 there to remap from the later segments.
685 As a refinement, sometimes we have an address that we would
686 prefer to map such objects at; but this is only a preference,
687 the OS can do whatever it likes. */
690 mappref
= (ELF_PREFERRED_ADDRESS (loader
, maplength
, c
->mapstart
)
691 - MAP_BASE_ADDR (l
));
692 mapat
= map_segment (mappref
, maplength
, c
->prot
, 0, c
->mapoff
);
693 l
->l_addr
= (ElfW(Addr
)) mapat
- c
->mapstart
;
695 /* Change protection on the excess portion to disallow all access;
696 the portions we do not remap later will be inaccessible as if
697 unallocated. Then jump into the normal segment-mapping loop to
698 handle the portion of the segment past the end of the file
700 __mprotect ((caddr_t
) (l
->l_addr
+ c
->mapend
),
701 loadcmds
[nloadcmds
- 1].allocend
- c
->mapend
,
707 /* Notify ELF_PREFERRED_ADDRESS that we have to load this one
709 ELF_FIXED_ADDRESS (loader
, c
->mapstart
);
712 while (c
< &loadcmds
[nloadcmds
])
714 if (c
->mapend
> c
->mapstart
)
715 /* Map the segment contents from the file. */
716 map_segment (l
->l_addr
+ c
->mapstart
, c
->mapend
- c
->mapstart
,
717 c
->prot
, MAP_FIXED
, c
->mapoff
);
720 if (c
->allocend
> c
->dataend
)
722 /* Extra zero pages should appear at the end of this segment,
723 after the data mapped from the file. */
724 ElfW(Addr
) zero
, zeroend
, zeropage
;
726 zero
= l
->l_addr
+ c
->dataend
;
727 zeroend
= l
->l_addr
+ c
->allocend
;
728 zeropage
= (zero
+ _dl_pagesize
- 1) & ~(_dl_pagesize
- 1);
730 if (zeroend
< zeropage
)
731 /* All the extra data is in the last page of the segment.
732 We can just zero it. */
737 /* Zero the final part of the last page of the segment. */
738 if ((c
->prot
& PROT_WRITE
) == 0)
741 if (__mprotect ((caddr_t
) (zero
& ~(_dl_pagesize
- 1)),
742 _dl_pagesize
, c
->prot
|PROT_WRITE
) < 0)
743 lose (errno
, "cannot change memory protections");
745 memset ((void *) zero
, 0, zeropage
- zero
);
746 if ((c
->prot
& PROT_WRITE
) == 0)
747 __mprotect ((caddr_t
) (zero
& ~(_dl_pagesize
- 1)),
748 _dl_pagesize
, c
->prot
);
751 if (zeroend
> zeropage
)
753 /* Map the remaining zero pages in from the zero fill FD. */
755 mapat
= __mmap ((caddr_t
) zeropage
, zeroend
- zeropage
,
756 c
->prot
, MAP_ANON
|MAP_PRIVATE
|MAP_FIXED
,
758 if (mapat
== MAP_FAILED
)
759 lose (errno
, "cannot map zero-fill pages");
768 /* There was no PT_PHDR specified. We need to find the phdr in the
769 load image ourselves. We assume it is in fact in the load image
770 somewhere, and that the first load command starts at the
771 beginning of the file and thus contains the ELF file header. */
772 ElfW(Addr
) bof
= l
->l_addr
+ loadcmds
[0].mapstart
;
773 assert (loadcmds
[0].mapoff
== 0);
774 l
->l_phdr
= (void *) (bof
+ ((const ElfW(Ehdr
) *) bof
)->e_phoff
);
777 /* Adjust the PT_PHDR value by the runtime load address. */
778 (ElfW(Addr
)) l
->l_phdr
+= l
->l_addr
;
781 /* We are done mapping in the file. We no longer need the descriptor. */
784 if (l
->l_type
== lt_library
&& type
== ET_EXEC
)
785 l
->l_type
= lt_executable
;
790 LOSE ("object file has no dynamic section");
793 (ElfW(Addr
)) l
->l_ld
+= l
->l_addr
;
795 l
->l_entry
+= l
->l_addr
;
799 const size_t nibbles
= sizeof (void *) * 2;
800 char buf1
[nibbles
+ 1];
801 char buf2
[nibbles
+ 1];
802 char buf3
[nibbles
+ 1];
804 buf1
[nibbles
] = '\0';
805 buf2
[nibbles
] = '\0';
806 buf3
[nibbles
] = '\0';
808 memset (buf1
, '0', nibbles
);
809 memset (buf2
, '0', nibbles
);
810 memset (buf3
, '0', nibbles
);
811 _itoa_word ((unsigned long int) l
->l_ld
, &buf1
[nibbles
], 16, 0);
812 _itoa_word ((unsigned long int) l
->l_addr
, &buf2
[nibbles
], 16, 0);
813 _itoa_word (maplength
, &buf3
[nibbles
], 16, 0);
815 _dl_debug_message (1, " dynamic: 0x", buf1
, " base: 0x", buf2
,
816 " size: 0x", buf3
, "\n", NULL
);
817 memset (buf1
, '0', nibbles
);
818 memset (buf2
, '0', nibbles
);
819 memset (buf3
, ' ', nibbles
);
820 _itoa_word ((unsigned long int) l
->l_entry
, &buf1
[nibbles
], 16, 0);
821 _itoa_word ((unsigned long int) l
->l_phdr
, &buf2
[nibbles
], 16, 0);
822 _itoa_word (l
->l_phnum
, &buf3
[nibbles
], 10, 0);
823 _dl_debug_message (1, " entry: 0x", buf1
, " phdr: 0x", buf2
,
824 " phnum: ", buf3
, "\n\n", NULL
);
827 elf_get_dynamic_info (l
->l_ld
, l
->l_info
);
828 if (l
->l_info
[DT_HASH
])
834 /* Print search path. */
836 print_search_path (struct r_search_path_elem
**list
,
837 const char *what
, const char *name
)
841 _dl_debug_message (1, " search path=", NULL
);
843 while (*list
!= NULL
&& (*list
)->what
== what
) /* Yes, ==. */
845 char *buf
= strdupa ((*list
)->dirname
);
847 if ((*list
)->machdirstatus
!= nonexisting
)
849 buf
[(*list
)->machdirnamelen
- 1] = '\0';
850 _dl_debug_message (0, first
? "" : ":", buf
, NULL
);
853 if ((*list
)->dirstatus
!= nonexisting
)
855 buf
[(*list
)->dirnamelen
- 1] = '\0';
856 _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
+ namelen
);
891 struct r_search_path_elem
*this_dir
= *dirs
;
894 /* If we are debugging the search for libraries print the path
895 now if it hasn't happened now. */
896 if (_dl_debug_libs
&& current_what
!= this_dir
->what
)
898 current_what
= this_dir
->what
;
899 print_search_path (dirs
, current_what
, this_dir
->where
);
902 if (this_dir
->machdirstatus
!= nonexisting
)
904 /* Construct the pathname to try. */
905 buflen
= ((char *) __mempcpy (__mempcpy (buf
, this_dir
->dirname
,
906 this_dir
->machdirnamelen
),
910 /* Print name we try if this is wanted. */
912 _dl_debug_message (1, " trying file=", buf
, "\n", NULL
);
914 fd
= __open (buf
, O_RDONLY
);
915 if (this_dir
->machdirstatus
== unknown
)
917 this_dir
->machdirstatus
= existing
;
920 /* We failed to open machine dependent library. Let's
921 test whether there is any directory at all. */
924 buf
[this_dir
->machdirnamelen
- 1] = '\0';
926 if (__xstat (_STAT_VER
, buf
, &st
) != 0
927 || ! S_ISDIR (st
.st_mode
))
928 /* The directory does not exist ot it is no directory. */
929 this_dir
->machdirstatus
= nonexisting
;
931 this_dir
->machdirstatus
= 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 if (fd
== -1 && errno
== ENOENT
&& this_dir
->dirstatus
!= nonexisting
)
959 /* Construct the pathname to try. */
960 buflen
= ((char *) __mempcpy (__mempcpy (buf
, this_dir
->dirname
,
961 this_dir
->dirnamelen
),
965 /* Print name we try if this is wanted. */
967 _dl_debug_message (1, " trying file=", buf
, "\n", NULL
);
969 fd
= __open (buf
, O_RDONLY
);
970 if (this_dir
->dirstatus
== unknown
)
972 this_dir
->dirstatus
= existing
;
974 /* We failed to open library. Let's test whether there
975 is any directory at all. */
976 if (this_dir
->dirnamelen
<= 1)
977 this_dir
->dirstatus
= existing
;
982 buf
[this_dir
->dirnamelen
- 1] = '\0';
984 if (__xstat (_STAT_VER
, buf
, &st
) != 0
985 || ! S_ISDIR (st
.st_mode
))
986 /* The directory does not exist ot it is no directory. */
987 this_dir
->dirstatus
= nonexisting
;
989 this_dir
->dirstatus
= existing
;
991 if (fd
!= -1 && preloaded
&& __libc_enable_secure
)
993 /* This is an extra security effort to make sure nobody can
994 preload broken shared objects which are in the trusted
995 directories and so exploit the bugs. */
998 if (__fxstat (_STAT_VER
, fd
, &st
) != 0
999 || (st
.st_mode
& S_ISUID
) == 0)
1001 /* The shared object cannot be tested for being SUID
1002 or this bit is not set. In this case we must not
1006 /* We simply ignore the file, signal this by setting
1007 the error value which would have been set by `open'. */
1015 *realname
= malloc (buflen
);
1016 if (*realname
!= NULL
)
1018 memcpy (*realname
, buf
, buflen
);
1023 /* No memory for the name, we certainly won't be able
1024 to load and link it. */
1029 if (errno
!= ENOENT
&& errno
!= EACCES
)
1030 /* The file exists and is readable, but something went wrong. */
1033 while (*++dirs
!= NULL
);
1038 /* Map in the shared object file NAME. */
1041 _dl_map_object (struct link_map
*loader
, const char *name
, int preloaded
,
1042 int type
, int trace_mode
)
1049 /* Look for this name among those already loaded. */
1050 for (l
= _dl_loaded
; l
; l
= l
->l_next
)
1052 /* If the requested name matches the soname of a loaded object,
1053 use that object. Elide this check for names that have not
1055 if (l
->l_opencount
<= 0)
1057 if (!_dl_name_match_p (name
, l
))
1061 if (l
->l_info
[DT_SONAME
] == NULL
)
1064 soname
= (const char *) (l
->l_addr
1065 + l
->l_info
[DT_STRTAB
]->d_un
.d_ptr
1066 + l
->l_info
[DT_SONAME
]->d_un
.d_val
);
1067 if (strcmp (name
, soname
) != 0)
1070 /* We have a match on a new name -- cache it. */
1071 add_name_to_object (l
, local_strdup (soname
));
1074 /* We have a match -- bump the reference count and return it. */
1079 /* Display information if we are debugging. */
1080 if (_dl_debug_files
&& loader
!= NULL
)
1081 _dl_debug_message (1, "\nfile=", name
, "; needed by ",
1082 loader
->l_name
[0] ? loader
->l_name
: _dl_argv
[0],
1085 if (strchr (name
, '/') == NULL
)
1087 /* Search for NAME in several places. */
1089 size_t namelen
= strlen (name
) + 1;
1092 _dl_debug_message (1, "find library=", name
, "; searching\n", NULL
);
1096 /* First try the DT_RPATH of the dependent object that caused NAME
1097 to be loaded. Then that object's dependent, and on up. */
1098 for (l
= loader
; fd
== -1 && l
; l
= l
->l_loader
)
1099 if (l
&& l
->l_info
[DT_RPATH
])
1101 /* Make sure the cache information is available. */
1102 if (l
->l_rpath_dirs
== NULL
)
1104 size_t ptrval
= (l
->l_addr
1105 + l
->l_info
[DT_STRTAB
]->d_un
.d_ptr
1106 + l
->l_info
[DT_RPATH
]->d_un
.d_val
);
1108 decompose_rpath ((const char *) ptrval
, 0,
1109 "RPATH", l
->l_name
);
1112 if (l
->l_rpath_dirs
!= (struct r_search_path_elem
**) -1l)
1113 fd
= open_path (name
, namelen
, preloaded
, l
->l_rpath_dirs
,
1117 /* If dynamically linked, try the DT_RPATH of the executable itself
1118 and the LD_LIBRARY_PATH environment variable. */
1120 if (fd
== -1 && l
&& l
->l_type
!= lt_loaded
1121 && l
->l_rpath_dirs
!= (struct r_search_path_elem
**) -1l)
1122 fd
= open_path (name
, namelen
, preloaded
, l
->l_rpath_dirs
, &realname
);
1124 /* This is used if a static binary uses dynamic loading and there
1125 is a LD_LIBRARY_PATH given. */
1126 if (fd
== -1 && fake_path_list
!= NULL
)
1127 fd
= open_path (name
, namelen
, preloaded
, fake_path_list
, &realname
);
1131 /* Check the list of libraries in the file /etc/ld.so.cache,
1132 for compatibility with Linux's ldconfig program. */
1133 extern const char *_dl_load_cache_lookup (const char *name
);
1134 const char *cached
= _dl_load_cache_lookup (name
);
1137 fd
= __open (cached
, O_RDONLY
);
1140 realname
= local_strdup (cached
);
1141 if (realname
== NULL
)
1150 /* Finally, try the default path. */
1152 fd
= open_path (name
, namelen
, preloaded
, rtld_search_dirs
, &realname
);
1154 /* Add another newline when we a tracing the library loading. */
1156 _dl_debug_message (1, "\n", NULL
);
1160 fd
= __open (name
, O_RDONLY
);
1163 realname
= local_strdup (name
);
1164 if (realname
== NULL
)
1174 name_copy
= local_strdup (name
);
1175 if (name_copy
== NULL
)
1186 /* We haven't found an appropriate library. But since we
1187 are only interested in the list of libraries this isn't
1188 so severe. Fake an entry with all the information we
1190 static const ElfW(Symndx
) dummy_bucket
= STN_UNDEF
;
1192 /* Enter the new object in the list of loaded objects. */
1193 if ((name_copy
= local_strdup (name
)) == NULL
1194 || (l
= _dl_new_object (name_copy
, name
, type
)) == NULL
)
1195 _dl_signal_error (ENOMEM
, name
,
1196 "cannot create shared object descriptor");
1197 /* We use an opencount of 0 as a sign for the faked entry. */
1200 l
->l_buckets
= &dummy_bucket
;
1207 _dl_signal_error (errno
, name
, "cannot open shared object file");
1210 return _dl_map_object_from_fd (name_copy
, fd
, realname
, loader
, type
);