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