]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - bfd/bfdwin.c
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / bfd / bfdwin.c
index 7236906e28bc3ddcd1711f14844af19079f62549..beb17398744d8c3522516d7bae4133691dbbd29c 100644 (file)
@@ -1,33 +1,31 @@
 /* Support for memory-mapped windows into a BFD.
-   Copyright 1995, 1996, 2001, 2002, 2003, 2005
-   Free Software Foundation, Inc.
+   Copyright (C) 1995-2024 Free Software Foundation, Inc.
    Written by Cygnus Support.
 
-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
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
-(at your option) any later version.
+   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
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
 
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
+   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., 51 Franklin Street - Fifth Floor, Boston,
+   MA 02110-1301, USA.  */
 
 #include "sysdep.h"
 
 #include "bfd.h"
 #include "libbfd.h"
 
-/* Currently, if USE_MMAP is undefined, none if the window stuff is
-   used.  Okay, so it's mis-named.  At least the command-line option
-   "--without-mmap" is more obvious than "--without-windows" or some
-   such.  */
+/* Currently, if USE_MMAP is undefined, none of the window stuff is
+   used.  Enabled by --with-mmap.  */
 
 #ifdef USE_MMAP
 
@@ -49,15 +47,47 @@ static int debug_windows;
    though.  */
 
 /*
-INTERNAL_DEFINITION
-
-.struct _bfd_window_internal {
+INTERNAL
+.typedef struct _bfd_window_internal
+.{
 .  struct _bfd_window_internal *next;
 .  void *data;
 .  bfd_size_type size;
 .  int refcount : 31;          {* should be enough...  *}
 .  unsigned mapped : 1;                {* 1 = mmap, 0 = malloc *}
-.};
+.}
+.bfd_window_internal;
+.
+
+EXTERNAL
+.struct _bfd_window_internal;
+.
+.typedef struct _bfd_window
+.{
+.  {* What the user asked for.  *}
+.  void *data;
+.  bfd_size_type size;
+.  {* The actual window used by BFD.  Small user-requested read-only
+.     regions sharing a page may share a single window into the object
+.     file.  Read-write versions shouldn't until I've fixed things to
+.     keep track of which portions have been claimed by the
+.     application; don't want to give the same region back when the
+.     application wants two writable copies!  *}
+.  struct _bfd_window_internal *i;
+.}
+.bfd_window;
+.
+*/
+
+/*
+FUNCTION
+       bfd_init_window
+
+SYNOPSIS
+       void bfd_init_window (bfd_window *);
+
+DESCRIPTION
+       Initialise mmap window.
 */
 
 void
@@ -68,6 +98,17 @@ bfd_init_window (bfd_window *windowp)
   windowp->size = 0;
 }
 
+/*
+FUNCTION
+       bfd_free_window
+
+SYNOPSIS
+       void bfd_free_window (bfd_window *);
+
+DESCRIPTION
+       Finalise mmap window struct.
+*/
+
 void
 bfd_free_window (bfd_window *windowp)
 {
@@ -79,7 +120,7 @@ bfd_free_window (bfd_window *windowp)
   i->refcount--;
   if (debug_windows)
     fprintf (stderr, "freeing window @%p<%p,%lx,%p>\n",
-            windowp, windowp->data, windowp->size, windowp->i);
+            windowp, windowp->data, (unsigned long) windowp->size, windowp->i);
   if (i->refcount != 0)
     return;
 
@@ -104,15 +145,26 @@ bfd_free_window (bfd_window *windowp)
   free (i);
 }
 
