Blame SOURCES/gdb-test-for-rhbz1976887.patch

ab2726
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
ab2726
From: =?UTF-8?q?Alexandra=20H=C3=A1jkov=C3=A1?= <ahajkova@redhat.com>
ab2726
Date: Wed, 29 Sep 2021 11:14:51 +0200
ab2726
Subject: gdb-test-for-rhbz1976887.patch
ab2726
ab2726
;; Backport test for RHBZ 1976887 (Kevin Buettner).
ab2726
ab2726
Test case reproducing PR28030 bug
ab2726
ab2726
The original reproducer for PR28030 required use of a specific
ab2726
compiler version - gcc-c++-11.1.1-3.fc34 is mentioned in the PR,
ab2726
though it seems probable that other gcc versions might also be able to
ab2726
reproduce the bug as well.  This commit introduces a test case which,
ab2726
using the DWARF assembler, provides a reproducer which is independent
ab2726
of the compiler version.  (Well, it'll work with whatever compilers
ab2726
the DWARF assembler works with.)
ab2726
ab2726
To the best of my knowledge, it's also the first test case which uses
ab2726
the DWARF assembler to provide debug info for a shared object.  That
ab2726
being the case, I provided more than the usual commentary which should
ab2726
allow this case to be used as a template when a combo shared
ab2726
library / DWARF assembler test case is required in the future.
ab2726
ab2726
I provide some details regarding the bug in a comment near the
ab2726
beginning of locexpr-dml.exp.
ab2726
ab2726
This problem was difficult to reproduce; I found myself constantly
ab2726
referring to the backtrace while trying to figure out what (else) I
ab2726
might be missing while trying to create a reproducer.  Below is a
ab2726
partial backtrace which I include for posterity.
ab2726
ab2726
 #0  internal_error (
ab2726
    file=0xc50110 "/ironwood1/sourceware-git/f34-pr28030/bld/../../worktree-pr28030/gdb/gdbtypes.c", line=5575,
ab2726
    fmt=0xc520c0 "Unexpected type field location kind: %d")
ab2726
    at /ironwood1/sourceware-git/f34-pr28030/bld/../../worktree-pr28030/gdbsupport/errors.cc:51
ab2726
 #1  0x00000000006ef0c5 in copy_type_recursive (objfile=0x1635930,
ab2726
    type=0x274c260, copied_types=0x30bb290)
ab2726
    at /ironwood1/sourceware-git/f34-pr28030/bld/../../worktree-pr28030/gdb/gdbtypes.c:5575
ab2726
 #2  0x00000000006ef382 in copy_type_recursive (objfile=0x1635930,
ab2726
    type=0x274ca10, copied_types=0x30bb290)
ab2726
    at /ironwood1/sourceware-git/f34-pr28030/bld/../../worktree-pr28030/gdb/gdbtypes.c:5602
ab2726
 #3  0x0000000000a7409a in preserve_one_value (value=0x24269f0,
ab2726
    objfile=0x1635930, copied_types=0x30bb290)
ab2726
    at /ironwood1/sourceware-git/f34-pr28030/bld/../../worktree-pr28030/gdb/value.c:2529
ab2726
 #4  0x000000000072012a in gdbscm_preserve_values (
ab2726
    extlang=0xc55720 <extension_language_guile>, objfile=0x1635930,
ab2726
    copied_types=0x30bb290)
ab2726
    at /ironwood1/sourceware-git/f34-pr28030/bld/../../worktree-pr28030/gdb/guile/scm-value.c:94
ab2726
 #5  0x00000000006a3f82 in preserve_ext_lang_values (objfile=0x1635930,
ab2726
    copied_types=0x30bb290)
ab2726
    at /ironwood1/sourceware-git/f34-pr28030/bld/../../worktree-pr28030/gdb/extension.c:568
ab2726
 #6  0x0000000000a7428d in preserve_values (objfile=0x1635930)
ab2726
    at /ironwood1/sourceware-git/f34-pr28030/bld/../../worktree-pr28030/gdb/value.c:2579
ab2726
 #7  0x000000000082d514 in objfile::~objfile (this=0x1635930,
ab2726
    __in_chrg=<optimized out>)
