/[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 34 - (hide annotations)
Mon Oct 8 16:21:17 2007 UTC (16 years, 7 months ago) by dpavlin
File MIME type: text/plain
File size: 10523 byte(s)
++ trunk/HISTORY	(local)
$Id: HISTORY,v 1.1480 2007/02/19 01:34:42 debug Exp $
20061029	Changing usleep(1) calls in the debugger to usleep(10000)
20061107	Adding a new disk image option (-d o...) which sets the ISO9660
		filesystem base offset; also making some other hacks to allow
		NetBSD/dreamcast and homebrew demos/games to boot directly
		from a filesystem image.
		Moving Dreamcast-specific stuff in the documentation to its
		own page (dreamcast.html).
		Adding a border to the Dreamcast PVR framebuffer.
20061108	Adding a -T command line option (again?), for halting the
		emulator on unimplemented memory accesses.
20061109	Continuing on various SH4 and Dreamcast related things.
		The emulator should now halt on more unimplemented device
		accesses, instead of just printing a warning, forcing me to
		actually implement missing stuff :)
20061111	Continuing on SH4 and Dreamcast stuff.
		Adding a bogus Landisk (SH4) machine mode.
20061112	Implementing some parts of the Dreamcast GDROM device. With
		some ugly hacks, NetBSD can (barely) mount an ISO image.
20061113	NetBSD/dreamcast now starts booting from the Live CD image,
		but crashes randomly quite early on in the boot process.
20061122	Beginning on a skeleton interrupt.h and interrupt.c for the
		new interrupt subsystem.
20061124	Continuing on the new interrupt system; taking the first steps
		to attempt to connect CPUs (SuperH and MIPS) and devices
		(dev_cons and SH4 timer interrupts) to it. Many things will
		probably break from now on.
20061125	Converting dev_ns16550, dev_8253 to the new interrupt system.
		Attempting to begin to convert the ISA bus.
20061130	Incorporating a patch from Brian Foley for the configure
		script, which checks for X11 libs in /usr/X11R6/lib64 (which
		is used on some Linux systems).
20061227	Adding a note in the man page about booting from Dreamcast
		CDROM images (i.e. that no external kernel is needed).
20061229	Continuing on the interrupt system rewrite: beginning to
		convert more devices, adding abort() calls for legacy interrupt
		system calls so that everything now _has_ to be rewritten!
		Almost all machine modes are now completely broken.
20061230	More progress on removing old interrupt code, mostly related
		to the ISA bus + devices, the LCA bus (on AlphaBook1), and
		the Footbridge bus (for CATS). And some minor PCI stuff.
		Connecting the ARM cpu to the new interrupt system.
		The CATS, NetWinder, and QEMU_MIPS machine modes now work with
		the new interrupt system :)
20061231	Connecting PowerPC CPUs to the new interrupt system.
		Making PReP machines (IBM 6050) work again.
		Beginning to convert the GT PCI controller (for e.g. Malta
		and Cobalt emulation). Some things work, but not everything.
		Updating Copyright notices for 2007.
20070101	Converting dev_kn02 from legacy style to devinit; the 3max
		machine mode now works with the new interrupt system :-]
20070105	Beginning to convert the SGI O2 machine to the new interrupt
		system; finally converting O2 (IP32) devices to devinit, etc.
20070106	Continuing on the interrupt system redesign/rewrite; KN01
		(PMAX), KN230, and Dreamcast ASIC interrupts should work again,
		moving out stuff from machine.h and devices.h into the
		corresponding devices, beginning the rewrite of i80321
		interrupts, etc.
20070107	Beginning on the rewrite of Eagle interrupt stuff (PReP, etc).
20070117	Beginning the rewrite of Algor (V3) interrupts (finally
		changing dev_v3 into devinit style).
20070118	Removing the "bus" registry concept from machine.h, because
		it was practically meaningless.
		Continuing on the rewrite of Algor V3 ISA interrupts.
20070121	More work on Algor interrupts; they are now working again,
		well enough to run NetBSD/algor. :-)
20070122	Converting VR41xx (HPCmips) interrupts. NetBSD/hpcmips
		can be installed using the new interrupt system :-)
20070123	Making the testmips mode work with the new interrupt system.
20070127	Beginning to convert DEC5800 devices to devinit, and to the
		new interrupt system.
		Converting Playstation 2 devices to devinit, and converting
		the interrupt system. Also fixing a severe bug: the interrupt
		mask register on Playstation 2 is bitwise _toggled_ on writes.
