1*3d8817e4Smiod /* stabs.c -- Parse COFF debugging information
2*3d8817e4Smiod Copyright 1996, 1999, 2000, 2002, 2003 Free Software Foundation, Inc.
3*3d8817e4Smiod Written by Ian Lance Taylor <ian@cygnus.com>.
4*3d8817e4Smiod
5*3d8817e4Smiod This file is part of GNU Binutils.
6*3d8817e4Smiod
7*3d8817e4Smiod This program is free software; you can redistribute it and/or modify
8*3d8817e4Smiod it under the terms of the GNU General Public License as published by
9*3d8817e4Smiod the Free Software Foundation; either version 2 of the License, or
10*3d8817e4Smiod (at your option) any later version.
11*3d8817e4Smiod
12*3d8817e4Smiod This program is distributed in the hope that it will be useful,
13*3d8817e4Smiod but WITHOUT ANY WARRANTY; without even the implied warranty of
14*3d8817e4Smiod MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15*3d8817e4Smiod GNU General Public License for more details.
16*3d8817e4Smiod
17*3d8817e4Smiod You should have received a copy of the GNU General Public License
18*3d8817e4Smiod along with this program; if not, write to the Free Software
19*3d8817e4Smiod Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
20*3d8817e4Smiod 02110-1301, USA. */
21*3d8817e4Smiod
22*3d8817e4Smiod /* This file contains code which parses COFF debugging information. */
23*3d8817e4Smiod
24*3d8817e4Smiod #include "bfd.h"
25*3d8817e4Smiod #include "coff/internal.h"
26*3d8817e4Smiod #include "bucomm.h"
27*3d8817e4Smiod #include "libiberty.h"
28*3d8817e4Smiod #include "debug.h"
29*3d8817e4Smiod #include "budbg.h"
30*3d8817e4Smiod
31*3d8817e4Smiod /* FIXME: We should not need this BFD internal file. We need it for
32*3d8817e4Smiod the N_BTMASK, etc., values. */
33*3d8817e4Smiod #include "libcoff.h"
34*3d8817e4Smiod
35*3d8817e4Smiod /* These macros extract the right mask and shifts for this BFD. They
36*3d8817e4Smiod assume that there is a local variable named ABFD. This is so that
37*3d8817e4Smiod macros like ISFCN and DECREF, from coff/internal.h, will work
38*3d8817e4Smiod without modification. */
39*3d8817e4Smiod #define N_BTMASK (coff_data (abfd)->local_n_btmask)
40*3d8817e4Smiod #define N_BTSHFT (coff_data (abfd)->local_n_btshft)
41*3d8817e4Smiod #define N_TMASK (coff_data (abfd)->local_n_tmask)
42*3d8817e4Smiod #define N_TSHIFT (coff_data (abfd)->local_n_tshift)
43*3d8817e4Smiod
44*3d8817e4Smiod /* This structure is used to hold the symbols, as well as the current
45*3d8817e4Smiod location within the symbols. */
46*3d8817e4Smiod
47*3d8817e4Smiod struct coff_symbols
48*3d8817e4Smiod {
49*3d8817e4Smiod /* The symbols. */
50*3d8817e4Smiod asymbol **syms;
51*3d8817e4Smiod /* The number of symbols. */
52*3d8817e4Smiod long symcount;
53*3d8817e4Smiod /* The index of the current symbol. */
54*3d8817e4Smiod long symno;
55*3d8817e4Smiod /* The index of the current symbol in the COFF symbol table (where
56*3d8817e4Smiod each auxent counts as a symbol). */
57*3d8817e4Smiod long coff_symno;
58*3d8817e4Smiod };
59*3d8817e4Smiod
60*3d8817e4Smiod /* The largest basic type we are prepared to handle. */
61*3d8817e4Smiod
62*3d8817e4Smiod #define T_MAX (T_LNGDBL)
63*3d8817e4Smiod
64*3d8817e4Smiod /* This structure is used to hold slots. */
65*3d8817e4Smiod
66*3d8817e4Smiod struct coff_slots
67*3d8817e4Smiod {
68*3d8817e4Smiod /* Next set of slots. */
69*3d8817e4Smiod struct coff_slots *next;
70*3d8817e4Smiod /* Slots. */
71*3d8817e4Smiod #define COFF_SLOTS (16)
72*3d8817e4Smiod debug_type slots[COFF_SLOTS];
73*3d8817e4Smiod };
74*3d8817e4Smiod
75*3d8817e4Smiod /* This structure is used to map symbol indices to types. */
76*3d8817e4Smiod
77*3d8817e4Smiod struct coff_types
78*3d8817e4Smiod {
79*3d8817e4Smiod /* Slots. */
80*3d8817e4Smiod struct coff_slots *slots;
81*3d8817e4Smiod /* Basic types. */
82*3d8817e4Smiod debug_type basic[T_MAX + 1];
83*3d8817e4Smiod };
84*3d8817e4Smiod
85*3d8817e4Smiod static debug_type *coff_get_slot (struct coff_types *, int);
86*3d8817e4Smiod static debug_type parse_coff_type
87*3d8817e4Smiod (bfd *, struct coff_symbols *, struct coff_types *, long, int,
88*3d8817e4Smiod union internal_auxent *, bfd_boolean, void *);
89*3d8817e4Smiod static debug_type parse_coff_base_type
90*3d8817e4Smiod (bfd *, struct coff_symbols *, struct coff_types *, long, int,
91*3d8817e4Smiod union internal_auxent *, void *);
92*3d8817e4Smiod static debug_type parse_coff_struct_type
93*3d8817e4Smiod (bfd *, struct coff_symbols *, struct coff_types *, int,
94*3d8817e4Smiod union internal_auxent *, void *);
95*3d8817e4Smiod static debug_type parse_coff_enum_type
96*3d8817e4Smiod (bfd *, struct coff_symbols *, struct coff_types *,
97*3d8817e4Smiod union internal_auxent *, void *);
98*3d8817e4Smiod static bfd_boolean parse_coff_symbol
99*3d8817e4Smiod (bfd *, struct coff_types *, asymbol *, long, struct internal_syment *,
100*3d8817e4Smiod void *, debug_type, bfd_boolean);
101*3d8817e4Smiod static bfd_boolean external_coff_symbol_p (int sym_class);
102*3d8817e4Smiod
103*3d8817e4Smiod /* Return the slot for a type. */
104*3d8817e4Smiod
105*3d8817e4Smiod static debug_type *
coff_get_slot(struct coff_types * types,int indx)106*3d8817e4Smiod coff_get_slot (struct coff_types *types, int indx)
107*3d8817e4Smiod {
108*3d8817e4Smiod struct coff_slots **pps;
109*3d8817e4Smiod
110*3d8817e4Smiod pps = &types->slots;
111*3d8817e4Smiod
112*3d8817e4Smiod while (indx >= COFF_SLOTS)
113*3d8817e4Smiod {
114*3d8817e4Smiod if (*pps == NULL)
115*3d8817e4Smiod {
116*3d8817e4Smiod *pps = (struct coff_slots *) xmalloc (sizeof **pps);
117*3d8817e4Smiod memset (*pps, 0, sizeof **pps);
118*3d8817e4Smiod }
119*3d8817e4Smiod pps = &(*pps)->next;
120*3d8817e4Smiod indx -= COFF_SLOTS;
121*3d8817e4Smiod }
122*3d8817e4Smiod
123*3d8817e4Smiod if (*pps == NULL)
124*3d8817e4Smiod {
125*3d8817e4Smiod *pps = (struct coff_slots *) xmalloc (sizeof **pps);
126*3d8817e4Smiod memset (*pps, 0, sizeof **pps);
127*3d8817e4Smiod }
128*3d8817e4Smiod
129*3d8817e4Smiod return (*pps)->slots + indx;
130*3d8817e4Smiod }
131*3d8817e4Smiod
132*3d8817e4Smiod /* Parse a COFF type code in NTYPE. */
133*3d8817e4Smiod
134*3d8817e4Smiod static debug_type
parse_coff_type(bfd * abfd,struct coff_symbols * symbols,struct coff_types * types,long coff_symno,int ntype,union internal_auxent * pauxent,bfd_boolean useaux,void * dhandle)135*3d8817e4Smiod parse_coff_type (bfd *abfd, struct coff_symbols *symbols,
136*3d8817e4Smiod struct coff_types *types, long coff_symno, int ntype,
137*3d8817e4Smiod union internal_auxent *pauxent, bfd_boolean useaux,
138*3d8817e4Smiod void *dhandle)
139*3d8817e4Smiod {
140*3d8817e4Smiod debug_type type;
141*3d8817e4Smiod
142*3d8817e4Smiod if ((ntype & ~N_BTMASK) != 0)
143*3d8817e4Smiod {
144*3d8817e4Smiod int newtype;
145*3d8817e4Smiod
146*3d8817e4Smiod newtype = DECREF (ntype);
147*3d8817e4Smiod
148*3d8817e4Smiod if (ISPTR (ntype))
149*3d8817e4Smiod {
150*3d8817e4Smiod type = parse_coff_type (abfd, symbols, types, coff_symno, newtype,
151*3d8817e4Smiod pauxent, useaux, dhandle);
152*3d8817e4Smiod type = debug_make_pointer_type (dhandle, type);
153*3d8817e4Smiod }
154*3d8817e4Smiod else if (ISFCN (ntype))
155*3d8817e4Smiod {
156*3d8817e4Smiod type = parse_coff_type (abfd, symbols, types, coff_symno, newtype,
157*3d8817e4Smiod pauxent, useaux, dhandle);
158*3d8817e4Smiod type = debug_make_function_type (dhandle, type, (debug_type *) NULL,
159*3d8817e4Smiod FALSE);
160*3d8817e4Smiod }
161*3d8817e4Smiod else if (ISARY (ntype))
162*3d8817e4Smiod {
163*3d8817e4Smiod int n;
164*3d8817e4Smiod
165*3d8817e4Smiod if (pauxent == NULL)
166*3d8817e4Smiod n = 0;
167*3d8817e4Smiod else
168*3d8817e4Smiod {
169*3d8817e4Smiod unsigned short *dim;
170*3d8817e4Smiod int i;
171*3d8817e4Smiod
172*3d8817e4Smiod /* FIXME: If pauxent->x_sym.x_tagndx.l == 0, gdb sets
173*3d8817e4Smiod the c_naux field of the syment to 0. */
174*3d8817e4Smiod
175*3d8817e4Smiod /* Move the dimensions down, so that the next array
176*3d8817e4Smiod picks up the next one. */
177*3d8817e4Smiod dim = pauxent->x_sym.x_fcnary.x_ary.x_dimen;
178*3d8817e4Smiod n = dim[0];
179*3d8817e4Smiod for (i = 0; *dim != 0 && i < DIMNUM - 1; i++, dim++)
180*3d8817e4Smiod *dim = *(dim + 1);
181*3d8817e4Smiod *dim = 0;
182*3d8817e4Smiod }
183*3d8817e4Smiod
184*3d8817e4Smiod type = parse_coff_type (abfd, symbols, types, coff_symno, newtype,
185*3d8817e4Smiod pauxent, FALSE, dhandle);
186*3d8817e4Smiod type = debug_make_array_type (dhandle, type,
187*3d8817e4Smiod parse_coff_base_type (abfd, symbols,
188*3d8817e4Smiod types,
189*3d8817e4Smiod coff_symno,
190*3d8817e4Smiod T_INT,
191*3d8817e4Smiod NULL, dhandle),
192*3d8817e4Smiod 0, n - 1, FALSE);
193*3d8817e4Smiod }
194*3d8817e4Smiod else
195*3d8817e4Smiod {
196*3d8817e4Smiod non_fatal (_("parse_coff_type: Bad type code 0x%x"), ntype);
197*3d8817e4Smiod return DEBUG_TYPE_NULL;
198*3d8817e4Smiod }
199*3d8817e4Smiod
200*3d8817e4Smiod return type;
201*3d8817e4Smiod }
202*3d8817e4Smiod
203*3d8817e4Smiod if (pauxent != NULL && pauxent->x_sym.x_tagndx.l > 0)
204*3d8817e4Smiod {
205*3d8817e4Smiod debug_type *slot;
206*3d8817e4Smiod
207*3d8817e4Smiod /* This is a reference to an existing type. FIXME: gdb checks
208*3d8817e4Smiod that the class is not C_STRTAG, nor C_UNTAG, nor C_ENTAG. */
209*3d8817e4Smiod slot = coff_get_slot (types, pauxent->x_sym.x_tagndx.l);
210*3d8817e4Smiod if (*slot != DEBUG_TYPE_NULL)
211*3d8817e4Smiod return *slot;
212*3d8817e4Smiod else
213*3d8817e4Smiod return debug_make_indirect_type (dhandle, slot, (const char *) NULL);
214*3d8817e4Smiod }
215*3d8817e4Smiod
216*3d8817e4Smiod /* If the aux entry has already been used for something, useaux will
217*3d8817e4Smiod have been set to false, indicating that parse_coff_base_type
218*3d8817e4Smiod should not use it. We need to do it this way, rather than simply
219*3d8817e4Smiod passing pauxent as NULL, because we need to be able handle
220*3d8817e4Smiod multiple array dimensions while still discarding pauxent after
221*3d8817e4Smiod having handled all of them. */
222*3d8817e4Smiod if (! useaux)
223*3d8817e4Smiod pauxent = NULL;
224*3d8817e4Smiod
225*3d8817e4Smiod return parse_coff_base_type (abfd, symbols, types, coff_symno, ntype,
226*3d8817e4Smiod pauxent, dhandle);
227*3d8817e4Smiod }
228*3d8817e4Smiod
229*3d8817e4Smiod /* Parse a basic COFF type in NTYPE. */
230*3d8817e4Smiod
231*3d8817e4Smiod static debug_type
parse_coff_base_type(bfd * abfd,struct coff_symbols * symbols,struct coff_types * types,long coff_symno,int ntype,union internal_auxent * pauxent,void * dhandle)232*3d8817e4Smiod parse_coff_base_type (bfd *abfd, struct coff_symbols *symbols,
233*3d8817e4Smiod struct coff_types *types, long coff_symno, int ntype,
234*3d8817e4Smiod union internal_auxent *pauxent, void *dhandle)
235*3d8817e4Smiod {
236*3d8817e4Smiod debug_type ret;
237*3d8817e4Smiod bfd_boolean set_basic;
238*3d8817e4Smiod const char *name;
239*3d8817e4Smiod debug_type *slot;
240*3d8817e4Smiod
241*3d8817e4Smiod if (ntype >= 0
242*3d8817e4Smiod && ntype <= T_MAX
243*3d8817e4Smiod && types->basic[ntype] != DEBUG_TYPE_NULL)
244*3d8817e4Smiod return types->basic[ntype];
245*3d8817e4Smiod
246*3d8817e4Smiod set_basic = TRUE;
247*3d8817e4Smiod name = NULL;
248*3d8817e4Smiod
249*3d8817e4Smiod switch (ntype)
250*3d8817e4Smiod {
251*3d8817e4Smiod default:
252*3d8817e4Smiod ret = debug_make_void_type (dhandle);
253*3d8817e4Smiod break;
254*3d8817e4Smiod
255*3d8817e4Smiod case T_NULL:
256*3d8817e4Smiod case T_VOID:
257*3d8817e4Smiod ret = debug_make_void_type (dhandle);
258*3d8817e4Smiod name = "void";
259*3d8817e4Smiod break;
260*3d8817e4Smiod
261*3d8817e4Smiod case T_CHAR:
262*3d8817e4Smiod ret = debug_make_int_type (dhandle, 1, FALSE);
263*3d8817e4Smiod name = "char";
264*3d8817e4Smiod break;
265*3d8817e4Smiod
266*3d8817e4Smiod case T_SHORT:
267*3d8817e4Smiod ret = debug_make_int_type (dhandle, 2, FALSE);
268*3d8817e4Smiod name = "short";
269*3d8817e4Smiod break;
270*3d8817e4Smiod
271*3d8817e4Smiod case T_INT:
272*3d8817e4Smiod /* FIXME: Perhaps the size should depend upon the architecture. */
273*3d8817e4Smiod ret = debug_make_int_type (dhandle, 4, FALSE);
274*3d8817e4Smiod name = "int";
275*3d8817e4Smiod break;
276*3d8817e4Smiod
277*3d8817e4Smiod case T_LONG:
278*3d8817e4Smiod ret = debug_make_int_type (dhandle, 4, FALSE);
279*3d8817e4Smiod name = "long";
280*3d8817e4Smiod break;
281*3d8817e4Smiod
282*3d8817e4Smiod case T_FLOAT:
283*3d8817e4Smiod ret = debug_make_float_type (dhandle, 4);
284*3d8817e4Smiod name = "float";
285*3d8817e4Smiod break;
286*3d8817e4Smiod
287*3d8817e4Smiod case T_DOUBLE:
288*3d8817e4Smiod ret = debug_make_float_type (dhandle, 8);
289*3d8817e4Smiod name = "double";
290*3d8817e4Smiod break;
291*3d8817e4Smiod
292*3d8817e4Smiod case T_LNGDBL:
293*3d8817e4Smiod ret = debug_make_float_type (dhandle, 12);
294*3d8817e4Smiod name = "long double";
295*3d8817e4Smiod break;
296*3d8817e4Smiod
297*3d8817e4Smiod case T_UCHAR:
298*3d8817e4Smiod ret = debug_make_int_type (dhandle, 1, TRUE);
299*3d8817e4Smiod name = "unsigned char";
300*3d8817e4Smiod break;
301*3d8817e4Smiod
302*3d8817e4Smiod case T_USHORT:
303*3d8817e4Smiod ret = debug_make_int_type (dhandle, 2, TRUE);
304*3d8817e4Smiod name = "unsigned short";
305*3d8817e4Smiod break;
306*3d8817e4Smiod
307*3d8817e4Smiod case T_UINT:
308*3d8817e4Smiod ret = debug_make_int_type (dhandle, 4, TRUE);
309*3d8817e4Smiod name = "unsigned int";
310*3d8817e4Smiod break;
311*3d8817e4Smiod
312*3d8817e4Smiod case T_ULONG:
313*3d8817e4Smiod ret = debug_make_int_type (dhandle, 4, TRUE);
314*3d8817e4Smiod name = "unsigned long";
315*3d8817e4Smiod break;
316*3d8817e4Smiod
317*3d8817e4Smiod case T_STRUCT:
318*3d8817e4Smiod if (pauxent == NULL)
319*3d8817e4Smiod ret = debug_make_struct_type (dhandle, TRUE, 0,
320*3d8817e4Smiod (debug_field *) NULL);
321*3d8817e4Smiod else
322*3d8817e4Smiod ret = parse_coff_struct_type (abfd, symbols, types, ntype, pauxent,
323*3d8817e4Smiod dhandle);
324*3d8817e4Smiod
325*3d8817e4Smiod slot = coff_get_slot (types, coff_symno);
326*3d8817e4Smiod *slot = ret;
327*3d8817e4Smiod
328*3d8817e4Smiod set_basic = FALSE;
329*3d8817e4Smiod break;
330*3d8817e4Smiod
331*3d8817e4Smiod case T_UNION:
332*3d8817e4Smiod if (pauxent == NULL)
333*3d8817e4Smiod ret = debug_make_struct_type (dhandle, FALSE, 0, (debug_field *) NULL);
334*3d8817e4Smiod else
335*3d8817e4Smiod ret = parse_coff_struct_type (abfd, symbols, types, ntype, pauxent,
336*3d8817e4Smiod dhandle);
337*3d8817e4Smiod
338*3d8817e4Smiod slot = coff_get_slot (types, coff_symno);
339*3d8817e4Smiod *slot = ret;
340*3d8817e4Smiod
341*3d8817e4Smiod set_basic = FALSE;
342*3d8817e4Smiod break;
343*3d8817e4Smiod
344*3d8817e4Smiod case T_ENUM:
345*3d8817e4Smiod if (pauxent == NULL)
346*3d8817e4Smiod ret = debug_make_enum_type (dhandle, (const char **) NULL,
347*3d8817e4Smiod (bfd_signed_vma *) NULL);
348*3d8817e4Smiod else
349*3d8817e4Smiod ret = parse_coff_enum_type (abfd, symbols, types, pauxent, dhandle);
350*3d8817e4Smiod
351*3d8817e4Smiod slot = coff_get_slot (types, coff_symno);
352*3d8817e4Smiod *slot = ret;
353*3d8817e4Smiod
354*3d8817e4Smiod set_basic = FALSE;
355*3d8817e4Smiod break;
356*3d8817e4Smiod }
357*3d8817e4Smiod
358*3d8817e4Smiod if (name != NULL)
359*3d8817e4Smiod ret = debug_name_type (dhandle, name, ret);
360*3d8817e4Smiod
361*3d8817e4Smiod if (set_basic
362*3d8817e4Smiod && ntype >= 0
363*3d8817e4Smiod && ntype <= T_MAX)
364*3d8817e4Smiod types->basic[ntype] = ret;
365*3d8817e4Smiod
366*3d8817e4Smiod return ret;
367*3d8817e4Smiod }
368*3d8817e4Smiod
369*3d8817e4Smiod /* Parse a struct type. */
370*3d8817e4Smiod
371*3d8817e4Smiod static debug_type
parse_coff_struct_type(bfd * abfd,struct coff_symbols * symbols,struct coff_types * types,int ntype,union internal_auxent * pauxent,void * dhandle)372*3d8817e4Smiod parse_coff_struct_type (bfd *abfd, struct coff_symbols *symbols,
373*3d8817e4Smiod struct coff_types *types, int ntype,
374*3d8817e4Smiod union internal_auxent *pauxent, void *dhandle)
375*3d8817e4Smiod {
376*3d8817e4Smiod long symend;
377*3d8817e4Smiod int alloc;
378*3d8817e4Smiod debug_field *fields;
379*3d8817e4Smiod int count;
380*3d8817e4Smiod bfd_boolean done;
381*3d8817e4Smiod
382*3d8817e4Smiod symend = pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l;
383*3d8817e4Smiod
384*3d8817e4Smiod alloc = 10;
385*3d8817e4Smiod fields = (debug_field *) xmalloc (alloc * sizeof *fields);
386*3d8817e4Smiod count = 0;
387*3d8817e4Smiod
388*3d8817e4Smiod done = FALSE;
389*3d8817e4Smiod while (! done
390*3d8817e4Smiod && symbols->coff_symno < symend
391*3d8817e4Smiod && symbols->symno < symbols->symcount)
392*3d8817e4Smiod {
393*3d8817e4Smiod asymbol *sym;
394*3d8817e4Smiod long this_coff_symno;
395*3d8817e4Smiod struct internal_syment syment;
396*3d8817e4Smiod union internal_auxent auxent;
397*3d8817e4Smiod union internal_auxent *psubaux;
398*3d8817e4Smiod bfd_vma bitpos = 0, bitsize = 0;
399*3d8817e4Smiod
400*3d8817e4Smiod sym = symbols->syms[symbols->symno];
401*3d8817e4Smiod
402*3d8817e4Smiod if (! bfd_coff_get_syment (abfd, sym, &syment))
403*3d8817e4Smiod {
404*3d8817e4Smiod non_fatal (_("bfd_coff_get_syment failed: %s"),
405*3d8817e4Smiod bfd_errmsg (bfd_get_error ()));
406*3d8817e4Smiod return DEBUG_TYPE_NULL;
407*3d8817e4Smiod }
408*3d8817e4Smiod
409*3d8817e4Smiod this_coff_symno = symbols->coff_symno;
410*3d8817e4Smiod
411*3d8817e4Smiod ++symbols->symno;
412*3d8817e4Smiod symbols->coff_symno += 1 + syment.n_numaux;
413*3d8817e4Smiod
414*3d8817e4Smiod if (syment.n_numaux == 0)
415*3d8817e4Smiod psubaux = NULL;
416*3d8817e4Smiod else
417*3d8817e4Smiod {
418*3d8817e4Smiod if (! bfd_coff_get_auxent (abfd, sym, 0, &auxent))
419*3d8817e4Smiod {
420*3d8817e4Smiod non_fatal (_("bfd_coff_get_auxent failed: %s"),
421*3d8817e4Smiod bfd_errmsg (bfd_get_error ()));
422*3d8817e4Smiod return DEBUG_TYPE_NULL;
423*3d8817e4Smiod }
424*3d8817e4Smiod psubaux = &auxent;
425*3d8817e4Smiod }
426*3d8817e4Smiod
427*3d8817e4Smiod switch (syment.n_sclass)
428*3d8817e4Smiod {
429*3d8817e4Smiod case C_MOS:
430*3d8817e4Smiod case C_MOU:
431*3d8817e4Smiod bitpos = 8 * bfd_asymbol_value (sym);
432*3d8817e4Smiod bitsize = 0;
433*3d8817e4Smiod break;
434*3d8817e4Smiod
435*3d8817e4Smiod case C_FIELD:
436*3d8817e4Smiod bitpos = bfd_asymbol_value (sym);
437*3d8817e4Smiod bitsize = auxent.x_sym.x_misc.x_lnsz.x_size;
438*3d8817e4Smiod break;
439*3d8817e4Smiod
440*3d8817e4Smiod case C_EOS:
441*3d8817e4Smiod done = TRUE;
442*3d8817e4Smiod break;
443*3d8817e4Smiod }
444*3d8817e4Smiod
445*3d8817e4Smiod if (! done)
446*3d8817e4Smiod {
447*3d8817e4Smiod debug_type ftype;
448*3d8817e4Smiod debug_field f;
449*3d8817e4Smiod
450*3d8817e4Smiod ftype = parse_coff_type (abfd, symbols, types, this_coff_symno,
451*3d8817e4Smiod syment.n_type, psubaux, TRUE, dhandle);
452*3d8817e4Smiod f = debug_make_field (dhandle, bfd_asymbol_name (sym), ftype,
453*3d8817e4Smiod bitpos, bitsize, DEBUG_VISIBILITY_PUBLIC);
454*3d8817e4Smiod if (f == DEBUG_FIELD_NULL)
455*3d8817e4Smiod return DEBUG_TYPE_NULL;
456*3d8817e4Smiod
457*3d8817e4Smiod if (count + 1 >= alloc)
458*3d8817e4Smiod {
459*3d8817e4Smiod alloc += 10;
460*3d8817e4Smiod fields = ((debug_field *)
461*3d8817e4Smiod xrealloc (fields, alloc * sizeof *fields));
462*3d8817e4Smiod }
463*3d8817e4Smiod
464*3d8817e4Smiod fields[count] = f;
465*3d8817e4Smiod ++count;
466*3d8817e4Smiod }
467*3d8817e4Smiod }
468*3d8817e4Smiod
469*3d8817e4Smiod fields[count] = DEBUG_FIELD_NULL;
470*3d8817e4Smiod
471*3d8817e4Smiod return debug_make_struct_type (dhandle, ntype == T_STRUCT,
472*3d8817e4Smiod pauxent->x_sym.x_misc.x_lnsz.x_size,
473*3d8817e4Smiod fields);
474*3d8817e4Smiod }
475*3d8817e4Smiod
476*3d8817e4Smiod /* Parse an enum type. */
477*3d8817e4Smiod
478*3d8817e4Smiod static debug_type
parse_coff_enum_type(bfd * abfd,struct coff_symbols * symbols,struct coff_types * types ATTRIBUTE_UNUSED,union internal_auxent * pauxent,void * dhandle)479*3d8817e4Smiod parse_coff_enum_type (bfd *abfd, struct coff_symbols *symbols,
480*3d8817e4Smiod struct coff_types *types ATTRIBUTE_UNUSED,
481*3d8817e4Smiod union internal_auxent *pauxent, void *dhandle)
482*3d8817e4Smiod {
483*3d8817e4Smiod long symend;
484*3d8817e4Smiod int alloc;
485*3d8817e4Smiod const char **names;
486*3d8817e4Smiod bfd_signed_vma *vals;
487*3d8817e4Smiod int count;
488*3d8817e4Smiod bfd_boolean done;
489*3d8817e4Smiod
490*3d8817e4Smiod symend = pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l;
491*3d8817e4Smiod
492*3d8817e4Smiod alloc = 10;
493*3d8817e4Smiod names = (const char **) xmalloc (alloc * sizeof *names);
494*3d8817e4Smiod vals = (bfd_signed_vma *) xmalloc (alloc * sizeof *vals);
495*3d8817e4Smiod count = 0;
496*3d8817e4Smiod
497*3d8817e4Smiod done = FALSE;
498*3d8817e4Smiod while (! done
499*3d8817e4Smiod && symbols->coff_symno < symend
500*3d8817e4Smiod && symbols->symno < symbols->symcount)
501*3d8817e4Smiod {
502*3d8817e4Smiod asymbol *sym;
503*3d8817e4Smiod struct internal_syment syment;
504*3d8817e4Smiod
505*3d8817e4Smiod sym = symbols->syms[symbols->symno];
506*3d8817e4Smiod
507*3d8817e4Smiod if (! bfd_coff_get_syment (abfd, sym, &syment))
508*3d8817e4Smiod {
509*3d8817e4Smiod non_fatal (_("bfd_coff_get_syment failed: %s"),
510*3d8817e4Smiod bfd_errmsg (bfd_get_error ()));
511*3d8817e4Smiod return DEBUG_TYPE_NULL;
512*3d8817e4Smiod }
513*3d8817e4Smiod
514*3d8817e4Smiod ++symbols->symno;
515*3d8817e4Smiod symbols->coff_symno += 1 + syment.n_numaux;
516*3d8817e4Smiod
517*3d8817e4Smiod switch (syment.n_sclass)
518*3d8817e4Smiod {
519*3d8817e4Smiod case C_MOE:
520*3d8817e4Smiod if (count + 1 >= alloc)
521*3d8817e4Smiod {
522*3d8817e4Smiod alloc += 10;
523*3d8817e4Smiod names = ((const char **)
524*3d8817e4Smiod xrealloc (names, alloc * sizeof *names));
525*3d8817e4Smiod vals = ((bfd_signed_vma *)
526*3d8817e4Smiod xrealloc (vals, alloc * sizeof *vals));
527*3d8817e4Smiod }
528*3d8817e4Smiod
529*3d8817e4Smiod names[count] = bfd_asymbol_name (sym);
530*3d8817e4Smiod vals[count] = bfd_asymbol_value (sym);
531*3d8817e4Smiod ++count;
532*3d8817e4Smiod break;
533*3d8817e4Smiod
534*3d8817e4Smiod case C_EOS:
535*3d8817e4Smiod done = TRUE;
536*3d8817e4Smiod break;
537*3d8817e4Smiod }
538*3d8817e4Smiod }
539*3d8817e4Smiod
540*3d8817e4Smiod names[count] = NULL;
541*3d8817e4Smiod
542*3d8817e4Smiod return debug_make_enum_type (dhandle, names, vals);
543*3d8817e4Smiod }
544*3d8817e4Smiod
545*3d8817e4Smiod /* Handle a single COFF symbol. */
546*3d8817e4Smiod
547*3d8817e4Smiod static bfd_boolean
parse_coff_symbol(bfd * abfd ATTRIBUTE_UNUSED,struct coff_types * types,asymbol * sym,long coff_symno,struct internal_syment * psyment,void * dhandle,debug_type type,bfd_boolean within_function)548*3d8817e4Smiod parse_coff_symbol (bfd *abfd ATTRIBUTE_UNUSED, struct coff_types *types,
549*3d8817e4Smiod asymbol *sym, long coff_symno,
550*3d8817e4Smiod struct internal_syment *psyment, void *dhandle,
551*3d8817e4Smiod debug_type type, bfd_boolean within_function)
552*3d8817e4Smiod {
553*3d8817e4Smiod switch (psyment->n_sclass)
554*3d8817e4Smiod {
555*3d8817e4Smiod case C_NULL:
556*3d8817e4Smiod break;
557*3d8817e4Smiod
558*3d8817e4Smiod case C_AUTO:
559*3d8817e4Smiod if (! debug_record_variable (dhandle, bfd_asymbol_name (sym), type,
560*3d8817e4Smiod DEBUG_LOCAL, bfd_asymbol_value (sym)))
561*3d8817e4Smiod return FALSE;
562*3d8817e4Smiod break;
563*3d8817e4Smiod
564*3d8817e4Smiod case C_WEAKEXT:
565*3d8817e4Smiod case C_EXT:
566*3d8817e4Smiod if (! debug_record_variable (dhandle, bfd_asymbol_name (sym), type,
567*3d8817e4Smiod DEBUG_GLOBAL, bfd_asymbol_value (sym)))
568*3d8817e4Smiod return FALSE;
569*3d8817e4Smiod break;
570*3d8817e4Smiod
571*3d8817e4Smiod case C_STAT:
572*3d8817e4Smiod if (! debug_record_variable (dhandle, bfd_asymbol_name (sym), type,
573*3d8817e4Smiod (within_function
574*3d8817e4Smiod ? DEBUG_LOCAL_STATIC
575*3d8817e4Smiod : DEBUG_STATIC),
576*3d8817e4Smiod bfd_asymbol_value (sym)))
577*3d8817e4Smiod return FALSE;
578*3d8817e4Smiod break;
579*3d8817e4Smiod
580*3d8817e4Smiod case C_REG:
581*3d8817e4Smiod /* FIXME: We may need to convert the register number. */
582*3d8817e4Smiod if (! debug_record_variable (dhandle, bfd_asymbol_name (sym), type,
583*3d8817e4Smiod DEBUG_REGISTER, bfd_asymbol_value (sym)))
584*3d8817e4Smiod return FALSE;
585*3d8817e4Smiod break;
586*3d8817e4Smiod
587*3d8817e4Smiod case C_LABEL:
588*3d8817e4Smiod break;
589*3d8817e4Smiod
590*3d8817e4Smiod case C_ARG:
591*3d8817e4Smiod if (! debug_record_parameter (dhandle, bfd_asymbol_name (sym), type,
592*3d8817e4Smiod DEBUG_PARM_STACK, bfd_asymbol_value (sym)))
593*3d8817e4Smiod return FALSE;
594*3d8817e4Smiod break;
595*3d8817e4Smiod
596*3d8817e4Smiod case C_REGPARM:
597*3d8817e4Smiod /* FIXME: We may need to convert the register number. */
598*3d8817e4Smiod if (! debug_record_parameter (dhandle, bfd_asymbol_name (sym), type,
599*3d8817e4Smiod DEBUG_PARM_REG, bfd_asymbol_value (sym)))
600*3d8817e4Smiod return FALSE;
601*3d8817e4Smiod break;
602*3d8817e4Smiod
603*3d8817e4Smiod case C_TPDEF:
604*3d8817e4Smiod type = debug_name_type (dhandle, bfd_asymbol_name (sym), type);
605*3d8817e4Smiod if (type == DEBUG_TYPE_NULL)
606*3d8817e4Smiod return FALSE;
607*3d8817e4Smiod break;
608*3d8817e4Smiod
609*3d8817e4Smiod case C_STRTAG:
610*3d8817e4Smiod case C_UNTAG:
611*3d8817e4Smiod case C_ENTAG:
612*3d8817e4Smiod {
613*3d8817e4Smiod debug_type *slot;
614*3d8817e4Smiod
615*3d8817e4Smiod type = debug_tag_type (dhandle, bfd_asymbol_name (sym), type);
616*3d8817e4Smiod if (type == DEBUG_TYPE_NULL)
617*3d8817e4Smiod return FALSE;
618*3d8817e4Smiod
619*3d8817e4Smiod /* Store the named type into the slot, so that references get
620*3d8817e4Smiod the name. */
621*3d8817e4Smiod slot = coff_get_slot (types, coff_symno);
622*3d8817e4Smiod *slot = type;
623*3d8817e4Smiod }
624*3d8817e4Smiod break;
625*3d8817e4Smiod
626*3d8817e4Smiod default:
627*3d8817e4Smiod break;
628*3d8817e4Smiod }
629*3d8817e4Smiod
630*3d8817e4Smiod return TRUE;
631*3d8817e4Smiod }
632*3d8817e4Smiod
633*3d8817e4Smiod /* Determine if a symbol has external visibility. */
634*3d8817e4Smiod
635*3d8817e4Smiod static bfd_boolean
external_coff_symbol_p(int sym_class)636*3d8817e4Smiod external_coff_symbol_p (int sym_class)
637*3d8817e4Smiod {
638*3d8817e4Smiod switch (sym_class)
639*3d8817e4Smiod {
640*3d8817e4Smiod case C_EXT:
641*3d8817e4Smiod case C_WEAKEXT:
642*3d8817e4Smiod return TRUE;
643*3d8817e4Smiod default:
644*3d8817e4Smiod break;
645*3d8817e4Smiod }
646*3d8817e4Smiod return FALSE;
647*3d8817e4Smiod }
648*3d8817e4Smiod
649*3d8817e4Smiod /* This is the main routine. It looks through all the symbols and
650*3d8817e4Smiod handles them. */
651*3d8817e4Smiod
652*3d8817e4Smiod bfd_boolean
parse_coff(bfd * abfd,asymbol ** syms,long symcount,void * dhandle)653*3d8817e4Smiod parse_coff (bfd *abfd, asymbol **syms, long symcount, void *dhandle)
654*3d8817e4Smiod {
655*3d8817e4Smiod struct coff_symbols symbols;
656*3d8817e4Smiod struct coff_types types;
657*3d8817e4Smiod int i;
658*3d8817e4Smiod long next_c_file;
659*3d8817e4Smiod const char *fnname;
660*3d8817e4Smiod int fnclass;
661*3d8817e4Smiod int fntype;
662*3d8817e4Smiod bfd_vma fnend;
663*3d8817e4Smiod alent *linenos;
664*3d8817e4Smiod bfd_boolean within_function;
665*3d8817e4Smiod long this_coff_symno;
666*3d8817e4Smiod
667*3d8817e4Smiod symbols.syms = syms;
668*3d8817e4Smiod symbols.symcount = symcount;
669*3d8817e4Smiod symbols.symno = 0;
670*3d8817e4Smiod symbols.coff_symno = 0;
671*3d8817e4Smiod
672*3d8817e4Smiod types.slots = NULL;
673*3d8817e4Smiod for (i = 0; i <= T_MAX; i++)
674*3d8817e4Smiod types.basic[i] = DEBUG_TYPE_NULL;
675*3d8817e4Smiod
676*3d8817e4Smiod next_c_file = -1;
677*3d8817e4Smiod fnname = NULL;
678*3d8817e4Smiod fnclass = 0;
679*3d8817e4Smiod fntype = 0;
680*3d8817e4Smiod fnend = 0;
681*3d8817e4Smiod linenos = NULL;
682*3d8817e4Smiod within_function = FALSE;
683*3d8817e4Smiod
684*3d8817e4Smiod while (symbols.symno < symcount)
685*3d8817e4Smiod {
686*3d8817e4Smiod asymbol *sym;
687*3d8817e4Smiod const char *name;
688*3d8817e4Smiod struct internal_syment syment;
689*3d8817e4Smiod union internal_auxent auxent;
690*3d8817e4Smiod union internal_auxent *paux;
691*3d8817e4Smiod debug_type type;
692*3d8817e4Smiod
693*3d8817e4Smiod sym = syms[symbols.symno];
694*3d8817e4Smiod
695*3d8817e4Smiod if (! bfd_coff_get_syment (abfd, sym, &syment))
696*3d8817e4Smiod {
697*3d8817e4Smiod non_fatal (_("bfd_coff_get_syment failed: %s"),
698*3d8817e4Smiod bfd_errmsg (bfd_get_error ()));
699*3d8817e4Smiod return FALSE;
700*3d8817e4Smiod }
701*3d8817e4Smiod
702*3d8817e4Smiod name = bfd_asymbol_name (sym);
703*3d8817e4Smiod
704*3d8817e4Smiod this_coff_symno = symbols.coff_symno;
705*3d8817e4Smiod
706*3d8817e4Smiod ++symbols.symno;
707*3d8817e4Smiod symbols.coff_symno += 1 + syment.n_numaux;
708*3d8817e4Smiod
709*3d8817e4Smiod /* We only worry about the first auxent, because that is the
710*3d8817e4Smiod only one which is relevant for debugging information. */
711*3d8817e4Smiod if (syment.n_numaux == 0)
712*3d8817e4Smiod paux = NULL;
713*3d8817e4Smiod else
714*3d8817e4Smiod {
715*3d8817e4Smiod if (! bfd_coff_get_auxent (abfd, sym, 0, &auxent))
716*3d8817e4Smiod {
717*3d8817e4Smiod non_fatal (_("bfd_coff_get_auxent failed: %s"),
718*3d8817e4Smiod bfd_errmsg (bfd_get_error ()));
719*3d8817e4Smiod return FALSE;
720*3d8817e4Smiod }
721*3d8817e4Smiod paux = &auxent;
722*3d8817e4Smiod }
723*3d8817e4Smiod
724*3d8817e4Smiod if (this_coff_symno == next_c_file && syment.n_sclass != C_FILE)
725*3d8817e4Smiod {
726*3d8817e4Smiod /* The last C_FILE symbol points to the first external
727*3d8817e4Smiod symbol. */
728*3d8817e4Smiod if (! debug_set_filename (dhandle, "*globals*"))
729*3d8817e4Smiod return FALSE;
730*3d8817e4Smiod }
731*3d8817e4Smiod
732*3d8817e4Smiod switch (syment.n_sclass)
733*3d8817e4Smiod {
734*3d8817e4Smiod case C_EFCN:
735*3d8817e4Smiod case C_EXTDEF:
736*3d8817e4Smiod case C_ULABEL:
737*3d8817e4Smiod case C_USTATIC:
738*3d8817e4Smiod case C_LINE:
739*3d8817e4Smiod case C_ALIAS:
740*3d8817e4Smiod case C_HIDDEN:
741*3d8817e4Smiod /* Just ignore these classes. */
742*3d8817e4Smiod break;
743*3d8817e4Smiod
744*3d8817e4Smiod case C_FILE:
745*3d8817e4Smiod next_c_file = syment.n_value;
746*3d8817e4Smiod if (! debug_set_filename (dhandle, name))
747*3d8817e4Smiod return FALSE;
748*3d8817e4Smiod break;
749*3d8817e4Smiod
750*3d8817e4Smiod case C_STAT:
751*3d8817e4Smiod /* Ignore static symbols with a type of T_NULL. These
752*3d8817e4Smiod represent section entries. */
753*3d8817e4Smiod if (syment.n_type == T_NULL)
754*3d8817e4Smiod break;
755*3d8817e4Smiod /* Fall through. */
756*3d8817e4Smiod case C_WEAKEXT:
757*3d8817e4Smiod case C_EXT:
758*3d8817e4Smiod if (ISFCN (syment.n_type))
759*3d8817e4Smiod {
760*3d8817e4Smiod fnname = name;
761*3d8817e4Smiod fnclass = syment.n_sclass;
762*3d8817e4Smiod fntype = syment.n_type;
763*3d8817e4Smiod if (syment.n_numaux > 0)
764*3d8817e4Smiod fnend = bfd_asymbol_value (sym) + auxent.x_sym.x_misc.x_fsize;
765*3d8817e4Smiod else
766*3d8817e4Smiod fnend = 0;
767*3d8817e4Smiod linenos = BFD_SEND (abfd, _get_lineno, (abfd, sym));
768*3d8817e4Smiod break;
769*3d8817e4Smiod }
770*3d8817e4Smiod type = parse_coff_type (abfd, &symbols, &types, this_coff_symno,
771*3d8817e4Smiod syment.n_type, paux, TRUE, dhandle);
772*3d8817e4Smiod if (type == DEBUG_TYPE_NULL)
773*3d8817e4Smiod return FALSE;
774*3d8817e4Smiod if (! parse_coff_symbol (abfd, &types, sym, this_coff_symno, &syment,
775*3d8817e4Smiod dhandle, type, within_function))
776*3d8817e4Smiod return FALSE;
777*3d8817e4Smiod break;
778*3d8817e4Smiod
779*3d8817e4Smiod case C_FCN:
780*3d8817e4Smiod if (strcmp (name, ".bf") == 0)
781*3d8817e4Smiod {
782*3d8817e4Smiod if (fnname == NULL)
783*3d8817e4Smiod {
784*3d8817e4Smiod non_fatal (_("%ld: .bf without preceding function"),
785*3d8817e4Smiod this_coff_symno);
786*3d8817e4Smiod return FALSE;
787*3d8817e4Smiod }
788*3d8817e4Smiod
789*3d8817e4Smiod type = parse_coff_type (abfd, &symbols, &types, this_coff_symno,
790*3d8817e4Smiod DECREF (fntype), paux, FALSE, dhandle);
791*3d8817e4Smiod if (type == DEBUG_TYPE_NULL)
792*3d8817e4Smiod return FALSE;
793*3d8817e4Smiod
794*3d8817e4Smiod if (! debug_record_function (dhandle, fnname, type,
795*3d8817e4Smiod external_coff_symbol_p (fnclass),
796*3d8817e4Smiod bfd_asymbol_value (sym)))
797*3d8817e4Smiod return FALSE;
798*3d8817e4Smiod
799*3d8817e4Smiod if (linenos != NULL)
800*3d8817e4Smiod {
801*3d8817e4Smiod int base;
802*3d8817e4Smiod bfd_vma addr;
803*3d8817e4Smiod
804*3d8817e4Smiod if (syment.n_numaux == 0)
805*3d8817e4Smiod base = 0;
806*3d8817e4Smiod else
807*3d8817e4Smiod base = auxent.x_sym.x_misc.x_lnsz.x_lnno - 1;
808*3d8817e4Smiod
809*3d8817e4Smiod addr = bfd_get_section_vma (abfd, bfd_get_section (sym));
810*3d8817e4Smiod
811*3d8817e4Smiod ++linenos;
812*3d8817e4Smiod
813*3d8817e4Smiod while (linenos->line_number != 0)
814*3d8817e4Smiod {
815*3d8817e4Smiod if (! debug_record_line (dhandle,
816*3d8817e4Smiod linenos->line_number + base,
817*3d8817e4Smiod linenos->u.offset + addr))
818*3d8817e4Smiod return FALSE;
819*3d8817e4Smiod ++linenos;
820*3d8817e4Smiod }
821*3d8817e4Smiod }
822*3d8817e4Smiod
823*3d8817e4Smiod fnname = NULL;
824*3d8817e4Smiod linenos = NULL;
825*3d8817e4Smiod fnclass = 0;
826*3d8817e4Smiod fntype = 0;
827*3d8817e4Smiod
828*3d8817e4Smiod within_function = TRUE;
829*3d8817e4Smiod }
830*3d8817e4Smiod else if (strcmp (name, ".ef") == 0)
831*3d8817e4Smiod {
832*3d8817e4Smiod if (! within_function)
833*3d8817e4Smiod {
834*3d8817e4Smiod non_fatal (_("%ld: unexpected .ef\n"), this_coff_symno);
835*3d8817e4Smiod return FALSE;
836*3d8817e4Smiod }
837*3d8817e4Smiod
838*3d8817e4Smiod if (bfd_asymbol_value (sym) > fnend)
839*3d8817e4Smiod fnend = bfd_asymbol_value (sym);
840*3d8817e4Smiod if (! debug_end_function (dhandle, fnend))
841*3d8817e4Smiod return FALSE;
842*3d8817e4Smiod
843*3d8817e4Smiod fnend = 0;
844*3d8817e4Smiod within_function = FALSE;
845*3d8817e4Smiod }
846*3d8817e4Smiod break;
847*3d8817e4Smiod
848*3d8817e4Smiod case C_BLOCK:
849*3d8817e4Smiod if (strcmp (name, ".bb") == 0)
850*3d8817e4Smiod {
851*3d8817e4Smiod if (! debug_start_block (dhandle, bfd_asymbol_value (sym)))
852*3d8817e4Smiod return FALSE;
853*3d8817e4Smiod }
854*3d8817e4Smiod else if (strcmp (name, ".eb") == 0)
855*3d8817e4Smiod {
856*3d8817e4Smiod if (! debug_end_block (dhandle, bfd_asymbol_value (sym)))
857*3d8817e4Smiod return FALSE;
858*3d8817e4Smiod }
859*3d8817e4Smiod break;
860*3d8817e4Smiod
861*3d8817e4Smiod default:
862*3d8817e4Smiod type = parse_coff_type (abfd, &symbols, &types, this_coff_symno,
863*3d8817e4Smiod syment.n_type, paux, TRUE, dhandle);
864*3d8817e4Smiod if (type == DEBUG_TYPE_NULL)
865*3d8817e4Smiod return FALSE;
866*3d8817e4Smiod if (! parse_coff_symbol (abfd, &types, sym, this_coff_symno, &syment,
867*3d8817e4Smiod dhandle, type, within_function))
868*3d8817e4Smiod return FALSE;
869*3d8817e4Smiod break;
870*3d8817e4Smiod }
871*3d8817e4Smiod }
872*3d8817e4Smiod
873*3d8817e4Smiod return TRUE;
874*3d8817e4Smiod }
875