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 libs/crc: implement AUTOSAR-compatible CRC algorithm 2026-01-30 17:32:15 +08:00
Kconfig libs/libc: add configurable skip count for mutex backtrace addresses 2026-02-27 19:54:37 +08:00
lib_backtrace.c lib/backtrace:need init lock before use 2025-12-22 15:23:04 +08:00
lib_bitmap.c style: fix spelling in code comments and strings 2025-05-23 10:48:41 +08:00
lib_circbuf.c circbuf: fix circbuf_get_read/writeptr return *size = 0 when circbuf full 2024-10-16 16:59:51 +08:00
lib_crc8.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_crc8ccitt.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_crc8h1d.c libs/crc: implement AUTOSAR-compatible CRC algorithm 2026-01-30 17:32:15 +08:00
lib_crc8h2f.c libs/crc: implement AUTOSAR-compatible CRC algorithm 2026-01-30 17:32:15 +08:00
lib_crc8rohc.c libc/crc: Add crc8rohcincr for incremental CRC8 ROHC calculation 2025-09-30 17:30:46 -03:00
lib_crc8table.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_crc16.c libs/libc/crc16: Separate implementation of crc16xmodem from crc16 2025-04-10 16:47:28 +08:00
lib_crc16ccitt.c libs/libc/crc16: move crc16 table to rodata 2025-04-07 09:02:01 +08:00
lib_crc16h1021.c libs/crc: implement AUTOSAR-compatible CRC algorithm 2026-01-30 17:32:15 +08:00
lib_crc16h8005.c libs/crc: implement AUTOSAR-compatible CRC algorithm 2026-01-30 17:32:15 +08:00
lib_crc16ibm.c libs/libc/crc16: add support of CRC-16/ANSI(aka CRC-16/IBM) 2025-04-07 12:10:05 +08:00
lib_crc16xmodem.c libs/libc/crc16: Separate implementation of crc16xmodem from crc16 2025-04-10 16:47:28 +08:00
lib_crc32.c libc/arm: optimize crc32/crc32c for arm 2025-04-04 09:51:50 -03:00
lib_crc32h04c11db7.c libs/crc: implement AUTOSAR-compatible CRC algorithm 2026-01-30 17:32:15 +08:00
lib_crc32hf4acfb13.c libs/crc: implement AUTOSAR-compatible CRC algorithm 2026-01-30 17:32:15 +08:00
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 libc: add creat function implementation 2026-01-16 01:38:20 +08:00
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 include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
lib_dumpbuffer.c include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
lib_dumpvbuffer.c include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
lib_envpath.c style: fix spelling in code comments and strings 2025-05-23 10:48:41 +08:00
lib_err.c lib_libvsprintf.c:add option in Kconfig to control "%p*" special format specifier. 2024-10-15 15:33:13 +08:00
lib_fchmodat.c Use lib_get_pathbuffer instead of stack variables 2024-11-12 17:21:42 +08:00
lib_fdcheck.c include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
lib_fdsan.c include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
lib_fnmatch.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_fstatat.c Use lib_get_pathbuffer instead of stack variables 2024-11-12 17:21:42 +08:00
lib_ftok.c libs/libc/ftok: fix sizeof(char *), after tempbuffer 2026-01-13 01:59:58 +08:00
lib_getfullpath.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_getnprocs.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_getrandom.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_glob.c libs: unify Private Types banners 2025-05-28 10:17:15 +08:00
lib_idr.c libc/idr: Remove nodes from RB trees during destroy 2025-07-12 14:24:32 +08:00
lib_impure.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_instrument.c style: fix spelling in code comments and strings 2025-05-23 10:48:41 +08:00
lib_kbddecode.c include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
lib_kbdencode.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_mallopt.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_memfd.c libc/shmfs: Ensure uniqueness of memfd. 2026-01-22 22:11:17 +08:00
lib_mkdirat.c Use lib_get_pathbuffer instead of stack variables 2024-11-12 17:21:42 +08:00
lib_mkfifo.c Use lib_get_pathbuffer instead of stack variables 2024-11-12 17:21:42 +08:00
lib_mknod.c Use lib_get_pathbuffer instead of stack variables 2024-11-12 17:21:42 +08:00
lib_mutex.c libs/libc: add configurable skip count for mutex backtrace addresses 2026-02-27 19:54:37 +08:00
lib_ncompress.c style: fix spelling in code comments and strings 2025-05-23 10:48:41 +08:00
lib_note.c driver/note: Fix compilation error when CONFIG_SYSLOG_TO_SCHED_NOTE=y 2026-01-23 07:48:08 -03:00
lib_openat.c Use lib_get_pathbuffer instead of stack variables 2024-11-12 17:21:42 +08:00
lib_slcddecode.c include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
lib_slcdencode.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_tea_decrypt.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_tea_encrypt.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_tempbuffer.c libs/lib_tempbuffer: set errno when fails 2025-01-17 08:16:14 +08:00
lib_uadd32x64.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_uadd64.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_umask.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_umul32.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_umul32x64.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_umul64.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_usub64.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_usub64x32.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_utimensat.c Use lib_get_pathbuffer instead of stack variables 2024-11-12 17:21:42 +08:00
lib_utsname.c libc/utsname: Only store version in RAM, use rodata for all others 2024-10-12 15:36:28 +08:00
lib_xorshift128.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
Make.defs arch/*/src/Makefile: Avoid uncessary relinking of the nuttx binary. 2026-05-18 09:33:58 +02:00