dbghelp: MSC types.
[wine] / dlls / dbghelp / msc.c
1 /*
2  * File msc.c - read VC++ debug information from COFF and eventually
3  * from PDB files.
4  *
5  * Copyright (C) 1996,      Eric Youngdale.
6  * Copyright (C) 1999-2000, Ulrich Weigand.
7  * Copyright (C) 2004-2005, Eric Pouech.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 /*
25  * Note - this handles reading debug information for 32 bit applications
26  * that run under Windows-NT for example.  I doubt that this would work well
27  * for 16 bit applications, but I don't think it really matters since the
28  * file format is different, and we should never get in here in such cases.
29  *
30  * TODO:
31  *      Get 16 bit CV stuff working.
32  *      Add symbol size to internal symbol table.
33  */
34
35 #include "config.h"
36 #include "wine/port.h"
37
38 #include <assert.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41
42 #include <string.h>
43 #ifdef HAVE_UNISTD_H
44 # include <unistd.h>
45 #endif
46 #ifndef PATH_MAX
47 #define PATH_MAX MAX_PATH
48 #endif
49 #include <stdarg.h>
50 #include "windef.h"
51 #include "winbase.h"
52 #include "winreg.h"
53 #include "winternl.h"
54
55 #include "wine/exception.h"
56 #include "wine/debug.h"
57 #include "excpt.h"
58 #include "dbghelp_private.h"
59 #include "mscvpdb.h"
60
61 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_msc);
62
63 #define MAX_PATHNAME_LEN 1024
64
65 /*========================================================================
66  * Debug file access helper routines
67  */
68
69 static void dump(const void* ptr, unsigned len)
70 {
71     int         i, j;
72     char        msg[128];
73     const char* hexof = "0123456789abcdef";
74     const BYTE* x = (const BYTE*)ptr;
75
76     for (i = 0; i < len; i += 16)
77     {
78         sprintf(msg, "%08x: ", i);
79         memset(msg + 10, ' ', 3 * 16 + 1 + 16);
80         for (j = 0; j < min(16, len - i); j++)
81         {
82             msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
83             msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
84             msg[10 + 3 * j + 2] = ' ';
85             msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
86                 x[i + j] : '.';
87         }
88         msg[10 + 3 * 16] = ' ';
89         msg[10 + 3 * 16 + 1 + 16] = '\0';
90         FIXME("%s\n", msg);
91     }
92 }
93
94 /*========================================================================
95  * Process CodeView type information.
96  */
97
98 #define MAX_BUILTIN_TYPES       0x0480
99 #define FIRST_DEFINABLE_TYPE    0x1000
100
101 static struct symt*     cv_basic_types[MAX_BUILTIN_TYPES];
102
103 #define SymTagCVBitField        (SymTagMax + 0x100)
104 struct codeview_bitfield
105 {
106     struct symt         symt;
107     unsigned            subtype;
108     unsigned            bitposition;
109     unsigned            bitlength;
110 };
111
112 struct cv_defined_module
113 {
114     BOOL                allowed;
115     unsigned int        num_defined_types;
116     struct symt**       defined_types;
117
118     struct codeview_bitfield* bitfields;
119     unsigned            num_bitfields;
120     unsigned            used_bitfields;
121 };
122 /* FIXME: don't make it static */
123 #define CV_MAX_MODULES          32
124 static struct cv_defined_module cv_zmodules[CV_MAX_MODULES];
125 static struct cv_defined_module*cv_current_module;
126
127 static void codeview_init_basic_types(struct module* module)
128 {
129     /*
130      * These are the common builtin types that are used by VC++.
131      */
132     cv_basic_types[T_NOTYPE] = NULL;
133     cv_basic_types[T_ABS]    = NULL;
134     cv_basic_types[T_VOID]   = &symt_new_basic(module, btVoid,  "void", 0)->symt;
135     cv_basic_types[T_CHAR]   = &symt_new_basic(module, btChar,  "char", 1)->symt;
136     cv_basic_types[T_SHORT]  = &symt_new_basic(module, btInt,   "short int", 2)->symt;
137     cv_basic_types[T_LONG]   = &symt_new_basic(module, btInt,   "long int", 4)->symt;
138     cv_basic_types[T_QUAD]   = &symt_new_basic(module, btInt,   "long long int", 8)->symt;
139     cv_basic_types[T_UCHAR]  = &symt_new_basic(module, btUInt,  "unsigned char", 1)->symt;
140     cv_basic_types[T_USHORT] = &symt_new_basic(module, btUInt,  "unsigned short", 2)->symt;
141     cv_basic_types[T_ULONG]  = &symt_new_basic(module, btUInt,  "unsigned long", 4)->symt;
142     cv_basic_types[T_UQUAD]  = &symt_new_basic(module, btUInt,  "unsigned long long", 8)->symt;
143     cv_basic_types[T_REAL32] = &symt_new_basic(module, btFloat, "float", 4)->symt;
144     cv_basic_types[T_REAL64] = &symt_new_basic(module, btFloat, "double", 8)->symt;
145     cv_basic_types[T_RCHAR]  = &symt_new_basic(module, btInt,   "signed char", 1)->symt;
146     cv_basic_types[T_WCHAR]  = &symt_new_basic(module, btWChar, "wchar_t", 2)->symt;
147     cv_basic_types[T_INT4]   = &symt_new_basic(module, btInt,   "INT4", 4)->symt;
148     cv_basic_types[T_UINT4]  = &symt_new_basic(module, btUInt,  "UINT4", 4)->symt;
149
150     cv_basic_types[T_32PVOID]   = &symt_new_pointer(module, cv_basic_types[T_VOID])->symt;
151     cv_basic_types[T_32PCHAR]   = &symt_new_pointer(module, cv_basic_types[T_CHAR])->symt;
152     cv_basic_types[T_32PSHORT]  = &symt_new_pointer(module, cv_basic_types[T_SHORT])->symt;
153     cv_basic_types[T_32PLONG]   = &symt_new_pointer(module, cv_basic_types[T_LONG])->symt;
154     cv_basic_types[T_32PQUAD]   = &symt_new_pointer(module, cv_basic_types[T_QUAD])->symt;
155     cv_basic_types[T_32PUCHAR]  = &symt_new_pointer(module, cv_basic_types[T_UCHAR])->symt;
156     cv_basic_types[T_32PUSHORT] = &symt_new_pointer(module, cv_basic_types[T_USHORT])->symt;
157     cv_basic_types[T_32PULONG]  = &symt_new_pointer(module, cv_basic_types[T_ULONG])->symt;
158     cv_basic_types[T_32PUQUAD]  = &symt_new_pointer(module, cv_basic_types[T_UQUAD])->symt;
159     cv_basic_types[T_32PREAL32] = &symt_new_pointer(module, cv_basic_types[T_REAL32])->symt;
160     cv_basic_types[T_32PREAL64] = &symt_new_pointer(module, cv_basic_types[T_REAL64])->symt;
161     cv_basic_types[T_32PRCHAR]  = &symt_new_pointer(module, cv_basic_types[T_RCHAR])->symt;
162     cv_basic_types[T_32PWCHAR]  = &symt_new_pointer(module, cv_basic_types[T_WCHAR])->symt;
163     cv_basic_types[T_32PINT4]   = &symt_new_pointer(module, cv_basic_types[T_INT4])->symt;
164     cv_basic_types[T_32PUINT4]  = &symt_new_pointer(module, cv_basic_types[T_UINT4])->symt;
165 }
166
167 static int numeric_leaf(int* value, const unsigned short int* leaf)
168 {
169     unsigned short int type = *leaf++;
170     int length = 2;
171
172     if (type < LF_NUMERIC)
173     {
174         *value = type;
175     }
176     else
177     {
178         switch (type)
179         {
180         case LF_CHAR:
181             length += 1;
182             *value = *(const char*)leaf;
183             break;
184
185         case LF_SHORT:
186             length += 2;
187             *value = *(const short*)leaf;
188             break;
189
190         case LF_USHORT:
191             length += 2;
192             *value = *(const unsigned short*)leaf;
193             break;
194
195         case LF_LONG:
196             length += 4;
197             *value = *(const int*)leaf;
198             break;
199
200         case LF_ULONG:
201             length += 4;
202             *value = *(const unsigned int*)leaf;
203             break;
204
205         case LF_QUADWORD:
206         case LF_UQUADWORD:
207             FIXME("Unsupported numeric leaf type %04x\n", type);
208             length += 8;
209             *value = 0;    /* FIXME */
210             break;
211
212         case LF_REAL32:
213             FIXME("Unsupported numeric leaf type %04x\n", type);
214             length += 4;
215             *value = 0;    /* FIXME */
216             break;
217
218         case LF_REAL48:
219             FIXME("Unsupported numeric leaf type %04x\n", type);
220             length += 6;
221             *value = 0;    /* FIXME */
222             break;
223
224         case LF_REAL64:
225             FIXME("Unsupported numeric leaf type %04x\n", type);
226             length += 8;
227             *value = 0;    /* FIXME */
228             break;
229
230         case LF_REAL80:
231             FIXME("Unsupported numeric leaf type %04x\n", type);
232             length += 10;
233             *value = 0;    /* FIXME */
234             break;
235
236         case LF_REAL128:
237             FIXME("Unsupported numeric leaf type %04x\n", type);
238             length += 16;
239             *value = 0;    /* FIXME */
240             break;
241
242         case LF_COMPLEX32:
243             FIXME("Unsupported numeric leaf type %04x\n", type);
244             length += 4;
245             *value = 0;    /* FIXME */
246             break;
247
248         case LF_COMPLEX64:
249             FIXME("Unsupported numeric leaf type %04x\n", type);
250             length += 8;
251             *value = 0;    /* FIXME */
252             break;
253
254         case LF_COMPLEX80:
255             FIXME("Unsupported numeric leaf type %04x\n", type);
256             length += 10;
257             *value = 0;    /* FIXME */
258             break;
259
260         case LF_COMPLEX128:
261             FIXME("Unsupported numeric leaf type %04x\n", type);
262             length += 16;
263             *value = 0;    /* FIXME */
264             break;
265
266         case LF_VARSTRING:
267             FIXME("Unsupported numeric leaf type %04x\n", type);
268             length += 2 + *leaf;
269             *value = 0;    /* FIXME */
270             break;
271
272         default:
273             FIXME("Unknown numeric leaf type %04x\n", type);
274             *value = 0;
275             break;
276         }
277     }
278
279     return length;
280 }
281
282 /* convert a pascal string (as stored in debug information) into
283  * a C string (null terminated).
284  */
285 static const char* terminate_string(const struct p_string* p_name)
286 {
287     static char symname[256];
288
289     memcpy(symname, p_name->name, p_name->namelen);
290     symname[p_name->namelen] = '\0';
291
292     return (!*symname || strcmp(symname, "__unnamed") == 0) ? NULL : symname;
293 }
294
295 static struct symt*  codeview_get_type(unsigned int typeno, BOOL allow_special)
296 {
297     struct symt*        symt = NULL;
298
299     /*
300      * Convert Codeview type numbers into something we can grok internally.
301      * Numbers < FIRST_DEFINABLE_TYPE are all fixed builtin types.
302      * Numbers from FIRST_DEFINABLE_TYPE and up are all user defined (structs, etc).
303      */
304     if (typeno < FIRST_DEFINABLE_TYPE)
305     {
306         if (typeno < MAX_BUILTIN_TYPES)
307             symt = cv_basic_types[typeno];
308     }
309     else
310     {
311         unsigned        mod_index = typeno >> 24;
312         unsigned        mod_typeno = typeno & 0x00FFFFFF;
313         struct cv_defined_module*       mod;
314
315         mod = (mod_index == 0) ? cv_current_module : &cv_zmodules[mod_index];
316
317         if (mod_index >= CV_MAX_MODULES || !mod->allowed) 
318             FIXME("Module of index %d isn't loaded yet (%x)\n", mod_index, typeno);
319         else
320         {
321             if (mod_typeno - FIRST_DEFINABLE_TYPE < mod->num_defined_types)
322                 symt = mod->defined_types[mod_typeno - FIRST_DEFINABLE_TYPE];
323         }
324     }
325     if (!allow_special && symt && symt->tag == SymTagCVBitField)
326         FIXME("bitfields are only handled for UDTs\n");
327     if (!symt && typeno) FIXME("Returning NULL symt for type-id %x\n", typeno);
328     return symt;
329 }
330
331 static int codeview_add_type(unsigned int typeno, struct symt* dt)
332 {
333     if (typeno < FIRST_DEFINABLE_TYPE)
334         FIXME("What the heck\n");
335     if (!cv_current_module)
336     {
337         FIXME("Adding %x to non allowed module\n", typeno);
338         return FALSE;
339     }
340     if ((typeno >> 24) != 0)
341         FIXME("No module index while inserting type-id assumption is wrong %x\n",
342               typeno);
343     while (typeno - FIRST_DEFINABLE_TYPE >= cv_current_module->num_defined_types)
344     {
345         cv_current_module->num_defined_types += 0x100;
346         if (cv_current_module->defined_types)
347             cv_current_module->defined_types = (struct symt**)
348                 HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 
349                             cv_current_module->defined_types,
350                             cv_current_module->num_defined_types * sizeof(struct symt*));
351         else
352             cv_current_module->defined_types = (struct symt**)
353                 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
354                           cv_current_module->num_defined_types * sizeof(struct symt*));
355
356         if (cv_current_module->defined_types == NULL) return FALSE;
357     }
358
359     cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE] = dt;
360     return TRUE;
361 }
362
363 static void codeview_clear_type_table(void)
364 {
365     int i;
366
367     for (i = 0; i < CV_MAX_MODULES; i++)
368     {
369         if (cv_zmodules[i].allowed && cv_zmodules[i].defined_types)
370             HeapFree(GetProcessHeap(), 0, cv_zmodules[i].defined_types);
371         cv_zmodules[i].allowed = FALSE;
372         cv_zmodules[i].defined_types = NULL;
373         cv_zmodules[i].num_defined_types = 0;
374         HeapFree(GetProcessHeap(), 0, cv_zmodules[i].bitfields);
375         cv_zmodules[i].bitfields = NULL;
376         cv_zmodules[i].num_bitfields = cv_zmodules[i].used_bitfields = 0;
377     }
378     cv_current_module = NULL;
379 }
380
381 static int codeview_add_type_pointer(struct module* module, unsigned int typeno, 
382                                      unsigned int datatype)
383 {
384     struct symt* symt = &symt_new_pointer(module,
385                                           codeview_get_type(datatype, FALSE))->symt;
386     return codeview_add_type(typeno, symt);
387 }
388
389 static int codeview_add_type_array(struct module* module, 
390                                    unsigned int typeno, const char* name,
391                                    unsigned int elemtype, unsigned int indextype,
392                                    unsigned int arr_len)
393 {
394     struct symt*        symt;
395     struct symt*        elem = codeview_get_type(elemtype, FALSE);
396     struct symt*        index = codeview_get_type(indextype, FALSE);
397     DWORD               arr_max = 0;
398
399     if (elem)
400     {
401         DWORD64 elem_size;
402         symt_get_info(elem, TI_GET_LENGTH, &elem_size);
403         if (elem_size) arr_max = arr_len / (DWORD)elem_size;
404     }
405     symt = &symt_new_array(module, 0, arr_max, elem, index)->symt;
406     return codeview_add_type(typeno, symt);
407 }
408
409 static int codeview_add_type_bitfield(unsigned int typeno, unsigned int bitoff,
410                                       unsigned int nbits, unsigned int basetype)
411 {
412     if (cv_current_module->used_bitfields >= cv_current_module->num_bitfields)
413     {
414         if (cv_current_module->bitfields)
415         {
416             cv_current_module->num_bitfields *= 2;
417             cv_current_module->bitfields = 
418                 HeapReAlloc(GetProcessHeap(), 0, 
419                             cv_current_module->bitfields, 
420                             cv_current_module->num_bitfields * sizeof(struct codeview_bitfield));
421         }
422         else
423         {
424             cv_current_module->num_bitfields = 64;
425             cv_current_module->bitfields = 
426                 HeapAlloc(GetProcessHeap(), 0, 
427                           cv_current_module->num_bitfields * sizeof(struct codeview_bitfield));
428         }
429         if (!cv_current_module->bitfields) return 0;
430     }
431     
432     cv_current_module->bitfields[cv_current_module->used_bitfields].symt.tag    = SymTagCVBitField;
433     cv_current_module->bitfields[cv_current_module->used_bitfields].subtype     = basetype;
434     cv_current_module->bitfields[cv_current_module->used_bitfields].bitposition = bitoff;
435     cv_current_module->bitfields[cv_current_module->used_bitfields].bitlength   = nbits;
436
437     return codeview_add_type(typeno, &cv_current_module->bitfields[cv_current_module->used_bitfields++].symt);
438 }
439
440 static int codeview_add_type_enum_field_list(struct module* module, 
441                                              unsigned int typeno, 
442                                              const unsigned char* list, int len)
443 {
444     struct symt_enum*           symt;
445     const unsigned char*        ptr = list;
446
447     symt = symt_new_enum(module, NULL);
448     while (ptr - list < len)
449     {
450         const union codeview_fieldtype* type = (const union codeview_fieldtype*)ptr;
451
452         if (*ptr >= 0xf0)       /* LF_PAD... */
453         {
454             ptr += *ptr & 0x0f;
455             continue;
456         }
457
458         switch (type->generic.id)
459         {
460         case LF_ENUMERATE_V1:
461         {
462             int value, vlen = numeric_leaf(&value, &type->enumerate_v1.value);
463             const struct p_string* p_name = (const struct p_string*)((const unsigned char*)&type->enumerate_v1.value + vlen);
464
465             symt_add_enum_element(module, symt, terminate_string(p_name), value);
466             ptr += 2 + 2 + vlen + (1 + p_name->namelen);
467             break;
468         }
469         case LF_ENUMERATE_V3:
470         {
471             int value, vlen = numeric_leaf(&value, &type->enumerate_v3.value);
472             const char* name = (const char*)&type->enumerate_v3.value + vlen;
473
474             symt_add_enum_element(module, symt, name, value);
475             ptr += 2 + 2 + vlen + (1 + strlen(name));
476             break;
477         }
478
479         default:
480             FIXME("Unsupported type %04x in ENUM field list\n", type->generic.id);
481             return FALSE;
482         }
483     }
484
485     return codeview_add_type(typeno, &symt->symt);
486 }
487
488 static int codeview_add_type_struct_field_list(struct module* module, 
489                                                unsigned int typeno, 
490                                                const unsigned char* list, int len)
491 {
492     struct symt_udt*            symt;
493     const unsigned char*        ptr = list;
494     int                         value, leaf_len;
495     const struct p_string*      p_name;
496     const char*                 c_name;
497     struct symt*                subtype;
498
499     symt = symt_new_udt(module, NULL, 0, UdtStruct /* don't care */);
500     while (ptr - list < len)
501     {
502         const union codeview_fieldtype* type = (const union codeview_fieldtype*)ptr;
503
504         if (*ptr >= 0xf0)       /* LF_PAD... */
505         {
506             ptr +=* ptr & 0x0f;
507             continue;
508         }
509
510         switch (type->generic.id)
511         {
512         case LF_BCLASS_V1:
513             leaf_len = numeric_leaf(&value, &type->bclass_v1.offset);
514
515             /* FIXME: ignored for now */
516
517             ptr += 2 + 2 + 2 + leaf_len;
518             break;
519
520         case LF_BCLASS_V2:
521             leaf_len = numeric_leaf(&value, &type->bclass_v2.offset);
522
523             /* FIXME: ignored for now */
524
525             ptr += 2 + 2 + 4 + leaf_len;
526             break;
527
528         case LF_VBCLASS_V1:
529         case LF_IVBCLASS_V1:
530             {
531                 const unsigned short int* p_vboff;
532                 int vpoff, vplen;
533                 leaf_len = numeric_leaf(&value, &type->vbclass_v1.vbpoff);
534                 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v1.vbpoff + leaf_len);
535                 vplen = numeric_leaf(&vpoff, p_vboff);
536
537                 /* FIXME: ignored for now */
538
539                 ptr += 2 + 2 + 2 + 2 + leaf_len + vplen;
540             }
541             break;
542
543         case LF_VBCLASS_V2:
544         case LF_IVBCLASS_V2:
545             {
546                 const unsigned short int* p_vboff;
547                 int vpoff, vplen;
548                 leaf_len = numeric_leaf(&value, &type->vbclass_v2.vbpoff);
549                 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v2.vbpoff + leaf_len);
550                 vplen = numeric_leaf(&vpoff, p_vboff);
551
552                 /* FIXME: ignored for now */
553
554                 ptr += 2 + 2 + 4 + 4 + leaf_len + vplen;
555             }
556             break;
557
558         case LF_MEMBER_V1:
559             leaf_len = numeric_leaf(&value, &type->member_v1.offset);
560             p_name = (const struct p_string*)((const char*)&type->member_v1.offset + leaf_len);
561             subtype = codeview_get_type(type->member_v1.type, TRUE);
562
563             if (!subtype || subtype->tag != SymTagCVBitField)
564             {
565                 DWORD64 elem_size = 0;
566                 if (subtype) symt_get_info(subtype, TI_GET_LENGTH, &elem_size);
567                 symt_add_udt_element(module, symt, terminate_string(p_name),
568                                      codeview_get_type(type->member_v1.type, TRUE),
569                                      value << 3, (DWORD)elem_size << 3);
570             }
571             else
572             {
573                 struct codeview_bitfield* cvbf = (struct codeview_bitfield*)subtype;
574                 symt_add_udt_element(module, symt, terminate_string(p_name),
575                                      codeview_get_type(cvbf->subtype, FALSE),
576                                      cvbf->bitposition, cvbf->bitlength);
577             }
578
579             ptr += 2 + 2 + 2 + leaf_len + (1 + p_name->namelen);
580             break;
581
582         case LF_MEMBER_V2:
583             leaf_len = numeric_leaf(&value, &type->member_v2.offset);
584             p_name = (const struct p_string*)((const unsigned char*)&type->member_v2.offset + leaf_len);
585             subtype = codeview_get_type(type->member_v2.type, TRUE);
586
587             if (!subtype || subtype->tag != SymTagCVBitField)
588             {
589                 DWORD64 elem_size = 0;
590                 if (subtype) symt_get_info(subtype, TI_GET_LENGTH, &elem_size);
591                 symt_add_udt_element(module, symt, terminate_string(p_name),
592                                      subtype, value << 3, (DWORD)elem_size << 3);
593             }
594             else
595             {
596                 struct codeview_bitfield* cvbf = (struct codeview_bitfield*)subtype;
597                 symt_add_udt_element(module, symt, terminate_string(p_name),
598                                      codeview_get_type(cvbf->subtype, FALSE),
599                                      cvbf->bitposition, cvbf->bitlength);
600             }
601
602             ptr += 2 + 2 + 4 + leaf_len + (1 + p_name->namelen);
603             break;
604
605         case LF_MEMBER_V3:
606             leaf_len = numeric_leaf(&value, &type->member_v3.offset);
607             c_name = (const char*)&type->member_v3.offset + leaf_len;
608             subtype = codeview_get_type(type->member_v3.type, TRUE);
609
610             if (!subtype || subtype->tag != SymTagCVBitField)
611             {
612                 DWORD64 elem_size = 0;
613                 if (subtype) symt_get_info(subtype, TI_GET_LENGTH, &elem_size);
614                 symt_add_udt_element(module, symt, c_name,
615                                      subtype, value << 3, (DWORD)elem_size << 3);
616             }
617             else
618             {
619                 struct codeview_bitfield* cvbf = (struct codeview_bitfield*)subtype;
620                 symt_add_udt_element(module, symt, c_name,
621                                      codeview_get_type(cvbf->subtype, FALSE),
622                                      cvbf->bitposition, cvbf->bitlength);
623             }
624
625             ptr += 2 + 2 + 4 + leaf_len + (strlen(c_name) + 1);
626             break;
627
628         case LF_STMEMBER_V1:
629             /* FIXME: ignored for now */
630             ptr += 2 + 2 + 2 + (1 + type->stmember_v1.p_name.namelen);
631             break;
632
633         case LF_STMEMBER_V2:
634             /* FIXME: ignored for now */
635             ptr += 2 + 4 + 2 + (1 + type->stmember_v2.p_name.namelen);
636             break;
637
638         case LF_METHOD_V1:
639             /* FIXME: ignored for now */
640             ptr += 2 + 2 + 2 + (1 + type->method_v1.p_name.namelen);
641             break;
642
643         case LF_METHOD_V2:
644             /* FIXME: ignored for now */
645             ptr += 2 + 2 + 4 + (1 + type->method_v2.p_name.namelen);
646             break;
647
648         case LF_NESTTYPE_V1:
649             /* FIXME: ignored for now */
650             ptr += 2 + 2 + (1 + type->nesttype_v1.p_name.namelen);
651             break;
652
653         case LF_NESTTYPE_V2:
654             /* FIXME: ignored for now */
655             ptr += 2 + 2 + 4 + (1 + type->nesttype_v2.p_name.namelen);
656             break;
657
658         case LF_VFUNCTAB_V1:
659             /* FIXME: ignored for now */
660             ptr += 2 + 2;
661             break;
662
663         case LF_VFUNCTAB_V2:
664             /* FIXME: ignored for now */
665             ptr += 2 + 2 + 4;
666             break;
667
668         case LF_ONEMETHOD_V1:
669             /* FIXME: ignored for now */
670             switch ((type->onemethod_v1.attribute >> 2) & 7)
671             {
672             case 4: case 6: /* (pure) introducing virtual method */
673                 ptr += 2 + 2 + 2 + 4 + (1 + type->onemethod_virt_v1.p_name.namelen);
674                 break;
675
676             default:
677                 ptr += 2 + 2 + 2 + (1 + type->onemethod_v1.p_name.namelen);
678                 break;
679             }
680             break;
681
682         case LF_ONEMETHOD_V2:
683             /* FIXME: ignored for now */
684             switch ((type->onemethod_v2.attribute >> 2) & 7)
685             {
686             case 4: case 6: /* (pure) introducing virtual method */
687                 ptr += 2 + 2 + 4 + 4 + (1 + type->onemethod_virt_v2.p_name.namelen);
688                 break;
689
690             default:
691                 ptr += 2 + 2 + 4 + (1 + type->onemethod_v2.p_name.namelen);
692                 break;
693             }
694             break;
695
696         default:
697             FIXME("Unsupported type %04x in STRUCT field list\n", type->generic.id);
698             return FALSE;
699         }
700     }
701
702     return codeview_add_type(typeno, &symt->symt);
703 }
704
705 static int codeview_add_type_enum(struct module* module, unsigned int typeno, 
706                                   const char* name, unsigned int fieldlist)
707 {
708     struct symt_enum*   symt = symt_new_enum(module, name);
709     struct symt*        list = codeview_get_type(fieldlist, FALSE);
710
711     /* FIXME: this is rather ugly !!! */
712     if (list) symt->vchildren = ((struct symt_enum*)list)->vchildren;
713
714     return codeview_add_type(typeno, &symt->symt);
715 }
716
717 static int codeview_add_type_struct(struct module* module, unsigned int typeno, 
718                                     const char* name, int structlen, 
719                                     unsigned int fieldlist, enum UdtKind kind)
720 {
721     struct symt_udt*    symt = symt_new_udt(module, name, structlen, kind);
722     struct symt*        list = codeview_get_type(fieldlist, FALSE);
723
724     /* FIXME: this is rather ugly !!! */
725     if (list) symt->vchildren = ((struct symt_udt*)list)->vchildren;
726
727     return codeview_add_type(typeno, &symt->symt);
728 }
729
730 static int codeview_new_func_signature(struct module* module, unsigned typeno,
731                                        unsigned ret_type, enum CV_call_e call_conv)
732 {
733     struct symt* symt;
734     symt = &symt_new_function_signature(module, 
735                                         codeview_get_type(ret_type, FALSE),
736                                         call_conv)->symt;
737     return codeview_add_type(typeno, symt);
738 }
739
740 static int codeview_parse_type_table(struct module* module, const BYTE* table,
741                                      const DWORD* offset, DWORD num)
742 {
743     unsigned int                curr_type = 0x1000;
744     int                         retv = TRUE;
745     const union codeview_type*  type;
746     int                         value, leaf_len;
747     const struct p_string*      p_name;
748     const char*                 c_name;
749
750     for (curr_type = 0x1000; retv && curr_type < 0x1000 + num; curr_type++)
751     {
752         retv = TRUE;
753         type = (const union codeview_type*)(table + offset[curr_type - 0x1000]);
754
755         /* type records we're interested in are the ones referenced by symbols
756          * The known ranges are (X mark the ones we want):
757          *   X  0000-0016       for V1 types
758          *      0200-020c       for V1 types referenced by other types
759          *      0400-040f       for V1 types (complex lists & sets)
760          *   X  1000-100f       for V2 types
761          *      1200-120c       for V2 types referenced by other types
762          *      1400-140f       for V1 types (complex lists & sets)
763          *   X  1500-150d       for V3 types
764          *      8000-8010       for numeric leafes
765          */
766         if ((type->generic.id & 0x8600) &&
767             type->generic.id != LF_BITFIELD_V1 && /* still some cases to fix */
768             type->generic.id != LF_BITFIELD_V2 &&
769             type->generic.id != LF_FIELDLIST_V1 &&
770             type->generic.id != LF_FIELDLIST_V2)
771             continue;
772         switch (type->generic.id)
773         {
774         case LF_MODIFIER_V1:
775             /* FIXME: we don't handle modifiers, 
776              * but readd previous type on the curr_type 
777              */
778             WARN("Modifier on %x: %s%s%s%s\n",
779                  type->modifier_v1.type,
780                  type->modifier_v1.attribute & 0x01 ? "const " : "",
781                  type->modifier_v1.attribute & 0x02 ? "volatile " : "",
782                  type->modifier_v1.attribute & 0x04 ? "unaligned " : "",
783                  type->modifier_v1.attribute & ~0x07 ? "unknown " : "");
784             codeview_add_type(curr_type, 
785                               codeview_get_type(type->modifier_v1.type, FALSE));
786             break;
787         case LF_MODIFIER_V2:
788             /* FIXME: we don't handle modifiers, but readd previous type on the curr_type */
789             WARN("Modifier on %x: %s%s%s%s\n",
790                  type->modifier_v2.type,
791                  type->modifier_v2.attribute & 0x01 ? "const " : "",
792                  type->modifier_v2.attribute & 0x02 ? "volatile " : "",
793                  type->modifier_v2.attribute & 0x04 ? "unaligned " : "",
794                  type->modifier_v2.attribute & ~0x07 ? "unknown " : "");
795             codeview_add_type(curr_type, 
796                               codeview_get_type(type->modifier_v2.type, FALSE));
797             break;
798
799         case LF_POINTER_V1:
800             retv = codeview_add_type_pointer(module, curr_type, 
801                                              type->pointer_v1.datatype);
802             break;
803         case LF_POINTER_V2:
804             retv = codeview_add_type_pointer(module, curr_type, 
805                                              type->pointer_v2.datatype);
806             break;
807
808         case LF_ARRAY_V1:
809             leaf_len = numeric_leaf(&value, &type->array_v1.arrlen);
810             p_name = (const struct p_string*)((const unsigned char*)&type->array_v1.arrlen + leaf_len);
811
812             retv = codeview_add_type_array(module, curr_type, terminate_string(p_name),
813                                            type->array_v1.elemtype, type->array_v1.idxtype, value);
814             break;
815         case LF_ARRAY_V2:
816             leaf_len = numeric_leaf(&value, &type->array_v2.arrlen);
817             p_name = (const struct p_string*)((const unsigned char*)&type->array_v2.arrlen + leaf_len);
818
819             retv = codeview_add_type_array(module, curr_type, terminate_string(p_name),
820                                            type->array_v2.elemtype, type->array_v2.idxtype, value);
821             break;
822         case LF_ARRAY_V3:
823             leaf_len = numeric_leaf(&value, &type->array_v3.arrlen);
824             c_name = (const char*)&type->array_v3.arrlen + leaf_len;
825
826             retv = codeview_add_type_array(module, curr_type, c_name,
827                                            type->array_v3.elemtype, type->array_v3.idxtype, value);
828             break;
829
830         case LF_BITFIELD_V1:
831             /* a bitfield is a CodeView specific data type which represent a bitfield
832              * in a structure or a class. For now, we store it in a SymTag-like type
833              * (so that the rest of the process is seamless), but check at udt 
834              * inclusion type for its presence
835              */
836             retv = codeview_add_type_bitfield(curr_type, type->bitfield_v1.bitoff,
837                                               type->bitfield_v1.nbits,
838                                               type->bitfield_v1.type);
839             break;
840         case LF_BITFIELD_V2:
841             retv = codeview_add_type_bitfield(curr_type, type->bitfield_v2.bitoff,
842                                               type->bitfield_v2.nbits,
843                                               type->bitfield_v2.type);
844             break;
845         case LF_FIELDLIST_V1:
846         case LF_FIELDLIST_V2:
847            {
848                /*
849                 * A 'field list' is a CodeView-specific data type which doesn't
850                 * directly correspond to any high-level data type.  It is used
851                 * to hold the collection of members of a struct, class, union
852                 * or enum type.  The actual definition of that type will follow
853                 * later, and refer to the field list definition record.
854                 *
855                 * As we don't have a field list type ourselves, we look ahead
856                 * in the field list to try to find out whether this field list
857                 * will be used for an enum or struct type, and create a dummy
858                 * type of the corresponding sort.  Later on, the definition of
859                 * the 'real' type will copy the member / enumeration data.
860                 */
861                const unsigned char* list = type->fieldlist.list;
862                int   len  = ((const BYTE*)type + type->generic.len + 2) - list;
863
864                if (((const union codeview_fieldtype*)list)->generic.id == LF_ENUMERATE_V1 ||
865                    ((const union codeview_fieldtype*)list)->generic.id == LF_ENUMERATE_V3)
866                    retv = codeview_add_type_enum_field_list(module, curr_type, list, len);
867                else
868                    retv = codeview_add_type_struct_field_list(module, curr_type, list, len);
869            }
870            break;
871
872         case LF_STRUCTURE_V1:
873         case LF_CLASS_V1:
874             leaf_len = numeric_leaf(&value, &type->struct_v1.structlen);
875             p_name = (const struct p_string*)((const unsigned char*)&type->struct_v1.structlen + leaf_len);
876
877             retv = codeview_add_type_struct(module, curr_type, terminate_string(p_name),
878                                             value, type->struct_v1.fieldlist,
879                                             type->generic.id == LF_CLASS_V1 ? UdtClass : UdtStruct);
880             break;
881
882         case LF_STRUCTURE_V2:
883         case LF_CLASS_V2:
884             leaf_len = numeric_leaf(&value, &type->struct_v2.structlen);
885             p_name = (const struct p_string*)((const unsigned char*)&type->struct_v2.structlen + leaf_len);
886
887             retv = codeview_add_type_struct(module, curr_type, terminate_string(p_name),
888                                             value, type->struct_v2.fieldlist,
889                                             type->generic.id == LF_CLASS_V2 ? UdtClass : UdtStruct);
890             break;
891
892         case LF_STRUCTURE_V3:
893         case LF_CLASS_V3:
894             leaf_len = numeric_leaf(&value, &type->struct_v3.structlen);
895             c_name = (const char*)&type->struct_v3.structlen + leaf_len;
896
897             retv = codeview_add_type_struct(module, curr_type, c_name,
898                                             value, type->struct_v3.fieldlist,
899                                             type->generic.id == LF_CLASS_V3 ? UdtClass : UdtStruct);
900             break;
901
902         case LF_UNION_V1:
903             leaf_len = numeric_leaf(&value, &type->union_v1.un_len);
904             p_name = (const struct p_string*)((const unsigned char*)&type->union_v1.un_len + leaf_len);
905
906             retv = codeview_add_type_struct(module, curr_type, terminate_string(p_name),
907                                             value, type->union_v1.fieldlist, UdtUnion);
908             break;
909         case LF_UNION_V2:
910             leaf_len = numeric_leaf(&value, &type->union_v2.un_len);
911             p_name = (const struct p_string*)((const unsigned char*)&type->union_v2.un_len + leaf_len);
912
913             retv = codeview_add_type_struct(module, curr_type, terminate_string(p_name),
914                                             value, type->union_v2.fieldlist, UdtUnion);
915             break;
916         case LF_UNION_V3:
917             leaf_len = numeric_leaf(&value, &type->union_v3.un_len);
918             c_name = (const char*)&type->union_v3.un_len + leaf_len;
919
920             retv = codeview_add_type_struct(module, curr_type, c_name,
921                                             value, type->union_v3.fieldlist, UdtUnion);
922
923         case LF_ENUM_V1:
924             retv = codeview_add_type_enum(module, curr_type, terminate_string(&type->enumeration_v1.p_name),
925                                           type->enumeration_v1.field);
926             break;
927
928         case LF_ENUM_V2:
929             retv = codeview_add_type_enum(module, curr_type, terminate_string(&type->enumeration_v2.p_name),
930                                           type->enumeration_v2.field);
931             break;
932         case LF_ENUM_V3:
933             retv = codeview_add_type_enum(module, curr_type, type->enumeration_v3.name,
934                                           type->enumeration_v3.field);
935             break;
936         case LF_PROCEDURE_V1:
937             retv = codeview_new_func_signature(module, curr_type, 
938                                                type->procedure_v1.rvtype,
939                                                type->procedure_v1.call);
940             break;
941         case LF_PROCEDURE_V2:
942             retv = codeview_new_func_signature(module, curr_type, 
943                                                type->procedure_v2.rvtype,
944                                                type->procedure_v2.call);
945             break;
946         case LF_MFUNCTION_V1:
947             /* FIXME: for C++, this is plain wrong, but as we don't use arg types
948              * nor class information, this would just do for now
949              */
950             retv = codeview_new_func_signature(module, curr_type,
951                                                type->mfunction_v1.rvtype,
952                                                type->mfunction_v1.call);
953             break;
954         case LF_MFUNCTION_V2:
955             /* FIXME: for C++, this is plain wrong, but as we don't use arg types
956              * nor class information, this would just do for now
957              */
958             retv = codeview_new_func_signature(module, curr_type,
959                                                type->mfunction_v2.rvtype,
960                                                type->mfunction_v2.call);
961             break;
962
963         default:
964             FIXME("Unsupported type-id leaf %x\n", type->generic.id);
965             dump(type, 2 + type->generic.len);
966             break;
967         }
968     }
969
970     return retv;
971 }
972
973 /*========================================================================
974  * Process CodeView line number information.
975  */
976
977 static struct codeview_linetab* codeview_snarf_linetab(struct module* module, 
978                                                        const BYTE* linetab, int size,
979                                                        BOOL pascal_str)
980 {
981     int                         file_segcount;
982     char                        filename[PATH_MAX];
983     const unsigned int*         filetab;
984     const struct p_string*      p_fn;
985     int                         i;
986     int                         k;
987     struct codeview_linetab*    lt_hdr;
988     const unsigned int*         lt_ptr;
989     int                         nfile;
990     int                         nseg;
991     union any_size              pnt;
992     union any_size              pnt2;
993     const struct startend*      start;
994     int                         this_seg;
995     unsigned                    source;
996
997     /*
998      * Now get the important bits.
999      */
1000     pnt.uc = linetab;
1001     nfile = *pnt.s++;
1002     nseg = *pnt.s++;
1003
1004     filetab = (const unsigned int*) pnt.c;
1005
1006     /*
1007      * Now count up the number of segments in the file.
1008      */
1009     nseg = 0;
1010     for (i = 0; i < nfile; i++)
1011     {
1012         pnt2.uc = linetab + filetab[i];
1013         nseg += *pnt2.s;
1014     }
1015
1016     /*
1017      * Next allocate the header we will be returning.
1018      * There is one header for each segment, so that we can reach in
1019      * and pull bits as required.
1020      */
1021     lt_hdr = (struct codeview_linetab*)
1022         HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nseg + 1) * sizeof(*lt_hdr));
1023     if (lt_hdr == NULL)
1024     {
1025         goto leave;
1026     }
1027
1028     /*
1029      * Now fill the header we will be returning, one for each segment.
1030      * Note that this will basically just contain pointers into the existing
1031      * line table, and we do not actually copy any additional information
1032      * or allocate any additional memory.
1033      */
1034
1035     this_seg = 0;
1036     for (i = 0; i < nfile; i++)
1037     {
1038         /*
1039          * Get the pointer into the segment information.
1040          */
1041         pnt2.uc = linetab + filetab[i];
1042         file_segcount = *pnt2.s;
1043
1044         pnt2.ui++;
1045         lt_ptr = (const unsigned int*) pnt2.c;
1046         start = (const struct startend*)(lt_ptr + file_segcount);
1047
1048         /*
1049          * Now snarf the filename for all of the segments for this file.
1050          */
1051         if (pascal_str)
1052         {
1053             p_fn = (const struct p_string*)(start + file_segcount);
1054             memset(filename, 0, sizeof(filename));
1055             memcpy(filename, p_fn->name, p_fn->namelen);
1056             source = source_new(module, filename);
1057         }
1058         else
1059             source = source_new(module, (const char*)(start + file_segcount));
1060         
1061         for (k = 0; k < file_segcount; k++, this_seg++)
1062         {
1063             pnt2.uc = linetab + lt_ptr[k];
1064             lt_hdr[this_seg].start      = start[k].start;
1065             lt_hdr[this_seg].end        = start[k].end;
1066             lt_hdr[this_seg].source     = source;
1067             lt_hdr[this_seg].segno      = *pnt2.s++;
1068             lt_hdr[this_seg].nline      = *pnt2.s++;
1069             lt_hdr[this_seg].offtab     = pnt2.ui;
1070             lt_hdr[this_seg].linetab    = (const unsigned short*)(pnt2.ui + lt_hdr[this_seg].nline);
1071         }
1072     }
1073
1074 leave:
1075
1076   return lt_hdr;
1077
1078 }
1079
1080 /*========================================================================
1081  * Process CodeView symbol information.
1082  */
1083
1084 static unsigned int codeview_map_offset(const struct msc_debug_info* msc_dbg,
1085                                         unsigned int offset)
1086 {
1087     int                 nomap = msc_dbg->nomap;
1088     const OMAP_DATA*    omapp = msc_dbg->omapp;
1089     int                 i;
1090
1091     if (!nomap || !omapp) return offset;
1092
1093     /* FIXME: use binary search */
1094     for (i = 0; i < nomap - 1; i++)
1095         if (omapp[i].from <= offset && omapp[i+1].from > offset)
1096             return !omapp[i].to ? 0 : omapp[i].to + (offset - omapp[i].from);
1097
1098     return 0;
1099 }
1100
1101 static const struct codeview_linetab*
1102 codeview_get_linetab(const struct codeview_linetab* linetab,
1103                      unsigned seg, unsigned offset)
1104 {
1105     /*
1106      * Check whether we have line number information
1107      */
1108     if (linetab)
1109     {
1110         for (; linetab->linetab; linetab++)
1111             if (linetab->segno == seg &&
1112                 linetab->start <= offset && linetab->end   >  offset)
1113                 break;
1114         if (!linetab->linetab) linetab = NULL;
1115     }
1116     return linetab;
1117 }
1118
1119 static unsigned codeview_get_address(const struct msc_debug_info* msc_dbg, 
1120                                      unsigned seg, unsigned offset)
1121 {
1122     int                         nsect = msc_dbg->nsect;
1123     const IMAGE_SECTION_HEADER* sectp = msc_dbg->sectp;
1124
1125     if (!seg || seg > nsect) return 0;
1126     return msc_dbg->module->module.BaseOfImage +
1127         codeview_map_offset(msc_dbg, sectp[seg-1].VirtualAddress + offset);
1128 }
1129
1130 static void codeview_add_func_linenum(struct module* module, 
1131                                       struct symt_function* func,
1132                                       const struct codeview_linetab* linetab,
1133                                       unsigned offset, unsigned size)
1134 {
1135     unsigned int        i;
1136
1137     if (!linetab) return;
1138     for (i = 0; i < linetab->nline; i++)
1139     {
1140         if (linetab->offtab[i] >= offset && linetab->offtab[i] < offset + size)
1141         {
1142             symt_add_func_line(module, func, linetab->source,
1143                                linetab->linetab[i], linetab->offtab[i] - offset);
1144         }
1145     }
1146 }
1147
1148 static int codeview_snarf(const struct msc_debug_info* msc_dbg, const BYTE* root, 
1149                           int offset, int size,
1150                           struct codeview_linetab* linetab)
1151 {
1152     struct symt_function*               curr_func = NULL;
1153     int                                 i, length;
1154     const struct codeview_linetab*      flt;
1155     struct symt_block*                  block = NULL;
1156     struct symt*                        symt;
1157     const char*                         name;
1158     struct symt_compiland*              compiland = NULL;
1159
1160     /*
1161      * Loop over the different types of records and whenever we
1162      * find something we are interested in, record it and move on.
1163      */
1164     for (i = offset; i < size; i += length)
1165     {
1166         const union codeview_symbol* sym = (const union codeview_symbol*)(root + i);
1167         length = sym->generic.len + 2;
1168         if (i + length > size) break;
1169         if (length & 3) FIXME("unpadded len %u\n", length);
1170
1171         switch (sym->generic.id)
1172         {
1173         /*
1174          * Global and local data symbols.  We don't associate these
1175          * with any given source file.
1176          */
1177         case S_GDATA_V1:
1178         case S_LDATA_V1:
1179             symt_new_global_variable(msc_dbg->module, compiland,
1180                                      terminate_string(&sym->data_v1.p_name), sym->generic.id == S_LDATA_V1,
1181                                      codeview_get_address(msc_dbg, sym->data_v1.segment, sym->data_v1.offset),
1182                                      0,
1183                                      codeview_get_type(sym->data_v1.symtype, FALSE));
1184             break;
1185         case S_GDATA_V2:
1186         case S_LDATA_V2:
1187             name = terminate_string(&sym->data_v2.p_name);
1188             if (name)
1189                 symt_new_global_variable(msc_dbg->module, compiland,
1190                                          name, sym->generic.id == S_LDATA_V2,
1191                                          codeview_get_address(msc_dbg, sym->data_v2.segment, sym->data_v2.offset),
1192                                          0,
1193                                          codeview_get_type(sym->data_v2.symtype, FALSE));
1194             break;
1195         case S_GDATA_V3:
1196         case S_LDATA_V3:
1197             if (*sym->data_v3.name)
1198                 symt_new_global_variable(msc_dbg->module, compiland,
1199                                          sym->data_v3.name,
1200                                          sym->generic.id == S_LDATA_V3,
1201                                          codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset),
1202                                          0,
1203                                          codeview_get_type(sym->data_v3.symtype, FALSE));
1204             break;
1205
1206         case S_PUB_V1: /* FIXME is this really a 'data_v1' structure ?? */
1207             if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1208             {
1209                 symt_new_public(msc_dbg->module, compiland,
1210                                 terminate_string(&sym->data_v1.p_name), 
1211                                 codeview_get_address(msc_dbg, sym->data_v1.segment, sym->data_v1.offset),
1212                                 1, TRUE /* FIXME */, TRUE /* FIXME */);
1213             }
1214             break;
1215         case S_PUB_V2: /* FIXME is this really a 'data_v2' structure ?? */
1216             if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1217             {
1218                 symt_new_public(msc_dbg->module, compiland,
1219                                 terminate_string(&sym->data_v2.p_name), 
1220                                 codeview_get_address(msc_dbg, sym->data_v2.segment, sym->data_v2.offset),
1221                                 1, TRUE /* FIXME */, TRUE /* FIXME */);
1222             }
1223             break;
1224
1225         /*
1226          * Sort of like a global function, but it just points
1227          * to a thunk, which is a stupid name for what amounts to
1228          * a PLT slot in the normal jargon that everyone else uses.
1229          */
1230         case S_THUNK_V1:
1231             symt_new_thunk(msc_dbg->module, compiland,
1232                            terminate_string(&sym->thunk_v1.p_name), sym->thunk_v1.thtype,
1233                            codeview_get_address(msc_dbg, sym->thunk_v1.segment, sym->thunk_v1.offset),
1234                            sym->thunk_v1.thunk_len);
1235             break;
1236         case S_THUNK_V3:
1237             symt_new_thunk(msc_dbg->module, compiland,
1238                            sym->thunk_v3.name, sym->thunk_v3.thtype,
1239                            codeview_get_address(msc_dbg, sym->thunk_v3.segment, sym->thunk_v3.offset),
1240                            sym->thunk_v3.thunk_len);
1241             break;
1242
1243         /*
1244          * Global and static functions.
1245          */
1246         case S_GPROC_V1:
1247         case S_LPROC_V1:
1248             flt = codeview_get_linetab(linetab, sym->proc_v1.segment, sym->proc_v1.offset);
1249             if (curr_func) FIXME("nested function\n");
1250             curr_func = symt_new_function(msc_dbg->module, compiland,
1251                                           terminate_string(&sym->proc_v1.p_name),
1252                                           codeview_get_address(msc_dbg, sym->proc_v1.segment, sym->proc_v1.offset),
1253                                           sym->proc_v1.proc_len,
1254                                           codeview_get_type(sym->proc_v1.proctype, FALSE));
1255             codeview_add_func_linenum(msc_dbg->module, curr_func, flt, 
1256                                       sym->proc_v1.offset, sym->proc_v1.proc_len);
1257             symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, sym->proc_v1.debug_start, NULL);
1258             symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, sym->proc_v1.debug_end, NULL);
1259             break;
1260         case S_GPROC_V2:
1261         case S_LPROC_V2:
1262             flt = codeview_get_linetab(linetab, sym->proc_v2.segment, sym->proc_v2.offset);
1263             if (curr_func) FIXME("nested function\n");
1264             curr_func = symt_new_function(msc_dbg->module, compiland,
1265                                           terminate_string(&sym->proc_v2.p_name),
1266                                           codeview_get_address(msc_dbg, sym->proc_v2.segment, sym->proc_v2.offset),
1267                                           sym->proc_v2.proc_len,
1268                                           codeview_get_type(sym->proc_v2.proctype, FALSE));
1269             codeview_add_func_linenum(msc_dbg->module, curr_func, flt, 
1270                                       sym->proc_v2.offset, sym->proc_v2.proc_len);
1271             symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, sym->proc_v2.debug_start, NULL);
1272             symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, sym->proc_v2.debug_end, NULL);
1273             break;
1274         case S_GPROC_V3:
1275         case S_LPROC_V3:
1276             flt = codeview_get_linetab(linetab, sym->proc_v3.segment, sym->proc_v3.offset);
1277             if (curr_func) FIXME("nested function\n");
1278             curr_func = symt_new_function(msc_dbg->module, compiland,
1279                                           sym->proc_v3.name,
1280                                           codeview_get_address(msc_dbg, sym->proc_v3.segment, sym->proc_v3.offset),
1281                                           sym->proc_v3.proc_len,
1282                                           codeview_get_type(sym->proc_v3.proctype, FALSE));
1283             codeview_add_func_linenum(msc_dbg->module, curr_func, flt, 
1284                                       sym->proc_v3.offset, sym->proc_v3.proc_len);
1285             symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, sym->proc_v3.debug_start, NULL);
1286             symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, sym->proc_v3.debug_end, NULL);
1287             break;
1288         /*
1289          * Function parameters and stack variables.
1290          */
1291         case S_BPREL_V1:
1292             symt_add_func_local(msc_dbg->module, curr_func, 0, sym->stack_v1.offset,
1293                                 block, codeview_get_type(sym->stack_v1.symtype, FALSE),
1294                                 terminate_string(&sym->stack_v1.p_name));
1295             break;
1296         case S_BPREL_V2:
1297             symt_add_func_local(msc_dbg->module, curr_func, 0, sym->stack_v2.offset,
1298                                 block, codeview_get_type(sym->stack_v2.symtype, FALSE),
1299                                 terminate_string(&sym->stack_v2.p_name));
1300             break;
1301         case S_BPREL_V3:
1302             symt_add_func_local(msc_dbg->module, curr_func, 0, sym->stack_v3.offset,
1303                                 block, codeview_get_type(sym->stack_v3.symtype, FALSE),
1304                                 sym->stack_v3.name);
1305             break;
1306
1307         case S_REGISTER_V1:
1308             symt_add_func_local(msc_dbg->module, curr_func, 0, sym->register_v1.reg,
1309                                 block, codeview_get_type(sym->register_v1.type, FALSE),
1310                                 terminate_string(&sym->register_v1.p_name));
1311             break;
1312         case S_REGISTER_V2:
1313             symt_add_func_local(msc_dbg->module, curr_func, 0, sym->register_v2.reg,
1314                                 block, codeview_get_type(sym->register_v2.type, FALSE),
1315                                 terminate_string(&sym->register_v2.p_name));
1316             break;
1317
1318         case S_BLOCK_V1:
1319             block = symt_open_func_block(msc_dbg->module, curr_func, block, 
1320                                          codeview_get_address(msc_dbg, sym->block_v1.segment, sym->block_v1.offset),
1321                                          sym->block_v1.length);
1322             break;
1323         case S_BLOCK_V3:
1324             block = symt_open_func_block(msc_dbg->module, curr_func, block, 
1325                                          codeview_get_address(msc_dbg, sym->block_v3.segment, sym->block_v3.offset),
1326                                          sym->block_v3.length);
1327             break;
1328
1329         case S_END_V1:
1330             if (block)
1331             {
1332                 block = symt_close_func_block(msc_dbg->module, curr_func, block, 0);
1333             }
1334             else if (curr_func)
1335             {
1336                 symt_normalize_function(msc_dbg->module, curr_func);
1337                 curr_func = NULL;
1338             }
1339             break;
1340
1341         case S_COMPILE_V1:
1342             TRACE("S-Compile-V1 %x %s\n", 
1343                   sym->compile_v1.unknown, terminate_string(&sym->compile_v1.p_name));
1344             break;
1345
1346         case S_COMPILE_V2:
1347             TRACE("S-Compile-V2 %s\n", terminate_string(&sym->compile_v2.p_name));
1348             if (TRACE_ON(dbghelp_msc))
1349             {
1350                 const char* ptr1 = sym->compile_v2.p_name.name + sym->compile_v2.p_name.namelen;
1351                 const char* ptr2;
1352                 while (*ptr1)
1353                 {
1354                     ptr2 = ptr1 + strlen(ptr1) + 1;
1355                     TRACE("\t%s => %s\n", ptr1, ptr2); 
1356                     ptr1 = ptr2 + strlen(ptr2) + 1;
1357                 }
1358             }
1359             break;
1360         case S_COMPILE_V3:
1361             TRACE("S-Compile-V3 %s\n", sym->compile_v3.name);
1362             if (TRACE_ON(dbghelp_msc))
1363             {
1364                 const char* ptr1 = sym->compile_v3.name + strlen(sym->compile_v3.name);
1365                 const char* ptr2;
1366                 while (*ptr1)
1367                 {
1368                     ptr2 = ptr1 + strlen(ptr1) + 1;
1369                     TRACE("\t%s => %s\n", ptr1, ptr2); 
1370                     ptr1 = ptr2 + strlen(ptr2) + 1;
1371                 }
1372             }
1373             break;
1374
1375         case S_OBJNAME_V1:
1376             TRACE("S-ObjName %s\n", terminate_string(&sym->objname_v1.p_name));
1377             compiland = symt_new_compiland(msc_dbg->module, terminate_string(&sym->objname_v1.p_name));
1378             break;
1379
1380         case S_LABEL_V1:
1381             if (curr_func)
1382             {
1383                 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel, 
1384                                         codeview_get_address(msc_dbg, sym->label_v1.segment, sym->label_v1.offset) - curr_func->address,
1385                                         terminate_string(&sym->label_v1.p_name));
1386             }
1387             else
1388                 FIXME("No current function for label %s\n",
1389                       terminate_string(&sym->label_v1.p_name));
1390             break;
1391         case S_LABEL_V3:
1392             if (curr_func)
1393             {
1394                 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel, 
1395                                         codeview_get_address(msc_dbg, sym->label_v3.segment, sym->label_v3.offset) - curr_func->address,
1396                                         sym->label_v3.name);
1397             }
1398             else
1399                 FIXME("No current function for label %s\n", sym->label_v3.name);
1400             break;
1401
1402         case S_CONSTANT_V1:
1403             {
1404                 int                     val, vlen;
1405                 const struct p_string*  name;
1406                 const char*             x;
1407                 struct symt*            se;
1408
1409                 vlen = numeric_leaf(&val, &sym->constant_v1.cvalue);
1410                 name = (const struct p_string*)((const char*)&sym->constant_v1.cvalue + vlen);
1411                 se = codeview_get_type(sym->constant_v1.type, FALSE);
1412                 if (!se) x = "---";
1413                 else if (se->tag == SymTagEnum) x = ((struct symt_enum*)se)->name;
1414                 else x = "###";
1415                     
1416                 TRACE("S-Constant-V1 %u %s %x (%s)\n", 
1417                       val, terminate_string(name), sym->constant_v1.type, x);
1418                 /* FIXME: we should add this as a constant value */
1419             }
1420             break;
1421         case S_CONSTANT_V2:
1422             {
1423                 int                     val, vlen;
1424                 const struct p_string*  name;
1425                 const char*             x;
1426                 struct symt*            se;
1427
1428                 vlen = numeric_leaf(&val, &sym->constant_v2.cvalue);
1429                 name = (const struct p_string*)((const char*)&sym->constant_v2.cvalue + vlen);
1430                 se = codeview_get_type(sym->constant_v2.type, FALSE);
1431                 if (!se) x = "---";
1432                 else if (se->tag == SymTagEnum) x = ((struct symt_enum*)se)->name;
1433                 else x = "###";
1434                     
1435                 TRACE("S-Constant-V2 %u %s %x (%s)\n", 
1436                       val, terminate_string(name), sym->constant_v2.type, x);
1437                 /* FIXME: we should add this as a constant value */
1438             }
1439             break;
1440         case S_CONSTANT_V3:
1441             {
1442                 int                     val, vlen;
1443                 const char*             name;
1444                 const char*             x;
1445                 struct symt*            se;
1446
1447                 vlen = numeric_leaf(&val, &sym->constant_v3.cvalue);
1448                 name = (const char*)&sym->constant_v3.cvalue + vlen;
1449                 se = codeview_get_type(sym->constant_v3.type, FALSE);
1450                 if (!se) x = "---";
1451                 else if (se->tag == SymTagEnum) x = ((struct symt_enum*)se)->name;
1452                 else x = "###";
1453                     
1454                 TRACE("S-Constant-V3 %u %s %x (%s)\n", 
1455                       val, name, sym->constant_v3.type, x);
1456                 /* FIXME: we should add this as a constant value */
1457             }
1458             break;
1459
1460         case S_UDT_V1:
1461             if (sym->udt_v1.type)
1462             {
1463                 if ((symt = codeview_get_type(sym->udt_v1.type, FALSE)))
1464                     symt_new_typedef(msc_dbg->module, symt, 
1465                                      terminate_string(&sym->udt_v1.p_name));
1466                 else
1467                     FIXME("S-Udt %s: couldn't find type 0x%x\n", 
1468                           terminate_string(&sym->udt_v1.p_name), sym->udt_v1.type);
1469             }
1470             break;
1471         case S_UDT_V2:
1472             if (sym->udt_v2.type)
1473             {
1474                 if ((symt = codeview_get_type(sym->udt_v2.type, FALSE)))
1475                     symt_new_typedef(msc_dbg->module, symt, 
1476                                      terminate_string(&sym->udt_v2.p_name));
1477                 else
1478                     FIXME("S-Udt %s: couldn't find type 0x%x\n", 
1479                           terminate_string(&sym->udt_v2.p_name), sym->udt_v2.type);
1480             }
1481             break;
1482         case S_UDT_V3:
1483             if (sym->udt_v3.type)
1484             {
1485                 if ((symt = codeview_get_type(sym->udt_v3.type, FALSE)))
1486                     symt_new_typedef(msc_dbg->module, symt, sym->udt_v3.name);
1487                 else
1488                     FIXME("S-Udt %s: couldn't find type 0x%x\n", 
1489                           sym->udt_v3.name, sym->udt_v3.type);
1490             }
1491             break;
1492
1493          /*
1494          * These are special, in that they are always followed by an
1495          * additional length-prefixed string which is *not* included
1496          * into the symbol length count.  We need to skip it.
1497          */
1498         case S_PROCREF_V1:
1499         case S_DATAREF_V1:
1500         case S_LPROCREF_V1:
1501             name = (const char*)sym + length;
1502             length += (*name + 1 + 3) & ~3;
1503             break;
1504
1505         case S_PUB_DATA_V3:
1506             if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1507             {
1508                 symt_new_public(msc_dbg->module, compiland,
1509                                 sym->data_v3.name, 
1510                                 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset),
1511                                 1, FALSE /* FIXME */, FALSE);
1512             }
1513             break;
1514         case S_PUB_FUNC1_V3:
1515         case S_PUB_FUNC2_V3: /* using a data_v3 isn't what we'd expect */
1516             if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1517             {
1518                 symt_new_public(msc_dbg->module, compiland,
1519                                 sym->data_v3.name, 
1520                                 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset),
1521                                 1, TRUE /* FIXME */, TRUE);
1522             }
1523             break;
1524
1525         case S_MSTOOL_V3: /* just to silence a few warnings */
1526             break;
1527
1528         case S_SSEARCH_V1:
1529             TRACE("Start search: seg=0x%x at offset 0x%08x\n",
1530                   sym->ssearch_v1.segment, sym->ssearch_v1.offset);
1531             break;
1532
1533         default:
1534             FIXME("Unsupported symbol id %x\n", sym->generic.id);
1535             dump(sym, 2 + sym->generic.len);
1536             break;
1537         }
1538     }
1539
1540     if (curr_func) symt_normalize_function(msc_dbg->module, curr_func);
1541
1542     HeapFree(GetProcessHeap(), 0, linetab);
1543     return TRUE;
1544 }
1545
1546 /*========================================================================
1547  * Process PDB file.
1548  */
1549
1550 struct pdb_lookup
1551 {
1552     const char*                 filename;
1553     enum {PDB_JG, PDB_DS}       kind;
1554     union
1555     {
1556         struct
1557         {
1558             DWORD               timestamp;
1559             struct PDB_JG_TOC*  toc;
1560         } jg;
1561         struct
1562         {
1563             GUID                guid;
1564             struct PDB_DS_TOC*  toc;
1565         } ds;
1566     } u;
1567 };
1568
1569 static void* pdb_jg_read(const struct PDB_JG_HEADER* pdb, const WORD* block_list,
1570                          int size)
1571 {
1572     int                         i, num_blocks;
1573     BYTE*                       buffer;
1574
1575     if (!size) return NULL;
1576
1577     num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
1578     buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
1579
1580     for (i = 0; i < num_blocks; i++)
1581         memcpy(buffer + i * pdb->block_size,
1582                (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
1583
1584     return buffer;
1585 }
1586
1587 static void* pdb_ds_read(const struct PDB_DS_HEADER* pdb, const DWORD* block_list,
1588                          int size)
1589 {
1590     int                         i, num_blocks;
1591     BYTE*                       buffer;
1592
1593     if (!size) return NULL;
1594
1595     num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
1596     buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
1597
1598     for (i = 0; i < num_blocks; i++)
1599         memcpy(buffer + i * pdb->block_size,
1600                (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
1601
1602     return buffer;
1603 }
1604
1605 static void* pdb_read_jg_file(const struct PDB_JG_HEADER* pdb,
1606                               const struct PDB_JG_TOC* toc, DWORD file_nr)
1607 {
1608     const WORD*                 block_list;
1609     DWORD                       i;
1610
1611     if (!toc || file_nr >= toc->num_files) return NULL;
1612
1613     block_list = (const WORD*) &toc->file[toc->num_files];
1614     for (i = 0; i < file_nr; i++)
1615         block_list += (toc->file[i].size + pdb->block_size - 1) / pdb->block_size;
1616
1617     return pdb_jg_read(pdb, block_list, toc->file[file_nr].size);
1618 }
1619
1620 static void* pdb_read_ds_file(const struct PDB_DS_HEADER* pdb,
1621                               const struct PDB_DS_TOC* toc, DWORD file_nr)
1622 {
1623     const DWORD*                block_list;
1624     DWORD                       i;
1625
1626     if (!toc || file_nr >= toc->num_files) return NULL;
1627
1628     if (toc->file_size[file_nr] == 0 || toc->file_size[file_nr] == 0xFFFFFFFF)
1629     {
1630         FIXME(">>> requesting NULL stream (%lu)\n", file_nr);
1631         return NULL;
1632     }
1633     block_list = &toc->file_size[toc->num_files];
1634     for (i = 0; i < file_nr; i++)
1635         block_list += (toc->file_size[i] + pdb->block_size - 1) / pdb->block_size;
1636
1637     return pdb_ds_read(pdb, block_list, toc->file_size[file_nr]);
1638 }
1639
1640 static void* pdb_read_file(const char* image, const struct pdb_lookup* pdb_lookup,
1641                            DWORD file_nr)
1642 {
1643     switch (pdb_lookup->kind)
1644     {
1645     case PDB_JG:
1646         return pdb_read_jg_file((const struct PDB_JG_HEADER*)image, 
1647                                 pdb_lookup->u.jg.toc, file_nr);
1648     case PDB_DS:
1649         return pdb_read_ds_file((const struct PDB_DS_HEADER*)image,
1650                                 pdb_lookup->u.ds.toc, file_nr);
1651     }
1652     return NULL;
1653 }
1654
1655 static unsigned pdb_get_file_size(const struct pdb_lookup* pdb_lookup, DWORD file_nr)
1656 {
1657     switch (pdb_lookup->kind)
1658     {
1659     case PDB_JG: return pdb_lookup->u.jg.toc->file[file_nr].size;
1660     case PDB_DS: return pdb_lookup->u.ds.toc->file_size[file_nr];
1661     }
1662     return 0;
1663 }
1664
1665 static void pdb_free(void* buffer)
1666 {
1667     HeapFree(GetProcessHeap(), 0, buffer);
1668 }
1669
1670 static void pdb_free_lookup(const struct pdb_lookup* pdb_lookup)
1671 {
1672     switch (pdb_lookup->kind)
1673     {
1674     case PDB_JG:
1675         if (pdb_lookup->u.jg.toc) pdb_free(pdb_lookup->u.jg.toc);
1676         break;
1677     case PDB_DS:
1678         if (pdb_lookup->u.ds.toc) pdb_free(pdb_lookup->u.ds.toc);
1679         break;
1680     }
1681 }
1682     
1683 static void pdb_convert_types_header(PDB_TYPES* types, const BYTE* image)
1684 {
1685     memset(types, 0, sizeof(PDB_TYPES));
1686     if (!image) return;
1687
1688     if (*(const DWORD*)image < 19960000)   /* FIXME: correct version? */
1689     {
1690         /* Old version of the types record header */
1691         const PDB_TYPES_OLD*    old = (const PDB_TYPES_OLD*)image;
1692         types->version     = old->version;
1693         types->type_offset = sizeof(PDB_TYPES_OLD);
1694         types->type_size   = old->type_size;
1695         types->first_index = old->first_index;
1696         types->last_index  = old->last_index;
1697         types->file        = old->file;
1698     }
1699     else
1700     {
1701         /* New version of the types record header */
1702         *types = *(const PDB_TYPES*)image;
1703     }
1704 }
1705
1706 static void pdb_convert_symbols_header(PDB_SYMBOLS* symbols,
1707                                        int* header_size, const BYTE* image)
1708 {
1709     memset(symbols, 0, sizeof(PDB_SYMBOLS));
1710     if (!image) return;
1711
1712     if (*(const DWORD*)image != 0xffffffff)
1713     {
1714         /* Old version of the symbols record header */
1715         const PDB_SYMBOLS_OLD*  old = (const PDB_SYMBOLS_OLD*)image;
1716         symbols->version         = 0;
1717         symbols->module_size     = old->module_size;
1718         symbols->offset_size     = old->offset_size;
1719         symbols->hash_size       = old->hash_size;
1720         symbols->srcmodule_size  = old->srcmodule_size;
1721         symbols->pdbimport_size  = 0;
1722         symbols->hash1_file      = old->hash1_file;
1723         symbols->hash2_file      = old->hash2_file;
1724         symbols->gsym_file       = old->gsym_file;
1725
1726         *header_size = sizeof(PDB_SYMBOLS_OLD);
1727     }
1728     else
1729     {
1730         /* New version of the symbols record header */
1731         *symbols = *(const PDB_SYMBOLS*)image;
1732         *header_size = sizeof(PDB_SYMBOLS);
1733     }
1734 }
1735
1736 static void pdb_convert_symbol_file(const PDB_SYMBOLS* symbols, 
1737                                     PDB_SYMBOL_FILE_EX* sfile, 
1738                                     unsigned* size, const void* image)
1739
1740 {
1741     if (symbols->version < 19970000)
1742     {
1743         const PDB_SYMBOL_FILE *sym_file = (const PDB_SYMBOL_FILE*)image;
1744         memset(sfile, 0, sizeof(*sfile));
1745         sfile->file        = sym_file->file;
1746         sfile->range.index = sym_file->range.index;
1747         sfile->symbol_size = sym_file->symbol_size;
1748         sfile->lineno_size = sym_file->lineno_size;
1749         *size = sizeof(PDB_SYMBOL_FILE) - 1;
1750     }
1751     else
1752     {
1753         memcpy(sfile, image, sizeof(PDB_SYMBOL_FILE_EX));
1754         *size = sizeof(PDB_SYMBOL_FILE_EX) - 1;
1755     }
1756 }
1757
1758 static BOOL CALLBACK pdb_match(char* file, void* user)
1759 {
1760     /* accept first file that exists */
1761     HANDLE h = CreateFileA(file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1762     TRACE("match with %s returns %p\n", file, h);
1763     if (INVALID_HANDLE_VALUE != h) {
1764         CloseHandle(h);
1765         return FALSE;
1766     }
1767     return TRUE;
1768 }
1769
1770 static HANDLE open_pdb_file(const struct process* pcs, const char* filename)
1771 {
1772     HANDLE      h;
1773     char        dbg_file_path[MAX_PATH];
1774
1775     h = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL, 
1776                     OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1777     /* FIXME: should give more bits on the file to look at */
1778     if (h == INVALID_HANDLE_VALUE &&
1779         SymFindFileInPath(pcs->handle, NULL, (char*)filename, NULL, 0, 0, 0,
1780                           dbg_file_path, pdb_match, NULL))
1781     {
1782         h = CreateFileA(dbg_file_path, GENERIC_READ, FILE_SHARE_READ, NULL, 
1783                         OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1784         TRACE("with %s returns %p\n", dbg_file_path, h);
1785     }
1786     return (h == INVALID_HANDLE_VALUE) ? NULL : h;
1787 }
1788
1789 static void pdb_process_types(const struct msc_debug_info* msc_dbg, 
1790                               const char* image, struct pdb_lookup* pdb_lookup)
1791 {
1792     BYTE*       types_image = NULL;
1793
1794     types_image = pdb_read_file(image, pdb_lookup, 2);
1795     if (types_image)
1796     {
1797         PDB_TYPES               types;
1798         DWORD*                  offset;
1799         DWORD                   num, total;
1800         const BYTE*             table;
1801         const BYTE*             ptr;
1802
1803         pdb_convert_types_header(&types, types_image);
1804
1805         /* Check for unknown versions */
1806         switch (types.version)
1807         {
1808         case 19950410:      /* VC 4.0 */
1809         case 19951122:
1810         case 19961031:      /* VC 5.0 / 6.0 */
1811         case 19990903:
1812             break;
1813         default:
1814             ERR("-Unknown type info version %ld\n", types.version);
1815         }
1816
1817         /* reconstruct the types offset...
1818          * FIXME: maybe it's present in the newest PDB_TYPES structures
1819          */
1820         total = types.last_index - types.first_index + 1;
1821         offset = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD) * total);
1822         table = ptr = types_image + types.type_offset;
1823         num = 0;
1824         while (ptr < table + types.type_size && num < total)
1825         {
1826             offset[num++] = ptr - table;
1827             ptr += ((const union codeview_type*)ptr)->generic.len + 2;
1828         }
1829
1830         /* Read type table */
1831         codeview_parse_type_table(msc_dbg->module, table, offset, num);
1832         HeapFree(GetProcessHeap(), 0, offset);
1833         pdb_free(types_image);
1834     }
1835 }
1836
1837 static const char       PDB_JG_IDENT[] = "Microsoft C/C++ program database 2.00\r\n\032JG\0";
1838 static const char       PDB_DS_IDENT[] = "Microsoft C/C++ MSF 7.00\r\n\032DS\0";
1839
1840 static BOOL pdb_init(struct pdb_lookup* pdb_lookup, const char* image)
1841 {
1842     /* check the file header, and if ok, load the TOC */
1843     TRACE("PDB(%s): %.40s\n", pdb_lookup->filename, debugstr_an(image, 40));
1844     switch (pdb_lookup->kind)
1845     {
1846     case PDB_JG:
1847         pdb_lookup->u.jg.toc = NULL;
1848         if (memcmp(image, PDB_JG_IDENT, sizeof(PDB_JG_IDENT)))
1849         {
1850             FIXME("Couldn't match JG header\n");
1851             return FALSE;
1852         }
1853         else
1854         {
1855             const struct PDB_JG_HEADER* pdb = (const struct PDB_JG_HEADER*)image;
1856             struct PDB_JG_ROOT*         root;
1857
1858             pdb_lookup->u.jg.toc = pdb_jg_read(pdb, pdb->toc_block, pdb->toc.size);
1859             root = pdb_read_jg_file(pdb, pdb_lookup->u.jg.toc, 1);
1860             if (!root)
1861             {
1862                 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
1863                 return FALSE;
1864             }
1865             switch (root->Version)
1866             {
1867             case 19950623:      /* VC 4.0 */
1868             case 19950814:
1869             case 19960307:      /* VC 5.0 */
1870             case 19970604:      /* VC 6.0 */
1871                 break;
1872             default:
1873                 ERR("-Unknown root block version %ld\n", root->Version);
1874             } 
1875             /* Check .PDB time stamp */
1876             if (root->TimeDateStamp != pdb_lookup->u.jg.timestamp)
1877             {
1878                 ERR("-Wrong time stamp of .PDB file %s (0x%08lx, 0x%08lx)\n",
1879                     pdb_lookup->filename, root->TimeDateStamp, 
1880                     pdb_lookup->u.jg.timestamp);
1881             }
1882             pdb_free(root);
1883         }
1884         break;
1885     case PDB_DS:
1886         pdb_lookup->u.ds.toc = NULL;
1887         if (memcmp(image, PDB_DS_IDENT, sizeof(PDB_DS_IDENT)))
1888         {
1889             FIXME("Couldn't match DS header\n");
1890             return FALSE;
1891         }
1892         else
1893         {
1894             const struct PDB_DS_HEADER* pdb = (const struct PDB_DS_HEADER*)image;
1895             struct PDB_DS_ROOT*         root;
1896
1897             pdb_lookup->u.ds.toc = 
1898                 pdb_ds_read(pdb, 
1899                             (const DWORD*)((const char*)pdb + pdb->toc_page * pdb->block_size), 
1900                             pdb->toc_size);
1901             root = pdb_read_ds_file(pdb, pdb_lookup->u.ds.toc, 1);
1902             if (!root)
1903             {
1904                 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
1905                 return FALSE;
1906             }
1907             switch (root->Version)
1908             {
1909             case 20000404:
1910                 break;
1911             default:
1912                 ERR("-Unknown root block version %ld\n", root->Version);
1913             } 
1914             /* Check .PDB time stamp */
1915             if (memcmp(&root->guid, &pdb_lookup->u.ds.guid, sizeof(GUID)))
1916             {
1917                 ERR("-Wrong GUID of .PDB file %s (%s, %s)\n",
1918                     pdb_lookup->filename, 
1919                     wine_dbgstr_guid(&root->guid), 
1920                     wine_dbgstr_guid(&pdb_lookup->u.ds.guid));
1921             }
1922             pdb_free(root);
1923         }
1924         break;
1925     }
1926
1927     if (0) /* some tool to dump the internal files from a PDB file */
1928     {
1929         int     i, num_files;
1930         
1931         switch (pdb_lookup->kind)
1932         {
1933         case PDB_JG: num_files = pdb_lookup->u.jg.toc->num_files; break;
1934         case PDB_DS: num_files = pdb_lookup->u.ds.toc->num_files; break;
1935         }
1936
1937         for (i = 1; i < num_files; i++)
1938         {
1939             unsigned char* x = pdb_read_file(image, pdb_lookup, i);
1940             FIXME("********************** [%u]: size=%08x\n",
1941                   i, pdb_get_file_size(pdb_lookup, i));
1942             dump(x, pdb_get_file_size(pdb_lookup, i));
1943             pdb_free(x);
1944         }
1945     }
1946     return TRUE;
1947 }
1948
1949 static BOOL pdb_process_internal(const struct process* pcs, 
1950                                  const struct msc_debug_info* msc_dbg,
1951                                  struct pdb_lookup* pdb_lookup,
1952                                  unsigned module_index);
1953
1954 static void pdb_process_symbol_imports(const struct process* pcs, 
1955                                        const struct msc_debug_info* msc_dbg,
1956                                        PDB_SYMBOLS* symbols, 
1957                                        const void* symbols_image,
1958                                        char* image, struct pdb_lookup* pdb_lookup,
1959                                        unsigned module_index)
1960 {
1961     if (module_index == -1 && symbols && symbols->pdbimport_size)
1962     {
1963         const PDB_SYMBOL_IMPORT*imp;
1964         const void*             first;
1965         const void*             last;
1966         const char*             ptr;
1967         int                     i = 0;
1968
1969         imp = (const PDB_SYMBOL_IMPORT*)((const char*)symbols_image + sizeof(PDB_SYMBOLS) + 
1970                                          symbols->module_size + symbols->offset_size + 
1971                                          symbols->hash_size + symbols->srcmodule_size);
1972         first = (const char*)imp;
1973         last = (const char*)imp + symbols->pdbimport_size;
1974         while (imp < (const PDB_SYMBOL_IMPORT*)last)
1975         {
1976             ptr = (const char*)imp + sizeof(*imp) + strlen(imp->filename);
1977             if (i >= CV_MAX_MODULES) FIXME("Out of bounds !!!\n");
1978             if (!strcasecmp(pdb_lookup->filename, imp->filename))
1979             {
1980                 if (module_index != -1) FIXME("Twice the entry\n");
1981                 else module_index = i;
1982             }
1983             else
1984             {
1985                 struct pdb_lookup       imp_pdb_lookup;
1986
1987                 imp_pdb_lookup.filename = imp->filename;
1988                 imp_pdb_lookup.kind = PDB_JG;
1989                 imp_pdb_lookup.u.jg.timestamp = imp->TimeDateStamp;
1990                 pdb_process_internal(pcs, msc_dbg, &imp_pdb_lookup, i);
1991             }
1992             i++;
1993             imp = (const PDB_SYMBOL_IMPORT*)((const char*)first + ((ptr - (const char*)first + strlen(ptr) + 1 + 3) & ~3));
1994         }
1995     }
1996     cv_current_module = &cv_zmodules[(module_index == -1) ? 0 : module_index];
1997     if (cv_current_module->allowed) FIXME("Already allowed ??\n");
1998     cv_current_module->allowed = TRUE;
1999     pdb_process_types(msc_dbg, image, pdb_lookup);
2000 }
2001
2002 static BOOL pdb_process_internal(const struct process* pcs, 
2003                                  const struct msc_debug_info* msc_dbg,
2004                                  struct pdb_lookup* pdb_lookup, 
2005                                  unsigned module_index)
2006 {
2007     BOOL        ret = FALSE;
2008     HANDLE      hFile, hMap = NULL;
2009     char*       image = NULL;
2010     BYTE*       symbols_image = NULL;
2011
2012     TRACE("Processing PDB file %s\n", pdb_lookup->filename);
2013
2014     /* Open and map() .PDB file */
2015     if ((hFile = open_pdb_file(pcs, pdb_lookup->filename)) == NULL ||
2016         ((hMap = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL) ||
2017         ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
2018     {
2019         WARN("Unable to open .PDB file: %s\n", pdb_lookup->filename);
2020         goto leave;
2021     }
2022     pdb_init(pdb_lookup, image);
2023
2024     symbols_image = pdb_read_file(image, pdb_lookup, 3);
2025     if (symbols_image)
2026     {
2027         PDB_SYMBOLS symbols;
2028         BYTE*       modimage;
2029         BYTE*       file;
2030         int         header_size = 0;
2031         
2032         pdb_convert_symbols_header(&symbols, &header_size, symbols_image);
2033         switch (symbols.version)
2034         {
2035         case 0:            /* VC 4.0 */
2036         case 19960307:     /* VC 5.0 */
2037         case 19970606:     /* VC 6.0 */
2038         case 19990903:
2039             break;
2040         default:
2041             ERR("-Unknown symbol info version %ld %08lx\n", 
2042                 symbols.version, symbols.version);
2043         }
2044
2045         pdb_process_symbol_imports(pcs, msc_dbg, &symbols, symbols_image, image, pdb_lookup, module_index);
2046
2047         /* Read global symbol table */
2048         modimage = pdb_read_file(image, pdb_lookup, symbols.gsym_file);
2049         if (modimage)
2050         {
2051             codeview_snarf(msc_dbg, modimage, 0, 
2052                            pdb_get_file_size(pdb_lookup, symbols.gsym_file), NULL);
2053
2054             pdb_free(modimage);
2055         }
2056
2057         /* Read per-module symbol / linenumber tables */
2058         file = symbols_image + header_size;
2059         while (file - symbols_image < header_size + symbols.module_size)
2060         {
2061             PDB_SYMBOL_FILE_EX          sfile;
2062             const char*                 file_name;
2063             unsigned                    size;
2064
2065             HeapValidate(GetProcessHeap(), 0, NULL);
2066             pdb_convert_symbol_file(&symbols, &sfile, &size, file);
2067
2068             modimage = pdb_read_file(image, pdb_lookup, sfile.file);
2069             if (modimage)
2070             {
2071                 struct codeview_linetab*    linetab = NULL;
2072
2073                 if (sfile.lineno_size)
2074                     linetab = codeview_snarf_linetab(msc_dbg->module, 
2075                                                      modimage + sfile.symbol_size,
2076                                                      sfile.lineno_size,
2077                                                      pdb_lookup->kind == PDB_JG);
2078
2079                 if (sfile.symbol_size)
2080                     codeview_snarf(msc_dbg, modimage, sizeof(DWORD),
2081                                    sfile.symbol_size, linetab);
2082
2083                 pdb_free(modimage);
2084             }
2085             file_name = (const char*)file + size;
2086             file_name += strlen(file_name) + 1;
2087             file = (BYTE*)((DWORD)(file_name + strlen(file_name) + 1 + 3) & ~3);
2088         }
2089     }
2090     else
2091         pdb_process_symbol_imports(pcs, msc_dbg, NULL, NULL, image, pdb_lookup, 
2092                                    module_index);
2093     msc_dbg->module->module.SymType = SymCv;
2094     ret = TRUE;
2095
2096  leave:
2097     /* Cleanup */
2098     if (symbols_image) pdb_free(symbols_image);
2099     pdb_free_lookup(pdb_lookup);
2100
2101     if (image) UnmapViewOfFile(image);
2102     if (hMap) CloseHandle(hMap);
2103     if (hFile) CloseHandle(hFile);
2104
2105     return ret;
2106 }
2107
2108 static BOOL pdb_process_file(const struct process* pcs, 
2109                              const struct msc_debug_info* msc_dbg,
2110                              struct pdb_lookup* pdb_lookup)
2111 {
2112     BOOL        ret;
2113
2114     memset(cv_zmodules, 0, sizeof(cv_zmodules));
2115     codeview_init_basic_types(msc_dbg->module);
2116     ret = pdb_process_internal(pcs, msc_dbg, pdb_lookup, -1);
2117     codeview_clear_type_table();
2118     return ret;
2119 }
2120
2121 /*========================================================================
2122  * Process CodeView debug information.
2123  */
2124
2125 #define MAKESIG(a,b,c,d)        ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
2126 #define CODEVIEW_NB09_SIG       MAKESIG('N','B','0','9')
2127 #define CODEVIEW_NB10_SIG       MAKESIG('N','B','1','0')
2128 #define CODEVIEW_NB11_SIG       MAKESIG('N','B','1','1')
2129 #define CODEVIEW_RSDS_SIG       MAKESIG('R','S','D','S')
2130
2131 static BOOL codeview_process_info(const struct process* pcs, 
2132                                   const struct msc_debug_info* msc_dbg)
2133 {
2134     const CODEVIEW_HEADER_NBxx* cv = (const CODEVIEW_HEADER_NBxx*)msc_dbg->root;
2135     BOOL                        ret = FALSE;
2136     struct pdb_lookup           pdb_lookup;
2137
2138     TRACE("Processing signature %.4s\n", (const char*)&cv->dwSignature);
2139
2140     switch (cv->dwSignature)
2141     {
2142     case CODEVIEW_NB09_SIG:
2143     case CODEVIEW_NB11_SIG:
2144     {
2145         const CV_DIRECTORY_HEADER*      hdr = (const CV_DIRECTORY_HEADER*)(msc_dbg->root + cv->lfoDirectory);
2146         const CV_DIRECTORY_ENTRY*       ent;
2147         const CV_DIRECTORY_ENTRY*       prev;
2148         const CV_DIRECTORY_ENTRY*       next;
2149         unsigned int                    i;
2150
2151         codeview_init_basic_types(msc_dbg->module);
2152         ent = (const CV_DIRECTORY_ENTRY*)((const BYTE*)hdr + hdr->cbDirHeader);
2153         for (i = 0; i < hdr->cDir; i++, ent = next)
2154         {
2155             next = (i == hdr->cDir-1)? NULL :
2156                    (const CV_DIRECTORY_ENTRY*)((const BYTE*)ent + hdr->cbDirEntry);
2157             prev = (i == 0)? NULL :
2158                    (const CV_DIRECTORY_ENTRY*)((const BYTE*)ent - hdr->cbDirEntry);
2159
2160             if (ent->subsection == sstAlignSym)
2161             {
2162                 /*
2163                  * Check the next and previous entry.  If either is a
2164                  * sstSrcModule, it contains the line number info for
2165                  * this file.
2166                  *
2167                  * FIXME: This is not a general solution!
2168                  */
2169                 struct codeview_linetab*        linetab = NULL;
2170
2171                 if (next && next->iMod == ent->iMod && 
2172                     next->subsection == sstSrcModule)
2173                     linetab = codeview_snarf_linetab(msc_dbg->module, 
2174                                                      msc_dbg->root + next->lfo, next->cb, 
2175                                                      TRUE);
2176
2177                 if (prev && prev->iMod == ent->iMod &&
2178                     prev->subsection == sstSrcModule)
2179                     linetab = codeview_snarf_linetab(msc_dbg->module, 
2180                                                      msc_dbg->root + prev->lfo, prev->cb, 
2181                                                      TRUE);
2182
2183                 codeview_snarf(msc_dbg, msc_dbg->root + ent->lfo, sizeof(DWORD),
2184                                ent->cb, linetab);
2185             }
2186         }
2187
2188         msc_dbg->module->module.SymType = SymCv;
2189         ret = TRUE;
2190         break;
2191     }
2192
2193     case CODEVIEW_NB10_SIG:
2194     {
2195         const CODEVIEW_PDB_DATA* pdb = (const CODEVIEW_PDB_DATA*)(cv + 1);
2196         pdb_lookup.filename = pdb->name;
2197         pdb_lookup.kind = PDB_JG;
2198         pdb_lookup.u.jg.timestamp = pdb->timestamp;
2199         pdb_lookup.u.jg.toc = NULL;
2200         ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
2201         break;
2202     }
2203     case CODEVIEW_RSDS_SIG:
2204     {
2205         const CODEVIEW_HEADER_RSDS* rsds = (const CODEVIEW_HEADER_RSDS*)msc_dbg->root;
2206
2207         TRACE("Got RSDS type of PDB file: guid=%s unk=%08lx name=%s\n",
2208               wine_dbgstr_guid(&rsds->guid), rsds->unknown, rsds->name);
2209         pdb_lookup.filename = rsds->name;
2210         pdb_lookup.kind = PDB_DS;
2211         pdb_lookup.u.ds.guid = rsds->guid;
2212         pdb_lookup.u.ds.toc = NULL;
2213         ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
2214         break;
2215     }
2216     default:
2217         ERR("Unknown CODEVIEW signature %08lX in module %s\n",
2218             cv->dwSignature, msc_dbg->module->module.ModuleName);
2219         break;
2220     }
2221
2222     return ret;
2223 }
2224
2225 /*========================================================================
2226  * Process debug directory.
2227  */
2228 BOOL pe_load_debug_directory(const struct process* pcs, struct module* module, 
2229                              const BYTE* mapping,
2230                              const IMAGE_SECTION_HEADER* sectp, DWORD nsect,
2231                              const IMAGE_DEBUG_DIRECTORY* dbg, int nDbg)
2232 {
2233     BOOL                        ret;
2234     int                         i;
2235     struct msc_debug_info       msc_dbg;
2236
2237     msc_dbg.module = module;
2238     msc_dbg.nsect  = nsect;
2239     msc_dbg.sectp  = sectp;
2240     msc_dbg.nomap  = 0;
2241     msc_dbg.omapp  = NULL;
2242
2243     __TRY
2244     {
2245         ret = FALSE;
2246
2247         /* First, watch out for OMAP data */
2248         for (i = 0; i < nDbg; i++)
2249         {
2250             if (dbg[i].Type == IMAGE_DEBUG_TYPE_OMAP_FROM_SRC)
2251             {
2252                 msc_dbg.nomap = dbg[i].SizeOfData / sizeof(OMAP_DATA);
2253                 msc_dbg.omapp = (const OMAP_DATA*)(mapping + dbg[i].PointerToRawData);
2254                 break;
2255             }
2256         }
2257   
2258         /* Now, try to parse CodeView debug info */
2259         for (i = 0; i < nDbg; i++)
2260         {
2261             if (dbg[i].Type == IMAGE_DEBUG_TYPE_CODEVIEW)
2262             {
2263                 msc_dbg.root = mapping + dbg[i].PointerToRawData;
2264                 if ((ret = codeview_process_info(pcs, &msc_dbg))) goto done;
2265             }
2266         }
2267     
2268         /* If not found, try to parse COFF debug info */
2269         for (i = 0; i < nDbg; i++)
2270         {
2271             if (dbg[i].Type == IMAGE_DEBUG_TYPE_COFF)
2272             {
2273                 msc_dbg.root = mapping + dbg[i].PointerToRawData;
2274                 if ((ret = coff_process_info(&msc_dbg))) goto done;
2275             }
2276         }
2277     done:
2278          /* FIXME: this should be supported... this is the debug information for
2279           * functions compiled without a frame pointer (FPO = frame pointer omission)
2280           * the associated data helps finding out the relevant information
2281           */
2282         for (i = 0; i < nDbg; i++)
2283             if (dbg[i].Type == IMAGE_DEBUG_TYPE_FPO)
2284                 FIXME("This guy has FPO information\n");
2285 #if 0
2286
2287 #define FRAME_FPO   0
2288 #define FRAME_TRAP  1
2289 #define FRAME_TSS   2
2290
2291 typedef struct _FPO_DATA 
2292 {
2293         DWORD       ulOffStart;            /* offset 1st byte of function code */
2294         DWORD       cbProcSize;            /* # bytes in function */
2295         DWORD       cdwLocals;             /* # bytes in locals/4 */
2296         WORD        cdwParams;             /* # bytes in params/4 */
2297
2298         WORD        cbProlog : 8;          /* # bytes in prolog */
2299         WORD        cbRegs   : 3;          /* # regs saved */
2300         WORD        fHasSEH  : 1;          /* TRUE if SEH in func */
2301         WORD        fUseBP   : 1;          /* TRUE if EBP has been allocated */
2302         WORD        reserved : 1;          /* reserved for future use */
2303         WORD        cbFrame  : 2;          /* frame type */
2304 } FPO_DATA;
2305 #endif
2306
2307     }
2308     __EXCEPT_PAGE_FAULT
2309     {
2310         ERR("Got a page fault while loading symbols\n");
2311         ret = FALSE;
2312     }
2313     __ENDTRY
2314     return ret;
2315 }