Skip to content

Commit 9b02626

Browse files
committed
Fix regression with dynamic array bounds
Kévin discovered that commit ba005d3 ("Handle dynamic field properties") regressed a test in the internal AdaCore test suite. The problem here is that, when writing that patch, I did not consider the case where an array type's bounds might come from a member of a structure -- but where the array is not defined in the structure's scope. In this scenario the field-resolution logic would trip this condition: /* Defensive programming in case we see unusual DWARF. */ if (fi == nullptr) return nullptr; This patch reworks this area, partly backing out that commit, and fixes the problem. In the new code, I chose to simply duplicate the field's location information. This isn't totally ideal, in that it might result in multiple copies of a baton. However, this seemed nicer than tracking the DIE/field correspondence for every field in every CU -- my thinking here is that this particular dynamic scenario is relatively rare overall. Also, if the baton cost does prove onerous, we could intern the batons somewhere. Regression tested on x86-64 Fedora 41. I also tested this using the AdaCore internal test suite. Tested-By: Simon Marchi <[email protected]>
1 parent 27ba92a commit 9b02626

File tree

6 files changed

+154
-81
lines changed

6 files changed

+154
-81
lines changed

gdb/dwarf2/cu.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
#include "gdbsupport/unordered_set.h"
2828
#include "dwarf2/die.h"
2929

30-
struct field_info;
31-
3230
/* Type used for delaying computation of method physnames.
3331
See comments for compute_delayed_physnames. */
3432
struct delayed_method_info
@@ -401,15 +399,6 @@ struct dwarf2_cu
401399
.debug_str_offsets section (8 or 4, depending on the address size). */
402400
std::optional<ULONGEST> str_offsets_base;
403401

404-
/* We may encounter a DIE where a property refers to a field in some
405-
outer type. For example, in Ada, an array length may depend on a
406-
field in some outer record. In this case, we need to be able to
407-
stash a pointer to the 'struct field' into the appropriate
408-
dynamic property baton. However, the fields aren't created until
409-
the type has been fully processed, so we need a temporary
410-
back-link to this object. */
411-
struct field_info *field_info = nullptr;
412-
413402
/* Mark used when releasing cached dies. */
414403
bool m_mark : 1;
415404

gdb/dwarf2/loc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1747,7 +1747,7 @@ dwarf2_evaluate_property (const dynamic_prop *prop,
17471747
if (pinfo == NULL)
17481748
error (_("cannot find reference address for offset property"));
17491749

1750-
struct field resolved_field = *baton->field;
1750+
struct field resolved_field = baton->field;
17511751
resolve_dynamic_field (resolved_field, pinfo, initial_frame);
17521752

17531753
/* Storage for memory if we need to read it. */

gdb/dwarf2/loc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#ifndef GDB_DWARF2_LOC_H
2121
#define GDB_DWARF2_LOC_H
2222

23+
#include "gdbtypes.h"
2324
#include "dwarf2/expr.h"
2425

2526
struct symbol_computed_ops;
@@ -245,7 +246,7 @@ struct dwarf2_property_baton
245246
struct dwarf2_loclist_baton loclist;
246247

247248
/* The location is stored in a field of PROPERTY_TYPE. */
248-
struct field *field;
249+
struct field field;
249250
};
250251
};
251252

gdb/dwarf2/read.c

Lines changed: 33 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -698,12 +698,6 @@ struct field_info
698698
we're reading. */
699699
std::vector<variant_part_builder> variant_parts;
700700

701-
/* All the field batons that must be updated. This is only used
702-
when a type's property depends on a field of this structure; for
703-
example in Ada when an array's length may come from a field of an
704-
enclosing record. */
705-
gdb::unordered_map<dwarf2_property_baton *, sect_offset> field_batons;
706-
707701
/* Return the total number of fields (including baseclasses). */
708702
int nfields () const
709703
{
@@ -9985,6 +9979,29 @@ handle_member_location (struct die_info *die, struct dwarf2_cu *cu,
99859979
}
99869980
}
99879981

