mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Ahmad Fatoum <a.fatoum@barebox.org>
Cc: barebox@lists.infradead.org
Subject: Re: [PATCH 2/2] ARM: cpu: common: define generic relocate_image
Date: Tue, 6 Jan 2026 10:16:40 +0100	[thread overview]
Message-ID: <aVzS-J58j8FsFpC3@pengutronix.de> (raw)
In-Reply-To: <20260106081659.3491945-2-a.fatoum@barebox.org>

On Tue, Jan 06, 2026 at 09:16:57AM +0100, Ahmad Fatoum wrote:
> For use by the ELF loader in PBL to relocate barebox proper, export a
> new relocate_image capable of relocating barebox and implement
> relocate_to_current_adr() in terms of it.
> 
> When doing ELF loading, the PT_DYNAMIC segment will contain the info
> needed to derive the arguments for relocate_image():
> 
>   - dstart: DT_REL  /DT_RELA
>   - dend:   DT_RELSZ/DT_RELASZ
>   - dynsym: DT_SYMTAB
> 
> dynend is optional and could in theory be generated by looking at
> DT_HASH or DT_GNU_HASH, but it's not required. Best guess is that the
> zeroing is there just to signify that we don't need this data anymore.
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
> ---
>  arch/arm/cpu/common.c        | 34 +++++++++++++++-------------------
>  arch/arm/include/asm/reloc.h |  3 +++
>  2 files changed, 18 insertions(+), 19 deletions(-)
> 
> diff --git a/arch/arm/cpu/common.c b/arch/arm/cpu/common.c
> index 5b65b0745b98..f4f3f8f6325b 100644
> --- a/arch/arm/cpu/common.c
> +++ b/arch/arm/cpu/common.c
> @@ -60,22 +60,10 @@ void pbl_barebox_break(void)
>  /*
>   * relocate binary to the currently running address
>   */
> -void __prereloc relocate_to_current_adr(void)
> +void __prereloc relocate_image(unsigned long offset,
> +			       void *dstart, void *dend,
> +			       long *dynsym, long *dynend)

As relocate_image() now contains two entirely separate implementations
for ARM32 and ARM64, should we move it to arch/arm/lib[32|64]/ ?

>  {
> -	unsigned long offset;
> -	unsigned long __maybe_unused *dynsym, *dynend;
> -	void *dstart, *dend;
> -
> -	/* Get offset between linked address and runtime address */
> -	offset = get_runtime_offset();
> -
> -	/*
> -	 * We have yet to relocate, so using runtime_address
> -	 * to compute the relocated address
> -	 */
> -	dstart = runtime_address(__rel_dyn_start);
> -	dend = runtime_address(__rel_dyn_end);
> -
>  #if defined(CONFIG_CPU_64)
>  	while (dstart < dend) {
>  		struct elf64_rela *rel = dstart;
> @@ -105,8 +93,6 @@ void __prereloc relocate_to_current_adr(void)
>  		dstart += sizeof(*rel);
>  	}
>  #elif defined(CONFIG_CPU_32)
> -	dynsym = runtime_address(__dynsym_start);
> -	dynend = runtime_address(__dynsym_end);
>  
>  	while (dstart < dend) {
>  		struct elf32_rel *rel = dstart;
> @@ -141,10 +127,21 @@ void __prereloc relocate_to_current_adr(void)
>  		dstart += sizeof(*rel);
>  	}
>  
> -	__memset(dynsym, 0, (unsigned long)dynend - (unsigned long)dynsym);
> +	/* Optional: not required for correctness */
> +	if (dynend)
> +		__memset(dynsym, 0, (unsigned long)dynend - (unsigned long)dynsym);

The purpose of this memset is that we can call relocate_to_current_adr()
multiple times on the same binary. This happens when relocate_to_current_adr()
is called in the board code and then again in uncompress.c. I think in
this particular case we could just early return above when offset == 0,
but I think there are (were?) some cases where the same binary is moved multiple
times.

Sascha

-- 
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 |



  reply	other threads:[~2026-01-06  9:17 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-06  8:16 [PATCH 1/2] ARM: cpu: common: skip R_ARM_NONE relocations Ahmad Fatoum
2026-01-06  8:16 ` [PATCH 2/2] ARM: cpu: common: define generic relocate_image Ahmad Fatoum
2026-01-06  9:16   ` Sascha Hauer [this message]
2026-01-06 11:43   ` Ahmad Fatoum
2026-01-06 13:18 ` [PATCH 1/2] ARM: cpu: common: skip R_ARM_NONE relocations Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aVzS-J58j8FsFpC3@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=a.fatoum@barebox.org \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox