Skip to content

Commit

Permalink
Remove static buffer from dwarf code.
Browse files Browse the repository at this point in the history
  • Loading branch information
wargio committed Jan 17, 2024
1 parent 0b54b57 commit 060da64
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
27 changes: 19 additions & 8 deletions librz/analysis/dwarf_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down
34 changes: 21 additions & 13 deletions librz/bin/dwarf/op.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -1748,21 +1759,18 @@ RZ_API void rz_bin_dwarf_location_dump(
case RzBinDwarfLocationKind_DECODE_ERROR:
rz_strbuf_append(sb, "<decoding error>");
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;
Expand Down

0 comments on commit 060da64

Please sign in to comment.