msi: Verify the existence of fusion.dll before reporting the .Net version.
[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-2009, 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 #define NONAMELESSUNION
36
37 #include "config.h"
38 #include "wine/port.h"
39
40 #include <assert.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43
44 #include <string.h>
45 #ifdef HAVE_UNISTD_H
46 # include <unistd.h>
47 #endif
48 #ifndef PATH_MAX
49 #define PATH_MAX MAX_PATH
50 #endif
51 #include <stdarg.h>
52 #include "windef.h"
53 #include "winbase.h"
54 #include "winternl.h"
55
56 #include "wine/exception.h"
57 #include "wine/debug.h"
58 #include "dbghelp_private.h"
59 #include "wine/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     unsigned int i, j;
72     char        msg[128];
73     const char* hexof = "0123456789abcdef";
74     const BYTE* x = 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 struct cv_defined_module
104 {
105     BOOL                allowed;
106     unsigned int        num_defined_types;
107     struct symt**       defined_types;
108 };
109 /* FIXME: don't make it static */
110 #define CV_MAX_MODULES          32
111 static struct cv_defined_module cv_zmodules[CV_MAX_MODULES];
112 static struct cv_defined_module*cv_current_module;
113
114 static void codeview_init_basic_types(struct module* module)
115 {
116     /*
117      * These are the common builtin types that are used by VC++.
118      */
119     cv_basic_types[T_NOTYPE] = NULL;
120     cv_basic_types[T_ABS]    = NULL;
121     cv_basic_types[T_VOID]   = &symt_new_basic(module, btVoid,  "void", 0)->symt;
122     cv_basic_types[T_CHAR]   = &symt_new_basic(module, btChar,  "char", 1)->symt;
123     cv_basic_types[T_SHORT]  = &symt_new_basic(module, btInt,   "short int", 2)->symt;
124     cv_basic_types[T_LONG]   = &symt_new_basic(module, btInt,   "long int", 4)->symt;
125     cv_basic_types[T_QUAD]   = &symt_new_basic(module, btInt,   "long long int", 8)->symt;
126     cv_basic_types[T_UCHAR]  = &symt_new_basic(module, btUInt,  "unsigned char", 1)->symt;
127     cv_basic_types[T_USHORT] = &symt_new_basic(module, btUInt,  "unsigned short", 2)->symt;
128     cv_basic_types[T_ULONG]  = &symt_new_basic(module, btUInt,  "unsigned long", 4)->symt;
129     cv_basic_types[T_UQUAD]  = &symt_new_basic(module, btUInt,  "unsigned long long", 8)->symt;
130     cv_basic_types[T_BOOL08] = &symt_new_basic(module, btBool,  "BOOL08", 1)->symt;
131     cv_basic_types[T_BOOL16] = &symt_new_basic(module, btBool,  "BOOL16", 2)->symt;
132     cv_basic_types[T_BOOL32] = &symt_new_basic(module, btBool,  "BOOL32", 4)->symt;
133     cv_basic_types[T_BOOL64] = &symt_new_basic(module, btBool,  "BOOL64", 8)->symt;
134     cv_basic_types[T_REAL32] = &symt_new_basic(module, btFloat, "float", 4)->symt;
135     cv_basic_types[T_REAL64] = &symt_new_basic(module, btFloat, "double", 8)->symt;
136     cv_basic_types[T_REAL80] = &symt_new_basic(module, btFloat, "long double", 10)->symt;
137     cv_basic_types[T_RCHAR]  = &symt_new_basic(module, btInt,   "signed char", 1)->symt;
138     cv_basic_types[T_WCHAR]  = &symt_new_basic(module, btWChar, "wchar_t", 2)->symt;
139     cv_basic_types[T_INT2]   = &symt_new_basic(module, btInt,   "INT2", 2)->symt;
140     cv_basic_types[T_UINT2]  = &symt_new_basic(module, btUInt,  "UINT2", 2)->symt;
141     cv_basic_types[T_INT4]   = &symt_new_basic(module, btInt,   "INT4", 4)->symt;
142     cv_basic_types[T_UINT4]  = &symt_new_basic(module, btUInt,  "UINT4", 4)->symt;
143     cv_basic_types[T_INT8]   = &symt_new_basic(module, btInt,   "INT8", 8)->symt;
144     cv_basic_types[T_UINT8]  = &symt_new_basic(module, btUInt,  "UINT8", 8)->symt;
145     cv_basic_types[T_HRESULT]= &symt_new_basic(module, btUInt,  "HRESULT", 4)->symt;
146
147     cv_basic_types[T_32PVOID]   = &symt_new_pointer(module, cv_basic_types[T_VOID])->symt;
148     cv_basic_types[T_32PCHAR]   = &symt_new_pointer(module, cv_basic_types[T_CHAR])->symt;
149     cv_basic_types[T_32PSHORT]  = &symt_new_pointer(module, cv_basic_types[T_SHORT])->symt;
150     cv_basic_types[T_32PLONG]   = &symt_new_pointer(module, cv_basic_types[T_LONG])->symt;
151     cv_basic_types[T_32PQUAD]   = &symt_new_pointer(module, cv_basic_types[T_QUAD])->symt;
152     cv_basic_types[T_32PUCHAR]  = &symt_new_pointer(module, cv_basic_types[T_UCHAR])->symt;
153     cv_basic_types[T_32PUSHORT] = &symt_new_pointer(module, cv_basic_types[T_USHORT])->symt;
154     cv_basic_types[T_32PULONG]  = &symt_new_pointer(module, cv_basic_types[T_ULONG])->symt;
155     cv_basic_types[T_32PUQUAD]  = &symt_new_pointer(module, cv_basic_types[T_UQUAD])->symt;
156     cv_basic_types[T_32PBOOL08] = &symt_new_pointer(module, cv_basic_types[T_BOOL08])->symt;
157     cv_basic_types[T_32PBOOL16] = &symt_new_pointer(module, cv_basic_types[T_BOOL16])->symt;
158     cv_basic_types[T_32PBOOL32] = &symt_new_pointer(module, cv_basic_types[T_BOOL32])->symt;
159     cv_basic_types[T_32PBOOL64] = &symt_new_pointer(module, cv_basic_types[T_BOOL64])->symt;
160     cv_basic_types[T_32PREAL32] = &symt_new_pointer(module, cv_basic_types[T_REAL32])->symt;
161     cv_basic_types[T_32PREAL64] = &symt_new_pointer(module, cv_basic_types[T_REAL64])->symt;
162     cv_basic_types[T_32PREAL80] = &symt_new_pointer(module, cv_basic_types[T_REAL80])->symt;
163     cv_basic_types[T_32PRCHAR]  = &symt_new_pointer(module, cv_basic_types[T_RCHAR])->symt;
164     cv_basic_types[T_32PWCHAR]  = &symt_new_pointer(module, cv_basic_types[T_WCHAR])->symt;
165     cv_basic_types[T_32PINT2]   = &symt_new_pointer(module, cv_basic_types[T_INT2])->symt;
166     cv_basic_types[T_32PUINT2]  = &symt_new_pointer(module, cv_basic_types[T_UINT2])->symt;
167     cv_basic_types[T_32PINT4]   = &symt_new_pointer(module, cv_basic_types[T_INT4])->symt;
168     cv_basic_types[T_32PUINT4]  = &symt_new_pointer(module, cv_basic_types[T_UINT4])->symt;
169     cv_basic_types[T_32PINT8]   = &symt_new_pointer(module, cv_basic_types[T_INT8])->symt;
170     cv_basic_types[T_32PUINT8]  = &symt_new_pointer(module, cv_basic_types[T_UINT8])->symt;
171     cv_basic_types[T_32PHRESULT]= &symt_new_pointer(module, cv_basic_types[T_HRESULT])->symt;
172 }
173
174 static int leaf_as_variant(VARIANT* v, const unsigned short int* leaf)
175 {
176     unsigned short int type = *leaf++;
177     int length = 2;
178
179     if (type < LF_NUMERIC)
180     {
181         v->n1.n2.vt = VT_UINT;
182         v->n1.n2.n3.uintVal = type;
183     }
184     else
185     {
186         switch (type)
187         {
188         case LF_CHAR:
189             length += 1;
190             v->n1.n2.vt = VT_I1;
191             v->n1.n2.n3.cVal = *(const char*)leaf;
192             break;
193
194         case LF_SHORT:
195             length += 2;
196             v->n1.n2.vt = VT_I2;
197             v->n1.n2.n3.iVal = *(const short*)leaf;
198             break;
199
200         case LF_USHORT:
201             length += 2;
202             v->n1.n2.vt = VT_UI2;
203             v->n1.n2.n3.uiVal = *leaf;
204             break;
205
206         case LF_LONG:
207             length += 4;
208             v->n1.n2.vt = VT_I4;
209             v->n1.n2.n3.lVal = *(const int*)leaf;
210             break;
211
212         case LF_ULONG:
213             length += 4;
214             v->n1.n2.vt = VT_UI4;
215             v->n1.n2.n3.uiVal = *(const unsigned int*)leaf;
216             break;
217
218         case LF_QUADWORD:
219             length += 8;
220             v->n1.n2.vt = VT_I8;
221             v->n1.n2.n3.llVal = *(const long long int*)leaf;
222             break;
223
224         case LF_UQUADWORD:
225             length += 8;
226             v->n1.n2.vt = VT_UI8;
227             v->n1.n2.n3.ullVal = *(const long long unsigned int*)leaf;
228             break;
229
230         case LF_REAL32:
231             length += 4;
232             v->n1.n2.vt = VT_R4;
233             v->n1.n2.n3.fltVal = *(const float*)leaf;
234             break;
235
236         case LF_REAL48:
237             FIXME("Unsupported numeric leaf type %04x\n", type);
238             length += 6;
239             v->n1.n2.vt = VT_EMPTY;     /* FIXME */
240             break;
241
242         case LF_REAL64:
243             length += 8;
244             v->n1.n2.vt = VT_R8;
245             v->n1.n2.n3.fltVal = *(const double*)leaf;
246             break;
247
248         case LF_REAL80:
249             FIXME("Unsupported numeric leaf type %04x\n", type);
250             length += 10;
251             v->n1.n2.vt = VT_EMPTY;     /* FIXME */
252             break;
253
254         case LF_REAL128:
255             FIXME("Unsupported numeric leaf type %04x\n", type);
256             length += 16;
257             v->n1.n2.vt = VT_EMPTY;     /* FIXME */
258             break;
259
260         case LF_COMPLEX32:
261             FIXME("Unsupported numeric leaf type %04x\n", type);
262             length += 4;
263             v->n1.n2.vt = VT_EMPTY;     /* FIXME */
264             break;
265
266         case LF_COMPLEX64:
267             FIXME("Unsupported numeric leaf type %04x\n", type);
268             length += 8;
269             v->n1.n2.vt = VT_EMPTY;     /* FIXME */
270             break;
271
272         case LF_COMPLEX80:
273             FIXME("Unsupported numeric leaf type %04x\n", type);
274             length += 10;
275             v->n1.n2.vt = VT_EMPTY;     /* FIXME */
276             break;
277
278         case LF_COMPLEX128:
279             FIXME("Unsupported numeric leaf type %04x\n", type);
280             length += 16;
281             v->n1.n2.vt = VT_EMPTY;     /* FIXME */
282             break;
283
284         case LF_VARSTRING:
285             FIXME("Unsupported numeric leaf type %04x\n", type);
286             length += 2 + *leaf;
287             v->n1.n2.vt = VT_EMPTY;     /* FIXME */
288             break;
289
290         default:
291             FIXME("Unknown numeric leaf type %04x\n", type);
292             v->n1.n2.vt = VT_EMPTY;     /* FIXME */
293             break;
294         }
295     }
296
297     return length;
298 }
299
300 static int numeric_leaf(int* value, const unsigned short int* leaf)
301 {
302     unsigned short int type = *leaf++;
303     int length = 2;
304
305     if (type < LF_NUMERIC)
306     {
307         *value = type;
308     }
309     else
310     {
311         switch (type)
312         {
313         case LF_CHAR:
314             length += 1;
315             *value = *(const char*)leaf;
316             break;
317
318         case LF_SHORT:
319             length += 2;
320             *value = *(const short*)leaf;
321             break;
322
323         case LF_USHORT:
324             length += 2;
325             *value = *leaf;
326             break;
327
328         case LF_LONG:
329             length += 4;
330             *value = *(const int*)leaf;
331             break;
332
333         case LF_ULONG:
334             length += 4;
335             *value = *(const unsigned int*)leaf;
336             break;
337
338         case LF_QUADWORD:
339         case LF_UQUADWORD:
340             FIXME("Unsupported numeric leaf type %04x\n", type);
341             length += 8;
342             *value = 0;    /* FIXME */
343             break;
344
345         case LF_REAL32:
346             FIXME("Unsupported numeric leaf type %04x\n", type);
347             length += 4;
348             *value = 0;    /* FIXME */
349             break;
350
351         case LF_REAL48:
352             FIXME("Unsupported numeric leaf type %04x\n", type);
353             length += 6;
354             *value = 0;    /* FIXME */
355             break;
356
357         case LF_REAL64:
358             FIXME("Unsupported numeric leaf type %04x\n", type);
359             length += 8;
360             *value = 0;    /* FIXME */
361             break;
362
363         case LF_REAL80:
364             FIXME("Unsupported numeric leaf type %04x\n", type);
365             length += 10;
366             *value = 0;    /* FIXME */
367             break;
368
369         case LF_REAL128:
370             FIXME("Unsupported numeric leaf type %04x\n", type);
371             length += 16;
372             *value = 0;    /* FIXME */
373             break;
374
375         case LF_COMPLEX32:
376             FIXME("Unsupported numeric leaf type %04x\n", type);
377             length += 4;
378             *value = 0;    /* FIXME */
379             break;
380
381         case LF_COMPLEX64:
382             FIXME("Unsupported numeric leaf type %04x\n", type);
383             length += 8;
384             *value = 0;    /* FIXME */
385             break;
386
387         case LF_COMPLEX80:
388             FIXME("Unsupported numeric leaf type %04x\n", type);
389             length += 10;
390             *value = 0;    /* FIXME */
391             break;
392
393         case LF_COMPLEX128:
394             FIXME("Unsupported numeric leaf type %04x\n", type);
395             length += 16;
396             *value = 0;    /* FIXME */
397             break;
398
399         case LF_VARSTRING:
400             FIXME("Unsupported numeric leaf type %04x\n", type);
401             length += 2 + *leaf;
402             *value = 0;    /* FIXME */
403             break;
404
405         default:
406             FIXME("Unknown numeric leaf type %04x\n", type);
407             *value = 0;
408             break;
409         }
410     }
411
412     return length;
413 }
414
415 /* convert a pascal string (as stored in debug information) into
416  * a C string (null terminated).
417  */
418 static const char* terminate_string(const struct p_string* p_name)
419 {
420     static char symname[256];
421
422     memcpy(symname, p_name->name, p_name->namelen);
423     symname[p_name->namelen] = '\0';
424
425     return (!*symname || strcmp(symname, "__unnamed") == 0) ? NULL : symname;
426 }
427
428 static struct symt*  codeview_get_type(unsigned int typeno, BOOL quiet)
429 {
430     struct symt*        symt = NULL;
431
432     /*
433      * Convert Codeview type numbers into something we can grok internally.
434      * Numbers < FIRST_DEFINABLE_TYPE are all fixed builtin types.
435      * Numbers from FIRST_DEFINABLE_TYPE and up are all user defined (structs, etc).
436      */
437     if (typeno < FIRST_DEFINABLE_TYPE)
438     {
439         if (typeno < MAX_BUILTIN_TYPES)
440             symt = cv_basic_types[typeno];
441     }
442     else
443     {
444         unsigned        mod_index = typeno >> 24;
445         unsigned        mod_typeno = typeno & 0x00FFFFFF;
446         struct cv_defined_module*       mod;
447
448         mod = (mod_index == 0) ? cv_current_module : &cv_zmodules[mod_index];
449
450         if (mod_index >= CV_MAX_MODULES || !mod->allowed) 
451             FIXME("Module of index %d isn't loaded yet (%x)\n", mod_index, typeno);
452         else
453         {
454             if (mod_typeno - FIRST_DEFINABLE_TYPE < mod->num_defined_types)
455                 symt = mod->defined_types[mod_typeno - FIRST_DEFINABLE_TYPE];
456         }
457     }
458     if (!quiet && !symt && typeno) FIXME("Returning NULL symt for type-id %x\n", typeno);
459     return symt;
460 }
461
462 struct codeview_type_parse
463 {
464     struct module*      module;
465     const BYTE*         table;
466     const DWORD*        offset;
467     DWORD               num;
468 };
469
470 static inline const void* codeview_jump_to_type(const struct codeview_type_parse* ctp, DWORD idx)
471 {
472     if (idx < FIRST_DEFINABLE_TYPE) return NULL;
473     idx -= FIRST_DEFINABLE_TYPE;
474     return (idx >= ctp->num) ? NULL : (ctp->table + ctp->offset[idx]); 
475 }
476
477 static int codeview_add_type(unsigned int typeno, struct symt* dt)
478 {
479     if (typeno < FIRST_DEFINABLE_TYPE)
480         FIXME("What the heck\n");
481     if (!cv_current_module)
482     {
483         FIXME("Adding %x to non allowed module\n", typeno);
484         return FALSE;
485     }
486     if ((typeno >> 24) != 0)
487         FIXME("No module index while inserting type-id assumption is wrong %x\n",
488               typeno);
489     while (typeno - FIRST_DEFINABLE_TYPE >= cv_current_module->num_defined_types)
490     {
491         cv_current_module->num_defined_types += 0x100;
492         if (cv_current_module->defined_types)
493             cv_current_module->defined_types = HeapReAlloc(GetProcessHeap(),
494                             HEAP_ZERO_MEMORY, cv_current_module->defined_types,
495                             cv_current_module->num_defined_types * sizeof(struct symt*));
496         else
497             cv_current_module->defined_types = HeapAlloc(GetProcessHeap(),
498                             HEAP_ZERO_MEMORY,
499                             cv_current_module->num_defined_types * sizeof(struct symt*));
500
501         if (cv_current_module->defined_types == NULL) return FALSE;
502     }
503     if (cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE])
504     {
505         if (cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE] != dt)
506             FIXME("Overwriting at %x\n", typeno);
507     }
508     cv_current_module->defined_types[typeno - FIRST_DEFINABLE_TYPE] = dt;
509     return TRUE;
510 }
511
512 static void codeview_clear_type_table(void)
513 {
514     int i;
515
516     for (i = 0; i < CV_MAX_MODULES; i++)
517     {
518         if (cv_zmodules[i].allowed)
519             HeapFree(GetProcessHeap(), 0, cv_zmodules[i].defined_types);
520         cv_zmodules[i].allowed = FALSE;
521         cv_zmodules[i].defined_types = NULL;
522         cv_zmodules[i].num_defined_types = 0;
523     }
524     cv_current_module = NULL;
525 }
526
527 static struct symt* codeview_parse_one_type(struct codeview_type_parse* ctp,
528                                             unsigned curr_type,
529                                             const union codeview_type* type, BOOL details);
530
531 static void* codeview_cast_symt(struct symt* symt, enum SymTagEnum tag)
532 {
533     if (symt->tag != tag)
534     {
535         FIXME("Bad tag. Expected %d, but got %d\n", tag, symt->tag);
536         return NULL;
537     }   
538     return symt;
539 }
540
541 static struct symt* codeview_fetch_type(struct codeview_type_parse* ctp,
542                                         unsigned typeno, BOOL details)
543 {
544     struct symt*                symt;
545     const union codeview_type*  p;
546
547     if (!typeno) return NULL;
548     if ((symt = codeview_get_type(typeno, TRUE))) return symt;
549
550     /* forward declaration */
551     if (!(p = codeview_jump_to_type(ctp, typeno)))
552     {
553         FIXME("Cannot locate type %x\n", typeno);
554         return NULL;
555     }
556     symt = codeview_parse_one_type(ctp, typeno, p, details);
557     if (!symt) FIXME("Couldn't load forward type %x\n", typeno);
558     return symt;
559 }
560
561 static struct symt* codeview_add_type_pointer(struct codeview_type_parse* ctp,
562                                               struct symt* existing,
563                                               unsigned int pointee_type)
564 {
565     struct symt* pointee;
566
567     if (existing)
568     {
569         existing = codeview_cast_symt(existing, SymTagPointerType);
570         return existing;
571     }
572     pointee = codeview_fetch_type(ctp, pointee_type, FALSE);
573     return &symt_new_pointer(ctp->module, pointee)->symt;
574 }
575
576 static struct symt* codeview_add_type_array(struct codeview_type_parse* ctp, 
577                                             const char* name,
578                                             unsigned int elemtype,
579                                             unsigned int indextype,
580                                             unsigned int arr_len)
581 {
582     struct symt*        elem = codeview_fetch_type(ctp, elemtype, FALSE);
583     struct symt*        index = codeview_fetch_type(ctp, indextype, FALSE);
584     DWORD               arr_max = 0;
585
586     if (elem)
587     {
588         DWORD64 elem_size;
589         symt_get_info(elem, TI_GET_LENGTH, &elem_size);
590         if (elem_size) arr_max = arr_len / (DWORD)elem_size;
591     }
592     return &symt_new_array(ctp->module, 0, arr_max, elem, index)->symt;
593 }
594
595 static int codeview_add_type_enum_field_list(struct module* module,
596                                              struct symt_enum* symt,
597                                              const union codeview_reftype* ref_type)
598 {
599     const unsigned char*                ptr = ref_type->fieldlist.list;
600     const unsigned char*                last = (const BYTE*)ref_type + ref_type->generic.len + 2;
601     const union codeview_fieldtype*     type;
602
603     while (ptr < last)
604     {
605         if (*ptr >= 0xf0)       /* LF_PAD... */
606         {
607             ptr += *ptr & 0x0f;
608             continue;
609         }
610
611         type = (const union codeview_fieldtype*)ptr;
612
613         switch (type->generic.id)
614         {
615         case LF_ENUMERATE_V1:
616         {
617             int value, vlen = numeric_leaf(&value, &type->enumerate_v1.value);
618             const struct p_string* p_name = (const struct p_string*)((const unsigned char*)&type->enumerate_v1.value + vlen);
619
620             symt_add_enum_element(module, symt, terminate_string(p_name), value);
621             ptr += 2 + 2 + vlen + (1 + p_name->namelen);
622             break;
623         }
624         case LF_ENUMERATE_V3:
625         {
626             int value, vlen = numeric_leaf(&value, &type->enumerate_v3.value);
627             const char* name = (const char*)&type->enumerate_v3.value + vlen;
628
629             symt_add_enum_element(module, symt, name, value);
630             ptr += 2 + 2 + vlen + (1 + strlen(name));
631             break;
632         }
633
634         default:
635             FIXME("Unsupported type %04x in ENUM field list\n", type->generic.id);
636             return FALSE;
637         }
638     }
639     return TRUE;
640 }
641
642 static void codeview_add_udt_element(struct codeview_type_parse* ctp,
643                                      struct symt_udt* symt, const char* name,
644                                      int value, unsigned type)
645 {
646     struct symt*                subtype;
647     const union codeview_reftype*cv_type;
648
649     if ((cv_type = codeview_jump_to_type(ctp, type)))
650     {
651         switch (cv_type->generic.id)
652         {
653         case LF_BITFIELD_V1:
654             symt_add_udt_element(ctp->module, symt, name,
655                                  codeview_fetch_type(ctp, cv_type->bitfield_v1.type, FALSE),
656                                  (value << 3) + cv_type->bitfield_v1.bitoff,
657                                  cv_type->bitfield_v1.nbits);
658             return;
659         case LF_BITFIELD_V2:
660             symt_add_udt_element(ctp->module, symt, name,
661                                  codeview_fetch_type(ctp, cv_type->bitfield_v2.type, FALSE),
662                                  (value << 3) + cv_type->bitfield_v2.bitoff,
663                                  cv_type->bitfield_v2.nbits);
664             return;
665         }
666     }
667     subtype = codeview_fetch_type(ctp, type, FALSE);
668
669     if (subtype)
670     {
671         DWORD64 elem_size = 0;
672         symt_get_info(subtype, TI_GET_LENGTH, &elem_size);
673         symt_add_udt_element(ctp->module, symt, name, subtype,
674                              value << 3, (DWORD)elem_size << 3);
675     }
676 }
677
678 static int codeview_add_type_struct_field_list(struct codeview_type_parse* ctp,
679                                                struct symt_udt* symt,
680                                                unsigned fieldlistno)
681 {
682     const unsigned char*        ptr;
683     const unsigned char*        last;
684     int                         value, leaf_len;
685     const struct p_string*      p_name;
686     const char*                 c_name;
687     const union codeview_reftype*type_ref;
688     const union codeview_fieldtype* type;
689
690     if (!fieldlistno) return TRUE;
691     type_ref = codeview_jump_to_type(ctp, fieldlistno);
692     ptr = type_ref->fieldlist.list;
693     last = (const BYTE*)type_ref + type_ref->generic.len + 2;
694
695     while (ptr < last)
696     {
697         if (*ptr >= 0xf0)       /* LF_PAD... */
698         {
699             ptr += *ptr & 0x0f;
700             continue;
701         }
702
703         type = (const union codeview_fieldtype*)ptr;
704
705         switch (type->generic.id)
706         {
707         case LF_BCLASS_V1:
708             leaf_len = numeric_leaf(&value, &type->bclass_v1.offset);
709
710             /* FIXME: ignored for now */
711
712             ptr += 2 + 2 + 2 + leaf_len;
713             break;
714
715         case LF_BCLASS_V2:
716             leaf_len = numeric_leaf(&value, &type->bclass_v2.offset);
717
718             /* FIXME: ignored for now */
719
720             ptr += 2 + 2 + 4 + leaf_len;
721             break;
722
723         case LF_VBCLASS_V1:
724         case LF_IVBCLASS_V1:
725             {
726                 const unsigned short int* p_vboff;
727                 int vpoff, vplen;
728                 leaf_len = numeric_leaf(&value, &type->vbclass_v1.vbpoff);
729                 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v1.vbpoff + leaf_len);
730                 vplen = numeric_leaf(&vpoff, p_vboff);
731
732                 /* FIXME: ignored for now */
733
734                 ptr += 2 + 2 + 2 + 2 + leaf_len + vplen;
735             }
736             break;
737
738         case LF_VBCLASS_V2:
739         case LF_IVBCLASS_V2:
740             {
741                 const unsigned short int* p_vboff;
742                 int vpoff, vplen;
743                 leaf_len = numeric_leaf(&value, &type->vbclass_v2.vbpoff);
744                 p_vboff = (const unsigned short int*)((const char*)&type->vbclass_v2.vbpoff + leaf_len);
745                 vplen = numeric_leaf(&vpoff, p_vboff);
746
747                 /* FIXME: ignored for now */
748
749                 ptr += 2 + 2 + 4 + 4 + leaf_len + vplen;
750             }
751             break;
752
753         case LF_MEMBER_V1:
754             leaf_len = numeric_leaf(&value, &type->member_v1.offset);
755             p_name = (const struct p_string*)((const char*)&type->member_v1.offset + leaf_len);
756
757             codeview_add_udt_element(ctp, symt, terminate_string(p_name), value, 
758                                      type->member_v1.type);
759
760             ptr += 2 + 2 + 2 + leaf_len + (1 + p_name->namelen);
761             break;
762
763         case LF_MEMBER_V2:
764             leaf_len = numeric_leaf(&value, &type->member_v2.offset);
765             p_name = (const struct p_string*)((const unsigned char*)&type->member_v2.offset + leaf_len);
766
767             codeview_add_udt_element(ctp, symt, terminate_string(p_name), value, 
768                                      type->member_v2.type);
769
770             ptr += 2 + 2 + 4 + leaf_len + (1 + p_name->namelen);
771             break;
772
773         case LF_MEMBER_V3:
774             leaf_len = numeric_leaf(&value, &type->member_v3.offset);
775             c_name = (const char*)&type->member_v3.offset + leaf_len;
776
777             codeview_add_udt_element(ctp, symt, c_name, value, type->member_v3.type);
778
779             ptr += 2 + 2 + 4 + leaf_len + (strlen(c_name) + 1);
780             break;
781
782         case LF_STMEMBER_V1:
783             /* FIXME: ignored for now */
784             ptr += 2 + 2 + 2 + (1 + type->stmember_v1.p_name.namelen);
785             break;
786
787         case LF_STMEMBER_V2:
788             /* FIXME: ignored for now */
789             ptr += 2 + 4 + 2 + (1 + type->stmember_v2.p_name.namelen);
790             break;
791
792         case LF_STMEMBER_V3:
793             /* FIXME: ignored for now */
794             ptr += 2 + 4 + 2 + (strlen(type->stmember_v3.name) + 1);
795             break;
796
797         case LF_METHOD_V1:
798             /* FIXME: ignored for now */
799             ptr += 2 + 2 + 2 + (1 + type->method_v1.p_name.namelen);
800             break;
801
802         case LF_METHOD_V2:
803             /* FIXME: ignored for now */
804             ptr += 2 + 2 + 4 + (1 + type->method_v2.p_name.namelen);
805             break;
806
807         case LF_METHOD_V3:
808             /* FIXME: ignored for now */
809             ptr += 2 + 2 + 4 + (strlen(type->method_v3.name) + 1);
810             break;
811
812         case LF_NESTTYPE_V1:
813             /* FIXME: ignored for now */
814             ptr += 2 + 2 + (1 + type->nesttype_v1.p_name.namelen);
815             break;
816
817         case LF_NESTTYPE_V2:
818             /* FIXME: ignored for now */
819             ptr += 2 + 2 + 4 + (1 + type->nesttype_v2.p_name.namelen);
820             break;
821
822         case LF_NESTTYPE_V3:
823             /* FIXME: ignored for now */
824             ptr += 2 + 2 + 4 + (strlen(type->nesttype_v3.name) + 1);
825             break;
826
827         case LF_VFUNCTAB_V1:
828             /* FIXME: ignored for now */
829             ptr += 2 + 2;
830             break;
831
832         case LF_VFUNCTAB_V2:
833             /* FIXME: ignored for now */
834             ptr += 2 + 2 + 4;
835             break;
836
837         case LF_ONEMETHOD_V1:
838             /* FIXME: ignored for now */
839             switch ((type->onemethod_v1.attribute >> 2) & 7)
840             {
841             case 4: case 6: /* (pure) introducing virtual method */
842                 ptr += 2 + 2 + 2 + 4 + (1 + type->onemethod_virt_v1.p_name.namelen);
843                 break;
844
845             default:
846                 ptr += 2 + 2 + 2 + (1 + type->onemethod_v1.p_name.namelen);
847                 break;
848             }
849             break;
850
851         case LF_ONEMETHOD_V2:
852             /* FIXME: ignored for now */
853             switch ((type->onemethod_v2.attribute >> 2) & 7)
854             {
855             case 4: case 6: /* (pure) introducing virtual method */
856                 ptr += 2 + 2 + 4 + 4 + (1 + type->onemethod_virt_v2.p_name.namelen);
857                 break;
858
859             default:
860                 ptr += 2 + 2 + 4 + (1 + type->onemethod_v2.p_name.namelen);
861                 break;
862             }
863             break;
864
865         case LF_ONEMETHOD_V3:
866             /* FIXME: ignored for now */
867             switch ((type->onemethod_v3.attribute >> 2) & 7)
868             {
869             case 4: case 6: /* (pure) introducing virtual method */
870                 ptr += 2 + 2 + 4 + 4 + (strlen(type->onemethod_virt_v3.name) + 1);
871                 break;
872
873             default:
874                 ptr += 2 + 2 + 4 + (strlen(type->onemethod_v3.name) + 1);
875                 break;
876             }
877             break;
878
879         default:
880             FIXME("Unsupported type %04x in STRUCT field list\n", type->generic.id);
881             return FALSE;
882         }
883     }
884
885     return TRUE;
886 }
887
888 static struct symt* codeview_add_type_enum(struct codeview_type_parse* ctp,
889                                            struct symt* existing,
890                                            const char* name,
891                                            unsigned fieldlistno,
892                                            unsigned basetype)
893 {
894     struct symt_enum*   symt;
895
896     if (existing)
897     {
898         if (!(symt = codeview_cast_symt(existing, SymTagEnum))) return NULL;
899         /* should also check that all fields are the same */
900     }
901     else
902     {
903         symt = symt_new_enum(ctp->module, name,
904                              codeview_fetch_type(ctp, basetype, FALSE));
905         if (fieldlistno)
906         {
907             const union codeview_reftype* fieldlist;
908             fieldlist = codeview_jump_to_type(ctp, fieldlistno);
909             codeview_add_type_enum_field_list(ctp->module, symt, fieldlist);
910         }
911     }
912     return &symt->symt;
913 }
914
915 static struct symt* codeview_add_type_struct(struct codeview_type_parse* ctp,
916                                              struct symt* existing,
917                                              const char* name, int structlen,
918                                              enum UdtKind kind, unsigned property)
919 {
920     struct symt_udt*    symt;
921
922     /* if we don't have an existing type, try to find one with same name
923      * FIXME: what to do when several types in different CUs have same name ?
924      */
925     if (!existing)
926     {
927         void*                       ptr;
928         struct symt_ht*             type;
929         struct hash_table_iter      hti;
930
931         hash_table_iter_init(&ctp->module->ht_types, &hti, name);
932         while ((ptr = hash_table_iter_up(&hti)))
933         {
934             type = GET_ENTRY(ptr, struct symt_ht, hash_elt);
935
936             if (type->symt.tag == SymTagUDT &&
937                 type->hash_elt.name && !strcmp(type->hash_elt.name, name))
938             {
939                 existing = &type->symt;
940                 break;
941             }
942         }
943     }
944     if (existing)
945     {
946         if (!(symt = codeview_cast_symt(existing, SymTagUDT))) return NULL;
947         /* should also check that all fields are the same */
948         if (!(property & 0x80)) /* 0x80 = forward declaration */
949         {
950             if (!symt->size) /* likely prior forward declaration, set UDT size */
951                 symt_set_udt_size(ctp->module, symt, structlen);
952             else /* different UDT with same name, create a new type */
953                 existing = NULL;
954         }
955     }
956     if (!existing) symt = symt_new_udt(ctp->module, name, structlen, kind);
957
958     return &symt->symt;
959 }
960
961 static struct symt* codeview_new_func_signature(struct codeview_type_parse* ctp, 
962                                                 struct symt* existing,
963                                                 enum CV_call_e call_conv)
964 {
965     struct symt_function_signature*     sym;
966
967     if (existing)
968     {
969         sym = codeview_cast_symt(existing, SymTagFunctionType);
970         if (!sym) return NULL;
971     }
972     else
973     {
974         sym = symt_new_function_signature(ctp->module, NULL, call_conv);
975     }
976     return &sym->symt;
977 }
978
979 static void codeview_add_func_signature_args(struct codeview_type_parse* ctp,
980                                              struct symt_function_signature* sym,
981                                              unsigned ret_type,
982                                              unsigned args_list)
983 {
984     const union codeview_reftype*       reftype;
985
986     sym->rettype = codeview_fetch_type(ctp, ret_type, FALSE);
987     if (args_list && (reftype = codeview_jump_to_type(ctp, args_list)))
988     {
989         unsigned int i;
990         switch (reftype->generic.id)
991         {
992         case LF_ARGLIST_V1:
993             for (i = 0; i < reftype->arglist_v1.num; i++)
994                 symt_add_function_signature_parameter(ctp->module, sym,
995                                                       codeview_fetch_type(ctp, reftype->arglist_v1.args[i], FALSE));
996             break;
997         case LF_ARGLIST_V2:
998             for (i = 0; i < reftype->arglist_v2.num; i++)
999                 symt_add_function_signature_parameter(ctp->module, sym,
1000                                                       codeview_fetch_type(ctp, reftype->arglist_v2.args[i], FALSE));
1001             break;
1002         default:
1003             FIXME("Unexpected leaf %x for signature's pmt\n", reftype->generic.id);
1004         }
1005     }
1006 }
1007
1008 static struct symt* codeview_parse_one_type(struct codeview_type_parse* ctp,
1009                                             unsigned curr_type,
1010                                             const union codeview_type* type, BOOL details)
1011 {
1012     struct symt*                symt;
1013     int                         value, leaf_len;
1014     const struct p_string*      p_name;
1015     const char*                 c_name;
1016     struct symt*                existing;
1017
1018     existing = codeview_get_type(curr_type, TRUE);
1019
1020     switch (type->generic.id)
1021     {
1022     case LF_MODIFIER_V1:
1023         /* FIXME: we don't handle modifiers,
1024          * but read previous type on the curr_type
1025          */
1026         WARN("Modifier on %x: %s%s%s%s\n",
1027              type->modifier_v1.type,
1028              type->modifier_v1.attribute & 0x01 ? "const " : "",
1029              type->modifier_v1.attribute & 0x02 ? "volatile " : "",
1030              type->modifier_v1.attribute & 0x04 ? "unaligned " : "",
1031              type->modifier_v1.attribute & ~0x07 ? "unknown " : "");
1032         symt = codeview_fetch_type(ctp, type->modifier_v1.type, details);
1033         break;
1034     case LF_MODIFIER_V2:
1035         /* FIXME: we don't handle modifiers, but readd previous type on the curr_type */
1036         WARN("Modifier on %x: %s%s%s%s\n",
1037              type->modifier_v2.type,
1038              type->modifier_v2.attribute & 0x01 ? "const " : "",
1039              type->modifier_v2.attribute & 0x02 ? "volatile " : "",
1040              type->modifier_v2.attribute & 0x04 ? "unaligned " : "",
1041              type->modifier_v2.attribute & ~0x07 ? "unknown " : "");
1042         symt = codeview_fetch_type(ctp, type->modifier_v2.type, details);
1043         break;
1044
1045     case LF_POINTER_V1:
1046         symt = codeview_add_type_pointer(ctp, existing, type->pointer_v1.datatype);
1047         break;
1048     case LF_POINTER_V2:
1049         symt = codeview_add_type_pointer(ctp, existing, type->pointer_v2.datatype);
1050         break;
1051
1052     case LF_ARRAY_V1:
1053         if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
1054         else
1055         {
1056             leaf_len = numeric_leaf(&value, &type->array_v1.arrlen);
1057             p_name = (const struct p_string*)((const unsigned char*)&type->array_v1.arrlen + leaf_len);
1058             symt = codeview_add_type_array(ctp, terminate_string(p_name),
1059                                            type->array_v1.elemtype,
1060                                            type->array_v1.idxtype, value);
1061         }
1062         break;
1063     case LF_ARRAY_V2:
1064         if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
1065         else
1066         {
1067             leaf_len = numeric_leaf(&value, &type->array_v2.arrlen);
1068             p_name = (const struct p_string*)((const unsigned char*)&type->array_v2.arrlen + leaf_len);
1069
1070             symt = codeview_add_type_array(ctp, terminate_string(p_name),
1071                                            type->array_v2.elemtype,
1072                                            type->array_v2.idxtype, value);
1073         }
1074         break;
1075     case LF_ARRAY_V3:
1076         if (existing) symt = codeview_cast_symt(existing, SymTagArrayType);
1077         else
1078         {
1079             leaf_len = numeric_leaf(&value, &type->array_v3.arrlen);
1080             c_name = (const char*)&type->array_v3.arrlen + leaf_len;
1081
1082             symt = codeview_add_type_array(ctp, c_name,
1083                                            type->array_v3.elemtype,
1084                                            type->array_v3.idxtype, value);
1085         }
1086         break;
1087
1088     case LF_STRUCTURE_V1:
1089     case LF_CLASS_V1:
1090         leaf_len = numeric_leaf(&value, &type->struct_v1.structlen);
1091         p_name = (const struct p_string*)((const unsigned char*)&type->struct_v1.structlen + leaf_len);
1092         symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name), value,
1093                                         type->generic.id == LF_CLASS_V1 ? UdtClass : UdtStruct,
1094                                         type->struct_v1.property);
1095         if (details)
1096         {
1097             codeview_add_type(curr_type, symt);
1098             codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt, 
1099                                                 type->struct_v1.fieldlist);
1100         }
1101         break;
1102
1103     case LF_STRUCTURE_V2:
1104     case LF_CLASS_V2:
1105         leaf_len = numeric_leaf(&value, &type->struct_v2.structlen);
1106         p_name = (const struct p_string*)((const unsigned char*)&type->struct_v2.structlen + leaf_len);
1107         symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name), value,
1108                                         type->generic.id == LF_CLASS_V2 ? UdtClass : UdtStruct,
1109                                         type->struct_v2.property);
1110         if (details)
1111         {
1112             codeview_add_type(curr_type, symt);
1113             codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1114                                                 type->struct_v2.fieldlist);
1115         }
1116         break;
1117
1118     case LF_STRUCTURE_V3:
1119     case LF_CLASS_V3:
1120         leaf_len = numeric_leaf(&value, &type->struct_v3.structlen);
1121         c_name = (const char*)&type->struct_v3.structlen + leaf_len;
1122         symt = codeview_add_type_struct(ctp, existing, c_name, value,
1123                                         type->generic.id == LF_CLASS_V3 ? UdtClass : UdtStruct,
1124                                         type->struct_v3.property);
1125         if (details)
1126         {
1127             codeview_add_type(curr_type, symt);
1128             codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1129                                                 type->struct_v3.fieldlist);
1130         }
1131         break;
1132
1133     case LF_UNION_V1:
1134         leaf_len = numeric_leaf(&value, &type->union_v1.un_len);
1135         p_name = (const struct p_string*)((const unsigned char*)&type->union_v1.un_len + leaf_len);
1136         symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name),
1137                                         value, UdtUnion, type->union_v1.property);
1138         if (details)
1139         {
1140             codeview_add_type(curr_type, symt);
1141             codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1142                                                 type->union_v1.fieldlist);
1143         }
1144         break;
1145
1146     case LF_UNION_V2:
1147         leaf_len = numeric_leaf(&value, &type->union_v2.un_len);
1148         p_name = (const struct p_string*)((const unsigned char*)&type->union_v2.un_len + leaf_len);
1149         symt = codeview_add_type_struct(ctp, existing, terminate_string(p_name),
1150                                         value, UdtUnion, type->union_v2.property);
1151         if (details)
1152         {
1153             codeview_add_type(curr_type, symt);
1154             codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1155                                                 type->union_v2.fieldlist);
1156         }
1157         break;
1158
1159     case LF_UNION_V3:
1160         leaf_len = numeric_leaf(&value, &type->union_v3.un_len);
1161         c_name = (const char*)&type->union_v3.un_len + leaf_len;
1162         symt = codeview_add_type_struct(ctp, existing, c_name,
1163                                         value, UdtUnion, type->union_v3.property);
1164         if (details)
1165         {
1166             codeview_add_type(curr_type, symt);
1167             codeview_add_type_struct_field_list(ctp, (struct symt_udt*)symt,
1168                                                 type->union_v3.fieldlist);
1169         }
1170         break;
1171
1172     case LF_ENUM_V1:
1173         symt = codeview_add_type_enum(ctp, existing,
1174                                       terminate_string(&type->enumeration_v1.p_name),
1175                                       type->enumeration_v1.fieldlist,
1176                                       type->enumeration_v1.type);
1177         break;
1178
1179     case LF_ENUM_V2:
1180         symt = codeview_add_type_enum(ctp, existing,
1181                                       terminate_string(&type->enumeration_v2.p_name),
1182                                       type->enumeration_v2.fieldlist,
1183                                       type->enumeration_v2.type);
1184         break;
1185
1186     case LF_ENUM_V3:
1187         symt = codeview_add_type_enum(ctp, existing, type->enumeration_v3.name,
1188                                       type->enumeration_v3.fieldlist,
1189                                       type->enumeration_v3.type);
1190         break;
1191
1192     case LF_PROCEDURE_V1:
1193         symt = codeview_new_func_signature(ctp, existing, type->procedure_v1.call);
1194         if (details)
1195         {
1196             codeview_add_type(curr_type, symt);
1197             codeview_add_func_signature_args(ctp,
1198                                              (struct symt_function_signature*)symt,
1199                                              type->procedure_v1.rvtype,
1200                                              type->procedure_v1.arglist);
1201         }
1202         break;
1203     case LF_PROCEDURE_V2:
1204         symt = codeview_new_func_signature(ctp, existing,type->procedure_v2.call);
1205         if (details)
1206         {
1207             codeview_add_type(curr_type, symt);
1208             codeview_add_func_signature_args(ctp,
1209                                              (struct symt_function_signature*)symt,
1210                                              type->procedure_v2.rvtype,
1211                                              type->procedure_v2.arglist);
1212         }
1213         break;
1214
1215     case LF_MFUNCTION_V1:
1216         /* FIXME: for C++, this is plain wrong, but as we don't use arg types
1217          * nor class information, this would just do for now
1218          */
1219         symt = codeview_new_func_signature(ctp, existing, type->mfunction_v1.call);
1220         if (details)
1221         {
1222             codeview_add_type(curr_type, symt);
1223             codeview_add_func_signature_args(ctp,
1224                                              (struct symt_function_signature*)symt,
1225                                              type->mfunction_v1.rvtype,
1226                                              type->mfunction_v1.arglist);
1227         }
1228         break;
1229     case LF_MFUNCTION_V2:
1230         /* FIXME: for C++, this is plain wrong, but as we don't use arg types
1231          * nor class information, this would just do for now
1232          */
1233         symt = codeview_new_func_signature(ctp, existing, type->mfunction_v2.call);
1234         if (details)
1235         {
1236             codeview_add_type(curr_type, symt);
1237             codeview_add_func_signature_args(ctp,
1238                                              (struct symt_function_signature*)symt,
1239                                              type->mfunction_v2.rvtype,
1240                                              type->mfunction_v2.arglist);
1241         }
1242         break;
1243
1244     case LF_VTSHAPE_V1:
1245         /* this is an ugly hack... FIXME when we have C++ support */
1246         if (!(symt = existing))
1247         {
1248             char    buf[128];
1249             snprintf(buf, sizeof(buf), "__internal_vt_shape_%x\n", curr_type);
1250             symt = &symt_new_udt(ctp->module, buf, 0, UdtStruct)->symt;
1251         }
1252         break;
1253     default:
1254         FIXME("Unsupported type-id leaf %x\n", type->generic.id);
1255         dump(type, 2 + type->generic.len);
1256         return FALSE;
1257     }
1258     return codeview_add_type(curr_type, symt) ? symt : NULL;
1259 }
1260
1261 static int codeview_parse_type_table(struct codeview_type_parse* ctp)
1262 {
1263     unsigned int                curr_type = FIRST_DEFINABLE_TYPE;
1264     const union codeview_type*  type;
1265
1266     for (curr_type = FIRST_DEFINABLE_TYPE; curr_type < FIRST_DEFINABLE_TYPE + ctp->num; curr_type++)
1267     {
1268         type = codeview_jump_to_type(ctp, curr_type);
1269
1270         /* type records we're interested in are the ones referenced by symbols
1271          * The known ranges are (X mark the ones we want):
1272          *   X  0000-0016       for V1 types
1273          *      0200-020c       for V1 types referenced by other types
1274          *      0400-040f       for V1 types (complex lists & sets)
1275          *   X  1000-100f       for V2 types
1276          *      1200-120c       for V2 types referenced by other types
1277          *      1400-140f       for V1 types (complex lists & sets)
1278          *   X  1500-150d       for V3 types
1279          *      8000-8010       for numeric leafes
1280          */
1281         if (!(type->generic.id & 0x8600) || (type->generic.id & 0x0100))
1282             codeview_parse_one_type(ctp, curr_type, type, TRUE);
1283     }
1284
1285     return TRUE;
1286 }
1287
1288 /*========================================================================
1289  * Process CodeView line number information.
1290  */
1291 static unsigned codeview_get_address(const struct msc_debug_info* msc_dbg,
1292                                      unsigned seg, unsigned offset);
1293
1294 static void codeview_snarf_linetab(const struct msc_debug_info* msc_dbg, const BYTE* linetab,
1295                                    int size, BOOL pascal_str)
1296 {
1297     const BYTE*                 ptr = linetab;
1298     int                         nfile, nseg;
1299     int                         i, j, k;
1300     const unsigned int*         filetab;
1301     const unsigned int*         lt_ptr;
1302     const unsigned short*       linenos;
1303     const struct startend*      start;
1304     unsigned                    source;
1305     unsigned                    addr, func_addr0;
1306     struct symt_function*       func;
1307     const struct codeview_linetab_block* ltb;
1308
1309     nfile = *(const short*)linetab;
1310     filetab = (const unsigned int*)(linetab + 2 * sizeof(short));
1311
1312     for (i = 0; i < nfile; i++)
1313     {
1314         ptr = linetab + filetab[i];
1315         nseg = *(const short*)ptr;
1316         lt_ptr = (const unsigned int*)(ptr + 2 * sizeof(short));
1317         start = (const struct startend*)(lt_ptr + nseg);
1318
1319         /*
1320          * Now snarf the filename for all of the segments for this file.
1321          */
1322         if (pascal_str)
1323             source = source_new(msc_dbg->module, NULL, terminate_string((const struct p_string*)(start + nseg)));
1324         else
1325             source = source_new(msc_dbg->module, NULL, (const char*)(start + nseg));
1326
1327         for (j = 0; j < nseg; j++)
1328         {
1329             ltb = (const struct codeview_linetab_block*)(linetab + *lt_ptr++);
1330             linenos = (const unsigned short*)&ltb->offsets[ltb->num_lines];
1331             func_addr0 = codeview_get_address(msc_dbg, ltb->seg, start[j].start);
1332             if (!func_addr0) continue;
1333             for (func = NULL, k = 0; k < ltb->num_lines; k++)
1334             {
1335                 /* now locate function (if any) */
1336                 addr = func_addr0 + ltb->offsets[k] - start[j].start;
1337                 /* unfortunetaly, we can have several functions in the same block, if there's no
1338                  * gap between them... find the new function if needed
1339                  */
1340                 if (!func || addr >= func->address + func->size)
1341                 {
1342                     func = (struct symt_function*)symt_find_nearest(msc_dbg->module, addr);
1343                     /* FIXME: at least labels support line numbers */
1344                     if (!func || func->symt.tag != SymTagFunction)
1345                     {
1346                         WARN("--not a func at %04x:%08x %x tag=%d\n",
1347                              ltb->seg, ltb->offsets[k], addr, func ? func->symt.tag : -1);
1348                         func = NULL;
1349                         break;
1350                     }
1351                 }
1352                 symt_add_func_line(msc_dbg->module, func, source,
1353                                    linenos[k], addr - func->address);
1354             }
1355         }
1356     }
1357 }
1358
1359 static void codeview_snarf_linetab2(const struct msc_debug_info* msc_dbg, const BYTE* linetab, DWORD size,
1360                                      const char* strimage, DWORD strsize)
1361 {
1362     DWORD       offset;
1363     unsigned    i;
1364     DWORD       addr;
1365     const struct codeview_linetab2_block* lbh;
1366     const struct codeview_linetab2_file* fd;
1367     unsigned    source;
1368     struct symt_function* func;
1369
1370     if (*(const DWORD*)linetab != 0x000000f4) return;
1371     offset = *((const DWORD*)linetab + 1);
1372
1373     for (lbh = (const struct codeview_linetab2_block*)(linetab + 8 + offset);
1374          (const BYTE*)lbh < linetab + size;
1375          lbh = (const struct codeview_linetab2_block*)((const char*)lbh + 8 + lbh->size_of_block))
1376     {
1377         if (lbh->header != 0x000000f2)
1378         /* FIXME: should also check that whole lbh fits in linetab + size */
1379         {
1380             TRACE("block end %x\n", lbh->header);
1381             break;
1382         }
1383         addr = codeview_get_address(msc_dbg, lbh->seg, lbh->start);
1384         TRACE("block from %04x:%08x #%x (%x lines)\n",
1385                lbh->seg, lbh->start, lbh->size, lbh->nlines);
1386         fd = (const struct codeview_linetab2_file*)(linetab + 8 + lbh->file_offset);
1387         /* FIXME: should check that string is within strimage + strsize */
1388         source = source_new(msc_dbg->module, NULL, strimage + fd->offset);
1389         func = (struct symt_function*)symt_find_nearest(msc_dbg->module, addr);
1390         /* FIXME: at least labels support line numbers */
1391         if (!func || func->symt.tag != SymTagFunction)
1392         {
1393             WARN("--not a func at %04x:%08x %x tag=%d\n",
1394                  lbh->seg, lbh->start, addr, func ? func->symt.tag : -1);
1395             continue;
1396         }
1397         for (i = 0; i < lbh->nlines; i++)
1398         {
1399             symt_add_func_line(msc_dbg->module, func, source,
1400                                lbh->l[i].lineno ^ 0x80000000, lbh->l[i].offset - lbh->start);
1401         }
1402     }
1403 }
1404
1405 /*========================================================================
1406  * Process CodeView symbol information.
1407  */
1408
1409 static unsigned int codeview_map_offset(const struct msc_debug_info* msc_dbg,
1410                                         unsigned int offset)
1411 {
1412     int                 nomap = msc_dbg->nomap;
1413     const OMAP_DATA*    omapp = msc_dbg->omapp;
1414     int                 i;
1415
1416     if (!nomap || !omapp) return offset;
1417
1418     /* FIXME: use binary search */
1419     for (i = 0; i < nomap - 1; i++)
1420         if (omapp[i].from <= offset && omapp[i+1].from > offset)
1421             return !omapp[i].to ? 0 : omapp[i].to + (offset - omapp[i].from);
1422
1423     return 0;
1424 }
1425
1426 static unsigned codeview_get_address(const struct msc_debug_info* msc_dbg, 
1427                                      unsigned seg, unsigned offset)
1428 {
1429     int                         nsect = msc_dbg->nsect;
1430     const IMAGE_SECTION_HEADER* sectp = msc_dbg->sectp;
1431
1432     if (!seg || seg > nsect) return 0;
1433     return msc_dbg->module->module.BaseOfImage +
1434         codeview_map_offset(msc_dbg, sectp[seg-1].VirtualAddress + offset);
1435 }
1436
1437 static inline void codeview_add_variable(const struct msc_debug_info* msc_dbg,
1438                                          struct symt_compiland* compiland,
1439                                          const char* name,
1440                                          unsigned segment, unsigned offset,
1441                                          unsigned symtype, BOOL is_local, BOOL force)
1442 {
1443     if (name && *name)
1444     {
1445         unsigned        address = codeview_get_address(msc_dbg, segment, offset);
1446
1447         if (force || !symt_find_nearest(msc_dbg->module, address))
1448         {
1449             symt_new_global_variable(msc_dbg->module, compiland,
1450                                      name, is_local, address, 0,
1451                                      codeview_get_type(symtype, FALSE));
1452         }
1453     }
1454 }
1455
1456 static int codeview_snarf(const struct msc_debug_info* msc_dbg, const BYTE* root, 
1457                           int offset, int size, BOOL do_globals)
1458 {
1459     struct symt_function*               curr_func = NULL;
1460     int                                 i, length;
1461     struct symt_block*                  block = NULL;
1462     struct symt*                        symt;
1463     const char*                         name;
1464     struct symt_compiland*              compiland = NULL;
1465     struct location                     loc;
1466
1467     /*
1468      * Loop over the different types of records and whenever we
1469      * find something we are interested in, record it and move on.
1470      */
1471     for (i = offset; i < size; i += length)
1472     {
1473         const union codeview_symbol* sym = (const union codeview_symbol*)(root + i);
1474         length = sym->generic.len + 2;
1475         if (i + length > size) break;
1476         if (!sym->generic.id || length < 4) break;
1477         if (length & 3) FIXME("unpadded len %u\n", length);
1478
1479         switch (sym->generic.id)
1480         {
1481         /*
1482          * Global and local data symbols.  We don't associate these
1483          * with any given source file.
1484          */
1485         case S_GDATA_V1:
1486         case S_LDATA_V1:
1487             if (do_globals)
1488                 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v1.p_name),
1489                                       sym->data_v1.segment, sym->data_v1.offset, sym->data_v1.symtype,
1490                                       sym->generic.id == S_LDATA_V1, TRUE);
1491             break;
1492         case S_GDATA_V2:
1493         case S_LDATA_V2:
1494             if (do_globals)
1495                 codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v2.p_name),
1496                                       sym->data_v2.segment, sym->data_v2.offset, sym->data_v2.symtype,
1497                                       sym->generic.id == S_LDATA_V2, TRUE);
1498             break;
1499         case S_GDATA_V3:
1500         case S_LDATA_V3:
1501             if (do_globals)
1502                 codeview_add_variable(msc_dbg, compiland, sym->data_v3.name,
1503                                       sym->data_v3.segment, sym->data_v3.offset, sym->data_v3.symtype,
1504                                       sym->generic.id == S_LDATA_V3, TRUE);
1505             break;
1506
1507         /* Public symbols */
1508         case S_PUB_V1:
1509         case S_PUB_V2:
1510         case S_PUB_V3:
1511         case S_PUB_FUNC1_V3:
1512         case S_PUB_FUNC2_V3:
1513             /* will be handled later on in codeview_snarf_public */
1514             break;
1515
1516         /*
1517          * Sort of like a global function, but it just points
1518          * to a thunk, which is a stupid name for what amounts to
1519          * a PLT slot in the normal jargon that everyone else uses.
1520          */
1521         case S_THUNK_V1:
1522             symt_new_thunk(msc_dbg->module, compiland,
1523                            terminate_string(&sym->thunk_v1.p_name), sym->thunk_v1.thtype,
1524                            codeview_get_address(msc_dbg, sym->thunk_v1.segment, sym->thunk_v1.offset),
1525                            sym->thunk_v1.thunk_len);
1526             break;
1527         case S_THUNK_V3:
1528             symt_new_thunk(msc_dbg->module, compiland,
1529                            sym->thunk_v3.name, sym->thunk_v3.thtype,
1530                            codeview_get_address(msc_dbg, sym->thunk_v3.segment, sym->thunk_v3.offset),
1531                            sym->thunk_v3.thunk_len);
1532             break;
1533
1534         /*
1535          * Global and static functions.
1536          */
1537         case S_GPROC_V1:
1538         case S_LPROC_V1:
1539             if (curr_func) FIXME("nested function\n");
1540             curr_func = symt_new_function(msc_dbg->module, compiland,
1541                                           terminate_string(&sym->proc_v1.p_name),
1542                                           codeview_get_address(msc_dbg, sym->proc_v1.segment, sym->proc_v1.offset),
1543                                           sym->proc_v1.proc_len,
1544                                           codeview_get_type(sym->proc_v1.proctype, FALSE));
1545             loc.kind = loc_absolute;
1546             loc.offset = sym->proc_v1.debug_start;
1547             symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1548             loc.offset = sym->proc_v1.debug_end;
1549             symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1550             break;
1551         case S_GPROC_V2:
1552         case S_LPROC_V2:
1553             if (curr_func) FIXME("nested function\n");
1554             curr_func = symt_new_function(msc_dbg->module, compiland,
1555                                           terminate_string(&sym->proc_v2.p_name),
1556                                           codeview_get_address(msc_dbg, sym->proc_v2.segment, sym->proc_v2.offset),
1557                                           sym->proc_v2.proc_len,
1558                                           codeview_get_type(sym->proc_v2.proctype, FALSE));
1559             loc.kind = loc_absolute;
1560             loc.offset = sym->proc_v2.debug_start;
1561             symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1562             loc.offset = sym->proc_v2.debug_end;
1563             symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1564             break;
1565         case S_GPROC_V3:
1566         case S_LPROC_V3:
1567             if (curr_func) FIXME("nested function\n");
1568             curr_func = symt_new_function(msc_dbg->module, compiland,
1569                                           sym->proc_v3.name,
1570                                           codeview_get_address(msc_dbg, sym->proc_v3.segment, sym->proc_v3.offset),
1571                                           sym->proc_v3.proc_len,
1572                                           codeview_get_type(sym->proc_v3.proctype, FALSE));
1573             loc.kind = loc_absolute;
1574             loc.offset = sym->proc_v3.debug_start;
1575             symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, &loc, NULL);
1576             loc.offset = sym->proc_v3.debug_end;
1577             symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, &loc, NULL);
1578             break;
1579         /*
1580          * Function parameters and stack variables.
1581          */
1582         case S_BPREL_V1:
1583             loc.kind = loc_regrel;
1584             loc.reg = 0; /* FIXME */
1585             loc.offset = sym->stack_v1.offset;
1586             symt_add_func_local(msc_dbg->module, curr_func, 
1587                                 sym->stack_v1.offset > 0 ? DataIsParam : DataIsLocal, 
1588                                 &loc, block,
1589                                 codeview_get_type(sym->stack_v1.symtype, FALSE),
1590                                 terminate_string(&sym->stack_v1.p_name));
1591             break;
1592         case S_BPREL_V2:
1593             loc.kind = loc_regrel;
1594             loc.reg = 0; /* FIXME */
1595             loc.offset = sym->stack_v2.offset;
1596             symt_add_func_local(msc_dbg->module, curr_func, 
1597                                 sym->stack_v2.offset > 0 ? DataIsParam : DataIsLocal, 
1598                                 &loc, block,
1599                                 codeview_get_type(sym->stack_v2.symtype, FALSE),
1600                                 terminate_string(&sym->stack_v2.p_name));
1601             break;
1602         case S_BPREL_V3:
1603             loc.kind = loc_regrel;
1604             loc.reg = 0; /* FIXME */
1605             loc.offset = sym->stack_v3.offset;
1606             symt_add_func_local(msc_dbg->module, curr_func, 
1607                                 sym->stack_v3.offset > 0 ? DataIsParam : DataIsLocal, 
1608                                 &loc, block,
1609                                 codeview_get_type(sym->stack_v3.symtype, FALSE),
1610                                 sym->stack_v3.name);
1611             break;
1612         case S_BPREL_XXXX_V3:
1613             loc.kind = loc_regrel;
1614             loc.reg = 0; /* FIXME */
1615             loc.offset = sym->stack_xxxx_v3.offset;
1616             WARN("Supposed stack variable %s (%d)\n", sym->stack_xxxx_v3.name, sym->stack_xxxx_v3.unknown);
1617             symt_add_func_local(msc_dbg->module, curr_func,
1618                                 sym->stack_xxxx_v3.offset > 0 ? DataIsParam : DataIsLocal,
1619                                 &loc, block,
1620                                 codeview_get_type(sym->stack_xxxx_v3.symtype, FALSE),
1621                                 sym->stack_xxxx_v3.name);
1622             break;
1623
1624         case S_REGISTER_V1:
1625             loc.kind = loc_register;
1626             loc.reg = sym->register_v1.reg;
1627             loc.offset = 0;
1628             symt_add_func_local(msc_dbg->module, curr_func, 
1629                                 DataIsLocal, &loc,
1630                                 block, codeview_get_type(sym->register_v1.type, FALSE),
1631                                 terminate_string(&sym->register_v1.p_name));
1632             break;
1633         case S_REGISTER_V2:
1634             loc.kind = loc_register;
1635             loc.reg = sym->register_v2.reg;
1636             loc.offset = 0;
1637             symt_add_func_local(msc_dbg->module, curr_func, 
1638                                 DataIsLocal, &loc,
1639                                 block, codeview_get_type(sym->register_v2.type, FALSE),
1640                                 terminate_string(&sym->register_v2.p_name));
1641             break;
1642         case S_REGISTER_V3:
1643             loc.kind = loc_register;
1644             loc.reg = sym->register_v3.reg;
1645             loc.offset = 0;
1646             symt_add_func_local(msc_dbg->module, curr_func,
1647                                 DataIsLocal, &loc,
1648                                 block, codeview_get_type(sym->register_v3.type, FALSE),
1649                                 sym->register_v3.name);
1650             break;
1651
1652         case S_BLOCK_V1:
1653             block = symt_open_func_block(msc_dbg->module, curr_func, block, 
1654                                          codeview_get_address(msc_dbg, sym->block_v1.segment, sym->block_v1.offset),
1655                                          sym->block_v1.length);
1656             break;
1657         case S_BLOCK_V3:
1658             block = symt_open_func_block(msc_dbg->module, curr_func, block, 
1659                                          codeview_get_address(msc_dbg, sym->block_v3.segment, sym->block_v3.offset),
1660                                          sym->block_v3.length);
1661             break;
1662
1663         case S_END_V1:
1664             if (block)
1665             {
1666                 block = symt_close_func_block(msc_dbg->module, curr_func, block, 0);
1667             }
1668             else if (curr_func)
1669             {
1670                 symt_normalize_function(msc_dbg->module, curr_func);
1671                 curr_func = NULL;
1672             }
1673             break;
1674
1675         case S_COMPILAND_V1:
1676             TRACE("S-Compiland-V1 %x %s\n",
1677                   sym->compiland_v1.unknown, terminate_string(&sym->compiland_v1.p_name));
1678             break;
1679
1680         case S_COMPILAND_V2:
1681             TRACE("S-Compiland-V2 %s\n", terminate_string(&sym->compiland_v2.p_name));
1682             if (TRACE_ON(dbghelp_msc))
1683             {
1684                 const char* ptr1 = sym->compiland_v2.p_name.name + sym->compiland_v2.p_name.namelen;
1685                 const char* ptr2;
1686                 while (*ptr1)
1687                 {
1688                     ptr2 = ptr1 + strlen(ptr1) + 1;
1689                     TRACE("\t%s => %s\n", ptr1, debugstr_a(ptr2));
1690                     ptr1 = ptr2 + strlen(ptr2) + 1;
1691                 }
1692             }
1693             break;
1694         case S_COMPILAND_V3:
1695             TRACE("S-Compiland-V3 %s\n", sym->compiland_v3.name);
1696             if (TRACE_ON(dbghelp_msc))
1697             {
1698                 const char* ptr1 = sym->compiland_v3.name + strlen(sym->compiland_v3.name);
1699                 const char* ptr2;
1700                 while (*ptr1)
1701                 {
1702                     ptr2 = ptr1 + strlen(ptr1) + 1;
1703                     TRACE("\t%s => %s\n", ptr1, debugstr_a(ptr2));
1704                     ptr1 = ptr2 + strlen(ptr2) + 1;
1705                 }
1706             }
1707             break;
1708
1709         case S_OBJNAME_V1:
1710             TRACE("S-ObjName %s\n", terminate_string(&sym->objname_v1.p_name));
1711             compiland = symt_new_compiland(msc_dbg->module, 0 /* FIXME */,
1712                                            source_new(msc_dbg->module, NULL,
1713                                                       terminate_string(&sym->objname_v1.p_name)));
1714             break;
1715
1716         case S_LABEL_V1:
1717             if (curr_func)
1718             {
1719                 loc.kind = loc_absolute;
1720                 loc.offset = codeview_get_address(msc_dbg, sym->label_v1.segment, sym->label_v1.offset) - curr_func->address;
1721                 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel, &loc,
1722                                         terminate_string(&sym->label_v1.p_name));
1723             }
1724             else symt_new_label(msc_dbg->module, compiland,
1725                                 terminate_string(&sym->label_v1.p_name),
1726                                 codeview_get_address(msc_dbg, sym->label_v1.segment, sym->label_v1.offset));
1727             break;
1728         case S_LABEL_V3:
1729             if (curr_func)
1730             {
1731                 loc.kind = loc_absolute;
1732                 loc.offset = codeview_get_address(msc_dbg, sym->label_v3.segment, sym->label_v3.offset) - curr_func->address;
1733                 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel, 
1734                                         &loc, sym->label_v3.name);
1735             }
1736             else symt_new_label(msc_dbg->module, compiland, sym->label_v3.name,
1737                                 codeview_get_address(msc_dbg, sym->label_v3.segment, sym->label_v3.offset));
1738             break;
1739
1740         case S_CONSTANT_V1:
1741             {
1742                 int                     vlen;
1743                 const struct p_string*  name;
1744                 struct symt*            se;
1745                 VARIANT                 v;
1746
1747                 vlen = leaf_as_variant(&v, &sym->constant_v1.cvalue);
1748                 name = (const struct p_string*)((const char*)&sym->constant_v1.cvalue + vlen);
1749                 se = codeview_get_type(sym->constant_v1.type, FALSE);
1750
1751                 TRACE("S-Constant-V1 %u %s %x\n",
1752                       v.n1.n2.n3.intVal, terminate_string(name), sym->constant_v1.type);
1753                 symt_new_constant(msc_dbg->module, compiland, terminate_string(name),
1754                                   se, &v);
1755             }
1756             break;
1757         case S_CONSTANT_V2:
1758             {
1759                 int                     vlen;
1760                 const struct p_string*  name;
1761                 struct symt*            se;
1762                 VARIANT                 v;
1763
1764                 vlen = leaf_as_variant(&v, &sym->constant_v2.cvalue);
1765                 name = (const struct p_string*)((const char*)&sym->constant_v2.cvalue + vlen);
1766                 se = codeview_get_type(sym->constant_v2.type, FALSE);
1767
1768                 TRACE("S-Constant-V2 %u %s %x\n",
1769                       v.n1.n2.n3.intVal, terminate_string(name), sym->constant_v2.type);
1770                 symt_new_constant(msc_dbg->module, compiland, terminate_string(name),
1771                                   se, &v);
1772             }
1773             break;
1774         case S_CONSTANT_V3:
1775             {
1776                 int                     vlen;
1777                 const char*             name;
1778                 struct symt*            se;
1779                 VARIANT                 v;
1780
1781                 vlen = leaf_as_variant(&v, &sym->constant_v3.cvalue);
1782                 name = (const char*)&sym->constant_v3.cvalue + vlen;
1783                 se = codeview_get_type(sym->constant_v3.type, FALSE);
1784
1785                 TRACE("S-Constant-V3 %u %s %x\n",
1786                       v.n1.n2.n3.intVal, name, sym->constant_v3.type);
1787                 /* FIXME: we should add this as a constant value */
1788                 symt_new_constant(msc_dbg->module, compiland, name, se, &v);
1789             }
1790             break;
1791
1792         case S_UDT_V1:
1793             if (sym->udt_v1.type)
1794             {
1795                 if ((symt = codeview_get_type(sym->udt_v1.type, FALSE)))
1796                     symt_new_typedef(msc_dbg->module, symt, 
1797                                      terminate_string(&sym->udt_v1.p_name));
1798                 else
1799                     FIXME("S-Udt %s: couldn't find type 0x%x\n", 
1800                           terminate_string(&sym->udt_v1.p_name), sym->udt_v1.type);
1801             }
1802             break;
1803         case S_UDT_V2:
1804             if (sym->udt_v2.type)
1805             {
1806                 if ((symt = codeview_get_type(sym->udt_v2.type, FALSE)))
1807                     symt_new_typedef(msc_dbg->module, symt, 
1808                                      terminate_string(&sym->udt_v2.p_name));
1809                 else
1810                     FIXME("S-Udt %s: couldn't find type 0x%x\n", 
1811                           terminate_string(&sym->udt_v2.p_name), sym->udt_v2.type);
1812             }
1813             break;
1814         case S_UDT_V3:
1815             if (sym->udt_v3.type)
1816             {
1817                 if ((symt = codeview_get_type(sym->udt_v3.type, FALSE)))
1818                     symt_new_typedef(msc_dbg->module, symt, sym->udt_v3.name);
1819                 else
1820                     FIXME("S-Udt %s: couldn't find type 0x%x\n", 
1821                           sym->udt_v3.name, sym->udt_v3.type);
1822             }
1823             break;
1824
1825          /*
1826          * These are special, in that they are always followed by an
1827          * additional length-prefixed string which is *not* included
1828          * into the symbol length count.  We need to skip it.
1829          */
1830         case S_PROCREF_V1:
1831         case S_DATAREF_V1:
1832         case S_LPROCREF_V1:
1833             name = (const char*)sym + length;
1834             length += (*name + 1 + 3) & ~3;
1835             break;
1836
1837         case S_MSTOOL_V3: /* just to silence a few warnings */
1838         case S_MSTOOLINFO_V3:
1839         case S_MSTOOLENV_V3:
1840             break;
1841
1842         case S_SSEARCH_V1:
1843             TRACE("Start search: seg=0x%x at offset 0x%08x\n",
1844                   sym->ssearch_v1.segment, sym->ssearch_v1.offset);
1845             break;
1846
1847         case S_ALIGN_V1:
1848             TRACE("S-Align V1\n");
1849             break;
1850
1851         /* the symbols we can safely ignore for now */
1852         case 0x112c:
1853         case S_FUNCINFO_V2:
1854         case S_SECUCOOKIE_V3:
1855         case S_SECTINFO_V3:
1856         case S_SUBSECTINFO_V3:
1857         case S_ENTRYPOINT_V3:
1858         case 0x1139:
1859             TRACE("Unsupported symbol id %x\n", sym->generic.id);
1860             break;
1861
1862         default:
1863             FIXME("Unsupported symbol id %x\n", sym->generic.id);
1864             dump(sym, 2 + sym->generic.len);
1865             break;
1866         }
1867     }
1868
1869     if (curr_func) symt_normalize_function(msc_dbg->module, curr_func);
1870
1871     return TRUE;
1872 }
1873
1874 static int codeview_snarf_public(const struct msc_debug_info* msc_dbg, const BYTE* root,
1875                                  int offset, int size)
1876
1877 {
1878     int                                 i, length;
1879     struct symt_compiland*              compiland = NULL;
1880
1881     /*
1882      * Loop over the different types of records and whenever we
1883      * find something we are interested in, record it and move on.
1884      */
1885     for (i = offset; i < size; i += length)
1886     {
1887         const union codeview_symbol* sym = (const union codeview_symbol*)(root + i);
1888         length = sym->generic.len + 2;
1889         if (i + length > size) break;
1890         if (!sym->generic.id || length < 4) break;
1891         if (length & 3) FIXME("unpadded len %u\n", length);
1892
1893         switch (sym->generic.id)
1894         {
1895         case S_PUB_V1: /* FIXME is this really a 'data_v1' structure ?? */
1896             if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1897             {
1898                 symt_new_public(msc_dbg->module, compiland,
1899                                 terminate_string(&sym->data_v1.p_name),
1900                                 codeview_get_address(msc_dbg, sym->data_v1.segment, sym->data_v1.offset),
1901                                 1, TRUE /* FIXME */, TRUE /* FIXME */);
1902             }
1903             break;
1904         case S_PUB_V2: /* FIXME is this really a 'data_v2' structure ?? */
1905             if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1906             {
1907                 symt_new_public(msc_dbg->module, compiland,
1908                                 terminate_string(&sym->data_v2.p_name),
1909                                 codeview_get_address(msc_dbg, sym->data_v2.segment, sym->data_v2.offset),
1910                                 1, TRUE /* FIXME */, TRUE /* FIXME */);
1911             }
1912             break;
1913
1914         case S_PUB_V3:
1915             if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1916             {
1917                 symt_new_public(msc_dbg->module, compiland,
1918                                 sym->data_v3.name,
1919                                 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset),
1920                                 1, FALSE /* FIXME */, FALSE);
1921             }
1922             break;
1923         case S_PUB_FUNC1_V3:
1924         case S_PUB_FUNC2_V3: /* using a data_v3 isn't what we'd expect */
1925 #if 0
1926             /* FIXME: this is plain wrong (from a simple test) */
1927             if (!(dbghelp_options & SYMOPT_NO_PUBLICS))
1928             {
1929                 symt_new_public(msc_dbg->module, compiland,
1930                                 sym->data_v3.name,
1931                                 codeview_get_address(msc_dbg, sym->data_v3.segment, sym->data_v3.offset),
1932                                 1, TRUE /* FIXME */, TRUE);
1933             }
1934 #endif
1935             break;
1936         /*
1937          * Global and local data symbols.  We don't associate these
1938          * with any given source file.
1939          */
1940         case S_GDATA_V1:
1941         case S_LDATA_V1:
1942             codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v1.p_name),
1943                                   sym->data_v1.segment, sym->data_v1.offset, sym->data_v1.symtype,
1944                                   sym->generic.id == S_LDATA_V1, FALSE);
1945             break;
1946         case S_GDATA_V2:
1947         case S_LDATA_V2:
1948             codeview_add_variable(msc_dbg, compiland, terminate_string(&sym->data_v2.p_name),
1949                                   sym->data_v2.segment, sym->data_v2.offset, sym->data_v2.symtype,
1950                                   sym->generic.id == S_LDATA_V2, FALSE);
1951             break;
1952         case S_GDATA_V3:
1953         case S_LDATA_V3:
1954             codeview_add_variable(msc_dbg, compiland, sym->data_v3.name,
1955                                   sym->data_v3.segment, sym->data_v3.offset, sym->data_v3.symtype,
1956                                   sym->generic.id == S_LDATA_V3, FALSE);
1957             break;
1958         /*
1959          * These are special, in that they are always followed by an
1960          * additional length-prefixed string which is *not* included
1961          * into the symbol length count.  We need to skip it.
1962          */
1963         case S_PROCREF_V1:
1964         case S_DATAREF_V1:
1965         case S_LPROCREF_V1:
1966             length += (((const char*)sym)[length] + 1 + 3) & ~3;
1967             break;
1968         }
1969         msc_dbg->module->sortlist_valid = TRUE;
1970     }
1971     msc_dbg->module->sortlist_valid = FALSE;
1972     return TRUE;
1973 }
1974
1975 /*========================================================================
1976  * Process PDB file.
1977  */
1978
1979 static void* pdb_jg_read(const struct PDB_JG_HEADER* pdb, const WORD* block_list,
1980                          int size)
1981 {
1982     int                         i, num_blocks;
1983     BYTE*                       buffer;
1984
1985     if (!size) return NULL;
1986
1987     num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
1988     buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
1989
1990     for (i = 0; i < num_blocks; i++)
1991         memcpy(buffer + i * pdb->block_size,
1992                (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
1993
1994     return buffer;
1995 }
1996
1997 static void* pdb_ds_read(const struct PDB_DS_HEADER* pdb, const DWORD* block_list,
1998                          int size)
1999 {
2000     int                         i, num_blocks;
2001     BYTE*                       buffer;
2002
2003     if (!size) return NULL;
2004
2005     num_blocks = (size + pdb->block_size - 1) / pdb->block_size;
2006     buffer = HeapAlloc(GetProcessHeap(), 0, num_blocks * pdb->block_size);
2007
2008     for (i = 0; i < num_blocks; i++)
2009         memcpy(buffer + i * pdb->block_size,
2010                (const char*)pdb + block_list[i] * pdb->block_size, pdb->block_size);
2011
2012     return buffer;
2013 }
2014
2015 static void* pdb_read_jg_file(const struct PDB_JG_HEADER* pdb,
2016                               const struct PDB_JG_TOC* toc, DWORD file_nr)
2017 {
2018     const WORD*                 block_list;
2019     DWORD                       i;
2020
2021     if (!toc || file_nr >= toc->num_files) return NULL;
2022
2023     block_list = (const WORD*) &toc->file[toc->num_files];
2024     for (i = 0; i < file_nr; i++)
2025         block_list += (toc->file[i].size + pdb->block_size - 1) / pdb->block_size;
2026
2027     return pdb_jg_read(pdb, block_list, toc->file[file_nr].size);
2028 }
2029
2030 static void* pdb_read_ds_file(const struct PDB_DS_HEADER* pdb,
2031                               const struct PDB_DS_TOC* toc, DWORD file_nr)
2032 {
2033     const DWORD*                block_list;
2034     DWORD                       i;
2035
2036     if (!toc || file_nr >= toc->num_files) return NULL;
2037
2038     if (toc->file_size[file_nr] == 0 || toc->file_size[file_nr] == 0xFFFFFFFF)
2039     {
2040         FIXME(">>> requesting NULL stream (%u)\n", file_nr);
2041         return NULL;
2042     }
2043     block_list = &toc->file_size[toc->num_files];
2044     for (i = 0; i < file_nr; i++)
2045         block_list += (toc->file_size[i] + pdb->block_size - 1) / pdb->block_size;
2046
2047     return pdb_ds_read(pdb, block_list, toc->file_size[file_nr]);
2048 }
2049
2050 static void* pdb_read_file(const char* image, const struct pdb_lookup* pdb_lookup,
2051                            DWORD file_nr)
2052 {
2053     switch (pdb_lookup->kind)
2054     {
2055     case PDB_JG:
2056         return pdb_read_jg_file((const struct PDB_JG_HEADER*)image, 
2057                                 pdb_lookup->u.jg.toc, file_nr);
2058     case PDB_DS:
2059         return pdb_read_ds_file((const struct PDB_DS_HEADER*)image,
2060                                 pdb_lookup->u.ds.toc, file_nr);
2061     }
2062     return NULL;
2063 }
2064
2065 static unsigned pdb_get_file_size(const struct pdb_lookup* pdb_lookup, DWORD file_nr)
2066 {
2067     switch (pdb_lookup->kind)
2068     {
2069     case PDB_JG: return pdb_lookup->u.jg.toc->file[file_nr].size;
2070     case PDB_DS: return pdb_lookup->u.ds.toc->file_size[file_nr];
2071     }
2072     return 0;
2073 }
2074
2075 static void pdb_free(void* buffer)
2076 {
2077     HeapFree(GetProcessHeap(), 0, buffer);
2078 }
2079
2080 static void pdb_free_lookup(const struct pdb_lookup* pdb_lookup)
2081 {
2082     switch (pdb_lookup->kind)
2083     {
2084     case PDB_JG:
2085         pdb_free(pdb_lookup->u.jg.toc);
2086         break;
2087     case PDB_DS:
2088         pdb_free(pdb_lookup->u.ds.toc);
2089         break;
2090     }
2091 }
2092     
2093 static void pdb_convert_types_header(PDB_TYPES* types, const BYTE* image)
2094 {
2095     memset(types, 0, sizeof(PDB_TYPES));
2096     if (!image) return;
2097
2098     if (*(const DWORD*)image < 19960000)   /* FIXME: correct version? */
2099     {
2100         /* Old version of the types record header */
2101         const PDB_TYPES_OLD*    old = (const PDB_TYPES_OLD*)image;
2102         types->version     = old->version;
2103         types->type_offset = sizeof(PDB_TYPES_OLD);
2104         types->type_size   = old->type_size;
2105         types->first_index = old->first_index;
2106         types->last_index  = old->last_index;
2107         types->file        = old->file;
2108     }
2109     else
2110     {
2111         /* New version of the types record header */
2112         *types = *(const PDB_TYPES*)image;
2113     }
2114 }
2115
2116 static void pdb_convert_symbols_header(PDB_SYMBOLS* symbols,
2117                                        int* header_size, const BYTE* image)
2118 {
2119     memset(symbols, 0, sizeof(PDB_SYMBOLS));
2120     if (!image) return;
2121
2122     if (*(const DWORD*)image != 0xffffffff)
2123     {
2124         /* Old version of the symbols record header */
2125         const PDB_SYMBOLS_OLD*  old = (const PDB_SYMBOLS_OLD*)image;
2126         symbols->version         = 0;
2127         symbols->module_size     = old->module_size;
2128         symbols->offset_size     = old->offset_size;
2129         symbols->hash_size       = old->hash_size;
2130         symbols->srcmodule_size  = old->srcmodule_size;
2131         symbols->pdbimport_size  = 0;
2132         symbols->hash1_file      = old->hash1_file;
2133         symbols->hash2_file      = old->hash2_file;
2134         symbols->gsym_file       = old->gsym_file;
2135
2136         *header_size = sizeof(PDB_SYMBOLS_OLD);
2137     }
2138     else
2139     {
2140         /* New version of the symbols record header */
2141         *symbols = *(const PDB_SYMBOLS*)image;
2142         *header_size = sizeof(PDB_SYMBOLS);
2143     }
2144 }
2145
2146 static void pdb_convert_symbol_file(const PDB_SYMBOLS* symbols, 
2147                                     PDB_SYMBOL_FILE_EX* sfile, 
2148                                     unsigned* size, const void* image)
2149
2150 {
2151     if (symbols->version < 19970000)
2152     {
2153         const PDB_SYMBOL_FILE *sym_file = image;
2154         memset(sfile, 0, sizeof(*sfile));
2155         sfile->file        = sym_file->file;
2156         sfile->range.index = sym_file->range.index;
2157         sfile->symbol_size = sym_file->symbol_size;
2158         sfile->lineno_size = sym_file->lineno_size;
2159         *size = sizeof(PDB_SYMBOL_FILE) - 1;
2160     }
2161     else
2162     {
2163         memcpy(sfile, image, sizeof(PDB_SYMBOL_FILE_EX));
2164         *size = sizeof(PDB_SYMBOL_FILE_EX) - 1;
2165     }
2166 }
2167
2168 static HANDLE open_pdb_file(const struct process* pcs,
2169                             const struct pdb_lookup* lookup,
2170                             struct module* module)
2171 {
2172     HANDLE      h;
2173     char        dbg_file_path[MAX_PATH];
2174     BOOL        ret = FALSE;
2175
2176     switch (lookup->kind)
2177     {
2178     case PDB_JG:
2179         ret = path_find_symbol_file(pcs, lookup->filename, NULL, lookup->u.jg.timestamp,
2180                                     lookup->age, dbg_file_path, &module->module.PdbUnmatched);
2181         break;
2182     case PDB_DS:
2183         ret = path_find_symbol_file(pcs, lookup->filename, &lookup->u.ds.guid, 0,
2184                                     lookup->age, dbg_file_path, &module->module.PdbUnmatched);
2185         break;
2186     }
2187     if (!ret)
2188     {
2189         WARN("\tCouldn't find %s\n", lookup->filename);
2190         return NULL;
2191     }
2192     h = CreateFileA(dbg_file_path, GENERIC_READ, FILE_SHARE_READ, NULL, 
2193                     OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2194     TRACE("%s: %s returns %p\n", lookup->filename, dbg_file_path, h);
2195     return (h == INVALID_HANDLE_VALUE) ? NULL : h;
2196 }
2197
2198 static void pdb_process_types(const struct msc_debug_info* msc_dbg, 
2199                               const char* image, const struct pdb_lookup* pdb_lookup)
2200 {
2201     BYTE*       types_image = NULL;
2202
2203     types_image = pdb_read_file(image, pdb_lookup, 2);
2204     if (types_image)
2205     {
2206         PDB_TYPES               types;
2207         struct codeview_type_parse      ctp;
2208         DWORD                   total;
2209         const BYTE*             ptr;
2210         DWORD*                  offset;
2211
2212         pdb_convert_types_header(&types, types_image);
2213
2214         /* Check for unknown versions */
2215         switch (types.version)
2216         {
2217         case 19950410:      /* VC 4.0 */
2218         case 19951122:
2219         case 19961031:      /* VC 5.0 / 6.0 */
2220         case 19990903:
2221             break;
2222         default:
2223             ERR("-Unknown type info version %d\n", types.version);
2224         }
2225
2226         ctp.module = msc_dbg->module;
2227         /* reconstruct the types offset...
2228          * FIXME: maybe it's present in the newest PDB_TYPES structures
2229          */
2230         total = types.last_index - types.first_index + 1;
2231         offset = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD) * total);
2232         ctp.table = ptr = types_image + types.type_offset;
2233         ctp.num = 0;
2234         while (ptr < ctp.table + types.type_size && ctp.num < total)
2235         {
2236             offset[ctp.num++] = ptr - ctp.table;
2237             ptr += ((const union codeview_type*)ptr)->generic.len + 2;
2238         }
2239         ctp.offset = offset;
2240
2241         /* Read type table */
2242         codeview_parse_type_table(&ctp);
2243         HeapFree(GetProcessHeap(), 0, offset);
2244         pdb_free(types_image);
2245     }
2246 }
2247
2248 static const char       PDB_JG_IDENT[] = "Microsoft C/C++ program database 2.00\r\n\032JG\0";
2249 static const char       PDB_DS_IDENT[] = "Microsoft C/C++ MSF 7.00\r\n\032DS\0";
2250
2251 /******************************************************************
2252  *              pdb_init
2253  *
2254  * Tries to load a pdb file
2255  * if do_fill is TRUE, then it just fills pdb_lookup with the information of the
2256  *      file
2257  * if do_fill is FALSE, then it just checks that the kind of PDB (stored in
2258  *      pdb_lookup) matches what's really in the file
2259  */
2260 static BOOL pdb_init(struct pdb_lookup* pdb_lookup, const char* image, BOOL do_fill)
2261 {
2262     BOOL        ret = TRUE;
2263
2264     /* check the file header, and if ok, load the TOC */
2265     TRACE("PDB(%s): %.40s\n", pdb_lookup->filename, debugstr_an(image, 40));
2266
2267     if (!memcmp(image, PDB_JG_IDENT, sizeof(PDB_JG_IDENT)))
2268     {
2269         const struct PDB_JG_HEADER* pdb = (const struct PDB_JG_HEADER*)image;
2270         struct PDB_JG_ROOT*         root;
2271
2272         pdb_lookup->u.jg.toc = pdb_jg_read(pdb, pdb->toc_block, pdb->toc.size);
2273         root = pdb_read_jg_file(pdb, pdb_lookup->u.jg.toc, 1);
2274         if (!root)
2275         {
2276             ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
2277             return FALSE;
2278         }
2279         switch (root->Version)
2280         {
2281         case 19950623:      /* VC 4.0 */
2282         case 19950814:
2283         case 19960307:      /* VC 5.0 */
2284         case 19970604:      /* VC 6.0 */
2285             break;
2286         default:
2287             ERR("-Unknown root block version %d\n", root->Version);
2288         }
2289         if (do_fill)
2290         {
2291             pdb_lookup->kind = PDB_JG;
2292             pdb_lookup->u.jg.timestamp = root->TimeDateStamp;
2293             pdb_lookup->age = root->Age;
2294         }
2295         else if (pdb_lookup->kind != PDB_JG ||
2296                  pdb_lookup->u.jg.timestamp != root->TimeDateStamp ||
2297                  pdb_lookup->age != root->Age)
2298             ret = FALSE;
2299         TRACE("found JG/%c for %s: age=%x timestamp=%x\n",
2300               do_fill ? 'f' : '-', pdb_lookup->filename, root->Age,
2301               root->TimeDateStamp);
2302         pdb_free(root);
2303     }
2304     else if (!memcmp(image, PDB_DS_IDENT, sizeof(PDB_DS_IDENT)))
2305     {
2306         const struct PDB_DS_HEADER* pdb = (const struct PDB_DS_HEADER*)image;
2307         struct PDB_DS_ROOT*         root;
2308
2309         pdb_lookup->u.ds.toc = 
2310             pdb_ds_read(pdb, 
2311                         (const DWORD*)((const char*)pdb + pdb->toc_page * pdb->block_size), 
2312                         pdb->toc_size);
2313         root = pdb_read_ds_file(pdb, pdb_lookup->u.ds.toc, 1);
2314         if (!root)
2315         {
2316             ERR("-Unable to get root from .PDB in %s\n", pdb_lookup->filename);
2317             return FALSE;
2318         }
2319         switch (root->Version)
2320         {
2321         case 20000404:
2322             break;
2323         default:
2324             ERR("-Unknown root block version %d\n", root->Version);
2325         }
2326         if (do_fill)
2327         {
2328             pdb_lookup->kind = PDB_DS;
2329             pdb_lookup->u.ds.guid = root->guid;
2330             pdb_lookup->age = root->Age;
2331         }
2332         else if (pdb_lookup->kind != PDB_DS ||
2333                  memcmp(&pdb_lookup->u.ds.guid, &root->guid, sizeof(GUID)) ||
2334                  pdb_lookup->age != root->Age)
2335             ret = FALSE;
2336         TRACE("found DS/%c for %s: age=%x guid=%s\n",
2337               do_fill ? 'f' : '-', pdb_lookup->filename, root->Age,
2338               debugstr_guid(&root->guid));
2339         pdb_free(root);
2340     }
2341
2342     if (0) /* some tool to dump the internal files from a PDB file */
2343     {
2344         int     i, num_files;
2345         
2346         switch (pdb_lookup->kind)
2347         {
2348         case PDB_JG: num_files = pdb_lookup->u.jg.toc->num_files; break;
2349         case PDB_DS: num_files = pdb_lookup->u.ds.toc->num_files; break;
2350         }
2351
2352         for (i = 1; i < num_files; i++)
2353         {
2354             unsigned char* x = pdb_read_file(image, pdb_lookup, i);
2355             FIXME("********************** [%u]: size=%08x\n",
2356                   i, pdb_get_file_size(pdb_lookup, i));
2357             dump(x, pdb_get_file_size(pdb_lookup, i));
2358             pdb_free(x);
2359         }
2360     }
2361     return ret;
2362 }
2363
2364 static BOOL pdb_process_internal(const struct process* pcs, 
2365                                  const struct msc_debug_info* msc_dbg,
2366                                  struct pdb_lookup* pdb_lookup,
2367                                  unsigned module_index);
2368
2369 static void pdb_process_symbol_imports(const struct process* pcs, 
2370                                        const struct msc_debug_info* msc_dbg,
2371                                        const PDB_SYMBOLS* symbols,
2372                                        const void* symbols_image,
2373                                        const char* image,
2374                                        const struct pdb_lookup* pdb_lookup,
2375                                        unsigned module_index)
2376 {
2377     if (module_index == -1 && symbols && symbols->pdbimport_size)
2378     {
2379         const PDB_SYMBOL_IMPORT*imp;
2380         const void*             first;
2381         const void*             last;
2382         const char*             ptr;
2383         int                     i = 0;
2384
2385         imp = (const PDB_SYMBOL_IMPORT*)((const char*)symbols_image + sizeof(PDB_SYMBOLS) + 
2386                                          symbols->module_size + symbols->offset_size + 
2387                                          symbols->hash_size + symbols->srcmodule_size);
2388         first = imp;
2389         last = (const char*)imp + symbols->pdbimport_size;
2390         while (imp < (const PDB_SYMBOL_IMPORT*)last)
2391         {
2392             ptr = (const char*)imp + sizeof(*imp) + strlen(imp->filename);
2393             if (i >= CV_MAX_MODULES) FIXME("Out of bounds !!!\n");
2394             if (!strcasecmp(pdb_lookup->filename, imp->filename))
2395             {
2396                 if (module_index != -1) FIXME("Twice the entry\n");
2397                 else module_index = i;
2398             }
2399             else
2400             {
2401                 struct pdb_lookup       imp_pdb_lookup;
2402
2403                 /* FIXME: this is an import of a JG PDB file
2404                  * how's a DS PDB handled ?
2405                  */
2406                 imp_pdb_lookup.filename = imp->filename;
2407                 imp_pdb_lookup.kind = PDB_JG;
2408                 imp_pdb_lookup.u.jg.timestamp = imp->TimeDateStamp;
2409                 imp_pdb_lookup.age = imp->Age;
2410                 TRACE("got for %s: age=%u ts=%x\n",
2411                       imp->filename, imp->Age, imp->TimeDateStamp);
2412                 pdb_process_internal(pcs, msc_dbg, &imp_pdb_lookup, i);
2413             }
2414             i++;
2415             imp = (const PDB_SYMBOL_IMPORT*)((const char*)first + ((ptr - (const char*)first + strlen(ptr) + 1 + 3) & ~3));
2416         }
2417     }
2418     cv_current_module = &cv_zmodules[(module_index == -1) ? 0 : module_index];
2419     if (cv_current_module->allowed) FIXME("Already allowed ??\n");
2420     cv_current_module->allowed = TRUE;
2421     pdb_process_types(msc_dbg, image, pdb_lookup);
2422 }
2423
2424 static BOOL pdb_process_internal(const struct process* pcs, 
2425                                  const struct msc_debug_info* msc_dbg,
2426                                  struct pdb_lookup* pdb_lookup, 
2427                                  unsigned module_index)
2428 {
2429     BOOL        ret = FALSE;
2430     HANDLE      hFile, hMap = NULL;
2431     char*       image = NULL;
2432     BYTE*       symbols_image = NULL;
2433     char*       files_image = NULL;
2434     DWORD       files_size = 0;
2435
2436     TRACE("Processing PDB file %s\n", pdb_lookup->filename);
2437
2438     /* Open and map() .PDB file */
2439     if ((hFile = open_pdb_file(pcs, pdb_lookup, msc_dbg->module)) == NULL ||
2440         ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL) ||
2441         ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
2442     {
2443         WARN("Unable to open .PDB file: %s\n", pdb_lookup->filename);
2444         goto leave;
2445     }
2446     pdb_init(pdb_lookup, image, FALSE);
2447
2448     symbols_image = pdb_read_file(image, pdb_lookup, 3);
2449     if (symbols_image)
2450     {
2451         PDB_SYMBOLS symbols;
2452         BYTE*       globalimage;
2453         BYTE*       modimage;
2454         BYTE*       file;
2455         int         header_size = 0;
2456         
2457         pdb_convert_symbols_header(&symbols, &header_size, symbols_image);
2458         switch (symbols.version)
2459         {
2460         case 0:            /* VC 4.0 */
2461         case 19960307:     /* VC 5.0 */
2462         case 19970606:     /* VC 6.0 */
2463         case 19990903:
2464             break;
2465         default:
2466             ERR("-Unknown symbol info version %d %08x\n",
2467                 symbols.version, symbols.version);
2468         }
2469
2470         files_image = pdb_read_file(image, pdb_lookup, 12);   /* FIXME: really fixed ??? */
2471         if (files_image)
2472         {
2473             if (*(const DWORD*)files_image == 0xeffeeffe)
2474             {
2475                 files_size = *(const DWORD*)(files_image + 8);
2476             }
2477             else
2478             {
2479                 WARN("wrong header %x expecting 0xeffeeffe\n", *(const DWORD*)files_image);
2480                 pdb_free(files_image);
2481                 files_image = NULL;
2482             }
2483         }
2484
2485         pdb_process_symbol_imports(pcs, msc_dbg, &symbols, symbols_image, image, pdb_lookup, module_index);
2486
2487         /* Read global symbol table */
2488         globalimage = pdb_read_file(image, pdb_lookup, symbols.gsym_file);
2489         if (globalimage)
2490         {
2491             codeview_snarf(msc_dbg, globalimage, 0,
2492                            pdb_get_file_size(pdb_lookup, symbols.gsym_file), FALSE);
2493         }
2494
2495         /* Read per-module symbols' tables */
2496         file = symbols_image + header_size;
2497         while (file - symbols_image < header_size + symbols.module_size)
2498         {
2499             PDB_SYMBOL_FILE_EX          sfile;
2500             const char*                 file_name;
2501             unsigned                    size;
2502
2503             HeapValidate(GetProcessHeap(), 0, NULL);
2504             pdb_convert_symbol_file(&symbols, &sfile, &size, file);
2505
2506             modimage = pdb_read_file(image, pdb_lookup, sfile.file);
2507             if (modimage)
2508             {
2509                 if (sfile.symbol_size)
2510                     codeview_snarf(msc_dbg, modimage, sizeof(DWORD),
2511                                    sfile.symbol_size, TRUE);
2512
2513                 if (sfile.lineno_size)
2514                     codeview_snarf_linetab(msc_dbg,
2515                                            modimage + sfile.symbol_size,
2516                                            sfile.lineno_size,
2517                                            pdb_lookup->kind == PDB_JG);
2518                 if (files_image)
2519                     codeview_snarf_linetab2(msc_dbg, modimage + sfile.symbol_size + sfile.lineno_size,
2520                                    pdb_get_file_size(pdb_lookup, sfile.file) - sfile.symbol_size - sfile.lineno_size,
2521                                    files_image + 12, files_size);
2522
2523                 pdb_free(modimage);
2524             }
2525             file_name = (const char*)file + size;
2526             file_name += strlen(file_name) + 1;
2527             file = (BYTE*)((DWORD_PTR)(file_name + strlen(file_name) + 1 + 3) & ~3);
2528         }
2529         /* finish the remaining public and global information */
2530         if (globalimage)
2531         {
2532             codeview_snarf_public(msc_dbg, globalimage, 0,
2533                                   pdb_get_file_size(pdb_lookup, symbols.gsym_file));
2534
2535             pdb_free(globalimage);
2536         }
2537     }
2538     else
2539         pdb_process_symbol_imports(pcs, msc_dbg, NULL, NULL, image, pdb_lookup, 
2540                                    module_index);
2541     ret = TRUE;
2542
2543  leave:
2544     /* Cleanup */
2545     pdb_free(symbols_image);
2546     pdb_free(files_image);
2547     pdb_free_lookup(pdb_lookup);
2548
2549     if (image) UnmapViewOfFile(image);
2550     if (hMap) CloseHandle(hMap);
2551     if (hFile) CloseHandle(hFile);
2552
2553     return ret;
2554 }
2555
2556 static BOOL pdb_process_file(const struct process* pcs, 
2557                              const struct msc_debug_info* msc_dbg,
2558                              struct pdb_lookup* pdb_lookup)
2559 {
2560     BOOL        ret;
2561
2562     memset(cv_zmodules, 0, sizeof(cv_zmodules));
2563     codeview_init_basic_types(msc_dbg->module);
2564     ret = pdb_process_internal(pcs, msc_dbg, pdb_lookup, -1);
2565     codeview_clear_type_table();
2566     if (ret)
2567     {
2568         msc_dbg->module->module.SymType = SymCv;
2569         if (pdb_lookup->kind == PDB_JG)
2570             msc_dbg->module->module.PdbSig = pdb_lookup->u.jg.timestamp;
2571         else
2572             msc_dbg->module->module.PdbSig70 = pdb_lookup->u.ds.guid;
2573         msc_dbg->module->module.PdbAge = pdb_lookup->age;
2574         MultiByteToWideChar(CP_ACP, 0, pdb_lookup->filename, -1,
2575                             msc_dbg->module->module.LoadedPdbName,
2576                             sizeof(msc_dbg->module->module.LoadedPdbName) / sizeof(WCHAR));
2577         /* FIXME: we could have a finer grain here */
2578         msc_dbg->module->module.LineNumbers = TRUE;
2579         msc_dbg->module->module.GlobalSymbols = TRUE;
2580         msc_dbg->module->module.TypeInfo = TRUE;
2581         msc_dbg->module->module.SourceIndexed = TRUE;
2582         msc_dbg->module->module.Publics = TRUE;
2583     }
2584     return ret;
2585 }
2586
2587 BOOL pdb_fetch_file_info(struct pdb_lookup* pdb_lookup)
2588 {
2589     HANDLE              hFile, hMap = NULL;
2590     char*               image = NULL;
2591     BOOL                ret = TRUE;
2592
2593     if ((hFile = CreateFileA(pdb_lookup->filename, GENERIC_READ, FILE_SHARE_READ, NULL,
2594                              OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE ||
2595         ((hMap = CreateFileMappingW(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL) ||
2596         ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
2597     {
2598         WARN("Unable to open .PDB file: %s\n", pdb_lookup->filename);
2599         ret = FALSE;
2600     }
2601     else
2602     {
2603         pdb_init(pdb_lookup, image, TRUE);
2604         pdb_free_lookup(pdb_lookup);
2605     }
2606
2607     if (image) UnmapViewOfFile(image);
2608     if (hMap) CloseHandle(hMap);
2609     if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
2610
2611     return ret;
2612 }
2613
2614 /*========================================================================
2615  * Process CodeView debug information.
2616  */
2617
2618 #define MAKESIG(a,b,c,d)        ((a) | ((b) << 8) | ((c) << 16) | ((d) << 24))
2619 #define CODEVIEW_NB09_SIG       MAKESIG('N','B','0','9')
2620 #define CODEVIEW_NB10_SIG       MAKESIG('N','B','1','0')
2621 #define CODEVIEW_NB11_SIG       MAKESIG('N','B','1','1')
2622 #define CODEVIEW_RSDS_SIG       MAKESIG('R','S','D','S')
2623
2624 static BOOL codeview_process_info(const struct process* pcs, 
2625                                   const struct msc_debug_info* msc_dbg)
2626 {
2627     const DWORD*                signature = (const DWORD*)msc_dbg->root;
2628     BOOL                        ret = FALSE;
2629     struct pdb_lookup           pdb_lookup;
2630
2631     TRACE("Processing signature %.4s\n", (const char*)signature);
2632
2633     switch (*signature)
2634     {
2635     case CODEVIEW_NB09_SIG:
2636     case CODEVIEW_NB11_SIG:
2637     {
2638         const OMFSignature*     cv = (const OMFSignature*)msc_dbg->root;
2639         const OMFDirHeader*     hdr = (const OMFDirHeader*)(msc_dbg->root + cv->filepos);
2640         const OMFDirEntry*      ent;
2641         const OMFDirEntry*      prev;
2642         const OMFDirEntry*      next;
2643         unsigned int                    i;
2644
2645         codeview_init_basic_types(msc_dbg->module);
2646
2647         for (i = 0; i < hdr->cDir; i++)
2648         {
2649             ent = (const OMFDirEntry*)((const BYTE*)hdr + hdr->cbDirHeader + i * hdr->cbDirEntry);
2650             if (ent->SubSection == sstGlobalTypes)
2651             {
2652                 const OMFGlobalTypes*           types;
2653                 struct codeview_type_parse      ctp;
2654
2655                 types = (const OMFGlobalTypes*)(msc_dbg->root + ent->lfo);
2656                 ctp.module = msc_dbg->module;
2657                 ctp.offset = (const DWORD*)(types + 1);
2658                 ctp.num    = types->cTypes;
2659                 ctp.table  = (const BYTE*)(ctp.offset + types->cTypes);
2660
2661                 cv_current_module = &cv_zmodules[0];
2662                 if (cv_current_module->allowed) FIXME("Already allowed ??\n");
2663                 cv_current_module->allowed = TRUE;
2664
2665                 codeview_parse_type_table(&ctp);
2666                 break;
2667             }
2668         }
2669
2670         ent = (const OMFDirEntry*)((const BYTE*)hdr + hdr->cbDirHeader);
2671         for (i = 0; i < hdr->cDir; i++, ent = next)
2672         {
2673             next = (i == hdr->cDir-1) ? NULL :
2674                    (const OMFDirEntry*)((const BYTE*)ent + hdr->cbDirEntry);
2675             prev = (i == 0) ? NULL :
2676                    (const OMFDirEntry*)((const BYTE*)ent - hdr->cbDirEntry);
2677
2678             if (ent->SubSection == sstAlignSym)
2679             {
2680                 codeview_snarf(msc_dbg, msc_dbg->root + ent->lfo, sizeof(DWORD),
2681                                ent->cb, TRUE);
2682
2683                 /*
2684                  * Check the next and previous entry.  If either is a
2685                  * sstSrcModule, it contains the line number info for
2686                  * this file.
2687                  *
2688                  * FIXME: This is not a general solution!
2689                  */
2690                 if (next && next->iMod == ent->iMod && next->SubSection == sstSrcModule)
2691                     codeview_snarf_linetab(msc_dbg, msc_dbg->root + next->lfo,
2692                                            next->cb, TRUE);
2693
2694                 if (prev && prev->iMod == ent->iMod && prev->SubSection == sstSrcModule)
2695                     codeview_snarf_linetab(msc_dbg, msc_dbg->root + prev->lfo,
2696                                            prev->cb, TRUE);
2697
2698             }
2699         }
2700
2701         msc_dbg->module->module.SymType = SymCv;
2702         /* FIXME: we could have a finer grain here */
2703         msc_dbg->module->module.LineNumbers = TRUE;
2704         msc_dbg->module->module.GlobalSymbols = TRUE;
2705         msc_dbg->module->module.TypeInfo = TRUE;
2706         msc_dbg->module->module.SourceIndexed = TRUE;
2707         msc_dbg->module->module.Publics = TRUE;
2708         codeview_clear_type_table();
2709         ret = TRUE;
2710         break;
2711     }
2712
2713     case CODEVIEW_NB10_SIG:
2714     {
2715         const CODEVIEW_PDB_DATA* pdb = (const CODEVIEW_PDB_DATA*)msc_dbg->root;
2716         pdb_lookup.filename = pdb->name;
2717         pdb_lookup.kind = PDB_JG;
2718         pdb_lookup.u.jg.timestamp = pdb->timestamp;
2719         pdb_lookup.u.jg.toc = NULL;
2720         pdb_lookup.age = pdb->age;
2721         ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
2722         break;
2723     }
2724     case CODEVIEW_RSDS_SIG:
2725     {
2726         const OMFSignatureRSDS* rsds = (const OMFSignatureRSDS*)msc_dbg->root;
2727
2728         TRACE("Got RSDS type of PDB file: guid=%s age=%08x name=%s\n",
2729               wine_dbgstr_guid(&rsds->guid), rsds->age, rsds->name);
2730         pdb_lookup.filename = rsds->name;
2731         pdb_lookup.kind = PDB_DS;
2732         pdb_lookup.u.ds.guid = rsds->guid;
2733         pdb_lookup.u.ds.toc = NULL;
2734         pdb_lookup.age = rsds->age;
2735         ret = pdb_process_file(pcs, msc_dbg, &pdb_lookup);
2736         break;
2737     }
2738     default:
2739         ERR("Unknown CODEVIEW signature %08x in module %s\n",
2740             *signature, debugstr_w(msc_dbg->module->module.ModuleName));
2741         break;
2742     }
2743     if (ret)
2744     {
2745         msc_dbg->module->module.CVSig = *signature;
2746         memcpy(msc_dbg->module->module.CVData, msc_dbg->root,
2747                sizeof(msc_dbg->module->module.CVData));
2748     }
2749     return ret;
2750 }
2751
2752 /*========================================================================
2753  * Process debug directory.
2754  */
2755 BOOL pe_load_debug_directory(const struct process* pcs, struct module* module, 
2756                              const BYTE* mapping,
2757                              const IMAGE_SECTION_HEADER* sectp, DWORD nsect,
2758                              const IMAGE_DEBUG_DIRECTORY* dbg, int nDbg)
2759 {
2760     BOOL                        ret;
2761     int                         i;
2762     struct msc_debug_info       msc_dbg;
2763
2764     msc_dbg.module = module;
2765     msc_dbg.nsect  = nsect;
2766     msc_dbg.sectp  = sectp;
2767     msc_dbg.nomap  = 0;
2768     msc_dbg.omapp  = NULL;
2769
2770     __TRY
2771     {
2772         ret = FALSE;
2773
2774         /* First, watch out for OMAP data */
2775         for (i = 0; i < nDbg; i++)
2776         {
2777             if (dbg[i].Type == IMAGE_DEBUG_TYPE_OMAP_FROM_SRC)
2778             {
2779                 msc_dbg.nomap = dbg[i].SizeOfData / sizeof(OMAP_DATA);
2780                 msc_dbg.omapp = (const OMAP_DATA*)(mapping + dbg[i].PointerToRawData);
2781                 break;
2782             }
2783         }
2784   
2785         /* Now, try to parse CodeView debug info */
2786         for (i = 0; i < nDbg; i++)
2787         {
2788             if (dbg[i].Type == IMAGE_DEBUG_TYPE_CODEVIEW)
2789             {
2790                 msc_dbg.root = mapping + dbg[i].PointerToRawData;
2791                 if ((ret = codeview_process_info(pcs, &msc_dbg))) goto done;
2792             }
2793         }
2794     
2795         /* If not found, try to parse COFF debug info */
2796         for (i = 0; i < nDbg; i++)
2797         {
2798             if (dbg[i].Type == IMAGE_DEBUG_TYPE_COFF)
2799             {
2800                 msc_dbg.root = mapping + dbg[i].PointerToRawData;
2801                 if ((ret = coff_process_info(&msc_dbg))) goto done;
2802             }
2803         }
2804     done:
2805          /* FIXME: this should be supported... this is the debug information for
2806           * functions compiled without a frame pointer (FPO = frame pointer omission)
2807           * the associated data helps finding out the relevant information
2808           */
2809         for (i = 0; i < nDbg; i++)
2810             if (dbg[i].Type == IMAGE_DEBUG_TYPE_FPO)
2811                 FIXME("This guy has FPO information\n");
2812 #if 0
2813
2814 #define FRAME_FPO   0
2815 #define FRAME_TRAP  1
2816 #define FRAME_TSS   2
2817
2818 typedef struct _FPO_DATA 
2819 {
2820         DWORD       ulOffStart;            /* offset 1st byte of function code */
2821         DWORD       cbProcSize;            /* # bytes in function */
2822         DWORD       cdwLocals;             /* # bytes in locals/4 */
2823         WORD        cdwParams;             /* # bytes in params/4 */
2824
2825         WORD        cbProlog : 8;          /* # bytes in prolog */
2826         WORD        cbRegs   : 3;          /* # regs saved */
2827         WORD        fHasSEH  : 1;          /* TRUE if SEH in func */
2828         WORD        fUseBP   : 1;          /* TRUE if EBP has been allocated */
2829         WORD        reserved : 1;          /* reserved for future use */
2830         WORD        cbFrame  : 2;          /* frame type */
2831 } FPO_DATA;
2832 #endif
2833
2834     }
2835     __EXCEPT_PAGE_FAULT
2836     {
2837         ERR("Got a page fault while loading symbols\n");
2838         ret = FALSE;
2839     }
2840     __ENDTRY
2841     return ret;
2842 }