source: vendor/emx/current/src/emxomf/stabshll.c

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

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 86.8 KB
Line 
1/* stabshll.c -- Convert GNU-style a.out debug information into
2 IBM OS/2 HLL 3 debug information
3 Copyright (c) 1993-1998 Eberhard Mattes
4
5This file is part of emxomf.
6
7emxomf is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12emxomf is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with emxomf; see the file COPYING. If not, write to
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
21
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <time.h>
27#include "defs.h"
28#include "emxomf.h"
29#include "grow.h"
30#include "stabshll.h"
31
32/* Field ID values for the type table. */
33
34#define FID_nil 0x80 /* No data */
35#define FID_void 0x81 /* Type void */
36#define FID_string 0x82 /* A length-prefixed string follows */
37#define FID_index 0x83 /* A type index follows */
38#define FID_span_16u 0x85 /* An unsigned 16-bit number follows */
39#define FID_span_32u 0x86 /* An unsigned 32-bit number follows */
40#define FID_span_8s 0x88 /* A signed 8-bit number follows */
41#define FID_span_16s 0x89 /* A signed 16-bit number follows */
42#define FID_span_32s 0x8a /* A signed 32-bit number follows */
43#define FID_span_8u 0x8b /* An unsigned 8-bit number follows */
44
45/* Static scope table subrecord types. */
46
47#define SST_begin 0x00 /* Begin block */
48#define SST_proc 0x01 /* Procedure */
49#define SST_end 0x02 /* End block or end procedure */
50#define SST_auto 0x04 /* Scoped variable */
51#define SST_static 0x05 /* Static variable */
52#define SST_reg 0x0d /* Register variable */
53#define SST_changseg 0x11 /* Change default segment */
54#define SST_tag 0x16 /* Structure, union, enum tags */
55#define SST_class 0x19 /* C++ class */
56#define SST_memfunc 0x1a /* C++ member function */
57#define SST_CPPproc 0x1d /* C++ function */
58#define SST_CPPstatic 0x1e /* C++ static variable */
59#define SST_cuinfo 0x40 /* Compile unit information */
60
61/* Class visibility. */
62
63#define VIS_PRIVATE 0x00 /* private */
64#define VIS_PROTECTED 0x01 /* protected */
65#define VIS_PUBLIC 0x02 /* public */
66
67#define MEMBER_VIS 0x03 /* Member visibility mask */
68#define MEMBER_FUNCTION 0x08 /* It's a member function */
69#define MEMBER_VIRTUAL 0x10 /* Member function / base class is virtual */
70#define MEMBER_VOLATILE 0x20 /* Member function is volatile */
71#define MEMBER_CONST 0x40 /* Member function is const */
72#define MEMBER_STATIC 0x80 /* Member is static */
73#define MEMBER_CTOR 0x100 /* Member function is a constructor */
74#define MEMBER_DTOR 0x200 /* Member function is a destructor */
75#define MEMBER_BASE 0x400 /* It's a base class */
76
77/* Flags for ty_struc. */
78
79#define STRUC_FORWARD 0x01 /* Structure declared forward */
80
81enum type_tag
82{
83 ty_stabs_ref, /* Reference to stabs type */
84 ty_prim, /* Primitive type */
85 ty_alias, /* Equivalent type */
86 ty_pointer, /* Pointer type */
87 ty_func, /* Function */
88 ty_args, /* Function arguments */
89 ty_struc, /* Structure or union */
90 ty_types, /* Type list for structure/union */
91 ty_fields, /* Field list for structure/union */
92 ty_enu, /* Enumeration type */
93 ty_values, /* Enumeration values */
94 ty_array, /* Array */
95 ty_bits, /* Bit field / bit string */
96 ty_ref, /* Reference type (C++) */
97 ty_member, /* Member of a C++ class */
98 ty_class, /* A C++ class */
99 ty_memfunc, /* A C++ member function */
100 ty_baseclass /* A C++ base class */
101};
102
103/* A structure field. */
104
105struct field
106{
107 dword offset; /* Offset, in bytes */
108 const char *name; /* Field name */
109};
110
111/* An enumeration value. */
112
113struct value
114{
115 long index; /* The value */
116 const char *name; /* The name */
117};
118
119/* Temporary representation of a structure field. */
120
121struct tmp_field
122{
123 struct type *type; /* The internal type */
124 dword offset; /* The offset of the field */
125 const char *name; /* The field name */
126 const char *sname; /* Static name */
127 int flags; /* Member flags (visibility) */
128};
129
130
131/* Internal representation of a type. */
132
133struct type
134{
135 enum type_tag tag; /* The type of this type */
136 int index; /* The HLL index for this type */
137 struct type *next; /* All types are chained in a single list */
138 union
139 {
140 int stabs_ref; /* Reference a stabs type */
141 struct type *pointer; /* Pointer to another type */
142 struct type *alias; /* Alias for another type */
143 struct type *ref; /* Reference to another type */
144 struct
145 { /* Structure */
146 struct type *types; /* Types of the fields */
147 struct type *fields; /* Field names and offsets */
148 dword size; /* Size of the structure, in bytes */
149 word count; /* Number of fields */
150 word flags; /* Flags (STRUC_FORWARD) */
151 const char *name; /* Name of the structure */
152 } struc;
153 struct
154 { /* Class */
155 struct type *members; /* Members */
156 dword size; /* Size of the class, in bytes */
157 word count; /* Number of fields */
158 const char *name; /* Name of the class */
159 } class;
160 struct
161 { /* Enumeration */
162 struct type *type; /* Base type */
163 struct type *values; /* Values */
164 long first; /* Smallest value */
165 long last; /* Biggest value */
166 const char *name; /* Name of the enum */
167 } enu;
168 struct
169 { /* Array */
170 struct type *itype; /* Index type */
171 struct type *etype; /* Element type */
172 long first; /* First index */
173 long last; /* Last index */
174 } array;
175 struct
176 { /* Bitfield */
177 struct type *type; /* Base type */
178 long start; /* First bit */
179 long count; /* Number of bits */
180 } bits;
181 struct
182 { /* Function */
183 struct type *ret; /* Return type */
184 struct type *args; /* Argument types */
185 int arg_count; /* Number of arguments */
186 } func;
187 struct
188 { /* Function arguments */
189 struct type **list; /* Array of argument types */
190 int count; /* Number of arguments */
191 } args;
192 struct
193 { /* List of types */
194 struct type **list; /* Array of types */
195 int count; /* Number of types */
196 } types;
197 struct
198 { /* Structure fields */
199 struct field *list; /* The fields */
200 int count; /* Number of fields */
201 } fields;
202 struct
203 { /* Class member */
204 struct type *type; /* Type of the member */
205 dword offset; /* Offset of the member, in bytes */
206 int flags; /* Member flags (visibility etc.) */
207 const char *name; /* Name of the member */
208 const char *sname; /* Static name */
209 } member;
210 struct
211 { /* Enumeration values */
212 struct value *list; /* Array of values */
213 int count; /* Number of values */
214 } values;
215 struct
216 { /* Member function */
217 struct type *type; /* Function type */
218 dword offset; /* Offset in the virtual table */
219 int flags; /* Member flags (visibility etc.) */
220 const char *name; /* Name of the member function */
221 } memfunc;
222 struct
223 { /* Base class */
224 struct type *type; /* The base class */
225 dword offset; /* Offset of the base class */
226 int flags; /* Visibility, virtual */
227 } baseclass;
228 } d;
229};
230
231
232/* A block. */
233
234struct block
235{
236 dword addr; /* Start address of the block */
237 size_t patch_ptr; /* Index into SST buffer for patching */
238};
239
240/* Timestamp for the compiler unit information SST subrecord. */
241
242#pragma pack(1)
243
244struct timestamp
245{
246 byte hours;
247 byte minutes;
248 byte seconds;
249 byte hundredths;
250 byte day;
251 byte month;
252 word year;
253};
254
255#pragma pack()
256
257/* This variable points to the next character of a stabs type being
258 parsed. */
259
260static const char *parse_ptr;
261
262/* The next HLL type index. When creating a complex type, this index
263 is used. Then, this variable is incremented. */
264
265static int hll_type_index;
266
267/* We keep all internal types in a single list. When creating a new
268 type, we first look through the list to find out whether an
269 identical type already exists. Doing a linear search is
270 inefficient, but that doesn't matter here, according to
271 experience. This variable points to the first type of the list. */
272
273static struct type *type_head;
274
275/* This growing array keeps track of stabs vs. internal types. It is
276 indexed by stabs type numbers. */
277
278static struct type **stype_list;
279static struct grow stype_grow;
280
281/* We collect function arguments in this growing array. */
282
283static struct type **args_list;
284static struct grow args_grow;
285
286/* This stack (implemented with a growing array) keeps track of
287 begin/end block nesting. */
288
289static struct block *block_stack;
290static struct grow block_grow;
291
292/* This string pool contains field names etc. */
293
294static struct strpool *str_pool;
295
296/* This variable holds the sym_ptr index of the most recently
297 encountered N_LBRAC symbol. */
298
299static int lbrac_index;
300
301/* The index (in sst) of the most recently started SST subrecord. */
302
303static size_t subrec_start;
304
305/* The proc_patch_base contains the index where the base address and
306 function length are to be patched into the SST. */
307
308static size_t proc_patch_base;
309
310/* The base address used by the most recently proc record in the
311 SST. */
312
313static size_t proc_start_addr;
314
315/* The internal type of the most recently defined function. Valid
316 only if last_fun_valid is TRUE. */
317
318static struct type last_fun_type;
319
320/* This variable is TRUE if a function has recently been defined. */
321
322static int last_fun_valid;
323
324/* The length of the prologue of the most recently defined
325 function. */
326
327static int prologue_length;
328
329/* The address of the most recently defined function. */
330
331static dword last_fun_addr;
332
333/* This variable is non-zero when converting a translated C++
334 program. */
335
336static int cplusplus_flag;
337
338/* Unnamed structures are numbered sequentially, using this
339 counter. */
340
341static int unnamed_struct_number;
342
343
344/* Start a type table record of type TYPE, with type qualifier QUAL.
345 tt_end() must be called to finish the record after writing the
346 record contents. */
347
348static void tt_start (byte type, byte qual)
349{
350 subrec_start = tt.size;
351 grow_by (&tt_boundary_grow, 1);
352 tt_boundary[tt_boundary_grow.count++] = tt.size;
353 buffer_word (&tt, 0); /* Length of sub-record */
354 buffer_byte (&tt, type); /* Type of sub-record */
355 buffer_byte (&tt, qual); /* Type qualifier */
356}
357
358
359/* Finish a type table record. This function must be called after
360 tt_start(). */
361
362static void tt_end (void)
363{
364 *(word *)(tt.buf + subrec_start) = tt.size - subrec_start - 2;
365}
366
367
368/* Put the type index TI into the type table. TI = -1 encodes the
369 `nil' type (no type), 0x97 encodes the `void' type. All other
370 values are type indices. */
371
372static void tt_index (int ti)
373{
374 if (ti == -1) /* nil */
375 buffer_byte (&tt, FID_nil);
376 else if (ti == 0x97) /* void */
377 buffer_byte (&tt, FID_void);
378 else
379 {
380 buffer_byte (&tt, FID_index);
381 buffer_word (&tt, ti);
382 }
383}
384
385
386/* Put an 8-bit byte into the type table. */
387
388static void tt_byte (byte x)
389{
390 buffer_byte (&tt, x);
391}
392
393
394/* Put a 16-bit word into the type table. */
395
396static void tt_word (word x)
397{
398 buffer_word (&tt, x);
399}
400
401
402/* Put a 32-bit double word into the type table. */
403
404static void tt_dword (dword x)
405{
406 buffer_dword (&tt, x);
407}
408
409
410/* Put the name S into the type table. */
411
412static void tt_name (const char *s)
413{
414 buffer_byte (&tt, FID_string);
415 buffer_nstr (&tt, s);
416}
417
418
419/* Put the mangled name S into the type table. */
420
421static void tt_enc (const char *s)
422{
423 buffer_enc (&tt, s);
424}
425
426
427/* Put the unsigned number N into the type table. Depending on the
428 magnitude of N, different encodings are used. */
429
430static void tt_unsigned_span (dword n)
431{
432 if (n <= 0xff)
433 {
434 buffer_byte (&tt, FID_span_8u);
435 buffer_byte (&tt, n);
436 }
437 else if (n <= 0xffff)
438 {
439 buffer_byte (&tt, FID_span_16u);
440 buffer_word (&tt, n);
441 }
442 else
443 {
444 buffer_byte (&tt, FID_span_32u);
445 buffer_dword (&tt, n);
446 }
447}
448
449
450/* Put the signed number N into the type table. Depending on the
451 magnitude of N, different encodings are used. */
452
453static void tt_signed_span (long n)
454{
455 if (-127 <= n && n <= 127)
456 {
457 buffer_byte (&tt, FID_span_8s);
458 buffer_byte (&tt, n);
459 }
460 else if (-32767 <= n && n <= 32767)
461 {
462 buffer_byte (&tt, FID_span_16s);
463 buffer_word (&tt, n);
464 }
465 else
466 {
467 buffer_byte (&tt, FID_span_32s);
468 buffer_dword (&tt, n);
469 }
470}
471
472
473/* Start a static scope table subrecord of type TYPE. sst_end() must
474 be called to finish the subrecord after writing the subrecord
475 data. */
476
477static void sst_start (byte type)
478{
479 subrec_start = sst.size;
480 grow_by (&sst_boundary_grow, 1);
481 sst_boundary[sst_boundary_grow.count++] = sst.size;
482 buffer_byte (&sst, 0); /* Length of sub-record */
483 buffer_byte (&sst, type); /* Type of sub-record */
484}
485
486
487/* Finish a static scope table subrecord. This function must be
488 called after sst_start(). */
489
490static void sst_end (void)
491{
492 if (sst.size - subrec_start - 1 > 255)
493 error ("sst_end: Record too big");
494 sst.buf[subrec_start] = sst.size - subrec_start - 1;
495}
496
497
498/* Define a static variable in the static scope table. NAME is the
499 name of the variable, ADDR is the address of the variable (segment
500 offset), TYPE_INDEX is the HLL type index. If EXT is zero, SYM_SEG
501 is the segment (N_DATA, for instance). If EXT is non-zero, SYM_SEG
502 is the stabs symbol index (for the relocation table). This
503 function also creates appropriate relocation table entries. */
504
505static void sst_static (const char *name, dword addr, int type_index,
506 int sym_seg, int ext)
507{
508 struct reloc r;
509
510 sst_start (SST_static);
511 r.address = sst.size;
512 buffer_dword (&sst, addr); /* Segment offset */
513 buffer_word (&sst, 0); /* Segment address */
514 buffer_word (&sst, type_index); /* Type index */
515 buffer_nstr (&sst, name); /* Symbol name */
516 sst_end ();
517 r.ext = ext;
518 r.length = 2;
519 r.symbolnum = sym_seg;
520 r.pcrel = 0;
521 r.unused = 0;
522 buffer_mem (&sst_reloc, &r, sizeof (r));
523 r.address += 4;
524 r.length = 1;
525 buffer_mem (&sst_reloc, &r, sizeof (r));
526}
527
528
529/* Add a new internal type to the list of internal types. If an
530 identical type already exists, a pointer to that type is returned.
531 Otherwise, a copy of the new type is added to the list and a
532 pointer to that type is returned. It isn't worth my while to
533 improve the speed of this function, for instance by using
534 hashing. */
535
536static struct type *type_add (struct type *src)
537{
538 struct type *p;
539 int i;
540
541 if (src->tag == ty_alias)
542 abort ();
543 for (p = type_head; p != NULL; p = p->next)
544 if (p->tag == src->tag)
545 switch (p->tag)
546 {
547 case ty_stabs_ref:
548 if (p->d.stabs_ref == src->d.stabs_ref)
549 return p;
550 break;
551 case ty_prim:
552 if (p->index == src->index)
553 return p;
554 break;
555 case ty_pointer:
556 if (p->d.pointer == src->d.pointer)
557 return p;
558 break;
559 case ty_ref:
560 if (p->d.ref == src->d.ref)
561 return p;
562 break;
563 case ty_struc:
564 if (p->d.struc.fields == src->d.struc.fields
565 && p->d.struc.types == src->d.struc.types
566 && p->d.struc.name == src->d.struc.name
567 && p->d.struc.flags == src->d.struc.flags)
568 return p;
569 break;
570 case ty_class:
571 if (p->d.class.members == src->d.class.members
572 && p->d.class.name == src->d.class.name)
573 return p;
574 break;
575 case ty_enu:
576 if (p->d.enu.type == src->d.enu.type
577 && p->d.enu.values == src->d.enu.values
578 && p->d.enu.name == src->d.enu.name)
579 return p;
580 break;
581 case ty_array:
582 if (p->d.array.etype == src->d.array.etype
583 && p->d.array.itype == src->d.array.itype
584 && p->d.array.first == src->d.array.first
585 && p->d.array.last == src->d.array.last)
586 return p;
587 break;
588 case ty_bits:
589 if (p->d.bits.type == src->d.bits.type
590 && p->d.bits.start == src->d.bits.start
591 && p->d.bits.count == src->d.bits.count)
592 return p;
593 break;
594 case ty_func:
595 if (p->d.func.ret == src->d.func.ret
596 && p->d.func.args == src->d.func.args)
597 return p;
598 break;
599 case ty_args:
600 if (p->d.args.count == src->d.args.count)
601 {
602 for (i = 0; i < p->d.args.count; ++i)
603 if (p->d.args.list[i] != src->d.args.list[i])
604 break;
605 if (i >= p->d.args.count)
606 return p;
607 }
608 break;
609 case ty_types:
610 if (p->d.types.count == src->d.types.count)
611 {
612 for (i = 0; i < p->d.types.count; ++i)
613 if (p->d.types.list[i] != src->d.types.list[i])
614 break;
615 if (i >= p->d.types.count)
616 return p;
617 }
618 break;
619 case ty_fields:
620 if (p->d.fields.count == src->d.fields.count)
621 {
622 for (i = 0; i < p->d.fields.count; ++i)
623 if (p->d.fields.list[i].offset != src->d.fields.list[i].offset
624 || p->d.fields.list[i].name != src->d.fields.list[i].name)
625 break;
626 if (i >= p->d.fields.count)
627 return p;
628 }
629 break;
630 case ty_member:
631 if (p->d.member.type == src->d.member.type
632 && p->d.member.offset == src->d.member.offset
633 && p->d.member.flags == src->d.member.flags
634 && p->d.member.name == src->d.member.name
635 && p->d.member.sname == src->d.member.sname)
636 return p;
637 break;
638 case ty_values:
639 if (p->d.values.count == src->d.values.count)
640 {
641 for (i = 0; i < p->d.values.count; ++i)
642 if (p->d.values.list[i].index != src->d.values.list[i].index
643 || p->d.values.list[i].name != src->d.values.list[i].name)
644 break;
645 if (i >= p->d.values.count)
646 return p;
647 }
648 break;
649 case ty_memfunc:
650 if (p->d.memfunc.type == src->d.memfunc.type
651 && p->d.memfunc.offset == src->d.memfunc.offset
652 && p->d.memfunc.flags == src->d.memfunc.flags
653 && p->d.memfunc.name == src->d.memfunc.name)
654 return p;
655 break;
656 case ty_baseclass:
657 if (p->d.baseclass.type == src->d.baseclass.type
658 && p->d.baseclass.offset == src->d.baseclass.offset
659 && p->d.baseclass.flags == src->d.baseclass.flags)
660 return p;
661 break;
662 default:
663 abort ();
664 }
665 p = xmalloc (sizeof (*p));
666 *p = *src;
667 p->next = type_head;
668 type_head = p;
669 switch (src->tag)
670 {
671 case ty_args:
672 i = src->d.args.count * sizeof (*(src->d.args.list));
673 p->d.args.list = xmalloc (i);
674 memcpy (p->d.args.list, src->d.args.list, i);
675 break;
676 case ty_types:
677 i = src->d.types.count * sizeof (*(src->d.types.list));
678 p->d.types.list = xmalloc (i);
679 memcpy (p->d.types.list, src->d.types.list, i);
680 break;
681 case ty_fields:
682 i = src->d.fields.count * sizeof (*(src->d.fields.list));
683 p->d.fields.list = xmalloc (i);
684 memcpy (p->d.fields.list, src->d.fields.list, i);
685 break;
686 case ty_values:
687 i = src->d.values.count * sizeof (*(src->d.values.list));
688 p->d.values.list = xmalloc (i);
689 memcpy (p->d.values.list, src->d.values.list, i);
690 break;
691 default:
692 break;
693 }
694 return p;
695}
696
697
698/* Associate the stabs type number NUMBER with the internal type
699 HLL. */
700
701static void stype_add (int number, struct type *hll)
702{
703 int i;
704
705 /* Stabs type numbers are greater than zero. */
706
707 if (number < 1)
708 error ("stype_add: Invalid type index %d", number);
709
710 /* Remember the current size of the table and grow the table as
711 required. */
712
713 i = stype_grow.alloc;
714 grow_to (&stype_grow, number);
715
716 /* Initialize the new table entries. */
717
718 while (i < stype_grow.alloc)
719 stype_list[i++] = NULL;
720
721 /* Put the type into the table. */
722
723 stype_list[number-1] = hll;
724}
725
726
727/* Find the internal type associated with the stabs type number
728 NUMBER. If there is no such type, return NULL. */
729
730static struct type *stype_find (int number)
731{
732 /* Stabs type numbers are greater than zero. */
733
734 if (number < 1)
735 error ("stype_find: Invalid type index %d", number);
736
737 /* Adjust the index and check if it is within the current size of
738 the table. */
739
740 --number;
741 if (number >= stype_grow.alloc)
742 return NULL;
743 else
744 return stype_list[number];
745}
746
747
748/* Dereference stabs references until finding a non-reference. */
749
750static const struct type *follow_refs (const struct type *t)
751{
752 const struct type *t2;
753
754 while (t->tag == ty_stabs_ref)
755 {
756 t2 = stype_find (t->d.stabs_ref);
757 if (t2 == NULL)
758 break;
759 t = t2;
760 }
761 return t;
762}
763
764
765#if defined (DEBUG)
766
767/* This function is used to print an internal type for debugging. */
768
769static void show_type (struct type *tp)
770{
771 int i;
772
773 switch (tp->tag)
774 {
775 case ty_prim:
776 printf ("%#x", tp->index);
777 break;
778 case ty_stabs_ref:
779 printf ("t%d", tp->d.stabs_ref);
780 break;
781 case ty_alias:
782 show_type (tp->d.alias);
783 break;
784 case ty_pointer:
785 printf ("(");
786 show_type (tp->d.pointer);
787 printf (")*");
788 break;
789 case ty_ref:
790 printf ("(");
791 show_type (tp->d.ref);
792 printf (")&");
793 break;
794 case ty_struc:
795 printf ("{");
796 show_type (tp->d.struc.types);
797 printf ("}");
798 break;
799 case ty_class:
800 printf ("{");
801 show_type (tp->d.class.members);
802 printf ("}");
803 break;
804 case ty_enu:
805 printf ("{");
806 show_type (tp->d.enu.values);
807 printf ("}");
808 break;
809 case ty_array:
810 printf ("(");
811 show_type (tp->d.array.etype);
812 printf ("[%d]", tp->d.array.last + 1 - tp->d.array.first);
813 break;
814 case ty_bits:
815 show_type (tp->d.bits.type);
816 printf (":%ld:%ld", tp->d.bits.start, tp->d.bits.count);
817 break;
818 case ty_func:
819 show_type (tp->d.func.ret);
820 printf ("(");
821 if (tp->d.func.args != NULL)
822 show_type (tp->d.func.args);
823 printf (")");
824 break;
825 case ty_args:
826 for (i = 0; i < tp->d.args.count; ++i)
827 {
828 if (i != 0)
829 printf (", ");
830 show_type (tp->d.args.list[i]);
831 }
832 break;
833 case ty_types:
834 for (i = 0; i < tp->d.types.count; ++i)
835 {
836 if (i != 0)
837 printf (", ");
838 show_type (tp->d.types.list[i]);
839 }
840 break;
841 case ty_fields:
842 for (i = 0; i < tp->d.fields.count; ++i)
843 {
844 if (i != 0)
845 printf (", ");
846 printf ("%s", tp->d.fields.list[i].name);
847 }
848 break;
849 case ty_member:
850 printf ("%s", tp->d.member.name);
851 break;
852 case ty_values:
853 for (i = 0; i < tp->d.values.count; ++i)
854 {
855 if (i != 0)
856 printf (", ");
857 printf ("%s", tp->d.values.list[i].name);
858 }
859 break;
860 case ty_memfunc:
861 show_type (tp->d.memfunc.type);
862 break;
863 case ty_baseclass:
864 printf ("{");
865 show_type (tp->d.baseclass.type);
866 printf ("}");
867 break;
868 default:
869 error ("show_type: Unknown type: %d", tp->tag);
870 }
871}
872
873#endif
874
875
876/* Return the size of the internal type TP, in bytes. */
877
878static dword type_size (const struct type *tp)
879{
880 switch (tp->tag)
881 {
882 case ty_prim:
883
884 /* Primitive types. */
885
886 if (tp->index >= 0xa0 && tp->index <= 0xbf)
887 return 4; /* Near pointer */
888 switch (tp->index)
889 {
890 case 0x97: /* void */
891 return 0;
892 case 0x80: /* 8 bit signed */
893 case 0x84: /* 8 bit unsigned */
894 case 0x90: /* 8 bit boolean */
895 return 1;
896 case 0x81: /* 16 bit signed */
897 case 0x85: /* 16 bit unsigned */
898 return 2;
899 case 0x82: /* 32 bit signed */
900 case 0x86: /* 32 bit unsigned */
901 case 0x88: /* 32 bit real */
902 return 4;
903 case 0x89: /* 64 bit real */
904 return 8;
905 case 0x8a: /* 80 bit real (96 bits of storage) */
906 return 12;
907 }
908 error ("type_size: Unknown primitive type: %d", tp->index);
909
910 case ty_stabs_ref:
911
912 /* This should not happen. */
913
914 warning ("stabs type %d not defined", tp->d.stabs_ref);
915 return 4;
916
917 case ty_alias:
918
919 /* An alias. Return the size of the referenced type. */
920
921 return type_size (tp->d.alias);
922
923 case ty_pointer:
924 case ty_ref:
925
926 /* Pointers and references are 32-bit values. */
927
928 return 4;
929
930 case ty_struc:
931
932 /* The size of a structure is stored in the type structure. */
933
934 if (tp->d.struc.flags & STRUC_FORWARD)
935 {
936 warning ("size of incomplete structure %s is unknown",
937 tp->d.struc.name);
938 return 0;
939 }
940 return tp->d.struc.size;
941
942 case ty_class:
943
944 /* The size of a class is stored in the type structure. */
945
946 return tp->d.class.size;
947
948 case ty_enu:
949
950 /* Return the size of the base type for an enumeration type. */
951
952 return type_size (tp->d.enu.type);
953
954 case ty_array:
955
956 /* Multiply the element size with the number of array elements
957 for arrays. */
958
959 return ((tp->d.array.last + 1 - tp->d.array.first) *
960 type_size (tp->d.array.etype));
961
962 case ty_bits:
963
964 /* Use the size of the base type for bitfields. */
965
966 return type_size (tp->d.bits.type);
967
968 case ty_member:
969
970 /* Use the size of the member's type for members. */
971
972 return type_size (tp->d.member.type);
973
974 case ty_baseclass:
975
976 /* The size of a base class is the size of the base class :-) */
977
978 return type_size (tp->d.baseclass.type);
979
980 case ty_func:
981 case ty_args:
982 case ty_types:
983 case ty_fields:
984 case ty_values:
985 case ty_memfunc:
986
987 /* This cannot not happen. */
988
989 error ("type_size: cannot compute size for tag %d", tp->tag);
990
991 default:
992
993 /* This cannot happen. */
994
995 error ("type_size: unknown type: %d", tp->tag);
996 }
997}
998
999
1000/* Turn a struct into a class. This needs to be done when a struct is
1001 referenced as base class (unfortunately, a simple class looks like
1002 a struct). */
1003
1004static void struct_to_class (struct type *tp)
1005{
1006 struct type t, *t1, **list, **types;
1007 struct field *fields;
1008 int i, n;
1009
1010 if (tp->index != -1)
1011 {
1012 warning ("Base class cannot be converted from struct");
1013 return;
1014 }
1015 n = tp->d.struc.count;
1016 if (n == 0)
1017 list = NULL;
1018 else
1019 {
1020 if (tp->d.struc.fields->tag != ty_fields
1021 || tp->d.struc.fields->d.fields.count != n)
1022 {
1023 warning ("Invalid struct field list");
1024 return;
1025 }
1026 fields = tp->d.struc.fields->d.fields.list;
1027 if (tp->d.struc.types->tag != ty_types
1028 || tp->d.struc.types->d.types.count != n)
1029 {
1030 warning ("Invalid struct types list");
1031 return;
1032 }
1033 types = tp->d.struc.types->d.types.list;
1034 list = xmalloc (n * sizeof (*list));
1035 for (i = 0; i < n; ++i)
1036 {
1037 t.tag = ty_member;
1038 t.index = -1;
1039 t.d.member.type = types[i];
1040 t.d.member.offset = fields[i].offset;
1041 t.d.member.flags = VIS_PUBLIC;
1042 t.d.member.name = fields[i].name;
1043 t.d.member.sname = NULL;
1044 list[i] = type_add (&t);
1045 }
1046 }
1047
1048 t.tag = ty_types;
1049 t.index = -1;
1050 t.d.types.count = n;
1051 t.d.types.list = list;
1052 t1 = type_add (&t);
1053
1054 t = *tp;
1055 tp->tag = ty_class;
1056 tp->d.class.members = t1;
1057 tp->d.class.size = t.d.struc.size;
1058 tp->d.class.count = t.d.struc.count;
1059 tp->d.class.name = t.d.struc.name;
1060}
1061
1062
1063/* Build and return the internal representation of the `long long'
1064 type. As IPMD doesn't know about `long long' we use the following
1065 structure instead:
1066
1067 struct _long_long
1068 {
1069 unsigned long lo, hi;
1070 };
1071
1072 This function could be optimized by remembering the type in a
1073 global variable. */
1074
1075static struct type *make_long_long (void)
1076{
1077 struct type t, *t1, *t2, *t3, *types[2];
1078 struct field fields[2];
1079
1080 /* Let t1 be `unsigned long'. */
1081
1082 t.tag = ty_prim;
1083 t.index = 0x86; /* 32 bit unsigned */
1084 t1 = type_add (&t);
1085
1086 /* Let t2 be the field types: unsigned long, unsigned long. */
1087
1088 types[0] = t1;
1089 types[1] = t1;
1090 t.tag = ty_types;
1091 t.index = -1;
1092 t.d.types.count = 2;
1093 t.d.types.list = types;
1094 t2 = type_add (&t);
1095
1096 /* Let t3 be the fields: lo (at 0), hi (at 4). */
1097
1098 fields[0].offset = 0; fields[0].name = strpool_add (str_pool, "lo");
1099 fields[1].offset = 4; fields[1].name = strpool_add (str_pool, "hi");
1100 t.tag = ty_fields;
1101 t.index = -1;
1102 t.d.fields.count = 2;
1103 t.d.fields.list = fields;
1104 t3 = type_add (&t);
1105
1106 /* Build the structure type from t1, t2 and t3. */
1107
1108 t.tag = ty_struc;
1109 t.d.struc.count = 2;
1110 t.d.struc.size = 8;
1111 t.d.struc.types = t2;
1112 t.d.struc.fields = t3;
1113 t.d.struc.name = strpool_add (str_pool, "_long_long");
1114 t.d.struc.flags = 0;
1115 return type_add (&t);
1116}
1117
1118
1119/* Parse a (signed) long long number in a stabs type. The number is
1120 given in octal or decimal notation. The result is stored to
1121 *NUMBER. This function returns TRUE iff successful. We don't
1122 check for overflow. */
1123
1124static int parse_long_long (long long *number)
1125{
1126 long long n;
1127 int neg, base;
1128
1129 n = 0; neg = FALSE; base = 10;
1130 if (*parse_ptr == '-')
1131 {
1132 neg = TRUE;
1133 ++parse_ptr;
1134 }
1135 if (*parse_ptr == '0')
1136 base = 8;
1137 if (!(*parse_ptr >= '0' && *parse_ptr < base + '0'))
1138 {
1139 *number = 0;
1140 return FALSE;
1141 }
1142 while (*parse_ptr >= '0' && *parse_ptr < base + '0')
1143 {
1144 n = n * base + (*parse_ptr - '0');
1145 ++parse_ptr;
1146 }
1147 *number = (neg ? -n : n);
1148 return TRUE;
1149}
1150
1151
1152/* Parse a (signed) number in a stabs type. The number is given in
1153 octal or decimal notation. The result is stored to *NUMBER. This
1154 function returns TRUE iff successful. We don't check for
1155 overflow. */
1156
1157static int parse_number (long *number)
1158{
1159 long long n;
1160 int result;
1161
1162 result = parse_long_long (&n);
1163 *number = (long)n;
1164 return result;
1165}
1166
1167
1168/* Check the next character in a stabs type for C. If the next
1169 character matches C, the character is skipped and TRUE is
1170 returned. Otherwise, a warning message is displayed and FALSE is
1171 returned. */
1172
1173static int parse_char (char c)
1174{
1175 if (*parse_ptr != c)
1176 {
1177 warning ("Invalid symbol data: `%c' expected", c);
1178 return FALSE;
1179 }
1180 ++parse_ptr;
1181 return TRUE;
1182}
1183
1184
1185/* Parse mangled arguments of a member functions. For instance, ic
1186 means an `int' argument and a `char' argument. Return FALSE on
1187 failure. */
1188
1189static int parse_mangled_args (void)
1190{
1191 while (*parse_ptr != ';')
1192 {
1193 switch (*parse_ptr)
1194 {
1195 case 0:
1196 warning ("Missing semicolon after mangled arguments");
1197 return FALSE;
1198 default:
1199 /* TODO */
1200 break;
1201 }
1202 ++parse_ptr;
1203 }
1204 ++parse_ptr; /* Skip semicolon */
1205 return TRUE;
1206}
1207
1208
1209/* Parse the visibility of a class or member. */
1210
1211static int parse_visibility (void)
1212{
1213 switch (*parse_ptr)
1214 {
1215 case '0':
1216 ++parse_ptr;
1217 return VIS_PRIVATE;
1218 case '1':
1219 ++parse_ptr;
1220 return VIS_PROTECTED;
1221 case '2':
1222 ++parse_ptr;
1223 return VIS_PUBLIC;
1224 default:
1225 ++parse_ptr;
1226 warning ("Invalid visibility: %c", *parse_ptr);
1227 if (*parse_ptr != 0)
1228 ++parse_ptr;
1229 return VIS_PUBLIC;
1230 }
1231}
1232
1233
1234/* Add TYPE_NAME to `str_pool', replacing a NULL string with a unique
1235 string. */
1236
1237static const char *add_struct_name (const char *type_name)
1238{
1239 char tmp[32];
1240
1241 if (type_name == NULL)
1242 {
1243 sprintf (tmp, "__%d", unnamed_struct_number++);
1244 type_name = tmp;
1245 }
1246 return strpool_add (str_pool, type_name);
1247}
1248
1249
1250/* Parse a stabs type (which is named TYPE_NAME; TYPE_NAME is NULL for
1251 an unnamed type) and return the internal representation of that
1252 type. */
1253
1254static struct type *parse_type (const char *type_name)
1255{
1256 const char *saved_ptr, *p1;
1257 struct type t, *result, *t1, *t2, *t3, **tlist;
1258 int i, n, class_flag, is_void;
1259 long num1, num2, size, lo, hi, valid, offset, width, code;
1260 long long range_lo, range_hi;
1261 struct tmp_field *tmp_fields, *tf;
1262 struct value *tmp_values, *tv;
1263 struct grow g1 = GROW_INIT;
1264 enum type_tag tt;
1265
1266 valid = TRUE; result = NULL;
1267 t.index = -1;
1268 switch (*parse_ptr)
1269 {
1270 case '0': case '1': case '2': case '3': case '4':
1271 case '5': case '6': case '7': case '8': case '9':
1272 if (!parse_number (&num1))
1273 goto syntax;
1274 /* Special case: self reference => void */
1275 is_void = FALSE;
1276 if (*parse_ptr == '=' && parse_ptr[1] >= '0' && parse_ptr[1] <= '9')
1277 {
1278 saved_ptr = parse_ptr++;
1279 if (!parse_number (&num2))
1280 goto syntax;
1281 if (num2 == num1)
1282 is_void = TRUE;
1283 else
1284 parse_ptr = saved_ptr;
1285 }
1286 if (is_void)
1287 {
1288 t.tag = ty_prim;
1289 t.index = 0x97; /* void */
1290 result = type_add (&t);
1291 stype_add (num1, result);
1292 }
1293 else if (*parse_ptr == '=')
1294 {
1295 /* Nested definition */
1296 ++parse_ptr;
1297 result = parse_type (type_name);
1298 stype_add (num1, result);
1299 }
1300 else
1301 {
1302 result = stype_find (num1);
1303 if (result == NULL)
1304 {
1305 t.tag = ty_stabs_ref;
1306 t.d.stabs_ref = num1;
1307 }
1308 }
1309 break;
1310
1311 case 'r':
1312
1313 /* A range type: r<base_type>;<lower_bound>;<upper_bound>;
1314
1315 Special cases:
1316
1317 lower_bound | upper_bound | type
1318 ------------+-------------+------------------------
1319 n > 0 | 0 | floating point, n bytes
1320
1321 */
1322
1323 ++parse_ptr;
1324 if (!parse_number (&num1) || !parse_char (';')
1325 || !parse_long_long (&range_lo) || !parse_char (';')
1326 || !parse_long_long (&range_hi) || !parse_char (';'))
1327 goto syntax;
1328 t.tag = ty_prim;
1329 if (range_lo == -128 && range_hi == 127)
1330 t.index = 0x80; /* 8 bit signed */
1331 else if (range_lo == -32768 && range_hi == 32767)
1332 t.index = 0x80; /* 16 bit signed */
1333 else if (range_lo == -2147483647 - 1 && range_hi == 2147483647)
1334 t.index = 0x82; /* 32 bit signed */
1335 else if (range_lo == 0 && range_hi == 127)
1336 t.index = 0x84; /* 8 bit character */
1337 else if (range_lo == 0 && range_hi == 255)
1338 t.index = 0x84; /* 8 bit unsigned */
1339 else if (range_lo == 0 && range_hi == 65535)
1340 t.index = 0x85; /* 16 bit unsigned */
1341 else if (range_lo == 0 && range_hi == 0xffffffff)
1342 t.index = 0x86; /* 32 bit unsigned */
1343 else if (range_lo == 0 && range_hi == -1)
1344 {
1345 if (type_name != NULL
1346 && (strcmp (type_name, "long long unsigned int") == 0
1347 || strcmp (type_name, "long long int") == 0))
1348 result = make_long_long ();
1349 else
1350 t.index = 0x86; /* 32 bit unsigned */
1351 }
1352 else if ((unsigned long long)range_lo == 0x8000000000000000ULL
1353 && range_hi == 0x7fffffffffffffffLL)
1354 result = make_long_long ();
1355 else if (range_lo == 4 && range_hi == 0)
1356 t.index = 0x88; /* 32 bit real */
1357 else if (range_lo == 8 && range_hi == 0)
1358 t.index = 0x89; /* 64 bit real */
1359 else if (range_lo == 12 && range_hi == 0)
1360 t.index = 0x8a; /* 80 bit real */
1361 else
1362 {
1363 warning ("Unknown range type: %lld..%lld", range_lo, range_hi);
1364 goto syntax;
1365 }
1366 break;
1367
1368 case '*':
1369
1370 /* A pointer type: *<type> */
1371
1372 ++parse_ptr;
1373 t1 = parse_type (NULL);
1374 t.tag = ty_pointer;
1375 t.d.pointer = t1;
1376 break;
1377
1378 case '&':
1379
1380 /* A reference type (C++): &<type> */
1381
1382 ++parse_ptr;
1383 t1 = parse_type (NULL);
1384 t.tag = ty_ref;
1385 t.d.ref = t1;
1386 break;
1387
1388 case '#':
1389
1390 /* A member function:
1391
1392 short format: ##<return_type>;
1393
1394 long format: #<domain_type>,<return_type>{,<arg_type>};
1395
1396 */
1397
1398 ++parse_ptr;
1399 if (*parse_ptr == '#')
1400 {
1401 ++parse_ptr;
1402 t1 = parse_type (NULL);
1403 if (!parse_char (';'))
1404 goto syntax;
1405 }
1406 else
1407 {
1408 t2 = parse_type (NULL); /* Domain type */
1409 if (!parse_char (','))
1410 goto syntax;
1411 t1 = parse_type (NULL); /* Return type */
1412 while (*parse_ptr != ';')
1413 {
1414 if (!parse_char (','))
1415 goto syntax;
1416 t2 = parse_type (NULL); /* Argument type */
1417 }
1418 ++parse_ptr;
1419 }
1420
1421 /* Make a function type from the return type t1 */
1422
1423 t.tag = ty_func;
1424 t.d.func.ret = t1;
1425 t.d.func.args = NULL;
1426 t.d.func.arg_count = 0;
1427 break;
1428
1429 case '@':
1430
1431 /* Special GNU-defined types:
1432
1433 @s<bits>;<code>;
1434
1435 BITS: Number of bits
1436 CODE: Identifies the type:
1437 -16 bool
1438 -20 char
1439
1440
1441 */
1442
1443 ++parse_ptr;
1444 switch (*parse_ptr)
1445 {
1446 case 's':
1447 ++parse_ptr;
1448 if (!parse_number (&size) || !parse_char (';'))
1449 goto syntax;
1450 if (!parse_number (&code))
1451 goto syntax;
1452 if (size == 8 && code == -16)
1453 {
1454 t.tag = ty_prim;
1455 t.index = 0x90; /* 8 bit boolean */
1456 }
1457 else
1458 {
1459 warning ("Unknown GNU extension type code: %ld, %ld bits",
1460 code, size);
1461 goto syntax;
1462 }
1463 if (!parse_char (';'))
1464 goto syntax;
1465 break;
1466
1467 default:
1468 warning ("Unknown GNU extension type: @%c", *parse_ptr);
1469 goto syntax;
1470 }
1471 break;
1472
1473 case 'x':
1474
1475 /* Incomplete type: xs<name>:
1476 xu<name>:
1477 xe<name>: */
1478
1479 ++parse_ptr;
1480 switch (*parse_ptr)
1481 {
1482 case 's':
1483 case 'u':
1484 break;
1485 case 'e':
1486 warning ("Cannot convert incomplete enum type");
1487 goto syntax;
1488 default:
1489 warning ("Unknown forward reference: %c", *parse_ptr);
1490 goto syntax;
1491 }
1492 ++parse_ptr;
1493 p1 = strchr (parse_ptr, ':');
1494 if (p1 == NULL)
1495 {
1496 warning ("Invalid forward reference: missing colon");
1497 goto syntax;
1498 }
1499 t.tag = ty_struc;
1500 t.d.struc.types = NULL;
1501 t.d.struc.fields = NULL;
1502 t.d.struc.size = 0;
1503 t.d.struc.count = 0;
1504 t.d.struc.name = strpool_addn (str_pool, parse_ptr, p1 - parse_ptr);
1505 t.d.struc.flags = STRUC_FORWARD;
1506
1507 /* Avoid adding an incomplete type if the complete one is
1508 already defined. We're ignoring scoping (TODO). */
1509
1510 for (t3 = type_head; t3 != NULL; t3 = t3->next)
1511 if ((t3->tag == ty_struc && t3->d.struc.name == t.d.struc.name
1512 && !(t3->d.struc.flags & STRUC_FORWARD))
1513 || (t3->tag == ty_class && t3->d.class.name == t.d.struc.name))
1514 {
1515 result = t3;
1516 break;
1517 }
1518 parse_ptr = p1 + 1;
1519 break;
1520
1521 case 's': /* struct, class */
1522 case 'u': /* union */
1523
1524 /* Structures, unions and classes:
1525
1526 s<size>[<baseclasses>]{<field>}{<method>}
1527
1528 BASECLASSES: !<#baseclasses>,{<baseclass>;}
1529 BASECLASS: <virtuality><visibility><offset>,<type>
1530 FIELD: <name>:[/0|1|2]<type>,<offset>,<width>;
1531 FIELD: <name>:[/0|1|2]<type>:<static_name>;
1532 METHOD: <name>::{<type>:<args>};
1533
1534 */
1535
1536 ++parse_ptr;
1537 class_flag = FALSE;
1538 if (!parse_number (&size))
1539 goto syntax;
1540 grow_init (&g1, &tmp_fields, sizeof (*tmp_fields), 8);
1541
1542 /* Parse the base classes, if present. */
1543
1544 if (*parse_ptr == '!')
1545 {
1546 long i, n;
1547
1548 ++parse_ptr;
1549 class_flag = TRUE;
1550 if (!parse_number (&n) || !parse_char (','))
1551 goto syntax;
1552 for (i = 0; i < n; ++i)
1553 {
1554 grow_by (&g1, 1);
1555 tf = &tmp_fields[g1.count++];
1556 tf->flags = MEMBER_BASE;
1557 tf->name = NULL;
1558 tf->sname = NULL;
1559 switch (*parse_ptr)
1560 {
1561 case '0':
1562 ++parse_ptr;
1563 break;
1564 case '1':
1565 tf->flags |= MEMBER_VIRTUAL;
1566 ++parse_ptr;
1567 break;
1568 default:
1569 warning ("Invalid base class visibility: %c", *parse_ptr);
1570 if (*parse_ptr != 0)
1571 ++parse_ptr;
1572 }
1573
1574 tf->flags |= parse_visibility ();
1575 if (!parse_number (&offset) || !parse_char (','))
1576 goto syntax;
1577 tf->offset = offset >> 3;
1578 t1 = parse_type (NULL);
1579 switch (t1->tag)
1580 {
1581 case ty_class:
1582 break;
1583 case ty_struc:
1584 struct_to_class (t1);
1585 break;
1586 default:
1587 warning ("Invalid base class type");
1588 }
1589 tf->type = t1;
1590 if (!parse_char (';'))
1591 goto syntax;
1592 }
1593 }
1594
1595 while (*parse_ptr != ';')
1596 {
1597 p1 = strchr (parse_ptr, ':');
1598 if (p1 == NULL)
1599 {
1600 warning ("Invalid structure type: missing colon");
1601 goto syntax;
1602 }
1603 n = p1 - parse_ptr;
1604 grow_by (&g1, 1);
1605 tf = &tmp_fields[g1.count++];
1606 tf->flags = VIS_PUBLIC;
1607 tf->name = strpool_addn (str_pool, parse_ptr, n);
1608 parse_ptr = p1 + 1;
1609 if (*parse_ptr == ':')
1610 {
1611 class_flag = TRUE;
1612 ++parse_ptr;
1613 do
1614 {
1615 t1 = parse_type (NULL);
1616 offset = 0;
1617
1618 if (*parse_ptr != ':')
1619 {
1620 warning ("Arguments for member function missing");
1621 goto syntax;
1622 }
1623 ++parse_ptr;
1624 if (!parse_mangled_args ())
1625 goto syntax;
1626 tf->flags = parse_visibility () | MEMBER_FUNCTION;
1627 switch (*parse_ptr)
1628 {
1629 case 'A':
1630 ++parse_ptr;
1631 break;
1632 case 'B':
1633 tf->flags |= MEMBER_CONST;
1634 ++parse_ptr;
1635 break;
1636 case 'C':
1637 tf->flags |= MEMBER_VOLATILE;
1638 ++parse_ptr;
1639 break;
1640 case 'D':
1641 tf->flags |= MEMBER_CONST | MEMBER_VOLATILE;
1642 ++parse_ptr;
1643 break;
1644 default:
1645 warning ("Unknown member function qualifier: %c",
1646 *parse_ptr);
1647 if (*parse_ptr != 0)
1648 ++parse_ptr;
1649 }
1650 switch (*parse_ptr)
1651 {
1652 case '.':
1653 ++parse_ptr;
1654 break;
1655 case '*':
1656 tf->flags |= MEMBER_VIRTUAL;
1657 ++parse_ptr;
1658 if (!parse_number (&offset) || !parse_char (';'))
1659 {
1660 warning ("Invalid data for virtual member function");
1661 goto syntax;
1662 }
1663 parse_type (NULL);
1664 if (!parse_char (';'))
1665 {
1666 warning ("Invalid data for virtual member function");
1667 goto syntax;
1668 }
1669 break;
1670 case '?':
1671 tf->flags |= MEMBER_STATIC;
1672 ++parse_ptr;
1673 break;
1674 default:
1675 warning ("Unknown member function qualifier: %c",
1676 *parse_ptr);
1677 if (*parse_ptr != 0)
1678 ++parse_ptr;
1679 }
1680 tf->type = t1;
1681 tf->offset = offset;
1682 if (*parse_ptr != ';')
1683 {
1684 /* Another member function with the same name
1685 follows. */
1686
1687 grow_by (&g1, 1);
1688 tf = &tmp_fields[g1.count++];
1689 tf->flags = VIS_PUBLIC | MEMBER_FUNCTION;
1690 tf->name = tf[-1].name;
1691 }
1692 } while (*parse_ptr != ';');
1693 ++parse_ptr;
1694 }
1695 else
1696 {
1697 if (*parse_ptr == '/')
1698 {
1699 class_flag = TRUE;
1700 ++parse_ptr;
1701 tf->flags = parse_visibility ();
1702 }
1703 t1 = parse_type (NULL);
1704 if (*parse_ptr == ':')
1705 {
1706 /* Static member */
1707
1708 ++parse_ptr;
1709 tf->flags |= MEMBER_STATIC;
1710 p1 = strchr (parse_ptr, ';');
1711 if (p1 == NULL)
1712 {
1713 warning ("Invalid static member: missing semicolon");
1714 goto syntax;
1715 }
1716 n = p1 - parse_ptr;
1717 tf->sname = strpool_addn (str_pool, parse_ptr, n);
1718 parse_ptr = p1 + 1;
1719 tf->offset = 0;
1720 }
1721 else
1722 {
1723 if (!parse_char (',') || !parse_number (&offset))
1724 goto syntax;
1725 if (*parse_ptr == ',')
1726 {
1727 ++parse_ptr;
1728 if (!parse_number (&width))
1729 goto syntax;
1730 }
1731 else
1732 width = 0; /* vtable pointer field */
1733 if (!parse_char (';'))
1734 goto syntax;
1735 if ((offset & 7) != 0 || width != 8 * type_size (t1))
1736 {
1737 t.tag = ty_bits;
1738 t.d.bits.type = t1;
1739 t.d.bits.start = offset & 7;
1740 t.d.bits.count = width;
1741 t1 = type_add (&t);
1742 }
1743 }
1744 tf->type = t1;
1745 tf->offset = offset >> 3;
1746 }
1747 }
1748 ++parse_ptr;
1749 if (*parse_ptr == '~')
1750 {
1751 /* Type reference to the first base class. This field is
1752 present for classes which contain virtual functions. */
1753
1754 ++parse_ptr;
1755 if (!parse_char ('%'))
1756 goto syntax;
1757 t1 = parse_type (NULL);
1758 if (!parse_char (';'))
1759 goto syntax;
1760 }
1761
1762 if (class_flag)
1763 {
1764 tlist = xmalloc (g1.count * sizeof (*t.d.types.list));
1765 for (i = 0; i < g1.count; ++i)
1766 {
1767 if (tmp_fields[i].flags & MEMBER_FUNCTION)
1768 {
1769 t2 = tmp_fields[i].type;
1770 tt = follow_refs (t2)->tag;
1771 /* TODO: check later if ty_stabs_ref */
1772 if (tt != ty_func && tt != ty_stabs_ref)
1773 error ("Invalid type for member function %s",
1774 tmp_fields[i].name);
1775 t.tag = ty_memfunc;
1776 t.d.memfunc.type = t2;
1777 t.d.memfunc.name = tmp_fields[i].name;
1778 t.d.memfunc.flags = tmp_fields[i].flags;
1779 t.d.memfunc.offset = tmp_fields[i].offset;
1780 }
1781 else if (tmp_fields[i].flags & MEMBER_BASE)
1782 {
1783 t.tag = ty_baseclass;
1784 t.d.baseclass.type = tmp_fields[i].type;
1785 t.d.baseclass.flags = tmp_fields[i].flags;
1786 t.d.baseclass.offset = tmp_fields[i].offset;
1787 }
1788 else
1789 {
1790 t.tag = ty_member;
1791 t.d.member.type = tmp_fields[i].type;
1792 t.d.member.offset = tmp_fields[i].offset;
1793 t.d.member.flags = tmp_fields[i].flags;
1794 t.d.member.name = tmp_fields[i].name;
1795 t.d.member.sname = tmp_fields[i].sname;
1796 }
1797 t2 = type_add (&t);
1798 tlist[i] = t2;
1799 }
1800 t.tag = ty_types;
1801 t.d.types.count = g1.count;
1802 t.d.types.list = tlist;
1803 t1 = type_add (&t);
1804 free (tlist);
1805
1806 t.tag = ty_class;
1807 t.d.class.members = t1;
1808 t.d.class.size = size;
1809 t.d.class.count = g1.count;
1810 t.d.class.name = add_struct_name (type_name);
1811
1812 /* Check if there is an incomplete type waiting to be filled
1813 in (forward reference). We're ignoring scoping (TODO). */
1814
1815 for (t3 = type_head; t3 != NULL; t3 = t3->next)
1816 if (t3->tag == ty_struc && t3->d.struc.name == t.d.class.name
1817 && t3->d.struc.types == NULL && t3->d.struc.fields == NULL
1818 && (t3->d.struc.flags & STRUC_FORWARD))
1819 {
1820 if (t3->index != -1)
1821 warning ("This cannot happen, case 1");
1822 result = t3;
1823 t3->tag = ty_class;
1824 t3->d.class = t.d.class;
1825 break;
1826 }
1827 }
1828 else
1829 {
1830 t.tag = ty_types;
1831 t.d.types.count = g1.count;
1832 t.d.types.list = xmalloc (g1.count * sizeof (*t.d.types.list));
1833 for (i = 0; i < g1.count; ++i)
1834 t.d.types.list[i] = tmp_fields[i].type;
1835 t1 = type_add (&t);
1836 free (t.d.types.list);
1837
1838 t.tag = ty_fields;
1839 t.d.fields.count = g1.count;
1840 t.d.fields.list = xmalloc (g1.count * sizeof (*t.d.fields.list));
1841 for (i = 0; i < g1.count; ++i)
1842 {
1843 t.d.fields.list[i].offset = tmp_fields[i].offset;
1844 t.d.fields.list[i].name = tmp_fields[i].name;
1845 }
1846 t2 = type_add (&t);
1847
1848 t.tag = ty_struc;
1849 t.d.struc.count = g1.count;
1850 t.d.struc.size = size;
1851 t.d.struc.types = t1;
1852 t.d.struc.fields = t2;
1853 t.d.struc.name = add_struct_name (type_name);
1854 t.d.struc.flags = 0;
1855
1856 /* Check if there is an incomplete type waiting to be filled
1857 in (forward reference). We're ignoring scoping (TODO). */
1858
1859 for (t3 = type_head; t3 != NULL; t3 = t3->next)
1860 if (t3->tag == ty_struc && t3->d.struc.name == t.d.struc.name
1861 && t3->d.struc.types == NULL && t3->d.struc.fields == NULL
1862 && (t3->d.struc.flags & STRUC_FORWARD))
1863 {
1864 if (t3->index != -1)
1865 warning ("This cannot happen, case 1");
1866 result = t3;
1867 t3->d.struc = t.d.struc;
1868 break;
1869 }
1870 }
1871 grow_free (&g1);
1872 break;
1873
1874 case 'e':
1875
1876 /* Enumeration type: e{<name>:<value>,}; */
1877
1878 ++parse_ptr;
1879 grow_init (&g1, &tmp_values, sizeof (*tmp_values), 8);
1880 while (*parse_ptr != ';')
1881 {
1882 p1 = strchr (parse_ptr, ':');
1883 if (p1 == NULL)
1884 {
1885 warning ("Invalid enum type: missing colon");
1886 goto syntax;
1887 }
1888 n = p1 - parse_ptr;
1889 grow_by (&g1, 1);
1890 tv = &tmp_values[g1.count++];
1891 tv->name = strpool_addn (str_pool, parse_ptr, n);
1892 parse_ptr = p1 + 1;
1893 if (!parse_number (&tv->index) || !parse_char (','))
1894 goto syntax;
1895 }
1896 ++parse_ptr;
1897 t.tag = ty_values;
1898 t.d.values.count = g1.count;
1899 t.d.values.list = xmalloc (g1.count * sizeof (*t.d.values.list));
1900 if (g1.count == 0)
1901 lo = hi = 0;
1902 else
1903 lo = hi = tmp_values[0].index;
1904 for (i = 0; i < g1.count; ++i)
1905 {
1906 t.d.values.list[i].index = tmp_values[i].index;
1907 t.d.values.list[i].name = tmp_values[i].name;
1908 if (tmp_values[i].index < lo)
1909 lo = tmp_values[i].index;
1910 if (tmp_values[i].index > hi)
1911 hi = tmp_values[i].index;
1912 }
1913 t1 = type_add (&t);
1914
1915 t.tag = ty_prim;
1916 t.index = 0x82; /* 32 bit signed */
1917 t2 = type_add (&t);
1918
1919 t.tag = ty_enu;
1920 t.index = -1;
1921 t.d.enu.values = t1;
1922 t.d.enu.type = t2;
1923 t.d.enu.name = add_struct_name (type_name);
1924 t.d.enu.first = lo;
1925 t.d.enu.last = hi;
1926 grow_free (&g1);
1927 break;
1928
1929 case 'a':
1930
1931 /* An array: ar<index_type>;<lower_bound>;<upper_bound>;<el_type> */
1932
1933 ++parse_ptr;
1934 if (!parse_char ('r'))
1935 goto syntax;
1936 if (strncmp (parse_ptr, "0;", 2) == 0)
1937 {
1938 /* This invalid type index seems to be caused by a bug of
1939 GCC 2.8.1. Replace with a 32-bit signed integer. */
1940
1941 t.tag = ty_prim;
1942 t.index = 0x82; /* 32 bit signed */
1943 t1 = type_add (&t);
1944 ++parse_ptr;
1945 }
1946 else
1947 t1 = parse_type (NULL); /* Index type */
1948 if (!parse_char (';')
1949 || !parse_number (&lo) || !parse_char (';') /* Lower bound */
1950 || !parse_number (&hi) || !parse_char (';')) /* Upper bound */
1951 goto syntax;
1952 t2 = parse_type (NULL); /* Elements type */
1953 if (lo == 0 && hi == 0) /* Variable-length array */
1954 {
1955 t.tag = ty_pointer; /* Turn into pointer */
1956 t.d.pointer = t2;
1957 }
1958 else
1959 {
1960 t.tag = ty_array;
1961 t.d.array.first = lo;
1962 t.d.array.last = hi;
1963 t.d.array.itype = t1;
1964 t.d.array.etype = t2;
1965 }
1966 break;
1967
1968 case 'f':
1969
1970 /* A function: f<return_type> */
1971
1972 ++parse_ptr;
1973 t1 = parse_type (NULL); /* Return type */
1974 t.tag = ty_func;
1975 t.d.func.ret = t1;
1976 t.d.func.args = NULL;
1977 t.d.func.arg_count = 0;
1978 break;
1979
1980 default:
1981 warning ("Unknown type: %c", *parse_ptr);
1982 goto syntax;
1983 }
1984
1985 /* Add the type to the tables. */
1986
1987 if (result == NULL)
1988 result = type_add (&t);
1989 return result;
1990
1991syntax:
1992 grow_free (&g1);
1993 t.tag = ty_prim;
1994 t.index = 0x82; /* 32 bit signed */
1995 return type_add (&t);
1996}
1997
1998
1999/* Parse a stabs type (the name of the type is TYPE_NAME, which is
2000 NULL for an unnamed type) and check for end of the string. If not
2001 all characters of the string have been parsed, a warning message is
2002 displayed. Return the internal representation of the type. */
2003
2004static struct type *parse_complete_type (const char *type_name)
2005{
2006 struct type *tp;
2007
2008 tp = parse_type (type_name);
2009 if (*parse_ptr != 0)
2010 warning ("unexpected character at end of stabs type: %c", *parse_ptr);
2011 return tp;
2012}
2013
2014
2015/* Create an HLL type number for type TP. If a type number has already
2016 been assigned to TP, use that number. Otherwise, allocate a new
2017 (complex) type number and define the type in the $$TYPES segment.
2018 The HLL type number us stored to *HLL.
2019
2020 Note: This function must not be called between sst_start() and
2021 sst_end() or tt_start() and tt_end() as entries are made both in
2022 the SST ($$SYMBOLS) and the type table ($$TYPES). */
2023
2024static void make_type (struct type *tp, int *hll)
2025{
2026 struct type *t1;
2027 int ti_1, ti_2, i, qual;
2028 int patch1;
2029
2030#define RETURN(X) do {*hll = (X); return;} while (0)
2031
2032 if (tp->index == -2)
2033 {
2034 warning ("Cycle detected by make_type");
2035 RETURN (0);
2036 }
2037 if (tp->index != -1)
2038 RETURN (tp->index);
2039 tp->index = -2;
2040 switch (tp->tag)
2041 {
2042 case ty_stabs_ref:
2043 t1 = stype_find (tp->d.stabs_ref);
2044 if (t1 == NULL)
2045 {
2046 warning ("Undefined stabs type %d referenced", tp->d.stabs_ref);
2047 RETURN (0x82); /* 32 bit signed */
2048 }
2049 make_type (t1, &tp->index);
2050 *hll = tp->index;
2051 break;
2052
2053 case ty_alias:
2054 make_type (tp->d.alias, &tp->index);
2055 *hll = tp->index;
2056 break;
2057
2058 case ty_pointer:
2059 ti_1 = tp->d.pointer->index;
2060 if (ti_1 >= 0x80 && ti_1 <= 0xa0)
2061 {
2062 tp->index = ti_1 + 0x20;
2063 RETURN (tp->index);
2064 }
2065 *hll = tp->index = hll_type_index++;
2066 tt_start (0x7a, 0x01); /* 32-bit near pointer */
2067 patch1 = tt.size;
2068 tt_index (0);
2069 tt_end ();
2070 make_type (tp->d.pointer, &ti_1);
2071 buffer_patch_word (&tt, patch1+1, ti_1);
2072 break;
2073
2074 case ty_ref:
2075 *hll = tp->index = hll_type_index++;
2076 tt_start (0x48, 0x00); /* Reference type (C++) */
2077 patch1 = tt.size;
2078 tt_word (0);
2079 tt_end ();
2080 make_type (tp->d.ref, &ti_1);
2081 buffer_patch_word (&tt, patch1, ti_1);
2082 break;
2083
2084 case ty_struc:
2085 tt_start (0x79, 0x00); /* Structure/Union */
2086 tt_dword (tp->d.struc.size);
2087 tt_word (tp->d.struc.count);
2088 patch1 = tt.size;
2089 tt_index (0);
2090 tt_index (0);
2091 tt_name (tp->d.struc.name);
2092 tt_end ();
2093 *hll = tp->index = hll_type_index++;
2094 if (tp->d.struc.types == NULL && tp->d.struc.fields == NULL)
2095 {
2096 ti_1 = 0;
2097 ti_2 = 1;
2098 }
2099 else
2100 {
2101 make_type (tp->d.struc.types, &ti_1);
2102 make_type (tp->d.struc.fields, &ti_2);
2103 }
2104 buffer_patch_word (&tt, patch1+1, ti_1);
2105 buffer_patch_word (&tt, patch1+4, ti_2);
2106 break;
2107
2108 case ty_class:
2109 *hll = tp->index = hll_type_index++;
2110 tt_start (0x40, 0x00); /* Class */
2111 tt_dword (tp->d.class.size);
2112 tt_word (tp->d.class.count);
2113 patch1 = tt.size;
2114 tt_word (0); /* Members */
2115 tt_enc (tp->d.class.name); /* Class name */
2116 tt_end ();
2117 make_type (tp->d.class.members, &ti_1);
2118 buffer_patch_word (&tt, patch1, ti_1);
2119 break;
2120
2121 case ty_member:
2122 make_type (tp->d.member.type, &ti_1);
2123 qual = 0;
2124 if (tp->d.member.flags & MEMBER_STATIC)
2125 qual |= 0x01;
2126 tt_start (0x46, qual);
2127 tt_byte (tp->d.member.flags & MEMBER_VIS);
2128 tt_word (ti_1);
2129 tt_unsigned_span (tp->d.member.offset);
2130 if (tp->d.member.flags & MEMBER_STATIC)
2131 tt_enc (tp->d.member.sname);
2132 else
2133 tt_enc ("");
2134 tt_enc (tp->d.member.name);
2135 tt_end ();
2136 *hll = tp->index = hll_type_index++;
2137 break;
2138
2139 case ty_enu:
2140 make_type (tp->d.enu.type, &ti_1);
2141 make_type (tp->d.enu.values, &ti_2);
2142 tt_start (0x7b, 0x00); /* Enum */
2143 tt_index (ti_1); /* Element's data type */
2144 tt_index (ti_2); /* List of values */
2145 tt_signed_span (tp->d.enu.first); /* Minimum index */
2146 tt_signed_span (tp->d.enu.last); /* Maximum index */
2147 tt_name (tp->d.enu.name);
2148 tt_end ();
2149 *hll = tp->index = hll_type_index++;
2150 break;
2151
2152 case ty_array:
2153 make_type (tp->d.array.itype, &ti_1);
2154 make_type (tp->d.array.etype, &ti_2);
2155 tt_start (0x78, 0x00); /* Array */
2156 tt_dword (type_size (tp));
2157 tt_index (ti_1);
2158 tt_index (ti_2);
2159 tt_name (""); /* Name */
2160 tt_end ();
2161 *hll = tp->index = hll_type_index++;
2162 break;
2163
2164 case ty_bits:
2165 tt_start (0x5c, 0x0a); /* Bit string */
2166 tt_byte (tp->d.bits.start);
2167 tt_unsigned_span (tp->d.bits.count);
2168 tt_end ();
2169 *hll = tp->index = hll_type_index++;
2170 break;
2171
2172 case ty_func:
2173 make_type (tp->d.func.ret, &ti_1);
2174 if (tp->d.func.args == NULL)
2175 ti_2 = 0;
2176 else
2177 make_type (tp->d.func.args, &ti_2);
2178 tt_start (0x54, 0x05); /* Function */
2179 tt_word (tp->d.func.arg_count); /* Number of parameters */
2180 tt_word (tp->d.func.arg_count); /* Max. Number of parameters */
2181 tt_index (ti_1); /* Return value */
2182 tt_index (ti_2); /* Argument list */
2183 tt_end ();
2184 *hll = tp->index = hll_type_index++;
2185 break;
2186
2187 case ty_args:
2188 if (tp->d.args.count == 0)
2189 {
2190 tp->index = 0;
2191 RETURN (tp->index);
2192 }
2193 for (i = 0; i < tp->d.args.count; ++i)
2194 make_type (tp->d.args.list[i], &ti_1);
2195 tt_start (0x7f, 0x04); /* List (function parameters) */
2196 for (i = 0; i < tp->d.args.count; ++i)
2197 {
2198 tt_byte (0x01); /* Flag: pass by value, no descriptor */
2199 tt_index (tp->d.args.list[i]->index);
2200 }
2201 tt_end ();
2202 *hll = tp->index = hll_type_index++;
2203 break;
2204
2205 case ty_types:
2206 if (tp->d.types.count == 0)
2207 {
2208 tp->index = 0;
2209 RETURN (tp->index);
2210 }
2211 *hll = tp->index = hll_type_index++;
2212 tt_start (0x7f, 0x01); /* List (structure field types) */
2213 patch1 = tt.size;
2214 for (i = 0; i < tp->d.types.count; ++i)
2215 tt_index (0);
2216 tt_end ();
2217 for (i = 0; i < tp->d.types.count; ++i)
2218 {
2219 make_type (tp->d.types.list[i], &ti_1);
2220 buffer_patch_word (&tt, patch1 + 1 + 3*i, ti_1);
2221 }
2222 break;
2223
2224 case ty_fields:
2225 if (tp->d.fields.count == 0)
2226 {
2227 tp->index = 0;
2228 RETURN (tp->index);
2229 }
2230 tt_start (0x7f, 0x02); /* List (structure field names) */
2231 for (i = 0; i < tp->d.fields.count; ++i)
2232 {
2233 tt_name (tp->d.fields.list[i].name);
2234 tt_unsigned_span (tp->d.fields.list[i].offset);
2235 }
2236 tt_end ();
2237 *hll = tp->index = hll_type_index++;
2238 break;
2239
2240 case ty_values:
2241 if (tp->d.values.count == 0)
2242 {
2243 tp->index = -1;
2244 RETURN (tp->index);
2245 }
2246 tt_start (0x7f, 0x03); /* Enum values */
2247 for (i = 0; i < tp->d.values.count; ++i)
2248 {
2249 tt_name (tp->d.values.list[i].name);
2250 tt_unsigned_span (tp->d.values.list[i].index);
2251 }
2252 tt_end ();
2253 *hll = tp->index = hll_type_index++;
2254 break;
2255
2256 case ty_memfunc:
2257 make_type (tp->d.memfunc.type, &ti_1);
2258 qual = 0;
2259 if (tp->d.memfunc.flags & MEMBER_STATIC)
2260 qual |= 0x01;
2261 if (tp->d.memfunc.flags & MEMBER_CONST)
2262 qual |= 0x04;
2263 if (tp->d.memfunc.flags & MEMBER_VOLATILE)
2264 qual |= 0x08;
2265 if (tp->d.memfunc.flags & MEMBER_VIRTUAL)
2266 qual |= 0x10;
2267 tt_start (0x45, qual); /* Member Function */
2268 tt_byte (tp->d.memfunc.flags & MEMBER_VIS); /* Protection */
2269 if (tp->d.memfunc.flags & MEMBER_CTOR)
2270 tt_byte (1); /* Constructor */
2271 else if (tp->d.memfunc.flags & MEMBER_DTOR)
2272 tt_byte (2); /* Destructor */
2273 else
2274 tt_byte (0); /* Regular member function */
2275 tt_word (ti_1); /* Type index of function */
2276 if (tp->d.memfunc.flags & MEMBER_VIRTUAL)
2277 tt_unsigned_span (tp->d.memfunc.offset); /* Index into vtable */
2278 tt_enc (tp->d.memfunc.name); /* Name of the function */
2279 tt_end ();
2280 *hll = tp->index = hll_type_index++;
2281 break;
2282
2283 case ty_baseclass:
2284 make_type (tp->d.baseclass.type, &ti_1);
2285 qual = 0;
2286 if (tp->d.baseclass.flags & MEMBER_VIRTUAL)
2287 qual |= 0x01;
2288 tt_start (0x41, qual); /* Base Class */
2289 tt_byte (tp->d.baseclass.flags & MEMBER_VIS);
2290 tt_word (ti_1); /* Type */
2291 tt_unsigned_span (tp->d.baseclass.offset);
2292 tt_end ();
2293 *hll = tp->index = hll_type_index++;
2294 break;
2295
2296 default:
2297 error ("make_type: unknown tag %d", tp->tag);
2298 }
2299}
2300
2301
2302/* Parse an unnamed stabs type and return an HLL type index for it. */
2303
2304static int hll_type (void)
2305{
2306 struct type *tp;
2307 int ti;
2308
2309 tp = parse_complete_type (NULL);
2310 make_type (tp, &ti);
2311 return ti;
2312}
2313
2314
2315/* Return the symbol name of symbol *INDEX, concatenating the names of
2316 successive symbol table entries if necessary. A pointer to the
2317 string is stored to *STR. If symbols are concatenated, *INDEX is
2318 adjusted to refer to the last symbol table entry used, in order to
2319 refer to the next symbol after incrementing by one. If TRUE is
2320 returned, the caller should free the string when it is no longer
2321 needed. If FALSE is returned, the string must not be freed. */
2322
2323static int concat_symbols (int *index, const char **str)
2324{
2325 int i, n, len;
2326 char *new;
2327 const char *p;
2328
2329 len = 0;
2330 for (i = *index; i < sym_count; ++i)
2331 {
2332 p = str_ptr + sym_ptr[i].string;
2333 n = strlen (p);
2334 if (n > 0 && p[n-1] == '\\')
2335 len += n - 1;
2336 else
2337 {
2338 len += n;
2339 break;
2340 }
2341 }
2342 if (i == *index)
2343 {
2344 *str = str_ptr + sym_ptr[i].string;
2345 return FALSE;
2346 }
2347 else
2348 {
2349 new = xmalloc (len + 1);
2350 *str = new;
2351 for (i = *index; i < sym_count; ++i)
2352 {
2353 p = str_ptr + sym_ptr[i].string;
2354 n = strlen (p);
2355 if (n > 0 && p[n-1] == '\\')
2356 {
2357 memcpy (new, p, n - 1);
2358 new += n - 1;
2359 }
2360 else
2361 {
2362 memcpy (new, p, n);
2363 new += n;
2364 break;
2365 }
2366 }
2367 *new = 0;
2368 *index = i;
2369 return TRUE;
2370 }
2371}
2372
2373
2374/* Parse a stabs type definition and store the internal representation
2375 of that type. The sym_ptr index is passed in INDEX. As we might
2376 have to concatenate successive symbol table entries, *INDEX is
2377 updated to refer to the next symbol. */
2378
2379static void parse_typedef (int *index)
2380{
2381 char *name;
2382 const char *str, *p;
2383 struct type *t;
2384 int n, alloc_flag;
2385
2386 alloc_flag = concat_symbols (index, &str);
2387 p = strchr (str, ':');
2388 if (p != NULL)
2389 {
2390 n = p - str;
2391 name = alloca (n + 1);
2392 memcpy (name, str, n);
2393 name[n] = 0;
2394#if defined (DEBUG)
2395 printf ("LSYM/LCSYM/GSYM/PSYM/RSYM/STSYM/FUN %s\n", str);
2396#endif
2397 parse_ptr = p + 1;
2398 switch (*parse_ptr)
2399 {
2400 case 'f':
2401 case 'F':
2402 ++parse_ptr;
2403 /* Note: don't call parse_complete_type() as there's a comma
2404 at the end for nested functions. */
2405 t = parse_type (NULL);
2406 break;
2407
2408 case 't':
2409 case 'T':
2410 ++parse_ptr;
2411 if (*parse_ptr == 't')
2412 ++parse_ptr; /* synonymous type */
2413 t = parse_complete_type (name);
2414#if defined (DEBUG)
2415 printf (" type: ");
2416 show_type (t);
2417 printf ("\n");
2418#endif
2419 break;
2420
2421 case 'p':
2422 case 'r':
2423 case 'G':
2424 case 'S':
2425 case 'V':
2426 ++parse_ptr;
2427 t = parse_complete_type (name);
2428#if defined (DEBUG)
2429 printf (" type: ");
2430 show_type (t);
2431 printf ("\n");
2432#endif
2433 break;
2434
2435 case '0': case '1': case '2': case '3': case '4':
2436 case '5': case '6': case '7': case '8': case '9':
2437 t = parse_complete_type (name);
2438#if defined (DEBUG)
2439 printf (" type: ");
2440 show_type (t);
2441 printf ("\n");
2442#endif
2443 break;
2444 }
2445 }
2446 if (alloc_flag)
2447 free ((void *)str); /* remove const attribute */
2448}
2449
2450
2451/* Write a `begin' subrecord to the SST. The block begins at ADDR in
2452 the text segment. */
2453
2454static size_t hll_begin (dword addr)
2455{
2456 size_t ptr;
2457 struct reloc r;
2458
2459 sst_start (SST_begin);
2460 r.address = sst.size;
2461 ptr = sst.size;
2462 buffer_dword (&sst, addr); /* Segment offset */
2463 buffer_dword (&sst, 0); /* Length of block -- patched later */
2464#if 0
2465 buffer_nstr (&sst, ""); /* Name of block (optional) */
2466#endif
2467 sst_end ();
2468
2469 r.ext = 0;
2470 r.length = 2;
2471 r.symbolnum = N_TEXT;
2472 r.pcrel = 0;
2473 r.unused = 0;
2474 buffer_mem (&sst_reloc, &r, sizeof (r));
2475 return ptr;
2476}
2477
2478
2479/* Start of a block. Write a `begin' subrecord to the SST. The block
2480 begins at ADDR in the text segment. Push the block onto the block
2481 stack. */
2482
2483static void block_begin (dword addr)
2484{
2485 size_t ptr;
2486
2487 ptr = hll_begin (addr);
2488 grow_by (&block_grow, 1);
2489 block_stack[block_grow.count].patch_ptr = ptr;
2490 block_stack[block_grow.count].addr = addr;
2491 ++block_grow.count;
2492}
2493
2494
2495/* Write an `end' subrecord to the SST. PATCH_PTR is the sst index of
2496 the `begin' subrecord for this block. Add ADD_START to the start
2497 address of the block and set the length of the block to LENGTH, in
2498 bytes. */
2499
2500static void hll_end (size_t patch_ptr, int add_start, size_t length)
2501{
2502 *(dword *)(sst.buf+patch_ptr+0) += add_start;
2503 *(dword *)(sst.buf+patch_ptr+4) = length;
2504 sst_start (SST_end);
2505 sst_end ();
2506}
2507
2508
2509/* Return TRUE iff symbol table entry S defines a function. */
2510
2511static int is_fun (const struct nlist *s)
2512{
2513 const char *p;
2514
2515 if (s->type != N_FUN)
2516 return FALSE;
2517 p = strchr (str_ptr + s->string, ':');
2518 if (p == NULL)
2519 return FALSE;
2520 return (p[1] == 'F' || p[1] == 'f');
2521}
2522
2523
2524/* Approximate a function prologue length by examining the code. The
2525 function starts at START and ends at END. */
2526
2527static int get_prologue_length (dword start, dword end)
2528{
2529 dword i;
2530
2531 if (end > text_size)
2532 end = text_size;
2533 i = start;
2534 if (i < end && text_ptr[i] == 0x55) ++i; /* PUSH EBP */
2535 if (i + 1 < end && text_ptr[i+0] == 0x89
2536 && text_ptr[i+1] == 0xe5) /* MOV EBP, ESP */
2537 i += 2;
2538 if (i + 2 < end && text_ptr[i+0] == 0x83
2539 && text_ptr[i+1] == 0xec) /* SUB ESP, n8 */
2540 i += 3;
2541 return i - start;
2542}
2543
2544
2545/* End of a function reached. Try to find the function length. I is
2546 the sym_ptr index of the current symbol -- we search the symbol
2547 table starting at I to find the next function for estimating the
2548 function length. */
2549
2550static void fun_end (int i)
2551{
2552 int p_len;
2553 dword addr;
2554
2555 addr = text_size;
2556 while (i < sym_count)
2557 {
2558 if (is_fun (&sym_ptr[i]))
2559 {
2560 addr = sym_ptr[i].value;
2561 break;
2562 }
2563 ++i;
2564 }
2565 if (prologue_length != -1)
2566 p_len = prologue_length;
2567 else
2568 p_len = get_prologue_length (proc_start_addr, addr);
2569 *(dword *)(sst.buf+proc_patch_base + 0) = addr - proc_start_addr;
2570 *(word *)(sst.buf+proc_patch_base + 4) = p_len;
2571 *(dword *)(sst.buf+proc_patch_base + 6) = addr - proc_start_addr;
2572 proc_patch_base = 0;
2573 sst_start (SST_end);
2574 sst_end ();
2575}
2576
2577
2578/* End of a block (N_RBRAC encountered). I is the sym_ptr index of
2579 the N_RBRAC symbol. If we reach the outmost block level, we call
2580 fun_end(). */
2581
2582static void block_end (int i)
2583{
2584 if (block_grow.count == 0)
2585 warning ("Invalid N_RBRAC without N_LBRAC");
2586 else
2587 {
2588 --block_grow.count;
2589 hll_end (block_stack[block_grow.count].patch_ptr, 0,
2590 sym_ptr[i].value - block_stack[block_grow.count].addr);
2591 if (block_grow.count == 0 && proc_patch_base != 0)
2592 fun_end (i);
2593 }
2594}
2595
2596
2597/* Define a function. SYMBOL points to the sym_ptr entry. The
2598 function arguments are taken from the args_list array. Several
2599 fields of the SST entry are filled-in later, when reaching the end
2600 of the function. proc_patch_base is used for remembering the place
2601 where to patch the SST entry. */
2602
2603static void define_fun (const struct nlist *symbol)
2604{
2605 struct type t, *t1, *t2;
2606 struct reloc r;
2607 const char *str, *p;
2608 char *name;
2609 int n, ti;
2610
2611 str = str_ptr + symbol->string;
2612 p = strchr (str, ':');
2613 if (p == NULL)
2614 abort ();
2615 n = p - str;
2616 name = alloca (n + 1);
2617 memcpy (name, str, n);
2618 name[n] = 0;
2619
2620 proc_start_addr = symbol->value;
2621
2622 if (args_grow.count == 0)
2623 t1 = NULL;
2624 else
2625 {
2626 t.tag = ty_args;
2627 t.index = -1;
2628 t.d.args.count = args_grow.count;
2629 t.d.args.list = xmalloc (args_grow.count * sizeof (*args_list));
2630 memcpy (t.d.args.list, args_list, args_grow.count * sizeof (*args_list));
2631 t1 = type_add (&t);
2632 free (t.d.args.list);
2633 }
2634 last_fun_type.d.func.args = t1;
2635 last_fun_type.d.func.arg_count = args_grow.count;
2636 t2 = type_add (&last_fun_type);
2637 make_type (t2, &ti);
2638 sst_start (cplusplus_flag ? SST_CPPproc : SST_proc);
2639 r.address = sst.size;
2640 buffer_dword (&sst, symbol->value); /* Segment offset */
2641 buffer_word (&sst, ti); /* Type index */
2642 proc_patch_base = sst.size;
2643 buffer_dword (&sst, 0); /* Length of proc */
2644 buffer_word (&sst, 0); /* Length of prologue */
2645 buffer_dword (&sst, 0); /* Length of prologue and body */
2646 buffer_word (&sst, 0); /* Class type (for member functions) */
2647 buffer_byte (&sst, 8); /* 32-bit near */
2648 if (cplusplus_flag)
2649 buffer_enc (&sst, name); /* Proc name */
2650 else
2651 buffer_nstr (&sst, name); /* Proc name */
2652 sst_end ();
2653 r.ext = 0;
2654 r.length = 2;
2655 r.symbolnum = N_TEXT;
2656 r.pcrel = 0;
2657 r.unused = 0;
2658 buffer_mem (&sst_reloc, &r, sizeof (r));
2659}
2660
2661
2662/* Locate the next N_LBRAC starting at symbol I. If found, set
2663 lbrac_index to the index of N_LBRAC and start a new block. If
2664 there is no N_LBRAC, set lbrac_index to -1. */
2665
2666static void next_lbrac (int i)
2667{
2668 lbrac_index = -1;
2669 for (; i < sym_count; ++i)
2670 if (sym_ptr[i].type == N_RBRAC || is_fun (&sym_ptr[i]))
2671 return;
2672 else if (sym_ptr[i].type == N_LBRAC)
2673 {
2674 lbrac_index = i;
2675 block_begin (sym_ptr[i].value);
2676 return;
2677 }
2678}
2679
2680
2681/* Parse a stabs symbol. The sym_ptr index is passed in INDEX. As we
2682 might have to concatenate successive symbol table entries, *INDEX
2683 is updated to refer to the next symbol. WHERE is the segment where
2684 the symbol is defined (N_DATA, for instance). If WHERE is -1,
2685 there is no segment. MSG is used for warning messages. This
2686 function is called recursively for static functions (F) There's up
2687 to one level of recursion. The arguments of a function are read
2688 twice: If FLAG is TRUE, we turn the arguments into local symbols;
2689 if FLAG is FALSE, we put arguments into the args_list array. */
2690
2691static void parse_symbol (int *index, int where, const char *msg, int flag)
2692{
2693 char *name;
2694 const char *str, *p;
2695 const struct nlist *symbol, *sym2;
2696 struct type *t1;
2697 int i, n, ti, alloc_flag;
2698
2699 symbol = &sym_ptr[*index];
2700 alloc_flag = concat_symbols (index, &str);
2701 p = strchr (str, ':');
2702 if (p != NULL)
2703 {
2704 n = p - str;
2705 name = alloca (n + 1);
2706 memcpy (name, str, n);
2707 name[n] = 0;
2708#if defined (DEBUG)
2709 printf ("%s %s\n", msg, str);
2710#endif
2711 parse_ptr = p + 1;
2712 switch (*parse_ptr)
2713 {
2714 case 'G':
2715
2716 /* Static storage, global scope */
2717
2718 ++parse_ptr;
2719 ti = hll_type ();
2720#if defined (DEBUG)
2721 printf (" type=%#x\n", ti);
2722#endif
2723 sym2 = find_symbol (name);
2724 if (sym2 == NULL)
2725 {
2726 warning ("Cannot find address of global variable %s", name);
2727 return;
2728 }
2729 if (sym2->type == N_EXT)
2730 sst_static (name, 0, ti, sym2 - sym_ptr, 1);
2731 else
2732 sst_static (name, sym2->value, ti, sym2->type, 0);
2733 break;
2734
2735 case 'S':
2736 case 'V':
2737
2738 /* S = static storage, file scope,
2739 V = static storage, local scope */
2740
2741 if (where == -1)
2742 {
2743 warning ("Cannot use symbol type %c with N_%s", *parse_ptr, msg);
2744 return;
2745 }
2746 ++parse_ptr;
2747 ti = hll_type ();
2748#if defined (DEBUG)
2749 printf (" type=%#x\n", ti);
2750#endif
2751 sst_static (name, symbol->value, ti, where, 0);
2752 break;
2753
2754 case 'p':
2755
2756 /* Function argument. */
2757
2758 if (where != -1)
2759 {
2760 warning ("Cannot use argument symbol with N_%s", msg);
2761 return;
2762 }
2763 ++parse_ptr;
2764 if (flag)
2765 {
2766 ti = hll_type ();
2767#if defined (DEBUG)
2768 printf (" type=%#x\n", ti);
2769#endif
2770 sst_start (SST_auto);
2771 buffer_dword (&sst, symbol->value); /* Offset into stk frame */
2772 buffer_word (&sst, ti); /* Type index */
2773 buffer_nstr (&sst, name); /* Symbol name */
2774 sst_end ();
2775 }
2776 else
2777 {
2778 t1 = parse_complete_type (NULL);
2779 grow_by (&args_grow, 1);
2780 args_list[args_grow.count++] = t1;
2781 }
2782 break;
2783
2784 case 'r':
2785
2786 /* Register variable. */
2787
2788 if (where != -1)
2789 {
2790 warning ("Cannot use register symbol with N_%s", msg);
2791 return;
2792 }
2793 ++parse_ptr;
2794 ti = hll_type ();
2795 switch (symbol->value)
2796 {
2797 case 0: /* EAX */
2798 i = 0x10;
2799 break;
2800 case 1: /* ECX */
2801 i = 0x11;
2802 break;
2803 case 2: /* EDX */
2804 i = 0x12;
2805 break;
2806 case 3: /* EBX */
2807 i = 0x13;
2808 break;
2809 case 4: /* ESP */
2810 i = 0x14;
2811 break;
2812 case 5: /* EBP */
2813 i = 0x15;
2814 break;
2815 case 6: /* ESI */
2816 i = 0x16;
2817 break;
2818 case 7: /* EDI */
2819 i = 0x17;
2820 break;
2821 case 12: /* ST(0) */
2822 i = 0x80;
2823 break;
2824 case 13: /* ST(1) */
2825 i = 0x81;
2826 break;
2827 case 14: /* ST(2) */
2828 i = 0x82;
2829 break;
2830 case 15: /* ST(3) */
2831 i = 0x83;
2832 break;
2833 case 16: /* ST(4) */
2834 i = 0x84;
2835 break;
2836 case 17: /* ST(5) */
2837 i = 0x85;
2838 break;
2839 case 18: /* ST(6) */
2840 i = 0x86;
2841 break;
2842 case 19: /* ST(7) */
2843 i = 0x87;
2844 break;
2845 default:
2846 warning ("unknown register %lu", symbol->value);
2847 return;
2848 }
2849 sst_start (SST_reg); /* Register variable */
2850 buffer_word (&sst, ti); /* Type index */
2851 buffer_byte (&sst, i); /* Register number */
2852 buffer_nstr (&sst, name); /* Symbol name */
2853 sst_end ();
2854 break;
2855
2856 case '0': case '1': case '2': case '3': case '4':
2857 case '5': case '6': case '7': case '8': case '9':
2858
2859 /* Local symbol. */
2860
2861 if (where != -1)
2862 {
2863 warning ("Cannot use local symbol with N_%s", msg);
2864 return;
2865 }
2866 ti = hll_type ();
2867#if defined (DEBUG)
2868 printf (" type=%#x\n", ti);
2869#endif
2870 sst_start (SST_auto);
2871 buffer_dword (&sst, symbol->value); /* Offset into stk frame */
2872 buffer_word (&sst, ti); /* Type index */
2873 buffer_nstr (&sst, name); /* Symbol name */
2874 sst_end ();
2875 break;
2876
2877 case 'F':
2878 case 'f':
2879
2880 /* Function. */
2881
2882 ++parse_ptr;
2883 if (where != N_TEXT)
2884 {
2885 warning ("Cannot use function with N_%s", msg);
2886 return;
2887 }
2888 last_fun_type.tag = ty_func;
2889 last_fun_type.index = -1;
2890 last_fun_type.d.func.ret = parse_type (NULL);
2891
2892 /* Don't check for end of string -- parse_ptr points to a
2893 comma for nested functions! */
2894
2895 last_fun_type.d.func.args = NULL;
2896 last_fun_type.d.func.arg_count = 0;
2897 last_fun_valid = TRUE;
2898 args_grow.count = 0;
2899 last_fun_addr = symbol->value;
2900 prologue_length = -1;
2901
2902 for (i = *index + 1; i < sym_count && sym_ptr[i].type == N_PSYM; ++i)
2903 parse_symbol (&i, -1, "PSYM", FALSE);
2904
2905 define_fun (symbol);
2906
2907 /* Now look for N_LBRAC */
2908
2909 next_lbrac (i);
2910 if (lbrac_index != -1)
2911 {
2912 if (prologue_length == -1)
2913 prologue_length = sym_ptr[lbrac_index].value - last_fun_addr;
2914 }
2915
2916 /* Parameters */
2917
2918 while (*index + 1 < sym_count && sym_ptr[*index+1].type == N_PSYM)
2919 {
2920 ++(*index);
2921 parse_symbol (index, -1, "PSYM", TRUE);
2922 }
2923
2924 /* If there's no N_LBRAC, write SST_end now */
2925
2926 if (lbrac_index == -1)
2927 fun_end (*index+1);
2928 break;
2929 }
2930 }
2931 if (alloc_flag)
2932 free ((void *)str); /* Remove the const attribute */
2933}
2934
2935
2936/* Create a type tag for HLL type index INDEX. TYPE is either SST_tag
2937 (for structures and unions) or SST_class (for classes). NAME is
2938 the name of the structure, union or class. */
2939
2940static void type_tag (int type, int index, const char *name)
2941{
2942 if (name != NULL)
2943 {
2944 sst_start (type);
2945 buffer_word (&sst, index);
2946 if (type == SST_class)
2947 buffer_enc (&sst, name);
2948 else
2949 buffer_nstr (&sst, name);
2950 sst_end ();
2951 }
2952}
2953
2954
2955/* Write a CuInfo SST entry to tell the debugger what compiler (C or
2956 C++) has been used. */
2957
2958static void write_cuinfo (void)
2959{
2960 struct timestamp ts;
2961 time_t now;
2962 struct tm *tp;
2963 byte lang;
2964
2965 if (cplusplus_flag)
2966 lang = 0x02; /* C++ */
2967 else
2968 lang = 0x01; /* C */
2969
2970 time (&now);
2971 tp = localtime (&now);
2972 ts.year = tp->tm_year + 1900;
2973 ts.month = tp->tm_mon + 1;
2974 ts.day = tp->tm_mday;
2975 ts.hours = tp->tm_hour;
2976 ts.minutes = tp->tm_min;
2977 ts.seconds = tp->tm_sec;
2978 ts.hundredths = 0;
2979 sst_start (SST_cuinfo);
2980 buffer_byte (&sst, lang);
2981 buffer_nstr (&sst, " "); /* Compiler options */
2982 buffer_nstr (&sst, ""); /* Compiler date */
2983 buffer_mem (&sst, &ts, sizeof (ts));
2984 sst_end ();
2985}
2986
2987
2988/* Write a ChangSeg SST entry to define the text segment. */
2989
2990static void write_changseg (void)
2991{
2992 struct reloc r;
2993
2994 sst_start (SST_changseg);
2995 r.address = sst.size;
2996 buffer_word (&sst, 0); /* Segment number */
2997 buffer_word (&sst, 0); /* Reserved */
2998 sst_end ();
2999 r.ext = 0;
3000 r.length = 1;
3001 r.symbolnum = N_TEXT;
3002 r.pcrel = 0;
3003 r.unused = 0;
3004 buffer_mem (&sst_reloc, &r, sizeof (r));
3005}
3006
3007
3008/* Convert debug information. The data is taken from the symbol and
3009 string tables of the current a.out module. Output is put into the
3010 tt, sst etc. buffers. */
3011
3012void convert_debug (void)
3013{
3014 int i;
3015 struct type *t1, *t2;
3016
3017 str_pool = strpool_init ();
3018 grow_init (&stype_grow, &stype_list, sizeof (*stype_list), 32);
3019 type_head = NULL; hll_type_index = 512;
3020 grow_init (&block_grow, &block_stack, sizeof (*block_stack), 16);
3021 grow_init (&args_grow, &args_list, sizeof (*args_list), 16);
3022 last_fun_valid = FALSE; prologue_length = -1;
3023 unnamed_struct_number = 1;
3024
3025 /* Check whether converting a translated C++ program. */
3026
3027 cplusplus_flag = (find_symbol ("__gnu_compiled_cplusplus") != NULL);
3028
3029 /* Parse the typedefs to avoid forward references. */
3030
3031 for (i = 0; i < sym_count; ++i)
3032 switch (sym_ptr[i].type)
3033 {
3034 case N_LSYM:
3035 case N_LCSYM:
3036 case N_GSYM:
3037 case N_PSYM:
3038 case N_RSYM:
3039 case N_STSYM:
3040 case N_FUN:
3041 parse_typedef (&i);
3042 break;
3043 }
3044
3045 /* Provide information about the compiler. */
3046
3047 write_cuinfo ();
3048
3049 /* Change the default segment. */
3050
3051 write_changseg ();
3052
3053 /* Parse the other symbol table entries. */
3054
3055 lbrac_index = -1; block_grow.count = 0; proc_patch_base = 0;
3056 for (i = 0; i < sym_count; ++i)
3057 switch (sym_ptr[i].type)
3058 {
3059 case N_LSYM:
3060 parse_symbol (&i, -1, "LSYM", TRUE);
3061 break;
3062
3063 case N_GSYM:
3064 parse_symbol (&i, -1, "GSYM", TRUE);
3065 break;
3066
3067 case N_LCSYM:
3068 parse_symbol (&i, N_BSS, "LCSYM", TRUE);
3069 break;
3070
3071 case N_STSYM:
3072 parse_symbol (&i, N_DATA, "STSYM", TRUE);
3073 break;
3074
3075 case N_RSYM:
3076 parse_symbol (&i, -1, "RSYM", TRUE);
3077 break;
3078
3079 case N_LBRAC:
3080 if (lbrac_index == -1) /* N_LBRAC without local symbols */
3081 block_begin (sym_ptr[i].value);
3082 else if (i != lbrac_index)
3083 warning ("Invalid N_LBRAC");
3084 next_lbrac (i + 1);
3085 break;
3086
3087 case N_RBRAC:
3088 block_end (i);
3089 next_lbrac (i + 1);
3090 break;
3091
3092 case N_FUN:
3093 parse_symbol (&i, N_TEXT, "FUN", TRUE);
3094 break;
3095 }
3096
3097 /* Create tags for structures, unions and classes. */
3098
3099 for (t1 = type_head; t1 != NULL; t1 = t1->next)
3100 if (t1->index != -1)
3101 switch (t1->tag)
3102 {
3103 case ty_struc:
3104 type_tag (SST_tag, t1->index, t1->d.struc.name);
3105 break;
3106 case ty_class:
3107 type_tag (SST_class, t1->index, t1->d.class.name);
3108 break;
3109 default:
3110 break;
3111 }
3112
3113 /* Deallocate memory. */
3114
3115 grow_free (&stype_grow);
3116 grow_free (&args_grow);
3117 grow_free (&block_grow);
3118 for (t1 = type_head; t1 != NULL; t1 = t2)
3119 {
3120 t2 = t1->next;
3121 switch (t1->tag)
3122 {
3123 case ty_args:
3124 if (t1->d.args.list != NULL)
3125 free (t1->d.args.list);
3126 break;
3127 case ty_types:
3128 if (t1->d.types.list != NULL)
3129 free (t1->d.types.list);
3130 break;
3131 case ty_fields:
3132 if (t1->d.fields.list != NULL)
3133 free (t1->d.fields.list);
3134 break;
3135 case ty_values:
3136 if (t1->d.values.list != NULL)
3137 free (t1->d.values.list);
3138 break;
3139 default:
3140 break;
3141 }
3142 free (t1);
3143 }
3144 type_head = NULL;
3145 strpool_free (str_pool);
3146 str_pool = NULL;
3147}
Note: See TracBrowser for help on using the repository browser.