stab: plain functions instead of passing around stab_default.

This commit is contained in:
Daniel Beer 2011-03-15 15:20:50 +13:00
parent ed2c841da9
commit ae2357170c
23 changed files with 123 additions and 146 deletions

View File

@ -29,7 +29,7 @@
struct file_format { struct file_format {
int (*check)(FILE *in); int (*check)(FILE *in);
int (*extract)(FILE *in, binfile_imgcb_t cb, void *user_data); int (*extract)(FILE *in, binfile_imgcb_t cb, void *user_data);
int (*syms)(FILE *in, stab_t stab); int (*syms)(FILE *in);
}; };
static const struct file_format formats[] = { static const struct file_format formats[] = {
@ -107,7 +107,7 @@ int binfile_extract(FILE *in, binfile_imgcb_t cb, void *user_data)
return fmt->extract(in, cb, user_data); return fmt->extract(in, cb, user_data);
} }
int binfile_syms(FILE *in, stab_t stab) int binfile_syms(FILE *in)
{ {
const struct file_format *fmt = identify(in); const struct file_format *fmt = identify(in);
@ -121,5 +121,5 @@ int binfile_syms(FILE *in, stab_t stab)
return -1; return -1;
} }
return fmt->syms(in, stab); return fmt->syms(in);
} }

View File

@ -47,6 +47,6 @@ int binfile_extract(FILE *in, binfile_imgcb_t cb, void *user_data);
/* Attempt to load symbols from the file and store them in the given /* Attempt to load symbols from the file and store them in the given
* symbol table. Returns 0 on success or -1 if an error occurs. * symbol table. Returns 0 on success or -1 if an error occurs.
*/ */
int binfile_syms(FILE *in, stab_t stab); int binfile_syms(FILE *in);
#endif #endif

4
coff.c
View File

@ -338,7 +338,7 @@ static int read_symtab(FILE *in, const struct coff_header *hdr,
return hdr->stab_count; return hdr->stab_count;
} }
int coff_syms(FILE *in, stab_t stab) int coff_syms(FILE *in)
{ {
struct coff_header hdr; struct coff_header hdr;
char *strtab; char *strtab;
@ -379,7 +379,7 @@ int coff_syms(FILE *in, stab_t stab)
} }
if ((storage_class == C_EXT || storage_class == C_LABEL) && if ((storage_class == C_EXT || storage_class == C_LABEL) &&
stab_set(stab, name, value) < 0) { stab_set(name, value) < 0) {
printc_err("coff: failed to insert symbol\n"); printc_err("coff: failed to insert symbol\n");
ret = -1; ret = -1;
break; break;

2
coff.h
View File

@ -23,6 +23,6 @@
int coff_check(FILE *in); int coff_check(FILE *in);
int coff_extract(FILE *in, binfile_imgcb_t cb, void *user_data); int coff_extract(FILE *in, binfile_imgcb_t cb, void *user_data);
int coff_syms(FILE *in, stab_t stab); int coff_syms(FILE *in);
#endif #endif

View File

@ -64,13 +64,13 @@ int cmd_md(char **arg)
return -1; return -1;
} }
if (expr_eval(stab_default, off_text, &offset) < 0) { if (expr_eval(off_text, &offset) < 0) {
printc_err("md: can't parse offset: %s\n", off_text); printc_err("md: can't parse offset: %s\n", off_text);
return -1; return -1;
} }
if (len_text) { if (len_text) {
if (expr_eval(stab_default, len_text, &length) < 0) { if (expr_eval(len_text, &length) < 0) {
printc_err("md: can't parse length: %s\n", printc_err("md: can't parse length: %s\n",
len_text); len_text);
return -1; return -1;
@ -109,7 +109,7 @@ int cmd_mw(char **arg)
return -1; return -1;
} }
if (expr_eval(stab_default, off_text, &offset) < 0) { if (expr_eval(off_text, &offset) < 0) {
printc_err("md: can't parse offset: %s\n", off_text); printc_err("md: can't parse offset: %s\n", off_text);
return -1; return -1;
} }
@ -144,7 +144,7 @@ int cmd_erase(char **arg)
device_erase_type_t type = DEVICE_ERASE_MAIN; device_erase_type_t type = DEVICE_ERASE_MAIN;
address_t segment = 0; address_t segment = 0;
if (seg_text && expr_eval(stab_default, seg_text, &segment) < 0) { if (seg_text && expr_eval(seg_text, &segment) < 0) {
printc_err("erase: invalid expression: %s\n", seg_text); printc_err("erase: invalid expression: %s\n", seg_text);
return -1; return -1;
} }
@ -180,7 +180,7 @@ int cmd_step(char **arg)
int i; int i;
if (count_text) { if (count_text) {
if (expr_eval(stab_default, count_text, &count) < 0) { if (expr_eval(count_text, &count) < 0) {
printc_err("step: can't parse count: %s\n", count_text); printc_err("step: can't parse count: %s\n", count_text);
return -1; return -1;
} }
@ -262,7 +262,7 @@ int cmd_set(char **arg)
return -1; return -1;
} }
if (expr_eval(stab_default, val_text, &value) < 0) { if (expr_eval(val_text, &value) < 0) {
printc_err("set: can't parse value: %s\n", val_text); printc_err("set: can't parse value: %s\n", val_text);
return -1; return -1;
} }
@ -290,13 +290,13 @@ int cmd_dis(char **arg)
return -1; return -1;
} }
if (expr_eval(stab_default, off_text, &offset) < 0) { if (expr_eval(off_text, &offset) < 0) {
printc_err("dis: can't parse offset: %s\n", off_text); printc_err("dis: can't parse offset: %s\n", off_text);
return -1; return -1;
} }
if (len_text) { if (len_text) {
if (expr_eval(stab_default, len_text, &length) < 0) { if (expr_eval(len_text, &length) < 0) {
printc_err("dis: can't parse length: %s\n", printc_err("dis: can't parse length: %s\n",
len_text); len_text);
return -1; return -1;
@ -440,8 +440,8 @@ int cmd_hexout(char **arg)
return -1; return -1;
} }
if (expr_eval(stab_default, off_text, &off) < 0 || if (expr_eval(off_text, &off) < 0 ||
expr_eval(stab_default, len_text, &length) < 0) expr_eval(len_text, &length) < 0)
return -1; return -1;
if (hexout_start(&hexout, filename) < 0) if (hexout_start(&hexout, filename) < 0)
@ -515,8 +515,8 @@ static int do_cmd_prog(char **arg, int prog_flags)
} }
if (prog_flags && (binfile_info(in) & BINFILE_HAS_SYMS)) { if (prog_flags && (binfile_info(in) & BINFILE_HAS_SYMS)) {
stab_clear(stab_default); stab_clear();
binfile_syms(in, stab_default); binfile_syms(in);
} }
fclose(in); fclose(in);
@ -555,7 +555,7 @@ int cmd_setbreak(char **arg)
return -1; return -1;
} }
if (expr_eval(stab_default, addr_text, &addr) < 0) { if (expr_eval(addr_text, &addr) < 0) {
printc_err("setbreak: invalid address\n"); printc_err("setbreak: invalid address\n");
return -1; return -1;
} }
@ -563,7 +563,7 @@ int cmd_setbreak(char **arg)
if (index_text) { if (index_text) {
address_t val; address_t val;
if (expr_eval(stab_default, index_text, &val) < 0 || if (expr_eval(index_text, &val) < 0 ||
val >= device_default->max_breakpoints) { val >= device_default->max_breakpoints) {
printc("setbreak: invalid breakpoint slot: %d\n", val); printc("setbreak: invalid breakpoint slot: %d\n", val);
return -1; return -1;
@ -591,7 +591,7 @@ int cmd_delbreak(char **arg)
if (index_text) { if (index_text) {
address_t index; address_t index;
if (expr_eval(stab_default, index_text, &index) < 0 || if (expr_eval(index_text, &index) < 0 ||
index >= device_default->max_breakpoints) { index >= device_default->max_breakpoints) {
printc("delbreak: invalid breakpoint slot: %d\n", printc("delbreak: invalid breakpoint slot: %d\n",
index); index);
@ -625,7 +625,7 @@ int cmd_break(char **arg)
address_t offset; address_t offset;
printc(" %d. 0x%05x", i, bp->addr); printc(" %d. 0x%05x", i, bp->addr);
if (!stab_nearest(stab_default, bp->addr, name, if (!stab_nearest(bp->addr, name,
sizeof(name), &offset)) { sizeof(name), &offset)) {
printc(" (%s", name); printc(" (%s", name);
if (offset) if (offset)

View File

@ -274,7 +274,7 @@ static int syms_load_strings(struct elf32_info *info, FILE *in, Elf32_Shdr *s)
#endif #endif
static int syms_load_syms(struct elf32_info *info, FILE *in, static int syms_load_syms(struct elf32_info *info, FILE *in,
Elf32_Shdr *s, stab_t stab) Elf32_Shdr *s)
{ {
Elf32_Sym syms[N_SYMS]; Elf32_Sym syms[N_SYMS];
int len = s->sh_size / sizeof(syms[0]); int len = s->sh_size / sizeof(syms[0]);
@ -314,7 +314,7 @@ static int syms_load_syms(struct elf32_info *info, FILE *in,
(st == STT_OBJECT || st == STT_FUNC || (st == STT_OBJECT || st == STT_FUNC ||
st == STT_SECTION || st == STT_COMMON || st == STT_SECTION || st == STT_COMMON ||
st == STT_TLS) && st == STT_TLS) &&
stab_set(stab, info->string_tab + y->st_name, stab_set(info->string_tab + y->st_name,
y->st_value) < 0) y->st_value) < 0)
return -1; return -1;
} }
@ -325,7 +325,7 @@ static int syms_load_syms(struct elf32_info *info, FILE *in,
return 0; return 0;
} }
int elf32_syms(FILE *in, stab_t stab) int elf32_syms(FILE *in)
{ {
struct elf32_info info; struct elf32_info info;
Elf32_Shdr *s; Elf32_Shdr *s;
@ -346,7 +346,7 @@ int elf32_syms(FILE *in, stab_t stab)
} }
if (syms_load_strings(&info, in, &info.file_shdrs[s->sh_link]) < 0 || if (syms_load_strings(&info, in, &info.file_shdrs[s->sh_link]) < 0 ||
syms_load_syms(&info, in, s, stab) < 0) syms_load_syms(&info, in, s) < 0)
ret = -1; ret = -1;
if (info.string_tab) if (info.string_tab)

View File

@ -23,6 +23,6 @@
int elf32_check(FILE *in); int elf32_check(FILE *in);
int elf32_extract(FILE *in, binfile_imgcb_t cb, void *user_data); int elf32_extract(FILE *in, binfile_imgcb_t cb, void *user_data);
int elf32_syms(FILE *in, stab_t stab); int elf32_syms(FILE *in);
#endif #endif

9
expr.c
View File

@ -42,8 +42,7 @@ struct addr_exp_state {
int op_stack_size; int op_stack_size;
}; };
static int addr_exp_data(stab_t stab, static int addr_exp_data(struct addr_exp_state *s, const char *text)
struct addr_exp_state *s, const char *text)
{ {
address_t value; address_t value;
@ -57,7 +56,7 @@ static int addr_exp_data(stab_t stab,
value = strtoul(text + 2, NULL, 16); value = strtoul(text + 2, NULL, 16);
} else if (*text == '0' && text[1] == 'd') { } else if (*text == '0' && text[1] == 'd') {
value = atoi(text + 2); value = atoi(text + 2);
} else if (stab_get(stab, text, &value) < 0) { } else if (stab_get(text, &value) < 0) {
char *end; char *end;
value = strtol(text, &end, opdb_get_numeric("iradix")); value = strtol(text, &end, opdb_get_numeric("iradix"));
@ -232,7 +231,7 @@ static int addr_exp_finish(struct addr_exp_state *s, address_t *ret)
return 0; return 0;
} }
int expr_eval(stab_t stab, const char *text, address_t *addr) int expr_eval(const char *text, address_t *addr)
{ {
const char *text_save = text; const char *text_save = text;
int last_cc = 1; int last_cc = 1;
@ -270,7 +269,7 @@ int expr_eval(stab_t stab, const char *text, address_t *addr)
token_buf[token_len] = 0; token_buf[token_len] = 0;
token_len = 0; token_len = 0;
if (addr_exp_data(stab, &s, token_buf) < 0) if (addr_exp_data(&s, token_buf) < 0)
goto fail; goto fail;
} }

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, address_t *value); int expr_eval(const char *text, address_t *value);
#endif #endif

2
gdb.c
View File

@ -703,7 +703,7 @@ int cmd_gdb(char **arg)
char *port_text = get_arg(arg); char *port_text = get_arg(arg);
address_t port = 2000; address_t port = 2000;
if (port_text && expr_eval(stab_default, port_text, &port) < 0) { if (port_text && expr_eval(port_text, &port) < 0) {
printc_err("gdb: can't parse port: %s\n", port_text); printc_err("gdb: can't parse port: %s\n", port_text);
return -1; return -1;
} }

8
main.c
View File

@ -271,13 +271,12 @@ int setup_driver(struct cmdline_args *args)
return -1; return -1;
} }
stab_default = stab_new(); if (stab_init() < 0)
if (!stab_default)
return -1; return -1;
device_default = driver_table[i]->open(&args->devarg); device_default = driver_table[i]->open(&args->devarg);
if (!device_default) { if (!device_default) {
stab_destroy(stab_default); stab_exit();
return -1; return -1;
} }
@ -318,8 +317,7 @@ int main(int argc, char **argv)
} }
simio_exit(); simio_exit();
stab_exit();
stab_destroy(stab_default);
device_destroy(); device_destroy();
return ret; return ret;

View File

@ -53,7 +53,7 @@ static int format_addr(msp430_amode_t amode, uint16_t addr)
if ((!numeric || if ((!numeric ||
(addr >= 0x200 && addr < 0xfff0)) && (addr >= 0x200 && addr < 0xfff0)) &&
!stab_nearest(stab_default, addr, name, sizeof(name), &offset) && !stab_nearest(addr, name, sizeof(name), &offset) &&
!offset) !offset)
return printc("%s\x1b[1m%s\x1b[0m", prefix, name); return printc("%s\x1b[1m%s\x1b[0m", prefix, name);
else if (numeric) else if (numeric)
@ -178,7 +178,7 @@ void disassemble(address_t offset, const uint8_t *data, int length)
address_t oboff; address_t oboff;
char obname[64]; char obname[64];
if (!stab_nearest(stab_default, offset, obname, sizeof(obname), if (!stab_nearest(offset, obname, sizeof(obname),
&oboff)) { &oboff)) {
if (!oboff) if (!oboff)
printc("\x1b[m%s:\x1b[0m\n", obname); printc("\x1b[m%s:\x1b[0m\n", obname);

View File

@ -154,7 +154,7 @@ static int isearch_addr(const char *term, char **arg,
return -1; return -1;
} }
if (expr_eval(stab_default, addr_text, &addr) < 0) if (expr_eval(addr_text, &addr) < 0)
return -1; return -1;
q->flags |= which; q->flags |= which;
@ -409,8 +409,8 @@ int cmd_isearch(char **arg)
return -1; return -1;
} }
if (expr_eval(stab_default, addr_text, &addr) < 0 || if (expr_eval(addr_text, &addr) < 0 ||
expr_eval(stab_default, len_text, &len) < 0) expr_eval(len_text, &len) < 0)
return -1; return -1;
q.flags = 0; q.flags = 0;
@ -755,7 +755,7 @@ static int cgraph_init(address_t offset, address_t len, uint8_t *memory,
if (add_irq_edges(offset, len, memory, graph) < 0) if (add_irq_edges(offset, len, memory, graph) < 0)
goto fail; goto fail;
if (stab_enum(stab_default, add_symbol_nodes, graph) < 0) if (stab_enum(add_symbol_nodes, graph) < 0)
goto fail; goto fail;
if (add_nodes_from_edges(graph) < 0) if (add_nodes_from_edges(graph) < 0)
goto fail; goto fail;
@ -807,7 +807,7 @@ static void cgraph_summary(struct call_graph *graph)
k++; k++;
} }
if (stab_nearest(stab_default, n->offset, if (stab_nearest(n->offset,
name, sizeof(name), &o) || name, sizeof(name), &o) ||
o) o)
name[0] = 0; name[0] = 0;
@ -845,8 +845,7 @@ static void cgraph_func_info(struct call_graph *graph, address_t addr)
CG_EDGE_TO(graph, k)->dst < n->offset) CG_EDGE_TO(graph, k)->dst < n->offset)
k++; k++;
if (stab_nearest(stab_default, n->offset, if (stab_nearest(n->offset, name, sizeof(name), &offset))
name, sizeof(name), &offset))
printc("0x%04x:\n", n->offset); printc("0x%04x:\n", n->offset);
else if (offset) else if (offset)
printc("0x%04x %s+0x%x:\n", n->offset, name, offset); printc("0x%04x %s+0x%x:\n", n->offset, name, offset);
@ -862,7 +861,7 @@ static void cgraph_func_info(struct call_graph *graph, address_t addr)
if (e->src != n->offset) if (e->src != n->offset)
break; break;
if (stab_nearest(stab_default, e->dst, if (stab_nearest(e->dst,
name, sizeof(name), name, sizeof(name),
&offset) || &offset) ||
offset) offset)
@ -884,7 +883,7 @@ static void cgraph_func_info(struct call_graph *graph, address_t addr)
if (e->dst != n->offset) if (e->dst != n->offset)
break; break;
if (stab_nearest(stab_default, e->src, if (stab_nearest(e->src,
name, sizeof(name), name, sizeof(name),
&offset) || &offset) ||
offset) offset)
@ -915,19 +914,19 @@ int cmd_cgraph(char **arg)
return -1; return -1;
} }
if (expr_eval(stab_default, offset_text, &offset) < 0) { if (expr_eval(offset_text, &offset) < 0) {
printc_err("cgraph: invalid offset: %s\n", offset_text); printc_err("cgraph: invalid offset: %s\n", offset_text);
return -1; return -1;
} }
offset &= ~1; offset &= ~1;
if (expr_eval(stab_default, len_text, &len) < 0) { if (expr_eval(len_text, &len) < 0) {
printc_err("cgraph: invalid length: %s\n", len_text); printc_err("cgraph: invalid length: %s\n", len_text);
return -1; return -1;
} }
len &= ~1; len &= ~1;
if (addr_text && expr_eval(stab_default, addr_text, &addr) < 0) { if (addr_text && expr_eval(addr_text, &addr) < 0) {
printc_err("cgraph: invalid address: %s\n", addr_text); printc_err("cgraph: invalid address: %s\n", addr_text);
return -1; return -1;
} }

