1 /* Map in a shared object's segments from the file.
2 Copyright (C) 1995,96,97,98,99,2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
28 #include <sys/param.h>
30 #include <sys/types.h>
31 #include "dynamic-link.h"
32 #include <stdio-common/_itoa.h>
36 /* On some systems, no flag bits are given to specify file mapping. */
41 /* The right way to map in the shared library files is MAP_COPY, which
42 makes a virtual copy of the data at the time of the mmap call; this
43 guarantees the mapped pages will be consistent even if the file is
44 overwritten. Some losing VM systems like Linux's lack MAP_COPY. All we
45 get is MAP_PRIVATE, which copies each page when it is modified; this
46 means if the file is overwritten, we may at some point get some pages
47 from the new version after starting with pages from the old version. */
49 # define MAP_COPY MAP_PRIVATE
52 /* Some systems link their relocatable objects for another base address
53 than 0. We want to know the base address for these such that we can
54 subtract this address from the segment addresses during mapping.
55 This results in a more efficient address space usage. Defaults to
56 zero for almost all systems. */
58 # define MAP_BASE_ADDR(l) 0
63 #if BYTE_ORDER == BIG_ENDIAN
64 # define byteorder ELFDATA2MSB
65 # define byteorder_name "big-endian"
66 #elif BYTE_ORDER == LITTLE_ENDIAN
67 # define byteorder ELFDATA2LSB
68 # define byteorder_name "little-endian"
70 # error "Unknown BYTE_ORDER " BYTE_ORDER
71 # define byteorder ELFDATANONE
74 #define STRING(x) __STRING (x)
77 /* The fd is not examined when using MAP_ANON. */
81 # define ANONFD _dl_zerofd
84 /* Handle situations where we have a preferred location in memory for
85 the shared objects. */
86 #ifdef ELF_PREFERRED_ADDRESS_DATA
87 ELF_PREFERRED_ADDRESS_DATA
;
89 #ifndef ELF_PREFERRED_ADDRESS
90 # define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
92 #ifndef ELF_FIXED_ADDRESS
93 # define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
98 extern const char *_dl_platform
;
99 extern size_t _dl_platformlen
;
101 /* This is the decomposed LD_LIBRARY_PATH search path. */
102 static struct r_search_path_elem
**env_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
;
109 const unsigned char _dl_pf_to_prot
[8] =
114 [PF_R
| PF_W
] = PROT_READ
| PROT_WRITE
,
116 [PF_R
| PF_X
] = PROT_READ
| PROT_EXEC
,
117 [PF_W
| PF_X
] = PROT_WRITE
| PROT_EXEC
,
118 [PF_R
| PF_W
| PF_X
] = PROT_READ
| PROT_WRITE
| PROT_EXEC
122 /* Get the generated information about the trusted directories. */
123 #include "trusted-dirs.h"
125 static const char system_dirs
[] = SYSTEM_DIRS
;
126 static const size_t system_dirs_len
[] =
132 /* Local version of `strdup' function. */
134 local_strdup (const char *s
)
136 size_t len
= strlen (s
) + 1;
137 void *new = malloc (len
);
142 return (char *) memcpy (new, s
, len
);
147 _dl_dst_count (const char *name
, int is_path
)
149 const char *const start
= name
;
156 /* $ORIGIN is not expanded for SUID/GUID programs.
158 Note that it is no bug that the strings in the first two `strncmp'
159 calls are longer than the sequence which is actually tested. */
160 if ((((strncmp (&name
[1], "ORIGIN}", 6) == 0
161 && (!__libc_enable_secure
162 || ((name
[7] == '\0' || (is_path
&& name
[7] == ':'))
163 && (name
== start
|| (is_path
&& name
[-1] == ':'))))
165 || (strncmp (&name
[1], "PLATFORM}", 8) == 0 && (len
= 9) != 0))
166 && (name
[len
] == '\0' || name
[len
] == '/'
167 || (is_path
&& name
[len
] == ':')))
169 && ((strncmp (&name
[2], "ORIGIN}", 7) == 0
170 && (!__libc_enable_secure
171 || ((name
[9] == '\0' || (is_path
&& name
[9] == ':'))
172 && (name
== start
|| (is_path
&& name
[-1] == ':'))))
174 || (strncmp (&name
[2], "PLATFORM}", 9) == 0
175 && (len
= 11) != 0))))
178 name
= strchr (name
+ len
, '$');
180 while (name
!= NULL
);
187 _dl_dst_substitute (struct link_map
*l
, const char *name
, char *result
,
190 const char *const start
= name
;
191 char *last_elem
, *wp
;
193 /* Now fill the result path. While copying over the string we keep
194 track of the start of the last path element. When we come accross
195 a DST we copy over the value or (if the value is not available)
196 leave the entire path element out. */
197 last_elem
= wp
= result
;
206 /* Note that it is no bug that the strings in the first two `strncmp'
207 calls are longer than the sequence which is actually tested. */
208 if ((((strncmp (&name
[1], "ORIGIN}", 6) == 0 && (len
= 7) != 0)
209 || (strncmp (&name
[1], "PLATFORM}", 8) == 0 && (len
= 9) != 0))
210 && (name
[len
] == '\0' || name
[len
] == '/'
211 || (is_path
&& name
[len
] == ':')))
213 && ((strncmp (&name
[2], "ORIGIN}", 7) == 0 && (len
= 9) != 0)
214 || (strncmp (&name
[2], "PLATFORM}", 9) == 0
215 && (len
= 11) != 0))))
217 repl
= ((len
== 7 || name
[2] == 'O')
218 ? (__libc_enable_secure
219 && ((name
[len
] != '\0'
220 && (!is_path
|| name
[len
] != ':'))
222 && (!is_path
|| name
[-1] != ':')))
223 ? NULL
: l
->l_origin
)
226 if (repl
!= NULL
&& repl
!= (const char *) -1)
228 wp
= __stpcpy (wp
, repl
);
233 /* We cannot use this path element, the value of the
234 replacement is unknown. */
237 while (*name
!= '\0' && (!is_path
|| *name
!= ':'))
242 /* No DST we recognize. */
245 else if (is_path
&& *name
== ':')
253 while (*name
!= '\0');
261 /* Return copy of argument with all recognized dynamic string tokens
262 ($ORIGIN and $PLATFORM for now) replaced. On some platforms it
263 might not be possible to determine the path from which the object
264 belonging to the map is loaded. In this case the path element
265 containing $ORIGIN is left out. */
267 expand_dynamic_string_token (struct link_map
*l
, const char *s
)
269 /* We make two runs over the string. First we determine how large the
270 resulting string is and then we copy it over. Since this is now
271 frequently executed operation we are looking here not for performance
272 but rather for code size. */
277 /* Determine the number of DST elements. */
278 cnt
= DL_DST_COUNT (s
, 1);
280 /* If we do not have to replace anything simply copy the string. */
282 return local_strdup (s
);
284 /* Determine the length of the substituted string. */
285 total
= DL_DST_REQUIRED (l
, s
, strlen (s
), cnt
);
287 /* Allocate the necessary memory. */
288 result
= (char *) malloc (total
+ 1);
292 return DL_DST_SUBSTITUTE (l
, s
, result
, 1);
296 /* Add `name' to the list of names for a particular shared object.
297 `name' is expected to have been allocated with malloc and will
298 be freed if the shared object already has this name.
299 Returns false if the object already had this name. */
302 add_name_to_object (struct link_map
*l
, const char *name
)
304 struct libname_list
*lnp
, *lastp
;
305 struct libname_list
*newname
;
309 for (lnp
= l
->l_libname
; lnp
!= NULL
; lastp
= lnp
, lnp
= lnp
->next
)
310 if (strcmp (name
, lnp
->name
) == 0)
313 name_len
= strlen (name
) + 1;
314 newname
= (struct libname_list
*) malloc (sizeof *newname
+ name_len
);
317 /* No more memory. */
318 _dl_signal_error (ENOMEM
, name
, "cannot allocate name record");
321 /* The object should have a libname set from _dl_new_object. */
322 assert (lastp
!= NULL
);
324 newname
->name
= memcpy (newname
+ 1, name
, name_len
);
325 newname
->next
= NULL
;
326 lastp
->next
= newname
;
329 /* All known directories in sorted order. */
330 static struct r_search_path_elem
*all_dirs
;
332 /* Standard search directories. */
333 static struct r_search_path_elem
**rtld_search_dirs
;
335 static size_t max_dirnamelen
;
337 static inline struct r_search_path_elem
**
338 fillin_rpath (char *rpath
, struct r_search_path_elem
**result
, const char *sep
,
339 int check_trusted
, const char *what
, const char *where
)
344 while ((cp
= __strsep (&rpath
, sep
)) != NULL
)
346 struct r_search_path_elem
*dirp
;
347 size_t len
= strlen (cp
);
349 /* `strsep' can pass an empty string. This has to be
350 interpreted as `use the current directory'. */
353 static const char curwd
[] = "./";
357 /* Remove trailing slashes (except for "/"). */
358 while (len
> 1 && cp
[len
- 1] == '/')
361 /* Now add one if there is none so far. */
362 if (len
> 0 && cp
[len
- 1] != '/')
365 /* Make sure we don't use untrusted directories if we run SUID. */
368 const char *trun
= system_dirs
;
371 /* All trusted directories must be complete names. */
376 idx
< sizeof (system_dirs_len
) / sizeof (system_dirs_len
[0]);
379 if (len
== system_dirs_len
[idx
] && memcmp (trun
, cp
, len
) == 0)
383 trun
+= system_dirs_len
[idx
] + 1;
386 if (idx
== sizeof (system_dirs_len
) / sizeof (system_dirs_len
[0]))
387 /* It's no trusted directory, skip it. */
391 /* See if this directory is already known. */
392 for (dirp
= all_dirs
; dirp
!= NULL
; dirp
= dirp
->next
)
393 if (dirp
->dirnamelen
== len
&& memcmp (cp
, dirp
->dirname
, len
) == 0)
398 /* It is available, see whether it's on our own list. */
400 for (cnt
= 0; cnt
< nelems
; ++cnt
)
401 if (result
[cnt
] == dirp
)
405 result
[nelems
++] = dirp
;
410 enum r_dir_status init_val
;
412 /* It's a new directory. Create an entry and add it. */
413 dirp
= (struct r_search_path_elem
*)
414 malloc (sizeof (*dirp
) + ncapstr
* sizeof (enum r_dir_status
));
416 _dl_signal_error (ENOMEM
, NULL
,
417 "cannot create cache for search path");
420 dirp
->dirnamelen
= len
;
422 if (len
> max_dirnamelen
)
423 max_dirnamelen
= len
;
425 /* We have to make sure all the relative directories are never
426 ignored. The current directory might change and all our
427 saved information would be void. */
428 init_val
= cp
[0] != '/' ? existing
: unknown
;
429 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
430 dirp
->status
[cnt
] = init_val
;
435 dirp
->next
= all_dirs
;
438 /* Put it in the result array. */
439 result
[nelems
++] = dirp
;
443 /* Terminate the array. */
444 result
[nelems
] = NULL
;
450 static struct r_search_path_elem
**
452 decompose_rpath (const char *rpath
, struct link_map
*l
, const char *what
)
454 /* Make a copy we can work with. */
455 const char *where
= l
->l_name
;
458 struct r_search_path_elem
**result
;
461 /* First see whether we must forget the RUNPATH and RPATH from this
463 if (_dl_inhibit_rpath
!= NULL
&& !__libc_enable_secure
)
465 const char *found
= strstr (_dl_inhibit_rpath
, where
);
468 size_t len
= strlen (where
);
469 if ((found
== _dl_inhibit_rpath
|| found
[-1] == ':')
470 && (found
[len
] == '\0' || found
[len
] == ':'))
472 /* This object is on the list of objects for which the
473 RUNPATH and RPATH must not be used. */
474 result
= (struct r_search_path_elem
**)
475 malloc (sizeof (*result
));
477 _dl_signal_error (ENOMEM
, NULL
,
478 "cannot create cache for search path");
486 /* Make a writable copy. At the same time expand possible dynamic
488 copy
= expand_dynamic_string_token (l
, rpath
);
490 _dl_signal_error (ENOMEM
, NULL
, "cannot create RUNPATH/RPATH copy");
492 /* Count the number of necessary elements in the result array. */
494 for (cp
= copy
; *cp
!= '\0'; ++cp
)
498 /* Allocate room for the result. NELEMS + 1 is an upper limit for the
499 number of necessary entries. */
500 result
= (struct r_search_path_elem
**) malloc ((nelems
+ 1 + 1)
503 _dl_signal_error (ENOMEM
, NULL
, "cannot create cache for search path");
505 return fillin_rpath (copy
, result
, ":", 0, what
, where
);
511 _dl_init_paths (const char *llp
)
515 struct r_search_path_elem
*pelem
, **aelem
;
521 /* Fill in the information about the application's RPATH and the
522 directories addressed by the LD_LIBRARY_PATH environment variable. */
524 /* Get the capabilities. */
525 capstr
= _dl_important_hwcaps (_dl_platform
, _dl_platformlen
,
526 &ncapstr
, &max_capstrlen
);
528 /* First set up the rest of the default search directory entries. */
529 aelem
= rtld_search_dirs
= (struct r_search_path_elem
**)
530 malloc ((sizeof (system_dirs_len
) / sizeof (system_dirs_len
[0]) + 1)
531 * sizeof (struct r_search_path_elem
*));
532 if (rtld_search_dirs
== NULL
)
533 _dl_signal_error (ENOMEM
, NULL
, "cannot create search path array");
535 round_size
= ((2 * sizeof (struct r_search_path_elem
) - 1
536 + ncapstr
* sizeof (enum r_dir_status
))
537 / sizeof (struct r_search_path_elem
));
539 rtld_search_dirs
[0] = (struct r_search_path_elem
*)
540 malloc ((sizeof (system_dirs
) / sizeof (system_dirs
[0]))
541 * round_size
* sizeof (struct r_search_path_elem
));
542 if (rtld_search_dirs
[0] == NULL
)
543 _dl_signal_error (ENOMEM
, NULL
, "cannot create cache for search path");
545 pelem
= all_dirs
= rtld_search_dirs
[0];
555 pelem
->what
= "system search path";
558 pelem
->dirname
= strp
;
559 pelem
->dirnamelen
= system_dirs_len
[idx
];
560 strp
+= system_dirs_len
[idx
] + 1;
562 if (pelem
->dirname
[0] != '/')
563 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
564 pelem
->status
[cnt
] = existing
;
566 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
567 pelem
->status
[cnt
] = unknown
;
569 pelem
->next
= (++idx
== (sizeof (system_dirs_len
)
570 / sizeof (system_dirs_len
[0]))
571 ? NULL
: (pelem
+ round_size
));
575 while (idx
< sizeof (system_dirs_len
) / sizeof (system_dirs_len
[0]));
577 max_dirnamelen
= SYSTEM_DIRS_MAX_LEN
;
581 /* This points to the map of the main object. */
585 assert (l
->l_type
!= lt_loaded
);
587 if (l
->l_info
[DT_RUNPATH
])
589 /* Allocate room for the search path and fill in information
592 decompose_rpath ((const void *) (D_PTR (l
, l_info
[DT_STRTAB
])
593 + l
->l_info
[DT_RUNPATH
]->d_un
.d_val
),
596 /* The RPATH is ignored. */
597 l
->l_rpath_dirs
= (void *) -1;
601 l
->l_runpath_dirs
= (void *) -1;
603 if (l
->l_info
[DT_RPATH
])
604 /* Allocate room for the search path and fill in information
607 decompose_rpath ((const void *) (D_PTR (l
, l_info
[DT_STRTAB
])
608 + l
->l_info
[DT_RPATH
]->d_un
.d_val
),
611 l
->l_rpath_dirs
= (void *) -1;
616 if (llp
!= NULL
&& *llp
!= '\0')
619 const char *cp
= llp
;
621 /* Decompose the LD_LIBRARY_PATH contents. First determine how many
626 if (*cp
== ':' || *cp
== ';')
631 env_path_list
= (struct r_search_path_elem
**)
632 malloc ((nllp
+ 1) * sizeof (struct r_search_path_elem
*));
633 if (env_path_list
== NULL
)
634 _dl_signal_error (ENOMEM
, NULL
,
635 "cannot create cache for search path");
637 (void) fillin_rpath (local_strdup (llp
), env_path_list
, ":;",
638 __libc_enable_secure
, "LD_LIBRARY_PATH", NULL
);
640 if (env_path_list
[0] == NULL
)
642 free (env_path_list
);
643 env_path_list
= (void *) -1;
647 env_path_list
= (void *) -1;
651 /* Think twice before changing anything in this function. It is placed
652 here and prepared using the `alloca' magic to prevent it from being
653 inlined. The function is only called in case of an error. But then
654 performance does not count. The function used to be "inlinable" and
655 the compiled did so all the time. This increased the code size for
656 absolutely no good reason. */
657 #define LOSE(code, s) lose (code, fd, name, realname, l, s)
659 __attribute__ ((noreturn
))
660 lose (int code
, int fd
, const char *name
, char *realname
, struct link_map
*l
,
663 /* The use of `alloca' here looks ridiculous but it helps. The goal
664 is to avoid the function from being inlined. There is no official
665 way to do this so we use this trick. gcc never inlines functions
666 which use `alloca'. */
667 int *a
= alloca (sizeof (int));
669 (void) __close (a
[0]);
672 /* Remove the stillborn object from the list and free it. */
674 l
->l_prev
->l_next
= l
->l_next
;
676 l
->l_next
->l_prev
= l
->l_prev
;
680 _dl_signal_error (code
, name
, msg
);
684 /* Map in the shared object NAME, actually located in REALNAME, and already
687 #ifndef EXTERNAL_MAP_FROM_FD
691 _dl_map_object_from_fd (const char *name
, int fd
, char *realname
,
692 struct link_map
*loader
, int l_type
)
694 /* This is the expected ELF header. */
695 #define ELF32_CLASS ELFCLASS32
696 #define ELF64_CLASS ELFCLASS64
697 #ifndef VALID_ELF_HEADER
698 # define VALID_ELF_HEADER(hdr,exp,size) (memcmp (hdr, exp, size) == 0)
699 # define VALID_ELF_OSABI(osabi) (osabi == ELFOSABI_SYSV)
700 # define VALID_ELF_ABIVERSION(ver) (ver == 0)
702 static const unsigned char expected
[EI_PAD
] =
708 [EI_CLASS
] = ELFW(CLASS
),
709 [EI_DATA
] = byteorder
,
710 [EI_VERSION
] = EV_CURRENT
,
711 [EI_OSABI
] = ELFOSABI_SYSV
,
714 struct link_map
*l
= NULL
;
716 inline caddr_t
map_segment (ElfW(Addr
) mapstart
, size_t len
,
717 int prot
, int fixed
, off_t offset
)
719 caddr_t mapat
= __mmap ((caddr_t
) mapstart
, len
, prot
,
720 fixed
|MAP_COPY
|MAP_FILE
,
722 if (mapat
== MAP_FAILED
)
723 LOSE (errno
, "failed to map segment from shared object");
727 const ElfW(Ehdr
) *header
;
728 const ElfW(Phdr
) *phdr
;
729 const ElfW(Phdr
) *ph
;
736 /* Get file information. */
737 if (__fxstat (_STAT_VER
, fd
, &st
) < 0)
738 LOSE (errno
, "cannot stat shared object");
740 /* Look again to see if the real name matched another already loaded. */
741 for (l
= _dl_loaded
; l
; l
= l
->l_next
)
742 if (l
->l_ino
== st
.st_ino
&& l
->l_dev
== st
.st_dev
)
744 /* The object is already loaded.
745 Just bump its reference count and return it. */
748 /* If the name is not in the list of names for this object add
751 add_name_to_object (l
, name
);
756 /* Print debugging message. */
757 if (__builtin_expect (_dl_debug_files
, 0))
758 _dl_debug_message (1, "file=", name
, "; generating link map\n", NULL
);
760 /* Read the header directly. */
761 readbuf
= alloca (_dl_pagesize
);
762 readlength
= __libc_read (fd
, readbuf
, _dl_pagesize
);
763 if (readlength
< (ssize_t
) sizeof (*header
))
764 LOSE (errno
, "cannot read file data");
765 header
= (void *) readbuf
;
767 /* Check the header for basic validity. */
768 if (__builtin_expect (!VALID_ELF_HEADER (header
->e_ident
, expected
, EI_PAD
),
771 /* Something is wrong. */
772 if (*(Elf32_Word
*) &header
->e_ident
!=
773 #if BYTE_ORDER == LITTLE_ENDIAN
774 ((ELFMAG0
<< (EI_MAG0
* 8)) |
775 (ELFMAG1
<< (EI_MAG1
* 8)) |
776 (ELFMAG2
<< (EI_MAG2
* 8)) |
777 (ELFMAG3
<< (EI_MAG3
* 8)))
779 ((ELFMAG0
<< (EI_MAG3
* 8)) |
780 (ELFMAG1
<< (EI_MAG2
* 8)) |
781 (ELFMAG2
<< (EI_MAG1
* 8)) |
782 (ELFMAG3
<< (EI_MAG0
* 8)))
785 LOSE (0, "invalid ELF header");
786 if (header
->e_ident
[EI_CLASS
] != ELFW(CLASS
))
787 LOSE (0, "ELF file class not " STRING(__ELF_NATIVE_CLASS
) "-bit");
788 if (header
->e_ident
[EI_DATA
] != byteorder
)
789 LOSE (0, "ELF file data encoding not " byteorder_name
);
790 if (header
->e_ident
[EI_VERSION
] != EV_CURRENT
)
791 LOSE (0, "ELF file version ident not " STRING(EV_CURRENT
));
792 /* XXX We should be able so set system specific versions which are
794 if (!VALID_ELF_OSABI (header
->e_ident
[EI_OSABI
]))
795 LOSE (0, "ELF file OS ABI invalid.");
796 if (!VALID_ELF_ABIVERSION (header
->e_ident
[EI_ABIVERSION
]))
797 LOSE (0, "ELF file ABI version invalid.");
798 LOSE (0, "internal error");
801 if (__builtin_expect (header
->e_version
, EV_CURRENT
) != EV_CURRENT
)
802 LOSE (0, "ELF file version not " STRING(EV_CURRENT
));
803 if (! __builtin_expect (elf_machine_matches_host (header
->e_machine
), 1))
804 LOSE (0, "ELF file machine architecture not " ELF_MACHINE_NAME
);
805 if (__builtin_expect (header
->e_phentsize
, sizeof (ElfW(Phdr
)))
806 != sizeof (ElfW(Phdr
)))
807 LOSE (0, "ELF file's phentsize not the expected size");
811 if (_dl_zerofd
== -1)
813 _dl_zerofd
= _dl_sysdep_open_zero_fill ();
814 if (_dl_zerofd
== -1)
817 _dl_signal_error (errno
, NULL
, "cannot open zero fill device");
822 /* Enter the new object in the list of loaded objects. */
823 l
= _dl_new_object (realname
, name
, l_type
, loader
);
824 if (__builtin_expect (! l
, 0))
825 LOSE (ENOMEM
, "cannot create shared object descriptor");
828 /* Extract the remaining details we need from the ELF header
829 and then read in the program header table. */
830 l
->l_entry
= header
->e_entry
;
831 type
= header
->e_type
;
832 l
->l_phnum
= header
->e_phnum
;
834 maplength
= header
->e_phnum
* sizeof (ElfW(Phdr
));
835 if (header
->e_phoff
+ maplength
<= readlength
)
836 phdr
= (void *) (readbuf
+ header
->e_phoff
);
839 phdr
= alloca (maplength
);
840 __lseek (fd
, SEEK_SET
, header
->e_phoff
);
841 if (__libc_read (fd
, (void *) phdr
, maplength
) != maplength
)
842 LOSE (errno
, "cannot read file data");
846 /* Scan the program header table, collecting its load commands. */
849 ElfW(Addr
) mapstart
, mapend
, dataend
, allocend
;
852 } loadcmds
[l
->l_phnum
], *c
;
853 size_t nloadcmds
= 0;
855 /* The struct is initialized to zero so this is not necessary:
859 for (ph
= phdr
; ph
< &phdr
[l
->l_phnum
]; ++ph
)
862 /* These entries tell us where to find things once the file's
863 segments are mapped in. We record the addresses it says
864 verbatim, and later correct for the run-time load address. */
866 l
->l_ld
= (void *) ph
->p_vaddr
;
867 l
->l_ldnum
= ph
->p_memsz
/ sizeof (ElfW(Dyn
));
870 l
->l_phdr
= (void *) ph
->p_vaddr
;
874 /* A load command tells us to map in part of the file.
875 We record the load commands and process them all later. */
876 if (ph
->p_align
% _dl_pagesize
!= 0)
877 LOSE (0, "ELF load command alignment not page-aligned");
878 if ((ph
->p_vaddr
- ph
->p_offset
) % ph
->p_align
)
879 LOSE (0, "ELF load command address/offset not properly aligned");
881 struct loadcmd
*c
= &loadcmds
[nloadcmds
++];
882 c
->mapstart
= ph
->p_vaddr
& ~(ph
->p_align
- 1);
883 c
->mapend
= ((ph
->p_vaddr
+ ph
->p_filesz
+ _dl_pagesize
- 1)
884 & ~(_dl_pagesize
- 1));
885 c
->dataend
= ph
->p_vaddr
+ ph
->p_filesz
;
886 c
->allocend
= ph
->p_vaddr
+ ph
->p_memsz
;
887 c
->mapoff
= ph
->p_offset
& ~(ph
->p_align
- 1);
889 /* Optimize a common case. */
890 if ((PF_R
| PF_W
| PF_X
) == 7
891 && (PROT_READ
| PROT_WRITE
| PROT_EXEC
) == 7)
892 c
->prot
= _dl_pf_to_prot
[ph
->p_flags
& (PF_R
| PF_W
| PF_X
)];
896 if (ph
->p_flags
& PF_R
)
897 c
->prot
|= PROT_READ
;
898 if (ph
->p_flags
& PF_W
)
899 c
->prot
|= PROT_WRITE
;
900 if (ph
->p_flags
& PF_X
)
901 c
->prot
|= PROT_EXEC
;
907 /* Now process the load commands and map segments into memory. */
910 /* Length of the sections to be loaded. */
911 maplength
= loadcmds
[nloadcmds
- 1].allocend
- c
->mapstart
;
913 if (type
== ET_DYN
|| type
== ET_REL
)
915 /* This is a position-independent shared object. We can let the
916 kernel map it anywhere it likes, but we must have space for all
917 the segments in their specified positions relative to the first.
918 So we map the first segment without MAP_FIXED, but with its
919 extent increased to cover all the segments. Then we remove
920 access from excess portion, and there is known sufficient space
921 there to remap from the later segments.
923 As a refinement, sometimes we have an address that we would
924 prefer to map such objects at; but this is only a preference,
925 the OS can do whatever it likes. */
927 mappref
= (ELF_PREFERRED_ADDRESS (loader
, maplength
, c
->mapstart
)
928 - MAP_BASE_ADDR (l
));
930 /* Remember which part of the address space this object uses. */
931 l
->l_map_start
= (ElfW(Addr
)) map_segment (mappref
, maplength
, c
->prot
,
933 l
->l_map_end
= l
->l_map_start
+ maplength
;
934 l
->l_addr
= l
->l_map_start
- c
->mapstart
;
936 /* Change protection on the excess portion to disallow all access;
937 the portions we do not remap later will be inaccessible as if
938 unallocated. Then jump into the normal segment-mapping loop to
939 handle the portion of the segment past the end of the file
941 __mprotect ((caddr_t
) (l
->l_addr
+ c
->mapend
),
942 loadcmds
[nloadcmds
- 1].allocend
- c
->mapend
,
949 /* Notify ELF_PREFERRED_ADDRESS that we have to load this one
951 ELF_FIXED_ADDRESS (loader
, c
->mapstart
);
954 /* Remember which part of the address space this object uses. */
955 l
->l_map_start
= c
->mapstart
+ l
->l_addr
;
956 l
->l_map_end
= l
->l_map_start
+ maplength
;
958 while (c
< &loadcmds
[nloadcmds
])
960 if (c
->mapend
> c
->mapstart
)
961 /* Map the segment contents from the file. */
962 map_segment (l
->l_addr
+ c
->mapstart
, c
->mapend
- c
->mapstart
,
963 c
->prot
, MAP_FIXED
, c
->mapoff
);
967 && c
->mapoff
<= header
->e_phoff
968 && (c
->mapend
- c
->mapstart
+ c
->mapoff
969 >= header
->e_phoff
+ header
->e_phnum
* sizeof (ElfW(Phdr
))))
970 /* Found the program header in this segment. */
971 l
->l_phdr
= (void *) (c
->mapstart
+ header
->e_phoff
- c
->mapoff
);
973 if (c
->allocend
> c
->dataend
)
975 /* Extra zero pages should appear at the end of this segment,
976 after the data mapped from the file. */
977 ElfW(Addr
) zero
, zeroend
, zeropage
;
979 zero
= l
->l_addr
+ c
->dataend
;
980 zeroend
= l
->l_addr
+ c
->allocend
;
981 zeropage
= (zero
+ _dl_pagesize
- 1) & ~(_dl_pagesize
- 1);
983 if (zeroend
< zeropage
)
984 /* All the extra data is in the last page of the segment.
985 We can just zero it. */
990 /* Zero the final part of the last page of the segment. */
991 if ((c
->prot
& PROT_WRITE
) == 0)
994 if (__mprotect ((caddr_t
) (zero
& ~(_dl_pagesize
- 1)),
995 _dl_pagesize
, c
->prot
|PROT_WRITE
) < 0)
996 LOSE (errno
, "cannot change memory protections");
998 memset ((void *) zero
, 0, zeropage
- zero
);
999 if ((c
->prot
& PROT_WRITE
) == 0)
1000 __mprotect ((caddr_t
) (zero
& ~(_dl_pagesize
- 1)),
1001 _dl_pagesize
, c
->prot
);
1004 if (zeroend
> zeropage
)
1006 /* Map the remaining zero pages in from the zero fill FD. */
1008 mapat
= __mmap ((caddr_t
) zeropage
, zeroend
- zeropage
,
1009 c
->prot
, MAP_ANON
|MAP_PRIVATE
|MAP_FIXED
,
1011 if (mapat
== MAP_FAILED
)
1012 LOSE (errno
, "cannot map zero-fill pages");
1019 if (l
->l_phdr
== NULL
)
1021 /* The program header is not contained in any of the segmenst.
1022 We have to allocate memory ourself and copy it over from
1023 out temporary place. */
1024 ElfW(Phdr
) *newp
= (ElfW(Phdr
) *) malloc (header
->e_phnum
1025 * sizeof (ElfW(Phdr
)));
1027 LOSE (ENOMEM
, "cannot allocate memory for program header");
1029 l
->l_phdr
= memcpy (newp
, phdr
,
1030 (header
->e_phnum
* sizeof (ElfW(Phdr
))));
1031 l
->l_phdr_allocated
= 1;
1034 /* Adjust the PT_PHDR value by the runtime load address. */
1035 (ElfW(Addr
)) l
->l_phdr
+= l
->l_addr
;
1038 /* We are done mapping in the file. We no longer need the descriptor. */
1041 if (l
->l_type
== lt_library
&& type
== ET_EXEC
)
1042 l
->l_type
= lt_executable
;
1047 LOSE (0, "object file has no dynamic section");
1050 (ElfW(Addr
)) l
->l_ld
+= l
->l_addr
;
1052 l
->l_entry
+= l
->l_addr
;
1054 if (__builtin_expect (_dl_debug_files
, 0))
1056 const size_t nibbles
= sizeof (void *) * 2;
1057 char buf1
[nibbles
+ 1];
1058 char buf2
[nibbles
+ 1];
1059 char buf3
[nibbles
+ 1];
1061 buf1
[nibbles
] = '\0';
1062 buf2
[nibbles
] = '\0';
1063 buf3
[nibbles
] = '\0';
1065 memset (buf1
, '0', nibbles
);
1066 memset (buf2
, '0', nibbles
);
1067 memset (buf3
, '0', nibbles
);
1068 _itoa_word ((unsigned long int) l
->l_ld
, &buf1
[nibbles
], 16, 0);
1069 _itoa_word ((unsigned long int) l
->l_addr
, &buf2
[nibbles
], 16, 0);
1070 _itoa_word (maplength
, &buf3
[nibbles
], 16, 0);
1072 _dl_debug_message (1, " dynamic: 0x", buf1
, " base: 0x", buf2
,
1073 " size: 0x", buf3
, "\n", NULL
);
1074 memset (buf1
, '0', nibbles
);
1075 memset (buf2
, '0', nibbles
);
1076 memset (buf3
, ' ', nibbles
);
1077 _itoa_word ((unsigned long int) l
->l_entry
, &buf1
[nibbles
], 16, 0);
1078 _itoa_word ((unsigned long int) l
->l_phdr
, &buf2
[nibbles
], 16, 0);
1079 _itoa_word (l
->l_phnum
, &buf3
[nibbles
], 10, 0);
1080 _dl_debug_message (1, " entry: 0x", buf1
, " phdr: 0x", buf2
,
1081 " phnum: ", buf3
, "\n\n", NULL
);
1084 elf_get_dynamic_info (l
);
1085 if (l
->l_info
[DT_HASH
])
1088 /* If this object has DT_SYMBOLIC set modify now its scope. We don't
1089 have to do this for the main map. */
1090 if (l
->l_info
[DT_SYMBOLIC
] && &l
->l_searchlist
!= l
->l_scope
[0])
1092 /* Create an appropriate searchlist. It contains only this map.
1094 XXX This is the definition of DT_SYMBOLIC in SysVr4. The old
1095 GNU ld.so implementation had a different interpretation which
1096 is more reasonable. We are prepared to add this possibility
1097 back as part of a GNU extension of the ELF format. */
1098 l
->l_symbolic_searchlist
.r_list
=
1099 (struct link_map
**) malloc (sizeof (struct link_map
*));
1101 if (l
->l_symbolic_searchlist
.r_list
== NULL
)
1102 LOSE (ENOMEM
, "cannot create searchlist");
1104 l
->l_symbolic_searchlist
.r_list
[0] = l
;
1105 l
->l_symbolic_searchlist
.r_nlist
= 1;
1106 l
->l_symbolic_searchlist
.r_duplist
= l
->l_symbolic_searchlist
.r_list
;
1107 l
->l_symbolic_searchlist
.r_nduplist
= 1;
1109 /* Now move the existing entries one back. */
1110 memmove (&l
->l_scope
[1], &l
->l_scope
[0],
1111 sizeof (l
->l_scope
) - sizeof (l
->l_scope
[0]));
1113 /* Now add the new entry. */
1114 l
->l_scope
[0] = &l
->l_symbolic_searchlist
;
1117 /* Finally the file information. */
1118 l
->l_dev
= st
.st_dev
;
1119 l
->l_ino
= st
.st_ino
;
1124 /* Print search path. */
1126 print_search_path (struct r_search_path_elem
**list
,
1127 const char *what
, const char *name
)
1129 char buf
[max_dirnamelen
+ max_capstrlen
];
1132 _dl_debug_message (1, " search path=", NULL
);
1134 while (*list
!= NULL
&& (*list
)->what
== what
) /* Yes, ==. */
1136 char *endp
= __mempcpy (buf
, (*list
)->dirname
, (*list
)->dirnamelen
);
1139 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
1140 if ((*list
)->status
[cnt
] != nonexisting
)
1142 char *cp
= __mempcpy (endp
, capstr
[cnt
].str
, capstr
[cnt
].len
);
1143 if (cp
== buf
|| (cp
== buf
+ 1 && buf
[0] == '/'))
1147 _dl_debug_message (0, first
? "" : ":", buf
, NULL
);
1155 _dl_debug_message (0, "\t\t(", what
, " from file ",
1156 name
[0] ? name
: _dl_argv
[0], ")\n", NULL
);
1158 _dl_debug_message (0, "\t\t(", what
, ")\n", NULL
);
1161 /* Try to open NAME in one of the directories in DIRS.
1162 Return the fd, or -1. If successful, fill in *REALNAME
1163 with the malloc'd full directory name. */
1166 open_path (const char *name
, size_t namelen
, int preloaded
,
1167 struct r_search_path_elem
***dirsp
,
1170 struct r_search_path_elem
**dirs
= *dirsp
;
1173 const char *current_what
= NULL
;
1176 buf
= alloca (max_dirnamelen
+ max_capstrlen
+ namelen
);
1179 struct r_search_path_elem
*this_dir
= *dirs
;
1184 /* If we are debugging the search for libraries print the path
1185 now if it hasn't happened now. */
1186 if (__builtin_expect (_dl_debug_libs
, 0)
1187 && current_what
!= this_dir
->what
)
1189 current_what
= this_dir
->what
;
1190 print_search_path (dirs
, current_what
, this_dir
->where
);
1193 edp
= (char *) __mempcpy (buf
, this_dir
->dirname
, this_dir
->dirnamelen
);
1194 for (cnt
= 0; fd
== -1 && cnt
< ncapstr
; ++cnt
)
1196 /* Skip this directory if we know it does not exist. */
1197 if (this_dir
->status
[cnt
] == nonexisting
)
1201 ((char *) __mempcpy (__mempcpy (edp
,
1202 capstr
[cnt
].str
, capstr
[cnt
].len
),
1206 /* Print name we try if this is wanted. */
1207 if (__builtin_expect (_dl_debug_libs
, 0))
1208 _dl_debug_message (1, " trying file=", buf
, "\n", NULL
);
1210 fd
= __open (buf
, O_RDONLY
);
1211 if (this_dir
->status
[cnt
] == unknown
)
1214 this_dir
->status
[cnt
] = existing
;
1217 /* We failed to open machine dependent library. Let's
1218 test whether there is any directory at all. */
1221 buf
[buflen
- namelen
- 1] = '\0';
1223 if (__xstat (_STAT_VER
, buf
, &st
) != 0
1224 || ! S_ISDIR (st
.st_mode
))
1225 /* The directory does not exist or it is no directory. */
1226 this_dir
->status
[cnt
] = nonexisting
;
1228 this_dir
->status
[cnt
] = existing
;
1232 /* Remember whether we found any existing directory. */
1233 any
|= this_dir
->status
[cnt
] == existing
;
1235 if (fd
!= -1 && preloaded
&& __libc_enable_secure
)
1237 /* This is an extra security effort to make sure nobody can
1238 preload broken shared objects which are in the trusted
1239 directories and so exploit the bugs. */
1242 if (__fxstat (_STAT_VER
, fd
, &st
) != 0
1243 || (st
.st_mode
& S_ISUID
) == 0)
1245 /* The shared object cannot be tested for being SUID
1246 or this bit is not set. In this case we must not
1250 /* We simply ignore the file, signal this by setting
1251 the error value which would have been set by `open'. */
1259 *realname
= malloc (buflen
);
1260 if (*realname
!= NULL
)
1262 memcpy (*realname
, buf
, buflen
);
1267 /* No memory for the name, we certainly won't be able
1268 to load and link it. */
1273 if (errno
!= ENOENT
&& errno
!= EACCES
)
1274 /* The file exists and is readable, but something went wrong. */
1277 while (*++dirs
!= NULL
);
1279 /* Remove the whole path if none of the directories exists. */
1283 *dirsp
= (void *) -1;
1289 /* Map in the shared object file NAME. */
1293 _dl_map_object (struct link_map
*loader
, const char *name
, int preloaded
,
1294 int type
, int trace_mode
)
1301 /* Look for this name among those already loaded. */
1302 for (l
= _dl_loaded
; l
; l
= l
->l_next
)
1304 /* If the requested name matches the soname of a loaded object,
1305 use that object. Elide this check for names that have not
1307 if (l
->l_opencount
<= 0)
1309 if (!_dl_name_match_p (name
, l
))
1313 if (l
->l_info
[DT_SONAME
] == NULL
)
1316 soname
= (const void *) (D_PTR (l
, l_info
[DT_STRTAB
])
1317 + l
->l_info
[DT_SONAME
]->d_un
.d_val
);
1318 if (strcmp (name
, soname
) != 0)
1321 /* We have a match on a new name -- cache it. */
1322 add_name_to_object (l
, soname
);
1325 /* We have a match -- bump the reference count and return it. */
1330 /* Display information if we are debugging. */
1331 if (__builtin_expect (_dl_debug_files
, 0) && loader
!= NULL
)
1332 _dl_debug_message (1, "\nfile=", name
, "; needed by ",
1333 loader
->l_name
[0] ? loader
->l_name
: _dl_argv
[0],
1336 if (strchr (name
, '/') == NULL
)
1338 /* Search for NAME in several places. */
1340 size_t namelen
= strlen (name
) + 1;
1342 if (__builtin_expect (_dl_debug_libs
, 0))
1343 _dl_debug_message (1, "find library=", name
, "; searching\n", NULL
);
1347 /* When the object has the RUNPATH information we don't use any
1349 if (loader
== NULL
|| loader
->l_info
[DT_RUNPATH
] == NULL
)
1351 /* First try the DT_RPATH of the dependent object that caused NAME
1352 to be loaded. Then that object's dependent, and on up. */
1353 for (l
= loader
; fd
== -1 && l
; l
= l
->l_loader
)
1355 if (l
->l_rpath_dirs
== NULL
)
1357 if (l
->l_info
[DT_RPATH
] == NULL
)
1358 /* There is no path. */
1359 l
->l_rpath_dirs
= (void *) -1;
1362 /* Make sure the cache information is available. */
1363 size_t ptrval
= (D_PTR (l
, l_info
[DT_STRTAB
])
1364 + l
->l_info
[DT_RPATH
]->d_un
.d_val
);
1366 decompose_rpath ((const char *) ptrval
, l
,
1369 if (l
->l_rpath_dirs
!= (void *) -1)
1370 fd
= open_path (name
, namelen
, preloaded
,
1371 &l
->l_rpath_dirs
, &realname
);
1374 else if (l
->l_rpath_dirs
!= (void *) -1)
1375 fd
= open_path (name
, namelen
, preloaded
, &l
->l_rpath_dirs
,
1379 /* If dynamically linked, try the DT_RPATH of the executable
1382 if (fd
== -1 && l
&& l
->l_type
!= lt_loaded
&& l
!= loader
1383 && l
->l_rpath_dirs
!= (void *) -1)
1384 fd
= open_path (name
, namelen
, preloaded
, &l
->l_rpath_dirs
,
1388 /* Try the LD_LIBRARY_PATH environment variable. */
1389 if (fd
== -1 && env_path_list
!= (void *) -1)
1390 fd
= open_path (name
, namelen
, preloaded
, &env_path_list
, &realname
);
1392 /* Look at the RUNPATH informaiton for this binary. */
1393 if (loader
!= NULL
&& loader
->l_runpath_dirs
!= (void *) -1)
1395 if (loader
->l_runpath_dirs
== NULL
)
1397 if (loader
->l_info
[DT_RUNPATH
] == NULL
)
1399 loader
->l_runpath_dirs
= (void *) -1;
1402 /* Make sure the cache information is available. */
1403 size_t ptrval
= (D_PTR (loader
, l_info
[DT_STRTAB
])
1404 + loader
->l_info
[DT_RUNPATH
]->d_un
.d_val
);
1405 loader
->l_runpath_dirs
=
1406 decompose_rpath ((const char *) ptrval
, loader
, "RUNPATH");
1408 if (loader
->l_runpath_dirs
!= (void *) -1)
1409 fd
= open_path (name
, namelen
, preloaded
,
1410 &loader
->l_runpath_dirs
, &realname
);
1413 else if (loader
->l_runpath_dirs
!= (void *) -1)
1414 fd
= open_path (name
, namelen
, preloaded
,
1415 &loader
->l_runpath_dirs
, &realname
);
1420 /* Check the list of libraries in the file /etc/ld.so.cache,
1421 for compatibility with Linux's ldconfig program. */
1422 extern const char *_dl_load_cache_lookup (const char *name
);
1423 const char *cached
= _dl_load_cache_lookup (name
);
1426 fd
= __open (cached
, O_RDONLY
);
1429 realname
= local_strdup (cached
);
1430 if (realname
== NULL
)
1439 /* Finally, try the default path. */
1441 fd
= open_path (name
, namelen
, preloaded
, &rtld_search_dirs
,
1444 /* Add another newline when we a tracing the library loading. */
1445 if (__builtin_expect (_dl_debug_libs
, 0))
1446 _dl_debug_message (1, "\n", NULL
);
1450 /* The path may contain dynamic string tokens. */
1452 ? expand_dynamic_string_token (loader
, name
)
1453 : local_strdup (name
));
1454 if (realname
== NULL
)
1458 fd
= __open (realname
, O_RDONLY
);
1468 /* We haven't found an appropriate library. But since we
1469 are only interested in the list of libraries this isn't
1470 so severe. Fake an entry with all the information we
1472 static const Elf_Symndx dummy_bucket
= STN_UNDEF
;
1474 /* Enter the new object in the list of loaded objects. */
1475 if ((name_copy
= local_strdup (name
)) == NULL
1476 || (l
= _dl_new_object (name_copy
, name
, type
, loader
)) == NULL
)
1477 _dl_signal_error (ENOMEM
, name
,
1478 "cannot create shared object descriptor");
1479 /* We use an opencount of 0 as a sign for the faked entry.
1480 Since the descriptor is initialized with zero we do not
1483 l->l_reserved = 0; */
1484 l
->l_buckets
= &dummy_bucket
;
1491 _dl_signal_error (errno
, name
, "cannot open shared object file");
1494 return _dl_map_object_from_fd (name
, fd
, realname
, loader
, type
);