* bfd-in.h (bfd_cache_close_all): New function declaration.
[deliverable/binutils-gdb.git] / bfd / cache.c
CommitLineData
252b5132 1/* BFD library -- caching of file descriptors.
7c192733
AC
2
3 Copyright 1990, 1991, 1992, 1993, 1994, 1996, 2000, 2001, 2002,
4 2003, 2004 Free Software Foundation, Inc.
5
252b5132
RH
6 Hacked by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
7
8This file is part of BFD, the Binary File Descriptor library.
9
10This program is free software; you can redistribute it and/or modify
11it under the terms of the GNU General Public License as published by
12the Free Software Foundation; either version 2 of the License, or
13(at your option) any later version.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23
24/*
25SECTION
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
e60b52c6 37 handle.
252b5132
RH
38
39*/
40
41#include "bfd.h"
42#include "sysdep.h"
43#include "libbfd.h"
44
c58b9523 45static bfd_boolean bfd_cache_delete (bfd *);
252b5132 46
40838a72
AC
47
48static file_ptr
49cache_btell (struct bfd *abfd)
50{
51 return real_ftell (bfd_cache_lookup (abfd));
52}
53
54static int
55cache_bseek (struct bfd *abfd, file_ptr offset, int whence)
56{
57 return real_fseek (bfd_cache_lookup (abfd), offset, whence);
58}
59
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.
62
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. */
66
67static file_ptr
68cache_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
69{
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;
82
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)
91 {
92 bfd_set_error (bfd_error_system_call);
93 return -1;
94 }
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)))
101 {
102 bfd_set_error (bfd_error_system_call);
103 return -1;
104 }
105#endif
106 return nread;
107}
108
109static file_ptr
110cache_bwrite (struct bfd *abfd, const void *where, file_ptr nbytes)
111{
112 file_ptr nwrite = fwrite (where, 1, nbytes, bfd_cache_lookup (abfd));
113 if (nwrite < nbytes && ferror (bfd_cache_lookup (abfd)))
114 {
115 bfd_set_error (bfd_error_system_call);
116 return -1;
117 }
118 return nwrite;
119}
120
121static int
122cache_bclose (struct bfd *abfd)
123{
124 return bfd_cache_close (abfd);
125}
126
127static int
128cache_bflush (struct bfd *abfd)
129{
130 int sts = fflush (bfd_cache_lookup (abfd));
131 if (sts < 0)
132 bfd_set_error (bfd_error_system_call);
133 return sts;
134}
135
136static int
137cache_bstat (struct bfd *abfd, struct stat *sb)
138{
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;
143}
144
145static const struct bfd_iovec cache_iovec = {
146 &cache_bread, &cache_bwrite, &cache_btell, &cache_bseek,
147 &cache_bclose, &cache_bflush, &cache_bstat
148};
149
252b5132
RH
150/*
151INTERNAL_FUNCTION
152 BFD_CACHE_MAX_OPEN macro
153
154DESCRIPTION
155 The maximum number of files which the cache will keep open at
156 one time.
157
158.#define BFD_CACHE_MAX_OPEN 10
159
160*/
161
162/* The number of BFD files we have open. */
163
164static int open_files;
165
166/*
167INTERNAL_FUNCTION
168 bfd_last_cache
169
170SYNOPSIS
171 extern bfd *bfd_last_cache;
172
173DESCRIPTION
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.
177*/
178
179bfd *bfd_last_cache;
180
181/*
182 INTERNAL_FUNCTION
183 bfd_cache_lookup
e60b52c6 184
252b5132
RH
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.
e60b52c6 190
252b5132 191 .#define bfd_cache_lookup(x) \
06fc8a8c
NC
192 . ((x) == bfd_last_cache ? \
193 . (FILE *) (bfd_last_cache->iostream): \
194 . bfd_cache_lookup_worker (x))
e60b52c6 195
252b5132
RH
196 */
197
198/* Insert a BFD into the cache. */
199
c58b9523
AM
200static void
201insert (bfd *abfd)
252b5132
RH
202{
203 if (bfd_last_cache == NULL)
204 {
205 abfd->lru_next = abfd;
206 abfd->lru_prev = abfd;
207 }
208 else
209 {
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;
214 }
215 bfd_last_cache = abfd;
216}
217
218/* Remove a BFD from the cache. */
219
c58b9523
AM
220static void
221snip (bfd *abfd)
252b5132
RH
222{
223 abfd->lru_prev->lru_next = abfd->lru_next;
224 abfd->lru_next->lru_prev = abfd->lru_prev;
225 if (abfd == bfd_last_cache)
226 {
227 bfd_last_cache = abfd->lru_next;
228 if (abfd == bfd_last_cache)
229 bfd_last_cache = NULL;
230 }
231}
232
233/* We need to open a new file, and the cache is full. Find the least
234 recently used cacheable BFD and close it. */
235
b34976b6 236static bfd_boolean
c58b9523 237close_one (void)
252b5132
RH
238{
239 register bfd *kill;
240
241 if (bfd_last_cache == NULL)
242 kill = NULL;
243 else
244 {
245 for (kill = bfd_last_cache->lru_prev;
246 ! kill->cacheable;
247 kill = kill->lru_prev)
248 {
249 if (kill == bfd_last_cache)
250 {
251 kill = NULL;
252 break;
253 }
254 }
255 }
256
257 if (kill == NULL)
258 {
259 /* There are no open cacheable BFD's. */
b34976b6 260 return TRUE;
252b5132
RH
261 }
262
7c192733 263 kill->where = real_ftell ((FILE *) kill->iostream);
252b5132
RH
264
265 return bfd_cache_delete (kill);
266}
267
268/* Close a BFD and remove it from the cache. */
269
b34976b6 270static bfd_boolean
c58b9523 271bfd_cache_delete (bfd *abfd)
252b5132 272{
b34976b6 273 bfd_boolean ret;
252b5132
RH
274
275 if (fclose ((FILE *) abfd->iostream) == 0)
b34976b6 276 ret = TRUE;
252b5132
RH
277 else
278 {
b34976b6 279 ret = FALSE;
252b5132
RH
280 bfd_set_error (bfd_error_system_call);
281 }
282
283 snip (abfd);
284
285 abfd->iostream = NULL;
286 --open_files;
287
288 return ret;
289}
290
291/*
292INTERNAL_FUNCTION
293 bfd_cache_init
294
295SYNOPSIS
b34976b6 296 bfd_boolean bfd_cache_init (bfd *abfd);
252b5132
RH
297
298DESCRIPTION
299 Add a newly opened BFD to the cache.
300*/
301
b34976b6 302bfd_boolean
c58b9523 303bfd_cache_init (bfd *abfd)
252b5132
RH
304{
305 BFD_ASSERT (abfd->iostream != NULL);
306 if (open_files >= BFD_CACHE_MAX_OPEN)
307 {
308 if (! close_one ())
b34976b6 309 return FALSE;
252b5132 310 }
40838a72 311 abfd->iovec = &cache_iovec;
252b5132
RH
312 insert (abfd);
313 ++open_files;
b34976b6 314 return TRUE;
252b5132
RH
315}
316
317/*
318INTERNAL_FUNCTION
319 bfd_cache_close
320
321SYNOPSIS
b34976b6 322 bfd_boolean bfd_cache_close (bfd *abfd);
252b5132
RH
323
324DESCRIPTION
325 Remove the BFD @var{abfd} from the cache. If the attached file is open,
326 then close it too.
327
328RETURNS
b34976b6 329 <<FALSE>> is returned if closing the file fails, <<TRUE>> is
252b5132
RH
330 returned if all is well.
331*/
332
b34976b6 333bfd_boolean
c58b9523 334bfd_cache_close (bfd *abfd)
252b5132 335{
40838a72 336 if (abfd->iovec != &cache_iovec)
b34976b6 337 return TRUE;
252b5132 338
fe2e161a
AC
339 if (abfd->iostream == NULL)
340 /* Previously closed. */
341 return TRUE;
342
252b5132
RH
343 return bfd_cache_delete (abfd);
344}
345
02d5a37b
JG
346/*
347FUNCTION
348 bfd_cache_close_all
349
350SYNOPSIS
351 bfd_boolean bfd_cache_close_all (void);
352
353DESCRIPTION
354 Remove all BFDs from the cache. If the attached file is open,
355 then close it too.
356
357RETURNS
358 <<FALSE>> is returned if closing one of the file fails, <<TRUE>> is
359 returned if all is well.
360*/
361
362bfd_boolean
363bfd_cache_close_all ()
364{
365 bfd_boolean ret = TRUE;
366
367 while (bfd_last_cache != NULL)
368 ret &= bfd_cache_close (bfd_last_cache);
369}
370
252b5132
RH
371/*
372INTERNAL_FUNCTION
373 bfd_open_file
374
375SYNOPSIS
c58b9523 376 FILE* bfd_open_file (bfd *abfd);
252b5132
RH
377
378DESCRIPTION
379 Call the OS to open a file for @var{abfd}. Return the <<FILE *>>
380 (possibly <<NULL>>) that results from this operation. Set up the
381 BFD so that future accesses know the file is open. If the <<FILE *>>
382 returned is <<NULL>>, then it won't have been put in the
383 cache, so it won't have to be removed from it.
384*/
385
386FILE *
c58b9523 387bfd_open_file (bfd *abfd)
252b5132 388{
b34976b6 389 abfd->cacheable = TRUE; /* Allow it to be closed later. */
252b5132
RH
390
391 if (open_files >= BFD_CACHE_MAX_OPEN)
392 {
393 if (! close_one ())
394 return NULL;
395 }
396
397 switch (abfd->direction)
398 {
399 case read_direction:
400 case no_direction:
401 abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_RB);
402 break;
403 case both_direction:
404 case write_direction:
82e51918 405 if (abfd->opened_once)
252b5132
RH
406 {
407 abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_RUB);
408 if (abfd->iostream == NULL)
409 abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_WUB);
410 }
411 else
412 {
9e422a2e
ILT
413 /* Create the file.
414
415 Some operating systems won't let us overwrite a running
416 binary. For them, we want to unlink the file first.
417
418 However, gcc 2.95 will create temporary files using
419 O_EXCL and tight permissions to prevent other users from
420 substituting other .o files during the compilation. gcc
421 will then tell the assembler to use the newly created
422 file as an output file. If we unlink the file here, we
423 open a brief window when another user could still
424 substitute a file.
425
426 So we unlink the output file if and only if it has
427 non-zero size. */
5af11cab
AM
428#ifndef __MSDOS__
429 /* Don't do this for MSDOS: it doesn't care about overwriting
430 a running binary, but if this file is already open by
431 another BFD, we will be in deep trouble if we delete an
432 open file. In fact, objdump does just that if invoked with
433 the --info option. */
9e422a2e
ILT
434 struct stat s;
435
436 if (stat (abfd->filename, &s) == 0 && s.st_size != 0)
437 unlink (abfd->filename);
5af11cab 438#endif
c46b7515 439 abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_WUB);
b34976b6 440 abfd->opened_once = TRUE;
252b5132
RH
441 }
442 break;
443 }
444
445 if (abfd->iostream != NULL)
446 {
447 if (! bfd_cache_init (abfd))
448 return NULL;
449 }
450
451 return (FILE *) abfd->iostream;
452}
453
454/*
455INTERNAL_FUNCTION
456 bfd_cache_lookup_worker
457
458SYNOPSIS
c58b9523 459 FILE *bfd_cache_lookup_worker (bfd *abfd);
252b5132
RH
460
461DESCRIPTION
462 Called when the macro <<bfd_cache_lookup>> fails to find a
463 quick answer. Find a file descriptor for @var{abfd}. If
464 necessary, it open it. If there are already more than
465 <<BFD_CACHE_MAX_OPEN>> files open, it tries to close one first, to
06fc8a8c
NC
466 avoid running out of file descriptors. It will abort rather than
467 returning NULL if it is unable to (re)open the @var{abfd}.
252b5132
RH
468*/
469
470FILE *
c58b9523 471bfd_cache_lookup_worker (bfd *abfd)
252b5132
RH
472{
473 if ((abfd->flags & BFD_IN_MEMORY) != 0)
474 abort ();
475
e60b52c6 476 if (abfd->my_archive)
252b5132
RH
477 abfd = abfd->my_archive;
478
479 if (abfd->iostream != NULL)
480 {
481 /* Move the file to the start of the cache. */
482 if (abfd != bfd_last_cache)
483 {
484 snip (abfd);
485 insert (abfd);
486 }
487 }
488 else
489 {
06fc8a8c
NC
490 if (bfd_open_file (abfd) == NULL
491 || abfd->where != (unsigned long) abfd->where
492 || real_fseek ((FILE *) abfd->iostream, abfd->where, SEEK_SET) != 0)
493 abort ();
252b5132
RH
494 }
495
496 return (FILE *) abfd->iostream;
497}
This page took 0.256894 seconds and 4 git commands to generate.