]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb/riscv: select rv32 target by default when requested
authorAndrew Burgess <andrew.burgess@embecosm.com>
Thu, 4 Feb 2021 18:34:13 +0000 (18:34 +0000)
committerAndrew Burgess <andrew.burgess@embecosm.com>
Wed, 24 Feb 2021 16:06:54 +0000 (16:06 +0000)
GDB for RISC-V always uses target descriptions.  When the target
doesn't provide a target description then a default is selected.
Usually this default is selected based on the properties of the
executable being debugged.  However, when there is no executable being
debugged we currently fallback to the riscv:rv64 target description as
the default.  This leads to strange behaviour like this:

  $ gdb
  (gdb) set architecture riscv:rv32
  (gdb) p sizeof ($pc)
  $1 = 8

Despite the users specifically setting the architecture to riscv:rv32
GDB still thinks that the target has riscv:rv64 register sizes.

The above is a bit of a contrived situation.  I actually ran into this
situation while trying to connect to a running riscv:rv32 target
without supplying an executable (the target didn't provide a target
description).  When I tried to set a register on the target I ran into
errors because GDB was passing 8 bytes to the target rather than the
expected 4.  Even when I manually specified the architecture (as
above) I couldn't convince GDB to only send 4 bytes.

This patch fixes this issue.  Now, when we selected a default target
description we will make use of the user selected architecture to
guide our choice.  In the above example we now get:

  $ gdb
  (gdb) set architecture riscv:rv32
  (gdb) p sizeof ($pc)
  $1 = 4

And my real world example of connecting to a remote without an
executable works fine.

I've used the fact that we can ask GDB about $pc even when no
executable is loaded as the basis for a test to cover this situation.

gdb/ChangeLog:

* riscv-tdep.c (riscv_features_from_gdbarch_info): Rename to...
(riscv_features_from_bfd): ...this.  Change parameter type to
'bfd*', and update as required.
(riscv_find_default_target_description): Update call to
riscv_features_from_bfd.  Select a default xlen based on
info.bfd_arch_info.
(riscv_gdbarch_init): Update call to riscv_features_from_bfd.

gdb/testsuite/ChangeLog:

* gdb.arch/riscv-default-tdesc.exp: New file.

gdb/ChangeLog
gdb/riscv-tdep.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.arch/riscv-default-tdesc.exp [new file with mode: 0644]

index b50a48ca1e535c6f7a726339f816a64fea0620f5..e136c98985fc79b65ed7bac5c10e6d786421f458 100644 (file)
@@ -1,3 +1,13 @@
+2021-02-24  Andrew Burgess  <andrew.burgess@embecosm.com>
+
+       * riscv-tdep.c (riscv_features_from_gdbarch_info): Rename to...
+       (riscv_features_from_bfd): ...this.  Change parameter type to
+       'bfd*', and update as required.
+       (riscv_find_default_target_description): Update call to
+       riscv_features_from_bfd.  Select a default xlen based on
+       info.bfd_arch_info.
+       (riscv_gdbarch_init): Update call to riscv_features_from_bfd.
+
 2021-02-24  Andrew Burgess  <andrew.burgess@embecosm.com>
 
        * eval.c (evaluate_subexp_standard): Call value_ind for points to
index 7463b0c33363db431a5b6a1ff52228b2574322c0..da86ed1271650e9e87dd98ebda9960c0c0691bea 100644 (file)
@@ -3215,13 +3215,11 @@ static const struct frame_unwind riscv_frame_unwind =
   /*.prev_arch     =*/ NULL,
 };
 
-/* Extract a set of required target features out of INFO, specifically the
-   bfd being executed is examined to see what target features it requires.
-   IF there is no current bfd, or the bfd doesn't indicate any useful
-   features then a RISCV_GDBARCH_FEATURES is returned in its default state.  */
+/* Extract a set of required target features out of ABFD.  If ABFD is
+   nullptr then a RISCV_GDBARCH_FEATURES is returned in its default state.  */
 
 static struct riscv_gdbarch_features
