Home | History | Annotate | Download | only in bfd
      1 /* BFD library -- caching of file descriptors.
      2 
      3    Copyright (C) 1990-2016 Free Software Foundation, Inc.
      4 
      5    Hacked by Steve Chamberlain of Cygnus Support (steve (at) cygnus.com).
      6 
      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.  */
     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
     37 	handle.
     38 
     39 SUBSECTION
     40 	Caching functions
     41 */
     42 
     43 #include "sysdep.h"
     44 #include "bfd.h"
     45 #include "libbfd.h"
     46 #include "libiberty.h"
     47 #include "bfd_stdint.h"
     48 
     49 #ifdef HAVE_MMAP
     50 #include <sys/mman.h>
     51 #endif
     52 
     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 
     68 /* The maximum number of files which the cache will keep open at
     69    one time.  When needed call bfd_cache_max_open to initialize.  */
     70 
     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;
     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
     85          in "Too many open files" errors.  This can happen here even though
     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
     91          for 32-bit Solaris only.  (It is hoped that the 32-bit libc
     92 	 limitation will be removed soon).  64-bit Solaris libc does not have
     93 	 this limitation.  */
     94       max = 16;
     95 #else
     96 #ifdef HAVE_GETRLIMIT
     97       struct rlimit rlim;
     98 
     99       if (getrlimit (RLIMIT_NOFILE, &rlim) == 0
    100 	  && rlim.rlim_cur != (rlim_t) RLIM_INFINITY)
    101 	max = rlim.rlim_cur / 8;
    102       else
    103 #endif
    104 #ifdef _SC_OPEN_MAX
    105 	max = sysconf (_SC_OPEN_MAX) / 8;
    106 #else
    107         max = 10;
    108 #endif
    109 #endif /* not 32-bit Solaris */
    110 
    111       max_open_files = max < 10 ? 10 : max;
    112     }
    113 
    114   return max_open_files;
    115 }
    116 
    117 /* The number of BFD files we have open.  */
    118 
    119 static int open_files;
    120 
    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.  */
    124 
    125 static bfd *bfd_last_cache = NULL;
    126 
    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 {
    191   register bfd *to_kill;
    192 
    193   if (bfd_last_cache == NULL)
    194     to_kill = NULL;
    195   else
    196     {
    197       for (to_kill = bfd_last_cache->lru_prev;
    198 	   ! to_kill->cacheable;
    199 	   to_kill = to_kill->lru_prev)
    200 	{
    201 	  if (to_kill == bfd_last_cache)
    202 	    {
    203 	      to_kill = NULL;
    204 	      break;
    205 	    }
    206 	}
    207     }
    208 
    209   if (to_kill == NULL)
    210     {
    211       /* There are no open cacheable BFD's.  */
    212       return TRUE;
    213     }
    214 
    215   to_kill->where = real_ftell ((FILE *) to_kill->iostream);
    216 
    217   return bfd_cache_delete (to_kill);
    218 }
    219 
    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.  */
    224 
    225 #define bfd_cache_lookup(x, flag) \
    226   ((x) == bfd_last_cache			\
    227    ? (FILE *) (bfd_last_cache->iostream)	\
    228    : bfd_cache_lookup_worker (x, flag))
    229 
    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
    233    <<bfd_cache_max_open>> files open, it tries to close one first, to
    234    avoid running out of file descriptors.  It will return NULL
    235    if it is unable to (re)open the @var{abfd}.  */
    236 
    237 static FILE *
    238 bfd_cache_lookup_worker (bfd *abfd, enum cache_flag flag)
    239 {
    240   bfd *orig_bfd = abfd;
    241   if ((abfd->flags & BFD_IN_MEMORY) != 0)
    242     abort ();
    243 
    244   while (abfd->my_archive != NULL
    245 	 && !bfd_is_thin_archive (abfd->my_archive))
    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 
    259   if (flag & CACHE_NO_OPEN)
    260     return NULL;
    261 
    262   if (bfd_open_file (abfd) == NULL)
    263     ;
    264   else if (!(flag & CACHE_NO_SEEK)
    265 	   && real_fseek ((FILE *) abfd->iostream, abfd->where, SEEK_SET) != 0
    266 	   && !(flag & CACHE_NO_SEEK_ERROR))
    267     bfd_set_error (bfd_error_system_call);
    268   else
    269     return (FILE *) abfd->iostream;
    270 
    271   (*_bfd_error_handler) (_("reopening %B: %s\n"),
    272 			 orig_bfd, bfd_errmsg (bfd_get_error ()));
    273   return NULL;
    274 }
    275 
    276 static file_ptr
    277 cache_btell (struct bfd *abfd)
    278 {
    279   FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
    280   if (f == NULL)
    281     return abfd->where;
    282   return real_ftell (f);
    283 }
    284 
    285 static int
    286 cache_bseek (struct bfd *abfd, file_ptr offset, int whence)
    287 {
    288   FILE *f = bfd_cache_lookup (abfd, whence != SEEK_CUR ? CACHE_NO_SEEK : CACHE_NORMAL);
    289   if (f == NULL)
    290     return -1;
    291   return real_fseek (f, offset, whence);
    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
    302 cache_bread_1 (struct bfd *abfd, void *buf, file_ptr nbytes)
    303 {
    304   FILE *f;
    305   file_ptr nread;
    306   /* FIXME - this looks like an optimization, but it's really to cover
    307      up for a feature of some OSs (not solaris - sigh) that
    308      ld/pe-dll.c takes advantage of (apparently) when it creates BFDs
    309      internally and tries to link against them.  BFD seems to be smart
    310      enough to realize there are no symbol records in the "file" that
    311      doesn't exist but attempts to read them anyway.  On Solaris,
    312      attempting to read zero bytes from a NULL file results in a core
    313      dump, but on other platforms it just returns zero bytes read.
    314      This makes it to something reasonable. - DJ */
    315   if (nbytes == 0)
    316     return 0;
    317 
    318   f = bfd_cache_lookup (abfd, CACHE_NORMAL);
    319   if (f == NULL)
    320     return 0;
    321 
    322 #if defined (__VAX) && defined (VMS)
    323   /* Apparently fread on Vax VMS does not keep the record length
    324      information.  */
    325   nread = read (fileno (f), buf, nbytes);
    326   /* Set bfd_error if we did not read as much data as we expected.  If
    327      the read failed due to an error set the bfd_error_system_call,
    328      else set bfd_error_file_truncated.  */
    329   if (nread == (file_ptr)-1)
    330     {
    331       bfd_set_error (bfd_error_system_call);
    332       return nread;
    333     }
    334 #else
    335   nread = fread (buf, 1, nbytes, f);
    336   /* Set bfd_error if we did not read as much data as we expected.  If
    337      the read failed due to an error set the bfd_error_system_call,
    338      else set bfd_error_file_truncated.  */
    339   if (nread < nbytes && ferror (f))
    340     {
    341       bfd_set_error (bfd_error_system_call);
    342       return nread;
    343     }
    344 #endif
    345   if (nread < nbytes)
    346     /* This may or may not be an error, but in case the calling code
    347        bails out because of it, set the right error code.  */
    348     bfd_set_error (bfd_error_file_truncated);
    349   return nread;
    350 }
    351 
    352 static file_ptr
    353 cache_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
    354 {
    355   file_ptr nread = 0;
    356 
    357   /* Some filesystems are unable to handle reads that are too large
    358      (for instance, NetApp shares with oplocks turned off).  To avoid
    359      hitting this limitation, we read the buffer in chunks of 8MB max.  */
    360   while (nread < nbytes)
    361     {
    362       const file_ptr max_chunk_size = 0x800000;
    363       file_ptr chunk_size = nbytes - nread;
    364       file_ptr chunk_nread;
    365 
    366       if (chunk_size > max_chunk_size)
    367         chunk_size = max_chunk_size;
    368 
    369       chunk_nread = cache_bread_1 (abfd, (char *) buf + nread, chunk_size);
    370 
    371       /* Update the nread count.
    372 
    373          We just have to be careful of the case when cache_bread_1 returns
    374          a negative count:  If this is our first read, then set nread to
    375          that negative count in order to return that negative value to the
    376          caller.  Otherwise, don't add it to our total count, or we would
    377          end up returning a smaller number of bytes read than we actually
    378          did.  */
    379       if (nread == 0 || chunk_nread > 0)
    380         nread += chunk_nread;
    381 
    382       if (chunk_nread < chunk_size)
    383         break;
    384     }
    385 
    386   return nread;
    387 }
    388 
    389 static file_ptr
    390 cache_bwrite (struct bfd *abfd, const void *where, file_ptr nbytes)
    391 {
    392   file_ptr nwrite;
    393   FILE *f = bfd_cache_lookup (abfd, CACHE_NORMAL);
    394 
    395   if (f == NULL)
    396     return 0;
    397   nwrite = fwrite (where, 1, nbytes, f);
    398   if (nwrite < nbytes && ferror (f))
    399     {
    400       bfd_set_error (bfd_error_system_call);
    401       return -1;
    402     }
    403   return nwrite;
    404 }
    405 
    406 static int
    407 cache_bclose (struct bfd *abfd)
    408 {
    409   return bfd_cache_close (abfd) - 1;
    410 }
    411 
    412 static int
    413 cache_bflush (struct bfd *abfd)
    414 {
    415   int sts;
    416   FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
    417 
    418   if (f == NULL)
    419     return 0;
    420   sts = fflush (f);
    421   if (sts < 0)
    422     bfd_set_error (bfd_error_system_call);
    423   return sts;
    424 }
    425 
    426 static int
    427 cache_bstat (struct bfd *abfd, struct stat *sb)
    428 {
    429   int sts;
    430   FILE *f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR);
    431 
    432   if (f == NULL)
    433     return -1;
    434   sts = fstat (fileno (f), sb);
    435   if (sts < 0)
    436     bfd_set_error (bfd_error_system_call);
    437   return sts;
    438 }
    439 
    440 static void *
    441 cache_bmmap (struct bfd *abfd ATTRIBUTE_UNUSED,
    442 	     void *addr ATTRIBUTE_UNUSED,
    443 	     bfd_size_type len ATTRIBUTE_UNUSED,
    444 	     int prot ATTRIBUTE_UNUSED,
    445 	     int flags ATTRIBUTE_UNUSED,
    446 	     file_ptr offset ATTRIBUTE_UNUSED,
    447              void **map_addr ATTRIBUTE_UNUSED,
    448              bfd_size_type *map_len ATTRIBUTE_UNUSED)
    449 {
    450   void *ret = (void *) -1;
    451 
    452   if ((abfd->flags & BFD_IN_MEMORY) != 0)
    453     abort ();
    454 #ifdef HAVE_MMAP
    455   else
    456     {
    457       static uintptr_t pagesize_m1;
    458       FILE *f;
    459       file_ptr pg_offset;
    460       bfd_size_type pg_len;
    461 
    462       f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR);
    463       if (f == NULL)
    464 	return ret;
    465 
    466       if (pagesize_m1 == 0)
    467         pagesize_m1 = getpagesize () - 1;
    468 
    469       /* Handle archive members.  */
    470       if (abfd->my_archive != NULL
    471 	  && !bfd_is_thin_archive (abfd->my_archive))
    472         offset += abfd->origin;
    473 
    474       /* Align.  */
    475       pg_offset = offset & ~pagesize_m1;
    476       pg_len = (len + (offset - pg_offset) + pagesize_m1) & ~pagesize_m1;
    477 
    478       ret = mmap (addr, pg_len, prot, flags, fileno (f), pg_offset);
    479       if (ret == (void *) -1)
    480 	bfd_set_error (bfd_error_system_call);
    481       else
    482         {
    483           *map_addr = ret;
    484           *map_len = pg_len;
    485           ret = (char *) ret + (offset & pagesize_m1);
    486         }
    487     }
    488 #endif
    489 
    490   return ret;
    491 }
    492 
    493 static const struct bfd_iovec cache_iovec =
    494 {
    495   &cache_bread, &cache_bwrite, &cache_btell, &cache_bseek,
    496   &cache_bclose, &cache_bflush, &cache_bstat, &cache_bmmap
    497 };
    498 
    499 /*
    500 INTERNAL_FUNCTION
    501 	bfd_cache_init
    502 
    503 SYNOPSIS
    504 	bfd_boolean bfd_cache_init (bfd *abfd);
    505 
    506 DESCRIPTION
    507 	Add a newly opened BFD to the cache.
    508 */
    509 
    510 bfd_boolean
    511 bfd_cache_init (bfd *abfd)
    512 {
    513   BFD_ASSERT (abfd->iostream != NULL);
    514   if (open_files >= bfd_cache_max_open ())
    515     {
    516       if (! close_one ())
    517 	return FALSE;
    518     }
    519   abfd->iovec = &cache_iovec;
    520   insert (abfd);
    521   ++open_files;
    522   return TRUE;
    523 }
    524 
    525 /*
    526 INTERNAL_FUNCTION
    527 	bfd_cache_close
    528 
    529 SYNOPSIS
    530 	bfd_boolean bfd_cache_close (bfd *abfd);
    531 
    532 DESCRIPTION
    533 	Remove the BFD @var{abfd} from the cache. If the attached file is open,
    534 	then close it too.
    535 
    536 RETURNS
    537 	<<FALSE>> is returned if closing the file fails, <<TRUE>> is
    538 	returned if all is well.
    539 */
    540 
    541 bfd_boolean
    542 bfd_cache_close (bfd *abfd)
    543 {
    544   if (abfd->iovec != &cache_iovec)
    545     return TRUE;
    546 
    547   if (abfd->iostream == NULL)
    548     /* Previously closed.  */
    549     return TRUE;
    550 
    551   return bfd_cache_delete (abfd);
    552 }
    553 
    554 /*
    555 FUNCTION
    556 	bfd_cache_close_all
    557 
    558 SYNOPSIS
    559 	bfd_boolean bfd_cache_close_all (void);
    560 
    561 DESCRIPTION
    562 	Remove all BFDs from the cache. If the attached file is open,
    563 	then close it too.
    564 
    565 RETURNS
    566 	<<FALSE>> is returned if closing one of the file fails, <<TRUE>> is
    567 	returned if all is well.
    568 */
    569 
    570 bfd_boolean
    571 bfd_cache_close_all (void)
    572 {
    573   bfd_boolean ret = TRUE;
    574 
    575   while (bfd_last_cache != NULL)
    576     ret &= bfd_cache_close (bfd_last_cache);
    577 
    578   return ret;
    579 }
    580 
    581 /*
    582 INTERNAL_FUNCTION
    583 	bfd_open_file
    584 
    585 SYNOPSIS
    586 	FILE* bfd_open_file (bfd *abfd);
    587 
    588 DESCRIPTION
    589 	Call the OS to open a file for @var{abfd}.  Return the <<FILE *>>
    590 	(possibly <<NULL>>) that results from this operation.  Set up the
    591 	BFD so that future accesses know the file is open. If the <<FILE *>>
    592 	returned is <<NULL>>, then it won't have been put in the
    593 	cache, so it won't have to be removed from it.
    594 */
    595 
    596 FILE *
    597 bfd_open_file (bfd *abfd)
    598 {
    599   abfd->cacheable = TRUE;	/* Allow it to be closed later.  */
    600 
    601   if (open_files >= bfd_cache_max_open ())
    602     {
    603       if (! close_one ())
    604 	return NULL;
    605     }
    606 
    607   switch (abfd->direction)
    608     {
    609     case read_direction:
    610     case no_direction:
    611       abfd->iostream = real_fopen (abfd->filename, FOPEN_RB);
    612       break;
    613     case both_direction:
    614     case write_direction:
    615       if (abfd->opened_once)
    616 	{
    617 	  abfd->iostream = real_fopen (abfd->filename, FOPEN_RUB);
    618 	  if (abfd->iostream == NULL)
    619 	    abfd->iostream = real_fopen (abfd->filename, FOPEN_WUB);
    620 	}
    621       else
    622 	{
    623 	  /* Create the file.
    624 
    625 	     Some operating systems won't let us overwrite a running
    626 	     binary.  For them, we want to unlink the file first.
    627 
    628 	     However, gcc 2.95 will create temporary files using
    629 	     O_EXCL and tight permissions to prevent other users from
    630 	     substituting other .o files during the compilation.  gcc
    631 	     will then tell the assembler to use the newly created
    632 	     file as an output file.  If we unlink the file here, we
    633 	     open a brief window when another user could still
    634 	     substitute a file.
    635 
    636 	     So we unlink the output file if and only if it has
    637 	     non-zero size.  */
    638 #ifndef __MSDOS__
    639 	  /* Don't do this for MSDOS: it doesn't care about overwriting
    640 	     a running binary, but if this file is already open by
    641 	     another BFD, we will be in deep trouble if we delete an
    642 	     open file.  In fact, objdump does just that if invoked with
    643 	     the --info option.  */
    644 	  struct stat s;
    645 
    646 	  if (stat (abfd->filename, &s) == 0 && s.st_size != 0)
    647 	    unlink_if_ordinary (abfd->filename);
    648 #endif
    649 	  abfd->iostream = real_fopen (abfd->filename, FOPEN_WUB);
    650 	  abfd->opened_once = TRUE;
    651 	}
    652       break;
    653     }
    654 
    655   if (abfd->iostream == NULL)
    656     bfd_set_error (bfd_error_system_call);
    657   else
    658     {
    659       if (! bfd_cache_init (abfd))
    660 	return NULL;
    661     }
    662 
    663   return (FILE *) abfd->iostream;
    664 }
    665