diff --git a/gdb/regcache.h b/gdb/regcache.h
index e5a7cf553279b8cc0d546ec1b8274cbf97e246d5..5bc99f5c1ef87318edf4e934ec60c7f1225e7561 100644
--- a/gdb/regcache.h
+++ b/gdb/regcache.h
@@ -202,6 +202,8 @@ extern struct type *register_type (struct gdbarch *gdbarch, int regnum);

extern int register_size (struct gdbarch *gdbarch, int regnum);

+/* Return the size of the largest register.  */
+extern long max_register_size (struct gdbarch *gdbarch);

/* Save/restore a register cache.  The set of registers saved /
   restored into the DST regcache determined by the save_reggroup /
diff --git a/gdb/regcache.c b/gdb/regcache.c
index 9d28aa2c2114e0f1c52758bb2fbe9669a329c13e..1cdc0604ff26110df4de1ab469d2fe623f918b3d 100644
--- a/gdb/regcache.c
+++ b/gdb/regcache.c
@@ -73,6 +73,9 @@ struct regcache_descr

  /* Cached table containing the type of each register.  */
  struct type **register_type;
+
+  /* Size of the largest register.  */
+  long max_register_size;
};

static void *
@@ -126,6 +129,8 @@ init_regcache_descr (struct gdbarch *gdbarch)
        descr->register_offset[i] = offset;
        offset += descr->sizeof_register[i];
        gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
+       descr->max_register_size = std::max (descr->max_register_size,
+                                            descr->sizeof_register[i]);
      }
    /* Set the real size of the raw register cache buffer.  */
    descr->sizeof_raw_registers = offset;
@@ -136,6 +141,8 @@ init_regcache_descr (struct gdbarch *gdbarch)
        descr->register_offset[i] = offset;
        offset += descr->sizeof_register[i];
        gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
+       descr->max_register_size = std::max (descr->max_register_size,
+                                            descr->sizeof_register[i]);
      }
    /* Set the real size of the readonly register cache buffer.  */
    descr->sizeof_cooked_registers = offset;
@@ -187,6 +194,13 @@ regcache_register_size (const struct regcache *regcache, int n)
  return register_size (get_regcache_arch (regcache), n);
}

+long
+max_register_size (struct gdbarch *gdbarch)
+{
+  struct regcache_descr *descr = regcache_descr (gdbarch);
+  return descr->max_register_size;
+}
+
/* The register cache for storing raw register values.  */

struct regcache
@@ -327,7 +341,7 @@ regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read,
               void *src)
{
  struct gdbarch *gdbarch = dst->descr->gdbarch;
-  gdb_byte buf[MAX_REGISTER_SIZE];
+  gdb_byte *buf = (gdb_byte *) alloca (max_register_size (gdbarch));
  int regnum;

  /* The DST should be `read-only', if it wasn't then the save would
@@ -369,7 +383,7 @@ regcache_restore (struct regcache *dst,
                  void *cooked_read_context)
{
  struct gdbarch *gdbarch = dst->descr->gdbarch;
-  gdb_byte buf[MAX_REGISTER_SIZE];
+  gdb_byte *buf = (gdb_byte *) alloca (max_register_size (gdbarch));
  int regnum;

  /* The dst had better not be read-only.  If it is, the `restore'
@@ -1279,7 +1293,7 @@ regcache_dump (struct regcache *regcache, struct ui_file *file,
  int footnote_register_offset = 0;
  int footnote_register_type_name_null = 0;
  long register_offset = 0;
-  gdb_byte buf[MAX_REGISTER_SIZE];
+  gdb_byte *buf = (gdb_byte *) alloca (max_register_size (gdbarch));

#if 0
  fprintf_unfiltered (file, "nr_raw_registers %d\n",
diff --git a/gdb/remote.c b/gdb/remote.c
index 9247d43b094925ff397eb36b450eaba521adfc99..86856e6a6aba1967faaa8ef547f8a48fcc63c383 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -7752,9 +7752,10 @@ remote_fetch_registers (struct target_ops *ops,
static void
remote_prepare_to_store (struct target_ops *self, struct regcache *regcache)
{
+  struct gdbarch *gdbarch = get_regcache_arch (regcache);
  struct remote_arch_state *rsa = get_remote_arch_state ();
  int i;
-  gdb_byte buf[MAX_REGISTER_SIZE];
+  gdb_byte *buf = (gdb_byte *) alloca (max_register_size (gdbarch));

  /* Make sure the entire registers array is valid.  */
  switch (packet_support (PACKET_P))
