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