* [PATCH 1/2] arm64: relocate: convert relocate_code to C
@ 2026-11-25 7:41 Ilias Apalodimas
2026-11-25 7:41 ` [PATCH 2/2] arm64: Add RELR relocs Ilias Apalodimas
` (2 more replies)
0 siblings, 3 replies; 25+ messages in thread
From: Ilias Apalodimas @ 2026-11-25 7:41 UTC (permalink / raw)
To: trini, richard.henderson; +Cc: casey.connolly, Ilias Apalodimas, u-boot
relocate_code() on arm64 is called from _main with sp and gd already
pointing at the relocated area and lr pointing to the relocated copy of
relocation_return. By that time pie_fixup has already run, if
POSITION_INDEPENDENT is enabled, the image is fixed up and plain C works
fine, like the rest of board_init_f(). So let's convert it to C which is
much easier to read and maintain.
While at it change the behaviour and order of cache maintenance.
Invalidate the I-cache after the D-cache clean instead of before
and add a 'dsb' between 'ic iallu' and the 'isb', which is the
architecturally required sequence.
Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
---
arch/arm/lib/relocate_64.S | 96 --------------------------------------
arch/arm/lib/relocate_64.c | 82 ++++++++++++++++++++++++++++++++
2 files changed, 82 insertions(+), 96 deletions(-)
delete mode 100644 arch/arm/lib/relocate_64.S
create mode 100644 arch/arm/lib/relocate_64.c
diff --git a/arch/arm/lib/relocate_64.S b/arch/arm/lib/relocate_64.S
deleted file mode 100644
index 495a5f03ba76..000000000000
--- a/arch/arm/lib/relocate_64.S
+++ /dev/null
@@ -1,96 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0+ */
-/*
- * relocate - common relocation function for AArch64 U-Boot
- *
- * (C) Copyright 2013
- * Albert ARIBAUD <albert.u.boot@aribaud.net>
- * David Feng <fenghua@phytium.com.cn>
- */
-
-#include <asm-offsets.h>
-#include <config.h>
-#include <elf.h>
-#include <linux/linkage.h>
-#include <asm/macro.h>
-
-/*
- * void relocate_code(addr_moni)
- *
- * This function relocates the monitor code.
- * x0 holds the destination address.
- */
-ENTRY(relocate_code)
- stp x29, x30, [sp, #-32]! /* create a stack frame */
- mov x29, sp
- str x0, [sp, #16]
- /*
- * Copy u-boot from flash to RAM
- */
- adrp x1, __image_copy_start /* x1 <- address bits [31:12] */
- add x1, x1, :lo12:__image_copy_start/* x1 <- address bits [11:00] */
- subs x9, x0, x1 /* x9 <- Run to copy offset */
- b.eq relocate_done /* skip relocation */
- /*
- * Don't ldr x1, __image_copy_start here, since if the code is already
- * running at an address other than it was linked to, that instruction
- * will load the relocated value of __image_copy_start. To
- * correctly apply relocations, we need to know the linked value.
- *
- * Linked &__image_copy_start, which we know was at
- * CONFIG_TEXT_BASE, which is stored in _TEXT_BASE, as a non-
- * relocated value, since it isn't a symbol reference.
- */
- ldr x1, _TEXT_BASE /* x1 <- Linked &__image_copy_start */
- subs x9, x0, x1 /* x9 <- Link to copy offset */
-
- adrp x1, __image_copy_start /* x1 <- address bits [31:12] */
- add x1, x1, :lo12:__image_copy_start/* x1 <- address bits [11:00] */
- adrp x2, __image_copy_end /* x2 <- address bits [31:12] */
- add x2, x2, :lo12:__image_copy_end /* x2 <- address bits [11:00] */
-copy_loop:
- ldp x10, x11, [x1], #16 /* copy from source address [x1] */
- stp x10, x11, [x0], #16 /* copy to target address [x0] */
- cmp x1, x2 /* until source end address [x2] */
- b.lo copy_loop
- str x0, [sp, #24]
-
- /*
- * Fix .rela.dyn relocations
- */
- adrp x2, __rel_dyn_start /* x2 <- address bits [31:12] */
- add x2, x2, :lo12:__rel_dyn_start /* x2 <- address bits [11:00] */
- adrp x3, __rel_dyn_end /* x3 <- address bits [31:12] */
- add x3, x3, :lo12:__rel_dyn_end /* x3 <- address bits [11:00] */
-fixloop:
- ldp x0, x1, [x2], #16 /* (x0,x1) <- (SRC location, fixup) */
- ldr x4, [x2], #8 /* x4 <- addend */
- and x1, x1, #0xffffffff
- cmp x1, #R_AARCH64_RELATIVE
- bne fixnext
-
- /* relative fix: store addend plus offset at dest location */
- add x0, x0, x9
- add x4, x4, x9
- str x4, [x0]
-fixnext:
- cmp x2, x3
- b.lo fixloop
-
-relocate_done:
- switch_el x1, 3f, 2f, 1f
- bl hang
-3: mrs x0, sctlr_el3
- b 0f
-2: mrs x0, sctlr_el2
- b 0f
-1: mrs x0, sctlr_el1
-0: tbz w0, #2, 5f /* skip flushing cache if disabled */
- tbz w0, #12, 4f /* skip invalidating i-cache if disabled */
- ic iallu /* i-cache invalidate all */
- isb sy
-4: ldp x0, x1, [sp, #16]
- bl __asm_flush_dcache_range
- bl __asm_flush_l3_dcache
-5: ldp x29, x30, [sp],#32
- ret
-ENDPROC(relocate_code)
diff --git a/arch/arm/lib/relocate_64.c b/arch/arm/lib/relocate_64.c
new file mode 100644
index 000000000000..6f7a50f2264e
--- /dev/null
+++ b/arch/arm/lib/relocate_64.c
@@ -0,0 +1,82 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * relocate - common relocation function for AArch64
+ *
+ * Adapted from the original asm from:
+ * Albert ARIBAUD <albert.u.boot@aribaud.net>
+ * David Feng <fenghua@phytium.com.cn>
+ *
+ * Copyright (c) 2026, Ilias Apalodimas <ilias.apalodimas@linaro.org>
+ */
+
+#include <elf.h>
+#include <init.h>
+#include <linux/compiler.h>
+#include <linux/string.h>
+#include <linux/types.h>
+#include <asm/sections.h>
+#include <asm/system.h>
+
+/**
+ * relocate_code() - copy U-Boot to its final location and fix it up
+ *
+ * @dest: destination address, gd->relocaddr
+ *
+ * This is entered from _main in crt0_64.S with sp and gd already
+ * pointing to the relocated area and with lr pointing to the relocated
+ * copy of relocation_return, so returning from here continues execution
+ * in the new image.
+ *
+ * Constraints for this function, since it runs from the old image and
+ * before BSS is usable:
+ * - It must not touch BSS. On arm64 .bss overlays .rela.dyn
+ * - It must not rely on anything that needs relocating in the destination.
+ * Symbol addresses are generated PC-relative (adrp/add, -fno-pic), so they
+ * resolve to the runtime location of the currently executing image, which
+ * is already correct, either because we run at CONFIG_TEXT_BASE or because
+ * pie_fixup has fixed up the running image in place.
+ * - It must not be instrumented.
+ */
+void notrace relocate_code(ulong dest)
+{
+ u64 src = (u64)__image_copy_start;
+ u64 size = (u64)__image_copy_end - src;
+ const Elf64_Rela *rela = (const Elf64_Rela *)__rel_dyn_start;
+ const Elf64_Rela *rela_end = (const Elf64_Rela *)__rel_dyn_end;
+ u64 sctlr;
+ u64 offset;
+
+ if (src == dest)
+ return;
+ memcpy((void *)dest, (void *)src, size);
+ /*
+ * r_offset and r_addend are link-time values, so the offset to apply
+ * is link-to-copy
+ */
+ offset = dest - CONFIG_TEXT_BASE;
+
+ /*
+ * We have a makefile check that aborts if reloc entries are not
+ * R_AARCH64_RELATIVE.
+ */
+ for (; rela < rela_end; rela++) {
+ if (ELF64_R_TYPE(rela->r_info) != R_AARCH64_RELATIVE)
+ continue;
+ *(u64 *)(rela->r_offset + offset) = rela->r_addend + offset;
+ }
+
+ sctlr = get_sctlr();
+ if (sctlr & CR_C) {
+ __asm_flush_dcache_range(dest, dest + size);
+ __asm_flush_l3_dcache();
+ } else {
+ /* Non-cacheable stores need to complete before we fetch new code */
+ asm volatile("dsb sy" : : : "memory");
+ }
+
+ if (sctlr & CR_I)
+ asm volatile("ic iallu\n\tdsb nsh\n\tisb" : : : "memory");
+
+ /* Always discard anything already fetched/prefetched */
+ asm volatile("isb" : : : "memory");
+}
--
2.53.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* [PATCH 2/2] arm64: Add RELR relocs
2026-11-25 7:41 [PATCH 1/2] arm64: relocate: convert relocate_code to C Ilias Apalodimas
@ 2026-11-25 7:41 ` Ilias Apalodimas
2026-11-25 17:20 ` Quentin Schulz
2026-11-26 16:35 ` Richard Henderson
2026-11-25 17:07 ` [PATCH 1/2] arm64: relocate: convert relocate_code to C Quentin Schulz
2026-11-26 16:11 ` Richard Henderson
2 siblings, 2 replies; 25+ messages in thread
From: Ilias Apalodimas @ 2026-11-25 7:41 UTC (permalink / raw)
To: trini, richard.henderson; +Cc: casey.connolly, Ilias Apalodimas, u-boot
Linking U-Boot with -pie emits an R_AARCH64_RELATIVE entry for every
pointer that needs fixing up. Each Elf64_Rela entry is 24 bytes, so the
.rela.dyn section ends up being quite large.
Modern toolchains support packing relative relocations in RELR format
via '-z pack-relative-relocs'. Instead of offset/info/addend tuples,
RELR stores a stream of 64-bit words. An even word is the link-time
address of a word to relocate and an odd word is a bitmap describing
which of the 63 words following the previous address need relocating.
The addend is the value already stored in the place.
Compiling for QEMU with RELA vs RELR looks something like this:
$~ readelf -SW u-boot | grep relr
[ 9] .relr.dyn RELR 000000000016f970 170970 000960 08 A 0 0 8
$~ readelf -SW u-boot.old | grep rela
[ 7] .rela.dyn RELA 0000000000115110 116110 0184f8 18 A 0 0 8
which is ~98% decrease in the space reloc entries need.
So let's add a Kconfig option and teach U-Boot RELR.
- pass '-z pack-relative-relocs' to the linker and add .relr.dyn
in the linker script and the objcopy'ed binary. relr.dyn is still
overlayed by .bss
- skip checkarmreloc, since we now expect R_AARCH64_RELATIVE entries
to be packed in .rel*.dyn and older tools don't decode them
properly
- process .relr.dyn in pie_fixup. It's worth noting that efi runtime
relocs, due to how ld emits RELR, are now packed in .relr.dyn and
efi_runtime_relocate() doesn't have to relocate anything.
A sideeffect is that we can't support SetVirtualAddressMap and ConvertPointer
for EFI, but some architectures are already ommitting this call
(e.g Linux on arm64, openBSD doesn't call it at all). [0]
- process .relr.dyn in relocate_code(). Note that the places are
link-time addresses, so we still need the link-to-copy offset to find
them, but the stored value has already been shifted to the run address
by pie_fixup, so it needs to be adjusted by the run-to-copy offset
[0] EFI runtime services relocate themselves when the OS calls
SetVirtualAddressMap, using the relocations between
__efi_runtime_rel_start and __efi_runtime_rel_stop. Those are collected
by the linker script from the .rela sections of the runtime code.
RELR packing, however, is done by the linker as a last step and all
relative relocations end up in a single .relr.dyn section for the entire
image. There's no way to emit an equivalent __efi_runtime_relr_start
marker for the runtime region, or emit RELA relocations for the EFI
runtime, so stop advertising SetVirtualAddressMap and ConvertPointer
in the EFI_RT_PROPERTIES_TABLE when RELR is enabled.
Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
---
Kconfig | 14 +++++++++++++
arch/arm/config.mk | 10 +++++++--
arch/arm/cpu/armv8/start.S | 38 ++++++++++++++++++++++++++++++++++
arch/arm/cpu/armv8/u-boot.lds | 9 ++++++++
arch/arm/lib/relocate_64.c | 28 +++++++++++++++++++++++++
include/asm-generic/sections.h | 3 +++
lib/efi_loader/efi_runtime.c | 9 +++++---
7 files changed, 106 insertions(+), 5 deletions(-)
diff --git a/Kconfig b/Kconfig
index c8cfbf5d5de2..ebe7b30d7b78 100644
--- a/Kconfig
+++ b/Kconfig
@@ -40,6 +40,20 @@ config COMPILE_TEST
here. If you are a user/distributor, say N here to exclude useless
drivers to be distributed.
+config RELR
+ bool "Use RELR packed relative relocations"
+ depends on ARM64
+ help
+ RELR stores relative relocations as a stream of 64-bit words: an
+ even word is the link-time address of a word to relocate, and an
+ odd word is a bitmap covering the 63 words that follow the previous
+ address. This usually leads to ~95% compression of emitted relocation
+ entries.
+ Due to the placement of the .relr.dyn section and the way ld
+ creates it we cannot currently relocate EFI functions at runtime.
+ SetVirtualAddressMap and ConvertPointer for EFI are marked as
+ unsupported.
+
config WERROR
bool "Compile U-Boot with warnings as errors"
default COMPILE_TEST
diff --git a/arch/arm/config.mk b/arch/arm/config.mk
index bce9a31e966b..ad73f728f7a2 100644
--- a/arch/arm/config.mk
+++ b/arch/arm/config.mk
@@ -111,6 +111,9 @@ endif
# needed for relocation
LDFLAGS_u-boot += -pie
+ifeq ($(CONFIG_RELR),y)
+LDFLAGS_u-boot += -z pack-relative-relocs
+endif
ifeq ($(CONFIG_ARM64),y)
# U-Boot uses fixed 4K granules, so we force the linker to match.
@@ -139,8 +142,11 @@ endif
endif
ifneq ($(CONFIG_XPL_BUILD),y)
-# Check that only R_ARM_RELATIVE relocations are generated.
+# Check that only R_ARM_RELATIVE relocations are generated
+# unless we compile with RELR
+ifneq ($(CONFIG_RELR),y)
INPUTS-y += checkarmreloc
+endif
# The movt / movw can hardcode 16 bit parts of the addresses in the
# instruction. Relocation is not supported for that case, so disable
# such usage by requiring word relocations.
@@ -152,7 +158,7 @@ endif
ifdef CONFIG_ARM64
OBJCOPYFLAGS += -j .text -j .secure_text -j .secure_data -j .rodata -j .data \
-j __u_boot_list -j .rela.dyn -j .got -j .got.plt \
- -j .binman_sym_table -j .text_rest
+ -j .binman_sym_table -j .text_rest -j .relr.dyn
else
OBJCOPYFLAGS += -j .text -j .secure_text -j .secure_data -j .rodata -j .hash \
-j .data -j .got -j .got.plt -j __u_boot_list -j .rel.dyn \
diff --git a/arch/arm/cpu/armv8/start.S b/arch/arm/cpu/armv8/start.S
index 40c342e97e9b..c43303eb0cf2 100644
--- a/arch/arm/cpu/armv8/start.S
+++ b/arch/arm/cpu/armv8/start.S
@@ -89,6 +89,7 @@ pie_fixup:
add x2, x2, #:lo12:__rel_dyn_start
adrp x3, __rel_dyn_end /* x3 <- Runtime &__rel_dyn_end */
add x3, x3, #:lo12:__rel_dyn_end
+ b pie_skip_reloc /* .rela.dyn may be empty */
pie_fix_loop:
ldp x0, x1, [x2], #16 /* (x0, x1) <- (Link location, fixup) */
ldr x4, [x2], #8 /* x4 <- addend */
@@ -101,6 +102,43 @@ pie_fix_loop:
pie_skip_reloc:
cmp x2, x3
b.lo pie_fix_loop
+
+#ifdef CONFIG_RELR
+ /*
+ * Fix .relr.dyn relocations. Entries hold link-time addresses
+ * while x9 holds the run-vs-link offset and the addend is the value
+ * already stored in the place.
+ */
+ adrp x2, __relr_dyn_start /* x2 <- Runtime &__relr_dyn_start */
+ add x2, x2, #:lo12:__relr_dyn_start
+ adrp x3, __relr_dyn_end /* x3 <- Runtime &__relr_dyn_end */
+ add x3, x3, #:lo12:__relr_dyn_end
+ b pie_relr_next
+pie_relr_loop:
+ ldr x4, [x2], #8 /* x4 <- RELR entry */
+ tbnz x4, #0, pie_relr_bitmap
+ add x5, x4, x9 /* x5 <- Runtime address of the word */
+ ldr x6, [x5]
+ add x6, x6, x9
+ str x6, [x5], #8 /* x5 <- Start of the next bitmap */
+ b pie_relr_next
+pie_relr_bitmap:
+ mov x7, x5
+ lsr x4, x4, #1
+pie_relr_bit:
+ tbz x4, #0, pie_relr_skip_bit
+ ldr x6, [x7]
+ add x6, x6, x9
+ str x6, [x7]
+pie_relr_skip_bit:
+ add x7, x7, #8
+ lsr x4, x4, #1
+ cbnz x4, pie_relr_bit
+ add x5, x5, #(63 * 8) /* Each bitmap covers 63 words */
+pie_relr_next:
+ cmp x2, x3
+ b.lo pie_relr_loop
+#endif
pie_fixup_done:
#endif
diff --git a/arch/arm/cpu/armv8/u-boot.lds b/arch/arm/cpu/armv8/u-boot.lds
index 231ab28b50cd..73f0230629cc 100644
--- a/arch/arm/cpu/armv8/u-boot.lds
+++ b/arch/arm/cpu/armv8/u-boot.lds
@@ -149,6 +149,15 @@ SECTIONS
. = ALIGN(8);
}
+#ifdef CONFIG_RELR
+ .relr.dyn ALIGN (8) : {
+ __relr_dyn_start = .;
+ *(.relr.dyn)
+ __relr_dyn_end = .;
+ . = ALIGN(8);
+ }
+#endif
+
_end = .;
/*
diff --git a/arch/arm/lib/relocate_64.c b/arch/arm/lib/relocate_64.c
index 6f7a50f2264e..0572fe2d29f8 100644
--- a/arch/arm/lib/relocate_64.c
+++ b/arch/arm/lib/relocate_64.c
@@ -43,12 +43,19 @@ void notrace relocate_code(ulong dest)
u64 size = (u64)__image_copy_end - src;
const Elf64_Rela *rela = (const Elf64_Rela *)__rel_dyn_start;
const Elf64_Rela *rela_end = (const Elf64_Rela *)__rel_dyn_end;
+#if IS_ENABLED(CONFIG_RELR)
+ const u64 *relr = (const u64 *)__relr_dyn_start;
+ const u64 *relr_end = (const u64 *)__relr_dyn_end;
+ u64 run_to_copy;
+ u64 *place = NULL;
+#endif
u64 sctlr;
u64 offset;
if (src == dest)
return;
memcpy((void *)dest, (void *)src, size);
+
/*
* r_offset and r_addend are link-time values, so the offset to apply
* is link-to-copy
@@ -65,6 +72,27 @@ void notrace relocate_code(ulong dest)
*(u64 *)(rela->r_offset + offset) = rela->r_addend + offset;
}
+#if IS_ENABLED(CONFIG_RELR)
+ /*
+ * RELR entries hold link-time addresses, so the location is found
+ * with the link-to-copy offset. The addend is the value already
+ * stored in the word, which pie_fixup has shifted to the run
+ * address, so it is adjusted by the run-to-copy offset.
+ */
+ run_to_copy = dest - src;
+ for (; relr < relr_end; relr++) {
+ if ((*relr & 1) == 0) {
+ place = (u64 *)(*relr + offset);
+ *place++ += run_to_copy;
+ } else {
+ for (u64 *p = place, r = *relr >> 1; r; p++, r >>= 1)
+ if (r & 1)
+ *p += run_to_copy;
+ place += 63;
+ }
+ }
+#endif
+
sctlr = get_sctlr();
if (sctlr & CR_C) {
__asm_flush_dcache_range(dest, dest + size);
diff --git a/include/asm-generic/sections.h b/include/asm-generic/sections.h
index 48bd4fa86043..0e957feacc4d 100644
--- a/include/asm-generic/sections.h
+++ b/include/asm-generic/sections.h
@@ -41,6 +41,9 @@ extern char __ctors_start[], __ctors_end[];
extern char __efi_runtime_rel_start[], __efi_runtime_rel_stop[];
extern char __efi_runtime_start[], __efi_runtime_stop[];
+#ifdef CONFIG_RELR
+extern char __relr_dyn_start[], __relr_dyn_end[];
+#endif
/* function descriptor handling (if any). Override
* in asm/sections.h */
#ifndef dereference_function_descriptor
diff --git a/lib/efi_loader/efi_runtime.c b/lib/efi_loader/efi_runtime.c
index 73d4097464c0..1bcd4b697b25 100644
--- a/lib/efi_loader/efi_runtime.c
+++ b/lib/efi_loader/efi_runtime.c
@@ -127,9 +127,12 @@ efi_status_t efi_init_runtime_supported(void)
rt_table->length = sizeof(struct efi_rt_properties_table);
rt_table->runtime_services_supported =
EFI_RT_SUPPORTED_GET_VARIABLE |
- EFI_RT_SUPPORTED_GET_NEXT_VARIABLE_NAME |
- EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP |
- EFI_RT_SUPPORTED_CONVERT_POINTER;
+ EFI_RT_SUPPORTED_GET_NEXT_VARIABLE_NAME;
+
+ if (!IS_ENABLED(CONFIG_RELR))
+ rt_table->runtime_services_supported |=
+ EFI_RT_SUPPORTED_SET_VIRTUAL_ADDRESS_MAP |
+ EFI_RT_SUPPORTED_CONVERT_POINTER;
if (IS_ENABLED(CONFIG_EFI_VARIABLE_FILE_STORE))
rt_table->runtime_services_supported |=
--
2.53.0
^ permalink raw reply related [flat|nested] 25+ messages in thread* Re: [PATCH 2/2] arm64: Add RELR relocs
2026-11-25 7:41 ` [PATCH 2/2] arm64: Add RELR relocs Ilias Apalodimas
@ 2026-11-25 17:20 ` Quentin Schulz
2026-11-26 12:48 ` Peter Robinson
2026-11-26 16:35 ` Richard Henderson
1 sibling, 1 reply; 25+ messages in thread
From: Quentin Schulz @ 2026-11-25 17:20 UTC (permalink / raw)
To: Ilias Apalodimas, trini, richard.henderson; +Cc: casey.connolly, u-boot
Hi Ilias,
On 11/25/26 8:41 AM, Ilias Apalodimas wrote:
> Linking U-Boot with -pie emits an R_AARCH64_RELATIVE entry for every
> pointer that needs fixing up. Each Elf64_Rela entry is 24 bytes, so the
> .rela.dyn section ends up being quite large.
>
> Modern toolchains support packing relative relocations in RELR format
> via '-z pack-relative-relocs'. Instead of offset/info/addend tuples,
> RELR stores a stream of 64-bit words. An even word is the link-time
> address of a word to relocate and an odd word is a bitmap describing
> which of the 63 words following the previous address need relocating.
> The addend is the value already stored in the place.
>
> Compiling for QEMU with RELA vs RELR looks something like this:
>
> $~ readelf -SW u-boot | grep relr
> [ 9] .relr.dyn RELR 000000000016f970 170970 000960 08 A 0 0 8
> $~ readelf -SW u-boot.old | grep rela
> [ 7] .rela.dyn RELA 0000000000115110 116110 0184f8 18 A 0 0 8
>
> which is ~98% decrease in the space reloc entries need.
>
> So let's add a Kconfig option and teach U-Boot RELR.
> - pass '-z pack-relative-relocs' to the linker and add .relr.dyn
> in the linker script and the objcopy'ed binary. relr.dyn is still
> overlayed by .bss
> - skip checkarmreloc, since we now expect R_AARCH64_RELATIVE entries
> to be packed in .rel*.dyn and older tools don't decode them
> properly
> - process .relr.dyn in pie_fixup. It's worth noting that efi runtime
> relocs, due to how ld emits RELR, are now packed in .relr.dyn and
> efi_runtime_relocate() doesn't have to relocate anything.
> A sideeffect is that we can't support SetVirtualAddressMap and ConvertPointer
> for EFI, but some architectures are already ommitting this call
> (e.g Linux on arm64, openBSD doesn't call it at all). [0]
> - process .relr.dyn in relocate_code(). Note that the places are
> link-time addresses, so we still need the link-to-copy offset to find
> them, but the stored value has already been shifted to the run address
> by pie_fixup, so it needs to be adjusted by the run-to-copy offset
>
> [0] EFI runtime services relocate themselves when the OS calls
> SetVirtualAddressMap, using the relocations between
> __efi_runtime_rel_start and __efi_runtime_rel_stop. Those are collected
> by the linker script from the .rela sections of the runtime code.
> RELR packing, however, is done by the linker as a last step and all
> relative relocations end up in a single .relr.dyn section for the entire
> image. There's no way to emit an equivalent __efi_runtime_relr_start
> marker for the runtime region, or emit RELA relocations for the EFI
> runtime, so stop advertising SetVirtualAddressMap and ConvertPointer
> in the EFI_RT_PROPERTIES_TABLE when RELR is enabled.
>
6.4% size reduction for u-boot.itb with RELR enabled on
tiger-rk3588_defconfig.
$ aarch64-linux-gnu-ld --version
GNU ld version 2.47.20260726
from Fedora 45.
I reached U-Boot proper CLI, which I have been told is sufficient to
test this series, so:
Tested-by: Quentin Schulz <quentin.schulz@cherry.de> # RK3588 Tiger
> Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
> ---
> Kconfig | 14 +++++++++++++
> arch/arm/config.mk | 10 +++++++--
> arch/arm/cpu/armv8/start.S | 38 ++++++++++++++++++++++++++++++++++
> arch/arm/cpu/armv8/u-boot.lds | 9 ++++++++
> arch/arm/lib/relocate_64.c | 28 +++++++++++++++++++++++++
> include/asm-generic/sections.h | 3 +++
> lib/efi_loader/efi_runtime.c | 9 +++++---
> 7 files changed, 106 insertions(+), 5 deletions(-)
>
> diff --git a/Kconfig b/Kconfig
> index c8cfbf5d5de2..ebe7b30d7b78 100644
> --- a/Kconfig
> +++ b/Kconfig
> @@ -40,6 +40,20 @@ config COMPILE_TEST
> here. If you are a user/distributor, say N here to exclude useless
> drivers to be distributed.
>
> +config RELR
> + bool "Use RELR packed relative relocations"
> + depends on ARM64
> + help
> + RELR stores relative relocations as a stream of 64-bit words: an
> + even word is the link-time address of a word to relocate, and an
> + odd word is a bitmap covering the 63 words that follow the previous
> + address. This usually leads to ~95% compression of emitted relocation
> + entries.
> + Due to the placement of the .relr.dyn section and the way ld
> + creates it we cannot currently relocate EFI functions at runtime.
> + SetVirtualAddressMap and ConvertPointer for EFI are marked as
> + unsupported.
> +
> config WERROR
> bool "Compile U-Boot with warnings as errors"
> default COMPILE_TEST
> diff --git a/arch/arm/config.mk b/arch/arm/config.mk
> index bce9a31e966b..ad73f728f7a2 100644
> --- a/arch/arm/config.mk
> +++ b/arch/arm/config.mk
> @@ -111,6 +111,9 @@ endif
>
> # needed for relocation
> LDFLAGS_u-boot += -pie
> +ifeq ($(CONFIG_RELR),y)
> +LDFLAGS_u-boot += -z pack-relative-relocs
> +endif
>
suggestion: make this transparent to the user
We've talked about this in private but wanted to put it in writing so
it's available on the mailing list archive. I think it makes more sense
to always enable the option (if it's safe!), if the linker supports it.
According to
https://rfc.archlinux.page/0023-pack-relative-relocs/#motivation, that
is GNU ld 2.36 and LLVM 15. We can use the same mechanism as in the
kernel, c.f. CC_HAS_UBSAN_BOUNDS_STRICT with def_bool
$(cc-option,-fsanitize=bounds-strict).
What I don't like about it is that if it's default off, nobody will turn
it on. If it's on and we force the option like in this patch, then
builds will fail on older toolchains. I bet most people don't know where
to get a newer toolchain than the one they have from their distro, or
how to install it so lots of headaches for people I guess. If we stay
with this solution here, is there a way to tell the user how to fix it
(either via a Make message, or at least a comment next to the places
where we set the flag?)
Cheers,
Quentin
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH 2/2] arm64: Add RELR relocs
2026-11-25 17:20 ` Quentin Schulz
@ 2026-11-26 12:48 ` Peter Robinson
2026-11-26 15:14 ` Ilias Apalodimas
0 siblings, 1 reply; 25+ messages in thread
From: Peter Robinson @ 2026-11-26 12:48 UTC (permalink / raw)
To: Quentin Schulz
Cc: Ilias Apalodimas, trini, richard.henderson, casey.connolly,
u-boot
[-- Attachment #1: Type: text/plain, Size: 6888 bytes --]
On Wed, 25 Nov 2026 at 17:20, Quentin Schulz <quentin.schulz@cherry.de>
wrote:
> Hi Ilias,
>
> On 11/25/26 8:41 AM, Ilias Apalodimas wrote:
> > Linking U-Boot with -pie emits an R_AARCH64_RELATIVE entry for every
> > pointer that needs fixing up. Each Elf64_Rela entry is 24 bytes, so the
> > .rela.dyn section ends up being quite large.
> >
> > Modern toolchains support packing relative relocations in RELR format
> > via '-z pack-relative-relocs'. Instead of offset/info/addend tuples,
> > RELR stores a stream of 64-bit words. An even word is the link-time
> > address of a word to relocate and an odd word is a bitmap describing
> > which of the 63 words following the previous address need relocating.
> > The addend is the value already stored in the place.
> >
> > Compiling for QEMU with RELA vs RELR looks something like this:
> >
> > $~ readelf -SW u-boot | grep relr
> > [ 9] .relr.dyn RELR 000000000016f970 170970 000960
> 08 A 0 0 8
> > $~ readelf -SW u-boot.old | grep rela
> > [ 7] .rela.dyn RELA 0000000000115110 116110 0184f8
> 18 A 0 0 8
> >
> > which is ~98% decrease in the space reloc entries need.
> >
> > So let's add a Kconfig option and teach U-Boot RELR.
> > - pass '-z pack-relative-relocs' to the linker and add .relr.dyn
> > in the linker script and the objcopy'ed binary. relr.dyn is still
> > overlayed by .bss
> > - skip checkarmreloc, since we now expect R_AARCH64_RELATIVE entries
> > to be packed in .rel*.dyn and older tools don't decode them
> > properly
> > - process .relr.dyn in pie_fixup. It's worth noting that efi runtime
> > relocs, due to how ld emits RELR, are now packed in .relr.dyn and
> > efi_runtime_relocate() doesn't have to relocate anything.
> > A sideeffect is that we can't support SetVirtualAddressMap and
> ConvertPointer
> > for EFI, but some architectures are already ommitting this call
> > (e.g Linux on arm64, openBSD doesn't call it at all). [0]
> > - process .relr.dyn in relocate_code(). Note that the places are
> > link-time addresses, so we still need the link-to-copy offset to find
> > them, but the stored value has already been shifted to the run address
> > by pie_fixup, so it needs to be adjusted by the run-to-copy offset
> >
> > [0] EFI runtime services relocate themselves when the OS calls
> > SetVirtualAddressMap, using the relocations between
> > __efi_runtime_rel_start and __efi_runtime_rel_stop. Those are collected
> > by the linker script from the .rela sections of the runtime code.
> > RELR packing, however, is done by the linker as a last step and all
> > relative relocations end up in a single .relr.dyn section for the entire
> > image. There's no way to emit an equivalent __efi_runtime_relr_start
> > marker for the runtime region, or emit RELA relocations for the EFI
> > runtime, so stop advertising SetVirtualAddressMap and ConvertPointer
> > in the EFI_RT_PROPERTIES_TABLE when RELR is enabled.
> >
>
> 6.4% size reduction for u-boot.itb with RELR enabled on
> tiger-rk3588_defconfig.
>
> $ aarch64-linux-gnu-ld --version
> GNU ld version 2.47.20260726
>
> from Fedora 45.
>
> I reached U-Boot proper CLI, which I have been told is sufficient to
> test this series, so:
>
I agree with enabling this by default, it should be opt out if there's
issues.
Looking at various default toolchains across distros (thanks Claude) Ubuntu
22.04 LTS has gcc11/binutils 2.38, RHEL 9 is gcc11/binutils 2.35.2, Debian
12 is gcc12.2/binutils 2.40
All have options for later versions so I think we should be fine to enable
by default and error if the toolchain isn't new enough. The reality is a
lot of users will be using old U-Boot if they're on ancient distros anyway.
> Tested-by: Quentin Schulz <quentin.schulz@cherry.de> # RK3588 Tiger
>
> > Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
> > ---
> > Kconfig | 14 +++++++++++++
> > arch/arm/config.mk | 10 +++++++--
> > arch/arm/cpu/armv8/start.S | 38 ++++++++++++++++++++++++++++++++++
> > arch/arm/cpu/armv8/u-boot.lds | 9 ++++++++
> > arch/arm/lib/relocate_64.c | 28 +++++++++++++++++++++++++
> > include/asm-generic/sections.h | 3 +++
> > lib/efi_loader/efi_runtime.c | 9 +++++---
> > 7 files changed, 106 insertions(+), 5 deletions(-)
> >
> > diff --git a/Kconfig b/Kconfig
> > index c8cfbf5d5de2..ebe7b30d7b78 100644
> > --- a/Kconfig
> > +++ b/Kconfig
> > @@ -40,6 +40,20 @@ config COMPILE_TEST
> > here. If you are a user/distributor, say N here to exclude
> useless
> > drivers to be distributed.
> >
> > +config RELR
> > + bool "Use RELR packed relative relocations"
> > + depends on ARM64
> > + help
> > + RELR stores relative relocations as a stream of 64-bit words: an
> > + even word is the link-time address of a word to relocate, and an
> > + odd word is a bitmap covering the 63 words that follow the
> previous
> > + address. This usually leads to ~95% compression of emitted
> relocation
> > + entries.
> > + Due to the placement of the .relr.dyn section and the way ld
> > + creates it we cannot currently relocate EFI functions at runtime.
> > + SetVirtualAddressMap and ConvertPointer for EFI are marked as
> > + unsupported.
> > +
> > config WERROR
> > bool "Compile U-Boot with warnings as errors"
> > default COMPILE_TEST
> > diff --git a/arch/arm/config.mk b/arch/arm/config.mk
> > index bce9a31e966b..ad73f728f7a2 100644
> > --- a/arch/arm/config.mk
> > +++ b/arch/arm/config.mk
> > @@ -111,6 +111,9 @@ endif
> >
> > # needed for relocation
> > LDFLAGS_u-boot += -pie
> > +ifeq ($(CONFIG_RELR),y)
> > +LDFLAGS_u-boot += -z pack-relative-relocs
> > +endif
> >
>
> suggestion: make this transparent to the user
>
> We've talked about this in private but wanted to put it in writing so
> it's available on the mailing list archive. I think it makes more sense
> to always enable the option (if it's safe!), if the linker supports it.
> According to
> https://rfc.archlinux.page/0023-pack-relative-relocs/#motivation, that
> is GNU ld 2.36 and LLVM 15. We can use the same mechanism as in the
> kernel, c.f. CC_HAS_UBSAN_BOUNDS_STRICT with def_bool
> $(cc-option,-fsanitize=bounds-strict).
>
> What I don't like about it is that if it's default off, nobody will turn
> it on. If it's on and we force the option like in this patch, then
> builds will fail on older toolchains. I bet most people don't know where
> to get a newer toolchain than the one they have from their distro, or
> how to install it so lots of headaches for people I guess. If we stay
> with this solution here, is there a way to tell the user how to fix it
> (either via a Make message, or at least a comment next to the places
> where we set the flag?)
>
> Cheers,
> Quentin
>
[-- Attachment #2: Type: text/html, Size: 9029 bytes --]
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH 2/2] arm64: Add RELR relocs
2026-11-26 12:48 ` Peter Robinson
@ 2026-11-26 15:14 ` Ilias Apalodimas
2026-11-27 18:46 ` Tom Rini
0 siblings, 1 reply; 25+ messages in thread
From: Ilias Apalodimas @ 2026-11-26 15:14 UTC (permalink / raw)
To: Peter Robinson, Quentin Schulz
Cc: trini, richard.henderson, casey.connolly, u-boot
Hi both
On Thu, 26 Nov 2026 at 14:48, Peter Robinson <pbrobinson@gmail.com> wrote:
>
>
>
> On Wed, 25 Nov 2026 at 17:20, Quentin Schulz <quentin.schulz@cherry.de> wrote:
>>
>> Hi Ilias,
>>
>> On 11/25/26 8:41 AM, Ilias Apalodimas wrote:
>> > Linking U-Boot with -pie emits an R_AARCH64_RELATIVE entry for every
>> > pointer that needs fixing up. Each Elf64_Rela entry is 24 bytes, so the
>> > .rela.dyn section ends up being quite large.
>> >
>> > Modern toolchains support packing relative relocations in RELR format
>> > via '-z pack-relative-relocs'. Instead of offset/info/addend tuples,
>> > RELR stores a stream of 64-bit words. An even word is the link-time
>> > address of a word to relocate and an odd word is a bitmap describing
>> > which of the 63 words following the previous address need relocating.
>> > The addend is the value already stored in the place.
>> >
>> > Compiling for QEMU with RELA vs RELR looks something like this:
>> >
>> > $~ readelf -SW u-boot | grep relr
>> > [ 9] .relr.dyn RELR 000000000016f970 170970 000960 08 A 0 0 8
>> > $~ readelf -SW u-boot.old | grep rela
>> > [ 7] .rela.dyn RELA 0000000000115110 116110 0184f8 18 A 0 0 8
>> >
>> > which is ~98% decrease in the space reloc entries need.
>> >
>> > So let's add a Kconfig option and teach U-Boot RELR.
>> > - pass '-z pack-relative-relocs' to the linker and add .relr.dyn
>> > in the linker script and the objcopy'ed binary. relr.dyn is still
>> > overlayed by .bss
>> > - skip checkarmreloc, since we now expect R_AARCH64_RELATIVE entries
>> > to be packed in .rel*.dyn and older tools don't decode them
>> > properly
>> > - process .relr.dyn in pie_fixup. It's worth noting that efi runtime
>> > relocs, due to how ld emits RELR, are now packed in .relr.dyn and
>> > efi_runtime_relocate() doesn't have to relocate anything.
>> > A sideeffect is that we can't support SetVirtualAddressMap and ConvertPointer
>> > for EFI, but some architectures are already ommitting this call
>> > (e.g Linux on arm64, openBSD doesn't call it at all). [0]
>> > - process .relr.dyn in relocate_code(). Note that the places are
>> > link-time addresses, so we still need the link-to-copy offset to find
>> > them, but the stored value has already been shifted to the run address
>> > by pie_fixup, so it needs to be adjusted by the run-to-copy offset
>> >
>> > [0] EFI runtime services relocate themselves when the OS calls
>> > SetVirtualAddressMap, using the relocations between
>> > __efi_runtime_rel_start and __efi_runtime_rel_stop. Those are collected
>> > by the linker script from the .rela sections of the runtime code.
>> > RELR packing, however, is done by the linker as a last step and all
>> > relative relocations end up in a single .relr.dyn section for the entire
>> > image. There's no way to emit an equivalent __efi_runtime_relr_start
>> > marker for the runtime region, or emit RELA relocations for the EFI
>> > runtime, so stop advertising SetVirtualAddressMap and ConvertPointer
>> > in the EFI_RT_PROPERTIES_TABLE when RELR is enabled.
>> >
>>
>> 6.4% size reduction for u-boot.itb with RELR enabled on
>> tiger-rk3588_defconfig.
>>
>> $ aarch64-linux-gnu-ld --version
>> GNU ld version 2.47.20260726
>>
>> from Fedora 45.
>>
>> I reached U-Boot proper CLI, which I have been told is sufficient to
>> test this series, so:
>
>
> I agree with enabling this by default, it should be opt out if there's issues.
>
> Looking at various default toolchains across distros (thanks Claude) Ubuntu 22.04 LTS has gcc11/binutils 2.38, RHEL 9 is gcc11/binutils 2.35.2, Debian 12 is gcc12.2/binutils 2.40
>
> All have options for later versions so I think we should be fine to enable by default and error if the toolchain isn't new enough. The reality is a lot of users will be using old U-Boot if they're on ancient distros anyway.
>
>>
>> suggestion: make this transparent to the user
>>
>> We've talked about this in private but wanted to put it in writing so
>> it's available on the mailing list archive. I think it makes more sense
>> to always enable the option (if it's safe!), if the linker supports it.
>> According to
>> https://rfc.archlinux.page/0023-pack-relative-relocs/#motivation, that
>> is GNU ld 2.36 and LLVM 15. We can use the same mechanism as in the
>> kernel, c.f. CC_HAS_UBSAN_BOUNDS_STRICT with def_bool
>> $(cc-option,-fsanitize=bounds-strict).
I don't think that's a good idea. You turn on an option that promises
to reduce the binary size. You compile but bercause you now made the
linker flags conditional the end user finds an *identical* binary
after enabling the option. I prefer breaking the build so users
understand that this relies on a newer linker. Whether they can update
it or not should not be our problem.
>>
>> What I don't like about it is that if it's default off, nobody will turn
>> it on. If it's on and we force the option like in this patch, then
>> builds will fail on older toolchains. I bet most people don't know where
>> to get a newer toolchain than the one they have from their distro, or
>> how to install it so lots of headaches for people I guess. If we stay
>> with this solution here, is there a way to tell the user how to fix it
>> (either via a Make message, or at least a comment next to the places
>> where we set the flag?)
There should be enough explanation in the commit message and Kconfig
option why this is not enabled by default.
TL;DR there are implications wrt to EFI if you enable RELR that do not
affect Linux for arm64, but Linux is not the only OS we boot. Since we
have it documented, I'd rather leave the option off, at least till the
EFI implications are gone.
Thanks
/Ilias
>>
>> Cheers,
>> Quentin
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH 2/2] arm64: Add RELR relocs
2026-11-26 15:14 ` Ilias Apalodimas
@ 2026-11-27 18:46 ` Tom Rini
2026-12-03 22:27 ` Ilias Apalodimas
0 siblings, 1 reply; 25+ messages in thread
From: Tom Rini @ 2026-11-27 18:46 UTC (permalink / raw)
To: Ilias Apalodimas
Cc: Peter Robinson, Quentin Schulz, richard.henderson, casey.connolly,
u-boot
[-- Attachment #1: Type: text/plain, Size: 6656 bytes --]
On Thu, Nov 26, 2026 at 05:14:26PM +0200, Ilias Apalodimas wrote:
> Hi both
>
> On Thu, 26 Nov 2026 at 14:48, Peter Robinson <pbrobinson@gmail.com> wrote:
> >
> >
> >
> > On Wed, 25 Nov 2026 at 17:20, Quentin Schulz <quentin.schulz@cherry.de> wrote:
> >>
> >> Hi Ilias,
> >>
> >> On 11/25/26 8:41 AM, Ilias Apalodimas wrote:
> >> > Linking U-Boot with -pie emits an R_AARCH64_RELATIVE entry for every
> >> > pointer that needs fixing up. Each Elf64_Rela entry is 24 bytes, so the
> >> > .rela.dyn section ends up being quite large.
> >> >
> >> > Modern toolchains support packing relative relocations in RELR format
> >> > via '-z pack-relative-relocs'. Instead of offset/info/addend tuples,
> >> > RELR stores a stream of 64-bit words. An even word is the link-time
> >> > address of a word to relocate and an odd word is a bitmap describing
> >> > which of the 63 words following the previous address need relocating.
> >> > The addend is the value already stored in the place.
> >> >
> >> > Compiling for QEMU with RELA vs RELR looks something like this:
> >> >
> >> > $~ readelf -SW u-boot | grep relr
> >> > [ 9] .relr.dyn RELR 000000000016f970 170970 000960 08 A 0 0 8
> >> > $~ readelf -SW u-boot.old | grep rela
> >> > [ 7] .rela.dyn RELA 0000000000115110 116110 0184f8 18 A 0 0 8
> >> >
> >> > which is ~98% decrease in the space reloc entries need.
> >> >
> >> > So let's add a Kconfig option and teach U-Boot RELR.
> >> > - pass '-z pack-relative-relocs' to the linker and add .relr.dyn
> >> > in the linker script and the objcopy'ed binary. relr.dyn is still
> >> > overlayed by .bss
> >> > - skip checkarmreloc, since we now expect R_AARCH64_RELATIVE entries
> >> > to be packed in .rel*.dyn and older tools don't decode them
> >> > properly
> >> > - process .relr.dyn in pie_fixup. It's worth noting that efi runtime
> >> > relocs, due to how ld emits RELR, are now packed in .relr.dyn and
> >> > efi_runtime_relocate() doesn't have to relocate anything.
> >> > A sideeffect is that we can't support SetVirtualAddressMap and ConvertPointer
> >> > for EFI, but some architectures are already ommitting this call
> >> > (e.g Linux on arm64, openBSD doesn't call it at all). [0]
> >> > - process .relr.dyn in relocate_code(). Note that the places are
> >> > link-time addresses, so we still need the link-to-copy offset to find
> >> > them, but the stored value has already been shifted to the run address
> >> > by pie_fixup, so it needs to be adjusted by the run-to-copy offset
> >> >
> >> > [0] EFI runtime services relocate themselves when the OS calls
> >> > SetVirtualAddressMap, using the relocations between
> >> > __efi_runtime_rel_start and __efi_runtime_rel_stop. Those are collected
> >> > by the linker script from the .rela sections of the runtime code.
> >> > RELR packing, however, is done by the linker as a last step and all
> >> > relative relocations end up in a single .relr.dyn section for the entire
> >> > image. There's no way to emit an equivalent __efi_runtime_relr_start
> >> > marker for the runtime region, or emit RELA relocations for the EFI
> >> > runtime, so stop advertising SetVirtualAddressMap and ConvertPointer
> >> > in the EFI_RT_PROPERTIES_TABLE when RELR is enabled.
> >> >
> >>
> >> 6.4% size reduction for u-boot.itb with RELR enabled on
> >> tiger-rk3588_defconfig.
> >>
> >> $ aarch64-linux-gnu-ld --version
> >> GNU ld version 2.47.20260726
> >>
> >> from Fedora 45.
> >>
> >> I reached U-Boot proper CLI, which I have been told is sufficient to
> >> test this series, so:
> >
> >
> > I agree with enabling this by default, it should be opt out if there's issues.
> >
> > Looking at various default toolchains across distros (thanks Claude) Ubuntu 22.04 LTS has gcc11/binutils 2.38, RHEL 9 is gcc11/binutils 2.35.2, Debian 12 is gcc12.2/binutils 2.40
> >
> > All have options for later versions so I think we should be fine to enable by default and error if the toolchain isn't new enough. The reality is a lot of users will be using old U-Boot if they're on ancient distros anyway.
> >
> >>
> >> suggestion: make this transparent to the user
> >>
> >> We've talked about this in private but wanted to put it in writing so
> >> it's available on the mailing list archive. I think it makes more sense
> >> to always enable the option (if it's safe!), if the linker supports it.
> >> According to
> >> https://rfc.archlinux.page/0023-pack-relative-relocs/#motivation, that
> >> is GNU ld 2.36 and LLVM 15. We can use the same mechanism as in the
> >> kernel, c.f. CC_HAS_UBSAN_BOUNDS_STRICT with def_bool
> >> $(cc-option,-fsanitize=bounds-strict).
>
> I don't think that's a good idea. You turn on an option that promises
> to reduce the binary size. You compile but bercause you now made the
> linker flags conditional the end user finds an *identical* binary
> after enabling the option. I prefer breaking the build so users
> understand that this relies on a newer linker. Whether they can update
> it or not should not be our problem.
So, we should be doing depends on LD_HAS_RELR_SUPPORT in the new options
and LD_HAS_RELR_SUPPORT is def_bool ..., and then the new option can be
default y.
> >> What I don't like about it is that if it's default off, nobody will turn
> >> it on. If it's on and we force the option like in this patch, then
> >> builds will fail on older toolchains. I bet most people don't know where
> >> to get a newer toolchain than the one they have from their distro, or
> >> how to install it so lots of headaches for people I guess. If we stay
> >> with this solution here, is there a way to tell the user how to fix it
> >> (either via a Make message, or at least a comment next to the places
> >> where we set the flag?)
>
> There should be enough explanation in the commit message and Kconfig
> option why this is not enabled by default.
> TL;DR there are implications wrt to EFI if you enable RELR that do not
> affect Linux for arm64, but Linux is not the only OS we boot. Since we
> have it documented, I'd rather leave the option off, at least till the
> EFI implications are gone.
Are the EFI implications something we can / will resolve, or should we
be asking the other OSes? Or, since the BSD folks do make it easy enough
to try them out, should the pushback be on you to fire up an installer
image or two? Or... is this something that won't blow up immediately and
rather you'd have to do some non-trivial testing to confirm?
--
Tom
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH 2/2] arm64: Add RELR relocs
2026-11-27 18:46 ` Tom Rini
@ 2026-12-03 22:27 ` Ilias Apalodimas
2026-12-03 22:42 ` Tom Rini
0 siblings, 1 reply; 25+ messages in thread
From: Ilias Apalodimas @ 2026-12-03 22:27 UTC (permalink / raw)
To: Tom Rini
Cc: Peter Robinson, Quentin Schulz, richard.henderson, casey.connolly,
u-boot
Hi Tom
On Fri, 27 Nov 2026 at 20:46, Tom Rini <trini@konsulko.com> wrote:
>
> On Thu, Nov 26, 2026 at 05:14:26PM +0200, Ilias Apalodimas wrote:
> > Hi both
> >
> > On Thu, 26 Nov 2026 at 14:48, Peter Robinson <pbrobinson@gmail.com> wrote:
> > >
> > >
> > >
> > > On Wed, 25 Nov 2026 at 17:20, Quentin Schulz <quentin.schulz@cherry.de> wrote:
> > >>
> > >> Hi Ilias,
> > >>
> > >> On 11/25/26 8:41 AM, Ilias Apalodimas wrote:
> > >> > Linking U-Boot with -pie emits an R_AARCH64_RELATIVE entry for every
> > >> > pointer that needs fixing up. Each Elf64_Rela entry is 24 bytes, so the
> > >> > .rela.dyn section ends up being quite large.
> > >> >
> > >> > Modern toolchains support packing relative relocations in RELR format
> > >> > via '-z pack-relative-relocs'. Instead of offset/info/addend tuples,
> > >> > RELR stores a stream of 64-bit words. An even word is the link-time
> > >> > address of a word to relocate and an odd word is a bitmap describing
> > >> > which of the 63 words following the previous address need relocating.
> > >> > The addend is the value already stored in the place.
> > >> >
> > >> > Compiling for QEMU with RELA vs RELR looks something like this:
> > >> >
> > >> > $~ readelf -SW u-boot | grep relr
> > >> > [ 9] .relr.dyn RELR 000000000016f970 170970 000960 08 A 0 0 8
> > >> > $~ readelf -SW u-boot.old | grep rela
> > >> > [ 7] .rela.dyn RELA 0000000000115110 116110 0184f8 18 A 0 0 8
> > >> >
> > >> > which is ~98% decrease in the space reloc entries need.
> > >> >
> > >> > So let's add a Kconfig option and teach U-Boot RELR.
> > >> > - pass '-z pack-relative-relocs' to the linker and add .relr.dyn
> > >> > in the linker script and the objcopy'ed binary. relr.dyn is still
> > >> > overlayed by .bss
> > >> > - skip checkarmreloc, since we now expect R_AARCH64_RELATIVE entries
> > >> > to be packed in .rel*.dyn and older tools don't decode them
> > >> > properly
> > >> > - process .relr.dyn in pie_fixup. It's worth noting that efi runtime
> > >> > relocs, due to how ld emits RELR, are now packed in .relr.dyn and
> > >> > efi_runtime_relocate() doesn't have to relocate anything.
> > >> > A sideeffect is that we can't support SetVirtualAddressMap and ConvertPointer
> > >> > for EFI, but some architectures are already ommitting this call
> > >> > (e.g Linux on arm64, openBSD doesn't call it at all). [0]
> > >> > - process .relr.dyn in relocate_code(). Note that the places are
> > >> > link-time addresses, so we still need the link-to-copy offset to find
> > >> > them, but the stored value has already been shifted to the run address
> > >> > by pie_fixup, so it needs to be adjusted by the run-to-copy offset
> > >> >
> > >> > [0] EFI runtime services relocate themselves when the OS calls
> > >> > SetVirtualAddressMap, using the relocations between
> > >> > __efi_runtime_rel_start and __efi_runtime_rel_stop. Those are collected
> > >> > by the linker script from the .rela sections of the runtime code.
> > >> > RELR packing, however, is done by the linker as a last step and all
> > >> > relative relocations end up in a single .relr.dyn section for the entire
> > >> > image. There's no way to emit an equivalent __efi_runtime_relr_start
> > >> > marker for the runtime region, or emit RELA relocations for the EFI
> > >> > runtime, so stop advertising SetVirtualAddressMap and ConvertPointer
> > >> > in the EFI_RT_PROPERTIES_TABLE when RELR is enabled.
> > >> >
> > >>
> > >> 6.4% size reduction for u-boot.itb with RELR enabled on
> > >> tiger-rk3588_defconfig.
> > >>
> > >> $ aarch64-linux-gnu-ld --version
> > >> GNU ld version 2.47.20260726
> > >>
> > >> from Fedora 45.
> > >>
> > >> I reached U-Boot proper CLI, which I have been told is sufficient to
> > >> test this series, so:
> > >
> > >
> > > I agree with enabling this by default, it should be opt out if there's issues.
> > >
> > > Looking at various default toolchains across distros (thanks Claude) Ubuntu 22.04 LTS has gcc11/binutils 2.38, RHEL 9 is gcc11/binutils 2.35.2, Debian 12 is gcc12.2/binutils 2.40
> > >
> > > All have options for later versions so I think we should be fine to enable by default and error if the toolchain isn't new enough. The reality is a lot of users will be using old U-Boot if they're on ancient distros anyway.
> > >
> > >>
> > >> suggestion: make this transparent to the user
> > >>
> > >> We've talked about this in private but wanted to put it in writing so
> > >> it's available on the mailing list archive. I think it makes more sense
> > >> to always enable the option (if it's safe!), if the linker supports it.
> > >> According to
> > >> https://rfc.archlinux.page/0023-pack-relative-relocs/#motivation, that
> > >> is GNU ld 2.36 and LLVM 15. We can use the same mechanism as in the
> > >> kernel, c.f. CC_HAS_UBSAN_BOUNDS_STRICT with def_bool
> > >> $(cc-option,-fsanitize=bounds-strict).
> >
> > I don't think that's a good idea. You turn on an option that promises
> > to reduce the binary size. You compile but bercause you now made the
> > linker flags conditional the end user finds an *identical* binary
> > after enabling the option. I prefer breaking the build so users
> > understand that this relies on a newer linker. Whether they can update
> > it or not should not be our problem.
>
> So, we should be doing depends on LD_HAS_RELR_SUPPORT in the new options
> and LD_HAS_RELR_SUPPORT is def_bool ..., and then the new option can be
> default y.
Ok, I can change thayt
>
> > >> What I don't like about it is that if it's default off, nobody will turn
> > >> it on. If it's on and we force the option like in this patch, then
> > >> builds will fail on older toolchains. I bet most people don't know where
> > >> to get a newer toolchain than the one they have from their distro, or
> > >> how to install it so lots of headaches for people I guess. If we stay
> > >> with this solution here, is there a way to tell the user how to fix it
> > >> (either via a Make message, or at least a comment next to the places
> > >> where we set the flag?)
> >
> > There should be enough explanation in the commit message and Kconfig
> > option why this is not enabled by default.
> > TL;DR there are implications wrt to EFI if you enable RELR that do not
> > affect Linux for arm64, but Linux is not the only OS we boot. Since we
> > have it documented, I'd rather leave the option off, at least till the
> > EFI implications are gone.
>
> Are the EFI implications something we can / will resolve, or should we
> be asking the other OSes?
We can but it requires many more changes in the EFI subsystem.
> Or, since the BSD folks do make it easy enough
> to try them out, should the pushback be on you to fire up an installer
> image or two? Or... is this something that won't blow up immediately and
> rather you'd have to do some non-trivial testing to confirm?
In theory (if the OS isn't buggy), nothing will blow up. When RELR is
enabled SetVirtualAddressMap is marked as unsupported. So worst case
is the OS will end up running without them. OS'es that avoid calling
it, just map RT servives 1:1 already and work fine
>
> --
> Tom
Cheers
/Ilias
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH 2/2] arm64: Add RELR relocs
2026-12-03 22:27 ` Ilias Apalodimas
@ 2026-12-03 22:42 ` Tom Rini
2026-12-03 22:53 ` Ilias Apalodimas
0 siblings, 1 reply; 25+ messages in thread
From: Tom Rini @ 2026-12-03 22:42 UTC (permalink / raw)
To: Ilias Apalodimas
Cc: Peter Robinson, Quentin Schulz, richard.henderson, casey.connolly,
u-boot
[-- Attachment #1: Type: text/plain, Size: 7971 bytes --]
On Fri, Dec 04, 2026 at 12:27:57AM +0200, Ilias Apalodimas wrote:
> Hi Tom
>
> On Fri, 27 Nov 2026 at 20:46, Tom Rini <trini@konsulko.com> wrote:
> >
> > On Thu, Nov 26, 2026 at 05:14:26PM +0200, Ilias Apalodimas wrote:
> > > Hi both
> > >
> > > On Thu, 26 Nov 2026 at 14:48, Peter Robinson <pbrobinson@gmail.com> wrote:
> > > >
> > > >
> > > >
> > > > On Wed, 25 Nov 2026 at 17:20, Quentin Schulz <quentin.schulz@cherry.de> wrote:
> > > >>
> > > >> Hi Ilias,
> > > >>
> > > >> On 11/25/26 8:41 AM, Ilias Apalodimas wrote:
> > > >> > Linking U-Boot with -pie emits an R_AARCH64_RELATIVE entry for every
> > > >> > pointer that needs fixing up. Each Elf64_Rela entry is 24 bytes, so the
> > > >> > .rela.dyn section ends up being quite large.
> > > >> >
> > > >> > Modern toolchains support packing relative relocations in RELR format
> > > >> > via '-z pack-relative-relocs'. Instead of offset/info/addend tuples,
> > > >> > RELR stores a stream of 64-bit words. An even word is the link-time
> > > >> > address of a word to relocate and an odd word is a bitmap describing
> > > >> > which of the 63 words following the previous address need relocating.
> > > >> > The addend is the value already stored in the place.
> > > >> >
> > > >> > Compiling for QEMU with RELA vs RELR looks something like this:
> > > >> >
> > > >> > $~ readelf -SW u-boot | grep relr
> > > >> > [ 9] .relr.dyn RELR 000000000016f970 170970 000960 08 A 0 0 8
> > > >> > $~ readelf -SW u-boot.old | grep rela
> > > >> > [ 7] .rela.dyn RELA 0000000000115110 116110 0184f8 18 A 0 0 8
> > > >> >
> > > >> > which is ~98% decrease in the space reloc entries need.
> > > >> >
> > > >> > So let's add a Kconfig option and teach U-Boot RELR.
> > > >> > - pass '-z pack-relative-relocs' to the linker and add .relr.dyn
> > > >> > in the linker script and the objcopy'ed binary. relr.dyn is still
> > > >> > overlayed by .bss
> > > >> > - skip checkarmreloc, since we now expect R_AARCH64_RELATIVE entries
> > > >> > to be packed in .rel*.dyn and older tools don't decode them
> > > >> > properly
> > > >> > - process .relr.dyn in pie_fixup. It's worth noting that efi runtime
> > > >> > relocs, due to how ld emits RELR, are now packed in .relr.dyn and
> > > >> > efi_runtime_relocate() doesn't have to relocate anything.
> > > >> > A sideeffect is that we can't support SetVirtualAddressMap and ConvertPointer
> > > >> > for EFI, but some architectures are already ommitting this call
> > > >> > (e.g Linux on arm64, openBSD doesn't call it at all). [0]
> > > >> > - process .relr.dyn in relocate_code(). Note that the places are
> > > >> > link-time addresses, so we still need the link-to-copy offset to find
> > > >> > them, but the stored value has already been shifted to the run address
> > > >> > by pie_fixup, so it needs to be adjusted by the run-to-copy offset
> > > >> >
> > > >> > [0] EFI runtime services relocate themselves when the OS calls
> > > >> > SetVirtualAddressMap, using the relocations between
> > > >> > __efi_runtime_rel_start and __efi_runtime_rel_stop. Those are collected
> > > >> > by the linker script from the .rela sections of the runtime code.
> > > >> > RELR packing, however, is done by the linker as a last step and all
> > > >> > relative relocations end up in a single .relr.dyn section for the entire
> > > >> > image. There's no way to emit an equivalent __efi_runtime_relr_start
> > > >> > marker for the runtime region, or emit RELA relocations for the EFI
> > > >> > runtime, so stop advertising SetVirtualAddressMap and ConvertPointer
> > > >> > in the EFI_RT_PROPERTIES_TABLE when RELR is enabled.
> > > >> >
> > > >>
> > > >> 6.4% size reduction for u-boot.itb with RELR enabled on
> > > >> tiger-rk3588_defconfig.
> > > >>
> > > >> $ aarch64-linux-gnu-ld --version
> > > >> GNU ld version 2.47.20260726
> > > >>
> > > >> from Fedora 45.
> > > >>
> > > >> I reached U-Boot proper CLI, which I have been told is sufficient to
> > > >> test this series, so:
> > > >
> > > >
> > > > I agree with enabling this by default, it should be opt out if there's issues.
> > > >
> > > > Looking at various default toolchains across distros (thanks Claude) Ubuntu 22.04 LTS has gcc11/binutils 2.38, RHEL 9 is gcc11/binutils 2.35.2, Debian 12 is gcc12.2/binutils 2.40
> > > >
> > > > All have options for later versions so I think we should be fine to enable by default and error if the toolchain isn't new enough. The reality is a lot of users will be using old U-Boot if they're on ancient distros anyway.
> > > >
> > > >>
> > > >> suggestion: make this transparent to the user
> > > >>
> > > >> We've talked about this in private but wanted to put it in writing so
> > > >> it's available on the mailing list archive. I think it makes more sense
> > > >> to always enable the option (if it's safe!), if the linker supports it.
> > > >> According to
> > > >> https://rfc.archlinux.page/0023-pack-relative-relocs/#motivation, that
> > > >> is GNU ld 2.36 and LLVM 15. We can use the same mechanism as in the
> > > >> kernel, c.f. CC_HAS_UBSAN_BOUNDS_STRICT with def_bool
> > > >> $(cc-option,-fsanitize=bounds-strict).
> > >
> > > I don't think that's a good idea. You turn on an option that promises
> > > to reduce the binary size. You compile but bercause you now made the
> > > linker flags conditional the end user finds an *identical* binary
> > > after enabling the option. I prefer breaking the build so users
> > > understand that this relies on a newer linker. Whether they can update
> > > it or not should not be our problem.
> >
> > So, we should be doing depends on LD_HAS_RELR_SUPPORT in the new options
> > and LD_HAS_RELR_SUPPORT is def_bool ..., and then the new option can be
> > default y.
>
> Ok, I can change thayt
>
> >
> > > >> What I don't like about it is that if it's default off, nobody will turn
> > > >> it on. If it's on and we force the option like in this patch, then
> > > >> builds will fail on older toolchains. I bet most people don't know where
> > > >> to get a newer toolchain than the one they have from their distro, or
> > > >> how to install it so lots of headaches for people I guess. If we stay
> > > >> with this solution here, is there a way to tell the user how to fix it
> > > >> (either via a Make message, or at least a comment next to the places
> > > >> where we set the flag?)
> > >
> > > There should be enough explanation in the commit message and Kconfig
> > > option why this is not enabled by default.
> > > TL;DR there are implications wrt to EFI if you enable RELR that do not
> > > affect Linux for arm64, but Linux is not the only OS we boot. Since we
> > > have it documented, I'd rather leave the option off, at least till the
> > > EFI implications are gone.
> >
> > Are the EFI implications something we can / will resolve, or should we
> > be asking the other OSes?
>
> We can but it requires many more changes in the EFI subsystem.
>
> > Or, since the BSD folks do make it easy enough
> > to try them out, should the pushback be on you to fire up an installer
> > image or two? Or... is this something that won't blow up immediately and
> > rather you'd have to do some non-trivial testing to confirm?
>
> In theory (if the OS isn't buggy), nothing will blow up. When RELR is
> enabled SetVirtualAddressMap is marked as unsupported. So worst case
> is the OS will end up running without them. OS'es that avoid calling
> it, just map RT servives 1:1 already and work fine
It sounds like we should document the needed EFI change then, but not
proactively do it? But to try and be clearer, is it easy to test at
run-time (in the OS) or a quick grep around the kernel sources, if say a
BSD is relying on SetVirtualAddressMap being supported, today?
--
Tom
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH 2/2] arm64: Add RELR relocs
2026-12-03 22:42 ` Tom Rini
@ 2026-12-03 22:53 ` Ilias Apalodimas
2026-12-03 23:02 ` Tom Rini
0 siblings, 1 reply; 25+ messages in thread
From: Ilias Apalodimas @ 2026-12-03 22:53 UTC (permalink / raw)
To: Tom Rini
Cc: Peter Robinson, Quentin Schulz, richard.henderson, casey.connolly,
u-boot
On Fri, 4 Dec 2026 at 00:42, Tom Rini <trini@konsulko.com> wrote:
>
> On Fri, Dec 04, 2026 at 12:27:57AM +0200, Ilias Apalodimas wrote:
> > Hi Tom
> >
> > On Fri, 27 Nov 2026 at 20:46, Tom Rini <trini@konsulko.com> wrote:
> > >
> > > On Thu, Nov 26, 2026 at 05:14:26PM +0200, Ilias Apalodimas wrote:
> > > > Hi both
> > > >
> > > > On Thu, 26 Nov 2026 at 14:48, Peter Robinson <pbrobinson@gmail.com> wrote:
> > > > >
> > > > >
> > > > >
> > > > > On Wed, 25 Nov 2026 at 17:20, Quentin Schulz <quentin.schulz@cherry.de> wrote:
> > > > >>
> > > > >> Hi Ilias,
> > > > >>
> > > > >> On 11/25/26 8:41 AM, Ilias Apalodimas wrote:
> > > > >> > Linking U-Boot with -pie emits an R_AARCH64_RELATIVE entry for every
> > > > >> > pointer that needs fixing up. Each Elf64_Rela entry is 24 bytes, so the
> > > > >> > .rela.dyn section ends up being quite large.
> > > > >> >
> > > > >> > Modern toolchains support packing relative relocations in RELR format
> > > > >> > via '-z pack-relative-relocs'. Instead of offset/info/addend tuples,
> > > > >> > RELR stores a stream of 64-bit words. An even word is the link-time
> > > > >> > address of a word to relocate and an odd word is a bitmap describing
> > > > >> > which of the 63 words following the previous address need relocating.
> > > > >> > The addend is the value already stored in the place.
> > > > >> >
> > > > >> > Compiling for QEMU with RELA vs RELR looks something like this:
> > > > >> >
> > > > >> > $~ readelf -SW u-boot | grep relr
> > > > >> > [ 9] .relr.dyn RELR 000000000016f970 170970 000960 08 A 0 0 8
> > > > >> > $~ readelf -SW u-boot.old | grep rela
> > > > >> > [ 7] .rela.dyn RELA 0000000000115110 116110 0184f8 18 A 0 0 8
> > > > >> >
> > > > >> > which is ~98% decrease in the space reloc entries need.
> > > > >> >
> > > > >> > So let's add a Kconfig option and teach U-Boot RELR.
> > > > >> > - pass '-z pack-relative-relocs' to the linker and add .relr.dyn
> > > > >> > in the linker script and the objcopy'ed binary. relr.dyn is still
> > > > >> > overlayed by .bss
> > > > >> > - skip checkarmreloc, since we now expect R_AARCH64_RELATIVE entries
> > > > >> > to be packed in .rel*.dyn and older tools don't decode them
> > > > >> > properly
> > > > >> > - process .relr.dyn in pie_fixup. It's worth noting that efi runtime
> > > > >> > relocs, due to how ld emits RELR, are now packed in .relr.dyn and
> > > > >> > efi_runtime_relocate() doesn't have to relocate anything.
> > > > >> > A sideeffect is that we can't support SetVirtualAddressMap and ConvertPointer
> > > > >> > for EFI, but some architectures are already ommitting this call
> > > > >> > (e.g Linux on arm64, openBSD doesn't call it at all). [0]
> > > > >> > - process .relr.dyn in relocate_code(). Note that the places are
> > > > >> > link-time addresses, so we still need the link-to-copy offset to find
> > > > >> > them, but the stored value has already been shifted to the run address
> > > > >> > by pie_fixup, so it needs to be adjusted by the run-to-copy offset
> > > > >> >
> > > > >> > [0] EFI runtime services relocate themselves when the OS calls
> > > > >> > SetVirtualAddressMap, using the relocations between
> > > > >> > __efi_runtime_rel_start and __efi_runtime_rel_stop. Those are collected
> > > > >> > by the linker script from the .rela sections of the runtime code.
> > > > >> > RELR packing, however, is done by the linker as a last step and all
> > > > >> > relative relocations end up in a single .relr.dyn section for the entire
> > > > >> > image. There's no way to emit an equivalent __efi_runtime_relr_start
> > > > >> > marker for the runtime region, or emit RELA relocations for the EFI
> > > > >> > runtime, so stop advertising SetVirtualAddressMap and ConvertPointer
> > > > >> > in the EFI_RT_PROPERTIES_TABLE when RELR is enabled.
> > > > >> >
> > > > >>
> > > > >> 6.4% size reduction for u-boot.itb with RELR enabled on
> > > > >> tiger-rk3588_defconfig.
> > > > >>
> > > > >> $ aarch64-linux-gnu-ld --version
> > > > >> GNU ld version 2.47.20260726
> > > > >>
> > > > >> from Fedora 45.
> > > > >>
> > > > >> I reached U-Boot proper CLI, which I have been told is sufficient to
> > > > >> test this series, so:
> > > > >
> > > > >
> > > > > I agree with enabling this by default, it should be opt out if there's issues.
> > > > >
> > > > > Looking at various default toolchains across distros (thanks Claude) Ubuntu 22.04 LTS has gcc11/binutils 2.38, RHEL 9 is gcc11/binutils 2.35.2, Debian 12 is gcc12.2/binutils 2.40
> > > > >
> > > > > All have options for later versions so I think we should be fine to enable by default and error if the toolchain isn't new enough. The reality is a lot of users will be using old U-Boot if they're on ancient distros anyway.
> > > > >
> > > > >>
> > > > >> suggestion: make this transparent to the user
> > > > >>
> > > > >> We've talked about this in private but wanted to put it in writing so
> > > > >> it's available on the mailing list archive. I think it makes more sense
> > > > >> to always enable the option (if it's safe!), if the linker supports it.
> > > > >> According to
> > > > >> https://rfc.archlinux.page/0023-pack-relative-relocs/#motivation, that
> > > > >> is GNU ld 2.36 and LLVM 15. We can use the same mechanism as in the
> > > > >> kernel, c.f. CC_HAS_UBSAN_BOUNDS_STRICT with def_bool
> > > > >> $(cc-option,-fsanitize=bounds-strict).
> > > >
> > > > I don't think that's a good idea. You turn on an option that promises
> > > > to reduce the binary size. You compile but bercause you now made the
> > > > linker flags conditional the end user finds an *identical* binary
> > > > after enabling the option. I prefer breaking the build so users
> > > > understand that this relies on a newer linker. Whether they can update
> > > > it or not should not be our problem.
> > >
> > > So, we should be doing depends on LD_HAS_RELR_SUPPORT in the new options
> > > and LD_HAS_RELR_SUPPORT is def_bool ..., and then the new option can be
> > > default y.
> >
> > Ok, I can change thayt
> >
> > >
> > > > >> What I don't like about it is that if it's default off, nobody will turn
> > > > >> it on. If it's on and we force the option like in this patch, then
> > > > >> builds will fail on older toolchains. I bet most people don't know where
> > > > >> to get a newer toolchain than the one they have from their distro, or
> > > > >> how to install it so lots of headaches for people I guess. If we stay
> > > > >> with this solution here, is there a way to tell the user how to fix it
> > > > >> (either via a Make message, or at least a comment next to the places
> > > > >> where we set the flag?)
> > > >
> > > > There should be enough explanation in the commit message and Kconfig
> > > > option why this is not enabled by default.
> > > > TL;DR there are implications wrt to EFI if you enable RELR that do not
> > > > affect Linux for arm64, but Linux is not the only OS we boot. Since we
> > > > have it documented, I'd rather leave the option off, at least till the
> > > > EFI implications are gone.
> > >
> > > Are the EFI implications something we can / will resolve, or should we
> > > be asking the other OSes?
> >
> > We can but it requires many more changes in the EFI subsystem.
> >
> > > Or, since the BSD folks do make it easy enough
> > > to try them out, should the pushback be on you to fire up an installer
> > > image or two? Or... is this something that won't blow up immediately and
> > > rather you'd have to do some non-trivial testing to confirm?
> >
> > In theory (if the OS isn't buggy), nothing will blow up. When RELR is
> > enabled SetVirtualAddressMap is marked as unsupported. So worst case
> > is the OS will end up running without them. OS'es that avoid calling
> > it, just map RT servives 1:1 already and work fine
>
> It sounds like we should document the needed EFI change then, but not
> proactively do it?
Sure, but where? In the commit message? docs?
> But to try and be clearer, is it easy to test at
> run-time (in the OS) or a quick grep around the kernel sources, if say a
> BSD is relying on SetVirtualAddressMap being supported, today?
From memory, openBSD doesn't, but FreeBSD does. I can double check
tomorrow, but that was the main reason I wanted the feature to be opt
in for now.
Cheers
/Ilias
>
> --
> Tom
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH 2/2] arm64: Add RELR relocs
2026-12-03 22:53 ` Ilias Apalodimas
@ 2026-12-03 23:02 ` Tom Rini
2026-12-04 6:44 ` Ilias Apalodimas
0 siblings, 1 reply; 25+ messages in thread
From: Tom Rini @ 2026-12-03 23:02 UTC (permalink / raw)
To: Ilias Apalodimas
Cc: Peter Robinson, Quentin Schulz, richard.henderson, casey.connolly,
u-boot
[-- Attachment #1: Type: text/plain, Size: 9132 bytes --]
On Fri, Dec 04, 2026 at 12:53:01AM +0200, Ilias Apalodimas wrote:
> On Fri, 4 Dec 2026 at 00:42, Tom Rini <trini@konsulko.com> wrote:
> >
> > On Fri, Dec 04, 2026 at 12:27:57AM +0200, Ilias Apalodimas wrote:
> > > Hi Tom
> > >
> > > On Fri, 27 Nov 2026 at 20:46, Tom Rini <trini@konsulko.com> wrote:
> > > >
> > > > On Thu, Nov 26, 2026 at 05:14:26PM +0200, Ilias Apalodimas wrote:
> > > > > Hi both
> > > > >
> > > > > On Thu, 26 Nov 2026 at 14:48, Peter Robinson <pbrobinson@gmail.com> wrote:
> > > > > >
> > > > > >
> > > > > >
> > > > > > On Wed, 25 Nov 2026 at 17:20, Quentin Schulz <quentin.schulz@cherry.de> wrote:
> > > > > >>
> > > > > >> Hi Ilias,
> > > > > >>
> > > > > >> On 11/25/26 8:41 AM, Ilias Apalodimas wrote:
> > > > > >> > Linking U-Boot with -pie emits an R_AARCH64_RELATIVE entry for every
> > > > > >> > pointer that needs fixing up. Each Elf64_Rela entry is 24 bytes, so the
> > > > > >> > .rela.dyn section ends up being quite large.
> > > > > >> >
> > > > > >> > Modern toolchains support packing relative relocations in RELR format
> > > > > >> > via '-z pack-relative-relocs'. Instead of offset/info/addend tuples,
> > > > > >> > RELR stores a stream of 64-bit words. An even word is the link-time
> > > > > >> > address of a word to relocate and an odd word is a bitmap describing
> > > > > >> > which of the 63 words following the previous address need relocating.
> > > > > >> > The addend is the value already stored in the place.
> > > > > >> >
> > > > > >> > Compiling for QEMU with RELA vs RELR looks something like this:
> > > > > >> >
> > > > > >> > $~ readelf -SW u-boot | grep relr
> > > > > >> > [ 9] .relr.dyn RELR 000000000016f970 170970 000960 08 A 0 0 8
> > > > > >> > $~ readelf -SW u-boot.old | grep rela
> > > > > >> > [ 7] .rela.dyn RELA 0000000000115110 116110 0184f8 18 A 0 0 8
> > > > > >> >
> > > > > >> > which is ~98% decrease in the space reloc entries need.
> > > > > >> >
> > > > > >> > So let's add a Kconfig option and teach U-Boot RELR.
> > > > > >> > - pass '-z pack-relative-relocs' to the linker and add .relr.dyn
> > > > > >> > in the linker script and the objcopy'ed binary. relr.dyn is still
> > > > > >> > overlayed by .bss
> > > > > >> > - skip checkarmreloc, since we now expect R_AARCH64_RELATIVE entries
> > > > > >> > to be packed in .rel*.dyn and older tools don't decode them
> > > > > >> > properly
> > > > > >> > - process .relr.dyn in pie_fixup. It's worth noting that efi runtime
> > > > > >> > relocs, due to how ld emits RELR, are now packed in .relr.dyn and
> > > > > >> > efi_runtime_relocate() doesn't have to relocate anything.
> > > > > >> > A sideeffect is that we can't support SetVirtualAddressMap and ConvertPointer
> > > > > >> > for EFI, but some architectures are already ommitting this call
> > > > > >> > (e.g Linux on arm64, openBSD doesn't call it at all). [0]
> > > > > >> > - process .relr.dyn in relocate_code(). Note that the places are
> > > > > >> > link-time addresses, so we still need the link-to-copy offset to find
> > > > > >> > them, but the stored value has already been shifted to the run address
> > > > > >> > by pie_fixup, so it needs to be adjusted by the run-to-copy offset
> > > > > >> >
> > > > > >> > [0] EFI runtime services relocate themselves when the OS calls
> > > > > >> > SetVirtualAddressMap, using the relocations between
> > > > > >> > __efi_runtime_rel_start and __efi_runtime_rel_stop. Those are collected
> > > > > >> > by the linker script from the .rela sections of the runtime code.
> > > > > >> > RELR packing, however, is done by the linker as a last step and all
> > > > > >> > relative relocations end up in a single .relr.dyn section for the entire
> > > > > >> > image. There's no way to emit an equivalent __efi_runtime_relr_start
> > > > > >> > marker for the runtime region, or emit RELA relocations for the EFI
> > > > > >> > runtime, so stop advertising SetVirtualAddressMap and ConvertPointer
> > > > > >> > in the EFI_RT_PROPERTIES_TABLE when RELR is enabled.
> > > > > >> >
> > > > > >>
> > > > > >> 6.4% size reduction for u-boot.itb with RELR enabled on
> > > > > >> tiger-rk3588_defconfig.
> > > > > >>
> > > > > >> $ aarch64-linux-gnu-ld --version
> > > > > >> GNU ld version 2.47.20260726
> > > > > >>
> > > > > >> from Fedora 45.
> > > > > >>
> > > > > >> I reached U-Boot proper CLI, which I have been told is sufficient to
> > > > > >> test this series, so:
> > > > > >
> > > > > >
> > > > > > I agree with enabling this by default, it should be opt out if there's issues.
> > > > > >
> > > > > > Looking at various default toolchains across distros (thanks Claude) Ubuntu 22.04 LTS has gcc11/binutils 2.38, RHEL 9 is gcc11/binutils 2.35.2, Debian 12 is gcc12.2/binutils 2.40
> > > > > >
> > > > > > All have options for later versions so I think we should be fine to enable by default and error if the toolchain isn't new enough. The reality is a lot of users will be using old U-Boot if they're on ancient distros anyway.
> > > > > >
> > > > > >>
> > > > > >> suggestion: make this transparent to the user
> > > > > >>
> > > > > >> We've talked about this in private but wanted to put it in writing so
> > > > > >> it's available on the mailing list archive. I think it makes more sense
> > > > > >> to always enable the option (if it's safe!), if the linker supports it.
> > > > > >> According to
> > > > > >> https://rfc.archlinux.page/0023-pack-relative-relocs/#motivation, that
> > > > > >> is GNU ld 2.36 and LLVM 15. We can use the same mechanism as in the
> > > > > >> kernel, c.f. CC_HAS_UBSAN_BOUNDS_STRICT with def_bool
> > > > > >> $(cc-option,-fsanitize=bounds-strict).
> > > > >
> > > > > I don't think that's a good idea. You turn on an option that promises
> > > > > to reduce the binary size. You compile but bercause you now made the
> > > > > linker flags conditional the end user finds an *identical* binary
> > > > > after enabling the option. I prefer breaking the build so users
> > > > > understand that this relies on a newer linker. Whether they can update
> > > > > it or not should not be our problem.
> > > >
> > > > So, we should be doing depends on LD_HAS_RELR_SUPPORT in the new options
> > > > and LD_HAS_RELR_SUPPORT is def_bool ..., and then the new option can be
> > > > default y.
> > >
> > > Ok, I can change thayt
> > >
> > > >
> > > > > >> What I don't like about it is that if it's default off, nobody will turn
> > > > > >> it on. If it's on and we force the option like in this patch, then
> > > > > >> builds will fail on older toolchains. I bet most people don't know where
> > > > > >> to get a newer toolchain than the one they have from their distro, or
> > > > > >> how to install it so lots of headaches for people I guess. If we stay
> > > > > >> with this solution here, is there a way to tell the user how to fix it
> > > > > >> (either via a Make message, or at least a comment next to the places
> > > > > >> where we set the flag?)
> > > > >
> > > > > There should be enough explanation in the commit message and Kconfig
> > > > > option why this is not enabled by default.
> > > > > TL;DR there are implications wrt to EFI if you enable RELR that do not
> > > > > affect Linux for arm64, but Linux is not the only OS we boot. Since we
> > > > > have it documented, I'd rather leave the option off, at least till the
> > > > > EFI implications are gone.
> > > >
> > > > Are the EFI implications something we can / will resolve, or should we
> > > > be asking the other OSes?
> > >
> > > We can but it requires many more changes in the EFI subsystem.
> > >
> > > > Or, since the BSD folks do make it easy enough
> > > > to try them out, should the pushback be on you to fire up an installer
> > > > image or two? Or... is this something that won't blow up immediately and
> > > > rather you'd have to do some non-trivial testing to confirm?
> > >
> > > In theory (if the OS isn't buggy), nothing will blow up. When RELR is
> > > enabled SetVirtualAddressMap is marked as unsupported. So worst case
> > > is the OS will end up running without them. OS'es that avoid calling
> > > it, just map RT servives 1:1 already and work fine
> >
> > It sounds like we should document the needed EFI change then, but not
> > proactively do it?
>
> Sure, but where? In the commit message? docs?
How about doc/api/efi.rst ? But..
>
> > But to try and be clearer, is it easy to test at
> > run-time (in the OS) or a quick grep around the kernel sources, if say a
> > BSD is relying on SetVirtualAddressMap being supported, today?
>
> From memory, openBSD doesn't, but FreeBSD does. I can double check
> tomorrow, but that was the main reason I wanted the feature to be opt
> in for now.
Ah, OK. So some FOSS OSes do need it. OK, yes. Is it a lot of work
that's needed, or just a bit more sit down and do it work? April and
it's merge window are still not close.
--
Tom
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH 2/2] arm64: Add RELR relocs
2026-12-03 23:02 ` Tom Rini
@ 2026-12-04 6:44 ` Ilias Apalodimas
2026-12-04 15:12 ` Tom Rini
0 siblings, 1 reply; 25+ messages in thread
From: Ilias Apalodimas @ 2026-12-04 6:44 UTC (permalink / raw)
To: Tom Rini
Cc: Peter Robinson, Quentin Schulz, richard.henderson, casey.connolly,
u-boot
On Fri, 4 Dec 2026 at 01:02, Tom Rini <trini@konsulko.com> wrote:
>
> On Fri, Dec 04, 2026 at 12:53:01AM +0200, Ilias Apalodimas wrote:
> > On Fri, 4 Dec 2026 at 00:42, Tom Rini <trini@konsulko.com> wrote:
> > >
> > > On Fri, Dec 04, 2026 at 12:27:57AM +0200, Ilias Apalodimas wrote:
> > > > Hi Tom
> > > >
> > > > On Fri, 27 Nov 2026 at 20:46, Tom Rini <trini@konsulko.com> wrote:
> > > > >
> > > > > On Thu, Nov 26, 2026 at 05:14:26PM +0200, Ilias Apalodimas wrote:
> > > > > > Hi both
> > > > > >
> > > > > > On Thu, 26 Nov 2026 at 14:48, Peter Robinson <pbrobinson@gmail.com> wrote:
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > > > On Wed, 25 Nov 2026 at 17:20, Quentin Schulz <quentin.schulz@cherry.de> wrote:
> > > > > > >>
> > > > > > >> Hi Ilias,
> > > > > > >>
> > > > > > >> On 11/25/26 8:41 AM, Ilias Apalodimas wrote:
> > > > > > >> > Linking U-Boot with -pie emits an R_AARCH64_RELATIVE entry for every
> > > > > > >> > pointer that needs fixing up. Each Elf64_Rela entry is 24 bytes, so the
> > > > > > >> > .rela.dyn section ends up being quite large.
> > > > > > >> >
> > > > > > >> > Modern toolchains support packing relative relocations in RELR format
> > > > > > >> > via '-z pack-relative-relocs'. Instead of offset/info/addend tuples,
> > > > > > >> > RELR stores a stream of 64-bit words. An even word is the link-time
> > > > > > >> > address of a word to relocate and an odd word is a bitmap describing
> > > > > > >> > which of the 63 words following the previous address need relocating.
> > > > > > >> > The addend is the value already stored in the place.
> > > > > > >> >
> > > > > > >> > Compiling for QEMU with RELA vs RELR looks something like this:
> > > > > > >> >
> > > > > > >> > $~ readelf -SW u-boot | grep relr
> > > > > > >> > [ 9] .relr.dyn RELR 000000000016f970 170970 000960 08 A 0 0 8
> > > > > > >> > $~ readelf -SW u-boot.old | grep rela
> > > > > > >> > [ 7] .rela.dyn RELA 0000000000115110 116110 0184f8 18 A 0 0 8
> > > > > > >> >
> > > > > > >> > which is ~98% decrease in the space reloc entries need.
> > > > > > >> >
> > > > > > >> > So let's add a Kconfig option and teach U-Boot RELR.
> > > > > > >> > - pass '-z pack-relative-relocs' to the linker and add .relr.dyn
> > > > > > >> > in the linker script and the objcopy'ed binary. relr.dyn is still
> > > > > > >> > overlayed by .bss
> > > > > > >> > - skip checkarmreloc, since we now expect R_AARCH64_RELATIVE entries
> > > > > > >> > to be packed in .rel*.dyn and older tools don't decode them
> > > > > > >> > properly
> > > > > > >> > - process .relr.dyn in pie_fixup. It's worth noting that efi runtime
> > > > > > >> > relocs, due to how ld emits RELR, are now packed in .relr.dyn and
> > > > > > >> > efi_runtime_relocate() doesn't have to relocate anything.
> > > > > > >> > A sideeffect is that we can't support SetVirtualAddressMap and ConvertPointer
> > > > > > >> > for EFI, but some architectures are already ommitting this call
> > > > > > >> > (e.g Linux on arm64, openBSD doesn't call it at all). [0]
> > > > > > >> > - process .relr.dyn in relocate_code(). Note that the places are
> > > > > > >> > link-time addresses, so we still need the link-to-copy offset to find
> > > > > > >> > them, but the stored value has already been shifted to the run address
> > > > > > >> > by pie_fixup, so it needs to be adjusted by the run-to-copy offset
> > > > > > >> >
> > > > > > >> > [0] EFI runtime services relocate themselves when the OS calls
> > > > > > >> > SetVirtualAddressMap, using the relocations between
> > > > > > >> > __efi_runtime_rel_start and __efi_runtime_rel_stop. Those are collected
> > > > > > >> > by the linker script from the .rela sections of the runtime code.
> > > > > > >> > RELR packing, however, is done by the linker as a last step and all
> > > > > > >> > relative relocations end up in a single .relr.dyn section for the entire
> > > > > > >> > image. There's no way to emit an equivalent __efi_runtime_relr_start
> > > > > > >> > marker for the runtime region, or emit RELA relocations for the EFI
> > > > > > >> > runtime, so stop advertising SetVirtualAddressMap and ConvertPointer
> > > > > > >> > in the EFI_RT_PROPERTIES_TABLE when RELR is enabled.
> > > > > > >> >
> > > > > > >>
> > > > > > >> 6.4% size reduction for u-boot.itb with RELR enabled on
> > > > > > >> tiger-rk3588_defconfig.
> > > > > > >>
> > > > > > >> $ aarch64-linux-gnu-ld --version
> > > > > > >> GNU ld version 2.47.20260726
> > > > > > >>
> > > > > > >> from Fedora 45.
> > > > > > >>
> > > > > > >> I reached U-Boot proper CLI, which I have been told is sufficient to
> > > > > > >> test this series, so:
> > > > > > >
> > > > > > >
> > > > > > > I agree with enabling this by default, it should be opt out if there's issues.
> > > > > > >
> > > > > > > Looking at various default toolchains across distros (thanks Claude) Ubuntu 22.04 LTS has gcc11/binutils 2.38, RHEL 9 is gcc11/binutils 2.35.2, Debian 12 is gcc12.2/binutils 2.40
> > > > > > >
> > > > > > > All have options for later versions so I think we should be fine to enable by default and error if the toolchain isn't new enough. The reality is a lot of users will be using old U-Boot if they're on ancient distros anyway.
> > > > > > >
> > > > > > >>
> > > > > > >> suggestion: make this transparent to the user
> > > > > > >>
> > > > > > >> We've talked about this in private but wanted to put it in writing so
> > > > > > >> it's available on the mailing list archive. I think it makes more sense
> > > > > > >> to always enable the option (if it's safe!), if the linker supports it.
> > > > > > >> According to
> > > > > > >> https://rfc.archlinux.page/0023-pack-relative-relocs/#motivation, that
> > > > > > >> is GNU ld 2.36 and LLVM 15. We can use the same mechanism as in the
> > > > > > >> kernel, c.f. CC_HAS_UBSAN_BOUNDS_STRICT with def_bool
> > > > > > >> $(cc-option,-fsanitize=bounds-strict).
> > > > > >
> > > > > > I don't think that's a good idea. You turn on an option that promises
> > > > > > to reduce the binary size. You compile but bercause you now made the
> > > > > > linker flags conditional the end user finds an *identical* binary
> > > > > > after enabling the option. I prefer breaking the build so users
> > > > > > understand that this relies on a newer linker. Whether they can update
> > > > > > it or not should not be our problem.
> > > > >
> > > > > So, we should be doing depends on LD_HAS_RELR_SUPPORT in the new options
> > > > > and LD_HAS_RELR_SUPPORT is def_bool ..., and then the new option can be
> > > > > default y.
> > > >
> > > > Ok, I can change thayt
> > > >
> > > > >
> > > > > > >> What I don't like about it is that if it's default off, nobody will turn
> > > > > > >> it on. If it's on and we force the option like in this patch, then
> > > > > > >> builds will fail on older toolchains. I bet most people don't know where
> > > > > > >> to get a newer toolchain than the one they have from their distro, or
> > > > > > >> how to install it so lots of headaches for people I guess. If we stay
> > > > > > >> with this solution here, is there a way to tell the user how to fix it
> > > > > > >> (either via a Make message, or at least a comment next to the places
> > > > > > >> where we set the flag?)
> > > > > >
> > > > > > There should be enough explanation in the commit message and Kconfig
> > > > > > option why this is not enabled by default.
> > > > > > TL;DR there are implications wrt to EFI if you enable RELR that do not
> > > > > > affect Linux for arm64, but Linux is not the only OS we boot. Since we
> > > > > > have it documented, I'd rather leave the option off, at least till the
> > > > > > EFI implications are gone.
> > > > >
> > > > > Are the EFI implications something we can / will resolve, or should we
> > > > > be asking the other OSes?
> > > >
> > > > We can but it requires many more changes in the EFI subsystem.
> > > >
> > > > > Or, since the BSD folks do make it easy enough
> > > > > to try them out, should the pushback be on you to fire up an installer
> > > > > image or two? Or... is this something that won't blow up immediately and
> > > > > rather you'd have to do some non-trivial testing to confirm?
> > > >
> > > > In theory (if the OS isn't buggy), nothing will blow up. When RELR is
> > > > enabled SetVirtualAddressMap is marked as unsupported. So worst case
> > > > is the OS will end up running without them. OS'es that avoid calling
> > > > it, just map RT servives 1:1 already and work fine
> > >
> > > It sounds like we should document the needed EFI change then, but not
> > > proactively do it?
> >
> > Sure, but where? In the commit message? docs?
>
> How about doc/api/efi.rst ? But..
I don't mind, OTOH the commit message explains enough already, I could
just expand it.
>
> >
> > > But to try and be clearer, is it easy to test at
> > > run-time (in the OS) or a quick grep around the kernel sources, if say a
> > > BSD is relying on SetVirtualAddressMap being supported, today?
> >
> > From memory, openBSD doesn't, but FreeBSD does. I can double check
> > tomorrow, but that was the main reason I wanted the feature to be opt
> > in for now.
>
> Ah, OK. So some FOSS OSes do need it. OK, yes. Is it a lot of work
> that's needed, or just a bit more sit down and do it work?
It requires significant work. We'll have to move all of the code
that's annotated with efi_runtime_* in a different folder, compile it
as an object and partially link it to U-Boot. What we currently do
under RELA is keep a separate section of efi_runtime* code and we
relocate that part only during SVAM. However, the RELR changes are
orthogonal. We can still pull this for anyone that *knows* he is
booting an OS that doesn't call SVAM, but keep it opt-in, and then
enable it by default once the EFI problems are gone.
Cheers
/Ilias
> April and
> it's merge window are still not close.
>
> --
> Tom
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH 2/2] arm64: Add RELR relocs
2026-12-04 6:44 ` Ilias Apalodimas
@ 2026-12-04 15:12 ` Tom Rini
2026-12-04 15:50 ` Ilias Apalodimas
0 siblings, 1 reply; 25+ messages in thread
From: Tom Rini @ 2026-12-04 15:12 UTC (permalink / raw)
To: Ilias Apalodimas
Cc: Peter Robinson, Quentin Schulz, richard.henderson, casey.connolly,
u-boot
[-- Attachment #1: Type: text/plain, Size: 11048 bytes --]
On Fri, Dec 04, 2026 at 08:44:04AM +0200, Ilias Apalodimas wrote:
> On Fri, 4 Dec 2026 at 01:02, Tom Rini <trini@konsulko.com> wrote:
> >
> > On Fri, Dec 04, 2026 at 12:53:01AM +0200, Ilias Apalodimas wrote:
> > > On Fri, 4 Dec 2026 at 00:42, Tom Rini <trini@konsulko.com> wrote:
> > > >
> > > > On Fri, Dec 04, 2026 at 12:27:57AM +0200, Ilias Apalodimas wrote:
> > > > > Hi Tom
> > > > >
> > > > > On Fri, 27 Nov 2026 at 20:46, Tom Rini <trini@konsulko.com> wrote:
> > > > > >
> > > > > > On Thu, Nov 26, 2026 at 05:14:26PM +0200, Ilias Apalodimas wrote:
> > > > > > > Hi both
> > > > > > >
> > > > > > > On Thu, 26 Nov 2026 at 14:48, Peter Robinson <pbrobinson@gmail.com> wrote:
> > > > > > > >
> > > > > > > >
> > > > > > > >
> > > > > > > > On Wed, 25 Nov 2026 at 17:20, Quentin Schulz <quentin.schulz@cherry.de> wrote:
> > > > > > > >>
> > > > > > > >> Hi Ilias,
> > > > > > > >>
> > > > > > > >> On 11/25/26 8:41 AM, Ilias Apalodimas wrote:
> > > > > > > >> > Linking U-Boot with -pie emits an R_AARCH64_RELATIVE entry for every
> > > > > > > >> > pointer that needs fixing up. Each Elf64_Rela entry is 24 bytes, so the
> > > > > > > >> > .rela.dyn section ends up being quite large.
> > > > > > > >> >
> > > > > > > >> > Modern toolchains support packing relative relocations in RELR format
> > > > > > > >> > via '-z pack-relative-relocs'. Instead of offset/info/addend tuples,
> > > > > > > >> > RELR stores a stream of 64-bit words. An even word is the link-time
> > > > > > > >> > address of a word to relocate and an odd word is a bitmap describing
> > > > > > > >> > which of the 63 words following the previous address need relocating.
> > > > > > > >> > The addend is the value already stored in the place.
> > > > > > > >> >
> > > > > > > >> > Compiling for QEMU with RELA vs RELR looks something like this:
> > > > > > > >> >
> > > > > > > >> > $~ readelf -SW u-boot | grep relr
> > > > > > > >> > [ 9] .relr.dyn RELR 000000000016f970 170970 000960 08 A 0 0 8
> > > > > > > >> > $~ readelf -SW u-boot.old | grep rela
> > > > > > > >> > [ 7] .rela.dyn RELA 0000000000115110 116110 0184f8 18 A 0 0 8
> > > > > > > >> >
> > > > > > > >> > which is ~98% decrease in the space reloc entries need.
> > > > > > > >> >
> > > > > > > >> > So let's add a Kconfig option and teach U-Boot RELR.
> > > > > > > >> > - pass '-z pack-relative-relocs' to the linker and add .relr.dyn
> > > > > > > >> > in the linker script and the objcopy'ed binary. relr.dyn is still
> > > > > > > >> > overlayed by .bss
> > > > > > > >> > - skip checkarmreloc, since we now expect R_AARCH64_RELATIVE entries
> > > > > > > >> > to be packed in .rel*.dyn and older tools don't decode them
> > > > > > > >> > properly
> > > > > > > >> > - process .relr.dyn in pie_fixup. It's worth noting that efi runtime
> > > > > > > >> > relocs, due to how ld emits RELR, are now packed in .relr.dyn and
> > > > > > > >> > efi_runtime_relocate() doesn't have to relocate anything.
> > > > > > > >> > A sideeffect is that we can't support SetVirtualAddressMap and ConvertPointer
> > > > > > > >> > for EFI, but some architectures are already ommitting this call
> > > > > > > >> > (e.g Linux on arm64, openBSD doesn't call it at all). [0]
> > > > > > > >> > - process .relr.dyn in relocate_code(). Note that the places are
> > > > > > > >> > link-time addresses, so we still need the link-to-copy offset to find
> > > > > > > >> > them, but the stored value has already been shifted to the run address
> > > > > > > >> > by pie_fixup, so it needs to be adjusted by the run-to-copy offset
> > > > > > > >> >
> > > > > > > >> > [0] EFI runtime services relocate themselves when the OS calls
> > > > > > > >> > SetVirtualAddressMap, using the relocations between
> > > > > > > >> > __efi_runtime_rel_start and __efi_runtime_rel_stop. Those are collected
> > > > > > > >> > by the linker script from the .rela sections of the runtime code.
> > > > > > > >> > RELR packing, however, is done by the linker as a last step and all
> > > > > > > >> > relative relocations end up in a single .relr.dyn section for the entire
> > > > > > > >> > image. There's no way to emit an equivalent __efi_runtime_relr_start
> > > > > > > >> > marker for the runtime region, or emit RELA relocations for the EFI
> > > > > > > >> > runtime, so stop advertising SetVirtualAddressMap and ConvertPointer
> > > > > > > >> > in the EFI_RT_PROPERTIES_TABLE when RELR is enabled.
> > > > > > > >> >
> > > > > > > >>
> > > > > > > >> 6.4% size reduction for u-boot.itb with RELR enabled on
> > > > > > > >> tiger-rk3588_defconfig.
> > > > > > > >>
> > > > > > > >> $ aarch64-linux-gnu-ld --version
> > > > > > > >> GNU ld version 2.47.20260726
> > > > > > > >>
> > > > > > > >> from Fedora 45.
> > > > > > > >>
> > > > > > > >> I reached U-Boot proper CLI, which I have been told is sufficient to
> > > > > > > >> test this series, so:
> > > > > > > >
> > > > > > > >
> > > > > > > > I agree with enabling this by default, it should be opt out if there's issues.
> > > > > > > >
> > > > > > > > Looking at various default toolchains across distros (thanks Claude) Ubuntu 22.04 LTS has gcc11/binutils 2.38, RHEL 9 is gcc11/binutils 2.35.2, Debian 12 is gcc12.2/binutils 2.40
> > > > > > > >
> > > > > > > > All have options for later versions so I think we should be fine to enable by default and error if the toolchain isn't new enough. The reality is a lot of users will be using old U-Boot if they're on ancient distros anyway.
> > > > > > > >
> > > > > > > >>
> > > > > > > >> suggestion: make this transparent to the user
> > > > > > > >>
> > > > > > > >> We've talked about this in private but wanted to put it in writing so
> > > > > > > >> it's available on the mailing list archive. I think it makes more sense
> > > > > > > >> to always enable the option (if it's safe!), if the linker supports it.
> > > > > > > >> According to
> > > > > > > >> https://rfc.archlinux.page/0023-pack-relative-relocs/#motivation, that
> > > > > > > >> is GNU ld 2.36 and LLVM 15. We can use the same mechanism as in the
> > > > > > > >> kernel, c.f. CC_HAS_UBSAN_BOUNDS_STRICT with def_bool
> > > > > > > >> $(cc-option,-fsanitize=bounds-strict).
> > > > > > >
> > > > > > > I don't think that's a good idea. You turn on an option that promises
> > > > > > > to reduce the binary size. You compile but bercause you now made the
> > > > > > > linker flags conditional the end user finds an *identical* binary
> > > > > > > after enabling the option. I prefer breaking the build so users
> > > > > > > understand that this relies on a newer linker. Whether they can update
> > > > > > > it or not should not be our problem.
> > > > > >
> > > > > > So, we should be doing depends on LD_HAS_RELR_SUPPORT in the new options
> > > > > > and LD_HAS_RELR_SUPPORT is def_bool ..., and then the new option can be
> > > > > > default y.
> > > > >
> > > > > Ok, I can change thayt
> > > > >
> > > > > >
> > > > > > > >> What I don't like about it is that if it's default off, nobody will turn
> > > > > > > >> it on. If it's on and we force the option like in this patch, then
> > > > > > > >> builds will fail on older toolchains. I bet most people don't know where
> > > > > > > >> to get a newer toolchain than the one they have from their distro, or
> > > > > > > >> how to install it so lots of headaches for people I guess. If we stay
> > > > > > > >> with this solution here, is there a way to tell the user how to fix it
> > > > > > > >> (either via a Make message, or at least a comment next to the places
> > > > > > > >> where we set the flag?)
> > > > > > >
> > > > > > > There should be enough explanation in the commit message and Kconfig
> > > > > > > option why this is not enabled by default.
> > > > > > > TL;DR there are implications wrt to EFI if you enable RELR that do not
> > > > > > > affect Linux for arm64, but Linux is not the only OS we boot. Since we
> > > > > > > have it documented, I'd rather leave the option off, at least till the
> > > > > > > EFI implications are gone.
> > > > > >
> > > > > > Are the EFI implications something we can / will resolve, or should we
> > > > > > be asking the other OSes?
> > > > >
> > > > > We can but it requires many more changes in the EFI subsystem.
> > > > >
> > > > > > Or, since the BSD folks do make it easy enough
> > > > > > to try them out, should the pushback be on you to fire up an installer
> > > > > > image or two? Or... is this something that won't blow up immediately and
> > > > > > rather you'd have to do some non-trivial testing to confirm?
> > > > >
> > > > > In theory (if the OS isn't buggy), nothing will blow up. When RELR is
> > > > > enabled SetVirtualAddressMap is marked as unsupported. So worst case
> > > > > is the OS will end up running without them. OS'es that avoid calling
> > > > > it, just map RT servives 1:1 already and work fine
> > > >
> > > > It sounds like we should document the needed EFI change then, but not
> > > > proactively do it?
> > >
> > > Sure, but where? In the commit message? docs?
> >
> > How about doc/api/efi.rst ? But..
>
> I don't mind, OTOH the commit message explains enough already, I could
> just expand it.
>
> >
> > >
> > > > But to try and be clearer, is it easy to test at
> > > > run-time (in the OS) or a quick grep around the kernel sources, if say a
> > > > BSD is relying on SetVirtualAddressMap being supported, today?
> > >
> > > From memory, openBSD doesn't, but FreeBSD does. I can double check
> > > tomorrow, but that was the main reason I wanted the feature to be opt
> > > in for now.
> >
> > Ah, OK. So some FOSS OSes do need it. OK, yes. Is it a lot of work
> > that's needed, or just a bit more sit down and do it work?
>
> It requires significant work. We'll have to move all of the code
> that's annotated with efi_runtime_* in a different folder, compile it
> as an object and partially link it to U-Boot. What we currently do
> under RELA is keep a separate section of efi_runtime* code and we
> relocate that part only during SVAM. However, the RELR changes are
> orthogonal. We can still pull this for anyone that *knows* he is
> booting an OS that doesn't call SVAM, but keep it opt-in, and then
> enable it by default once the EFI problems are gone.
There's annotations in 18 files, but a few of them aren't arm. If we
*only* need to refactor the files, that doesn't sound too bad, and we
should be able to keep the directory structure and manipulate the link
flags for making one object which is then linked to built-in.o, yes? I
thought there were examples of that but I don't see one quickly. Because
I'd really rather do all this at once, rather than RELR now and fix EFI
later, since EFI is default and it's not a commercial OS that needs it,
but another FOSS OS.
--
Tom
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH 2/2] arm64: Add RELR relocs
2026-12-04 15:12 ` Tom Rini
@ 2026-12-04 15:50 ` Ilias Apalodimas
2026-12-04 16:13 ` Ilias Apalodimas
0 siblings, 1 reply; 25+ messages in thread
From: Ilias Apalodimas @ 2026-12-04 15:50 UTC (permalink / raw)
To: Tom Rini
Cc: Peter Robinson, Quentin Schulz, richard.henderson, casey.connolly,
u-boot
Hi Tom,
On Fri, 4 Dec 2026 at 17:12, Tom Rini <trini@konsulko.com> wrote:
>
> On Fri, Dec 04, 2026 at 08:44:04AM +0200, Ilias Apalodimas wrote:
> > On Fri, 4 Dec 2026 at 01:02, Tom Rini <trini@konsulko.com> wrote:
> > >
> > > On Fri, Dec 04, 2026 at 12:53:01AM +0200, Ilias Apalodimas wrote:
> > > > On Fri, 4 Dec 2026 at 00:42, Tom Rini <trini@konsulko.com> wrote:
> > > > >
> > > > > On Fri, Dec 04, 2026 at 12:27:57AM +0200, Ilias Apalodimas wrote:
> > > > > > Hi Tom
> > > > > >
> > > > > > On Fri, 27 Nov 2026 at 20:46, Tom Rini <trini@konsulko.com> wrote:
> > > > > > >
> > > > > > > On Thu, Nov 26, 2026 at 05:14:26PM +0200, Ilias Apalodimas wrote:
> > > > > > > > Hi both
> > > > > > > >
> > > > > > > > On Thu, 26 Nov 2026 at 14:48, Peter Robinson <pbrobinson@gmail.com> wrote:
> > > > > > > > >
> > > > > > > > >
> > > > > > > > >
> > > > > > > > > On Wed, 25 Nov 2026 at 17:20, Quentin Schulz <quentin.schulz@cherry.de> wrote:
> > > > > > > > >>
> > > > > > > > >> Hi Ilias,
> > > > > > > > >>
> > > > > > > > >> On 11/25/26 8:41 AM, Ilias Apalodimas wrote:
> > > > > > > > >> > Linking U-Boot with -pie emits an R_AARCH64_RELATIVE entry for every
> > > > > > > > >> > pointer that needs fixing up. Each Elf64_Rela entry is 24 bytes, so the
> > > > > > > > >> > .rela.dyn section ends up being quite large.
> > > > > > > > >> >
> > > > > > > > >> > Modern toolchains support packing relative relocations in RELR format
> > > > > > > > >> > via '-z pack-relative-relocs'. Instead of offset/info/addend tuples,
> > > > > > > > >> > RELR stores a stream of 64-bit words. An even word is the link-time
> > > > > > > > >> > address of a word to relocate and an odd word is a bitmap describing
> > > > > > > > >> > which of the 63 words following the previous address need relocating.
> > > > > > > > >> > The addend is the value already stored in the place.
> > > > > > > > >> >
> > > > > > > > >> > Compiling for QEMU with RELA vs RELR looks something like this:
> > > > > > > > >> >
> > > > > > > > >> > $~ readelf -SW u-boot | grep relr
> > > > > > > > >> > [ 9] .relr.dyn RELR 000000000016f970 170970 000960 08 A 0 0 8
> > > > > > > > >> > $~ readelf -SW u-boot.old | grep rela
> > > > > > > > >> > [ 7] .rela.dyn RELA 0000000000115110 116110 0184f8 18 A 0 0 8
> > > > > > > > >> >
> > > > > > > > >> > which is ~98% decrease in the space reloc entries need.
> > > > > > > > >> >
> > > > > > > > >> > So let's add a Kconfig option and teach U-Boot RELR.
> > > > > > > > >> > - pass '-z pack-relative-relocs' to the linker and add .relr.dyn
> > > > > > > > >> > in the linker script and the objcopy'ed binary. relr.dyn is still
> > > > > > > > >> > overlayed by .bss
> > > > > > > > >> > - skip checkarmreloc, since we now expect R_AARCH64_RELATIVE entries
> > > > > > > > >> > to be packed in .rel*.dyn and older tools don't decode them
> > > > > > > > >> > properly
> > > > > > > > >> > - process .relr.dyn in pie_fixup. It's worth noting that efi runtime
> > > > > > > > >> > relocs, due to how ld emits RELR, are now packed in .relr.dyn and
> > > > > > > > >> > efi_runtime_relocate() doesn't have to relocate anything.
> > > > > > > > >> > A sideeffect is that we can't support SetVirtualAddressMap and ConvertPointer
> > > > > > > > >> > for EFI, but some architectures are already ommitting this call
> > > > > > > > >> > (e.g Linux on arm64, openBSD doesn't call it at all). [0]
> > > > > > > > >> > - process .relr.dyn in relocate_code(). Note that the places are
> > > > > > > > >> > link-time addresses, so we still need the link-to-copy offset to find
> > > > > > > > >> > them, but the stored value has already been shifted to the run address
> > > > > > > > >> > by pie_fixup, so it needs to be adjusted by the run-to-copy offset
> > > > > > > > >> >
> > > > > > > > >> > [0] EFI runtime services relocate themselves when the OS calls
> > > > > > > > >> > SetVirtualAddressMap, using the relocations between
> > > > > > > > >> > __efi_runtime_rel_start and __efi_runtime_rel_stop. Those are collected
> > > > > > > > >> > by the linker script from the .rela sections of the runtime code.
> > > > > > > > >> > RELR packing, however, is done by the linker as a last step and all
> > > > > > > > >> > relative relocations end up in a single .relr.dyn section for the entire
> > > > > > > > >> > image. There's no way to emit an equivalent __efi_runtime_relr_start
> > > > > > > > >> > marker for the runtime region, or emit RELA relocations for the EFI
> > > > > > > > >> > runtime, so stop advertising SetVirtualAddressMap and ConvertPointer
> > > > > > > > >> > in the EFI_RT_PROPERTIES_TABLE when RELR is enabled.
> > > > > > > > >> >
> > > > > > > > >>
> > > > > > > > >> 6.4% size reduction for u-boot.itb with RELR enabled on
> > > > > > > > >> tiger-rk3588_defconfig.
> > > > > > > > >>
> > > > > > > > >> $ aarch64-linux-gnu-ld --version
> > > > > > > > >> GNU ld version 2.47.20260726
> > > > > > > > >>
> > > > > > > > >> from Fedora 45.
> > > > > > > > >>
> > > > > > > > >> I reached U-Boot proper CLI, which I have been told is sufficient to
> > > > > > > > >> test this series, so:
> > > > > > > > >
> > > > > > > > >
> > > > > > > > > I agree with enabling this by default, it should be opt out if there's issues.
> > > > > > > > >
> > > > > > > > > Looking at various default toolchains across distros (thanks Claude) Ubuntu 22.04 LTS has gcc11/binutils 2.38, RHEL 9 is gcc11/binutils 2.35.2, Debian 12 is gcc12.2/binutils 2.40
> > > > > > > > >
> > > > > > > > > All have options for later versions so I think we should be fine to enable by default and error if the toolchain isn't new enough. The reality is a lot of users will be using old U-Boot if they're on ancient distros anyway.
> > > > > > > > >
> > > > > > > > >>
> > > > > > > > >> suggestion: make this transparent to the user
> > > > > > > > >>
> > > > > > > > >> We've talked about this in private but wanted to put it in writing so
> > > > > > > > >> it's available on the mailing list archive. I think it makes more sense
> > > > > > > > >> to always enable the option (if it's safe!), if the linker supports it.
> > > > > > > > >> According to
> > > > > > > > >> https://rfc.archlinux.page/0023-pack-relative-relocs/#motivation, that
> > > > > > > > >> is GNU ld 2.36 and LLVM 15. We can use the same mechanism as in the
> > > > > > > > >> kernel, c.f. CC_HAS_UBSAN_BOUNDS_STRICT with def_bool
> > > > > > > > >> $(cc-option,-fsanitize=bounds-strict).
> > > > > > > >
> > > > > > > > I don't think that's a good idea. You turn on an option that promises
> > > > > > > > to reduce the binary size. You compile but bercause you now made the
> > > > > > > > linker flags conditional the end user finds an *identical* binary
> > > > > > > > after enabling the option. I prefer breaking the build so users
> > > > > > > > understand that this relies on a newer linker. Whether they can update
> > > > > > > > it or not should not be our problem.
> > > > > > >
> > > > > > > So, we should be doing depends on LD_HAS_RELR_SUPPORT in the new options
> > > > > > > and LD_HAS_RELR_SUPPORT is def_bool ..., and then the new option can be
> > > > > > > default y.
> > > > > >
> > > > > > Ok, I can change thayt
> > > > > >
> > > > > > >
> > > > > > > > >> What I don't like about it is that if it's default off, nobody will turn
> > > > > > > > >> it on. If it's on and we force the option like in this patch, then
> > > > > > > > >> builds will fail on older toolchains. I bet most people don't know where
> > > > > > > > >> to get a newer toolchain than the one they have from their distro, or
> > > > > > > > >> how to install it so lots of headaches for people I guess. If we stay
> > > > > > > > >> with this solution here, is there a way to tell the user how to fix it
> > > > > > > > >> (either via a Make message, or at least a comment next to the places
> > > > > > > > >> where we set the flag?)
> > > > > > > >
> > > > > > > > There should be enough explanation in the commit message and Kconfig
> > > > > > > > option why this is not enabled by default.
> > > > > > > > TL;DR there are implications wrt to EFI if you enable RELR that do not
> > > > > > > > affect Linux for arm64, but Linux is not the only OS we boot. Since we
> > > > > > > > have it documented, I'd rather leave the option off, at least till the
> > > > > > > > EFI implications are gone.
> > > > > > >
> > > > > > > Are the EFI implications something we can / will resolve, or should we
> > > > > > > be asking the other OSes?
> > > > > >
> > > > > > We can but it requires many more changes in the EFI subsystem.
> > > > > >
> > > > > > > Or, since the BSD folks do make it easy enough
> > > > > > > to try them out, should the pushback be on you to fire up an installer
> > > > > > > image or two? Or... is this something that won't blow up immediately and
> > > > > > > rather you'd have to do some non-trivial testing to confirm?
> > > > > >
> > > > > > In theory (if the OS isn't buggy), nothing will blow up. When RELR is
> > > > > > enabled SetVirtualAddressMap is marked as unsupported. So worst case
> > > > > > is the OS will end up running without them. OS'es that avoid calling
> > > > > > it, just map RT servives 1:1 already and work fine
> > > > >
> > > > > It sounds like we should document the needed EFI change then, but not
> > > > > proactively do it?
> > > >
> > > > Sure, but where? In the commit message? docs?
> > >
> > > How about doc/api/efi.rst ? But..
> >
> > I don't mind, OTOH the commit message explains enough already, I could
> > just expand it.
> >
> > >
> > > >
> > > > > But to try and be clearer, is it easy to test at
> > > > > run-time (in the OS) or a quick grep around the kernel sources, if say a
> > > > > BSD is relying on SetVirtualAddressMap being supported, today?
> > > >
> > > > From memory, openBSD doesn't, but FreeBSD does. I can double check
> > > > tomorrow, but that was the main reason I wanted the feature to be opt
> > > > in for now.
> > >
> > > Ah, OK. So some FOSS OSes do need it. OK, yes. Is it a lot of work
> > > that's needed, or just a bit more sit down and do it work?
> >
> > It requires significant work. We'll have to move all of the code
> > that's annotated with efi_runtime_* in a different folder, compile it
> > as an object and partially link it to U-Boot. What we currently do
> > under RELA is keep a separate section of efi_runtime* code and we
> > relocate that part only during SVAM. However, the RELR changes are
> > orthogonal. We can still pull this for anyone that *knows* he is
> > booting an OS that doesn't call SVAM, but keep it opt-in, and then
> > enable it by default once the EFI problems are gone.
>
> There's annotations in 18 files, but a few of them aren't arm. If we
> *only* need to refactor the files, that doesn't sound too bad, and we
> should be able to keep the directory structure and manipulate the link
> flags for making one object which is then linked to built-in.o, yes?
It's not that simple (and I don't know all the details yet, I am still
reading up). The problem right now is that the linker bundles all the
relocation in relr.dyn, so we loose access to to the efi.rela.dyn we
keep to enable runtime relocation. Also we overlay .rel* with .bss, so
once we relocate we permanently loose all of the relocation data. The
idea is to compile those new files as a standalone binary (with -pie)
and preserve the relr relocations for that special binary, so we can
use them once SVAM is called by the OS. U-Boot will then have to copy
& relocate that EFI RT binary on init. There will also be implications
for functions that both U-Boot and the EFI RT will need, e.g crc32.
Tke kernels' EFI stub is doing something very similar, so that's what
I am looking currently to understand the whole process.
> I
> thought there were examples of that but I don't see one quickly. Because
> I'd really rather do all this at once, rather than RELR now and fix EFI
> later, since EFI is default and it's not a commercial OS that needs it,
> but another FOSS OS.
That's ok, I don't mind keeping this patch until the EFI part is
fixed. The files it touches do not change often, so rebasing it isnt
too much effort
Thanks
/Ilias
>
> --
> Tom
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH 2/2] arm64: Add RELR relocs
2026-12-04 15:50 ` Ilias Apalodimas
@ 2026-12-04 16:13 ` Ilias Apalodimas
0 siblings, 0 replies; 25+ messages in thread
From: Ilias Apalodimas @ 2026-12-04 16:13 UTC (permalink / raw)
To: Tom Rini
Cc: Peter Robinson, Quentin Schulz, richard.henderson, casey.connolly,
u-boot
On Fri, 4 Dec 2026 at 17:50, Ilias Apalodimas
<ilias.apalodimas@linaro.org> wrote:
>
> Hi Tom,
>
> On Fri, 4 Dec 2026 at 17:12, Tom Rini <trini@konsulko.com> wrote:
> >
> > On Fri, Dec 04, 2026 at 08:44:04AM +0200, Ilias Apalodimas wrote:
> > > On Fri, 4 Dec 2026 at 01:02, Tom Rini <trini@konsulko.com> wrote:
> > > >
> > > > On Fri, Dec 04, 2026 at 12:53:01AM +0200, Ilias Apalodimas wrote:
> > > > > On Fri, 4 Dec 2026 at 00:42, Tom Rini <trini@konsulko.com> wrote:
> > > > > >
> > > > > > On Fri, Dec 04, 2026 at 12:27:57AM +0200, Ilias Apalodimas wrote:
> > > > > > > Hi Tom
> > > > > > >
> > > > > > > On Fri, 27 Nov 2026 at 20:46, Tom Rini <trini@konsulko.com> wrote:
> > > > > > > >
> > > > > > > > On Thu, Nov 26, 2026 at 05:14:26PM +0200, Ilias Apalodimas wrote:
> > > > > > > > > Hi both
> > > > > > > > >
> > > > > > > > > On Thu, 26 Nov 2026 at 14:48, Peter Robinson <pbrobinson@gmail.com> wrote:
> > > > > > > > > >
> > > > > > > > > >
> > > > > > > > > >
> > > > > > > > > > On Wed, 25 Nov 2026 at 17:20, Quentin Schulz <quentin.schulz@cherry.de> wrote:
> > > > > > > > > >>
> > > > > > > > > >> Hi Ilias,
> > > > > > > > > >>
> > > > > > > > > >> On 11/25/26 8:41 AM, Ilias Apalodimas wrote:
> > > > > > > > > >> > Linking U-Boot with -pie emits an R_AARCH64_RELATIVE entry for every
> > > > > > > > > >> > pointer that needs fixing up. Each Elf64_Rela entry is 24 bytes, so the
> > > > > > > > > >> > .rela.dyn section ends up being quite large.
> > > > > > > > > >> >
> > > > > > > > > >> > Modern toolchains support packing relative relocations in RELR format
> > > > > > > > > >> > via '-z pack-relative-relocs'. Instead of offset/info/addend tuples,
> > > > > > > > > >> > RELR stores a stream of 64-bit words. An even word is the link-time
> > > > > > > > > >> > address of a word to relocate and an odd word is a bitmap describing
> > > > > > > > > >> > which of the 63 words following the previous address need relocating.
> > > > > > > > > >> > The addend is the value already stored in the place.
> > > > > > > > > >> >
> > > > > > > > > >> > Compiling for QEMU with RELA vs RELR looks something like this:
> > > > > > > > > >> >
> > > > > > > > > >> > $~ readelf -SW u-boot | grep relr
> > > > > > > > > >> > [ 9] .relr.dyn RELR 000000000016f970 170970 000960 08 A 0 0 8
> > > > > > > > > >> > $~ readelf -SW u-boot.old | grep rela
> > > > > > > > > >> > [ 7] .rela.dyn RELA 0000000000115110 116110 0184f8 18 A 0 0 8
> > > > > > > > > >> >
> > > > > > > > > >> > which is ~98% decrease in the space reloc entries need.
> > > > > > > > > >> >
> > > > > > > > > >> > So let's add a Kconfig option and teach U-Boot RELR.
> > > > > > > > > >> > - pass '-z pack-relative-relocs' to the linker and add .relr.dyn
> > > > > > > > > >> > in the linker script and the objcopy'ed binary. relr.dyn is still
> > > > > > > > > >> > overlayed by .bss
> > > > > > > > > >> > - skip checkarmreloc, since we now expect R_AARCH64_RELATIVE entries
> > > > > > > > > >> > to be packed in .rel*.dyn and older tools don't decode them
> > > > > > > > > >> > properly
> > > > > > > > > >> > - process .relr.dyn in pie_fixup. It's worth noting that efi runtime
> > > > > > > > > >> > relocs, due to how ld emits RELR, are now packed in .relr.dyn and
> > > > > > > > > >> > efi_runtime_relocate() doesn't have to relocate anything.
> > > > > > > > > >> > A sideeffect is that we can't support SetVirtualAddressMap and ConvertPointer
> > > > > > > > > >> > for EFI, but some architectures are already ommitting this call
> > > > > > > > > >> > (e.g Linux on arm64, openBSD doesn't call it at all). [0]
> > > > > > > > > >> > - process .relr.dyn in relocate_code(). Note that the places are
> > > > > > > > > >> > link-time addresses, so we still need the link-to-copy offset to find
> > > > > > > > > >> > them, but the stored value has already been shifted to the run address
> > > > > > > > > >> > by pie_fixup, so it needs to be adjusted by the run-to-copy offset
> > > > > > > > > >> >
> > > > > > > > > >> > [0] EFI runtime services relocate themselves when the OS calls
> > > > > > > > > >> > SetVirtualAddressMap, using the relocations between
> > > > > > > > > >> > __efi_runtime_rel_start and __efi_runtime_rel_stop. Those are collected
> > > > > > > > > >> > by the linker script from the .rela sections of the runtime code.
> > > > > > > > > >> > RELR packing, however, is done by the linker as a last step and all
> > > > > > > > > >> > relative relocations end up in a single .relr.dyn section for the entire
> > > > > > > > > >> > image. There's no way to emit an equivalent __efi_runtime_relr_start
> > > > > > > > > >> > marker for the runtime region, or emit RELA relocations for the EFI
> > > > > > > > > >> > runtime, so stop advertising SetVirtualAddressMap and ConvertPointer
> > > > > > > > > >> > in the EFI_RT_PROPERTIES_TABLE when RELR is enabled.
> > > > > > > > > >> >
> > > > > > > > > >>
> > > > > > > > > >> 6.4% size reduction for u-boot.itb with RELR enabled on
> > > > > > > > > >> tiger-rk3588_defconfig.
> > > > > > > > > >>
> > > > > > > > > >> $ aarch64-linux-gnu-ld --version
> > > > > > > > > >> GNU ld version 2.47.20260726
> > > > > > > > > >>
> > > > > > > > > >> from Fedora 45.
> > > > > > > > > >>
> > > > > > > > > >> I reached U-Boot proper CLI, which I have been told is sufficient to
> > > > > > > > > >> test this series, so:
> > > > > > > > > >
> > > > > > > > > >
> > > > > > > > > > I agree with enabling this by default, it should be opt out if there's issues.
> > > > > > > > > >
> > > > > > > > > > Looking at various default toolchains across distros (thanks Claude) Ubuntu 22.04 LTS has gcc11/binutils 2.38, RHEL 9 is gcc11/binutils 2.35.2, Debian 12 is gcc12.2/binutils 2.40
> > > > > > > > > >
> > > > > > > > > > All have options for later versions so I think we should be fine to enable by default and error if the toolchain isn't new enough. The reality is a lot of users will be using old U-Boot if they're on ancient distros anyway.
> > > > > > > > > >
> > > > > > > > > >>
> > > > > > > > > >> suggestion: make this transparent to the user
> > > > > > > > > >>
> > > > > > > > > >> We've talked about this in private but wanted to put it in writing so
> > > > > > > > > >> it's available on the mailing list archive. I think it makes more sense
> > > > > > > > > >> to always enable the option (if it's safe!), if the linker supports it.
> > > > > > > > > >> According to
> > > > > > > > > >> https://rfc.archlinux.page/0023-pack-relative-relocs/#motivation, that
> > > > > > > > > >> is GNU ld 2.36 and LLVM 15. We can use the same mechanism as in the
> > > > > > > > > >> kernel, c.f. CC_HAS_UBSAN_BOUNDS_STRICT with def_bool
> > > > > > > > > >> $(cc-option,-fsanitize=bounds-strict).
> > > > > > > > >
> > > > > > > > > I don't think that's a good idea. You turn on an option that promises
> > > > > > > > > to reduce the binary size. You compile but bercause you now made the
> > > > > > > > > linker flags conditional the end user finds an *identical* binary
> > > > > > > > > after enabling the option. I prefer breaking the build so users
> > > > > > > > > understand that this relies on a newer linker. Whether they can update
> > > > > > > > > it or not should not be our problem.
> > > > > > > >
> > > > > > > > So, we should be doing depends on LD_HAS_RELR_SUPPORT in the new options
> > > > > > > > and LD_HAS_RELR_SUPPORT is def_bool ..., and then the new option can be
> > > > > > > > default y.
> > > > > > >
> > > > > > > Ok, I can change thayt
> > > > > > >
> > > > > > > >
> > > > > > > > > >> What I don't like about it is that if it's default off, nobody will turn
> > > > > > > > > >> it on. If it's on and we force the option like in this patch, then
> > > > > > > > > >> builds will fail on older toolchains. I bet most people don't know where
> > > > > > > > > >> to get a newer toolchain than the one they have from their distro, or
> > > > > > > > > >> how to install it so lots of headaches for people I guess. If we stay
> > > > > > > > > >> with this solution here, is there a way to tell the user how to fix it
> > > > > > > > > >> (either via a Make message, or at least a comment next to the places
> > > > > > > > > >> where we set the flag?)
> > > > > > > > >
> > > > > > > > > There should be enough explanation in the commit message and Kconfig
> > > > > > > > > option why this is not enabled by default.
> > > > > > > > > TL;DR there are implications wrt to EFI if you enable RELR that do not
> > > > > > > > > affect Linux for arm64, but Linux is not the only OS we boot. Since we
> > > > > > > > > have it documented, I'd rather leave the option off, at least till the
> > > > > > > > > EFI implications are gone.
> > > > > > > >
> > > > > > > > Are the EFI implications something we can / will resolve, or should we
> > > > > > > > be asking the other OSes?
> > > > > > >
> > > > > > > We can but it requires many more changes in the EFI subsystem.
> > > > > > >
> > > > > > > > Or, since the BSD folks do make it easy enough
> > > > > > > > to try them out, should the pushback be on you to fire up an installer
> > > > > > > > image or two? Or... is this something that won't blow up immediately and
> > > > > > > > rather you'd have to do some non-trivial testing to confirm?
> > > > > > >
> > > > > > > In theory (if the OS isn't buggy), nothing will blow up. When RELR is
> > > > > > > enabled SetVirtualAddressMap is marked as unsupported. So worst case
> > > > > > > is the OS will end up running without them. OS'es that avoid calling
> > > > > > > it, just map RT servives 1:1 already and work fine
> > > > > >
> > > > > > It sounds like we should document the needed EFI change then, but not
> > > > > > proactively do it?
> > > > >
> > > > > Sure, but where? In the commit message? docs?
> > > >
> > > > How about doc/api/efi.rst ? But..
> > >
> > > I don't mind, OTOH the commit message explains enough already, I could
> > > just expand it.
> > >
> > > >
> > > > >
> > > > > > But to try and be clearer, is it easy to test at
> > > > > > run-time (in the OS) or a quick grep around the kernel sources, if say a
> > > > > > BSD is relying on SetVirtualAddressMap being supported, today?
> > > > >
> > > > > From memory, openBSD doesn't, but FreeBSD does. I can double check
> > > > > tomorrow, but that was the main reason I wanted the feature to be opt
> > > > > in for now.
> > > >
> > > > Ah, OK. So some FOSS OSes do need it. OK, yes. Is it a lot of work
> > > > that's needed, or just a bit more sit down and do it work?
> > >
> > > It requires significant work. We'll have to move all of the code
> > > that's annotated with efi_runtime_* in a different folder, compile it
> > > as an object and partially link it to U-Boot. What we currently do
> > > under RELA is keep a separate section of efi_runtime* code and we
> > > relocate that part only during SVAM. However, the RELR changes are
> > > orthogonal. We can still pull this for anyone that *knows* he is
> > > booting an OS that doesn't call SVAM, but keep it opt-in, and then
> > > enable it by default once the EFI problems are gone.
> >
> > There's annotations in 18 files, but a few of them aren't arm. If we
> > *only* need to refactor the files, that doesn't sound too bad, and we
> > should be able to keep the directory structure and manipulate the link
> > flags for making one object which is then linked to built-in.o, yes?
>
> It's not that simple (and I don't know all the details yet, I am still
> reading up). The problem right now is that the linker bundles all the
> relocation in relr.dyn, so we loose access to to the efi.rela.dyn we
> keep to enable runtime relocation. Also we overlay .rel* with .bss, so
> once we relocate we permanently loose all of the relocation data. The
> idea is to compile those new files as a standalone binary (with -pie)
> and preserve the relr relocations for that special binary, so we can
> use them once SVAM is called by the OS. U-Boot will then have to copy
> & relocate that EFI RT binary on init. There will also be implications
> for functions that both U-Boot and the EFI RT will need, e.g crc32.
> Tke kernels' EFI stub is doing something very similar, so that's what
> I am looking currently to understand the whole process.
And while reading up. The self contained EFI-RT binary ideally can be
compiled and linked to be pc-relative, so it wont need to relocate
anything (again looking ideas from the kernel)
>
> > I
> > thought there were examples of that but I don't see one quickly. Because
> > I'd really rather do all this at once, rather than RELR now and fix EFI
> > later, since EFI is default and it's not a commercial OS that needs it,
> > but another FOSS OS.
>
> That's ok, I don't mind keeping this patch until the EFI part is
> fixed. The files it touches do not change often, so rebasing it isnt
> too much effort
>
> Thanks
> /Ilias
> >
> > --
> > Tom
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH 2/2] arm64: Add RELR relocs
2026-11-25 7:41 ` [PATCH 2/2] arm64: Add RELR relocs Ilias Apalodimas
2026-11-25 17:20 ` Quentin Schulz
@ 2026-11-26 16:35 ` Richard Henderson
2026-11-26 16:58 ` Ilias Apalodimas
1 sibling, 1 reply; 25+ messages in thread
From: Richard Henderson @ 2026-11-26 16:35 UTC (permalink / raw)
To: Ilias Apalodimas, trini; +Cc: casey.connolly, u-boot
On 11/24/26 23:41, Ilias Apalodimas wrote:
> ifneq ($(CONFIG_XPL_BUILD),y)
> -# Check that only R_ARM_RELATIVE relocations are generated.
> +# Check that only R_ARM_RELATIVE relocations are generated
> +# unless we compile with RELR
> +ifneq ($(CONFIG_RELR),y)
> INPUTS-y += checkarmreloc
> +endif
No, you should still run this. If there are non-relative relocs they
won't be packed. You still want the build to fail if something goes wrong.
Alternately, a different check that ensures *only* relr exists.
> +++ b/arch/arm/cpu/armv8/start.S
> @@ -89,6 +89,7 @@ pie_fixup:
> add x2, x2, #:lo12:__rel_dyn_start
> adrp x3, __rel_dyn_end /* x3 <- Runtime &__rel_dyn_end */
> add x3, x3, #:lo12:__rel_dyn_end
> + b pie_skip_reloc /* .rela.dyn may be empty */
> pie_fix_loop:
> ldp x0, x1, [x2], #16 /* (x0, x1) <- (Link location, fixup) */
> ldr x4, [x2], #8 /* x4 <- addend */
> @@ -101,6 +102,43 @@ pie_fix_loop:
> pie_skip_reloc:
> cmp x2, x3
> b.lo pie_fix_loop
> +
> +#ifdef CONFIG_RELR
With RELR, the above pie_fix_loop *will* be empty, not may be.
So why are you compiling it in at all?
> + /*
> + * Fix .relr.dyn relocations. Entries hold link-time addresses
> + * while x9 holds the run-vs-link offset and the addend is the value
> + * already stored in the place.
> + */
> + adrp x2, __relr_dyn_start /* x2 <- Runtime &__relr_dyn_start */
> + add x2, x2, #:lo12:__relr_dyn_start
> + adrp x3, __relr_dyn_end /* x3 <- Runtime &__relr_dyn_end */
> + add x3, x3, #:lo12:__relr_dyn_end
> + b pie_relr_next
> +pie_relr_loop:
> + ldr x4, [x2], #8 /* x4 <- RELR entry */
> + tbnz x4, #0, pie_relr_bitmap
> + add x5, x4, x9 /* x5 <- Runtime address of the word */
> + ldr x6, [x5]
> + add x6, x6, x9
> + str x6, [x5], #8 /* x5 <- Start of the next bitmap */
> + b pie_relr_next
> +pie_relr_bitmap:
> + mov x7, x5
> + lsr x4, x4, #1
> +pie_relr_bit:
> + tbz x4, #0, pie_relr_skip_bit
> + ldr x6, [x7]
> + add x6, x6, x9
> + str x6, [x7]
> +pie_relr_skip_bit:
> + add x7, x7, #8
> + lsr x4, x4, #1
> + cbnz x4, pie_relr_bit
> + add x5, x5, #(63 * 8) /* Each bitmap covers 63 words */
> +pie_relr_next:
> + cmp x2, x3
> + b.lo pie_relr_loop
> +#endif
> pie_fixup_done:
> #endif
>
> diff --git a/arch/arm/cpu/armv8/u-boot.lds b/arch/arm/cpu/armv8/u-boot.lds
> index 231ab28b50cd..73f0230629cc 100644
> --- a/arch/arm/cpu/armv8/u-boot.lds
> +++ b/arch/arm/cpu/armv8/u-boot.lds
> @@ -149,6 +149,15 @@ SECTIONS
> . = ALIGN(8);
> }
>
> +#ifdef CONFIG_RELR
> + .relr.dyn ALIGN (8) : {
> + __relr_dyn_start = .;
> + *(.relr.dyn)
> + __relr_dyn_end = .;
> + . = ALIGN(8);
> + }
> +#endif
> +
> _end = .;
>
> /*
> diff --git a/arch/arm/lib/relocate_64.c b/arch/arm/lib/relocate_64.c
> index 6f7a50f2264e..0572fe2d29f8 100644
> --- a/arch/arm/lib/relocate_64.c
> +++ b/arch/arm/lib/relocate_64.c
> @@ -43,12 +43,19 @@ void notrace relocate_code(ulong dest)
> u64 size = (u64)__image_copy_end - src;
> const Elf64_Rela *rela = (const Elf64_Rela *)__rel_dyn_start;
> const Elf64_Rela *rela_end = (const Elf64_Rela *)__rel_dyn_end;
> +#if IS_ENABLED(CONFIG_RELR)
> + const u64 *relr = (const u64 *)__relr_dyn_start;
> + const u64 *relr_end = (const u64 *)__relr_dyn_end;
> + u64 run_to_copy;
> + u64 *place = NULL;
> +#endif
> u64 sctlr;
> u64 offset;
>
> if (src == dest)
> return;
> memcpy((void *)dest, (void *)src, size);
> +
> /*
> * r_offset and r_addend are link-time values, so the offset to apply
> * is link-to-copy
> @@ -65,6 +72,27 @@ void notrace relocate_code(ulong dest)
> *(u64 *)(rela->r_offset + offset) = rela->r_addend + offset;
> }
>
> +#if IS_ENABLED(CONFIG_RELR)
> + /*
> + * RELR entries hold link-time addresses, so the location is found
> + * with the link-to-copy offset. The addend is the value already
> + * stored in the word, which pie_fixup has shifted to the run
> + * address, so it is adjusted by the run-to-copy offset.
> + */
> + run_to_copy = dest - src;
> + for (; relr < relr_end; relr++) {
> + if ((*relr & 1) == 0) {
> + place = (u64 *)(*relr + offset);
> + *place++ += run_to_copy;
> + } else {
> + for (u64 *p = place, r = *relr >> 1; r; p++, r >>= 1)
> + if (r & 1)
> + *p += run_to_copy;
> + place += 63;
> + }
> + }
> +#endif
Similarly.
r~
^ permalink raw reply [flat|nested] 25+ messages in thread* Re: [PATCH 2/2] arm64: Add RELR relocs
2026-11-26 16:35 ` Richard Henderson
@ 2026-11-26 16:58 ` Ilias Apalodimas
2026-11-26 20:51 ` Ilias Apalodimas
0 siblings, 1 reply; 25+ messages in thread
From: Ilias Apalodimas @ 2026-11-26 16:58 UTC (permalink / raw)
To: Richard Henderson; +Cc: trini, casey.connolly, u-boot
Hi Richard,
Thanks for taking the time!
On Thu, 26 Nov 2026 at 18:35, Richard Henderson
<richard.henderson@linaro.org> wrote:
>
> On 11/24/26 23:41, Ilias Apalodimas wrote:
> > ifneq ($(CONFIG_XPL_BUILD),y)
> > -# Check that only R_ARM_RELATIVE relocations are generated.
> > +# Check that only R_ARM_RELATIVE relocations are generated
> > +# unless we compile with RELR
> > +ifneq ($(CONFIG_RELR),y)
> > INPUTS-y += checkarmreloc
> > +endif
>
> No, you should still run this. If there are non-relative relocs they
> won't be packed. You still want the build to fail if something goes wrong.
>
> Alternately, a different check that ensures *only* relr exists.
Fair enough. Tbh I tried to preserve the logic but readelf -rW seemed
to produce unreadable output. At least not so readable as
'0000000e3c68 000000000403 R_AARCH64_RELATIV
ff531'.
OTOH I was lazy enough and I (wrongly?) assumed that this couldn't be
detected yet, so I mentioned it in the commit message.
>
> > +++ b/arch/arm/cpu/armv8/start.S
> > @@ -89,6 +89,7 @@ pie_fixup:
> > add x2, x2, #:lo12:__rel_dyn_start
> > adrp x3, __rel_dyn_end /* x3 <- Runtime &__rel_dyn_end */
> > add x3, x3, #:lo12:__rel_dyn_end
> > + b pie_skip_reloc /* .rela.dyn may be empty */
> > pie_fix_loop:
> > ldp x0, x1, [x2], #16 /* (x0, x1) <- (Link location, fixup) */
> > ldr x4, [x2], #8 /* x4 <- addend */
> > @@ -101,6 +102,43 @@ pie_fix_loop:
> > pie_skip_reloc:
> > cmp x2, x3
> > b.lo pie_fix_loop
> > +
> > +#ifdef CONFIG_RELR
>
> With RELR, the above pie_fix_loop *will* be empty, not may be.
> So why are you compiling it in at all?
Is it always absent? Maybe I read this wrong but packed structs that
might end up being misaligned will still end up in rela.dyn.
>
> > + /*
> > + * Fix .relr.dyn relocations. Entries hold link-time addresses
> > + * while x9 holds the run-vs-link offset and the addend is the value
> > + * already stored in the place.
> > + */
> > + adrp x2, __relr_dyn_start /* x2 <- Runtime &__relr_dyn_start */
> > + add x2, x2, #:lo12:__relr_dyn_start
> > + adrp x3, __relr_dyn_end /* x3 <- Runtime &__relr_dyn_end */
> > + add x3, x3, #:lo12:__relr_dyn_end
> > + b pie_relr_next
> > +pie_relr_loop:
> > + ldr x4, [x2], #8 /* x4 <- RELR entry */
> > + tbnz x4, #0, pie_relr_bitmap
> > + add x5, x4, x9 /* x5 <- Runtime address of the word */
> > + ldr x6, [x5]
> > + add x6, x6, x9
> > + str x6, [x5], #8 /* x5 <- Start of the next bitmap */
> > + b pie_relr_next
> > +pie_relr_bitmap:
> > + mov x7, x5
> > + lsr x4, x4, #1
> > +pie_relr_bit:
> > + tbz x4, #0, pie_relr_skip_bit
> > + ldr x6, [x7]
> > + add x6, x6, x9
> > + str x6, [x7]
> > +pie_relr_skip_bit:
> > + add x7, x7, #8
> > + lsr x4, x4, #1
> > + cbnz x4, pie_relr_bit
> > + add x5, x5, #(63 * 8) /* Each bitmap covers 63 words */
> > +pie_relr_next:
> > + cmp x2, x3
> > + b.lo pie_relr_loop
> > +#endif
> > pie_fixup_done:
> > #endif
> >
> > diff --git a/arch/arm/cpu/armv8/u-boot.lds b/arch/arm/cpu/armv8/u-boot.lds
> > index 231ab28b50cd..73f0230629cc 100644
> > --- a/arch/arm/cpu/armv8/u-boot.lds
> > +++ b/arch/arm/cpu/armv8/u-boot.lds
> > @@ -149,6 +149,15 @@ SECTIONS
> > . = ALIGN(8);
> > }
> >
> > +#ifdef CONFIG_RELR
> > + .relr.dyn ALIGN (8) : {
> > + __relr_dyn_start = .;
> > + *(.relr.dyn)
> > + __relr_dyn_end = .;
> > + . = ALIGN(8);
> > + }
> > +#endif
> > +
> > _end = .;
> >
> > /*
> > diff --git a/arch/arm/lib/relocate_64.c b/arch/arm/lib/relocate_64.c
> > index 6f7a50f2264e..0572fe2d29f8 100644
> > --- a/arch/arm/lib/relocate_64.c
> > +++ b/arch/arm/lib/relocate_64.c
> > @@ -43,12 +43,19 @@ void notrace relocate_code(ulong dest)
> > u64 size = (u64)__image_copy_end - src;
> > const Elf64_Rela *rela = (const Elf64_Rela *)__rel_dyn_start;
> > const Elf64_Rela *rela_end = (const Elf64_Rela *)__rel_dyn_end;
> > +#if IS_ENABLED(CONFIG_RELR)
> > + const u64 *relr = (const u64 *)__relr_dyn_start;
> > + const u64 *relr_end = (const u64 *)__relr_dyn_end;
> > + u64 run_to_copy;
> > + u64 *place = NULL;
> > +#endif
> > u64 sctlr;
> > u64 offset;
> >
> > if (src == dest)
> > return;
> > memcpy((void *)dest, (void *)src, size);
> > +
> > /*
> > * r_offset and r_addend are link-time values, so the offset to apply
> > * is link-to-copy
> > @@ -65,6 +72,27 @@ void notrace relocate_code(ulong dest)
> > *(u64 *)(rela->r_offset + offset) = rela->r_addend + offset;
> > }
> >
> > +#if IS_ENABLED(CONFIG_RELR)
> > + /*
> > + * RELR entries hold link-time addresses, so the location is found
> > + * with the link-to-copy offset. The addend is the value already
> > + * stored in the word, which pie_fixup has shifted to the run
> > + * address, so it is adjusted by the run-to-copy offset.
> > + */
> > + run_to_copy = dest - src;
> > + for (; relr < relr_end; relr++) {
> > + if ((*relr & 1) == 0) {
> > + place = (u64 *)(*relr + offset);
> > + *place++ += run_to_copy;
> > + } else {
> > + for (u64 *p = place, r = *relr >> 1; r; p++, r >>= 1)
> > + if (r & 1)
> > + *p += run_to_copy;
> > + place += 63;
> > + }
> > + }
> > +#endif
>
> Similarly.
>
>
> r~
^ permalink raw reply [flat|nested] 25+ messages in thread* Re: [PATCH 2/2] arm64: Add RELR relocs
2026-11-26 16:58 ` Ilias Apalodimas
@ 2026-11-26 20:51 ` Ilias Apalodimas
2026-12-03 22:14 ` Richard Henderson
0 siblings, 1 reply; 25+ messages in thread
From: Ilias Apalodimas @ 2026-11-26 20:51 UTC (permalink / raw)
To: Richard Henderson; +Cc: trini, casey.connolly, u-boot
On Thu, 26 Nov 2026 at 18:58, Ilias Apalodimas
<ilias.apalodimas@linaro.org> wrote:
>
> Hi Richard,
>
> Thanks for taking the time!
>
> On Thu, 26 Nov 2026 at 18:35, Richard Henderson
> <richard.henderson@linaro.org> wrote:
> >
> > On 11/24/26 23:41, Ilias Apalodimas wrote:
> > > ifneq ($(CONFIG_XPL_BUILD),y)
> > > -# Check that only R_ARM_RELATIVE relocations are generated.
> > > +# Check that only R_ARM_RELATIVE relocations are generated
> > > +# unless we compile with RELR
> > > +ifneq ($(CONFIG_RELR),y)
> > > INPUTS-y += checkarmreloc
> > > +endif
> >
> > No, you should still run this. If there are non-relative relocs they
> > won't be packed. You still want the build to fail if something goes wrong.
> >
> > Alternately, a different check that ensures *only* relr exists.
>
> Fair enough. Tbh I tried to preserve the logic but readelf -rW seemed
> to produce unreadable output. At least not so readable as
> '0000000e3c68 000000000403 R_AARCH64_RELATIV
> ff531'.
> OTOH I was lazy enough and I (wrongly?) assumed that this couldn't be
> detected yet, so I mentioned it in the commit message.
Replying to myself here, but looking at the output again we can parse
that only RELA & RELR are emitted.
I'll fix it in v2
>
> >
> > > +++ b/arch/arm/cpu/armv8/start.S
> > > @@ -89,6 +89,7 @@ pie_fixup:
> > > add x2, x2, #:lo12:__rel_dyn_start
> > > adrp x3, __rel_dyn_end /* x3 <- Runtime &__rel_dyn_end */
> > > add x3, x3, #:lo12:__rel_dyn_end
> > > + b pie_skip_reloc /* .rela.dyn may be empty */
> > > pie_fix_loop:
> > > ldp x0, x1, [x2], #16 /* (x0, x1) <- (Link location, fixup) */
> > > ldr x4, [x2], #8 /* x4 <- addend */
> > > @@ -101,6 +102,43 @@ pie_fix_loop:
> > > pie_skip_reloc:
> > > cmp x2, x3
> > > b.lo pie_fix_loop
> > > +
> > > +#ifdef CONFIG_RELR
> >
> > With RELR, the above pie_fix_loop *will* be empty, not may be.
> > So why are you compiling it in at all?
>
> Is it always absent? Maybe I read this wrong but packed structs that
> might end up being misaligned will still end up in rela.dyn.
So something (nasty) like the patch below
--- a/board/emulation/qemu-arm/qemu-arm.c
+++ b/board/emulation/qemu-arm/qemu-arm.c
@@ -102,6 +102,12 @@ static struct mm_region qemu_arm64_mem_map[] = {
struct mm_region *mem_map = qemu_arm64_mem_map;
#endif
+struct relr_test {
+ u8 dummy;
+ void *ptr;
+} __packed;
+
+struct relr_test relr_test = { .dummy = 1, .ptr = qemu_arm64_mem_map };
int board_late_init(void)
{
@@ -109,6 +115,7 @@ int board_late_init(void)
* Make sure virtio bus is enumerated so that peripherals
* on the virtio bus can be discovered by their drivers
*/
+ printf("relr_test.ptr %p\n", relr_test.ptr);
virtio_init();
produces both rela & relr for me.
$~/work/u-boot-tpm>readelf -SW u-boot
[...]
[ 9] .rela.dyn RELA 000000000013f810 140810
000018 18 A 0 0 8
[10] .relr.dyn RELR 000000000013f828 140828
000800 08 A 0 0 8
Cheers
/Ilias
> >
> > > + /*
> > > + * Fix .relr.dyn relocations. Entries hold link-time addresses
> > > + * while x9 holds the run-vs-link offset and the addend is the value
> > > + * already stored in the place.
> > > + */
> > > + adrp x2, __relr_dyn_start /* x2 <- Runtime &__relr_dyn_start */
> > > + add x2, x2, #:lo12:__relr_dyn_start
> > > + adrp x3, __relr_dyn_end /* x3 <- Runtime &__relr_dyn_end */
> > > + add x3, x3, #:lo12:__relr_dyn_end
> > > + b pie_relr_next
> > > +pie_relr_loop:
> > > + ldr x4, [x2], #8 /* x4 <- RELR entry */
> > > + tbnz x4, #0, pie_relr_bitmap
> > > + add x5, x4, x9 /* x5 <- Runtime address of the word */
> > > + ldr x6, [x5]
> > > + add x6, x6, x9
> > > + str x6, [x5], #8 /* x5 <- Start of the next bitmap */
> > > + b pie_relr_next
> > > +pie_relr_bitmap:
> > > + mov x7, x5
> > > + lsr x4, x4, #1
> > > +pie_relr_bit:
> > > + tbz x4, #0, pie_relr_skip_bit
> > > + ldr x6, [x7]
> > > + add x6, x6, x9
> > > + str x6, [x7]
> > > +pie_relr_skip_bit:
> > > + add x7, x7, #8
> > > + lsr x4, x4, #1
> > > + cbnz x4, pie_relr_bit
> > > + add x5, x5, #(63 * 8) /* Each bitmap covers 63 words */
> > > +pie_relr_next:
> > > + cmp x2, x3
> > > + b.lo pie_relr_loop
> > > +#endif
> > > pie_fixup_done:
> > > #endif
> > >
> > > diff --git a/arch/arm/cpu/armv8/u-boot.lds b/arch/arm/cpu/armv8/u-boot.lds
> > > index 231ab28b50cd..73f0230629cc 100644
> > > --- a/arch/arm/cpu/armv8/u-boot.lds
> > > +++ b/arch/arm/cpu/armv8/u-boot.lds
> > > @@ -149,6 +149,15 @@ SECTIONS
> > > . = ALIGN(8);
> > > }
> > >
> > > +#ifdef CONFIG_RELR
> > > + .relr.dyn ALIGN (8) : {
> > > + __relr_dyn_start = .;
> > > + *(.relr.dyn)
> > > + __relr_dyn_end = .;
> > > + . = ALIGN(8);
> > > + }
> > > +#endif
> > > +
> > > _end = .;
> > >
> > > /*
> > > diff --git a/arch/arm/lib/relocate_64.c b/arch/arm/lib/relocate_64.c
> > > index 6f7a50f2264e..0572fe2d29f8 100644
> > > --- a/arch/arm/lib/relocate_64.c
> > > +++ b/arch/arm/lib/relocate_64.c
> > > @@ -43,12 +43,19 @@ void notrace relocate_code(ulong dest)
> > > u64 size = (u64)__image_copy_end - src;
> > > const Elf64_Rela *rela = (const Elf64_Rela *)__rel_dyn_start;
> > > const Elf64_Rela *rela_end = (const Elf64_Rela *)__rel_dyn_end;
> > > +#if IS_ENABLED(CONFIG_RELR)
> > > + const u64 *relr = (const u64 *)__relr_dyn_start;
> > > + const u64 *relr_end = (const u64 *)__relr_dyn_end;
> > > + u64 run_to_copy;
> > > + u64 *place = NULL;
> > > +#endif
> > > u64 sctlr;
> > > u64 offset;
> > >
> > > if (src == dest)
> > > return;
> > > memcpy((void *)dest, (void *)src, size);
> > > +
> > > /*
> > > * r_offset and r_addend are link-time values, so the offset to apply
> > > * is link-to-copy
> > > @@ -65,6 +72,27 @@ void notrace relocate_code(ulong dest)
> > > *(u64 *)(rela->r_offset + offset) = rela->r_addend + offset;
> > > }
> > >
> > > +#if IS_ENABLED(CONFIG_RELR)
> > > + /*
> > > + * RELR entries hold link-time addresses, so the location is found
> > > + * with the link-to-copy offset. The addend is the value already
> > > + * stored in the word, which pie_fixup has shifted to the run
> > > + * address, so it is adjusted by the run-to-copy offset.
> > > + */
> > > + run_to_copy = dest - src;
> > > + for (; relr < relr_end; relr++) {
> > > + if ((*relr & 1) == 0) {
> > > + place = (u64 *)(*relr + offset);
> > > + *place++ += run_to_copy;
> > > + } else {
> > > + for (u64 *p = place, r = *relr >> 1; r; p++, r >>= 1)
> > > + if (r & 1)
> > > + *p += run_to_copy;
> > > + place += 63;
> > > + }
> > > + }
> > > +#endif
> >
> > Similarly.
> >
> >
> > r~
^ permalink raw reply [flat|nested] 25+ messages in thread* Re: [PATCH 2/2] arm64: Add RELR relocs
2026-11-26 20:51 ` Ilias Apalodimas
@ 2026-12-03 22:14 ` Richard Henderson
0 siblings, 0 replies; 25+ messages in thread
From: Richard Henderson @ 2026-12-03 22:14 UTC (permalink / raw)
To: Ilias Apalodimas; +Cc: trini, casey.connolly, u-boot
On 11/26/26 12:51, Ilias Apalodimas wrote:
> On Thu, 26 Nov 2026 at 18:58, Ilias Apalodimas
> <ilias.apalodimas@linaro.org> wrote:
>>
>> Hi Richard,
>>
>> Thanks for taking the time!
>>
>> On Thu, 26 Nov 2026 at 18:35, Richard Henderson
>> <richard.henderson@linaro.org> wrote:
>>>
>>> On 11/24/26 23:41, Ilias Apalodimas wrote:
>>>> ifneq ($(CONFIG_XPL_BUILD),y)
>>>> -# Check that only R_ARM_RELATIVE relocations are generated.
>>>> +# Check that only R_ARM_RELATIVE relocations are generated
>>>> +# unless we compile with RELR
>>>> +ifneq ($(CONFIG_RELR),y)
>>>> INPUTS-y += checkarmreloc
>>>> +endif
>>>
>>> No, you should still run this. If there are non-relative relocs they
>>> won't be packed. You still want the build to fail if something goes wrong.
>>>
>>> Alternately, a different check that ensures *only* relr exists.
>>
>> Fair enough. Tbh I tried to preserve the logic but readelf -rW seemed
>> to produce unreadable output. At least not so readable as
>> '0000000e3c68 000000000403 R_AARCH64_RELATIV
>> ff531'.
>> OTOH I was lazy enough and I (wrongly?) assumed that this couldn't be
>> detected yet, so I mentioned it in the commit message.
>
> Replying to myself here, but looking at the output again we can parse
> that only RELA & RELR are emitted.
> I'll fix it in v2
Yep.
>>> With RELR, the above pie_fix_loop *will* be empty, not may be.
>>> So why are you compiling it in at all?
>>
>> Is it always absent? Maybe I read this wrong but packed structs that
>> might end up being misaligned will still end up in rela.dyn.
>
> So something (nasty) like the patch below
>
> --- a/board/emulation/qemu-arm/qemu-arm.c
> +++ b/board/emulation/qemu-arm/qemu-arm.c
> @@ -102,6 +102,12 @@ static struct mm_region qemu_arm64_mem_map[] = {
>
> struct mm_region *mem_map = qemu_arm64_mem_map;
> #endif
> +struct relr_test {
> + u8 dummy;
> + void *ptr;
> +} __packed;
> +
> +struct relr_test relr_test = { .dummy = 1, .ptr = qemu_arm64_mem_map };
>
> int board_late_init(void)
> {
> @@ -109,6 +115,7 @@ int board_late_init(void)
> * Make sure virtio bus is enumerated so that peripherals
> * on the virtio bus can be discovered by their drivers
> */
> + printf("relr_test.ptr %p\n", relr_test.ptr);
> virtio_init();
>
> produces both rela & relr for me.
>
> $~/work/u-boot-tpm>readelf -SW u-boot
> [...]
> [ 9] .rela.dyn RELA 000000000013f810 140810
> 000018 18 A 0 0 8
> [10] .relr.dyn RELR 000000000013f828 140828
> 000800 08 A 0 0 8
Granted.
OTOH, we're not attempting to handle generic third-party code, we're
handling stuff in u-boot. Which, without going too far out on a limb,
shouldn't have misaligned pointers. Thankfully, we get to catch any
such mistake at build-time and fix it.
r~
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH 1/2] arm64: relocate: convert relocate_code to C
2026-11-25 7:41 [PATCH 1/2] arm64: relocate: convert relocate_code to C Ilias Apalodimas
2026-11-25 7:41 ` [PATCH 2/2] arm64: Add RELR relocs Ilias Apalodimas
@ 2026-11-25 17:07 ` Quentin Schulz
2026-11-25 17:08 ` Casey Connolly
2026-11-25 17:11 ` Ilias Apalodimas
2026-11-26 16:11 ` Richard Henderson
2 siblings, 2 replies; 25+ messages in thread
From: Quentin Schulz @ 2026-11-25 17:07 UTC (permalink / raw)
To: Ilias Apalodimas, trini, richard.henderson; +Cc: casey.connolly, u-boot
Hi Ilias,
On 11/25/26 8:41 AM, Ilias Apalodimas wrote:
> relocate_code() on arm64 is called from _main with sp and gd already
> pointing at the relocated area and lr pointing to the relocated copy of
> relocation_return. By that time pie_fixup has already run, if
> POSITION_INDEPENDENT is enabled, the image is fixed up and plain C works
> fine, like the rest of board_init_f(). So let's convert it to C which is
> much easier to read and maintain.
>
> While at it change the behaviour and order of cache maintenance.
> Invalidate the I-cache after the D-cache clean instead of before
> and add a 'dsb' between 'ic iallu' and the 'isb', which is the
> architecturally required sequence.
>
suggestion: can you please make those separate commits?
That should help us if we ever need to bisect this change. If it was
only in the conversion from ASM to C, or if it's related to the cache
changes?
Thanks!
Quentin
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH 1/2] arm64: relocate: convert relocate_code to C
2026-11-25 17:07 ` [PATCH 1/2] arm64: relocate: convert relocate_code to C Quentin Schulz
@ 2026-11-25 17:08 ` Casey Connolly
2026-11-25 17:11 ` Ilias Apalodimas
1 sibling, 0 replies; 25+ messages in thread
From: Casey Connolly @ 2026-11-25 17:08 UTC (permalink / raw)
To: Quentin Schulz, Ilias Apalodimas, trini, richard.henderson; +Cc: u-boot
On 11/25/26 17:07, Quentin Schulz wrote:
> Hi Ilias,
>
> On 11/25/26 8:41 AM, Ilias Apalodimas wrote:
>> relocate_code() on arm64 is called from _main with sp and gd already
>> pointing at the relocated area and lr pointing to the relocated copy of
>> relocation_return. By that time pie_fixup has already run, if
>> POSITION_INDEPENDENT is enabled, the image is fixed up and plain C works
>> fine, like the rest of board_init_f(). So let's convert it to C which is
>> much easier to read and maintain.
>>
>> While at it change the behaviour and order of cache maintenance.
>> Invalidate the I-cache after the D-cache clean instead of before
>> and add a 'dsb' between 'ic iallu' and the 'isb', which is the
>> architecturally required sequence.
>>
>
> suggestion: can you please make those separate commits?
>
> That should help us if we ever need to bisect this change. If it was
> only in the conversion from ASM to C, or if it's related to the cache
> changes?
+1
Tested-by: Casey Connolly <casey.connolly@linaro.org>
>
> Thanks!
> Quentin
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH 1/2] arm64: relocate: convert relocate_code to C
2026-11-25 17:07 ` [PATCH 1/2] arm64: relocate: convert relocate_code to C Quentin Schulz
2026-11-25 17:08 ` Casey Connolly
@ 2026-11-25 17:11 ` Ilias Apalodimas
2026-11-27 18:35 ` Tom Rini
1 sibling, 1 reply; 25+ messages in thread
From: Ilias Apalodimas @ 2026-11-25 17:11 UTC (permalink / raw)
To: Quentin Schulz, trini; +Cc: richard.henderson, casey.connolly, u-boot
On Wed, 25 Nov 2026 at 19:07, Quentin Schulz <quentin.schulz@cherry.de> wrote:
>
> Hi Ilias,
>
> On 11/25/26 8:41 AM, Ilias Apalodimas wrote:
> > relocate_code() on arm64 is called from _main with sp and gd already
> > pointing at the relocated area and lr pointing to the relocated copy of
> > relocation_return. By that time pie_fixup has already run, if
> > POSITION_INDEPENDENT is enabled, the image is fixed up and plain C works
> > fine, like the rest of board_init_f(). So let's convert it to C which is
> > much easier to read and maintain.
> >
> > While at it change the behaviour and order of cache maintenance.
> > Invalidate the I-cache after the D-cache clean instead of before
> > and add a 'dsb' between 'ic iallu' and the 'isb', which is the
> > architecturally required sequence.
> >
>
> suggestion: can you please make those separate commits?
>
> That should help us if we ever need to bisect this change. If it was
> only in the conversion from ASM to C, or if it's related to the cache
> changes?
Yea I don't mind. But given that the older version was wrong and this
is the sequence defined by the architecture, I was aiming to have it
correct from the original commit.
Tom any preference?
Cheers
/Ilias
>
> Thanks!
> Quentin
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH 1/2] arm64: relocate: convert relocate_code to C
2026-11-25 17:11 ` Ilias Apalodimas
@ 2026-11-27 18:35 ` Tom Rini
0 siblings, 0 replies; 25+ messages in thread
From: Tom Rini @ 2026-11-27 18:35 UTC (permalink / raw)
To: Ilias Apalodimas
Cc: Quentin Schulz, richard.henderson, casey.connolly, u-boot
[-- Attachment #1: Type: text/plain, Size: 1468 bytes --]
On Wed, Nov 25, 2026 at 07:11:13PM +0200, Ilias Apalodimas wrote:
> On Wed, 25 Nov 2026 at 19:07, Quentin Schulz <quentin.schulz@cherry.de> wrote:
> >
> > Hi Ilias,
> >
> > On 11/25/26 8:41 AM, Ilias Apalodimas wrote:
> > > relocate_code() on arm64 is called from _main with sp and gd already
> > > pointing at the relocated area and lr pointing to the relocated copy of
> > > relocation_return. By that time pie_fixup has already run, if
> > > POSITION_INDEPENDENT is enabled, the image is fixed up and plain C works
> > > fine, like the rest of board_init_f(). So let's convert it to C which is
> > > much easier to read and maintain.
> > >
> > > While at it change the behaviour and order of cache maintenance.
> > > Invalidate the I-cache after the D-cache clean instead of before
> > > and add a 'dsb' between 'ic iallu' and the 'isb', which is the
> > > architecturally required sequence.
> > >
> >
> > suggestion: can you please make those separate commits?
> >
> > That should help us if we ever need to bisect this change. If it was
> > only in the conversion from ASM to C, or if it's related to the cache
> > changes?
>
> Yea I don't mind. But given that the older version was wrong and this
> is the sequence defined by the architecture, I was aiming to have it
> correct from the original commit.
>
> Tom any preference?
I think Quentin's point is valid, and correcting the order in C,
afterwards, is easy.
--
Tom
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: [PATCH 1/2] arm64: relocate: convert relocate_code to C
2026-11-25 7:41 [PATCH 1/2] arm64: relocate: convert relocate_code to C Ilias Apalodimas
2026-11-25 7:41 ` [PATCH 2/2] arm64: Add RELR relocs Ilias Apalodimas
2026-11-25 17:07 ` [PATCH 1/2] arm64: relocate: convert relocate_code to C Quentin Schulz
@ 2026-11-26 16:11 ` Richard Henderson
2026-11-26 16:18 ` Ilias Apalodimas
2 siblings, 1 reply; 25+ messages in thread
From: Richard Henderson @ 2026-11-26 16:11 UTC (permalink / raw)
To: Ilias Apalodimas, trini; +Cc: casey.connolly, u-boot
On 11/24/26 23:41, Ilias Apalodimas wrote:
> +/**
> + * relocate_code() - copy U-Boot to its final location and fix it up
> + *
> + * @dest: destination address, gd->relocaddr
> + *
> + * This is entered from _main in crt0_64.S with sp and gd already
> + * pointing to the relocated area and with lr pointing to the relocated
> + * copy of relocation_return, so returning from here continues execution
> + * in the new image.
> + *
> + * Constraints for this function, since it runs from the old image and
> + * before BSS is usable:
> + * - It must not touch BSS. On arm64 .bss overlays .rela.dyn
> + * - It must not rely on anything that needs relocating in the destination.
> + * Symbol addresses are generated PC-relative (adrp/add, -fno-pic), so they
> + * resolve to the runtime location of the currently executing image, which
> + * is already correct, either because we run at CONFIG_TEXT_BASE or because
> + * pie_fixup has fixed up the running image in place.
> + * - It must not be instrumented.
> + */
> +void notrace relocate_code(ulong dest)
> +{
> + u64 src = (u64)__image_copy_start;
> + u64 size = (u64)__image_copy_end - src;
> + const Elf64_Rela *rela = (const Elf64_Rela *)__rel_dyn_start;
> + const Elf64_Rela *rela_end = (const Elf64_Rela *)__rel_dyn_end;
> + u64 sctlr;
> + u64 offset;
> +
> + if (src == dest)
> + return;
> + memcpy((void *)dest, (void *)src, size);
> + /*
> + * r_offset and r_addend are link-time values, so the offset to apply
> + * is link-to-copy
> + */
> + offset = dest - CONFIG_TEXT_BASE;
> +
> + /*
> + * We have a makefile check that aborts if reloc entries are not
> + * R_AARCH64_RELATIVE.
> + */
> + for (; rela < rela_end; rela++) {
> + if (ELF64_R_TYPE(rela->r_info) != R_AARCH64_RELATIVE)
> + continue;
> + *(u64 *)(rela->r_offset + offset) = rela->r_addend + offset;
> + }
If you've got a build-time check that all reloc entries are RELATIVE,
why are you checking again at runtime?
> +
> + sctlr = get_sctlr();
> + if (sctlr & CR_C) {
> + __asm_flush_dcache_range(dest, dest + size);
> + __asm_flush_l3_dcache();
> + } else {
> + /* Non-cacheable stores need to complete before we fetch new code */
> + asm volatile("dsb sy" : : : "memory");
> + }
> +
> + if (sctlr & CR_I)
> + asm volatile("ic iallu\n\tdsb nsh\n\tisb" : : : "memory");
isb here is redundant with the one below.
> +
> + /* Always discard anything already fetched/prefetched */
> + asm volatile("isb" : : : "memory");
> +}
r~
^ permalink raw reply [flat|nested] 25+ messages in thread* Re: [PATCH 1/2] arm64: relocate: convert relocate_code to C
2026-11-26 16:11 ` Richard Henderson
@ 2026-11-26 16:18 ` Ilias Apalodimas
2026-11-26 17:03 ` Ilias Apalodimas
0 siblings, 1 reply; 25+ messages in thread
From: Ilias Apalodimas @ 2026-11-26 16:18 UTC (permalink / raw)
To: Richard Henderson; +Cc: trini, casey.connolly, u-boot
Hi Richard,
On Thu, 26 Nov 2026 at 18:11, Richard Henderson
<richard.henderson@linaro.org> wrote:
>
> On 11/24/26 23:41, Ilias Apalodimas wrote:
> > +/**
> > + * relocate_code() - copy U-Boot to its final location and fix it up
> > + *
> > + * @dest: destination address, gd->relocaddr
> > + *
> > + * This is entered from _main in crt0_64.S with sp and gd already
> > + * pointing to the relocated area and with lr pointing to the relocated
> > + * copy of relocation_return, so returning from here continues execution
> > + * in the new image.
> > + *
> > + * Constraints for this function, since it runs from the old image and
> > + * before BSS is usable:
> > + * - It must not touch BSS. On arm64 .bss overlays .rela.dyn
> > + * - It must not rely on anything that needs relocating in the destination.
> > + * Symbol addresses are generated PC-relative (adrp/add, -fno-pic), so they
> > + * resolve to the runtime location of the currently executing image, which
> > + * is already correct, either because we run at CONFIG_TEXT_BASE or because
> > + * pie_fixup has fixed up the running image in place.
> > + * - It must not be instrumented.
> > + */
> > +void notrace relocate_code(ulong dest)
> > +{
> > + u64 src = (u64)__image_copy_start;
> > + u64 size = (u64)__image_copy_end - src;
> > + const Elf64_Rela *rela = (const Elf64_Rela *)__rel_dyn_start;
> > + const Elf64_Rela *rela_end = (const Elf64_Rela *)__rel_dyn_end;
> > + u64 sctlr;
> > + u64 offset;
> > +
> > + if (src == dest)
> > + return;
> > + memcpy((void *)dest, (void *)src, size);
> > + /*
> > + * r_offset and r_addend are link-time values, so the offset to apply
> > + * is link-to-copy
> > + */
> > + offset = dest - CONFIG_TEXT_BASE;
> > +
> > + /*
> > + * We have a makefile check that aborts if reloc entries are not
> > + * R_AARCH64_RELATIVE.
> > + */
> > + for (; rela < rela_end; rela++) {
> > + if (ELF64_R_TYPE(rela->r_info) != R_AARCH64_RELATIVE)
> > + continue;
> > + *(u64 *)(rela->r_offset + offset) = rela->r_addend + offset;
> > + }
>
> If you've got a build-time check that all reloc entries are RELATIVE,
> why are you checking again at runtime?
Defensive programming I guess, I can remove it, but I'd rather keep it
in case the Makefile checks ever go away.
>
> > +
> > + sctlr = get_sctlr();
> > + if (sctlr & CR_C) {
> > + __asm_flush_dcache_range(dest, dest + size);
> > + __asm_flush_l3_dcache();
> > + } else {
> > + /* Non-cacheable stores need to complete before we fetch new code */
> > + asm volatile("dsb sy" : : : "memory");
> > + }
> > +
> > + if (sctlr & CR_I)
> > + asm volatile("ic iallu\n\tdsb nsh\n\tisb" : : : "memory");
>
> isb here is redundant with the one below.
Ah fair enough. I need it whether CR_I is 0 or 1 right? So I can just
remove it from the if()
>
> > +
> > + /* Always discard anything already fetched/prefetched */
> > + asm volatile("isb" : : : "memory");
> > +}
>
>
> r~
Thanks
/Ilias
^ permalink raw reply [flat|nested] 25+ messages in thread* Re: [PATCH 1/2] arm64: relocate: convert relocate_code to C
2026-11-26 16:18 ` Ilias Apalodimas
@ 2026-11-26 17:03 ` Ilias Apalodimas
0 siblings, 0 replies; 25+ messages in thread
From: Ilias Apalodimas @ 2026-11-26 17:03 UTC (permalink / raw)
To: Richard Henderson; +Cc: trini, casey.connolly, u-boot
On Thu, 26 Nov 2026 at 18:18, Ilias Apalodimas
<ilias.apalodimas@linaro.org> wrote:
>
> Hi Richard,
>
> On Thu, 26 Nov 2026 at 18:11, Richard Henderson
> <richard.henderson@linaro.org> wrote:
> >
> > On 11/24/26 23:41, Ilias Apalodimas wrote:
> > > +/**
> > > + * relocate_code() - copy U-Boot to its final location and fix it up
> > > + *
> > > + * @dest: destination address, gd->relocaddr
> > > + *
> > > + * This is entered from _main in crt0_64.S with sp and gd already
> > > + * pointing to the relocated area and with lr pointing to the relocated
> > > + * copy of relocation_return, so returning from here continues execution
> > > + * in the new image.
> > > + *
> > > + * Constraints for this function, since it runs from the old image and
> > > + * before BSS is usable:
> > > + * - It must not touch BSS. On arm64 .bss overlays .rela.dyn
> > > + * - It must not rely on anything that needs relocating in the destination.
> > > + * Symbol addresses are generated PC-relative (adrp/add, -fno-pic), so they
> > > + * resolve to the runtime location of the currently executing image, which
> > > + * is already correct, either because we run at CONFIG_TEXT_BASE or because
> > > + * pie_fixup has fixed up the running image in place.
> > > + * - It must not be instrumented.
> > > + */
> > > +void notrace relocate_code(ulong dest)
> > > +{
> > > + u64 src = (u64)__image_copy_start;
> > > + u64 size = (u64)__image_copy_end - src;
> > > + const Elf64_Rela *rela = (const Elf64_Rela *)__rel_dyn_start;
> > > + const Elf64_Rela *rela_end = (const Elf64_Rela *)__rel_dyn_end;
> > > + u64 sctlr;
> > > + u64 offset;
> > > +
> > > + if (src == dest)
> > > + return;
> > > + memcpy((void *)dest, (void *)src, size);
> > > + /*
> > > + * r_offset and r_addend are link-time values, so the offset to apply
> > > + * is link-to-copy
> > > + */
> > > + offset = dest - CONFIG_TEXT_BASE;
> > > +
> > > + /*
> > > + * We have a makefile check that aborts if reloc entries are not
> > > + * R_AARCH64_RELATIVE.
> > > + */
> > > + for (; rela < rela_end; rela++) {
> > > + if (ELF64_R_TYPE(rela->r_info) != R_AARCH64_RELATIVE)
> > > + continue;
> > > + *(u64 *)(rela->r_offset + offset) = rela->r_addend + offset;
> > > + }
> >
> > If you've got a build-time check that all reloc entries are RELATIVE,
> > why are you checking again at runtime?
>
> Defensive programming I guess, I can remove it, but I'd rather keep it
> in case the Makefile checks ever go away.
Actually I remembered why I did this while responding to the second
email. It wasn't defensive. The second patch removes the script
because I couldn't find an easy way to parse RELR + RELA. Also while
reading up on RELR, I thought RELA+RELR can co-exist.
Thanks
/Ilias
>
> >
> > > +
> > > + sctlr = get_sctlr();
> > > + if (sctlr & CR_C) {
> > > + __asm_flush_dcache_range(dest, dest + size);
> > > + __asm_flush_l3_dcache();
> > > + } else {
> > > + /* Non-cacheable stores need to complete before we fetch new code */
> > > + asm volatile("dsb sy" : : : "memory");
> > > + }
> > > +
> > > + if (sctlr & CR_I)
> > > + asm volatile("ic iallu\n\tdsb nsh\n\tisb" : : : "memory");
> >
> > isb here is redundant with the one below.
>
> Ah fair enough. I need it whether CR_I is 0 or 1 right? So I can just
> remove it from the if()
>
> >
> > > +
> > > + /* Always discard anything already fetched/prefetched */
> > > + asm volatile("isb" : : : "memory");
> > > +}
> >
> >
> > r~
>
> Thanks
> /Ilias
^ permalink raw reply [flat|nested] 25+ messages in thread
end of thread, other threads:[~2026-12-04 16:14 UTC | newest]
Thread overview: 25+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 7:41 [PATCH 1/2] arm64: relocate: convert relocate_code to C Ilias Apalodimas
2026-11-25 7:41 ` [PATCH 2/2] arm64: Add RELR relocs Ilias Apalodimas
2026-11-25 17:20 ` Quentin Schulz
2026-11-26 12:48 ` Peter Robinson
2026-11-26 15:14 ` Ilias Apalodimas
2026-11-27 18:46 ` Tom Rini
2026-12-03 22:27 ` Ilias Apalodimas
2026-12-03 22:42 ` Tom Rini
2026-12-03 22:53 ` Ilias Apalodimas
2026-12-03 23:02 ` Tom Rini
2026-12-04 6:44 ` Ilias Apalodimas
2026-12-04 15:12 ` Tom Rini
2026-12-04 15:50 ` Ilias Apalodimas
2026-12-04 16:13 ` Ilias Apalodimas
2026-11-26 16:35 ` Richard Henderson
2026-11-26 16:58 ` Ilias Apalodimas
2026-11-26 20:51 ` Ilias Apalodimas
2026-12-03 22:14 ` Richard Henderson
2026-11-25 17:07 ` [PATCH 1/2] arm64: relocate: convert relocate_code to C Quentin Schulz
2026-11-25 17:08 ` Casey Connolly
2026-11-25 17:11 ` Ilias Apalodimas
2026-11-27 18:35 ` Tom Rini
2026-11-26 16:11 ` Richard Henderson
2026-11-26 16:18 ` Ilias Apalodimas
2026-11-26 17:03 ` Ilias Apalodimas
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.