Skip to contentSkip to conversation

Use medany instead of large model for RISCV

PATCH In review Trevor Gamblin 3 participants 5 messages Mar 20 – Mar 21

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Use medany instead of large model for RISCV

From: Jason Montleon <[email protected]>

Upstream-Status: Submitted [https://savannah.gnu.org/bugs/?65909]

---
We have recently added this to the openembedded-core layer to help with RISC-V builds on certain platforms (e.g. the VisionFive 2). I have left the patch as it is there, including the link to the original bug submission and the author's attribution. Jason, if you would like to change the attribution or anything about the patch, let me know and I can re-submit.

 configure.ac | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
configure.ac +4 -2
index d19fb625d..2a070c577 100644
@@ -1675,7 +1675,7 @@ fi]
 
 CFLAGS="$TARGET_CFLAGS"
 
-if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 || test "$target_cpu" = riscv64 ; then
+if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 ; 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"
@@ -1685,9 +1685,11 @@ if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 || test "$target_
   ])
   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
+  elif test "$target_cpu" = sparc64; then
     TARGET_CFLAGS="$TARGET_CFLAGS -mcmodel=medany"
   fi
+elif test "$target_cpu" = riscv64 ; then
+    TARGET_CFLAGS="$TARGET_CFLAGS -mcmodel=medany"
 fi
 
 # Stack smashing protector.
Signature · 7 lines
2.53.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#1 lore raw
Quoted 4 lines · Trevor Gamblin

From: Jason Montleon <[email protected]>

Upstream-Status: Submitted [https://savannah.gnu.org/bugs/?65909]

Quoted 8 lines

---
We have recently added this to the openembedded-core layer to help with RISC-V builds on certain platforms (e.g. the VisionFive 2). I have left the patch as it is there, including the link to the original bug submission and the author's attribution. Jason, if you would like to change the attribution or anything about the patch, let me know and I can re-submit.

Hi Trevor,
Fedora is still using this patch and it is working for us.
https://src.fedoraproject.org/rpms/grub2/blob/rawhide/f/0283-Use-medany-instead-of-large-model-for-RISCV.patch

I did want to share that we revisited building with large model and managed to get it to build. This resulted in a smaller binary with far fewer relocations. That work is at http://fedora.riscv.rocks:3000/rpms/grub2/commits/branch/main-riscv64-large-code

I am not sure if one approach would be preferred over the other.

Quoted 30 lines
 configure.ac | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
configure.ac +4 -3
index d19fb625d..2a070c577 100644
@@ -1675,7 +1675,7 @@ fi]
 
 CFLAGS="$TARGET_CFLAGS"
 
-if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 || test "$target_cpu" = riscv64 ; then
+if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 ; 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"
@@ -1685,9 +1685,11 @@ if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 || test "$target_
   ])
   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
+  elif test "$target_cpu" = sparc64; then
     TARGET_CFLAGS="$TARGET_CFLAGS -mcmodel=medany"
   fi
+elif test "$target_cpu" = riscv64 ; then
+    TARGET_CFLAGS="$TARGET_CFLAGS -mcmodel=medany"
 fi
 
 # Stack smashing protector.
--
Quoted 3 lines

2.53.0

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#2 lore raw

We prefer to use large model. Did you try my patch with using ET_DYN?
https://lists.libreplanet.org/archive/html/grub-devel/2025-04/msg00093.html

Regards
Vladimir 'phcoder' Serbinenko

Quoted 75 lines · Jason Montleon via Grub-devel
Quoted 8 lines · Trevor Gamblin

From: Jason Montleon <[email protected]>

Upstream-Status: Submitted [https://savannah.gnu.org/bugs/?65909]

---
We have recently added this to the openembedded-core layer to help with

RISC-V

Quoted 1 line

builds on certain platforms (e.g. the VisionFive 2). I have left the

patch as it

Quoted 1 line

is there, including the link to the original bug submission and the

author's

Quoted 1 line

attribution. Jason, if you would like to change the attribution or

anything

Quoted 1 line

about the patch, let me know and I can re-submit.

Hi Trevor,
Fedora is still using this patch and it is working for us.

https://src.fedoraproject.org/rpms/grub2/blob/rawhide/f/0283-Use-medany-instead-of-large-model-for-RISCV.patch

I did want to share that we revisited building with large model and managed to get it to build. This resulted in a smaller binary with far fewer relocations.
That work is at
http://fedora.riscv.rocks:3000/rpms/grub2/commits/branch/main-riscv64-large-code

I am not sure if one approach would be preferred over the other.

configure.ac fi] · line 1678

Quoted 12 lines
 configure.ac | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
configure.ac +0 -1
index d19fb625d..2a070c577 100644
@@ -1675,7 +1675,7 @@ fi]
 
 CFLAGS="$TARGET_CFLAGS"
 
-if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 || test

"$target_cpu" = riscv64 ; then

configure.ac fi]

