Coverage Report

Created: 2025-06-24 06:45

/src/binutils-gdb/bfd/cache.c
Line
Count
Source (jump to first uncovered line)
1
/* BFD library -- caching of file descriptors.
2
3
   Copyright (C) 1990-2025 Free Software Foundation, Inc.
4
5
   Hacked by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
6
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.  */
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 "sysdep.h"
44
#include "bfd.h"
45
#include "libbfd.h"
46
#include "libiberty.h"
47
48
static FILE *_bfd_open_file_unlocked (bfd *abfd);
49
50
/* In some cases we can optimize cache operation when reopening files.
51
   For instance, a flush is entirely unnecessary if the file is already
52
   closed, so a flush would use CACHE_NO_OPEN.  Similarly, a seek using
53
   SEEK_SET or SEEK_END need not first seek to the current position.
54
   For stat we ignore seek errors, just in case the file has changed
55
   while we weren't looking.  If it has, then it's possible that the
56
   file is shorter and we don't want a seek error to prevent us doing
57
   the stat.  */
58
enum cache_flag {
59
  CACHE_NORMAL = 0,
60
  CACHE_NO_OPEN = 1,
61
  CACHE_NO_SEEK = 2,
62
  CACHE_NO_SEEK_ERROR = 4
63
};
64
65
/* The maximum number of files which the cache will keep open at
66
   one time.  When needed call bfd_cache_max_open to initialize.  */
67
68
static unsigned max_open_files = 0;
69
70
/* Set max_open_files, if not already set, to 12.5% of the allowed open
71
   file descriptors, but at least 10, and return the value.  */
72
static unsigned
73
bfd_cache_max_open (void)
74
200k
{
75
200k
  if (max_open_files == 0)
76
17
    {
77
17
      int max;
78
#if defined(__sun) && !defined(__sparcv9) && !defined(__x86_64__)
79
      /* PR ld/19260: 32-bit Solaris has very inelegant handling of the 255
80
   file descriptor limit.  The problem is that setrlimit(2) can raise
81
   RLIMIT_NOFILE to a value that is not supported by libc, resulting
82
   in "Too many open files" errors.  This can happen here even though
83
   max_open_files is set to rlim.rlim_cur / 8.  For example, if
84
   a parent process has set rlim.rlim_cur to 65536, then max_open_files
85
   will be computed as 8192.
86
87
   This check essentially reverts to the behavior from binutils 2.23.1
88
   for 32-bit Solaris only.  (It is hoped that the 32-bit libc
89
   limitation will be removed soon).  64-bit Solaris libc does not have
90
   this limitation.  */
91
      max = 16;
92
#else
93
17
#ifdef HAVE_GETRLIMIT
94
17
      struct rlimit rlim;
95
96
17
      if (getrlimit (RLIMIT_NOFILE, &rlim) == 0
97
17
    && rlim.rlim_cur != (rlim_t) RLIM_INFINITY)
98
17
  max = rlim.rlim_cur / 8;
99
0
      else
100
0
#endif
101
0
#ifdef _SC_OPEN_MAX
102
0
  max = sysconf (_SC_OPEN_MAX) / 8;
103
#else
104
  max = 10;
105
#endif
106
17
#endif /* not 32-bit Solaris */
107
108
17
      max_open_files = max < 10 ? 10 : max;
109
17
    }
110
111
200k
  return max_open_files;
112
200k
}
113
114
/* The number of BFD files we have open.  */
115
116
static unsigned open_files;
117
118
/* Zero, or a pointer to the topmost BFD on the chain.  This is
119
   used by the <<bfd_cache_lookup>> macro in @file{libbfd.h} to
120
   determine when it can avoid a function call.  */
