1 /* stabs.c -- Parse COFF debugging information
2 Copyright (C) 1996 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"
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
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
));
103 /* Return the slot for a type. */
106 coff_get_slot (types
, indx
)
107 struct coff_types
*types
;
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 (abfd
, symbols
, types
, coff_symno
, ntype
, pauxent
, useaux
,
140 struct coff_symbols
*symbols
;
141 struct coff_types
*types
;
144 union internal_auxent
*pauxent
;
150 if ((ntype
& ~N_BTMASK
) != 0)
154 newtype
= DECREF (ntype
);
158 type
= parse_coff_type (abfd
, symbols
, types
, coff_symno
, newtype
,
159 pauxent
, useaux
, dhandle
);
160 type
= debug_make_pointer_type (dhandle
, type
);
162 else if (ISFCN (ntype
))
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
,
169 else if (ISARY (ntype
))
180 /* FIXME: If pauxent->x_sym.x_tagndx.l == 0, gdb sets
181 the c_naux field of the syment to 0. */
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
;
187 for (i
= 0; *dim
!= 0 && i
< DIMNUM
- 1; i
++, dim
++)
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
,
206 if (pauxent
!= NULL
&& pauxent
->x_sym
.x_tagndx
.l
> 0)
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
)
216 return debug_make_indirect_type (dhandle
, slot
, (const char *) NULL
);
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. */
228 return parse_coff_base_type (abfd
, symbols
, types
, coff_symno
, ntype
,
232 /* Parse a basic COFF type in NTYPE. */
235 parse_coff_base_type (abfd
, symbols
, types
, coff_symno
, ntype
, pauxent
,
238 struct coff_symbols
*symbols
;
239 struct coff_types
*types
;
242 union internal_auxent
*pauxent
;
252 && types
->basic
[ntype
] != DEBUG_TYPE_NULL
)
253 return types
->basic
[ntype
];
261 ret
= debug_make_void_type (dhandle
);
266 ret
= debug_make_void_type (dhandle
);
271 ret
= debug_make_int_type (dhandle
, 1, false);
276 ret
= debug_make_int_type (dhandle
, 2, false);
281 /* FIXME: Perhaps the size should depend upon the architecture. */
282 ret
= debug_make_int_type (dhandle
, 4, false);
287 ret
= debug_make_int_type (dhandle
, 4, false);
292 ret
= debug_make_float_type (dhandle
, 4);
297 ret
= debug_make_float_type (dhandle
, 8);
302 ret
= debug_make_float_type (dhandle
, 12);
303 name
= "long double";
307 ret
= debug_make_int_type (dhandle
, 1, true);
308 name
= "unsigned char";
312 ret
= debug_make_int_type (dhandle
, 2, true);
313 name
= "unsigned short";
317 ret
= debug_make_int_type (dhandle
, 4, true);
318 name
= "unsigned int";
322 ret
= debug_make_int_type (dhandle
, 4, true);
323 name
= "unsigned long";
328 ret
= debug_make_struct_type (dhandle
, true, 0,
329 (debug_field
*) NULL
);
331 ret
= parse_coff_struct_type (abfd
, symbols
, types
, ntype
, pauxent
,
334 slot
= coff_get_slot (types
, coff_symno
);
342 ret
= debug_make_struct_type (dhandle
, false, 0, (debug_field
*) NULL
);
344 ret
= parse_coff_struct_type (abfd
, symbols
, types
, ntype
, pauxent
,
347 slot
= coff_get_slot (types
, coff_symno
);
355 ret
= debug_make_enum_type (dhandle
, (const char **) NULL
,
356 (bfd_signed_vma
*) NULL
);
358 ret
= parse_coff_enum_type (abfd
, symbols
, types
, pauxent
, dhandle
);
360 slot
= coff_get_slot (types
, coff_symno
);
368 ret
= debug_name_type (dhandle
, name
, ret
);
373 types
->basic
[ntype
] = ret
;
378 /* Parse a struct type. */
381 parse_coff_struct_type (abfd
, symbols
, types
, ntype
, pauxent
, dhandle
)
383 struct coff_symbols
*symbols
;
384 struct coff_types
*types
;
386 union internal_auxent
*pauxent
;
395 symend
= pauxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
398 fields
= (debug_field
*) xmalloc (alloc
* sizeof *fields
);
403 && symbols
->coff_symno
< symend
404 && symbols
->symno
< symbols
->symcount
)
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;
413 sym
= symbols
->syms
[symbols
->symno
];
415 if (! bfd_coff_get_syment (abfd
, sym
, &syment
))
417 fprintf (stderr
, "%s: bfd_coff_get_syment failed: %s\n",
418 program_name
, bfd_errmsg (bfd_get_error ()));
419 return DEBUG_TYPE_NULL
;
422 this_coff_symno
= symbols
->coff_symno
;
425 symbols
->coff_symno
+= 1 + syment
.n_numaux
;
427 if (syment
.n_numaux
== 0)
431 if (! bfd_coff_get_auxent (abfd
, sym
, 0, &auxent
))
433 fprintf (stderr
, "%s: bfd_coff_get_auxent failed: %s\n",
434 program_name
, bfd_errmsg (bfd_get_error ()));
435 return DEBUG_TYPE_NULL
;
440 switch (syment
.n_sclass
)
444 bitpos
= 8 * bfd_asymbol_value (sym
);
449 bitpos
= bfd_asymbol_value (sym
);
450 bitsize
= auxent
.x_sym
.x_misc
.x_lnsz
.x_size
;
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
;
470 if (count
+ 1 >= alloc
)
473 fields
= ((debug_field
*)
474 xrealloc (fields
, alloc
* sizeof *fields
));
482 fields
[count
] = DEBUG_FIELD_NULL
;
484 return debug_make_struct_type (dhandle
, ntype
== T_STRUCT
,
485 pauxent
->x_sym
.x_misc
.x_lnsz
.x_size
,
489 /* Parse an enum type. */
492 parse_coff_enum_type (abfd
, symbols
, types
, pauxent
, dhandle
)
494 struct coff_symbols
*symbols
;
495 struct coff_types
*types
;
496 union internal_auxent
*pauxent
;
502 bfd_signed_vma
*vals
;
506 symend
= pauxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
509 names
= (const char **) xmalloc (alloc
* sizeof *names
);
510 vals
= (bfd_signed_vma
*) xmalloc (alloc
* sizeof *vals
);
515 && symbols
->coff_symno
< symend
516 && symbols
->symno
< symbols
->symcount
)
519 struct internal_syment syment
;
521 sym
= symbols
->syms
[symbols
->symno
];
523 if (! bfd_coff_get_syment (abfd
, sym
, &syment
))
525 fprintf (stderr
, "%s: bfd_coff_get_syment failed: %s\n",
526 program_name
, bfd_errmsg (bfd_get_error ()));
527 return DEBUG_TYPE_NULL
;
531 symbols
->coff_symno
+= 1 + syment
.n_numaux
;
533 switch (syment
.n_sclass
)
536 if (count
+ 1 >= alloc
)
539 names
= ((const char **)
540 xrealloc (names
, alloc
* sizeof *names
));
541 vals
= ((bfd_signed_vma
*)
542 xrealloc (vals
, alloc
* sizeof *vals
));
545 names
[count
] = bfd_asymbol_name (sym
);
546 vals
[count
] = bfd_asymbol_value (sym
);
558 return debug_make_enum_type (dhandle
, names
, vals
);
561 /* Handle a single COFF symbol. */
564 parse_coff_symbol (abfd
, types
, sym
, coff_symno
, psyment
, dhandle
, type
,
567 struct coff_types
*types
;
570 struct internal_syment
*psyment
;
573 boolean within_function
;
575 switch (psyment
->n_sclass
)
581 if (! debug_record_variable (dhandle
, bfd_asymbol_name (sym
), type
,
582 DEBUG_LOCAL
, bfd_asymbol_value (sym
)))
587 if (! debug_record_variable (dhandle
, bfd_asymbol_name (sym
), type
,
588 DEBUG_GLOBAL
, bfd_asymbol_value (sym
)))
593 if (! debug_record_variable (dhandle
, bfd_asymbol_name (sym
), type
,
597 bfd_asymbol_value (sym
)))
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
)))
612 if (! debug_record_parameter (dhandle
, bfd_asymbol_name (sym
), type
,
613 DEBUG_PARM_STACK
, bfd_asymbol_value (sym
)))
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
)))
625 type
= debug_name_type (dhandle
, bfd_asymbol_name (sym
), type
);
626 if (type
== DEBUG_TYPE_NULL
)
636 type
= debug_tag_type (dhandle
, bfd_asymbol_name (sym
), type
);
637 if (type
== DEBUG_TYPE_NULL
)
640 /* Store the named type into the slot, so that references get
642 slot
= coff_get_slot (types
, coff_symno
);
654 /* This is the main routine. It looks through all the symbols and
658 parse_coff (abfd
, syms
, symcount
, dhandle
)
664 struct coff_symbols symbols
;
665 struct coff_types types
;
672 boolean within_function
;
673 long this_coff_symno
;
676 symbols
.symcount
= symcount
;
678 symbols
.coff_symno
= 0;
681 for (i
= 0; i
<= T_MAX
; i
++)
682 types
.basic
[i
] = DEBUG_TYPE_NULL
;
689 within_function
= false;
691 while (symbols
.symno
< symcount
)
695 struct internal_syment syment
;
696 union internal_auxent auxent
;
697 union internal_auxent
*paux
;
700 sym
= syms
[symbols
.symno
];
702 if (! bfd_coff_get_syment (abfd
, sym
, &syment
))
704 fprintf (stderr
, "%s: bfd_coff_get_syment failed: %s\n",
705 program_name
, bfd_errmsg (bfd_get_error ()));
709 name
= bfd_asymbol_name (sym
);
711 this_coff_symno
= symbols
.coff_symno
;
714 symbols
.coff_symno
+= 1 + syment
.n_numaux
;
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)
722 if (! bfd_coff_get_auxent (abfd
, sym
, 0, &auxent
))
724 fprintf (stderr
, "%s: bfd_coff_get_auxent failed: %s\n",
725 program_name
, bfd_errmsg (bfd_get_error ()));
731 if (this_coff_symno
== next_c_file
&& syment
.n_sclass
!= C_FILE
)
733 /* The last C_FILE symbol points to the first external
735 if (! debug_set_filename (dhandle
, "*globals*"))
739 switch (syment
.n_sclass
)
748 /* Just ignore these classes. */
752 next_c_file
= syment
.n_value
;
753 if (! debug_set_filename (dhandle
, name
))
758 /* Ignore static symbols with a type of T_NULL. These
759 represent section entries. */
760 if (syment
.n_type
== T_NULL
)
764 if (ISFCN (syment
.n_type
))
767 fnclass
= syment
.n_sclass
;
768 fntype
= syment
.n_type
;
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 fprintf (stderr
, "%s: %ld: .bf without preceding function\n",
787 program_name
, this_coff_symno
);
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
,
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 fprintf (stderr
, "%s: %ld: unexpected .ef\n",
837 program_name
, this_coff_symno
);
841 if (! debug_end_function (dhandle
, bfd_asymbol_value (sym
)))
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
))