Repository navigation
build: pack the relative relocations as RELR - #488
Draft
RyanTheTide wants to merge 1 commit into
Draft
RyanTheTide wants to merge 1 commit into
RyanTheTide wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem. The
CHAINLOADING=1 SPTM_VIRT=1build (clang, -Oz) does not link on main:_file_vital_endis 0x12c718, past aura-raw.ld's_file_limitof 0x120000. 132 KiB of that file part is.rela.dyn(24 bytes for each relocated pointer).Change. Link with
-z pack-relative-relocswhen the linker supports it. The Makefile tests$(LD)with--fatal-warnings, so a linker that cannot pack relocations keeps plain RELA.start.Sapplies the new.relr.dynwithapply_relr()(startup.c, standard RELR encoding), then runsapply_rela()as before for anything left unpacked. Both linker scripts place.relr.dynbetween.rodataand.rela.dyn._file_limitis unchanged.Testing.
_file_vital_endbefore -> after:tests/python/test_relr.py(4 cases).Still to run. Production Stage 1 and Stage 2 boot rows with a RELR image on the reference Macs (normal boot, an M3
stage2_relocatesmove, arun_basechainload), and the reset paths (CPU resets, s2r wake). Draft until those are done.Details for reviewers
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.pychecks both tables.validate_legacy_boot.pyaccepts an image with no.rela.dynentries.stage1_room.pynames the new section._starton 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.