Skip to content

[4/10] libs/libc/elf: Translate link-time addresses through one place. - #19941

Open
casaroli wants to merge 1 commit into
apache:masterfrom
casaroli:elf-addr-translate
Open

[4/10] libs/libc/elf: Translate link-time addresses through one place.#19941
casaroli wants to merge 1 commit into
apache:masterfrom
casaroli:elf-addr-translate

Conversation

@casaroli

@casaroli casaroli commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

Summary

The ET_DYN path computes run-time addresses from link-time ones in five places, each open-coding the arithmetic, and two of them disagree about how. libelf_relocatedyn() adds textalloc to a relocation's r_offset in one branch and subtracts datasec before adding datastart in the next, while the value translation a few lines down picks between those two forms with an explicit test on datasec.

This collects that into libelf_addr(), which makes the test once: an address below the data segment's link-time base belongs to text, anything at or above it to data.

It changes nothing today. datastart - datasec is textalloc for the way an object is placed now, so both forms reduce to the same arithmetic. They stop being the same once text and data are placed independently, which is what an FDPIC object requires, and having the translation in one function is what makes that possible without auditing every open-coded expression again.

This is the fourth of ten PRs that #19673 is being split into, so each can be reviewed on its own. The first four have no FDPIC content at all and do not depend on each other, so they can merge in any order. The remaining six are the FDPIC work itself, one subsystem each, and each is a no-op with CONFIG_FDPIC off: loader core, ARM relocations, the callback entry points, the exec() path, DT_NEEDED through dlopen(), then documentation and a board configuration. The others are [1/10] #19938, [2/10] #19939 and [3/10] #19940.

Impact

None intended. Pure refactor, no behaviour change, no configuration change.

Testing

Built for mps3-an547:picostest, which is CONFIG_ELF with CONFIG_PIC. tools/checkpatch.sh passes.

Runtime evidence on real hardware follows tomorrow, on an RP2350.

@github-actions

github-actions Bot commented Aug 23, 2026

Copy link
Copy Markdown

MemBrowse Memory Report

s698pm-dkit

@casaroli casaroli changed the title [4/4] libs/libc/elf: Translate link-time addresses through one place. [4/10] libs/libc/elf: Translate link-time addresses through one place. Aug 23, 2026
The ET_DYN path computes run-time addresses from link-time ones in five
places, each open-coding the arithmetic, and two of them disagree about
how: libelf_relocatedyn() adds textalloc to a relocation's r_offset in
one branch and subtracts datasec before adding datastart in the next,
while the value translation a few lines further down picks between those
two forms with an explicit test on datasec.

Collect that into libelf_addr(), which makes the test once: an address
below the data segment's link-time base belongs to text, anything at or
above it to data.

This changes nothing today.  libelf_elfsize() sets

  segpad   = datasec - (text_vaddr + textsize)

and libelf_load() then places

  datastart = textalloc + textsize + segpad

so datastart - datasec is textalloc, and the data branch reduces to
textalloc + vaddr -- exactly what the text branch returns, and exactly
what adding a single load bias did before.  The two forms are the same
arithmetic written twice.

They stop being the same once text and data are placed independently,
which is what an FDPIC object requires: its two PT_LOAD segments are
relocated separately so that the read-only one can be mapped in place on
the media while only the writable one is copied.  Having the translation
in one function is what makes that possible without auditing every
open-coded expression again.

Built for mps3-an547:picostest, which is CONFIG_ELF with CONFIG_PIC, and
boots identically to the same configuration without this change.

Assisted-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@xiaoxiang781216
xiaoxiang781216 marked this pull request as ready for review August 24, 2026 02:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: OS Components OS Components issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants