2010-04-10 02:00:00 +00:00
|
|
|
/* MSPDebug - debugging tool for the eZ430
|
|
|
|
* Copyright (C) 2009, 2010 Daniel Beer
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2010-05-13 00:57:21 +00:00
|
|
|
#include <stdint.h>
|
2010-04-10 02:00:00 +00:00
|
|
|
#include <regex.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include "stab.h"
|
2010-05-01 05:54:10 +00:00
|
|
|
#include "expr.h"
|
2010-04-10 02:00:00 +00:00
|
|
|
#include "binfile.h"
|
|
|
|
#include "util.h"
|
2010-08-16 23:07:03 +00:00
|
|
|
#include "output.h"
|
2010-04-20 23:18:56 +00:00
|
|
|
#include "vector.h"
|
2010-04-21 04:56:56 +00:00
|
|
|
#include "sym.h"
|
2010-08-13 04:40:58 +00:00
|
|
|
#include "reader.h"
|
2010-04-10 02:00:00 +00:00
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
int cmd_eval(char **arg)
|
2010-04-10 02:00:00 +00:00
|
|
|
{
|
2010-08-05 01:59:33 +00:00
|
|
|
address_t addr;
|
|
|
|
address_t offset;
|
2010-04-10 02:00:00 +00:00
|
|
|
char name[64];
|
|
|
|
|
2010-08-13 00:12:50 +00:00
|
|
|
if (expr_eval(stab_default, *arg, &addr) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("=: can't parse: %s\n", *arg);
|
2010-04-10 02:00:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-08-16 23:07:03 +00:00
|
|
|
printc("0x%05x", addr);
|
2010-08-13 00:12:50 +00:00
|
|
|
if (!stab_nearest(stab_default, addr, name, sizeof(name), &offset)) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc(" = %s", name);
|
2010-04-10 02:00:00 +00:00
|
|
|
if (offset)
|
2010-08-16 23:07:03 +00:00
|
|
|
printc("+0x%x", offset);
|
2010-04-10 02:00:00 +00:00
|
|
|
}
|
2010-08-16 23:07:03 +00:00
|
|
|
printc("\n");
|
2010-04-10 02:00:00 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
static int cmd_sym_load_add(int clear, char **arg)
|
2010-04-10 02:00:00 +00:00
|
|
|
{
|
|
|
|
FILE *in;
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
if (clear && prompt_abort(MODIFY_SYMS))
|
2010-04-10 02:00:00 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
in = fopen(*arg, "r");
|
|
|
|
if (!in) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("sym: %s: %s\n", *arg, strerror(errno));
|
2010-04-10 02:00:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-05-15 03:13:58 +00:00
|
|
|
if (clear) {
|
2010-08-13 00:12:50 +00:00
|
|
|
stab_clear(stab_default);
|
2010-08-13 04:40:58 +00:00
|
|
|
unmark_modified(MODIFY_SYMS);
|
2010-05-15 03:13:58 +00:00
|
|
|
} else {
|
2010-08-13 04:40:58 +00:00
|
|
|
mark_modified(MODIFY_SYMS);
|
2010-05-15 03:13:58 +00:00
|
|
|
}
|
2010-04-10 02:00:00 +00:00
|
|
|
|
2010-08-13 00:12:50 +00:00
|
|
|
if (binfile_syms(in, stab_default) < 0) {
|
2010-05-15 03:13:58 +00:00
|
|
|
fclose(in);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(in);
|
|
|
|
return 0;
|
2010-04-10 02:00:00 +00:00
|
|
|
}
|
|
|
|
|
2010-08-05 01:59:33 +00:00
|
|
|
static int savemap_cb(void *user_data, const char *name, address_t value)
|
2010-04-10 02:00:00 +00:00
|
|
|
{
|
2010-04-30 10:17:43 +00:00
|
|
|
FILE *savemap_out = (FILE *)user_data;
|
|
|
|
|
2010-04-10 02:00:00 +00:00
|
|
|
if (fprintf(savemap_out, "%04x t %s\n", value, name) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
pr_error("sym: can't write to file");
|
2010-04-10 02:00:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
static int cmd_sym_savemap(char **arg)
|
2010-04-10 02:00:00 +00:00
|
|
|
{
|
2010-04-30 10:17:43 +00:00
|
|
|
FILE *savemap_out;
|
2010-04-10 02:00:00 +00:00
|
|
|
char *fname = get_arg(arg);
|
|
|
|
|
|
|
|
if (!fname) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("sym: filename required to save map\n");
|
2010-04-10 02:00:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
savemap_out = fopen(fname, "w");
|
|
|
|
if (!savemap_out) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("sym: couldn't write to %s: %s\n", fname,
|
2010-04-10 02:00:00 +00:00
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-08-13 00:12:50 +00:00
|
|
|
if (stab_enum(stab_default, savemap_cb, savemap_out) < 0) {
|
2010-04-10 02:00:00 +00:00
|
|
|
fclose(savemap_out);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fclose(savemap_out) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("sym: error on close: %s\n", strerror(errno));
|
2010-04-10 02:00:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
unmark_modified(MODIFY_SYMS);
|
2010-04-10 02:00:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-05 01:59:33 +00:00
|
|
|
static int print_sym(void *user_data, const char *name, address_t value)
|
2010-04-10 02:00:00 +00:00
|
|
|
{
|
2010-08-16 23:07:03 +00:00
|
|
|
printc("0x%04x: %s\n", value, name);
|
2010-04-10 02:00:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-05 01:59:33 +00:00
|
|
|
static int find_sym(void *user_data, const char *name, address_t value)
|
2010-04-10 02:00:00 +00:00
|
|
|
{
|
2010-04-30 10:17:43 +00:00
|
|
|
regex_t *find_preg = (regex_t *)user_data;
|
|
|
|
|
|
|
|
if (!regexec(find_preg, name, 0, NULL, 0))
|
2010-08-16 23:07:03 +00:00
|
|
|
printc("0x%04x: %s\n", value, name);
|
2010-04-10 02:00:00 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
static int cmd_sym_find(char **arg)
|
2010-04-10 02:00:00 +00:00
|
|
|
{
|
2010-04-30 10:17:43 +00:00
|
|
|
regex_t find_preg;
|
2010-04-10 02:00:00 +00:00
|
|
|
char *expr = get_arg(arg);
|
|
|
|
|
|
|
|
if (!expr) {
|
2010-08-13 00:12:50 +00:00
|
|
|
stab_enum(stab_default, print_sym, NULL);
|
2010-04-10 02:00:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (regcomp(&find_preg, expr, REG_EXTENDED | REG_NOSUB)) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("sym: failed to compile: %s\n", expr);
|
2010-04-10 02:00:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-08-13 00:12:50 +00:00
|
|
|
stab_enum(stab_default, find_sym, &find_preg);
|
2010-04-10 02:00:00 +00:00
|
|
|
regfree(&find_preg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct rename_record {
|
|
|
|
char old_name[64];
|
|
|
|
int start, end;
|
|
|
|
};
|
|
|
|
|
2010-04-30 10:17:43 +00:00
|
|
|
struct rename_data {
|
|
|
|
struct vector list;
|
|
|
|
regex_t preg;
|
|
|
|
};
|
2010-04-10 02:00:00 +00:00
|
|
|
|
2010-08-13 00:12:50 +00:00
|
|
|
static int renames_do(struct rename_data *rename, const char *replace)
|
2010-04-10 02:00:00 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int count = 0;
|
|
|
|
|
2010-04-30 10:17:43 +00:00
|
|
|
for (i = 0; i < rename->list.size; i++) {
|
2010-04-20 23:18:56 +00:00
|
|
|
struct rename_record *r =
|
2010-04-30 10:17:43 +00:00
|
|
|
VECTOR_PTR(rename->list, i, struct rename_record);
|
2010-04-10 02:00:00 +00:00
|
|
|
char new_name[128];
|
|
|
|
int len = r->start;
|
2010-08-05 01:59:33 +00:00
|
|
|
address_t value;
|
2010-04-10 02:00:00 +00:00
|
|
|
|
|
|
|
if (len + 1 > sizeof(new_name))
|
|
|
|
len = sizeof(new_name) - 1;
|
|
|
|
|
|
|
|
memcpy(new_name, r->old_name, len);
|
|
|
|
snprintf(new_name + len,
|
|
|
|
sizeof(new_name) - len,
|
|
|
|
"%s%s", replace, r->old_name + r->end);
|
|
|
|
|
2010-08-16 23:07:03 +00:00
|
|
|
printc("%s -> %s\n", r->old_name, new_name);
|
2010-04-10 02:00:00 +00:00
|
|
|
|
2010-08-13 00:12:50 +00:00
|
|
|
if (stab_get(stab_default, r->old_name, &value) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("sym: warning: "
|
2010-04-10 02:00:00 +00:00
|
|
|
"symbol missing: %s\n",
|
|
|
|
r->old_name);
|
|
|
|
} else {
|
2010-08-13 00:12:50 +00:00
|
|
|
stab_del(stab_default, r->old_name);
|
|
|
|
if (stab_set(stab_default, new_name, value) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("sym: warning: "
|
2010-04-10 02:00:00 +00:00
|
|
|
"failed to set new name: %s\n",
|
|
|
|
new_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
2010-08-16 23:07:03 +00:00
|
|
|
printc("%d symbols renamed\n", count);
|
2010-04-10 02:00:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-05 01:59:33 +00:00
|
|
|
static int find_renames(void *user_data, const char *name, address_t value)
|
2010-04-10 02:00:00 +00:00
|
|
|
{
|
2010-04-30 10:17:43 +00:00
|
|
|
struct rename_data *rename = (struct rename_data *)user_data;
|
2010-04-10 02:00:00 +00:00
|
|
|
regmatch_t pmatch;
|
|
|
|
|
2010-04-30 10:17:43 +00:00
|
|
|
if (!regexec(&rename->preg, name, 1, &pmatch, 0) &&
|
2010-04-20 23:18:56 +00:00
|
|
|
pmatch.rm_so >= 0 && pmatch.rm_eo > pmatch.rm_so) {
|
|
|
|
struct rename_record r;
|
|
|
|
|
|
|
|
strncpy(r.old_name, name, sizeof(r.old_name));
|
|
|
|
r.old_name[sizeof(r.old_name) - 1] = 0;
|
|
|
|
r.start = pmatch.rm_so;
|
|
|
|
r.end = pmatch.rm_eo;
|
|
|
|
|
2010-04-30 10:17:43 +00:00
|
|
|
return vector_push(&rename->list, &r, 1);
|
2010-04-20 23:18:56 +00:00
|
|
|
}
|
2010-04-10 02:00:00 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
static int cmd_sym_rename(char **arg)
|
2010-04-10 02:00:00 +00:00
|
|
|
{
|
|
|
|
const char *expr = get_arg(arg);
|
|
|
|
const char *replace = get_arg(arg);
|
|
|
|
int ret;
|
2010-04-30 10:17:43 +00:00
|
|
|
struct rename_data rename;
|
2010-04-10 02:00:00 +00:00
|
|
|
|
|
|
|
if (!(expr && replace)) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("sym: expected pattern and replacement\n");
|
2010-04-10 02:00:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-04-30 10:17:43 +00:00
|
|
|
if (regcomp(&rename.preg, expr, REG_EXTENDED)) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("sym: failed to compile: %s\n", expr);
|
2010-04-10 02:00:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-04-30 10:17:43 +00:00
|
|
|
vector_init(&rename.list, sizeof(struct rename_record));
|
2010-04-20 23:18:56 +00:00
|
|
|
|
2010-08-13 00:12:50 +00:00
|
|
|
if (stab_enum(stab_default, find_renames, &rename) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("sym: rename failed\n");
|
2010-04-30 10:17:43 +00:00
|
|
|
regfree(&rename.preg);
|
|
|
|
vector_destroy(&rename.list);
|
2010-04-10 02:00:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-04-30 10:17:43 +00:00
|
|
|
regfree(&rename.preg);
|
2010-08-13 00:12:50 +00:00
|
|
|
ret = renames_do(&rename, replace);
|
2010-04-30 10:17:43 +00:00
|
|
|
vector_destroy(&rename.list);
|
2010-04-21 04:56:56 +00:00
|
|
|
|
|
|
|
if (ret > 0)
|
2010-08-13 04:40:58 +00:00
|
|
|
mark_modified(MODIFY_SYMS);
|
2010-04-21 04:56:56 +00:00
|
|
|
|
|
|
|
return ret >= 0 ? 0 : -1;
|
2010-04-10 02:00:00 +00:00
|
|
|
}
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
static int cmd_sym_del(char **arg)
|
2010-04-10 02:00:00 +00:00
|
|
|
{
|
|
|
|
char *name = get_arg(arg);
|
|
|
|
|
|
|
|
if (!name) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("sym: need a name to delete "
|
2010-04-10 02:00:00 +00:00
|
|
|
"symbol table entries\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-08-13 00:12:50 +00:00
|
|
|
if (stab_del(stab_default, name) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("sym: can't delete nonexistent symbol: %s\n",
|
2010-04-10 02:00:00 +00:00
|
|
|
name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
mark_modified(MODIFY_SYMS);
|
2010-04-10 02:00:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
int cmd_sym(char **arg)
|
2010-04-10 02:00:00 +00:00
|
|
|
{
|
|
|
|
char *subcmd = get_arg(arg);
|
|
|
|
|
|
|
|
if (!subcmd) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("sym: need to specify a subcommand "
|
2010-04-10 02:00:00 +00:00
|
|
|
"(try \"help sym\")\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp(subcmd, "clear")) {
|
2010-08-13 04:40:58 +00:00
|
|
|
if (prompt_abort(MODIFY_SYMS))
|
2010-04-10 02:00:00 +00:00
|
|
|
return 0;
|
2010-08-13 00:12:50 +00:00
|
|
|
stab_clear(stab_default);
|
2010-08-13 04:40:58 +00:00
|
|
|
unmark_modified(MODIFY_SYMS);
|
2010-04-10 02:00:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp(subcmd, "set")) {
|
|
|
|
char *name = get_arg(arg);
|
|
|
|
char *val_text = get_arg(arg);
|
2010-08-05 01:59:33 +00:00
|
|
|
address_t value;
|
2010-04-10 02:00:00 +00:00
|
|
|
|
|
|
|
if (!(name && val_text)) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("sym: need a name and value to set "
|
2010-04-10 02:00:00 +00:00
|
|
|
"symbol table entries\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-08-13 00:12:50 +00:00
|
|
|
if (expr_eval(stab_default, val_text, &value) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("sym: can't parse value: %s\n",
|
2010-04-10 02:00:00 +00:00
|
|
|
val_text);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-08-13 00:12:50 +00:00
|
|
|
if (stab_set(stab_default, name, value) < 0)
|
2010-04-10 02:00:00 +00:00
|
|
|
return -1;
|
|
|
|
|
2010-08-13 04:40:58 +00:00
|
|
|
mark_modified(MODIFY_SYMS);
|
2010-04-10 02:00:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcasecmp(subcmd, "del"))
|
2010-08-13 04:40:58 +00:00
|
|
|
return cmd_sym_del(arg);
|
2010-04-10 02:00:00 +00:00
|
|
|
if (!strcasecmp(subcmd, "import"))
|
2010-08-13 04:40:58 +00:00
|
|
|
return cmd_sym_load_add(1, arg);
|
2010-04-10 02:00:00 +00:00
|
|
|
if (!strcasecmp(subcmd, "import+"))
|
2010-08-13 04:40:58 +00:00
|
|
|
return cmd_sym_load_add(0, arg);
|
2010-04-10 02:00:00 +00:00
|
|
|
if (!strcasecmp(subcmd, "export"))
|
2010-08-13 04:40:58 +00:00
|
|
|
return cmd_sym_savemap(arg);
|
2010-04-10 02:00:00 +00:00
|
|
|
if (!strcasecmp(subcmd, "rename"))
|
2010-08-13 04:40:58 +00:00
|
|
|
return cmd_sym_rename(arg);
|
2010-04-10 02:00:00 +00:00
|
|
|
if (!strcasecmp(subcmd, "find"))
|
2010-08-13 04:40:58 +00:00
|
|
|
return cmd_sym_find(arg);
|
2010-04-10 02:00:00 +00:00
|
|
|
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("sym: unknown subcommand: %s\n", subcmd);
|
2010-04-10 02:00:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|