1 /* BFD library -- caching of file descriptors.
3 Copyright 1990, 1991, 1992, 1993, 1994, 1996, 2000, 2001, 2002,
4 2003, 2004 Free Software Foundation, Inc.
6 Hacked by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
8 This file is part of BFD, the Binary File Descriptor library.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
28 The file caching mechanism is embedded within BFD and allows
29 the application to open as many BFDs as it wants without
30 regard to the underlying operating system's file descriptor
31 limit (often as low as 20 open files). The module in
32 <<cache.c>> maintains a least recently used list of
33 <<BFD_CACHE_MAX_OPEN>> files, and exports the name
34 <<bfd_cache_lookup>>, which runs around and makes sure that
35 the required BFD is open. If not, then it chooses a file to
36 close, closes it and opens the one wanted, returning its file
46 #include "libiberty.h"
48 static bfd_boolean
bfd_cache_delete (bfd
*);
52 cache_btell (struct bfd
*abfd
)
54 FILE *f
= bfd_cache_lookup (abfd
);
57 return real_ftell (f
);
61 cache_bseek (struct bfd
*abfd
, file_ptr offset
, int whence
)
63 FILE *f
= bfd_cache_lookup (abfd
);
66 return real_fseek (f
, offset
, whence
);
69 /* Note that archive entries don't have streams; they share their parent's.
70 This allows someone to play with the iostream behind BFD's back.
72 Also, note that the origin pointer points to the beginning of a file's
73 contents (0 for non-archive elements). For archive entries this is the
74 first octet in the file, NOT the beginning of the archive header. */
77 cache_bread (struct bfd
*abfd
, void *buf
, file_ptr nbytes
)
81 /* FIXME - this looks like an optimization, but it's really to cover
82 up for a feature of some OSs (not solaris - sigh) that
83 ld/pe-dll.c takes advantage of (apparently) when it creates BFDs
84 internally and tries to link against them. BFD seems to be smart
85 enough to realize there are no symbol records in the "file" that
86 doesn't exist but attempts to read them anyway. On Solaris,
87 attempting to read zero bytes from a NULL file results in a core
88 dump, but on other platforms it just returns zero bytes read.
89 This makes it to something reasonable. - DJ */
93 f
= bfd_cache_lookup (abfd
);
97 #if defined (__VAX) && defined (VMS)
98 /* Apparently fread on Vax VMS does not keep the record length
100 nread
= read (fileno (f
), buf
, nbytes
);
101 /* Set bfd_error if we did not read as much data as we expected. If
102 the read failed due to an error set the bfd_error_system_call,
103 else set bfd_error_file_truncated. */
104 if (nread
== (file_ptr
)-1)
106 bfd_set_error (bfd_error_system_call
);
110 nread
= fread (buf
, 1, nbytes
, f
);
111 /* Set bfd_error if we did not read as much data as we expected. If
112 the read failed due to an error set the bfd_error_system_call,
113 else set bfd_error_file_truncated. */
114 if (nread
< nbytes
&& ferror (f
))
116 bfd_set_error (bfd_error_system_call
);
124 cache_bwrite (struct bfd
*abfd
, const void *where
, file_ptr nbytes
)
127 FILE *f
= bfd_cache_lookup (abfd
);
130 nwrite
= fwrite (where
, 1, nbytes
, f
);
131 if (nwrite
< nbytes
&& ferror (f
))
133 bfd_set_error (bfd_error_system_call
);
140 cache_bclose (struct bfd
*abfd
)
142 return bfd_cache_close (abfd
);
146 cache_bflush (struct bfd
*abfd
)
149 FILE *f
= bfd_cache_lookup (abfd
);
154 bfd_set_error (bfd_error_system_call
);
159 cache_bstat (struct bfd
*abfd
, struct stat
*sb
)
162 FILE *f
= bfd_cache_lookup (abfd
);
165 sts
= fstat (fileno (f
), sb
);
167 bfd_set_error (bfd_error_system_call
);
171 static const struct bfd_iovec cache_iovec
= {
172 &cache_bread
, &cache_bwrite
, &cache_btell
, &cache_bseek
,
173 &cache_bclose
, &cache_bflush
, &cache_bstat
178 BFD_CACHE_MAX_OPEN macro
181 The maximum number of files which the cache will keep open at
184 .#define BFD_CACHE_MAX_OPEN 10
188 /* The number of BFD files we have open. */
190 static int open_files
;
197 extern bfd *bfd_last_cache;
200 Zero, or a pointer to the topmost BFD on the chain. This is
201 used by the <<bfd_cache_lookup>> macro in @file{libbfd.h} to
202 determine when it can avoid a function call.
205 bfd
*bfd_last_cache
= NULL
;
212 Check to see if the required BFD is the same as the last one
213 looked up. If so, then it can use the stream in the BFD with
214 impunity, since it can't have changed since the last lookup;
215 otherwise, it has to perform the complicated lookup function.
217 .#define bfd_cache_lookup(x) \
218 . ((x) == bfd_last_cache ? \
219 . (FILE *) (bfd_last_cache->iostream): \
220 . bfd_cache_lookup_worker (x))
224 /* Insert a BFD into the cache. */
229 if (bfd_last_cache
== NULL
)
231 abfd
->lru_next
= abfd
;
232 abfd
->lru_prev
= abfd
;
236 abfd
->lru_next
= bfd_last_cache
;
237 abfd
->lru_prev
= bfd_last_cache
->lru_prev
;
238 abfd
->lru_prev
->lru_next
= abfd
;
239 abfd
->lru_next
->lru_prev
= abfd
;
241 bfd_last_cache
= abfd
;
244 /* Remove a BFD from the cache. */
249 abfd
->lru_prev
->lru_next
= abfd
->lru_next
;
250 abfd
->lru_next
->lru_prev
= abfd
->lru_prev
;
251 if (abfd
== bfd_last_cache
)
253 bfd_last_cache
= abfd
->lru_next
;
254 if (abfd
== bfd_last_cache
)
255 bfd_last_cache
= NULL
;
259 /* We need to open a new file, and the cache is full. Find the least
260 recently used cacheable BFD and close it. */
267 if (bfd_last_cache
== NULL
)
271 for (kill
= bfd_last_cache
->lru_prev
;
273 kill
= kill
->lru_prev
)
275 if (kill
== bfd_last_cache
)
285 /* There are no open cacheable BFD's. */
289 kill
->where
= real_ftell ((FILE *) kill
->iostream
);
291 return bfd_cache_delete (kill
);
294 /* Close a BFD and remove it from the cache. */
297 bfd_cache_delete (bfd
*abfd
)
301 if (fclose ((FILE *) abfd
->iostream
) == 0)
306 bfd_set_error (bfd_error_system_call
);
311 abfd
->iostream
= NULL
;
322 bfd_boolean bfd_cache_init (bfd *abfd);
325 Add a newly opened BFD to the cache.
329 bfd_cache_init (bfd
*abfd
)
331 BFD_ASSERT (abfd
->iostream
!= NULL
);
332 if (open_files
>= BFD_CACHE_MAX_OPEN
)
337 abfd
->iovec
= &cache_iovec
;
348 bfd_boolean bfd_cache_close (bfd *abfd);
351 Remove the BFD @var{abfd} from the cache. If the attached file is open,
355 <<FALSE>> is returned if closing the file fails, <<TRUE>> is
356 returned if all is well.
360 bfd_cache_close (bfd
*abfd
)
362 if (abfd
->iovec
!= &cache_iovec
)
365 if (abfd
->iostream
== NULL
)
366 /* Previously closed. */
369 return bfd_cache_delete (abfd
);
377 bfd_boolean bfd_cache_close_all (void);
380 Remove all BFDs from the cache. If the attached file is open,
384 <<FALSE>> is returned if closing one of the file fails, <<TRUE>> is
385 returned if all is well.
389 bfd_cache_close_all ()
391 bfd_boolean ret
= TRUE
;
393 while (bfd_last_cache
!= NULL
)
394 ret
&= bfd_cache_close (bfd_last_cache
);
404 FILE* bfd_open_file (bfd *abfd);
407 Call the OS to open a file for @var{abfd}. Return the <<FILE *>>
408 (possibly <<NULL>>) that results from this operation. Set up the
409 BFD so that future accesses know the file is open. If the <<FILE *>>
410 returned is <<NULL>>, then it won't have been put in the
411 cache, so it won't have to be removed from it.
415 bfd_open_file (bfd
*abfd
)
417 abfd
->cacheable
= TRUE
; /* Allow it to be closed later. */
419 if (open_files
>= BFD_CACHE_MAX_OPEN
)
425 switch (abfd
->direction
)
429 abfd
->iostream
= (PTR
) fopen (abfd
->filename
, FOPEN_RB
);
432 case write_direction
:
433 if (abfd
->opened_once
)
435 abfd
->iostream
= (PTR
) fopen (abfd
->filename
, FOPEN_RUB
);
436 if (abfd
->iostream
== NULL
)
437 abfd
->iostream
= (PTR
) fopen (abfd
->filename
, FOPEN_WUB
);
443 Some operating systems won't let us overwrite a running
444 binary. For them, we want to unlink the file first.
446 However, gcc 2.95 will create temporary files using
447 O_EXCL and tight permissions to prevent other users from
448 substituting other .o files during the compilation. gcc
449 will then tell the assembler to use the newly created
450 file as an output file. If we unlink the file here, we
451 open a brief window when another user could still
454 So we unlink the output file if and only if it has
457 /* Don't do this for MSDOS: it doesn't care about overwriting
458 a running binary, but if this file is already open by
459 another BFD, we will be in deep trouble if we delete an
460 open file. In fact, objdump does just that if invoked with
461 the --info option. */
464 if (stat (abfd
->filename
, &s
) == 0 && s
.st_size
!= 0)
465 unlink_if_ordinary (abfd
->filename
);
467 abfd
->iostream
= (PTR
) fopen (abfd
->filename
, FOPEN_WUB
);
468 abfd
->opened_once
= TRUE
;
473 if (abfd
->iostream
== NULL
)
474 bfd_set_error (bfd_error_system_call
);
477 if (! bfd_cache_init (abfd
))
481 return (FILE *) abfd
->iostream
;
486 bfd_cache_lookup_worker
489 FILE *bfd_cache_lookup_worker (bfd *abfd);
492 Called when the macro <<bfd_cache_lookup>> fails to find a
493 quick answer. Find a file descriptor for @var{abfd}. If
494 necessary, it open it. If there are already more than
495 <<BFD_CACHE_MAX_OPEN>> files open, it tries to close one first, to
496 avoid running out of file descriptors. It will return NULL
497 if it is unable to (re)open the @var{abfd}.
501 bfd_cache_lookup_worker (bfd
*abfd
)
503 bfd
*orig_bfd
= abfd
;
504 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
507 if (abfd
->my_archive
)
508 abfd
= abfd
->my_archive
;
510 if (abfd
->iostream
!= NULL
)
512 /* Move the file to the start of the cache. */
513 if (abfd
!= bfd_last_cache
)
518 return (FILE *) abfd
->iostream
;
521 if (bfd_open_file (abfd
) == NULL
)
523 else if (real_fseek ((FILE *) abfd
->iostream
, abfd
->where
, SEEK_SET
) != 0)
524 bfd_set_error (bfd_error_system_call
);
526 return (FILE *) abfd
->iostream
;
528 (*_bfd_error_handler
) (_("reopening %B: %s\n"),
529 orig_bfd
, bfd_errmsg (bfd_get_error ()));
This page took 0.041061 seconds and 4 git commands to generate.