winedump: Handling of S_SALIGN symbols.
[wine] / tools / winedump / msc.c
1 /*
2  *      MS debug info dumping utility
3  *
4  *      Copyright 2006 Eric Pouech
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #ifdef HAVE_UNISTD_H
28 # include <unistd.h>
29 #endif
30 #include <time.h>
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34 #ifdef HAVE_SYS_STAT_H
35 # include <sys/stat.h>
36 #endif
37 #ifdef HAVE_SYS_MMAN_H
38 #include <sys/mman.h>
39 #endif
40 #include <fcntl.h>
41
42 #define NONAMELESSUNION
43 #define NONAMELESSSTRUCT
44 #include "windef.h"
45 #include "winbase.h"
46 #include "winedump.h"
47 #include "wine/mscvpdb.h"
48
49 #define PSTRING(adr, ofs) \
50     ((const struct p_string*)((const char*)(adr) + (ofs)))
51
52 static const char* p_string(const struct p_string* s)
53 {
54     static char tmp[256 + 1];
55     memcpy(tmp, s->name, s->namelen);
56     tmp[s->namelen] = '\0';
57     return tmp;
58 }
59
60 static int numeric_leaf(int* value, const unsigned short int* leaf)
61 {
62     unsigned short int type = *leaf++;
63     int length = 2;
64
65     if (type < LF_NUMERIC)
66     {
67         *value = type;
68     }
69     else
70     {
71         switch (type)
72         {
73         case LF_CHAR:
74             length += 1;
75             *value = *(const char*)leaf;
76             break;
77
78         case LF_SHORT:
79             length += 2;
80             *value = *(const short*)leaf;
81             break;
82
83         case LF_USHORT:
84             length += 2;
85             *value = *(const unsigned short*)leaf;
86             break;
87
88         case LF_LONG:
89             length += 4;
90             *value = *(const int*)leaf;
91             break;
92
93         case LF_ULONG:
94             length += 4;
95             *value = *(const unsigned int*)leaf;
96             break;
97
98         case LF_QUADWORD:
99         case LF_UQUADWORD:
100             length += 8;
101             printf(">>> unsupported leaf value\n");
102             *value = 0;    /* FIXME */
103             break;
104
105         case LF_REAL32:
106             length += 4;
107             printf(">>> unsupported leaf value\n");
108             *value = 0;    /* FIXME */
109             break;
110
111         case LF_REAL48:
112             length += 6;
113             *value = 0;    /* FIXME */
114             printf(">>> unsupported leaf value\n");
115             break;
116
117         case LF_REAL64:
118             length += 8;
119             *value = 0;    /* FIXME */
120             printf(">>> unsupported leaf value\n");
121             break;
122
123         case LF_REAL80:
124             length += 10;
125             *value = 0;    /* FIXME */
126             printf(">>> unsupported leaf value\n");
127             break;
128
129         case LF_REAL128:
130             length += 16;
131             *value = 0;    /* FIXME */
132             printf(">>> unsupported leaf value\n");
133             break;
134
135         case LF_COMPLEX32:
136             length += 4;
137             *value = 0;    /* FIXME */
138             printf(">>> unsupported leaf value\n");
139             break;
140
141         case LF_COMPLEX64:
142             length += 8;
143             *value = 0;    /* FIXME */
144             printf(">>> unsupported leaf value\n");
145             break;
146
147         case LF_COMPLEX80:
148             length += 10;
149             *value = 0;    /* FIXME */
150             printf(">>> unsupported leaf value\n");
151             break;
152
153         case LF_COMPLEX128:
154             length += 16;
155             *value = 0;    /* FIXME */
156             printf(">>> unsupported leaf value\n");
157             break;
158
159         case LF_VARSTRING:
160             length += 2 + *leaf;
161             *value = 0;    /* FIXME */
162             printf(">>> unsupported leaf value\n");
163             break;
164
165         default:
166             printf(">>> Unsupported numeric leaf-id %04x\n", type);
167             *value = 0;
168             break;
169         }
170     }
171     return length;
172 }
173
174 static const char* get_attr(unsigned attr)
175 {
176     static char tmp[256];
177
178     switch (attr & 3)
179     {
180     case 0: strcpy(tmp, ""); break;
181     case 1: strcpy(tmp, "private "); break;
182     case 2: strcpy(tmp, "protected "); break;
183     case 3: strcpy(tmp, "public "); break;
184     }
185     switch ((attr >> 2) & 7)
186     {
187     case 0: strcat(tmp, ""); break;
188     case 1: strcat(tmp, "virtual "); break;
189     case 2: strcat(tmp, "static "); break;
190     case 3: strcat(tmp, "friend "); break;
191     case 4: strcat(tmp, "introducing virtual "); break;
192     case 5: strcat(tmp, "pure virtual "); break;
193     case 6: strcat(tmp, "pure introducing virtual "); break;
194     case 7: strcat(tmp, "reserved "); break;
195     }
196     if ((attr >> 5) & 1) strcat(tmp, "pseudo ");
197     if ((attr >> 6) & 1) strcat(tmp, "no-inherit ");
198     if ((attr >> 7) & 1) strcat(tmp, "no-construct ");
199     return tmp;
200 }
201
202 static void do_field(const unsigned char* start, const unsigned char* end)
203 {
204     /*
205      * A 'field list' is a CodeView-specific data type which doesn't
206      * directly correspond to any high-level data type.  It is used
207      * to hold the collection of members of a struct, class, union
208      * or enum type.  The actual definition of that type will follow
209      * later, and refer to the field list definition record.
210      *
211      * As we don't have a field list type ourselves, we look ahead
212      * in the field list to try to find out whether this field list
213      * will be used for an enum or struct type, and create a dummy
214      * type of the corresponding sort.  Later on, the definition of
215      * the 'real' type will copy the member / enumeration data.
216      */
217     const unsigned char*        ptr = start;
218     const char*                 cstr;
219     const struct p_string*      pstr;
220     int leaf_len, value;
221
222     while (ptr < end)
223     {
224         const union codeview_fieldtype* fieldtype = (const union codeview_fieldtype*)ptr;
225
226         if (*ptr >= 0xf0)       /* LF_PAD... */
227         {
228             ptr +=* ptr & 0x0f;
229             continue;
230         }
231
232         switch (fieldtype->generic.id)
233         {
234         case LF_ENUMERATE_V1:
235             leaf_len = numeric_leaf(&value, &fieldtype->enumerate_v1.value);
236             pstr = PSTRING(&fieldtype->enumerate_v1.value, leaf_len);
237             printf("\t\tEnumerate V1: '%s' value:%d\n",
238                    p_string(pstr), value);
239             ptr += 2 + 2 + leaf_len + 1 + pstr->namelen;
240             break;
241
242         case LF_ENUMERATE_V3:
243             leaf_len = numeric_leaf(&value, &fieldtype->enumerate_v3.value);
244             cstr = (const char*)&fieldtype->enumerate_v3.value + leaf_len;
245             printf("\t\tEnumerate V3: '%s' value:%d\n",
246                    cstr, value);
247             ptr += 2 + 2 + leaf_len + strlen(cstr) + 1;
248             break;
249
250         case LF_MEMBER_V1:
251             leaf_len = numeric_leaf(&value, &fieldtype->member_v1.offset);
252             pstr = PSTRING(&fieldtype->member_v1.offset, leaf_len);
253             printf("\t\tMember V1: '%s' type:%x attr:%s @%d\n",
254                    p_string(pstr), fieldtype->member_v1.type,
255                    get_attr(fieldtype->member_v1.attribute), value);
256             ptr += 2 + 2 + 2 + leaf_len + 1 + pstr->namelen;
257             break;
258
259         case LF_MEMBER_V2:
260             leaf_len = numeric_leaf(&value, &fieldtype->member_v2.offset);
261             pstr = PSTRING(&fieldtype->member_v2.offset, leaf_len);
262             printf("\t\tMember V2: '%s' type:%x attr:%s @%d\n",
263                    p_string(pstr), fieldtype->member_v2.type,
264                    get_attr(fieldtype->member_v2.attribute), value);
265             ptr += 2 + 2 + 4 + leaf_len + 1 + pstr->namelen;
266             break;
267
268         case LF_MEMBER_V3:
269             leaf_len = numeric_leaf(&value, &fieldtype->member_v3.offset);
270             cstr = (const char*)&fieldtype->member_v3.offset + leaf_len;
271             printf("\t\tMember V3: '%s' type:%x attr:%s @%d\n",
272                    cstr, fieldtype->member_v3.type, 
273                    get_attr(fieldtype->member_v3.attribute), value);
274             ptr += 2 + 2 + 4 + leaf_len + strlen(cstr) + 1;
275             break;
276
277         case LF_ONEMETHOD_V1:
278             switch ((fieldtype->onemethod_v1.attribute >> 2) & 7)
279             {
280             case 4: case 6:
281                 printf("\t\tVirtual-method V1: '%s' attr:%s type:%x vtable_offset:%u\n",
282                        p_string(&fieldtype->onemethod_virt_v1.p_name),
283                        get_attr(fieldtype->onemethod_virt_v1.attribute),
284                        fieldtype->onemethod_virt_v1.type,
285                        fieldtype->onemethod_virt_v1.vtab_offset);
286                 ptr += 2 + 2 + 2 + 4 + (1 + fieldtype->onemethod_virt_v1.p_name.namelen);
287                 break;
288
289             default:
290                 printf("\t\tMethod V1: '%s' attr:%s type:%x\n",
291                        p_string(&fieldtype->onemethod_v1.p_name),
292                        get_attr(fieldtype->onemethod_v1.attribute),
293                        fieldtype->onemethod_v1.type);
294                 ptr += 2 + 2 + 2 + (1 + fieldtype->onemethod_v1.p_name.namelen);
295                 break;
296             }
297             break;
298
299         case LF_ONEMETHOD_V2:
300             switch ((fieldtype->onemethod_v2.attribute >> 2) & 7)
301             {
302             case 4: case 6:
303                 printf("\t\tVirtual-method V2: '%s' attr:%s type:%x vtable_offset:%u\n",
304                        p_string(&fieldtype->onemethod_virt_v2.p_name),
305                        get_attr(fieldtype->onemethod_virt_v2.attribute),
306                        fieldtype->onemethod_virt_v2.type,
307                        fieldtype->onemethod_virt_v2.vtab_offset);
308                 ptr += 2 + 2 + 4 + 4 + (1 + fieldtype->onemethod_virt_v2.p_name.namelen);
309                 break;
310
311             default:
312                 printf("\t\tMethod V2: '%s' attr:%s type:%x\n",
313                        p_string(&fieldtype->onemethod_v2.p_name),
314                        get_attr(fieldtype->onemethod_v2.attribute),
315                        fieldtype->onemethod_v2.type);
316                 ptr += 2 + 2 + 4 + (1 + fieldtype->onemethod_v2.p_name.namelen);
317                 break;
318             }
319             break;
320
321         case LF_ONEMETHOD_V3:
322             switch ((fieldtype->onemethod_v3.attribute >> 2) & 7)
323             {
324             case 4: case 6:
325                 printf("\t\tVirtual-method V3: '%s' attr:%s type:%x vtable_offset:%u\n",
326                        fieldtype->onemethod_virt_v3.name,
327                        get_attr(fieldtype->onemethod_virt_v3.attribute),
328                        fieldtype->onemethod_virt_v3.type,
329                        fieldtype->onemethod_virt_v3.vtab_offset);
330                 ptr += 2 + 2 + 4 + 4 + (strlen(fieldtype->onemethod_virt_v3.name) + 1);
331                 break;
332
333             default:
334                 printf("\t\tMethod V3: '%s' attr:%s type:%x\n",
335                        fieldtype->onemethod_v3.name,
336                        get_attr(fieldtype->onemethod_v3.attribute),
337                        fieldtype->onemethod_v3.type);
338                 ptr += 2 + 2 + 4 + (strlen(fieldtype->onemethod_v3.name) + 1);
339                 break;
340             }
341             break;
342
343         case LF_METHOD_V1:
344             printf("\t\tMethod V1: '%s' overloaded=#%d method-list=%x\n",
345                    p_string(&fieldtype->method_v1.p_name),
346                    fieldtype->method_v1.count, fieldtype->method_v1.mlist);
347             ptr += 2 + 2 + 2 + (1 + fieldtype->method_v1.p_name.namelen);
348             break;
349
350         case LF_METHOD_V2:
351             printf("\t\tMethod V2: '%s' overloaded=#%d method-list=%x\n",
352                    p_string(&fieldtype->method_v2.p_name),
353                    fieldtype->method_v2.count, fieldtype->method_v2.mlist);
354             ptr += 2 + 2 + 4 + (1 + fieldtype->method_v2.p_name.namelen);
355             break;
356
357         case LF_METHOD_V3:
358             printf("\t\tMethod V3: '%s' overloaded=#%d method-list=%x\n",
359                    fieldtype->method_v3.name,
360                    fieldtype->method_v3.count, fieldtype->method_v3.mlist);
361             ptr += 2 + 2 + 4 + (strlen(fieldtype->method_v3.name) + 1);
362             break;
363
364         case LF_STMEMBER_V1:
365             printf("\t\tStatic member V1: '%s' attr:%s type:%x\n",
366                    p_string(&fieldtype->stmember_v1.p_name),
367                    get_attr(fieldtype->stmember_v1.attribute),
368                    fieldtype->stmember_v1.type);
369             ptr += 2 + 2 + 2 + (1 + fieldtype->stmember_v1.p_name.namelen);
370             break;
371
372         case LF_STMEMBER_V2:
373             printf("\t\tStatic member V2: '%s' attr:%s type:%x\n",
374                    p_string(&fieldtype->stmember_v2.p_name),
375                    get_attr(fieldtype->stmember_v2.attribute),
376                    fieldtype->stmember_v2.type);
377             ptr += 2 + 2 + 4 + (1 + fieldtype->stmember_v2.p_name.namelen);
378             break;
379
380         case LF_STMEMBER_V3:
381             printf("\t\tStatic member V3: '%s' attr:%s type:%x\n",
382                    fieldtype->stmember_v3.name,
383                    get_attr(fieldtype->stmember_v3.attribute),
384                    fieldtype->stmember_v3.type);
385             ptr += 2 + 2 + 4 + (strlen(fieldtype->stmember_v3.name) + 1);
386             break;
387
388         case LF_FRIENDFCN_V1:
389             printf("\t\tFriend function V1: '%s' type:%x\n",
390                    p_string(&fieldtype->friendfcn_v1.p_name),
391                    fieldtype->friendfcn_v1.type);
392             break;
393
394         case LF_FRIENDFCN_V2:
395             printf("\t\tFriend function V2: '%s' type:%x\n",
396                    p_string(&fieldtype->friendfcn_v2.p_name),
397                    fieldtype->friendfcn_v2.type);
398             break;
399
400 #if 0
401         case LF_FRIENDFCN_V3:
402             printf("\t\tFriend function V3: '%s' type:%x\n",
403                    fieldtype->friendfcn_v3.name,
404                    fieldtype->friendfcn_v3.type);
405             break;
406 #endif
407
408         case LF_BCLASS_V1:
409             leaf_len = numeric_leaf(&value, &fieldtype->bclass_v1.offset);
410             printf("\t\tBase class V1: type:%x attr:%s @%d\n",
411                    fieldtype->bclass_v1.type, 
412                    get_attr(fieldtype->bclass_v1.attribute), value);
413             ptr += 2 + 2 + 2 + leaf_len;
414             break;
415
416         case LF_BCLASS_V2:
417             leaf_len = numeric_leaf(&value, &fieldtype->bclass_v2.offset);
418             printf("\t\tBase class V2: type:%x attr:%s @%d\n",
419                    fieldtype->bclass_v2.type, 
420                    get_attr(fieldtype->bclass_v2.attribute), value);
421             ptr += 2 + 2 + 4 + leaf_len;
422             break;
423
424         case LF_VBCLASS_V1:
425         case LF_IVBCLASS_V1:
426             leaf_len = numeric_leaf(&value, &fieldtype->vbclass_v1.vbpoff);
427             printf("\t\t%sirtual base class V1: type:%x (ptr:%x) attr:%s vbpoff:%d ",
428                    (fieldtype->generic.id == LF_VBCLASS_V2) ? "V" : "Indirect v",
429                    fieldtype->vbclass_v1.btype, fieldtype->vbclass_v1.vbtype,
430                    get_attr(fieldtype->vbclass_v1.attribute), value);
431             ptr += 2 + 2 + 2 + 2 + leaf_len;
432             leaf_len = numeric_leaf(&value, (const unsigned short*)ptr);
433             printf("vboff:%d\n", value);
434             ptr += leaf_len;
435             break;
436
437         case LF_VBCLASS_V2:
438         case LF_IVBCLASS_V2:
439             leaf_len = numeric_leaf(&value, &fieldtype->vbclass_v1.vbpoff);
440             printf("\t\t%sirtual base class V2: type:%x (ptr:%x) attr:%s vbpoff:%d ",
441                    (fieldtype->generic.id == LF_VBCLASS_V2) ? "V" : "Indirect v",
442                    fieldtype->vbclass_v2.btype, fieldtype->vbclass_v2.vbtype,
443                    get_attr(fieldtype->vbclass_v2.attribute), value);
444             ptr += 2 + 2 + 4 + 4 + leaf_len;
445             leaf_len = numeric_leaf(&value, (const unsigned short*)ptr);
446             printf("vboff:%d\n", value);
447             ptr += leaf_len;
448             break;
449
450         case LF_FRIENDCLS_V1:
451             printf("\t\tFriend class V1: type:%x\n", fieldtype->friendcls_v1.type);
452             break;
453
454         case LF_FRIENDCLS_V2:
455             printf("\t\tFriend class V2: type:%x\n", fieldtype->friendcls_v2.type);
456             break;
457
458         case LF_NESTTYPE_V1:
459             printf("\t\tNested type V1: '%s' type:%x\n",
460                    p_string(&fieldtype->nesttype_v1.p_name),
461                    fieldtype->nesttype_v1.type);
462             ptr += 2 + 2 + (1 + fieldtype->nesttype_v1.p_name.namelen);
463             break;
464
465         case LF_NESTTYPE_V2:
466             printf("\t\tNested type V2: '%s' pad0:%u type:%x\n",
467                    p_string(&fieldtype->nesttype_v2.p_name),
468                    fieldtype->nesttype_v2._pad0, fieldtype->nesttype_v2.type);
469             ptr += 2 + 2 + 4 + (1 + fieldtype->nesttype_v2.p_name.namelen);
470             break;
471
472         case LF_NESTTYPE_V3:
473             printf("\t\tNested type V3: '%s' pad0:%u type:%x\n",
474                    fieldtype->nesttype_v3.name,
475                    fieldtype->nesttype_v3._pad0, fieldtype->nesttype_v3.type);
476             ptr += 2 + 2 + 4 + (strlen(fieldtype->nesttype_v3.name) + 1);
477             break;
478
479         case LF_VFUNCTAB_V1:
480             printf("\t\tVirtual function table V1: type:%x\n",
481                    fieldtype->vfunctab_v1.type);
482             ptr += 2 + 2;
483             break;
484
485         case LF_VFUNCTAB_V2:
486             printf("\t\tVirtual function table V2: type:%x\n",
487                    fieldtype->vfunctab_v2.type);
488             ptr += 2 + 2 + 4;
489             break;
490
491         case LF_VFUNCOFF_V1:
492             printf("\t\tVirtual function table offset V1: type:%x offset:%x\n",
493                    fieldtype->vfuncoff_v1.type, fieldtype->vfuncoff_v1.offset);
494             break;
495
496         case LF_VFUNCOFF_V2:
497             printf("\t\tVirtual function table offset V2: type:%x offset:%x\n",
498                    fieldtype->vfuncoff_v2.type, fieldtype->vfuncoff_v2.offset);
499             break;
500
501         default:
502             printf(">>> Unsupported field-id %x\n", fieldtype->generic.id);
503             dump_data((const void*)fieldtype, 0x30, "\t");
504             return;
505             break;
506         }
507     }
508 }
509
510 int codeview_dump_types(const void* table, unsigned long len)
511 {
512     unsigned int        curr_type = 0x1000;
513     const unsigned char*ptr = table;
514     int                 i, leaf_len, value;
515     const char*         str;
516
517     while (ptr - (const unsigned char*)table < len)
518     {
519         const union codeview_type* type = (const union codeview_type*)ptr;
520         const union codeview_reftype* reftype = (const union codeview_reftype*)ptr;
521         int retv = TRUE;
522
523         switch (type->generic.id)
524         {
525         case LF_POINTER_V1:
526             printf("\t%x => Pointer V1 to type:%x\n",
527                    curr_type, type->pointer_v1.datatype);
528             break;
529         case LF_POINTER_V2:
530             printf("\t%x => Pointer V2 to type:%x\n",
531                    curr_type, type->pointer_v2.datatype);
532             break;
533         case LF_ARRAY_V1:
534             leaf_len = numeric_leaf(&value, &type->array_v1.arrlen);
535             printf("\t%x => Array V1-'%s'[%u type:%x] type:%x\n",
536                    curr_type, p_string(PSTRING(&type->array_v1.arrlen, leaf_len)),
537                    value, type->array_v1.idxtype, type->array_v1.elemtype);
538             break;
539         case LF_ARRAY_V2:
540             leaf_len = numeric_leaf(&value, &type->array_v2.arrlen);
541             printf("\t%x => Array V2-'%s'[%u type:%x] type:%x\n",
542                    curr_type, p_string(PSTRING(&type->array_v2.arrlen, leaf_len)),
543                    value, type->array_v2.idxtype, type->array_v2.elemtype);
544             break;
545         case LF_ARRAY_V3:
546             leaf_len = numeric_leaf(&value, &type->array_v3.arrlen);
547             str = (const char*)&type->array_v3.arrlen + leaf_len;
548             printf("\t%x => Array V3-'%s'[%u type:%x] type:%x\n",
549                    curr_type, str, value, 
550                    type->array_v3.idxtype, type->array_v3.elemtype);
551             break;
552
553
554         /* a bitfields is a CodeView specific data type which represent a bitfield
555          * in a structure or a class. For now, we store it in a SymTag-like type
556          * (so that the rest of the process is seamless), but check at udt inclusion
557          * type for its presence
558          */
559         case LF_BITFIELD_V1:
560             printf("\t%x => Bitfield V1:%x offset:%u #bits:%u\n",
561                    curr_type, reftype->bitfield_v1.type, reftype->bitfield_v1.bitoff,
562                    reftype->bitfield_v1.nbits);
563             break;
564
565         case LF_BITFIELD_V2:
566             printf("\t%x => Bitfield V2:%x offset:%u #bits:%u\n",
567                    curr_type, reftype->bitfield_v2.type, reftype->bitfield_v2.bitoff,
568                    reftype->bitfield_v2.nbits);
569             break;
570
571         case LF_FIELDLIST_V1:
572         case LF_FIELDLIST_V2:
573             printf("\t%x => Fieldlist\n", curr_type);
574             do_field(reftype->fieldlist.list, ptr + reftype->generic.len + 2);
575             break;
576
577         case LF_STRUCTURE_V1:
578         case LF_CLASS_V1:
579             leaf_len = numeric_leaf(&value, &type->struct_v1.structlen);
580             printf("\t%x => %s V1 '%s' elts:%u prop:%u fieldlist-type:%x derived-type:%x vshape:%x size:%u\n",
581                    curr_type, type->generic.id == LF_CLASS_V1 ? "Class" : "Struct",
582                    p_string(PSTRING(&type->struct_v1.structlen, leaf_len)),
583                    type->struct_v1.n_element, type->struct_v1.property,
584                    type->struct_v1.fieldlist, type->struct_v1.derived, 
585                    type->struct_v1.vshape, value);
586             break;
587
588         case LF_STRUCTURE_V2:
589         case LF_CLASS_V2:
590             leaf_len = numeric_leaf(&value, &type->struct_v2.structlen);
591             printf("\t%x => %s V2 '%s' elts:%u prop:%u\n"
592                    "                fieldlist-type:%x derived-type:%x vshape:%x size:%u\n",
593                    curr_type, type->generic.id == LF_CLASS_V2 ? "Class" : "Struct",
594                    p_string(PSTRING(&type->struct_v2.structlen, leaf_len)),
595                    type->struct_v2.n_element, type->struct_v2.property,
596                    type->struct_v2.fieldlist, type->struct_v2.derived, 
597                    type->struct_v2.vshape, value);
598             break;
599
600         case LF_STRUCTURE_V3:
601         case LF_CLASS_V3:
602             leaf_len = numeric_leaf(&value, &type->struct_v3.structlen);
603             str = (const char*)&type->struct_v3.structlen + leaf_len;
604             printf("\t%x => %s V3 '%s' elts:%u prop:%u\n"
605                    "                fieldlist-type:%x derived-type:%x vshape:%x size:%u\n",
606                    curr_type, type->generic.id == LF_CLASS_V3 ? "Class" : "Struct",
607                    str, type->struct_v3.n_element, type->struct_v3.property,
608                    type->struct_v3.fieldlist, type->struct_v3.derived, 
609                    type->struct_v3.vshape, value);
610             break;
611
612         case LF_UNION_V1:
613             leaf_len = numeric_leaf(&value, &type->union_v1.un_len);
614             printf("\t%x => Union V1 '%s' count:%u prop:%u fieldlist-type:%x size:%u\n",
615                    curr_type, p_string(PSTRING(&type->union_v1.un_len, leaf_len)),
616                    type->union_v1.count, type->union_v1.property,
617                    type->union_v1.fieldlist, value);
618             break;
619
620         case LF_UNION_V2:
621             leaf_len = numeric_leaf(&value, &type->union_v2.un_len);
622             printf("\t%x => Union V2 '%s' count:%u prop:%u fieldlist-type:%x size:%u\n",
623                    curr_type, p_string(PSTRING(&type->union_v2.un_len, leaf_len)),
624                    type->union_v2.count, type->union_v2.property,
625                    type->union_v2.fieldlist, value);
626             break;
627
628         case LF_UNION_V3:
629             leaf_len = numeric_leaf(&value, &type->union_v3.un_len);
630             str = (const char*)&type->union_v3.un_len + leaf_len;
631             printf("\t%x => Union V3 '%s' count:%u prop:%u fieldlist-type:%x size:%u\n",
632                    curr_type, str, type->union_v3.count, 
633                    type->union_v3.property, type->union_v3.fieldlist, value);
634             break;
635
636         case LF_ENUM_V1:
637             printf("\t%x => Enum V1 '%s' type:%x field-type:%x count:%u property:%x\n",
638                    curr_type, p_string(&type->enumeration_v1.p_name),
639                    type->enumeration_v1.type,
640                    type->enumeration_v1.fieldlist,
641                    type->enumeration_v1.count,
642                    type->enumeration_v1.property);
643             break;
644
645         case LF_ENUM_V2:
646             printf("\t%x => Enum V2 '%s' type:%x field-type:%x count:%u property:%x\n",
647                    curr_type, p_string(&type->enumeration_v2.p_name),
648                    type->enumeration_v2.type,
649                    type->enumeration_v2.fieldlist,
650                    type->enumeration_v2.count,
651                    type->enumeration_v2.property);
652             break;
653
654         case LF_ENUM_V3:
655             printf("\t%x => Enum V3 '%s' type:%x field-type:%x count:%u property:%x\n",
656                    curr_type, type->enumeration_v3.name, 
657                    type->enumeration_v3.type,
658                    type->enumeration_v3.fieldlist,
659                    type->enumeration_v3.count,
660                    type->enumeration_v3.property);
661             break;
662
663         case LF_ARGLIST_V1:
664             printf("\t%x => Arglist V1(#%u):", curr_type, reftype->arglist_v1.num);
665             for (i = 0; i < reftype->arglist_v1.num; i++)
666             {
667                 printf(" %x", reftype->arglist_v1.args[i]);
668             }
669             printf("\n");
670             break;
671
672         case LF_ARGLIST_V2:
673             printf("\t%x => Arglist V2(#%u):", curr_type, reftype->arglist_v2.num);
674             for (i = 0; i < reftype->arglist_v2.num; i++)
675             {
676                 printf("\t %x", reftype->arglist_v2.args[i]);
677             }
678             printf("\t\n");
679             break;
680
681         case LF_PROCEDURE_V1:
682             /* FIXME: unknown could be the calling convention for the proc */
683             printf("\t%x => Procedure V1 ret_type:%x call:%x (#%u args_type:%x)\n",
684                    curr_type, type->procedure_v1.rvtype,
685                    type->procedure_v1.call, type->procedure_v1.params,
686                    type->procedure_v1.arglist);
687             break;
688         case LF_PROCEDURE_V2:
689             printf("\t%x => Procedure V2 ret_type:%x unk:%x (#%u args_type:%x)\n",
690                    curr_type, type->procedure_v2.rvtype,
691                    type->procedure_v2.call, type->procedure_v2.params,
692                    type->procedure_v2.arglist);
693             break;
694
695         case LF_MFUNCTION_V2:
696             printf("\t%x => MFunction V2 ret-type:%x call:%x class-type:%x this-type:%x\n"
697                    "\t\t#args:%x args-type:%x this_adjust:%x\n",
698                    curr_type,
699                    type->mfunction_v2.rvtype,
700                    type->mfunction_v2.call,
701                    type->mfunction_v2.class_type,
702                    type->mfunction_v2.this_type,
703                    type->mfunction_v2.params,
704                    type->mfunction_v2.arglist, 
705                    type->mfunction_v2.this_adjust);
706             break;
707
708         case LF_MODIFIER_V1:
709             printf("\t%x => Modifier V1 type:%x modif:%x\n",
710                    curr_type, type->modifier_v1.type, type->modifier_v1.attribute);
711             break;
712         case LF_MODIFIER_V2:
713             printf("\t%x => Modifier V2 type:%x modif:%x\n",
714                    curr_type, type->modifier_v2.type, type->modifier_v2.attribute);  
715             break;
716
717         case LF_METHODLIST_V1:
718             {
719                 const unsigned short* pattr = (const unsigned short*)((const char*)type + 4);
720
721                 printf("\t%x => Method list\n", curr_type);
722                 while ((const char*)pattr < (const char*)type + type->generic.len + 2)
723                 {
724                     switch ((*pattr >> 2) & 7)
725                     {
726                     case 4: case 6:
727                         printf("\t\t\tattr:%s type:%x vtab-offset:%x\n",
728                                get_attr(pattr[0]), pattr[1],
729                                *(const unsigned*)(&pattr[2]));
730                         pattr += 3;
731                         break;
732                     default:
733                         printf("\t\t\tattr:%s type:%x\n",
734                                get_attr(pattr[0]), pattr[1]);
735                         pattr += 2;
736                     }
737                 }
738             }
739             break;
740
741         case LF_METHODLIST_V2:
742             {
743                 const unsigned* pattr = (const unsigned*)((const char*)type + 4);
744
745                 printf("\t%x => Method list\n", curr_type);
746                 while ((const char*)pattr < (const char*)type + type->generic.len + 2)
747                 {
748                     switch ((*pattr >> 2) & 7)
749                     {
750                     case 4: case 6:
751                         printf("\t\t\tattr:%s type:%x vtab-offset:%x\n",
752                                get_attr(pattr[0]), pattr[1], pattr[2]);
753                         pattr += 3;
754                         break;
755                     default:
756                         printf("\t\t\tattr:%s type:%x\n",
757                                get_attr(pattr[0]), pattr[1]);
758                         pattr += 2;
759                     }
760                 }
761             }
762             break;
763
764         case LF_VTSHAPE_V1:
765             {
766                 int count = *(const unsigned short*)((const char*)type + 4);
767                 int shift = 0;
768                 const char* ptr = (const char*)type + 6;
769                 const char* desc[] = {"Near", "Far", "Thin", "Disp to outtermost",
770                                       "Pointer to metaclass", "Near32", "Far32"};
771                 printf("\t%x => VT Shape #%d: ", curr_type, count);
772                 while (count--)
773                 {
774                     if (((*ptr << shift) & 0xF) <= 6)
775                         printf("%s ", desc[(*ptr << shift) & 0xF]);
776                     else
777                         printf("%x ", (*ptr << shift) & 0xF);
778                     if (shift == 0) shift = 4; else {shift = 0; ptr++;}
779                 }
780                 printf("\n");
781             }
782             break;
783
784         default:
785             printf(">>> Unsupported type-id %x for %x\n", type->generic.id, curr_type);
786             dump_data((const void*)type, type->generic.len + 2, "");
787             break;
788         }
789
790         if (!retv)
791             return FALSE;
792
793         curr_type++;
794         ptr += type->generic.len + 2;
795     }
796
797     return TRUE;
798 }
799
800 int codeview_dump_symbols(const void* root, unsigned long size)
801 {
802     int     i, length;
803     char*   curr_func = NULL;
804     int     nest_block = 0;
805     /*
806      * Loop over the different types of records and whenever we
807      * find something we are interested in, record it and move on.
808      */
809     for (i = 0; i < size; i += length)
810     {
811         const union codeview_symbol* sym = (const union codeview_symbol*)((const char*)root + i);
812         length = sym->generic.len + 2;
813         if (!sym->generic.id || length < 4) break;
814         switch (sym->generic.id)
815         {
816         /*
817          * Global and local data symbols.  We don't associate these
818          * with any given source file.
819          */
820         case S_GDATA_V2:
821         case S_LDATA_V2:
822             printf("\tS-%s-Data V2 '%s' %04x:%08x type:%08x\n", 
823                    sym->generic.id == S_GDATA_V2 ? "Global" : "Local",
824                    p_string(&sym->data_v2.p_name),
825                    sym->data_v2.segment, sym->data_v2.offset, sym->data_v2.symtype);
826             break;
827
828         case S_LDATA_V3:
829         case S_GDATA_V3:
830 /* EPP         case S_DATA_V3: */
831             printf("\tS-%s-Data V3 '%s' (%04x:%08x) type:%08x\n",
832                    sym->generic.id == S_GDATA_V3 ? "Global" : "Local",
833                    sym->data_v3.name, 
834                    sym->data_v3.segment, sym->data_v3.offset, 
835                    sym->data_v3.symtype);
836             break;
837
838         case S_PUB_V2:
839             printf("\tS-Public V2 '%s' %04x:%08x type:%08x\n",
840                    p_string(&sym->public_v2.p_name),
841                    sym->public_v2.segment, sym->public_v2.offset,
842                    sym->public_v2.symtype);
843             break;
844
845         case S_PUB_V3:
846         /* not completely sure of those two anyway */
847         case S_PUB_FUNC1_V3:
848         case S_PUB_FUNC2_V3:
849             printf("\tS-Public%s V3 '%s' %04x:%08x type:%08x\n",
850                    sym->generic.id == S_PUB_V3 ? "" :
851                                       (sym->generic.id == S_PUB_FUNC1_V3 ? "<subkind1" : "<subkind2"),
852                    sym->public_v3.name,
853                    sym->public_v3.segment,
854                    sym->public_v3.offset, sym->public_v3.symtype);
855             break;
856
857         /*
858          * Sort of like a global function, but it just points
859          * to a thunk, which is a stupid name for what amounts to
860          * a PLT slot in the normal jargon that everyone else uses.
861          */
862         case S_THUNK_V1:
863             printf("\tS-Thunk V1 '%s' (%04x:%08x#%x) type:%x\n", 
864                    p_string(&sym->thunk_v1.p_name),
865                    sym->thunk_v1.segment, sym->thunk_v1.offset,
866                    sym->thunk_v1.thunk_len, sym->thunk_v1.thtype);
867             curr_func = strdup(p_string(&sym->thunk_v1.p_name));
868             break;
869
870         case S_THUNK_V3:
871             printf("\tS-Thunk V3 '%s' (%04x:%08x#%x) type:%x\n", 
872                    sym->thunk_v3.name,
873                    sym->thunk_v3.segment, sym->thunk_v3.offset,
874                    sym->thunk_v3.thunk_len, sym->thunk_v3.thtype);
875             curr_func = strdup(sym->thunk_v3.name);
876             break;
877
878         /* Global and static functions */
879         case S_GPROC_V1:
880         case S_LPROC_V1:
881             printf("\tS-%s-Proc V1: '%s' (%04x:%08x#%x) type:%x\n",
882                    sym->generic.id == S_GPROC_V1 ? "Global" : "-Local",
883                    p_string(&sym->proc_v1.p_name),
884                    sym->proc_v1.segment, sym->proc_v1.offset,
885                    sym->proc_v1.proc_len, sym->proc_v1.proctype);
886             if (nest_block)
887             {
888                 printf(">>> prev func '%s' still has nest_block %u count\n", curr_func, nest_block);
889                 nest_block = 0;
890             }
891             curr_func = strdup(p_string(&sym->proc_v1.p_name));
892 /* EPP  unsigned int    pparent; */
893 /* EPP  unsigned int    pend; */
894 /* EPP  unsigned int    next; */
895 /* EPP  unsigned int    debug_start; */
896 /* EPP  unsigned int    debug_end; */
897 /* EPP  unsigned char   flags; */
898             break;
899
900         case S_GPROC_V2:
901         case S_LPROC_V2:
902             printf("\tS-%s-Proc V2: '%s' (%04x:%08x#%x) type:%x\n",
903                    sym->generic.id == S_GPROC_V2 ? "Global" : "-Local",
904                    p_string(&sym->proc_v2.p_name),
905                    sym->proc_v2.segment, sym->proc_v2.offset,
906                    sym->proc_v2.proc_len, sym->proc_v2.proctype);
907             if (nest_block)
908             {
909                 printf(">>> prev func '%s' still has nest_block %u count\n", curr_func, nest_block);
910                 nest_block = 0;
911             }
912             curr_func = strdup(p_string(&sym->proc_v2.p_name));
913 /* EPP  unsigned int    pparent; */
914 /* EPP  unsigned int    pend; */
915 /* EPP  unsigned int    next; */
916 /* EPP  unsigned int    debug_start; */
917 /* EPP  unsigned int    debug_end; */
918 /* EPP  unsigned char   flags; */
919             break;
920
921         case S_LPROC_V3:
922         case S_GPROC_V3:
923             printf("\tS-%s-Procedure V3 '%s' (%04x:%08x#%x) type:%x\n",
924                    sym->generic.id == S_GPROC_V3 ? "Global" : "Local",
925                    sym->proc_v3.name,
926                    sym->proc_v3.segment, sym->proc_v3.offset,
927                    sym->proc_v3.proc_len, sym->proc_v3.proctype);
928             if (nest_block)
929             {
930                 printf(">>> prev func '%s' still has nest_block %u count\n", curr_func, nest_block);
931                 nest_block = 0;
932             }
933             curr_func = strdup(sym->proc_v3.name);
934 /* EPP  unsigned int    pparent; */
935 /* EPP  unsigned int    pend; */
936 /* EPP  unsigned int    next; */
937 /* EPP  unsigned int    debug_start; */
938 /* EPP  unsigned int    debug_end; */
939 /* EPP  unsigned char   flags; */
940             break;
941
942         /* Function parameters and stack variables */
943         case S_BPREL_V1:
944             printf("\tS-BP-relative V1: '%s' @%d type:%x (%s)\n", 
945                    p_string(&sym->stack_v1.p_name),
946                    sym->stack_v1.offset, sym->stack_v1.symtype, curr_func);
947             break;
948
949         case S_BPREL_V2:
950             printf("\tS-BP-relative V2: '%s' @%d type:%x (%s)\n", 
951                    p_string(&sym->stack_v2.p_name),
952                    sym->stack_v2.offset, sym->stack_v2.symtype, curr_func);
953             break;
954
955         case S_BPREL_V3:
956             printf("\tS-BP-relative V3: '%s' @%d type:%x (in %s)\n", 
957                    sym->stack_v3.name, sym->stack_v3.offset,
958                    sym->stack_v3.symtype, curr_func);
959             break;
960
961         case S_REGISTER_V1:
962             printf("\tS-Register V1 '%s' in %s type:%x register:%x\n",
963                    p_string(&sym->register_v1.p_name),
964                    curr_func, sym->register_v1.reg, sym->register_v1.type);
965             break;
966
967         case S_REGISTER_V2:
968             printf("\tS-Register V2 '%s' in %s type:%x register:%x\n",
969                    p_string(&sym->register_v2.p_name),
970                    curr_func, sym->register_v2.reg, sym->register_v2.type);
971             break;
972
973         case S_REGISTER_V3:
974             printf("\tS-Register V3 '%s' in %s type:%x register:%x\n",
975                    sym->register_v3.name,
976                    curr_func, sym->register_v3.reg, sym->register_v3.type);
977             break;
978
979         case S_BLOCK_V1:
980             printf("\tS-Block V1 '%s' in '%s' (%04x:%08x#%08x)\n",
981                    p_string(&sym->block_v1.p_name),
982                    curr_func, 
983                    sym->block_v1.segment, sym->block_v1.offset,
984                    sym->block_v1.length);
985             nest_block++;
986             break;
987
988         case S_BLOCK_V3:
989             printf("\tS-Block V3 '%s' in '%s' (%04x:%08x#%08x) parent:%u end:%x\n",
990                    sym->block_v3.name, curr_func, 
991                    sym->block_v3.segment, sym->block_v3.offset, sym->block_v3.length,
992                    sym->block_v3.parent, sym->block_v3.end);
993             nest_block++;
994             break;
995
996         case S_END_V1:
997             if (nest_block)
998             {
999                 nest_block--;
1000                 printf("\tS-End-Of block (%u)\n", nest_block);
1001             }
1002             else
1003             {
1004                 printf("\tS-End-Of %s\n", curr_func);
1005                 free(curr_func);
1006                 curr_func = NULL;
1007             }
1008             break;
1009
1010         case S_COMPILAND_V1:
1011             {
1012                 const char*     machine;
1013                 const char*     lang;
1014
1015                 switch (sym->compiland_v1.unknown & 0xFF)
1016                 {
1017                 case 0x00:      machine = "Intel 8080"; break;
1018                 case 0x01:      machine = "Intel 8086"; break;
1019                 case 0x02:      machine = "Intel 80286"; break;
1020                 case 0x03:      machine = "Intel 80386"; break;
1021                 case 0x04:      machine = "Intel 80486"; break;
1022                 case 0x05:      machine = "Intel Pentium"; break;
1023                 case 0x10:      machine = "MIPS R4000"; break;
1024                 default:
1025                     {
1026                         static char tmp[16];
1027                         sprintf(tmp, "machine=%x", sym->compiland_v1.unknown & 0xFF);
1028                         machine = tmp;
1029                     }
1030                     break;
1031                 }
1032                 switch ((sym->compiland_v1.unknown >> 8) & 0xFF)
1033                 {
1034                 case 0x00:      lang = "C"; break;
1035                 case 0x01:      lang = "C++"; break;
1036                 case 0x02:      lang = "Fortran"; break;
1037                 case 0x03:      lang = "Masm"; break;
1038                 case 0x04:      lang = "Pascal"; break;
1039                 case 0x05:      lang = "Basic"; break;
1040                 case 0x06:      lang = "Cobol"; break;
1041                 default:
1042                     {
1043                         static char tmp[16];
1044                         sprintf(tmp, "language=%x", (sym->compiland_v1.unknown >> 8) & 0xFF);
1045                         lang = tmp;
1046                     }
1047                     break;
1048                 }
1049
1050                 printf("\tS-Compiland V1 '%s' %s %s unk:%x\n",
1051                        p_string(&sym->compiland_v1.p_name), machine, lang,
1052                        sym->compiland_v1.unknown >> 16);
1053             }
1054             break;
1055
1056         case S_COMPILAND_V2:
1057             printf("\tS-Compiland V2 '%s'\n",
1058                    p_string(&sym->compiland_v2.p_name));
1059             dump_data((const void*)sym, sym->generic.len + 2, "  ");
1060             {
1061                 const char* ptr = sym->compiland_v2.p_name.name + sym->compiland_v2.p_name.namelen;
1062                 while (*ptr)
1063                 {
1064                     printf("\t\t%s => ", ptr); ptr += strlen(ptr) + 1;
1065                     printf("%s\n", ptr); ptr += strlen(ptr) + 1;
1066                 }
1067             }
1068             break;
1069
1070         case S_COMPILAND_V3:
1071             printf("\tS-Compiland V3 '%s' unknown:%x\n",
1072                    sym->compiland_v3.name, sym->compiland_v3.unknown);
1073             break;
1074
1075         case S_OBJNAME_V1:
1076             printf("\tS-ObjName V1 sig:%.4s '%s'\n",
1077                    sym->objname_v1.signature, p_string(&sym->objname_v1.p_name));
1078             break;
1079
1080         case S_LABEL_V1:
1081             printf("\tS-Label V1 '%s' in '%s' (%04x:%08x)\n", 
1082                    p_string(&sym->label_v1.p_name),     
1083                    curr_func, sym->label_v1.segment, sym->label_v1.offset);
1084             break;
1085
1086         case S_LABEL_V3:
1087             printf("\tS-Label V3 '%s' in '%s' (%04x:%08x) flag:%x\n", 
1088                    sym->label_v3.name, curr_func, sym->label_v3.segment,
1089                    sym->label_v3.offset, sym->label_v3.flags);
1090             break;
1091
1092         case S_CONSTANT_V2:
1093             {
1094                 int             val, vlen;
1095
1096                 vlen = numeric_leaf(&val, &sym->constant_v2.cvalue);
1097                 printf("\tS-Constant V2 '%s' = %u type:%x\n", 
1098                        p_string(PSTRING(&sym->constant_v2.cvalue, vlen)), val, sym->constant_v2.type);
1099             }
1100             break;
1101
1102         case S_CONSTANT_V3:
1103             {
1104                 int             val, vlen;
1105
1106                 vlen = numeric_leaf(&val, &sym->constant_v3.cvalue);
1107                 printf("\tS-Constant V3 '%s' = %u type:%x\n",
1108                        (const char*)&sym->constant_v3.cvalue + vlen,
1109                        val, sym->constant_v3.type);
1110             }
1111             break;
1112
1113         case S_UDT_V1:
1114             printf("\tS-Udt V1 '%s': type:0x%x\n", 
1115                    p_string(&sym->udt_v1.p_name), sym->udt_v1.type);
1116             break;
1117
1118         case S_UDT_V2:
1119             printf("\tS-Udt V2 '%s': type:0x%x\n", 
1120                    p_string(&sym->udt_v2.p_name), sym->udt_v2.type);
1121             break;
1122
1123         case S_UDT_V3:
1124             printf("\tS-Udt V3 '%s': type:0x%x\n",
1125                    sym->udt_v3.name, sym->udt_v3.type);
1126             break;
1127         /*
1128          * These are special, in that they are always followed by an
1129          * additional length-prefixed string which is *not* included
1130          * into the symbol length count.  We need to skip it.
1131          */
1132         case S_PROCREF_V1:
1133             printf("\tS-Procref V1 "); goto doaref;
1134         case S_DATAREF_V1:
1135             printf("\tS-Dataref V1 "); goto doaref;
1136         case S_LPROCREF_V1:
1137             printf("\tS-L-Procref V1 "); goto doaref;
1138         doaref:
1139             {
1140                 const struct p_string* pname;
1141
1142                 pname = PSTRING(sym, length);
1143                 length += (pname->namelen + 1 + 3) & ~3;
1144                 printf("\t%08x %08x %08x '%s'\n",
1145                        *(((const DWORD*)sym) + 1), *(((const DWORD*)sym) + 2), *(((const DWORD*)sym) + 3),
1146                        p_string(pname));
1147             }
1148             break;
1149         case S_MSTOOL_V3:    /* info about tool used to create CU */
1150             {
1151                 const unsigned short*     ptr = ((const unsigned short*)sym) + 2;
1152
1153                 /* FIXME: what are all those values for ? */
1154                 printf("\tTool V3 ??? %x-%x-%x-%x-%x-%x-%x-%x-%x %s\n",
1155                        ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], ptr[6], ptr[7],
1156                        ptr[8], (const char*)(&ptr[9]));
1157                 dump_data((const void*)sym, sym->generic.len + 2, "\t\t");
1158             }
1159             break;
1160
1161         case S_ALIGN_V1:
1162             /* simply skip it */
1163             break;
1164
1165         default:
1166             printf(">>> Unsupported symbol-id %x sz=%d\n", sym->generic.id, sym->generic.len + 2);
1167             dump_data((const void*)sym, sym->generic.len + 2, "  ");
1168         }
1169     }
1170     return 0;
1171 }