U-boot Archive mirror
 help / color / mirror / Atom feed
* [PATCH] arm64: Fix relocation of env_addr if POSITION_INDEPENDENT=y
@ 2021-06-15  6:33 Kunihiko Hayashi
  2021-06-27  1:42 ` Marek Vasut
  2021-06-29 12:38 ` Tom Rini
  0 siblings, 2 replies; 3+ messages in thread
From: Kunihiko Hayashi @ 2021-06-15  6:33 UTC (permalink / raw
  To: u-boot
  Cc: Stephen Warren, Michal Simek, Joe Hershberger, Tom Rini,
	Kunihiko Hayashi, Marek Vasut

If both POSITION_INDEPENDENT and SYS_RELOC_GD_ENV_ADDR are enabled,
wherever original env is placed anywhere, it should be relocated to
the right address.

Relocation offset gd->reloc_off is calculated with SYS_TEXT_BASE in
setup_reloc() and env address gd->env_addr is relocated by the offset in
initr_reloc_global_data().

gd->env_addr
  = (orig env) + gd->reloc_off
  = (orig env) + (gd->relocaddr - SYS_TEXT_BASE)

However, SYS_TEXT_BASE isn't always runtime base address when
POSITION_INDEPENDENT is enabled. So the relocated env_addr might point to
wrong address. For example, if SYS_TEXT_BASE is zero, gd->env_addr is
out of memory location and memory exception will occur.

There is a difference between linked address such as SYS_TEXT_BASE and
runtime base address. In _main, the difference is calculated as
"run-vs-link" offset. The env_addr should also be added to the offset
to fix the address.

gd->env_addr
  = (orig env) + ("run-vs-link" offset)   + gd->reloc_off
  = (orig env) + (SYS_TEXT_BASE - _start) + (gd->relocaddr - SYS_TEXT_BASE)
  = (orig env) + (gd->relocaddr - _start)

Cc: Marek Vasut <marex@denx.de>
Signed-off-by: Kunihiko Hayashi <hayashi.kunihiko@socionext.com>
---
 arch/arm/lib/crt0_64.S | 5 +++++
 lib/asm-offsets.c      | 2 ++
 2 files changed, 7 insertions(+)

This patch is based on the previous topic:
"env: Leave invalid env for nowhere location"
https://patchwork.ozlabs.org/project/uboot/patch/1620828554-24013-1-git-send-email-hayashi.kunihiko@socionext.com/

diff --git a/arch/arm/lib/crt0_64.S b/arch/arm/lib/crt0_64.S
index 9d2319c..680e674 100644
--- a/arch/arm/lib/crt0_64.S
+++ b/arch/arm/lib/crt0_64.S
@@ -112,6 +112,11 @@ ENTRY(_main)
 	ldr	x9, _TEXT_BASE		/* x9 <- Linked value of _start */
 	sub	x9, x9, x0		/* x9 <- Run-vs-link offset */
 	add	lr, lr, x9
+#if defined(CONFIG_SYS_RELOC_GD_ENV_ADDR)
+	ldr	x0, [x18, #GD_ENV_ADDR]	/* x0 <- gd->env_addr */
+	add	x0, x0, x9
+	str	x0, [x18, #GD_ENV_ADDR]
+#endif
 #endif
 	/* Add in link-vs-relocation offset */
 	ldr	x9, [x18, #GD_RELOC_OFF]	/* x9 <- gd->reloc_off */
diff --git a/lib/asm-offsets.c b/lib/asm-offsets.c
index ee592cf..c691066 100644
--- a/lib/asm-offsets.c
+++ b/lib/asm-offsets.c
@@ -41,5 +41,7 @@ int main(void)
 
 	DEFINE(GD_NEW_GD, offsetof(struct global_data, new_gd));
 
+	DEFINE(GD_ENV_ADDR, offsetof(struct global_data, env_addr));
+
 	return 0;
 }
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] arm64: Fix relocation of env_addr if POSITION_INDEPENDENT=y
  2021-06-15  6:33 [PATCH] arm64: Fix relocation of env_addr if POSITION_INDEPENDENT=y Kunihiko Hayashi
@ 2021-06-27  1:42 ` Marek Vasut
  2021-06-29 12:38 ` Tom Rini
  1 sibling, 0 replies; 3+ messages in thread
From: Marek Vasut @ 2021-06-27  1:42 UTC (permalink / raw
  To: Kunihiko Hayashi, u-boot
  Cc: Stephen Warren, Michal Simek, Joe Hershberger, Tom Rini

On 6/15/21 8:33 AM, Kunihiko Hayashi wrote:
> If both POSITION_INDEPENDENT and SYS_RELOC_GD_ENV_ADDR are enabled,
> wherever original env is placed anywhere, it should be relocated to
> the right address.
> 
> Relocation offset gd->reloc_off is calculated with SYS_TEXT_BASE in
> setup_reloc() and env address gd->env_addr is relocated by the offset in
> initr_reloc_global_data().
> 
> gd->env_addr
>    = (orig env) + gd->reloc_off
>    = (orig env) + (gd->relocaddr - SYS_TEXT_BASE)
> 
> However, SYS_TEXT_BASE isn't always runtime base address when
> POSITION_INDEPENDENT is enabled. So the relocated env_addr might point to
> wrong address. For example, if SYS_TEXT_BASE is zero, gd->env_addr is
> out of memory location and memory exception will occur.
> 
> There is a difference between linked address such as SYS_TEXT_BASE and
> runtime base address. In _main, the difference is calculated as
> "run-vs-link" offset. The env_addr should also be added to the offset
> to fix the address.
> 
> gd->env_addr
>    = (orig env) + ("run-vs-link" offset)   + gd->reloc_off
>    = (orig env) + (SYS_TEXT_BASE - _start) + (gd->relocaddr - SYS_TEXT_BASE)
>    = (orig env) + (gd->relocaddr - _start)
> 
> Cc: Marek Vasut <marex@denx.de>
> Signed-off-by: Kunihiko Hayashi <hayashi.kunihiko@socionext.com>

Thank you for debugging and fixing this properly.

Acked-by: Marek Vasut <marex@denx.de>
Tested-by: Marek Vasut <marex@denx.de>

I did manage to reproduce it on RCar3, and this patch fixes the crash on 
boot indeed.

Tom, it would be good to include it in this release too.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] arm64: Fix relocation of env_addr if POSITION_INDEPENDENT=y
  2021-06-15  6:33 [PATCH] arm64: Fix relocation of env_addr if POSITION_INDEPENDENT=y Kunihiko Hayashi
  2021-06-27  1:42 ` Marek Vasut
@ 2021-06-29 12:38 ` Tom Rini
  1 sibling, 0 replies; 3+ messages in thread
From: Tom Rini @ 2021-06-29 12:38 UTC (permalink / raw
  To: Kunihiko Hayashi
  Cc: u-boot, Stephen Warren, Michal Simek, Joe Hershberger,
	Marek Vasut

[-- Attachment #1: Type: text/plain, Size: 1483 bytes --]

On Tue, Jun 15, 2021 at 03:33:02PM +0900, Kunihiko Hayashi wrote:

> If both POSITION_INDEPENDENT and SYS_RELOC_GD_ENV_ADDR are enabled,
> wherever original env is placed anywhere, it should be relocated to
> the right address.
> 
> Relocation offset gd->reloc_off is calculated with SYS_TEXT_BASE in
> setup_reloc() and env address gd->env_addr is relocated by the offset in
> initr_reloc_global_data().
> 
> gd->env_addr
>   = (orig env) + gd->reloc_off
>   = (orig env) + (gd->relocaddr - SYS_TEXT_BASE)
> 
> However, SYS_TEXT_BASE isn't always runtime base address when
> POSITION_INDEPENDENT is enabled. So the relocated env_addr might point to
> wrong address. For example, if SYS_TEXT_BASE is zero, gd->env_addr is
> out of memory location and memory exception will occur.
> 
> There is a difference between linked address such as SYS_TEXT_BASE and
> runtime base address. In _main, the difference is calculated as
> "run-vs-link" offset. The env_addr should also be added to the offset
> to fix the address.
> 
> gd->env_addr
>   = (orig env) + ("run-vs-link" offset)   + gd->reloc_off
>   = (orig env) + (SYS_TEXT_BASE - _start) + (gd->relocaddr - SYS_TEXT_BASE)
>   = (orig env) + (gd->relocaddr - _start)
> 
> Cc: Marek Vasut <marex@denx.de>
> Signed-off-by: Kunihiko Hayashi <hayashi.kunihiko@socionext.com>
> Acked-by: Marek Vasut <marex@denx.de>
> Tested-by: Marek Vasut <marex@denx.de>

Applied to u-boot/master, thanks!

-- 
Tom

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 659 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-06-29 12:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-06-15  6:33 [PATCH] arm64: Fix relocation of env_addr if POSITION_INDEPENDENT=y Kunihiko Hayashi
2021-06-27  1:42 ` Marek Vasut
2021-06-29 12:38 ` Tom Rini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).