nuttx/libs/libc
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
..
aio include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
assert lib_stackchk.c: a duplicate implementation error reported during compilation 2026-02-02 13:22:07 +08:00
audio include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
builtin libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
ctype style: fix spelling in code comments and strings 2025-05-23 10:48:41 +08:00
dirent cmake:refine nuttx cmake build system fix CMake build missing part 2024-10-13 02:25:06 +08:00
dlfcn include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
elf include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
errno libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
eventfd libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
fdt libs/libc/fdt/Make.defs: cannot move due to directory not empty bugfix 2024-10-09 15:42:17 +08:00
fixedmath !compiler: drop CONFIG_HAVE_LONG_LONG and require long long support 2026-05-19 16:21:28 +08:00
gdbstub arch/arm64: fix backtrace register access and gdbstub config guard 2026-01-16 08:55:47 -03:00
gnssutils libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
grp libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
hex2bin include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
inttypes !compiler: drop CONFIG_HAVE_LONG_LONG and require long long support 2026-05-19 16:21:28 +08:00
libgen libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
locale style/FAR: remove FAR qualifier 2025-11-10 10:56:29 +01:00
lzf !compiler: drop CONFIG_HAVE_LONG_LONG and require long long support 2026-05-19 16:21:28 +08:00
machine arm: memcpy: add NEON paths for aligned copies 2026-05-09 09:46:43 +08:00
misc arch/sim: replace macOS C++ constructor runtime hack with post-link patch 2026-05-19 07:08:55 -03:00
net include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
netdb !sys/types.h: change time_t and clock_t to int64_t to align with other OSes 2026-05-19 16:21:28 +08:00
obstack include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
pthread include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
pwd libs/libc/unistd: add getspnam function 2024-10-16 11:15:25 +08:00
queue libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
regex libs/libc/regex: remove logically dead code in regcomp.c 2026-01-24 19:28:56 +08:00
sched include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
search libc/search: add foreach api for hcreate_data 2025-12-25 09:39:03 +08:00
semaphore pthread: move pthread_cond to userspace 2026-01-26 16:26:39 +08:00
signal sched/signal: Add support for disabling all signal functions 2026-01-18 08:24:13 -03:00
spawn include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
stdio !libc/stream: remove CONFIG_LIBC_LONG_LONG and always support long long 2026-05-19 16:21:28 +08:00
stdlib !compiler: drop CONFIG_HAVE_LONG_LONG and require long long support 2026-05-19 16:21:28 +08:00
stream !libc/stream: remove CONFIG_LIBC_LONG_LONG and always support long long 2026-05-19 16:21:28 +08:00
string !compiler: drop CONFIG_HAVE_LONG_LONG and require long long support 2026-05-19 16:21:28 +08:00
symtab include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
syslog drivers/syslog: Add RFC 5424 protocol support 2025-06-13 20:26:57 +08:00
termios libs: unify Private Types banners 2025-05-28 10:17:15 +08:00
time !nuttx: drop redundant casts on tv_sec/tv_nsec and fix printf formats 2026-05-19 16:21:28 +08:00
tls libc/tls: update preprocessor condition comment to match implementation 2026-01-31 02:49:10 +08:00
uio move readv/writev to the kernel 2024-10-30 17:07:54 +08:00
unistd include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
userfs include/debug.h: Move to include/nuttx/debug.h 2026-04-07 07:50:06 -03:00
uuid libc: Refine the arc4random_buf implementation 2024-10-26 18:04:21 -03:00
wchar style: fix spelling in code comments and strings 2025-05-23 10:48:41 +08:00
wctype libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
wqueue !sched/clock: remove CONFIG_SYSTEM_TIME64 and always use 64-bit time 2026-05-19 16:21:28 +08:00
zoneinfo Makefile: Remove make depend files by make distclean 2026-02-16 16:27:57 +01:00
.gitignore libc/elf: rename modlib to libelf 2025-04-11 09:43:22 +08:00
CMakeLists.txt libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
Kconfig libc/elf: rename modlib to libelf 2025-04-11 09:43:22 +08:00
libc.csv !compiler: drop CONFIG_HAVE_LONG_LONG and require long long support 2026-05-19 16:21:28 +08:00
libc.h libc/machine:Add prefixes to libc functions implemented by arch 2024-10-31 18:11:16 +08:00
Makefile Makefile: Remove make depend files by make distclean 2026-02-16 16:27:57 +01:00