1 /* Map in a shared object's segments from the file.
2 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
26 #include <elf/ldsodefs.h>
28 #include <sys/param.h>
30 #include <sys/types.h>
31 #include "dynamic-link.h"
32 #include <stdio-common/_itoa.h>
35 /* On some systems, no flag bits are given to specify file mapping. */
40 /* The right way to map in the shared library files is MAP_COPY, which
41 makes a virtual copy of the data at the time of the mmap call; this
42 guarantees the mapped pages will be consistent even if the file is
43 overwritten. Some losing VM systems like Linux's lack MAP_COPY. All we
44 get is MAP_PRIVATE, which copies each page when it is modified; this
45 means if the file is overwritten, we may at some point get some pages
46 from the new version after starting with pages from the old version. */
48 #define MAP_COPY MAP_PRIVATE
51 /* Some systems link their relocatable objects for another base address
52 than 0. We want to know the base address for these such that we can
53 subtract this address from the segment addresses during mapping.
54 This results in a more efficient address space usage. Defaults to
55 zero for almost all systems. */
57 #define MAP_BASE_ADDR(l) 0
62 #if BYTE_ORDER == BIG_ENDIAN
63 #define byteorder ELFDATA2MSB
64 #define byteorder_name "big-endian"
65 #elif BYTE_ORDER == LITTLE_ENDIAN
66 #define byteorder ELFDATA2LSB
67 #define byteorder_name "little-endian"
69 #error "Unknown BYTE_ORDER " BYTE_ORDER
70 #define byteorder ELFDATANONE
73 #define STRING(x) __STRING (x)
76 /* The fd is not examined when using MAP_ANON. */
80 #define ANONFD _dl_zerofd
83 /* Handle situations where we have a preferred location in memory for
84 the shared objects. */
85 #ifdef ELF_PREFERRED_ADDRESS_DATA
86 ELF_PREFERRED_ADDRESS_DATA
;
88 #ifndef ELF_PREFERRED_ADDRESS
89 #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
91 #ifndef ELF_FIXED_ADDRESS
92 #define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
97 extern const char *_dl_platform
;
98 extern size_t _dl_platformlen
;
100 /* This is a fake list to store the RPATH information for static
102 static struct r_search_path_elem
**fake_path_list
;
104 /* List of the hardware capabilities we might end up using. */
105 static const struct r_strlenpair
*capstr
;
106 static size_t ncapstr
;
107 static size_t max_capstrlen
;
110 /* This function has no public prototype. */
111 extern ssize_t
__libc_read (int, void *, size_t);
114 /* Local version of `strdup' function. */
116 local_strdup (const char *s
)
118 size_t len
= strlen (s
) + 1;
119 void *new = malloc (len
);
124 return (char *) memcpy (new, s
, len
);
127 /* Add `name' to the list of names for a particular shared object.
128 `name' is expected to have been allocated with malloc and will
129 be freed if the shared object already has this name.
130 Returns false if the object already had this name. */
133 add_name_to_object (struct link_map
*l
, char *name
)
135 struct libname_list
*lnp
, *lastp
;
136 struct libname_list
*newname
;
140 /* No more memory. */
141 _dl_signal_error (ENOMEM
, NULL
, "could not allocate name string");
146 for (lnp
= l
->l_libname
; lnp
!= NULL
; lastp
= lnp
, lnp
= lnp
->next
)
147 if (strcmp (name
, lnp
->name
) == 0)
153 newname
= malloc (sizeof *newname
);
156 /* No more memory. */
157 _dl_signal_error (ENOMEM
, name
, "cannot allocate name record");
161 /* The object should have a libname set from _dl_new_object. */
162 assert (lastp
!= NULL
);
164 newname
->name
= name
;
165 newname
->next
= NULL
;
166 lastp
->next
= newname
;
170 /* All known directories in sorted order. */
171 static struct r_search_path_elem
*all_dirs
;
173 /* Standard search directories. */
174 static struct r_search_path_elem
**rtld_search_dirs
;
176 static size_t max_dirnamelen
;
178 static inline struct r_search_path_elem
**
179 fillin_rpath (char *rpath
, struct r_search_path_elem
**result
, const char *sep
,
180 const char **trusted
, const char *what
, const char *where
)
185 while ((cp
= __strsep (&rpath
, sep
)) != NULL
)
187 struct r_search_path_elem
*dirp
;
188 size_t len
= strlen (cp
);
190 /* `strsep' can pass an empty string. This has to be
191 interpreted as `use the current directory'. */
194 static char curwd
[2];
195 cp
= strcpy (curwd
, ".");
198 /* Remove trailing slashes. */
199 while (len
> 1 && cp
[len
- 1] == '/')
202 /* Make sure we don't use untrusted directories if we run SUID. */
205 const char **trun
= trusted
;
207 /* All trusted directories must be complete names. */
212 && (memcmp (*trun
, cp
, len
) != 0
213 || (*trun
)[len
] != '/'
214 || (*trun
)[len
+ 1] != '\0'))
218 /* It's no trusted directory, skip it. */
226 /* See if this directory is already known. */
227 for (dirp
= all_dirs
; dirp
!= NULL
; dirp
= dirp
->next
)
228 if (dirp
->dirnamelen
== len
&& memcmp (cp
, dirp
->dirname
, len
) == 0)
233 /* It is available, see whether it's on our own list. */
235 for (cnt
= 0; cnt
< nelems
; ++cnt
)
236 if (result
[cnt
] == dirp
)
240 result
[nelems
++] = dirp
;
246 /* It's a new directory. Create an entry and add it. */
247 dirp
= (struct r_search_path_elem
*)
248 malloc (sizeof (*dirp
) + ncapstr
* sizeof (enum r_dir_status
));
250 _dl_signal_error (ENOMEM
, NULL
,
251 "cannot create cache for search path");
254 dirp
->dirnamelen
= len
;
256 if (len
> max_dirnamelen
)
257 max_dirnamelen
= len
;
259 /* We have to make sure all the relative directories are never
260 ignored. The current directory might change and all our
261 saved information would be void. */
263 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
264 dirp
->status
[cnt
] = existing
;
266 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
267 dirp
->status
[cnt
] = unknown
;
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
**
289 decompose_rpath (const char *rpath
, size_t additional_room
, const char *where
)
291 /* Make a copy we can work with. */
294 struct r_search_path_elem
**result
;
297 /* First see whether we must forget the RPATH from this object. */
298 if (_dl_inhibit_rpath
!= NULL
&& !__libc_enable_secure
)
300 const char *found
= strstr (_dl_inhibit_rpath
, where
);
303 size_t len
= strlen (where
);
304 if ((found
== _dl_inhibit_rpath
|| found
[-1] == ':')
305 && (found
[len
] == '\0' || found
[len
] == ':'))
307 /* This object is on the list of objects for which the RPATH
309 result
= (struct r_search_path_elem
**)
310 malloc ((additional_room
+ 1) * sizeof (*result
));
312 _dl_signal_error (ENOMEM
, NULL
,
313 "cannot create cache for search path");
321 /* Count the number of necessary elements in the result array. */
322 copy
= local_strdup (rpath
);
324 for (cp
= copy
; *cp
!= '\0'; ++cp
)
328 /* Allocate room for the result. NELEMS + 1 + ADDITIONAL_ROOM is an upper
329 limit for the number of necessary entries. */
330 result
= (struct r_search_path_elem
**) malloc ((nelems
+ 1
331 + additional_room
+ 1)
334 _dl_signal_error (ENOMEM
, NULL
, "cannot create cache for search path");
336 return fillin_rpath (copy
, result
, ":", NULL
, "RPATH", where
);
342 _dl_init_paths (const char *llp
)
344 static const char *system_dirs
[] =
346 #include "trusted-dirs.h"
350 struct r_search_path_elem
*pelem
, **aelem
;
353 /* We have in `search_path' the information about the RPATH of the
354 dynamic loader. Now fill in the information about the applications
355 RPATH and the directories addressed by the LD_LIBRARY_PATH environment
359 /* Number of elements in the library path. */
362 /* First determine how many elements the LD_LIBRARY_PATH contents has. */
363 if (llp
!= NULL
&& *llp
!= '\0')
365 /* Simply count the number of colons. */
366 const char *cp
= llp
;
370 if (*cp
== ':' || *cp
== ';')
378 /* Get the capabilities. */
379 capstr
= _dl_important_hwcaps (_dl_platform
, _dl_platformlen
,
380 &ncapstr
, &max_capstrlen
);
382 /* First set up the rest of the default search directory entries. */
383 aelem
= rtld_search_dirs
= (struct r_search_path_elem
**)
384 malloc ((ncapstr
+ 1) * sizeof (struct r_search_path_elem
*));
386 round_size
= ((2 * sizeof (struct r_search_path_elem
) - 1
387 + ncapstr
* sizeof (enum r_dir_status
))
388 / sizeof (struct r_search_path_elem
));
390 rtld_search_dirs
[0] = (struct r_search_path_elem
*)
391 malloc ((sizeof (system_dirs
) / sizeof (system_dirs
[0]) - 1)
392 * round_size
* sizeof (struct r_search_path_elem
));
393 if (rtld_search_dirs
[0] == NULL
)
394 _dl_signal_error (ENOMEM
, NULL
, "cannot create cache for search path");
396 pelem
= all_dirs
= rtld_search_dirs
[0];
397 for (strp
= system_dirs
; *strp
!= NULL
; ++strp
, pelem
+= round_size
)
403 pelem
->next
= *(strp
+ 1) == NULL
? NULL
: (pelem
+ round_size
);
405 pelem
->what
= "system search path";
408 pelem
->dirnamelen
= strlen (pelem
->dirname
= *strp
);
409 if (pelem
->dirnamelen
> max_dirnamelen
)
410 max_dirnamelen
= pelem
->dirnamelen
;
412 if (pelem
->dirname
[0] != '/')
413 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
414 pelem
->status
[cnt
] = existing
;
416 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
417 pelem
->status
[cnt
] = unknown
;
424 if (l
->l_type
!= lt_loaded
&& l
->l_info
[DT_RPATH
])
426 /* Allocate room for the search path and fill in information
429 decompose_rpath ((const char *)
430 (l
->l_addr
+ l
->l_info
[DT_STRTAB
]->d_un
.d_ptr
431 + l
->l_info
[DT_RPATH
]->d_un
.d_val
),
436 /* If we have no LD_LIBRARY_PATH and no RPATH we must tell
437 this somehow to prevent we look this up again and again. */
439 l
->l_rpath_dirs
= (struct r_search_path_elem
**) -1l;
442 l
->l_rpath_dirs
= (struct r_search_path_elem
**)
443 malloc ((nllp
+ 1) * sizeof (*l
->l_rpath_dirs
));
444 if (l
->l_rpath_dirs
== NULL
)
445 _dl_signal_error (ENOMEM
, NULL
,
446 "cannot create cache for search path");
447 l
->l_rpath_dirs
[0] = NULL
;
451 /* We don't need to search the list of fake entries which is searched
452 when no dynamic objects were loaded at this time. */
453 fake_path_list
= NULL
;
457 char *copy
= local_strdup (llp
);
459 /* Decompose the LD_LIBRARY_PATH and fill in the result.
460 First search for the next place to enter elements. */
461 struct r_search_path_elem
**result
= l
->l_rpath_dirs
;
462 while (*result
!= NULL
)
465 /* We need to take care that the LD_LIBRARY_PATH environment
466 variable can contain a semicolon. */
467 (void) fillin_rpath (copy
, result
, ":;",
468 __libc_enable_secure
? system_dirs
: NULL
,
469 "LD_LIBRARY_PATH", NULL
);
474 /* This is a statically linked program but we still have to
475 take care for the LD_LIBRARY_PATH environment variable. We
476 use a fake link_map entry. This will only contain the
477 l_rpath_dirs information. */
480 fake_path_list
= NULL
;
483 fake_path_list
= (struct r_search_path_elem
**)
484 malloc ((nllp
+ 1) * sizeof (struct r_search_path_elem
*));
485 if (fake_path_list
== NULL
)
486 _dl_signal_error (ENOMEM
, NULL
,
487 "cannot create cache for search path");
489 (void) fillin_rpath (local_strdup (llp
), fake_path_list
, ":;",
490 __libc_enable_secure
? system_dirs
: NULL
,
491 "LD_LIBRARY_PATH", NULL
);
497 /* Map in the shared object NAME, actually located in REALNAME, and already
501 _dl_map_object_from_fd (char *name
, int fd
, char *realname
,
502 struct link_map
*loader
, int l_type
)
504 struct link_map
*l
= NULL
;
506 #define LOSE(s) lose (0, (s))
507 void lose (int code
, const char *msg
)
512 /* Remove the stillborn object from the list and free it. */
514 l
->l_prev
->l_next
= l
->l_next
;
516 l
->l_next
->l_prev
= l
->l_prev
;
520 _dl_signal_error (code
, name
, msg
);
521 free (name
); /* Hmmm. Can this leak memory? Better
522 than a segfault, anyway. */
525 inline caddr_t
map_segment (ElfW(Addr
) mapstart
, size_t len
,
526 int prot
, int fixed
, off_t offset
)
528 caddr_t mapat
= __mmap ((caddr_t
) mapstart
, len
, prot
,
529 fixed
|MAP_COPY
|MAP_FILE
,
531 if (mapat
== MAP_FAILED
)
532 lose (errno
, "failed to map segment from shared object");
536 const ElfW(Ehdr
) *header
;
537 const ElfW(Phdr
) *phdr
;
538 const ElfW(Phdr
) *ph
;
544 /* Look again to see if the real name matched another already loaded. */
545 for (l
= _dl_loaded
; l
; l
= l
->l_next
)
546 if (! strcmp (realname
, l
->l_name
))
548 /* The object is already loaded.
549 Just bump its reference count and return it. */
552 /* If the name is not in the list of names for this object add
555 add_name_to_object (l
, name
);
560 /* Print debugging message. */
562 _dl_debug_message (1, "file=", name
, "; generating link map\n", NULL
);
564 /* Read the header directly. */
565 readbuf
= alloca (_dl_pagesize
);
566 readlength
= __libc_read (fd
, readbuf
, _dl_pagesize
);
567 if (readlength
< (ssize_t
) sizeof(*header
))
568 lose (errno
, "cannot read file data");
569 header
= (void *) readbuf
;
571 /* Check the header for basic validity. */
572 if (*(Elf32_Word
*) &header
->e_ident
!=
573 #if BYTE_ORDER == LITTLE_ENDIAN
574 ((ELFMAG0
<< (EI_MAG0
* 8)) |
575 (ELFMAG1
<< (EI_MAG1
* 8)) |
576 (ELFMAG2
<< (EI_MAG2
* 8)) |
577 (ELFMAG3
<< (EI_MAG3
* 8)))
579 ((ELFMAG0
<< (EI_MAG3
* 8)) |
580 (ELFMAG1
<< (EI_MAG2
* 8)) |
581 (ELFMAG2
<< (EI_MAG1
* 8)) |
582 (ELFMAG3
<< (EI_MAG0
* 8)))
585 LOSE ("invalid ELF header");
586 #define ELF32_CLASS ELFCLASS32
587 #define ELF64_CLASS ELFCLASS64
588 if (header
->e_ident
[EI_CLASS
] != ELFW(CLASS
))
589 LOSE ("ELF file class not " STRING(__ELF_NATIVE_CLASS
) "-bit");
590 if (header
->e_ident
[EI_DATA
] != byteorder
)
591 LOSE ("ELF file data encoding not " byteorder_name
);
592 if (header
->e_ident
[EI_VERSION
] != EV_CURRENT
)
593 LOSE ("ELF file version ident not " STRING(EV_CURRENT
));
594 if (header
->e_version
!= EV_CURRENT
)
595 LOSE ("ELF file version not " STRING(EV_CURRENT
));
596 if (! elf_machine_matches_host (header
->e_machine
))
597 LOSE ("ELF file machine architecture not " ELF_MACHINE_NAME
);
598 if (header
->e_phentsize
!= sizeof (ElfW(Phdr
)))
599 LOSE ("ELF file's phentsize not the expected size");
603 if (_dl_zerofd
== -1)
605 _dl_zerofd
= _dl_sysdep_open_zero_fill ();
606 if (_dl_zerofd
== -1)
609 _dl_signal_error (errno
, NULL
, "cannot open zero fill device");
614 /* Enter the new object in the list of loaded objects. */
615 l
= _dl_new_object (realname
, name
, l_type
);
617 lose (ENOMEM
, "cannot create shared object descriptor");
619 l
->l_loader
= loader
;
621 /* Extract the remaining details we need from the ELF header
622 and then read in the program header table. */
623 l
->l_entry
= header
->e_entry
;
624 type
= header
->e_type
;
625 l
->l_phnum
= header
->e_phnum
;
627 maplength
= header
->e_phnum
* sizeof (ElfW(Phdr
));
628 if (header
->e_phoff
+ maplength
<= readlength
)
629 phdr
= (void *) (readbuf
+ header
->e_phoff
);
632 phdr
= alloca (maplength
);
633 __lseek (fd
, SEEK_SET
, header
->e_phoff
);
634 if (__libc_read (fd
, (void *) phdr
, maplength
) != maplength
)
635 lose (errno
, "cannot read file data");
639 /* Scan the program header table, collecting its load commands. */
642 ElfW(Addr
) mapstart
, mapend
, dataend
, allocend
;
645 } loadcmds
[l
->l_phnum
], *c
;
646 size_t nloadcmds
= 0;
651 for (ph
= phdr
; ph
< &phdr
[l
->l_phnum
]; ++ph
)
654 /* These entries tell us where to find things once the file's
655 segments are mapped in. We record the addresses it says
656 verbatim, and later correct for the run-time load address. */
658 l
->l_ld
= (void *) ph
->p_vaddr
;
661 l
->l_phdr
= (void *) ph
->p_vaddr
;
665 /* A load command tells us to map in part of the file.
666 We record the load commands and process them all later. */
667 if (ph
->p_align
% _dl_pagesize
!= 0)
668 LOSE ("ELF load command alignment not page-aligned");
669 if ((ph
->p_vaddr
- ph
->p_offset
) % ph
->p_align
)
670 LOSE ("ELF load command address/offset not properly aligned");
672 struct loadcmd
*c
= &loadcmds
[nloadcmds
++];
673 c
->mapstart
= ph
->p_vaddr
& ~(ph
->p_align
- 1);
674 c
->mapend
= ((ph
->p_vaddr
+ ph
->p_filesz
+ _dl_pagesize
- 1)
675 & ~(_dl_pagesize
- 1));
676 c
->dataend
= ph
->p_vaddr
+ ph
->p_filesz
;
677 c
->allocend
= ph
->p_vaddr
+ ph
->p_memsz
;
678 c
->mapoff
= ph
->p_offset
& ~(ph
->p_align
- 1);
680 if (ph
->p_flags
& PF_R
)
681 c
->prot
|= PROT_READ
;
682 if (ph
->p_flags
& PF_W
)
683 c
->prot
|= PROT_WRITE
;
684 if (ph
->p_flags
& PF_X
)
685 c
->prot
|= PROT_EXEC
;
690 /* Now process the load commands and map segments into memory. */
693 /* Length of the sections to be loaded. */
694 maplength
= loadcmds
[nloadcmds
- 1].allocend
- c
->mapstart
;
696 if (type
== ET_DYN
|| type
== ET_REL
)
698 /* This is a position-independent shared object. We can let the
699 kernel map it anywhere it likes, but we must have space for all
700 the segments in their specified positions relative to the first.
701 So we map the first segment without MAP_FIXED, but with its
702 extent increased to cover all the segments. Then we remove
703 access from excess portion, and there is known sufficient space
704 there to remap from the later segments.
706 As a refinement, sometimes we have an address that we would
707 prefer to map such objects at; but this is only a preference,
708 the OS can do whatever it likes. */
711 mappref
= (ELF_PREFERRED_ADDRESS (loader
, maplength
, c
->mapstart
)
712 - MAP_BASE_ADDR (l
));
713 mapat
= map_segment (mappref
, maplength
, c
->prot
, 0, c
->mapoff
);
714 l
->l_addr
= (ElfW(Addr
)) mapat
- c
->mapstart
;
716 /* Change protection on the excess portion to disallow all access;
717 the portions we do not remap later will be inaccessible as if
718 unallocated. Then jump into the normal segment-mapping loop to
719 handle the portion of the segment past the end of the file
721 __mprotect ((caddr_t
) (l
->l_addr
+ c
->mapend
),
722 loadcmds
[nloadcmds
- 1].allocend
- c
->mapend
,
728 /* Notify ELF_PREFERRED_ADDRESS that we have to load this one
730 ELF_FIXED_ADDRESS (loader
, c
->mapstart
);
733 while (c
< &loadcmds
[nloadcmds
])
735 if (c
->mapend
> c
->mapstart
)
736 /* Map the segment contents from the file. */
737 map_segment (l
->l_addr
+ c
->mapstart
, c
->mapend
- c
->mapstart
,
738 c
->prot
, MAP_FIXED
, c
->mapoff
);
741 if (c
->allocend
> c
->dataend
)
743 /* Extra zero pages should appear at the end of this segment,
744 after the data mapped from the file. */
745 ElfW(Addr
) zero
, zeroend
, zeropage
;
747 zero
= l
->l_addr
+ c
->dataend
;
748 zeroend
= l
->l_addr
+ c
->allocend
;
749 zeropage
= (zero
+ _dl_pagesize
- 1) & ~(_dl_pagesize
- 1);
751 if (zeroend
< zeropage
)
752 /* All the extra data is in the last page of the segment.
753 We can just zero it. */
758 /* Zero the final part of the last page of the segment. */
759 if ((c
->prot
& PROT_WRITE
) == 0)
762 if (__mprotect ((caddr_t
) (zero
& ~(_dl_pagesize
- 1)),
763 _dl_pagesize
, c
->prot
|PROT_WRITE
) < 0)
764 lose (errno
, "cannot change memory protections");
766 memset ((void *) zero
, 0, zeropage
- zero
);
767 if ((c
->prot
& PROT_WRITE
) == 0)
768 __mprotect ((caddr_t
) (zero
& ~(_dl_pagesize
- 1)),
769 _dl_pagesize
, c
->prot
);
772 if (zeroend
> zeropage
)
774 /* Map the remaining zero pages in from the zero fill FD. */
776 mapat
= __mmap ((caddr_t
) zeropage
, zeroend
- zeropage
,
777 c
->prot
, MAP_ANON
|MAP_PRIVATE
|MAP_FIXED
,
779 if (mapat
== MAP_FAILED
)
780 lose (errno
, "cannot map zero-fill pages");
789 /* There was no PT_PHDR specified. We need to find the phdr in the
790 load image ourselves. We assume it is in fact in the load image
791 somewhere, and that the first load command starts at the
792 beginning of the file and thus contains the ELF file header. */
793 ElfW(Addr
) bof
= l
->l_addr
+ loadcmds
[0].mapstart
;
794 assert (loadcmds
[0].mapoff
== 0);
795 l
->l_phdr
= (void *) (bof
+ ((const ElfW(Ehdr
) *) bof
)->e_phoff
);
798 /* Adjust the PT_PHDR value by the runtime load address. */
799 (ElfW(Addr
)) l
->l_phdr
+= l
->l_addr
;
802 /* We are done mapping in the file. We no longer need the descriptor. */
805 if (l
->l_type
== lt_library
&& type
== ET_EXEC
)
806 l
->l_type
= lt_executable
;
811 LOSE ("object file has no dynamic section");
814 (ElfW(Addr
)) l
->l_ld
+= l
->l_addr
;
816 l
->l_entry
+= l
->l_addr
;
820 const size_t nibbles
= sizeof (void *) * 2;
821 char buf1
[nibbles
+ 1];
822 char buf2
[nibbles
+ 1];
823 char buf3
[nibbles
+ 1];
825 buf1
[nibbles
] = '\0';
826 buf2
[nibbles
] = '\0';
827 buf3
[nibbles
] = '\0';
829 memset (buf1
, '0', nibbles
);
830 memset (buf2
, '0', nibbles
);
831 memset (buf3
, '0', nibbles
);
832 _itoa_word ((unsigned long int) l
->l_ld
, &buf1
[nibbles
], 16, 0);
833 _itoa_word ((unsigned long int) l
->l_addr
, &buf2
[nibbles
], 16, 0);
834 _itoa_word (maplength
, &buf3
[nibbles
], 16, 0);
836 _dl_debug_message (1, " dynamic: 0x", buf1
, " base: 0x", buf2
,
837 " size: 0x", buf3
, "\n", NULL
);
838 memset (buf1
, '0', nibbles
);
839 memset (buf2
, '0', nibbles
);
840 memset (buf3
, ' ', nibbles
);
841 _itoa_word ((unsigned long int) l
->l_entry
, &buf1
[nibbles
], 16, 0);
842 _itoa_word ((unsigned long int) l
->l_phdr
, &buf2
[nibbles
], 16, 0);
843 _itoa_word (l
->l_phnum
, &buf3
[nibbles
], 10, 0);
844 _dl_debug_message (1, " entry: 0x", buf1
, " phdr: 0x", buf2
,
845 " phnum: ", buf3
, "\n\n", NULL
);
848 elf_get_dynamic_info (l
->l_ld
, l
->l_info
);
849 if (l
->l_info
[DT_HASH
])
855 /* Print search path. */
857 print_search_path (struct r_search_path_elem
**list
,
858 const char *what
, const char *name
)
860 char buf
[max_dirnamelen
+ max_capstrlen
];
863 _dl_debug_message (1, " search path=", NULL
);
865 while (*list
!= NULL
&& (*list
)->what
== what
) /* Yes, ==. */
867 char *endp
= __mempcpy (buf
, (*list
)->dirname
, (*list
)->dirnamelen
);
870 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
871 if ((*list
)->status
[cnt
] != nonexisting
)
873 char *cp
= __mempcpy (endp
, capstr
[cnt
].str
, capstr
[cnt
].len
);
875 _dl_debug_message (0, first
? "" : ":", buf
, NULL
);
883 _dl_debug_message (0, "\t\t(", what
, " from file ",
884 name
[0] ? name
: _dl_argv
[0], ")\n", NULL
);
886 _dl_debug_message (0, "\t\t(", what
, ")\n", NULL
);
889 /* Try to open NAME in one of the directories in DIRS.
890 Return the fd, or -1. If successful, fill in *REALNAME
891 with the malloc'd full directory name. */
894 open_path (const char *name
, size_t namelen
, int preloaded
,
895 struct r_search_path_elem
**dirs
,
900 const char *current_what
= NULL
;
902 if (dirs
== NULL
|| *dirs
== NULL
)
904 __set_errno (ENOENT
);
908 buf
= __alloca (max_dirnamelen
+ max_capstrlen
+ namelen
);
911 struct r_search_path_elem
*this_dir
= *dirs
;
916 /* If we are debugging the search for libraries print the path
917 now if it hasn't happened now. */
918 if (_dl_debug_libs
&& current_what
!= this_dir
->what
)
920 current_what
= this_dir
->what
;
921 print_search_path (dirs
, current_what
, this_dir
->where
);
924 edp
= (char *) __mempcpy (buf
, this_dir
->dirname
, this_dir
->dirnamelen
);
925 for (cnt
= 0; fd
== -1 && cnt
< ncapstr
; ++cnt
)
927 /* Skip this directory if we know it does not exist. */
928 if (this_dir
->status
[cnt
] == nonexisting
)
932 ((char *) __mempcpy (__mempcpy (edp
,
933 capstr
[cnt
].str
, capstr
[cnt
].len
),
937 /* Print name we try if this is wanted. */
939 _dl_debug_message (1, " trying file=", buf
, "\n", NULL
);
941 fd
= __open (buf
, O_RDONLY
);
942 if (this_dir
->status
[cnt
] == unknown
)
944 this_dir
->status
[cnt
] = existing
;
947 /* We failed to open machine dependent library. Let's
948 test whether there is any directory at all. */
951 buf
[buflen
- namelen
- 1] = '\0';
953 if (__xstat (_STAT_VER
, buf
, &st
) != 0
954 || ! S_ISDIR (st
.st_mode
))
955 /* The directory does not exist or it is no directory. */
956 this_dir
->status
[cnt
] = nonexisting
;
958 this_dir
->status
[cnt
] = existing
;
961 if (fd
!= -1 && preloaded
&& __libc_enable_secure
)
963 /* This is an extra security effort to make sure nobody can
964 preload broken shared objects which are in the trusted
965 directories and so exploit the bugs. */
968 if (__fxstat (_STAT_VER
, fd
, &st
) != 0
969 || (st
.st_mode
& S_ISUID
) == 0)
971 /* The shared object cannot be tested for being SUID
972 or this bit is not set. In this case we must not
976 /* We simply ignore the file, signal this by setting
977 the error value which would have been set by `open'. */
985 *realname
= malloc (buflen
);
986 if (*realname
!= NULL
)
988 memcpy (*realname
, buf
, buflen
);
993 /* No memory for the name, we certainly won't be able
994 to load and link it. */
999 if (errno
!= ENOENT
&& errno
!= EACCES
)
1000 /* The file exists and is readable, but something went wrong. */
1003 while (*++dirs
!= NULL
);
1008 /* Map in the shared object file NAME. */
1012 _dl_map_object (struct link_map
*loader
, const char *name
, int preloaded
,
1013 int type
, int trace_mode
)
1020 /* Look for this name among those already loaded. */
1021 for (l
= _dl_loaded
; l
; l
= l
->l_next
)
1023 /* If the requested name matches the soname of a loaded object,
1024 use that object. Elide this check for names that have not
1026 if (l
->l_opencount
<= 0)
1028 if (!_dl_name_match_p (name
, l
))
1032 if (l
->l_info
[DT_SONAME
] == NULL
)
1035 soname
= (const char *) (l
->l_addr
1036 + l
->l_info
[DT_STRTAB
]->d_un
.d_ptr
1037 + l
->l_info
[DT_SONAME
]->d_un
.d_val
);
1038 if (strcmp (name
, soname
) != 0)
1041 /* We have a match on a new name -- cache it. */
1042 add_name_to_object (l
, local_strdup (soname
));
1045 /* We have a match -- bump the reference count and return it. */
1050 /* Display information if we are debugging. */
1051 if (_dl_debug_files
&& loader
!= NULL
)
1052 _dl_debug_message (1, "\nfile=", name
, "; needed by ",
1053 loader
->l_name
[0] ? loader
->l_name
: _dl_argv
[0],
1056 if (strchr (name
, '/') == NULL
)
1058 /* Search for NAME in several places. */
1060 size_t namelen
= strlen (name
) + 1;
1063 _dl_debug_message (1, "find library=", name
, "; searching\n", NULL
);
1067 /* First try the DT_RPATH of the dependent object that caused NAME
1068 to be loaded. Then that object's dependent, and on up. */
1069 for (l
= loader
; fd
== -1 && l
; l
= l
->l_loader
)
1070 if (l
&& l
->l_info
[DT_RPATH
])
1072 /* Make sure the cache information is available. */
1073 if (l
->l_rpath_dirs
== NULL
)
1075 size_t ptrval
= (l
->l_addr
1076 + l
->l_info
[DT_STRTAB
]->d_un
.d_ptr
1077 + l
->l_info
[DT_RPATH
]->d_un
.d_val
);
1079 decompose_rpath ((const char *) ptrval
, 0, l
->l_name
);
1082 if (l
->l_rpath_dirs
!= (struct r_search_path_elem
**) -1l)
1083 fd
= open_path (name
, namelen
, preloaded
, l
->l_rpath_dirs
,
1087 /* If dynamically linked, try the DT_RPATH of the executable itself
1088 and the LD_LIBRARY_PATH environment variable. */
1090 if (fd
== -1 && l
&& l
->l_type
!= lt_loaded
1091 && l
->l_rpath_dirs
!= (struct r_search_path_elem
**) -1l)
1092 fd
= open_path (name
, namelen
, preloaded
, l
->l_rpath_dirs
, &realname
);
1094 /* This is used if a static binary uses dynamic loading and there
1095 is a LD_LIBRARY_PATH given. */
1096 if (fd
== -1 && fake_path_list
!= NULL
)
1097 fd
= open_path (name
, namelen
, preloaded
, fake_path_list
, &realname
);
1101 /* Check the list of libraries in the file /etc/ld.so.cache,
1102 for compatibility with Linux's ldconfig program. */
1103 extern const char *_dl_load_cache_lookup (const char *name
);
1104 const char *cached
= _dl_load_cache_lookup (name
);
1107 fd
= __open (cached
, O_RDONLY
);
1110 realname
= local_strdup (cached
);
1111 if (realname
== NULL
)
1120 /* Finally, try the default path. */
1122 fd
= open_path (name
, namelen
, preloaded
, rtld_search_dirs
, &realname
);
1124 /* Add another newline when we a tracing the library loading. */
1126 _dl_debug_message (1, "\n", NULL
);
1130 fd
= __open (name
, O_RDONLY
);
1133 realname
= local_strdup (name
);
1134 if (realname
== NULL
)
1144 name_copy
= local_strdup (name
);
1145 if (name_copy
== NULL
)
1156 /* We haven't found an appropriate library. But since we
1157 are only interested in the list of libraries this isn't
1158 so severe. Fake an entry with all the information we
1160 static const ElfW(Symndx
) dummy_bucket
= STN_UNDEF
;
1162 /* Enter the new object in the list of loaded objects. */
1163 if ((name_copy
= local_strdup (name
)) == NULL
1164 || (l
= _dl_new_object (name_copy
, name
, type
)) == NULL
)
1165 _dl_signal_error (ENOMEM
, name
,
1166 "cannot create shared object descriptor");
1167 /* We use an opencount of 0 as a sign for the faked entry. */
1170 l
->l_buckets
= &dummy_bucket
;
1177 _dl_signal_error (errno
, name
, "cannot open shared object file");
1180 return _dl_map_object_from_fd (name_copy
, fd
, realname
, loader
, type
);