/[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

Contents of /trunk/src/symbol.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 14 - (show annotations)
Mon Oct 8 16:18:51 2007 UTC (16 years, 6 months ago) by dpavlin
File MIME type: text/plain
File size: 10364 byte(s)
++ trunk/HISTORY	(local)
$Id: HISTORY,v 1.982 2005/10/07 22:45:32 debug Exp $
20050816	Some success in decoding the way the SGI O2 PROM draws graphics
		during bootup; lines/rectangles and bitmaps work, enough to
		show the bootlogo etc. :-)
		Adding more PPC instructions, and (dummy) BAT registers.
20050817	Updating the pckbc to support scancode type 3 keyboards
		(required in order to interact with the SGI O2 PROM).
		Adding more PPC instructions.
20050818	Adding more ARM instructions; general register forms.
		Importing armreg.h from NetBSD (ARM cpu ids). Adding a (dummy)
		CATS machine mode (using SA110 as the default CPU).
		Continuing on general dyntrans related stuff.
20050819	Register forms for ARM load/stores. Gaah! The Compaq C Compiler
		bug is triggered for ARM loads as well, not just PPC :-(
		Adding full support for ARM PC-relative load/stores, and load/
		stores where the PC register is the destination register.
		Adding support for ARM a.out binaries.
20050820	Continuing to add more ARM instructions, and correcting some
		bugs. Continuing on CATS emulation.
		More work on the PPC stuff.
20050821	Minor PPC and ARM updates. Adding more machine types.
20050822	All ARM "data processing instructions" are now generated
		automatically.
20050824	Beginning the work on the ARM system control coprocessor.
		Adding support for ARM halfword load/stores, and signed loads.
20050825	Fixing an important bug related to the ARM condition codes.
		OpenBSD/zaurus and NetBSD/netwinder now print some boot
		messages. :)
		Adding a dummy SH (Hitachi SuperH) cpu family.
		Beginning to add some ARM virtual address translation.
		MIPS bugfixes: unaligned PC now cause an ADEL exception (at
		least for non-bintrans execution), and ADEL/ADES (not
		TLBL/TLBS) are used if userland tries to access kernel space.
		(Thanks to Joshua Wise for making me aware of these bugs.)
20050827	More work on the ARM emulation, and various other updates.
20050828	More ARM updates.
		Finally taking the time to work on translation invalidation
		(i.e. invalidating translated code mappings when memory is
		written to). Hopefully this doesn't break anything.
20050829	Moving CPU related files from src/ to a new subdir, src/cpus/.
		Moving PROM emulation stuff from src/ to src/promemul/.
		Better debug instruction trace for ARM loads and stores.
20050830	Various ARM updates (correcting CMP flag calculation, etc).
20050831	PPC instruction updates. (Flag fixes, etc.)
20050901	Various minor PPC and ARM instruction emulation updates.
		Minor OpenFirmware emulation updates.
20050903	Adding support for adding arbitrary ARM coprocessors (with
		the i80321 I/O coprocessor as a first test).
		Various other ARM and PPC updates.
20050904	Adding some SHcompact disassembly routines.
20050907	(Re)adding a dummy HPPA CPU module, and a dummy i960 module.
20050908	Began hacking on some Apple Partition Table support.
20050909	Adding support for loading Mach-O (Darwin PPC) binaries.
20050910	Fixing an ARM bug (Carry flag was incorrectly updated for some
		data processing instructions); OpenBSD/cats and NetBSD/
		netwinder get quite a bit further now.
		Applying a patch to dev_wdc, and a one-liner to dev_pcic, to
		make them work better when emulating new versions of OpenBSD.
		(Thanks to Alexander Yurchenko for the patches.)
		Also doing some other minor updates to dev_wdc. (Some cleanup,
		and finally converting to devinit, etc.)
20050912	IRIX doesn't have u_int64_t by default (noticed by Andreas
		<avr@gnulinux.nl>); configure updated to reflect this.
		Working on ARM register bank switching, CPSR vs SPSR issues,
		and beginning the work on interrupt/exception support.
20050913	Various minor ARM updates (speeding up load/store multiple,
		and fixing a ROR bug in R(); NetBSD/cats now boots as far as
		OpenBSD/cats).
20050917	Adding a dummy Atmel AVR (8-bit) cpu family skeleton.
20050918	Various minor updates.
20050919	Symbols are now loaded from Mach-O executables.
		Continuing the work on adding ARM exception support.
20050920	More work on ARM stuff: OpenBSD/cats and NetBSD/cats reach
		userland! :-)
20050921	Some more progress on ARM interrupt specifics.
20050923	Fixing linesize for VR4121 (patch by Yurchenko). Also fixing
		linesizes/cachesizes for some other VR4xxx.
		Adding a dummy Acer Labs M1543 PCI-ISA bridge (for CATS) and a
		dummy Symphony Labs 83C553 bridge (for Netwinder), usable by 
		dev_footbridge.
20050924	Some PPC progress.
20050925	More PPC progress.
20050926	PPC progress (fixing some bugs etc); Darwin's kernel gets
		slightly further than before.
20050928	Various updates: footbridge/ISA/pciide stuff, and finally
		fixing the VGA text scroll-by-changing-the-base-offset bug.
20050930	Adding a dummy S3 ViRGE pci card for CATS emulation, which
		both NetBSD and OpenBSD detects as VGA.
		Continuing on Footbridge (timers, ISA interrupt stuff).
20051001	Continuing... there are still bugs, probably interrupt-
		related.
20051002	More work on the Footbridge (interrupt stuff).
20051003	Various minor updates. (Trying to find the bug(s).)
20051004	Continuing on the ARM stuff.
20051005	More ARM-related fixes.
20051007	FINALLY! Found and fixed 2 ARM bugs: 1 memory related, and the
		other was because of an error in the ARM manual (load multiple
		with the S-bit set should _NOT_ load usermode registers, as the
		manual says, but it should load saved registers, which may or
		may not happen to be usermode registers).
		NetBSD/cats and OpenBSD/cats seem to install fine now :-)
		except for a minor bug at the end of the OpenBSD/cats install.
		Updating the documentation, preparing for the next release.
20051008	Continuing with release testing and cleanup.

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.32 2005/09/26 00:08:02 debug Exp $
29 *
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_and_n_args():
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 n_argsp is non-NULL, *n_argsp is set to the symbol's n_args value.
111 *
112 * If no symbol was found, NULL is returned instead.
113 */
114 static char symbol_buf[SYMBOLBUF_MAX+1];
115 char *get_symbol_name_and_n_args(struct symbol_context *sc, uint64_t addr,
116 uint64_t *offset, int *n_argsp)
117 {
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 if (n_argsp != NULL)
147 *n_argsp = s->n_args;
148 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 if (n_argsp != NULL)
171 *n_argsp = s->n_args;
172 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 * 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 * add_symbol_name():
211 *
212 * Add a symbol to the symbol list.
213 */
214 void add_symbol_name(struct symbol_context *sc,
215 uint64_t addr, uint64_t len, char *name, int type, int n_args)
216 {
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 if (name == NULL) {
226 fprintf(stderr, "add_symbol_name(): name = NULL\n");
227 exit(1);
228 }
229
230 if (name[0] == '\0')
231 return;
232
233 /* TODO: Maybe this should be optional? */
234 if (name[0] == '.' || name[0] == '$')
235 return;
236
237 /* 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 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 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 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 s->addr = addr;
278 s->len = len;
279 s->type = type;
280 s->n_args = n_args;
281
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 strlcpy(b3, b2, sizeof(b3));
322 strlcpy(b2, "0", sizeof(b2));
323 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 add_symbol_name(sc, addr, len, b4, type, -1);
340 }
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