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