1 /* $MirOS: src/gnu/usr.bin/binutils/bfd/bfdwin.c,v 1.3 2005/06/05 21:23:53 tg Exp $ */
2
3 /* Support for memory-mapped windows into a BFD.
4 Copyright 1995, 1996, 2001, 2002, 2003 Free Software Foundation, Inc.
5 Written by Cygnus Support.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
22
23 #include "sysdep.h"
24
25 #include "bfd.h"
26 #include "libbfd.h"
27
28 __RCSID("$MirOS: src/gnu/usr.bin/binutils/bfd/bfdwin.c,v 1.3 2005/06/05 21:23:53 tg Exp $");
29
30 /* Currently, if USE_MMAP is undefined, none if the window stuff is
31 used. Okay, so it's mis-named. At least the command-line option
32 "--without-mmap" is more obvious than "--without-windows" or some
33 such. */
34
35 #ifdef USE_MMAP
36
37 #undef HAVE_MPROTECT /* code's not tested yet */
38
39 #if HAVE_MMAP || HAVE_MPROTECT || HAVE_MADVISE
40 #include <sys/mman.h>
41 #endif
42
43 #ifndef MAP_FILE
44 #define MAP_FILE 0
45 #endif
46
47 static int debug_windows;
48
49 /* The idea behind the next and refcount fields is that one mapped
50 region can suffice for multiple read-only windows or multiple
51 non-overlapping read-write windows. It's not implemented yet
52 though. */
53
54 /*
55 INTERNAL_DEFINITION
56
57 .struct _bfd_window_internal {
58 . struct _bfd_window_internal *next;
59 . void *data;
60 . bfd_size_type size;
61 . int refcount : 31; {* should be enough... *}
62 . unsigned mapped : 1; {* 1 = mmap, 0 = malloc *}
63 .};
64 */
65
66 void
bfd_init_window(bfd_window * windowp)67 bfd_init_window (bfd_window *windowp)
68 {
69 windowp->data = 0;
70 windowp->i = 0;
71 windowp->size = 0;
72 }
73
74 void
bfd_free_window(bfd_window * windowp)75 bfd_free_window (bfd_window *windowp)
76 {
77 bfd_window_internal *i = windowp->i;
78 windowp->i = 0;
79 windowp->data = 0;
80 if (i == 0)
81 return;
82 i->refcount--;
83 if (debug_windows)
84 fprintf (stderr, "freeing window @%p<%p,%lx,%p>\n",
85 windowp, windowp->data, (unsigned long)windowp->size, windowp->i);
86 if (i->refcount != 0)
87 return;
88
89 if (i->mapped)
90 {
91 #ifdef HAVE_MMAP
92 munmap (i->data, i->size);
93 goto no_free;
94 #else
95 abort ();
96 #endif
97 }
98 #ifdef HAVE_MPROTECT
99 mprotect (i->data, i->size, PROT_READ | PROT_WRITE);
100 #endif
101 free (i->data);
102 #ifdef HAVE_MMAP
103 no_free:
104 #endif
105 i->data = 0;
106 /* There should be no more references to i at this point. */
107 free (i);
108 }
109
110 static int ok_to_map = 1;
111
112 bfd_boolean
bfd_get_file_window(bfd * abfd,file_ptr offset,bfd_size_type size,bfd_window * windowp,bfd_boolean writable)113 bfd_get_file_window (bfd *abfd,
114 file_ptr offset,
115 bfd_size_type size,
116 bfd_window *windowp,
117 bfd_boolean writable)
118 {
119 static size_t pagesize;
120 bfd_window_internal *i = windowp->i;
121 bfd_size_type size_to_alloc = size;
122
123 if (debug_windows)
124 fprintf (stderr, "bfd_get_file_window (%p, %6ld, %6ld, %p<%p,%lx,%p>, %d)",
125 abfd, (long) offset, (long) size,
126 windowp, windowp->data, (unsigned long) windowp->size,
127 windowp->i, writable);
128
129 /* Make sure we know the page size, so we can be friendly to mmap. */
130 if (pagesize == 0)
131 pagesize = getpagesize ();
132 if (pagesize == 0)
133 abort ();
134
135 if (i == 0)
136 {
137 i = bfd_zmalloc (sizeof (bfd_window_internal));
138 windowp->i = i;
139 if (i == 0)
140 return FALSE;
141 i->data = 0;
142 }
143 #ifdef HAVE_MMAP
144 if (ok_to_map
145 && (i->data == 0 || i->mapped == 1)
146 && (abfd->flags & BFD_IN_MEMORY) == 0)
147 {
148 file_ptr file_offset, offset2;
149 size_t real_size;
150 int fd;
151 FILE *f;
152
153 /* Find the real file and the real offset into it. */
154 while (abfd->my_archive != NULL)
155 {
156 offset += abfd->origin;
157 abfd = abfd->my_archive;
158 }
159 f = bfd_cache_lookup (abfd);
160 fd = fileno (f);
161
162 /* Compute offsets and size for mmap and for the user's data. */
163 offset2 = offset % pagesize;
164 if (offset2 < 0)
165 abort ();
166 file_offset = offset - offset2;
167 real_size = offset + size - file_offset;
168 real_size = real_size + pagesize - 1;
169 real_size -= real_size % pagesize;
170
171 /* If we're re-using a memory region, make sure it's big enough. */
172 if (i->data && i->size < size)
173 {
174 munmap (i->data, i->size);
175 i->data = 0;
176 }
177 i->data = mmap (i->data, real_size,
178 writable ? PROT_WRITE | PROT_READ : PROT_READ,
179 (writable
180 ? MAP_FILE | MAP_PRIVATE
181 : MAP_FILE | MAP_SHARED),
182 fd, file_offset);
183 if (i->data == (void *) -1)
184 {
185 /* An error happened. Report it, or try using malloc, or
186 something. */
187 bfd_set_error (bfd_error_system_call);
188 i->data = 0;
189 windowp->data = 0;
190 if (debug_windows)
191 fprintf (stderr, "\t\tmmap failed!\n");
192 return FALSE;
193 }
194 if (debug_windows)
195 fprintf (stderr, "\n\tmapped %ld at %p, offset is %ld\n",
196 (long) real_size, i->data, (long) offset2);
197 i->size = real_size;
198 windowp->data = (bfd_byte *) i->data + offset2;
199 windowp->size = size;
200 i->mapped = 1;
201 return TRUE;
202 }
203 else if (debug_windows)
204 {
205 if (ok_to_map)
206 fprintf (stderr, _("not mapping: data=%lx mapped=%d\n"),
207 (unsigned long) i->data, (int) i->mapped);
208 else
209 fprintf (stderr, _("not mapping: env var not set\n"));
210 }
211 #else
212 ok_to_map = 0;
213 #endif
214
215 #ifdef HAVE_MPROTECT
216 if (!writable)
217 {
218 size_to_alloc += pagesize - 1;
219 size_to_alloc -= size_to_alloc % pagesize;
220 }
221 #endif
222 if (debug_windows)
223 fprintf (stderr, "\n\t%s(%6ld)",
224 i->data ? "realloc" : " malloc", (long) size_to_alloc);
225 i->data = bfd_realloc (i->data, size_to_alloc);
226 if (debug_windows)
227 fprintf (stderr, "\t-> %p\n", i->data);
228 i->refcount = 1;
229 if (i->data == NULL)
230 {
231 if (size_to_alloc == 0)
232 return TRUE;
233 return FALSE;
234 }
235 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
236 return FALSE;
237 i->size = bfd_bread (i->data, size, abfd);
238 if (i->size != size)
239 return FALSE;
240 i->mapped = 0;
241 #ifdef HAVE_MPROTECT
242 if (!writable)
243 {
244 if (debug_windows)
245 fprintf (stderr, "\tmprotect (%p, %ld, PROT_READ)\n", i->data,
246 (long) i->size);
247 mprotect (i->data, i->size, PROT_READ);
248 }
249 #endif
250 windowp->data = i->data;
251 windowp->size = i->size;
252 return TRUE;
253 }
254
255 #endif /* USE_MMAP */
256