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