Repository navigation
feat(elf_loader): run .preinit_array/.init_array/.fini_array of loaded ELF - #797
Closed
franz-hoepfinger-4diac wants to merge 1 commit into
Closed
franz-hoepfinger-4diac wants to merge 1 commit into
franz-hoepfinger-4diac wants to merge 1 commit into
Conversation
…+ static objects) Static constructors of C++ ELF applications were never executed because .preinit_array/.init_array/.fini_array were ignored. After relocation the loader now records these arrays, runs preinit/init once before the entry point in esp_elf_request() and fini in esp_elf_deinit(). The Xtensa toolchain emits the legacy .ctors/.dtors sections, which are supported too (.ctors is run backwards). In section mode (ESP32/S2/S3) the arrays are additionally copied to memory, since relocations targeting them had no destination before. Also document C++ ELF applications (host must register C++ runtime symbols) and add a changelog entry. Tested on ESP32-S31 (RISC-V) and ESP32-S3 (Xtensa). Fixes espressif#796 Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
franz-hoepfinger-4diac
force-pushed
the
feature/elf-loader-init-array
branch
from
October 9, 2026 06:51
e379341 to
9d559c3
Compare
2 tasks done
zhanzhaocheng
pushed a commit
that referenced
this pull request
Oct 27, 2026
Preserve Franz Höpfinger's original PR #797 commit as the preceding author-attributed commit. Rework the lifecycle section allocator and validator for ELF loader 2.0.0, retaining aligned data layout, section mapping and strict relocation bounds. Add real Xtensa/RISC-V callback tests and C++ ELF build fixtures. Closes: #796
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
C++ ELF applications loaded by
elf_loadernever had their static constructors executed, because.preinit_array/.init_array/.fini_arraywere ignored.This PR:
esp_elf_find_init_arrays()); in section mode (ESP32/S2/S3) it now also copies them to memory, because otherwise relocations that target them have no destination, and it handles the legacy.ctors/.dtorslayout the Xtensa toolchain emits (.ctorsruns backwards),.preinit_arrayand.init_arrayonce, in order, before the entry point inesp_elf_request(),.fini_arrayin reverse order inesp_elf_deinit()(only if the constructors were run),operator new/deleteand__cxa_pure_virtualwithesp_elf_register_symbol();__dso_handle/__cxa_atexitmust live in the ELF or be exported by the host) and adds a CHANGELOG entry.Fixes #796
Testing
Tested on ESP32-S31 (RISC-V) and ESP32-S3 (Xtensa), ESP-IDF v6.1, with
elf_console_example-style host (ELF on LittleFS,execshell command). The test ELF has a class hierarchy with virtual functions,new/delete, a global object with constructor/destructor, and__attribute__((constructor(101)))/__attribute__((destructor))functions. Same output on both targets:Not tested: ESP32 / ESP32-S2 (same code path as ESP32-S3), the dynamic shared object (
dlopen) path,.preinit_array, C++ exceptions and RTTI.Checklist
🤖 Generated with Claude Code