1 /* Map in a shared object's segments from the file.
2 Copyright (C) 1995,96,97,98,99,2000,2001,2002 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
30 #include <sys/param.h>
32 #include <sys/types.h>
33 #include "dynamic-link.h"
35 #include <dl-osinfo.h>
39 /* On some systems, no flag bits are given to specify file mapping. */
44 /* The right way to map in the shared library files is MAP_COPY, which
45 makes a virtual copy of the data at the time of the mmap call; this
46 guarantees the mapped pages will be consistent even if the file is
47 overwritten. Some losing VM systems like Linux's lack MAP_COPY. All we
48 get is MAP_PRIVATE, which copies each page when it is modified; this
49 means if the file is overwritten, we may at some point get some pages
50 from the new version after starting with pages from the old version. */
52 # define MAP_COPY MAP_PRIVATE
55 /* Some systems link their relocatable objects for another base address
56 than 0. We want to know the base address for these such that we can
57 subtract this address from the segment addresses during mapping.
58 This results in a more efficient address space usage. Defaults to
59 zero for almost all systems. */
61 # define MAP_BASE_ADDR(l) 0
66 #if BYTE_ORDER == BIG_ENDIAN
67 # define byteorder ELFDATA2MSB
68 #elif BYTE_ORDER == LITTLE_ENDIAN
69 # define byteorder ELFDATA2LSB
71 # error "Unknown BYTE_ORDER " BYTE_ORDER
72 # define byteorder ELFDATANONE
75 #define STRING(x) __STRING (x)
78 /* The fd is not examined when using MAP_ANON. */
82 # define ANONFD _dl_zerofd
85 /* Handle situations where we have a preferred location in memory for
86 the shared objects. */
87 #ifdef ELF_PREFERRED_ADDRESS_DATA
88 ELF_PREFERRED_ADDRESS_DATA
;
90 #ifndef ELF_PREFERRED_ADDRESS
91 # define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
93 #ifndef ELF_FIXED_ADDRESS
94 # define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
97 /* Type for the buffer we put the ELF header and hopefully the program
98 header. This buffer does not really have to be too large. In most
99 cases the program header follows the ELF header directly. If this
100 is not the case all bets are off and we can make the header arbitrarily
101 large and still won't get it read. This means the only question is
102 how large are the ELF and program header combined. The ELF header
103 in 64-bit files is 56 bytes long. Each program header entry is again
104 56 bytes long. I.e., even with a file which has 17 program header
105 entries we only have to read 1kB. And 17 program header entries is
106 plenty, normal files have < 10. If this heuristic should really fail
107 for some file the code in `_dl_map_object_from_fd' knows how to
115 /* This is the decomposed LD_LIBRARY_PATH search path. */
116 static struct r_search_path_struct env_path_list
;
118 /* List of the hardware capabilities we might end up using. */
119 static const struct r_strlenpair
*capstr
;
120 static size_t ncapstr
;
121 static size_t max_capstrlen
;
124 /* Get the generated information about the trusted directories. */
125 #include "trusted-dirs.h"
127 static const char system_dirs
[] = SYSTEM_DIRS
;
128 static const size_t system_dirs_len
[] =
132 #define nsystem_dirs_len \
133 (sizeof (system_dirs_len) / sizeof (system_dirs_len[0]))
136 /* Local version of `strdup' function. */
138 local_strdup (const char *s
)
140 size_t len
= strlen (s
) + 1;
141 void *new = malloc (len
);
146 return (char *) memcpy (new, s
, len
);
151 is_dst (const char *start
, const char *name
, const char *str
,
152 int is_path
, int secure
)
155 bool is_curly
= false;
164 while (name
[len
] == str
[len
] && name
[len
] != '\0')
169 if (name
[len
] != '}')
172 /* Point again at the beginning of the name. */
174 /* Skip over closing curly brace and adjust for the --name. */
177 else if (name
[len
] != '\0' && name
[len
] != '/'
178 && (!is_path
|| name
[len
] != ':'))
181 if (__builtin_expect (secure
, 0)
182 && ((name
[len
] != '\0' && (!is_path
|| name
[len
] != ':'))
183 || (name
!= start
+ 1 && (!is_path
|| name
[-2] != ':'))))
191 _dl_dst_count (const char *name
, int is_path
)
193 const char *const start
= name
;
200 /* $ORIGIN is not expanded for SUID/GUID programs (except if it
201 is $ORIGIN alone) and it must always appear first in path. */
203 if ((len
= is_dst (start
, name
, "ORIGIN", is_path
,
204 INTUSE(__libc_enable_secure
))) != 0
205 || (len
= is_dst (start
, name
, "PLATFORM", is_path
, 0)) != 0
206 || (len
= is_dst (start
, name
, "LIB", is_path
, 0)) != 0)
209 name
= strchr (name
+ len
, '$');
211 while (name
!= NULL
);
215 INTDEF (_dl_dst_count
)
219 _dl_dst_substitute (struct link_map
*l
, const char *name
, char *result
,
222 const char *const start
= name
;
223 char *last_elem
, *wp
;
225 /* Now fill the result path. While copying over the string we keep
226 track of the start of the last path element. When we come accross
227 a DST we copy over the value or (if the value is not available)
228 leave the entire path element out. */
229 last_elem
= wp
= result
;
233 if (__builtin_expect (*name
== '$', 0))
235 const char *repl
= NULL
;
239 if ((len
= is_dst (start
, name
, "ORIGIN", is_path
,
240 INTUSE(__libc_enable_secure
))) != 0)
242 else if ((len
= is_dst (start
, name
, "PLATFORM", is_path
, 0)) != 0)
243 repl
= GL(dl_platform
);
244 else if ((len
= is_dst (start
, name
, "LIB", is_path
, 0)) != 0)
247 if (repl
!= NULL
&& repl
!= (const char *) -1)
249 wp
= __stpcpy (wp
, repl
);
254 /* We cannot use this path element, the value of the
255 replacement is unknown. */
258 while (*name
!= '\0' && (!is_path
|| *name
!= ':'))
262 /* No DST we recognize. */
268 if (is_path
&& *name
== ':')
272 while (*name
!= '\0');
278 INTDEF (_dl_dst_substitute
)
281 /* Return copy of argument with all recognized dynamic string tokens
282 ($ORIGIN and $PLATFORM for now) replaced. On some platforms it
283 might not be possible to determine the path from which the object
284 belonging to the map is loaded. In this case the path element
285 containing $ORIGIN is left out. */
287 expand_dynamic_string_token (struct link_map
*l
, const char *s
)
289 /* We make two runs over the string. First we determine how large the
290 resulting string is and then we copy it over. Since this is now
291 frequently executed operation we are looking here not for performance
292 but rather for code size. */
297 /* Determine the number of DST elements. */
298 cnt
= DL_DST_COUNT (s
, 1);
300 /* If we do not have to replace anything simply copy the string. */
301 if (__builtin_expect (cnt
, 0) == 0)
302 return local_strdup (s
);
304 /* Determine the length of the substituted string. */
305 total
= DL_DST_REQUIRED (l
, s
, strlen (s
), cnt
);
307 /* Allocate the necessary memory. */
308 result
= (char *) malloc (total
+ 1);
312 return INTUSE(_dl_dst_substitute
) (l
, s
, result
, 1);
316 /* Add `name' to the list of names for a particular shared object.
317 `name' is expected to have been allocated with malloc and will
318 be freed if the shared object already has this name.
319 Returns false if the object already had this name. */
322 add_name_to_object (struct link_map
*l
, const char *name
)
324 struct libname_list
*lnp
, *lastp
;
325 struct libname_list
*newname
;
329 for (lnp
= l
->l_libname
; lnp
!= NULL
; lastp
= lnp
, lnp
= lnp
->next
)
330 if (strcmp (name
, lnp
->name
) == 0)
333 name_len
= strlen (name
) + 1;
334 newname
= (struct libname_list
*) malloc (sizeof *newname
+ name_len
);
337 /* No more memory. */
338 INTUSE(_dl_signal_error
) (ENOMEM
, name
, NULL
,
339 N_("cannot allocate name record"));
342 /* The object should have a libname set from _dl_new_object. */
343 assert (lastp
!= NULL
);
345 newname
->name
= memcpy (newname
+ 1, name
, name_len
);
346 newname
->next
= NULL
;
347 newname
->dont_free
= 0;
348 lastp
->next
= newname
;
351 /* Standard search directories. */
352 static struct r_search_path_struct rtld_search_dirs
;
354 static size_t max_dirnamelen
;
356 static inline struct r_search_path_elem
**
357 fillin_rpath (char *rpath
, struct r_search_path_elem
**result
, const char *sep
,
358 int check_trusted
, const char *what
, const char *where
)
363 while ((cp
= __strsep (&rpath
, sep
)) != NULL
)
365 struct r_search_path_elem
*dirp
;
366 size_t len
= strlen (cp
);
368 /* `strsep' can pass an empty string. This has to be
369 interpreted as `use the current directory'. */
372 static const char curwd
[] = "./";
376 /* Remove trailing slashes (except for "/"). */
377 while (len
> 1 && cp
[len
- 1] == '/')
380 /* Now add one if there is none so far. */
381 if (len
> 0 && cp
[len
- 1] != '/')
384 /* Make sure we don't use untrusted directories if we run SUID. */
385 if (__builtin_expect (check_trusted
, 0))
387 const char *trun
= system_dirs
;
391 /* All trusted directories must be complete names. */
394 for (idx
= 0; idx
< nsystem_dirs_len
; ++idx
)
396 if (len
== system_dirs_len
[idx
]
397 && memcmp (trun
, cp
, len
) == 0)
404 trun
+= system_dirs_len
[idx
] + 1;
409 /* Simply drop this directory. */
413 /* See if this directory is already known. */
414 for (dirp
= GL(dl_all_dirs
); dirp
!= NULL
; dirp
= dirp
->next
)
415 if (dirp
->dirnamelen
== len
&& memcmp (cp
, dirp
->dirname
, len
) == 0)
420 /* It is available, see whether it's on our own list. */
422 for (cnt
= 0; cnt
< nelems
; ++cnt
)
423 if (result
[cnt
] == dirp
)
427 result
[nelems
++] = dirp
;
432 enum r_dir_status init_val
;
433 size_t where_len
= where
? strlen (where
) + 1 : 0;
435 /* It's a new directory. Create an entry and add it. */
436 dirp
= (struct r_search_path_elem
*)
437 malloc (sizeof (*dirp
) + ncapstr
* sizeof (enum r_dir_status
)
438 + where_len
+ len
+ 1);
440 INTUSE(_dl_signal_error
) (ENOMEM
, NULL
, NULL
,
441 N_("cannot create cache for search path"));
443 dirp
->dirname
= ((char *) dirp
+ sizeof (*dirp
)
444 + ncapstr
* sizeof (enum r_dir_status
));
445 *((char *) __mempcpy ((char *) dirp
->dirname
, cp
, len
)) = '\0';
446 dirp
->dirnamelen
= len
;
448 if (len
> max_dirnamelen
)
449 max_dirnamelen
= len
;
451 /* We have to make sure all the relative directories are
452 never ignored. The current directory might change and
453 all our saved information would be void. */
454 init_val
= cp
[0] != '/' ? existing
: unknown
;
455 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
456 dirp
->status
[cnt
] = init_val
;
459 if (__builtin_expect (where
!= NULL
, 1))
460 dirp
->where
= memcpy ((char *) dirp
+ sizeof (*dirp
) + len
+ 1
461 + (ncapstr
* sizeof (enum r_dir_status
)),
466 dirp
->next
= GL(dl_all_dirs
);
467 GL(dl_all_dirs
) = dirp
;
469 /* Put it in the result array. */
470 result
[nelems
++] = dirp
;
474 /* Terminate the array. */
475 result
[nelems
] = NULL
;
483 decompose_rpath (struct r_search_path_struct
*sps
,
484 const char *rpath
, struct link_map
*l
, const char *what
)
486 /* Make a copy we can work with. */
487 const char *where
= l
->l_name
;
490 struct r_search_path_elem
**result
;
492 /* Initialize to please the compiler. */
493 const char *errstring
= NULL
;
495 /* First see whether we must forget the RUNPATH and RPATH from this
497 if (__builtin_expect (GL(dl_inhibit_rpath
) != NULL
, 0)
498 && !INTUSE(__libc_enable_secure
))
500 const char *inhp
= GL(dl_inhibit_rpath
);
504 const char *wp
= where
;
506 while (*inhp
== *wp
&& *wp
!= '\0')
512 if (*wp
== '\0' && (*inhp
== '\0' || *inhp
== ':'))
514 /* This object is on the list of objects for which the
515 RUNPATH and RPATH must not be used. */
516 result
= (struct r_search_path_elem
**)
517 malloc (sizeof (*result
));
521 errstring
= N_("cannot create cache for search path");
523 INTUSE(_dl_signal_error
) (ENOMEM
, NULL
, NULL
, errstring
);
534 while (*inhp
!= '\0')
538 while (*inhp
!= '\0');
541 /* Make a writable copy. At the same time expand possible dynamic
543 copy
= expand_dynamic_string_token (l
, rpath
);
546 errstring
= N_("cannot create RUNPATH/RPATH copy");
550 /* Count the number of necessary elements in the result array. */
552 for (cp
= copy
; *cp
!= '\0'; ++cp
)
556 /* Allocate room for the result. NELEMS + 1 is an upper limit for the
557 number of necessary entries. */
558 result
= (struct r_search_path_elem
**) malloc ((nelems
+ 1 + 1)
561 goto signal_error_cache
;
563 fillin_rpath (copy
, result
, ":", 0, what
, where
);
565 /* Free the copied RPATH string. `fillin_rpath' make own copies if
570 /* The caller will change this value if we haven't used a real malloc. */
577 _dl_init_paths (const char *llp
)
581 struct r_search_path_elem
*pelem
, **aelem
;
586 /* Initialize to please the compiler. */
587 const char *errstring
= NULL
;
589 /* Fill in the information about the application's RPATH and the
590 directories addressed by the LD_LIBRARY_PATH environment variable. */
592 /* Get the capabilities. */
593 capstr
= _dl_important_hwcaps (GL(dl_platform
), GL(dl_platformlen
),
594 &ncapstr
, &max_capstrlen
);
596 /* First set up the rest of the default search directory entries. */
597 aelem
= rtld_search_dirs
.dirs
= (struct r_search_path_elem
**)
598 malloc ((nsystem_dirs_len
+ 1) * sizeof (struct r_search_path_elem
*));
599 if (rtld_search_dirs
.dirs
== NULL
)
601 errstring
= N_("cannot create search path array");
603 INTUSE(_dl_signal_error
) (ENOMEM
, NULL
, NULL
, errstring
);
606 round_size
= ((2 * sizeof (struct r_search_path_elem
) - 1
607 + ncapstr
* sizeof (enum r_dir_status
))
608 / sizeof (struct r_search_path_elem
));
610 rtld_search_dirs
.dirs
[0] = (struct r_search_path_elem
*)
611 malloc ((sizeof (system_dirs
) / sizeof (system_dirs
[0]))
612 * round_size
* sizeof (struct r_search_path_elem
));
613 if (rtld_search_dirs
.dirs
[0] == NULL
)
615 errstring
= N_("cannot create cache for search path");
619 rtld_search_dirs
.malloced
= 0;
620 pelem
= GL(dl_all_dirs
) = rtld_search_dirs
.dirs
[0];
630 pelem
->what
= "system search path";
633 pelem
->dirname
= strp
;
634 pelem
->dirnamelen
= system_dirs_len
[idx
];
635 strp
+= system_dirs_len
[idx
] + 1;
637 /* System paths must be absolute. */
638 assert (pelem
->dirname
[0] == '/');
639 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
640 pelem
->status
[cnt
] = unknown
;
642 pelem
->next
= (++idx
== nsystem_dirs_len
? NULL
: (pelem
+ round_size
));
646 while (idx
< nsystem_dirs_len
);
648 max_dirnamelen
= SYSTEM_DIRS_MAX_LEN
;
652 /* This points to the map of the main object. */
656 assert (l
->l_type
!= lt_loaded
);
658 if (l
->l_info
[DT_RUNPATH
])
660 /* Allocate room for the search path and fill in information
662 decompose_rpath (&l
->l_runpath_dirs
,
663 (const void *) (D_PTR (l
, l_info
[DT_STRTAB
])
664 + l
->l_info
[DT_RUNPATH
]->d_un
.d_val
),
667 /* The RPATH is ignored. */
668 l
->l_rpath_dirs
.dirs
= (void *) -1;
672 l
->l_runpath_dirs
.dirs
= (void *) -1;
674 if (l
->l_info
[DT_RPATH
])
676 /* Allocate room for the search path and fill in information
678 decompose_rpath (&l
->l_rpath_dirs
,
679 (const void *) (D_PTR (l
, l_info
[DT_STRTAB
])
680 + l
->l_info
[DT_RPATH
]->d_un
.d_val
),
682 l
->l_rpath_dirs
.malloced
= 0;
685 l
->l_rpath_dirs
.dirs
= (void *) -1;
690 if (llp
!= NULL
&& *llp
!= '\0')
693 const char *cp
= llp
;
694 char *llp_tmp
= strdupa (llp
);
696 /* Decompose the LD_LIBRARY_PATH contents. First determine how many
701 if (*cp
== ':' || *cp
== ';')
706 env_path_list
.dirs
= (struct r_search_path_elem
**)
707 malloc ((nllp
+ 1) * sizeof (struct r_search_path_elem
*));
708 if (env_path_list
.dirs
== NULL
)
710 errstring
= N_("cannot create cache for search path");
714 (void) fillin_rpath (llp_tmp
, env_path_list
.dirs
, ":;",
715 INTUSE(__libc_enable_secure
), "LD_LIBRARY_PATH",
718 if (env_path_list
.dirs
[0] == NULL
)
720 free (env_path_list
.dirs
);
721 env_path_list
.dirs
= (void *) -1;
724 env_path_list
.malloced
= 0;
727 env_path_list
.dirs
= (void *) -1;
729 /* Remember the last search directory added at startup. */
730 GL(dl_init_all_dirs
) = GL(dl_all_dirs
);
734 /* Think twice before changing anything in this function. It is placed
735 here and prepared using the `alloca' magic to prevent it from being
736 inlined. The function is only called in case of an error. But then
737 performance does not count. The function used to be "inlinable" and
738 the compiled did so all the time. This increased the code size for
739 absolutely no good reason. */
741 __attribute__ ((noreturn
))
742 lose (int code
, int fd
, const char *name
, char *realname
, struct link_map
*l
,
745 /* The use of `alloca' here looks ridiculous but it helps. The goal
746 is to avoid the function from being inlined. There is no official
747 way to do this so we use this trick. gcc never inlines functions
748 which use `alloca'. */
749 int *a
= (int *) alloca (sizeof (int));
751 /* The file might already be closed. */
753 (void) __close (a
[0]);
756 /* Remove the stillborn object from the list and free it. */
757 assert (l
->l_next
== NULL
);
758 if (l
->l_prev
== NULL
)
759 /* No other module loaded. This happens only in the static library,
760 or in rtld under --verify. */
761 GL(dl_loaded
) = NULL
;
763 l
->l_prev
->l_next
= NULL
;
768 INTUSE(_dl_signal_error
) (code
, name
, NULL
, msg
);
772 /* Map in the shared object NAME, actually located in REALNAME, and already
775 #ifndef EXTERNAL_MAP_FROM_FD
779 _dl_map_object_from_fd (const char *name
, int fd
, struct filebuf
*fbp
,
780 char *realname
, struct link_map
*loader
, int l_type
,
783 struct link_map
*l
= NULL
;
784 const ElfW(Ehdr
) *header
;
785 const ElfW(Phdr
) *phdr
;
786 const ElfW(Phdr
) *ph
;
790 /* Initialize to keep the compiler happy. */
791 const char *errstring
= NULL
;
794 /* Get file information. */
795 if (__builtin_expect (__fxstat64 (_STAT_VER
, fd
, &st
) < 0, 0))
797 errstring
= N_("cannot stat shared object");
801 lose (errval
, fd
, name
, realname
, l
, errstring
);
804 /* Look again to see if the real name matched another already loaded. */
805 for (l
= GL(dl_loaded
); l
; l
= l
->l_next
)
806 if (l
->l_ino
== st
.st_ino
&& l
->l_dev
== st
.st_dev
)
808 /* The object is already loaded.
809 Just bump its reference count and return it. */
812 /* If the name is not in the list of names for this object add
815 add_name_to_object (l
, name
);
820 if (mode
& RTLD_NOLOAD
)
821 /* We are not supposed to load the object unless it is already
822 loaded. So return now. */
825 /* Print debugging message. */
826 if (__builtin_expect (GL(dl_debug_mask
) & DL_DEBUG_FILES
, 0))
827 INTUSE(_dl_debug_printf
) ("file=%s; generating link map\n", name
);
829 /* This is the ELF header. We read it in `open_verify'. */
830 header
= (void *) fbp
->buf
;
834 if (_dl_zerofd
== -1)
836 _dl_zerofd
= _dl_sysdep_open_zero_fill ();
837 if (_dl_zerofd
== -1)
840 INTUSE(_dl_signal_error
) (errno
, NULL
, NULL
,
841 N_("cannot open zero fill device"));
846 /* Enter the new object in the list of loaded objects. */
847 l
= _dl_new_object (realname
, name
, l_type
, loader
);
848 if (__builtin_expect (! l
, 0))
850 errstring
= N_("cannot create shared object descriptor");
851 goto call_lose_errno
;
854 /* Extract the remaining details we need from the ELF header
855 and then read in the program header table. */
856 l
->l_entry
= header
->e_entry
;
857 type
= header
->e_type
;
858 l
->l_phnum
= header
->e_phnum
;
860 maplength
= header
->e_phnum
* sizeof (ElfW(Phdr
));
861 if (header
->e_phoff
+ maplength
<= (size_t) fbp
->len
)
862 phdr
= (void *) (fbp
->buf
+ header
->e_phoff
);
865 phdr
= alloca (maplength
);
866 __lseek (fd
, header
->e_phoff
, SEEK_SET
);
867 if ((size_t) __libc_read (fd
, (void *) phdr
, maplength
) != maplength
)
869 errstring
= N_("cannot read file data");
870 goto call_lose_errno
;
875 /* Scan the program header table, collecting its load commands. */
878 ElfW(Addr
) mapstart
, mapend
, dataend
, allocend
;
881 } loadcmds
[l
->l_phnum
], *c
;
882 size_t nloadcmds
= 0;
884 /* The struct is initialized to zero so this is not necessary:
888 for (ph
= phdr
; ph
< &phdr
[l
->l_phnum
]; ++ph
)
891 /* These entries tell us where to find things once the file's
892 segments are mapped in. We record the addresses it says
893 verbatim, and later correct for the run-time load address. */
895 l
->l_ld
= (void *) ph
->p_vaddr
;
896 l
->l_ldnum
= ph
->p_memsz
/ sizeof (ElfW(Dyn
));
900 l
->l_phdr
= (void *) ph
->p_vaddr
;
904 /* A load command tells us to map in part of the file.
905 We record the load commands and process them all later. */
906 if (__builtin_expect ((ph
->p_align
& (GL(dl_pagesize
) - 1)) != 0,
909 errstring
= N_("ELF load command alignment not page-aligned");
912 if (__builtin_expect (((ph
->p_vaddr
- ph
->p_offset
)
913 & (ph
->p_align
- 1)) != 0, 0))
916 = N_("ELF load command address/offset not properly aligned");
920 c
= &loadcmds
[nloadcmds
++];
921 c
->mapstart
= ph
->p_vaddr
& ~(ph
->p_align
- 1);
922 c
->mapend
= ((ph
->p_vaddr
+ ph
->p_filesz
+ GL(dl_pagesize
) - 1)
923 & ~(GL(dl_pagesize
) - 1));
924 c
->dataend
= ph
->p_vaddr
+ ph
->p_filesz
;
925 c
->allocend
= ph
->p_vaddr
+ ph
->p_memsz
;
926 c
->mapoff
= ph
->p_offset
& ~(ph
->p_align
- 1);
928 /* Optimize a common case. */
929 #if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7
930 c
->prot
= (PF_TO_PROT
931 >> ((ph
->p_flags
& (PF_R
| PF_W
| PF_X
)) * 4)) & 0xf;
934 if (ph
->p_flags
& PF_R
)
935 c
->prot
|= PROT_READ
;
936 if (ph
->p_flags
& PF_W
)
937 c
->prot
|= PROT_WRITE
;
938 if (ph
->p_flags
& PF_X
)
939 c
->prot
|= PROT_EXEC
;
947 l
->l_tls_blocksize
= ph
->p_memsz
;
948 l
->l_tls_align
= ph
->p_align
;
949 l
->l_tls_initimage_size
= ph
->p_filesz
;
950 /* Since we don't know the load address yet only store the
951 offset. We will adjust it later. */
952 l
->l_tls_initimage
= (void *) ph
->p_vaddr
;
954 /* Assign the next available module ID. */
955 l
->l_tls_modid
= _dl_next_tls_modid ();
958 /* Uh-oh, the binary expects TLS support but we cannot
960 _dl_fatal_printf ("cannot handle file '%s' with TLS data\n", name
);
965 /* Now process the load commands and map segments into memory. */
968 /* Length of the sections to be loaded. */
969 maplength
= loadcmds
[nloadcmds
- 1].allocend
- c
->mapstart
;
971 if (__builtin_expect (type
, ET_DYN
) == ET_DYN
)
973 /* This is a position-independent shared object. We can let the
974 kernel map it anywhere it likes, but we must have space for all
975 the segments in their specified positions relative to the first.
976 So we map the first segment without MAP_FIXED, but with its
977 extent increased to cover all the segments. Then we remove
978 access from excess portion, and there is known sufficient space
979 there to remap from the later segments.
981 As a refinement, sometimes we have an address that we would
982 prefer to map such objects at; but this is only a preference,
983 the OS can do whatever it likes. */
985 mappref
= (ELF_PREFERRED_ADDRESS (loader
, maplength
, c
->mapstart
)
986 - MAP_BASE_ADDR (l
));
988 /* Remember which part of the address space this object uses. */
989 l
->l_map_start
= (ElfW(Addr
)) __mmap ((void *) mappref
, maplength
,
990 c
->prot
, MAP_COPY
| MAP_FILE
,
992 if (__builtin_expect ((void *) l
->l_map_start
== MAP_FAILED
, 0))
995 errstring
= N_("failed to map segment from shared object");
996 goto call_lose_errno
;
999 l
->l_map_end
= l
->l_map_start
+ maplength
;
1000 l
->l_addr
= l
->l_map_start
- c
->mapstart
;
1002 /* Change protection on the excess portion to disallow all access;
1003 the portions we do not remap later will be inaccessible as if
1004 unallocated. Then jump into the normal segment-mapping loop to
1005 handle the portion of the segment past the end of the file
1007 __mprotect ((caddr_t
) (l
->l_addr
+ c
->mapend
),
1008 loadcmds
[nloadcmds
- 1].allocend
- c
->mapend
,
1015 /* This object is loaded at a fixed address. This must never
1016 happen for objects loaded with dlopen(). */
1017 if (__builtin_expect (mode
& __RTLD_DLOPEN
, 0))
1019 errstring
= N_("cannot dynamically load executable");
1023 /* Notify ELF_PREFERRED_ADDRESS that we have to load this one
1025 ELF_FIXED_ADDRESS (loader
, c
->mapstart
);
1028 /* Remember which part of the address space this object uses. */
1029 l
->l_map_start
= c
->mapstart
+ l
->l_addr
;
1030 l
->l_map_end
= l
->l_map_start
+ maplength
;
1032 while (c
< &loadcmds
[nloadcmds
])
1034 if (c
->mapend
> c
->mapstart
1035 /* Map the segment contents from the file. */
1036 && (__mmap ((void *) (l
->l_addr
+ c
->mapstart
),
1037 c
->mapend
- c
->mapstart
, c
->prot
,
1038 MAP_FIXED
| MAP_COPY
| MAP_FILE
, fd
, c
->mapoff
)
1044 && (ElfW(Off
)) c
->mapoff
<= header
->e_phoff
1045 && ((size_t) (c
->mapend
- c
->mapstart
+ c
->mapoff
)
1046 >= header
->e_phoff
+ header
->e_phnum
* sizeof (ElfW(Phdr
))))
1047 /* Found the program header in this segment. */
1048 l
->l_phdr
= (void *) (c
->mapstart
+ header
->e_phoff
- c
->mapoff
);
1050 if (c
->allocend
> c
->dataend
)
1052 /* Extra zero pages should appear at the end of this segment,
1053 after the data mapped from the file. */
1054 ElfW(Addr
) zero
, zeroend
, zeropage
;
1056 zero
= l
->l_addr
+ c
->dataend
;
1057 zeroend
= l
->l_addr
+ c
->allocend
;
1058 zeropage
= ((zero
+ GL(dl_pagesize
) - 1)
1059 & ~(GL(dl_pagesize
) - 1));
1061 if (zeroend
< zeropage
)
1062 /* All the extra data is in the last page of the segment.
1063 We can just zero it. */
1066 if (zeropage
> zero
)
1068 /* Zero the final part of the last page of the segment. */
1069 if ((c
->prot
& PROT_WRITE
) == 0)
1072 if (__builtin_expect (__mprotect ((caddr_t
)
1077 c
->prot
|PROT_WRITE
) < 0,
1080 errstring
= N_("cannot change memory protections");
1081 goto call_lose_errno
;
1084 memset ((void *) zero
, '\0', zeropage
- zero
);
1085 if ((c
->prot
& PROT_WRITE
) == 0)
1086 __mprotect ((caddr_t
) (zero
& ~(GL(dl_pagesize
) - 1)),
1087 GL(dl_pagesize
), c
->prot
);
1090 if (zeroend
> zeropage
)
1092 /* Map the remaining zero pages in from the zero fill FD. */
1094 mapat
= __mmap ((caddr_t
) zeropage
, zeroend
- zeropage
,
1095 c
->prot
, MAP_ANON
|MAP_PRIVATE
|MAP_FIXED
,
1097 if (__builtin_expect (mapat
== MAP_FAILED
, 0))
1099 errstring
= N_("cannot map zero-fill pages");
1100 goto call_lose_errno
;
1108 if (l
->l_phdr
== NULL
)
1110 /* The program header is not contained in any of the segments.
1111 We have to allocate memory ourself and copy it over from
1112 out temporary place. */
1113 ElfW(Phdr
) *newp
= (ElfW(Phdr
) *) malloc (header
->e_phnum
1114 * sizeof (ElfW(Phdr
)));
1117 errstring
= N_("cannot allocate memory for program header");
1118 goto call_lose_errno
;
1121 l
->l_phdr
= memcpy (newp
, phdr
,
1122 (header
->e_phnum
* sizeof (ElfW(Phdr
))));
1123 l
->l_phdr_allocated
= 1;
1126 /* Adjust the PT_PHDR value by the runtime load address. */
1127 (ElfW(Addr
)) l
->l_phdr
+= l
->l_addr
;
1131 /* Adjust the address of the TLS initialization image. */
1132 if (l
->l_tls_initimage
!= NULL
)
1133 l
->l_tls_initimage
= (char *) l
->l_tls_initimage
+ l
->l_addr
;
1136 /* We are done mapping in the file. We no longer need the descriptor. */
1138 /* Signal that we closed the file. */
1141 if (l
->l_type
== lt_library
&& type
== ET_EXEC
)
1142 l
->l_type
= lt_executable
;
1146 if (__builtin_expect (type
== ET_DYN
, 0))
1148 errstring
= N_("object file has no dynamic section");
1153 (ElfW(Addr
)) l
->l_ld
+= l
->l_addr
;
1155 l
->l_entry
+= l
->l_addr
;
1157 if (__builtin_expect (GL(dl_debug_mask
) & DL_DEBUG_FILES
, 0))
1158 INTUSE(_dl_debug_printf
) ("\
1159 dynamic: 0x%0*lx base: 0x%0*lx size: 0x%0*Zx\n\
1160 entry: 0x%0*lx phdr: 0x%0*lx phnum: %*u\n\n",
1161 (int) sizeof (void *) * 2,
1162 (unsigned long int) l
->l_ld
,
1163 (int) sizeof (void *) * 2,
1164 (unsigned long int) l
->l_addr
,
1165 (int) sizeof (void *) * 2, maplength
,
1166 (int) sizeof (void *) * 2,
1167 (unsigned long int) l
->l_entry
,
1168 (int) sizeof (void *) * 2,
1169 (unsigned long int) l
->l_phdr
,
1170 (int) sizeof (void *) * 2, l
->l_phnum
);
1172 elf_get_dynamic_info (l
);
1174 /* Make sure we are not dlopen'ing an object
1175 that has the DF_1_NOOPEN flag set. */
1176 if ((__builtin_expect (l
->l_flags_1
& DF_1_NOOPEN
, 0)
1178 || __builtin_expect (l
->l_flags
& DF_STATIC_TLS
, 0)
1181 && (mode
& __RTLD_DLOPEN
))
1183 /* We are not supposed to load this object. Free all resources. */
1184 __munmap ((void *) l
->l_map_start
, l
->l_map_end
- l
->l_map_start
);
1186 if (!l
->l_libname
->dont_free
)
1187 free (l
->l_libname
);
1189 if (l
->l_phdr_allocated
)
1190 free ((void *) l
->l_phdr
);
1192 errstring
= N_("shared object cannot be dlopen()ed");
1196 if (l
->l_info
[DT_HASH
])
1199 /* If this object has DT_SYMBOLIC set modify now its scope. We don't
1200 have to do this for the main map. */
1201 if (__builtin_expect (l
->l_info
[DT_SYMBOLIC
] != NULL
, 0)
1202 && &l
->l_searchlist
!= l
->l_scope
[0])
1204 /* Create an appropriate searchlist. It contains only this map.
1206 XXX This is the definition of DT_SYMBOLIC in SysVr4. The old
1207 GNU ld.so implementation had a different interpretation which
1208 is more reasonable. We are prepared to add this possibility
1209 back as part of a GNU extension of the ELF format. */
1210 l
->l_symbolic_searchlist
.r_list
=
1211 (struct link_map
**) malloc (sizeof (struct link_map
*));
1213 if (l
->l_symbolic_searchlist
.r_list
== NULL
)
1215 errstring
= N_("cannot create searchlist");
1216 goto call_lose_errno
;
1219 l
->l_symbolic_searchlist
.r_list
[0] = l
;
1220 l
->l_symbolic_searchlist
.r_nlist
= 1;
1222 /* Now move the existing entries one back. */
1223 memmove (&l
->l_scope
[1], &l
->l_scope
[0],
1224 (l
->l_scope_max
- 1) * sizeof (l
->l_scope
[0]));
1226 /* Now add the new entry. */
1227 l
->l_scope
[0] = &l
->l_symbolic_searchlist
;
1230 /* Remember whether this object must be initialized first. */
1231 if (l
->l_flags_1
& DF_1_INITFIRST
)
1232 GL(dl_initfirst
) = l
;
1234 /* Finally the file information. */
1235 l
->l_dev
= st
.st_dev
;
1236 l
->l_ino
= st
.st_ino
;
1238 /* When we profile the SONAME might be needed for something else but
1239 loading. Add it right away. */
1240 if (__builtin_expect (GL(dl_profile
) != NULL
, 0)
1241 && l
->l_info
[DT_SONAME
] != NULL
)
1242 add_name_to_object (l
, ((const char *) D_PTR (l
, l_info
[DT_STRTAB
])
1243 + l
->l_info
[DT_SONAME
]->d_un
.d_val
));
1248 /* Print search path. */
1250 print_search_path (struct r_search_path_elem
**list
,
1251 const char *what
, const char *name
)
1253 char buf
[max_dirnamelen
+ max_capstrlen
];
1256 INTUSE(_dl_debug_printf
) (" search path=");
1258 while (*list
!= NULL
&& (*list
)->what
== what
) /* Yes, ==. */
1260 char *endp
= __mempcpy (buf
, (*list
)->dirname
, (*list
)->dirnamelen
);
1263 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
1264 if ((*list
)->status
[cnt
] != nonexisting
)
1266 char *cp
= __mempcpy (endp
, capstr
[cnt
].str
, capstr
[cnt
].len
);
1267 if (cp
== buf
|| (cp
== buf
+ 1 && buf
[0] == '/'))
1272 _dl_debug_printf_c (first
? "%s" : ":%s", buf
);
1280 _dl_debug_printf_c ("\t\t(%s from file %s)\n", what
,
1281 name
[0] ? name
: rtld_progname
);
1283 _dl_debug_printf_c ("\t\t(%s)\n", what
);
1286 /* Open a file and verify it is an ELF file for this architecture. We
1287 ignore only ELF files for other architectures. Non-ELF files and
1288 ELF files with different header information cause fatal errors since
1289 this could mean there is something wrong in the installation and the
1290 user might want to know about this. */
1292 open_verify (const char *name
, struct filebuf
*fbp
)
1294 /* This is the expected ELF header. */
1295 #define ELF32_CLASS ELFCLASS32
1296 #define ELF64_CLASS ELFCLASS64
1297 #ifndef VALID_ELF_HEADER
1298 # define VALID_ELF_HEADER(hdr,exp,size) (memcmp (hdr, exp, size) == 0)
1299 # define VALID_ELF_OSABI(osabi) (osabi == ELFOSABI_SYSV)
1300 # define VALID_ELF_ABIVERSION(ver) (ver == 0)
1302 static const unsigned char expected
[EI_PAD
] =
1304 [EI_MAG0
] = ELFMAG0
,
1305 [EI_MAG1
] = ELFMAG1
,
1306 [EI_MAG2
] = ELFMAG2
,
1307 [EI_MAG3
] = ELFMAG3
,
1308 [EI_CLASS
] = ELFW(CLASS
),
1309 [EI_DATA
] = byteorder
,
1310 [EI_VERSION
] = EV_CURRENT
,
1311 [EI_OSABI
] = ELFOSABI_SYSV
,
1316 ElfW(Word
) vendorlen
;
1320 } expected_note
= { 4, 16, 1, "GNU" };
1322 /* Initialize it to make the compiler happy. */
1323 const char *errstring
= NULL
;
1326 /* Open the file. We always open files read-only. */
1327 fd
= __open (name
, O_RDONLY
);
1331 ElfW(Phdr
) *phdr
, *ph
;
1332 ElfW(Word
) *abi_note
, abi_note_buf
[8];
1333 unsigned int osversion
;
1336 /* We successfully openened the file. Now verify it is a file
1339 fbp
->len
= __libc_read (fd
, fbp
->buf
, sizeof (fbp
->buf
));
1341 /* This is where the ELF header is loaded. */
1342 assert (sizeof (fbp
->buf
) > sizeof (ElfW(Ehdr
)));
1343 ehdr
= (ElfW(Ehdr
) *) fbp
->buf
;
1345 /* Now run the tests. */
1346 if (__builtin_expect (fbp
->len
< (ssize_t
) sizeof (ElfW(Ehdr
)), 0))
1349 errstring
= (errval
== 0
1350 ? N_("file too short") : N_("cannot read file data"));
1352 lose (errval
, fd
, name
, NULL
, NULL
, errstring
);
1355 /* See whether the ELF header is what we expect. */
1356 if (__builtin_expect (! VALID_ELF_HEADER (ehdr
->e_ident
, expected
,
1359 /* Something is wrong. */
1360 if (*(Elf32_Word
*) &ehdr
->e_ident
!=
1361 #if BYTE_ORDER == LITTLE_ENDIAN
1362 ((ELFMAG0
<< (EI_MAG0
* 8)) |
1363 (ELFMAG1
<< (EI_MAG1
* 8)) |
1364 (ELFMAG2
<< (EI_MAG2
* 8)) |
1365 (ELFMAG3
<< (EI_MAG3
* 8)))
1367 ((ELFMAG0
<< (EI_MAG3
* 8)) |
1368 (ELFMAG1
<< (EI_MAG2
* 8)) |
1369 (ELFMAG2
<< (EI_MAG1
* 8)) |
1370 (ELFMAG3
<< (EI_MAG0
* 8)))
1373 errstring
= N_("invalid ELF header");
1374 else if (ehdr
->e_ident
[EI_CLASS
] != ELFW(CLASS
))
1375 /* This is not a fatal error. On architectures where
1376 32-bit and 64-bit binaries can be run this might
1379 else if (ehdr
->e_ident
[EI_DATA
] != byteorder
)
1381 if (BYTE_ORDER
== BIG_ENDIAN
)
1382 errstring
= N_("ELF file data encoding not big-endian");
1384 errstring
= N_("ELF file data encoding not little-endian");
1386 else if (ehdr
->e_ident
[EI_VERSION
] != EV_CURRENT
)
1388 = N_("ELF file version ident does not match current one");
1389 /* XXX We should be able so set system specific versions which are
1391 else if (!VALID_ELF_OSABI (ehdr
->e_ident
[EI_OSABI
]))
1392 errstring
= N_("ELF file OS ABI invalid");
1393 else if (!VALID_ELF_ABIVERSION (ehdr
->e_ident
[EI_ABIVERSION
]))
1394 errstring
= N_("ELF file ABI version invalid");
1396 /* Otherwise we don't know what went wrong. */
1397 errstring
= N_("internal error");
1402 if (__builtin_expect (ehdr
->e_version
, EV_CURRENT
) != EV_CURRENT
)
1404 errstring
= N_("ELF file version does not match current one");
1407 if (! __builtin_expect (elf_machine_matches_host (ehdr
), 1))
1409 else if (__builtin_expect (ehdr
->e_phentsize
, sizeof (ElfW(Phdr
)))
1410 != sizeof (ElfW(Phdr
)))
1412 errstring
= N_("ELF file's phentsize not the expected size");
1415 else if (__builtin_expect (ehdr
->e_type
, ET_DYN
) != ET_DYN
1416 && __builtin_expect (ehdr
->e_type
, ET_EXEC
) != ET_EXEC
)
1418 errstring
= N_("only ET_DYN and ET_EXEC can be loaded");
1422 maplength
= ehdr
->e_phnum
* sizeof (ElfW(Phdr
));
1423 if (ehdr
->e_phoff
+ maplength
<= (size_t) fbp
->len
)
1424 phdr
= (void *) (fbp
->buf
+ ehdr
->e_phoff
);
1427 phdr
= alloca (maplength
);
1428 __lseek (fd
, ehdr
->e_phoff
, SEEK_SET
);
1429 if ((size_t) __libc_read (fd
, (void *) phdr
, maplength
) != maplength
)
1433 errstring
= N_("cannot read file data");
1438 /* Check .note.ABI-tag if present. */
1439 for (ph
= phdr
; ph
< &phdr
[ehdr
->e_phnum
]; ++ph
)
1440 if (ph
->p_type
== PT_NOTE
&& ph
->p_filesz
== 32 && ph
->p_align
>= 4)
1442 if (ph
->p_offset
+ 32 <= (size_t) fbp
->len
)
1443 abi_note
= (void *) (fbp
->buf
+ ph
->p_offset
);
1446 __lseek (fd
, ph
->p_offset
, SEEK_SET
);
1447 if (__libc_read (fd
, (void *) abi_note_buf
, 32) != 32)
1450 abi_note
= abi_note_buf
;
1453 if (memcmp (abi_note
, &expected_note
, sizeof (expected_note
)))
1456 osversion
= (abi_note
[5] & 0xff) * 65536
1457 + (abi_note
[6] & 0xff) * 256
1458 + (abi_note
[7] & 0xff);
1459 if (abi_note
[4] != __ABI_TAG_OS
1460 || (GL(dl_osversion
) && GL(dl_osversion
) < osversion
))
1464 __set_errno (ENOENT
);
1475 /* Try to open NAME in one of the directories in *DIRSP.
1476 Return the fd, or -1. If successful, fill in *REALNAME
1477 with the malloc'd full directory name. If it turns out
1478 that none of the directories in *DIRSP exists, *DIRSP is
1479 replaced with (void *) -1, and the old value is free()d
1480 if MAY_FREE_DIRS is true. */
1483 open_path (const char *name
, size_t namelen
, int preloaded
,
1484 struct r_search_path_struct
*sps
, char **realname
,
1485 struct filebuf
*fbp
)
1487 struct r_search_path_elem
**dirs
= sps
->dirs
;
1490 const char *current_what
= NULL
;
1493 buf
= alloca (max_dirnamelen
+ max_capstrlen
+ namelen
);
1496 struct r_search_path_elem
*this_dir
= *dirs
;
1503 /* If we are debugging the search for libraries print the path
1504 now if it hasn't happened now. */
1505 if (__builtin_expect (GL(dl_debug_mask
) & DL_DEBUG_LIBS
, 0)
1506 && current_what
!= this_dir
->what
)
1508 current_what
= this_dir
->what
;
1509 print_search_path (dirs
, current_what
, this_dir
->where
);
1512 edp
= (char *) __mempcpy (buf
, this_dir
->dirname
, this_dir
->dirnamelen
);
1513 for (cnt
= 0; fd
== -1 && cnt
< ncapstr
; ++cnt
)
1515 /* Skip this directory if we know it does not exist. */
1516 if (this_dir
->status
[cnt
] == nonexisting
)
1520 ((char *) __mempcpy (__mempcpy (edp
, capstr
[cnt
].str
,
1525 /* Print name we try if this is wanted. */
1526 if (__builtin_expect (GL(dl_debug_mask
) & DL_DEBUG_LIBS
, 0))
1527 INTUSE(_dl_debug_printf
) (" trying file=%s\n", buf
);
1529 fd
= open_verify (buf
, fbp
);
1530 if (this_dir
->status
[cnt
] == unknown
)
1533 this_dir
->status
[cnt
] = existing
;
1536 /* We failed to open machine dependent library. Let's
1537 test whether there is any directory at all. */
1540 buf
[buflen
- namelen
- 1] = '\0';
1542 if (__xstat64 (_STAT_VER
, buf
, &st
) != 0
1543 || ! S_ISDIR (st
.st_mode
))
1544 /* The directory does not exist or it is no directory. */
1545 this_dir
->status
[cnt
] = nonexisting
;
1547 this_dir
->status
[cnt
] = existing
;
1551 /* Remember whether we found any existing directory. */
1552 here_any
|= this_dir
->status
[cnt
] == existing
;
1554 if (fd
!= -1 && __builtin_expect (preloaded
, 0)
1555 && INTUSE(__libc_enable_secure
))
1557 /* This is an extra security effort to make sure nobody can
1558 preload broken shared objects which are in the trusted
1559 directories and so exploit the bugs. */
1562 if (__fxstat64 (_STAT_VER
, fd
, &st
) != 0
1563 || (st
.st_mode
& S_ISUID
) == 0)
1565 /* The shared object cannot be tested for being SUID
1566 or this bit is not set. In this case we must not
1570 /* We simply ignore the file, signal this by setting
1571 the error value which would have been set by `open'. */
1579 *realname
= (char *) malloc (buflen
);
1580 if (*realname
!= NULL
)
1582 memcpy (*realname
, buf
, buflen
);
1587 /* No memory for the name, we certainly won't be able
1588 to load and link it. */
1593 if (here_any
&& (err
= errno
) != ENOENT
&& err
!= EACCES
)
1594 /* The file exists and is readable, but something went wrong. */
1597 /* Remember whether we found anything. */
1600 while (*++dirs
!= NULL
);
1602 /* Remove the whole path if none of the directories exists. */
1603 if (__builtin_expect (! any
, 0))
1605 /* Paths which were allocated using the minimal malloc() in ld.so
1606 must not be freed using the general free() in libc. */
1609 sps
->dirs
= (void *) -1;
1615 /* Map in the shared object file NAME. */
1619 _dl_map_object (struct link_map
*loader
, const char *name
, int preloaded
,
1620 int type
, int trace_mode
, int mode
)
1628 /* Look for this name among those already loaded. */
1629 for (l
= GL(dl_loaded
); l
; l
= l
->l_next
)
1631 /* If the requested name matches the soname of a loaded object,
1632 use that object. Elide this check for names that have not
1634 if (__builtin_expect (l
->l_faked
, 0) != 0)
1636 if (!_dl_name_match_p (name
, l
))
1640 if (__builtin_expect (l
->l_soname_added
, 1)
1641 || l
->l_info
[DT_SONAME
] == NULL
)
1644 soname
= ((const char *) D_PTR (l
, l_info
[DT_STRTAB
])
1645 + l
->l_info
[DT_SONAME
]->d_un
.d_val
);
1646 if (strcmp (name
, soname
) != 0)
1649 /* We have a match on a new name -- cache it. */
1650 add_name_to_object (l
, soname
);
1651 l
->l_soname_added
= 1;
1654 /* We have a match. */
1658 /* Display information if we are debugging. */
1659 if (__builtin_expect (GL(dl_debug_mask
) & DL_DEBUG_FILES
, 0)
1661 INTUSE(_dl_debug_printf
) ("\nfile=%s; needed by %s\n", name
,
1663 ? loader
->l_name
: rtld_progname
);
1665 if (strchr (name
, '/') == NULL
)
1667 /* Search for NAME in several places. */
1669 size_t namelen
= strlen (name
) + 1;
1671 if (__builtin_expect (GL(dl_debug_mask
) & DL_DEBUG_LIBS
, 0))
1672 INTUSE(_dl_debug_printf
) ("find library=%s; searching\n", name
);
1676 /* When the object has the RUNPATH information we don't use any
1678 if (loader
== NULL
|| loader
->l_info
[DT_RUNPATH
] == NULL
)
1680 /* First try the DT_RPATH of the dependent object that caused NAME
1681 to be loaded. Then that object's dependent, and on up. */
1682 for (l
= loader
; fd
== -1 && l
; l
= l
->l_loader
)
1684 if (l
->l_rpath_dirs
.dirs
== NULL
)
1686 if (l
->l_info
[DT_RPATH
] == NULL
)
1688 /* There is no path. */
1689 l
->l_rpath_dirs
.dirs
= (void *) -1;
1694 /* Make sure the cache information is available. */
1695 size_t ptrval
= (D_PTR (l
, l_info
[DT_STRTAB
])
1696 + l
->l_info
[DT_RPATH
]->d_un
.d_val
);
1697 decompose_rpath (&l
->l_rpath_dirs
,
1698 (const char *) ptrval
, l
, "RPATH");
1702 if (l
->l_rpath_dirs
.dirs
!= (void *) -1)
1703 fd
= open_path (name
, namelen
, preloaded
, &l
->l_rpath_dirs
,
1707 /* If dynamically linked, try the DT_RPATH of the executable
1710 if (fd
== -1 && l
&& l
->l_type
!= lt_loaded
&& l
!= loader
1711 && l
->l_rpath_dirs
.dirs
!= (void *) -1)
1712 fd
= open_path (name
, namelen
, preloaded
, &l
->l_rpath_dirs
,
1716 /* Try the LD_LIBRARY_PATH environment variable. */
1717 if (fd
== -1 && env_path_list
.dirs
!= (void *) -1)
1718 fd
= open_path (name
, namelen
, preloaded
, &env_path_list
,
1721 /* Look at the RUNPATH information for this binary.
1723 Note that this is no real loop. 'while' is used only to enable
1724 us to use 'break' instead of a 'goto' to jump to the end. The
1725 loop is always left after the first round. */
1726 while (fd
== -1 && loader
!= NULL
1727 && loader
->l_runpath_dirs
.dirs
!= (void *) -1)
1729 if (loader
->l_runpath_dirs
.dirs
== NULL
)
1731 if (loader
->l_info
[DT_RUNPATH
] == NULL
)
1734 loader
->l_runpath_dirs
.dirs
= (void *) -1;
1739 /* Make sure the cache information is available. */
1740 size_t ptrval
= (D_PTR (loader
, l_info
[DT_STRTAB
])
1741 + loader
->l_info
[DT_RUNPATH
]->d_un
.d_val
);
1742 decompose_rpath (&loader
->l_runpath_dirs
,
1743 (const char *) ptrval
, loader
, "RUNPATH");
1747 if (loader
->l_runpath_dirs
.dirs
!= (void *) -1)
1748 fd
= open_path (name
, namelen
, preloaded
,
1749 &loader
->l_runpath_dirs
, &realname
, &fb
);
1754 && (__builtin_expect (! preloaded
, 1)
1755 || ! INTUSE(__libc_enable_secure
)))
1757 /* Check the list of libraries in the file /etc/ld.so.cache,
1758 for compatibility with Linux's ldconfig program. */
1759 const char *cached
= _dl_load_cache_lookup (name
);
1764 l
= loader
?: GL(dl_loaded
);
1769 /* If the loader has the DF_1_NODEFLIB flag set we must not
1770 use a cache entry from any of these directories. */
1773 /* 'l' is always != NULL for dynamically linked objects. */
1776 __builtin_expect (l
->l_flags_1
& DF_1_NODEFLIB
, 0))
1778 const char *dirp
= system_dirs
;
1779 unsigned int cnt
= 0;
1783 if (memcmp (cached
, dirp
, system_dirs_len
[cnt
]) == 0)
1785 /* The prefix matches. Don't use the entry. */
1790 dirp
+= system_dirs_len
[cnt
] + 1;
1793 while (cnt
< nsystem_dirs_len
);
1798 fd
= open_verify (cached
, &fb
);
1799 if (__builtin_expect (fd
!= -1, 1))
1801 realname
= local_strdup (cached
);
1802 if (realname
== NULL
)
1812 /* Finally, try the default path. */
1814 && ((l
= loader
?: GL(dl_loaded
)) == NULL
1815 || __builtin_expect (!(l
->l_flags_1
& DF_1_NODEFLIB
), 1))
1816 && rtld_search_dirs
.dirs
!= (void *) -1)
1817 fd
= open_path (name
, namelen
, preloaded
, &rtld_search_dirs
,
1820 /* Add another newline when we are tracing the library loading. */
1821 if (__builtin_expect (GL(dl_debug_mask
) & DL_DEBUG_LIBS
, 0))
1822 INTUSE(_dl_debug_printf
) ("\n");
1826 /* The path may contain dynamic string tokens. */
1828 ? expand_dynamic_string_token (loader
, name
)
1829 : local_strdup (name
));
1830 if (realname
== NULL
)
1834 fd
= open_verify (realname
, &fb
);
1835 if (__builtin_expect (fd
, 0) == -1)
1840 if (__builtin_expect (fd
, 0) == -1)
1843 && __builtin_expect (GL(dl_debug_mask
) & DL_DEBUG_PRELINK
, 0) == 0)
1845 /* We haven't found an appropriate library. But since we
1846 are only interested in the list of libraries this isn't
1847 so severe. Fake an entry with all the information we
1849 static const Elf_Symndx dummy_bucket
= STN_UNDEF
;
1851 /* Enter the new object in the list of loaded objects. */
1852 if ((name_copy
= local_strdup (name
)) == NULL
1853 || (l
= _dl_new_object (name_copy
, name
, type
, loader
)) == NULL
)
1854 INTUSE(_dl_signal_error
) (ENOMEM
, name
, NULL
, N_("\
1855 cannot create shared object descriptor"));
1856 /* Signal that this is a faked entry. */
1858 /* Since the descriptor is initialized with zero we do not
1860 l->l_reserved = 0; */
1861 l
->l_buckets
= &dummy_bucket
;
1868 INTUSE(_dl_signal_error
) (errno
, name
, NULL
,
1869 N_("cannot open shared object file"));
1872 return _dl_map_object_from_fd (name
, fd
, &fb
, realname
, loader
, type
, mode
);
1874 INTDEF (_dl_map_object
)