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