From 4ef0072c22b8607a3af6a7dd564e633865b99ba2 Mon Sep 17 00:00:00 2001 From: Kinsey Moore Date: Tue, 14 Apr 2026 15:25:41 -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 self-hosted GDB server is only expected to support TLS in RTEMS 6 branch and the current development branch and will only respond without error to qGetTLSAddr request in those versions. Other GDB servers will respond with TLS information if available. --- gdb/Makefile.in | 2 + gdb/configure.tgt | 2 + gdb/rtems-tdep.c | 90 ++++++++++++++++++++++++++++++++++++++++++++ gdbsupport/osabi.def | 1 + 4 files changed, 95 insertions(+) create mode 100644 gdb/rtems-tdep.c diff --git a/gdb/Makefile.in b/gdb/Makefile.in index e38ba95eebd..b7a2c9c777d 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 \ @@ -1938,6 +1939,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 ba418653e86..29f0a03d648 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";; 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..3640f954ffc --- /dev/null +++ b/gdb/rtems-tdep.c @@ -0,0 +1,90 @@ +/* Target-dependent code for RTEMS, architecture independent. + + Copyright (C) 2026 Free Software Foundation, Inc. + + Contributed by 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 . */ + +#include "arch-utils.h" +#include "gdbarch-gen.h" +#include "osabi.h" + +/* Sniffer to detect RTEMS via section names. */ + +static enum gdb_osabi +rtems_osabi_sniffer (bfd *abfd) +{ + for (asection *sect : gdb_bfd_sections (abfd)) + { + if (streq (bfd_section_name (sect), ".rtemsroset")) + { + return GDB_OSABI_RTEMS; + } + } + + return GDB_OSABI_UNKNOWN; +} + +/* Configure TLS ABI for RTEMS. */ + +static CORE_ADDR +rtems_fetch_objfile_link_map (struct objfile *objfile) +{ + return 0; +} + +static void +rtems_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) +{ + set_gdbarch_fetch_tls_load_module_address (gdbarch, + rtems_fetch_objfile_link_map); +} + +/* Register OSABI components for RTEMS. */ + +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 == nullptr) + { + return; + } + + gdbarch_register_osabi_sniffer (arch, bfd_target_elf_flavour, + rtems_osabi_sniffer); + gdbarch_register_osabi (arch, machine, GDB_OSABI_RTEMS, rtems_init_abi); +} + +/* Initializer for RTEMS TLS support across all relevant architectures. */ + +INIT_GDB_FILE (rtems_tdep) +{ + /* mips, moxie, and or1k are supported by RTEMS, but omitted here since they + do not have TLS support in RTEMS. */ + + 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_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 230c21f0236..1b518887eff 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, "", nullptr) -- 2.47.3