1 /* Low-level I/O routines for BFDs.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002 Free Software Foundation, Inc.
4 Written by Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
30 #define S_IXUSR 0100 /* Execute by owner. */
33 #define S_IXGRP 0010 /* Execute by group. */
36 #define S_IXOTH 0001 /* Execute by others. */
39 /* Note that archive entries don't have streams; they share their parent's.
40 This allows someone to play with the iostream behind BFD's back.
42 Also, note that the origin pointer points to the beginning of a file's
43 contents (0 for non-archive elements). For archive entries this is the
44 first octet in the file, NOT the beginning of the archive header. */
46 static size_t real_read
PARAMS ((PTR where
, size_t a
, size_t b
, FILE *file
));
48 real_read (where
, a
, b
, file
)
54 /* FIXME - this looks like an optimization, but it's really to cover
55 up for a feature of some OSs (not solaris - sigh) that
56 ld/pe-dll.c takes advantage of (apparently) when it creates BFDs
57 internally and tries to link against them. BFD seems to be smart
58 enough to realize there are no symbol records in the "file" that
59 doesn't exist but attempts to read them anyway. On Solaris,
60 attempting to read zero bytes from a NULL file results in a core
61 dump, but on other platforms it just returns zero bytes read.
62 This makes it to something reasonable. - DJ */
67 #if defined (__VAX) && defined (VMS)
68 /* Apparently fread on Vax VMS does not keep the record length
70 return read (fileno (file
), where
, a
* b
);
72 return fread (where
, a
, b
, file
);
76 /* Return value is amount read. */
79 bfd_bread (ptr
, size
, abfd
)
86 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
88 struct bfd_in_memory
*bim
;
91 bim
= (struct bfd_in_memory
*) abfd
->iostream
;
93 if (abfd
->where
+ get
> bim
->size
)
95 if (bim
->size
< (bfd_size_type
) abfd
->where
)
98 get
= bim
->size
- abfd
->where
;
99 bfd_set_error (bfd_error_file_truncated
);
101 memcpy (ptr
, bim
->buffer
+ abfd
->where
, (size_t) get
);
106 nread
= real_read (ptr
, 1, (size_t) size
, bfd_cache_lookup (abfd
));
107 if (nread
!= (size_t) -1)
108 abfd
->where
+= nread
;
110 /* Set bfd_error if we did not read as much data as we expected.
112 If the read failed due to an error set the bfd_error_system_call,
113 else set bfd_error_file_truncated.
115 A BFD backend may wish to override bfd_error_file_truncated to
116 provide something more useful (eg. no_symbols or wrong_format). */
119 if (ferror (bfd_cache_lookup (abfd
)))
120 bfd_set_error (bfd_error_system_call
);
122 bfd_set_error (bfd_error_file_truncated
);
129 bfd_bwrite (ptr
, size
, abfd
)
136 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
138 struct bfd_in_memory
*bim
= (struct bfd_in_memory
*) (abfd
->iostream
);
139 size
= (size_t) size
;
140 if (abfd
->where
+ size
> bim
->size
)
142 bfd_size_type newsize
, oldsize
;
144 oldsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
145 bim
->size
= abfd
->where
+ size
;
146 /* Round up to cut down on memory fragmentation */
147 newsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
148 if (newsize
> oldsize
)
150 bim
->buffer
= (bfd_byte
*) bfd_realloc (bim
->buffer
, newsize
);
151 if (bim
->buffer
== 0)
158 memcpy (bim
->buffer
+ abfd
->where
, ptr
, (size_t) size
);
163 nwrote
= fwrite (ptr
, 1, (size_t) size
, bfd_cache_lookup (abfd
));
164 if (nwrote
!= (size_t) -1)
165 abfd
->where
+= nwrote
;
171 bfd_set_error (bfd_error_system_call
);
182 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
185 ptr
= ftell (bfd_cache_lookup (abfd
));
187 if (abfd
->my_archive
)
197 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
199 return fflush (bfd_cache_lookup(abfd
));
202 /* Returns 0 for success, negative value for failure (in which case
203 bfd_get_error can retrieve the error code). */
205 bfd_stat (abfd
, statbuf
)
207 struct stat
*statbuf
;
212 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
215 f
= bfd_cache_lookup (abfd
);
218 bfd_set_error (bfd_error_system_call
);
221 result
= fstat (fileno (f
), statbuf
);
223 bfd_set_error (bfd_error_system_call
);
227 /* Returns 0 for success, nonzero for failure (in which case bfd_get_error
228 can retrieve the error code). */
231 bfd_seek (abfd
, position
, direction
)
239 /* For the time being, a BFD may not seek to it's end. The problem
240 is that we don't easily have a way to recognize the end of an
241 element in an archive. */
243 BFD_ASSERT (direction
== SEEK_SET
|| direction
== SEEK_CUR
);
245 if (direction
== SEEK_CUR
&& position
== 0)
248 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
250 struct bfd_in_memory
*bim
;
252 bim
= (struct bfd_in_memory
*) abfd
->iostream
;
254 if (direction
== SEEK_SET
)
255 abfd
->where
= position
;
257 abfd
->where
+= position
;
259 if (abfd
->where
> bim
->size
)
261 if ((abfd
->direction
== write_direction
) ||
262 (abfd
->direction
== both_direction
))
264 bfd_size_type newsize
, oldsize
;
265 oldsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
266 bim
->size
= abfd
->where
;
267 /* Round up to cut down on memory fragmentation */
268 newsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
269 if (newsize
> oldsize
)
271 bim
->buffer
= (bfd_byte
*) bfd_realloc (bim
->buffer
, newsize
);
272 if (bim
->buffer
== 0)
281 abfd
->where
= bim
->size
;
282 bfd_set_error (bfd_error_file_truncated
);
289 if (abfd
->format
!= bfd_archive
&& abfd
->my_archive
== 0)
292 /* Explanation for this code: I'm only about 95+% sure that the above
293 conditions are sufficient and that all i/o calls are properly
294 adjusting the `where' field. So this is sort of an `assert'
295 that the `where' field is correct. If we can go a while without
296 tripping the abort, we can probably safely disable this code,
297 so that the real optimizations happen. */
298 file_ptr where_am_i_now
;
299 where_am_i_now
= ftell (bfd_cache_lookup (abfd
));
300 if (abfd
->my_archive
)
301 where_am_i_now
-= abfd
->origin
;
302 if (where_am_i_now
!= abfd
->where
)
305 if (direction
== SEEK_SET
&& (bfd_vma
) position
== abfd
->where
)
310 /* We need something smarter to optimize access to archives.
311 Currently, anything inside an archive is read via the file
312 handle for the archive. Which means that a bfd_seek on one
313 component affects the `current position' in the archive, as
314 well as in any other component.
316 It might be sufficient to put a spike through the cache
317 abstraction, and look to the archive for the file position,
318 but I think we should try for something cleaner.
320 In the meantime, no optimization for archives. */
323 f
= bfd_cache_lookup (abfd
);
324 file_position
= position
;
325 if (direction
== SEEK_SET
&& abfd
->my_archive
!= NULL
)
326 file_position
+= abfd
->origin
;
328 result
= fseek (f
, file_position
, direction
);
331 int hold_errno
= errno
;
333 /* Force redetermination of `where' field. */
336 /* An EINVAL error probably means that the file offset was
338 if (hold_errno
== EINVAL
)
339 bfd_set_error (bfd_error_file_truncated
);
342 bfd_set_error (bfd_error_system_call
);
348 /* Adjust `where' field. */
349 if (direction
== SEEK_SET
)
350 abfd
->where
= position
;
352 abfd
->where
+= position
;
362 long bfd_get_mtime(bfd *abfd);
365 Return the file modification time (as read from the file system, or
366 from the archive header for archive members).
380 fp
= bfd_cache_lookup (abfd
);
381 if (0 != fstat (fileno (fp
), &buf
))
384 abfd
->mtime
= buf
.st_mtime
; /* Save value in case anyone wants it */
393 long 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 quesion, "is the
428 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
429 return ((struct bfd_in_memory
*) abfd
->iostream
)->size
;
431 fp
= bfd_cache_lookup (abfd
);
432 if (0 != fstat (fileno (fp
), & buf
))