From: Hui Li <lihui@loongson.cn>
To: gdb-patches@sourceware.org
Cc: Tiezhu Yang <yangtiezhu@loongson.cn>
Subject: [PATCH] gdb: LoongArch: Add internal TLS support
Date: Mon, 13 Jul 2026 18:37:24 +0800 [thread overview]
Message-ID: <20260713103724.7713-1-lihui@loongson.cn> (raw)
Implements the LoongArch-specific get_tls_dtv_addr() and uses
svr4_tls_register_tls_methods() to register it, together with
svr4_tls_get_thread_local_address as the get_thread_local_address
gdbarch method, to support internal TLS address lookup.
LoongArch doesn't need get_tls_dtp_offset since musl's DTP_OFFSET
is defined as 0.
This is the LoongArch version of commit c34309bea22
("Internal TLS support for aarch64, x86_64, riscv, ppc64, and s390x").
Depends-On: bfc3ac0d13b
("LoongArch: Support R_LARCH_TLS_DTPREL32/64 used in debug section")
Before:
make check-gdb TESTS="gdb.base/tls-nothreads.exp"
=== gdb Summary ===
# of expected passes 101
# of known failures 48
make check-gdb TESTS="gdb.base/tls-dlobj.exp"
=== gdb Summary ===
# of expected passes 261
After:
make check-gdb TESTS="gdb.base/tls-nothreads.exp"
=== gdb Summary ===
# of expected passes 302
make check-gdb TESTS="gdb.base/tls-dlobj.exp"
=== gdb Summary ===
# of expected passes 523
Signed-off-by: Hui Li <lihui@loongson.cn>
---
gdb/arch/loongarch.h | 1 +
gdb/configure.tgt | 2 +-
gdb/loongarch-linux-tdep.c | 54 +++++++++++++++++++++++
gdb/testsuite/gdb.base/tls-common.exp.tcl | 2 +-
4 files changed, 57 insertions(+), 2 deletions(-)
diff --git a/gdb/arch/loongarch.h b/gdb/arch/loongarch.h
index ee9d378afd6..ec3d0d195c9 100644
--- a/gdb/arch/loongarch.h
+++ b/gdb/arch/loongarch.h
@@ -26,6 +26,7 @@
enum loongarch_regnum
{
LOONGARCH_RA_REGNUM = 1, /* Return Address. */
+ LOONGARCH_TP_REGNUM = 2, /* Thread Pointer. */
LOONGARCH_SP_REGNUM = 3, /* Stack Pointer. */
LOONGARCH_A0_REGNUM = 4, /* First Argument/Return Value. */
LOONGARCH_A7_REGNUM = 11, /* Seventh Argument/Syscall Number. */
diff --git a/gdb/configure.tgt b/gdb/configure.tgt
index 362de60065c..7313b31e42a 100644
--- a/gdb/configure.tgt
+++ b/gdb/configure.tgt
@@ -374,7 +374,7 @@ loongarch*-*-linux*)
# Target: LoongArch running Linux
gdb_target_obs="loongarch-linux-tdep.o glibc-tdep.o \
linux-tdep.o solib-svr4.o solib-svr4-linux.o \
- linux-record.o"
+ linux-record.o svr4-tls-tdep.o"
;;
m32c-*-*)
diff --git a/gdb/loongarch-linux-tdep.c b/gdb/loongarch-linux-tdep.c
index bab84fd126a..9ccb26914a7 100644
--- a/gdb/loongarch-linux-tdep.c
+++ b/gdb/loongarch-linux-tdep.c
@@ -26,6 +26,7 @@
#include "linux-record.h"
#include "linux-tdep.h"
#include "solib-svr4-linux.h"
+#include "svr4-tls-tdep.h"
#include "loongarch-tdep.h"
#include "record-full.h"
#include "regset.h"
@@ -1137,6 +1138,57 @@ init_loongarch_linux_record_tdep (struct gdbarch *gdbarch)
loongarch_linux_record_tdep.arg7 = LOONGARCH_A0_REGNUM + 6;
}
+/* Fetch and return the TLS DTV (dynamic thread vector) address for PTID.
+ Throw a suitable TLS error if something goes wrong. */
+
+static CORE_ADDR
+loongarch_linux_get_tls_dtv_addr (struct gdbarch *gdbarch, ptid_t ptid,
+ svr4_tls_libc libc)
+{
+ /* On LoongArch, the thread pointer is found in TP. */
+ regcache *regcache
+ = get_thread_arch_regcache (current_inferior (), ptid, gdbarch);
+ target_fetch_registers (regcache, LOONGARCH_TP_REGNUM);
+ ULONGEST thr_ptr;
+ if (regcache->cooked_read (LOONGARCH_TP_REGNUM, &thr_ptr) != REG_VALID)
+ throw_error (TLS_GENERIC_ERROR, _("Unable to fetch thread pointer"));
+
+ CORE_ADDR dtv_ptr_addr;
+ switch (libc)
+ {
+ case svr4_tls_libc_musl:
+ /* MUSL: The DTV pointer is found at the very end of the pthread
+ struct which is located *before* the thread pointer. I.e.
+ the thread pointer will be just beyond the end of the struct,
+ so the address of the DTV pointer is found one pointer-size
+ before the thread pointer. */
+ dtv_ptr_addr = thr_ptr - (gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT);
+ break;
+ case svr4_tls_libc_glibc:
+ /* GLIBC: The thread pointer (TP) points just beyond the end of
+ the TCB (thread control block). On LoongArch, this struct
+ (tcbhead_t) is defined to contain two pointers. The first is
+ a pointer to the DTV and the second is a pointer to private
+ data. So the DTV pointer address is found two pointer-size
+ before thread pointer. */
+
+ dtv_ptr_addr = thr_ptr - 2 * (gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT);
+ break;
+ default:
+ throw_error (TLS_GENERIC_ERROR, _("Unknown LoongArch C library"));
+ break;
+ }
+
+ gdb::byte_vector buf (gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT);
+ if (target_read_memory (dtv_ptr_addr, buf.data (), buf.size ()) != 0)
+ throw_error (TLS_GENERIC_ERROR, _("Unable to fetch DTV address"));
+
+ const struct builtin_type *builtin = builtin_type (gdbarch);
+ CORE_ADDR dtv_addr = gdbarch_pointer_to_address
+ (gdbarch, builtin->builtin_data_ptr, buf.data ());
+ return dtv_addr;
+}
+
/* Initialize LoongArch Linux ABI info. */
static void
@@ -1158,6 +1210,8 @@ loongarch_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
/* Enable TLS support. */
set_gdbarch_fetch_tls_load_module_address (gdbarch, svr4_fetch_objfile_link_map);
+ set_gdbarch_get_thread_local_address (gdbarch, svr4_tls_get_thread_local_address);
+ svr4_tls_register_tls_methods (info, gdbarch, loongarch_linux_get_tls_dtv_addr);
/* Prepend tramp frame unwinder for signal. */
tramp_frame_prepend_unwinder (gdbarch, &loongarch_linux_rt_sigframe);
diff --git a/gdb/testsuite/gdb.base/tls-common.exp.tcl b/gdb/testsuite/gdb.base/tls-common.exp.tcl
index 06cab649519..53ed1ca4d8a 100644
--- a/gdb/testsuite/gdb.base/tls-common.exp.tcl
+++ b/gdb/testsuite/gdb.base/tls-common.exp.tcl
@@ -26,7 +26,7 @@ require {is_any_target "*-*-linux*"}
set internal_tls_linux_targets {"x86_64-*-linux*" "aarch64-*-linux*"
"riscv*-*-linux*" "powerpc64*-*-linux*"
- "s390x*-*-linux*"}
+ "s390x*-*-linux*" "loongarch*-*-linux*"}
# The "maint set force-internal-tls-address-lookup" command is only
# available for certain Linux architectures. Don't attempt to force
--
2.48.1
next reply other threads:[~2026-07-13 10:38 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-13 10:37 Hui Li [this message]
2026-07-13 12:20 ` Simon Marchi
2026-07-18 11:35 ` Tiezhu Yang
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=20260713103724.7713-1-lihui@loongson.cn \
--to=lihui@loongson.cn \
--cc=gdb-patches@sourceware.org \
--cc=yangtiezhu@loongson.cn \
/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