binutils-gdb/gdb/dwarf2/die.h
Simon Marchi 2b0c7f41d1 gdb/dwarf: split dwarf2_cu::ranges_base in two
Consider the test case added in this patch.  It defines a compilation
unit with a DW_AT_rnglists_base attribute (used for attributes of form
DW_FORM_rnglistx), but also uses DW_AT_ranges of form
DW_FORM_sec_offset:

    0x00000027: DW_TAG_compile_unit
                  DW_AT_ranges [DW_FORM_sec_offset] (0x0000004c
                     [0x0000000000005000, 0x0000000000006000))
                  DW_AT_rnglists_base [DW_FORM_sec_offset]  (0x00000044)

The DW_AT_rnglists_base does not play a role in reading the DW_AT_ranges of
form DW_FORM_sec_offset, but it should also not do any harm.

This case is currently not handled correctly by GDB.  This is not
something that a compiler is likely to emit, but in my opinion there's
no reason why GDB should fail reading it.

The problem is that in partial_die_info::read and a few other places
where the same logic is replicated, the cu->ranges_base value,
containing the DW_AT_rnglists_base value, is wrongfully added to the
DW_AT_ranges value.

It is quite messy how to decide whether cu->ranges_base should be added
to the attribute's value or not.  But to summarize, the only time we
want to add it is when the attribute comes from a pre-DWARF 5 split unit
file (a .dwo) [1].  In this case, the DW_AT_ranges attribute from the
split unit file will have form DW_FORM_sec_offset, pointing somewhere in
the linked file's .debug_ranges section.  *But* it's not a "true"
DW_FORM_sec_offset, in that it's an offset relative to the beginning of
that CU's contribution in the section, not relative to the beginning of
the section.  So in that case, and only that case, do we want to add the
ranges base value, which we found from the DW_AT_GNU_ranges_base
attribute on the skeleton unit.

Almost all instances of the DW_AT_ranges attribute will be found in the
split unit (on DW_TAG_subprogram, for example), and therefore need to
have the ranges base added.  However, the DW_TAG_compile_unit DIE in the
skeleton may also have a DW_AT_ranges attribute.  For that one, the
ranges base must not be added.  Once the DIEs have been loaded in GDB,
however, the distinction between what's coming from the skeleton and
what's coming from the split unit is not clear.  It is all merged in one
big happy tree.  So how do we know if a given attribute comes from the
split unit or not?

We use the fact that in pre-DWARF 5 split DWARF, DW_AT_ranges is found
on the skeleton's DW_TAG_compile_unit (in the linked file) and never in
the split unit's DW_TAG_compile_unit.  This is why you have this in
partial_die_info::read:

      int need_ranges_base = (tag != DW_TAG_compile_unit
			      && attr.form != DW_FORM_rnglistx);

However, with the corner case described above (where we have a
DW_AT_rnglists_base attribute and a DW_AT_ranges attribute of form
DW_FORM_sec_offset) the condition gets it wrong when it encounters an
attribute like DW_TAG_subprogram with a DW_AT_ranges attribute of
DW_FORM_sec_offset form: it thinks that it is necessary to add the base,
when it reality it is not.

The problem boils down to failing to differentiate these cases:

  - a DW_AT_ranges attribute of form DW_FORM_sec_offset in a
    pre-DWARF 5 split unit (in which case we need to add the base)
  - a DW_AT_ranges attribute of form DW_FORM_sec_offset in a DWARF 5
    non-split unit (in which case we must not add the base)

What makes it unnecessarily complex is that the cu->ranges_base field is
overloaded, used to hold the pre-DWARF 5, non-standard
DW_AT_GNU_ranges_base and the DWARF 5 DW_AT_rnglists_base.  In reality,
these two are called "bases" but are not the same thing.  The result is
that we need twisted conditions to try to determine whether or not we
should add the base to the attribute's value.

To fix it, split the field in two distinct fields.  I renamed everything
related to the "old" ranges base to "gnu_ranges_base", to make it clear
that it's about the non-standard, pre-DWARF 5 thing.  And everything
related to the DWARF 5 thing gets renamed "rnglists".  I think it
becomes much easier to reason this way.

The issue described above gets fixed by the fact that the
DW_AT_rnglists_base value does not end up in cu->gnu_ranges_base, so
cu->gnu_ranges_base stays 0.  The condition to determine whether
gnu_ranges_base should be added can therefore be simplified back to:

  tag != DW_TAG_compile_unit

... as it was before rnglistx support was added.

Extend the gdb.dwarf2/rnglists-sec-offset.exp to cover this case.  I
also extended the test case for loclists similarly, just to see if there
would be some similar problem.  There wasn't, but I think it's not a bad
idea to test that case for loclists as well, so I left it in the patch.

