From d3606d810d0338c7dbeaa30191578b32f7d884bd Mon Sep 17 00:00:00 2001 From: Daniel Beer Date: Wed, 6 Jan 2010 13:24:59 +1300 Subject: [PATCH] Disassembler now looks up symbols for address operands. --- dis.c | 29 ++++++++++++++++++++++------- main.c | 4 ++-- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/dis.c b/dis.c index ccbcfb2..26003cd 100644 --- a/dis.c +++ b/dis.c @@ -21,6 +21,7 @@ #include #include "dis.h" +#include "stab.h" /**********************************************************************/ /* Disassembler @@ -516,6 +517,20 @@ static const char *const msp430_reg_names[] = { "R12", "R13", "R14", "R15" }; +static int format_addr(char *buf, int max_len, const char *prefix, + u_int16_t addr) +{ + const char *name; + + if (stab_find(&addr, &name) < 0) + return snprintf(buf, max_len, "%s0x%04x", prefix, addr); + + if (addr) + return snprintf(buf, max_len, "%s%s+0x%x", prefix, name, addr); + + return snprintf(buf, max_len, "%s%s", prefix, name); +} + /* Given an operands addressing mode, value and associated register, * print the canonical representation of it to stdout. * @@ -532,23 +547,23 @@ static int format_operand(char *buf, int max_len, return snprintf(buf, max_len, "%s", msp430_reg_names[reg]); case MSP430_AMODE_INDEXED: - return snprintf(buf, max_len, "%d(%s)", (int16_t)addr, + return snprintf(buf, max_len, "0x%x(%s)", (u_int16_t)addr, msp430_reg_names[reg]); case MSP430_AMODE_SYMBOLIC: - return snprintf(buf, max_len, "0x%04x", addr); + return format_addr(buf, max_len, "", addr); case MSP430_AMODE_ABSOLUTE: - return snprintf(buf, max_len, "&0x%04x", addr); + return format_addr(buf, max_len, "&", addr); case MSP430_AMODE_INDIRECT: - return snprintf(buf, max_len, "@%s", msp430_reg_names[reg]); + return format_addr(buf, max_len, "@", addr); case MSP430_AMODE_INDIRECT_INC: return snprintf(buf, max_len, "@%s+", msp430_reg_names[reg]); case MSP430_AMODE_IMMEDIATE: - return snprintf(buf, max_len, "#%d", (int16_t)addr); + return snprintf(buf, max_len, "#0x%x", (u_int16_t)addr); } return snprintf(buf, max_len, "???"); @@ -586,8 +601,8 @@ int dis_format(char *buf, int max_len, if ((insn->op == MSP430_OP_CALL || insn->op == MSP430_OP_BR) && insn->dst_mode == MSP430_AMODE_IMMEDIATE) - count += snprintf(buf + count, max_len - count, - "#0x%04x", insn->dst_addr); + count += format_addr(buf + count, max_len - count, + "#", insn->dst_addr); else count += format_operand(buf + count, max_len - count, diff --git a/main.c b/main.c index ff5343c..b9e5c6b 100644 --- a/main.c +++ b/main.c @@ -194,7 +194,7 @@ static void disassemble(u_int16_t offset, u_int8_t *data, int length) for (i = 0; i < count; i++) printf(" %02x", data[i]); - while (i < 8) { + while (i < 7) { printf(" "); i++; } @@ -220,7 +220,7 @@ static int cmd_dis(char **arg) char *len_text = get_arg(arg); int offset = 0; int length = 0x40; - u_int8_t buf[128]; + u_int8_t buf[512]; if (!off_text) { fprintf(stderr, "md: offset must be specified\n");