]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/bfdio.c
Update year range in copyright notice of binutils files
[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);
157 wchar_t * fullPathOffset = fullPath + prefixLen - 1;
158
cb7da2a6
TS
159 GetFullPathNameW (partPath, fullPathWSize, fullPathOffset, lpFilePart);
160 free (partPath);
161
162 /* It is non-standard for modes to exceed 16 characters. */
5f3fc928 163 wchar_t modesW[16];
ba0eb22c 164
68e80d96 165 MultiByteToWideChar (cp, 0, modes, -1, modesW, sizeof(modesW));
cb7da2a6 166
ba0eb22c 167 FILE * file = _wfopen (fullPath, modesW);
cb7da2a6
TS
168 free (fullPath);
169
170 return close_on_exec (file);
0b8448af
NC
171
172#elif defined (HAVE_FOPEN64)
428b207a 173 return close_on_exec (fopen64 (filename, modes));
0b8448af 174
cb7da2a6 175#else
0b8448af 176 return close_on_exec (fopen (filename, modes));
cb7da2a6 177#endif
2e6f4fae
DJ
178}
179
40838a72
AC
180/*
181INTERNAL_DEFINITION
182 struct bfd_iovec
93509525 183
40838a72 184DESCRIPTION
93509525 185
40838a72
AC
186 The <<struct bfd_iovec>> contains the internal file I/O class.
187 Each <<BFD>> has an instance of this class and all file I/O is
188 routed through it (it is assumed that the instance implements
189 all methods listed below).
190
191.struct bfd_iovec
192.{
193. {* To avoid problems with macros, a "b" rather than "f"
194. prefix is prepended to each method name. *}
195. {* Attempt to read/write NBYTES on ABFD's IOSTREAM storing/fetching
196. bytes starting at PTR. Return the number of bytes actually
197. transfered (a read past end-of-file returns less than NBYTES),
198. or -1 (setting <<bfd_error>>) if an error occurs. *}
199. file_ptr (*bread) (struct bfd *abfd, void *ptr, file_ptr nbytes);
200. file_ptr (*bwrite) (struct bfd *abfd, const void *ptr,
07d6d2b8 201. file_ptr nbytes);
40838a72
AC
202. {* Return the current IOSTREAM file offset, or -1 (setting <<bfd_error>>
203. if an error occurs. *}
204. file_ptr (*btell) (struct bfd *abfd);
205. {* For the following, on successful completion a value of 0 is returned.
07d6d2b8 206. Otherwise, a value of -1 is returned (and <<bfd_error>> is set). *}
40838a72 207. int (*bseek) (struct bfd *abfd, file_ptr offset, int whence);
405bf443 208. int (*bclose) (struct bfd *abfd);
40838a72
AC
209. int (*bflush) (struct bfd *abfd);
210. int (*bstat) (struct bfd *abfd, struct stat *sb);
4c95ab76
TG
211. {* Mmap a part of the files. ADDR, LEN, PROT, FLAGS and OFFSET are the usual
212. mmap parameter, except that LEN and OFFSET do not need to be page
213. aligned. Returns (void *)-1 on failure, mmapped address on success.
214. Also write in MAP_ADDR the address of the page aligned buffer and in
215. MAP_LEN the size mapped (a page multiple). Use unmap with MAP_ADDR and
216. MAP_LEN to unmap. *}
f07749bb 217. void *(*bmmap) (struct bfd *abfd, void *addr, bfd_size_type len,
07d6d2b8
AM
218. int prot, int flags, file_ptr offset,
219. void **map_addr, bfd_size_type *map_len);
40838a72 220.};
93509525 221
65077aa8
TG
222.extern const struct bfd_iovec _bfd_memory_iovec;
223
40838a72 224*/
93509525 225
93509525
KD
226
227/* Return value is amount read. */
228
229bfd_size_type
c58b9523 230bfd_bread (void *ptr, bfd_size_type size, bfd *abfd)
93509525 231{
5c4ce239
AM
232 file_ptr nread;
233 bfd *element_bfd = abfd;
234 ufile_ptr offset = 0;
235
236 while (abfd->my_archive != NULL
237 && !bfd_is_thin_archive (abfd->my_archive))
238 {
239 offset += abfd->origin;
240 abfd = abfd->my_archive;
241 }
4d095f5b 242 offset += abfd->origin;
93509525 243
c17eb63b 244 /* If this is a non-thin archive element, don't read past the end of
1fb41da4 245 this element. */
c17eb63b
L
246 if (element_bfd->arelt_data != NULL
247 && element_bfd->my_archive != NULL
248 && !bfd_is_thin_archive (element_bfd->my_archive))
1fb41da4 249 {
5c4ce239 250 bfd_size_type maxbytes = arelt_size (element_bfd);
f1bb16f8 251
5c4ce239 252 if (abfd->where < offset || abfd->where - offset >= maxbytes)
07d6d2b8 253 {
5c4ce239
AM
254 bfd_set_error (bfd_error_invalid_operation);
255 return -1;
07d6d2b8 256 }
5c4ce239
AM
257 if (abfd->where - offset + size > maxbytes)
258 size = maxbytes - (abfd->where - offset);
1fb41da4
AM
259 }
260
5c4ce239
AM
261 if (abfd->iovec == NULL)
262 {
263 bfd_set_error (bfd_error_invalid_operation);
264 return -1;
265 }
266
267 nread = abfd->iovec->bread (abfd, ptr, size);
268 if (nread != -1)
93509525
KD
269 abfd->where += nread;
270
93509525
KD
271 return nread;
272}
273
274bfd_size_type
c58b9523 275bfd_bwrite (const void *ptr, bfd_size_type size, bfd *abfd)
93509525 276{
5c4ce239 277 file_ptr nwrote;
93509525 278
5c4ce239
AM
279 while (abfd->my_archive != NULL
280 && !bfd_is_thin_archive (abfd->my_archive))
281 abfd = abfd->my_archive;
69fd4758 282
5c4ce239
AM
283 if (abfd->iovec == NULL)
284 {
285 bfd_set_error (bfd_error_invalid_operation);
286 return -1;
287 }
288
289 nwrote = abfd->iovec->bwrite (abfd, ptr, size);
290 if (nwrote != -1)
93509525 291 abfd->where += nwrote;
5c4ce239 292 if ((bfd_size_type) nwrote != size)
93509525
KD
293 {
294#ifdef ENOSPC
295 errno = ENOSPC;
296#endif
297 bfd_set_error (bfd_error_system_call);
298 }
299 return nwrote;
300}
301
7c192733 302file_ptr
c58b9523 303bfd_tell (bfd *abfd)
93509525 304{
5c4ce239 305 ufile_ptr offset = 0;
93509525
KD
306 file_ptr ptr;
307
5c4ce239
AM
308 while (abfd->my_archive != NULL
309 && !bfd_is_thin_archive (abfd->my_archive))
69fd4758 310 {
5c4ce239
AM
311 offset += abfd->origin;
312 abfd = abfd->my_archive;
69fd4758 313 }
4d095f5b 314 offset += abfd->origin;
93509525 315
5c4ce239
AM
316 if (abfd->iovec == NULL)
317 return 0;
318
319 ptr = abfd->iovec->btell (abfd);
93509525 320 abfd->where = ptr;
5c4ce239 321 return ptr - offset;
93509525
KD
322}
323
324int
c58b9523 325bfd_flush (bfd *abfd)
93509525 326{
5c4ce239
AM
327 while (abfd->my_archive != NULL
328 && !bfd_is_thin_archive (abfd->my_archive))
329 abfd = abfd->my_archive;
330
331 if (abfd->iovec == NULL)
332 return 0;
333
334 return abfd->iovec->bflush (abfd);
93509525
KD
335}
336
337/* Returns 0 for success, negative value for failure (in which case
338 bfd_get_error can retrieve the error code). */
339int
c58b9523 340bfd_stat (bfd *abfd, struct stat *statbuf)
93509525 341{
93509525
KD
342 int result;
343
5c4ce239
AM
344 while (abfd->my_archive != NULL
345 && !bfd_is_thin_archive (abfd->my_archive))
346 abfd = abfd->my_archive;
69fd4758 347
5c4ce239
AM
348 if (abfd->iovec == NULL)
349 {
350 bfd_set_error (bfd_error_invalid_operation);
351 return -1;
352 }
353
354 result = abfd->iovec->bstat (abfd, statbuf);
93509525
KD
355 if (result < 0)
356 bfd_set_error (bfd_error_system_call);
357 return result;
358}
359
360/* Returns 0 for success, nonzero for failure (in which case bfd_get_error
361 can retrieve the error code). */
362
363int
c58b9523 364bfd_seek (bfd *abfd, file_ptr position, int direction)
93509525
KD
365{
366 int result;
5c4ce239 367 ufile_ptr offset = 0;
93509525 368
5c4ce239
AM
369 while (abfd->my_archive != NULL
370 && !bfd_is_thin_archive (abfd->my_archive))
93509525 371 {
5c4ce239
AM
372 offset += abfd->origin;
373 abfd = abfd->my_archive;
93509525 374 }
4d095f5b 375 offset += abfd->origin;
93509525 376
5c4ce239 377 if (abfd->iovec == NULL)
660722b0 378 {
5c4ce239
AM
379 bfd_set_error (bfd_error_invalid_operation);
380 return -1;
660722b0 381 }
93509525 382
5c4ce239
AM
383 /* For the time being, a BFD may not seek to it's end. The problem
384 is that we don't easily have a way to recognize the end of an
385 element in an archive. */
386 BFD_ASSERT (direction == SEEK_SET || direction == SEEK_CUR);
387
388 if (direction != SEEK_CUR)
389 position += offset;
69fd4758 390
ff91d2f0
AM
391 if ((direction == SEEK_CUR && position == 0)
392 || (direction == SEEK_SET && (ufile_ptr) position == abfd->where))
393 return 0;
394
5c4ce239 395 result = abfd->iovec->bseek (abfd, position, direction);
93509525
KD
396 if (result != 0)
397 {
93509525 398 /* An EINVAL error probably means that the file offset was
07d6d2b8 399 absurd. */
5c4ce239 400 if (errno == EINVAL)
93509525
KD
401 bfd_set_error (bfd_error_file_truncated);
402 else
5c4ce239 403 bfd_set_error (bfd_error_system_call);
93509525
KD
404 }
405 else
406 {
407 /* Adjust `where' field. */
5c4ce239 408 if (direction == SEEK_CUR)
93509525 409 abfd->where += position;
5c4ce239
AM
410 else
411 abfd->where = position;
93509525 412 }
5c4ce239 413
93509525
KD
414 return result;
415}
416
417/*
418FUNCTION
419 bfd_get_mtime
420
421SYNOPSIS
c58b9523 422 long bfd_get_mtime (bfd *abfd);
93509525
KD
423
424DESCRIPTION
425 Return the file modification time (as read from the file system, or
426 from the archive header for archive members).
427
428*/
429
430long
c58b9523 431bfd_get_mtime (bfd *abfd)
93509525 432{
93509525
KD
433 struct stat buf;
434
435 if (abfd->mtime_set)
436 return abfd->mtime;
437
5c4ce239 438 if (bfd_stat (abfd, &buf) != 0)
93509525
KD
439 return 0;
440
441 abfd->mtime = buf.st_mtime; /* Save value in case anyone wants it */
442 return buf.st_mtime;
443}
444
445/*
446FUNCTION
447 bfd_get_size
448
449SYNOPSIS
47fdcf63 450 ufile_ptr bfd_get_size (bfd *abfd);
93509525
KD
451
452DESCRIPTION
453 Return the file size (as read from file system) for the file
454 associated with BFD @var{abfd}.
455
456 The initial motivation for, and use of, this routine is not
457 so we can get the exact size of the object the BFD applies to, since
458 that might not be generally possible (archive members for example).
459 It would be ideal if someone could eventually modify
460 it so that such results were guaranteed.
461
462 Instead, we want to ask questions like "is this NNN byte sized
463 object I'm about to try read from file offset YYY reasonable?"
464 As as example of where we might do this, some object formats
465 use string tables for which the first <<sizeof (long)>> bytes of the
466 table contain the size of the table itself, including the size bytes.
467 If an application tries to read what it thinks is one of these
468 string tables, without some way to validate the size, and for
469 some reason the size is wrong (byte swapping error, wrong location
470 for the string table, etc.), the only clue is likely to be a read
471 error when it tries to read the table, or a "virtual memory
472 exhausted" error when it tries to allocate 15 bazillon bytes
473 of space for the 15 bazillon byte table it is about to read.
5c4491d3 474 This function at least allows us to answer the question, "is the
93509525 475 size reasonable?".
b03202e3
AM
476
477 A return value of zero indicates the file size is unknown.
93509525
KD
478*/
479
47fdcf63 480ufile_ptr
c58b9523 481bfd_get_size (bfd *abfd)
93509525 482{
b03202e3
AM
483 /* A size of 0 means we haven't yet called bfd_stat. A size of 1
484 means we have a cached value of 0, ie. unknown. */
485 if (abfd->size <= 1 || bfd_write_p (abfd))
486 {
487 struct stat buf;
93509525 488
b03202e3
AM
489 if (abfd->size == 1 && !bfd_write_p (abfd))
490 return 0;
93509525 491
b03202e3
AM
492 if (bfd_stat (abfd, &buf) != 0
493 || buf.st_size == 0
494 || buf.st_size - (ufile_ptr) buf.st_size != 0)
495 {
496 abfd->size = 1;
497 return 0;
498 }
499 abfd->size = buf.st_size;
500 }
501 return abfd->size;
93509525 502}
25b88f33 503
8e2f54bc
L
504/*
505FUNCTION
506 bfd_get_file_size
507
508SYNOPSIS
47fdcf63 509 ufile_ptr bfd_get_file_size (bfd *abfd);
8e2f54bc
L
510
511DESCRIPTION
512 Return the file size (as read from file system) for the file
513 associated with BFD @var{abfd}. It supports both normal files
514 and archive elements.
515
516*/
517
47fdcf63 518ufile_ptr
8e2f54bc
L
519bfd_get_file_size (bfd *abfd)
520{
b570b954
AM
521 ufile_ptr file_size, archive_size = (ufile_ptr) -1;
522
8e2f54bc
L
523 if (abfd->my_archive != NULL
524 && !bfd_is_thin_archive (abfd->my_archive))
b570b954
AM
525 {
526 struct areltdata *adata = (struct areltdata *) abfd->arelt_data;
c01de193
AM
527 if (adata != NULL)
528 {
529 archive_size = adata->parsed_size;
530 /* If the archive is compressed we can't compare against
531 file size. */
532 if (adata->arch_header != NULL
533 && memcmp (((struct ar_hdr *) adata->arch_header)->ar_fmag,
534 "Z\012", 2) == 0)
535 return archive_size;
536 abfd = abfd->my_archive;
537 }
b570b954 538 }
8e2f54bc 539
b570b954
AM
540 file_size = bfd_get_size (abfd);
541 if (archive_size < file_size)
542 return archive_size;
543 return file_size;
8e2f54bc 544}
25b88f33
PP
545
546/*
547FUNCTION
548 bfd_mmap
549
550SYNOPSIS
551 void *bfd_mmap (bfd *abfd, void *addr, bfd_size_type len,
07d6d2b8
AM
552 int prot, int flags, file_ptr offset,
553 void **map_addr, bfd_size_type *map_len);
25b88f33
PP
554
555DESCRIPTION
556 Return mmap()ed region of the file, if possible and implemented.
07d6d2b8
AM
557 LEN and OFFSET do not need to be page aligned. The page aligned
558 address and length are written to MAP_ADDR and MAP_LEN.
25b88f33
PP
559
560*/
561
562void *
563bfd_mmap (bfd *abfd, void *addr, bfd_size_type len,
4c95ab76 564 int prot, int flags, file_ptr offset,
07d6d2b8 565 void **map_addr, bfd_size_type *map_len)
25b88f33 566{
5c4ce239
AM
567 while (abfd->my_archive != NULL
568 && !bfd_is_thin_archive (abfd->my_archive))
569 {
570 offset += abfd->origin;
571 abfd = abfd->my_archive;
572 }
4d095f5b 573 offset += abfd->origin;
25b88f33
PP
574
575 if (abfd->iovec == NULL)
5c4ce239
AM
576 {
577 bfd_set_error (bfd_error_invalid_operation);
578 return (void *) -1;
579 }
25b88f33 580
4c95ab76 581 return abfd->iovec->bmmap (abfd, addr, len, prot, flags, offset,
07d6d2b8 582 map_addr, map_len);
25b88f33 583}
65077aa8
TG
584
585/* Memory file I/O operations. */
586
587static file_ptr
588memory_bread (bfd *abfd, void *ptr, file_ptr size)
589{
590 struct bfd_in_memory *bim;
591 bfd_size_type get;
592
593 bim = (struct bfd_in_memory *) abfd->iostream;
594 get = size;
595 if (abfd->where + get > bim->size)
596 {
597 if (bim->size < (bfd_size_type) abfd->where)
07d6d2b8 598 get = 0;
65077aa8 599 else
07d6d2b8 600 get = bim->size - abfd->where;
65077aa8
TG
601 bfd_set_error (bfd_error_file_truncated);
602 }
603 memcpy (ptr, bim->buffer + abfd->where, (size_t) get);
604 return get;
605}
606
607static file_ptr
608memory_bwrite (bfd *abfd, const void *ptr, file_ptr size)
609{
610 struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream;
611
612 if (abfd->where + size > bim->size)
613 {
614 bfd_size_type newsize, oldsize;
615
616 oldsize = (bim->size + 127) & ~(bfd_size_type) 127;
617 bim->size = abfd->where + size;
618 /* Round up to cut down on memory fragmentation */
619 newsize = (bim->size + 127) & ~(bfd_size_type) 127;
620 if (newsize > oldsize)
07d6d2b8
AM
621 {
622 bim->buffer = (bfd_byte *) bfd_realloc_or_free (bim->buffer, newsize);
623 if (bim->buffer == NULL)
624 {
625 bim->size = 0;
626 return 0;
627 }
628 if (newsize > bim->size)
629 memset (bim->buffer + bim->size, 0, newsize - bim->size);
630 }
65077aa8
TG
631 }
632 memcpy (bim->buffer + abfd->where, ptr, (size_t) size);
633 return size;
634}
635
636static file_ptr
637memory_btell (bfd *abfd)
638{
639 return abfd->where;
640}
641
642static int
643memory_bseek (bfd *abfd, file_ptr position, int direction)
644{
645 file_ptr nwhere;
646 struct bfd_in_memory *bim;
647
648 bim = (struct bfd_in_memory *) abfd->iostream;
649
650 if (direction == SEEK_SET)
651 nwhere = position;
652 else
653 nwhere = abfd->where + position;
654
655 if (nwhere < 0)
656 {
657 abfd->where = 0;
658 errno = EINVAL;
659 return -1;
660 }
661
662 if ((bfd_size_type)nwhere > bim->size)
663 {
664 if (abfd->direction == write_direction
07d6d2b8
AM
665 || abfd->direction == both_direction)
666 {
667 bfd_size_type newsize, oldsize;
668
669 oldsize = (bim->size + 127) & ~(bfd_size_type) 127;
670 bim->size = nwhere;
671 /* Round up to cut down on memory fragmentation */
672 newsize = (bim->size + 127) & ~(bfd_size_type) 127;
673 if (newsize > oldsize)
674 {
675 bim->buffer = (bfd_byte *) bfd_realloc_or_free (bim->buffer, newsize);
676 if (bim->buffer == NULL)
677 {
678 errno = EINVAL;
679 bim->size = 0;
680 return -1;
681 }
682 memset (bim->buffer + oldsize, 0, newsize - oldsize);
683 }
684 }
65077aa8 685 else
07d6d2b8
AM
686 {
687 abfd->where = bim->size;
688 errno = EINVAL;
689 bfd_set_error (bfd_error_file_truncated);
690 return -1;
691 }
65077aa8
TG
692 }
693 return 0;
694}
695
405bf443 696static int
65077aa8
TG
697memory_bclose (struct bfd *abfd)
698{
699 struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream;
700
c9594989 701 free (bim->buffer);
65077aa8
TG
702 free (bim);
703 abfd->iostream = NULL;
704
405bf443 705 return 0;
65077aa8
TG
706}
707
708static int
709memory_bflush (bfd *abfd ATTRIBUTE_UNUSED)
710{
711 return 0;
712}
713
714static int
715memory_bstat (bfd *abfd, struct stat *statbuf)
716{
717 struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream;
718
b5dee4ea 719 memset (statbuf, 0, sizeof (*statbuf));
65077aa8
TG
720 statbuf->st_size = bim->size;
721
722 return 0;
723}
724
725static void *
726memory_bmmap (bfd *abfd ATTRIBUTE_UNUSED, void *addr ATTRIBUTE_UNUSED,
07d6d2b8
AM
727 bfd_size_type len ATTRIBUTE_UNUSED, int prot ATTRIBUTE_UNUSED,
728 int flags ATTRIBUTE_UNUSED, file_ptr offset ATTRIBUTE_UNUSED,
729 void **map_addr ATTRIBUTE_UNUSED,
730 bfd_size_type *map_len ATTRIBUTE_UNUSED)
65077aa8
TG
731{
732 return (void *)-1;
733}
734
735const struct bfd_iovec _bfd_memory_iovec =
736{
737 &memory_bread, &memory_bwrite, &memory_btell, &memory_bseek,
738 &memory_bclose, &memory_bflush, &memory_bstat, &memory_bmmap
739};