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

Last change on this file since 2673 was 2673, checked in by bird, 19 years ago
  • *:

o Synced over changed from 0.6.1 bugfixing.

  • emxbind:

o #38: Fixed truncation bug writing to the LX nametable. (Yuri)
o #38: Imports and exports are limited to 255 not 127 chars. (Yuri)
o #28: Use DLL name from the .def file when present.

  • emxomf:

o #70: Demangle symbol names in debug info. (thanks to Yuri)

  • emxomfld:

o #55: delete the response file when reinit the args.
o #46: specify .map file extension to the linker.
o #34: Removed all the silliness trying to deal with truncated symbols.
o Don't display usage() on failure, just the error message.
o #20: use mkstemp + close instead of mktemp for the response file.

  • ld:

o #20: use make_temp_file instead of mktemp. This involved including

libiberty.h which required some adjustments of duplicate code to work.

o #27: Applied fix from Yuri.

  • libmoddef:

o Allow '.' and '@' in LIBRARY/NAME names.

  • Property cvs2svn:cvs-rev set to 1.35
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 118.1 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 @<basetype>,<membertype>
1951 Used for addressing class/struct/union members.
1952 */
1953
1954 ++parse_ptr;
1955 switch (*parse_ptr)
1956 {
1957 case 's':
1958 ++parse_ptr;
1959 if (!parse_number (&size) || !parse_char (';'))
1960 goto syntax;
1961 if (*parse_ptr == 'r')
1962 { /* nested */
1963 return parse_type(type_name);
1964 }
1965
1966 if (!parse_number (&code))
1967 goto syntax;
1968 if (size == 8 && code == -16)
1969 {
1970#if 0 /* debugger doesn't understand this game */
1971 t.tag = ty_prim;
1972 t.index = 0x90; /* 8 bit boolean */
1973#else
1974 t.tag = ty_values;
1975 t.d.values.count = 2;
1976 t.d.values.list = xmalloc (2 * sizeof (*t.d.values.list));
1977 t.d.values.list[0].index = 0;
1978 t.d.values.list[0].name = strpool_add (str_pool, "false");
1979 t.d.values.list[1].index = 1;
1980 t.d.values.list[1].name = strpool_add (str_pool, "true");
1981 t1 = type_add (&t);
1982 free (t.d.values.list);
1983
1984 t.tag = ty_prim;
1985 t.index = 0x80;
1986 t2 = type_add (&t);
1987
1988 t.tag = ty_enu;
1989 t.index = -1;
1990 t.d.enu.values = t1;
1991 t.d.enu.type = t2;
1992 t.d.enu.name = add_struct_name (NULL);
1993 t.d.enu.first = 0;
1994 t.d.enu.last = 1;
1995#endif
1996 }
1997 else
1998 {
1999 warning ("Unknown GNU extension type code: %ld, %ld bits",
2000 code, size);
2001 goto syntax;
2002 }
2003 if (!parse_char (';'))
2004 goto syntax;
2005 break;
2006
2007 case '0': case '1': case '2': case '3': case '4':
2008 case '5': case '6': case '7': case '8': case '9':
2009 {
2010 /* just make it an integer - see dbxout.c OFFSET_TYPE case. */
2011 if ( parse_number (&num1)
2012 && *parse_ptr == ','
2013 && ++parse_ptr
2014 && parse_number (&num2)
2015 )
2016 {
2017 t.tag = ty_prim;
2018 t.index = 0x82; /* 32 bit signed */
2019 }
2020 else
2021 {
2022 warning_parse ("Invalid BASE_OFFSET GNU extension format!");
2023 goto syntax;
2024 }
2025 break;
2026 }
2027
2028 default:
2029 warning ("Unknown GNU extension type: @%c", *parse_ptr);
2030 goto syntax;
2031 }
2032 break;
2033
2034 case 'x':
2035
2036 /* Incomplete type: xs<name>:
2037 xu<name>:
2038 xe<name>: */
2039
2040 ++parse_ptr;
2041 ch = *parse_ptr++;
2042 p1 = nextcolon2 (parse_ptr, 'x');
2043 if (p1 == NULL)
2044 {
2045 warning ("Invalid forward reference: missing colon");
2046 goto syntax;
2047 }
2048
2049 switch (ch)
2050 {
2051 case 's':
2052 case 'u':
2053 {
2054 /* make a fake structure. */
2055 t.tag = ty_prim;
2056 t.index = 0x80;
2057 tlist = xmalloc (1 * sizeof (*t.d.types.list));
2058 tlist[0] = type_add (&t);
2059 t.tag = ty_types;
2060 t.index = -1;
2061 t.d.types.count = 1;
2062 t.d.types.list = tlist;
2063 t1 = type_add (&t);
2064 free (tlist);
2065
2066 t.tag = ty_fields;
2067 t.index = -1;
2068 t.d.fields.count = 1;
2069 t.d.fields.list = xmalloc (1 * sizeof (*t.d.fields.list));
2070 t.d.fields.list[0].offset = 0;
2071 t.d.fields.list[0].name = strpool_add (str_pool, "This_Is_an_Incomplete_Structure_SORRY");
2072 t2 = type_add (&t);
2073 free (t.d.fields.list);
2074
2075 t.tag = ty_struc;
2076 t.index = -1;
2077 t.d.struc.types = t1;
2078 t.d.struc.fields = t2;
2079 t.d.struc.size = 1;
2080 t.d.struc.count = 1;
2081 t.d.struc.name = strpool_addn (str_pool, parse_ptr, p1 - parse_ptr);
2082 t.d.struc.flags = STRUC_FORWARD;
2083
2084 /* Avoid adding an incomplete type if the complete one is
2085 already defined. We're ignoring scoping (TODO). */
2086
2087 for (t3 = type_head; t3 != NULL; t3 = t3->next)
2088 if ((t3->tag == ty_struc && t3->d.struc.name == t.d.struc.name
2089 && !(t3->d.struc.flags & STRUC_FORWARD))
2090 || (t3->tag == ty_class && t3->d.class.name == t.d.struc.name))
2091 {
2092 result = t3;
2093 break;
2094 }
2095 break;
2096 }
2097
2098 case 'e':
2099 { /* just make it a primary. */
2100 /** @todo make an enum perhaps? */
2101 t.tag = ty_prim;
2102 t.index = 0x82; /* 32 bit signed */
2103 break;
2104 }
2105
2106 default:
2107 warning ("Unknown forward reference: %c", ch);
2108 parse_ptr = p1 + 1;
2109 goto syntax;
2110 }
2111 parse_ptr = p1 + 1;
2112 break;
2113
2114 case 's': /* struct, class */
2115 case 'u': /* union */
2116
2117 /* Structures, unions and classes:
2118
2119 s<size>[<baseclasses>]{<field>}{<method>}
2120
2121 BASECLASSES: !<#baseclasses>,{<baseclass>;}
2122 BASECLASS: <virtuality><visibility><offset>,<type>
2123 FIELD: <name>:[/0|1|2]<type>,<offset>,<width>;
2124 FIELD: <name>:[/0|1|2]<type>:<static_name>;
2125 METHOD: <name>::{<type>:<args>};
2126
2127 */
2128
2129 ++parse_ptr;
2130 class_flag = FALSE;
2131 if (!parse_number (&size))
2132 goto syntax;
2133 grow_init (&g1, &tmp_fields, sizeof (*tmp_fields), 8);
2134
2135 /* Parse the base classes, if present. */
2136
2137 if (*parse_ptr == '!')
2138 {
2139 long i, n;
2140
2141 ++parse_ptr;
2142 class_flag = TRUE;
2143 if (!parse_number (&n) || !parse_char (','))
2144 goto syntax;
2145 for (i = 0; i < n; ++i)
2146 {
2147 grow_by (&g1, 1);
2148 tf = &tmp_fields[g1.count++];
2149 tf->flags = MEMBER_BASE;
2150 tf->name = NULL;
2151 tf->sname = NULL;
2152 switch (*parse_ptr)
2153 {
2154 case '0':
2155 ++parse_ptr;
2156 break;
2157 case '1':
2158 tf->flags |= MEMBER_VIRTUAL;
2159 ++parse_ptr;
2160 break;
2161 default:
2162 warning ("Invalid base class visibility: %c", *parse_ptr);
2163 if (*parse_ptr != 0)
2164 ++parse_ptr;
2165 }
2166
2167 tf->flags |= parse_visibility ();
2168 if (!parse_number (&offset) || !parse_char (','))
2169 goto syntax;
2170 tf->offset = offset >> 3;
2171 t1 = parse_type (NULL);
2172 switch (t1->tag)
2173 {
2174 case ty_class:
2175 break;
2176 case ty_struc:
2177 /* kso #456 2003-06-11: Don't mess with forward defines! */
2178 if (!( (t1->d.struc.flags & STRUC_FORWARD)
2179 /*&& t1->d.struc.types == NULL
2180 && t1->d.struc.fields == NULL*/))
2181 struct_to_class (t1);
2182 break;
2183 default:
2184 warning ("Invalid base class type");
2185 }
2186 tf->type = t1;
2187 if (!parse_char (';'))
2188 goto syntax;
2189 }
2190 }
2191
2192 while (*parse_ptr != ';')
2193 {
2194 p1 = nextcolon (parse_ptr);
2195 if (p1 == NULL)
2196 {
2197 warning ("Invalid structure type: missing colon");
2198 goto syntax;
2199 }
2200 n = p1 - parse_ptr;
2201 grow_by (&g1, 1);
2202 tf = &tmp_fields[g1.count++];
2203 tf->flags = VIS_PUBLIC;
2204 tf->name = strpool_addn (str_pool, parse_ptr, n);
2205 tf->mnglname = NULL;
2206 parse_ptr = p1 + 1;
2207 if (*parse_ptr == ':')
2208 {
2209 class_flag = TRUE;
2210 ++parse_ptr;
2211 do
2212 {
2213 const char * pszMangled;
2214
2215 t1 = parse_type (NULL);
2216 offset = 0;
2217 if (t1 == NULL || t1->tag != ty_func)
2218 {
2219 no_warning ("Invalid member function type");
2220 goto syntax;
2221 }
2222
2223 if (*parse_ptr != ':')
2224 {
2225 no_warning ("Arguments for member function missing");
2226 goto syntax;
2227 }
2228 ++parse_ptr;
2229 pszMangled = parse_ptr;
2230 if (!parse_mangled_args ())
2231 goto syntax;
2232 tf->mnglname = strpool_addn(str_pool, pszMangled, parse_ptr - pszMangled - 1); /* ASSUMES no extra spaces! */
2233 tf->flags = parse_visibility () | MEMBER_FUNCTION;
2234 switch (*parse_ptr)
2235 {
2236 case 'A':
2237 ++parse_ptr;
2238 break;
2239 case 'B':
2240 tf->flags |= MEMBER_CONST;
2241 ++parse_ptr;
2242 break;
2243 case 'C':
2244 tf->flags |= MEMBER_VOLATILE;
2245 ++parse_ptr;
2246 break;
2247 case 'D':
2248 tf->flags |= MEMBER_CONST | MEMBER_VOLATILE;
2249 ++parse_ptr;
2250 break;
2251 default:
2252 no_warning ("Unknown member function qualifier: %c",
2253 *parse_ptr);
2254 if (*parse_ptr != 0)
2255 ++parse_ptr;
2256 }
2257 switch (*parse_ptr)
2258 {
2259 case '.':
2260 ++parse_ptr;
2261 break;
2262 case '*':
2263 tf->flags |= MEMBER_VIRTUAL;
2264 ++parse_ptr;
2265 if (!parse_number (&offset) || !parse_char (';'))
2266 {
2267 warning ("Invalid data for virtual member function");
2268 goto syntax;
2269 }
2270 parse_type (NULL);
2271 if (!parse_char (';'))
2272 {
2273 warning ("Invalid data for virtual member function");
2274 goto syntax;
2275 }
2276 break;
2277 case '?':
2278 tf->flags |= MEMBER_STATIC;
2279 ++parse_ptr;
2280 break;
2281 default:
2282 no_warning ("Unknown member function qualifier: %c",
2283 *parse_ptr);
2284 if (*parse_ptr != 0)
2285 ++parse_ptr;
2286 }
2287 /* Contstructor / Destructor hack -
2288 * TODO: verify that this doesn't really work with overloaded constructors and fix it.
2289 */
2290 if (!(tf->flags & MEMBER_STATIC))
2291 {
2292 /* using the strpool is much faster! */
2293 static const char *pszCompCtor, *pszBaseCtor, *pszCompDtor, *pszBaseDtor = NULL;
2294 if (!pszBaseDtor)
2295 {
2296 pszCompCtor = strpool_addn(str_pool, "__comp_ctor", 11);
2297 pszBaseCtor = strpool_addn(str_pool, "__base_ctor", 11);
2298 pszCompDtor = strpool_addn(str_pool, "__comp_dtor", 11);
2299 pszBaseDtor = strpool_addn(str_pool, "__base_dtor", 11);
2300 }
2301
2302 if (tf->name == pszCompCtor || tf->name == pszBaseCtor)
2303 tf->flags |= MEMBER_CTOR;
2304 else if (tf->name == pszCompDtor || tf->name == pszBaseDtor)
2305 tf->flags |= MEMBER_DTOR;
2306 }
2307 tf->type = t1;
2308 tf->offset = offset;
2309 if (*parse_ptr != ';')
2310 {
2311 /* Another member function with the same name
2312 follows. */
2313
2314 grow_by (&g1, 1);
2315 tf = &tmp_fields[g1.count++];
2316 tf->flags = VIS_PUBLIC | MEMBER_FUNCTION;
2317 tf->name = tf[-1].name;
2318 }
2319 } while (*parse_ptr != ';');
2320 ++parse_ptr;
2321 }
2322 else
2323 {
2324 if (*parse_ptr == '/')
2325 {
2326 class_flag = TRUE;
2327 ++parse_ptr;
2328 tf->flags = parse_visibility ();
2329 }
2330 t1 = parse_type (NULL);
2331 if (*parse_ptr == ':')
2332 {
2333 /* Static member */
2334
2335 ++parse_ptr;
2336 tf->flags |= MEMBER_STATIC;
2337 p1 = strchr (parse_ptr, ';');
2338 if (p1 == NULL)
2339 {
2340 warning ("Invalid static member: missing semicolon");
2341 goto syntax;
2342 }
2343 n = p1 - parse_ptr;
2344 tf->sname = strpool_addn (str_pool, parse_ptr, n);
2345 parse_ptr = p1 + 1;
2346 tf->offset = 0;
2347 }
2348 else
2349 {
2350 if (!parse_char (',') || !parse_number (&offset))
2351 goto syntax;
2352 if (*parse_ptr == ',')
2353 {
2354 ++parse_ptr;
2355 if (!parse_number (&width))
2356 goto syntax;
2357 }
2358 else
2359 width = 0; /* vtable pointer field */
2360 if (!parse_char (';'))
2361 goto syntax;
2362 if ((offset & 7) != 0 || width != 8 * type_size (t1))
2363 {
2364 t.tag = ty_bits;
2365 t.d.bits.type = t1;
2366 t.d.bits.start = offset & 7;
2367 t.d.bits.count = width;
2368 t1 = type_add (&t);
2369 }
2370 tf->offset = offset >> 3;
2371 }
2372 tf->type = t1;
2373 }
2374 }
2375 ++parse_ptr;
2376 if (*parse_ptr == '~')
2377 {
2378 /* Type reference to the first base class. This field is
2379 present for classes which contain virtual functions. */
2380
2381 ++parse_ptr;
2382 if (!parse_char ('%'))
2383 goto syntax;
2384 t1 = parse_type (NULL);
2385 if (!parse_char (';'))
2386 goto syntax;
2387 }
2388
2389 if (class_flag)
2390 {
2391 tlist = xmalloc (g1.count * sizeof (*t.d.types.list));
2392 for (i = 0; i < g1.count; ++i)
2393 {
2394 if (tmp_fields[i].flags & MEMBER_FUNCTION)
2395 {
2396 t2 = tmp_fields[i].type;
2397 tt = follow_refs (t2)->tag;
2398 /* TODO: check later if ty_stabs_ref */
2399 if (tt != ty_func && tt != ty_stabs_ref)
2400 error ("Invalid type for member function %s",
2401 tmp_fields[i].name);
2402 t.tag = ty_memfunc;
2403 t.d.memfunc.type = t2;
2404 t.d.memfunc.name = tmp_fields[i].name;
2405 t.d.memfunc.mnglname = tmp_fields[i].mnglname;
2406 t.d.memfunc.flags = tmp_fields[i].flags;
2407 t.d.memfunc.offset = tmp_fields[i].offset;
2408 }
2409 else if (tmp_fields[i].flags & MEMBER_BASE)
2410 {
2411 t.tag = ty_baseclass;
2412 t.d.baseclass.type = tmp_fields[i].type;
2413 t.d.baseclass.flags = tmp_fields[i].flags;
2414 t.d.baseclass.offset = tmp_fields[i].offset;
2415 }
2416 else
2417 {
2418 t.tag = ty_member;
2419 t.d.member.type = tmp_fields[i].type;
2420 t.d.member.offset = tmp_fields[i].offset;
2421 t.d.member.flags = tmp_fields[i].flags;
2422 t.d.member.name = tmp_fields[i].name;
2423 t.d.member.sname = tmp_fields[i].sname;
2424 }
2425 t2 = type_add (&t);
2426 tlist[i] = t2;
2427 }
2428 t.tag = ty_types;
2429 t.d.types.count = g1.count;
2430 t.d.types.list = tlist;
2431 t1 = type_add (&t);
2432 free (tlist);
2433
2434 t.tag = ty_class;
2435 t.d.class.members = t1;
2436 t.d.class.size = size;
2437 t.d.class.count = g1.count;
2438 t.d.class.name = add_struct_name (type_name);
2439
2440 /* Check if there is an incomplete type waiting to be filled
2441 in (forward reference). We're ignoring scoping (TODO). */
2442
2443 for (t3 = type_head; t3 != NULL; t3 = t3->next)
2444 if (t3->tag == ty_struc && t3->d.struc.name == t.d.class.name
2445 /*&& t3->d.struc.types == NULL && t3->d.struc.fields == NULL */
2446 && (t3->d.struc.flags & STRUC_FORWARD))
2447 {
2448 if (t3->index != -1)
2449 warning ("This cannot happen, case 1");
2450 result = t3;
2451 t3->tag = ty_class;
2452 t3->d.class = t.d.class;
2453 break;
2454 }
2455 }
2456 else
2457 {
2458 t.tag = ty_types;
2459 t.d.types.count = g1.count;
2460 t.d.types.list = xmalloc (g1.count * sizeof (*t.d.types.list));
2461 for (i = 0; i < g1.count; ++i)
2462 t.d.types.list[i] = tmp_fields[i].type;
2463 t1 = type_add (&t);
2464 free (t.d.types.list);
2465
2466 t.tag = ty_fields;
2467 t.d.fields.count = g1.count;
2468 t.d.fields.list = xmalloc (g1.count * sizeof (*t.d.fields.list));
2469 for (i = 0; i < g1.count; ++i)
2470 {
2471 t.d.fields.list[i].offset = tmp_fields[i].offset;
2472 t.d.fields.list[i].name = tmp_fields[i].name;
2473 }
2474 t2 = type_add (&t);
2475 free (t.d.fields.list);
2476
2477 t.tag = ty_struc;
2478 t.d.struc.count = g1.count;
2479 t.d.struc.size = size;
2480 t.d.struc.types = t1;
2481 t.d.struc.fields = t2;
2482 t.d.struc.name = add_struct_name (type_name);
2483 t.d.struc.flags = 0;
2484
2485 /* Check if there is an incomplete type waiting to be filled
2486 in (forward reference). We're ignoring scoping (TODO). */
2487
2488 for (t3 = type_head; t3 != NULL; t3 = t3->next)
2489 if (t3->tag == ty_struc && t3->d.struc.name == t.d.struc.name
2490 /* && t3->d.struc.types == NULL && t3->d.struc.fields == NULL */
2491 && (t3->d.struc.flags & STRUC_FORWARD))
2492 {
2493 if (t3->index != -1)
2494 warning ("This cannot happen, case 1");
2495 result = t3;
2496 t3->d.struc = t.d.struc;
2497 break;
2498 }
2499 }
2500 grow_free (&g1);
2501 break;
2502
2503 case 'e':
2504
2505 /* Enumeration type: e{<name>:<value>,}; */
2506
2507 ++parse_ptr;
2508 grow_init (&g1, &tmp_values, sizeof (*tmp_values), 8);
2509 while (*parse_ptr != ';')
2510 {
2511 p1 = nextcolon (parse_ptr);
2512 if (p1 == NULL)
2513 {
2514 warning ("Invalid enum type: missing colon");
2515 goto syntax;
2516 }
2517 n = p1 - parse_ptr;
2518 grow_by (&g1, 1);
2519 tv = &tmp_values[g1.count++];
2520 tv->name = strpool_addn (str_pool, parse_ptr, n);
2521 parse_ptr = p1 + 1;
2522 if (!parse_number (&tv->index) || !parse_char (','))
2523 goto syntax;
2524 }
2525 ++parse_ptr;
2526 t.tag = ty_values;
2527 t.d.values.count = g1.count;
2528 t.d.values.list = xmalloc (g1.count * sizeof (*t.d.values.list));
2529 if (g1.count == 0)
2530 lo = hi = 0;
2531 else
2532 lo = hi = tmp_values[0].index;
2533 for (i = 0; i < g1.count; ++i)
2534 {
2535 t.d.values.list[i].index = tmp_values[i].index;
2536 t.d.values.list[i].name = tmp_values[i].name;
2537 if (tmp_values[i].index < lo)
2538 lo = tmp_values[i].index;
2539 if (tmp_values[i].index > hi)
2540 hi = tmp_values[i].index;
2541 }
2542 t1 = type_add (&t);
2543 free (t.d.values.list);
2544
2545 t.tag = ty_prim;
2546 t.index = 0x82; /* 32 bit signed */
2547 t2 = type_add (&t);
2548
2549 t.tag = ty_enu;
2550 t.index = -1;
2551 t.d.enu.values = t1;
2552 t.d.enu.type = t2;
2553 t.d.enu.name = add_struct_name (type_name);
2554 t.d.enu.first = lo;
2555 t.d.enu.last = hi;
2556 grow_free (&g1);
2557 break;
2558
2559 case 'a':
2560 /* An array: ar<index_type>;<lower_bound>;<upper_bound>;<el_type> */
2561
2562 ++parse_ptr;
2563 if (!parse_char ('r'))
2564 goto syntax;
2565 if (strncmp (parse_ptr, "0;", 2) == 0)
2566 {
2567 /* This invalid type index seems to be caused by a bug of
2568 GCC 2.8.1. Replace with a 32-bit signed integer. */
2569
2570 t.tag = ty_prim;
2571 t.index = 0x82; /* 32 bit signed */
2572 t1 = type_add (&t);
2573 ++parse_ptr;
2574 }
2575 else
2576 t1 = parse_type (NULL); /* Index type */
2577 if (!parse_char (';')
2578 || !parse_number (&lo) || !parse_char (';') /* Lower bound */
2579 || !parse_number (&hi) || !parse_char (';')) /* Upper bound */
2580 goto syntax;
2581 t2 = parse_type (NULL); /* Elements type */
2582 if (lo == 0 && hi == 0) /* Variable-length array */
2583 {
2584 t.tag = ty_pointer; /* Turn into pointer */
2585 t.d.pointer = t2;
2586 }
2587 else
2588 {
2589 t.tag = ty_array;
2590 t.d.array.first = lo;
2591 t.d.array.last = hi;
2592 t.d.array.itype = t1;
2593 t.d.array.etype = t2;
2594 }
2595 break;
2596
2597 case 'f':
2598
2599 /* A function: f<return_type> */
2600
2601 ++parse_ptr;
2602 t1 = parse_type (NULL); /* Return type */
2603 t.tag = ty_func;
2604 t.d.func.ret = t1;
2605 t.d.func.domain = NULL;
2606 t.d.func.args = NULL;
2607 t.d.func.arg_count = 0;
2608 break;
2609
2610 default:
2611 no_warning ("Unknown type: %c", *parse_ptr);
2612 goto syntax;
2613 }
2614
2615 /* Add the type to the tables. */
2616
2617 if (result == NULL)
2618 result = type_add (&t);
2619 return result;
2620
2621syntax:
2622 no_warning ("syntax error in stabs: %c (off %ld)\n stabs: %s",
2623 *parse_ptr, parse_ptr - parse_start, parse_start);
2624
2625 grow_free (&g1);
2626 t.tag = ty_prim;
2627 t.index = 0x82; /* 32 bit signed */
2628 return type_add (&t);
2629}
2630
2631
2632/* Parse a stabs type (the name of the type is TYPE_NAME, which is
2633 NULL for an unnamed type) and check for end of the string. If not
2634 all characters of the string have been parsed, a warning message is
2635 displayed. Return the internal representation of the type. */
2636
2637static struct type *parse_complete_type (const char *type_name)
2638{
2639 struct type *tp;
2640
2641 tp = parse_type (type_name);
2642 if (*parse_ptr != 0)
2643 no_warning ("unexpected character at end of stabs type: %c (off %ld)\n stabs: %s",
2644 *parse_ptr, parse_ptr - parse_start, parse_start);
2645 return tp;
2646}
2647
2648
2649/* Create an HLL type number for type TP. If a type number has already
2650 been assigned to TP, use that number. Otherwise, allocate a new
2651 (complex) type number and define the type in the $$TYPES segment.
2652 The HLL type number us stored to *HLL.
2653
2654 Note: This function must not be called between sst_start() and
2655 sst_end() or tt_start() and tt_end() as entries are made both in
2656 the SST ($$SYMBOLS) and the type table ($$TYPES). */
2657
2658static void make_type (struct type *tp, int *hll)
2659{
2660 struct type *t1;
2661 int ti_1, ti_2, ti_3, i, qual;
2662 int patch1;
2663
2664#define RETURN(X) do {*hll = (X); return;} while (0)
2665
2666 if (tp->index <= -2)
2667 {
2668 /* bird: A hack where we allow a 2nd recursion of rectain types.
2669 * The normal issue is C++ class references. */
2670 if (tp->tag != ty_alias || tp->index <= -3)
2671 {
2672 warning_parse ("Cycle detected by make_type.");
2673 RETURN (0);
2674 }
2675 }
2676 if (tp->index > -1)
2677 RETURN (tp->index);
2678 tp->index--;
2679 switch (tp->tag)
2680 {
2681 case ty_stabs_ref:
2682 t1 = stype_find (tp->d.stabs_ref);
2683 if (t1 == NULL)
2684 {
2685 warning_parse ("Undefined stabs type %d referenced", tp->d.stabs_ref);
2686 RETURN (0x82); /* 32 bit signed */
2687 }
2688 make_type (t1, &tp->index);
2689 *hll = tp->index;
2690 break;
2691
2692 case ty_alias:
2693 make_type (tp->d.alias, &tp->index);
2694 *hll = tp->index;
2695 break;
2696
2697 case ty_pointer:
2698 ti_1 = tp->d.pointer->index;
2699 if (ti_1 >= 0x80 && ti_1 <= 0xa0)
2700 {
2701 tp->index = ti_1 + 0x20;
2702 RETURN (tp->index);
2703 }
2704 *hll = tp->index = hll_type_index++;
2705 tt_start (0x7a, 0x01); /* 32-bit near pointer */
2706 patch1 = tt.size;
2707 tt_index (0);
2708 tt_end ();
2709 make_type (tp->d.pointer, &ti_1);
2710 buffer_patch_word (&tt, patch1+1, ti_1);
2711 break;
2712
2713 case ty_ref:
2714 *hll = tp->index = hll_type_index++;
2715 tt_start (0x48, 0x00); /* Reference type (C++) */
2716 patch1 = tt.size;
2717 tt_word (0);
2718 tt_end ();
2719 make_type (tp->d.ref, &ti_1);
2720 buffer_patch_word (&tt, patch1, ti_1);
2721 break;
2722
2723 case ty_struc:
2724 {
2725 struct field *fields = NULL;
2726 struct type **types = NULL;
2727 int cFields = tp->d.struc.count;
2728
2729 if ( tp->d.struc.fields
2730 && tp->d.struc.fields->tag == ty_fields
2731 && tp->d.struc.fields->d.fields.count == cFields)
2732 fields = tp->d.struc.fields->d.fields.list;
2733 if ( tp->d.struc.types
2734 && tp->d.struc.types->tag == ty_types
2735 && tp->d.struc.types->d.types.count == cFields)
2736 types = tp->d.struc.types->d.types.list;
2737
2738 if (cplusplus_flag && ((types && fields) || !cFields))
2739 {
2740 struct type t;
2741 struct type **list;
2742 int i;
2743 *hll = tp->index = hll_type_index++;
2744 tt_start (0x40, 0x01); /* Class is-struct */
2745 tt_dword (tp->d.struc.size);
2746 tt_word (tp->d.struc.count);
2747 patch1 = tt.size;
2748 tt_word (0); /* Members */
2749 tt_enc (tp->d.struc.name); /* Class name */
2750 tt_end ();
2751
2752 list = xmalloc (cFields * sizeof (*list));
2753 for (i = 0; i < cFields; ++i)
2754 {
2755 t.tag = ty_member;
2756 t.index = -1;
2757 t.d.member.type = types[i];
2758 t.d.member.offset = fields[i].offset;
2759 t.d.member.flags = VIS_PUBLIC;
2760 t.d.member.name = fields[i].name;
2761 t.d.member.sname = NULL;
2762 list[i] = type_add (&t);
2763 }
2764
2765 t.tag = ty_types;
2766 t.index = -1;
2767 t.d.types.count = cFields;
2768 t.d.types.list = list;
2769 t1 = type_add (&t);
2770 free (list);
2771
2772 make_type (t1, &ti_1);
2773 buffer_patch_word (&tt, patch1, ti_1);
2774 }
2775 else
2776 {
2777 tt_start (0x79, 0x00); /* Structure/Union */
2778 tt_dword (tp->d.struc.size);
2779 tt_word (tp->d.struc.count);
2780 patch1 = tt.size;
2781 tt_index (0);
2782 tt_index (0);
2783 tt_name (tp->d.struc.name);
2784 tt_end ();
2785 *hll = tp->index = hll_type_index++;
2786 if (tp->d.struc.types == NULL && tp->d.struc.fields == NULL)
2787 {
2788 ti_1 = 0;
2789 ti_2 = 1;
2790 }
2791 else
2792 {
2793 make_type (tp->d.struc.types, &ti_1);
2794 make_type (tp->d.struc.fields, &ti_2);
2795 }
2796 buffer_patch_word (&tt, patch1+1, ti_1);
2797 buffer_patch_word (&tt, patch1+4, ti_2);
2798 }
2799 break;
2800 }
2801
2802 case ty_class:
2803 *hll = tp->index = hll_type_index++;
2804 tt_start (0x40, 0x00); /* Class */
2805 tt_dword (tp->d.class.size);
2806 tt_word (tp->d.class.count);
2807 patch1 = tt.size;
2808 tt_word (0); /* Members */
2809 tt_enc (tp->d.class.name); /* Class name */
2810 tt_end ();
2811 make_type (tp->d.class.members, &ti_1);
2812 buffer_patch_word (&tt, patch1, ti_1);
2813 break;
2814
2815 case ty_member:
2816 qual = 0;
2817 if (tp->d.member.flags & MEMBER_STATIC)
2818 qual |= 0x01;
2819 tt_start (0x46, qual);
2820 tt_byte (tp->d.member.flags & MEMBER_VIS);
2821 patch1 = tt.size;
2822 tt_word (0);
2823 tt_unsigned_span (tp->d.member.offset);
2824 if (tp->d.member.flags & MEMBER_STATIC)
2825 tt_enc (tp->d.member.sname);
2826 else
2827 tt_enc ("");
2828 tt_enc (tp->d.member.name);
2829 tt_end ();
2830 *hll = tp->index = hll_type_index++;
2831
2832 make_type (tp->d.member.type, &ti_1);
2833 buffer_patch_word (&tt, patch1, ti_1);
2834 break;
2835
2836 case ty_enu:
2837 make_type (tp->d.enu.type, &ti_1);
2838 make_type (tp->d.enu.values, &ti_2);
2839 tt_start (0x7b, 0x00); /* Enum */
2840 tt_index (ti_1); /* Element's data type */
2841 tt_index (ti_2); /* List of values */
2842 tt_signed_span (tp->d.enu.first); /* Minimum index */
2843 tt_signed_span (tp->d.enu.last); /* Maximum index */
2844 tt_name (tp->d.enu.name);
2845 tt_end ();
2846 *hll = tp->index = hll_type_index++;
2847 break;
2848
2849 case ty_array:
2850 make_type (tp->d.array.itype, &ti_1);
2851 make_type (tp->d.array.etype, &ti_2);
2852 tt_start (0x78, 0x00); /* Array */
2853 tt_dword (type_size (tp));
2854 tt_index (ti_1);
2855 tt_index (ti_2);
2856 tt_name (""); /* Name */
2857 tt_end ();
2858 *hll = tp->index = hll_type_index++;
2859 break;
2860
2861 case ty_bits:
2862 tt_start (0x5c, 0x0a); /* Bit string */
2863 tt_byte (tp->d.bits.start);
2864 tt_unsigned_span (tp->d.bits.count);
2865 tt_end ();
2866 *hll = tp->index = hll_type_index++;
2867 break;
2868
2869 case ty_func:
2870 tt_start (0x54, 0x05); /* Function */
2871 tt_word (tp->d.func.arg_count); /* Number of parameters */
2872 tt_word (tp->d.func.arg_count); /* Max. Number of parameters */
2873 patch1 = tt.size;
2874 tt_index (0); /* Return value */
2875 tt_index (0); /* Argument list */
2876 tt_end ();
2877 *hll = tp->index = hll_type_index++;
2878
2879 if (tp->d.func.domain)
2880 make_type (tp->d.func.domain, &ti_3);
2881 make_type (tp->d.func.ret, &ti_1);
2882 if (tp->d.func.args == NULL)
2883 ti_2 = 0;
2884 else
2885 make_type (tp->d.func.args, &ti_2);
2886 buffer_patch_word (&tt, patch1+1, ti_1);
2887 buffer_patch_word (&tt, patch1+4, ti_2);
2888 break;
2889
2890 case ty_args:
2891 if (tp->d.args.count == 0)
2892 {
2893 tp->index = 0;
2894 RETURN (tp->index);
2895 }
2896 for (i = 0; i < tp->d.args.count; ++i)
2897 make_type (tp->d.args.list[i], &ti_1);
2898 tt_start (0x7f, 0x04); /* List (function parameters) */
2899 for (i = 0; i < tp->d.args.count; ++i)
2900 {
2901 tt_byte (0x01); /* Flag: pass by value, no descriptor */
2902 tt_index (tp->d.args.list[i]->index);
2903 }
2904 tt_end ();
2905 *hll = tp->index = hll_type_index++;
2906 break;
2907
2908 case ty_types:
2909 if (tp->d.types.count == 0)
2910 {
2911 tp->index = 0;
2912 RETURN (tp->index);
2913 }
2914 *hll = tp->index = hll_type_index++;
2915 tt_start (0x7f, 0x01); /* List (structure field types) */
2916 patch1 = tt.size;
2917 for (i = 0; i < tp->d.types.count; ++i)
2918 tt_index (0);
2919 tt_end ();
2920 for (i = 0; i < tp->d.types.count; ++i)
2921 {
2922 make_type (tp->d.types.list[i], &ti_1);
2923 buffer_patch_word (&tt, patch1 + 1 + 3*i, ti_1);
2924 }
2925 break;
2926
2927 case ty_fields:
2928 if (tp->d.fields.count == 0)
2929 {
2930 tp->index = 0;
2931 RETURN (tp->index);
2932 }
2933 tt_start (0x7f, 0x02); /* List (structure field names) */
2934 for (i = 0; i < tp->d.fields.count; ++i)
2935 {
2936 tt_name (tp->d.fields.list[i].name);
2937 tt_unsigned_span (tp->d.fields.list[i].offset);
2938 }
2939 tt_end ();
2940 *hll = tp->index = hll_type_index++;
2941 break;
2942
2943 case ty_values:
2944 if (tp->d.values.count == 0)
2945 {
2946 tp->index = -1;
2947 RETURN (tp->index);
2948 }
2949 tt_start (0x7f, 0x03); /* Enum values */
2950 for (i = 0; i < tp->d.values.count; ++i)
2951 {
2952 tt_name (tp->d.values.list[i].name);
2953 tt_unsigned_span (tp->d.values.list[i].index);
2954 }
2955 tt_end ();
2956 *hll = tp->index = hll_type_index++;
2957 break;
2958
2959 case ty_memfunc:
2960 make_type (tp->d.memfunc.type, &ti_1);
2961 qual = 0;
2962 if (tp->d.memfunc.flags & MEMBER_STATIC)
2963 qual |= 0x01;
2964 if (tp->d.memfunc.flags & MEMBER_CONST)
2965 qual |= 0x04;
2966 if (tp->d.memfunc.flags & MEMBER_VOLATILE)
2967 qual |= 0x08;
2968 if (tp->d.memfunc.flags & MEMBER_VIRTUAL)
2969 qual |= 0x10;
2970 tt_start (0x45, qual); /* Member Function */
2971 tt_byte (tp->d.memfunc.flags & MEMBER_VIS); /* Protection */
2972 if (tp->d.memfunc.flags & MEMBER_CTOR)
2973 tt_byte (1); /* Constructor */
2974 else if (tp->d.memfunc.flags & MEMBER_DTOR)
2975 tt_byte (2); /* Destructor */
2976 else
2977 tt_byte (0); /* Regular member function */
2978 tt_word (ti_1); /* Type index of function */
2979 if (tp->d.memfunc.flags & MEMBER_VIRTUAL)
2980 tt_unsigned_span (tp->d.memfunc.offset); /* Index into vtable */
2981 tt_enc (tp->d.memfunc.name); /* Name of the function */
2982 tt_end ();
2983 *hll = tp->index = hll_type_index++;
2984 break;
2985
2986 case ty_baseclass:
2987 make_type (tp->d.baseclass.type, &ti_1);
2988 qual = 0;
2989 if (tp->d.baseclass.flags & MEMBER_VIRTUAL)
2990 qual |= 0x01;
2991 tt_start (0x41, qual); /* Base Class */
2992 tt_byte (tp->d.baseclass.flags & MEMBER_VIS);
2993 tt_word (ti_1); /* Type */
2994 tt_unsigned_span (tp->d.baseclass.offset);
2995 tt_end ();
2996 *hll = tp->index = hll_type_index++;
2997 break;
2998
2999 default:
3000 error ("make_type: unknown tag %d", tp->tag);
3001 }
3002}
3003
3004
3005/* Parse an unnamed stabs type and return an HLL type index for it. */
3006
3007static int hll_type (void)
3008{
3009 struct type *tp;
3010 int ti;
3011
3012 tp = parse_complete_type (NULL);
3013 make_type (tp, &ti);
3014 return ti;
3015}
3016
3017
3018/* Return the symbol name of symbol *INDEX, concatenating the names of
3019 successive symbol table entries if necessary. A pointer to the
3020 string is stored to *STR. If symbols are concatenated, *INDEX is
3021 adjusted to refer to the last symbol table entry used, in order to
3022 refer to the next symbol after incrementing by one. If TRUE is
3023 returned, the caller should free the string when it is no longer
3024 needed. If FALSE is returned, the string must not be freed. */
3025
3026static int concat_symbols (int *index, const char **str)
3027{
3028 int i, n, len;
3029 char *new;
3030 const char *p;
3031
3032 len = 0;
3033 for (i = *index; i < sym_count; ++i)
3034 {
3035 p = str_ptr + sym_ptr[i].n_un.n_strx;
3036 n = strlen (p);
3037 if (n > 0 && p[n-1] == '\\')
3038 len += n - 1;
3039 else
3040 {
3041 len += n;
3042 break;
3043 }
3044 }
3045 if (i == *index)
3046 {
3047 *str = str_ptr + sym_ptr[i].n_un.n_strx;
3048 return FALSE;
3049 }
3050 else
3051 {
3052 new = xmalloc (len + 1);
3053 *str = new;
3054 for (i = *index; i < sym_count; ++i)
3055 {
3056 p = str_ptr + sym_ptr[i].n_un.n_strx;
3057 n = strlen (p);
3058 if (n > 0 && p[n-1] == '\\')
3059 {
3060 memcpy (new, p, n - 1);
3061 new += n - 1;
3062 }
3063 else
3064 {
3065 memcpy (new, p, n);
3066 new += n;
3067 break;
3068 }
3069 }
3070 *new = 0;
3071 *index = i;
3072 return TRUE;
3073 }
3074}
3075
3076
3077/* Parse a stabs type definition and store the internal representation
3078 of that type. The sym_ptr index is passed in INDEX. As we might
3079 have to concatenate successive symbol table entries, *INDEX is
3080 updated to refer to the next symbol. */
3081
3082static void parse_typedef (int *index)
3083{
3084 char *name;
3085 const char *str, *p;
3086 struct type *t;
3087 int n, alloc_flag;
3088
3089 alloc_flag = concat_symbols (index, &str);
3090 parse_start = str;
3091 parse_pindex = index;
3092 p = nextcolon (str);
3093 if (p != NULL)
3094 {
3095 n = p - str;
3096 name = alloca (n + 1);
3097 memcpy (name, str, n);
3098 name[n] = 0;
3099#if defined (HLL_DEBUG)
3100 printf ("LSYM/LCSYM/GSYM/PSYM/RSYM/STSYM/FUN %s\n", str);
3101#endif
3102 parse_ptr = p + 1;
3103 switch (*parse_ptr)
3104 {
3105 case 'f':
3106 case 'F':
3107 ++parse_ptr;
3108 /* Note: don't call parse_complete_type() as there's a comma
3109 at the end for nested functions. */
3110 t = parse_type (NULL);
3111 break;
3112
3113 case 't':
3114 case 'T':
3115 ++parse_ptr;
3116 if (*parse_ptr == 't')
3117 ++parse_ptr; /* synonymous type */
3118 t = parse_complete_type (name);
3119#if defined (HLL_DEBUG)
3120 printf (" type: ");
3121 show_type (t);
3122 printf ("\n");
3123#endif
3124 break;
3125
3126 case 'p':
3127 case 'r':
3128 case 'G':
3129 case 'S':
3130 case 'V':
3131 ++parse_ptr;
3132 t = parse_complete_type (name);
3133#if defined (HLL_DEBUG)
3134 printf (" type: ");
3135 show_type (t);
3136 printf ("\n");
3137#endif
3138 break;
3139
3140 case '0': case '1': case '2': case '3': case '4':
3141 case '5': case '6': case '7': case '8': case '9':
3142 t = parse_complete_type (name);
3143#if defined (HLL_DEBUG)
3144 printf (" type: ");
3145 show_type (t);
3146 printf ("\n");
3147#endif
3148 break;
3149 }
3150 }
3151 if (alloc_flag)
3152 free ((void *)str); /* remove const attribute */
3153}
3154
3155
3156/* Write a `begin' subrecord to the SST. The block begins at ADDR in
3157 the text segment. */
3158
3159static size_t hll_begin (dword addr)
3160{
3161 size_t ptr;
3162 struct relocation_info r;
3163
3164 sst_start (SST_begin);
3165 r.r_address = sst.size;
3166 ptr = sst.size;
3167 buffer_dword (&sst, addr); /* Segment offset */
3168 buffer_dword (&sst, 0); /* Length of block -- patched later */
3169#if 1
3170 buffer_nstr (&sst, ""); /* Name of block (optional) */
3171#endif
3172 sst_end ();
3173
3174 r.r_extern = 0;
3175 r.r_length = 2;
3176 r.r_symbolnum = N_TEXT;
3177 r.r_pcrel = 0;
3178 r.r_pad = 0;
3179 buffer_mem (&sst_reloc, &r, sizeof (r));
3180 return ptr;
3181}
3182
3183
3184/* Start of a block. Write a `begin' subrecord to the SST. The block
3185 begins at ADDR in the text segment. Push the block onto the block
3186 stack. */
3187
3188static void block_begin (dword addr)
3189{
3190 size_t ptr;
3191
3192 ptr = hll_begin (addr);
3193 grow_by (&block_grow, 1);
3194 block_stack[block_grow.count].patch_ptr = ptr;
3195 block_stack[block_grow.count].addr = addr;
3196 ++block_grow.count;
3197}
3198
3199
3200/* Write an `end' subrecord to the SST. PATCH_PTR is the sst index of
3201 the `begin' subrecord for this block. Add ADD_START to the start
3202 address of the block and set the length of the block to LENGTH, in
3203 bytes. */
3204
3205static void hll_end (size_t patch_ptr, int add_start, size_t length)
3206{
3207 *(dword *)(sst.buf+patch_ptr+0) += add_start;
3208 *(dword *)(sst.buf+patch_ptr+4) = length;
3209 sst_start (SST_end);
3210 sst_end ();
3211}
3212
3213
3214/* Return TRUE iff symbol table entry S defines a function. */
3215
3216static int is_fun (const struct nlist *s)
3217{
3218 const char *p;
3219
3220 if (s->n_type != N_FUN)
3221 return FALSE;
3222 p = nextcolon (str_ptr + s->n_un.n_strx);
3223 if (p == NULL)
3224 return FALSE;
3225 return (p[1] == 'F' || p[1] == 'f');
3226}
3227
3228
3229/* Approximate a function prologue length by examining the code. The
3230 function starts at START and ends at END. */
3231
3232static int get_prologue_length (dword start, dword end)
3233{
3234 dword i;
3235
3236 if (end > text_size)
3237 end = text_size;
3238 i = start;
3239 if (i < end && text_ptr[i] == 0x55) ++i; /* PUSH EBP */
3240 if (i + 1 < end && text_ptr[i+0] == 0x89
3241 && text_ptr[i+1] == 0xe5) /* MOV EBP, ESP */
3242 i += 2;
3243 if (i + 2 < end && text_ptr[i+0] == 0x83
3244 && text_ptr[i+1] == 0xec) /* SUB ESP, n8 */
3245 i += 3;
3246 return i - start;
3247}
3248
3249
3250/* End of a function reached. Try to find the function length. I is
3251 the sym_ptr index of the current symbol -- we search the symbol
3252 table starting at I to find the next function for estimating the
3253 function length. */
3254
3255static void fun_end (int i)
3256{
3257 int p_len;
3258 dword addr;
3259
3260 addr = text_size;
3261 while (i < sym_count)
3262 {
3263 if (is_fun (&sym_ptr[i]))
3264 {
3265 addr = sym_ptr[i].n_value;
3266 break;
3267 }
3268 ++i;
3269 }
3270 if (prologue_length != -1)
3271 p_len = prologue_length;
3272 else
3273 p_len = get_prologue_length (proc_start_addr, addr);
3274 *(dword *)(sst.buf+proc_patch_base + 0) = addr - proc_start_addr;
3275 *(word *)(sst.buf+proc_patch_base + 4) = p_len;
3276 *(dword *)(sst.buf+proc_patch_base + 6) = addr - proc_start_addr;
3277 proc_patch_base = 0;
3278 sst_start (SST_end);
3279 sst_end ();
3280}
3281
3282
3283/* End of a block (N_RBRAC encountered). I is the sym_ptr index of
3284 the N_RBRAC symbol. If we reach the outmost block level, we call
3285 fun_end(). */
3286
3287static void block_end (int i)
3288{
3289 if (block_grow.count == 0)
3290 {
3291 /** @todo fix this - broken after my last change (bird 2003-10-06) */
3292 /* warning ("Invalid N_RBRAC without N_LBRAC"); */
3293 }
3294 else
3295 {
3296 --block_grow.count;
3297 hll_end (block_stack[block_grow.count].patch_ptr, 0,
3298 sym_ptr[i].n_value - block_stack[block_grow.count].addr);
3299 if (block_grow.count == 1) /* we make one extra begin */
3300 {
3301 --block_grow.count;
3302 hll_end (block_stack[block_grow.count].patch_ptr, 0,
3303 sym_ptr[i].n_value - block_stack[block_grow.count].addr);
3304 }
3305 if (block_grow.count == 0 && proc_patch_base != 0)
3306 fun_end (i);
3307 }
3308}
3309
3310
3311/* Define a function. SYMBOL points to the sym_ptr entry. The
3312 function arguments are taken from the args_list array. Several
3313 fields of the SST entry are filled-in later, when reaching the end
3314 of the function. proc_patch_base is used for remembering the place
3315 where to patch the SST entry. */
3316
3317static void define_fun (const struct nlist *symbol)
3318{
3319 struct type t, *t1, *t2, *t3;
3320 struct relocation_info r;
3321 const char *str, *p, *p2;
3322 const char *name, *mnglname;
3323#ifdef DEMANGLE_PROC_NAMES
3324 char *pszFree;
3325#endif
3326 size_t cchName, cchMnglName;
3327 int ti, ticlass;
3328
3329 str = str_ptr + symbol->n_un.n_strx;
3330 p = nextcolon (str);
3331 if (p == NULL)
3332 abort ();
3333 cchName = cchMnglName = p - str;
3334 name = mnglname = strpool_addn (str_pool, str, cchMnglName);
3335 proc_start_addr = symbol->n_value;
3336
3337#ifdef DEMANGLE_PROC_NAMES
3338 /* demangle the name */
3339 pszFree = cplus_demangle (mnglname, DMGL_ANSI | DMGL_PARAMS);
3340 if (!pszFree && (*mnglname == '_' || *mnglname == '*'))
3341 pszFree = cplus_demangle (mnglname + 1, DMGL_ANSI | DMGL_PARAMS);
3342 if (pszFree)
3343 {
3344 cchName = strlen (pszFree);
3345 name = pszFree;
3346 }
3347 else if (*mnglname == '*')
3348 {
3349 cchName--;
3350 name++;
3351 }
3352#endif
3353
3354 /* now let's see if there is a memfunc for this name. */
3355 for (t1 = type_head; t1; t1 = t1->next)
3356 if ( t1->tag == ty_memfunc
3357 && t1->d.memfunc.mnglname == mnglname)
3358 break;
3359
3360 if (t1)
3361 { /* C++ member function */
3362 t2 = t1->d.memfunc.type;
3363 if (t2 && t2->tag == ty_func && t2->d.func.domain)
3364 make_type (t2->d.func.domain, &ticlass);
3365 else
3366 { /* the hard way - search for the freaking string in suitable .stabs.
3367 * I think this is rather slow when done for many members.. :/ */
3368 int i;
3369 for (i = 0, t3 = NULL, ticlass = -1; !t3 && i < sym_count; ++i)
3370 switch (sym_ptr[i].n_type)
3371 {
3372 case N_LSYM:
3373 {
3374 str = str_ptr + sym_ptr[i].n_un.n_strx;
3375 p = nextcolon (str);
3376 if (p && p[1] == 'T' && p[2] == 't')
3377 {
3378 /* Now, there can be several name which NAME is a
3379 * substring in.
3380 */
3381 /** @todo: the string parsing still is a bit spooky. */
3382 for (p2 = p; (p2 = strstr (p2, mnglname)) != NULL; p2 += cchMnglName)
3383 if (p2[-1] == ':' && p2[cchMnglName] == ';')
3384 {
3385 int stab = atoi (&p[3]);
3386 t3 = stype_find (stab);
3387 break;
3388 }
3389 }
3390 break;
3391 }
3392 }
3393 if (t3)
3394 make_type (t3, &ticlass);
3395 if (ticlass < 0)
3396 {
3397 warning (" Can't figure out which class method '%s' is a member of!", mnglname);
3398 ticlass = 0;
3399 }
3400 }
3401
3402 make_type (t1, &ti);
3403 sst_start (SST_memfunc);
3404 r.r_address = sst.size;
3405 buffer_dword (&sst, symbol->n_value); /* Segment offset */
3406 buffer_word (&sst, ti); /* Type index */
3407 proc_patch_base = sst.size;
3408 buffer_dword (&sst, 0); /* Length of proc */
3409 buffer_word (&sst, 0); /* Length of prologue */
3410 buffer_dword (&sst, 0); /* Length of prologue and body */
3411 buffer_word (&sst, ticlass); /* Class type (for member functions) */
3412 buffer_byte (&sst, 8); /* 32-bit near */
3413 buffer_enc (&sst, name); /* Proc name */
3414 sst_end ();
3415 }
3416 else
3417 {
3418 if (args_grow.count == 0)
3419 t1 = NULL;
3420 else
3421 {
3422 t.tag = ty_args;
3423 t.index = -1;
3424 t.d.args.count = args_grow.count;
3425 t.d.args.list = xmalloc (args_grow.count * sizeof (*args_list));
3426 memcpy (t.d.args.list, args_list, args_grow.count * sizeof (*args_list));
3427 t1 = type_add (&t);
3428 free (t.d.args.list);
3429 }
3430 last_fun_type.d.func.args = t1;
3431 last_fun_type.d.func.arg_count = args_grow.count;
3432 t2 = type_add (&last_fun_type);
3433 make_type (t2, &ti);
3434
3435 sst_start (cplusplus_flag || cchName > 255 ? SST_CPPproc : SST_proc);
3436 r.r_address = sst.size;
3437 buffer_dword (&sst, symbol->n_value); /* Segment offset */
3438 buffer_word (&sst, ti); /* Type index */
3439 proc_patch_base = sst.size;
3440 buffer_dword (&sst, 0); /* Length of proc */
3441 buffer_word (&sst, 0); /* Length of prologue */
3442 buffer_dword (&sst, 0); /* Length of prologue and body */
3443 buffer_word (&sst, 0); /* Class type (for member functions) */
3444 buffer_byte (&sst, 8); /* 32-bit near */
3445 if (cplusplus_flag || cchName > 255)
3446 buffer_enc (&sst, name); /* Proc name */
3447 else
3448 buffer_nstr (&sst, name); /* Proc name */
3449 sst_end ();
3450 }
3451 r.r_extern = 0;
3452 r.r_length = 2;
3453 r.r_symbolnum = N_TEXT;
3454 r.r_pcrel = 0;
3455 r.r_pad = 0;
3456 buffer_mem (&sst_reloc, &r, sizeof (r));
3457 set_hll_type (symbol - sym_ptr, ti);
3458
3459#ifdef DEMANGLE_PROC_NAMES
3460 /* free the demangled name. */
3461 free (pszFree);
3462#endif
3463}
3464
3465
3466/* Locate the next N_LBRAC starting at symbol I. If found, set
3467 lbrac_index to the index of N_LBRAC and start a new block. If
3468 there is no N_LBRAC, set lbrac_index to -1. */
3469
3470static void next_lbrac (int i)
3471{
3472 lbrac_index = -1;
3473 for (; i < sym_count; ++i)
3474 if (sym_ptr[i].n_type == N_RBRAC || is_fun (&sym_ptr[i]))
3475 return;
3476 else if (sym_ptr[i].n_type == N_LBRAC)
3477 {
3478 lbrac_index = i;
3479 block_begin (sym_ptr[i].n_value);
3480 return;
3481 }
3482}
3483
3484
3485/* Parse a stabs symbol. The sym_ptr index is passed in INDEX. As we
3486 might have to concatenate successive symbol table entries, *INDEX
3487 is updated to refer to the next symbol. WHERE is the segment where
3488 the symbol is defined (N_DATA, for instance). If WHERE is -1,
3489 there is no segment. MSG is used for warning messages. This
3490 function is called recursively for static functions (F) There's up
3491 to one level of recursion. The arguments of a function are read
3492 twice: If FLAG is TRUE, we turn the arguments into local symbols;
3493 if FLAG is FALSE, we put arguments into the args_list array. */
3494
3495static void parse_symbol (int *index, int where, const char *msg, int flag)
3496{
3497 char *name;
3498 const char *str, *p;
3499 const struct nlist *symbol;
3500 struct type *t1;
3501 int i, n, ti, alloc_flag;
3502
3503 symbol = &sym_ptr[*index];
3504 alloc_flag = concat_symbols (index, &str);
3505 parse_start = str;
3506 parse_pindex = index;
3507 p = nextcolon (str);
3508 if (p != NULL)
3509 {
3510 n = p - str;
3511 name = alloca (n + 1);
3512 memcpy (name, str, n);
3513 name[n] = 0;
3514#if defined (HLL_DEBUG)
3515 printf ("%s %s\n", msg, str);
3516#endif
3517
3518 parse_ptr = p + 1;
3519 switch (*parse_ptr)
3520 {
3521 case 'G':
3522 /* Static storage, global scope */
3523 {
3524 const struct nlist *sym2 = NULL;
3525 ++parse_ptr;
3526 ti = hll_type ();
3527#if defined (HLL_DEBUG)
3528 printf (" type=%#x\n", ti);
3529#endif
3530
3531 /* Check for a ugly hack in dbxout.c which allow us to resolve
3532 communal and global variables more accuratly. The n_value is -12357
3533 and the previous entry is 0xfe. What the previous entry contains
3534 is the assembly name of the symbol.
3535
3536 Another part of that hack is that n_value is contains the
3537 symbol value so we don't need to look up the symbol for that
3538 reason (but as it turns out we have to look it up to see where
3539 it is in most cases, bah). */
3540 if ( symbol->n_value == -12357
3541 && *index >= 1
3542 && symbol[-1].n_type == 0xfe)
3543 {
3544 sym2 = find_symbol_ex (symbol[-1].n_un.n_strx + str_ptr, *index - 1, 1);
3545 if (!sym2)
3546 {
3547 warning ("Cannot find address of communal/external variable %s", name);
3548 return;
3549 }
3550 }
3551 else if (where == -1)
3552 { /* This is not very nice, we have to look up the symbol
3553 like we used to do in order to the the segment right.
3554 Should probably just use the external/communal hack above
3555 for everything or change the n_type of the 'G' stab... However,
3556 this will work for most cases and it will be compatible with
3557 the unmodified dbxout.c code. */
3558 if (symbol->n_type == N_FUN) /* in case GCC change... */
3559 where = N_TEXT;
3560 else
3561 {
3562 size_t cch = strlen (name);
3563 char *psz = alloca (cch + 2);
3564 *psz = '_';
3565 memcpy (psz + 1, name, cch + 1);
3566 sym2 = find_symbol (psz);
3567 if (!sym2)
3568 sym2 = find_symbol (psz + 1);
3569 where = N_DATA;
3570 }
3571 }
3572 /* if we're lucky we've got a symbol... */
3573 if (sym2)
3574 {
3575 if (sym2->n_type == N_EXT)
3576 sst_static (name, 0, ti, sym2 - sym_ptr, 1, sym2);
3577 else
3578 sst_static (name, sym2->n_value, ti, sym2->n_type, 0, sym2);
3579 }
3580 else
3581 sst_static (name, symbol->n_value, ti, where, 0, NULL);
3582 break;
3583 }
3584
3585 case 'S':
3586 case 'V':
3587
3588 /* S = static storage, file scope,
3589 V = static storage, local scope */
3590
3591 if (where == -1)
3592 {
3593 warning ("Cannot use symbol type %c with N_%s", *parse_ptr, msg);
3594 return;
3595 }
3596 ++parse_ptr;
3597 ti = hll_type ();
3598#if defined (HLL_DEBUG)
3599 printf (" type=%#x\n", ti);
3600#endif
3601 sst_static (name, symbol->n_value, ti, where, 0, NULL);
3602 break;
3603
3604 case 'p':
3605
3606 /* Function argument. */
3607
3608 if (where != -1)
3609 {
3610 warning ("Cannot use argument symbol with N_%s", msg);
3611 return;
3612 }
3613 ++parse_ptr;
3614 if (flag)
3615 {
3616 ti = hll_type ();
3617#if defined (HLL_DEBUG)
3618 printf (" type=%#x\n", ti);
3619#endif
3620 sst_start (SST_auto);
3621 buffer_dword (&sst, symbol->n_value);/* Offset into stk frame */
3622 buffer_word (&sst, ti); /* Type index */
3623 buffer_nstr (&sst, name); /* Symbol name */
3624 sst_end ();
3625 }
3626 else
3627 {
3628 t1 = parse_complete_type (NULL);
3629 grow_by (&args_grow, 1);
3630 args_list[args_grow.count++] = t1;
3631 }
3632 break;
3633
3634 case 'r':
3635
3636 /* Register variable. */
3637
3638 if (where != -1)
3639 {
3640 warning ("Cannot use register symbol with N_%s", msg);
3641 return;
3642 }
3643 ++parse_ptr;
3644 ti = hll_type ();
3645 switch (symbol->n_value)
3646 {
3647 case 0: /* EAX */
3648 i = 0x10;
3649 break;
3650 case 1: /* ECX */
3651 i = 0x11;
3652 break;
3653 case 2: /* EDX */
3654 i = 0x12;
3655 break;
3656 case 3: /* EBX */
3657 i = 0x13;
3658 break;
3659 case 4: /* ESP */
3660 i = 0x14;
3661 break;
3662 case 5: /* EBP */
3663 i = 0x15;
3664 break;
3665 case 6: /* ESI */
3666 i = 0x16;
3667 break;
3668 case 7: /* EDI */
3669 i = 0x17;
3670 break;
3671 case 12: /* ST(0) */
3672 i = 0x80;
3673 break;
3674 case 13: /* ST(1) */
3675 i = 0x81;
3676 break;
3677 case 14: /* ST(2) */
3678 i = 0x82;
3679 break;
3680 case 15: /* ST(3) */
3681 i = 0x83;
3682 break;
3683 case 16: /* ST(4) */
3684 i = 0x84;
3685 break;
3686 case 17: /* ST(5) */
3687 i = 0x85;
3688 break;
3689 case 18: /* ST(6) */
3690 i = 0x86;
3691 break;
3692 case 19: /* ST(7) */
3693 i = 0x87;
3694 break;
3695 default:
3696 warning ("unknown register %lu", symbol->n_value);
3697 return;
3698 }
3699 sst_start (SST_reg); /* Register variable */
3700 buffer_word (&sst, ti); /* Type index */
3701 buffer_byte (&sst, i); /* Register number */
3702 buffer_nstr (&sst, name); /* Symbol name */
3703 sst_end ();
3704 break;
3705
3706 case '0': case '1': case '2': case '3': case '4':
3707 case '5': case '6': case '7': case '8': case '9':
3708
3709 /* Local symbol. */
3710
3711 if (where != -1)
3712 {
3713 warning ("Cannot use local symbol with N_%s", msg);
3714 return;
3715 }
3716 ti = hll_type ();
3717#if defined (HLL_DEBUG)
3718 printf (" type=%#x\n", ti);
3719#endif
3720 sst_start (SST_auto);
3721 buffer_dword (&sst, symbol->n_value); /* Offset into stk frame */
3722 buffer_word (&sst, ti); /* Type index */
3723 buffer_nstr (&sst, name); /* Symbol name */
3724 sst_end ();
3725 break;
3726
3727 case 'F':
3728 case 'f':
3729
3730 /* Function. */
3731
3732 ++parse_ptr;
3733 if (where != N_TEXT)
3734 {
3735 warning ("Cannot use function with N_%s", msg);
3736 return;
3737 }
3738 last_fun_type.tag = ty_func;
3739 last_fun_type.index = -1;
3740 last_fun_type.d.func.domain = NULL;
3741 last_fun_type.d.func.ret = parse_type (NULL);
3742
3743 /* Don't check for end of string -- parse_ptr points to a
3744 comma for nested functions! */
3745
3746 last_fun_type.d.func.args = NULL;
3747 last_fun_type.d.func.arg_count = 0;
3748 last_fun_valid = TRUE;
3749 args_grow.count = 0;
3750 last_fun_addr = symbol->n_value;
3751 prologue_length = -1;
3752
3753 for (i = *index + 1; i < sym_count && sym_ptr[i].n_type == N_PSYM; ++i)
3754 parse_symbol (&i, -1, "PSYM", FALSE);
3755
3756 define_fun (symbol);
3757
3758 /* Now look for N_LBRAC */
3759 next_lbrac (i);
3760 if (lbrac_index != -1)
3761 {
3762 if (prologue_length == -1)
3763 prologue_length = sym_ptr[lbrac_index].n_value - last_fun_addr;
3764 }
3765 else
3766 {/* kso #456 2003-06-05: We need a begin to catch the parameters. */
3767 prologue_length = 0;
3768 block_begin (proc_start_addr);
3769 }
3770
3771 /* Parameters */
3772 while (*index + 1 < sym_count && sym_ptr[*index+1].n_type == N_PSYM)
3773 {
3774 ++(*index);
3775 parse_symbol (index, -1, "PSYM", TRUE);
3776 }
3777
3778 /* Start another block for the local variables. This is to work
3779 around some missing bracets in stabs and to fix problem with
3780 C++ bool type. */
3781 dword addr = proc_start_addr + prologue_length;
3782 if (lbrac_index >= 0 && sym_ptr[lbrac_index].n_value > addr)
3783 addr = sym_ptr[lbrac_index].n_value;
3784 block_begin (addr);
3785
3786 /* Local variables (fix for boolparam testcase too). */
3787 while (*index + 1 < sym_count && sym_ptr[*index+1].n_type == N_LSYM)
3788 {
3789 ++(*index);
3790 parse_symbol (index, -1, "LSYM", TRUE);
3791 }
3792
3793 /* If there's no N_LBRAC, write SST_end now */
3794 if (lbrac_index == -1)
3795 {/* kso #456 2003-06-05: We need a begin to catch the parameters. */
3796 while (block_grow.count > 0)
3797 block_end (*index+1);
3798 }
3799 break;
3800 }
3801 }
3802 if (alloc_flag)
3803 free ((void *)str); /* Remove the const attribute */
3804}
3805
3806
3807/* Create a type tag for HLL type index INDEX. TYPE is either SST_tag
3808 (for structures and unions) or SST_tag2 (for classes). NAME is
3809 the name of the structure, union or class. */
3810
3811static void type_tag (int type, int index, const char *name)
3812{
3813 if (name != NULL)
3814 {
3815 size_t cch;
3816 if (type == SST_tag2 || (cch = strlen(name)) > 250)
3817 {
3818 sst_start (SST_tag2);
3819 buffer_word (&sst, index);
3820 buffer_enc (&sst, name);
3821 }
3822 else
3823 {
3824 char achName[256];
3825 memcpy (achName, name, cch);
3826 achName[cch] = '\0';
3827
3828 sst_start (type);
3829 buffer_word (&sst, index);
3830 buffer_nstr (&sst, achName);
3831 }
3832 sst_end ();
3833 }
3834}
3835
3836
3837/* Write a CuInfo SST entry to tell the debugger what compiler (C or
3838 C++) has been used. */
3839
3840static void write_cuinfo (void)
3841{
3842 struct timestamp ts;
3843 time_t now;
3844 struct tm *tp;
3845 byte lang;
3846
3847 if (cplusplus_flag)
3848 lang = 0x02; /* C++ */
3849 else
3850 lang = 0x01; /* C */
3851
3852 time (&now);
3853 tp = localtime (&now);
3854 ts.year = tp->tm_year + 1900;
3855 ts.month = tp->tm_mon + 1;
3856 ts.day = tp->tm_mday;
3857 ts.hours = tp->tm_hour;
3858 ts.minutes = tp->tm_min;
3859 ts.seconds = tp->tm_sec;
3860 ts.hundredths = 0;
3861 sst_start (SST_cuinfo);
3862 buffer_byte (&sst, lang);
3863 buffer_nstr (&sst, " "); /* Compiler options */
3864 buffer_nstr (&sst, __DATE__); /* Compiler date */
3865 buffer_mem (&sst, &ts, sizeof (ts));
3866 sst_end ();
3867}
3868
3869
3870/* Write a ChangSeg SST entry to define the text segment. */
3871
3872static void write_changseg (void)
3873{
3874 struct relocation_info r;
3875
3876 sst_start (SST_changseg);
3877 r.r_address = sst.size;
3878 buffer_word (&sst, 0); /* Segment number */
3879 buffer_word (&sst, 0); /* Reserved */
3880 sst_end ();
3881 r.r_extern = 0;
3882 r.r_length = 1;
3883 r.r_symbolnum = N_TEXT;
3884 r.r_pcrel = 0;
3885 r.r_pad = 0;
3886 buffer_mem (&sst_reloc, &r, sizeof (r));
3887}
3888
3889
3890/* Convert debug information. The data is taken from the symbol and
3891 string tables of the current a.out module. Output is put into the
3892 tt, sst etc. buffers. */
3893
3894void convert_debug (void)
3895{
3896 int i;
3897 struct type *t1, *t2;
3898#if defined (HLL_DEBUG)
3899 setvbuf(stdout, NULL, _IONBF, 256);
3900#endif
3901
3902 str_pool = strpool_init ();
3903 grow_init (&stype_grow, &stype_list, sizeof (*stype_list), 32);
3904 type_head = NULL; hll_type_index = 512;
3905 grow_init (&block_grow, &block_stack, sizeof (*block_stack), 16);
3906 grow_init (&args_grow, &args_list, sizeof (*args_list), 16);
3907 last_fun_valid = FALSE; prologue_length = -1;
3908 unnamed_struct_number = 1;
3909
3910 /* Check whether converting a translated C++ program. */
3911#if 1 /* kso #465 2003-06-04: pretend everything is C++, that symbols is no longer present. */
3912 cplusplus_flag = 1;
3913#else
3914 cplusplus_flag = (find_symbol ("__gnu_compiled_cplusplus") != NULL);
3915#endif
3916
3917
3918 /* Parse the typedefs to avoid forward references. */
3919
3920 for (i = 0; i < sym_count; ++i)
3921 switch (sym_ptr[i].n_type)
3922 {
3923 case N_LSYM:
3924 case N_LCSYM:
3925 case N_GSYM:
3926 case N_PSYM:
3927 case N_RSYM:
3928 case N_STSYM:
3929 case N_FUN:
3930 parse_typedef (&i);
3931 break;
3932 }
3933
3934 /* Provide information about the compiler. */
3935
3936 write_cuinfo ();
3937
3938 /* Change the default segment. */
3939
3940 write_changseg ();
3941
3942 /* Parse the other symbol table entries. */
3943
3944 lbrac_index = -1; block_grow.count = 0; proc_patch_base = 0;
3945 for (i = 0; i < sym_count; ++i)
3946 switch (sym_ptr[i].n_type)
3947 {
3948 case N_LSYM:
3949 parse_symbol (&i, -1, "LSYM", TRUE);
3950 break;
3951
3952 case N_GSYM:
3953 parse_symbol (&i, -1, "GSYM", TRUE);
3954 break;
3955
3956 case N_LCSYM:
3957 parse_symbol (&i, N_BSS, "LCSYM", TRUE);
3958 break;
3959
3960 case N_STSYM:
3961 parse_symbol (&i, N_DATA, "STSYM", TRUE);
3962 break;
3963
3964 case N_RSYM:
3965 parse_symbol (&i, -1, "RSYM", TRUE);
3966 break;
3967
3968 case N_LBRAC:
3969 if (lbrac_index == -1) /* N_LBRAC without local symbols */
3970 block_begin (sym_ptr[i].n_value);
3971 else if (i != lbrac_index)
3972 warning ("Invalid N_LBRAC");
3973 next_lbrac (i + 1);
3974 break;
3975
3976 case N_RBRAC:
3977 block_end (i);
3978 next_lbrac (i + 1);
3979 break;
3980
3981 case N_FUN:
3982 parse_symbol (&i, N_TEXT, "FUN", TRUE);
3983 break;
3984 }
3985
3986#ifdef HASH_DEBUG
3987fprintf(stderr, "\n");
3988for (i = 0; i < TYPE_HASH_MAX; i++)
3989 if (i % 7 == 6)
3990 fprintf(stderr, "%3d: %4d\n", i, ctype_tag[i]);
3991 else
3992 fprintf(stderr, "%3d: %4d ", i, ctype_tag[i]);
3993fprintf(stderr, "\nctypes=%d\n", ctypes);
3994for (i = 0; i < TYPE_HASH_MAX; i++)
3995 if (ctype_tag[i] > 1000)
3996 {
3997 int j;
3998 unsigned au[ty_max];
3999 memset(&au[0], 0, sizeof(au));
4000 for (t1 = atype_tag[i]; t1 != NULL; t1 = t1->nexthash)
4001 au[t1->tag]++;
4002
4003 fprintf(stderr, "\nIndex %d is overpopulated (%d):\n", i, ctype_tag[i]);
4004 for (j = 0; j < 18; j++)
4005 if (j % 7 == 6)
4006 fprintf(stderr, "%2d: %4d\n", j, au[j]);
4007 else
4008 fprintf(stderr, "%2d: %4d ", j, au[j]);
4009 }
4010#endif
4011
4012 /* Create tags for structures, unions and classes. */
4013
4014 for (t1 = type_head; t1 != NULL; t1 = t1->next)
4015 if (t1->index != -1)
4016 switch (t1->tag)
4017 {
4018 case ty_struc:
4019 type_tag (SST_tag, t1->index, t1->d.struc.name);
4020 break;
4021 case ty_class:
4022 type_tag (SST_tag2, t1->index, t1->d.class.name);
4023 break;
4024 case ty_enu:
4025 type_tag (SST_tag, t1->index, t1->d.enu.name);
4026 break;
4027 default:
4028 break;
4029 }
4030
4031#ifdef HLL_DEBUG
4032 printf ("Stabs to HLL type mappings: %d/%d\n", stype_grow.count, stype_grow.alloc);
4033 for (i = 0; i < stype_grow.alloc; i++)
4034 if (stype_list[i])
4035 {
4036 printf (" %3d => 0x%03x/%-3d ", i + 1, stype_list[i]->index, stype_list[i]->index);
4037 show_type (stype_list[i]);
4038 printf("\n");
4039 }
4040#endif
4041
4042
4043 /* Deallocate memory. */
4044
4045 grow_free (&stype_grow);
4046 grow_free (&args_grow);
4047 grow_free (&block_grow);
4048 for (t1 = type_head; t1 != NULL; t1 = t2)
4049 {
4050 t2 = t1->next;
4051 switch (t1->tag)
4052 {
4053 case ty_args:
4054 if (t1->d.args.list != NULL)
4055 free (t1->d.args.list);
4056 break;
4057 case ty_types:
4058 if (t1->d.types.list != NULL)
4059 free (t1->d.types.list);
4060 break;
4061 case ty_fields:
4062 if (t1->d.fields.list != NULL)
4063 free (t1->d.fields.list);
4064 break;
4065 case ty_values:
4066 if (t1->d.values.list != NULL)
4067 free (t1->d.values.list);
4068 break;
4069 default:
4070 break;
4071 }
4072 free (t1);
4073 }
4074 type_head = NULL;
4075 memset (&atype_tag, 0, sizeof(atype_tag));
4076 strpool_free (str_pool);
4077 str_pool = NULL;
4078}
Note: See TracBrowser for help on using the repository browser.