OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [binutils-2.18.50/] [bfd/] [bfdwin.c] - Diff between revs 156 and 816

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 156 Rev 816
/* Support for memory-mapped windows into a BFD.
/* Support for memory-mapped windows into a BFD.
   Copyright 1995, 1996, 2001, 2002, 2003, 2005, 2007, 2008
   Copyright 1995, 1996, 2001, 2002, 2003, 2005, 2007, 2008
   Free Software Foundation, Inc.
   Free Software Foundation, Inc.
   Written by Cygnus Support.
   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
   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.  */
 
 
#include "sysdep.h"
#include "sysdep.h"
 
 
#include "bfd.h"
#include "bfd.h"
#include "libbfd.h"
#include "libbfd.h"
 
 
/* Currently, if USE_MMAP is undefined, none if the window stuff is
/* 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
   used.  Okay, so it's mis-named.  At least the command-line option
   "--without-mmap" is more obvious than "--without-windows" or some
   "--without-mmap" is more obvious than "--without-windows" or some
   such.  */
   such.  */
 
 
#ifdef USE_MMAP
#ifdef USE_MMAP
 
 
#undef HAVE_MPROTECT /* code's not tested yet */
#undef HAVE_MPROTECT /* code's not tested yet */
 
 
#if HAVE_MMAP || HAVE_MPROTECT || HAVE_MADVISE
#if HAVE_MMAP || HAVE_MPROTECT || HAVE_MADVISE
#include <sys/mman.h>
#include <sys/mman.h>
#endif
#endif
 
 
#ifndef MAP_FILE
#ifndef MAP_FILE
#define MAP_FILE 0
#define MAP_FILE 0
#endif
#endif
 
 
static int debug_windows;
static int debug_windows;
 
 
/* The idea behind the next and refcount fields is that one mapped
/* The idea behind the next and refcount fields is that one mapped
   region can suffice for multiple read-only windows or multiple
   region can suffice for multiple read-only windows or multiple
   non-overlapping read-write windows.  It's not implemented yet
   non-overlapping read-write windows.  It's not implemented yet
   though.  */
   though.  */
 
 