121
122
static bfd *bfd_last_cache = NULL;
123
124
/* Insert a BFD into the cache.  */
125
126
static void
127
insert (bfd *abfd)
128
507k
{
129
507k
  if (bfd_last_cache == NULL)
130
485k
    {
131
485k
      abfd->lru_next = abfd;
132
485k
      abfd->lru_prev = abfd;
133
485k
    }
134
22.5k
  else
135
22.5k
    {
136
22.5k
      abfd->lru_next = bfd_last_cache;
137
22.5k
      abfd->lru_prev = bfd_last_cache->lru_prev;
138
22.5k
      abfd->lru_prev->lru_next = abfd;
139
22.5k
      abfd->lru_next->lru_prev = abfd;
140
22.5k
    }
141
507k
  bfd_last_cache = abfd;
142
507k
}
143
144
/* Remove a BFD from the cache.  */
145
146
static void
147
snip (bfd *abfd)
148
507k
{
149
507k
  abfd->lru_prev->lru_next = abfd->lru_next;
150
507k
  abfd->lru_next->lru_prev = abfd->lru_prev;
151
507k
  if (abfd == bfd_last_cache)
152
491k
    {
153
491k
      bfd_last_cache = abfd->lru_next;
154
491k
      if (abfd == bfd_last_cache)
155
485k
  bfd_last_cache = NULL;
156
491k
    }
157
507k
}
158
159
/* Close a BFD and remove it from the cache.  */
160
161
static bool
162
bfd_cache_delete (bfd *abfd)
163
195k
{
164
195k
  bool ret;
165
166
195k
  if (fclose ((FILE *) abfd->iostream) == 0)
167
195k
    ret = true;
168
0
  else
169
0
    {
170
0
      ret = false;
171
0
      bfd_set_error (bfd_error_system_call);
172
0
    }
173
174
195k
  snip (abfd);
175
176
195k
  abfd->iostream = NULL;
177
195k
  BFD_ASSERT (open_files > 0);
178
195k
  --open_files;
179
195k
  abfd->flags |= BFD_CLOSED_BY_CACHE;
180
181
195k
  return ret;
182
195k
}
183
184
/* We need to open a new file, and the cache is full.  Find the least
185
   recently used cacheable BFD and close it.  */
186
187
static bool
188
close_one (void)
189
0
{
190
0
  register bfd *to_kill;
191
192
0
  if (bfd_last_cache == NULL)
193
0
    to_kill = NULL;
194
0
  else
195
0
    {
196
0
      for (to_kill = bfd_last_cache->lru_prev;
197
0
     ! to_kill->cacheable;
198
0
     to_kill = to_kill->lru_prev)
199
0
  {
200
0
    if (to_kill == bfd_last_cache)
201
0
      {
202
0
        to_kill = NULL;
203
0
        break;
204
0
      }
205
0
  }
206
0
    }
207
208
0
  if (to_kill == NULL)
209
0
    {
210
      /* There are no open cacheable BFD's.  */
211
0
      return true;
212
0
    }
213
214
0
  to_kill->where = _bfd_real_ftell ((FILE *) to_kill->iostream);
215
216
0
  return bfd_cache_delete (to_kill);
217
0
}
218
219
/* Check to see if the required BFD is the same as the last one
220
   looked up. If so, then it can use the stream in the BFD with
221
   impunity, since it can't have changed since the last lookup;
222
   otherwise, it has to perform the complicated lookup function.  */
223
224
#define bfd_cache_lookup(x, flag) \
225
2.16G
  ((x) == bfd_last_cache      \
226
2.16G
   ? (FILE *) (bfd_last_cache->iostream)  \
227
2.16G
   : bfd_cache_lookup_worker (x, flag))
228
229
/* A helper function that returns true if ABFD can possibly be cached
230
   -- that is, whether bfd_cache_lookup_worker will accept it.  */
231
232
static bool
233
possibly_cached (bfd *abfd)
234
2.14G
{
235
2.14G
  if ((abfd->flags & BFD_IN_MEMORY) != 0)
236
0
    return false;
237
2.14G
  if (abfd->my_archive != NULL
238
2.14G
      && !bfd_is_thin_archive (abfd->my_archive))
239
3.32M
    return false;
240
2.14G
  return true;
241
2.14G
}
242
243
/* Called when the macro <<bfd_cache_lookup>> fails to find a
244
   quick answer.  Find a file descriptor for @var{abfd}.  If
245
   necessary, it open it.  If there are already more than
246
   <<bfd_cache_max_open>> files open, it tries to close one first, to
247
   avoid running out of file descriptors.  It will return NULL
248
   if it is unable to (re)open the @var{abfd}.  */
