1 /* BFD library -- caching of file descriptors.
3 Copyright 1990, 1991, 1992, 1993, 1994, 1996, 2000, 2001, 2002,
4 2003, 2004, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
6 Hacked by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
8 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
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
47 #include "libiberty.h"
48 #include "bfd_stdint.h"
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
66 CACHE_NO_SEEK_ERROR
= 4
69 /* The maximum number of files which the cache will keep open at
70 one time. When needed call bfd_cache_max_open to initialize. */
72 static int max_open_files
= 0;
74 /* Set max_open_files, if not already set, to 12.5% of the allowed open
75 file descriptors, but at least 10, and return the value. */
77 bfd_cache_max_open (void)
79 if (max_open_files
== 0)
84 if (getrlimit (RLIMIT_NOFILE
, &rlim
) == 0
85 && rlim
.rlim_cur
!= (rlim_t
) RLIM_INFINITY
)
86 max
= rlim
.rlim_cur
/ 8;
88 #endif /* HAVE_GETRLIMIT */
90 max
= sysconf (_SC_OPEN_MAX
) / 8;
93 #endif /* _SC_OPEN_MAX */
94 max_open_files
= max
< 10 ? 10 : max
;
97 return max_open_files
;
100 /* The number of BFD files we have open. */
102 static int open_files
;
104 /* Zero, or a pointer to the topmost BFD on the chain. This is
105 used by the <<bfd_cache_lookup>> macro in @file{libbfd.h} to
106 determine when it can avoid a function call. */
108 static bfd
*bfd_last_cache
= NULL
;
110 /* Insert a BFD into the cache. */
115 if (bfd_last_cache
== NULL
)
117 abfd
->lru_next
= abfd
;
118 abfd
->lru_prev
= abfd
;
122 abfd
->lru_next
= bfd_last_cache
;
123 abfd
->lru_prev
= bfd_last_cache
->lru_prev
;
124 abfd
->lru_prev
->lru_next
= abfd
;
125 abfd
->lru_next
->lru_prev
= abfd
;
127 bfd_last_cache
= abfd
;
130 /* Remove a BFD from the cache. */
135 abfd
->lru_prev
->lru_next
= abfd
->lru_next
;
136 abfd
->lru_next
->lru_prev
= abfd
->lru_prev
;
137 if (abfd
== bfd_last_cache
)
139 bfd_last_cache
= abfd
->lru_next
;
140 if (abfd
== bfd_last_cache
)
141 bfd_last_cache
= NULL
;
145 /* Close a BFD and remove it from the cache. */
148 bfd_cache_delete (bfd
*abfd
)
152 if (fclose ((FILE *) abfd
->iostream
) == 0)
157 bfd_set_error (bfd_error_system_call
);
162 abfd
->iostream
= NULL
;
168 /* We need to open a new file, and the cache is full. Find the least
169 recently used cacheable BFD and close it. */
174 register bfd
*to_kill
;
176 if (bfd_last_cache
== NULL
)
180 for (to_kill
= bfd_last_cache
->lru_prev
;
181 ! to_kill
->cacheable
;
182 to_kill
= to_kill
->lru_prev
)
184 if (to_kill
== bfd_last_cache
)
194 /* There are no open cacheable BFD's. */
198 to_kill
->where
= real_ftell ((FILE *) to_kill
->iostream
);
200 return bfd_cache_delete (to_kill
);
203 /* Check to see if the required BFD is the same as the last one
204 looked up. If so, then it can use the stream in the BFD with
205 impunity, since it can't have changed since the last lookup;
206 otherwise, it has to perform the complicated lookup function. */
208 #define bfd_cache_lookup(x, flag) \
209 ((x) == bfd_last_cache \
210 ? (FILE *) (bfd_last_cache->iostream) \
211 : bfd_cache_lookup_worker (x, flag))
213 /* Called when the macro <<bfd_cache_lookup>> fails to find a
214 quick answer. Find a file descriptor for @var{abfd}. If
215 necessary, it open it. If there are already more than
216 <<bfd_cache_max_open>> files open, it tries to close one first, to
217 avoid running out of file descriptors. It will return NULL
218 if it is unable to (re)open the @var{abfd}. */
221 bfd_cache_lookup_worker (bfd
*abfd
, enum cache_flag flag
)
223 bfd
*orig_bfd
= abfd
;
224 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
227 while (abfd
->my_archive
)
228 abfd
= abfd
->my_archive
;
230 if (abfd
->iostream
!= NULL
)
232 /* Move the file to the start of the cache. */
233 if (abfd
!= bfd_last_cache
)
238 return (FILE *) abfd
->iostream
;
241 if (flag
& CACHE_NO_OPEN
)
244 if (bfd_open_file (abfd
) == NULL
)
246 else if (!(flag
& CACHE_NO_SEEK
)
247 && real_fseek ((FILE *) abfd
->iostream
, abfd
->where
, SEEK_SET
) != 0
248 && !(flag
& CACHE_NO_SEEK_ERROR
))
249 bfd_set_error (bfd_error_system_call
);
251 return (FILE *) abfd
->iostream
;
253 (*_bfd_error_handler
) (_("reopening %B: %s\n"),
254 orig_bfd
, bfd_errmsg (bfd_get_error ()));
259 cache_btell (struct bfd
*abfd
)
261 FILE *f
= bfd_cache_lookup (abfd
, CACHE_NO_OPEN
);
264 return real_ftell (f
);
268 cache_bseek (struct bfd
*abfd
, file_ptr offset
, int whence
)
270 FILE *f
= bfd_cache_lookup (abfd
, whence
!= SEEK_CUR
? CACHE_NO_SEEK
: CACHE_NORMAL
);
273 return real_fseek (f
, offset
, whence
);
276 /* Note that archive entries don't have streams; they share their parent's.
277 This allows someone to play with the iostream behind BFD's back.
279 Also, note that the origin pointer points to the beginning of a file's
280 contents (0 for non-archive elements). For archive entries this is the
281 first octet in the file, NOT the beginning of the archive header. */
284 cache_bread_1 (struct bfd
*abfd
, void *buf
, file_ptr nbytes
)
288 /* FIXME - this looks like an optimization, but it's really to cover
289 up for a feature of some OSs (not solaris - sigh) that
290 ld/pe-dll.c takes advantage of (apparently) when it creates BFDs
291 internally and tries to link against them. BFD seems to be smart
292 enough to realize there are no symbol records in the "file" that
293 doesn't exist but attempts to read them anyway. On Solaris,
294 attempting to read zero bytes from a NULL file results in a core
295 dump, but on other platforms it just returns zero bytes read.
296 This makes it to something reasonable. - DJ */
300 f
= bfd_cache_lookup (abfd
, CACHE_NORMAL
);
304 #if defined (__VAX) && defined (VMS)
305 /* Apparently fread on Vax VMS does not keep the record length
307 nread
= read (fileno (f
), buf
, nbytes
);
308 /* Set bfd_error if we did not read as much data as we expected. If
309 the read failed due to an error set the bfd_error_system_call,
310 else set bfd_error_file_truncated. */
311 if (nread
== (file_ptr
)-1)
313 bfd_set_error (bfd_error_system_call
);
317 nread
= fread (buf
, 1, nbytes
, f
);
318 /* Set bfd_error if we did not read as much data as we expected. If
319 the read failed due to an error set the bfd_error_system_call,
320 else set bfd_error_file_truncated. */
321 if (nread
< nbytes
&& ferror (f
))
323 bfd_set_error (bfd_error_system_call
);
328 /* This may or may not be an error, but in case the calling code
329 bails out because of it, set the right error code. */
330 bfd_set_error (bfd_error_file_truncated
);
335 cache_bread (struct bfd
*abfd
, void *buf
, file_ptr nbytes
)
339 /* Some filesystems are unable to handle reads that are too large
340 (for instance, NetApp shares with oplocks turned off). To avoid
341 hitting this limitation, we read the buffer in chunks of 8MB max. */
342 while (nread
< nbytes
)
344 const file_ptr max_chunk_size
= 0x800000;
345 file_ptr chunk_size
= nbytes
- nread
;
346 file_ptr chunk_nread
;
348 if (chunk_size
> max_chunk_size
)
349 chunk_size
= max_chunk_size
;
351 chunk_nread
= cache_bread_1 (abfd
, (char *) buf
+ nread
, chunk_size
);
353 /* Update the nread count.
355 We just have to be careful of the case when cache_bread_1 returns
356 a negative count: If this is our first read, then set nread to
357 that negative count in order to return that negative value to the
358 caller. Otherwise, don't add it to our total count, or we would
359 end up returning a smaller number of bytes read than we actually
361 if (nread
== 0 || chunk_nread
> 0)
362 nread
+= chunk_nread
;
364 if (chunk_nread
< chunk_size
)
372 cache_bwrite (struct bfd
*abfd
, const void *where
, file_ptr nbytes
)
375 FILE *f
= bfd_cache_lookup (abfd
, CACHE_NORMAL
);
379 nwrite
= fwrite (where
, 1, nbytes
, f
);
380 if (nwrite
< nbytes
&& ferror (f
))
382 bfd_set_error (bfd_error_system_call
);
389 cache_bclose (struct bfd
*abfd
)
391 return bfd_cache_close (abfd
) - 1;
395 cache_bflush (struct bfd
*abfd
)
398 FILE *f
= bfd_cache_lookup (abfd
, CACHE_NO_OPEN
);
404 bfd_set_error (bfd_error_system_call
);
409 cache_bstat (struct bfd
*abfd
, struct stat
*sb
)
412 FILE *f
= bfd_cache_lookup (abfd
, CACHE_NO_SEEK_ERROR
);
416 sts
= fstat (fileno (f
), sb
);
418 bfd_set_error (bfd_error_system_call
);
423 cache_bmmap (struct bfd
*abfd ATTRIBUTE_UNUSED
,
424 void *addr ATTRIBUTE_UNUSED
,
425 bfd_size_type len ATTRIBUTE_UNUSED
,
426 int prot ATTRIBUTE_UNUSED
,
427 int flags ATTRIBUTE_UNUSED
,
428 file_ptr offset ATTRIBUTE_UNUSED
,
429 void **map_addr ATTRIBUTE_UNUSED
,
430 bfd_size_type
*map_len ATTRIBUTE_UNUSED
)
432 void *ret
= (void *) -1;
434 if ((abfd
->flags
& BFD_IN_MEMORY
) != 0)
439 static uintptr_t pagesize_m1
;
442 bfd_size_type pg_len
;
444 f
= bfd_cache_lookup (abfd
, CACHE_NO_SEEK_ERROR
);
448 if (pagesize_m1
== 0)
449 pagesize_m1
= getpagesize () - 1;
451 /* Handle archive members. */
452 if (abfd
->my_archive
!= NULL
)
453 offset
+= abfd
->origin
;
456 pg_offset
= offset
& ~pagesize_m1
;
457 pg_len
= (len
+ (offset
- pg_offset
) + pagesize_m1
) & ~pagesize_m1
;
459 ret
= mmap (addr
, pg_len
, prot
, flags
, fileno (f
), pg_offset
);
460 if (ret
== (void *) -1)
461 bfd_set_error (bfd_error_system_call
);
466 ret
= (char *) ret
+ (offset
& pagesize_m1
);
474 static const struct bfd_iovec cache_iovec
=
476 &cache_bread
, &cache_bwrite
, &cache_btell
, &cache_bseek
,
477 &cache_bclose
, &cache_bflush
, &cache_bstat
, &cache_bmmap
485 bfd_boolean bfd_cache_init (bfd *abfd);
488 Add a newly opened BFD to the cache.
492 bfd_cache_init (bfd
*abfd
)
494 BFD_ASSERT (abfd
->iostream
!= NULL
);
495 if (open_files
>= bfd_cache_max_open ())
500 abfd
->iovec
= &cache_iovec
;
511 bfd_boolean bfd_cache_close (bfd *abfd);
514 Remove the BFD @var{abfd} from the cache. If the attached file is open,
518 <<FALSE>> is returned if closing the file fails, <<TRUE>> is
519 returned if all is well.
523 bfd_cache_close (bfd
*abfd
)
525 if (abfd
->iovec
!= &cache_iovec
)
528 if (abfd
->iostream
== NULL
)
529 /* Previously closed. */
532 return bfd_cache_delete (abfd
);
540 bfd_boolean bfd_cache_close_all (void);
543 Remove all BFDs from the cache. If the attached file is open,
547 <<FALSE>> is returned if closing one of the file fails, <<TRUE>> is
548 returned if all is well.
552 bfd_cache_close_all ()
554 bfd_boolean ret
= TRUE
;
556 while (bfd_last_cache
!= NULL
)
557 ret
&= bfd_cache_close (bfd_last_cache
);
567 FILE* bfd_open_file (bfd *abfd);
570 Call the OS to open a file for @var{abfd}. Return the <<FILE *>>
571 (possibly <<NULL>>) that results from this operation. Set up the
572 BFD so that future accesses know the file is open. If the <<FILE *>>
573 returned is <<NULL>>, then it won't have been put in the
574 cache, so it won't have to be removed from it.
578 bfd_open_file (bfd
*abfd
)
580 abfd
->cacheable
= TRUE
; /* Allow it to be closed later. */
582 if (open_files
>= bfd_cache_max_open ())
588 switch (abfd
->direction
)
592 abfd
->iostream
= real_fopen (abfd
->filename
, FOPEN_RB
);
595 case write_direction
:
596 if (abfd
->opened_once
)
598 abfd
->iostream
= real_fopen (abfd
->filename
, FOPEN_RUB
);
599 if (abfd
->iostream
== NULL
)
600 abfd
->iostream
= real_fopen (abfd
->filename
, FOPEN_WUB
);
606 Some operating systems won't let us overwrite a running
607 binary. For them, we want to unlink the file first.
609 However, gcc 2.95 will create temporary files using
610 O_EXCL and tight permissions to prevent other users from
611 substituting other .o files during the compilation. gcc
612 will then tell the assembler to use the newly created
613 file as an output file. If we unlink the file here, we
614 open a brief window when another user could still
617 So we unlink the output file if and only if it has
620 /* Don't do this for MSDOS: it doesn't care about overwriting
621 a running binary, but if this file is already open by
622 another BFD, we will be in deep trouble if we delete an
623 open file. In fact, objdump does just that if invoked with
624 the --info option. */
627 if (stat (abfd
->filename
, &s
) == 0 && s
.st_size
!= 0)
628 unlink_if_ordinary (abfd
->filename
);
630 abfd
->iostream
= real_fopen (abfd
->filename
, FOPEN_WUB
);
631 abfd
->opened_once
= TRUE
;
636 if (abfd
->iostream
== NULL
)
637 bfd_set_error (bfd_error_system_call
);
640 if (! bfd_cache_init (abfd
))
644 return (FILE *) abfd
->iostream
;