[committed 1/4] gprofng: Resolve DWARF 5 indexed string and address forms
Claudiu Zissulescu
claziss@gmail.com
Fri Nov 27 09:42:25 GMT 2026
Clang emits DW_FORM_strx* and DW_FORM_addrx* by default for DWARF 5.
gprofng used the strx index as a byte offset into .debug_str and kept
the addrx index as the address, so compilation unit names, directories
and every PC range were wrong. "gprofng display text -lines" then
showed "<Function: ..., instructions without line numbers>" and
"-source" failed with "Source location not recorded in experiment".
GCC does not use the indexed forms and was not affected.
This is a follow-up to PR 32110. That fix made DW_FORM_strx* stop
crashing, but it treated the index as an offset into .debug_str, which
yields wrong strings.
Read .debug_str_offsets and .debug_addr. The string and address
indices are resolved through DW_AT_str_offsets_base and
DW_AT_addr_base. Clang emits those two attributes after the ones that
use them, so queue the indexed attributes while reading a DIE and
resolve them once the whole DIE has been read. An index that cannot
be resolved gives NULL or 0, as before.
Also accept the addrx forms in Dwarf_addr and get_high_pc.
* src/Dwarf.h (Dwarf): Add debug_str_offsetsSec and debug_addrSec.
* src/Dwarf.cc (Dwarf::Dwarf): Load .debug_str_offsets and
.debug_addr.
* src/DwarfLib.h (DwrCU): Add str_offsets_base, addr_base,
get_indexed_string and get_indexed_addr.
* src/DwarfLib.cc (DwrCU::DwrCU): Initialize the new members.
(DwrCU::set_die): Record DW_AT_str_offsets_base and
DW_AT_addr_base. Keep only the raw index for DW_FORM_strx* and
DW_FORM_addrx*, and resolve it after the DIE has been read.
(DwrCU::get_indexed_string, DwrCU::get_indexed_addr): New.
(DwrCU::Dwarf_addr, DwrCU::get_high_pc): Handle DW_FORM_addrx*.
(Dwr_Tag::dump): Print DW_FORM_strx as a string.
---
gprofng/src/Dwarf.cc | 4 ++
gprofng/src/Dwarf.h | 2 +
gprofng/src/DwarfLib.cc | 106 +++++++++++++++++++++++++++++++++++++---
gprofng/src/DwarfLib.h | 4 ++
4 files changed, 110 insertions(+), 6 deletions(-)
diff --git a/gprofng/src/Dwarf.cc b/gprofng/src/Dwarf.cc
index a6f600c3d37..3cbb74edd66 100644
--- a/gprofng/src/Dwarf.cc
+++ b/gprofng/src/Dwarf.cc
@@ -371,6 +371,8 @@ Dwarf::Dwarf (Stabs *_stabs)
debug_alt_strSec = NULL;
debug_lineSec = NULL;
debug_line_strSec = NULL;
+ debug_str_offsetsSec = NULL;
+ debug_addrSec = NULL;
debug_rangesSec = NULL;
elf = stabs->openElf (true);
if (elf == NULL)
@@ -384,6 +386,8 @@ Dwarf::Dwarf (Stabs *_stabs)
debug_lineSec = dwrGetSec (NTXT (".debug_line"));
debug_rangesSec = dwrGetSec (NTXT (".debug_ranges"));
debug_line_strSec = dwrGetSec (".debug_line_str");
+ debug_str_offsetsSec = dwrGetSec (".debug_str_offsets");
+ debug_addrSec = dwrGetSec (".debug_addr");
debug_rnglists = NULL;
if (elf->gnu_debugalt_file)
debug_alt_strSec = elf->gnu_debugalt_file->get_dwr_section (".debug_str");
diff --git a/gprofng/src/Dwarf.h b/gprofng/src/Dwarf.h
index 0338217fefe..4644da8f777 100644
--- a/gprofng/src/Dwarf.h
+++ b/gprofng/src/Dwarf.h
@@ -81,6 +81,8 @@ public:
DwrSec *debug_alt_strSec;
DwrSec *debug_lineSec;
DwrSec *debug_line_strSec;
+ DwrSec *debug_str_offsetsSec;
+ DwrSec *debug_addrSec;
DwrSec *debug_rangesSec;
Elf *elf;
Stabs *stabs;
diff --git a/gprofng/src/DwarfLib.cc b/gprofng/src/DwarfLib.cc
index 4e6a0f61239..9a3bdcc119d 100644
--- a/gprofng/src/DwarfLib.cc
+++ b/gprofng/src/DwarfLib.cc
@@ -32,6 +32,7 @@
#include "Symbol.h"
#define NO_STMT_LIST ((uint64_t) -1)
+#define NO_BASE ((uint64_t) -1)
#define CASE_S(x) case x: s = (char *) #x; break
static char *
@@ -745,6 +746,7 @@ Dwr_Tag::dump ()
case DW_FORM_line_strp:
case DW_FORM_GNU_strp_alt:
case DW_FORM_strp_sup:
+ case DW_FORM_strx:
case DW_FORM_strx1:
case DW_FORM_strx2:
case DW_FORM_strx3:
@@ -1545,6 +1547,8 @@ DwrCU::DwrCU (Dwarf *_dwarf)
rng_list = NULL;
rng_list_inited = false;
base_address = 0;
+ str_offsets_base = NO_BASE;
+ addr_base = NO_BASE;
symbols = NULL;
symbols_sorted_by_name = NULL;
cu_offset = dwarf->debug_infoSec->offset;
@@ -1718,6 +1722,11 @@ DwrCU::set_die (Dwarf_Die die)
dwrTag.hasChild = abbTbl->hasChild;
dwrTag.firstAttribute = abbTbl->firstAtForm;
dwrTag.lastAttribute = abbTbl->lastAtForm;
+ // Indexed forms (DW_FORM_strx*, DW_FORM_addrx*) hold an index that can only be
+ // resolved once DW_AT_str_offsets_base / DW_AT_addr_base are known. Clang emits
+ // them AFTER the attributes that use them, so resolve after the whole DIE is read.
+ struct { int k; int form; } pend[64];
+ int npend = 0;
for (int k = abbTbl->firstAtForm; k < abbTbl->lastAtForm; k++)
{
Dwr_Attr *atf = abbrevAtForm->get (k);
@@ -1828,23 +1837,18 @@ DwrCU::set_die (Dwarf_Die die)
break;
case DW_FORM_strx:
atf->u.offset = debug_infoSec->GetULEB128 ();
- atf->u.str = get_string (dwarf->debug_strSec, atf->u.offset);
break;
case DW_FORM_strx1:
atf->u.offset = debug_infoSec->Get_8 ();
- atf->u.str = get_string (dwarf->debug_strSec, atf->u.offset);
break;
case DW_FORM_strx2:
atf->u.offset = debug_infoSec->Get_16 ();
- atf->u.str = get_string (dwarf->debug_strSec, atf->u.offset);
break;
case DW_FORM_strx3:
atf->u.offset = debug_infoSec->Get_24 ();
- atf->u.str = get_string (dwarf->debug_strSec, atf->u.offset);
break;
case DW_FORM_strx4:
atf->u.offset = debug_infoSec->Get_32 ();
- atf->u.str = get_string (dwarf->debug_strSec, atf->u.offset);
break;
case DW_FORM_addrx1:
atf->u.offset = debug_infoSec->Get_8 ();
@@ -1885,6 +1889,54 @@ DwrCU::set_die (Dwarf_Die die)
atf->len = 0;
break;
}
+ switch (atf->at_name)
+ {
+ case DW_AT_str_offsets_base:
+ str_offsets_base = atf->u.offset;
+ break;
+ case DW_AT_addr_base:
+ addr_base = atf->u.offset;
+ break;
+ }
+ switch (at_form)
+ {
+ case DW_FORM_strx:
+ case DW_FORM_strx1:
+ case DW_FORM_strx2:
+ case DW_FORM_strx3:
+ case DW_FORM_strx4:
+ case DW_FORM_addrx:
+ case DW_FORM_addrx1:
+ case DW_FORM_addrx2:
+ case DW_FORM_addrx3:
+ case DW_FORM_addrx4:
+ if (npend < (int) (sizeof (pend) / sizeof (pend[0])))
+ {
+ pend[npend].k = k;
+ pend[npend].form = at_form;
+ npend++;
+ }
+ else
+ atf->u.offset = 0; // too many indexed attributes: leave unresolved
+ break;
+ }
+ }
+ for (int i = 0; i < npend; i++)
+ {
+ Dwr_Attr *atf = abbrevAtForm->get (pend[i].k);
+ switch (pend[i].form)
+ {
+ case DW_FORM_strx:
+ case DW_FORM_strx1:
+ case DW_FORM_strx2:
+ case DW_FORM_strx3:
+ case DW_FORM_strx4:
+ atf->u.str = get_indexed_string (atf->u.offset); // u.offset is the index
+ break;
+ default: // DW_FORM_addrx*
+ atf->u.offset = get_indexed_addr (atf->u.offset);
+ break;
+ }
}
dwrTag.dump ();
return DW_DLV_OK;
@@ -2204,6 +2256,38 @@ DwrCU::Dwarf_string (Dwarf_Half attr)
return dwrAttr ? dwrAttr->u.str : NULL;
}
+// DW_FORM_strx*: index into .debug_str_offsets (relative to DW_AT_str_offsets_base),
+// whose entry is an offset into .debug_str. Returns NULL if it cannot be resolved.
+char *
+DwrCU::get_indexed_string (uint64_t index)
+{
+ DwrSec *sec = dwarf->debug_str_offsetsSec;
+ if (sec == NULL || str_offsets_base == NO_BASE)
+ return NULL;
+ unsigned sz = debug_infoSec->fmt64 ? 8 : 4;
+ uint64_t pos = str_offsets_base + index * sz;
+ if (index > sec->sizeSec || pos + sz > sec->sizeSec)
+ return NULL;
+ sec->offset = pos;
+ uint64_t off = (sz == 8) ? sec->Get_64 () : sec->Get_32 ();
+ return get_string (dwarf->debug_strSec, off);
+}
+
+// DW_FORM_addrx*: index into .debug_addr (relative to DW_AT_addr_base).
+// Returns 0 if it cannot be resolved.
+uint64_t
+DwrCU::get_indexed_addr (uint64_t index)
+{
+ DwrSec *sec = dwarf->debug_addrSec;
+ if (sec == NULL || addr_base == NO_BASE || (address_size != 4 && address_size != 8))
+ return 0;
+ uint64_t pos = addr_base + index * address_size;
+ if (index > sec->sizeSec || pos + address_size > sec->sizeSec)
+ return 0;
+ sec->offset = pos;
+ return (address_size == 4) ? sec->Get_32 () : sec->Get_64 ();
+}
+
uint64_t
DwrCU::get_high_pc (uint64_t low_pc)
{
@@ -2212,6 +2296,11 @@ DwrCU::get_high_pc (uint64_t low_pc)
switch (dwrAttr->at_form)
{
case DW_FORM_addr:
+ case DW_FORM_addrx:
+ case DW_FORM_addrx1:
+ case DW_FORM_addrx2:
+ case DW_FORM_addrx3:
+ case DW_FORM_addrx4:
return dwrAttr->u.offset;
default:
return dwrAttr->u.offset + low_pc;
@@ -2227,7 +2316,12 @@ DwrCU::Dwarf_addr (Dwarf_Half attr)
switch (dwrAttr->at_form)
{
case DW_FORM_addr:
- return dwrAttr->u.offset;
+ case DW_FORM_addrx:
+ case DW_FORM_addrx1:
+ case DW_FORM_addrx2:
+ case DW_FORM_addrx3:
+ case DW_FORM_addrx4:
+ return dwrAttr->u.offset; // addrx*: already resolved by set_die
}
return 0;
}
diff --git a/gprofng/src/DwarfLib.h b/gprofng/src/DwarfLib.h
index 8aa11b076e2..95e2dca7b8d 100644
--- a/gprofng/src/DwarfLib.h
+++ b/gprofng/src/DwarfLib.h
@@ -343,6 +343,8 @@ private:
int64_t Dwarf_ref (Dwarf_Half attr);
DwrSec *Dwarf_block (Dwarf_Half attr);
Dwarf_Addr Dwarf_addr (Dwarf_Half attr);
+ char *get_indexed_string (uint64_t index);
+ uint64_t get_indexed_addr (uint64_t index);
Dwarf_Addr Dwarf_location (Dwarf_Attribute attr);
Sp_lang_code Dwarf_lang ();
@@ -363,6 +365,8 @@ private:
Dwr_rng_entry *rng_list; // entry in the .debug_rnglists section
bool rng_list_inited;
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)
};
#endif /* _DWARFLIB_H_ */
--
2.56.0
More information about the Binutils
mailing list