libs/libc/elf: Load an FDPIC object's data into the data heap.

An architecture that sets CONFIG_ARCH_USE_DATA_HEAP gives a loaded module
its data from up_dataheap_memalign(), because the ordinary heap is not where
that data belongs there.  The ELF loader honours it for every object but an
FDPIC one: an FDPIC object places its writable segment on its own, and that
allocation, and the two places that free it, still use lib_memalign() and
lib_free().  Its text already comes from the text heap.

So an FDPIC module's data goes to the data heap too, and back to it when the
module is unloaded or removed.

On mps3-an547, which sets both heaps, fdpicxip loaded the data of its two
instances at 0x1007220 and 0x104e480, in the ordinary heap.  With this change
they are at 0x21000000 and 0x21000180, in the SRAM2 data heap, and both
instances run.  In a protected build the difference matters: there the
ordinary heap is kernel memory, and the module takes a data access violation
on its first access to its data.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
This commit is contained in:
Marco Casaroli 2026-09-28 13:15:28 +02:00 • committed by Alan C. Assis
parent 1c6ed642bf
commit ec78241ebe
3 changed files with 20 additions and 0 deletions

View file

@ -869,9 +869,21 @@ int libelf_load(FAR struct mod_loadinfo_s *loadinfo)
if (loadinfo->datasize > 0)
{
# if defined(CONFIG_ARCH_USE_DATA_HEAP) && \
defined(CONFIG_ARCH_USE_SEPARATED_SECTION)
loadinfo->datastart =
(uintptr_t)up_dataheap_memalign(".data",
loadinfo->dataalign,
loadinfo->datasize);
# elif defined(CONFIG_ARCH_USE_DATA_HEAP)
loadinfo->datastart =
(uintptr_t)up_dataheap_memalign(loadinfo->dataalign,
loadinfo->datasize);
# else
loadinfo->datastart =
(uintptr_t)lib_memalign(loadinfo->dataalign,
loadinfo->datasize);
# endif
if (!loadinfo->datastart)
{
berr("ERROR: Failed to allocate the module's data\n");

View file

@ -180,7 +180,11 @@ int libelf_uninit(FAR struct module_s *modp)
#endif
}
#ifdef CONFIG_ARCH_USE_DATA_HEAP
up_dataheap_free((FAR void *)modp->dataalloc);
#else
lib_free((FAR void *)modp->dataalloc);
#endif
}
else
{

View file

@ -96,7 +96,11 @@ int libelf_unload(FAR struct mod_loadinfo_s *loadinfo)
if (loadinfo->datastart != 0)
{
#ifdef CONFIG_ARCH_USE_DATA_HEAP
up_dataheap_free((FAR void *)loadinfo->datastart);
#else
lib_free((FAR void *)loadinfo->datastart);
#endif
loadinfo->datastart = 0;
}