Skip to content

Commit

Permalink
Split bin_dwarf in Setting and Printing
Browse files Browse the repository at this point in the history
  • Loading branch information
thestr4ng3r committed Mar 25, 2021
1 parent 653ca25 commit 132dd38
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 23 deletions.
65 changes: 42 additions & 23 deletions librz/core/cbin.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
static RZ_NULLABLE RZ_BORROW const RzList *core_bin_strings(RzCore *r, RzBinFile *file);
static void _print_strings(RzCore *r, const RzList *list, PJ *pj, int mode, int va);
static bool bin_raw_strings(RzCore *r, PJ *pj, int mode, int va);
static bool bin_dwarf(RzCore *core, RzBinFile *binfile, PJ *pj, int mode);
static int bin_entry(RzCore *r, PJ *pj, int mode, ut64 laddr, int va, bool inifin);
static int bin_sections(RzCore *r, PJ *pj, int mode, ut64 laddr, int va, ut64 at, const char *name, const char *chksum, bool print_segments);
static int bin_map_sections_to_segments(RzBin *bin, PJ *pj, int mode);
Expand Down Expand Up @@ -378,7 +377,7 @@ RZ_API int rz_core_bin_apply_all_info(RzCore *r, RzBinFile *binfile) {
rz_core_bin_apply_strings(r, binfile);
rz_core_bin_apply_config(r, binfile);
rz_core_bin_apply_main(r, binfile, va);
bin_dwarf(r, binfile, NULL, RZ_MODE_SET);
rz_core_bin_apply_dwarf(r, binfile);
bin_entry(r, NULL, RZ_MODE_SET, loadaddr, va, false);
bin_sections(r, NULL, RZ_MODE_SET, loadaddr, va, UT64_MAX, NULL, NULL, false);
bin_sections(r, NULL, RZ_MODE_SET, loadaddr, va, UT64_MAX, NULL, NULL, true);
Expand Down Expand Up @@ -510,6 +509,40 @@ RZ_API bool rz_core_bin_apply_main(RzCore *r, RzBinFile *binfile, bool va) {
return true;
}

RZ_API bool rz_core_bin_apply_dwarf(RzCore *core, RzBinFile *binfile) {
rz_return_val_if_fail(core && binfile, false);
if (!rz_config_get_i(core->config, "bin.dbginfo") || !binfile->o) {
return false;
}
RzBinObject *o = binfile->o;
const RzBinSourceLineInfo *li = NULL;
RzBinDwarfDebugAbbrev *da = rz_bin_dwarf_parse_abbrev(binfile);
RzBinDwarfDebugInfo *info = da ? rz_bin_dwarf_parse_info(binfile, da) : NULL;
HtUP /*<offset, List *<LocListEntry>*/ *loc_table = rz_bin_dwarf_parse_loc(binfile, core->analysis->bits / 8);
if (info) {
RzAnalysisDwarfContext ctx = {
.info = info,
.loc = loc_table
};
rz_analysis_dwarf_process_info(core->analysis, &ctx);
}
if (loc_table) {
rz_bin_dwarf_loc_free(loc_table);
}
RzBinDwarfLineInfo *lines = rz_bin_dwarf_parse_line(binfile, info, RZ_BIN_DWARF_LINE_INFO_MASK_LINES);
rz_bin_dwarf_debug_info_free(info);
if (lines) {
// move all produced rows line info out (TODO: bin loading should do that)
li = o->lines = lines->lines;
lines->lines = NULL;
}
rz_bin_dwarf_debug_abbrev_free(da);
if (!li) {
return false;
}
return true;
}

RZ_API int rz_core_bin_set_cur(RzCore *core, RzBinFile *binfile) {
if (!core->bin) {
return false;
Expand Down Expand Up @@ -1112,8 +1145,6 @@ static bool bin_dwarf(RzCore *core, RzBinFile *binfile, PJ *pj, int mode) {
if (!rz_config_get_i(core->config, "bin.dbginfo") || !binfile->o) {
return false;
}
RzBinObject *o = binfile->o;
const RzBinSourceLineInfo *li = NULL;
RzBinDwarfDebugAbbrev *da = rz_bin_dwarf_parse_abbrev(binfile);
RzBinDwarfDebugInfo *info = da ? rz_bin_dwarf_parse_info(binfile, da) : NULL;
if (mode == RZ_MODE_PRINT) {
Expand All @@ -1125,15 +1156,6 @@ static bool bin_dwarf(RzCore *core, RzBinFile *binfile, PJ *pj, int mode) {
}
}
HtUP /*<offset, List *<LocListEntry>*/ *loc_table = rz_bin_dwarf_parse_loc(binfile, core->analysis->bits / 8);
// I suppose there is no reason the parse it for a printing purposes
if (info && mode != RZ_MODE_PRINT) {
/* Should we do this by default? */
RzAnalysisDwarfContext ctx = {
.info = info,
.loc = loc_table
};
rz_analysis_dwarf_process_info(core->analysis, &ctx);
}
if (loc_table) {
if (mode == RZ_MODE_PRINT) {
rz_core_bin_dwarf_print_loc(loc_table, core->analysis->bits / 8);
Expand All @@ -1147,25 +1169,22 @@ static bool bin_dwarf(RzCore *core, RzBinFile *binfile, PJ *pj, int mode) {
rz_list_free(aranges);
}
}
bool ret = false;
RzBinDwarfLineInfo *lines = rz_bin_dwarf_parse_line(binfile, info,
RZ_BIN_DWARF_LINE_INFO_MASK_LINES | (mode == RZ_MODE_PRINT ? RZ_BIN_DWARF_LINE_INFO_MASK_OPS : 0));
rz_bin_dwarf_debug_info_free(info);
if (lines) {
if (mode == RZ_MODE_PRINT) {
rz_core_bin_dwarf_print_line_units(lines->units);
}
// move all produced rows line info out (TODO: bin loading should do that)
li = o->lines = lines->lines;
lines->lines = NULL;
if (lines->lines) {
ret = true;
rz_core_bin_print_source_line_info(core, lines->lines, IS_MODE_JSON(mode) ? RZ_OUTPUT_MODE_JSON : RZ_OUTPUT_MODE_STANDARD, pj);
}
rz_bin_dwarf_line_info_free(lines);
}
rz_bin_dwarf_debug_abbrev_free(da);
if (!li) {
return false;
}
if (!IS_MODE_SET(mode)) {
rz_core_bin_print_source_line_info(core, li, IS_MODE_JSON(mode) ? RZ_OUTPUT_MODE_JSON : RZ_OUTPUT_MODE_STANDARD, pj);
}
return true;
return ret;
}

RZ_API void rz_core_bin_print_source_line_sample(RzCore *core, const RzBinSourceLineSample *s, RzOutputMode mode, PJ *pj) {
Expand Down
1 change: 1 addition & 0 deletions librz/include/rz_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,7 @@ RZ_API bool rz_core_bin_raise(RzCore *core, ut32 bfid);
RZ_API bool rz_core_bin_apply_strings(RzCore *r, RzBinFile *binfile);
RZ_API bool rz_core_bin_apply_config(RzCore *r, RzBinFile *binfile);
RZ_API bool rz_core_bin_apply_main(RzCore *r, RzBinFile *binfile, bool va);
RZ_API bool rz_core_bin_apply_dwarf(RzCore *core, RzBinFile *binfile);
RZ_API int rz_core_bin_apply_all_info(RzCore *r, RzBinFile *binfile);
RZ_API int rz_core_bin_set_by_fd(RzCore *core, ut64 bin_fd);
RZ_API int rz_core_bin_set_by_name(RzCore *core, const char *name);
Expand Down

0 comments on commit 132dd38

Please sign in to comment.