new_info_ptr,
                                                           &bytes_read);
          new_info_ptr += bytes_read;
-         scan_attributes (scanning_per_cu, new_reader, new_info_ptr, new_info_ptr,
-                          new_abbrev, name, linkage_name, flags, nullptr,
-                          parent_entry, maybe_defer, true);
+
+         if (new_reader->cu == reader->cu && new_info_ptr == watermark_ptr)
+           {
+             /* Self-reference, we're done.  */
+           }
+         else
+           scan_attributes (scanning_per_cu, new_reader, new_info_ptr,
+                            new_info_ptr, new_abbrev, name, linkage_name,
+                            flags, nullptr, parent_entry, maybe_defer, true);
        }
     }
 
       if (!spec)
        break;
 
+      struct die_info *prev_die = die;
       die = follow_die_ref (die, spec, &cu);
+      if (die == prev_die)
+       /* Self-reference, we're done.  */
+       break;
     }
 
   return NULL;
   struct dwarf2_cu *cu = *ref_cu;
   struct die_info *die;
 
+  if (attr->form != DW_FORM_GNU_ref_alt && src_die->sect_off == sect_off)
+    {
+      /* Self-reference, we're done.  */
+      return src_die;
+    }
+
   die = follow_die_offset (sect_off,
                           (attr->form == DW_FORM_GNU_ref_alt
                            || cu->per_cu->is_dwz),
 
--- /dev/null
+# Copyright 2023 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 that gdb doesn't hang or segfault on reading a DIE with a
+# specification reference to itself.
+
+load_lib dwarf.exp
+
+require dwarf2_support
+
+standard_testfile main.c .S
+
+# Create the DWARF.
+set asm_file [standard_output_file $srcfile2]
+Dwarf::assemble $asm_file {
+    cu {} {
+       compile_unit {{language @DW_LANG_C_plus_plus}} {
+           declare_labels c1
+           c1: class_type {
+               {name c1}
+               {specification :$c1}
+           }
+       }
+    }
+}
+
+if [prepare_for_testing "failed to prepare" $testfile "${asm_file} ${srcfile}" {}] {
+    return -1
+}
+
+set index [have_index $binfile]
+require {string eq $index ""}
+
+require !readnow
+
+gdb_test "maint print objfiles $testfile" \
+    "\r\n *qualified: *c1\r\n.*" \
+    "class c1 in cooked index"
+
+gdb_test "maint expand-symtabs"