binfmt/elf: Load FDPIC modules through the ELF loader.

exec() of an FDPIC module now works.  The loader already places such an
object and binds it; what was missing is everything binfmt has to carry
across from the load to the running task.

The task needs the module's data base in its PIC base register.  binfmt
builds a D-Space for any object with a GOT, taking the base from the .got
section address; an FDPIC object names it in DT_PLTGOT instead, which the
loader has already translated, so the two are the same idea reached by
different routes and both are what up_initial_state() installs.

Constructors are not binfmt's business.  A module carries its own crt0,
which walks .init_array on the task that runs the module and then calls
main, so they run in the module's own context and with its own data base.
For a module that arrives through dlopen(), libelf_insert() walks the array
instead, and it enters each entry through fdpic_invoke() because a
descriptor resolved on the calling task carries the wrong base.

The read-only segment of a module that executes in place is held by a
filesystem pin.  The load takes it, and the module owns it from the point
where nothing can fail any more; it is given back when the task that runs
the module exits.  The pin is held through a reference to the file rather
than a descriptor, because the descriptor belongs to the task that called
the loader and the release happens on another one.

libelf_remove() and libelf_uninit() give back what an FDPIC module holds:
the pin, and the writable segment, while the read-only one is media rather
than an allocation and must not be freed.

Built for mps3-an547:picostest with CONFIG_FDPIC both ways.

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-25 11:14:11 +02:00 • committed by Alan C. Assis
parent 3b301b4844
commit 7a006e7ef6
6 changed files with 116 additions and 5 deletions

View file

@ -159,6 +159,36 @@ static inline void fdpic_invoke(uintptr_t arg,
}
}
/****************************************************************************
* Name: fdpic_call
*
* Description:
* Call a function of a module the caller is not running in, such as a
* constructor of a module being loaded or a destructor of one being
* unloaded, with the data base that function needs.
*
* A zero base means the function is not a module's, or the caller already
* carries the right one, and it is called directly. A non-FDPIC object
* has a GOT too, so the caller decides which base to pass, not this.
*
* Input Parameters:
* arg - The one word argument.
* fn - The function to call.
* got - The data base to enter it with, or zero.
*
****************************************************************************/
static inline void fdpic_call(uintptr_t arg, CODE void (*fn)(void),
uintptr_t got)
{
struct fdpic_desc_s desc;
desc.entry = (uintptr_t)fn;
desc.got = got;
fdpic_invoke(arg, &desc);
}
#else
# define fdpic_base() (0)
@ -167,6 +197,8 @@ static inline void fdpic_invoke(uintptr_t arg,
((desc)->entry = (uintptr_t)(fn), (desc)->got = 0)
# define fdpic_invoke(arg, desc) \
(((CODE void (*)(uintptr_t))(desc)->entry)(arg))
# define fdpic_call(arg, fn, got) \
((void)(got), ((CODE void (*)(uintptr_t))(fn))(arg))
#endif /* CONFIG_FDPIC */

View file

@ -190,6 +190,19 @@ struct module_s
uint16_t nsect; /* Number of entries in sectalloc array */
#endif
int dynamic; /* Module is a dynamic shared object */
uintptr_t gotbase; /* An FDPIC object's data base, to
* enter its destructors with. Zero
* for anything else, so it also says
* which kind of object this is: an
* FDPIC one placed its two segments
* separately and its text may be
* media rather than an allocation
*/
#ifdef HAVE_LIBC_ELF_PIN
FAR struct file *pinfile; /* Holds the XIP pin on the text until
* the module is unloaded
*/
#endif
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MODULE)
size_t textsize; /* Size of the kernel .text memory allocation */
size_t datasize; /* Size of the kernel .bss/.data memory allocation */