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, | |
4 | 2003, 2004 Free Software Foundation, Inc. | |
5 | ||
252b5132 RH |
6 | Hacked by Steve Chamberlain of Cygnus Support (steve@cygnus.com). |
7 | ||
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 2 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 | |
3e110533 | 22 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 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 | |
33 | <<BFD_CACHE_MAX_OPEN>> files, and exports the name | |
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 | ||
43 | #include "bfd.h" | |
44 | #include "sysdep.h" | |
45 | #include "libbfd.h" | |
bb14f524 | 46 | #include "libiberty.h" |
252b5132 | 47 | |
c58b9523 | 48 | static bfd_boolean bfd_cache_delete (bfd *); |
252b5132 | 49 | |
40838a72 AC |
50 | |
51 | static file_ptr | |
52 | cache_btell (struct bfd *abfd) | |
53 | { | |
54 | return real_ftell (bfd_cache_lookup (abfd)); | |
55 | } | |
56 | ||
57 | static int | |
58 | cache_bseek (struct bfd *abfd, file_ptr offset, int whence) | |
59 | { | |
60 | return real_fseek (bfd_cache_lookup (abfd), offset, whence); | |
61 | } | |
62 | ||
63 | /* Note that archive entries don't have streams; they share their parent's. | |
64 | This allows someone to play with the iostream behind BFD's back. | |
65 | ||
66 | Also, note that the origin pointer points to the beginning of a file's | |
67 | contents (0 for non-archive elements). For archive entries this is the | |
68 | first octet in the file, NOT the beginning of the archive header. */ | |
69 | ||
70 | static file_ptr | |
71 | cache_bread (struct bfd *abfd, void *buf, file_ptr nbytes) | |
72 | { | |
73 | file_ptr nread; | |
74 | /* FIXME - this looks like an optimization, but it's really to cover | |
75 | up for a feature of some OSs (not solaris - sigh) that | |
76 | ld/pe-dll.c takes advantage of (apparently) when it creates BFDs | |
77 | internally and tries to link against them. BFD seems to be smart | |
78 | enough to realize there are no symbol records in the "file" that | |
79 | doesn't exist but attempts to read them anyway. On Solaris, | |
80 | attempting to read zero bytes from a NULL file results in a core | |
81 | dump, but on other platforms it just returns zero bytes read. | |
82 | This makes it to something reasonable. - DJ */ | |
83 | if (nbytes == 0) | |
84 | return 0; | |
85 | ||
86 | #if defined (__VAX) && defined (VMS) | |
87 | /* Apparently fread on Vax VMS does not keep the record length | |
88 | information. */ | |
89 | nread = read (fileno (bfd_cache_lookup (abfd)), buf, nbytes); | |
90 | /* Set bfd_error if we did not read as much data as we expected. If | |
91 | the read failed due to an error set the bfd_error_system_call, | |
92 | else set bfd_error_file_truncated. */ | |
93 | if (nread == (file_ptr)-1) | |
94 | { | |
95 | bfd_set_error (bfd_error_system_call); | |
96 | return -1; | |
97 | } | |
98 | #else | |
99 | nread = fread (buf, 1, nbytes, bfd_cache_lookup (abfd)); | |
100 | /* Set bfd_error if we did not read as much data as we expected. If | |
101 | the read failed due to an error set the bfd_error_system_call, | |
102 | else set bfd_error_file_truncated. */ | |
103 | if (nread < nbytes && ferror (bfd_cache_lookup (abfd))) | |
104 | { | |
105 | bfd_set_error (bfd_error_system_call); | |
106 | return -1; | |
107 | } | |
108 | #endif | |
109 | return nread; | |
110 | } | |
111 | ||
112 | static file_ptr | |
113 | cache_bwrite (struct bfd *abfd, const void *where, file_ptr nbytes) | |
114 | { | |
115 | file_ptr nwrite = fwrite (where, 1, nbytes, bfd_cache_lookup (abfd)); | |
116 | if (nwrite < nbytes && ferror (bfd_cache_lookup (abfd))) | |
117 | { | |
118 | bfd_set_error (bfd_error_system_call); | |
119 | return -1; | |
120 | } | |
121 | return nwrite; | |
122 | } | |
123 | ||
124 | static int | |
125 | cache_bclose (struct bfd *abfd) | |
126 | { | |
127 | return bfd_cache_close (abfd); | |
128 | } | |
129 | ||
130 | static int | |
131 | cache_bflush (struct bfd *abfd) | |
132 | { | |
133 | int sts = fflush (bfd_cache_lookup (abfd)); | |
134 | if (sts < 0) | |
135 | bfd_set_error (bfd_error_system_call); | |
136 | return sts; | |
137 | } | |
138 | ||
139 | static int | |
140 | cache_bstat (struct bfd *abfd, struct stat *sb) | |
141 | { | |
142 | int sts = fstat (fileno (bfd_cache_lookup (abfd)), sb); | |
143 | if (sts < 0) | |
144 | bfd_set_error (bfd_error_system_call); | |
145 | return sts; | |
146 | } | |
147 | ||
148 | static const struct bfd_iovec cache_iovec = { | |
149 | &cache_bread, &cache_bwrite, &cache_btell, &cache_bseek, | |
150 | &cache_bclose, &cache_bflush, &cache_bstat | |
151 | }; | |
152 | ||
252b5132 RH |
153 | /* |
154 | INTERNAL_FUNCTION | |
155 | BFD_CACHE_MAX_OPEN macro | |
156 | ||
157 | DESCRIPTION | |
158 | The maximum number of files which the cache will keep open at | |
159 | one time. | |
160 | ||
161 | .#define BFD_CACHE_MAX_OPEN 10 | |
162 | ||
163 | */ | |
164 | ||
165 | /* The number of BFD files we have open. */ | |
166 | ||
167 | static int open_files; | |
168 | ||
169 | /* | |
170 | INTERNAL_FUNCTION | |
171 | bfd_last_cache | |
172 | ||
173 | SYNOPSIS | |
174 | extern bfd *bfd_last_cache; | |
175 | ||
176 | DESCRIPTION | |
177 | Zero, or a pointer to the topmost BFD on the chain. This is | |
178 | used by the <<bfd_cache_lookup>> macro in @file{libbfd.h} to | |
179 | determine when it can avoid a function call. | |
180 | */ | |
181 | ||
1787c340 | 182 | bfd *bfd_last_cache = NULL; |
252b5132 RH |
183 | |
184 | /* | |
185 | INTERNAL_FUNCTION | |
186 | bfd_cache_lookup | |
e60b52c6 | 187 | |
252b5132 RH |
188 | DESCRIPTION |
189 | Check to see if the required BFD is the same as the last one | |
190 | looked up. If so, then it can use the stream in the BFD with | |
191 | impunity, since it can't have changed since the last lookup; | |
192 | otherwise, it has to perform the complicated lookup function. | |
e60b52c6 | 193 | |
252b5132 | 194 | .#define bfd_cache_lookup(x) \ |
06fc8a8c NC |
195 | . ((x) == bfd_last_cache ? \ |
196 | . (FILE *) (bfd_last_cache->iostream): \ | |
197 | . bfd_cache_lookup_worker (x)) | |
e60b52c6 | 198 | |
252b5132 RH |
199 | */ |
200 | ||
201 | /* Insert a BFD into the cache. */ | |
202 | ||
c58b9523 AM |
203 | static void |
204 | insert (bfd *abfd) | |
252b5132 RH |
205 | { |
206 | if (bfd_last_cache == NULL) | |
207 | { | |
208 | abfd->lru_next = abfd; | |
209 | abfd->lru_prev = abfd; | |
210 | } | |
211 | else | |
212 | { | |
213 | abfd->lru_next = bfd_last_cache; | |
214 | abfd->lru_prev = bfd_last_cache->lru_prev; | |
215 | abfd->lru_prev->lru_next = abfd; | |
216 | abfd->lru_next->lru_prev = abfd; | |
217 | } | |
218 | bfd_last_cache = abfd; | |
219 | } | |
220 | ||
221 | /* Remove a BFD from the cache. */ | |
222 | ||
c58b9523 AM |
223 | static void |
224 | snip (bfd *abfd) | |
252b5132 RH |
225 | { |
226 | abfd->lru_prev->lru_next = abfd->lru_next; | |
227 | abfd->lru_next->lru_prev = abfd->lru_prev; | |
228 | if (abfd == bfd_last_cache) | |
229 | { | |
230 | bfd_last_cache = abfd->lru_next; | |
231 | if (abfd == bfd_last_cache) | |
232 | bfd_last_cache = NULL; | |
233 | } | |
234 | } | |
235 | ||
236 | /* We need to open a new file, and the cache is full. Find the least | |
237 | recently used cacheable BFD and close it. */ | |
238 | ||
b34976b6 | 239 | static bfd_boolean |
c58b9523 | 240 | close_one (void) |
252b5132 RH |
241 | { |
242 | register bfd *kill; | |
243 | ||
244 | if (bfd_last_cache == NULL) | |
245 | kill = NULL; | |
246 | else | |
247 | { | |
248 | for (kill = bfd_last_cache->lru_prev; | |
249 | ! kill->cacheable; | |
250 | kill = kill->lru_prev) | |
251 | { | |
252 | if (kill == bfd_last_cache) | |
253 | { | |
254 | kill = NULL; | |
255 | break; | |
256 | } | |
257 | } | |
258 | } | |
259 | ||
260 | if (kill == NULL) | |
261 | { | |
262 | /* There are no open cacheable BFD's. */ | |
b34976b6 | 263 | return TRUE; |
252b5132 RH |
264 | } |
265 | ||
7c192733 | 266 | kill->where = real_ftell ((FILE *) kill->iostream); |
252b5132 RH |
267 | |
268 | return bfd_cache_delete (kill); | |
269 | } | |
270 | ||
271 | /* Close a BFD and remove it from the cache. */ | |
272 | ||
b34976b6 | 273 | static bfd_boolean |
c58b9523 | 274 | bfd_cache_delete (bfd *abfd) |
252b5132 | 275 | { |
b34976b6 | 276 | bfd_boolean ret; |
252b5132 RH |
277 | |
278 | if (fclose ((FILE *) abfd->iostream) == 0) | |
b34976b6 | 279 | ret = TRUE; |
252b5132 RH |
280 | else |
281 | { | |
b34976b6 | 282 | ret = FALSE; |
252b5132 RH |
283 | bfd_set_error (bfd_error_system_call); |
284 | } | |
285 | ||
286 | snip (abfd); | |
287 | ||
288 | abfd->iostream = NULL; | |
289 | --open_files; | |
290 | ||
291 | return ret; | |
292 | } | |
293 | ||
294 | /* | |
295 | INTERNAL_FUNCTION | |
296 | bfd_cache_init | |
297 | ||
298 | SYNOPSIS | |
b34976b6 | 299 | bfd_boolean bfd_cache_init (bfd *abfd); |
252b5132 RH |
300 | |
301 | DESCRIPTION | |
302 | Add a newly opened BFD to the cache. | |
303 | */ | |
304 | ||
b34976b6 | 305 | bfd_boolean |
c58b9523 | 306 | bfd_cache_init (bfd *abfd) |
252b5132 RH |
307 | { |
308 | BFD_ASSERT (abfd->iostream != NULL); | |
309 | if (open_files >= BFD_CACHE_MAX_OPEN) | |
310 | { | |
311 | if (! close_one ()) | |
b34976b6 | 312 | return FALSE; |
252b5132 | 313 | } |
40838a72 | 314 | abfd->iovec = &cache_iovec; |
252b5132 RH |
315 | insert (abfd); |
316 | ++open_files; | |
b34976b6 | 317 | return TRUE; |
252b5132 RH |
318 | } |
319 | ||
320 | /* | |
321 | INTERNAL_FUNCTION | |
322 | bfd_cache_close | |
323 | ||
324 | SYNOPSIS | |
b34976b6 | 325 | bfd_boolean bfd_cache_close (bfd *abfd); |
252b5132 RH |
326 | |
327 | DESCRIPTION | |
328 | Remove the BFD @var{abfd} from the cache. If the attached file is open, | |
329 | then close it too. | |
330 | ||
331 | RETURNS | |
b34976b6 | 332 | <<FALSE>> is returned if closing the file fails, <<TRUE>> is |
252b5132 RH |
333 | returned if all is well. |
334 | */ | |
335 | ||
b34976b6 | 336 | bfd_boolean |
c58b9523 | 337 | bfd_cache_close (bfd *abfd) |
252b5132 | 338 | { |
40838a72 | 339 | if (abfd->iovec != &cache_iovec) |
b34976b6 | 340 | return TRUE; |
252b5132 | 341 | |
fe2e161a AC |
342 | if (abfd->iostream == NULL) |
343 | /* Previously closed. */ | |
344 | return TRUE; | |
345 | ||
252b5132 RH |
346 | return bfd_cache_delete (abfd); |
347 | } | |
348 | ||
02d5a37b JG |
349 | /* |
350 | FUNCTION | |
351 | bfd_cache_close_all | |
352 | ||
353 | SYNOPSIS | |
354 | bfd_boolean bfd_cache_close_all (void); | |
355 | ||
356 | DESCRIPTION | |
357 | Remove all BFDs from the cache. If the attached file is open, | |
358 | then close it too. | |
359 | ||
360 | RETURNS | |
361 | <<FALSE>> is returned if closing one of the file fails, <<TRUE>> is | |
362 | returned if all is well. | |
363 | */ | |
364 | ||
365 | bfd_boolean | |
366 | bfd_cache_close_all () | |
367 | { | |
368 | bfd_boolean ret = TRUE; | |
369 | ||
370 | while (bfd_last_cache != NULL) | |
371 | ret &= bfd_cache_close (bfd_last_cache); | |
c9b549b2 JG |
372 | |
373 | return ret; | |
02d5a37b JG |
374 | } |
375 | ||
252b5132 RH |
376 | /* |
377 | INTERNAL_FUNCTION | |
378 | bfd_open_file | |
379 | ||
380 | SYNOPSIS | |
c58b9523 | 381 | FILE* bfd_open_file (bfd *abfd); |
252b5132 RH |
382 | |
383 | DESCRIPTION | |
384 | Call the OS to open a file for @var{abfd}. Return the <<FILE *>> | |
385 | (possibly <<NULL>>) that results from this operation. Set up the | |
386 | BFD so that future accesses know the file is open. If the <<FILE *>> | |
387 | returned is <<NULL>>, then it won't have been put in the | |
388 | cache, so it won't have to be removed from it. | |
389 | */ | |
390 | ||
391 | FILE * | |
c58b9523 | 392 | bfd_open_file (bfd *abfd) |
252b5132 | 393 | { |
b34976b6 | 394 | abfd->cacheable = TRUE; /* Allow it to be closed later. */ |
252b5132 RH |
395 | |
396 | if (open_files >= BFD_CACHE_MAX_OPEN) | |
397 | { | |
398 | if (! close_one ()) | |
399 | return NULL; | |
400 | } | |
401 | ||
402 | switch (abfd->direction) | |
403 | { | |
404 | case read_direction: | |
405 | case no_direction: | |
406 | abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_RB); | |
407 | break; | |
408 | case both_direction: | |
409 | case write_direction: | |
82e51918 | 410 | if (abfd->opened_once) |
252b5132 RH |
411 | { |
412 | abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_RUB); | |
413 | if (abfd->iostream == NULL) | |
414 | abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_WUB); | |
415 | } | |
416 | else | |
417 | { | |
9e422a2e ILT |
418 | /* Create the file. |
419 | ||
420 | Some operating systems won't let us overwrite a running | |
421 | binary. For them, we want to unlink the file first. | |
422 | ||
423 | However, gcc 2.95 will create temporary files using | |
424 | O_EXCL and tight permissions to prevent other users from | |
425 | substituting other .o files during the compilation. gcc | |
426 | will then tell the assembler to use the newly created | |
427 | file as an output file. If we unlink the file here, we | |
428 | open a brief window when another user could still | |
429 | substitute a file. | |
430 | ||
431 | So we unlink the output file if and only if it has | |
432 | non-zero size. */ | |
5af11cab AM |
433 | #ifndef __MSDOS__ |
434 | /* Don't do this for MSDOS: it doesn't care about overwriting | |
435 | a running binary, but if this file is already open by | |
436 | another BFD, we will be in deep trouble if we delete an | |
437 | open file. In fact, objdump does just that if invoked with | |
438 | the --info option. */ | |
9e422a2e ILT |
439 | struct stat s; |
440 | ||
441 | if (stat (abfd->filename, &s) == 0 && s.st_size != 0) | |
bb14f524 | 442 | unlink_if_ordinary (abfd->filename); |
5af11cab | 443 | #endif |
c46b7515 | 444 | abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_WUB); |
b34976b6 | 445 | abfd->opened_once = TRUE; |
252b5132 RH |
446 | } |
447 | break; | |
448 | } | |
449 | ||
5c91cdfb AM |
450 | if (abfd->iostream == NULL) |
451 | bfd_set_error (bfd_error_system_call); | |
452 | else | |
252b5132 RH |
453 | { |
454 | if (! bfd_cache_init (abfd)) | |
455 | return NULL; | |
456 | } | |
457 | ||
458 | return (FILE *) abfd->iostream; | |
459 | } | |
460 | ||
461 | /* | |
462 | INTERNAL_FUNCTION | |
463 | bfd_cache_lookup_worker | |
464 | ||
465 | SYNOPSIS | |
c58b9523 | 466 | FILE *bfd_cache_lookup_worker (bfd *abfd); |
252b5132 RH |
467 | |
468 | DESCRIPTION | |
469 | Called when the macro <<bfd_cache_lookup>> fails to find a | |
470 | quick answer. Find a file descriptor for @var{abfd}. If | |
471 | necessary, it open it. If there are already more than | |
472 | <<BFD_CACHE_MAX_OPEN>> files open, it tries to close one first, to | |
06fc8a8c NC |
473 | avoid running out of file descriptors. It will abort rather than |
474 | returning NULL if it is unable to (re)open the @var{abfd}. | |
252b5132 RH |
475 | */ |
476 | ||
477 | FILE * | |
c58b9523 | 478 | bfd_cache_lookup_worker (bfd *abfd) |
252b5132 | 479 | { |
d53e85d8 | 480 | bfd *orig_bfd = abfd; |
252b5132 RH |
481 | if ((abfd->flags & BFD_IN_MEMORY) != 0) |
482 | abort (); | |
483 | ||
e60b52c6 | 484 | if (abfd->my_archive) |
252b5132 RH |
485 | abfd = abfd->my_archive; |
486 | ||
487 | if (abfd->iostream != NULL) | |
488 | { | |
489 | /* Move the file to the start of the cache. */ | |
490 | if (abfd != bfd_last_cache) | |
491 | { | |
492 | snip (abfd); | |
493 | insert (abfd); | |
494 | } | |
5c91cdfb | 495 | return (FILE *) abfd->iostream; |
252b5132 | 496 | } |
5c91cdfb AM |
497 | |
498 | if (bfd_open_file (abfd) == NULL) | |
499 | ; | |
500 | else if (real_fseek ((FILE *) abfd->iostream, abfd->where, SEEK_SET) != 0) | |
501 | bfd_set_error (bfd_error_system_call); | |
252b5132 | 502 | else |
5c91cdfb | 503 | return (FILE *) abfd->iostream; |
252b5132 | 504 | |
d53e85d8 AM |
505 | (*_bfd_error_handler) (_("reopening %B: %s\n"), |
506 | orig_bfd, bfd_errmsg (bfd_get_error ())); | |
5c91cdfb AM |
507 | abort (); |
508 | return NULL; | |
252b5132 | 509 | } |