Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* BFD library -- caching of file descriptors. |
7c192733 | 2 | |
219d1afa | 3 | Copyright (C) 1990-2018 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" |
4c95ab76 | 47 | #include "bfd_stdint.h" |
252b5132 | 48 | |
25b88f33 PP |
49 | #ifdef HAVE_MMAP |
50 | #include <sys/mman.h> | |
51 | #endif | |
52 | ||
95560129 AM |
53 | /* In some cases we can optimize cache operation when reopening files. |
54 | For instance, a flush is entirely unnecessary if the file is already | |
55 | closed, so a flush would use CACHE_NO_OPEN. Similarly, a seek using | |
56 | SEEK_SET or SEEK_END need not first seek to the current position. | |
57 | For stat we ignore seek errors, just in case the file has changed | |
58 | while we weren't looking. If it has, then it's possible that the | |
59 | file is shorter and we don't want a seek error to prevent us doing | |
60 | the stat. */ | |
61 | enum cache_flag { | |
62 | CACHE_NORMAL = 0, | |
63 | CACHE_NO_OPEN = 1, | |
64 | CACHE_NO_SEEK = 2, | |
65 | CACHE_NO_SEEK_ERROR = 4 | |
66 | }; | |
67 | ||
d00967c7 | 68 | /* The maximum number of files which the cache will keep open at |
9d782e8d | 69 | one time. When needed call bfd_cache_max_open to initialize. */ |
d0fdd288 | 70 | |
9d782e8d MW |
71 | static int max_open_files = 0; |
72 | ||
73 | /* Set max_open_files, if not already set, to 12.5% of the allowed open | |
74 | file descriptors, but at least 10, and return the value. */ | |
75 | static int | |
76 | bfd_cache_max_open (void) | |
77 | { | |
78 | if (max_open_files == 0) | |
79 | { | |
80 | int max; | |
0b1fa288 ST |
81 | #if defined(__sun) && !defined(__sparcv9) && !defined(__x86_64__) |
82 | /* PR ld/19260: 32-bit Solaris has very inelegant handling of the 255 | |
83 | file descriptor limit. The problem is that setrlimit(2) can raise | |
84 | RLIMIT_NOFILE to a value that is not supported by libc, resulting | |
07d6d2b8 | 85 | in "Too many open files" errors. This can happen here even though |
0b1fa288 ST |
86 | max_open_files is set to rlim.rlim_cur / 8. For example, if |
87 | a parent process has set rlim.rlim_cur to 65536, then max_open_files | |
88 | will be computed as 8192. | |
89 | ||
90 | This check essentially reverts to the behavior from binutils 2.23.1 | |
07d6d2b8 | 91 | for 32-bit Solaris only. (It is hoped that the 32-bit libc |
0b1fa288 ST |
92 | limitation will be removed soon). 64-bit Solaris libc does not have |
93 | this limitation. */ | |
94 | max = 16; | |
95 | #else | |
9d782e8d MW |
96 | #ifdef HAVE_GETRLIMIT |
97 | struct rlimit rlim; | |
0b1fa288 | 98 | |
9d782e8d | 99 | if (getrlimit (RLIMIT_NOFILE, &rlim) == 0 |
d1eb5696 | 100 | && rlim.rlim_cur != (rlim_t) RLIM_INFINITY) |
9d782e8d MW |
101 | max = rlim.rlim_cur / 8; |
102 | else | |
0b1fa288 | 103 | #endif |
9d782e8d MW |
104 | #ifdef _SC_OPEN_MAX |
105 | max = sysconf (_SC_OPEN_MAX) / 8; | |
106 | #else | |
07d6d2b8 | 107 | max = 10; |
0b1fa288 ST |
108 | #endif |
109 | #endif /* not 32-bit Solaris */ | |
110 | ||
9d782e8d MW |
111 | max_open_files = max < 10 ? 10 : max; |
112 | } | |
113 | ||
114 | return max_open_files; | |
115 | } | |
d0fdd288 AM |
116 | |
117 | /* The number of BFD files we have open. */ | |
118 | ||
119 | static int open_files; | |
120 | ||
d00967c7 AM |
121 | /* Zero, or a pointer to the topmost BFD on the chain. This is |
122 | used by the <<bfd_cache_lookup>> macro in @file{libbfd.h} to | |
123 | determine when it can avoid a function call. */ | |
d0fdd288 | 124 | |
d00967c7 | 125 | static bfd *bfd_last_cache = NULL; |
252b5132 | 126 | |
d0fdd288 AM |
127 | /* Insert a BFD into the cache. */ |
128 | ||
129 | static void | |
130 | insert (bfd *abfd) | |
131 | { | |
132 | if (bfd_last_cache == NULL) | |
133 | { | |
134 | abfd->lru_next = abfd; | |
135 | abfd->lru_prev = abfd; | |
136 | } | |
137 | else | |
138 | { | |
139 | abfd->lru_next = bfd_last_cache; | |
140 | abfd->lru_prev = bfd_last_cache->lru_prev; | |
141 | abfd->lru_prev->lru_next = abfd; | |
142 | abfd->lru_next->lru_prev = abfd; | |
143 | } | |
144 | bfd_last_cache = abfd; | |
145 | } | |
146 | ||
147 | /* Remove a BFD from the cache. */ | |
148 | ||
149 | static void | |
150 | snip (bfd *abfd) | |
151 | { | |
152 | abfd->lru_prev->lru_next = abfd->lru_next; | |
153 | abfd->lru_next->lru_prev = abfd->lru_prev; | |
154 | if (abfd == bfd_last_cache) | |
155 | { | |
156 | bfd_last_cache = abfd->lru_next; | |
157 | if (abfd == bfd_last_cache) | |
158 | bfd_last_cache = NULL; | |
159 | } | |
160 | } | |
161 | ||
162 | /* Close a BFD and remove it from the cache. */ | |
163 | ||
164 | static bfd_boolean | |
165 | bfd_cache_delete (bfd *abfd) | |
166 | { | |
167 | bfd_boolean ret; | |
168 | ||
169 | if (fclose ((FILE *) abfd->iostream) == 0) | |
170 | ret = TRUE; | |
171 | else | |
172 | { | |
173 | ret = FALSE; | |
174 | bfd_set_error (bfd_error_system_call); | |
175 | } | |
176 | ||
177 | snip (abfd); | |
178 | ||
179 | abfd->iostream = NULL; | |
180 | --open_files; | |
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 | ||
188 | static bfd_boolean | |
189 | close_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. */ | |
212 | return TRUE; | |
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 | 237 | static FILE * |
95560129 | 238 | bfd_cache_lookup_worker (bfd *abfd, enum cache_flag flag) |
d0fdd288 AM |
239 | { |
240 | bfd *orig_bfd = abfd; | |
241 | if ((abfd->flags & BFD_IN_MEMORY) != 0) | |
242 | abort (); | |
243 | ||
b0cffb47 AM |
244 | while (abfd->my_archive != NULL |
245 | && !bfd_is_thin_archive (abfd->my_archive)) | |
d0fdd288 AM |
246 | abfd = abfd->my_archive; |
247 | ||
248 | if (abfd->iostream != NULL) | |
249 | { | |
250 | /* Move the file to the start of the cache. */ | |
251 | if (abfd != bfd_last_cache) | |
252 | { | |
253 | snip (abfd); | |
254 | insert (abfd); | |
255 | } | |
256 | return (FILE *) abfd->iostream; | |
257 | } | |
258 | ||
95560129 AM |
259 | if (flag & CACHE_NO_OPEN) |
260 | return NULL; | |
261 | ||
d0fdd288 AM |
262 | if (bfd_open_file (abfd) == NULL) |
263 | ; | |
95560129 | 264 | else if (!(flag & CACHE_NO_SEEK) |
c7c3d11b PA |
265 | && _bfd_real_fseek ((FILE *) abfd->iostream, |
266 | abfd->where, SEEK_SET) != 0 | |
95560129 | 267 | && !(flag & CACHE_NO_SEEK_ERROR)) |
d0fdd288 AM |
268 | bfd_set_error (bfd_error_system_call); |
269 | else | |
270 | return (FILE *) abfd->iostream; | |
271 | ||
695344c0 | 272 | /* xgettext:c-format */ |
4eca0228 AM |
273 | _bfd_error_handler (_("reopening %B: %s\n"), |
274 | orig_bfd, bfd_errmsg (bfd_get_error ())); | |
d0fdd288 AM |
275 | return NULL; |
276 | } | |
40838a72 AC |
277 | |
278 | static file_ptr | |
279 | cache_btell (struct bfd *abfd) | |
280 | { | |
95560129 | 281 | FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN); |
3dff57e8 | 282 | if (f == NULL) |
95560129 | 283 | return abfd->where; |
c7c3d11b | 284 | return _bfd_real_ftell (f); |
40838a72 AC |
285 | } |
286 | ||
287 | static int | |
288 | cache_bseek (struct bfd *abfd, file_ptr offset, int whence) | |
289 | { | |
a50b1753 | 290 | FILE *f = bfd_cache_lookup (abfd, whence != SEEK_CUR ? CACHE_NO_SEEK : CACHE_NORMAL); |
3dff57e8 AM |
291 | if (f == NULL) |
292 | return -1; | |
c7c3d11b | 293 | return _bfd_real_fseek (f, offset, whence); |
40838a72 AC |
294 | } |
295 | ||
296 | /* Note that archive entries don't have streams; they share their parent's. | |
297 | This allows someone to play with the iostream behind BFD's back. | |
298 | ||
299 | Also, note that the origin pointer points to the beginning of a file's | |
300 | contents (0 for non-archive elements). For archive entries this is the | |
301 | first octet in the file, NOT the beginning of the archive header. */ | |
302 | ||
303 | static file_ptr | |
f12a02c0 | 304 | cache_bread_1 (struct bfd *abfd, void *buf, file_ptr nbytes) |
40838a72 | 305 | { |
3dff57e8 | 306 | FILE *f; |
40838a72 AC |
307 | file_ptr nread; |
308 | /* FIXME - this looks like an optimization, but it's really to cover | |
309 | up for a feature of some OSs (not solaris - sigh) that | |
310 | ld/pe-dll.c takes advantage of (apparently) when it creates BFDs | |
311 | internally and tries to link against them. BFD seems to be smart | |
312 | enough to realize there are no symbol records in the "file" that | |
313 | doesn't exist but attempts to read them anyway. On Solaris, | |
314 | attempting to read zero bytes from a NULL file results in a core | |
315 | dump, but on other platforms it just returns zero bytes read. | |
316 | This makes it to something reasonable. - DJ */ | |
317 | if (nbytes == 0) | |
318 | return 0; | |
319 | ||
a50b1753 | 320 | f = bfd_cache_lookup (abfd, CACHE_NORMAL); |
3dff57e8 AM |
321 | if (f == NULL) |
322 | return 0; | |
323 | ||
40838a72 AC |
324 | #if defined (__VAX) && defined (VMS) |
325 | /* Apparently fread on Vax VMS does not keep the record length | |
326 | information. */ | |
3dff57e8 | 327 | nread = read (fileno (f), buf, nbytes); |
40838a72 AC |
328 | /* Set bfd_error if we did not read as much data as we expected. If |
329 | the read failed due to an error set the bfd_error_system_call, | |
330 | else set bfd_error_file_truncated. */ | |
331 | if (nread == (file_ptr)-1) | |
332 | { | |
333 | bfd_set_error (bfd_error_system_call); | |
87f14779 | 334 | return nread; |
40838a72 AC |
335 | } |
336 | #else | |
3dff57e8 | 337 | nread = fread (buf, 1, nbytes, f); |
40838a72 AC |
338 | /* Set bfd_error if we did not read as much data as we expected. If |
339 | the read failed due to an error set the bfd_error_system_call, | |
340 | else set bfd_error_file_truncated. */ | |
3dff57e8 | 341 | if (nread < nbytes && ferror (f)) |
40838a72 AC |
342 | { |
343 | bfd_set_error (bfd_error_system_call); | |
87f14779 | 344 | return nread; |
40838a72 AC |
345 | } |
346 | #endif | |
662ed161 DJ |
347 | if (nread < nbytes) |
348 | /* This may or may not be an error, but in case the calling code | |
349 | bails out because of it, set the right error code. */ | |
350 | bfd_set_error (bfd_error_file_truncated); | |
40838a72 AC |
351 | return nread; |
352 | } | |
353 | ||
f12a02c0 JB |
354 | static file_ptr |
355 | cache_bread (struct bfd *abfd, void *buf, file_ptr nbytes) | |
356 | { | |
357 | file_ptr nread = 0; | |
358 | ||
359 | /* Some filesystems are unable to handle reads that are too large | |
360 | (for instance, NetApp shares with oplocks turned off). To avoid | |
361 | hitting this limitation, we read the buffer in chunks of 8MB max. */ | |
362 | while (nread < nbytes) | |
363 | { | |
364 | const file_ptr max_chunk_size = 0x800000; | |
365 | file_ptr chunk_size = nbytes - nread; | |
366 | file_ptr chunk_nread; | |
367 | ||
368 | if (chunk_size > max_chunk_size) | |
07d6d2b8 | 369 | chunk_size = max_chunk_size; |
f12a02c0 | 370 | |
cb5220a0 | 371 | chunk_nread = cache_bread_1 (abfd, (char *) buf + nread, chunk_size); |
f12a02c0 JB |
372 | |
373 | /* Update the nread count. | |
374 | ||
07d6d2b8 AM |
375 | We just have to be careful of the case when cache_bread_1 returns |
376 | a negative count: If this is our first read, then set nread to | |
377 | that negative count in order to return that negative value to the | |
378 | caller. Otherwise, don't add it to our total count, or we would | |
379 | end up returning a smaller number of bytes read than we actually | |
380 | did. */ | |
f12a02c0 | 381 | if (nread == 0 || chunk_nread > 0) |
07d6d2b8 | 382 | nread += chunk_nread; |
f12a02c0 JB |
383 | |
384 | if (chunk_nread < chunk_size) | |
07d6d2b8 | 385 | break; |
f12a02c0 JB |
386 | } |
387 | ||
388 | return nread; | |
389 | } | |
390 | ||
40838a72 AC |
391 | static file_ptr |
392 | cache_bwrite (struct bfd *abfd, const void *where, file_ptr nbytes) | |
393 | { | |
3dff57e8 | 394 | file_ptr nwrite; |
a50b1753 | 395 | FILE *f = bfd_cache_lookup (abfd, CACHE_NORMAL); |
cb5220a0 | 396 | |
3dff57e8 AM |
397 | if (f == NULL) |
398 | return 0; | |
399 | nwrite = fwrite (where, 1, nbytes, f); | |
400 | if (nwrite < nbytes && ferror (f)) | |
40838a72 AC |
401 | { |
402 | bfd_set_error (bfd_error_system_call); | |
403 | return -1; | |
404 | } | |
405 | return nwrite; | |
406 | } | |
407 | ||
405bf443 | 408 | static int |
40838a72 AC |
409 | cache_bclose (struct bfd *abfd) |
410 | { | |
405bf443 | 411 | return bfd_cache_close (abfd) - 1; |
40838a72 AC |
412 | } |
413 | ||
414 | static int | |
415 | cache_bflush (struct bfd *abfd) | |
416 | { | |
3dff57e8 | 417 | int sts; |
95560129 | 418 | FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN); |
cb5220a0 | 419 | |
3dff57e8 | 420 | if (f == NULL) |
95560129 | 421 | return 0; |
3dff57e8 | 422 | sts = fflush (f); |
40838a72 AC |
423 | if (sts < 0) |
424 | bfd_set_error (bfd_error_system_call); | |
425 | return sts; | |
426 | } | |
427 | ||
428 | static int | |
429 | cache_bstat (struct bfd *abfd, struct stat *sb) | |
430 | { | |
3dff57e8 | 431 | int sts; |
95560129 | 432 | FILE *f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR); |
cb5220a0 | 433 | |
3dff57e8 AM |
434 | if (f == NULL) |
435 | return -1; | |
436 | sts = fstat (fileno (f), sb); | |
40838a72 AC |
437 | if (sts < 0) |
438 | bfd_set_error (bfd_error_system_call); | |
439 | return sts; | |
440 | } | |
441 | ||
25b88f33 PP |
442 | static void * |
443 | cache_bmmap (struct bfd *abfd ATTRIBUTE_UNUSED, | |
444 | void *addr ATTRIBUTE_UNUSED, | |
445 | bfd_size_type len ATTRIBUTE_UNUSED, | |
446 | int prot ATTRIBUTE_UNUSED, | |
447 | int flags ATTRIBUTE_UNUSED, | |
4c95ab76 | 448 | file_ptr offset ATTRIBUTE_UNUSED, |
07d6d2b8 AM |
449 | void **map_addr ATTRIBUTE_UNUSED, |
450 | bfd_size_type *map_len ATTRIBUTE_UNUSED) | |
25b88f33 PP |
451 | { |
452 | void *ret = (void *) -1; | |
453 | ||
454 | if ((abfd->flags & BFD_IN_MEMORY) != 0) | |
455 | abort (); | |
456 | #ifdef HAVE_MMAP | |
457 | else | |
458 | { | |
48d96f80 | 459 | static uintptr_t pagesize_m1; |
4c95ab76 TG |
460 | FILE *f; |
461 | file_ptr pg_offset; | |
462 | bfd_size_type pg_len; | |
463 | ||
464 | f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR); | |
25b88f33 PP |
465 | if (f == NULL) |
466 | return ret; | |
467 | ||
48d96f80 | 468 | if (pagesize_m1 == 0) |
07d6d2b8 | 469 | pagesize_m1 = getpagesize () - 1; |
48d96f80 | 470 | |
4c95ab76 | 471 | /* Handle archive members. */ |
b0cffb47 AM |
472 | if (abfd->my_archive != NULL |
473 | && !bfd_is_thin_archive (abfd->my_archive)) | |
07d6d2b8 | 474 | offset += abfd->origin; |
4c95ab76 TG |
475 | |
476 | /* Align. */ | |
477 | pg_offset = offset & ~pagesize_m1; | |
478 | pg_len = (len + (offset - pg_offset) + pagesize_m1) & ~pagesize_m1; | |
479 | ||
480 | ret = mmap (addr, pg_len, prot, flags, fileno (f), pg_offset); | |
25b88f33 PP |
481 | if (ret == (void *) -1) |
482 | bfd_set_error (bfd_error_system_call); | |
4c95ab76 | 483 | else |
07d6d2b8 AM |
484 | { |
485 | *map_addr = ret; | |
486 | *map_len = pg_len; | |
487 | ret = (char *) ret + (offset & pagesize_m1); | |
488 | } | |
25b88f33 PP |
489 | } |
490 | #endif | |
491 | ||
492 | return ret; | |
493 | } | |
494 | ||
cb5220a0 NC |
495 | static const struct bfd_iovec cache_iovec = |
496 | { | |
40838a72 | 497 | &cache_bread, &cache_bwrite, &cache_btell, &cache_bseek, |
25b88f33 | 498 | &cache_bclose, &cache_bflush, &cache_bstat, &cache_bmmap |
40838a72 AC |
499 | }; |
500 | ||
252b5132 RH |
501 | /* |
502 | INTERNAL_FUNCTION | |
503 | bfd_cache_init | |
504 | ||
505 | SYNOPSIS | |
b34976b6 | 506 | bfd_boolean bfd_cache_init (bfd *abfd); |
252b5132 RH |
507 | |
508 | DESCRIPTION | |
509 | Add a newly opened BFD to the cache. | |
510 | */ | |
511 | ||
b34976b6 | 512 | bfd_boolean |
c58b9523 | 513 | bfd_cache_init (bfd *abfd) |
252b5132 RH |
514 | { |
515 | BFD_ASSERT (abfd->iostream != NULL); | |
9d782e8d | 516 | if (open_files >= bfd_cache_max_open ()) |
252b5132 RH |
517 | { |
518 | if (! close_one ()) | |
b34976b6 | 519 | return FALSE; |
252b5132 | 520 | } |
40838a72 | 521 | abfd->iovec = &cache_iovec; |
252b5132 RH |
522 | insert (abfd); |
523 | ++open_files; | |
b34976b6 | 524 | return TRUE; |
252b5132 RH |
525 | } |
526 | ||
527 | /* | |
528 | INTERNAL_FUNCTION | |
529 | bfd_cache_close | |
530 | ||
531 | SYNOPSIS | |
b34976b6 | 532 | bfd_boolean bfd_cache_close (bfd *abfd); |
252b5132 RH |
533 | |
534 | DESCRIPTION | |
535 | Remove the BFD @var{abfd} from the cache. If the attached file is open, | |
536 | then close it too. | |
537 | ||
538 | RETURNS | |
b34976b6 | 539 | <<FALSE>> is returned if closing the file fails, <<TRUE>> is |
252b5132 RH |
540 | returned if all is well. |
541 | */ | |
542 | ||
b34976b6 | 543 | bfd_boolean |
c58b9523 | 544 | bfd_cache_close (bfd *abfd) |
252b5132 | 545 | { |
40838a72 | 546 | if (abfd->iovec != &cache_iovec) |
b34976b6 | 547 | return TRUE; |
252b5132 | 548 | |
fe2e161a AC |
549 | if (abfd->iostream == NULL) |
550 | /* Previously closed. */ | |
551 | return TRUE; | |
552 | ||
252b5132 RH |
553 | return bfd_cache_delete (abfd); |
554 | } | |
555 | ||
02d5a37b JG |
556 | /* |
557 | FUNCTION | |
558 | bfd_cache_close_all | |
559 | ||
560 | SYNOPSIS | |
561 | bfd_boolean bfd_cache_close_all (void); | |
562 | ||
563 | DESCRIPTION | |
564 | Remove all BFDs from the cache. If the attached file is open, | |
565 | then close it too. | |
566 | ||
567 | RETURNS | |
568 | <<FALSE>> is returned if closing one of the file fails, <<TRUE>> is | |
569 | returned if all is well. | |
570 | */ | |
571 | ||
572 | bfd_boolean | |
e6c7cdec | 573 | bfd_cache_close_all (void) |
02d5a37b JG |
574 | { |
575 | bfd_boolean ret = TRUE; | |
576 | ||
577 | while (bfd_last_cache != NULL) | |
578 | ret &= bfd_cache_close (bfd_last_cache); | |
c9b549b2 JG |
579 | |
580 | return ret; | |
02d5a37b JG |
581 | } |
582 | ||
252b5132 RH |
583 | /* |
584 | INTERNAL_FUNCTION | |
585 | bfd_open_file | |
586 | ||
587 | SYNOPSIS | |
c58b9523 | 588 | FILE* bfd_open_file (bfd *abfd); |
252b5132 RH |
589 | |
590 | DESCRIPTION | |
591 | Call the OS to open a file for @var{abfd}. Return the <<FILE *>> | |
592 | (possibly <<NULL>>) that results from this operation. Set up the | |
593 | BFD so that future accesses know the file is open. If the <<FILE *>> | |
594 | returned is <<NULL>>, then it won't have been put in the | |
595 | cache, so it won't have to be removed from it. | |
596 | */ | |
597 | ||
598 | FILE * | |
c58b9523 | 599 | bfd_open_file (bfd *abfd) |
252b5132 | 600 | { |
b34976b6 | 601 | abfd->cacheable = TRUE; /* Allow it to be closed later. */ |
252b5132 | 602 | |
9d782e8d | 603 | if (open_files >= bfd_cache_max_open ()) |
252b5132 RH |
604 | { |
605 | if (! close_one ()) | |
606 | return NULL; | |
607 | } | |
608 | ||
609 | switch (abfd->direction) | |
610 | { | |
611 | case read_direction: | |
612 | case no_direction: | |
c7c3d11b | 613 | abfd->iostream = _bfd_real_fopen (abfd->filename, FOPEN_RB); |
252b5132 RH |
614 | break; |
615 | case both_direction: | |
616 | case write_direction: | |
82e51918 | 617 | if (abfd->opened_once) |
252b5132 | 618 | { |
c7c3d11b | 619 | abfd->iostream = _bfd_real_fopen (abfd->filename, FOPEN_RUB); |
252b5132 | 620 | if (abfd->iostream == NULL) |
c7c3d11b | 621 | abfd->iostream = _bfd_real_fopen (abfd->filename, FOPEN_WUB); |
252b5132 RH |
622 | } |
623 | else | |
624 | { | |
9e422a2e ILT |
625 | /* Create the file. |
626 | ||
627 | Some operating systems won't let us overwrite a running | |
628 | binary. For them, we want to unlink the file first. | |
629 | ||
630 | However, gcc 2.95 will create temporary files using | |
631 | O_EXCL and tight permissions to prevent other users from | |
632 | substituting other .o files during the compilation. gcc | |
633 | will then tell the assembler to use the newly created | |
634 | file as an output file. If we unlink the file here, we | |
635 | open a brief window when another user could still | |
636 | substitute a file. | |
637 | ||
638 | So we unlink the output file if and only if it has | |
639 | non-zero size. */ | |
5af11cab AM |
640 | #ifndef __MSDOS__ |
641 | /* Don't do this for MSDOS: it doesn't care about overwriting | |
642 | a running binary, but if this file is already open by | |
643 | another BFD, we will be in deep trouble if we delete an | |
644 | open file. In fact, objdump does just that if invoked with | |
645 | the --info option. */ | |
9e422a2e ILT |
646 | struct stat s; |
647 | ||
648 | if (stat (abfd->filename, &s) == 0 && s.st_size != 0) | |
bb14f524 | 649 | unlink_if_ordinary (abfd->filename); |
5af11cab | 650 | #endif |
c7c3d11b | 651 | abfd->iostream = _bfd_real_fopen (abfd->filename, FOPEN_WUB); |
b34976b6 | 652 | abfd->opened_once = TRUE; |
252b5132 RH |
653 | } |
654 | break; | |
655 | } | |
656 | ||
5c91cdfb AM |
657 | if (abfd->iostream == NULL) |
658 | bfd_set_error (bfd_error_system_call); | |
659 | else | |
252b5132 RH |
660 | { |
661 | if (! bfd_cache_init (abfd)) | |
662 | return NULL; | |
663 | } | |
664 | ||
665 | return (FILE *) abfd->iostream; | |
666 | } |