nuttx/libs/libc/misc
Xiang Xiao 754dfd980b 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>
2026-05-19 07:08:55 -03:00
..
CMakeLists.txt
Kconfig
lib_backtrace.c
lib_bitmap.c
lib_circbuf.c
lib_crc8.c
lib_crc8ccitt.c
lib_crc8h1d.c
lib_crc8h2f.c
lib_crc8rohc.c
lib_crc8table.c
lib_crc16.c
lib_crc16ccitt.c
lib_crc16h1021.c
lib_crc16h8005.c
lib_crc16ibm.c
lib_crc16xmodem.c
lib_crc32.c
lib_crc32h04c11db7.c
lib_crc32hf4acfb13.c
lib_crc64.c !compiler: drop CONFIG_HAVE_LONG_LONG and require long long support 2026-05-19 16:21:28 +08:00
lib_crc64emac.c !compiler: drop CONFIG_HAVE_LONG_LONG and require long long support 2026-05-19 16:21:28 +08:00
lib_creat.c
lib_cxx_initialize.c arch/sim: replace macOS C++ constructor runtime hack with post-link patch 2026-05-19 07:08:55 -03:00
lib_debug.c
lib_dumpbuffer.c
lib_dumpvbuffer.c
lib_envpath.c
lib_err.c
lib_fchmodat.c
lib_fdcheck.c
lib_fdsan.c
lib_fnmatch.c
lib_fstatat.c
lib_ftok.c
lib_getfullpath.c
lib_getnprocs.c
lib_getrandom.c
lib_glob.c
lib_idr.c
lib_impure.c
lib_instrument.c
lib_kbddecode.c
lib_kbdencode.c
lib_mallopt.c
lib_memfd.c
lib_mkdirat.c
lib_mkfifo.c
lib_mknod.c
lib_mutex.c
lib_ncompress.c
lib_note.c
lib_openat.c
lib_slcddecode.c
lib_slcdencode.c
lib_tea_decrypt.c
lib_tea_encrypt.c
lib_tempbuffer.c
lib_uadd32x64.c
lib_uadd64.c
lib_umask.c
lib_umul32.c
lib_umul32x64.c
lib_umul64.c
lib_usub64.c
lib_usub64x32.c
lib_utimensat.c
lib_utsname.c
lib_xorshift128.c
Make.defs