From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Cc: mfe@pengutronix.de, mtr@pengutronix.de,
Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [RFC PATCH 11/17] RISC-V: allow relocating the PBL more than once
Date: Tue, 27 Oct 2026 08:24:44 +0100 [thread overview]
Message-ID: <20261027074014.3280396-12-a.fatoum@barebox.org> (raw)
In-Reply-To: <20261027074014.3280396-1-a.fatoum@barebox.org>
relocate_image() doesn't update the relocation entries, so a second
relocation applies an offset relative to the first runtime address to
the link-time addresses and writes wrong values to wrong places.
This happens when the PBL is first relocated in place and
barebox_pbl_start() then copies it into the memory it was passed. ARM64
has handled this since fab26e665163 ("ARM: aarch64: Fixup relocation
table for the second relocation"), so let's rebase each entry after
applying it the same way.
Like on ARM, relocate_to_adr() now also copies the BSS, as it's already
in use and setup_c() won't clear it again at the destination.
Tested on QEMU virt with an image whose stage memory starts above the
address OpenSBI starts it at, which used to fault right after relocation.
Assisted-by: Claude:opus-5.5
Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
arch/riscv/lib/reloc.c | 6 +++++-
arch/riscv/lib/setupc.S | 2 +-
2 files changed, 6 insertions(+), 2 deletions(-)
diff --git a/arch/riscv/lib/reloc.c b/arch/riscv/lib/reloc.c
index 99dde7bfffe2..9eede11b3528 100644
--- a/arch/riscv/lib/reloc.c
+++ b/arch/riscv/lib/reloc.c
@@ -46,7 +46,7 @@ static void relocate_image(unsigned long offset,
switch (RISC_R_TYPE(rela->r_info)) {
case R_RISCV_NONE:
- break;
+ continue;
case R_RISCV_RELATIVE:
*fixup = rela->r_addend + offset;
break;
@@ -63,6 +63,10 @@ static void relocate_image(unsigned long offset,
putc_ll('\n');
__hang();
}
+
+ /* Rebase the entry, so the image can be relocated again */
+ rela->r_addend += offset;
+ rela->r_offset += offset;
}
}
diff --git a/arch/riscv/lib/setupc.S b/arch/riscv/lib/setupc.S
index 68924f2f94df..ba9193288a81 100644
--- a/arch/riscv/lib/setupc.S
+++ b/arch/riscv/lib/setupc.S
@@ -53,7 +53,7 @@ ENTRY(relocate_to_adr_full)
ENDPROC(relocate_to_adr_full)
ENTRY(relocate_to_adr)
- lla a3, __bss_start /* skip piggy data */
+ lla a3, __bss_stop /* skip piggy data */
1:
addi sp, sp, -SZREG * 2
lla a1, _text /* a1: source address */
--
2.47.3
next prev parent reply other threads:[~2026-10-27 7:41 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-27 7:24 [RFC PATCH 00/17] Switch RISC-V to declarative entry points Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 01/17] asm-generic: add registers.h with a pc_inside() helper Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 02/17] RISC-V: add call_with_stack() helper Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 03/17] RISC-V: implement relocate_to_adr_full() Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 04/17] PBL: add SoC type declaration and handoff support Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 05/17] PBL: introduce board info based entry point framework Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 06/17] PBL: describe the console in the board info and report the stage Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 07/17] RISC-V: make barebox_riscv_entry() non-naked Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 08/17] RISC-V: add PBL heads for the board info stage dispatch Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 09/17] RISC-V: board-dt-2nd: migrate to board info entry point Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 10/17] RISC-V: hifive: " Ahmad Fatoum
2026-10-27 7:24 ` Ahmad Fatoum [this message]
2026-10-27 7:24 ` [RFC PATCH 12/17] RISC-V: nmon: preserve s1-s3 for the caller Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 13/17] RISC-V: beaglev: migrate to board info entry point Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 14/17] RISC-V: allwinner-d1: " Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 15/17] RISC-V: erizo: " Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 16/17] RISC-V: litex-linux: " Ahmad Fatoum
2026-10-27 7:24 ` [RFC PATCH 17/17] Documentation: porting: describe the " Ahmad Fatoum
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=20261027074014.3280396-12-a.fatoum@barebox.org \
--to=a.fatoum@barebox.org \
--cc=barebox@lists.infradead.org \
--cc=mfe@pengutronix.de \
--cc=mtr@pengutronix.de \
/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