ab2726
    at /ironwood1/sourceware-git/f34-pr28030/bld/../../worktree-pr28030/gdb/objfiles.c:549
ab2726
 #8  0x0000000000831cc8 in std::_Sp_counted_ptr<objfile*, (__gnu_cxx::_Lock_policy)2>::_M_dispose (this=0x1654580)
ab2726
    at /usr/include/c++/11/bits/shared_ptr_base.h:348
ab2726
 #9  0x00000000004e6617 in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release (this=0x1654580) at /usr/include/c++/11/bits/shared_ptr_base.h:168
ab2726
 #10 0x00000000004e1d2f in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count (this=0x190bb88, __in_chrg=<optimized out>)
ab2726
    at /usr/include/c++/11/bits/shared_ptr_base.h:705
ab2726
 #11 0x000000000082feee in std::__shared_ptr<objfile, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr (this=0x190bb80, __in_chrg=<optimized out>)
ab2726
    at /usr/include/c++/11/bits/shared_ptr_base.h:1154
ab2726
 #12 0x000000000082ff0a in std::shared_ptr<objfile>::~shared_ptr (
ab2726
    this=0x190bb80, __in_chrg=<optimized out>)
ab2726
    at /usr/include/c++/11/bits/shared_ptr.h:122
ab2726
 #13 0x000000000085ed7e in __gnu_cxx::new_allocator<std::_List_node<std::shared_ptr<objfile> > >::destroy<std::shared_ptr<objfile> > (this=0x114bc00,
ab2726
    __p=0x190bb80) at /usr/include/c++/11/ext/new_allocator.h:168
ab2726
 #14 0x000000000085e88d in std::allocator_traits<std::allocator<std::_List_node<std::shared_ptr<objfile> > > >::destroy<std::shared_ptr<objfile> > (__a=...,
ab2726
    __p=0x190bb80) at /usr/include/c++/11/bits/alloc_traits.h:531
ab2726
 #15 0x000000000085e50c in std::__cxx11::list<std::shared_ptr<objfile>, std::allocator<std::shared_ptr<objfile> > >::_M_erase (this=0x114bc00, __position=
ab2726
  std::shared_ptr<objfile> (expired, weak count 1) = {get() = 0x1635930})
ab2726
    at /usr/include/c++/11/bits/stl_list.h:1925
ab2726
 #16 0x000000000085df0e in std::__cxx11::list<std::shared_ptr<objfile>, std::allocator<std::shared_ptr<objfile> > >::erase (this=0x114bc00, __position=
ab2726
  std::shared_ptr<objfile> (expired, weak count 1) = {get() = 0x1635930})
ab2726
    at /usr/include/c++/11/bits/list.tcc:158
ab2726
 #17 0x000000000085c748 in program_space::remove_objfile (this=0x114bbc0,
ab2726
    objfile=0x1635930)
ab2726
    at /ironwood1/sourceware-git/f34-pr28030/bld/../../worktree-pr28030/gdb/progspace.c:210
ab2726
 #18 0x000000000082d3ae in objfile::unlink (this=0x1635930)
ab2726
    at /ironwood1/sourceware-git/f34-pr28030/bld/../../worktree-pr28030/gdb/objfiles.c:487
ab2726
 #19 0x000000000082e68c in objfile_purge_solibs ()
ab2726
    at /ironwood1/sourceware-git/f34-pr28030/bld/../../worktree-pr28030/gdb/objfiles.c:875
ab2726
 #20 0x000000000092dd37 in no_shared_libraries (ignored=0x0, from_tty=1)
ab2726
    at /ironwood1/sourceware-git/f34-pr28030/bld/../../worktree-pr28030/gdb/solib.c:1236
ab2726
 #21 0x00000000009a37fe in target_pre_inferior (from_tty=1)
ab2726
    at /ironwood1/sourceware-git/f34-pr28030/bld/../../worktree-pr28030/gdb/target.c:2496
ab2726
 #22 0x00000000007454d6 in run_command_1 (args=0x0, from_tty=1,
ab2726
    run_how=RUN_NORMAL)
ab2726
    at /ironwood1/sourceware-git/f34-pr28030/bld/../../worktree-pr28030/gdb/infcmd.c:437
