1 @findex struct bfd_iovec 2 @subsubsection @code{struct bfd_iovec} 3 The @code{struct bfd_iovec} contains the internal file I/O class. 4 Each @code{BFD} has an instance of this class and all file I/O is 5 routed through it (it is assumed that the instance implements 6 all methods listed below). 7 @example 8 struct 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 MAP_FAILED 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, size_t len, 35 int prot, int flags, file_ptr offset, 36 void **map_addr, size_t *map_len); 37 @}; 38 extern 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; 45 Attempt to read SIZE bytes from ABFD's iostream to PTR. 46 Return 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; 52 Attempt to write SIZE bytes to ABFD's iostream from PTR. 53 Return 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; 59 Return 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 *); 65 Flush 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; 71 Call fstat on ABFD's iostream. Return 0 on success, and a 72 negative 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; 78 Call fseek on ABFD's iostream. Return 0 on success, and a 79 negative 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); 85 Return the file modification time (as read from the file system, or 86 from 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); 92 Return the file size (as read from file system) for the file 93 associated with BFD @var{abfd}. 94 95 The initial motivation for, and use of, this routine is not 96 so we can get the exact size of the object the BFD applies to, since 97 that might not be generally possible (archive members for example). 98 It would be ideal if someone could eventually modify 99 it so that such results were guaranteed. 100 101 Instead, we want to ask questions like "is this NNN byte sized 102 object I'm about to try read from file offset YYY reasonable?" 103 As as example of where we might do this, some object formats 104 use string tables for which the first @code{sizeof (long)} bytes of the 105 table contain the size of the table itself, including the size bytes. 106 If an application tries to read what it thinks is one of these 107 string tables, without some way to validate the size, and for 108 some reason the size is wrong (byte swapping error, wrong location 109 for the string table, etc.), the only clue is likely to be a read 110 error when it tries to read the table, or a "virtual memory 111 exhausted" error when it tries to allocate 15 bazillon bytes 112 of space for the 15 bazillon byte table it is about to read. 113 This function at least allows us to answer the question, "is the 114 size reasonable?". 115 116 A 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); 122 Return the file size (as read from file system) for the file 123 associated with BFD @var{abfd}. It supports both normal files 124 and 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, size_t len, int prot, int flags, file_ptr offset, void **map_addr, size_t *map_len) ATTRIBUTE_WARN_UNUSED_RESULT; 130 Return mmap()ed region of the file, if possible and implemented. 131 LEN and OFFSET do not need to be page aligned. The page aligned 132 address and length are written to MAP_ADDR and MAP_LEN. 133 Returns MAP_FAILED on failure. 134 135 @end deftypefn 136 @findex bfd_get_current_time 137 @subsubsection @code{bfd_get_current_time} 138 @deftypefn {Function} time_t bfd_get_current_time (time_t now); 139 Returns the current time. 140 141 If the environment variable SOURCE_DATE_EPOCH is defined 142 then this is parsed and its value is returned. Otherwise 143 if the paramter NOW is non-zero, then that is returned. 144 Otherwise the result of the system call "time(NULL)" is 145 returned. 146 147 @end deftypefn 148