diff --git a/gdb/gmp-utils.c b/gdb/gmp-utils.c
index 57f5f9766b9..0afa344781b 100644
--- a/gdb/gmp-utils.c
+++ b/gdb/gmp-utils.c
@@ -66,18 +66,8 @@ gdb_mpz::read (gdb::array_view<const gdb_byte> buf, enum bfd_endian byte_order,
 /* See gmp-utils.h.  */
 
 void
-gdb_mpz::write (gdb::array_view<gdb_byte> buf, enum bfd_endian byte_order,
-		bool unsigned_p) const
-{
-  this->safe_export
-    (buf, byte_order == BFD_ENDIAN_BIG ? 1 : -1 /* endian */, unsigned_p);
-}
-
-/* See gmp-utils.h.  */
-
-void
-gdb_mpz::safe_export (gdb::array_view<gdb_byte> buf,
-		      int endian, bool unsigned_p) const
+gdb_mpz::export_bits (gdb::array_view<gdb_byte> buf, int endian, bool unsigned_p,
+		      bool safe) const
 {
   gdb_assert (buf.size () > 0);
 
@@ -92,36 +82,39 @@ gdb_mpz::safe_export (gdb::array_view<gdb_byte> buf,
       return;
     }
 
-  /* Determine the maximum range of values that our buffer can hold,
-     and verify that VAL is within that range.  */
-
-  gdb_mpz lo, hi;
-  const size_t max_usable_bits = buf.size () * HOST_CHAR_BIT;
-  if (unsigned_p)
-    {
-      lo = 0;
-
-      mpz_ui_pow_ui (hi.m_val, 2, max_usable_bits);
-      mpz_sub_ui (hi.m_val, hi.m_val, 1);
-    }
-  else
+  if (safe)
     {
-      mpz_ui_pow_ui (lo.m_val, 2, max_usable_bits - 1);
-      mpz_neg (lo.m_val, lo.m_val);
-
-      mpz_ui_pow_ui (hi.m_val, 2, max_usable_bits - 1);
-      mpz_sub_ui (hi.m_val, hi.m_val, 1);
+      /* Determine the maximum range of values that our buffer can
+	 hold, and verify that VAL is within that range.  */
+
+      gdb_mpz lo, hi;
+      const size_t max_usable_bits = buf.size () * HOST_CHAR_BIT;
+      if (unsigned_p)
+	{
+	  lo = 0;
+
+	  mpz_ui_pow_ui (hi.m_val, 2, max_usable_bits);
+	  mpz_sub_ui (hi.m_val, hi.m_val, 1);
+	}
+      else
+	{
+	  mpz_ui_pow_ui (lo.m_val, 2, max_usable_bits - 1);
+	  mpz_neg (lo.m_val, lo.m_val);
+
+	  mpz_ui_pow_ui (hi.m_val, 2, max_usable_bits - 1);
+	  mpz_sub_ui (hi.m_val, hi.m_val, 1);
+	}
+
+      if (mpz_cmp (m_val, lo.m_val) < 0 || mpz_cmp (m_val, hi.m_val) > 0)
+	error (_("Cannot export value %s as %zu-bits %s integer"
+		 " (must be between %s and %s)"),
+	       this->str ().c_str (),
+	       max_usable_bits,
+	       unsigned_p ? _("unsigned") : _("signed"),
+	       lo.str ().c_str (),
+	       hi.str ().c_str ());
     }
 
-  if (mpz_cmp (m_val, lo.m_val) < 0 || mpz_cmp (m_val, hi.m_val) > 0)
-    error (_("Cannot export value %s as %zu-bits %s integer"
-	     " (must be between %s and %s)"),
-	   this->str ().c_str (),
-	   max_usable_bits,
-	   unsigned_p ? _("unsigned") : _("signed"),
-	   lo.str ().c_str (),
-	   hi.str ().c_str ());
-
   const gdb_mpz *exported_val = this;
   gdb_mpz un_signed;
   if (sign < 0)
@@ -134,6 +127,28 @@ gdb_mpz::safe_export (gdb::array_view<gdb_byte> buf,
       exported_val = &un_signed;
     }
 
+  /* If the value is too large, truncate it.  */
+  if (!safe
+      && mpz_sizeinbase (exported_val->m_val, 2) > buf.size () * HOST_CHAR_BIT)
+    {
+      /* If we don't already have a copy, make it now.  */
+      if (exported_val != &un_signed)
+	{
+	  un_signed = *exported_val;
+	  exported_val = &un_signed;
+	}
+
+      un_signed.mask (buf.size () * HOST_CHAR_BIT);
+    }
+
+  /* It's possible that one of the above results in zero, which has to
+     be handled specially.  */
+  if (exported_val->sgn () == 0)
+    {
+      memset (buf.data (), 0, buf.size ());
+      return;
+    }
+
   /* Do the export into a buffer allocated by GMP itself; that way,
      we can detect cases where BUF is not large enough to export
      our value, and thus avoid a buffer overlow.  Normally, this should
diff --git a/gdb/gmp-utils.h b/gdb/gmp-utils.h
index d4e5015e345..d378499d287 100644
--- a/gdb/gmp-utils.h
+++ b/gdb/gmp-utils.h
@@ -137,7 +137,20 @@ struct gdb_mpz
 
      UNSIGNED_P indicates whether the number has an unsigned type.  */
   void write (gdb::array_view<gdb_byte> buf, enum bfd_endian byte_order,
-	      bool unsigned_p) const;
+	      bool unsigned_p) const
+  {
+    export_bits (buf, byte_order == BFD_ENDIAN_BIG ? 1 : -1 /* endian */,
+		 unsigned_p, true /* safe */);
+  }
+
+  /* Like write, but truncates the value to the desired number of
+     bytes.  */
+  void truncate (gdb::array_view<gdb_byte> buf, enum bfd_endian byte_order,
+		 bool unsigned_p) const
+  {
+    export_bits (buf, byte_order == BFD_ENDIAN_BIG ? 1 : -1 /* endian */,
+		 unsigned_p, false /* safe */);
+  }
 
   /* Return a string containing VAL.  */
   std::string str () const { return gmp_string_printf ("%Zd", m_val); }
@@ -337,10 +350,11 @@ struct gdb_mpz
 	   . -1 for least significant byte first; or
 	   . 0 for native endianness.
 
-    An error is raised if BUF is not large enough to contain the value
-    being exported.  */
-  void safe_export (gdb::array_view<gdb_byte> buf,
-		    int endian, bool unsigned_p) const;
+    If SAFE is true, an error is raised if BUF is not large enough to
+    contain the value being exported.  If SAFE is false, the value is
+    truncated to fit in BUF.  */
+  void export_bits (gdb::array_view<gdb_byte> buf, int endian, bool unsigned_p,
+		    bool safe) const;
 
   friend struct gdb_mpq;
   friend struct gdb_mpf;
@@ -590,9 +604,10 @@ gdb_mpz::as_integer () const
 {
   T result;
 
-  this->safe_export ({(gdb_byte *) &result, sizeof (result)},
+  this->export_bits ({(gdb_byte *) &result, sizeof (result)},
 		     0 /* endian (0 = native) */,
-		     !std::is_signed<T>::value /* unsigned_p */);
+		     !std::is_signed<T>::value /* unsigned_p */,
+		     true /* safe */);
 
   return result;
 }
