Skip to content

Commit

Permalink
Use binary file instead of 'SourceFile' in dex debug info ##bin
Browse files Browse the repository at this point in the history
  • Loading branch information
trufae authored Jan 23, 2025
1 parent 6c5ac57 commit 2c770da
Showing 1 changed file with 46 additions and 21 deletions.
67 changes: 46 additions & 21 deletions libr/bin/p/bin_dex.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,24 +431,24 @@ static void dex_parse_debug_item(RBinFile *bf, RBinDexClass *c, int MI, int MA,
}
while (keep) {
switch (opcode) {
case 0x0: // DBG_END_SEQUENCE
case 0x00: // DBG_END_SEQUENCE
keep = false;
break;
case 0x1: // DBG_ADVANCE_PC
case 0x01: // DBG_ADVANCE_PC
{
ut64 addr_diff;
r_buf_uleb128 (bf->buf, &addr_diff);
address += addr_diff;
}
break;
case 0x2: // DBG_ADVANCE_LINE
case 0x02: // DBG_ADVANCE_LINE
{
st64 line_diff;
r_buf_sleb128 (bf->buf, &line_diff);
line += line_diff;
}
break;
case 0x3: // DBG_START_LOCAL
case 0x03: // DBG_START_LOCAL
{
ut64 register_num, name_idx, type_idx;
r_buf_uleb128 (bf->buf, &register_num);
Expand Down Expand Up @@ -485,7 +485,7 @@ static void dex_parse_debug_item(RBinFile *bf, RBinDexClass *c, int MI, int MA,
//eprintf("DBG_START_LOCAL %x %x %x\n", register_num, name_idx, type_idx);
}
break;
case 0x4: //DBG_START_LOCAL_EXTENDED
case 0x04: // DBG_START_LOCAL_EXTENDED
{
ut64 register_num, name_idx, type_idx, sig_idx;
r_buf_uleb128 (bf->buf, &register_num);
Expand Down Expand Up @@ -524,7 +524,7 @@ static void dex_parse_debug_item(RBinFile *bf, RBinDexClass *c, int MI, int MA,
debug_locals[register_num].live = true;
}
break;
case 0x5: // DBG_END_LOCAL
case 0x05: // DBG_END_LOCAL
{
ut64 register_num;
r_buf_uleb128 (bf->buf, &register_num);
Expand All @@ -551,7 +551,7 @@ static void dex_parse_debug_item(RBinFile *bf, RBinDexClass *c, int MI, int MA,
debug_locals[register_num].live = false;
}
break;
case 0x6: // DBG_RESTART_LOCAL
case 0x06: // DBG_RESTART_LOCAL
{
ut64 register_num;
r_buf_uleb128 (bf->buf, &register_num);
Expand All @@ -564,18 +564,39 @@ static void dex_parse_debug_item(RBinFile *bf, RBinDexClass *c, int MI, int MA,
}
}
break;
case 0x7: //DBG_SET_PROLOGUE_END
case 0x07: // DBG_SET_PROLOGUE_END
break;
case 0x8: //DBG_SET_PROLOGUE_BEGIN
case 0x08: // DBG_SET_PROLOGUE_BEGIN
break;
case 0x9:
case 0x09: // DBG_SET_FILE
{
ut64 res;
r_buf_uleb128 (bf->buf, &res);
source_file_idx = res - 1;
}
break;
case 0x0a: // DBG_SET_FIRST_SPECIAL
{
eprintf ("especial\n");
// TODO
}
break;
#if 0
case -4: // DBG_LINE_BASE
{
// TODO
}
break;
#endif
#if 0
default:
// we parse trash or we dont support undocumented opcodes. but this must improve
R_LOG_WARN ("Unhandled dex debug opcode 0x%02x", opcode);
break;
#else
default:
#endif
case 0x0f: // DBG_LINE_RANGE
{
int adjusted_opcode = opcode - 10;
address += (adjusted_opcode / 15);
Expand Down Expand Up @@ -606,13 +627,21 @@ static void dex_parse_debug_item(RBinFile *bf, RBinDexClass *c, int MI, int MA,
struct dex_debug_position_t *pos;
// Loading the debug info takes too much time and nobody uses this afaik
// 0.5s of 5s is spent in this loop
ut64 old_source_file_idx = -1;
const char *source_file = NULL;
r_list_foreach (debug_positions, iter1, pos) {
const char *line = getstr (dex, pos->source_file_idx);
if (old_source_file_idx != pos->source_file_idx) {
source_file = getstr (dex, pos->source_file_idx);
if (!strcmp (source_file, "SourceFile")) {
source_file = bf->file;
}
old_source_file_idx = pos->source_file_idx;
}
char offset[SDB_NUM_BUFSZ] = {0};
if (R_STR_ISEMPTY (line)) {
if (R_STR_ISEMPTY (source_file)) {
continue;
}
char *fileline = r_str_newf ("%s|%"PFMT64d, line, pos->line);
char *fileline = r_str_newf ("%s|%"PFMT64d, source_file, pos->line);
char *offset_ptr = sdb_itoa (pos->address + paddr, 16, offset, sizeof (offset));
sdb_set (bf->sdb_addrinfo, offset_ptr, fileline, 0);
sdb_set (bf->sdb_addrinfo, fileline, offset_ptr, 0);
Expand All @@ -623,14 +652,10 @@ static void dex_parse_debug_item(RBinFile *bf, RBinDexClass *c, int MI, int MA,
dex->dexdump = false;
break;
}
if (line) {
rbindwardrow->file = strdup (line);
rbindwardrow->address = pos->address;
rbindwardrow->line = pos->line;
r_list_append (dex->lines_list, rbindwardrow);
} else {
free (rbindwardrow);
}
rbindwardrow->file = strdup (source_file);
rbindwardrow->address = pos->address;
rbindwardrow->line = pos->line;
r_list_append (dex->lines_list, rbindwardrow);
}
if (!dex->dexdump) {
goto beach;
Expand Down

0 comments on commit 2c770da

Please sign in to comment.