249
250
static FILE *
251
bfd_cache_lookup_worker (bfd *abfd, enum cache_flag flag)
252
2.14G
{
253
2.14G
  if (!possibly_cached (abfd))
254
0
    abort ();
255
256
  /* If the BFD is being processed by bfd_check_format_matches, it
257
     must already be open and won't be on the list.  */
258
2.14G
  if (abfd->in_format_matches)
259
2.14G
    {
260
2.14G
      if (abfd->iostream == NULL)
261
0
  abort ();
262
2.14G
      return (FILE *) abfd->iostream;
263
2.14G
    }
264
265
12.1k
  if (abfd->iostream != NULL)
266
12.1k
    {
267
      /* Move the file to the start of the cache.  */
268
12.1k
      if (abfd != bfd_last_cache)
269
12.1k
  {
270
12.1k
    snip (abfd);
271
12.1k
    insert (abfd);
272
12.1k
  }
273
12.1k
      return (FILE *) abfd->iostream;
274
12.1k
    }
275
276
0
  if (flag & CACHE_NO_OPEN)
277
0
    return NULL;
278
279
0
  if (_bfd_open_file_unlocked (abfd) == NULL)
280
0
    ;
281
0
  else if (!(flag & CACHE_NO_SEEK)
282
0
     && _bfd_real_fseek ((FILE *) abfd->iostream,
283
0
             abfd->where, SEEK_SET) != 0
284
0
     && !(flag & CACHE_NO_SEEK_ERROR))
285
0
    bfd_set_error (bfd_error_system_call);
286
0
  else
287
0
    return (FILE *) abfd->iostream;
288
289
  /* xgettext:c-format */
290
0
  _bfd_error_handler (_("reopening %pB: %s"),
291
0
          abfd, bfd_errmsg (bfd_get_error ()));
292
0
  return NULL;
293
0
}
294
295
static file_ptr
296
cache_btell (struct bfd *abfd)
297
9.86M
{
298
9.86M
  if (!bfd_lock ())
299
0
    return -1;
300
9.86M
  FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
301
9.86M
  if (f == NULL)
302
0
    {
303
0
      if (!bfd_unlock ())
304
0
  return -1;
305
0
      return abfd->where;
306
0
    }
307
9.86M
  file_ptr result = _bfd_real_ftell (f);
308
9.86M
  if (!bfd_unlock ())
309
0
    return -1;
310
9.86M
  return result;
311
9.86M
}
312
313
static int
314
cache_bseek (struct bfd *abfd, file_ptr offset, int whence)
315
992M
{
316
992M
  if (!bfd_lock ())
317
0
    return -1;
318
992M
  FILE *f = bfd_cache_lookup (abfd, whence != SEEK_CUR ? CACHE_NO_SEEK : CACHE_NORMAL);
319
992M
  if (f == NULL)
320
0
    {
321
0
      bfd_unlock ();
322
0
      return -1;
323
0
    }
324
992M
  int result = _bfd_real_fseek (f, offset, whence);
325
992M
  if (!bfd_unlock ())
326
0
    return -1;
327
992M
  return result;
328
992M
}
329
330
/* Note that archive entries don't have streams; they share their parent's.
331
   This allows someone to play with the iostream behind BFD's back.
332
333
   Also, note that the origin pointer points to the beginning of a file's
334
   contents (0 for non-archive elements).  For archive entries this is the
335
   first octet in the file, NOT the beginning of the archive header.  */
