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