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