[committed 3/4] gprofng: Support DWARF 5 range lists with an offsets table
Claudiu Zissulescu
claziss@gmail.com
Fri Nov 27 09:42:27 GMT 2026
Clang emits DW_AT_ranges as DW_FORM_rnglistx, with DW_AT_rnglists_base
and an offsets table after the .debug_rnglists header, for compilation
units whose code is not contiguous, for example with
-ffunction-sections, and for AMDGPU code objects. gprofng did not
handle this: it parsed the offsets table as range list entries,
stored the DW_FORM_rnglistx index as if it were a section offset, and
dropped the DW_RLE_*x* entries. The compilation unit then had no
ranges, and "gprofng display src -source" failed with "Source location
not recorded in experiment".
Skip the offsets table when reading .debug_rnglists and remember its
contents. Resolve DW_FORM_rnglistx through DW_AT_rnglists_base and
the offsets table once the whole DIE has been read, because Clang
emits DW_AT_rnglists_base after DW_AT_ranges. Read the operands of
DW_RLE_startx_endx, DW_RLE_startx_length and DW_RLE_base_addressx as
indexes into .debug_addr and resolve them through DW_AT_addr_base. An
offset pair that follows DW_RLE_base_addressx is relative to the
indexed base. A list that starts with a base address entry is now
found as well. Lists that do not use these forms are read as before.
* src/DwarfLib.h (ExtRange): Add type and base_idx.
(Dwr_rng_entry): Add table_offset and entry_offsets.
(DwrCU): Add rnglists_base and get_rnglist_offset.
* src/Dwarf.cc (Dwarf::get_debug_rnglists): Read the offsets
table. Handle DW_RLE_base_addressx, DW_RLE_startx_endx and
DW_RLE_startx_length, and an offset pair after an indexed base.
* src/DwarfLib.cc (DwrCU::DwrCU): Initialize rnglists_base.
(DwrCU::set_die): Record DW_AT_rnglists_base and resolve
DW_FORM_rnglistx after the DIE has been read.
(DwrCU::get_ranges): Find a list that does not start with a
range entry. Resolve the indexed range list entries.
(DwrCU::get_rnglist_offset): New.
(Dwr_rng_entry::Dwr_rng_entry, Dwr_rng_entry::~Dwr_rng_entry):
Allocate and free entry_offsets.
* testsuite/gprofng.display/dwarf5.exp: Add -ffunction-sections
cases for GCC and Clang.
---
gprofng/src/Dwarf.cc | 59 ++++++++++----
gprofng/src/DwarfLib.cc | 83 ++++++++++++++++++++
gprofng/src/DwarfLib.h | 18 +++++
gprofng/testsuite/gprofng.display/dwarf5.exp | 9 ++-
4 files changed, 153 insertions(+), 16 deletions(-)
diff --git a/gprofng/src/Dwarf.cc b/gprofng/src/Dwarf.cc
index 3cbb74edd66..1e373da276c 100644
--- a/gprofng/src/Dwarf.cc
+++ b/gprofng/src/Dwarf.cc
@@ -1108,6 +1108,8 @@ Dwarf::get_debug_rnglists ()
while (debug_rnglistsSec->offset < debug_rnglistsSec->sizeSec)
{
uint64_t base_address = 0;
+ bool base_is_index = false;
+ uint64_t base_idx = 0;
uint64_t length = debug_rnglistsSec->ReadLength ();
Dwr_rng_entry *rng = new Dwr_rng_entry ();
debug_rnglists->append (rng);
@@ -1118,6 +1120,18 @@ Dwarf::get_debug_rnglists ()
rng->address_size = debug_rnglistsSec->Get_8 ();
rng->segment_selector_size = debug_rnglistsSec->Get_8 ();
rng->offset_entry_count = debug_rnglistsSec->Get_32 ();
+ // Clang emits an offsets table right after the header when it uses
+ // DW_FORM_rnglistx. Record where it starts (that is what
+ // DW_AT_rnglists_base points at) and skip over it before parsing the
+ // range lists; indexes into the table select the list at
+ // table_offset + entry_offsets[i].
+ rng->table_offset = debug_rnglistsSec->offset;
+ unsigned off_sz = rng->fmt64 ? 8 : 4;
+ for (int i = 0;
+ i < rng->offset_entry_count
+ && debug_rnglistsSec->offset + off_sz <= debug_rnglistsSec->size;
+ i++)
+ rng->entry_offsets->append (debug_rnglistsSec->GetLong ());
while (debug_rnglistsSec->offset < debug_rnglistsSec->size)
{
uint64_t off = debug_rnglistsSec->offset;
@@ -1132,6 +1146,7 @@ Dwarf::get_debug_rnglists ()
break;
case DW_RLE_base_address:
base_address = debug_rnglistsSec->GetADDR ();
+ base_is_index = false;
low_pc = base_address;
high_pc = 0;
continue;
@@ -1140,6 +1155,15 @@ Dwarf::get_debug_rnglists ()
high_pc = low_pc + debug_rnglistsSec->GetULEB128 ();
break;
case DW_RLE_offset_pair:
+ // An offset_pair following a DW_RLE_base_addressx is relative
+ // to the indexed base; it is resolved in DwrCU::get_ranges ()
+ // against the CU's DW_AT_addr_base.
+ if (base_is_index)
+ {
+ low_pc = debug_rnglistsSec->GetULEB128 ();
+ high_pc = debug_rnglistsSec->GetULEB128 ();
+ break;
+ }
low_pc = base_address + debug_rnglistsSec->GetULEB128 ();
high_pc = base_address + debug_rnglistsSec->GetULEB128 ();
break;
@@ -1148,22 +1172,17 @@ Dwarf::get_debug_rnglists ()
high_pc = debug_rnglistsSec->GetADDR ();
break;
case DW_RLE_base_addressx:
- base_address = debug_rnglistsSec->GetULEB128 ();
- low_pc = base_address;
- high_pc = 0;
+ base_is_index = true;
+ base_idx = debug_rnglistsSec->GetULEB128 ();
continue;
-
- // TODO x-variants need .debug_addr support used for split-dwarf
case DW_RLE_startx_endx:
- low_pc = debug_rnglistsSec->GetRef ();
- high_pc = debug_rnglistsSec->GetRef ();
- Dprintf (1, "DW_RLE_startx_endx is not implemented\n");
- continue;
+ low_pc = debug_rnglistsSec->GetULEB128 ();
+ high_pc = debug_rnglistsSec->GetULEB128 ();
+ break;
case DW_RLE_startx_length:
- low_pc = debug_rnglistsSec->GetRef ();
- high_pc = low_pc + debug_rnglistsSec->GetULEB128 ();
- Dprintf (1, "DW_RLE_startx_length is not implemented\n");
- continue;
+ low_pc = debug_rnglistsSec->GetULEB128 ();
+ high_pc = debug_rnglistsSec->GetULEB128 ();
+ break;
default:
Dprintf (1, "Unknown tag DW_RLE: %d, offset=0x%llx\n", re,
(long long) off);
@@ -1173,7 +1192,19 @@ Dwarf::get_debug_rnglists ()
Dprintf (DUMP_DWARFLIB, "0x%08llx %d-%-20s [0x%08llx - 0x%08llx)\n",
(long long) off, re, Dwr_rng_entry::rng_entry2str (re),
(long long) low_pc, (long long) high_pc);
- rng->ranges->append (new ExtRange (off, low_pc, high_pc));
+ ExtRange *er = new ExtRange (off, low_pc, high_pc);
+ if (re == DW_RLE_startx_endx || re == DW_RLE_startx_length
+ || (re == DW_RLE_offset_pair && base_is_index))
+ {
+ // Operands are indexes into .debug_addr, resolved for each
+ // CU against its DW_AT_addr_base in DwrCU::get_ranges ().
+ // An offset_pair after a base_addressx carries the base
+ // index so the base address can be added later.
+ er->type = re;
+ if (re == DW_RLE_offset_pair)
+ er->base_idx = base_idx;
+ }
+ rng->ranges->append (er);
}
debug_rnglistsSec->size = debug_rnglistsSec->sizeSec;
debug_rnglistsSec->offset = length;
diff --git a/gprofng/src/DwarfLib.cc b/gprofng/src/DwarfLib.cc
index 9a3bdcc119d..a767b34230b 100644
--- a/gprofng/src/DwarfLib.cc
+++ b/gprofng/src/DwarfLib.cc
@@ -1549,6 +1549,7 @@ DwrCU::DwrCU (Dwarf *_dwarf)
base_address = 0;
str_offsets_base = NO_BASE;
addr_base = NO_BASE;
+ rnglists_base = NO_BASE;
symbols = NULL;
symbols_sorted_by_name = NULL;
cu_offset = dwarf->debug_infoSec->offset;
@@ -1897,6 +1898,9 @@ DwrCU::set_die (Dwarf_Die die)
case DW_AT_addr_base:
addr_base = atf->u.offset;
break;
+ case DW_AT_rnglists_base:
+ rnglists_base = atf->u.offset;
+ break;
}
switch (at_form)
{
@@ -1910,6 +1914,7 @@ DwrCU::set_die (Dwarf_Die die)
case DW_FORM_addrx2:
case DW_FORM_addrx3:
case DW_FORM_addrx4:
+ case DW_FORM_rnglistx:
if (npend < (int) (sizeof (pend) / sizeof (pend[0])))
{
pend[npend].k = k;
@@ -1933,6 +1938,13 @@ DwrCU::set_die (Dwarf_Die die)
case DW_FORM_strx4:
atf->u.str = get_indexed_string (atf->u.offset); // u.offset is the index
break;
+ case DW_FORM_rnglistx:
+ // The index selects an entry of the offsets table; the range
+ // list lives at rnglists_base + offsets[index]. If it cannot
+ // be resolved leave u.offset unchanged so that get_ranges ()
+ // fails gracefully with a diagnostic.
+ atf->u.offset = get_rnglist_offset (atf->u.offset);
+ break;
default: // DW_FORM_addrx*
atf->u.offset = get_indexed_addr (atf->u.offset);
break;
@@ -2078,6 +2090,24 @@ DwrCU::get_ranges ()
return NULL;
int ind = rng_list->ranges->bisearch (0, -1, &offset, cmp_ExtRange);
if (ind == -1)
+ {
+ // A list whose first entry is DW_RLE_base_address /
+ // DW_RLE_base_addressx has no ExtRange at the list start
+ // offset, so the exact search fails: use a lower-bound
+ // search for the first entry whose offset is >= the list
+ // start. The entries are in increasing offset order.
+ long lo = 0, hi = VecSize (rng_list->ranges), mid;
+ for (mid = (lo + hi) / 2; lo < hi; mid = (lo + hi) / 2)
+ {
+ ExtRange *er = rng_list->ranges->get (mid);
+ if (er->offset < offset)
+ lo = mid + 1;
+ else
+ hi = mid;
+ }
+ ind = lo;
+ }
+ if (ind == (long) VecSize (rng_list->ranges))
{
Dprintf (1, "Cannot find rnglist. DW_AT_ranges=0x%llx\n",
(long long) offset);
@@ -2087,6 +2117,32 @@ DwrCU::get_ranges ()
for (long i = ind, sz = VecSize (rng_list->ranges); i < sz; i++)
{
ExtRange *r = rng_list->ranges->get (i);
+ if (r->type != 0)
+ {
+ // Operands are indexes into .debug_addr, resolved
+ // against the CU's DW_AT_addr_base; an index that
+ // cannot be resolved yields 0 and the entry is
+ // skipped. base_address is NOT added for these.
+ uint64_t low, high;
+ if (r->type == DW_RLE_offset_pair)
+ {
+ uint64_t base = get_indexed_addr (r->base_idx);
+ if (base == 0)
+ continue;
+ low = base + r->low;
+ high = base + r->high;
+ }
+ else
+ {
+ low = get_indexed_addr (r->low);
+ if (low == 0)
+ continue;
+ high = (r->type == DW_RLE_startx_length)
+ ? low + r->high : get_indexed_addr (r->high);
+ }
+ ranges->append (new Range (low, high));
+ continue;
+ }
if (r->high == 0)
break;
ranges->append (new Range (r->low + base_address,
@@ -2288,6 +2344,30 @@ DwrCU::get_indexed_addr (uint64_t index)
return (address_size == 4) ? sec->Get_32 () : sec->Get_64 ();
}
+// DW_FORM_rnglistx: index into the offsets table of the matching
+// .debug_rnglists contribution. Returns the section offset of the range
+// list, or the raw index (so that get_ranges () reports it as
+// unresolvable) when DW_AT_rnglists_base is missing or the index is out
+// of range.
+uint64_t
+DwrCU::get_rnglist_offset (uint64_t index)
+{
+ if (rnglists_base == NO_BASE)
+ return index;
+ Vector <Dwr_rng_entry *> *rng_entrys = dwarf->get_debug_rnglists ();
+ for (long i = 0; i < VecSize (rng_entrys); i++)
+ {
+ Dwr_rng_entry *rng = rng_entrys->get (i);
+ if (rng->table_offset == rnglists_base)
+ {
+ if (index >= (uint64_t) VecSize (rng->entry_offsets))
+ return index;
+ return rnglists_base + rng->entry_offsets->get (index);
+ }
+ }
+ return index;
+}
+
uint64_t
DwrCU::get_high_pc (uint64_t low_pc)
{
@@ -2599,11 +2679,14 @@ DwrInlinedSubr::dump ()
Dwr_rng_entry::Dwr_rng_entry ()
{
ranges = new Vector <ExtRange *>();
+ entry_offsets = new Vector <uint64_t>();
+ table_offset = 0;
}
Dwr_rng_entry::~Dwr_rng_entry ()
{
delete ranges;
+ delete entry_offsets;
}
void
diff --git a/gprofng/src/DwarfLib.h b/gprofng/src/DwarfLib.h
index 95e2dca7b8d..def82bd4c32 100644
--- a/gprofng/src/DwarfLib.h
+++ b/gprofng/src/DwarfLib.h
@@ -262,11 +262,21 @@ public:
offset = _off;
low = _low;
high = _high;
+ type = 0;
+ base_idx = 0;
};
uint64_t offset;
uint64_t low;
uint64_t high;
+ // 0 = plain range (low/high are absolute, resolved with the CU base
+ // address); otherwise the real DW_RLE_* kind of an entry resolved in
+ // DwrCU::get_ranges (): DW_RLE_startx_endx / DW_RLE_startx_length
+ // operands are indexes into .debug_addr (resolved via the CU's
+ // DW_AT_addr_base), and an offset_pair that follows a
+ // DW_RLE_base_addressx is relative to get_indexed_addr (base_idx).
+ int type;
+ uint64_t base_idx;
};
class Dwr_rng_entry
@@ -281,6 +291,12 @@ public:
Dwarf_Small address_size;
Dwarf_Small segment_selector_size;
int offset_entry_count;
+ // Section offset of the first entry of the offsets table, i.e. where
+ // DW_AT_rnglists_base points; entry_offsets holds the table contents.
+ // A DW_FORM_rnglistx index i selects the list at table_offset +
+ // entry_offsets[i].
+ uint64_t table_offset;
+ Vector <uint64_t> *entry_offsets;
uint64_t length;
uint64_t offset;
bool fmt64;
@@ -367,6 +383,8 @@ private:
uint64_t base_address; // low_pc in compile_unit
uint64_t str_offsets_base; // DW_AT_str_offsets_base (.debug_str_offsets)
uint64_t addr_base; // DW_AT_addr_base (.debug_addr)
+ uint64_t rnglists_base; // DW_AT_rnglists_base (.debug_rnglists)
+ uint64_t get_rnglist_offset (uint64_t index);
};
#endif /* _DWARFLIB_H_ */
diff --git a/gprofng/testsuite/gprofng.display/dwarf5.exp b/gprofng/testsuite/gprofng.display/dwarf5.exp
index 7d3e72369d4..e98c99433c5 100644
--- a/gprofng/testsuite/gprofng.display/dwarf5.exp
+++ b/gprofng/testsuite/gprofng.display/dwarf5.exp
@@ -32,11 +32,11 @@ run_native_host_cmd "cp $srcdir/lib/dwarf5test.c $tdir/t.c"
# Build t.c with COMPILER and -gdwarf-5, profile it, and check that the
# hot function has a line number and that its source file can be found.
-proc check_dwarf5 { name compiler } {
+proc check_dwarf5 { name compiler {flags ""} } {
global gprofng tdir
set output [run_native_host_cmd "cd $tdir && \
- $compiler -g -O0 -gdwarf-5 t.c -o $name.exe"]
+ $compiler -g -O0 -gdwarf-5 $flags t.c -o $name.exe"]
if { [lindex $output 0] != 0 } then {
send_log "$compiler -gdwarf-5 failed: [lindex $output 1]\n"
unsupported "dwarf5 $name"
@@ -76,11 +76,16 @@ proc check_dwarf5 { name compiler } {
# GCC is the control: DW_FORM_addr/line_strp, worked before.
check_dwarf5 "gcc" $CC
+check_dwarf5 "gcc-ffs" $CC "-ffunction-sections"
# Clang is the case that needs .debug_str_offsets and .debug_addr.
+# With -ffunction-sections it also emits DW_FORM_rnglistx and a
+# .debug_rnglists offsets table.
set output [run_native_host_cmd "command -v clang"]
if { [lindex $output 0] == 0 } then {
check_dwarf5 "clang" "clang"
+ check_dwarf5 "clang-ffs" "clang" "-ffunction-sections"
} else {
unsupported "dwarf5 clang"
+ unsupported "dwarf5 clang-ffs"
}
--
2.56.0
More information about the Binutils
mailing list