1 |
/* |
/* |
2 |
* Copyright (C) 2003-2005 Anders Gavare. All rights reserved. |
* Copyright (C) 2003-2006 Anders Gavare. All rights reserved. |
3 |
* |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
* Redistribution and use in source and binary forms, with or without |
5 |
* modification, are permitted provided that the following conditions are met: |
* modification, are permitted provided that the following conditions are met: |
25 |
* SUCH DAMAGE. |
* SUCH DAMAGE. |
26 |
* |
* |
27 |
* |
* |
28 |
* $Id: symbol.c,v 1.26 2005/06/21 16:22:52 debug Exp $ |
* $Id: symbol.c,v 1.36 2006/07/07 19:38:55 debug Exp $ |
29 |
* |
* |
30 |
* Address to symbol translation routines. |
* Address to symbol translation routines. |
31 |
* |
* |
32 |
* This module is (probably) independant from the rest of the emulator. |
* This module is (probably) independent from the rest of the emulator. |
33 |
* symbol_init() must be called before any other function in this |
* symbol_init() must be called before any other function in this file is used. |
|
* file is used. |
|
34 |
*/ |
*/ |
35 |
|
|
36 |
#include <stdio.h> |
#include <stdio.h> |
37 |
#include <stdlib.h> |
#include <stdlib.h> |
38 |
#include <string.h> |
#include <string.h> |
39 |
|
|
|
#include "misc.h" |
|
|
|
|
40 |
#include "symbol.h" |
#include "symbol.h" |
41 |
|
|
42 |
|
|
91 |
|
|
92 |
|
|
93 |
/* |
/* |
94 |
* get_symbol_name(): |
* get_symbol_name_and_n_args(): |
95 |
* |
* |
96 |
* Translate an address into a symbol name. The return value is a pointer |
* Translate an address into a symbol name. The return value is a pointer |
97 |
* to a static char array, containing the symbol name. (In other words, |
* to a static char array, containing the symbol name. (In other words, |
104 |
* 0x1008, the symbol's name will be found in the static char array, and |
* 0x1008, the symbol's name will be found in the static char array, and |
105 |
* *offset will be set to 0x8. |
* *offset will be set to 0x8. |
106 |
* |
* |
107 |
|
* If n_argsp is non-NULL, *n_argsp is set to the symbol's n_args value. |
108 |
|
* |
109 |
* If no symbol was found, NULL is returned instead. |
* If no symbol was found, NULL is returned instead. |
110 |
*/ |
*/ |
111 |
static char symbol_buf[SYMBOLBUF_MAX+1]; |
static char symbol_buf[SYMBOLBUF_MAX+1]; |
112 |
char *get_symbol_name(struct symbol_context *sc, uint64_t addr, |
char *get_symbol_name_and_n_args(struct symbol_context *sc, uint64_t addr, |
113 |
uint64_t *offset) |
uint64_t *offset, int *n_argsp) |
114 |
{ |
{ |
115 |
struct symbol *s; |
struct symbol *s; |
116 |
int stepsize, ofs; |
int stepsize, ofs; |
140 |
(addr - s->addr)); |
(addr - s->addr)); |
141 |
if (offset != NULL) |
if (offset != NULL) |
142 |
*offset = addr - s->addr; |
*offset = addr - s->addr; |
143 |
|
if (n_argsp != NULL) |
144 |
|
*n_argsp = s->n_args; |
145 |
return symbol_buf; |
return symbol_buf; |
146 |
} |
} |
147 |
s = s->next; |
s = s->next; |
164 |
(addr - s->addr)); |
(addr - s->addr)); |
165 |
if (offset != NULL) |
if (offset != NULL) |
166 |
*offset = addr - s->addr; |
*offset = addr - s->addr; |
167 |
|
if (n_argsp != NULL) |
168 |
|
*n_argsp = s->n_args; |
169 |
return symbol_buf; |
return symbol_buf; |
170 |
} |
} |
171 |
|
|
193 |
|
|
194 |
|
|
195 |
/* |
/* |
196 |
|
* get_symbol_name(): |
197 |
|
* |
198 |
|
* See get_symbol_name_and_n_args(). |
199 |
|
*/ |
200 |
|
char *get_symbol_name(struct symbol_context *sc, uint64_t addr, uint64_t *offs) |
201 |
|
{ |
202 |
|
return get_symbol_name_and_n_args(sc, addr, offs, NULL); |
203 |
|
} |
204 |
|
|
205 |
|
|
206 |
|
/* |
207 |
* add_symbol_name(): |
* add_symbol_name(): |
208 |
* |
* |
209 |
* Add a symbol to the symbol list. |
* Add a symbol to the symbol list. |
210 |
*/ |
*/ |
211 |
void add_symbol_name(struct symbol_context *sc, |
void add_symbol_name(struct symbol_context *sc, |
212 |
uint64_t addr, uint64_t len, char *name, int type) |
uint64_t addr, uint64_t len, char *name, int type, int n_args) |
213 |
{ |
{ |
214 |
struct symbol *s; |
struct symbol *s; |
215 |
|
|
224 |
exit(1); |
exit(1); |
225 |
} |
} |
226 |
|
|
227 |
|
if (addr == 0 && strcmp(name, "_DYNAMIC_LINK") == 0) |
228 |
|
return; |
229 |
|
|
230 |
if (name[0] == '\0') |
if (name[0] == '\0') |
231 |
return; |
return; |
232 |
|
|
233 |
/* TODO: Maybe this should be optional? */ |
/* TODO: Maybe this should be optional? */ |
234 |
if (name[0] == '$') |
if (name[0] == '.' || name[0] == '$') |
235 |
return; |
return; |
236 |
|
|
237 |
|
/* Quick test-hack: */ |
238 |
|
if (n_args < 0) { |
239 |
|
if (strcmp(name, "strlen") == 0) |
240 |
|
n_args = 1; |
241 |
|
if (strcmp(name, "strcmp") == 0) |
242 |
|
n_args = 2; |
243 |
|
if (strcmp(name, "strcpy") == 0) |
244 |
|
n_args = 2; |
245 |
|
if (strcmp(name, "strncpy") == 0) |
246 |
|
n_args = 3; |
247 |
|
if (strcmp(name, "strlcpy") == 0) |
248 |
|
n_args = 3; |
249 |
|
if (strcmp(name, "strlcat") == 0) |
250 |
|
n_args = 3; |
251 |
|
if (strcmp(name, "strncmp") == 0) |
252 |
|
n_args = 3; |
253 |
|
if (strcmp(name, "memset") == 0) |
254 |
|
n_args = 3; |
255 |
|
if (strcmp(name, "memcpy") == 0) |
256 |
|
n_args = 3; |
257 |
|
if (strcmp(name, "bzero") == 0) |
258 |
|
n_args = 2; |
259 |
|
if (strcmp(name, "bcopy") == 0) |
260 |
|
n_args = 3; |
261 |
|
} |
262 |
|
|
263 |
if ((addr >> 32) == 0 && (addr & 0x80000000ULL)) |
if ((addr >> 32) == 0 && (addr & 0x80000000ULL)) |
264 |
addr |= 0xffffffff00000000ULL; |
addr |= 0xffffffff00000000ULL; |
265 |
|
|
269 |
exit(1); |
exit(1); |
270 |
} |
} |
271 |
|
|
272 |
s->name = strdup(name); |
memset(s, 0, sizeof(struct symbol)); |
273 |
|
|
274 |
|
s->name = symbol_demangle_cplusplus(name); |
275 |
|
|
276 |
if (s->name == NULL) { |
if (s->name == NULL) { |
277 |
fprintf(stderr, "out of memory\n"); |
s->name = strdup(name); |
278 |
exit(1); |
if (s->name == NULL) { |
279 |
|
fprintf(stderr, "out of memory\n"); |
280 |
|
exit(1); |
281 |
|
} |
282 |
} |
} |
283 |
s->addr = addr; |
|
284 |
s->len = len; |
s->addr = addr; |
285 |
s->type = type; |
s->len = len; |
286 |
|
s->type = type; |
287 |
|
s->n_args = n_args; |
288 |
|
|
289 |
sc->n_symbols ++; |
sc->n_symbols ++; |
290 |
|
|
343 |
if (type == 't' || type == 'r' || type == 'g') |
if (type == 't' || type == 'r' || type == 'g') |
344 |
continue; |
continue; |
345 |
|
|
346 |
add_symbol_name(sc, addr, len, b4, type); |
add_symbol_name(sc, addr, len, b4, type, -1); |
347 |
} |
} |
348 |
|
|
349 |
fclose(f); |
fclose(f); |