From a8c065f24ba53b5f37e0862526470043b3291e34 Mon Sep 17 00:00:00 2001
From: Kinsey Moore <kinsey.moore@oarcorp.com>
Date: Fri, 20 Jun 2025 12:07:09 -0500
Subject: [PATCH] gdb: Add support for TLS under RTEMS

This support uses the presence of the ".rtemsroset" section to detect
whether an ELF was created using RTEMS. This works for current RTEMS
development (7) branch, 6 branch, 5 branch, and 4.11 branch. This
registers the TLS callback for architectures under which RTEMS is known
to support TLS. The RTEMS GDB stub is only expected to support TLS in 6
branch and the current development branch and will only respond without
error to vGetTLSAddr request in those versions.
---
 gdb/Makefile.in      |  2 ++
 gdb/configure.tgt    |  2 ++
 gdb/rtems-tdep.c     | 74 ++++++++++++++++++++++++++++++++++++++++++++
 gdbsupport/osabi.def |  1 +
 4 files changed, 79 insertions(+)
 create mode 100644 gdb/rtems-tdep.c

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 6d627b61fe7..b507abc3568 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -874,6 +874,7 @@ ALL_TARGET_OBS = \
 	rs6000-aix-tdep.o \
 	rs6000-lynx178-tdep.o \
 	rs6000-tdep.o \
+	rtems-tdep.o \
 	rx-tdep.o \
 	s12z-tdep.o \
 	s390-linux-tdep.o \
@@ -1834,6 +1835,7 @@ ALLDEPFILES = \
 	rs6000-aix-nat.c \
 	rs6000-lynx178-tdep.c \
 	rs6000-tdep.c \
+	rtems-tdep.c \
 	rx-tdep.c \
 	s390-linux-nat.c \
 	s390-linux-tdep.c \
diff --git a/gdb/configure.tgt b/gdb/configure.tgt
index f7b9e32cba9..697e3df60f2 100644
--- a/gdb/configure.tgt
+++ b/gdb/configure.tgt
@@ -128,6 +128,8 @@ case "${targ}" in
 	os_obs="netbsd-tdep.o solib-svr4.o";;
 *-*-openbsd*)
 	os_obs="obsd-tdep.o solib-svr4.o";;
+*-rtems*)
+	os_obs="rtems-tdep.o solib-svr4.o";;
 esac
 
 # 3. Get the rest of objects.
diff --git a/gdb/rtems-tdep.c b/gdb/rtems-tdep.c
new file mode 100644
index 00000000000..9ec56fce447
--- /dev/null
+++ b/gdb/rtems-tdep.c
@@ -0,0 +1,74 @@
+/* Target-dependent code for RTEMS, architecture independent.
+
+   Copyright (C) 2025 On-Line Applications Research (OAR) Corporation
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include "arch-utils.h"
+#include "gdbarch-gen.h"
+#include "osabi.h"
+#include "solib-svr4.h"
+
+static enum gdb_osabi
+rtems_osabi_sniffer (bfd *abfd)
+{
+  for (asection *sect : gdb_bfd_sections (abfd)) {
+    if (strcmp(bfd_section_name (sect), ".rtemsroset") == 0) {
+      return GDB_OSABI_RTEMS;
+    }
+  }
+
+  return GDB_OSABI_UNKNOWN;
+}
+
+static void
+rtems_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
+{
+  set_gdbarch_fetch_tls_load_module_address (gdbarch,
+					     svr4_fetch_objfile_link_map);
+}
+
+static void register_osabi(enum bfd_architecture arch, unsigned long machine)
+{
+  const struct bfd_arch_info *arch_info = bfd_lookup_arch (arch, machine);
+
+  if (arch_info == NULL) {
+    return;
+  }
+
+  gdbarch_register_osabi_sniffer (arch, bfd_target_elf_flavour,
+				  rtems_osabi_sniffer);
+  gdbarch_register_osabi (arch, machine, GDB_OSABI_RTEMS,
+			  rtems_init_abi);
+}
+
+void _initialize_rtems_tdep ();
+void
+_initialize_rtems_tdep ()
+{
+  register_osabi (bfd_arch_arm, 0);
+  register_osabi (bfd_arch_aarch64, 0);
+  register_osabi (bfd_arch_i386, 0);
+  register_osabi (bfd_arch_m68k, 0);
+  register_osabi (bfd_arch_microblaze, 0);
+//  register_osabi (bfd_arch_mips, 0);
+//  register_osabi (bfd_arch_moxie, 0);
+//  register_osabi (bfd_arch_or1k, 0);
+  register_osabi (bfd_arch_powerpc, 0);
+  register_osabi (bfd_arch_riscv, 0);
+  register_osabi (bfd_arch_sparc, 0);
+  register_osabi (bfd_arch_i386, bfd_mach_x86_64);
+}
diff --git a/gdbsupport/osabi.def b/gdbsupport/osabi.def
index 637da26a050..e1c69fb1c40 100644
--- a/gdbsupport/osabi.def
+++ b/gdbsupport/osabi.def
@@ -53,5 +53,6 @@ GDB_OSABI_DEF (LYNXOS178, "LynxOS178", nullptr)
 GDB_OSABI_DEF (NEWLIB, "Newlib", nullptr)
 GDB_OSABI_DEF (SDE, "SDE", nullptr)
 GDB_OSABI_DEF (PIKEOS, "PikeOS", nullptr)
+GDB_OSABI_DEF (RTEMS, "RTEMS", ".*-rtems.*")
 
 GDB_OSABI_DEF_LAST (INVALID, "<invalid>", nullptr)
-- 
2.39.5

