]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - sim/common/sim-memopt.c
sim: switch config.h usage to defs.h
[thirdparty/binutils-gdb.git] / sim / common / sim-memopt.c
index 8aa36ffce8769d1590d087335b8bd566d4c31c9a..636f6a9a0febb94fffc00a64079427587da214a6 100644 (file)
@@ -1,6 +1,5 @@
 /* Simulator memory option handling.
-   Copyright (C) 1996-1999, 2007, 2008, 2009, 2010, 2011
-   Free Software Foundation, Inc.
+   Copyright (C) 1996-2021 Free Software Foundation, Inc.
    Contributed by Cygnus Support.
 
 This file is part of GDB, the GNU debugger.
@@ -18,25 +17,16 @@ 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, see <http://www.gnu.org/licenses/>.  */
 
-#include "cconfig.h"
+/* This must come before any other includes.  */
+#include "defs.h"
 
 #include "sim-main.h"
 #include "sim-assert.h"
 #include "sim-options.h"
 
-#ifdef HAVE_STRING_H
 #include <string.h>
-#else
-#ifdef HAVE_STRINGS_H
-#include <strings.h>
-#endif
-#endif
-#ifdef HAVE_STDLIB_H
 #include <stdlib.h>
-#endif
-#ifdef HAVE_ERRNO_H
 #include <errno.h>
-#endif
 #ifdef HAVE_FCNTL_H
 #include <fcntl.h>
 #endif
@@ -154,7 +144,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 +173,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 +186,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 +273,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 +284,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 +375,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 +388,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 +490,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 +620,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 +634,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);
        }
     }
 }