/* BFD library -- caching of file descriptors.
|
/* BFD library -- caching of file descriptors.
|
|
|
Copyright 1990, 1991, 1992, 1993, 1994, 1996, 2000, 2001, 2002,
|
Copyright 1990, 1991, 1992, 1993, 1994, 1996, 2000, 2001, 2002,
|
2003, 2004, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
|
2003, 2004, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
|
|
|
Hacked by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
|
Hacked by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
|
|
|
This file is part of BFD, the Binary File Descriptor library.
|
This file is part of BFD, the Binary File Descriptor library.
|
|
|
This program is free software; you can redistribute it and/or modify
|
This program is free software; you can redistribute it and/or modify
|
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
the Free Software Foundation; either version 3 of the License, or
|
the Free Software Foundation; either version 3 of the License, or
|
(at your option) any later version.
|
(at your option) any later version.
|
|
|
This program is distributed in the hope that it will be useful,
|
This program is distributed in the hope that it will be useful,
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
GNU General Public License for more details.
|
GNU General Public License for more details.
|
|
|
You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
along with this program; if not, write to the Free Software
|
along with this program; if not, write to the Free Software
|
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
|
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
|
MA 02110-1301, USA. */
|
MA 02110-1301, USA. */
|
|
|
/*
|
/*
|
SECTION
|
SECTION
|
File caching
|
File caching
|
|
|
The file caching mechanism is embedded within BFD and allows
|
The file caching mechanism is embedded within BFD and allows
|
the application to open as many BFDs as it wants without
|
the application to open as many BFDs as it wants without
|
regard to the underlying operating system's file descriptor
|
regard to the underlying operating system's file descriptor
|
limit (often as low as 20 open files). The module in
|
limit (often as low as 20 open files). The module in
|
<<cache.c>> maintains a least recently used list of
|
<<cache.c>> maintains a least recently used list of
|
<<BFD_CACHE_MAX_OPEN>> files, and exports the name
|
<<BFD_CACHE_MAX_OPEN>> files, and exports the name
|
<<bfd_cache_lookup>>, which runs around and makes sure that
|
<<bfd_cache_lookup>>, which runs around and makes sure that
|
the required BFD is open. If not, then it chooses a file to
|
the required BFD is open. If not, then it chooses a file to
|
close, closes it and opens the one wanted, returning its file
|
close, closes it and opens the one wanted, returning its file
|
handle.
|
handle.
|
|
|
SUBSECTION
|
SUBSECTION
|
Caching functions
|
Caching functions
|
*/
|
*/
|
|
|
#include "sysdep.h"
|
#include "sysdep.h"
|
#include "bfd.h"
|
#include "bfd.h"
|
#include "libbfd.h"
|
#include "libbfd.h"
|
#include "libiberty.h"
|
#include "libiberty.h"
|
|
|
#ifdef HAVE_MMAP
|
#ifdef HAVE_MMAP
|
#include <sys/mman.h>
|
#include <sys/mman.h>
|
#endif
|
#endif
|
|
|
/* In some cases we can optimize cache operation when reopening files.
|
/* In some cases we can optimize cache operation when reopening files.
|
For instance, a flush is entirely unnecessary if the file is already
|
For instance, a flush is entirely unnecessary if the file is already
|
closed, so a flush would use CACHE_NO_OPEN. Similarly, a seek using
|
closed, so a flush would use CACHE_NO_OPEN. Similarly, a seek using
|
SEEK_SET or SEEK_END need not first seek to the current position.
|
SEEK_SET or SEEK_END need not first seek to the current position.
|
For stat we ignore seek errors, just in case the file has changed
|
For stat we ignore seek errors, just in case the file has changed
|
while we weren't looking. If it has, then it's possible that the
|
while we weren't looking. If it has, then it's possible that the
|
file is shorter and we don't want a seek error to prevent us doing
|
file is shorter and we don't want a seek error to prevent us doing
|
the stat. */
|
the stat. */
|
enum cache_flag {
|
enum cache_flag {
|
CACHE_NORMAL = 0,
|
CACHE_NORMAL = 0,
|
CACHE_NO_OPEN = 1,
|
CACHE_NO_OPEN = 1,
|
CACHE_NO_SEEK = 2,
|
CACHE_NO_SEEK = 2,
|
CACHE_NO_SEEK_ERROR = 4
|
CACHE_NO_SEEK_ERROR = 4
|
};
|
};
|
|
|
/* The maximum number of files which the cache will keep open at
|
/* The maximum number of files which the cache will keep open at
|
one time. */
|
one time. */
|
|
|
#define BFD_CACHE_MAX_OPEN 10
|
#define BFD_CACHE_MAX_OPEN 10
|
|
|
/* The number of BFD files we have open. */
|
/* The number of BFD files we have open. */
|
|
|
static int open_files;
|
static int open_files;
|
|
|
/* Zero, or a pointer to the topmost BFD on the chain. This is
|
/* Zero, or a pointer to the topmost BFD on the chain. This is
|
used by the <<bfd_cache_lookup>> macro in @file{libbfd.h} to
|
used by the <<bfd_cache_lookup>> macro in @file{libbfd.h} to
|
determine when it can avoid a function call. */
|
determine when it can avoid a function call. */
|
|
|
static bfd *bfd_last_cache = NULL;
|
static bfd *bfd_last_cache = NULL;
|
|
|
/* Insert a BFD into the cache. */
|
/* Insert a BFD into the cache. */
|
|
|
static void
|
static void
|
insert (bfd *abfd)
|
insert (bfd *abfd)
|
{
|
{
|
if (bfd_last_cache == NULL)
|
if (bfd_last_cache == NULL)
|
{
|
{
|
abfd->lru_next = abfd;
|
abfd->lru_next = abfd;
|
abfd->lru_prev = abfd;
|
abfd->lru_prev = abfd;
|
}
|
}
|
else
|
else
|
{
|
{
|
abfd->lru_next = bfd_last_cache;
|
abfd->lru_next = bfd_last_cache;
|
abfd->lru_prev = bfd_last_cache->lru_prev;
|
abfd->lru_prev = bfd_last_cache->lru_prev;
|
abfd->lru_prev->lru_next = abfd;
|
abfd->lru_prev->lru_next = abfd;
|
abfd->lru_next->lru_prev = abfd;
|
abfd->lru_next->lru_prev = abfd;
|
}
|
}
|
bfd_last_cache = abfd;
|
bfd_last_cache = abfd;
|
}
|
}
|
|
|
/* Remove a BFD from the cache. */
|
/* Remove a BFD from the cache. */
|
|
|
static void
|
static void
|
snip (bfd *abfd)
|
snip (bfd *abfd)
|
{
|
{
|
abfd->lru_prev->lru_next = abfd->lru_next;
|
abfd->lru_prev->lru_next = abfd->lru_next;
|
abfd->lru_next->lru_prev = abfd->lru_prev;
|
abfd->lru_next->lru_prev = abfd->lru_prev;
|
if (abfd == bfd_last_cache)
|
if (abfd == bfd_last_cache)
|
{
|
{
|
bfd_last_cache = abfd->lru_next;
|
bfd_last_cache = abfd->lru_next;
|
if (abfd == bfd_last_cache)
|
if (abfd == bfd_last_cache)
|
bfd_last_cache = NULL;
|
bfd_last_cache = NULL;
|
}
|
}
|
}
|
}
|
|
|
/* Close a BFD and remove it from the cache. */
|
/* Close a BFD and remove it from the cache. */
|
|
|
static bfd_boolean
|
static bfd_boolean
|
bfd_cache_delete (bfd *abfd)
|
bfd_cache_delete (bfd *abfd)
|
{
|
{
|
bfd_boolean ret;
|
bfd_boolean ret;
|
|
|
if (fclose ((FILE *) abfd->iostream) == 0)
|
if (fclose ((FILE *) abfd->iostream) == 0)
|
ret = TRUE;
|
ret = TRUE;
|
else
|
else
|
{
|
{
|
ret = FALSE;
|
ret = FALSE;
|
bfd_set_error (bfd_error_system_call);
|
bfd_set_error (bfd_error_system_call);
|
}
|
}
|
|
|
snip (abfd);
|
snip (abfd);
|
|
|
abfd->iostream = NULL;
|
abfd->iostream = NULL;
|
--open_files;
|
--open_files;
|
|
|
return ret;
|
return ret;
|
}
|
}
|
|
|
/* We need to open a new file, and the cache is full. Find the least
|
/* We need to open a new file, and the cache is full. Find the least
|
recently used cacheable BFD and close it. */
|
recently used cacheable BFD and close it. */
|
|
|
static bfd_boolean
|
static bfd_boolean
|
close_one (void)
|
close_one (void)
|
{
|
{
|
register bfd *kill;
|
register bfd *kill;
|
|
|
if (bfd_last_cache == NULL)
|
if (bfd_last_cache == NULL)
|
kill = NULL;
|
kill = NULL;
|
else
|
else
|
{
|
{
|
for (kill = bfd_last_cache->lru_prev;
|
for (kill = bfd_last_cache->lru_prev;
|
! kill->cacheable;
|
! kill->cacheable;
|
kill = kill->lru_prev)
|
kill = kill->lru_prev)
|
{
|
{
|
if (kill == bfd_last_cache)
|
if (kill == bfd_last_cache)
|
{
|
{
|
kill = NULL;
|
kill = NULL;
|
break;
|
break;
|
}
|
}
|
}
|
}
|
}
|
}
|
|
|
if (kill == NULL)
|
if (kill == NULL)
|
{
|
{
|
/* There are no open cacheable BFD's. */
|
/* There are no open cacheable BFD's. */
|
return TRUE;
|
return TRUE;
|
}
|
}
|
|
|
kill->where = real_ftell ((FILE *) kill->iostream);
|
kill->where = real_ftell ((FILE *) kill->iostream);
|
|
|
return bfd_cache_delete (kill);
|
return bfd_cache_delete (kill);
|
}
|
}
|
|
|
/* Check to see if the required BFD is the same as the last one
|
/* Check to see if the required BFD is the same as the last one
|
looked up. If so, then it can use the stream in the BFD with
|
looked up. If so, then it can use the stream in the BFD with
|
impunity, since it can't have changed since the last lookup;
|
impunity, since it can't have changed since the last lookup;
|
otherwise, it has to perform the complicated lookup function. */
|
otherwise, it has to perform the complicated lookup function. */
|
|
|
#define bfd_cache_lookup(x, flag) \
|
#define bfd_cache_lookup(x, flag) \
|
((x) == bfd_last_cache \
|
((x) == bfd_last_cache \
|
? (FILE *) (bfd_last_cache->iostream) \
|
? (FILE *) (bfd_last_cache->iostream) \
|
: bfd_cache_lookup_worker (x, flag))
|
: bfd_cache_lookup_worker (x, flag))
|
|
|
/* Called when the macro <<bfd_cache_lookup>> fails to find a
|
/* Called when the macro <<bfd_cache_lookup>> fails to find a
|
quick answer. Find a file descriptor for @var{abfd}. If
|
quick answer. Find a file descriptor for @var{abfd}. If
|
necessary, it open it. If there are already more than
|
necessary, it open it. If there are already more than
|
<<BFD_CACHE_MAX_OPEN>> files open, it tries to close one first, to
|
<<BFD_CACHE_MAX_OPEN>> files open, it tries to close one first, to
|
avoid running out of file descriptors. It will return NULL
|
avoid running out of file descriptors. It will return NULL
|
if it is unable to (re)open the @var{abfd}. */
|
if it is unable to (re)open the @var{abfd}. */
|
|
|
static FILE *
|
static FILE *
|
bfd_cache_lookup_worker (bfd *abfd, enum cache_flag flag)
|
bfd_cache_lookup_worker (bfd *abfd, enum cache_flag flag)
|
{
|
{
|
bfd *orig_bfd = abfd;
|
bfd *orig_bfd = abfd;
|
if ((abfd->flags & BFD_IN_MEMORY) != 0)
|
if ((abfd->flags & BFD_IN_MEMORY) != 0)
|
abort ();
|
abort ();
|
|
|
if (abfd->my_archive)
|
if (abfd->my_archive)
|
abfd = abfd->my_archive;
|
abfd = abfd->my_archive;
|
|
|
if (abfd->iostream != NULL)
|
if (abfd->iostream != NULL)
|
{
|
{
|
/* Move the file to the start of the cache. */
|
/* Move the file to the start of the cache. */
|
if (abfd != bfd_last_cache)
|
if (abfd != bfd_last_cache)
|
{
|
{
|
snip (abfd);
|
snip (abfd);
|
insert (abfd);
|
insert (abfd);
|
}
|
}
|
return (FILE *) abfd->iostream;
|
return (FILE *) abfd->iostream;
|
}
|
}
|
|
|
if (flag & CACHE_NO_OPEN)
|
if (flag & CACHE_NO_OPEN)
|
return NULL;
|
return NULL;
|
|
|
if (bfd_open_file (abfd) == NULL)
|
if (bfd_open_file (abfd) == NULL)
|
;
|
;
|
else if (!(flag & CACHE_NO_SEEK)
|
else if (!(flag & CACHE_NO_SEEK)
|
&& real_fseek ((FILE *) abfd->iostream, abfd->where, SEEK_SET) != 0
|
&& real_fseek ((FILE *) abfd->iostream, abfd->where, SEEK_SET) != 0
|
&& !(flag & CACHE_NO_SEEK_ERROR))
|
&& !(flag & CACHE_NO_SEEK_ERROR))
|
bfd_set_error (bfd_error_system_call);
|
bfd_set_error (bfd_error_system_call);
|
else
|
else
|
return (FILE *) abfd->iostream;
|
return (FILE *) abfd->iostream;
|
|
|
(*_bfd_error_handler) (_("reopening %B: %s\n"),
|
(*_bfd_error_handler) (_("reopening %B: %s\n"),
|
orig_bfd, bfd_errmsg (bfd_get_error ()));
|
orig_bfd, bfd_errmsg (bfd_get_error ()));
|
return NULL;
|
return NULL;
|
}
|
}
|
|
|
static file_ptr
|
static file_ptr
|
cache_btell (struct bfd *abfd)
|
cache_btell (struct bfd *abfd)
|
{
|
{
|
FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
|
FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
|
if (f == NULL)
|
if (f == NULL)
|
return abfd->where;
|
return abfd->where;
|
return real_ftell (f);
|
return real_ftell (f);
|
}
|
}
|
|
|
static int
|
static int
|
cache_bseek (struct bfd *abfd, file_ptr offset, int whence)
|
cache_bseek (struct bfd *abfd, file_ptr offset, int whence)
|
{
|
{
|
FILE *f = bfd_cache_lookup (abfd, whence != SEEK_CUR ? CACHE_NO_SEEK : CACHE_NORMAL);
|
FILE *f = bfd_cache_lookup (abfd, whence != SEEK_CUR ? CACHE_NO_SEEK : CACHE_NORMAL);
|
if (f == NULL)
|
if (f == NULL)
|
return -1;
|
return -1;
|
return real_fseek (f, offset, whence);
|
return real_fseek (f, offset, whence);
|
}
|
}
|
|
|
/* Note that archive entries don't have streams; they share their parent's.
|
/* Note that archive entries don't have streams; they share their parent's.
|
This allows someone to play with the iostream behind BFD's back.
|
This allows someone to play with the iostream behind BFD's back.
|
|
|
Also, note that the origin pointer points to the beginning of a file's
|
Also, note that the origin pointer points to the beginning of a file's
|
contents (0 for non-archive elements). For archive entries this is the
|
contents (0 for non-archive elements). For archive entries this is the
|
first octet in the file, NOT the beginning of the archive header. */
|
first octet in the file, NOT the beginning of the archive header. */
|
|
|
static file_ptr
|
static file_ptr
|
cache_bread_1 (struct bfd *abfd, void *buf, file_ptr nbytes)
|
cache_bread_1 (struct bfd *abfd, void *buf, file_ptr nbytes)
|
{
|
{
|
FILE *f;
|
FILE *f;
|
file_ptr nread;
|
file_ptr nread;
|
/* FIXME - this looks like an optimization, but it's really to cover
|
/* FIXME - this looks like an optimization, but it's really to cover
|
up for a feature of some OSs (not solaris - sigh) that
|
up for a feature of some OSs (not solaris - sigh) that
|
ld/pe-dll.c takes advantage of (apparently) when it creates BFDs
|
ld/pe-dll.c takes advantage of (apparently) when it creates BFDs
|
internally and tries to link against them. BFD seems to be smart
|
internally and tries to link against them. BFD seems to be smart
|
enough to realize there are no symbol records in the "file" that
|
enough to realize there are no symbol records in the "file" that
|
doesn't exist but attempts to read them anyway. On Solaris,
|
doesn't exist but attempts to read them anyway. On Solaris,
|
attempting to read zero bytes from a NULL file results in a core
|
attempting to read zero bytes from a NULL file results in a core
|
dump, but on other platforms it just returns zero bytes read.
|
dump, but on other platforms it just returns zero bytes read.
|
This makes it to something reasonable. - DJ */
|
This makes it to something reasonable. - DJ */
|
if (nbytes == 0)
|
if (nbytes == 0)
|
return 0;
|
return 0;
|
|
|
f = bfd_cache_lookup (abfd, CACHE_NORMAL);
|
f = bfd_cache_lookup (abfd, CACHE_NORMAL);
|
if (f == NULL)
|
if (f == NULL)
|
return 0;
|
return 0;
|
|
|
#if defined (__VAX) && defined (VMS)
|
#if defined (__VAX) && defined (VMS)
|
/* Apparently fread on Vax VMS does not keep the record length
|
/* Apparently fread on Vax VMS does not keep the record length
|
information. */
|
information. */
|
nread = read (fileno (f), buf, nbytes);
|
nread = read (fileno (f), buf, nbytes);
|
/* Set bfd_error if we did not read as much data as we expected. If
|
/* Set bfd_error if we did not read as much data as we expected. If
|
the read failed due to an error set the bfd_error_system_call,
|
the read failed due to an error set the bfd_error_system_call,
|
else set bfd_error_file_truncated. */
|
else set bfd_error_file_truncated. */
|
if (nread == (file_ptr)-1)
|
if (nread == (file_ptr)-1)
|
{
|
{
|
bfd_set_error (bfd_error_system_call);
|
bfd_set_error (bfd_error_system_call);
|
return -1;
|
return -1;
|
}
|
}
|
#else
|
#else
|
nread = fread (buf, 1, nbytes, f);
|
nread = fread (buf, 1, nbytes, f);
|
/* Set bfd_error if we did not read as much data as we expected. If
|
/* Set bfd_error if we did not read as much data as we expected. If
|
the read failed due to an error set the bfd_error_system_call,
|
the read failed due to an error set the bfd_error_system_call,
|
else set bfd_error_file_truncated. */
|
else set bfd_error_file_truncated. */
|
if (nread < nbytes && ferror (f))
|
if (nread < nbytes && ferror (f))
|
{
|
{
|
bfd_set_error (bfd_error_system_call);
|
bfd_set_error (bfd_error_system_call);
|
return -1;
|
return -1;
|
}
|
}
|
#endif
|
#endif
|
if (nread < nbytes)
|
if (nread < nbytes)
|
/* This may or may not be an error, but in case the calling code
|
/* This may or may not be an error, but in case the calling code
|
bails out because of it, set the right error code. */
|
bails out because of it, set the right error code. */
|
bfd_set_error (bfd_error_file_truncated);
|
bfd_set_error (bfd_error_file_truncated);
|
return nread;
|
return nread;
|
}
|
}
|
|
|
static file_ptr
|
static file_ptr
|
cache_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
|
cache_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
|
{
|
{
|
file_ptr nread = 0;
|
file_ptr nread = 0;
|
|
|
/* Some filesystems are unable to handle reads that are too large
|
/* Some filesystems are unable to handle reads that are too large
|
(for instance, NetApp shares with oplocks turned off). To avoid
|
(for instance, NetApp shares with oplocks turned off). To avoid
|
hitting this limitation, we read the buffer in chunks of 8MB max. */
|
hitting this limitation, we read the buffer in chunks of 8MB max. */
|
while (nread < nbytes)
|
while (nread < nbytes)
|
{
|
{
|
const file_ptr max_chunk_size = 0x800000;
|
const file_ptr max_chunk_size = 0x800000;
|
file_ptr chunk_size = nbytes - nread;
|
file_ptr chunk_size = nbytes - nread;
|
file_ptr chunk_nread;
|
file_ptr chunk_nread;
|
|
|
if (chunk_size > max_chunk_size)
|
if (chunk_size > max_chunk_size)
|
chunk_size = max_chunk_size;
|
chunk_size = max_chunk_size;
|
|
|
chunk_nread = cache_bread_1 (abfd, (char *) buf + nread, chunk_size);
|
chunk_nread = cache_bread_1 (abfd, (char *) buf + nread, chunk_size);
|
|
|
/* Update the nread count.
|
/* Update the nread count.
|
|
|
We just have to be careful of the case when cache_bread_1 returns
|
We just have to be careful of the case when cache_bread_1 returns
|
a negative count: If this is our first read, then set nread to
|
a negative count: If this is our first read, then set nread to
|
that negative count in order to return that negative value to the
|
that negative count in order to return that negative value to the
|
caller. Otherwise, don't add it to our total count, or we would
|
caller. Otherwise, don't add it to our total count, or we would
|
end up returning a smaller number of bytes read than we actually
|
end up returning a smaller number of bytes read than we actually
|
did. */
|
did. */
|
if (nread == 0 || chunk_nread > 0)
|
if (nread == 0 || chunk_nread > 0)
|
nread += chunk_nread;
|
nread += chunk_nread;
|
|
|
if (chunk_nread < chunk_size)
|
if (chunk_nread < chunk_size)
|
break;
|
break;
|
}
|
}
|
|
|
return nread;
|
return nread;
|
}
|
}
|
|
|
static file_ptr
|
static file_ptr
|
cache_bwrite (struct bfd *abfd, const void *where, file_ptr nbytes)
|
cache_bwrite (struct bfd *abfd, const void *where, file_ptr nbytes)
|
{
|
{
|
file_ptr nwrite;
|
file_ptr nwrite;
|
FILE *f = bfd_cache_lookup (abfd, CACHE_NORMAL);
|
FILE *f = bfd_cache_lookup (abfd, CACHE_NORMAL);
|
|
|
if (f == NULL)
|
if (f == NULL)
|
return 0;
|
return 0;
|
nwrite = fwrite (where, 1, nbytes, f);
|
nwrite = fwrite (where, 1, nbytes, f);
|
if (nwrite < nbytes && ferror (f))
|
if (nwrite < nbytes && ferror (f))
|
{
|
{
|
bfd_set_error (bfd_error_system_call);
|
bfd_set_error (bfd_error_system_call);
|
return -1;
|
return -1;
|
}
|
}
|
return nwrite;
|
return nwrite;
|
}
|
}
|
|
|
static int
|
static int
|
cache_bclose (struct bfd *abfd)
|
cache_bclose (struct bfd *abfd)
|
{
|
{
|
return bfd_cache_close (abfd);
|
return bfd_cache_close (abfd);
|
}
|
}
|
|
|
static int
|
static int
|
cache_bflush (struct bfd *abfd)
|
cache_bflush (struct bfd *abfd)
|
{
|
{
|
int sts;
|
int sts;
|
FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
|
FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
|
|
|
if (f == NULL)
|
if (f == NULL)
|
return 0;
|
return 0;
|
sts = fflush (f);
|
sts = fflush (f);
|
if (sts < 0)
|
if (sts < 0)
|
bfd_set_error (bfd_error_system_call);
|
bfd_set_error (bfd_error_system_call);
|
return sts;
|
return sts;
|
}
|
}
|
|
|
static int
|
static int
|
cache_bstat (struct bfd *abfd, struct stat *sb)
|
cache_bstat (struct bfd *abfd, struct stat *sb)
|
{
|
{
|
int sts;
|
int sts;
|
FILE *f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR);
|
FILE *f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR);
|
|
|
if (f == NULL)
|
if (f == NULL)
|
return -1;
|
return -1;
|
sts = fstat (fileno (f), sb);
|
sts = fstat (fileno (f), sb);
|
if (sts < 0)
|
if (sts < 0)
|
bfd_set_error (bfd_error_system_call);
|
bfd_set_error (bfd_error_system_call);
|
return sts;
|
return sts;
|
}
|
}
|
|
|
static void *
|
static void *
|
cache_bmmap (struct bfd *abfd ATTRIBUTE_UNUSED,
|
cache_bmmap (struct bfd *abfd ATTRIBUTE_UNUSED,
|
void *addr ATTRIBUTE_UNUSED,
|
void *addr ATTRIBUTE_UNUSED,
|
bfd_size_type len ATTRIBUTE_UNUSED,
|
bfd_size_type len ATTRIBUTE_UNUSED,
|
int prot ATTRIBUTE_UNUSED,
|
int prot ATTRIBUTE_UNUSED,
|
int flags ATTRIBUTE_UNUSED,
|
int flags ATTRIBUTE_UNUSED,
|
file_ptr offset ATTRIBUTE_UNUSED)
|
file_ptr offset ATTRIBUTE_UNUSED)
|
{
|
{
|
void *ret = (void *) -1;
|
void *ret = (void *) -1;
|
|
|
if ((abfd->flags & BFD_IN_MEMORY) != 0)
|
if ((abfd->flags & BFD_IN_MEMORY) != 0)
|
abort ();
|
abort ();
|
#ifdef HAVE_MMAP
|
#ifdef HAVE_MMAP
|
else
|
else
|
{
|
{
|
FILE *f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR);
|
FILE *f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR);
|
if (f == NULL)
|
if (f == NULL)
|
return ret;
|
return ret;
|
|
|
ret = mmap (addr, len, prot, flags, fileno (f), offset);
|
ret = mmap (addr, len, prot, flags, fileno (f), offset);
|
if (ret == (void *) -1)
|
if (ret == (void *) -1)
|
bfd_set_error (bfd_error_system_call);
|
bfd_set_error (bfd_error_system_call);
|
}
|
}
|
#endif
|
#endif
|
|
|
return ret;
|
return ret;
|
}
|
}
|
|
|
static const struct bfd_iovec cache_iovec =
|
static const struct bfd_iovec cache_iovec =
|
{
|
{
|
&cache_bread, &cache_bwrite, &cache_btell, &cache_bseek,
|
&cache_bread, &cache_bwrite, &cache_btell, &cache_bseek,
|
&cache_bclose, &cache_bflush, &cache_bstat, &cache_bmmap
|
&cache_bclose, &cache_bflush, &cache_bstat, &cache_bmmap
|
};
|
};
|
|
|
/*
|
/*
|
INTERNAL_FUNCTION
|
INTERNAL_FUNCTION
|
bfd_cache_init
|
bfd_cache_init
|
|
|
SYNOPSIS
|
SYNOPSIS
|
bfd_boolean bfd_cache_init (bfd *abfd);
|
bfd_boolean bfd_cache_init (bfd *abfd);
|
|
|
DESCRIPTION
|
DESCRIPTION
|
Add a newly opened BFD to the cache.
|
Add a newly opened BFD to the cache.
|
*/
|
*/
|
|
|
bfd_boolean
|
bfd_boolean
|
bfd_cache_init (bfd *abfd)
|
bfd_cache_init (bfd *abfd)
|
{
|
{
|
BFD_ASSERT (abfd->iostream != NULL);
|
BFD_ASSERT (abfd->iostream != NULL);
|
if (open_files >= BFD_CACHE_MAX_OPEN)
|
if (open_files >= BFD_CACHE_MAX_OPEN)
|
{
|
{
|
if (! close_one ())
|
if (! close_one ())
|
return FALSE;
|
return FALSE;
|
}
|
}
|
abfd->iovec = &cache_iovec;
|
abfd->iovec = &cache_iovec;
|
insert (abfd);
|
insert (abfd);
|
++open_files;
|
++open_files;
|
return TRUE;
|
return TRUE;
|
}
|
}
|
|
|
/*
|
/*
|
INTERNAL_FUNCTION
|
INTERNAL_FUNCTION
|
bfd_cache_close
|
bfd_cache_close
|
|
|
SYNOPSIS
|
SYNOPSIS
|
bfd_boolean bfd_cache_close (bfd *abfd);
|
bfd_boolean bfd_cache_close (bfd *abfd);
|
|
|
DESCRIPTION
|
DESCRIPTION
|
Remove the BFD @var{abfd} from the cache. If the attached file is open,
|
Remove the BFD @var{abfd} from the cache. If the attached file is open,
|
then close it too.
|
then close it too.
|
|
|
RETURNS
|
RETURNS
|
<<FALSE>> is returned if closing the file fails, <<TRUE>> is
|
<<FALSE>> is returned if closing the file fails, <<TRUE>> is
|
returned if all is well.
|
returned if all is well.
|
*/
|
*/
|
|
|
bfd_boolean
|
bfd_boolean
|
bfd_cache_close (bfd *abfd)
|
bfd_cache_close (bfd *abfd)
|
{
|
{
|
if (abfd->iovec != &cache_iovec)
|
if (abfd->iovec != &cache_iovec)
|
return TRUE;
|
return TRUE;
|
|
|
if (abfd->iostream == NULL)
|
if (abfd->iostream == NULL)
|
/* Previously closed. */
|
/* Previously closed. */
|
return TRUE;
|
return TRUE;
|
|
|
return bfd_cache_delete (abfd);
|
return bfd_cache_delete (abfd);
|
}
|
}
|
|
|
/*
|
/*
|
FUNCTION
|
FUNCTION
|
bfd_cache_close_all
|
bfd_cache_close_all
|
|
|
SYNOPSIS
|
SYNOPSIS
|
bfd_boolean bfd_cache_close_all (void);
|
bfd_boolean bfd_cache_close_all (void);
|
|
|
DESCRIPTION
|
DESCRIPTION
|
Remove all BFDs from the cache. If the attached file is open,
|
Remove all BFDs from the cache. If the attached file is open,
|
then close it too.
|
then close it too.
|
|
|
RETURNS
|
RETURNS
|
<<FALSE>> is returned if closing one of the file fails, <<TRUE>> is
|
<<FALSE>> is returned if closing one of the file fails, <<TRUE>> is
|
returned if all is well.
|
returned if all is well.
|
*/
|
*/
|
|
|
bfd_boolean
|
bfd_boolean
|
bfd_cache_close_all ()
|
bfd_cache_close_all ()
|
{
|
{
|
bfd_boolean ret = TRUE;
|
bfd_boolean ret = TRUE;
|
|
|
while (bfd_last_cache != NULL)
|
while (bfd_last_cache != NULL)
|
ret &= bfd_cache_close (bfd_last_cache);
|
ret &= bfd_cache_close (bfd_last_cache);
|
|
|
return ret;
|
return ret;
|
}
|
}
|
|
|
/*
|
/*
|
INTERNAL_FUNCTION
|
INTERNAL_FUNCTION
|
bfd_open_file
|
bfd_open_file
|
|
|
SYNOPSIS
|
SYNOPSIS
|
FILE* bfd_open_file (bfd *abfd);
|
FILE* bfd_open_file (bfd *abfd);
|
|
|
DESCRIPTION
|
DESCRIPTION
|
Call the OS to open a file for @var{abfd}. Return the <<FILE *>>
|
Call the OS to open a file for @var{abfd}. Return the <<FILE *>>
|
(possibly <<NULL>>) that results from this operation. Set up the
|
(possibly <<NULL>>) that results from this operation. Set up the
|
BFD so that future accesses know the file is open. If the <<FILE *>>
|
BFD so that future accesses know the file is open. If the <<FILE *>>
|
returned is <<NULL>>, then it won't have been put in the
|
returned is <<NULL>>, then it won't have been put in the
|
cache, so it won't have to be removed from it.
|
cache, so it won't have to be removed from it.
|
*/
|
*/
|
|
|
FILE *
|
FILE *
|
bfd_open_file (bfd *abfd)
|
bfd_open_file (bfd *abfd)
|
{
|
{
|
abfd->cacheable = TRUE; /* Allow it to be closed later. */
|
abfd->cacheable = TRUE; /* Allow it to be closed later. */
|
|
|
if (open_files >= BFD_CACHE_MAX_OPEN)
|
if (open_files >= BFD_CACHE_MAX_OPEN)
|
{
|
{
|
if (! close_one ())
|
if (! close_one ())
|
return NULL;
|
return NULL;
|
}
|
}
|
|
|
switch (abfd->direction)
|
switch (abfd->direction)
|
{
|
{
|
case read_direction:
|
case read_direction:
|
case no_direction:
|
case no_direction:
|
abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_RB);
|
abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_RB);
|
break;
|
break;
|
case both_direction:
|
case both_direction:
|
case write_direction:
|
case write_direction:
|
if (abfd->opened_once)
|
if (abfd->opened_once)
|
{
|
{
|
abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_RUB);
|
abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_RUB);
|
if (abfd->iostream == NULL)
|
if (abfd->iostream == NULL)
|
abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_WUB);
|
abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_WUB);
|
}
|
}
|
else
|
else
|
{
|
{
|
/* Create the file.
|
/* Create the file.
|
|
|
Some operating systems won't let us overwrite a running
|
Some operating systems won't let us overwrite a running
|
binary. For them, we want to unlink the file first.
|
binary. For them, we want to unlink the file first.
|
|
|
However, gcc 2.95 will create temporary files using
|
However, gcc 2.95 will create temporary files using
|
O_EXCL and tight permissions to prevent other users from
|
O_EXCL and tight permissions to prevent other users from
|
substituting other .o files during the compilation. gcc
|
substituting other .o files during the compilation. gcc
|
will then tell the assembler to use the newly created
|
will then tell the assembler to use the newly created
|
file as an output file. If we unlink the file here, we
|
file as an output file. If we unlink the file here, we
|
open a brief window when another user could still
|
open a brief window when another user could still
|
substitute a file.
|
substitute a file.
|
|
|
So we unlink the output file if and only if it has
|
So we unlink the output file if and only if it has
|
non-zero size. */
|
non-zero size. */
|
#ifndef __MSDOS__
|
#ifndef __MSDOS__
|
/* Don't do this for MSDOS: it doesn't care about overwriting
|
/* Don't do this for MSDOS: it doesn't care about overwriting
|
a running binary, but if this file is already open by
|
a running binary, but if this file is already open by
|
another BFD, we will be in deep trouble if we delete an
|
another BFD, we will be in deep trouble if we delete an
|
open file. In fact, objdump does just that if invoked with
|
open file. In fact, objdump does just that if invoked with
|
the --info option. */
|
the --info option. */
|
struct stat s;
|
struct stat s;
|
|
|
if (stat (abfd->filename, &s) == 0 && s.st_size != 0)
|
if (stat (abfd->filename, &s) == 0 && s.st_size != 0)
|
unlink_if_ordinary (abfd->filename);
|
unlink_if_ordinary (abfd->filename);
|
#endif
|
#endif
|
abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_WUB);
|
abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_WUB);
|
abfd->opened_once = TRUE;
|
abfd->opened_once = TRUE;
|
}
|
}
|
break;
|
break;
|
}
|
}
|
|
|
if (abfd->iostream == NULL)
|
if (abfd->iostream == NULL)
|
bfd_set_error (bfd_error_system_call);
|
bfd_set_error (bfd_error_system_call);
|
else
|
else
|
{
|
{
|
if (! bfd_cache_init (abfd))
|
if (! bfd_cache_init (abfd))
|
return NULL;
|
return NULL;
|
}
|
}
|
|
|
return (FILE *) abfd->iostream;
|
return (FILE *) abfd->iostream;
|
}
|
}
|
|
|