arch/sim: replace macOS C++ constructor runtime hack with post-link patch

The sim architecture needs to defer C++ global constructors until after
NuttX kernel initialization completes. On macOS this was previously done
via a runtime hack (sim_macho_init.c): a __attribute__((constructor))
function intercepted constructors, saved them, and replayed them later.
That approach was fragile because it depended on constructor ordering
and required mprotect() to patch the read-only __mod_init_func section
at runtime.

This commit replaces the runtime hack with a post-link patching scheme:

  1. Link with -Wl,-ld_classic,-no_fixup_chains to keep the classic
     __mod_init_func pointer format (prevents ld64 from converting it
     to __init_offsets).
  2. Post-link, run patch_macho_initsection.py (python3 + lief) to
     patch Mach-O section type flags from
     MOD_INIT_FUNC_POINTERS/INIT_FUNC_OFFSETS to REGULAR, so dyld
     skips them entirely.
  3. Use the Mach-O auto-generated boundary symbols
     section$start$__DATA_CONST$__mod_init_func /
     section$end$__DATA_CONST$__mod_init_func, mapped via __asm()
     labels in arch/sim/include/arch.h to the common _sinit[]/_einit[]
     names used by lib_cxx_initialize().

Linux behavior is unchanged.

Changes:
  - arch/sim/include/arch.h: add macOS __asm() declarations for
    _sinit/_einit
  - arch/sim/src/Makefile: drop sim_macho_init.c HEADSRC handling,
    always pass -ld_classic,-no_fixup_chains on macOS, run
    patch_macho_initsection.py after link when CONFIG_HAVE_CXXINITIALIZE
  - arch/sim/src/sim/CMakeLists.txt: same for the CMake build
  - arch/sim/src/patch_macho_initsection.py: new lief-based patcher
  - arch/sim/src/sim/posix/sim_macho_init.c: deleted (135-line hack)
  - libs/libc/misc/lib_cxx_initialize.c: remove
    macho_call_saved_init_funcs special case; single unified loop

Testing:
  - macOS (Ventura VM): verified lief patching with standalone test
    (test_sinit7) confirming the constructor is deferred past main()
    and only invoked when explicitly called via the _sinit/_einit loop.

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2026-03-11 03:05:52 +08:00 • committed by Alan C. Assis
parent 306de5b04b
commit 754dfd980b
7 changed files with 130 additions and 165 deletions

View file

@ -27,4 +27,42 @@
#ifndef __ARCH_SIM_INCLUDE_ARCH_H
#define __ARCH_SIM_INCLUDE_ARCH_H
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Public Data
****************************************************************************/
/* _sinit and _einit mark the beginning and end of the C++ constructor
* array. They are mapped to platform-specific linker symbols:
* macOS: section$start$__DATA_CONST$__mod_init_func /
* section$end$__DATA_CONST$__mod_init_func (Mach-O auto)
* On macOS, the section type flags are patched post-link to prevent
* dyld from auto-running constructors before NuttX is initialized.
*/
#ifdef CONFIG_HAVE_CXXINITIALIZE
# if defined(CONFIG_HOST_MACOS)
extern void (*_sinit[])(void)
__asm("section$start$__DATA_CONST$__mod_init_func");
extern void (*_einit[])(void)
__asm("section$end$__DATA_CONST$__mod_init_func");
# endif
#endif
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* __ARCH_SIM_INCLUDE_ARCH_H */