2004-04-21 Andrew Cagney <cagney@redhat.com>
[binutils.git] / bfd / cache.c
blob9146c05caca698febe0159986ad3665abcdc96d8
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 SECTION
26 File caching
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
37 handle.
41 #include "bfd.h"
42 #include "sysdep.h"
43 #include "libbfd.h"
45 static bfd_boolean bfd_cache_delete (bfd *);
48 static file_ptr
49 cache_btell (struct bfd *abfd)
51 return real_ftell (bfd_cache_lookup (abfd));
54 static int
55 cache_bseek (struct bfd *abfd, file_ptr offset, int whence)
57 return real_fseek (bfd_cache_lookup (abfd), offset, whence);
60 /* Note that archive entries don't have streams; they share their parent's.
61 This allows someone to play with the iostream behind BFD's back.
63 Also, note that the origin pointer points to the beginning of a file's
64 contents (0 for non-archive elements). For archive entries this is the
65 first octet in the file, NOT the beginning of the archive header. */
67 static file_ptr
68 cache_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
70 file_ptr nread;
71 /* FIXME - this looks like an optimization, but it's really to cover
72 up for a feature of some OSs (not solaris - sigh) that
73 ld/pe-dll.c takes advantage of (apparently) when it creates BFDs
74 internally and tries to link against them. BFD seems to be smart
75 enough to realize there are no symbol records in the "file" that
76 doesn't exist but attempts to read them anyway. On Solaris,
77 attempting to read zero bytes from a NULL file results in a core
78 dump, but on other platforms it just returns zero bytes read.
79 This makes it to something reasonable. - DJ */
80 if (nbytes == 0)
81 return 0;
83 #if defined (__VAX) && defined (VMS)
84 /* Apparently fread on Vax VMS does not keep the record length
85 information. */
86 nread = read (fileno (bfd_cache_lookup (abfd)), buf, nbytes);
87 /* Set bfd_error if we did not read as much data as we expected. If
88 the read failed due to an error set the bfd_error_system_call,
89 else set bfd_error_file_truncated. */
90 if (nread == (file_ptr)-1)
92 bfd_set_error (bfd_error_system_call);
93 return -1;
95 #else
96 nread = fread (buf, 1, nbytes, bfd_cache_lookup (abfd));
97 /* Set bfd_error if we did not read as much data as we expected. If
98 the read failed due to an error set the bfd_error_system_call,
99 else set bfd_error_file_truncated. */
100 if (nread < nbytes && ferror (bfd_cache_lookup (abfd)))
102 bfd_set_error (bfd_error_system_call);
103 return -1;
105 #endif
106 return nread;
109 static file_ptr
110 cache_bwrite (struct bfd *abfd, const void *where, file_ptr nbytes)
112 file_ptr nwrite = fwrite (where, 1, nbytes, bfd_cache_lookup (abfd));
113 if (nwrite < nbytes && ferror (bfd_cache_lookup (abfd)))
115 bfd_set_error (bfd_error_system_call);
116 return -1;
118 return nwrite;
121 static int
122 cache_bclose (struct bfd *abfd)
124 return bfd_cache_close (abfd);
127 static int
128 cache_bflush (struct bfd *abfd)
130 int sts = fflush (bfd_cache_lookup (abfd));
131 if (sts < 0)
132 bfd_set_error (bfd_error_system_call);
133 return sts;
136 static int
137 cache_bstat (struct bfd *abfd, struct stat *sb)
139 int sts = fstat (fileno (bfd_cache_lookup (abfd)), sb);
140 if (sts < 0)
141 bfd_set_error (bfd_error_system_call);
142 return sts;
145 static const struct bfd_iovec cache_iovec = {
146 &cache_bread, &cache_bwrite, &cache_btell, &cache_bseek,
147 &cache_bclose, &cache_bflush, &cache_bstat
151 INTERNAL_FUNCTION
152 BFD_CACHE_MAX_OPEN macro
154 DESCRIPTION
155 The maximum number of files which the cache will keep open at
156 one time.
158 .#define BFD_CACHE_MAX_OPEN 10
162 /* The number of BFD files we have open. */
164 static int open_files;
167 INTERNAL_FUNCTION
168 bfd_last_cache
170 SYNOPSIS
171 extern bfd *bfd_last_cache;
173 DESCRIPTION
174 Zero, or a pointer to the topmost BFD on the chain. This is
175 used by the <<bfd_cache_lookup>> macro in @file{libbfd.h} to
176 determine when it can avoid a function call.
179 bfd *bfd_last_cache;
182 INTERNAL_FUNCTION
183 bfd_cache_lookup
185 DESCRIPTION
186 Check to see if the required BFD is the same as the last one
187 looked up. If so, then it can use the stream in the BFD with
188 impunity, since it can't have changed since the last lookup;
189 otherwise, it has to perform the complicated lookup function.
191 .#define bfd_cache_lookup(x) \
192 . ((x)==bfd_last_cache? \
193 . (FILE*) (bfd_last_cache->iostream): \
194 . bfd_cache_lookup_worker(x))
198 /* Insert a BFD into the cache. */
200 static void
201 insert (bfd *abfd)
203 if (bfd_last_cache == NULL)
205 abfd->lru_next = abfd;
206 abfd->lru_prev = abfd;
208 else
210 abfd->lru_next = bfd_last_cache;
211 abfd->lru_prev = bfd_last_cache->lru_prev;
212 abfd->lru_prev->lru_next = abfd;
213 abfd->lru_next->lru_prev = abfd;
215 bfd_last_cache = abfd;
218 /* Remove a BFD from the cache. */
220 static void
221 snip (bfd *abfd)
223 abfd->lru_prev->lru_next = abfd->lru_next;
224 abfd->lru_next->lru_prev = abfd->lru_prev;
225 if (abfd == bfd_last_cache)
227 bfd_last_cache = abfd->lru_next;
228 if (abfd == bfd_last_cache)
229 bfd_last_cache = NULL;
233 /* We need to open a new file, and the cache is full. Find the least
234 recently used cacheable BFD and close it. */
236 static bfd_boolean
237 close_one (void)
239 register bfd *kill;
241 if (bfd_last_cache == NULL)
242 kill = NULL;
243 else
245 for (kill = bfd_last_cache->lru_prev;
246 ! kill->cacheable;
247 kill = kill->lru_prev)
249 if (kill == bfd_last_cache)
251 kill = NULL;
252 break;
257 if (kill == NULL)
259 /* There are no open cacheable BFD's. */
260 return TRUE;
263 kill->where = real_ftell ((FILE *) kill->iostream);
265 return bfd_cache_delete (kill);
268 /* Close a BFD and remove it from the cache. */
270 static bfd_boolean
271 bfd_cache_delete (bfd *abfd)
273 bfd_boolean ret;
275 if (fclose ((FILE *) abfd->iostream) == 0)
276 ret = TRUE;
277 else
279 ret = FALSE;
280 bfd_set_error (bfd_error_system_call);
283 snip (abfd);
285 abfd->iostream = NULL;
286 --open_files;
288 return ret;
292 INTERNAL_FUNCTION
293 bfd_cache_init
295 SYNOPSIS
296 bfd_boolean bfd_cache_init (bfd *abfd);
298 DESCRIPTION
299 Add a newly opened BFD to the cache.
302 bfd_boolean
303 bfd_cache_init (bfd *abfd)
305 BFD_ASSERT (abfd->iostream != NULL);
306 if (open_files >= BFD_CACHE_MAX_OPEN)
308 if (! close_one ())
309 return FALSE;
311 abfd->iovec = &cache_iovec;
312 insert (abfd);
313 ++open_files;
314 return TRUE;
318 INTERNAL_FUNCTION
319 bfd_cache_close
321 SYNOPSIS
322 bfd_boolean bfd_cache_close (bfd *abfd);
324 DESCRIPTION
325 Remove the BFD @var{abfd} from the cache. If the attached file is open,
326 then close it too.
328 RETURNS
329 <<FALSE>> is returned if closing the file fails, <<TRUE>> is
330 returned if all is well.
333 bfd_boolean
334 bfd_cache_close (bfd *abfd)
336 if (abfd->iovec != &cache_iovec)
337 return TRUE;
339 return bfd_cache_delete (abfd);
343 INTERNAL_FUNCTION
344 bfd_open_file
346 SYNOPSIS
347 FILE* bfd_open_file (bfd *abfd);
349 DESCRIPTION
350 Call the OS to open a file for @var{abfd}. Return the <<FILE *>>
351 (possibly <<NULL>>) that results from this operation. Set up the
352 BFD so that future accesses know the file is open. If the <<FILE *>>
353 returned is <<NULL>>, then it won't have been put in the
354 cache, so it won't have to be removed from it.
357 FILE *
358 bfd_open_file (bfd *abfd)
360 abfd->cacheable = TRUE; /* Allow it to be closed later. */
362 if (open_files >= BFD_CACHE_MAX_OPEN)
364 if (! close_one ())
365 return NULL;
368 switch (abfd->direction)
370 case read_direction:
371 case no_direction:
372 abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_RB);
373 break;
374 case both_direction:
375 case write_direction:
376 if (abfd->opened_once)
378 abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_RUB);
379 if (abfd->iostream == NULL)
380 abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_WUB);
382 else
384 /* Create the file.
386 Some operating systems won't let us overwrite a running
387 binary. For them, we want to unlink the file first.
389 However, gcc 2.95 will create temporary files using
390 O_EXCL and tight permissions to prevent other users from
391 substituting other .o files during the compilation. gcc
392 will then tell the assembler to use the newly created
393 file as an output file. If we unlink the file here, we
394 open a brief window when another user could still
395 substitute a file.
397 So we unlink the output file if and only if it has
398 non-zero size. */
399 #ifndef __MSDOS__
400 /* Don't do this for MSDOS: it doesn't care about overwriting
401 a running binary, but if this file is already open by
402 another BFD, we will be in deep trouble if we delete an
403 open file. In fact, objdump does just that if invoked with
404 the --info option. */
405 struct stat s;
407 if (stat (abfd->filename, &s) == 0 && s.st_size != 0)
408 unlink (abfd->filename);
409 #endif
410 abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_WUB);
411 abfd->opened_once = TRUE;
413 break;
416 if (abfd->iostream != NULL)
418 if (! bfd_cache_init (abfd))
419 return NULL;
422 return (FILE *) abfd->iostream;
426 INTERNAL_FUNCTION
427 bfd_cache_lookup_worker
429 SYNOPSIS
430 FILE *bfd_cache_lookup_worker (bfd *abfd);
432 DESCRIPTION
433 Called when the macro <<bfd_cache_lookup>> fails to find a
434 quick answer. Find a file descriptor for @var{abfd}. If
435 necessary, it open it. If there are already more than
436 <<BFD_CACHE_MAX_OPEN>> files open, it tries to close one first, to
437 avoid running out of file descriptors.
440 FILE *
441 bfd_cache_lookup_worker (bfd *abfd)
443 if ((abfd->flags & BFD_IN_MEMORY) != 0)
444 abort ();
446 if (abfd->my_archive)
447 abfd = abfd->my_archive;
449 if (abfd->iostream != NULL)
451 /* Move the file to the start of the cache. */
452 if (abfd != bfd_last_cache)
454 snip (abfd);
455 insert (abfd);
458 else
460 if (bfd_open_file (abfd) == NULL)
461 return NULL;
462 if (abfd->where != (unsigned long) abfd->where)
463 return NULL;
464 if (real_fseek ((FILE *) abfd->iostream, abfd->where, SEEK_SET) != 0)
465 return NULL;
468 return (FILE *) abfd->iostream;