-riscv_features_from_gdbarch_info (const struct gdbarch_info info)
+riscv_features_from_bfd (const bfd *abfd)
 {
   struct riscv_gdbarch_features features;
 
@@ -3231,11 +3229,10 @@ riscv_features_from_gdbarch_info (const struct gdbarch_info info)
      only used at all if the target hasn't given us a description, so this
      is really a last ditched effort to do something sane before giving
      up.  */
-  if (info.abfd != NULL
-      && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
+  if (abfd != nullptr && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
     {
-      unsigned char eclass = elf_elfheader (info.abfd)->e_ident[EI_CLASS];
-      int e_flags = elf_elfheader (info.abfd)->e_flags;
+      unsigned char eclass = elf_elfheader (abfd)->e_ident[EI_CLASS];
+      int e_flags = elf_elfheader (abfd)->e_flags;
 
       if (eclass == ELFCLASS32)
        features.xlen = 4;
@@ -3273,13 +3270,14 @@ riscv_find_default_target_description (const struct gdbarch_info info)
 {
   /* Extract desired feature set from INFO.  */
   struct riscv_gdbarch_features features
-    = riscv_features_from_gdbarch_info (info);
+    = riscv_features_from_bfd (info.abfd);
 
-  /* If the XLEN field is still 0 then we got nothing useful from INFO.  In
-     this case we fall back to a minimal useful target, 8-byte x-registers,
-     with no floating point.  */
+  /* If the XLEN field is still 0 then we got nothing useful from INFO.BFD,
+     maybe there was no bfd object.  In this case we fall back to a minimal
+     useful target with no floating point, the x-register size is selected
+     based on the architecture from INFO.  */
   if (features.xlen == 0)
-    features.xlen = 8;
+    features.xlen = info.bfd_arch_info->bits_per_word == 32 ? 4 : 8;
 
   /* Now build a target description based on the feature set.  */
   return riscv_lookup_target_description (features);
@@ -3497,7 +3495,7 @@ riscv_gdbarch_init (struct gdbarch_info info,
      target, then check that this matches with what the target is
      providing.  */
   struct riscv_gdbarch_features abi_features
-    = riscv_features_from_gdbarch_info (info);
+    = riscv_features_from_bfd (info.abfd);
 
   /* If the ABI_FEATURES xlen is 0 then this indicates we got no useful abi
      features from the INFO object.  In this case we just treat the
index 6f1cb0a85268b5adc0fe683f7ec9f63ca8da5db1..4d2d5434aa08eda93fdacdd34aafe9d072737888 100644 (file)
@@ -1,3 +1,7 @@
+2021-02-24  Andrew Burgess  <andrew.burgess@embecosm.com>
+
+       * gdb.arch/riscv-default-tdesc.exp: New file.
+
 2021-02-24  Andrew Burgess  <andrew.burgess@embecosm.com>
 
        * gdb.fortran/pointer-to-pointer.exp: Additional tests.
diff --git a/gdb/testsuite/gdb.arch/riscv-default-tdesc.exp b/gdb/testsuite/gdb.arch/riscv-default-tdesc.exp
new file mode 100644 (file)
index 0000000..2b7e8fa
--- /dev/null
@@ -0,0 +1,59 @@
+# Copyright 2021 Free Software Foundation, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Check the size of the $pc register as the user changes the selected
+# architecture.  As no executable is provided then the size of the $pc
+# register is defined by the default target description selected by
+# GDB.
+#
+# This test ensures that GDB is selecting an RV32 default if the user
+# has forced the current architecture to be riscv:rv32.
+#
+# In all other cases the default will be RV64.
+
+if {![istarget "riscv*-*-*"]} {
+    verbose "Skipping ${gdb_test_file_name}."
+    return
+}
+
+# Start GDB with no executable.
+gdb_start
+
+# The tests are defined by a list of architecture names to switch too
+# and the expected size of $pc.  The first list entry is special and
+# has an empty architecture string, this checks GDB's default value on
+# startup.
+foreach data {{"" 8} {"riscv:rv32" 4} {"riscv:rv64" 8} {"riscv" 8} \
+                 {"auto" 8}} {
+    set arch [lindex $data 0]
+    set size [lindex $data 1]
+
+    # Switch architecture.
+    if { $arch != "" && $arch != "auto" } {
+       gdb_test "set architecture $arch" \
+           "The target architecture is set to \"$arch\"\\."
+    } elseif { $arch == "auto" } {
+       gdb_test "set architecture $arch" \
+           "The target architecture is set to \"auto\" \\(currently \"riscv\"\\)\\."
+    } else {
+       set arch "default architecture"
+    }
+
+    # Check the size of $pc.
+    with_test_prefix "$arch" {
+       gdb_test "p sizeof (\$pc)" " = $size" \
+           "size of \$pc register is $size"
+    }
+}