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