/[gxemul]/trunk/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 /trunk/src/symbol.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10 - (hide annotations)
Mon Oct 8 16:18:27 2007 UTC (13 years, 1 month ago) by dpavlin
File MIME type: text/plain
File size: 9270 byte(s)
++ trunk/HISTORY	(local)
$Id: HISTORY,v 1.815 2005/06/27 23:04:35 debug Exp $
20050617	Experimenting some more with netbooting OpenBSD/sgi. Adding
		a hack which allows emulated ethernet networks to be
		distributed across multiple emulator processes.
20050618	Minor updates (documentation, dummy YAMON emulation, etc).
20050620	strcpy/strcat -> strlcpy/strlcat updates.
		Some more progress on evbmips (Malta).
20050621	Adding a section to doc/configfiles.html about ethernet
		emulation across multiple hosts.
		Beginning the work on the ARM translation engine (using the
		dynamic-but-not-binary translation method).
		Fixing a bintrans bug: 0x9fc00000 should always be treated as
		PROM area, just as 0xbfc00000 is.
		Minor progress on Malta emulation (the PCI-ISA bus).
20050622	NetBSD/evbmips can now be installed (using another emulated
		machine) and run (including userland and so on). :-)
		Spliting up the bintrans haddr_entry field into two (one for
		read, one for write). Probably not much of a speed increase,
		though.
		Updating some NetBSD 2.0 -> 2.0.2 in the documentation.
20050623	Minor updates (documentation, the TODO file, etc).
		gzipped kernels are now always automagically gunzipped when
		loaded.
20050624	Adding a dummy Playstation Portable (PSP) mode, just barely
		enough to run Hello World (in weird colors :-).
		Removing the -b command line option; old bintrans is enabled
		by default instead. It makes more sense.
		Trying to finally fix the non-working performance measurement
		thing (instr/second etc).
20050625	Continuing on the essential basics for ARM emulation. Two
		instructions seem to work, a branch and a simple "mov". (The
		mov arguments are not correct yet.) Performance is definitely
		reasonable.
		Various other minor updates.
		Adding the ARM "bl" instruction.
		Adding support for combining multiple ARM instructions into one
		function call. ("mov" + "mov" is the only one implemented so
		far, but it seems to work.)
		Cleaning up some IP32 interrupt things (crime/mace); disabling
		the PS/2 keyboard controller on IP32, so that NetBSD/sgimips
		boots into userland again.
20050626	Finally! NetBSD/sgimips netboots. Adding instructions to
		doc/guestoses.html on how to set up an nfs server etc.
		Various other minor fixes.
		Playstation Portable ".pbp" files can now be used directly.
		(The ELF part of the .pbp is extracted transparently.)
		Converting some sprintf -> snprintf.
		Adding some more instructions to the ARM disassembler.
20050627	More ARM updates. Adding some simple ldr(b), str(b),
		cmps, and conditional branch instructions, enough to run
		a simple Hello World program.
		All ARM instructions are now inlined/generated for all possible
		condition codes.
		Adding add and sub, and more load/store instructions.
		Removing dummy files: cpu_alpha.c, cpu_hppa.c, and cpu_sparc.c.
		Some minor documentation updates; preparing for a 0.3.4
		release. Updating some URLs.

