struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / sdbinutils / binutils / wrstabs.c
blob0db3c5ba518b6554e81f68f7f8fbc88f4a46f45e
1 /* wrstabs.c -- Output stabs debugging information
2 Copyright (C) 1996-2022 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@cygnus.com>.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 /* This file contains code which writes out stabs debugging
23 information. */
25 #include "sysdep.h"
26 #include <assert.h>
27 #include "bfd.h"
28 #include "libiberty.h"
29 #include "filenames.h"
30 #include "safe-ctype.h"
31 #include "bucomm.h"
32 #include "debug.h"
33 #include "budbg.h"
34 #include "aout/aout64.h"
35 #include "aout/stab_gnu.h"
37 /* The size of a stabs symbol. This presumes 32 bit values. */
39 #define STAB_SYMBOL_SIZE (12)
41 /* An entry in a string hash table. */
43 struct string_hash_entry
45 struct bfd_hash_entry root;
46 /* Next string in this table. */
47 struct string_hash_entry *next;
48 /* Index in string table. */
49 long index;
50 /* Size of type if this is a typedef. */
51 unsigned int size;
54 /* A string hash table. */
56 struct string_hash_table
58 struct bfd_hash_table table;
61 /* The type stack. Each element on the stack is a string. */
63 struct stab_type_stack
65 /* The next element on the stack. */
66 struct stab_type_stack *next;
67 /* This element as a string. */
68 char *string;
69 /* The type index of this element. */
70 long index;
71 /* The size of the type. */
72 unsigned int size;
73 /* Whether type string defines a new type. */
74 bool definition;
75 /* String defining struct fields. */
76 char *fields;
77 /* NULL terminated array of strings defining base classes for a
78 class. */
79 char **baseclasses;
80 /* String defining class methods. */
81 char *methods;
82 /* String defining vtable pointer for a class. */
83 char *vtable;
86 /* This structure is used to keep track of type indices for tagged
87 types. */
89 struct stab_tag
91 /* The type index. */
92 long index;
93 /* The tag name. */
94 const char *tag;
95 /* The kind of type. This is set to DEBUG_KIND_ILLEGAL when the
96 type is defined. */
97 enum debug_type_kind kind;
98 /* The size of the struct. */
99 unsigned int size;
102 /* We remember various sorts of type indices. They are not related,
103 but, for convenience, we keep all the information in this
104 structure. */
106 struct stab_type_cache
108 /* The void type index. */
109 long void_type;
110 /* Signed integer type indices, indexed by size - 1. */
111 long signed_integer_types[8];
112 /* Unsigned integer type indices, indexed by size - 1. */
113 long unsigned_integer_types[8];
114 /* Floating point types, indexed by size - 1. */
115 long float_types[16];
116 /* Pointers to types, indexed by the type index. */
117 long *pointer_types;
118 size_t pointer_types_alloc;
119 /* Functions returning types, indexed by the type index. */
120 long *function_types;
121 size_t function_types_alloc;
122 /* References to types, indexed by the type index. */
123 long *reference_types;
124 size_t reference_types_alloc;
125 /* Struct/union/class type indices, indexed by the struct id. */
126 struct stab_tag *struct_types;
127 size_t struct_types_alloc;
130 /* This is the handle passed through debug_write. */
132 struct stab_write_handle
134 /* The BFD. */
135 bfd *abfd;
136 /* This buffer holds the symbols. */
137 bfd_byte *symbols;
138 size_t symbols_size;
139 size_t symbols_alloc;
140 /* This is a list of hash table entries for the strings. */
141 struct string_hash_entry *strings;
142 /* The last string hash table entry. */
143 struct string_hash_entry *last_string;
144 /* The size of the strings. */
145 size_t strings_size;
146 /* This hash table eliminates duplicate strings. */
147 struct string_hash_table strhash;
148 /* The type stack. */
149 struct stab_type_stack *type_stack;
150 /* The next type index. */
151 long type_index;
152 /* The type cache. */
153 struct stab_type_cache type_cache;
154 /* A mapping from typedef names to type indices. */
155 struct string_hash_table typedef_hash;
156 /* If this is not -1, it is the offset to the most recent N_SO
157 symbol, and the value of that symbol needs to be set. */
158 long so_offset;
159 /* If this is not -1, it is the offset to the most recent N_FUN
160 symbol, and the value of that symbol needs to be set. */
161 long fun_offset;
162 /* The last text section address seen. */
163 bfd_vma last_text_address;
164 /* The block nesting depth. */
165 unsigned int nesting;
166 /* The function address. */
167 bfd_vma fnaddr;
168 /* A pending LBRAC symbol. */
169 bfd_vma pending_lbrac;
170 /* The current line number file name. */
171 const char *lineno_filename;
174 static struct bfd_hash_entry *string_hash_newfunc
175 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
176 static bool stab_write_symbol
177 (struct stab_write_handle *, int, int, bfd_vma, const char *);
178 static bool stab_push_string
179 (struct stab_write_handle *, const char *, long, bool, unsigned int);
180 static bool stab_push_defined_type
181 (struct stab_write_handle *, long, unsigned int);
182 static char *stab_pop_type (struct stab_write_handle *);
183 static bool stab_modify_type
184 (struct stab_write_handle *, int, unsigned int, long **, size_t *);
185 static long stab_get_struct_index
186 (struct stab_write_handle *, const char *, unsigned int,
187 enum debug_type_kind, unsigned int *);
188 static bool stab_class_method_var
189 (struct stab_write_handle *, const char *, enum debug_visibility,
190 bool, bool, bool, bfd_vma, bool);
191 static bool stab_start_compilation_unit (void *, const char *);
192 static bool stab_start_source (void *, const char *);
193 static bool stab_empty_type (void *);
194 static bool stab_void_type (void *);
195 static bool stab_int_type (void *, unsigned int, bool);
196 static bool stab_float_type (void *, unsigned int);
197 static bool stab_complex_type (void *, unsigned int);
198 static bool stab_bool_type (void *, unsigned int);
199 static bool stab_enum_type
200 (void *, const char *, const char **, bfd_signed_vma *);
201 static bool stab_pointer_type (void *);
202 static bool stab_function_type (void *, int, bool);
203 static bool stab_reference_type (void *);
204 static bool stab_range_type (void *, bfd_signed_vma, bfd_signed_vma);
205 static bool stab_array_type
206 (void *, bfd_signed_vma, bfd_signed_vma, bool);
207 static bool stab_set_type (void *, bool);
208 static bool stab_offset_type (void *);
209 static bool stab_method_type (void *, bool, int, bool);
210 static bool stab_const_type (void *);
211 static bool stab_volatile_type (void *);
212 static bool stab_start_struct_type
213 (void *, const char *, unsigned int, bool, unsigned int);
214 static bool stab_struct_field
215 (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
216 static bool stab_end_struct_type (void *);
217 static bool stab_start_class_type
218 (void *, const char *, unsigned int, bool, unsigned int,
219 bool, bool);
220 static bool stab_class_static_member
221 (void *, const char *, const char *, enum debug_visibility);
222 static bool stab_class_baseclass
223 (void *, bfd_vma, bool, enum debug_visibility);
224 static bool stab_class_start_method (void *, const char *);
225 static bool stab_class_method_variant
226 (void *, const char *, enum debug_visibility, bool, bool,
227 bfd_vma, bool);
228 static bool stab_class_static_method_variant
229 (void *, const char *, enum debug_visibility, bool, bool);
230 static bool stab_class_end_method (void *);
231 static bool stab_end_class_type (void *);
232 static bool stab_typedef_type (void *, const char *);
233 static bool stab_tag_type
234 (void *, const char *, unsigned int, enum debug_type_kind);
235 static bool stab_typdef (void *, const char *);
236 static bool stab_tag (void *, const char *);
237 static bool stab_int_constant (void *, const char *, bfd_vma);
238 static bool stab_float_constant (void *, const char *, double);
239 static bool stab_typed_constant (void *, const char *, bfd_vma);
240 static bool stab_variable
241 (void *, const char *, enum debug_var_kind, bfd_vma);
242 static bool stab_start_function (void *, const char *, bool);
243 static bool stab_function_parameter
244 (void *, const char *, enum debug_parm_kind, bfd_vma);
245 static bool stab_start_block (void *, bfd_vma);
246 static bool stab_end_block (void *, bfd_vma);
247 static bool stab_end_function (void *);
248 static bool stab_lineno (void *, const char *, unsigned long, bfd_vma);
250 static const struct debug_write_fns stab_fns =
252 stab_start_compilation_unit,
253 stab_start_source,
254 stab_empty_type,
255 stab_void_type,
256 stab_int_type,
257 stab_float_type,
258 stab_complex_type,
259 stab_bool_type,
260 stab_enum_type,
261 stab_pointer_type,
262 stab_function_type,
263 stab_reference_type,
264 stab_range_type,
265 stab_array_type,
266 stab_set_type,
267 stab_offset_type,
268 stab_method_type,
269 stab_const_type,
270 stab_volatile_type,
271 stab_start_struct_type,
272 stab_struct_field,
273 stab_end_struct_type,
274 stab_start_class_type,
275 stab_class_static_member,
276 stab_class_baseclass,
277 stab_class_start_method,
278 stab_class_method_variant,
279 stab_class_static_method_variant,
280 stab_class_end_method,
281 stab_end_class_type,
282 stab_typedef_type,
283 stab_tag_type,
284 stab_typdef,
285 stab_tag,
286 stab_int_constant,
287 stab_float_constant,
288 stab_typed_constant,
289 stab_variable,
290 stab_start_function,
291 stab_function_parameter,
292 stab_start_block,
293 stab_end_block,
294 stab_end_function,
295 stab_lineno
298 /* Routine to create an entry in a string hash table. */
300 static struct bfd_hash_entry *
301 string_hash_newfunc (struct bfd_hash_entry *entry,
302 struct bfd_hash_table *table, const char *string)
304 struct string_hash_entry *ret = (struct string_hash_entry *) entry;
306 /* Allocate the structure if it has not already been allocated by a
307 subclass. */
308 if (ret == (struct string_hash_entry *) NULL)
309 ret = ((struct string_hash_entry *)
310 bfd_hash_allocate (table, sizeof (struct string_hash_entry)));
311 if (ret == (struct string_hash_entry *) NULL)
312 return NULL;
314 /* Call the allocation method of the superclass. */
315 ret = ((struct string_hash_entry *)
316 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
318 if (ret)
320 /* Initialize the local fields. */
321 ret->next = NULL;
322 ret->index = -1;
323 ret->size = 0;
326 return (struct bfd_hash_entry *) ret;
329 /* Look up an entry in a string hash table. */
331 #define string_hash_lookup(t, string, create, copy) \
332 ((struct string_hash_entry *) \
333 bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
335 /* Add a symbol to the stabs debugging information we are building. */
337 static bool
338 stab_write_symbol (struct stab_write_handle *info, int type, int desc,
339 bfd_vma value, const char *string)
341 bfd_size_type strx;
342 bfd_byte sym[STAB_SYMBOL_SIZE];
344 if (string == NULL)
345 strx = 0;
346 else
348 struct string_hash_entry *h;
350 h = string_hash_lookup (&info->strhash, string, true, true);
351 if (h == NULL)
353 non_fatal (_("string_hash_lookup failed: %s"),
354 bfd_errmsg (bfd_get_error ()));
355 return false;
357 if (h->index != -1)
358 strx = h->index;
359 else
361 strx = info->strings_size;
362 h->index = strx;
363 if (info->last_string == NULL)
364 info->strings = h;
365 else
366 info->last_string->next = h;
367 info->last_string = h;
368 info->strings_size += strlen (string) + 1;
372 /* This presumes 32 bit values. */
373 bfd_put_32 (info->abfd, strx, sym);
374 bfd_put_8 (info->abfd, type, sym + 4);
375 bfd_put_8 (info->abfd, 0, sym + 5);
376 bfd_put_16 (info->abfd, desc, sym + 6);
377 bfd_put_32 (info->abfd, value, sym + 8);
379 if (info->symbols_size + STAB_SYMBOL_SIZE > info->symbols_alloc)
381 info->symbols_alloc *= 2;
382 info->symbols = (bfd_byte *) xrealloc (info->symbols,
383 info->symbols_alloc);
386 memcpy (info->symbols + info->symbols_size, sym, STAB_SYMBOL_SIZE);
388 info->symbols_size += STAB_SYMBOL_SIZE;
390 return true;
393 /* Push a string on to the type stack. */
395 static bool
396 stab_push_string (struct stab_write_handle *info, const char *string,
397 long tindex, bool definition, unsigned int size)
399 struct stab_type_stack *s;
401 s = (struct stab_type_stack *) xmalloc (sizeof *s);
402 s->string = xstrdup (string);
403 s->index = tindex;
404 s->definition = definition;
405 s->size = size;
407 s->fields = NULL;
408 s->baseclasses = NULL;
409 s->methods = NULL;
410 s->vtable = NULL;
412 s->next = info->type_stack;
413 info->type_stack = s;
415 return true;
418 /* Push a type index which has already been defined. */
420 static bool
421 stab_push_defined_type (struct stab_write_handle *info, long tindex,
422 unsigned int size)
424 char buf[20];
426 sprintf (buf, "%ld", tindex);
427 return stab_push_string (info, buf, tindex, false, size);
430 /* Pop a type off the type stack. The caller is responsible for
431 freeing the string. */
433 static char *
434 stab_pop_type (struct stab_write_handle *info)
436 struct stab_type_stack *s;
437 char *ret;
439 s = info->type_stack;
440 if (s == NULL)
441 return NULL;
443 info->type_stack = s->next;
445 ret = s->string;
447 free (s);
449 return ret;
452 /* The general routine to write out stabs in sections debugging
453 information. This accumulates the stabs symbols and the strings in
454 two obstacks. We can't easily write out the information as we go
455 along, because we need to know the section sizes before we can
456 write out the section contents. ABFD is the BFD and DHANDLE is the
457 handle for the debugging information. This sets *PSYMS to point to
458 the symbols, *PSYMSIZE the size of the symbols, *PSTRINGS to the
459 strings, and *PSTRINGSIZE to the size of the strings. */
461 bool
462 write_stabs_in_sections_debugging_info (bfd *abfd, void *dhandle,
463 bfd_byte **psyms,
464 bfd_size_type *psymsize,
465 bfd_byte **pstrings,
466 bfd_size_type *pstringsize)
468 struct stab_write_handle info;
469 struct string_hash_entry *h;
470 bfd_byte *p;
472 info.abfd = abfd;
474 info.symbols_size = 0;
475 info.symbols_alloc = 500;
476 info.symbols = (bfd_byte *) xmalloc (info.symbols_alloc);
478 info.strings = NULL;
479 info.last_string = NULL;
480 /* Reserve 1 byte for a null byte. */
481 info.strings_size = 1;
483 if (!bfd_hash_table_init (&info.strhash.table, string_hash_newfunc,
484 sizeof (struct string_hash_entry))
485 || !bfd_hash_table_init (&info.typedef_hash.table, string_hash_newfunc,
486 sizeof (struct string_hash_entry)))
488 non_fatal ("bfd_hash_table_init_failed: %s",
489 bfd_errmsg (bfd_get_error ()));
490 return false;
493 info.type_stack = NULL;
494 info.type_index = 1;
495 memset (&info.type_cache, 0, sizeof info.type_cache);
496 info.so_offset = -1;
497 info.fun_offset = -1;
498 info.last_text_address = 0;
499 info.nesting = 0;
500 info.fnaddr = 0;
501 info.pending_lbrac = (bfd_vma) -1;
503 /* The initial symbol holds the string size. */
504 if (! stab_write_symbol (&info, 0, 0, 0, (const char *) NULL))
505 return false;
507 /* Output an initial N_SO symbol. */
508 info.so_offset = info.symbols_size;
509 if (! stab_write_symbol (&info, N_SO, 0, 0, bfd_get_filename (abfd)))
510 return false;
512 if (! debug_write (dhandle, &stab_fns, (void *) &info))
513 return false;
515 if (info.pending_lbrac != (bfd_vma) -1)
516 return false;
518 /* Output a trailing N_SO. */
519 if (! stab_write_symbol (&info, N_SO, 0, info.last_text_address,
520 (const char *) NULL))
521 return false;
523 /* Put the string size in the initial symbol. */
524 bfd_put_32 (abfd, info.strings_size, info.symbols + 8);
526 *psyms = info.symbols;
527 *psymsize = info.symbols_size;
529 *pstringsize = info.strings_size;
530 *pstrings = (bfd_byte *) xmalloc (info.strings_size);
532 p = *pstrings;
533 *p++ = '\0';
534 for (h = info.strings; h != NULL; h = h->next)
536 strcpy ((char *) p, h->root.string);
537 p += strlen ((char *) p) + 1;
540 return true;
543 /* Start writing out information for a compilation unit. */
545 static bool
546 stab_start_compilation_unit (void *p, const char *filename)
548 struct stab_write_handle *info = (struct stab_write_handle *) p;
550 /* We would normally output an N_SO symbol here. However, that
551 would force us to reset all of our type information. I think we
552 will be better off just outputting an N_SOL symbol, and not
553 worrying about splitting information between files. */
555 info->lineno_filename = filename;
557 return stab_write_symbol (info, N_SOL, 0, 0, filename);
560 /* Start writing out information for a particular source file. */
562 static bool
563 stab_start_source (void *p, const char *filename)
565 struct stab_write_handle *info = (struct stab_write_handle *) p;
567 /* FIXME: The symbol's value is supposed to be the text section
568 address. However, we would have to fill it in later, and gdb
569 doesn't care, so we don't bother with it. */
571 info->lineno_filename = filename;
573 return stab_write_symbol (info, N_SOL, 0, 0, filename);
576 /* Push an empty type. This shouldn't normally happen. We just use a
577 void type. */
579 static bool
580 stab_empty_type (void *p)
582 struct stab_write_handle *info = (struct stab_write_handle *) p;
584 /* We don't call stab_void_type if the type is not yet defined,
585 because that might screw up the typedef. */
587 if (info->type_cache.void_type != 0)
588 return stab_push_defined_type (info, info->type_cache.void_type, 0);
589 else
591 long tindex;
592 char buf[40];
594 tindex = info->type_index;
595 ++info->type_index;
597 sprintf (buf, "%ld=%ld", tindex, tindex);
599 return stab_push_string (info, buf, tindex, false, 0);
603 /* Push a void type. */
605 static bool
606 stab_void_type (void *p)
608 struct stab_write_handle *info = (struct stab_write_handle *) p;
610 if (info->type_cache.void_type != 0)
611 return stab_push_defined_type (info, info->type_cache.void_type, 0);
612 else
614 long tindex;
615 char buf[40];
617 tindex = info->type_index;
618 ++info->type_index;
620 info->type_cache.void_type = tindex;
622 sprintf (buf, "%ld=%ld", tindex, tindex);
624 return stab_push_string (info, buf, tindex, true, 0);
628 /* Push an integer type. */
630 static bool
631 stab_int_type (void *p, unsigned int size, bool unsignedp)
633 struct stab_write_handle *info = (struct stab_write_handle *) p;
634 long *cache;
636 if (size <= 0 || (size > sizeof (long) && size != 8))
638 non_fatal (_("stab_int_type: bad size %u"), size);
639 return false;
642 if (unsignedp)
643 cache = info->type_cache.signed_integer_types;
644 else
645 cache = info->type_cache.unsigned_integer_types;
647 if (cache[size - 1] != 0)
648 return stab_push_defined_type (info, cache[size - 1], size);
649 else
651 long tindex;
652 char buf[100];
654 tindex = info->type_index;
655 ++info->type_index;
657 cache[size - 1] = tindex;
659 sprintf (buf, "%ld=r%ld;", tindex, tindex);
660 if (unsignedp)
662 strcat (buf, "0;");
663 if (size < sizeof (long))
664 sprintf (buf + strlen (buf), "%ld;", ((long) 1 << (size * 8)) - 1);
665 else if (size == sizeof (long))
666 strcat (buf, "-1;");
667 else if (size == 8)
668 strcat (buf, "01777777777777777777777;");
669 else
670 abort ();
672 else
674 if (size <= sizeof (long))
675 sprintf (buf + strlen (buf), "%ld;%ld;",
676 (long) - ((unsigned long) 1 << (size * 8 - 1)),
677 (long) (((unsigned long) 1 << (size * 8 - 1)) - 1));
678 else if (size == 8)
679 strcat (buf, "01000000000000000000000;0777777777777777777777;");
680 else
681 abort ();
684 return stab_push_string (info, buf, tindex, true, size);
688 /* Push a floating point type. */
690 static bool
691 stab_float_type (void *p, unsigned int size)
693 struct stab_write_handle *info = (struct stab_write_handle *) p;
695 if (size > 0
696 && size - 1 < (sizeof info->type_cache.float_types
697 / sizeof info->type_cache.float_types[0])
698 && info->type_cache.float_types[size - 1] != 0)
699 return stab_push_defined_type (info,
700 info->type_cache.float_types[size - 1],
701 size);
702 else
704 long tindex;
705 char *int_type;
706 char buf[50];
708 /* Floats are defined as a subrange of int. */
709 if (! stab_int_type (info, 4, false))
710 return false;
711 int_type = stab_pop_type (info);
713 tindex = info->type_index;
714 ++info->type_index;
716 if (size > 0
717 && size - 1 < (sizeof info->type_cache.float_types
718 / sizeof info->type_cache.float_types[0]))
719 info->type_cache.float_types[size - 1] = tindex;
721 sprintf (buf, "%ld=r%s;%u;0;", tindex, int_type, size);
723 free (int_type);
725 return stab_push_string (info, buf, tindex, true, size);
729 /* Push a complex type. */
731 static bool
732 stab_complex_type (void *p, unsigned int size)
734 struct stab_write_handle *info = (struct stab_write_handle *) p;
735 char buf[50];
736 long tindex;
738 tindex = info->type_index;
739 ++info->type_index;
741 sprintf (buf, "%ld=r%ld;%u;0;", tindex, tindex, size);
743 return stab_push_string (info, buf, tindex, true, size * 2);
746 /* Push a bool type. We use an XCOFF predefined type, since gdb
747 always recognizes them. */
749 static bool
750 stab_bool_type (void *p, unsigned int size)
752 struct stab_write_handle *info = (struct stab_write_handle *) p;
753 long tindex;
755 switch (size)
757 case 1:
758 tindex = -21;
759 break;
761 case 2:
762 tindex = -22;
763 break;
765 default:
766 case 4:
767 tindex = -16;
768 break;
770 case 8:
771 tindex = -33;
772 break;
775 return stab_push_defined_type (info, tindex, size);
778 /* Push an enum type. */
780 static bool
781 stab_enum_type (void *p, const char *tag, const char **names,
782 bfd_signed_vma *vals)
784 struct stab_write_handle *info = (struct stab_write_handle *) p;
785 size_t len;
786 const char **pn;
787 char *buf;
788 long tindex = 0;
789 bfd_signed_vma *pv;
791 if (names == NULL)
793 if (tag == NULL)
794 return false;
796 buf = (char *) xmalloc (10 + strlen (tag));
797 sprintf (buf, "xe%s:", tag);
798 /* FIXME: The size is just a guess. */
799 if (! stab_push_string (info, buf, 0, false, 4))
800 return false;
801 free (buf);
802 return true;
805 len = 10;
806 if (tag != NULL)
807 len += strlen (tag);
808 for (pn = names; *pn != NULL; pn++)
809 len += strlen (*pn) + 20;
811 buf = (char *) xmalloc (len);
813 if (tag == NULL)
814 strcpy (buf, "e");
815 else
817 tindex = info->type_index;
818 ++info->type_index;
819 sprintf (buf, "%s:T%ld=e", tag, tindex);
822 for (pn = names, pv = vals; *pn != NULL; pn++, pv++)
823 sprintf (buf + strlen (buf), "%s:%ld,", *pn, (long) *pv);
824 strcat (buf, ";");
826 if (tag == NULL)
828 /* FIXME: The size is just a guess. */
829 if (! stab_push_string (info, buf, 0, false, 4))
830 return false;
832 else
834 /* FIXME: The size is just a guess. */
835 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf)
836 || ! stab_push_defined_type (info, tindex, 4))
837 return false;
840 free (buf);
842 return true;
845 /* Push a modification of the top type on the stack. Cache the
846 results in CACHE and CACHE_ALLOC. */
848 static bool
849 stab_modify_type (struct stab_write_handle *info, int mod,
850 unsigned int size, long **cache, size_t *cache_alloc)
852 long targindex;
853 long tindex;
854 char *s, *buf;
856 if (info->type_stack == NULL)
857 return false;
858 targindex = info->type_stack->index;
860 if (targindex <= 0
861 || cache == NULL)
863 bool definition;
865 /* Either the target type has no index, or we aren't caching
866 this modifier. Either way we have no way of recording the
867 new type, so we don't bother to define one. */
868 definition = info->type_stack->definition;
869 s = stab_pop_type (info);
870 buf = (char *) xmalloc (strlen (s) + 2);
871 sprintf (buf, "%c%s", mod, s);
872 free (s);
873 if (! stab_push_string (info, buf, 0, definition, size))
874 return false;
875 free (buf);
877 else
879 if ((size_t) targindex >= *cache_alloc)
881 size_t alloc;
883 alloc = *cache_alloc;
884 if (alloc == 0)
885 alloc = 10;
886 while ((size_t) targindex >= alloc)
887 alloc *= 2;
888 *cache = (long *) xrealloc (*cache, alloc * sizeof (long));
889 memset (*cache + *cache_alloc, 0,
890 (alloc - *cache_alloc) * sizeof (long));
891 *cache_alloc = alloc;
894 tindex = (*cache)[targindex];
895 if (tindex != 0 && ! info->type_stack->definition)
897 /* We have already defined a modification of this type, and
898 the entry on the type stack is not a definition, so we
899 can safely discard it (we may have a definition on the
900 stack, even if we already defined a modification, if it
901 is a struct which we did not define at the time it was
902 referenced). */
903 free (stab_pop_type (info));
904 if (! stab_push_defined_type (info, tindex, size))
905 return false;
907 else
909 tindex = info->type_index;
910 ++info->type_index;
912 s = stab_pop_type (info);
913 buf = (char *) xmalloc (strlen (s) + 20);
914 sprintf (buf, "%ld=%c%s", tindex, mod, s);
915 free (s);
917 (*cache)[targindex] = tindex;
919 if (! stab_push_string (info, buf, tindex, true, size))
920 return false;
922 free (buf);
926 return true;
929 /* Push a pointer type. */
931 static bool
932 stab_pointer_type (void *p)
934 struct stab_write_handle *info = (struct stab_write_handle *) p;
936 /* FIXME: The size should depend upon the architecture. */
937 return stab_modify_type (info, '*', 4, &info->type_cache.pointer_types,
938 &info->type_cache.pointer_types_alloc);
941 /* Push a function type. */
943 static bool
944 stab_function_type (void *p, int argcount,
945 bool varargs ATTRIBUTE_UNUSED)
947 struct stab_write_handle *info = (struct stab_write_handle *) p;
948 int i;
950 /* We have no way to represent the argument types, so we just
951 discard them. However, if they define new types, we must output
952 them. We do this by producing empty typedefs. */
953 for (i = 0; i < argcount; i++)
955 if (! info->type_stack->definition)
956 free (stab_pop_type (info));
957 else
959 char *s, *buf;
961 s = stab_pop_type (info);
963 buf = (char *) xmalloc (strlen (s) + 3);
964 sprintf (buf, ":t%s", s);
965 free (s);
967 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
968 return false;
970 free (buf);
974 return stab_modify_type (info, 'f', 0, &info->type_cache.function_types,
975 &info->type_cache.function_types_alloc);
978 /* Push a reference type. */
980 static bool
981 stab_reference_type (void *p)
983 struct stab_write_handle *info = (struct stab_write_handle *) p;
985 /* FIXME: The size should depend upon the architecture. */
986 return stab_modify_type (info, '&', 4, &info->type_cache.reference_types,
987 &info->type_cache.reference_types_alloc);
990 /* Push a range type. */
992 static bool
993 stab_range_type (void *p, bfd_signed_vma low, bfd_signed_vma high)
995 struct stab_write_handle *info = (struct stab_write_handle *) p;
996 bool definition;
997 unsigned int size;
998 char *s, *buf;
1000 definition = info->type_stack->definition;
1001 size = info->type_stack->size;
1003 s = stab_pop_type (info);
1004 buf = (char *) xmalloc (strlen (s) + 100);
1005 sprintf (buf, "r%s;%ld;%ld;", s, (long) low, (long) high);
1006 free (s);
1008 if (! stab_push_string (info, buf, 0, definition, size))
1009 return false;
1011 free (buf);
1013 return true;
1016 /* Push an array type. */
1018 static bool
1019 stab_array_type (void *p, bfd_signed_vma low, bfd_signed_vma high,
1020 bool stringp)
1022 struct stab_write_handle *info = (struct stab_write_handle *) p;
1023 bool definition;
1024 unsigned int element_size;
1025 char *range, *element, *buf;
1026 long tindex;
1027 unsigned int size;
1029 definition = info->type_stack->definition;
1030 range = stab_pop_type (info);
1032 definition = definition || info->type_stack->definition;
1033 element_size = info->type_stack->size;
1034 element = stab_pop_type (info);
1036 buf = (char *) xmalloc (strlen (range) + strlen (element) + 100);
1038 if (! stringp)
1040 tindex = 0;
1041 *buf = '\0';
1043 else
1045 /* We need to define a type in order to include the string
1046 attribute. */
1047 tindex = info->type_index;
1048 ++info->type_index;
1049 definition = true;
1050 sprintf (buf, "%ld=@S;", tindex);
1053 sprintf (buf + strlen (buf), "ar%s;%ld;%ld;%s",
1054 range, (long) low, (long) high, element);
1055 free (range);
1056 free (element);
1058 if (high < low)
1059 size = 0;
1060 else
1061 size = element_size * ((high - low) + 1);
1062 if (! stab_push_string (info, buf, tindex, definition, size))
1063 return false;
1065 free (buf);
1067 return true;
1070 /* Push a set type. */
1072 static bool
1073 stab_set_type (void *p, bool bitstringp)
1075 struct stab_write_handle *info = (struct stab_write_handle *) p;
1076 bool definition;
1077 char *s, *buf;
1078 long tindex;
1080 definition = info->type_stack->definition;
1082 s = stab_pop_type (info);
1083 buf = (char *) xmalloc (strlen (s) + 30);
1085 if (! bitstringp)
1087 *buf = '\0';
1088 tindex = 0;
1090 else
1092 /* We need to define a type in order to include the string
1093 attribute. */
1094 tindex = info->type_index;
1095 ++info->type_index;
1096 definition = true;
1097 sprintf (buf, "%ld=@S;", tindex);
1100 sprintf (buf + strlen (buf), "S%s", s);
1101 free (s);
1103 if (! stab_push_string (info, buf, tindex, definition, 0))
1104 return false;
1106 free (buf);
1108 return true;
1111 /* Push an offset type. */
1113 static bool
1114 stab_offset_type (void *p)
1116 struct stab_write_handle *info = (struct stab_write_handle *) p;
1117 bool definition;
1118 char *target, *base, *buf;
1120 definition = info->type_stack->definition;
1121 target = stab_pop_type (info);
1123 definition = definition || info->type_stack->definition;
1124 base = stab_pop_type (info);
1126 buf = (char *) xmalloc (strlen (target) + strlen (base) + 3);
1127 sprintf (buf, "@%s,%s", base, target);
1128 free (base);
1129 free (target);
1131 if (! stab_push_string (info, buf, 0, definition, 0))
1132 return false;
1134 free (buf);
1136 return true;
1139 /* Push a method type. */
1141 static bool
1142 stab_method_type (void *p, bool domainp, int argcount,
1143 bool varargs)
1145 struct stab_write_handle *info = (struct stab_write_handle *) p;
1146 bool definition;
1147 char *domain, *return_type, *buf;
1148 char **args;
1149 int i;
1150 size_t len;
1152 /* We don't bother with stub method types, because that would
1153 require a mangler for C++ argument types. This will waste space
1154 in the debugging output. */
1156 /* We need a domain. I'm not sure DOMAINP can ever be false,
1157 anyhow. */
1158 if (! domainp)
1160 if (! stab_empty_type (p))
1161 return false;
1164 definition = info->type_stack->definition;
1165 domain = stab_pop_type (info);
1167 /* A non-varargs function is indicated by making the last parameter
1168 type be void. */
1170 if (argcount < 0)
1172 args = NULL;
1173 argcount = 0;
1175 else if (argcount == 0)
1177 if (varargs)
1178 args = NULL;
1179 else
1181 args = (char **) xmalloc (1 * sizeof (*args));
1182 if (! stab_empty_type (p))
1183 return false;
1184 definition = definition || info->type_stack->definition;
1185 args[0] = stab_pop_type (info);
1186 argcount = 1;
1189 else
1191 args = (char **) xmalloc ((argcount + 1) * sizeof (*args));
1192 for (i = argcount - 1; i >= 0; i--)
1194 definition = definition || info->type_stack->definition;
1195 args[i] = stab_pop_type (info);
1197 if (! varargs)
1199 if (! stab_empty_type (p))
1200 return false;
1201 definition = definition || info->type_stack->definition;
1202 args[argcount] = stab_pop_type (info);
1203 ++argcount;
1207 definition = definition || info->type_stack->definition;
1208 return_type = stab_pop_type (info);
1210 len = strlen (domain) + strlen (return_type) + 10;
1211 for (i = 0; i < argcount; i++)
1212 len += strlen (args[i]);
1214 buf = (char *) xmalloc (len);
1216 sprintf (buf, "#%s,%s", domain, return_type);
1217 free (domain);
1218 free (return_type);
1219 for (i = 0; i < argcount; i++)
1221 strcat (buf, ",");
1222 strcat (buf, args[i]);
1223 free (args[i]);
1225 strcat (buf, ";");
1227 free (args);
1229 if (! stab_push_string (info, buf, 0, definition, 0))
1230 return false;
1232 free (buf);
1234 return true;
1237 /* Push a const version of a type. */
1239 static bool
1240 stab_const_type (void *p)
1242 struct stab_write_handle *info = (struct stab_write_handle *) p;
1244 return stab_modify_type (info, 'k', info->type_stack->size,
1245 (long **) NULL, (size_t *) NULL);
1248 /* Push a volatile version of a type. */
1250 static bool
1251 stab_volatile_type (void *p)
1253 struct stab_write_handle *info = (struct stab_write_handle *) p;
1255 return stab_modify_type (info, 'B', info->type_stack->size,
1256 (long **) NULL, (size_t *) NULL);
1259 /* Get the type index to use for a struct/union/class ID. This should
1260 return -1 if it fails. */
1262 static long
1263 stab_get_struct_index (struct stab_write_handle *info, const char *tag,
1264 unsigned int id, enum debug_type_kind kind,
1265 unsigned int *psize)
1267 if (id >= info->type_cache.struct_types_alloc)
1269 size_t alloc;
1271 alloc = info->type_cache.struct_types_alloc;
1272 if (alloc == 0)
1273 alloc = 10;
1274 while (id >= alloc)
1275 alloc *= 2;
1276 info->type_cache.struct_types =
1277 (struct stab_tag *) xrealloc (info->type_cache.struct_types,
1278 alloc * sizeof (struct stab_tag));
1279 memset ((info->type_cache.struct_types
1280 + info->type_cache.struct_types_alloc),
1282 ((alloc - info->type_cache.struct_types_alloc)
1283 * sizeof (struct stab_tag)));
1284 info->type_cache.struct_types_alloc = alloc;
1287 if (info->type_cache.struct_types[id].index == 0)
1289 info->type_cache.struct_types[id].index = info->type_index;
1290 ++info->type_index;
1291 info->type_cache.struct_types[id].tag = tag;
1292 info->type_cache.struct_types[id].kind = kind;
1295 if (kind == DEBUG_KIND_ILLEGAL)
1297 /* This is a definition of the struct. */
1298 info->type_cache.struct_types[id].kind = kind;
1299 info->type_cache.struct_types[id].size = *psize;
1301 else
1302 *psize = info->type_cache.struct_types[id].size;
1304 return info->type_cache.struct_types[id].index;
1307 /* Start outputting a struct. We ignore the tag, and handle it in
1308 stab_tag. */
1310 static bool
1311 stab_start_struct_type (void *p, const char *tag, unsigned int id,
1312 bool structp, unsigned int size)
1314 struct stab_write_handle *info = (struct stab_write_handle *) p;
1315 long tindex;
1316 bool definition;
1317 char buf[40];
1319 if (id == 0)
1321 tindex = 0;
1322 *buf = '\0';
1323 definition = false;
1325 else
1327 tindex = stab_get_struct_index (info, tag, id, DEBUG_KIND_ILLEGAL,
1328 &size);
1329 if (tindex < 0)
1330 return false;
1331 sprintf (buf, "%ld=", tindex);
1332 definition = true;
1335 sprintf (buf + strlen (buf), "%c%u",
1336 structp ? 's' : 'u',
1337 size);
1339 if (! stab_push_string (info, buf, tindex, definition, size))
1340 return false;
1342 info->type_stack->fields = (char *) xmalloc (1);
1343 info->type_stack->fields[0] = '\0';
1345 return true;
1348 /* Add a field to a struct. */
1350 static bool
1351 stab_struct_field (void *p, const char *name, bfd_vma bitpos,
1352 bfd_vma bitsize, enum debug_visibility visibility)
1354 struct stab_write_handle *info = (struct stab_write_handle *) p;
1355 bool definition;
1356 unsigned int size;
1357 char *s, *n;
1358 const char *vis;
1360 definition = info->type_stack->definition;
1361 size = info->type_stack->size;
1362 s = stab_pop_type (info);
1364 /* Add this field to the end of the current struct fields, which is
1365 currently on the top of the stack. */
1366 if (info->type_stack->fields == NULL)
1367 return false;
1369 n = (char *) xmalloc (strlen (info->type_stack->fields)
1370 + strlen (name)
1371 + strlen (s)
1372 + 50);
1374 switch (visibility)
1376 default:
1377 abort ();
1379 case DEBUG_VISIBILITY_PUBLIC:
1380 vis = "";
1381 break;
1383 case DEBUG_VISIBILITY_PRIVATE:
1384 vis = "/0";
1385 break;
1387 case DEBUG_VISIBILITY_PROTECTED:
1388 vis = "/1";
1389 break;
1392 if (bitsize == 0)
1394 bitsize = size * 8;
1395 if (bitsize == 0)
1396 non_fatal (_("%s: warning: unknown size for field `%s' in struct"),
1397 bfd_get_filename (info->abfd), name);
1400 sprintf (n, "%s%s:%s%s,%ld,%ld;", info->type_stack->fields, name, vis, s,
1401 (long) bitpos, (long) bitsize);
1403 free (info->type_stack->fields);
1404 info->type_stack->fields = n;
1406 if (definition)
1407 info->type_stack->definition = true;
1409 return true;
1412 /* Finish up a struct. */
1414 static bool
1415 stab_end_struct_type (void *p)
1417 struct stab_write_handle *info = (struct stab_write_handle *) p;
1418 bool definition;
1419 long tindex;
1420 unsigned int size;
1421 char *fields, *first, *buf;
1423 if (info->type_stack == NULL || info->type_stack->fields == NULL)
1424 return false;
1426 definition = info->type_stack->definition;
1427 tindex = info->type_stack->index;
1428 size = info->type_stack->size;
1429 fields = info->type_stack->fields;
1430 first = stab_pop_type (info);
1432 buf = (char *) xmalloc (strlen (first) + strlen (fields) + 2);
1433 sprintf (buf, "%s%s;", first, fields);
1434 free (first);
1435 free (fields);
1437 if (! stab_push_string (info, buf, tindex, definition, size))
1438 return false;
1440 free (buf);
1442 return true;
1445 /* Start outputting a class. */
1447 static bool
1448 stab_start_class_type (void *p, const char *tag, unsigned int id,
1449 bool structp, unsigned int size,
1450 bool vptr, bool ownvptr)
1452 struct stab_write_handle *info = (struct stab_write_handle *) p;
1453 bool definition = false;
1454 char *vstring = NULL;
1456 if (vptr && !ownvptr)
1458 definition = info->type_stack->definition;
1459 vstring = stab_pop_type (info);
1462 if (! stab_start_struct_type (p, tag, id, structp, size))
1463 return false;
1465 if (vptr)
1467 char *vtable;
1469 if (ownvptr)
1471 if (info->type_stack->index < 1)
1472 return false;
1473 vtable = (char *) xmalloc (20);
1474 sprintf (vtable, "~%%%ld", info->type_stack->index);
1476 else
1478 if (vstring == NULL)
1479 return false;
1480 vtable = (char *) xmalloc (strlen (vstring) + 3);
1481 sprintf (vtable, "~%%%s", vstring);
1482 free (vstring);
1483 if (definition)
1484 info->type_stack->definition = true;
1486 info->type_stack->vtable = vtable;
1489 return true;
1492 /* Add a static member to the class on the type stack. */
1494 static bool
1495 stab_class_static_member (void *p, const char *name, const char *physname,
1496 enum debug_visibility visibility)
1498 struct stab_write_handle *info = (struct stab_write_handle *) p;
1499 bool definition;
1500 char *s, *n;
1501 const char *vis;
1503 definition = info->type_stack->definition;
1504 s = stab_pop_type (info);
1506 /* Add this field to the end of the current struct fields, which is
1507 currently on the top of the stack. */
1509 if (info->type_stack->fields == NULL)
1510 return false;
1511 n = (char *) xmalloc (strlen (info->type_stack->fields)
1512 + strlen (name)
1513 + strlen (s)
1514 + strlen (physname)
1515 + 10);
1517 switch (visibility)
1519 default:
1520 abort ();
1522 case DEBUG_VISIBILITY_PUBLIC:
1523 vis = "";
1524 break;
1526 case DEBUG_VISIBILITY_PRIVATE:
1527 vis = "/0";
1528 break;
1530 case DEBUG_VISIBILITY_PROTECTED:
1531 vis = "/1";
1532 break;
1535 sprintf (n, "%s%s:%s%s:%s;", info->type_stack->fields, name, vis, s,
1536 physname);
1538 free (info->type_stack->fields);
1539 info->type_stack->fields = n;
1541 if (definition)
1542 info->type_stack->definition = true;
1544 return true;
1547 /* Add a base class to the class on the type stack. */
1549 static bool
1550 stab_class_baseclass (void *p, bfd_vma bitpos, bool is_virtual,
1551 enum debug_visibility visibility)
1553 struct stab_write_handle *info = (struct stab_write_handle *) p;
1554 bool definition;
1555 char *s;
1556 char *buf;
1557 unsigned int c;
1558 char **baseclasses;
1560 definition = info->type_stack->definition;
1561 s = stab_pop_type (info);
1563 /* Build the base class specifier. */
1565 buf = (char *) xmalloc (strlen (s) + 25);
1566 buf[0] = is_virtual ? '1' : '0';
1567 switch (visibility)
1569 default:
1570 abort ();
1572 case DEBUG_VISIBILITY_PRIVATE:
1573 buf[1] = '0';
1574 break;
1576 case DEBUG_VISIBILITY_PROTECTED:
1577 buf[1] = '1';
1578 break;
1580 case DEBUG_VISIBILITY_PUBLIC:
1581 buf[1] = '2';
1582 break;
1585 sprintf (buf + 2, "%ld,%s;", (long) bitpos, s);
1586 free (s);
1588 /* Add the new baseclass to the existing ones. */
1590 if (info->type_stack == NULL || info->type_stack->fields == NULL)
1591 return false;
1593 if (info->type_stack->baseclasses == NULL)
1594 c = 0;
1595 else
1597 c = 0;
1598 while (info->type_stack->baseclasses[c] != NULL)
1599 ++c;
1602 baseclasses = (char **) xrealloc (info->type_stack->baseclasses,
1603 (c + 2) * sizeof (*baseclasses));
1604 baseclasses[c] = buf;
1605 baseclasses[c + 1] = NULL;
1607 info->type_stack->baseclasses = baseclasses;
1609 if (definition)
1610 info->type_stack->definition = true;
1612 return true;
1615 /* Start adding a method to the class on the type stack. */
1617 static bool
1618 stab_class_start_method (void *p, const char *name)
1620 struct stab_write_handle *info = (struct stab_write_handle *) p;
1621 char *m;
1623 if (info->type_stack == NULL || info->type_stack->fields == NULL)
1624 return false;
1626 if (info->type_stack->methods == NULL)
1628 m = (char *) xmalloc (strlen (name) + 3);
1629 *m = '\0';
1631 else
1633 m = (char *) xrealloc (info->type_stack->methods,
1634 (strlen (info->type_stack->methods)
1635 + strlen (name)
1636 + 4));
1639 sprintf (m + strlen (m), "%s::", name);
1641 info->type_stack->methods = m;
1643 return true;
1646 /* Add a variant, either static or not, to the current method. */
1648 static bool
1649 stab_class_method_var (struct stab_write_handle *info, const char *physname,
1650 enum debug_visibility visibility,
1651 bool staticp, bool constp,
1652 bool volatilep, bfd_vma voffset,
1653 bool contextp)
1655 bool definition;
1656 char *type;
1657 char *context = NULL;
1658 char visc, qualc, typec;
1660 definition = info->type_stack->definition;
1661 type = stab_pop_type (info);
1663 if (contextp)
1665 definition = definition || info->type_stack->definition;
1666 context = stab_pop_type (info);
1669 if (info->type_stack == NULL || info->type_stack->methods == NULL)
1670 return false;
1672 switch (visibility)
1674 default:
1675 abort ();
1677 case DEBUG_VISIBILITY_PRIVATE:
1678 visc = '0';
1679 break;
1681 case DEBUG_VISIBILITY_PROTECTED:
1682 visc = '1';
1683 break;
1685 case DEBUG_VISIBILITY_PUBLIC:
1686 visc = '2';
1687 break;
1690 if (constp)
1692 if (volatilep)
1693 qualc = 'D';
1694 else
1695 qualc = 'B';
1697 else
1699 if (volatilep)
1700 qualc = 'C';
1701 else
1702 qualc = 'A';
1705 if (staticp)
1706 typec = '?';
1707 else if (! contextp)
1708 typec = '.';
1709 else
1710 typec = '*';
1712 info->type_stack->methods =
1713 (char *) xrealloc (info->type_stack->methods,
1714 (strlen (info->type_stack->methods)
1715 + strlen (type)
1716 + strlen (physname)
1717 + (contextp ? strlen (context) : 0)
1718 + 40));
1720 sprintf (info->type_stack->methods + strlen (info->type_stack->methods),
1721 "%s:%s;%c%c%c", type, physname, visc, qualc, typec);
1722 free (type);
1724 if (contextp)
1726 sprintf (info->type_stack->methods + strlen (info->type_stack->methods),
1727 "%ld;%s;", (long) voffset, context);
1728 free (context);
1731 if (definition)
1732 info->type_stack->definition = true;
1734 return true;
1737 /* Add a variant to the current method. */
1739 static bool
1740 stab_class_method_variant (void *p, const char *physname,
1741 enum debug_visibility visibility,
1742 bool constp, bool volatilep,
1743 bfd_vma voffset, bool contextp)
1745 struct stab_write_handle *info = (struct stab_write_handle *) p;
1747 return stab_class_method_var (info, physname, visibility, false, constp,
1748 volatilep, voffset, contextp);
1751 /* Add a static variant to the current method. */
1753 static bool
1754 stab_class_static_method_variant (void *p, const char *physname,
1755 enum debug_visibility visibility,
1756 bool constp, bool volatilep)
1758 struct stab_write_handle *info = (struct stab_write_handle *) p;
1760 return stab_class_method_var (info, physname, visibility, true, constp,
1761 volatilep, 0, false);
1764 /* Finish up a method. */
1766 static bool
1767 stab_class_end_method (void *p)
1769 struct stab_write_handle *info = (struct stab_write_handle *) p;
1771 if (info->type_stack == NULL || info->type_stack->methods == NULL)
1772 return false;
1774 /* We allocated enough room on info->type_stack->methods to add the
1775 trailing semicolon. */
1776 strcat (info->type_stack->methods, ";");
1778 return true;
1781 /* Finish up a class. */
1783 static bool
1784 stab_end_class_type (void *p)
1786 struct stab_write_handle *info = (struct stab_write_handle *) p;
1787 size_t len;
1788 unsigned int i = 0;
1789 char *buf;
1791 if (info->type_stack == NULL
1792 || info->type_stack->string == NULL
1793 || info->type_stack->fields == NULL)
1794 return false;
1796 /* Work out the size we need to allocate for the class definition. */
1798 len = (strlen (info->type_stack->string)
1799 + strlen (info->type_stack->fields)
1800 + 10);
1801 if (info->type_stack->baseclasses != NULL)
1803 len += 20;
1804 for (i = 0; info->type_stack->baseclasses[i] != NULL; i++)
1805 len += strlen (info->type_stack->baseclasses[i]);
1807 if (info->type_stack->methods != NULL)
1808 len += strlen (info->type_stack->methods);
1809 if (info->type_stack->vtable != NULL)
1810 len += strlen (info->type_stack->vtable);
1812 /* Build the class definition. */
1814 buf = (char *) xmalloc (len);
1816 strcpy (buf, info->type_stack->string);
1818 if (info->type_stack->baseclasses != NULL)
1820 sprintf (buf + strlen (buf), "!%u,", i);
1821 for (i = 0; info->type_stack->baseclasses[i] != NULL; i++)
1823 strcat (buf, info->type_stack->baseclasses[i]);
1824 free (info->type_stack->baseclasses[i]);
1826 free (info->type_stack->baseclasses);
1827 info->type_stack->baseclasses = NULL;
1830 strcat (buf, info->type_stack->fields);
1831 free (info->type_stack->fields);
1832 info->type_stack->fields = NULL;
1834 if (info->type_stack->methods != NULL)
1836 strcat (buf, info->type_stack->methods);
1837 free (info->type_stack->methods);
1838 info->type_stack->methods = NULL;
1841 strcat (buf, ";");
1843 if (info->type_stack->vtable != NULL)
1845 strcat (buf, info->type_stack->vtable);
1846 free (info->type_stack->vtable);
1847 info->type_stack->vtable = NULL;
1850 /* Replace the string on the top of the stack with the complete
1851 class definition. */
1852 free (info->type_stack->string);
1853 info->type_stack->string = buf;
1855 return true;
1858 /* Push a typedef which was previously defined. */
1860 static bool
1861 stab_typedef_type (void *p, const char *name)
1863 struct stab_write_handle *info = (struct stab_write_handle *) p;
1864 struct string_hash_entry *h;
1866 h = string_hash_lookup (&info->typedef_hash, name, false, false);
1867 if (h == NULL || h->index < 1)
1868 return false;
1870 return stab_push_defined_type (info, h->index, h->size);
1873 /* Push a struct, union or class tag. */
1875 static bool
1876 stab_tag_type (void *p, const char *name, unsigned int id,
1877 enum debug_type_kind kind)
1879 struct stab_write_handle *info = (struct stab_write_handle *) p;
1880 long tindex;
1881 unsigned int size = 0;
1883 tindex = stab_get_struct_index (info, name, id, kind, &size);
1884 if (tindex < 0)
1885 return false;
1887 return stab_push_defined_type (info, tindex, size);
1890 /* Define a typedef. */
1892 static bool
1893 stab_typdef (void *p, const char *name)
1895 struct stab_write_handle *info = (struct stab_write_handle *) p;
1896 long tindex;
1897 unsigned int size;
1898 char *s, *buf;
1899 struct string_hash_entry *h;
1901 tindex = info->type_stack->index;
1902 size = info->type_stack->size;
1903 s = stab_pop_type (info);
1905 buf = (char *) xmalloc (strlen (name) + strlen (s) + 20);
1907 if (tindex > 0)
1908 sprintf (buf, "%s:t%s", name, s);
1909 else
1911 tindex = info->type_index;
1912 ++info->type_index;
1913 sprintf (buf, "%s:t%ld=%s", name, tindex, s);
1916 free (s);
1918 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
1919 return false;
1921 free (buf);
1923 h = string_hash_lookup (&info->typedef_hash, name, true, false);
1924 if (h == NULL)
1926 non_fatal (_("string_hash_lookup failed: %s"),
1927 bfd_errmsg (bfd_get_error ()));
1928 return false;
1931 /* I don't think we care about redefinitions. */
1933 h->index = tindex;
1934 h->size = size;
1936 return true;
1939 /* Define a tag. */
1941 static bool
1942 stab_tag (void *p, const char *tag)
1944 struct stab_write_handle *info = (struct stab_write_handle *) p;
1945 char *s, *buf;
1947 s = stab_pop_type (info);
1949 buf = (char *) xmalloc (strlen (tag) + strlen (s) + 3);
1951 sprintf (buf, "%s:T%s", tag, s);
1952 free (s);
1954 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
1955 return false;
1957 free (buf);
1959 return true;
1962 /* Define an integer constant. */
1964 static bool
1965 stab_int_constant (void *p, const char *name, bfd_vma val)
1967 struct stab_write_handle *info = (struct stab_write_handle *) p;
1968 char *buf;
1970 buf = (char *) xmalloc (strlen (name) + 20);
1971 sprintf (buf, "%s:c=i%ld", name, (long) val);
1973 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
1974 return false;
1976 free (buf);
1978 return true;
1981 /* Define a floating point constant. */
1983 static bool
1984 stab_float_constant (void *p, const char *name, double val)
1986 struct stab_write_handle *info = (struct stab_write_handle *) p;
1987 char *buf;
1989 buf = (char *) xmalloc (strlen (name) + 20);
1990 sprintf (buf, "%s:c=f%g", name, val);
1992 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
1993 return false;
1995 free (buf);
1997 return true;
2000 /* Define a typed constant. */
2002 static bool
2003 stab_typed_constant (void *p, const char *name, bfd_vma val)
2005 struct stab_write_handle *info = (struct stab_write_handle *) p;
2006 char *s, *buf;
2008 s = stab_pop_type (info);
2010 buf = (char *) xmalloc (strlen (name) + strlen (s) + 20);
2011 sprintf (buf, "%s:c=e%s,%ld", name, s, (long) val);
2012 free (s);
2014 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
2015 return false;
2017 free (buf);
2019 return true;
2022 /* Record a variable. */
2024 static bool
2025 stab_variable (void *p, const char *name, enum debug_var_kind kind,
2026 bfd_vma val)
2028 struct stab_write_handle *info = (struct stab_write_handle *) p;
2029 char *s, *buf;
2030 int stab_type;
2031 const char *kindstr;
2033 s = stab_pop_type (info);
2035 switch (kind)
2037 default:
2038 abort ();
2040 case DEBUG_GLOBAL:
2041 stab_type = N_GSYM;
2042 kindstr = "G";
2043 break;
2045 case DEBUG_STATIC:
2046 stab_type = N_STSYM;
2047 kindstr = "S";
2048 break;
2050 case DEBUG_LOCAL_STATIC:
2051 stab_type = N_STSYM;
2052 kindstr = "V";
2053 break;
2055 case DEBUG_LOCAL:
2056 stab_type = N_LSYM;
2057 kindstr = "";
2059 /* Make sure that this is a type reference or definition. */
2060 if (! ISDIGIT (*s))
2062 char *n;
2063 long tindex;
2065 tindex = info->type_index;
2066 ++info->type_index;
2067 n = (char *) xmalloc (strlen (s) + 20);
2068 sprintf (n, "%ld=%s", tindex, s);
2069 free (s);
2070 s = n;
2072 break;
2074 case DEBUG_REGISTER:
2075 stab_type = N_RSYM;
2076 kindstr = "r";
2077 break;
2080 buf = (char *) xmalloc (strlen (name) + strlen (s) + 3);
2081 sprintf (buf, "%s:%s%s", name, kindstr, s);
2082 free (s);
2084 if (! stab_write_symbol (info, stab_type, 0, val, buf))
2085 return false;
2087 free (buf);
2089 return true;
2092 /* Start outputting a function. */
2094 static bool
2095 stab_start_function (void *p, const char *name, bool globalp)
2097 struct stab_write_handle *info = (struct stab_write_handle *) p;
2098 char *rettype, *buf;
2100 if (info->nesting != 0 || info->fun_offset != -1)
2101 return false;
2103 rettype = stab_pop_type (info);
2105 buf = (char *) xmalloc (strlen (name) + strlen (rettype) + 3);
2106 sprintf (buf, "%s:%c%s", name,
2107 globalp ? 'F' : 'f',
2108 rettype);
2110 /* We don't know the value now, so we set it in start_block. */
2111 info->fun_offset = info->symbols_size;
2113 if (! stab_write_symbol (info, N_FUN, 0, 0, buf))
2114 return false;
2116 free (buf);
2118 return true;
2121 /* Output a function parameter. */
2123 static bool
2124 stab_function_parameter (void *p, const char *name, enum debug_parm_kind kind, bfd_vma val)
2126 struct stab_write_handle *info = (struct stab_write_handle *) p;
2127 char *s, *buf;
2128 int stab_type;
2129 char kindc;
2131 s = stab_pop_type (info);
2133 switch (kind)
2135 default:
2136 abort ();
2138 case DEBUG_PARM_STACK:
2139 stab_type = N_PSYM;
2140 kindc = 'p';
2141 break;
2143 case DEBUG_PARM_REG:
2144 stab_type = N_RSYM;
2145 kindc = 'P';
2146 break;
2148 case DEBUG_PARM_REFERENCE:
2149 stab_type = N_PSYM;
2150 kindc = 'v';
2151 break;
2153 case DEBUG_PARM_REF_REG:
2154 stab_type = N_RSYM;
2155 kindc = 'a';
2156 break;
2159 buf = (char *) xmalloc (strlen (name) + strlen (s) + 3);
2160 sprintf (buf, "%s:%c%s", name, kindc, s);
2161 free (s);
2163 if (! stab_write_symbol (info, stab_type, 0, val, buf))
2164 return false;
2166 free (buf);
2168 return true;
2171 /* Start a block. */
2173 static bool
2174 stab_start_block (void *p, bfd_vma addr)
2176 struct stab_write_handle *info = (struct stab_write_handle *) p;
2178 /* Fill in any slots which have been waiting for the first known
2179 text address. */
2181 if (info->so_offset != -1)
2183 bfd_put_32 (info->abfd, addr, info->symbols + info->so_offset + 8);
2184 info->so_offset = -1;
2187 if (info->fun_offset != -1)
2189 bfd_put_32 (info->abfd, addr, info->symbols + info->fun_offset + 8);
2190 info->fun_offset = -1;
2193 ++info->nesting;
2195 /* We will be called with a top level block surrounding the
2196 function, but stabs information does not output that block, so we
2197 ignore it. */
2199 if (info->nesting == 1)
2201 info->fnaddr = addr;
2202 return true;
2205 /* We have to output the LBRAC symbol after any variables which are
2206 declared inside the block. We postpone the LBRAC until the next
2207 start_block or end_block. */
2209 /* If we have postponed an LBRAC, output it now. */
2210 if (info->pending_lbrac != (bfd_vma) -1)
2212 if (! stab_write_symbol (info, N_LBRAC, 0, info->pending_lbrac,
2213 (const char *) NULL))
2214 return false;
2217 /* Remember the address and output it later. */
2219 info->pending_lbrac = addr - info->fnaddr;
2221 return true;
2224 /* End a block. */
2226 static bool
2227 stab_end_block (void *p, bfd_vma addr)
2229 struct stab_write_handle *info = (struct stab_write_handle *) p;
2231 if (addr > info->last_text_address)
2232 info->last_text_address = addr;
2234 /* If we have postponed an LBRAC, output it now. */
2235 if (info->pending_lbrac != (bfd_vma) -1)
2237 if (! stab_write_symbol (info, N_LBRAC, 0, info->pending_lbrac,
2238 (const char *) NULL))
2239 return false;
2240 info->pending_lbrac = (bfd_vma) -1;
2243 if (info->nesting < 1)
2244 return false;
2246 --info->nesting;
2248 /* We ignore the outermost block. */
2249 if (info->nesting == 0)
2250 return true;
2252 return stab_write_symbol (info, N_RBRAC, 0, addr - info->fnaddr,
2253 (const char *) NULL);
2256 /* End a function. */
2258 static bool
2259 stab_end_function (void *p ATTRIBUTE_UNUSED)
2261 return true;
2264 /* Output a line number. */
2266 static bool
2267 stab_lineno (void *p, const char *file, unsigned long lineno, bfd_vma addr)
2269 struct stab_write_handle *info = (struct stab_write_handle *) p;
2271 if (info->lineno_filename == NULL)
2272 return false;
2274 if (addr > info->last_text_address)
2275 info->last_text_address = addr;
2277 if (filename_cmp (file, info->lineno_filename) != 0)
2279 if (! stab_write_symbol (info, N_SOL, 0, addr, file))
2280 return false;
2281 info->lineno_filename = file;
2284 return stab_write_symbol (info, N_SLINE, lineno, addr - info->fnaddr,
2285 (const char *) NULL);