1@findex struct bfd_iovec 2@subsubsection @code{struct bfd_iovec} 3The @code{struct bfd_iovec} contains the internal file I/O class. 4Each @code{BFD} has an instance of this class and all file I/O is 5routed through it (it is assumed that the instance implements 6all methods listed below). 7@example 8struct bfd_iovec 9@{ 10 /* To avoid problems with macros, a "b" rather than "f" 11 prefix is prepended to each method name. */ 12 /* Attempt to read/write NBYTES on ABFD's IOSTREAM storing/fetching 13 bytes starting at PTR. Return the number of bytes actually 14 transfered (a read past end-of-file returns less than NBYTES), 15 or -1 (setting @code{bfd_error}) if an error occurs. */ 16 file_ptr (*bread) (struct bfd *abfd, void *ptr, file_ptr nbytes); 17 file_ptr (*bwrite) (struct bfd *abfd, const void *ptr, 18 file_ptr nbytes); 19 /* Return the current IOSTREAM file offset, or -1 (setting @code{bfd_error} 20 if an error occurs. */ 21 file_ptr (*btell) (struct bfd *abfd); 22 /* For the following, on successful completion a value of 0 is returned. 23 Otherwise, a value of -1 is returned (and @code{bfd_error} is set). */ 24 int (*bseek) (struct bfd *abfd, file_ptr offset, int whence); 25 int (*bclose) (struct bfd *abfd); 26 int (*bflush) (struct bfd *abfd); 27 int (*bstat) (struct bfd *abfd, struct stat *sb); 28 /* Mmap a part of the files. ADDR, LEN, PROT, FLAGS and OFFSET are the usual 29 mmap parameter, except that LEN and OFFSET do not need to be page 30 aligned. Returns (void *)-1 on failure, mmapped address on success. 31 Also write in MAP_ADDR the address of the page aligned buffer and in 32 MAP_LEN the size mapped (a page multiple). Use unmap with MAP_ADDR and 33 MAP_LEN to unmap. */ 34 void *(*bmmap) (struct bfd *abfd, void *addr, bfd_size_type len, 35 int prot, int flags, file_ptr offset, 36 void **map_addr, bfd_size_type *map_len); 37@}; 38extern const struct bfd_iovec _bfd_memory_iovec; 39 40@end example 41 42@findex bfd_read 43@subsubsection @code{bfd_read} 44@deftypefn {Function} bfd_size_type bfd_read (void *, bfd_size_type, bfd *) ATTRIBUTE_WARN_UNUSED_RESULT; 45Attempt to read SIZE bytes from ABFD's iostream to PTR. 46Return the amount read. 47 48@end deftypefn 49@findex bfd_write 50@subsubsection @code{bfd_write} 51@deftypefn {Function} bfd_size_type bfd_write (const void *, bfd_size_type, bfd *) ATTRIBUTE_WARN_UNUSED_RESULT; 52Attempt to write SIZE bytes to ABFD's iostream from PTR. 53Return the amount written. 54 55@end deftypefn 56@findex bfd_tell 57@subsubsection @code{bfd_tell} 58@deftypefn {Function} file_ptr bfd_tell (bfd *) ATTRIBUTE_WARN_UNUSED_RESULT; 59Return ABFD's iostream file position. 60 61@end deftypefn 62@findex bfd_flush 63@subsubsection @code{bfd_flush} 64@deftypefn {Function} int bfd_flush (bfd *); 65Flush ABFD's iostream pending IO. 66 67@end deftypefn 68@findex bfd_stat 69@subsubsection @code{bfd_stat} 70@deftypefn {Function} int bfd_stat (bfd *, struct stat *) ATTRIBUTE_WARN_UNUSED_RESULT; 71Call fstat on ABFD's iostream. Return 0 on success, and a 72negative value on failure. 73 74@end deftypefn 75@findex bfd_seek 76@subsubsection @code{bfd_seek} 77@deftypefn {Function} int bfd_seek (bfd *, file_ptr, int) ATTRIBUTE_WARN_UNUSED_RESULT; 78Call fseek on ABFD's iostream. Return 0 on success, and a 79negative value on failure. 80 81@end deftypefn 82@findex bfd_get_mtime 83@subsubsection @code{bfd_get_mtime} 84@deftypefn {Function} long bfd_get_mtime (bfd *abfd); 85Return the file modification time (as read from the file system, or 86from the archive header for archive members). 87 88@end deftypefn 89@findex bfd_get_size 90@subsubsection @code{bfd_get_size} 91@deftypefn {Function} ufile_ptr bfd_get_size (bfd *abfd); 92Return the file size (as read from file system) for the file 93associated with BFD @var{abfd}. 94 95The initial motivation for, and use of, this routine is not 96so we can get the exact size of the object the BFD applies to, since 97that might not be generally possible (archive members for example). 98It would be ideal if someone could eventually modify 99it so that such results were guaranteed. 100 101Instead, we want to ask questions like "is this NNN byte sized 102object I'm about to try read from file offset YYY reasonable?" 103As as example of where we might do this, some object formats 104use string tables for which the first @code{sizeof (long)} bytes of the 105table contain the size of the table itself, including the size bytes. 106If an application tries to read what it thinks is one of these 107string tables, without some way to validate the size, and for 108some reason the size is wrong (byte swapping error, wrong location 109for the string table, etc.), the only clue is likely to be a read 110error when it tries to read the table, or a "virtual memory 111exhausted" error when it tries to allocate 15 bazillon bytes 112of space for the 15 bazillon byte table it is about to read. 113This function at least allows us to answer the question, "is the 114size reasonable?". 115 116A return value of zero indicates the file size is unknown. 117 118@end deftypefn 119@findex bfd_get_file_size 120@subsubsection @code{bfd_get_file_size} 121@deftypefn {Function} ufile_ptr bfd_get_file_size (bfd *abfd); 122Return the file size (as read from file system) for the file 123associated with BFD @var{abfd}. It supports both normal files 124and archive elements. 125 126@end deftypefn 127@findex bfd_mmap 128@subsubsection @code{bfd_mmap} 129@deftypefn {Function} void *bfd_mmap (bfd *abfd, void *addr, bfd_size_type len, int prot, int flags, file_ptr offset, void **map_addr, bfd_size_type *map_len) ATTRIBUTE_WARN_UNUSED_RESULT; 130Return mmap()ed region of the file, if possible and implemented. 131LEN and OFFSET do not need to be page aligned. The page aligned 132address and length are written to MAP_ADDR and MAP_LEN. 133 134@end deftypefn 135@findex bfd_get_current_time 136@subsubsection @code{bfd_get_current_time} 137@deftypefn {Function} time_t bfd_get_current_time (time_t now); 138Returns the current time. 139 140If the environment variable SOURCE_DATE_EPOCH is defined 141then this is parsed and its value is returned. Otherwise 142if the paramter NOW is non-zero, then that is returned. 143Otherwise the result of the system call "time(NULL)" is 144returned. 145 146@end deftypefn 147