add function to get global variable address

This commit is contained in:
Gabriel Ionita 2025-10-24 16:10:00 +01:00
parent 3f4396689e
commit 95c179b107
Signed by: gabi
SSH Key Fingerprint: SHA256:mrbYmB/SGtDvT3etRoS6pDrMYWxR0/B5GSF6rR3qhhc

View File

@ -56,3 +56,62 @@ bool Symbol::is_integer_type(Dwarf_Debug dbg, Dwarf_Die die) {
dwarf_dealloc(dbg, type_die, DW_DLA_DIE); dwarf_dealloc(dbg, type_die, DW_DLA_DIE);
return is_integer; return is_integer;
} }
Dwarf_Addr Symbol::get_global_var_addr(Dwarf_Debug dbg, Dwarf_Die die) {
Dwarf_Error err;
Dwarf_Attribute loc_attr;
Dwarf_Addr addr = 0;
// Get the location attribute
if (dwarf_attr(die, DW_AT_location, &loc_attr, &err) != DW_DLV_OK)
return 0;
// Get location list
Dwarf_Loc_Head_c loclist_head;
Dwarf_Unsigned locentry_count;
if (dwarf_get_loclist_c(loc_attr, &loclist_head, &locentry_count, &err) == DW_DLV_OK) {
// Iterate through location entries
for (Dwarf_Unsigned i = 0; i < locentry_count; i++) {
Dwarf_Small loclist_lkind;
Dwarf_Small lle_value;
Dwarf_Unsigned rawval1, rawval2;
Dwarf_Bool debug_addr_unavailable;
Dwarf_Addr lopc, hipc;
Dwarf_Unsigned loclist_expr_op_count;
Dwarf_Locdesc_c locdesc_entry;
Dwarf_Unsigned expression_offset;
Dwarf_Unsigned locdesc_offset;
if (dwarf_get_locdesc_entry_d(loclist_head, i, &lle_value, &rawval1, &rawval2,
&debug_addr_unavailable, &lopc, &hipc,
&loclist_expr_op_count, &locdesc_entry,
&loclist_lkind, &expression_offset, &locdesc_offset,
&err) == DW_DLV_OK) {
// Check for simple address location
for (Dwarf_Unsigned j = 0; j < loclist_expr_op_count; j++) {
Dwarf_Small op;
Dwarf_Unsigned opd1, opd2, opd3;
Dwarf_Unsigned offsetforbranch;
if (dwarf_get_location_op_value_c(locdesc_entry, j, &op, &opd1, &opd2, &opd3,
&offsetforbranch, &err) == DW_DLV_OK) {
if (op == DW_OP_addr) {
addr = opd1;
break;
}
}
}
}
if (addr != 0)
break;
}
dwarf_dealloc_loc_head_c(loclist_head);
}
dwarf_dealloc(dbg, loc_attr, DW_DLA_ATTR);
return addr;
}