bfd/
[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,
aa820537 4 2003, 2004, 2005, 2007, 2008, 2009 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
9d782e8d 34 <<bfd_cache_max_open>> files, and exports the name
252b5132
RH
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"
4c95ab76 48#include "bfd_stdint.h"
252b5132 49
25b88f33
PP
50#ifdef HAVE_MMAP
51#include <sys/mman.h>
52#endif
53
95560129
AM
54/* In some cases we can optimize cache operation when reopening files.
55 For instance, a flush is entirely unnecessary if the file is already
56 closed, so a flush would use CACHE_NO_OPEN. Similarly, a seek using
57 SEEK_SET or SEEK_END need not first seek to the current position.
58 For stat we ignore seek errors, just in case the file has changed
59 while we weren't looking. If it has, then it's possible that the
60 file is shorter and we don't want a seek error to prevent us doing
61 the stat. */
62enum cache_flag {
63 CACHE_NORMAL = 0,
64 CACHE_NO_OPEN = 1,
65 CACHE_NO_SEEK = 2,
66 CACHE_NO_SEEK_ERROR = 4
67};
68
d00967c7 69/* The maximum number of files which the cache will keep open at
9d782e8d 70 one time. When needed call bfd_cache_max_open to initialize. */
d0fdd288 71
9d782e8d
MW
72static int max_open_files = 0;
73
74/* Set max_open_files, if not already set, to 12.5% of the allowed open
75 file descriptors, but at least 10, and return the value. */
76static int
77bfd_cache_max_open (void)
78{
79 if (max_open_files == 0)
80 {
81 int max;
82#ifdef HAVE_GETRLIMIT
83 struct rlimit rlim;
84 if (getrlimit (RLIMIT_NOFILE, &rlim) == 0
85 && rlim.rlim_cur != RLIM_INFINITY)
86 max = rlim.rlim_cur / 8;
87 else
88#endif /* HAVE_GETRLIMIT */
89#ifdef _SC_OPEN_MAX
90 max = sysconf (_SC_OPEN_MAX) / 8;
91#else
92 max = 10;
93#endif /* _SC_OPEN_MAX */
94 max_open_files = max < 10 ? 10 : max;
95 }
96
97 return max_open_files;
98}
d0fdd288
AM
99
100/* The number of BFD files we have open. */
101
102static int open_files;
103
d00967c7
AM
104/* Zero, or a pointer to the topmost BFD on the chain. This is
105 used by the <<bfd_cache_lookup>> macro in @file{libbfd.h} to
106 determine when it can avoid a function call. */
d0fdd288 107
d00967c7 108static bfd *bfd_last_cache = NULL;
252b5132 109
d0fdd288
AM
110/* Insert a BFD into the cache. */
111
112static void
113insert (bfd *abfd)
114{
115 if (bfd_last_cache == NULL)
116 {
117 abfd->lru_next = abfd;
118 abfd->lru_prev = abfd;
119 }
120 else
121 {
122 abfd->lru_next = bfd_last_cache;
123 abfd->lru_prev = bfd_last_cache->lru_prev;
124 abfd->lru_prev->lru_next = abfd;
125 abfd->lru_next->lru_prev = abfd;
126 }
127 bfd_last_cache = abfd;
128}
129
130/* Remove a BFD from the cache. */
131
132static void
133snip (bfd *abfd)
134{
135 abfd->lru_prev->lru_next = abfd->lru_next;
136 abfd->lru_next->lru_prev = abfd->lru_prev;
137 if (abfd == bfd_last_cache)
138 {
139 bfd_last_cache = abfd->lru_next;
140 if (abfd == bfd_last_cache)
141 bfd_last_cache = NULL;
142 }
143}
144
145/* Close a BFD and remove it from the cache. */
146
147static bfd_boolean
148bfd_cache_delete (bfd *abfd)
149{
150 bfd_boolean ret;
151
152 if (fclose ((FILE *) abfd->iostream) == 0)
153 ret = TRUE;
154 else
155 {
156 ret = FALSE;
157 bfd_set_error (bfd_error_system_call);
158 }
159
160 snip (abfd);
161
162 abfd->iostream = NULL;
163 --open_files;
164
165 return ret;
166}
167
168/* We need to open a new file, and the cache is full. Find the least
169 recently used cacheable BFD and close it. */
170
171static bfd_boolean
172close_one (void)
173{
f664f618 174 register bfd *to_kill;
d0fdd288
AM
175
176 if (bfd_last_cache == NULL)
f664f618 177 to_kill = NULL;
d0fdd288
AM
178 else
179 {
f664f618
TG
180 for (to_kill = bfd_last_cache->lru_prev;
181 ! to_kill->cacheable;
182 to_kill = to_kill->lru_prev)
d0fdd288 183 {
f664f618 184 if (to_kill == bfd_last_cache)
d0fdd288 185 {
f664f618 186 to_kill = NULL;
d0fdd288
AM
187 break;
188 }
189 }
190 }
191
f664f618 192 if (to_kill == NULL)
d0fdd288
AM
193 {
194 /* There are no open cacheable BFD's. */
195 return TRUE;
196 }
197
f664f618 198 to_kill->where = real_ftell ((FILE *) to_kill->iostream);
d0fdd288 199
f664f618 200 return bfd_cache_delete (to_kill);
d0fdd288
AM
201}
202
d00967c7
AM
203/* Check to see if the required BFD is the same as the last one
204 looked up. If so, then it can use the stream in the BFD with
205 impunity, since it can't have changed since the last lookup;
206 otherwise, it has to perform the complicated lookup function. */
d0fdd288 207
95560129 208#define bfd_cache_lookup(x, flag) \
d00967c7
AM
209 ((x) == bfd_last_cache \
210 ? (FILE *) (bfd_last_cache->iostream) \
95560129 211 : bfd_cache_lookup_worker (x, flag))
d0fdd288 212
d00967c7
AM
213/* Called when the macro <<bfd_cache_lookup>> fails to find a
214 quick answer. Find a file descriptor for @var{abfd}. If
215 necessary, it open it. If there are already more than
9d782e8d 216 <<bfd_cache_max_open>> files open, it tries to close one first, to
d00967c7
AM
217 avoid running out of file descriptors. It will return NULL
218 if it is unable to (re)open the @var{abfd}. */
d0fdd288 219
d00967c7 220static FILE *
95560129 221bfd_cache_lookup_worker (bfd *abfd, enum cache_flag flag)
d0fdd288
AM
222{
223 bfd *orig_bfd = abfd;
224 if ((abfd->flags & BFD_IN_MEMORY) != 0)
225 abort ();
226
660722b0 227 while (abfd->my_archive)
d0fdd288
AM
228 abfd = abfd->my_archive;
229
230 if (abfd->iostream != NULL)
231 {
232 /* Move the file to the start of the cache. */
233 if (abfd != bfd_last_cache)
234 {
235 snip (abfd);
236 insert (abfd);
237 }
238 return (FILE *) abfd->iostream;
239 }
240
95560129
AM
241 if (flag & CACHE_NO_OPEN)
242 return NULL;
243
d0fdd288
AM
244 if (bfd_open_file (abfd) == NULL)
245 ;
95560129
AM
246 else if (!(flag & CACHE_NO_SEEK)
247 && real_fseek ((FILE *) abfd->iostream, abfd->where, SEEK_SET) != 0
248 && !(flag & CACHE_NO_SEEK_ERROR))
d0fdd288
AM
249 bfd_set_error (bfd_error_system_call);
250 else
251 return (FILE *) abfd->iostream;
252
253 (*_bfd_error_handler) (_("reopening %B: %s\n"),
254 orig_bfd, bfd_errmsg (bfd_get_error ()));
255 return NULL;
256}
40838a72
AC
257
258static file_ptr
259cache_btell (struct bfd *abfd)
260{
95560129 261 FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
3dff57e8 262 if (f == NULL)
95560129 263 return abfd->where;
3dff57e8 264 return real_ftell (f);
40838a72
AC
265}
266
267static int
268cache_bseek (struct bfd *abfd, file_ptr offset, int whence)
269{
a50b1753 270 FILE *f = bfd_cache_lookup (abfd, whence != SEEK_CUR ? CACHE_NO_SEEK : CACHE_NORMAL);
3dff57e8
AM
271 if (f == NULL)
272 return -1;
273 return real_fseek (f, offset, whence);
40838a72
AC
274}
275
276/* Note that archive entries don't have streams; they share their parent's.
277 This allows someone to play with the iostream behind BFD's back.
278
279 Also, note that the origin pointer points to the beginning of a file's
280 contents (0 for non-archive elements). For archive entries this is the
281 first octet in the file, NOT the beginning of the archive header. */
282
283static file_ptr
f12a02c0 284cache_bread_1 (struct bfd *abfd, void *buf, file_ptr nbytes)
40838a72 285{
3dff57e8 286 FILE *f;
40838a72
AC
287 file_ptr nread;
288 /* FIXME - this looks like an optimization, but it's really to cover
289 up for a feature of some OSs (not solaris - sigh) that
290 ld/pe-dll.c takes advantage of (apparently) when it creates BFDs
291 internally and tries to link against them. BFD seems to be smart
292 enough to realize there are no symbol records in the "file" that
293 doesn't exist but attempts to read them anyway. On Solaris,
294 attempting to read zero bytes from a NULL file results in a core
295 dump, but on other platforms it just returns zero bytes read.
296 This makes it to something reasonable. - DJ */
297 if (nbytes == 0)
298 return 0;
299
a50b1753 300 f = bfd_cache_lookup (abfd, CACHE_NORMAL);
3dff57e8
AM
301 if (f == NULL)
302 return 0;
303
40838a72
AC
304#if defined (__VAX) && defined (VMS)
305 /* Apparently fread on Vax VMS does not keep the record length
306 information. */
3dff57e8 307 nread = read (fileno (f), buf, nbytes);
40838a72
AC
308 /* Set bfd_error if we did not read as much data as we expected. If
309 the read failed due to an error set the bfd_error_system_call,
310 else set bfd_error_file_truncated. */
311 if (nread == (file_ptr)-1)
312 {
313 bfd_set_error (bfd_error_system_call);
314 return -1;
315 }
316#else
3dff57e8 317 nread = fread (buf, 1, nbytes, f);
40838a72
AC
318 /* Set bfd_error if we did not read as much data as we expected. If
319 the read failed due to an error set the bfd_error_system_call,
320 else set bfd_error_file_truncated. */
3dff57e8 321 if (nread < nbytes && ferror (f))
40838a72
AC
322 {
323 bfd_set_error (bfd_error_system_call);
324 return -1;
325 }
326#endif
662ed161
DJ
327 if (nread < nbytes)
328 /* This may or may not be an error, but in case the calling code
329 bails out because of it, set the right error code. */
330 bfd_set_error (bfd_error_file_truncated);
40838a72
AC
331 return nread;
332}
333
f12a02c0
JB
334static file_ptr
335cache_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
336{
337 file_ptr nread = 0;
338
339 /* Some filesystems are unable to handle reads that are too large
340 (for instance, NetApp shares with oplocks turned off). To avoid
341 hitting this limitation, we read the buffer in chunks of 8MB max. */
342 while (nread < nbytes)
343 {
344 const file_ptr max_chunk_size = 0x800000;
345 file_ptr chunk_size = nbytes - nread;
346 file_ptr chunk_nread;
347
348 if (chunk_size > max_chunk_size)
349 chunk_size = max_chunk_size;
350
cb5220a0 351 chunk_nread = cache_bread_1 (abfd, (char *) buf + nread, chunk_size);
f12a02c0
JB
352
353 /* Update the nread count.
354
355 We just have to be careful of the case when cache_bread_1 returns
356 a negative count: If this is our first read, then set nread to
357 that negative count in order to return that negative value to the
358 caller. Otherwise, don't add it to our total count, or we would
359 end up returning a smaller number of bytes read than we actually
360 did. */
361 if (nread == 0 || chunk_nread > 0)
362 nread += chunk_nread;
363
364 if (chunk_nread < chunk_size)
365 break;
366 }
367
368 return nread;
369}
370
40838a72
AC
371static file_ptr
372cache_bwrite (struct bfd *abfd, const void *where, file_ptr nbytes)
373{
3dff57e8 374 file_ptr nwrite;
a50b1753 375 FILE *f = bfd_cache_lookup (abfd, CACHE_NORMAL);
cb5220a0 376
3dff57e8
AM
377 if (f == NULL)
378 return 0;
379 nwrite = fwrite (where, 1, nbytes, f);
380 if (nwrite < nbytes && ferror (f))
40838a72
AC
381 {
382 bfd_set_error (bfd_error_system_call);
383 return -1;
384 }
385 return nwrite;
386}
387
405bf443 388static int
40838a72
AC
389cache_bclose (struct bfd *abfd)
390{
405bf443 391 return bfd_cache_close (abfd) - 1;
40838a72
AC
392}
393
394static int
395cache_bflush (struct bfd *abfd)
396{
3dff57e8 397 int sts;
95560129 398 FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
cb5220a0 399
3dff57e8 400 if (f == NULL)
95560129 401 return 0;
3dff57e8 402 sts = fflush (f);
40838a72
AC
403 if (sts < 0)
404 bfd_set_error (bfd_error_system_call);
405 return sts;
406}
407
408static int
409cache_bstat (struct bfd *abfd, struct stat *sb)
410{
3dff57e8 411 int sts;
95560129 412 FILE *f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR);
cb5220a0 413
3dff57e8
AM
414 if (f == NULL)
415 return -1;
416 sts = fstat (fileno (f), sb);
40838a72
AC
417 if (sts < 0)
418 bfd_set_error (bfd_error_system_call);
419 return sts;
420}
421
25b88f33
PP
422static void *
423cache_bmmap (struct bfd *abfd ATTRIBUTE_UNUSED,
424 void *addr ATTRIBUTE_UNUSED,
425 bfd_size_type len ATTRIBUTE_UNUSED,
426 int prot ATTRIBUTE_UNUSED,
427 int flags ATTRIBUTE_UNUSED,
4c95ab76
TG
428 file_ptr offset ATTRIBUTE_UNUSED,
429 void **map_addr ATTRIBUTE_UNUSED,
430 bfd_size_type *map_len ATTRIBUTE_UNUSED)
25b88f33
PP
431{
432 void *ret = (void *) -1;
433
434 if ((abfd->flags & BFD_IN_MEMORY) != 0)
435 abort ();
436#ifdef HAVE_MMAP
437 else
438 {
4c95ab76
TG
439 static uintptr_t pagesize_m1;
440 FILE *f;
441 file_ptr pg_offset;
442 bfd_size_type pg_len;
443
444 f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR);
25b88f33
PP
445 if (f == NULL)
446 return ret;
447
4c95ab76
TG
448 if (pagesize_m1 == 0)
449 pagesize_m1 = getpagesize () - 1;
450
451 /* Handle archive members. */
452 if (abfd->my_archive != NULL)
453 offset += abfd->origin;
454
455 /* Align. */
456 pg_offset = offset & ~pagesize_m1;
457 pg_len = (len + (offset - pg_offset) + pagesize_m1) & ~pagesize_m1;
458
459 ret = mmap (addr, pg_len, prot, flags, fileno (f), pg_offset);
25b88f33
PP
460 if (ret == (void *) -1)
461 bfd_set_error (bfd_error_system_call);
4c95ab76
TG
462 else
463 {
464 *map_addr = ret;
465 *map_len = pg_len;
eac3aa9e 466 ret = (char *) ret + (offset & pagesize_m1);
4c95ab76 467 }
25b88f33
PP
468 }
469#endif
470
471 return ret;
472}
473
cb5220a0
NC
474static const struct bfd_iovec cache_iovec =
475{
40838a72 476 &cache_bread, &cache_bwrite, &cache_btell, &cache_bseek,
25b88f33 477 &cache_bclose, &cache_bflush, &cache_bstat, &cache_bmmap
40838a72
AC
478};
479
252b5132
RH
480/*
481INTERNAL_FUNCTION
482 bfd_cache_init
483
484SYNOPSIS
b34976b6 485 bfd_boolean bfd_cache_init (bfd *abfd);
252b5132
RH
486
487DESCRIPTION
488 Add a newly opened BFD to the cache.
489*/
490
b34976b6 491bfd_boolean
c58b9523 492bfd_cache_init (bfd *abfd)
252b5132
RH
493{
494 BFD_ASSERT (abfd->iostream != NULL);
9d782e8d 495 if (open_files >= bfd_cache_max_open ())
252b5132
RH
496 {
497 if (! close_one ())
b34976b6 498 return FALSE;
252b5132 499 }
40838a72 500 abfd->iovec = &cache_iovec;
252b5132
RH
501 insert (abfd);
502 ++open_files;
b34976b6 503 return TRUE;
252b5132
RH
504}
505
506/*
507INTERNAL_FUNCTION
508 bfd_cache_close
509
510SYNOPSIS
b34976b6 511 bfd_boolean bfd_cache_close (bfd *abfd);
252b5132
RH
512
513DESCRIPTION
514 Remove the BFD @var{abfd} from the cache. If the attached file is open,
515 then close it too.
516
517RETURNS
b34976b6 518 <<FALSE>> is returned if closing the file fails, <<TRUE>> is
252b5132
RH
519 returned if all is well.
520*/
521
b34976b6 522bfd_boolean
c58b9523 523bfd_cache_close (bfd *abfd)
252b5132 524{
40838a72 525 if (abfd->iovec != &cache_iovec)
b34976b6 526 return TRUE;
252b5132 527
fe2e161a
AC
528 if (abfd->iostream == NULL)
529 /* Previously closed. */
530 return TRUE;
531
252b5132
RH
532 return bfd_cache_delete (abfd);
533}
534
02d5a37b
JG
535/*
536FUNCTION
537 bfd_cache_close_all
538
539SYNOPSIS
540 bfd_boolean bfd_cache_close_all (void);
541
542DESCRIPTION
543 Remove all BFDs from the cache. If the attached file is open,
544 then close it too.
545
546RETURNS
547 <<FALSE>> is returned if closing one of the file fails, <<TRUE>> is
548 returned if all is well.
549*/
550
551bfd_boolean
552bfd_cache_close_all ()
553{
554 bfd_boolean ret = TRUE;
555
556 while (bfd_last_cache != NULL)
557 ret &= bfd_cache_close (bfd_last_cache);
c9b549b2
JG
558
559 return ret;
02d5a37b
JG
560}
561
252b5132
RH
562/*
563INTERNAL_FUNCTION
564 bfd_open_file
565
566SYNOPSIS
c58b9523 567 FILE* bfd_open_file (bfd *abfd);
252b5132
RH
568
569DESCRIPTION
570 Call the OS to open a file for @var{abfd}. Return the <<FILE *>>
571 (possibly <<NULL>>) that results from this operation. Set up the
572 BFD so that future accesses know the file is open. If the <<FILE *>>
573 returned is <<NULL>>, then it won't have been put in the
574 cache, so it won't have to be removed from it.
575*/
576
577FILE *
c58b9523 578bfd_open_file (bfd *abfd)
252b5132 579{
b34976b6 580 abfd->cacheable = TRUE; /* Allow it to be closed later. */
252b5132 581
9d782e8d 582 if (open_files >= bfd_cache_max_open ())
252b5132
RH
583 {
584 if (! close_one ())
585 return NULL;
586 }
587
588 switch (abfd->direction)
589 {
590 case read_direction:
591 case no_direction:
2c3fc389 592 abfd->iostream = real_fopen (abfd->filename, FOPEN_RB);
252b5132
RH
593 break;
594 case both_direction:
595 case write_direction:
82e51918 596 if (abfd->opened_once)
252b5132 597 {
2c3fc389 598 abfd->iostream = real_fopen (abfd->filename, FOPEN_RUB);
252b5132 599 if (abfd->iostream == NULL)
2c3fc389 600 abfd->iostream = real_fopen (abfd->filename, FOPEN_WUB);
252b5132
RH
601 }
602 else
603 {
9e422a2e
ILT
604 /* Create the file.
605
606 Some operating systems won't let us overwrite a running
607 binary. For them, we want to unlink the file first.
608
609 However, gcc 2.95 will create temporary files using
610 O_EXCL and tight permissions to prevent other users from
611 substituting other .o files during the compilation. gcc
612 will then tell the assembler to use the newly created
613 file as an output file. If we unlink the file here, we
614 open a brief window when another user could still
615 substitute a file.
616
617 So we unlink the output file if and only if it has
618 non-zero size. */
5af11cab
AM
619#ifndef __MSDOS__
620 /* Don't do this for MSDOS: it doesn't care about overwriting
621 a running binary, but if this file is already open by
622 another BFD, we will be in deep trouble if we delete an
623 open file. In fact, objdump does just that if invoked with
624 the --info option. */
9e422a2e
ILT
625 struct stat s;
626
627 if (stat (abfd->filename, &s) == 0 && s.st_size != 0)
bb14f524 628 unlink_if_ordinary (abfd->filename);
5af11cab 629#endif
2c3fc389 630 abfd->iostream = real_fopen (abfd->filename, FOPEN_WUB);
b34976b6 631 abfd->opened_once = TRUE;
252b5132
RH
632 }
633 break;
634 }
635
5c91cdfb
AM
636 if (abfd->iostream == NULL)
637 bfd_set_error (bfd_error_system_call);
638 else
252b5132
RH
639 {
640 if (! bfd_cache_init (abfd))
641 return NULL;
642 }
643
644 return (FILE *) abfd->iostream;
645}
This page took 0.646195 seconds and 4 git commands to generate.