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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 22 - (hide annotations)
Mon Oct 8 16:19:37 2007 UTC (16 years, 6 months ago) by dpavlin
Original Path: trunk/src/symbol.c
File MIME type: text/plain
File size: 10360 byte(s)
++ trunk/HISTORY	(local)
$Id: HISTORY,v 1.1121 2006/02/18 21:03:08 debug Exp $
20051126	Cobalt and PReP now work with the 21143 NIC.
		Continuing on Alpha dyntrans things.
		Fixing some more left-shift-by-24 to unsigned.
20051127	Working on OpenFirmware emulation; major cleanup/redesign.
		Progress on MacPPC emulation: NetBSD detects two CPUs (when
		running with -n 2), framebuffer output (for text) works.
		Adding quick-hack Bandit PCI controller and "gc" interrupt
		controller for MacPPC.
20051128	Changing from a Bandit to a Uni-North controller for macppc.
		Continuing on OpenFirmware and MacPPC emulation in general
		(obio controller, and wdc attached to the obio seems to work).
20051129	More work on MacPPC emulation (adding a dummy ADB controller).
		Continuing the PCI bus cleanup (endianness and tag composition)
		and rewriting all PCI controllers' access functions.
20051130	Various minor PPC dyntrans optimizations.
		Manually inlining some parts of the framebuffer redraw routine.
		Slowly beginning the conversion of the old MIPS emulation into
		dyntrans (but this will take quite some time to get right).
		Generalizing quick_pc_to_pointers.
20051201	Documentation update (David Muse has made available a kernel
		which simplifies Debian/DECstation installation).
		Continuing on the ADB bus controller.
20051202	Beginning a rewrite of the Zilog serial controller (dev_zs).
20051203	Continuing on the zs rewrite (now called dev_z8530); conversion
		to devinit style.
		Reworking some of the input-only vs output-only vs input-output
		details of src/console.c, better warning messages, and adding
		a debug dump.
		Removing the concept of "device state"; it wasn't really used.
		Changing some debug output (-vv should now be used to show all
		details about devices and busses; not shown during normal
		startup anymore).
		Beginning on some SPARC instruction disassembly support.
20051204	Minor PPC updates (WALNUT skeleton stuff).
		Continuing on the MIPS dyntrans rewrite.
		More progress on the ADB controller (a keyboard is "detected"
		by NetBSD and OpenBSD).
		Downgrading OpenBSD/arc as a guest OS from "working" to
		"almost working" in the documentation.
		Progress on Algor emulation ("v3" PCI controller).
20051205	Minor updates.
20051207	Sorting devices according to address; this reduces complexity
		of device lookups from O(n) to O(log n) in memory_rw (but no
		real performance increase (yet) in experiments).
20051210	Beginning the work on native dyntrans backends (by making a
		simple skeleton; so far only for Alpha hosts).
20051211	Some very minor SPARC updates.
20051215	Fixing a bug in the MIPS mul (note: not mult) instruction,
		so it also works with non-64-bit emulation. (Thanks to Alec
		Voropay for noticing the problem.)
20051216	More work on the fake/empty/simple/skeleton/whatever backend;
		performance doesn't increase, so this isn't really worth it,
		but it was probably worth it to prepare for a real backend
		later.
20051219	More instr call statistics gathering and analysis stuff.
20051220	Another fix for MIPS 'mul'. Also converting mul and {d,}cl{o,z}
		to dyntrans.
		memory_ppc.c syntax error fix (noticed by Peter Valchev).
		Beginning to move out machines from src/machine.c into
		individual files in src/machines (in a way similar to the
		autodev system for devices).
20051222	Updating the documentation regarding NetBSD/pmax 3.0.
20051223	- " - NetBSD/cats 3.0.
20051225	- " - NetBSD/hpcmips 3.0.
20051226	Continuing on the machine registry redesign.
		Adding support for ARM rrx (33-bit rotate).
		Fixing some signed/unsigned issues (exposed by gcc -W).
20051227	Fixing the bug which prevented a NetBSD/prep 3.0 install kernel
		from starting (triggered when an mtmsr was the last instruction
		on a page). Unfortunately not enough to get the kernel to run
		as well as the 2.1 kernels did.
