armv7a:need initialize constructor and destructor on crt0

The C++ constructor and destructor need to be executed in crt0

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
This commit is contained in:
anjiahao 2025-02-06 16:21:34 +08:00 committed by Xiang Xiao
parent ac2078a8bf
commit 297a1cb1fe
2 changed files with 91 additions and 18 deletions

View file

@ -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);

View file

@ -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 = . ;
}