diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index 9bba5da016e950e8acd0dc9d53a82b945e619163..38ed773c222d47a6dcb4dd35794f5fab037415ee 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -1702,7 +1702,9 @@ riscv_valid_lo_sum_p (enum riscv_symbol_type sym_type, machine_mode mode,
       align = (SYMBOL_REF_DECL (x)
 	       ? DECL_ALIGN (SYMBOL_REF_DECL (x))
 	       : 1);
-      size = (SYMBOL_REF_DECL (x) && DECL_SIZE (SYMBOL_REF_DECL (x))
+      size = (SYMBOL_REF_DECL (x)
+	      && DECL_SIZE (SYMBOL_REF_DECL (x))
+	      && tree_fits_uhwi_p (DECL_SIZE (SYMBOL_REF_DECL (x)))
 	      ? tree_to_uhwi (DECL_SIZE (SYMBOL_REF_DECL (x)))
 	      : 2*BITS_PER_WORD);
     }
diff --git a/gcc/testsuite/gnat.dg/array41.adb b/gcc/testsuite/gnat.dg/array41.adb
new file mode 100644
index 0000000000000000000000000000000000000000..d0d5a69eeaf0f15f50ca32afb4fe78c24100b3d8
--- /dev/null
+++ b/gcc/testsuite/gnat.dg/array41.adb
@@ -0,0 +1,37 @@
+-- { dg-do compile }
+
+with System.Storage_Elements;
+
+package body Array41 is
+
+   procedure Program_Initialization
+   with
+     Export,
+     Convention => Ada,
+     External_Name => "program_initialization";
+
+   procedure Program_Initialization is
+      use System.Storage_Elements;
+
+      Sdata : Storage_Element
+        with Import, Convention => Asm, External_Name => "_sdata";
+      Edata : Storage_Element
+        with Import, Convention => Asm, External_Name => "_edata";
+
+      Data_Size : constant Storage_Offset := Edata'Address - Sdata'Address;
+
+      --  Index from 1 so as to avoid subtracting 1 from the size
+      Data_In_Flash : constant Storage_Array (1 .. Data_Size)
+        with Import, Convention => Asm, External_Name => "_sidata";
+
+      Data_In_Sram : Storage_Array (1 .. Data_Size)
+        with Volatile, Import, Convention => Asm, External_Name => "_sdata";
+
+   begin
+      --  Copy rw data from flash to ram
+      for J in Data_In_Flash'Range loop
+         Data_In_Sram (J) := Data_In_Flash (J);
+      end loop;
+   end Program_Initialization;
+
+end Array41;
diff --git a/gcc/testsuite/gnat.dg/array41.ads b/gcc/testsuite/gnat.dg/array41.ads
new file mode 100644
index 0000000000000000000000000000000000000000..50cde3cd81936814f9b3ce0c15753884d2765970
--- /dev/null
+++ b/gcc/testsuite/gnat.dg/array41.ads
@@ -0,0 +1,5 @@
+package Array41 is
+
+  pragma Elaborate_Body;
+
+end Array41;