PR 6753
[binutils.git] / bfd / cache.c
blob3906335041d0cef17f31ef53357ae13b39eddda5
1 /* BFD library -- caching of file descriptors.
3 Copyright 1990, 1991, 1992, 1993, 1994, 1996, 2000, 2001, 2002,
4 2003, 2004, 2005, 2007 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 3 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,
23 MA 02110-1301, USA. */
26 SECTION
27 File caching
29 The file caching mechanism is embedded within BFD and allows
30 the application to open as many BFDs as it wants without
31 regard to the underlying operating system's file descriptor
32 limit (often as low as 20 open files). The module in
33 <<cache.c>> maintains a least recently used list of
34 <<BFD_CACHE_MAX_OPEN>> files, and exports the name
35 <<bfd_cache_lookup>>, which runs around and makes sure that
36 the required BFD is open. If not, then it chooses a file to
37 close, closes it and opens the one wanted, returning its file
38 handle.
40 SUBSECTION
41 Caching functions
44 #include "sysdep.h"
45 #include "bfd.h"
46 #include "libbfd.h"
47 #include "libiberty.h"
49 /* In some cases we can optimize cache operation when reopening files.
50 For instance, a flush is entirely unnecessary if the file is already
51 closed, so a flush would use CACHE_NO_OPEN. Similarly, a seek using
52 SEEK_SET or SEEK_END need not first seek to the current position.
53 For stat we ignore seek errors, just in case the file has changed
54 while we weren't looking. If it has, then it's possible that the
55 file is shorter and we don't want a seek error to prevent us doing
56 the stat. */
57 enum cache_flag {
58 CACHE_NORMAL = 0,
59 CACHE_NO_OPEN = 1,
60 CACHE_NO_SEEK = 2,
61 CACHE_NO_SEEK_ERROR = 4
64 /* The maximum number of files which the cache will keep open at
65 one time. */
67 #define BFD_CACHE_MAX_OPEN 10
69 /* The number of BFD files we have open. */
71 static int open_files;
73 /* Zero, or a pointer to the topmost BFD on the chain. This is
74 used by the <<bfd_cache_lookup>> macro in @file{libbfd.h} to
75 determine when it can avoid a function call. */
77 static bfd *bfd_last_cache = NULL;
79 /* Insert a BFD into the cache. */
81 static void
82 insert (bfd *abfd)
84 if (bfd_last_cache == NULL)
86 abfd->lru_next = abfd;
87 abfd->lru_prev = abfd;
89 else
91 abfd->lru_next = bfd_last_cache;
92 abfd->lru_prev = bfd_last_cache->lru_prev;
93 abfd->lru_prev->lru_next = abfd;
94 abfd->lru_next->lru_prev = abfd;
96 bfd_last_cache = abfd;
99 /* Remove a BFD from the cache. */
101 static void
102 snip (bfd *abfd)
104 abfd->lru_prev->lru_next = abfd->lru_next;
105 abfd->lru_next->lru_prev = abfd->lru_prev;
106 if (abfd == bfd_last_cache)
108 bfd_last_cache = abfd->lru_next;
109 if (abfd == bfd_last_cache)
110 bfd_last_cache = NULL;
114 /* Close a BFD and remove it from the cache. */
116 static bfd_boolean
117 bfd_cache_delete (bfd *abfd)
119 bfd_boolean ret;
121 if (fclose ((FILE *) abfd->iostream) == 0)
122 ret = TRUE;
123 else
125 ret = FALSE;
126 bfd_set_error (bfd_error_system_call);
129 snip (abfd);
131 abfd->iostream = NULL;
132 --open_files;
134 return ret;
137 /* We need to open a new file, and the cache is full. Find the least
138 recently used cacheable BFD and close it. */
140 static bfd_boolean
141 close_one (void)
143 register bfd *kill;
145 if (bfd_last_cache == NULL)
146 kill = NULL;
147 else
149 for (kill = bfd_last_cache->lru_prev;
150 ! kill->cacheable;
151 kill = kill->lru_prev)
153 if (kill == bfd_last_cache)
155 kill = NULL;
156 break;
161 if (kill == NULL)
163 /* There are no open cacheable BFD's. */
164 return TRUE;
167 kill->where = real_ftell ((FILE *) kill->iostream);
169 return bfd_cache_delete (kill);
172 /* Check to see if the required BFD is the same as the last one
173 looked up. If so, then it can use the stream in the BFD with
174 impunity, since it can't have changed since the last lookup;
175 otherwise, it has to perform the complicated lookup function. */
177 #define bfd_cache_lookup(x, flag) \
178 ((x) == bfd_last_cache \
179 ? (FILE *) (bfd_last_cache->iostream) \
180 : bfd_cache_lookup_worker (x, flag))
182 /* Called when the macro <<bfd_cache_lookup>> fails to find a
183 quick answer. Find a file descriptor for @var{abfd}. If
184 necessary, it open it. If there are already more than
185 <<BFD_CACHE_MAX_OPEN>> files open, it tries to close one first, to
186 avoid running out of file descriptors. It will return NULL
187 if it is unable to (re)open the @var{abfd}. */
189 static FILE *
190 bfd_cache_lookup_worker (bfd *abfd, enum cache_flag flag)
192 bfd *orig_bfd = abfd;
193 if ((abfd->flags & BFD_IN_MEMORY) != 0)
194 abort ();
196 if (abfd->my_archive)
197 abfd = abfd->my_archive;
199 if (abfd->iostream != NULL)
201 /* Move the file to the start of the cache. */
202 if (abfd != bfd_last_cache)
204 snip (abfd);
205 insert (abfd);
207 return (FILE *) abfd->iostream;
210 if (flag & CACHE_NO_OPEN)
211 return NULL;
213 if (bfd_open_file (abfd) == NULL)
215 else if (!(flag & CACHE_NO_SEEK)
216 && real_fseek ((FILE *) abfd->iostream, abfd->where, SEEK_SET) != 0
217 && !(flag & CACHE_NO_SEEK_ERROR))
218 bfd_set_error (bfd_error_system_call);
219 else
220 return (FILE *) abfd->iostream;
222 (*_bfd_error_handler) (_("reopening %B: %s\n"),
223 orig_bfd, bfd_errmsg (bfd_get_error ()));
224 return NULL;
227 static file_ptr
228 cache_btell (struct bfd *abfd)
230 FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
231 if (f == NULL)
232 return abfd->where;
233 return real_ftell (f);
236 static int
237 cache_bseek (struct bfd *abfd, file_ptr offset, int whence)
239 FILE *f = bfd_cache_lookup (abfd, whence != SEEK_CUR ? CACHE_NO_SEEK : 0);
240 if (f == NULL)
241 return -1;
242 return real_fseek (f, offset, whence);
245 /* Note that archive entries don't have streams; they share their parent's.
246 This allows someone to play with the iostream behind BFD's back.
248 Also, note that the origin pointer points to the beginning of a file's
249 contents (0 for non-archive elements). For archive entries this is the
250 first octet in the file, NOT the beginning of the archive header. */
252 static file_ptr
253 cache_bread_1 (struct bfd *abfd, void *buf, file_ptr nbytes)
255 FILE *f;
256 file_ptr nread;
257 /* FIXME - this looks like an optimization, but it's really to cover
258 up for a feature of some OSs (not solaris - sigh) that
259 ld/pe-dll.c takes advantage of (apparently) when it creates BFDs
260 internally and tries to link against them. BFD seems to be smart
261 enough to realize there are no symbol records in the "file" that
262 doesn't exist but attempts to read them anyway. On Solaris,
263 attempting to read zero bytes from a NULL file results in a core
264 dump, but on other platforms it just returns zero bytes read.
265 This makes it to something reasonable. - DJ */
266 if (nbytes == 0)
267 return 0;
269 f = bfd_cache_lookup (abfd, 0);
270 if (f == NULL)
271 return 0;
273 #if defined (__VAX) && defined (VMS)
274 /* Apparently fread on Vax VMS does not keep the record length
275 information. */
276 nread = read (fileno (f), buf, nbytes);
277 /* Set bfd_error if we did not read as much data as we expected. If
278 the read failed due to an error set the bfd_error_system_call,
279 else set bfd_error_file_truncated. */
280 if (nread == (file_ptr)-1)
282 bfd_set_error (bfd_error_system_call);
283 return -1;
285 #else
286 nread = fread (buf, 1, nbytes, f);
287 /* Set bfd_error if we did not read as much data as we expected. If
288 the read failed due to an error set the bfd_error_system_call,
289 else set bfd_error_file_truncated. */
290 if (nread < nbytes && ferror (f))
292 bfd_set_error (bfd_error_system_call);
293 return -1;
295 #endif
296 if (nread < nbytes)
297 /* This may or may not be an error, but in case the calling code
298 bails out because of it, set the right error code. */
299 bfd_set_error (bfd_error_file_truncated);
300 return nread;
303 static file_ptr
304 cache_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
306 file_ptr nread = 0;
308 /* Some filesystems are unable to handle reads that are too large
309 (for instance, NetApp shares with oplocks turned off). To avoid
310 hitting this limitation, we read the buffer in chunks of 8MB max. */
311 while (nread < nbytes)
313 const file_ptr max_chunk_size = 0x800000;
314 file_ptr chunk_size = nbytes - nread;
315 file_ptr chunk_nread;
317 if (chunk_size > max_chunk_size)
318 chunk_size = max_chunk_size;
320 chunk_nread = cache_bread_1 (abfd, buf + nread, chunk_size);
322 /* Update the nread count.
324 We just have to be careful of the case when cache_bread_1 returns
325 a negative count: If this is our first read, then set nread to
326 that negative count in order to return that negative value to the
327 caller. Otherwise, don't add it to our total count, or we would
328 end up returning a smaller number of bytes read than we actually
329 did. */
330 if (nread == 0 || chunk_nread > 0)
331 nread += chunk_nread;
333 if (chunk_nread < chunk_size)
334 break;
337 return nread;
340 static file_ptr
341 cache_bwrite (struct bfd *abfd, const void *where, file_ptr nbytes)
343 file_ptr nwrite;
344 FILE *f = bfd_cache_lookup (abfd, 0);
345 if (f == NULL)
346 return 0;
347 nwrite = fwrite (where, 1, nbytes, f);
348 if (nwrite < nbytes && ferror (f))
350 bfd_set_error (bfd_error_system_call);
351 return -1;
353 return nwrite;
356 static int
357 cache_bclose (struct bfd *abfd)
359 return bfd_cache_close (abfd);
362 static int
363 cache_bflush (struct bfd *abfd)
365 int sts;
366 FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
367 if (f == NULL)
368 return 0;
369 sts = fflush (f);
370 if (sts < 0)
371 bfd_set_error (bfd_error_system_call);
372 return sts;
375 static int
376 cache_bstat (struct bfd *abfd, struct stat *sb)
378 int sts;
379 FILE *f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR);
380 if (f == NULL)
381 return -1;
382 sts = fstat (fileno (f), sb);
383 if (sts < 0)
384 bfd_set_error (bfd_error_system_call);
385 return sts;
388 static const struct bfd_iovec cache_iovec = {
389 &cache_bread, &cache_bwrite, &cache_btell, &cache_bseek,
390 &cache_bclose, &cache_bflush, &cache_bstat
394 INTERNAL_FUNCTION
395 bfd_cache_init
397 SYNOPSIS
398 bfd_boolean bfd_cache_init (bfd *abfd);
400 DESCRIPTION
401 Add a newly opened BFD to the cache.
404 bfd_boolean
405 bfd_cache_init (bfd *abfd)
407 BFD_ASSERT (abfd->iostream != NULL);
408 if (open_files >= BFD_CACHE_MAX_OPEN)
410 if (! close_one ())
411 return FALSE;
413 abfd->iovec = &cache_iovec;
414 insert (abfd);
415 ++open_files;
416 return TRUE;
420 INTERNAL_FUNCTION
421 bfd_cache_close
423 SYNOPSIS
424 bfd_boolean bfd_cache_close (bfd *abfd);
426 DESCRIPTION
427 Remove the BFD @var{abfd} from the cache. If the attached file is open,
428 then close it too.
430 RETURNS
431 <<FALSE>> is returned if closing the file fails, <<TRUE>> is
432 returned if all is well.
435 bfd_boolean
436 bfd_cache_close (bfd *abfd)
438 if (abfd->iovec != &cache_iovec)
439 return TRUE;
441 if (abfd->iostream == NULL)
442 /* Previously closed. */
443 return TRUE;
445 return bfd_cache_delete (abfd);
449 FUNCTION
450 bfd_cache_close_all
452 SYNOPSIS
453 bfd_boolean bfd_cache_close_all (void);
455 DESCRIPTION
456 Remove all BFDs from the cache. If the attached file is open,
457 then close it too.
459 RETURNS
460 <<FALSE>> is returned if closing one of the file fails, <<TRUE>> is
461 returned if all is well.
464 bfd_boolean
465 bfd_cache_close_all ()
467 bfd_boolean ret = TRUE;
469 while (bfd_last_cache != NULL)
470 ret &= bfd_cache_close (bfd_last_cache);
472 return ret;
476 INTERNAL_FUNCTION
477 bfd_open_file
479 SYNOPSIS
480 FILE* bfd_open_file (bfd *abfd);
482 DESCRIPTION
483 Call the OS to open a file for @var{abfd}. Return the <<FILE *>>
484 (possibly <<NULL>>) that results from this operation. Set up the
485 BFD so that future accesses know the file is open. If the <<FILE *>>
486 returned is <<NULL>>, then it won't have been put in the
487 cache, so it won't have to be removed from it.
490 FILE *
491 bfd_open_file (bfd *abfd)
493 abfd->cacheable = TRUE; /* Allow it to be closed later. */
495 if (open_files >= BFD_CACHE_MAX_OPEN)
497 if (! close_one ())
498 return NULL;
501 switch (abfd->direction)
503 case read_direction:
504 case no_direction:
505 abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_RB);
506 break;
507 case both_direction:
508 case write_direction:
509 if (abfd->opened_once)
511 abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_RUB);
512 if (abfd->iostream == NULL)
513 abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_WUB);
515 else
517 /* Create the file.
519 Some operating systems won't let us overwrite a running
520 binary. For them, we want to unlink the file first.
522 However, gcc 2.95 will create temporary files using
523 O_EXCL and tight permissions to prevent other users from
524 substituting other .o files during the compilation. gcc
525 will then tell the assembler to use the newly created
526 file as an output file. If we unlink the file here, we
527 open a brief window when another user could still
528 substitute a file.
530 So we unlink the output file if and only if it has
531 non-zero size. */
532 #ifndef __MSDOS__
533 /* Don't do this for MSDOS: it doesn't care about overwriting
534 a running binary, but if this file is already open by
535 another BFD, we will be in deep trouble if we delete an
536 open file. In fact, objdump does just that if invoked with
537 the --info option. */
538 struct stat s;
540 if (stat (abfd->filename, &s) == 0 && s.st_size != 0)
541 unlink_if_ordinary (abfd->filename);
542 #endif
543 abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_WUB);
544 abfd->opened_once = TRUE;
546 break;
549 if (abfd->iostream == NULL)
550 bfd_set_error (bfd_error_system_call);
551 else
553 if (! bfd_cache_init (abfd))
554 return NULL;
557 return (FILE *) abfd->iostream;