diff --git a/librz/analysis/dwarf_process.c b/librz/analysis/dwarf_process.c index 0b0fcfc7132..8bdc197314a 100644 --- a/librz/analysis/dwarf_process.c +++ b/librz/analysis/dwarf_process.c @@ -517,11 +517,6 @@ static const char *map_dwarf_reg_to_arm64(ut32 reg_num) { #include "hexagon_dwarf_reg_num_table.inc" -static const char *map_dwarf_register_dummy(ut32 reg_num) { - static char buf[32]; - return rz_strf(buf, "reg%u", reg_num); -} - /** * \brief Returns a function that maps a DWARF register number to a register name * \param arch The architecture name @@ -549,7 +544,7 @@ static DWARF_RegisterMapping dwarf_register_mapping_query(RZ_NONNULL char *arch, return map_dwarf_reg_to_hexagon_reg; } RZ_LOG_ERROR("No DWARF register mapping function defined for %s %d bits\n", arch, bits); - return map_dwarf_register_dummy; + return NULL; } static void variable_fini(RzAnalysisDwarfVariable *var) { @@ -1897,6 +1892,17 @@ static RzBinDwarfLocation *location_by_biggest_range(const RzBinDwarfLocList *lo return biggest_range_loc; } +static char *get_dwarf_register_mapping(const RzAnalysis *a, ut64 reg) { + const char *reg_mapped = NULL; + if (a->debug_info && a->debug_info->dwarf_register_mapping) { + reg_mapped = a->debug_info->dwarf_register_mapping(reg); + } + if (!reg_mapped) { + return rz_str_newf("reg%" PFMT64u, reg); + } + return rz_str_new(reg_mapped); +} + static bool RzBinDwarfLocation_as_RzAnalysisVarStorage( RzAnalysis *a, RzAnalysisFunction *f, RzAnalysisDwarfVariable *dw_var, RzBinDwarfLocation *loc, @@ -1905,12 +1911,17 @@ static bool RzBinDwarfLocation_as_RzAnalysisVarStorage( var->origin.dw_var = dw_var; switch (loc->kind) { case RzBinDwarfLocationKind_REGISTER: { - rz_analysis_var_storage_init_reg(storage, a->debug_info->dwarf_register_mapping(loc->register_number)); + char *reg_mapped = get_dwarf_register_mapping(a, loc->register_number); + rz_analysis_var_storage_init_reg(storage, reg_mapped); + free(reg_mapped); break; } case RzBinDwarfLocationKind_REGISTER_OFFSET: { // Convert some register offset to stack offset - if (fixup_regoff_to_stackoff(a, f, dw_var, a->debug_info->dwarf_register_mapping(loc->register_number), var)) { + char *reg_mapped = get_dwarf_register_mapping(a, loc->register_number); + bool fixed = fixup_regoff_to_stackoff(a, f, dw_var, reg_mapped, var); + free(reg_mapped); + if (fixed) { break; } break; diff --git a/librz/bin/dwarf/op.c b/librz/bin/dwarf/op.c index 8ca43be952e..1d906498bd8 100644 --- a/librz/bin/dwarf/op.c +++ b/librz/bin/dwarf/op.c @@ -1736,6 +1736,17 @@ RZ_API void rz_bin_dwarf_location_composite_dump( rz_strbuf_appendf(sb, "%s]", rz_str_get(opt->composite_indent)); } +static char *dump_dwarf_register_mapping(const RzBinDWARFDumpOption *opt, ut64 reg) { + const char *reg_mapped = NULL; + if (opt && opt->dwarf_register_mapping) { + reg_mapped = opt->dwarf_register_mapping(reg); + } + if (!reg_mapped) { + return rz_str_newf("reg%" PFMT64u, reg); + } + return rz_str_new(reg_mapped); +} + RZ_API void rz_bin_dwarf_location_dump( RZ_BORROW RZ_NONNULL const RzBinDwarfLocation *loc, RZ_BORROW RZ_NONNULL RzStrBuf *sb, @@ -1748,21 +1759,18 @@ RZ_API void rz_bin_dwarf_location_dump( case RzBinDwarfLocationKind_DECODE_ERROR: rz_strbuf_append(sb, ""); break; - case RzBinDwarfLocationKind_REGISTER: - if (opt->dwarf_register_mapping) { - rz_strbuf_append(sb, opt->dwarf_register_mapping(loc->register_number)); - } else { - rz_strbuf_appendf(sb, "reg%" PFMT64d, loc->register_number); - } + case RzBinDwarfLocationKind_REGISTER: { + char *reg_mapped = dump_dwarf_register_mapping(opt, loc->register_number); + rz_strbuf_append(sb, reg_mapped); + free(reg_mapped); break; - case RzBinDwarfLocationKind_REGISTER_OFFSET: - if (opt->dwarf_register_mapping) { - rz_strbuf_append(sb, opt->dwarf_register_mapping(loc->register_number)); - } else { - rz_strbuf_appendf(sb, "reg%" PFMT64d, loc->register_number); - } - rz_strbuf_appendf(sb, "%+" PFMT64d, loc->offset); + } + case RzBinDwarfLocationKind_REGISTER_OFFSET: { + char *reg_mapped = dump_dwarf_register_mapping(opt, loc->register_number); + rz_strbuf_appendf(sb, "%s%+" PFMT64d, reg_mapped, loc->offset); + free(reg_mapped); break; + } case RzBinDwarfLocationKind_ADDRESS: rz_strbuf_appendf(sb, "address 0x%" PFMT64x, loc->address); break;