[PATCH] Mask x86 segment registers in Windows gdbserver

Tom Tromey tromey@adacore.com
Tue Dec 19 14:48:23 GMT 2023


A test internal to AdaCore prints the segment registers.  When run
using gdbserver, it shows:

    (gdb) print /x $gs
    $6 = 0x2b0000

However, the segment registers are only 16 bits -- so this has some
invalid bits.

gdb's windows-nat.c has long had a fix for this problem.  This patch
applies the fix to gdbserver as well.
---
 gdbserver/win32-i386-low.cc | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/gdbserver/win32-i386-low.cc b/gdbserver/win32-i386-low.cc
index f78e0120678..d60ff83c15d 100644
--- a/gdbserver/win32-i386-low.cc
+++ b/gdbserver/win32-i386-low.cc
@@ -35,6 +35,12 @@ using namespace windows_nat;
 #define FCS_REGNUM 27
 #define FOP_REGNUM 31
 
+#define I386_CS_REGNUM 10
+#define I386_GS_REGNUM 15
+
+#define AMD64_CS_REGNUM 18
+#define AMD64_GS_REGNUM 23
+
 #define FLAG_TRACE_BIT 0x100
 
 static struct x86_debug_reg_state debug_reg_state;
@@ -459,6 +465,18 @@ static const int amd64_mappings[] =
 
 #endif /* __x86_64__ */
 
+/* Return true if R is a segment register.  */
+static bool
+is_segment_register (int r)
+{
+#ifdef __x86_64__
+  if (!windows_process.wow64_process)
+    return r >= AMD64_CS_REGNUM && r <= AMD64_GS_REGNUM;
+  else
+#endif
+    return r >= I386_CS_REGNUM && r <= I386_GS_REGNUM;
+}
+
 /* Fetch register from gdbserver regcache data.  */
 static void
 i386_fetch_inferior_register (struct regcache *regcache,
@@ -491,6 +509,14 @@ i386_fetch_inferior_register (struct regcache *regcache,
       l = (*((long *) context_offset) >> 16) & ((1 << 11) - 1);
       supply_register (regcache, r, (char *) &l);
     }
+  else if (is_segment_register (r))
+    {
+      /* GDB treats segment registers as 32bit registers, but they are
+	 in fact only 16 bits long.  Make sure we do not read extra
+	 bits from our source buffer.  */
+      l = *((long *) context_offset) & 0xffff;
+      supply_register (regcache, r, (char *) &l);
+    }
   else
     supply_register (regcache, r, context_offset);
 }
-- 
2.43.0



More information about the Gdb-patches mailing list