This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit dd9f666ad28d72071533f9f6eeb73e7951615e70
Author: guoshengyuan1 <[email protected]>
AuthorDate: Fri Jan 9 18:32:48 2026 +0800

    arch/arm: Refactor __aeabi_read_tp to ensure EABI compliance
    
    Reimplement __aeabi_read_tp using naked function with inline assembly
    to strictly follow ARM EABI TLS helper specification.
    
    The ARM EABI specifies that __aeabi_read_tp may only modify r0, and
    compilers rely on this guarantee by NOT saving r1-r3 registers when
    calling this function. The previous simple C implementation could
    potentially clobber these registers through compiler optimizations,
    violating the ABI contract and causing subtle bugs.
    
    Changes:
    - Add __aeabi_read_tp_core() helper to allow tls_get_info() macro
      expansion in C context
    - Explicitly preserve r1-r3 registers per EABI requirement
    
    This ensures proper register usage compliance for all calling contexts
    while maintaining the ability to use tls_get_info() macro correctly.
    
    Reference: ARM EABI TLS Helper Specification
    
https://github.com/ARM-software/abi-aa/blob/main/rtabi32/rtabi32.rst#thread-local-storage-new-in-v2-01
    
    Signed-off-by: guoshengyuan1 <[email protected]>
---
 arch/arm/src/common/arm_tls.c | 33 ++++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)

diff --git a/arch/arm/src/common/arm_tls.c b/arch/arm/src/common/arm_tls.c
index e01cb51ca76..f887a08066b 100644
--- a/arch/arm/src/common/arm_tls.c
+++ b/arch/arm/src/common/arm_tls.c
@@ -30,6 +30,24 @@
 
 #include "arm_internal.h"
 
+/****************************************************************************
+ * Private Function
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: arm_read_tp
+ *
+ * Description:
+ *   Helper function for __aeabi_read_tp.
+ *   This allows tls_get_info() macro to expand properly.
+ *
+ ****************************************************************************/
+
+void *arm_read_tp(void)
+{
+  return (void *)(tls_get_info() + 1);
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -84,9 +102,22 @@ void up_tls_initialize(struct tls_info_s *info)
  * Description:
  *   Read thread local storage region pointer.
  *
+ *   This function follows the ARM EABI specification for TLS helper
+ *   functions:
+ *   - Only r0 is modified (return value)
+ *   - r1-r3 must be preserved for the caller
+ *
+ *   The naked attribute ensures strict register usage compliance.
+ *
  ****************************************************************************/
 
+__attribute__((naked))
 void *__aeabi_read_tp(void)
 {
-  return (void *)(tls_get_info() + 1);
+  __asm__ __volatile__
+    (
+      "push {r1-r3, lr}\n\t"
+      "bl arm_read_tp\n\t"
+      "pop {r1-r3, pc}\n\t"
+    );
 }

Reply via email to