336
337
static file_ptr
338
cache_bread_1 (FILE *f, void *buf, file_ptr nbytes)
339
1.10G
{
340
1.10G
  file_ptr nread;
341
342
#if defined (__VAX) && defined (VMS)
343
  /* Apparently fread on Vax VMS does not keep the record length
344
     information.  */
345
  nread = read (fileno (f), buf, nbytes);
346
  /* Set bfd_error if we did not read as much data as we expected.  If
347
     the read failed due to an error set the bfd_error_system_call,
348
     else set bfd_error_file_truncated.  */
349
  if (nread == (file_ptr)-1)
350
    {
351
      bfd_set_error (bfd_error_system_call);
352
      return nread;
353
    }
354
#else
355
1.10G
  nread = fread (buf, 1, nbytes, f);
356
  /* Set bfd_error if we did not read as much data as we expected.  If
357
     the read failed due to an error set the bfd_error_system_call,
358
     else set bfd_error_file_truncated.  */
359
1.10G
  if (nread < nbytes && ferror (f))
360
249k
    {
361
249k
      bfd_set_error (bfd_error_system_call);
362
249k
      return nread;
363
249k
    }
364
1.10G
#endif
365
1.10G
  if (nread < nbytes)
366
    /* This may or may not be an error, but in case the calling code
367
       bails out because of it, set the right error code.  */
368
78.9M
    bfd_set_error (bfd_error_file_truncated);
369
1.10G
  return nread;
370
1.10G
}
371
372
static file_ptr
373
cache_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
374
1.10G
{
375
1.10G
  if (!bfd_lock ())
376
0
    return -1;
377
1.10G
  file_ptr nread = 0;
378
1.10G
  FILE *f;
379
380
1.10G
  f = bfd_cache_lookup (abfd, CACHE_NORMAL);
381
1.10G
  if (f == NULL)
382
0
    {
383
0
      bfd_unlock ();
384
0
      return -1;
385
0
    }
386
387
  /* Some filesystems are unable to handle reads that are too large
388
     (for instance, NetApp shares with oplocks turned off).  To avoid
389
     hitting this limitation, we read the buffer in chunks of 8MB max.  */
390
2.13G
  while (nread < nbytes)
391
1.10G
    {
392
1.10G
      const file_ptr max_chunk_size = 0x800000;
393
1.10G
      file_ptr chunk_size = nbytes - nread;
394
1.10G
      file_ptr chunk_nread;
395
396
1.10G
      if (chunk_size > max_chunk_size)
397
148
  chunk_size = max_chunk_size;
398
399
1.10G
      chunk_nread = cache_bread_1 (f, (char *) buf + nread, chunk_size);
400
401
      /* Update the nread count.
402
403
   We just have to be careful of the case when cache_bread_1 returns
404
   a negative count:  If this is our first read, then set nread to
405
   that negative count in order to return that negative value to the
406
   caller.  Otherwise, don't add it to our total count, or we would
407
   end up returning a smaller number of bytes read than we actually
408
   did.  */
409
1.10G
      if (nread == 0 || chunk_nread > 0)
410
1.10G
  nread += chunk_nread;
411
412
1.10G
      if (chunk_nread < chunk_size)
413
79.2M
  break;
414
1.10G
    }
415
416
1.10G
  if (!bfd_unlock ())
417
0
    return -1;
418
1.10G
  return nread;
419
1.10G
}
420
421
static file_ptr
422
cache_bwrite (struct bfd *abfd, const void *from, file_ptr nbytes)
423
85.7k
{
424
85.7k
  if (!bfd_lock ())
425
0
    return -1;
426
85.7k
  file_ptr nwrite;
427
85.7k
  FILE *f = bfd_cache_lookup (abfd, CACHE_NORMAL);
428
429
85.7k
  if (f == NULL)
430
0
    {
431
0
      if (!bfd_unlock ())
432
0
  return -1;
433
0
      return 0;
434
0
    }
435
85.7k
  nwrite = fwrite (from, 1, nbytes, f);
436
85.7k
  if (nwrite < nbytes && ferror (f))
437
0
    {
438
0
      bfd_set_error (bfd_error_system_call);
439
0
      bfd_unlock ();
440
0
      return -1;
441
0
    }
442
85.7k
  if (!bfd_unlock ())
443
0
    return -1;
444
85.7k
  return nwrite;
445
85.7k
}
446
447
static int
448
cache_bclose (struct bfd *abfd)
449
3.44M
{
450
  /* No locking needed here, it's handled by the callee.  */
451
3.44M
  return bfd_cache_close (abfd) - 1;
452
3.44M
}
453
454
static int
455
cache_bflush (struct bfd *abfd)
456
3.09k
{
457
3.09k
  if (!bfd_lock ())
458
0
    return -1;
459
3.09k
  int sts;
460
3.09k
  FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
461
462
3.09k
  if (f == NULL)
463
0
    {
464
0
      if (!bfd_unlock ())
465
0
  return -1;
466
0
      return 0;
467
0
    }
468
3.09k
  sts = fflush (f);
469
3.09k
  if (sts < 0)
470
0
    bfd_set_error (bfd_error_system_call);
471
3.09k
  if (!bfd_unlock ())
472
0
    return -1;
473
3.09k
  return sts;
474
3.09k
}
475
476
static int
477
cache_bstat (struct bfd *abfd, struct stat *sb)
478
59.1M
{
479
59.1M
  if (!bfd_lock ())
480
0
    return -1;
481
59.1M
  int sts;
482
59.1M
  FILE *f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR);
483
484
59.1M
  if (f == NULL)
