Skip to content

build: pack the relative relocations as RELR - #488

Draft
RyanTheTide wants to merge 1 commit into
mainfrom
build/relr-relocs
Draft

RyanTheTide wants to merge 1 commit into
mainfrom
build/relr-relocs

Conversation

@RyanTheTide

@RyanTheTide RyanTheTide commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Problem. The CHAINLOADING=1 SPTM_VIRT=1 build (clang, -Oz) does not link on main: _file_vital_end is 0x12c718, past aura-raw.ld's _file_limit of 0x120000. 132 KiB of that file part is .rela.dyn (24 bytes for each relocated pointer).

Change. Link with -z pack-relative-relocs when the linker supports it. The Makefile tests $(LD) with --fatal-warnings, so a linker that cannot pack relocations keeps plain RELA. start.S applies the new .relr.dyn with apply_relr() (startup.c, standard RELR encoding), then runs apply_rela() as before for anything left unpacked. Both linker scripts place .relr.dyn between .rodata and .rela.dyn. _file_limit is unchanged.

Testing.

  • Builds, _file_vital_end before -> after:
    • SPTM_VIRT -Oz: 0x12c718 (no link) -> 0x10c718.
    • ESP Stage 2 release: 0x11c0d0 -> 0x10c0d0.
    • Default Stage 1: 0x10c000 -> 0xfc000.
    • esp-stage2-size CI line: 0xc6008 -> 0xb6008.
    • Every image is 64 KiB smaller, or 128 KiB for SPTM_VIRT.
  • Same objects linked with RELA and with RELR, both relocated at one base: the relocated words are identical (lld and GNU ld). New tests/python/test_relr.py (4 cases).
  • CI pytest set: 1124 -> 1128 passed.
  • Hardware, J473 (M2 Mac mini, T8112): SPTM-virt images built with RELR ran through chainload, the proxy, stage-2 setup, SMP guest reset setup and SPTM entry (rows r14-r18). A RELA build of the same tree behaved identically (r20).

Still to run. Production Stage 1 and Stage 2 boot rows with a RELR image on the reference Macs (normal boot, an M3 stage2_relocates move, a run_base chainload), and the reset paths (CPU resets, s2r wake). Draft until those are done.

Details for reviewers
  • Relocations run exactly once per boot, on a fresh image: after the run_base / stage2_relocates copies, before .bss and stack use. CPU resets, s2r wakes and SPTM-virt guest resets enter cpu_reset, not _start. Adding the base is not idempotent, so this matters; no path relocates the same image in memory twice.
  • proxyclient/aura/loadobjs.py checks both tables. validate_legacy_boot.py accepts an image with no .rela.dyn entries. stage1_room.py names the new section.
  • Gain in data headroom (stage1_room): ESP release 16176 -> 81712 B, plain 81920 -> 147456 B.
  • Risks: a future path that re-enters _start on an already-relocated image would corrupt pointers (RELA tolerated that). A linker without RELR silently keeps RELA; the SPTM_VIRT build then fails the limit check, as it does today.
  • Evidence (lab-internal): build logs and comparison tools for each line; the J473 rows are in the SPTM-virt lane notes.

Every pointer aura relocates at boot had a 24-byte RELA entry in
.rela.dyn, which aura-raw.ld places in the file part below _file_limit.
The CHAINLOADING=1 SPTM_VIRT=1 build (clang, -Oz) had 2828 of them,
0x10920 bytes, and its _file_vital_end reached 0x12c718, past the
0x120000 limit, so it did not link.

Link with -z pack-relative-relocs when the linker can: the Makefile
probes $(LD) with a one-symbol link under --fatal-warnings (LLD 15 and
later and a GNU ld with AArch64 DT_RELR support pass; an older linker
that only warns about the -z value does not) and adds the flag to
LDFLAGS. The same 2828 relocations then take 0x440 bytes of .relr.dyn,
and .data with everything after it moves down 128 KiB in that build.
With a linker that cannot pack them, nothing changes.

RELR stores the link-time value in the word itself and the loader adds
the base, while RELA sets base + addend. start.S now passes
[_relr_start, _relr_end) to the new apply_relr() (startup.c, the
standard encoding: an even entry names a word, an odd one is a bitmap
of the next 63 words) and then calls apply_rela() as before, for
anything the linker left unpacked. Both run at the same point, after
the run_base and stage2_relocates copies, which copy the image before
it is relocated, with the stack set up and the MMU off. Adding the base
is not idempotent, so a second pass over the same image would be wrong.
No path does one: CPU resets, s2r wakes and SPTM-virt guest resets
enter at cpu_reset, not _start, and every chainload or proxy reload
starts from a fresh copy of the image file.

Both linker scripts get a .relr.dyn output section between .rodata and
.rela.dyn, with _relr_start/_relr_end, so it is not an orphan and adds
no 64 KiB step. aura.ld gets it too, so aura.macho shrinks as well.
proxyclient's loadobjs.py checks both tables against the target,
validate_legacy_boot.py takes an image with no .rela.dyn entries (it
runs the image at its link address, where a packed word already holds
its value), and stage1_room.py names the section.

Sizes, before -> after (_file_vital_end; aura.bin):
  clang -Oz CHAINLOADING=1 SPTM_VIRT=1:     0x12c718 (no link) -> 0x10c718
  RELEASE=1 CHAINLOADING=1 ESP_STAGE2=1 -Os: 0x11c0d0 -> 0x10c0d0;
                                            3997696 -> 3932160
  default Stage 1:                          0x10c000 -> 0xfc000;
                                            3932160 -> 3866624
  esp-stage2-size CI line:                  0xc6008 -> 0xb6008;
                                            3801088 -> 3735552
  GNU ld, CHAINLOADING=1 -Os:               0x11c210 -> 0x10c210

The relocated image does not change: linking the same objects of the
SPTM_VIRT build once with RELA and once with RELR, relocating both at
one base (RELR with apply_relr() built for the host) gives the same
relocated words and targets, section for section, with only PC-relative
instruction immediates and the load header's _image_footprint
differing, as .data moved. The same holds for a GNU ld link.

tests/python/test_relr.py links a small image with runs of pointers,
gaps longer than one bitmap and pointers in .text and .rodata with each
AArch64 linker and both scripts, with and without packing, and checks
that RELR relocates it exactly as RELA does, also with startup.c's
apply_relr() compiled for the host. test_rela_layout.py's assemble()
takes the source to assemble.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant