1 /* Support for memory-mapped windows into a BFD.
2 Copyright (C) 1995-2016 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
27 /* Currently, if USE_MMAP is undefined, none of the window stuff is
28 used. Enabled by --with-mmap. */
32 #undef HAVE_MPROTECT /* code's not tested yet */
34 #if HAVE_MMAP || HAVE_MPROTECT || HAVE_MADVISE
42 static int debug_windows;
44 /* The idea behind the next and refcount fields is that one mapped
45 region can suffice for multiple read-only windows or multiple
46 non-overlapping read-write windows. It's not implemented yet
52 .struct _bfd_window_internal {
53 . struct _bfd_window_internal *next;
56 . int refcount : 31; {* should be enough... *}
57 . unsigned mapped : 1; {* 1 = mmap, 0 = malloc *}
62 bfd_init_window (bfd_window *windowp)
70 bfd_free_window (bfd_window *windowp)
72 bfd_window_internal *i = windowp->i;
79 fprintf (stderr, "freeing window @%p<%p,%lx,%p>\n",
80 windowp, windowp->data, (unsigned long) windowp->size, windowp->i);
87 munmap (i->data, i->size);
94 mprotect (i->data, i->size, PROT_READ | PROT_WRITE);
101 /* There should be no more references to i at this point. */
105 static int ok_to_map = 1;
108 bfd_get_file_window (bfd *abfd,
112 bfd_boolean writable)
114 static size_t pagesize;
115 bfd_window_internal *i = windowp->i;
116 bfd_size_type size_to_alloc = size;
119 fprintf (stderr, "bfd_get_file_window (%p, %6ld, %6ld, %p<%p,%lx,%p>, %d)",
120 abfd, (long) offset, (long) size,
121 windowp, windowp->data, (unsigned long) windowp->size,
122 windowp->i, writable);
124 /* Make sure we know the page size, so we can be friendly to mmap. */
126 pagesize = getpagesize ();
132 i = bfd_zmalloc (sizeof (bfd_window_internal));
139 && (i->data == NULL || i->mapped == 1)
140 && (abfd->flags & BFD_IN_MEMORY) == 0)
142 file_ptr file_offset, offset2;
146 /* Find the real file and the real offset into it. */
147 while (abfd->my_archive != NULL)
149 offset += abfd->origin;
150 abfd = abfd->my_archive;
153 /* Seek into the file, to ensure it is open if cacheable. */
154 if (abfd->iostream == NULL
155 && (abfd->iovec == NULL
156 || abfd->iovec->bseek (abfd, offset, SEEK_SET) != 0))
159 fd = fileno ((FILE *) abfd->iostream);
160 /* Compute offsets and size for mmap and for the user's data. */
161 offset2 = offset % pagesize;
164 file_offset = offset - offset2;
165 real_size = offset + size - file_offset;
166 real_size = real_size + pagesize - 1;
167 real_size -= real_size % pagesize;
169 /* If we're re-using a memory region, make sure it's big enough. */
170 if (i->data != NULL && i->size < size)
172 munmap (i->data, i->size);
175 i->data = mmap (i->data, real_size,
176 writable ? PROT_WRITE | PROT_READ : PROT_READ,
178 ? MAP_FILE | MAP_PRIVATE
179 : MAP_FILE | MAP_SHARED),
181 if (i->data == (void *) -1)
183 /* An error happened. Report it, or try using malloc, or
185 bfd_set_error (bfd_error_system_call);
188 fprintf (stderr, "\t\tmmap failed!\n");
192 fprintf (stderr, "\n\tmapped %ld at %p, offset is %ld\n",
193 (long) real_size, i->data, (long) offset2);
195 windowp->data = (bfd_byte *) i->data + offset2;
196 windowp->size = size;
202 else if (debug_windows)
205 fprintf (stderr, _("not mapping: data=%lx mapped=%d\n"),
206 (unsigned long) i->data, (int) i->mapped);
208 fprintf (stderr, _("not mapping: env var not set\n"));
217 size_to_alloc += pagesize - 1;
218 size_to_alloc -= size_to_alloc % pagesize;
222 fprintf (stderr, "\n\t%s(%6ld)",
223 i->data ? "realloc" : " malloc", (long) size_to_alloc);
224 i->data = bfd_realloc_or_free (i->data, size_to_alloc);
226 fprintf (stderr, "\t-> %p\n", i->data);
229 if (size_to_alloc == 0)
237 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
239 i->size = bfd_bread (i->data, size, abfd);
247 fprintf (stderr, "\tmprotect (%p, %ld, PROT_READ)\n", i->data,
249 mprotect (i->data, i->size, PROT_READ);
252 windowp->data = i->data;
253 windowp->size = i->size;
258 /* We have a bfd_window_internal, but an error occurred. Free it. */
263 #endif /* USE_MMAP */