1 |
/* |
2 |
* Copyright (C) 2003-2005 Anders Gavare. All rights reserved. |
3 |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
5 |
* modification, are permitted provided that the following conditions are met: |
6 |
* |
7 |
* 1. Redistributions of source code must retain the above copyright |
8 |
* notice, this list of conditions and the following disclaimer. |
9 |
* 2. Redistributions in binary form must reproduce the above copyright |
10 |
* notice, this list of conditions and the following disclaimer in the |
11 |
* documentation and/or other materials provided with the distribution. |
12 |
* 3. The name of the author may not be used to endorse or promote products |
13 |
* derived from this software without specific prior written permission. |
14 |
* |
15 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
16 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
19 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
20 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
21 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
22 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
23 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
24 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
25 |
* SUCH DAMAGE. |
26 |
* |
27 |
* |
28 |
* $Id: symbol.c,v 1.29 2005/08/09 17:18:22 debug Exp $ |
29 |
* |
30 |
* Address to symbol translation routines. |
31 |
* |
32 |
* This module is (probably) independant from the rest of the emulator. |
33 |
* symbol_init() must be called before any other function in this |
34 |
* file is used. |
35 |
*/ |
36 |
|
37 |
#include <stdio.h> |
38 |
#include <stdlib.h> |
39 |
#include <string.h> |
40 |
|
41 |
#include "misc.h" |
42 |
|
43 |
#include "symbol.h" |
44 |
|
45 |
|
46 |
#define SYMBOLBUF_MAX 100 |
47 |
|
48 |
|
49 |
/* |
50 |
* symbol_nsymbols(): |
51 |
* |
52 |
* Return n_symbols. |
53 |
*/ |
54 |
int symbol_nsymbols(struct symbol_context *sc) |
55 |
{ |
56 |
return sc->n_symbols; |
57 |
} |
58 |
|
59 |
|
60 |
/* |
61 |
* get_symbol_addr(): |
62 |
* |
63 |
* Find a symbol by name. If addr is non-NULL, *addr is set to the symbol's |
64 |
* address. Return value is 1 if the symbol is found, 0 otherwise. |
65 |
* |
66 |
* NOTE: This is O(n). |
67 |
*/ |
68 |
int get_symbol_addr(struct symbol_context *sc, char *symbol, uint64_t *addr) |
69 |
{ |
70 |
struct symbol *s; |
71 |
|
72 |
if (sc->sorted_array) { |
73 |
int i; |
74 |
for (i=0; i<sc->n_symbols; i++) |
75 |
if (strcmp(symbol, sc->first_symbol[i].name) == 0) { |
76 |
if (addr != NULL) |
77 |
*addr = sc->first_symbol[i].addr; |
78 |
return 1; |
79 |
} |
80 |
} else { |
81 |
s = sc->first_symbol; |
82 |
while (s != NULL) { |
83 |
if (strcmp(symbol, s->name) == 0) { |
84 |
if (addr != NULL) |
85 |
*addr = s->addr; |
86 |
return 1; |
87 |
} |
88 |
s = s->next; |
89 |
} |
90 |
} |
91 |
|
92 |
return 0; |
93 |
} |
94 |
|
95 |
|
96 |
/* |
97 |
* get_symbol_name_and_n_args(): |
98 |
* |
99 |
* Translate an address into a symbol name. The return value is a pointer |
100 |
* to a static char array, containing the symbol name. (In other words, |
101 |
* this function is not reentrant. This removes the need for memory allocation |
102 |
* at the caller's side.) |
103 |
* |
104 |
* If offset is not a NULL pointer, *offset is set to the offset within |
105 |
* the symbol. For example, if there is a symbol at address 0x1000 with |
106 |
* length 0x100, and a caller wants to know the symbol name of address |
107 |
* 0x1008, the symbol's name will be found in the static char array, and |
108 |
* *offset will be set to 0x8. |
109 |
* |
110 |
* If n_argsp is non-NULL, *n_argsp is set to the symbol's n_args value. |
111 |
* |
112 |
* If no symbol was found, NULL is returned instead. |
113 |
*/ |
114 |
static char symbol_buf[SYMBOLBUF_MAX+1]; |
115 |
char *get_symbol_name_and_n_args(struct symbol_context *sc, uint64_t addr, |
116 |
uint64_t *offset, int *n_argsp) |
117 |
{ |
118 |
struct symbol *s; |
119 |
int stepsize, ofs; |
120 |
|
121 |
if (sc->n_symbols == 0) |
122 |
return NULL; |
123 |
|
124 |
if ((addr >> 32) == 0 && (addr & 0x80000000ULL)) |
125 |
addr |= 0xffffffff00000000ULL; |
126 |
|
127 |
symbol_buf[0] = symbol_buf[SYMBOLBUF_MAX] = '\0'; |
128 |
if (offset != NULL) |
129 |
*offset = 0; |
130 |
|
131 |
if (!sc->sorted_array) { |
132 |
/* Slow, linear O(n) search: */ |
133 |
s = sc->first_symbol; |
134 |
while (s != NULL) { |
135 |
/* Found a match? */ |
136 |
if (addr >= s->addr && addr < s->addr + s->len) { |
137 |
if (addr == s->addr) |
138 |
snprintf(symbol_buf, SYMBOLBUF_MAX, |
139 |
"%s", s->name); |
140 |
else |
141 |
snprintf(symbol_buf, SYMBOLBUF_MAX, |
142 |
"%s+0x%lx", s->name, (long) |
143 |
(addr - s->addr)); |
144 |
if (offset != NULL) |
145 |
*offset = addr - s->addr; |
146 |
if (n_argsp != NULL) |
147 |
*n_argsp = s->n_args; |
148 |
return symbol_buf; |
149 |
} |
150 |
s = s->next; |
151 |
} |
152 |
} else { |
153 |
/* Faster, O(log n) search: */ |
154 |
stepsize = sc->n_symbols / 2; |
155 |
ofs = stepsize; |
156 |
while (stepsize > 0 || (stepsize == 0 && ofs == 0)) { |
157 |
s = sc->first_symbol + ofs; |
158 |
|
159 |
/* Found a match? */ |
160 |
if (addr >= s->addr && addr < s->addr + s->len) { |
161 |
if (addr == s->addr) |
162 |
snprintf(symbol_buf, SYMBOLBUF_MAX, |
163 |
"%s", s->name); |
164 |
else |
165 |
snprintf(symbol_buf, SYMBOLBUF_MAX, |
166 |
"%s+0x%lx", s->name, (long) |
167 |
(addr - s->addr)); |
168 |
if (offset != NULL) |
169 |
*offset = addr - s->addr; |
170 |
if (n_argsp != NULL) |
171 |
*n_argsp = s->n_args; |
172 |
return symbol_buf; |
173 |
} |
174 |
|
175 |
if (ofs == 0) |
176 |
break; |
177 |
|
178 |
stepsize >>= 1; |
179 |
|
180 |
/* Special case for offset 0 (end of search in |
181 |
the Left direction */ |
182 |
if (stepsize == 0) |
183 |
ofs = 0; |
184 |
else { |
185 |
if (addr < s->addr) |
186 |
ofs -= stepsize; |
187 |
else |
188 |
ofs += stepsize; |
189 |
} |
190 |
} |
191 |
} |
192 |
|
193 |
/* Not found? Then return NULL. */ |
194 |
return NULL; |
195 |
} |
196 |
|
197 |
|
198 |
/* |
199 |
* get_symbol_name(): |
200 |
* |
201 |
* See get_symbol_name_and_n_args(). |
202 |
*/ |
203 |
char *get_symbol_name(struct symbol_context *sc, uint64_t addr, uint64_t *offs) |
204 |
{ |
205 |
return get_symbol_name_and_n_args(sc, addr, offs, NULL); |
206 |
} |
207 |
|
208 |
|
209 |
/* |
210 |
* add_symbol_name(): |
211 |
* |
212 |
* Add a symbol to the symbol list. |
213 |
*/ |
214 |
void add_symbol_name(struct symbol_context *sc, |
215 |
uint64_t addr, uint64_t len, char *name, int type, int n_args) |
216 |
{ |
217 |
struct symbol *s; |
218 |
|
219 |
if (sc->sorted_array) { |
220 |
fprintf(stderr, "add_symbol_name(): Internal error: the " |
221 |
"symbol array is already sorted\n"); |
222 |
exit(1); |
223 |
} |
224 |
|
225 |
if (name == NULL) { |
226 |
fprintf(stderr, "add_symbol_name(): name = NULL\n"); |
227 |
exit(1); |
228 |
} |
229 |
|
230 |
if (name[0] == '\0') |
231 |
return; |
232 |
|
233 |
/* TODO: Maybe this should be optional? */ |
234 |
if (name[0] == '.' || name[0] == '$') |
235 |
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, "strncmp") == 0) |
246 |
n_args = 3; |
247 |
if (strcmp(name, "memset") == 0) |
248 |
n_args = 3; |
249 |
if (strcmp(name, "memcpy") == 0) |
250 |
n_args = 3; |
251 |
if (strcmp(name, "bzero") == 0) |
252 |
n_args = 2; |
253 |
if (strcmp(name, "bcopy") == 0) |
254 |
n_args = 3; |
255 |
} |
256 |
|
257 |
if ((addr >> 32) == 0 && (addr & 0x80000000ULL)) |
258 |
addr |= 0xffffffff00000000ULL; |
259 |
|
260 |
s = malloc(sizeof(struct symbol)); |
261 |
if (s == NULL) { |
262 |
fprintf(stderr, "out of memory\n"); |
263 |
exit(1); |
264 |
} |
265 |
|
266 |
s->name = strdup(name); |
267 |
if (s->name == NULL) { |
268 |
fprintf(stderr, "out of memory\n"); |
269 |
exit(1); |
270 |
} |
271 |
s->addr = addr; |
272 |
s->len = len; |
273 |
s->type = type; |
274 |
s->n_args = n_args; |
275 |
|
276 |
sc->n_symbols ++; |
277 |
|
278 |
/* Add first in list: */ |
279 |
s->next = sc->first_symbol; |
280 |
sc->first_symbol = s; |
281 |
} |
282 |
|
283 |
|
284 |
/* |
285 |
* symbol_readfile(): |
286 |
* |
287 |
* Read 'nm -S' style symbols from a file. |
288 |
* |
289 |
* TODO: This function is an ugly hack, and should be replaced |
290 |
* with something that reads symbols directly from the executable |
291 |
* images. |
292 |
*/ |
293 |
void symbol_readfile(struct symbol_context *sc, char *fname) |
294 |
{ |
295 |
FILE *f; |
296 |
char b1[80]; uint64_t addr; |
297 |
char b2[80]; uint64_t len; |
298 |
char b3[80]; int type; |
299 |
char b4[80]; |
300 |
int cur_n_symbols = sc->n_symbols; |
301 |
|
302 |
f = fopen(fname, "r"); |
303 |
if (f == NULL) { |
304 |
perror(fname); |
305 |
exit(1); |
306 |
} |
307 |
|
308 |
while (!feof(f)) { |
309 |
memset(b1, 0, sizeof(b1)); |
310 |
memset(b2, 0, sizeof(b2)); |
311 |
memset(b3, 0, sizeof(b3)); |
312 |
memset(b4, 0, sizeof(b4)); |
313 |
fscanf(f, "%s %s\n", b1,b2); |
314 |
if (strlen(b2) < 2 && !(b2[0]>='0' && b2[0]<='9')) { |
315 |
strlcpy(b3, b2, sizeof(b3)); |
316 |
strlcpy(b2, "0", sizeof(b2)); |
317 |
fscanf(f, "%s\n", b4); |
318 |
} else { |
319 |
fscanf(f, "%s %s\n", b3,b4); |
320 |
} |
321 |
|
322 |
/* printf("b1='%s' b2='%s' b3='%s' b4='%s'\n", |
323 |
b1,b2,b3,b4); */ |
324 |
addr = strtoull(b1, NULL, 16); |
325 |
len = strtoull(b2, NULL, 16); |
326 |
type = b3[0]; |
327 |
/* printf("addr=%016llx len=%016llx type=%i\n", |
328 |
addr, len, type); */ |
329 |
|
330 |
if (type == 't' || type == 'r' || type == 'g') |
331 |
continue; |
332 |
|
333 |
add_symbol_name(sc, addr, len, b4, type, -1); |
334 |
} |
335 |
|
336 |
fclose(f); |
337 |
|
338 |
debug("%i symbols\n", sc->n_symbols - cur_n_symbols); |
339 |
} |
340 |
|
341 |
|
342 |
/* |
343 |
* sym_addr_compare(): |
344 |
* |
345 |
* Helper function for sorting symbols according to their address. |
346 |
*/ |
347 |
int sym_addr_compare(const void *a, const void *b) |
348 |
{ |
349 |
struct symbol *p1 = (struct symbol *) a; |
350 |
struct symbol *p2 = (struct symbol *) b; |
351 |
|
352 |
if (p1->addr < p2->addr) |
353 |
return -1; |
354 |
if (p1->addr > p2->addr) |
355 |
return 1; |
356 |
|
357 |
return 0; |
358 |
} |
359 |
|
360 |
|
361 |
/* |
362 |
* symbol_recalc_sizes(): |
363 |
* |
364 |
* Recalculate sizes of symbols that have size = 0, by creating an array |
365 |
* containing all symbols, qsort()-ing that array according to address, and |
366 |
* recalculating the size fields if necessary. |
367 |
*/ |
368 |
void symbol_recalc_sizes(struct symbol_context *sc) |
369 |
{ |
370 |
struct symbol *tmp_array; |
371 |
struct symbol *last_ptr; |
372 |
struct symbol *tmp_ptr; |
373 |
int i; |
374 |
|
375 |
tmp_array = malloc(sizeof (struct symbol) * sc->n_symbols); |
376 |
if (tmp_array == NULL) { |
377 |
fprintf(stderr, "out of memory\n"); |
378 |
exit(1); |
379 |
} |
380 |
|
381 |
/* Copy first_symbol --> tmp_array, and remove the old |
382 |
first_symbol at the same time: */ |
383 |
tmp_ptr = sc->first_symbol; |
384 |
i = 0; |
385 |
while (tmp_ptr != NULL) { |
386 |
tmp_array[i] = *tmp_ptr; |
387 |
last_ptr = tmp_ptr; |
388 |
tmp_ptr = tmp_ptr->next; |
389 |
free(last_ptr); |
390 |
i++; |
391 |
} |
392 |
|
393 |
qsort(tmp_array, sc->n_symbols, sizeof(struct symbol), |
394 |
sym_addr_compare); |
395 |
sc->sorted_array = 1; |
396 |
|
397 |
/* Recreate the first_symbol chain: */ |
398 |
sc->first_symbol = NULL; |
399 |
for (i=0; i<sc->n_symbols; i++) { |
400 |
/* Recalculate size, if 0: */ |
401 |
if (tmp_array[i].len == 0) { |
402 |
uint64_t len; |
403 |
if (i != sc->n_symbols-1) |
404 |
len = tmp_array[i+1].addr |
405 |
- tmp_array[i].addr; |
406 |
else |
407 |
len = 1; |
408 |
tmp_array[i].len = len; |
409 |
} |
410 |
|
411 |
tmp_array[i].next = &tmp_array[i+1]; |
412 |
} |
413 |
|
414 |
sc->first_symbol = tmp_array; |
415 |
} |
416 |
|
417 |
|
418 |
/* |
419 |
* symbol_init(): |
420 |
* |
421 |
* Initialize the symbol hashtables. |
422 |
*/ |
423 |
void symbol_init(struct symbol_context *sc) |
424 |
{ |
425 |
sc->first_symbol = NULL; |
426 |
sc->sorted_array = 0; |
427 |
sc->n_symbols = 0; |
428 |
} |
429 |
|