Skip to content

[8/10] binfmt/elf: Load FDPIC modules through the ELF loader - #20131

Open
casaroli wants to merge 1 commit into
apache:masterfrom
casaroli:fdpic-exec
Open

casaroli wants to merge 1 commit into
apache:masterfrom
casaroli:fdpic-exec

Conversation

@casaroli

@casaroli casaroli commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

[5/10] #19942, [6/10] #20089 and [7/10] #20130 are merged: an FDPIC object is placed, bound and relocated, and the firmware can call back into it. This makes exec() of one work.

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 address; an FDPIC object names it in DT_PLTGOT instead, which the loader has already translated. 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 with its own data base. For a module arriving through dlopen(), libelf_insert() walks the array instead, entering 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 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 while the release happens on another.

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

Impact

Behind CONFIG_FDPIC, which defaults off. With it off, binfmt is what it was.

Testing

mps3-an547:picostest builds on master with CONFIG_FDPIC off and on. With it on, the apps the configuration carries come out as FDPIC objects (OS/ABI: ARM FDPIC), which is what this patch loads.

The constructor and destructor loops enter the module through fdpic_invoke(), which [7/10] defines and which is now in master.

tools/checkpatch.sh -c -u -m -g passes.

Review

Everything below this in the series is merged, so it stands on master by itself.

@github-actions

github-actions Bot commented Sep 13, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report

s698pm-dkit

@github-actions

Copy link
Copy Markdown

🔗 Cross-repo PR dependencies

The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):

CI run: https://github.com/apache/nuttx/actions/runs/34775786256

acassis
acassis previously approved these changes Sep 14, 2026
@acassis
acassis marked this pull request as ready for review September 14, 2026 21:54
@casaroli
casaroli marked this pull request as draft September 15, 2026 10:45
@github-actions

Copy link
Copy Markdown

🔗 Cross-repo PR dependencies

The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):

CI run: https://github.com/apache/nuttx/actions/runs/34957863761

@github-actions

Copy link
Copy Markdown

🔗 Cross-repo PR dependencies

The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):

CI run: https://github.com/apache/nuttx/actions/runs/35060360729

acassis
acassis previously approved these changes Sep 25, 2026
acassis
acassis previously approved these changes Sep 25, 2026
Comment thread libs/libc/elf/elf_remove.c Outdated

if (modp->xipbase == 0)
{
#if defined(CONFIG_ARCH_USE_TEXT_HEAP)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#ifdef CONFIG_ARCH_USE_TEXT_HEAP

Comment thread libs/libc/elf/elf_remove.c Outdated
* unloading thread does not carry.
*/

gotbase = modp->fdpic ? modp->gotbase : 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why need? gotbase should be zero if fdpic equals false.

Comment thread libs/libc/elf/elf_remove.c Outdated
for (i = 0; i < modp->nfini; i++)
{
array[i]();
fdpic_call(array[i], gotbase);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotbase->modp->gotbase

Comment thread libs/libc/elf/elf_insert.c Outdated
* none of its own.
*/

gotbase = loadinfo.fdpic ? loadinfo.gotbase : 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Comment thread libs/libc/elf/elf_bind.c Outdated
case DT_PLTRELSZ:
reldata.relsz[I_PLT] = dyn[i].d_un.d_val;
break;
case DT_NEEDED:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's move to DT_NEEDED patch

Comment thread include/nuttx/fdpic.h Outdated
*
****************************************************************************/

static inline void fdpic_call(CODE void (*fn)(void), uintptr_t got)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we pass arg like fdpic_invoke

Comment thread include/nuttx/lib/elf.h Outdated
uint16_t nsect; /* Number of entries in sectalloc array */
#endif
int dynamic; /* Module is a dynamic shared object */
bool fdpic; /* Module is an FDPIC object: its two

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove?

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: BINFMT Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants