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