xref: /dflybsd-src/contrib/binutils-2.34/binutils/wrstabs.c (revision b52ef7118d1621abed722c5bbbd542210290ecef)
1*fae548d3Szrj /* wrstabs.c -- Output stabs debugging information
2*fae548d3Szrj    Copyright (C) 1996-2020 Free Software Foundation, Inc.
3*fae548d3Szrj    Written by Ian Lance Taylor <ian@cygnus.com>.
4*fae548d3Szrj 
5*fae548d3Szrj    This file is part of GNU Binutils.
6*fae548d3Szrj 
7*fae548d3Szrj    This program is free software; you can redistribute it and/or modify
8*fae548d3Szrj    it under the terms of the GNU General Public License as published by
9*fae548d3Szrj    the Free Software Foundation; either version 3 of the License, or
10*fae548d3Szrj    (at your option) any later version.
11*fae548d3Szrj 
12*fae548d3Szrj    This program is distributed in the hope that it will be useful,
13*fae548d3Szrj    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*fae548d3Szrj    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*fae548d3Szrj    GNU General Public License for more details.
16*fae548d3Szrj 
17*fae548d3Szrj    You should have received a copy of the GNU General Public License
18*fae548d3Szrj    along with this program; if not, write to the Free Software
19*fae548d3Szrj    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
20*fae548d3Szrj    02110-1301, USA.  */
21*fae548d3Szrj 
22*fae548d3Szrj /* This file contains code which writes out stabs debugging
23*fae548d3Szrj    information.  */
24*fae548d3Szrj 
25*fae548d3Szrj #include "sysdep.h"
26*fae548d3Szrj #include <assert.h>
27*fae548d3Szrj #include "bfd.h"
28*fae548d3Szrj #include "libiberty.h"
29*fae548d3Szrj #include "filenames.h"
30*fae548d3Szrj #include "safe-ctype.h"
31*fae548d3Szrj #include "bucomm.h"
32*fae548d3Szrj #include "debug.h"
33*fae548d3Szrj #include "budbg.h"
34*fae548d3Szrj #include "aout/aout64.h"
35*fae548d3Szrj #include "aout/stab_gnu.h"
36*fae548d3Szrj 
37*fae548d3Szrj /* The size of a stabs symbol.  This presumes 32 bit values.  */
38*fae548d3Szrj 
39*fae548d3Szrj #define STAB_SYMBOL_SIZE (12)
40*fae548d3Szrj 
41*fae548d3Szrj /* An entry in a string hash table.  */
42*fae548d3Szrj 
43*fae548d3Szrj struct string_hash_entry
44*fae548d3Szrj {
45*fae548d3Szrj   struct bfd_hash_entry root;
46*fae548d3Szrj   /* Next string in this table.  */
47*fae548d3Szrj   struct string_hash_entry *next;
48*fae548d3Szrj   /* Index in string table.  */
49*fae548d3Szrj   long index;
50*fae548d3Szrj   /* Size of type if this is a typedef.  */
51*fae548d3Szrj   unsigned int size;
52*fae548d3Szrj };
53*fae548d3Szrj 
54*fae548d3Szrj /* A string hash table.  */
55*fae548d3Szrj 
56*fae548d3Szrj struct string_hash_table
57*fae548d3Szrj {
58*fae548d3Szrj   struct bfd_hash_table table;
59*fae548d3Szrj };
60*fae548d3Szrj 
61*fae548d3Szrj /* The type stack.  Each element on the stack is a string.  */
62*fae548d3Szrj 
63*fae548d3Szrj struct stab_type_stack
64*fae548d3Szrj {
65*fae548d3Szrj   /* The next element on the stack.  */
66*fae548d3Szrj   struct stab_type_stack *next;
67*fae548d3Szrj   /* This element as a string.  */
68*fae548d3Szrj   char *string;
69*fae548d3Szrj   /* The type index of this element.  */
70*fae548d3Szrj   long index;
71*fae548d3Szrj   /* The size of the type.  */
72*fae548d3Szrj   unsigned int size;
73*fae548d3Szrj   /* Whether type string defines a new type.  */
74*fae548d3Szrj   bfd_boolean definition;
75*fae548d3Szrj   /* String defining struct fields.  */
76*fae548d3Szrj   char *fields;
77*fae548d3Szrj   /* NULL terminated array of strings defining base classes for a
78*fae548d3Szrj      class.  */
79*fae548d3Szrj   char **baseclasses;
80*fae548d3Szrj   /* String defining class methods.  */
81*fae548d3Szrj   char *methods;
82*fae548d3Szrj   /* String defining vtable pointer for a class.  */
83*fae548d3Szrj   char *vtable;
84*fae548d3Szrj };
85*fae548d3Szrj 
86*fae548d3Szrj /* This structure is used to keep track of type indices for tagged
87*fae548d3Szrj    types.  */
88*fae548d3Szrj 
89*fae548d3Szrj struct stab_tag
90*fae548d3Szrj {
91*fae548d3Szrj   /* The type index.  */
92*fae548d3Szrj   long index;
93*fae548d3Szrj   /* The tag name.  */
94*fae548d3Szrj   const char *tag;
95*fae548d3Szrj   /* The kind of type.  This is set to DEBUG_KIND_ILLEGAL when the
96*fae548d3Szrj      type is defined.  */
97*fae548d3Szrj   enum debug_type_kind kind;
98*fae548d3Szrj   /* The size of the struct.  */
99*fae548d3Szrj   unsigned int size;
100*fae548d3Szrj };
101*fae548d3Szrj 
102*fae548d3Szrj /* We remember various sorts of type indices.  They are not related,
103*fae548d3Szrj    but, for convenience, we keep all the information in this
104*fae548d3Szrj    structure.  */
105*fae548d3Szrj 
106*fae548d3Szrj struct stab_type_cache
107*fae548d3Szrj {
108*fae548d3Szrj   /* The void type index.  */
109*fae548d3Szrj   long void_type;
110*fae548d3Szrj   /* Signed integer type indices, indexed by size - 1.  */
111*fae548d3Szrj   long signed_integer_types[8];
112*fae548d3Szrj   /* Unsigned integer type indices, indexed by size - 1.  */
113*fae548d3Szrj   long unsigned_integer_types[8];
114*fae548d3Szrj   /* Floating point types, indexed by size - 1.  */
115*fae548d3Szrj   long float_types[16];
116*fae548d3Szrj   /* Pointers to types, indexed by the type index.  */
117*fae548d3Szrj   long *pointer_types;
118*fae548d3Szrj   size_t pointer_types_alloc;
119*fae548d3Szrj   /* Functions returning types, indexed by the type index.  */
120*fae548d3Szrj   long *function_types;
121*fae548d3Szrj   size_t function_types_alloc;
122*fae548d3Szrj   /* References to types, indexed by the type index.  */
123*fae548d3Szrj   long *reference_types;
124*fae548d3Szrj   size_t reference_types_alloc;
125*fae548d3Szrj   /* Struct/union/class type indices, indexed by the struct id.  */
126*fae548d3Szrj   struct stab_tag *struct_types;
127*fae548d3Szrj   size_t struct_types_alloc;
128*fae548d3Szrj };
129*fae548d3Szrj 
130*fae548d3Szrj /* This is the handle passed through debug_write.  */
131*fae548d3Szrj 
132*fae548d3Szrj struct stab_write_handle
133*fae548d3Szrj {
134*fae548d3Szrj   /* The BFD.  */
135*fae548d3Szrj   bfd *abfd;
136*fae548d3Szrj   /* This buffer holds the symbols.  */
137*fae548d3Szrj   bfd_byte *symbols;
138*fae548d3Szrj   size_t symbols_size;
139*fae548d3Szrj   size_t symbols_alloc;
140*fae548d3Szrj   /* This is a list of hash table entries for the strings.  */
141*fae548d3Szrj   struct string_hash_entry *strings;
142*fae548d3Szrj   /* The last string hash table entry.  */
143*fae548d3Szrj   struct string_hash_entry *last_string;
144*fae548d3Szrj   /* The size of the strings.  */
145*fae548d3Szrj   size_t strings_size;
146*fae548d3Szrj   /* This hash table eliminates duplicate strings.  */
147*fae548d3Szrj   struct string_hash_table strhash;
148*fae548d3Szrj   /* The type stack.  */
149*fae548d3Szrj   struct stab_type_stack *type_stack;
150*fae548d3Szrj   /* The next type index.  */
151*fae548d3Szrj   long type_index;
152*fae548d3Szrj   /* The type cache.  */
153*fae548d3Szrj   struct stab_type_cache type_cache;
154*fae548d3Szrj   /* A mapping from typedef names to type indices.  */
155*fae548d3Szrj   struct string_hash_table typedef_hash;
156*fae548d3Szrj   /* If this is not -1, it is the offset to the most recent N_SO
157*fae548d3Szrj      symbol, and the value of that symbol needs to be set.  */
158*fae548d3Szrj   long so_offset;
159*fae548d3Szrj   /* If this is not -1, it is the offset to the most recent N_FUN
160*fae548d3Szrj      symbol, and the value of that symbol needs to be set.  */
161*fae548d3Szrj   long fun_offset;
162*fae548d3Szrj   /* The last text section address seen.  */
163*fae548d3Szrj   bfd_vma last_text_address;
164*fae548d3Szrj   /* The block nesting depth.  */
165*fae548d3Szrj   unsigned int nesting;
166*fae548d3Szrj   /* The function address.  */
167*fae548d3Szrj   bfd_vma fnaddr;
168*fae548d3Szrj   /* A pending LBRAC symbol.  */
169*fae548d3Szrj   bfd_vma pending_lbrac;
170*fae548d3Szrj   /* The current line number file name.  */
171*fae548d3Szrj   const char *lineno_filename;
172*fae548d3Szrj };
173*fae548d3Szrj 
174*fae548d3Szrj static struct bfd_hash_entry *string_hash_newfunc
175*fae548d3Szrj   (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
176*fae548d3Szrj static bfd_boolean stab_write_symbol
177*fae548d3Szrj   (struct stab_write_handle *, int, int, bfd_vma, const char *);
178*fae548d3Szrj static bfd_boolean stab_push_string
179*fae548d3Szrj   (struct stab_write_handle *, const char *, long, bfd_boolean, unsigned int);
180*fae548d3Szrj static bfd_boolean stab_push_defined_type
181*fae548d3Szrj   (struct stab_write_handle *, long, unsigned int);
182*fae548d3Szrj static char *stab_pop_type (struct stab_write_handle *);
183*fae548d3Szrj static bfd_boolean stab_modify_type
184*fae548d3Szrj   (struct stab_write_handle *, int, unsigned int, long **, size_t *);
185*fae548d3Szrj static long stab_get_struct_index
186*fae548d3Szrj   (struct stab_write_handle *, const char *, unsigned int,
187*fae548d3Szrj    enum debug_type_kind, unsigned int *);
188*fae548d3Szrj static bfd_boolean stab_class_method_var
189*fae548d3Szrj   (struct stab_write_handle *, const char *, enum debug_visibility,
190*fae548d3Szrj    bfd_boolean, bfd_boolean, bfd_boolean, bfd_vma, bfd_boolean);
191*fae548d3Szrj static bfd_boolean stab_start_compilation_unit (void *, const char *);
192*fae548d3Szrj static bfd_boolean stab_start_source (void *, const char *);
193*fae548d3Szrj static bfd_boolean stab_empty_type (void *);
194*fae548d3Szrj static bfd_boolean stab_void_type (void *);
195*fae548d3Szrj static bfd_boolean stab_int_type (void *, unsigned int, bfd_boolean);
196*fae548d3Szrj static bfd_boolean stab_float_type (void *, unsigned int);
197*fae548d3Szrj static bfd_boolean stab_complex_type (void *, unsigned int);
198*fae548d3Szrj static bfd_boolean stab_bool_type (void *, unsigned int);
199*fae548d3Szrj static bfd_boolean stab_enum_type
200*fae548d3Szrj   (void *, const char *, const char **, bfd_signed_vma *);
201*fae548d3Szrj static bfd_boolean stab_pointer_type (void *);
202*fae548d3Szrj static bfd_boolean stab_function_type (void *, int, bfd_boolean);
203*fae548d3Szrj static bfd_boolean stab_reference_type (void *);
204*fae548d3Szrj static bfd_boolean stab_range_type (void *, bfd_signed_vma, bfd_signed_vma);
205*fae548d3Szrj static bfd_boolean stab_array_type
206*fae548d3Szrj   (void *, bfd_signed_vma, bfd_signed_vma, bfd_boolean);
207*fae548d3Szrj static bfd_boolean stab_set_type (void *, bfd_boolean);
208*fae548d3Szrj static bfd_boolean stab_offset_type (void *);
209*fae548d3Szrj static bfd_boolean stab_method_type (void *, bfd_boolean, int, bfd_boolean);
210*fae548d3Szrj static bfd_boolean stab_const_type (void *);
211*fae548d3Szrj static bfd_boolean stab_volatile_type (void *);
212*fae548d3Szrj static bfd_boolean stab_start_struct_type
213*fae548d3Szrj   (void *, const char *, unsigned int, bfd_boolean, unsigned int);
214*fae548d3Szrj static bfd_boolean stab_struct_field
215*fae548d3Szrj   (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
216*fae548d3Szrj static bfd_boolean stab_end_struct_type (void *);
217*fae548d3Szrj static bfd_boolean stab_start_class_type
218*fae548d3Szrj   (void *, const char *, unsigned int, bfd_boolean, unsigned int,
219*fae548d3Szrj    bfd_boolean, bfd_boolean);
220*fae548d3Szrj static bfd_boolean stab_class_static_member
221*fae548d3Szrj   (void *, const char *, const char *, enum debug_visibility);
222*fae548d3Szrj static bfd_boolean stab_class_baseclass
223*fae548d3Szrj   (void *, bfd_vma, bfd_boolean, enum debug_visibility);
224*fae548d3Szrj static bfd_boolean stab_class_start_method (void *, const char *);
225*fae548d3Szrj static bfd_boolean stab_class_method_variant
226*fae548d3Szrj   (void *, const char *, enum debug_visibility, bfd_boolean, bfd_boolean,
227*fae548d3Szrj    bfd_vma, bfd_boolean);
228*fae548d3Szrj static bfd_boolean stab_class_static_method_variant
229*fae548d3Szrj   (void *, const char *, enum debug_visibility, bfd_boolean, bfd_boolean);
230*fae548d3Szrj static bfd_boolean stab_class_end_method (void *);
231*fae548d3Szrj static bfd_boolean stab_end_class_type (void *);
232*fae548d3Szrj static bfd_boolean stab_typedef_type (void *, const char *);
233*fae548d3Szrj static bfd_boolean stab_tag_type
234*fae548d3Szrj   (void *, const char *, unsigned int, enum debug_type_kind);
235*fae548d3Szrj static bfd_boolean stab_typdef (void *, const char *);
236*fae548d3Szrj static bfd_boolean stab_tag (void *, const char *);
237*fae548d3Szrj static bfd_boolean stab_int_constant (void *, const char *, bfd_vma);
238*fae548d3Szrj static bfd_boolean stab_float_constant (void *, const char *, double);
239*fae548d3Szrj static bfd_boolean stab_typed_constant (void *, const char *, bfd_vma);
240*fae548d3Szrj static bfd_boolean stab_variable
241*fae548d3Szrj   (void *, const char *, enum debug_var_kind, bfd_vma);
242*fae548d3Szrj static bfd_boolean stab_start_function (void *, const char *, bfd_boolean);
243*fae548d3Szrj static bfd_boolean stab_function_parameter
244*fae548d3Szrj   (void *, const char *, enum debug_parm_kind, bfd_vma);
245*fae548d3Szrj static bfd_boolean stab_start_block (void *, bfd_vma);
246*fae548d3Szrj static bfd_boolean stab_end_block (void *, bfd_vma);
247*fae548d3Szrj static bfd_boolean stab_end_function (void *);
248*fae548d3Szrj static bfd_boolean stab_lineno (void *, const char *, unsigned long, bfd_vma);
249*fae548d3Szrj 
250*fae548d3Szrj static const struct debug_write_fns stab_fns =
251*fae548d3Szrj {
252*fae548d3Szrj   stab_start_compilation_unit,
253*fae548d3Szrj   stab_start_source,
254*fae548d3Szrj   stab_empty_type,
255*fae548d3Szrj   stab_void_type,
256*fae548d3Szrj   stab_int_type,
257*fae548d3Szrj   stab_float_type,
258*fae548d3Szrj   stab_complex_type,
259*fae548d3Szrj   stab_bool_type,
260*fae548d3Szrj   stab_enum_type,
261*fae548d3Szrj   stab_pointer_type,
262*fae548d3Szrj   stab_function_type,
263*fae548d3Szrj   stab_reference_type,
264*fae548d3Szrj   stab_range_type,
265*fae548d3Szrj   stab_array_type,
266*fae548d3Szrj   stab_set_type,
267*fae548d3Szrj   stab_offset_type,
268*fae548d3Szrj   stab_method_type,
269*fae548d3Szrj   stab_const_type,
270*fae548d3Szrj   stab_volatile_type,
271*fae548d3Szrj   stab_start_struct_type,
272*fae548d3Szrj   stab_struct_field,
273*fae548d3Szrj   stab_end_struct_type,
274*fae548d3Szrj   stab_start_class_type,
275*fae548d3Szrj   stab_class_static_member,
276*fae548d3Szrj   stab_class_baseclass,
277*fae548d3Szrj   stab_class_start_method,
278*fae548d3Szrj   stab_class_method_variant,
279*fae548d3Szrj   stab_class_static_method_variant,
280*fae548d3Szrj   stab_class_end_method,
281*fae548d3Szrj   stab_end_class_type,
282*fae548d3Szrj   stab_typedef_type,
283*fae548d3Szrj   stab_tag_type,
284*fae548d3Szrj   stab_typdef,
285*fae548d3Szrj   stab_tag,
286*fae548d3Szrj   stab_int_constant,
287*fae548d3Szrj   stab_float_constant,
288*fae548d3Szrj   stab_typed_constant,
289*fae548d3Szrj   stab_variable,
290*fae548d3Szrj   stab_start_function,
291*fae548d3Szrj   stab_function_parameter,
292*fae548d3Szrj   stab_start_block,
293*fae548d3Szrj   stab_end_block,
294*fae548d3Szrj   stab_end_function,
295*fae548d3Szrj   stab_lineno
296*fae548d3Szrj };
297*fae548d3Szrj 
298*fae548d3Szrj /* Routine to create an entry in a string hash table.  */
299*fae548d3Szrj 
300*fae548d3Szrj static struct bfd_hash_entry *
string_hash_newfunc(struct bfd_hash_entry * entry,struct bfd_hash_table * table,const char * string)301*fae548d3Szrj string_hash_newfunc (struct bfd_hash_entry *entry,
302*fae548d3Szrj 		     struct bfd_hash_table *table, const char *string)
303*fae548d3Szrj {
304*fae548d3Szrj   struct string_hash_entry *ret = (struct string_hash_entry *) entry;
305*fae548d3Szrj 
306*fae548d3Szrj   /* Allocate the structure if it has not already been allocated by a
307*fae548d3Szrj      subclass.  */
308*fae548d3Szrj   if (ret == (struct string_hash_entry *) NULL)
309*fae548d3Szrj     ret = ((struct string_hash_entry *)
310*fae548d3Szrj 	   bfd_hash_allocate (table, sizeof (struct string_hash_entry)));
311*fae548d3Szrj   if (ret == (struct string_hash_entry *) NULL)
312*fae548d3Szrj     return NULL;
313*fae548d3Szrj 
314*fae548d3Szrj   /* Call the allocation method of the superclass.  */
315*fae548d3Szrj   ret = ((struct string_hash_entry *)
316*fae548d3Szrj 	 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
317*fae548d3Szrj 
318*fae548d3Szrj   if (ret)
319*fae548d3Szrj     {
320*fae548d3Szrj       /* Initialize the local fields.  */
321*fae548d3Szrj       ret->next = NULL;
322*fae548d3Szrj       ret->index = -1;
323*fae548d3Szrj       ret->size = 0;
324*fae548d3Szrj     }
325*fae548d3Szrj 
326*fae548d3Szrj   return (struct bfd_hash_entry *) ret;
327*fae548d3Szrj }
328*fae548d3Szrj 
329*fae548d3Szrj /* Look up an entry in a string hash table.  */
330*fae548d3Szrj 
331*fae548d3Szrj #define string_hash_lookup(t, string, create, copy) \
332*fae548d3Szrj   ((struct string_hash_entry *) \
333*fae548d3Szrj    bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
334*fae548d3Szrj 
335*fae548d3Szrj /* Add a symbol to the stabs debugging information we are building.  */
336*fae548d3Szrj 
337*fae548d3Szrj static bfd_boolean
stab_write_symbol(struct stab_write_handle * info,int type,int desc,bfd_vma value,const char * string)338*fae548d3Szrj stab_write_symbol (struct stab_write_handle *info, int type, int desc,
339*fae548d3Szrj 		   bfd_vma value, const char *string)
340*fae548d3Szrj {
341*fae548d3Szrj   bfd_size_type strx;
342*fae548d3Szrj   bfd_byte sym[STAB_SYMBOL_SIZE];
343*fae548d3Szrj 
344*fae548d3Szrj   if (string == NULL)
345*fae548d3Szrj     strx = 0;
346*fae548d3Szrj   else
347*fae548d3Szrj     {
348*fae548d3Szrj       struct string_hash_entry *h;
349*fae548d3Szrj 
350*fae548d3Szrj       h = string_hash_lookup (&info->strhash, string, TRUE, TRUE);
351*fae548d3Szrj       if (h == NULL)
352*fae548d3Szrj 	{
353*fae548d3Szrj 	  non_fatal (_("string_hash_lookup failed: %s"),
354*fae548d3Szrj 		     bfd_errmsg (bfd_get_error ()));
355*fae548d3Szrj 	  return FALSE;
356*fae548d3Szrj 	}
357*fae548d3Szrj       if (h->index != -1)
358*fae548d3Szrj 	strx = h->index;
359*fae548d3Szrj       else
360*fae548d3Szrj 	{
361*fae548d3Szrj 	  strx = info->strings_size;
362*fae548d3Szrj 	  h->index = strx;
363*fae548d3Szrj 	  if (info->last_string == NULL)
364*fae548d3Szrj 	    info->strings = h;
365*fae548d3Szrj 	  else
366*fae548d3Szrj 	    info->last_string->next = h;
367*fae548d3Szrj 	  info->last_string = h;
368*fae548d3Szrj 	  info->strings_size += strlen (string) + 1;
369*fae548d3Szrj 	}
370*fae548d3Szrj     }
371*fae548d3Szrj 
372*fae548d3Szrj   /* This presumes 32 bit values.  */
373*fae548d3Szrj   bfd_put_32 (info->abfd, strx, sym);
374*fae548d3Szrj   bfd_put_8 (info->abfd, type, sym + 4);
375*fae548d3Szrj   bfd_put_8 (info->abfd, 0, sym + 5);
376*fae548d3Szrj   bfd_put_16 (info->abfd, desc, sym + 6);
377*fae548d3Szrj   bfd_put_32 (info->abfd, value, sym + 8);
378*fae548d3Szrj 
379*fae548d3Szrj   if (info->symbols_size + STAB_SYMBOL_SIZE > info->symbols_alloc)
380*fae548d3Szrj     {
381*fae548d3Szrj       info->symbols_alloc *= 2;
382*fae548d3Szrj       info->symbols = (bfd_byte *) xrealloc (info->symbols,
383*fae548d3Szrj 					     info->symbols_alloc);
384*fae548d3Szrj     }
385*fae548d3Szrj 
386*fae548d3Szrj   memcpy (info->symbols + info->symbols_size, sym, STAB_SYMBOL_SIZE);
387*fae548d3Szrj 
388*fae548d3Szrj   info->symbols_size += STAB_SYMBOL_SIZE;
389*fae548d3Szrj 
390*fae548d3Szrj   return TRUE;
391*fae548d3Szrj }
392*fae548d3Szrj 
393*fae548d3Szrj /* Push a string on to the type stack.  */
394*fae548d3Szrj 
395*fae548d3Szrj static bfd_boolean
stab_push_string(struct stab_write_handle * info,const char * string,long tindex,bfd_boolean definition,unsigned int size)396*fae548d3Szrj stab_push_string (struct stab_write_handle *info, const char *string,
397*fae548d3Szrj 		  long tindex, bfd_boolean definition, unsigned int size)
398*fae548d3Szrj {
399*fae548d3Szrj   struct stab_type_stack *s;
400*fae548d3Szrj 
401*fae548d3Szrj   s = (struct stab_type_stack *) xmalloc (sizeof *s);
402*fae548d3Szrj   s->string = xstrdup (string);
403*fae548d3Szrj   s->index = tindex;
404*fae548d3Szrj   s->definition = definition;
405*fae548d3Szrj   s->size = size;
406*fae548d3Szrj 
407*fae548d3Szrj   s->fields = NULL;
408*fae548d3Szrj   s->baseclasses = NULL;
409*fae548d3Szrj   s->methods = NULL;
410*fae548d3Szrj   s->vtable = NULL;
411*fae548d3Szrj 
412*fae548d3Szrj   s->next = info->type_stack;
413*fae548d3Szrj   info->type_stack = s;
414*fae548d3Szrj 
415*fae548d3Szrj   return TRUE;
416*fae548d3Szrj }
417*fae548d3Szrj 
418*fae548d3Szrj /* Push a type index which has already been defined.  */
419*fae548d3Szrj 
420*fae548d3Szrj static bfd_boolean
stab_push_defined_type(struct stab_write_handle * info,long tindex,unsigned int size)421*fae548d3Szrj stab_push_defined_type (struct stab_write_handle *info, long tindex,
422*fae548d3Szrj 			unsigned int size)
423*fae548d3Szrj {
424*fae548d3Szrj   char buf[20];
425*fae548d3Szrj 
426*fae548d3Szrj   sprintf (buf, "%ld", tindex);
427*fae548d3Szrj   return stab_push_string (info, buf, tindex, FALSE, size);
428*fae548d3Szrj }
429*fae548d3Szrj 
430*fae548d3Szrj /* Pop a type off the type stack.  The caller is responsible for
431*fae548d3Szrj    freeing the string.  */
432*fae548d3Szrj 
433*fae548d3Szrj static char *
stab_pop_type(struct stab_write_handle * info)434*fae548d3Szrj stab_pop_type (struct stab_write_handle *info)
435*fae548d3Szrj {
436*fae548d3Szrj   struct stab_type_stack *s;
437*fae548d3Szrj   char *ret;
438*fae548d3Szrj 
439*fae548d3Szrj   s = info->type_stack;
440*fae548d3Szrj   assert (s != NULL);
441*fae548d3Szrj 
442*fae548d3Szrj   info->type_stack = s->next;
443*fae548d3Szrj 
444*fae548d3Szrj   ret = s->string;
445*fae548d3Szrj 
446*fae548d3Szrj   free (s);
447*fae548d3Szrj 
448*fae548d3Szrj   return ret;
449*fae548d3Szrj }
450*fae548d3Szrj 
451*fae548d3Szrj /* The general routine to write out stabs in sections debugging
452*fae548d3Szrj    information.  This accumulates the stabs symbols and the strings in
453*fae548d3Szrj    two obstacks.  We can't easily write out the information as we go
454*fae548d3Szrj    along, because we need to know the section sizes before we can
455*fae548d3Szrj    write out the section contents.  ABFD is the BFD and DHANDLE is the
456*fae548d3Szrj    handle for the debugging information.  This sets *PSYMS to point to
457*fae548d3Szrj    the symbols, *PSYMSIZE the size of the symbols, *PSTRINGS to the
458*fae548d3Szrj    strings, and *PSTRINGSIZE to the size of the strings.  */
459*fae548d3Szrj 
460*fae548d3Szrj bfd_boolean
write_stabs_in_sections_debugging_info(bfd * abfd,void * dhandle,bfd_byte ** psyms,bfd_size_type * psymsize,bfd_byte ** pstrings,bfd_size_type * pstringsize)461*fae548d3Szrj write_stabs_in_sections_debugging_info (bfd *abfd, void *dhandle,
462*fae548d3Szrj 					bfd_byte **psyms,
463*fae548d3Szrj 					bfd_size_type *psymsize,
464*fae548d3Szrj 					bfd_byte **pstrings,
465*fae548d3Szrj 					bfd_size_type *pstringsize)
466*fae548d3Szrj {
467*fae548d3Szrj   struct stab_write_handle info;
468*fae548d3Szrj   struct string_hash_entry *h;
469*fae548d3Szrj   bfd_byte *p;
470*fae548d3Szrj 
471*fae548d3Szrj   info.abfd = abfd;
472*fae548d3Szrj 
473*fae548d3Szrj   info.symbols_size = 0;
474*fae548d3Szrj   info.symbols_alloc = 500;
475*fae548d3Szrj   info.symbols = (bfd_byte *) xmalloc (info.symbols_alloc);
476*fae548d3Szrj 
477*fae548d3Szrj   info.strings = NULL;
478*fae548d3Szrj   info.last_string = NULL;
479*fae548d3Szrj   /* Reserve 1 byte for a null byte.  */
480*fae548d3Szrj   info.strings_size = 1;
481*fae548d3Szrj 
482*fae548d3Szrj   if (!bfd_hash_table_init (&info.strhash.table, string_hash_newfunc,
483*fae548d3Szrj 			    sizeof (struct string_hash_entry))
484*fae548d3Szrj       || !bfd_hash_table_init (&info.typedef_hash.table, string_hash_newfunc,
485*fae548d3Szrj 			       sizeof (struct string_hash_entry)))
486*fae548d3Szrj     {
487*fae548d3Szrj       non_fatal ("bfd_hash_table_init_failed: %s",
488*fae548d3Szrj 		 bfd_errmsg (bfd_get_error ()));
489*fae548d3Szrj       return FALSE;
490*fae548d3Szrj     }
491*fae548d3Szrj 
492*fae548d3Szrj   info.type_stack = NULL;
493*fae548d3Szrj   info.type_index = 1;
494*fae548d3Szrj   memset (&info.type_cache, 0, sizeof info.type_cache);
495*fae548d3Szrj   info.so_offset = -1;
496*fae548d3Szrj   info.fun_offset = -1;
497*fae548d3Szrj   info.last_text_address = 0;
498*fae548d3Szrj   info.nesting = 0;
499*fae548d3Szrj   info.fnaddr = 0;
500*fae548d3Szrj   info.pending_lbrac = (bfd_vma) -1;
501*fae548d3Szrj 
502*fae548d3Szrj   /* The initial symbol holds the string size.  */
503*fae548d3Szrj   if (! stab_write_symbol (&info, 0, 0, 0, (const char *) NULL))
504*fae548d3Szrj     return FALSE;
505*fae548d3Szrj 
506*fae548d3Szrj   /* Output an initial N_SO symbol.  */
507*fae548d3Szrj   info.so_offset = info.symbols_size;
508*fae548d3Szrj   if (! stab_write_symbol (&info, N_SO, 0, 0, bfd_get_filename (abfd)))
509*fae548d3Szrj     return FALSE;
510*fae548d3Szrj 
511*fae548d3Szrj   if (! debug_write (dhandle, &stab_fns, (void *) &info))
512*fae548d3Szrj     return FALSE;
513*fae548d3Szrj 
514*fae548d3Szrj   assert (info.pending_lbrac == (bfd_vma) -1);
515*fae548d3Szrj 
516*fae548d3Szrj   /* Output a trailing N_SO.  */
517*fae548d3Szrj   if (! stab_write_symbol (&info, N_SO, 0, info.last_text_address,
518*fae548d3Szrj 			   (const char *) NULL))
519*fae548d3Szrj     return FALSE;
520*fae548d3Szrj 
521*fae548d3Szrj   /* Put the string size in the initial symbol.  */
522*fae548d3Szrj   bfd_put_32 (abfd, info.strings_size, info.symbols + 8);
523*fae548d3Szrj 
524*fae548d3Szrj   *psyms = info.symbols;
525*fae548d3Szrj   *psymsize = info.symbols_size;
526*fae548d3Szrj 
527*fae548d3Szrj   *pstringsize = info.strings_size;
528*fae548d3Szrj   *pstrings = (bfd_byte *) xmalloc (info.strings_size);
529*fae548d3Szrj 
530*fae548d3Szrj   p = *pstrings;
531*fae548d3Szrj   *p++ = '\0';
532*fae548d3Szrj   for (h = info.strings; h != NULL; h = h->next)
533*fae548d3Szrj     {
534*fae548d3Szrj       strcpy ((char *) p, h->root.string);
535*fae548d3Szrj       p += strlen ((char *) p) + 1;
536*fae548d3Szrj     }
537*fae548d3Szrj 
538*fae548d3Szrj   return TRUE;
539*fae548d3Szrj }
540*fae548d3Szrj 
541*fae548d3Szrj /* Start writing out information for a compilation unit.  */
542*fae548d3Szrj 
543*fae548d3Szrj static bfd_boolean
stab_start_compilation_unit(void * p,const char * filename)544*fae548d3Szrj stab_start_compilation_unit (void *p, const char *filename)
545*fae548d3Szrj {
546*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
547*fae548d3Szrj 
548*fae548d3Szrj   /* We would normally output an N_SO symbol here.  However, that
549*fae548d3Szrj      would force us to reset all of our type information.  I think we
550*fae548d3Szrj      will be better off just outputting an N_SOL symbol, and not
551*fae548d3Szrj      worrying about splitting information between files.  */
552*fae548d3Szrj 
553*fae548d3Szrj   info->lineno_filename = filename;
554*fae548d3Szrj 
555*fae548d3Szrj   return stab_write_symbol (info, N_SOL, 0, 0, filename);
556*fae548d3Szrj }
557*fae548d3Szrj 
558*fae548d3Szrj /* Start writing out information for a particular source file.  */
559*fae548d3Szrj 
560*fae548d3Szrj static bfd_boolean
stab_start_source(void * p,const char * filename)561*fae548d3Szrj stab_start_source (void *p, const char *filename)
562*fae548d3Szrj {
563*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
564*fae548d3Szrj 
565*fae548d3Szrj   /* FIXME: The symbol's value is supposed to be the text section
566*fae548d3Szrj      address.  However, we would have to fill it in later, and gdb
567*fae548d3Szrj      doesn't care, so we don't bother with it.  */
568*fae548d3Szrj 
569*fae548d3Szrj   info->lineno_filename = filename;
570*fae548d3Szrj 
571*fae548d3Szrj   return stab_write_symbol (info, N_SOL, 0, 0, filename);
572*fae548d3Szrj }
573*fae548d3Szrj 
574*fae548d3Szrj /* Push an empty type.  This shouldn't normally happen.  We just use a
575*fae548d3Szrj    void type.  */
576*fae548d3Szrj 
577*fae548d3Szrj static bfd_boolean
stab_empty_type(void * p)578*fae548d3Szrj stab_empty_type (void *p)
579*fae548d3Szrj {
580*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
581*fae548d3Szrj 
582*fae548d3Szrj   /* We don't call stab_void_type if the type is not yet defined,
583*fae548d3Szrj      because that might screw up the typedef.  */
584*fae548d3Szrj 
585*fae548d3Szrj   if (info->type_cache.void_type != 0)
586*fae548d3Szrj     return stab_push_defined_type (info, info->type_cache.void_type, 0);
587*fae548d3Szrj   else
588*fae548d3Szrj     {
589*fae548d3Szrj       long tindex;
590*fae548d3Szrj       char buf[40];
591*fae548d3Szrj 
592*fae548d3Szrj       tindex = info->type_index;
593*fae548d3Szrj       ++info->type_index;
594*fae548d3Szrj 
595*fae548d3Szrj       sprintf (buf, "%ld=%ld", tindex, tindex);
596*fae548d3Szrj 
597*fae548d3Szrj       return stab_push_string (info, buf, tindex, FALSE, 0);
598*fae548d3Szrj     }
599*fae548d3Szrj }
600*fae548d3Szrj 
601*fae548d3Szrj /* Push a void type.  */
602*fae548d3Szrj 
603*fae548d3Szrj static bfd_boolean
stab_void_type(void * p)604*fae548d3Szrj stab_void_type (void *p)
605*fae548d3Szrj {
606*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
607*fae548d3Szrj 
608*fae548d3Szrj   if (info->type_cache.void_type != 0)
609*fae548d3Szrj     return stab_push_defined_type (info, info->type_cache.void_type, 0);
610*fae548d3Szrj   else
611*fae548d3Szrj     {
612*fae548d3Szrj       long tindex;
613*fae548d3Szrj       char buf[40];
614*fae548d3Szrj 
615*fae548d3Szrj       tindex = info->type_index;
616*fae548d3Szrj       ++info->type_index;
617*fae548d3Szrj 
618*fae548d3Szrj       info->type_cache.void_type = tindex;
619*fae548d3Szrj 
620*fae548d3Szrj       sprintf (buf, "%ld=%ld", tindex, tindex);
621*fae548d3Szrj 
622*fae548d3Szrj       return stab_push_string (info, buf, tindex, TRUE, 0);
623*fae548d3Szrj     }
624*fae548d3Szrj }
625*fae548d3Szrj 
626*fae548d3Szrj /* Push an integer type.  */
627*fae548d3Szrj 
628*fae548d3Szrj static bfd_boolean
stab_int_type(void * p,unsigned int size,bfd_boolean unsignedp)629*fae548d3Szrj stab_int_type (void *p, unsigned int size, bfd_boolean unsignedp)
630*fae548d3Szrj {
631*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
632*fae548d3Szrj   long *cache;
633*fae548d3Szrj 
634*fae548d3Szrj   if (size <= 0 || (size > sizeof (long) && size != 8))
635*fae548d3Szrj     {
636*fae548d3Szrj       non_fatal (_("stab_int_type: bad size %u"), size);
637*fae548d3Szrj       return FALSE;
638*fae548d3Szrj     }
639*fae548d3Szrj 
640*fae548d3Szrj   if (unsignedp)
641*fae548d3Szrj     cache = info->type_cache.signed_integer_types;
642*fae548d3Szrj   else
643*fae548d3Szrj     cache = info->type_cache.unsigned_integer_types;
644*fae548d3Szrj 
645*fae548d3Szrj   if (cache[size - 1] != 0)
646*fae548d3Szrj     return stab_push_defined_type (info, cache[size - 1], size);
647*fae548d3Szrj   else
648*fae548d3Szrj     {
649*fae548d3Szrj       long tindex;
650*fae548d3Szrj       char buf[100];
651*fae548d3Szrj 
652*fae548d3Szrj       tindex = info->type_index;
653*fae548d3Szrj       ++info->type_index;
654*fae548d3Szrj 
655*fae548d3Szrj       cache[size - 1] = tindex;
656*fae548d3Szrj 
657*fae548d3Szrj       sprintf (buf, "%ld=r%ld;", tindex, tindex);
658*fae548d3Szrj       if (unsignedp)
659*fae548d3Szrj 	{
660*fae548d3Szrj 	  strcat (buf, "0;");
661*fae548d3Szrj 	  if (size < sizeof (long))
662*fae548d3Szrj 	    sprintf (buf + strlen (buf), "%ld;", ((long) 1 << (size * 8)) - 1);
663*fae548d3Szrj 	  else if (size == sizeof (long))
664*fae548d3Szrj 	    strcat (buf, "-1;");
665*fae548d3Szrj 	  else if (size == 8)
666*fae548d3Szrj 	    strcat (buf, "01777777777777777777777;");
667*fae548d3Szrj 	  else
668*fae548d3Szrj 	    abort ();
669*fae548d3Szrj 	}
670*fae548d3Szrj       else
671*fae548d3Szrj 	{
672*fae548d3Szrj 	  if (size <= sizeof (long))
673*fae548d3Szrj 	    sprintf (buf + strlen (buf), "%ld;%ld;",
674*fae548d3Szrj 		     (long) - ((unsigned long) 1 << (size * 8 - 1)),
675*fae548d3Szrj 		     (long) (((unsigned long) 1 << (size * 8 - 1)) - 1));
676*fae548d3Szrj 	  else if (size == 8)
677*fae548d3Szrj 	    strcat (buf, "01000000000000000000000;0777777777777777777777;");
678*fae548d3Szrj 	  else
679*fae548d3Szrj 	    abort ();
680*fae548d3Szrj 	}
681*fae548d3Szrj 
682*fae548d3Szrj       return stab_push_string (info, buf, tindex, TRUE, size);
683*fae548d3Szrj     }
684*fae548d3Szrj }
685*fae548d3Szrj 
686*fae548d3Szrj /* Push a floating point type.  */
687*fae548d3Szrj 
688*fae548d3Szrj static bfd_boolean
stab_float_type(void * p,unsigned int size)689*fae548d3Szrj stab_float_type (void *p, unsigned int size)
690*fae548d3Szrj {
691*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
692*fae548d3Szrj 
693*fae548d3Szrj   if (size > 0
694*fae548d3Szrj       && size - 1 < (sizeof info->type_cache.float_types
695*fae548d3Szrj 		     / sizeof info->type_cache.float_types[0])
696*fae548d3Szrj       && info->type_cache.float_types[size - 1] != 0)
697*fae548d3Szrj     return stab_push_defined_type (info,
698*fae548d3Szrj 				   info->type_cache.float_types[size - 1],
699*fae548d3Szrj 				   size);
700*fae548d3Szrj   else
701*fae548d3Szrj     {
702*fae548d3Szrj       long tindex;
703*fae548d3Szrj       char *int_type;
704*fae548d3Szrj       char buf[50];
705*fae548d3Szrj 
706*fae548d3Szrj       /* Floats are defined as a subrange of int.  */
707*fae548d3Szrj       if (! stab_int_type (info, 4, FALSE))
708*fae548d3Szrj 	return FALSE;
709*fae548d3Szrj       int_type = stab_pop_type (info);
710*fae548d3Szrj 
711*fae548d3Szrj       tindex = info->type_index;
712*fae548d3Szrj       ++info->type_index;
713*fae548d3Szrj 
714*fae548d3Szrj       if (size > 0
715*fae548d3Szrj 	  && size - 1 < (sizeof info->type_cache.float_types
716*fae548d3Szrj 			 / sizeof info->type_cache.float_types[0]))
717*fae548d3Szrj 	info->type_cache.float_types[size - 1] = tindex;
718*fae548d3Szrj 
719*fae548d3Szrj       sprintf (buf, "%ld=r%s;%u;0;", tindex, int_type, size);
720*fae548d3Szrj 
721*fae548d3Szrj       free (int_type);
722*fae548d3Szrj 
723*fae548d3Szrj       return stab_push_string (info, buf, tindex, TRUE, size);
724*fae548d3Szrj     }
725*fae548d3Szrj }
726*fae548d3Szrj 
727*fae548d3Szrj /* Push a complex type.  */
728*fae548d3Szrj 
729*fae548d3Szrj static bfd_boolean
stab_complex_type(void * p,unsigned int size)730*fae548d3Szrj stab_complex_type (void *p, unsigned int size)
731*fae548d3Szrj {
732*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
733*fae548d3Szrj   char buf[50];
734*fae548d3Szrj   long tindex;
735*fae548d3Szrj 
736*fae548d3Szrj   tindex = info->type_index;
737*fae548d3Szrj   ++info->type_index;
738*fae548d3Szrj 
739*fae548d3Szrj   sprintf (buf, "%ld=r%ld;%u;0;", tindex, tindex, size);
740*fae548d3Szrj 
741*fae548d3Szrj   return stab_push_string (info, buf, tindex, TRUE, size * 2);
742*fae548d3Szrj }
743*fae548d3Szrj 
744*fae548d3Szrj /* Push a bfd_boolean type.  We use an XCOFF predefined type, since gdb
745*fae548d3Szrj    always recognizes them.  */
746*fae548d3Szrj 
747*fae548d3Szrj static bfd_boolean
stab_bool_type(void * p,unsigned int size)748*fae548d3Szrj stab_bool_type (void *p, unsigned int size)
749*fae548d3Szrj {
750*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
751*fae548d3Szrj   long tindex;
752*fae548d3Szrj 
753*fae548d3Szrj   switch (size)
754*fae548d3Szrj     {
755*fae548d3Szrj     case 1:
756*fae548d3Szrj       tindex = -21;
757*fae548d3Szrj       break;
758*fae548d3Szrj 
759*fae548d3Szrj     case 2:
760*fae548d3Szrj       tindex = -22;
761*fae548d3Szrj       break;
762*fae548d3Szrj 
763*fae548d3Szrj     default:
764*fae548d3Szrj     case 4:
765*fae548d3Szrj       tindex = -16;
766*fae548d3Szrj       break;
767*fae548d3Szrj 
768*fae548d3Szrj     case 8:
769*fae548d3Szrj       tindex = -33;
770*fae548d3Szrj       break;
771*fae548d3Szrj     }
772*fae548d3Szrj 
773*fae548d3Szrj   return stab_push_defined_type (info, tindex, size);
774*fae548d3Szrj }
775*fae548d3Szrj 
776*fae548d3Szrj /* Push an enum type.  */
777*fae548d3Szrj 
778*fae548d3Szrj static bfd_boolean
stab_enum_type(void * p,const char * tag,const char ** names,bfd_signed_vma * vals)779*fae548d3Szrj stab_enum_type (void *p, const char *tag, const char **names,
780*fae548d3Szrj 		bfd_signed_vma *vals)
781*fae548d3Szrj {
782*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
783*fae548d3Szrj   size_t len;
784*fae548d3Szrj   const char **pn;
785*fae548d3Szrj   char *buf;
786*fae548d3Szrj   long tindex = 0;
787*fae548d3Szrj   bfd_signed_vma *pv;
788*fae548d3Szrj 
789*fae548d3Szrj   if (names == NULL)
790*fae548d3Szrj     {
791*fae548d3Szrj       assert (tag != NULL);
792*fae548d3Szrj 
793*fae548d3Szrj       buf = (char *) xmalloc (10 + strlen (tag));
794*fae548d3Szrj       sprintf (buf, "xe%s:", tag);
795*fae548d3Szrj       /* FIXME: The size is just a guess.  */
796*fae548d3Szrj       if (! stab_push_string (info, buf, 0, FALSE, 4))
797*fae548d3Szrj 	return FALSE;
798*fae548d3Szrj       free (buf);
799*fae548d3Szrj       return TRUE;
800*fae548d3Szrj     }
801*fae548d3Szrj 
802*fae548d3Szrj   len = 10;
803*fae548d3Szrj   if (tag != NULL)
804*fae548d3Szrj     len += strlen (tag);
805*fae548d3Szrj   for (pn = names; *pn != NULL; pn++)
806*fae548d3Szrj     len += strlen (*pn) + 20;
807*fae548d3Szrj 
808*fae548d3Szrj   buf = (char *) xmalloc (len);
809*fae548d3Szrj 
810*fae548d3Szrj   if (tag == NULL)
811*fae548d3Szrj     strcpy (buf, "e");
812*fae548d3Szrj   else
813*fae548d3Szrj     {
814*fae548d3Szrj       tindex = info->type_index;
815*fae548d3Szrj       ++info->type_index;
816*fae548d3Szrj       sprintf (buf, "%s:T%ld=e", tag, tindex);
817*fae548d3Szrj     }
818*fae548d3Szrj 
819*fae548d3Szrj   for (pn = names, pv = vals; *pn != NULL; pn++, pv++)
820*fae548d3Szrj     sprintf (buf + strlen (buf), "%s:%ld,", *pn, (long) *pv);
821*fae548d3Szrj   strcat (buf, ";");
822*fae548d3Szrj 
823*fae548d3Szrj   if (tag == NULL)
824*fae548d3Szrj     {
825*fae548d3Szrj       /* FIXME: The size is just a guess.  */
826*fae548d3Szrj       if (! stab_push_string (info, buf, 0, FALSE, 4))
827*fae548d3Szrj 	return FALSE;
828*fae548d3Szrj     }
829*fae548d3Szrj   else
830*fae548d3Szrj     {
831*fae548d3Szrj       /* FIXME: The size is just a guess.  */
832*fae548d3Szrj       if (! stab_write_symbol (info, N_LSYM, 0, 0, buf)
833*fae548d3Szrj 	  || ! stab_push_defined_type (info, tindex, 4))
834*fae548d3Szrj 	return FALSE;
835*fae548d3Szrj     }
836*fae548d3Szrj 
837*fae548d3Szrj   free (buf);
838*fae548d3Szrj 
839*fae548d3Szrj   return TRUE;
840*fae548d3Szrj }
841*fae548d3Szrj 
842*fae548d3Szrj /* Push a modification of the top type on the stack.  Cache the
843*fae548d3Szrj    results in CACHE and CACHE_ALLOC.  */
844*fae548d3Szrj 
845*fae548d3Szrj static bfd_boolean
stab_modify_type(struct stab_write_handle * info,int mod,unsigned int size,long ** cache,size_t * cache_alloc)846*fae548d3Szrj stab_modify_type (struct stab_write_handle *info, int mod,
847*fae548d3Szrj 		  unsigned int size, long **cache, size_t *cache_alloc)
848*fae548d3Szrj {
849*fae548d3Szrj   long targindex;
850*fae548d3Szrj   long tindex;
851*fae548d3Szrj   char *s, *buf;
852*fae548d3Szrj 
853*fae548d3Szrj   assert (info->type_stack != NULL);
854*fae548d3Szrj   targindex = info->type_stack->index;
855*fae548d3Szrj 
856*fae548d3Szrj   if (targindex <= 0
857*fae548d3Szrj       || cache == NULL)
858*fae548d3Szrj     {
859*fae548d3Szrj       bfd_boolean definition;
860*fae548d3Szrj 
861*fae548d3Szrj       /* Either the target type has no index, or we aren't caching
862*fae548d3Szrj          this modifier.  Either way we have no way of recording the
863*fae548d3Szrj          new type, so we don't bother to define one.  */
864*fae548d3Szrj       definition = info->type_stack->definition;
865*fae548d3Szrj       s = stab_pop_type (info);
866*fae548d3Szrj       buf = (char *) xmalloc (strlen (s) + 2);
867*fae548d3Szrj       sprintf (buf, "%c%s", mod, s);
868*fae548d3Szrj       free (s);
869*fae548d3Szrj       if (! stab_push_string (info, buf, 0, definition, size))
870*fae548d3Szrj 	return FALSE;
871*fae548d3Szrj       free (buf);
872*fae548d3Szrj     }
873*fae548d3Szrj   else
874*fae548d3Szrj     {
875*fae548d3Szrj       if ((size_t) targindex >= *cache_alloc)
876*fae548d3Szrj 	{
877*fae548d3Szrj 	  size_t alloc;
878*fae548d3Szrj 
879*fae548d3Szrj 	  alloc = *cache_alloc;
880*fae548d3Szrj 	  if (alloc == 0)
881*fae548d3Szrj 	    alloc = 10;
882*fae548d3Szrj 	  while ((size_t) targindex >= alloc)
883*fae548d3Szrj 	    alloc *= 2;
884*fae548d3Szrj 	  *cache = (long *) xrealloc (*cache, alloc * sizeof (long));
885*fae548d3Szrj 	  memset (*cache + *cache_alloc, 0,
886*fae548d3Szrj 		  (alloc - *cache_alloc) * sizeof (long));
887*fae548d3Szrj 	  *cache_alloc = alloc;
888*fae548d3Szrj 	}
889*fae548d3Szrj 
890*fae548d3Szrj       tindex = (*cache)[targindex];
891*fae548d3Szrj       if (tindex != 0 && ! info->type_stack->definition)
892*fae548d3Szrj 	{
893*fae548d3Szrj 	  /* We have already defined a modification of this type, and
894*fae548d3Szrj              the entry on the type stack is not a definition, so we
895*fae548d3Szrj              can safely discard it (we may have a definition on the
896*fae548d3Szrj              stack, even if we already defined a modification, if it
897*fae548d3Szrj              is a struct which we did not define at the time it was
898*fae548d3Szrj              referenced).  */
899*fae548d3Szrj 	  free (stab_pop_type (info));
900*fae548d3Szrj 	  if (! stab_push_defined_type (info, tindex, size))
901*fae548d3Szrj 	    return FALSE;
902*fae548d3Szrj 	}
903*fae548d3Szrj       else
904*fae548d3Szrj 	{
905*fae548d3Szrj 	  tindex = info->type_index;
906*fae548d3Szrj 	  ++info->type_index;
907*fae548d3Szrj 
908*fae548d3Szrj 	  s = stab_pop_type (info);
909*fae548d3Szrj 	  buf = (char *) xmalloc (strlen (s) + 20);
910*fae548d3Szrj 	  sprintf (buf, "%ld=%c%s", tindex, mod, s);
911*fae548d3Szrj 	  free (s);
912*fae548d3Szrj 
913*fae548d3Szrj 	  (*cache)[targindex] = tindex;
914*fae548d3Szrj 
915*fae548d3Szrj 	  if (! stab_push_string (info, buf, tindex, TRUE, size))
916*fae548d3Szrj 	    return FALSE;
917*fae548d3Szrj 
918*fae548d3Szrj 	  free (buf);
919*fae548d3Szrj 	}
920*fae548d3Szrj     }
921*fae548d3Szrj 
922*fae548d3Szrj   return TRUE;
923*fae548d3Szrj }
924*fae548d3Szrj 
925*fae548d3Szrj /* Push a pointer type.  */
926*fae548d3Szrj 
927*fae548d3Szrj static bfd_boolean
stab_pointer_type(void * p)928*fae548d3Szrj stab_pointer_type (void *p)
929*fae548d3Szrj {
930*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
931*fae548d3Szrj 
932*fae548d3Szrj   /* FIXME: The size should depend upon the architecture.  */
933*fae548d3Szrj   return stab_modify_type (info, '*', 4, &info->type_cache.pointer_types,
934*fae548d3Szrj 			   &info->type_cache.pointer_types_alloc);
935*fae548d3Szrj }
936*fae548d3Szrj 
937*fae548d3Szrj /* Push a function type.  */
938*fae548d3Szrj 
939*fae548d3Szrj static bfd_boolean
stab_function_type(void * p,int argcount,bfd_boolean varargs ATTRIBUTE_UNUSED)940*fae548d3Szrj stab_function_type (void *p, int argcount,
941*fae548d3Szrj 		    bfd_boolean varargs ATTRIBUTE_UNUSED)
942*fae548d3Szrj {
943*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
944*fae548d3Szrj   int i;
945*fae548d3Szrj 
946*fae548d3Szrj   /* We have no way to represent the argument types, so we just
947*fae548d3Szrj      discard them.  However, if they define new types, we must output
948*fae548d3Szrj      them.  We do this by producing empty typedefs.  */
949*fae548d3Szrj   for (i = 0; i < argcount; i++)
950*fae548d3Szrj     {
951*fae548d3Szrj       if (! info->type_stack->definition)
952*fae548d3Szrj 	free (stab_pop_type (info));
953*fae548d3Szrj       else
954*fae548d3Szrj 	{
955*fae548d3Szrj 	  char *s, *buf;
956*fae548d3Szrj 
957*fae548d3Szrj 	  s = stab_pop_type (info);
958*fae548d3Szrj 
959*fae548d3Szrj 	  buf = (char *) xmalloc (strlen (s) + 3);
960*fae548d3Szrj 	  sprintf (buf, ":t%s", s);
961*fae548d3Szrj 	  free (s);
962*fae548d3Szrj 
963*fae548d3Szrj 	  if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
964*fae548d3Szrj 	    return FALSE;
965*fae548d3Szrj 
966*fae548d3Szrj 	  free (buf);
967*fae548d3Szrj 	}
968*fae548d3Szrj     }
969*fae548d3Szrj 
970*fae548d3Szrj   return stab_modify_type (info, 'f', 0, &info->type_cache.function_types,
971*fae548d3Szrj 			   &info->type_cache.function_types_alloc);
972*fae548d3Szrj }
973*fae548d3Szrj 
974*fae548d3Szrj /* Push a reference type.  */
975*fae548d3Szrj 
976*fae548d3Szrj static bfd_boolean
stab_reference_type(void * p)977*fae548d3Szrj stab_reference_type (void *p)
978*fae548d3Szrj {
979*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
980*fae548d3Szrj 
981*fae548d3Szrj   /* FIXME: The size should depend upon the architecture.  */
982*fae548d3Szrj   return stab_modify_type (info, '&', 4, &info->type_cache.reference_types,
983*fae548d3Szrj 			   &info->type_cache.reference_types_alloc);
984*fae548d3Szrj }
985*fae548d3Szrj 
986*fae548d3Szrj /* Push a range type.  */
987*fae548d3Szrj 
988*fae548d3Szrj static bfd_boolean
stab_range_type(void * p,bfd_signed_vma low,bfd_signed_vma high)989*fae548d3Szrj stab_range_type (void *p, bfd_signed_vma low, bfd_signed_vma high)
990*fae548d3Szrj {
991*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
992*fae548d3Szrj   bfd_boolean definition;
993*fae548d3Szrj   unsigned int size;
994*fae548d3Szrj   char *s, *buf;
995*fae548d3Szrj 
996*fae548d3Szrj   definition = info->type_stack->definition;
997*fae548d3Szrj   size = info->type_stack->size;
998*fae548d3Szrj 
999*fae548d3Szrj   s = stab_pop_type (info);
1000*fae548d3Szrj   buf = (char *) xmalloc (strlen (s) + 100);
1001*fae548d3Szrj   sprintf (buf, "r%s;%ld;%ld;", s, (long) low, (long) high);
1002*fae548d3Szrj   free (s);
1003*fae548d3Szrj 
1004*fae548d3Szrj   if (! stab_push_string (info, buf, 0, definition, size))
1005*fae548d3Szrj     return FALSE;
1006*fae548d3Szrj 
1007*fae548d3Szrj   free (buf);
1008*fae548d3Szrj 
1009*fae548d3Szrj   return TRUE;
1010*fae548d3Szrj }
1011*fae548d3Szrj 
1012*fae548d3Szrj /* Push an array type.  */
1013*fae548d3Szrj 
1014*fae548d3Szrj static bfd_boolean
stab_array_type(void * p,bfd_signed_vma low,bfd_signed_vma high,bfd_boolean stringp)1015*fae548d3Szrj stab_array_type (void *p, bfd_signed_vma low, bfd_signed_vma high,
1016*fae548d3Szrj 		 bfd_boolean stringp)
1017*fae548d3Szrj {
1018*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
1019*fae548d3Szrj   bfd_boolean definition;
1020*fae548d3Szrj   unsigned int element_size;
1021*fae548d3Szrj   char *range, *element, *buf;
1022*fae548d3Szrj   long tindex;
1023*fae548d3Szrj   unsigned int size;
1024*fae548d3Szrj 
1025*fae548d3Szrj   definition = info->type_stack->definition;
1026*fae548d3Szrj   range = stab_pop_type (info);
1027*fae548d3Szrj 
1028*fae548d3Szrj   definition = definition || info->type_stack->definition;
1029*fae548d3Szrj   element_size = info->type_stack->size;
1030*fae548d3Szrj   element = stab_pop_type (info);
1031*fae548d3Szrj 
1032*fae548d3Szrj   buf = (char *) xmalloc (strlen (range) + strlen (element) + 100);
1033*fae548d3Szrj 
1034*fae548d3Szrj   if (! stringp)
1035*fae548d3Szrj     {
1036*fae548d3Szrj       tindex = 0;
1037*fae548d3Szrj       *buf = '\0';
1038*fae548d3Szrj     }
1039*fae548d3Szrj   else
1040*fae548d3Szrj     {
1041*fae548d3Szrj       /* We need to define a type in order to include the string
1042*fae548d3Szrj          attribute.  */
1043*fae548d3Szrj       tindex = info->type_index;
1044*fae548d3Szrj       ++info->type_index;
1045*fae548d3Szrj       definition = TRUE;
1046*fae548d3Szrj       sprintf (buf, "%ld=@S;", tindex);
1047*fae548d3Szrj     }
1048*fae548d3Szrj 
1049*fae548d3Szrj   sprintf (buf + strlen (buf), "ar%s;%ld;%ld;%s",
1050*fae548d3Szrj 	   range, (long) low, (long) high, element);
1051*fae548d3Szrj   free (range);
1052*fae548d3Szrj   free (element);
1053*fae548d3Szrj 
1054*fae548d3Szrj   if (high < low)
1055*fae548d3Szrj     size = 0;
1056*fae548d3Szrj   else
1057*fae548d3Szrj     size = element_size * ((high - low) + 1);
1058*fae548d3Szrj   if (! stab_push_string (info, buf, tindex, definition, size))
1059*fae548d3Szrj     return FALSE;
1060*fae548d3Szrj 
1061*fae548d3Szrj   free (buf);
1062*fae548d3Szrj 
1063*fae548d3Szrj   return TRUE;
1064*fae548d3Szrj }
1065*fae548d3Szrj 
1066*fae548d3Szrj /* Push a set type.  */
1067*fae548d3Szrj 
1068*fae548d3Szrj static bfd_boolean
stab_set_type(void * p,bfd_boolean bitstringp)1069*fae548d3Szrj stab_set_type (void *p, bfd_boolean bitstringp)
1070*fae548d3Szrj {
1071*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
1072*fae548d3Szrj   bfd_boolean definition;
1073*fae548d3Szrj   char *s, *buf;
1074*fae548d3Szrj   long tindex;
1075*fae548d3Szrj 
1076*fae548d3Szrj   definition = info->type_stack->definition;
1077*fae548d3Szrj 
1078*fae548d3Szrj   s = stab_pop_type (info);
1079*fae548d3Szrj   buf = (char *) xmalloc (strlen (s) + 30);
1080*fae548d3Szrj 
1081*fae548d3Szrj   if (! bitstringp)
1082*fae548d3Szrj     {
1083*fae548d3Szrj       *buf = '\0';
1084*fae548d3Szrj       tindex = 0;
1085*fae548d3Szrj     }
1086*fae548d3Szrj   else
1087*fae548d3Szrj     {
1088*fae548d3Szrj       /* We need to define a type in order to include the string
1089*fae548d3Szrj          attribute.  */
1090*fae548d3Szrj       tindex = info->type_index;
1091*fae548d3Szrj       ++info->type_index;
1092*fae548d3Szrj       definition = TRUE;
1093*fae548d3Szrj       sprintf (buf, "%ld=@S;", tindex);
1094*fae548d3Szrj     }
1095*fae548d3Szrj 
1096*fae548d3Szrj   sprintf (buf + strlen (buf), "S%s", s);
1097*fae548d3Szrj   free (s);
1098*fae548d3Szrj 
1099*fae548d3Szrj   if (! stab_push_string (info, buf, tindex, definition, 0))
1100*fae548d3Szrj     return FALSE;
1101*fae548d3Szrj 
1102*fae548d3Szrj   free (buf);
1103*fae548d3Szrj 
1104*fae548d3Szrj   return TRUE;
1105*fae548d3Szrj }
1106*fae548d3Szrj 
1107*fae548d3Szrj /* Push an offset type.  */
1108*fae548d3Szrj 
1109*fae548d3Szrj static bfd_boolean
stab_offset_type(void * p)1110*fae548d3Szrj stab_offset_type (void *p)
1111*fae548d3Szrj {
1112*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
1113*fae548d3Szrj   bfd_boolean definition;
1114*fae548d3Szrj   char *target, *base, *buf;
1115*fae548d3Szrj 
1116*fae548d3Szrj   definition = info->type_stack->definition;
1117*fae548d3Szrj   target = stab_pop_type (info);
1118*fae548d3Szrj 
1119*fae548d3Szrj   definition = definition || info->type_stack->definition;
1120*fae548d3Szrj   base = stab_pop_type (info);
1121*fae548d3Szrj 
1122*fae548d3Szrj   buf = (char *) xmalloc (strlen (target) + strlen (base) + 3);
1123*fae548d3Szrj   sprintf (buf, "@%s,%s", base, target);
1124*fae548d3Szrj   free (base);
1125*fae548d3Szrj   free (target);
1126*fae548d3Szrj 
1127*fae548d3Szrj   if (! stab_push_string (info, buf, 0, definition, 0))
1128*fae548d3Szrj     return FALSE;
1129*fae548d3Szrj 
1130*fae548d3Szrj   free (buf);
1131*fae548d3Szrj 
1132*fae548d3Szrj   return TRUE;
1133*fae548d3Szrj }
1134*fae548d3Szrj 
1135*fae548d3Szrj /* Push a method type.  */
1136*fae548d3Szrj 
1137*fae548d3Szrj static bfd_boolean
stab_method_type(void * p,bfd_boolean domainp,int argcount,bfd_boolean varargs)1138*fae548d3Szrj stab_method_type (void *p, bfd_boolean domainp, int argcount,
1139*fae548d3Szrj 		  bfd_boolean varargs)
1140*fae548d3Szrj {
1141*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
1142*fae548d3Szrj   bfd_boolean definition;
1143*fae548d3Szrj   char *domain, *return_type, *buf;
1144*fae548d3Szrj   char **args;
1145*fae548d3Szrj   int i;
1146*fae548d3Szrj   size_t len;
1147*fae548d3Szrj 
1148*fae548d3Szrj   /* We don't bother with stub method types, because that would
1149*fae548d3Szrj      require a mangler for C++ argument types.  This will waste space
1150*fae548d3Szrj      in the debugging output.  */
1151*fae548d3Szrj 
1152*fae548d3Szrj   /* We need a domain.  I'm not sure DOMAINP can ever be false,
1153*fae548d3Szrj      anyhow.  */
1154*fae548d3Szrj   if (! domainp)
1155*fae548d3Szrj     {
1156*fae548d3Szrj       if (! stab_empty_type (p))
1157*fae548d3Szrj 	return FALSE;
1158*fae548d3Szrj     }
1159*fae548d3Szrj 
1160*fae548d3Szrj   definition = info->type_stack->definition;
1161*fae548d3Szrj   domain = stab_pop_type (info);
1162*fae548d3Szrj 
1163*fae548d3Szrj   /* A non-varargs function is indicated by making the last parameter
1164*fae548d3Szrj      type be void.  */
1165*fae548d3Szrj 
1166*fae548d3Szrj   if (argcount < 0)
1167*fae548d3Szrj     {
1168*fae548d3Szrj       args = NULL;
1169*fae548d3Szrj       argcount = 0;
1170*fae548d3Szrj     }
1171*fae548d3Szrj   else if (argcount == 0)
1172*fae548d3Szrj     {
1173*fae548d3Szrj       if (varargs)
1174*fae548d3Szrj 	args = NULL;
1175*fae548d3Szrj       else
1176*fae548d3Szrj 	{
1177*fae548d3Szrj 	  args = (char **) xmalloc (1 * sizeof (*args));
1178*fae548d3Szrj 	  if (! stab_empty_type (p))
1179*fae548d3Szrj 	    return FALSE;
1180*fae548d3Szrj 	  definition = definition || info->type_stack->definition;
1181*fae548d3Szrj 	  args[0] = stab_pop_type (info);
1182*fae548d3Szrj 	  argcount = 1;
1183*fae548d3Szrj 	}
1184*fae548d3Szrj     }
1185*fae548d3Szrj   else
1186*fae548d3Szrj     {
1187*fae548d3Szrj       args = (char **) xmalloc ((argcount + 1) * sizeof (*args));
1188*fae548d3Szrj       for (i = argcount - 1; i >= 0; i--)
1189*fae548d3Szrj 	{
1190*fae548d3Szrj 	  definition = definition || info->type_stack->definition;
1191*fae548d3Szrj 	  args[i] = stab_pop_type (info);
1192*fae548d3Szrj 	}
1193*fae548d3Szrj       if (! varargs)
1194*fae548d3Szrj 	{
1195*fae548d3Szrj 	  if (! stab_empty_type (p))
1196*fae548d3Szrj 	    return FALSE;
1197*fae548d3Szrj 	  definition = definition || info->type_stack->definition;
1198*fae548d3Szrj 	  args[argcount] = stab_pop_type (info);
1199*fae548d3Szrj 	  ++argcount;
1200*fae548d3Szrj 	}
1201*fae548d3Szrj     }
1202*fae548d3Szrj 
1203*fae548d3Szrj   definition = definition || info->type_stack->definition;
1204*fae548d3Szrj   return_type = stab_pop_type (info);
1205*fae548d3Szrj 
1206*fae548d3Szrj   len = strlen (domain) + strlen (return_type) + 10;
1207*fae548d3Szrj   for (i = 0; i < argcount; i++)
1208*fae548d3Szrj     len += strlen (args[i]);
1209*fae548d3Szrj 
1210*fae548d3Szrj   buf = (char *) xmalloc (len);
1211*fae548d3Szrj 
1212*fae548d3Szrj   sprintf (buf, "#%s,%s", domain, return_type);
1213*fae548d3Szrj   free (domain);
1214*fae548d3Szrj   free (return_type);
1215*fae548d3Szrj   for (i = 0; i < argcount; i++)
1216*fae548d3Szrj     {
1217*fae548d3Szrj       strcat (buf, ",");
1218*fae548d3Szrj       strcat (buf, args[i]);
1219*fae548d3Szrj       free (args[i]);
1220*fae548d3Szrj     }
1221*fae548d3Szrj   strcat (buf, ";");
1222*fae548d3Szrj 
1223*fae548d3Szrj   if (args != NULL)
1224*fae548d3Szrj     free (args);
1225*fae548d3Szrj 
1226*fae548d3Szrj   if (! stab_push_string (info, buf, 0, definition, 0))
1227*fae548d3Szrj     return FALSE;
1228*fae548d3Szrj 
1229*fae548d3Szrj   free (buf);
1230*fae548d3Szrj 
1231*fae548d3Szrj   return TRUE;
1232*fae548d3Szrj }
1233*fae548d3Szrj 
1234*fae548d3Szrj /* Push a const version of a type.  */
1235*fae548d3Szrj 
1236*fae548d3Szrj static bfd_boolean
stab_const_type(void * p)1237*fae548d3Szrj stab_const_type (void *p)
1238*fae548d3Szrj {
1239*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
1240*fae548d3Szrj 
1241*fae548d3Szrj   return stab_modify_type (info, 'k', info->type_stack->size,
1242*fae548d3Szrj 			   (long **) NULL, (size_t *) NULL);
1243*fae548d3Szrj }
1244*fae548d3Szrj 
1245*fae548d3Szrj /* Push a volatile version of a type.  */
1246*fae548d3Szrj 
1247*fae548d3Szrj static bfd_boolean
stab_volatile_type(void * p)1248*fae548d3Szrj stab_volatile_type (void *p)
1249*fae548d3Szrj {
1250*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
1251*fae548d3Szrj 
1252*fae548d3Szrj   return stab_modify_type (info, 'B', info->type_stack->size,
1253*fae548d3Szrj 			   (long **) NULL, (size_t *) NULL);
1254*fae548d3Szrj }
1255*fae548d3Szrj 
1256*fae548d3Szrj /* Get the type index to use for a struct/union/class ID.  This should
1257*fae548d3Szrj    return -1 if it fails.  */
1258*fae548d3Szrj 
1259*fae548d3Szrj static long
stab_get_struct_index(struct stab_write_handle * info,const char * tag,unsigned int id,enum debug_type_kind kind,unsigned int * psize)1260*fae548d3Szrj stab_get_struct_index (struct stab_write_handle *info, const char *tag,
1261*fae548d3Szrj 		       unsigned int id, enum debug_type_kind kind,
1262*fae548d3Szrj 		       unsigned int *psize)
1263*fae548d3Szrj {
1264*fae548d3Szrj   if (id >= info->type_cache.struct_types_alloc)
1265*fae548d3Szrj     {
1266*fae548d3Szrj       size_t alloc;
1267*fae548d3Szrj 
1268*fae548d3Szrj       alloc = info->type_cache.struct_types_alloc;
1269*fae548d3Szrj       if (alloc == 0)
1270*fae548d3Szrj 	alloc = 10;
1271*fae548d3Szrj       while (id >= alloc)
1272*fae548d3Szrj 	alloc *= 2;
1273*fae548d3Szrj       info->type_cache.struct_types =
1274*fae548d3Szrj 	(struct stab_tag *) xrealloc (info->type_cache.struct_types,
1275*fae548d3Szrj 				      alloc * sizeof (struct stab_tag));
1276*fae548d3Szrj       memset ((info->type_cache.struct_types
1277*fae548d3Szrj 	       + info->type_cache.struct_types_alloc),
1278*fae548d3Szrj 	      0,
1279*fae548d3Szrj 	      ((alloc - info->type_cache.struct_types_alloc)
1280*fae548d3Szrj 	       * sizeof (struct stab_tag)));
1281*fae548d3Szrj       info->type_cache.struct_types_alloc = alloc;
1282*fae548d3Szrj     }
1283*fae548d3Szrj 
1284*fae548d3Szrj   if (info->type_cache.struct_types[id].index == 0)
1285*fae548d3Szrj     {
1286*fae548d3Szrj       info->type_cache.struct_types[id].index = info->type_index;
1287*fae548d3Szrj       ++info->type_index;
1288*fae548d3Szrj       info->type_cache.struct_types[id].tag = tag;
1289*fae548d3Szrj       info->type_cache.struct_types[id].kind = kind;
1290*fae548d3Szrj     }
1291*fae548d3Szrj 
1292*fae548d3Szrj   if (kind == DEBUG_KIND_ILLEGAL)
1293*fae548d3Szrj     {
1294*fae548d3Szrj       /* This is a definition of the struct.  */
1295*fae548d3Szrj       info->type_cache.struct_types[id].kind = kind;
1296*fae548d3Szrj       info->type_cache.struct_types[id].size = *psize;
1297*fae548d3Szrj     }
1298*fae548d3Szrj   else
1299*fae548d3Szrj     *psize = info->type_cache.struct_types[id].size;
1300*fae548d3Szrj 
1301*fae548d3Szrj   return info->type_cache.struct_types[id].index;
1302*fae548d3Szrj }
1303*fae548d3Szrj 
1304*fae548d3Szrj /* Start outputting a struct.  We ignore the tag, and handle it in
1305*fae548d3Szrj    stab_tag.  */
1306*fae548d3Szrj 
1307*fae548d3Szrj static bfd_boolean
stab_start_struct_type(void * p,const char * tag,unsigned int id,bfd_boolean structp,unsigned int size)1308*fae548d3Szrj stab_start_struct_type (void *p, const char *tag, unsigned int id,
1309*fae548d3Szrj 			bfd_boolean structp, unsigned int size)
1310*fae548d3Szrj {
1311*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
1312*fae548d3Szrj   long tindex;
1313*fae548d3Szrj   bfd_boolean definition;
1314*fae548d3Szrj   char buf[40];
1315*fae548d3Szrj 
1316*fae548d3Szrj   if (id == 0)
1317*fae548d3Szrj     {
1318*fae548d3Szrj       tindex = 0;
1319*fae548d3Szrj       *buf = '\0';
1320*fae548d3Szrj       definition = FALSE;
1321*fae548d3Szrj     }
1322*fae548d3Szrj   else
1323*fae548d3Szrj     {
1324*fae548d3Szrj       tindex = stab_get_struct_index (info, tag, id, DEBUG_KIND_ILLEGAL,
1325*fae548d3Szrj 				     &size);
1326*fae548d3Szrj       if (tindex < 0)
1327*fae548d3Szrj 	return FALSE;
1328*fae548d3Szrj       sprintf (buf, "%ld=", tindex);
1329*fae548d3Szrj       definition = TRUE;
1330*fae548d3Szrj     }
1331*fae548d3Szrj 
1332*fae548d3Szrj   sprintf (buf + strlen (buf), "%c%u",
1333*fae548d3Szrj 	   structp ? 's' : 'u',
1334*fae548d3Szrj 	   size);
1335*fae548d3Szrj 
1336*fae548d3Szrj   if (! stab_push_string (info, buf, tindex, definition, size))
1337*fae548d3Szrj     return FALSE;
1338*fae548d3Szrj 
1339*fae548d3Szrj   info->type_stack->fields = (char *) xmalloc (1);
1340*fae548d3Szrj   info->type_stack->fields[0] = '\0';
1341*fae548d3Szrj 
1342*fae548d3Szrj   return TRUE;
1343*fae548d3Szrj }
1344*fae548d3Szrj 
1345*fae548d3Szrj /* Add a field to a struct.  */
1346*fae548d3Szrj 
1347*fae548d3Szrj static bfd_boolean
stab_struct_field(void * p,const char * name,bfd_vma bitpos,bfd_vma bitsize,enum debug_visibility visibility)1348*fae548d3Szrj stab_struct_field (void *p, const char *name, bfd_vma bitpos,
1349*fae548d3Szrj 		   bfd_vma bitsize, enum debug_visibility visibility)
1350*fae548d3Szrj {
1351*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
1352*fae548d3Szrj   bfd_boolean definition;
1353*fae548d3Szrj   unsigned int size;
1354*fae548d3Szrj   char *s, *n;
1355*fae548d3Szrj   const char *vis;
1356*fae548d3Szrj 
1357*fae548d3Szrj   definition = info->type_stack->definition;
1358*fae548d3Szrj   size = info->type_stack->size;
1359*fae548d3Szrj   s = stab_pop_type (info);
1360*fae548d3Szrj 
1361*fae548d3Szrj   /* Add this field to the end of the current struct fields, which is
1362*fae548d3Szrj      currently on the top of the stack.  */
1363*fae548d3Szrj 
1364*fae548d3Szrj   assert (info->type_stack->fields != NULL);
1365*fae548d3Szrj   n = (char *) xmalloc (strlen (info->type_stack->fields)
1366*fae548d3Szrj 			+ strlen (name)
1367*fae548d3Szrj 			+ strlen (s)
1368*fae548d3Szrj 			+ 50);
1369*fae548d3Szrj 
1370*fae548d3Szrj   switch (visibility)
1371*fae548d3Szrj     {
1372*fae548d3Szrj     default:
1373*fae548d3Szrj       abort ();
1374*fae548d3Szrj 
1375*fae548d3Szrj     case DEBUG_VISIBILITY_PUBLIC:
1376*fae548d3Szrj       vis = "";
1377*fae548d3Szrj       break;
1378*fae548d3Szrj 
1379*fae548d3Szrj     case DEBUG_VISIBILITY_PRIVATE:
1380*fae548d3Szrj       vis = "/0";
1381*fae548d3Szrj       break;
1382*fae548d3Szrj 
1383*fae548d3Szrj     case DEBUG_VISIBILITY_PROTECTED:
1384*fae548d3Szrj       vis = "/1";
1385*fae548d3Szrj       break;
1386*fae548d3Szrj     }
1387*fae548d3Szrj 
1388*fae548d3Szrj   if (bitsize == 0)
1389*fae548d3Szrj     {
1390*fae548d3Szrj       bitsize = size * 8;
1391*fae548d3Szrj       if (bitsize == 0)
1392*fae548d3Szrj 	non_fatal (_("%s: warning: unknown size for field `%s' in struct"),
1393*fae548d3Szrj 		   bfd_get_filename (info->abfd), name);
1394*fae548d3Szrj     }
1395*fae548d3Szrj 
1396*fae548d3Szrj   sprintf (n, "%s%s:%s%s,%ld,%ld;", info->type_stack->fields, name, vis, s,
1397*fae548d3Szrj 	   (long) bitpos, (long) bitsize);
1398*fae548d3Szrj 
1399*fae548d3Szrj   free (info->type_stack->fields);
1400*fae548d3Szrj   info->type_stack->fields = n;
1401*fae548d3Szrj 
1402*fae548d3Szrj   if (definition)
1403*fae548d3Szrj     info->type_stack->definition = TRUE;
1404*fae548d3Szrj 
1405*fae548d3Szrj   return TRUE;
1406*fae548d3Szrj }
1407*fae548d3Szrj 
1408*fae548d3Szrj /* Finish up a struct.  */
1409*fae548d3Szrj 
1410*fae548d3Szrj static bfd_boolean
stab_end_struct_type(void * p)1411*fae548d3Szrj stab_end_struct_type (void *p)
1412*fae548d3Szrj {
1413*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
1414*fae548d3Szrj   bfd_boolean definition;
1415*fae548d3Szrj   long tindex;
1416*fae548d3Szrj   unsigned int size;
1417*fae548d3Szrj   char *fields, *first, *buf;
1418*fae548d3Szrj 
1419*fae548d3Szrj   assert (info->type_stack != NULL && info->type_stack->fields != NULL);
1420*fae548d3Szrj 
1421*fae548d3Szrj   definition = info->type_stack->definition;
1422*fae548d3Szrj   tindex = info->type_stack->index;
1423*fae548d3Szrj   size = info->type_stack->size;
1424*fae548d3Szrj   fields = info->type_stack->fields;
1425*fae548d3Szrj   first = stab_pop_type (info);
1426*fae548d3Szrj 
1427*fae548d3Szrj   buf = (char *) xmalloc (strlen (first) + strlen (fields) + 2);
1428*fae548d3Szrj   sprintf (buf, "%s%s;", first, fields);
1429*fae548d3Szrj   free (first);
1430*fae548d3Szrj   free (fields);
1431*fae548d3Szrj 
1432*fae548d3Szrj   if (! stab_push_string (info, buf, tindex, definition, size))
1433*fae548d3Szrj     return FALSE;
1434*fae548d3Szrj 
1435*fae548d3Szrj   free (buf);
1436*fae548d3Szrj 
1437*fae548d3Szrj   return TRUE;
1438*fae548d3Szrj }
1439*fae548d3Szrj 
1440*fae548d3Szrj /* Start outputting a class.  */
1441*fae548d3Szrj 
1442*fae548d3Szrj static bfd_boolean
stab_start_class_type(void * p,const char * tag,unsigned int id,bfd_boolean structp,unsigned int size,bfd_boolean vptr,bfd_boolean ownvptr)1443*fae548d3Szrj stab_start_class_type (void *p, const char *tag, unsigned int id,
1444*fae548d3Szrj 		       bfd_boolean structp, unsigned int size,
1445*fae548d3Szrj 		       bfd_boolean vptr, bfd_boolean ownvptr)
1446*fae548d3Szrj {
1447*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
1448*fae548d3Szrj   bfd_boolean definition = FALSE;
1449*fae548d3Szrj   char *vstring = NULL;
1450*fae548d3Szrj 
1451*fae548d3Szrj   if (vptr && !ownvptr)
1452*fae548d3Szrj     {
1453*fae548d3Szrj       definition = info->type_stack->definition;
1454*fae548d3Szrj       vstring = stab_pop_type (info);
1455*fae548d3Szrj     }
1456*fae548d3Szrj 
1457*fae548d3Szrj   if (! stab_start_struct_type (p, tag, id, structp, size))
1458*fae548d3Szrj     return FALSE;
1459*fae548d3Szrj 
1460*fae548d3Szrj   if (vptr)
1461*fae548d3Szrj     {
1462*fae548d3Szrj       char *vtable;
1463*fae548d3Szrj 
1464*fae548d3Szrj       if (ownvptr)
1465*fae548d3Szrj 	{
1466*fae548d3Szrj 	  assert (info->type_stack->index > 0);
1467*fae548d3Szrj 	  vtable = (char *) xmalloc (20);
1468*fae548d3Szrj 	  sprintf (vtable, "~%%%ld", info->type_stack->index);
1469*fae548d3Szrj 	}
1470*fae548d3Szrj       else
1471*fae548d3Szrj 	{
1472*fae548d3Szrj 	  assert (vstring);
1473*fae548d3Szrj 	  vtable = (char *) xmalloc (strlen (vstring) + 3);
1474*fae548d3Szrj 	  sprintf (vtable, "~%%%s", vstring);
1475*fae548d3Szrj 	  free (vstring);
1476*fae548d3Szrj 	  if (definition)
1477*fae548d3Szrj 	    info->type_stack->definition = TRUE;
1478*fae548d3Szrj 	}
1479*fae548d3Szrj       info->type_stack->vtable = vtable;
1480*fae548d3Szrj     }
1481*fae548d3Szrj 
1482*fae548d3Szrj   return TRUE;
1483*fae548d3Szrj }
1484*fae548d3Szrj 
1485*fae548d3Szrj /* Add a static member to the class on the type stack.  */
1486*fae548d3Szrj 
1487*fae548d3Szrj static bfd_boolean
stab_class_static_member(void * p,const char * name,const char * physname,enum debug_visibility visibility)1488*fae548d3Szrj stab_class_static_member (void *p, const char *name, const char *physname,
1489*fae548d3Szrj 			  enum debug_visibility visibility)
1490*fae548d3Szrj {
1491*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
1492*fae548d3Szrj   bfd_boolean definition;
1493*fae548d3Szrj   char *s, *n;
1494*fae548d3Szrj   const char *vis;
1495*fae548d3Szrj 
1496*fae548d3Szrj   definition = info->type_stack->definition;
1497*fae548d3Szrj   s = stab_pop_type (info);
1498*fae548d3Szrj 
1499*fae548d3Szrj   /* Add this field to the end of the current struct fields, which is
1500*fae548d3Szrj      currently on the top of the stack.  */
1501*fae548d3Szrj 
1502*fae548d3Szrj   assert (info->type_stack->fields != NULL);
1503*fae548d3Szrj   n = (char *) xmalloc (strlen (info->type_stack->fields)
1504*fae548d3Szrj 			+ strlen (name)
1505*fae548d3Szrj 			+ strlen (s)
1506*fae548d3Szrj 			+ strlen (physname)
1507*fae548d3Szrj 			+ 10);
1508*fae548d3Szrj 
1509*fae548d3Szrj   switch (visibility)
1510*fae548d3Szrj     {
1511*fae548d3Szrj     default:
1512*fae548d3Szrj       abort ();
1513*fae548d3Szrj 
1514*fae548d3Szrj     case DEBUG_VISIBILITY_PUBLIC:
1515*fae548d3Szrj       vis = "";
1516*fae548d3Szrj       break;
1517*fae548d3Szrj 
1518*fae548d3Szrj     case DEBUG_VISIBILITY_PRIVATE:
1519*fae548d3Szrj       vis = "/0";
1520*fae548d3Szrj       break;
1521*fae548d3Szrj 
1522*fae548d3Szrj     case DEBUG_VISIBILITY_PROTECTED:
1523*fae548d3Szrj       vis = "/1";
1524*fae548d3Szrj       break;
1525*fae548d3Szrj     }
1526*fae548d3Szrj 
1527*fae548d3Szrj   sprintf (n, "%s%s:%s%s:%s;", info->type_stack->fields, name, vis, s,
1528*fae548d3Szrj 	   physname);
1529*fae548d3Szrj 
1530*fae548d3Szrj   free (info->type_stack->fields);
1531*fae548d3Szrj   info->type_stack->fields = n;
1532*fae548d3Szrj 
1533*fae548d3Szrj   if (definition)
1534*fae548d3Szrj     info->type_stack->definition = TRUE;
1535*fae548d3Szrj 
1536*fae548d3Szrj   return TRUE;
1537*fae548d3Szrj }
1538*fae548d3Szrj 
1539*fae548d3Szrj /* Add a base class to the class on the type stack.  */
1540*fae548d3Szrj 
1541*fae548d3Szrj static bfd_boolean
stab_class_baseclass(void * p,bfd_vma bitpos,bfd_boolean is_virtual,enum debug_visibility visibility)1542*fae548d3Szrj stab_class_baseclass (void *p, bfd_vma bitpos, bfd_boolean is_virtual,
1543*fae548d3Szrj 		      enum debug_visibility visibility)
1544*fae548d3Szrj {
1545*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
1546*fae548d3Szrj   bfd_boolean definition;
1547*fae548d3Szrj   char *s;
1548*fae548d3Szrj   char *buf;
1549*fae548d3Szrj   unsigned int c;
1550*fae548d3Szrj   char **baseclasses;
1551*fae548d3Szrj 
1552*fae548d3Szrj   definition = info->type_stack->definition;
1553*fae548d3Szrj   s = stab_pop_type (info);
1554*fae548d3Szrj 
1555*fae548d3Szrj   /* Build the base class specifier.  */
1556*fae548d3Szrj 
1557*fae548d3Szrj   buf = (char *) xmalloc (strlen (s) + 25);
1558*fae548d3Szrj   buf[0] = is_virtual ? '1' : '0';
1559*fae548d3Szrj   switch (visibility)
1560*fae548d3Szrj     {
1561*fae548d3Szrj     default:
1562*fae548d3Szrj       abort ();
1563*fae548d3Szrj 
1564*fae548d3Szrj     case DEBUG_VISIBILITY_PRIVATE:
1565*fae548d3Szrj       buf[1] = '0';
1566*fae548d3Szrj       break;
1567*fae548d3Szrj 
1568*fae548d3Szrj     case DEBUG_VISIBILITY_PROTECTED:
1569*fae548d3Szrj       buf[1] = '1';
1570*fae548d3Szrj       break;
1571*fae548d3Szrj 
1572*fae548d3Szrj     case DEBUG_VISIBILITY_PUBLIC:
1573*fae548d3Szrj       buf[1] = '2';
1574*fae548d3Szrj       break;
1575*fae548d3Szrj     }
1576*fae548d3Szrj 
1577*fae548d3Szrj   sprintf (buf + 2, "%ld,%s;", (long) bitpos, s);
1578*fae548d3Szrj   free (s);
1579*fae548d3Szrj 
1580*fae548d3Szrj   /* Add the new baseclass to the existing ones.  */
1581*fae548d3Szrj 
1582*fae548d3Szrj   assert (info->type_stack != NULL && info->type_stack->fields != NULL);
1583*fae548d3Szrj 
1584*fae548d3Szrj   if (info->type_stack->baseclasses == NULL)
1585*fae548d3Szrj     c = 0;
1586*fae548d3Szrj   else
1587*fae548d3Szrj     {
1588*fae548d3Szrj       c = 0;
1589*fae548d3Szrj       while (info->type_stack->baseclasses[c] != NULL)
1590*fae548d3Szrj 	++c;
1591*fae548d3Szrj     }
1592*fae548d3Szrj 
1593*fae548d3Szrj   baseclasses = (char **) xrealloc (info->type_stack->baseclasses,
1594*fae548d3Szrj 				    (c + 2) * sizeof (*baseclasses));
1595*fae548d3Szrj   baseclasses[c] = buf;
1596*fae548d3Szrj   baseclasses[c + 1] = NULL;
1597*fae548d3Szrj 
1598*fae548d3Szrj   info->type_stack->baseclasses = baseclasses;
1599*fae548d3Szrj 
1600*fae548d3Szrj   if (definition)
1601*fae548d3Szrj     info->type_stack->definition = TRUE;
1602*fae548d3Szrj 
1603*fae548d3Szrj   return TRUE;
1604*fae548d3Szrj }
1605*fae548d3Szrj 
1606*fae548d3Szrj /* Start adding a method to the class on the type stack.  */
1607*fae548d3Szrj 
1608*fae548d3Szrj static bfd_boolean
stab_class_start_method(void * p,const char * name)1609*fae548d3Szrj stab_class_start_method (void *p, const char *name)
1610*fae548d3Szrj {
1611*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
1612*fae548d3Szrj   char *m;
1613*fae548d3Szrj 
1614*fae548d3Szrj   assert (info->type_stack != NULL && info->type_stack->fields != NULL);
1615*fae548d3Szrj 
1616*fae548d3Szrj   if (info->type_stack->methods == NULL)
1617*fae548d3Szrj     {
1618*fae548d3Szrj       m = (char *) xmalloc (strlen (name) + 3);
1619*fae548d3Szrj       *m = '\0';
1620*fae548d3Szrj     }
1621*fae548d3Szrj   else
1622*fae548d3Szrj     {
1623*fae548d3Szrj       m = (char *) xrealloc (info->type_stack->methods,
1624*fae548d3Szrj 			     (strlen (info->type_stack->methods)
1625*fae548d3Szrj 			      + strlen (name)
1626*fae548d3Szrj 			      + 4));
1627*fae548d3Szrj     }
1628*fae548d3Szrj 
1629*fae548d3Szrj   sprintf (m + strlen (m), "%s::", name);
1630*fae548d3Szrj 
1631*fae548d3Szrj   info->type_stack->methods = m;
1632*fae548d3Szrj 
1633*fae548d3Szrj   return TRUE;
1634*fae548d3Szrj }
1635*fae548d3Szrj 
1636*fae548d3Szrj /* Add a variant, either static or not, to the current method.  */
1637*fae548d3Szrj 
1638*fae548d3Szrj static bfd_boolean
stab_class_method_var(struct stab_write_handle * info,const char * physname,enum debug_visibility visibility,bfd_boolean staticp,bfd_boolean constp,bfd_boolean volatilep,bfd_vma voffset,bfd_boolean contextp)1639*fae548d3Szrj stab_class_method_var (struct stab_write_handle *info, const char *physname,
1640*fae548d3Szrj 		       enum debug_visibility visibility,
1641*fae548d3Szrj 		       bfd_boolean staticp, bfd_boolean constp,
1642*fae548d3Szrj 		       bfd_boolean volatilep, bfd_vma voffset,
1643*fae548d3Szrj 		       bfd_boolean contextp)
1644*fae548d3Szrj {
1645*fae548d3Szrj   bfd_boolean definition;
1646*fae548d3Szrj   char *type;
1647*fae548d3Szrj   char *context = NULL;
1648*fae548d3Szrj   char visc, qualc, typec;
1649*fae548d3Szrj 
1650*fae548d3Szrj   definition = info->type_stack->definition;
1651*fae548d3Szrj   type = stab_pop_type (info);
1652*fae548d3Szrj 
1653*fae548d3Szrj   if (contextp)
1654*fae548d3Szrj     {
1655*fae548d3Szrj       definition = definition || info->type_stack->definition;
1656*fae548d3Szrj       context = stab_pop_type (info);
1657*fae548d3Szrj     }
1658*fae548d3Szrj 
1659*fae548d3Szrj   assert (info->type_stack != NULL && info->type_stack->methods != NULL);
1660*fae548d3Szrj 
1661*fae548d3Szrj   switch (visibility)
1662*fae548d3Szrj     {
1663*fae548d3Szrj     default:
1664*fae548d3Szrj       abort ();
1665*fae548d3Szrj 
1666*fae548d3Szrj     case DEBUG_VISIBILITY_PRIVATE:
1667*fae548d3Szrj       visc = '0';
1668*fae548d3Szrj       break;
1669*fae548d3Szrj 
1670*fae548d3Szrj     case DEBUG_VISIBILITY_PROTECTED:
1671*fae548d3Szrj       visc = '1';
1672*fae548d3Szrj       break;
1673*fae548d3Szrj 
1674*fae548d3Szrj     case DEBUG_VISIBILITY_PUBLIC:
1675*fae548d3Szrj       visc = '2';
1676*fae548d3Szrj       break;
1677*fae548d3Szrj     }
1678*fae548d3Szrj 
1679*fae548d3Szrj   if (constp)
1680*fae548d3Szrj     {
1681*fae548d3Szrj       if (volatilep)
1682*fae548d3Szrj 	qualc = 'D';
1683*fae548d3Szrj       else
1684*fae548d3Szrj 	qualc = 'B';
1685*fae548d3Szrj     }
1686*fae548d3Szrj   else
1687*fae548d3Szrj     {
1688*fae548d3Szrj       if (volatilep)
1689*fae548d3Szrj 	qualc = 'C';
1690*fae548d3Szrj       else
1691*fae548d3Szrj 	qualc = 'A';
1692*fae548d3Szrj     }
1693*fae548d3Szrj 
1694*fae548d3Szrj   if (staticp)
1695*fae548d3Szrj     typec = '?';
1696*fae548d3Szrj   else if (! contextp)
1697*fae548d3Szrj     typec = '.';
1698*fae548d3Szrj   else
1699*fae548d3Szrj     typec = '*';
1700*fae548d3Szrj 
1701*fae548d3Szrj   info->type_stack->methods =
1702*fae548d3Szrj     (char *) xrealloc (info->type_stack->methods,
1703*fae548d3Szrj 		       (strlen (info->type_stack->methods)
1704*fae548d3Szrj 			+ strlen (type)
1705*fae548d3Szrj 			+ strlen (physname)
1706*fae548d3Szrj 			+ (contextp ? strlen (context) : 0)
1707*fae548d3Szrj 			+ 40));
1708*fae548d3Szrj 
1709*fae548d3Szrj   sprintf (info->type_stack->methods + strlen (info->type_stack->methods),
1710*fae548d3Szrj 	   "%s:%s;%c%c%c", type, physname, visc, qualc, typec);
1711*fae548d3Szrj   free (type);
1712*fae548d3Szrj 
1713*fae548d3Szrj   if (contextp)
1714*fae548d3Szrj     {
1715*fae548d3Szrj       sprintf (info->type_stack->methods + strlen (info->type_stack->methods),
1716*fae548d3Szrj 	       "%ld;%s;", (long) voffset, context);
1717*fae548d3Szrj       free (context);
1718*fae548d3Szrj     }
1719*fae548d3Szrj 
1720*fae548d3Szrj   if (definition)
1721*fae548d3Szrj     info->type_stack->definition = TRUE;
1722*fae548d3Szrj 
1723*fae548d3Szrj   return TRUE;
1724*fae548d3Szrj }
1725*fae548d3Szrj 
1726*fae548d3Szrj /* Add a variant to the current method.  */
1727*fae548d3Szrj 
1728*fae548d3Szrj static bfd_boolean
stab_class_method_variant(void * p,const char * physname,enum debug_visibility visibility,bfd_boolean constp,bfd_boolean volatilep,bfd_vma voffset,bfd_boolean contextp)1729*fae548d3Szrj stab_class_method_variant (void *p, const char *physname,
1730*fae548d3Szrj 			   enum debug_visibility visibility,
1731*fae548d3Szrj 			   bfd_boolean constp, bfd_boolean volatilep,
1732*fae548d3Szrj 			   bfd_vma voffset, bfd_boolean contextp)
1733*fae548d3Szrj {
1734*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
1735*fae548d3Szrj 
1736*fae548d3Szrj   return stab_class_method_var (info, physname, visibility, FALSE, constp,
1737*fae548d3Szrj 				volatilep, voffset, contextp);
1738*fae548d3Szrj }
1739*fae548d3Szrj 
1740*fae548d3Szrj /* Add a static variant to the current method.  */
1741*fae548d3Szrj 
1742*fae548d3Szrj static bfd_boolean
stab_class_static_method_variant(void * p,const char * physname,enum debug_visibility visibility,bfd_boolean constp,bfd_boolean volatilep)1743*fae548d3Szrj stab_class_static_method_variant (void *p, const char *physname,
1744*fae548d3Szrj 				  enum debug_visibility visibility,
1745*fae548d3Szrj 				  bfd_boolean constp, bfd_boolean volatilep)
1746*fae548d3Szrj {
1747*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
1748*fae548d3Szrj 
1749*fae548d3Szrj   return stab_class_method_var (info, physname, visibility, TRUE, constp,
1750*fae548d3Szrj 				volatilep, 0, FALSE);
1751*fae548d3Szrj }
1752*fae548d3Szrj 
1753*fae548d3Szrj /* Finish up a method.  */
1754*fae548d3Szrj 
1755*fae548d3Szrj static bfd_boolean
stab_class_end_method(void * p)1756*fae548d3Szrj stab_class_end_method (void *p)
1757*fae548d3Szrj {
1758*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
1759*fae548d3Szrj 
1760*fae548d3Szrj   assert (info->type_stack != NULL && info->type_stack->methods != NULL);
1761*fae548d3Szrj 
1762*fae548d3Szrj   /* We allocated enough room on info->type_stack->methods to add the
1763*fae548d3Szrj      trailing semicolon.  */
1764*fae548d3Szrj   strcat (info->type_stack->methods, ";");
1765*fae548d3Szrj 
1766*fae548d3Szrj   return TRUE;
1767*fae548d3Szrj }
1768*fae548d3Szrj 
1769*fae548d3Szrj /* Finish up a class.  */
1770*fae548d3Szrj 
1771*fae548d3Szrj static bfd_boolean
stab_end_class_type(void * p)1772*fae548d3Szrj stab_end_class_type (void *p)
1773*fae548d3Szrj {
1774*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
1775*fae548d3Szrj   size_t len;
1776*fae548d3Szrj   unsigned int i = 0;
1777*fae548d3Szrj   char *buf;
1778*fae548d3Szrj 
1779*fae548d3Szrj   assert (info->type_stack != NULL && info->type_stack->fields != NULL);
1780*fae548d3Szrj 
1781*fae548d3Szrj   /* Work out the size we need to allocate for the class definition.  */
1782*fae548d3Szrj 
1783*fae548d3Szrj   len = (strlen (info->type_stack->string)
1784*fae548d3Szrj 	 + strlen (info->type_stack->fields)
1785*fae548d3Szrj 	 + 10);
1786*fae548d3Szrj   if (info->type_stack->baseclasses != NULL)
1787*fae548d3Szrj     {
1788*fae548d3Szrj       len += 20;
1789*fae548d3Szrj       for (i = 0; info->type_stack->baseclasses[i] != NULL; i++)
1790*fae548d3Szrj 	len += strlen (info->type_stack->baseclasses[i]);
1791*fae548d3Szrj     }
1792*fae548d3Szrj   if (info->type_stack->methods != NULL)
1793*fae548d3Szrj     len += strlen (info->type_stack->methods);
1794*fae548d3Szrj   if (info->type_stack->vtable != NULL)
1795*fae548d3Szrj     len += strlen (info->type_stack->vtable);
1796*fae548d3Szrj 
1797*fae548d3Szrj   /* Build the class definition.  */
1798*fae548d3Szrj 
1799*fae548d3Szrj   buf = (char *) xmalloc (len);
1800*fae548d3Szrj 
1801*fae548d3Szrj   strcpy (buf, info->type_stack->string);
1802*fae548d3Szrj 
1803*fae548d3Szrj   if (info->type_stack->baseclasses != NULL)
1804*fae548d3Szrj     {
1805*fae548d3Szrj       sprintf (buf + strlen (buf), "!%u,", i);
1806*fae548d3Szrj       for (i = 0; info->type_stack->baseclasses[i] != NULL; i++)
1807*fae548d3Szrj 	{
1808*fae548d3Szrj 	  strcat (buf, info->type_stack->baseclasses[i]);
1809*fae548d3Szrj 	  free (info->type_stack->baseclasses[i]);
1810*fae548d3Szrj 	}
1811*fae548d3Szrj       free (info->type_stack->baseclasses);
1812*fae548d3Szrj       info->type_stack->baseclasses = NULL;
1813*fae548d3Szrj     }
1814*fae548d3Szrj 
1815*fae548d3Szrj   strcat (buf, info->type_stack->fields);
1816*fae548d3Szrj   free (info->type_stack->fields);
1817*fae548d3Szrj   info->type_stack->fields = NULL;
1818*fae548d3Szrj 
1819*fae548d3Szrj   if (info->type_stack->methods != NULL)
1820*fae548d3Szrj     {
1821*fae548d3Szrj       strcat (buf, info->type_stack->methods);
1822*fae548d3Szrj       free (info->type_stack->methods);
1823*fae548d3Szrj       info->type_stack->methods = NULL;
1824*fae548d3Szrj     }
1825*fae548d3Szrj 
1826*fae548d3Szrj   strcat (buf, ";");
1827*fae548d3Szrj 
1828*fae548d3Szrj   if (info->type_stack->vtable != NULL)
1829*fae548d3Szrj     {
1830*fae548d3Szrj       strcat (buf, info->type_stack->vtable);
1831*fae548d3Szrj       free (info->type_stack->vtable);
1832*fae548d3Szrj       info->type_stack->vtable = NULL;
1833*fae548d3Szrj     }
1834*fae548d3Szrj 
1835*fae548d3Szrj   /* Replace the string on the top of the stack with the complete
1836*fae548d3Szrj      class definition.  */
1837*fae548d3Szrj   free (info->type_stack->string);
1838*fae548d3Szrj   info->type_stack->string = buf;
1839*fae548d3Szrj 
1840*fae548d3Szrj   return TRUE;
1841*fae548d3Szrj }
1842*fae548d3Szrj 
1843*fae548d3Szrj /* Push a typedef which was previously defined.  */
1844*fae548d3Szrj 
1845*fae548d3Szrj static bfd_boolean
stab_typedef_type(void * p,const char * name)1846*fae548d3Szrj stab_typedef_type (void *p, const char *name)
1847*fae548d3Szrj {
1848*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
1849*fae548d3Szrj   struct string_hash_entry *h;
1850*fae548d3Szrj 
1851*fae548d3Szrj   h = string_hash_lookup (&info->typedef_hash, name, FALSE, FALSE);
1852*fae548d3Szrj   assert (h != NULL && h->index > 0);
1853*fae548d3Szrj 
1854*fae548d3Szrj   return stab_push_defined_type (info, h->index, h->size);
1855*fae548d3Szrj }
1856*fae548d3Szrj 
1857*fae548d3Szrj /* Push a struct, union or class tag.  */
1858*fae548d3Szrj 
1859*fae548d3Szrj static bfd_boolean
stab_tag_type(void * p,const char * name,unsigned int id,enum debug_type_kind kind)1860*fae548d3Szrj stab_tag_type (void *p, const char *name, unsigned int id,
1861*fae548d3Szrj 	       enum debug_type_kind kind)
1862*fae548d3Szrj {
1863*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
1864*fae548d3Szrj   long tindex;
1865*fae548d3Szrj   unsigned int size = 0;
1866*fae548d3Szrj 
1867*fae548d3Szrj   tindex = stab_get_struct_index (info, name, id, kind, &size);
1868*fae548d3Szrj   if (tindex < 0)
1869*fae548d3Szrj     return FALSE;
1870*fae548d3Szrj 
1871*fae548d3Szrj   return stab_push_defined_type (info, tindex, size);
1872*fae548d3Szrj }
1873*fae548d3Szrj 
1874*fae548d3Szrj /* Define a typedef.  */
1875*fae548d3Szrj 
1876*fae548d3Szrj static bfd_boolean
stab_typdef(void * p,const char * name)1877*fae548d3Szrj stab_typdef (void *p, const char *name)
1878*fae548d3Szrj {
1879*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
1880*fae548d3Szrj   long tindex;
1881*fae548d3Szrj   unsigned int size;
1882*fae548d3Szrj   char *s, *buf;
1883*fae548d3Szrj   struct string_hash_entry *h;
1884*fae548d3Szrj 
1885*fae548d3Szrj   tindex = info->type_stack->index;
1886*fae548d3Szrj   size = info->type_stack->size;
1887*fae548d3Szrj   s = stab_pop_type (info);
1888*fae548d3Szrj 
1889*fae548d3Szrj   buf = (char *) xmalloc (strlen (name) + strlen (s) + 20);
1890*fae548d3Szrj 
1891*fae548d3Szrj   if (tindex > 0)
1892*fae548d3Szrj     sprintf (buf, "%s:t%s", name, s);
1893*fae548d3Szrj   else
1894*fae548d3Szrj     {
1895*fae548d3Szrj       tindex = info->type_index;
1896*fae548d3Szrj       ++info->type_index;
1897*fae548d3Szrj       sprintf (buf, "%s:t%ld=%s", name, tindex, s);
1898*fae548d3Szrj     }
1899*fae548d3Szrj 
1900*fae548d3Szrj   free (s);
1901*fae548d3Szrj 
1902*fae548d3Szrj   if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
1903*fae548d3Szrj     return FALSE;
1904*fae548d3Szrj 
1905*fae548d3Szrj   free (buf);
1906*fae548d3Szrj 
1907*fae548d3Szrj   h = string_hash_lookup (&info->typedef_hash, name, TRUE, FALSE);
1908*fae548d3Szrj   if (h == NULL)
1909*fae548d3Szrj     {
1910*fae548d3Szrj       non_fatal (_("string_hash_lookup failed: %s"),
1911*fae548d3Szrj 		 bfd_errmsg (bfd_get_error ()));
1912*fae548d3Szrj       return FALSE;
1913*fae548d3Szrj     }
1914*fae548d3Szrj 
1915*fae548d3Szrj   /* I don't think we care about redefinitions.  */
1916*fae548d3Szrj 
1917*fae548d3Szrj   h->index = tindex;
1918*fae548d3Szrj   h->size = size;
1919*fae548d3Szrj 
1920*fae548d3Szrj   return TRUE;
1921*fae548d3Szrj }
1922*fae548d3Szrj 
1923*fae548d3Szrj /* Define a tag.  */
1924*fae548d3Szrj 
1925*fae548d3Szrj static bfd_boolean
stab_tag(void * p,const char * tag)1926*fae548d3Szrj stab_tag (void *p, const char *tag)
1927*fae548d3Szrj {
1928*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
1929*fae548d3Szrj   char *s, *buf;
1930*fae548d3Szrj 
1931*fae548d3Szrj   s = stab_pop_type (info);
1932*fae548d3Szrj 
1933*fae548d3Szrj   buf = (char *) xmalloc (strlen (tag) + strlen (s) + 3);
1934*fae548d3Szrj 
1935*fae548d3Szrj   sprintf (buf, "%s:T%s", tag, s);
1936*fae548d3Szrj   free (s);
1937*fae548d3Szrj 
1938*fae548d3Szrj   if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
1939*fae548d3Szrj     return FALSE;
1940*fae548d3Szrj 
1941*fae548d3Szrj   free (buf);
1942*fae548d3Szrj 
1943*fae548d3Szrj   return TRUE;
1944*fae548d3Szrj }
1945*fae548d3Szrj 
1946*fae548d3Szrj /* Define an integer constant.  */
1947*fae548d3Szrj 
1948*fae548d3Szrj static bfd_boolean
stab_int_constant(void * p,const char * name,bfd_vma val)1949*fae548d3Szrj stab_int_constant (void *p, const char *name, bfd_vma val)
1950*fae548d3Szrj {
1951*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
1952*fae548d3Szrj   char *buf;
1953*fae548d3Szrj 
1954*fae548d3Szrj   buf = (char *) xmalloc (strlen (name) + 20);
1955*fae548d3Szrj   sprintf (buf, "%s:c=i%ld", name, (long) val);
1956*fae548d3Szrj 
1957*fae548d3Szrj   if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
1958*fae548d3Szrj     return FALSE;
1959*fae548d3Szrj 
1960*fae548d3Szrj   free (buf);
1961*fae548d3Szrj 
1962*fae548d3Szrj   return TRUE;
1963*fae548d3Szrj }
1964*fae548d3Szrj 
1965*fae548d3Szrj /* Define a floating point constant.  */
1966*fae548d3Szrj 
1967*fae548d3Szrj static bfd_boolean
stab_float_constant(void * p,const char * name,double val)1968*fae548d3Szrj stab_float_constant (void *p, const char *name, double val)
1969*fae548d3Szrj {
1970*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
1971*fae548d3Szrj   char *buf;
1972*fae548d3Szrj 
1973*fae548d3Szrj   buf = (char *) xmalloc (strlen (name) + 20);
1974*fae548d3Szrj   sprintf (buf, "%s:c=f%g", name, val);
1975*fae548d3Szrj 
1976*fae548d3Szrj   if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
1977*fae548d3Szrj     return FALSE;
1978*fae548d3Szrj 
1979*fae548d3Szrj   free (buf);
1980*fae548d3Szrj 
1981*fae548d3Szrj   return TRUE;
1982*fae548d3Szrj }
1983*fae548d3Szrj 
1984*fae548d3Szrj /* Define a typed constant.  */
1985*fae548d3Szrj 
1986*fae548d3Szrj static bfd_boolean
stab_typed_constant(void * p,const char * name,bfd_vma val)1987*fae548d3Szrj stab_typed_constant (void *p, const char *name, bfd_vma val)
1988*fae548d3Szrj {
1989*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
1990*fae548d3Szrj   char *s, *buf;
1991*fae548d3Szrj 
1992*fae548d3Szrj   s = stab_pop_type (info);
1993*fae548d3Szrj 
1994*fae548d3Szrj   buf = (char *) xmalloc (strlen (name) + strlen (s) + 20);
1995*fae548d3Szrj   sprintf (buf, "%s:c=e%s,%ld", name, s, (long) val);
1996*fae548d3Szrj   free (s);
1997*fae548d3Szrj 
1998*fae548d3Szrj   if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
1999*fae548d3Szrj     return FALSE;
2000*fae548d3Szrj 
2001*fae548d3Szrj   free (buf);
2002*fae548d3Szrj 
2003*fae548d3Szrj   return TRUE;
2004*fae548d3Szrj }
2005*fae548d3Szrj 
2006*fae548d3Szrj /* Record a variable.  */
2007*fae548d3Szrj 
2008*fae548d3Szrj static bfd_boolean
stab_variable(void * p,const char * name,enum debug_var_kind kind,bfd_vma val)2009*fae548d3Szrj stab_variable (void *p, const char *name, enum debug_var_kind kind,
2010*fae548d3Szrj 	       bfd_vma val)
2011*fae548d3Szrj {
2012*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
2013*fae548d3Szrj   char *s, *buf;
2014*fae548d3Szrj   int stab_type;
2015*fae548d3Szrj   const char *kindstr;
2016*fae548d3Szrj 
2017*fae548d3Szrj   s = stab_pop_type (info);
2018*fae548d3Szrj 
2019*fae548d3Szrj   switch (kind)
2020*fae548d3Szrj     {
2021*fae548d3Szrj     default:
2022*fae548d3Szrj       abort ();
2023*fae548d3Szrj 
2024*fae548d3Szrj     case DEBUG_GLOBAL:
2025*fae548d3Szrj       stab_type = N_GSYM;
2026*fae548d3Szrj       kindstr = "G";
2027*fae548d3Szrj       break;
2028*fae548d3Szrj 
2029*fae548d3Szrj     case DEBUG_STATIC:
2030*fae548d3Szrj       stab_type = N_STSYM;
2031*fae548d3Szrj       kindstr = "S";
2032*fae548d3Szrj       break;
2033*fae548d3Szrj 
2034*fae548d3Szrj     case DEBUG_LOCAL_STATIC:
2035*fae548d3Szrj       stab_type = N_STSYM;
2036*fae548d3Szrj       kindstr = "V";
2037*fae548d3Szrj       break;
2038*fae548d3Szrj 
2039*fae548d3Szrj     case DEBUG_LOCAL:
2040*fae548d3Szrj       stab_type = N_LSYM;
2041*fae548d3Szrj       kindstr = "";
2042*fae548d3Szrj 
2043*fae548d3Szrj       /* Make sure that this is a type reference or definition.  */
2044*fae548d3Szrj       if (! ISDIGIT (*s))
2045*fae548d3Szrj 	{
2046*fae548d3Szrj 	  char *n;
2047*fae548d3Szrj 	  long tindex;
2048*fae548d3Szrj 
2049*fae548d3Szrj 	  tindex = info->type_index;
2050*fae548d3Szrj 	  ++info->type_index;
2051*fae548d3Szrj 	  n = (char *) xmalloc (strlen (s) + 20);
2052*fae548d3Szrj 	  sprintf (n, "%ld=%s", tindex, s);
2053*fae548d3Szrj 	  free (s);
2054*fae548d3Szrj 	  s = n;
2055*fae548d3Szrj 	}
2056*fae548d3Szrj       break;
2057*fae548d3Szrj 
2058*fae548d3Szrj     case DEBUG_REGISTER:
2059*fae548d3Szrj       stab_type = N_RSYM;
2060*fae548d3Szrj       kindstr = "r";
2061*fae548d3Szrj       break;
2062*fae548d3Szrj     }
2063*fae548d3Szrj 
2064*fae548d3Szrj   buf = (char *) xmalloc (strlen (name) + strlen (s) + 3);
2065*fae548d3Szrj   sprintf (buf, "%s:%s%s", name, kindstr, s);
2066*fae548d3Szrj   free (s);
2067*fae548d3Szrj 
2068*fae548d3Szrj   if (! stab_write_symbol (info, stab_type, 0, val, buf))
2069*fae548d3Szrj     return FALSE;
2070*fae548d3Szrj 
2071*fae548d3Szrj   free (buf);
2072*fae548d3Szrj 
2073*fae548d3Szrj   return TRUE;
2074*fae548d3Szrj }
2075*fae548d3Szrj 
2076*fae548d3Szrj /* Start outputting a function.  */
2077*fae548d3Szrj 
2078*fae548d3Szrj static bfd_boolean
stab_start_function(void * p,const char * name,bfd_boolean globalp)2079*fae548d3Szrj stab_start_function (void *p, const char *name, bfd_boolean globalp)
2080*fae548d3Szrj {
2081*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
2082*fae548d3Szrj   char *rettype, *buf;
2083*fae548d3Szrj 
2084*fae548d3Szrj   assert (info->nesting == 0 && info->fun_offset == -1);
2085*fae548d3Szrj 
2086*fae548d3Szrj   rettype = stab_pop_type (info);
2087*fae548d3Szrj 
2088*fae548d3Szrj   buf = (char *) xmalloc (strlen (name) + strlen (rettype) + 3);
2089*fae548d3Szrj   sprintf (buf, "%s:%c%s", name,
2090*fae548d3Szrj 	   globalp ? 'F' : 'f',
2091*fae548d3Szrj 	   rettype);
2092*fae548d3Szrj 
2093*fae548d3Szrj   /* We don't know the value now, so we set it in start_block.  */
2094*fae548d3Szrj   info->fun_offset = info->symbols_size;
2095*fae548d3Szrj 
2096*fae548d3Szrj   if (! stab_write_symbol (info, N_FUN, 0, 0, buf))
2097*fae548d3Szrj     return FALSE;
2098*fae548d3Szrj 
2099*fae548d3Szrj   free (buf);
2100*fae548d3Szrj 
2101*fae548d3Szrj   return TRUE;
2102*fae548d3Szrj }
2103*fae548d3Szrj 
2104*fae548d3Szrj /* Output a function parameter.  */
2105*fae548d3Szrj 
2106*fae548d3Szrj static bfd_boolean
stab_function_parameter(void * p,const char * name,enum debug_parm_kind kind,bfd_vma val)2107*fae548d3Szrj stab_function_parameter (void *p, const char *name, enum debug_parm_kind kind, bfd_vma val)
2108*fae548d3Szrj {
2109*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
2110*fae548d3Szrj   char *s, *buf;
2111*fae548d3Szrj   int stab_type;
2112*fae548d3Szrj   char kindc;
2113*fae548d3Szrj 
2114*fae548d3Szrj   s = stab_pop_type (info);
2115*fae548d3Szrj 
2116*fae548d3Szrj   switch (kind)
2117*fae548d3Szrj     {
2118*fae548d3Szrj     default:
2119*fae548d3Szrj       abort ();
2120*fae548d3Szrj 
2121*fae548d3Szrj     case DEBUG_PARM_STACK:
2122*fae548d3Szrj       stab_type = N_PSYM;
2123*fae548d3Szrj       kindc = 'p';
2124*fae548d3Szrj       break;
2125*fae548d3Szrj 
2126*fae548d3Szrj     case DEBUG_PARM_REG:
2127*fae548d3Szrj       stab_type = N_RSYM;
2128*fae548d3Szrj       kindc = 'P';
2129*fae548d3Szrj       break;
2130*fae548d3Szrj 
2131*fae548d3Szrj     case DEBUG_PARM_REFERENCE:
2132*fae548d3Szrj       stab_type = N_PSYM;
2133*fae548d3Szrj       kindc = 'v';
2134*fae548d3Szrj       break;
2135*fae548d3Szrj 
2136*fae548d3Szrj     case DEBUG_PARM_REF_REG:
2137*fae548d3Szrj       stab_type = N_RSYM;
2138*fae548d3Szrj       kindc = 'a';
2139*fae548d3Szrj       break;
2140*fae548d3Szrj     }
2141*fae548d3Szrj 
2142*fae548d3Szrj   buf = (char *) xmalloc (strlen (name) + strlen (s) + 3);
2143*fae548d3Szrj   sprintf (buf, "%s:%c%s", name, kindc, s);
2144*fae548d3Szrj   free (s);
2145*fae548d3Szrj 
2146*fae548d3Szrj   if (! stab_write_symbol (info, stab_type, 0, val, buf))
2147*fae548d3Szrj     return FALSE;
2148*fae548d3Szrj 
2149*fae548d3Szrj   free (buf);
2150*fae548d3Szrj 
2151*fae548d3Szrj   return TRUE;
2152*fae548d3Szrj }
2153*fae548d3Szrj 
2154*fae548d3Szrj /* Start a block.  */
2155*fae548d3Szrj 
2156*fae548d3Szrj static bfd_boolean
stab_start_block(void * p,bfd_vma addr)2157*fae548d3Szrj stab_start_block (void *p, bfd_vma addr)
2158*fae548d3Szrj {
2159*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
2160*fae548d3Szrj 
2161*fae548d3Szrj   /* Fill in any slots which have been waiting for the first known
2162*fae548d3Szrj      text address.  */
2163*fae548d3Szrj 
2164*fae548d3Szrj   if (info->so_offset != -1)
2165*fae548d3Szrj     {
2166*fae548d3Szrj       bfd_put_32 (info->abfd, addr, info->symbols + info->so_offset + 8);
2167*fae548d3Szrj       info->so_offset = -1;
2168*fae548d3Szrj     }
2169*fae548d3Szrj 
2170*fae548d3Szrj   if (info->fun_offset != -1)
2171*fae548d3Szrj     {
2172*fae548d3Szrj       bfd_put_32 (info->abfd, addr, info->symbols + info->fun_offset + 8);
2173*fae548d3Szrj       info->fun_offset = -1;
2174*fae548d3Szrj     }
2175*fae548d3Szrj 
2176*fae548d3Szrj   ++info->nesting;
2177*fae548d3Szrj 
2178*fae548d3Szrj   /* We will be called with a top level block surrounding the
2179*fae548d3Szrj      function, but stabs information does not output that block, so we
2180*fae548d3Szrj      ignore it.  */
2181*fae548d3Szrj 
2182*fae548d3Szrj   if (info->nesting == 1)
2183*fae548d3Szrj     {
2184*fae548d3Szrj       info->fnaddr = addr;
2185*fae548d3Szrj       return TRUE;
2186*fae548d3Szrj     }
2187*fae548d3Szrj 
2188*fae548d3Szrj   /* We have to output the LBRAC symbol after any variables which are
2189*fae548d3Szrj      declared inside the block.  We postpone the LBRAC until the next
2190*fae548d3Szrj      start_block or end_block.  */
2191*fae548d3Szrj 
2192*fae548d3Szrj   /* If we have postponed an LBRAC, output it now.  */
2193*fae548d3Szrj   if (info->pending_lbrac != (bfd_vma) -1)
2194*fae548d3Szrj     {
2195*fae548d3Szrj       if (! stab_write_symbol (info, N_LBRAC, 0, info->pending_lbrac,
2196*fae548d3Szrj 			       (const char *) NULL))
2197*fae548d3Szrj 	return FALSE;
2198*fae548d3Szrj     }
2199*fae548d3Szrj 
2200*fae548d3Szrj   /* Remember the address and output it later.  */
2201*fae548d3Szrj 
2202*fae548d3Szrj   info->pending_lbrac = addr - info->fnaddr;
2203*fae548d3Szrj 
2204*fae548d3Szrj   return TRUE;
2205*fae548d3Szrj }
2206*fae548d3Szrj 
2207*fae548d3Szrj /* End a block.  */
2208*fae548d3Szrj 
2209*fae548d3Szrj static bfd_boolean
stab_end_block(void * p,bfd_vma addr)2210*fae548d3Szrj stab_end_block (void *p, bfd_vma addr)
2211*fae548d3Szrj {
2212*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
2213*fae548d3Szrj 
2214*fae548d3Szrj   if (addr > info->last_text_address)
2215*fae548d3Szrj     info->last_text_address = addr;
2216*fae548d3Szrj 
2217*fae548d3Szrj   /* If we have postponed an LBRAC, output it now.  */
2218*fae548d3Szrj   if (info->pending_lbrac != (bfd_vma) -1)
2219*fae548d3Szrj     {
2220*fae548d3Szrj       if (! stab_write_symbol (info, N_LBRAC, 0, info->pending_lbrac,
2221*fae548d3Szrj 			       (const char *) NULL))
2222*fae548d3Szrj 	return FALSE;
2223*fae548d3Szrj       info->pending_lbrac = (bfd_vma) -1;
2224*fae548d3Szrj     }
2225*fae548d3Szrj 
2226*fae548d3Szrj   assert (info->nesting > 0);
2227*fae548d3Szrj 
2228*fae548d3Szrj   --info->nesting;
2229*fae548d3Szrj 
2230*fae548d3Szrj   /* We ignore the outermost block.  */
2231*fae548d3Szrj   if (info->nesting == 0)
2232*fae548d3Szrj     return TRUE;
2233*fae548d3Szrj 
2234*fae548d3Szrj   return stab_write_symbol (info, N_RBRAC, 0, addr - info->fnaddr,
2235*fae548d3Szrj 			    (const char *) NULL);
2236*fae548d3Szrj }
2237*fae548d3Szrj 
2238*fae548d3Szrj /* End a function.  */
2239*fae548d3Szrj 
2240*fae548d3Szrj static bfd_boolean
stab_end_function(void * p ATTRIBUTE_UNUSED)2241*fae548d3Szrj stab_end_function (void *p ATTRIBUTE_UNUSED)
2242*fae548d3Szrj {
2243*fae548d3Szrj   return TRUE;
2244*fae548d3Szrj }
2245*fae548d3Szrj 
2246*fae548d3Szrj /* Output a line number.  */
2247*fae548d3Szrj 
2248*fae548d3Szrj static bfd_boolean
stab_lineno(void * p,const char * file,unsigned long lineno,bfd_vma addr)2249*fae548d3Szrj stab_lineno (void *p, const char *file, unsigned long lineno, bfd_vma addr)
2250*fae548d3Szrj {
2251*fae548d3Szrj   struct stab_write_handle *info = (struct stab_write_handle *) p;
2252*fae548d3Szrj 
2253*fae548d3Szrj   assert (info->lineno_filename != NULL);
2254*fae548d3Szrj 
2255*fae548d3Szrj   if (addr > info->last_text_address)
2256*fae548d3Szrj     info->last_text_address = addr;
2257*fae548d3Szrj 
2258*fae548d3Szrj   if (filename_cmp (file, info->lineno_filename) != 0)
2259*fae548d3Szrj     {
2260*fae548d3Szrj       if (! stab_write_symbol (info, N_SOL, 0, addr, file))
2261*fae548d3Szrj 	return FALSE;
2262*fae548d3Szrj       info->lineno_filename = file;
2263*fae548d3Szrj     }
2264*fae548d3Szrj 
2265*fae548d3Szrj   return stab_write_symbol (info, N_SLINE, lineno, addr - info->fnaddr,
2266*fae548d3Szrj 			    (const char *) NULL);
2267*fae548d3Szrj }
2268