Compute proper length for dynamic types of TYPE_CODE_TYPEDEF

This patch fixes gdb/21356 in which we hit an assertion in
value_contents_bits_eq:

(gdb) p container_object2
(gdb) p container_object2
$1 = {_container_member2 = 15, _vla_struct_object2 = {_some_member = 0,
    _vla_field = {
../../src/gdb/value.c:829: internal-error: \
  int value_contents_bits_eq(const value*, int, const value*, int, int): \
  Assertion `offset1 + length \
             <= TYPE_LENGTH (val1->enclosing_type) * TARGET_CHAR_BIT' failed.

This is happening because TYPE_LENGTH (val1->enclosing_type) is erroneously
based on enclosing_type, which is a typedef, instead of the actual underlying
type.

This can be traced back to resolve_dynamic_struct, where the size of the
type is computed:
...
        TYPE_FIELD_TYPE (resolved_type, i)
          = resolve_dynamic_type_internal (TYPE_FIELD_TYPE (resolved_type, i),
                                           &pinfo, 0);
        gdb_assert (TYPE_FIELD_LOC_KIND (resolved_type, i)
                    == FIELD_LOC_KIND_BITPOS);

        new_bit_length = TYPE_FIELD_BITPOS (resolved_type, i);
        if (TYPE_FIELD_BITSIZE (resolved_type, i) != 0)
          new_bit_length += TYPE_FIELD_BITSIZE (resolved_type, i);
        else
          new_bit_length += (TYPE_LENGTH (TYPE_FIELD_TYPE (resolved_type, i))
                             * TARGET_CHAR_BIT);
...

In this function, resolved_type is TYPE_CODE_TYPEDEF which is not what we
want to use to calculate the size of the actual field.

This patch fixes this and the similar problem in resolve_dynamic_union.

gdb/ChangeLog:
2020-06-11  Keith Seitz  <keiths@redhat.com>

	PR gdb/21356
	* gdbtypes.c (resolve_dynamic_union, resolve_dynamic_struct):
	Resolve typedefs for type length calculations.

gdb/testsuite/ChangeLog:
2020-06-11  Keith Seitz  <keiths@redhat.com>

	PR gdb/21356
	* gdb.base/vla-datatypes.c (vla_factory): Add typedef for struct
	vla_struct.
	Add new struct vla_typedef and union vla_typedef_union and
	corresponding instantiation objects.
	Initialize new objects.
	* gdb.base/vla-datatypes.exp: Add tests for vla_typedef_struct_object
	and vla_typedef_union_object.
	Fixup type for vla_struct_object.
This commit is contained in:
Keith Seitz
2020-06-11 14:34:44 +02:00
committed by Tom de Vries
parent a8baf0a32b
commit 2f33032a93
5 changed files with 60 additions and 6 deletions

View File

@ -2264,8 +2264,10 @@ resolve_dynamic_union (struct type *type,
t = resolve_dynamic_type_internal (resolved_type->field (i).type (),
addr_stack, 0);
resolved_type->field (i).set_type (t);
if (TYPE_LENGTH (t) > max_len)
max_len = TYPE_LENGTH (t);
struct type *real_type = check_typedef (t);
if (TYPE_LENGTH (real_type) > max_len)
max_len = TYPE_LENGTH (real_type);
}
TYPE_LENGTH (resolved_type) = max_len;
@ -2521,8 +2523,12 @@ resolve_dynamic_struct (struct type *type,
if (TYPE_FIELD_BITSIZE (resolved_type, i) != 0)
new_bit_length += TYPE_FIELD_BITSIZE (resolved_type, i);
else
new_bit_length += (TYPE_LENGTH (resolved_type->field (i).type ())
* TARGET_CHAR_BIT);
{
struct type *real_type
= check_typedef (resolved_type->field (i).type ());
new_bit_length += (TYPE_LENGTH (real_type) * TARGET_CHAR_BIT);
}
/* Normally, we would use the position and size of the last field
to determine the size of the enclosing structure. But GCC seems