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. */
34 #define S_IXUSR 0100 /* Execute by owner. */
37 #define S_IXGRP 0010 /* Execute by group. */
40 #define S_IXOTH 0001 /* Execute by others. */
48 _bfd_real_ftell (FILE *file
)
50 #if defined (HAVE_FTELLO64)
51 return ftello64 (file
);
52 #elif defined (HAVE_FTELLO)
60 _bfd_real_fseek (FILE *file
, file_ptr offset
, int whence
)
62 #if defined (HAVE_FSEEKO64)
63 return fseeko64 (file
, offset
, whence
);
64 #elif defined (HAVE_FSEEKO)
65 return fseeko (file
, offset
, whence
);
67 return fseek (file
, offset
, whence
);
71 /* Mark FILE as close-on-exec. Return FILE. FILE may be NULL, in
72 which case nothing is done. */
74 close_on_exec (FILE *file
)
76 #if defined (HAVE_FILENO) && defined (F_GETFD)
79 int fd
= fileno (file
);
80 int old
= fcntl (fd
, F_GETFD
, 0);
82 fcntl (fd
, F_SETFD
, old
| FD_CLOEXEC
);
89 _bfd_real_fopen (const char *filename
, const char *modes
)
94 /* On VMS, fopen allows file attributes as optional arguments.
95 We need to use them but we'd better to use the common prototype.
96 In fopen-vms.h, they are separated from the mode with a comma.
98 vms_attr
= strchr (modes
, ',');
101 /* Attributes found. Split. */
102 size_t modes_len
= strlen (modes
) + 1;
103 char attrs
[modes_len
+ 1];
107 memcpy (attrs
, modes
, modes_len
);
109 for (i
= 0; i
< 2; i
++)
111 at
[i
+ 1] = strchr (at
[i
], ',');
112 BFD_ASSERT (at
[i
+ 1] != NULL
);
113 *(at
[i
+ 1]++) = 0; /* Replace ',' with a nul, and skip it. */
115 return close_on_exec (fopen (filename
, at
[0], at
[1], at
[2]));
118 #elif defined (_WIN32)
119 size_t filelen
= strlen (filename
) + 1;
121 if (filelen
> MAX_PATH
- 1)
124 char * fullpath
= (char *) malloc (filelen
+ 8);
127 /* Add a Microsoft recommended prefix that
128 will allow the extra-long path to work. */
129 strcpy (fullpath
, "\\\\?\\");
130 strcat (fullpath
, filename
);
132 /* Convert any UNIX style path separators into the DOS form. */
133 for (i
= 0; fullpath
[i
]; i
++)
135 if (IS_UNIX_DIR_SEPARATOR (fullpath
[i
]))
139 file
= close_on_exec (fopen (fullpath
, modes
));
144 #elif defined (HAVE_FOPEN64)
145 return close_on_exec (fopen64 (filename
, modes
));
148 return close_on_exec (fopen (filename
, modes
));
157 The <<struct bfd_iovec>> contains the internal file I/O class.
158 Each <<BFD>> has an instance of this class and all file I/O is
159 routed through it (it is assumed that the instance implements
160 all methods listed below).
164 . {* To avoid problems with macros, a "b" rather than "f"
165 . prefix is prepended to each method name. *}
166 . {* Attempt to read/write NBYTES on ABFD's IOSTREAM storing/fetching
167 . bytes starting at PTR. Return the number of bytes actually
168 . transfered (a read past end-of-file returns less than NBYTES),
169 . or -1 (setting <<bfd_error>>) if an error occurs. *}
170 . file_ptr (*bread) (struct bfd *abfd, void *ptr, file_ptr nbytes);
171 . file_ptr (*bwrite) (struct bfd *abfd, const void *ptr,
173 . {* Return the current IOSTREAM file offset, or -1 (setting <<bfd_error>>
174 . if an error occurs. *}
175 . file_ptr (*btell) (struct bfd *abfd);
176 . {* For the following, on successful completion a value of 0 is returned.
177 . Otherwise, a value of -1 is returned (and <<bfd_error>> is set). *}
178 . int (*bseek) (struct bfd *abfd, file_ptr offset, int whence);
179 . int (*bclose) (struct bfd *abfd);
180 . int (*bflush) (struct bfd *abfd);
181 . int (*bstat) (struct bfd *abfd, struct stat *sb);
182 . {* Mmap a part of the files. ADDR, LEN, PROT, FLAGS and OFFSET are the usual
183 . mmap parameter, except that LEN and OFFSET do not need to be page
184 . aligned. Returns (void *)-1 on failure, mmapped address on success.
185 . Also write in MAP_ADDR the address of the page aligned buffer and in
186 . MAP_LEN the size mapped (a page multiple). Use unmap with MAP_ADDR and
187 . MAP_LEN to unmap. *}
188 . void *(*bmmap) (struct bfd *abfd, void *addr, bfd_size_type len,
189 . int prot, int flags, file_ptr offset,
190 . void **map_addr, bfd_size_type *map_len);
193 .extern const struct bfd_iovec _bfd_memory_iovec;
198 /* Return value is amount read. */
201 bfd_bread (void *ptr
, bfd_size_type size
, bfd
*abfd
)
204 bfd
*element_bfd
= abfd
;
205 ufile_ptr offset
= 0;
207 while (abfd
->my_archive
!= NULL
208 && !bfd_is_thin_archive (abfd
->my_archive
))
210 offset
+= abfd
->origin
;
211 abfd
= abfd
->my_archive
;
213 offset
+= abfd
->origin
;
215 /* If this is an archive element, don't read past the end of
217 if (element_bfd
->arelt_data
!= NULL
)
219 bfd_size_type maxbytes
= arelt_size (element_bfd
);
221 if (abfd
->where
< offset
|| abfd
->where
- offset
>= maxbytes
)
223 bfd_set_error (bfd_error_invalid_operation
);
226 if (abfd
->where
- offset
+ size
> maxbytes
)
227 size
= maxbytes
- (abfd
->where
- offset
);
230 if (abfd
->iovec
== NULL
)
232 bfd_set_error (bfd_error_invalid_operation
);
236 nread
= abfd
->iovec
->bread (abfd
, ptr
, size
);
238 abfd
->where
+= nread
;
244 bfd_bwrite (const void *ptr
, bfd_size_type size
, bfd
*abfd
)
248 while (abfd
->my_archive
!= NULL
249 && !bfd_is_thin_archive (abfd
->my_archive
))
250 abfd
= abfd
->my_archive
;
252 if (abfd
->iovec
== NULL
)
254 bfd_set_error (bfd_error_invalid_operation
);
258 nwrote
= abfd
->iovec
->bwrite (abfd
, ptr
, size
);
260 abfd
->where
+= nwrote
;
261 if ((bfd_size_type
) nwrote
!= size
)
266 bfd_set_error (bfd_error_system_call
);
274 ufile_ptr offset
= 0;
277 while (abfd
->my_archive
!= NULL
278 && !bfd_is_thin_archive (abfd
->my_archive
))
280 offset
+= abfd
->origin
;
281 abfd
= abfd
->my_archive
;
283 offset
+= abfd
->origin
;
285 if (abfd
->iovec
== NULL
)
288 ptr
= abfd
->iovec
->btell (abfd
);
294 bfd_flush (bfd
*abfd
)
296 while (abfd
->my_archive
!= NULL
297 && !bfd_is_thin_archive (abfd
->my_archive
))
298 abfd
= abfd
->my_archive
;
300 if (abfd
->iovec
== NULL
)
303 return abfd
->iovec
->bflush (abfd
);
306 /* Returns 0 for success, negative value for failure (in which case
307 bfd_get_error can retrieve the error code). */
309 bfd_stat (bfd
*abfd
, struct stat
*statbuf
)
313 while (abfd
->my_archive
!= NULL
314 && !bfd_is_thin_archive (abfd
->my_archive
))
315 abfd
= abfd
->my_archive
;
317 if (abfd
->iovec
== NULL
)
319 bfd_set_error (bfd_error_invalid_operation
);
323 result
= abfd
->iovec
->bstat (abfd
, statbuf
);
325 bfd_set_error (bfd_error_system_call
);
329 /* Returns 0 for success, nonzero for failure (in which case bfd_get_error
330 can retrieve the error code). */
333 bfd_seek (bfd
*abfd
, file_ptr position
, int direction
)
336 ufile_ptr offset
= 0;
338 while (abfd
->my_archive
!= NULL
339 && !bfd_is_thin_archive (abfd
->my_archive
))
341 offset
+= abfd
->origin
;
342 abfd
= abfd
->my_archive
;
344 offset
+= abfd
->origin
;
346 if (abfd
->iovec
== NULL
)
348 bfd_set_error (bfd_error_invalid_operation
);
352 /* For the time being, a BFD may not seek to it's end. The problem
353 is that we don't easily have a way to recognize the end of an
354 element in an archive. */
355 BFD_ASSERT (direction
== SEEK_SET
|| direction
== SEEK_CUR
);
357 if (direction
!= SEEK_CUR
)
360 if ((direction
== SEEK_CUR
&& position
== 0)
361 || (direction
== SEEK_SET
&& (ufile_ptr
) position
== abfd
->where
))
364 result
= abfd
->iovec
->bseek (abfd
, position
, direction
);
367 /* An EINVAL error probably means that the file offset was
370 bfd_set_error (bfd_error_file_truncated
);
372 bfd_set_error (bfd_error_system_call
);
376 /* Adjust `where' field. */
377 if (direction
== SEEK_CUR
)
378 abfd
->where
+= position
;
380 abfd
->where
= position
;
391 long bfd_get_mtime (bfd *abfd);
394 Return the file modification time (as read from the file system, or
395 from the archive header for archive members).
400 bfd_get_mtime (bfd
*abfd
)
407 if (bfd_stat (abfd
, &buf
) != 0)
410 abfd
->mtime
= buf
.st_mtime
; /* Save value in case anyone wants it */
419 ufile_ptr bfd_get_size (bfd *abfd);
422 Return the file size (as read from file system) for the file
423 associated with BFD @var{abfd}.
425 The initial motivation for, and use of, this routine is not
426 so we can get the exact size of the object the BFD applies to, since
427 that might not be generally possible (archive members for example).
428 It would be ideal if someone could eventually modify
429 it so that such results were guaranteed.
431 Instead, we want to ask questions like "is this NNN byte sized
432 object I'm about to try read from file offset YYY reasonable?"
433 As as example of where we might do this, some object formats
434 use string tables for which the first <<sizeof (long)>> bytes of the
435 table contain the size of the table itself, including the size bytes.
436 If an application tries to read what it thinks is one of these
437 string tables, without some way to validate the size, and for
438 some reason the size is wrong (byte swapping error, wrong location
439 for the string table, etc.), the only clue is likely to be a read
440 error when it tries to read the table, or a "virtual memory
441 exhausted" error when it tries to allocate 15 bazillon bytes
442 of space for the 15 bazillon byte table it is about to read.
443 This function at least allows us to answer the question, "is the
446 A return value of zero indicates the file size is unknown.
450 bfd_get_size (bfd
*abfd
)
452 /* A size of 0 means we haven't yet called bfd_stat. A size of 1
453 means we have a cached value of 0, ie. unknown. */
454 if (abfd
->size
<= 1 || bfd_write_p (abfd
))
458 if (abfd
->size
== 1 && !bfd_write_p (abfd
))
461 if (bfd_stat (abfd
, &buf
) != 0
463 || buf
.st_size
- (ufile_ptr
) buf
.st_size
!= 0)
468 abfd
->size
= buf
.st_size
;
478 ufile_ptr bfd_get_file_size (bfd *abfd);
481 Return the file size (as read from file system) for the file
482 associated with BFD @var{abfd}. It supports both normal files
483 and archive elements.
488 bfd_get_file_size (bfd
*abfd
)
490 ufile_ptr file_size
, archive_size
= (ufile_ptr
) -1;
492 if (abfd
->my_archive
!= NULL
493 && !bfd_is_thin_archive (abfd
->my_archive
))
495 struct areltdata
*adata
= (struct areltdata
*) abfd
->arelt_data
;
496 archive_size
= adata
->parsed_size
;
497 /* If the archive is compressed we can't compare against file size. */
498 if (adata
->arch_header
!= NULL
499 && memcmp (((struct ar_hdr
*) adata
->arch_header
)->ar_fmag
,
502 abfd
= abfd
->my_archive
;
505 file_size
= bfd_get_size (abfd
);
506 if (archive_size
< file_size
)
516 void *bfd_mmap (bfd *abfd, void *addr, bfd_size_type len,
517 int prot, int flags, file_ptr offset,
518 void **map_addr, bfd_size_type *map_len);
521 Return mmap()ed region of the file, if possible and implemented.
522 LEN and OFFSET do not need to be page aligned. The page aligned
523 address and length are written to MAP_ADDR and MAP_LEN.
528 bfd_mmap (bfd
*abfd
, void *addr
, bfd_size_type len
,
529 int prot
, int flags
, file_ptr offset
,
530 void **map_addr
, bfd_size_type
*map_len
)
532 while (abfd
->my_archive
!= NULL
533 && !bfd_is_thin_archive (abfd
->my_archive
))
535 offset
+= abfd
->origin
;
536 abfd
= abfd
->my_archive
;
538 offset
+= abfd
->origin
;
540 if (abfd
->iovec
== NULL
)
542 bfd_set_error (bfd_error_invalid_operation
);
546 return abfd
->iovec
->bmmap (abfd
, addr
, len
, prot
, flags
, offset
,
550 /* Memory file I/O operations. */
553 memory_bread (bfd
*abfd
, void *ptr
, file_ptr size
)
555 struct bfd_in_memory
*bim
;
558 bim
= (struct bfd_in_memory
*) abfd
->iostream
;
560 if (abfd
->where
+ get
> bim
->size
)
562 if (bim
->size
< (bfd_size_type
) abfd
->where
)
565 get
= bim
->size
- abfd
->where
;
566 bfd_set_error (bfd_error_file_truncated
);
568 memcpy (ptr
, bim
->buffer
+ abfd
->where
, (size_t) get
);
573 memory_bwrite (bfd
*abfd
, const void *ptr
, file_ptr size
)
575 struct bfd_in_memory
*bim
= (struct bfd_in_memory
*) abfd
->iostream
;
577 if (abfd
->where
+ size
> bim
->size
)
579 bfd_size_type newsize
, oldsize
;
581 oldsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
582 bim
->size
= abfd
->where
+ size
;
583 /* Round up to cut down on memory fragmentation */
584 newsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
585 if (newsize
> oldsize
)
587 bim
->buffer
= (bfd_byte
*) bfd_realloc_or_free (bim
->buffer
, newsize
);
588 if (bim
->buffer
== NULL
)
593 if (newsize
> bim
->size
)
594 memset (bim
->buffer
+ bim
->size
, 0, newsize
- bim
->size
);
597 memcpy (bim
->buffer
+ abfd
->where
, ptr
, (size_t) size
);
602 memory_btell (bfd
*abfd
)
608 memory_bseek (bfd
*abfd
, file_ptr position
, int direction
)
611 struct bfd_in_memory
*bim
;
613 bim
= (struct bfd_in_memory
*) abfd
->iostream
;
615 if (direction
== SEEK_SET
)
618 nwhere
= abfd
->where
+ position
;
627 if ((bfd_size_type
)nwhere
> bim
->size
)
629 if (abfd
->direction
== write_direction
630 || abfd
->direction
== both_direction
)
632 bfd_size_type newsize
, oldsize
;
634 oldsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
636 /* Round up to cut down on memory fragmentation */
637 newsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
638 if (newsize
> oldsize
)
640 bim
->buffer
= (bfd_byte
*) bfd_realloc_or_free (bim
->buffer
, newsize
);
641 if (bim
->buffer
== NULL
)
647 memset (bim
->buffer
+ oldsize
, 0, newsize
- oldsize
);
652 abfd
->where
= bim
->size
;
654 bfd_set_error (bfd_error_file_truncated
);
662 memory_bclose (struct bfd
*abfd
)
664 struct bfd_in_memory
*bim
= (struct bfd_in_memory
*) abfd
->iostream
;
668 abfd
->iostream
= NULL
;
674 memory_bflush (bfd
*abfd ATTRIBUTE_UNUSED
)
680 memory_bstat (bfd
*abfd
, struct stat
*statbuf
)
682 struct bfd_in_memory
*bim
= (struct bfd_in_memory
*) abfd
->iostream
;
684 memset (statbuf
, 0, sizeof (*statbuf
));
685 statbuf
->st_size
= bim
->size
;
691 memory_bmmap (bfd
*abfd ATTRIBUTE_UNUSED
, void *addr ATTRIBUTE_UNUSED
,
692 bfd_size_type len ATTRIBUTE_UNUSED
, int prot ATTRIBUTE_UNUSED
,
693 int flags ATTRIBUTE_UNUSED
, file_ptr offset ATTRIBUTE_UNUSED
,
694 void **map_addr ATTRIBUTE_UNUSED
,
695 bfd_size_type
*map_len ATTRIBUTE_UNUSED
)
700 const struct bfd_iovec _bfd_memory_iovec
=
702 &memory_bread
, &memory_bwrite
, &memory_btell
, &memory_bseek
,
703 &memory_bclose
, &memory_bflush
, &memory_bstat
, &memory_bmmap