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