Update copyright.
[glibc.git] / elf / dl-load.c
blob4773ab05f6367d2548bd3585fb44695fa379cc98
1 /* _dl_map_object -- Map in a shared object's segments from the file.
2 Copyright (C) 1995, 1996, 1997, 1998 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. */
20 #include <link.h>
21 #include <sys/types.h>
22 #include <sys/mman.h>
23 #include <string.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <stdlib.h>
27 #include <errno.h>
28 #include <sys/stat.h>
29 #include "dynamic-link.h"
32 /* On some systems, no flag bits are given to specify file mapping. */
33 #ifndef MAP_FILE
34 #define MAP_FILE 0
35 #endif
37 /* The right way to map in the shared library files is MAP_COPY, which
38 makes a virtual copy of the data at the time of the mmap call; this
39 guarantees the mapped pages will be consistent even if the file is
40 overwritten. Some losing VM systems like Linux's lack MAP_COPY. All we
41 get is MAP_PRIVATE, which copies each page when it is modified; this
42 means if the file is overwritten, we may at some point get some pages
43 from the new version after starting with pages from the old version. */
44 #ifndef MAP_COPY
45 #define MAP_COPY MAP_PRIVATE
46 #endif
49 #include <endian.h>
50 #if BYTE_ORDER == BIG_ENDIAN
51 #define byteorder ELFDATA2MSB
52 #define byteorder_name "big-endian"
53 #elif BYTE_ORDER == LITTLE_ENDIAN
54 #define byteorder ELFDATA2LSB
55 #define byteorder_name "little-endian"
56 #else
57 #error "Unknown BYTE_ORDER " BYTE_ORDER
58 #define byteorder ELFDATANONE
59 #endif
61 #define STRING(x) #x
63 #ifdef MAP_ANON
64 /* The fd is not examined when using MAP_ANON. */
65 #define ANONFD -1
66 #else
67 int _dl_zerofd = -1;
68 #define ANONFD _dl_zerofd
69 #endif
71 size_t _dl_pagesize;
73 /* The load path, defined in rtld.c. */
74 extern const char *_dl_library_path;
76 /* Local version of `strdup' function. */
77 static inline char *
78 local_strdup (const char *s)
80 size_t len = strlen (s) + 1;
81 void *new = malloc (len);
83 if (new == NULL)
84 return NULL;
86 return (char *) memcpy (new, s, len);
90 /* Map in the shared object NAME, actually located in REALNAME, and already
91 opened on FD. */
93 struct link_map *
94 _dl_map_object_from_fd (char *name, int fd, char *realname,
95 struct link_map *loader, int l_type)
97 struct link_map *l = NULL;
98 void *file_mapping = NULL;
99 size_t mapping_size = 0;
101 #define LOSE(s) lose (0, (s))
102 void lose (int code, const char *msg)
104 (void) __close (fd);
105 if (file_mapping)
106 __munmap (file_mapping, mapping_size);
107 if (l)
109 /* Remove the stillborn object from the list and free it. */
110 if (l->l_prev)
111 l->l_prev->l_next = l->l_next;
112 if (l->l_next)
113 l->l_next->l_prev = l->l_prev;
114 free (l);
116 free (name);
117 free (realname);
118 _dl_signal_error (code, name, msg);
121 inline caddr_t map_segment (ElfW(Addr) mapstart, size_t len,
122 int prot, int fixed, off_t offset)
124 caddr_t mapat = __mmap ((caddr_t) mapstart, len, prot,
125 fixed|MAP_COPY|MAP_FILE,
126 fd, offset);
127 if (mapat == (caddr_t) -1)
128 lose (errno, "failed to map segment from shared object");
129 return mapat;
132 /* Make sure LOCATION is mapped in. */
133 void *map (off_t location, size_t size)
135 if ((off_t) mapping_size <= location + (off_t) size)
137 void *result;
138 if (file_mapping)
139 __munmap (file_mapping, mapping_size);
140 mapping_size = (location + size + 1 + _dl_pagesize - 1);
141 mapping_size &= ~(_dl_pagesize - 1);
142 result = __mmap (file_mapping, mapping_size, PROT_READ,
143 MAP_COPY|MAP_FILE, fd, 0);
144 if (result == (void *) -1)
145 lose (errno, "cannot map file data");
146 file_mapping = result;
148 return file_mapping + location;
151 const ElfW(Ehdr) *header;
152 const ElfW(Phdr) *phdr;
153 const ElfW(Phdr) *ph;
154 int type;
156 /* Look again to see if the real name matched another already loaded. */
157 for (l = _dl_loaded; l; l = l->l_next)
158 if (! strcmp (realname, l->l_name))
160 /* The object is already loaded.
161 Just bump its reference count and return it. */
162 __close (fd);
163 free (name);
164 free (realname);
165 ++l->l_opencount;
166 return l;
169 if (_dl_pagesize == 0)
170 _dl_pagesize = __getpagesize ();
172 /* Map in the first page to read the header. */
173 header = map (0, sizeof *header);
175 /* Check the header for basic validity. */
176 if (*(Elf32_Word *) &header->e_ident !=
177 #if BYTE_ORDER == LITTLE_ENDIAN
178 ((ELFMAG0 << (EI_MAG0 * 8)) |
179 (ELFMAG1 << (EI_MAG1 * 8)) |
180 (ELFMAG2 << (EI_MAG2 * 8)) |
181 (ELFMAG3 << (EI_MAG3 * 8)))
182 #else
183 ((ELFMAG0 << (EI_MAG3 * 8)) |
184 (ELFMAG1 << (EI_MAG2 * 8)) |
185 (ELFMAG2 << (EI_MAG1 * 8)) |
186 (ELFMAG3 << (EI_MAG0 * 8)))
187 #endif
189 LOSE ("invalid ELF header");
190 #define ELF32_CLASS ELFCLASS32
191 #define ELF64_CLASS ELFCLASS64
192 if (header->e_ident[EI_CLASS] != ELFW(CLASS))
193 LOSE ("ELF file class not " STRING(__ELF_WORDSIZE) "-bit");
194 if (header->e_ident[EI_DATA] != byteorder)
195 LOSE ("ELF file data encoding not " byteorder_name);
196 if (header->e_ident[EI_VERSION] != EV_CURRENT)
197 LOSE ("ELF file version ident not " STRING(EV_CURRENT));
198 if (header->e_version != EV_CURRENT)
199 LOSE ("ELF file version not " STRING(EV_CURRENT));
200 if (! elf_machine_matches_host (header->e_machine))
201 LOSE ("ELF file machine architecture not " ELF_MACHINE_NAME);
202 if (header->e_phentsize != sizeof (ElfW(Phdr)))
203 LOSE ("ELF file's phentsize not the expected size");
205 #ifndef MAP_ANON
206 #define MAP_ANON 0
207 if (_dl_zerofd == -1)
209 _dl_zerofd = _dl_sysdep_open_zero_fill ();
210 if (_dl_zerofd == -1)
212 __close (fd);
213 _dl_signal_error (errno, NULL, "cannot open zero fill device");
216 #endif
218 /* Enter the new object in the list of loaded objects. */
219 l = _dl_new_object (realname, name, l_type);
220 if (! l)
221 lose (ENOMEM, "cannot create shared object descriptor");
222 l->l_opencount = 1;
223 l->l_loader = loader;
225 /* Extract the remaining details we need from the ELF header
226 and then map in the program header table. */
227 l->l_entry = header->e_entry;
228 type = header->e_type;
229 l->l_phnum = header->e_phnum;
230 phdr = map (header->e_phoff, l->l_phnum * sizeof (ElfW(Phdr)));
233 /* Scan the program header table, collecting its load commands. */
234 struct loadcmd
236 ElfW(Addr) mapstart, mapend, dataend, allocend;
237 off_t mapoff;
238 int prot;
239 } loadcmds[l->l_phnum], *c;
240 size_t nloadcmds = 0;
242 l->l_ld = 0;
243 l->l_phdr = 0;
244 l->l_addr = 0;
245 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
246 switch (ph->p_type)
248 /* These entries tell us where to find things once the file's
249 segments are mapped in. We record the addresses it says
250 verbatim, and later correct for the run-time load address. */
251 case PT_DYNAMIC:
252 l->l_ld = (void *) ph->p_vaddr;
253 break;
254 case PT_PHDR:
255 l->l_phdr = (void *) ph->p_vaddr;
256 break;
258 case PT_LOAD:
259 /* A load command tells us to map in part of the file.
260 We record the load commands and process them all later. */
261 if (ph->p_align % _dl_pagesize != 0)
262 LOSE ("ELF load command alignment not page-aligned");
263 if ((ph->p_vaddr - ph->p_offset) % ph->p_align)
264 LOSE ("ELF load command address/offset not properly aligned");
266 struct loadcmd *c = &loadcmds[nloadcmds++];
267 c->mapstart = ph->p_vaddr & ~(ph->p_align - 1);
268 c->mapend = ((ph->p_vaddr + ph->p_filesz + _dl_pagesize - 1)
269 & ~(_dl_pagesize - 1));
270 c->dataend = ph->p_vaddr + ph->p_filesz;
271 c->allocend = ph->p_vaddr + ph->p_memsz;
272 c->mapoff = ph->p_offset & ~(ph->p_align - 1);
273 c->prot = 0;
274 if (ph->p_flags & PF_R)
275 c->prot |= PROT_READ;
276 if (ph->p_flags & PF_W)
277 c->prot |= PROT_WRITE;
278 if (ph->p_flags & PF_X)
279 c->prot |= PROT_EXEC;
280 break;
284 /* We are done reading the file's headers now. Unmap them. */
285 __munmap (file_mapping, mapping_size);
287 /* Now process the load commands and map segments into memory. */
288 c = loadcmds;
290 if (type == ET_DYN || type == ET_REL)
292 /* This is a position-independent shared object. We can let the
293 kernel map it anywhere it likes, but we must have space for all
294 the segments in their specified positions relative to the first.
295 So we map the first segment without MAP_FIXED, but with its
296 extent increased to cover all the segments. Then we remove
297 access from excess portion, and there is known sufficient space
298 there to remap from the later segments. */
299 caddr_t mapat;
300 mapat = map_segment (c->mapstart,
301 loadcmds[nloadcmds - 1].allocend - c->mapstart,
302 c->prot, 0, c->mapoff);
303 l->l_addr = (ElfW(Addr)) mapat - c->mapstart;
305 /* Change protection on the excess portion to disallow all access;
306 the portions we do not remap later will be inaccessible as if
307 unallocated. Then jump into the normal segment-mapping loop to
308 handle the portion of the segment past the end of the file
309 mapping. */
310 __mprotect ((caddr_t) (l->l_addr + c->mapend),
311 loadcmds[nloadcmds - 1].allocend - c->mapend,
313 goto postmap;
316 while (c < &loadcmds[nloadcmds])
318 if (c->mapend > c->mapstart)
319 /* Map the segment contents from the file. */
320 map_segment (l->l_addr + c->mapstart, c->mapend - c->mapstart,
321 c->prot, MAP_FIXED, c->mapoff);
323 postmap:
324 if (c->allocend > c->dataend)
326 /* Extra zero pages should appear at the end of this segment,
327 after the data mapped from the file. */
328 ElfW(Addr) zero, zeroend, zeropage;
330 zero = l->l_addr + c->dataend;
331 zeroend = l->l_addr + c->allocend;
332 zeropage = (zero + _dl_pagesize - 1) & ~(_dl_pagesize - 1);
334 if (zeroend < zeropage)
335 /* All the extra data is in the last page of the segment.
336 We can just zero it. */
337 zeropage = zeroend;
339 if (zeropage > zero)
341 /* Zero the final part of the last page of the segment. */
342 if ((c->prot & PROT_WRITE) == 0)
344 /* Dag nab it. */
345 if (__mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)),
346 _dl_pagesize, c->prot|PROT_WRITE) < 0)
347 lose (errno, "cannot change memory protections");
349 memset ((void *) zero, 0, zeropage - zero);
350 if ((c->prot & PROT_WRITE) == 0)
351 __mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)),
352 _dl_pagesize, c->prot);
355 if (zeroend > zeropage)
357 /* Map the remaining zero pages in from the zero fill FD. */
358 caddr_t mapat;
359 mapat = __mmap ((caddr_t) zeropage, zeroend - zeropage,
360 c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
361 ANONFD, 0);
362 if (mapat == (caddr_t) -1)
363 lose (errno, "cannot map zero-fill pages");
367 ++c;
370 if (l->l_phdr == 0)
372 /* There was no PT_PHDR specified. We need to find the phdr in the
373 load image ourselves. We assume it is in fact in the load image
374 somewhere, and that the first load command starts at the
375 beginning of the file and thus contains the ELF file header. */
376 ElfW(Addr) bof = l->l_addr + loadcmds[0].mapstart;
377 assert (loadcmds[0].mapoff == 0);
378 l->l_phdr = (void *) (bof + ((const ElfW(Ehdr) *) bof)->e_phoff);
380 else
381 /* Adjust the PT_PHDR value by the runtime load address. */
382 (ElfW(Addr)) l->l_phdr += l->l_addr;
385 /* We are done mapping in the file. We no longer need the descriptor. */
386 __close (fd);
388 if (l->l_type == lt_library && type == ET_EXEC)
389 l->l_type = lt_executable;
391 if (l->l_ld == 0)
393 if (type == ET_DYN)
394 LOSE ("object file has no dynamic section");
396 else
397 (ElfW(Addr)) l->l_ld += l->l_addr;
399 l->l_entry += l->l_addr;
401 elf_get_dynamic_info (l->l_ld, l->l_info);
402 if (l->l_info[DT_HASH])
403 _dl_setup_hash (l);
405 return l;
408 /* Try to open NAME in one of the directories in DIRPATH.
409 Return the fd, or -1. If successful, fill in *REALNAME
410 with the malloc'd full directory name. */
412 static int
413 open_path (const char *name, size_t namelen,
414 const char *dirpath,
415 int preloaded,
416 char **realname,
417 const char *trusted_dirs[])
419 char *buf;
420 const char *p;
421 int fd;
423 p = dirpath;
424 if (p == NULL || *p == '\0')
426 __set_errno (ENOENT);
427 return -1;
430 buf = __alloca (strlen (dirpath) + 1 + namelen);
433 size_t buflen;
434 size_t this_len;
436 dirpath = p;
437 p = strpbrk (dirpath, ":;");
438 if (p == NULL)
439 p = strchr (dirpath, '\0');
441 this_len = p - dirpath;
443 /* When we run a setuid program we do not accept any directory. */
444 if (__libc_enable_secure)
446 /* All trusted directory must be complete name. */
447 if (dirpath[0] != '/')
448 continue;
450 /* If we got a list of trusted directories only accept one
451 of these. */
452 if (trusted_dirs != NULL)
454 const char **trust = trusted_dirs;
456 while (*trust != NULL)
457 if (memcmp (dirpath, *trust, this_len) == 0
458 && (*trust)[this_len] == '\0')
459 break;
460 else
461 ++trust;
463 /* If directory is not trusted, ignore this directory. */
464 if (*trust == NULL)
465 continue;
469 if (this_len == 0)
471 /* Two adjacent colons, or a colon at the beginning or the end of
472 the path means to search the current directory. */
473 (void) memcpy (buf, name, namelen);
474 buflen = namelen;
476 else
478 /* Construct the pathname to try. */
479 (void) memcpy (buf, dirpath, this_len);
480 buf[this_len] = '/';
481 (void) memcpy (&buf[this_len + 1], name, namelen);
482 buflen = this_len + 1 + namelen;
485 fd = __open (buf, O_RDONLY);
486 if (fd != -1)
488 if (preloaded && __libc_enable_secure)
490 /* This is an extra security effort to make sure nobody can
491 preload broken shared objects which are in the trusted
492 directories and so exploit the bugs. */
493 struct stat st;
495 if (__fxstat (_STAT_VER, fd, &st) != 0
496 || (st.st_mode & S_ISUID) == 0)
498 /* The shared object cannot be tested for being SUID
499 or this bit is not set. In this case we must not
500 use this object. */
501 __close (fd);
502 fd = -1;
503 /* We simply ignore the file, signal this by setting
504 the error value which would have been set by `open'. */
505 errno = ENOENT;
509 /* Test again. */
510 if (fd != -1)
512 *realname = malloc (buflen);
513 if (*realname)
515 memcpy (*realname, buf, buflen);
516 return fd;
518 else
520 /* No memory for the name, we certainly won't be able
521 to load and link it. */
522 __close (fd);
523 return -1;
527 if (errno != ENOENT && errno != EACCES)
528 /* The file exists and is readable, but something went wrong. */
529 return -1;
531 while (*p++ != '\0');
533 return -1;
536 /* Map in the shared object file NAME. */
538 struct link_map *
539 _dl_map_object (struct link_map *loader, const char *name, int preloaded,
540 int type, int trace_mode)
542 int fd;
543 char *realname;
544 char *name_copy;
545 struct link_map *l;
547 /* Look for this name among those already loaded. */
548 for (l = _dl_loaded; l; l = l->l_next)
549 if (! strcmp (name, l->l_libname) || /* NAME was requested before. */
550 ! strcmp (name, l->l_name) || /* NAME was found before. */
551 /* If the requested name matches the soname of a loaded object,
552 use that object. */
553 (l->l_info[DT_SONAME] &&
554 ! strcmp (name, (const char *) (l->l_addr +
555 l->l_info[DT_STRTAB]->d_un.d_ptr +
556 l->l_info[DT_SONAME]->d_un.d_val))))
558 /* The object is already loaded.
559 Just bump its reference count and return it. */
560 ++l->l_opencount;
561 return l;
564 if (strchr (name, '/') == NULL)
566 /* Search for NAME in several places. */
568 size_t namelen = strlen (name) + 1;
570 inline void trypath (const char *dirpath, const char *trusted[])
572 fd = open_path (name, namelen, dirpath, preloaded, &realname,
573 trusted);
576 fd = -1;
578 /* First try the DT_RPATH of the dependent object that caused NAME
579 to be loaded. Then that object's dependent, and on up. */
580 for (l = loader; fd == -1 && l; l = l->l_loader)
581 if (l && l->l_info[DT_RPATH])
582 trypath ((const char *) (l->l_addr +
583 l->l_info[DT_STRTAB]->d_un.d_ptr +
584 l->l_info[DT_RPATH]->d_un.d_val), NULL);
585 /* If dynamically linked, try the DT_RPATH of the executable itself. */
586 l = _dl_loaded;
587 if (fd == -1 && l && l->l_type != lt_loaded && l->l_info[DT_RPATH])
588 trypath ((const char *) (l->l_addr +
589 l->l_info[DT_STRTAB]->d_un.d_ptr +
590 l->l_info[DT_RPATH]->d_un.d_val), NULL);
591 /* Try an environment variable (unless setuid). */
592 if (fd == -1 && ! __libc_enable_secure)
594 static const char *trusted_dirs[] =
596 #include "trusted-dirs.h"
597 NULL
600 trypath (_dl_library_path, trusted_dirs);
602 if (fd == -1)
604 /* Check the list of libraries in the file /etc/ld.so.cache,
605 for compatibility with Linux's ldconfig program. */
606 extern const char *_dl_load_cache_lookup (const char *name);
607 const char *cached = _dl_load_cache_lookup (name);
608 if (cached)
610 fd = __open (cached, O_RDONLY);
611 if (fd != -1)
613 realname = local_strdup (cached);
614 if (realname == NULL)
616 __close (fd);
617 fd = -1;
622 /* Finally, try the default path. */
623 if (fd == -1)
625 extern const char *_dl_rpath; /* Set in rtld.c. */
626 trypath (_dl_rpath, NULL);
629 else
631 fd = __open (name, O_RDONLY);
632 if (fd != -1)
634 realname = local_strdup (name);
635 if (realname == NULL)
637 __close (fd);
638 fd = -1;
643 if (fd != -1)
645 name_copy = local_strdup (name);
646 if (name_copy == NULL)
648 __close (fd);
649 fd = -1;
653 if (fd == -1)
655 if (trace_mode)
657 /* We haven't found an appropriate library. But since we
658 are only interested in the list of libraries this isn't
659 so severe. Fake an entry with all the information we
660 have. */
661 static const ElfW(Symndx) dummy_bucket = STN_UNDEF;
663 /* Enter the new object in the list of loaded objects. */
664 if ((name_copy = local_strdup (name)) == NULL
665 || (l = _dl_new_object (name_copy, name, type)) == NULL)
666 _dl_signal_error (ENOMEM, name,
667 "cannot create shared object descriptor");
668 /* We use an opencount of 0 as a sign for the faked entry. */
669 l->l_opencount = 0;
670 l->l_reserved = 0;
671 l->l_buckets = &dummy_bucket;
672 l->l_nbuckets = 1;
673 l->l_relocated = 1;
675 return l;
677 else
678 _dl_signal_error (errno, name, "cannot open shared object file");
681 return _dl_map_object_from_fd (name_copy, fd, realname, loader, type);