Bug#1140751: trixie-pu: package grub2/2.12-9+deb13u3
Han Gao
rabenda.cn at gmail.com
Thu Jun 25 18:42:11 BST 2026
Package: release.debian.org
Severity: normal
Tags: trixie
X-Debbugs-Cc: grub2 at packages.debian.org, 1140542 at bugs.debian.org
User: release.debian.org at packages.debian.org
Usertags: pu
[ Reason ]
When `cmodel=large` is not used, GRUB2 uses R_RISCV_CALL/R_RISCV_CALL_PLT
for jumps, which are limited to a ±2 GiB range,however, this is
insufficient for (monolithic)grub.efi.
The solution for GRUB is to use `mcmodel=large`.
On RISC-V, GCC does not allow the simultaneous use of `-fPIE`
and `-mcmodel=large`, issuing an error indicating that this
combination is unimplemented.
- cc1: sorry, unimplemented: code model ‘large’ with ‘-fPIC’
This causes `cmodel=large` to stop working if PIE is detected first.
In addition to the bug described in 1140542,
GRUB must also be prevented from generating `R_RISCV_ALIGN` relocations,
as GRUB cannot ignore them.
[ Impact ]
This issue causes a "relocation overflow" GRUB error when
booting Debian Trixie using EDK2/uboot.
Affected model:
SiFive Premier P550 (uboot efi boot grub)
Milkv Pioneer/Sophgo SG2042 EVB (EDK2 boot grub)
Sophgo SG2044 (EDK2 boot grub)
Spacmit V100 (EDK2 boot grub)
[ Tests ]
I have tested these two patches and verified them on
Milkv Pioneer/Sophgo SG2042 EVB/Sophgo SG2044/SiFive Premier P550
machines.
[ Risks ]
I think the risk is small; both of these patches are adjusting
the compilation options for riscv64.
These changes are already included in GRUB 2.14.
[ Checklist ]
[x] *all* changes are documented in the d/changelog
[x] I reviewed all changes and I approve them
[x] attach debdiff against the package in (old)stable
[x] the issue is verified as fixed in unstable
[ Changes ]
* Non-maintainer upload.
* Apply patch from upstream to fix relocation overflow crash on riscv64
in grub.efi (Closes: #1140542)
- configure-Add-mno-relax-on-riscv
from 8fef533cf69547576f522b0e563c9c6fee635ece
- configure-Defer-check-for-mcmodel-large-until-PIC-PI
form 1aa0dd0c04a7e66d127915c54d7d7f1de5974d4f
-------------- next part --------------
diff -Nru grub2-2.12/debian/changelog grub2-2.12/debian/changelog
--- grub2-2.12/debian/changelog 2026-05-01 03:02:01.000000000 +0800
+++ grub2-2.12/debian/changelog 2026-06-26 00:49:46.000000000 +0800
@@ -1,3 +1,15 @@
+grub2 (2.12-9+deb13u3) trixie; urgency=medium
+
+ * Non-maintainer upload.
+ * Apply patch from upstream to fix relocation overflow crash on riscv64
+ in grub.efi (Closes: #1140542)
+ - configure-Add-mno-relax-on-riscv
+ from 8fef533cf69547576f522b0e563c9c6fee635ece
+ - configure-Defer-check-for-mcmodel-large-until-PIC-PI
+ form 1aa0dd0c04a7e66d127915c54d7d7f1de5974d4f
+
+ -- Han Gao <rabenda.cn at gmail.com> Fri, 26 Jun 2026 00:49:46 +0800
+
grub2 (2.12-9+deb13u2) trixie; urgency=medium
* Non-maintainer upload.
diff -Nru grub2-2.12/debian/patches/series grub2-2.12/debian/patches/series
--- grub2-2.12/debian/patches/series 2026-05-01 03:02:01.000000000 +0800
+++ grub2-2.12/debian/patches/series 2026-06-26 00:48:47.000000000 +0800
@@ -72,6 +72,8 @@
upstream/acpi-Fix-out-of-bounds-access-in-grub_acpi_xsdt_find_tabl.patch
upstream/zfs-fix-LINUX_ROOT_DEVICE-when-grub-probe-fails.patch
upstream/kern-riscv-efi-init-Use-time-register-in-grub_efi_ge.patch
+upstream/configure-Add-mno-relax-on-riscv.patch
+upstream/configure-Defer-check-for-mcmodel-large-until-PIC-PI.patch
install-signed.patch
grub-install-extra-removable.patch
grub-install-removable-shim.patch
diff -Nru grub2-2.12/debian/patches/upstream/configure-Add-mno-relax-on-riscv.patch grub2-2.12/debian/patches/upstream/configure-Add-mno-relax-on-riscv.patch
--- grub2-2.12/debian/patches/upstream/configure-Add-mno-relax-on-riscv.patch 1970-01-01 08:00:00.000000000 +0800
+++ grub2-2.12/debian/patches/upstream/configure-Add-mno-relax-on-riscv.patch 2026-06-26 00:43:16.000000000 +0800
@@ -0,0 +1,54 @@
+From 8fef533cf69547576f522b0e563c9c6fee635ece Mon Sep 17 00:00:00 2001
+From: Vladimir Serbinenko <phcoder at gmail.com>
+Date: Thu, 27 Mar 2025 19:32:23 +0300
+Subject: [PATCH] configure: Add -mno-relax on riscv*
+
+Without this option compiler sometimes emits R_RISCV_ALIGN relocs.
+Unlike other relocs this one requires the linker to do NOP deletions
+and we can't ignore them. Just instruct compiler not to emit them.
+
+Signed-off-by: Vladimir Serbinenko <phcoder at gmail.com>
+Reviewed-by: Daniel Kiper <daniel.kiper at oracle.com>
+---
+ configure.ac | 25 +++++++++++++++++++++++++
+ 1 file changed, 25 insertions(+)
+
+diff --git a/configure.ac b/configure.ac
+index ad1e7bea5f12..83e3ddf90845 100644
+--- a/configure.ac
++++ b/configure.ac
+@@ -913,6 +913,31 @@ if test "x$target_cpu" = xloongarch64; then
+ TARGET_CCASFLAGS="$TARGET_CCASFLAGS -Wa,-mla-global-with-abs"
+ fi
+
++if test "x$target_cpu" = xriscv64 || test "x$target_cpu" = xriscv32; then
++ AC_CACHE_CHECK([for no-relax options], grub_cv_target_cc_mno_relax, [
++ grub_cv_target_cc_mno_relax=no
++ for cand in "-mno-relax" "-Wa,-mno-relax"; do
++ if test x"$grub_cv_target_cc_mno_relax" != xno ; then
++ break
++ fi
++ CFLAGS="$TARGET_CFLAGS $cand -Werror"
++ AC_LINK_IFELSE([AC_LANG_PROGRAM([[
++ asm (".globl start; start:");
++ void __main (void);
++ void __main (void) {}
++ int main (void);
++ ]], [[]])], [grub_cv_target_cc_mno_relax="$cand"], [])
++ done
++ ])
++
++ CFLAGS="$TARGET_CFLAGS"
++
++ if test x"$grub_cv_target_cc_mno_relax" != xno ; then
++ TARGET_CFLAGS="$TARGET_CFLAGS $grub_cv_target_cc_mno_relax"
++ TARGET_CCASFLAGS="$TARGET_CCASFLAGS $grub_cv_target_cc_mno_relax"
++ fi
++fi
++
+ # GRUB doesn't use float or doubles at all. Yet some toolchains may decide
+ # that floats are a good fit to run instead of what's written in the code.
+ # Given that floating point unit is disabled (if present to begin with)
+--
+2.53.0
+
diff -Nru grub2-2.12/debian/patches/upstream/configure-Defer-check-for-mcmodel-large-until-PIC-PI.patch grub2-2.12/debian/patches/upstream/configure-Defer-check-for-mcmodel-large-until-PIC-PI.patch
--- grub2-2.12/debian/patches/upstream/configure-Defer-check-for-mcmodel-large-until-PIC-PI.patch 1970-01-01 08:00:00.000000000 +0800
+++ grub2-2.12/debian/patches/upstream/configure-Defer-check-for-mcmodel-large-until-PIC-PI.patch 2026-06-26 00:46:50.000000000 +0800
@@ -0,0 +1,81 @@
+From 1aa0dd0c04a7e66d127915c54d7d7f1de5974d4f Mon Sep 17 00:00:00 2001
+From: Yao Zi <me at ziyao.cc>
+Date: Wed, 31 Dec 2025 09:05:53 +0000
+Subject: [PATCH] configure: Defer check for -mcmodel=large until PIC/PIE
+ checks are done
+
+On RISC-V, large code model is only compatible with position-depedent
+code. However, the configure script checks availability of -mcmodel=large
+before determining whether PIC/PIE is enabled, and disable them.
+
+This is problematic with toolchains that enable PIE by default, where
+check for -mcmodel=large will always fail with,
+
+ cc1: sorry, unimplemented: code model 'large' with '-fPIC'
+
+and -mcmodel=medany will be silently used instead, causing relocation
+failures at runtime with some memory layouts since -mcmodel=medany
+requires all data and code to stay within a contiguous 4 GiB range.
+
+Let's defer the check for -mcmodel=large until PIC/PIE is ensured disabled.
+
+Fixes: f1957dc8a334 (RISC-V: Add to build system)
+
+Reported-by: Han Gao <gaohan at iscas.ac.cn>
+Signed-off-by: Yao Zi <me at ziyao.cc>
+Reviewed-by: Daniel Kiper <daniel.kiper at oracle.com>
+---
+ configure.ac | 30 +++++++++++++++---------------
+ 1 file changed, 15 insertions(+), 15 deletions(-)
+
+diff --git a/configure.ac b/configure.ac
+index a282bf7bfbc7..a833c7612734 100644
+--- a/configure.ac
++++ b/configure.ac
+@@ -1543,21 +1543,6 @@ AC_SUBST(TARGET_LDFLAGS_OLDMAGIC)
+
+ LDFLAGS="$TARGET_LDFLAGS"
+
+-if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 || test "$target_cpu" = riscv64 ; then
+- # Use large model to support 4G memory
+- AC_CACHE_CHECK([whether option -mcmodel=large works], grub_cv_cc_mcmodel, [
+- CFLAGS="$TARGET_CFLAGS -mcmodel=large"
+- AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
+- [grub_cv_cc_mcmodel=yes],
+- [grub_cv_cc_mcmodel=no])
+- ])
+- if test "x$grub_cv_cc_mcmodel" = xyes; then
+- TARGET_CFLAGS="$TARGET_CFLAGS -mcmodel=large"
+- elif test "$target_cpu" = sparc64 || test "$target_cpu" = riscv64; then
+- TARGET_CFLAGS="$TARGET_CFLAGS -mcmodel=medany"
+- fi
+-fi
+-
+ if test "$target_cpu"-"$platform" = x86_64-efi; then
+ # EFI writes to stack below %rsp, we must not use the red zone
+ AC_CACHE_CHECK([whether option -mno-red-zone works], grub_cv_cc_no_red_zone, [
+@@ -1666,6 +1651,21 @@ fi]
+
+ CFLAGS="$TARGET_CFLAGS"
+
++if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 || test "$target_cpu" = riscv64 ; then
++ # Use large model to support 4G memory
++ AC_CACHE_CHECK([whether option -mcmodel=large works], grub_cv_cc_mcmodel, [
++ CFLAGS="$TARGET_CFLAGS -mcmodel=large"
++ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]], [[]])],
++ [grub_cv_cc_mcmodel=yes],
++ [grub_cv_cc_mcmodel=no])
++ ])
++ if test "x$grub_cv_cc_mcmodel" = xyes; then
++ TARGET_CFLAGS="$TARGET_CFLAGS -mcmodel=large"
++ elif test "$target_cpu" = sparc64 || test "$target_cpu" = riscv64; then
++ TARGET_CFLAGS="$TARGET_CFLAGS -mcmodel=medany"
++ fi
++fi
++
+ # Stack smashing protector.
+ grub_CHECK_STACK_PROTECTOR
+ AC_ARG_ENABLE([stack-protector],
+--
+2.53.0
+
More information about the Pkg-grub-devel
mailing list