1 /* Low-level I/O routines for BFDs.
3 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2001, 2002, 2003, 2004, 2005
5 Free Software Foundation, Inc.
7 Written by Cygnus Support.
9 This file is part of BFD, the Binary File Descriptor library.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
33 #define S_IXUSR 0100 /* Execute by owner. */
36 #define S_IXGRP 0010 /* Execute by group. */
39 #define S_IXOTH 0001 /* Execute by others. */
43 real_ftell (FILE *file)
45 #if defined (HAVE_FTELLO64)
46 return ftello64 (file);
47 #elif defined (HAVE_FTELLO)
55 real_fseek (FILE *file, file_ptr offset, int whence)
57 #if defined (HAVE_FSEEKO64)
58 return fseeko64 (file, offset, whence);
59 #elif defined (HAVE_FSEEKO)
60 return fseeko (file, offset, whence);
62 return fseek (file, offset, whence);
67 real_fopen (const char *filename, const char *modes)
69 #if defined (HAVE_FOPEN64)
70 return fopen64 (filename, modes);
72 return fopen (filename, modes);
82 The <<struct bfd_iovec>> contains the internal file I/O class.
83 Each <<BFD>> has an instance of this class and all file I/O is
84 routed through it (it is assumed that the instance implements
85 all methods listed below).
89 . {* To avoid problems with macros, a "b" rather than "f"
90 . prefix is prepended to each method name. *}
91 . {* Attempt to read/write NBYTES on ABFD's IOSTREAM storing/fetching
92 . bytes starting at PTR. Return the number of bytes actually
93 . transfered (a read past end-of-file returns less than NBYTES),
94 . or -1 (setting <<bfd_error>>) if an error occurs. *}
95 . file_ptr (*bread) (struct bfd *abfd, void *ptr, file_ptr nbytes);
96 . file_ptr (*bwrite) (struct bfd *abfd, const void *ptr,
98 . {* Return the current IOSTREAM file offset, or -1 (setting <<bfd_error>>
99 . if an error occurs. *}
100 . file_ptr (*btell) (struct bfd *abfd);
101 . {* For the following, on successful completion a value of 0 is returned.
102 . Otherwise, a value of -1 is returned (and <<bfd_error>> is set). *}
103 . int (*bseek) (struct bfd *abfd, file_ptr offset, int whence);
104 . int (*bclose) (struct bfd *abfd);
105 . int (*bflush) (struct bfd *abfd);
106 . int (*bstat) (struct bfd *abfd, struct stat *sb);
112 /* Return value is amount read. */
115 bfd_bread (void *ptr, bfd_size_type size, bfd *abfd)
119 if ((abfd->flags & BFD_IN_MEMORY) != 0)
121 struct bfd_in_memory *bim;
124 bim = abfd->iostream;
126 if (abfd->where + get > bim->size)
128 if (bim->size < (bfd_size_type) abfd->where)
131 get = bim->size - abfd->where;
132 bfd_set_error (bfd_error_file_truncated);
134 memcpy (ptr, bim->buffer + abfd->where, (size_t) get);
140 nread = abfd->iovec->bread (abfd, ptr, size);
143 if (nread != (size_t) -1)
144 abfd->where += nread;
150 bfd_bwrite (const void *ptr, bfd_size_type size, bfd *abfd)
154 if ((abfd->flags & BFD_IN_MEMORY) != 0)
156 struct bfd_in_memory *bim = abfd->iostream;
158 size = (size_t) size;
159 if (abfd->where + size > bim->size)
161 bfd_size_type newsize, oldsize;
163 oldsize = (bim->size + 127) & ~(bfd_size_type) 127;
164 bim->size = abfd->where + size;
165 /* Round up to cut down on memory fragmentation */
166 newsize = (bim->size + 127) & ~(bfd_size_type) 127;
167 if (newsize > oldsize)
169 bim->buffer = bfd_realloc (bim->buffer, newsize);
170 if (bim->buffer == 0)
177 memcpy (bim->buffer + abfd->where, ptr, (size_t) size);
183 nwrote = abfd->iovec->bwrite (abfd, ptr, size);
187 if (nwrote != (size_t) -1)
188 abfd->where += nwrote;
194 bfd_set_error (bfd_error_system_call);
204 if ((abfd->flags & BFD_IN_MEMORY) != 0)
209 ptr = abfd->iovec->btell (abfd);
211 if (abfd->my_archive)
222 bfd_flush (bfd *abfd)
224 if ((abfd->flags & BFD_IN_MEMORY) != 0)
228 return abfd->iovec->bflush (abfd);
232 /* Returns 0 for success, negative value for failure (in which case
233 bfd_get_error can retrieve the error code). */
235 bfd_stat (bfd *abfd, struct stat *statbuf)
239 if ((abfd->flags & BFD_IN_MEMORY) != 0)
243 result = abfd->iovec->bstat (abfd, statbuf);
248 bfd_set_error (bfd_error_system_call);
252 /* Returns 0 for success, nonzero for failure (in which case bfd_get_error
253 can retrieve the error code). */
256 bfd_seek (bfd *abfd, file_ptr position, int direction)
259 file_ptr file_position;
260 /* For the time being, a BFD may not seek to it's end. The problem
261 is that we don't easily have a way to recognize the end of an
262 element in an archive. */
264 BFD_ASSERT (direction == SEEK_SET || direction == SEEK_CUR);
266 if (direction == SEEK_CUR && position == 0)
269 if ((abfd->flags & BFD_IN_MEMORY) != 0)
271 struct bfd_in_memory *bim;
273 bim = abfd->iostream;
275 if (direction == SEEK_SET)
276 abfd->where = position;
278 abfd->where += position;
280 if (abfd->where > bim->size)
282 if ((abfd->direction == write_direction) ||
283 (abfd->direction == both_direction))
285 bfd_size_type newsize, oldsize;
287 oldsize = (bim->size + 127) & ~(bfd_size_type) 127;
288 bim->size = abfd->where;
289 /* Round up to cut down on memory fragmentation */
290 newsize = (bim->size + 127) & ~(bfd_size_type) 127;
291 if (newsize > oldsize)
293 bim->buffer = bfd_realloc (bim->buffer, newsize);
294 if (bim->buffer == 0)
303 abfd->where = bim->size;
304 bfd_set_error (bfd_error_file_truncated);
311 if (abfd->format != bfd_archive && abfd->my_archive == 0)
313 if (direction == SEEK_SET && (bfd_vma) position == abfd->where)
318 /* We need something smarter to optimize access to archives.
319 Currently, anything inside an archive is read via the file
320 handle for the archive. Which means that a bfd_seek on one
321 component affects the `current position' in the archive, as
322 well as in any other component.
324 It might be sufficient to put a spike through the cache
325 abstraction, and look to the archive for the file position,
326 but I think we should try for something cleaner.
328 In the meantime, no optimization for archives. */
331 file_position = position;
332 if (direction == SEEK_SET && abfd->my_archive != NULL)
333 file_position += abfd->origin;
336 result = abfd->iovec->bseek (abfd, file_position, direction);
342 int hold_errno = errno;
344 /* Force redetermination of `where' field. */
347 /* An EINVAL error probably means that the file offset was
349 if (hold_errno == EINVAL)
350 bfd_set_error (bfd_error_file_truncated);
353 bfd_set_error (bfd_error_system_call);
359 /* Adjust `where' field. */
360 if (direction == SEEK_SET)
361 abfd->where = position;
363 abfd->where += position;
373 long bfd_get_mtime (bfd *abfd);
376 Return the file modification time (as read from the file system, or
377 from the archive header for archive members).
382 bfd_get_mtime (bfd *abfd)
389 if (abfd->iovec == NULL)
392 if (abfd->iovec->bstat (abfd, &buf) != 0)
395 abfd->mtime = buf.st_mtime; /* Save value in case anyone wants it */
404 long bfd_get_size (bfd *abfd);
407 Return the file size (as read from file system) for the file
408 associated with BFD @var{abfd}.
410 The initial motivation for, and use of, this routine is not
411 so we can get the exact size of the object the BFD applies to, since
412 that might not be generally possible (archive members for example).
413 It would be ideal if someone could eventually modify
414 it so that such results were guaranteed.
416 Instead, we want to ask questions like "is this NNN byte sized
417 object I'm about to try read from file offset YYY reasonable?"
418 As as example of where we might do this, some object formats
419 use string tables for which the first <<sizeof (long)>> bytes of the
420 table contain the size of the table itself, including the size bytes.
421 If an application tries to read what it thinks is one of these
422 string tables, without some way to validate the size, and for
423 some reason the size is wrong (byte swapping error, wrong location
424 for the string table, etc.), the only clue is likely to be a read
425 error when it tries to read the table, or a "virtual memory
426 exhausted" error when it tries to allocate 15 bazillon bytes
427 of space for the 15 bazillon byte table it is about to read.
428 This function at least allows us to answer the question, "is the
433 bfd_get_size (bfd *abfd)
437 if ((abfd->flags & BFD_IN_MEMORY) != 0)
438 return ((struct bfd_in_memory *) abfd->iostream)->size;
440 if (abfd->iovec == NULL)
443 if (abfd->iovec->bstat (abfd, &buf) != 0)