]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/bfdwin.c
Add forgotten ChangeLog snippet for last elf64-x86-64.c patch.
[thirdparty/binutils-gdb.git] / bfd / bfdwin.c
CommitLineData
93509525
KD
1/* Support for memory-mapped windows into a BFD.
2 Copyright 1995, 1996, 2001, 2002 Free Software Foundation, Inc.
3 Written by Cygnus Support.
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21#include "sysdep.h"
22
23#include "bfd.h"
24#include "libbfd.h"
25
26/* Currently, if USE_MMAP is undefined, none if the window stuff is
27 used. Okay, so it's mis-named. At least the command-line option
28 "--without-mmap" is more obvious than "--without-windows" or some
29 such. */
30
31#ifdef USE_MMAP
32
33#undef HAVE_MPROTECT /* code's not tested yet */
34
35#if HAVE_MMAP || HAVE_MPROTECT || HAVE_MADVISE
36#include <sys/mman.h>
37#endif
38
39#ifndef MAP_FILE
40#define MAP_FILE 0
41#endif
42
43static int debug_windows;
44
45/* The idea behind the next and refcount fields is that one mapped
46 region can suffice for multiple read-only windows or multiple
47 non-overlapping read-write windows. It's not implemented yet
48 though. */
49
50/*
51INTERNAL_DEFINITION
52
53.struct _bfd_window_internal {
54. struct _bfd_window_internal *next;
55. PTR data;
56. bfd_size_type size;
57. int refcount : 31; {* should be enough... *}
58. unsigned mapped : 1; {* 1 = mmap, 0 = malloc *}
59.};
60*/
61
62void
63bfd_init_window (windowp)
64 bfd_window *windowp;
65{
66 windowp->data = 0;
67 windowp->i = 0;
68 windowp->size = 0;
69}
70
71void
72bfd_free_window (windowp)
73 bfd_window *windowp;
74{
75 bfd_window_internal *i = windowp->i;
76 windowp->i = 0;
77 windowp->data = 0;
78 if (i == 0)
79 return;
80 i->refcount--;
81 if (debug_windows)
82 fprintf (stderr, "freeing window @%p<%p,%lx,%p>\n",
83 windowp, windowp->data, windowp->size, windowp->i);
84 if (i->refcount != 0)
85 return;
86
87 if (i->mapped)
88 {
89#ifdef HAVE_MMAP
90 munmap (i->data, i->size);
91 goto no_free;
92#else
93 abort ();
94#endif
95 }
96#ifdef HAVE_MPROTECT
97 mprotect (i->data, i->size, PROT_READ | PROT_WRITE);
98#endif
99 free (i->data);
100#ifdef HAVE_MMAP
101 no_free:
102#endif
103 i->data = 0;
104 /* There should be no more references to i at this point. */
105 free (i);
106}
107
108static int ok_to_map = 1;
109
b34976b6 110bfd_boolean
93509525
KD
111bfd_get_file_window (abfd, offset, size, windowp, writable)
112 bfd *abfd;
113 file_ptr offset;
114 bfd_size_type size;
115 bfd_window *windowp;
b34976b6 116 bfd_boolean writable;
93509525
KD
117{
118 static size_t pagesize;
119 bfd_window_internal *i = windowp->i;
120 bfd_size_type size_to_alloc = size;
121
122 if (debug_windows)
123 fprintf (stderr, "bfd_get_file_window (%p, %6ld, %6ld, %p<%p,%lx,%p>, %d)",
124 abfd, (long) offset, (long) size,
125 windowp, windowp->data, (unsigned long) windowp->size,
126 windowp->i, writable);
127
128 /* Make sure we know the page size, so we can be friendly to mmap. */
129 if (pagesize == 0)
130 pagesize = getpagesize ();
131 if (pagesize == 0)
132 abort ();
133
134 if (i == 0)
135 {
136 i = ((bfd_window_internal *)
137 bfd_zmalloc ((bfd_size_type) sizeof (bfd_window_internal)));
138 windowp->i = i;
139 if (i == 0)
b34976b6 140 return FALSE;
93509525
KD
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 == (PTR) -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");
b34976b6 192 return FALSE;
93509525
KD
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 = (PTR) ((bfd_byte *) i->data + offset2);
199 windowp->size = size;
200 i->mapped = 1;
b34976b6 201 return TRUE;
93509525
KD
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 = (PTR) 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)
b34976b6
AM
232 return TRUE;
233 return FALSE;
93509525
KD
234 }
235 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
b34976b6 236 return FALSE;
93509525
KD
237 i->size = bfd_bread (i->data, size, abfd);
238 if (i->size != size)
b34976b6 239 return FALSE;
93509525
KD
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;
b34976b6 252 return TRUE;
93509525
KD
253}
254
255#endif /* USE_MMAP */