Update my e-mail address.
[binutils-gdb.git] / binutils / wrstabs.c
blob54a14af3ebb919a61dae131de9d6f7394a745f34
1 /* wrstabs.c -- Output stabs debugging information
2 Copyright (C) 1996-2017 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 bfd_boolean 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 bfd_boolean stab_write_symbol
177 (struct stab_write_handle *, int, int, bfd_vma, const char *);
178 static bfd_boolean stab_push_string
179 (struct stab_write_handle *, const char *, long, bfd_boolean, unsigned int);
180 static bfd_boolean stab_push_defined_type
181 (struct stab_write_handle *, long, unsigned int);
182 static char *stab_pop_type (struct stab_write_handle *);
183 static bfd_boolean 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 bfd_boolean stab_class_method_var
189 (struct stab_write_handle *, const char *, enum debug_visibility,
190 bfd_boolean, bfd_boolean, bfd_boolean, bfd_vma, bfd_boolean);
191 static bfd_boolean stab_start_compilation_unit (void *, const char *);
192 static bfd_boolean stab_start_source (void *, const char *);
193 static bfd_boolean stab_empty_type (void *);
194 static bfd_boolean stab_void_type (void *);
195 static bfd_boolean stab_int_type (void *, unsigned int, bfd_boolean);
196 static bfd_boolean stab_float_type (void *, unsigned int);
197 static bfd_boolean stab_complex_type (void *, unsigned int);
198 static bfd_boolean stab_bool_type (void *, unsigned int);
199 static bfd_boolean stab_enum_type
200 (void *, const char *, const char **, bfd_signed_vma *);
201 static bfd_boolean stab_pointer_type (void *);
202 static bfd_boolean stab_function_type (void *, int, bfd_boolean);
203 static bfd_boolean stab_reference_type (void *);
204 static bfd_boolean stab_range_type (void *, bfd_signed_vma, bfd_signed_vma);
205 static bfd_boolean stab_array_type
206 (void *, bfd_signed_vma, bfd_signed_vma, bfd_boolean);
207 static bfd_boolean stab_set_type (void *, bfd_boolean);
208 static bfd_boolean stab_offset_type (void *);
209 static bfd_boolean stab_method_type (void *, bfd_boolean, int, bfd_boolean);
210 static bfd_boolean stab_const_type (void *);
211 static bfd_boolean stab_volatile_type (void *);
212 static bfd_boolean stab_start_struct_type
213 (void *, const char *, unsigned int, bfd_boolean, unsigned int);
214 static bfd_boolean stab_struct_field
215 (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
216 static bfd_boolean stab_end_struct_type (void *);
217 static bfd_boolean stab_start_class_type
218 (void *, const char *, unsigned int, bfd_boolean, unsigned int,
219 bfd_boolean, bfd_boolean);
220 static bfd_boolean stab_class_static_member
221 (void *, const char *, const char *, enum debug_visibility);
222 static bfd_boolean stab_class_baseclass
223 (void *, bfd_vma, bfd_boolean, enum debug_visibility);
224 static bfd_boolean stab_class_start_method (void *, const char *);
225 static bfd_boolean stab_class_method_variant
226 (void *, const char *, enum debug_visibility, bfd_boolean, bfd_boolean,
227 bfd_vma, bfd_boolean);
228 static bfd_boolean stab_class_static_method_variant
229 (void *, const char *, enum debug_visibility, bfd_boolean, bfd_boolean);
230 static bfd_boolean stab_class_end_method (void *);
231 static bfd_boolean stab_end_class_type (void *);
232 static bfd_boolean stab_typedef_type (void *, const char *);
233 static bfd_boolean stab_tag_type
234 (void *, const char *, unsigned int, enum debug_type_kind);
235 static bfd_boolean stab_typdef (void *, const char *);
236 static bfd_boolean stab_tag (void *, const char *);
237 static bfd_boolean stab_int_constant (void *, const char *, bfd_vma);
238 static bfd_boolean stab_float_constant (void *, const char *, double);
239 static bfd_boolean stab_typed_constant (void *, const char *, bfd_vma);
240 static bfd_boolean stab_variable
241 (void *, const char *, enum debug_var_kind, bfd_vma);
242 static bfd_boolean stab_start_function (void *, const char *, bfd_boolean);
243 static bfd_boolean stab_function_parameter
244 (void *, const char *, enum debug_parm_kind, bfd_vma);
245 static bfd_boolean stab_start_block (void *, bfd_vma);
246 static bfd_boolean stab_end_block (void *, bfd_vma);
247 static bfd_boolean stab_end_function (void *);
248 static bfd_boolean 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 bfd_boolean
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 bfd_boolean
396 stab_push_string (struct stab_write_handle *info, const char *string,
397 long tindex, bfd_boolean 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 bfd_boolean
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 assert (s != NULL);
442 info->type_stack = s->next;
444 ret = s->string;
446 free (s);
448 return ret;
451 /* The general routine to write out stabs in sections debugging
452 information. This accumulates the stabs symbols and the strings in
453 two obstacks. We can't easily write out the information as we go
454 along, because we need to know the section sizes before we can
455 write out the section contents. ABFD is the BFD and DHANDLE is the
456 handle for the debugging information. This sets *PSYMS to point to
457 the symbols, *PSYMSIZE the size of the symbols, *PSTRINGS to the
458 strings, and *PSTRINGSIZE to the size of the strings. */
460 bfd_boolean
461 write_stabs_in_sections_debugging_info (bfd *abfd, void *dhandle,
462 bfd_byte **psyms,
463 bfd_size_type *psymsize,
464 bfd_byte **pstrings,
465 bfd_size_type *pstringsize)
467 struct stab_write_handle info;
468 struct string_hash_entry *h;
469 bfd_byte *p;
471 info.abfd = abfd;
473 info.symbols_size = 0;
474 info.symbols_alloc = 500;
475 info.symbols = (bfd_byte *) xmalloc (info.symbols_alloc);
477 info.strings = NULL;
478 info.last_string = NULL;
479 /* Reserve 1 byte for a null byte. */
480 info.strings_size = 1;
482 if (!bfd_hash_table_init (&info.strhash.table, string_hash_newfunc,
483 sizeof (struct string_hash_entry))
484 || !bfd_hash_table_init (&info.typedef_hash.table, string_hash_newfunc,
485 sizeof (struct string_hash_entry)))
487 non_fatal ("bfd_hash_table_init_failed: %s",
488 bfd_errmsg (bfd_get_error ()));
489 return FALSE;
492 info.type_stack = NULL;
493 info.type_index = 1;
494 memset (&info.type_cache, 0, sizeof info.type_cache);
495 info.so_offset = -1;
496 info.fun_offset = -1;
497 info.last_text_address = 0;
498 info.nesting = 0;
499 info.fnaddr = 0;
500 info.pending_lbrac = (bfd_vma) -1;
502 /* The initial symbol holds the string size. */
503 if (! stab_write_symbol (&info, 0, 0, 0, (const char *) NULL))
504 return FALSE;
506 /* Output an initial N_SO symbol. */
507 info.so_offset = info.symbols_size;
508 if (! stab_write_symbol (&info, N_SO, 0, 0, bfd_get_filename (abfd)))
509 return FALSE;
511 if (! debug_write (dhandle, &stab_fns, (void *) &info))
512 return FALSE;
514 assert (info.pending_lbrac == (bfd_vma) -1);
516 /* Output a trailing N_SO. */
517 if (! stab_write_symbol (&info, N_SO, 0, info.last_text_address,
518 (const char *) NULL))
519 return FALSE;
521 /* Put the string size in the initial symbol. */
522 bfd_put_32 (abfd, info.strings_size, info.symbols + 8);
524 *psyms = info.symbols;
525 *psymsize = info.symbols_size;
527 *pstringsize = info.strings_size;
528 *pstrings = (bfd_byte *) xmalloc (info.strings_size);
530 p = *pstrings;
531 *p++ = '\0';
532 for (h = info.strings; h != NULL; h = h->next)
534 strcpy ((char *) p, h->root.string);
535 p += strlen ((char *) p) + 1;
538 return TRUE;
541 /* Start writing out information for a compilation unit. */
543 static bfd_boolean
544 stab_start_compilation_unit (void *p, const char *filename)
546 struct stab_write_handle *info = (struct stab_write_handle *) p;
548 /* We would normally output an N_SO symbol here. However, that
549 would force us to reset all of our type information. I think we
550 will be better off just outputting an N_SOL symbol, and not
551 worrying about splitting information between files. */
553 info->lineno_filename = filename;
555 return stab_write_symbol (info, N_SOL, 0, 0, filename);
558 /* Start writing out information for a particular source file. */
560 static bfd_boolean
561 stab_start_source (void *p, const char *filename)
563 struct stab_write_handle *info = (struct stab_write_handle *) p;
565 /* FIXME: The symbol's value is supposed to be the text section
566 address. However, we would have to fill it in later, and gdb
567 doesn't care, so we don't bother with it. */
569 info->lineno_filename = filename;
571 return stab_write_symbol (info, N_SOL, 0, 0, filename);
574 /* Push an empty type. This shouldn't normally happen. We just use a
575 void type. */
577 static bfd_boolean
578 stab_empty_type (void *p)
580 struct stab_write_handle *info = (struct stab_write_handle *) p;
582 /* We don't call stab_void_type if the type is not yet defined,
583 because that might screw up the typedef. */
585 if (info->type_cache.void_type != 0)
586 return stab_push_defined_type (info, info->type_cache.void_type, 0);
587 else
589 long tindex;
590 char buf[40];
592 tindex = info->type_index;
593 ++info->type_index;
595 sprintf (buf, "%ld=%ld", tindex, tindex);
597 return stab_push_string (info, buf, tindex, FALSE, 0);
601 /* Push a void type. */
603 static bfd_boolean
604 stab_void_type (void *p)
606 struct stab_write_handle *info = (struct stab_write_handle *) p;
608 if (info->type_cache.void_type != 0)
609 return stab_push_defined_type (info, info->type_cache.void_type, 0);
610 else
612 long tindex;
613 char buf[40];
615 tindex = info->type_index;
616 ++info->type_index;
618 info->type_cache.void_type = tindex;
620 sprintf (buf, "%ld=%ld", tindex, tindex);
622 return stab_push_string (info, buf, tindex, TRUE, 0);
626 /* Push an integer type. */
628 static bfd_boolean
629 stab_int_type (void *p, unsigned int size, bfd_boolean unsignedp)
631 struct stab_write_handle *info = (struct stab_write_handle *) p;
632 long *cache;
634 if (size <= 0 || (size > sizeof (long) && size != 8))
636 non_fatal (_("stab_int_type: bad size %u"), size);
637 return FALSE;
640 if (unsignedp)
641 cache = info->type_cache.signed_integer_types;
642 else
643 cache = info->type_cache.unsigned_integer_types;
645 if (cache[size - 1] != 0)
646 return stab_push_defined_type (info, cache[size - 1], size);
647 else
649 long tindex;
650 char buf[100];
652 tindex = info->type_index;
653 ++info->type_index;
655 cache[size - 1] = tindex;
657 sprintf (buf, "%ld=r%ld;", tindex, tindex);
658 if (unsignedp)
660 strcat (buf, "0;");
661 if (size < sizeof (long))
662 sprintf (buf + strlen (buf), "%ld;", ((long) 1 << (size * 8)) - 1);
663 else if (size == sizeof (long))
664 strcat (buf, "-1;");
665 else if (size == 8)
666 strcat (buf, "01777777777777777777777;");
667 else
668 abort ();
670 else
672 if (size <= sizeof (long))
673 sprintf (buf + strlen (buf), "%ld;%ld;",
674 (long) - ((unsigned long) 1 << (size * 8 - 1)),
675 (long) (((unsigned long) 1 << (size * 8 - 1)) - 1));
676 else if (size == 8)
677 strcat (buf, "01000000000000000000000;0777777777777777777777;");
678 else
679 abort ();
682 return stab_push_string (info, buf, tindex, TRUE, size);
686 /* Push a floating point type. */
688 static bfd_boolean
689 stab_float_type (void *p, unsigned int size)
691 struct stab_write_handle *info = (struct stab_write_handle *) p;
693 if (size > 0
694 && size - 1 < (sizeof info->type_cache.float_types
695 / sizeof info->type_cache.float_types[0])
696 && info->type_cache.float_types[size - 1] != 0)
697 return stab_push_defined_type (info,
698 info->type_cache.float_types[size - 1],
699 size);
700 else
702 long tindex;
703 char *int_type;
704 char buf[50];
706 /* Floats are defined as a subrange of int. */
707 if (! stab_int_type (info, 4, FALSE))
708 return FALSE;
709 int_type = stab_pop_type (info);
711 tindex = info->type_index;
712 ++info->type_index;
714 if (size > 0
715 && size - 1 < (sizeof info->type_cache.float_types
716 / sizeof info->type_cache.float_types[0]))
717 info->type_cache.float_types[size - 1] = tindex;
719 sprintf (buf, "%ld=r%s;%u;0;", tindex, int_type, size);
721 free (int_type);
723 return stab_push_string (info, buf, tindex, TRUE, size);
727 /* Push a complex type. */
729 static bfd_boolean
730 stab_complex_type (void *p, unsigned int size)
732 struct stab_write_handle *info = (struct stab_write_handle *) p;
733 char buf[50];
734 long tindex;
736 tindex = info->type_index;
737 ++info->type_index;
739 sprintf (buf, "%ld=r%ld;%u;0;", tindex, tindex, size);
741 return stab_push_string (info, buf, tindex, TRUE, size * 2);
744 /* Push a bfd_boolean type. We use an XCOFF predefined type, since gdb
745 always recognizes them. */
747 static bfd_boolean
748 stab_bool_type (void *p, unsigned int size)
750 struct stab_write_handle *info = (struct stab_write_handle *) p;
751 long tindex;
753 switch (size)
755 case 1:
756 tindex = -21;
757 break;
759 case 2:
760 tindex = -22;
761 break;
763 default:
764 case 4:
765 tindex = -16;
766 break;
768 case 8:
769 tindex = -33;
770 break;
773 return stab_push_defined_type (info, tindex, size);
776 /* Push an enum type. */
778 static bfd_boolean
779 stab_enum_type (void *p, const char *tag, const char **names,
780 bfd_signed_vma *vals)
782 struct stab_write_handle *info = (struct stab_write_handle *) p;
783 size_t len;
784 const char **pn;
785 char *buf;
786 long tindex = 0;
787 bfd_signed_vma *pv;
789 if (names == NULL)
791 assert (tag != NULL);
793 buf = (char *) xmalloc (10 + strlen (tag));
794 sprintf (buf, "xe%s:", tag);
795 /* FIXME: The size is just a guess. */
796 if (! stab_push_string (info, buf, 0, FALSE, 4))
797 return FALSE;
798 free (buf);
799 return TRUE;
802 len = 10;
803 if (tag != NULL)
804 len += strlen (tag);
805 for (pn = names; *pn != NULL; pn++)
806 len += strlen (*pn) + 20;
808 buf = (char *) xmalloc (len);
810 if (tag == NULL)
811 strcpy (buf, "e");
812 else
814 tindex = info->type_index;
815 ++info->type_index;
816 sprintf (buf, "%s:T%ld=e", tag, tindex);
819 for (pn = names, pv = vals; *pn != NULL; pn++, pv++)
820 sprintf (buf + strlen (buf), "%s:%ld,", *pn, (long) *pv);
821 strcat (buf, ";");
823 if (tag == NULL)
825 /* FIXME: The size is just a guess. */
826 if (! stab_push_string (info, buf, 0, FALSE, 4))
827 return FALSE;
829 else
831 /* FIXME: The size is just a guess. */
832 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf)
833 || ! stab_push_defined_type (info, tindex, 4))
834 return FALSE;
837 free (buf);
839 return TRUE;
842 /* Push a modification of the top type on the stack. Cache the
843 results in CACHE and CACHE_ALLOC. */
845 static bfd_boolean
846 stab_modify_type (struct stab_write_handle *info, int mod,
847 unsigned int size, long **cache, size_t *cache_alloc)
849 long targindex;
850 long tindex;
851 char *s, *buf;
853 assert (info->type_stack != NULL);
854 targindex = info->type_stack->index;
856 if (targindex <= 0
857 || cache == NULL)
859 bfd_boolean definition;
861 /* Either the target type has no index, or we aren't caching
862 this modifier. Either way we have no way of recording the
863 new type, so we don't bother to define one. */
864 definition = info->type_stack->definition;
865 s = stab_pop_type (info);
866 buf = (char *) xmalloc (strlen (s) + 2);
867 sprintf (buf, "%c%s", mod, s);
868 free (s);
869 if (! stab_push_string (info, buf, 0, definition, size))
870 return FALSE;
871 free (buf);
873 else
875 if ((size_t) targindex >= *cache_alloc)
877 size_t alloc;
879 alloc = *cache_alloc;
880 if (alloc == 0)
881 alloc = 10;
882 while ((size_t) targindex >= alloc)
883 alloc *= 2;
884 *cache = (long *) xrealloc (*cache, alloc * sizeof (long));
885 memset (*cache + *cache_alloc, 0,
886 (alloc - *cache_alloc) * sizeof (long));
887 *cache_alloc = alloc;
890 tindex = (*cache)[targindex];
891 if (tindex != 0 && ! info->type_stack->definition)
893 /* We have already defined a modification of this type, and
894 the entry on the type stack is not a definition, so we
895 can safely discard it (we may have a definition on the
896 stack, even if we already defined a modification, if it
897 is a struct which we did not define at the time it was
898 referenced). */
899 free (stab_pop_type (info));
900 if (! stab_push_defined_type (info, tindex, size))
901 return FALSE;
903 else
905 tindex = info->type_index;
906 ++info->type_index;
908 s = stab_pop_type (info);
909 buf = (char *) xmalloc (strlen (s) + 20);
910 sprintf (buf, "%ld=%c%s", tindex, mod, s);
911 free (s);
913 (*cache)[targindex] = tindex;
915 if (! stab_push_string (info, buf, tindex, TRUE, size))
916 return FALSE;
918 free (buf);
922 return TRUE;
925 /* Push a pointer type. */
927 static bfd_boolean
928 stab_pointer_type (void *p)
930 struct stab_write_handle *info = (struct stab_write_handle *) p;
932 /* FIXME: The size should depend upon the architecture. */
933 return stab_modify_type (info, '*', 4, &info->type_cache.pointer_types,
934 &info->type_cache.pointer_types_alloc);
937 /* Push a function type. */
939 static bfd_boolean
940 stab_function_type (void *p, int argcount,
941 bfd_boolean varargs ATTRIBUTE_UNUSED)
943 struct stab_write_handle *info = (struct stab_write_handle *) p;
944 int i;
946 /* We have no way to represent the argument types, so we just
947 discard them. However, if they define new types, we must output
948 them. We do this by producing empty typedefs. */
949 for (i = 0; i < argcount; i++)
951 if (! info->type_stack->definition)
952 free (stab_pop_type (info));
953 else
955 char *s, *buf;
957 s = stab_pop_type (info);
959 buf = (char *) xmalloc (strlen (s) + 3);
960 sprintf (buf, ":t%s", s);
961 free (s);
963 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
964 return FALSE;
966 free (buf);
970 return stab_modify_type (info, 'f', 0, &info->type_cache.function_types,
971 &info->type_cache.function_types_alloc);
974 /* Push a reference type. */
976 static bfd_boolean
977 stab_reference_type (void *p)
979 struct stab_write_handle *info = (struct stab_write_handle *) p;
981 /* FIXME: The size should depend upon the architecture. */
982 return stab_modify_type (info, '&', 4, &info->type_cache.reference_types,
983 &info->type_cache.reference_types_alloc);
986 /* Push a range type. */
988 static bfd_boolean
989 stab_range_type (void *p, bfd_signed_vma low, bfd_signed_vma high)
991 struct stab_write_handle *info = (struct stab_write_handle *) p;
992 bfd_boolean definition;
993 unsigned int size;
994 char *s, *buf;
996 definition = info->type_stack->definition;
997 size = info->type_stack->size;
999 s = stab_pop_type (info);
1000 buf = (char *) xmalloc (strlen (s) + 100);
1001 sprintf (buf, "r%s;%ld;%ld;", s, (long) low, (long) high);
1002 free (s);
1004 if (! stab_push_string (info, buf, 0, definition, size))
1005 return FALSE;
1007 free (buf);
1009 return TRUE;
1012 /* Push an array type. */
1014 static bfd_boolean
1015 stab_array_type (void *p, bfd_signed_vma low, bfd_signed_vma high,
1016 bfd_boolean stringp)
1018 struct stab_write_handle *info = (struct stab_write_handle *) p;
1019 bfd_boolean definition;
1020 unsigned int element_size;
1021 char *range, *element, *buf;
1022 long tindex;
1023 unsigned int size;
1025 definition = info->type_stack->definition;
1026 range = stab_pop_type (info);
1028 definition = definition || info->type_stack->definition;
1029 element_size = info->type_stack->size;
1030 element = stab_pop_type (info);
1032 buf = (char *) xmalloc (strlen (range) + strlen (element) + 100);
1034 if (! stringp)
1036 tindex = 0;
1037 *buf = '\0';
1039 else
1041 /* We need to define a type in order to include the string
1042 attribute. */
1043 tindex = info->type_index;
1044 ++info->type_index;
1045 definition = TRUE;
1046 sprintf (buf, "%ld=@S;", tindex);
1049 sprintf (buf + strlen (buf), "ar%s;%ld;%ld;%s",
1050 range, (long) low, (long) high, element);
1051 free (range);
1052 free (element);
1054 if (high < low)
1055 size = 0;
1056 else
1057 size = element_size * ((high - low) + 1);
1058 if (! stab_push_string (info, buf, tindex, definition, size))
1059 return FALSE;
1061 free (buf);
1063 return TRUE;
1066 /* Push a set type. */
1068 static bfd_boolean
1069 stab_set_type (void *p, bfd_boolean bitstringp)
1071 struct stab_write_handle *info = (struct stab_write_handle *) p;
1072 bfd_boolean definition;
1073 char *s, *buf;
1074 long tindex;
1076 definition = info->type_stack->definition;
1078 s = stab_pop_type (info);
1079 buf = (char *) xmalloc (strlen (s) + 30);
1081 if (! bitstringp)
1083 *buf = '\0';
1084 tindex = 0;
1086 else
1088 /* We need to define a type in order to include the string
1089 attribute. */
1090 tindex = info->type_index;
1091 ++info->type_index;
1092 definition = TRUE;
1093 sprintf (buf, "%ld=@S;", tindex);
1096 sprintf (buf + strlen (buf), "S%s", s);
1097 free (s);
1099 if (! stab_push_string (info, buf, tindex, definition, 0))
1100 return FALSE;
1102 free (buf);
1104 return TRUE;
1107 /* Push an offset type. */
1109 static bfd_boolean
1110 stab_offset_type (void *p)
1112 struct stab_write_handle *info = (struct stab_write_handle *) p;
1113 bfd_boolean definition;
1114 char *target, *base, *buf;
1116 definition = info->type_stack->definition;
1117 target = stab_pop_type (info);
1119 definition = definition || info->type_stack->definition;
1120 base = stab_pop_type (info);
1122 buf = (char *) xmalloc (strlen (target) + strlen (base) + 3);
1123 sprintf (buf, "@%s,%s", base, target);
1124 free (base);
1125 free (target);
1127 if (! stab_push_string (info, buf, 0, definition, 0))
1128 return FALSE;
1130 free (buf);
1132 return TRUE;
1135 /* Push a method type. */
1137 static bfd_boolean
1138 stab_method_type (void *p, bfd_boolean domainp, int argcount,
1139 bfd_boolean varargs)
1141 struct stab_write_handle *info = (struct stab_write_handle *) p;
1142 bfd_boolean definition;
1143 char *domain, *return_type, *buf;
1144 char **args;
1145 int i;
1146 size_t len;
1148 /* We don't bother with stub method types, because that would
1149 require a mangler for C++ argument types. This will waste space
1150 in the debugging output. */
1152 /* We need a domain. I'm not sure DOMAINP can ever be false,
1153 anyhow. */
1154 if (! domainp)
1156 if (! stab_empty_type (p))
1157 return FALSE;
1160 definition = info->type_stack->definition;
1161 domain = stab_pop_type (info);
1163 /* A non-varargs function is indicated by making the last parameter
1164 type be void. */
1166 if (argcount < 0)
1168 args = NULL;
1169 argcount = 0;
1171 else if (argcount == 0)
1173 if (varargs)
1174 args = NULL;
1175 else
1177 args = (char **) xmalloc (1 * sizeof (*args));
1178 if (! stab_empty_type (p))
1179 return FALSE;
1180 definition = definition || info->type_stack->definition;
1181 args[0] = stab_pop_type (info);
1182 argcount = 1;
1185 else
1187 args = (char **) xmalloc ((argcount + 1) * sizeof (*args));
1188 for (i = argcount - 1; i >= 0; i--)
1190 definition = definition || info->type_stack->definition;
1191 args[i] = stab_pop_type (info);
1193 if (! varargs)
1195 if (! stab_empty_type (p))
1196 return FALSE;
1197 definition = definition || info->type_stack->definition;
1198 args[argcount] = stab_pop_type (info);
1199 ++argcount;
1203 definition = definition || info->type_stack->definition;
1204 return_type = stab_pop_type (info);
1206 len = strlen (domain) + strlen (return_type) + 10;
1207 for (i = 0; i < argcount; i++)
1208 len += strlen (args[i]);
1210 buf = (char *) xmalloc (len);
1212 sprintf (buf, "#%s,%s", domain, return_type);
1213 free (domain);
1214 free (return_type);
1215 for (i = 0; i < argcount; i++)
1217 strcat (buf, ",");
1218 strcat (buf, args[i]);
1219 free (args[i]);
1221 strcat (buf, ";");
1223 if (args != NULL)
1224 free (args);
1226 if (! stab_push_string (info, buf, 0, definition, 0))
1227 return FALSE;
1229 free (buf);
1231 return TRUE;
1234 /* Push a const version of a type. */
1236 static bfd_boolean
1237 stab_const_type (void *p)
1239 struct stab_write_handle *info = (struct stab_write_handle *) p;
1241 return stab_modify_type (info, 'k', info->type_stack->size,
1242 (long **) NULL, (size_t *) NULL);
1245 /* Push a volatile version of a type. */
1247 static bfd_boolean
1248 stab_volatile_type (void *p)
1250 struct stab_write_handle *info = (struct stab_write_handle *) p;
1252 return stab_modify_type (info, 'B', info->type_stack->size,
1253 (long **) NULL, (size_t *) NULL);
1256 /* Get the type index to use for a struct/union/class ID. This should
1257 return -1 if it fails. */
1259 static long
1260 stab_get_struct_index (struct stab_write_handle *info, const char *tag,
1261 unsigned int id, enum debug_type_kind kind,
1262 unsigned int *psize)
1264 if (id >= info->type_cache.struct_types_alloc)
1266 size_t alloc;
1268 alloc = info->type_cache.struct_types_alloc;
1269 if (alloc == 0)
1270 alloc = 10;
1271 while (id >= alloc)
1272 alloc *= 2;
1273 info->type_cache.struct_types =
1274 (struct stab_tag *) xrealloc (info->type_cache.struct_types,
1275 alloc * sizeof (struct stab_tag));
1276 memset ((info->type_cache.struct_types
1277 + info->type_cache.struct_types_alloc),
1279 ((alloc - info->type_cache.struct_types_alloc)
1280 * sizeof (struct stab_tag)));
1281 info->type_cache.struct_types_alloc = alloc;
1284 if (info->type_cache.struct_types[id].index == 0)
1286 info->type_cache.struct_types[id].index = info->type_index;
1287 ++info->type_index;
1288 info->type_cache.struct_types[id].tag = tag;
1289 info->type_cache.struct_types[id].kind = kind;
1292 if (kind == DEBUG_KIND_ILLEGAL)
1294 /* This is a definition of the struct. */
1295 info->type_cache.struct_types[id].kind = kind;
1296 info->type_cache.struct_types[id].size = *psize;
1298 else
1299 *psize = info->type_cache.struct_types[id].size;
1301 return info->type_cache.struct_types[id].index;
1304 /* Start outputting a struct. We ignore the tag, and handle it in
1305 stab_tag. */
1307 static bfd_boolean
1308 stab_start_struct_type (void *p, const char *tag, unsigned int id,
1309 bfd_boolean structp, unsigned int size)
1311 struct stab_write_handle *info = (struct stab_write_handle *) p;
1312 long tindex;
1313 bfd_boolean definition;
1314 char buf[40];
1316 if (id == 0)
1318 tindex = 0;
1319 *buf = '\0';
1320 definition = FALSE;
1322 else
1324 tindex = stab_get_struct_index (info, tag, id, DEBUG_KIND_ILLEGAL,
1325 &size);
1326 if (tindex < 0)
1327 return FALSE;
1328 sprintf (buf, "%ld=", tindex);
1329 definition = TRUE;
1332 sprintf (buf + strlen (buf), "%c%u",
1333 structp ? 's' : 'u',
1334 size);
1336 if (! stab_push_string (info, buf, tindex, definition, size))
1337 return FALSE;
1339 info->type_stack->fields = (char *) xmalloc (1);
1340 info->type_stack->fields[0] = '\0';
1342 return TRUE;
1345 /* Add a field to a struct. */
1347 static bfd_boolean
1348 stab_struct_field (void *p, const char *name, bfd_vma bitpos,
1349 bfd_vma bitsize, enum debug_visibility visibility)
1351 struct stab_write_handle *info = (struct stab_write_handle *) p;
1352 bfd_boolean definition;
1353 unsigned int size;
1354 char *s, *n;
1355 const char *vis;
1357 definition = info->type_stack->definition;
1358 size = info->type_stack->size;
1359 s = stab_pop_type (info);
1361 /* Add this field to the end of the current struct fields, which is
1362 currently on the top of the stack. */
1364 assert (info->type_stack->fields != NULL);
1365 n = (char *) xmalloc (strlen (info->type_stack->fields)
1366 + strlen (name)
1367 + strlen (s)
1368 + 50);
1370 switch (visibility)
1372 default:
1373 abort ();
1375 case DEBUG_VISIBILITY_PUBLIC:
1376 vis = "";
1377 break;
1379 case DEBUG_VISIBILITY_PRIVATE:
1380 vis = "/0";
1381 break;
1383 case DEBUG_VISIBILITY_PROTECTED:
1384 vis = "/1";
1385 break;
1388 if (bitsize == 0)
1390 bitsize = size * 8;
1391 if (bitsize == 0)
1392 non_fatal (_("%s: warning: unknown size for field `%s' in struct"),
1393 bfd_get_filename (info->abfd), name);
1396 sprintf (n, "%s%s:%s%s,%ld,%ld;", info->type_stack->fields, name, vis, s,
1397 (long) bitpos, (long) bitsize);
1399 free (info->type_stack->fields);
1400 info->type_stack->fields = n;
1402 if (definition)
1403 info->type_stack->definition = TRUE;
1405 return TRUE;
1408 /* Finish up a struct. */
1410 static bfd_boolean
1411 stab_end_struct_type (void *p)
1413 struct stab_write_handle *info = (struct stab_write_handle *) p;
1414 bfd_boolean definition;
1415 long tindex;
1416 unsigned int size;
1417 char *fields, *first, *buf;
1419 assert (info->type_stack != NULL && info->type_stack->fields != NULL);
1421 definition = info->type_stack->definition;
1422 tindex = info->type_stack->index;
1423 size = info->type_stack->size;
1424 fields = info->type_stack->fields;
1425 first = stab_pop_type (info);
1427 buf = (char *) xmalloc (strlen (first) + strlen (fields) + 2);
1428 sprintf (buf, "%s%s;", first, fields);
1429 free (first);
1430 free (fields);
1432 if (! stab_push_string (info, buf, tindex, definition, size))
1433 return FALSE;
1435 free (buf);
1437 return TRUE;
1440 /* Start outputting a class. */
1442 static bfd_boolean
1443 stab_start_class_type (void *p, const char *tag, unsigned int id, bfd_boolean structp, unsigned int size, bfd_boolean vptr, bfd_boolean ownvptr)
1445 struct stab_write_handle *info = (struct stab_write_handle *) p;
1446 bfd_boolean definition;
1447 char *vstring;
1449 if (! vptr || ownvptr)
1451 definition = FALSE;
1452 vstring = NULL;
1454 else
1456 definition = info->type_stack->definition;
1457 vstring = stab_pop_type (info);
1460 if (! stab_start_struct_type (p, tag, id, structp, size))
1461 return FALSE;
1463 if (vptr)
1465 char *vtable;
1467 if (ownvptr)
1469 assert (info->type_stack->index > 0);
1470 vtable = (char *) xmalloc (20);
1471 sprintf (vtable, "~%%%ld", info->type_stack->index);
1473 else
1475 vtable = (char *) xmalloc (strlen (vstring) + 3);
1476 sprintf (vtable, "~%%%s", vstring);
1477 free (vstring);
1480 info->type_stack->vtable = vtable;
1483 if (definition)
1484 info->type_stack->definition = TRUE;
1486 return TRUE;
1489 /* Add a static member to the class on the type stack. */
1491 static bfd_boolean
1492 stab_class_static_member (void *p, const char *name, const char *physname,
1493 enum debug_visibility visibility)
1495 struct stab_write_handle *info = (struct stab_write_handle *) p;
1496 bfd_boolean definition;
1497 char *s, *n;
1498 const char *vis;
1500 definition = info->type_stack->definition;
1501 s = stab_pop_type (info);
1503 /* Add this field to the end of the current struct fields, which is
1504 currently on the top of the stack. */
1506 assert (info->type_stack->fields != NULL);
1507 n = (char *) xmalloc (strlen (info->type_stack->fields)
1508 + strlen (name)
1509 + strlen (s)
1510 + strlen (physname)
1511 + 10);
1513 switch (visibility)
1515 default:
1516 abort ();
1518 case DEBUG_VISIBILITY_PUBLIC:
1519 vis = "";
1520 break;
1522 case DEBUG_VISIBILITY_PRIVATE:
1523 vis = "/0";
1524 break;
1526 case DEBUG_VISIBILITY_PROTECTED:
1527 vis = "/1";
1528 break;
1531 sprintf (n, "%s%s:%s%s:%s;", info->type_stack->fields, name, vis, s,
1532 physname);
1534 free (info->type_stack->fields);
1535 info->type_stack->fields = n;
1537 if (definition)
1538 info->type_stack->definition = TRUE;
1540 return TRUE;
1543 /* Add a base class to the class on the type stack. */
1545 static bfd_boolean
1546 stab_class_baseclass (void *p, bfd_vma bitpos, bfd_boolean is_virtual,
1547 enum debug_visibility visibility)
1549 struct stab_write_handle *info = (struct stab_write_handle *) p;
1550 bfd_boolean definition;
1551 char *s;
1552 char *buf;
1553 unsigned int c;
1554 char **baseclasses;
1556 definition = info->type_stack->definition;
1557 s = stab_pop_type (info);
1559 /* Build the base class specifier. */
1561 buf = (char *) xmalloc (strlen (s) + 25);
1562 buf[0] = is_virtual ? '1' : '0';
1563 switch (visibility)
1565 default:
1566 abort ();
1568 case DEBUG_VISIBILITY_PRIVATE:
1569 buf[1] = '0';
1570 break;
1572 case DEBUG_VISIBILITY_PROTECTED:
1573 buf[1] = '1';
1574 break;
1576 case DEBUG_VISIBILITY_PUBLIC:
1577 buf[1] = '2';
1578 break;
1581 sprintf (buf + 2, "%ld,%s;", (long) bitpos, s);
1582 free (s);
1584 /* Add the new baseclass to the existing ones. */
1586 assert (info->type_stack != NULL && info->type_stack->fields != NULL);
1588 if (info->type_stack->baseclasses == NULL)
1589 c = 0;
1590 else
1592 c = 0;
1593 while (info->type_stack->baseclasses[c] != NULL)
1594 ++c;
1597 baseclasses = (char **) xrealloc (info->type_stack->baseclasses,
1598 (c + 2) * sizeof (*baseclasses));
1599 baseclasses[c] = buf;
1600 baseclasses[c + 1] = NULL;
1602 info->type_stack->baseclasses = baseclasses;
1604 if (definition)
1605 info->type_stack->definition = TRUE;
1607 return TRUE;
1610 /* Start adding a method to the class on the type stack. */
1612 static bfd_boolean
1613 stab_class_start_method (void *p, const char *name)
1615 struct stab_write_handle *info = (struct stab_write_handle *) p;
1616 char *m;
1618 assert (info->type_stack != NULL && info->type_stack->fields != NULL);
1620 if (info->type_stack->methods == NULL)
1622 m = (char *) xmalloc (strlen (name) + 3);
1623 *m = '\0';
1625 else
1627 m = (char *) xrealloc (info->type_stack->methods,
1628 (strlen (info->type_stack->methods)
1629 + strlen (name)
1630 + 4));
1633 sprintf (m + strlen (m), "%s::", name);
1635 info->type_stack->methods = m;
1637 return TRUE;
1640 /* Add a variant, either static or not, to the current method. */
1642 static bfd_boolean
1643 stab_class_method_var (struct stab_write_handle *info, const char *physname,
1644 enum debug_visibility visibility,
1645 bfd_boolean staticp, bfd_boolean constp,
1646 bfd_boolean volatilep, bfd_vma voffset,
1647 bfd_boolean contextp)
1649 bfd_boolean definition;
1650 char *type;
1651 char *context = NULL;
1652 char visc, qualc, typec;
1654 definition = info->type_stack->definition;
1655 type = stab_pop_type (info);
1657 if (contextp)
1659 definition = definition || info->type_stack->definition;
1660 context = stab_pop_type (info);
1663 assert (info->type_stack != NULL && info->type_stack->methods != NULL);
1665 switch (visibility)
1667 default:
1668 abort ();
1670 case DEBUG_VISIBILITY_PRIVATE:
1671 visc = '0';
1672 break;
1674 case DEBUG_VISIBILITY_PROTECTED:
1675 visc = '1';
1676 break;
1678 case DEBUG_VISIBILITY_PUBLIC:
1679 visc = '2';
1680 break;
1683 if (constp)
1685 if (volatilep)
1686 qualc = 'D';
1687 else
1688 qualc = 'B';
1690 else
1692 if (volatilep)
1693 qualc = 'C';
1694 else
1695 qualc = 'A';
1698 if (staticp)
1699 typec = '?';
1700 else if (! contextp)
1701 typec = '.';
1702 else
1703 typec = '*';
1705 info->type_stack->methods =
1706 (char *) xrealloc (info->type_stack->methods,
1707 (strlen (info->type_stack->methods)
1708 + strlen (type)
1709 + strlen (physname)
1710 + (contextp ? strlen (context) : 0)
1711 + 40));
1713 sprintf (info->type_stack->methods + strlen (info->type_stack->methods),
1714 "%s:%s;%c%c%c", type, physname, visc, qualc, typec);
1715 free (type);
1717 if (contextp)
1719 sprintf (info->type_stack->methods + strlen (info->type_stack->methods),
1720 "%ld;%s;", (long) voffset, context);
1721 free (context);
1724 if (definition)
1725 info->type_stack->definition = TRUE;
1727 return TRUE;
1730 /* Add a variant to the current method. */
1732 static bfd_boolean
1733 stab_class_method_variant (void *p, const char *physname,
1734 enum debug_visibility visibility,
1735 bfd_boolean constp, bfd_boolean volatilep,
1736 bfd_vma voffset, bfd_boolean contextp)
1738 struct stab_write_handle *info = (struct stab_write_handle *) p;
1740 return stab_class_method_var (info, physname, visibility, FALSE, constp,
1741 volatilep, voffset, contextp);
1744 /* Add a static variant to the current method. */
1746 static bfd_boolean
1747 stab_class_static_method_variant (void *p, const char *physname,
1748 enum debug_visibility visibility,
1749 bfd_boolean constp, bfd_boolean volatilep)
1751 struct stab_write_handle *info = (struct stab_write_handle *) p;
1753 return stab_class_method_var (info, physname, visibility, TRUE, constp,
1754 volatilep, 0, FALSE);
1757 /* Finish up a method. */
1759 static bfd_boolean
1760 stab_class_end_method (void *p)
1762 struct stab_write_handle *info = (struct stab_write_handle *) p;
1764 assert (info->type_stack != NULL && info->type_stack->methods != NULL);
1766 /* We allocated enough room on info->type_stack->methods to add the
1767 trailing semicolon. */
1768 strcat (info->type_stack->methods, ";");
1770 return TRUE;
1773 /* Finish up a class. */
1775 static bfd_boolean
1776 stab_end_class_type (void *p)
1778 struct stab_write_handle *info = (struct stab_write_handle *) p;
1779 size_t len;
1780 unsigned int i = 0;
1781 char *buf;
1783 assert (info->type_stack != NULL && info->type_stack->fields != NULL);
1785 /* Work out the size we need to allocate for the class definition. */
1787 len = (strlen (info->type_stack->string)
1788 + strlen (info->type_stack->fields)
1789 + 10);
1790 if (info->type_stack->baseclasses != NULL)
1792 len += 20;
1793 for (i = 0; info->type_stack->baseclasses[i] != NULL; i++)
1794 len += strlen (info->type_stack->baseclasses[i]);
1796 if (info->type_stack->methods != NULL)
1797 len += strlen (info->type_stack->methods);
1798 if (info->type_stack->vtable != NULL)
1799 len += strlen (info->type_stack->vtable);
1801 /* Build the class definition. */
1803 buf = (char *) xmalloc (len);
1805 strcpy (buf, info->type_stack->string);
1807 if (info->type_stack->baseclasses != NULL)
1809 sprintf (buf + strlen (buf), "!%u,", i);
1810 for (i = 0; info->type_stack->baseclasses[i] != NULL; i++)
1812 strcat (buf, info->type_stack->baseclasses[i]);
1813 free (info->type_stack->baseclasses[i]);
1815 free (info->type_stack->baseclasses);
1816 info->type_stack->baseclasses = NULL;
1819 strcat (buf, info->type_stack->fields);
1820 free (info->type_stack->fields);
1821 info->type_stack->fields = NULL;
1823 if (info->type_stack->methods != NULL)
1825 strcat (buf, info->type_stack->methods);
1826 free (info->type_stack->methods);
1827 info->type_stack->methods = NULL;
1830 strcat (buf, ";");
1832 if (info->type_stack->vtable != NULL)
1834 strcat (buf, info->type_stack->vtable);
1835 free (info->type_stack->vtable);
1836 info->type_stack->vtable = NULL;
1839 /* Replace the string on the top of the stack with the complete
1840 class definition. */
1841 free (info->type_stack->string);
1842 info->type_stack->string = buf;
1844 return TRUE;
1847 /* Push a typedef which was previously defined. */
1849 static bfd_boolean
1850 stab_typedef_type (void *p, const char *name)
1852 struct stab_write_handle *info = (struct stab_write_handle *) p;
1853 struct string_hash_entry *h;
1855 h = string_hash_lookup (&info->typedef_hash, name, FALSE, FALSE);
1856 assert (h != NULL && h->index > 0);
1858 return stab_push_defined_type (info, h->index, h->size);
1861 /* Push a struct, union or class tag. */
1863 static bfd_boolean
1864 stab_tag_type (void *p, const char *name, unsigned int id,
1865 enum debug_type_kind kind)
1867 struct stab_write_handle *info = (struct stab_write_handle *) p;
1868 long tindex;
1869 unsigned int size = 0;
1871 tindex = stab_get_struct_index (info, name, id, kind, &size);
1872 if (tindex < 0)
1873 return FALSE;
1875 return stab_push_defined_type (info, tindex, size);
1878 /* Define a typedef. */
1880 static bfd_boolean
1881 stab_typdef (void *p, const char *name)
1883 struct stab_write_handle *info = (struct stab_write_handle *) p;
1884 long tindex;
1885 unsigned int size;
1886 char *s, *buf;
1887 struct string_hash_entry *h;
1889 tindex = info->type_stack->index;
1890 size = info->type_stack->size;
1891 s = stab_pop_type (info);
1893 buf = (char *) xmalloc (strlen (name) + strlen (s) + 20);
1895 if (tindex > 0)
1896 sprintf (buf, "%s:t%s", name, s);
1897 else
1899 tindex = info->type_index;
1900 ++info->type_index;
1901 sprintf (buf, "%s:t%ld=%s", name, tindex, s);
1904 free (s);
1906 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
1907 return FALSE;
1909 free (buf);
1911 h = string_hash_lookup (&info->typedef_hash, name, TRUE, FALSE);
1912 if (h == NULL)
1914 non_fatal (_("string_hash_lookup failed: %s"),
1915 bfd_errmsg (bfd_get_error ()));
1916 return FALSE;
1919 /* I don't think we care about redefinitions. */
1921 h->index = tindex;
1922 h->size = size;
1924 return TRUE;
1927 /* Define a tag. */
1929 static bfd_boolean
1930 stab_tag (void *p, const char *tag)
1932 struct stab_write_handle *info = (struct stab_write_handle *) p;
1933 char *s, *buf;
1935 s = stab_pop_type (info);
1937 buf = (char *) xmalloc (strlen (tag) + strlen (s) + 3);
1939 sprintf (buf, "%s:T%s", tag, s);
1940 free (s);
1942 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
1943 return FALSE;
1945 free (buf);
1947 return TRUE;
1950 /* Define an integer constant. */
1952 static bfd_boolean
1953 stab_int_constant (void *p, const char *name, bfd_vma val)
1955 struct stab_write_handle *info = (struct stab_write_handle *) p;
1956 char *buf;
1958 buf = (char *) xmalloc (strlen (name) + 20);
1959 sprintf (buf, "%s:c=i%ld", name, (long) val);
1961 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
1962 return FALSE;
1964 free (buf);
1966 return TRUE;
1969 /* Define a floating point constant. */
1971 static bfd_boolean
1972 stab_float_constant (void *p, const char *name, double val)
1974 struct stab_write_handle *info = (struct stab_write_handle *) p;
1975 char *buf;
1977 buf = (char *) xmalloc (strlen (name) + 20);
1978 sprintf (buf, "%s:c=f%g", name, val);
1980 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
1981 return FALSE;
1983 free (buf);
1985 return TRUE;
1988 /* Define a typed constant. */
1990 static bfd_boolean
1991 stab_typed_constant (void *p, const char *name, bfd_vma val)
1993 struct stab_write_handle *info = (struct stab_write_handle *) p;
1994 char *s, *buf;
1996 s = stab_pop_type (info);
1998 buf = (char *) xmalloc (strlen (name) + strlen (s) + 20);
1999 sprintf (buf, "%s:c=e%s,%ld", name, s, (long) val);
2000 free (s);
2002 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
2003 return FALSE;
2005 free (buf);
2007 return TRUE;
2010 /* Record a variable. */
2012 static bfd_boolean
2013 stab_variable (void *p, const char *name, enum debug_var_kind kind,
2014 bfd_vma val)
2016 struct stab_write_handle *info = (struct stab_write_handle *) p;
2017 char *s, *buf;
2018 int stab_type;
2019 const char *kindstr;
2021 s = stab_pop_type (info);
2023 switch (kind)
2025 default:
2026 abort ();
2028 case DEBUG_GLOBAL:
2029 stab_type = N_GSYM;
2030 kindstr = "G";
2031 break;
2033 case DEBUG_STATIC:
2034 stab_type = N_STSYM;
2035 kindstr = "S";
2036 break;
2038 case DEBUG_LOCAL_STATIC:
2039 stab_type = N_STSYM;
2040 kindstr = "V";
2041 break;
2043 case DEBUG_LOCAL:
2044 stab_type = N_LSYM;
2045 kindstr = "";
2047 /* Make sure that this is a type reference or definition. */
2048 if (! ISDIGIT (*s))
2050 char *n;
2051 long tindex;
2053 tindex = info->type_index;
2054 ++info->type_index;
2055 n = (char *) xmalloc (strlen (s) + 20);
2056 sprintf (n, "%ld=%s", tindex, s);
2057 free (s);
2058 s = n;
2060 break;
2062 case DEBUG_REGISTER:
2063 stab_type = N_RSYM;
2064 kindstr = "r";
2065 break;
2068 buf = (char *) xmalloc (strlen (name) + strlen (s) + 3);
2069 sprintf (buf, "%s:%s%s", name, kindstr, s);
2070 free (s);
2072 if (! stab_write_symbol (info, stab_type, 0, val, buf))
2073 return FALSE;
2075 free (buf);
2077 return TRUE;
2080 /* Start outputting a function. */
2082 static bfd_boolean
2083 stab_start_function (void *p, const char *name, bfd_boolean globalp)
2085 struct stab_write_handle *info = (struct stab_write_handle *) p;
2086 char *rettype, *buf;
2088 assert (info->nesting == 0 && info->fun_offset == -1);
2090 rettype = stab_pop_type (info);
2092 buf = (char *) xmalloc (strlen (name) + strlen (rettype) + 3);
2093 sprintf (buf, "%s:%c%s", name,
2094 globalp ? 'F' : 'f',
2095 rettype);
2097 /* We don't know the value now, so we set it in start_block. */
2098 info->fun_offset = info->symbols_size;
2100 if (! stab_write_symbol (info, N_FUN, 0, 0, buf))
2101 return FALSE;
2103 free (buf);
2105 return TRUE;
2108 /* Output a function parameter. */
2110 static bfd_boolean
2111 stab_function_parameter (void *p, const char *name, enum debug_parm_kind kind, bfd_vma val)
2113 struct stab_write_handle *info = (struct stab_write_handle *) p;
2114 char *s, *buf;
2115 int stab_type;
2116 char kindc;
2118 s = stab_pop_type (info);
2120 switch (kind)
2122 default:
2123 abort ();
2125 case DEBUG_PARM_STACK:
2126 stab_type = N_PSYM;
2127 kindc = 'p';
2128 break;
2130 case DEBUG_PARM_REG:
2131 stab_type = N_RSYM;
2132 kindc = 'P';
2133 break;
2135 case DEBUG_PARM_REFERENCE:
2136 stab_type = N_PSYM;
2137 kindc = 'v';
2138 break;
2140 case DEBUG_PARM_REF_REG:
2141 stab_type = N_RSYM;
2142 kindc = 'a';
2143 break;
2146 buf = (char *) xmalloc (strlen (name) + strlen (s) + 3);
2147 sprintf (buf, "%s:%c%s", name, kindc, s);
2148 free (s);
2150 if (! stab_write_symbol (info, stab_type, 0, val, buf))
2151 return FALSE;
2153 free (buf);
2155 return TRUE;
2158 /* Start a block. */
2160 static bfd_boolean
2161 stab_start_block (void *p, bfd_vma addr)
2163 struct stab_write_handle *info = (struct stab_write_handle *) p;
2165 /* Fill in any slots which have been waiting for the first known
2166 text address. */
2168 if (info->so_offset != -1)
2170 bfd_put_32 (info->abfd, addr, info->symbols + info->so_offset + 8);
2171 info->so_offset = -1;
2174 if (info->fun_offset != -1)
2176 bfd_put_32 (info->abfd, addr, info->symbols + info->fun_offset + 8);
2177 info->fun_offset = -1;
2180 ++info->nesting;
2182 /* We will be called with a top level block surrounding the
2183 function, but stabs information does not output that block, so we
2184 ignore it. */
2186 if (info->nesting == 1)
2188 info->fnaddr = addr;
2189 return TRUE;
2192 /* We have to output the LBRAC symbol after any variables which are
2193 declared inside the block. We postpone the LBRAC until the next
2194 start_block or end_block. */
2196 /* If we have postponed an LBRAC, output it now. */
2197 if (info->pending_lbrac != (bfd_vma) -1)
2199 if (! stab_write_symbol (info, N_LBRAC, 0, info->pending_lbrac,
2200 (const char *) NULL))
2201 return FALSE;
2204 /* Remember the address and output it later. */
2206 info->pending_lbrac = addr - info->fnaddr;
2208 return TRUE;
2211 /* End a block. */
2213 static bfd_boolean
2214 stab_end_block (void *p, bfd_vma addr)
2216 struct stab_write_handle *info = (struct stab_write_handle *) p;
2218 if (addr > info->last_text_address)
2219 info->last_text_address = addr;
2221 /* If we have postponed an LBRAC, output it now. */
2222 if (info->pending_lbrac != (bfd_vma) -1)
2224 if (! stab_write_symbol (info, N_LBRAC, 0, info->pending_lbrac,
2225 (const char *) NULL))
2226 return FALSE;
2227 info->pending_lbrac = (bfd_vma) -1;
2230 assert (info->nesting > 0);
2232 --info->nesting;
2234 /* We ignore the outermost block. */
2235 if (info->nesting == 0)
2236 return TRUE;
2238 return stab_write_symbol (info, N_RBRAC, 0, addr - info->fnaddr,
2239 (const char *) NULL);
2242 /* End a function. */
2244 static bfd_boolean
2245 stab_end_function (void *p ATTRIBUTE_UNUSED)
2247 return TRUE;
2250 /* Output a line number. */
2252 static bfd_boolean
2253 stab_lineno (void *p, const char *file, unsigned long lineno, bfd_vma addr)
2255 struct stab_write_handle *info = (struct stab_write_handle *) p;
2257 assert (info->lineno_filename != NULL);
2259 if (addr > info->last_text_address)
2260 info->last_text_address = addr;
2262 if (filename_cmp (file, info->lineno_filename) != 0)
2264 if (! stab_write_symbol (info, N_SOL, 0, addr, file))
2265 return FALSE;
2266 info->lineno_filename = file;
2269 return stab_write_symbol (info, N_SLINE, lineno, addr - info->fnaddr,
2270 (const char *) NULL);