arm: Prepare linker scripts for memory permissions

Upcoming patches are switching the memory mappings to RW, RO, RX
after the U-Boot binary and its data are relocated. Add
annotations in the linker scripts to and mark text, data, rodata
sections and align them to a page boundary.

It's worth noting that .efi_runtime memory permissions are left
untouched for now. There's two problems with EFI currently.

The first problem is that we bundle data, rodata and text in a single
.efi_runtime section which also must be close to .text for now.
As a result we also dont change the permissions for anything contained
in CPUDIR/start.o. In order to fix that we have to decoule .text_rest,
.text and .efi_runtime and have the runtime services on their own
section with proper memory permission annotations (efi_rodata etc).

The efi runtime regions (.efi_runtime_rel) can be relocated by the OS when
the latter is calling SetVirtualAddressMap. Which means we have to
configure those pages as RX for U-Boot but convert them to RWX just before
ExitBootServices. It also needs extra code in efi_tuntime relocation
code since R_AARCH64_NONE are emitted as well if we page align the
section.

Due to the above ignore EFI for now and fix it later once we have the
rest in place.

Acked-by: Jerome Forissier <jerome.forissier@linaro.org>
Tested-by: Neil Armstrong <neil.armstrong@linaro.org> # on AML-S905X-CC
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
This commit is contained in:
Ilias Apalodimas 2025-02-20 15:54:40 +02:00
parent 62b2d933cf
commit 1c7d0c411c
2 changed files with 43 additions and 20 deletions

View file

@ -36,9 +36,18 @@ SECTIONS
__efi_runtime_stop = .; __efi_runtime_stop = .;
} }
#ifdef CONFIG_MMU_PGPROT
.text_rest ALIGN(CONSTANT(COMMONPAGESIZE)) :
#else
.text_rest : .text_rest :
#endif
{ {
__text_start = .;
*(.text*) *(.text*)
#ifdef CONFIG_MMU_PGPROT
. = ALIGN(CONSTANT(COMMONPAGESIZE));
#endif
__text_end = .;
} }
#ifdef CONFIG_ARMV8_PSCI #ifdef CONFIG_ARMV8_PSCI
@ -97,24 +106,6 @@ SECTIONS
LONG(0x1d1071c); /* Must output something to reset LMA */ LONG(0x1d1071c); /* Must output something to reset LMA */
} }
#endif #endif
. = ALIGN(8);
.rodata : { *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.rodata*))) }
. = ALIGN(8);
.data : {
*(.data*)
}
. = ALIGN(8);
. = .;
. = ALIGN(8);
__u_boot_list : {
KEEP(*(SORT(__u_boot_list*)));
}
.efi_runtime_rel : { .efi_runtime_rel : {
__efi_runtime_rel_start = .; __efi_runtime_rel_start = .;
*(.rel*.efi_runtime) *(.rel*.efi_runtime)
@ -122,10 +113,36 @@ SECTIONS
__efi_runtime_rel_stop = .; __efi_runtime_rel_stop = .;
} }
#ifdef CONFIG_MMU_PGPROT
.rodata ALIGN(CONSTANT(COMMONPAGESIZE)): {
#else
.rodata ALIGN(8) : {
#endif
__start_rodata = .;
*(SORT_BY_ALIGNMENT(SORT_BY_NAME(.rodata*)))
}
__u_boot_list ALIGN(8) : {
KEEP(*(SORT(__u_boot_list*)));
#ifdef CONFIG_MMU_PGPROT
. = ALIGN(CONSTANT(COMMONPAGESIZE));
#endif
__end_rodata = .;
}
#ifdef CONFIG_MMU_PGPROT
.data ALIGN(CONSTANT(COMMONPAGESIZE)) : {
#else
.data ALIGN(8) : {
#endif
__start_data = .;
*(.data*)
}
. = ALIGN(8); . = ALIGN(8);
__image_copy_end = .; __image_copy_end = .;
.rela.dyn : { .rela.dyn ALIGN(8) : {
__rel_dyn_start = .; __rel_dyn_start = .;
*(.rela*) *(.rela*)
__rel_dyn_end = .; __rel_dyn_end = .;
@ -136,11 +153,15 @@ SECTIONS
/* /*
* arch/arm/lib/crt0_64.S assumes __bss_start - __bss_end % 8 == 0 * arch/arm/lib/crt0_64.S assumes __bss_start - __bss_end % 8 == 0
*/ */
.bss ALIGN(8) : { .bss ADDR(.rela.dyn) (OVERLAY) : {
__bss_start = .; __bss_start = .;
*(.bss*) *(.bss*)
. = ALIGN(8); . = ALIGN(8);
__bss_end = .; __bss_end = .;
#ifdef CONFIG_MMU_PGPROT
. = ALIGN(CONSTANT(COMMONPAGESIZE));
#endif
__end_data = .;
} }
/DISCARD/ : { *(.dynsym) } /DISCARD/ : { *(.dynsym) }

View file

@ -23,6 +23,7 @@ extern char __kprobes_text_start[], __kprobes_text_end[];
extern char __entry_text_start[], __entry_text_end[]; extern char __entry_text_start[], __entry_text_end[];
extern char __initdata_begin[], __initdata_end[]; extern char __initdata_begin[], __initdata_end[];
extern char __start_rodata[], __end_rodata[]; extern char __start_rodata[], __end_rodata[];
extern char __start_data[], __end_data[];
extern char __efi_helloworld_begin[]; extern char __efi_helloworld_begin[];
extern char __efi_helloworld_end[]; extern char __efi_helloworld_end[];
extern char __efi_var_file_begin[]; extern char __efi_var_file_begin[];
@ -63,6 +64,7 @@ static inline int arch_is_kernel_data(unsigned long addr)
/* Start of U-Boot text region */ /* Start of U-Boot text region */
extern char __text_start[]; extern char __text_start[];
extern char __text_end[];
/* This marks the text region which must be relocated */ /* This marks the text region which must be relocated */
extern char __image_copy_start[], __image_copy_end[]; extern char __image_copy_start[], __image_copy_end[];