1 /* stabs.c -- Parse COFF debugging information
2 Copyright (C) 1996-2014 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 3 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., 51 Franklin Street - Fifth Floor, Boston, MA
22 /* This file contains code which parses COFF debugging information. */
26 #include "coff/internal.h"
27 #include "libiberty.h"
32 /* FIXME: We should not need this BFD internal file. We need it for
33 the N_BTMASK, etc., values. */
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)
45 /* This structure is used to hold the symbols, as well as the current
46 location within the symbols. */
52 /* The number of symbols. */
54 /* The index of the current symbol. */
56 /* The index of the current symbol in the COFF symbol table (where
57 each auxent counts as a symbol). */
61 /* The largest basic type we are prepared to handle. */
63 #define T_MAX (T_LNGDBL)
65 /* This structure is used to hold slots. */
69 /* Next set of slots. */
70 struct coff_slots *next;
72 #define COFF_SLOTS (16)
73 debug_type slots[COFF_SLOTS];
76 /* This structure is used to map symbol indices to types. */
81 struct coff_slots *slots;
83 debug_type basic[T_MAX + 1];
86 static debug_type *coff_get_slot (struct coff_types *, int);
87 static debug_type parse_coff_type
88 (bfd *, struct coff_symbols *, struct coff_types *, long, int,
89 union internal_auxent *, bfd_boolean, void *);
90 static debug_type parse_coff_base_type
91 (bfd *, struct coff_symbols *, struct coff_types *, long, int,
92 union internal_auxent *, void *);
93 static debug_type parse_coff_struct_type
94 (bfd *, struct coff_symbols *, struct coff_types *, int,
95 union internal_auxent *, void *);
96 static debug_type parse_coff_enum_type
97 (bfd *, struct coff_symbols *, struct coff_types *,
98 union internal_auxent *, void *);
99 static bfd_boolean parse_coff_symbol
100 (bfd *, struct coff_types *, asymbol *, long, struct internal_syment *,
101 void *, debug_type, bfd_boolean);
102 static bfd_boolean external_coff_symbol_p (int sym_class);
104 /* Return the slot for a type. */
107 coff_get_slot (struct coff_types *types, int indx)
109 struct coff_slots **pps;
113 while (indx >= COFF_SLOTS)
117 *pps = (struct coff_slots *) xmalloc (sizeof **pps);
118 memset (*pps, 0, sizeof **pps);
126 *pps = (struct coff_slots *) xmalloc (sizeof **pps);
127 memset (*pps, 0, sizeof **pps);
130 return (*pps)->slots + indx;
133 /* Parse a COFF type code in NTYPE. */
136 parse_coff_type (bfd *abfd, struct coff_symbols *symbols,
137 struct coff_types *types, long coff_symno, int ntype,
138 union internal_auxent *pauxent, bfd_boolean useaux,
143 if ((ntype & ~N_BTMASK) != 0)
147 newtype = DECREF (ntype);
151 type = parse_coff_type (abfd, symbols, types, coff_symno, newtype,
152 pauxent, useaux, dhandle);
153 type = debug_make_pointer_type (dhandle, type);
155 else if (ISFCN (ntype))
157 type = parse_coff_type (abfd, symbols, types, coff_symno, newtype,
158 pauxent, useaux, dhandle);
159 type = debug_make_function_type (dhandle, type, (debug_type *) NULL,
162 else if (ISARY (ntype))
173 /* FIXME: If pauxent->x_sym.x_tagndx.l == 0, gdb sets
174 the c_naux field of the syment to 0. */
176 /* Move the dimensions down, so that the next array
177 picks up the next one. */
178 dim = pauxent->x_sym.x_fcnary.x_ary.x_dimen;
180 for (i = 0; *dim != 0 && i < DIMNUM - 1; i++, dim++)
185 type = parse_coff_type (abfd, symbols, types, coff_symno, newtype,
186 pauxent, FALSE, dhandle);
187 type = debug_make_array_type (dhandle, type,
188 parse_coff_base_type (abfd, symbols,
197 non_fatal (_("parse_coff_type: Bad type code 0x%x"), ntype);
198 return DEBUG_TYPE_NULL;
204 if (pauxent != NULL && pauxent->x_sym.x_tagndx.l > 0)
208 /* This is a reference to an existing type. FIXME: gdb checks
209 that the class is not C_STRTAG, nor C_UNTAG, nor C_ENTAG. */
210 slot = coff_get_slot (types, pauxent->x_sym.x_tagndx.l);
211 if (*slot != DEBUG_TYPE_NULL)
214 return debug_make_indirect_type (dhandle, slot, (const char *) NULL);
217 /* If the aux entry has already been used for something, useaux will
218 have been set to false, indicating that parse_coff_base_type
219 should not use it. We need to do it this way, rather than simply
220 passing pauxent as NULL, because we need to be able handle
221 multiple array dimensions while still discarding pauxent after
222 having handled all of them. */
226 return parse_coff_base_type (abfd, symbols, types, coff_symno, ntype,
230 /* Parse a basic COFF type in NTYPE. */
233 parse_coff_base_type (bfd *abfd, struct coff_symbols *symbols,
234 struct coff_types *types, long coff_symno, int ntype,
235 union internal_auxent *pauxent, void *dhandle)
238 bfd_boolean set_basic;
244 && types->basic[ntype] != DEBUG_TYPE_NULL)
245 return types->basic[ntype];
253 ret = debug_make_void_type (dhandle);
258 ret = debug_make_void_type (dhandle);
263 ret = debug_make_int_type (dhandle, 1, FALSE);
268 ret = debug_make_int_type (dhandle, 2, FALSE);
273 /* FIXME: Perhaps the size should depend upon the architecture. */
274 ret = debug_make_int_type (dhandle, 4, FALSE);
279 ret = debug_make_int_type (dhandle, 4, FALSE);
284 ret = debug_make_float_type (dhandle, 4);
289 ret = debug_make_float_type (dhandle, 8);
294 ret = debug_make_float_type (dhandle, 12);
295 name = "long double";
299 ret = debug_make_int_type (dhandle, 1, TRUE);
300 name = "unsigned char";
304 ret = debug_make_int_type (dhandle, 2, TRUE);
305 name = "unsigned short";
309 ret = debug_make_int_type (dhandle, 4, TRUE);
310 name = "unsigned int";
314 ret = debug_make_int_type (dhandle, 4, TRUE);
315 name = "unsigned long";
320 ret = debug_make_struct_type (dhandle, TRUE, 0,
321 (debug_field *) NULL);
323 ret = parse_coff_struct_type (abfd, symbols, types, ntype, pauxent,
326 slot = coff_get_slot (types, coff_symno);
334 ret = debug_make_struct_type (dhandle, FALSE, 0, (debug_field *) NULL);
336 ret = parse_coff_struct_type (abfd, symbols, types, ntype, pauxent,
339 slot = coff_get_slot (types, coff_symno);
347 ret = debug_make_enum_type (dhandle, (const char **) NULL,
348 (bfd_signed_vma *) NULL);
350 ret = parse_coff_enum_type (abfd, symbols, types, pauxent, dhandle);
352 slot = coff_get_slot (types, coff_symno);
360 ret = debug_name_type (dhandle, name, ret);
365 types->basic[ntype] = ret;
370 /* Parse a struct type. */
373 parse_coff_struct_type (bfd *abfd, struct coff_symbols *symbols,
374 struct coff_types *types, int ntype,
375 union internal_auxent *pauxent, void *dhandle)
383 symend = pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l;
386 fields = (debug_field *) xmalloc (alloc * sizeof *fields);
391 && symbols->coff_symno < symend
392 && symbols->symno < symbols->symcount)
395 long this_coff_symno;
396 struct internal_syment syment;
397 union internal_auxent auxent;
398 union internal_auxent *psubaux;
399 bfd_vma bitpos = 0, bitsize = 0;
401 sym = symbols->syms[symbols->symno];
403 if (! bfd_coff_get_syment (abfd, sym, &syment))
405 non_fatal (_("bfd_coff_get_syment failed: %s"),
406 bfd_errmsg (bfd_get_error ()));
407 return DEBUG_TYPE_NULL;
410 this_coff_symno = symbols->coff_symno;
413 symbols->coff_symno += 1 + syment.n_numaux;
415 if (syment.n_numaux == 0)
419 if (! bfd_coff_get_auxent (abfd, sym, 0, &auxent))
421 non_fatal (_("bfd_coff_get_auxent failed: %s"),
422 bfd_errmsg (bfd_get_error ()));
423 return DEBUG_TYPE_NULL;
428 switch (syment.n_sclass)
432 bitpos = 8 * bfd_asymbol_value (sym);
437 bitpos = bfd_asymbol_value (sym);
438 bitsize = auxent.x_sym.x_misc.x_lnsz.x_size;
451 ftype = parse_coff_type (abfd, symbols, types, this_coff_symno,
452 syment.n_type, psubaux, TRUE, dhandle);
453 f = debug_make_field (dhandle, bfd_asymbol_name (sym), ftype,
454 bitpos, bitsize, DEBUG_VISIBILITY_PUBLIC);
455 if (f == DEBUG_FIELD_NULL)
456 return DEBUG_TYPE_NULL;
458 if (count + 1 >= alloc)
461 fields = ((debug_field *)
462 xrealloc (fields, alloc * sizeof *fields));
470 fields[count] = DEBUG_FIELD_NULL;
472 return debug_make_struct_type (dhandle, ntype == T_STRUCT,
473 pauxent->x_sym.x_misc.x_lnsz.x_size,
477 /* Parse an enum type. */
480 parse_coff_enum_type (bfd *abfd, struct coff_symbols *symbols,
481 struct coff_types *types ATTRIBUTE_UNUSED,
482 union internal_auxent *pauxent, void *dhandle)
487 bfd_signed_vma *vals;
491 symend = pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l;
494 names = (const char **) xmalloc (alloc * sizeof *names);
495 vals = (bfd_signed_vma *) xmalloc (alloc * sizeof *vals);
500 && symbols->coff_symno < symend
501 && symbols->symno < symbols->symcount)
504 struct internal_syment syment;
506 sym = symbols->syms[symbols->symno];
508 if (! bfd_coff_get_syment (abfd, sym, &syment))
510 non_fatal (_("bfd_coff_get_syment failed: %s"),
511 bfd_errmsg (bfd_get_error ()));
512 return DEBUG_TYPE_NULL;
516 symbols->coff_symno += 1 + syment.n_numaux;
518 switch (syment.n_sclass)
521 if (count + 1 >= alloc)
524 names = ((const char **)
525 xrealloc (names, alloc * sizeof *names));
526 vals = ((bfd_signed_vma *)
527 xrealloc (vals, alloc * sizeof *vals));
530 names[count] = bfd_asymbol_name (sym);
531 vals[count] = bfd_asymbol_value (sym);
543 return debug_make_enum_type (dhandle, names, vals);
546 /* Handle a single COFF symbol. */
549 parse_coff_symbol (bfd *abfd ATTRIBUTE_UNUSED, struct coff_types *types,
550 asymbol *sym, long coff_symno,
551 struct internal_syment *psyment, void *dhandle,
552 debug_type type, bfd_boolean within_function)
554 switch (psyment->n_sclass)
560 if (! debug_record_variable (dhandle, bfd_asymbol_name (sym), type,
561 DEBUG_LOCAL, bfd_asymbol_value (sym)))
567 if (! debug_record_variable (dhandle, bfd_asymbol_name (sym), type,
568 DEBUG_GLOBAL, bfd_asymbol_value (sym)))
573 if (! debug_record_variable (dhandle, bfd_asymbol_name (sym), type,
577 bfd_asymbol_value (sym)))
582 /* FIXME: We may need to convert the register number. */
583 if (! debug_record_variable (dhandle, bfd_asymbol_name (sym), type,
584 DEBUG_REGISTER, bfd_asymbol_value (sym)))
592 if (! debug_record_parameter (dhandle, bfd_asymbol_name (sym), type,
593 DEBUG_PARM_STACK, bfd_asymbol_value (sym)))
598 /* FIXME: We may need to convert the register number. */
599 if (! debug_record_parameter (dhandle, bfd_asymbol_name (sym), type,
600 DEBUG_PARM_REG, bfd_asymbol_value (sym)))
605 type = debug_name_type (dhandle, bfd_asymbol_name (sym), type);
606 if (type == DEBUG_TYPE_NULL)
616 type = debug_tag_type (dhandle, bfd_asymbol_name (sym), type);
617 if (type == DEBUG_TYPE_NULL)
620 /* Store the named type into the slot, so that references get
622 slot = coff_get_slot (types, coff_symno);
634 /* Determine if a symbol has external visibility. */
637 external_coff_symbol_p (int sym_class)
650 /* This is the main routine. It looks through all the symbols and
654 parse_coff (bfd *abfd, asymbol **syms, long symcount, void *dhandle)
656 struct coff_symbols symbols;
657 struct coff_types types;
665 bfd_boolean within_function;
666 long this_coff_symno;
669 symbols.symcount = symcount;
671 symbols.coff_symno = 0;
674 for (i = 0; i <= T_MAX; i++)
675 types.basic[i] = DEBUG_TYPE_NULL;
683 within_function = FALSE;
685 while (symbols.symno < symcount)
689 struct internal_syment syment;
690 union internal_auxent auxent;
691 union internal_auxent *paux;
694 sym = syms[symbols.symno];
696 if (! bfd_coff_get_syment (abfd, sym, &syment))
698 non_fatal (_("bfd_coff_get_syment failed: %s"),
699 bfd_errmsg (bfd_get_error ()));
703 name = bfd_asymbol_name (sym);
705 this_coff_symno = symbols.coff_symno;
708 symbols.coff_symno += 1 + syment.n_numaux;
710 /* We only worry about the first auxent, because that is the
711 only one which is relevant for debugging information. */
712 if (syment.n_numaux == 0)
716 if (! bfd_coff_get_auxent (abfd, sym, 0, &auxent))
718 non_fatal (_("bfd_coff_get_auxent failed: %s"),
719 bfd_errmsg (bfd_get_error ()));
725 if (this_coff_symno == next_c_file && syment.n_sclass != C_FILE)
727 /* The last C_FILE symbol points to the first external
729 if (! debug_set_filename (dhandle, "*globals*"))
733 switch (syment.n_sclass)
742 /* Just ignore these classes. */
746 next_c_file = syment.n_value;
747 if (! debug_set_filename (dhandle, name))
752 /* Ignore static symbols with a type of T_NULL. These
753 represent section entries. */
754 if (syment.n_type == T_NULL)
759 if (ISFCN (syment.n_type))
762 fnclass = syment.n_sclass;
763 fntype = syment.n_type;
764 if (syment.n_numaux > 0)
765 fnend = bfd_asymbol_value (sym) + auxent.x_sym.x_misc.x_fsize;
768 linenos = BFD_SEND (abfd, _get_lineno, (abfd, sym));
771 type = parse_coff_type (abfd, &symbols, &types, this_coff_symno,
772 syment.n_type, paux, TRUE, dhandle);
773 if (type == DEBUG_TYPE_NULL)
775 if (! parse_coff_symbol (abfd, &types, sym, this_coff_symno, &syment,
776 dhandle, type, within_function))
781 if (strcmp (name, ".bf") == 0)
785 non_fatal (_("%ld: .bf without preceding function"),
790 type = parse_coff_type (abfd, &symbols, &types, this_coff_symno,
791 DECREF (fntype), paux, FALSE, dhandle);
792 if (type == DEBUG_TYPE_NULL)
795 if (! debug_record_function (dhandle, fnname, type,
796 external_coff_symbol_p (fnclass),
797 bfd_asymbol_value (sym)))
805 if (syment.n_numaux == 0)
808 base = auxent.x_sym.x_misc.x_lnsz.x_lnno - 1;
810 addr = bfd_get_section_vma (abfd, bfd_get_section (sym));
814 while (linenos->line_number != 0)
816 if (! debug_record_line (dhandle,
817 linenos->line_number + base,
818 linenos->u.offset + addr))
829 within_function = TRUE;
831 else if (strcmp (name, ".ef") == 0)
833 if (! within_function)
835 non_fatal (_("%ld: unexpected .ef\n"), this_coff_symno);
839 if (bfd_asymbol_value (sym) > fnend)
840 fnend = bfd_asymbol_value (sym);
841 if (! debug_end_function (dhandle, fnend))
845 within_function = FALSE;
850 if (strcmp (name, ".bb") == 0)
852 if (! debug_start_block (dhandle, bfd_asymbol_value (sym)))
855 else if (strcmp (name, ".eb") == 0)
857 if (! debug_end_block (dhandle, bfd_asymbol_value (sym)))
863 type = parse_coff_type (abfd, &symbols, &types, this_coff_symno,
864 syment.n_type, paux, TRUE, dhandle);
865 if (type == DEBUG_TYPE_NULL)
867 if (! parse_coff_symbol (abfd, &types, sym, this_coff_symno, &syment,
868 dhandle, type, within_function))