-static int ok_to_map = 1;
+/*
+FUNCTION
+       bfd_get_file_window
 
-bfd_boolean
+SYNOPSIS
+       bool bfd_get_file_window
+         (bfd *, file_ptr, bfd_size_type, bfd_window *, bool {*writable*});
+
+DESCRIPTION
+       mmap from a bfd's iostream.
+*/
+
+bool
 bfd_get_file_window (bfd *abfd,
                     file_ptr offset,
                     bfd_size_type size,
                     bfd_window *windowp,
-                    bfd_boolean writable)
+                    bool writable)
 {
+  static int ok_to_map = 1;
   static size_t pagesize;
   bfd_window_internal *i = windowp->i;
   bfd_size_type size_to_alloc = size;
@@ -129,17 +181,16 @@ bfd_get_file_window (bfd *abfd,
   if (pagesize == 0)
     abort ();
 
-  if (i == 0)
+  if (i == NULL)
     {
       i = bfd_zmalloc (sizeof (bfd_window_internal));
-      windowp->i = i;
-      if (i == 0)
-       return FALSE;
-      i->data = 0;
+      if (i == NULL)
+       return false;
+      i->data = NULL;
     }
 #ifdef HAVE_MMAP
   if (ok_to_map
-      && (i->data == 0 || i->mapped == 1)
+      && (i->data == NULL || i->mapped == 1)
       && (abfd->flags & BFD_IN_MEMORY) == 0)
     {
       file_ptr file_offset, offset2;
@@ -147,19 +198,21 @@ bfd_get_file_window (bfd *abfd,
       int fd;
 
       /* Find the real file and the real offset into it.  */
-      while (abfd->my_archive != NULL)
+      while (abfd->my_archive != NULL
+            && !bfd_is_thin_archive (abfd->my_archive))
        {
          offset += abfd->origin;
          abfd = abfd->my_archive;
        }
+      offset += abfd->origin;
 
       /* 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);
+       goto free_and_fail;
 
+      fd = fileno ((FILE *) abfd->iostream);
       /* Compute offsets and size for mmap and for the user's data.  */
       offset2 = offset % pagesize;
       if (offset2 < 0)
@@ -170,10 +223,10 @@ bfd_get_file_window (bfd *abfd,
       real_size -= real_size % pagesize;
 
       /* If we're re-using a memory region, make sure it's big enough.  */
-      if (i->data && i->size < size)
+      if (i->data != NULL && i->size < size)
        {
          munmap (i->data, i->size);
-         i->data = 0;
+         i->data = NULL;
        }
       i->data = mmap (i->data, real_size,
                      writable ? PROT_WRITE | PROT_READ : PROT_READ,
@@ -186,11 +239,10 @@ bfd_get_file_window (bfd *abfd,
          /* An error happened.  Report it, or try using malloc, or
             something.  */
          bfd_set_error (bfd_error_system_call);
-         i->data = 0;
          windowp->data = 0;
          if (debug_windows)
            fprintf (stderr, "\t\tmmap failed!\n");
-         return FALSE;
+         goto free_and_fail;
        }
       if (debug_windows)
        fprintf (stderr, "\n\tmapped %ld at %p, offset is %ld\n",
@@ -199,7 +251,9 @@ bfd_get_file_window (bfd *abfd,
       windowp->data = (bfd_byte *) i->data + offset2;
       windowp->size = size;
       i->mapped = 1;
-      return TRUE;
+      i->refcount = 1;
+      windowp->i = i;
+      return true;
     }
   else if (debug_windows)
     {
@@ -223,21 +277,24 @@ bfd_get_file_window (bfd *abfd,
   if (debug_windows)
     fprintf (stderr, "\n\t%s(%6ld)",
             i->data ? "realloc" : " malloc", (long) size_to_alloc);
-  i->data = bfd_realloc (i->data, size_to_alloc);
+  i->data = bfd_realloc_or_free (i->data, size_to_alloc);
   if (debug_windows)
     fprintf (stderr, "\t-> %p\n", i->data);
-  i->refcount = 1;
   if (i->data == NULL)
     {
       if (size_to_alloc == 0)
-       return TRUE;
-      return FALSE;
+       {
+         windowp->i = i;
+         return true;
+       }
+      goto free_and_fail;
     }
+  i->refcount = 1;
   if (bfd_seek (abfd, offset, SEEK_SET) != 0)
-    return FALSE;
-  i->size = bfd_bread (i->data, size, abfd);
+    goto free_and_fail;
+  i->size = bfd_read (i->data, size, abfd);
   if (i->size != size)
-    return FALSE;
+    goto free_and_fail;
   i->mapped = 0;
 #ifdef HAVE_MPROTECT
   if (!writable)
@@ -250,7 +307,13 @@ bfd_get_file_window (bfd *abfd,
 #endif
   windowp->data = i->data;
   windowp->size = i->size;
-  return TRUE;
+  windowp->i = i;
+  return true;
+
+ free_and_fail:
+  /* We have a bfd_window_internal, but an error occurred.  Free it. */
+  free (i);
+  return false;
 }
 
 #endif /* USE_MMAP */