ab2726
ab2726
I'll note a few points regarding this backtrace:
ab2726
ab2726
Frame #1 is where the internal error occurs.  It's caused by an
ab2726
unhandled case for FIELD_LOC_KIND_DWARF_BLOCK.  The fix for this bug
ab2726
adds support for this case.
ab2726
ab2726
Frame #22 - it's a partial backtrace - shows that GDB is attempting to
ab2726
(re)run the program.  You can see the exact command sequence that was
ab2726
used for reproducing this problem in the PR (at
ab2726
https://sourceware.org/bugzilla/show_bug.cgi?id=28030), but in a
ab2726
nutshell, after starting the program and advancing to the appropriate
ab2726
source line, GDB was asked to step into libstdc++; a "finish" command
ab2726
was issued, returning a value.  The fact that a value was returned is
ab2726
very important.  GDB was then used to step back into libstdc++.  A
ab2726
breakpoint was set on a source line in the library after which a "run"
ab2726
command was issued.
ab2726
ab2726
Frame #19 shows a call to objfile_purge_solibs.  It's aptly named.
ab2726
ab2726
Frame #7 is a call to the destructor for one of the objfile solibs; it
ab2726
turned out to be the one for libstdc++.
ab2726
ab2726
Frames #6 thru #3 show various value preservation frames.  If you look
ab2726
at preserve_values() in gdb/value.c, the value history is preserved
ab2726
first, followed by internal variables, followed by values for the
ab2726
extension languages (python and guile).
ab2726
ab2726
diff --git a/gdb/testsuite/gdb.dwarf2/locexpr-data-member-location-lib.c b/gdb/testsuite/gdb.dwarf2/locexpr-data-member-location-lib.c
ab2726
new file mode 100644
ab2726
--- /dev/null
ab2726
+++ b/gdb/testsuite/gdb.dwarf2/locexpr-data-member-location-lib.c
ab2726
@@ -0,0 +1,48 @@
ab2726
+/* Copyright (C) 2021 Free Software Foundation, Inc.
ab2726
+
ab2726
+   This file is part of GDB.
ab2726
+
ab2726
+   This program is free software; you can redistribute it and/or modify
ab2726
+   it under the terms of the GNU General Public License as published by
ab2726
+   the Free Software Foundation; either version 3 of the License, or
ab2726
+   (at your option) any later version.
ab2726
+
ab2726
+   This program is distributed in the hope that it will be useful,
ab2726
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
ab2726
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ab2726
+   GNU General Public License for more details.
ab2726
+
ab2726
+   You should have received a copy of the GNU General Public License
ab2726
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
ab2726
+
ab2726
+#include "locexpr-data-member-location.h"
ab2726
+
ab2726
+struct A g_A = {3, 4};
ab2726
+struct B g_B = { {8, 9}, 10, 11 };
ab2726
+
ab2726
+B *
ab2726
+foo ()
ab2726
+{						/* foo prologue */
ab2726
+  asm ("foo_label: .globl foo_label");
ab2726
+  return &g_;;					/* foo return */
ab2726
+}						/* foo end */
ab2726
+
ab2726
+B *
ab2726
+bar (B *v)
ab2726
+{						/* bar prologue */
ab2726
+  asm ("bar_label: .globl bar_label");
ab2726
+  return v;					/* bar return */
ab2726
+}						/* bar end */
ab2726
+
ab2726
+/* Some of the DWARF assembler procs (e.g. function_range) compile
ab2726
+   this file, expecting it to be a complete program with a main()
ab2726
+   function.  When IS_SHAREDLIB is NOT defined, we have main() as
ab2726
+   defined below.  */
ab2726
+
ab2726
+#ifndef IS_SHAREDLIB
ab2726
+int
ab2726
+main ()
ab2726
+{
ab2726
+  B *b = foo ();
ab2726
+}
ab2726
+#endif
ab2726
diff --git a/gdb/testsuite/gdb.dwarf2/locexpr-data-member-location-main.c b/gdb/testsuite/gdb.dwarf2/locexpr-data-member-location-main.c
ab2726
new file mode 100644
ab2726
--- /dev/null
ab2726
+++ b/gdb/testsuite/gdb.dwarf2/locexpr-data-member-location-main.c
ab2726
@@ -0,0 +1,27 @@
ab2726
+/* Copyright (C) 2021 Free Software Foundation, Inc.
ab2726
+
ab2726
+   This file is part of GDB.
ab2726
+
ab2726
+   This program is free software; you can redistribute it and/or modify
ab2726
+   it under the terms of the GNU General Public License as published by
ab2726
+   the Free Software Foundation; either version 3 of the License, or
ab2726
+   (at your option) any later version.
ab2726
+
ab2726
+   This program is distributed in the hope that it will be useful,
ab2726
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
ab2726
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ab2726
+   GNU General Public License for more details.
ab2726
+
ab2726
+   You should have received a copy of the GNU General Public License
ab2726
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
ab2726
+
ab2726
+#include "locexpr-data-member-location.h"
ab2726
+
ab2726
+int
ab2726
+main (void)
ab2726
+{
ab2726
+  B *v1;
ab2726
+  v1 = bar (foo ());
ab2726
+
ab2726
+  return 0;
ab2726
+}
ab2726
diff --git a/gdb/testsuite/gdb.dwarf2/locexpr-data-member-location.exp b/gdb/testsuite/gdb.dwarf2/locexpr-data-member-location.exp
ab2726
new file mode 100644
ab2726
--- /dev/null
ab2726
+++ b/gdb/testsuite/gdb.dwarf2/locexpr-data-member-location.exp
ab2726
@@ -0,0 +1,349 @@
ab2726
+# Copyright 2021 Free Software Foundation, Inc.
ab2726
+
ab2726
+# This program is free software; you can redistribute it and/or modify
ab2726
+# it under the terms of the GNU General Public License as published by
ab2726
+# the Free Software Foundation; either version 3 of the License, or
ab2726
+# (at your option) any later version.
ab2726
+#
ab2726
+# This program is distributed in the hope that it will be useful,
ab2726
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
ab2726
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ab2726
+# GNU General Public License for more details.
ab2726
+#
ab2726
+# You should have received a copy of the GNU General Public License
ab2726
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
ab2726
+
ab2726
+# This test case uses the DWARF assembler to reproduce the problem
ab2726
+# described by PR28030.  The bug turned out to be that
ab2726
+# FIELD_LOC_KIND_DWARF_BLOCK was not handled when recursively copying
ab2726
+# a value's type when preserving the value history during the freeing
ab2726
+# up of objfiles associated with a shared object.  (Yes, figuring out
ab2726
+# how to make this happen in a concise test case turned out to be
ab2726
+# challenging.)
ab2726
+#
ab2726
+# The following elements proved to be necessary for reproducing the
ab2726
+# problem:
ab2726
+#
ab2726
+# 1) A location expression needed to be used with
ab2726
+#    DW_AT_data_member_location rather than a simple offset.
ab2726
+#    Moreover, this location expression needed to use opcodes
ab2726
+#    which GDB's DWARF reader could not convert to a simple
ab2726
+#    offset.  (Note, however, that GDB could probably be improved
ab2726
+#    to handle the opcodes chosen for this test; if decode_locdesc()
ab2726
+#    in dwarf2/read.c is ever updated to handle both DW_OP_pick and
ab2726
+#    DW_OP_drop, then this test could end up passing even if
ab2726
+#    the bug it's intended to test has not been fixed.)
ab2726
+#
ab2726
+# 2) The debug info containing the above DWARF info needed
ab2726
+#    to be associated with a shared object since the problem
ab2726
+#    occurred while GDB was preserving values during the
ab2726
+#    purging of shared objects.
ab2726
+#
ab2726
+# 3) After performing some simple gdb commands, the program is
ab2726
+#    run again.  In the course of running the objfile destructor
ab2726
+#    associated with the shared object, values are preserved
ab2726
+#    along with their types.  As noted earlier, it was during
ab2726
+#    the recursive type copy that the bug was observed.
ab2726
+#
ab2726
+# Therefore, due to #2 above, this test case creates debug info
ab2726
+# which is then used by a shared object.
ab2726
+
ab2726
+# This test can't be run on targets lacking shared library support.
ab2726
+if [skip_shlib_tests] {
ab2726
+    return 0
ab2726
+}
ab2726
+
ab2726
+load_lib dwarf.exp
ab2726
+
ab2726
+# This test can only be run on targets which support DWARF-2 and use gas.
ab2726
+if ![dwarf2_support] {
ab2726
+    return 0
ab2726
+}
ab2726
+
ab2726
+# gdb_test_file_name is the name of this file without the .exp
ab2726
+# extension.  Use it to form basenames for the main program
ab2726
+# and shared object.
ab2726
+set main_basename ${::gdb_test_file_name}-main
ab2726
+set lib_basename ${::gdb_test_file_name}-lib
ab2726
+
ab2726
+# We're generating DWARF assembly for the shared object; therefore,
ab2726
+# the source file for the library / shared object must be listed first
ab2726
+# (in the standard_testfile invocation) since ${srcfile} is used by
ab2726
+# get_func_info (for determining the start, end, and length of a
ab2726
+# function).
ab2726
+#
ab2726
+# The output of Dwarf::assemble will be placed in $lib_basename.S
ab2726
+# which will be ${srcfile3} after the execution of standard_testfile.
ab2726
+
ab2726
+standard_testfile $lib_basename.c $main_basename.c $lib_basename.S
ab2726
+
ab2726
+set libsrc "${::srcdir}/${::subdir}/${::srcfile}"
ab2726
+set lib_so [standard_output_file ${lib_basename}.so]
ab2726
+set asm_file [standard_output_file ${::srcfile3}]
ab2726
+
ab2726
+# We need to know the size of some types in order to write some of the
ab2726
+# debugging info that we're about to generate.  For that, we ask GDB
ab2726
+# by debugging the shared object associated with this test case.
ab2726
+
ab2726
+# Compile the shared library: -DIS_SHAREDLIB prevents main() from
ab2726
+# being defined.  Note that debugging symbols will be present for
ab2726
+# this compilation.
ab2726
+if {[gdb_compile_shlib $libsrc $lib_so \
ab2726
+                       {additional_flags=-DIS_SHAREDLIB debug}] != ""} {
ab2726
+    untested "failed to compile shared library"
ab2726
+    return
ab2726
+}
ab2726
+
ab2726
+# Start a fresh GDB and load the shared library.
ab2726
+clean_restart $lib_so
ab2726
+
ab2726
+# Using our running GDB session, determine sizes of several types.
ab2726
+set long_size [get_sizeof "long" -1]
ab2726
+set addr_size [get_sizeof "void *" -1]
ab2726
+set struct_A_size [get_sizeof "g_A" -1]
ab2726
+set struct_B_size [get_sizeof "g_B" -1]
ab2726
+
ab2726
+if { $long_size == -1 || $addr_size == -1 \
ab2726
+     || $struct_A_size == -1 || $struct_B_size == -1} {
ab2726
+    perror "Can't determine type sizes"
ab2726
+    return
ab2726
+}
ab2726
+
ab2726
+# Retrieve struct offset of MBR in struct TP
ab2726
+proc get_offsetof { tp mbr } {
ab2726
+    return [get_integer_valueof "&((${tp} *) 0)->${mbr}" -1]
ab2726
+}
ab2726
+
ab2726
+# Use running GDB session to get struct offsets
ab2726
+set A_a [get_offsetof A a]
ab2726
+set A_x [get_offsetof A x]
ab2726
+set B_a [get_offsetof B a]
ab2726
+set B_b [get_offsetof B b]
ab2726
+set B_x2 [get_offsetof B x2]
ab2726
+
ab2726
+# Create the DWARF.
ab2726
+Dwarf::assemble ${asm_file} {
ab2726
+    declare_labels L
ab2726
+
ab2726
+    # Find start, end, and length of functions foo and bar.
ab2726
+    # These calls to get_func_info will create and set variables
ab2726
+    # foo_start, bar_start, foo_end, bar_end, foo_len, and
ab2726
+    # bar_len.
ab2726
+    #
ab2726
+    # In order to get the right answers, get_func_info (and,
ab2726
+    # underneath, function_range) should use the same compiler flags
ab2726
+    # as those used to make a shared object.  For any targets that get
ab2726
+    # this far, -fpic is probably correct.
ab2726
+    #
ab2726
+    # Also, it should be noted that IS_SHAREDLIB is NOT defined as one
ab2726
+    # of the additional flags.  Not defining IS_SHAREDLIB will cause a
ab2726
+    # main() to be defined for the compilation of the shared library
ab2726
+    # source file which happens as a result of using get_func_info;
ab2726
+    # this is currently required in order to this facility.
ab2726
+    set flags {additional_flags=-fpic debug}
ab2726
+    get_func_info foo $flags
ab2726
+    get_func_info bar $flags
ab2726
+
ab2726
+    cu {} {
ab2726
+	DW_TAG_compile_unit {
ab2726
+	    {DW_AT_language @DW_LANG_C_plus_plus}
ab2726
+	    {name ${::srcfile}}
ab2726
+	    {stmt_list $L DW_FORM_sec_offset}
ab2726
+        } {
ab2726
+	    declare_labels int_label class_A_label class_B_label \
ab2726
+	                   B_ptr_label
ab2726
+
ab2726
+	    int_label: DW_TAG_base_type {
ab2726
+		{DW_AT_byte_size ${::long_size} DW_FORM_udata}
ab2726
+		{DW_AT_encoding @DW_ATE_signed}
ab2726
+		{DW_AT_name "int"}
ab2726
+	    }
ab2726
+
ab2726
+	    class_A_label: DW_TAG_class_type {
ab2726
+		{DW_AT_name "A"}
ab2726
+		{DW_AT_byte_size ${::struct_A_size} DW_FORM_sdata}
ab2726
+	    } {
ab2726
+		DW_TAG_member {
ab2726
+		    {DW_AT_name "a"}
ab2726
+		    {DW_AT_type :$int_label}
ab2726
+		    {DW_AT_data_member_location ${::A_a} DW_FORM_udata}
ab2726
+		}
ab2726
+		DW_TAG_member {
ab2726
+		    {DW_AT_name "x"}
ab2726
+		    {DW_AT_type :$int_label}
ab2726
+		    {DW_AT_data_member_location ${::A_x} DW_FORM_udata}
ab2726
+		}
ab2726
+	    }
ab2726
+
ab2726
+	    class_B_label: DW_TAG_class_type {
ab2726
+		{DW_AT_name "B"}
ab2726
+		{DW_AT_byte_size ${::struct_B_size} DW_FORM_sdata}
ab2726
+	    } {
ab2726
+		# While there are easier / better ways to specify an
ab2726
+		# offset used by DW_AT_data_member_location than that
ab2726
+		# used below, we need a location expression here in
ab2726
+		# order to reproduce the bug.  Moreover, this location
ab2726
+		# expression needs to use opcodes that aren't handled
ab2726
+		# by decode_locdesc() in dwarf2/read.c; if we use
ab2726
+		# opcodes that _are_ handled by that function, the
ab2726
+		# location expression will be converted into a simple
ab2726
+		# offset - which will then (again) not reproduce the
ab2726
+		# bug.  At the time that this test was written,
ab2726
+		# neither DW_OP_pick nor DW_OP_drop were being handled
ab2726
+		# by decode_locdesc(); this is why those opcodes were
ab2726
+		# chosen.
ab2726
+		DW_TAG_inheritance {
ab2726
+		    {DW_AT_type :$class_A_label}
ab2726
+		    {DW_AT_data_member_location {
ab2726
+			DW_OP_constu ${::B_a}
ab2726
+			DW_OP_plus
ab2726
+			DW_OP_pick 0
ab2726
+			DW_OP_drop} SPECIAL_expr}
ab2726
+		    {DW_AT_accessibility 1 DW_FORM_data1}
ab2726
+		}
ab2726
+		DW_TAG_member {
ab2726
+		    {DW_AT_name "b"}
ab2726
+		    {DW_AT_type :$int_label}
ab2726
+		    {DW_AT_data_member_location ${::B_b} DW_FORM_udata}
ab2726
+		}
ab2726
+		DW_TAG_member {
ab2726
+		    {DW_AT_name "x2"}
ab2726
+		    {DW_AT_type :$int_label}
ab2726
+		    {DW_AT_data_member_location ${::B_x2} DW_FORM_udata}
ab2726
+		}
ab2726
+	    }
ab2726
+
ab2726
+	    B_ptr_label: DW_TAG_pointer_type {
ab2726
+		{DW_AT_type :$class_B_label}
ab2726
+		{DW_AT_byte_size ${::addr_size} DW_FORM_sdata}
ab2726
+	    }
ab2726
+
ab2726
+	    DW_TAG_variable {
ab2726
+		{DW_AT_name "g_A"}
ab2726
+		{DW_AT_type :$class_A_label}
ab2726
+		{DW_AT_external 1 flag}
ab2726
+		{DW_AT_location {DW_OP_addr [gdb_target_symbol "g_A"]} \
ab2726
+		                 SPECIAL_expr}
ab2726
+	    }
ab2726
+
ab2726
+	    DW_TAG_variable {
ab2726
+		{DW_AT_name "g_B"}
ab2726
+		{DW_AT_type :$class_B_label}
ab2726
+		{DW_AT_external 1 flag}
ab2726
+		{DW_AT_location {DW_OP_addr [gdb_target_symbol "g_B"]} \
ab2726
+		                 SPECIAL_expr}
ab2726
+	    }
ab2726
+
ab2726
+	    # We can't use MACRO_AT for the definitions of foo and bar
ab2726
+	    # because it doesn't provide a way to pass the appropriate
ab2726
+	    # flags.  Therefore, we list the name, low_pc, and high_pc
ab2726
+	    # explicitly.
ab2726
+	    DW_TAG_subprogram {
ab2726
+		{DW_AT_name foo}
ab2726
+		{DW_AT_low_pc $foo_start DW_FORM_addr}
ab2726
+		{DW_AT_high_pc $foo_end DW_FORM_addr}
ab2726
+		{DW_AT_type :${B_ptr_label}}
ab2726
+		{DW_AT_external 1 flag}
ab2726
+	    }
ab2726
+
ab2726
+	    DW_TAG_subprogram {
ab2726
+		{DW_AT_name bar}
ab2726
+		{DW_AT_low_pc $bar_start DW_FORM_addr}
ab2726
+		{DW_AT_high_pc $bar_end DW_FORM_addr}
ab2726
+		{DW_AT_type :${B_ptr_label}}
ab2726
+		{DW_AT_external 1 flag}
ab2726
+	    } {
ab2726
+		DW_TAG_formal_parameter {
ab2726
+		    {DW_AT_name v}
ab2726
+		    {DW_AT_type :${B_ptr_label}}
ab2726
+		}
ab2726
+	    }
ab2726
+	}
ab2726
+    }
ab2726
+
ab2726
+    lines {version 2} L {
ab2726
+	include_dir "${::srcdir}/${::subdir}"
ab2726
+	file_name "${::srcfile}" 1
ab2726
+
ab2726
+	# Generate a line table program.
ab2726
+	program {
ab2726
+	    {DW_LNE_set_address $foo_start}
ab2726
+	    {line [gdb_get_line_number "foo prologue"]}
ab2726
+	    {DW_LNS_copy}
ab2726
+	    {DW_LNE_set_address foo_label}
ab2726
+	    {line [gdb_get_line_number "foo return"]}
ab2726
+	    {DW_LNS_copy}
ab2726
+	    {line [gdb_get_line_number "foo end"]}
ab2726
+	    {DW_LNS_copy}
ab2726
+	    {DW_LNE_set_address $foo_end}
ab2726
+	    {DW_LNS_advance_line 1}
ab2726
+	    {DW_LNS_copy}
ab2726
+	    {DW_LNE_end_sequence}
ab2726
+
ab2726
+	    {DW_LNE_set_address $bar_start}
ab2726
+	    {line [gdb_get_line_number "bar prologue"]}
ab2726
+	    {DW_LNS_copy}
ab2726
+	    {DW_LNE_set_address bar_label}
ab2726
+	    {line [gdb_get_line_number "bar return"]}
ab2726
+	    {DW_LNS_copy}
ab2726
+	    {line [gdb_get_line_number "bar end"]}
ab2726
+	    {DW_LNS_copy}
ab2726
+	    {DW_LNE_set_address $bar_end}
ab2726
+	    {DW_LNS_advance_line 1}
ab2726
+	    {DW_LNS_copy}
ab2726
+	    {DW_LNE_end_sequence}
ab2726
+	}
ab2726
+    }
ab2726
+}
ab2726
+
ab2726
+# Compile the shared object again, but this time include / use the
ab2726
+# DWARF info that we've created above.  Note that (again)
ab2726
+# -DIS_SHAREDLIB is used to prevent inclusion of main() in the shared
ab2726
+# object.  Also note the use of the "nodebug" option.  Any debugging
ab2726
+# information that we need will be provided by the DWARF info created
ab2726
+# above.
ab2726
+if {[gdb_compile_shlib [list $libsrc $asm_file] $lib_so \
ab2726
+                       {additional_flags=-DIS_SHAREDLIB nodebug}] != ""} {
ab2726
+    untested "failed to compile shared library"
ab2726
+    return
ab2726
+}
ab2726
+
ab2726
+# Compile the main program for use with the shared object.
ab2726
+if [prepare_for_testing "failed to prepare" ${testfile} \
ab2726
+                        ${::srcfile2} [list debug shlib=$lib_so]] {
ab2726
+    return -1
ab2726
+}
ab2726
+
ab2726
+# Do whatever is necessary to make sure that the shared library is
ab2726
+# loaded for remote targets.
ab2726
+gdb_load_shlib ${lib_so}
ab2726
+
ab2726
+if ![runto_main] then {
ab2726
+    fail "can't run to main"
ab2726
+    return
ab2726
+}
ab2726
+
ab2726
+# Step into foo so that we can finish out of it.
ab2726
+gdb_test "step" "foo .. at .* foo end.*" "step into foo"
ab2726
+
ab2726
+# Finishing out of foo will create a value that will later need to
ab2726
+# be preserved when restarting the program.
ab2726
+gdb_test "finish" "= \\(class B \\*\\) ${::hex} .*" "finish out of foo"
ab2726
+
ab2726
+# Dereferencing and printing the return value isn't necessary
ab2726
+# for reproducing the bug, but we should make sure that the
ab2726
+# return value is what we expect it to be.
ab2726
+gdb_test "p *$" { = { = {a = 8, x = 9}, b = 10, x2 = 11}} \
ab2726
+         "dereference return value"
ab2726
+
ab2726
+# The original PR28030 reproducer stepped back into the shared object,
ab2726
+# so we'll do the same here:
ab2726
+gdb_test "step" "bar \\(.*" "step into bar"
ab2726
+
ab2726
+# We don't want a clean restart here since that will be too clean.
ab2726
+# The original reproducer for PR28030 set a breakpoint in the shared
ab2726
+# library and then restarted via "run".  The command below does roughly
ab2726
+# the same thing.  It's at this step that an internal error would
ab2726
+# occur for PR28030.  The "message" argument tells runto to turn on
ab2726
+# the printing of PASSes while runto is doing its job.
ab2726
+runto "bar" message
ab2726
diff --git a/gdb/testsuite/gdb.dwarf2/locexpr-data-member-location.h b/gdb/testsuite/gdb.dwarf2/locexpr-data-member-location.h
ab2726
new file mode 100644
ab2726
--- /dev/null
ab2726
+++ b/gdb/testsuite/gdb.dwarf2/locexpr-data-member-location.h
ab2726
@@ -0,0 +1,30 @@
ab2726
+/* Copyright (C) 2021 Free Software Foundation, Inc.
ab2726
+
ab2726
+   This file is part of GDB.
ab2726
+
ab2726
+   This program is free software; you can redistribute it and/or modify
ab2726
+   it under the terms of the GNU General Public License as published by
ab2726
+   the Free Software Foundation; either version 3 of the License, or
ab2726
+   (at your option) any later version.
ab2726
+
ab2726
+   This program is distributed in the hope that it will be useful,
ab2726
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
ab2726
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ab2726
+   GNU General Public License for more details.
ab2726
+
ab2726
+   You should have received a copy of the GNU General Public License
ab2726
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
ab2726
+
ab2726
+typedef struct A {
ab2726
+    long a;
ab2726
+    long x;
ab2726
+} A;
ab2726
+
ab2726
+typedef struct B {
ab2726
+    A a;
ab2726
+    long b;
ab2726
+    long x2;
ab2726
+} B;
ab2726
+
ab2726
+extern B *foo ();
ab2726
+extern B *bar (B *v);