Symbol table and address expressions are now 32-bit clean.

This commit is contained in:
Daniel Beer 2010-08-05 13:59:33 +12:00
parent 622396a1c1
commit 137329cc13
11 changed files with 68 additions and 64 deletions

View File

@ -66,7 +66,7 @@ struct cproc_option {
union { union {
char text[128]; char text[128];
int numeric; address_t numeric;
} data; } data;
}; };

View File

@ -28,7 +28,7 @@ static int format_addr(stab_t stab, char *buf, int max_len,
msp430_amode_t amode, uint16_t addr) msp430_amode_t amode, uint16_t addr)
{ {
char name[64]; char name[64];
uint16_t offset; address_t offset;
int numeric = 0; int numeric = 0;
const char *prefix = ""; const char *prefix = "";
@ -193,7 +193,7 @@ void cproc_disassemble(cproc_t cp,
int retval; int retval;
int count; int count;
int i; int i;
uint16_t oboff; address_t oboff;
char obname[64]; char obname[64];
char buf[256]; char buf[256];
int len = 0; int len = 0;

View File

@ -58,8 +58,8 @@ static int cmd_md(cproc_t cp, char **arg)
device_t dev = cproc_device(cp); device_t dev = cproc_device(cp);
char *off_text = get_arg(arg); char *off_text = get_arg(arg);
char *len_text = get_arg(arg); char *len_text = get_arg(arg);
int offset = 0; address_t offset = 0;
int length = 0x40; address_t length = 0x40;
if (!off_text) { if (!off_text) {
fprintf(stderr, "md: offset must be specified\n"); fprintf(stderr, "md: offset must be specified\n");
@ -107,8 +107,8 @@ static int cmd_mw(cproc_t cp, char **arg)
stab_t stab = cproc_stab(cp); stab_t stab = cproc_stab(cp);
char *off_text = get_arg(arg); char *off_text = get_arg(arg);
char *byte_text; char *byte_text;
int offset = 0; address_t offset = 0;
int length = 0; address_t length = 0;
uint8_t buf[1024]; uint8_t buf[1024];
if (!off_text) { if (!off_text) {
@ -236,7 +236,7 @@ static int cmd_set(cproc_t cp, char **arg)
char *reg_text = get_arg(arg); char *reg_text = get_arg(arg);
char *val_text = get_arg(arg); char *val_text = get_arg(arg);
int reg; int reg;
int value = 0; address_t value = 0;
uint16_t regs[DEVICE_NUM_REGS]; uint16_t regs[DEVICE_NUM_REGS];
if (!(reg_text && val_text)) { if (!(reg_text && val_text)) {
@ -271,8 +271,8 @@ static int cmd_dis(cproc_t cp, char **arg)
stab_t stab = cproc_stab(cp); stab_t stab = cproc_stab(cp);
char *off_text = get_arg(arg); char *off_text = get_arg(arg);
char *len_text = get_arg(arg); char *len_text = get_arg(arg);
int offset = 0; address_t offset = 0;
int length = 0x40; address_t length = 0x40;
uint8_t buf[4096]; uint8_t buf[4096];
if (!off_text) { if (!off_text) {
@ -397,8 +397,8 @@ static int cmd_hexout(cproc_t cp, char **arg)
char *off_text = get_arg(arg); char *off_text = get_arg(arg);
char *len_text = get_arg(arg); char *len_text = get_arg(arg);
char *filename = *arg; char *filename = *arg;
int off; address_t off;
int length; address_t length;
struct hexout_data hexout; struct hexout_data hexout;
if (!(off_text && len_text && *filename)) { if (!(off_text && len_text && *filename)) {
@ -583,7 +583,7 @@ static int cmd_setbreak(cproc_t cp, char **arg)
char *addr_text = get_arg(arg); char *addr_text = get_arg(arg);
char *index_text = get_arg(arg); char *index_text = get_arg(arg);
int index = -1; int index = -1;
int addr; address_t addr;
if (!addr_text) { if (!addr_text) {
fprintf(stderr, "setbreak: address required\n"); fprintf(stderr, "setbreak: address required\n");
@ -656,7 +656,7 @@ static int cmd_break(cproc_t cp, char **arg)
if (bp->flags & DEVICE_BP_ENABLED) { if (bp->flags & DEVICE_BP_ENABLED) {
char name[128]; char name[128];
uint16_t offset; address_t offset;
printf(" %d. 0x%04x", i, bp->addr); printf(" %d. 0x%04x", i, bp->addr);
if (!stab_nearest(stab, bp->addr, name, if (!stab_nearest(stab, bp->addr, name,

23
expr.c
View File

@ -33,17 +33,17 @@
*/ */
struct addr_exp_state { struct addr_exp_state {
int last_operator; int last_operator;
int data_stack[32]; address_t data_stack[32];
int data_stack_size; int data_stack_size;
int op_stack[32]; int op_stack[32];
int op_stack_size; int op_stack_size;
}; };
static int addr_exp_data(stab_t stab, static int addr_exp_data(stab_t stab,
struct addr_exp_state *s, const char *text) struct addr_exp_state *s, const char *text)
{ {
int value; address_t value;
if (!s->last_operator || s->last_operator == ')') { if (!s->last_operator || s->last_operator == ')') {
fprintf(stderr, "syntax error at token %s\n", text); fprintf(stderr, "syntax error at token %s\n", text);
@ -73,10 +73,9 @@ static int addr_exp_data(stab_t stab,
static int addr_exp_pop(struct addr_exp_state *s) static int addr_exp_pop(struct addr_exp_state *s)
{ {
char op = s->op_stack[--s->op_stack_size]; char op = s->op_stack[--s->op_stack_size];
int data1 = s->data_stack[--s->data_stack_size]; address_t data1 = s->data_stack[--s->data_stack_size];
int data2 = 0; address_t data2 = 0;
address_t result = 0;
int result = 0;
if (op != 'N') if (op != 'N')
data2 = s->data_stack[--s->data_stack_size]; data2 = s->data_stack[--s->data_stack_size];
@ -197,7 +196,7 @@ static int addr_exp_op(struct addr_exp_state *s, char op)
return -1; return -1;
} }
static int addr_exp_finish(struct addr_exp_state *s, int *ret) static int addr_exp_finish(struct addr_exp_state *s, address_t *ret)
{ {
if (s->last_operator && s->last_operator != ')') { if (s->last_operator && s->last_operator != ')') {
fprintf(stderr, "syntax error at end of expression\n"); fprintf(stderr, "syntax error at end of expression\n");
@ -226,7 +225,7 @@ static int addr_exp_finish(struct addr_exp_state *s, int *ret)
return 0; return 0;
} }
int expr_eval(stab_t stab, const char *text, int *addr) int expr_eval(stab_t stab, const char *text, address_t *addr)
{ {
const char *text_save = text; const char *text_save = text;
int last_cc = 1; int last_cc = 1;

2
expr.h
View File

@ -25,6 +25,6 @@
/* Parse an address expression, storing the result in the integer /* Parse an address expression, storing the result in the integer
* pointed to. Returns 0 if parsed successfully, -1 if not. * pointed to. Returns 0 if parsed successfully, -1 if not.
*/ */
int expr_eval(stab_t stab, const char *text, int *value); int expr_eval(stab_t stab, const char *text, address_t *value);
#endif #endif

15
main.c
View File

@ -51,20 +51,21 @@ static void io_prefix(stab_t stab,
uint16_t addr, int is_byte) uint16_t addr, int is_byte)
{ {
char name[64]; char name[64];
address_t offset;
if (!stab_nearest(stab, pc, name, sizeof(name), &pc)) { if (!stab_nearest(stab, pc, name, sizeof(name), &offset)) {
printf("%s", name); printf("%s", name);
if (pc) if (offset)
printf("+0x%x", pc); printf("+0x%x", offset);
} else { } else {
printf("0x%04x", pc); printf("0x%04x", pc);
} }
printf(": IO %s.%c: 0x%04x", prefix, is_byte ? 'B' : 'W', addr); printf(": IO %s.%c: 0x%04x", prefix, is_byte ? 'B' : 'W', addr);
if (!stab_nearest(stab, addr, name, sizeof(name), &addr)) { if (!stab_nearest(stab, addr, name, sizeof(name), &offset)) {
printf(" (%s", name); printf(" (%s", name);
if (addr) if (offset)
printf("+0x%x", addr); printf("+0x%x", offset);
printf(")"); printf(")");
} }
} }
@ -79,7 +80,7 @@ static int fetch_io(void *user_data, uint16_t pc,
for (;;) { for (;;) {
char text[128]; char text[128];
int len; int len;
int data; address_t data;
printf("? "); printf("? ");
fflush(stdout); fflush(stdout);

View File

@ -128,7 +128,7 @@ static int isearch_addr(cproc_t cp, const char *term, char **arg,
int which = toupper(*term) == 'S' ? int which = toupper(*term) == 'S' ?
ISEARCH_SRC_ADDR : ISEARCH_DST_ADDR; ISEARCH_SRC_ADDR : ISEARCH_DST_ADDR;
const char *addr_text; const char *addr_text;
int addr; address_t addr;
if (q->flags & which) { if (q->flags & which) {
fprintf(stderr, "isearch: address already specified\n"); fprintf(stderr, "isearch: address already specified\n");
@ -392,8 +392,8 @@ static int cmd_isearch(cproc_t cp, char **arg)
struct isearch_query q; struct isearch_query q;
const char *addr_text; const char *addr_text;
const char *len_text; const char *len_text;
int addr; address_t addr;
int len; address_t len;
addr_text = get_arg(arg); addr_text = get_arg(arg);
len_text = get_arg(arg); len_text = get_arg(arg);
@ -711,7 +711,7 @@ static int add_irq_edges(int offset, int len, uint8_t *memory,
} }
static int add_symbol_nodes(void *user_data, const char *name, static int add_symbol_nodes(void *user_data, const char *name,
uint16_t offset) address_t offset)
{ {
struct call_graph *graph = (struct call_graph *)user_data; struct call_graph *graph = (struct call_graph *)user_data;
@ -778,7 +778,7 @@ static void cgraph_summary(struct call_graph *graph, cproc_t cp)
int from_count = 0; int from_count = 0;
int to_count = 0; int to_count = 0;
char name[64]; char name[64];
uint16_t o; address_t o;
while (j < graph->edge_from.size && while (j < graph->edge_from.size &&
CG_EDGE_FROM(graph, j)->src < n->offset) CG_EDGE_FROM(graph, j)->src < n->offset)
@ -817,7 +817,7 @@ static void cgraph_func_info(struct call_graph *graph, cproc_t cp,
int j = 0; int j = 0;
int k = 0; int k = 0;
char name[64]; char name[64];
u_int16_t offset; address_t offset;
struct cg_node *n; struct cg_node *n;
while (i + 1 < graph->node_list.size && while (i + 1 < graph->node_list.size &&
@ -893,7 +893,7 @@ static int cmd_cgraph(cproc_t cp, char **arg)
stab_t stab = cproc_stab(cp); stab_t stab = cproc_stab(cp);
device_t dev = cproc_device(cp); device_t dev = cproc_device(cp);
char *offset_text, *len_text, *addr_text;; char *offset_text, *len_text, *addr_text;;
int offset, len, addr; address_t offset, len, addr;
uint8_t *memory; uint8_t *memory;
struct call_graph graph; struct call_graph graph;

22
stab.c
View File

@ -58,8 +58,8 @@ static void sym_key_init(struct sym_key *key, const char *text)
} }
struct addr_key { struct addr_key {
uint16_t addr; address_t addr;
char name[64]; char name[64];
}; };
static const struct addr_key addr_key_zero = { static const struct addr_key addr_key_zero = {
@ -80,7 +80,7 @@ static int addr_key_compare(const void *left, const void *right)
return strcmp(kl->name, kr->name); return strcmp(kl->name, kr->name);
} }
static void addr_key_init(struct addr_key *key, uint16_t addr, static void addr_key_init(struct addr_key *key, address_t addr,
const char *text) const char *text)
{ {
int len = strlen(text); int len = strlen(text);
@ -98,7 +98,7 @@ static const struct btree_def sym_table_def = {
.zero = &sym_key_zero, .zero = &sym_key_zero,
.branches = 32, .branches = 32,
.key_size = sizeof(struct sym_key), .key_size = sizeof(struct sym_key),
.data_size = sizeof(uint16_t) .data_size = sizeof(address_t)
}; };
static const struct btree_def addr_table_def = { static const struct btree_def addr_table_def = {
@ -128,8 +128,8 @@ int stab_set(stab_t st, const char *name, int value)
{ {
struct sym_key skey; struct sym_key skey;
struct addr_key akey; struct addr_key akey;
uint16_t addr = value; address_t addr = value;
uint16_t old_addr; address_t old_addr;
sym_key_init(&skey, name); sym_key_init(&skey, name);
@ -152,8 +152,8 @@ int stab_set(stab_t st, const char *name, int value)
return 0; return 0;
} }
int stab_nearest(stab_t st, uint16_t addr, char *ret_name, int max_len, int stab_nearest(stab_t st, address_t addr, char *ret_name, int max_len,
uint16_t *ret_offset) address_t *ret_offset)
{ {
struct addr_key akey; struct addr_key akey;
int i; int i;
@ -173,10 +173,10 @@ int stab_nearest(stab_t st, uint16_t addr, char *ret_name, int max_len,
return -1; return -1;
} }
int stab_get(stab_t st, const char *name, int *value) int stab_get(stab_t st, const char *name, address_t *value)
{ {
struct sym_key skey; struct sym_key skey;
uint16_t addr; address_t addr;
sym_key_init(&skey, name); sym_key_init(&skey, name);
if (btree_get(st->sym, &skey, &addr)) if (btree_get(st->sym, &skey, &addr))
@ -189,7 +189,7 @@ int stab_get(stab_t st, const char *name, int *value)
int stab_del(stab_t st, const char *name) int stab_del(stab_t st, const char *name)
{ {
struct sym_key skey; struct sym_key skey;
uint16_t value; address_t value;
struct addr_key akey; struct addr_key akey;
sym_key_init(&skey, name); sym_key_init(&skey, name);

9
stab.h
View File

@ -20,6 +20,7 @@
#define STAB_H_ #define STAB_H_
#include <stdint.h> #include <stdint.h>
#include "util.h"
struct stab; struct stab;
typedef struct stab *stab_t; typedef struct stab *stab_t;
@ -41,13 +42,13 @@ int stab_set(stab_t st, const char *name, int value);
* *
* Returns 0 if found, 1 otherwise. * Returns 0 if found, 1 otherwise.
*/ */
int stab_nearest(stab_t st, uint16_t addr, char *ret_name, int max_len, int stab_nearest(stab_t st, address_t addr, char *ret_name, int max_len,
uint16_t *ret_offset); address_t *ret_offset);
/* Retrieve the value of a symbol. Returns 0 on success or -1 if the symbol /* Retrieve the value of a symbol. Returns 0 on success or -1 if the symbol
* doesn't exist. * doesn't exist.
*/ */
int stab_get(stab_t st, const char *name, int *value); int stab_get(stab_t st, const char *name, address_t *value);
/* Delete a symbol table entry. Returns 0 on success or -1 if the symbol /* Delete a symbol table entry. Returns 0 on success or -1 if the symbol
* doesn't exist. * doesn't exist.
@ -56,7 +57,7 @@ int stab_del(stab_t st, const char *name);
/* Enumerate all symbols in the table */ /* Enumerate all symbols in the table */
typedef int (*stab_callback_t)(void *user_data, typedef int (*stab_callback_t)(void *user_data,
const char *name, uint16_t value); const char *name, address_t value);
int stab_enum(stab_t st, stab_callback_t cb, void *user_data); int stab_enum(stab_t st, stab_callback_t cb, void *user_data);

16
sym.c
View File

@ -33,8 +33,8 @@
static int cmd_eval(cproc_t cp, char **arg) static int cmd_eval(cproc_t cp, char **arg)
{ {
stab_t stab = cproc_stab(cp); stab_t stab = cproc_stab(cp);
int addr; address_t addr;
uint16_t offset; address_t offset;
char name[64]; char name[64];
if (expr_eval(stab, *arg, &addr) < 0) { if (expr_eval(stab, *arg, &addr) < 0) {
@ -83,7 +83,7 @@ static int cmd_sym_load_add(cproc_t cp, int clear, char **arg)
return 0; return 0;
} }
static int savemap_cb(void *user_data, const char *name, uint16_t value) static int savemap_cb(void *user_data, const char *name, address_t value)
{ {
FILE *savemap_out = (FILE *)user_data; FILE *savemap_out = (FILE *)user_data;
@ -127,13 +127,13 @@ static int cmd_sym_savemap(cproc_t cp, char **arg)
return 0; return 0;
} }
static int print_sym(void *user_data, const char *name, uint16_t value) static int print_sym(void *user_data, const char *name, address_t value)
{ {
printf("0x%04x: %s\n", value, name); printf("0x%04x: %s\n", value, name);
return 0; return 0;
} }
static int find_sym(void *user_data, const char *name, uint16_t value) static int find_sym(void *user_data, const char *name, address_t value)
{ {
regex_t *find_preg = (regex_t *)user_data; regex_t *find_preg = (regex_t *)user_data;
@ -185,7 +185,7 @@ static int renames_do(stab_t stab,
VECTOR_PTR(rename->list, i, struct rename_record); VECTOR_PTR(rename->list, i, struct rename_record);
char new_name[128]; char new_name[128];
int len = r->start; int len = r->start;
int value; address_t value;
if (len + 1 > sizeof(new_name)) if (len + 1 > sizeof(new_name))
len = sizeof(new_name) - 1; len = sizeof(new_name) - 1;
@ -217,7 +217,7 @@ static int renames_do(stab_t stab,
return 0; return 0;
} }
static int find_renames(void *user_data, const char *name, uint16_t value) static int find_renames(void *user_data, const char *name, address_t value)
{ {
struct rename_data *rename = (struct rename_data *)user_data; struct rename_data *rename = (struct rename_data *)user_data;
regmatch_t pmatch; regmatch_t pmatch;
@ -317,7 +317,7 @@ static int cmd_sym(cproc_t cp, char **arg)
if (!strcasecmp(subcmd, "set")) { if (!strcasecmp(subcmd, "set")) {
char *name = get_arg(arg); char *name = get_arg(arg);
char *val_text = get_arg(arg); char *val_text = get_arg(arg);
int value; address_t value;
if (!(name && val_text)) { if (!(name && val_text)) {
fprintf(stderr, "sym: need a name and value to set " fprintf(stderr, "sym: need a name and value to set "

3
util.h
View File

@ -28,6 +28,9 @@
#define LE_WORD(b, x) ((LE_BYTE(b, x + 1) << 8) | LE_BYTE(b, x)) #define LE_WORD(b, x) ((LE_BYTE(b, x + 1) << 8) | LE_BYTE(b, x))
#define LE_LONG(b, x) ((LE_WORD(b, x + 2) << 16) | LE_WORD(b, x)) #define LE_LONG(b, x) ((LE_WORD(b, x + 2) << 16) | LE_WORD(b, x))
/* This type fits an MSP430X register value */
typedef uint32_t address_t;
/* Various utility functions for IO */ /* Various utility functions for IO */
int open_serial(const char *device, int rate); int open_serial(const char *device, int rate);
int read_with_timeout(int fd, uint8_t *data, int len); int read_with_timeout(int fd, uint8_t *data, int len);