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
47 /* Some systems link their relocatable objects for another base address
48 than 0. We want to know the base address for these such that we can
49 subtract this address from the segment addresses during mapping.
50 This results in a more efficient address space usage. Defaults to
51 zero for almost all systems. */
53 #define MAP_BASE_ADDR(l) 0
58 #if BYTE_ORDER == BIG_ENDIAN
59 #define byteorder ELFDATA2MSB
60 #define byteorder_name "big-endian"
61 #elif BYTE_ORDER == LITTLE_ENDIAN
62 #define byteorder ELFDATA2LSB
63 #define byteorder_name "little-endian"
65 #error "Unknown BYTE_ORDER " BYTE_ORDER
66 #define byteorder ELFDATANONE
72 /* The fd is not examined when using MAP_ANON. */
76 #define ANONFD _dl_zerofd
79 /* Handle situations where we have a preferred location in memory for
80 the shared objects. */
81 #ifdef ELF_PREFERRED_ADDRESS_DATA
82 ELF_PREFERRED_ADDRESS_DATA;
84 #ifndef ELF_PREFERRED_ADDRESS
85 #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
87 #ifndef ELF_FIXED_ADDRESS
88 #define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
94 /* Local version of `strdup' function. */
96 local_strdup (const char *s)
98 size_t len = strlen (s) + 1;
99 void *new = malloc (len);
104 return (char *) memcpy (new, s, len);
108 /* Map in the shared object NAME, actually located in REALNAME, and already
112 _dl_map_object_from_fd (char *name, int fd, char *realname,
113 struct link_map *loader, int l_type)
115 struct link_map *l = NULL;
116 void *file_mapping = NULL;
117 size_t mapping_size = 0;
119 #define LOSE(s) lose (0, (s))
120 void lose (int code, const char *msg)
124 __munmap (file_mapping, mapping_size);
127 /* Remove the stillborn object from the list and free it. */
129 l->l_prev->l_next = l->l_next;
131 l->l_next->l_prev = l->l_prev;
136 _dl_signal_error (code, name, msg);
139 inline caddr_t map_segment (ElfW(Addr) mapstart, size_t len,
140 int prot, int fixed, off_t offset)
142 caddr_t mapat = __mmap ((caddr_t) mapstart, len, prot,
143 fixed|MAP_COPY|MAP_FILE,
145 if (mapat == (caddr_t) -1)
146 lose (errno, "failed to map segment from shared object");
150 /* Make sure LOCATION is mapped in. */
151 void *map (off_t location, size_t size)
153 if ((off_t) mapping_size <= location + (off_t) size)
157 __munmap (file_mapping, mapping_size);
158 mapping_size = (location + size + 1 + _dl_pagesize - 1);
159 mapping_size &= ~(_dl_pagesize - 1);
160 result = __mmap (file_mapping, mapping_size, PROT_READ,
161 MAP_COPY|MAP_FILE, fd, 0);
162 if (result == (void *) -1)
163 lose (errno, "cannot map file data");
164 file_mapping = result;
166 return file_mapping + location;
169 const ElfW(Ehdr) *header;
170 const ElfW(Phdr) *phdr;
171 const ElfW(Phdr) *ph;
174 /* Look again to see if the real name matched another already loaded. */
175 for (l = _dl_loaded; l; l = l->l_next)
176 if (! strcmp (realname, l->l_name))
178 struct libname_list *lnp, *lastp;
179 /* The object is already loaded.
180 Just bump its reference count and return it. */
183 /* If the name is not in the list of names for this object add
187 for (lnp = l->l_libname; lnp != NULL; lastp = lnp, lnp = lnp->next)
188 if (strcmp (name, lnp->name) == 0)
195 struct libname_list *newname = malloc (sizeof *newname);
197 /* No more memory. */
198 lose (ENOMEM, "cannot allocate name record");
199 /* The object should have a libname set. */
200 assert (lastp != NULL);
202 newname->name = name;
203 newname->next = NULL;
204 lastp->next = newname;
210 if (_dl_pagesize == 0)
211 _dl_pagesize = __getpagesize ();
213 /* Map in the first page to read the header. */
214 header = map (0, sizeof *header);
216 /* Check the header for basic validity. */
217 if (*(Elf32_Word *) &header->e_ident !=
218 #if BYTE_ORDER == LITTLE_ENDIAN
219 ((ELFMAG0 << (EI_MAG0 * 8)) |
220 (ELFMAG1 << (EI_MAG1 * 8)) |
221 (ELFMAG2 << (EI_MAG2 * 8)) |
222 (ELFMAG3 << (EI_MAG3 * 8)))
224 ((ELFMAG0 << (EI_MAG3 * 8)) |
225 (ELFMAG1 << (EI_MAG2 * 8)) |
226 (ELFMAG2 << (EI_MAG1 * 8)) |
227 (ELFMAG3 << (EI_MAG0 * 8)))
230 LOSE ("invalid ELF header");
231 #define ELF32_CLASS ELFCLASS32
232 #define ELF64_CLASS ELFCLASS64
233 if (header->e_ident[EI_CLASS] != ELFW(CLASS))
234 LOSE ("ELF file class not " STRING(__ELF_WORDSIZE) "-bit");
235 if (header->e_ident[EI_DATA] != byteorder)
236 LOSE ("ELF file data encoding not " byteorder_name);
237 if (header->e_ident[EI_VERSION] != EV_CURRENT)
238 LOSE ("ELF file version ident not " STRING(EV_CURRENT));
239 if (header->e_version != EV_CURRENT)
240 LOSE ("ELF file version not " STRING(EV_CURRENT));
241 if (! elf_machine_matches_host (header->e_machine))
242 LOSE ("ELF file machine architecture not " ELF_MACHINE_NAME);
243 if (header->e_phentsize != sizeof (ElfW(Phdr)))
244 LOSE ("ELF file's phentsize not the expected size");
248 if (_dl_zerofd == -1)
250 _dl_zerofd = _dl_sysdep_open_zero_fill ();
251 if (_dl_zerofd == -1)
254 _dl_signal_error (errno, NULL, "cannot open zero fill device");
259 /* Enter the new object in the list of loaded objects. */
260 l = _dl_new_object (realname, name, l_type);
262 lose (ENOMEM, "cannot create shared object descriptor");
264 l->l_loader = loader;
266 /* Extract the remaining details we need from the ELF header
267 and then map in the program header table. */
268 l->l_entry = header->e_entry;
269 type = header->e_type;
270 l->l_phnum = header->e_phnum;
271 phdr = map (header->e_phoff, l->l_phnum * sizeof (ElfW(Phdr)));
274 /* Scan the program header table, collecting its load commands. */
277 ElfW(Addr) mapstart, mapend, dataend, allocend;
280 } loadcmds[l->l_phnum], *c;
281 size_t nloadcmds = 0;
286 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
289 /* These entries tell us where to find things once the file's
290 segments are mapped in. We record the addresses it says
291 verbatim, and later correct for the run-time load address. */
293 l->l_ld = (void *) ph->p_vaddr;
296 l->l_phdr = (void *) ph->p_vaddr;
300 /* A load command tells us to map in part of the file.
301 We record the load commands and process them all later. */
302 if (ph->p_align % _dl_pagesize != 0)
303 LOSE ("ELF load command alignment not page-aligned");
304 if ((ph->p_vaddr - ph->p_offset) % ph->p_align)
305 LOSE ("ELF load command address/offset not properly aligned");
307 struct loadcmd *c = &loadcmds[nloadcmds++];
308 c->mapstart = ph->p_vaddr & ~(ph->p_align - 1);
309 c->mapend = ((ph->p_vaddr + ph->p_filesz + _dl_pagesize - 1)
310 & ~(_dl_pagesize - 1));
311 c->dataend = ph->p_vaddr + ph->p_filesz;
312 c->allocend = ph->p_vaddr + ph->p_memsz;
313 c->mapoff = ph->p_offset & ~(ph->p_align - 1);
315 if (ph->p_flags & PF_R)
316 c->prot |= PROT_READ;
317 if (ph->p_flags & PF_W)
318 c->prot |= PROT_WRITE;
319 if (ph->p_flags & PF_X)
320 c->prot |= PROT_EXEC;
325 /* We are done reading the file's headers now. Unmap them. */
326 __munmap (file_mapping, mapping_size);
328 /* Now process the load commands and map segments into memory. */
331 if (type == ET_DYN || type == ET_REL)
333 /* This is a position-independent shared object. We can let the
334 kernel map it anywhere it likes, but we must have space for all
335 the segments in their specified positions relative to the first.
336 So we map the first segment without MAP_FIXED, but with its
337 extent increased to cover all the segments. Then we remove
338 access from excess portion, and there is known sufficient space
339 there to remap from the later segments.
341 As a refinement, sometimes we have an address that we would
342 prefer to map such objects at; but this is only a preference,
343 the OS can do whatever it likes. */
346 size_t maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart;
347 mappref = (ELF_PREFERRED_ADDRESS (loader, maplength, c->mapstart)
348 - MAP_BASE_ADDR (l));
349 mapat = map_segment (mappref, maplength, c->prot, 0, c->mapoff);
350 l->l_addr = (ElfW(Addr)) mapat - c->mapstart;
352 /* Change protection on the excess portion to disallow all access;
353 the portions we do not remap later will be inaccessible as if
354 unallocated. Then jump into the normal segment-mapping loop to
355 handle the portion of the segment past the end of the file
357 __mprotect ((caddr_t) (l->l_addr + c->mapend),
358 loadcmds[nloadcmds - 1].allocend - c->mapend,
364 /* Notify ELF_PREFERRED_ADDRESS that we have to load this one
366 ELF_FIXED_ADDRESS (loader, c->mapstart);
369 while (c < &loadcmds[nloadcmds])
371 if (c->mapend > c->mapstart)
372 /* Map the segment contents from the file. */
373 map_segment (l->l_addr + c->mapstart, c->mapend - c->mapstart,
374 c->prot, MAP_FIXED, c->mapoff);
377 if (c->allocend > c->dataend)
379 /* Extra zero pages should appear at the end of this segment,
380 after the data mapped from the file. */
381 ElfW(Addr) zero, zeroend, zeropage;
383 zero = l->l_addr + c->dataend;
384 zeroend = l->l_addr + c->allocend;
385 zeropage = (zero + _dl_pagesize - 1) & ~(_dl_pagesize - 1);
387 if (zeroend < zeropage)
388 /* All the extra data is in the last page of the segment.
389 We can just zero it. */
394 /* Zero the final part of the last page of the segment. */
395 if ((c->prot & PROT_WRITE) == 0)
398 if (__mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)),
399 _dl_pagesize, c->prot|PROT_WRITE) < 0)
400 lose (errno, "cannot change memory protections");
402 memset ((void *) zero, 0, zeropage - zero);
403 if ((c->prot & PROT_WRITE) == 0)
404 __mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)),
405 _dl_pagesize, c->prot);
408 if (zeroend > zeropage)
410 /* Map the remaining zero pages in from the zero fill FD. */
412 mapat = __mmap ((caddr_t) zeropage, zeroend - zeropage,
413 c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
415 if (mapat == (caddr_t) -1)
416 lose (errno, "cannot map zero-fill pages");
425 /* There was no PT_PHDR specified. We need to find the phdr in the
426 load image ourselves. We assume it is in fact in the load image
427 somewhere, and that the first load command starts at the
428 beginning of the file and thus contains the ELF file header. */
429 ElfW(Addr) bof = l->l_addr + loadcmds[0].mapstart;
430 assert (loadcmds[0].mapoff == 0);
431 l->l_phdr = (void *) (bof + ((const ElfW(Ehdr) *) bof)->e_phoff);
434 /* Adjust the PT_PHDR value by the runtime load address. */
435 (ElfW(Addr)) l->l_phdr += l->l_addr;
438 /* We are done mapping in the file. We no longer need the descriptor. */
441 if (l->l_type == lt_library && type == ET_EXEC)
442 l->l_type = lt_executable;
447 LOSE ("object file has no dynamic section");
450 (ElfW(Addr)) l->l_ld += l->l_addr;
452 l->l_entry += l->l_addr;
454 elf_get_dynamic_info (l->l_ld, l->l_info);
455 if (l->l_info[DT_HASH])
461 /* Try to open NAME in one of the directories in DIRPATH.
462 Return the fd, or -1. If successful, fill in *REALNAME
463 with the malloc'd full directory name. */
466 open_path (const char *name, size_t namelen,
469 const char *trusted_dirs[])
476 if (p == NULL || *p == '\0')
478 __set_errno (ENOENT);
482 buf = __alloca (strlen (dirpath) + 1 + namelen);
489 p = strpbrk (dirpath, ":;");
491 p = strchr (dirpath, '\0');
493 this_len = p - dirpath;
495 /* When we run a setuid program we do not accept any directory. */
496 if (__libc_enable_secure)
498 /* All trusted directory must be complete name. */
499 if (dirpath[0] != '/')
502 /* If we got a list of trusted directories only accept one
504 if (trusted_dirs != NULL)
506 const char **trust = trusted_dirs;
508 while (*trust != NULL)
509 if (memcmp (dirpath, *trust, this_len) == 0
510 && (*trust)[this_len] == '\0')
515 /* If directory is not trusted, ignore this directory. */
523 /* Two adjacent colons, or a colon at the beginning or the end of
524 the path means to search the current directory. */
525 (void) memcpy (buf, name, namelen);
530 /* Construct the pathname to try. */
531 (void) memcpy (buf, dirpath, this_len);
533 (void) memcpy (&buf[this_len + 1], name, namelen);
534 buflen = this_len + 1 + namelen;
537 fd = __open (buf, O_RDONLY);
540 *realname = malloc (buflen);
543 memcpy (*realname, buf, buflen);
548 /* No memory for the name, we certainly won't be able
549 to load and link it. */
554 if (errno != ENOENT && errno != EACCES)
555 /* The file exists and is readable, but something went wrong. */
558 while (*p++ != '\0');
563 /* Map in the shared object file NAME. */
566 _dl_map_object (struct link_map *loader, const char *name, int type,
574 /* Look for this name among those already loaded. */
575 for (l = _dl_loaded; l; l = l->l_next)
576 if (_dl_name_match_p (name, l) ||
577 /* If the requested name matches the soname of a loaded object,
579 (l->l_info[DT_SONAME] &&
580 ! strcmp (name, (const char *) (l->l_addr +
581 l->l_info[DT_STRTAB]->d_un.d_ptr +
582 l->l_info[DT_SONAME]->d_un.d_val))))
584 /* The object is already loaded.
585 Just bump its reference count and return it. */
590 if (strchr (name, '/') == NULL)
592 /* Search for NAME in several places. */
594 size_t namelen = strlen (name) + 1;
596 inline void trypath (const char *dirpath, const char *trusted[])
598 fd = open_path (name, namelen, dirpath, &realname, trusted);
603 /* First try the DT_RPATH of the dependent object that caused NAME
604 to be loaded. Then that object's dependent, and on up. */
605 for (l = loader; fd == -1 && l; l = l->l_loader)
606 if (l && l->l_info[DT_RPATH])
607 trypath ((const char *) (l->l_addr +
608 l->l_info[DT_STRTAB]->d_un.d_ptr +
609 l->l_info[DT_RPATH]->d_un.d_val), NULL);
610 /* If dynamically linked, try the DT_RPATH of the executable itself. */
612 if (fd == -1 && l && l->l_type != lt_loaded && l->l_info[DT_RPATH])
613 trypath ((const char *) (l->l_addr +
614 l->l_info[DT_STRTAB]->d_un.d_ptr +
615 l->l_info[DT_RPATH]->d_un.d_val), NULL);
616 /* Try an environment variable (unless setuid). */
619 static const char *trusted_dirs[] =
621 #include "trusted-dirs.h"
624 const char *ld_library_path = getenv ("LD_LIBRARY_PATH");
626 if (ld_library_path != NULL && *ld_library_path != '\0')
627 trypath (ld_library_path, trusted_dirs);
631 /* Check the list of libraries in the file /etc/ld.so.cache,
632 for compatibility with Linux's ldconfig program. */
633 extern const char *_dl_load_cache_lookup (const char *name);
634 const char *cached = _dl_load_cache_lookup (name);
637 fd = __open (cached, O_RDONLY);
640 realname = local_strdup (cached);
641 if (realname == NULL)
649 /* Finally, try the default path. */
652 extern const char *_dl_rpath; /* Set in rtld.c. */
653 trypath (_dl_rpath, NULL);
658 fd = __open (name, O_RDONLY);
661 realname = local_strdup (name);
662 if (realname == NULL)
672 name_copy = local_strdup (name);
673 if (name_copy == NULL)
684 /* We haven't found an appropriate library. But since we
685 are only interested in the list of libraries this isn't
686 so severe. Fake an entry with all the information we
688 static const ElfW(Symndx) dummy_bucket = STN_UNDEF;
690 /* Enter the new object in the list of loaded objects. */
691 if ((name_copy = local_strdup (name)) == NULL
692 || (l = _dl_new_object (name_copy, name, type)) == NULL)
693 _dl_signal_error (ENOMEM, name,
694 "cannot create shared object descriptor");
695 /* We use an opencount of 0 as a sign for the faked entry. */
698 l->l_buckets = &dummy_bucket;
705 _dl_signal_error (errno, name, "cannot open shared object file");
708 return _dl_map_object_from_fd (name_copy, fd, realname, loader, type);