Don't edit bogus sh_link on reading relocatable objects (Oracle fix)
[binutils-gdb.git] / gdb / disasm-selftests.c
index f7d0ecca0e1fd73246ebc0a7880ad66785d77108..59c09c983813a1ae025a264c60378f22a3f2e644 100644 (file)
@@ -1,6 +1,6 @@
 /* Self tests for disassembler for GDB, the GNU debugger.
 
-   Copyright (C) 2017-2018 Free Software Foundation, Inc.
+   Copyright (C) 2017-2021 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
 
 #include "defs.h"
 #include "disasm.h"
-
-#if GDB_SELF_TEST
-#include "selftest.h"
+#include "gdbsupport/selftest.h"
 #include "selftest-arch.h"
+#include "gdbarch.h"
 
 namespace selftests {
 
@@ -70,21 +69,35 @@ print_one_insn_test (struct gdbarch *gdbarch)
       insn = xstormy16_insn;
       len = sizeof (xstormy16_insn);
       break;
+    case bfd_arch_nios2:
+    case bfd_arch_score:
+    case bfd_arch_riscv:
+      /* nios2, riscv, and score need to know the current instruction
+        to select breakpoint instruction.  Give the breakpoint
+        instruction kind explicitly.  */
+      {
+       int bplen;
+       insn = gdbarch_sw_breakpoint_from_kind (gdbarch, 4, &bplen);
+       len = bplen;
+      }
+      break;
     case bfd_arch_arc:
       /* PR 21003 */
       if (gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_arc_arc601)
        return;
+      goto generic_case;
+    case bfd_arch_i386:
+      {
+       const struct bfd_arch_info *info = gdbarch_bfd_arch_info (gdbarch);
+       /* The disassembly tests will fail on x86-linux because
+          opcodes rejects an attempt to disassemble for an arch with
+          a 64-bit address size when bfd_vma is 32-bit.  */
+       if (info->bits_per_address > sizeof (bfd_vma) * CHAR_BIT)
+         return;
+      }
       /* fall through */
-    case bfd_arch_nios2:
-    case bfd_arch_score:
-      /* nios2 and score need to know the current instruction to select
-        breakpoint instruction.  Give the breakpoint instruction kind
-        explicitly.  */
-      int bplen;
-      insn = gdbarch_sw_breakpoint_from_kind (gdbarch, 4, &bplen);
-      len = bplen;
-      break;
     default:
+    generic_case:
       {
        /* Test disassemble breakpoint instruction.  */
        CORE_ADDR pc = 0;
@@ -101,8 +114,7 @@ print_one_insn_test (struct gdbarch *gdbarch)
 
   /* Test gdb_disassembler for a given gdbarch by reading data from a
      pre-allocated buffer.  If you want to see the disassembled
-     instruction printed to gdb_stdout, set verbose to true.  */
-  static const bool verbose = false;
+     instruction printed to gdb_stdout, use maint selftest -verbose.  */
 
   class gdb_disassembler_test : public gdb_disassembler
   {
@@ -112,7 +124,7 @@ print_one_insn_test (struct gdbarch *gdbarch)
                                    const gdb_byte *insn,
                                    size_t len)
       : gdb_disassembler (gdbarch,
-                         (verbose ? gdb_stdout : &null_stream),
+                         (run_verbose () ? gdb_stdout : &null_stream),
                          gdb_disassembler_test::read_memory),
        m_insn (insn), m_len (len)
     {
@@ -121,7 +133,7 @@ print_one_insn_test (struct gdbarch *gdbarch)
     int
     print_insn (CORE_ADDR memaddr)
     {
-      if (verbose)
+      if (run_verbose ())
        {
          fprintf_unfiltered (stream (), "%s ",
                              gdbarch_bfd_arch_info (arch ())->arch_name);
@@ -129,7 +141,7 @@ print_one_insn_test (struct gdbarch *gdbarch)
 
       int len = gdb_disassembler::print_insn (memaddr);
 
-      if (verbose)
+      if (run_verbose ())
        fprintf_unfiltered (stream (), "\n");
 
       return len;
@@ -186,34 +198,41 @@ memory_error_test (struct gdbarch *gdbarch)
     }
   };
 
+  if (gdbarch_bfd_arch_info (gdbarch)->arch == bfd_arch_i386)
+    {
+      const struct bfd_arch_info *info = gdbarch_bfd_arch_info (gdbarch);
+      /* This test will fail on x86-linux because opcodes rejects an
+        attempt to disassemble for an arch with a 64-bit address size
+        when bfd_vma is 32-bit.  */
+      if (info->bits_per_address > sizeof (bfd_vma) * CHAR_BIT)
+       return;
+    }
+
   gdb_disassembler_test di (gdbarch);
   bool saw_memory_error = false;
 
-  TRY
+  try
     {
       di.print_insn (0);
     }
-  CATCH (ex, RETURN_MASK_ERROR)
+  catch (const gdb_exception_error &ex)
     {
       if (ex.error == MEMORY_ERROR)
        saw_memory_error = true;
     }
-  END_CATCH
 
   /* Expect MEMORY_ERROR.  */
   SELF_CHECK (saw_memory_error);
 }
 
 } // namespace selftests
-#endif /* GDB_SELF_TEST */
 
+void _initialize_disasm_selftests ();
 void
-_initialize_disasm_selftests (void)
+_initialize_disasm_selftests ()
 {
-#if GDB_SELF_TEST
   selftests::register_test_foreach_arch ("print_one_insn",
                                         selftests::print_one_insn_test);
   selftests::register_test_foreach_arch ("memory_error",
                                         selftests::memory_error_test);
-#endif
 }