]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* BFD library -- caching of file descriptors. |
7c192733 | 2 | |
250d07de | 3 | Copyright (C) 1990-2021 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 | /* | |
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 | |
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 |
39 | SUBSECTION |
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. */ | |
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 | ||
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 |
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 | { | |
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 | ||
118 | static 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 | 124 | static bfd *bfd_last_cache = NULL; |
252b5132 | 125 | |
d0fdd288 AM |
126 | /* Insert a BFD into the cache. */ |
127 | ||
128 | static void | |
129 | insert (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 | ||
148 | static void | |
149 | snip (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 | ||
163 | static bfd_boolean | |
164 | bfd_cache_delete (bfd *abfd) | |
165 | { | |
166 | bfd_boolean ret; | |
167 | ||
168 | if (fclose ((FILE *) abfd->iostream) == 0) | |
169 | ret = TRUE; | |
170 | else | |
171 | { | |
172 | ret = FALSE; | |
173 | bfd_set_error (bfd_error_system_call); | |
174 | } | |
175 | ||
176 | snip (abfd); | |
177 | ||
178 | abfd->iostream = NULL; | |
179 | --open_files; | |
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 | ||
187 | static bfd_boolean | |
188 | close_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. */ | |
211 | return TRUE; | |
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 | |
d00967c7 AM |
229 | /* Called when the macro <<bfd_cache_lookup>> fails to find a |
230 | quick answer. Find a file descriptor for @var{abfd}. If | |
231 | necessary, it open it. If there are already more than | |
9d782e8d | 232 | <<bfd_cache_max_open>> files open, it tries to close one first, to |
d00967c7 AM |
233 | avoid running out of file descriptors. It will return NULL |
234 | if it is unable to (re)open the @var{abfd}. */ | |
d0fdd288 | 235 | |
d00967c7 | 236 | static FILE * |
95560129 | 237 | bfd_cache_lookup_worker (bfd *abfd, enum cache_flag flag) |
d0fdd288 | 238 | { |
d0fdd288 AM |
239 | if ((abfd->flags & BFD_IN_MEMORY) != 0) |
240 | abort (); | |
241 | ||
5c4ce239 AM |
242 | if (abfd->my_archive != NULL |
243 | && !bfd_is_thin_archive (abfd->my_archive)) | |
244 | abort (); | |
d0fdd288 AM |
245 | |
246 | if (abfd->iostream != NULL) | |
247 | { | |
248 | /* Move the file to the start of the cache. */ | |
249 | if (abfd != bfd_last_cache) | |
250 | { | |
251 | snip (abfd); | |
252 | insert (abfd); | |
253 | } | |
254 | return (FILE *) abfd->iostream; | |
255 | } | |
256 | ||
95560129 AM |
257 | if (flag & CACHE_NO_OPEN) |
258 | return NULL; | |
259 | ||
d0fdd288 AM |
260 | if (bfd_open_file (abfd) == NULL) |
261 | ; | |
95560129 | 262 | else if (!(flag & CACHE_NO_SEEK) |
c7c3d11b PA |
263 | && _bfd_real_fseek ((FILE *) abfd->iostream, |
264 | abfd->where, SEEK_SET) != 0 | |
95560129 | 265 | && !(flag & CACHE_NO_SEEK_ERROR)) |
d0fdd288 AM |
266 | bfd_set_error (bfd_error_system_call); |
267 | else | |
268 | return (FILE *) abfd->iostream; | |
269 | ||
695344c0 | 270 | /* xgettext:c-format */ |
871b3ab2 | 271 | _bfd_error_handler (_("reopening %pB: %s\n"), |
5c4ce239 | 272 | abfd, bfd_errmsg (bfd_get_error ())); |
d0fdd288 AM |
273 | return NULL; |
274 | } | |
40838a72 AC |
275 | |
276 | static file_ptr | |
277 | cache_btell (struct bfd *abfd) | |
278 | { | |
95560129 | 279 | FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN); |
3dff57e8 | 280 | if (f == NULL) |
95560129 | 281 | return abfd->where; |
c7c3d11b | 282 | return _bfd_real_ftell (f); |
40838a72 AC |
283 | } |
284 | ||
285 | static int | |
286 | cache_bseek (struct bfd *abfd, file_ptr offset, int whence) | |
287 | { | |
a50b1753 | 288 | FILE *f = bfd_cache_lookup (abfd, whence != SEEK_CUR ? CACHE_NO_SEEK : CACHE_NORMAL); |
3dff57e8 AM |
289 | if (f == NULL) |
290 | return -1; | |
c7c3d11b | 291 | return _bfd_real_fseek (f, offset, whence); |
40838a72 AC |
292 | } |
293 | ||
294 | /* Note that archive entries don't have streams; they share their parent's. | |
295 | This allows someone to play with the iostream behind BFD's back. | |
296 | ||
297 | Also, note that the origin pointer points to the beginning of a file's | |
298 | contents (0 for non-archive elements). For archive entries this is the | |
299 | first octet in the file, NOT the beginning of the archive header. */ | |
300 | ||
301 | static file_ptr | |
5c4ce239 | 302 | cache_bread_1 (FILE *f, void *buf, file_ptr nbytes) |
40838a72 AC |
303 | { |
304 | file_ptr nread; | |
3dff57e8 | 305 | |
40838a72 AC |
306 | #if defined (__VAX) && defined (VMS) |
307 | /* Apparently fread on Vax VMS does not keep the record length | |
308 | information. */ | |
3dff57e8 | 309 | nread = read (fileno (f), buf, nbytes); |
40838a72 AC |
310 | /* Set bfd_error if we did not read as much data as we expected. If |
311 | the read failed due to an error set the bfd_error_system_call, | |
312 | else set bfd_error_file_truncated. */ | |
313 | if (nread == (file_ptr)-1) | |
314 | { | |
315 | bfd_set_error (bfd_error_system_call); | |
87f14779 | 316 | return nread; |
40838a72 AC |
317 | } |
318 | #else | |
3dff57e8 | 319 | nread = fread (buf, 1, nbytes, f); |
40838a72 AC |
320 | /* Set bfd_error if we did not read as much data as we expected. If |
321 | the read failed due to an error set the bfd_error_system_call, | |
322 | else set bfd_error_file_truncated. */ | |
3dff57e8 | 323 | if (nread < nbytes && ferror (f)) |
40838a72 AC |
324 | { |
325 | bfd_set_error (bfd_error_system_call); | |
87f14779 | 326 | return nread; |
40838a72 AC |
327 | } |
328 | #endif | |
662ed161 DJ |
329 | if (nread < nbytes) |
330 | /* This may or may not be an error, but in case the calling code | |
331 | bails out because of it, set the right error code. */ | |
332 | bfd_set_error (bfd_error_file_truncated); | |
40838a72 AC |
333 | return nread; |
334 | } | |
335 | ||
f12a02c0 JB |
336 | static file_ptr |
337 | cache_bread (struct bfd *abfd, void *buf, file_ptr nbytes) | |
338 | { | |
339 | file_ptr nread = 0; | |
5c4ce239 AM |
340 | FILE *f; |
341 | ||
342 | f = bfd_cache_lookup (abfd, CACHE_NORMAL); | |
343 | if (f == NULL) | |
344 | return -1; | |
f12a02c0 JB |
345 | |
346 | /* Some filesystems are unable to handle reads that are too large | |
347 | (for instance, NetApp shares with oplocks turned off). To avoid | |
348 | hitting this limitation, we read the buffer in chunks of 8MB max. */ | |
349 | while (nread < nbytes) | |
350 | { | |
351 | const file_ptr max_chunk_size = 0x800000; | |
352 | file_ptr chunk_size = nbytes - nread; | |
353 | file_ptr chunk_nread; | |
354 | ||
355 | if (chunk_size > max_chunk_size) | |
07d6d2b8 | 356 | chunk_size = max_chunk_size; |
f12a02c0 | 357 | |
5c4ce239 | 358 | chunk_nread = cache_bread_1 (f, (char *) buf + nread, chunk_size); |
f12a02c0 JB |
359 | |
360 | /* Update the nread count. | |
361 | ||
07d6d2b8 AM |
362 | We just have to be careful of the case when cache_bread_1 returns |
363 | a negative count: If this is our first read, then set nread to | |
364 | that negative count in order to return that negative value to the | |
365 | caller. Otherwise, don't add it to our total count, or we would | |
366 | end up returning a smaller number of bytes read than we actually | |
367 | did. */ | |
f12a02c0 | 368 | if (nread == 0 || chunk_nread > 0) |
07d6d2b8 | 369 | nread += chunk_nread; |
f12a02c0 JB |
370 | |
371 | if (chunk_nread < chunk_size) | |
07d6d2b8 | 372 | break; |
f12a02c0 JB |
373 | } |
374 | ||
375 | return nread; | |
376 | } | |
377 | ||
40838a72 | 378 | static file_ptr |
5c4ce239 | 379 | cache_bwrite (struct bfd *abfd, const void *from, file_ptr nbytes) |
40838a72 | 380 | { |
3dff57e8 | 381 | file_ptr nwrite; |
a50b1753 | 382 | FILE *f = bfd_cache_lookup (abfd, CACHE_NORMAL); |
cb5220a0 | 383 | |
3dff57e8 AM |
384 | if (f == NULL) |
385 | return 0; | |
5c4ce239 | 386 | nwrite = fwrite (from, 1, nbytes, f); |
3dff57e8 | 387 | if (nwrite < nbytes && ferror (f)) |
40838a72 AC |
388 | { |
389 | bfd_set_error (bfd_error_system_call); | |
390 | return -1; | |
391 | } | |
392 | return nwrite; | |
393 | } | |
394 | ||
405bf443 | 395 | static int |
40838a72 AC |
396 | cache_bclose (struct bfd *abfd) |
397 | { | |
405bf443 | 398 | return bfd_cache_close (abfd) - 1; |
40838a72 AC |
399 | } |
400 | ||
401 | static int | |
402 | cache_bflush (struct bfd *abfd) | |
403 | { | |
3dff57e8 | 404 | int sts; |
95560129 | 405 | FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN); |
cb5220a0 | 406 | |
3dff57e8 | 407 | if (f == NULL) |
95560129 | 408 | return 0; |
3dff57e8 | 409 | sts = fflush (f); |
40838a72 AC |
410 | if (sts < 0) |
411 | bfd_set_error (bfd_error_system_call); | |
412 | return sts; | |
413 | } | |
414 | ||
415 | static int | |
416 | cache_bstat (struct bfd *abfd, struct stat *sb) | |
417 | { | |
3dff57e8 | 418 | int sts; |
95560129 | 419 | FILE *f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR); |
cb5220a0 | 420 | |
3dff57e8 AM |
421 | if (f == NULL) |
422 | return -1; | |
423 | sts = fstat (fileno (f), sb); | |
40838a72 AC |
424 | if (sts < 0) |
425 | bfd_set_error (bfd_error_system_call); | |
426 | return sts; | |
427 | } | |
428 | ||
25b88f33 PP |
429 | static void * |
430 | cache_bmmap (struct bfd *abfd ATTRIBUTE_UNUSED, | |
431 | void *addr ATTRIBUTE_UNUSED, | |
432 | bfd_size_type len ATTRIBUTE_UNUSED, | |
433 | int prot ATTRIBUTE_UNUSED, | |
434 | int flags ATTRIBUTE_UNUSED, | |
4c95ab76 | 435 | file_ptr offset ATTRIBUTE_UNUSED, |
07d6d2b8 AM |
436 | void **map_addr ATTRIBUTE_UNUSED, |
437 | bfd_size_type *map_len ATTRIBUTE_UNUSED) | |
25b88f33 PP |
438 | { |
439 | void *ret = (void *) -1; | |
440 | ||
441 | if ((abfd->flags & BFD_IN_MEMORY) != 0) | |
442 | abort (); | |
443 | #ifdef HAVE_MMAP | |
444 | else | |
445 | { | |
48d96f80 | 446 | static uintptr_t pagesize_m1; |
4c95ab76 TG |
447 | FILE *f; |
448 | file_ptr pg_offset; | |
449 | bfd_size_type pg_len; | |
450 | ||
451 | f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR); | |
25b88f33 PP |
452 | if (f == NULL) |
453 | return ret; | |
454 | ||
48d96f80 | 455 | if (pagesize_m1 == 0) |
07d6d2b8 | 456 | pagesize_m1 = getpagesize () - 1; |
48d96f80 | 457 | |
4c95ab76 TG |
458 | /* Align. */ |
459 | pg_offset = offset & ~pagesize_m1; | |
460 | pg_len = (len + (offset - pg_offset) + pagesize_m1) & ~pagesize_m1; | |
461 | ||
462 | ret = mmap (addr, pg_len, prot, flags, fileno (f), pg_offset); | |
25b88f33 PP |
463 | if (ret == (void *) -1) |
464 | bfd_set_error (bfd_error_system_call); | |
4c95ab76 | 465 | else |
07d6d2b8 AM |
466 | { |
467 | *map_addr = ret; | |
468 | *map_len = pg_len; | |
469 | ret = (char *) ret + (offset & pagesize_m1); | |
470 | } | |
25b88f33 PP |
471 | } |
472 | #endif | |
473 | ||
474 | return ret; | |
475 | } | |
476 | ||
cb5220a0 NC |
477 | static const struct bfd_iovec cache_iovec = |
478 | { | |
40838a72 | 479 | &cache_bread, &cache_bwrite, &cache_btell, &cache_bseek, |
25b88f33 | 480 | &cache_bclose, &cache_bflush, &cache_bstat, &cache_bmmap |
40838a72 AC |
481 | }; |
482 | ||
252b5132 RH |
483 | /* |
484 | INTERNAL_FUNCTION | |
485 | bfd_cache_init | |
486 | ||
487 | SYNOPSIS | |
b34976b6 | 488 | bfd_boolean bfd_cache_init (bfd *abfd); |
252b5132 RH |
489 | |
490 | DESCRIPTION | |
491 | Add a newly opened BFD to the cache. | |
492 | */ | |
493 | ||
b34976b6 | 494 | bfd_boolean |
c58b9523 | 495 | bfd_cache_init (bfd *abfd) |
252b5132 RH |
496 | { |
497 | BFD_ASSERT (abfd->iostream != NULL); | |
9d782e8d | 498 | if (open_files >= bfd_cache_max_open ()) |
252b5132 RH |
499 | { |
500 | if (! close_one ()) | |
b34976b6 | 501 | return FALSE; |
252b5132 | 502 | } |
40838a72 | 503 | abfd->iovec = &cache_iovec; |
252b5132 RH |
504 | insert (abfd); |
505 | ++open_files; | |
b34976b6 | 506 | return TRUE; |
252b5132 RH |
507 | } |
508 | ||
509 | /* | |
510 | INTERNAL_FUNCTION | |
511 | bfd_cache_close | |
512 | ||
513 | SYNOPSIS | |
b34976b6 | 514 | bfd_boolean bfd_cache_close (bfd *abfd); |
252b5132 RH |
515 | |
516 | DESCRIPTION | |
517 | Remove the BFD @var{abfd} from the cache. If the attached file is open, | |
518 | then close it too. | |
519 | ||
520 | RETURNS | |
b34976b6 | 521 | <<FALSE>> is returned if closing the file fails, <<TRUE>> is |
252b5132 RH |
522 | returned if all is well. |
523 | */ | |
524 | ||
b34976b6 | 525 | bfd_boolean |
c58b9523 | 526 | bfd_cache_close (bfd *abfd) |
252b5132 | 527 | { |
40838a72 | 528 | if (abfd->iovec != &cache_iovec) |
b34976b6 | 529 | return TRUE; |
252b5132 | 530 | |
fe2e161a AC |
531 | if (abfd->iostream == NULL) |
532 | /* Previously closed. */ | |
533 | return TRUE; | |
534 | ||
252b5132 RH |
535 | return bfd_cache_delete (abfd); |
536 | } | |
537 | ||
02d5a37b JG |
538 | /* |
539 | FUNCTION | |
540 | bfd_cache_close_all | |
541 | ||
542 | SYNOPSIS | |
543 | bfd_boolean bfd_cache_close_all (void); | |
544 | ||
545 | DESCRIPTION | |
546 | Remove all BFDs from the cache. If the attached file is open, | |
547 | then close it too. | |
548 | ||
549 | RETURNS | |
550 | <<FALSE>> is returned if closing one of the file fails, <<TRUE>> is | |
551 | returned if all is well. | |
552 | */ | |
553 | ||
554 | bfd_boolean | |
e6c7cdec | 555 | bfd_cache_close_all (void) |
02d5a37b JG |
556 | { |
557 | bfd_boolean ret = TRUE; | |
558 | ||
559 | while (bfd_last_cache != NULL) | |
560 | ret &= bfd_cache_close (bfd_last_cache); | |
c9b549b2 JG |
561 | |
562 | return ret; | |
02d5a37b JG |
563 | } |
564 | ||
252b5132 RH |
565 | /* |
566 | INTERNAL_FUNCTION | |
567 | bfd_open_file | |
568 | ||
569 | SYNOPSIS | |
c58b9523 | 570 | FILE* bfd_open_file (bfd *abfd); |
252b5132 RH |
571 | |
572 | DESCRIPTION | |
573 | Call the OS to open a file for @var{abfd}. Return the <<FILE *>> | |
574 | (possibly <<NULL>>) that results from this operation. Set up the | |
575 | BFD so that future accesses know the file is open. If the <<FILE *>> | |
576 | returned is <<NULL>>, then it won't have been put in the | |
577 | cache, so it won't have to be removed from it. | |
578 | */ | |
579 | ||
580 | FILE * | |
c58b9523 | 581 | bfd_open_file (bfd *abfd) |
252b5132 | 582 | { |
b34976b6 | 583 | abfd->cacheable = TRUE; /* Allow it to be closed later. */ |
252b5132 | 584 | |
9d782e8d | 585 | if (open_files >= bfd_cache_max_open ()) |
252b5132 RH |
586 | { |
587 | if (! close_one ()) | |
588 | return NULL; | |
589 | } | |
590 | ||
591 | switch (abfd->direction) | |
592 | { | |
593 | case read_direction: | |
594 | case no_direction: | |
765cf5f6 | 595 | abfd->iostream = _bfd_real_fopen (bfd_get_filename (abfd), FOPEN_RB); |
252b5132 RH |
596 | break; |
597 | case both_direction: | |
598 | case write_direction: | |
82e51918 | 599 | if (abfd->opened_once) |
252b5132 | 600 | { |
765cf5f6 AM |
601 | abfd->iostream = _bfd_real_fopen (bfd_get_filename (abfd), |
602 | FOPEN_RUB); | |
252b5132 | 603 | if (abfd->iostream == NULL) |
765cf5f6 AM |
604 | abfd->iostream = _bfd_real_fopen (bfd_get_filename (abfd), |
605 | FOPEN_WUB); | |
252b5132 RH |
606 | } |
607 | else | |
608 | { | |
9e422a2e ILT |
609 | /* Create the file. |
610 | ||
611 | Some operating systems won't let us overwrite a running | |
612 | binary. For them, we want to unlink the file first. | |
613 | ||
614 | However, gcc 2.95 will create temporary files using | |
615 | O_EXCL and tight permissions to prevent other users from | |
616 | substituting other .o files during the compilation. gcc | |
617 | will then tell the assembler to use the newly created | |
618 | file as an output file. If we unlink the file here, we | |
619 | open a brief window when another user could still | |
620 | substitute a file. | |
621 | ||
622 | So we unlink the output file if and only if it has | |
623 | non-zero size. */ | |
5af11cab AM |
624 | #ifndef __MSDOS__ |
625 | /* Don't do this for MSDOS: it doesn't care about overwriting | |
626 | a running binary, but if this file is already open by | |
627 | another BFD, we will be in deep trouble if we delete an | |
628 | open file. In fact, objdump does just that if invoked with | |
629 | the --info option. */ | |
9e422a2e ILT |
630 | struct stat s; |
631 | ||
765cf5f6 AM |
632 | if (stat (bfd_get_filename (abfd), &s) == 0 && s.st_size != 0) |
633 | unlink_if_ordinary (bfd_get_filename (abfd)); | |
5af11cab | 634 | #endif |
765cf5f6 AM |
635 | abfd->iostream = _bfd_real_fopen (bfd_get_filename (abfd), |
636 | FOPEN_WUB); | |
b34976b6 | 637 | abfd->opened_once = TRUE; |
252b5132 RH |
638 | } |
639 | break; | |
640 | } | |
641 | ||
5c91cdfb AM |
642 | if (abfd->iostream == NULL) |
643 | bfd_set_error (bfd_error_system_call); | |
644 | else | |
252b5132 RH |
645 | { |
646 | if (! bfd_cache_init (abfd)) | |
647 | return NULL; | |
648 | } | |
649 | ||
650 | return (FILE *) abfd->iostream; | |
651 | } |