20070128	Removing the dummy NetGear machine mode and the 8250 device
		(which was only used by the NetGear machine).
		Beginning to convert the MacPPC GC (Grand Central) interrupt
		controller to the new interrupt system.
		Converting Jazz interrupts (PICA61 etc.) to the new interrupt
		system. NetBSD/arc can be installed again :-)
		Fixing the JAZZ timer (hardcoding it at 100 Hz, works with
		NetBSD and it is better than a completely dummy timer as it
		was before).
		Converting dev_mp to the new interrupt system, although I
		haven't had time to actually test it yet.
		Completely removing src/machines/interrupts.c, cpu_interrupt
		and cpu_interrupt_ack in src/cpu.c, and
		src/include/machine_interrupts.h! Adding fatal error messages
		+ abort() in the few places that are left to fix.
		Converting dev_z8530 to the new interrupt system.
		FINALLY removing the md_int struct completely from the
		machine struct.
		SH4 fixes (adding a PADDR invalidation in the ITLB replacement
		code in memory_sh.c); the NetBSD/dreamcast LiveCD now runs
		all the way to the login prompt, and can be interacted with :-)
		Converting the CPC700 controller (PCI and interrupt controller
		for PM/PPC) to the new interrupt system.
20070129	Fixing MACE ISA interrupts (SGI IP32 emulation). Both NetBSD/
		sgimips' and OpenBSD/sgi's ramdisk kernels can now be
		interacted with again.
20070130	Moving out the MIPS multi_lw and _sw instruction combinations
		so that they are auto-generated at compile time instead.
20070131	Adding detection of amd64/x86_64 hosts in the configure script,
		for doing initial experiments (again :-) with native code
		generation.
		Adding a -k command line option to set the size of the dyntrans
		cache, and a -B command line option to disable native code
		generation, even if GXemul was compiled with support for
		native code generation for the specific host CPU architecture.
20070201	Experimenting with a skeleton for native code generation.
		Changing the default behaviour, so that native code generation
		is now disabled by default, and has to be enabled by using
		-b on the command line.
20070202	Continuing the native code generation experiments.
		Making PCI interrupts work for Footbridge again.
20070203	More native code generation experiments.
		Removing most of the native code generation experimental code,
		it does not make sense to include any quick hacks like this.
		Minor cleanup/removal of some more legacy MIPS interrupt code.
20070204	Making i80321 interrupts work again (for NetBSD/evbarm etc.),
		and fixing the timer at 100 Hz.
20070206	Experimenting with removing the wdc interrupt slowness hack.
20070207	Lowering the number of dyntrans TLB entries for MIPS from
		192 to 128, resulting in a minor speed improvement.
		Minor optimization to the code invalidation routine in
		cpu_dyntrans.c.
20070208	Increasing (experimentally) the nr of dyntrans instructions per
		loop from 60 to 120.
20070210	Commenting out (experimentally) the dyntrans_device_danger
		detection in memory_rw.c.
		Changing the testmips and baremips machines to use a revision 2
		MIPS64 CPU by default, instead of revision 1.
		Removing the dummy i960, IA64, x86, AVR32, and HP PA-RISC
		files, the PC bios emulation, and the Olivetti M700 (ARC) and
		db64360 emulation modes.
20070211	Adding an "mp" demo to the demos directory, which tests the
		SMP functionality of the testmips machine.
		Fixing PReP interrupts some more. NetBSD/prep now boots again.
20070216	Adding a "nop workaround" for booting Mach/PMAX to the
		documentation; thanks to Artur Bujdoso for the values.
		Converting more of the MacPPC interrupt stuff to the new
		system.
		Beginning to convert BeBox interrupts to the new system.
		PPC603e should NOT have the PPC_NO_DEC flag! Removing it.
		Correcting BeBox clock speed (it was set to 100 in the NetBSD
		bootinfo block, but should be 33000000/4), allowing NetBSD
		to start without using the (incorrect) PPC_NO_DEC hack.
20070217	Implementing (slow) AltiVec vector loads and stores, allowing
		NetBSD/macppc to finally boot using the GENERIC kernel :-)
		Updating the documentation with install instructions for
		NetBSD/macppc.
20070218-19	Regression testing for the release.

==============  RELEASE 0.4.4  ==============


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

  ViewVC Help
Powered by ViewVC 1.1.26