1 /* stabs.c -- Parse COFF debugging information
2 Copyright 1996, 2000, 2002 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
86 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
*, bfd_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 bfd_boolean parse_coff_symbol
100 PARAMS ((bfd
*, struct coff_types
*, asymbol
*, long,
101 struct internal_syment
*, PTR
, debug_type
, bfd_boolean
));
102 static bfd_boolean external_coff_symbol_p
103 PARAMS ((int sym_class
));
105 /* Return the slot for a type. */
108 coff_get_slot (types
, indx
)
109 struct coff_types
*types
;
112 struct coff_slots
**pps
;
116 while (indx
>= COFF_SLOTS
)
120 *pps
= (struct coff_slots
*) xmalloc (sizeof **pps
);
121 memset (*pps
, 0, sizeof **pps
);
129 *pps
= (struct coff_slots
*) xmalloc (sizeof **pps
);
130 memset (*pps
, 0, sizeof **pps
);
133 return (*pps
)->slots
+ indx
;
136 /* Parse a COFF type code in NTYPE. */
139 parse_coff_type (abfd
, symbols
, types
, coff_symno
, ntype
, pauxent
, useaux
,
142 struct coff_symbols
*symbols
;
143 struct coff_types
*types
;
146 union internal_auxent
*pauxent
;
152 if ((ntype
& ~N_BTMASK
) != 0)
156 newtype
= DECREF (ntype
);
160 type
= parse_coff_type (abfd
, symbols
, types
, coff_symno
, newtype
,
161 pauxent
, useaux
, dhandle
);
162 type
= debug_make_pointer_type (dhandle
, type
);
164 else if (ISFCN (ntype
))
166 type
= parse_coff_type (abfd
, symbols
, types
, coff_symno
, newtype
,
167 pauxent
, useaux
, dhandle
);
168 type
= debug_make_function_type (dhandle
, type
, (debug_type
*) NULL
,
171 else if (ISARY (ntype
))
182 /* FIXME: If pauxent->x_sym.x_tagndx.l == 0, gdb sets
183 the c_naux field of the syment to 0. */
185 /* Move the dimensions down, so that the next array
186 picks up the next one. */
187 dim
= pauxent
->x_sym
.x_fcnary
.x_ary
.x_dimen
;
189 for (i
= 0; *dim
!= 0 && i
< DIMNUM
- 1; i
++, dim
++)
194 type
= parse_coff_type (abfd
, symbols
, types
, coff_symno
, newtype
,
195 pauxent
, FALSE
, dhandle
);
196 type
= debug_make_array_type (dhandle
, type
,
197 parse_coff_base_type (abfd
, symbols
,
206 non_fatal (_("parse_coff_type: Bad type code 0x%x"), ntype
);
207 return DEBUG_TYPE_NULL
;
213 if (pauxent
!= NULL
&& pauxent
->x_sym
.x_tagndx
.l
> 0)
217 /* This is a reference to an existing type. FIXME: gdb checks
218 that the class is not C_STRTAG, nor C_UNTAG, nor C_ENTAG. */
219 slot
= coff_get_slot (types
, pauxent
->x_sym
.x_tagndx
.l
);
220 if (*slot
!= DEBUG_TYPE_NULL
)
223 return debug_make_indirect_type (dhandle
, slot
, (const char *) NULL
);
226 /* If the aux entry has already been used for something, useaux will
227 have been set to false, indicating that parse_coff_base_type
228 should not use it. We need to do it this way, rather than simply
229 passing pauxent as NULL, because we need to be able handle
230 multiple array dimensions while still discarding pauxent after
231 having handled all of them. */
235 return parse_coff_base_type (abfd
, symbols
, types
, coff_symno
, ntype
,
239 /* Parse a basic COFF type in NTYPE. */
242 parse_coff_base_type (abfd
, symbols
, types
, coff_symno
, ntype
, pauxent
,
245 struct coff_symbols
*symbols
;
246 struct coff_types
*types
;
249 union internal_auxent
*pauxent
;
253 bfd_boolean set_basic
;
259 && types
->basic
[ntype
] != DEBUG_TYPE_NULL
)
260 return types
->basic
[ntype
];
268 ret
= debug_make_void_type (dhandle
);
273 ret
= debug_make_void_type (dhandle
);
278 ret
= debug_make_int_type (dhandle
, 1, FALSE
);
283 ret
= debug_make_int_type (dhandle
, 2, FALSE
);
288 /* FIXME: Perhaps the size should depend upon the architecture. */
289 ret
= debug_make_int_type (dhandle
, 4, FALSE
);
294 ret
= debug_make_int_type (dhandle
, 4, FALSE
);
299 ret
= debug_make_float_type (dhandle
, 4);
304 ret
= debug_make_float_type (dhandle
, 8);
309 ret
= debug_make_float_type (dhandle
, 12);
310 name
= "long double";
314 ret
= debug_make_int_type (dhandle
, 1, TRUE
);
315 name
= "unsigned char";
319 ret
= debug_make_int_type (dhandle
, 2, TRUE
);
320 name
= "unsigned short";
324 ret
= debug_make_int_type (dhandle
, 4, TRUE
);
325 name
= "unsigned int";
329 ret
= debug_make_int_type (dhandle
, 4, TRUE
);
330 name
= "unsigned long";
335 ret
= debug_make_struct_type (dhandle
, TRUE
, 0,
336 (debug_field
*) NULL
);
338 ret
= parse_coff_struct_type (abfd
, symbols
, types
, ntype
, pauxent
,
341 slot
= coff_get_slot (types
, coff_symno
);
349 ret
= debug_make_struct_type (dhandle
, FALSE
, 0, (debug_field
*) NULL
);
351 ret
= parse_coff_struct_type (abfd
, symbols
, types
, ntype
, pauxent
,
354 slot
= coff_get_slot (types
, coff_symno
);
362 ret
= debug_make_enum_type (dhandle
, (const char **) NULL
,
363 (bfd_signed_vma
*) NULL
);
365 ret
= parse_coff_enum_type (abfd
, symbols
, types
, pauxent
, dhandle
);
367 slot
= coff_get_slot (types
, coff_symno
);
375 ret
= debug_name_type (dhandle
, name
, ret
);
380 types
->basic
[ntype
] = ret
;
385 /* Parse a struct type. */
388 parse_coff_struct_type (abfd
, symbols
, types
, ntype
, pauxent
, dhandle
)
390 struct coff_symbols
*symbols
;
391 struct coff_types
*types
;
393 union internal_auxent
*pauxent
;
402 symend
= pauxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
405 fields
= (debug_field
*) xmalloc (alloc
* sizeof *fields
);
410 && symbols
->coff_symno
< symend
411 && symbols
->symno
< symbols
->symcount
)
414 long this_coff_symno
;
415 struct internal_syment syment
;
416 union internal_auxent auxent
;
417 union internal_auxent
*psubaux
;
418 bfd_vma bitpos
= 0, bitsize
= 0;
420 sym
= symbols
->syms
[symbols
->symno
];
422 if (! bfd_coff_get_syment (abfd
, sym
, &syment
))
424 non_fatal (_("bfd_coff_get_syment failed: %s"),
425 bfd_errmsg (bfd_get_error ()));
426 return DEBUG_TYPE_NULL
;
429 this_coff_symno
= symbols
->coff_symno
;
432 symbols
->coff_symno
+= 1 + syment
.n_numaux
;
434 if (syment
.n_numaux
== 0)
438 if (! bfd_coff_get_auxent (abfd
, sym
, 0, &auxent
))
440 non_fatal (_("bfd_coff_get_auxent failed: %s"),
441 bfd_errmsg (bfd_get_error ()));
442 return DEBUG_TYPE_NULL
;
447 switch (syment
.n_sclass
)
451 bitpos
= 8 * bfd_asymbol_value (sym
);
456 bitpos
= bfd_asymbol_value (sym
);
457 bitsize
= auxent
.x_sym
.x_misc
.x_lnsz
.x_size
;
470 ftype
= parse_coff_type (abfd
, symbols
, types
, this_coff_symno
,
471 syment
.n_type
, psubaux
, TRUE
, dhandle
);
472 f
= debug_make_field (dhandle
, bfd_asymbol_name (sym
), ftype
,
473 bitpos
, bitsize
, DEBUG_VISIBILITY_PUBLIC
);
474 if (f
== DEBUG_FIELD_NULL
)
475 return DEBUG_TYPE_NULL
;
477 if (count
+ 1 >= alloc
)
480 fields
= ((debug_field
*)
481 xrealloc (fields
, alloc
* sizeof *fields
));
489 fields
[count
] = DEBUG_FIELD_NULL
;
491 return debug_make_struct_type (dhandle
, ntype
== T_STRUCT
,
492 pauxent
->x_sym
.x_misc
.x_lnsz
.x_size
,
496 /* Parse an enum type. */
499 parse_coff_enum_type (abfd
, symbols
, types
, pauxent
, dhandle
)
501 struct coff_symbols
*symbols
;
502 struct coff_types
*types ATTRIBUTE_UNUSED
;
503 union internal_auxent
*pauxent
;
509 bfd_signed_vma
*vals
;
513 symend
= pauxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
516 names
= (const char **) xmalloc (alloc
* sizeof *names
);
517 vals
= (bfd_signed_vma
*) xmalloc (alloc
* sizeof *vals
);
522 && symbols
->coff_symno
< symend
523 && symbols
->symno
< symbols
->symcount
)
526 struct internal_syment syment
;
528 sym
= symbols
->syms
[symbols
->symno
];
530 if (! bfd_coff_get_syment (abfd
, sym
, &syment
))
532 non_fatal (_("bfd_coff_get_syment failed: %s"),
533 bfd_errmsg (bfd_get_error ()));
534 return DEBUG_TYPE_NULL
;
538 symbols
->coff_symno
+= 1 + syment
.n_numaux
;
540 switch (syment
.n_sclass
)
543 if (count
+ 1 >= alloc
)
546 names
= ((const char **)
547 xrealloc (names
, alloc
* sizeof *names
));
548 vals
= ((bfd_signed_vma
*)
549 xrealloc (vals
, alloc
* sizeof *vals
));
552 names
[count
] = bfd_asymbol_name (sym
);
553 vals
[count
] = bfd_asymbol_value (sym
);
565 return debug_make_enum_type (dhandle
, names
, vals
);
568 /* Handle a single COFF symbol. */
571 parse_coff_symbol (abfd
, types
, sym
, coff_symno
, psyment
, dhandle
, type
,
573 bfd
*abfd ATTRIBUTE_UNUSED
;
574 struct coff_types
*types
;
577 struct internal_syment
*psyment
;
580 bfd_boolean within_function
;
582 switch (psyment
->n_sclass
)
588 if (! debug_record_variable (dhandle
, bfd_asymbol_name (sym
), type
,
589 DEBUG_LOCAL
, bfd_asymbol_value (sym
)))
595 if (! debug_record_variable (dhandle
, bfd_asymbol_name (sym
), type
,
596 DEBUG_GLOBAL
, bfd_asymbol_value (sym
)))
601 if (! debug_record_variable (dhandle
, bfd_asymbol_name (sym
), type
,
605 bfd_asymbol_value (sym
)))
610 /* FIXME: We may need to convert the register number. */
611 if (! debug_record_variable (dhandle
, bfd_asymbol_name (sym
), type
,
612 DEBUG_REGISTER
, bfd_asymbol_value (sym
)))
620 if (! debug_record_parameter (dhandle
, bfd_asymbol_name (sym
), type
,
621 DEBUG_PARM_STACK
, bfd_asymbol_value (sym
)))
626 /* FIXME: We may need to convert the register number. */
627 if (! debug_record_parameter (dhandle
, bfd_asymbol_name (sym
), type
,
628 DEBUG_PARM_REG
, bfd_asymbol_value (sym
)))
633 type
= debug_name_type (dhandle
, bfd_asymbol_name (sym
), type
);
634 if (type
== DEBUG_TYPE_NULL
)
644 type
= debug_tag_type (dhandle
, bfd_asymbol_name (sym
), type
);
645 if (type
== DEBUG_TYPE_NULL
)
648 /* Store the named type into the slot, so that references get
650 slot
= coff_get_slot (types
, coff_symno
);
662 /* Determine if a symbol has external visibility. */
665 external_coff_symbol_p (sym_class
)
679 /* This is the main routine. It looks through all the symbols and
683 parse_coff (abfd
, syms
, symcount
, dhandle
)
689 struct coff_symbols symbols
;
690 struct coff_types types
;
698 bfd_boolean within_function
;
699 long this_coff_symno
;
702 symbols
.symcount
= symcount
;
704 symbols
.coff_symno
= 0;
707 for (i
= 0; i
<= T_MAX
; i
++)
708 types
.basic
[i
] = DEBUG_TYPE_NULL
;
716 within_function
= FALSE
;
718 while (symbols
.symno
< symcount
)
722 struct internal_syment syment
;
723 union internal_auxent auxent
;
724 union internal_auxent
*paux
;
727 sym
= syms
[symbols
.symno
];
729 if (! bfd_coff_get_syment (abfd
, sym
, &syment
))
731 non_fatal (_("bfd_coff_get_syment failed: %s"),
732 bfd_errmsg (bfd_get_error ()));
736 name
= bfd_asymbol_name (sym
);
738 this_coff_symno
= symbols
.coff_symno
;
741 symbols
.coff_symno
+= 1 + syment
.n_numaux
;
743 /* We only worry about the first auxent, because that is the
744 only one which is relevant for debugging information. */
745 if (syment
.n_numaux
== 0)
749 if (! bfd_coff_get_auxent (abfd
, sym
, 0, &auxent
))
751 non_fatal (_("bfd_coff_get_auxent failed: %s"),
752 bfd_errmsg (bfd_get_error ()));
758 if (this_coff_symno
== next_c_file
&& syment
.n_sclass
!= C_FILE
)
760 /* The last C_FILE symbol points to the first external
762 if (! debug_set_filename (dhandle
, "*globals*"))
766 switch (syment
.n_sclass
)
775 /* Just ignore these classes. */
779 next_c_file
= syment
.n_value
;
780 if (! debug_set_filename (dhandle
, name
))
785 /* Ignore static symbols with a type of T_NULL. These
786 represent section entries. */
787 if (syment
.n_type
== T_NULL
)
792 if (ISFCN (syment
.n_type
))
795 fnclass
= syment
.n_sclass
;
796 fntype
= syment
.n_type
;
797 if (syment
.n_numaux
> 0)
798 fnend
= bfd_asymbol_value (sym
) + auxent
.x_sym
.x_misc
.x_fsize
;
801 linenos
= BFD_SEND (abfd
, _get_lineno
, (abfd
, sym
));
804 type
= parse_coff_type (abfd
, &symbols
, &types
, this_coff_symno
,
805 syment
.n_type
, paux
, TRUE
, dhandle
);
806 if (type
== DEBUG_TYPE_NULL
)
808 if (! parse_coff_symbol (abfd
, &types
, sym
, this_coff_symno
, &syment
,
809 dhandle
, type
, within_function
))
814 if (strcmp (name
, ".bf") == 0)
818 non_fatal (_("%ld: .bf without preceding function"),
823 type
= parse_coff_type (abfd
, &symbols
, &types
, this_coff_symno
,
824 DECREF (fntype
), paux
, FALSE
, dhandle
);
825 if (type
== DEBUG_TYPE_NULL
)
828 if (! debug_record_function (dhandle
, fnname
, type
,
829 external_coff_symbol_p (fnclass
),
830 bfd_asymbol_value (sym
)))
838 if (syment
.n_numaux
== 0)
841 base
= auxent
.x_sym
.x_misc
.x_lnsz
.x_lnno
- 1;
843 addr
= bfd_get_section_vma (abfd
, bfd_get_section (sym
));
847 while (linenos
->line_number
!= 0)
849 if (! debug_record_line (dhandle
,
850 linenos
->line_number
+ base
,
851 linenos
->u
.offset
+ addr
))
862 within_function
= TRUE
;
864 else if (strcmp (name
, ".ef") == 0)
866 if (! within_function
)
868 non_fatal (_("%ld: unexpected .ef\n"), this_coff_symno
);
872 if (bfd_asymbol_value (sym
) > fnend
)
873 fnend
= bfd_asymbol_value (sym
);
874 if (! debug_end_function (dhandle
, fnend
))
878 within_function
= FALSE
;
883 if (strcmp (name
, ".bb") == 0)
885 if (! debug_start_block (dhandle
, bfd_asymbol_value (sym
)))
888 else if (strcmp (name
, ".eb") == 0)
890 if (! debug_end_block (dhandle
, bfd_asymbol_value (sym
)))
896 type
= parse_coff_type (abfd
, &symbols
, &types
, this_coff_symno
,
897 syment
.n_type
, paux
, TRUE
, dhandle
);
898 if (type
== DEBUG_TYPE_NULL
)
900 if (! parse_coff_symbol (abfd
, &types
, sym
, this_coff_symno
, &syment
,
901 dhandle
, type
, within_function
))