daily update
[external/binutils.git] / sim / common / sim-memopt.c
index 99e6add..89a08bc 100644 (file)
@@ -1,5 +1,5 @@
 /* Simulator memory option handling.
-   Copyright (C) 1996-1999, 2007, 2008, 2009, 2010
+   Copyright (C) 1996-1999, 2007, 2008, 2009, 2010, 2011
    Free Software Foundation, Inc.
    Contributed by Cygnus Support.
 
@@ -154,7 +154,27 @@ do_memopt_add (SIM_DESC sd,
       /* Allocate new well-aligned buffer, just as sim_core_attach(). */
       void *aligned_buffer;
       int padding = (addr % sizeof (unsigned64));
-      unsigned long bytes = (modulo == 0 ? nr_bytes : modulo) + padding;
+      unsigned long bytes;
+
+#ifdef HAVE_MMAP
+      struct stat s;
+
+      if (mmap_next_fd >= 0)
+       {
+         /* Check that given file is big enough. */
+         int rc = fstat (mmap_next_fd, &s);
+
+         if (rc < 0)
+           sim_io_error (sd, "Error, unable to stat file: %s\n",
+                         strerror (errno));
+
+         /* Autosize the mapping to the file length.  */
+         if (nr_bytes == 0)
+           nr_bytes = s.st_size;
+       }
+#endif
+
+      bytes = (modulo == 0 ? nr_bytes : modulo) + padding;
 
       free_buffer = NULL;
       free_length = bytes;
@@ -163,14 +183,9 @@ do_memopt_add (SIM_DESC sd,
       /* Memory map or malloc(). */
       if (mmap_next_fd >= 0)
        {
-         /* Check that given file is big enough. */
-         struct stat s;
-         int rc;
-
          /* Some kernels will SIGBUS the application if mmap'd file
-            is not large enough.  */ 
-         rc = fstat (mmap_next_fd, &s);
-         if (rc < 0 || s.st_size < bytes)
+            is not large enough.  */
+         if (s.st_size < bytes)
            {
              sim_io_error (sd,
                            "Error, cannot confirm that mmap file is large enough "
@@ -181,12 +196,12 @@ do_memopt_add (SIM_DESC sd,
          if (free_buffer == 0 || free_buffer == (char*)-1) /* MAP_FAILED */
            {
              sim_io_error (sd, "Error, cannot mmap file (%s).\n",
-                           strerror(errno));
+                           strerror (errno));
            }
        }
-#endif 
+#endif
 
-      /* Need heap allocation? */ 
+      /* Need heap allocation? */
       if (free_buffer == NULL)
        {
          /* If filling with non-zero value, do not use clearing allocator. */
@@ -268,7 +283,7 @@ do_memopt_delete (SIM_DESC sd,
        munmap ((*entry)->buffer, (*entry)->munmap_length);
       else
 #endif
-       zfree ((*entry)->buffer);
+       free ((*entry)->buffer);
     }
 
   /* delete it and its aliases */
@@ -279,7 +294,7 @@ do_memopt_delete (SIM_DESC sd,
       sim_memopt *dead = alias;
       alias = alias->alias;
       sim_core_detach (sd, NULL, dead->level, dead->space, dead->addr);
-      zfree (dead);
+      free (dead);
     }
   return SIM_RC_OK;
 }
@@ -370,7 +385,7 @@ memory_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
          parse_addr (arg, &level, &space, &addr);
          return do_memopt_delete (sd, level, space, addr);
        }
-    
+
     case OPTION_MEMORY_REGION:
       {
        char *chp = arg;
@@ -383,10 +398,15 @@ memory_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
        chp = parse_addr (chp, &level, &space, &addr);
        if (*chp != ',')
          {
-           sim_io_eprintf (sd, "Missing size for memory-region\n");
-           return SIM_RC_FAIL;
+           /* let the file autosize */
+           if (mmap_next_fd == -1)
+             {
+               sim_io_eprintf (sd, "Missing size for memory-region\n");
+               return SIM_RC_FAIL;
+             }
          }
-       chp = parse_size (chp + 1, &nr_bytes, &modulo);
+       else
+         chp = parse_size (chp + 1, &nr_bytes, &modulo);
        /* old style */
        if (*chp == ',')
          modulo = strtoul (chp + 1, &chp, 0);
@@ -480,7 +500,7 @@ memory_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
        if (mmap_next_fd < 0)
          {
            sim_io_eprintf (sd, "Cannot open file `%s': %s\n",
-                           arg, strerror(errno));
+                           arg, strerror (errno));
            return SIM_RC_FAIL;
          }
 
@@ -610,7 +630,7 @@ sim_memory_uninstall (SIM_DESC sd)
            munmap ((*entry)->buffer, (*entry)->munmap_length);
          else
 #endif
-           zfree ((*entry)->buffer);
+           free ((*entry)->buffer);
        }
 
       /* delete it and its aliases */
@@ -624,7 +644,7 @@ sim_memory_uninstall (SIM_DESC sd)
          sim_memopt *dead = alias;
          alias = alias->alias;
          sim_core_detach (sd, NULL, dead->level, dead->space, dead->addr);
-         zfree (dead);
+         free (dead);
        }
     }
 }