/[gxemul]/upstream/0.3.6/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

Annotation of /upstream/0.3.6/src/symbol.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 15 - (hide annotations)
Mon Oct 8 16:18:56 2007 UTC (16 years, 8 months ago) by dpavlin
File MIME type: text/plain
File size: 10364 byte(s)
0.3.6
1 dpavlin 2 /*
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 dpavlin 14 * $Id: symbol.c,v 1.32 2005/09/26 00:08:02 debug Exp $
29 dpavlin 2 *
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 dpavlin 12 * get_symbol_name_and_n_args():
98 dpavlin 2 *
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 dpavlin 12 * If n_argsp is non-NULL, *n_argsp is set to the symbol's n_args value.
111     *
112 dpavlin 2 * If no symbol was found, NULL is returned instead.
113     */
114     static char symbol_buf[SYMBOLBUF_MAX+1];
115 dpavlin 12 char *get_symbol_name_and_n_args(struct symbol_context *sc, uint64_t addr,
116     uint64_t *offset, int *n_argsp)
117 dpavlin 2 {
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 dpavlin 12 if (n_argsp != NULL)
147     *n_argsp = s->n_args;
148 dpavlin 2 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 dpavlin 12 if (n_argsp != NULL)
171     *n_argsp = s->n_args;
172 dpavlin 2 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 dpavlin 12 * 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 dpavlin 2 * add_symbol_name():
211     *
212     * Add a symbol to the symbol list.
213     */
214     void add_symbol_name(struct symbol_context *sc,
215 dpavlin 12 uint64_t addr, uint64_t len, char *name, int type, int n_args)
216 dpavlin 2 {
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 dpavlin 4 if (name == NULL) {
226     fprintf(stderr, "add_symbol_name(): name = NULL\n");
227     exit(1);
228     }
229    
230 dpavlin 10 if (name[0] == '\0')
231     return;
232    
233 dpavlin 4 /* TODO: Maybe this should be optional? */
234 dpavlin 12 if (name[0] == '.' || name[0] == '$')
235 dpavlin 4 return;
236    
237 dpavlin 12 /* 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 dpavlin 14 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 dpavlin 12 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 dpavlin 2 if ((addr >> 32) == 0 && (addr & 0x80000000ULL))
264     addr |= 0xffffffff00000000ULL;
265    
266     s = malloc(sizeof(struct symbol));
267     if (s == NULL) {
268     fprintf(stderr, "out of memory\n");
269     exit(1);
270     }
271    
272     s->name = strdup(name);
273     if (s->name == NULL) {
274     fprintf(stderr, "out of memory\n");
275     exit(1);
276     }
277 dpavlin 12 s->addr = addr;
278     s->len = len;
279     s->type = type;
280     s->n_args = n_args;
281 dpavlin 2
282     sc->n_symbols ++;
283    
284     /* Add first in list: */
285     s->next = sc->first_symbol;
286     sc->first_symbol = s;
287     }
288    
289    
290     /*
291     * symbol_readfile():
292     *
293     * Read 'nm -S' style symbols from a file.
294     *
295     * TODO: This function is an ugly hack, and should be replaced
296     * with something that reads symbols directly from the executable
297     * images.
298     */
299     void symbol_readfile(struct symbol_context *sc, char *fname)
300     {
301     FILE *f;
302     char b1[80]; uint64_t addr;
303     char b2[80]; uint64_t len;
304     char b3[80]; int type;
305     char b4[80];
306     int cur_n_symbols = sc->n_symbols;
307    
308     f = fopen(fname, "r");
309     if (f == NULL) {
310     perror(fname);
311     exit(1);
312     }
313    
314     while (!feof(f)) {
315     memset(b1, 0, sizeof(b1));
316     memset(b2, 0, sizeof(b2));
317     memset(b3, 0, sizeof(b3));
318     memset(b4, 0, sizeof(b4));
319     fscanf(f, "%s %s\n", b1,b2);
320     if (strlen(b2) < 2 && !(b2[0]>='0' && b2[0]<='9')) {
321 dpavlin 10 strlcpy(b3, b2, sizeof(b3));
322     strlcpy(b2, "0", sizeof(b2));
323 dpavlin 2 fscanf(f, "%s\n", b4);
324     } else {
325     fscanf(f, "%s %s\n", b3,b4);
326     }
327    
328     /* printf("b1='%s' b2='%s' b3='%s' b4='%s'\n",
329     b1,b2,b3,b4); */
330     addr = strtoull(b1, NULL, 16);
331     len = strtoull(b2, NULL, 16);
332     type = b3[0];
333     /* printf("addr=%016llx len=%016llx type=%i\n",
334     addr, len, type); */
335    
336     if (type == 't' || type == 'r' || type == 'g')
337     continue;
338    
339 dpavlin 12 add_symbol_name(sc, addr, len, b4, type, -1);
340 dpavlin 2 }
341    
342     fclose(f);
343    
344     debug("%i symbols\n", sc->n_symbols - cur_n_symbols);
345     }
346    
347    
348     /*
349     * sym_addr_compare():
350     *
351     * Helper function for sorting symbols according to their address.
352     */
353     int sym_addr_compare(const void *a, const void *b)
354     {
355     struct symbol *p1 = (struct symbol *) a;
356     struct symbol *p2 = (struct symbol *) b;
357    
358     if (p1->addr < p2->addr)
359     return -1;
360     if (p1->addr > p2->addr)
361     return 1;
362    
363     return 0;
364     }
365    
366    
367     /*
368     * symbol_recalc_sizes():
369     *
370     * Recalculate sizes of symbols that have size = 0, by creating an array
371     * containing all symbols, qsort()-ing that array according to address, and
372     * recalculating the size fields if necessary.
373     */
374     void symbol_recalc_sizes(struct symbol_context *sc)
375     {
376     struct symbol *tmp_array;
377     struct symbol *last_ptr;
378     struct symbol *tmp_ptr;
379     int i;
380    
381     tmp_array = malloc(sizeof (struct symbol) * sc->n_symbols);
382     if (tmp_array == NULL) {
383     fprintf(stderr, "out of memory\n");
384     exit(1);
385     }
386    
387     /* Copy first_symbol --> tmp_array, and remove the old
388     first_symbol at the same time: */
389     tmp_ptr = sc->first_symbol;
390     i = 0;
391     while (tmp_ptr != NULL) {
392     tmp_array[i] = *tmp_ptr;
393     last_ptr = tmp_ptr;
394     tmp_ptr = tmp_ptr->next;
395     free(last_ptr);
396     i++;
397     }
398    
399     qsort(tmp_array, sc->n_symbols, sizeof(struct symbol),
400     sym_addr_compare);
401     sc->sorted_array = 1;
402    
403     /* Recreate the first_symbol chain: */
404     sc->first_symbol = NULL;
405     for (i=0; i<sc->n_symbols; i++) {
406     /* Recalculate size, if 0: */
407     if (tmp_array[i].len == 0) {
408     uint64_t len;
409     if (i != sc->n_symbols-1)
410     len = tmp_array[i+1].addr
411     - tmp_array[i].addr;
412     else
413     len = 1;
414     tmp_array[i].len = len;
415     }
416    
417     tmp_array[i].next = &tmp_array[i+1];
418     }
419    
420     sc->first_symbol = tmp_array;
421     }
422    
423    
424     /*
425     * symbol_init():
426     *
427     * Initialize the symbol hashtables.
428     */
429     void symbol_init(struct symbol_context *sc)
430     {
431     sc->first_symbol = NULL;
432     sc->sorted_array = 0;
433     sc->n_symbols = 0;
434     }
435    

  ViewVC Help
Powered by ViewVC 1.1.26