]> git.ipfire.org Git - thirdparty/u-boot.git/commit
arm64: Fix relocation of env_addr if POSITION_INDEPENDENT=y
authorKunihiko Hayashi <hayashi.kunihiko@socionext.com>
Tue, 15 Jun 2021 06:33:02 +0000 (15:33 +0900)
committerMichal Simek <michal.simek@xilinx.com>
Wed, 30 Jun 2021 08:20:24 +0000 (10:20 +0200)
commit91961465577714e1eac21d13b6b2300244adbced
tree136cd0ad2badebaea331f4cb1bc2fb036ce99c4a
parent4e3336dcb575a18b4702f30fb9a05d40d1f7f64d
arm64: Fix relocation of env_addr if POSITION_INDEPENDENT=y

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>
Link: https://lists.denx.de/pipermail/u-boot/2021-June/452135.html
State: upstream (534f0fbd65203871c2b054096422a5d0f3346cb1)
arch/arm/lib/crt0_64.S
lib/asm-offsets.c