misc: Implement two-pass linking to separate rodata sections. #245
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.
This patch changes the build to perform two-pass linking to separate rodata with relocations (copied to RAM) from rodata with no relocations (pure const data) that can be kept in flash by llext.
For dynamic linking mode:
For static linking mode:
Output from readelf on an example sketch that includes ~64KBs CA certs blob. Before the patch:
After:
Alternatives considered and tested:
This is by far the simplest and safest approach since it relies on the toolchain to do all the magic so we can trust it Not to break the elf. That said, I implemented and tested two more solutions:
1- Patch elf to set custom flags and/or section type: first attempt, using a custom Python/Go script, can set special section flags or section type, to mark rodata sections with no relocations, but this required more modifications to llext and using section attributes.
2- Reorder sections: using a custom Python/Go script, reorder elf sections such that all rodata with relocs come first (or last, doesn't matter). llext doesn't support non-contiguous regions, so if we just reorder them, plus my patch above, things work out. While this fixes the issue in one pass, it doesn't merge the sections, so final elf will have many rodata and rodata.noreloc sections. This can be fixed by merging the sections, but the relocations will have to be fixed. Doable, but tricky and risky.