* cache.c (bfd_cache_lookup_worker): Don't abort on failing to
[external/binutils.git] / bfd / cache.c
1 /* BFD library -- caching of file descriptors.
2
3    Copyright 1990, 1991, 1992, 1993, 1994, 1996, 2000, 2001, 2002,
4    2003, 2004 Free Software Foundation, Inc.
5
6    Hacked by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
7
8 This file is part of BFD, the Binary File Descriptor library.
9
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.
14
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.
19
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.  */
23
24 /*
25 SECTION
26         File caching
27
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.
38
39 SUBSECTION
40         Caching functions
41 */
42
43 #include "bfd.h"
44 #include "sysdep.h"
45 #include "libbfd.h"
46 #include "libiberty.h"
47
48 static bfd_boolean bfd_cache_delete (bfd *);
49
50
51 static file_ptr
52 cache_btell (struct bfd *abfd)
53 {
54   FILE *f = bfd_cache_lookup (abfd);
55   if (f == NULL)
56     return -1;
57   return real_ftell (f);
58 }
59
60 static int
61 cache_bseek (struct bfd *abfd, file_ptr offset, int whence)
62 {
63   FILE *f = bfd_cache_lookup (abfd);
64   if (f == NULL)
65     return -1;
66   return real_fseek (f, offset, whence);
67 }
68
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.
71
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.  */
75
76 static file_ptr
77 cache_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
78 {
79   FILE *f;
80   file_ptr nread;
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 */
90   if (nbytes == 0)
91     return 0;
92
93   f = bfd_cache_lookup (abfd);
94   if (f == NULL)
95     return 0;
96
97 #if defined (__VAX) && defined (VMS)
98   /* Apparently fread on Vax VMS does not keep the record length
99      information.  */
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)
105     {
106       bfd_set_error (bfd_error_system_call);
107       return -1;
108     }
109 #else
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))
115     {
116       bfd_set_error (bfd_error_system_call);
117       return -1;
118     }
119 #endif
120   return nread;
121 }
122
123 static file_ptr
124 cache_bwrite (struct bfd *abfd, const void *where, file_ptr nbytes)
125 {
126   file_ptr nwrite;
127   FILE *f = bfd_cache_lookup (abfd);
128   if (f == NULL)
129     return 0;
130   nwrite = fwrite (where, 1, nbytes, f);
131   if (nwrite < nbytes && ferror (f))
132     {
133       bfd_set_error (bfd_error_system_call);
134       return -1;
135     }
136   return nwrite;
137 }
138
139 static int
140 cache_bclose (struct bfd *abfd)
141 {
142   return bfd_cache_close (abfd);
143 }
144
145 static int
146 cache_bflush (struct bfd *abfd)
147 {
148   int sts;
149   FILE *f = bfd_cache_lookup (abfd);
150   if (f == NULL)
151     return -1;
152   sts = fflush (f);
153   if (sts < 0)
154     bfd_set_error (bfd_error_system_call);
155   return sts;
156 }
157
158 static int
159 cache_bstat (struct bfd *abfd, struct stat *sb)
160 {
161   int sts;
162   FILE *f = bfd_cache_lookup (abfd);
163   if (f == NULL)
164     return -1;
165   sts = fstat (fileno (f), sb);
166   if (sts < 0)
167     bfd_set_error (bfd_error_system_call);
168   return sts;
169 }
170
171 static const struct bfd_iovec cache_iovec = {
172   &cache_bread, &cache_bwrite, &cache_btell, &cache_bseek,
173   &cache_bclose, &cache_bflush, &cache_bstat
174 };
175
176 /*
177 INTERNAL_FUNCTION
178         BFD_CACHE_MAX_OPEN macro
179
180 DESCRIPTION
181         The maximum number of files which the cache will keep open at
182         one time.
183
184 .#define BFD_CACHE_MAX_OPEN 10
185
186 */
187
188 /* The number of BFD files we have open.  */
189
190 static int open_files;
191
192 /*
193 INTERNAL_FUNCTION
194         bfd_last_cache
195
196 SYNOPSIS
197         extern bfd *bfd_last_cache;
198
199 DESCRIPTION
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.
203 */
204
205 bfd *bfd_last_cache = NULL;
206
207 /*
208   INTERNAL_FUNCTION
209         bfd_cache_lookup
210
211   DESCRIPTION
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.
216
217   .#define bfd_cache_lookup(x) \
218   .    ((x) == bfd_last_cache ? \
219   .      (FILE *) (bfd_last_cache->iostream): \
220   .       bfd_cache_lookup_worker (x))
221
222  */
223
224 /* Insert a BFD into the cache.  */
225
226 static void
227 insert (bfd *abfd)
228 {
229   if (bfd_last_cache == NULL)
230     {
231       abfd->lru_next = abfd;
232       abfd->lru_prev = abfd;
233     }
234   else
235     {
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;
240     }
241   bfd_last_cache = abfd;
242 }
243
244 /* Remove a BFD from the cache.  */
245
246 static void
247 snip (bfd *abfd)
248 {
249   abfd->lru_prev->lru_next = abfd->lru_next;
250   abfd->lru_next->lru_prev = abfd->lru_prev;
251   if (abfd == bfd_last_cache)
252     {
253       bfd_last_cache = abfd->lru_next;
254       if (abfd == bfd_last_cache)
255         bfd_last_cache = NULL;
256     }
257 }
258
259 /* We need to open a new file, and the cache is full.  Find the least
260    recently used cacheable BFD and close it.  */
261
262 static bfd_boolean
263 close_one (void)
264 {
265   register bfd *kill;
266
267   if (bfd_last_cache == NULL)
268     kill = NULL;
269   else
270     {
271       for (kill = bfd_last_cache->lru_prev;
272            ! kill->cacheable;
273            kill = kill->lru_prev)
274         {
275           if (kill == bfd_last_cache)
276             {
277               kill = NULL;
278               break;
279             }
280         }
281     }
282
283   if (kill == NULL)
284     {
285       /* There are no open cacheable BFD's.  */
286       return TRUE;
287     }
288
289   kill->where = real_ftell ((FILE *) kill->iostream);
290
291   return bfd_cache_delete (kill);
292 }
293
294 /* Close a BFD and remove it from the cache.  */
295
296 static bfd_boolean
297 bfd_cache_delete (bfd *abfd)
298 {
299   bfd_boolean ret;
300
301   if (fclose ((FILE *) abfd->iostream) == 0)
302     ret = TRUE;
303   else
304     {
305       ret = FALSE;
306       bfd_set_error (bfd_error_system_call);
307     }
308
309   snip (abfd);
310
311   abfd->iostream = NULL;
312   --open_files;
313
314   return ret;
315 }
316
317 /*
318 INTERNAL_FUNCTION
319         bfd_cache_init
320
321 SYNOPSIS
322         bfd_boolean bfd_cache_init (bfd *abfd);
323
324 DESCRIPTION
325         Add a newly opened BFD to the cache.
326 */
327
328 bfd_boolean
329 bfd_cache_init (bfd *abfd)
330 {
331   BFD_ASSERT (abfd->iostream != NULL);
332   if (open_files >= BFD_CACHE_MAX_OPEN)
333     {
334       if (! close_one ())
335         return FALSE;
336     }
337   abfd->iovec = &cache_iovec;
338   insert (abfd);
339   ++open_files;
340   return TRUE;
341 }
342
343 /*
344 INTERNAL_FUNCTION
345         bfd_cache_close
346
347 SYNOPSIS
348         bfd_boolean bfd_cache_close (bfd *abfd);
349
350 DESCRIPTION
351         Remove the BFD @var{abfd} from the cache. If the attached file is open,
352         then close it too.
353
354 RETURNS
355         <<FALSE>> is returned if closing the file fails, <<TRUE>> is
356         returned if all is well.
357 */
358
359 bfd_boolean
360 bfd_cache_close (bfd *abfd)
361 {
362   if (abfd->iovec != &cache_iovec)
363     return TRUE;
364
365   if (abfd->iostream == NULL)
366     /* Previously closed.  */
367     return TRUE;
368
369   return bfd_cache_delete (abfd);
370 }
371
372 /*
373 FUNCTION
374         bfd_cache_close_all
375
376 SYNOPSIS
377         bfd_boolean bfd_cache_close_all (void);
378
379 DESCRIPTION
380         Remove all BFDs from the cache. If the attached file is open,
381         then close it too.
382
383 RETURNS
384         <<FALSE>> is returned if closing one of the file fails, <<TRUE>> is
385         returned if all is well.
386 */
387
388 bfd_boolean
389 bfd_cache_close_all ()
390 {
391   bfd_boolean ret = TRUE;
392
393   while (bfd_last_cache != NULL)
394     ret &= bfd_cache_close (bfd_last_cache);
395
396   return ret;
397 }
398
399 /*
400 INTERNAL_FUNCTION
401         bfd_open_file
402
403 SYNOPSIS
404         FILE* bfd_open_file (bfd *abfd);
405
406 DESCRIPTION
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.
412 */
413
414 FILE *
415 bfd_open_file (bfd *abfd)
416 {
417   abfd->cacheable = TRUE;       /* Allow it to be closed later.  */
418
419   if (open_files >= BFD_CACHE_MAX_OPEN)
420     {
421       if (! close_one ())
422         return NULL;
423     }
424
425   switch (abfd->direction)
426     {
427     case read_direction:
428     case no_direction:
429       abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_RB);
430       break;
431     case both_direction:
432     case write_direction:
433       if (abfd->opened_once)
434         {
435           abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_RUB);
436           if (abfd->iostream == NULL)
437             abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_WUB);
438         }
439       else
440         {
441           /* Create the file.
442
443              Some operating systems won't let us overwrite a running
444              binary.  For them, we want to unlink the file first.
445
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
452              substitute a file.
453
454              So we unlink the output file if and only if it has
455              non-zero size.  */
456 #ifndef __MSDOS__
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.  */
462           struct stat s;
463
464           if (stat (abfd->filename, &s) == 0 && s.st_size != 0)
465             unlink_if_ordinary (abfd->filename);
466 #endif
467           abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_WUB);
468           abfd->opened_once = TRUE;
469         }
470       break;
471     }
472
473   if (abfd->iostream == NULL)
474     bfd_set_error (bfd_error_system_call);
475   else
476     {
477       if (! bfd_cache_init (abfd))
478         return NULL;
479     }
480
481   return (FILE *) abfd->iostream;
482 }
483
484 /*
485 INTERNAL_FUNCTION
486         bfd_cache_lookup_worker
487
488 SYNOPSIS
489         FILE *bfd_cache_lookup_worker (bfd *abfd);
490
491 DESCRIPTION
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}.
498 */
499
500 FILE *
501 bfd_cache_lookup_worker (bfd *abfd)
502 {
503   bfd *orig_bfd = abfd;
504   if ((abfd->flags & BFD_IN_MEMORY) != 0)
505     abort ();
506
507   if (abfd->my_archive)
508     abfd = abfd->my_archive;
509
510   if (abfd->iostream != NULL)
511     {
512       /* Move the file to the start of the cache.  */
513       if (abfd != bfd_last_cache)
514         {
515           snip (abfd);
516           insert (abfd);
517         }
518       return (FILE *) abfd->iostream;
519     }
520
521   if (bfd_open_file (abfd) == NULL)
522     ;
523   else if (real_fseek ((FILE *) abfd->iostream, abfd->where, SEEK_SET) != 0)
524     bfd_set_error (bfd_error_system_call);
525   else
526     return (FILE *) abfd->iostream;
527
528   (*_bfd_error_handler) (_("reopening %B: %s\n"),
529                          orig_bfd, bfd_errmsg (bfd_get_error ()));
530   return NULL;
531 }