Coverage Report

Created: 2023-08-28 06:31

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