Merge branch 'vendor/GREP'
[dragonfly.git] / contrib / binutils-2.24 / bfd / cache.c
1 /* BFD library -- caching of file descriptors.
2
3    Copyright 1990, 1991, 1992, 1993, 1994, 1996, 2000, 2001, 2002,
4    2003, 2004, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
5
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 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.  */
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
38         handle.
39
40 SUBSECTION
41         Caching functions
42 */
43
44 #include "sysdep.h"
45 #include "bfd.h"
46 #include "libbfd.h"
47 #include "libiberty.h"
48 #include "bfd_stdint.h"
49
50 #ifdef HAVE_MMAP
51 #include <sys/mman.h>
52 #endif
53
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
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.  */
71
72 static int max_open_files = 0;
73
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.  */
76 static int
77 bfd_cache_max_open (void)
78 {
79   if (max_open_files == 0)
80     {
81       int max;
82 #ifdef HAVE_GETRLIMIT
83       struct rlimit rlim;
84       if (getrlimit (RLIMIT_NOFILE, &rlim) == 0
85           && rlim.rlim_cur != RLIM_INFINITY)
86         max = rlim.rlim_cur / 8;
87       else
88 #endif /* HAVE_GETRLIMIT */
89 #ifdef _SC_OPEN_MAX
90         max = sysconf (_SC_OPEN_MAX) / 8;
91 #else
92         max = 10;
93 #endif /* _SC_OPEN_MAX */
94       max_open_files = max < 10 ? 10 : max;
95     }
96
97   return max_open_files;
98 }
99
100 /* The number of BFD files we have open.  */
101
102 static int open_files;
103
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.  */
107
108 static bfd *bfd_last_cache = NULL;
109
110 /* Insert a BFD into the cache.  */
111
112 static void
113 insert (bfd *abfd)
114 {
115   if (bfd_last_cache == NULL)
116     {
117       abfd->lru_next = abfd;
118       abfd->lru_prev = abfd;
119     }
120   else
121     {
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;
126     }
127   bfd_last_cache = abfd;
128 }
129
130 /* Remove a BFD from the cache.  */
131
132 static void
133 snip (bfd *abfd)
134 {
135   abfd->lru_prev->lru_next = abfd->lru_next;
136   abfd->lru_next->lru_prev = abfd->lru_prev;
137   if (abfd == bfd_last_cache)
138     {
139       bfd_last_cache = abfd->lru_next;
140       if (abfd == bfd_last_cache)
141         bfd_last_cache = NULL;
142     }
143 }
144
145 /* Close a BFD and remove it from the cache.  */
146
147 static bfd_boolean
148 bfd_cache_delete (bfd *abfd)
149 {
150   bfd_boolean ret;
151
152   if (fclose ((FILE *) abfd->iostream) == 0)
153     ret = TRUE;
154   else
155     {
156       ret = FALSE;
157       bfd_set_error (bfd_error_system_call);
158     }
159
160   snip (abfd);
161
162   abfd->iostream = NULL;
163   --open_files;
164
165   return ret;
166 }
167
168 /* We need to open a new file, and the cache is full.  Find the least
169    recently used cacheable BFD and close it.  */
170
171 static bfd_boolean
172 close_one (void)
173 {
174   register bfd *to_kill;
175
176   if (bfd_last_cache == NULL)
177     to_kill = NULL;
178   else
179     {
180       for (to_kill = bfd_last_cache->lru_prev;
181            ! to_kill->cacheable;
182            to_kill = to_kill->lru_prev)
183         {
184           if (to_kill == bfd_last_cache)
185             {
186               to_kill = NULL;
187               break;
188             }
189         }
190     }
191
192   if (to_kill == NULL)
193     {
194       /* There are no open cacheable BFD's.  */
195       return TRUE;
196     }
197
198   to_kill->where = real_ftell ((FILE *) to_kill->iostream);
199
200   return bfd_cache_delete (to_kill);
201 }
202
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.  */
207
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))
212
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}.  */
219
220 static FILE *
221 bfd_cache_lookup_worker (bfd *abfd, enum cache_flag flag)
222 {
223   bfd *orig_bfd = abfd;
224   if ((abfd->flags & BFD_IN_MEMORY) != 0)
225     abort ();
226
227   while (abfd->my_archive)
228     abfd = abfd->my_archive;
229
230   if (abfd->iostream != NULL)
231     {
232       /* Move the file to the start of the cache.  */
233       if (abfd != bfd_last_cache)
234         {
235           snip (abfd);
236           insert (abfd);
237         }
238       return (FILE *) abfd->iostream;
239     }
240
241   if (flag & CACHE_NO_OPEN)
242     return NULL;
243
244   if (bfd_open_file (abfd) == NULL)
245     ;
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);
250   else
251     return (FILE *) abfd->iostream;
252
253   (*_bfd_error_handler) (_("reopening %B: %s\n"),
254                          orig_bfd, bfd_errmsg (bfd_get_error ()));
255   return NULL;
256 }
257
258 static file_ptr
259 cache_btell (struct bfd *abfd)
260 {
261   FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
262   if (f == NULL)
263     return abfd->where;
264   return real_ftell (f);
265 }
266
267 static int
268 cache_bseek (struct bfd *abfd, file_ptr offset, int whence)
269 {
270   FILE *f = bfd_cache_lookup (abfd, whence != SEEK_CUR ? CACHE_NO_SEEK : CACHE_NORMAL);
271   if (f == NULL)
272     return -1;
273   return real_fseek (f, offset, whence);
274 }
275
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.
278
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.  */
282
283 static file_ptr
284 cache_bread_1 (struct bfd *abfd, void *buf, file_ptr nbytes)
285 {
286   FILE *f;
287   file_ptr nread;
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 */
297   if (nbytes == 0)
298     return 0;
299
300   f = bfd_cache_lookup (abfd, CACHE_NORMAL);
301   if (f == NULL)
302     return 0;
303
304 #if defined (__VAX) && defined (VMS)
305   /* Apparently fread on Vax VMS does not keep the record length
306      information.  */
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)
312     {
313       bfd_set_error (bfd_error_system_call);
314       return -1;
315     }
316 #else
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))
322     {
323       bfd_set_error (bfd_error_system_call);
324       return -1;
325     }
326 #endif
327   if (nread < nbytes)
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);
331   return nread;
332 }
333
334 static file_ptr
335 cache_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
336 {
337   file_ptr nread = 0;
338
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)
343     {
344       const file_ptr max_chunk_size = 0x800000;
345       file_ptr chunk_size = nbytes - nread;
346       file_ptr chunk_nread;
347
348       if (chunk_size > max_chunk_size)
349         chunk_size = max_chunk_size;
350
351       chunk_nread = cache_bread_1 (abfd, (char *) buf + nread, chunk_size);
352
353       /* Update the nread count.
354
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
360          did.  */
361       if (nread == 0 || chunk_nread > 0)
362         nread += chunk_nread;
363
364       if (chunk_nread < chunk_size)
365         break;
366     }
367
368   return nread;
369 }
370
371 static file_ptr
372 cache_bwrite (struct bfd *abfd, const void *where, file_ptr nbytes)
373 {
374   file_ptr nwrite;
375   FILE *f = bfd_cache_lookup (abfd, CACHE_NORMAL);
376
377   if (f == NULL)
378     return 0;
379   nwrite = fwrite (where, 1, nbytes, f);
380   if (nwrite < nbytes && ferror (f))
381     {
382       bfd_set_error (bfd_error_system_call);
383       return -1;
384     }
385   return nwrite;
386 }
387
388 static int
389 cache_bclose (struct bfd *abfd)
390 {
391   return bfd_cache_close (abfd) - 1;
392 }
393
394 static int
395 cache_bflush (struct bfd *abfd)
396 {
397   int sts;
398   FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
399
400   if (f == NULL)
401     return 0;
402   sts = fflush (f);
403   if (sts < 0)
404     bfd_set_error (bfd_error_system_call);
405   return sts;
406 }
407
408 static int
409 cache_bstat (struct bfd *abfd, struct stat *sb)
410 {
411   int sts;
412   FILE *f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR);
413
414   if (f == NULL)
415     return -1;
416   sts = fstat (fileno (f), sb);
417   if (sts < 0)
418     bfd_set_error (bfd_error_system_call);
419   return sts;
420 }
421
422 static void *
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)
431 {
432   void *ret = (void *) -1;
433
434   if ((abfd->flags & BFD_IN_MEMORY) != 0)
435     abort ();
436 #ifdef HAVE_MMAP
437   else
438     {
439       static uintptr_t pagesize_m1;
440       FILE *f;
441       file_ptr pg_offset;
442       bfd_size_type pg_len;
443
444       f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR);
445       if (f == NULL)
446         return ret;
447
448       if (pagesize_m1 == 0)
449         pagesize_m1 = getpagesize () - 1;
450
451       /* Handle archive members.  */
452       if (abfd->my_archive != NULL)
453         offset += abfd->origin;
454
455       /* Align.  */
456       pg_offset = offset & ~pagesize_m1;
457       pg_len = (len + (offset - pg_offset) + pagesize_m1) & ~pagesize_m1;
458
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);
462       else
463         {
464           *map_addr = ret;
465           *map_len = pg_len;
466           ret = (char *) ret + (offset & pagesize_m1);
467         }
468     }
469 #endif
470
471   return ret;
472 }
473
474 static const struct bfd_iovec cache_iovec =
475 {
476   &cache_bread, &cache_bwrite, &cache_btell, &cache_bseek,
477   &cache_bclose, &cache_bflush, &cache_bstat, &cache_bmmap
478 };
479
480 /*
481 INTERNAL_FUNCTION
482         bfd_cache_init
483
484 SYNOPSIS
485         bfd_boolean bfd_cache_init (bfd *abfd);
486
487 DESCRIPTION
488         Add a newly opened BFD to the cache.
489 */
490
491 bfd_boolean
492 bfd_cache_init (bfd *abfd)
493 {
494   BFD_ASSERT (abfd->iostream != NULL);
495   if (open_files >= bfd_cache_max_open ())
496     {
497       if (! close_one ())
498         return FALSE;
499     }
500   abfd->iovec = &cache_iovec;
501   insert (abfd);
502   ++open_files;
503   return TRUE;
504 }
505
506 /*
507 INTERNAL_FUNCTION
508         bfd_cache_close
509
510 SYNOPSIS
511         bfd_boolean bfd_cache_close (bfd *abfd);
512
513 DESCRIPTION
514         Remove the BFD @var{abfd} from the cache. If the attached file is open,
515         then close it too.
516
517 RETURNS
518         <<FALSE>> is returned if closing the file fails, <<TRUE>> is
519         returned if all is well.
520 */
521
522 bfd_boolean
523 bfd_cache_close (bfd *abfd)
524 {
525   if (abfd->iovec != &cache_iovec)
526     return TRUE;
527
528   if (abfd->iostream == NULL)
529     /* Previously closed.  */
530     return TRUE;
531
532   return bfd_cache_delete (abfd);
533 }
534
535 /*
536 FUNCTION
537         bfd_cache_close_all
538
539 SYNOPSIS
540         bfd_boolean bfd_cache_close_all (void);
541
542 DESCRIPTION
543         Remove all BFDs from the cache. If the attached file is open,
544         then close it too.
545
546 RETURNS
547         <<FALSE>> is returned if closing one of the file fails, <<TRUE>> is
548         returned if all is well.
549 */
550
551 bfd_boolean
552 bfd_cache_close_all ()
553 {
554   bfd_boolean ret = TRUE;
555
556   while (bfd_last_cache != NULL)
557     ret &= bfd_cache_close (bfd_last_cache);
558
559   return ret;
560 }
561
562 /*
563 INTERNAL_FUNCTION
564         bfd_open_file
565
566 SYNOPSIS
567         FILE* bfd_open_file (bfd *abfd);
568
569 DESCRIPTION
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.
575 */
576
577 FILE *
578 bfd_open_file (bfd *abfd)
579 {
580   abfd->cacheable = TRUE;       /* Allow it to be closed later.  */
581
582   if (open_files >= bfd_cache_max_open ())
583     {
584       if (! close_one ())
585         return NULL;
586     }
587
588   switch (abfd->direction)
589     {
590     case read_direction:
591     case no_direction:
592       abfd->iostream = real_fopen (abfd->filename, FOPEN_RB);
593       break;
594     case both_direction:
595     case write_direction:
596       if (abfd->opened_once)
597         {
598           abfd->iostream = real_fopen (abfd->filename, FOPEN_RUB);
599           if (abfd->iostream == NULL)
600             abfd->iostream = real_fopen (abfd->filename, FOPEN_WUB);
601         }
602       else
603         {
604           /* Create the file.
605
606              Some operating systems won't let us overwrite a running
607              binary.  For them, we want to unlink the file first.
608
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
615              substitute a file.
616
617              So we unlink the output file if and only if it has
618              non-zero size.  */
619 #ifndef __MSDOS__
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.  */
625           struct stat s;
626
627           if (stat (abfd->filename, &s) == 0 && s.st_size != 0)
628             unlink_if_ordinary (abfd->filename);
629 #endif
630           abfd->iostream = real_fopen (abfd->filename, FOPEN_WUB);
631           abfd->opened_once = TRUE;
632         }
633       break;
634     }
635
636   if (abfd->iostream == NULL)
637     bfd_set_error (bfd_error_system_call);
638   else
639     {
640       if (! bfd_cache_init (abfd))
641         return NULL;
642     }
643
644   return (FILE *) abfd->iostream;
645 }