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. */
47 real_read (void *where
, size_t a
, size_t b
, FILE *file
)
49 /* FIXME - this looks like an optimization, but it's really to cover
50 up for a feature of some OSs (not solaris - sigh) that
51 ld/pe-dll.c takes advantage of (apparently) when it creates BFDs
52 internally and tries to link against them. BFD seems to be smart
53 enough to realize there are no symbol records in the "file" that
54 doesn't exist but attempts to read them anyway. On Solaris,
55 attempting to read zero bytes from a NULL file results in a core
56 dump, but on other platforms it just returns zero bytes read.
57 This makes it to something reasonable. - DJ */
62 #if defined (__VAX) && defined (VMS)
63 /* Apparently fread on Vax VMS does not keep the record length
65 return read (fileno (file
), where
, a
* b
);
67 return fread (where
, a
, b
, file
);
71 /* Return value is amount read. */
74 bfd_bread (void *ptr
, bfd_size_type size
, bfd
*abfd
)
78 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
80 struct bfd_in_memory
*bim
;
85 if (abfd
->where
+ get
> bim
->size
)
87 if (bim
->size
< (bfd_size_type
) abfd
->where
)
90 get
= bim
->size
- abfd
->where
;
91 bfd_set_error (bfd_error_file_truncated
);
93 memcpy (ptr
, bim
->buffer
+ abfd
->where
, (size_t) get
);
98 nread
= real_read (ptr
, 1, (size_t) size
, bfd_cache_lookup (abfd
));
99 if (nread
!= (size_t) -1)
100 abfd
->where
+= nread
;
102 /* Set bfd_error if we did not read as much data as we expected.
104 If the read failed due to an error set the bfd_error_system_call,
105 else set bfd_error_file_truncated.
107 A BFD backend may wish to override bfd_error_file_truncated to
108 provide something more useful (eg. no_symbols or wrong_format). */
111 if (ferror (bfd_cache_lookup (abfd
)))
112 bfd_set_error (bfd_error_system_call
);
114 bfd_set_error (bfd_error_file_truncated
);
121 bfd_bwrite (const void *ptr
, bfd_size_type size
, bfd
*abfd
)
125 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
127 struct bfd_in_memory
*bim
= abfd
->iostream
;
128 size
= (size_t) size
;
129 if (abfd
->where
+ size
> bim
->size
)
131 bfd_size_type newsize
, oldsize
;
133 oldsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
134 bim
->size
= abfd
->where
+ size
;
135 /* Round up to cut down on memory fragmentation */
136 newsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
137 if (newsize
> oldsize
)
139 bim
->buffer
= bfd_realloc (bim
->buffer
, newsize
);
140 if (bim
->buffer
== 0)
147 memcpy (bim
->buffer
+ abfd
->where
, ptr
, (size_t) size
);
152 nwrote
= fwrite (ptr
, 1, (size_t) size
, bfd_cache_lookup (abfd
));
153 if (nwrote
!= (size_t) -1)
154 abfd
->where
+= nwrote
;
160 bfd_set_error (bfd_error_system_call
);
170 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
173 ptr
= ftell (bfd_cache_lookup (abfd
));
175 if (abfd
->my_archive
)
182 bfd_flush (bfd
*abfd
)
184 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
186 return fflush (bfd_cache_lookup(abfd
));
189 /* Returns 0 for success, negative value for failure (in which case
190 bfd_get_error can retrieve the error code). */
192 bfd_stat (bfd
*abfd
, struct stat
*statbuf
)
197 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
200 f
= bfd_cache_lookup (abfd
);
203 bfd_set_error (bfd_error_system_call
);
206 result
= fstat (fileno (f
), statbuf
);
208 bfd_set_error (bfd_error_system_call
);
212 /* Returns 0 for success, nonzero for failure (in which case bfd_get_error
213 can retrieve the error code). */
216 bfd_seek (bfd
*abfd
, file_ptr position
, int direction
)
221 /* For the time being, a BFD may not seek to it's end. The problem
222 is that we don't easily have a way to recognize the end of an
223 element in an archive. */
225 BFD_ASSERT (direction
== SEEK_SET
|| direction
== SEEK_CUR
);
227 if (direction
== SEEK_CUR
&& position
== 0)
230 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
232 struct bfd_in_memory
*bim
;
234 bim
= abfd
->iostream
;
236 if (direction
== SEEK_SET
)
237 abfd
->where
= position
;
239 abfd
->where
+= position
;
241 if (abfd
->where
> bim
->size
)
243 if ((abfd
->direction
== write_direction
) ||
244 (abfd
->direction
== both_direction
))
246 bfd_size_type newsize
, oldsize
;
247 oldsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
248 bim
->size
= abfd
->where
;
249 /* Round up to cut down on memory fragmentation */
250 newsize
= (bim
->size
+ 127) & ~(bfd_size_type
) 127;
251 if (newsize
> oldsize
)
253 bim
->buffer
= bfd_realloc (bim
->buffer
, newsize
);
254 if (bim
->buffer
== 0)
263 abfd
->where
= bim
->size
;
264 bfd_set_error (bfd_error_file_truncated
);
271 if (abfd
->format
!= bfd_archive
&& abfd
->my_archive
== 0)
274 /* Explanation for this code: I'm only about 95+% sure that the above
275 conditions are sufficient and that all i/o calls are properly
276 adjusting the `where' field. So this is sort of an `assert'
277 that the `where' field is correct. If we can go a while without
278 tripping the abort, we can probably safely disable this code,
279 so that the real optimizations happen. */
280 file_ptr where_am_i_now
;
281 where_am_i_now
= ftell (bfd_cache_lookup (abfd
));
282 if (abfd
->my_archive
)
283 where_am_i_now
-= abfd
->origin
;
284 if (where_am_i_now
!= abfd
->where
)
287 if (direction
== SEEK_SET
&& (bfd_vma
) position
== abfd
->where
)
292 /* We need something smarter to optimize access to archives.
293 Currently, anything inside an archive is read via the file
294 handle for the archive. Which means that a bfd_seek on one
295 component affects the `current position' in the archive, as
296 well as in any other component.
298 It might be sufficient to put a spike through the cache
299 abstraction, and look to the archive for the file position,
300 but I think we should try for something cleaner.
302 In the meantime, no optimization for archives. */
305 f
= bfd_cache_lookup (abfd
);
306 file_position
= position
;
307 if (direction
== SEEK_SET
&& abfd
->my_archive
!= NULL
)
308 file_position
+= abfd
->origin
;
310 result
= fseek (f
, file_position
, direction
);
313 int hold_errno
= errno
;
315 /* Force redetermination of `where' field. */
318 /* An EINVAL error probably means that the file offset was
320 if (hold_errno
== EINVAL
)
321 bfd_set_error (bfd_error_file_truncated
);
324 bfd_set_error (bfd_error_system_call
);
330 /* Adjust `where' field. */
331 if (direction
== SEEK_SET
)
332 abfd
->where
= position
;
334 abfd
->where
+= position
;
344 long bfd_get_mtime (bfd *abfd);
347 Return the file modification time (as read from the file system, or
348 from the archive header for archive members).
353 bfd_get_mtime (bfd
*abfd
)
361 fp
= bfd_cache_lookup (abfd
);
362 if (0 != fstat (fileno (fp
), &buf
))
365 abfd
->mtime
= buf
.st_mtime
; /* Save value in case anyone wants it */
374 long bfd_get_size (bfd *abfd);
377 Return the file size (as read from file system) for the file
378 associated with BFD @var{abfd}.
380 The initial motivation for, and use of, this routine is not
381 so we can get the exact size of the object the BFD applies to, since
382 that might not be generally possible (archive members for example).
383 It would be ideal if someone could eventually modify
384 it so that such results were guaranteed.
386 Instead, we want to ask questions like "is this NNN byte sized
387 object I'm about to try read from file offset YYY reasonable?"
388 As as example of where we might do this, some object formats
389 use string tables for which the first <<sizeof (long)>> bytes of the
390 table contain the size of the table itself, including the size bytes.
391 If an application tries to read what it thinks is one of these
392 string tables, without some way to validate the size, and for
393 some reason the size is wrong (byte swapping error, wrong location
394 for the string table, etc.), the only clue is likely to be a read
395 error when it tries to read the table, or a "virtual memory
396 exhausted" error when it tries to allocate 15 bazillon bytes
397 of space for the 15 bazillon byte table it is about to read.
398 This function at least allows us to answer the quesion, "is the
403 bfd_get_size (bfd
*abfd
)
408 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
409 return ((struct bfd_in_memory
*) abfd
->iostream
)->size
;
411 fp
= bfd_cache_lookup (abfd
);
412 if (0 != fstat (fileno (fp
), & buf
))
This page took 0.036766 seconds and 4 git commands to generate.