[04/17,gdb/aarch64] sve: Fix return command when using V registers in a SVE-enabled target

Message ID 20230411042658.1852730-5-luis.machado@arm.com
State New
Headers
Series SME support for AArch64 gdb/gdbserver on Linux. |

Commit Message

Luis Machado April 11, 2023, 4:26 a.m. UTC
  In a target without SVE support, the V registers have a size of 16 bytes,
otherwise they may have a size bigger than 16 bytes (depending on the current
vector length for the Z registers, as they overlap the V registers).

In aarch64-tdep.c:aarch64_store_return_value, the code is laid
out in a way that allocates the buffer with the size of the register, but
only updates the amount of bytes for the particular type we're returning.

This may cause a situation where we have a register size of 32 bytes but
are returning a floating point value of 8 bytes.  The temporary buffer
will therefore have 32 bytes, but we'll only update 8 bytes of it.

When we write the entire register back, it will have potentially 24 bytes
of garbage in it.

Fix this by first reading the original contents of the register and then
overriding only the bytes that we need for the return value.

Tested on aarch64-linux Ubuntu 22.04/20.04.
---
 gdb/aarch64-tdep.c | 5 +++++
 1 file changed, 5 insertions(+)
  

Patch

diff --git a/gdb/aarch64-tdep.c b/gdb/aarch64-tdep.c
index d8349e4ccdb..ea0d8ce2e27 100644
--- a/gdb/aarch64-tdep.c
+++ b/gdb/aarch64-tdep.c
@@ -2411,6 +2411,11 @@  aarch64_store_return_value (struct type *type, struct regcache *regs,
 	    ("write HFA or HVA return value element %d to %s",
 	     i + 1, gdbarch_register_name (gdbarch, regno));
 
+	  /* Depending on whether the target supports SVE or not, the V
+	     registers may report a size > 16 bytes.  In that case, read the
+	     original contents of the register before overriding it with a new
+	     value that has a potential size <= 16 bytes.  */
+	  regs->cooked_read (regno, tmpbuf);
 	  memcpy (tmpbuf, valbuf,
 		  len > V_REGISTER_SIZE ? V_REGISTER_SIZE : len);
 	  regs->cooked_write (regno, tmpbuf);