485
0
    {
486
0
      bfd_unlock ();
487
0
      return -1;
488
0
    }
489
59.1M
  sts = fstat (fileno (f), sb);
490
59.1M
  if (sts < 0)
491
0
    bfd_set_error (bfd_error_system_call);
492
59.1M
  if (!bfd_unlock ())
493
0
    return -1;
494
59.1M
  return sts;
495
59.1M
}
496
497
static void *
498
cache_bmmap (struct bfd *abfd ATTRIBUTE_UNUSED,
499
       void *addr ATTRIBUTE_UNUSED,
500
       size_t len ATTRIBUTE_UNUSED,
501
       int prot ATTRIBUTE_UNUSED,
502
       int flags ATTRIBUTE_UNUSED,
503
       file_ptr offset ATTRIBUTE_UNUSED,
504
       void **map_addr ATTRIBUTE_UNUSED,
505
       size_t *map_len ATTRIBUTE_UNUSED)
506
9.61k
{
507
9.61k
  void *ret = MAP_FAILED;
508
509
9.61k
  if (!bfd_lock ())
510
0
    return ret;
511
9.61k
  if ((abfd->flags & BFD_IN_MEMORY) != 0)
512
0
    abort ();
513
9.61k
#ifdef HAVE_MMAP
514
9.61k
  else
515
9.61k
    {
516
9.61k
      uintptr_t pagesize_m1 = _bfd_pagesize_m1;
517
9.61k
      FILE *f;
518
9.61k
      file_ptr pg_offset;
519
9.61k
      size_t pg_len;
520
521
9.61k
      f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR);
522
9.61k
      if (f == NULL)
523
0
  {
524
0
    bfd_unlock ();
525
0
    return ret;
526
0
  }
527
528
      /* Align.  */
529
9.61k
      pg_offset = offset & ~pagesize_m1;
530
9.61k
      pg_len = (len + (offset - pg_offset) + pagesize_m1) & ~pagesize_m1;
531
532
9.61k
      ret = mmap (addr, pg_len, prot, flags, fileno (f), pg_offset);
533
9.61k
      if (ret == MAP_FAILED)
534
0
  bfd_set_error (bfd_error_system_call);
535
9.61k
      else
536
9.61k
  {
537
9.61k
    *map_addr = ret;
538
9.61k
    *map_len = pg_len;
539
9.61k
    ret = (char *) ret + (offset & pagesize_m1);
540
9.61k
  }
541
9.61k
    }
542
9.61k
#endif
543
544
9.61k
  if (!bfd_unlock ())
545
0
    return MAP_FAILED;
546
9.61k
  return ret;
