]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/cache.c
Fix potential infinite loop in bfd_cache_close_all.
[thirdparty/binutils-gdb.git] / bfd / cache.c
CommitLineData
252b5132 1/* BFD library -- caching of file descriptors.
7c192733 2
d87bef3a 3 Copyright (C) 1990-2023 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
25b88f33
PP
48#ifdef HAVE_MMAP
49#include <sys/mman.h>
50#endif
51
95560129
AM
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. */
60enum cache_flag {
61 CACHE_NORMAL = 0,
62 CACHE_NO_OPEN = 1,
63 CACHE_NO_SEEK = 2,
64 CACHE_NO_SEEK_ERROR = 4
65};
66
d00967c7 67/* The maximum number of files which the cache will keep open at
9d782e8d 68 one time. When needed call bfd_cache_max_open to initialize. */
d0fdd288 69
9d782e8d
MW
70static 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. */
74static int
75bfd_cache_max_open (void)
76{
77 if (max_open_files == 0)
78 {
79 int max;
0b1fa288
ST
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
07d6d2b8 84 in "Too many open files" errors. This can happen here even though
0b1fa288
ST
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
07d6d2b8 90 for 32-bit Solaris only. (It is hoped that the 32-bit libc
0b1fa288
ST
91 limitation will be removed soon). 64-bit Solaris libc does not have
92 this limitation. */
93 max = 16;
94#else
9d782e8d
MW
95#ifdef HAVE_GETRLIMIT
96 struct rlimit rlim;
0b1fa288 97
9d782e8d 98 if (getrlimit (RLIMIT_NOFILE, &rlim) == 0
d1eb5696 99 && rlim.rlim_cur != (rlim_t) RLIM_INFINITY)
9d782e8d
MW
100 max = rlim.rlim_cur / 8;
101 else
0b1fa288 102#endif
9d782e8d
MW
103#ifdef _SC_OPEN_MAX
104 max = sysconf (_SC_OPEN_MAX) / 8;
105#else
07d6d2b8 106 max = 10;
0b1fa288
ST
107#endif
108#endif /* not 32-bit Solaris */
109
9d782e8d
MW
110 max_open_files = max < 10 ? 10 : max;
111 }
112
113 return max_open_files;
114}
d0fdd288
AM
115
116/* The number of BFD files we have open. */
117
118static int open_files;
119
d00967c7
AM
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. */
d0fdd288 123
d00967c7 124static bfd *bfd_last_cache = NULL;
252b5132 125
d0fdd288
AM
126/* Insert a BFD into the cache. */
127
128static void
129insert (bfd *abfd)
130{
131 if (bfd_last_cache == NULL)
132 {
133 abfd->lru_next = abfd;
134 abfd->lru_prev = abfd;
135 }
136 else
137 {
138 abfd->lru_next = bfd_last_cache;
139 abfd->lru_prev = bfd_last_cache->lru_prev;
140 abfd->lru_prev->lru_next = abfd;
141 abfd->lru_next->lru_prev = abfd;
142 }
143 bfd_last_cache = abfd;
144}
145
146/* Remove a BFD from the cache. */
147
148static void
149snip (bfd *abfd)
150{
151 abfd->lru_prev->lru_next = abfd->lru_next;
152 abfd->lru_next->lru_prev = abfd->lru_prev;
153 if (abfd == bfd_last_cache)
154 {
155 bfd_last_cache = abfd->lru_next;
156 if (abfd == bfd_last_cache)
157 bfd_last_cache = NULL;
158 }
159}
160
161/* Close a BFD and remove it from the cache. */
162
0a1b45a2 163static bool
d0fdd288
AM
164bfd_cache_delete (bfd *abfd)
165{
0a1b45a2 166 bool ret;
d0fdd288
AM
167
168 if (fclose ((FILE *) abfd->iostream) == 0)
0a1b45a2 169 ret = true;
d0fdd288
AM
170 else
171 {
0a1b45a2 172 ret = false;
d0fdd288
AM
173 bfd_set_error (bfd_error_system_call);
174 }
175
176 snip (abfd);
177
178 abfd->iostream = NULL;
179 --open_files;
a6ad7914 180 abfd->flags |= BFD_CLOSED_BY_CACHE;
d0fdd288
AM
181
182 return ret;
183}
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
0a1b45a2 188static bool
d0fdd288
AM
189close_one (void)
190{
f664f618 191 register bfd *to_kill;
d0fdd288
AM
192
193 if (bfd_last_cache == NULL)
f664f618 194 to_kill = NULL;
d0fdd288
AM
195 else
196 {
f664f618
TG
197 for (to_kill = bfd_last_cache->lru_prev;
198 ! to_kill->cacheable;
199 to_kill = to_kill->lru_prev)
d0fdd288 200 {
f664f618 201 if (to_kill == bfd_last_cache)
d0fdd288 202 {
f664f618 203 to_kill = NULL;
d0fdd288
AM
204 break;
205 }
206 }
207 }
208
f664f618 209 if (to_kill == NULL)
d0fdd288
AM
210 {
211 /* There are no open cacheable BFD's. */
0a1b45a2 212 return true;
d0fdd288
AM
213 }
214
c7c3d11b 215 to_kill->where = _bfd_real_ftell ((FILE *) to_kill->iostream);
d0fdd288 216
f664f618 217 return bfd_cache_delete (to_kill);
d0fdd288
AM
218}
219
d00967c7
AM
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. */
d0fdd288 224
95560129 225#define bfd_cache_lookup(x, flag) \
d00967c7
AM
226 ((x) == bfd_last_cache \
227 ? (FILE *) (bfd_last_cache->iostream) \
95560129 228 : bfd_cache_lookup_worker (x, flag))
d0fdd288 229
d00967c7
AM
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
9d782e8d 233 <<bfd_cache_max_open>> files open, it tries to close one first, to
d00967c7
AM
234 avoid running out of file descriptors. It will return NULL
235 if it is unable to (re)open the @var{abfd}. */
d0fdd288 236
d00967c7 237static FILE *
95560129 238bfd_cache_lookup_worker (bfd *abfd, enum cache_flag flag)
d0fdd288 239{
d0fdd288
AM
240 if ((abfd->flags & BFD_IN_MEMORY) != 0)
241 abort ();
242
5c4ce239
AM
243 if (abfd->my_archive != NULL
244 && !bfd_is_thin_archive (abfd->my_archive))
245 abort ();
d0fdd288
AM
246
247 if (abfd->iostream != NULL)
248 {
249 /* Move the file to the start of the cache. */
250 if (abfd != bfd_last_cache)
251 {
252 snip (abfd);
253 insert (abfd);
254 }
255 return (FILE *) abfd->iostream;
256 }
257
95560129
AM
258 if (flag & CACHE_NO_OPEN)
259 return NULL;
260
d0fdd288
AM
261 if (bfd_open_file (abfd) == NULL)
262 ;
95560129 263 else if (!(flag & CACHE_NO_SEEK)
c7c3d11b
PA
264 && _bfd_real_fseek ((FILE *) abfd->iostream,
265 abfd->where, SEEK_SET) != 0
95560129 266 && !(flag & CACHE_NO_SEEK_ERROR))
d0fdd288
AM
267 bfd_set_error (bfd_error_system_call);
268 else
f656f9c7 269 return (FILE *) abfd->iostream;
d0fdd288 270
695344c0 271 /* xgettext:c-format */
a6ad7914 272 _bfd_error_handler (_("reopening %pB: %s"),
5c4ce239 273 abfd, bfd_errmsg (bfd_get_error ()));
d0fdd288
AM
274 return NULL;
275}
40838a72
AC
276
277static file_ptr
278cache_btell (struct bfd *abfd)
279{
95560129 280 FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
3dff57e8 281 if (f == NULL)
95560129 282 return abfd->where;
c7c3d11b 283 return _bfd_real_ftell (f);
40838a72
AC
284}
285
286static int
287cache_bseek (struct bfd *abfd, file_ptr offset, int whence)
288{
a50b1753 289 FILE *f = bfd_cache_lookup (abfd, whence != SEEK_CUR ? CACHE_NO_SEEK : CACHE_NORMAL);
3dff57e8
AM
290 if (f == NULL)
291 return -1;
c7c3d11b 292 return _bfd_real_fseek (f, offset, whence);
40838a72
AC
293}
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
302static file_ptr
5c4ce239 303cache_bread_1 (FILE *f, void *buf, file_ptr nbytes)
40838a72
AC
304{
305 file_ptr nread;
3dff57e8 306
40838a72
AC
307#if defined (__VAX) && defined (VMS)
308 /* Apparently fread on Vax VMS does not keep the record length
309 information. */
3dff57e8 310 nread = read (fileno (f), buf, nbytes);
40838a72
AC
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);
87f14779 317 return nread;
40838a72
AC
318 }
319#else
3dff57e8 320 nread = fread (buf, 1, nbytes, f);
40838a72
AC
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. */
3dff57e8 324 if (nread < nbytes && ferror (f))
40838a72
AC
325 {
326 bfd_set_error (bfd_error_system_call);
87f14779 327 return nread;
40838a72
AC
328 }
329#endif
662ed161
DJ
330 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 bfd_set_error (bfd_error_file_truncated);
40838a72
AC
334 return nread;
335}
336
f12a02c0
JB
337static file_ptr
338cache_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
339{
340 file_ptr nread = 0;
5c4ce239
AM
341 FILE *f;
342
343 f = bfd_cache_lookup (abfd, CACHE_NORMAL);
344 if (f == NULL)
345 return -1;
f12a02c0
JB
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 while (nread < nbytes)
351 {
352 const file_ptr max_chunk_size = 0x800000;
353 file_ptr chunk_size = nbytes - nread;
354 file_ptr chunk_nread;
355
356 if (chunk_size > max_chunk_size)
07d6d2b8 357 chunk_size = max_chunk_size;
f12a02c0 358
5c4ce239 359 chunk_nread = cache_bread_1 (f, (char *) buf + nread, chunk_size);
f12a02c0
JB
360
361 /* Update the nread count.
362
07d6d2b8
AM
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. */
f12a02c0 369 if (nread == 0 || chunk_nread > 0)
07d6d2b8 370 nread += chunk_nread;
f12a02c0
JB
371
372 if (chunk_nread < chunk_size)
07d6d2b8 373 break;
f12a02c0
JB
374 }
375
376 return nread;
377}
378
40838a72 379static file_ptr
5c4ce239 380cache_bwrite (struct bfd *abfd, const void *from, file_ptr nbytes)
40838a72 381{
3dff57e8 382 file_ptr nwrite;
a50b1753 383 FILE *f = bfd_cache_lookup (abfd, CACHE_NORMAL);
cb5220a0 384
3dff57e8
AM
385 if (f == NULL)
386 return 0;
5c4ce239 387 nwrite = fwrite (from, 1, nbytes, f);
3dff57e8 388 if (nwrite < nbytes && ferror (f))
40838a72
AC
389 {
390 bfd_set_error (bfd_error_system_call);
391 return -1;
392 }
393 return nwrite;
394}
395
405bf443 396static int
40838a72
AC
397cache_bclose (struct bfd *abfd)
398{
405bf443 399 return bfd_cache_close (abfd) - 1;
40838a72
AC
400}
401
402static int
403cache_bflush (struct bfd *abfd)
404{
3dff57e8 405 int sts;
95560129 406 FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
cb5220a0 407
3dff57e8 408 if (f == NULL)
95560129 409 return 0;
3dff57e8 410 sts = fflush (f);
40838a72
AC
411 if (sts < 0)
412 bfd_set_error (bfd_error_system_call);
413 return sts;
414}
415
416static int
417cache_bstat (struct bfd *abfd, struct stat *sb)
418{
3dff57e8 419 int sts;
95560129 420 FILE *f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR);
cb5220a0 421
3dff57e8
AM
422 if (f == NULL)
423 return -1;
424 sts = fstat (fileno (f), sb);
40838a72
AC
425 if (sts < 0)
426 bfd_set_error (bfd_error_system_call);
427 return sts;
428}
429
25b88f33
PP
430static void *
431cache_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,
4c95ab76 436 file_ptr offset ATTRIBUTE_UNUSED,
07d6d2b8
AM
437 void **map_addr ATTRIBUTE_UNUSED,
438 bfd_size_type *map_len ATTRIBUTE_UNUSED)
25b88f33
PP
439{
440 void *ret = (void *) -1;
441
442 if ((abfd->flags & BFD_IN_MEMORY) != 0)
443 abort ();
444#ifdef HAVE_MMAP
445 else
446 {
48d96f80 447 static uintptr_t pagesize_m1;
4c95ab76
TG
448 FILE *f;
449 file_ptr pg_offset;
450 bfd_size_type pg_len;
451
452 f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR);
25b88f33
PP
453 if (f == NULL)
454 return ret;
455
48d96f80 456 if (pagesize_m1 == 0)
07d6d2b8 457 pagesize_m1 = getpagesize () - 1;
48d96f80 458
4c95ab76
TG
459 /* Align. */
460 pg_offset = offset & ~pagesize_m1;
461 pg_len = (len + (offset - pg_offset) + pagesize_m1) & ~pagesize_m1;
462
463 ret = mmap (addr, pg_len, prot, flags, fileno (f), pg_offset);
25b88f33
PP
464 if (ret == (void *) -1)
465 bfd_set_error (bfd_error_system_call);
4c95ab76 466 else
07d6d2b8
AM
467 {
468 *map_addr = ret;
469 *map_len = pg_len;
470 ret = (char *) ret + (offset & pagesize_m1);
471 }
25b88f33
PP
472 }
473#endif
474
475 return ret;
476}
477
cb5220a0
NC
478static const struct bfd_iovec cache_iovec =
479{
40838a72 480 &cache_bread, &cache_bwrite, &cache_btell, &cache_bseek,
25b88f33 481 &cache_bclose, &cache_bflush, &cache_bstat, &cache_bmmap
40838a72
AC
482};
483
252b5132
RH
484/*
485INTERNAL_FUNCTION
486 bfd_cache_init
487
488SYNOPSIS
0a1b45a2 489 bool bfd_cache_init (bfd *abfd);
252b5132
RH
490
491DESCRIPTION
492 Add a newly opened BFD to the cache.
493*/
494
0a1b45a2 495bool
c58b9523 496bfd_cache_init (bfd *abfd)
252b5132
RH
497{
498 BFD_ASSERT (abfd->iostream != NULL);
9d782e8d 499 if (open_files >= bfd_cache_max_open ())
252b5132
RH
500 {
501 if (! close_one ())
0a1b45a2 502 return false;
252b5132 503 }
40838a72 504 abfd->iovec = &cache_iovec;
252b5132 505 insert (abfd);
f656f9c7 506 abfd->flags &= ~BFD_CLOSED_BY_CACHE;
252b5132 507 ++open_files;
0a1b45a2 508 return true;
252b5132
RH
509}
510
511/*
717d4bd6 512FUNCTION
252b5132
RH
513 bfd_cache_close
514
515SYNOPSIS
0a1b45a2 516 bool bfd_cache_close (bfd *abfd);
252b5132
RH
517
518DESCRIPTION
519 Remove the BFD @var{abfd} from the cache. If the attached file is open,
520 then close it too.
521
b34976b6 522 <<FALSE>> is returned if closing the file fails, <<TRUE>> is
252b5132
RH
523 returned if all is well.
524*/
525
0a1b45a2 526bool
c58b9523 527bfd_cache_close (bfd *abfd)
252b5132 528{
f656f9c7 529 /* Don't remove this test. bfd_reinit depends on it. */
40838a72 530 if (abfd->iovec != &cache_iovec)
0a1b45a2 531 return true;
252b5132 532
fe2e161a
AC
533 if (abfd->iostream == NULL)
534 /* Previously closed. */
0a1b45a2 535 return true;
fe2e161a 536
252b5132
RH
537 return bfd_cache_delete (abfd);
538}
539
02d5a37b
JG
540/*
541FUNCTION
542 bfd_cache_close_all
543
544SYNOPSIS
0a1b45a2 545 bool bfd_cache_close_all (void);
02d5a37b
JG
546
547DESCRIPTION
548 Remove all BFDs from the cache. If the attached file is open,
0133072f
NC
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.
02d5a37b 552
02d5a37b
JG
553 <<FALSE>> is returned if closing one of the file fails, <<TRUE>> is
554 returned if all is well.
555*/
556
0a1b45a2 557bool
e6c7cdec 558bfd_cache_close_all (void)
02d5a37b 559{
0a1b45a2 560 bool ret = true;
02d5a37b
JG
561
562 while (bfd_last_cache != NULL)
0133072f
NC
563 {
564 bfd *prev_bfd_last_cache = bfd_last_cache;
565
566 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 if (bfd_last_cache == prev_bfd_last_cache)
571 break;
572 }
c9b549b2
JG
573
574 return ret;
02d5a37b
JG
575}
576
252b5132
RH
577/*
578INTERNAL_FUNCTION
579 bfd_open_file
580
581SYNOPSIS
c58b9523 582 FILE* bfd_open_file (bfd *abfd);
252b5132
RH
583
584DESCRIPTION
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
592FILE *
c58b9523 593bfd_open_file (bfd *abfd)
252b5132 594{
0a1b45a2 595 abfd->cacheable = true; /* Allow it to be closed later. */
252b5132 596
9d782e8d 597 if (open_files >= bfd_cache_max_open ())
252b5132
RH
598 {
599 if (! close_one ())
600 return NULL;
601 }
602
603 switch (abfd->direction)
604 {
605 case read_direction:
606 case no_direction:
765cf5f6 607 abfd->iostream = _bfd_real_fopen (bfd_get_filename (abfd), FOPEN_RB);
252b5132
RH
608 break;
609 case both_direction:
610 case write_direction:
82e51918 611 if (abfd->opened_once)
252b5132 612 {
765cf5f6
AM
613 abfd->iostream = _bfd_real_fopen (bfd_get_filename (abfd),
614 FOPEN_RUB);
252b5132 615 if (abfd->iostream == NULL)
765cf5f6
AM
616 abfd->iostream = _bfd_real_fopen (bfd_get_filename (abfd),
617 FOPEN_WUB);
252b5132
RH
618 }
619 else
620 {
9e422a2e
ILT
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. */
5af11cab
AM
636#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. */
9e422a2e
ILT
642 struct stat s;
643
765cf5f6
AM
644 if (stat (bfd_get_filename (abfd), &s) == 0 && s.st_size != 0)
645 unlink_if_ordinary (bfd_get_filename (abfd));
5af11cab 646#endif
765cf5f6
AM
647 abfd->iostream = _bfd_real_fopen (bfd_get_filename (abfd),
648 FOPEN_WUB);
0a1b45a2 649 abfd->opened_once = true;
252b5132
RH
650 }
651 break;
652 }
653
5c91cdfb
AM
654 if (abfd->iostream == NULL)
655 bfd_set_error (bfd_error_system_call);
656 else
252b5132
RH
657 {
658 if (! bfd_cache_init (abfd))
659 return NULL;
660 }
661
662 return (FILE *) abfd->iostream;
663}