Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [RFC PATCH] Implement native TLS support on Windows
       [not found] <20260508144232.940078-1-ssbssa.ref@yahoo.de>
@ 2026-05-08 14:42 ` Hannes Domani
  2026-05-08 14:56   ` Eli Zaretskii
  2026-05-08 19:09   ` Tom Tromey
  0 siblings, 2 replies; 3+ messages in thread
From: Hannes Domani @ 2026-05-08 14:42 UTC (permalink / raw)
  To: gdb-patches

GCC 16 introduced native TLS variables on Windows, so this adds
debugger support for them.

I've used the fetch_tls_load_module_address gdbarch method to get the
address of _tls_index of the OBJFILE, which is then forwarded as LM_ADDR
to windows_get_thread_local_address, but I'm not really sure if this is
allowed.
---
 gdb/windows-tdep.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)

diff --git a/gdb/windows-tdep.c b/gdb/windows-tdep.c
index 38ee1d7275e..0ff8c5f580a 100644
--- a/gdb/windows-tdep.c
+++ b/gdb/windows-tdep.c
@@ -954,6 +954,58 @@ windows_init_abi_common (struct gdbarch_info info, struct gdbarch *gdbarch)
   set_gdbarch_get_siginfo_type (gdbarch, windows_get_siginfo_type);
 }
 
+/* Get the address of variable "_tls_index" of OBJFILE.  */
+
+static CORE_ADDR
+windows_tls_index_address (struct objfile *objfile)
+{
+  bound_minimal_symbol minsym
+    = lookup_minimal_symbol_linkage("_tls_index", objfile, false);
+  if (minsym.minsym == nullptr)
+    throw_error (TLS_GENERIC_ERROR, _("Cannot find address of _tls_index"));
+
+  return minsym.value_address ();
+}
+
+/* Implement the get_thread_local_address gdbarch method.  */
+
+static CORE_ADDR
+windows_get_thread_local_address (struct gdbarch *gdbarch, ptid_t ptid,
+				  CORE_ADDR lm_addr, CORE_ADDR offset)
+{
+  int ptr_bytes;
+  int tls_offset;
+  if (gdbarch_ptr_bit (gdbarch) == 32)
+    {
+      ptr_bytes = 4;
+      tls_offset = 44;
+    }
+  else
+    {
+      ptr_bytes = 8;
+      tls_offset = 88;
+    }
+
+  gdb_byte buf[8];
+  if (target_read_memory (lm_addr, buf, 4))
+    throw_error (TLS_GENERIC_ERROR, _("Cannot read _tls_index"));
+
+  enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
+  uint32_t tls_index = extract_unsigned_integer (buf, 4, byte_order);
+  CORE_ADDR tlb, tls_ptr, slot_ptr;
+  if (target_get_tib_address (ptid, &tlb)
+      && !target_read_memory (tlb + tls_offset, buf, ptr_bytes)
+      && (tls_ptr = extract_unsigned_integer (buf, ptr_bytes,
+					      byte_order)) != 0
+      && !target_read_memory (tls_ptr + tls_index * ptr_bytes,
+			      buf, ptr_bytes)
+      && (slot_ptr = extract_unsigned_integer (buf, ptr_bytes,
+					       byte_order)) != 0)
+    return slot_ptr + offset;
+
+  throw_error (TLS_NOT_ALLOCATED_YET_ERROR, _("TLS not allocated yet"));
+}
+
 /* See windows-tdep.h.  */
 void
 windows_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
@@ -963,6 +1015,12 @@ windows_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
 
   /* On Windows, "long"s are only 32bit.  */
   set_gdbarch_long_bit (gdbarch, 32);
+
+  /* Enable TLS support.  */
+  set_gdbarch_fetch_tls_load_module_address (gdbarch,
+					     windows_tls_index_address);
+  set_gdbarch_get_thread_local_address (gdbarch,
+					windows_get_thread_local_address);
 }
 
 /* See windows-tdep.h.  */
-- 
2.54.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [RFC PATCH] Implement native TLS support on Windows
  2026-05-08 14:42 ` [RFC PATCH] Implement native TLS support on Windows Hannes Domani
@ 2026-05-08 14:56   ` Eli Zaretskii
  2026-05-08 19:09   ` Tom Tromey
  1 sibling, 0 replies; 3+ messages in thread
From: Eli Zaretskii @ 2026-05-08 14:56 UTC (permalink / raw)
  To: Hannes Domani; +Cc: gdb-patches

> From: Hannes Domani <ssbssa@yahoo.de>
> Date: Fri,  8 May 2026 16:42:28 +0200
> 
> GCC 16 introduced native TLS variables on Windows, so this adds
> debugger support for them.

Thanks.  Should this be called out in NEWS?

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [RFC PATCH] Implement native TLS support on Windows
  2026-05-08 14:42 ` [RFC PATCH] Implement native TLS support on Windows Hannes Domani
  2026-05-08 14:56   ` Eli Zaretskii
@ 2026-05-08 19:09   ` Tom Tromey
  1 sibling, 0 replies; 3+ messages in thread
From: Tom Tromey @ 2026-05-08 19:09 UTC (permalink / raw)
  To: Hannes Domani; +Cc: gdb-patches

>>>>> "Hannes" == Hannes Domani <ssbssa@yahoo.de> writes:

Hannes> I've used the fetch_tls_load_module_address gdbarch method to get the
Hannes> address of _tls_index of the OBJFILE, which is then forwarded as LM_ADDR
Hannes> to windows_get_thread_local_address, but I'm not really sure if this is
Hannes> allowed.

I think it is fine, or at most would need a small update to the comment
string explaining that the values are opaque and intended to be
target-specific.

Hannes> +/* Get the address of variable "_tls_index" of OBJFILE.  */
Hannes> +
Hannes> +static CORE_ADDR
Hannes> +windows_tls_index_address (struct objfile *objfile)
Hannes> +{
Hannes> +  bound_minimal_symbol minsym
Hannes> +    = lookup_minimal_symbol_linkage("_tls_index", objfile, false);

Missing space.

Hannes> +  gdb_byte buf[8];
Hannes> +  if (target_read_memory (lm_addr, buf, 4))
Hannes> +    throw_error (TLS_GENERIC_ERROR, _("Cannot read _tls_index"));
Hannes> +
Hannes> +  enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
Hannes> +  uint32_t tls_index = extract_unsigned_integer (buf, 4, byte_order);
Hannes> +  CORE_ADDR tlb, tls_ptr, slot_ptr;
Hannes> +  if (target_get_tib_address (ptid, &tlb)
Hannes> +      && !target_read_memory (tlb + tls_offset, buf, ptr_bytes)
Hannes> +      && (tls_ptr = extract_unsigned_integer (buf, ptr_bytes,
Hannes> +					      byte_order)) != 0
Hannes> +      && !target_read_memory (tls_ptr + tls_index * ptr_bytes,
Hannes> +			      buf, ptr_bytes)
Hannes> +      && (slot_ptr = extract_unsigned_integer (buf, ptr_bytes,
Hannes> +					       byte_order)) != 0)

Instead of multiple assignments in an 'if' I think it'd be better to
have separate 'if's and early returns.

Tom

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-05-08 19:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20260508144232.940078-1-ssbssa.ref@yahoo.de>
2026-05-08 14:42 ` [RFC PATCH] Implement native TLS support on Windows Hannes Domani
2026-05-08 14:56   ` Eli Zaretskii
2026-05-08 19:09   ` Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox