[PATCH] libgloss: arm: Add hardfault handler for M-profile

Christophe Lyon <[email protected]> Fri, 12 Jun 2026 17:53:06 +0200
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
After reaching a hardfault, the simulator enters an infinite loop
unless we map the handler to 'abort'.

The patch adds this minimal setup for M-profile in
libgloss/arm/cpu-init/rdimon-aem.S and introduces the new
libgloss/arm/elf-mprofile-mps2.specs to add the mapping to the right
memory address.

2026-05-29  Andre Vieira <[email protected]>
	Christophe Lyon  <[email protected]>

	libgloss/
	* Makefile.in: Add entry for arm/mprofile-mps2.specs.
	* arm/Makefile.inc: Add entry for mprofile-mps2.specs.
	* arm/cpu-init/rdimon-aem.S: Update comment. Add hardfault
	handler.
	* arm/elf-mprofile-mps2.specs: New file.
---
 libgloss/Makefile.in                 |  1 +
 libgloss/arm/Makefile.inc            |  3 ++-
 libgloss/arm/cpu-init/rdimon-aem.S   | 11 +++++++++--
 libgloss/arm/elf-mprofile-mps2.specs | 26 ++++++++++++++++++++++++++
 4 files changed, 38 insertions(+), 3 deletions(-)
 create mode 100644 libgloss/arm/elf-mprofile-mps2.specs

diff --git a/libgloss/Makefile.in b/libgloss/Makefile.in
index 5146a7880..c03ea4e6b 100644
--- a/libgloss/Makefile.in
+++ b/libgloss/Makefile.in
@@ -2255,6 +2255,7 @@ TEXINFO_TEX = ../texinfo/texinfo.tex
 @CONFIG_ARM_TRUE@	arm/aprofile-validation-v2m.specs \
 @CONFIG_ARM_TRUE@	arm/aprofile-ve-v2m.specs \
 @CONFIG_ARM_TRUE@	arm/aprofile-ve.specs arm/pid.specs \
+@CONFIG_ARM_TRUE@       arm/mprofile-mps2.specs \
 @CONFIG_ARM_TRUE@	arm/iq80310.specs
 @CONFIG_ARM_TRUE@arm_libgloss_linux_a_SOURCES = \
 @CONFIG_ARM_TRUE@	arm/linux-syscalls0.c \
diff --git a/libgloss/arm/Makefile.inc b/libgloss/arm/Makefile.inc
index 41fce3c99..5c740edcb 100644
--- a/libgloss/arm/Makefile.inc
+++ b/libgloss/arm/Makefile.inc
@@ -47,7 +47,8 @@ multilibtool_LIBRARIES += %D%/librdpmon.a
 	%D%/aprofile-validation.specs \
 	%D%/aprofile-validation-v2m.specs \
 	%D%/aprofile-ve-v2m.specs \
-	%D%/aprofile-ve.specs
+	%D%/aprofile-ve.specs \
+	%D%/mprofile-mps2.specs
 multilibtool_DATA += \
 	%D%/rdimon-crt0.o \
 	%D%/rdimon-crt0-v2m.o
diff --git a/libgloss/arm/cpu-init/rdimon-aem.S b/libgloss/arm/cpu-init/rdimon-aem.S
index 2aacbeba9..c960924d4 100644
--- a/libgloss/arm/cpu-init/rdimon-aem.S
+++ b/libgloss/arm/cpu-init/rdimon-aem.S
@@ -25,7 +25,9 @@
 
 /* This file gives a basic initialisation of a Cortex-A series core.  It is
    the bare minimum required to get Cortex-A core running with a semihosting
-   interface.
+   interface.  For M-profile we only define a hardfault handler, required to
+   prevent the simulator from entering into an infinite loop.  The comments
+   below apply to A-profile only.
 
    It sets up a basic 1:1 phsyical address to virtual address mapping;
    turns the MMU on; enables branch prediction; activates any integrated
@@ -539,4 +541,9 @@ is_a15_a7:
 page_tables:
      PT7(0x1c0e)
 
-#endif //#if defined(__ARM_ARCH_7A__) || __ARM_ARCH_PROFILE == 'A'
+#elif  (__ARM_ARCH_PROFILE == 'M')
+    /* On M-profile, call abort in case of hardfault: this causes the simulator to
+       abort execution instead of going into an infinite loop.  */
+    .section .hardfault_handler_addr, "a"
+    .word abort
+#endif //#if defined(__ARM_ARCH_7A__) || (__ARM_ARCH_PROFILE == 'A')
diff --git a/libgloss/arm/elf-mprofile-mps2.specs b/libgloss/arm/elf-mprofile-mps2.specs
new file mode 100644
index 000000000..edaf71917
--- /dev/null
+++ b/libgloss/arm/elf-mprofile-mps2.specs
@@ -0,0 +1,26 @@
+# rdimon.specs
+#
+# Spec file for AArch32 M profile baremetal newlib with version 2 of
+# the AngelAPI semi-hosting using the SVC trap instruction.
+#
+# This version supports extensibility through an extension mechanism.
+
+%rename link_gcc_c_sequence                rdimon_link_gcc_c_sequence
+
+%rename link old_link
+
+*link:
+--section-start=.hardfault_handler_addr=0xc %(old_link)
+
+*rdimon_libc:
+%{!specs=nano.specs:-lc} %{specs=nano.specs:-lc_nano}
+
+*rdimon_libgloss:
+%{!specs=nano.specs:-lrdimon} %{specs=nano.specs:-lrdimon_nano}
+
+*link_gcc_c_sequence:
+%(rdimon_link_gcc_c_sequence) --start-group %G %(rdimon_libc) %(rdimon_libgloss) --end-group
+
+*startfile:
+crti%O%s crtbegin%O%s cpu-init/rdimon-aem.o%s %{!pg:rdimon-crt0%O%s} %{pg:rdimon-crt0%O%s}
+
-- 
2.34.1