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