1 /* stabs.c -- Parse COFF debugging information
2 Copyright (C) 1996-2023 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
*, long);
87 static debug_type parse_coff_type
88 (bfd
*, struct coff_symbols
*, struct coff_types
*, long, int,
89 union internal_auxent
*, bool, 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 bool parse_coff_symbol
100 (bfd
*, struct coff_types
*, asymbol
*, long, struct internal_syment
*,
101 void *, debug_type
, bool);
102 static bool external_coff_symbol_p (int sym_class
);
104 /* Return the slot for a type. */
107 coff_get_slot (struct coff_types
*types
, long indx
)
109 struct coff_slots
**pps
;
113 /* PR 17512: file: 078-18333-0.001:0.1.
114 FIXME: The value of 1000 is a guess. Maybe a better heuristic is needed. */
115 if (indx
/ COFF_SLOTS
> 1000)
116 fatal (_("Excessively large slot index: %lx"), indx
);
118 while (indx
>= COFF_SLOTS
)
122 *pps
= (struct coff_slots
*) xmalloc (sizeof **pps
);
123 memset (*pps
, 0, sizeof **pps
);
131 *pps
= (struct coff_slots
*) xmalloc (sizeof **pps
);
132 memset (*pps
, 0, sizeof **pps
);
135 return (*pps
)->slots
+ indx
;
138 /* Parse a COFF type code in NTYPE. */
141 parse_coff_type (bfd
*abfd
, struct coff_symbols
*symbols
,
142 struct coff_types
*types
, long coff_symno
, int ntype
,
143 union internal_auxent
*pauxent
, bool useaux
,
148 if ((ntype
& ~N_BTMASK
) != 0)
152 newtype
= DECREF (ntype
);
156 type
= parse_coff_type (abfd
, symbols
, types
, coff_symno
, newtype
,
157 pauxent
, useaux
, dhandle
);
158 type
= debug_make_pointer_type (dhandle
, type
);
160 else if (ISFCN (ntype
))
162 type
= parse_coff_type (abfd
, symbols
, types
, coff_symno
, newtype
,
163 pauxent
, useaux
, dhandle
);
164 type
= debug_make_function_type (dhandle
, type
, (debug_type
*) NULL
,
167 else if (ISARY (ntype
))
178 /* FIXME: If pauxent->x_sym.x_tagndx.l == 0, gdb sets
179 the c_naux field of the syment to 0. */
181 /* Move the dimensions down, so that the next array
182 picks up the next one. */
183 dim
= pauxent
->x_sym
.x_fcnary
.x_ary
.x_dimen
;
185 for (i
= 0; *dim
!= 0 && i
< DIMNUM
- 1; i
++, dim
++)
190 type
= parse_coff_type (abfd
, symbols
, types
, coff_symno
, newtype
,
191 pauxent
, false, dhandle
);
192 type
= debug_make_array_type (dhandle
, type
,
193 parse_coff_base_type (abfd
, symbols
,
202 non_fatal (_("parse_coff_type: Bad type code 0x%x"), ntype
);
203 return DEBUG_TYPE_NULL
;
209 if (pauxent
!= NULL
&& pauxent
->x_sym
.x_tagndx
.l
> 0)
213 /* This is a reference to an existing type. FIXME: gdb checks
214 that the class is not C_STRTAG, nor C_UNTAG, nor C_ENTAG. */
215 slot
= coff_get_slot (types
, pauxent
->x_sym
.x_tagndx
.l
);
216 if (*slot
!= DEBUG_TYPE_NULL
)
219 return debug_make_indirect_type (dhandle
, slot
, (const char *) NULL
);
222 /* If the aux entry has already been used for something, useaux will
223 have been set to false, indicating that parse_coff_base_type
224 should not use it. We need to do it this way, rather than simply
225 passing pauxent as NULL, because we need to be able handle
226 multiple array dimensions while still discarding pauxent after
227 having handled all of them. */
231 return parse_coff_base_type (abfd
, symbols
, types
, coff_symno
, ntype
,
235 /* Parse a basic COFF type in NTYPE. */
238 parse_coff_base_type (bfd
*abfd
, struct coff_symbols
*symbols
,
239 struct coff_types
*types
, long coff_symno
, int ntype
,
240 union internal_auxent
*pauxent
, void *dhandle
)
249 && types
->basic
[ntype
] != DEBUG_TYPE_NULL
)
250 return types
->basic
[ntype
];
258 ret
= debug_make_void_type (dhandle
);
263 ret
= debug_make_void_type (dhandle
);
268 ret
= debug_make_int_type (dhandle
, 1, false);
273 ret
= debug_make_int_type (dhandle
, 2, false);
278 /* FIXME: Perhaps the size should depend upon the architecture. */
279 ret
= debug_make_int_type (dhandle
, 4, false);
284 ret
= debug_make_int_type (dhandle
, 4, false);
289 ret
= debug_make_float_type (dhandle
, 4);
294 ret
= debug_make_float_type (dhandle
, 8);
299 ret
= debug_make_float_type (dhandle
, 12);
300 name
= "long double";
304 ret
= debug_make_int_type (dhandle
, 1, true);
305 name
= "unsigned char";
309 ret
= debug_make_int_type (dhandle
, 2, true);
310 name
= "unsigned short";
314 ret
= debug_make_int_type (dhandle
, 4, true);
315 name
= "unsigned int";
319 ret
= debug_make_int_type (dhandle
, 4, true);
320 name
= "unsigned long";
325 ret
= debug_make_struct_type (dhandle
, true, 0,
326 (debug_field
*) NULL
);
328 ret
= parse_coff_struct_type (abfd
, symbols
, types
, ntype
, pauxent
,
331 slot
= coff_get_slot (types
, coff_symno
);
339 ret
= debug_make_struct_type (dhandle
, false, 0, (debug_field
*) NULL
);
341 ret
= parse_coff_struct_type (abfd
, symbols
, types
, ntype
, pauxent
,
344 slot
= coff_get_slot (types
, coff_symno
);
352 ret
= debug_make_enum_type (dhandle
, (const char **) NULL
,
353 (bfd_signed_vma
*) NULL
);
355 ret
= parse_coff_enum_type (abfd
, symbols
, types
, pauxent
, dhandle
);
357 slot
= coff_get_slot (types
, coff_symno
);
365 ret
= debug_name_type (dhandle
, name
, ret
);
370 types
->basic
[ntype
] = ret
;
375 /* Parse a struct type. */
378 parse_coff_struct_type (bfd
*abfd
, struct coff_symbols
*symbols
,
379 struct coff_types
*types
, int ntype
,
380 union internal_auxent
*pauxent
, void *dhandle
)
388 symend
= pauxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
391 fields
= (debug_field
*) xmalloc (alloc
* sizeof *fields
);
396 && symbols
->coff_symno
< symend
397 && symbols
->symno
< symbols
->symcount
)
400 long this_coff_symno
;
401 struct internal_syment syment
;
402 union internal_auxent auxent
;
403 union internal_auxent
*psubaux
;
404 bfd_vma bitpos
= 0, bitsize
= 0;
406 sym
= symbols
->syms
[symbols
->symno
];
408 if (! bfd_coff_get_syment (abfd
, sym
, &syment
))
410 non_fatal (_("bfd_coff_get_syment failed: %s"),
411 bfd_errmsg (bfd_get_error ()));
413 return DEBUG_TYPE_NULL
;
416 this_coff_symno
= symbols
->coff_symno
;
419 symbols
->coff_symno
+= 1 + syment
.n_numaux
;
421 if (syment
.n_numaux
== 0)
425 if (! bfd_coff_get_auxent (abfd
, sym
, 0, &auxent
))
427 non_fatal (_("bfd_coff_get_auxent failed: %s"),
428 bfd_errmsg (bfd_get_error ()));
430 return DEBUG_TYPE_NULL
;
435 switch (syment
.n_sclass
)
439 bitpos
= 8 * bfd_asymbol_value (sym
);
444 bitpos
= bfd_asymbol_value (sym
);
445 bitsize
= auxent
.x_sym
.x_misc
.x_lnsz
.x_size
;
458 ftype
= parse_coff_type (abfd
, symbols
, types
, this_coff_symno
,
459 syment
.n_type
, psubaux
, true, dhandle
);
460 f
= debug_make_field (dhandle
, bfd_asymbol_name (sym
), ftype
,
461 bitpos
, bitsize
, DEBUG_VISIBILITY_PUBLIC
);
462 if (f
== DEBUG_FIELD_NULL
)
463 return DEBUG_TYPE_NULL
;
465 if (count
+ 1 >= alloc
)
468 fields
= ((debug_field
*)
469 xrealloc (fields
, alloc
* sizeof *fields
));
477 fields
[count
] = DEBUG_FIELD_NULL
;
479 return debug_make_struct_type (dhandle
, ntype
== T_STRUCT
,
480 pauxent
->x_sym
.x_misc
.x_lnsz
.x_size
,
484 /* Parse an enum type. */
487 parse_coff_enum_type (bfd
*abfd
, struct coff_symbols
*symbols
,
488 struct coff_types
*types ATTRIBUTE_UNUSED
,
489 union internal_auxent
*pauxent
, void *dhandle
)
494 bfd_signed_vma
*vals
;
498 symend
= pauxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
501 names
= (const char **) xmalloc (alloc
* sizeof *names
);
502 vals
= (bfd_signed_vma
*) xmalloc (alloc
* sizeof *vals
);
507 && symbols
->coff_symno
< symend
508 && symbols
->symno
< symbols
->symcount
)
511 struct internal_syment syment
;
513 sym
= symbols
->syms
[symbols
->symno
];
515 if (! bfd_coff_get_syment (abfd
, sym
, &syment
))
517 non_fatal (_("bfd_coff_get_syment failed: %s"),
518 bfd_errmsg (bfd_get_error ()));
521 return DEBUG_TYPE_NULL
;
525 symbols
->coff_symno
+= 1 + syment
.n_numaux
;
527 switch (syment
.n_sclass
)
530 if (count
+ 1 >= alloc
)
533 names
= ((const char **)
534 xrealloc (names
, alloc
* sizeof *names
));
535 vals
= ((bfd_signed_vma
*)
536 xrealloc (vals
, alloc
* sizeof *vals
));
539 names
[count
] = bfd_asymbol_name (sym
);
540 vals
[count
] = bfd_asymbol_value (sym
);
552 return debug_make_enum_type (dhandle
, names
, vals
);
555 /* Handle a single COFF symbol. */
558 parse_coff_symbol (bfd
*abfd ATTRIBUTE_UNUSED
, struct coff_types
*types
,
559 asymbol
*sym
, long coff_symno
,
560 struct internal_syment
*psyment
, void *dhandle
,
561 debug_type type
, bool within_function
)
563 switch (psyment
->n_sclass
)
569 if (! debug_record_variable (dhandle
, bfd_asymbol_name (sym
), type
,
570 DEBUG_LOCAL
, bfd_asymbol_value (sym
)))
576 if (! debug_record_variable (dhandle
, bfd_asymbol_name (sym
), type
,
577 DEBUG_GLOBAL
, bfd_asymbol_value (sym
)))
582 if (! debug_record_variable (dhandle
, bfd_asymbol_name (sym
), type
,
586 bfd_asymbol_value (sym
)))
591 /* FIXME: We may need to convert the register number. */
592 if (! debug_record_variable (dhandle
, bfd_asymbol_name (sym
), type
,
593 DEBUG_REGISTER
, bfd_asymbol_value (sym
)))
601 if (! debug_record_parameter (dhandle
, bfd_asymbol_name (sym
), type
,
602 DEBUG_PARM_STACK
, bfd_asymbol_value (sym
)))
607 /* FIXME: We may need to convert the register number. */
608 if (! debug_record_parameter (dhandle
, bfd_asymbol_name (sym
), type
,
609 DEBUG_PARM_REG
, bfd_asymbol_value (sym
)))
614 type
= debug_name_type (dhandle
, bfd_asymbol_name (sym
), type
);
615 if (type
== DEBUG_TYPE_NULL
)
625 type
= debug_tag_type (dhandle
, bfd_asymbol_name (sym
), type
);
626 if (type
== DEBUG_TYPE_NULL
)
629 /* Store the named type into the slot, so that references get
631 slot
= coff_get_slot (types
, coff_symno
);
643 /* Determine if a symbol has external visibility. */
646 external_coff_symbol_p (int sym_class
)
659 /* This is the main routine. It looks through all the symbols and
663 parse_coff (bfd
*abfd
, asymbol
**syms
, long symcount
, void *dhandle
)
665 struct coff_symbols symbols
;
666 struct coff_types types
;
674 bool within_function
;
675 long this_coff_symno
;
678 symbols
.symcount
= symcount
;
680 symbols
.coff_symno
= 0;
683 for (i
= 0; i
<= T_MAX
; i
++)
684 types
.basic
[i
] = DEBUG_TYPE_NULL
;
692 within_function
= false;
694 while (symbols
.symno
< symcount
)
698 struct internal_syment syment
;
699 union internal_auxent auxent
;
700 union internal_auxent
*paux
;
703 sym
= syms
[symbols
.symno
];
705 if (! bfd_coff_get_syment (abfd
, sym
, &syment
))
707 non_fatal (_("bfd_coff_get_syment failed: %s"),
708 bfd_errmsg (bfd_get_error ()));
712 name
= bfd_asymbol_name (sym
);
714 this_coff_symno
= symbols
.coff_symno
;
717 symbols
.coff_symno
+= 1 + syment
.n_numaux
;
719 /* We only worry about the first auxent, because that is the
720 only one which is relevant for debugging information. */
721 if (syment
.n_numaux
== 0)
725 if (! bfd_coff_get_auxent (abfd
, sym
, 0, &auxent
))
727 non_fatal (_("bfd_coff_get_auxent failed: %s"),
728 bfd_errmsg (bfd_get_error ()));
734 if (this_coff_symno
== next_c_file
&& syment
.n_sclass
!= C_FILE
)
736 /* The last C_FILE symbol points to the first external
738 if (! debug_set_filename (dhandle
, "*globals*"))
742 switch (syment
.n_sclass
)
751 /* Just ignore these classes. */
755 next_c_file
= syment
.n_value
;
756 if (! debug_set_filename (dhandle
, name
))
761 /* Ignore static symbols with a type of T_NULL. These
762 represent section entries. */
763 if (syment
.n_type
== T_NULL
)
768 if (ISFCN (syment
.n_type
))
771 fnclass
= syment
.n_sclass
;
772 fntype
= syment
.n_type
;
773 if (syment
.n_numaux
> 0)
774 fnend
= bfd_asymbol_value (sym
) + auxent
.x_sym
.x_misc
.x_fsize
;
777 linenos
= BFD_SEND (abfd
, _get_lineno
, (abfd
, sym
));
780 type
= parse_coff_type (abfd
, &symbols
, &types
, this_coff_symno
,
781 syment
.n_type
, paux
, true, dhandle
);
782 if (type
== DEBUG_TYPE_NULL
)
784 if (! parse_coff_symbol (abfd
, &types
, sym
, this_coff_symno
, &syment
,
785 dhandle
, type
, within_function
))
790 if (strcmp (name
, ".bf") == 0)
794 non_fatal (_("%ld: .bf without preceding function"),
799 type
= parse_coff_type (abfd
, &symbols
, &types
, this_coff_symno
,
800 DECREF (fntype
), paux
, false, dhandle
);
801 if (type
== DEBUG_TYPE_NULL
)
804 if (! debug_record_function (dhandle
, fnname
, type
,
805 external_coff_symbol_p (fnclass
),
806 bfd_asymbol_value (sym
)))
814 if (syment
.n_numaux
== 0)
817 base
= auxent
.x_sym
.x_misc
.x_lnsz
.x_lnno
- 1;
819 addr
= bfd_section_vma (bfd_asymbol_section (sym
));
823 while (linenos
->line_number
!= 0)
825 if (! debug_record_line (dhandle
,
826 linenos
->line_number
+ base
,
827 linenos
->u
.offset
+ addr
))
838 within_function
= true;
840 else if (strcmp (name
, ".ef") == 0)
842 if (! within_function
)
844 non_fatal (_("%ld: unexpected .ef\n"), this_coff_symno
);
848 if (bfd_asymbol_value (sym
) > fnend
)
849 fnend
= bfd_asymbol_value (sym
);
850 if (! debug_end_function (dhandle
, fnend
))
854 within_function
= false;
859 if (strcmp (name
, ".bb") == 0)
861 if (! debug_start_block (dhandle
, bfd_asymbol_value (sym
)))
864 else if (strcmp (name
, ".eb") == 0)
866 if (! debug_end_block (dhandle
, bfd_asymbol_value (sym
)))
872 type
= parse_coff_type (abfd
, &symbols
, &types
, this_coff_symno
,
873 syment
.n_type
, paux
, true, dhandle
);
874 if (type
== DEBUG_TYPE_NULL
)
876 if (! parse_coff_symbol (abfd
, &types
, sym
, this_coff_symno
, &syment
,
877 dhandle
, type
, within_function
))