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 297a1cb1fed795257374d5ee7383d628a71112fc
Author: anjiahao <anjia...@xiaomi.com>
AuthorDate: Thu Feb 6 16:21:34 2025 +0800

    armv7a:need initialize constructor and destructor on crt0
    
    The C++ constructor and destructor need to be executed in crt0
    
    Signed-off-by: anjiahao <anjia...@xiaomi.com>
---
 arch/arm/src/armv7-a/crt0.c    | 59 +++++++++++++++++++++++++++++++++++++++++-
 libs/libc/modlib/gnu-elf.ld.in | 50 +++++++++++++++++++++++------------
 2 files changed, 91 insertions(+), 18 deletions(-)

diff --git a/arch/arm/src/armv7-a/crt0.c b/arch/arm/src/armv7-a/crt0.c
index b0dbda1ca4..93e74b2c05 100644
--- a/arch/arm/src/armv7-a/crt0.c
+++ b/arch/arm/src/armv7-a/crt0.c
@@ -30,11 +30,23 @@
 #include <stdlib.h>
 
 #include <nuttx/addrenv.h>
+#include <nuttx/arch.h>
 
 #include <arch/syscall.h>
 
 #ifdef CONFIG_BUILD_KERNEL
 
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/* Linker defined symbols to .ctors and .dtors */
+
+extern initializer_t _sctors[];
+extern initializer_t _ectors[];
+extern initializer_t _sdtors[];
+extern initializer_t _edtors[];
+
 /****************************************************************************
  * Public Function Prototypes
  ****************************************************************************/
@@ -88,6 +100,41 @@ static void sig_trampoline(void)
   );
 }
 
+#ifdef CONFIG_HAVE_CXXINITIALIZE
+
+/****************************************************************************
+ * Name: exec_ctors
+ *
+ * Description:
+ *   Call static constructors
+ *
+ ****************************************************************************/
+
+static void exec_ctors(void)
+{
+  for (initializer_t *ctor = _sctors; ctor != _ectors; ctor++)
+    {
+      (*ctor)();
+    }
+}
+
+/****************************************************************************
+ * Name: exec_dtors
+ *
+ * Description:
+ *   Call static destructors
+ *
+ ****************************************************************************/
+
+static void exec_dtors(void)
+{
+  for (initializer_t *dtor = _sdtors; dtor != _edtors; dtor++)
+    {
+      (*dtor)();
+    }
+}
+
+#endif
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -122,16 +169,26 @@ void __start(int argc, char *argv[])
 
   ARCH_DATA_RESERVE->ar_sigtramp = (addrenv_sigtramp_t)sig_trampoline;
 
+#ifdef CONFIG_HAVE_CXXINITIALIZE
   /* Call C++ constructors */
 
+  exec_ctors();
+
   /* Setup so that C++ destructors called on task exit */
 
-  /* REVISIT: Missing logic */
+#  if CONFIG_LIBC_MAX_EXITFUNS > 0
+  atexit(exec_dtors);
+#  endif
+#endif
 
   /* Call the main() entry point passing argc and argv. */
 
   ret = main(argc, argv);
 
+#if defined(CONFIG_HAVE_CXXINITIALIZE) && CONFIG_LIBC_MAX_EXITFUNS <= 0
+  exec_dtors();
+#endif
+
   /* Call exit() if/when the main() returns */
 
   exit(ret);
diff --git a/libs/libc/modlib/gnu-elf.ld.in b/libs/libc/modlib/gnu-elf.ld.in
index 695fd8bc44..52287c7a49 100644
--- a/libs/libc/modlib/gnu-elf.ld.in
+++ b/libs/libc/modlib/gnu-elf.ld.in
@@ -33,6 +33,12 @@
 #  define DATA
 #endif
 
+#if defined(CONFIG_LIBC_ARCH_ELF_64BIT)
+#  define SECTIONS_ALIGN 8
+#else
+#  define SECTIONS_ALIGN 4
+#endif
+
 SECTIONS
 {
   .text TEXT :
@@ -45,24 +51,10 @@ SECTIONS
       *(.glue_7)
       *(.glue_7t)
       *(.jcr)
+      . = ALIGN(SECTIONS_ALIGN);
       _etext = . ;
     }
 
-  .init_array :
-    {
-      _sinit = .;
-      KEEP(*(SORT_BY_INIT_PRIORITY(.init_array.*) 
SORT_BY_INIT_PRIORITY(.ctors.*)))
-      KEEP(*(.init_array .ctors))
-      _einit = .;
-    }
-
-  .fini_array :
-    {
-      KEEP (*(.dtors))
-      KEEP (*(.fini_array))
-      KEEP (*(SORT(.fini_array.*)))
-    }
-
   .rodata :
     {
       _srodata = . ;
@@ -70,6 +62,7 @@ SECTIONS
       *(.rodata1)
       *(.rodata.*)
       *(.gnu.linkonce.r*)
+      . = ALIGN(SECTIONS_ALIGN);
       _erodata = . ;
     }
 
@@ -80,10 +73,33 @@ SECTIONS
       *(.data1)
       *(.data.*)
       *(.gnu.linkonce.d*)
-      . = ALIGN(4);
+      . = ALIGN(SECTIONS_ALIGN);
       _edata = . ;
     }
 
+  .init_array :
+    {
+      _sinit = .;
+      _sctors = .;
+      KEEP(*(SORT_BY_INIT_PRIORITY(.init_array.*) 
SORT_BY_INIT_PRIORITY(.ctors.*)))
+      KEEP(*(.init_array .ctors))
+      . = ALIGN(SECTIONS_ALIGN);
+      _einit = .;
+      _ectors = .;
+    }
+
+  .fini_array :
+    {
+      _sfini = .;
+      _sdtors = .;
+      KEEP (*(.dtors))
+      KEEP (*(.fini_array))
+      KEEP (*(SORT(.fini_array.*)))
+      . = ALIGN(SECTIONS_ALIGN);
+      _efini = .;
+      _edtors = .;
+    }
+
   .bss :
     {
       _sbss = . ;
@@ -93,7 +109,7 @@ SECTIONS
       *(.sbss.*)
       *(.gnu.linkonce.b*)
       *(COMMON)
-      . = ALIGN(4);
+      . = ALIGN(SECTIONS_ALIGN);
       _ebss = . ;
     }
 

Reply via email to