/[gxemul]/upstream/0.3.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

Contents of /upstream/0.3.1/src/symbol.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3 - (show annotations)
Mon Oct 8 16:17:52 2007 UTC (16 years, 8 months ago) by dpavlin
File MIME type: text/plain
File size: 9042 byte(s)
0.3.1
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.23 2005/03/20 11:11:48 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():
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 if ((addr >> 32) == 0 && (addr & 0x80000000ULL))
209 addr |= 0xffffffff00000000ULL;
210
211 s = malloc(sizeof(struct symbol));
212 if (s == NULL) {
213 fprintf(stderr, "out of memory\n");
214 exit(1);
215 }
216
217 s->name = strdup(name);
218 if (s->name == NULL) {
219 fprintf(stderr, "out of memory\n");
220 exit(1);
221 }
222 s->addr = addr;
223 s->len = len;
224 s->type = type;
225
226 sc->n_symbols ++;
227
228 /* Add first in list: */
229 s->next = sc->first_symbol;
230 sc->first_symbol = s;
231 }
232
233
234 /*
235 * symbol_readfile():
236 *
237 * Read 'nm -S' style symbols from a file.
238 *
239 * TODO: This function is an ugly hack, and should be replaced
240 * with something that reads symbols directly from the executable
241 * images.
242 */
243 void symbol_readfile(struct symbol_context *sc, char *fname)
244 {
245 FILE *f;
246 char b1[80]; uint64_t addr;
247 char b2[80]; uint64_t len;
248 char b3[80]; int type;
249 char b4[80];
250 int cur_n_symbols = sc->n_symbols;
251
252 f = fopen(fname, "r");
253 if (f == NULL) {
254 perror(fname);
255 exit(1);
256 }
257
258 while (!feof(f)) {
259 memset(b1, 0, sizeof(b1));
260 memset(b2, 0, sizeof(b2));
261 memset(b3, 0, sizeof(b3));
262 memset(b4, 0, sizeof(b4));
263 fscanf(f, "%s %s\n", b1,b2);
264 if (strlen(b2) < 2 && !(b2[0]>='0' && b2[0]<='9')) {
265 strcpy(b3, b2);
266 strcpy(b2, "0");
267 fscanf(f, "%s\n", b4);
268 } else {
269 fscanf(f, "%s %s\n", b3,b4);
270 }
271
272 /* printf("b1='%s' b2='%s' b3='%s' b4='%s'\n",
273 b1,b2,b3,b4); */
274 addr = strtoull(b1, NULL, 16);
275 len = strtoull(b2, NULL, 16);
276 type = b3[0];
277 /* printf("addr=%016llx len=%016llx type=%i\n",
278 addr, len, type); */
279
280 if (type == 't' || type == 'r' || type == 'g')
281 continue;
282
283 add_symbol_name(sc, addr, len, b4, type);
284 }
285
286 fclose(f);
287
288 debug("%i symbols\n", sc->n_symbols - cur_n_symbols);
289 }
290
291
292 /*
293 * sym_addr_compare():
294 *
295 * Helper function for sorting symbols according to their address.
296 */
297 int sym_addr_compare(const void *a, const void *b)
298 {
299 struct symbol *p1 = (struct symbol *) a;
300 struct symbol *p2 = (struct symbol *) b;
301
302 if (p1->addr < p2->addr)
303 return -1;
304 if (p1->addr > p2->addr)
305 return 1;
306
307 return 0;
308 }
309
310
311 /*
312 * symbol_recalc_sizes():
313 *
314 * Recalculate sizes of symbols that have size = 0, by creating an array
315 * containing all symbols, qsort()-ing that array according to address, and
316 * recalculating the size fields if necessary.
317 */
318 void symbol_recalc_sizes(struct symbol_context *sc)
319 {
320 struct symbol *tmp_array;
321 struct symbol *last_ptr;
322 struct symbol *tmp_ptr;
323 int i;
324
325 tmp_array = malloc(sizeof (struct symbol) * sc->n_symbols);
326 if (tmp_array == NULL) {
327 fprintf(stderr, "out of memory\n");
328 exit(1);
329 }
330
331 /* Copy first_symbol --> tmp_array, and remove the old
332 first_symbol at the same time: */
333 tmp_ptr = sc->first_symbol;
334 i = 0;
335 while (tmp_ptr != NULL) {
336 tmp_array[i] = *tmp_ptr;
337 last_ptr = tmp_ptr;
338 tmp_ptr = tmp_ptr->next;
339 free(last_ptr);
340 i++;
341 }
342
343 qsort(tmp_array, sc->n_symbols, sizeof(struct symbol),
344 sym_addr_compare);
345 sc->sorted_array = 1;
346
347 /* Recreate the first_symbol chain: */
348 sc->first_symbol = NULL;
349 for (i=0; i<sc->n_symbols; i++) {
350 /* Recalculate size, if 0: */
351 if (tmp_array[i].len == 0) {
352 uint64_t len;
353 if (i != sc->n_symbols-1)
354 len = tmp_array[i+1].addr
355 - tmp_array[i].addr;
356 else
357 len = 1;
358 tmp_array[i].len = len;
359 }
360
361 tmp_array[i].next = &tmp_array[i+1];
362 }
363
364 sc->first_symbol = tmp_array;
365 }
366
367
368 /*
369 * symbol_init():
370 *
371 * Initialize the symbol hashtables.
372 */
373 void symbol_init(struct symbol_context *sc)
374 {
375 sc->first_symbol = NULL;
376 sc->sorted_array = 0;
377 sc->n_symbols = 0;
378 }
379

  ViewVC Help
Powered by ViewVC 1.1.26