Coverage Report

Created: 2026-04-04 08:16

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