1 /* Map in a shared object's segments from the file.
2 Copyright (C) 1995-2002, 2003, 2004, 2005 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 <bits/wordsize.h>
31 #include <sys/param.h>
33 #include <sys/types.h>
34 #include "dynamic-link.h"
36 #include <dl-osinfo.h>
37 #include <stackinfo.h>
43 /* On some systems, no flag bits are given to specify file mapping. */
48 /* The right way to map in the shared library files is MAP_COPY, which
49 makes a virtual copy of the data at the time of the mmap call; this
50 guarantees the mapped pages will be consistent even if the file is
51 overwritten. Some losing VM systems like Linux's lack MAP_COPY. All we
52 get is MAP_PRIVATE, which copies each page when it is modified; this
53 means if the file is overwritten, we may at some point get some pages
54 from the new version after starting with pages from the old version. */
56 # define MAP_COPY MAP_PRIVATE
59 /* We want to prevent people from modifying DSOs which are currently in
60 use. This is what MAP_DENYWRITE is for. */
62 # define MAP_DENYWRITE 0
65 /* Some systems link their relocatable objects for another base address
66 than 0. We want to know the base address for these such that we can
67 subtract this address from the segment addresses during mapping.
68 This results in a more efficient address space usage. Defaults to
69 zero for almost all systems. */
71 # define MAP_BASE_ADDR(l) 0
76 #if BYTE_ORDER == BIG_ENDIAN
77 # define byteorder ELFDATA2MSB
78 #elif BYTE_ORDER == LITTLE_ENDIAN
79 # define byteorder ELFDATA2LSB
81 # error "Unknown BYTE_ORDER " BYTE_ORDER
82 # define byteorder ELFDATANONE
85 #define STRING(x) __STRING (x)
88 /* The fd is not examined when using MAP_ANON. */
92 # define ANONFD _dl_zerofd
95 /* Handle situations where we have a preferred location in memory for
96 the shared objects. */
97 #ifdef ELF_PREFERRED_ADDRESS_DATA
98 ELF_PREFERRED_ADDRESS_DATA
;
100 #ifndef ELF_PREFERRED_ADDRESS
101 # define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
103 #ifndef ELF_FIXED_ADDRESS
104 # define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
108 int __stack_prot attribute_hidden attribute_relro
109 #if _STACK_GROWS_DOWN && defined PROT_GROWSDOWN
111 #elif _STACK_GROWS_UP && defined PROT_GROWSUP
118 /* Type for the buffer we put the ELF header and hopefully the program
119 header. This buffer does not really have to be too large. In most
120 cases the program header follows the ELF header directly. If this
121 is not the case all bets are off and we can make the header
122 arbitrarily large and still won't get it read. This means the only
123 question is how large are the ELF and program header combined. The
124 ELF header 32-bit files is 52 bytes long and in 64-bit files is 64
125 bytes long. Each program header entry is again 32 and 56 bytes
126 long respectively. I.e., even with a file which has 7 program
127 header entries we only have to read 512B. Add to this a bit of
128 margin for program notes and reading 512B and 640B for 32-bit and
129 64-bit files respecitvely is enough. If this heuristic should
130 really fail for some file the code in `_dl_map_object_from_fd'
131 knows how to recover. */
136 # define FILEBUF_SIZE 512
138 # define FILEBUF_SIZE 640
140 char buf
[FILEBUF_SIZE
] __attribute__ ((aligned (__alignof (ElfW(Ehdr
)))));
143 /* This is the decomposed LD_LIBRARY_PATH search path. */
144 static struct r_search_path_struct env_path_list attribute_relro
;
146 /* List of the hardware capabilities we might end up using. */
147 static const struct r_strlenpair
*capstr attribute_relro
;
148 static size_t ncapstr attribute_relro
;
149 static size_t max_capstrlen attribute_relro
;
152 /* Get the generated information about the trusted directories. */
153 #include "trusted-dirs.h"
155 static const char system_dirs
[] = SYSTEM_DIRS
;
156 static const size_t system_dirs_len
[] =
160 #define nsystem_dirs_len \
161 (sizeof (system_dirs_len) / sizeof (system_dirs_len[0]))
164 /* Local version of `strdup' function. */
166 local_strdup (const char *s
)
168 size_t len
= strlen (s
) + 1;
169 void *new = malloc (len
);
174 return (char *) memcpy (new, s
, len
);
179 is_dst (const char *start
, const char *name
, const char *str
,
180 int is_path
, int secure
)
183 bool is_curly
= false;
192 while (name
[len
] == str
[len
] && name
[len
] != '\0')
197 if (name
[len
] != '}')
200 /* Point again at the beginning of the name. */
202 /* Skip over closing curly brace and adjust for the --name. */
205 else if (name
[len
] != '\0' && name
[len
] != '/'
206 && (!is_path
|| name
[len
] != ':'))
209 if (__builtin_expect (secure
, 0)
210 && ((name
[len
] != '\0' && (!is_path
|| name
[len
] != ':'))
211 || (name
!= start
+ 1 && (!is_path
|| name
[-2] != ':'))))
219 _dl_dst_count (const char *name
, int is_path
)
221 const char *const start
= name
;
228 /* $ORIGIN is not expanded for SUID/GUID programs (except if it
229 is $ORIGIN alone) and it must always appear first in path. */
231 if ((len
= is_dst (start
, name
, "ORIGIN", is_path
,
232 INTUSE(__libc_enable_secure
))) != 0
233 || (len
= is_dst (start
, name
, "PLATFORM", is_path
, 0)) != 0
234 || (len
= is_dst (start
, name
, "LIB", is_path
, 0)) != 0)
237 name
= strchr (name
+ len
, '$');
239 while (name
!= NULL
);
246 _dl_dst_substitute (struct link_map
*l
, const char *name
, char *result
,
249 const char *const start
= name
;
250 char *last_elem
, *wp
;
252 /* Now fill the result path. While copying over the string we keep
253 track of the start of the last path element. When we come accross
254 a DST we copy over the value or (if the value is not available)
255 leave the entire path element out. */
256 last_elem
= wp
= result
;
260 if (__builtin_expect (*name
== '$', 0))
262 const char *repl
= NULL
;
266 if ((len
= is_dst (start
, name
, "ORIGIN", is_path
,
267 INTUSE(__libc_enable_secure
))) != 0)
269 else if ((len
= is_dst (start
, name
, "PLATFORM", is_path
, 0)) != 0)
270 repl
= GLRO(dl_platform
);
271 else if ((len
= is_dst (start
, name
, "LIB", is_path
, 0)) != 0)
274 if (repl
!= NULL
&& repl
!= (const char *) -1)
276 wp
= __stpcpy (wp
, repl
);
281 /* We cannot use this path element, the value of the
282 replacement is unknown. */
285 while (*name
!= '\0' && (!is_path
|| *name
!= ':'))
289 /* No DST we recognize. */
295 if (is_path
&& *name
== ':')
299 while (*name
!= '\0');
307 /* Return copy of argument with all recognized dynamic string tokens
308 ($ORIGIN and $PLATFORM for now) replaced. On some platforms it
309 might not be possible to determine the path from which the object
310 belonging to the map is loaded. In this case the path element
311 containing $ORIGIN is left out. */
313 expand_dynamic_string_token (struct link_map
*l
, const char *s
)
315 /* We make two runs over the string. First we determine how large the
316 resulting string is and then we copy it over. Since this is now
317 frequently executed operation we are looking here not for performance
318 but rather for code size. */
323 /* Determine the number of DST elements. */
324 cnt
= DL_DST_COUNT (s
, 1);
326 /* If we do not have to replace anything simply copy the string. */
327 if (__builtin_expect (cnt
, 0) == 0)
328 return local_strdup (s
);
330 /* Determine the length of the substituted string. */
331 total
= DL_DST_REQUIRED (l
, s
, strlen (s
), cnt
);
333 /* Allocate the necessary memory. */
334 result
= (char *) malloc (total
+ 1);
338 return _dl_dst_substitute (l
, s
, result
, 1);
342 /* Add `name' to the list of names for a particular shared object.
343 `name' is expected to have been allocated with malloc and will
344 be freed if the shared object already has this name.
345 Returns false if the object already had this name. */
348 add_name_to_object (struct link_map
*l
, const char *name
)
350 struct libname_list
*lnp
, *lastp
;
351 struct libname_list
*newname
;
355 for (lnp
= l
->l_libname
; lnp
!= NULL
; lastp
= lnp
, lnp
= lnp
->next
)
356 if (strcmp (name
, lnp
->name
) == 0)
359 name_len
= strlen (name
) + 1;
360 newname
= (struct libname_list
*) malloc (sizeof *newname
+ name_len
);
363 /* No more memory. */
364 _dl_signal_error (ENOMEM
, name
, NULL
, N_("cannot allocate name record"));
367 /* The object should have a libname set from _dl_new_object. */
368 assert (lastp
!= NULL
);
370 newname
->name
= memcpy (newname
+ 1, name
, name_len
);
371 newname
->next
= NULL
;
372 newname
->dont_free
= 0;
373 lastp
->next
= newname
;
376 /* Standard search directories. */
377 static struct r_search_path_struct rtld_search_dirs attribute_relro
;
379 static size_t max_dirnamelen
;
381 static struct r_search_path_elem
**
382 fillin_rpath (char *rpath
, struct r_search_path_elem
**result
, const char *sep
,
383 int check_trusted
, const char *what
, const char *where
)
388 while ((cp
= __strsep (&rpath
, sep
)) != NULL
)
390 struct r_search_path_elem
*dirp
;
391 size_t len
= strlen (cp
);
393 /* `strsep' can pass an empty string. This has to be
394 interpreted as `use the current directory'. */
397 static const char curwd
[] = "./";
401 /* Remove trailing slashes (except for "/"). */
402 while (len
> 1 && cp
[len
- 1] == '/')
405 /* Now add one if there is none so far. */
406 if (len
> 0 && cp
[len
- 1] != '/')
409 /* Make sure we don't use untrusted directories if we run SUID. */
410 if (__builtin_expect (check_trusted
, 0))
412 const char *trun
= system_dirs
;
416 /* All trusted directories must be complete names. */
419 for (idx
= 0; idx
< nsystem_dirs_len
; ++idx
)
421 if (len
== system_dirs_len
[idx
]
422 && memcmp (trun
, cp
, len
) == 0)
429 trun
+= system_dirs_len
[idx
] + 1;
434 /* Simply drop this directory. */
438 /* See if this directory is already known. */
439 for (dirp
= GL(dl_all_dirs
); dirp
!= NULL
; dirp
= dirp
->next
)
440 if (dirp
->dirnamelen
== len
&& memcmp (cp
, dirp
->dirname
, len
) == 0)
445 /* It is available, see whether it's on our own list. */
447 for (cnt
= 0; cnt
< nelems
; ++cnt
)
448 if (result
[cnt
] == dirp
)
452 result
[nelems
++] = dirp
;
457 enum r_dir_status init_val
;
458 size_t where_len
= where
? strlen (where
) + 1 : 0;
460 /* It's a new directory. Create an entry and add it. */
461 dirp
= (struct r_search_path_elem
*)
462 malloc (sizeof (*dirp
) + ncapstr
* sizeof (enum r_dir_status
)
463 + where_len
+ len
+ 1);
465 _dl_signal_error (ENOMEM
, NULL
, NULL
,
466 N_("cannot create cache for search path"));
468 dirp
->dirname
= ((char *) dirp
+ sizeof (*dirp
)
469 + ncapstr
* sizeof (enum r_dir_status
));
470 *((char *) __mempcpy ((char *) dirp
->dirname
, cp
, len
)) = '\0';
471 dirp
->dirnamelen
= len
;
473 if (len
> max_dirnamelen
)
474 max_dirnamelen
= len
;
476 /* We have to make sure all the relative directories are
477 never ignored. The current directory might change and
478 all our saved information would be void. */
479 init_val
= cp
[0] != '/' ? existing
: unknown
;
480 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
481 dirp
->status
[cnt
] = init_val
;
484 if (__builtin_expect (where
!= NULL
, 1))
485 dirp
->where
= memcpy ((char *) dirp
+ sizeof (*dirp
) + len
+ 1
486 + (ncapstr
* sizeof (enum r_dir_status
)),
491 dirp
->next
= GL(dl_all_dirs
);
492 GL(dl_all_dirs
) = dirp
;
494 /* Put it in the result array. */
495 result
[nelems
++] = dirp
;
499 /* Terminate the array. */
500 result
[nelems
] = NULL
;
508 decompose_rpath (struct r_search_path_struct
*sps
,
509 const char *rpath
, struct link_map
*l
, const char *what
)
511 /* Make a copy we can work with. */
512 const char *where
= l
->l_name
;
515 struct r_search_path_elem
**result
;
517 /* Initialize to please the compiler. */
518 const char *errstring
= NULL
;
520 /* First see whether we must forget the RUNPATH and RPATH from this
522 if (__builtin_expect (GLRO(dl_inhibit_rpath
) != NULL
, 0)
523 && !INTUSE(__libc_enable_secure
))
525 const char *inhp
= GLRO(dl_inhibit_rpath
);
529 const char *wp
= where
;
531 while (*inhp
== *wp
&& *wp
!= '\0')
537 if (*wp
== '\0' && (*inhp
== '\0' || *inhp
== ':'))
539 /* This object is on the list of objects for which the
540 RUNPATH and RPATH must not be used. */
541 result
= calloc (1, sizeof *result
);
545 errstring
= N_("cannot create cache for search path");
547 _dl_signal_error (ENOMEM
, NULL
, NULL
, errstring
);
556 while (*inhp
!= '\0')
560 while (*inhp
!= '\0');
563 /* Make a writable copy. At the same time expand possible dynamic
565 copy
= expand_dynamic_string_token (l
, rpath
);
568 errstring
= N_("cannot create RUNPATH/RPATH copy");
572 /* Count the number of necessary elements in the result array. */
574 for (cp
= copy
; *cp
!= '\0'; ++cp
)
578 /* Allocate room for the result. NELEMS + 1 is an upper limit for the
579 number of necessary entries. */
580 result
= (struct r_search_path_elem
**) malloc ((nelems
+ 1 + 1)
583 goto signal_error_cache
;
585 fillin_rpath (copy
, result
, ":", 0, what
, where
);
587 /* Free the copied RPATH string. `fillin_rpath' make own copies if
592 /* The caller will change this value if we haven't used a real malloc. */
596 /* Make sure cached path information is stored in *SP
597 and return true if there are any paths to search there. */
599 cache_rpath (struct link_map
*l
,
600 struct r_search_path_struct
*sp
,
604 if (sp
->dirs
== (void *) -1)
607 if (sp
->dirs
!= NULL
)
610 if (l
->l_info
[tag
] == NULL
)
612 /* There is no path. */
613 sp
->dirs
= (void *) -1;
617 /* Make sure the cache information is available. */
618 decompose_rpath (sp
, (const char *) (D_PTR (l
, l_info
[DT_STRTAB
])
619 + l
->l_info
[tag
]->d_un
.d_val
),
627 _dl_init_paths (const char *llp
)
631 struct r_search_path_elem
*pelem
, **aelem
;
636 /* Initialize to please the compiler. */
637 const char *errstring
= NULL
;
639 /* Fill in the information about the application's RPATH and the
640 directories addressed by the LD_LIBRARY_PATH environment variable. */
642 /* Get the capabilities. */
643 capstr
= _dl_important_hwcaps (GLRO(dl_platform
), GLRO(dl_platformlen
),
644 &ncapstr
, &max_capstrlen
);
646 /* First set up the rest of the default search directory entries. */
647 aelem
= rtld_search_dirs
.dirs
= (struct r_search_path_elem
**)
648 malloc ((nsystem_dirs_len
+ 1) * sizeof (struct r_search_path_elem
*));
649 if (rtld_search_dirs
.dirs
== NULL
)
651 errstring
= N_("cannot create search path array");
653 _dl_signal_error (ENOMEM
, NULL
, NULL
, errstring
);
656 round_size
= ((2 * sizeof (struct r_search_path_elem
) - 1
657 + ncapstr
* sizeof (enum r_dir_status
))
658 / sizeof (struct r_search_path_elem
));
660 rtld_search_dirs
.dirs
[0] = (struct r_search_path_elem
*)
661 malloc ((sizeof (system_dirs
) / sizeof (system_dirs
[0]))
662 * round_size
* sizeof (struct r_search_path_elem
));
663 if (rtld_search_dirs
.dirs
[0] == NULL
)
665 errstring
= N_("cannot create cache for search path");
669 rtld_search_dirs
.malloced
= 0;
670 pelem
= GL(dl_all_dirs
) = rtld_search_dirs
.dirs
[0];
680 pelem
->what
= "system search path";
683 pelem
->dirname
= strp
;
684 pelem
->dirnamelen
= system_dirs_len
[idx
];
685 strp
+= system_dirs_len
[idx
] + 1;
687 /* System paths must be absolute. */
688 assert (pelem
->dirname
[0] == '/');
689 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
690 pelem
->status
[cnt
] = unknown
;
692 pelem
->next
= (++idx
== nsystem_dirs_len
? NULL
: (pelem
+ round_size
));
696 while (idx
< nsystem_dirs_len
);
698 max_dirnamelen
= SYSTEM_DIRS_MAX_LEN
;
702 /* This points to the map of the main object. */
703 l
= GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
;
706 assert (l
->l_type
!= lt_loaded
);
708 if (l
->l_info
[DT_RUNPATH
])
710 /* Allocate room for the search path and fill in information
712 decompose_rpath (&l
->l_runpath_dirs
,
713 (const void *) (D_PTR (l
, l_info
[DT_STRTAB
])
714 + l
->l_info
[DT_RUNPATH
]->d_un
.d_val
),
717 /* The RPATH is ignored. */
718 l
->l_rpath_dirs
.dirs
= (void *) -1;
722 l
->l_runpath_dirs
.dirs
= (void *) -1;
724 if (l
->l_info
[DT_RPATH
])
726 /* Allocate room for the search path and fill in information
728 decompose_rpath (&l
->l_rpath_dirs
,
729 (const void *) (D_PTR (l
, l_info
[DT_STRTAB
])
730 + l
->l_info
[DT_RPATH
]->d_un
.d_val
),
732 l
->l_rpath_dirs
.malloced
= 0;
735 l
->l_rpath_dirs
.dirs
= (void *) -1;
740 if (llp
!= NULL
&& *llp
!= '\0')
743 const char *cp
= llp
;
744 char *llp_tmp
= strdupa (llp
);
746 /* Decompose the LD_LIBRARY_PATH contents. First determine how many
751 if (*cp
== ':' || *cp
== ';')
756 env_path_list
.dirs
= (struct r_search_path_elem
**)
757 malloc ((nllp
+ 1) * sizeof (struct r_search_path_elem
*));
758 if (env_path_list
.dirs
== NULL
)
760 errstring
= N_("cannot create cache for search path");
764 (void) fillin_rpath (llp_tmp
, env_path_list
.dirs
, ":;",
765 INTUSE(__libc_enable_secure
), "LD_LIBRARY_PATH",
768 if (env_path_list
.dirs
[0] == NULL
)
770 free (env_path_list
.dirs
);
771 env_path_list
.dirs
= (void *) -1;
774 env_path_list
.malloced
= 0;
777 env_path_list
.dirs
= (void *) -1;
779 /* Remember the last search directory added at startup. */
780 GLRO(dl_init_all_dirs
) = GL(dl_all_dirs
);
785 __attribute__ ((noreturn
, noinline
))
786 lose (int code
, int fd
, const char *name
, char *realname
, struct link_map
*l
,
789 /* The file might already be closed. */
794 /* Remove the stillborn object from the list and free it. */
795 assert (l
->l_next
== NULL
);
796 if (l
->l_prev
== NULL
)
797 /* No other module loaded. This happens only in the static library,
798 or in rtld under --verify. */
799 GL(dl_ns
)[l
->l_ns
]._ns_loaded
= NULL
;
801 l
->l_prev
->l_next
= NULL
;
802 --GL(dl_ns
)[l
->l_ns
]._ns_nloaded
;
806 _dl_signal_error (code
, name
, NULL
, msg
);
810 /* Map in the shared object NAME, actually located in REALNAME, and already
813 #ifndef EXTERNAL_MAP_FROM_FD
817 _dl_map_object_from_fd (const char *name
, int fd
, struct filebuf
*fbp
,
818 char *realname
, struct link_map
*loader
, int l_type
,
819 int mode
, void **stack_endp
, Lmid_t nsid
)
821 struct link_map
*l
= NULL
;
822 const ElfW(Ehdr
) *header
;
823 const ElfW(Phdr
) *phdr
;
824 const ElfW(Phdr
) *ph
;
828 /* Initialize to keep the compiler happy. */
829 const char *errstring
= NULL
;
831 struct r_debug
*r
= _dl_debug_initialize (0, nsid
);
832 bool make_consistent
= false;
834 /* Get file information. */
835 if (__builtin_expect (__fxstat64 (_STAT_VER
, fd
, &st
) < 0, 0))
837 errstring
= N_("cannot stat shared object");
843 r
->r_state
= RT_CONSISTENT
;
847 lose (errval
, fd
, name
, realname
, l
, errstring
);
850 /* Look again to see if the real name matched another already loaded. */
851 for (l
= GL(dl_ns
)[nsid
]._ns_loaded
; l
; l
= l
->l_next
)
852 if (l
->l_ino
== st
.st_ino
&& l
->l_dev
== st
.st_dev
)
854 /* The object is already loaded.
855 Just bump its reference count and return it. */
858 /* If the name is not in the list of names for this object add
861 add_name_to_object (l
, name
);
867 /* When loading into a namespace other than the base one we must
868 avoid loading ld.so since there can only be one copy. Ever. */
869 if (__builtin_expect (nsid
!= LM_ID_BASE
, 0)
870 && ((st
.st_ino
== GL(dl_rtld_map
).l_ino
871 && st
.st_dev
== GL(dl_rtld_map
).l_dev
)
872 || _dl_name_match_p (name
, &GL(dl_rtld_map
))))
874 /* This is indeed ld.so. Create a new link_map which refers to
875 the real one for almost everything. */
876 l
= _dl_new_object (realname
, name
, l_type
, loader
, mode
, nsid
);
880 /* Refer to the real descriptor. */
881 l
->l_real
= &GL(dl_rtld_map
);
883 /* No need to bump the refcount of the real object, ld.so will
884 never be unloaded. */
891 if (mode
& RTLD_NOLOAD
)
892 /* We are not supposed to load the object unless it is already
893 loaded. So return now. */
896 /* Print debugging message. */
897 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_FILES
, 0))
898 _dl_debug_printf ("file=%s [%lu]; generating link map\n", name
, nsid
);
900 /* This is the ELF header. We read it in `open_verify'. */
901 header
= (void *) fbp
->buf
;
905 if (_dl_zerofd
== -1)
907 _dl_zerofd
= _dl_sysdep_open_zero_fill ();
908 if (_dl_zerofd
== -1)
911 _dl_signal_error (errno
, NULL
, NULL
,
912 N_("cannot open zero fill device"));
917 /* Signal that we are going to add new objects. */
918 if (r
->r_state
== RT_CONSISTENT
)
921 /* Auditing checkpoint: we are going to add new objects. */
922 if (__builtin_expect (GLRO(dl_naudit
) > 0, 0))
924 struct link_map
*head
= GL(dl_ns
)[nsid
]._ns_loaded
;
925 /* Do not call the functions for any auditing object. */
926 if (head
->l_auditing
== 0)
928 struct audit_ifaces
*afct
= GLRO(dl_audit
);
929 for (unsigned int cnt
= 0; cnt
< GLRO(dl_naudit
); ++cnt
)
931 if (afct
->activity
!= NULL
)
932 afct
->activity (&head
->l_audit
[cnt
].cookie
, LA_ACT_ADD
);
940 /* Notify the debugger we have added some objects. We need to
941 call _dl_debug_initialize in a static program in case dynamic
942 linking has not been used before. */
945 make_consistent
= true;
948 assert (r
->r_state
== RT_ADD
);
950 /* Enter the new object in the list of loaded objects. */
951 l
= _dl_new_object (realname
, name
, l_type
, loader
, mode
, nsid
);
952 if (__builtin_expect (l
== NULL
, 0))
957 errstring
= N_("cannot create shared object descriptor");
958 goto call_lose_errno
;
961 /* Extract the remaining details we need from the ELF header
962 and then read in the program header table. */
963 l
->l_entry
= header
->e_entry
;
964 type
= header
->e_type
;
965 l
->l_phnum
= header
->e_phnum
;
967 maplength
= header
->e_phnum
* sizeof (ElfW(Phdr
));
968 if (header
->e_phoff
+ maplength
<= (size_t) fbp
->len
)
969 phdr
= (void *) (fbp
->buf
+ header
->e_phoff
);
972 phdr
= alloca (maplength
);
973 __lseek (fd
, header
->e_phoff
, SEEK_SET
);
974 if ((size_t) __libc_read (fd
, (void *) phdr
, maplength
) != maplength
)
976 errstring
= N_("cannot read file data");
977 goto call_lose_errno
;
981 /* Presumed absent PT_GNU_STACK. */
982 uint_fast16_t stack_flags
= PF_R
|PF_W
|PF_X
;
985 /* Scan the program header table, collecting its load commands. */
988 ElfW(Addr
) mapstart
, mapend
, dataend
, allocend
;
991 } loadcmds
[l
->l_phnum
], *c
;
992 size_t nloadcmds
= 0;
993 bool has_holes
= false;
995 /* The struct is initialized to zero so this is not necessary:
999 for (ph
= phdr
; ph
< &phdr
[l
->l_phnum
]; ++ph
)
1002 /* These entries tell us where to find things once the file's
1003 segments are mapped in. We record the addresses it says
1004 verbatim, and later correct for the run-time load address. */
1006 l
->l_ld
= (void *) ph
->p_vaddr
;
1007 l
->l_ldnum
= ph
->p_memsz
/ sizeof (ElfW(Dyn
));
1011 l
->l_phdr
= (void *) ph
->p_vaddr
;
1015 /* A load command tells us to map in part of the file.
1016 We record the load commands and process them all later. */
1017 if (__builtin_expect ((ph
->p_align
& (GLRO(dl_pagesize
) - 1)) != 0,
1020 errstring
= N_("ELF load command alignment not page-aligned");
1023 if (__builtin_expect (((ph
->p_vaddr
- ph
->p_offset
)
1024 & (ph
->p_align
- 1)) != 0, 0))
1027 = N_("ELF load command address/offset not properly aligned");
1031 c
= &loadcmds
[nloadcmds
++];
1032 c
->mapstart
= ph
->p_vaddr
& ~(GLRO(dl_pagesize
) - 1);
1033 c
->mapend
= ((ph
->p_vaddr
+ ph
->p_filesz
+ GLRO(dl_pagesize
) - 1)
1034 & ~(GLRO(dl_pagesize
) - 1));
1035 c
->dataend
= ph
->p_vaddr
+ ph
->p_filesz
;
1036 c
->allocend
= ph
->p_vaddr
+ ph
->p_memsz
;
1037 c
->mapoff
= ph
->p_offset
& ~(GLRO(dl_pagesize
) - 1);
1039 /* Determine whether there is a gap between the last segment
1041 if (nloadcmds
> 1 && c
[-1].mapend
!= c
->mapstart
)
1044 /* Optimize a common case. */
1045 #if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7
1046 c
->prot
= (PF_TO_PROT
1047 >> ((ph
->p_flags
& (PF_R
| PF_W
| PF_X
)) * 4)) & 0xf;
1050 if (ph
->p_flags
& PF_R
)
1051 c
->prot
|= PROT_READ
;
1052 if (ph
->p_flags
& PF_W
)
1053 c
->prot
|= PROT_WRITE
;
1054 if (ph
->p_flags
& PF_X
)
1055 c
->prot
|= PROT_EXEC
;
1061 if (ph
->p_memsz
== 0)
1062 /* Nothing to do for an empty segment. */
1065 l
->l_tls_blocksize
= ph
->p_memsz
;
1066 l
->l_tls_align
= ph
->p_align
;
1067 if (ph
->p_align
== 0)
1068 l
->l_tls_firstbyte_offset
= 0;
1070 l
->l_tls_firstbyte_offset
= ph
->p_vaddr
& (ph
->p_align
- 1);
1071 l
->l_tls_initimage_size
= ph
->p_filesz
;
1072 /* Since we don't know the load address yet only store the
1073 offset. We will adjust it later. */
1074 l
->l_tls_initimage
= (void *) ph
->p_vaddr
;
1076 /* If not loading the initial set of shared libraries,
1077 check whether we should permit loading a TLS segment. */
1078 if (__builtin_expect (l
->l_type
== lt_library
, 1)
1079 /* If GL(dl_tls_dtv_slotinfo_list) == NULL, then rtld.c did
1080 not set up TLS data structures, so don't use them now. */
1081 || __builtin_expect (GL(dl_tls_dtv_slotinfo_list
) != NULL
, 1))
1083 /* Assign the next available module ID. */
1084 l
->l_tls_modid
= _dl_next_tls_modid ();
1089 if (l
->l_prev
== NULL
|| (mode
& __RTLD_AUDIT
) != 0)
1090 /* We are loading the executable itself when the dynamic linker
1091 was executed directly. The setup will happen later. */
1094 /* In a static binary there is no way to tell if we dynamically
1095 loaded libpthread. */
1096 if (GL(dl_error_catch_tsd
) == &_dl_initial_error_catch_tsd
)
1099 /* We have not yet loaded libpthread.
1100 We can do the TLS setup right now! */
1104 /* The first call allocates TLS bookkeeping data structures.
1105 Then we allocate the TCB for the initial thread. */
1106 if (__builtin_expect (_dl_tls_setup (), 0)
1107 || __builtin_expect ((tcb
= _dl_allocate_tls (NULL
)) == NULL
,
1112 cannot allocate TLS data structures for initial thread");
1116 /* Now we install the TCB in the thread register. */
1117 errstring
= TLS_INIT_TP (tcb
, 0);
1118 if (__builtin_expect (errstring
== NULL
, 1))
1120 /* Now we are all good. */
1121 l
->l_tls_modid
= ++GL(dl_tls_max_dtv_idx
);
1125 /* The kernel is too old or somesuch. */
1127 _dl_deallocate_tls (tcb
, 1);
1132 /* Uh-oh, the binary expects TLS support but we cannot
1135 errstring
= N_("cannot handle TLS data");
1140 stack_flags
= ph
->p_flags
;
1144 l
->l_relro_addr
= ph
->p_vaddr
;
1145 l
->l_relro_size
= ph
->p_memsz
;
1149 if (__builtin_expect (nloadcmds
== 0, 0))
1151 /* This only happens for a bogus object that will be caught with
1152 another error below. But we don't want to go through the
1153 calculations below using NLOADCMDS - 1. */
1154 errstring
= N_("object file has no loadable segments");
1158 /* Now process the load commands and map segments into memory. */
1161 /* Length of the sections to be loaded. */
1162 maplength
= loadcmds
[nloadcmds
- 1].allocend
- c
->mapstart
;
1164 if (__builtin_expect (type
, ET_DYN
) == ET_DYN
)
1166 /* This is a position-independent shared object. We can let the
1167 kernel map it anywhere it likes, but we must have space for all
1168 the segments in their specified positions relative to the first.
1169 So we map the first segment without MAP_FIXED, but with its
1170 extent increased to cover all the segments. Then we remove
1171 access from excess portion, and there is known sufficient space
1172 there to remap from the later segments.
1174 As a refinement, sometimes we have an address that we would
1175 prefer to map such objects at; but this is only a preference,
1176 the OS can do whatever it likes. */
1178 mappref
= (ELF_PREFERRED_ADDRESS (loader
, maplength
,
1179 c
->mapstart
& GLRO(dl_use_load_bias
))
1180 - MAP_BASE_ADDR (l
));
1182 /* Remember which part of the address space this object uses. */
1183 l
->l_map_start
= (ElfW(Addr
)) __mmap ((void *) mappref
, maplength
,
1185 MAP_COPY
|MAP_FILE
|MAP_DENYWRITE
,
1187 if (__builtin_expect ((void *) l
->l_map_start
== MAP_FAILED
, 0))
1190 errstring
= N_("failed to map segment from shared object");
1191 goto call_lose_errno
;
1194 l
->l_map_end
= l
->l_map_start
+ maplength
;
1195 l
->l_addr
= l
->l_map_start
- c
->mapstart
;
1198 /* Change protection on the excess portion to disallow all access;
1199 the portions we do not remap later will be inaccessible as if
1200 unallocated. Then jump into the normal segment-mapping loop to
1201 handle the portion of the segment past the end of the file
1203 __mprotect ((caddr_t
) (l
->l_addr
+ c
->mapend
),
1204 loadcmds
[nloadcmds
- 1].allocend
- c
->mapend
,
1210 /* This object is loaded at a fixed address. This must never
1211 happen for objects loaded with dlopen(). */
1212 if (__builtin_expect ((mode
& __RTLD_OPENEXEC
) == 0, 0))
1214 errstring
= N_("cannot dynamically load executable");
1218 /* Notify ELF_PREFERRED_ADDRESS that we have to load this one
1220 ELF_FIXED_ADDRESS (loader
, c
->mapstart
);
1223 /* Remember which part of the address space this object uses. */
1224 l
->l_map_start
= c
->mapstart
+ l
->l_addr
;
1225 l
->l_map_end
= l
->l_map_start
+ maplength
;
1227 while (c
< &loadcmds
[nloadcmds
])
1229 if (c
->mapend
> c
->mapstart
1230 /* Map the segment contents from the file. */
1231 && (__mmap ((void *) (l
->l_addr
+ c
->mapstart
),
1232 c
->mapend
- c
->mapstart
, c
->prot
,
1233 MAP_FIXED
|MAP_COPY
|MAP_FILE
|MAP_DENYWRITE
,
1239 if (c
->prot
& PROT_EXEC
)
1240 l
->l_text_end
= l
->l_addr
+ c
->mapend
;
1243 && (ElfW(Off
)) c
->mapoff
<= header
->e_phoff
1244 && ((size_t) (c
->mapend
- c
->mapstart
+ c
->mapoff
)
1245 >= header
->e_phoff
+ header
->e_phnum
* sizeof (ElfW(Phdr
))))
1246 /* Found the program header in this segment. */
1247 l
->l_phdr
= (void *) (c
->mapstart
+ header
->e_phoff
- c
->mapoff
);
1249 if (c
->allocend
> c
->dataend
)
1251 /* Extra zero pages should appear at the end of this segment,
1252 after the data mapped from the file. */
1253 ElfW(Addr
) zero
, zeroend
, zeropage
;
1255 zero
= l
->l_addr
+ c
->dataend
;
1256 zeroend
= l
->l_addr
+ c
->allocend
;
1257 zeropage
= ((zero
+ GLRO(dl_pagesize
) - 1)
1258 & ~(GLRO(dl_pagesize
) - 1));
1260 if (zeroend
< zeropage
)
1261 /* All the extra data is in the last page of the segment.
1262 We can just zero it. */
1265 if (zeropage
> zero
)
1267 /* Zero the final part of the last page of the segment. */
1268 if (__builtin_expect ((c
->prot
& PROT_WRITE
) == 0, 0))
1271 if (__mprotect ((caddr_t
) (zero
1272 & ~(GLRO(dl_pagesize
) - 1)),
1273 GLRO(dl_pagesize
), c
->prot
|PROT_WRITE
) < 0)
1275 errstring
= N_("cannot change memory protections");
1276 goto call_lose_errno
;
1279 memset ((void *) zero
, '\0', zeropage
- zero
);
1280 if (__builtin_expect ((c
->prot
& PROT_WRITE
) == 0, 0))
1281 __mprotect ((caddr_t
) (zero
& ~(GLRO(dl_pagesize
) - 1)),
1282 GLRO(dl_pagesize
), c
->prot
);
1285 if (zeroend
> zeropage
)
1287 /* Map the remaining zero pages in from the zero fill FD. */
1289 mapat
= __mmap ((caddr_t
) zeropage
, zeroend
- zeropage
,
1290 c
->prot
, MAP_ANON
|MAP_PRIVATE
|MAP_FIXED
,
1292 if (__builtin_expect (mapat
== MAP_FAILED
, 0))
1294 errstring
= N_("cannot map zero-fill pages");
1295 goto call_lose_errno
;
1306 if (__builtin_expect (type
== ET_DYN
, 0))
1308 errstring
= N_("object file has no dynamic section");
1313 l
->l_ld
= (ElfW(Dyn
) *) ((ElfW(Addr
)) l
->l_ld
+ l
->l_addr
);
1315 elf_get_dynamic_info (l
, NULL
);
1317 /* Make sure we are not dlopen'ing an object that has the
1318 DF_1_NOOPEN flag set. */
1319 if (__builtin_expect (l
->l_flags_1
& DF_1_NOOPEN
, 0)
1320 && (mode
& __RTLD_DLOPEN
))
1322 /* We are not supposed to load this object. Free all resources. */
1323 __munmap ((void *) l
->l_map_start
, l
->l_map_end
- l
->l_map_start
);
1325 if (!l
->l_libname
->dont_free
)
1326 free (l
->l_libname
);
1328 if (l
->l_phdr_allocated
)
1329 free ((void *) l
->l_phdr
);
1331 errstring
= N_("shared object cannot be dlopen()ed");
1335 if (l
->l_phdr
== NULL
)
1337 /* The program header is not contained in any of the segments.
1338 We have to allocate memory ourself and copy it over from out
1340 ElfW(Phdr
) *newp
= (ElfW(Phdr
) *) malloc (header
->e_phnum
1341 * sizeof (ElfW(Phdr
)));
1344 errstring
= N_("cannot allocate memory for program header");
1345 goto call_lose_errno
;
1348 l
->l_phdr
= memcpy (newp
, phdr
,
1349 (header
->e_phnum
* sizeof (ElfW(Phdr
))));
1350 l
->l_phdr_allocated
= 1;
1353 /* Adjust the PT_PHDR value by the runtime load address. */
1354 l
->l_phdr
= (ElfW(Phdr
) *) ((ElfW(Addr
)) l
->l_phdr
+ l
->l_addr
);
1356 if (__builtin_expect ((stack_flags
&~ GL(dl_stack_flags
)) & PF_X
, 0))
1358 /* The stack is presently not executable, but this module
1359 requires that it be executable. We must change the
1360 protection of the variable which contains the flags used in
1361 the mprotect calls. */
1363 if ((mode
& (__RTLD_DLOPEN
| __RTLD_AUDIT
)) == __RTLD_DLOPEN
)
1365 uintptr_t p
= ((uintptr_t) &__stack_prot
) & ~(GLRO(dl_pagesize
) - 1);
1366 size_t s
= (uintptr_t) &__stack_prot
- p
+ sizeof (int);
1368 __mprotect ((void *) p
, s
, PROT_READ
|PROT_WRITE
);
1369 if (__builtin_expect (__check_caller (RETURN_ADDRESS (0),
1372 __stack_prot
|= PROT_READ
|PROT_WRITE
|PROT_EXEC
;
1373 __mprotect ((void *) p
, s
, PROT_READ
);
1377 __stack_prot
|= PROT_READ
|PROT_WRITE
|PROT_EXEC
;
1379 #ifdef check_consistency
1380 check_consistency ();
1383 errval
= (*GL(dl_make_stack_executable_hook
)) (stack_endp
);
1387 cannot enable executable stack as shared object requires");
1393 /* Adjust the address of the TLS initialization image. */
1394 if (l
->l_tls_initimage
!= NULL
)
1395 l
->l_tls_initimage
= (char *) l
->l_tls_initimage
+ l
->l_addr
;
1398 /* We are done mapping in the file. We no longer need the descriptor. */
1399 if (__builtin_expect (__close (fd
) != 0, 0))
1401 errstring
= N_("cannot close file descriptor");
1402 goto call_lose_errno
;
1404 /* Signal that we closed the file. */
1407 if (l
->l_type
== lt_library
&& type
== ET_EXEC
)
1408 l
->l_type
= lt_executable
;
1410 l
->l_entry
+= l
->l_addr
;
1412 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_FILES
, 0))
1413 _dl_debug_printf ("\
1414 dynamic: 0x%0*lx base: 0x%0*lx size: 0x%0*Zx\n\
1415 entry: 0x%0*lx phdr: 0x%0*lx phnum: %*u\n\n",
1416 (int) sizeof (void *) * 2,
1417 (unsigned long int) l
->l_ld
,
1418 (int) sizeof (void *) * 2,
1419 (unsigned long int) l
->l_addr
,
1420 (int) sizeof (void *) * 2, maplength
,
1421 (int) sizeof (void *) * 2,
1422 (unsigned long int) l
->l_entry
,
1423 (int) sizeof (void *) * 2,
1424 (unsigned long int) l
->l_phdr
,
1425 (int) sizeof (void *) * 2, l
->l_phnum
);
1427 /* Set up the symbol hash table. */
1430 /* If this object has DT_SYMBOLIC set modify now its scope. We don't
1431 have to do this for the main map. */
1432 if ((mode
& RTLD_DEEPBIND
) == 0
1433 && __builtin_expect (l
->l_info
[DT_SYMBOLIC
] != NULL
, 0)
1434 && &l
->l_searchlist
!= l
->l_scope
[0])
1436 /* Create an appropriate searchlist. It contains only this map.
1437 This is the definition of DT_SYMBOLIC in SysVr4. */
1438 l
->l_symbolic_searchlist
.r_list
=
1439 (struct link_map
**) malloc (sizeof (struct link_map
*));
1441 if (l
->l_symbolic_searchlist
.r_list
== NULL
)
1443 errstring
= N_("cannot create searchlist");
1444 goto call_lose_errno
;
1447 l
->l_symbolic_searchlist
.r_list
[0] = l
;
1448 l
->l_symbolic_searchlist
.r_nlist
= 1;
1450 /* Now move the existing entries one back. */
1451 memmove (&l
->l_scope
[1], &l
->l_scope
[0],
1452 (l
->l_scope_max
- 1) * sizeof (l
->l_scope
[0]));
1454 /* Now add the new entry. */
1455 l
->l_scope
[0] = &l
->l_symbolic_searchlist
;
1458 /* Remember whether this object must be initialized first. */
1459 if (l
->l_flags_1
& DF_1_INITFIRST
)
1460 GL(dl_initfirst
) = l
;
1462 /* Finally the file information. */
1463 l
->l_dev
= st
.st_dev
;
1464 l
->l_ino
= st
.st_ino
;
1466 /* When we profile the SONAME might be needed for something else but
1467 loading. Add it right away. */
1468 if (__builtin_expect (GLRO(dl_profile
) != NULL
, 0)
1469 && l
->l_info
[DT_SONAME
] != NULL
)
1470 add_name_to_object (l
, ((const char *) D_PTR (l
, l_info
[DT_STRTAB
])
1471 + l
->l_info
[DT_SONAME
]->d_un
.d_val
));
1474 /* Auditing checkpoint: we have a new object. */
1475 if (__builtin_expect (GLRO(dl_naudit
) > 0, 0)
1476 && !GL(dl_ns
)[l
->l_ns
]._ns_loaded
->l_auditing
)
1478 struct audit_ifaces
*afct
= GLRO(dl_audit
);
1479 for (unsigned int cnt
= 0; cnt
< GLRO(dl_naudit
); ++cnt
)
1481 if (afct
->objopen
!= NULL
)
1483 l
->l_audit
[cnt
].bindflags
1484 = afct
->objopen (l
, nsid
, &l
->l_audit
[cnt
].cookie
);
1486 l
->l_audit_any_plt
|= l
->l_audit
[cnt
].bindflags
!= 0;
1497 /* Print search path. */
1499 print_search_path (struct r_search_path_elem
**list
,
1500 const char *what
, const char *name
)
1502 char buf
[max_dirnamelen
+ max_capstrlen
];
1505 _dl_debug_printf (" search path=");
1507 while (*list
!= NULL
&& (*list
)->what
== what
) /* Yes, ==. */
1509 char *endp
= __mempcpy (buf
, (*list
)->dirname
, (*list
)->dirnamelen
);
1512 for (cnt
= 0; cnt
< ncapstr
; ++cnt
)
1513 if ((*list
)->status
[cnt
] != nonexisting
)
1515 char *cp
= __mempcpy (endp
, capstr
[cnt
].str
, capstr
[cnt
].len
);
1516 if (cp
== buf
|| (cp
== buf
+ 1 && buf
[0] == '/'))
1521 _dl_debug_printf_c (first
? "%s" : ":%s", buf
);
1529 _dl_debug_printf_c ("\t\t(%s from file %s)\n", what
,
1530 name
[0] ? name
: rtld_progname
);
1532 _dl_debug_printf_c ("\t\t(%s)\n", what
);
1535 /* Open a file and verify it is an ELF file for this architecture. We
1536 ignore only ELF files for other architectures. Non-ELF files and
1537 ELF files with different header information cause fatal errors since
1538 this could mean there is something wrong in the installation and the
1539 user might want to know about this. */
1541 open_verify (const char *name
, struct filebuf
*fbp
, struct link_map
*loader
,
1544 /* This is the expected ELF header. */
1545 #define ELF32_CLASS ELFCLASS32
1546 #define ELF64_CLASS ELFCLASS64
1547 #ifndef VALID_ELF_HEADER
1548 # define VALID_ELF_HEADER(hdr,exp,size) (memcmp (hdr, exp, size) == 0)
1549 # define VALID_ELF_OSABI(osabi) (osabi == ELFOSABI_SYSV)
1550 # define VALID_ELF_ABIVERSION(ver) (ver == 0)
1552 static const unsigned char expected
[EI_PAD
] =
1554 [EI_MAG0
] = ELFMAG0
,
1555 [EI_MAG1
] = ELFMAG1
,
1556 [EI_MAG2
] = ELFMAG2
,
1557 [EI_MAG3
] = ELFMAG3
,
1558 [EI_CLASS
] = ELFW(CLASS
),
1559 [EI_DATA
] = byteorder
,
1560 [EI_VERSION
] = EV_CURRENT
,
1561 [EI_OSABI
] = ELFOSABI_SYSV
,
1566 ElfW(Word
) vendorlen
;
1570 } expected_note
= { 4, 16, 1, "GNU" };
1571 /* Initialize it to make the compiler happy. */
1572 const char *errstring
= NULL
;
1576 /* Give the auditing libraries a chance. */
1577 if (__builtin_expect (GLRO(dl_naudit
) > 0, 0) && whatcode
!= 0
1578 && loader
->l_auditing
== 0)
1580 struct audit_ifaces
*afct
= GLRO(dl_audit
);
1581 for (unsigned int cnt
= 0; cnt
< GLRO(dl_naudit
); ++cnt
)
1583 if (afct
->objsearch
!= NULL
)
1585 name
= afct
->objsearch (name
, &loader
->l_audit
[cnt
].cookie
,
1588 /* Ignore the path. */
1597 /* Open the file. We always open files read-only. */
1598 int fd
= __open (name
, O_RDONLY
);
1602 ElfW(Phdr
) *phdr
, *ph
;
1603 ElfW(Word
) *abi_note
, abi_note_buf
[8];
1604 unsigned int osversion
;
1607 /* We successfully openened the file. Now verify it is a file
1610 fbp
->len
= __libc_read (fd
, fbp
->buf
, sizeof (fbp
->buf
));
1612 /* This is where the ELF header is loaded. */
1613 assert (sizeof (fbp
->buf
) > sizeof (ElfW(Ehdr
)));
1614 ehdr
= (ElfW(Ehdr
) *) fbp
->buf
;
1616 /* Now run the tests. */
1617 if (__builtin_expect (fbp
->len
< (ssize_t
) sizeof (ElfW(Ehdr
)), 0))
1620 errstring
= (errval
== 0
1621 ? N_("file too short") : N_("cannot read file data"));
1623 lose (errval
, fd
, name
, NULL
, NULL
, errstring
);
1626 /* See whether the ELF header is what we expect. */
1627 if (__builtin_expect (! VALID_ELF_HEADER (ehdr
->e_ident
, expected
,
1630 /* Something is wrong. */
1631 if (*(Elf32_Word
*) &ehdr
->e_ident
!=
1632 #if BYTE_ORDER == LITTLE_ENDIAN
1633 ((ELFMAG0
<< (EI_MAG0
* 8)) |
1634 (ELFMAG1
<< (EI_MAG1
* 8)) |
1635 (ELFMAG2
<< (EI_MAG2
* 8)) |
1636 (ELFMAG3
<< (EI_MAG3
* 8)))
1638 ((ELFMAG0
<< (EI_MAG3
* 8)) |
1639 (ELFMAG1
<< (EI_MAG2
* 8)) |
1640 (ELFMAG2
<< (EI_MAG1
* 8)) |
1641 (ELFMAG3
<< (EI_MAG0
* 8)))
1644 errstring
= N_("invalid ELF header");
1645 else if (ehdr
->e_ident
[EI_CLASS
] != ELFW(CLASS
))
1646 /* This is not a fatal error. On architectures where
1647 32-bit and 64-bit binaries can be run this might
1650 else if (ehdr
->e_ident
[EI_DATA
] != byteorder
)
1652 if (BYTE_ORDER
== BIG_ENDIAN
)
1653 errstring
= N_("ELF file data encoding not big-endian");
1655 errstring
= N_("ELF file data encoding not little-endian");
1657 else if (ehdr
->e_ident
[EI_VERSION
] != EV_CURRENT
)
1659 = N_("ELF file version ident does not match current one");
1660 /* XXX We should be able so set system specific versions which are
1662 else if (!VALID_ELF_OSABI (ehdr
->e_ident
[EI_OSABI
]))
1663 errstring
= N_("ELF file OS ABI invalid");
1664 else if (!VALID_ELF_ABIVERSION (ehdr
->e_ident
[EI_ABIVERSION
]))
1665 errstring
= N_("ELF file ABI version invalid");
1667 /* Otherwise we don't know what went wrong. */
1668 errstring
= N_("internal error");
1673 if (__builtin_expect (ehdr
->e_version
, EV_CURRENT
) != EV_CURRENT
)
1675 errstring
= N_("ELF file version does not match current one");
1678 if (! __builtin_expect (elf_machine_matches_host (ehdr
), 1))
1680 else if (__builtin_expect (ehdr
->e_type
, ET_DYN
) != ET_DYN
1681 && __builtin_expect (ehdr
->e_type
, ET_EXEC
) != ET_EXEC
)
1683 errstring
= N_("only ET_DYN and ET_EXEC can be loaded");
1686 else if (__builtin_expect (ehdr
->e_phentsize
, sizeof (ElfW(Phdr
)))
1687 != sizeof (ElfW(Phdr
)))
1689 errstring
= N_("ELF file's phentsize not the expected size");
1693 maplength
= ehdr
->e_phnum
* sizeof (ElfW(Phdr
));
1694 if (ehdr
->e_phoff
+ maplength
<= (size_t) fbp
->len
)
1695 phdr
= (void *) (fbp
->buf
+ ehdr
->e_phoff
);
1698 phdr
= alloca (maplength
);
1699 __lseek (fd
, ehdr
->e_phoff
, SEEK_SET
);
1700 if ((size_t) __libc_read (fd
, (void *) phdr
, maplength
) != maplength
)
1704 errstring
= N_("cannot read file data");
1709 /* Check .note.ABI-tag if present. */
1710 for (ph
= phdr
; ph
< &phdr
[ehdr
->e_phnum
]; ++ph
)
1711 if (ph
->p_type
== PT_NOTE
&& ph
->p_filesz
== 32 && ph
->p_align
>= 4)
1713 if (ph
->p_offset
+ 32 <= (size_t) fbp
->len
)
1714 abi_note
= (void *) (fbp
->buf
+ ph
->p_offset
);
1717 __lseek (fd
, ph
->p_offset
, SEEK_SET
);
1718 if (__libc_read (fd
, (void *) abi_note_buf
, 32) != 32)
1721 abi_note
= abi_note_buf
;
1724 if (memcmp (abi_note
, &expected_note
, sizeof (expected_note
)))
1727 osversion
= (abi_note
[5] & 0xff) * 65536
1728 + (abi_note
[6] & 0xff) * 256
1729 + (abi_note
[7] & 0xff);
1730 if (abi_note
[4] != __ABI_TAG_OS
1731 || (GLRO(dl_osversion
) && GLRO(dl_osversion
) < osversion
))
1735 __set_errno (ENOENT
);
1746 /* Try to open NAME in one of the directories in *DIRSP.
1747 Return the fd, or -1. If successful, fill in *REALNAME
1748 with the malloc'd full directory name. If it turns out
1749 that none of the directories in *DIRSP exists, *DIRSP is
1750 replaced with (void *) -1, and the old value is free()d
1751 if MAY_FREE_DIRS is true. */
1754 open_path (const char *name
, size_t namelen
, int preloaded
,
1755 struct r_search_path_struct
*sps
, char **realname
,
1756 struct filebuf
*fbp
, struct link_map
*loader
, int whatcode
)
1758 struct r_search_path_elem
**dirs
= sps
->dirs
;
1761 const char *current_what
= NULL
;
1764 buf
= alloca (max_dirnamelen
+ max_capstrlen
+ namelen
);
1767 struct r_search_path_elem
*this_dir
= *dirs
;
1774 /* If we are debugging the search for libraries print the path
1775 now if it hasn't happened now. */
1776 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_LIBS
, 0)
1777 && current_what
!= this_dir
->what
)
1779 current_what
= this_dir
->what
;
1780 print_search_path (dirs
, current_what
, this_dir
->where
);
1783 edp
= (char *) __mempcpy (buf
, this_dir
->dirname
, this_dir
->dirnamelen
);
1784 for (cnt
= 0; fd
== -1 && cnt
< ncapstr
; ++cnt
)
1786 /* Skip this directory if we know it does not exist. */
1787 if (this_dir
->status
[cnt
] == nonexisting
)
1791 ((char *) __mempcpy (__mempcpy (edp
, capstr
[cnt
].str
,
1796 /* Print name we try if this is wanted. */
1797 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_LIBS
, 0))
1798 _dl_debug_printf (" trying file=%s\n", buf
);
1800 fd
= open_verify (buf
, fbp
, loader
, whatcode
);
1801 if (this_dir
->status
[cnt
] == unknown
)
1804 this_dir
->status
[cnt
] = existing
;
1805 /* Do not update the directory information when loading
1806 auditing code. We must try to disturb the program as
1807 little as possible. */
1808 else if (loader
== NULL
1809 || GL(dl_ns
)[loader
->l_ns
]._ns_loaded
->l_audit
== 0)
1811 /* We failed to open machine dependent library. Let's
1812 test whether there is any directory at all. */
1815 buf
[buflen
- namelen
- 1] = '\0';
1817 if (__xstat64 (_STAT_VER
, buf
, &st
) != 0
1818 || ! S_ISDIR (st
.st_mode
))
1819 /* The directory does not exist or it is no directory. */
1820 this_dir
->status
[cnt
] = nonexisting
;
1822 this_dir
->status
[cnt
] = existing
;
1826 /* Remember whether we found any existing directory. */
1827 here_any
|= this_dir
->status
[cnt
] != nonexisting
;
1829 if (fd
!= -1 && __builtin_expect (preloaded
, 0)
1830 && INTUSE(__libc_enable_secure
))
1832 /* This is an extra security effort to make sure nobody can
1833 preload broken shared objects which are in the trusted
1834 directories and so exploit the bugs. */
1837 if (__fxstat64 (_STAT_VER
, fd
, &st
) != 0
1838 || (st
.st_mode
& S_ISUID
) == 0)
1840 /* The shared object cannot be tested for being SUID
1841 or this bit is not set. In this case we must not
1845 /* We simply ignore the file, signal this by setting
1846 the error value which would have been set by `open'. */
1854 *realname
= (char *) malloc (buflen
);
1855 if (*realname
!= NULL
)
1857 memcpy (*realname
, buf
, buflen
);
1862 /* No memory for the name, we certainly won't be able
1863 to load and link it. */
1868 if (here_any
&& (err
= errno
) != ENOENT
&& err
!= EACCES
)
1869 /* The file exists and is readable, but something went wrong. */
1872 /* Remember whether we found anything. */
1875 while (*++dirs
!= NULL
);
1877 /* Remove the whole path if none of the directories exists. */
1878 if (__builtin_expect (! any
, 0))
1880 /* Paths which were allocated using the minimal malloc() in ld.so
1881 must not be freed using the general free() in libc. */
1885 /* rtld_search_dirs is attribute_relro, therefore avoid writing
1887 if (sps
!= &rtld_search_dirs
)
1889 sps
->dirs
= (void *) -1;
1895 /* Map in the shared object file NAME. */
1899 _dl_map_object (struct link_map
*loader
, const char *name
, int preloaded
,
1900 int type
, int trace_mode
, int mode
, Lmid_t nsid
)
1909 assert (nsid
< DL_NNS
);
1911 /* Look for this name among those already loaded. */
1912 for (l
= GL(dl_ns
)[nsid
]._ns_loaded
; l
; l
= l
->l_next
)
1914 /* If the requested name matches the soname of a loaded object,
1915 use that object. Elide this check for names that have not
1917 if (__builtin_expect (l
->l_faked
, 0) != 0)
1919 if (!_dl_name_match_p (name
, l
))
1923 if (__builtin_expect (l
->l_soname_added
, 1)
1924 || l
->l_info
[DT_SONAME
] == NULL
)
1927 soname
= ((const char *) D_PTR (l
, l_info
[DT_STRTAB
])
1928 + l
->l_info
[DT_SONAME
]->d_un
.d_val
);
1929 if (strcmp (name
, soname
) != 0)
1932 /* We have a match on a new name -- cache it. */
1933 add_name_to_object (l
, soname
);
1934 l
->l_soname_added
= 1;
1937 /* We have a match. */
1941 /* Display information if we are debugging. */
1942 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_FILES
, 0)
1944 _dl_debug_printf ("\nfile=%s [%lu]; needed by %s [%lu]\n", name
, nsid
,
1946 ? loader
->l_name
: rtld_progname
, loader
->l_ns
);
1949 /* Give the auditing libraries a chance to change the name before we
1951 if (__builtin_expect (GLRO(dl_naudit
) > 0, 0)
1952 && (loader
== NULL
|| loader
->l_auditing
== 0))
1954 struct audit_ifaces
*afct
= GLRO(dl_audit
);
1955 for (unsigned int cnt
= 0; cnt
< GLRO(dl_naudit
); ++cnt
)
1957 if (afct
->objsearch
!= NULL
)
1959 name
= afct
->objsearch (name
, &loader
->l_audit
[cnt
].cookie
,
1963 /* Do not try anything further. */
1974 if (strchr (name
, '/') == NULL
)
1976 /* Search for NAME in several places. */
1978 size_t namelen
= strlen (name
) + 1;
1980 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_LIBS
, 0))
1981 _dl_debug_printf ("find library=%s [%lu]; searching\n", name
, nsid
);
1985 /* When the object has the RUNPATH information we don't use any
1987 if (loader
== NULL
|| loader
->l_info
[DT_RUNPATH
] == NULL
)
1989 /* First try the DT_RPATH of the dependent object that caused NAME
1990 to be loaded. Then that object's dependent, and on up. */
1991 for (l
= loader
; fd
== -1 && l
; l
= l
->l_loader
)
1992 if (cache_rpath (l
, &l
->l_rpath_dirs
, DT_RPATH
, "RPATH"))
1993 fd
= open_path (name
, namelen
, preloaded
, &l
->l_rpath_dirs
,
1994 &realname
, &fb
, loader
, LA_SER_RUNPATH
);
1996 /* If dynamically linked, try the DT_RPATH of the executable
1997 itself. NB: we do this for lookups in any namespace. */
2000 l
= GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
;
2001 if (l
&& l
->l_type
!= lt_loaded
&& l
!= loader
2002 && cache_rpath (l
, &l
->l_rpath_dirs
, DT_RPATH
, "RPATH"))
2003 fd
= open_path (name
, namelen
, preloaded
, &l
->l_rpath_dirs
,
2004 &realname
, &fb
, loader
?: l
, LA_SER_RUNPATH
);
2008 /* Try the LD_LIBRARY_PATH environment variable. */
2009 if (fd
== -1 && env_path_list
.dirs
!= (void *) -1)
2010 fd
= open_path (name
, namelen
, preloaded
, &env_path_list
,
2012 loader
?: GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
,
2015 /* Look at the RUNPATH information for this binary. */
2016 if (fd
== -1 && loader
!= NULL
2017 && cache_rpath (loader
, &loader
->l_runpath_dirs
,
2018 DT_RUNPATH
, "RUNPATH"))
2019 fd
= open_path (name
, namelen
, preloaded
,
2020 &loader
->l_runpath_dirs
, &realname
, &fb
, loader
,
2024 && (__builtin_expect (! preloaded
, 1)
2025 || ! INTUSE(__libc_enable_secure
)))
2027 /* Check the list of libraries in the file /etc/ld.so.cache,
2028 for compatibility with Linux's ldconfig program. */
2029 const char *cached
= _dl_load_cache_lookup (name
);
2034 // XXX Correct to unconditionally default to namespace 0?
2035 l
= loader
?: GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
;
2040 /* If the loader has the DF_1_NODEFLIB flag set we must not
2041 use a cache entry from any of these directories. */
2044 /* 'l' is always != NULL for dynamically linked objects. */
2047 __builtin_expect (l
->l_flags_1
& DF_1_NODEFLIB
, 0))
2049 const char *dirp
= system_dirs
;
2050 unsigned int cnt
= 0;
2054 if (memcmp (cached
, dirp
, system_dirs_len
[cnt
]) == 0)
2056 /* The prefix matches. Don't use the entry. */
2061 dirp
+= system_dirs_len
[cnt
] + 1;
2064 while (cnt
< nsystem_dirs_len
);
2069 fd
= open_verify (cached
,
2070 &fb
, loader
?: GL(dl_ns
)[nsid
]._ns_loaded
,
2072 if (__builtin_expect (fd
!= -1, 1))
2074 realname
= local_strdup (cached
);
2075 if (realname
== NULL
)
2085 /* Finally, try the default path. */
2087 && ((l
= loader
?: GL(dl_ns
)[nsid
]._ns_loaded
) == NULL
2088 || __builtin_expect (!(l
->l_flags_1
& DF_1_NODEFLIB
), 1))
2089 && rtld_search_dirs
.dirs
!= (void *) -1)
2090 fd
= open_path (name
, namelen
, preloaded
, &rtld_search_dirs
,
2091 &realname
, &fb
, l
, LA_SER_DEFAULT
);
2093 /* Add another newline when we are tracing the library loading. */
2094 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_LIBS
, 0))
2095 _dl_debug_printf ("\n");
2099 /* The path may contain dynamic string tokens. */
2101 ? expand_dynamic_string_token (loader
, name
)
2102 : local_strdup (name
));
2103 if (realname
== NULL
)
2107 fd
= open_verify (realname
, &fb
,
2108 loader
?: GL(dl_ns
)[nsid
]._ns_loaded
, 0);
2109 if (__builtin_expect (fd
, 0) == -1)
2117 /* In case the LOADER information has only been provided to get to
2118 the appropriate RUNPATH/RPATH information we do not need it
2120 if (mode
& __RTLD_CALLMAP
)
2123 if (__builtin_expect (fd
, 0) == -1)
2126 && __builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_PRELINK
, 0) == 0)
2128 /* We haven't found an appropriate library. But since we
2129 are only interested in the list of libraries this isn't
2130 so severe. Fake an entry with all the information we
2132 static const Elf_Symndx dummy_bucket
= STN_UNDEF
;
2134 /* Enter the new object in the list of loaded objects. */
2135 if ((name_copy
= local_strdup (name
)) == NULL
2136 || (l
= _dl_new_object (name_copy
, name
, type
, loader
,
2137 mode
, nsid
)) == NULL
)
2138 _dl_signal_error (ENOMEM
, name
, NULL
,
2139 N_("cannot create shared object descriptor"));
2140 /* Signal that this is a faked entry. */
2142 /* Since the descriptor is initialized with zero we do not
2144 l->l_reserved = 0; */
2145 l
->l_buckets
= &dummy_bucket
;
2152 _dl_signal_error (errno
, name
, NULL
,
2153 N_("cannot open shared object file"));
2156 void *stack_end
= __libc_stack_end
;
2157 return _dl_map_object_from_fd (name
, fd
, &fb
, realname
, loader
, type
, mode
,
2164 _dl_rtld_di_serinfo (struct link_map
*loader
, Dl_serinfo
*si
, bool counting
)
2172 unsigned int idx
= 0;
2173 char *allocptr
= (char *) &si
->dls_serpath
[si
->dls_cnt
];
2174 void add_path (const struct r_search_path_struct
*sps
, unsigned int flags
)
2175 # define add_path(sps, flags) add_path(sps, 0) /* XXX */
2177 if (sps
->dirs
!= (void *) -1)
2179 struct r_search_path_elem
**dirs
= sps
->dirs
;
2182 const struct r_search_path_elem
*const r
= *dirs
++;
2186 si
->dls_size
+= r
->dirnamelen
;
2190 Dl_serpath
*const sp
= &si
->dls_serpath
[idx
++];
2191 sp
->dls_name
= allocptr
;
2192 allocptr
= __mempcpy (allocptr
,
2193 r
->dirname
, r
->dirnamelen
- 1);
2195 sp
->dls_flags
= flags
;
2198 while (*dirs
!= NULL
);
2202 /* When the object has the RUNPATH information we don't use any RPATHs. */
2203 if (loader
->l_info
[DT_RUNPATH
] == NULL
)
2205 /* First try the DT_RPATH of the dependent object that caused NAME
2206 to be loaded. Then that object's dependent, and on up. */
2208 struct link_map
*l
= loader
;
2211 if (cache_rpath (l
, &l
->l_rpath_dirs
, DT_RPATH
, "RPATH"))
2212 add_path (&l
->l_rpath_dirs
, XXX_RPATH
);
2217 /* If dynamically linked, try the DT_RPATH of the executable itself. */
2218 if (loader
->l_ns
== LM_ID_BASE
)
2220 l
= GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
;
2221 if (l
!= NULL
&& l
->l_type
!= lt_loaded
&& l
!= loader
)
2222 if (cache_rpath (l
, &l
->l_rpath_dirs
, DT_RPATH
, "RPATH"))
2223 add_path (&l
->l_rpath_dirs
, XXX_RPATH
);
2227 /* Try the LD_LIBRARY_PATH environment variable. */
2228 add_path (&env_path_list
, XXX_ENV
);
2230 /* Look at the RUNPATH information for this binary. */
2231 if (cache_rpath (loader
, &loader
->l_runpath_dirs
, DT_RUNPATH
, "RUNPATH"))
2232 add_path (&loader
->l_runpath_dirs
, XXX_RUNPATH
);
2235 Here is where ld.so.cache gets checked, but we don't have
2236 a way to indicate that in the results for Dl_serinfo. */
2238 /* Finally, try the default path. */
2239 if (!(loader
->l_flags_1
& DF_1_NODEFLIB
))
2240 add_path (&rtld_search_dirs
, XXX_default
);
2243 /* Count the struct size before the string area, which we didn't
2244 know before we completed dls_cnt. */
2245 si
->dls_size
+= (char *) &si
->dls_serpath
[si
->dls_cnt
] - (char *) si
;