From d211a1d38f4a7d16385668300301e2fc63a6e8c6 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/osabi.c       |  1 +
 gdb/osabi.h       |  1 +
 gdb/rtems-tdep.c  | 75 +++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 81 insertions(+)
 create mode 100644 gdb/rtems-tdep.c

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 84bc54b303e..abec819d277 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -872,6 +872,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 \
@@ -1826,6 +1827,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 47a674201f9..5d40f2b89bf 100644
--- a/gdb/configure.tgt
+++ b/gdb/configure.tgt
@@ -127,6 +127,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/osabi.c b/gdb/osabi.c
index d494d899623..cb9bce1102c 100644
--- a/gdb/osabi.c
+++ b/gdb/osabi.c
@@ -81,6 +81,7 @@ static const struct osabi_names gdb_osabi_names[] =
   { "Newlib", NULL },
   { "SDE", NULL },
   { "PikeOS", NULL },
+  { "RTEMS", ".*-rtems.*" },
 
   { "<invalid>", NULL }
 };
diff --git a/gdb/osabi.h b/gdb/osabi.h
index c1a85d1b9a3..1e047c62d51 100644
--- a/gdb/osabi.h
+++ b/gdb/osabi.h
@@ -46,6 +46,7 @@ enum gdb_osabi
   GDB_OSABI_NEWLIB,
   GDB_OSABI_SDE,
   GDB_OSABI_PIKEOS,
+  GDB_OSABI_RTEMS,
 
   GDB_OSABI_INVALID		/* keep this last */
 };
diff --git a/gdb/rtems-tdep.c b/gdb/rtems-tdep.c
new file mode 100644
index 00000000000..3c1f5b29e10
--- /dev/null
+++ b/gdb/rtems-tdep.c
@@ -0,0 +1,75 @@
+/* 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_nios2, 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);
+}
-- 
2.39.5