547
9.61k
}
548
549
static const struct bfd_iovec cache_iovec =
550
{
551
  &cache_bread, &cache_bwrite, &cache_btell, &cache_bseek,
552
  &cache_bclose, &cache_bflush, &cache_bstat, &cache_bmmap
553
};
554
555
static bool
556
_bfd_cache_init_unlocked (bfd *abfd)
557
195k
{
558
195k
  BFD_ASSERT (abfd->iostream != NULL);
559
195k
  if (open_files >= bfd_cache_max_open ())
560
0
    {
561
0
      if (! close_one ())
562
0
  return false;
563
0
    }
564
195k
  abfd->iovec = &cache_iovec;
565
195k
  insert (abfd);
566
195k
  abfd->flags &= ~BFD_CLOSED_BY_CACHE;
567
195k
  ++open_files;
568
195k
  return true;
569
195k
}
570
571
/*
572
INTERNAL_FUNCTION
573
  bfd_cache_init
574
575
SYNOPSIS
576
  bool bfd_cache_init (bfd *abfd);
577
578
DESCRIPTION
579
  Add a newly opened BFD to the cache.
580
*/
581
582
bool
583
bfd_cache_init (bfd *abfd)
584
190k
{
585
190k
  if (!bfd_lock ())
586
0
    return false;
587
190k
  bool result = _bfd_cache_init_unlocked (abfd);
588
190k
  if (!bfd_unlock ())
589
0
    return false;
590
190k
  return result;
591
190k
}
592
593
static bool
594
_bfd_cache_close_unlocked (bfd *abfd)
595
3.63M
{
596
  /* Don't remove this test.  bfd_reinit depends on it.  */
597
3.63M
  if (abfd->iovec != &cache_iovec)
598
64.5k
    return true;
599
600
3.57M
  if (abfd->iostream == NULL)
601
    /* Previously closed.  */
602
3.37M
    return true;
603
604
  /* Note: no locking needed in this function, as it is handled by
605
     bfd_cache_delete.  */
606
195k
  return bfd_cache_delete (abfd);
607
3.57M
}
608
609
/*
610
FUNCTION
611
  bfd_cache_close
612
613
SYNOPSIS
614
  bool bfd_cache_close (bfd *abfd);
615
616
DESCRIPTION
617
  Remove the BFD @var{abfd} from the cache. If the attached file is open,
618
  then close it too.
619
620
  <<FALSE>> is returned if closing the file fails, <<TRUE>> is
621
  returned if all is well.
622
*/
623
624
bool
625
bfd_cache_close (bfd *abfd)
626
3.63M
{
627
3.63M
  if (!bfd_lock ())
628
0
    return false;
629
3.63M
  bool result = _bfd_cache_close_unlocked (abfd);
630
3.63M
  if (!bfd_unlock ())
631
0
    return false;
632
3.63M
  return result;
633
3.63M
}
634
635
/*
636
FUNCTION
637
  bfd_cache_close_all
638
639
SYNOPSIS
640
  bool bfd_cache_close_all (void);
641
642
DESCRIPTION
643
  Remove all BFDs from the cache. If the attached file is open,
644
  then close it too.  Note - despite its name this function will
645
  close a BFD even if it is not marked as being cacheable, ie
646
  even if bfd_get_cacheable() returns false.
647
648
  <<FALSE>> is returned if closing one of the file fails, <<TRUE>> is
649
  returned if all is well.
650
*/
651
652
bool
653
bfd_cache_close_all (void)
654
0
{
655
0
  bool ret = true;
656
657
0
  if (!bfd_lock ())
658
0
    return false;
659
0
  while (bfd_last_cache != NULL)
660
0
    {
661
0
      bfd *prev_bfd_last_cache = bfd_last_cache;
662
663
0
      ret &= _bfd_cache_close_unlocked (bfd_last_cache);
664
665
      /* Stop a potential infinite loop should bfd_cache_close()
666
   not update bfd_last_cache.  */
667
0
      if (bfd_last_cache == prev_bfd_last_cache)
668
0
  break;
669
0
    }
670
671
0
  if (!bfd_unlock ())
672
0
    return false;
673
0
  return ret;
674
0
}
675
676
/*
677
INTERNAL_FUNCTION
678
  bfd_cache_set_uncloseable
679
680
SYNOPSIS
681
  bool bfd_cache_set_uncloseable (bfd *abfd, bool value, bool *old);
682
683
DESCRIPTION
684
  Internal function to mark ABFD as either closeable or not.
685
  This is used by bfd_check_format_matches to avoid races
686
  where bfd_cache_close_all is called in another thread.
687
  VALUE is true to mark the BFD as temporarily uncloseable
688
  by the cache; false to mark it as closeable once again.
689
  OLD, if non-NULL, is set to the previous value of the flag.
690
  Returns false on error, true on success.
691
*/
692
693
bool
694
bfd_cache_set_uncloseable (bfd *abfd, bool value, bool *old)
695
7.24M
{
696
7.24M
  bool result = true;
697
698
7.24M
  if (!bfd_lock ())
699
0
    return false;
700
7.24M
  if (old != NULL)
701
3.62M
    *old = abfd->in_format_matches;
702
703
  /* Only perform any action when the state changes,and only when this
704
     BFD is actually using the cache.  */
705
7.24M
  if (value != abfd->in_format_matches
706
7.24M
      && abfd->iovec == &cache_iovec
707
7.24M
      && possibly_cached (abfd))
708
599k
    {
709
599k
      if (value)
710
300k
  {
711
    /* Marking as uncloseable for the first time.  Ensure the
712
       file is open, and remove from the cache list.  */
713
300k
    FILE *f = bfd_cache_lookup (abfd, CACHE_NORMAL);
714
300k
    if (f == NULL)
715
0
      result = false;
716
300k
    else
717
300k
      snip (abfd);
718
300k
  }
719
299k
      else
720
299k
  {
721
    /* Mark as closeable again.  */
722
299k
    insert (abfd);
723
299k
  }
724
725
599k
      abfd->in_format_matches = value;
726
599k
    }
727
728
7.24M
  if (!bfd_unlock ())
729
0
    return false;
730
7.24M
  return result;
731
7.24M
}
732
733
/*
734
FUNCTION
735
  bfd_cache_size
736
737
SYNOPSIS
738
  unsigned bfd_cache_size (void);
739
740
DESCRIPTION
741
  Return the number of open files in the cache.
742
*/
743
744
unsigned
745
bfd_cache_size (void)
746
0
{
747
0
  return open_files;
748
0
}
749
750
static FILE *
751
_bfd_open_file_unlocked (bfd *abfd)
752
5.13k
{
753
5.13k
  abfd->cacheable = true; /* Allow it to be closed later.  */
754
755
5.13k
  if (open_files >= bfd_cache_max_open ())
756
0
    {
757
0
      if (! close_one ())
758
0
  return NULL;
759
0
    }
760
761
5.13k
  switch (abfd->direction)
762
5.13k
    {
763
328
    case read_direction:
764
328
    case no_direction:
765
328
      abfd->iostream = _bfd_real_fopen (bfd_get_filename (abfd), FOPEN_RB);
766
328
      break;
767
0
    case both_direction:
768
4.81k
    case write_direction:
769
4.81k
      if (abfd->opened_once)
770
0
  {
771
0
    abfd->iostream = _bfd_real_fopen (bfd_get_filename (abfd),
772
0
              FOPEN_RUB);
773
0
    if (abfd->iostream == NULL)
774
0
      abfd->iostream = _bfd_real_fopen (bfd_get_filename (abfd),
775
0
                FOPEN_WUB);
776
0
  }
777
4.81k
      else
778
4.81k
  {
779
    /* Create the file.
780
781
       Some operating systems won't let us overwrite a running
782
       binary.  For them, we want to unlink the file first.
783
784
       However, gcc 2.95 will create temporary files using
785
       O_EXCL and tight permissions to prevent other users from
786
       substituting other .o files during the compilation.  gcc
787
       will then tell the assembler to use the newly created
788
       file as an output file.  If we unlink the file here, we
789
       open a brief window when another user could still
790
       substitute a file.
791
792
       So we unlink the output file if and only if it has
793
       non-zero size.  */
794
4.81k
#ifndef __MSDOS__
795
    /* Don't do this for MSDOS: it doesn't care about overwriting
796
       a running binary, but if this file is already open by
797
       another BFD, we will be in deep trouble if we delete an
798
       open file.  In fact, objdump does just that if invoked with
799
       the --info option.  */
800
4.81k
    struct stat s;
801
802
4.81k
    if (stat (bfd_get_filename (abfd), &s) == 0 && s.st_size != 0)
803
210
      unlink_if_ordinary (bfd_get_filename (abfd));
804
4.81k
#endif
805
4.81k
    abfd->iostream = _bfd_real_fopen (bfd_get_filename (abfd),
806
4.81k
              FOPEN_WUB);
807
4.81k
    abfd->opened_once = true;
808
4.81k
  }
809
4.81k
      break;
810
5.13k
    }
811
812
5.13k
  if (abfd->iostream == NULL)
813
164
    bfd_set_error (bfd_error_system_call);
814
4.97k
  else
815
4.97k
    {
816
4.97k
      if (! _bfd_cache_init_unlocked (abfd))
817
0
  return NULL;
818
4.97k
    }
819
820
5.13k
  return (FILE *) abfd->iostream;
821
5.13k
}
822
823
/*
824
INTERNAL_FUNCTION
825
  bfd_open_file
826
827
SYNOPSIS
828
  FILE* bfd_open_file (bfd *abfd);
829
830
DESCRIPTION
831
  Call the OS to open a file for @var{abfd}.  Return the <<FILE *>>
832
  (possibly <<NULL>>) that results from this operation.  Set up the
833
  BFD so that future accesses know the file is open. If the <<FILE *>>
834
  returned is <<NULL>>, then it won't have been put in the
835
  cache, so it won't have to be removed from it.
836
*/
837
838
FILE *
839
bfd_open_file (bfd *abfd)
840
5.13k
{
841
5.13k
  if (!bfd_lock ())
842
0
    return NULL;
843
5.13k
  FILE *result = _bfd_open_file_unlocked (abfd);
844
5.13k
  if (!bfd_unlock ())
845
0
    return NULL;
846
5.13k
  return result;
847
5.13k
}