[PATCH 07/13] gdb/dwarf: read DW_AT_ranges value as unsigned in partial_die_info::read

Simon Marchi via Gdb-patches
2021-01-20T05:39:36Z

From: Simon Marchi <simon.marchi@efficios.com>

While writing a test for this series, I made a function
(DW_AT_subprogram) with a DW_AT_ranges attribute using the
DW_FORM_rnglistx form:

0x00000012: DW_TAG_subprogram
DW_AT_name [DW_FORM_string] ("foo")
DW_AT_ranges [DW_FORM_rnglistx] (indexed (0x0) rangelist = 0x00000036
[0x0000000000004000, 0x0000000000005000))

And strangely I couldn't print it:

(gdb) p foo
No symbol "foo" in current context.

This is because of the `attr.constant_value (0)` in the DW_AT_ranges
handling of partial_die_info::read. Since DW_FORM_rnglistx is not
recognized as a constant value by attribute::constant_value, the default
value (0) is returned. Down the line, this causes
dwarf2_rnglists_process to try read a range list at offset 0 in the
.debug_rnglists section, which is obviously wrong. In the end, no
symbol is created for foo because we didn't find an address range.

Use attr->as_unsigned instead. This is what is done for the equivalent
code in dwarf2_get_pc_bounds. With this, GDB processes the subprogram
DIE and we are able to print the function symbol:

(gdb) p foo
$1 = {void (void)} 0x4000

Note that I didn't see an actual compiler use DW_FORM_rnglistx for a
subprogram's range. However, in the binary attached to PR 26813, there
are some lexical blocks with it:

0x0000d34a: DW_TAG_lexical_block
DW_AT_ranges [DW_FORM_rnglistx] (indexed (0x2) rangelist = 0x000000d4
[0x0000000000005db1, 0x0000000000005e36)
[0x0000000000005e48, 0x0000000000005f3c)
[0x0000000000006045, 0x0000000000006053))

Their ranges are read incorrectly just like the ranges of the
subprograms. With this patch applied, they are read correctly.

gdb/ChangeLog:

* dwarf2/read.c (partial_die_info::read): Use
attribute::as_unsigned instead of attribute::constant_value
for DW_AT_ranges.

Change-Id: I285381a15588574058d934bbc88f06029e8a3bd1
---
gdb/dwarf2/read.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 9701d422a338..5277e2a09e3f 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -19805,7 +19805,7 @@ partial_die_info::read (const struct die_reader_specs *reader,
/* It would be nice to reuse dwarf2_get_pc_bounds here,
but that requires a full DIE, so instead we just
reimplement it. */
- unsigned int ranges_offset = (attr.constant_value (0)
+ unsigned int ranges_offset = (attr.as_unsigned ()
+ (need_ranges_base
? cu->ranges_base
: 0));
--
2.30.0