1 /* stabs.c -- Parse COFF debugging information
2 Copyright 1996, 1999, 2000, 2002, 2003, 2007
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@cygnus.com>.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
23 /* This file contains code which parses COFF debugging information. */
27 #include "coff/internal.h"
28 #include "libiberty.h"
33 /* FIXME: We should not need this BFD internal file. We need it for
34 the N_BTMASK, etc., values. */
37 /* These macros extract the right mask and shifts for this BFD. They
38 assume that there is a local variable named ABFD. This is so that
39 macros like ISFCN and DECREF, from coff/internal.h, will work
40 without modification. */
41 #define N_BTMASK (coff_data (abfd)->local_n_btmask)
42 #define N_BTSHFT (coff_data (abfd)->local_n_btshft)
43 #define N_TMASK (coff_data (abfd)->local_n_tmask)
44 #define N_TSHIFT (coff_data (abfd)->local_n_tshift)
46 /* This structure is used to hold the symbols, as well as the current
47 location within the symbols. */
53 /* The number of symbols. */
55 /* The index of the current symbol. */
57 /* The index of the current symbol in the COFF symbol table (where
58 each auxent counts as a symbol). */
62 /* The largest basic type we are prepared to handle. */
64 #define T_MAX (T_LNGDBL)
66 /* This structure is used to hold slots. */
70 /* Next set of slots. */
71 struct coff_slots *next;
73 #define COFF_SLOTS (16)
74 debug_type slots[COFF_SLOTS];
77 /* This structure is used to map symbol indices to types. */
82 struct coff_slots *slots;
84 debug_type basic[T_MAX + 1];
87 static debug_type *coff_get_slot (struct coff_types *, int);
88 static debug_type parse_coff_type
89 (bfd *, struct coff_symbols *, struct coff_types *, long, int,
90 union internal_auxent *, bfd_boolean, void *);
91 static debug_type parse_coff_base_type
92 (bfd *, struct coff_symbols *, struct coff_types *, long, int,
93 union internal_auxent *, void *);
94 static debug_type parse_coff_struct_type
95 (bfd *, struct coff_symbols *, struct coff_types *, int,
96 union internal_auxent *, void *);
97 static debug_type parse_coff_enum_type
98 (bfd *, struct coff_symbols *, struct coff_types *,
99 union internal_auxent *, void *);
100 static bfd_boolean parse_coff_symbol
101 (bfd *, struct coff_types *, asymbol *, long, struct internal_syment *,
102 void *, debug_type, bfd_boolean);
103 static bfd_boolean external_coff_symbol_p (int sym_class);
105 /* Return the slot for a type. */
108 coff_get_slot (struct coff_types *types, int indx)
110 struct coff_slots **pps;
114 while (indx >= COFF_SLOTS)
118 *pps = (struct coff_slots *) xmalloc (sizeof **pps);
119 memset (*pps, 0, sizeof **pps);
127 *pps = (struct coff_slots *) xmalloc (sizeof **pps);
128 memset (*pps, 0, sizeof **pps);
131 return (*pps)->slots + indx;
134 /* Parse a COFF type code in NTYPE. */
137 parse_coff_type (bfd *abfd, struct coff_symbols *symbols,
138 struct coff_types *types, long coff_symno, int ntype,
139 union internal_auxent *pauxent, bfd_boolean useaux,
144 if ((ntype & ~N_BTMASK) != 0)
148 newtype = DECREF (ntype);
152 type = parse_coff_type (abfd, symbols, types, coff_symno, newtype,
153 pauxent, useaux, dhandle);
154 type = debug_make_pointer_type (dhandle, type);
156 else if (ISFCN (ntype))
158 type = parse_coff_type (abfd, symbols, types, coff_symno, newtype,
159 pauxent, useaux, dhandle);
160 type = debug_make_function_type (dhandle, type, (debug_type *) NULL,
163 else if (ISARY (ntype))
174 /* FIXME: If pauxent->x_sym.x_tagndx.l == 0, gdb sets
175 the c_naux field of the syment to 0. */
177 /* Move the dimensions down, so that the next array
178 picks up the next one. */
179 dim = pauxent->x_sym.x_fcnary.x_ary.x_dimen;
181 for (i = 0; *dim != 0 && i < DIMNUM - 1; i++, dim++)
186 type = parse_coff_type (abfd, symbols, types, coff_symno, newtype,
187 pauxent, FALSE, dhandle);
188 type = debug_make_array_type (dhandle, type,
189 parse_coff_base_type (abfd, symbols,
198 non_fatal (_("parse_coff_type: Bad type code 0x%x"), ntype);
199 return DEBUG_TYPE_NULL;
205 if (pauxent != NULL && pauxent->x_sym.x_tagndx.l > 0)
209 /* This is a reference to an existing type. FIXME: gdb checks
210 that the class is not C_STRTAG, nor C_UNTAG, nor C_ENTAG. */
211 slot = coff_get_slot (types, pauxent->x_sym.x_tagndx.l);
212 if (*slot != DEBUG_TYPE_NULL)
215 return debug_make_indirect_type (dhandle, slot, (const char *) NULL);
218 /* If the aux entry has already been used for something, useaux will
219 have been set to false, indicating that parse_coff_base_type
220 should not use it. We need to do it this way, rather than simply
221 passing pauxent as NULL, because we need to be able handle
222 multiple array dimensions while still discarding pauxent after
223 having handled all of them. */
227 return parse_coff_base_type (abfd, symbols, types, coff_symno, ntype,
231 /* Parse a basic COFF type in NTYPE. */
234 parse_coff_base_type (bfd *abfd, struct coff_symbols *symbols,
235 struct coff_types *types, long coff_symno, int ntype,
236 union internal_auxent *pauxent, void *dhandle)
239 bfd_boolean set_basic;
245 && types->basic[ntype] != DEBUG_TYPE_NULL)
246 return types->basic[ntype];
254 ret = debug_make_void_type (dhandle);
259 ret = debug_make_void_type (dhandle);
264 ret = debug_make_int_type (dhandle, 1, FALSE);
269 ret = debug_make_int_type (dhandle, 2, FALSE);
274 /* FIXME: Perhaps the size should depend upon the architecture. */
275 ret = debug_make_int_type (dhandle, 4, FALSE);
280 ret = debug_make_int_type (dhandle, 4, FALSE);
285 ret = debug_make_float_type (dhandle, 4);
290 ret = debug_make_float_type (dhandle, 8);
295 ret = debug_make_float_type (dhandle, 12);
296 name = "long double";
300 ret = debug_make_int_type (dhandle, 1, TRUE);
301 name = "unsigned char";
305 ret = debug_make_int_type (dhandle, 2, TRUE);
306 name = "unsigned short";
310 ret = debug_make_int_type (dhandle, 4, TRUE);
311 name = "unsigned int";
315 ret = debug_make_int_type (dhandle, 4, TRUE);
316 name = "unsigned long";
321 ret = debug_make_struct_type (dhandle, TRUE, 0,
322 (debug_field *) NULL);
324 ret = parse_coff_struct_type (abfd, symbols, types, ntype, pauxent,
327 slot = coff_get_slot (types, coff_symno);
335 ret = debug_make_struct_type (dhandle, FALSE, 0, (debug_field *) NULL);
337 ret = parse_coff_struct_type (abfd, symbols, types, ntype, pauxent,
340 slot = coff_get_slot (types, coff_symno);
348 ret = debug_make_enum_type (dhandle, (const char **) NULL,
349 (bfd_signed_vma *) NULL);
351 ret = parse_coff_enum_type (abfd, symbols, types, pauxent, dhandle);
353 slot = coff_get_slot (types, coff_symno);
361 ret = debug_name_type (dhandle, name, ret);
366 types->basic[ntype] = ret;
371 /* Parse a struct type. */
374 parse_coff_struct_type (bfd *abfd, struct coff_symbols *symbols,
375 struct coff_types *types, int ntype,
376 union internal_auxent *pauxent, void *dhandle)
384 symend = pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l;
387 fields = (debug_field *) xmalloc (alloc * sizeof *fields);
392 && symbols->coff_symno < symend
393 && symbols->symno < symbols->symcount)
396 long this_coff_symno;
397 struct internal_syment syment;
398 union internal_auxent auxent;
399 union internal_auxent *psubaux;
400 bfd_vma bitpos = 0, bitsize = 0;
402 sym = symbols->syms[symbols->symno];
404 if (! bfd_coff_get_syment (abfd, sym, &syment))
406 non_fatal (_("bfd_coff_get_syment failed: %s"),
407 bfd_errmsg (bfd_get_error ()));
408 return DEBUG_TYPE_NULL;
411 this_coff_symno = symbols->coff_symno;
414 symbols->coff_symno += 1 + syment.n_numaux;
416 if (syment.n_numaux == 0)
420 if (! bfd_coff_get_auxent (abfd, sym, 0, &auxent))
422 non_fatal (_("bfd_coff_get_auxent failed: %s"),
423 bfd_errmsg (bfd_get_error ()));
424 return DEBUG_TYPE_NULL;
429 switch (syment.n_sclass)
433 bitpos = 8 * bfd_asymbol_value (sym);
438 bitpos = bfd_asymbol_value (sym);
439 bitsize = auxent.x_sym.x_misc.x_lnsz.x_size;
452 ftype = parse_coff_type (abfd, symbols, types, this_coff_symno,
453 syment.n_type, psubaux, TRUE, dhandle);
454 f = debug_make_field (dhandle, bfd_asymbol_name (sym), ftype,
455 bitpos, bitsize, DEBUG_VISIBILITY_PUBLIC);
456 if (f == DEBUG_FIELD_NULL)
457 return DEBUG_TYPE_NULL;
459 if (count + 1 >= alloc)
462 fields = ((debug_field *)
463 xrealloc (fields, alloc * sizeof *fields));
471 fields[count] = DEBUG_FIELD_NULL;
473 return debug_make_struct_type (dhandle, ntype == T_STRUCT,
474 pauxent->x_sym.x_misc.x_lnsz.x_size,
478 /* Parse an enum type. */
481 parse_coff_enum_type (bfd *abfd, struct coff_symbols *symbols,
482 struct coff_types *types ATTRIBUTE_UNUSED,
483 union internal_auxent *pauxent, void *dhandle)
488 bfd_signed_vma *vals;
492 symend = pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l;
495 names = (const char **) xmalloc (alloc * sizeof *names);
496 vals = (bfd_signed_vma *) xmalloc (alloc * sizeof *vals);
501 && symbols->coff_symno < symend
502 && symbols->symno < symbols->symcount)
505 struct internal_syment syment;
507 sym = symbols->syms[symbols->symno];
509 if (! bfd_coff_get_syment (abfd, sym, &syment))
511 non_fatal (_("bfd_coff_get_syment failed: %s"),
512 bfd_errmsg (bfd_get_error ()));
513 return DEBUG_TYPE_NULL;
517 symbols->coff_symno += 1 + syment.n_numaux;
519 switch (syment.n_sclass)
522 if (count + 1 >= alloc)
525 names = ((const char **)
526 xrealloc (names, alloc * sizeof *names));
527 vals = ((bfd_signed_vma *)
528 xrealloc (vals, alloc * sizeof *vals));
531 names[count] = bfd_asymbol_name (sym);
532 vals[count] = bfd_asymbol_value (sym);
544 return debug_make_enum_type (dhandle, names, vals);
547 /* Handle a single COFF symbol. */
550 parse_coff_symbol (bfd *abfd ATTRIBUTE_UNUSED, struct coff_types *types,
551 asymbol *sym, long coff_symno,
552 struct internal_syment *psyment, void *dhandle,
553 debug_type type, bfd_boolean within_function)
555 switch (psyment->n_sclass)
561 if (! debug_record_variable (dhandle, bfd_asymbol_name (sym), type,
562 DEBUG_LOCAL, bfd_asymbol_value (sym)))
568 if (! debug_record_variable (dhandle, bfd_asymbol_name (sym), type,
569 DEBUG_GLOBAL, bfd_asymbol_value (sym)))
574 if (! debug_record_variable (dhandle, bfd_asymbol_name (sym), type,
578 bfd_asymbol_value (sym)))
583 /* FIXME: We may need to convert the register number. */
584 if (! debug_record_variable (dhandle, bfd_asymbol_name (sym), type,
585 DEBUG_REGISTER, bfd_asymbol_value (sym)))
593 if (! debug_record_parameter (dhandle, bfd_asymbol_name (sym), type,
594 DEBUG_PARM_STACK, bfd_asymbol_value (sym)))
599 /* FIXME: We may need to convert the register number. */
600 if (! debug_record_parameter (dhandle, bfd_asymbol_name (sym), type,
601 DEBUG_PARM_REG, bfd_asymbol_value (sym)))
606 type = debug_name_type (dhandle, bfd_asymbol_name (sym), type);
607 if (type == DEBUG_TYPE_NULL)
617 type = debug_tag_type (dhandle, bfd_asymbol_name (sym), type);
618 if (type == DEBUG_TYPE_NULL)
621 /* Store the named type into the slot, so that references get
623 slot = coff_get_slot (types, coff_symno);
635 /* Determine if a symbol has external visibility. */
638 external_coff_symbol_p (int sym_class)
651 /* This is the main routine. It looks through all the symbols and
655 parse_coff (bfd *abfd, asymbol **syms, long symcount, void *dhandle)
657 struct coff_symbols symbols;
658 struct coff_types types;
666 bfd_boolean within_function;
667 long this_coff_symno;
670 symbols.symcount = symcount;
672 symbols.coff_symno = 0;
675 for (i = 0; i <= T_MAX; i++)
676 types.basic[i] = DEBUG_TYPE_NULL;
684 within_function = FALSE;
686 while (symbols.symno < symcount)
690 struct internal_syment syment;
691 union internal_auxent auxent;
692 union internal_auxent *paux;
695 sym = syms[symbols.symno];
697 if (! bfd_coff_get_syment (abfd, sym, &syment))
699 non_fatal (_("bfd_coff_get_syment failed: %s"),
700 bfd_errmsg (bfd_get_error ()));
704 name = bfd_asymbol_name (sym);
706 this_coff_symno = symbols.coff_symno;
709 symbols.coff_symno += 1 + syment.n_numaux;
711 /* We only worry about the first auxent, because that is the
712 only one which is relevant for debugging information. */
713 if (syment.n_numaux == 0)
717 if (! bfd_coff_get_auxent (abfd, sym, 0, &auxent))
719 non_fatal (_("bfd_coff_get_auxent failed: %s"),
720 bfd_errmsg (bfd_get_error ()));
726 if (this_coff_symno == next_c_file && syment.n_sclass != C_FILE)
728 /* The last C_FILE symbol points to the first external
730 if (! debug_set_filename (dhandle, "*globals*"))
734 switch (syment.n_sclass)
743 /* Just ignore these classes. */
747 next_c_file = syment.n_value;
748 if (! debug_set_filename (dhandle, name))
753 /* Ignore static symbols with a type of T_NULL. These
754 represent section entries. */
755 if (syment.n_type == T_NULL)
760 if (ISFCN (syment.n_type))
763 fnclass = syment.n_sclass;
764 fntype = syment.n_type;
765 if (syment.n_numaux > 0)
766 fnend = bfd_asymbol_value (sym) + auxent.x_sym.x_misc.x_fsize;
769 linenos = BFD_SEND (abfd, _get_lineno, (abfd, sym));
772 type = parse_coff_type (abfd, &symbols, &types, this_coff_symno,
773 syment.n_type, paux, TRUE, dhandle);
774 if (type == DEBUG_TYPE_NULL)
776 if (! parse_coff_symbol (abfd, &types, sym, this_coff_symno, &syment,
777 dhandle, type, within_function))
782 if (strcmp (name, ".bf") == 0)
786 non_fatal (_("%ld: .bf without preceding function"),
791 type = parse_coff_type (abfd, &symbols, &types, this_coff_symno,
792 DECREF (fntype), paux, FALSE, dhandle);
793 if (type == DEBUG_TYPE_NULL)
796 if (! debug_record_function (dhandle, fnname, type,
797 external_coff_symbol_p (fnclass),
798 bfd_asymbol_value (sym)))
806 if (syment.n_numaux == 0)
809 base = auxent.x_sym.x_misc.x_lnsz.x_lnno - 1;
811 addr = bfd_get_section_vma (abfd, bfd_get_section (sym));
815 while (linenos->line_number != 0)
817 if (! debug_record_line (dhandle,
818 linenos->line_number + base,
819 linenos->u.offset + addr))
830 within_function = TRUE;
832 else if (strcmp (name, ".ef") == 0)
834 if (! within_function)
836 non_fatal (_("%ld: unexpected .ef\n"), this_coff_symno);
840 if (bfd_asymbol_value (sym) > fnend)
841 fnend = bfd_asymbol_value (sym);
842 if (! debug_end_function (dhandle, fnend))
846 within_function = FALSE;
851 if (strcmp (name, ".bb") == 0)
853 if (! debug_start_block (dhandle, bfd_asymbol_value (sym)))
856 else if (strcmp (name, ".eb") == 0)
858 if (! debug_end_block (dhandle, bfd_asymbol_value (sym)))
864 type = parse_coff_type (abfd, &symbols, &types, this_coff_symno,
865 syment.n_type, paux, TRUE, dhandle);
866 if (type == DEBUG_TYPE_NULL)
868 if (! parse_coff_symbol (abfd, &types, sym, this_coff_symno, &syment,
869 dhandle, type, within_function))