1 /* stabs.c -- Parse COFF debugging information
2 Copyright 1996, 1999, 2000, 2002, 2003 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@cygnus.com>.
5 This file is part of GNU Binutils.
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.
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.
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
22 /* This file contains code which parses COFF debugging information. */
25 #include "coff/internal.h"
27 #include "libiberty.h"
31 /* FIXME: We should not need this BFD internal file. We need it for
32 the N_BTMASK, etc., values. */
35 /* These macros extract the right mask and shifts for this BFD. They
36 assume that there is a local variable named ABFD. This is so that
37 macros like ISFCN and DECREF, from coff/internal.h, will work
38 without modification. */
39 #define N_BTMASK (coff_data (abfd)->local_n_btmask)
40 #define N_BTSHFT (coff_data (abfd)->local_n_btshft)
41 #define N_TMASK (coff_data (abfd)->local_n_tmask)
42 #define N_TSHIFT (coff_data (abfd)->local_n_tshift)
44 /* This structure is used to hold the symbols, as well as the current
45 location within the symbols. */
51 /* The number of symbols. */
53 /* The index of the current symbol. */
55 /* The index of the current symbol in the COFF symbol table (where
56 each auxent counts as a symbol). */
60 /* The largest basic type we are prepared to handle. */
62 #define T_MAX (T_LNGDBL)
64 /* This structure is used to hold slots. */
68 /* Next set of slots. */
69 struct coff_slots *next;
71 #define COFF_SLOTS (16)
72 debug_type slots[COFF_SLOTS];
75 /* This structure is used to map symbol indices to types. */
80 struct coff_slots *slots;
82 debug_type basic[T_MAX + 1];
85 static debug_type *coff_get_slot (struct coff_types *, int);
86 static debug_type parse_coff_type
87 (bfd *, struct coff_symbols *, struct coff_types *, long, int,
88 union internal_auxent *, bfd_boolean, void *);
89 static debug_type parse_coff_base_type
90 (bfd *, struct coff_symbols *, struct coff_types *, long, int,
91 union internal_auxent *, void *);
92 static debug_type parse_coff_struct_type
93 (bfd *, struct coff_symbols *, struct coff_types *, int,
94 union internal_auxent *, void *);
95 static debug_type parse_coff_enum_type
96 (bfd *, struct coff_symbols *, struct coff_types *,
97 union internal_auxent *, void *);
98 static bfd_boolean parse_coff_symbol
99 (bfd *, struct coff_types *, asymbol *, long, struct internal_syment *,
100 void *, debug_type, bfd_boolean);
101 static bfd_boolean external_coff_symbol_p (int sym_class);
103 /* Return the slot for a type. */
106 coff_get_slot (struct coff_types *types, int indx)
108 struct coff_slots **pps;
112 while (indx >= COFF_SLOTS)
116 *pps = (struct coff_slots *) xmalloc (sizeof **pps);
117 memset (*pps, 0, sizeof **pps);
125 *pps = (struct coff_slots *) xmalloc (sizeof **pps);
126 memset (*pps, 0, sizeof **pps);
129 return (*pps)->slots + indx;
132 /* Parse a COFF type code in NTYPE. */
135 parse_coff_type (bfd *abfd, struct coff_symbols *symbols,
136 struct coff_types *types, long coff_symno, int ntype,
137 union internal_auxent *pauxent, bfd_boolean useaux,
142 if ((ntype & ~N_BTMASK) != 0)
146 newtype = DECREF (ntype);
150 type = parse_coff_type (abfd, symbols, types, coff_symno, newtype,
151 pauxent, useaux, dhandle);
152 type = debug_make_pointer_type (dhandle, type);
154 else if (ISFCN (ntype))
156 type = parse_coff_type (abfd, symbols, types, coff_symno, newtype,
157 pauxent, useaux, dhandle);
158 type = debug_make_function_type (dhandle, type, (debug_type *) NULL,
161 else if (ISARY (ntype))
172 /* FIXME: If pauxent->x_sym.x_tagndx.l == 0, gdb sets
173 the c_naux field of the syment to 0. */
175 /* Move the dimensions down, so that the next array
176 picks up the next one. */
177 dim = pauxent->x_sym.x_fcnary.x_ary.x_dimen;
179 for (i = 0; *dim != 0 && i < DIMNUM - 1; i++, dim++)
184 type = parse_coff_type (abfd, symbols, types, coff_symno, newtype,
185 pauxent, FALSE, dhandle);
186 type = debug_make_array_type (dhandle, type,
187 parse_coff_base_type (abfd, symbols,
196 non_fatal (_("parse_coff_type: Bad type code 0x%x"), ntype);
197 return DEBUG_TYPE_NULL;
203 if (pauxent != NULL && pauxent->x_sym.x_tagndx.l > 0)
207 /* This is a reference to an existing type. FIXME: gdb checks
208 that the class is not C_STRTAG, nor C_UNTAG, nor C_ENTAG. */
209 slot = coff_get_slot (types, pauxent->x_sym.x_tagndx.l);
210 if (*slot != DEBUG_TYPE_NULL)
213 return debug_make_indirect_type (dhandle, slot, (const char *) NULL);
216 /* If the aux entry has already been used for something, useaux will
217 have been set to false, indicating that parse_coff_base_type
218 should not use it. We need to do it this way, rather than simply
219 passing pauxent as NULL, because we need to be able handle
220 multiple array dimensions while still discarding pauxent after
221 having handled all of them. */
225 return parse_coff_base_type (abfd, symbols, types, coff_symno, ntype,
229 /* Parse a basic COFF type in NTYPE. */
232 parse_coff_base_type (bfd *abfd, struct coff_symbols *symbols,
233 struct coff_types *types, long coff_symno, int ntype,
234 union internal_auxent *pauxent, void *dhandle)
237 bfd_boolean set_basic;
243 && types->basic[ntype] != DEBUG_TYPE_NULL)
244 return types->basic[ntype];
252 ret = debug_make_void_type (dhandle);
257 ret = debug_make_void_type (dhandle);
262 ret = debug_make_int_type (dhandle, 1, FALSE);
267 ret = debug_make_int_type (dhandle, 2, FALSE);
272 /* FIXME: Perhaps the size should depend upon the architecture. */
273 ret = debug_make_int_type (dhandle, 4, FALSE);
278 ret = debug_make_int_type (dhandle, 4, FALSE);
283 ret = debug_make_float_type (dhandle, 4);
288 ret = debug_make_float_type (dhandle, 8);
293 ret = debug_make_float_type (dhandle, 12);
294 name = "long double";
298 ret = debug_make_int_type (dhandle, 1, TRUE);
299 name = "unsigned char";
303 ret = debug_make_int_type (dhandle, 2, TRUE);
304 name = "unsigned short";
308 ret = debug_make_int_type (dhandle, 4, TRUE);
309 name = "unsigned int";
313 ret = debug_make_int_type (dhandle, 4, TRUE);
314 name = "unsigned long";
319 ret = debug_make_struct_type (dhandle, TRUE, 0,
320 (debug_field *) NULL);
322 ret = parse_coff_struct_type (abfd, symbols, types, ntype, pauxent,
325 slot = coff_get_slot (types, coff_symno);
333 ret = debug_make_struct_type (dhandle, FALSE, 0, (debug_field *) NULL);
335 ret = parse_coff_struct_type (abfd, symbols, types, ntype, pauxent,
338 slot = coff_get_slot (types, coff_symno);
346 ret = debug_make_enum_type (dhandle, (const char **) NULL,
347 (bfd_signed_vma *) NULL);
349 ret = parse_coff_enum_type (abfd, symbols, types, pauxent, dhandle);
351 slot = coff_get_slot (types, coff_symno);
359 ret = debug_name_type (dhandle, name, ret);
364 types->basic[ntype] = ret;
369 /* Parse a struct type. */
372 parse_coff_struct_type (bfd *abfd, struct coff_symbols *symbols,
373 struct coff_types *types, int ntype,
374 union internal_auxent *pauxent, void *dhandle)
382 symend = pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l;
385 fields = (debug_field *) xmalloc (alloc * sizeof *fields);
390 && symbols->coff_symno < symend
391 && symbols->symno < symbols->symcount)
394 long this_coff_symno;
395 struct internal_syment syment;
396 union internal_auxent auxent;
397 union internal_auxent *psubaux;
398 bfd_vma bitpos = 0, bitsize = 0;
400 sym = symbols->syms[symbols->symno];
402 if (! bfd_coff_get_syment (abfd, sym, &syment))
404 non_fatal (_("bfd_coff_get_syment failed: %s"),
405 bfd_errmsg (bfd_get_error ()));
406 return DEBUG_TYPE_NULL;
409 this_coff_symno = symbols->coff_symno;
412 symbols->coff_symno += 1 + syment.n_numaux;
414 if (syment.n_numaux == 0)
418 if (! bfd_coff_get_auxent (abfd, sym, 0, &auxent))
420 non_fatal (_("bfd_coff_get_auxent failed: %s"),
421 bfd_errmsg (bfd_get_error ()));
422 return DEBUG_TYPE_NULL;
427 switch (syment.n_sclass)
431 bitpos = 8 * bfd_asymbol_value (sym);
436 bitpos = bfd_asymbol_value (sym);
437 bitsize = auxent.x_sym.x_misc.x_lnsz.x_size;
450 ftype = parse_coff_type (abfd, symbols, types, this_coff_symno,
451 syment.n_type, psubaux, TRUE, dhandle);
452 f = debug_make_field (dhandle, bfd_asymbol_name (sym), ftype,
453 bitpos, bitsize, DEBUG_VISIBILITY_PUBLIC);
454 if (f == DEBUG_FIELD_NULL)
455 return DEBUG_TYPE_NULL;
457 if (count + 1 >= alloc)
460 fields = ((debug_field *)
461 xrealloc (fields, alloc * sizeof *fields));
469 fields[count] = DEBUG_FIELD_NULL;
471 return debug_make_struct_type (dhandle, ntype == T_STRUCT,
472 pauxent->x_sym.x_misc.x_lnsz.x_size,
476 /* Parse an enum type. */
479 parse_coff_enum_type (bfd *abfd, struct coff_symbols *symbols,
480 struct coff_types *types ATTRIBUTE_UNUSED,
481 union internal_auxent *pauxent, void *dhandle)
486 bfd_signed_vma *vals;
490 symend = pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l;
493 names = (const char **) xmalloc (alloc * sizeof *names);
494 vals = (bfd_signed_vma *) xmalloc (alloc * sizeof *vals);
499 && symbols->coff_symno < symend
500 && symbols->symno < symbols->symcount)
503 struct internal_syment syment;
505 sym = symbols->syms[symbols->symno];
507 if (! bfd_coff_get_syment (abfd, sym, &syment))
509 non_fatal (_("bfd_coff_get_syment failed: %s"),
510 bfd_errmsg (bfd_get_error ()));
511 return DEBUG_TYPE_NULL;
515 symbols->coff_symno += 1 + syment.n_numaux;
517 switch (syment.n_sclass)
520 if (count + 1 >= alloc)
523 names = ((const char **)
524 xrealloc (names, alloc * sizeof *names));
525 vals = ((bfd_signed_vma *)
526 xrealloc (vals, alloc * sizeof *vals));
529 names[count] = bfd_asymbol_name (sym);
530 vals[count] = bfd_asymbol_value (sym);
542 return debug_make_enum_type (dhandle, names, vals);
545 /* Handle a single COFF symbol. */
548 parse_coff_symbol (bfd *abfd ATTRIBUTE_UNUSED, struct coff_types *types,
549 asymbol *sym, long coff_symno,
550 struct internal_syment *psyment, void *dhandle,
551 debug_type type, bfd_boolean within_function)
553 switch (psyment->n_sclass)
559 if (! debug_record_variable (dhandle, bfd_asymbol_name (sym), type,
560 DEBUG_LOCAL, bfd_asymbol_value (sym)))
566 if (! debug_record_variable (dhandle, bfd_asymbol_name (sym), type,
567 DEBUG_GLOBAL, bfd_asymbol_value (sym)))
572 if (! debug_record_variable (dhandle, bfd_asymbol_name (sym), type,
576 bfd_asymbol_value (sym)))
581 /* FIXME: We may need to convert the register number. */
582 if (! debug_record_variable (dhandle, bfd_asymbol_name (sym), type,
583 DEBUG_REGISTER, bfd_asymbol_value (sym)))
591 if (! debug_record_parameter (dhandle, bfd_asymbol_name (sym), type,
592 DEBUG_PARM_STACK, bfd_asymbol_value (sym)))
597 /* FIXME: We may need to convert the register number. */
598 if (! debug_record_parameter (dhandle, bfd_asymbol_name (sym), type,
599 DEBUG_PARM_REG, bfd_asymbol_value (sym)))
604 type = debug_name_type (dhandle, bfd_asymbol_name (sym), type);
605 if (type == DEBUG_TYPE_NULL)
615 type = debug_tag_type (dhandle, bfd_asymbol_name (sym), type);
616 if (type == DEBUG_TYPE_NULL)
619 /* Store the named type into the slot, so that references get
621 slot = coff_get_slot (types, coff_symno);
633 /* Determine if a symbol has external visibility. */
636 external_coff_symbol_p (int sym_class)
649 /* This is the main routine. It looks through all the symbols and
653 parse_coff (bfd *abfd, asymbol **syms, long symcount, void *dhandle)
655 struct coff_symbols symbols;
656 struct coff_types types;
664 bfd_boolean within_function;
665 long this_coff_symno;
668 symbols.symcount = symcount;
670 symbols.coff_symno = 0;
673 for (i = 0; i <= T_MAX; i++)
674 types.basic[i] = DEBUG_TYPE_NULL;
682 within_function = FALSE;
684 while (symbols.symno < symcount)
688 struct internal_syment syment;
689 union internal_auxent auxent;
690 union internal_auxent *paux;
693 sym = syms[symbols.symno];
695 if (! bfd_coff_get_syment (abfd, sym, &syment))
697 non_fatal (_("bfd_coff_get_syment failed: %s"),
698 bfd_errmsg (bfd_get_error ()));
702 name = bfd_asymbol_name (sym);
704 this_coff_symno = symbols.coff_symno;
707 symbols.coff_symno += 1 + syment.n_numaux;
709 /* We only worry about the first auxent, because that is the
710 only one which is relevant for debugging information. */
711 if (syment.n_numaux == 0)
715 if (! bfd_coff_get_auxent (abfd, sym, 0, &auxent))
717 non_fatal (_("bfd_coff_get_auxent failed: %s"),
718 bfd_errmsg (bfd_get_error ()));
724 if (this_coff_symno == next_c_file && syment.n_sclass != C_FILE)
726 /* The last C_FILE symbol points to the first external
728 if (! debug_set_filename (dhandle, "*globals*"))
732 switch (syment.n_sclass)
741 /* Just ignore these classes. */
745 next_c_file = syment.n_value;
746 if (! debug_set_filename (dhandle, name))
751 /* Ignore static symbols with a type of T_NULL. These
752 represent section entries. */
753 if (syment.n_type == T_NULL)
758 if (ISFCN (syment.n_type))
761 fnclass = syment.n_sclass;
762 fntype = syment.n_type;
763 if (syment.n_numaux > 0)
764 fnend = bfd_asymbol_value (sym) + auxent.x_sym.x_misc.x_fsize;
767 linenos = BFD_SEND (abfd, _get_lineno, (abfd, sym));
770 type = parse_coff_type (abfd, &symbols, &types, this_coff_symno,
771 syment.n_type, paux, TRUE, dhandle);
772 if (type == DEBUG_TYPE_NULL)
774 if (! parse_coff_symbol (abfd, &types, sym, this_coff_symno, &syment,
775 dhandle, type, within_function))
780 if (strcmp (name, ".bf") == 0)
784 non_fatal (_("%ld: .bf without preceding function"),
789 type = parse_coff_type (abfd, &symbols, &types, this_coff_symno,
790 DECREF (fntype), paux, FALSE, dhandle);
791 if (type == DEBUG_TYPE_NULL)
794 if (! debug_record_function (dhandle, fnname, type,
795 external_coff_symbol_p (fnclass),
796 bfd_asymbol_value (sym)))
804 if (syment.n_numaux == 0)
807 base = auxent.x_sym.x_misc.x_lnsz.x_lnno - 1;
809 addr = bfd_get_section_vma (abfd, bfd_get_section (sym));
813 while (linenos->line_number != 0)
815 if (! debug_record_line (dhandle,
816 linenos->line_number + base,
817 linenos->u.offset + addr))
828 within_function = TRUE;
830 else if (strcmp (name, ".ef") == 0)
832 if (! within_function)
834 non_fatal (_("%ld: unexpected .ef\n"), this_coff_symno);
838 if (bfd_asymbol_value (sym) > fnend)
839 fnend = bfd_asymbol_value (sym);
840 if (! debug_end_function (dhandle, fnend))
844 within_function = FALSE;
849 if (strcmp (name, ".bb") == 0)
851 if (! debug_start_block (dhandle, bfd_asymbol_value (sym)))
854 else if (strcmp (name, ".eb") == 0)
856 if (! debug_end_block (dhandle, bfd_asymbol_value (sym)))
862 type = parse_coff_type (abfd, &symbols, &types, this_coff_symno,
863 syment.n_type, paux, TRUE, dhandle);
864 if (type == DEBUG_TYPE_NULL)
866 if (! parse_coff_symbol (abfd, &types, sym, this_coff_symno, &syment,
867 dhandle, type, within_function))