1 /* stabs.c -- Parse COFF debugging information
2 Copyright (C) 1996-2024 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 /* This structure is used to map symbol indices to types. */
65 /* Next set of slots. */
66 struct coff_types
*next
;
67 /* Where the TYPES array starts. */
68 unsigned int base_index
;
70 #define COFF_SLOTS (16)
71 debug_type types
[COFF_SLOTS
];
74 static debug_type parse_coff_base_type
75 (bfd
*, struct coff_symbols
*, struct coff_types
**, long, int,
76 union internal_auxent
*, void *);
77 static debug_type parse_coff_struct_type
78 (bfd
*, struct coff_symbols
*, struct coff_types
**, int,
79 union internal_auxent
*, void *);
80 static debug_type parse_coff_enum_type
81 (bfd
*, struct coff_symbols
*, struct coff_types
**,
82 union internal_auxent
*, void *);
84 /* Return the slot for a type. */
87 coff_get_slot (void *dhandle
, struct coff_types
**types
, long indx
)
89 unsigned int base_index
;
91 base_index
= indx
/ COFF_SLOTS
* COFF_SLOTS
;
94 while (*types
&& (*types
)->base_index
< base_index
)
95 types
= &(*types
)->next
;
97 if (*types
== NULL
|| (*types
)->base_index
!= base_index
)
99 struct coff_types
*n
= debug_xzalloc (dhandle
, sizeof (*n
));
101 n
->base_index
= base_index
;
105 return (*types
)->types
+ indx
;
108 /* Parse a COFF type code in NTYPE. */
111 parse_coff_type (bfd
*abfd
, struct coff_symbols
*symbols
,
112 struct coff_types
**types
, long coff_symno
, int ntype
,
113 union internal_auxent
*pauxent
, bool useaux
,
118 if ((ntype
& ~N_BTMASK
) != 0)
122 newtype
= DECREF (ntype
);
126 type
= parse_coff_type (abfd
, symbols
, types
, coff_symno
, newtype
,
127 pauxent
, useaux
, dhandle
);
128 type
= debug_make_pointer_type (dhandle
, type
);
130 else if (ISFCN (ntype
))
132 type
= parse_coff_type (abfd
, symbols
, types
, coff_symno
, newtype
,
133 pauxent
, useaux
, dhandle
);
134 type
= debug_make_function_type (dhandle
, type
, (debug_type
*) NULL
,
137 else if (ISARY (ntype
))
148 /* FIXME: If pauxent->x_sym.x_tagndx.l == 0, gdb sets
149 the c_naux field of the syment to 0. */
151 /* Move the dimensions down, so that the next array
152 picks up the next one. */
153 dim
= pauxent
->x_sym
.x_fcnary
.x_ary
.x_dimen
;
155 for (i
= 0; *dim
!= 0 && i
< DIMNUM
- 1; i
++, dim
++)
160 type
= parse_coff_type (abfd
, symbols
, types
, coff_symno
, newtype
,
161 pauxent
, false, dhandle
);
162 type
= debug_make_array_type (dhandle
, type
,
163 parse_coff_base_type (abfd
, symbols
,
172 non_fatal (_("parse_coff_type: Bad type code 0x%x"), ntype
);
173 return DEBUG_TYPE_NULL
;
179 if (pauxent
!= NULL
&& (int32_t) pauxent
->x_sym
.x_tagndx
.u32
> 0)
183 /* This is a reference to an existing type. FIXME: gdb checks
184 that the class is not C_STRTAG, nor C_UNTAG, nor C_ENTAG. */
185 slot
= coff_get_slot (dhandle
, types
, pauxent
->x_sym
.x_tagndx
.u32
);
186 if (*slot
!= DEBUG_TYPE_NULL
)
189 return debug_make_indirect_type (dhandle
, slot
, (const char *) NULL
);
192 /* If the aux entry has already been used for something, useaux will
193 have been set to false, indicating that parse_coff_base_type
194 should not use it. We need to do it this way, rather than simply
195 passing pauxent as NULL, because we need to be able handle
196 multiple array dimensions while still discarding pauxent after
197 having handled all of them. */
201 return parse_coff_base_type (abfd
, symbols
, types
, coff_symno
, ntype
,
205 /* Parse a basic COFF type in NTYPE. */
208 parse_coff_base_type (bfd
*abfd
, struct coff_symbols
*symbols
,
209 struct coff_types
**types
, long coff_symno
, int ntype
,
210 union internal_auxent
*pauxent
, void *dhandle
)
213 const char *name
= NULL
;
218 ret
= debug_make_void_type (dhandle
);
223 ret
= debug_make_void_type (dhandle
);
228 ret
= debug_make_int_type (dhandle
, 1, false);
233 ret
= debug_make_int_type (dhandle
, 2, false);
238 /* FIXME: Perhaps the size should depend upon the architecture. */
239 ret
= debug_make_int_type (dhandle
, 4, false);
244 ret
= debug_make_int_type (dhandle
, 4, false);
249 ret
= debug_make_float_type (dhandle
, 4);
254 ret
= debug_make_float_type (dhandle
, 8);
259 ret
= debug_make_float_type (dhandle
, 12);
260 name
= "long double";
264 ret
= debug_make_int_type (dhandle
, 1, true);
265 name
= "unsigned char";
269 ret
= debug_make_int_type (dhandle
, 2, true);
270 name
= "unsigned short";
274 ret
= debug_make_int_type (dhandle
, 4, true);
275 name
= "unsigned int";
279 ret
= debug_make_int_type (dhandle
, 4, true);
280 name
= "unsigned long";
285 ret
= debug_make_struct_type (dhandle
, true, 0,
286 (debug_field
*) NULL
);
288 ret
= parse_coff_struct_type (abfd
, symbols
, types
, ntype
, pauxent
,
294 ret
= debug_make_struct_type (dhandle
, false, 0, (debug_field
*) NULL
);
296 ret
= parse_coff_struct_type (abfd
, symbols
, types
, ntype
, pauxent
,
302 ret
= debug_make_enum_type (dhandle
, (const char **) NULL
,
303 (bfd_signed_vma
*) NULL
);
305 ret
= parse_coff_enum_type (abfd
, symbols
, types
, pauxent
, dhandle
);
310 ret
= debug_name_type (dhandle
, name
, ret
);
312 debug_type
*slot
= coff_get_slot (dhandle
, types
, coff_symno
);
318 /* Parse a struct type. */
321 parse_coff_struct_type (bfd
*abfd
, struct coff_symbols
*symbols
,
322 struct coff_types
**types
, int ntype
,
323 union internal_auxent
*pauxent
, void *dhandle
)
327 debug_field
*fields
, *xfields
;
331 symend
= pauxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.u32
;
334 fields
= (debug_field
*) xmalloc (alloc
* sizeof *fields
);
339 && symbols
->coff_symno
< symend
340 && symbols
->symno
< symbols
->symcount
)
343 long this_coff_symno
;
344 struct internal_syment syment
;
345 union internal_auxent auxent
;
346 union internal_auxent
*psubaux
;
347 bfd_vma bitpos
= 0, bitsize
= 0;
349 sym
= symbols
->syms
[symbols
->symno
];
351 if (! bfd_coff_get_syment (abfd
, sym
, &syment
))
353 non_fatal (_("bfd_coff_get_syment failed: %s"),
354 bfd_errmsg (bfd_get_error ()));
356 return DEBUG_TYPE_NULL
;
359 this_coff_symno
= symbols
->coff_symno
;
362 symbols
->coff_symno
+= 1 + syment
.n_numaux
;
364 if (syment
.n_numaux
== 0)
368 if (! bfd_coff_get_auxent (abfd
, sym
, 0, &auxent
))
370 non_fatal (_("bfd_coff_get_auxent failed: %s"),
371 bfd_errmsg (bfd_get_error ()));
373 return DEBUG_TYPE_NULL
;
378 switch (syment
.n_sclass
)
382 bitpos
= 8 * bfd_asymbol_value (sym
);
387 bitpos
= bfd_asymbol_value (sym
);
389 bitsize
= psubaux
->x_sym
.x_misc
.x_lnsz
.x_size
;
402 ftype
= parse_coff_type (abfd
, symbols
, types
, this_coff_symno
,
403 syment
.n_type
, psubaux
, true, dhandle
);
404 f
= debug_make_field (dhandle
, bfd_asymbol_name (sym
), ftype
,
405 bitpos
, bitsize
, DEBUG_VISIBILITY_PUBLIC
);
406 if (f
== DEBUG_FIELD_NULL
)
409 return DEBUG_TYPE_NULL
;
412 if (count
+ 1 >= alloc
)
415 fields
= ((debug_field
*)
416 xrealloc (fields
, alloc
* sizeof *fields
));
424 fields
[count
] = DEBUG_FIELD_NULL
;
425 xfields
= debug_xalloc (dhandle
, (count
+ 1) * sizeof (*fields
));
426 memcpy (xfields
, fields
, (count
+ 1) * sizeof (*fields
));
429 return debug_make_struct_type (dhandle
, ntype
== T_STRUCT
,
430 pauxent
->x_sym
.x_misc
.x_lnsz
.x_size
,
434 /* Parse an enum type. */
437 parse_coff_enum_type (bfd
*abfd
, struct coff_symbols
*symbols
,
438 struct coff_types
**types ATTRIBUTE_UNUSED
,
439 union internal_auxent
*pauxent
, void *dhandle
)
443 const char **names
, **xnames
;
444 bfd_signed_vma
*vals
, *xvals
;
448 symend
= pauxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.u32
;
451 names
= (const char **) xmalloc (alloc
* sizeof *names
);
452 vals
= (bfd_signed_vma
*) xmalloc (alloc
* sizeof *vals
);
457 && symbols
->coff_symno
< symend
458 && symbols
->symno
< symbols
->symcount
)
461 struct internal_syment syment
;
463 sym
= symbols
->syms
[symbols
->symno
];
465 if (! bfd_coff_get_syment (abfd
, sym
, &syment
))
467 non_fatal (_("bfd_coff_get_syment failed: %s"),
468 bfd_errmsg (bfd_get_error ()));
471 return DEBUG_TYPE_NULL
;
475 symbols
->coff_symno
+= 1 + syment
.n_numaux
;
477 switch (syment
.n_sclass
)
480 if (count
+ 1 >= alloc
)
483 names
= ((const char **)
484 xrealloc (names
, alloc
* sizeof *names
));
485 vals
= ((bfd_signed_vma
*)
486 xrealloc (vals
, alloc
* sizeof *vals
));
489 names
[count
] = bfd_asymbol_name (sym
);
490 vals
[count
] = bfd_asymbol_value (sym
);
502 xnames
= debug_xalloc (dhandle
, (count
+ 1) * sizeof (*names
));
503 memcpy (xnames
, names
, (count
+ 1) * sizeof (*names
));
505 xvals
= debug_xalloc (dhandle
, (count
+ 1) * sizeof (*vals
));
506 memcpy (xvals
, vals
, (count
+ 1) * sizeof (*vals
));
509 return debug_make_enum_type (dhandle
, xnames
, xvals
);
512 /* Handle a single COFF symbol. */
515 parse_coff_symbol (bfd
*abfd ATTRIBUTE_UNUSED
, struct coff_types
**types
,
516 asymbol
*sym
, long coff_symno
,
517 struct internal_syment
*psyment
, void *dhandle
,
518 debug_type type
, bool within_function
)
520 switch (psyment
->n_sclass
)
526 if (! debug_record_variable (dhandle
, bfd_asymbol_name (sym
), type
,
527 DEBUG_LOCAL
, bfd_asymbol_value (sym
)))
533 if (! debug_record_variable (dhandle
, bfd_asymbol_name (sym
), type
,
534 DEBUG_GLOBAL
, bfd_asymbol_value (sym
)))
539 if (! debug_record_variable (dhandle
, bfd_asymbol_name (sym
), type
,
543 bfd_asymbol_value (sym
)))
548 /* FIXME: We may need to convert the register number. */
549 if (! debug_record_variable (dhandle
, bfd_asymbol_name (sym
), type
,
550 DEBUG_REGISTER
, bfd_asymbol_value (sym
)))
558 if (! debug_record_parameter (dhandle
, bfd_asymbol_name (sym
), type
,
559 DEBUG_PARM_STACK
, bfd_asymbol_value (sym
)))
564 /* FIXME: We may need to convert the register number. */
565 if (! debug_record_parameter (dhandle
, bfd_asymbol_name (sym
), type
,
566 DEBUG_PARM_REG
, bfd_asymbol_value (sym
)))
571 type
= debug_name_type (dhandle
, bfd_asymbol_name (sym
), type
);
572 if (type
== DEBUG_TYPE_NULL
)
582 type
= debug_tag_type (dhandle
, bfd_asymbol_name (sym
), type
);
583 if (type
== DEBUG_TYPE_NULL
)
586 /* Store the named type into the slot, so that references get
588 slot
= coff_get_slot (dhandle
, types
, coff_symno
);
600 /* Determine if a symbol has external visibility. */
603 external_coff_symbol_p (int sym_class
)
616 /* This is the main routine. It looks through all the symbols and
620 parse_coff (bfd
*abfd
, asymbol
**syms
, long symcount
, void *dhandle
)
622 struct coff_symbols symbols
;
623 struct coff_types
*types
;
630 bool within_function
;
631 long this_coff_symno
;
634 symbols
.symcount
= symcount
;
636 symbols
.coff_symno
= 0;
646 within_function
= false;
648 while (symbols
.symno
< symcount
)
652 struct internal_syment syment
;
653 union internal_auxent auxent
;
654 union internal_auxent
*paux
;
657 sym
= syms
[symbols
.symno
];
659 if (! bfd_coff_get_syment (abfd
, sym
, &syment
))
661 non_fatal (_("bfd_coff_get_syment failed: %s"),
662 bfd_errmsg (bfd_get_error ()));
666 name
= bfd_asymbol_name (sym
);
668 this_coff_symno
= symbols
.coff_symno
;
671 symbols
.coff_symno
+= 1 + syment
.n_numaux
;
673 /* We only worry about the first auxent, because that is the
674 only one which is relevant for debugging information. */
675 if (syment
.n_numaux
== 0)
679 if (! bfd_coff_get_auxent (abfd
, sym
, 0, &auxent
))
681 non_fatal (_("bfd_coff_get_auxent failed: %s"),
682 bfd_errmsg (bfd_get_error ()));
688 if (this_coff_symno
== next_c_file
&& syment
.n_sclass
!= C_FILE
)
690 /* The last C_FILE symbol points to the first external
692 if (! debug_set_filename (dhandle
, "*globals*"))
696 switch (syment
.n_sclass
)
705 /* Just ignore these classes. */
709 next_c_file
= syment
.n_value
;
710 if (! debug_set_filename (dhandle
, name
))
715 /* Ignore static symbols with a type of T_NULL. These
716 represent section entries. */
717 if (syment
.n_type
== T_NULL
)
722 if (ISFCN (syment
.n_type
))
725 fnclass
= syment
.n_sclass
;
726 fntype
= syment
.n_type
;
727 if (syment
.n_numaux
> 0)
728 fnend
= bfd_asymbol_value (sym
) + auxent
.x_sym
.x_misc
.x_fsize
;
731 linenos
= BFD_SEND (abfd
, _get_lineno
, (abfd
, sym
));
734 type
= parse_coff_type (abfd
, &symbols
, &types
, this_coff_symno
,
735 syment
.n_type
, paux
, true, dhandle
);
736 if (type
== DEBUG_TYPE_NULL
)
738 if (! parse_coff_symbol (abfd
, &types
, sym
, this_coff_symno
, &syment
,
739 dhandle
, type
, within_function
))
744 if (strcmp (name
, ".bf") == 0)
748 non_fatal (_("%ld: .bf without preceding function"),
753 type
= parse_coff_type (abfd
, &symbols
, &types
, this_coff_symno
,
754 DECREF (fntype
), paux
, false, dhandle
);
755 if (type
== DEBUG_TYPE_NULL
)
758 if (! debug_record_function (dhandle
, fnname
, type
,
759 external_coff_symbol_p (fnclass
),
760 bfd_asymbol_value (sym
)))
768 if (syment
.n_numaux
== 0)
771 base
= auxent
.x_sym
.x_misc
.x_lnsz
.x_lnno
- 1;
773 addr
= bfd_section_vma (bfd_asymbol_section (sym
));
777 while (linenos
->line_number
!= 0)
779 if (! debug_record_line (dhandle
,
780 linenos
->line_number
+ base
,
781 linenos
->u
.offset
+ addr
))
792 within_function
= true;
794 else if (strcmp (name
, ".ef") == 0)
796 if (! within_function
)
798 non_fatal (_("%ld: unexpected .ef\n"), this_coff_symno
);
802 if (bfd_asymbol_value (sym
) > fnend
)
803 fnend
= bfd_asymbol_value (sym
);
804 if (! debug_end_function (dhandle
, fnend
))
808 within_function
= false;
813 if (strcmp (name
, ".bb") == 0)
815 if (! debug_start_block (dhandle
, bfd_asymbol_value (sym
)))
818 else if (strcmp (name
, ".eb") == 0)
820 if (! debug_end_block (dhandle
, bfd_asymbol_value (sym
)))
826 type
= parse_coff_type (abfd
, &symbols
, &types
, this_coff_symno
,
827 syment
.n_type
, paux
, true, dhandle
);
828 if (type
== DEBUG_TYPE_NULL
)
830 if (! parse_coff_symbol (abfd
, &types
, sym
, this_coff_symno
, &syment
,
831 dhandle
, type
, within_function
))