]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/cache.c
* hppabsd-core.c (hppabsd_core_core_file_p): Use bfd_stat, not fstat.
[thirdparty/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
3e110533 22Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
252b5132
RH
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 38
1b74d094
BW
39SUBSECTION
40 Caching functions
252b5132
RH
41*/
42
43#include "bfd.h"
44#include "sysdep.h"
45#include "libbfd.h"
bb14f524 46#include "libiberty.h"
252b5132 47
d0fdd288
AM
48/*
49INTERNAL_FUNCTION
50 BFD_CACHE_MAX_OPEN macro
51
52DESCRIPTION
53 The maximum number of files which the cache will keep open at
54 one time.
55
56.#define BFD_CACHE_MAX_OPEN 10
57
58*/
59
60/* The number of BFD files we have open. */
61
62static int open_files;
63
64/*
65INTERNAL_FUNCTION
66 bfd_last_cache
67
68SYNOPSIS
69 extern bfd *bfd_last_cache;
70
71DESCRIPTION
72 Zero, or a pointer to the topmost BFD on the chain. This is
73 used by the <<bfd_cache_lookup>> macro in @file{libbfd.h} to
74 determine when it can avoid a function call.
75*/
76
77bfd *bfd_last_cache = NULL;
252b5132 78
d0fdd288
AM
79/* Insert a BFD into the cache. */
80
81static void
82insert (bfd *abfd)
83{
84 if (bfd_last_cache == NULL)
85 {
86 abfd->lru_next = abfd;
87 abfd->lru_prev = abfd;
88 }
89 else
90 {
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;
95 }
96 bfd_last_cache = abfd;
97}
98
99/* Remove a BFD from the cache. */
100
101static void
102snip (bfd *abfd)
103{
104 abfd->lru_prev->lru_next = abfd->lru_next;
105 abfd->lru_next->lru_prev = abfd->lru_prev;
106 if (abfd == bfd_last_cache)
107 {
108 bfd_last_cache = abfd->lru_next;
109 if (abfd == bfd_last_cache)
110 bfd_last_cache = NULL;
111 }
112}
113
114/* Close a BFD and remove it from the cache. */
115
116static bfd_boolean
117bfd_cache_delete (bfd *abfd)
118{
119 bfd_boolean ret;
120
121 if (fclose ((FILE *) abfd->iostream) == 0)
122 ret = TRUE;
123 else
124 {
125 ret = FALSE;
126 bfd_set_error (bfd_error_system_call);
127 }
128
129 snip (abfd);
130
131 abfd->iostream = NULL;
132 --open_files;
133
134 return ret;
135}
136
137/* We need to open a new file, and the cache is full. Find the least
138 recently used cacheable BFD and close it. */
139
140static bfd_boolean
141close_one (void)
142{
143 register bfd *kill;
144
145 if (bfd_last_cache == NULL)
146 kill = NULL;
147 else
148 {
149 for (kill = bfd_last_cache->lru_prev;
150 ! kill->cacheable;
151 kill = kill->lru_prev)
152 {
153 if (kill == bfd_last_cache)
154 {
155 kill = NULL;
156 break;
157 }
158 }
159 }
160
161 if (kill == NULL)
162 {
163 /* There are no open cacheable BFD's. */
164 return TRUE;
165 }
166
167 kill->where = real_ftell ((FILE *) kill->iostream);
168
169 return bfd_cache_delete (kill);
170}
171
172/*
173 INTERNAL_FUNCTION
174 bfd_cache_lookup
175
176 DESCRIPTION
177 Check to see if the required BFD is the same as the last one
178 looked up. If so, then it can use the stream in the BFD with
179 impunity, since it can't have changed since the last lookup;
180 otherwise, it has to perform the complicated lookup function.
181
182 .#define bfd_cache_lookup(x) \
183 . ((x) == bfd_last_cache ? \
184 . (FILE *) (bfd_last_cache->iostream): \
185 . bfd_cache_lookup_worker (x))
186
187 */
188
189/*
190INTERNAL_FUNCTION
191 bfd_cache_lookup_worker
192
193SYNOPSIS
194 FILE *bfd_cache_lookup_worker (bfd *abfd);
195
196DESCRIPTION
197 Called when the macro <<bfd_cache_lookup>> fails to find a
198 quick answer. Find a file descriptor for @var{abfd}. If
199 necessary, it open it. If there are already more than
200 <<BFD_CACHE_MAX_OPEN>> files open, it tries to close one first, to
201 avoid running out of file descriptors. It will return NULL
202 if it is unable to (re)open the @var{abfd}.
203*/
204
205FILE *
206bfd_cache_lookup_worker (bfd *abfd)
207{
208 bfd *orig_bfd = abfd;
209 if ((abfd->flags & BFD_IN_MEMORY) != 0)
210 abort ();
211
212 if (abfd->my_archive)
213 abfd = abfd->my_archive;
214
215 if (abfd->iostream != NULL)
216 {
217 /* Move the file to the start of the cache. */
218 if (abfd != bfd_last_cache)
219 {
220 snip (abfd);
221 insert (abfd);
222 }
223 return (FILE *) abfd->iostream;
224 }
225
226 if (bfd_open_file (abfd) == NULL)
227 ;
228 else if (real_fseek ((FILE *) abfd->iostream, abfd->where, SEEK_SET) != 0)
229 bfd_set_error (bfd_error_system_call);
230 else
231 return (FILE *) abfd->iostream;
232
233 (*_bfd_error_handler) (_("reopening %B: %s\n"),
234 orig_bfd, bfd_errmsg (bfd_get_error ()));
235 return NULL;
236}
40838a72
AC
237
238static file_ptr
239cache_btell (struct bfd *abfd)
240{
3dff57e8
AM
241 FILE *f = bfd_cache_lookup (abfd);
242 if (f == NULL)
243 return -1;
244 return real_ftell (f);
40838a72
AC
245}
246
247static int
248cache_bseek (struct bfd *abfd, file_ptr offset, int whence)
249{
3dff57e8
AM
250 FILE *f = bfd_cache_lookup (abfd);
251 if (f == NULL)
252 return -1;
253 return real_fseek (f, offset, whence);
40838a72
AC
254}
255
256/* Note that archive entries don't have streams; they share their parent's.
257 This allows someone to play with the iostream behind BFD's back.
258
259 Also, note that the origin pointer points to the beginning of a file's
260 contents (0 for non-archive elements). For archive entries this is the
261 first octet in the file, NOT the beginning of the archive header. */
262
263static file_ptr
264cache_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
265{
3dff57e8 266 FILE *f;
40838a72
AC
267 file_ptr nread;
268 /* FIXME - this looks like an optimization, but it's really to cover
269 up for a feature of some OSs (not solaris - sigh) that
270 ld/pe-dll.c takes advantage of (apparently) when it creates BFDs
271 internally and tries to link against them. BFD seems to be smart
272 enough to realize there are no symbol records in the "file" that
273 doesn't exist but attempts to read them anyway. On Solaris,
274 attempting to read zero bytes from a NULL file results in a core
275 dump, but on other platforms it just returns zero bytes read.
276 This makes it to something reasonable. - DJ */
277 if (nbytes == 0)
278 return 0;
279
3dff57e8
AM
280 f = bfd_cache_lookup (abfd);
281 if (f == NULL)
282 return 0;
283
40838a72
AC
284#if defined (__VAX) && defined (VMS)
285 /* Apparently fread on Vax VMS does not keep the record length
286 information. */
3dff57e8 287 nread = read (fileno (f), buf, nbytes);
40838a72
AC
288 /* Set bfd_error if we did not read as much data as we expected. If
289 the read failed due to an error set the bfd_error_system_call,
290 else set bfd_error_file_truncated. */
291 if (nread == (file_ptr)-1)
292 {
293 bfd_set_error (bfd_error_system_call);
294 return -1;
295 }
296#else
3dff57e8 297 nread = fread (buf, 1, nbytes, f);
40838a72
AC
298 /* Set bfd_error if we did not read as much data as we expected. If
299 the read failed due to an error set the bfd_error_system_call,
300 else set bfd_error_file_truncated. */
3dff57e8 301 if (nread < nbytes && ferror (f))
40838a72
AC
302 {
303 bfd_set_error (bfd_error_system_call);
304 return -1;
305 }
306#endif
307 return nread;
308}
309
310static file_ptr
311cache_bwrite (struct bfd *abfd, const void *where, file_ptr nbytes)
312{
3dff57e8
AM
313 file_ptr nwrite;
314 FILE *f = bfd_cache_lookup (abfd);
315 if (f == NULL)
316 return 0;
317 nwrite = fwrite (where, 1, nbytes, f);
318 if (nwrite < nbytes && ferror (f))
40838a72
AC
319 {
320 bfd_set_error (bfd_error_system_call);
321 return -1;
322 }
323 return nwrite;
324}
325
326static int
327cache_bclose (struct bfd *abfd)
328{
329 return bfd_cache_close (abfd);
330}
331
332static int
333cache_bflush (struct bfd *abfd)
334{
3dff57e8
AM
335 int sts;
336 FILE *f = bfd_cache_lookup (abfd);
337 if (f == NULL)
338 return -1;
339 sts = fflush (f);
40838a72
AC
340 if (sts < 0)
341 bfd_set_error (bfd_error_system_call);
342 return sts;
343}
344
345static int
346cache_bstat (struct bfd *abfd, struct stat *sb)
347{
3dff57e8
AM
348 int sts;
349 FILE *f = bfd_cache_lookup (abfd);
350 if (f == NULL)
351 return -1;
352 sts = fstat (fileno (f), sb);
40838a72
AC
353 if (sts < 0)
354 bfd_set_error (bfd_error_system_call);
355 return sts;
356}
357
358static const struct bfd_iovec cache_iovec = {
359 &cache_bread, &cache_bwrite, &cache_btell, &cache_bseek,
360 &cache_bclose, &cache_bflush, &cache_bstat
361};
362
252b5132
RH
363/*
364INTERNAL_FUNCTION
365 bfd_cache_init
366
367SYNOPSIS
b34976b6 368 bfd_boolean bfd_cache_init (bfd *abfd);
252b5132
RH
369
370DESCRIPTION
371 Add a newly opened BFD to the cache.
372*/
373
b34976b6 374bfd_boolean
c58b9523 375bfd_cache_init (bfd *abfd)
252b5132
RH
376{
377 BFD_ASSERT (abfd->iostream != NULL);
378 if (open_files >= BFD_CACHE_MAX_OPEN)
379 {
380 if (! close_one ())
b34976b6 381 return FALSE;
252b5132 382 }
40838a72 383 abfd->iovec = &cache_iovec;
252b5132
RH
384 insert (abfd);
385 ++open_files;
b34976b6 386 return TRUE;
252b5132
RH
387}
388
389/*
390INTERNAL_FUNCTION
391 bfd_cache_close
392
393SYNOPSIS
b34976b6 394 bfd_boolean bfd_cache_close (bfd *abfd);
252b5132
RH
395
396DESCRIPTION
397 Remove the BFD @var{abfd} from the cache. If the attached file is open,
398 then close it too.
399
400RETURNS
b34976b6 401 <<FALSE>> is returned if closing the file fails, <<TRUE>> is
252b5132
RH
402 returned if all is well.
403*/
404
b34976b6 405bfd_boolean
c58b9523 406bfd_cache_close (bfd *abfd)
252b5132 407{
40838a72 408 if (abfd->iovec != &cache_iovec)
b34976b6 409 return TRUE;
252b5132 410
fe2e161a
AC
411 if (abfd->iostream == NULL)
412 /* Previously closed. */
413 return TRUE;
414
252b5132
RH
415 return bfd_cache_delete (abfd);
416}
417
02d5a37b
JG
418/*
419FUNCTION
420 bfd_cache_close_all
421
422SYNOPSIS
423 bfd_boolean bfd_cache_close_all (void);
424
425DESCRIPTION
426 Remove all BFDs from the cache. If the attached file is open,
427 then close it too.
428
429RETURNS
430 <<FALSE>> is returned if closing one of the file fails, <<TRUE>> is
431 returned if all is well.
432*/
433
434bfd_boolean
435bfd_cache_close_all ()
436{
437 bfd_boolean ret = TRUE;
438
439 while (bfd_last_cache != NULL)
440 ret &= bfd_cache_close (bfd_last_cache);
c9b549b2
JG
441
442 return ret;
02d5a37b
JG
443}
444
252b5132
RH
445/*
446INTERNAL_FUNCTION
447 bfd_open_file
448
449SYNOPSIS
c58b9523 450 FILE* bfd_open_file (bfd *abfd);
252b5132
RH
451
452DESCRIPTION
453 Call the OS to open a file for @var{abfd}. Return the <<FILE *>>
454 (possibly <<NULL>>) that results from this operation. Set up the
455 BFD so that future accesses know the file is open. If the <<FILE *>>
456 returned is <<NULL>>, then it won't have been put in the
457 cache, so it won't have to be removed from it.
458*/
459
460FILE *
c58b9523 461bfd_open_file (bfd *abfd)
252b5132 462{
b34976b6 463 abfd->cacheable = TRUE; /* Allow it to be closed later. */
252b5132
RH
464
465 if (open_files >= BFD_CACHE_MAX_OPEN)
466 {
467 if (! close_one ())
468 return NULL;
469 }
470
471 switch (abfd->direction)
472 {
473 case read_direction:
474 case no_direction:
475 abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_RB);
476 break;
477 case both_direction:
478 case write_direction:
82e51918 479 if (abfd->opened_once)
252b5132
RH
480 {
481 abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_RUB);
482 if (abfd->iostream == NULL)
483 abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_WUB);
484 }
485 else
486 {
9e422a2e
ILT
487 /* Create the file.
488
489 Some operating systems won't let us overwrite a running
490 binary. For them, we want to unlink the file first.
491
492 However, gcc 2.95 will create temporary files using
493 O_EXCL and tight permissions to prevent other users from
494 substituting other .o files during the compilation. gcc
495 will then tell the assembler to use the newly created
496 file as an output file. If we unlink the file here, we
497 open a brief window when another user could still
498 substitute a file.
499
500 So we unlink the output file if and only if it has
501 non-zero size. */
5af11cab
AM
502#ifndef __MSDOS__
503 /* Don't do this for MSDOS: it doesn't care about overwriting
504 a running binary, but if this file is already open by
505 another BFD, we will be in deep trouble if we delete an
506 open file. In fact, objdump does just that if invoked with
507 the --info option. */
9e422a2e
ILT
508 struct stat s;
509
510 if (stat (abfd->filename, &s) == 0 && s.st_size != 0)
bb14f524 511 unlink_if_ordinary (abfd->filename);
5af11cab 512#endif
c46b7515 513 abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_WUB);
b34976b6 514 abfd->opened_once = TRUE;
252b5132
RH
515 }
516 break;
517 }
518
5c91cdfb
AM
519 if (abfd->iostream == NULL)
520 bfd_set_error (bfd_error_system_call);
521 else
252b5132
RH
522 {
523 if (! bfd_cache_init (abfd))
524 return NULL;
525 }
526
527 return (FILE *) abfd->iostream;
528}