/[gxemul]/upstream/0.3.7/src/symbol.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /upstream/0.3.7/src/symbol.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 21 - (show annotations)
Mon Oct 8 16:19:28 2007 UTC (16 years, 6 months ago) by dpavlin
File MIME type: text/plain
File size: 10360 byte(s)
0.3.7
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.33 2005/10/26 14:37:02 debug Exp $
29 *
30 * Address to symbol translation routines.
31 *
32 * This module is (probably) independent from the rest of the emulator.
33 * symbol_init() must be called before any other function in this file is used.
34 */
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 #include "misc.h"
41
42 #include "symbol.h"
43
44
45 #define SYMBOLBUF_MAX 100
46
47
48 /*
49 * symbol_nsymbols():
50 *
51 * Return n_symbols.
52 */
53 int symbol_nsymbols(struct symbol_context *sc)
54 {
55 return sc->n_symbols;
56 }
57
58
59 /*
60 * get_symbol_addr():
61 *
62 * Find a symbol by name. If addr is non-NULL, *addr is set to the symbol's
63 * address. Return value is 1 if the symbol is found, 0 otherwise.
64 *
65 * NOTE: This is O(n).
66 */
67 int get_symbol_addr(struct symbol_context *sc, char *symbol, uint64_t *addr)
68 {
69 struct symbol *s;
70
71 if (sc->sorted_array) {
72 int i;
73 for (i=0; i<sc->n_symbols; i++)
74 if (strcmp(symbol, sc->first_symbol[i].name) == 0) {
75 if (addr != NULL)
76 *addr = sc->first_symbol[i].addr;
77 return 1;
78 }
79 } else {
80 s = sc->first_symbol;
81 while (s != NULL) {
82 if (strcmp(symbol, s->name) == 0) {
83 if (addr != NULL)
84 *addr = s->addr;
85 return 1;
86 }
87 s = s->next;
88 }
89 }
90
91 return 0;
92 }
93
94
95 /*
96 * get_symbol_name_and_n_args():
97 *
98 * Translate an address into a symbol name. The return value is a pointer
99 * to a static char array, containing the symbol name. (In other words,
100 * this function is not reentrant. This removes the need for memory allocation
101 * at the caller's side.)
102 *
103 * If offset is not a NULL pointer, *offset is set to the offset within
104 * the symbol. For example, if there is a symbol at address 0x1000 with
105 * length 0x100, and a caller wants to know the symbol name of address
106 * 0x1008, the symbol's name will be found in the static char array, and
107 * *offset will be set to 0x8.
108 *
109 * If n_argsp is non-NULL, *n_argsp is set to the symbol's n_args value.
110 *
111 * If no symbol was found, NULL is returned instead.
112 */
113 static char symbol_buf[SYMBOLBUF_MAX+1];
114 char *get_symbol_name_and_n_args(struct symbol_context *sc, uint64_t addr,
115 uint64_t *offset, int *n_argsp)
116 {
117 struct symbol *s;
118 int stepsize, ofs;
119
120 if (sc->n_symbols == 0)
121 return NULL;
122
123 if ((addr >> 32) == 0 && (addr & 0x80000000ULL))
124 addr |= 0xffffffff00000000ULL;
125
126 symbol_buf[0] = symbol_buf[SYMBOLBUF_MAX] = '\0';
127 if (offset != NULL)
128 *offset = 0;
129
130 if (!sc->sorted_array) {
131 /* Slow, linear O(n) search: */
132 s = sc->first_symbol;
133 while (s != NULL) {
134 /* Found a match? */
135 if (addr >= s->addr && addr < s->addr + s->len) {
136 if (addr == s->addr)
137 snprintf(symbol_buf, SYMBOLBUF_MAX,
138 "%s", s->name);
139 else
140 snprintf(symbol_buf, SYMBOLBUF_MAX,
141 "%s+0x%lx", s->name, (long)
142 (addr - s->addr));
143 if (offset != NULL)
144 *offset = addr - s->addr;
145 if (n_argsp != NULL)
146 *n_argsp = s->n_args;
147 return symbol_buf;
148 }
149 s = s->next;
150 }
151 } else {
152 /* Faster, O(log n) search: */
153 stepsize = sc->n_symbols / 2;
154 ofs = stepsize;
155 while (stepsize > 0 || (stepsize == 0 && ofs == 0)) {
156 s = sc->first_symbol + ofs;
157
158 /* Found a match? */
159 if (addr >= s->addr && addr < s->addr + s->len) {
160 if (addr == s->addr)
161 snprintf(symbol_buf, SYMBOLBUF_MAX,
162 "%s", s->name);
163 else
164 snprintf(symbol_buf, SYMBOLBUF_MAX,
165 "%s+0x%lx", s->name, (long)
166 (addr - s->addr));
167 if (offset != NULL)
168 *offset = addr - s->addr;
169 if (n_argsp != NULL)
170 *n_argsp = s->n_args;
171 return symbol_buf;
172 }
173
174 if (ofs == 0)
175 break;
176
177 stepsize >>= 1;
178
179 /* Special case for offset 0 (end of search in
180 the Left direction */
181 if (stepsize == 0)
182 ofs = 0;
183 else {
184 if (addr < s->addr)
185 ofs -= stepsize;
186 else
187 ofs += stepsize;
188 }
189 }
190 }
191
192 /* Not found? Then return NULL. */
193 return NULL;
194 }
195
196
197 /*
198 * get_symbol_name():
199 *
200 * See get_symbol_name_and_n_args().
201 */
202 char *get_symbol_name(struct symbol_context *sc, uint64_t addr, uint64_t *offs)
203 {
204 return get_symbol_name_and_n_args(sc, addr, offs, NULL);
205 }
206
207
208 /*
209 * add_symbol_name():
210 *
211 * Add a symbol to the symbol list.
212 */
213 void add_symbol_name(struct symbol_context *sc,
214 uint64_t addr, uint64_t len, char *name, int type, int n_args)
215 {
216 struct symbol *s;
217
218 if (sc->sorted_array) {
219 fprintf(stderr, "add_symbol_name(): Internal error: the "
220 "symbol array is already sorted\n");
221 exit(1);
222 }
223
224 if (name == NULL) {
225 fprintf(stderr, "add_symbol_name(): name = NULL\n");
226 exit(1);
227 }
228
229 if (name[0] == '\0')
230 return;
231
232 /* TODO: Maybe this should be optional? */
233 if (name[0] == '.' || name[0] == '$')
234 return;
235
236 /* Quick test-hack: */
237 if (n_args < 0) {
238 if (strcmp(name, "strlen") == 0)
239 n_args = 1;
240 if (strcmp(name, "strcmp") == 0)
241 n_args = 2;
242 if (strcmp(name, "strcpy") == 0)
243 n_args = 2;
244 if (strcmp(name, "strncpy") == 0)
245 n_args = 3;
246 if (strcmp(name, "strlcpy") == 0)
247 n_args = 3;
248 if (strcmp(name, "strlcat") == 0)
249 n_args = 3;
250 if (strcmp(name, "strncmp") == 0)
251 n_args = 3;
252 if (strcmp(name, "memset") == 0)
253 n_args = 3;
254 if (strcmp(name, "memcpy") == 0)
255 n_args = 3;
256 if (strcmp(name, "bzero") == 0)
257 n_args = 2;
258 if (strcmp(name, "bcopy") == 0)
259 n_args = 3;
260 }
261
262 if ((addr >> 32) == 0 && (addr & 0x80000000ULL))
263 addr |= 0xffffffff00000000ULL;
264
265 s = malloc(sizeof(struct symbol));
266 if (s == NULL) {
267 fprintf(stderr, "out of memory\n");
268 exit(1);
269 }
270
271 s->name = strdup(name);
272 if (s->name == NULL) {
273 fprintf(stderr, "out of memory\n");
274 exit(1);
275 }
276 s->addr = addr;
277 s->len = len;
278 s->type = type;
279 s->n_args = n_args;
280
281 sc->n_symbols ++;
282
283 /* Add first in list: */
284 s->next = sc->first_symbol;
285 sc->first_symbol = s;
286 }
287
288
289 /*
290 * symbol_readfile():
291 *
292 * Read 'nm -S' style symbols from a file.
293 *
294 * TODO: This function is an ugly hack, and should be replaced
295 * with something that reads symbols directly from the executable
296 * images.
297 */
298 void symbol_readfile(struct symbol_context *sc, char *fname)
299 {
300 FILE *f;
301 char b1[80]; uint64_t addr;
302 char b2[80]; uint64_t len;
303 char b3[80]; int type;
304 char b4[80];
305 int cur_n_symbols = sc->n_symbols;
306
307 f = fopen(fname, "r");
308 if (f == NULL) {
309 perror(fname);
310 exit(1);
311 }
312
313 while (!feof(f)) {
314 memset(b1, 0, sizeof(b1));
315 memset(b2, 0, sizeof(b2));
316 memset(b3, 0, sizeof(b3));
317 memset(b4, 0, sizeof(b4));
318 fscanf(f, "%s %s\n", b1,b2);
319 if (strlen(b2) < 2 && !(b2[0]>='0' && b2[0]<='9')) {
320 strlcpy(b3, b2, sizeof(b3));
321 strlcpy(b2, "0", sizeof(b2));
322 fscanf(f, "%s\n", b4);
323 } else {
324 fscanf(f, "%s %s\n", b3,b4);
325 }
326
327 /* printf("b1='%s' b2='%s' b3='%s' b4='%s'\n",
328 b1,b2,b3,b4); */
329 addr = strtoull(b1, NULL, 16);
330 len = strtoull(b2, NULL, 16);
331 type = b3[0];
332 /* printf("addr=%016llx len=%016llx type=%i\n",
333 addr, len, type); */
334
335 if (type == 't' || type == 'r' || type == 'g')
336 continue;
337
338 add_symbol_name(sc, addr, len, b4, type, -1);
339 }
340
341 fclose(f);
342
343 debug("%i symbols\n", sc->n_symbols - cur_n_symbols);
344 }
345
346
347 /*
348 * sym_addr_compare():
349 *
350 * Helper function for sorting symbols according to their address.
351 */
352 int sym_addr_compare(const void *a, const void *b)
353 {
354 struct symbol *p1 = (struct symbol *) a;
355 struct symbol *p2 = (struct symbol *) b;
356
357 if (p1->addr < p2->addr)
358 return -1;
359 if (p1->addr > p2->addr)
360 return 1;
361
362 return 0;
363 }
364
365
366 /*
367 * symbol_recalc_sizes():
368 *
369 * Recalculate sizes of symbols that have size = 0, by creating an array
370 * containing all symbols, qsort()-ing that array according to address, and
371 * recalculating the size fields if necessary.
372 */
373 void symbol_recalc_sizes(struct symbol_context *sc)
374 {
375 struct symbol *tmp_array;
376 struct symbol *last_ptr;
377 struct symbol *tmp_ptr;
378 int i;
379
380 tmp_array = malloc(sizeof (struct symbol) * sc->n_symbols);
381 if (tmp_array == NULL) {
382 fprintf(stderr, "out of memory\n");
383 exit(1);
384 }
385
386 /* Copy first_symbol --> tmp_array, and remove the old
387 first_symbol at the same time: */
388 tmp_ptr = sc->first_symbol;
389 i = 0;
390 while (tmp_ptr != NULL) {
391 tmp_array[i] = *tmp_ptr;
392 last_ptr = tmp_ptr;
393 tmp_ptr = tmp_ptr->next;
394 free(last_ptr);
395 i++;
396 }
397
398 qsort(tmp_array, sc->n_symbols, sizeof(struct symbol),
399 sym_addr_compare);
400 sc->sorted_array = 1;
401
402 /* Recreate the first_symbol chain: */
403 sc->first_symbol = NULL;
404 for (i=0; i<sc->n_symbols; i++) {
405 /* Recalculate size, if 0: */
406 if (tmp_array[i].len == 0) {
407 uint64_t len;
408 if (i != sc->n_symbols-1)
409 len = tmp_array[i+1].addr
410 - tmp_array[i].addr;
411 else
412 len = 1;
413 tmp_array[i].len = len;
414 }
415
416 tmp_array[i].next = &tmp_array[i+1];
417 }
418
419 sc->first_symbol = tmp_array;
420 }
421
422
423 /*
424 * symbol_init():
425 *
426 * Initialize the symbol hashtables.
427 */
428 void symbol_init(struct symbol_context *sc)
429 {
430 sc->first_symbol = NULL;
431 sc->sorted_array = 0;
432 sc->n_symbols = 0;
433 }
434

  ViewVC Help
Powered by ViewVC 1.1.26