1 /* Support for memory-mapped windows into a BFD.
2 Copyright 1995, 1996, 2001, 2002, 2003, 2005, 2007, 2008
3 Free Software Foundation, Inc.
4 Written by Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
28 /* Currently, if USE_MMAP is undefined, none if the window stuff is
29 used. Okay, so it's mis-named. At least the command-line option
30 "--without-mmap" is more obvious than "--without-windows" or some
35 #undef HAVE_MPROTECT /* code's not tested yet */
37 #if HAVE_MMAP || HAVE_MPROTECT || HAVE_MADVISE
45 static int debug_windows;
47 /* The idea behind the next and refcount fields is that one mapped
48 region can suffice for multiple read-only windows or multiple
49 non-overlapping read-write windows. It's not implemented yet
55 .struct _bfd_window_internal {
56 . struct _bfd_window_internal *next;
59 . int refcount : 31; {* should be enough... *}
60 . unsigned mapped : 1; {* 1 = mmap, 0 = malloc *}
65 bfd_init_window (bfd_window *windowp)
73 bfd_free_window (bfd_window *windowp)
75 bfd_window_internal *i = windowp->i;
82 fprintf (stderr, "freeing window @%p<%p,%lx,%p>\n",
83 windowp, windowp->data, windowp->size, windowp->i);
90 munmap (i->data, i->size);
97 mprotect (i->data, i->size, PROT_READ | PROT_WRITE);
104 /* There should be no more references to i at this point. */
108 static int ok_to_map = 1;
111 bfd_get_file_window (bfd *abfd,
115 bfd_boolean writable)
117 static size_t pagesize;
118 bfd_window_internal *i = windowp->i;
119 bfd_size_type size_to_alloc = size;
122 fprintf (stderr, "bfd_get_file_window (%p, %6ld, %6ld, %p<%p,%lx,%p>, %d)",
123 abfd, (long) offset, (long) size,
124 windowp, windowp->data, (unsigned long) windowp->size,
125 windowp->i, writable);
127 /* Make sure we know the page size, so we can be friendly to mmap. */
129 pagesize = getpagesize ();
135 i = bfd_zmalloc (sizeof (bfd_window_internal));
143 && (i->data == 0 || i->mapped == 1)
144 && (abfd->flags & BFD_IN_MEMORY) == 0)
146 file_ptr file_offset, offset2;
150 /* Find the real file and the real offset into it. */
151 while (abfd->my_archive != NULL)
153 offset += abfd->origin;
154 abfd = abfd->my_archive;
157 /* Seek into the file, to ensure it is open if cacheable. */
158 if (abfd->iostream == NULL
159 && (abfd->iovec == NULL
160 || abfd->iovec->bseek (abfd, offset, SEEK_SET) != 0))
162 fd = fileno ((FILE *) abfd->iostream);
164 /* Compute offsets and size for mmap and for the user's data. */
165 offset2 = offset % pagesize;
168 file_offset = offset - offset2;
169 real_size = offset + size - file_offset;
170 real_size = real_size + pagesize - 1;
171 real_size -= real_size % pagesize;
173 /* If we're re-using a memory region, make sure it's big enough. */
174 if (i->data && i->size < size)
176 munmap (i->data, i->size);
179 i->data = mmap (i->data, real_size,
180 writable ? PROT_WRITE | PROT_READ : PROT_READ,
182 ? MAP_FILE | MAP_PRIVATE
183 : MAP_FILE | MAP_SHARED),
185 if (i->data == (void *) -1)
187 /* An error happened. Report it, or try using malloc, or
189 bfd_set_error (bfd_error_system_call);
193 fprintf (stderr, "\t\tmmap failed!\n");
197 fprintf (stderr, "\n\tmapped %ld at %p, offset is %ld\n",
198 (long) real_size, i->data, (long) offset2);
200 windowp->data = (bfd_byte *) i->data + offset2;
201 windowp->size = size;
205 else if (debug_windows)
208 fprintf (stderr, _("not mapping: data=%lx mapped=%d\n"),
209 (unsigned long) i->data, (int) i->mapped);
211 fprintf (stderr, _("not mapping: env var not set\n"));
220 size_to_alloc += pagesize - 1;
221 size_to_alloc -= size_to_alloc % pagesize;
225 fprintf (stderr, "\n\t%s(%6ld)",
226 i->data ? "realloc" : " malloc", (long) size_to_alloc);
227 i->data = bfd_realloc_or_free (i->data, size_to_alloc);
229 fprintf (stderr, "\t-> %p\n", i->data);
232 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;
257 #endif /* USE_MMAP */