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