libs/libc/elf: Place an FDPIC object's segments independently.

An ET_DYN object is loaded into one allocation with its data behind its
text, because its data references sit at a fixed distance from the code
that makes them.  An FDPIC object does not work that way: it reaches its
data through a base register, so the two segments can be placed wherever
suits, and the point of the format is that the read-only one is left on
the media and executed there while only the writable one is copied.  One
copy of the text then serves every instance.

So libelf_load() grows a second case.  The object announces itself in the
OS/ABI byte, which is noted once in libelf_loadhdrs() rather than
re-derived; e_flags cannot be used for this, as an FDPIC object's are an
unremarkable EABI version and testing them would reject every valid
module.  Text is taken from the media address plus the segment's own file
offset -- the same arithmetic the ET_REL path already does with
sh_offset -- and libelf_loadfile() does not read it.  If the filesystem
cannot show its media, the loader copies the text to RAM instead.  The
module then loses the shared text and the flash saving, but it runs.

Obtaining that address needs two mechanisms, and they are not
interchangeable.  A compacting filesystem can move a file's blocks, so it
hands out an address only with a pin that holds them still and expects
the pin back; xipfs is the one in tree.  A filesystem whose layout never
changes has nothing to hold and answers FIOC_XIPBASE with a bare address;
romfs and tmpfs are those.  libelf_xipacquire() asks for the pin first,
because a filesystem that needs one is not safe without it, and
libelf_unload() gives it back.  The loader asks for a pin only if it can
hold one, or the pin would stay for ever.

The pin is thus not specific to FDPIC.  Any module that executes in place
from a compacting filesystem takes one, and gives it back at unload.

mmap() is not used, though both filesystems implement it.  The mapping
would be recorded against whichever task called the loader, while the
release happens when the module's own task exits, which is a different
group -- so the pin would outlive the module and the extent would never
become movable again.

Unloading has to change with placement: the existing path frees only
textalloc because ET_DYN had a single allocation, which would leak an
FDPIC object's data and free media the filesystem only lent us.

Nothing here runs for a non-FDPIC object; every branch is behind the flag
and the single-allocation path is untouched.  Built and booted
mps3-an547:picostest, which is CONFIG_ELF with CONFIG_PIC, with no change
in behaviour.

Assisted-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
This commit is contained in:
Marco Casaroli 2026-08-03 00:56:27 +02:00 committed by Alan C. Assis
parent 9f1107862b
commit 1aa32bbc07
10 changed files with 342 additions and 25 deletions

View file

@ -149,6 +149,7 @@
#define ELFOSABI_MODESTO 11 /* Novell Modesto. */
#define ELFOSABI_OPENBSD 12 /* OpenBSD. */
#define ELFOSABI_ARM_AEABI 64 /* ARM EABI */
#define ELFOSABI_ARM_FDPIC 65 /* ARM FDPIC */
#define ELFOSABI_ARM 97 /* ARM */
#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */

View file

@ -37,6 +37,12 @@
#define ELF_PRARGSZ (80) /* Number of chars for args */
/* An architecture that has no FDPIC ABI recognises no FDPIC object. */
#ifndef ELF_IS_FDPIC
# define ELF_IS_FDPIC(ehdr) false
#endif
/****************************************************************************
* Public Types
****************************************************************************/

View file

@ -44,6 +44,19 @@
# define CONFIG_LIBC_ELF_MAXDEPEND 0
#endif
/* A compacting filesystem gives its media address with a pin that holds the
* blocks in place. The loader holds the pin through a file reference,
* because the unload runs on another task.
*
* That reference is taken with file_get() and file_dup2(), which are kernel
* side, so this is for the copy of libc that runs there.
*/
#if defined(CONFIG_FS_PIN) && \
(defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__))
# define HAVE_LIBC_ELF_PIN 1
#endif
#ifndef CONFIG_LIBC_ELF_ALIGN_LOG2
# define CONFIG_LIBC_ELF_ALIGN_LOG2 2
#endif
@ -123,6 +136,7 @@ typedef CODE int (*mod_uninitializer_t)(FAR void *arg);
* nexports - The number of symbols in the exported symbol table.
*/
struct file;
struct symtab_s;
struct mod_info_s
{
@ -252,6 +266,16 @@ struct mod_loadinfo_s
* skip the copy.
*/
/* True if e_ident[EI_OSABI] marked this an FDPIC object. */
bool fdpic;
#ifdef HAVE_LIBC_ELF_PIN
/* The file the pin is held through, handed to the module once it loads. */
FAR struct file *pinfile;
#endif
/* Address environment.
*
* addrenv - This is the handle created by addrenv_allocate() that can be