* [PATCH v1] Add TLS variable debug support for AIX [ 64-bit XCOFF files only]
@ 2026-07-29 9:33 Aditya Vidyadhar Kamath
2026-07-29 13:16 ` Ulrich Weigand
0 siblings, 1 reply; 2+ messages in thread
From: Aditya Vidyadhar Kamath @ 2026-07-29 9:33 UTC (permalink / raw)
To: ulrich.weigand, simon.marchi, tom
Cc: gdb-patches, Aditya.Kamath1, sangamesh.swamy, PRAJWAL.B.MEHENDARKAR
From: Aditya Kamath <Aditya.Kamath1@ibm.com>
This is a patch that adds TLS debug support for variables in AIX
for 64 bit XCOFF binaries only.
Currently in AIX, if we debug a TLS variable we get,
(gdb) print tls_counter
Cannot find thread-local variables on this target
This patch uses thread pointer in R13 plus adds the offset to get the TLS variable address.
Also adds the AIX specific gdbarch () methods.
After this patch we get,
gdb/gdb thread-local
GNU gdb (GDB) 18.0.50.20260728-git
Copyright (C) 2026 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "powerpc64-ibm-aix7.2.0.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
+------------------------------------------------------------------------------+
| Find the GDB manual online at: |
| http://www.gnu.org/software/gdb/documentation/. |
| For help, type "help". |
| Type "apropos word" to search for commands related to "word". |
+------------------------------------------------------------------------------+
Reading symbols from thread-local...
(gdb) b 16
Breakpoint 1 at 0x10000894: file thread-local.c, line 16.
(gdb) r
Starting program: /home/aditya/binutils-gdb/thread-local
[New Thread 258 (tid 118292825) (id 2)]
[Switching to thread 2 (Thread 258 (tid 118292825))]
Thread 2 hit Breakpoint 1, thread_function (arg=0x1) at thread-local.c:16
16 sleep(1);
(gdb) p tls_counter
$1 = 100
(gdb) q
---
gdb/rs6000-aix-tdep.c | 56 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 56 insertions(+)
diff --git a/gdb/rs6000-aix-tdep.c b/gdb/rs6000-aix-tdep.c
index d823fea5645..34af3dc0a7c 100644
--- a/gdb/rs6000-aix-tdep.c
+++ b/gdb/rs6000-aix-tdep.c
@@ -39,6 +39,7 @@
#include "gdbsupport/xml-utils.h"
#include "trad-frame.h"
#include "frame-unwind.h"
+#include "inferior.h"
/* If the kernel has to deliver a signal, it pushes a sigcontext
structure on the stack and then calls the signal handler, passing
@@ -1355,6 +1356,53 @@ rs6000_aix_core_xfer_shared_libraries_aix (struct gdbarch *gdbarch,
offset, len, 0);
}
+/* For AIX, use the rs6000_aix_fetch_tls_load_module_address gdb_arch method.
+ TLS variable addresses on AIX using the Local Exec TLS architecture are
+ calculated directly from the thread pointer (R13) plus the signed
+ TP-relative offset of the symbol. In this function we just return 0 and
+ all calculations are done in rs6000_aix_get_thread_local_address (). */
+
+static CORE_ADDR
+rs6000_aix_fetch_tls_load_module_address (struct objfile *objfile)
+{
+ return 0;
+}
+
+/* Use the AIX get_thread_local_address gdbarch function.
+
+ The thread pointer in AIX (64-bit) is located at R13.
+ The value of the XCOFF symbol is a signed offset from the thread
+ pointer for the Local Exec TLS model, which is the only model that
+ the AIX toolchain presently emits for executables. The TLS variable's
+ per-thread address is as follows since GDB provides that raw symbol value as OFFSET:
+
+ address = (int64_t) offset + thread_pointer */
+
+static CORE_ADDR
+rs6000_aix_get_thread_local_address (struct gdbarch *gdbarch, ptid_t ptid,
+ CORE_ADDR lm_addr, CORE_ADDR offset)
+{
+ ppc_gdbarch_tdep *tdep = gdbarch_tdep<ppc_gdbarch_tdep> (gdbarch);
+
+ /* 64-bit AIX: the thread pointer is in R13. */
+ if (tdep->wordsize != 8)
+ throw_error (TLS_GENERIC_ERROR,
+ _("TLS lookup not yet supported for 32-bit AIX"));
+
+ int tp_regnum = PPC_R0_REGNUM + 13;
+
+ regcache *regcache
+ = get_thread_arch_regcache (current_inferior (), ptid, gdbarch);
+ target_fetch_registers (regcache, tp_regnum);
+
+ ULONGEST tp;
+ if (regcache->cooked_read (tp_regnum, &tp) != REG_VALID)
+ throw_error (TLS_GENERIC_ERROR,
+ _("Unable to fetch thread pointer for TLS lookup"));
+
+ return tp + (CORE_ADDR)(int64_t) offset;
+}
+
static void
rs6000_aix_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
{
@@ -1411,6 +1459,14 @@ rs6000_aix_init_osabi (struct gdbarch_info info, struct gdbarch *gdbarch)
set_gdbarch_auto_wide_charset (gdbarch, rs6000_aix_auto_wide_charset);
set_gdbarch_make_solib_ops (gdbarch, make_aix_solib_ops);
+
+
+ /* TLS support for AIX. */
+ set_gdbarch_fetch_tls_load_module_address
+ (gdbarch, rs6000_aix_fetch_tls_load_module_address);
+ set_gdbarch_get_thread_local_address
+ (gdbarch, rs6000_aix_get_thread_local_address);
+
frame_unwind_append_unwinder (gdbarch, &aix_sighandle_frame_unwind);
}
--
2.51.2
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH v1] Add TLS variable debug support for AIX [ 64-bit XCOFF files only]
2026-07-29 9:33 [PATCH v1] Add TLS variable debug support for AIX [ 64-bit XCOFF files only] Aditya Vidyadhar Kamath
@ 2026-07-29 13:16 ` Ulrich Weigand
0 siblings, 0 replies; 2+ messages in thread
From: Ulrich Weigand @ 2026-07-29 13:16 UTC (permalink / raw)
To: akamath996, tom, simon.marchi
Cc: gdb-patches, SANGAMESH MALLAYYA, PRAJWAL B MEHENDARKAR, Aditya Kamath
Aditya Vidyadhar Kamath <akamath996@gmail.com> wrote:
>+ The thread pointer in AIX (64-bit) is located at R13.
>+ The value of the XCOFF symbol is a signed offset from the thread
>+ pointer for the Local Exec TLS model, which is the only model that
>+ the AIX toolchain presently emits for executables. The TLS
variable's
>+ per-thread address is as follows since GDB provides that raw
symbol value as OFFSET:
>+
>+ address = (int64_t) offset + thread_pointer */
This doesn't look right to me. The issue is not so much the
"TLS model", which is mostly a matter of optimization. The
issue is more fundamental: thread-local variables defined in
libraries loaded with dlopen *cannot* be accessed directly
relative to the thread pointer, because the storage for
those is only allocated at dlopen time and not statically
known at compile or link time.
It is of course true that thread-local variables defined in
the main executable (or in libraries pulled in at startup)
can be accessed this way. But your code doesn't even attempt
to verify that the variable in question *is* one of those,
so it will simply access random memory if used on a variable
in a dlopen'ed library.
Ideally, you should be able to implement full support (by
accessing the AIX libc equivalent of the DTV). If that is
not possible for some reason, then you should at least
restrict the implementation to variables guaranteed to
work (e.g. by checking whether the objfile passsed to
fetch_tls_load_module_address corresponds to the main
executable or an initially loaded library).
Bye,
Ulrich
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-07-29 13:16 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-07-29 9:33 [PATCH v1] Add TLS variable debug support for AIX [ 64-bit XCOFF files only] Aditya Vidyadhar Kamath
2026-07-29 13:16 ` Ulrich Weigand
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox