[PATCH master 3/3] RISC-V: add compiler barriers around unrelocated accesses

Ahmad Fatoum a.fatoum at pengutronix.de
Thu Oct 13 03:36:26 PDT 2022


On 13.10.22 11:03, Ahmad Fatoum wrote:
> We observed on ARM miscompilation because get_runtime_offset() was
> cached before relocation, while address computation of symbol happened
> after, effectively adding the base address twice to the symbol offset.
> 
> New get_unrelocated() hides origin of the symbol going into the address
> calculation and thereby thwarts this optimization. Employ it in RISC-V
> code as well to avoid such issues as experienced on ARM.
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
> ---
>  arch/riscv/boot/uncompress.c      | 4 ++--
>  arch/riscv/include/asm/sections.h | 2 +-
>  arch/riscv/lib/reloc.c            | 6 +++---
>  3 files changed, 6 insertions(+), 6 deletions(-)
> 
> diff --git a/arch/riscv/boot/uncompress.c b/arch/riscv/boot/uncompress.c
> index ee24f81e0159..eab51c8d52bc 100644
> --- a/arch/riscv/boot/uncompress.c
> +++ b/arch/riscv/boot/uncompress.c
> @@ -36,8 +36,8 @@ void __noreturn barebox_pbl_start(unsigned long membase, unsigned long memsize,
>  	irq_init_vector(riscv_mode());
>  
>  	/* piggy data is not relocated, so determine the bounds now */
> -	pg_start = input_data + get_runtime_offset();
> -	pg_end = input_data_end + get_runtime_offset();
> +	pg_start = get_unrelocated(input_data);
> +	pg_end = get_unrelocated(input_data_end);
>  	pg_len = pg_end - pg_start;
>  	uncompressed_len = input_data_len();
>  
> diff --git a/arch/riscv/include/asm/sections.h b/arch/riscv/include/asm/sections.h
> index 6673648bcd58..b90f4d6d2ad5 100644
> --- a/arch/riscv/include/asm/sections.h
> +++ b/arch/riscv/include/asm/sections.h
> @@ -19,7 +19,7 @@ unsigned long get_runtime_offset(void);
>  
>  static inline unsigned int input_data_len(void)
>  {
> -	return get_unaligned((const u32 *)(input_data_end + get_runtime_offset() - 4));
> +	return get_unaligned((const u32 *)get_unrelocated(input_data_end) - 1);
>  }
>  
>  #endif
> diff --git a/arch/riscv/lib/reloc.c b/arch/riscv/lib/reloc.c
> index 13118a9ac54f..1dddf627d0b9 100644
> --- a/arch/riscv/lib/reloc.c
> +++ b/arch/riscv/lib/reloc.c
> @@ -42,9 +42,9 @@ void relocate_to_current_adr(void)
>  	if (!offset)
>  		return;
>  
> -	dstart = __rel_dyn_start + offset;
> -	dend = __rel_dyn_end + offset;
> -	dynsym = (void *)__dynsym_start + offset;
> +	dstart = get_unrelocated(__rel_dyn_start);
> +	dend = get_unrelocated(__rel_dyn_end);
> +	dynsym = get_unrelocated(__dynsym_start) + offset;

Ouch, + offset shouldn't be here. Will fix for v2.

>  
>  	for (rela = dstart; (void *)rela < dend; rela++) {
>  		unsigned long *fixup;


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



More information about the barebox mailing list