Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* BFD library -- caching of file descriptors. |
7c192733 AC |
2 | |
3 | Copyright 1990, 1991, 1992, 1993, 1994, 1996, 2000, 2001, 2002, | |
aa820537 | 4 | 2003, 2004, 2005, 2007, 2008, 2009 Free Software Foundation, Inc. |
7c192733 | 5 | |
252b5132 RH |
6 | Hacked by Steve Chamberlain of Cygnus Support (steve@cygnus.com). |
7 | ||
cd123cb7 NC |
8 | This file is part of BFD, the Binary File Descriptor library. |
9 | ||
10 | This program is free software; you can redistribute it and/or modify | |
11 | it under the terms of the GNU General Public License as published by | |
12 | the Free Software Foundation; either version 3 of the License, or | |
13 | (at your option) any later version. | |
14 | ||
15 | This program is distributed in the hope that it will be useful, | |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | GNU General Public License for more details. | |
19 | ||
20 | You should have received a copy of the GNU General Public License | |
21 | along with this program; if not, write to the Free Software | |
22 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
23 | MA 02110-1301, USA. */ | |
252b5132 RH |
24 | |
25 | /* | |
26 | SECTION | |
27 | File caching | |
28 | ||
29 | The file caching mechanism is embedded within BFD and allows | |
30 | the application to open as many BFDs as it wants without | |
31 | regard to the underlying operating system's file descriptor | |
32 | limit (often as low as 20 open files). The module in | |
33 | <<cache.c>> maintains a least recently used list of | |
34 | <<BFD_CACHE_MAX_OPEN>> files, and exports the name | |
35 | <<bfd_cache_lookup>>, which runs around and makes sure that | |
36 | the required BFD is open. If not, then it chooses a file to | |
37 | close, closes it and opens the one wanted, returning its file | |
e60b52c6 | 38 | handle. |
252b5132 | 39 | |
1b74d094 BW |
40 | SUBSECTION |
41 | Caching functions | |
252b5132 RH |
42 | */ |
43 | ||
252b5132 | 44 | #include "sysdep.h" |
3db64b00 | 45 | #include "bfd.h" |
252b5132 | 46 | #include "libbfd.h" |
bb14f524 | 47 | #include "libiberty.h" |
4c95ab76 | 48 | #include "bfd_stdint.h" |
252b5132 | 49 | |
25b88f33 PP |
50 | #ifdef HAVE_MMAP |
51 | #include <sys/mman.h> | |
52 | #endif | |
53 | ||
95560129 AM |
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 | |
61 | the stat. */ | |
62 | enum cache_flag { | |
63 | CACHE_NORMAL = 0, | |
64 | CACHE_NO_OPEN = 1, | |
65 | CACHE_NO_SEEK = 2, | |
66 | CACHE_NO_SEEK_ERROR = 4 | |
67 | }; | |
68 | ||
d00967c7 AM |
69 | /* The maximum number of files which the cache will keep open at |
70 | one time. */ | |
d0fdd288 | 71 | |
d00967c7 | 72 | #define BFD_CACHE_MAX_OPEN 10 |
d0fdd288 AM |
73 | |
74 | /* The number of BFD files we have open. */ | |
75 | ||
76 | static int open_files; | |
77 | ||
d00967c7 AM |
78 | /* Zero, or a pointer to the topmost BFD on the chain. This is |
79 | used by the <<bfd_cache_lookup>> macro in @file{libbfd.h} to | |
80 | determine when it can avoid a function call. */ | |
d0fdd288 | 81 | |
d00967c7 | 82 | static bfd *bfd_last_cache = NULL; |
252b5132 | 83 | |
d0fdd288 AM |
84 | /* Insert a BFD into the cache. */ |
85 | ||
86 | static void | |
87 | insert (bfd *abfd) | |
88 | { | |
89 | if (bfd_last_cache == NULL) | |
90 | { | |
91 | abfd->lru_next = abfd; | |
92 | abfd->lru_prev = abfd; | |
93 | } | |
94 | else | |
95 | { | |
96 | abfd->lru_next = bfd_last_cache; | |
97 | abfd->lru_prev = bfd_last_cache->lru_prev; | |
98 | abfd->lru_prev->lru_next = abfd; | |
99 | abfd->lru_next->lru_prev = abfd; | |
100 | } | |
101 | bfd_last_cache = abfd; | |
102 | } | |
103 | ||
104 | /* Remove a BFD from the cache. */ | |
105 | ||
106 | static void | |
107 | snip (bfd *abfd) | |
108 | { | |
109 | abfd->lru_prev->lru_next = abfd->lru_next; | |
110 | abfd->lru_next->lru_prev = abfd->lru_prev; | |
111 | if (abfd == bfd_last_cache) | |
112 | { | |
113 | bfd_last_cache = abfd->lru_next; | |
114 | if (abfd == bfd_last_cache) | |
115 | bfd_last_cache = NULL; | |
116 | } | |
117 | } | |
118 | ||
119 | /* Close a BFD and remove it from the cache. */ | |
120 | ||
121 | static bfd_boolean | |
122 | bfd_cache_delete (bfd *abfd) | |
123 | { | |
124 | bfd_boolean ret; | |
125 | ||
126 | if (fclose ((FILE *) abfd->iostream) == 0) | |
127 | ret = TRUE; | |
128 | else | |
129 | { | |
130 | ret = FALSE; | |
131 | bfd_set_error (bfd_error_system_call); | |
132 | } | |
133 | ||
134 | snip (abfd); | |
135 | ||
136 | abfd->iostream = NULL; | |
137 | --open_files; | |
138 | ||
139 | return ret; | |
140 | } | |
141 | ||
142 | /* We need to open a new file, and the cache is full. Find the least | |
143 | recently used cacheable BFD and close it. */ | |
144 | ||
145 | static bfd_boolean | |
146 | close_one (void) | |
147 | { | |
f664f618 | 148 | register bfd *to_kill; |
d0fdd288 AM |
149 | |
150 | if (bfd_last_cache == NULL) | |
f664f618 | 151 | to_kill = NULL; |
d0fdd288 AM |
152 | else |
153 | { | |
f664f618 TG |
154 | for (to_kill = bfd_last_cache->lru_prev; |
155 | ! to_kill->cacheable; | |
156 | to_kill = to_kill->lru_prev) | |
d0fdd288 | 157 | { |
f664f618 | 158 | if (to_kill == bfd_last_cache) |
d0fdd288 | 159 | { |
f664f618 | 160 | to_kill = NULL; |
d0fdd288 AM |
161 | break; |
162 | } | |
163 | } | |
164 | } | |
165 | ||
f664f618 | 166 | if (to_kill == NULL) |
d0fdd288 AM |
167 | { |
168 | /* There are no open cacheable BFD's. */ | |
169 | return TRUE; | |
170 | } | |
171 | ||
f664f618 | 172 | to_kill->where = real_ftell ((FILE *) to_kill->iostream); |
d0fdd288 | 173 | |
f664f618 | 174 | return bfd_cache_delete (to_kill); |
d0fdd288 AM |
175 | } |
176 | ||
d00967c7 AM |
177 | /* Check to see if the required BFD is the same as the last one |
178 | looked up. If so, then it can use the stream in the BFD with | |
179 | impunity, since it can't have changed since the last lookup; | |
180 | otherwise, it has to perform the complicated lookup function. */ | |
d0fdd288 | 181 | |
95560129 | 182 | #define bfd_cache_lookup(x, flag) \ |
d00967c7 AM |
183 | ((x) == bfd_last_cache \ |
184 | ? (FILE *) (bfd_last_cache->iostream) \ | |
95560129 | 185 | : bfd_cache_lookup_worker (x, flag)) |
d0fdd288 | 186 | |
d00967c7 AM |
187 | /* Called when the macro <<bfd_cache_lookup>> fails to find a |
188 | quick answer. Find a file descriptor for @var{abfd}. If | |
189 | necessary, it open it. If there are already more than | |
190 | <<BFD_CACHE_MAX_OPEN>> files open, it tries to close one first, to | |
191 | avoid running out of file descriptors. It will return NULL | |
192 | if it is unable to (re)open the @var{abfd}. */ | |
d0fdd288 | 193 | |
d00967c7 | 194 | static FILE * |
95560129 | 195 | bfd_cache_lookup_worker (bfd *abfd, enum cache_flag flag) |
d0fdd288 AM |
196 | { |
197 | bfd *orig_bfd = abfd; | |
198 | if ((abfd->flags & BFD_IN_MEMORY) != 0) | |
199 | abort (); | |
200 | ||
201 | if (abfd->my_archive) | |
202 | abfd = abfd->my_archive; | |
203 | ||
204 | if (abfd->iostream != NULL) | |
205 | { | |
206 | /* Move the file to the start of the cache. */ | |
207 | if (abfd != bfd_last_cache) | |
208 | { | |
209 | snip (abfd); | |
210 | insert (abfd); | |
211 | } | |
212 | return (FILE *) abfd->iostream; | |
213 | } | |
214 | ||
95560129 AM |
215 | if (flag & CACHE_NO_OPEN) |
216 | return NULL; | |
217 | ||
d0fdd288 AM |
218 | if (bfd_open_file (abfd) == NULL) |
219 | ; | |
95560129 AM |
220 | else if (!(flag & CACHE_NO_SEEK) |
221 | && real_fseek ((FILE *) abfd->iostream, abfd->where, SEEK_SET) != 0 | |
222 | && !(flag & CACHE_NO_SEEK_ERROR)) | |
d0fdd288 AM |
223 | bfd_set_error (bfd_error_system_call); |
224 | else | |
225 | return (FILE *) abfd->iostream; | |
226 | ||
227 | (*_bfd_error_handler) (_("reopening %B: %s\n"), | |
228 | orig_bfd, bfd_errmsg (bfd_get_error ())); | |
229 | return NULL; | |
230 | } | |
40838a72 AC |
231 | |
232 | static file_ptr | |
233 | cache_btell (struct bfd *abfd) | |
234 | { | |
95560129 | 235 | FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN); |
3dff57e8 | 236 | if (f == NULL) |
95560129 | 237 | return abfd->where; |
3dff57e8 | 238 | return real_ftell (f); |
40838a72 AC |
239 | } |
240 | ||
241 | static int | |
242 | cache_bseek (struct bfd *abfd, file_ptr offset, int whence) | |
243 | { | |
a50b1753 | 244 | FILE *f = bfd_cache_lookup (abfd, whence != SEEK_CUR ? CACHE_NO_SEEK : CACHE_NORMAL); |
3dff57e8 AM |
245 | if (f == NULL) |
246 | return -1; | |
247 | return real_fseek (f, offset, whence); | |
40838a72 AC |
248 | } |
249 | ||
250 | /* Note that archive entries don't have streams; they share their parent's. | |
251 | This allows someone to play with the iostream behind BFD's back. | |
252 | ||
253 | Also, note that the origin pointer points to the beginning of a file's | |
254 | contents (0 for non-archive elements). For archive entries this is the | |
255 | first octet in the file, NOT the beginning of the archive header. */ | |
256 | ||
257 | static file_ptr | |
f12a02c0 | 258 | cache_bread_1 (struct bfd *abfd, void *buf, file_ptr nbytes) |
40838a72 | 259 | { |
3dff57e8 | 260 | FILE *f; |
40838a72 AC |
261 | file_ptr nread; |
262 | /* FIXME - this looks like an optimization, but it's really to cover | |
263 | up for a feature of some OSs (not solaris - sigh) that | |
264 | ld/pe-dll.c takes advantage of (apparently) when it creates BFDs | |
265 | internally and tries to link against them. BFD seems to be smart | |
266 | enough to realize there are no symbol records in the "file" that | |
267 | doesn't exist but attempts to read them anyway. On Solaris, | |
268 | attempting to read zero bytes from a NULL file results in a core | |
269 | dump, but on other platforms it just returns zero bytes read. | |
270 | This makes it to something reasonable. - DJ */ | |
271 | if (nbytes == 0) | |
272 | return 0; | |
273 | ||
a50b1753 | 274 | f = bfd_cache_lookup (abfd, CACHE_NORMAL); |
3dff57e8 AM |
275 | if (f == NULL) |
276 | return 0; | |
277 | ||
40838a72 AC |
278 | #if defined (__VAX) && defined (VMS) |
279 | /* Apparently fread on Vax VMS does not keep the record length | |
280 | information. */ | |
3dff57e8 | 281 | nread = read (fileno (f), buf, nbytes); |
40838a72 AC |
282 | /* Set bfd_error if we did not read as much data as we expected. If |
283 | the read failed due to an error set the bfd_error_system_call, | |
284 | else set bfd_error_file_truncated. */ | |
285 | if (nread == (file_ptr)-1) | |
286 | { | |
287 | bfd_set_error (bfd_error_system_call); | |
288 | return -1; | |
289 | } | |
290 | #else | |
3dff57e8 | 291 | nread = fread (buf, 1, nbytes, f); |
40838a72 AC |
292 | /* Set bfd_error if we did not read as much data as we expected. If |
293 | the read failed due to an error set the bfd_error_system_call, | |
294 | else set bfd_error_file_truncated. */ | |
3dff57e8 | 295 | if (nread < nbytes && ferror (f)) |
40838a72 AC |
296 | { |
297 | bfd_set_error (bfd_error_system_call); | |
298 | return -1; | |
299 | } | |
300 | #endif | |
662ed161 DJ |
301 | if (nread < nbytes) |
302 | /* This may or may not be an error, but in case the calling code | |
303 | bails out because of it, set the right error code. */ | |
304 | bfd_set_error (bfd_error_file_truncated); | |
40838a72 AC |
305 | return nread; |
306 | } | |
307 | ||
f12a02c0 JB |
308 | static file_ptr |
309 | cache_bread (struct bfd *abfd, void *buf, file_ptr nbytes) | |
310 | { | |
311 | file_ptr nread = 0; | |
312 | ||
313 | /* Some filesystems are unable to handle reads that are too large | |
314 | (for instance, NetApp shares with oplocks turned off). To avoid | |
315 | hitting this limitation, we read the buffer in chunks of 8MB max. */ | |
316 | while (nread < nbytes) | |
317 | { | |
318 | const file_ptr max_chunk_size = 0x800000; | |
319 | file_ptr chunk_size = nbytes - nread; | |
320 | file_ptr chunk_nread; | |
321 | ||
322 | if (chunk_size > max_chunk_size) | |
323 | chunk_size = max_chunk_size; | |
324 | ||
cb5220a0 | 325 | chunk_nread = cache_bread_1 (abfd, (char *) buf + nread, chunk_size); |
f12a02c0 JB |
326 | |
327 | /* Update the nread count. | |
328 | ||
329 | We just have to be careful of the case when cache_bread_1 returns | |
330 | a negative count: If this is our first read, then set nread to | |
331 | that negative count in order to return that negative value to the | |
332 | caller. Otherwise, don't add it to our total count, or we would | |
333 | end up returning a smaller number of bytes read than we actually | |
334 | did. */ | |
335 | if (nread == 0 || chunk_nread > 0) | |
336 | nread += chunk_nread; | |
337 | ||
338 | if (chunk_nread < chunk_size) | |
339 | break; | |
340 | } | |
341 | ||
342 | return nread; | |
343 | } | |
344 | ||
40838a72 AC |
345 | static file_ptr |
346 | cache_bwrite (struct bfd *abfd, const void *where, file_ptr nbytes) | |
347 | { | |
3dff57e8 | 348 | file_ptr nwrite; |
a50b1753 | 349 | FILE *f = bfd_cache_lookup (abfd, CACHE_NORMAL); |
cb5220a0 | 350 | |
3dff57e8 AM |
351 | if (f == NULL) |
352 | return 0; | |
353 | nwrite = fwrite (where, 1, nbytes, f); | |
354 | if (nwrite < nbytes && ferror (f)) | |
40838a72 AC |
355 | { |
356 | bfd_set_error (bfd_error_system_call); | |
357 | return -1; | |
358 | } | |
359 | return nwrite; | |
360 | } | |
361 | ||
362 | static int | |
363 | cache_bclose (struct bfd *abfd) | |
364 | { | |
365 | return bfd_cache_close (abfd); | |
366 | } | |
367 | ||
368 | static int | |
369 | cache_bflush (struct bfd *abfd) | |
370 | { | |
3dff57e8 | 371 | int sts; |
95560129 | 372 | FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN); |
cb5220a0 | 373 | |
3dff57e8 | 374 | if (f == NULL) |
95560129 | 375 | return 0; |
3dff57e8 | 376 | sts = fflush (f); |
40838a72 AC |
377 | if (sts < 0) |
378 | bfd_set_error (bfd_error_system_call); | |
379 | return sts; | |
380 | } | |
381 | ||
382 | static int | |
383 | cache_bstat (struct bfd *abfd, struct stat *sb) | |
384 | { | |
3dff57e8 | 385 | int sts; |
95560129 | 386 | FILE *f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR); |
cb5220a0 | 387 | |
3dff57e8 AM |
388 | if (f == NULL) |
389 | return -1; | |
390 | sts = fstat (fileno (f), sb); | |
40838a72 AC |
391 | if (sts < 0) |
392 | bfd_set_error (bfd_error_system_call); | |
393 | return sts; | |
394 | } | |
395 | ||
25b88f33 PP |
396 | static void * |
397 | cache_bmmap (struct bfd *abfd ATTRIBUTE_UNUSED, | |
398 | void *addr ATTRIBUTE_UNUSED, | |
399 | bfd_size_type len ATTRIBUTE_UNUSED, | |
400 | int prot ATTRIBUTE_UNUSED, | |
401 | int flags ATTRIBUTE_UNUSED, | |
4c95ab76 TG |
402 | file_ptr offset ATTRIBUTE_UNUSED, |
403 | void **map_addr ATTRIBUTE_UNUSED, | |
404 | bfd_size_type *map_len ATTRIBUTE_UNUSED) | |
25b88f33 PP |
405 | { |
406 | void *ret = (void *) -1; | |
407 | ||
408 | if ((abfd->flags & BFD_IN_MEMORY) != 0) | |
409 | abort (); | |
410 | #ifdef HAVE_MMAP | |
411 | else | |
412 | { | |
4c95ab76 TG |
413 | static uintptr_t pagesize_m1; |
414 | FILE *f; | |
415 | file_ptr pg_offset; | |
416 | bfd_size_type pg_len; | |
417 | ||
418 | f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR); | |
25b88f33 PP |
419 | if (f == NULL) |
420 | return ret; | |
421 | ||
4c95ab76 TG |
422 | if (pagesize_m1 == 0) |
423 | pagesize_m1 = getpagesize () - 1; | |
424 | ||
425 | /* Handle archive members. */ | |
426 | if (abfd->my_archive != NULL) | |
427 | offset += abfd->origin; | |
428 | ||
429 | /* Align. */ | |
430 | pg_offset = offset & ~pagesize_m1; | |
431 | pg_len = (len + (offset - pg_offset) + pagesize_m1) & ~pagesize_m1; | |
432 | ||
433 | ret = mmap (addr, pg_len, prot, flags, fileno (f), pg_offset); | |
25b88f33 PP |
434 | if (ret == (void *) -1) |
435 | bfd_set_error (bfd_error_system_call); | |
4c95ab76 TG |
436 | else |
437 | { | |
438 | *map_addr = ret; | |
439 | *map_len = pg_len; | |
440 | ret += offset & pagesize_m1; | |
441 | } | |
25b88f33 PP |
442 | } |
443 | #endif | |
444 | ||
445 | return ret; | |
446 | } | |
447 | ||
cb5220a0 NC |
448 | static const struct bfd_iovec cache_iovec = |
449 | { | |
40838a72 | 450 | &cache_bread, &cache_bwrite, &cache_btell, &cache_bseek, |
25b88f33 | 451 | &cache_bclose, &cache_bflush, &cache_bstat, &cache_bmmap |
40838a72 AC |
452 | }; |
453 | ||
252b5132 RH |
454 | /* |
455 | INTERNAL_FUNCTION | |
456 | bfd_cache_init | |
457 | ||
458 | SYNOPSIS | |
b34976b6 | 459 | bfd_boolean bfd_cache_init (bfd *abfd); |
252b5132 RH |
460 | |
461 | DESCRIPTION | |
462 | Add a newly opened BFD to the cache. | |
463 | */ | |
464 | ||
b34976b6 | 465 | bfd_boolean |
c58b9523 | 466 | bfd_cache_init (bfd *abfd) |
252b5132 RH |
467 | { |
468 | BFD_ASSERT (abfd->iostream != NULL); | |
469 | if (open_files >= BFD_CACHE_MAX_OPEN) | |
470 | { | |
471 | if (! close_one ()) | |
b34976b6 | 472 | return FALSE; |
252b5132 | 473 | } |
40838a72 | 474 | abfd->iovec = &cache_iovec; |
252b5132 RH |
475 | insert (abfd); |
476 | ++open_files; | |
b34976b6 | 477 | return TRUE; |
252b5132 RH |
478 | } |
479 | ||
480 | /* | |
481 | INTERNAL_FUNCTION | |
482 | bfd_cache_close | |
483 | ||
484 | SYNOPSIS | |
b34976b6 | 485 | bfd_boolean bfd_cache_close (bfd *abfd); |
252b5132 RH |
486 | |
487 | DESCRIPTION | |
488 | Remove the BFD @var{abfd} from the cache. If the attached file is open, | |
489 | then close it too. | |
490 | ||
491 | RETURNS | |
b34976b6 | 492 | <<FALSE>> is returned if closing the file fails, <<TRUE>> is |
252b5132 RH |
493 | returned if all is well. |
494 | */ | |
495 | ||
b34976b6 | 496 | bfd_boolean |
c58b9523 | 497 | bfd_cache_close (bfd *abfd) |
252b5132 | 498 | { |
40838a72 | 499 | if (abfd->iovec != &cache_iovec) |
b34976b6 | 500 | return TRUE; |
252b5132 | 501 | |
fe2e161a AC |
502 | if (abfd->iostream == NULL) |
503 | /* Previously closed. */ | |
504 | return TRUE; | |
505 | ||
252b5132 RH |
506 | return bfd_cache_delete (abfd); |
507 | } | |
508 | ||
02d5a37b JG |
509 | /* |
510 | FUNCTION | |
511 | bfd_cache_close_all | |
512 | ||
513 | SYNOPSIS | |
514 | bfd_boolean bfd_cache_close_all (void); | |
515 | ||
516 | DESCRIPTION | |
517 | Remove all BFDs from the cache. If the attached file is open, | |
518 | then close it too. | |
519 | ||
520 | RETURNS | |
521 | <<FALSE>> is returned if closing one of the file fails, <<TRUE>> is | |
522 | returned if all is well. | |
523 | */ | |
524 | ||
525 | bfd_boolean | |
526 | bfd_cache_close_all () | |
527 | { | |
528 | bfd_boolean ret = TRUE; | |
529 | ||
530 | while (bfd_last_cache != NULL) | |
531 | ret &= bfd_cache_close (bfd_last_cache); | |
c9b549b2 JG |
532 | |
533 | return ret; | |
02d5a37b JG |
534 | } |
535 | ||
252b5132 RH |
536 | /* |
537 | INTERNAL_FUNCTION | |
538 | bfd_open_file | |
539 | ||
540 | SYNOPSIS | |
c58b9523 | 541 | FILE* bfd_open_file (bfd *abfd); |
252b5132 RH |
542 | |
543 | DESCRIPTION | |
544 | Call the OS to open a file for @var{abfd}. Return the <<FILE *>> | |
545 | (possibly <<NULL>>) that results from this operation. Set up the | |
546 | BFD so that future accesses know the file is open. If the <<FILE *>> | |
547 | returned is <<NULL>>, then it won't have been put in the | |
548 | cache, so it won't have to be removed from it. | |
549 | */ | |
550 | ||
551 | FILE * | |
c58b9523 | 552 | bfd_open_file (bfd *abfd) |
252b5132 | 553 | { |
b34976b6 | 554 | abfd->cacheable = TRUE; /* Allow it to be closed later. */ |
252b5132 RH |
555 | |
556 | if (open_files >= BFD_CACHE_MAX_OPEN) | |
557 | { | |
558 | if (! close_one ()) | |
559 | return NULL; | |
560 | } | |
561 | ||
562 | switch (abfd->direction) | |
563 | { | |
564 | case read_direction: | |
565 | case no_direction: | |
2e6f4fae | 566 | abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_RB); |
252b5132 RH |
567 | break; |
568 | case both_direction: | |
569 | case write_direction: | |
82e51918 | 570 | if (abfd->opened_once) |
252b5132 | 571 | { |
2e6f4fae | 572 | abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_RUB); |
252b5132 | 573 | if (abfd->iostream == NULL) |
2e6f4fae | 574 | abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_WUB); |
252b5132 RH |
575 | } |
576 | else | |
577 | { | |
9e422a2e ILT |
578 | /* Create the file. |
579 | ||
580 | Some operating systems won't let us overwrite a running | |
581 | binary. For them, we want to unlink the file first. | |
582 | ||
583 | However, gcc 2.95 will create temporary files using | |
584 | O_EXCL and tight permissions to prevent other users from | |
585 | substituting other .o files during the compilation. gcc | |
586 | will then tell the assembler to use the newly created | |
587 | file as an output file. If we unlink the file here, we | |
588 | open a brief window when another user could still | |
589 | substitute a file. | |
590 | ||
591 | So we unlink the output file if and only if it has | |
592 | non-zero size. */ | |
5af11cab AM |
593 | #ifndef __MSDOS__ |
594 | /* Don't do this for MSDOS: it doesn't care about overwriting | |
595 | a running binary, but if this file is already open by | |
596 | another BFD, we will be in deep trouble if we delete an | |
597 | open file. In fact, objdump does just that if invoked with | |
598 | the --info option. */ | |
9e422a2e ILT |
599 | struct stat s; |
600 | ||
601 | if (stat (abfd->filename, &s) == 0 && s.st_size != 0) | |
bb14f524 | 602 | unlink_if_ordinary (abfd->filename); |
5af11cab | 603 | #endif |
2e6f4fae | 604 | abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_WUB); |
b34976b6 | 605 | abfd->opened_once = TRUE; |
252b5132 RH |
606 | } |
607 | break; | |
608 | } | |
609 | ||
5c91cdfb AM |
610 | if (abfd->iostream == NULL) |
611 | bfd_set_error (bfd_error_system_call); | |
612 | else | |
252b5132 RH |
613 | { |
614 | if (! bfd_cache_init (abfd)) | |
615 | return NULL; | |
616 | } | |
617 | ||
618 | return (FILE *) abfd->iostream; | |
619 | } |