/* 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.
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
/* 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;
/* 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 "
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. */
munmap ((*entry)->buffer, (*entry)->munmap_length);
else
#endif
- zfree ((*entry)->buffer);
+ free ((*entry)->buffer);
}
/* delete it and its aliases */
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;
}
parse_addr (arg, &level, &space, &addr);
return do_memopt_delete (sd, level, space, addr);
}
-
+
case OPTION_MEMORY_REGION:
{
char *chp = arg;
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);
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;
}
munmap ((*entry)->buffer, (*entry)->munmap_length);
else
#endif
- zfree ((*entry)->buffer);
+ free ((*entry)->buffer);
}
/* delete it and its aliases */
sim_memopt *dead = alias;
alias = alias->alias;
sim_core_detach (sd, NULL, dead->level, dead->space, dead->addr);
- zfree (dead);
+ free (dead);
}
}
}