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. */
29 #include <sys/param.h>
31 #include <sys/types.h>
32 #include "dynamic-link.h"
33 #include <stdio-common/_itoa.h>
37 /* On some systems, no flag bits are given to specify file mapping. */
42 /* The right way to map in the shared library files is MAP_COPY, which
43 makes a virtual copy of the data at the time of the mmap call; this
44 guarantees the mapped pages will be consistent even if the file is
45 overwritten. Some losing VM systems like Linux's lack MAP_COPY. All we
46 get is MAP_PRIVATE, which copies each page when it is modified; this
47 means if the file is overwritten, we may at some point get some pages
48 from the new version after starting with pages from the old version. */
50 # define MAP_COPY MAP_PRIVATE
53 /* Some systems link their relocatable objects for another base address
54 than 0. We want to know the base address for these such that we can
55 subtract this address from the segment addresses during mapping.
56 This results in a more efficient address space usage. Defaults to
57 zero for almost all systems. */
59 # define MAP_BASE_ADDR(l) 0
64 #if BYTE_ORDER == BIG_ENDIAN
65 # define byteorder ELFDATA2MSB
66 #elif BYTE_ORDER == LITTLE_ENDIAN
67 # define byteorder ELFDATA2LSB
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 the decomposed LD_LIBRARY_PATH search path. */
101 static struct r_search_path_elem
**env_path_list
;
103 /* List of the hardware capabilities we might end up using. */
104 static const struct r_strlenpair
*capstr
;
105 static size_t ncapstr
;
106 static size_t max_capstrlen
;
108 const unsigned char _dl_pf_to_prot
[8] =
113 [PF_R
| PF_W
] = PROT_READ
| PROT_WRITE
,
115 [PF_R
| PF_X
] = PROT_READ
| PROT_EXEC
,
116 [PF_W
| PF_X
] = PROT_WRITE
| PROT_EXEC
,
117 [PF_R
| PF_W
| PF_X
] = PROT_READ
| PROT_WRITE
| PROT_EXEC
121 /* Get the generated information about the trusted directories. */
122 #include "trusted-dirs.h"
124 static const char system_dirs
[] = SYSTEM_DIRS
;
125 static const size_t system_dirs_len
[] =
131 /* Local version of `strdup' function. */
133 local_strdup (const char *s
)
135 size_t len
= strlen (s
) + 1;
136 void *new = malloc (len
);
141 return (char *) memcpy (new, s
, len
);
146 _dl_dst_count (const char *name
, int is_path
)
148 const char *const start
= name
;
155 /* $ORIGIN is not expanded for SUID/GUID programs.
157 Note that it is no bug that the strings in the first two `strncmp'
158 calls are longer than the sequence which is actually tested. */
159 if ((((strncmp (&name
[1], "ORIGIN}", 6) == 0
160 && (!__libc_enable_secure
161 || ((name
[7] == '\0' || (is_path
&& name
[7] == ':'))
162 && (name
== start
|| (is_path
&& name
[-1] == ':'))))
164 || (strncmp (&name
[1], "PLATFORM}", 8) == 0 && (len
= 9) != 0))
165 && (name
[len
] == '\0' || name
[len
] == '/'
166 || (is_path
&& name
[len
] == ':')))
168 && ((strncmp (&name
[2], "ORIGIN}", 7) == 0
169 && (!__libc_enable_secure
170 || ((name
[9] == '\0' || (is_path
&& name
[9] == ':'))
171 && (name
== start
|| (is_path
&& name
[-1] == ':'))))
173 || (strncmp (&name
[2], "PLATFORM}", 9) == 0
174 && (len
= 11) != 0))))
177 name
= strchr (name
+ len
, '$');
179 while (name
!= NULL
);
186 _dl_dst_substitute (struct link_map
*l
, const char *name
, char *result
,
189 const char *const start
= name
;
190 char *last_elem
, *wp
;
192 /* Now fill the result path. While copying over the string we keep
193 track of the start of the last path element. When we come accross
194 a DST we copy over the value or (if the value is not available)
195 leave the entire path element out. */
196 last_elem
= wp
= result
;
205 /* Note that it is no bug that the strings in the first two `strncmp'
206 calls are longer than the sequence which is actually tested. */
207 if ((((strncmp (&name
[1], "ORIGIN}", 6) == 0 && (len
= 7) != 0)
208 || (strncmp (&name
[1], "PLATFORM}", 8) == 0 && (len
= 9) != 0))
209 && (name
[len
] == '\0' || name
[len
] == '/'
210 || (is_path
&& name
[len
] == ':')))
212 && ((strncmp (&name
[2], "ORIGIN}", 7) == 0 && (len
= 9) != 0)
213 || (strncmp (&name
[2], "PLATFORM}", 9) == 0
214 && (len
= 11) != 0))))
216 repl
= ((len
== 7 || name
[2] == 'O')
217 ? (__libc_enable_secure
218 && ((name
[len
] != '\0'
219 && (!is_path
|| name
[len
] != ':'))
221 && (!is_path
|| name
[-1] != ':')))
222 ? NULL
: l
->l_origin
)
225 if (repl
!= NULL
&& repl
!= (const char *) -1)
227 wp
= __stpcpy (wp
, repl
);
232 /* We cannot use this path element, the value of the
233 replacement is unknown. */
236 while (*name
!= '\0' && (!is_path
|| *name
!= ':'))
241 /* No DST we recognize. */
244 else if (is_path
&& *name
== ':')
252 while (*name
!= '\0');
260 /* Return copy of argument with all recognized dynamic string tokens
261 ($ORIGIN and $PLATFORM for now) replaced. On some platforms it
262 might not be possible to determine the path from which the object
263 belonging to the map is loaded. In this case the path element
264 containing $ORIGIN is left out. */
266 expand_dynamic_string_token (struct link_map
*l
, const char *s
)
268 /* We make two runs over the string. First we determine how large the
269 resulting string is and then we copy it over. Since this is now
270 frequently executed operation we are looking here not for performance
271 but rather for code size. */
276 /* Determine the number of DST elements. */
277 cnt
= DL_DST_COUNT (s
, 1);
279 /* If we do not have to replace anything simply copy the string. */
281 return local_strdup (s
);
283 /* Determine the length of the substituted string. */
284 total
= DL_DST_REQUIRED (l
, s
, strlen (s
), cnt
);
286 /* Allocate the necessary memory. */
287 result
= (char *) malloc (total
+ 1);
291 return DL_DST_SUBSTITUTE (l
, s
, result
, 1);
295 /* Add `name' to the list of names for a particular shared object.
296 `name' is expected to have been allocated with malloc and will
297 be freed if the shared object already has this name.
298 Returns false if the object already had this name. */
301 add_name_to_object (struct link_map
*l
, const char *name
)
303 struct libname_list
*lnp
, *lastp
;
304 struct libname_list
*newname
;
308 for (lnp
= l
->l_libname
; lnp
!= NULL
; lastp
= lnp
, lnp
= lnp
->next
)
309 if (strcmp (name
, lnp
->name
) == 0)
312 name_len
= strlen (name
) + 1;
313 newname
= (struct libname_list
*) malloc (sizeof *newname
+ name_len
);
316 /* No more memory. */
317 _dl_signal_error (ENOMEM
, name
, N_("cannot allocate name record"));
320 /* The object should have a libname set from _dl_new_object. */
321 assert (lastp
!= NULL
);
323 newname
->name
= memcpy (newname
+ 1, name
, name_len
);
324 newname
->next
= NULL
;
325 lastp
->next
= newname
;
328 /* All known directories in sorted order. */
329 static struct r_search_path_elem
*all_dirs
;
331 /* Standard search directories. */
332 static struct r_search_path_elem
**rtld_search_dirs
;
334 static size_t max_dirnamelen
;
336 static inline struct r_search_path_elem
**
337 fillin_rpath (char *rpath
, struct r_search_path_elem
**result
, const char *sep
,
338 int check_trusted
, const char *what
, const char *where
)
343 while ((cp
= __strsep (&rpath
, sep
)) != NULL
)
345 struct r_search_path_elem
*dirp
;
346 size_t len
= strlen (cp
);
348 /* `strsep' can pass an empty string. This has to be
349 interpreted as `use the current directory'. */
352 static const char curwd
[] = "./";
356 /* Remove trailing slashes (except for "/"). */
357 while (len
> 1 && cp
[len
- 1] == '/')
360 /* Now add one if there is none so far. */
361 if (len
> 0 && cp
[len
- 1] != '/')
364 /* Make sure we don't use untrusted directories if we run SUID. */
367 const char *trun
= system_dirs
;
370 /* All trusted directories must be complete names. */
375 idx
< sizeof (system_dirs_len
) / sizeof (system_dirs_len
[0]);
378 if (len
== system_dirs_len
[idx
] && memcmp (trun
, cp
, len
) == 0)
382 trun
+= system_dirs_len
[idx
] + 1;
385 if (idx
== sizeof (system_dirs_len
) / sizeof (system_dirs_len
[0]))
386 /* It's no trusted directory, skip it. */
390 /* See if this directory is already known. */
391 for (dirp
= all_dirs
; dirp
!= NULL
; dirp
= dirp
->next
)
392 if (dirp
->dirnamelen
== len
&& memcmp (cp
, dirp
->dirname
, len
) == 0)
397 /* It is available, see whether it's on our own list. */
399 for (cnt
= 0; cnt
< nelems
; ++cnt
)
400 if (result
[cnt
] == dirp
)
404 result
[nelems
++] = dirp
;
409 enum r_dir_status init_val
;
411 /* It's a new directory. Create an entry and add it. */
412 dirp
= (struct r_search_path_elem
*)
413 malloc (sizeof (*dirp
) + ncapstr
* sizeof (enum r_dir_status
));
415 _dl_signal_error (ENOMEM
, NULL
,
416 N_("cannot create cache for search path"));
419 dirp
->dirnamelen
= len
;
421 if (len
> max_dirnamelen
)
422 max_dirnamelen
= len
;
424 /* We have to make sure all the relative directories are never
425 ignored. The current directory might change and all our
426 saved information would be void. */
427 init_val
= cp
[0] != '/' ? existing
: unknown
;
428 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
429 dirp
->status
[cnt
] = init_val
;
434 dirp
->next
= all_dirs
;
437 /* Put it in the result array. */
438 result
[nelems
++] = dirp
;
442 /* Terminate the array. */
443 result
[nelems
] = NULL
;
449 static struct r_search_path_elem
**
451 decompose_rpath (const char *rpath
, struct link_map
*l
, const char *what
)
453 /* Make a copy we can work with. */
454 const char *where
= l
->l_name
;
457 struct r_search_path_elem
**result
;
460 /* First see whether we must forget the RUNPATH and RPATH from this
462 if (_dl_inhibit_rpath
!= NULL
&& !__libc_enable_secure
)
464 const char *found
= strstr (_dl_inhibit_rpath
, where
);
467 size_t len
= strlen (where
);
468 if ((found
== _dl_inhibit_rpath
|| found
[-1] == ':')
469 && (found
[len
] == '\0' || found
[len
] == ':'))
471 /* This object is on the list of objects for which the
472 RUNPATH and RPATH must not be used. */
473 result
= (struct r_search_path_elem
**)
474 malloc (sizeof (*result
));
476 _dl_signal_error (ENOMEM
, NULL
,
477 N_("cannot create cache for search path"));
485 /* Make a writable copy. At the same time expand possible dynamic
487 copy
= expand_dynamic_string_token (l
, rpath
);
489 _dl_signal_error (ENOMEM
, NULL
, N_("cannot create RUNPATH/RPATH copy"));
491 /* Count the number of necessary elements in the result array. */
493 for (cp
= copy
; *cp
!= '\0'; ++cp
)
497 /* Allocate room for the result. NELEMS + 1 is an upper limit for the
498 number of necessary entries. */
499 result
= (struct r_search_path_elem
**) malloc ((nelems
+ 1 + 1)
502 _dl_signal_error (ENOMEM
, NULL
, N_("cannot create cache for search path"));
504 return fillin_rpath (copy
, result
, ":", 0, what
, where
);
510 _dl_init_paths (const char *llp
)
514 struct r_search_path_elem
*pelem
, **aelem
;
520 /* Fill in the information about the application's RPATH and the
521 directories addressed by the LD_LIBRARY_PATH environment variable. */
523 /* Get the capabilities. */
524 capstr
= _dl_important_hwcaps (_dl_platform
, _dl_platformlen
,
525 &ncapstr
, &max_capstrlen
);
527 /* First set up the rest of the default search directory entries. */
528 aelem
= rtld_search_dirs
= (struct r_search_path_elem
**)
529 malloc ((sizeof (system_dirs_len
) / sizeof (system_dirs_len
[0]) + 1)
530 * sizeof (struct r_search_path_elem
*));
531 if (rtld_search_dirs
== NULL
)
532 _dl_signal_error (ENOMEM
, NULL
, N_("cannot create search path array"));
534 round_size
= ((2 * sizeof (struct r_search_path_elem
) - 1
535 + ncapstr
* sizeof (enum r_dir_status
))
536 / sizeof (struct r_search_path_elem
));
538 rtld_search_dirs
[0] = (struct r_search_path_elem
*)
539 malloc ((sizeof (system_dirs
) / sizeof (system_dirs
[0]))
540 * round_size
* sizeof (struct r_search_path_elem
));
541 if (rtld_search_dirs
[0] == NULL
)
542 _dl_signal_error (ENOMEM
, NULL
, N_("cannot create cache for search path"));
544 pelem
= all_dirs
= rtld_search_dirs
[0];
554 pelem
->what
= "system search path";
557 pelem
->dirname
= strp
;
558 pelem
->dirnamelen
= system_dirs_len
[idx
];
559 strp
+= system_dirs_len
[idx
] + 1;
561 if (pelem
->dirname
[0] != '/')
562 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
563 pelem
->status
[cnt
] = existing
;
565 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
566 pelem
->status
[cnt
] = unknown
;
568 pelem
->next
= (++idx
== (sizeof (system_dirs_len
)
569 / sizeof (system_dirs_len
[0]))
570 ? NULL
: (pelem
+ round_size
));
574 while (idx
< sizeof (system_dirs_len
) / sizeof (system_dirs_len
[0]));
576 max_dirnamelen
= SYSTEM_DIRS_MAX_LEN
;
580 /* This points to the map of the main object. */
584 assert (l
->l_type
!= lt_loaded
);
586 if (l
->l_info
[DT_RUNPATH
])
588 /* Allocate room for the search path and fill in information
591 decompose_rpath ((const void *) (D_PTR (l
, l_info
[DT_STRTAB
])
592 + l
->l_info
[DT_RUNPATH
]->d_un
.d_val
),
595 /* The RPATH is ignored. */
596 l
->l_rpath_dirs
= (void *) -1;
600 l
->l_runpath_dirs
= (void *) -1;
602 if (l
->l_info
[DT_RPATH
])
603 /* Allocate room for the search path and fill in information
606 decompose_rpath ((const void *) (D_PTR (l
, l_info
[DT_STRTAB
])
607 + l
->l_info
[DT_RPATH
]->d_un
.d_val
),
610 l
->l_rpath_dirs
= (void *) -1;
615 if (llp
!= NULL
&& *llp
!= '\0')
618 const char *cp
= llp
;
620 /* Decompose the LD_LIBRARY_PATH contents. First determine how many
625 if (*cp
== ':' || *cp
== ';')
630 env_path_list
= (struct r_search_path_elem
**)
631 malloc ((nllp
+ 1) * sizeof (struct r_search_path_elem
*));
632 if (env_path_list
== NULL
)
633 _dl_signal_error (ENOMEM
, NULL
,
634 N_("cannot create cache for search path"));
636 (void) fillin_rpath (local_strdup (llp
), env_path_list
, ":;",
637 __libc_enable_secure
, "LD_LIBRARY_PATH", NULL
);
639 if (env_path_list
[0] == NULL
)
641 free (env_path_list
);
642 env_path_list
= (void *) -1;
646 env_path_list
= (void *) -1;
650 /* Think twice before changing anything in this function. It is placed
651 here and prepared using the `alloca' magic to prevent it from being
652 inlined. The function is only called in case of an error. But then
653 performance does not count. The function used to be "inlinable" and
654 the compiled did so all the time. This increased the code size for
655 absolutely no good reason. */
656 #define LOSE(code, s) lose (code, fd, name, realname, l, s)
658 __attribute__ ((noreturn
))
659 lose (int code
, int fd
, const char *name
, char *realname
, struct link_map
*l
,
662 /* The use of `alloca' here looks ridiculous but it helps. The goal
663 is to avoid the function from being inlined. There is no official
664 way to do this so we use this trick. gcc never inlines functions
665 which use `alloca'. */
666 int *a
= alloca (sizeof (int));
668 (void) __close (a
[0]);
671 /* Remove the stillborn object from the list and free it. */
673 l
->l_prev
->l_next
= l
->l_next
;
675 l
->l_next
->l_prev
= l
->l_prev
;
679 _dl_signal_error (code
, name
, msg
);
683 /* Map in the shared object NAME, actually located in REALNAME, and already
686 #ifndef EXTERNAL_MAP_FROM_FD
690 _dl_map_object_from_fd (const char *name
, int fd
, char *realname
,
691 struct link_map
*loader
, int l_type
)
693 /* This is the expected ELF header. */
694 #define ELF32_CLASS ELFCLASS32
695 #define ELF64_CLASS ELFCLASS64
696 #ifndef VALID_ELF_HEADER
697 # define VALID_ELF_HEADER(hdr,exp,size) (memcmp (hdr, exp, size) == 0)
698 # define VALID_ELF_OSABI(osabi) (osabi == ELFOSABI_SYSV)
699 # define VALID_ELF_ABIVERSION(ver) (ver == 0)
701 static const unsigned char expected
[EI_PAD
] =
707 [EI_CLASS
] = ELFW(CLASS
),
708 [EI_DATA
] = byteorder
,
709 [EI_VERSION
] = EV_CURRENT
,
710 [EI_OSABI
] = ELFOSABI_SYSV
,
713 struct link_map
*l
= NULL
;
715 inline caddr_t
map_segment (ElfW(Addr
) mapstart
, size_t len
,
716 int prot
, int fixed
, off_t offset
)
718 caddr_t mapat
= __mmap ((caddr_t
) mapstart
, len
, prot
,
719 fixed
|MAP_COPY
|MAP_FILE
,
721 if (mapat
== MAP_FAILED
)
722 LOSE (errno
, N_("failed to map segment from shared object"));
726 const ElfW(Ehdr
) *header
;
727 const ElfW(Phdr
) *phdr
;
728 const ElfW(Phdr
) *ph
;
735 /* Get file information. */
736 if (__fxstat (_STAT_VER
, fd
, &st
) < 0)
737 LOSE (errno
, N_("cannot stat shared object"));
739 /* Look again to see if the real name matched another already loaded. */
740 for (l
= _dl_loaded
; l
; l
= l
->l_next
)
741 if (l
->l_ino
== st
.st_ino
&& l
->l_dev
== st
.st_dev
)
743 /* The object is already loaded.
744 Just bump its reference count and return it. */
747 /* If the name is not in the list of names for this object add
750 add_name_to_object (l
, name
);
755 /* Print debugging message. */
756 if (__builtin_expect (_dl_debug_files
, 0))
757 _dl_debug_message (1, "file=", name
, "; generating link map\n", NULL
);
759 /* Read the header directly. */
760 readbuf
= alloca (_dl_pagesize
);
761 readlength
= __libc_read (fd
, readbuf
, _dl_pagesize
);
762 if (readlength
< (ssize_t
) sizeof (*header
))
763 LOSE (errno
, N_("cannot read file data"));
764 header
= (void *) readbuf
;
766 /* Check the header for basic validity. */
767 if (__builtin_expect (!VALID_ELF_HEADER (header
->e_ident
, expected
, EI_PAD
),
770 /* Something is wrong. */
771 if (*(Elf32_Word
*) &header
->e_ident
!=
772 #if BYTE_ORDER == LITTLE_ENDIAN
773 ((ELFMAG0
<< (EI_MAG0
* 8)) |
774 (ELFMAG1
<< (EI_MAG1
* 8)) |
775 (ELFMAG2
<< (EI_MAG2
* 8)) |
776 (ELFMAG3
<< (EI_MAG3
* 8)))
778 ((ELFMAG0
<< (EI_MAG3
* 8)) |
779 (ELFMAG1
<< (EI_MAG2
* 8)) |
780 (ELFMAG2
<< (EI_MAG1
* 8)) |
781 (ELFMAG3
<< (EI_MAG0
* 8)))
784 LOSE (0, N_("invalid ELF header"));
785 if (header
->e_ident
[EI_CLASS
] != ELFW(CLASS
))
787 if (__ELF_NATIVE_CLASS
== 32)
788 LOSE (0, N_("ELF file class not 32-bit"));
790 LOSE (0, N_("ELF file class not 64-bit"));
792 if (header
->e_ident
[EI_DATA
] != byteorder
)
794 if (BYTE_ORDER
== BIG_ENDIAN
)
795 LOSE (0, "ELF file data encoding not big-endian");
797 LOSE (0, "ELF file data encoding not little-endian");
799 if (header
->e_ident
[EI_VERSION
] != EV_CURRENT
)
800 LOSE (0, N_("ELF file version ident does not match current one"));
801 /* XXX We should be able so set system specific versions which are
803 if (!VALID_ELF_OSABI (header
->e_ident
[EI_OSABI
]))
804 LOSE (0, N_("ELF file OS ABI invalid."));
805 if (!VALID_ELF_ABIVERSION (header
->e_ident
[EI_ABIVERSION
]))
806 LOSE (0, N_("ELF file ABI version invalid."));
807 LOSE (0, N_("internal error"));
810 if (__builtin_expect (header
->e_version
, EV_CURRENT
) != EV_CURRENT
)
811 LOSE (0, N_("ELF file version does not not match current one"));
812 if (! __builtin_expect (elf_machine_matches_host (header
->e_machine
), 1))
813 LOSE (0, N_("ELF file machine architecture does not match"));
814 if (__builtin_expect (header
->e_phentsize
, sizeof (ElfW(Phdr
)))
815 != sizeof (ElfW(Phdr
)))
816 LOSE (0, N_("ELF file's phentsize not the expected size"));
820 if (_dl_zerofd
== -1)
822 _dl_zerofd
= _dl_sysdep_open_zero_fill ();
823 if (_dl_zerofd
== -1)
826 _dl_signal_error (errno
, NULL
, N_("cannot open zero fill device"));
831 /* Enter the new object in the list of loaded objects. */
832 l
= _dl_new_object (realname
, name
, l_type
, loader
);
833 if (__builtin_expect (! l
, 0))
834 LOSE (ENOMEM
, N_("cannot create shared object descriptor"));
837 /* Extract the remaining details we need from the ELF header
838 and then read in the program header table. */
839 l
->l_entry
= header
->e_entry
;
840 type
= header
->e_type
;
841 l
->l_phnum
= header
->e_phnum
;
843 maplength
= header
->e_phnum
* sizeof (ElfW(Phdr
));
844 if (header
->e_phoff
+ maplength
<= readlength
)
845 phdr
= (void *) (readbuf
+ header
->e_phoff
);
848 phdr
= alloca (maplength
);
849 __lseek (fd
, SEEK_SET
, header
->e_phoff
);
850 if (__libc_read (fd
, (void *) phdr
, maplength
) != maplength
)
851 LOSE (errno
, N_("cannot read file data"));
855 /* Scan the program header table, collecting its load commands. */
858 ElfW(Addr
) mapstart
, mapend
, dataend
, allocend
;
861 } loadcmds
[l
->l_phnum
], *c
;
862 size_t nloadcmds
= 0;
864 /* The struct is initialized to zero so this is not necessary:
868 for (ph
= phdr
; ph
< &phdr
[l
->l_phnum
]; ++ph
)
871 /* These entries tell us where to find things once the file's
872 segments are mapped in. We record the addresses it says
873 verbatim, and later correct for the run-time load address. */
875 l
->l_ld
= (void *) ph
->p_vaddr
;
876 l
->l_ldnum
= ph
->p_memsz
/ sizeof (ElfW(Dyn
));
879 l
->l_phdr
= (void *) ph
->p_vaddr
;
883 /* A load command tells us to map in part of the file.
884 We record the load commands and process them all later. */
885 if (ph
->p_align
% _dl_pagesize
!= 0)
886 LOSE (0, N_("ELF load command alignment not page-aligned"));
887 if ((ph
->p_vaddr
- ph
->p_offset
) % ph
->p_align
)
889 N_("ELF load command address/offset not properly aligned"));
891 struct loadcmd
*c
= &loadcmds
[nloadcmds
++];
892 c
->mapstart
= ph
->p_vaddr
& ~(ph
->p_align
- 1);
893 c
->mapend
= ((ph
->p_vaddr
+ ph
->p_filesz
+ _dl_pagesize
- 1)
894 & ~(_dl_pagesize
- 1));
895 c
->dataend
= ph
->p_vaddr
+ ph
->p_filesz
;
896 c
->allocend
= ph
->p_vaddr
+ ph
->p_memsz
;
897 c
->mapoff
= ph
->p_offset
& ~(ph
->p_align
- 1);
899 /* Optimize a common case. */
900 if ((PF_R
| PF_W
| PF_X
) == 7
901 && (PROT_READ
| PROT_WRITE
| PROT_EXEC
) == 7)
902 c
->prot
= _dl_pf_to_prot
[ph
->p_flags
& (PF_R
| PF_W
| PF_X
)];
906 if (ph
->p_flags
& PF_R
)
907 c
->prot
|= PROT_READ
;
908 if (ph
->p_flags
& PF_W
)
909 c
->prot
|= PROT_WRITE
;
910 if (ph
->p_flags
& PF_X
)
911 c
->prot
|= PROT_EXEC
;
917 /* Now process the load commands and map segments into memory. */
920 /* Length of the sections to be loaded. */
921 maplength
= loadcmds
[nloadcmds
- 1].allocend
- c
->mapstart
;
923 if (type
== ET_DYN
|| type
== ET_REL
)
925 /* This is a position-independent shared object. We can let the
926 kernel map it anywhere it likes, but we must have space for all
927 the segments in their specified positions relative to the first.
928 So we map the first segment without MAP_FIXED, but with its
929 extent increased to cover all the segments. Then we remove
930 access from excess portion, and there is known sufficient space
931 there to remap from the later segments.
933 As a refinement, sometimes we have an address that we would
934 prefer to map such objects at; but this is only a preference,
935 the OS can do whatever it likes. */
937 mappref
= (ELF_PREFERRED_ADDRESS (loader
, maplength
, c
->mapstart
)
938 - MAP_BASE_ADDR (l
));
940 /* Remember which part of the address space this object uses. */
941 l
->l_map_start
= (ElfW(Addr
)) map_segment (mappref
, maplength
, c
->prot
,
943 l
->l_map_end
= l
->l_map_start
+ maplength
;
944 l
->l_addr
= l
->l_map_start
- c
->mapstart
;
946 /* Change protection on the excess portion to disallow all access;
947 the portions we do not remap later will be inaccessible as if
948 unallocated. Then jump into the normal segment-mapping loop to
949 handle the portion of the segment past the end of the file
951 __mprotect ((caddr_t
) (l
->l_addr
+ c
->mapend
),
952 loadcmds
[nloadcmds
- 1].allocend
- c
->mapend
,
959 /* Notify ELF_PREFERRED_ADDRESS that we have to load this one
961 ELF_FIXED_ADDRESS (loader
, c
->mapstart
);
964 /* Remember which part of the address space this object uses. */
965 l
->l_map_start
= c
->mapstart
+ l
->l_addr
;
966 l
->l_map_end
= l
->l_map_start
+ maplength
;
968 while (c
< &loadcmds
[nloadcmds
])
970 if (c
->mapend
> c
->mapstart
)
971 /* Map the segment contents from the file. */
972 map_segment (l
->l_addr
+ c
->mapstart
, c
->mapend
- c
->mapstart
,
973 c
->prot
, MAP_FIXED
, c
->mapoff
);
977 && c
->mapoff
<= header
->e_phoff
978 && (c
->mapend
- c
->mapstart
+ c
->mapoff
979 >= header
->e_phoff
+ header
->e_phnum
* sizeof (ElfW(Phdr
))))
980 /* Found the program header in this segment. */
981 l
->l_phdr
= (void *) (c
->mapstart
+ header
->e_phoff
- c
->mapoff
);
983 if (c
->allocend
> c
->dataend
)
985 /* Extra zero pages should appear at the end of this segment,
986 after the data mapped from the file. */
987 ElfW(Addr
) zero
, zeroend
, zeropage
;
989 zero
= l
->l_addr
+ c
->dataend
;
990 zeroend
= l
->l_addr
+ c
->allocend
;
991 zeropage
= (zero
+ _dl_pagesize
- 1) & ~(_dl_pagesize
- 1);
993 if (zeroend
< zeropage
)
994 /* All the extra data is in the last page of the segment.
995 We can just zero it. */
1000 /* Zero the final part of the last page of the segment. */
1001 if ((c
->prot
& PROT_WRITE
) == 0)
1004 if (__mprotect ((caddr_t
) (zero
& ~(_dl_pagesize
- 1)),
1005 _dl_pagesize
, c
->prot
|PROT_WRITE
) < 0)
1006 LOSE (errno
, N_("cannot change memory protections"));
1008 memset ((void *) zero
, 0, zeropage
- zero
);
1009 if ((c
->prot
& PROT_WRITE
) == 0)
1010 __mprotect ((caddr_t
) (zero
& ~(_dl_pagesize
- 1)),
1011 _dl_pagesize
, c
->prot
);
1014 if (zeroend
> zeropage
)
1016 /* Map the remaining zero pages in from the zero fill FD. */
1018 mapat
= __mmap ((caddr_t
) zeropage
, zeroend
- zeropage
,
1019 c
->prot
, MAP_ANON
|MAP_PRIVATE
|MAP_FIXED
,
1021 if (mapat
== MAP_FAILED
)
1022 LOSE (errno
, N_("cannot map zero-fill pages"));
1029 if (l
->l_phdr
== NULL
)
1031 /* The program header is not contained in any of the segmenst.
1032 We have to allocate memory ourself and copy it over from
1033 out temporary place. */
1034 ElfW(Phdr
) *newp
= (ElfW(Phdr
) *) malloc (header
->e_phnum
1035 * sizeof (ElfW(Phdr
)));
1037 LOSE (ENOMEM
, N_("cannot allocate memory for program header"));
1039 l
->l_phdr
= memcpy (newp
, phdr
,
1040 (header
->e_phnum
* sizeof (ElfW(Phdr
))));
1041 l
->l_phdr_allocated
= 1;
1044 /* Adjust the PT_PHDR value by the runtime load address. */
1045 (ElfW(Addr
)) l
->l_phdr
+= l
->l_addr
;
1048 /* We are done mapping in the file. We no longer need the descriptor. */
1051 if (l
->l_type
== lt_library
&& type
== ET_EXEC
)
1052 l
->l_type
= lt_executable
;
1057 LOSE (0, N_("object file has no dynamic section"));
1060 (ElfW(Addr
)) l
->l_ld
+= l
->l_addr
;
1062 l
->l_entry
+= l
->l_addr
;
1064 if (__builtin_expect (_dl_debug_files
, 0))
1066 const size_t nibbles
= sizeof (void *) * 2;
1067 char buf1
[nibbles
+ 1];
1068 char buf2
[nibbles
+ 1];
1069 char buf3
[nibbles
+ 1];
1071 buf1
[nibbles
] = '\0';
1072 buf2
[nibbles
] = '\0';
1073 buf3
[nibbles
] = '\0';
1075 memset (buf1
, '0', nibbles
);
1076 memset (buf2
, '0', nibbles
);
1077 memset (buf3
, '0', nibbles
);
1078 _itoa_word ((unsigned long int) l
->l_ld
, &buf1
[nibbles
], 16, 0);
1079 _itoa_word ((unsigned long int) l
->l_addr
, &buf2
[nibbles
], 16, 0);
1080 _itoa_word (maplength
, &buf3
[nibbles
], 16, 0);
1082 _dl_debug_message (1, " dynamic: 0x", buf1
, " base: 0x", buf2
,
1083 " size: 0x", buf3
, "\n", NULL
);
1084 memset (buf1
, '0', nibbles
);
1085 memset (buf2
, '0', nibbles
);
1086 memset (buf3
, ' ', nibbles
);
1087 _itoa_word ((unsigned long int) l
->l_entry
, &buf1
[nibbles
], 16, 0);
1088 _itoa_word ((unsigned long int) l
->l_phdr
, &buf2
[nibbles
], 16, 0);
1089 _itoa_word (l
->l_phnum
, &buf3
[nibbles
], 10, 0);
1090 _dl_debug_message (1, " entry: 0x", buf1
, " phdr: 0x", buf2
,
1091 " phnum: ", buf3
, "\n\n", NULL
);
1094 elf_get_dynamic_info (l
);
1095 if (l
->l_info
[DT_HASH
])
1098 /* If this object has DT_SYMBOLIC set modify now its scope. We don't
1099 have to do this for the main map. */
1100 if (l
->l_info
[DT_SYMBOLIC
] && &l
->l_searchlist
!= l
->l_scope
[0])
1102 /* Create an appropriate searchlist. It contains only this map.
1104 XXX This is the definition of DT_SYMBOLIC in SysVr4. The old
1105 GNU ld.so implementation had a different interpretation which
1106 is more reasonable. We are prepared to add this possibility
1107 back as part of a GNU extension of the ELF format. */
1108 l
->l_symbolic_searchlist
.r_list
=
1109 (struct link_map
**) malloc (sizeof (struct link_map
*));
1111 if (l
->l_symbolic_searchlist
.r_list
== NULL
)
1112 LOSE (ENOMEM
, N_("cannot create searchlist"));
1114 l
->l_symbolic_searchlist
.r_list
[0] = l
;
1115 l
->l_symbolic_searchlist
.r_nlist
= 1;
1116 l
->l_symbolic_searchlist
.r_duplist
= l
->l_symbolic_searchlist
.r_list
;
1117 l
->l_symbolic_searchlist
.r_nduplist
= 1;
1119 /* Now move the existing entries one back. */
1120 memmove (&l
->l_scope
[1], &l
->l_scope
[0],
1121 sizeof (l
->l_scope
) - sizeof (l
->l_scope
[0]));
1123 /* Now add the new entry. */
1124 l
->l_scope
[0] = &l
->l_symbolic_searchlist
;
1127 /* Finally the file information. */
1128 l
->l_dev
= st
.st_dev
;
1129 l
->l_ino
= st
.st_ino
;
1134 /* Print search path. */
1136 print_search_path (struct r_search_path_elem
**list
,
1137 const char *what
, const char *name
)
1139 char buf
[max_dirnamelen
+ max_capstrlen
];
1142 _dl_debug_message (1, " search path=", NULL
);
1144 while (*list
!= NULL
&& (*list
)->what
== what
) /* Yes, ==. */
1146 char *endp
= __mempcpy (buf
, (*list
)->dirname
, (*list
)->dirnamelen
);
1149 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
1150 if ((*list
)->status
[cnt
] != nonexisting
)
1152 char *cp
= __mempcpy (endp
, capstr
[cnt
].str
, capstr
[cnt
].len
);
1153 if (cp
== buf
|| (cp
== buf
+ 1 && buf
[0] == '/'))
1157 _dl_debug_message (0, first
? "" : ":", buf
, NULL
);
1165 _dl_debug_message (0, "\t\t(", what
, " from file ",
1166 name
[0] ? name
: _dl_argv
[0], ")\n", NULL
);
1168 _dl_debug_message (0, "\t\t(", what
, ")\n", NULL
);
1171 /* Try to open NAME in one of the directories in DIRS.
1172 Return the fd, or -1. If successful, fill in *REALNAME
1173 with the malloc'd full directory name. */
1176 open_path (const char *name
, size_t namelen
, int preloaded
,
1177 struct r_search_path_elem
***dirsp
,
1180 struct r_search_path_elem
**dirs
= *dirsp
;
1183 const char *current_what
= NULL
;
1186 buf
= alloca (max_dirnamelen
+ max_capstrlen
+ namelen
);
1189 struct r_search_path_elem
*this_dir
= *dirs
;
1194 /* If we are debugging the search for libraries print the path
1195 now if it hasn't happened now. */
1196 if (__builtin_expect (_dl_debug_libs
, 0)
1197 && current_what
!= this_dir
->what
)
1199 current_what
= this_dir
->what
;
1200 print_search_path (dirs
, current_what
, this_dir
->where
);
1203 edp
= (char *) __mempcpy (buf
, this_dir
->dirname
, this_dir
->dirnamelen
);
1204 for (cnt
= 0; fd
== -1 && cnt
< ncapstr
; ++cnt
)
1206 /* Skip this directory if we know it does not exist. */
1207 if (this_dir
->status
[cnt
] == nonexisting
)
1211 ((char *) __mempcpy (__mempcpy (edp
,
1212 capstr
[cnt
].str
, capstr
[cnt
].len
),
1216 /* Print name we try if this is wanted. */
1217 if (__builtin_expect (_dl_debug_libs
, 0))
1218 _dl_debug_message (1, " trying file=", buf
, "\n", NULL
);
1220 fd
= __open (buf
, O_RDONLY
);
1221 if (this_dir
->status
[cnt
] == unknown
)
1224 this_dir
->status
[cnt
] = existing
;
1227 /* We failed to open machine dependent library. Let's
1228 test whether there is any directory at all. */
1231 buf
[buflen
- namelen
- 1] = '\0';
1233 if (__xstat (_STAT_VER
, buf
, &st
) != 0
1234 || ! S_ISDIR (st
.st_mode
))
1235 /* The directory does not exist or it is no directory. */
1236 this_dir
->status
[cnt
] = nonexisting
;
1238 this_dir
->status
[cnt
] = existing
;
1242 /* Remember whether we found any existing directory. */
1243 any
|= this_dir
->status
[cnt
] == existing
;
1245 if (fd
!= -1 && preloaded
&& __libc_enable_secure
)
1247 /* This is an extra security effort to make sure nobody can
1248 preload broken shared objects which are in the trusted
1249 directories and so exploit the bugs. */
1252 if (__fxstat (_STAT_VER
, fd
, &st
) != 0
1253 || (st
.st_mode
& S_ISUID
) == 0)
1255 /* The shared object cannot be tested for being SUID
1256 or this bit is not set. In this case we must not
1260 /* We simply ignore the file, signal this by setting
1261 the error value which would have been set by `open'. */
1269 *realname
= malloc (buflen
);
1270 if (*realname
!= NULL
)
1272 memcpy (*realname
, buf
, buflen
);
1277 /* No memory for the name, we certainly won't be able
1278 to load and link it. */
1283 if (errno
!= ENOENT
&& errno
!= EACCES
)
1284 /* The file exists and is readable, but something went wrong. */
1287 while (*++dirs
!= NULL
);
1289 /* Remove the whole path if none of the directories exists. */
1293 *dirsp
= (void *) -1;
1299 /* Map in the shared object file NAME. */
1303 _dl_map_object (struct link_map
*loader
, const char *name
, int preloaded
,
1304 int type
, int trace_mode
)
1311 /* Look for this name among those already loaded. */
1312 for (l
= _dl_loaded
; l
; l
= l
->l_next
)
1314 /* If the requested name matches the soname of a loaded object,
1315 use that object. Elide this check for names that have not
1317 if (l
->l_opencount
<= 0)
1319 if (!_dl_name_match_p (name
, l
))
1323 if (l
->l_info
[DT_SONAME
] == NULL
)
1326 soname
= (const void *) (D_PTR (l
, l_info
[DT_STRTAB
])
1327 + l
->l_info
[DT_SONAME
]->d_un
.d_val
);
1328 if (strcmp (name
, soname
) != 0)
1331 /* We have a match on a new name -- cache it. */
1332 add_name_to_object (l
, soname
);
1335 /* We have a match -- bump the reference count and return it. */
1340 /* Display information if we are debugging. */
1341 if (__builtin_expect (_dl_debug_files
, 0) && loader
!= NULL
)
1342 _dl_debug_message (1, "\nfile=", name
, "; needed by ",
1343 loader
->l_name
[0] ? loader
->l_name
: _dl_argv
[0],
1346 if (strchr (name
, '/') == NULL
)
1348 /* Search for NAME in several places. */
1350 size_t namelen
= strlen (name
) + 1;
1352 if (__builtin_expect (_dl_debug_libs
, 0))
1353 _dl_debug_message (1, "find library=", name
, "; searching\n", NULL
);
1357 /* When the object has the RUNPATH information we don't use any
1359 if (loader
== NULL
|| loader
->l_info
[DT_RUNPATH
] == NULL
)
1361 /* First try the DT_RPATH of the dependent object that caused NAME
1362 to be loaded. Then that object's dependent, and on up. */
1363 for (l
= loader
; fd
== -1 && l
; l
= l
->l_loader
)
1365 if (l
->l_rpath_dirs
== NULL
)
1367 if (l
->l_info
[DT_RPATH
] == NULL
)
1368 /* There is no path. */
1369 l
->l_rpath_dirs
= (void *) -1;
1372 /* Make sure the cache information is available. */
1373 size_t ptrval
= (D_PTR (l
, l_info
[DT_STRTAB
])
1374 + l
->l_info
[DT_RPATH
]->d_un
.d_val
);
1376 decompose_rpath ((const char *) ptrval
, l
,
1379 if (l
->l_rpath_dirs
!= (void *) -1)
1380 fd
= open_path (name
, namelen
, preloaded
,
1381 &l
->l_rpath_dirs
, &realname
);
1384 else if (l
->l_rpath_dirs
!= (void *) -1)
1385 fd
= open_path (name
, namelen
, preloaded
, &l
->l_rpath_dirs
,
1389 /* If dynamically linked, try the DT_RPATH of the executable
1392 if (fd
== -1 && l
&& l
->l_type
!= lt_loaded
&& l
!= loader
1393 && l
->l_rpath_dirs
!= (void *) -1)
1394 fd
= open_path (name
, namelen
, preloaded
, &l
->l_rpath_dirs
,
1398 /* Try the LD_LIBRARY_PATH environment variable. */
1399 if (fd
== -1 && env_path_list
!= (void *) -1)
1400 fd
= open_path (name
, namelen
, preloaded
, &env_path_list
, &realname
);
1402 /* Look at the RUNPATH informaiton for this binary. */
1403 if (loader
!= NULL
&& loader
->l_runpath_dirs
!= (void *) -1)
1405 if (loader
->l_runpath_dirs
== NULL
)
1407 if (loader
->l_info
[DT_RUNPATH
] == NULL
)
1409 loader
->l_runpath_dirs
= (void *) -1;
1412 /* Make sure the cache information is available. */
1413 size_t ptrval
= (D_PTR (loader
, l_info
[DT_STRTAB
])
1414 + loader
->l_info
[DT_RUNPATH
]->d_un
.d_val
);
1415 loader
->l_runpath_dirs
=
1416 decompose_rpath ((const char *) ptrval
, loader
, "RUNPATH");
1418 if (loader
->l_runpath_dirs
!= (void *) -1)
1419 fd
= open_path (name
, namelen
, preloaded
,
1420 &loader
->l_runpath_dirs
, &realname
);
1423 else if (loader
->l_runpath_dirs
!= (void *) -1)
1424 fd
= open_path (name
, namelen
, preloaded
,
1425 &loader
->l_runpath_dirs
, &realname
);
1430 /* Check the list of libraries in the file /etc/ld.so.cache,
1431 for compatibility with Linux's ldconfig program. */
1432 extern const char *_dl_load_cache_lookup (const char *name
);
1433 const char *cached
= _dl_load_cache_lookup (name
);
1436 fd
= __open (cached
, O_RDONLY
);
1439 realname
= local_strdup (cached
);
1440 if (realname
== NULL
)
1449 /* Finally, try the default path. */
1451 fd
= open_path (name
, namelen
, preloaded
, &rtld_search_dirs
,
1454 /* Add another newline when we a tracing the library loading. */
1455 if (__builtin_expect (_dl_debug_libs
, 0))
1456 _dl_debug_message (1, "\n", NULL
);
1460 /* The path may contain dynamic string tokens. */
1462 ? expand_dynamic_string_token (loader
, name
)
1463 : local_strdup (name
));
1464 if (realname
== NULL
)
1468 fd
= __open (realname
, O_RDONLY
);
1478 /* We haven't found an appropriate library. But since we
1479 are only interested in the list of libraries this isn't
1480 so severe. Fake an entry with all the information we
1482 static const Elf_Symndx dummy_bucket
= STN_UNDEF
;
1484 /* Enter the new object in the list of loaded objects. */
1485 if ((name_copy
= local_strdup (name
)) == NULL
1486 || (l
= _dl_new_object (name_copy
, name
, type
, loader
)) == NULL
)
1487 _dl_signal_error (ENOMEM
, name
,
1488 N_("cannot create shared object descriptor"));
1489 /* We use an opencount of 0 as a sign for the faked entry.
1490 Since the descriptor is initialized with zero we do not
1493 l->l_reserved = 0; */
1494 l
->l_buckets
= &dummy_bucket
;
1501 _dl_signal_error (errno
, name
, N_("cannot open shared object file"));
1504 return _dl_map_object_from_fd (name
, fd
, realname
, loader
, type
);