wldap32: Added configure checks for some potentially missing functions.
[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 arr_len)
392 {
393     struct symt*        symt;
394     struct symt*        elem = codeview_get_type(elemtype, FALSE);
395     DWORD               arr_max = 0;
396
397     if (elem)
398     {
399         DWORD64 elem_size;
400         symt_get_info(elem, TI_GET_LENGTH, &elem_size);
401         if (elem_size) arr_max = arr_len / (DWORD)elem_size;
402     }
403     symt = &symt_new_array(module, 0, arr_max, elem)->symt;
404     return codeview_add_type(typeno, symt);
405 }
406
407 static int codeview_add_type_bitfield(unsigned int typeno, unsigned int bitoff,
408                                       unsigned int nbits, unsigned int basetype)
409 {
410     if (cv_current_module->used_bitfields >= cv_current_module->num_bitfields)
411     {
412         if (cv_current_module->bitfields)
413         {
414             cv_current_module->num_bitfields *= 2;
415             cv_current_module->bitfields = 
416                 HeapReAlloc(GetProcessHeap(), 0, 
417                             cv_current_module->bitfields, 
418                             cv_current_module->num_bitfields * sizeof(struct codeview_bitfield));
419         }
420         else
421         {
422             cv_current_module->num_bitfields = 64;
423             cv_current_module->bitfields = 
424                 HeapAlloc(GetProcessHeap(), 0, 
425                           cv_current_module->num_bitfields * sizeof(struct codeview_bitfield));
426         }
427         if (!cv_current_module->bitfields) return 0;
428     }
429     
430     cv_current_module->bitfields[cv_current_module->used_bitfields].symt.tag    = SymTagCVBitField;
431     cv_current_module->bitfields[cv_current_module->used_bitfields].subtype     = basetype;
432     cv_current_module->bitfields[cv_current_module->used_bitfields].bitposition = bitoff;
433     cv_current_module->bitfields[cv_current_module->used_bitfields].bitlength   = nbits;
434
435     return codeview_add_type(typeno, &cv_current_module->bitfields[cv_current_module->used_bitfields++].symt);
436 }
437
438 static int codeview_add_type_enum_field_list(struct module* module, 
439                                              unsigned int typeno, 
440                                              const unsigned char* list, int len)
441 {
442     struct symt_enum*           symt;
443     const unsigned char*        ptr = list;
444
445     symt = symt_new_enum(module, NULL);
446     while (ptr - list < len)
447     {
448         const union codeview_fieldtype* type = (const union codeview_fieldtype*)ptr;
449
450         if (*ptr >= 0xf0)       /* LF_PAD... */
451         {
452             ptr += *ptr & 0x0f;
453             continue;
454         }
455
456         switch (type->generic.id)
457         {
458         case LF_ENUMERATE_V1:
459         {
460             int value, vlen = numeric_leaf(&value, &type->enumerate_v1.value);
461             const struct p_string* p_name = (const struct p_string*)((const unsigned char*)&type->enumerate_v1.value + vlen);
462
463             symt_add_enum_element(module, symt, terminate_string(p_name), value);
464             ptr += 2 + 2 + vlen + (1 + p_name->namelen);
465             break;
466         }
467         case LF_ENUMERATE_V3:
468         {
469             int value, vlen = numeric_leaf(&value, &type->enumerate_v3.value);
470             const char* name = (const char*)&type->enumerate_v3.value + vlen;
471
472             symt_add_enum_element(module, symt, name, value);
473             ptr += 2 + 2 + vlen + (1 + strlen(name));
474             break;
475         }
476
477         default:
478             FIXME("Unsupported type %04x in ENUM field list\n", type->generic.id);
479             return FALSE;
480         }
481     }
482
483     return codeview_add_type(typeno, &symt->symt);
484 }
485
486 static int codeview_add_type_struct_field_list(struct module* module, 
487                                                unsigned int typeno, 
488                                                const unsigned char* list, int len)
489 {
490     struct symt_udt*            symt;
491     const unsigned char*        ptr = list;
492     int                         value, leaf_len;
493     const struct p_string*      p_name;
494     const char*                 c_name;
495     struct symt*                subtype;
496
497     symt = symt_new_udt(module, NULL, 0, UdtStruct /* don't care */);
498     while (ptr - list < len)
499     {
500         const union codeview_fieldtype* type = (const union codeview_fieldtype*)ptr;
501
502         if (*ptr >= 0xf0)       /* LF_PAD... */
503         {
504             ptr +=* ptr & 0x0f;
505             continue;
506         }
507
508         switch (type->generic.id)
509         {
510         case LF_BCLASS_V1:
511             leaf_len = numeric_leaf(&value, &type->bclass_v1.offset);
512
513             /* FIXME: ignored for now */
514
515             ptr += 2 + 2 + 2 + leaf_len;
516             break;
517
518         case LF_BCLASS_V2:
519             leaf_len = numeric_leaf(&value, &type->bclass_v2.offset);
520
521             /* FIXME: ignored for now */
522
523             ptr += 2 + 2 + 4 + leaf_len;
524             break;
525
526         case LF_VBCLASS_V1:
527         case LF_IVBCLASS_V1:
528             {
529                 const unsigned short int* p_vboff;
530                 int vpoff, vplen;
531                 leaf_len = numeric_leaf(&value, &type->vbclass_v1.vbpoff);
532                 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v1.vbpoff + leaf_len);
533                 vplen = numeric_leaf(&vpoff, p_vboff);
534
535                 /* FIXME: ignored for now */
536
537                 ptr += 2 + 2 + 2 + 2 + leaf_len + vplen;
538             }
539             break;
540
541         case LF_VBCLASS_V2:
542         case LF_IVBCLASS_V2:
543             {
544                 const unsigned short int* p_vboff;
545                 int vpoff, vplen;
546                 leaf_len = numeric_leaf(&value, &type->vbclass_v2.vbpoff);
547                 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v2.vbpoff + leaf_len);
548                 vplen = numeric_leaf(&vpoff, p_vboff);
549
550                 /* FIXME: ignored for now */
551
552                 ptr += 2 + 2 + 4 + 4 + leaf_len + vplen;
553             }
554             break;
555
556         case LF_MEMBER_V1:
557             leaf_len = numeric_leaf(&value, &type->member_v1.offset);
558             p_name = (const struct p_string*)((const char*)&type->member_v1.offset + leaf_len);
559             subtype = codeview_get_type(type->member_v1.type, TRUE);
560
561             if (!subtype || subtype->tag != SymTagCVBitField)
562             {
563                 DWORD64 elem_size = 0;
564                 if (subtype) symt_get_info(subtype, TI_GET_LENGTH, &elem_size);
565                 symt_add_udt_element(module, symt, terminate_string(p_name),
566                                      codeview_get_type(type->member_v1.type, TRUE),
567                                      value << 3, (DWORD)elem_size << 3);
568             }
569             else
570             {
571                 struct codeview_bitfield* cvbf = (struct codeview_bitfield*)subtype;
572                 symt_add_udt_element(module, symt, terminate_string(p_name),
573                                      codeview_get_type(cvbf->subtype, FALSE),
574                                      cvbf->bitposition, cvbf->bitlength);
575             }
576
577             ptr += 2 + 2 + 2 + leaf_len + (1 + p_name->namelen);
578             break;
579
580         case LF_MEMBER_V2:
581             leaf_len = numeric_leaf(&value, &type->member_v2.offset);
582             p_name = (const struct p_string*)((const unsigned char*)&type->member_v2.offset + leaf_len);
583             subtype = codeview_get_type(type->member_v2.type, TRUE);
584
585             if (!subtype || subtype->tag != SymTagCVBitField)
586             {
587                 DWORD64 elem_size = 0;
588                 if (subtype) symt_get_info(subtype, TI_GET_LENGTH, &elem_size);
589                 symt_add_udt_element(module, symt, terminate_string(p_name),
590                                      subtype, value << 3, (DWORD)elem_size << 3);
591             }
592             else
593             {
594                 struct codeview_bitfield* cvbf = (struct codeview_bitfield*)subtype;
595                 symt_add_udt_element(module, symt, terminate_string(p_name),
596                                      codeview_get_type(cvbf->subtype, FALSE),
597                                      cvbf->bitposition, cvbf->bitlength);
598             }
599
600             ptr += 2 + 2 + 4 + leaf_len + (1 + p_name->namelen);
601             break;
602
603         case LF_MEMBER_V3:
604             leaf_len = numeric_leaf(&value, &type->member_v3.offset);
605             c_name = (const char*)&type->member_v3.offset + leaf_len;
606             subtype = codeview_get_type(type->member_v3.type, TRUE);
607
608             if (!subtype || subtype->tag != SymTagCVBitField)
609             {
610                 DWORD64 elem_size = 0;
611                 if (subtype) symt_get_info(subtype, TI_GET_LENGTH, &elem_size);
612                 symt_add_udt_element(module, symt, c_name,
613                                      subtype, value << 3, (DWORD)elem_size << 3);
614             }
615             else
616             {
617                 struct codeview_bitfield* cvbf = (struct codeview_bitfield*)subtype;
618                 symt_add_udt_element(module, symt, c_name,
619                                      codeview_get_type(cvbf->subtype, FALSE),
620                                      cvbf->bitposition, cvbf->bitlength);
621             }
622
623             ptr += 2 + 2 + 4 + leaf_len + (strlen(c_name) + 1);
624             break;
625
626         case LF_STMEMBER_V1:
627             /* FIXME: ignored for now */
628             ptr += 2 + 2 + 2 + (1 + type->stmember_v1.p_name.namelen);
629             break;
630
631         case LF_STMEMBER_V2:
632             /* FIXME: ignored for now */
633             ptr += 2 + 4 + 2 + (1 + type->stmember_v2.p_name.namelen);
634             break;
635
636         case LF_METHOD_V1:
637             /* FIXME: ignored for now */
638             ptr += 2 + 2 + 2 + (1 + type->method_v1.p_name.namelen);
639             break;
640
641         case LF_METHOD_V2:
642             /* FIXME: ignored for now */
643             ptr += 2 + 2 + 4 + (1 + type->method_v2.p_name.namelen);
644             break;
645
646         case LF_NESTTYPE_V1:
647             /* FIXME: ignored for now */
648             ptr += 2 + 2 + (1 + type->nesttype_v1.p_name.namelen);
649             break;
650
651         case LF_NESTTYPE_V2:
652             /* FIXME: ignored for now */
653             ptr += 2 + 2 + 4 + (1 + type->nesttype_v2.p_name.namelen);
654             break;
655
656         case LF_VFUNCTAB_V1:
657             /* FIXME: ignored for now */
658             ptr += 2 + 2;
659             break;
660
661         case LF_VFUNCTAB_V2:
662             /* FIXME: ignored for now */
663             ptr += 2 + 2 + 4;
664             break;
665
666         case LF_ONEMETHOD_V1:
667             /* FIXME: ignored for now */
668             switch ((type->onemethod_v1.attribute >> 2) & 7)
669             {
670             case 4: case 6: /* (pure) introducing virtual method */
671                 ptr += 2 + 2 + 2 + 4 + (1 + type->onemethod_virt_v1.p_name.namelen);
672                 break;
673
674             default:
675                 ptr += 2 + 2 + 2 + (1 + type->onemethod_v1.p_name.namelen);
676                 break;
677             }
678             break;
679
680         case LF_ONEMETHOD_V2:
681             /* FIXME: ignored for now */
682             switch ((type->onemethod_v2.attribute >> 2) & 7)
683             {
684             case 4: case 6: /* (pure) introducing virtual method */
685                 ptr += 2 + 2 + 4 + 4 + (1 + type->onemethod_virt_v2.p_name.namelen);
686                 break;
687
688             default:
689                 ptr += 2 + 2 + 4 + (1 + type->onemethod_v2.p_name.namelen);
690                 break;
691             }
692             break;
693
694         default:
695             FIXME("Unsupported type %04x in STRUCT field list\n", type->generic.id);
696             return FALSE;
697         }
698     }
699
700     return codeview_add_type(typeno, &symt->symt);
701 }
702
703 static int codeview_add_type_enum(struct module* module, unsigned int typeno, 
704                                   const char* name, unsigned int fieldlist)
705 {
706     struct symt_enum*   symt = symt_new_enum(module, name);
707     struct symt*        list = codeview_get_type(fieldlist, FALSE);
708
709     /* FIXME: this is rather ugly !!! */
710     if (list) symt->vchildren = ((struct symt_enum*)list)->vchildren;
711
712     return codeview_add_type(typeno, &symt->symt);
713 }
714
715 static int codeview_add_type_struct(struct module* module, unsigned int typeno, 
716                                     const char* name, int structlen, 
717                                     unsigned int fieldlist, enum UdtKind kind)
718 {
719     struct symt_udt*    symt = symt_new_udt(module, name, structlen, kind);
720     struct symt*        list = codeview_get_type(fieldlist, FALSE);
721
722     /* FIXME: this is rather ugly !!! */
723     if (list) symt->vchildren = ((struct symt_udt*)list)->vchildren;
724
725     return codeview_add_type(typeno, &symt->symt);
726 }
727
728 static int codeview_new_func_signature(struct module* module, unsigned typeno,
729                                        unsigned ret_type, enum CV_call_e call_conv)
730 {
731     struct symt* symt;
732     symt = &symt_new_function_signature(module, 
733                                         codeview_get_type(ret_type, FALSE),
734                                         call_conv)->symt;
735     return codeview_add_type(typeno, symt);
736 }
737
738 static int codeview_parse_type_table(struct module* module, const BYTE* table,
739                                      int len)
740 {
741     unsigned int                curr_type = 0x1000;
742     const BYTE*                 ptr = table;
743     int                         retv;
744     const union codeview_type*  type;
745     int                         value, leaf_len;
746     const struct p_string*      p_name;
747     const char*                 c_name;
748
749     while (ptr - table < len)
750     {
751         retv = TRUE;
752         type = (const union codeview_type*)ptr;
753
754         switch (type->generic.id)
755         {
756         case LF_MODIFIER_V1:
757             /* FIXME: we don't handle modifiers, 
758              * but readd previous type on the curr_type 
759              */
760             WARN("Modifier on %x: %s%s%s%s\n",
761                  type->modifier_v1.type,
762                  type->modifier_v1.attribute & 0x01 ? "const " : "",
763                  type->modifier_v1.attribute & 0x02 ? "volatile " : "",
764                  type->modifier_v1.attribute & 0x04 ? "unaligned " : "",
765                  type->modifier_v1.attribute & ~0x07 ? "unknown " : "");
766             codeview_add_type(curr_type, 
767                               codeview_get_type(type->modifier_v1.type, FALSE));
768             break;
769         case LF_MODIFIER_V2:
770             /* FIXME: we don't handle modifiers, but readd previous type on the curr_type */
771             WARN("Modifier on %x: %s%s%s%s\n",
772                  type->modifier_v2.type,
773                  type->modifier_v2.attribute & 0x01 ? "const " : "",
774                  type->modifier_v2.attribute & 0x02 ? "volatile " : "",
775                  type->modifier_v2.attribute & 0x04 ? "unaligned " : "",
776                  type->modifier_v2.attribute & ~0x07 ? "unknown " : "");
777             codeview_add_type(curr_type, 
778                               codeview_get_type(type->modifier_v2.type, FALSE));
779             break;
780
781         case LF_POINTER_V1:
782             retv = codeview_add_type_pointer(module, curr_type, 
783                                              type->pointer_v1.datatype);
784             break;
785         case LF_POINTER_V2:
786             retv = codeview_add_type_pointer(module, curr_type, 
787                                              type->pointer_v2.datatype);
788             break;
789
790         case LF_ARRAY_V1:
791             leaf_len = numeric_leaf(&value, &type->array_v1.arrlen);
792             p_name = (const struct p_string*)((const unsigned char*)&type->array_v1.arrlen + leaf_len);
793
794             retv = codeview_add_type_array(module, curr_type, terminate_string(p_name),
795                                            type->array_v1.elemtype, value);
796             break;
797         case LF_ARRAY_V2:
798             leaf_len = numeric_leaf(&value, &type->array_v2.arrlen);
799             p_name = (const struct p_string*)((const unsigned char*)&type->array_v2.arrlen + leaf_len);
800
801             retv = codeview_add_type_array(module, curr_type, terminate_string(p_name),
802                                            type->array_v2.elemtype, value);
803             break;
804         case LF_ARRAY_V3:
805             leaf_len = numeric_leaf(&value, &type->array_v3.arrlen);
806             c_name = (const char*)&type->array_v3.arrlen + leaf_len;
807
808             retv = codeview_add_type_array(module, curr_type, c_name,
809                                            type->array_v3.elemtype, value);
810             break;
811
812         case LF_BITFIELD_V1:
813             /* a bitfield is a CodeView specific data type which represent a bitfield
814              * in a structure or a class. For now, we store it in a SymTag-like type
815              * (so that the rest of the process is seamless), but check at udt 
816              * inclusion type for its presence
817              */
818             retv = codeview_add_type_bitfield(curr_type, type->bitfield_v1.bitoff,
819                                               type->bitfield_v1.nbits,
820                                               type->bitfield_v1.type);
821             break;
822         case LF_BITFIELD_V2:
823             retv = codeview_add_type_bitfield(curr_type, type->bitfield_v2.bitoff,
824                                               type->bitfield_v2.nbits,
825                                               type->bitfield_v2.type);
826             break;
827         case LF_FIELDLIST_V1:
828         case LF_FIELDLIST_V2:
829            {
830                /*
831                 * A 'field list' is a CodeView-specific data type which doesn't
832                 * directly correspond to any high-level data type.  It is used
833                 * to hold the collection of members of a struct, class, union
834                 * or enum type.  The actual definition of that type will follow
835                 * later, and refer to the field list definition record.
836                 *
837                 * As we don't have a field list type ourselves, we look ahead
838                 * in the field list to try to find out whether this field list
839                 * will be used for an enum or struct type, and create a dummy
840                 * type of the corresponding sort.  Later on, the definition of
841                 * the 'real' type will copy the member / enumeration data.
842                 */
843                const unsigned char* list = type->fieldlist.list;
844                int   len  = (ptr + type->generic.len + 2) - list;
845
846                if (((const union codeview_fieldtype*)list)->generic.id == LF_ENUMERATE_V1 ||
847                    ((const union codeview_fieldtype*)list)->generic.id == LF_ENUMERATE_V3)
848                    retv = codeview_add_type_enum_field_list(module, curr_type, list, len);
849                else
850                    retv = codeview_add_type_struct_field_list(module, curr_type, list, len);
851            }
852            break;
853
854         case LF_STRUCTURE_V1:
855         case LF_CLASS_V1:
856             leaf_len = numeric_leaf(&value, &type->struct_v1.structlen);
857             p_name = (const struct p_string*)((const unsigned char*)&type->struct_v1.structlen + leaf_len);
858
859             retv = codeview_add_type_struct(module, curr_type, terminate_string(p_name),
860                                             value, type->struct_v1.fieldlist,
861                                             type->generic.id == LF_CLASS_V1 ? UdtClass : UdtStruct);
862             break;
863
864         case LF_STRUCTURE_V2:
865         case LF_CLASS_V2:
866             leaf_len = numeric_leaf(&value, &type->struct_v2.structlen);
867             p_name = (const struct p_string*)((const unsigned char*)&type->struct_v2.structlen + leaf_len);
868
869             retv = codeview_add_type_struct(module, curr_type, terminate_string(p_name),
870                                             value, type->struct_v2.fieldlist,
871                                             type->generic.id == LF_CLASS_V2 ? UdtClass : UdtStruct);
872             break;
873
874         case LF_STRUCTURE_V3:
875         case LF_CLASS_V3:
876             leaf_len = numeric_leaf(&value, &type->struct_v3.structlen);
877             c_name = (const char*)&type->struct_v3.structlen + leaf_len;
878
879             retv = codeview_add_type_struct(module, curr_type, c_name,
880                                             value, type->struct_v3.fieldlist,
881                                             type->generic.id == LF_CLASS_V3 ? UdtClass : UdtStruct);
882             break;
883
884         case LF_UNION_V1:
885             leaf_len = numeric_leaf(&value, &type->union_v1.un_len);
886             p_name = (const struct p_string*)((const unsigned char*)&type->union_v1.un_len + leaf_len);
887
888             retv = codeview_add_type_struct(module, curr_type, terminate_string(p_name),
889                                             value, type->union_v1.fieldlist, UdtUnion);
890             break;
891         case LF_UNION_V2:
892             leaf_len = numeric_leaf(&value, &type->union_v2.un_len);
893             p_name = (const struct p_string*)((const unsigned char*)&type->union_v2.un_len + leaf_len);
894
895             retv = codeview_add_type_struct(module, curr_type, terminate_string(p_name),
896                                             value, type->union_v2.fieldlist, UdtUnion);
897             break;
898         case LF_UNION_V3:
899             leaf_len = numeric_leaf(&value, &type->union_v3.un_len);
900             c_name = (const char*)&type->union_v3.un_len + leaf_len;
901
902             retv = codeview_add_type_struct(module, curr_type, c_name,
903                                             value, type->union_v3.fieldlist, UdtUnion);
904
905         case LF_ENUM_V1:
906             retv = codeview_add_type_enum(module, curr_type, terminate_string(&type->enumeration_v1.p_name),
907                                           type->enumeration_v1.field);
908             break;
909
910         case LF_ENUM_V2:
911             retv = codeview_add_type_enum(module, curr_type, terminate_string(&type->enumeration_v2.p_name),
912                                           type->enumeration_v2.field);
913             break;
914         case LF_ENUM_V3:
915             retv = codeview_add_type_enum(module, curr_type, type->enumeration_v3.name,
916                                           type->enumeration_v3.field);
917             break;
918         case LF_PROCEDURE_V1:
919             retv = codeview_new_func_signature(module, curr_type, 
920                                                type->procedure_v1.rvtype,
921                                                type->procedure_v1.call);
922             break;
923         case LF_PROCEDURE_V2:
924             retv = codeview_new_func_signature(module, curr_type, 
925                                                type->procedure_v2.rvtype,
926                                                type->procedure_v2.call);
927             break;
928         case LF_MFUNCTION_V1:
929             /* FIXME: for C++, this is plain wrong, but as we don't use arg types
930              * nor class information, this would just do for now
931              */
932             retv = codeview_new_func_signature(module, curr_type,
933                                                type->mfunction_v1.rvtype,
934                                                type->mfunction_v1.call);
935             break;
936         case LF_MFUNCTION_V2:
937             /* FIXME: for C++, this is plain wrong, but as we don't use arg types
938              * nor class information, this would just do for now
939              */
940             retv = codeview_new_func_signature(module, curr_type,
941                                                type->mfunction_v2.rvtype,
942                                                type->mfunction_v2.call);
943             break;
944         case LF_ARGLIST_V1:
945         case LF_ARGLIST_V2:
946             {
947                 static int once;
948                 if (!once++) 
949                     FIXME("Not adding parameters' types to function signature\n");
950             }
951             break;
952
953         default:
954             FIXME("Unsupported type-id leaf %x\n", type->generic.id);
955             dump(type, 2 + type->generic.len);
956             break;
957         }
958         if (!retv) return FALSE;
959         curr_type++;
960         ptr += type->generic.len + 2;
961     }
962
963     return TRUE;
964 }
965
966 /*========================================================================
967  * Process CodeView line number information.
968  */
969
970 static struct codeview_linetab* codeview_snarf_linetab(struct module* module, 
971                                                        const BYTE* linetab, int size,
972                                                        BOOL pascal_str)
973 {
974     int                         file_segcount;
975     char                        filename[PATH_MAX];
976     const unsigned int*         filetab;
977     const struct p_string*      p_fn;
978     int                         i;
979     int                         k;
980     struct codeview_linetab*    lt_hdr;
981     const unsigned int*         lt_ptr;
982     int                         nfile;
983     int                         nseg;
984     union any_size              pnt;
985     union any_size              pnt2;
986     const struct startend*      start;
987     int                         this_seg;
988     struct symt_compiland*      compiland;
989
990     /*
991      * Now get the important bits.
992      */
993     pnt.uc = linetab;
994     nfile = *pnt.s++;
995     nseg = *pnt.s++;
996
997     filetab = (const unsigned int*) pnt.c;
998
999     /*
1000      * Now count up the number of segments in the file.
1001      */
1002     nseg = 0;
1003     for (i = 0; i < nfile; i++)
1004     {
1005         pnt2.uc = linetab + filetab[i];
1006         nseg += *pnt2.s;
1007     }
1008
1009     /*
1010      * Next allocate the header we will be returning.
1011      * There is one header for each segment, so that we can reach in
1012      * and pull bits as required.
1013      */
1014     lt_hdr = (struct codeview_linetab*)
1015         HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nseg + 1) * sizeof(*lt_hdr));
1016     if (lt_hdr == NULL)
1017     {
1018         goto leave;
1019     }
1020
1021     /*
1022      * Now fill the header we will be returning, one for each segment.
1023      * Note that this will basically just contain pointers into the existing
1024      * line table, and we do not actually copy any additional information
1025      * or allocate any additional memory.
1026      */
1027
1028     this_seg = 0;
1029     for (i = 0; i < nfile; i++)
1030     {
1031         /*
1032          * Get the pointer into the segment information.
1033          */
1034         pnt2.uc = linetab + filetab[i];
1035         file_segcount = *pnt2.s;
1036
1037         pnt2.ui++;
1038         lt_ptr = (const unsigned int*) pnt2.c;
1039         start = (const struct startend*)(lt_ptr + file_segcount);
1040
1041         /*
1042          * Now snarf the filename for all of the segments for this file.
1043          */
1044         if (pascal_str)
1045         {
1046             p_fn = (const struct p_string*)(start + file_segcount);
1047             memset(filename, 0, sizeof(filename));
1048             memcpy(filename, p_fn->name, p_fn->namelen);
1049             compiland = symt_new_compiland(module, filename);
1050         }
1051         else
1052             compiland = symt_new_compiland(module, (const char*)(start + file_segcount));
1053         
1054         for (k = 0; k < file_segcount; k++, this_seg++)
1055         {
1056             pnt2.uc = linetab + lt_ptr[k];
1057             lt_hdr[this_seg].start      = start[k].start;
1058             lt_hdr[this_seg].end        = start[k].end;
1059             lt_hdr[this_seg].compiland  = compiland;
1060             lt_hdr[this_seg].segno      = *pnt2.s++;
1061             lt_hdr[this_seg].nline      = *pnt2.s++;
1062             lt_hdr[this_seg].offtab     = pnt2.ui;
1063             lt_hdr[this_seg].linetab    = (const unsigned short*)(pnt2.ui + lt_hdr[this_seg].nline);
1064         }
1065     }
1066
1067 leave:
1068
1069   return lt_hdr;
1070
1071 }
1072
1073 /*========================================================================
1074  * Process CodeView symbol information.
1075  */
1076
1077 static unsigned int codeview_map_offset(const struct msc_debug_info* msc_dbg,
1078                                         unsigned int offset)
1079 {
1080     int                 nomap = msc_dbg->nomap;
1081     const OMAP_DATA*    omapp = msc_dbg->omapp;
1082     int                 i;
1083
1084     if (!nomap || !omapp) return offset;
1085
1086     /* FIXME: use binary search */
1087     for (i = 0; i < nomap - 1; i++)
1088         if (omapp[i].from <= offset && omapp[i+1].from > offset)
1089             return !omapp[i].to ? 0 : omapp[i].to + (offset - omapp[i].from);
1090
1091     return 0;
1092 }
1093
1094 static const struct codeview_linetab*
1095 codeview_get_linetab(const struct codeview_linetab* linetab,
1096                      unsigned seg, unsigned offset)
1097 {
1098     /*
1099      * Check whether we have line number information
1100      */
1101     if (linetab)
1102     {
1103         for (; linetab->linetab; linetab++)
1104             if (linetab->segno == seg &&
1105                 linetab->start <= offset && linetab->end   >  offset)
1106                 break;
1107         if (!linetab->linetab) linetab = NULL;
1108     }
1109     return linetab;
1110 }
1111
1112 static unsigned codeview_get_address(const struct msc_debug_info* msc_dbg, 
1113                                      unsigned seg, unsigned offset)
1114 {
1115     int                         nsect = msc_dbg->nsect;
1116     const IMAGE_SECTION_HEADER* sectp = msc_dbg->sectp;
1117
1118     if (!seg || seg > nsect) return 0;
1119     return msc_dbg->module->module.BaseOfImage +
1120         codeview_map_offset(msc_dbg, sectp[seg-1].VirtualAddress + offset);
1121 }
1122
1123 static void codeview_add_func_linenum(struct module* module, 
1124                                       struct symt_function* func,
1125                                       const struct codeview_linetab* linetab,
1126                                       unsigned offset, unsigned size)
1127 {
1128     unsigned int        i;
1129
1130     if (!linetab) return;
1131     for (i = 0; i < linetab->nline; i++)
1132     {
1133         if (linetab->offtab[i] >= offset && linetab->offtab[i] < offset + size)
1134         {
1135             symt_add_func_line(module, func, linetab->compiland->source,
1136                                linetab->linetab[i], linetab->offtab[i] - offset);
1137         }
1138     }
1139 }
1140
1141 static int codeview_snarf(const struct msc_debug_info* msc_dbg, const BYTE* root, 
1142                           int offset, int size,
1143                           struct codeview_linetab* linetab)
1144 {
1145     struct symt_function*               curr_func = NULL;
1146     int                                 i, length;
1147     const struct codeview_linetab*      flt;
1148     struct symt_block*                  block = NULL;
1149     struct symt*                        symt;
1150     const char*                         name;
1151
1152     /*
1153      * Loop over the different types of records and whenever we
1154      * find something we are interested in, record it and move on.
1155      */
1156     for (i = offset; i < size; i += length)
1157     {
1158         const union codeview_symbol* sym = (const union codeview_symbol*)(root + i);
1159         length = sym->generic.len + 2;
1160         if (i + length > size) break;
1161         if (length & 3) FIXME("unpadded len %u\n", length);
1162
1163         switch (sym->generic.id)
1164         {
1165         /*
1166          * Global and local data symbols.  We don't associate these
1167          * with any given source file.
1168          */
1169         case S_GDATA_V1:
1170         case S_LDATA_V1:
1171             flt = codeview_get_linetab(linetab, sym->data_v1.segment, sym->data_v1.offset);
1172             symt_new_global_variable(msc_dbg->module, 
1173                                      flt ? flt->compiland : NULL,
1174                                      terminate_string(&sym->data_v1.p_name), sym->generic.id == S_LDATA_V1,
1175                                      codeview_get_address(msc_dbg, sym->data_v1.segment, sym->data_v1.offset),
1176                                      0,
1177                                      codeview_get_type(sym->data_v1.symtype, FALSE));
1178             break;
1179         case S_GDATA_V2:
1180         case S_LDATA_V2:
1181             flt = codeview_get_linetab(linetab, sym->data_v2.segment, sym->data_v2.offset);
1182             name = terminate_string(&sym->data_v2.p_name);
1183             if (name)
1184                 symt_new_global_variable(msc_dbg->module, flt ? flt->compiland : NULL,
1185                                          name, sym->generic.id == S_LDATA_V2,
1186                                          codeview_get_address(msc_dbg, sym->data_v2.segment, sym->data_v2.offset),
1187                                          0,
1188                                          codeview_get_type(sym->data_v2.symtype, FALSE));
1189             break;
1190         case S_GDATA_V3:
1191         case S_LDATA_V3:
1192             flt = codeview_get_linetab(linetab, sym->data_v3.segment, sym->data_v3.offset);
1193             if (*sym->data_v3.name)
1194                 symt_new_global_variable(msc_dbg->module, flt ? flt->compiland : NULL,
1195                                          sym->data_v3.name,
1196                                          sym->generic.id == S_LDATA_V3,
1197                                          codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset),
1198                                          0,
1199                                          codeview_get_type(sym->data_v3.symtype, FALSE));
1200             break;
1201
1202         case S_PUB_V1: /* FIXME is this really a 'data_v1' structure ?? */
1203             if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1204             {
1205                 flt = codeview_get_linetab(linetab, sym->data_v1.segment, sym->data_v1.offset);
1206                 symt_new_public(msc_dbg->module, flt ? flt->compiland : NULL,
1207                                 terminate_string(&sym->data_v1.p_name), 
1208                                 codeview_get_address(msc_dbg, sym->data_v1.segment, sym->data_v1.offset),
1209                                 0, TRUE /* FIXME */, TRUE /* FIXME */);
1210             }
1211             break;
1212         case S_PUB_V2: /* FIXME is this really a 'data_v2' structure ?? */
1213             if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1214             {
1215                 flt = codeview_get_linetab(linetab, sym->data_v2.segment, sym->data_v2.offset);
1216                 symt_new_public(msc_dbg->module, flt ? flt->compiland : NULL,
1217                                 terminate_string(&sym->data_v2.p_name), 
1218                                 codeview_get_address(msc_dbg, sym->data_v2.segment, sym->data_v2.offset),
1219                                 0, TRUE /* FIXME */, TRUE /* FIXME */);
1220             }
1221             break;
1222
1223         /*
1224          * Sort of like a global function, but it just points
1225          * to a thunk, which is a stupid name for what amounts to
1226          * a PLT slot in the normal jargon that everyone else uses.
1227          */
1228         case S_THUNK_V1:
1229             flt = codeview_get_linetab(linetab, sym->thunk_v1.segment, sym->thunk_v1.offset);
1230             symt_new_thunk(msc_dbg->module, flt ? flt->compiland : NULL,
1231                            terminate_string(&sym->thunk_v1.p_name), sym->thunk_v1.thtype,
1232                            codeview_get_address(msc_dbg, sym->thunk_v1.segment, sym->thunk_v1.offset),
1233                            sym->thunk_v1.thunk_len);
1234             break;
1235         case S_THUNK_V3:
1236             flt = codeview_get_linetab(linetab, sym->thunk_v3.segment, sym->thunk_v3.offset);
1237             symt_new_thunk(msc_dbg->module, flt ? flt->compiland : NULL,
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,
1251                                           flt ? flt->compiland : NULL,
1252                                           terminate_string(&sym->proc_v1.p_name),
1253                                           codeview_get_address(msc_dbg, sym->proc_v1.segment, sym->proc_v1.offset),
1254                                           sym->proc_v1.proc_len,
1255                                           codeview_get_type(sym->proc_v1.proctype, FALSE));
1256             codeview_add_func_linenum(msc_dbg->module, curr_func, flt, 
1257                                       sym->proc_v1.offset, sym->proc_v1.proc_len);
1258             symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, sym->proc_v1.debug_start, NULL);
1259             symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, sym->proc_v1.debug_end, NULL);
1260             break;
1261         case S_GPROC_V2:
1262         case S_LPROC_V2:
1263             flt = codeview_get_linetab(linetab, sym->proc_v2.segment, sym->proc_v2.offset);
1264             if (curr_func) FIXME("nested function\n");
1265             curr_func = symt_new_function(msc_dbg->module, 
1266                                           flt ? flt->compiland : NULL,
1267                                           terminate_string(&sym->proc_v2.p_name),
1268                                           codeview_get_address(msc_dbg, sym->proc_v2.segment, sym->proc_v2.offset),
1269                                           sym->proc_v2.proc_len,
1270                                           codeview_get_type(sym->proc_v2.proctype, FALSE));
1271             codeview_add_func_linenum(msc_dbg->module, curr_func, flt, 
1272                                       sym->proc_v2.offset, sym->proc_v2.proc_len);
1273             symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, sym->proc_v2.debug_start, NULL);
1274             symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, sym->proc_v2.debug_end, NULL);
1275             break;
1276         case S_GPROC_V3:
1277         case S_LPROC_V3:
1278             flt = codeview_get_linetab(linetab, sym->proc_v3.segment, sym->proc_v3.offset);
1279             if (curr_func) FIXME("nested function\n");
1280             curr_func = symt_new_function(msc_dbg->module, 
1281                                           flt ? flt->compiland : NULL,
1282                                           sym->proc_v3.name,
1283                                           codeview_get_address(msc_dbg, sym->proc_v3.segment, sym->proc_v3.offset),
1284                                           sym->proc_v3.proc_len,
1285                                           codeview_get_type(sym->proc_v3.proctype, FALSE));
1286             codeview_add_func_linenum(msc_dbg->module, curr_func, flt, 
1287                                       sym->proc_v3.offset, sym->proc_v3.proc_len);
1288             symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, sym->proc_v3.debug_start, NULL);
1289             symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, sym->proc_v3.debug_end, NULL);
1290             break;
1291         /*
1292          * Function parameters and stack variables.
1293          */
1294         case S_BPREL_V1:
1295             symt_add_func_local(msc_dbg->module, curr_func, 0, sym->stack_v1.offset,
1296                                 block, codeview_get_type(sym->stack_v1.symtype, FALSE),
1297                                 terminate_string(&sym->stack_v1.p_name));
1298             break;
1299         case S_BPREL_V2:
1300             symt_add_func_local(msc_dbg->module, curr_func, 0, sym->stack_v2.offset,
1301                                 block, codeview_get_type(sym->stack_v2.symtype, FALSE),
1302                                 terminate_string(&sym->stack_v2.p_name));
1303             break;
1304         case S_BPREL_V3:
1305             symt_add_func_local(msc_dbg->module, curr_func, 0, sym->stack_v3.offset,
1306                                 block, codeview_get_type(sym->stack_v3.symtype, FALSE),
1307                                 sym->stack_v3.name);
1308             break;
1309
1310         case S_REGISTER_V1:
1311             symt_add_func_local(msc_dbg->module, curr_func, 0, sym->register_v1.reg,
1312                                 block, codeview_get_type(sym->register_v1.type, FALSE),
1313                                 terminate_string(&sym->register_v1.p_name));
1314             break;
1315         case S_REGISTER_V2:
1316             symt_add_func_local(msc_dbg->module, curr_func, 0, sym->register_v2.reg,
1317                                 block, codeview_get_type(sym->register_v2.type, FALSE),
1318                                 terminate_string(&sym->register_v2.p_name));
1319             break;
1320
1321         case S_BLOCK_V1:
1322             block = symt_open_func_block(msc_dbg->module, curr_func, block, 
1323                                          codeview_get_address(msc_dbg, sym->block_v1.segment, sym->block_v1.offset),
1324                                          sym->block_v1.length);
1325             break;
1326         case S_BLOCK_V3:
1327             block = symt_open_func_block(msc_dbg->module, curr_func, block, 
1328                                          codeview_get_address(msc_dbg, sym->block_v3.segment, sym->block_v3.offset),
1329                                          sym->block_v3.length);
1330             break;
1331
1332         case S_END_V1:
1333             if (block)
1334             {
1335                 block = symt_close_func_block(msc_dbg->module, curr_func, block, 0);
1336             }
1337             else if (curr_func)
1338             {
1339                 symt_normalize_function(msc_dbg->module, curr_func);
1340                 curr_func = NULL;
1341             }
1342             break;
1343
1344         /* FIXME: we should use this as a compiland, instead of guessing it on the fly */
1345         case S_COMPILAND_V1:
1346             TRACE("S-Compiland-V1e %x %s\n", 
1347                   sym->compiland_v1.unknown, 
1348                   terminate_string(&sym->compiland_v1.p_name));
1349             break;
1350
1351         case S_COMPILAND_V2:
1352             TRACE("S-Compiland-V2 %s\n", 
1353                   terminate_string(&sym->compiland_v2.p_name));
1354             if (TRACE_ON(dbghelp_msc))
1355             {
1356                 const char* ptr1 = sym->compiland_v2.p_name.name + sym->compiland_v2.p_name.namelen;
1357                 const char* ptr2;
1358                 while (*ptr1)
1359                 {
1360                     ptr2 = ptr1 + strlen(ptr1) + 1;
1361                     TRACE("\t%s => %s\n", ptr1, ptr2); 
1362                     ptr1 = ptr2 + strlen(ptr2) + 1;
1363                 }
1364             }
1365             break;
1366         case S_COMPILAND_V3:
1367             TRACE("S-Compiland-V3 %s\n", sym->compiland_v3.name);
1368             if (TRACE_ON(dbghelp_msc))
1369             {
1370                 const char* ptr1 = sym->compiland_v3.name + strlen(sym->compiland_v3.name);
1371                 const char* ptr2;
1372                 while (*ptr1)
1373                 {
1374                     ptr2 = ptr1 + strlen(ptr1) + 1;
1375                     TRACE("\t%s => %s\n", ptr1, ptr2); 
1376                     ptr1 = ptr2 + strlen(ptr2) + 1;
1377                 }
1378             }
1379             break;
1380
1381         case S_OBJNAME_V1:
1382             TRACE("S-ObjName %.*s\n", ((const BYTE*)sym)[8], (const BYTE*)sym + 9);
1383             break;
1384
1385         case S_LABEL_V1:
1386             if (curr_func)
1387             {
1388                 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel, 
1389                                         codeview_get_address(msc_dbg, sym->label_v1.segment, sym->label_v1.offset) - curr_func->address,
1390                                         terminate_string(&sym->label_v1.p_name));
1391             }
1392             else
1393                 FIXME("No current function for label %s\n",
1394                       terminate_string(&sym->label_v1.p_name));
1395             break;
1396         case S_LABEL_V3:
1397             if (curr_func)
1398             {
1399                 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel, 
1400                                         codeview_get_address(msc_dbg, sym->label_v3.segment, sym->label_v3.offset) - curr_func->address,
1401                                         sym->label_v3.name);
1402             }
1403             else
1404                 FIXME("No current function for label %s\n", sym->label_v3.name);
1405             break;
1406
1407         case S_CONSTANT_V1:
1408             {
1409                 int                     val, vlen;
1410                 const struct p_string*  name;
1411                 const char*             x;
1412                 struct symt*            se;
1413
1414                 vlen = numeric_leaf(&val, &sym->constant_v1.cvalue);
1415                 name = (const struct p_string*)((const char*)&sym->constant_v1.cvalue + vlen);
1416                 se = codeview_get_type(sym->constant_v1.type, FALSE);
1417                 if (!se) x = "---";
1418                 else if (se->tag == SymTagEnum) x = ((struct symt_enum*)se)->name;
1419                 else x = "###";
1420                     
1421                 TRACE("S-Constant-V1 %u %s %x (%s)\n", 
1422                       val, terminate_string(name), sym->constant_v1.type, x);
1423                 /* FIXME: we should add this as a constant value */
1424             }
1425             break;
1426         case S_CONSTANT_V2:
1427             {
1428                 int                     val, vlen;
1429                 const struct p_string*  name;
1430                 const char*             x;
1431                 struct symt*            se;
1432
1433                 vlen = numeric_leaf(&val, &sym->constant_v2.cvalue);
1434                 name = (const struct p_string*)((const char*)&sym->constant_v2.cvalue + vlen);
1435                 se = codeview_get_type(sym->constant_v2.type, FALSE);
1436                 if (!se) x = "---";
1437                 else if (se->tag == SymTagEnum) x = ((struct symt_enum*)se)->name;
1438                 else x = "###";
1439                     
1440                 TRACE("S-Constant-V2 %u %s %x (%s)\n", 
1441                       val, terminate_string(name), sym->constant_v2.type, x);
1442                 /* FIXME: we should add this as a constant value */
1443             }
1444             break;
1445         case S_CONSTANT_V3:
1446             {
1447                 int                     val, vlen;
1448                 const char*             name;
1449                 const char*             x;
1450                 struct symt*            se;
1451
1452                 vlen = numeric_leaf(&val, &sym->constant_v3.cvalue);
1453                 name = (const char*)&sym->constant_v3.cvalue + vlen;
1454                 se = codeview_get_type(sym->constant_v3.type, FALSE);
1455                 if (!se) x = "---";
1456                 else if (se->tag == SymTagEnum) x = ((struct symt_enum*)se)->name;
1457                 else x = "###";
1458                     
1459                 TRACE("S-Constant-V3 %u %s %x (%s)\n", 
1460                       val, name, sym->constant_v3.type, x);
1461                 /* FIXME: we should add this as a constant value */
1462             }
1463             break;
1464
1465         case S_UDT_V1:
1466             if (sym->udt_v1.type)
1467             {
1468                 if ((symt = codeview_get_type(sym->udt_v1.type, FALSE)))
1469                     symt_new_typedef(msc_dbg->module, symt, 
1470                                      terminate_string(&sym->udt_v1.p_name));
1471                 else
1472                     FIXME("S-Udt %s: couldn't find type 0x%x\n", 
1473                           terminate_string(&sym->udt_v1.p_name), sym->udt_v1.type);
1474             }
1475             break;
1476         case S_UDT_V2:
1477             if (sym->udt_v2.type)
1478             {
1479                 if ((symt = codeview_get_type(sym->udt_v2.type, FALSE)))
1480                     symt_new_typedef(msc_dbg->module, symt, 
1481                                      terminate_string(&sym->udt_v2.p_name));
1482                 else
1483                     FIXME("S-Udt %s: couldn't find type 0x%x\n", 
1484                           terminate_string(&sym->udt_v2.p_name), sym->udt_v2.type);
1485             }
1486             break;
1487         case S_UDT_V3:
1488             if (sym->udt_v3.type)
1489             {
1490                 if ((symt = codeview_get_type(sym->udt_v3.type, FALSE)))
1491                     symt_new_typedef(msc_dbg->module, symt, sym->udt_v3.name);
1492                 else
1493                     FIXME("S-Udt %s: couldn't find type 0x%x\n", 
1494                           sym->udt_v3.name, sym->udt_v3.type);
1495             }
1496             break;
1497
1498          /*
1499          * These are special, in that they are always followed by an
1500          * additional length-prefixed string which is *not* included
1501          * into the symbol length count.  We need to skip it.
1502          */
1503         case S_PROCREF_V1:
1504         case S_DATAREF_V1:
1505         case S_LPROCREF_V1:
1506             name = (const char*)sym + length;
1507             length += (*name + 1 + 3) & ~3;
1508             break;
1509
1510         case S_PUB_DATA_V3:
1511             if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1512             {
1513                 flt = codeview_get_linetab(linetab, sym->data_v3.segment, sym->data_v3.offset);
1514                 symt_new_public(msc_dbg->module, 
1515                                 flt ? flt->compiland : NULL,
1516                                 sym->data_v3.name, 
1517                                 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset),
1518                                 0, FALSE /* FIXME */, FALSE);
1519             }
1520             break;
1521         case S_PUB_FUNC1_V3:
1522         case S_PUB_FUNC2_V3: /* using a data_v3 isn't what we'd expect */
1523             if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1524             {
1525                 flt = codeview_get_linetab(linetab, sym->data_v3.segment, sym->data_v3.offset);
1526                 symt_new_public(msc_dbg->module, 
1527                                 flt ? flt->compiland : NULL,
1528                                 sym->data_v3.name, 
1529                                 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset),
1530                                 0, TRUE /* FIXME */, TRUE);
1531             }
1532             break;
1533
1534         case S_MSTOOL_V3: /* just to silence a few warnings */
1535             break;
1536
1537         default:
1538             FIXME("Unsupported symbol id %x\n", sym->generic.id);
1539             dump(sym, 2 + sym->generic.len);
1540             break;
1541         }
1542     }
1543
1544     if (curr_func) symt_normalize_function(msc_dbg->module, curr_func);
1545
1546     HeapFree(GetProcessHeap(), 0, linetab);
1547     return TRUE;
1548 }
1549
1550 /*========================================================================
1551  * Process PDB file.
1552  */
1553
1554 struct pdb_lookup
1555 {
1556     const char*                 filename;
1557     enum {PDB_JG, PDB_DS}       kind;
1558     union
1559     {
1560         struct
1561         {
1562             DWORD               timestamp;
1563             struct PDB_JG_TOC*  toc;
1564         } jg;
1565         struct
1566         {
1567             GUID                guid;
1568             struct PDB_DS_TOC*  toc;
1569         } ds;
1570     } u;
1571 };
1572
1573 static void* pdb_jg_read(const struct PDB_JG_HEADER* pdb, const WORD* block_list,
1574                          int size)
1575 {
1576     int                         i, num_blocks;
1577     BYTE*                       buffer;
1578
1579     if (!size) return NULL;
1580
1581     num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
1582     buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
1583
1584     for (i = 0; i < num_blocks; i++)
1585         memcpy(buffer + i * pdb->block_size,
1586                (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
1587
1588     return buffer;
1589 }
1590
1591 static void* pdb_ds_read(const struct PDB_DS_HEADER* pdb, const DWORD* block_list,
1592                          int size)
1593 {
1594     int                         i, num_blocks;
1595     BYTE*                       buffer;
1596
1597     if (!size) return NULL;
1598
1599     num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
1600     buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
1601
1602     for (i = 0; i < num_blocks; i++)
1603         memcpy(buffer + i * pdb->block_size,
1604                (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
1605
1606     return buffer;
1607 }
1608
1609 static void* pdb_read_jg_file(const struct PDB_JG_HEADER* pdb,
1610                               const struct PDB_JG_TOC* toc, DWORD file_nr)
1611 {
1612     const WORD*                 block_list;
1613     DWORD                       i;
1614
1615     if (!toc || file_nr >= toc->num_files) return NULL;
1616
1617     block_list = (const WORD*) &toc->file[toc->num_files];
1618     for (i = 0; i < file_nr; i++)
1619         block_list += (toc->file[i].size + pdb->block_size - 1) / pdb->block_size;
1620
1621     return pdb_jg_read(pdb, block_list, toc->file[file_nr].size);
1622 }
1623
1624 static void* pdb_read_ds_file(const struct PDB_DS_HEADER* pdb,
1625                               const struct PDB_DS_TOC* toc, DWORD file_nr)
1626 {
1627     const DWORD*                block_list;
1628     DWORD                       i;
1629
1630     if (!toc || file_nr >= toc->num_files) return NULL;
1631
1632     if (toc->file_size[file_nr] == 0 || toc->file_size[file_nr] == 0xFFFFFFFF)
1633     {
1634         FIXME(">>> requesting NULL stream (%lu)\n", file_nr);
1635         return NULL;
1636     }
1637     block_list = &toc->file_size[toc->num_files];
1638     for (i = 0; i < file_nr; i++)
1639         block_list += (toc->file_size[i] + pdb->block_size - 1) / pdb->block_size;
1640
1641     return pdb_ds_read(pdb, block_list, toc->file_size[file_nr]);
1642 }
1643
1644 static void* pdb_read_file(const char* image, const struct pdb_lookup* pdb_lookup,
1645                            DWORD file_nr)
1646 {
1647     switch (pdb_lookup->kind)
1648     {
1649     case PDB_JG:
1650         return pdb_read_jg_file((const struct PDB_JG_HEADER*)image, 
1651                                 pdb_lookup->u.jg.toc, file_nr);
1652     case PDB_DS:
1653         return pdb_read_ds_file((const struct PDB_DS_HEADER*)image,
1654                                 pdb_lookup->u.ds.toc, file_nr);
1655     }
1656     return NULL;
1657 }
1658
1659 static unsigned pdb_get_file_size(const struct pdb_lookup* pdb_lookup, DWORD file_nr)
1660 {
1661     switch (pdb_lookup->kind)
1662     {
1663     case PDB_JG: return pdb_lookup->u.jg.toc->file[file_nr].size;
1664     case PDB_DS: return pdb_lookup->u.ds.toc->file_size[file_nr];
1665     }
1666     return 0;
1667 }
1668
1669 static void pdb_free(void* buffer)
1670 {
1671     HeapFree(GetProcessHeap(), 0, buffer);
1672 }
1673
1674 static void pdb_free_lookup(const struct pdb_lookup* pdb_lookup)
1675 {
1676     switch (pdb_lookup->kind)
1677     {
1678     case PDB_JG:
1679         if (pdb_lookup->u.jg.toc) pdb_free(pdb_lookup->u.jg.toc);
1680         break;
1681     case PDB_DS:
1682         if (pdb_lookup->u.ds.toc) pdb_free(pdb_lookup->u.ds.toc);
1683         break;
1684     }
1685 }
1686     
1687 static void pdb_convert_types_header(PDB_TYPES* types, const BYTE* image)
1688 {
1689     memset(types, 0, sizeof(PDB_TYPES));
1690     if (!image) return;
1691
1692     if (*(const DWORD*)image < 19960000)   /* FIXME: correct version? */
1693     {
1694         /* Old version of the types record header */
1695         const PDB_TYPES_OLD*    old = (const PDB_TYPES_OLD*)image;
1696         types->version     = old->version;
1697         types->type_offset = sizeof(PDB_TYPES_OLD);
1698         types->type_size   = old->type_size;
1699         types->first_index = old->first_index;
1700         types->last_index  = old->last_index;
1701         types->file        = old->file;
1702     }
1703     else
1704     {
1705         /* New version of the types record header */
1706         *types = *(const PDB_TYPES*)image;
1707     }
1708 }
1709
1710 static void pdb_convert_symbols_header(PDB_SYMBOLS* symbols,
1711                                        int* header_size, const BYTE* image)
1712 {
1713     memset(symbols, 0, sizeof(PDB_SYMBOLS));
1714     if (!image) return;
1715
1716     if (*(const DWORD*)image != 0xffffffff)
1717     {
1718         /* Old version of the symbols record header */
1719         const PDB_SYMBOLS_OLD*  old = (const PDB_SYMBOLS_OLD*)image;
1720         symbols->version         = 0;
1721         symbols->module_size     = old->module_size;
1722         symbols->offset_size     = old->offset_size;
1723         symbols->hash_size       = old->hash_size;
1724         symbols->srcmodule_size  = old->srcmodule_size;
1725         symbols->pdbimport_size  = 0;
1726         symbols->hash1_file      = old->hash1_file;
1727         symbols->hash2_file      = old->hash2_file;
1728         symbols->gsym_file       = old->gsym_file;
1729
1730         *header_size = sizeof(PDB_SYMBOLS_OLD);
1731     }
1732     else
1733     {
1734         /* New version of the symbols record header */
1735         *symbols = *(const PDB_SYMBOLS*)image;
1736         *header_size = sizeof(PDB_SYMBOLS);
1737     }
1738 }
1739
1740 static void pdb_convert_symbol_file(const PDB_SYMBOLS* symbols, 
1741                                     PDB_SYMBOL_FILE_EX* sfile, 
1742                                     unsigned* size, const void* image)
1743
1744 {
1745     if (symbols->version < 19970000)
1746     {
1747         const PDB_SYMBOL_FILE *sym_file = (const PDB_SYMBOL_FILE*)image;
1748         memset(sfile, 0, sizeof(*sfile));
1749         sfile->file        = sym_file->file;
1750         sfile->range.index = sym_file->range.index;
1751         sfile->symbol_size = sym_file->symbol_size;
1752         sfile->lineno_size = sym_file->lineno_size;
1753         *size = sizeof(PDB_SYMBOL_FILE) - 1;
1754     }
1755     else
1756     {
1757         memcpy(sfile, image, sizeof(PDB_SYMBOL_FILE_EX));
1758         *size = sizeof(PDB_SYMBOL_FILE_EX) - 1;
1759     }
1760 }
1761
1762 static BOOL CALLBACK pdb_match(char* file, void* user)
1763 {
1764     /* accept first file that exists */
1765     HANDLE h = CreateFileA(file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1766     TRACE("match with %s returns %p\n", file, h);
1767     if (INVALID_HANDLE_VALUE != h) {
1768         CloseHandle(h);
1769         return FALSE;
1770     }
1771     return TRUE;
1772 }
1773
1774 static HANDLE open_pdb_file(const struct process* pcs, const char* filename)
1775 {
1776     HANDLE      h;
1777     char        dbg_file_path[MAX_PATH];
1778
1779     h = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL, 
1780                     OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1781     /* FIXME: should give more bits on the file to look at */
1782     if (h == INVALID_HANDLE_VALUE &&
1783         SymFindFileInPath(pcs->handle, NULL, (char*)filename, NULL, 0, 0, 0,
1784                           dbg_file_path, pdb_match, NULL))
1785     {
1786         h = CreateFileA(dbg_file_path, GENERIC_READ, FILE_SHARE_READ, NULL, 
1787                         OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1788         TRACE("with %s returns %p\n", dbg_file_path, h);
1789     }
1790     return (h == INVALID_HANDLE_VALUE) ? NULL : h;
1791 }
1792
1793 static void pdb_process_types(const struct msc_debug_info* msc_dbg, 
1794                               const char* image, struct pdb_lookup* pdb_lookup)
1795 {
1796     BYTE*       types_image = NULL;
1797
1798     types_image = pdb_read_file(image, pdb_lookup, 2);
1799     if (types_image)
1800     {
1801         PDB_TYPES   types;
1802         pdb_convert_types_header(&types, types_image);
1803
1804         /* Check for unknown versions */
1805         switch (types.version)
1806         {
1807         case 19950410:      /* VC 4.0 */
1808         case 19951122:
1809         case 19961031:      /* VC 5.0 / 6.0 */
1810         case 19990903:
1811             break;
1812         default:
1813             ERR("-Unknown type info version %ld\n", types.version);
1814         }
1815
1816         /* Read type table */
1817         codeview_parse_type_table(msc_dbg->module, types_image + types.type_offset,
1818                                   types.type_size);
1819         pdb_free(types_image);
1820     }
1821 }
1822
1823 static const char       PDB_JG_IDENT[] = "Microsoft C/C++ program database 2.00\r\n\032JG\0";
1824 static const char       PDB_DS_IDENT[] = "Microsoft C/C++ MSF 7.00\r\n\032DS\0";
1825
1826 static BOOL pdb_init(struct pdb_lookup* pdb_lookup, const char* image)
1827 {
1828     /* check the file header, and if ok, load the TOC */
1829     TRACE("PDB(%s): %.40s\n", pdb_lookup->filename, debugstr_an(image, 40));
1830     switch (pdb_lookup->kind)
1831     {
1832     case PDB_JG:
1833         pdb_lookup->u.jg.toc = NULL;
1834         if (memcmp(image, PDB_JG_IDENT, sizeof(PDB_JG_IDENT)))
1835         {
1836             FIXME("Couldn't match JG header\n");
1837             return FALSE;
1838         }
1839         else
1840         {
1841             const struct PDB_JG_HEADER* pdb = (const struct PDB_JG_HEADER*)image;
1842             struct PDB_JG_ROOT*         root;
1843
1844             pdb_lookup->u.jg.toc = pdb_jg_read(pdb, pdb->toc_block, pdb->toc.size);
1845             root = pdb_read_jg_file(pdb, pdb_lookup->u.jg.toc, 1);
1846             if (!root)
1847             {
1848                 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
1849                 return FALSE;
1850             }
1851             switch (root->Version)
1852             {
1853             case 19950623:      /* VC 4.0 */
1854             case 19950814:
1855             case 19960307:      /* VC 5.0 */
1856             case 19970604:      /* VC 6.0 */
1857                 break;
1858             default:
1859                 ERR("-Unknown root block version %ld\n", root->Version);
1860             } 
1861             /* Check .PDB time stamp */
1862             if (root->TimeDateStamp != pdb_lookup->u.jg.timestamp)
1863             {
1864                 ERR("-Wrong time stamp of .PDB file %s (0x%08lx, 0x%08lx)\n",
1865                     pdb_lookup->filename, root->TimeDateStamp, 
1866                     pdb_lookup->u.jg.timestamp);
1867             }
1868             pdb_free(root);
1869         }
1870         break;
1871     case PDB_DS:
1872         pdb_lookup->u.ds.toc = NULL;
1873         if (memcmp(image, PDB_DS_IDENT, sizeof(PDB_DS_IDENT)))
1874         {
1875             FIXME("Couldn't match DS header\n");
1876             return FALSE;
1877         }
1878         else
1879         {
1880             const struct PDB_DS_HEADER* pdb = (const struct PDB_DS_HEADER*)image;
1881             struct PDB_DS_ROOT*         root;
1882
1883             pdb_lookup->u.ds.toc = 
1884                 pdb_ds_read(pdb, 
1885                             (const DWORD*)((const char*)pdb + pdb->toc_page * pdb->block_size), 
1886                             pdb->toc_size);
1887             root = pdb_read_ds_file(pdb, pdb_lookup->u.ds.toc, 1);
1888             if (!root)
1889             {
1890                 ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
1891                 return FALSE;
1892             }
1893             switch (root->Version)
1894             {
1895             case 20000404:
1896                 break;
1897             default:
1898                 ERR("-Unknown root block version %ld\n", root->Version);
1899             } 
1900             /* Check .PDB time stamp */
1901             if (memcmp(&root->guid, &pdb_lookup->u.ds.guid, sizeof(GUID)))
1902             {
1903                 ERR("-Wrong GUID of .PDB file %s (%s, %s)\n",
1904                     pdb_lookup->filename, 
1905                     wine_dbgstr_guid(&root->guid), 
1906                     wine_dbgstr_guid(&pdb_lookup->u.ds.guid));
1907             }
1908             pdb_free(root);
1909         }
1910         break;
1911     }
1912
1913     if (0) /* some tool to dump the internal files from a PDB file */
1914     {
1915         int     i, num_files;
1916         
1917         switch (pdb_lookup->kind)
1918         {
1919         case PDB_JG: num_files = pdb_lookup->u.jg.toc->num_files; break;
1920         case PDB_DS: num_files = pdb_lookup->u.ds.toc->num_files; break;
1921         }
1922
1923         for (i = 1; i < num_files; i++)
1924         {
1925             unsigned char* x = pdb_read_file(image, pdb_lookup, i);
1926             FIXME("********************** [%u]: size=%08x\n",
1927                   i, pdb_get_file_size(pdb_lookup, i));
1928             dump(x, pdb_get_file_size(pdb_lookup, i));
1929             pdb_free(x);
1930         }
1931     }
1932     return TRUE;
1933 }
1934
1935 static BOOL pdb_process_internal(const struct process* pcs, 
1936                                  const struct msc_debug_info* msc_dbg,
1937                                  struct pdb_lookup* pdb_lookup,
1938                                  unsigned module_index);
1939
1940 static void pdb_process_symbol_imports(const struct process* pcs, 
1941                                        const struct msc_debug_info* msc_dbg,
1942                                        PDB_SYMBOLS* symbols, 
1943                                        const void* symbols_image,
1944                                        char* image, struct pdb_lookup* pdb_lookup,
1945                                        unsigned module_index)
1946 {
1947     if (module_index == -1 && symbols && symbols->pdbimport_size)
1948     {
1949         const PDB_SYMBOL_IMPORT*imp;
1950         const void*             first;
1951         const void*             last;
1952         const char*             ptr;
1953         int                     i = 0;
1954
1955         imp = (const PDB_SYMBOL_IMPORT*)((const char*)symbols_image + sizeof(PDB_SYMBOLS) + 
1956                                          symbols->module_size + symbols->offset_size + 
1957                                          symbols->hash_size + symbols->srcmodule_size);
1958         first = (const char*)imp;
1959         last = (const char*)imp + symbols->pdbimport_size;
1960         while (imp < (const PDB_SYMBOL_IMPORT*)last)
1961         {
1962             ptr = (const char*)imp + sizeof(*imp) + strlen(imp->filename);
1963             if (i >= CV_MAX_MODULES) FIXME("Out of bounds !!!\n");
1964             if (!strcasecmp(pdb_lookup->filename, imp->filename))
1965             {
1966                 if (module_index != -1) FIXME("Twice the entry\n");
1967                 else module_index = i;
1968             }
1969             else
1970             {
1971                 struct pdb_lookup       imp_pdb_lookup;
1972
1973                 imp_pdb_lookup.filename = imp->filename;
1974                 imp_pdb_lookup.kind = PDB_JG;
1975                 imp_pdb_lookup.u.jg.timestamp = imp->TimeDateStamp;
1976                 pdb_process_internal(pcs, msc_dbg, &imp_pdb_lookup, i);
1977             }
1978             i++;
1979             imp = (const PDB_SYMBOL_IMPORT*)((const char*)first + ((ptr - (const char*)first + strlen(ptr) + 1 + 3) & ~3));
1980         }
1981     }
1982     cv_current_module = &cv_zmodules[(module_index == -1) ? 0 : module_index];
1983     if (cv_current_module->allowed) FIXME("Already allowed ??\n");
1984     cv_current_module->allowed = TRUE;
1985     pdb_process_types(msc_dbg, image, pdb_lookup);
1986 }
1987
1988 static BOOL pdb_process_internal(const struct process* pcs, 
1989                                  const struct msc_debug_info* msc_dbg,
1990                                  struct pdb_lookup* pdb_lookup, 
1991                                  unsigned module_index)
1992 {
1993     BOOL        ret = FALSE;
1994     HANDLE      hFile, hMap = NULL;
1995     char*       image = NULL;
1996     BYTE*       symbols_image = NULL;
1997
1998     TRACE("Processing PDB file %s\n", pdb_lookup->filename);
1999
2000     /* Open and map() .PDB file */
2001     if ((hFile = open_pdb_file(pcs, pdb_lookup->filename)) == NULL ||
2002         ((hMap = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL) ||
2003         ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
2004     {
2005         WARN("Unable to open .PDB file: %s\n", pdb_lookup->filename);
2006         goto leave;
2007     }
2008     pdb_init(pdb_lookup, image);
2009
2010     symbols_image = pdb_read_file(image, pdb_lookup, 3);
2011     if (symbols_image)
2012     {
2013         PDB_SYMBOLS symbols;
2014         BYTE*       modimage;
2015         BYTE*       file;
2016         int         header_size = 0;
2017         
2018         pdb_convert_symbols_header(&symbols, &header_size, symbols_image);
2019         switch (symbols.version)
2020         {
2021         case 0:            /* VC 4.0 */
2022         case 19960307:     /* VC 5.0 */
2023         case 19970606:     /* VC 6.0 */
2024         case 19990903:
2025             break;
2026         default:
2027             ERR("-Unknown symbol info version %ld %08lx\n", 
2028                 symbols.version, symbols.version);
2029         }
2030
2031         pdb_process_symbol_imports(pcs, msc_dbg, &symbols, symbols_image, image, pdb_lookup, module_index);
2032
2033         /* Read global symbol table */
2034         modimage = pdb_read_file(image, pdb_lookup, symbols.gsym_file);
2035         if (modimage)
2036         {
2037             codeview_snarf(msc_dbg, modimage, 0, 
2038                            pdb_get_file_size(pdb_lookup, symbols.gsym_file), NULL);
2039
2040             pdb_free(modimage);
2041         }
2042
2043         /* Read per-module symbol / linenumber tables */
2044         file = symbols_image + header_size;
2045         while (file - symbols_image < header_size + symbols.module_size)
2046         {
2047             PDB_SYMBOL_FILE_EX          sfile;
2048             const char*                 file_name;
2049             unsigned                    size;
2050
2051             HeapValidate(GetProcessHeap(), 0, NULL);
2052             pdb_convert_symbol_file(&symbols, &sfile, &size, file);
2053
2054             modimage = pdb_read_file(image, pdb_lookup, sfile.file);
2055             if (modimage)
2056             {
2057                 struct codeview_linetab*    linetab = NULL;
2058
2059                 if (sfile.lineno_size)
2060                     linetab = codeview_snarf_linetab(msc_dbg->module, 
2061                                                      modimage + sfile.symbol_size,
2062                                                      sfile.lineno_size,
2063                                                      pdb_lookup->kind == PDB_JG);
2064
2065                 if (sfile.symbol_size)
2066                     codeview_snarf(msc_dbg, modimage, sizeof(DWORD),
2067                                    sfile.symbol_size, linetab);
2068
2069                 pdb_free(modimage);
2070             }
2071             file_name = (const char*)file + size;
2072             file_name += strlen(file_name) + 1;
2073             file = (BYTE*)((DWORD)(file_name + strlen(file_name) + 1 + 3) & ~3);
2074         }
2075     }
2076     else
2077         pdb_process_symbol_imports(pcs, msc_dbg, NULL, NULL, image, pdb_lookup, 
2078                                    module_index);
2079     msc_dbg->module->module.SymType = SymCv;
2080     ret = TRUE;
2081
2082  leave:
2083     /* Cleanup */
2084     if (symbols_image) pdb_free(symbols_image);
2085     pdb_free_lookup(pdb_lookup);
2086
2087     if (image) UnmapViewOfFile(image);
2088     if (hMap) CloseHandle(hMap);
2089     if (hFile) CloseHandle(hFile);
2090
2091     return ret;
2092 }
2093
2094 static BOOL pdb_process_file(const struct process* pcs, 
2095                              const struct msc_debug_info* msc_dbg,
2096                              struct pdb_lookup* pdb_lookup)
2097 {
2098     BOOL        ret;
2099
2100     memset(cv_zmodules, 0, sizeof(cv_zmodules));
2101     codeview_init_basic_types(msc_dbg->module);
2102     ret = pdb_process_internal(pcs, msc_dbg, pdb_lookup, -1);
2103     codeview_clear_type_table();
2104     return ret;
2105 }
2106
2107 /*========================================================================
2108  * Process CodeView debug information.
2109  */
2110
2111 #define MAKESIG(a,b,c,d)        ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
2112 #define CODEVIEW_NB09_SIG       MAKESIG('N','B','0','9')
2113 #define CODEVIEW_NB10_SIG       MAKESIG('N','B','1','0')
2114 #define CODEVIEW_NB11_SIG       MAKESIG('N','B','1','1')
2115 #define CODEVIEW_RSDS_SIG       MAKESIG('R','S','D','S')
2116
2117 typedef struct _CODEVIEW_HEADER_NBxx
2118 {
2119     DWORD       dwSignature;
2120     DWORD       lfoDirectory;
2121 } CODEVIEW_HEADER_NBxx,* PCODEVIEW_HEADER_NBxx;
2122
2123 typedef struct _CODEVIEW_HEADER_RSDS
2124 {
2125     DWORD       dwSignature;
2126     GUID        guid;
2127     DWORD       unknown;
2128     CHAR        name[1];
2129 } CODEVIEW_HEADER_RSDS,* PCODEVIEW_HEADER_RSDS;
2130
2131 typedef struct _CODEVIEW_PDB_DATA
2132 {
2133     DWORD       timestamp;
2134     DWORD       unknown;
2135     CHAR        name[1];
2136 } CODEVIEW_PDB_DATA, *PCODEVIEW_PDB_DATA;
2137
2138 typedef struct _CV_DIRECTORY_HEADER
2139 {
2140     WORD        cbDirHeader;
2141     WORD        cbDirEntry;
2142     DWORD       cDir;
2143     DWORD       lfoNextDir;
2144     DWORD       flags;
2145 } CV_DIRECTORY_HEADER, *PCV_DIRECTORY_HEADER;
2146
2147 typedef struct _CV_DIRECTORY_ENTRY
2148 {
2149     WORD        subsection;
2150     WORD        iMod;
2151     DWORD       lfo;
2152     DWORD       cb;
2153 } CV_DIRECTORY_ENTRY, *PCV_DIRECTORY_ENTRY;
2154
2155 #define sstAlignSym             0x125
2156 #define sstSrcModule            0x127
2157
2158 static BOOL codeview_process_info(const struct process* pcs, 
2159                                   const struct msc_debug_info* msc_dbg)
2160 {
2161     const CODEVIEW_HEADER_NBxx* cv = (const CODEVIEW_HEADER_NBxx*)msc_dbg->root;
2162     BOOL                        ret = FALSE;
2163     struct pdb_lookup           pdb_lookup;
2164
2165     TRACE("Processing signature %.4s\n", (const char*)&cv->dwSignature);
2166
2167     switch (cv->dwSignature)
2168     {
2169     case CODEVIEW_NB09_SIG:
2170     case CODEVIEW_NB11_SIG:
2171     {
2172         const CV_DIRECTORY_HEADER*      hdr = (const CV_DIRECTORY_HEADER*)(msc_dbg->root + cv->lfoDirectory);
2173         const CV_DIRECTORY_ENTRY*       ent;
2174         const CV_DIRECTORY_ENTRY*       prev;
2175         const CV_DIRECTORY_ENTRY*       next;
2176         unsigned int                    i;
2177
2178         codeview_init_basic_types(msc_dbg->module);
2179         ent = (const CV_DIRECTORY_ENTRY*)((const BYTE*)hdr + hdr->cbDirHeader);
2180         for (i = 0; i < hdr->cDir; i++, ent = next)
2181         {
2182             next = (i == hdr->cDir-1)? NULL :
2183                    (const CV_DIRECTORY_ENTRY*)((const BYTE*)ent + hdr->cbDirEntry);
2184             prev = (i == 0)? NULL :
2185                    (const CV_DIRECTORY_ENTRY*)((const BYTE*)ent - hdr->cbDirEntry);
2186
2187             if (ent->subsection == sstAlignSym)
2188             {
2189                 /*
2190                  * Check the next and previous entry.  If either is a
2191                  * sstSrcModule, it contains the line number info for
2192                  * this file.
2193                  *
2194                  * FIXME: This is not a general solution!
2195                  */
2196                 struct codeview_linetab*        linetab = NULL;
2197
2198                 if (next && next->iMod == ent->iMod && 
2199                     next->subsection == sstSrcModule)
2200                     linetab = codeview_snarf_linetab(msc_dbg->module, 
2201                                                      msc_dbg->root + next->lfo, next->cb, 
2202                                                      TRUE);
2203
2204                 if (prev && prev->iMod == ent->iMod &&
2205                     prev->subsection == sstSrcModule)
2206                     linetab = codeview_snarf_linetab(msc_dbg->module, 
2207                                                      msc_dbg->root + prev->lfo, prev->cb, 
2208                                                      TRUE);
2209
2210                 codeview_snarf(msc_dbg, msc_dbg->root + ent->lfo, sizeof(DWORD),
2211                                ent->cb, linetab);
2212             }
2213         }
2214
2215         msc_dbg->module->module.SymType = SymCv;
2216         ret = TRUE;
2217         break;
2218     }
2219
2220     case CODEVIEW_NB10_SIG:
2221     {
2222         const CODEVIEW_PDB_DATA* pdb = (const CODEVIEW_PDB_DATA*)(cv + 1);
2223         pdb_lookup.filename = pdb->name;
2224         pdb_lookup.kind = PDB_JG;
2225         pdb_lookup.u.jg.timestamp = pdb->timestamp;
2226         pdb_lookup.u.jg.toc = NULL;
2227         ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
2228         break;
2229     }
2230     case CODEVIEW_RSDS_SIG:
2231     {
2232         const CODEVIEW_HEADER_RSDS* rsds = (const CODEVIEW_HEADER_RSDS*)msc_dbg->root;
2233
2234         TRACE("Got RSDS type of PDB file: guid=%s unk=%08lx name=%s\n",
2235               wine_dbgstr_guid(&rsds->guid), rsds->unknown, rsds->name);
2236         pdb_lookup.filename = rsds->name;
2237         pdb_lookup.kind = PDB_DS;
2238         pdb_lookup.u.ds.guid = rsds->guid;
2239         pdb_lookup.u.ds.toc = NULL;
2240         ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
2241         break;
2242     }
2243     default:
2244         ERR("Unknown CODEVIEW signature %08lX in module %s\n",
2245             cv->dwSignature, msc_dbg->module->module.ModuleName);
2246         break;
2247     }
2248
2249     return ret;
2250 }
2251
2252 /*========================================================================
2253  * Process debug directory.
2254  */
2255 BOOL pe_load_debug_directory(const struct process* pcs, struct module* module, 
2256                              const BYTE* mapping,
2257                              const IMAGE_SECTION_HEADER* sectp, DWORD nsect,
2258                              const IMAGE_DEBUG_DIRECTORY* dbg, int nDbg)
2259 {
2260     BOOL                        ret;
2261     int                         i;
2262     struct msc_debug_info       msc_dbg;
2263
2264     msc_dbg.module = module;
2265     msc_dbg.nsect  = nsect;
2266     msc_dbg.sectp  = sectp;
2267     msc_dbg.nomap  = 0;
2268     msc_dbg.omapp  = NULL;
2269
2270     __TRY
2271     {
2272         ret = FALSE;
2273
2274         /* First, watch out for OMAP data */
2275         for (i = 0; i < nDbg; i++)
2276         {
2277             if (dbg[i].Type == IMAGE_DEBUG_TYPE_OMAP_FROM_SRC)
2278             {
2279                 msc_dbg.nomap = dbg[i].SizeOfData / sizeof(OMAP_DATA);
2280                 msc_dbg.omapp = (const OMAP_DATA*)(mapping + dbg[i].PointerToRawData);
2281                 break;
2282             }
2283         }
2284   
2285         /* Now, try to parse CodeView debug info */
2286         for (i = 0; i < nDbg; i++)
2287         {
2288             if (dbg[i].Type == IMAGE_DEBUG_TYPE_CODEVIEW)
2289             {
2290                 msc_dbg.root = mapping + dbg[i].PointerToRawData;
2291                 if ((ret = codeview_process_info(pcs, &msc_dbg))) goto done;
2292             }
2293         }
2294     
2295         /* If not found, try to parse COFF debug info */
2296         for (i = 0; i < nDbg; i++)
2297         {
2298             if (dbg[i].Type == IMAGE_DEBUG_TYPE_COFF)
2299             {
2300                 msc_dbg.root = mapping + dbg[i].PointerToRawData;
2301                 if ((ret = coff_process_info(&msc_dbg))) goto done;
2302             }
2303         }
2304     done:
2305          /* FIXME: this should be supported... this is the debug information for
2306           * functions compiled without a frame pointer (FPO = frame pointer omission)
2307           * the associated data helps finding out the relevant information
2308           */
2309         for (i = 0; i < nDbg; i++)
2310             if (dbg[i].Type == IMAGE_DEBUG_TYPE_FPO)
2311                 FIXME("This guy has FPO information\n");
2312 #if 0
2313
2314 #define FRAME_FPO   0
2315 #define FRAME_TRAP  1
2316 #define FRAME_TSS   2
2317
2318 typedef struct _FPO_DATA 
2319 {
2320         DWORD       ulOffStart;            /* offset 1st byte of function code */
2321         DWORD       cbProcSize;            /* # bytes in function */
2322         DWORD       cdwLocals;             /* # bytes in locals/4 */
2323         WORD        cdwParams;             /* # bytes in params/4 */
2324
2325         WORD        cbProlog : 8;          /* # bytes in prolog */
2326         WORD        cbRegs   : 3;          /* # regs saved */
2327         WORD        fHasSEH  : 1;          /* TRUE if SEH in func */
2328         WORD        fUseBP   : 1;          /* TRUE if EBP has been allocated */
2329         WORD        reserved : 1;          /* reserved for future use */
2330         WORD        cbFrame  : 2;          /* frame type */
2331 } FPO_DATA;
2332 #endif
2333
2334     }
2335     __EXCEPT_PAGE_FAULT
2336     {
2337         ERR("Got a page fault while loading symbols\n");
2338         ret = FALSE;
2339     }
2340     __ENDTRY
2341     return ret;
2342 }