merge from gcc
[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,
3db64b00 4 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
7c192733 5
252b5132
RH
6 Hacked by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
7
cd123cb7
NC
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 3 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,
23 MA 02110-1301, USA. */
252b5132
RH
24
25/*
26SECTION
27 File caching
28
29 The file caching mechanism is embedded within BFD and allows
30 the application to open as many BFDs as it wants without
31 regard to the underlying operating system's file descriptor
32 limit (often as low as 20 open files). The module in
33 <<cache.c>> maintains a least recently used list of
34 <<BFD_CACHE_MAX_OPEN>> files, and exports the name
35 <<bfd_cache_lookup>>, which runs around and makes sure that
36 the required BFD is open. If not, then it chooses a file to
37 close, closes it and opens the one wanted, returning its file
e60b52c6 38 handle.
252b5132 39
1b74d094
BW
40SUBSECTION
41 Caching functions
252b5132
RH
42*/
43
252b5132 44#include "sysdep.h"
3db64b00 45#include "bfd.h"
252b5132 46#include "libbfd.h"
bb14f524 47#include "libiberty.h"
252b5132 48
95560129
AM
49/* In some cases we can optimize cache operation when reopening files.
50 For instance, a flush is entirely unnecessary if the file is already
51 closed, so a flush would use CACHE_NO_OPEN. Similarly, a seek using
52 SEEK_SET or SEEK_END need not first seek to the current position.
53 For stat we ignore seek errors, just in case the file has changed
54 while we weren't looking. If it has, then it's possible that the
55 file is shorter and we don't want a seek error to prevent us doing
56 the stat. */
57enum cache_flag {
58 CACHE_NORMAL = 0,
59 CACHE_NO_OPEN = 1,
60 CACHE_NO_SEEK = 2,
61 CACHE_NO_SEEK_ERROR = 4
62};
63
d00967c7
AM
64/* The maximum number of files which the cache will keep open at
65 one time. */
d0fdd288 66
d00967c7 67#define BFD_CACHE_MAX_OPEN 10
d0fdd288
AM
68
69/* The number of BFD files we have open. */
70
71static int open_files;
72
d00967c7
AM
73/* Zero, or a pointer to the topmost BFD on the chain. This is
74 used by the <<bfd_cache_lookup>> macro in @file{libbfd.h} to
75 determine when it can avoid a function call. */
d0fdd288 76
d00967c7 77static bfd *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
d00967c7
AM
172/* Check to see if the required BFD is the same as the last one
173 looked up. If so, then it can use the stream in the BFD with
174 impunity, since it can't have changed since the last lookup;
175 otherwise, it has to perform the complicated lookup function. */
d0fdd288 176
95560129 177#define bfd_cache_lookup(x, flag) \
d00967c7
AM
178 ((x) == bfd_last_cache \
179 ? (FILE *) (bfd_last_cache->iostream) \
95560129 180 : bfd_cache_lookup_worker (x, flag))
d0fdd288 181
d00967c7
AM
182/* Called when the macro <<bfd_cache_lookup>> fails to find a
183 quick answer. Find a file descriptor for @var{abfd}. If
184 necessary, it open it. If there are already more than
185 <<BFD_CACHE_MAX_OPEN>> files open, it tries to close one first, to
186 avoid running out of file descriptors. It will return NULL
187 if it is unable to (re)open the @var{abfd}. */
d0fdd288 188
d00967c7 189static FILE *
95560129 190bfd_cache_lookup_worker (bfd *abfd, enum cache_flag flag)
d0fdd288
AM
191{
192 bfd *orig_bfd = abfd;
193 if ((abfd->flags & BFD_IN_MEMORY) != 0)
194 abort ();
195
196 if (abfd->my_archive)
197 abfd = abfd->my_archive;
198
199 if (abfd->iostream != NULL)
200 {
201 /* Move the file to the start of the cache. */
202 if (abfd != bfd_last_cache)
203 {
204 snip (abfd);
205 insert (abfd);
206 }
207 return (FILE *) abfd->iostream;
208 }
209
95560129
AM
210 if (flag & CACHE_NO_OPEN)
211 return NULL;
212
d0fdd288
AM
213 if (bfd_open_file (abfd) == NULL)
214 ;
95560129
AM
215 else if (!(flag & CACHE_NO_SEEK)
216 && real_fseek ((FILE *) abfd->iostream, abfd->where, SEEK_SET) != 0
217 && !(flag & CACHE_NO_SEEK_ERROR))
d0fdd288
AM
218 bfd_set_error (bfd_error_system_call);
219 else
220 return (FILE *) abfd->iostream;
221
222 (*_bfd_error_handler) (_("reopening %B: %s\n"),
223 orig_bfd, bfd_errmsg (bfd_get_error ()));
224 return NULL;
225}
40838a72
AC
226
227static file_ptr
228cache_btell (struct bfd *abfd)
229{
95560129 230 FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
3dff57e8 231 if (f == NULL)
95560129 232 return abfd->where;
3dff57e8 233 return real_ftell (f);
40838a72
AC
234}
235
236static int
237cache_bseek (struct bfd *abfd, file_ptr offset, int whence)
238{
95560129 239 FILE *f = bfd_cache_lookup (abfd, whence != SEEK_CUR ? CACHE_NO_SEEK : 0);
3dff57e8
AM
240 if (f == NULL)
241 return -1;
242 return real_fseek (f, offset, whence);
40838a72
AC
243}
244
245/* Note that archive entries don't have streams; they share their parent's.
246 This allows someone to play with the iostream behind BFD's back.
247
248 Also, note that the origin pointer points to the beginning of a file's
249 contents (0 for non-archive elements). For archive entries this is the
250 first octet in the file, NOT the beginning of the archive header. */
251
252static file_ptr
253cache_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
254{
3dff57e8 255 FILE *f;
40838a72
AC
256 file_ptr nread;
257 /* FIXME - this looks like an optimization, but it's really to cover
258 up for a feature of some OSs (not solaris - sigh) that
259 ld/pe-dll.c takes advantage of (apparently) when it creates BFDs
260 internally and tries to link against them. BFD seems to be smart
261 enough to realize there are no symbol records in the "file" that
262 doesn't exist but attempts to read them anyway. On Solaris,
263 attempting to read zero bytes from a NULL file results in a core
264 dump, but on other platforms it just returns zero bytes read.
265 This makes it to something reasonable. - DJ */
266 if (nbytes == 0)
267 return 0;
268
95560129 269 f = bfd_cache_lookup (abfd, 0);
3dff57e8
AM
270 if (f == NULL)
271 return 0;
272
40838a72
AC
273#if defined (__VAX) && defined (VMS)
274 /* Apparently fread on Vax VMS does not keep the record length
275 information. */
3dff57e8 276 nread = read (fileno (f), buf, nbytes);
40838a72
AC
277 /* Set bfd_error if we did not read as much data as we expected. If
278 the read failed due to an error set the bfd_error_system_call,
279 else set bfd_error_file_truncated. */
280 if (nread == (file_ptr)-1)
281 {
282 bfd_set_error (bfd_error_system_call);
283 return -1;
284 }
285#else
3dff57e8 286 nread = fread (buf, 1, nbytes, f);
40838a72
AC
287 /* Set bfd_error if we did not read as much data as we expected. If
288 the read failed due to an error set the bfd_error_system_call,
289 else set bfd_error_file_truncated. */
3dff57e8 290 if (nread < nbytes && ferror (f))
40838a72
AC
291 {
292 bfd_set_error (bfd_error_system_call);
293 return -1;
294 }
295#endif
662ed161
DJ
296 if (nread < nbytes)
297 /* This may or may not be an error, but in case the calling code
298 bails out because of it, set the right error code. */
299 bfd_set_error (bfd_error_file_truncated);
40838a72
AC
300 return nread;
301}
302
303static file_ptr
304cache_bwrite (struct bfd *abfd, const void *where, file_ptr nbytes)
305{
3dff57e8 306 file_ptr nwrite;
95560129 307 FILE *f = bfd_cache_lookup (abfd, 0);
3dff57e8
AM
308 if (f == NULL)
309 return 0;
310 nwrite = fwrite (where, 1, nbytes, f);
311 if (nwrite < nbytes && ferror (f))
40838a72
AC
312 {
313 bfd_set_error (bfd_error_system_call);
314 return -1;
315 }
316 return nwrite;
317}
318
319static int
320cache_bclose (struct bfd *abfd)
321{
322 return bfd_cache_close (abfd);
323}
324
325static int
326cache_bflush (struct bfd *abfd)
327{
3dff57e8 328 int sts;
95560129 329 FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
3dff57e8 330 if (f == NULL)
95560129 331 return 0;
3dff57e8 332 sts = fflush (f);
40838a72
AC
333 if (sts < 0)
334 bfd_set_error (bfd_error_system_call);
335 return sts;
336}
337
338static int
339cache_bstat (struct bfd *abfd, struct stat *sb)
340{
3dff57e8 341 int sts;
95560129 342 FILE *f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR);
3dff57e8
AM
343 if (f == NULL)
344 return -1;
345 sts = fstat (fileno (f), sb);
40838a72
AC
346 if (sts < 0)
347 bfd_set_error (bfd_error_system_call);
348 return sts;
349}
350
351static const struct bfd_iovec cache_iovec = {
352 &cache_bread, &cache_bwrite, &cache_btell, &cache_bseek,
353 &cache_bclose, &cache_bflush, &cache_bstat
354};
355
252b5132
RH
356/*
357INTERNAL_FUNCTION
358 bfd_cache_init
359
360SYNOPSIS
b34976b6 361 bfd_boolean bfd_cache_init (bfd *abfd);
252b5132
RH
362
363DESCRIPTION
364 Add a newly opened BFD to the cache.
365*/
366
b34976b6 367bfd_boolean
c58b9523 368bfd_cache_init (bfd *abfd)
252b5132
RH
369{
370 BFD_ASSERT (abfd->iostream != NULL);
371 if (open_files >= BFD_CACHE_MAX_OPEN)
372 {
373 if (! close_one ())
b34976b6 374 return FALSE;
252b5132 375 }
40838a72 376 abfd->iovec = &cache_iovec;
252b5132
RH
377 insert (abfd);
378 ++open_files;
b34976b6 379 return TRUE;
252b5132
RH
380}
381
382/*
383INTERNAL_FUNCTION
384 bfd_cache_close
385
386SYNOPSIS
b34976b6 387 bfd_boolean bfd_cache_close (bfd *abfd);
252b5132
RH
388
389DESCRIPTION
390 Remove the BFD @var{abfd} from the cache. If the attached file is open,
391 then close it too.
392
393RETURNS
b34976b6 394 <<FALSE>> is returned if closing the file fails, <<TRUE>> is
252b5132
RH
395 returned if all is well.
396*/
397
b34976b6 398bfd_boolean
c58b9523 399bfd_cache_close (bfd *abfd)
252b5132 400{
40838a72 401 if (abfd->iovec != &cache_iovec)
b34976b6 402 return TRUE;
252b5132 403
fe2e161a
AC
404 if (abfd->iostream == NULL)
405 /* Previously closed. */
406 return TRUE;
407
252b5132
RH
408 return bfd_cache_delete (abfd);
409}
410
02d5a37b
JG
411/*
412FUNCTION
413 bfd_cache_close_all
414
415SYNOPSIS
416 bfd_boolean bfd_cache_close_all (void);
417
418DESCRIPTION
419 Remove all BFDs from the cache. If the attached file is open,
420 then close it too.
421
422RETURNS
423 <<FALSE>> is returned if closing one of the file fails, <<TRUE>> is
424 returned if all is well.
425*/
426
427bfd_boolean
428bfd_cache_close_all ()
429{
430 bfd_boolean ret = TRUE;
431
432 while (bfd_last_cache != NULL)
433 ret &= bfd_cache_close (bfd_last_cache);
c9b549b2
JG
434
435 return ret;
02d5a37b
JG
436}
437
252b5132
RH
438/*
439INTERNAL_FUNCTION
440 bfd_open_file
441
442SYNOPSIS
c58b9523 443 FILE* bfd_open_file (bfd *abfd);
252b5132
RH
444
445DESCRIPTION
446 Call the OS to open a file for @var{abfd}. Return the <<FILE *>>
447 (possibly <<NULL>>) that results from this operation. Set up the
448 BFD so that future accesses know the file is open. If the <<FILE *>>
449 returned is <<NULL>>, then it won't have been put in the
450 cache, so it won't have to be removed from it.
451*/
452
453FILE *
c58b9523 454bfd_open_file (bfd *abfd)
252b5132 455{
b34976b6 456 abfd->cacheable = TRUE; /* Allow it to be closed later. */
252b5132
RH
457
458 if (open_files >= BFD_CACHE_MAX_OPEN)
459 {
460 if (! close_one ())
461 return NULL;
462 }
463
464 switch (abfd->direction)
465 {
466 case read_direction:
467 case no_direction:
2e6f4fae 468 abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_RB);
252b5132
RH
469 break;
470 case both_direction:
471 case write_direction:
82e51918 472 if (abfd->opened_once)
252b5132 473 {
2e6f4fae 474 abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_RUB);
252b5132 475 if (abfd->iostream == NULL)
2e6f4fae 476 abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_WUB);
252b5132
RH
477 }
478 else
479 {
9e422a2e
ILT
480 /* Create the file.
481
482 Some operating systems won't let us overwrite a running
483 binary. For them, we want to unlink the file first.
484
485 However, gcc 2.95 will create temporary files using
486 O_EXCL and tight permissions to prevent other users from
487 substituting other .o files during the compilation. gcc
488 will then tell the assembler to use the newly created
489 file as an output file. If we unlink the file here, we
490 open a brief window when another user could still
491 substitute a file.
492
493 So we unlink the output file if and only if it has
494 non-zero size. */
5af11cab
AM
495#ifndef __MSDOS__
496 /* Don't do this for MSDOS: it doesn't care about overwriting
497 a running binary, but if this file is already open by
498 another BFD, we will be in deep trouble if we delete an
499 open file. In fact, objdump does just that if invoked with
500 the --info option. */
9e422a2e
ILT
501 struct stat s;
502
503 if (stat (abfd->filename, &s) == 0 && s.st_size != 0)
bb14f524 504 unlink_if_ordinary (abfd->filename);
5af11cab 505#endif
2e6f4fae 506 abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_WUB);
b34976b6 507 abfd->opened_once = TRUE;
252b5132
RH
508 }
509 break;
510 }
511
5c91cdfb
AM
512 if (abfd->iostream == NULL)
513 bfd_set_error (bfd_error_system_call);
514 else
252b5132
RH
515 {
516 if (! bfd_cache_init (abfd))
517 return NULL;
518 }
519
520 return (FILE *) abfd->iostream;
521}
This page took 0.371691 seconds and 4 git commands to generate.