diff options
author | Klee Dienes <kdienes@apple.com> | 2002-11-18 16:37:54 +0000 |
---|---|---|
committer | Klee Dienes <kdienes@apple.com> | 2002-11-18 16:37:54 +0000 |
commit | 93509525ce7ec98e549760eae1848ebf2fff0c9b (patch) | |
tree | 631684b5c008f47a5fd235020b6a2411decf239e /bfd/bfdwin.c | |
parent | eb1284494b10dc685a3b2cf3bd622d605a37b003 (diff) | |
download | gdb-93509525ce7ec98e549760eae1848ebf2fff0c9b.zip gdb-93509525ce7ec98e549760eae1848ebf2fff0c9b.tar.gz gdb-93509525ce7ec98e549760eae1848ebf2fff0c9b.tar.bz2 |
2002-11-16 Klee Dienes <kdienes@apple.com>
* Makefile.am (BFD32_LIBS): Add bfdwin.lo, bfdio.lo.
(BFD32_LIBS_CFILES): Add bfdwin.c, bfdio.c.
(BFD_H_FILES): Add bfdwin.c, bfdio.c.
(LIBBFD_H_FILES): Add bfdwin.c, bfdio.c.
Add dependencies for bfdwin.c, bfdio.c.
* bfd.c: Remove bfd_get_mtime, bfd_get_size.
* libbfd.c: Remove real_read, bfd_bread, _bfd_window_internal,
bfd_init_window, bfd_free_window, bfd_get_file_window, bfd_bwrite,
bfd_tell, bfd_flush, bfd_stat, bfd_seek.
* bfdio.c: New file. Contains real_read, bfd_bread, bfd_write,
bfd_tell, bfd_flush, bfd_stat, bfd_seek, bfd_ge_mtime,
bfd_get_size (moved from libbfd.c and bfd.c).
* bfdwin.c New file. Contains _bfd_window_internal,
bfd_init_window, bfd_free_window, bfd_get_file_window (moved from
libbfd.c and bfd.c).
* po/SRC-POTFILES.in: Regenerate.
* po/bfd.pot: Regenerate.
* libbfd.h: Regenerate.
* bfd-in2.h: Regenerate.
* aclocal.m4: Regenerate.
* Makefile.in: Regenerate.
* configure: Regenerate.
2002-11-16 Klee Dienes <kdienes@apple.com>
* Makefile.am (DOCFILES): Add bfdwin.texi, bfdio.texi.
(PROTOS): Add bfdio.p, bfdwin.p.
(SRCDOC): Add bfdio.c, bfdwin.c.
(SRCPROT): Add bfdio.c, bfdwin.c.
(SRCIPROT): Add bfdio.c, bfdwin.c.
(LIBBFD_H_DEP): Add bfdio.c, bfdwin.c.
(BFD_H_DEP): Add bfdio.c, bfdwin.c.
Add rules for bfdio.texi, bfdwin.text.
* bfd.texinfo: Include bfdio.texi.
Diffstat (limited to 'bfd/bfdwin.c')
-rw-r--r-- | bfd/bfdwin.c | 255 |
1 files changed, 255 insertions, 0 deletions
diff --git a/bfd/bfdwin.c b/bfd/bfdwin.c new file mode 100644 index 0000000..4965458 --- /dev/null +++ b/bfd/bfdwin.c @@ -0,0 +1,255 @@ +/* Support for memory-mapped windows into a BFD. + Copyright 1995, 1996, 2001, 2002 Free Software Foundation, Inc. + Written by Cygnus Support. + +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 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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. */ + +#ifdef USE_MMAP + +#undef HAVE_MPROTECT /* code's not tested yet */ + +#if HAVE_MMAP || HAVE_MPROTECT || HAVE_MADVISE +#include <sys/mman.h> +#endif + +#ifndef MAP_FILE +#define MAP_FILE 0 +#endif + +static int debug_windows; + +/* The idea behind the next and refcount fields is that one mapped + region can suffice for multiple read-only windows or multiple + non-overlapping read-write windows. It's not implemented yet + though. */ + +/* +INTERNAL_DEFINITION + +.struct _bfd_window_internal { +. struct _bfd_window_internal *next; +. PTR data; +. bfd_size_type size; +. int refcount : 31; {* should be enough... *} +. unsigned mapped : 1; {* 1 = mmap, 0 = malloc *} +.}; +*/ + +void +bfd_init_window (windowp) + bfd_window *windowp; +{ + windowp->data = 0; + windowp->i = 0; + windowp->size = 0; +} + +void +bfd_free_window (windowp) + bfd_window *windowp; +{ + bfd_window_internal *i = windowp->i; + windowp->i = 0; + windowp->data = 0; + if (i == 0) + return; + i->refcount--; + if (debug_windows) + fprintf (stderr, "freeing window @%p<%p,%lx,%p>\n", + windowp, windowp->data, windowp->size, windowp->i); + if (i->refcount != 0) + return; + + if (i->mapped) + { +#ifdef HAVE_MMAP + munmap (i->data, i->size); + goto no_free; +#else + abort (); +#endif + } +#ifdef HAVE_MPROTECT + mprotect (i->data, i->size, PROT_READ | PROT_WRITE); +#endif + free (i->data); +#ifdef HAVE_MMAP + no_free: +#endif + i->data = 0; + /* There should be no more references to i at this point. */ + free (i); +} + +static int ok_to_map = 1; + +boolean +bfd_get_file_window (abfd, offset, size, windowp, writable) + bfd *abfd; + file_ptr offset; + bfd_size_type size; + bfd_window *windowp; + boolean writable; +{ + static size_t pagesize; + bfd_window_internal *i = windowp->i; + bfd_size_type size_to_alloc = size; + + if (debug_windows) + fprintf (stderr, "bfd_get_file_window (%p, %6ld, %6ld, %p<%p,%lx,%p>, %d)", + abfd, (long) offset, (long) size, + windowp, windowp->data, (unsigned long) windowp->size, + windowp->i, writable); + + /* Make sure we know the page size, so we can be friendly to mmap. */ + if (pagesize == 0) + pagesize = getpagesize (); + if (pagesize == 0) + abort (); + + if (i == 0) + { + i = ((bfd_window_internal *) + bfd_zmalloc ((bfd_size_type) sizeof (bfd_window_internal))); + windowp->i = i; + if (i == 0) + return false; + i->data = 0; + } +#ifdef HAVE_MMAP + if (ok_to_map + && (i->data == 0 || i->mapped == 1) + && (abfd->flags & BFD_IN_MEMORY) == 0) + { + 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) + { + offset += abfd->origin; + abfd = abfd->my_archive; + } + f = bfd_cache_lookup (abfd); + fd = fileno (f); + + /* Compute offsets and size for mmap and for the user's data. */ + offset2 = offset % pagesize; + if (offset2 < 0) + abort (); + file_offset = offset - offset2; + real_size = offset + size - file_offset; + real_size = real_size + pagesize - 1; + 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) + { + munmap (i->data, i->size); + i->data = 0; + } + i->data = mmap (i->data, real_size, + writable ? PROT_WRITE | PROT_READ : PROT_READ, + (writable + ? MAP_FILE | MAP_PRIVATE + : MAP_FILE | MAP_SHARED), + fd, file_offset); + if (i->data == (PTR) -1) + { + /* 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; + } + if (debug_windows) + 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->size = size; + i->mapped = 1; + return true; + } + else if (debug_windows) + { + if (ok_to_map) + fprintf (stderr, _("not mapping: data=%lx mapped=%d\n"), + (unsigned long) i->data, (int) i->mapped); + else + fprintf (stderr, _("not mapping: env var not set\n")); + } +#else + ok_to_map = 0; +#endif + +#ifdef HAVE_MPROTECT + if (!writable) + { + size_to_alloc += pagesize - 1; + size_to_alloc -= size_to_alloc % pagesize; + } +#endif + 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); + 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; + } + if (bfd_seek (abfd, offset, SEEK_SET) != 0) + return false; + i->size = bfd_bread (i->data, size, abfd); + if (i->size != size) + return false; + i->mapped = 0; +#ifdef HAVE_MPROTECT + if (!writable) + { + if (debug_windows) + fprintf (stderr, "\tmprotect (%p, %ld, PROT_READ)\n", i->data, + (long) i->size); + mprotect (i->data, i->size, PROT_READ); + } +#endif + windowp->data = i->data; + windowp->size = i->size; + return true; +} + +#endif /* USE_MMAP */ |