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