1 /* _dl_map_object -- Map in a shared object's segments from the file.
2 Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
21 #include <sys/types.h>
28 #include "dynamic-link.h"
31 /* On some systems, no flag bits are given to specify file mapping. */
36 /* The right way to map in the shared library files is MAP_COPY, which
37 makes a virtual copy of the data at the time of the mmap call; this
38 guarantees the mapped pages will be consistent even if the file is
39 overwritten. Some losing VM systems like Linux's lack MAP_COPY. All we
40 get is MAP_PRIVATE, which copies each page when it is modified; this
41 means if the file is overwritten, we may at some point get some pages
42 from the new version after starting with pages from the old version. */
44 #define MAP_COPY MAP_PRIVATE
49 #if BYTE_ORDER == BIG_ENDIAN
50 #define byteorder ELFDATA2MSB
51 #define byteorder_name "big-endian"
52 #elif BYTE_ORDER == LITTLE_ENDIAN
53 #define byteorder ELFDATA2LSB
54 #define byteorder_name "little-endian"
56 #error "Unknown BYTE_ORDER " BYTE_ORDER
57 #define byteorder ELFDATANONE
63 /* The fd is not examined when using MAP_ANON. */
67 #define ANONFD _dl_zerofd
73 /* Local version of `strdup' function. */
75 local_strdup (const char *s
)
77 size_t len
= strlen (s
) + 1;
78 void *new = malloc (len
);
83 return (char *) memcpy (new, s
, len
);
87 /* Map in the shared object NAME, actually located in REALNAME, and already
91 _dl_map_object_from_fd (char *name
, int fd
, char *realname
,
92 struct link_map
*loader
, int l_type
)
94 struct link_map
*l
= NULL
;
95 void *file_mapping
= NULL
;
96 size_t mapping_size
= 0;
98 #define LOSE(s) lose (0, (s))
99 void lose (int code
, const char *msg
)
103 __munmap (file_mapping
, mapping_size
);
106 /* Remove the stillborn object from the list and free it. */
108 l
->l_prev
->l_next
= l
->l_next
;
110 l
->l_next
->l_prev
= l
->l_prev
;
115 _dl_signal_error (code
, name
, msg
);
118 inline caddr_t
map_segment (ElfW(Addr
) mapstart
, size_t len
,
119 int prot
, int fixed
, off_t offset
)
121 caddr_t mapat
= __mmap ((caddr_t
) mapstart
, len
, prot
,
122 fixed
|MAP_COPY
|MAP_FILE
,
124 if (mapat
== (caddr_t
) -1)
125 lose (errno
, "failed to map segment from shared object");
129 /* Make sure LOCATION is mapped in. */
130 void *map (off_t location
, size_t size
)
132 if ((off_t
) mapping_size
<= location
+ (off_t
) size
)
136 __munmap (file_mapping
, mapping_size
);
137 mapping_size
= (location
+ size
+ 1 + _dl_pagesize
- 1);
138 mapping_size
&= ~(_dl_pagesize
- 1);
139 result
= __mmap (file_mapping
, mapping_size
, PROT_READ
,
140 MAP_COPY
|MAP_FILE
, fd
, 0);
141 if (result
== (void *) -1)
142 lose (errno
, "cannot map file data");
143 file_mapping
= result
;
145 return file_mapping
+ location
;
148 const ElfW(Ehdr
) *header
;
149 const ElfW(Phdr
) *phdr
;
150 const ElfW(Phdr
) *ph
;
153 /* Look again to see if the real name matched another already loaded. */
154 for (l
= _dl_loaded
; l
; l
= l
->l_next
)
155 if (! strcmp (realname
, l
->l_name
))
157 struct libname_list
*lnp
, *lastp
;
158 /* The object is already loaded.
159 Just bump its reference count and return it. */
162 /* If the name is not in the list of names for this object add
166 for (lnp
= l
->l_libname
; lnp
!= NULL
; lastp
= lnp
, lnp
= lnp
->next
)
167 if (strcmp (name
, lnp
->name
) == 0)
174 struct libname_list
*newname
= malloc (sizeof *newname
);
176 /* No more memory. */
177 lose (ENOMEM
, "cannot allocate name record");
178 /* The object should have a libname set. */
179 assert (lastp
!= NULL
);
181 newname
->name
= name
;
182 newname
->next
= NULL
;
183 lastp
->next
= newname
;
189 if (_dl_pagesize
== 0)
190 _dl_pagesize
= __getpagesize ();
192 /* Map in the first page to read the header. */
193 header
= map (0, sizeof *header
);
195 /* Check the header for basic validity. */
196 if (*(Elf32_Word
*) &header
->e_ident
!=
197 #if BYTE_ORDER == LITTLE_ENDIAN
198 ((ELFMAG0
<< (EI_MAG0
* 8)) |
199 (ELFMAG1
<< (EI_MAG1
* 8)) |
200 (ELFMAG2
<< (EI_MAG2
* 8)) |
201 (ELFMAG3
<< (EI_MAG3
* 8)))
203 ((ELFMAG0
<< (EI_MAG3
* 8)) |
204 (ELFMAG1
<< (EI_MAG2
* 8)) |
205 (ELFMAG2
<< (EI_MAG1
* 8)) |
206 (ELFMAG3
<< (EI_MAG0
* 8)))
209 LOSE ("invalid ELF header");
210 #define ELF32_CLASS ELFCLASS32
211 #define ELF64_CLASS ELFCLASS64
212 if (header
->e_ident
[EI_CLASS
] != ELFW(CLASS
))
213 LOSE ("ELF file class not " STRING(__ELF_WORDSIZE
) "-bit");
214 if (header
->e_ident
[EI_DATA
] != byteorder
)
215 LOSE ("ELF file data encoding not " byteorder_name
);
216 if (header
->e_ident
[EI_VERSION
] != EV_CURRENT
)
217 LOSE ("ELF file version ident not " STRING(EV_CURRENT
));
218 if (header
->e_version
!= EV_CURRENT
)
219 LOSE ("ELF file version not " STRING(EV_CURRENT
));
220 if (! elf_machine_matches_host (header
->e_machine
))
221 LOSE ("ELF file machine architecture not " ELF_MACHINE_NAME
);
222 if (header
->e_phentsize
!= sizeof (ElfW(Phdr
)))
223 LOSE ("ELF file's phentsize not the expected size");
227 if (_dl_zerofd
== -1)
229 _dl_zerofd
= _dl_sysdep_open_zero_fill ();
230 if (_dl_zerofd
== -1)
233 _dl_signal_error (errno
, NULL
, "cannot open zero fill device");
238 /* Enter the new object in the list of loaded objects. */
239 l
= _dl_new_object (realname
, name
, l_type
);
241 lose (ENOMEM
, "cannot create shared object descriptor");
243 l
->l_loader
= loader
;
245 /* Extract the remaining details we need from the ELF header
246 and then map in the program header table. */
247 l
->l_entry
= header
->e_entry
;
248 type
= header
->e_type
;
249 l
->l_phnum
= header
->e_phnum
;
250 phdr
= map (header
->e_phoff
, l
->l_phnum
* sizeof (ElfW(Phdr
)));
253 /* Scan the program header table, collecting its load commands. */
256 ElfW(Addr
) mapstart
, mapend
, dataend
, allocend
;
259 } loadcmds
[l
->l_phnum
], *c
;
260 size_t nloadcmds
= 0;
265 for (ph
= phdr
; ph
< &phdr
[l
->l_phnum
]; ++ph
)
268 /* These entries tell us where to find things once the file's
269 segments are mapped in. We record the addresses it says
270 verbatim, and later correct for the run-time load address. */
272 l
->l_ld
= (void *) ph
->p_vaddr
;
275 l
->l_phdr
= (void *) ph
->p_vaddr
;
279 /* A load command tells us to map in part of the file.
280 We record the load commands and process them all later. */
281 if (ph
->p_align
% _dl_pagesize
!= 0)
282 LOSE ("ELF load command alignment not page-aligned");
283 if ((ph
->p_vaddr
- ph
->p_offset
) % ph
->p_align
)
284 LOSE ("ELF load command address/offset not properly aligned");
286 struct loadcmd
*c
= &loadcmds
[nloadcmds
++];
287 c
->mapstart
= ph
->p_vaddr
& ~(ph
->p_align
- 1);
288 c
->mapend
= ((ph
->p_vaddr
+ ph
->p_filesz
+ _dl_pagesize
- 1)
289 & ~(_dl_pagesize
- 1));
290 c
->dataend
= ph
->p_vaddr
+ ph
->p_filesz
;
291 c
->allocend
= ph
->p_vaddr
+ ph
->p_memsz
;
292 c
->mapoff
= ph
->p_offset
& ~(ph
->p_align
- 1);
294 if (ph
->p_flags
& PF_R
)
295 c
->prot
|= PROT_READ
;
296 if (ph
->p_flags
& PF_W
)
297 c
->prot
|= PROT_WRITE
;
298 if (ph
->p_flags
& PF_X
)
299 c
->prot
|= PROT_EXEC
;
304 /* We are done reading the file's headers now. Unmap them. */
305 __munmap (file_mapping
, mapping_size
);
307 /* Now process the load commands and map segments into memory. */
310 if (type
== ET_DYN
|| type
== ET_REL
)
312 /* This is a position-independent shared object. We can let the
313 kernel map it anywhere it likes, but we must have space for all
314 the segments in their specified positions relative to the first.
315 So we map the first segment without MAP_FIXED, but with its
316 extent increased to cover all the segments. Then we remove
317 access from excess portion, and there is known sufficient space
318 there to remap from the later segments. */
320 mapat
= map_segment (c
->mapstart
,
321 loadcmds
[nloadcmds
- 1].allocend
- c
->mapstart
,
322 c
->prot
, 0, c
->mapoff
);
323 l
->l_addr
= (ElfW(Addr
)) mapat
- c
->mapstart
;
325 /* Change protection on the excess portion to disallow all access;
326 the portions we do not remap later will be inaccessible as if
327 unallocated. Then jump into the normal segment-mapping loop to
328 handle the portion of the segment past the end of the file
330 __mprotect ((caddr_t
) (l
->l_addr
+ c
->mapend
),
331 loadcmds
[nloadcmds
- 1].allocend
- c
->mapend
,
336 while (c
< &loadcmds
[nloadcmds
])
338 if (c
->mapend
> c
->mapstart
)
339 /* Map the segment contents from the file. */
340 map_segment (l
->l_addr
+ c
->mapstart
, c
->mapend
- c
->mapstart
,
341 c
->prot
, MAP_FIXED
, c
->mapoff
);
344 if (c
->allocend
> c
->dataend
)
346 /* Extra zero pages should appear at the end of this segment,
347 after the data mapped from the file. */
348 ElfW(Addr
) zero
, zeroend
, zeropage
;
350 zero
= l
->l_addr
+ c
->dataend
;
351 zeroend
= l
->l_addr
+ c
->allocend
;
352 zeropage
= (zero
+ _dl_pagesize
- 1) & ~(_dl_pagesize
- 1);
354 if (zeroend
< zeropage
)
355 /* All the extra data is in the last page of the segment.
356 We can just zero it. */
361 /* Zero the final part of the last page of the segment. */
362 if ((c
->prot
& PROT_WRITE
) == 0)
365 if (__mprotect ((caddr_t
) (zero
& ~(_dl_pagesize
- 1)),
366 _dl_pagesize
, c
->prot
|PROT_WRITE
) < 0)
367 lose (errno
, "cannot change memory protections");
369 memset ((void *) zero
, 0, zeropage
- zero
);
370 if ((c
->prot
& PROT_WRITE
) == 0)
371 __mprotect ((caddr_t
) (zero
& ~(_dl_pagesize
- 1)),
372 _dl_pagesize
, c
->prot
);
375 if (zeroend
> zeropage
)
377 /* Map the remaining zero pages in from the zero fill FD. */
379 mapat
= __mmap ((caddr_t
) zeropage
, zeroend
- zeropage
,
380 c
->prot
, MAP_ANON
|MAP_PRIVATE
|MAP_FIXED
,
382 if (mapat
== (caddr_t
) -1)
383 lose (errno
, "cannot map zero-fill pages");
392 /* There was no PT_PHDR specified. We need to find the phdr in the
393 load image ourselves. We assume it is in fact in the load image
394 somewhere, and that the first load command starts at the
395 beginning of the file and thus contains the ELF file header. */
396 ElfW(Addr
) bof
= l
->l_addr
+ loadcmds
[0].mapstart
;
397 assert (loadcmds
[0].mapoff
== 0);
398 l
->l_phdr
= (void *) (bof
+ ((const ElfW(Ehdr
) *) bof
)->e_phoff
);
401 /* Adjust the PT_PHDR value by the runtime load address. */
402 (ElfW(Addr
)) l
->l_phdr
+= l
->l_addr
;
405 /* We are done mapping in the file. We no longer need the descriptor. */
408 if (l
->l_type
== lt_library
&& type
== ET_EXEC
)
409 l
->l_type
= lt_executable
;
414 LOSE ("object file has no dynamic section");
417 (ElfW(Addr
)) l
->l_ld
+= l
->l_addr
;
419 l
->l_entry
+= l
->l_addr
;
421 elf_get_dynamic_info (l
->l_ld
, l
->l_info
);
422 if (l
->l_info
[DT_HASH
])
428 /* Try to open NAME in one of the directories in DIRPATH.
429 Return the fd, or -1. If successful, fill in *REALNAME
430 with the malloc'd full directory name. */
433 open_path (const char *name
, size_t namelen
,
436 const char *trusted_dirs
[])
443 if (p
== NULL
|| *p
== '\0')
445 __set_errno (ENOENT
);
449 buf
= __alloca (strlen (dirpath
) + 1 + namelen
);
456 p
= strpbrk (dirpath
, ":;");
458 p
= strchr (dirpath
, '\0');
460 this_len
= p
- dirpath
;
462 /* When we run a setuid program we do not accept any directory. */
463 if (__libc_enable_secure
)
465 /* All trusted directory must be complete name. */
466 if (dirpath
[0] != '/')
469 /* If we got a list of trusted directories only accept one
471 if (trusted_dirs
!= NULL
)
473 const char **trust
= trusted_dirs
;
475 while (*trust
!= NULL
)
476 if (memcmp (dirpath
, *trust
, this_len
) == 0
477 && (*trust
)[this_len
] == '\0')
482 /* If directory is not trusted, ignore this directory. */
490 /* Two adjacent colons, or a colon at the beginning or the end of
491 the path means to search the current directory. */
492 (void) memcpy (buf
, name
, namelen
);
497 /* Construct the pathname to try. */
498 (void) memcpy (buf
, dirpath
, this_len
);
500 (void) memcpy (&buf
[this_len
+ 1], name
, namelen
);
501 buflen
= this_len
+ 1 + namelen
;
504 fd
= __open (buf
, O_RDONLY
);
507 *realname
= malloc (buflen
);
510 memcpy (*realname
, buf
, buflen
);
515 /* No memory for the name, we certainly won't be able
516 to load and link it. */
521 if (errno
!= ENOENT
&& errno
!= EACCES
)
522 /* The file exists and is readable, but something went wrong. */
525 while (*p
++ != '\0');
530 /* Map in the shared object file NAME. */
533 _dl_map_object (struct link_map
*loader
, const char *name
, int type
,
541 /* Look for this name among those already loaded. */
542 for (l
= _dl_loaded
; l
; l
= l
->l_next
)
543 if (_dl_does_name_match_p (name
, l
) ||
544 /* If the requested name matches the soname of a loaded object,
546 (l
->l_info
[DT_SONAME
] &&
547 ! strcmp (name
, (const char *) (l
->l_addr
+
548 l
->l_info
[DT_STRTAB
]->d_un
.d_ptr
+
549 l
->l_info
[DT_SONAME
]->d_un
.d_val
))))
551 /* The object is already loaded.
552 Just bump its reference count and return it. */
557 if (strchr (name
, '/') == NULL
)
559 /* Search for NAME in several places. */
561 size_t namelen
= strlen (name
) + 1;
563 inline void trypath (const char *dirpath
, const char *trusted
[])
565 fd
= open_path (name
, namelen
, dirpath
, &realname
, trusted
);
570 /* First try the DT_RPATH of the dependent object that caused NAME
571 to be loaded. Then that object's dependent, and on up. */
572 for (l
= loader
; fd
== -1 && l
; l
= l
->l_loader
)
573 if (l
&& l
->l_info
[DT_RPATH
])
574 trypath ((const char *) (l
->l_addr
+
575 l
->l_info
[DT_STRTAB
]->d_un
.d_ptr
+
576 l
->l_info
[DT_RPATH
]->d_un
.d_val
), NULL
);
577 /* If dynamically linked, try the DT_RPATH of the executable itself. */
579 if (fd
== -1 && l
&& l
->l_type
!= lt_loaded
&& l
->l_info
[DT_RPATH
])
580 trypath ((const char *) (l
->l_addr
+
581 l
->l_info
[DT_STRTAB
]->d_un
.d_ptr
+
582 l
->l_info
[DT_RPATH
]->d_un
.d_val
), NULL
);
583 /* Try an environment variable (unless setuid). */
586 static const char *trusted_dirs
[] =
588 #include "trusted-dirs.h"
591 const char *ld_library_path
= getenv ("LD_LIBRARY_PATH");
593 if (ld_library_path
!= NULL
&& *ld_library_path
!= '\0')
594 trypath (ld_library_path
, trusted_dirs
);
598 /* Check the list of libraries in the file /etc/ld.so.cache,
599 for compatibility with Linux's ldconfig program. */
600 extern const char *_dl_load_cache_lookup (const char *name
);
601 const char *cached
= _dl_load_cache_lookup (name
);
604 fd
= __open (cached
, O_RDONLY
);
607 realname
= local_strdup (cached
);
608 if (realname
== NULL
)
616 /* Finally, try the default path. */
619 extern const char *_dl_rpath
; /* Set in rtld.c. */
620 trypath (_dl_rpath
, NULL
);
625 fd
= __open (name
, O_RDONLY
);
628 realname
= local_strdup (name
);
629 if (realname
== NULL
)
639 name_copy
= local_strdup (name
);
640 if (name_copy
== NULL
)
651 /* We haven't found an appropriate library. But since we
652 are only interested in the list of libraries this isn't
653 so severe. Fake an entry with all the information we
655 static const ElfW(Symndx
) dummy_bucket
= STN_UNDEF
;
657 /* Enter the new object in the list of loaded objects. */
658 if ((name_copy
= local_strdup (name
)) == NULL
659 || (l
= _dl_new_object (name_copy
, name
, type
)) == NULL
)
660 _dl_signal_error (ENOMEM
, name
,
661 "cannot create shared object descriptor");
662 /* We use an opencount of 0 as a sign for the faked entry. */
665 l
->l_buckets
= &dummy_bucket
;
672 _dl_signal_error (errno
, name
, "cannot open shared object file");
675 return _dl_map_object_from_fd (name_copy
, fd
, realname
, loader
, type
);