[1] https://gcc.gnu.org/wiki/DebugFission

gdb/ChangeLog:

	* dwarf2/die.h (struct die_info) <ranges_base>: Split in...
	<gnu_ranges_base>: ... this...
	<rnglists_base>: ... and this.
	* dwarf2/read.c (struct dwarf2_cu) <ranges_base>: Split in...
	<gnu_ranges_base>: ... this...
	<rnglists_base>: ... and this.
	(read_cutu_die_from_dwo): Adjust
	(dwarf2_get_pc_bounds): Adjust
	(dwarf2_record_block_ranges): Adjust.
	(read_full_die_1): Adjust
	(partial_die_info::read): Adjust.
	(read_rnglist_index): Adjust.

gdb/testsuite/ChangeLog:

	* gdb.dwarf2/rnglists-sec-offset.exp: Add test for DW_AT_ranges
	of DW_FORM_sec_offset form plus DW_AT_rnglists_base attribute.
	* gdb.dwarf2/loclists-sec-offset.exp: Add test for
	DW_AT_location of DW_FORM_sec_offset plus DW_AT_loclists_base
	attribute

Change-Id: Icd109038634b75d0e6e9d7d1dcb62fb9eb951d83
2021-02-02 10:42:11 -05:00

130 lines
3.9 KiB
C

/* DWARF DIEs
Copyright (C) 2003-2021 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#ifndef GDB_DWARF2_DIE_H
#define GDB_DWARF2_DIE_H
#include "complaints.h"
/* This data structure holds a complete die structure. */
struct die_info
{
/* Return the named attribute or NULL if not there, but do not
follow DW_AT_specification, etc. */
struct attribute *attr (dwarf_attribute name)
{
for (unsigned i = 0; i < num_attrs; ++i)
if (attrs[i].name == name)
return &attrs[i];
return NULL;
}
/* Return the address base of the compile unit, which, if exists, is
stored either at the attribute DW_AT_GNU_addr_base, or
DW_AT_addr_base. */
gdb::optional<ULONGEST> addr_base ()
{
for (unsigned i = 0; i < num_attrs; ++i)
if (attrs[i].name == DW_AT_addr_base
|| attrs[i].name == DW_AT_GNU_addr_base)
{
if (attrs[i].form_is_unsigned ())
{
/* If both exist, just use the first one. */
return attrs[i].as_unsigned ();
}
complaint (_("address base attribute (offset %s) as wrong form"),
sect_offset_str (sect_off));
}
return gdb::optional<ULONGEST> ();
}
/* Return the base address of the compile unit into the .debug_ranges section,
which, if exists, is stored in the DW_AT_GNU_ranges_base attribute. This
value is only relevant in pre-DWARF 5 split-unit scenarios. */
ULONGEST gnu_ranges_base ()
{
for (unsigned i = 0; i < num_attrs; ++i)
if (attrs[i].name == DW_AT_GNU_ranges_base)
{
if (attrs[i].form_is_unsigned ())
return attrs[i].as_unsigned ();
complaint (_("ranges base attribute (offset %s) has wrong form"),
sect_offset_str (sect_off));
}
return 0;
}
/* Return the rnglists base of the compile unit, which, if exists, is stored
in the DW_AT_rnglists_base attribute. */
ULONGEST rnglists_base ()
{
for (unsigned i = 0; i < num_attrs; ++i)
if (attrs[i].name == DW_AT_rnglists_base)
{
if (attrs[i].form_is_unsigned ())
return attrs[i].as_unsigned ();
complaint (_("rnglists base attribute (offset %s) has wrong form"),
sect_offset_str (sect_off));
}
return 0;
}
/* DWARF-2 tag for this DIE. */
ENUM_BITFIELD(dwarf_tag) tag : 16;
/* Number of attributes */
unsigned char num_attrs;
/* True if we're presently building the full type name for the
type derived from this DIE. */
unsigned char building_fullname : 1;
/* True if this die is in process. PR 16581. */
unsigned char in_process : 1;
/* True if this DIE has children. */
unsigned char has_children : 1;
/* Abbrev number */
unsigned int abbrev;
/* Offset in .debug_info or .debug_types section. */
sect_offset sect_off;
/* The dies in a compilation unit form an n-ary tree. PARENT
points to this die's parent; CHILD points to the first child of
this node; and all the children of a given node are chained
together via their SIBLING fields. */
struct die_info *child; /* Its first child, if any. */
struct die_info *sibling; /* Its next sibling, if any. */
struct die_info *parent; /* Its parent, if any. */
/* An array of attributes, with NUM_ATTRS elements. There may be
zero, but it's not common and zero-sized arrays are not
sufficiently portable C. */
struct attribute attrs[1];
};
#endif /* GDB_DWARF2_DIE_H */