(elf_machine_rel): Print warning about changed size in copy relocation
[glibc/history.git] / elf / dl-load.c
blob10cc074a6a3c28468cb2bc1a758efcb1a3bbea4c
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. */
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 "dynamic-link.h"
31 /* On some systems, no flag bits are given to specify file mapping. */
32 #ifndef MAP_FILE
33 #define MAP_FILE 0
34 #endif
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. */
43 #ifndef MAP_COPY
44 #define MAP_COPY MAP_PRIVATE
45 #endif
48 #include <endian.h>
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"
55 #else
56 #error "Unknown BYTE_ORDER " BYTE_ORDER
57 #define byteorder ELFDATANONE
58 #endif
60 #define STRING(x) #x
62 #ifdef MAP_ANON
63 /* The fd is not examined when using MAP_ANON. */
64 #define ANONFD -1
65 #else
66 int _dl_zerofd = -1;
67 #define ANONFD _dl_zerofd
68 #endif
70 /* Handle situations where we have a preferred location in memory for
71 the shared objects. */
72 #ifdef ELF_PREFERRED_ADDRESS_DATA
73 ELF_PREFERRED_ADDRESS_DATA;
74 #endif
75 #ifndef ELF_PREFERRED_ADDRESS
76 #define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
77 #endif
78 #ifndef ELF_FIXED_ADDRESS
79 #define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
80 #endif
82 size_t _dl_pagesize;
85 /* Local version of `strdup' function. */
86 static inline char *
87 local_strdup (const char *s)
89 size_t len = strlen (s) + 1;
90 void *new = malloc (len);
92 if (new == NULL)
93 return NULL;
95 return (char *) memcpy (new, s, len);
99 /* Map in the shared object NAME, actually located in REALNAME, and already
100 opened on FD. */
102 struct link_map *
103 _dl_map_object_from_fd (char *name, int fd, char *realname,
104 struct link_map *loader, int l_type)
106 struct link_map *l = NULL;
107 void *file_mapping = NULL;
108 size_t mapping_size = 0;
110 #define LOSE(s) lose (0, (s))
111 void lose (int code, const char *msg)
113 (void) __close (fd);
114 if (file_mapping)
115 __munmap (file_mapping, mapping_size);
116 if (l)
118 /* Remove the stillborn object from the list and free it. */
119 if (l->l_prev)
120 l->l_prev->l_next = l->l_next;
121 if (l->l_next)
122 l->l_next->l_prev = l->l_prev;
123 free (l);
125 free (name);
126 free (realname);
127 _dl_signal_error (code, name, msg);
130 inline caddr_t map_segment (ElfW(Addr) mapstart, size_t len,
131 int prot, int fixed, off_t offset)
133 caddr_t mapat = __mmap ((caddr_t) mapstart, len, prot,
134 fixed|MAP_COPY|MAP_FILE,
135 fd, offset);
136 if (mapat == (caddr_t) -1)
137 lose (errno, "failed to map segment from shared object");
138 return mapat;
141 /* Make sure LOCATION is mapped in. */
142 void *map (off_t location, size_t size)
144 if ((off_t) mapping_size <= location + (off_t) size)
146 void *result;
147 if (file_mapping)
148 __munmap (file_mapping, mapping_size);
149 mapping_size = (location + size + 1 + _dl_pagesize - 1);
150 mapping_size &= ~(_dl_pagesize - 1);
151 result = __mmap (file_mapping, mapping_size, PROT_READ,
152 MAP_COPY|MAP_FILE, fd, 0);
153 if (result == (void *) -1)
154 lose (errno, "cannot map file data");
155 file_mapping = result;
157 return file_mapping + location;
160 const ElfW(Ehdr) *header;
161 const ElfW(Phdr) *phdr;
162 const ElfW(Phdr) *ph;
163 int type;
165 /* Look again to see if the real name matched another already loaded. */
166 for (l = _dl_loaded; l; l = l->l_next)
167 if (! strcmp (realname, l->l_name))
169 struct libname_list *lnp, *lastp;
170 /* The object is already loaded.
171 Just bump its reference count and return it. */
172 __close (fd);
174 /* If the name is not in the list of names for this object add
175 it. */
176 free (realname);
177 lastp = NULL;
178 for (lnp = l->l_libname; lnp != NULL; lastp = lnp, lnp = lnp->next)
179 if (strcmp (name, lnp->name) == 0)
181 free (name);
182 break;
184 if (lnp == NULL)
186 struct libname_list *newname = malloc (sizeof *newname);
187 if (newname == NULL)
188 /* No more memory. */
189 lose (ENOMEM, "cannot allocate name record");
190 /* The object should have a libname set. */
191 assert (lastp != NULL);
193 newname->name = name;
194 newname->next = NULL;
195 lastp->next = newname;
197 ++l->l_opencount;
198 return l;
201 if (_dl_pagesize == 0)
202 _dl_pagesize = __getpagesize ();
204 /* Map in the first page to read the header. */
205 header = map (0, sizeof *header);
207 /* Check the header for basic validity. */
208 if (*(Elf32_Word *) &header->e_ident !=
209 #if BYTE_ORDER == LITTLE_ENDIAN
210 ((ELFMAG0 << (EI_MAG0 * 8)) |
211 (ELFMAG1 << (EI_MAG1 * 8)) |
212 (ELFMAG2 << (EI_MAG2 * 8)) |
213 (ELFMAG3 << (EI_MAG3 * 8)))
214 #else
215 ((ELFMAG0 << (EI_MAG3 * 8)) |
216 (ELFMAG1 << (EI_MAG2 * 8)) |
217 (ELFMAG2 << (EI_MAG1 * 8)) |
218 (ELFMAG3 << (EI_MAG0 * 8)))
219 #endif
221 LOSE ("invalid ELF header");
222 #define ELF32_CLASS ELFCLASS32
223 #define ELF64_CLASS ELFCLASS64
224 if (header->e_ident[EI_CLASS] != ELFW(CLASS))
225 LOSE ("ELF file class not " STRING(__ELF_WORDSIZE) "-bit");
226 if (header->e_ident[EI_DATA] != byteorder)
227 LOSE ("ELF file data encoding not " byteorder_name);
228 if (header->e_ident[EI_VERSION] != EV_CURRENT)
229 LOSE ("ELF file version ident not " STRING(EV_CURRENT));
230 if (header->e_version != EV_CURRENT)
231 LOSE ("ELF file version not " STRING(EV_CURRENT));
232 if (! elf_machine_matches_host (header->e_machine))
233 LOSE ("ELF file machine architecture not " ELF_MACHINE_NAME);
234 if (header->e_phentsize != sizeof (ElfW(Phdr)))
235 LOSE ("ELF file's phentsize not the expected size");
237 #ifndef MAP_ANON
238 #define MAP_ANON 0
239 if (_dl_zerofd == -1)
241 _dl_zerofd = _dl_sysdep_open_zero_fill ();
242 if (_dl_zerofd == -1)
244 __close (fd);
245 _dl_signal_error (errno, NULL, "cannot open zero fill device");
248 #endif
250 /* Enter the new object in the list of loaded objects. */
251 l = _dl_new_object (realname, name, l_type);
252 if (! l)
253 lose (ENOMEM, "cannot create shared object descriptor");
254 l->l_opencount = 1;
255 l->l_loader = loader;
257 /* Extract the remaining details we need from the ELF header
258 and then map in the program header table. */
259 l->l_entry = header->e_entry;
260 type = header->e_type;
261 l->l_phnum = header->e_phnum;
262 phdr = map (header->e_phoff, l->l_phnum * sizeof (ElfW(Phdr)));
265 /* Scan the program header table, collecting its load commands. */
266 struct loadcmd
268 ElfW(Addr) mapstart, mapend, dataend, allocend;
269 off_t mapoff;
270 int prot;
271 } loadcmds[l->l_phnum], *c;
272 size_t nloadcmds = 0;
274 l->l_ld = 0;
275 l->l_phdr = 0;
276 l->l_addr = 0;
277 for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
278 switch (ph->p_type)
280 /* These entries tell us where to find things once the file's
281 segments are mapped in. We record the addresses it says
282 verbatim, and later correct for the run-time load address. */
283 case PT_DYNAMIC:
284 l->l_ld = (void *) ph->p_vaddr;
285 break;
286 case PT_PHDR:
287 l->l_phdr = (void *) ph->p_vaddr;
288 break;
290 case PT_LOAD:
291 /* A load command tells us to map in part of the file.
292 We record the load commands and process them all later. */
293 if (ph->p_align % _dl_pagesize != 0)
294 LOSE ("ELF load command alignment not page-aligned");
295 if ((ph->p_vaddr - ph->p_offset) % ph->p_align)
296 LOSE ("ELF load command address/offset not properly aligned");
298 struct loadcmd *c = &loadcmds[nloadcmds++];
299 c->mapstart = ph->p_vaddr & ~(ph->p_align - 1);
300 c->mapend = ((ph->p_vaddr + ph->p_filesz + _dl_pagesize - 1)
301 & ~(_dl_pagesize - 1));
302 c->dataend = ph->p_vaddr + ph->p_filesz;
303 c->allocend = ph->p_vaddr + ph->p_memsz;
304 c->mapoff = ph->p_offset & ~(ph->p_align - 1);
305 c->prot = 0;
306 if (ph->p_flags & PF_R)
307 c->prot |= PROT_READ;
308 if (ph->p_flags & PF_W)
309 c->prot |= PROT_WRITE;
310 if (ph->p_flags & PF_X)
311 c->prot |= PROT_EXEC;
312 break;
316 /* We are done reading the file's headers now. Unmap them. */
317 __munmap (file_mapping, mapping_size);
319 /* Now process the load commands and map segments into memory. */
320 c = loadcmds;
322 if (type == ET_DYN || type == ET_REL)
324 /* This is a position-independent shared object. We can let the
325 kernel map it anywhere it likes, but we must have space for all
326 the segments in their specified positions relative to the first.
327 So we map the first segment without MAP_FIXED, but with its
328 extent increased to cover all the segments. Then we remove
329 access from excess portion, and there is known sufficient space
330 there to remap from the later segments.
332 As a refinement, sometimes we have an address that we would
333 prefer to map such objects at; but this is only a preference,
334 the OS can do whatever it likes. */
335 caddr_t mapat;
336 ElfW(Addr) mappref;
337 size_t maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart;
338 mappref = ELF_PREFERRED_ADDRESS (loader, maplength, c->mapstart);
339 mapat = map_segment (mappref, maplength, c->prot, 0, c->mapoff);
340 l->l_addr = (ElfW(Addr)) mapat - c->mapstart;
342 /* Change protection on the excess portion to disallow all access;
343 the portions we do not remap later will be inaccessible as if
344 unallocated. Then jump into the normal segment-mapping loop to
345 handle the portion of the segment past the end of the file
346 mapping. */
347 __mprotect ((caddr_t) (l->l_addr + c->mapend),
348 loadcmds[nloadcmds - 1].allocend - c->mapend,
350 goto postmap;
352 else
354 /* Notify ELF_PREFERRED_ADDRESS that we have to load this one
355 fixed. */
356 ELF_FIXED_ADDRESS (loader, c->mapstart);
359 while (c < &loadcmds[nloadcmds])
361 if (c->mapend > c->mapstart)
362 /* Map the segment contents from the file. */
363 map_segment (l->l_addr + c->mapstart, c->mapend - c->mapstart,
364 c->prot, MAP_FIXED, c->mapoff);
366 postmap:
367 if (c->allocend > c->dataend)
369 /* Extra zero pages should appear at the end of this segment,
370 after the data mapped from the file. */
371 ElfW(Addr) zero, zeroend, zeropage;
373 zero = l->l_addr + c->dataend;
374 zeroend = l->l_addr + c->allocend;
375 zeropage = (zero + _dl_pagesize - 1) & ~(_dl_pagesize - 1);
377 if (zeroend < zeropage)
378 /* All the extra data is in the last page of the segment.
379 We can just zero it. */
380 zeropage = zeroend;
382 if (zeropage > zero)
384 /* Zero the final part of the last page of the segment. */
385 if ((c->prot & PROT_WRITE) == 0)
387 /* Dag nab it. */
388 if (__mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)),
389 _dl_pagesize, c->prot|PROT_WRITE) < 0)
390 lose (errno, "cannot change memory protections");
392 memset ((void *) zero, 0, zeropage - zero);
393 if ((c->prot & PROT_WRITE) == 0)
394 __mprotect ((caddr_t) (zero & ~(_dl_pagesize - 1)),
395 _dl_pagesize, c->prot);
398 if (zeroend > zeropage)
400 /* Map the remaining zero pages in from the zero fill FD. */
401 caddr_t mapat;
402 mapat = __mmap ((caddr_t) zeropage, zeroend - zeropage,
403 c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
404 ANONFD, 0);
405 if (mapat == (caddr_t) -1)
406 lose (errno, "cannot map zero-fill pages");
410 ++c;
413 if (l->l_phdr == 0)
415 /* There was no PT_PHDR specified. We need to find the phdr in the
416 load image ourselves. We assume it is in fact in the load image
417 somewhere, and that the first load command starts at the
418 beginning of the file and thus contains the ELF file header. */
419 ElfW(Addr) bof = l->l_addr + loadcmds[0].mapstart;
420 assert (loadcmds[0].mapoff == 0);
421 l->l_phdr = (void *) (bof + ((const ElfW(Ehdr) *) bof)->e_phoff);
423 else
424 /* Adjust the PT_PHDR value by the runtime load address. */
425 (ElfW(Addr)) l->l_phdr += l->l_addr;
428 /* We are done mapping in the file. We no longer need the descriptor. */
429 __close (fd);
431 if (l->l_type == lt_library && type == ET_EXEC)
432 l->l_type = lt_executable;
434 if (l->l_ld == 0)
436 if (type == ET_DYN)
437 LOSE ("object file has no dynamic section");
439 else
440 (ElfW(Addr)) l->l_ld += l->l_addr;
442 l->l_entry += l->l_addr;
444 elf_get_dynamic_info (l->l_ld, l->l_info);
445 if (l->l_info[DT_HASH])
446 _dl_setup_hash (l);
448 return l;
451 /* Try to open NAME in one of the directories in DIRPATH.
452 Return the fd, or -1. If successful, fill in *REALNAME
453 with the malloc'd full directory name. */
455 static int
456 open_path (const char *name, size_t namelen,
457 const char *dirpath,
458 char **realname,
459 const char *trusted_dirs[])
461 char *buf;
462 const char *p;
463 int fd;
465 p = dirpath;
466 if (p == NULL || *p == '\0')
468 __set_errno (ENOENT);
469 return -1;
472 buf = __alloca (strlen (dirpath) + 1 + namelen);
475 size_t buflen;
476 size_t this_len;
478 dirpath = p;
479 p = strpbrk (dirpath, ":;");
480 if (p == NULL)
481 p = strchr (dirpath, '\0');
483 this_len = p - dirpath;
485 /* When we run a setuid program we do not accept any directory. */
486 if (__libc_enable_secure)
488 /* All trusted directory must be complete name. */
489 if (dirpath[0] != '/')
490 continue;
492 /* If we got a list of trusted directories only accept one
493 of these. */
494 if (trusted_dirs != NULL)
496 const char **trust = trusted_dirs;
498 while (*trust != NULL)
499 if (memcmp (dirpath, *trust, this_len) == 0
500 && (*trust)[this_len] == '\0')
501 break;
502 else
503 ++trust;
505 /* If directory is not trusted, ignore this directory. */
506 if (*trust == NULL)
507 continue;
511 if (this_len == 0)
513 /* Two adjacent colons, or a colon at the beginning or the end of
514 the path means to search the current directory. */
515 (void) memcpy (buf, name, namelen);
516 buflen = namelen;
518 else
520 /* Construct the pathname to try. */
521 (void) memcpy (buf, dirpath, this_len);
522 buf[this_len] = '/';
523 (void) memcpy (&buf[this_len + 1], name, namelen);
524 buflen = this_len + 1 + namelen;
527 fd = __open (buf, O_RDONLY);
528 if (fd != -1)
530 *realname = malloc (buflen);
531 if (*realname)
533 memcpy (*realname, buf, buflen);
534 return fd;
536 else
538 /* No memory for the name, we certainly won't be able
539 to load and link it. */
540 __close (fd);
541 return -1;
544 if (errno != ENOENT && errno != EACCES)
545 /* The file exists and is readable, but something went wrong. */
546 return -1;
548 while (*p++ != '\0');
550 return -1;
553 /* Map in the shared object file NAME. */
555 struct link_map *
556 _dl_map_object (struct link_map *loader, const char *name, int type,
557 int trace_mode)
559 int fd;
560 char *realname;
561 char *name_copy;
562 struct link_map *l;
564 /* Look for this name among those already loaded. */
565 for (l = _dl_loaded; l; l = l->l_next)
566 if (_dl_name_match_p (name, l) ||
567 /* If the requested name matches the soname of a loaded object,
568 use that object. */
569 (l->l_info[DT_SONAME] &&
570 ! strcmp (name, (const char *) (l->l_addr +
571 l->l_info[DT_STRTAB]->d_un.d_ptr +
572 l->l_info[DT_SONAME]->d_un.d_val))))
574 /* The object is already loaded.
575 Just bump its reference count and return it. */
576 ++l->l_opencount;
577 return l;
580 if (strchr (name, '/') == NULL)
582 /* Search for NAME in several places. */
584 size_t namelen = strlen (name) + 1;
586 inline void trypath (const char *dirpath, const char *trusted[])
588 fd = open_path (name, namelen, dirpath, &realname, trusted);
591 fd = -1;
593 /* First try the DT_RPATH of the dependent object that caused NAME
594 to be loaded. Then that object's dependent, and on up. */
595 for (l = loader; fd == -1 && l; l = l->l_loader)
596 if (l && l->l_info[DT_RPATH])
597 trypath ((const char *) (l->l_addr +
598 l->l_info[DT_STRTAB]->d_un.d_ptr +
599 l->l_info[DT_RPATH]->d_un.d_val), NULL);
600 /* If dynamically linked, try the DT_RPATH of the executable itself. */
601 l = _dl_loaded;
602 if (fd == -1 && l && l->l_type != lt_loaded && l->l_info[DT_RPATH])
603 trypath ((const char *) (l->l_addr +
604 l->l_info[DT_STRTAB]->d_un.d_ptr +
605 l->l_info[DT_RPATH]->d_un.d_val), NULL);
606 /* Try an environment variable (unless setuid). */
607 if (fd == -1)
609 static const char *trusted_dirs[] =
611 #include "trusted-dirs.h"
612 NULL
614 const char *ld_library_path = getenv ("LD_LIBRARY_PATH");
616 if (ld_library_path != NULL && *ld_library_path != '\0')
617 trypath (ld_library_path, trusted_dirs);
619 if (fd == -1)
621 /* Check the list of libraries in the file /etc/ld.so.cache,
622 for compatibility with Linux's ldconfig program. */
623 extern const char *_dl_load_cache_lookup (const char *name);
624 const char *cached = _dl_load_cache_lookup (name);
625 if (cached)
627 fd = __open (cached, O_RDONLY);
628 if (fd != -1)
630 realname = local_strdup (cached);
631 if (realname == NULL)
633 __close (fd);
634 fd = -1;
639 /* Finally, try the default path. */
640 if (fd == -1)
642 extern const char *_dl_rpath; /* Set in rtld.c. */
643 trypath (_dl_rpath, NULL);
646 else
648 fd = __open (name, O_RDONLY);
649 if (fd != -1)
651 realname = local_strdup (name);
652 if (realname == NULL)
654 __close (fd);
655 fd = -1;
660 if (fd != -1)
662 name_copy = local_strdup (name);
663 if (name_copy == NULL)
665 __close (fd);
666 fd = -1;
670 if (fd == -1)
672 if (trace_mode)
674 /* We haven't found an appropriate library. But since we
675 are only interested in the list of libraries this isn't
676 so severe. Fake an entry with all the information we
677 have. */
678 static const ElfW(Symndx) dummy_bucket = STN_UNDEF;
680 /* Enter the new object in the list of loaded objects. */
681 if ((name_copy = local_strdup (name)) == NULL
682 || (l = _dl_new_object (name_copy, name, type)) == NULL)
683 _dl_signal_error (ENOMEM, name,
684 "cannot create shared object descriptor");
685 /* We use an opencount of 0 as a sign for the faked entry. */
686 l->l_opencount = 0;
687 l->l_reserved = 0;
688 l->l_buckets = &dummy_bucket;
689 l->l_nbuckets = 1;
690 l->l_relocated = 1;
692 return l;
694 else
695 _dl_signal_error (errno, name, "cannot open shared object file");
698 return _dl_map_object_from_fd (name_copy, fd, realname, loader, type);