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