9982+
/* A helper that computes the location of a field. The CU and the
9983+
DW_TAG_member DIE are passed in. The results are stored in
9984+
*FP. */
9985+
9986+
static void
9987+
compute_field_location (dwarf2_cu *cu, die_info *die, field *fp)
9988+
{
9989+
/* Get type of field. */
9990+
fp->set_type (die_type (die, cu));
9991+
9992+
fp->set_loc_bitpos (0);
9993+
9994+
/* Get bit size of field (zero if none). */
9995+
attribute *attr = dwarf2_attr (die, DW_AT_bit_size, cu);
9996+
if (attr != nullptr)
9997+
fp->set_bitsize (attr->unsigned_constant ().value_or (0));
9998+
else
9999+
fp->set_bitsize (0);
10000+
10001+
/* Get bit offset of field. */
10002+
handle_member_location (die, cu, fp);
10003+
}
10004+
998810005
/* Add an aggregate field to the field list. */
998910006

999010007
static void
@@ -10040,20 +10057,7 @@ dwarf2_add_field (struct field_info *fip, struct die_info *die,
1004010057
}
1004110058
/* Data member other than a C++ static data member. */
1004210059

10043-
/* Get type of field. */
10044-
fp->set_type (die_type (die, cu));
10045-
10046-
fp->set_loc_bitpos (0);
10047-
10048-
/* Get bit size of field (zero if none). */
10049-
attr = dwarf2_attr (die, DW_AT_bit_size, cu);
10050-
if (attr != nullptr)
10051-
fp->set_bitsize (attr->unsigned_constant ().value_or (0));
10052-
else
10053-
fp->set_bitsize (0);
10054-
10055-
/* Get bit offset of field. */
10056-
handle_member_location (die, cu, fp);
10060+
compute_field_location (cu, die, fp);
1005710061

