Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Hannes Domani <ssbssa@yahoo.de>
To: gdb-patches@sourceware.org
Subject: [RFC PATCH] Implement native TLS support on Windows
Date: Fri,  8 May 2026 16:42:28 +0200	[thread overview]
Message-ID: <20260508144232.940078-1-ssbssa@yahoo.de> (raw)
In-Reply-To: <20260508144232.940078-1-ssbssa.ref@yahoo.de>

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


       reply	other threads:[~2026-05-08 14:43 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20260508144232.940078-1-ssbssa.ref@yahoo.de>
2026-05-08 14:42 ` Hannes Domani [this message]
2026-05-08 14:56   ` Eli Zaretskii
2026-05-08 19:09   ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260508144232.940078-1-ssbssa@yahoo.de \
    --to=ssbssa@yahoo.de \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox