/[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 12 - (show annotations)
Mon Oct 8 16:18:38 2007 UTC (16 years, 6 months ago) by dpavlin
File MIME type: text/plain
File size: 10211 byte(s)
++ trunk/HISTORY	(local)
$Id: HISTORY,v 1.905 2005/08/16 09:16:24 debug Exp $
20050628	Continuing the work on the ARM translation engine. end_of_page
		works. Experimenting with load/store translation caches
		(virtual -> physical -> host).
20050629	More ARM stuff (memory access translation cache, mostly). This
		might break a lot of stuff elsewhere, probably some MIPS-
		related translation things.
20050630	Many load/stores are now automatically generated and included
		into cpu_arm_instr.c; 1024 functions in total (!).
		Fixes based on feedback from Alec Voropay: only print 8 hex
		digits instead of 16 in some cases when emulating 32-bit
		machines; similar 8 vs 16 digit fix for breakpoint addresses;
		4Kc has 16 TLB entries, not 48; the MIPS config select1
		register is now printed with "reg ,0".
		Also changing many other occurances of 16 vs 8 digit output.
		Adding cache associativity fields to mips_cpu_types.h; updating
		some other cache fields; making the output of
		mips_cpu_dumpinfo() look nicer.
		Generalizing the bintrans stuff for device accesses to also
		work with the new translation system. (This might also break
		some MIPS things.)
		Adding multi-load/store instructions to the ARM disassembler
		and the translator, and some optimizations of various kinds.
20050701	Adding a simple dev_disk (it can read/write sectors from
		disk images).
20050712	Adding dev_ether (a simple ethernet send/receive device).
		Debugger command "ninstrs" for toggling show_nr_of_instructions
		during runtime.
		Removing the framebuffer logo.
20050713	Continuing on dev_ether.
		Adding a dummy cpu_alpha (again).
20050714	More work on cpu_alpha.
20050715	More work on cpu_alpha. Many instructions work, enough to run
		a simple framebuffer fill test (similar to the ARM test).
20050716	More Alpha stuff.
20050717	Minor updates (Alpha stuff).
20050718	Minor updates (Alpha stuff).
20050719	Generalizing some Alpha instructions.
20050720	More Alpha-related updates.
20050721	Continuing on cpu_alpha. Importing rpb.h from NetBSD/alpha.
20050722	Alpha-related updates: userland stuff (Hello World using
		write() compiled statically for FreeBSD/Alpha runs fine), and
		more instructions are now implemented.
20050723	Fixing ldq_u and stq_u.
		Adding more instructions (conditional moves, masks, extracts,
		shifts).
20050724	More FreeBSD/Alpha userland stuff, and adding some more
		instructions (inserts).
20050725	Continuing on the Alpha stuff. (Adding dummy ldt/stt.)
		Adding a -A command line option to turn off alignment checks
		in some cases (for translated code).
		Trying to remove the old bintrans code which updated the pc
		and nr_of_executed_instructions for every instruction.
20050726	Making another attempt att removing the pc/nr of instructions
		code. This time it worked, huge performance increase for
		artificial test code, but performance loss for real-world
		code :-( so I'm scrapping that code for now.
		Tiny performance increase on Alpha (by using ret instead of
		jmp, to play nice with the Alpha's branch prediction) for the
		old MIPS bintrans backend.
20050727	Various minor fixes and cleanups.
20050728	Switching from a 2-level virtual to host/physical translation
		system for ARM emulation, to a 1-level translation.
		Trying to switch from 2-level to 1-level for the MIPS bintrans
		system as well (Alpha only, so far), but there is at least one
		problem: caches and/or how they work with device mappings.
20050730	Doing the 2-level to 1-level conversion for the i386 backend.
		The cache/device bug is still there for R2K/3K :(
		Various other minor updates (Malta etc).
		The mc146818 clock now updates the UIP bit in a way which works
		better with Linux for at least sgimips and Malta emulation.
		Beginning the work on refactoring the dyntrans system.
20050731	Continuing the dyntrans refactoring.
		Fixing a small but serious host alignment bug in memory_rw.
		Adding support for big-endian load/stores to the i386 bintrans
		backend.
		Another minor i386 bintrans backend update: stores from the
		zero register are now one (or two) loads shorter.
		The slt and sltu instructions were incorrectly implemented for
		the i386 backend; only using them for 32-bit mode for now.
20050801	Continuing the dyntrans refactoring.
		Cleanup of the ns16550 serial controller (removing unnecessary
		code).
		Bugfix (memory corruption bug) in dev_gt, and a patch/hack from
		Alec Voropay for Linux/Malta.
20050802	More cleanup/refactoring of the dyntrans subsystem: adding
		phys_page pointers to the lookup tables, for quick jumps
		between translated pages.
		Better fix for the ns16550 device (but still no real FIFO
		functionality).
		Converting cpu_ppc to the new dyntrans system. This means that
		I will have to start from scratch with implementing each
		instruction, and figure out how to implement dual 64/32-bit
		modes etc.
		Removing the URISC CPU family, because it was useless.
20050803	When selecting a machine type, the main type can now be omitted
		if the subtype name is unique. (I.e. -E can be omitted.)
		Fixing a dyntrans/device update bug. (Writes to offset 0 of
		a device could sometimes go unnoticed.)
		Adding an experimental "instruction combination" hack for
		ARM for memset-like byte fill loops.
20050804	Minor progress on cpu_alpha and related things.
		Finally fixing the MIPS dmult/dmultu bugs.
		Fixing some minor TODOs.
20050805	Generalizing the 8259 PIC. It now also works with Cobalt
		and evbmips emulation, in addition to the x86 hack.
		Finally converting the ns16550 device to use devinit.
		Continuing the work on the dyntrans system. Thinking about
		how to add breakpoints.
20050806	More dyntrans updates. Breakpoints seem to work now.
20050807	Minor updates: cpu_alpha and related things; removing
		dev_malta (as it isn't used any more).
		Dyntrans: working on general "show trace tree" support.
		The trace tree stuff now works with both the old MIPS code and
		with newer dyntrans modes. :)
		Continuing on Alpha-related stuff (trying to get *BSD to boot
		a bit further, adding more instructions, etc).
20050808	Adding a dummy IA64 cpu family, and continuing the refactoring
		of the dyntrans system.
		Removing the regression test stuff, because it was more or
		less useless.
		Adding loadlinked/storeconditional type instructions to the
		Alpha emulation. (Needed for Linux/alpha. Not very well tested
		yet.)
20050809	The function call trace tree now prints a per-function nr of
		arguments. (Semi-meaningless, since that data isn't read yet
		from the ELFs; some hardcoded symbols such as memcpy() and
		strlen() work fine, though.)
		More dyntrans refactoring; taking out more of the things that
		are common to all cpu families.
20050810	Working on adding support for "dual mode" for PPC dyntrans
		(i.e. both 64-bit and 32-bit modes).
		(Re)adding some simple PPC instructions.
20050811	Adding a dummy M68K cpu family. The dyntrans system isn't ready
		for variable-length ISAs yet, so it's completely bogus so far.
		Re-adding more PPC instructions.
		Adding a hack to src/file.c which allows OpenBSD/mac68k a.out
		kernels to be loaded.
		Beginning to add PPC loads/stores. So far they only work in
		32-bit mode.
20050812	The configure file option "add_remote" now accepts symbolic
		host names, in addition to numeric IPv4 addresses.
		Re-adding more PPC instructions.
20050814	Continuing to port back more PPC instructions.
		Found and fixed the cache/device write-update bug for 32-bit
		MIPS bintrans. :-)
		Triggered a really weird and annoying bug in Compaq's C
		compiler; ccc sometimes outputs code which loads from an
		address _before_ checking whether the pointer was NULL or not.
		(I'm not sure how to handle this problem.)
20050815	Removing all of the old x86 instruction execution code; adding
		a new (dummy) dyntrans module for x86.
		Taking the first steps to extend the dyntrans system to support
		variable-length instructions.
		Slowly preparing for the next release.
20050816	Adding a dummy SPARC cpu module.
		Minor updates (documentation etc) for the release.

==============  RELEASE 0.3.5  ==============


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.29 2005/08/09 17:18:22 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, "strncmp") == 0)
246 n_args = 3;
247 if (strcmp(name, "memset") == 0)
248 n_args = 3;
249 if (strcmp(name, "memcpy") == 0)
250 n_args = 3;
251 if (strcmp(name, "bzero") == 0)
252 n_args = 2;
253 if (strcmp(name, "bcopy") == 0)
254 n_args = 3;
255 }
256
257 if ((addr >> 32) == 0 && (addr & 0x80000000ULL))
258 addr |= 0xffffffff00000000ULL;
259
260 s = malloc(sizeof(struct symbol));
261 if (s == NULL) {
262 fprintf(stderr, "out of memory\n");
263 exit(1);
264 }
265
266 s->name = strdup(name);
267 if (s->name == NULL) {
268 fprintf(stderr, "out of memory\n");
269 exit(1);
270 }
271 s->addr = addr;
272 s->len = len;
273 s->type = type;
274 s->n_args = n_args;
275
276 sc->n_symbols ++;
277
278 /* Add first in list: */
279 s->next = sc->first_symbol;
280 sc->first_symbol = s;
281 }
282
283
284 /*
285 * symbol_readfile():
286 *
287 * Read 'nm -S' style symbols from a file.
288 *
289 * TODO: This function is an ugly hack, and should be replaced
290 * with something that reads symbols directly from the executable
291 * images.
292 */
293 void symbol_readfile(struct symbol_context *sc, char *fname)
294 {
295 FILE *f;
296 char b1[80]; uint64_t addr;
297 char b2[80]; uint64_t len;
298 char b3[80]; int type;
299 char b4[80];
300 int cur_n_symbols = sc->n_symbols;
301
302 f = fopen(fname, "r");
303 if (f == NULL) {
304 perror(fname);
305 exit(1);
306 }
307
308 while (!feof(f)) {
309 memset(b1, 0, sizeof(b1));
310 memset(b2, 0, sizeof(b2));
311 memset(b3, 0, sizeof(b3));
312 memset(b4, 0, sizeof(b4));
313 fscanf(f, "%s %s\n", b1,b2);
314 if (strlen(b2) < 2 && !(b2[0]>='0' && b2[0]<='9')) {
315 strlcpy(b3, b2, sizeof(b3));
316 strlcpy(b2, "0", sizeof(b2));
317 fscanf(f, "%s\n", b4);
318 } else {
319 fscanf(f, "%s %s\n", b3,b4);
320 }
321
322 /* printf("b1='%s' b2='%s' b3='%s' b4='%s'\n",
323 b1,b2,b3,b4); */
324 addr = strtoull(b1, NULL, 16);
325 len = strtoull(b2, NULL, 16);
326 type = b3[0];
327 /* printf("addr=%016llx len=%016llx type=%i\n",
328 addr, len, type); */
329
330 if (type == 't' || type == 'r' || type == 'g')
331 continue;
332
333 add_symbol_name(sc, addr, len, b4, type, -1);
334 }
335
336 fclose(f);
337
338 debug("%i symbols\n", sc->n_symbols - cur_n_symbols);
339 }
340
341
342 /*
343 * sym_addr_compare():
344 *
345 * Helper function for sorting symbols according to their address.
346 */
347 int sym_addr_compare(const void *a, const void *b)
348 {
349 struct symbol *p1 = (struct symbol *) a;
350 struct symbol *p2 = (struct symbol *) b;
351
352 if (p1->addr < p2->addr)
353 return -1;
354 if (p1->addr > p2->addr)
355 return 1;
356
357 return 0;
358 }
359
360
361 /*
362 * symbol_recalc_sizes():
363 *
364 * Recalculate sizes of symbols that have size = 0, by creating an array
365 * containing all symbols, qsort()-ing that array according to address, and
366 * recalculating the size fields if necessary.
367 */
368 void symbol_recalc_sizes(struct symbol_context *sc)
369 {
370 struct symbol *tmp_array;
371 struct symbol *last_ptr;
372 struct symbol *tmp_ptr;
373 int i;
374
375 tmp_array = malloc(sizeof (struct symbol) * sc->n_symbols);
376 if (tmp_array == NULL) {
377 fprintf(stderr, "out of memory\n");
378 exit(1);
379 }
380
381 /* Copy first_symbol --> tmp_array, and remove the old
382 first_symbol at the same time: */
383 tmp_ptr = sc->first_symbol;
384 i = 0;
385 while (tmp_ptr != NULL) {
386 tmp_array[i] = *tmp_ptr;
387 last_ptr = tmp_ptr;
388 tmp_ptr = tmp_ptr->next;
389 free(last_ptr);
390 i++;
391 }
392
393 qsort(tmp_array, sc->n_symbols, sizeof(struct symbol),
394 sym_addr_compare);
395 sc->sorted_array = 1;
396
397 /* Recreate the first_symbol chain: */
398 sc->first_symbol = NULL;
399 for (i=0; i<sc->n_symbols; i++) {
400 /* Recalculate size, if 0: */
401 if (tmp_array[i].len == 0) {
402 uint64_t len;
403 if (i != sc->n_symbols-1)
404 len = tmp_array[i+1].addr
405 - tmp_array[i].addr;
406 else
407 len = 1;
408 tmp_array[i].len = len;
409 }
410
411 tmp_array[i].next = &tmp_array[i+1];
412 }
413
414 sc->first_symbol = tmp_array;
415 }
416
417
418 /*
419 * symbol_init():
420 *
421 * Initialize the symbol hashtables.
422 */
423 void symbol_init(struct symbol_context *sc)
424 {
425 sc->first_symbol = NULL;
426 sc->sorted_array = 0;
427 sc->n_symbols = 0;
428 }
429

  ViewVC Help
Powered by ViewVC 1.1.26