Skip to content

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

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

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

depends-on: [/pull/20130]

Summary

[5/10] #19942 and [6/10] #20089 are merged: an FDPIC object is placed, bound and relocated. [7/10] #20130 lets the firmware 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 with CONFIG_FDPIC off on master alone, and with it on, on top of [7/10].

The constructor and destructor loops enter the module through fdpic_invoke(), which [7/10] defines. They are compiled only under CONFIG_FDPIC, so with it off this one still stands alone on master.

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

Review

[6/10] is merged. This is the point where [7/10] and this one first have to work together, so it wants reading beside [7/10].

@github-actions

github-actions Bot commented Sep 13, 2026

Copy link
Copy Markdown

MemBrowse Memory Report

arduino-mega2560

  • flash: .data +16 B (+0.0%, 65,144 B / 262,144 B, total: 25% used)
  • sram: .data +16 B (+0.6%, 2,718 B / 8,192 B, total: 33% used)

esp32-devkitc

  • ROM: .dram0.data +16 B, .flash.rodata +12 B (+0.0%, 124,932 B / 4,194,272 B, total: 3% used)
  • dram0_0_seg: .dram0.data +16 B (+0.2%, 9,932 B / 180,736 B, total: 5% used)
  • drom0_0_seg: .flash.rodata +12 B (+0.1%, 13,472 B / 4,194,272 B, total: 0% used)

hifive1-revb

  • flash: .data +8 B, .text +16 B (+0.0%, 83,748 B / 4,194,304 B, total: 2% used)
  • sram: .bss -16 B, .data +8 B (-0.2%, 3,956 B / 16,384 B, total: 24% used)

mirtoo

  • kseg0_progmem: .data +4 B, .text +12 B (+0.0%, 67,788 B / 131,072 B, total: 52% used)
  • kseg1_datamem: .data +4 B (+0.2%, 2,588 B / 32,256 B, total: 8% used)

qemu-armv8a

  • Code: .rodata +16 B, .text.qsort -896 B (+0.0%, 344,384 B)
  • Data: .data.g_version +2 B (+0.0%, 77,476 B)

qemu-intel64

  • Code: .text +16 B (+0.0%, 8,659,785 B)

rx65n-rsk2mb

  • RAM: .data +4 B (+0.1%, 6,590 B / 262,144 B, total: 3% used)
  • ROM: .rodata +16 B (+0.0%, 86,912 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text +256 B (+0.1%, 367,936 B)

stm32-nucleo-f103rb

  • flash: .text +16 B (+0.0%, 34,572 B / 131,072 B, total: 26% used)

@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

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>
@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

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.

2 participants