==============  RELEASE 0.3.4  ==============


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 10 * $Id: symbol.c,v 1.26 2005/06/21 16:22:52 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     * get_symbol_name():
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 no symbol was found, NULL is returned instead.
111     */
112     static char symbol_buf[SYMBOLBUF_MAX+1];
113     char *get_symbol_name(struct symbol_context *sc, uint64_t addr,
114     uint64_t *offset)
115     {
116     struct symbol *s;
117     int stepsize, ofs;
118    
119     if (sc->n_symbols == 0)
120     return NULL;
121    
122     if ((addr >> 32) == 0 && (addr & 0x80000000ULL))
123     addr |= 0xffffffff00000000ULL;
124    
125     symbol_buf[0] = symbol_buf[SYMBOLBUF_MAX] = '\0';
126     if (offset != NULL)
127     *offset = 0;
128    
129     if (!sc->sorted_array) {
130     /* Slow, linear O(n) search: */
131     s = sc->first_symbol;
132     while (s != NULL) {
133     /* Found a match? */
134     if (addr >= s->addr && addr < s->addr + s->len) {
135     if (addr == s->addr)
136     snprintf(symbol_buf, SYMBOLBUF_MAX,
137     "%s", s->name);
138     else
139     snprintf(symbol_buf, SYMBOLBUF_MAX,
140     "%s+0x%lx", s->name, (long)
141     (addr - s->addr));
142     if (offset != NULL)
143     *offset = addr - s->addr;
144     return symbol_buf;
145     }
146     s = s->next;
147     }
148     } else {
149     /* Faster, O(log n) search: */
150     stepsize = sc->n_symbols / 2;
151     ofs = stepsize;
152     while (stepsize > 0 || (stepsize == 0 && ofs == 0)) {
153     s = sc->first_symbol + ofs;
154    
155     /* Found a match? */
156     if (addr >= s->addr && addr < s->addr + s->len) {
157     if (addr == s->addr)
158     snprintf(symbol_buf, SYMBOLBUF_MAX,
159     "%s", s->name);
160     else
161     snprintf(symbol_buf, SYMBOLBUF_MAX,
162     "%s+0x%lx", s->name, (long)
163     (addr - s->addr));
164     if (offset != NULL)
165     *offset = addr - s->addr;
166     return symbol_buf;
167     }
168    
169     if (ofs == 0)
170     break;
171    
172     stepsize >>= 1;
173    
174     /* Special case for offset 0 (end of search in
175     the Left direction */
176     if (stepsize == 0)
177     ofs = 0;
178     else {
179     if (addr < s->addr)
180     ofs -= stepsize;
181     else
182     ofs += stepsize;
183     }
184     }
185     }
186    
187     /* Not found? Then return NULL. */
188     return NULL;
189     }
190    
191    
192     /*
193     * add_symbol_name():
194     *
195     * Add a symbol to the symbol list.
196     */
197     void add_symbol_name(struct symbol_context *sc,
198     uint64_t addr, uint64_t len, char *name, int type)
199     {
200     struct symbol *s;
201    
202     if (sc->sorted_array) {
203     fprintf(stderr, "add_symbol_name(): Internal error: the "
204     "symbol array is already sorted\n");
205     exit(1);
206     }
207    
208 dpavlin 4 if (name == NULL) {
209     fprintf(stderr, "add_symbol_name(): name = NULL\n");
210     exit(1);
211     }
212    
213 dpavlin 10 if (name[0] == '\0')
214     return;
215    
216 dpavlin 4 /* TODO: Maybe this should be optional? */
217     if (name[0] == '$')
218     return;
219    
220 dpavlin 2 if ((addr >> 32) == 0 && (addr & 0x80000000ULL))
221     addr |= 0xffffffff00000000ULL;
222    
223     s = malloc(sizeof(struct symbol));
224     if (s == NULL) {
225     fprintf(stderr, "out of memory\n");
226     exit(1);
227     }
228    
229     s->name = strdup(name);
230     if (s->name == NULL) {
231     fprintf(stderr, "out of memory\n");
232     exit(1);
233     }
234     s->addr = addr;
235     s->len = len;
236     s->type = type;
237    
238     sc->n_symbols ++;
239    
240     /* Add first in list: */
241     s->next = sc->first_symbol;
242     sc->first_symbol = s;
243     }
244    
245    
246     /*
247     * symbol_readfile():
248     *
249     * Read 'nm -S' style symbols from a file.
250     *
251     * TODO: This function is an ugly hack, and should be replaced
252     * with something that reads symbols directly from the executable
253     * images.
254     */
255     void symbol_readfile(struct symbol_context *sc, char *fname)
256     {
257     FILE *f;
258     char b1[80]; uint64_t addr;
259     char b2[80]; uint64_t len;
260     char b3[80]; int type;
261     char b4[80];
262     int cur_n_symbols = sc->n_symbols;
263    
264     f = fopen(fname, "r");
265     if (f == NULL) {
266     perror(fname);
267     exit(1);
268     }
269    
270     while (!feof(f)) {
271     memset(b1, 0, sizeof(b1));
272     memset(b2, 0, sizeof(b2));
273     memset(b3, 0, sizeof(b3));
274     memset(b4, 0, sizeof(b4));
275     fscanf(f, "%s %s\n", b1,b2);
276     if (strlen(b2) < 2 && !(b2[0]>='0' && b2[0]<='9')) {
277 dpavlin 10 strlcpy(b3, b2, sizeof(b3));
278     strlcpy(b2, "0", sizeof(b2));
279 dpavlin 2 fscanf(f, "%s\n", b4);
280     } else {
281     fscanf(f, "%s %s\n", b3,b4);
282     }
283    
284     /* printf("b1='%s' b2='%s' b3='%s' b4='%s'\n",
285     b1,b2,b3,b4); */
286     addr = strtoull(b1, NULL, 16);
287     len = strtoull(b2, NULL, 16);
288     type = b3[0];
289     /* printf("addr=%016llx len=%016llx type=%i\n",
290     addr, len, type); */
291    
292     if (type == 't' || type == 'r' || type == 'g')
293     continue;
294    
295     add_symbol_name(sc, addr, len, b4, type);
296     }
297    
298     fclose(f);
299    
300     debug("%i symbols\n", sc->n_symbols - cur_n_symbols);
301     }
302    
303    
304     /*
305     * sym_addr_compare():
306     *
307     * Helper function for sorting symbols according to their address.
308     */
309     int sym_addr_compare(const void *a, const void *b)
310     {
311     struct symbol *p1 = (struct symbol *) a;
312     struct symbol *p2 = (struct symbol *) b;
313    
314     if (p1->addr < p2->addr)
315     return -1;
316     if (p1->addr > p2->addr)
317     return 1;
318    
319     return 0;
320     }
321    
322    
323     /*
324     * symbol_recalc_sizes():
325     *
326     * Recalculate sizes of symbols that have size = 0, by creating an array
327     * containing all symbols, qsort()-ing that array according to address, and
328     * recalculating the size fields if necessary.
329     */
330     void symbol_recalc_sizes(struct symbol_context *sc)
331     {
332     struct symbol *tmp_array;
333     struct symbol *last_ptr;
334     struct symbol *tmp_ptr;
335     int i;
336    
337     tmp_array = malloc(sizeof (struct symbol) * sc->n_symbols);
338     if (tmp_array == NULL) {
339     fprintf(stderr, "out of memory\n");
340     exit(1);
341     }
342    
343     /* Copy first_symbol --> tmp_array, and remove the old
344     first_symbol at the same time: */
345     tmp_ptr = sc->first_symbol;
346     i = 0;
347     while (tmp_ptr != NULL) {
348     tmp_array[i] = *tmp_ptr;
349     last_ptr = tmp_ptr;
350     tmp_ptr = tmp_ptr->next;
351     free(last_ptr);
352     i++;
353     }
354    
355     qsort(tmp_array, sc->n_symbols, sizeof(struct symbol),
356     sym_addr_compare);
357     sc->sorted_array = 1;
358    
359     /* Recreate the first_symbol chain: */
360     sc->first_symbol = NULL;
361     for (i=0; i<sc->n_symbols; i++) {
362     /* Recalculate size, if 0: */
363     if (tmp_array[i].len == 0) {
364     uint64_t len;
365     if (i != sc->n_symbols-1)
366     len = tmp_array[i+1].addr
367     - tmp_array[i].addr;
368     else
369     len = 1;
370     tmp_array[i].len = len;
371     }
372    
373     tmp_array[i].next = &tmp_array[i+1];
374     }
375    
376     sc->first_symbol = tmp_array;
377     }
378    
379    
380     /*
381     * symbol_init():
382     *
383     * Initialize the symbol hashtables.
384     */
385     void symbol_init(struct symbol_context *sc)
386     {
387     sc->first_symbol = NULL;
388     sc->sorted_array = 0;
389     sc->n_symbols = 0;
390     }
391    

  ViewVC Help
Powered by ViewVC 1.1.26