/*
/*
INTERNAL_DEFINITION
INTERNAL_DEFINITION
 
 
.struct _bfd_window_internal {
.struct _bfd_window_internal {
.  struct _bfd_window_internal *next;
.  struct _bfd_window_internal *next;
.  void *data;
.  void *data;
.  bfd_size_type size;
.  bfd_size_type size;
.  int refcount : 31;           {* should be enough...  *}
.  int refcount : 31;           {* should be enough...  *}
.  unsigned mapped : 1;         {* 1 = mmap, 0 = malloc *}
.  unsigned mapped : 1;         {* 1 = mmap, 0 = malloc *}
.};
.};
*/
*/
 
 
void
void
bfd_init_window (bfd_window *windowp)
bfd_init_window (bfd_window *windowp)
{
{
  windowp->data = 0;
  windowp->data = 0;
  windowp->i = 0;
  windowp->i = 0;
  windowp->size = 0;
  windowp->size = 0;
}
}
 
 
void
void
bfd_free_window (bfd_window *windowp)
bfd_free_window (bfd_window *windowp)
{
{
  bfd_window_internal *i = windowp->i;
  bfd_window_internal *i = windowp->i;
  windowp->i = 0;
  windowp->i = 0;
  windowp->data = 0;
  windowp->data = 0;
  if (i == 0)
  if (i == 0)
    return;
    return;
  i->refcount--;
  i->refcount--;
  if (debug_windows)
  if (debug_windows)
    fprintf (stderr, "freeing window @%p<%p,%lx,%p>\n",
    fprintf (stderr, "freeing window @%p<%p,%lx,%p>\n",
             windowp, windowp->data, windowp->size, windowp->i);
             windowp, windowp->data, windowp->size, windowp->i);
  if (i->refcount != 0)
  if (i->refcount != 0)
    return;
    return;
 
 
  if (i->mapped)
  if (i->mapped)
    {
    {
#ifdef HAVE_MMAP
#ifdef HAVE_MMAP
      munmap (i->data, i->size);
      munmap (i->data, i->size);
      goto no_free;
      goto no_free;
#else
#else
      abort ();
      abort ();
#endif
#endif
    }
    }
#ifdef HAVE_MPROTECT
#ifdef HAVE_MPROTECT
  mprotect (i->data, i->size, PROT_READ | PROT_WRITE);
  mprotect (i->data, i->size, PROT_READ | PROT_WRITE);
#endif
#endif
  free (i->data);
  free (i->data);
#ifdef HAVE_MMAP
#ifdef HAVE_MMAP
 no_free:
 no_free:
#endif
#endif
  i->data = 0;
  i->data = 0;
  /* There should be no more references to i at this point.  */
  /* There should be no more references to i at this point.  */
  free (i);
  free (i);
}
}
 
 
static int ok_to_map = 1;
static int ok_to_map = 1;
 
 
bfd_boolean
bfd_boolean
bfd_get_file_window (bfd *abfd,
bfd_get_file_window (bfd *abfd,
                     file_ptr offset,
                     file_ptr offset,
                     bfd_size_type size,
                     bfd_size_type size,
                     bfd_window *windowp,
                     bfd_window *windowp,
                     bfd_boolean writable)
                     bfd_boolean writable)
{
{
  static size_t pagesize;
  static size_t pagesize;
  bfd_window_internal *i = windowp->i;
  bfd_window_internal *i = windowp->i;
  bfd_size_type size_to_alloc = size;
  bfd_size_type size_to_alloc = size;
 
 
  if (debug_windows)
  if (debug_windows)
    fprintf (stderr, "bfd_get_file_window (%p, %6ld, %6ld, %p<%p,%lx,%p>, %d)",
    fprintf (stderr, "bfd_get_file_window (%p, %6ld, %6ld, %p<%p,%lx,%p>, %d)",
             abfd, (long) offset, (long) size,
             abfd, (long) offset, (long) size,
             windowp, windowp->data, (unsigned long) windowp->size,
             windowp, windowp->data, (unsigned long) windowp->size,
             windowp->i, writable);
             windowp->i, writable);
 
 
  /* Make sure we know the page size, so we can be friendly to mmap.  */
  /* Make sure we know the page size, so we can be friendly to mmap.  */
  if (pagesize == 0)
  if (pagesize == 0)
    pagesize = getpagesize ();
    pagesize = getpagesize ();
  if (pagesize == 0)
  if (pagesize == 0)
    abort ();
    abort ();
 
 
  if (i == 0)
  if (i == 0)
    {
    {
      i = bfd_zmalloc (sizeof (bfd_window_internal));
      i = bfd_zmalloc (sizeof (bfd_window_internal));
      windowp->i = i;
      windowp->i = i;
      if (i == 0)
      if (i == 0)
        return FALSE;
        return FALSE;
      i->data = 0;
      i->data = 0;
    }
    }
#ifdef HAVE_MMAP
#ifdef HAVE_MMAP
  if (ok_to_map
  if (ok_to_map
      && (i->data == 0 || i->mapped == 1)
      && (i->data == 0 || i->mapped == 1)
      && (abfd->flags & BFD_IN_MEMORY) == 0)
      && (abfd->flags & BFD_IN_MEMORY) == 0)
    {
    {
      file_ptr file_offset, offset2;
      file_ptr file_offset, offset2;
      size_t real_size;
      size_t real_size;
      int fd;
      int fd;
 
 
      /* Find the real file and the real offset into it.  */
      /* Find the real file and the real offset into it.  */
      while (abfd->my_archive != NULL)
      while (abfd->my_archive != NULL)
        {
        {
          offset += abfd->origin;
          offset += abfd->origin;
          abfd = abfd->my_archive;
          abfd = abfd->my_archive;
        }
        }
 
 
      /* Seek into the file, to ensure it is open if cacheable.  */
      /* Seek into the file, to ensure it is open if cacheable.  */
      if (abfd->iostream == NULL
      if (abfd->iostream == NULL
          && (abfd->iovec == NULL
          && (abfd->iovec == NULL
              || abfd->iovec->bseek (abfd, offset, SEEK_SET) != 0))
              || abfd->iovec->bseek (abfd, offset, SEEK_SET) != 0))
        return FALSE;
        return FALSE;
      fd = fileno ((FILE *) abfd->iostream);
      fd = fileno ((FILE *) abfd->iostream);
 
 
      /* Compute offsets and size for mmap and for the user's data.  */
      /* Compute offsets and size for mmap and for the user's data.  */
      offset2 = offset % pagesize;
      offset2 = offset % pagesize;
      if (offset2 < 0)
      if (offset2 < 0)
        abort ();
        abort ();
      file_offset = offset - offset2;
      file_offset = offset - offset2;
      real_size = offset + size - file_offset;
      real_size = offset + size - file_offset;
      real_size = real_size + pagesize - 1;
      real_size = real_size + pagesize - 1;
      real_size -= real_size % pagesize;
      real_size -= real_size % pagesize;
 
 
      /* If we're re-using a memory region, make sure it's big enough.  */
      /* If we're re-using a memory region, make sure it's big enough.  */
      if (i->data && i->size < size)
      if (i->data && i->size < size)
        {
        {
          munmap (i->data, i->size);
          munmap (i->data, i->size);
          i->data = 0;
          i->data = 0;
        }
        }
      i->data = mmap (i->data, real_size,
      i->data = mmap (i->data, real_size,
                      writable ? PROT_WRITE | PROT_READ : PROT_READ,
                      writable ? PROT_WRITE | PROT_READ : PROT_READ,
                      (writable
                      (writable
                       ? MAP_FILE | MAP_PRIVATE
                       ? MAP_FILE | MAP_PRIVATE
                       : MAP_FILE | MAP_SHARED),
                       : MAP_FILE | MAP_SHARED),
                      fd, file_offset);
                      fd, file_offset);
      if (i->data == (void *) -1)
      if (i->data == (void *) -1)
        {
        {
          /* An error happened.  Report it, or try using malloc, or
          /* An error happened.  Report it, or try using malloc, or
             something.  */
             something.  */
          bfd_set_error (bfd_error_system_call);
          bfd_set_error (bfd_error_system_call);
          i->data = 0;
          i->data = 0;
          windowp->data = 0;
          windowp->data = 0;
          if (debug_windows)
          if (debug_windows)
            fprintf (stderr, "\t\tmmap failed!\n");
            fprintf (stderr, "\t\tmmap failed!\n");
          return FALSE;
          return FALSE;
        }
        }
      if (debug_windows)
      if (debug_windows)
        fprintf (stderr, "\n\tmapped %ld at %p, offset is %ld\n",
        fprintf (stderr, "\n\tmapped %ld at %p, offset is %ld\n",
                 (long) real_size, i->data, (long) offset2);
                 (long) real_size, i->data, (long) offset2);
      i->size = real_size;
      i->size = real_size;
      windowp->data = (bfd_byte *) i->data + offset2;
      windowp->data = (bfd_byte *) i->data + offset2;
      windowp->size = size;
      windowp->size = size;
      i->mapped = 1;
      i->mapped = 1;
      return TRUE;
      return TRUE;
    }
    }
  else if (debug_windows)
  else if (debug_windows)
    {
    {
      if (ok_to_map)
      if (ok_to_map)
        fprintf (stderr, _("not mapping: data=%lx mapped=%d\n"),
        fprintf (stderr, _("not mapping: data=%lx mapped=%d\n"),
                 (unsigned long) i->data, (int) i->mapped);
                 (unsigned long) i->data, (int) i->mapped);
      else
      else
        fprintf (stderr, _("not mapping: env var not set\n"));
        fprintf (stderr, _("not mapping: env var not set\n"));
    }
    }
#else
#else
  ok_to_map = 0;
  ok_to_map = 0;
#endif
#endif
 
 
#ifdef HAVE_MPROTECT
#ifdef HAVE_MPROTECT
  if (!writable)
  if (!writable)
    {
    {
      size_to_alloc += pagesize - 1;
      size_to_alloc += pagesize - 1;
      size_to_alloc -= size_to_alloc % pagesize;
      size_to_alloc -= size_to_alloc % pagesize;
    }
    }
#endif
#endif
  if (debug_windows)
  if (debug_windows)
    fprintf (stderr, "\n\t%s(%6ld)",
    fprintf (stderr, "\n\t%s(%6ld)",
             i->data ? "realloc" : " malloc", (long) size_to_alloc);
             i->data ? "realloc" : " malloc", (long) size_to_alloc);
  i->data = bfd_realloc_or_free (i->data, size_to_alloc);
  i->data = bfd_realloc_or_free (i->data, size_to_alloc);
  if (debug_windows)
  if (debug_windows)
    fprintf (stderr, "\t-> %p\n", i->data);
    fprintf (stderr, "\t-> %p\n", i->data);
  if (i->data == NULL)
  if (i->data == NULL)
    {
    {
      if (size_to_alloc == 0)
      if (size_to_alloc == 0)
        return TRUE;
        return TRUE;
      return FALSE;
      return FALSE;
    }
    }
  i->refcount = 1;
  i->refcount = 1;
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
  if (bfd_seek (abfd, offset, SEEK_SET) != 0)
    return FALSE;
    return FALSE;
  i->size = bfd_bread (i->data, size, abfd);
  i->size = bfd_bread (i->data, size, abfd);
  if (i->size != size)
  if (i->size != size)
    return FALSE;
    return FALSE;
  i->mapped = 0;
  i->mapped = 0;
#ifdef HAVE_MPROTECT
#ifdef HAVE_MPROTECT
  if (!writable)
  if (!writable)
    {
    {
      if (debug_windows)
      if (debug_windows)
        fprintf (stderr, "\tmprotect (%p, %ld, PROT_READ)\n", i->data,
        fprintf (stderr, "\tmprotect (%p, %ld, PROT_READ)\n", i->data,
                 (long) i->size);
                 (long) i->size);
      mprotect (i->data, i->size, PROT_READ);
      mprotect (i->data, i->size, PROT_READ);
    }
    }
#endif
#endif
  windowp->data = i->data;
  windowp->data = i->data;
  windowp->size = i->size;
  windowp->size = i->size;
  return TRUE;
  return TRUE;
}
}
 
 
#endif /* USE_MMAP */
#endif /* USE_MMAP */
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.