20051230	Some dyntrans refactoring.
20051231	Continuing on the machine registry redesign.
20060101-10	Continuing... moving more machines. Moving MD interrupt stuff
		from machine.c into a new src/machines/interrupts.c.
20060114	Adding various mvmeppc machine skeletons.
20060115	Continuing on mvme* stuff. NetBSD/mvmeppc prints boot messages
		(for MVME1600) and reaches the root device prompt, but no
		specific hardware devices are emulated yet.
20060116	Minor updates to the mvme1600 emulation mode; the Eagle PCI bus
		seems to work without much modification, and a 21143 can be
		detected, interrupts might work (but untested so far).
		Adding a fake MK48Txx (mkclock) device, for NetBSD/mvmeppc.
20060121	Adding an aux control register for ARM. (A BIG thank you to
		Olivier Houchard for tracking down this bug.)
20060122	Adding more ARM instructions (smulXY), and dev_iq80321_7seg.
20060124	Adding disassembly of more ARM instructions (mia*, mra/mar),
		and some semi-bogus XScale and i80321 registers.
20060201-02	Various minor updates. Moving the last machines out of
		machine.c.
20060204	Adding a -c command line option, for running debugger commands
		before the simulation starts, but after all files have been
		loaded.
		Minor iq80321-related updates.
20060209	Minor hacks (DEVINIT macro, etc).
		Preparing for the generalization of the 64-bit dyntrans address
		translation subsystem.
20060216	Adding ARM ldrd (double-register load).
20060217	Continuing on various ARM-related stuff.
20060218	More progress on the ATA/wdc emulation for NetBSD/iq80321.
		NetBSD/evbarm can now be installed :-)  Updating the docs, etc.
		Continuing on Algor emulation.

==============  RELEASE 0.3.8  ==============


1 dpavlin 2 /*
2 dpavlin 22 * Copyright (C) 2003-2006 Anders Gavare. All rights reserved.
3 dpavlin 2 *
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 22 * $Id: symbol.c,v 1.34 2006/01/14 12:51:59 debug Exp $
29 dpavlin 2 *
30     * Address to symbol translation routines.
31     *
32 dpavlin 18 * 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 dpavlin 2 */
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 dpavlin 12 * get_symbol_name_and_n_args():
97 dpavlin 2 *
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 dpavlin 12 * If n_argsp is non-NULL, *n_argsp is set to the symbol's n_args value.
110     *
111 dpavlin 2 * If no symbol was found, NULL is returned instead.
112     */
113     static char symbol_buf[SYMBOLBUF_MAX+1];
114 dpavlin 12 char *get_symbol_name_and_n_args(struct symbol_context *sc, uint64_t addr,
115     uint64_t *offset, int *n_argsp)
116 dpavlin 2 {
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 dpavlin 12 if (n_argsp != NULL)
146     *n_argsp = s->n_args;
147 dpavlin 2 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 dpavlin 12 if (n_argsp != NULL)
170     *n_argsp = s->n_args;
171 dpavlin 2 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 dpavlin 12 * 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 dpavlin 2 * add_symbol_name():
210     *
211     * Add a symbol to the symbol list.
212     */
213     void add_symbol_name(struct symbol_context *sc,
214 dpavlin 12 uint64_t addr, uint64_t len, char *name, int type, int n_args)
215 dpavlin 2 {
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 dpavlin 4 if (name == NULL) {
225     fprintf(stderr, "add_symbol_name(): name = NULL\n");
226     exit(1);
227     }
228    
229 dpavlin 10 if (name[0] == '\0')
230     return;
231    
232 dpavlin 4 /* TODO: Maybe this should be optional? */
233 dpavlin 12 if (name[0] == '.' || name[0] == '$')
234 dpavlin 4 return;
235    
236 dpavlin 12 /* 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 dpavlin 14 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 dpavlin 12 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 dpavlin 2 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 dpavlin 12 s->addr = addr;
277     s->len = len;
278     s->type = type;
279     s->n_args = n_args;
280 dpavlin 2
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 dpavlin 10 strlcpy(b3, b2, sizeof(b3));
321     strlcpy(b2, "0", sizeof(b2));
322 dpavlin 2 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 dpavlin 12 add_symbol_name(sc, addr, len, b4, type, -1);
339 dpavlin 2 }
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