[2/10] libs/libc/elf: Give a shared object a word alignment, not zero. - #19939
Merged
Conversation
This was referenced Aug 23, 2026
|
libelf_elfsize() takes textalign and dataalign from the section headers, which only the ET_REL path walks. An ET_DYN object is sized from its program headers instead, so both fields stay at zero, and the allocation a few lines later asks for that alignment: loadinfo->textalloc = lib_memalign(loadinfo->textalign, ...); Zero is not a valid alignment, and every path that receives it divides by it. mm_memalign() accepts zero as a power of two, because 0 & -0 is 0, then takes the "alignment <= MM_ALIGN" branch and evaluates "((uintptr_t)ptr) % alignment" in a DEBUGASSERT. With CONFIG_MM_HEAP_MEMPOOL and a pool that fits the request the object never reaches that branch and gets ALIGN_UP(blk, 0) instead, which is ((blk - 1) / 0) * 0. On Cortex-M this is usually invisible: UDIV returns zero for a division by zero unless CCR.DIV_0_TRP is set, which NuttX does not set, so the assertion compares zero against zero and passes. It is a SIGFPE on the simulator, and the mempool path returns a null pointer wherever the division yields zero, which the loader reports as -ENOMEM. Ask for a natural word when the program headers gave nothing. p_align is the linker's page granularity, not a section requirement, so honouring it would cost a page per module for no gain, and the sections of a shared object need no more than a word. Built for mps3-an547:picostest, which is CONFIG_ELF with CONFIG_PIC. Runtime evidence on hardware follows. Assisted-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
casaroli
force-pushed
the
elf-align-zero
branch
from
August 23, 2026 21:34
8a05ed0 to
2941df7
Compare
xiaoxiang781216
approved these changes
Aug 24, 2026
xiaoxiang781216
marked this pull request as ready for review
August 24, 2026 01:54
jerpelea
approved these changes
Aug 24, 2026
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.
Summary
libelf_elfsize()takestextalignanddataalignfrom the section headers, which only theET_RELpath walks. AnET_DYNobject is sized from its program headers instead, so both stay at zero, and the allocation a few lines later askslib_memalign()for that alignment.Zero is not a valid alignment, and every path that receives it divides by it.
mm_memalign()accepts zero as a power of two, because0 & -0is 0, then takes thealignment <= MM_ALIGNbranch and evaluates((uintptr_t)ptr) % alignmentin aDEBUGASSERT. WithCONFIG_MM_HEAP_MEMPOOLand a pool that fits the request the object never reaches that branch and getsALIGN_UP(blk, 0)instead, which is((blk - 1) / 0) * 0.On Cortex-M this is usually invisible:
UDIVreturns zero for a division by zero unlessCCR.DIV_0_TRPis set, which NuttX does not set. It is a SIGFPE on the simulator, and the mempool path returns a null pointer wherever the division yields zero, which the loader reports as-ENOMEM.Ask for a natural word when the program headers gave nothing.
p_alignis the linker's page granularity, not a section requirement, so honouring it would cost a page per module for no gain.This is the second of ten PRs that #19673 is being split into, so each can be reviewed on its own. The first four have no FDPIC content at all and do not depend on each other, so they can merge in any order. The remaining six are the FDPIC work itself, one subsystem each, and each is a no-op with
CONFIG_FDPICoff: loader core, ARM relocations, the callback entry points, theexec()path,DT_NEEDEDthroughdlopen(), then documentation and a board configuration. The others are[1/10]#19938,[3/10]#19940 and[4/10]#19941.Impact
Every
ET_DYNmodule, which is every shared object opened withdlopen(), is allocated through an aligned path rather than a zero one.No configuration change, no size change beyond the alignment of one allocation.
Testing
Built for
mps3-an547:picostest, which isCONFIG_ELFwithCONFIG_PIC.tools/checkpatch.shpasses.Runtime evidence on real hardware follows tomorrow, on an RP2350.