Commit | Line | Data |
---|---|---|
93509525 | 1 | /* Low-level I/O routines for BFDs. |
7c192733 | 2 | |
b3adc24a | 3 | Copyright (C) 1990-2020 Free Software Foundation, Inc. |
7c192733 | 4 | |
93509525 KD |
5 | Written by Cygnus Support. |
6 | ||
cd123cb7 | 7 | This file is part of BFD, the Binary File Descriptor library. |
93509525 | 8 | |
cd123cb7 NC |
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 3 of the License, or | |
12 | (at your option) any later version. | |
93509525 | 13 | |
cd123cb7 NC |
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. | |
93509525 | 18 | |
cd123cb7 NC |
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, | |
22 | MA 02110-1301, USA. */ | |
93509525 KD |
23 | |
24 | #include "sysdep.h" | |
3db64b00 | 25 | #include <limits.h> |
93509525 KD |
26 | #include "bfd.h" |
27 | #include "libbfd.h" | |
28 | ||
93509525 KD |
29 | #ifndef S_IXUSR |
30 | #define S_IXUSR 0100 /* Execute by owner. */ | |
31 | #endif | |
32 | #ifndef S_IXGRP | |
33 | #define S_IXGRP 0010 /* Execute by group. */ | |
34 | #endif | |
35 | #ifndef S_IXOTH | |
36 | #define S_IXOTH 0001 /* Execute by others. */ | |
37 | #endif | |
38 | ||
428b207a TT |
39 | #ifndef FD_CLOEXEC |
40 | #define FD_CLOEXEC 1 | |
41 | #endif | |
42 | ||
7c192733 | 43 | file_ptr |
c7c3d11b | 44 | _bfd_real_ftell (FILE *file) |
7c192733 AC |
45 | { |
46 | #if defined (HAVE_FTELLO64) | |
47 | return ftello64 (file); | |
48 | #elif defined (HAVE_FTELLO) | |
49 | return ftello (file); | |
50 | #else | |
51 | return ftell (file); | |
52 | #endif | |
53 | } | |
54 | ||
55 | int | |
c7c3d11b | 56 | _bfd_real_fseek (FILE *file, file_ptr offset, int whence) |
7c192733 AC |
57 | { |
58 | #if defined (HAVE_FSEEKO64) | |
59 | return fseeko64 (file, offset, whence); | |
60 | #elif defined (HAVE_FSEEKO) | |
61 | return fseeko (file, offset, whence); | |
62 | #else | |
63 | return fseek (file, offset, whence); | |
64 | #endif | |
65 | } | |
66 | ||
428b207a TT |
67 | /* Mark FILE as close-on-exec. Return FILE. FILE may be NULL, in |
68 | which case nothing is done. */ | |
69 | static FILE * | |
70 | close_on_exec (FILE *file) | |
71 | { | |
72 | #if defined (HAVE_FILENO) && defined (F_GETFD) | |
73 | if (file) | |
74 | { | |
75 | int fd = fileno (file); | |
76 | int old = fcntl (fd, F_GETFD, 0); | |
77 | if (old >= 0) | |
78 | fcntl (fd, F_SETFD, old | FD_CLOEXEC); | |
79 | } | |
80 | #endif | |
81 | return file; | |
82 | } | |
83 | ||
2e6f4fae | 84 | FILE * |
c7c3d11b | 85 | _bfd_real_fopen (const char *filename, const char *modes) |
2e6f4fae | 86 | { |
d387240a | 87 | #ifdef VMS |
d387240a TG |
88 | char *vms_attr; |
89 | ||
9aff4b7a | 90 | /* On VMS, fopen allows file attributes as optional arguments. |
d387240a TG |
91 | We need to use them but we'd better to use the common prototype. |
92 | In fopen-vms.h, they are separated from the mode with a comma. | |
93 | Split here. */ | |
94 | vms_attr = strchr (modes, ','); | |
95 | if (vms_attr == NULL) | |
96 | { | |
97 | /* No attributes. */ | |
98 | return close_on_exec (fopen (filename, modes)); | |
99 | } | |
100 | else | |
101 | { | |
0c376465 TG |
102 | /* Attributes found. Split. */ |
103 | size_t modes_len = strlen (modes) + 1; | |
104 | char attrs[modes_len + 1]; | |
105 | char *at[3]; | |
106 | int i; | |
107 | ||
108 | memcpy (attrs, modes, modes_len); | |
109 | at[0] = attrs; | |
110 | for (i = 0; i < 2; i++) | |
111 | { | |
112 | at[i + 1] = strchr (at[i], ','); | |
113 | BFD_ASSERT (at[i + 1] != NULL); | |
114 | *(at[i + 1]++) = 0; /* Replace ',' with a nul, and skip it. */ | |
115 | } | |
116 | return close_on_exec (fopen (filename, at[0], at[1], at[2])); | |
d387240a TG |
117 | } |
118 | #else /* !VMS */ | |
2e6f4fae | 119 | #if defined (HAVE_FOPEN64) |
428b207a | 120 | return close_on_exec (fopen64 (filename, modes)); |
2e6f4fae | 121 | #else |
428b207a | 122 | return close_on_exec (fopen (filename, modes)); |
2e6f4fae | 123 | #endif |
d387240a | 124 | #endif /* !VMS */ |
2e6f4fae DJ |
125 | } |
126 | ||
40838a72 AC |
127 | /* |
128 | INTERNAL_DEFINITION | |
129 | struct bfd_iovec | |
93509525 | 130 | |
40838a72 | 131 | DESCRIPTION |
93509525 | 132 | |
40838a72 AC |
133 | The <<struct bfd_iovec>> contains the internal file I/O class. |
134 | Each <<BFD>> has an instance of this class and all file I/O is | |
135 | routed through it (it is assumed that the instance implements | |
136 | all methods listed below). | |
137 | ||
138 | .struct bfd_iovec | |
139 | .{ | |
140 | . {* To avoid problems with macros, a "b" rather than "f" | |
141 | . prefix is prepended to each method name. *} | |
142 | . {* Attempt to read/write NBYTES on ABFD's IOSTREAM storing/fetching | |
143 | . bytes starting at PTR. Return the number of bytes actually | |
144 | . transfered (a read past end-of-file returns less than NBYTES), | |
145 | . or -1 (setting <<bfd_error>>) if an error occurs. *} | |
146 | . file_ptr (*bread) (struct bfd *abfd, void *ptr, file_ptr nbytes); | |
147 | . file_ptr (*bwrite) (struct bfd *abfd, const void *ptr, | |
07d6d2b8 | 148 | . file_ptr nbytes); |
40838a72 AC |
149 | . {* Return the current IOSTREAM file offset, or -1 (setting <<bfd_error>> |
150 | . if an error occurs. *} | |
151 | . file_ptr (*btell) (struct bfd *abfd); | |
152 | . {* For the following, on successful completion a value of 0 is returned. | |
07d6d2b8 | 153 | . Otherwise, a value of -1 is returned (and <<bfd_error>> is set). *} |
40838a72 | 154 | . int (*bseek) (struct bfd *abfd, file_ptr offset, int whence); |
405bf443 | 155 | . int (*bclose) (struct bfd *abfd); |
40838a72 AC |
156 | . int (*bflush) (struct bfd *abfd); |
157 | . int (*bstat) (struct bfd *abfd, struct stat *sb); | |
4c95ab76 TG |
158 | . {* Mmap a part of the files. ADDR, LEN, PROT, FLAGS and OFFSET are the usual |
159 | . mmap parameter, except that LEN and OFFSET do not need to be page | |
160 | . aligned. Returns (void *)-1 on failure, mmapped address on success. | |
161 | . Also write in MAP_ADDR the address of the page aligned buffer and in | |
162 | . MAP_LEN the size mapped (a page multiple). Use unmap with MAP_ADDR and | |
163 | . MAP_LEN to unmap. *} | |
f07749bb | 164 | . void *(*bmmap) (struct bfd *abfd, void *addr, bfd_size_type len, |
07d6d2b8 AM |
165 | . int prot, int flags, file_ptr offset, |
166 | . void **map_addr, bfd_size_type *map_len); | |
40838a72 | 167 | .}; |
93509525 | 168 | |
65077aa8 TG |
169 | .extern const struct bfd_iovec _bfd_memory_iovec; |
170 | ||
40838a72 | 171 | */ |
93509525 | 172 | |
93509525 KD |
173 | |
174 | /* Return value is amount read. */ | |
175 | ||
176 | bfd_size_type | |
c58b9523 | 177 | bfd_bread (void *ptr, bfd_size_type size, bfd *abfd) |
93509525 | 178 | { |
5c4ce239 AM |
179 | file_ptr nread; |
180 | bfd *element_bfd = abfd; | |
181 | ufile_ptr offset = 0; | |
182 | ||
183 | while (abfd->my_archive != NULL | |
184 | && !bfd_is_thin_archive (abfd->my_archive)) | |
185 | { | |
186 | offset += abfd->origin; | |
187 | abfd = abfd->my_archive; | |
188 | } | |
93509525 | 189 | |
1fb41da4 AM |
190 | /* If this is an archive element, don't read past the end of |
191 | this element. */ | |
5c4ce239 | 192 | if (element_bfd->arelt_data != NULL) |
1fb41da4 | 193 | { |
5c4ce239 | 194 | bfd_size_type maxbytes = arelt_size (element_bfd); |
f1bb16f8 | 195 | |
5c4ce239 | 196 | if (abfd->where < offset || abfd->where - offset >= maxbytes) |
07d6d2b8 | 197 | { |
5c4ce239 AM |
198 | bfd_set_error (bfd_error_invalid_operation); |
199 | return -1; | |
07d6d2b8 | 200 | } |
5c4ce239 AM |
201 | if (abfd->where - offset + size > maxbytes) |
202 | size = maxbytes - (abfd->where - offset); | |
1fb41da4 AM |
203 | } |
204 | ||
5c4ce239 AM |
205 | if (abfd->iovec == NULL) |
206 | { | |
207 | bfd_set_error (bfd_error_invalid_operation); | |
208 | return -1; | |
209 | } | |
210 | ||
211 | nread = abfd->iovec->bread (abfd, ptr, size); | |
212 | if (nread != -1) | |
93509525 KD |
213 | abfd->where += nread; |
214 | ||
93509525 KD |
215 | return nread; |
216 | } | |
217 | ||
218 | bfd_size_type | |
c58b9523 | 219 | bfd_bwrite (const void *ptr, bfd_size_type size, bfd *abfd) |
93509525 | 220 | { |
5c4ce239 | 221 | file_ptr nwrote; |
93509525 | 222 | |
5c4ce239 AM |
223 | while (abfd->my_archive != NULL |
224 | && !bfd_is_thin_archive (abfd->my_archive)) | |
225 | abfd = abfd->my_archive; | |
69fd4758 | 226 | |
5c4ce239 AM |
227 | if (abfd->iovec == NULL) |
228 | { | |
229 | bfd_set_error (bfd_error_invalid_operation); | |
230 | return -1; | |
231 | } | |
232 | ||
233 | nwrote = abfd->iovec->bwrite (abfd, ptr, size); | |
234 | if (nwrote != -1) | |
93509525 | 235 | abfd->where += nwrote; |
5c4ce239 | 236 | if ((bfd_size_type) nwrote != size) |
93509525 KD |
237 | { |
238 | #ifdef ENOSPC | |
239 | errno = ENOSPC; | |
240 | #endif | |
241 | bfd_set_error (bfd_error_system_call); | |
242 | } | |
243 | return nwrote; | |
244 | } | |
245 | ||
7c192733 | 246 | file_ptr |
c58b9523 | 247 | bfd_tell (bfd *abfd) |
93509525 | 248 | { |
5c4ce239 | 249 | ufile_ptr offset = 0; |
93509525 KD |
250 | file_ptr ptr; |
251 | ||
5c4ce239 AM |
252 | while (abfd->my_archive != NULL |
253 | && !bfd_is_thin_archive (abfd->my_archive)) | |
69fd4758 | 254 | { |
5c4ce239 AM |
255 | offset += abfd->origin; |
256 | abfd = abfd->my_archive; | |
69fd4758 | 257 | } |
93509525 | 258 | |
5c4ce239 AM |
259 | if (abfd->iovec == NULL) |
260 | return 0; | |
261 | ||
262 | ptr = abfd->iovec->btell (abfd); | |
93509525 | 263 | abfd->where = ptr; |
5c4ce239 | 264 | return ptr - offset; |
93509525 KD |
265 | } |
266 | ||
267 | int | |
c58b9523 | 268 | bfd_flush (bfd *abfd) |
93509525 | 269 | { |
5c4ce239 AM |
270 | while (abfd->my_archive != NULL |
271 | && !bfd_is_thin_archive (abfd->my_archive)) | |
272 | abfd = abfd->my_archive; | |
273 | ||
274 | if (abfd->iovec == NULL) | |
275 | return 0; | |
276 | ||
277 | return abfd->iovec->bflush (abfd); | |
93509525 KD |
278 | } |
279 | ||
280 | /* Returns 0 for success, negative value for failure (in which case | |
281 | bfd_get_error can retrieve the error code). */ | |
282 | int | |
c58b9523 | 283 | bfd_stat (bfd *abfd, struct stat *statbuf) |
93509525 | 284 | { |
93509525 KD |
285 | int result; |
286 | ||
5c4ce239 AM |
287 | while (abfd->my_archive != NULL |
288 | && !bfd_is_thin_archive (abfd->my_archive)) | |
289 | abfd = abfd->my_archive; | |
69fd4758 | 290 | |
5c4ce239 AM |
291 | if (abfd->iovec == NULL) |
292 | { | |
293 | bfd_set_error (bfd_error_invalid_operation); | |
294 | return -1; | |
295 | } | |
296 | ||
297 | result = abfd->iovec->bstat (abfd, statbuf); | |
93509525 KD |
298 | if (result < 0) |
299 | bfd_set_error (bfd_error_system_call); | |
300 | return result; | |
301 | } | |
302 | ||
303 | /* Returns 0 for success, nonzero for failure (in which case bfd_get_error | |
304 | can retrieve the error code). */ | |
305 | ||
306 | int | |
c58b9523 | 307 | bfd_seek (bfd *abfd, file_ptr position, int direction) |
93509525 KD |
308 | { |
309 | int result; | |
5c4ce239 | 310 | ufile_ptr offset = 0; |
93509525 | 311 | |
5c4ce239 AM |
312 | while (abfd->my_archive != NULL |
313 | && !bfd_is_thin_archive (abfd->my_archive)) | |
93509525 | 314 | { |
5c4ce239 AM |
315 | offset += abfd->origin; |
316 | abfd = abfd->my_archive; | |
93509525 | 317 | } |
93509525 | 318 | |
5c4ce239 | 319 | if (abfd->iovec == NULL) |
660722b0 | 320 | { |
5c4ce239 AM |
321 | bfd_set_error (bfd_error_invalid_operation); |
322 | return -1; | |
660722b0 | 323 | } |
93509525 | 324 | |
5c4ce239 AM |
325 | /* For the time being, a BFD may not seek to it's end. The problem |
326 | is that we don't easily have a way to recognize the end of an | |
327 | element in an archive. */ | |
328 | BFD_ASSERT (direction == SEEK_SET || direction == SEEK_CUR); | |
329 | ||
330 | if (direction != SEEK_CUR) | |
331 | position += offset; | |
69fd4758 | 332 | |
ff91d2f0 AM |
333 | if ((direction == SEEK_CUR && position == 0) |
334 | || (direction == SEEK_SET && (ufile_ptr) position == abfd->where)) | |
335 | return 0; | |
336 | ||
5c4ce239 | 337 | result = abfd->iovec->bseek (abfd, position, direction); |
93509525 KD |
338 | if (result != 0) |
339 | { | |
93509525 | 340 | /* An EINVAL error probably means that the file offset was |
07d6d2b8 | 341 | absurd. */ |
5c4ce239 | 342 | if (errno == EINVAL) |
93509525 KD |
343 | bfd_set_error (bfd_error_file_truncated); |
344 | else | |
5c4ce239 | 345 | bfd_set_error (bfd_error_system_call); |
93509525 KD |
346 | } |
347 | else | |
348 | { | |
349 | /* Adjust `where' field. */ | |
5c4ce239 | 350 | if (direction == SEEK_CUR) |
93509525 | 351 | abfd->where += position; |
5c4ce239 AM |
352 | else |
353 | abfd->where = position; | |
93509525 | 354 | } |
5c4ce239 | 355 | |
93509525 KD |
356 | return result; |
357 | } | |
358 | ||
359 | /* | |
360 | FUNCTION | |
361 | bfd_get_mtime | |
362 | ||
363 | SYNOPSIS | |
c58b9523 | 364 | long bfd_get_mtime (bfd *abfd); |
93509525 KD |
365 | |
366 | DESCRIPTION | |
367 | Return the file modification time (as read from the file system, or | |
368 | from the archive header for archive members). | |
369 | ||
370 | */ | |
371 | ||
372 | long | |
c58b9523 | 373 | bfd_get_mtime (bfd *abfd) |
93509525 | 374 | { |
93509525 KD |
375 | struct stat buf; |
376 | ||
377 | if (abfd->mtime_set) | |
378 | return abfd->mtime; | |
379 | ||
5c4ce239 | 380 | if (bfd_stat (abfd, &buf) != 0) |
93509525 KD |
381 | return 0; |
382 | ||
383 | abfd->mtime = buf.st_mtime; /* Save value in case anyone wants it */ | |
384 | return buf.st_mtime; | |
385 | } | |
386 | ||
387 | /* | |
388 | FUNCTION | |
389 | bfd_get_size | |
390 | ||
391 | SYNOPSIS | |
47fdcf63 | 392 | ufile_ptr bfd_get_size (bfd *abfd); |
93509525 KD |
393 | |
394 | DESCRIPTION | |
395 | Return the file size (as read from file system) for the file | |
396 | associated with BFD @var{abfd}. | |
397 | ||
398 | The initial motivation for, and use of, this routine is not | |
399 | so we can get the exact size of the object the BFD applies to, since | |
400 | that might not be generally possible (archive members for example). | |
401 | It would be ideal if someone could eventually modify | |
402 | it so that such results were guaranteed. | |
403 | ||
404 | Instead, we want to ask questions like "is this NNN byte sized | |
405 | object I'm about to try read from file offset YYY reasonable?" | |
406 | As as example of where we might do this, some object formats | |
407 | use string tables for which the first <<sizeof (long)>> bytes of the | |
408 | table contain the size of the table itself, including the size bytes. | |
409 | If an application tries to read what it thinks is one of these | |
410 | string tables, without some way to validate the size, and for | |
411 | some reason the size is wrong (byte swapping error, wrong location | |
412 | for the string table, etc.), the only clue is likely to be a read | |
413 | error when it tries to read the table, or a "virtual memory | |
414 | exhausted" error when it tries to allocate 15 bazillon bytes | |
415 | of space for the 15 bazillon byte table it is about to read. | |
5c4491d3 | 416 | This function at least allows us to answer the question, "is the |
93509525 | 417 | size reasonable?". |
b03202e3 AM |
418 | |
419 | A return value of zero indicates the file size is unknown. | |
93509525 KD |
420 | */ |
421 | ||
47fdcf63 | 422 | ufile_ptr |
c58b9523 | 423 | bfd_get_size (bfd *abfd) |
93509525 | 424 | { |
b03202e3 AM |
425 | /* A size of 0 means we haven't yet called bfd_stat. A size of 1 |
426 | means we have a cached value of 0, ie. unknown. */ | |
427 | if (abfd->size <= 1 || bfd_write_p (abfd)) | |
428 | { | |
429 | struct stat buf; | |
93509525 | 430 | |
b03202e3 AM |
431 | if (abfd->size == 1 && !bfd_write_p (abfd)) |
432 | return 0; | |
93509525 | 433 | |
b03202e3 AM |
434 | if (bfd_stat (abfd, &buf) != 0 |
435 | || buf.st_size == 0 | |
436 | || buf.st_size - (ufile_ptr) buf.st_size != 0) | |
437 | { | |
438 | abfd->size = 1; | |
439 | return 0; | |
440 | } | |
441 | abfd->size = buf.st_size; | |
442 | } | |
443 | return abfd->size; | |
93509525 | 444 | } |
25b88f33 | 445 | |
8e2f54bc L |
446 | /* |
447 | FUNCTION | |
448 | bfd_get_file_size | |
449 | ||
450 | SYNOPSIS | |
47fdcf63 | 451 | ufile_ptr bfd_get_file_size (bfd *abfd); |
8e2f54bc L |
452 | |
453 | DESCRIPTION | |
454 | Return the file size (as read from file system) for the file | |
455 | associated with BFD @var{abfd}. It supports both normal files | |
456 | and archive elements. | |
457 | ||
458 | */ | |
459 | ||
47fdcf63 | 460 | ufile_ptr |
8e2f54bc L |
461 | bfd_get_file_size (bfd *abfd) |
462 | { | |
463 | if (abfd->my_archive != NULL | |
464 | && !bfd_is_thin_archive (abfd->my_archive)) | |
465 | return arelt_size (abfd); | |
466 | ||
467 | return bfd_get_size (abfd); | |
468 | } | |
25b88f33 PP |
469 | |
470 | /* | |
471 | FUNCTION | |
472 | bfd_mmap | |
473 | ||
474 | SYNOPSIS | |
475 | void *bfd_mmap (bfd *abfd, void *addr, bfd_size_type len, | |
07d6d2b8 AM |
476 | int prot, int flags, file_ptr offset, |
477 | void **map_addr, bfd_size_type *map_len); | |
25b88f33 PP |
478 | |
479 | DESCRIPTION | |
480 | Return mmap()ed region of the file, if possible and implemented. | |
07d6d2b8 AM |
481 | LEN and OFFSET do not need to be page aligned. The page aligned |
482 | address and length are written to MAP_ADDR and MAP_LEN. | |
25b88f33 PP |
483 | |
484 | */ | |
485 | ||
486 | void * | |
487 | bfd_mmap (bfd *abfd, void *addr, bfd_size_type len, | |
4c95ab76 | 488 | int prot, int flags, file_ptr offset, |
07d6d2b8 | 489 | void **map_addr, bfd_size_type *map_len) |
25b88f33 | 490 | { |
5c4ce239 AM |
491 | while (abfd->my_archive != NULL |
492 | && !bfd_is_thin_archive (abfd->my_archive)) | |
493 | { | |
494 | offset += abfd->origin; | |
495 | abfd = abfd->my_archive; | |
496 | } | |
25b88f33 PP |
497 | |
498 | if (abfd->iovec == NULL) | |
5c4ce239 AM |
499 | { |
500 | bfd_set_error (bfd_error_invalid_operation); | |
501 | return (void *) -1; | |
502 | } | |
25b88f33 | 503 | |
4c95ab76 | 504 | return abfd->iovec->bmmap (abfd, addr, len, prot, flags, offset, |
07d6d2b8 | 505 | map_addr, map_len); |
25b88f33 | 506 | } |
65077aa8 TG |
507 | |
508 | /* Memory file I/O operations. */ | |
509 | ||
510 | static file_ptr | |
511 | memory_bread (bfd *abfd, void *ptr, file_ptr size) | |
512 | { | |
513 | struct bfd_in_memory *bim; | |
514 | bfd_size_type get; | |
515 | ||
516 | bim = (struct bfd_in_memory *) abfd->iostream; | |
517 | get = size; | |
518 | if (abfd->where + get > bim->size) | |
519 | { | |
520 | if (bim->size < (bfd_size_type) abfd->where) | |
07d6d2b8 | 521 | get = 0; |
65077aa8 | 522 | else |
07d6d2b8 | 523 | get = bim->size - abfd->where; |
65077aa8 TG |
524 | bfd_set_error (bfd_error_file_truncated); |
525 | } | |
526 | memcpy (ptr, bim->buffer + abfd->where, (size_t) get); | |
527 | return get; | |
528 | } | |
529 | ||
530 | static file_ptr | |
531 | memory_bwrite (bfd *abfd, const void *ptr, file_ptr size) | |
532 | { | |
533 | struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream; | |
534 | ||
535 | if (abfd->where + size > bim->size) | |
536 | { | |
537 | bfd_size_type newsize, oldsize; | |
538 | ||
539 | oldsize = (bim->size + 127) & ~(bfd_size_type) 127; | |
540 | bim->size = abfd->where + size; | |
541 | /* Round up to cut down on memory fragmentation */ | |
542 | newsize = (bim->size + 127) & ~(bfd_size_type) 127; | |
543 | if (newsize > oldsize) | |
07d6d2b8 AM |
544 | { |
545 | bim->buffer = (bfd_byte *) bfd_realloc_or_free (bim->buffer, newsize); | |
546 | if (bim->buffer == NULL) | |
547 | { | |
548 | bim->size = 0; | |
549 | return 0; | |
550 | } | |
551 | if (newsize > bim->size) | |
552 | memset (bim->buffer + bim->size, 0, newsize - bim->size); | |
553 | } | |
65077aa8 TG |
554 | } |
555 | memcpy (bim->buffer + abfd->where, ptr, (size_t) size); | |
556 | return size; | |
557 | } | |
558 | ||
559 | static file_ptr | |
560 | memory_btell (bfd *abfd) | |
561 | { | |
562 | return abfd->where; | |
563 | } | |
564 | ||
565 | static int | |
566 | memory_bseek (bfd *abfd, file_ptr position, int direction) | |
567 | { | |
568 | file_ptr nwhere; | |
569 | struct bfd_in_memory *bim; | |
570 | ||
571 | bim = (struct bfd_in_memory *) abfd->iostream; | |
572 | ||
573 | if (direction == SEEK_SET) | |
574 | nwhere = position; | |
575 | else | |
576 | nwhere = abfd->where + position; | |
577 | ||
578 | if (nwhere < 0) | |
579 | { | |
580 | abfd->where = 0; | |
581 | errno = EINVAL; | |
582 | return -1; | |
583 | } | |
584 | ||
585 | if ((bfd_size_type)nwhere > bim->size) | |
586 | { | |
587 | if (abfd->direction == write_direction | |
07d6d2b8 AM |
588 | || abfd->direction == both_direction) |
589 | { | |
590 | bfd_size_type newsize, oldsize; | |
591 | ||
592 | oldsize = (bim->size + 127) & ~(bfd_size_type) 127; | |
593 | bim->size = nwhere; | |
594 | /* Round up to cut down on memory fragmentation */ | |
595 | newsize = (bim->size + 127) & ~(bfd_size_type) 127; | |
596 | if (newsize > oldsize) | |
597 | { | |
598 | bim->buffer = (bfd_byte *) bfd_realloc_or_free (bim->buffer, newsize); | |
599 | if (bim->buffer == NULL) | |
600 | { | |
601 | errno = EINVAL; | |
602 | bim->size = 0; | |
603 | return -1; | |
604 | } | |
605 | memset (bim->buffer + oldsize, 0, newsize - oldsize); | |
606 | } | |
607 | } | |
65077aa8 | 608 | else |
07d6d2b8 AM |
609 | { |
610 | abfd->where = bim->size; | |
611 | errno = EINVAL; | |
612 | bfd_set_error (bfd_error_file_truncated); | |
613 | return -1; | |
614 | } | |
65077aa8 TG |
615 | } |
616 | return 0; | |
617 | } | |
618 | ||
405bf443 | 619 | static int |
65077aa8 TG |
620 | memory_bclose (struct bfd *abfd) |
621 | { | |
622 | struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream; | |
623 | ||
624 | if (bim->buffer != NULL) | |
625 | free (bim->buffer); | |
626 | free (bim); | |
627 | abfd->iostream = NULL; | |
628 | ||
405bf443 | 629 | return 0; |
65077aa8 TG |
630 | } |
631 | ||
632 | static int | |
633 | memory_bflush (bfd *abfd ATTRIBUTE_UNUSED) | |
634 | { | |
635 | return 0; | |
636 | } | |
637 | ||
638 | static int | |
639 | memory_bstat (bfd *abfd, struct stat *statbuf) | |
640 | { | |
641 | struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream; | |
642 | ||
b5dee4ea | 643 | memset (statbuf, 0, sizeof (*statbuf)); |
65077aa8 TG |
644 | statbuf->st_size = bim->size; |
645 | ||
646 | return 0; | |
647 | } | |
648 | ||
649 | static void * | |
650 | memory_bmmap (bfd *abfd ATTRIBUTE_UNUSED, void *addr ATTRIBUTE_UNUSED, | |
07d6d2b8 AM |
651 | bfd_size_type len ATTRIBUTE_UNUSED, int prot ATTRIBUTE_UNUSED, |
652 | int flags ATTRIBUTE_UNUSED, file_ptr offset ATTRIBUTE_UNUSED, | |
653 | void **map_addr ATTRIBUTE_UNUSED, | |
654 | bfd_size_type *map_len ATTRIBUTE_UNUSED) | |
65077aa8 TG |
655 | { |
656 | return (void *)-1; | |
657 | } | |
658 | ||
659 | const struct bfd_iovec _bfd_memory_iovec = | |
660 | { | |
661 | &memory_bread, &memory_bwrite, &memory_btell, &memory_bseek, | |
662 | &memory_bclose, &memory_bflush, &memory_bstat, &memory_bmmap | |
663 | }; |