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