Fix gdb crash when trying to print the address of a synthetic C++ reference

After compiling a program which uses C++ references some optimizations may
convert the references into synthetic "pointers".  Trying to print the address
of one of such synthetic references causes gdb to crash with the following
error:

(gdb) print &ref
/build/buildd/gdb-7.7.1/gdb/dwarf2loc.c:1624: internal-error: Should not be able to create a lazy value with an enclosing type
A problem internal to GDB has been detected,
further debugging may prove unreliable.

Apparently, what was causing it was that value_addr returns a copy of the value
that represents the reference with its type set to T* instead of T&.  However,
its enclosing_type is left untouched, which fails a check made in
read_pieced_value.  We only see the crash happen for references that are
synthetic because they're treated as pieced values, thus the call to
read_pieced_value.

On a related note, it seems that in general there are all sorts of breakage
when working with synthetic references.  This is reported here:

https://sourceware.org/bugzilla/show_bug.cgi?id=19893

gdb/ChangeLog:
2016-04-18  Martin Galvan  <martin.galvan@tallertechnologies.com>

	* valops.c (value_addr): For C++ references, set the copied value's
	enclosing_type as well.

gdb/testsuite/ChangeLog:
2016-04-18  Martin Galvan  <martin.galvan@tallertechnologies.com>

	* gdb.dwarf2/implref.exp: New file.
This commit is contained in:
Martin Galvan
2016-04-18 10:58:14 -03:00
parent 0c13f7e559
commit a22df60ad2
4 changed files with 120 additions and 4 deletions

View File

@@ -1463,11 +1463,20 @@ value_addr (struct value *arg1)
if (TYPE_CODE (type) == TYPE_CODE_REF)
{
/* Copy the value, but change the type from (T&) to (T*). We
keep the same location information, which is efficient, and
allows &(&X) to get the location containing the reference. */
keep the same location information, which is efficient, and
allows &(&X) to get the location containing the reference.
Do the same to its enclosing type for consistency. */
struct type *type_ptr
= lookup_pointer_type (TYPE_TARGET_TYPE (type));
struct type *enclosing_type
= check_typedef (value_enclosing_type (arg1));
struct type *enclosing_type_ptr
= lookup_pointer_type (TYPE_TARGET_TYPE (enclosing_type));
arg2 = value_copy (arg1);
deprecated_set_value_type (arg2,
lookup_pointer_type (TYPE_TARGET_TYPE (type)));
deprecated_set_value_type (arg2, type_ptr);
set_value_enclosing_type (arg2, enclosing_type_ptr);
return arg2;
}
if (TYPE_CODE (type) == TYPE_CODE_FUNC)