1 /* BFD library -- caching of file descriptors.
3 Copyright (C) 1990-2023 Free Software Foundation, Inc.
5 Hacked by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
7 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
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
46 #include "libiberty.h"
52 static FILE *_bfd_open_file_unlocked (bfd
*abfd
);
54 /* In some cases we can optimize cache operation when reopening files.
55 For instance, a flush is entirely unnecessary if the file is already
56 closed, so a flush would use CACHE_NO_OPEN. Similarly, a seek using
57 SEEK_SET or SEEK_END need not first seek to the current position.
58 For stat we ignore seek errors, just in case the file has changed
59 while we weren't looking. If it has, then it's possible that the
60 file is shorter and we don't want a seek error to prevent us doing
66 CACHE_NO_SEEK_ERROR
= 4
69 /* The maximum number of files which the cache will keep open at
70 one time. When needed call bfd_cache_max_open to initialize. */
72 static unsigned max_open_files
= 0;
74 /* Set max_open_files, if not already set, to 12.5% of the allowed open
75 file descriptors, but at least 10, and return the value. */
77 bfd_cache_max_open (void)
79 if (max_open_files
== 0)
82 #if defined(__sun) && !defined(__sparcv9) && !defined(__x86_64__)
83 /* PR ld/19260: 32-bit Solaris has very inelegant handling of the 255
84 file descriptor limit. The problem is that setrlimit(2) can raise
85 RLIMIT_NOFILE to a value that is not supported by libc, resulting
86 in "Too many open files" errors. This can happen here even though
87 max_open_files is set to rlim.rlim_cur / 8. For example, if
88 a parent process has set rlim.rlim_cur to 65536, then max_open_files
89 will be computed as 8192.
91 This check essentially reverts to the behavior from binutils 2.23.1
92 for 32-bit Solaris only. (It is hoped that the 32-bit libc
93 limitation will be removed soon). 64-bit Solaris libc does not have
100 if (getrlimit (RLIMIT_NOFILE
, &rlim
) == 0
101 && rlim
.rlim_cur
!= (rlim_t
) RLIM_INFINITY
)
102 max
= rlim
.rlim_cur
/ 8;
106 max
= sysconf (_SC_OPEN_MAX
) / 8;
110 #endif /* not 32-bit Solaris */
112 max_open_files
= max
< 10 ? 10 : max
;
115 return max_open_files
;
118 /* The number of BFD files we have open. */
120 static unsigned open_files
;
122 /* Zero, or a pointer to the topmost BFD on the chain. This is
123 used by the <<bfd_cache_lookup>> macro in @file{libbfd.h} to
124 determine when it can avoid a function call. */
126 static bfd
*bfd_last_cache
= NULL
;
128 /* Insert a BFD into the cache. */
133 if (bfd_last_cache
== NULL
)
135 abfd
->lru_next
= abfd
;
136 abfd
->lru_prev
= abfd
;
140 abfd
->lru_next
= bfd_last_cache
;
141 abfd
->lru_prev
= bfd_last_cache
->lru_prev
;
142 abfd
->lru_prev
->lru_next
= abfd
;
143 abfd
->lru_next
->lru_prev
= abfd
;
145 bfd_last_cache
= abfd
;
148 /* Remove a BFD from the cache. */
153 abfd
->lru_prev
->lru_next
= abfd
->lru_next
;
154 abfd
->lru_next
->lru_prev
= abfd
->lru_prev
;
155 if (abfd
== bfd_last_cache
)
157 bfd_last_cache
= abfd
->lru_next
;
158 if (abfd
== bfd_last_cache
)
159 bfd_last_cache
= NULL
;
163 /* Close a BFD and remove it from the cache. */
166 bfd_cache_delete (bfd
*abfd
)
170 if (fclose ((FILE *) abfd
->iostream
) == 0)
175 bfd_set_error (bfd_error_system_call
);
180 abfd
->iostream
= NULL
;
181 BFD_ASSERT (open_files
> 0);
183 abfd
->flags
|= BFD_CLOSED_BY_CACHE
;
188 /* We need to open a new file, and the cache is full. Find the least
189 recently used cacheable BFD and close it. */
194 register bfd
*to_kill
;
196 if (bfd_last_cache
== NULL
)
200 for (to_kill
= bfd_last_cache
->lru_prev
;
201 ! to_kill
->cacheable
;
202 to_kill
= to_kill
->lru_prev
)
204 if (to_kill
== bfd_last_cache
)
214 /* There are no open cacheable BFD's. */
218 to_kill
->where
= _bfd_real_ftell ((FILE *) to_kill
->iostream
);
220 return bfd_cache_delete (to_kill
);
223 /* Check to see if the required BFD is the same as the last one
224 looked up. If so, then it can use the stream in the BFD with
225 impunity, since it can't have changed since the last lookup;
226 otherwise, it has to perform the complicated lookup function. */
228 #define bfd_cache_lookup(x, flag) \
229 ((x) == bfd_last_cache \
230 ? (FILE *) (bfd_last_cache->iostream) \
231 : bfd_cache_lookup_worker (x, flag))
233 /* Called when the macro <<bfd_cache_lookup>> fails to find a
234 quick answer. Find a file descriptor for @var{abfd}. If
235 necessary, it open it. If there are already more than
236 <<bfd_cache_max_open>> files open, it tries to close one first, to
237 avoid running out of file descriptors. It will return NULL
238 if it is unable to (re)open the @var{abfd}. */
241 bfd_cache_lookup_worker (bfd
*abfd
, enum cache_flag flag
)
243 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
246 if (abfd
->my_archive
!= NULL
247 && !bfd_is_thin_archive (abfd
->my_archive
))
250 if (abfd
->iostream
!= NULL
)
252 /* Move the file to the start of the cache. */
253 if (abfd
!= bfd_last_cache
)
258 return (FILE *) abfd
->iostream
;
261 if (flag
& CACHE_NO_OPEN
)
264 if (_bfd_open_file_unlocked (abfd
) == NULL
)
266 else if (!(flag
& CACHE_NO_SEEK
)
267 && _bfd_real_fseek ((FILE *) abfd
->iostream
,
268 abfd
->where
, SEEK_SET
) != 0
269 && !(flag
& CACHE_NO_SEEK_ERROR
))
270 bfd_set_error (bfd_error_system_call
);
272 return (FILE *) abfd
->iostream
;
274 /* xgettext:c-format */
275 _bfd_error_handler (_("reopening %pB: %s"),
276 abfd
, bfd_errmsg (bfd_get_error ()));
281 cache_btell (struct bfd
*abfd
)
285 FILE *f
= bfd_cache_lookup (abfd
, CACHE_NO_OPEN
);
292 file_ptr result
= _bfd_real_ftell (f
);
299 cache_bseek (struct bfd
*abfd
, file_ptr offset
, int whence
)
303 FILE *f
= bfd_cache_lookup (abfd
, whence
!= SEEK_CUR
? CACHE_NO_SEEK
: CACHE_NORMAL
);
309 int result
= _bfd_real_fseek (f
, offset
, whence
);
315 /* Note that archive entries don't have streams; they share their parent's.
316 This allows someone to play with the iostream behind BFD's back.
318 Also, note that the origin pointer points to the beginning of a file's
319 contents (0 for non-archive elements). For archive entries this is the
320 first octet in the file, NOT the beginning of the archive header. */
323 cache_bread_1 (FILE *f
, void *buf
, file_ptr nbytes
)
327 #if defined (__VAX) && defined (VMS)
328 /* Apparently fread on Vax VMS does not keep the record length
330 nread
= read (fileno (f
), buf
, nbytes
);
331 /* Set bfd_error if we did not read as much data as we expected. If
332 the read failed due to an error set the bfd_error_system_call,
333 else set bfd_error_file_truncated. */
334 if (nread
== (file_ptr
)-1)
336 bfd_set_error (bfd_error_system_call
);
340 nread
= fread (buf
, 1, nbytes
, f
);
341 /* Set bfd_error if we did not read as much data as we expected. If
342 the read failed due to an error set the bfd_error_system_call,
343 else set bfd_error_file_truncated. */
344 if (nread
< nbytes
&& ferror (f
))
346 bfd_set_error (bfd_error_system_call
);
351 /* This may or may not be an error, but in case the calling code
352 bails out because of it, set the right error code. */
353 bfd_set_error (bfd_error_file_truncated
);
358 cache_bread (struct bfd
*abfd
, void *buf
, file_ptr nbytes
)
365 f
= bfd_cache_lookup (abfd
, CACHE_NORMAL
);
372 /* Some filesystems are unable to handle reads that are too large
373 (for instance, NetApp shares with oplocks turned off). To avoid
374 hitting this limitation, we read the buffer in chunks of 8MB max. */
375 while (nread
< nbytes
)
377 const file_ptr max_chunk_size
= 0x800000;
378 file_ptr chunk_size
= nbytes
- nread
;
379 file_ptr chunk_nread
;
381 if (chunk_size
> max_chunk_size
)
382 chunk_size
= max_chunk_size
;
384 chunk_nread
= cache_bread_1 (f
, (char *) buf
+ nread
, chunk_size
);
386 /* Update the nread count.
388 We just have to be careful of the case when cache_bread_1 returns
389 a negative count: If this is our first read, then set nread to
390 that negative count in order to return that negative value to the
391 caller. Otherwise, don't add it to our total count, or we would
392 end up returning a smaller number of bytes read than we actually
394 if (nread
== 0 || chunk_nread
> 0)
395 nread
+= chunk_nread
;
397 if (chunk_nread
< chunk_size
)
407 cache_bwrite (struct bfd
*abfd
, const void *from
, file_ptr nbytes
)
412 FILE *f
= bfd_cache_lookup (abfd
, CACHE_NORMAL
);
420 nwrite
= fwrite (from
, 1, nbytes
, f
);
421 if (nwrite
< nbytes
&& ferror (f
))
423 bfd_set_error (bfd_error_system_call
);
433 cache_bclose (struct bfd
*abfd
)
435 /* No locking needed here, it's handled by the callee. */
436 return bfd_cache_close (abfd
) - 1;
440 cache_bflush (struct bfd
*abfd
)
445 FILE *f
= bfd_cache_lookup (abfd
, CACHE_NO_OPEN
);
455 bfd_set_error (bfd_error_system_call
);
462 cache_bstat (struct bfd
*abfd
, struct stat
*sb
)
467 FILE *f
= bfd_cache_lookup (abfd
, CACHE_NO_SEEK_ERROR
);
474 sts
= fstat (fileno (f
), sb
);
476 bfd_set_error (bfd_error_system_call
);
483 cache_bmmap (struct bfd
*abfd ATTRIBUTE_UNUSED
,
484 void *addr ATTRIBUTE_UNUSED
,
485 bfd_size_type len ATTRIBUTE_UNUSED
,
486 int prot ATTRIBUTE_UNUSED
,
487 int flags ATTRIBUTE_UNUSED
,
488 file_ptr offset ATTRIBUTE_UNUSED
,
489 void **map_addr ATTRIBUTE_UNUSED
,
490 bfd_size_type
*map_len ATTRIBUTE_UNUSED
)
492 void *ret
= (void *) -1;
496 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
501 static uintptr_t pagesize_m1
;
504 bfd_size_type pg_len
;
506 f
= bfd_cache_lookup (abfd
, CACHE_NO_SEEK_ERROR
);
513 if (pagesize_m1
== 0)
514 pagesize_m1
= getpagesize () - 1;
517 pg_offset
= offset
& ~pagesize_m1
;
518 pg_len
= (len
+ (offset
- pg_offset
) + pagesize_m1
) & ~pagesize_m1
;
520 ret
= mmap (addr
, pg_len
, prot
, flags
, fileno (f
), pg_offset
);
521 if (ret
== (void *) -1)
522 bfd_set_error (bfd_error_system_call
);
527 ret
= (char *) ret
+ (offset
& pagesize_m1
);
537 static const struct bfd_iovec cache_iovec
=
539 &cache_bread
, &cache_bwrite
, &cache_btell
, &cache_bseek
,
540 &cache_bclose
, &cache_bflush
, &cache_bstat
, &cache_bmmap
544 _bfd_cache_init_unlocked (bfd
*abfd
)
546 BFD_ASSERT (abfd
->iostream
!= NULL
);
547 if (open_files
>= bfd_cache_max_open ())
552 abfd
->iovec
= &cache_iovec
;
554 abfd
->flags
&= ~BFD_CLOSED_BY_CACHE
;
564 bool bfd_cache_init (bfd *abfd);
567 Add a newly opened BFD to the cache.
571 bfd_cache_init (bfd
*abfd
)
575 bool result
= _bfd_cache_init_unlocked (abfd
);
582 _bfd_cache_close_unlocked (bfd
*abfd
)
584 /* Don't remove this test. bfd_reinit depends on it. */
585 if (abfd
->iovec
!= &cache_iovec
)
588 if (abfd
->iostream
== NULL
)
589 /* Previously closed. */
592 /* Note: no locking needed in this function, as it is handled by
594 return bfd_cache_delete (abfd
);
602 bool bfd_cache_close (bfd *abfd);
605 Remove the BFD @var{abfd} from the cache. If the attached file is open,
608 <<FALSE>> is returned if closing the file fails, <<TRUE>> is
609 returned if all is well.
613 bfd_cache_close (bfd
*abfd
)
617 bool result
= _bfd_cache_close_unlocked (abfd
);
628 bool bfd_cache_close_all (void);
631 Remove all BFDs from the cache. If the attached file is open,
632 then close it too. Note - despite its name this function will
633 close a BFD even if it is not marked as being cacheable, ie
634 even if bfd_get_cacheable() returns false.
636 <<FALSE>> is returned if closing one of the file fails, <<TRUE>> is
637 returned if all is well.
641 bfd_cache_close_all (void)
647 while (bfd_last_cache
!= NULL
)
649 bfd
*prev_bfd_last_cache
= bfd_last_cache
;
651 ret
&= _bfd_cache_close_unlocked (bfd_last_cache
);
653 /* Stop a potential infinite loop should bfd_cache_close()
654 not update bfd_last_cache. */
655 if (bfd_last_cache
== prev_bfd_last_cache
)
669 unsigned bfd_cache_size (void);
672 Return the number of open files in the cache.
676 bfd_cache_size (void)
682 _bfd_open_file_unlocked (bfd
*abfd
)
684 abfd
->cacheable
= true; /* Allow it to be closed later. */
686 if (open_files
>= bfd_cache_max_open ())
692 switch (abfd
->direction
)
696 abfd
->iostream
= _bfd_real_fopen (bfd_get_filename (abfd
), FOPEN_RB
);
699 case write_direction
:
700 if (abfd
->opened_once
)
702 abfd
->iostream
= _bfd_real_fopen (bfd_get_filename (abfd
),
704 if (abfd
->iostream
== NULL
)
705 abfd
->iostream
= _bfd_real_fopen (bfd_get_filename (abfd
),
712 Some operating systems won't let us overwrite a running
713 binary. For them, we want to unlink the file first.
715 However, gcc 2.95 will create temporary files using
716 O_EXCL and tight permissions to prevent other users from
717 substituting other .o files during the compilation. gcc
718 will then tell the assembler to use the newly created
719 file as an output file. If we unlink the file here, we
720 open a brief window when another user could still
723 So we unlink the output file if and only if it has
726 /* Don't do this for MSDOS: it doesn't care about overwriting
727 a running binary, but if this file is already open by
728 another BFD, we will be in deep trouble if we delete an
729 open file. In fact, objdump does just that if invoked with
730 the --info option. */
733 if (stat (bfd_get_filename (abfd
), &s
) == 0 && s
.st_size
!= 0)
734 unlink_if_ordinary (bfd_get_filename (abfd
));
736 abfd
->iostream
= _bfd_real_fopen (bfd_get_filename (abfd
),
738 abfd
->opened_once
= true;
743 if (abfd
->iostream
== NULL
)
744 bfd_set_error (bfd_error_system_call
);
747 if (! _bfd_cache_init_unlocked (abfd
))
751 return (FILE *) abfd
->iostream
;
759 FILE* bfd_open_file (bfd *abfd);
762 Call the OS to open a file for @var{abfd}. Return the <<FILE *>>
763 (possibly <<NULL>>) that results from this operation. Set up the
764 BFD so that future accesses know the file is open. If the <<FILE *>>
765 returned is <<NULL>>, then it won't have been put in the
766 cache, so it won't have to be removed from it.
770 bfd_open_file (bfd
*abfd
)
774 FILE *result
= _bfd_open_file_unlocked (abfd
);