]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/cache.c
gdb, amd64: remove unused forward declarations
[thirdparty/binutils-gdb.git] / bfd / cache.c
CommitLineData
252b5132 1/* BFD library -- caching of file descriptors.
7c192733 2
fd67aa11 3 Copyright (C) 1990-2024 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"
252b5132 47
1185b5b7
TT
48static FILE *_bfd_open_file_unlocked (bfd *abfd);
49
95560129
AM
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. */
58enum cache_flag {
59 CACHE_NORMAL = 0,
60 CACHE_NO_OPEN = 1,
61 CACHE_NO_SEEK = 2,
62 CACHE_NO_SEEK_ERROR = 4
63};
64
d00967c7 65/* The maximum number of files which the cache will keep open at
9d782e8d 66 one time. When needed call bfd_cache_max_open to initialize. */
d0fdd288 67
241f29fb 68static unsigned max_open_files = 0;
9d782e8d
MW
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. */
241f29fb 72static unsigned
9d782e8d
MW
73bfd_cache_max_open (void)
74{
75 if (max_open_files == 0)
76 {
77 int max;
0b1fa288
ST
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
07d6d2b8 82 in "Too many open files" errors. This can happen here even though
0b1fa288
ST
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
07d6d2b8 88 for 32-bit Solaris only. (It is hoped that the 32-bit libc
0b1fa288
ST
89 limitation will be removed soon). 64-bit Solaris libc does not have
90 this limitation. */
91 max = 16;
92#else
9d782e8d
MW
93#ifdef HAVE_GETRLIMIT
94 struct rlimit rlim;
0b1fa288 95
9d782e8d 96 if (getrlimit (RLIMIT_NOFILE, &rlim) == 0
d1eb5696 97 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY)
9d782e8d
MW
98 max = rlim.rlim_cur / 8;
99 else
0b1fa288 100#endif
9d782e8d
MW
101#ifdef _SC_OPEN_MAX
102 max = sysconf (_SC_OPEN_MAX) / 8;
103#else
07d6d2b8 104 max = 10;
0b1fa288
ST
105#endif
106#endif /* not 32-bit Solaris */
107
9d782e8d
MW
108 max_open_files = max < 10 ? 10 : max;
109 }
110
111 return max_open_files;
112}
d0fdd288
AM
113
114/* The number of BFD files we have open. */
115
241f29fb 116static unsigned open_files;
d0fdd288 117
d00967c7
AM
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. */
d0fdd288 121
d00967c7 122static bfd *bfd_last_cache = NULL;
252b5132 123
d0fdd288
AM
124/* Insert a BFD into the cache. */
125
126static void
127insert (bfd *abfd)
128{
129 if (bfd_last_cache == NULL)
130 {
131 abfd->lru_next = abfd;
132 abfd->lru_prev = abfd;
133 }
134 else
135 {
136 abfd->lru_next = bfd_last_cache;
137 abfd->lru_prev = bfd_last_cache->lru_prev;
138 abfd->lru_prev->lru_next = abfd;
139 abfd->lru_next->lru_prev = abfd;
140 }
141 bfd_last_cache = abfd;
142}
143
144/* Remove a BFD from the cache. */
145
146static void
147snip (bfd *abfd)
148{
149 abfd->lru_prev->lru_next = abfd->lru_next;
150 abfd->lru_next->lru_prev = abfd->lru_prev;
151 if (abfd == bfd_last_cache)
152 {
153 bfd_last_cache = abfd->lru_next;
154 if (abfd == bfd_last_cache)
155 bfd_last_cache = NULL;
156 }
157}
158
159/* Close a BFD and remove it from the cache. */
160
0a1b45a2 161static bool
d0fdd288
AM
162bfd_cache_delete (bfd *abfd)
163{
0a1b45a2 164 bool ret;
d0fdd288
AM
165
166 if (fclose ((FILE *) abfd->iostream) == 0)
0a1b45a2 167 ret = true;
d0fdd288
AM
168 else
169 {
0a1b45a2 170 ret = false;
d0fdd288
AM
171 bfd_set_error (bfd_error_system_call);
172 }
173
174 snip (abfd);
175
176 abfd->iostream = NULL;
241f29fb 177 BFD_ASSERT (open_files > 0);
d0fdd288 178 --open_files;
a6ad7914 179 abfd->flags |= BFD_CLOSED_BY_CACHE;
d0fdd288
AM
180
181 return ret;
182}
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
0a1b45a2 187static bool
d0fdd288
AM
188close_one (void)
189{
f664f618 190 register bfd *to_kill;
d0fdd288
AM
191
192 if (bfd_last_cache == NULL)
f664f618 193 to_kill = NULL;
d0fdd288
AM
194 else
195 {
f664f618
TG
196 for (to_kill = bfd_last_cache->lru_prev;
197 ! to_kill->cacheable;
198 to_kill = to_kill->lru_prev)
d0fdd288 199 {
f664f618 200 if (to_kill == bfd_last_cache)
d0fdd288 201 {
f664f618 202 to_kill = NULL;
d0fdd288
AM
203 break;
204 }
205 }
206 }
207
f664f618 208 if (to_kill == NULL)
d0fdd288
AM
209 {
210 /* There are no open cacheable BFD's. */
0a1b45a2 211 return true;
d0fdd288
AM
212 }
213
c7c3d11b 214 to_kill->where = _bfd_real_ftell ((FILE *) to_kill->iostream);
d0fdd288 215
f664f618 216 return bfd_cache_delete (to_kill);
d0fdd288
AM
217}
218
d00967c7
AM
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. */
d0fdd288 223
95560129 224#define bfd_cache_lookup(x, flag) \
d00967c7
AM
225 ((x) == bfd_last_cache \
226 ? (FILE *) (bfd_last_cache->iostream) \
95560129 227 : bfd_cache_lookup_worker (x, flag))
d0fdd288 228
20bf7711
TT
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
232static bool
233possibly_cached (bfd *abfd)
234{
235 if ((abfd->flags & BFD_IN_MEMORY) != 0)
236 return false;
237 if (abfd->my_archive != NULL
238 && !bfd_is_thin_archive (abfd->my_archive))
239 return false;
240 return true;
241}
242
d00967c7
AM
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
9d782e8d 246 <<bfd_cache_max_open>> files open, it tries to close one first, to
d00967c7
AM
247 avoid running out of file descriptors. It will return NULL
248 if it is unable to (re)open the @var{abfd}. */
d0fdd288 249
d00967c7 250static FILE *
95560129 251bfd_cache_lookup_worker (bfd *abfd, enum cache_flag flag)
d0fdd288 252{
20bf7711 253 if (!possibly_cached (abfd))
d0fdd288
AM
254 abort ();
255
20bf7711
TT
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 if (abfd->in_format_matches)
259 {
260 if (abfd->iostream == NULL)
261 abort ();
262 return (FILE *) abfd->iostream;
263 }
d0fdd288
AM
264
265 if (abfd->iostream != NULL)
266 {
267 /* Move the file to the start of the cache. */
268 if (abfd != bfd_last_cache)
269 {
270 snip (abfd);
271 insert (abfd);
272 }
273 return (FILE *) abfd->iostream;
274 }
275
95560129
AM
276 if (flag & CACHE_NO_OPEN)
277 return NULL;
278
1185b5b7 279 if (_bfd_open_file_unlocked (abfd) == NULL)
d0fdd288 280 ;
95560129 281 else if (!(flag & CACHE_NO_SEEK)
c7c3d11b
PA
282 && _bfd_real_fseek ((FILE *) abfd->iostream,
283 abfd->where, SEEK_SET) != 0
95560129 284 && !(flag & CACHE_NO_SEEK_ERROR))
d0fdd288
AM
285 bfd_set_error (bfd_error_system_call);
286 else
f656f9c7 287 return (FILE *) abfd->iostream;
d0fdd288 288
695344c0 289 /* xgettext:c-format */
a6ad7914 290 _bfd_error_handler (_("reopening %pB: %s"),
5c4ce239 291 abfd, bfd_errmsg (bfd_get_error ()));
d0fdd288
AM
292 return NULL;
293}
40838a72
AC
294
295static file_ptr
296cache_btell (struct bfd *abfd)
297{
1185b5b7
TT
298 if (!bfd_lock ())
299 return -1;
95560129 300 FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
3dff57e8 301 if (f == NULL)
1185b5b7
TT
302 {
303 if (!bfd_unlock ())
304 return -1;
305 return abfd->where;
306 }
307 file_ptr result = _bfd_real_ftell (f);
308 if (!bfd_unlock ())
309 return -1;
310 return result;
40838a72
AC
311}
312
313static int
314cache_bseek (struct bfd *abfd, file_ptr offset, int whence)
315{
1185b5b7
TT
316 if (!bfd_lock ())
317 return -1;
a50b1753 318 FILE *f = bfd_cache_lookup (abfd, whence != SEEK_CUR ? CACHE_NO_SEEK : CACHE_NORMAL);
3dff57e8 319 if (f == NULL)
1185b5b7
TT
320 {
321 bfd_unlock ();
322 return -1;
323 }
324 int result = _bfd_real_fseek (f, offset, whence);
325 if (!bfd_unlock ())
3dff57e8 326 return -1;
1185b5b7 327 return result;
40838a72
AC
328}
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
337static file_ptr
5c4ce239 338cache_bread_1 (FILE *f, void *buf, file_ptr nbytes)
40838a72
AC
339{
340 file_ptr nread;
3dff57e8 341
40838a72
AC
342#if defined (__VAX) && defined (VMS)
343 /* Apparently fread on Vax VMS does not keep the record length
344 information. */
3dff57e8 345 nread = read (fileno (f), buf, nbytes);
40838a72
AC
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);
87f14779 352 return nread;
40838a72
AC
353 }
354#else
3dff57e8 355 nread = fread (buf, 1, nbytes, f);
40838a72
AC
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. */
3dff57e8 359 if (nread < nbytes && ferror (f))
40838a72
AC
360 {
361 bfd_set_error (bfd_error_system_call);
87f14779 362 return nread;
40838a72
AC
363 }
364#endif
662ed161
DJ
365 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 bfd_set_error (bfd_error_file_truncated);
40838a72
AC
369 return nread;
370}
371
f12a02c0
JB
372static file_ptr
373cache_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
374{
1185b5b7
TT
375 if (!bfd_lock ())
376 return -1;
f12a02c0 377 file_ptr nread = 0;
5c4ce239
AM
378 FILE *f;
379
380 f = bfd_cache_lookup (abfd, CACHE_NORMAL);
381 if (f == NULL)
1185b5b7
TT
382 {
383 bfd_unlock ();
384 return -1;
385 }
f12a02c0
JB
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 while (nread < nbytes)
391 {
392 const file_ptr max_chunk_size = 0x800000;
393 file_ptr chunk_size = nbytes - nread;
394 file_ptr chunk_nread;
395
396 if (chunk_size > max_chunk_size)
07d6d2b8 397 chunk_size = max_chunk_size;
f12a02c0 398
5c4ce239 399 chunk_nread = cache_bread_1 (f, (char *) buf + nread, chunk_size);
f12a02c0
JB
400
401 /* Update the nread count.
402
07d6d2b8
AM
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. */
f12a02c0 409 if (nread == 0 || chunk_nread > 0)
07d6d2b8 410 nread += chunk_nread;
f12a02c0
JB
411
412 if (chunk_nread < chunk_size)
07d6d2b8 413 break;
f12a02c0
JB
414 }
415
1185b5b7
TT
416 if (!bfd_unlock ())
417 return -1;
f12a02c0
JB
418 return nread;
419}
420
40838a72 421static file_ptr
5c4ce239 422cache_bwrite (struct bfd *abfd, const void *from, file_ptr nbytes)
40838a72 423{
1185b5b7
TT
424 if (!bfd_lock ())
425 return -1;
3dff57e8 426 file_ptr nwrite;
a50b1753 427 FILE *f = bfd_cache_lookup (abfd, CACHE_NORMAL);
cb5220a0 428
3dff57e8 429 if (f == NULL)
1185b5b7
TT
430 {
431 if (!bfd_unlock ())
432 return -1;
433 return 0;
434 }
5c4ce239 435 nwrite = fwrite (from, 1, nbytes, f);
3dff57e8 436 if (nwrite < nbytes && ferror (f))
40838a72
AC
437 {
438 bfd_set_error (bfd_error_system_call);
1185b5b7 439 bfd_unlock ();
40838a72
AC
440 return -1;
441 }
1185b5b7
TT
442 if (!bfd_unlock ())
443 return -1;
40838a72
AC
444 return nwrite;
445}
446
405bf443 447static int
40838a72
AC
448cache_bclose (struct bfd *abfd)
449{
1185b5b7 450 /* No locking needed here, it's handled by the callee. */
405bf443 451 return bfd_cache_close (abfd) - 1;
40838a72
AC
452}
453
454static int
455cache_bflush (struct bfd *abfd)
456{
1185b5b7
TT
457 if (!bfd_lock ())
458 return -1;
3dff57e8 459 int sts;
95560129 460 FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
cb5220a0 461
3dff57e8 462 if (f == NULL)
1185b5b7
TT
463 {
464 if (!bfd_unlock ())
465 return -1;
466 return 0;
467 }
3dff57e8 468 sts = fflush (f);
40838a72
AC
469 if (sts < 0)
470 bfd_set_error (bfd_error_system_call);
1185b5b7
TT
471 if (!bfd_unlock ())
472 return -1;
40838a72
AC
473 return sts;
474}
475
476static int
477cache_bstat (struct bfd *abfd, struct stat *sb)
478{
1185b5b7
TT
479 if (!bfd_lock ())
480 return -1;
3dff57e8 481 int sts;
95560129 482 FILE *f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR);
cb5220a0 483
3dff57e8 484 if (f == NULL)
1185b5b7
TT
485 {
486 bfd_unlock ();
487 return -1;
488 }
3dff57e8 489 sts = fstat (fileno (f), sb);
40838a72
AC
490 if (sts < 0)
491 bfd_set_error (bfd_error_system_call);
1185b5b7
TT
492 if (!bfd_unlock ())
493 return -1;
40838a72
AC
494 return sts;
495}
496
25b88f33
PP
497static void *
498cache_bmmap (struct bfd *abfd ATTRIBUTE_UNUSED,
499 void *addr ATTRIBUTE_UNUSED,
4810a2d9 500 size_t len ATTRIBUTE_UNUSED,
25b88f33
PP
501 int prot ATTRIBUTE_UNUSED,
502 int flags ATTRIBUTE_UNUSED,
4c95ab76 503 file_ptr offset ATTRIBUTE_UNUSED,
07d6d2b8 504 void **map_addr ATTRIBUTE_UNUSED,
4810a2d9 505 size_t *map_len ATTRIBUTE_UNUSED)
25b88f33 506{
2e384d4f 507 void *ret = MAP_FAILED;
25b88f33 508
1185b5b7
TT
509 if (!bfd_lock ())
510 return ret;
25b88f33
PP
511 if ((abfd->flags & BFD_IN_MEMORY) != 0)
512 abort ();
513#ifdef HAVE_MMAP
514 else
515 {
9ba56ace 516 uintptr_t pagesize_m1 = _bfd_pagesize_m1;
4c95ab76
TG
517 FILE *f;
518 file_ptr pg_offset;
9ba56ace 519 size_t pg_len;
4c95ab76
TG
520
521 f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR);
25b88f33 522 if (f == NULL)
1185b5b7
TT
523 {
524 bfd_unlock ();
525 return ret;
526 }
25b88f33 527
4c95ab76
TG
528 /* Align. */
529 pg_offset = offset & ~pagesize_m1;
530 pg_len = (len + (offset - pg_offset) + pagesize_m1) & ~pagesize_m1;
531
532 ret = mmap (addr, pg_len, prot, flags, fileno (f), pg_offset);
2e384d4f 533 if (ret == MAP_FAILED)
25b88f33 534 bfd_set_error (bfd_error_system_call);
4c95ab76 535 else
07d6d2b8
AM
536 {
537 *map_addr = ret;
538 *map_len = pg_len;
539 ret = (char *) ret + (offset & pagesize_m1);
540 }
25b88f33
PP
541 }
542#endif
543
1185b5b7 544 if (!bfd_unlock ())
2e384d4f 545 return MAP_FAILED;
25b88f33
PP
546 return ret;
547}
548
cb5220a0
NC
549static const struct bfd_iovec cache_iovec =
550{
40838a72 551 &cache_bread, &cache_bwrite, &cache_btell, &cache_bseek,
25b88f33 552 &cache_bclose, &cache_bflush, &cache_bstat, &cache_bmmap
40838a72
AC
553};
554
1185b5b7
TT
555static bool
556_bfd_cache_init_unlocked (bfd *abfd)
557{
558 BFD_ASSERT (abfd->iostream != NULL);
559 if (open_files >= bfd_cache_max_open ())
560 {
561 if (! close_one ())
562 return false;
563 }
564 abfd->iovec = &cache_iovec;
565 insert (abfd);
566 abfd->flags &= ~BFD_CLOSED_BY_CACHE;
567 ++open_files;
568 return true;
569}
570
252b5132
RH
571/*
572INTERNAL_FUNCTION
573 bfd_cache_init
574
575SYNOPSIS
0a1b45a2 576 bool bfd_cache_init (bfd *abfd);
252b5132
RH
577
578DESCRIPTION
579 Add a newly opened BFD to the cache.
580*/
581
0a1b45a2 582bool
c58b9523 583bfd_cache_init (bfd *abfd)
252b5132 584{
1185b5b7
TT
585 if (!bfd_lock ())
586 return false;
587 bool result = _bfd_cache_init_unlocked (abfd);
588 if (!bfd_unlock ())
589 return false;
590 return result;
591}
592
593static bool
594_bfd_cache_close_unlocked (bfd *abfd)
595{
596 /* Don't remove this test. bfd_reinit depends on it. */
597 if (abfd->iovec != &cache_iovec)
598 return true;
599
600 if (abfd->iostream == NULL)
601 /* Previously closed. */
602 return true;
603
604 /* Note: no locking needed in this function, as it is handled by
605 bfd_cache_delete. */
606 return bfd_cache_delete (abfd);
252b5132
RH
607}
608
609/*
717d4bd6 610FUNCTION
252b5132
RH
611 bfd_cache_close
612
613SYNOPSIS
0a1b45a2 614 bool bfd_cache_close (bfd *abfd);
252b5132
RH
615
616DESCRIPTION
617 Remove the BFD @var{abfd} from the cache. If the attached file is open,
618 then close it too.
619
b34976b6 620 <<FALSE>> is returned if closing the file fails, <<TRUE>> is
252b5132
RH
621 returned if all is well.
622*/
623
0a1b45a2 624bool
c58b9523 625bfd_cache_close (bfd *abfd)
252b5132 626{
1185b5b7
TT
627 if (!bfd_lock ())
628 return false;
629 bool result = _bfd_cache_close_unlocked (abfd);
630 if (!bfd_unlock ())
631 return false;
632 return result;
252b5132
RH
633}
634
02d5a37b
JG
635/*
636FUNCTION
637 bfd_cache_close_all
638
639SYNOPSIS
0a1b45a2 640 bool bfd_cache_close_all (void);
02d5a37b
JG
641
642DESCRIPTION
643 Remove all BFDs from the cache. If the attached file is open,
0133072f
NC
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.
02d5a37b 647
02d5a37b
JG
648 <<FALSE>> is returned if closing one of the file fails, <<TRUE>> is
649 returned if all is well.
650*/
651
0a1b45a2 652bool
e6c7cdec 653bfd_cache_close_all (void)
02d5a37b 654{
0a1b45a2 655 bool ret = true;
02d5a37b 656
1185b5b7
TT
657 if (!bfd_lock ())
658 return false;
02d5a37b 659 while (bfd_last_cache != NULL)
0133072f
NC
660 {
661 bfd *prev_bfd_last_cache = bfd_last_cache;
662
1185b5b7 663 ret &= _bfd_cache_close_unlocked (bfd_last_cache);
0133072f
NC
664
665 /* Stop a potential infinite loop should bfd_cache_close()
666 not update bfd_last_cache. */
667 if (bfd_last_cache == prev_bfd_last_cache)
668 break;
669 }
c9b549b2 670
1185b5b7
TT
671 if (!bfd_unlock ())
672 return false;
c9b549b2 673 return ret;
02d5a37b
JG
674}
675
20bf7711
TT
676/*
677INTERNAL_FUNCTION
678 bfd_cache_set_uncloseable
679
680SYNOPSIS
681 bool bfd_cache_set_uncloseable (bfd *abfd, bool value, bool *old);
682
683DESCRIPTION
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
693bool
694bfd_cache_set_uncloseable (bfd *abfd, bool value, bool *old)
695{
696 bool result = true;
697
698 if (!bfd_lock ())
699 return false;
700 if (old != NULL)
701 *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 if (value != abfd->in_format_matches
706 && abfd->iovec == &cache_iovec
707 && possibly_cached (abfd))
708 {
709 if (value)
710 {
711 /* Marking as uncloseable for the first time. Ensure the
712 file is open, and remove from the cache list. */
713 FILE *f = bfd_cache_lookup (abfd, CACHE_NORMAL);
714 if (f == NULL)
715 result = false;
716 else
717 snip (abfd);
718 }
719 else
720 {
721 /* Mark as closeable again. */
722 insert (abfd);
723 }
724
725 abfd->in_format_matches = value;
726 }
727
728 if (!bfd_unlock ())
729 return false;
730 return result;
731}
732
b8ead7d5
AB
733/*
734FUNCTION
735 bfd_cache_size
736
737SYNOPSIS
738 unsigned bfd_cache_size (void);
739
740DESCRIPTION
741 Return the number of open files in the cache.
742*/
743
744unsigned
745bfd_cache_size (void)
746{
747 return open_files;
748}
749
1185b5b7
TT
750static FILE *
751_bfd_open_file_unlocked (bfd *abfd)
252b5132 752{
0a1b45a2 753 abfd->cacheable = true; /* Allow it to be closed later. */
252b5132 754
9d782e8d 755 if (open_files >= bfd_cache_max_open ())
252b5132
RH
756 {
757 if (! close_one ())
758 return NULL;
759 }
760
761 switch (abfd->direction)
762 {
763 case read_direction:
764 case no_direction:
765cf5f6 765 abfd->iostream = _bfd_real_fopen (bfd_get_filename (abfd), FOPEN_RB);
252b5132
RH
766 break;
767 case both_direction:
768 case write_direction:
82e51918 769 if (abfd->opened_once)
252b5132 770 {
765cf5f6
AM
771 abfd->iostream = _bfd_real_fopen (bfd_get_filename (abfd),
772 FOPEN_RUB);
252b5132 773 if (abfd->iostream == NULL)
765cf5f6
AM
774 abfd->iostream = _bfd_real_fopen (bfd_get_filename (abfd),
775 FOPEN_WUB);
252b5132
RH
776 }
777 else
778 {
9e422a2e
ILT
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. */
5af11cab
AM
794#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. */
9e422a2e
ILT
800 struct stat s;
801
765cf5f6
AM
802 if (stat (bfd_get_filename (abfd), &s) == 0 && s.st_size != 0)
803 unlink_if_ordinary (bfd_get_filename (abfd));
5af11cab 804#endif
765cf5f6
AM
805 abfd->iostream = _bfd_real_fopen (bfd_get_filename (abfd),
806 FOPEN_WUB);
0a1b45a2 807 abfd->opened_once = true;
252b5132
RH
808 }
809 break;
810 }
811
5c91cdfb
AM
812 if (abfd->iostream == NULL)
813 bfd_set_error (bfd_error_system_call);
814 else
252b5132 815 {
1185b5b7 816 if (! _bfd_cache_init_unlocked (abfd))
252b5132
RH
817 return NULL;
818 }
819
820 return (FILE *) abfd->iostream;
821}
1185b5b7
TT
822
823/*
824INTERNAL_FUNCTION
825 bfd_open_file
826
827SYNOPSIS
828 FILE* bfd_open_file (bfd *abfd);
829
830DESCRIPTION
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
838FILE *
839bfd_open_file (bfd *abfd)
840{
841 if (!bfd_lock ())
842 return NULL;
843 FILE *result = _bfd_open_file_unlocked (abfd);
844 if (!bfd_unlock ())
845 return NULL;
846 return result;
847}