arch/sim: replace macOS C++ constructor runtime hack with post-link patch#18886
arch/sim: replace macOS C++ constructor runtime hack with post-link patch#18886xiaoxiang781216 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR replaces the macOS simulator C++ constructor deferral mechanism with post-link Mach-O section patching, so dyld does not run constructors before NuttX initialization.
Changes:
- Removes the old runtime Mach-O constructor interception path.
- Adds macOS linker flags and post-build patching for Mach-O init sections.
- Maps macOS Mach-O section boundary symbols to
_sinit/_einit.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
libs/libc/misc/lib_cxx_initialize.c |
Uses the common _sinit / _einit constructor loop for macOS too. |
arch/sim/src/sim/posix/sim_macho_init.c |
Removes the previous runtime constructor-saving hack. |
arch/sim/src/sim/CMakeLists.txt |
Adds macOS link options and a post-build Mach-O patch command. |
arch/sim/src/patch_macho_initsection.py |
Adds the LIEF-based Mach-O section type patcher. |
arch/sim/src/Makefile |
Adds macOS linker flags and invokes the patcher in make builds. |
arch/sim/include/arch.h |
Adds macOS-specific _sinit / _einit asm symbol mappings. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
7ee85e0 to
0f309e2
Compare
…atch
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>
|
Hello. Regarding
|
|
about Writing the file invalidates the ad-hoc signature that ld attached at link time, which causes the kernel to SIGKILL the process at exec. |
Summary
Replace the macOS sim C++ global constructor runtime hack (
sim_macho_init.c)with a post-link Mach-O section patching scheme so dyld no longer auto-runs
constructors before NuttX is initialized.
Impact
Testing
CONFIG_HAVE_CXX=y/CONFIG_HAVE_CXXINITIALIZE=ytest_sinit7) that the C++ constructor isdeferred past
main()and is only invoked when explicitly called throughthe
_sinit[]/_einit[]loop inlib_cxx_initialize().Details
-Wl,-ld_classic,-no_fixup_chainsto keep the classic__mod_init_funcpointer format (prevents ld64 from converting it to__init_offsets).patch_macho_initsection.py(python3 +lief) to patchthe Mach-O section type flags from
MOD_INIT_FUNC_POINTERS/INIT_FUNC_OFFSETStoREGULAR, so dyld skips them.section$start$__DATA_CONST$__mod_init_func/section$end$__DATA_CONST$__mod_init_func, mapped via__asm()labels inarch/sim/include/arch.hto the common_sinit[]/_einit[]names usedby
lib_cxx_initialize().sim_macho_init.cruntime hack and the macOS-specificmacho_call_saved_init_funcs()path inlib_cxx_initialize.c.Dependency:
pip install liefon the build host whenCONFIG_HAVE_CXXINITIALIZE=y.