source: trunk/emx/src/emxomf/stabshll.c@ 3061

Last change on this file since 3061 was 3061, checked in by bird, 18 years ago

Set structure field name to <anonymous> if it doesn't have any name. Fixes #148.

  • Property cvs2svn:cvs-rev set to 1.35
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 118.8 KB
Line 
1/* stabshll.c -- Convert GNU-style a.out debug information into
2 IBM OS/2 HLL 3 debug information
3 Copyright (c) 1993-1998 Eberhard Mattes
4
5This file is part of emxomf.
6
7emxomf is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12emxomf is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with emxomf; see the file COPYING. If not, write to
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
21//#define HLL_DEBUG 1
22
23#define DEMANGLE_PROC_NAMES 1
24
25/*******************************************************************************
26* Header Files *
27*******************************************************************************/
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <stdarg.h>
32#include <time.h>
33#include <alloca.h>
34#include <ctype.h>
35#ifdef DEMANGLE_PROC_NAMES
36# include <demangle.h>
37#endif
38#include "defs.h"
39#include "emxomf.h"
40#include "grow.h"
41#include "stabshll.h"
42
43
44/*******************************************************************************
45* Defined Constants And Macros *
46*******************************************************************************/
47/* Field ID values for the type table. */
48
49#define FID_nil 0x80 /* No data */
50#define FID_void 0x81 /* Type void */
51#define FID_string 0x82 /* A length-prefixed string follows */
52#define FID_index 0x83 /* A type index follows */
53#define FID_span_16u 0x85 /* An unsigned 16-bit number follows */
54#define FID_span_32u 0x86 /* An unsigned 32-bit number follows */
55#define FID_span_8s 0x88 /* A signed 8-bit number follows */
56#define FID_span_16s 0x89 /* A signed 16-bit number follows */
57#define FID_span_32s 0x8a /* A signed 32-bit number follows */
58#define FID_span_8u 0x8b /* An unsigned 8-bit number follows */
59
60/* Static scope table subrecord types. */
61
62#define SST_begin 0x00 /* Begin block */
63#define SST_proc 0x01 /* Procedure */
64#define SST_end 0x02 /* End block or end procedure */
65#define SST_auto 0x04 /* Scoped variable */
66#define SST_static 0x05 /* Static variable */
67#define SST_reg 0x0d /* Register variable */
68#define SST_changseg 0x11 /* Change default segment */
69#define SST_tag 0x16 /* Structure, union, enum tags */
70#define SST_tag2 0x19 /* Extended tag for long names (C++ class). */
71#define SST_memfunc 0x1a /* C++ member function */
72#define SST_CPPproc 0x1d /* C++ function */
73#define SST_CPPstatic 0x1e /* C++ static variable */
74#define SST_cuinfo 0x40 /* Compile unit information */
75
76/* Class visibility. */
77
78#define VIS_PRIVATE 0x00 /* private */
79#define VIS_PROTECTED 0x01 /* protected */
80#define VIS_PUBLIC 0x02 /* public */
81
82#define MEMBER_VIS 0x03 /* Member visibility mask */
83#define MEMBER_FUNCTION 0x08 /* It's a member function */
84#define MEMBER_VIRTUAL 0x10 /* Member function / base class is virtual */
85#define MEMBER_VOLATILE 0x20 /* Member function is volatile */
86#define MEMBER_CONST 0x40 /* Member function is const */
87#define MEMBER_STATIC 0x80 /* Member is static */
88#define MEMBER_CTOR 0x100 /* Member function is a constructor */
89#define MEMBER_DTOR 0x200 /* Member function is a destructor */
90#define MEMBER_BASE 0x400 /* It's a base class */
91
92/* Flags for ty_struc. */
93
94#define STRUC_FORWARD 0x01 /* Structure declared forward */
95
96
97/*******************************************************************************
98* Structures and Typedefs *
99*******************************************************************************/
100enum type_tag
101{
102 ty_stabs_ref, /* Reference to stabs type */
103 ty_prim, /* Primitive type */
104 ty_alias, /* Equivalent type */
105 ty_pointer, /* Pointer type */
106 ty_func, /* Function */
107 ty_args, /* Function arguments */
108 ty_struc, /* Structure or union */
109 ty_types, /* Type list for structure/union */
110 ty_fields, /* Field list for structure/union */
111 ty_enu, /* Enumeration type */
112 ty_values, /* Enumeration values */
113 ty_array, /* Array */
114 ty_bits, /* Bit field / bit string */
115 ty_ref, /* Reference type (C++) */
116 ty_member, /* Member of a C++ class */
117 ty_class, /* A C++ class */
118 ty_memfunc, /* A C++ member function */
119 ty_baseclass, /* A C++ base class */
120 ty_max
121};
122
123/* A structure field. */
124
125struct field
126{
127 dword offset; /* Offset, in bytes */
128 const char *name; /* Field name */
129};
130
131/* An enumeration value. */
132
133struct value
134{
135 long index; /* The value */
136 const char *name; /* The name */
137};
138
139/* Temporary representation of a structure field. */
140
141struct tmp_field
142{
143 struct type *type; /* The internal type */
144 dword offset; /* The offset of the field */
145 const char *name; /* The field name */
146 const char *sname; /* Static name */
147 const char *mnglname; /* Mangled name for methods. (#456) */
148 int flags; /* Member flags (visibility) */
149};
150
151
152/* Internal representation of a type. */
153
154struct type
155{
156 enum type_tag tag; /* The type of this type */
157 int index; /* The HLL index for this type */
158 struct type *next; /* All types are chained in a single list */
159 struct type *nexthash; /* All types are chained in the hash list */
160 union
161 {
162 int stabs_ref; /* Reference a stabs type */
163 struct type *pointer; /* Pointer to another type */
164 struct type *alias; /* Alias for another type */
165 struct type *ref; /* Reference to another type */
166 struct
167 { /* Structure */
168 struct type *types; /* Types of the fields */
169 struct type *fields; /* Field names and offsets */
170 dword size; /* Size of the structure, in bytes */
171 word count; /* Number of fields */
172 word flags; /* Flags (STRUC_FORWARD) */
173 const char *name; /* Name of the structure */
174 } struc;
175 struct
176 { /* Class */
177 struct type *members; /* Members */
178 dword size; /* Size of the class, in bytes */
179 word count; /* Number of fields */
180 const char *name; /* Name of the class */
181 } class;
182 struct
183 { /* Enumeration */
184 struct type *type; /* Base type */
185 struct type *values; /* Values */
186 long first; /* Smallest value */
187 long last; /* Biggest value */
188 const char *name; /* Name of the enum */
189 } enu;
190 struct
191 { /* Array */
192 struct type *itype; /* Index type */
193 struct type *etype; /* Element type */
194 long first; /* First index */
195 long last; /* Last index */
196 } array;
197 struct
198 { /* Bitfield */
199 struct type *type; /* Base type */
200 long start; /* First bit */
201 long count; /* Number of bits */
202 } bits;
203 struct
204 { /* Function */
205 struct type *ret; /* Return type */
206 struct type *args; /* Argument types */
207 struct type *domain; /* Domain type (C++ member) (#456) */
208 int arg_count; /* Number of arguments */
209 } func;
210 struct
211 { /* Function arguments */
212 struct type **list; /* Array of argument types */
213 int count; /* Number of arguments */
214 } args;
215 struct
216 { /* List of types */
217 struct type **list; /* Array of types */
218 int count; /* Number of types */
219 } types;
220 struct
221 { /* Structure fields */
222 struct field *list; /* The fields */
223 int count; /* Number of fields */
224 } fields;
225 struct
226 { /* Class member */
227 struct type *type; /* Type of the member */
228 dword offset; /* Offset of the member, in bytes */
229 int flags; /* Member flags (visibility etc.) */
230 const char *name; /* Name of the member */
231 const char *sname; /* Static name */
232 } member;
233 struct
234 { /* Enumeration values */
235 struct value *list; /* Array of values */
236 int count; /* Number of values */
237 } values;
238 struct
239 { /* Member function */
240 struct type *type; /* Function type */
241 dword offset; /* Offset in the virtual table */
242 int flags; /* Member flags (visibility etc.) */
243 const char *name; /* Name of the member function */
244 const char *mnglname; /* Mangled Name of the member function. (#456) */
245 } memfunc;
246 struct
247 { /* Base class */
248 struct type *type; /* The base class */
249 dword offset; /* Offset of the base class */
250 int flags; /* Visibility, virtual */
251 } baseclass;
252 } d;
253};
254
255
256/* A block. */
257
258struct block
259{
260 dword addr; /* Start address of the block */
261 size_t patch_ptr; /* Index into SST buffer for patching */
262};
263
264/* Timestamp for the compiler unit information SST subrecord. */
265
266#pragma pack(1)
267
268struct timestamp
269{
270 byte hours;
271 byte minutes;
272 byte seconds;
273 byte hundredths;
274 byte day;
275 byte month;
276 word year;
277};
278
279#pragma pack()
280
281
282/*******************************************************************************
283* Global Variables *
284*******************************************************************************/
285/* This variable points to the next character of a stabs type being
286 parsed. */
287
288static const char *parse_ptr;
289
290/* Start of the stabs string we're parsing. Useful when printing
291 messages. */
292static const char *parse_start;
293
294/* Current stabs index. Useful when printing messages. */
295static int *parse_pindex;
296
297/* The next HLL type index. When creating a complex type, this index
298 is used. Then, this variable is incremented. */
299
300static int hll_type_index;
301
302/* We keep all internal types in a single list. When creating a new
303 type, we first look through the list to find out whether an
304 identical type already exists. Doing a linear search is
305 inefficient, but that doesn't matter here, according to
306 experience. This variable points to the first type of the list. */
307
308static struct type *type_head;
309#define TYPE_HASH_MAX 211
310static struct type *atype_tag[TYPE_HASH_MAX];
311#ifdef HASH_DEBUG
312static int ctypes = 0;
313static int ctype_tag[TYPE_HASH_MAX];
314#endif
315
316/* This growing array keeps track of stabs vs. internal types. It is
317 indexed by stabs type numbers. */
318
319static struct type **stype_list;
320static struct grow stype_grow;
321
322/* We collect function arguments in this growing array. */
323
324static struct type **args_list;
325static struct grow args_grow;
326
327/* This stack (implemented with a growing array) keeps track of
328 begin/end block nesting. */
329
330static struct block *block_stack;
331static struct grow block_grow;
332
333/* This string pool contains field names etc. */
334
335static struct strpool *str_pool;
336
337/* This variable holds the sym_ptr index of the most recently
338 encountered N_LBRAC symbol. */
339
340static int lbrac_index;
341
342/* The index (in sst) of the most recently started SST subrecord. */
343
344static size_t subrec_start;
345
346/* The proc_patch_base contains the index where the base address and
347 function length are to be patched into the SST. */
348
349static size_t proc_patch_base;
350
351/* The base address used by the most recently proc record in the
352 SST. */
353
354static size_t proc_start_addr;
355
356/* The internal type of the most recently defined function. Valid
357 only if last_fun_valid is TRUE. */
358
359static struct type last_fun_type;
360
361/* This variable is TRUE if a function has recently been defined. */
362
363static int last_fun_valid;
364
365/* The length of the prologue of the most recently defined
366 function. */
367
368static int prologue_length;
369
370/* The address of the most recently defined function. */
371
372static dword last_fun_addr;
373
374/* This variable is non-zero when converting a translated C++
375 program. */
376
377static int cplusplus_flag;
378
379/* Unnamed structures are numbered sequentially, using this
380 counter. */
381
382static int unnamed_struct_number;
383
384/* kso #456 2003-06-11: Reversed quiet workaround. */
385#define no_warning warning_parse
386
387
388/*******************************************************************************
389* Internal Functions *
390*******************************************************************************/
391static void parse_typedef (int *index);
392static void warning_parse (const char *pszFormat, ...);
393
394
395/**
396 * Report an warning in which might be connecting to parsing.
397 *
398 * @param pszFormat Message string.
399 * @param ... More arguments
400 */
401static void warning_parse (const char *pszFormat, ...)
402{
403 va_list args;
404
405 va_start (args, pszFormat);
406 fprintf (stderr, "emxomf warning: ");
407 vfprintf (stderr, pszFormat, args);
408 va_end (args);
409 fputc ('\n', stderr);
410
411
412 if (parse_ptr && parse_start && parse_ptr >= parse_start)
413 {
414 if (parse_pindex && *parse_pindex >= 0 && *parse_pindex < sym_count)
415 fprintf (stderr, "emxomf info: parsing sym no.%d type=%d at char '%c' in position %d:\n%s\n",
416 *parse_pindex, sym_ptr[*parse_pindex].n_type,
417 *parse_ptr, parse_ptr - parse_start, parse_start);
418 else
419 fprintf (stderr, "emxomf info: parsing '%c' at position %d:\n%s\n",
420 *parse_ptr, parse_ptr - parse_start, parse_start);
421 }
422}
423
424
425/* Start a type table record of type TYPE, with type qualifier QUAL.
426 tt_end() must be called to finish the record after writing the
427 record contents. */
428
429static void tt_start (byte type, byte qual)
430{
431 subrec_start = tt.size;
432 grow_by (&tt_boundary_grow, 1);
433 tt_boundary[tt_boundary_grow.count++] = tt.size;
434 buffer_word (&tt, 0); /* Length of sub-record */
435 buffer_byte (&tt, type); /* Type of sub-record */
436 buffer_byte (&tt, qual); /* Type qualifier */
437}
438
439
440/* Finish a type table record. This function must be called after
441 tt_start(). */
442
443static void tt_end (void)
444{
445 *(word *)(tt.buf + subrec_start) = tt.size - subrec_start - 2;
446}
447
448
449/* Put the type index TI into the type table. TI = -1 encodes the
450 `nil' type (no type), 0x97 encodes the `void' type. All other
451 values are type indices. */
452
453static void tt_index (int ti)
454{
455 if (ti == -1) /* nil */
456 buffer_byte (&tt, FID_nil);
457 else if (ti == 0x97) /* void */
458 buffer_byte (&tt, FID_void);
459 else
460 {
461 buffer_byte (&tt, FID_index);
462 buffer_word (&tt, ti);
463 }
464}
465
466
467/* Put an 8-bit byte into the type table. */
468
469static void tt_byte (byte x)
470{
471 buffer_byte (&tt, x);
472}
473
474
475/* Put a 16-bit word into the type table. */
476
477static void tt_word (word x)
478{
479 buffer_word (&tt, x);
480}
481
482
483/* Put a 32-bit double word into the type table. */
484
485static void tt_dword (dword x)
486{
487 buffer_dword (&tt, x);
488}
489
490
491/* Put the name S into the type table. */
492
493static void tt_name (const char *s)
494{
495 buffer_byte (&tt, FID_string);
496 buffer_nstr (&tt, s);
497}
498
499
500/* Put the mangled name S into the type table. */
501
502static void tt_enc (const char *s)
503{
504 buffer_enc (&tt, s);
505}
506
507
508/* Put the unsigned number N into the type table. Depending on the
509 magnitude of N, different encodings are used. */
510
511static void tt_unsigned_span (dword n)
512{
513 if (n <= 0xff)
514 {
515 buffer_byte (&tt, FID_span_8u);
516 buffer_byte (&tt, n);
517 }
518 else if (n <= 0xffff)
519 {
520 buffer_byte (&tt, FID_span_16u);
521 buffer_word (&tt, n);
522 }
523 else
524 {
525 buffer_byte (&tt, FID_span_32u);
526 buffer_dword (&tt, n);
527 }
528}
529
530
531/* Put the signed number N into the type table. Depending on the
532 magnitude of N, different encodings are used. */
533
534static void tt_signed_span (long n)
535{
536 if (-127 <= n && n <= 127)
537 {
538 buffer_byte (&tt, FID_span_8s);
539 buffer_byte (&tt, n);
540 }
541 else if (-32767 <= n && n <= 32767)
542 {
543 buffer_byte (&tt, FID_span_16s);
544 buffer_word (&tt, n);
545 }
546 else
547 {
548 buffer_byte (&tt, FID_span_32s);
549 buffer_dword (&tt, n);
550 }
551}
552
553
554/* Start a static scope table subrecord of type TYPE. sst_end() must
555 be called to finish the subrecord after writing the subrecord
556 data. */
557
558static void sst_start (byte type)
559{
560 subrec_start = sst.size;
561 grow_by (&sst_boundary_grow, 1);
562 sst_boundary[sst_boundary_grow.count++] = sst.size;
563 /* Length of sub-record - bird: not sure if HL03 likes this. */
564 buffer_byte (&sst, 0x80);
565 buffer_byte (&sst, 0);
566 /* Type of sub-record */
567 buffer_byte (&sst, type);
568}
569
570
571/* Finish a static scope table subrecord. This function must be
572 called after sst_start(). */
573
574static void sst_end (void)
575{
576 int len = sst.size - subrec_start - 2;
577 /* -- experimental...
578 if (len > 1000)
579 error ("sst_end: Record too big");
580 */
581 sst.buf[subrec_start] = (len >> 8) | 0x80;
582 sst.buf[subrec_start + 1] = 0xff & len;
583}
584
585
586/* Define a static variable in the static scope table. NAME is the
587 name of the variable, ADDR is the address of the variable (segment
588 offset), TYPE_INDEX is the HLL type index. If EXT is zero, SYM_SEG
589 is the segment (N_DATA, for instance). If EXT is non-zero, SYM_SEG
590 is the stabs symbol index (for the relocation table). This
591 function also creates appropriate relocation table entries. */
592
593static void sst_static (const char *name, dword addr, int type_index,
594 int sym_seg, int ext, const struct nlist *psym)
595{
596 struct relocation_info r;
597 size_t len = strlen (name);
598
599 if (!psym)
600 {
601 psym = find_symbol_ex (name, -1, ext);
602 if (!psym)
603 {
604 char *psz = alloca (len + 2);
605 *psz = '_';
606 memcpy(psz + 1, name, len + 1);
607 psym = find_symbol_ex (psz, -1, ext);
608 }
609 }
610 if (psym)
611 set_hll_type (psym - sym_ptr, type_index);
612
613 if (len > 255)
614 sst_start (SST_CPPstatic);
615 else
616 sst_start (SST_static);
617 r.r_address = sst.size;
618 buffer_dword (&sst, addr); /* Segment offset */
619 buffer_word (&sst, 0); /* Segment address */
620 buffer_word (&sst, type_index); /* Type index */
621 if (len > 255)
622 buffer_enc (&sst, name); /* Symbol name - encoded. */
623 else
624 buffer_nstr (&sst, name); /* Symbol name */
625 sst_end ();
626 r.r_extern = ext;
627 r.r_length = 2;
628 r.r_symbolnum = sym_seg;
629 r.r_pcrel = 0;
630 r.r_pad = 0;
631 buffer_mem (&sst_reloc, &r, sizeof (r));
632 r.r_address += 4;
633 r.r_length = 1;
634 buffer_mem (&sst_reloc, &r, sizeof (r));
635}
636
637/**
638 * Try calculate a good hash (yeah sure).
639 *
640 * @returns hash index.
641 * @param t type
642 */
643static inline unsigned type_hash (struct type *t)
644{
645 register unsigned uhash = 0;
646 switch (t->tag)
647 {
648 case ty_alias: uhash = (unsigned)t->d.alias; break;
649 case ty_stabs_ref: uhash = (unsigned)t->d.stabs_ref; break;
650 case ty_prim: uhash = (unsigned)t->index; break;
651 case ty_pointer: uhash = (unsigned)t->d.pointer; break;
652 case ty_ref: uhash = (unsigned)t->d.ref; break;
653 case ty_struc: uhash = (unsigned)t->d.struc.name; break;
654 case ty_class: uhash = (unsigned)t->d.class.name; break;
655 case ty_enu: uhash = (unsigned)t->d.enu.name; break;
656 case ty_array: uhash = (unsigned)t->d.array.etype; break;
657 case ty_bits: uhash = (unsigned)t->d.bits.type; break;
658 case ty_func: uhash = (unsigned)t->d.func.args ^ (unsigned)t->d.func.arg_count ^ (unsigned)t->d.func.ret ^ (unsigned)t->d.func.domain; break;
659 case ty_types: uhash = (unsigned)t->d.types.count << 3; break;
660 case ty_args: uhash = (unsigned)t->d.args.count << 3; break;
661 case ty_fields: uhash = (unsigned)t->d.fields.count << 3; break;
662 case ty_member: uhash = (unsigned)t->d.member.type ^ (unsigned)t->d.member.name >> 7; break;
663 case ty_values: uhash = (unsigned)t->d.values.count << 3; break;
664 case ty_memfunc: uhash = (unsigned)t->d.memfunc.type; break;
665 case ty_baseclass: uhash = (unsigned)t->d.baseclass.type; break;
666 default:break; /* shut up warnings. */
667 }
668 uhash += t->tag;
669 return (uhash ^ uhash >> 13) % TYPE_HASH_MAX;
670}
671
672
673/**
674 * Search a list for a type.
675 * @returns pointer to the type we found.
676 * @returns NULL if not found.
677 * @param p Pointer to the head list.
678 * @param src Type to find.
679 * @param fhash Whether or not to follow the hash list or the main list.
680 */
681#ifdef HASH_DEBUG
682static struct type *type_find (struct type *p, const struct type *src, int fhash)
683#else
684static struct type *type_find (struct type *p, const struct type *src)
685#endif
686{
687 int i;
688 for (; p != NULL;
689#ifdef HASH_DEBUG
690 p = fhash ? p->nexthash : p->next )
691#else
692 p = p->nexthash )
693#endif
694 if (p->tag == src->tag)
695 switch (p->tag)
696 {
697 case ty_alias:
698 if (p->d.alias == src->d.alias)
699 return p;
700 break;
701 case ty_stabs_ref:
702 if (p->d.stabs_ref == src->d.stabs_ref)
703 return p;
704 break;
705 case ty_prim:
706 if (p->index == src->index)
707 return p;
708 break;
709 case ty_pointer:
710 if (p->d.pointer == src->d.pointer)
711 return p;
712 break;
713 case ty_ref:
714 if (p->d.ref == src->d.ref)
715 return p;
716 break;
717 case ty_struc:
718 if (p->d.struc.fields == src->d.struc.fields
719 && p->d.struc.types == src->d.struc.types
720 && p->d.struc.name == src->d.struc.name
721 && p->d.struc.flags == src->d.struc.flags)
722 return p;
723 break;
724 case ty_class:
725 if (p->d.class.members == src->d.class.members
726 && p->d.class.name == src->d.class.name)
727 return p;
728 break;
729 case ty_enu:
730 if (p->d.enu.type == src->d.enu.type
731 && p->d.enu.values == src->d.enu.values
732 && p->d.enu.name == src->d.enu.name)
733 return p;
734 break;
735 case ty_array:
736 if (p->d.array.etype == src->d.array.etype
737 && p->d.array.itype == src->d.array.itype
738 && p->d.array.first == src->d.array.first
739 && p->d.array.last == src->d.array.last)
740 return p;
741 break;
742 case ty_bits:
743 if (p->d.bits.type == src->d.bits.type
744 && p->d.bits.start == src->d.bits.start
745 && p->d.bits.count == src->d.bits.count)
746 return p;
747 break;
748 case ty_func:
749 if (p->d.func.ret == src->d.func.ret
750 && p->d.func.args == src->d.func.args
751 && p->d.func.domain == src->d.func.domain
752 && p->d.func.arg_count == src->d.func.arg_count)
753 return p;
754 break;
755 case ty_args:
756 if (p->d.args.count == src->d.args.count)
757 {
758 for (i = 0; i < p->d.args.count; ++i)
759 if (p->d.args.list[i] != src->d.args.list[i])
760 break;
761 if (i >= p->d.args.count)
762 return p;
763 }
764 break;
765 case ty_types:
766 if (p->d.types.count == src->d.types.count)
767 {
768 for (i = 0; i < p->d.types.count; ++i)
769 if (p->d.types.list[i] != src->d.types.list[i])
770 break;
771 if (i >= p->d.types.count)
772 return p;
773 }
774 break;
775 case ty_fields:
776 if (p->d.fields.count == src->d.fields.count)
777 {
778 for (i = 0; i < p->d.fields.count; ++i)
779 if (p->d.fields.list[i].offset != src->d.fields.list[i].offset
780 || p->d.fields.list[i].name != src->d.fields.list[i].name)
781 break;
782 if (i >= p->d.fields.count)
783 return p;
784 }
785 break;
786 case ty_member:
787 if (p->d.member.type == src->d.member.type
788 && p->d.member.offset == src->d.member.offset
789 && p->d.member.flags == src->d.member.flags
790 && p->d.member.name == src->d.member.name
791 && p->d.member.sname == src->d.member.sname)
792 return p;
793 break;
794 case ty_values:
795 if (p->d.values.count == src->d.values.count)
796 {
797 for (i = 0; i < p->d.values.count; ++i)
798 if (p->d.values.list[i].index != src->d.values.list[i].index
799 || p->d.values.list[i].name != src->d.values.list[i].name)
800 break;
801 if (i >= p->d.values.count)
802 return p;
803 }
804 break;
805 case ty_memfunc:
806 if (p->d.memfunc.type == src->d.memfunc.type
807 && p->d.memfunc.offset == src->d.memfunc.offset
808 && p->d.memfunc.flags == src->d.memfunc.flags
809 && p->d.memfunc.name == src->d.memfunc.name
810 && p->d.memfunc.mnglname == src->d.memfunc.mnglname)
811 return p;
812 break;
813 case ty_baseclass:
814 if (p->d.baseclass.type == src->d.baseclass.type
815 && p->d.baseclass.offset == src->d.baseclass.offset
816 && p->d.baseclass.flags == src->d.baseclass.flags)
817 return p;
818 break;
819 default:
820 abort ();
821 }
822 return NULL;
823}
824
825
826/* Add a new internal type to the list of internal types. If an
827 identical type already exists, a pointer to that type is returned.
828 Otherwise, a copy of the new type is added to the list and a
829 pointer to that type is returned. It isn't worth my while to
830 improve the speed of this function, for instance by using
831 hashing.
832
833 Actually it is worth while optimizing it as it takes 85% of the
834 time here... or rather it was.
835
836 ctypes=25301 */
837
838static struct type *type_add (struct type *src)
839{
840 struct type *p;
841 unsigned ihash;
842 int i;
843
844 ihash = type_hash(src);
845#ifdef HASH_DEBUG
846 p = type_find (atype_tag[ihash], src, 1);
847#else
848 p = type_find (atype_tag[ihash], src);
849#endif
850 if (p)
851 {
852 return p;
853 }
854
855#ifdef HASH_DEBUG
856/* {
857 struct type *pcheck = type_find (type_head, src, 0);
858 if (pcheck)
859 {
860 printf("\n\thash algorithm is borked! tag=%d\n", pcheck->tag);
861 return pcheck;
862 }
863 } */
864 ctype_tag[ihash]++;
865 ctypes++;
866#endif
867
868 p = xmalloc (sizeof (*p));
869 *p = *src;
870 p->next = type_head;
871 type_head = p;
872 p->nexthash = atype_tag[ihash];
873 atype_tag[ihash] = p;
874 switch (src->tag)
875 {
876 case ty_args:
877 i = src->d.args.count * sizeof (*(src->d.args.list));
878 p->d.args.list = xmalloc (i);
879 memcpy (p->d.args.list, src->d.args.list, i);
880 break;
881 case ty_types:
882 i = src->d.types.count * sizeof (*(src->d.types.list));
883 p->d.types.list = xmalloc (i);
884 memcpy (p->d.types.list, src->d.types.list, i);
885 break;
886 case ty_fields:
887 i = src->d.fields.count * sizeof (*(src->d.fields.list));
888 p->d.fields.list = xmalloc (i);
889 memcpy (p->d.fields.list, src->d.fields.list, i);
890 break;
891 case ty_values:
892 i = src->d.values.count * sizeof (*(src->d.values.list));
893 p->d.values.list = xmalloc (i);
894 memcpy (p->d.values.list, src->d.values.list, i);
895 break;
896 default:
897 break;
898 }
899 return p;
900}
901
902
903/* Associate the stabs type number NUMBER with the internal type
904 HLL. */
905
906static void stype_add (int number, struct type *hll)
907{
908 int i;
909
910 /* Stabs type numbers are greater than zero. */
911
912 if (number < 1)
913 error ("stype_add: Invalid type index %d", number);
914
915 /* Remember the current size of the table and grow the table as
916 required. */
917
918 i = stype_grow.alloc;
919 grow_to (&stype_grow, number);
920
921 /* Initialize the new table entries. */
922
923 while (i < stype_grow.alloc)
924 stype_list[i++] = NULL;
925
926 /* Put the type into the table. */
927
928#ifdef HLL_DEBUG
929 printf (" type: stabs %d => HLL 0x%x\n", number, hll->index);
930#endif
931#if 0 /* not good test... as we do some types twice. */
932 if (stype_list[number-1] != NULL && stype_list[number-1] != hll)
933 warning ("Type stabs %d allready mapped to HLL 0x%x, new HLL 0x%x!!\n", number, stype_list[number-1]->index, hll->index);
934#endif
935
936 stype_list[number-1] = hll;
937}
938
939
940/* Find the internal type associated with the stabs type number
941 NUMBER. If there is no such type, return NULL. */
942
943static struct type *stype_find (int number)
944{
945 /* Stabs type numbers are greater than zero. */
946
947 if (number < 1)
948 error ("stype_find: Invalid type index %d", number);
949
950 /* Adjust the index and check if it is within the current size of
951 the table. */
952
953 --number;
954 if (number >= stype_grow.alloc)
955 return NULL;
956 else
957 return stype_list[number];
958}
959
960
961/* Dereference stabs references until finding a non-reference. */
962
963static const struct type *follow_refs (const struct type *t)
964{
965 const struct type *t2;
966
967 while (t->tag == ty_stabs_ref)
968 {
969 t2 = stype_find (t->d.stabs_ref);
970 if (t2 == NULL)
971 break;
972 t = t2;
973 }
974
975 return t;
976}
977
978
979#if defined (HLL_DEBUG)
980
981/* This function is used to print an internal type for debugging. */
982
983static void show_type (struct type *tp)
984{
985 /* precaution so we don't iterate for ever here when showing classes. */
986 static int fShowClass;
987 int i;
988
989 /* Currently we crash when showing FILE (stdio.h). So, better not crash here.. */
990 if (!tp)
991 {
992 printf("!!! error! NULL type pointer! !!!");
993 return;
994 }
995
996 switch (tp->tag)
997 {
998 case ty_prim:
999 printf ("%#x", tp->index);
1000 break;
1001 case ty_stabs_ref:
1002 printf ("t%d", tp->d.stabs_ref);
1003 break;
1004 case ty_alias:
1005 show_type (tp->d.alias);
1006 break;
1007 case ty_pointer:
1008 printf ("(");
1009 show_type (tp->d.pointer);
1010 printf (")*");
1011 break;
1012 case ty_ref:
1013 printf ("(");
1014 show_type (tp->d.ref);
1015 printf (")&");
1016 break;
1017 case ty_struc:
1018 printf ("{");
1019 show_type (tp->d.struc.types);
1020 printf ("}");
1021 break;
1022 case ty_class:
1023 printf ("{");
1024 if (!fShowClass)
1025 {
1026 fShowClass = 1;
1027 show_type (tp->d.class.members);
1028 fShowClass = 0;
1029 }
1030 else
1031 printf ("<-!-!->");
1032 printf ("}");
1033 break;
1034 case ty_enu:
1035 printf ("{");
1036 show_type (tp->d.enu.values);
1037 printf ("}");
1038 break;
1039 case ty_array:
1040 printf ("(");
1041 show_type (tp->d.array.etype);
1042 printf ("[%ld]", tp->d.array.last + 1 - tp->d.array.first);
1043 break;
1044 case ty_bits:
1045 show_type (tp->d.bits.type);
1046 printf (":%ld:%ld", tp->d.bits.start, tp->d.bits.count);
1047 break;
1048 case ty_func:
1049 show_type (tp->d.func.ret);
1050 printf ("(");
1051 if (tp->d.func.args != NULL)
1052 show_type (tp->d.func.args);
1053 printf (")");
1054 break;
1055 case ty_args:
1056 for (i = 0; i < tp->d.args.count; ++i)
1057 {
1058 if (i != 0)
1059 printf (", ");
1060 show_type (tp->d.args.list[i]);
1061 }
1062 break;
1063 case ty_types:
1064 for (i = 0; i < tp->d.types.count; ++i)
1065 {
1066 if (i != 0)
1067 printf (", ");
1068 show_type (tp->d.types.list[i]);
1069 }
1070 break;
1071 case ty_fields:
1072 for (i = 0; i < tp->d.fields.count; ++i)
1073 {
1074 if (i != 0)
1075 printf (", ");
1076 printf ("%s", tp->d.fields.list[i].name);
1077 }
1078 break;
1079 case ty_member:
1080 printf ("%s", tp->d.member.name);
1081 break;
1082 case ty_values:
1083 for (i = 0; i < tp->d.values.count; ++i)
1084 {
1085 if (i != 0)
1086 printf (", ");
1087 printf ("%s", tp->d.values.list[i].name);
1088 }
1089 break;
1090 case ty_memfunc:
1091 show_type (tp->d.memfunc.type);
1092 break;
1093 case ty_baseclass:
1094 printf ("{");
1095 show_type (tp->d.baseclass.type);
1096 printf ("}");
1097 break;
1098 default:
1099 error ("show_type: Unknown type: %d", tp->tag);
1100 }
1101}
1102
1103#endif
1104
1105
1106/**
1107 * Tries to complete a struct forward reference.
1108 *
1109 * @returns 0 on success
1110 * @returns 1 on failure.
1111 * @param tp Pointer to the struct/class to complete.
1112 * @remark Nasy hack for strstreambuf.
1113 */
1114static int try_complete_struct(const struct type *tp)
1115{
1116 int cch;
1117 const char *pszName;
1118 int i;
1119
1120 if (tp->tag == ty_struc)
1121 return 0;
1122 if (tp->d.struc.flags != STRUC_FORWARD)
1123 return 1;
1124
1125 pszName = tp->d.struc.name;
1126 cch = strlen(pszName);
1127 for (i = 0; i < sym_count; ++i)
1128 switch (sym_ptr[i].n_type)
1129 {
1130 case N_LSYM:
1131 case N_LCSYM:
1132 case N_GSYM:
1133 case N_PSYM:
1134 case N_RSYM:
1135 case N_STSYM:
1136 case N_FUN:
1137 if ( !memcmp(str_ptr + sym_ptr[i].n_un.n_strx, pszName, cch)
1138 && *(char*)(str_ptr + sym_ptr[i].n_un.n_strx + cch) == ':'
1139 )
1140 {
1141 parse_typedef(&i);
1142 return 1;
1143 } /* if */
1144 } /* switch */
1145
1146 return 0;
1147}
1148
1149
1150
1151/* Return the size of the internal type TP, in bytes. */
1152
1153static dword type_size (const struct type *tp)
1154{
1155 switch (tp->tag)
1156 {
1157 case ty_prim:
1158
1159 /* Primitive types. */
1160
1161 if (tp->index >= 0xa0 && tp->index <= 0xbf)
1162 return 4; /* Near pointer */
1163 switch (tp->index)
1164 {
1165 case 0x97: /* void */
1166 return 0;
1167 case 0x80: /* 8 bit signed */
1168 case 0x84: /* 8 bit unsigned */
1169 case 0x90: /* 8 bit boolean */
1170 return 1;
1171 case 0x81: /* 16 bit signed */
1172 case 0x85: /* 16 bit unsigned */
1173 return 2;
1174 case 0x82: /* 32 bit signed */
1175 case 0x86: /* 32 bit unsigned */
1176 case 0x88: /* 32 bit real */
1177 return 4;
1178 case 0x89: /* 64 bit real */
1179 case 0x8c: /* 64 bit complex */
1180 return 8;
1181 case 0x8a: /* 80 bit real (96 bits of storage) */
1182 return 12;
1183 case 0x8d: /* 128 bit complex */
1184 return 16;
1185 case 0x8e: /* 160 bit complex */
1186 return 24;
1187 case 0x8f: /* 256 bit complex (birds invention) */
1188 return 32;
1189 }
1190 error ("type_size: Unknown primitive type: %d", tp->index);
1191
1192 case ty_stabs_ref:
1193
1194 #if 0
1195 /* This should not happen. */
1196
1197 no_warning ("stabs type %d not defined", tp->d.stabs_ref);
1198
1199 return 4;
1200 #else
1201 /* This seems to happen although it shouldn't... Try do something at least. */
1202
1203 tp = follow_refs (tp);
1204 if (!tp || tp->tag == ty_stabs_ref)
1205 {
1206 no_warning ("stabs type %d not defined", tp->d.stabs_ref);
1207 return 4;
1208 }
1209 return type_size (tp);
1210 #endif
1211
1212 case ty_alias:
1213
1214 /* An alias. Return the size of the referenced type. */
1215
1216 return type_size (tp->d.alias);
1217
1218 case ty_pointer:
1219 case ty_ref:
1220
1221 /* Pointers and references are 32-bit values. */
1222
1223 return 4;
1224
1225 case ty_struc:
1226
1227 /* The size of a structure is stored in the type structure. */
1228
1229 if ( tp->d.struc.flags & STRUC_FORWARD
1230 && try_complete_struct(tp))
1231 {
1232 no_warning ("size of incomplete structure %s is unknown (off %ld)\n stabs: %s",
1233 tp->d.struc.name, parse_ptr - parse_start, parse_start);
1234 return 0;
1235 }
1236 return tp->d.struc.size;
1237
1238 case ty_class:
1239
1240 /* The size of a class is stored in the type structure. */
1241
1242 return tp->d.class.size;
1243
1244 case ty_enu:
1245
1246 /* Return the size of the base type for an enumeration type. */
1247
1248 return type_size (tp->d.enu.type);
1249
1250 case ty_array:
1251
1252 /* Multiply the element size with the number of array elements
1253 for arrays. */
1254
1255 return ((tp->d.array.last + 1 - tp->d.array.first) *
1256 type_size (tp->d.array.etype));
1257
1258 case ty_bits:
1259
1260 /* Use the size of the base type for bitfields. */
1261
1262 return type_size (tp->d.bits.type);
1263
1264 case ty_member:
1265
1266 /* Use the size of the member's type for members. */
1267
1268 return type_size (tp->d.member.type);
1269
1270 case ty_baseclass:
1271
1272 /* The size of a base class is the size of the base class :-) */
1273
1274 return type_size (tp->d.baseclass.type);
1275
1276 case ty_func:
1277 case ty_args:
1278 case ty_types:
1279 case ty_fields:
1280 case ty_values:
1281 case ty_memfunc:
1282
1283 /* This cannot not happen. */
1284
1285 error ("type_size: cannot compute size for tag %d", tp->tag);
1286
1287 default:
1288
1289 /* This cannot happen. */
1290
1291 error ("type_size: unknown type: %d", tp->tag);
1292 }
1293}
1294
1295
1296/* Turn a struct into a class. This needs to be done when a struct is
1297 referenced as base class (unfortunately, a simple class looks like
1298 a struct). */
1299
1300static void struct_to_class (struct type *tp)
1301{
1302 struct type t, *t1, **list, **types;
1303 struct field *fields;
1304 int i, n;
1305
1306 if (tp->index != -1)
1307 {
1308 warning ("Base class cannot be converted from struct");
1309 return;
1310 }
1311 n = tp->d.struc.count;
1312 if (n == 0)
1313 list = NULL;
1314 else
1315 {
1316 if (tp->d.struc.fields->tag != ty_fields
1317 || tp->d.struc.fields->d.fields.count != n)
1318 {
1319 warning ("Invalid struct field list");
1320 return;
1321 }
1322 fields = tp->d.struc.fields->d.fields.list;
1323 if (tp->d.struc.types->tag != ty_types
1324 || tp->d.struc.types->d.types.count != n)
1325 {
1326 warning ("Invalid struct types list");
1327 return;
1328 }
1329 types = tp->d.struc.types->d.types.list;
1330 list = xmalloc (n * sizeof (*list));
1331 for (i = 0; i < n; ++i)
1332 {
1333 t.tag = ty_member;
1334 t.index = -1;
1335 t.d.member.type = types[i];
1336 t.d.member.offset = fields[i].offset;
1337 t.d.member.flags = VIS_PUBLIC;
1338 t.d.member.name = fields[i].name;
1339 t.d.member.sname = NULL;
1340 list[i] = type_add (&t);
1341 }
1342 }
1343
1344 t.tag = ty_types;
1345 t.index = -1;
1346 t.d.types.count = n;
1347 t.d.types.list = list;
1348 t1 = type_add (&t);
1349 free (list);
1350
1351 t = *tp;
1352 tp->tag = ty_class;
1353 tp->d.class.members = t1;
1354 tp->d.class.size = t.d.struc.size;
1355 tp->d.class.count = t.d.struc.count;
1356 tp->d.class.name = t.d.struc.name;
1357}
1358
1359
1360/* Build and return the internal representation of the `long long'
1361 type. As IPMD doesn't know about `long long' we use the following
1362 structure instead:
1363
1364 struct _long_long
1365 {
1366 unsigned long lo, hi;
1367 };
1368
1369 This function could be optimized by remembering the type in a
1370 global variable. */
1371
1372static struct type *make_long_long (void)
1373{
1374 struct type t, *t1, *t2, *t3, *types[2];
1375 struct field fields[2];
1376
1377 /* Let t1 be `unsigned long'. */
1378
1379 t.tag = ty_prim;
1380 t.index = 0x86; /* 32 bit unsigned */
1381 t1 = type_add (&t);
1382
1383 /* Let t2 be the field types: unsigned long, unsigned long. */
1384
1385 types[0] = t1;
1386 types[1] = t1;
1387 t.tag = ty_types;
1388 t.index = -1;
1389 t.d.types.count = 2;
1390 t.d.types.list = types;
1391 t2 = type_add (&t);
1392
1393 /* Let t3 be the fields: lo (at 0), hi (at 4). */
1394
1395 fields[0].offset = 0; fields[0].name = strpool_add (str_pool, "lo");
1396 fields[1].offset = 4; fields[1].name = strpool_add (str_pool, "hi");
1397 t.tag = ty_fields;
1398 t.index = -1;
1399 t.d.fields.count = 2;
1400 t.d.fields.list = fields;
1401 t3 = type_add (&t);
1402
1403 /* Build the structure type from t1, t2 and t3. */
1404
1405 t.tag = ty_struc;
1406 t.d.struc.count = 2;
1407 t.d.struc.size = 8;
1408 t.d.struc.types = t2;
1409 t.d.struc.fields = t3;
1410 t.d.struc.name = strpool_add (str_pool, "_long_long");
1411 t.d.struc.flags = 0;
1412 return type_add (&t);
1413}
1414
1415
1416/* Parse a (signed) long long number in a stabs type. The number is
1417 given in octal or decimal notation. The result is stored to
1418 *NUMBER. This function returns TRUE iff successful. We don't
1419 check for overflow. */
1420
1421static int parse_long_long (long long *number)
1422{
1423 long long n;
1424 int neg, base;
1425
1426 n = 0; neg = FALSE; base = 10;
1427 if (*parse_ptr == '-')
1428 {
1429 neg = TRUE;
1430 ++parse_ptr;
1431 }
1432 if (*parse_ptr == '0')
1433 base = 8;
1434 if (!(*parse_ptr >= '0' && *parse_ptr < base + '0'))
1435 {
1436 *number = 0;
1437 return FALSE;
1438 }
1439 while (*parse_ptr >= '0' && *parse_ptr < base + '0')
1440 {
1441 n = n * base + (*parse_ptr - '0');
1442 ++parse_ptr;
1443 }
1444 *number = (neg ? -n : n);
1445 return TRUE;
1446}
1447
1448
1449/* Parse a (signed) number in a stabs type. The number is given in
1450 octal or decimal notation. The result is stored to *NUMBER. This
1451 function returns TRUE iff successful. We don't check for
1452 overflow. */
1453
1454static int parse_number (long *number)
1455{
1456 long long n;
1457 int result;
1458
1459 result = parse_long_long (&n);
1460 *number = (long)n;
1461 return result;
1462}
1463
1464
1465/* Check the next character in a stabs type for C. If the next
1466 character matches C, the character is skipped and TRUE is
1467 returned. Otherwise, a warning message is displayed and FALSE is
1468 returned. */
1469
1470static int parse_char (char c)
1471{
1472 if (*parse_ptr != c)
1473 {
1474 no_warning ("Invalid symbol data: `%c' expected, found '%c' (off %ld)\n stabs: %s",
1475 c, *parse_ptr, parse_ptr - parse_start, parse_start);
1476 return FALSE;
1477 }
1478 ++parse_ptr;
1479 return TRUE;
1480}
1481
1482
1483/* Parse mangled arguments of a member functions. For instance, ic
1484 means an `int' argument and a `char' argument. Return FALSE on
1485 failure. */
1486
1487static int parse_mangled_args (void)
1488{
1489 while (*parse_ptr != ';')
1490 {
1491 switch (*parse_ptr)
1492 {
1493 case 0:
1494 warning ("Missing semicolon after mangled arguments");
1495 return FALSE;
1496 default:
1497 /* TODO */
1498 break;
1499 }
1500 ++parse_ptr;
1501 }
1502 ++parse_ptr; /* Skip semicolon */
1503 return TRUE;
1504}
1505
1506
1507/* Parse the visibility of a class or member. */
1508
1509static int parse_visibility (void)
1510{
1511 switch (*parse_ptr)
1512 {
1513 case '0':
1514 ++parse_ptr;
1515 return VIS_PRIVATE;
1516 case '1':
1517 ++parse_ptr;
1518 return VIS_PROTECTED;
1519 case '2':
1520 ++parse_ptr;
1521 return VIS_PUBLIC;
1522 default:
1523 ++parse_ptr;
1524 no_warning ("Invalid visibility: %c", *parse_ptr);
1525 if (*parse_ptr != 0)
1526 ++parse_ptr;
1527 return VIS_PUBLIC;
1528 }
1529}
1530
1531
1532/* Add TYPE_NAME to `str_pool', replacing a NULL string with a unique
1533 string. */
1534
1535static const char *add_struct_name (const char *type_name)
1536{
1537 char tmp[32];
1538
1539 /* Some names have only spaces for gdb safety, let's make an
1540 fake name for those. */
1541 if (type_name != NULL)
1542 {
1543 const char *psz = type_name;
1544 while (*psz == ' ' || *psz == '\t')
1545 psz++;
1546 if (!*psz)
1547 type_name = NULL;
1548 }
1549
1550 if (type_name == NULL)
1551 {
1552 sprintf (tmp, "__%d", unnamed_struct_number++);
1553 type_name = tmp;
1554 }
1555 return strpool_add (str_pool, type_name);
1556}
1557
1558/**
1559 * Finds the next colon in the string and returns pointer to it.
1560 * For C++ template support, colons within <> are not considered.
1561 *
1562 * @returns Pointer to next colon within psz.
1563 * @returns NULL if not found.
1564 * @param pszString String to search.
1565 * @param chType Type we're parsing. Use '\0' if unknown or it
1566 * should be ignored
1567 * @remark Still a bad hack this.
1568 */
1569#ifdef COLON_DEBUG
1570static const char *nextcolon2(const char *pszString, char chType)
1571{
1572 static const char *_nextcolon2(const char *pszString, char chType);
1573 const char *psz = _nextcolon2(pszString, chType);
1574 fprintf(stderr, "COLON: %.100s\n%.32s\n", pszString, psz);
1575 return psz;
1576}
1577static const char *_nextcolon2(const char *pszString, char chType)
1578#else
1579static const char *nextcolon2(const char *pszString, char chType)
1580#endif
1581{
1582 const char *pszColon;
1583 const char *psz;
1584
1585 /* permanent operator hack */
1586 if ( pszString[0] == 'o'
1587 && pszString[1] == 'p'
1588 && pszString[2] == 'e'
1589 && pszString[3] == 'r'
1590 && pszString[4] == 'a'
1591 && pszString[5] == 't'
1592 && pszString[6] == 'o'
1593 && pszString[7] == 'r')
1594 {
1595 psz = pszString + 8;
1596 while (*psz && !isalnum(*psz) && *psz != '_') /* potential operator chars */
1597 {
1598 if (*psz == ':')
1599 {
1600 if ( psz - pszString >= 1 + 8
1601 && psz - pszString <= 3 + 8)
1602 return psz;
1603 break;
1604 }
1605 psz++;
1606 }
1607
1608 }
1609
1610 /* normal life */
1611 pszColon = strchr(pszString, ':');
1612 if (!pszColon)
1613 return NULL;
1614 psz = strchr(pszString, '<');
1615
1616 if (psz && psz < pszColon && pszColon[1] == ':')
1617 {
1618 int cNesting = 0; /* <> level */
1619
1620 for (;;psz++)
1621 {
1622 switch (*psz)
1623 {
1624 case '<': cNesting++; break;
1625 case '>': cNesting--; break;
1626 case ':':
1627 if (cNesting > 0)
1628 break;
1629 /* hack: continue on ">::" (but not on "operator...") */
1630 if ( chType != 'x'
1631 && psz[1] == ':'
1632 && psz > pszString && psz[-1] == '>'
1633 )
1634 {
1635 psz++;
1636 break;
1637 }
1638 return psz;
1639 case '\0':
1640 return NULL;
1641 }
1642 }
1643 }
1644 else
1645 {
1646 if (chType != 'x')
1647 {
1648 /* skip '::' (hack:) if not followed by number. */
1649 while (*pszColon && pszColon[1] == ':' && (pszColon[2] > '9' || pszColon[2] < '0'))
1650 {
1651 pszColon += 2;
1652 while (*pszColon != ':' && *pszColon)
1653 pszColon++;
1654 }
1655 }
1656 }
1657
1658 return pszColon;
1659}
1660
1661/* old version */
1662static inline const char *nextcolon(const char *pszString)
1663{
1664 return nextcolon2(pszString, '\0');
1665}
1666
1667
1668
1669/* Parse a stabs type (which is named TYPE_NAME; TYPE_NAME is NULL for
1670 an unnamed type) and return the internal representation of that
1671 type. */
1672
1673static struct type *parse_type (const char *type_name)
1674{
1675 const char *saved_ptr, *p1;
1676 char ch;
1677 struct type t, *result, *t1, *t2, *t3, **tlist;
1678 int i, n, class_flag, is_void;
1679 long num1, num2, size, lo, hi, valid, offset, width, code;
1680 long long range_lo, range_hi;
1681 struct tmp_field *tmp_fields, *tf;
1682 struct value *tmp_values, *tv;
1683 struct grow g1 = GROW_INIT;
1684 enum type_tag tt;
1685
1686 valid = TRUE; result = NULL;
1687 t.index = -1;
1688 switch (*parse_ptr)
1689 {
1690 case '0': case '1': case '2': case '3': case '4':
1691 case '5': case '6': case '7': case '8': case '9':
1692 if (!parse_number (&num1))
1693 goto syntax;
1694
1695 /* Special case: self reference => void */
1696 is_void = FALSE;
1697 if (*parse_ptr == '=' && parse_ptr[1] >= '0' && parse_ptr[1] <= '9')
1698 {
1699 saved_ptr = parse_ptr++;
1700 if (!parse_number (&num2))
1701 goto syntax;
1702 if (num2 == num1)
1703 is_void = TRUE;
1704 else
1705 parse_ptr = saved_ptr;
1706 }
1707 if (is_void)
1708 {
1709 t.tag = ty_prim;
1710 t.index = 0x97; /* void */
1711 result = type_add (&t);
1712 stype_add (num1, result);
1713 }
1714 else if (*parse_ptr == '=')
1715 {
1716 /* Nested definition */
1717 ++parse_ptr;
1718 result = parse_type (type_name);
1719 stype_add (num1, result);
1720 }
1721 else
1722 {
1723 result = stype_find (num1);
1724 if (result == NULL)
1725 {
1726 t.tag = ty_stabs_ref;
1727 t.d.stabs_ref = num1;
1728 }
1729 }
1730 break;
1731
1732 case 'r':
1733
1734 /* A range type: r<base_type>;<lower_bound>;<upper_bound>;
1735
1736 Special cases:
1737
1738 lower_bound | upper_bound | type
1739 ------------+-------------+------------------------
1740 n > 0 | 0 | floating point, n bytes
1741
1742 */
1743
1744 ++parse_ptr;
1745 if (!parse_number (&num1) || !parse_char (';')
1746 || !parse_long_long (&range_lo) || !parse_char (';')
1747 || !parse_long_long (&range_hi) || !parse_char (';'))
1748 goto syntax;
1749 t.tag = ty_prim;
1750 if (range_lo == -128 && range_hi == 127)
1751 t.index = 0x80; /* 8 bit signed */
1752 else if (range_lo == -32768 && range_hi == 32767)
1753 t.index = 0x81; /* 16 bit signed */
1754 else if (range_lo == -2147483647 - 1 && range_hi == 2147483647)
1755 t.index = 0x82; /* 32 bit signed */
1756 else if (range_lo == 0 && range_hi == 127)
1757 t.index = 0x84; /* 8 bit character */
1758 else if (range_lo == 0 && range_hi == 255)
1759 t.index = 0x84; /* 8 bit unsigned */
1760 else if (range_lo == 0 && range_hi == 65535)
1761 t.index = 0x85; /* 16 bit unsigned */
1762 else if (range_lo == 0 && range_hi == 0xffffffff)
1763 t.index = 0x86; /* 32 bit unsigned */
1764 else if (range_lo == 0 && range_hi == -1)
1765 {
1766 if (type_name != NULL
1767 && (strcmp (type_name, "long long unsigned int") == 0
1768 || strcmp (type_name, "long long int") == 0))
1769 result = make_long_long ();
1770 else
1771 t.index = 0x86; /* 32 bit unsigned */
1772 }
1773 else if ((unsigned long long)range_lo == 0x8000000000000000ULL
1774 && range_hi == 0x7fffffffffffffffLL)
1775 result = make_long_long ();
1776 else if (range_lo == 4 && range_hi == 0)
1777 t.index = 0x88; /* 32 bit real */
1778 else if (range_lo == 8 && range_hi == 0)
1779 t.index = 0x89; /* 64 bit real */
1780 else if (range_lo == 12 && range_hi == 0)
1781 t.index = 0x8a; /* 80 bit real */
1782 else
1783 {
1784 no_warning ("Unknown range type: %lld..%lld", range_lo, range_hi);
1785 goto syntax;
1786 }
1787 break;
1788
1789 case 'R':
1790
1791 /* Complex number: R<?base_type?>;<size bytes>;<0>; */
1792
1793 ++parse_ptr;
1794 if (!parse_number (&num1) || !parse_char (';')
1795 || !parse_number (&width) || !parse_char (';')
1796 || !parse_number (&code) || !parse_char (';'))
1797 goto syntax;
1798
1799 /*
1800 * The 192bit type is not defined in the HLL specs, but we assume it for
1801 * convenince right now.
1802 *
1803 * It seems like no debugger acutally supports these types. So, we should,
1804 * when somebody requires it for debugging complex math, make structs out
1805 * of these declarations like GCC does for the 'complex int' type.
1806 *
1807 * .stabs "FakeComplexInt:t21=22=s8real:1,0,32;imag:1,32,32;;",128,0,0,0
1808 * .stabs "FakeComplexFloat:t23=24=s8real:12,0,32;imag:12,32,32;;",128,0,0,0
1809 * .stabs "FakeComplexDouble:t25=26=s16real:13,0,64;imag:13,64,64;;",128,0,0,0
1810 * .stabs "FakeComplexLongDouble:t27=28=s24real:14,0,96;imag:14,96,96;;",128,0,0,0
1811 *
1812 */
1813 t.tag = ty_prim;
1814 if (width == 8 && code == 0)
1815 t.index = 0x8c;
1816 else if (width == 16 && code == 0)
1817 t.index = 0x8d ;
1818 else if (width == 20 && code == 0)
1819 t.index = 0x8e;
1820 else if (width == 24 && code == 0)
1821 t.index = 0x8f; //TODO: bogus!!!
1822 else
1823 {
1824 no_warning ("Unknown complex type: %ld/%ld", width, code);
1825 goto syntax;
1826 }
1827 break;
1828
1829 case '*':
1830
1831 /* A pointer type: *<type> */
1832
1833 ++parse_ptr;
1834 t1 = parse_type (NULL);
1835 t.tag = ty_pointer;
1836 t.d.pointer = t1;
1837 break;
1838
1839 case 'k':
1840
1841 /* A const type: k<type>
1842 HLL don't have const typedefs afaik, so we just make it an alias. */
1843
1844 ++parse_ptr;
1845 t1 = parse_type (NULL);
1846 t.tag = ty_alias;
1847 t.d.alias = t1;
1848 break;
1849
1850 case 'B':
1851
1852 /* A volatile type: B<type>
1853 This is a SUN extension, and right now we don't care to generate
1854 specific HLL for it and stick with an alias. */
1855
1856 ++parse_ptr;
1857 t1 = parse_type (NULL);
1858 t.tag = ty_alias;
1859 t.d.alias = t1;
1860 break;
1861
1862
1863 case '&':
1864
1865 /* A reference type (C++): &<type> */
1866
1867 ++parse_ptr;
1868 t1 = parse_type (NULL);
1869 t.tag = ty_ref;
1870 t.d.ref = t1;
1871 break;
1872
1873 case '#':
1874
1875 /* A member function:
1876
1877 short format: ##<return_type>;
1878
1879 long format: #<domain_type>,<return_type>{,<arg_type>};
1880
1881 */
1882
1883 ++parse_ptr;
1884 args_grow.count = 0; /* ASSUMES we can safely use args_grow/args_list. */
1885 t2 = NULL; /* t2=Domain type; t1=Return type; */
1886 if (*parse_ptr == '#')
1887 {
1888 ++parse_ptr;
1889 t1 = parse_type (NULL);
1890 if (!parse_char (';'))
1891 goto syntax;
1892 no_warning ("We don't understand '##' methods.");
1893 }
1894 else
1895 {
1896 t2 = parse_type (NULL); /* Domain type */
1897 if (!parse_char (','))
1898 goto syntax;
1899 t1 = parse_type (NULL); /* Return type */
1900 /* Arguments */
1901 while (*parse_ptr != ';')
1902 {
1903 if (!parse_char (','))
1904 goto syntax;
1905 grow_by (&args_grow, 1);
1906 args_list[args_grow.count++] = parse_type (NULL); /* Argument type */
1907 }
1908 ++parse_ptr;
1909 }
1910
1911 t3 = NULL;
1912 #if 0 /* this doesn't really seems to be required, but can just as well leave it in. */
1913 if (args_grow.count)
1914 {
1915 t.tag = ty_args;
1916 t.index = -1;
1917 t.d.args.count = args_grow.count;
1918 t.d.args.list = xmalloc (args_grow.count * sizeof (*args_list));
1919 memcpy (t.d.args.list, args_list, args_grow.count * sizeof (*args_list));
1920 t3 = type_add (&t);
1921 free (t.d.args.list);
1922 }
1923 #endif
1924
1925 /* Make a function type from the return type t1 */
1926 t.tag = ty_func;
1927 t.d.func.ret = t1;
1928 t.d.func.domain = t2;
1929 t.d.func.args = t3;
1930 t.d.func.arg_count = args_grow.count;
1931 args_grow.count = 0;
1932 t1 = type_add(&t);
1933
1934 break;
1935
1936 case '@':
1937
1938 /* Special GNU-defined types:
1939
1940 @s<bits>;<code>;
1941
1942 BITS: Number of bits
1943 CODE: Identifies the type:
1944 -16 bool
1945 -20 char
1946
1947 @s<bits>;<range record>
1948 Used for 64 bit ints.
1949
1950 @s<bits>;<num records>
1951 Used for non-standard enums.
1952
1953 @<basetype>,<membertype>
1954 Used for addressing class/struct/union members.
1955 */
1956
1957 ++parse_ptr;
1958 switch (*parse_ptr)
1959 {
1960 case 's':
1961 ++parse_ptr;
1962 if (!parse_number (&size) || !parse_char (';'))
1963 goto syntax;
1964 if (*parse_ptr == 'r')
1965 { /* nested */
1966 return parse_type(type_name);
1967 }
1968 if (*parse_ptr == 'e')
1969 goto l_parse_enum;
1970
1971 if (!parse_number (&code))
1972 goto syntax;
1973 if (size == 8 && code == -16)
1974 {
1975#if 0 /* debugger doesn't understand this game */
1976 t.tag = ty_prim;
1977 t.index = 0x90; /* 8 bit boolean */
1978#else
1979 t.tag = ty_values;
1980 t.d.values.count = 2;
1981 t.d.values.list = xmalloc (2 * sizeof (*t.d.values.list));
1982 t.d.values.list[0].index = 0;
1983 t.d.values.list[0].name = strpool_add (str_pool, "false");
1984 t.d.values.list[1].index = 1;
1985 t.d.values.list[1].name = strpool_add (str_pool, "true");
1986 t1 = type_add (&t);
1987 free (t.d.values.list);
1988
1989 t.tag = ty_prim;
1990 t.index = 0x80;
1991 t2 = type_add (&t);
1992
1993 t.tag = ty_enu;
1994 t.index = -1;
1995 t.d.enu.values = t1;
1996 t.d.enu.type = t2;
1997 t.d.enu.name = add_struct_name (NULL);
1998 t.d.enu.first = 0;
1999 t.d.enu.last = 1;
2000#endif
2001 }
2002 else
2003 {
2004 warning ("Unknown GNU extension type code: %ld, %ld bits",
2005 code, size);
2006 goto syntax;
2007 }
2008 if (!parse_char (';'))
2009 goto syntax;
2010 break;
2011
2012 case '0': case '1': case '2': case '3': case '4':
2013 case '5': case '6': case '7': case '8': case '9':
2014 {
2015 /* just make it an integer - see dbxout.c OFFSET_TYPE case. */
2016 if ( parse_number (&num1)
2017 && *parse_ptr == ','
2018 && ++parse_ptr
2019 && parse_number (&num2)
2020 )
2021 {
2022 t.tag = ty_prim;
2023 t.index = 0x82; /* 32 bit signed */
2024 }
2025 else
2026 {
2027 warning_parse ("Invalid BASE_OFFSET GNU extension format!");
2028 goto syntax;
2029 }
2030 break;
2031 }
2032
2033 default:
2034 warning ("Unknown GNU extension type: @%c", *parse_ptr);
2035 goto syntax;
2036 }
2037 break;
2038
2039 case 'x':
2040
2041 /* Incomplete type: xs<name>:
2042 xu<name>:
2043 xe<name>: */
2044
2045 ++parse_ptr;
2046 ch = *parse_ptr++;
2047 p1 = nextcolon2 (parse_ptr, 'x');
2048 if (p1 == NULL)
2049 {
2050 warning ("Invalid forward reference: missing colon");
2051 goto syntax;
2052 }
2053
2054 switch (ch)
2055 {
2056 case 's':
2057 case 'u':
2058 {
2059 /* make a fake structure. */
2060 t.tag = ty_prim;
2061 t.index = 0x80;
2062 tlist = xmalloc (1 * sizeof (*t.d.types.list));
2063 tlist[0] = type_add (&t);
2064 t.tag = ty_types;
2065 t.index = -1;
2066 t.d.types.count = 1;
2067 t.d.types.list = tlist;
2068 t1 = type_add (&t);
2069 free (tlist);
2070
2071 t.tag = ty_fields;
2072 t.index = -1;
2073 t.d.fields.count = 1;
2074 t.d.fields.list = xmalloc (1 * sizeof (*t.d.fields.list));
2075 t.d.fields.list[0].offset = 0;
2076 t.d.fields.list[0].name = strpool_add (str_pool, "This_Is_an_Incomplete_Structure_SORRY");
2077 t2 = type_add (&t);
2078 free (t.d.fields.list);
2079
2080 t.tag = ty_struc;
2081 t.index = -1;
2082 t.d.struc.types = t1;
2083 t.d.struc.fields = t2;
2084 t.d.struc.size = 1;
2085 t.d.struc.count = 1;
2086 t.d.struc.name = strpool_addn (str_pool, parse_ptr, p1 - parse_ptr);
2087 t.d.struc.flags = STRUC_FORWARD;
2088
2089 /* Avoid adding an incomplete type if the complete one is
2090 already defined. We're ignoring scoping (TODO). */
2091
2092 for (t3 = type_head; t3 != NULL; t3 = t3->next)
2093 if ((t3->tag == ty_struc && t3->d.struc.name == t.d.struc.name
2094 && !(t3->d.struc.flags & STRUC_FORWARD))
2095 || (t3->tag == ty_class && t3->d.class.name == t.d.struc.name))
2096 {
2097 result = t3;
2098 break;
2099 }
2100 break;
2101 }
2102
2103 case 'e':
2104 { /* just make it a primary. */
2105 /** @todo make an enum perhaps? */
2106 t.tag = ty_prim;
2107 t.index = 0x82; /* 32 bit signed */
2108 break;
2109 }
2110
2111 default:
2112 warning ("Unknown forward reference: %c", ch);
2113 parse_ptr = p1 + 1;
2114 goto syntax;
2115 }
2116 parse_ptr = p1 + 1;
2117 break;
2118
2119 case 's': /* struct, class */
2120 case 'u': /* union */
2121
2122 /* Structures, unions and classes:
2123
2124 s<size>[<baseclasses>]{<field>}{<method>}
2125
2126 BASECLASSES: !<#baseclasses>,{<baseclass>;}
2127 BASECLASS: <virtuality><visibility><offset>,<type>
2128 FIELD: <name>:[/0|1|2]<type>,<offset>,<width>;
2129 FIELD: <name>:[/0|1|2]<type>:<static_name>;
2130 METHOD: <name>::{<type>:<args>};
2131
2132 */
2133
2134 ++parse_ptr;
2135 class_flag = FALSE;
2136 if (!parse_number (&size))
2137 goto syntax;
2138 grow_init (&g1, &tmp_fields, sizeof (*tmp_fields), 8);
2139
2140 /* Parse the base classes, if present. */
2141
2142 if (*parse_ptr == '!')
2143 {
2144 long i, n;
2145
2146 ++parse_ptr;
2147 class_flag = TRUE;
2148 if (!parse_number (&n) || !parse_char (','))
2149 goto syntax;
2150 for (i = 0; i < n; ++i)
2151 {
2152 grow_by (&g1, 1);
2153 tf = &tmp_fields[g1.count++];
2154 tf->flags = MEMBER_BASE;
2155 tf->name = NULL;
2156 tf->sname = NULL;
2157 switch (*parse_ptr)
2158 {
2159 case '0':
2160 ++parse_ptr;
2161 break;
2162 case '1':
2163 tf->flags |= MEMBER_VIRTUAL;
2164 ++parse_ptr;
2165 break;
2166 default:
2167 warning ("Invalid base class visibility: %c", *parse_ptr);
2168 if (*parse_ptr != 0)
2169 ++parse_ptr;
2170 }
2171
2172 tf->flags |= parse_visibility ();
2173 if (!parse_number (&offset) || !parse_char (','))
2174 goto syntax;
2175 tf->offset = offset >> 3;
2176 t1 = parse_type (NULL);
2177 switch (t1->tag)
2178 {
2179 case ty_class:
2180 break;
2181 case ty_struc:
2182 /* kso #456 2003-06-11: Don't mess with forward defines! */
2183 if (!( (t1->d.struc.flags & STRUC_FORWARD)
2184 /*&& t1->d.struc.types == NULL
2185 && t1->d.struc.fields == NULL*/))
2186 struct_to_class (t1);
2187 break;
2188 default:
2189 warning ("Invalid base class type");
2190 }
2191 tf->type = t1;
2192 if (!parse_char (';'))
2193 goto syntax;
2194 }
2195 }
2196
2197 while (*parse_ptr != ';')
2198 {
2199 p1 = nextcolon (parse_ptr);
2200 if (p1 == NULL)
2201 {
2202 warning ("Invalid structure type: missing colon");
2203 goto syntax;
2204 }
2205 n = p1 - parse_ptr;
2206 grow_by (&g1, 1);
2207 tf = &tmp_fields[g1.count++];
2208 tf->flags = VIS_PUBLIC;
2209 tf->name = strpool_addn (str_pool, parse_ptr, n);
2210 tf->mnglname = NULL;
2211 parse_ptr = p1 + 1;
2212 if (*parse_ptr == ':')
2213 {
2214 class_flag = TRUE;
2215 ++parse_ptr;
2216 do
2217 {
2218 const char * pszMangled;
2219
2220 t1 = parse_type (NULL);
2221 offset = 0;
2222 if (t1 == NULL || t1->tag != ty_func)
2223 {
2224 no_warning ("Invalid member function type");
2225 goto syntax;
2226 }
2227
2228 if (*parse_ptr != ':')
2229 {
2230 no_warning ("Arguments for member function missing");
2231 goto syntax;
2232 }
2233 ++parse_ptr;
2234 pszMangled = parse_ptr;
2235 if (!parse_mangled_args ())
2236 goto syntax;
2237 tf->mnglname = strpool_addn(str_pool, pszMangled, parse_ptr - pszMangled - 1); /* ASSUMES no extra spaces! */
2238 tf->flags = parse_visibility () | MEMBER_FUNCTION;
2239 switch (*parse_ptr)
2240 {
2241 case 'A':
2242 ++parse_ptr;
2243 break;
2244 case 'B':
2245 tf->flags |= MEMBER_CONST;
2246 ++parse_ptr;
2247 break;
2248 case 'C':
2249 tf->flags |= MEMBER_VOLATILE;
2250 ++parse_ptr;
2251 break;
2252 case 'D':
2253 tf->flags |= MEMBER_CONST | MEMBER_VOLATILE;
2254 ++parse_ptr;
2255 break;
2256 default:
2257 no_warning ("Unknown member function qualifier: %c",
2258 *parse_ptr);
2259 if (*parse_ptr != 0)
2260 ++parse_ptr;
2261 }
2262 switch (*parse_ptr)
2263 {
2264 case '.':
2265 ++parse_ptr;
2266 break;
2267 case '*':
2268 tf->flags |= MEMBER_VIRTUAL;
2269 ++parse_ptr;
2270 if (!parse_number (&offset) || !parse_char (';'))
2271 {
2272 warning ("Invalid data for virtual member function");
2273 goto syntax;
2274 }
2275 parse_type (NULL);
2276 if (!parse_char (';'))
2277 {
2278 warning ("Invalid data for virtual member function");
2279 goto syntax;
2280 }
2281 break;
2282 case '?':
2283 tf->flags |= MEMBER_STATIC;
2284 ++parse_ptr;
2285 break;
2286 default:
2287 no_warning ("Unknown member function qualifier: %c",
2288 *parse_ptr);
2289 if (*parse_ptr != 0)
2290 ++parse_ptr;
2291 }
2292 /* Contstructor / Destructor hack -
2293 * TODO: verify that this doesn't really work with overloaded constructors and fix it.
2294 */
2295 if (!(tf->flags & MEMBER_STATIC))
2296 {
2297 /* using the strpool is much faster! */
2298 static const char *pszCompCtor, *pszBaseCtor, *pszCompDtor, *pszBaseDtor = NULL;
2299 if (!pszBaseDtor)
2300 {
2301 pszCompCtor = strpool_addn(str_pool, "__comp_ctor", 11);
2302 pszBaseCtor = strpool_addn(str_pool, "__base_ctor", 11);
2303 pszCompDtor = strpool_addn(str_pool, "__comp_dtor", 11);
2304 pszBaseDtor = strpool_addn(str_pool, "__base_dtor", 11);
2305 }
2306
2307 if (tf->name == pszCompCtor || tf->name == pszBaseCtor)
2308 tf->flags |= MEMBER_CTOR;
2309 else if (tf->name == pszCompDtor || tf->name == pszBaseDtor)
2310 tf->flags |= MEMBER_DTOR;
2311 }
2312 tf->type = t1;
2313 tf->offset = offset;
2314 if (*parse_ptr != ';')
2315 {
2316 /* Another member function with the same name
2317 follows. */
2318
2319 grow_by (&g1, 1);
2320 tf = &tmp_fields[g1.count++];
2321 tf->flags = VIS_PUBLIC | MEMBER_FUNCTION;
2322 tf->name = tf[-1].name;
2323 }
2324 } while (*parse_ptr != ';');
2325 ++parse_ptr;
2326 }
2327 else
2328 {
2329 if (*parse_ptr == '/')
2330 {
2331 class_flag = TRUE;
2332 ++parse_ptr;
2333 tf->flags = parse_visibility ();
2334 }
2335 t1 = parse_type (NULL);
2336 if (*parse_ptr == ':')
2337 {
2338 /* Static member */
2339
2340 ++parse_ptr;
2341 tf->flags |= MEMBER_STATIC;
2342 p1 = strchr (parse_ptr, ';');
2343 if (p1 == NULL)
2344 {
2345 warning ("Invalid static member: missing semicolon");
2346 goto syntax;
2347 }
2348 n = p1 - parse_ptr;
2349 tf->sname = strpool_addn (str_pool, parse_ptr, n);
2350 parse_ptr = p1 + 1;
2351 tf->offset = 0;
2352 }
2353 else
2354 {
2355 if (!parse_char (',') || !parse_number (&offset))
2356 goto syntax;
2357 if (*parse_ptr == ',')
2358 {
2359 ++parse_ptr;
2360 if (!parse_number (&width))
2361 goto syntax;
2362 }
2363 else
2364 width = 0; /* vtable pointer field */
2365 if (!parse_char (';'))
2366 goto syntax;
2367 if ((offset & 7) != 0 || width != 8 * type_size (t1))
2368 {
2369 t.tag = ty_bits;
2370 t.d.bits.type = t1;
2371 t.d.bits.start = offset & 7;
2372 t.d.bits.count = width;
2373 t1 = type_add (&t);
2374 }
2375 tf->offset = offset >> 3;
2376 }
2377 tf->type = t1;
2378 }
2379
2380 /* the debuggers croak if the field doesn't have a name. */
2381
2382 if ((!tf->name || !*tf->name)
2383 && (!tf->sname || !*tf->sname)
2384 && (!tf->mnglname || !*tf->mnglname))
2385 tf->name = strpool_add (str_pool, "<anonymous>");
2386 }
2387 ++parse_ptr;
2388 if (*parse_ptr == '~')
2389 {
2390 /* Type reference to the first base class. This field is
2391 present for classes which contain virtual functions. */
2392
2393 ++parse_ptr;
2394 if (!parse_char ('%'))
2395 goto syntax;
2396 t1 = parse_type (NULL);
2397 if (!parse_char (';'))
2398 goto syntax;
2399 }
2400
2401 if (class_flag)
2402 {
2403 tlist = xmalloc (g1.count * sizeof (*t.d.types.list));
2404 for (i = 0; i < g1.count; ++i)
2405 {
2406 if (tmp_fields[i].flags & MEMBER_FUNCTION)
2407 {
2408 t2 = tmp_fields[i].type;
2409 tt = follow_refs (t2)->tag;
2410 /* TODO: check later if ty_stabs_ref */
2411 if (tt != ty_func && tt != ty_stabs_ref)
2412 error ("Invalid type for member function %s",
2413 tmp_fields[i].name);
2414 t.tag = ty_memfunc;
2415 t.d.memfunc.type = t2;
2416 t.d.memfunc.name = tmp_fields[i].name;
2417 t.d.memfunc.mnglname = tmp_fields[i].mnglname;
2418 t.d.memfunc.flags = tmp_fields[i].flags;
2419 t.d.memfunc.offset = tmp_fields[i].offset;
2420 }
2421 else if (tmp_fields[i].flags & MEMBER_BASE)
2422 {
2423 t.tag = ty_baseclass;
2424 t.d.baseclass.type = tmp_fields[i].type;
2425 t.d.baseclass.flags = tmp_fields[i].flags;
2426 t.d.baseclass.offset = tmp_fields[i].offset;
2427 }
2428 else
2429 {
2430 t.tag = ty_member;
2431 t.d.member.type = tmp_fields[i].type;
2432 t.d.member.offset = tmp_fields[i].offset;
2433 t.d.member.flags = tmp_fields[i].flags;
2434 t.d.member.name = tmp_fields[i].name;
2435 t.d.member.sname = tmp_fields[i].sname;
2436 }
2437 t2 = type_add (&t);
2438 tlist[i] = t2;
2439 }
2440 t.tag = ty_types;
2441 t.d.types.count = g1.count;
2442 t.d.types.list = tlist;
2443 t1 = type_add (&t);
2444 free (tlist);
2445
2446 t.tag = ty_class;
2447 t.d.class.members = t1;
2448 t.d.class.size = size;
2449 t.d.class.count = g1.count;
2450 t.d.class.name = add_struct_name (type_name);
2451
2452 /* Check if there is an incomplete type waiting to be filled
2453 in (forward reference). We're ignoring scoping (TODO). */
2454
2455 for (t3 = type_head; t3 != NULL; t3 = t3->next)
2456 if (t3->tag == ty_struc && t3->d.struc.name == t.d.class.name
2457 /*&& t3->d.struc.types == NULL && t3->d.struc.fields == NULL */
2458 && (t3->d.struc.flags & STRUC_FORWARD))
2459 {
2460 if (t3->index != -1)
2461 warning ("This cannot happen, case 1");
2462 result = t3;
2463 t3->tag = ty_class;
2464 t3->d.class = t.d.class;
2465 break;
2466 }
2467 }
2468 else
2469 {
2470 t.tag = ty_types;
2471 t.d.types.count = g1.count;
2472 t.d.types.list = xmalloc (g1.count * sizeof (*t.d.types.list));
2473 for (i = 0; i < g1.count; ++i)
2474 t.d.types.list[i] = tmp_fields[i].type;
2475 t1 = type_add (&t);
2476 free (t.d.types.list);
2477
2478 t.tag = ty_fields;
2479 t.d.fields.count = g1.count;
2480 t.d.fields.list = xmalloc (g1.count * sizeof (*t.d.fields.list));
2481 for (i = 0; i < g1.count; ++i)
2482 {
2483 t.d.fields.list[i].offset = tmp_fields[i].offset;
2484 t.d.fields.list[i].name = tmp_fields[i].name;
2485 }
2486 t2 = type_add (&t);
2487 free (t.d.fields.list);
2488
2489 t.tag = ty_struc;
2490 t.d.struc.count = g1.count;
2491 t.d.struc.size = size;
2492 t.d.struc.types = t1;
2493 t.d.struc.fields = t2;
2494 t.d.struc.name = add_struct_name (type_name);
2495 t.d.struc.flags = 0;
2496
2497 /* Check if there is an incomplete type waiting to be filled
2498 in (forward reference). We're ignoring scoping (TODO). */
2499
2500 for (t3 = type_head; t3 != NULL; t3 = t3->next)
2501 if (t3->tag == ty_struc && t3->d.struc.name == t.d.struc.name
2502 /* && t3->d.struc.types == NULL && t3->d.struc.fields == NULL */
2503 && (t3->d.struc.flags & STRUC_FORWARD))
2504 {
2505 if (t3->index != -1)
2506 warning ("This cannot happen, case 1");
2507 result = t3;
2508 t3->d.struc = t.d.struc;
2509 break;
2510 }
2511 }
2512 grow_free (&g1);
2513 break;
2514
2515 case 'e':
2516 size = 32;
2517l_parse_enum:
2518
2519 /* Enumeration type: e{<name>:<value>,}; */
2520
2521 ++parse_ptr;
2522 grow_init (&g1, &tmp_values, sizeof (*tmp_values), 8);
2523 while (*parse_ptr != ';')
2524 {
2525 p1 = nextcolon (parse_ptr);
2526 if (p1 == NULL)
2527 {
2528 warning ("Invalid enum type: missing colon");
2529 goto syntax;
2530 }
2531 n = p1 - parse_ptr;
2532 grow_by (&g1, 1);
2533 tv = &tmp_values[g1.count++];
2534 tv->name = strpool_addn (str_pool, parse_ptr, n);
2535 parse_ptr = p1 + 1;
2536 if (!parse_number (&tv->index) || !parse_char (','))
2537 goto syntax;
2538 }
2539 ++parse_ptr;
2540 t.tag = ty_values;
2541 t.d.values.count = g1.count;
2542 t.d.values.list = xmalloc (g1.count * sizeof (*t.d.values.list));
2543 if (g1.count == 0)
2544 lo = hi = 0;
2545 else
2546 lo = hi = tmp_values[0].index;
2547 for (i = 0; i < g1.count; ++i)
2548 {
2549 t.d.values.list[i].index = tmp_values[i].index;
2550 t.d.values.list[i].name = tmp_values[i].name;
2551 if (tmp_values[i].index < lo)
2552 lo = tmp_values[i].index;
2553 if (tmp_values[i].index > hi)
2554 hi = tmp_values[i].index;
2555 }
2556 t1 = type_add (&t);
2557 free (t.d.values.list);
2558
2559 t.tag = ty_prim;
2560 switch (size)
2561 {
2562 case 8: t.index = 0x80; break; /* 8 bit signed */
2563 case 16: t.index = 0x81; break; /* 16 bit signed */
2564 case 32: t.index = 0x82; break; /* 32 bit signed */
2565 case 64: result = t2 = make_long_long (); break;
2566 default: goto syntax;
2567 }
2568 if (!result)
2569 t2 = type_add (&t);
2570
2571 t.tag = ty_enu;
2572 t.index = -1;
2573 t.d.enu.values = t1;
2574 t.d.enu.type = t2;
2575 t.d.enu.name = add_struct_name (type_name);
2576 t.d.enu.first = lo;
2577 t.d.enu.last = hi;
2578 grow_free (&g1);
2579 break;
2580
2581 case 'a':
2582 /* An array: ar<index_type>;<lower_bound>;<upper_bound>;<el_type> */
2583
2584 ++parse_ptr;
2585 if (!parse_char ('r'))
2586 goto syntax;
2587 if (strncmp (parse_ptr, "0;", 2) == 0)
2588 {
2589 /* This invalid type index seems to be caused by a bug of
2590 GCC 2.8.1. Replace with a 32-bit signed integer. */
2591
2592 t.tag = ty_prim;
2593 t.index = 0x82; /* 32 bit signed */
2594 t1 = type_add (&t);
2595 ++parse_ptr;
2596 }
2597 else
2598 t1 = parse_type (NULL); /* Index type */
2599 if (!parse_char (';')
2600 || !parse_number (&lo) || !parse_char (';') /* Lower bound */
2601 || !parse_number (&hi) || !parse_char (';')) /* Upper bound */
2602 goto syntax;
2603 t2 = parse_type (NULL); /* Elements type */
2604 if (lo == 0 && hi == 0) /* Variable-length array */
2605 {
2606 t.tag = ty_pointer; /* Turn into pointer */
2607 t.d.pointer = t2;
2608 }
2609 else
2610 {
2611 t.tag = ty_array;
2612 t.d.array.first = lo;
2613 t.d.array.last = hi;
2614 t.d.array.itype = t1;
2615 t.d.array.etype = t2;
2616 }
2617 break;
2618
2619 case 'f':
2620
2621 /* A function: f<return_type> */
2622
2623 ++parse_ptr;
2624 t1 = parse_type (NULL); /* Return type */
2625 t.tag = ty_func;
2626 t.d.func.ret = t1;
2627 t.d.func.domain = NULL;
2628 t.d.func.args = NULL;
2629 t.d.func.arg_count = 0;
2630 break;
2631
2632 default:
2633 no_warning ("Unknown type: %c", *parse_ptr);
2634 goto syntax;
2635 }
2636
2637 /* Add the type to the tables. */
2638
2639 if (result == NULL)
2640 result = type_add (&t);
2641 return result;
2642
2643syntax:
2644 no_warning ("syntax error in stabs: %c (off %ld)\n stabs: %s",
2645 *parse_ptr, parse_ptr - parse_start, parse_start);
2646
2647 grow_free (&g1);
2648 t.tag = ty_prim;
2649 t.index = 0x82; /* 32 bit signed */
2650 return type_add (&t);
2651}
2652
2653
2654/* Parse a stabs type (the name of the type is TYPE_NAME, which is
2655 NULL for an unnamed type) and check for end of the string. If not
2656 all characters of the string have been parsed, a warning message is
2657 displayed. Return the internal representation of the type. */
2658
2659static struct type *parse_complete_type (const char *type_name)
2660{
2661 struct type *tp;
2662
2663 tp = parse_type (type_name);
2664 if (*parse_ptr != 0)
2665 no_warning ("unexpected character at end of stabs type: %c (off %ld)\n stabs: %s",
2666 *parse_ptr, parse_ptr - parse_start, parse_start);
2667 return tp;
2668}
2669
2670
2671/* Create an HLL type number for type TP. If a type number has already
2672 been assigned to TP, use that number. Otherwise, allocate a new
2673 (complex) type number and define the type in the $$TYPES segment.
2674 The HLL type number us stored to *HLL.
2675
2676 Note: This function must not be called between sst_start() and
2677 sst_end() or tt_start() and tt_end() as entries are made both in
2678 the SST ($$SYMBOLS) and the type table ($$TYPES). */
2679
2680static void make_type (struct type *tp, int *hll)
2681{
2682 struct type *t1;
2683 int ti_1, ti_2, ti_3, i, qual;
2684 int patch1;
2685
2686#define RETURN(X) do {*hll = (X); return;} while (0)
2687
2688 if (tp->index <= -2)
2689 {
2690 /* bird: A hack where we allow a 2nd recursion of rectain types.
2691 * The normal issue is C++ class references. */
2692 if (tp->tag != ty_alias || tp->index <= -3)
2693 {
2694 warning_parse ("Cycle detected by make_type.");
2695 RETURN (0);
2696 }
2697 }
2698 if (tp->index > -1)
2699 RETURN (tp->index);
2700 tp->index--;
2701 switch (tp->tag)
2702 {
2703 case ty_stabs_ref:
2704 t1 = stype_find (tp->d.stabs_ref);
2705 if (t1 == NULL)
2706 {
2707 warning_parse ("Undefined stabs type %d referenced", tp->d.stabs_ref);
2708 RETURN (0x82); /* 32 bit signed */
2709 }
2710 make_type (t1, &tp->index);
2711 *hll = tp->index;
2712 break;
2713
2714 case ty_alias:
2715 make_type (tp->d.alias, &tp->index);
2716 *hll = tp->index;
2717 break;
2718
2719 case ty_pointer:
2720 ti_1 = tp->d.pointer->index;
2721 if (ti_1 >= 0x80 && ti_1 <= 0xa0)
2722 {
2723 tp->index = ti_1 + 0x20;
2724 RETURN (tp->index);
2725 }
2726 *hll = tp->index = hll_type_index++;
2727 tt_start (0x7a, 0x01); /* 32-bit near pointer */
2728 patch1 = tt.size;
2729 tt_index (0);
2730 tt_end ();
2731 make_type (tp->d.pointer, &ti_1);
2732 buffer_patch_word (&tt, patch1+1, ti_1);
2733 break;
2734
2735 case ty_ref:
2736 *hll = tp->index = hll_type_index++;
2737 tt_start (0x48, 0x00); /* Reference type (C++) */
2738 patch1 = tt.size;
2739 tt_word (0);
2740 tt_end ();
2741 make_type (tp->d.ref, &ti_1);
2742 buffer_patch_word (&tt, patch1, ti_1);
2743 break;
2744
2745 case ty_struc:
2746 {
2747 struct field *fields = NULL;
2748 struct type **types = NULL;
2749 int cFields = tp->d.struc.count;
2750
2751 if ( tp->d.struc.fields
2752 && tp->d.struc.fields->tag == ty_fields
2753 && tp->d.struc.fields->d.fields.count == cFields)
2754 fields = tp->d.struc.fields->d.fields.list;
2755 if ( tp->d.struc.types
2756 && tp->d.struc.types->tag == ty_types
2757 && tp->d.struc.types->d.types.count == cFields)
2758 types = tp->d.struc.types->d.types.list;
2759
2760 if (cplusplus_flag && ((types && fields) || !cFields))
2761 {
2762 struct type t;
2763 struct type **list;
2764 int i;
2765 *hll = tp->index = hll_type_index++;
2766 tt_start (0x40, 0x01); /* Class is-struct */
2767 tt_dword (tp->d.struc.size);
2768 tt_word (tp->d.struc.count);
2769 patch1 = tt.size;
2770 tt_word (0); /* Members */
2771 tt_enc (tp->d.struc.name); /* Class name */
2772 tt_end ();
2773
2774 list = xmalloc (cFields * sizeof (*list));
2775 for (i = 0; i < cFields; ++i)
2776 {
2777 t.tag = ty_member;
2778 t.index = -1;
2779 t.d.member.type = types[i];
2780 t.d.member.offset = fields[i].offset;
2781 t.d.member.flags = VIS_PUBLIC;
2782 t.d.member.name = fields[i].name;
2783 t.d.member.sname = NULL;
2784 list[i] = type_add (&t);
2785 }
2786
2787 t.tag = ty_types;
2788 t.index = -1;
2789 t.d.types.count = cFields;
2790 t.d.types.list = list;
2791 t1 = type_add (&t);
2792 free (list);
2793
2794 make_type (t1, &ti_1);
2795 buffer_patch_word (&tt, patch1, ti_1);
2796 }
2797 else
2798 {
2799 tt_start (0x79, 0x00); /* Structure/Union */
2800 tt_dword (tp->d.struc.size);
2801 tt_word (tp->d.struc.count);
2802 patch1 = tt.size;
2803 tt_index (0);
2804 tt_index (0);
2805 tt_name (tp->d.struc.name);
2806 tt_end ();
2807 *hll = tp->index = hll_type_index++;
2808 if (tp->d.struc.types == NULL && tp->d.struc.fields == NULL)
2809 {
2810 ti_1 = 0;
2811 ti_2 = 1;
2812 }
2813 else
2814 {
2815 make_type (tp->d.struc.types, &ti_1);
2816 make_type (tp->d.struc.fields, &ti_2);
2817 }
2818 buffer_patch_word (&tt, patch1+1, ti_1);
2819 buffer_patch_word (&tt, patch1+4, ti_2);
2820 }
2821 break;
2822 }
2823
2824 case ty_class:
2825 *hll = tp->index = hll_type_index++;
2826 tt_start (0x40, 0x00); /* Class */
2827 tt_dword (tp->d.class.size);
2828 tt_word (tp->d.class.count);
2829 patch1 = tt.size;
2830 tt_word (0); /* Members */
2831 tt_enc (tp->d.class.name); /* Class name */
2832 tt_end ();
2833 make_type (tp->d.class.members, &ti_1);
2834 buffer_patch_word (&tt, patch1, ti_1);
2835 break;
2836
2837 case ty_member:
2838 qual = 0;
2839 if (tp->d.member.flags & MEMBER_STATIC)
2840 qual |= 0x01;
2841 tt_start (0x46, qual);
2842 tt_byte (tp->d.member.flags & MEMBER_VIS);
2843 patch1 = tt.size;
2844 tt_word (0);
2845 tt_unsigned_span (tp->d.member.offset);
2846 if (tp->d.member.flags & MEMBER_STATIC)
2847 tt_enc (tp->d.member.sname);
2848 else
2849 tt_enc ("");
2850 tt_enc (tp->d.member.name);
2851 tt_end ();
2852 *hll = tp->index = hll_type_index++;
2853
2854 make_type (tp->d.member.type, &ti_1);
2855 buffer_patch_word (&tt, patch1, ti_1);
2856 break;
2857
2858 case ty_enu:
2859 make_type (tp->d.enu.type, &ti_1);
2860 make_type (tp->d.enu.values, &ti_2);
2861 tt_start (0x7b, 0x00); /* Enum */
2862 tt_index (ti_1); /* Element's data type */
2863 tt_index (ti_2); /* List of values */
2864 tt_signed_span (tp->d.enu.first); /* Minimum index */
2865 tt_signed_span (tp->d.enu.last); /* Maximum index */
2866 tt_name (tp->d.enu.name);
2867 tt_end ();
2868 *hll = tp->index = hll_type_index++;
2869 break;
2870
2871 case ty_array:
2872 make_type (tp->d.array.itype, &ti_1);
2873 make_type (tp->d.array.etype, &ti_2);
2874 tt_start (0x78, 0x00); /* Array */
2875 tt_dword (type_size (tp));
2876 tt_index (ti_1);
2877 tt_index (ti_2);
2878 tt_name (""); /* Name */
2879 tt_end ();
2880 *hll = tp->index = hll_type_index++;
2881 break;
2882
2883 case ty_bits:
2884 tt_start (0x5c, 0x0a); /* Bit string */
2885 tt_byte (tp->d.bits.start);
2886 tt_unsigned_span (tp->d.bits.count);
2887 tt_end ();
2888 *hll = tp->index = hll_type_index++;
2889 break;
2890
2891 case ty_func:
2892 tt_start (0x54, 0x05); /* Function */
2893 tt_word (tp->d.func.arg_count); /* Number of parameters */
2894 tt_word (tp->d.func.arg_count); /* Max. Number of parameters */
2895 patch1 = tt.size;
2896 tt_index (0); /* Return value */
2897 tt_index (0); /* Argument list */
2898 tt_end ();
2899 *hll = tp->index = hll_type_index++;
2900
2901 if (tp->d.func.domain)
2902 make_type (tp->d.func.domain, &ti_3);
2903 make_type (tp->d.func.ret, &ti_1);
2904 if (tp->d.func.args == NULL)
2905 ti_2 = 0;
2906 else
2907 make_type (tp->d.func.args, &ti_2);
2908 buffer_patch_word (&tt, patch1+1, ti_1);
2909 buffer_patch_word (&tt, patch1+4, ti_2);
2910 break;
2911
2912 case ty_args:
2913 if (tp->d.args.count == 0)
2914 {
2915 tp->index = 0;
2916 RETURN (tp->index);
2917 }
2918 for (i = 0; i < tp->d.args.count; ++i)
2919 make_type (tp->d.args.list[i], &ti_1);
2920 tt_start (0x7f, 0x04); /* List (function parameters) */
2921 for (i = 0; i < tp->d.args.count; ++i)
2922 {
2923 tt_byte (0x01); /* Flag: pass by value, no descriptor */
2924 tt_index (tp->d.args.list[i]->index);
2925 }
2926 tt_end ();
2927 *hll = tp->index = hll_type_index++;
2928 break;
2929
2930 case ty_types:
2931 if (tp->d.types.count == 0)
2932 {
2933 tp->index = 0;
2934 RETURN (tp->index);
2935 }
2936 *hll = tp->index = hll_type_index++;
2937 tt_start (0x7f, 0x01); /* List (structure field types) */
2938 patch1 = tt.size;
2939 for (i = 0; i < tp->d.types.count; ++i)
2940 tt_index (0);
2941 tt_end ();
2942 for (i = 0; i < tp->d.types.count; ++i)
2943 {
2944 make_type (tp->d.types.list[i], &ti_1);
2945 buffer_patch_word (&tt, patch1 + 1 + 3*i, ti_1);
2946 }
2947 break;
2948
2949 case ty_fields:
2950 if (tp->d.fields.count == 0)
2951 {
2952 tp->index = 0;
2953 RETURN (tp->index);
2954 }
2955 tt_start (0x7f, 0x02); /* List (structure field names) */
2956 for (i = 0; i < tp->d.fields.count; ++i)
2957 {
2958 tt_name (tp->d.fields.list[i].name);
2959 tt_unsigned_span (tp->d.fields.list[i].offset);
2960 }
2961 tt_end ();
2962 *hll = tp->index = hll_type_index++;
2963 break;
2964
2965 case ty_values:
2966 if (tp->d.values.count == 0)
2967 {
2968 tp->index = -1;
2969 RETURN (tp->index);
2970 }
2971 tt_start (0x7f, 0x03); /* Enum values */
2972 for (i = 0; i < tp->d.values.count; ++i)
2973 {
2974 tt_name (tp->d.values.list[i].name);
2975 tt_unsigned_span (tp->d.values.list[i].index);
2976 }
2977 tt_end ();
2978 *hll = tp->index = hll_type_index++;
2979 break;
2980
2981 case ty_memfunc:
2982 make_type (tp->d.memfunc.type, &ti_1);
2983 qual = 0;
2984 if (tp->d.memfunc.flags & MEMBER_STATIC)
2985 qual |= 0x01;
2986 if (tp->d.memfunc.flags & MEMBER_CONST)
2987 qual |= 0x04;
2988 if (tp->d.memfunc.flags & MEMBER_VOLATILE)
2989 qual |= 0x08;
2990 if (tp->d.memfunc.flags & MEMBER_VIRTUAL)
2991 qual |= 0x10;
2992 tt_start (0x45, qual); /* Member Function */
2993 tt_byte (tp->d.memfunc.flags & MEMBER_VIS); /* Protection */
2994 if (tp->d.memfunc.flags & MEMBER_CTOR)
2995 tt_byte (1); /* Constructor */
2996 else if (tp->d.memfunc.flags & MEMBER_DTOR)
2997 tt_byte (2); /* Destructor */
2998 else
2999 tt_byte (0); /* Regular member function */
3000 tt_word (ti_1); /* Type index of function */
3001 if (tp->d.memfunc.flags & MEMBER_VIRTUAL)
3002 tt_unsigned_span (tp->d.memfunc.offset); /* Index into vtable */
3003 tt_enc (tp->d.memfunc.name); /* Name of the function */
3004 tt_end ();
3005 *hll = tp->index = hll_type_index++;
3006 break;
3007
3008 case ty_baseclass:
3009 make_type (tp->d.baseclass.type, &ti_1);
3010 qual = 0;
3011 if (tp->d.baseclass.flags & MEMBER_VIRTUAL)
3012 qual |= 0x01;
3013 tt_start (0x41, qual); /* Base Class */
3014 tt_byte (tp->d.baseclass.flags & MEMBER_VIS);
3015 tt_word (ti_1); /* Type */
3016 tt_unsigned_span (tp->d.baseclass.offset);
3017 tt_end ();
3018 *hll = tp->index = hll_type_index++;
3019 break;
3020
3021 default:
3022 error ("make_type: unknown tag %d", tp->tag);
3023 }
3024}
3025
3026
3027/* Parse an unnamed stabs type and return an HLL type index for it. */
3028
3029static int hll_type (void)
3030{
3031 struct type *tp;
3032 int ti;
3033
3034 tp = parse_complete_type (NULL);
3035 make_type (tp, &ti);
3036 return ti;
3037}
3038
3039
3040/* Return the symbol name of symbol *INDEX, concatenating the names of
3041 successive symbol table entries if necessary. A pointer to the
3042 string is stored to *STR. If symbols are concatenated, *INDEX is
3043 adjusted to refer to the last symbol table entry used, in order to
3044 refer to the next symbol after incrementing by one. If TRUE is
3045 returned, the caller should free the string when it is no longer
3046 needed. If FALSE is returned, the string must not be freed. */
3047
3048static int concat_symbols (int *index, const char **str)
3049{
3050 int i, n, len;
3051 char *new;
3052 const char *p;
3053
3054 len = 0;
3055 for (i = *index; i < sym_count; ++i)
3056 {
3057 p = str_ptr + sym_ptr[i].n_un.n_strx;
3058 n = strlen (p);
3059 if (n > 0 && p[n-1] == '\\')
3060 len += n - 1;
3061 else
3062 {
3063 len += n;
3064 break;
3065 }
3066 }
3067 if (i == *index)
3068 {
3069 *str = str_ptr + sym_ptr[i].n_un.n_strx;
3070 return FALSE;
3071 }
3072 else
3073 {
3074 new = xmalloc (len + 1);
3075 *str = new;
3076 for (i = *index; i < sym_count; ++i)
3077 {
3078 p = str_ptr + sym_ptr[i].n_un.n_strx;
3079 n = strlen (p);
3080 if (n > 0 && p[n-1] == '\\')
3081 {
3082 memcpy (new, p, n - 1);
3083 new += n - 1;
3084 }
3085 else
3086 {
3087 memcpy (new, p, n);
3088 new += n;
3089 break;
3090 }
3091 }
3092 *new = 0;
3093 *index = i;
3094 return TRUE;
3095 }
3096}
3097
3098
3099/* Parse a stabs type definition and store the internal representation
3100 of that type. The sym_ptr index is passed in INDEX. As we might
3101 have to concatenate successive symbol table entries, *INDEX is
3102 updated to refer to the next symbol. */
3103
3104static void parse_typedef (int *index)
3105{
3106 char *name;
3107 const char *str, *p;
3108 struct type *t;
3109 int n, alloc_flag;
3110
3111 alloc_flag = concat_symbols (index, &str);
3112 parse_start = str;
3113 parse_pindex = index;
3114 p = nextcolon (str);
3115 if (p != NULL)
3116 {
3117 n = p - str;
3118 name = alloca (n + 1);
3119 memcpy (name, str, n);
3120 name[n] = 0;
3121#if defined (HLL_DEBUG)
3122 printf ("LSYM/LCSYM/GSYM/PSYM/RSYM/STSYM/FUN %s\n", str);
3123#endif
3124 parse_ptr = p + 1;
3125 switch (*parse_ptr)
3126 {
3127 case 'f':
3128 case 'F':
3129 ++parse_ptr;
3130 /* Note: don't call parse_complete_type() as there's a comma
3131 at the end for nested functions. */
3132 t = parse_type (NULL);
3133 break;
3134
3135 case 't':
3136 case 'T':
3137 ++parse_ptr;
3138 if (*parse_ptr == 't')
3139 ++parse_ptr; /* synonymous type */
3140 t = parse_complete_type (name);
3141#if defined (HLL_DEBUG)
3142 printf (" type: ");
3143 show_type (t);
3144 printf ("\n");
3145#endif
3146 break;
3147
3148 case 'p':
3149 case 'r':
3150 case 'G':
3151 case 'S':
3152 case 'V':
3153 ++parse_ptr;
3154 t = parse_complete_type (name);
3155#if defined (HLL_DEBUG)
3156 printf (" type: ");
3157 show_type (t);
3158 printf ("\n");
3159#endif
3160 break;
3161
3162 case '0': case '1': case '2': case '3': case '4':
3163 case '5': case '6': case '7': case '8': case '9':
3164 t = parse_complete_type (name);
3165#if defined (HLL_DEBUG)
3166 printf (" type: ");
3167 show_type (t);
3168 printf ("\n");
3169#endif
3170 break;
3171 }
3172 }
3173 if (alloc_flag)
3174 free ((void *)str); /* remove const attribute */
3175}
3176
3177
3178/* Write a `begin' subrecord to the SST. The block begins at ADDR in
3179 the text segment. */
3180
3181static size_t hll_begin (dword addr)
3182{
3183 size_t ptr;
3184 struct relocation_info r;
3185
3186 sst_start (SST_begin);
3187 r.r_address = sst.size;
3188 ptr = sst.size;
3189 buffer_dword (&sst, addr); /* Segment offset */
3190 buffer_dword (&sst, 0); /* Length of block -- patched later */
3191#if 1
3192 buffer_nstr (&sst, ""); /* Name of block (optional) */
3193#endif
3194 sst_end ();
3195
3196 r.r_extern = 0;
3197 r.r_length = 2;
3198 r.r_symbolnum = N_TEXT;
3199 r.r_pcrel = 0;
3200 r.r_pad = 0;
3201 buffer_mem (&sst_reloc, &r, sizeof (r));
3202 return ptr;
3203}
3204
3205
3206/* Start of a block. Write a `begin' subrecord to the SST. The block
3207 begins at ADDR in the text segment. Push the block onto the block
3208 stack. */
3209
3210static void block_begin (dword addr)
3211{
3212 size_t ptr;
3213
3214 ptr = hll_begin (addr);
3215 grow_by (&block_grow, 1);
3216 block_stack[block_grow.count].patch_ptr = ptr;
3217 block_stack[block_grow.count].addr = addr;
3218 ++block_grow.count;
3219}
3220
3221
3222/* Write an `end' subrecord to the SST. PATCH_PTR is the sst index of
3223 the `begin' subrecord for this block. Add ADD_START to the start
3224 address of the block and set the length of the block to LENGTH, in
3225 bytes. */
3226
3227static void hll_end (size_t patch_ptr, int add_start, size_t length)
3228{
3229 *(dword *)(sst.buf+patch_ptr+0) += add_start;
3230 *(dword *)(sst.buf+patch_ptr+4) = length;
3231 sst_start (SST_end);
3232 sst_end ();
3233}
3234
3235
3236/* Return TRUE iff symbol table entry S defines a function. */
3237
3238static int is_fun (const struct nlist *s)
3239{
3240 const char *p;
3241
3242 if (s->n_type != N_FUN)
3243 return FALSE;
3244 p = nextcolon (str_ptr + s->n_un.n_strx);
3245 if (p == NULL)
3246 return FALSE;
3247 return (p[1] == 'F' || p[1] == 'f');
3248}
3249
3250
3251/* Approximate a function prologue length by examining the code. The
3252 function starts at START and ends at END. */
3253
3254static int get_prologue_length (dword start, dword end)
3255{
3256 dword i;
3257
3258 if (end > text_size)
3259 end = text_size;
3260 i = start;
3261 if (i < end && text_ptr[i] == 0x55) ++i; /* PUSH EBP */
3262 if (i + 1 < end && text_ptr[i+0] == 0x89
3263 && text_ptr[i+1] == 0xe5) /* MOV EBP, ESP */
3264 i += 2;
3265 if (i + 2 < end && text_ptr[i+0] == 0x83
3266 && text_ptr[i+1] == 0xec) /* SUB ESP, n8 */
3267 i += 3;
3268 return i - start;
3269}
3270
3271
3272/* End of a function reached. Try to find the function length. I is
3273 the sym_ptr index of the current symbol -- we search the symbol
3274 table starting at I to find the next function for estimating the
3275 function length. */
3276
3277static void fun_end (int i)
3278{
3279 int p_len;
3280 dword addr;
3281
3282 addr = text_size;
3283 while (i < sym_count)
3284 {
3285 if (is_fun (&sym_ptr[i]))
3286 {
3287 addr = sym_ptr[i].n_value;
3288 break;
3289 }
3290 ++i;
3291 }
3292 if (prologue_length != -1)
3293 p_len = prologue_length;
3294 else
3295 p_len = get_prologue_length (proc_start_addr, addr);
3296 *(dword *)(sst.buf+proc_patch_base + 0) = addr - proc_start_addr;
3297 *(word *)(sst.buf+proc_patch_base + 4) = p_len;
3298 *(dword *)(sst.buf+proc_patch_base + 6) = addr - proc_start_addr;
3299 proc_patch_base = 0;
3300 sst_start (SST_end);
3301 sst_end ();
3302}
3303
3304
3305/* End of a block (N_RBRAC encountered). I is the sym_ptr index of
3306 the N_RBRAC symbol. If we reach the outmost block level, we call
3307 fun_end(). */
3308
3309static void block_end (int i)
3310{
3311 if (block_grow.count == 0)
3312 {
3313 /** @todo fix this - broken after my last change (bird 2003-10-06) */
3314 /* warning ("Invalid N_RBRAC without N_LBRAC"); */
3315 }
3316 else
3317 {
3318 --block_grow.count;
3319 hll_end (block_stack[block_grow.count].patch_ptr, 0,
3320 sym_ptr[i].n_value - block_stack[block_grow.count].addr);
3321 if (block_grow.count == 1) /* we make one extra begin */
3322 {
3323 --block_grow.count;
3324 hll_end (block_stack[block_grow.count].patch_ptr, 0,
3325 sym_ptr[i].n_value - block_stack[block_grow.count].addr);
3326 }
3327 if (block_grow.count == 0 && proc_patch_base != 0)
3328 fun_end (i);
3329 }
3330}
3331
3332
3333/* Define a function. SYMBOL points to the sym_ptr entry. The
3334 function arguments are taken from the args_list array. Several
3335 fields of the SST entry are filled-in later, when reaching the end
3336 of the function. proc_patch_base is used for remembering the place
3337 where to patch the SST entry. */
3338
3339static void define_fun (const struct nlist *symbol)
3340{
3341 struct type t, *t1, *t2, *t3;
3342 struct relocation_info r;
3343 const char *str, *p, *p2;
3344 const char *name, *mnglname;
3345#ifdef DEMANGLE_PROC_NAMES
3346 char *pszFree;
3347#endif
3348 size_t cchName, cchMnglName;
3349 int ti, ticlass;
3350
3351 str = str_ptr + symbol->n_un.n_strx;
3352 p = nextcolon (str);
3353 if (p == NULL)
3354 abort ();
3355 cchName = cchMnglName = p - str;
3356 name = mnglname = strpool_addn (str_pool, str, cchMnglName);
3357 proc_start_addr = symbol->n_value;
3358
3359#ifdef DEMANGLE_PROC_NAMES
3360 /* demangle the name */
3361 pszFree = cplus_demangle (mnglname, DMGL_ANSI | DMGL_PARAMS);
3362 if (!pszFree && (*mnglname == '_' || *mnglname == '*'))
3363 pszFree = cplus_demangle (mnglname + 1, DMGL_ANSI | DMGL_PARAMS);
3364 if (pszFree)
3365 {
3366 cchName = strlen (pszFree);
3367 name = pszFree;
3368 }
3369 else if (*mnglname == '*')
3370 {
3371 cchName--;
3372 name++;
3373 }
3374#endif
3375
3376 /* now let's see if there is a memfunc for this name. */
3377 for (t1 = type_head; t1; t1 = t1->next)
3378 if ( t1->tag == ty_memfunc
3379 && t1->d.memfunc.mnglname == mnglname)
3380 break;
3381
3382 if (t1)
3383 { /* C++ member function */
3384 t2 = t1->d.memfunc.type;
3385 if (t2 && t2->tag == ty_func && t2->d.func.domain)
3386 make_type (t2->d.func.domain, &ticlass);
3387 else
3388 { /* the hard way - search for the freaking string in suitable .stabs.
3389 * I think this is rather slow when done for many members.. :/ */
3390 int i;
3391 for (i = 0, t3 = NULL, ticlass = -1; !t3 && i < sym_count; ++i)
3392 switch (sym_ptr[i].n_type)
3393 {
3394 case N_LSYM:
3395 {
3396 str = str_ptr + sym_ptr[i].n_un.n_strx;
3397 p = nextcolon (str);
3398 if (p && p[1] == 'T' && p[2] == 't')
3399 {
3400 /* Now, there can be several name which NAME is a
3401 * substring in.
3402 */
3403 /** @todo: the string parsing still is a bit spooky. */
3404 for (p2 = p; (p2 = strstr (p2, mnglname)) != NULL; p2 += cchMnglName)
3405 if (p2[-1] == ':' && p2[cchMnglName] == ';')
3406 {
3407 int stab = atoi (&p[3]);
3408 t3 = stype_find (stab);
3409 break;
3410 }
3411 }
3412 break;
3413 }
3414 }
3415 if (t3)
3416 make_type (t3, &ticlass);
3417 if (ticlass < 0)
3418 {
3419 warning (" Can't figure out which class method '%s' is a member of!", mnglname);
3420 ticlass = 0;
3421 }
3422 }
3423
3424 make_type (t1, &ti);
3425 sst_start (SST_memfunc);
3426 r.r_address = sst.size;
3427 buffer_dword (&sst, symbol->n_value); /* Segment offset */
3428 buffer_word (&sst, ti); /* Type index */
3429 proc_patch_base = sst.size;
3430 buffer_dword (&sst, 0); /* Length of proc */
3431 buffer_word (&sst, 0); /* Length of prologue */
3432 buffer_dword (&sst, 0); /* Length of prologue and body */
3433 buffer_word (&sst, ticlass); /* Class type (for member functions) */
3434 buffer_byte (&sst, 8); /* 32-bit near */
3435 buffer_enc (&sst, name); /* Proc name */
3436 sst_end ();
3437 }
3438 else
3439 {
3440 if (args_grow.count == 0)
3441 t1 = NULL;
3442 else
3443 {
3444 t.tag = ty_args;
3445 t.index = -1;
3446 t.d.args.count = args_grow.count;
3447 t.d.args.list = xmalloc (args_grow.count * sizeof (*args_list));
3448 memcpy (t.d.args.list, args_list, args_grow.count * sizeof (*args_list));
3449 t1 = type_add (&t);
3450 free (t.d.args.list);
3451 }
3452 last_fun_type.d.func.args = t1;
3453 last_fun_type.d.func.arg_count = args_grow.count;
3454 t2 = type_add (&last_fun_type);
3455 make_type (t2, &ti);
3456
3457 sst_start (cplusplus_flag || cchName > 255 ? SST_CPPproc : SST_proc);
3458 r.r_address = sst.size;
3459 buffer_dword (&sst, symbol->n_value); /* Segment offset */
3460 buffer_word (&sst, ti); /* Type index */
3461 proc_patch_base = sst.size;
3462 buffer_dword (&sst, 0); /* Length of proc */
3463 buffer_word (&sst, 0); /* Length of prologue */
3464 buffer_dword (&sst, 0); /* Length of prologue and body */
3465 buffer_word (&sst, 0); /* Class type (for member functions) */
3466 buffer_byte (&sst, 8); /* 32-bit near */
3467 if (cplusplus_flag || cchName > 255)
3468 buffer_enc (&sst, name); /* Proc name */
3469 else
3470 buffer_nstr (&sst, name); /* Proc name */
3471 sst_end ();
3472 }
3473 r.r_extern = 0;
3474 r.r_length = 2;
3475 r.r_symbolnum = N_TEXT;
3476 r.r_pcrel = 0;
3477 r.r_pad = 0;
3478 buffer_mem (&sst_reloc, &r, sizeof (r));
3479 set_hll_type (symbol - sym_ptr, ti);
3480
3481#ifdef DEMANGLE_PROC_NAMES
3482 /* free the demangled name. */
3483 free (pszFree);
3484#endif
3485}
3486
3487
3488/* Locate the next N_LBRAC starting at symbol I. If found, set
3489 lbrac_index to the index of N_LBRAC and start a new block. If
3490 there is no N_LBRAC, set lbrac_index to -1. */
3491
3492static void next_lbrac (int i)
3493{
3494 lbrac_index = -1;
3495 for (; i < sym_count; ++i)
3496 if (sym_ptr[i].n_type == N_RBRAC || is_fun (&sym_ptr[i]))
3497 return;
3498 else if (sym_ptr[i].n_type == N_LBRAC)
3499 {
3500 lbrac_index = i;
3501 block_begin (sym_ptr[i].n_value);
3502 return;
3503 }
3504}
3505
3506
3507/* Parse a stabs symbol. The sym_ptr index is passed in INDEX. As we
3508 might have to concatenate successive symbol table entries, *INDEX
3509 is updated to refer to the next symbol. WHERE is the segment where
3510 the symbol is defined (N_DATA, for instance). If WHERE is -1,
3511 there is no segment. MSG is used for warning messages. This
3512 function is called recursively for static functions (F) There's up
3513 to one level of recursion. The arguments of a function are read
3514 twice: If FLAG is TRUE, we turn the arguments into local symbols;
3515 if FLAG is FALSE, we put arguments into the args_list array. */
3516
3517static void parse_symbol (int *index, int where, const char *msg, int flag)
3518{
3519 char *name;
3520 const char *str, *p;
3521 const struct nlist *symbol;
3522 struct type *t1;
3523 int i, n, ti, alloc_flag;
3524
3525 symbol = &sym_ptr[*index];
3526 alloc_flag = concat_symbols (index, &str);
3527 parse_start = str;
3528 parse_pindex = index;
3529 p = nextcolon (str);
3530 if (p != NULL)
3531 {
3532 n = p - str;
3533 name = alloca (n + 1);
3534 memcpy (name, str, n);
3535 name[n] = 0;
3536#if defined (HLL_DEBUG)
3537 printf ("%s %s\n", msg, str);
3538#endif
3539
3540 parse_ptr = p + 1;
3541 switch (*parse_ptr)
3542 {
3543 case 'G':
3544 /* Static storage, global scope */
3545 {
3546 const struct nlist *sym2 = NULL;
3547 ++parse_ptr;
3548 ti = hll_type ();
3549#if defined (HLL_DEBUG)
3550 printf (" type=%#x\n", ti);
3551#endif
3552
3553 /* Check for a ugly hack in dbxout.c which allow us to resolve
3554 communal and global variables more accuratly. The n_value is -12357
3555 and the previous entry is 0xfe. What the previous entry contains
3556 is the assembly name of the symbol.
3557
3558 Another part of that hack is that n_value is contains the
3559 symbol value so we don't need to look up the symbol for that
3560 reason (but as it turns out we have to look it up to see where
3561 it is in most cases, bah). */
3562 if ( symbol->n_value == -12357
3563 && *index >= 1
3564 && symbol[-1].n_type == 0xfe)
3565 {
3566 sym2 = find_symbol_ex (symbol[-1].n_un.n_strx + str_ptr, *index - 1, 1);
3567 if (!sym2)
3568 {
3569 warning ("Cannot find address of communal/external variable %s", name);
3570 return;
3571 }
3572 }
3573 else if (where == -1)
3574 { /* This is not very nice, we have to look up the symbol
3575 like we used to do in order to the the segment right.
3576 Should probably just use the external/communal hack above
3577 for everything or change the n_type of the 'G' stab... However,
3578 this will work for most cases and it will be compatible with
3579 the unmodified dbxout.c code. */
3580 if (symbol->n_type == N_FUN) /* in case GCC change... */
3581 where = N_TEXT;
3582 else
3583 {
3584 size_t cch = strlen (name);
3585 char *psz = alloca (cch + 2);
3586 *psz = '_';
3587 memcpy (psz + 1, name, cch + 1);
3588 sym2 = find_symbol (psz);
3589 if (!sym2)
3590 sym2 = find_symbol (psz + 1);
3591 where = N_DATA;
3592 }
3593 }
3594 /* if we're lucky we've got a symbol... */
3595 if (sym2)
3596 {
3597 if (sym2->n_type == N_EXT)
3598 sst_static (name, 0, ti, sym2 - sym_ptr, 1, sym2);
3599 else
3600 sst_static (name, sym2->n_value, ti, sym2->n_type, 0, sym2);
3601 }
3602 else
3603 sst_static (name, symbol->n_value, ti, where, 0, NULL);
3604 break;
3605 }
3606
3607 case 'S':
3608 case 'V':
3609
3610 /* S = static storage, file scope,
3611 V = static storage, local scope */
3612
3613 if (where == -1)
3614 {
3615 warning ("Cannot use symbol type %c with N_%s", *parse_ptr, msg);
3616 return;
3617 }
3618 ++parse_ptr;
3619 ti = hll_type ();
3620#if defined (HLL_DEBUG)
3621 printf (" type=%#x\n", ti);
3622#endif
3623 sst_static (name, symbol->n_value, ti, where, 0, NULL);
3624 break;
3625
3626 case 'p':
3627
3628 /* Function argument. */
3629
3630 if (where != -1)
3631 {
3632 warning ("Cannot use argument symbol with N_%s", msg);
3633 return;
3634 }
3635 ++parse_ptr;
3636 if (flag)
3637 {
3638 ti = hll_type ();
3639#if defined (HLL_DEBUG)
3640 printf (" type=%#x\n", ti);
3641#endif
3642 sst_start (SST_auto);
3643 buffer_dword (&sst, symbol->n_value);/* Offset into stk frame */
3644 buffer_word (&sst, ti); /* Type index */
3645 buffer_nstr (&sst, name); /* Symbol name */
3646 sst_end ();
3647 }
3648 else
3649 {
3650 t1 = parse_complete_type (NULL);
3651 grow_by (&args_grow, 1);
3652 args_list[args_grow.count++] = t1;
3653 }
3654 break;
3655
3656 case 'r':
3657
3658 /* Register variable. */
3659
3660 if (where != -1)
3661 {
3662 warning ("Cannot use register symbol with N_%s", msg);
3663 return;
3664 }
3665 ++parse_ptr;
3666 ti = hll_type ();
3667 switch (symbol->n_value)
3668 {
3669 case 0: /* EAX */
3670 i = 0x10;
3671 break;
3672 case 1: /* ECX */
3673 i = 0x11;
3674 break;
3675 case 2: /* EDX */
3676 i = 0x12;
3677 break;
3678 case 3: /* EBX */
3679 i = 0x13;
3680 break;
3681 case 4: /* ESP */
3682 i = 0x14;
3683 break;
3684 case 5: /* EBP */
3685 i = 0x15;
3686 break;
3687 case 6: /* ESI */
3688 i = 0x16;
3689 break;
3690 case 7: /* EDI */
3691 i = 0x17;
3692 break;
3693 case 12: /* ST(0) */
3694 i = 0x80;
3695 break;
3696 case 13: /* ST(1) */
3697 i = 0x81;
3698 break;
3699 case 14: /* ST(2) */
3700 i = 0x82;
3701 break;
3702 case 15: /* ST(3) */
3703 i = 0x83;
3704 break;
3705 case 16: /* ST(4) */
3706 i = 0x84;
3707 break;
3708 case 17: /* ST(5) */
3709 i = 0x85;
3710 break;
3711 case 18: /* ST(6) */
3712 i = 0x86;
3713 break;
3714 case 19: /* ST(7) */
3715 i = 0x87;
3716 break;
3717 default:
3718 warning ("unknown register %lu", symbol->n_value);
3719 return;
3720 }
3721 sst_start (SST_reg); /* Register variable */
3722 buffer_word (&sst, ti); /* Type index */
3723 buffer_byte (&sst, i); /* Register number */
3724 buffer_nstr (&sst, name); /* Symbol name */
3725 sst_end ();
3726 break;
3727
3728 case '0': case '1': case '2': case '3': case '4':
3729 case '5': case '6': case '7': case '8': case '9':
3730
3731 /* Local symbol. */
3732
3733 if (where != -1)
3734 {
3735 warning ("Cannot use local symbol with N_%s", msg);
3736 return;
3737 }
3738 ti = hll_type ();
3739#if defined (HLL_DEBUG)
3740 printf (" type=%#x\n", ti);
3741#endif
3742 sst_start (SST_auto);
3743 buffer_dword (&sst, symbol->n_value); /* Offset into stk frame */
3744 buffer_word (&sst, ti); /* Type index */
3745 buffer_nstr (&sst, name); /* Symbol name */
3746 sst_end ();
3747 break;
3748
3749 case 'F':
3750 case 'f':
3751
3752 /* Function. */
3753
3754 ++parse_ptr;
3755 if (where != N_TEXT)
3756 {
3757 warning ("Cannot use function with N_%s", msg);
3758 return;
3759 }
3760 last_fun_type.tag = ty_func;
3761 last_fun_type.index = -1;
3762 last_fun_type.d.func.domain = NULL;
3763 last_fun_type.d.func.ret = parse_type (NULL);
3764
3765 /* Don't check for end of string -- parse_ptr points to a
3766 comma for nested functions! */
3767
3768 last_fun_type.d.func.args = NULL;
3769 last_fun_type.d.func.arg_count = 0;
3770 last_fun_valid = TRUE;
3771 args_grow.count = 0;
3772 last_fun_addr = symbol->n_value;
3773 prologue_length = -1;
3774
3775 for (i = *index + 1; i < sym_count && sym_ptr[i].n_type == N_PSYM; ++i)
3776 parse_symbol (&i, -1, "PSYM", FALSE);
3777
3778 define_fun (symbol);
3779
3780 /* Now look for N_LBRAC */
3781 next_lbrac (i);
3782 if (lbrac_index != -1)
3783 {
3784 if (prologue_length == -1)
3785 prologue_length = sym_ptr[lbrac_index].n_value - last_fun_addr;
3786 }
3787 else
3788 {/* kso #456 2003-06-05: We need a begin to catch the parameters. */
3789 prologue_length = 0;
3790 block_begin (proc_start_addr);
3791 }
3792
3793 /* Parameters */
3794 while (*index + 1 < sym_count && sym_ptr[*index+1].n_type == N_PSYM)
3795 {
3796 ++(*index);
3797 parse_symbol (index, -1, "PSYM", TRUE);
3798 }
3799
3800 /* Start another block for the local variables. This is to work
3801 around some missing bracets in stabs and to fix problem with
3802 C++ bool type. */
3803 dword addr = proc_start_addr + prologue_length;
3804 if (lbrac_index >= 0 && sym_ptr[lbrac_index].n_value > addr)
3805 addr = sym_ptr[lbrac_index].n_value;
3806 block_begin (addr);
3807
3808 /* Local variables (fix for boolparam testcase too). */
3809 while (*index + 1 < sym_count && sym_ptr[*index+1].n_type == N_LSYM)
3810 {
3811 ++(*index);
3812 parse_symbol (index, -1, "LSYM", TRUE);
3813 }
3814
3815 /* If there's no N_LBRAC, write SST_end now */
3816 if (lbrac_index == -1)
3817 {/* kso #456 2003-06-05: We need a begin to catch the parameters. */
3818 while (block_grow.count > 0)
3819 block_end (*index+1);
3820 }
3821 break;
3822 }
3823 }
3824 if (alloc_flag)
3825 free ((void *)str); /* Remove the const attribute */
3826}
3827
3828
3829/* Create a type tag for HLL type index INDEX. TYPE is either SST_tag
3830 (for structures and unions) or SST_tag2 (for classes). NAME is
3831 the name of the structure, union or class. */
3832
3833static void type_tag (int type, int index, const char *name)
3834{
3835 if (name != NULL)
3836 {
3837 size_t cch;
3838 if (type == SST_tag2 || (cch = strlen(name)) > 250)
3839 {
3840 sst_start (SST_tag2);
3841 buffer_word (&sst, index);
3842 buffer_enc (&sst, name);
3843 }
3844 else
3845 {
3846 char achName[256];
3847 memcpy (achName, name, cch);
3848 achName[cch] = '\0';
3849
3850 sst_start (type);
3851 buffer_word (&sst, index);
3852 buffer_nstr (&sst, achName);
3853 }
3854 sst_end ();
3855 }
3856}
3857
3858
3859/* Write a CuInfo SST entry to tell the debugger what compiler (C or
3860 C++) has been used. */
3861
3862static void write_cuinfo (void)
3863{
3864 struct timestamp ts;
3865 time_t now;
3866 struct tm *tp;
3867 byte lang;
3868
3869 if (cplusplus_flag)
3870 lang = 0x02; /* C++ */
3871 else
3872 lang = 0x01; /* C */
3873
3874 time (&now);
3875 tp = localtime (&now);
3876 ts.year = tp->tm_year + 1900;
3877 ts.month = tp->tm_mon + 1;
3878 ts.day = tp->tm_mday;
3879 ts.hours = tp->tm_hour;
3880 ts.minutes = tp->tm_min;
3881 ts.seconds = tp->tm_sec;
3882 ts.hundredths = 0;
3883 sst_start (SST_cuinfo);
3884 buffer_byte (&sst, lang);
3885 buffer_nstr (&sst, " "); /* Compiler options */
3886 buffer_nstr (&sst, __DATE__); /* Compiler date */
3887 buffer_mem (&sst, &ts, sizeof (ts));
3888 sst_end ();
3889}
3890
3891
3892/* Write a ChangSeg SST entry to define the text segment. */
3893
3894static void write_changseg (void)
3895{
3896 struct relocation_info r;
3897
3898 sst_start (SST_changseg);
3899 r.r_address = sst.size;
3900 buffer_word (&sst, 0); /* Segment number */
3901 buffer_word (&sst, 0); /* Reserved */
3902 sst_end ();
3903 r.r_extern = 0;
3904 r.r_length = 1;
3905 r.r_symbolnum = N_TEXT;
3906 r.r_pcrel = 0;
3907 r.r_pad = 0;
3908 buffer_mem (&sst_reloc, &r, sizeof (r));
3909}
3910
3911
3912/* Convert debug information. The data is taken from the symbol and
3913 string tables of the current a.out module. Output is put into the
3914 tt, sst etc. buffers. */
3915
3916void convert_debug (void)
3917{
3918 int i;
3919 struct type *t1, *t2;
3920#if defined (HLL_DEBUG)
3921 setvbuf(stdout, NULL, _IONBF, 256);
3922#endif
3923
3924 str_pool = strpool_init ();
3925 grow_init (&stype_grow, &stype_list, sizeof (*stype_list), 32);
3926 type_head = NULL; hll_type_index = 512;
3927 grow_init (&block_grow, &block_stack, sizeof (*block_stack), 16);
3928 grow_init (&args_grow, &args_list, sizeof (*args_list), 16);
3929 last_fun_valid = FALSE; prologue_length = -1;
3930 unnamed_struct_number = 1;
3931
3932 /* Check whether converting a translated C++ program. */
3933#if 1 /* kso #465 2003-06-04: pretend everything is C++, that symbols is no longer present. */
3934 cplusplus_flag = 1;
3935#else
3936 cplusplus_flag = (find_symbol ("__gnu_compiled_cplusplus") != NULL);
3937#endif
3938
3939
3940 /* Parse the typedefs to avoid forward references. */
3941
3942 for (i = 0; i < sym_count; ++i)
3943 switch (sym_ptr[i].n_type)
3944 {
3945 case N_LSYM:
3946 case N_LCSYM:
3947 case N_GSYM:
3948 case N_PSYM:
3949 case N_RSYM:
3950 case N_STSYM:
3951 case N_FUN:
3952 parse_typedef (&i);
3953 break;
3954 }
3955
3956 /* Provide information about the compiler. */
3957
3958 write_cuinfo ();
3959
3960 /* Change the default segment. */
3961
3962 write_changseg ();
3963
3964 /* Parse the other symbol table entries. */
3965
3966 lbrac_index = -1; block_grow.count = 0; proc_patch_base = 0;
3967 for (i = 0; i < sym_count; ++i)
3968 switch (sym_ptr[i].n_type)
3969 {
3970 case N_LSYM:
3971 parse_symbol (&i, -1, "LSYM", TRUE);
3972 break;
3973
3974 case N_GSYM:
3975 parse_symbol (&i, -1, "GSYM", TRUE);
3976 break;
3977
3978 case N_LCSYM:
3979 parse_symbol (&i, N_BSS, "LCSYM", TRUE);
3980 break;
3981
3982 case N_STSYM:
3983 parse_symbol (&i, N_DATA, "STSYM", TRUE);
3984 break;
3985
3986 case N_RSYM:
3987 parse_symbol (&i, -1, "RSYM", TRUE);
3988 break;
3989
3990 case N_LBRAC:
3991 if (lbrac_index == -1) /* N_LBRAC without local symbols */
3992 block_begin (sym_ptr[i].n_value);
3993 else if (i != lbrac_index)
3994 warning ("Invalid N_LBRAC");
3995 next_lbrac (i + 1);
3996 break;
3997
3998 case N_RBRAC:
3999 block_end (i);
4000 next_lbrac (i + 1);
4001 break;
4002
4003 case N_FUN:
4004 parse_symbol (&i, N_TEXT, "FUN", TRUE);
4005 break;
4006 }
4007
4008#ifdef HASH_DEBUG
4009fprintf(stderr, "\n");
4010for (i = 0; i < TYPE_HASH_MAX; i++)
4011 if (i % 7 == 6)
4012 fprintf(stderr, "%3d: %4d\n", i, ctype_tag[i]);
4013 else
4014 fprintf(stderr, "%3d: %4d ", i, ctype_tag[i]);
4015fprintf(stderr, "\nctypes=%d\n", ctypes);
4016for (i = 0; i < TYPE_HASH_MAX; i++)
4017 if (ctype_tag[i] > 1000)
4018 {
4019 int j;
4020 unsigned au[ty_max];
4021 memset(&au[0], 0, sizeof(au));
4022 for (t1 = atype_tag[i]; t1 != NULL; t1 = t1->nexthash)
4023 au[t1->tag]++;
4024
4025 fprintf(stderr, "\nIndex %d is overpopulated (%d):\n", i, ctype_tag[i]);
4026 for (j = 0; j < 18; j++)
4027 if (j % 7 == 6)
4028 fprintf(stderr, "%2d: %4d\n", j, au[j]);
4029 else
4030 fprintf(stderr, "%2d: %4d ", j, au[j]);
4031 }
4032#endif
4033
4034 /* Create tags for structures, unions and classes. */
4035
4036 for (t1 = type_head; t1 != NULL; t1 = t1->next)
4037 if (t1->index != -1)
4038 switch (t1->tag)
4039 {
4040 case ty_struc:
4041 type_tag (SST_tag, t1->index, t1->d.struc.name);
4042 break;
4043 case ty_class:
4044 type_tag (SST_tag2, t1->index, t1->d.class.name);
4045 break;
4046 case ty_enu:
4047 type_tag (SST_tag, t1->index, t1->d.enu.name);
4048 break;
4049 default:
4050 break;
4051 }
4052
4053#ifdef HLL_DEBUG
4054 printf ("Stabs to HLL type mappings: %d/%d\n", stype_grow.count, stype_grow.alloc);
4055 for (i = 0; i < stype_grow.alloc; i++)
4056 if (stype_list[i])
4057 {
4058 printf (" %3d => 0x%03x/%-3d ", i + 1, stype_list[i]->index, stype_list[i]->index);
4059 show_type (stype_list[i]);
4060 printf("\n");
4061 }
4062#endif
4063
4064
4065 /* Deallocate memory. */
4066
4067 grow_free (&stype_grow);
4068 grow_free (&args_grow);
4069 grow_free (&block_grow);
4070 for (t1 = type_head; t1 != NULL; t1 = t2)
4071 {
4072 t2 = t1->next;
4073 switch (t1->tag)
4074 {
4075 case ty_args:
4076 if (t1->d.args.list != NULL)
4077 free (t1->d.args.list);
4078 break;
4079 case ty_types:
4080 if (t1->d.types.list != NULL)
4081 free (t1->d.types.list);
4082 break;
4083 case ty_fields:
4084 if (t1->d.fields.list != NULL)
4085 free (t1->d.fields.list);
4086 break;
4087 case ty_values:
4088 if (t1->d.values.list != NULL)
4089 free (t1->d.values.list);
4090 break;
4091 default:
4092 break;
4093 }
4094 free (t1);
4095 }
4096 type_head = NULL;
4097 memset (&atype_tag, 0, sizeof(atype_tag));
4098 strpool_free (str_pool);
4099 str_pool = NULL;
4100}
Note: See TracBrowser for help on using the repository browser.