2007-05-03 Sandra Loosemore <sandra@codesourcery.com>
[deliverable/binutils-gdb.git] / bfd / bfdwin.c
index 442a8a4905bf835cd402f4071360f6a660ae94f4..7236906e28bc3ddcd1711f14844af19079f62549 100644 (file)
@@ -1,5 +1,6 @@
 /* Support for memory-mapped windows into a BFD.
-   Copyright 1995, 1996, 2001, 2002 Free Software Foundation, Inc.
+   Copyright 1995, 1996, 2001, 2002, 2003, 2005
+   Free Software Foundation, Inc.
    Written by Cygnus Support.
 
 This file is part of BFD, the Binary File Descriptor library.
@@ -16,7 +17,7 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
 
 #include "sysdep.h"
 
@@ -52,7 +53,7 @@ INTERNAL_DEFINITION
 
 .struct _bfd_window_internal {
 .  struct _bfd_window_internal *next;
-.  PTR data;
+.  void *data;
 .  bfd_size_type size;
 .  int refcount : 31;          {* should be enough...  *}
 .  unsigned mapped : 1;                {* 1 = mmap, 0 = malloc *}
@@ -60,8 +61,7 @@ INTERNAL_DEFINITION
 */
 
 void
-bfd_init_window (windowp)
-     bfd_window *windowp;
+bfd_init_window (bfd_window *windowp)
 {
   windowp->data = 0;
   windowp->i = 0;
@@ -69,8 +69,7 @@ bfd_init_window (windowp)
 }
 
 void
-bfd_free_window (windowp)
-     bfd_window *windowp;
+bfd_free_window (bfd_window *windowp)
 {
   bfd_window_internal *i = windowp->i;
   windowp->i = 0;
@@ -108,12 +107,11 @@ bfd_free_window (windowp)
 static int ok_to_map = 1;
 
 bfd_boolean
-bfd_get_file_window (abfd, offset, size, windowp, writable)
-     bfd *abfd;
-     file_ptr offset;
-     bfd_size_type size;
-     bfd_window *windowp;
-     bfd_boolean writable;
+bfd_get_file_window (bfd *abfd,
+                    file_ptr offset,
+                    bfd_size_type size,
+                    bfd_window *windowp,
+                    bfd_boolean writable)
 {
   static size_t pagesize;
   bfd_window_internal *i = windowp->i;
@@ -133,8 +131,7 @@ bfd_get_file_window (abfd, offset, size, windowp, writable)
 
   if (i == 0)
     {
-      i = ((bfd_window_internal *)
-          bfd_zmalloc ((bfd_size_type) sizeof (bfd_window_internal)));
+      i = bfd_zmalloc (sizeof (bfd_window_internal));
       windowp->i = i;
       if (i == 0)
        return FALSE;
@@ -148,7 +145,6 @@ bfd_get_file_window (abfd, offset, size, windowp, writable)
       file_ptr file_offset, offset2;
       size_t real_size;
       int fd;
-      FILE *f;
 
       /* Find the real file and the real offset into it.  */
       while (abfd->my_archive != NULL)
@@ -156,8 +152,13 @@ bfd_get_file_window (abfd, offset, size, windowp, writable)
          offset += abfd->origin;
          abfd = abfd->my_archive;
        }
-      f = bfd_cache_lookup (abfd);
-      fd = fileno (f);
+
+      /* Seek into the file, to ensure it is open if cacheable.  */
+      if (abfd->iostream == NULL
+         && (abfd->iovec == NULL
+             || abfd->iovec->bseek (abfd, offset, SEEK_SET) != 0))
+       return FALSE;
+      fd = fileno ((FILE *) abfd->iostream);
 
       /* Compute offsets and size for mmap and for the user's data.  */
       offset2 = offset % pagesize;
@@ -180,7 +181,7 @@ bfd_get_file_window (abfd, offset, size, windowp, writable)
                       ? MAP_FILE | MAP_PRIVATE
                       : MAP_FILE | MAP_SHARED),
                      fd, file_offset);
-      if (i->data == (PTR) -1)
+      if (i->data == (void *) -1)
        {
          /* An error happened.  Report it, or try using malloc, or
             something.  */
@@ -195,7 +196,7 @@ bfd_get_file_window (abfd, offset, size, windowp, writable)
        fprintf (stderr, "\n\tmapped %ld at %p, offset is %ld\n",
                 (long) real_size, i->data, (long) offset2);
       i->size = real_size;
-      windowp->data = (PTR) ((bfd_byte *) i->data + offset2);
+      windowp->data = (bfd_byte *) i->data + offset2;
       windowp->size = size;
       i->mapped = 1;
       return TRUE;
@@ -222,7 +223,7 @@ bfd_get_file_window (abfd, offset, size, windowp, writable)
   if (debug_windows)
     fprintf (stderr, "\n\t%s(%6ld)",
             i->data ? "realloc" : " malloc", (long) size_to_alloc);
-  i->data = (PTR) bfd_realloc (i->data, size_to_alloc);
+  i->data = bfd_realloc (i->data, size_to_alloc);
   if (debug_windows)
     fprintf (stderr, "\t-> %p\n", i->data);
   i->refcount = 1;
This page took 0.028673 seconds and 4 git commands to generate.