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

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