Properly report filename for alignment reduction.
[binutils.git] / binutils / wrstabs.c
blob77c7d72a306193ccedd926b5fd00d34d1ed6fcc6
1 /* wrstabs.c -- Output stabs debugging information
2 Copyright 1996, 1997, 1998, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@cygnus.com>.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
23 /* This file contains code which writes out stabs debugging
24 information. */
26 #include <stdio.h>
27 #include <assert.h>
29 #include "bfd.h"
30 #include "bucomm.h"
31 #include "libiberty.h"
32 #include "safe-ctype.h"
33 #include "debug.h"
34 #include "budbg.h"
35 #include "aout/aout64.h"
36 #include "aout/stab_gnu.h"
38 /* The size of a stabs symbol. This presumes 32 bit values. */
40 #define STAB_SYMBOL_SIZE (12)
42 /* An entry in a string hash table. */
44 struct string_hash_entry
46 struct bfd_hash_entry root;
47 /* Next string in this table. */
48 struct string_hash_entry *next;
49 /* Index in string table. */
50 long index;
51 /* Size of type if this is a typedef. */
52 unsigned int size;
55 /* A string hash table. */
57 struct string_hash_table
59 struct bfd_hash_table table;
62 /* The type stack. Each element on the stack is a string. */
64 struct stab_type_stack
66 /* The next element on the stack. */
67 struct stab_type_stack *next;
68 /* This element as a string. */
69 char *string;
70 /* The type index of this element. */
71 long index;
72 /* The size of the type. */
73 unsigned int size;
74 /* Whether type string defines a new type. */
75 bfd_boolean definition;
76 /* String defining struct fields. */
77 char *fields;
78 /* NULL terminated array of strings defining base classes for a
79 class. */
80 char **baseclasses;
81 /* String defining class methods. */
82 char *methods;
83 /* String defining vtable pointer for a class. */
84 char *vtable;
87 /* This structure is used to keep track of type indices for tagged
88 types. */
90 struct stab_tag
92 /* The type index. */
93 long index;
94 /* The tag name. */
95 const char *tag;
96 /* The kind of type. This is set to DEBUG_KIND_ILLEGAL when the
97 type is defined. */
98 enum debug_type_kind kind;
99 /* The size of the struct. */
100 unsigned int size;
103 /* We remember various sorts of type indices. They are not related,
104 but, for convenience, we keep all the information in this
105 structure. */
107 struct stab_type_cache
109 /* The void type index. */
110 long void_type;
111 /* Signed integer type indices, indexed by size - 1. */
112 long signed_integer_types[8];
113 /* Unsigned integer type indices, indexed by size - 1. */
114 long unsigned_integer_types[8];
115 /* Floating point types, indexed by size - 1. */
116 long float_types[16];
117 /* Pointers to types, indexed by the type index. */
118 long *pointer_types;
119 size_t pointer_types_alloc;
120 /* Functions returning types, indexed by the type index. */
121 long *function_types;
122 size_t function_types_alloc;
123 /* References to types, indexed by the type index. */
124 long *reference_types;
125 size_t reference_types_alloc;
126 /* Struct/union/class type indices, indexed by the struct id. */
127 struct stab_tag *struct_types;
128 size_t struct_types_alloc;
131 /* This is the handle passed through debug_write. */
133 struct stab_write_handle
135 /* The BFD. */
136 bfd *abfd;
137 /* This buffer holds the symbols. */
138 bfd_byte *symbols;
139 size_t symbols_size;
140 size_t symbols_alloc;
141 /* This is a list of hash table entries for the strings. */
142 struct string_hash_entry *strings;
143 /* The last string hash table entry. */
144 struct string_hash_entry *last_string;
145 /* The size of the strings. */
146 size_t strings_size;
147 /* This hash table eliminates duplicate strings. */
148 struct string_hash_table strhash;
149 /* The type stack. */
150 struct stab_type_stack *type_stack;
151 /* The next type index. */
152 long type_index;
153 /* The type cache. */
154 struct stab_type_cache type_cache;
155 /* A mapping from typedef names to type indices. */
156 struct string_hash_table typedef_hash;
157 /* If this is not -1, it is the offset to the most recent N_SO
158 symbol, and the value of that symbol needs to be set. */
159 long so_offset;
160 /* If this is not -1, it is the offset to the most recent N_FUN
161 symbol, and the value of that symbol needs to be set. */
162 long fun_offset;
163 /* The last text section address seen. */
164 bfd_vma last_text_address;
165 /* The block nesting depth. */
166 unsigned int nesting;
167 /* The function address. */
168 bfd_vma fnaddr;
169 /* A pending LBRAC symbol. */
170 bfd_vma pending_lbrac;
171 /* The current line number file name. */
172 const char *lineno_filename;
175 static struct bfd_hash_entry *string_hash_newfunc
176 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
177 static bfd_boolean stab_write_symbol
178 PARAMS ((struct stab_write_handle *, int, int, bfd_vma, const char *));
179 static bfd_boolean stab_push_string
180 PARAMS ((struct stab_write_handle *, const char *, long, bfd_boolean,
181 unsigned int));
182 static bfd_boolean stab_push_defined_type
183 PARAMS ((struct stab_write_handle *, long, unsigned int));
184 static char *stab_pop_type
185 PARAMS ((struct stab_write_handle *));
186 static bfd_boolean stab_modify_type
187 PARAMS ((struct stab_write_handle *, int, unsigned int, long **, size_t *));
188 static long stab_get_struct_index
189 PARAMS ((struct stab_write_handle *, const char *, unsigned int,
190 enum debug_type_kind, unsigned int *));
191 static bfd_boolean stab_class_method_var
192 PARAMS ((struct stab_write_handle *, const char *, enum debug_visibility,
193 bfd_boolean, bfd_boolean, bfd_boolean, bfd_vma, bfd_boolean));
194 static bfd_boolean stab_start_compilation_unit
195 PARAMS ((PTR, const char *));
196 static bfd_boolean stab_start_source
197 PARAMS ((PTR, const char *));
198 static bfd_boolean stab_empty_type
199 PARAMS ((PTR));
200 static bfd_boolean stab_void_type
201 PARAMS ((PTR));
202 static bfd_boolean stab_int_type
203 PARAMS ((PTR, unsigned int, bfd_boolean));
204 static bfd_boolean stab_float_type
205 PARAMS ((PTR, unsigned int));
206 static bfd_boolean stab_complex_type
207 PARAMS ((PTR, unsigned int));
208 static bfd_boolean stab_bool_type
209 PARAMS ((PTR, unsigned int));
210 static bfd_boolean stab_enum_type
211 PARAMS ((PTR, const char *, const char **, bfd_signed_vma *));
212 static bfd_boolean stab_pointer_type
213 PARAMS ((PTR));
214 static bfd_boolean stab_function_type
215 PARAMS ((PTR, int, bfd_boolean));
216 static bfd_boolean stab_reference_type
217 PARAMS ((PTR));
218 static bfd_boolean stab_range_type
219 PARAMS ((PTR, bfd_signed_vma, bfd_signed_vma));
220 static bfd_boolean stab_array_type
221 PARAMS ((PTR, bfd_signed_vma, bfd_signed_vma, bfd_boolean));
222 static bfd_boolean stab_set_type
223 PARAMS ((PTR, bfd_boolean));
224 static bfd_boolean stab_offset_type
225 PARAMS ((PTR));
226 static bfd_boolean stab_method_type
227 PARAMS ((PTR, bfd_boolean, int, bfd_boolean));
228 static bfd_boolean stab_const_type
229 PARAMS ((PTR));
230 static bfd_boolean stab_volatile_type
231 PARAMS ((PTR));
232 static bfd_boolean stab_start_struct_type
233 PARAMS ((PTR, const char *, unsigned int, bfd_boolean, unsigned int));
234 static bfd_boolean stab_struct_field
235 PARAMS ((PTR, const char *, bfd_vma, bfd_vma, enum debug_visibility));
236 static bfd_boolean stab_end_struct_type
237 PARAMS ((PTR));
238 static bfd_boolean stab_start_class_type
239 PARAMS ((PTR, const char *, unsigned int, bfd_boolean, unsigned int,
240 bfd_boolean, bfd_boolean));
241 static bfd_boolean stab_class_static_member
242 PARAMS ((PTR, const char *, const char *, enum debug_visibility));
243 static bfd_boolean stab_class_baseclass
244 PARAMS ((PTR, bfd_vma, bfd_boolean, enum debug_visibility));
245 static bfd_boolean stab_class_start_method
246 PARAMS ((PTR, const char *));
247 static bfd_boolean stab_class_method_variant
248 PARAMS ((PTR, const char *, enum debug_visibility, bfd_boolean, bfd_boolean,
249 bfd_vma, bfd_boolean));
250 static bfd_boolean stab_class_static_method_variant
251 PARAMS ((PTR, const char *, enum debug_visibility, bfd_boolean,
252 bfd_boolean));
253 static bfd_boolean stab_class_end_method
254 PARAMS ((PTR));
255 static bfd_boolean stab_end_class_type
256 PARAMS ((PTR));
257 static bfd_boolean stab_typedef_type
258 PARAMS ((PTR, const char *));
259 static bfd_boolean stab_tag_type
260 PARAMS ((PTR, const char *, unsigned int, enum debug_type_kind));
261 static bfd_boolean stab_typdef
262 PARAMS ((PTR, const char *));
263 static bfd_boolean stab_tag
264 PARAMS ((PTR, const char *));
265 static bfd_boolean stab_int_constant
266 PARAMS ((PTR, const char *, bfd_vma));
267 static bfd_boolean stab_float_constant
268 PARAMS ((PTR, const char *, double));
269 static bfd_boolean stab_typed_constant
270 PARAMS ((PTR, const char *, bfd_vma));
271 static bfd_boolean stab_variable
272 PARAMS ((PTR, const char *, enum debug_var_kind, bfd_vma));
273 static bfd_boolean stab_start_function
274 PARAMS ((PTR, const char *, bfd_boolean));
275 static bfd_boolean stab_function_parameter
276 PARAMS ((PTR, const char *, enum debug_parm_kind, bfd_vma));
277 static bfd_boolean stab_start_block
278 PARAMS ((PTR, bfd_vma));
279 static bfd_boolean stab_end_block
280 PARAMS ((PTR, bfd_vma));
281 static bfd_boolean stab_end_function
282 PARAMS ((PTR));
283 static bfd_boolean stab_lineno
284 PARAMS ((PTR, const char *, unsigned long, bfd_vma));
286 static const struct debug_write_fns stab_fns =
288 stab_start_compilation_unit,
289 stab_start_source,
290 stab_empty_type,
291 stab_void_type,
292 stab_int_type,
293 stab_float_type,
294 stab_complex_type,
295 stab_bool_type,
296 stab_enum_type,
297 stab_pointer_type,
298 stab_function_type,
299 stab_reference_type,
300 stab_range_type,
301 stab_array_type,
302 stab_set_type,
303 stab_offset_type,
304 stab_method_type,
305 stab_const_type,
306 stab_volatile_type,
307 stab_start_struct_type,
308 stab_struct_field,
309 stab_end_struct_type,
310 stab_start_class_type,
311 stab_class_static_member,
312 stab_class_baseclass,
313 stab_class_start_method,
314 stab_class_method_variant,
315 stab_class_static_method_variant,
316 stab_class_end_method,
317 stab_end_class_type,
318 stab_typedef_type,
319 stab_tag_type,
320 stab_typdef,
321 stab_tag,
322 stab_int_constant,
323 stab_float_constant,
324 stab_typed_constant,
325 stab_variable,
326 stab_start_function,
327 stab_function_parameter,
328 stab_start_block,
329 stab_end_block,
330 stab_end_function,
331 stab_lineno
334 /* Routine to create an entry in a string hash table. */
336 static struct bfd_hash_entry *
337 string_hash_newfunc (entry, table, string)
338 struct bfd_hash_entry *entry;
339 struct bfd_hash_table *table;
340 const char *string;
342 struct string_hash_entry *ret = (struct string_hash_entry *) entry;
344 /* Allocate the structure if it has not already been allocated by a
345 subclass. */
346 if (ret == (struct string_hash_entry *) NULL)
347 ret = ((struct string_hash_entry *)
348 bfd_hash_allocate (table, sizeof (struct string_hash_entry)));
349 if (ret == (struct string_hash_entry *) NULL)
350 return NULL;
352 /* Call the allocation method of the superclass. */
353 ret = ((struct string_hash_entry *)
354 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
356 if (ret)
358 /* Initialize the local fields. */
359 ret->next = NULL;
360 ret->index = -1;
361 ret->size = 0;
364 return (struct bfd_hash_entry *) ret;
367 /* Look up an entry in a string hash table. */
369 #define string_hash_lookup(t, string, create, copy) \
370 ((struct string_hash_entry *) \
371 bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
373 /* Add a symbol to the stabs debugging information we are building. */
375 static bfd_boolean
376 stab_write_symbol (info, type, desc, value, string)
377 struct stab_write_handle *info;
378 int type;
379 int desc;
380 bfd_vma value;
381 const char *string;
383 bfd_size_type strx;
384 bfd_byte sym[STAB_SYMBOL_SIZE];
386 if (string == NULL)
387 strx = 0;
388 else
390 struct string_hash_entry *h;
392 h = string_hash_lookup (&info->strhash, string, TRUE, TRUE);
393 if (h == NULL)
395 non_fatal (_("string_hash_lookup failed: %s"),
396 bfd_errmsg (bfd_get_error ()));
397 return FALSE;
399 if (h->index != -1)
400 strx = h->index;
401 else
403 strx = info->strings_size;
404 h->index = strx;
405 if (info->last_string == NULL)
406 info->strings = h;
407 else
408 info->last_string->next = h;
409 info->last_string = h;
410 info->strings_size += strlen (string) + 1;
414 /* This presumes 32 bit values. */
415 bfd_put_32 (info->abfd, strx, sym);
416 bfd_put_8 (info->abfd, type, sym + 4);
417 bfd_put_8 (info->abfd, 0, sym + 5);
418 bfd_put_16 (info->abfd, desc, sym + 6);
419 bfd_put_32 (info->abfd, value, sym + 8);
421 if (info->symbols_size + STAB_SYMBOL_SIZE > info->symbols_alloc)
423 info->symbols_alloc *= 2;
424 info->symbols = (bfd_byte *) xrealloc (info->symbols,
425 info->symbols_alloc);
428 memcpy (info->symbols + info->symbols_size, sym, STAB_SYMBOL_SIZE);
430 info->symbols_size += STAB_SYMBOL_SIZE;
432 return TRUE;
435 /* Push a string on to the type stack. */
437 static bfd_boolean
438 stab_push_string (info, string, index, definition, size)
439 struct stab_write_handle *info;
440 const char *string;
441 long index;
442 bfd_boolean definition;
443 unsigned int size;
445 struct stab_type_stack *s;
447 s = (struct stab_type_stack *) xmalloc (sizeof *s);
448 s->string = xstrdup (string);
449 s->index = index;
450 s->definition = definition;
451 s->size = size;
453 s->fields = NULL;
454 s->baseclasses = NULL;
455 s->methods = NULL;
456 s->vtable = NULL;
458 s->next = info->type_stack;
459 info->type_stack = s;
461 return TRUE;
464 /* Push a type index which has already been defined. */
466 static bfd_boolean
467 stab_push_defined_type (info, index, size)
468 struct stab_write_handle *info;
469 long index;
470 unsigned int size;
472 char buf[20];
474 sprintf (buf, "%ld", index);
475 return stab_push_string (info, buf, index, FALSE, size);
478 /* Pop a type off the type stack. The caller is responsible for
479 freeing the string. */
481 static char *
482 stab_pop_type (info)
483 struct stab_write_handle *info;
485 struct stab_type_stack *s;
486 char *ret;
488 s = info->type_stack;
489 assert (s != NULL);
491 info->type_stack = s->next;
493 ret = s->string;
495 free (s);
497 return ret;
500 /* The general routine to write out stabs in sections debugging
501 information. This accumulates the stabs symbols and the strings in
502 two obstacks. We can't easily write out the information as we go
503 along, because we need to know the section sizes before we can
504 write out the section contents. ABFD is the BFD and DHANDLE is the
505 handle for the debugging information. This sets *PSYMS to point to
506 the symbols, *PSYMSIZE the size of the symbols, *PSTRINGS to the
507 strings, and *PSTRINGSIZE to the size of the strings. */
509 bfd_boolean
510 write_stabs_in_sections_debugging_info (abfd, dhandle, psyms, psymsize,
511 pstrings, pstringsize)
512 bfd *abfd;
513 PTR dhandle;
514 bfd_byte **psyms;
515 bfd_size_type *psymsize;
516 bfd_byte **pstrings;
517 bfd_size_type *pstringsize;
519 struct stab_write_handle info;
520 struct string_hash_entry *h;
521 bfd_byte *p;
523 info.abfd = abfd;
525 info.symbols_size = 0;
526 info.symbols_alloc = 500;
527 info.symbols = (bfd_byte *) xmalloc (info.symbols_alloc);
529 info.strings = NULL;
530 info.last_string = NULL;
531 /* Reserve 1 byte for a null byte. */
532 info.strings_size = 1;
534 if (! bfd_hash_table_init (&info.strhash.table, string_hash_newfunc)
535 || ! bfd_hash_table_init (&info.typedef_hash.table, string_hash_newfunc))
537 non_fatal ("bfd_hash_table_init_failed: %s",
538 bfd_errmsg (bfd_get_error ()));
539 return FALSE;
542 info.type_stack = NULL;
543 info.type_index = 1;
544 memset (&info.type_cache, 0, sizeof info.type_cache);
545 info.so_offset = -1;
546 info.fun_offset = -1;
547 info.last_text_address = 0;
548 info.nesting = 0;
549 info.fnaddr = 0;
550 info.pending_lbrac = (bfd_vma) -1;
552 /* The initial symbol holds the string size. */
553 if (! stab_write_symbol (&info, 0, 0, 0, (const char *) NULL))
554 return FALSE;
556 /* Output an initial N_SO symbol. */
557 info.so_offset = info.symbols_size;
558 if (! stab_write_symbol (&info, N_SO, 0, 0, bfd_get_filename (abfd)))
559 return FALSE;
561 if (! debug_write (dhandle, &stab_fns, (PTR) &info))
562 return FALSE;
564 assert (info.pending_lbrac == (bfd_vma) -1);
566 /* Output a trailing N_SO. */
567 if (! stab_write_symbol (&info, N_SO, 0, info.last_text_address,
568 (const char *) NULL))
569 return FALSE;
571 /* Put the string size in the initial symbol. */
572 bfd_put_32 (abfd, info.strings_size, info.symbols + 8);
574 *psyms = info.symbols;
575 *psymsize = info.symbols_size;
577 *pstringsize = info.strings_size;
578 *pstrings = (bfd_byte *) xmalloc (info.strings_size);
580 p = *pstrings;
581 *p++ = '\0';
582 for (h = info.strings; h != NULL; h = h->next)
584 strcpy ((char *) p, h->root.string);
585 p += strlen ((char *) p) + 1;
588 return TRUE;
591 /* Start writing out information for a compilation unit. */
593 static bfd_boolean
594 stab_start_compilation_unit (p, filename)
595 PTR p;
596 const char *filename;
598 struct stab_write_handle *info = (struct stab_write_handle *) p;
600 /* We would normally output an N_SO symbol here. However, that
601 would force us to reset all of our type information. I think we
602 will be better off just outputting an N_SOL symbol, and not
603 worrying about splitting information between files. */
605 info->lineno_filename = filename;
607 return stab_write_symbol (info, N_SOL, 0, 0, filename);
610 /* Start writing out information for a particular source file. */
612 static bfd_boolean
613 stab_start_source (p, filename)
614 PTR p;
615 const char *filename;
617 struct stab_write_handle *info = (struct stab_write_handle *) p;
619 /* FIXME: The symbol's value is supposed to be the text section
620 address. However, we would have to fill it in later, and gdb
621 doesn't care, so we don't bother with it. */
623 info->lineno_filename = filename;
625 return stab_write_symbol (info, N_SOL, 0, 0, filename);
628 /* Push an empty type. This shouldn't normally happen. We just use a
629 void type. */
631 static bfd_boolean
632 stab_empty_type (p)
633 PTR p;
635 struct stab_write_handle *info = (struct stab_write_handle *) p;
637 /* We don't call stab_void_type if the type is not yet defined,
638 because that might screw up the typedef. */
640 if (info->type_cache.void_type != 0)
641 return stab_push_defined_type (info, info->type_cache.void_type, 0);
642 else
644 long index;
645 char buf[40];
647 index = info->type_index;
648 ++info->type_index;
650 sprintf (buf, "%ld=%ld", index, index);
652 return stab_push_string (info, buf, index, FALSE, 0);
656 /* Push a void type. */
658 static bfd_boolean
659 stab_void_type (p)
660 PTR p;
662 struct stab_write_handle *info = (struct stab_write_handle *) p;
664 if (info->type_cache.void_type != 0)
665 return stab_push_defined_type (info, info->type_cache.void_type, 0);
666 else
668 long index;
669 char buf[40];
671 index = info->type_index;
672 ++info->type_index;
674 info->type_cache.void_type = index;
676 sprintf (buf, "%ld=%ld", index, index);
678 return stab_push_string (info, buf, index, TRUE, 0);
682 /* Push an integer type. */
684 static bfd_boolean
685 stab_int_type (p, size, unsignedp)
686 PTR p;
687 unsigned int size;
688 bfd_boolean unsignedp;
690 struct stab_write_handle *info = (struct stab_write_handle *) p;
691 long *cache;
693 if (size <= 0 || (size > sizeof (long) && size != 8))
695 non_fatal (_("stab_int_type: bad size %u"), size);
696 return FALSE;
699 if (unsignedp)
700 cache = info->type_cache.signed_integer_types;
701 else
702 cache = info->type_cache.unsigned_integer_types;
704 if (cache[size - 1] != 0)
705 return stab_push_defined_type (info, cache[size - 1], size);
706 else
708 long index;
709 char buf[100];
711 index = info->type_index;
712 ++info->type_index;
714 cache[size - 1] = index;
716 sprintf (buf, "%ld=r%ld;", index, index);
717 if (unsignedp)
719 strcat (buf, "0;");
720 if (size < sizeof (long))
721 sprintf (buf + strlen (buf), "%ld;", ((long) 1 << (size * 8)) - 1);
722 else if (size == sizeof (long))
723 strcat (buf, "-1;");
724 else if (size == 8)
725 strcat (buf, "01777777777777777777777;");
726 else
727 abort ();
729 else
731 if (size <= sizeof (long))
732 sprintf (buf + strlen (buf), "%ld;%ld;",
733 (long) - ((unsigned long) 1 << (size * 8 - 1)),
734 (long) (((unsigned long) 1 << (size * 8 - 1)) - 1));
735 else if (size == 8)
736 strcat (buf, "01000000000000000000000;0777777777777777777777;");
737 else
738 abort ();
741 return stab_push_string (info, buf, index, TRUE, size);
745 /* Push a floating point type. */
747 static bfd_boolean
748 stab_float_type (p, size)
749 PTR p;
750 unsigned int size;
752 struct stab_write_handle *info = (struct stab_write_handle *) p;
754 if (size > 0
755 && size - 1 < (sizeof info->type_cache.float_types
756 / sizeof info->type_cache.float_types[0])
757 && info->type_cache.float_types[size - 1] != 0)
758 return stab_push_defined_type (info,
759 info->type_cache.float_types[size - 1],
760 size);
761 else
763 long index;
764 char *int_type;
765 char buf[50];
767 /* Floats are defined as a subrange of int. */
768 if (! stab_int_type (info, 4, FALSE))
769 return FALSE;
770 int_type = stab_pop_type (info);
772 index = info->type_index;
773 ++info->type_index;
775 if (size > 0
776 && size - 1 < (sizeof info->type_cache.float_types
777 / sizeof info->type_cache.float_types[0]))
778 info->type_cache.float_types[size - 1] = index;
780 sprintf (buf, "%ld=r%s;%u;0;", index, int_type, size);
782 free (int_type);
784 return stab_push_string (info, buf, index, TRUE, size);
788 /* Push a complex type. */
790 static bfd_boolean
791 stab_complex_type (p, size)
792 PTR p;
793 unsigned int size;
795 struct stab_write_handle *info = (struct stab_write_handle *) p;
796 char buf[50];
797 long index;
799 index = info->type_index;
800 ++info->type_index;
802 sprintf (buf, "%ld=r%ld;%u;0;", index, index, size);
804 return stab_push_string (info, buf, index, TRUE, size * 2);
807 /* Push a bfd_boolean type. We use an XCOFF predefined type, since gdb
808 always recognizes them. */
810 static bfd_boolean
811 stab_bool_type (p, size)
812 PTR p;
813 unsigned int size;
815 struct stab_write_handle *info = (struct stab_write_handle *) p;
816 long index;
818 switch (size)
820 case 1:
821 index = -21;
822 break;
824 case 2:
825 index = -22;
826 break;
828 default:
829 case 4:
830 index = -16;
831 break;
833 case 8:
834 index = -33;
835 break;
838 return stab_push_defined_type (info, index, size);
841 /* Push an enum type. */
843 static bfd_boolean
844 stab_enum_type (p, tag, names, vals)
845 PTR p;
846 const char *tag;
847 const char **names;
848 bfd_signed_vma *vals;
850 struct stab_write_handle *info = (struct stab_write_handle *) p;
851 size_t len;
852 const char **pn;
853 char *buf;
854 long index = 0;
855 bfd_signed_vma *pv;
857 if (names == NULL)
859 assert (tag != NULL);
861 buf = (char *) xmalloc (10 + strlen (tag));
862 sprintf (buf, "xe%s:", tag);
863 /* FIXME: The size is just a guess. */
864 if (! stab_push_string (info, buf, 0, FALSE, 4))
865 return FALSE;
866 free (buf);
867 return TRUE;
870 len = 10;
871 if (tag != NULL)
872 len += strlen (tag);
873 for (pn = names; *pn != NULL; pn++)
874 len += strlen (*pn) + 20;
876 buf = (char *) xmalloc (len);
878 if (tag == NULL)
879 strcpy (buf, "e");
880 else
882 index = info->type_index;
883 ++info->type_index;
884 sprintf (buf, "%s:T%ld=e", tag, index);
887 for (pn = names, pv = vals; *pn != NULL; pn++, pv++)
888 sprintf (buf + strlen (buf), "%s:%ld,", *pn, (long) *pv);
889 strcat (buf, ";");
891 if (tag == NULL)
893 /* FIXME: The size is just a guess. */
894 if (! stab_push_string (info, buf, 0, FALSE, 4))
895 return FALSE;
897 else
899 /* FIXME: The size is just a guess. */
900 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf)
901 || ! stab_push_defined_type (info, index, 4))
902 return FALSE;
905 free (buf);
907 return TRUE;
910 /* Push a modification of the top type on the stack. Cache the
911 results in CACHE and CACHE_ALLOC. */
913 static bfd_boolean
914 stab_modify_type (info, mod, size, cache, cache_alloc)
915 struct stab_write_handle *info;
916 int mod;
917 unsigned int size;
918 long **cache;
919 size_t *cache_alloc;
921 long targindex;
922 long index;
923 char *s, *buf;
925 assert (info->type_stack != NULL);
926 targindex = info->type_stack->index;
928 if (targindex <= 0
929 || cache == NULL)
931 bfd_boolean definition;
933 /* Either the target type has no index, or we aren't caching
934 this modifier. Either way we have no way of recording the
935 new type, so we don't bother to define one. */
936 definition = info->type_stack->definition;
937 s = stab_pop_type (info);
938 buf = (char *) xmalloc (strlen (s) + 2);
939 sprintf (buf, "%c%s", mod, s);
940 free (s);
941 if (! stab_push_string (info, buf, 0, definition, size))
942 return FALSE;
943 free (buf);
945 else
947 if ((size_t) targindex >= *cache_alloc)
949 size_t alloc;
951 alloc = *cache_alloc;
952 if (alloc == 0)
953 alloc = 10;
954 while ((size_t) targindex >= alloc)
955 alloc *= 2;
956 *cache = (long *) xrealloc (*cache, alloc * sizeof (long));
957 memset (*cache + *cache_alloc, 0,
958 (alloc - *cache_alloc) * sizeof (long));
959 *cache_alloc = alloc;
962 index = (*cache)[targindex];
963 if (index != 0 && ! info->type_stack->definition)
965 /* We have already defined a modification of this type, and
966 the entry on the type stack is not a definition, so we
967 can safely discard it (we may have a definition on the
968 stack, even if we already defined a modification, if it
969 is a struct which we did not define at the time it was
970 referenced). */
971 free (stab_pop_type (info));
972 if (! stab_push_defined_type (info, index, size))
973 return FALSE;
975 else
977 index = info->type_index;
978 ++info->type_index;
980 s = stab_pop_type (info);
981 buf = (char *) xmalloc (strlen (s) + 20);
982 sprintf (buf, "%ld=%c%s", index, mod, s);
983 free (s);
985 (*cache)[targindex] = index;
987 if (! stab_push_string (info, buf, index, TRUE, size))
988 return FALSE;
990 free (buf);
994 return TRUE;
997 /* Push a pointer type. */
999 static bfd_boolean
1000 stab_pointer_type (p)
1001 PTR p;
1003 struct stab_write_handle *info = (struct stab_write_handle *) p;
1005 /* FIXME: The size should depend upon the architecture. */
1006 return stab_modify_type (info, '*', 4, &info->type_cache.pointer_types,
1007 &info->type_cache.pointer_types_alloc);
1010 /* Push a function type. */
1012 static bfd_boolean
1013 stab_function_type (p, argcount, varargs)
1014 PTR p;
1015 int argcount;
1016 bfd_boolean varargs ATTRIBUTE_UNUSED;
1018 struct stab_write_handle *info = (struct stab_write_handle *) p;
1019 int i;
1021 /* We have no way to represent the argument types, so we just
1022 discard them. However, if they define new types, we must output
1023 them. We do this by producing empty typedefs. */
1024 for (i = 0; i < argcount; i++)
1026 if (! info->type_stack->definition)
1027 free (stab_pop_type (info));
1028 else
1030 char *s, *buf;
1032 s = stab_pop_type (info);
1034 buf = (char *) xmalloc (strlen (s) + 3);
1035 sprintf (buf, ":t%s", s);
1036 free (s);
1038 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
1039 return FALSE;
1041 free (buf);
1045 return stab_modify_type (info, 'f', 0, &info->type_cache.function_types,
1046 &info->type_cache.function_types_alloc);
1049 /* Push a reference type. */
1051 static bfd_boolean
1052 stab_reference_type (p)
1053 PTR p;
1055 struct stab_write_handle *info = (struct stab_write_handle *) p;
1057 /* FIXME: The size should depend upon the architecture. */
1058 return stab_modify_type (info, '&', 4, &info->type_cache.reference_types,
1059 &info->type_cache.reference_types_alloc);
1062 /* Push a range type. */
1064 static bfd_boolean
1065 stab_range_type (p, low, high)
1066 PTR p;
1067 bfd_signed_vma low;
1068 bfd_signed_vma high;
1070 struct stab_write_handle *info = (struct stab_write_handle *) p;
1071 bfd_boolean definition;
1072 unsigned int size;
1073 char *s, *buf;
1075 definition = info->type_stack->definition;
1076 size = info->type_stack->size;
1078 s = stab_pop_type (info);
1079 buf = (char *) xmalloc (strlen (s) + 100);
1080 sprintf (buf, "r%s;%ld;%ld;", s, (long) low, (long) high);
1081 free (s);
1083 if (! stab_push_string (info, buf, 0, definition, size))
1084 return FALSE;
1086 free (buf);
1088 return TRUE;
1091 /* Push an array type. */
1093 static bfd_boolean
1094 stab_array_type (p, low, high, stringp)
1095 PTR p;
1096 bfd_signed_vma low;
1097 bfd_signed_vma high;
1098 bfd_boolean stringp;
1100 struct stab_write_handle *info = (struct stab_write_handle *) p;
1101 bfd_boolean definition;
1102 unsigned int element_size;
1103 char *range, *element, *buf;
1104 long index;
1105 unsigned int size;
1107 definition = info->type_stack->definition;
1108 range = stab_pop_type (info);
1110 definition = definition || info->type_stack->definition;
1111 element_size = info->type_stack->size;
1112 element = stab_pop_type (info);
1114 buf = (char *) xmalloc (strlen (range) + strlen (element) + 100);
1116 if (! stringp)
1118 index = 0;
1119 *buf = '\0';
1121 else
1123 /* We need to define a type in order to include the string
1124 attribute. */
1125 index = info->type_index;
1126 ++info->type_index;
1127 definition = TRUE;
1128 sprintf (buf, "%ld=@S;", index);
1131 sprintf (buf + strlen (buf), "ar%s;%ld;%ld;%s",
1132 range, (long) low, (long) high, element);
1133 free (range);
1134 free (element);
1136 if (high < low)
1137 size = 0;
1138 else
1139 size = element_size * ((high - low) + 1);
1140 if (! stab_push_string (info, buf, index, definition, size))
1141 return FALSE;
1143 free (buf);
1145 return TRUE;
1148 /* Push a set type. */
1150 static bfd_boolean
1151 stab_set_type (p, bitstringp)
1152 PTR p;
1153 bfd_boolean bitstringp;
1155 struct stab_write_handle *info = (struct stab_write_handle *) p;
1156 bfd_boolean definition;
1157 char *s, *buf;
1158 long index;
1160 definition = info->type_stack->definition;
1162 s = stab_pop_type (info);
1163 buf = (char *) xmalloc (strlen (s) + 30);
1165 if (! bitstringp)
1167 *buf = '\0';
1168 index = 0;
1170 else
1172 /* We need to define a type in order to include the string
1173 attribute. */
1174 index = info->type_index;
1175 ++info->type_index;
1176 definition = TRUE;
1177 sprintf (buf, "%ld=@S;", index);
1180 sprintf (buf + strlen (buf), "S%s", s);
1181 free (s);
1183 if (! stab_push_string (info, buf, index, definition, 0))
1184 return FALSE;
1186 free (buf);
1188 return TRUE;
1191 /* Push an offset type. */
1193 static bfd_boolean
1194 stab_offset_type (p)
1195 PTR p;
1197 struct stab_write_handle *info = (struct stab_write_handle *) p;
1198 bfd_boolean definition;
1199 char *target, *base, *buf;
1201 definition = info->type_stack->definition;
1202 target = stab_pop_type (info);
1204 definition = definition || info->type_stack->definition;
1205 base = stab_pop_type (info);
1207 buf = (char *) xmalloc (strlen (target) + strlen (base) + 3);
1208 sprintf (buf, "@%s,%s", base, target);
1209 free (base);
1210 free (target);
1212 if (! stab_push_string (info, buf, 0, definition, 0))
1213 return FALSE;
1215 free (buf);
1217 return TRUE;
1220 /* Push a method type. */
1222 static bfd_boolean
1223 stab_method_type (p, domainp, argcount, varargs)
1224 PTR p;
1225 bfd_boolean domainp;
1226 int argcount;
1227 bfd_boolean varargs;
1229 struct stab_write_handle *info = (struct stab_write_handle *) p;
1230 bfd_boolean definition;
1231 char *domain, *return_type, *buf;
1232 char **args;
1233 int i;
1234 size_t len;
1236 /* We don't bother with stub method types, because that would
1237 require a mangler for C++ argument types. This will waste space
1238 in the debugging output. */
1240 /* We need a domain. I'm not sure DOMAINP can ever be false,
1241 anyhow. */
1242 if (! domainp)
1244 if (! stab_empty_type (p))
1245 return FALSE;
1248 definition = info->type_stack->definition;
1249 domain = stab_pop_type (info);
1251 /* A non-varargs function is indicated by making the last parameter
1252 type be void. */
1254 if (argcount < 0)
1256 args = NULL;
1257 argcount = 0;
1259 else if (argcount == 0)
1261 if (varargs)
1262 args = NULL;
1263 else
1265 args = (char **) xmalloc (1 * sizeof (*args));
1266 if (! stab_empty_type (p))
1267 return FALSE;
1268 definition = definition || info->type_stack->definition;
1269 args[0] = stab_pop_type (info);
1270 argcount = 1;
1273 else
1275 args = (char **) xmalloc ((argcount + 1) * sizeof (*args));
1276 for (i = argcount - 1; i >= 0; i--)
1278 definition = definition || info->type_stack->definition;
1279 args[i] = stab_pop_type (info);
1281 if (! varargs)
1283 if (! stab_empty_type (p))
1284 return FALSE;
1285 definition = definition || info->type_stack->definition;
1286 args[argcount] = stab_pop_type (info);
1287 ++argcount;
1291 definition = definition || info->type_stack->definition;
1292 return_type = stab_pop_type (info);
1294 len = strlen (domain) + strlen (return_type) + 10;
1295 for (i = 0; i < argcount; i++)
1296 len += strlen (args[i]);
1298 buf = (char *) xmalloc (len);
1300 sprintf (buf, "#%s,%s", domain, return_type);
1301 free (domain);
1302 free (return_type);
1303 for (i = 0; i < argcount; i++)
1305 strcat (buf, ",");
1306 strcat (buf, args[i]);
1307 free (args[i]);
1309 strcat (buf, ";");
1311 if (args != NULL)
1312 free (args);
1314 if (! stab_push_string (info, buf, 0, definition, 0))
1315 return FALSE;
1317 free (buf);
1319 return TRUE;
1322 /* Push a const version of a type. */
1324 static bfd_boolean
1325 stab_const_type (p)
1326 PTR p;
1328 struct stab_write_handle *info = (struct stab_write_handle *) p;
1330 return stab_modify_type (info, 'k', info->type_stack->size,
1331 (long **) NULL, (size_t *) NULL);
1334 /* Push a volatile version of a type. */
1336 static bfd_boolean
1337 stab_volatile_type (p)
1338 PTR p;
1340 struct stab_write_handle *info = (struct stab_write_handle *) p;
1342 return stab_modify_type (info, 'B', info->type_stack->size,
1343 (long **) NULL, (size_t *) NULL);
1346 /* Get the type index to use for a struct/union/class ID. This should
1347 return -1 if it fails. */
1349 static long
1350 stab_get_struct_index (info, tag, id, kind, psize)
1351 struct stab_write_handle *info;
1352 const char *tag;
1353 unsigned int id;
1354 enum debug_type_kind kind;
1355 unsigned int *psize;
1357 if (id >= info->type_cache.struct_types_alloc)
1359 size_t alloc;
1361 alloc = info->type_cache.struct_types_alloc;
1362 if (alloc == 0)
1363 alloc = 10;
1364 while (id >= alloc)
1365 alloc *= 2;
1366 info->type_cache.struct_types =
1367 (struct stab_tag *) xrealloc (info->type_cache.struct_types,
1368 alloc * sizeof (struct stab_tag));
1369 memset ((info->type_cache.struct_types
1370 + info->type_cache.struct_types_alloc),
1372 ((alloc - info->type_cache.struct_types_alloc)
1373 * sizeof (struct stab_tag)));
1374 info->type_cache.struct_types_alloc = alloc;
1377 if (info->type_cache.struct_types[id].index == 0)
1379 info->type_cache.struct_types[id].index = info->type_index;
1380 ++info->type_index;
1381 info->type_cache.struct_types[id].tag = tag;
1382 info->type_cache.struct_types[id].kind = kind;
1385 if (kind == DEBUG_KIND_ILLEGAL)
1387 /* This is a definition of the struct. */
1388 info->type_cache.struct_types[id].kind = kind;
1389 info->type_cache.struct_types[id].size = *psize;
1391 else
1392 *psize = info->type_cache.struct_types[id].size;
1394 return info->type_cache.struct_types[id].index;
1397 /* Start outputting a struct. We ignore the tag, and handle it in
1398 stab_tag. */
1400 static bfd_boolean
1401 stab_start_struct_type (p, tag, id, structp, size)
1402 PTR p;
1403 const char *tag;
1404 unsigned int id;
1405 bfd_boolean structp;
1406 unsigned int size;
1408 struct stab_write_handle *info = (struct stab_write_handle *) p;
1409 long index;
1410 bfd_boolean definition;
1411 char *buf;
1413 buf = (char *) xmalloc (40);
1415 if (id == 0)
1417 index = 0;
1418 *buf = '\0';
1419 definition = FALSE;
1421 else
1423 index = stab_get_struct_index (info, tag, id, DEBUG_KIND_ILLEGAL,
1424 &size);
1425 if (index < 0)
1426 return FALSE;
1427 sprintf (buf, "%ld=", index);
1428 definition = TRUE;
1431 sprintf (buf + strlen (buf), "%c%u",
1432 structp ? 's' : 'u',
1433 size);
1435 if (! stab_push_string (info, buf, index, definition, size))
1436 return FALSE;
1438 info->type_stack->fields = (char *) xmalloc (1);
1439 info->type_stack->fields[0] = '\0';
1441 return TRUE;
1444 /* Add a field to a struct. */
1446 static bfd_boolean
1447 stab_struct_field (p, name, bitpos, bitsize, visibility)
1448 PTR p;
1449 const char *name;
1450 bfd_vma bitpos;
1451 bfd_vma bitsize;
1452 enum debug_visibility visibility;
1454 struct stab_write_handle *info = (struct stab_write_handle *) p;
1455 bfd_boolean definition;
1456 unsigned int size;
1457 char *s, *n;
1458 const char *vis;
1460 definition = info->type_stack->definition;
1461 size = info->type_stack->size;
1462 s = stab_pop_type (info);
1464 /* Add this field to the end of the current struct fields, which is
1465 currently on the top of the stack. */
1467 assert (info->type_stack->fields != NULL);
1468 n = (char *) xmalloc (strlen (info->type_stack->fields)
1469 + strlen (name)
1470 + strlen (s)
1471 + 50);
1473 switch (visibility)
1475 default:
1476 abort ();
1478 case DEBUG_VISIBILITY_PUBLIC:
1479 vis = "";
1480 break;
1482 case DEBUG_VISIBILITY_PRIVATE:
1483 vis = "/0";
1484 break;
1486 case DEBUG_VISIBILITY_PROTECTED:
1487 vis = "/1";
1488 break;
1491 if (bitsize == 0)
1493 bitsize = size * 8;
1494 if (bitsize == 0)
1495 non_fatal (_("%s: warning: unknown size for field `%s' in struct"),
1496 bfd_get_filename (info->abfd), name);
1499 sprintf (n, "%s%s:%s%s,%ld,%ld;", info->type_stack->fields, name, vis, s,
1500 (long) bitpos, (long) bitsize);
1502 free (info->type_stack->fields);
1503 info->type_stack->fields = n;
1505 if (definition)
1506 info->type_stack->definition = TRUE;
1508 return TRUE;
1511 /* Finish up a struct. */
1513 static bfd_boolean
1514 stab_end_struct_type (p)
1515 PTR p;
1517 struct stab_write_handle *info = (struct stab_write_handle *) p;
1518 bfd_boolean definition;
1519 long index;
1520 unsigned int size;
1521 char *fields, *first, *buf;
1523 assert (info->type_stack != NULL && info->type_stack->fields != NULL);
1525 definition = info->type_stack->definition;
1526 index = info->type_stack->index;
1527 size = info->type_stack->size;
1528 fields = info->type_stack->fields;
1529 first = stab_pop_type (info);
1531 buf = (char *) xmalloc (strlen (first) + strlen (fields) + 2);
1532 sprintf (buf, "%s%s;", first, fields);
1533 free (first);
1534 free (fields);
1536 if (! stab_push_string (info, buf, index, definition, size))
1537 return FALSE;
1539 free (buf);
1541 return TRUE;
1544 /* Start outputting a class. */
1546 static bfd_boolean
1547 stab_start_class_type (p, tag, id, structp, size, vptr, ownvptr)
1548 PTR p;
1549 const char *tag;
1550 unsigned int id;
1551 bfd_boolean structp;
1552 unsigned int size;
1553 bfd_boolean vptr;
1554 bfd_boolean ownvptr;
1556 struct stab_write_handle *info = (struct stab_write_handle *) p;
1557 bfd_boolean definition;
1558 char *vstring;
1560 if (! vptr || ownvptr)
1562 definition = FALSE;
1563 vstring = NULL;
1565 else
1567 definition = info->type_stack->definition;
1568 vstring = stab_pop_type (info);
1571 if (! stab_start_struct_type (p, tag, id, structp, size))
1572 return FALSE;
1574 if (vptr)
1576 char *vtable;
1578 if (ownvptr)
1580 assert (info->type_stack->index > 0);
1581 vtable = (char *) xmalloc (20);
1582 sprintf (vtable, "~%%%ld", info->type_stack->index);
1584 else
1586 vtable = (char *) xmalloc (strlen (vstring) + 3);
1587 sprintf (vtable, "~%%%s", vstring);
1588 free (vstring);
1591 info->type_stack->vtable = vtable;
1594 if (definition)
1595 info->type_stack->definition = TRUE;
1597 return TRUE;
1600 /* Add a static member to the class on the type stack. */
1602 static bfd_boolean
1603 stab_class_static_member (p, name, physname, visibility)
1604 PTR p;
1605 const char *name;
1606 const char *physname;
1607 enum debug_visibility visibility;
1609 struct stab_write_handle *info = (struct stab_write_handle *) p;
1610 bfd_boolean definition;
1611 char *s, *n;
1612 const char *vis;
1614 definition = info->type_stack->definition;
1615 s = stab_pop_type (info);
1617 /* Add this field to the end of the current struct fields, which is
1618 currently on the top of the stack. */
1620 assert (info->type_stack->fields != NULL);
1621 n = (char *) xmalloc (strlen (info->type_stack->fields)
1622 + strlen (name)
1623 + strlen (s)
1624 + strlen (physname)
1625 + 10);
1627 switch (visibility)
1629 default:
1630 abort ();
1632 case DEBUG_VISIBILITY_PUBLIC:
1633 vis = "";
1634 break;
1636 case DEBUG_VISIBILITY_PRIVATE:
1637 vis = "/0";
1638 break;
1640 case DEBUG_VISIBILITY_PROTECTED:
1641 vis = "/1";
1642 break;
1645 sprintf (n, "%s%s:%s%s:%s;", info->type_stack->fields, name, vis, s,
1646 physname);
1648 free (info->type_stack->fields);
1649 info->type_stack->fields = n;
1651 if (definition)
1652 info->type_stack->definition = TRUE;
1654 return TRUE;
1657 /* Add a base class to the class on the type stack. */
1659 static bfd_boolean
1660 stab_class_baseclass (p, bitpos, virtual, visibility)
1661 PTR p;
1662 bfd_vma bitpos;
1663 bfd_boolean virtual;
1664 enum debug_visibility visibility;
1666 struct stab_write_handle *info = (struct stab_write_handle *) p;
1667 bfd_boolean definition;
1668 char *s;
1669 char *buf;
1670 unsigned int c;
1671 char **baseclasses;
1673 definition = info->type_stack->definition;
1674 s = stab_pop_type (info);
1676 /* Build the base class specifier. */
1678 buf = (char *) xmalloc (strlen (s) + 25);
1679 buf[0] = virtual ? '1' : '0';
1680 switch (visibility)
1682 default:
1683 abort ();
1685 case DEBUG_VISIBILITY_PRIVATE:
1686 buf[1] = '0';
1687 break;
1689 case DEBUG_VISIBILITY_PROTECTED:
1690 buf[1] = '1';
1691 break;
1693 case DEBUG_VISIBILITY_PUBLIC:
1694 buf[1] = '2';
1695 break;
1698 sprintf (buf + 2, "%ld,%s;", (long) bitpos, s);
1699 free (s);
1701 /* Add the new baseclass to the existing ones. */
1703 assert (info->type_stack != NULL && info->type_stack->fields != NULL);
1705 if (info->type_stack->baseclasses == NULL)
1706 c = 0;
1707 else
1709 c = 0;
1710 while (info->type_stack->baseclasses[c] != NULL)
1711 ++c;
1714 baseclasses = (char **) xrealloc (info->type_stack->baseclasses,
1715 (c + 2) * sizeof (*baseclasses));
1716 baseclasses[c] = buf;
1717 baseclasses[c + 1] = NULL;
1719 info->type_stack->baseclasses = baseclasses;
1721 if (definition)
1722 info->type_stack->definition = TRUE;
1724 return TRUE;
1727 /* Start adding a method to the class on the type stack. */
1729 static bfd_boolean
1730 stab_class_start_method (p, name)
1731 PTR p;
1732 const char *name;
1734 struct stab_write_handle *info = (struct stab_write_handle *) p;
1735 char *m;
1737 assert (info->type_stack != NULL && info->type_stack->fields != NULL);
1739 if (info->type_stack->methods == NULL)
1741 m = (char *) xmalloc (strlen (name) + 3);
1742 *m = '\0';
1744 else
1746 m = (char *) xrealloc (info->type_stack->methods,
1747 (strlen (info->type_stack->methods)
1748 + strlen (name)
1749 + 4));
1752 sprintf (m + strlen (m), "%s::", name);
1754 info->type_stack->methods = m;
1756 return TRUE;
1759 /* Add a variant, either static or not, to the current method. */
1761 static bfd_boolean
1762 stab_class_method_var (info, physname, visibility, staticp, constp, volatilep,
1763 voffset, contextp)
1764 struct stab_write_handle *info;
1765 const char *physname;
1766 enum debug_visibility visibility;
1767 bfd_boolean staticp;
1768 bfd_boolean constp;
1769 bfd_boolean volatilep;
1770 bfd_vma voffset;
1771 bfd_boolean contextp;
1773 bfd_boolean definition;
1774 char *type;
1775 char *context = NULL;
1776 char visc, qualc, typec;
1778 definition = info->type_stack->definition;
1779 type = stab_pop_type (info);
1781 if (contextp)
1783 definition = definition || info->type_stack->definition;
1784 context = stab_pop_type (info);
1787 assert (info->type_stack != NULL && info->type_stack->methods != NULL);
1789 switch (visibility)
1791 default:
1792 abort ();
1794 case DEBUG_VISIBILITY_PRIVATE:
1795 visc = '0';
1796 break;
1798 case DEBUG_VISIBILITY_PROTECTED:
1799 visc = '1';
1800 break;
1802 case DEBUG_VISIBILITY_PUBLIC:
1803 visc = '2';
1804 break;
1807 if (constp)
1809 if (volatilep)
1810 qualc = 'D';
1811 else
1812 qualc = 'B';
1814 else
1816 if (volatilep)
1817 qualc = 'C';
1818 else
1819 qualc = 'A';
1822 if (staticp)
1823 typec = '?';
1824 else if (! contextp)
1825 typec = '.';
1826 else
1827 typec = '*';
1829 info->type_stack->methods =
1830 (char *) xrealloc (info->type_stack->methods,
1831 (strlen (info->type_stack->methods)
1832 + strlen (type)
1833 + strlen (physname)
1834 + (contextp ? strlen (context) : 0)
1835 + 40));
1837 sprintf (info->type_stack->methods + strlen (info->type_stack->methods),
1838 "%s:%s;%c%c%c", type, physname, visc, qualc, typec);
1839 free (type);
1841 if (contextp)
1843 sprintf (info->type_stack->methods + strlen (info->type_stack->methods),
1844 "%ld;%s;", (long) voffset, context);
1845 free (context);
1848 if (definition)
1849 info->type_stack->definition = TRUE;
1851 return TRUE;
1854 /* Add a variant to the current method. */
1856 static bfd_boolean
1857 stab_class_method_variant (p, physname, visibility, constp, volatilep,
1858 voffset, contextp)
1859 PTR p;
1860 const char *physname;
1861 enum debug_visibility visibility;
1862 bfd_boolean constp;
1863 bfd_boolean volatilep;
1864 bfd_vma voffset;
1865 bfd_boolean contextp;
1867 struct stab_write_handle *info = (struct stab_write_handle *) p;
1869 return stab_class_method_var (info, physname, visibility, FALSE, constp,
1870 volatilep, voffset, contextp);
1873 /* Add a static variant to the current method. */
1875 static bfd_boolean
1876 stab_class_static_method_variant (p, physname, visibility, constp, volatilep)
1877 PTR p;
1878 const char *physname;
1879 enum debug_visibility visibility;
1880 bfd_boolean constp;
1881 bfd_boolean volatilep;
1883 struct stab_write_handle *info = (struct stab_write_handle *) p;
1885 return stab_class_method_var (info, physname, visibility, TRUE, constp,
1886 volatilep, 0, FALSE);
1889 /* Finish up a method. */
1891 static bfd_boolean
1892 stab_class_end_method (p)
1893 PTR p;
1895 struct stab_write_handle *info = (struct stab_write_handle *) p;
1897 assert (info->type_stack != NULL && info->type_stack->methods != NULL);
1899 /* We allocated enough room on info->type_stack->methods to add the
1900 trailing semicolon. */
1901 strcat (info->type_stack->methods, ";");
1903 return TRUE;
1906 /* Finish up a class. */
1908 static bfd_boolean
1909 stab_end_class_type (p)
1910 PTR p;
1912 struct stab_write_handle *info = (struct stab_write_handle *) p;
1913 size_t len;
1914 unsigned int i = 0;
1915 char *buf;
1917 assert (info->type_stack != NULL && info->type_stack->fields != NULL);
1919 /* Work out the size we need to allocate for the class definition. */
1921 len = (strlen (info->type_stack->string)
1922 + strlen (info->type_stack->fields)
1923 + 10);
1924 if (info->type_stack->baseclasses != NULL)
1926 len += 20;
1927 for (i = 0; info->type_stack->baseclasses[i] != NULL; i++)
1928 len += strlen (info->type_stack->baseclasses[i]);
1930 if (info->type_stack->methods != NULL)
1931 len += strlen (info->type_stack->methods);
1932 if (info->type_stack->vtable != NULL)
1933 len += strlen (info->type_stack->vtable);
1935 /* Build the class definition. */
1937 buf = (char *) xmalloc (len);
1939 strcpy (buf, info->type_stack->string);
1941 if (info->type_stack->baseclasses != NULL)
1943 sprintf (buf + strlen (buf), "!%u,", i);
1944 for (i = 0; info->type_stack->baseclasses[i] != NULL; i++)
1946 strcat (buf, info->type_stack->baseclasses[i]);
1947 free (info->type_stack->baseclasses[i]);
1949 free (info->type_stack->baseclasses);
1950 info->type_stack->baseclasses = NULL;
1953 strcat (buf, info->type_stack->fields);
1954 free (info->type_stack->fields);
1955 info->type_stack->fields = NULL;
1957 if (info->type_stack->methods != NULL)
1959 strcat (buf, info->type_stack->methods);
1960 free (info->type_stack->methods);
1961 info->type_stack->methods = NULL;
1964 strcat (buf, ";");
1966 if (info->type_stack->vtable != NULL)
1968 strcat (buf, info->type_stack->vtable);
1969 free (info->type_stack->vtable);
1970 info->type_stack->vtable = NULL;
1973 /* Replace the string on the top of the stack with the complete
1974 class definition. */
1975 free (info->type_stack->string);
1976 info->type_stack->string = buf;
1978 return TRUE;
1981 /* Push a typedef which was previously defined. */
1983 static bfd_boolean
1984 stab_typedef_type (p, name)
1985 PTR p;
1986 const char *name;
1988 struct stab_write_handle *info = (struct stab_write_handle *) p;
1989 struct string_hash_entry *h;
1991 h = string_hash_lookup (&info->typedef_hash, name, FALSE, FALSE);
1992 assert (h != NULL && h->index > 0);
1994 return stab_push_defined_type (info, h->index, h->size);
1997 /* Push a struct, union or class tag. */
1999 static bfd_boolean
2000 stab_tag_type (p, name, id, kind)
2001 PTR p;
2002 const char *name;
2003 unsigned int id;
2004 enum debug_type_kind kind;
2006 struct stab_write_handle *info = (struct stab_write_handle *) p;
2007 long index;
2008 unsigned int size;
2010 index = stab_get_struct_index (info, name, id, kind, &size);
2011 if (index < 0)
2012 return FALSE;
2014 return stab_push_defined_type (info, index, size);
2017 /* Define a typedef. */
2019 static bfd_boolean
2020 stab_typdef (p, name)
2021 PTR p;
2022 const char *name;
2024 struct stab_write_handle *info = (struct stab_write_handle *) p;
2025 long index;
2026 unsigned int size;
2027 char *s, *buf;
2028 struct string_hash_entry *h;
2030 index = info->type_stack->index;
2031 size = info->type_stack->size;
2032 s = stab_pop_type (info);
2034 buf = (char *) xmalloc (strlen (name) + strlen (s) + 20);
2036 if (index > 0)
2037 sprintf (buf, "%s:t%s", name, s);
2038 else
2040 index = info->type_index;
2041 ++info->type_index;
2042 sprintf (buf, "%s:t%ld=%s", name, index, s);
2045 free (s);
2047 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
2048 return FALSE;
2050 free (buf);
2052 h = string_hash_lookup (&info->typedef_hash, name, TRUE, FALSE);
2053 if (h == NULL)
2055 non_fatal (_("string_hash_lookup failed: %s"),
2056 bfd_errmsg (bfd_get_error ()));
2057 return FALSE;
2060 /* I don't think we care about redefinitions. */
2062 h->index = index;
2063 h->size = size;
2065 return TRUE;
2068 /* Define a tag. */
2070 static bfd_boolean
2071 stab_tag (p, tag)
2072 PTR p;
2073 const char *tag;
2075 struct stab_write_handle *info = (struct stab_write_handle *) p;
2076 char *s, *buf;
2078 s = stab_pop_type (info);
2080 buf = (char *) xmalloc (strlen (tag) + strlen (s) + 3);
2082 sprintf (buf, "%s:T%s", tag, s);
2083 free (s);
2085 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
2086 return FALSE;
2088 free (buf);
2090 return TRUE;
2093 /* Define an integer constant. */
2095 static bfd_boolean
2096 stab_int_constant (p, name, val)
2097 PTR p;
2098 const char *name;
2099 bfd_vma val;
2101 struct stab_write_handle *info = (struct stab_write_handle *) p;
2102 char *buf;
2104 buf = (char *) xmalloc (strlen (name) + 20);
2105 sprintf (buf, "%s:c=i%ld", name, (long) val);
2107 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
2108 return FALSE;
2110 free (buf);
2112 return TRUE;
2115 /* Define a floating point constant. */
2117 static bfd_boolean
2118 stab_float_constant (p, name, val)
2119 PTR p;
2120 const char *name;
2121 double val;
2123 struct stab_write_handle *info = (struct stab_write_handle *) p;
2124 char *buf;
2126 buf = (char *) xmalloc (strlen (name) + 20);
2127 sprintf (buf, "%s:c=f%g", name, val);
2129 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
2130 return FALSE;
2132 free (buf);
2134 return TRUE;
2137 /* Define a typed constant. */
2139 static bfd_boolean
2140 stab_typed_constant (p, name, val)
2141 PTR p;
2142 const char *name;
2143 bfd_vma val;
2145 struct stab_write_handle *info = (struct stab_write_handle *) p;
2146 char *s, *buf;
2148 s = stab_pop_type (info);
2150 buf = (char *) xmalloc (strlen (name) + strlen (s) + 20);
2151 sprintf (buf, "%s:c=e%s,%ld", name, s, (long) val);
2152 free (s);
2154 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
2155 return FALSE;
2157 free (buf);
2159 return TRUE;
2162 /* Record a variable. */
2164 static bfd_boolean
2165 stab_variable (p, name, kind, val)
2166 PTR p;
2167 const char *name;
2168 enum debug_var_kind kind;
2169 bfd_vma val;
2171 struct stab_write_handle *info = (struct stab_write_handle *) p;
2172 char *s, *buf;
2173 int stab_type;
2174 const char *kindstr;
2176 s = stab_pop_type (info);
2178 switch (kind)
2180 default:
2181 abort ();
2183 case DEBUG_GLOBAL:
2184 stab_type = N_GSYM;
2185 kindstr = "G";
2186 break;
2188 case DEBUG_STATIC:
2189 stab_type = N_STSYM;
2190 kindstr = "S";
2191 break;
2193 case DEBUG_LOCAL_STATIC:
2194 stab_type = N_STSYM;
2195 kindstr = "V";
2196 break;
2198 case DEBUG_LOCAL:
2199 stab_type = N_LSYM;
2200 kindstr = "";
2202 /* Make sure that this is a type reference or definition. */
2203 if (! ISDIGIT (*s))
2205 char *n;
2206 long index;
2208 index = info->type_index;
2209 ++info->type_index;
2210 n = (char *) xmalloc (strlen (s) + 20);
2211 sprintf (n, "%ld=%s", index, s);
2212 free (s);
2213 s = n;
2215 break;
2217 case DEBUG_REGISTER:
2218 stab_type = N_RSYM;
2219 kindstr = "r";
2220 break;
2223 buf = (char *) xmalloc (strlen (name) + strlen (s) + 3);
2224 sprintf (buf, "%s:%s%s", name, kindstr, s);
2225 free (s);
2227 if (! stab_write_symbol (info, stab_type, 0, val, buf))
2228 return FALSE;
2230 free (buf);
2232 return TRUE;
2235 /* Start outputting a function. */
2237 static bfd_boolean
2238 stab_start_function (p, name, globalp)
2239 PTR p;
2240 const char *name;
2241 bfd_boolean globalp;
2243 struct stab_write_handle *info = (struct stab_write_handle *) p;
2244 char *rettype, *buf;
2246 assert (info->nesting == 0 && info->fun_offset == -1);
2248 rettype = stab_pop_type (info);
2250 buf = (char *) xmalloc (strlen (name) + strlen (rettype) + 3);
2251 sprintf (buf, "%s:%c%s", name,
2252 globalp ? 'F' : 'f',
2253 rettype);
2255 /* We don't know the value now, so we set it in start_block. */
2256 info->fun_offset = info->symbols_size;
2258 if (! stab_write_symbol (info, N_FUN, 0, 0, buf))
2259 return FALSE;
2261 free (buf);
2263 return TRUE;
2266 /* Output a function parameter. */
2268 static bfd_boolean
2269 stab_function_parameter (p, name, kind, val)
2270 PTR p;
2271 const char *name;
2272 enum debug_parm_kind kind;
2273 bfd_vma val;
2275 struct stab_write_handle *info = (struct stab_write_handle *) p;
2276 char *s, *buf;
2277 int stab_type;
2278 char kindc;
2280 s = stab_pop_type (info);
2282 switch (kind)
2284 default:
2285 abort ();
2287 case DEBUG_PARM_STACK:
2288 stab_type = N_PSYM;
2289 kindc = 'p';
2290 break;
2292 case DEBUG_PARM_REG:
2293 stab_type = N_RSYM;
2294 kindc = 'P';
2295 break;
2297 case DEBUG_PARM_REFERENCE:
2298 stab_type = N_PSYM;
2299 kindc = 'v';
2300 break;
2302 case DEBUG_PARM_REF_REG:
2303 stab_type = N_RSYM;
2304 kindc = 'a';
2305 break;
2308 buf = (char *) xmalloc (strlen (name) + strlen (s) + 3);
2309 sprintf (buf, "%s:%c%s", name, kindc, s);
2310 free (s);
2312 if (! stab_write_symbol (info, stab_type, 0, val, buf))
2313 return FALSE;
2315 free (buf);
2317 return TRUE;
2320 /* Start a block. */
2322 static bfd_boolean
2323 stab_start_block (p, addr)
2324 PTR p;
2325 bfd_vma addr;
2327 struct stab_write_handle *info = (struct stab_write_handle *) p;
2329 /* Fill in any slots which have been waiting for the first known
2330 text address. */
2332 if (info->so_offset != -1)
2334 bfd_put_32 (info->abfd, addr, info->symbols + info->so_offset + 8);
2335 info->so_offset = -1;
2338 if (info->fun_offset != -1)
2340 bfd_put_32 (info->abfd, addr, info->symbols + info->fun_offset + 8);
2341 info->fun_offset = -1;
2344 ++info->nesting;
2346 /* We will be called with a top level block surrounding the
2347 function, but stabs information does not output that block, so we
2348 ignore it. */
2350 if (info->nesting == 1)
2352 info->fnaddr = addr;
2353 return TRUE;
2356 /* We have to output the LBRAC symbol after any variables which are
2357 declared inside the block. We postpone the LBRAC until the next
2358 start_block or end_block. */
2360 /* If we have postponed an LBRAC, output it now. */
2361 if (info->pending_lbrac != (bfd_vma) -1)
2363 if (! stab_write_symbol (info, N_LBRAC, 0, info->pending_lbrac,
2364 (const char *) NULL))
2365 return FALSE;
2368 /* Remember the address and output it later. */
2370 info->pending_lbrac = addr - info->fnaddr;
2372 return TRUE;
2375 /* End a block. */
2377 static bfd_boolean
2378 stab_end_block (p, addr)
2379 PTR p;
2380 bfd_vma addr;
2382 struct stab_write_handle *info = (struct stab_write_handle *) p;
2384 if (addr > info->last_text_address)
2385 info->last_text_address = addr;
2387 /* If we have postponed an LBRAC, output it now. */
2388 if (info->pending_lbrac != (bfd_vma) -1)
2390 if (! stab_write_symbol (info, N_LBRAC, 0, info->pending_lbrac,
2391 (const char *) NULL))
2392 return FALSE;
2393 info->pending_lbrac = (bfd_vma) -1;
2396 assert (info->nesting > 0);
2398 --info->nesting;
2400 /* We ignore the outermost block. */
2401 if (info->nesting == 0)
2402 return TRUE;
2404 return stab_write_symbol (info, N_RBRAC, 0, addr - info->fnaddr,
2405 (const char *) NULL);
2408 /* End a function. */
2410 static bfd_boolean
2411 stab_end_function (p)
2412 PTR p ATTRIBUTE_UNUSED;
2414 return TRUE;
2417 /* Output a line number. */
2419 static bfd_boolean
2420 stab_lineno (p, file, lineno, addr)
2421 PTR p;
2422 const char *file;
2423 unsigned long lineno;
2424 bfd_vma addr;
2426 struct stab_write_handle *info = (struct stab_write_handle *) p;
2428 assert (info->lineno_filename != NULL);
2430 if (addr > info->last_text_address)
2431 info->last_text_address = addr;
2433 if (strcmp (file, info->lineno_filename) != 0)
2435 if (! stab_write_symbol (info, N_SOL, 0, addr, file))
2436 return FALSE;
2437 info->lineno_filename = file;
2440 return stab_write_symbol (info, N_SLINE, lineno, addr - info->fnaddr,
2441 (const char *) NULL);