Quoted 3 lines
configure.ac +1 -0
+if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 ; then
   # Use large model to support 4G memory
   AC_CACHE_CHECK([whether option -mcmodel=large works],

grub_cv_cc_mcmodel, [

configure.ac if test "$target_cpu" = x86_64 || test

Quoted 2 lines
configure.ac +0 -0
     CFLAGS="$TARGET_CFLAGS -mcmodel=large"
@@ -1685,9 +1685,11 @@ if test "$target_cpu" = x86_64 || test

"$target_cpu" = sparc64 || test "$target_

configure.ac if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 || test "$target_

Quoted 4 lines
configure.ac +0 -1
   ])
   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

Quoted 9 lines
configure.ac +3 -1
+  elif test "$target_cpu" = sparc64; then
     TARGET_CFLAGS="$TARGET_CFLAGS -mcmodel=medany"
   fi
+elif test "$target_cpu" = riscv64 ; then
+    TARGET_CFLAGS="$TARGET_CFLAGS -mcmodel=medany"
 fi
 
 # Stack smashing protector.
--
Quoted 3 lines

2.53.0

Quoted 5 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#3 lore raw
In reply to Trevor Gamblin
Quoted 4 lines · Trevor Gamblin

From: Jason Montleon <[email protected]>

Upstream-Status: Submitted [https://savannah.gnu.org/bugs/?65909]

Quoted 8 lines

---
We have recently added this to the openembedded-core layer to help with RISC-V builds on certain platforms (e.g. the VisionFive 2). I have left the patch as it is there, including the link to the original bug submission and the author's attribution. Jason, if you would like to change the attribution or anything about the patch, let me know and I can re-submit.

However on riscv64, medany memory model only allows addressing in +-2GiB range, while large memory model could index the whole 64-bit address space. This patch would break cases that grub core and dynamically loaded modules (i.e. EFI memory allocated with type EFI_LOADER_CODE) are located too far away to address each other with medany code model.

The issue reported in your link is related to handling of R_RISCV_ALIGN relocations, which shouldn't happen anymore in Grub 2.14+ as commit 8fef533cf695 ("configure: Add -mno-relax on riscv*") is a fix for it.

Thanks,
Yao Zi

Quoted 37 lines
 configure.ac | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
configure.ac +4 -2
index d19fb625d..2a070c577 100644
@@ -1675,7 +1675,7 @@ fi]
 
 CFLAGS="$TARGET_CFLAGS"
 
-if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 || test "$target_cpu" = riscv64 ; then
+if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 ; 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"
@@ -1685,9 +1685,11 @@ if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 || test "$target_
   ])
   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
+  elif test "$target_cpu" = sparc64; then
     TARGET_CFLAGS="$TARGET_CFLAGS -mcmodel=medany"
   fi
+elif test "$target_cpu" = riscv64 ; then
+    TARGET_CFLAGS="$TARGET_CFLAGS -mcmodel=medany"
 fi
 
 # Stack smashing protector.
Signature · 7 lines
2.53.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#4 lore raw
Quoted 19 lines · Yao Zi
Quoted 4 lines · Trevor Gamblin

From: Jason Montleon <[email protected]>

Upstream-Status: Submitted [https://savannah.gnu.org/bugs/?65909]

Quoted 8 lines

---
We have recently added this to the openembedded-core layer to help with RISC-V builds on certain platforms (e.g. the VisionFive 2). I have left the patch as it is there, including the link to the original bug submission and the author's attribution. Jason, if you would like to change the attribution or anything about the patch, let me know and I can re-submit.

However on riscv64, medany memory model only allows addressing in +-2GiB range, while large memory model could index the whole 64-bit address space. This patch would break cases that grub core and dynamically loaded modules (i.e. EFI memory allocated with type EFI_LOADER_CODE) are located too far away to address each other with medany code model.

Quoted 3 lines

The issue reported in your link is related to handling of R_RISCV_ALIGN relocations, which shouldn't happen anymore in Grub 2.14+ as commit 8fef533cf695 ("configure: Add -mno-relax on riscv*") is a fix for it.

Thank you for clarifying. I've submitted a reversion patch in oe-core so we don't carry this and run into the problem later.

Trevor

Quoted 37 lines

Thanks,
Yao Zi

Quoted 33 lines
  configure.ac | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
configure.ac +4 -2
index d19fb625d..2a070c577 100644
@@ -1675,7 +1675,7 @@ fi]
 
  CFLAGS="$TARGET_CFLAGS"
 
-if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 || test "$target_cpu" = riscv64 ; then
+if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 ; 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"
@@ -1685,9 +1685,11 @@ if test "$target_cpu" = x86_64 || test "$target_cpu" = sparc64 || test "$target_
    ])
    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
+  elif test "$target_cpu" = sparc64; then
      TARGET_CFLAGS="$TARGET_CFLAGS -mcmodel=medany"
    fi
+elif test "$target_cpu" = riscv64 ; then
+    TARGET_CFLAGS="$TARGET_CFLAGS -mcmodel=medany"
  fi
 
  # Stack smashing protector.
Signature · 1 line
2.53.0
Quoted 4 lines
Quoted 4 lines

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel