Back to index

cell-binutils  2.17cvs20070401
cache.c
Go to the documentation of this file.
00001 /* BFD library -- caching of file descriptors.
00002 
00003    Copyright 1990, 1991, 1992, 1993, 1994, 1996, 2000, 2001, 2002,
00004    2003, 2004, 2005 Free Software Foundation, Inc.
00005 
00006    Hacked by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
00007 
00008 This file is part of BFD, the Binary File Descriptor library.
00009 
00010 This program is free software; you can redistribute it and/or modify
00011 it under the terms of the GNU General Public License as published by
00012 the Free Software Foundation; either version 2 of the License, or
00013 (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00023 
00024 /*
00025 SECTION
00026        File caching
00027 
00028        The file caching mechanism is embedded within BFD and allows
00029        the application to open as many BFDs as it wants without
00030        regard to the underlying operating system's file descriptor
00031        limit (often as low as 20 open files).  The module in
00032        <<cache.c>> maintains a least recently used list of
00033        <<BFD_CACHE_MAX_OPEN>> files, and exports the name
00034        <<bfd_cache_lookup>>, which runs around and makes sure that
00035        the required BFD is open. If not, then it chooses a file to
00036        close, closes it and opens the one wanted, returning its file
00037        handle.
00038 
00039 SUBSECTION
00040        Caching functions
00041 */
00042 
00043 #include "bfd.h"
00044 #include "sysdep.h"
00045 #include "libbfd.h"
00046 #include "libiberty.h"
00047 
00048 /* In some cases we can optimize cache operation when reopening files.
00049    For instance, a flush is entirely unnecessary if the file is already
00050    closed, so a flush would use CACHE_NO_OPEN.  Similarly, a seek using
00051    SEEK_SET or SEEK_END need not first seek to the current position.
00052    For stat we ignore seek errors, just in case the file has changed
00053    while we weren't looking.  If it has, then it's possible that the
00054    file is shorter and we don't want a seek error to prevent us doing
00055    the stat.  */
00056 enum cache_flag {
00057   CACHE_NORMAL = 0,
00058   CACHE_NO_OPEN = 1,
00059   CACHE_NO_SEEK = 2,
00060   CACHE_NO_SEEK_ERROR = 4
00061 };
00062 
00063 /* The maximum number of files which the cache will keep open at
00064    one time.  */
00065 
00066 #define BFD_CACHE_MAX_OPEN 10
00067 
00068 /* The number of BFD files we have open.  */
00069 
00070 static int open_files;
00071 
00072 /* Zero, or a pointer to the topmost BFD on the chain.  This is
00073    used by the <<bfd_cache_lookup>> macro in @file{libbfd.h} to
00074    determine when it can avoid a function call.  */
00075 
00076 static bfd *bfd_last_cache = NULL;
00077 
00078 /* Insert a BFD into the cache.  */
00079 
00080 static void
00081 insert (bfd *abfd)
00082 {
00083   if (bfd_last_cache == NULL)
00084     {
00085       abfd->lru_next = abfd;
00086       abfd->lru_prev = abfd;
00087     }
00088   else
00089     {
00090       abfd->lru_next = bfd_last_cache;
00091       abfd->lru_prev = bfd_last_cache->lru_prev;
00092       abfd->lru_prev->lru_next = abfd;
00093       abfd->lru_next->lru_prev = abfd;
00094     }
00095   bfd_last_cache = abfd;
00096 }
00097 
00098 /* Remove a BFD from the cache.  */
00099 
00100 static void
00101 snip (bfd *abfd)
00102 {
00103   abfd->lru_prev->lru_next = abfd->lru_next;
00104   abfd->lru_next->lru_prev = abfd->lru_prev;
00105   if (abfd == bfd_last_cache)
00106     {
00107       bfd_last_cache = abfd->lru_next;
00108       if (abfd == bfd_last_cache)
00109        bfd_last_cache = NULL;
00110     }
00111 }
00112 
00113 /* Close a BFD and remove it from the cache.  */
00114 
00115 static bfd_boolean
00116 bfd_cache_delete (bfd *abfd)
00117 {
00118   bfd_boolean ret;
00119 
00120   if (fclose ((FILE *) abfd->iostream) == 0)
00121     ret = TRUE;
00122   else
00123     {
00124       ret = FALSE;
00125       bfd_set_error (bfd_error_system_call);
00126     }
00127 
00128   snip (abfd);
00129 
00130   abfd->iostream = NULL;
00131   --open_files;
00132 
00133   return ret;
00134 }
00135 
00136 /* We need to open a new file, and the cache is full.  Find the least
00137    recently used cacheable BFD and close it.  */
00138 
00139 static bfd_boolean
00140 close_one (void)
00141 {
00142   register bfd *kill;
00143 
00144   if (bfd_last_cache == NULL)
00145     kill = NULL;
00146   else
00147     {
00148       for (kill = bfd_last_cache->lru_prev;
00149           ! kill->cacheable;
00150           kill = kill->lru_prev)
00151        {
00152          if (kill == bfd_last_cache)
00153            {
00154              kill = NULL;
00155              break;
00156            }
00157        }
00158     }
00159 
00160   if (kill == NULL)
00161     {
00162       /* There are no open cacheable BFD's.  */
00163       return TRUE;
00164     }
00165 
00166   kill->where = real_ftell ((FILE *) kill->iostream);
00167 
00168   /* Save the file st_mtime.  This is a hack so that gdb can detect when
00169      an executable has been deleted and recreated.  The only thing that
00170      makes this reasonable is that st_mtime doesn't change when a file
00171      is unlinked, so saving st_mtime makes BFD's file cache operation
00172      a little more transparent for this particular usage pattern.  If we
00173      hadn't closed the file then we would not have lost the original
00174      contents, st_mtime etc.  Of course, if something is writing to an
00175      existing file, then this is the wrong thing to do.
00176      FIXME: gdb should save these times itself on first opening a file,
00177      and this hack be removed.  */
00178   if (kill->direction == no_direction || kill->direction == read_direction)
00179     {
00180       bfd_get_mtime (kill);
00181       kill->mtime_set = TRUE;
00182     }
00183 
00184   return bfd_cache_delete (kill);
00185 }
00186 
00187 /* Check to see if the required BFD is the same as the last one
00188    looked up. If so, then it can use the stream in the BFD with
00189    impunity, since it can't have changed since the last lookup;
00190    otherwise, it has to perform the complicated lookup function.  */
00191 
00192 #define bfd_cache_lookup(x, flag) \
00193   ((x) == bfd_last_cache                  \
00194    ? (FILE *) (bfd_last_cache->iostream)  \
00195    : bfd_cache_lookup_worker (x, flag))
00196 
00197 /* Called when the macro <<bfd_cache_lookup>> fails to find a
00198    quick answer.  Find a file descriptor for @var{abfd}.  If
00199    necessary, it open it.  If there are already more than
00200    <<BFD_CACHE_MAX_OPEN>> files open, it tries to close one first, to
00201    avoid running out of file descriptors.  It will return NULL
00202    if it is unable to (re)open the @var{abfd}.  */
00203 
00204 static FILE *
00205 bfd_cache_lookup_worker (bfd *abfd, enum cache_flag flag)
00206 {
00207   bfd *orig_bfd = abfd;
00208   if ((abfd->flags & BFD_IN_MEMORY) != 0)
00209     abort ();
00210 
00211   if (abfd->my_archive)
00212     abfd = abfd->my_archive;
00213 
00214   if (abfd->iostream != NULL)
00215     {
00216       /* Move the file to the start of the cache.  */
00217       if (abfd != bfd_last_cache)
00218        {
00219          snip (abfd);
00220          insert (abfd);
00221        }
00222       return (FILE *) abfd->iostream;
00223     }
00224 
00225   if (flag & CACHE_NO_OPEN)
00226     return NULL;
00227 
00228   if (bfd_open_file (abfd) == NULL)
00229     ;
00230   else if (!(flag & CACHE_NO_SEEK)
00231           && real_fseek ((FILE *) abfd->iostream, abfd->where, SEEK_SET) != 0
00232           && !(flag & CACHE_NO_SEEK_ERROR))
00233     bfd_set_error (bfd_error_system_call);
00234   else
00235     return (FILE *) abfd->iostream;
00236 
00237   (*_bfd_error_handler) (_("reopening %B: %s\n"),
00238                       orig_bfd, bfd_errmsg (bfd_get_error ()));
00239   return NULL;
00240 }
00241 
00242 static file_ptr
00243 cache_btell (struct bfd *abfd)
00244 {
00245   FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
00246   if (f == NULL)
00247     return abfd->where;
00248   return real_ftell (f);
00249 }
00250 
00251 static int
00252 cache_bseek (struct bfd *abfd, file_ptr offset, int whence)
00253 {
00254   FILE *f = bfd_cache_lookup (abfd, whence != SEEK_CUR ? CACHE_NO_SEEK : 0);
00255   if (f == NULL)
00256     return -1;
00257   return real_fseek (f, offset, whence);
00258 }
00259 
00260 /* Note that archive entries don't have streams; they share their parent's.
00261    This allows someone to play with the iostream behind BFD's back.
00262 
00263    Also, note that the origin pointer points to the beginning of a file's
00264    contents (0 for non-archive elements).  For archive entries this is the
00265    first octet in the file, NOT the beginning of the archive header.  */
00266 
00267 static file_ptr
00268 cache_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
00269 {
00270   FILE *f;
00271   file_ptr nread;
00272   /* FIXME - this looks like an optimization, but it's really to cover
00273      up for a feature of some OSs (not solaris - sigh) that
00274      ld/pe-dll.c takes advantage of (apparently) when it creates BFDs
00275      internally and tries to link against them.  BFD seems to be smart
00276      enough to realize there are no symbol records in the "file" that
00277      doesn't exist but attempts to read them anyway.  On Solaris,
00278      attempting to read zero bytes from a NULL file results in a core
00279      dump, but on other platforms it just returns zero bytes read.
00280      This makes it to something reasonable. - DJ */
00281   if (nbytes == 0)
00282     return 0;
00283 
00284   f = bfd_cache_lookup (abfd, 0);
00285   if (f == NULL)
00286     return 0;
00287 
00288 #if defined (__VAX) && defined (VMS)
00289   /* Apparently fread on Vax VMS does not keep the record length
00290      information.  */
00291   nread = read (fileno (f), buf, nbytes);
00292   /* Set bfd_error if we did not read as much data as we expected.  If
00293      the read failed due to an error set the bfd_error_system_call,
00294      else set bfd_error_file_truncated.  */
00295   if (nread == (file_ptr)-1)
00296     {
00297       bfd_set_error (bfd_error_system_call);
00298       return -1;
00299     }
00300 #else
00301   nread = fread (buf, 1, nbytes, f);
00302   /* Set bfd_error if we did not read as much data as we expected.  If
00303      the read failed due to an error set the bfd_error_system_call,
00304      else set bfd_error_file_truncated.  */
00305   if (nread < nbytes && ferror (f))
00306     {
00307       bfd_set_error (bfd_error_system_call);
00308       return -1;
00309     }
00310 #endif
00311   return nread;
00312 }
00313 
00314 static file_ptr
00315 cache_bwrite (struct bfd *abfd, const void *where, file_ptr nbytes)
00316 {
00317   file_ptr nwrite;
00318   FILE *f = bfd_cache_lookup (abfd, 0);
00319   if (f == NULL)
00320     return 0;
00321   nwrite = fwrite (where, 1, nbytes, f);
00322   if (nwrite < nbytes && ferror (f))
00323     {
00324       bfd_set_error (bfd_error_system_call);
00325       return -1;
00326     }
00327   return nwrite;
00328 }
00329 
00330 static int
00331 cache_bclose (struct bfd *abfd)
00332 {
00333   return bfd_cache_close (abfd);
00334 }
00335 
00336 static int
00337 cache_bflush (struct bfd *abfd)
00338 {
00339   int sts;
00340   FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
00341   if (f == NULL)
00342     return 0;
00343   sts = fflush (f);
00344   if (sts < 0)
00345     bfd_set_error (bfd_error_system_call);
00346   return sts;
00347 }
00348 
00349 static int
00350 cache_bstat (struct bfd *abfd, struct stat *sb)
00351 {
00352   int sts;
00353   FILE *f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR);
00354   if (f == NULL)
00355     return -1;
00356   sts = fstat (fileno (f), sb);
00357   if (sts < 0)
00358     bfd_set_error (bfd_error_system_call);
00359   return sts;
00360 }
00361 
00362 static const struct bfd_iovec cache_iovec = {
00363   &cache_bread, &cache_bwrite, &cache_btell, &cache_bseek,
00364   &cache_bclose, &cache_bflush, &cache_bstat
00365 };
00366 
00367 /*
00368 INTERNAL_FUNCTION
00369        bfd_cache_init
00370 
00371 SYNOPSIS
00372        bfd_boolean bfd_cache_init (bfd *abfd);
00373 
00374 DESCRIPTION
00375        Add a newly opened BFD to the cache.
00376 */
00377 
00378 bfd_boolean
00379 bfd_cache_init (bfd *abfd)
00380 {
00381   BFD_ASSERT (abfd->iostream != NULL);
00382   if (open_files >= BFD_CACHE_MAX_OPEN)
00383     {
00384       if (! close_one ())
00385        return FALSE;
00386     }
00387   abfd->iovec = &cache_iovec;
00388   insert (abfd);
00389   ++open_files;
00390   return TRUE;
00391 }
00392 
00393 /*
00394 INTERNAL_FUNCTION
00395        bfd_cache_close
00396 
00397 SYNOPSIS
00398        bfd_boolean bfd_cache_close (bfd *abfd);
00399 
00400 DESCRIPTION
00401        Remove the BFD @var{abfd} from the cache. If the attached file is open,
00402        then close it too.
00403 
00404 RETURNS
00405        <<FALSE>> is returned if closing the file fails, <<TRUE>> is
00406        returned if all is well.
00407 */
00408 
00409 bfd_boolean
00410 bfd_cache_close (bfd *abfd)
00411 {
00412   if (abfd->iovec != &cache_iovec)
00413     return TRUE;
00414 
00415   if (abfd->iostream == NULL)
00416     /* Previously closed.  */
00417     return TRUE;
00418 
00419   return bfd_cache_delete (abfd);
00420 }
00421 
00422 /*
00423 FUNCTION
00424        bfd_cache_close_all
00425 
00426 SYNOPSIS
00427        bfd_boolean bfd_cache_close_all (void);
00428 
00429 DESCRIPTION
00430        Remove all BFDs from the cache. If the attached file is open,
00431        then close it too.
00432 
00433 RETURNS
00434        <<FALSE>> is returned if closing one of the file fails, <<TRUE>> is
00435        returned if all is well.
00436 */
00437 
00438 bfd_boolean
00439 bfd_cache_close_all ()
00440 {
00441   bfd_boolean ret = TRUE;
00442 
00443   while (bfd_last_cache != NULL)
00444     ret &= bfd_cache_close (bfd_last_cache);
00445 
00446   return ret;
00447 }
00448 
00449 /*
00450 INTERNAL_FUNCTION
00451        bfd_open_file
00452 
00453 SYNOPSIS
00454        FILE* bfd_open_file (bfd *abfd);
00455 
00456 DESCRIPTION
00457        Call the OS to open a file for @var{abfd}.  Return the <<FILE *>>
00458        (possibly <<NULL>>) that results from this operation.  Set up the
00459        BFD so that future accesses know the file is open. If the <<FILE *>>
00460        returned is <<NULL>>, then it won't have been put in the
00461        cache, so it won't have to be removed from it.
00462 */
00463 
00464 FILE *
00465 bfd_open_file (bfd *abfd)
00466 {
00467   abfd->cacheable = TRUE;   /* Allow it to be closed later.  */
00468 
00469   if (open_files >= BFD_CACHE_MAX_OPEN)
00470     {
00471       if (! close_one ())
00472        return NULL;
00473     }
00474 
00475   switch (abfd->direction)
00476     {
00477     case read_direction:
00478     case no_direction:
00479       abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_RB);
00480       break;
00481     case both_direction:
00482     case write_direction:
00483       if (abfd->opened_once)
00484        {
00485          abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_RUB);
00486          if (abfd->iostream == NULL)
00487            abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_WUB);
00488        }
00489       else
00490        {
00491          /* Create the file.
00492 
00493             Some operating systems won't let us overwrite a running
00494             binary.  For them, we want to unlink the file first.
00495 
00496             However, gcc 2.95 will create temporary files using
00497             O_EXCL and tight permissions to prevent other users from
00498             substituting other .o files during the compilation.  gcc
00499             will then tell the assembler to use the newly created
00500             file as an output file.  If we unlink the file here, we
00501             open a brief window when another user could still
00502             substitute a file.
00503 
00504             So we unlink the output file if and only if it has
00505             non-zero size.  */
00506 #ifndef __MSDOS__
00507          /* Don't do this for MSDOS: it doesn't care about overwriting
00508             a running binary, but if this file is already open by
00509             another BFD, we will be in deep trouble if we delete an
00510             open file.  In fact, objdump does just that if invoked with
00511             the --info option.  */
00512          struct stat s;
00513 
00514          if (stat (abfd->filename, &s) == 0 && s.st_size != 0)
00515            unlink_if_ordinary (abfd->filename);
00516 #endif
00517          abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_WUB);
00518          abfd->opened_once = TRUE;
00519        }
00520       break;
00521     }
00522 
00523   if (abfd->iostream == NULL)
00524     bfd_set_error (bfd_error_system_call);
00525   else
00526     {
00527       if (! bfd_cache_init (abfd))
00528        return NULL;
00529     }
00530 
00531   return (FILE *) abfd->iostream;
00532 }