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