1005810062
/* Get name of field. */
1005910063
fieldname = dwarf2_name (die, cu);
@@ -11241,45 +11245,14 @@ handle_struct_member_die (struct die_info *child_die, struct type *type,
1124111245
update_field_batons. If OFFSET is not found, NULL is returned. */
1124211246

1124311247
static dwarf2_property_baton *
11244-
find_field_create_baton (dwarf2_cu *cu, sect_offset offset)
11248+
find_field_create_baton (dwarf2_cu *cu, die_info *die)
1124511249
{
11246-
field_info *fi = cu->field_info;
11247-
/* Defensive programming in case we see unusual DWARF. */
11248-
if (fi == nullptr)
11249-
return nullptr;
11250-
for (const auto &fld : fi->fields)
11251-
if (fld.offset == offset)
11252-
{
11253-
dwarf2_property_baton *result
11254-
= XOBNEW (&cu->per_objfile->objfile->objfile_obstack,
11255-
struct dwarf2_property_baton);
11256-
fi->field_batons[result] = offset;
11257-
return result;
11258-
}
11259-
return nullptr;
11260-
}
11261-
11262-
/* Update all the stored field property batons. FI is the field info
11263-
for the structure being created. TYPE is the corresponding struct
11264-
type with its fields already filled in. This fills in the correct
11265-
field for each baton that was stored while processing this
11266-
type. */
11267-
11268-
static void
11269-
update_field_batons (field_info *fi, struct type *type)
11270-
{
11271-
int n_bases = fi->baseclasses.size ();
11272-
for (auto &[baton, offset] : fi->field_batons)
11273-
{
11274-
for (int i = 0; i < fi->fields.size (); ++i)
11275-
{
11276-
if (fi->fields[i].offset == offset)
11277-
{
11278-
baton->field = &type->field (n_bases + i);
11279-
break;
11280-
}
11281-
}
11282-
}
11250+
dwarf2_property_baton *result
11251+
= XOBNEW (&cu->per_objfile->objfile->objfile_obstack,
11252+
struct dwarf2_property_baton);
11253+
memset (&result->field, 0, sizeof (result->field));
11254+
compute_field_location (cu, die, &result->field);
11255+
return result;
1128311256
}
1128411257

1128511258
/* Finish creating a structure or union type, including filling in its
@@ -11303,9 +11276,6 @@ process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
1130311276
struct field_info fi;
1130411277
std::vector<struct symbol *> template_args;
1130511278

11306-
scoped_restore save_field_info
11307-
= make_scoped_restore (&cu->field_info, &fi);
11308-
1130911279
for (die_info *child_die : die->children ())
1131011280
handle_struct_member_die (child_die, type, &fi, &template_args, cu);
1131111281

@@ -11327,11 +11297,7 @@ process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
1132711297

1132811298
/* Attach fields and member functions to the type. */
1132911299
if (fi.nfields () > 0)
11330-
{
11331-
dwarf2_attach_fields_to_type (&fi, type, cu);
11332-
update_field_batons (&fi, type);
11333-
}
11334-
11300+
dwarf2_attach_fields_to_type (&fi, type, cu);
1133511301
if (!fi.fnfieldlists.empty ())
1133611302
{
1133711303
dwarf2_attach_fn_fields_to_type (&fi, type, cu);
@@ -13946,13 +13912,12 @@ attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
1394613912
case DW_AT_data_member_location:
1394713913
case DW_AT_data_bit_offset:
1394813914
{
13949-
baton = find_field_create_baton (cu, target_die->sect_off);
13915+
baton = find_field_create_baton (cu, target_die);
1395013916
if (baton == nullptr)
1395113917
return 0;
1395213918

1395313919
baton->property_type = read_type_die (target_die->parent,
1395413920
target_cu);
13955-
baton->field = nullptr;
1395613921
prop->set_field (baton);
1395713922
break;
1395813923
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* This testcase is part of GDB, the GNU debugger.
2+
3+
Copyright 2025 Free Software Foundation, Inc.
4+
5+
This program is free software; you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation; either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with this program. If not, see <http://www.gnu.org/licenses/>. */
17+
18+
/* The data used for the structure. */
19+
20+
unsigned char our_data[] = { 3, 7, 11, 13 };
21+
22+
/* Dummy main function. */
23+
24+
int
25+
main()
26+
{
27+
asm ("main_label: .globl main_label");
28+
return 0;
29+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Copyright 2025 Free Software Foundation, Inc.
2+
3+
# This program is free software; you can redistribute it and/or modify
4+
# it under the terms of the GNU General Public License as published by
5+
# the Free Software Foundation; either version 3 of the License, or
6+
# (at your option) any later version.
7+
#
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU General Public License
14+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
16+
# Test handling of an array type whose bound comes from the field of a
17+
# structure.
18+
19+
load_lib dwarf.exp
20+
21+
# This test can only be run on targets which support DWARF-2 and use gas.
22+
require dwarf2_support
23+
24+
standard_testfile .c -debug.S
25+
26+
# Set up the DWARF for the test.
27+
28+
set asm_file [standard_output_file $srcfile2]
29+
Dwarf::assemble $asm_file {
30+
global srcdir subdir srcfile
31+
32+
cu {} {
33+
DW_TAG_compile_unit {
34+
{DW_AT_language @DW_LANG_Ada95}
35+
{DW_AT_name $srcfile}
36+
} {
37+
declare_labels byte array disc struct
38+
39+
byte: DW_TAG_base_type {
40+
{DW_AT_byte_size 1 DW_FORM_sdata}
41+
{DW_AT_encoding @DW_ATE_unsigned}
42+
{DW_AT_name byte}
43+
}
44+
45+
array: DW_TAG_array_type {
46+
{DW_AT_name array_type}
47+
{DW_AT_type :$byte}
48+
} {
49+
DW_TAG_subrange_type {
50+
{DW_AT_type :$byte}
51+
{DW_AT_upper_bound :$disc}
52+
}
53+
}
54+
55+
struct: DW_TAG_structure_type {
56+
{DW_AT_name discriminated}
57+
{DW_AT_byte_size 4 DW_FORM_sdata}
58+
} {
59+
disc: DW_TAG_member {
60+
{DW_AT_name disc}
61+
{DW_AT_type :$byte}
62+
{DW_AT_data_member_location 0 DW_FORM_sdata}
63+
}
64+
DW_TAG_member {
65+
{DW_AT_name nums}
66+
{DW_AT_type :$array}
67+
{DW_AT_data_member_location 1 DW_FORM_sdata}
68+
}
69+
}
70+
71+
DW_TAG_variable {
72+
{DW_AT_name "value"}
73+
{DW_AT_type :$struct}
74+
{DW_AT_external 1 DW_FORM_flag}
75+
{DW_AT_location {DW_OP_addr [gdb_target_symbol "our_data"]}
76+
SPECIAL_expr}
77+
}
78+
}
79+
}
80+
}
81+
82+
if { [prepare_for_testing "failed to prepare" ${testfile} \
83+
[list $srcfile $asm_file] {nodebug}] } {
84+
return -1
85+
}
86+
87+
gdb_test_no_output "set language ada"
88+
gdb_test "print value" \
89+
[string_to_regexp " = (disc => 3, nums => (7, 11, 13))"]

0 commit comments

Comments
 (0)