1 /* Map in a shared object's segments from the file.
2 Copyright (C) 1995,96,97,98,99,2000,2001 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
29 #include <sys/param.h>
31 #include <sys/types.h>
32 #include "dynamic-link.h"
34 #include <dl-osinfo.h>
38 /* On some systems, no flag bits are given to specify file mapping. */
43 /* The right way to map in the shared library files is MAP_COPY, which
44 makes a virtual copy of the data at the time of the mmap call; this
45 guarantees the mapped pages will be consistent even if the file is
46 overwritten. Some losing VM systems like Linux's lack MAP_COPY. All we
47 get is MAP_PRIVATE, which copies each page when it is modified; this
48 means if the file is overwritten, we may at some point get some pages
49 from the new version after starting with pages from the old version. */
51 # define MAP_COPY MAP_PRIVATE
54 /* Some systems link their relocatable objects for another base address
55 than 0. We want to know the base address for these such that we can
56 subtract this address from the segment addresses during mapping.
57 This results in a more efficient address space usage. Defaults to
58 zero for almost all systems. */
60 # define MAP_BASE_ADDR(l) 0
65 #if BYTE_ORDER == BIG_ENDIAN
66 # define byteorder ELFDATA2MSB
67 #elif BYTE_ORDER == LITTLE_ENDIAN
68 # define byteorder ELFDATA2LSB
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)
96 /* Type for the buffer we put the ELF header and hopefully the program
97 header. This buffer does not really have to be too large. In most
98 cases the program header follows the ELF header directly. If this
99 is not the case all bets are off and we can make the header arbitrarily
100 large and still won't get it read. This means the only question is
101 how large are the ELF and program header combined. The ELF header
102 in 64-bit files is 56 bytes long. Each program header entry is again
103 56 bytes long. I.e., even with a file which has 17 program header
104 entries we only have to read 1kB. And 17 program header entries is
105 plenty, normal files have < 10. If this heuristic should really fail
106 for some file the code in `_dl_map_object_from_fd' knows how to
116 unsigned int _dl_osversion
;
120 extern const char *_dl_platform
;
121 extern size_t _dl_platformlen
;
123 /* The object to be initialized first. */
124 struct link_map
*_dl_initfirst
;
126 /* This is the decomposed LD_LIBRARY_PATH search path. */
127 static struct r_search_path_struct env_path_list
;
129 /* List of the hardware capabilities we might end up using. */
130 static const struct r_strlenpair
*capstr
;
131 static size_t ncapstr
;
132 static size_t max_capstrlen
;
135 /* Get the generated information about the trusted directories. */
136 #include "trusted-dirs.h"
138 static const char system_dirs
[] = SYSTEM_DIRS
;
139 static const size_t system_dirs_len
[] =
143 #define nsystem_dirs_len \
144 (sizeof (system_dirs_len) / sizeof (system_dirs_len[0]))
147 /* Local version of `strdup' function. */
149 local_strdup (const char *s
)
151 size_t len
= strlen (s
) + 1;
152 void *new = malloc (len
);
157 return (char *) memcpy (new, s
, len
);
162 is_dst (const char *start
, const char *name
, const char *str
, size_t cmplen
,
163 int is_path
, int secure
)
167 if (strncmp (name
, str
, cmplen
) == 0)
169 else if (strncmp (name
, str
+ 1, cmplen
- 2) == 0
170 && (name
[cmplen
- 2] == '\0' || name
[cmplen
- 2] == '/'
171 || (is_path
&& name
[cmplen
- 2] == ':')))
176 if (__builtin_expect (secure
, 0)
177 && ((name
[len
- 1] != '\0' && (!is_path
|| name
[len
- 1] != ':'))
178 || (name
!= start
+ 1 && (!is_path
|| name
[-2] != ':'))))
186 _dl_dst_count (const char *name
, int is_path
)
188 const char *const start
= name
;
195 /* $ORIGIN is not expanded for SUID/GUID programs (except if it
196 is $ORIGIN alone) and it must always appear first in path.
198 Note that it is no bug that the string in the second and
199 fourth `strncmp' call is longer than the sequence which is
201 if ((len
= is_dst (start
, name
+ 1, "{ORIGIN}", 8, is_path
,
202 __libc_enable_secure
)) != 0
203 || ((len
= is_dst (start
, name
+ 1, "{PLATFORM}", 10, is_path
, 0))
207 name
= strchr (name
+ len
, '$');
209 while (name
!= NULL
);
216 _dl_dst_substitute (struct link_map
*l
, const char *name
, char *result
,
219 const char *const start
= name
;
220 char *last_elem
, *wp
;
222 /* Now fill the result path. While copying over the string we keep
223 track of the start of the last path element. When we come accross
224 a DST we copy over the value or (if the value is not available)
225 leave the entire path element out. */
226 last_elem
= wp
= result
;
230 if (__builtin_expect (*name
== '$', 0))
232 const char *repl
= NULL
;
235 /* Note that it is no bug that the string in the second and
236 fourth `strncmp' call is longer than the sequence which
237 is actually tested. */
238 if ((len
= is_dst (start
, name
+ 1, "{ORIGIN}", 8, is_path
,
239 __libc_enable_secure
)) != 0)
241 else if ((len
= is_dst (start
, name
+ 1, "{PLATFORM}", 10, is_path
,
245 if (repl
!= NULL
&& repl
!= (const char *) -1)
247 wp
= __stpcpy (wp
, repl
);
252 /* We cannot use this path element, the value of the
253 replacement is unknown. */
256 while (*name
!= '\0' && (!is_path
|| *name
!= ':'))
260 /* No DST we recognize. */
266 if (is_path
&& *name
== ':')
270 while (*name
!= '\0');
278 /* Return copy of argument with all recognized dynamic string tokens
279 ($ORIGIN and $PLATFORM for now) replaced. On some platforms it
280 might not be possible to determine the path from which the object
281 belonging to the map is loaded. In this case the path element
282 containing $ORIGIN is left out. */
284 expand_dynamic_string_token (struct link_map
*l
, const char *s
)
286 /* We make two runs over the string. First we determine how large the
287 resulting string is and then we copy it over. Since this is now
288 frequently executed operation we are looking here not for performance
289 but rather for code size. */
294 /* Determine the number of DST elements. */
295 cnt
= DL_DST_COUNT (s
, 1);
297 /* If we do not have to replace anything simply copy the string. */
298 if (__builtin_expect (cnt
, 0) == 0)
299 return local_strdup (s
);
301 /* Determine the length of the substituted string. */
302 total
= DL_DST_REQUIRED (l
, s
, strlen (s
), cnt
);
304 /* Allocate the necessary memory. */
305 result
= (char *) malloc (total
+ 1);
309 return DL_DST_SUBSTITUTE (l
, s
, result
, 1);
313 /* Add `name' to the list of names for a particular shared object.
314 `name' is expected to have been allocated with malloc and will
315 be freed if the shared object already has this name.
316 Returns false if the object already had this name. */
319 add_name_to_object (struct link_map
*l
, const char *name
)
321 struct libname_list
*lnp
, *lastp
;
322 struct libname_list
*newname
;
326 for (lnp
= l
->l_libname
; lnp
!= NULL
; lastp
= lnp
, lnp
= lnp
->next
)
327 if (strcmp (name
, lnp
->name
) == 0)
330 name_len
= strlen (name
) + 1;
331 newname
= (struct libname_list
*) malloc (sizeof *newname
+ name_len
);
334 /* No more memory. */
335 _dl_signal_error (ENOMEM
, name
, NULL
, N_("cannot allocate name record"));
338 /* The object should have a libname set from _dl_new_object. */
339 assert (lastp
!= NULL
);
341 newname
->name
= memcpy (newname
+ 1, name
, name_len
);
342 newname
->next
= NULL
;
343 newname
->dont_free
= 0;
344 lastp
->next
= newname
;
347 /* All known directories in sorted order. */
348 struct r_search_path_elem
*_dl_all_dirs
;
350 /* All directories after startup. */
351 struct r_search_path_elem
*_dl_init_all_dirs
;
353 /* Standard search directories. */
354 static struct r_search_path_struct rtld_search_dirs
;
356 static size_t max_dirnamelen
;
358 static inline struct r_search_path_elem
**
359 fillin_rpath (char *rpath
, struct r_search_path_elem
**result
, const char *sep
,
360 int check_trusted
, const char *what
, const char *where
)
365 while ((cp
= __strsep (&rpath
, sep
)) != NULL
)
367 struct r_search_path_elem
*dirp
;
368 size_t len
= strlen (cp
);
370 /* `strsep' can pass an empty string. This has to be
371 interpreted as `use the current directory'. */
374 static const char curwd
[] = "./";
378 /* Remove trailing slashes (except for "/"). */
379 while (len
> 1 && cp
[len
- 1] == '/')
382 /* Now add one if there is none so far. */
383 if (len
> 0 && cp
[len
- 1] != '/')
386 /* Make sure we don't use untrusted directories if we run SUID. */
387 if (__builtin_expect (check_trusted
, 0))
389 const char *trun
= system_dirs
;
393 /* All trusted directories must be complete names. */
396 for (idx
= 0; idx
< nsystem_dirs_len
; ++idx
)
398 if (len
== system_dirs_len
[idx
]
399 && memcmp (trun
, cp
, len
) == 0)
406 trun
+= system_dirs_len
[idx
] + 1;
411 /* Simply drop this directory. */
415 /* See if this directory is already known. */
416 for (dirp
= _dl_all_dirs
; dirp
!= NULL
; dirp
= dirp
->next
)
417 if (dirp
->dirnamelen
== len
&& memcmp (cp
, dirp
->dirname
, len
) == 0)
422 /* It is available, see whether it's on our own list. */
424 for (cnt
= 0; cnt
< nelems
; ++cnt
)
425 if (result
[cnt
] == dirp
)
429 result
[nelems
++] = dirp
;
434 enum r_dir_status init_val
;
435 size_t where_len
= where
? strlen (where
) + 1 : 0;
437 /* It's a new directory. Create an entry and add it. */
438 dirp
= (struct r_search_path_elem
*)
439 malloc (sizeof (*dirp
) + ncapstr
* sizeof (enum r_dir_status
)
440 + where_len
+ len
+ 1);
442 _dl_signal_error (ENOMEM
, NULL
, NULL
,
443 N_("cannot create cache for search path"));
445 dirp
->dirname
= ((char *) dirp
+ sizeof (*dirp
)
446 + ncapstr
* sizeof (enum r_dir_status
));
447 *((char *) __mempcpy ((char *) dirp
->dirname
, cp
, len
)) = '\0';
448 dirp
->dirnamelen
= len
;
450 if (len
> max_dirnamelen
)
451 max_dirnamelen
= len
;
453 /* We have to make sure all the relative directories are
454 never ignored. The current directory might change and
455 all our saved information would be void. */
456 init_val
= cp
[0] != '/' ? existing
: unknown
;
457 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
458 dirp
->status
[cnt
] = init_val
;
461 if (__builtin_expect (where
!= NULL
, 1))
462 dirp
->where
= memcpy ((char *) dirp
+ sizeof (*dirp
) + len
+ 1
463 + ncapstr
* sizeof (enum r_dir_status
),
468 dirp
->next
= _dl_all_dirs
;
471 /* Put it in the result array. */
472 result
[nelems
++] = dirp
;
476 /* Terminate the array. */
477 result
[nelems
] = NULL
;
485 decompose_rpath (struct r_search_path_struct
*sps
,
486 const char *rpath
, struct link_map
*l
, const char *what
)
488 /* Make a copy we can work with. */
489 const char *where
= l
->l_name
;
492 struct r_search_path_elem
**result
;
494 /* Initialize to please the compiler. */
495 const char *errstring
= NULL
;
497 /* First see whether we must forget the RUNPATH and RPATH from this
499 if (__builtin_expect (_dl_inhibit_rpath
!= NULL
, 0) && !__libc_enable_secure
)
501 const char *found
= strstr (_dl_inhibit_rpath
, where
);
504 size_t len
= strlen (where
);
505 if ((found
== _dl_inhibit_rpath
|| found
[-1] == ':')
506 && (found
[len
] == '\0' || found
[len
] == ':'))
508 /* This object is on the list of objects for which the
509 RUNPATH and RPATH must not be used. */
510 result
= (struct r_search_path_elem
**)
511 malloc (sizeof (*result
));
515 errstring
= N_("cannot create cache for search path");
517 _dl_signal_error (ENOMEM
, NULL
, NULL
, errstring
);
530 /* Make a writable copy. At the same time expand possible dynamic
532 copy
= expand_dynamic_string_token (l
, rpath
);
535 errstring
= N_("cannot create RUNPATH/RPATH copy");
539 /* Count the number of necessary elements in the result array. */
541 for (cp
= copy
; *cp
!= '\0'; ++cp
)
545 /* Allocate room for the result. NELEMS + 1 is an upper limit for the
546 number of necessary entries. */
547 result
= (struct r_search_path_elem
**) malloc ((nelems
+ 1 + 1)
550 goto signal_error_cache
;
552 fillin_rpath (copy
, result
, ":", 0, what
, where
);
554 /* Free the copied RPATH string. `fillin_rpath' make own copies if
559 /* The caller will change this value if we haven't used a real malloc. */
566 _dl_init_paths (const char *llp
)
570 struct r_search_path_elem
*pelem
, **aelem
;
575 /* Initialize to please the compiler. */
576 const char *errstring
= NULL
;
578 /* Fill in the information about the application's RPATH and the
579 directories addressed by the LD_LIBRARY_PATH environment variable. */
581 /* Get the capabilities. */
582 capstr
= _dl_important_hwcaps (_dl_platform
, _dl_platformlen
,
583 &ncapstr
, &max_capstrlen
);
585 /* First set up the rest of the default search directory entries. */
586 aelem
= rtld_search_dirs
.dirs
= (struct r_search_path_elem
**)
587 malloc ((nsystem_dirs_len
+ 1) * sizeof (struct r_search_path_elem
*));
588 if (rtld_search_dirs
.dirs
== NULL
)
590 errstring
= N_("cannot create search path array");
592 _dl_signal_error (ENOMEM
, NULL
, NULL
, errstring
);
595 round_size
= ((2 * sizeof (struct r_search_path_elem
) - 1
596 + ncapstr
* sizeof (enum r_dir_status
))
597 / sizeof (struct r_search_path_elem
));
599 rtld_search_dirs
.dirs
[0] = (struct r_search_path_elem
*)
600 malloc ((sizeof (system_dirs
) / sizeof (system_dirs
[0]))
601 * round_size
* sizeof (struct r_search_path_elem
));
602 if (rtld_search_dirs
.dirs
[0] == NULL
)
604 errstring
= N_("cannot create cache for search path");
608 rtld_search_dirs
.malloced
= 0;
609 pelem
= _dl_all_dirs
= rtld_search_dirs
.dirs
[0];
619 pelem
->what
= "system search path";
622 pelem
->dirname
= strp
;
623 pelem
->dirnamelen
= system_dirs_len
[idx
];
624 strp
+= system_dirs_len
[idx
] + 1;
626 /* System paths must be absolute. */
627 assert (pelem
->dirname
[0] == '/');
628 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
629 pelem
->status
[cnt
] = unknown
;
631 pelem
->next
= (++idx
== nsystem_dirs_len
? NULL
: (pelem
+ round_size
));
635 while (idx
< nsystem_dirs_len
);
637 max_dirnamelen
= SYSTEM_DIRS_MAX_LEN
;
641 /* This points to the map of the main object. */
645 assert (l
->l_type
!= lt_loaded
);
647 if (l
->l_info
[DT_RUNPATH
])
649 /* Allocate room for the search path and fill in information
651 decompose_rpath (&l
->l_runpath_dirs
,
652 (const void *) (D_PTR (l
, l_info
[DT_STRTAB
])
653 + l
->l_info
[DT_RUNPATH
]->d_un
.d_val
),
656 /* The RPATH is ignored. */
657 l
->l_rpath_dirs
.dirs
= (void *) -1;
661 l
->l_runpath_dirs
.dirs
= (void *) -1;
663 if (l
->l_info
[DT_RPATH
])
665 /* Allocate room for the search path and fill in information
667 decompose_rpath (&l
->l_rpath_dirs
,
668 (const void *) (D_PTR (l
, l_info
[DT_STRTAB
])
669 + l
->l_info
[DT_RPATH
]->d_un
.d_val
),
671 l
->l_rpath_dirs
.malloced
= 0;
674 l
->l_rpath_dirs
.dirs
= (void *) -1;
679 if (llp
!= NULL
&& *llp
!= '\0')
682 const char *cp
= llp
;
683 char *llp_tmp
= strdupa (llp
);
685 /* Decompose the LD_LIBRARY_PATH contents. First determine how many
690 if (*cp
== ':' || *cp
== ';')
695 env_path_list
.dirs
= (struct r_search_path_elem
**)
696 malloc ((nllp
+ 1) * sizeof (struct r_search_path_elem
*));
697 if (env_path_list
.dirs
== NULL
)
699 errstring
= N_("cannot create cache for search path");
703 (void) fillin_rpath (llp_tmp
, env_path_list
.dirs
, ":;",
704 __libc_enable_secure
, "LD_LIBRARY_PATH", NULL
);
706 if (env_path_list
.dirs
[0] == NULL
)
708 free (env_path_list
.dirs
);
709 env_path_list
.dirs
= (void *) -1;
712 env_path_list
.malloced
= 0;
715 env_path_list
.dirs
= (void *) -1;
717 /* Remember the last search directory added at startup. */
718 _dl_init_all_dirs
= _dl_all_dirs
;
722 /* Think twice before changing anything in this function. It is placed
723 here and prepared using the `alloca' magic to prevent it from being
724 inlined. The function is only called in case of an error. But then
725 performance does not count. The function used to be "inlinable" and
726 the compiled did so all the time. This increased the code size for
727 absolutely no good reason. */
729 __attribute__ ((noreturn
))
730 lose (int code
, int fd
, const char *name
, char *realname
, struct link_map
*l
,
733 /* The use of `alloca' here looks ridiculous but it helps. The goal
734 is to avoid the function from being inlined. There is no official
735 way to do this so we use this trick. gcc never inlines functions
736 which use `alloca'. */
737 int *a
= (int *) alloca (sizeof (int));
739 /* The file might already be closed. */
741 (void) __close (a
[0]);
744 /* Remove the stillborn object from the list and free it. */
745 assert (l
->l_next
== NULL
);
747 if (l
->l_prev
== NULL
)
748 /* No other module loaded. */
752 l
->l_prev
->l_next
= NULL
;
757 _dl_signal_error (code
, name
, NULL
, msg
);
761 /* Map in the shared object NAME, actually located in REALNAME, and already
764 #ifndef EXTERNAL_MAP_FROM_FD
768 _dl_map_object_from_fd (const char *name
, int fd
, struct filebuf
*fbp
,
769 char *realname
, struct link_map
*loader
, int l_type
,
772 struct link_map
*l
= NULL
;
773 const ElfW(Ehdr
) *header
;
774 const ElfW(Phdr
) *phdr
;
775 const ElfW(Phdr
) *ph
;
779 /* Initialize to keep the compiler happy. */
780 const char *errstring
= NULL
;
783 /* Get file information. */
784 if (__builtin_expect (__fxstat64 (_STAT_VER
, fd
, &st
) < 0, 0))
786 errstring
= N_("cannot stat shared object");
790 lose (errval
, fd
, name
, realname
, l
, errstring
);
793 /* Look again to see if the real name matched another already loaded. */
794 for (l
= _dl_loaded
; l
; l
= l
->l_next
)
795 if (l
->l_ino
== st
.st_ino
&& l
->l_dev
== st
.st_dev
)
797 /* The object is already loaded.
798 Just bump its reference count and return it. */
801 /* If the name is not in the list of names for this object add
804 add_name_to_object (l
, name
);
809 if (mode
& RTLD_NOLOAD
)
810 /* We are not supposed to load the object unless it is already
811 loaded. So return now. */
814 /* Print debugging message. */
815 if (__builtin_expect (_dl_debug_mask
& DL_DEBUG_FILES
, 0))
816 _dl_debug_printf ("file=%s; generating link map\n", name
);
818 /* This is the ELF header. We read it in `open_verify'. */
819 header
= (void *) fbp
->buf
;
823 if (_dl_zerofd
== -1)
825 _dl_zerofd
= _dl_sysdep_open_zero_fill ();
826 if (_dl_zerofd
== -1)
829 _dl_signal_error (errno
, NULL
, NULL
,
830 N_("cannot open zero fill device"));
835 /* Enter the new object in the list of loaded objects. */
836 l
= _dl_new_object (realname
, name
, l_type
, loader
);
837 if (__builtin_expect (! l
, 0))
839 errstring
= N_("cannot create shared object descriptor");
840 goto call_lose_errno
;
843 /* Extract the remaining details we need from the ELF header
844 and then read in the program header table. */
845 l
->l_entry
= header
->e_entry
;
846 type
= header
->e_type
;
847 l
->l_phnum
= header
->e_phnum
;
849 maplength
= header
->e_phnum
* sizeof (ElfW(Phdr
));
850 if (header
->e_phoff
+ maplength
<= fbp
->len
)
851 phdr
= (void *) (fbp
->buf
+ header
->e_phoff
);
854 phdr
= alloca (maplength
);
855 __lseek (fd
, SEEK_SET
, header
->e_phoff
);
856 if (__libc_read (fd
, (void *) phdr
, maplength
) != maplength
)
858 errstring
= N_("cannot read file data");
859 goto call_lose_errno
;
864 /* Scan the program header table, collecting its load commands. */
867 ElfW(Addr
) mapstart
, mapend
, dataend
, allocend
;
870 } loadcmds
[l
->l_phnum
], *c
;
871 size_t nloadcmds
= 0;
873 /* The struct is initialized to zero so this is not necessary:
877 for (ph
= phdr
; ph
< &phdr
[l
->l_phnum
]; ++ph
)
880 /* These entries tell us where to find things once the file's
881 segments are mapped in. We record the addresses it says
882 verbatim, and later correct for the run-time load address. */
884 l
->l_ld
= (void *) ph
->p_vaddr
;
885 l
->l_ldnum
= ph
->p_memsz
/ sizeof (ElfW(Dyn
));
889 l
->l_phdr
= (void *) ph
->p_vaddr
;
893 /* A load command tells us to map in part of the file.
894 We record the load commands and process them all later. */
895 if ((ph
->p_align
& (_dl_pagesize
- 1)) != 0)
897 errstring
= N_("ELF load command alignment not page-aligned");
900 if (((ph
->p_vaddr
- ph
->p_offset
) & (ph
->p_align
- 1)) != 0)
903 = N_("ELF load command address/offset not properly aligned");
908 struct loadcmd
*c
= &loadcmds
[nloadcmds
++];
909 c
->mapstart
= ph
->p_vaddr
& ~(ph
->p_align
- 1);
910 c
->mapend
= ((ph
->p_vaddr
+ ph
->p_filesz
+ _dl_pagesize
- 1)
911 & ~(_dl_pagesize
- 1));
912 c
->dataend
= ph
->p_vaddr
+ ph
->p_filesz
;
913 c
->allocend
= ph
->p_vaddr
+ ph
->p_memsz
;
914 c
->mapoff
= ph
->p_offset
& ~(ph
->p_align
- 1);
916 /* Optimize a common case. */
917 #if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7
918 c
->prot
= (PF_TO_PROT
919 >> ((ph
->p_flags
& (PF_R
| PF_W
| PF_X
)) * 4)) & 0xf;
922 if (ph
->p_flags
& PF_R
)
923 c
->prot
|= PROT_READ
;
924 if (ph
->p_flags
& PF_W
)
925 c
->prot
|= PROT_WRITE
;
926 if (ph
->p_flags
& PF_X
)
927 c
->prot
|= PROT_EXEC
;
933 /* Now process the load commands and map segments into memory. */
936 /* Length of the sections to be loaded. */
937 maplength
= loadcmds
[nloadcmds
- 1].allocend
- c
->mapstart
;
939 if (__builtin_expect (type
, ET_DYN
) == ET_DYN
)
941 /* This is a position-independent shared object. We can let the
942 kernel map it anywhere it likes, but we must have space for all
943 the segments in their specified positions relative to the first.
944 So we map the first segment without MAP_FIXED, but with its
945 extent increased to cover all the segments. Then we remove
946 access from excess portion, and there is known sufficient space
947 there to remap from the later segments.
949 As a refinement, sometimes we have an address that we would
950 prefer to map such objects at; but this is only a preference,
951 the OS can do whatever it likes. */
953 mappref
= (ELF_PREFERRED_ADDRESS (loader
, maplength
, c
->mapstart
)
954 - MAP_BASE_ADDR (l
));
956 /* Remember which part of the address space this object uses. */
957 l
->l_map_start
= (ElfW(Addr
)) __mmap ((void *) mappref
, maplength
,
958 c
->prot
, MAP_COPY
| MAP_FILE
,
960 if ((void *) l
->l_map_start
== MAP_FAILED
)
963 errstring
= N_("failed to map segment from shared object");
964 goto call_lose_errno
;
967 l
->l_map_end
= l
->l_map_start
+ maplength
;
968 l
->l_addr
= l
->l_map_start
- c
->mapstart
;
970 /* Change protection on the excess portion to disallow all access;
971 the portions we do not remap later will be inaccessible as if
972 unallocated. Then jump into the normal segment-mapping loop to
973 handle the portion of the segment past the end of the file
975 __mprotect ((caddr_t
) (l
->l_addr
+ c
->mapend
),
976 loadcmds
[nloadcmds
- 1].allocend
- c
->mapend
,
983 /* This object is loaded at a fixed address. This must never
984 happen for objects loaded with dlopen(). */
985 if (__builtin_expect (mode
& __RTLD_DLOPEN
, 0))
987 errstring
= N_("cannot dynamically load executable");
991 /* Notify ELF_PREFERRED_ADDRESS that we have to load this one
993 ELF_FIXED_ADDRESS (loader
, c
->mapstart
);
996 /* Remember which part of the address space this object uses. */
997 l
->l_map_start
= c
->mapstart
+ l
->l_addr
;
998 l
->l_map_end
= l
->l_map_start
+ maplength
;
1000 while (c
< &loadcmds
[nloadcmds
])
1002 if (c
->mapend
> c
->mapstart
1003 /* Map the segment contents from the file. */
1004 && (__mmap ((void *) (l
->l_addr
+ c
->mapstart
),
1005 c
->mapend
- c
->mapstart
, c
->prot
,
1006 MAP_FIXED
| MAP_COPY
| MAP_FILE
, fd
, c
->mapoff
)
1012 && c
->mapoff
<= header
->e_phoff
1013 && (c
->mapend
- c
->mapstart
+ c
->mapoff
1014 >= header
->e_phoff
+ header
->e_phnum
* sizeof (ElfW(Phdr
))))
1015 /* Found the program header in this segment. */
1016 l
->l_phdr
= (void *) (c
->mapstart
+ header
->e_phoff
- c
->mapoff
);
1018 if (c
->allocend
> c
->dataend
)
1020 /* Extra zero pages should appear at the end of this segment,
1021 after the data mapped from the file. */
1022 ElfW(Addr
) zero
, zeroend
, zeropage
;
1024 zero
= l
->l_addr
+ c
->dataend
;
1025 zeroend
= l
->l_addr
+ c
->allocend
;
1026 zeropage
= (zero
+ _dl_pagesize
- 1) & ~(_dl_pagesize
- 1);
1028 if (zeroend
< zeropage
)
1029 /* All the extra data is in the last page of the segment.
1030 We can just zero it. */
1033 if (zeropage
> zero
)
1035 /* Zero the final part of the last page of the segment. */
1036 if ((c
->prot
& PROT_WRITE
) == 0)
1039 if (__mprotect ((caddr_t
) (zero
& ~(_dl_pagesize
- 1)),
1040 _dl_pagesize
, c
->prot
|PROT_WRITE
) < 0)
1042 errstring
= N_("cannot change memory protections");
1043 goto call_lose_errno
;
1046 memset ((void *) zero
, '\0', zeropage
- zero
);
1047 if ((c
->prot
& PROT_WRITE
) == 0)
1048 __mprotect ((caddr_t
) (zero
& ~(_dl_pagesize
- 1)),
1049 _dl_pagesize
, c
->prot
);
1052 if (zeroend
> zeropage
)
1054 /* Map the remaining zero pages in from the zero fill FD. */
1056 mapat
= __mmap ((caddr_t
) zeropage
, zeroend
- zeropage
,
1057 c
->prot
, MAP_ANON
|MAP_PRIVATE
|MAP_FIXED
,
1059 if (mapat
== MAP_FAILED
)
1061 errstring
= N_("cannot map zero-fill pages");
1062 goto call_lose_errno
;
1070 if (l
->l_phdr
== NULL
)
1072 /* The program header is not contained in any of the segments.
1073 We have to allocate memory ourself and copy it over from
1074 out temporary place. */
1075 ElfW(Phdr
) *newp
= (ElfW(Phdr
) *) malloc (header
->e_phnum
1076 * sizeof (ElfW(Phdr
)));
1079 errstring
= N_("cannot allocate memory for program header");
1080 goto call_lose_errno
;
1083 l
->l_phdr
= memcpy (newp
, phdr
,
1084 (header
->e_phnum
* sizeof (ElfW(Phdr
))));
1085 l
->l_phdr_allocated
= 1;
1088 /* Adjust the PT_PHDR value by the runtime load address. */
1089 (ElfW(Addr
)) l
->l_phdr
+= l
->l_addr
;
1092 /* We are done mapping in the file. We no longer need the descriptor. */
1094 /* Signal that we closed the file. */
1097 if (l
->l_type
== lt_library
&& type
== ET_EXEC
)
1098 l
->l_type
= lt_executable
;
1104 errstring
= N_("object file has no dynamic section");
1109 (ElfW(Addr
)) l
->l_ld
+= l
->l_addr
;
1111 l
->l_entry
+= l
->l_addr
;
1113 if (__builtin_expect (_dl_debug_mask
& DL_DEBUG_FILES
, 0))
1114 _dl_debug_printf (" dynamic: 0x%0*lx base: 0x%0*lx size: 0x%0*Zx\n"
1115 " entry: 0x%0*lx phdr: 0x%0*lx phnum: %*u\n\n",
1116 (int) sizeof (void *) * 2, (unsigned long int) l
->l_ld
,
1117 (int) sizeof (void *) * 2, (unsigned long int) l
->l_addr
,
1118 (int) sizeof (void *) * 2, maplength
,
1119 (int) sizeof (void *) * 2, (unsigned long int) l
->l_entry
,
1120 (int) sizeof (void *) * 2, (unsigned long int) l
->l_phdr
,
1121 (int) sizeof (void *) * 2, l
->l_phnum
);
1123 elf_get_dynamic_info (l
);
1125 /* Make sure we are dlopen()ing an object which has the DF_1_NOOPEN
1127 if (__builtin_expect (l
->l_flags_1
& DF_1_NOOPEN
, 0)
1128 && (mode
& __RTLD_DLOPEN
))
1130 /* We are not supposed to load this object. Free all resources. */
1131 __munmap ((void *) l
->l_map_start
, l
->l_map_end
- l
->l_map_start
);
1133 if (!l
->l_libname
->dont_free
)
1134 free (l
->l_libname
);
1136 if (l
->l_phdr_allocated
)
1137 free ((void *) l
->l_phdr
);
1139 errstring
= N_("shared object cannot be dlopen()ed");
1143 if (l
->l_info
[DT_HASH
])
1146 /* If this object has DT_SYMBOLIC set modify now its scope. We don't
1147 have to do this for the main map. */
1148 if (__builtin_expect (l
->l_info
[DT_SYMBOLIC
] != NULL
, 0)
1149 && &l
->l_searchlist
!= l
->l_scope
[0])
1151 /* Create an appropriate searchlist. It contains only this map.
1153 XXX This is the definition of DT_SYMBOLIC in SysVr4. The old
1154 GNU ld.so implementation had a different interpretation which
1155 is more reasonable. We are prepared to add this possibility
1156 back as part of a GNU extension of the ELF format. */
1157 l
->l_symbolic_searchlist
.r_list
=
1158 (struct link_map
**) malloc (sizeof (struct link_map
*));
1160 if (l
->l_symbolic_searchlist
.r_list
== NULL
)
1162 errstring
= N_("cannot create searchlist");
1163 goto call_lose_errno
;
1166 l
->l_symbolic_searchlist
.r_list
[0] = l
;
1167 l
->l_symbolic_searchlist
.r_nlist
= 1;
1169 /* Now move the existing entries one back. */
1170 memmove (&l
->l_scope
[1], &l
->l_scope
[0],
1171 (l
->l_scope_max
- 1) * sizeof (l
->l_scope
[0]));
1173 /* Now add the new entry. */
1174 l
->l_scope
[0] = &l
->l_symbolic_searchlist
;
1177 /* Remember whether this object must be initialized first. */
1178 if (l
->l_flags_1
& DF_1_INITFIRST
)
1181 /* Finally the file information. */
1182 l
->l_dev
= st
.st_dev
;
1183 l
->l_ino
= st
.st_ino
;
1188 /* Print search path. */
1190 print_search_path (struct r_search_path_elem
**list
,
1191 const char *what
, const char *name
)
1193 char buf
[max_dirnamelen
+ max_capstrlen
];
1196 _dl_debug_printf (" search path=");
1198 while (*list
!= NULL
&& (*list
)->what
== what
) /* Yes, ==. */
1200 char *endp
= __mempcpy (buf
, (*list
)->dirname
, (*list
)->dirnamelen
);
1203 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
1204 if ((*list
)->status
[cnt
] != nonexisting
)
1206 char *cp
= __mempcpy (endp
, capstr
[cnt
].str
, capstr
[cnt
].len
);
1207 if (cp
== buf
|| (cp
== buf
+ 1 && buf
[0] == '/'))
1212 _dl_debug_printf_c (first
? "%s" : ":%s", buf
);
1220 _dl_debug_printf_c ("\t\t(%s from file %s)\n", what
,
1221 name
[0] ? name
: _dl_argv
[0]);
1223 _dl_debug_printf_c ("\t\t(%s)\n", what
);
1226 /* Open a file and verify it is an ELF file for this architecture. We
1227 ignore only ELF files for other architectures. Non-ELF files and
1228 ELF files with different header information cause fatal errors since
1229 this could mean there is something wrong in the installation and the
1230 user might want to know about this. */
1232 open_verify (const char *name
, struct filebuf
*fbp
)
1234 /* This is the expected ELF header. */
1235 #define ELF32_CLASS ELFCLASS32
1236 #define ELF64_CLASS ELFCLASS64
1237 #ifndef VALID_ELF_HEADER
1238 # define VALID_ELF_HEADER(hdr,exp,size) (memcmp (hdr, exp, size) == 0)
1239 # define VALID_ELF_OSABI(osabi) (osabi == ELFOSABI_SYSV)
1240 # define VALID_ELF_ABIVERSION(ver) (ver == 0)
1242 static const unsigned char expected
[EI_PAD
] =
1244 [EI_MAG0
] = ELFMAG0
,
1245 [EI_MAG1
] = ELFMAG1
,
1246 [EI_MAG2
] = ELFMAG2
,
1247 [EI_MAG3
] = ELFMAG3
,
1248 [EI_CLASS
] = ELFW(CLASS
),
1249 [EI_DATA
] = byteorder
,
1250 [EI_VERSION
] = EV_CURRENT
,
1251 [EI_OSABI
] = ELFOSABI_SYSV
,
1256 ElfW(Word
) vendorlen
;
1260 } expected_note
= { 4, 16, 1, "GNU" };
1262 /* Initialize it to make the compiler happy. */
1263 const char *errstring
= NULL
;
1266 /* Open the file. We always open files read-only. */
1267 fd
= __open (name
, O_RDONLY
);
1271 ElfW(Phdr
) *phdr
, *ph
;
1272 ElfW(Word
) *abi_note
, abi_note_buf
[8];
1273 unsigned int osversion
;
1276 /* We successfully openened the file. Now verify it is a file
1279 fbp
->len
= __libc_read (fd
, fbp
->buf
, sizeof (fbp
->buf
));
1281 /* This is where the ELF header is loaded. */
1282 assert (sizeof (fbp
->buf
) > sizeof (ElfW(Ehdr
)));
1283 ehdr
= (ElfW(Ehdr
) *) fbp
->buf
;
1285 /* Now run the tests. */
1286 if (__builtin_expect (fbp
->len
< (ssize_t
) sizeof (ElfW(Ehdr
)), 0))
1289 errstring
= (errval
== 0
1290 ? N_("file too short") : N_("cannot read file data"));
1292 lose (errval
, fd
, name
, NULL
, NULL
, errstring
);
1295 /* See whether the ELF header is what we expect. */
1296 if (__builtin_expect (! VALID_ELF_HEADER (ehdr
->e_ident
, expected
,
1299 /* Something is wrong. */
1300 if (*(Elf32_Word
*) &ehdr
->e_ident
!=
1301 #if BYTE_ORDER == LITTLE_ENDIAN
1302 ((ELFMAG0
<< (EI_MAG0
* 8)) |
1303 (ELFMAG1
<< (EI_MAG1
* 8)) |
1304 (ELFMAG2
<< (EI_MAG2
* 8)) |
1305 (ELFMAG3
<< (EI_MAG3
* 8)))
1307 ((ELFMAG0
<< (EI_MAG3
* 8)) |
1308 (ELFMAG1
<< (EI_MAG2
* 8)) |
1309 (ELFMAG2
<< (EI_MAG1
* 8)) |
1310 (ELFMAG3
<< (EI_MAG0
* 8)))
1313 errstring
= N_("invalid ELF header");
1314 else if (ehdr
->e_ident
[EI_CLASS
] != ELFW(CLASS
))
1315 /* This is not a fatal error. On architectures where
1316 32-bit and 64-bit binaries can be run this might
1319 else if (ehdr
->e_ident
[EI_DATA
] != byteorder
)
1321 if (BYTE_ORDER
== BIG_ENDIAN
)
1322 errstring
= N_("ELF file data encoding not big-endian");
1324 errstring
= N_("ELF file data encoding not little-endian");
1326 else if (ehdr
->e_ident
[EI_VERSION
] != EV_CURRENT
)
1328 = N_("ELF file version ident does not match current one");
1329 /* XXX We should be able so set system specific versions which are
1331 else if (!VALID_ELF_OSABI (ehdr
->e_ident
[EI_OSABI
]))
1332 errstring
= N_("ELF file OS ABI invalid");
1333 else if (!VALID_ELF_ABIVERSION (ehdr
->e_ident
[EI_ABIVERSION
]))
1334 errstring
= N_("ELF file ABI version invalid");
1336 /* Otherwise we don't know what went wrong. */
1337 errstring
= N_("internal error");
1342 if (__builtin_expect (ehdr
->e_version
, EV_CURRENT
) != EV_CURRENT
)
1344 errstring
= N_("ELF file version does not match current one");
1347 if (! __builtin_expect (elf_machine_matches_host (ehdr
), 1))
1349 else if (__builtin_expect (ehdr
->e_phentsize
, sizeof (ElfW(Phdr
)))
1350 != sizeof (ElfW(Phdr
)))
1352 errstring
= N_("ELF file's phentsize not the expected size");
1355 else if (__builtin_expect (ehdr
->e_type
, ET_DYN
) != ET_DYN
1356 && __builtin_expect (ehdr
->e_type
, ET_EXEC
) != ET_EXEC
)
1358 errstring
= N_("only ET_DYN and ET_EXEC can be loaded");
1362 maplength
= ehdr
->e_phnum
* sizeof (ElfW(Phdr
));
1363 if (ehdr
->e_phoff
+ maplength
<= fbp
->len
)
1364 phdr
= (void *) (fbp
->buf
+ ehdr
->e_phoff
);
1367 phdr
= alloca (maplength
);
1368 __lseek (fd
, SEEK_SET
, ehdr
->e_phoff
);
1369 if (__libc_read (fd
, (void *) phdr
, maplength
) != maplength
)
1373 errstring
= N_("cannot read file data");
1378 /* Check .note.ABI-tag if present. */
1379 for (ph
= phdr
; ph
< &phdr
[ehdr
->e_phnum
]; ++ph
)
1380 if (ph
->p_type
== PT_NOTE
&& ph
->p_filesz
== 32 && ph
->p_align
>= 4)
1382 if (ph
->p_offset
+ 32 <= fbp
->len
)
1383 abi_note
= (void *) (fbp
->buf
+ ph
->p_offset
);
1386 __lseek (fd
, SEEK_SET
, ph
->p_offset
);
1387 if (__libc_read (fd
, (void *) abi_note_buf
, 32) != 32)
1390 abi_note
= abi_note_buf
;
1393 if (memcmp (abi_note
, &expected_note
, sizeof (expected_note
)))
1396 osversion
= (abi_note
[5] & 0xff) * 65536
1397 + (abi_note
[6] & 0xff) * 256
1398 + (abi_note
[7] & 0xff);
1399 if (abi_note
[4] != __ABI_TAG_OS
1400 || (_dl_osversion
&& _dl_osversion
< osversion
))
1404 __set_errno (ENOENT
);
1415 /* Try to open NAME in one of the directories in *DIRSP.
1416 Return the fd, or -1. If successful, fill in *REALNAME
1417 with the malloc'd full directory name. If it turns out
1418 that none of the directories in *DIRSP exists, *DIRSP is
1419 replaced with (void *) -1, and the old value is free()d
1420 if MAY_FREE_DIRS is true. */
1423 open_path (const char *name
, size_t namelen
, int preloaded
,
1424 struct r_search_path_struct
*sps
, char **realname
,
1425 struct filebuf
*fbp
)
1427 struct r_search_path_elem
**dirs
= sps
->dirs
;
1430 const char *current_what
= NULL
;
1433 buf
= alloca (max_dirnamelen
+ max_capstrlen
+ namelen
);
1436 struct r_search_path_elem
*this_dir
= *dirs
;
1443 /* If we are debugging the search for libraries print the path
1444 now if it hasn't happened now. */
1445 if (__builtin_expect (_dl_debug_mask
& DL_DEBUG_LIBS
, 0)
1446 && current_what
!= this_dir
->what
)
1448 current_what
= this_dir
->what
;
1449 print_search_path (dirs
, current_what
, this_dir
->where
);
1452 edp
= (char *) __mempcpy (buf
, this_dir
->dirname
, this_dir
->dirnamelen
);
1453 for (cnt
= 0; fd
== -1 && cnt
< ncapstr
; ++cnt
)
1455 /* Skip this directory if we know it does not exist. */
1456 if (this_dir
->status
[cnt
] == nonexisting
)
1460 ((char *) __mempcpy (__mempcpy (edp
,
1461 capstr
[cnt
].str
, capstr
[cnt
].len
),
1465 /* Print name we try if this is wanted. */
1466 if (__builtin_expect (_dl_debug_mask
& DL_DEBUG_LIBS
, 0))
1467 _dl_debug_printf (" trying file=%s\n", buf
);
1469 fd
= open_verify (buf
, fbp
);
1470 if (this_dir
->status
[cnt
] == unknown
)
1473 this_dir
->status
[cnt
] = existing
;
1476 /* We failed to open machine dependent library. Let's
1477 test whether there is any directory at all. */
1480 buf
[buflen
- namelen
- 1] = '\0';
1482 if (__xstat64 (_STAT_VER
, buf
, &st
) != 0
1483 || ! S_ISDIR (st
.st_mode
))
1484 /* The directory does not exist or it is no directory. */
1485 this_dir
->status
[cnt
] = nonexisting
;
1487 this_dir
->status
[cnt
] = existing
;
1491 /* Remember whether we found any existing directory. */
1492 here_any
|= this_dir
->status
[cnt
] == existing
;
1494 if (fd
!= -1 && __builtin_expect (preloaded
, 0)
1495 && __libc_enable_secure
)
1497 /* This is an extra security effort to make sure nobody can
1498 preload broken shared objects which are in the trusted
1499 directories and so exploit the bugs. */
1502 if (__fxstat64 (_STAT_VER
, fd
, &st
) != 0
1503 || (st
.st_mode
& S_ISUID
) == 0)
1505 /* The shared object cannot be tested for being SUID
1506 or this bit is not set. In this case we must not
1510 /* We simply ignore the file, signal this by setting
1511 the error value which would have been set by `open'. */
1519 *realname
= (char *) malloc (buflen
);
1520 if (*realname
!= NULL
)
1522 memcpy (*realname
, buf
, buflen
);
1527 /* No memory for the name, we certainly won't be able
1528 to load and link it. */
1533 if (here_any
&& (err
= errno
) != ENOENT
&& err
!= EACCES
)
1534 /* The file exists and is readable, but something went wrong. */
1537 /* Remember whether we found anything. */
1540 while (*++dirs
!= NULL
);
1542 /* Remove the whole path if none of the directories exists. */
1543 if (__builtin_expect (! any
, 0))
1545 /* Paths which were allocated using the minimal malloc() in ld.so
1546 must not be freed using the general free() in libc. */
1549 sps
->dirs
= (void *) -1;
1555 /* Map in the shared object file NAME. */
1559 _dl_map_object (struct link_map
*loader
, const char *name
, int preloaded
,
1560 int type
, int trace_mode
, int mode
)
1568 /* Look for this name among those already loaded. */
1569 for (l
= _dl_loaded
; l
; l
= l
->l_next
)
1571 /* If the requested name matches the soname of a loaded object,
1572 use that object. Elide this check for names that have not
1574 if (__builtin_expect (l
->l_faked
, 0) != 0)
1576 if (!_dl_name_match_p (name
, l
))
1580 if (__builtin_expect (l
->l_soname_added
, 1)
1581 || l
->l_info
[DT_SONAME
] == NULL
)
1584 soname
= ((const char *) D_PTR (l
, l_info
[DT_STRTAB
])
1585 + l
->l_info
[DT_SONAME
]->d_un
.d_val
);
1586 if (strcmp (name
, soname
) != 0)
1589 /* We have a match on a new name -- cache it. */
1590 add_name_to_object (l
, soname
);
1591 l
->l_soname_added
= 1;
1594 /* We have a match. */
1598 /* Display information if we are debugging. */
1599 if (__builtin_expect (_dl_debug_mask
& DL_DEBUG_FILES
, 0) && loader
!= NULL
)
1600 _dl_debug_printf ("\nfile=%s; needed by %s\n", name
,
1601 loader
->l_name
[0] ? loader
->l_name
: _dl_argv
[0]);
1603 if (strchr (name
, '/') == NULL
)
1605 /* Search for NAME in several places. */
1607 size_t namelen
= strlen (name
) + 1;
1609 if (__builtin_expect (_dl_debug_mask
& DL_DEBUG_LIBS
, 0))
1610 _dl_debug_printf ("find library=%s; searching\n", name
);
1614 /* When the object has the RUNPATH information we don't use any
1616 if (loader
== NULL
|| loader
->l_info
[DT_RUNPATH
] == NULL
)
1618 /* First try the DT_RPATH of the dependent object that caused NAME
1619 to be loaded. Then that object's dependent, and on up. */
1620 for (l
= loader
; fd
== -1 && l
; l
= l
->l_loader
)
1622 if (l
->l_rpath_dirs
.dirs
== NULL
)
1624 if (l
->l_info
[DT_RPATH
] == NULL
)
1626 /* There is no path. */
1627 l
->l_rpath_dirs
.dirs
= (void *) -1;
1632 /* Make sure the cache information is available. */
1633 size_t ptrval
= (D_PTR (l
, l_info
[DT_STRTAB
])
1634 + l
->l_info
[DT_RPATH
]->d_un
.d_val
);
1635 decompose_rpath (&l
->l_rpath_dirs
,
1636 (const char *) ptrval
, l
, "RPATH");
1640 if (l
->l_rpath_dirs
.dirs
!= (void *) -1)
1641 fd
= open_path (name
, namelen
, preloaded
, &l
->l_rpath_dirs
,
1645 /* If dynamically linked, try the DT_RPATH of the executable
1648 if (fd
== -1 && l
&& l
->l_type
!= lt_loaded
&& l
!= loader
1649 && l
->l_rpath_dirs
.dirs
!= (void *) -1)
1650 fd
= open_path (name
, namelen
, preloaded
, &l
->l_rpath_dirs
,
1654 /* Try the LD_LIBRARY_PATH environment variable. */
1655 if (fd
== -1 && env_path_list
.dirs
!= (void *) -1)
1656 fd
= open_path (name
, namelen
, preloaded
, &env_path_list
,
1659 /* Look at the RUNPATH information for this binary.
1661 Note that this is no real loop. 'while' is used only to enable
1662 us to use 'break' instead of a 'goto' to jump to the end. The
1663 loop is always left after the first round. */
1664 while (fd
== -1 && loader
!= NULL
1665 && loader
->l_runpath_dirs
.dirs
!= (void *) -1)
1667 if (loader
->l_runpath_dirs
.dirs
== NULL
)
1669 if (loader
->l_info
[DT_RUNPATH
] == NULL
)
1672 loader
->l_runpath_dirs
.dirs
= (void *) -1;
1677 /* Make sure the cache information is available. */
1678 size_t ptrval
= (D_PTR (loader
, l_info
[DT_STRTAB
])
1679 + loader
->l_info
[DT_RUNPATH
]->d_un
.d_val
);
1680 decompose_rpath (&loader
->l_runpath_dirs
,
1681 (const char *) ptrval
, loader
, "RUNPATH");
1685 if (loader
->l_runpath_dirs
.dirs
!= (void *) -1)
1686 fd
= open_path (name
, namelen
, preloaded
,
1687 &loader
->l_runpath_dirs
, &realname
, &fb
);
1692 && (__builtin_expect (! preloaded
, 1) || ! __libc_enable_secure
))
1694 /* Check the list of libraries in the file /etc/ld.so.cache,
1695 for compatibility with Linux's ldconfig program. */
1696 const char *cached
= _dl_load_cache_lookup (name
);
1701 l
= loader
?: _dl_loaded
;
1706 /* If the loader has the DF_1_NODEFLIB flag set we must not
1707 use a cache entry from any of these directories. */
1710 /* 'l' is always != NULL for dynamically linked objects. */
1713 __builtin_expect (l
->l_flags_1
& DF_1_NODEFLIB
, 0))
1715 const char *dirp
= system_dirs
;
1716 unsigned int cnt
= 0;
1720 if (memcmp (cached
, dirp
, system_dirs_len
[cnt
]) == 0)
1722 /* The prefix matches. Don't use the entry. */
1727 dirp
+= system_dirs_len
[cnt
] + 1;
1730 while (cnt
< nsystem_dirs_len
);
1735 fd
= open_verify (cached
, &fb
);
1736 if (__builtin_expect (fd
!= -1, 1))
1738 realname
= local_strdup (cached
);
1739 if (realname
== NULL
)
1749 /* Finally, try the default path. */
1751 && ((l
= loader
?: _dl_loaded
)
1752 /* 'l' is always != NULL for dynamically linked objects. */
1758 __builtin_expect (!(l
->l_flags_1
& DF_1_NODEFLIB
), 1))
1759 && rtld_search_dirs
.dirs
!= (void *) -1)
1760 fd
= open_path (name
, namelen
, preloaded
, &rtld_search_dirs
,
1763 /* Add another newline when we a tracing the library loading. */
1764 if (__builtin_expect (_dl_debug_mask
& DL_DEBUG_LIBS
, 0))
1765 _dl_debug_printf ("\n");
1769 /* The path may contain dynamic string tokens. */
1771 ? expand_dynamic_string_token (loader
, name
)
1772 : local_strdup (name
));
1773 if (realname
== NULL
)
1777 fd
= open_verify (realname
, &fb
);
1778 if (__builtin_expect (fd
, 0) == -1)
1783 if (__builtin_expect (fd
, 0) == -1)
1787 /* We haven't found an appropriate library. But since we
1788 are only interested in the list of libraries this isn't
1789 so severe. Fake an entry with all the information we
1791 static const Elf_Symndx dummy_bucket
= STN_UNDEF
;
1793 /* Enter the new object in the list of loaded objects. */
1794 if ((name_copy
= local_strdup (name
)) == NULL
1795 || (l
= _dl_new_object (name_copy
, name
, type
, loader
)) == NULL
)
1796 _dl_signal_error (ENOMEM
, name
, NULL
,
1797 N_("cannot create shared object descriptor"));
1798 /* Signal that this is a faked entry. */
1800 /* Since the descriptor is initialized with zero we do not
1802 l->l_reserved = 0; */
1803 l
->l_buckets
= &dummy_bucket
;
1810 _dl_signal_error (errno
, name
, NULL
,
1811 N_("cannot open shared object file"));
1814 return _dl_map_object_from_fd (name
, fd
, &fb
, realname
, loader
, type
, mode
);