]>
Commit | Line | Data |
---|---|---|
f32fb3fd ILT |
1 | /* stabs.c -- Parse COFF debugging information |
2 | Copyright (C) 1996 Free Software Foundation, Inc. | |
3 | Written by Ian Lance Taylor <[email protected]>. | |
4 | ||
5 | This file is part of GNU Binutils. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
20 | 02111-1307, USA. */ | |
21 | ||
22 | /* This file contains code which parses COFF debugging information. */ | |
23 | ||
24 | #include "bfd.h" | |
25 | #include "coff/internal.h" | |
26 | #include "bucomm.h" | |
27 | #include "libiberty.h" | |
28 | #include "demangle.h" | |
29 | #include "debug.h" | |
30 | #include "budbg.h" | |
31 | ||
32 | /* FIXME: We should not need this BFD internal file. We need it for | |
33 | the N_BTMASK, etc., values. */ | |
34 | #include "libcoff.h" | |
35 | ||
36 | /* These macros extract the right mask and shifts for this BFD. They | |
37 | assume that there is a local variable named ABFD. This is so that | |
38 | macros like ISFCN and DECREF, from coff/internal.h, will work | |
39 | without modification. */ | |
40 | #define N_BTMASK (coff_data (abfd)->local_n_btmask) | |
41 | #define N_BTSHFT (coff_data (abfd)->local_n_btshft) | |
42 | #define N_TMASK (coff_data (abfd)->local_n_tmask) | |
43 | #define N_TSHIFT (coff_data (abfd)->local_n_tshift) | |
44 | ||
45 | /* This structure is used to hold the symbols, as well as the current | |
46 | location within the symbols. */ | |
47 | ||
48 | struct coff_symbols | |
49 | { | |
50 | /* The symbols. */ | |
51 | asymbol **syms; | |
52 | /* The number of symbols. */ | |
53 | long symcount; | |
54 | /* The index of the current symbol. */ | |
55 | long symno; | |
56 | /* The index of the current symbol in the COFF symbol table (where | |
57 | each auxent counts as a symbol). */ | |
58 | long coff_symno; | |
59 | }; | |
60 | ||
61 | /* The largest basic type we are prepared to handle. */ | |
62 | ||
63 | #define T_MAX (T_LNGDBL) | |
64 | ||
65 | /* This structure is used to hold slots. */ | |
66 | ||
67 | struct coff_slots | |
68 | { | |
69 | /* Next set of slots. */ | |
70 | struct coff_slots *next; | |
71 | /* Slots. */ | |
72 | #define COFF_SLOTS (16) | |
73 | debug_type slots[COFF_SLOTS]; | |
74 | }; | |
75 | ||
76 | /* This structure is used to map symbol indices to types. */ | |
77 | ||
78 | struct coff_types | |
79 | { | |
80 | /* Slots. */ | |
81 | struct coff_slots *slots; | |
82 | /* Basic types. */ | |
83 | debug_type basic[T_MAX + 1]; | |
84 | }; | |
85 | ||
86 | static debug_type *coff_get_slot PARAMS ((struct coff_types *, int)); | |
87 | static debug_type parse_coff_type | |
88 | PARAMS ((bfd *, struct coff_symbols *, struct coff_types *, long, int, | |
89 | union internal_auxent *, boolean, PTR)); | |
90 | static debug_type parse_coff_base_type | |
91 | PARAMS ((bfd *, struct coff_symbols *, struct coff_types *, long, int, | |
92 | union internal_auxent *, PTR)); | |
93 | static debug_type parse_coff_struct_type | |
94 | PARAMS ((bfd *, struct coff_symbols *, struct coff_types *, int, | |
95 | union internal_auxent *, PTR)); | |
96 | static debug_type parse_coff_enum_type | |
97 | PARAMS ((bfd *, struct coff_symbols *, struct coff_types *, | |
98 | union internal_auxent *, PTR)); | |
99 | static boolean parse_coff_symbol | |
100 | PARAMS ((bfd *, struct coff_types *, asymbol *, long, | |
101 | struct internal_syment *, PTR, debug_type, boolean)); | |
102 | \f | |
103 | /* Return the slot for a type. */ | |
104 | ||
105 | static debug_type * | |
106 | coff_get_slot (types, indx) | |
107 | struct coff_types *types; | |
108 | int indx; | |
109 | { | |
110 | struct coff_slots **pps; | |
111 | ||
112 | pps = &types->slots; | |
113 | ||
114 | while (indx >= COFF_SLOTS) | |
115 | { | |
116 | if (*pps == NULL) | |
117 | { | |
118 | *pps = (struct coff_slots *) xmalloc (sizeof **pps); | |
119 | memset (*pps, 0, sizeof **pps); | |
120 | } | |
121 | pps = &(*pps)->next; | |
122 | indx -= COFF_SLOTS; | |
123 | } | |
124 | ||
125 | if (*pps == NULL) | |
126 | { | |
127 | *pps = (struct coff_slots *) xmalloc (sizeof **pps); | |
128 | memset (*pps, 0, sizeof **pps); | |
129 | } | |
130 | ||
131 | return (*pps)->slots + indx; | |
132 | } | |
133 | ||
134 | /* Parse a COFF type code in NTYPE. */ | |
135 | ||
136 | static debug_type | |
137 | parse_coff_type (abfd, symbols, types, coff_symno, ntype, pauxent, useaux, | |
138 | dhandle) | |
139 | bfd *abfd; | |
140 | struct coff_symbols *symbols; | |
141 | struct coff_types *types; | |
142 | long coff_symno; | |
143 | int ntype; | |
144 | union internal_auxent *pauxent; | |
145 | boolean useaux; | |
146 | PTR dhandle; | |
147 | { | |
148 | debug_type type; | |
149 | ||
150 | if ((ntype & ~N_BTMASK) != 0) | |
151 | { | |
152 | int newtype; | |
153 | ||
154 | newtype = DECREF (ntype); | |
155 | ||
156 | if (ISPTR (ntype)) | |
157 | { | |
158 | type = parse_coff_type (abfd, symbols, types, coff_symno, newtype, | |
159 | pauxent, useaux, dhandle); | |
160 | type = debug_make_pointer_type (dhandle, type); | |
161 | } | |
162 | else if (ISFCN (ntype)) | |
163 | { | |
164 | type = parse_coff_type (abfd, symbols, types, coff_symno, newtype, | |
165 | pauxent, useaux, dhandle); | |
166 | type = debug_make_function_type (dhandle, type, (debug_type *) NULL, | |
167 | false); | |
168 | } | |
169 | else if (ISARY (ntype)) | |
170 | { | |
171 | int n; | |
172 | ||
173 | if (pauxent == NULL) | |
174 | n = 0; | |
175 | else | |
176 | { | |
177 | unsigned short *dim; | |
178 | int i; | |
179 | ||
180 | /* FIXME: If pauxent->x_sym.x_tagndx.l == 0, gdb sets | |
181 | the c_naux field of the syment to 0. */ | |
182 | ||
183 | /* Move the dimensions down, so that the next array | |
184 | picks up the next one. */ | |
185 | dim = pauxent->x_sym.x_fcnary.x_ary.x_dimen; | |
186 | n = dim[0]; | |
187 | for (i = 0; *dim != 0 && i < DIMNUM - 1; i++, dim++) | |
188 | *dim = *(dim + 1); | |
189 | *dim = 0; | |
190 | } | |
191 | ||
192 | type = parse_coff_type (abfd, symbols, types, coff_symno, newtype, | |
193 | pauxent, false, dhandle); | |
194 | type = debug_make_array_type (dhandle, type, | |
195 | parse_coff_base_type (abfd, symbols, | |
196 | types, | |
197 | coff_symno, | |
198 | T_INT, | |
199 | NULL, dhandle), | |
200 | 0, n - 1, false); | |
201 | } | |
202 | ||
203 | return type; | |
204 | } | |
205 | ||
206 | if (pauxent != NULL && pauxent->x_sym.x_tagndx.l > 0) | |
207 | { | |
208 | debug_type *slot; | |
209 | ||
210 | /* This is a reference to an existing type. FIXME: gdb checks | |
211 | that the class is not C_STRTAG, nor C_UNTAG, nor C_ENTAG. */ | |
212 | slot = coff_get_slot (types, pauxent->x_sym.x_tagndx.l); | |
213 | if (*slot != DEBUG_TYPE_NULL) | |
214 | return *slot; | |
215 | else | |
216 | return debug_make_indirect_type (dhandle, slot, (const char *) NULL); | |
217 | } | |
218 | ||
219 | /* If the aux entry has already been used for something, useaux will | |
220 | have been set to false, indicating that parse_coff_base_type | |
221 | should not use it. We need to do it this way, rather than simply | |
222 | passing pauxent as NULL, because we need to be able handle | |
223 | multiple array dimensions while still discarding pauxent after | |
224 | having handled all of them. */ | |
225 | if (! useaux) | |
226 | pauxent = NULL; | |
227 | ||
228 | return parse_coff_base_type (abfd, symbols, types, coff_symno, ntype, | |
229 | pauxent, dhandle); | |
230 | } | |
231 | ||
232 | /* Parse a basic COFF type in NTYPE. */ | |
233 | ||
234 | static debug_type | |
235 | parse_coff_base_type (abfd, symbols, types, coff_symno, ntype, pauxent, | |
236 | dhandle) | |
237 | bfd *abfd; | |
238 | struct coff_symbols *symbols; | |
239 | struct coff_types *types; | |
240 | long coff_symno; | |
241 | int ntype; | |
242 | union internal_auxent *pauxent; | |
243 | PTR dhandle; | |
244 | { | |
245 | debug_type ret; | |
246 | boolean set_basic; | |
247 | const char *name; | |
248 | debug_type *slot; | |
249 | ||
250 | if (ntype >= 0 | |
251 | && ntype <= T_MAX | |
252 | && types->basic[ntype] != DEBUG_TYPE_NULL) | |
253 | return types->basic[ntype]; | |
254 | ||
255 | set_basic = true; | |
256 | name = NULL; | |
257 | ||
258 | switch (ntype) | |
259 | { | |
260 | default: | |
261 | ret = debug_make_void_type (dhandle); | |
262 | break; | |
263 | ||
264 | case T_NULL: | |
265 | case T_VOID: | |
266 | ret = debug_make_void_type (dhandle); | |
267 | name = "void"; | |
268 | break; | |
269 | ||
270 | case T_CHAR: | |
271 | ret = debug_make_int_type (dhandle, 1, false); | |
272 | name = "char"; | |
273 | break; | |
274 | ||
275 | case T_SHORT: | |
276 | ret = debug_make_int_type (dhandle, 2, false); | |
277 | name = "short"; | |
278 | break; | |
279 | ||
280 | case T_INT: | |
281 | /* FIXME: Perhaps the size should depend upon the architecture. */ | |
282 | ret = debug_make_int_type (dhandle, 4, false); | |
283 | name = "int"; | |
284 | break; | |
285 | ||
286 | case T_LONG: | |
287 | ret = debug_make_int_type (dhandle, 4, false); | |
288 | name = "long"; | |
289 | break; | |
290 | ||
291 | case T_FLOAT: | |
292 | ret = debug_make_float_type (dhandle, 4); | |
293 | name = "float"; | |
294 | break; | |
295 | ||
296 | case T_DOUBLE: | |
297 | ret = debug_make_float_type (dhandle, 8); | |
298 | name = "double"; | |
299 | break; | |
300 | ||
301 | case T_LNGDBL: | |
302 | ret = debug_make_float_type (dhandle, 12); | |
303 | name = "long double"; | |
304 | break; | |
305 | ||
306 | case T_UCHAR: | |
307 | ret = debug_make_int_type (dhandle, 1, true); | |
308 | name = "unsigned char"; | |
309 | break; | |
310 | ||
311 | case T_USHORT: | |
312 | ret = debug_make_int_type (dhandle, 2, true); | |
313 | name = "unsigned short"; | |
314 | break; | |
315 | ||
316 | case T_UINT: | |
317 | ret = debug_make_int_type (dhandle, 4, true); | |
318 | name = "unsigned int"; | |
319 | break; | |
320 | ||
321 | case T_ULONG: | |
322 | ret = debug_make_int_type (dhandle, 4, true); | |
323 | name = "unsigned long"; | |
324 | break; | |
325 | ||
326 | case T_STRUCT: | |
327 | if (pauxent == NULL) | |
328 | ret = debug_make_struct_type (dhandle, true, 0, | |
329 | (debug_field *) NULL); | |
330 | else | |
331 | ret = parse_coff_struct_type (abfd, symbols, types, ntype, pauxent, | |
332 | dhandle); | |
333 | ||
334 | slot = coff_get_slot (types, coff_symno); | |
335 | *slot = ret; | |
336 | ||
337 | set_basic = false; | |
338 | break; | |
339 | ||
340 | case T_UNION: | |
341 | if (pauxent == NULL) | |
342 | ret = debug_make_struct_type (dhandle, false, 0, (debug_field *) NULL); | |
343 | else | |
344 | ret = parse_coff_struct_type (abfd, symbols, types, ntype, pauxent, | |
345 | dhandle); | |
346 | ||
347 | slot = coff_get_slot (types, coff_symno); | |
348 | *slot = ret; | |
349 | ||
350 | set_basic = false; | |
351 | break; | |
352 | ||
353 | case T_ENUM: | |
354 | if (pauxent == NULL) | |
355 | ret = debug_make_enum_type (dhandle, (const char **) NULL, | |
356 | (bfd_signed_vma *) NULL); | |
357 | else | |
358 | ret = parse_coff_enum_type (abfd, symbols, types, pauxent, dhandle); | |
359 | ||
360 | slot = coff_get_slot (types, coff_symno); | |
361 | *slot = ret; | |
362 | ||
363 | set_basic = false; | |
364 | break; | |
365 | } | |
366 | ||
367 | if (name != NULL) | |
368 | ret = debug_name_type (dhandle, name, ret); | |
369 | ||
370 | if (set_basic | |
371 | && ntype >= 0 | |
372 | && ntype <= T_MAX) | |
373 | types->basic[ntype] = ret; | |
374 | ||
375 | return ret; | |
376 | } | |
377 | ||
378 | /* Parse a struct type. */ | |
379 | ||
380 | static debug_type | |
381 | parse_coff_struct_type (abfd, symbols, types, ntype, pauxent, dhandle) | |
382 | bfd *abfd; | |
383 | struct coff_symbols *symbols; | |
384 | struct coff_types *types; | |
385 | int ntype; | |
386 | union internal_auxent *pauxent; | |
387 | PTR dhandle; | |
388 | { | |
389 | long symend; | |
390 | int alloc; | |
391 | debug_field *fields; | |
392 | int count; | |
393 | boolean done; | |
394 | ||
395 | symend = pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l; | |
396 | ||
397 | alloc = 10; | |
398 | fields = (debug_field *) xmalloc (alloc * sizeof *fields); | |
399 | count = 0; | |
400 | ||
401 | done = false; | |
402 | while (! done | |
403 | && symbols->coff_symno < symend | |
404 | && symbols->symno < symbols->symcount) | |
405 | { | |
406 | asymbol *sym; | |
407 | long this_coff_symno; | |
408 | struct internal_syment syment; | |
409 | union internal_auxent auxent; | |
410 | union internal_auxent *psubaux; | |
411 | bfd_vma bitpos = 0, bitsize = 0; | |
412 | ||
413 | sym = symbols->syms[symbols->symno]; | |
414 | ||
415 | if (! bfd_coff_get_syment (abfd, sym, &syment)) | |
416 | { | |
417 | fprintf (stderr, "%s: bfd_coff_get_syment failed: %s\n", | |
418 | program_name, bfd_errmsg (bfd_get_error ())); | |
419 | return DEBUG_TYPE_NULL; | |
420 | } | |
421 | ||
422 | this_coff_symno = symbols->coff_symno; | |
423 | ||
424 | ++symbols->symno; | |
425 | symbols->coff_symno += 1 + syment.n_numaux; | |
426 | ||
427 | if (syment.n_numaux == 0) | |
428 | psubaux = NULL; | |
429 | else | |
430 | { | |
431 | if (! bfd_coff_get_auxent (abfd, sym, 0, &auxent)) | |
432 | { | |
433 | fprintf (stderr, "%s: bfd_coff_get_auxent failed: %s\n", | |
434 | program_name, bfd_errmsg (bfd_get_error ())); | |
435 | return DEBUG_TYPE_NULL; | |
436 | } | |
437 | psubaux = &auxent; | |
438 | } | |
439 | ||
440 | switch (syment.n_sclass) | |
441 | { | |
442 | case C_MOS: | |
443 | case C_MOU: | |
444 | bitpos = 8 * bfd_asymbol_value (sym); | |
445 | bitsize = 0; | |
446 | break; | |
447 | ||
448 | case C_FIELD: | |
449 | bitpos = bfd_asymbol_value (sym); | |
450 | bitsize = auxent.x_sym.x_misc.x_lnsz.x_size; | |
451 | break; | |
452 | ||
453 | case C_EOS: | |
454 | done = true; | |
455 | break; | |
456 | } | |
457 | ||
458 | if (! done) | |
459 | { | |
460 | debug_type ftype; | |
461 | debug_field f; | |
462 | ||
463 | ftype = parse_coff_type (abfd, symbols, types, this_coff_symno, | |
464 | syment.n_type, psubaux, true, dhandle); | |
465 | f = debug_make_field (dhandle, bfd_asymbol_name (sym), ftype, | |
466 | bitpos, bitsize, DEBUG_VISIBILITY_PUBLIC); | |
467 | if (f == DEBUG_FIELD_NULL) | |
468 | return DEBUG_TYPE_NULL; | |
469 | ||
470 | if (count + 1 >= alloc) | |
471 | { | |
472 | alloc += 10; | |
473 | fields = ((debug_field *) | |
474 | xrealloc (fields, alloc * sizeof *fields)); | |
475 | } | |
476 | ||
477 | fields[count] = f; | |
478 | ++count; | |
479 | } | |
480 | } | |
481 | ||
482 | fields[count] = DEBUG_FIELD_NULL; | |
483 | ||
484 | return debug_make_struct_type (dhandle, ntype == T_STRUCT, | |
485 | pauxent->x_sym.x_misc.x_lnsz.x_size, | |
486 | fields); | |
487 | } | |
488 | ||
489 | /* Parse an enum type. */ | |
490 | ||
491 | static debug_type | |
492 | parse_coff_enum_type (abfd, symbols, types, pauxent, dhandle) | |
493 | bfd *abfd; | |
494 | struct coff_symbols *symbols; | |
495 | struct coff_types *types; | |
496 | union internal_auxent *pauxent; | |
497 | PTR dhandle; | |
498 | { | |
499 | long symend; | |
500 | int alloc; | |
501 | const char **names; | |
502 | bfd_signed_vma *vals; | |
503 | int count; | |
504 | boolean done; | |
505 | ||
506 | symend = pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l; | |
507 | ||
508 | alloc = 10; | |
509 | names = (const char **) xmalloc (alloc * sizeof *names); | |
510 | vals = (bfd_signed_vma *) xmalloc (alloc * sizeof *vals); | |
511 | count = 0; | |
512 | ||
513 | done = false; | |
514 | while (! done | |
515 | && symbols->coff_symno < symend | |
516 | && symbols->symno < symbols->symcount) | |
517 | { | |
518 | asymbol *sym; | |
519 | struct internal_syment syment; | |
520 | ||
521 | sym = symbols->syms[symbols->symno]; | |
522 | ||
523 | if (! bfd_coff_get_syment (abfd, sym, &syment)) | |
524 | { | |
525 | fprintf (stderr, "%s: bfd_coff_get_syment failed: %s\n", | |
526 | program_name, bfd_errmsg (bfd_get_error ())); | |
527 | return DEBUG_TYPE_NULL; | |
528 | } | |
529 | ||
530 | ++symbols->symno; | |
531 | symbols->coff_symno += 1 + syment.n_numaux; | |
532 | ||
533 | switch (syment.n_sclass) | |
534 | { | |
535 | case C_MOE: | |
536 | if (count + 1 >= alloc) | |
537 | { | |
538 | alloc += 10; | |
539 | names = ((const char **) | |
540 | xrealloc (names, alloc * sizeof *names)); | |
541 | vals = ((bfd_signed_vma *) | |
542 | xrealloc (vals, alloc * sizeof *vals)); | |
543 | } | |
544 | ||
545 | names[count] = bfd_asymbol_name (sym); | |
546 | vals[count] = bfd_asymbol_value (sym); | |
547 | ++count; | |
548 | break; | |
549 | ||
550 | case C_EOS: | |
551 | done = true; | |
552 | break; | |
553 | } | |
554 | } | |
555 | ||
556 | names[count] = NULL; | |
557 | ||
558 | return debug_make_enum_type (dhandle, names, vals); | |
559 | } | |
560 | ||
561 | /* Handle a single COFF symbol. */ | |
562 | ||
563 | static boolean | |
564 | parse_coff_symbol (abfd, types, sym, coff_symno, psyment, dhandle, type, | |
565 | within_function) | |
566 | bfd *abfd; | |
567 | struct coff_types *types; | |
568 | asymbol *sym; | |
569 | long coff_symno; | |
570 | struct internal_syment *psyment; | |
571 | PTR dhandle; | |
572 | debug_type type; | |
573 | boolean within_function; | |
574 | { | |
575 | switch (psyment->n_sclass) | |
576 | { | |
577 | case C_NULL: | |
578 | break; | |
579 | ||
580 | case C_AUTO: | |
581 | if (! debug_record_variable (dhandle, bfd_asymbol_name (sym), type, | |
582 | DEBUG_LOCAL, bfd_asymbol_value (sym))) | |
583 | return false; | |
584 | break; | |
585 | ||
586 | case C_EXT: | |
587 | if (! debug_record_variable (dhandle, bfd_asymbol_name (sym), type, | |
588 | DEBUG_GLOBAL, bfd_asymbol_value (sym))) | |
589 | return false; | |
590 | break; | |
591 | ||
592 | case C_STAT: | |
593 | if (! debug_record_variable (dhandle, bfd_asymbol_name (sym), type, | |
594 | (within_function | |
595 | ? DEBUG_LOCAL_STATIC | |
596 | : DEBUG_STATIC), | |
597 | bfd_asymbol_value (sym))) | |
598 | return false; | |
599 | break; | |
600 | ||
601 | case C_REG: | |
602 | /* FIXME: We may need to convert the register number. */ | |
603 | if (! debug_record_variable (dhandle, bfd_asymbol_name (sym), type, | |
604 | DEBUG_REGISTER, bfd_asymbol_value (sym))) | |
605 | return false; | |
606 | break; | |
607 | ||
608 | case C_LABEL: | |
609 | break; | |
610 | ||
611 | case C_ARG: | |
612 | if (! debug_record_parameter (dhandle, bfd_asymbol_name (sym), type, | |
613 | DEBUG_PARM_STACK, bfd_asymbol_value (sym))) | |
614 | return false; | |
615 | break; | |
616 | ||
617 | case C_REGPARM: | |
618 | /* FIXME: We may need to convert the register number. */ | |
619 | if (! debug_record_parameter (dhandle, bfd_asymbol_name (sym), type, | |
620 | DEBUG_PARM_REG, bfd_asymbol_value (sym))) | |
621 | return false; | |
622 | break; | |
623 | ||
624 | case C_TPDEF: | |
625 | type = debug_name_type (dhandle, bfd_asymbol_name (sym), type); | |
626 | if (type == DEBUG_TYPE_NULL) | |
627 | return false; | |
628 | break; | |
629 | ||
630 | case C_STRTAG: | |
631 | case C_UNTAG: | |
632 | case C_ENTAG: | |
633 | { | |
634 | debug_type *slot; | |
635 | ||
636 | type = debug_tag_type (dhandle, bfd_asymbol_name (sym), type); | |
637 | if (type == DEBUG_TYPE_NULL) | |
638 | return false; | |
639 | ||
640 | /* Store the named type into the slot, so that references get | |
641 | the name. */ | |
642 | slot = coff_get_slot (types, coff_symno); | |
643 | *slot = type; | |
644 | } | |
645 | break; | |
646 | ||
647 | default: | |
648 | break; | |
649 | } | |
650 | ||
651 | return true; | |
652 | } | |
653 | ||
654 | /* This is the main routine. It looks through all the symbols and | |
655 | handles them. */ | |
656 | ||
657 | boolean | |
658 | parse_coff (abfd, syms, symcount, dhandle) | |
659 | bfd *abfd; | |
660 | asymbol **syms; | |
661 | long symcount; | |
662 | PTR dhandle; | |
663 | { | |
664 | struct coff_symbols symbols; | |
665 | struct coff_types types; | |
666 | int i; | |
667 | long next_c_file; | |
668 | const char *fnname; | |
669 | int fnclass; | |
670 | int fntype; | |
671 | alent *linenos; | |
672 | boolean within_function; | |
673 | long this_coff_symno; | |
674 | ||
675 | symbols.syms = syms; | |
676 | symbols.symcount = symcount; | |
677 | symbols.symno = 0; | |
678 | symbols.coff_symno = 0; | |
679 | ||
680 | types.slots = NULL; | |
681 | for (i = 0; i <= T_MAX; i++) | |
682 | types.basic[i] = DEBUG_TYPE_NULL; | |
683 | ||
684 | next_c_file = -1; | |
685 | fnname = NULL; | |
686 | fnclass = 0; | |
687 | fntype = 0; | |
688 | linenos = NULL; | |
689 | within_function = false; | |
690 | ||
691 | while (symbols.symno < symcount) | |
692 | { | |
693 | asymbol *sym; | |
694 | const char *name; | |
695 | struct internal_syment syment; | |
696 | union internal_auxent auxent; | |
697 | union internal_auxent *paux; | |
698 | debug_type type; | |
699 | ||
700 | sym = syms[symbols.symno]; | |
701 | ||
702 | if (! bfd_coff_get_syment (abfd, sym, &syment)) | |
703 | { | |
704 | fprintf (stderr, "%s: bfd_coff_get_syment failed: %s\n", | |
705 | program_name, bfd_errmsg (bfd_get_error ())); | |
706 | return false; | |
707 | } | |
708 | ||
709 | name = bfd_asymbol_name (sym); | |
710 | ||
711 | this_coff_symno = symbols.coff_symno; | |
712 | ||
713 | ++symbols.symno; | |
714 | symbols.coff_symno += 1 + syment.n_numaux; | |
715 | ||
716 | /* We only worry about the first auxent, because that is the | |
717 | only one which is relevant for debugging information. */ | |
718 | if (syment.n_numaux == 0) | |
719 | paux = NULL; | |
720 | else | |
721 | { | |
722 | if (! bfd_coff_get_auxent (abfd, sym, 0, &auxent)) | |
723 | { | |
724 | fprintf (stderr, "%s: bfd_coff_get_auxent failed: %s\n", | |
725 | program_name, bfd_errmsg (bfd_get_error ())); | |
726 | return false; | |
727 | } | |
728 | paux = &auxent; | |
729 | } | |
730 | ||
731 | if (this_coff_symno == next_c_file && syment.n_sclass != C_FILE) | |
732 | { | |
733 | /* The last C_FILE symbol points to the first external | |
734 | symbol. */ | |
735 | if (! debug_set_filename (dhandle, "*globals*")) | |
736 | return false; | |
737 | } | |
738 | ||
739 | switch (syment.n_sclass) | |
740 | { | |
741 | case C_EFCN: | |
742 | case C_EXTDEF: | |
743 | case C_ULABEL: | |
744 | case C_USTATIC: | |
745 | case C_LINE: | |
746 | case C_ALIAS: | |
747 | case C_HIDDEN: | |
748 | /* Just ignore these classes. */ | |
749 | break; | |
750 | ||
751 | case C_FILE: | |
752 | next_c_file = syment.n_value; | |
753 | if (! debug_set_filename (dhandle, name)) | |
754 | return false; | |
755 | break; | |
756 | ||
757 | case C_STAT: | |
758 | /* Ignore static symbols with a type of T_NULL. These | |
759 | represent section entries. */ | |
760 | if (syment.n_type == T_NULL) | |
761 | break; | |
762 | /* Fall through. */ | |
763 | case C_EXT: | |
764 | if (ISFCN (syment.n_type)) | |
765 | { | |
766 | fnname = name; | |
767 | fnclass = syment.n_sclass; | |
768 | fntype = syment.n_type; | |
769 | linenos = BFD_SEND (abfd, _get_lineno, (abfd, sym)); | |
770 | break; | |
771 | } | |
772 | type = parse_coff_type (abfd, &symbols, &types, this_coff_symno, | |
773 | syment.n_type, paux, true, dhandle); | |
774 | if (type == DEBUG_TYPE_NULL) | |
775 | return false; | |
776 | if (! parse_coff_symbol (abfd, &types, sym, this_coff_symno, &syment, | |
777 | dhandle, type, within_function)) | |
778 | return false; | |
779 | break; | |
780 | ||
781 | case C_FCN: | |
782 | if (strcmp (name, ".bf") == 0) | |
783 | { | |
784 | if (fnname == NULL) | |
785 | { | |
786 | fprintf (stderr, "%s: %ld: .bf without preceding function\n", | |
787 | program_name, this_coff_symno); | |
788 | return false; | |
789 | } | |
790 | ||
791 | type = parse_coff_type (abfd, &symbols, &types, this_coff_symno, | |
792 | DECREF (fntype), paux, false, dhandle); | |
793 | if (type == DEBUG_TYPE_NULL) | |
794 | return false; | |
795 | ||
796 | if (! debug_record_function (dhandle, fnname, type, | |
797 | fnclass == C_EXT, | |
798 | bfd_asymbol_value (sym))) | |
799 | return false; | |
800 | ||
801 | if (linenos != NULL) | |
802 | { | |
803 | int base; | |
804 | bfd_vma addr; | |
805 | ||
806 | if (syment.n_numaux == 0) | |
807 | base = 0; | |
808 | else | |
809 | base = auxent.x_sym.x_misc.x_lnsz.x_lnno - 1; | |
810 | ||
811 | addr = bfd_get_section_vma (abfd, bfd_get_section (sym)); | |
812 | ||
813 | ++linenos; | |
814 | ||
815 | while (linenos->line_number != 0) | |
816 | { | |
817 | if (! debug_record_line (dhandle, | |
818 | linenos->line_number + base, | |
819 | linenos->u.offset + addr)) | |
820 | return false; | |
821 | ++linenos; | |
822 | } | |
823 | } | |
824 | ||
825 | fnname = NULL; | |
826 | linenos = NULL; | |
827 | fnclass = 0; | |
828 | fntype = 0; | |
829 | ||
830 | within_function = true; | |
831 | } | |
832 | else if (strcmp (name, ".ef") == 0) | |
833 | { | |
834 | if (! within_function) | |
835 | { | |
836 | fprintf (stderr, "%s: %ld: unexpected .ef\n", | |
837 | program_name, this_coff_symno); | |
838 | return false; | |
839 | } | |
840 | ||
841 | if (! debug_end_function (dhandle, bfd_asymbol_value (sym))) | |
842 | return false; | |
843 | ||
844 | within_function = false; | |
845 | } | |
846 | break; | |
847 | ||
848 | case C_BLOCK: | |
849 | if (strcmp (name, ".bb") == 0) | |
850 | { | |
851 | if (! debug_start_block (dhandle, bfd_asymbol_value (sym))) | |
852 | return false; | |
853 | } | |
854 | else if (strcmp (name, ".eb") == 0) | |
855 | { | |
856 | if (! debug_end_block (dhandle, bfd_asymbol_value (sym))) | |
857 | return false; | |
858 | } | |
859 | break; | |
860 | ||
861 | default: | |
862 | type = parse_coff_type (abfd, &symbols, &types, this_coff_symno, | |
863 | syment.n_type, paux, true, dhandle); | |
864 | if (type == DEBUG_TYPE_NULL) | |
865 | return false; | |
866 | if (! parse_coff_symbol (abfd, &types, sym, this_coff_symno, &syment, | |
867 | dhandle, type, within_function)) | |
868 | return false; | |
869 | break; | |
870 | } | |
871 | } | |
872 | ||
873 | return true; | |
874 | } |