View File

@ -94,7 +94,7 @@ static int config_addr(address_t *addr, char **arg_text)
return -1; return -1;
} }
if (expr_eval(stab_default, text, addr) < 0) { if (expr_eval(text, addr) < 0) {
printc_err("gpio: can't parse address: %s\n", text); printc_err("gpio: can't parse address: %s\n", text);
return -1; return -1;
} }
@ -112,7 +112,7 @@ static int config_irq(int *irq, char **arg_text)
return -1; return -1;
} }
if (expr_eval(stab_default, text, &value) < 0) { if (expr_eval(text, &value) < 0) {
printc_err("gpio: can't parse interrupt number: %s\n", text); printc_err("gpio: can't parse interrupt number: %s\n", text);
return -1; return -1;
} }
@ -134,13 +134,13 @@ static int config_channel(struct gpio *g, char **arg_text)
return -1; return -1;
} }
if (expr_eval(stab_default, which_text, &which) < 0) { if (expr_eval(which_text, &which) < 0) {
printc_err("gpio: can't parse pin number: %s\n", printc_err("gpio: can't parse pin number: %s\n",
which_text); which_text);
return -1; return -1;
} }
if (expr_eval(stab_default, value_text, &value) < 0) { if (expr_eval(value_text, &value) < 0) {
printc_err("gpio: can't parse pin value: %s\n", printc_err("gpio: can't parse pin value: %s\n",
value_text); value_text);
return -1; return -1;

View File

@ -83,7 +83,7 @@ static struct simio_device *timer_create(char **arg_text)
if (size_text) { if (size_text) {
address_t value; address_t value;
if (expr_eval(stab_default, size_text, &value) < 0) { if (expr_eval(size_text, &value) < 0) {
printc_err("timer: can't parse size: %s\n", printc_err("timer: can't parse size: %s\n",
size_text); size_text);
return NULL; return NULL;
@ -139,7 +139,7 @@ static int config_addr(address_t *addr, char **arg_text)
return -1; return -1;
} }
if (expr_eval(stab_default, text, addr) < 0) { if (expr_eval(text, addr) < 0) {
printc_err("timer: can't parse address: %s\n", text); printc_err("timer: can't parse address: %s\n", text);
return -1; return -1;
} }
@ -157,7 +157,7 @@ static int config_irq(int *irq, char **arg_text)
return -1; return -1;
} }
if (expr_eval(stab_default, text, &value) < 0) { if (expr_eval(text, &value) < 0) {
printc_err("timer: can't parse interrupt number: %s\n", text); printc_err("timer: can't parse interrupt number: %s\n", text);
return -1; return -1;
} }
@ -180,13 +180,13 @@ static int config_channel(struct timer *tr, char **arg_text)
return -1; return -1;
} }
if (expr_eval(stab_default, which_text, &which) < 0) { if (expr_eval(which_text, &which) < 0) {
printc_err("timer: can't parse channel number: %s\n", printc_err("timer: can't parse channel number: %s\n",
which_text); which_text);
return -1; return -1;
} }
if (expr_eval(stab_default, value_text, &value) < 0) { if (expr_eval(value_text, &value) < 0) {
printc_err("timer: can't parse channel value: %s\n", printc_err("timer: can't parse channel value: %s\n",
value_text); value_text);
return -1; return -1;

View File

@ -71,7 +71,7 @@ static void print_address(address_t addr)
address_t offset; address_t offset;
printc("0x%04x", addr); printc("0x%04x", addr);
if (!stab_nearest(stab_default, addr, name, sizeof(name), &offset)) { if (!stab_nearest(addr, name, sizeof(name), &offset)) {
printc(" (%s", name); printc(" (%s", name);
if (offset) if (offset)
printc("+0x%x", offset); printc("+0x%x", offset);
@ -150,7 +150,7 @@ static struct simio_device *tracer_create(char **arg_text)
if (size_text) { if (size_text) {
address_t value; address_t value;
if (expr_eval(stab_default, size_text, &value) < 0) { if (expr_eval(size_text, &value) < 0) {
printc_err("tracer: can't parse history size: %s\n", printc_err("tracer: can't parse history size: %s\n",
size_text); size_text);
return NULL; return NULL;
@ -225,7 +225,7 @@ static int tracer_config(struct simio_device *dev,
return -1; return -1;
} }
if (expr_eval(stab_default, irq_text, &value) < 0) { if (expr_eval(irq_text, &value) < 0) {
printc_err("tracer: trigger: can't parse IRQ " printc_err("tracer: trigger: can't parse IRQ "
"number: %s\n", irq_text); "number: %s\n", irq_text);
return -1; return -1;

View File

@ -102,7 +102,7 @@ static int parse_int(int *val, char **arg_text)
return -1; return -1;
} }
if (expr_eval(stab_default, text, &value) < 0) { if (expr_eval(text, &value) < 0) {
printc_err("wdt: couldn't parse argument: %s\n", text); printc_err("wdt: couldn't parse argument: %s\n", text);
return -1; return -1;
} }

80
stab.c
View File

@ -29,8 +29,6 @@
#include "util.h" #include "util.h"
#include "output.h" #include "output.h"
stab_t stab_default;
/************************************************************************ /************************************************************************
* B+Tree definitions * B+Tree definitions
*/ */
@ -116,18 +114,16 @@ static const struct btree_def addr_table_def = {
* Symbol table methods * Symbol table methods
*/ */
struct stab { static btree_t stab_sym;
btree_t sym; static btree_t stab_addr;
btree_t addr;
};
void stab_clear(stab_t st) void stab_clear(void)
{ {
btree_clear(st->sym); btree_clear(stab_sym);
btree_clear(st->addr); btree_clear(stab_addr);
} }
int stab_set(stab_t st, const char *name, int value) int stab_set(const char *name, int value)
{ {
struct sym_key skey; struct sym_key skey;
struct addr_key akey; struct addr_key akey;
@ -139,15 +135,15 @@ int stab_set(stab_t st, const char *name, int value)
/* Look for an old address first, and delete the reverse mapping /* Look for an old address first, and delete the reverse mapping
* if it's there. * if it's there.
*/ */
if (!btree_get(st->sym, &skey, &old_addr)) { if (!btree_get(stab_sym, &skey, &old_addr)) {
addr_key_init(&akey, old_addr, skey.name); addr_key_init(&akey, old_addr, skey.name);
btree_delete(st->addr, &akey); btree_delete(stab_addr, &akey);
} }
/* Put the new mapping into both tables */ /* Put the new mapping into both tables */
addr_key_init(&akey, addr, name); addr_key_init(&akey, addr, name);
if (btree_put(st->addr, &akey, NULL) < 0 || if (btree_put(stab_addr, &akey, NULL) < 0 ||
btree_put(st->sym, &skey, &addr) < 0) { btree_put(stab_sym, &skey, &addr) < 0) {
printc_err("stab: can't set %s = 0x%04x\n", name, addr); printc_err("stab: can't set %s = 0x%04x\n", name, addr);
return -1; return -1;
} }
@ -155,7 +151,7 @@ int stab_set(stab_t st, const char *name, int value)
return 0; return 0;
} }
int stab_nearest(stab_t st, address_t addr, char *ret_name, int max_len, int stab_nearest(address_t addr, char *ret_name, int max_len,
address_t *ret_offset) address_t *ret_offset)
{ {
struct addr_key akey; struct addr_key akey;
@ -166,7 +162,7 @@ int stab_nearest(stab_t st, address_t addr, char *ret_name, int max_len,
akey.name[i] = 0xff; akey.name[i] = 0xff;
akey.name[sizeof(akey.name) - 1] = 0xff; akey.name[sizeof(akey.name) - 1] = 0xff;
if (!btree_select(st->addr, &akey, BTREE_LE, &akey, NULL)) { if (!btree_select(stab_addr, &akey, BTREE_LE, &akey, NULL)) {
strncpy(ret_name, akey.name, max_len); strncpy(ret_name, akey.name, max_len);
ret_name[max_len - 1] = 0; ret_name[max_len - 1] = 0;
*ret_offset = addr - akey.addr; *ret_offset = addr - akey.addr;
@ -176,83 +172,73 @@ int stab_nearest(stab_t st, address_t addr, char *ret_name, int max_len,
return -1; return -1;
} }
int stab_get(stab_t st, const char *name, address_t *value) int stab_get(const char *name, address_t *value)
{ {
struct sym_key skey; struct sym_key skey;
address_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(stab_sym, &skey, &addr))
return -1; return -1;
*value = addr; *value = addr;
return 0; return 0;
} }
int stab_del(stab_t st, const char *name) int stab_del(const char *name)
{ {
struct sym_key skey; struct sym_key skey;
address_t value; address_t value;
struct addr_key akey; struct addr_key akey;
sym_key_init(&skey, name); sym_key_init(&skey, name);
if (btree_get(st->sym, &skey, &value)) if (btree_get(stab_sym, &skey, &value))
return -1; return -1;
addr_key_init(&akey, value, name); addr_key_init(&akey, value, name);
btree_delete(st->sym, &skey); btree_delete(stab_sym, &skey);
btree_delete(st->addr, &akey); btree_delete(stab_addr, &akey);
return 0; return 0;
} }
int stab_enum(stab_t st, stab_callback_t cb, void *user_data) int stab_enum(stab_callback_t cb, void *user_data)
{ {
int ret; int ret;
struct addr_key akey; struct addr_key akey;
ret = btree_select(st->addr, NULL, BTREE_FIRST, ret = btree_select(stab_addr, NULL, BTREE_FIRST,
&akey, NULL); &akey, NULL);
while (!ret) { while (!ret) {
if (cb(user_data, akey.name, akey.addr) < 0) if (cb(user_data, akey.name, akey.addr) < 0)
return -1; return -1;
ret = btree_select(st->addr, NULL, BTREE_NEXT, ret = btree_select(stab_addr, NULL, BTREE_NEXT,
&akey, NULL); &akey, NULL);
} }
return 0; return 0;
} }
stab_t stab_new(void) int stab_init(void)
{ {
stab_t st = malloc(sizeof(*st)); stab_sym = btree_alloc(&sym_table_def);
if (!stab_sym) {
if (!st) {
pr_error("stab: failed to allocate memory\n");
return NULL;
}
st->sym = btree_alloc(&sym_table_def);
if (!st->sym) {
printc_err("stab: failed to allocate symbol table\n"); printc_err("stab: failed to allocate symbol table\n");
free(st); return -1;
return NULL;
} }
st->addr = btree_alloc(&addr_table_def); stab_addr = btree_alloc(&addr_table_def);
if (!st->addr) { if (!stab_addr) {
printc_err("stab: failed to allocate address table\n"); printc_err("stab: failed to allocate address table\n");
btree_free(st->sym); btree_free(stab_sym);
free(st); return -1;
return NULL;
} }
return st; return 0;
} }
void stab_destroy(stab_t st) void stab_exit(void)
{ {
btree_free(st->sym); btree_free(stab_sym);
btree_free(st->addr); btree_free(stab_addr);
free(st);
} }

21
stab.h
View File

@ -22,45 +22,40 @@
#include <stdint.h> #include <stdint.h>
#include "util.h" #include "util.h"
struct stab;
typedef struct stab *stab_t;
extern stab_t stab_default;
/* Create/destroy a symbol table. The constructor returns NULL if it /* Create/destroy a symbol table. The constructor returns NULL if it
* was unable to allocate memory for the table. * was unable to allocate memory for the table.
*/ */
stab_t stab_new(void); int stab_init(void);
void stab_destroy(stab_t st); void stab_exit(void);
/* Reset the symbol table (delete all symbols) */ /* Reset the symbol table (delete all symbols) */
void stab_clear(stab_t st); void stab_clear(void);
/* Set a symbol in the table. Returns 0 on success, or -1 on error. */ /* Set a symbol in the table. Returns 0 on success, or -1 on error. */
int stab_set(stab_t st, const char *name, int value); int stab_set(const char *name, int value);
/* Take an address and find the nearest symbol and offset (always /* Take an address and find the nearest symbol and offset (always
* non-negative). * non-negative).
* *
* Returns 0 if found, 1 otherwise. * Returns 0 if found, 1 otherwise.
*/ */
int stab_nearest(stab_t st, address_t addr, char *ret_name, int max_len, int stab_nearest(address_t addr, char *ret_name, int max_len,
address_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, address_t *value); int stab_get(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.
*/ */
int stab_del(stab_t st, const char *name); int stab_del(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, address_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_callback_t cb, void *user_data);
#endif #endif

View File

@ -120,7 +120,7 @@ static int parse_option(opdb_type_t type, union opdb_value *value,
break; break;
case OPDB_TYPE_NUMERIC: case OPDB_TYPE_NUMERIC:
return expr_eval(stab_default, word, &value->numeric); return expr_eval(word, &value->numeric);
case OPDB_TYPE_STRING: case OPDB_TYPE_STRING:
strncpy(value->string, word, sizeof(value->string)); strncpy(value->string, word, sizeof(value->string));

30
sym.c
View File

@ -38,13 +38,13 @@ int cmd_eval(char **arg)
address_t offset; address_t offset;
char name[64]; char name[64];
if (expr_eval(stab_default, *arg, &addr) < 0) { if (expr_eval(*arg, &addr) < 0) {
printc_err("=: can't parse: %s\n", *arg); printc_err("=: can't parse: %s\n", *arg);
return -1; return -1;
} }
printc("0x%05x", addr); printc("0x%05x", addr);
if (!stab_nearest(stab_default, addr, name, sizeof(name), &offset)) { if (!stab_nearest(addr, name, sizeof(name), &offset)) {
printc(" = %s", name); printc(" = %s", name);
if (offset) if (offset)
printc("+0x%x", offset); printc("+0x%x", offset);
@ -68,13 +68,13 @@ static int cmd_sym_load_add(int clear, char **arg)
} }
if (clear) { if (clear) {
stab_clear(stab_default); stab_clear();
unmark_modified(MODIFY_SYMS); unmark_modified(MODIFY_SYMS);
} else { } else {
mark_modified(MODIFY_SYMS); mark_modified(MODIFY_SYMS);
} }
if (binfile_syms(in, stab_default) < 0) { if (binfile_syms(in) < 0) {
fclose(in); fclose(in);
return -1; return -1;
} }
@ -112,7 +112,7 @@ static int cmd_sym_savemap(char **arg)
return -1; return -1;
} }
if (stab_enum(stab_default, savemap_cb, savemap_out) < 0) { if (stab_enum(savemap_cb, savemap_out) < 0) {
fclose(savemap_out); fclose(savemap_out);
return -1; return -1;
} }
@ -148,7 +148,7 @@ static int cmd_sym_find(char **arg)
char *expr = get_arg(arg); char *expr = get_arg(arg);
if (!expr) { if (!expr) {
stab_enum(stab_default, print_sym, NULL); stab_enum(print_sym, NULL);
return 0; return 0;
} }
@ -157,7 +157,7 @@ static int cmd_sym_find(char **arg)
return -1; return -1;
} }
stab_enum(stab_default, find_sym, &find_preg); stab_enum(find_sym, &find_preg);
regfree(&find_preg); regfree(&find_preg);
return 0; return 0;
} }
@ -194,13 +194,13 @@ static int renames_do(struct rename_data *rename, const char *replace)
printc("%s -> %s\n", r->old_name, new_name); printc("%s -> %s\n", r->old_name, new_name);
if (stab_get(stab_default, r->old_name, &value) < 0) { if (stab_get(r->old_name, &value) < 0) {
printc_err("sym: warning: " printc_err("sym: warning: "
"symbol missing: %s\n", "symbol missing: %s\n",
r->old_name); r->old_name);
} else { } else {
stab_del(stab_default, r->old_name); stab_del(r->old_name);
if (stab_set(stab_default, new_name, value) < 0) { if (stab_set(new_name, value) < 0) {
printc_err("sym: warning: " printc_err("sym: warning: "
"failed to set new name: %s\n", "failed to set new name: %s\n",
new_name); new_name);
@ -253,7 +253,7 @@ static int cmd_sym_rename(char **arg)
vector_init(&rename.list, sizeof(struct rename_record)); vector_init(&rename.list, sizeof(struct rename_record));
if (stab_enum(stab_default, find_renames, &rename) < 0) { if (stab_enum(find_renames, &rename) < 0) {
printc_err("sym: rename failed\n"); printc_err("sym: rename failed\n");
regfree(&rename.preg); regfree(&rename.preg);
vector_destroy(&rename.list); vector_destroy(&rename.list);
@ -280,7 +280,7 @@ static int cmd_sym_del(char **arg)
return -1; return -1;
} }
if (stab_del(stab_default, name) < 0) { if (stab_del(name) < 0) {
printc_err("sym: can't delete nonexistent symbol: %s\n", printc_err("sym: can't delete nonexistent symbol: %s\n",
name); name);
return -1; return -1;
@ -303,7 +303,7 @@ int cmd_sym(char **arg)
if (!strcasecmp(subcmd, "clear")) { if (!strcasecmp(subcmd, "clear")) {
if (prompt_abort(MODIFY_SYMS)) if (prompt_abort(MODIFY_SYMS))
return 0; return 0;
stab_clear(stab_default); stab_clear();
unmark_modified(MODIFY_SYMS); unmark_modified(MODIFY_SYMS);
return 0; return 0;
} }
@ -319,13 +319,13 @@ int cmd_sym(char **arg)
return -1; return -1;
} }
if (expr_eval(stab_default, val_text, &value) < 0) { if (expr_eval(val_text, &value) < 0) {
printc_err("sym: can't parse value: %s\n", printc_err("sym: can't parse value: %s\n",
val_text); val_text);
return -1; return -1;
} }
if (stab_set(stab_default, name, value) < 0) if (stab_set(name, value) < 0)
return -1; return -1;
mark_modified(MODIFY_SYMS); mark_modified(MODIFY_SYMS);

View File

@ -45,7 +45,7 @@ int symmap_check(FILE *in)
return spc_count >= 2; return spc_count >= 2;
} }
int symmap_syms(FILE *in, stab_t stab) int symmap_syms(FILE *in)
{ {
rewind(in); rewind(in);
char buf[128]; char buf[128];
@ -60,7 +60,7 @@ int symmap_syms(FILE *in, stab_t stab)
if (addr && name) { if (addr && name) {
address_t addr_val = strtoul(addr, NULL, 16); address_t addr_val = strtoul(addr, NULL, 16);
if (stab_set(stab, name, addr_val) < 0) if (stab_set(name, addr_val) < 0)
return -1; return -1;
} }
} }

View File

@ -22,6 +22,6 @@
#include "binfile.h" #include "binfile.h"
int symmap_check(FILE *in); int symmap_check(FILE *in);
int symmap_syms(FILE *in, stab_t stab); int symmap_syms(FILE *in);
#endif #endif