1 /* Low-level I/O routines for BFDs.
3 Copyright (C) 1990-2020 Free Software Foundation, Inc.
5 Written by Cygnus Support.
7 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
31 #define S_IXUSR 0100 /* Execute by owner. */
34 #define S_IXGRP 0010 /* Execute by group. */
37 #define S_IXOTH 0001 /* Execute by others. */
45 _bfd_real_ftell (FILE *file
)
47 #if defined (HAVE_FTELLO64)
48 return ftello64 (file
);
49 #elif defined (HAVE_FTELLO)
57 _bfd_real_fseek (FILE *file
, file_ptr offset
, int whence
)
59 #if defined (HAVE_FSEEKO64)
60 return fseeko64 (file
, offset
, whence
);
61 #elif defined (HAVE_FSEEKO)
62 return fseeko (file
, offset
, whence
);
64 return fseek (file
, offset
, whence
);
68 /* Mark FILE as close-on-exec. Return FILE. FILE may be NULL, in
69 which case nothing is done. */
71 close_on_exec (FILE *file
)
73 #if defined (HAVE_FILENO) && defined (F_GETFD)
76 int fd
= fileno (file
);
77 int old
= fcntl (fd
, F_GETFD
, 0);
79 fcntl (fd
, F_SETFD
, old
| FD_CLOEXEC
);
86 _bfd_real_fopen (const char *filename
, const char *modes
)
91 /* On VMS, fopen allows file attributes as optional arguments.
92 We need to use them but we'd better to use the common prototype.
93 In fopen-vms.h, they are separated from the mode with a comma.
95 vms_attr
= strchr (modes
, ',');
99 return close_on_exec (fopen (filename
, modes
));
103 /* Attributes found. Split. */
104 size_t modes_len
= strlen (modes
) + 1;
105 char attrs
[modes_len
+ 1];
109 memcpy (attrs
, modes
, modes_len
);
111 for (i
= 0; i
< 2; i
++)
113 at
[i
+ 1] = strchr (at
[i
], ',');
114 BFD_ASSERT (at
[i
+ 1] != NULL
);
115 *(at
[i
+ 1]++) = 0; /* Replace ',' with a nul, and skip it. */
117 return close_on_exec (fopen (filename
, at
[0], at
[1], at
[2]));
120 #if defined (HAVE_FOPEN64)
121 return close_on_exec (fopen64 (filename
, modes
));
123 return close_on_exec (fopen (filename
, modes
));
134 The <<struct bfd_iovec>> contains the internal file I/O class.
135 Each <<BFD>> has an instance of this class and all file I/O is
136 routed through it (it is assumed that the instance implements
137 all methods listed below).
141 . {* To avoid problems with macros, a "b" rather than "f"
142 . prefix is prepended to each method name. *}
143 . {* Attempt to read/write NBYTES on ABFD's IOSTREAM storing/fetching
144 . bytes starting at PTR. Return the number of bytes actually
145 . transfered (a read past end-of-file returns less than NBYTES),
146 . or -1 (setting <<bfd_error>>) if an error occurs. *}
147 . file_ptr (*bread) (struct bfd *abfd, void *ptr, file_ptr nbytes);
148 . file_ptr (*bwrite) (struct bfd *abfd, const void *ptr,
150 . {* Return the current IOSTREAM file offset, or -1 (setting <<bfd_error>>
151 . if an error occurs. *}
152 . file_ptr (*btell) (struct bfd *abfd);
153 . {* For the following, on successful completion a value of 0 is returned.
154 . Otherwise, a value of -1 is returned (and <<bfd_error>> is set). *}
155 . int (*bseek) (struct bfd *abfd, file_ptr offset, int whence);
156 . int (*bclose) (struct bfd *abfd);
157 . int (*bflush) (struct bfd *abfd);
158 . int (*bstat) (struct bfd *abfd, struct stat *sb);
159 . {* Mmap a part of the files. ADDR, LEN, PROT, FLAGS and OFFSET are the usual
160 . mmap parameter, except that LEN and OFFSET do not need to be page
161 . aligned. Returns (void *)-1 on failure, mmapped address on success.
162 . Also write in MAP_ADDR the address of the page aligned buffer and in
163 . MAP_LEN the size mapped (a page multiple). Use unmap with MAP_ADDR and
164 . MAP_LEN to unmap. *}
165 . void *(*bmmap) (struct bfd *abfd, void *addr, bfd_size_type len,
166 . int prot, int flags, file_ptr offset,
167 . void **map_addr, bfd_size_type *map_len);
170 .extern const struct bfd_iovec _bfd_memory_iovec;
175 /* Return value is amount read. */
178 bfd_bread (void *ptr
, bfd_size_type size
, bfd
*abfd
)
181 bfd
*element_bfd
= abfd
;
182 ufile_ptr offset
= 0;
184 while (abfd
->my_archive
!= NULL
185 && !bfd_is_thin_archive (abfd
->my_archive
))
187 offset
+= abfd
->origin
;
188 abfd
= abfd
->my_archive
;
191 /* If this is an archive element, don't read past the end of
193 if (element_bfd
->arelt_data
!= NULL
)
195 bfd_size_type maxbytes
= arelt_size (element_bfd
);
197 if (abfd
->where
< offset
|| abfd
->where
- offset
>= maxbytes
)
199 bfd_set_error (bfd_error_invalid_operation
);
202 if (abfd
->where
- offset
+ size
> maxbytes
)
203 size
= maxbytes
- (abfd
->where
- offset
);
206 if (abfd
->iovec
== NULL
)
208 bfd_set_error (bfd_error_invalid_operation
);
212 nread
= abfd
->iovec
->bread (abfd
, ptr
, size
);
214 abfd
->where
+= nread
;
220 bfd_bwrite (const void *ptr
, bfd_size_type size
, bfd
*abfd
)
224 while (abfd
->my_archive
!= NULL
225 && !bfd_is_thin_archive (abfd
->my_archive
))
226 abfd
= abfd
->my_archive
;
228 if (abfd
->iovec
== NULL
)
230 bfd_set_error (bfd_error_invalid_operation
);
234 nwrote
= abfd
->iovec
->bwrite (abfd
, ptr
, size
);
236 abfd
->where
+= nwrote
;
237 if ((bfd_size_type
) nwrote
!= size
)
242 bfd_set_error (bfd_error_system_call
);
250 ufile_ptr offset
= 0;
253 while (abfd
->my_archive
!= NULL
254 && !bfd_is_thin_archive (abfd
->my_archive
))
256 offset
+= abfd
->origin
;
257 abfd
= abfd
->my_archive
;
260 if (abfd
->iovec
== NULL
)
263 ptr
= abfd
->iovec
->btell (abfd
);
269 bfd_flush (bfd
*abfd
)
271 while (abfd
->my_archive
!= NULL
272 && !bfd_is_thin_archive (abfd
->my_archive
))
273 abfd
= abfd
->my_archive
;
275 if (abfd
->iovec
== NULL
)
278 return abfd
->iovec
->bflush (abfd
);
281 /* Returns 0 for success, negative value for failure (in which case
282 bfd_get_error can retrieve the error code). */
284 bfd_stat (bfd
*abfd
, struct stat
*statbuf
)
288 while (abfd
->my_archive
!= NULL
289 && !bfd_is_thin_archive (abfd
->my_archive
))
290 abfd
= abfd
->my_archive
;
292 if (abfd
->iovec
== NULL
)
294 bfd_set_error (bfd_error_invalid_operation
);
298 result
= abfd
->iovec
->bstat (abfd
, statbuf
);
300 bfd_set_error (bfd_error_system_call
);
304 /* Returns 0 for success, nonzero for failure (in which case bfd_get_error
305 can retrieve the error code). */
308 bfd_seek (bfd
*abfd
, file_ptr position
, int direction
)
311 ufile_ptr offset
= 0;
313 while (abfd
->my_archive
!= NULL
314 && !bfd_is_thin_archive (abfd
->my_archive
))
316 offset
+= abfd
->origin
;
317 abfd
= abfd
->my_archive
;
320 if (abfd
->iovec
== NULL
)
322 bfd_set_error (bfd_error_invalid_operation
);
326 /* For the time being, a BFD may not seek to it's end. The problem
327 is that we don't easily have a way to recognize the end of an
328 element in an archive. */
329 BFD_ASSERT (direction
== SEEK_SET
|| direction
== SEEK_CUR
);
331 if (direction
!= SEEK_CUR
)
334 if ((direction
== SEEK_CUR
&& position
== 0)
335 || (direction
== SEEK_SET
&& (ufile_ptr
) position
== abfd
->where
))
338 result
= abfd
->iovec
->bseek (abfd
, position
, direction
);
341 /* An EINVAL error probably means that the file offset was
344 bfd_set_error (bfd_error_file_truncated
);
346 bfd_set_error (bfd_error_system_call
);
350 /* Adjust `where' field. */
351 if (direction
== SEEK_CUR
)
352 abfd
->where
+= position
;
354 abfd
->where
= position
;
365 long bfd_get_mtime (bfd *abfd);
368 Return the file modification time (as read from the file system, or
369 from the archive header for archive members).
374 bfd_get_mtime (bfd
*abfd
)
381 if (bfd_stat (abfd
, &buf
) != 0)
384 abfd
->mtime
= buf
.st_mtime
; /* Save value in case anyone wants it */
393 ufile_ptr bfd_get_size (bfd *abfd);
396 Return the file size (as read from file system) for the file
397 associated with BFD @var{abfd}.
399 The initial motivation for, and use of, this routine is not
400 so we can get the exact size of the object the BFD applies to, since
401 that might not be generally possible (archive members for example).
402 It would be ideal if someone could eventually modify
403 it so that such results were guaranteed.
405 Instead, we want to ask questions like "is this NNN byte sized
406 object I'm about to try read from file offset YYY reasonable?"
407 As as example of where we might do this, some object formats
408 use string tables for which the first <<sizeof (long)>> bytes of the
409 table contain the size of the table itself, including the size bytes.
410 If an application tries to read what it thinks is one of these
411 string tables, without some way to validate the size, and for
412 some reason the size is wrong (byte swapping error, wrong location
413 for the string table, etc.), the only clue is likely to be a read
414 error when it tries to read the table, or a "virtual memory
415 exhausted" error when it tries to allocate 15 bazillon bytes
416 of space for the 15 bazillon byte table it is about to read.
417 This function at least allows us to answer the question, "is the
420 A return value of zero indicates the file size is unknown.
424 bfd_get_size (bfd
*abfd
)
426 /* A size of 0 means we haven't yet called bfd_stat. A size of 1
427 means we have a cached value of 0, ie. unknown. */
428 if (abfd
->size
<= 1 || bfd_write_p (abfd
))
432 if (abfd
->size
== 1 && !bfd_write_p (abfd
))
435 if (bfd_stat (abfd
, &buf
) != 0
437 || buf
.st_size
- (ufile_ptr
) buf
.st_size
!= 0)
442 abfd
->size
= buf
.st_size
;
452 ufile_ptr bfd_get_file_size (bfd *abfd);
455 Return the file size (as read from file system) for the file
456 associated with BFD @var{abfd}. It supports both normal files
457 and archive elements.
462 bfd_get_file_size (bfd
*abfd
)
464 ufile_ptr file_size
, archive_size
= (ufile_ptr
) -1;
466 if (abfd
->my_archive
!= NULL
467 && !bfd_is_thin_archive (abfd
->my_archive
))
469 struct areltdata
*adata
= (struct areltdata
*) abfd
->arelt_data
;
470 archive_size
= adata
->parsed_size
;
471 /* If the archive is compressed we can't compare against file size. */
472 if (memcmp (((struct ar_hdr
*) adata
->arch_header
)->ar_fmag
,
475 abfd
= abfd
->my_archive
;
478 file_size
= bfd_get_size (abfd
);
479 if (archive_size
< file_size
)
489 void *bfd_mmap (bfd *abfd, void *addr, bfd_size_type len,
490 int prot, int flags, file_ptr offset,
491 void **map_addr, bfd_size_type *map_len);
494 Return mmap()ed region of the file, if possible and implemented.
495 LEN and OFFSET do not need to be page aligned. The page aligned
496 address and length are written to MAP_ADDR and MAP_LEN.
501 bfd_mmap (bfd
*abfd
, void *addr
, bfd_size_type len
,
502 int prot
, int flags
, file_ptr offset
,
503 void **map_addr
, bfd_size_type
*map_len
)
505 while (abfd
->my_archive
!= NULL
506 && !bfd_is_thin_archive (abfd
->my_archive
))
508 offset
+= abfd
->origin
;
509 abfd
= abfd
->my_archive
;
512 if (abfd
->iovec
== NULL
)
514 bfd_set_error (bfd_error_invalid_operation
);
518 return abfd
->iovec
->bmmap (abfd
, addr
, len
, prot
, flags
, offset
,
522 /* Memory file I/O operations. */
525 memory_bread (bfd
*abfd
, void *ptr
, file_ptr size
)
527 struct bfd_in_memory
*bim
;
530 bim
= (struct bfd_in_memory
*) abfd
->iostream
;
532 if (abfd
->where
+ get
> bim
->size
)
534 if (bim
->size
< (bfd_size_type
) abfd
->where
)
537 get
= bim
->size
- abfd
->where
;
538 bfd_set_error (bfd_error_file_truncated
);
540 memcpy (ptr
, bim
->buffer
+ abfd
->where
, (size_t) get
);
545 memory_bwrite (bfd
*abfd
, const void *ptr
, file_ptr size
)
547 struct bfd_in_memory
*bim
= (struct bfd_in_memory
*) abfd
->iostream
;
549 if (abfd
->where
+ size
> bim
->size
)
551 bfd_size_type newsize
, oldsize
;
553 oldsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
554 bim
->size
= abfd
->where
+ size
;
555 /* Round up to cut down on memory fragmentation */
556 newsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
557 if (newsize
> oldsize
)
559 bim
->buffer
= (bfd_byte
*) bfd_realloc_or_free (bim
->buffer
, newsize
);
560 if (bim
->buffer
== NULL
)
565 if (newsize
> bim
->size
)
566 memset (bim
->buffer
+ bim
->size
, 0, newsize
- bim
->size
);
569 memcpy (bim
->buffer
+ abfd
->where
, ptr
, (size_t) size
);
574 memory_btell (bfd
*abfd
)
580 memory_bseek (bfd
*abfd
, file_ptr position
, int direction
)
583 struct bfd_in_memory
*bim
;
585 bim
= (struct bfd_in_memory
*) abfd
->iostream
;
587 if (direction
== SEEK_SET
)
590 nwhere
= abfd
->where
+ position
;
599 if ((bfd_size_type
)nwhere
> bim
->size
)
601 if (abfd
->direction
== write_direction
602 || abfd
->direction
== both_direction
)
604 bfd_size_type newsize
, oldsize
;
606 oldsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
608 /* Round up to cut down on memory fragmentation */
609 newsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
610 if (newsize
> oldsize
)
612 bim
->buffer
= (bfd_byte
*) bfd_realloc_or_free (bim
->buffer
, newsize
);
613 if (bim
->buffer
== NULL
)
619 memset (bim
->buffer
+ oldsize
, 0, newsize
- oldsize
);
624 abfd
->where
= bim
->size
;
626 bfd_set_error (bfd_error_file_truncated
);
634 memory_bclose (struct bfd
*abfd
)
636 struct bfd_in_memory
*bim
= (struct bfd_in_memory
*) abfd
->iostream
;
638 if (bim
->buffer
!= NULL
)
641 abfd
->iostream
= NULL
;
647 memory_bflush (bfd
*abfd ATTRIBUTE_UNUSED
)
653 memory_bstat (bfd
*abfd
, struct stat
*statbuf
)
655 struct bfd_in_memory
*bim
= (struct bfd_in_memory
*) abfd
->iostream
;
657 memset (statbuf
, 0, sizeof (*statbuf
));
658 statbuf
->st_size
= bim
->size
;
664 memory_bmmap (bfd
*abfd ATTRIBUTE_UNUSED
, void *addr ATTRIBUTE_UNUSED
,
665 bfd_size_type len ATTRIBUTE_UNUSED
, int prot ATTRIBUTE_UNUSED
,
666 int flags ATTRIBUTE_UNUSED
, file_ptr offset ATTRIBUTE_UNUSED
,
667 void **map_addr ATTRIBUTE_UNUSED
,
668 bfd_size_type
*map_len ATTRIBUTE_UNUSED
)
673 const struct bfd_iovec _bfd_memory_iovec
=
675 &memory_bread
, &memory_bwrite
, &memory_btell
, &memory_bseek
,
676 &memory_bclose
, &memory_bflush
, &memory_bstat
, &memory_bmmap