Skip to content

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

Merged
acassis merged 2 commits into
apache:masterfrom
casaroli:elf-addr-translate
Aug 27, 2026
Merged

[4/10] libs/libc/elf: Translate link-time addresses through one place.#19941
acassis merged 2 commits 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 the PRs that #19673 is being split into, so each can be reviewed on its own. [1/10] #19938 is withdrawn: the bug it described is not real, because a relocatable module already runs its constructors from crt0. [2/10] #19939 is merged. [3/10] #19940 is independent of this one and they can merge in any order. The rest are the FDPIC work itself, one subsystem each, and each is a no-op with CONFIG_FDPIC off: loader core [5/10] #19942, ARM relocations, the callback entry points, the exec() path, DT_NEEDED through dlopen(), then documentation and a board configuration.

Impact

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

Testing

Built for mps3-an547:picostest, which is CONFIG_ELF with CONFIG_PIC.

There is no hardware for this series, so the runtime evidence is emulated. mps2-an500:xipfs runs the whole execute-in-place loader under QEMU with no board, and that is where the module load path is exercised.

@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
@casaroli
casaroli force-pushed the elf-addr-translate branch from 79ed0b4 to afda079 Compare August 23, 2026 21:34
@xiaoxiang781216
xiaoxiang781216 marked this pull request as ready for review August 24, 2026 02:12
@casaroli
casaroli force-pushed the elf-addr-translate branch from afda079 to c1f458c Compare August 26, 2026 15:54
@github-actions github-actions Bot added Size: M The size of the change in this PR is medium and removed Size: S The size of the change in this PR is small labels Aug 26, 2026
@xiaoxiang781216

Copy link
Copy Markdown
Contributor

ci was fixed, please rebase your change to the last master. @casaroli

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>
CI feeds nxstyle the diff hunks with three lines of context, so style errors
that are older than this change, in the lines around the hunks, fail the
check job.  They are a switch body indented two columns too deep, an
initializer brace one level in, and two declarations with no blank line
after them.

Whitespace only, no change in behaviour.

Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@casaroli
casaroli force-pushed the elf-addr-translate branch from a99a8de to 35be954 Compare August 27, 2026 12:33
@acassis
acassis merged commit e662d52 into apache:master Aug 27, 2026
53 checks passed
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: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants