Removed W->A from DEFWND_ImmIsUIMessageW.
[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,      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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 <stdlib.h>
40
41 #include <string.h>
42 #ifdef HAVE_UNISTD_H
43 # include <unistd.h>
44 #endif
45 #ifndef PATH_MAX
46 #define PATH_MAX MAX_PATH
47 #endif
48 #include <stdarg.h>
49 #include "windef.h"
50 #include "winbase.h"
51 #include "winreg.h"
52 #include "winternl.h"
53
54 #include "wine/exception.h"
55 #include "wine/debug.h"
56 #include "excpt.h"
57 #include "dbghelp_private.h"
58
59 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_msc);
60
61 #define MAX_PATHNAME_LEN 1024
62
63 typedef struct
64 {
65     DWORD  from;
66     DWORD  to;
67 } OMAP_DATA;
68
69 struct msc_debug_info
70 {
71     struct module*              module;
72     int                         nsect;
73     const IMAGE_SECTION_HEADER* sectp;
74     int                         nomap;
75     const OMAP_DATA*            omapp;
76     const BYTE*                 root;
77 };
78
79 /*========================================================================
80  * Debug file access helper routines
81  */
82
83 static WINE_EXCEPTION_FILTER(page_fault)
84 {
85     if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
86         return EXCEPTION_EXECUTE_HANDLER;
87     return EXCEPTION_CONTINUE_SEARCH;
88 }
89
90 /*========================================================================
91  * Process COFF debug information.
92  */
93
94 struct CoffFile
95 {
96     unsigned int                startaddr;
97     unsigned int                endaddr;
98     struct symt_compiland*      compiland;
99     int                         linetab_offset;
100     int                         linecnt;
101     struct symt**               entries;
102     int                         neps;
103     int                         neps_alloc;
104 };
105
106 struct CoffFileSet
107 {
108     struct CoffFile*    files;
109     int                 nfiles;
110     int                 nfiles_alloc;
111 };
112
113 static const char*      coff_get_name(const IMAGE_SYMBOL* coff_sym, 
114                                       const char* coff_strtab)
115 {
116     static      char    namebuff[9];
117     const char*         nampnt;
118
119     if (coff_sym->N.Name.Short)
120     {
121         memcpy(namebuff, coff_sym->N.ShortName, 8);
122         namebuff[8] = '\0';
123         nampnt = &namebuff[0];
124     }
125     else
126     {
127         nampnt = coff_strtab + coff_sym->N.Name.Long;
128     }
129
130     if (nampnt[0] == '_') nampnt++;
131     return nampnt;
132 }
133
134 static int coff_add_file(struct CoffFileSet* coff_files, struct module* module,
135                          const char* filename)
136 {
137     struct CoffFile* file;
138
139     if (coff_files->nfiles + 1 >= coff_files->nfiles_alloc)
140     {
141         coff_files->nfiles_alloc += 10;
142         coff_files->files = (coff_files->files) ?
143             HeapReAlloc(GetProcessHeap(), 0, coff_files->files,
144                         coff_files->nfiles_alloc * sizeof(struct CoffFile)) :
145             HeapAlloc(GetProcessHeap(), 0,
146                       coff_files->nfiles_alloc * sizeof(struct CoffFile));
147     }
148     file = coff_files->files + coff_files->nfiles;
149     file->startaddr = 0xffffffff;
150     file->endaddr   = 0;
151     file->compiland = symt_new_compiland(module, filename);
152     file->linetab_offset = -1;
153     file->linecnt = 0;
154     file->entries = NULL;
155     file->neps = file->neps_alloc = 0;
156
157     return coff_files->nfiles++;
158 }
159
160 static void coff_add_symbol(struct CoffFile* coff_file, struct symt* sym)
161 {
162     if (coff_file->neps + 1 >= coff_file->neps_alloc)
163     {
164         coff_file->neps_alloc += 10;
165         coff_file->entries = (coff_file->entries) ?
166             HeapReAlloc(GetProcessHeap(), 0, coff_file->entries,
167                         coff_file->neps_alloc * sizeof(struct symt*)) :
168             HeapAlloc(GetProcessHeap(), 0, 
169                       coff_file->neps_alloc * sizeof(struct symt*));
170     }
171     coff_file->entries[coff_file->neps++] = sym;
172 }
173
174 static BOOL coff_process_info(const struct msc_debug_info* msc_dbg)
175 {
176     const IMAGE_AUX_SYMBOL*             aux;
177     const IMAGE_COFF_SYMBOLS_HEADER*    coff;
178     const IMAGE_LINENUMBER*             coff_linetab;
179     const IMAGE_LINENUMBER*             linepnt;
180     const char*                         coff_strtab;
181     const IMAGE_SYMBOL*                 coff_sym;
182     const IMAGE_SYMBOL*                 coff_symbols;
183     struct CoffFileSet                  coff_files;
184     int                                 curr_file_idx = -1;
185     unsigned int                        i;
186     int                                 j;
187     int                                 k;
188     int                                 l;
189     int                                 linetab_indx;
190     const char*                         nampnt;
191     int                                 naux;
192     BOOL                                ret = FALSE;
193     DWORD                               addr;
194
195     TRACE("Processing COFF symbols...\n");
196
197     assert(sizeof(IMAGE_SYMBOL) == IMAGE_SIZEOF_SYMBOL);
198     assert(sizeof(IMAGE_LINENUMBER) == IMAGE_SIZEOF_LINENUMBER);
199
200     coff_files.files = NULL;
201     coff_files.nfiles = coff_files.nfiles_alloc = 0;
202
203     coff = (const IMAGE_COFF_SYMBOLS_HEADER*)msc_dbg->root;
204
205     coff_symbols = (const IMAGE_SYMBOL*)((unsigned int)coff + 
206                                          coff->LvaToFirstSymbol);
207     coff_linetab = (const IMAGE_LINENUMBER*)((unsigned int)coff + 
208                                              coff->LvaToFirstLinenumber);
209     coff_strtab = (const char*)(coff_symbols + coff->NumberOfSymbols);
210
211     linetab_indx = 0;
212
213     for (i = 0; i < coff->NumberOfSymbols; i++)
214     {
215         coff_sym = coff_symbols + i;
216         naux = coff_sym->NumberOfAuxSymbols;
217
218         if (coff_sym->StorageClass == IMAGE_SYM_CLASS_FILE)
219         {
220             curr_file_idx = coff_add_file(&coff_files, msc_dbg->module, 
221                                           (const char*)(coff_sym + 1));
222             TRACE("New file %s\n", (const char*)(coff_sym + 1));
223             i += naux;
224             continue;
225         }
226
227         if (curr_file_idx < 0)
228         {
229             assert(coff_files.nfiles == 0 && coff_files.nfiles_alloc == 0);
230             curr_file_idx = coff_add_file(&coff_files, msc_dbg->module, "<none>");
231             TRACE("New file <none>\n");
232         }
233
234         /*
235          * This guy marks the size and location of the text section
236          * for the current file.  We need to keep track of this so
237          * we can figure out what file the different global functions
238          * go with.
239          */
240         if (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC &&
241             naux != 0 && coff_sym->Type == 0 && coff_sym->SectionNumber == 1)
242         {
243             aux = (const IMAGE_AUX_SYMBOL*) (coff_sym + 1);
244
245             if (coff_files.files[curr_file_idx].linetab_offset != -1)
246             {
247                 /*
248                  * Save this so we can still get the old name.
249                  */
250                 const char* fn;
251
252                 fn = source_get(msc_dbg->module, 
253                                 coff_files.files[curr_file_idx].compiland->source);
254
255                 TRACE("Duplicating sect from %s: %lx %x %x %d %d\n",
256                       fn, aux->Section.Length,
257                       aux->Section.NumberOfRelocations,
258                       aux->Section.NumberOfLinenumbers,
259                       aux->Section.Number, aux->Section.Selection);
260                 TRACE("More sect %d %s %08lx %d %d %d\n",
261                       coff_sym->SectionNumber,
262                       coff_get_name(coff_sym, coff_strtab),
263                       coff_sym->Value, coff_sym->Type,
264                       coff_sym->StorageClass, coff_sym->NumberOfAuxSymbols);
265
266                 /*
267                  * Duplicate the file entry.  We have no way to describe
268                  * multiple text sections in our current way of handling things.
269                  */
270                 coff_add_file(&coff_files, msc_dbg->module, fn);
271             }
272             else
273             {
274                 TRACE("New text sect from %s: %lx %x %x %d %d\n",
275                       source_get(msc_dbg->module, coff_files.files[curr_file_idx].compiland->source),
276                       aux->Section.Length,
277                       aux->Section.NumberOfRelocations,
278                       aux->Section.NumberOfLinenumbers,
279                       aux->Section.Number, aux->Section.Selection);
280             }
281
282             if (coff_files.files[curr_file_idx].startaddr > coff_sym->Value)
283             {
284                 coff_files.files[curr_file_idx].startaddr = coff_sym->Value;
285             }
286
287             if (coff_files.files[curr_file_idx].endaddr < coff_sym->Value + aux->Section.Length)
288             {
289                 coff_files.files[curr_file_idx].endaddr = coff_sym->Value + aux->Section.Length;
290             }
291
292             coff_files.files[curr_file_idx].linetab_offset = linetab_indx;
293             coff_files.files[curr_file_idx].linecnt = aux->Section.NumberOfLinenumbers;
294             linetab_indx += aux->Section.NumberOfLinenumbers;
295             i += naux;
296             continue;
297         }
298
299         if (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC && naux == 0 && 
300             coff_sym->SectionNumber == 1)
301         {
302             DWORD base = msc_dbg->sectp[coff_sym->SectionNumber - 1].VirtualAddress;
303             /*
304              * This is a normal static function when naux == 0.
305              * Just register it.  The current file is the correct
306              * one in this instance.
307              */
308             nampnt = coff_get_name(coff_sym, coff_strtab);
309
310             TRACE("\tAdding static symbol %s\n", nampnt);
311
312             /* FIXME: was adding symbol to this_file ??? */
313             coff_add_symbol(&coff_files.files[curr_file_idx],
314                             &symt_new_function(msc_dbg->module, 
315                                                coff_files.files[curr_file_idx].compiland, 
316                                                nampnt,
317                                                msc_dbg->module->module.BaseOfImage + base + coff_sym->Value,
318                                                0 /* FIXME */,
319                                                NULL /* FIXME */)->symt);
320             i += naux;
321             continue;
322         }
323
324         if (coff_sym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
325             ISFCN(coff_sym->Type) && coff_sym->SectionNumber > 0)
326         {
327             struct symt_compiland* compiland = NULL;
328             DWORD base = msc_dbg->sectp[coff_sym->SectionNumber - 1].VirtualAddress;
329             nampnt = coff_get_name(coff_sym, coff_strtab);
330
331             TRACE("%d: %lx %s\n",
332                   i, msc_dbg->module->module.BaseOfImage + base + coff_sym->Value,
333                   nampnt);
334             TRACE("\tAdding global symbol %s (sect=%s)\n",
335                   nampnt, msc_dbg->sectp[coff_sym->SectionNumber - 1].Name);
336
337             /*
338              * Now we need to figure out which file this guy belongs to.
339              */
340             for (j = 0; j < coff_files.nfiles; j++)
341             {
342                 if (coff_files.files[j].startaddr <= base + coff_sym->Value
343                      && coff_files.files[j].endaddr > base + coff_sym->Value)
344                 {
345                     compiland = coff_files.files[j].compiland;
346                     break;
347                 }
348             }
349             if (j < coff_files.nfiles)
350             {
351                 coff_add_symbol(&coff_files.files[j],
352                                 &symt_new_function(msc_dbg->module, compiland, nampnt, 
353                                                    msc_dbg->module->module.BaseOfImage + base + coff_sym->Value,
354                                                    0 /* FIXME */, NULL /* FIXME */)->symt);
355             } 
356             else 
357             {
358                 symt_new_function(msc_dbg->module, NULL, nampnt, 
359                                   msc_dbg->module->module.BaseOfImage + base + coff_sym->Value,
360                                   0 /* FIXME */, NULL /* FIXME */);
361             }
362             i += naux;
363             continue;
364         }
365
366         if (coff_sym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
367             coff_sym->SectionNumber > 0)
368         {
369             DWORD base = msc_dbg->sectp[coff_sym->SectionNumber - 1].VirtualAddress;
370             /*
371              * Similar to above, but for the case of data symbols.
372              * These aren't treated as entrypoints.
373              */
374             nampnt = coff_get_name(coff_sym, coff_strtab);
375
376             TRACE("%d: %lx %s\n",
377                   i, msc_dbg->module->module.BaseOfImage + base + coff_sym->Value,
378                   nampnt);
379             TRACE("\tAdding global data symbol %s\n", nampnt);
380
381             /*
382              * Now we need to figure out which file this guy belongs to.
383              */
384             symt_new_global_variable(msc_dbg->module, NULL, nampnt, TRUE /* FIXME */,
385                                      msc_dbg->module->module.BaseOfImage + base + coff_sym->Value,
386                                      0 /* FIXME */, NULL /* FIXME */);
387             i += naux;
388             continue;
389         }
390
391         if (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC && naux == 0)
392         {
393             /*
394              * Ignore these.  They don't have anything to do with
395              * reality.
396              */
397             i += naux;
398             continue;
399         }
400
401         TRACE("Skipping unknown entry '%s' %d %d %d\n",
402               coff_get_name(coff_sym, coff_strtab),
403               coff_sym->StorageClass, coff_sym->SectionNumber, naux);
404         
405         /*
406          * For now, skip past the aux entries.
407          */
408         i += naux;
409     }
410
411     if (coff_files.files != NULL)
412     {
413         /*
414          * OK, we now should have a list of files, and we should have a list
415          * of entrypoints.  We need to sort the entrypoints so that we are
416          * able to tie the line numbers with the given functions within the
417          * file.
418          */
419         for (j = 0; j < coff_files.nfiles; j++)
420         {
421             if (coff_files.files[j].entries != NULL)
422             {
423                 qsort(coff_files.files[j].entries, coff_files.files[j].neps,
424                       sizeof(struct symt*), symt_cmp_addr);
425             }
426         }
427
428         /*
429          * Now pick apart the line number tables, and attach the entries
430          * to the given functions.
431          */
432         for (j = 0; j < coff_files.nfiles; j++)
433         {
434             l = 0;
435             if (coff_files.files[j].neps != 0)
436             {
437                 for (k = 0; k < coff_files.files[j].linecnt; k++)
438                 {
439                     linepnt = coff_linetab + coff_files.files[j].linetab_offset + k;
440                     /*
441                      * If we have spilled onto the next entrypoint, then
442                      * bump the counter..
443                      */
444                     for (;;)
445                     {
446                         if (l+1 >= coff_files.files[j].neps) break;
447                         symt_get_info(coff_files.files[j].entries[l+1], TI_GET_ADDRESS, &addr);
448                         if (((msc_dbg->module->module.BaseOfImage + linepnt->Type.VirtualAddress) < addr))
449                             break;
450                         l++;
451                     }
452
453                     if (coff_files.files[j].entries[l+1]->tag == SymTagFunction)
454                     {
455                         /*
456                          * Add the line number.  This is always relative to the
457                          * start of the function, so we need to subtract that offset
458                          * first.
459                          */
460                         symt_get_info(coff_files.files[j].entries[l+1], TI_GET_ADDRESS, &addr);
461                         symt_add_func_line(msc_dbg->module, (struct symt_function*)coff_files.files[j].entries[l+1], 
462                                            coff_files.files[j].compiland->source, linepnt->Linenumber,
463                                            msc_dbg->module->module.BaseOfImage + linepnt->Type.VirtualAddress - addr);
464                     }
465                 }
466             }
467         }
468
469         for (j = 0; j < coff_files.nfiles; j++)
470         {
471             if (coff_files.files[j].entries != NULL)
472             {
473                 HeapFree(GetProcessHeap(), 0, coff_files.files[j].entries);
474             }
475         }
476         HeapFree(GetProcessHeap(), 0, coff_files.files);
477         msc_dbg->module->module.SymType = SymCoff;
478         ret = TRUE;
479     }
480
481     return ret;
482 }
483
484
485 /*========================================================================
486  * Process CodeView type information.
487  */
488
489 union codeview_type
490 {
491     struct
492     {
493         unsigned short int  len;
494         short int           id;
495     } generic;
496
497     struct
498     {
499         unsigned short int len;
500         short int          id;
501         short int          attribute;
502         short int          datatype;
503         unsigned char      variant[1];
504     } pointer;
505
506     struct
507     {
508         unsigned short int len;
509         short int          id;
510         unsigned int       datatype;
511         unsigned int       attribute;
512         unsigned char      variant[1];
513     } pointer32;
514
515     struct
516     {
517         unsigned short int len;
518         short int          id;
519         unsigned char      nbits;
520         unsigned char      bitoff;
521         unsigned short     type;
522     } bitfield;
523     
524     struct
525     {
526         unsigned short int len;
527         short int          id;
528         unsigned int       type;
529         unsigned char      nbits;
530         unsigned char      bitoff;
531     } bitfield32;
532
533     struct
534     {
535         unsigned short int len;
536         short int          id;
537         short int          elemtype;
538         short int          idxtype;
539         unsigned short int arrlen;     /* numeric leaf */
540 #if 0
541         unsigned char      name[1];
542 #endif
543     } array;
544
545     struct
546     {
547         unsigned short int len;
548         short int          id;
549         unsigned int       elemtype;
550         unsigned int       idxtype;
551         unsigned short int arrlen;    /* numeric leaf */
552 #if 0
553         unsigned char      name[1];
554 #endif
555     } array32;
556
557     struct
558     {
559         unsigned short int len;
560         short int          id;
561         short int          n_element;
562         short int          fieldlist;
563         short int          property;
564         short int          derived;
565         short int          vshape;
566         unsigned short int structlen;  /* numeric leaf */
567 #if 0
568         unsigned char      name[1];
569 #endif
570     } structure;
571
572   struct
573   {
574       unsigned short int len;
575       short int          id;
576       short int          n_element;
577       short int          property;
578       unsigned int       fieldlist;
579       unsigned int       derived;
580       unsigned int       vshape;
581       unsigned short int structlen;  /* numeric leaf */
582 #if 0
583       unsigned char      name[1];
584 #endif
585   } structure32;
586
587   struct
588   {
589       unsigned short int len;
590       short int          id;
591       short int          count;
592       short int          fieldlist;
593       short int          property;
594       unsigned short int un_len;     /* numeric leaf */
595 #if 0
596       unsigned char      name[1];
597 #endif
598   } t_union;
599
600     struct
601     {
602         unsigned short int len;
603         short int          id;
604         short int          count;
605         short int          property;
606         unsigned int       fieldlist;
607         unsigned short int un_len;     /* numeric leaf */
608 #if 0
609         unsigned char      name[1];
610 #endif
611     } t_union32;
612
613     struct
614     {
615         unsigned short int len;
616         short int          id;
617         short int          count;
618         short int          type;
619         short int          field;
620         short int          property;
621         unsigned char      name[1];
622     } enumeration;
623
624     struct
625     {
626         unsigned short int len;
627         short int          id;
628         short int          count;
629         short int          property;
630         unsigned int       type;
631         unsigned int       field;
632         unsigned char      name[1];
633     } enumeration32;
634
635     struct
636     {
637         unsigned short int len;
638         short int          id;
639         unsigned char      list[1];
640     } fieldlist;
641
642     struct
643     {
644         unsigned short int len;
645         short int          id;
646         unsigned short int rvtype;
647         unsigned char      call;
648         unsigned char      reserved;
649         unsigned short int params;
650         unsigned short int arglist;
651     } procedure;
652
653     struct
654     {
655         unsigned short int len;
656         short int          id;
657         unsigned int       rvtype;
658         unsigned char      call;
659         unsigned char      reserved;
660         unsigned short int params;
661         unsigned int       arglist;
662     } procedure32;
663 };
664
665 union codeview_fieldtype
666 {
667     struct
668     {
669         short int               id;
670     } generic;
671
672     struct
673     {
674         short int               id;
675         short int               type;
676         short int               attribute;
677         unsigned short int      offset;     /* numeric leaf */
678     } bclass;
679
680     struct
681     {
682         short int               id;
683         short int               attribute;
684         unsigned int            type;
685         unsigned short int      offset;     /* numeric leaf */
686     } bclass32;
687
688     struct
689     {
690         short int               id;
691         short int               btype;
692         short int               vbtype;
693         short int               attribute;
694         unsigned short int      vbpoff;     /* numeric leaf */
695 #if 0
696         unsigned short int      vboff;      /* numeric leaf */
697 #endif
698     } vbclass;
699
700     struct
701     {
702         short int               id;
703         short int               attribute;
704         unsigned int            btype;
705         unsigned int            vbtype;
706         unsigned short int      vbpoff;     /* numeric leaf */
707 #if 0
708         unsigned short int      vboff;      /* numeric leaf */
709 #endif
710     } vbclass32;
711
712     struct
713     {
714         short int               id;
715         short int               attribute;
716         unsigned short int      value;     /* numeric leaf */
717 #if 0
718         unsigned char           name[1];
719 #endif
720     } enumerate;
721
722     struct
723     {
724         short int               id;
725         short int               type;
726         unsigned char           name[1];
727     } friendfcn;
728
729     struct
730     {
731         short int               id;
732         short int               _pad0;
733         unsigned int            type;
734         unsigned char           name[1];
735     } friendfcn32;
736
737     struct
738     {
739         short int               id;
740         short int               type;
741         short int               attribute;
742         unsigned short int      offset;    /* numeric leaf */
743 #if 0
744         unsigned char           name[1];
745 #endif
746     } member;
747
748     struct
749     {
750         short int               id;
751         short int               attribute;
752         unsigned int            type;
753         unsigned short int      offset;    /* numeric leaf */
754 #if 0
755         unsigned char           name[1];
756 #endif
757     } member32;
758
759     struct
760     {
761         short int               id;
762         short int               type;
763         short int               attribute;
764         unsigned char           name[1];
765     } stmember;
766
767     struct
768     {
769         short int               id;
770         short int               attribute;
771         unsigned int            type;
772         unsigned char           name[1];
773     } stmember32;
774
775     struct
776     {
777         short int               id;
778         short int               count;
779         short int               mlist;
780         unsigned char           name[1];
781     } method;
782
783     struct
784     {
785         short int               id;
786         short int               count;
787         unsigned int            mlist;
788         unsigned char           name[1];
789     } method32;
790
791     struct
792     {
793         short int               id;
794         short int               index;
795         unsigned char           name[1];
796     } nesttype;
797
798     struct
799     {
800         short int               id;
801         short int               _pad0;
802         unsigned int            index;
803         unsigned char           name[1];
804     } nesttype32;
805
806     struct
807     {
808         short int               id;
809         short int               type;
810     } vfunctab;
811
812     struct
813     {
814         short int               id;
815         short int               _pad0;
816         unsigned int            type;
817     } vfunctab32;
818
819     struct
820     {
821         short int               id;
822         short int               type;
823     } friendcls;
824
825     struct
826     {
827         short int               id;
828         short int               _pad0;
829         unsigned int            type;
830     } friendcls32;
831
832     struct
833     {
834         short int               id;
835         short int               attribute;
836         short int               type;
837         unsigned char           name[1];
838     } onemethod;
839
840     struct
841     {
842         short int               id;
843         short int               attribute;
844         short int               type;
845         unsigned int            vtab_offset;
846         unsigned char           name[1];
847     } onemethod_virt;
848
849     struct
850     {
851         short int               id;
852         short int               attribute;
853         unsigned int            type;
854         unsigned char           name[1];
855     } onemethod32;
856
857     struct
858     {
859         short int               id;
860         short int               attribute;
861         unsigned int            type;
862         unsigned int            vtab_offset;
863         unsigned char           name[1];
864     } onemethod32_virt;
865
866     struct
867     {
868         short int               id;
869         short int               type;
870         unsigned int            offset;
871     } vfuncoff;
872
873     struct
874     {
875         short int               id;
876         short int               _pad0;
877         unsigned int            type;
878         unsigned int            offset;
879     } vfuncoff32;
880
881     struct
882     {
883         short int               id;
884         short int               attribute;
885         short int               index;
886         unsigned char           name[1];
887     } nesttypeex;
888
889     struct
890     {
891         short int               id;
892         short int               attribute;
893         unsigned int            index;
894         unsigned char           name[1];
895     } nesttypeex32;
896
897     struct
898     {
899         short int               id;
900         short int               attribute;
901         unsigned int            type;
902         unsigned char           name[1];
903     } membermodify;
904 };
905
906
907 /*
908  * This covers the basic datatypes that VC++ seems to be using these days.
909  * 32 bit mode only.  There are additional numbers for the pointers in 16
910  * bit mode.  There are many other types listed in the documents, but these
911  * are apparently not used by the compiler, or represent pointer types
912  * that are not used.
913  */
914 #define T_NOTYPE        0x0000  /* Notype */
915 #define T_ABS           0x0001  /* Abs */
916 #define T_VOID          0x0003  /* Void */
917 #define T_CHAR          0x0010  /* signed char */
918 #define T_SHORT         0x0011  /* short */
919 #define T_LONG          0x0012  /* long */
920 #define T_QUAD          0x0013  /* long long */
921 #define T_UCHAR         0x0020  /* unsigned  char */
922 #define T_USHORT        0x0021  /* unsigned short */
923 #define T_ULONG         0x0022  /* unsigned long */
924 #define T_UQUAD         0x0023  /* unsigned long long */
925 #define T_REAL32        0x0040  /* float */
926 #define T_REAL64        0x0041  /* double */
927 #define T_RCHAR         0x0070  /* real char */
928 #define T_WCHAR         0x0071  /* wide char */
929 #define T_INT4          0x0074  /* int */
930 #define T_UINT4         0x0075  /* unsigned int */
931
932 #define T_32PVOID       0x0403  /* 32 bit near pointer to void */
933 #define T_32PCHAR       0x0410  /* 16:32 near pointer to signed char */
934 #define T_32PSHORT      0x0411  /* 16:32 near pointer to short */
935 #define T_32PLONG       0x0412  /* 16:32 near pointer to int */
936 #define T_32PQUAD       0x0413  /* 16:32 near pointer to long long */
937 #define T_32PUCHAR      0x0420  /* 16:32 near pointer to unsigned char */
938 #define T_32PUSHORT     0x0421  /* 16:32 near pointer to unsigned short */
939 #define T_32PULONG      0x0422  /* 16:32 near pointer to unsigned int */
940 #define T_32PUQUAD      0x0423  /* 16:32 near pointer to long long */
941 #define T_32PREAL32     0x0440  /* 16:32 near pointer to float */
942 #define T_32PREAL64     0x0441  /* 16:32 near pointer to float */
943 #define T_32PRCHAR      0x0470  /* 16:32 near pointer to real char */
944 #define T_32PWCHAR      0x0471  /* 16:32 near pointer to real char */
945 #define T_32PINT4       0x0474  /* 16:32 near pointer to int */
946 #define T_32PUINT4      0x0475  /* 16:32 near pointer to unsigned int */
947
948
949 #define LF_MODIFIER     0x0001
950 #define LF_POINTER      0x0002
951 #define LF_ARRAY        0x0003
952 #define LF_CLASS        0x0004
953 #define LF_STRUCTURE    0x0005
954 #define LF_UNION        0x0006
955 #define LF_ENUM         0x0007
956 #define LF_PROCEDURE    0x0008
957 #define LF_MFUNCTION    0x0009
958 #define LF_VTSHAPE      0x000a
959 #define LF_COBOL0       0x000b
960 #define LF_COBOL1       0x000c
961 #define LF_BARRAY       0x000d
962 #define LF_LABEL        0x000e
963 #define LF_NULL         0x000f
964 #define LF_NOTTRAN      0x0010
965 #define LF_DIMARRAY     0x0011
966 #define LF_VFTPATH      0x0012
967 #define LF_PRECOMP      0x0013
968 #define LF_ENDPRECOMP   0x0014
969 #define LF_OEM          0x0015
970 #define LF_TYPESERVER   0x0016
971
972 #define LF_MODIFIER_32  0x1001     /* variants with new 32-bit type indices */
973 #define LF_POINTER_32   0x1002
974 #define LF_ARRAY_32     0x1003
975 #define LF_CLASS_32     0x1004
976 #define LF_STRUCTURE_32 0x1005
977 #define LF_UNION_32     0x1006
978 #define LF_ENUM_32      0x1007
979 #define LF_PROCEDURE_32 0x1008
980 #define LF_MFUNCTION_32 0x1009
981 #define LF_COBOL0_32    0x100a
982 #define LF_BARRAY_32    0x100b
983 #define LF_DIMARRAY_32  0x100c
984 #define LF_VFTPATH_32   0x100d
985 #define LF_PRECOMP_32   0x100e
986 #define LF_OEM_32       0x100f
987
988 #define LF_SKIP         0x0200
989 #define LF_ARGLIST      0x0201
990 #define LF_DEFARG       0x0202
991 #define LF_LIST         0x0203
992 #define LF_FIELDLIST    0x0204
993 #define LF_DERIVED      0x0205
994 #define LF_BITFIELD     0x0206
995 #define LF_METHODLIST   0x0207
996 #define LF_DIMCONU      0x0208
997 #define LF_DIMCONLU     0x0209
998 #define LF_DIMVARU      0x020a
999 #define LF_DIMVARLU     0x020b
1000 #define LF_REFSYM       0x020c
1001
1002 #define LF_SKIP_32      0x1200    /* variants with new 32-bit type indices */
1003 #define LF_ARGLIST_32   0x1201
1004 #define LF_DEFARG_32    0x1202
1005 #define LF_FIELDLIST_32 0x1203
1006 #define LF_DERIVED_32   0x1204
1007 #define LF_BITFIELD_32  0x1205
1008 #define LF_METHODLIST_32 0x1206
1009 #define LF_DIMCONU_32   0x1207
1010 #define LF_DIMCONLU_32  0x1208
1011 #define LF_DIMVARU_32   0x1209
1012 #define LF_DIMVARLU_32  0x120a
1013
1014 #define LF_BCLASS       0x0400
1015 #define LF_VBCLASS      0x0401
1016 #define LF_IVBCLASS     0x0402
1017 #define LF_ENUMERATE    0x0403
1018 #define LF_FRIENDFCN    0x0404
1019 #define LF_INDEX        0x0405
1020 #define LF_MEMBER       0x0406
1021 #define LF_STMEMBER     0x0407
1022 #define LF_METHOD       0x0408
1023 #define LF_NESTTYPE     0x0409
1024 #define LF_VFUNCTAB     0x040a
1025 #define LF_FRIENDCLS    0x040b
1026 #define LF_ONEMETHOD    0x040c
1027 #define LF_VFUNCOFF     0x040d
1028 #define LF_NESTTYPEEX   0x040e
1029 #define LF_MEMBERMODIFY 0x040f
1030
1031 #define LF_BCLASS_32    0x1400    /* variants with new 32-bit type indices */
1032 #define LF_VBCLASS_32   0x1401
1033 #define LF_IVBCLASS_32  0x1402
1034 #define LF_FRIENDFCN_32 0x1403
1035 #define LF_INDEX_32     0x1404
1036 #define LF_MEMBER_32    0x1405
1037 #define LF_STMEMBER_32  0x1406
1038 #define LF_METHOD_32    0x1407
1039 #define LF_NESTTYPE_32  0x1408
1040 #define LF_VFUNCTAB_32  0x1409
1041 #define LF_FRIENDCLS_32 0x140a
1042 #define LF_ONEMETHOD_32 0x140b
1043 #define LF_VFUNCOFF_32  0x140c
1044 #define LF_NESTTYPEEX_32 0x140d
1045
1046 #define LF_NUMERIC      0x8000    /* numeric leaf types */
1047 #define LF_CHAR         0x8000
1048 #define LF_SHORT        0x8001
1049 #define LF_USHORT       0x8002
1050 #define LF_LONG         0x8003
1051 #define LF_ULONG        0x8004
1052 #define LF_REAL32       0x8005
1053 #define LF_REAL64       0x8006
1054 #define LF_REAL80       0x8007
1055 #define LF_REAL128      0x8008
1056 #define LF_QUADWORD     0x8009
1057 #define LF_UQUADWORD    0x800a
1058 #define LF_REAL48       0x800b
1059 #define LF_COMPLEX32    0x800c
1060 #define LF_COMPLEX64    0x800d
1061 #define LF_COMPLEX80    0x800e
1062 #define LF_COMPLEX128   0x800f
1063 #define LF_VARSTRING    0x8010
1064
1065 #define MAX_BUILTIN_TYPES       0x480
1066 static struct symt*     cv_basic_types[MAX_BUILTIN_TYPES];
1067 static unsigned int     num_cv_defined_types = 0;
1068 static struct symt**    cv_defined_types = NULL;
1069 #define SymTagCVBitField        (SymTagMax + 0x100)
1070 struct codeview_bitfield
1071 {
1072     struct symt symt;
1073     unsigned    subtype;
1074     unsigned    bitposition;
1075     unsigned    bitlength;
1076 };
1077 static struct codeview_bitfield*        cv_bitfields;
1078 static unsigned                         num_cv_bitfields;
1079 static unsigned                         used_cv_bitfields;
1080
1081 static void codeview_init_basic_types(struct module* module)
1082 {
1083     /*
1084      * These are the common builtin types that are used by VC++.
1085      */
1086     cv_basic_types[T_NOTYPE] = NULL;
1087     cv_basic_types[T_ABS]    = NULL;
1088     cv_basic_types[T_VOID]   = &symt_new_basic(module, btVoid,  "void", 0)->symt;
1089     cv_basic_types[T_CHAR]   = &symt_new_basic(module, btChar,  "char", 1)->symt;
1090     cv_basic_types[T_SHORT]  = &symt_new_basic(module, btInt,   "short int", 2)->symt;
1091     cv_basic_types[T_LONG]   = &symt_new_basic(module, btInt,   "long int", 4)->symt;
1092     cv_basic_types[T_QUAD]   = &symt_new_basic(module, btInt,   "long long int", 8)->symt;
1093     cv_basic_types[T_UCHAR]  = &symt_new_basic(module, btUInt,  "unsigned char", 1)->symt;
1094     cv_basic_types[T_USHORT] = &symt_new_basic(module, btUInt,  "unsigned short", 2)->symt;
1095     cv_basic_types[T_ULONG]  = &symt_new_basic(module, btUInt,  "unsigned long", 4)->symt;
1096     cv_basic_types[T_UQUAD]  = &symt_new_basic(module, btUInt,  "unsigned long long", 8)->symt;
1097     cv_basic_types[T_REAL32] = &symt_new_basic(module, btFloat, "float", 4)->symt;
1098     cv_basic_types[T_REAL64] = &symt_new_basic(module, btFloat, "double", 8)->symt;
1099     cv_basic_types[T_RCHAR]  = &symt_new_basic(module, btInt,   "signed char", 1)->symt;
1100     cv_basic_types[T_WCHAR]  = &symt_new_basic(module, btWChar, "wchar_t", 2)->symt;
1101     cv_basic_types[T_INT4]   = &symt_new_basic(module, btInt,   "INT4", 4)->symt;
1102     cv_basic_types[T_UINT4]  = &symt_new_basic(module, btUInt,  "UINT4", 4)->symt;
1103
1104     cv_basic_types[T_32PVOID]   = &symt_new_pointer(module, cv_basic_types[T_VOID])->symt;
1105     cv_basic_types[T_32PCHAR]   = &symt_new_pointer(module, cv_basic_types[T_CHAR])->symt;
1106     cv_basic_types[T_32PSHORT]  = &symt_new_pointer(module, cv_basic_types[T_SHORT])->symt;
1107     cv_basic_types[T_32PLONG]   = &symt_new_pointer(module, cv_basic_types[T_LONG])->symt;
1108     cv_basic_types[T_32PQUAD]   = &symt_new_pointer(module, cv_basic_types[T_QUAD])->symt;
1109     cv_basic_types[T_32PUCHAR]  = &symt_new_pointer(module, cv_basic_types[T_UCHAR])->symt;
1110     cv_basic_types[T_32PUSHORT] = &symt_new_pointer(module, cv_basic_types[T_USHORT])->symt;
1111     cv_basic_types[T_32PULONG]  = &symt_new_pointer(module, cv_basic_types[T_ULONG])->symt;
1112     cv_basic_types[T_32PUQUAD]  = &symt_new_pointer(module, cv_basic_types[T_UQUAD])->symt;
1113     cv_basic_types[T_32PREAL32] = &symt_new_pointer(module, cv_basic_types[T_REAL32])->symt;
1114     cv_basic_types[T_32PREAL64] = &symt_new_pointer(module, cv_basic_types[T_REAL64])->symt;
1115     cv_basic_types[T_32PRCHAR]  = &symt_new_pointer(module, cv_basic_types[T_RCHAR])->symt;
1116     cv_basic_types[T_32PWCHAR]  = &symt_new_pointer(module, cv_basic_types[T_WCHAR])->symt;
1117     cv_basic_types[T_32PINT4]   = &symt_new_pointer(module, cv_basic_types[T_INT4])->symt;
1118     cv_basic_types[T_32PUINT4]  = &symt_new_pointer(module, cv_basic_types[T_UINT4])->symt;
1119 }
1120
1121 static int numeric_leaf(int* value, const unsigned short int* leaf)
1122 {
1123     unsigned short int type = *leaf++;
1124     int length = 2;
1125
1126     if (type < LF_NUMERIC)
1127     {
1128         *value = type;
1129     }
1130     else
1131     {
1132         switch (type)
1133         {
1134         case LF_CHAR:
1135             length += 1;
1136             *value = *(const char*)leaf;
1137             break;
1138
1139         case LF_SHORT:
1140             length += 2;
1141             *value = *(const short*)leaf;
1142             break;
1143
1144         case LF_USHORT:
1145             length += 2;
1146             *value = *(const unsigned short*)leaf;
1147             break;
1148
1149         case LF_LONG:
1150             length += 4;
1151             *value = *(const int*)leaf;
1152             break;
1153
1154         case LF_ULONG:
1155             length += 4;
1156             *value = *(const unsigned int*)leaf;
1157             break;
1158
1159         case LF_QUADWORD:
1160         case LF_UQUADWORD:
1161             length += 8;
1162             *value = 0;    /* FIXME */
1163             break;
1164
1165         case LF_REAL32:
1166             length += 4;
1167             *value = 0;    /* FIXME */
1168             break;
1169
1170         case LF_REAL48:
1171             length += 6;
1172             *value = 0;    /* FIXME */
1173             break;
1174
1175         case LF_REAL64:
1176             length += 8;
1177             *value = 0;    /* FIXME */
1178             break;
1179
1180         case LF_REAL80:
1181             length += 10;
1182             *value = 0;    /* FIXME */
1183             break;
1184
1185         case LF_REAL128:
1186             length += 16;
1187             *value = 0;    /* FIXME */
1188             break;
1189
1190         case LF_COMPLEX32:
1191             length += 4;
1192             *value = 0;    /* FIXME */
1193             break;
1194
1195         case LF_COMPLEX64:
1196             length += 8;
1197             *value = 0;    /* FIXME */
1198             break;
1199
1200         case LF_COMPLEX80:
1201             length += 10;
1202             *value = 0;    /* FIXME */
1203             break;
1204
1205         case LF_COMPLEX128:
1206             length += 16;
1207             *value = 0;    /* FIXME */
1208             break;
1209
1210         case LF_VARSTRING:
1211             length += 2 + *leaf;
1212             *value = 0;    /* FIXME */
1213             break;
1214
1215         default:
1216             FIXME("Unknown numeric leaf type %04x\n", type);
1217             *value = 0;
1218             break;
1219         }
1220     }
1221
1222     return length;
1223 }
1224
1225 static const char* terminate_string(const unsigned char* name)
1226 {
1227     static char symname[256];
1228
1229     int namelen = name[0];
1230     assert(namelen >= 0 && namelen < 256);
1231
1232     memcpy(symname, name + 1, namelen);
1233     symname[namelen] = '\0';
1234
1235     return (!*symname || strcmp(symname, "__unnamed") == 0) ? NULL : symname;
1236 }
1237
1238 static struct symt*  codeview_get_type(unsigned int typeno, BOOL allow_special)
1239 {
1240     struct symt*        symt = NULL;
1241
1242     /*
1243      * Convert Codeview type numbers into something we can grok internally.
1244      * Numbers < 0x1000 are all fixed builtin types.  Numbers from 0x1000 and
1245      * up are all user defined (structs, etc).
1246      */
1247     if (typeno < 0x1000)
1248     {
1249         if (typeno < MAX_BUILTIN_TYPES)
1250             symt = cv_basic_types[typeno];
1251     }
1252     else
1253     {
1254         if (typeno - 0x1000 < num_cv_defined_types)
1255             symt = cv_defined_types[typeno - 0x1000];
1256     }
1257     if (!allow_special && symt && symt->tag == SymTagCVBitField)
1258         FIXME("bitfields are only handled for UDTs\n");
1259     return symt;
1260 }
1261
1262 static int codeview_add_type(unsigned int typeno, struct symt* dt)
1263 {
1264     while (typeno - 0x1000 >= num_cv_defined_types)
1265     {
1266         num_cv_defined_types += 0x100;
1267         if (cv_defined_types)
1268             cv_defined_types = (struct symt**)
1269                 HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cv_defined_types,
1270                             num_cv_defined_types * sizeof(struct symt*));
1271         else
1272             cv_defined_types = (struct symt**)
1273                 HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1274                           num_cv_defined_types * sizeof(struct symt*));
1275
1276         if (cv_defined_types == NULL) return FALSE;
1277     }
1278
1279     cv_defined_types[typeno - 0x1000] = dt;
1280     return TRUE;
1281 }
1282
1283 static void codeview_clear_type_table(void)
1284 {
1285     if (cv_defined_types) HeapFree(GetProcessHeap(), 0, cv_defined_types);
1286     cv_defined_types = NULL;
1287     num_cv_defined_types = 0;
1288
1289     if (cv_bitfields) HeapFree(GetProcessHeap(), 0, cv_bitfields);
1290     cv_bitfields = NULL;
1291     num_cv_bitfields = used_cv_bitfields = 0;
1292 }
1293
1294 static int codeview_add_type_pointer(struct module* module, unsigned int typeno, 
1295                                      unsigned int datatype)
1296 {
1297     struct symt* symt = &symt_new_pointer(module, codeview_get_type(datatype, FALSE))->symt;
1298     return codeview_add_type(typeno, symt);
1299 }
1300
1301 static int codeview_add_type_array(struct module* module, 
1302                                    unsigned int typeno, const char* name,
1303                                    unsigned int elemtype, unsigned int arr_len)
1304 {
1305     struct symt*        symt;
1306     struct symt*        elem = codeview_get_type(elemtype, FALSE);
1307     DWORD               arr_max = 0;
1308
1309     if (elem)
1310     {
1311         DWORD elem_size;
1312         symt_get_info(elem, TI_GET_LENGTH, &elem_size);
1313         if (elem_size) arr_max = arr_len / elem_size;
1314     }
1315     symt = &symt_new_array(module, 0, arr_max, elem)->symt;
1316     return codeview_add_type(typeno, symt);
1317 }
1318
1319 static int codeview_add_type_bitfield(unsigned int typeno, unsigned int bitoff,
1320                                       unsigned int nbits, unsigned int basetype)
1321 {
1322     if (used_cv_bitfields >= num_cv_bitfields)
1323     {
1324         num_cv_bitfields *= 2;
1325         if (cv_bitfields)
1326             cv_bitfields = HeapReAlloc(GetProcessHeap(), 0, cv_bitfields, 
1327                                        num_cv_bitfields * sizeof(struct codeview_bitfield));
1328         else
1329             cv_bitfields = HeapAlloc(GetProcessHeap(), 0, 
1330                                      num_cv_bitfields * sizeof(struct codeview_bitfield));
1331         if (!cv_bitfields) return 0;
1332     }
1333     
1334     cv_bitfields[used_cv_bitfields].symt.tag    = SymTagCVBitField;
1335     cv_bitfields[used_cv_bitfields].subtype     = basetype;
1336     cv_bitfields[used_cv_bitfields].bitposition = bitoff;
1337     cv_bitfields[used_cv_bitfields].bitlength   = nbits;
1338
1339     return codeview_add_type(typeno, &cv_bitfields[used_cv_bitfields++].symt);
1340 }
1341
1342 static int codeview_add_type_enum_field_list(struct module* module, 
1343                                              unsigned int typeno, 
1344                                              const unsigned char* list, int len)
1345 {
1346     struct symt_enum*           symt;
1347     const unsigned char*        ptr = list;
1348
1349     symt = symt_new_enum(module, NULL);
1350     while (ptr - list < len)
1351     {
1352         const union codeview_fieldtype* type = (const union codeview_fieldtype*)ptr;
1353
1354         if (*ptr >= 0xf0)       /* LF_PAD... */
1355         {
1356             ptr += *ptr & 0x0f;
1357             continue;
1358         }
1359
1360         switch (type->generic.id)
1361         {
1362         case LF_ENUMERATE:
1363         {
1364             int value, vlen = numeric_leaf(&value, &type->enumerate.value);
1365             const unsigned char* name = (const unsigned char*)&type->enumerate.value + vlen;
1366
1367             symt_add_enum_element(module, symt, terminate_string(name), value);
1368             ptr += 2 + 2 + vlen + (1 + name[0]);
1369             break;
1370         }
1371
1372         default:
1373             FIXME("Unhandled type %04x in ENUM field list\n", type->generic.id);
1374             return FALSE;
1375         }
1376     }
1377
1378     return codeview_add_type(typeno, &symt->symt);
1379 }
1380
1381 static int codeview_add_type_struct_field_list(struct module* module, 
1382                                                unsigned int typeno, 
1383                                                const unsigned char* list, int len)
1384 {
1385     struct symt_udt*            symt;
1386     const unsigned char*        ptr = list;
1387
1388     symt = symt_new_udt(module, NULL, 0, UdtStruct /* don't care */);
1389     while (ptr - list < len)
1390     {
1391         const union codeview_fieldtype* type = (const union codeview_fieldtype*)ptr;
1392
1393         if (*ptr >= 0xf0)       /* LF_PAD... */
1394         {
1395             ptr +=* ptr & 0x0f;
1396             continue;
1397         }
1398
1399         switch (type->generic.id)
1400         {
1401         case LF_BCLASS:
1402         {
1403             int offset, olen = numeric_leaf(&offset, &type->bclass.offset);
1404
1405             /* FIXME: ignored for now */
1406
1407             ptr += 2 + 2 + 2 + olen;
1408             break;
1409         }
1410
1411         case LF_BCLASS_32:
1412         {
1413             int offset, olen = numeric_leaf(&offset, &type->bclass32.offset);
1414
1415             /* FIXME: ignored for now */
1416
1417             ptr += 2 + 2 + 4 + olen;
1418             break;
1419         }
1420
1421         case LF_VBCLASS:
1422         case LF_IVBCLASS:
1423         {
1424             int vbpoff, vbplen = numeric_leaf(&vbpoff, &type->vbclass.vbpoff);
1425             const unsigned short int* p_vboff = (const unsigned short int*)((const char*)&type->vbclass.vbpoff + vbpoff);
1426             int vpoff, vplen = numeric_leaf(&vpoff, p_vboff);
1427
1428             /* FIXME: ignored for now */
1429
1430             ptr += 2 + 2 + 2 + 2 + vbplen + vplen;
1431             break;
1432         }
1433
1434         case LF_VBCLASS_32:
1435         case LF_IVBCLASS_32:
1436         {
1437             int vbpoff, vbplen = numeric_leaf(&vbpoff, &type->vbclass32.vbpoff);
1438             const unsigned short int* p_vboff = (const unsigned short int*)((const char*)&type->vbclass32.vbpoff + vbpoff);
1439             int vpoff, vplen = numeric_leaf(&vpoff, p_vboff);
1440
1441             /* FIXME: ignored for now */
1442
1443             ptr += 2 + 2 + 4 + 4 + vbplen + vplen;
1444             break;
1445         }
1446
1447         case LF_MEMBER:
1448         {
1449             int offset, olen = numeric_leaf(&offset, &type->member.offset);
1450             const unsigned char* name = (const unsigned char*)&type->member.offset + olen;
1451             struct symt* subtype = codeview_get_type(type->member.type, TRUE);
1452
1453             if (!subtype || subtype->tag != SymTagCVBitField)
1454             {
1455                 DWORD elem_size = 0;
1456                 if (subtype) symt_get_info(subtype, TI_GET_LENGTH, &elem_size);
1457                 symt_add_udt_element(module, symt, terminate_string(name),
1458                                      subtype, offset << 3, elem_size << 3);
1459             }
1460             else
1461             {
1462                 struct codeview_bitfield* cvbf = (struct codeview_bitfield*)subtype;
1463                 symt_add_udt_element(module, symt, terminate_string(name),
1464                                      codeview_get_type(cvbf->subtype, FALSE),
1465                                      cvbf->bitposition, cvbf->bitlength);
1466             }
1467
1468             ptr += 2 + 2 + 2 + olen + (1 + name[0]);
1469             break;
1470         }
1471
1472         case LF_MEMBER_32:
1473         {
1474             int offset, olen = numeric_leaf(&offset, &type->member32.offset);
1475             const unsigned char* name = (const unsigned char*)&type->member32.offset + olen;
1476             struct symt* subtype = codeview_get_type(type->member32.type, TRUE);
1477
1478             if (!subtype || subtype->tag != SymTagCVBitField)
1479             {
1480                 DWORD elem_size = 0;
1481                 if (subtype) symt_get_info(subtype, TI_GET_LENGTH, &elem_size);
1482                 symt_add_udt_element(module, symt, terminate_string(name),
1483                                      subtype, offset << 3, elem_size << 3);
1484             }
1485             else
1486             {
1487                 struct codeview_bitfield* cvbf = (struct codeview_bitfield*)subtype;
1488                 symt_add_udt_element(module, symt, terminate_string(name),
1489                                      codeview_get_type(cvbf->subtype, FALSE),
1490                                      cvbf->bitposition, cvbf->bitlength);
1491             }
1492
1493             ptr += 2 + 2 + 4 + olen + (1 + name[0]);
1494             break;
1495         }
1496
1497         case LF_STMEMBER:
1498             /* FIXME: ignored for now */
1499             ptr += 2 + 2 + 2 + (1 + type->stmember.name[0]);
1500             break;
1501
1502         case LF_STMEMBER_32:
1503             /* FIXME: ignored for now */
1504             ptr += 2 + 4 + 2 + (1 + type->stmember32.name[0]);
1505             break;
1506
1507         case LF_METHOD:
1508             /* FIXME: ignored for now */
1509             ptr += 2 + 2 + 2 + (1 + type->method.name[0]);
1510             break;
1511
1512         case LF_METHOD_32:
1513             /* FIXME: ignored for now */
1514             ptr += 2 + 2 + 4 + (1 + type->method32.name[0]);
1515             break;
1516
1517         case LF_NESTTYPE:
1518             /* FIXME: ignored for now */
1519             ptr += 2 + 2 + (1 + type->nesttype.name[0]);
1520             break;
1521
1522         case LF_NESTTYPE_32:
1523             /* FIXME: ignored for now */
1524             ptr += 2 + 2 + 4 + (1 + type->nesttype32.name[0]);
1525             break;
1526
1527         case LF_VFUNCTAB:
1528             /* FIXME: ignored for now */
1529             ptr += 2 + 2;
1530             break;
1531
1532         case LF_VFUNCTAB_32:
1533             /* FIXME: ignored for now */
1534             ptr += 2 + 2 + 4;
1535             break;
1536
1537         case LF_ONEMETHOD:
1538             /* FIXME: ignored for now */
1539             switch ((type->onemethod.attribute >> 2) & 7)
1540             {
1541             case 4: case 6: /* (pure) introducing virtual method */
1542                 ptr += 2 + 2 + 2 + 4 + (1 + type->onemethod_virt.name[0]);
1543                 break;
1544
1545             default:
1546                 ptr += 2 + 2 + 2 + (1 + type->onemethod.name[0]);
1547                 break;
1548             }
1549             break;
1550
1551         case LF_ONEMETHOD_32:
1552             /* FIXME: ignored for now */
1553             switch ((type->onemethod32.attribute >> 2) & 7)
1554             {
1555             case 4: case 6: /* (pure) introducing virtual method */
1556                 ptr += 2 + 2 + 4 + 4 + (1 + type->onemethod32_virt.name[0]);
1557                 break;
1558
1559             default:
1560                 ptr += 2 + 2 + 4 + (1 + type->onemethod32.name[0]);
1561                 break;
1562             }
1563             break;
1564
1565         default:
1566             FIXME("Unhandled type %04x in STRUCT field list\n", type->generic.id);
1567             return FALSE;
1568         }
1569     }
1570
1571     return codeview_add_type(typeno, &symt->symt);
1572 }
1573
1574 static int codeview_add_type_enum(struct module* module, unsigned int typeno, 
1575                                   const char* name, unsigned int fieldlist)
1576 {
1577     struct symt_enum*   symt = symt_new_enum(module, name);
1578     struct symt*        list = codeview_get_type(fieldlist, FALSE);
1579
1580     /* FIXME: this is rather ugly !!! */
1581     if (list) symt->vchildren = ((struct symt_enum*)list)->vchildren;
1582
1583     return codeview_add_type(typeno, &symt->symt);
1584 }
1585
1586 static int codeview_add_type_struct(struct module* module, unsigned int typeno, 
1587                                     const char* name, int structlen, 
1588                                     unsigned int fieldlist, enum UdtKind kind)
1589 {
1590     struct symt_udt*    symt = symt_new_udt(module, name, structlen, kind);
1591     struct symt*        list = codeview_get_type(fieldlist, FALSE);
1592
1593     /* FIXME: this is rather ugly !!! */
1594     if (list) symt->vchildren = ((struct symt_udt*)list)->vchildren;
1595
1596     return codeview_add_type(typeno, &symt->symt);
1597 }
1598
1599 static int codeview_parse_type_table(struct module* module, const char* table, int len)
1600 {
1601     unsigned int        curr_type = 0x1000;
1602     const char*         ptr = table;
1603
1604     while (ptr - table < len)
1605     {
1606         const union codeview_type* type = (const union codeview_type*)ptr;
1607         int retv = TRUE;
1608
1609         switch (type->generic.id)
1610         {
1611         case LF_POINTER:
1612             retv = codeview_add_type_pointer(module, curr_type, 
1613                                              type->pointer.datatype);
1614             break;
1615         case LF_POINTER_32:
1616             retv = codeview_add_type_pointer(module, curr_type, 
1617                                              type->pointer32.datatype);
1618             break;
1619
1620         case LF_ARRAY:
1621         {
1622             int arrlen, alen = numeric_leaf(&arrlen, &type->array.arrlen);
1623             const unsigned char* name = (const unsigned char*)&type->array.arrlen + alen;
1624
1625             retv = codeview_add_type_array(module, curr_type, terminate_string(name),
1626                                            type->array.elemtype, arrlen);
1627             break;
1628         }
1629         case LF_ARRAY_32:
1630         {
1631             int arrlen, alen = numeric_leaf(&arrlen, &type->array32.arrlen);
1632             const unsigned char* name = (const unsigned char*)&type->array32.arrlen + alen;
1633
1634             retv = codeview_add_type_array(module, curr_type, terminate_string(name),
1635                                            type->array32.elemtype, 
1636                                            type->array32.arrlen);
1637             break;
1638         }
1639
1640         /* a bitfields is a CodeView specific data type which represent a bitfield
1641          * in a structure or a class. For now, we store it in a SymTag-like type
1642          * (so that the rest of the process is seamless), but check at udt inclusion
1643          * type for its presence
1644          */
1645         case LF_BITFIELD:
1646             retv = codeview_add_type_bitfield(curr_type, type->bitfield.bitoff,
1647                                               type->bitfield.nbits,
1648                                               type->bitfield.type);
1649             break;
1650         case LF_BITFIELD_32:
1651             retv = codeview_add_type_bitfield(curr_type, type->bitfield32.bitoff,
1652                                               type->bitfield32.nbits,
1653                                               type->bitfield32.type);
1654             break;
1655
1656         case LF_FIELDLIST:
1657         case LF_FIELDLIST_32:
1658         {
1659             /*
1660              * A 'field list' is a CodeView-specific data type which doesn't
1661              * directly correspond to any high-level data type.  It is used
1662              * to hold the collection of members of a struct, class, union
1663              * or enum type.  The actual definition of that type will follow
1664              * later, and refer to the field list definition record.
1665              *
1666              * As we don't have a field list type ourselves, we look ahead
1667              * in the field list to try to find out whether this field list
1668              * will be used for an enum or struct type, and create a dummy
1669              * type of the corresponding sort.  Later on, the definition of
1670              * the 'real' type will copy the member / enumeration data.
1671              */
1672
1673             const char* list = type->fieldlist.list;
1674             int   len  = (ptr + type->generic.len + 2) - list;
1675
1676             if (((union codeview_fieldtype*)list)->generic.id == LF_ENUMERATE)
1677                 retv = codeview_add_type_enum_field_list(module, curr_type, list, len);
1678             else
1679                 retv = codeview_add_type_struct_field_list(module, curr_type, list, len);
1680             break;
1681         }
1682
1683         case LF_STRUCTURE:
1684         case LF_CLASS:
1685         {
1686             int structlen, slen = numeric_leaf(&structlen, &type->structure.structlen);
1687             const unsigned char* name = (const unsigned char*)&type->structure.structlen + slen;
1688
1689             retv = codeview_add_type_struct(module, curr_type, terminate_string(name),
1690                                             structlen, type->structure.fieldlist,
1691                                             type->generic.id == LF_CLASS ? UdtClass : UdtStruct);
1692             break;
1693         }
1694         case LF_STRUCTURE_32:
1695         case LF_CLASS_32:
1696         {
1697             int structlen, slen = numeric_leaf(&structlen, &type->structure32.structlen);
1698             const unsigned char* name = (const unsigned char*)&type->structure32.structlen + slen;
1699
1700             retv = codeview_add_type_struct(module, curr_type, terminate_string(name),
1701                                             structlen, type->structure32.fieldlist,
1702                                             type->generic.id == LF_CLASS ? UdtClass : UdtStruct);
1703             break;
1704         }
1705
1706         case LF_UNION:
1707         {
1708             int un_len, ulen = numeric_leaf(&un_len, &type->t_union.un_len);
1709             const unsigned char* name = (const unsigned char*)&type->t_union.un_len + ulen;
1710
1711             retv = codeview_add_type_struct(module, curr_type, terminate_string(name),
1712                                             un_len, type->t_union.fieldlist, UdtUnion);
1713             break;
1714         }
1715         case LF_UNION_32:
1716         {
1717             int un_len, ulen = numeric_leaf(&un_len, &type->t_union32.un_len);
1718             const unsigned char* name = (const unsigned char*)&type->t_union32.un_len + ulen;
1719
1720             retv = codeview_add_type_struct(module, curr_type, terminate_string(name),
1721                                             un_len, type->t_union32.fieldlist, UdtUnion);
1722             break;
1723         }
1724
1725         case LF_ENUM:
1726             retv = codeview_add_type_enum(module, curr_type, terminate_string(type->enumeration.name),
1727                                           type->enumeration.field);
1728             break;
1729         case LF_ENUM_32:
1730             retv = codeview_add_type_enum(module, curr_type, terminate_string(type->enumeration32.name),
1731                                           type->enumeration32.field);
1732             break;
1733
1734         case LF_PROCEDURE:
1735             FIXME("LF_PROCEDURE unhandled\n");
1736             break; 
1737
1738         case LF_PROCEDURE_32:
1739             FIXME("LF_PROCEDURE_32 unhandled\n");
1740             break; 
1741
1742         default:
1743             FIXME("Unhandled leaf %x\n", type->generic.id);
1744             break;
1745         }
1746
1747         if (!retv)
1748             return FALSE;
1749
1750         curr_type++;
1751         ptr += type->generic.len + 2;
1752     }
1753
1754     return TRUE;
1755 }
1756
1757 /*========================================================================
1758  * Process CodeView line number information.
1759  */
1760
1761 union any_size
1762 {
1763     const char*           c;
1764     const short*          s;
1765     const int*            i;
1766     const unsigned int*   ui;
1767 };
1768
1769 struct startend
1770 {
1771     unsigned int          start;
1772     unsigned int          end;
1773 };
1774
1775 struct codeview_linetab
1776 {
1777     unsigned int                nline;
1778     unsigned int                segno;
1779     unsigned int                start;
1780     unsigned int                end;
1781     struct symt_compiland*      compiland;
1782     const unsigned short*       linetab;
1783     const unsigned int*         offtab;
1784 };
1785
1786 static struct codeview_linetab* codeview_snarf_linetab(struct module* module, 
1787                                                        const char* linetab, int size)
1788 {
1789     int                         file_segcount;
1790     char                        filename[PATH_MAX];
1791     const unsigned int*         filetab;
1792     const char*                 fn;
1793     int                         i;
1794     int                         k;
1795     struct codeview_linetab*    lt_hdr;
1796     const unsigned int*         lt_ptr;
1797     int                         nfile;
1798     int                         nseg;
1799     union any_size              pnt;
1800     union any_size              pnt2;
1801     const struct startend*      start;
1802     int                         this_seg;
1803     struct symt_compiland*      compiland;
1804
1805     /*
1806      * Now get the important bits.
1807      */
1808     pnt.c = linetab;
1809     nfile = *pnt.s++;
1810     nseg = *pnt.s++;
1811
1812     filetab = (const unsigned int*) pnt.c;
1813
1814     /*
1815      * Now count up the number of segments in the file.
1816      */
1817     nseg = 0;
1818     for (i = 0; i < nfile; i++)
1819     {
1820         pnt2.c = linetab + filetab[i];
1821         nseg += *pnt2.s;
1822     }
1823
1824     /*
1825      * Next allocate the header we will be returning.
1826      * There is one header for each segment, so that we can reach in
1827      * and pull bits as required.
1828      */
1829     lt_hdr = (struct codeview_linetab*)
1830         HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (nseg + 1) * sizeof(*lt_hdr));
1831     if (lt_hdr == NULL)
1832     {
1833         goto leave;
1834     }
1835
1836     /*
1837      * Now fill the header we will be returning, one for each segment.
1838      * Note that this will basically just contain pointers into the existing
1839      * line table, and we do not actually copy any additional information
1840      * or allocate any additional memory.
1841      */
1842
1843     this_seg = 0;
1844     for (i = 0; i < nfile; i++)
1845     {
1846         /*
1847          * Get the pointer into the segment information.
1848          */
1849         pnt2.c = linetab + filetab[i];
1850         file_segcount = *pnt2.s;
1851
1852         pnt2.ui++;
1853         lt_ptr = (const unsigned int*) pnt2.c;
1854         start = (const struct startend*)(lt_ptr + file_segcount);
1855
1856         /*
1857          * Now snarf the filename for all of the segments for this file.
1858          */
1859         fn = (const unsigned char*)(start + file_segcount);
1860         memset(filename, 0, sizeof(filename));
1861         memcpy(filename, fn + 1, *fn);
1862         compiland = symt_new_compiland(module, filename);
1863         
1864         for (k = 0; k < file_segcount; k++, this_seg++)
1865         {
1866             pnt2.c = linetab + lt_ptr[k];
1867             lt_hdr[this_seg].start      = start[k].start;
1868             lt_hdr[this_seg].end        = start[k].end;
1869             lt_hdr[this_seg].compiland  = compiland;
1870             lt_hdr[this_seg].segno      = *pnt2.s++;
1871             lt_hdr[this_seg].nline      = *pnt2.s++;
1872             lt_hdr[this_seg].offtab     = pnt2.ui;
1873             lt_hdr[this_seg].linetab    = (const unsigned short*)(pnt2.ui + lt_hdr[this_seg].nline);
1874         }
1875     }
1876
1877 leave:
1878
1879   return lt_hdr;
1880
1881 }
1882
1883 /*========================================================================
1884  * Process CodeView symbol information.
1885  */
1886
1887 union codeview_symbol
1888 {
1889     struct
1890     {
1891         short int       len;
1892         short int       id;
1893     } generic;
1894
1895     struct
1896     {
1897         short int       len;
1898         short int       id;
1899         unsigned int    offset;
1900         unsigned short  seg;
1901         unsigned short  symtype;
1902         unsigned char   namelen;
1903         unsigned char   name[1];
1904     } data;
1905
1906     struct
1907     {
1908         short int       len;
1909         short int       id;
1910         unsigned int    symtype;
1911         unsigned int    offset;
1912         unsigned short  seg;
1913         unsigned char   namelen;
1914         unsigned char   name[1];
1915     } data32;
1916
1917     struct
1918     {
1919         short int       len;
1920         short int       id;
1921         unsigned int    pparent;
1922         unsigned int    pend;
1923         unsigned int    next;
1924         unsigned int    offset;
1925         unsigned short  segment;
1926         unsigned short  thunk_len;
1927         unsigned char   thtype;
1928         unsigned char   namelen;
1929         unsigned char   name[1];
1930     } thunk;
1931
1932     struct
1933     {
1934         short int       len;
1935         short int       id;
1936         unsigned int    pparent;
1937         unsigned int    pend;
1938         unsigned int    next;
1939         unsigned int    proc_len;
1940         unsigned int    debug_start;
1941         unsigned int    debug_end;
1942         unsigned int    offset;
1943         unsigned short  segment;
1944         unsigned short  proctype;
1945         unsigned char   flags;
1946         unsigned char   namelen;
1947         unsigned char   name[1];
1948     } proc;
1949
1950     struct
1951     {
1952         short int       len;
1953         short int       id;
1954         unsigned int    pparent;
1955         unsigned int    pend;
1956         unsigned int    next;
1957         unsigned int    proc_len;
1958         unsigned int    debug_start;
1959         unsigned int    debug_end;
1960         unsigned int    proctype;
1961         unsigned int    offset;
1962         unsigned short  segment;
1963         unsigned char   flags;
1964         unsigned char   namelen;
1965         unsigned char   name[1];
1966     } proc32;
1967
1968     struct
1969     {
1970         short int       len;            /* Total length of this entry */
1971         short int       id;             /* Always S_BPREL */
1972         unsigned int    offset;         /* Stack offset relative to BP */
1973         unsigned short  symtype;
1974         unsigned char   namelen;
1975         unsigned char   name[1];
1976     } stack;
1977
1978     struct
1979     {
1980         short int       len;            /* Total length of this entry */
1981         short int       id;             /* Always S_BPREL_32 */
1982         unsigned int    offset;         /* Stack offset relative to EBP */
1983         unsigned int    symtype;
1984         unsigned char   namelen;
1985         unsigned char   name[1];
1986     } stack32;
1987
1988     struct
1989     {
1990         short int       len;            /* Total length of this entry */
1991         short int       id;             /* Always S_REGISTER */
1992         unsigned short  type;
1993         unsigned short  reg;
1994         unsigned char   namelen;
1995         unsigned char   name[1];
1996         /* don't handle register tracking */
1997     } s_register;
1998
1999     struct
2000     {
2001         short int       len;            /* Total length of this entry */
2002         short int       id;             /* Always S_REGISTER_32 */
2003         unsigned int    type;           /* check whether type & reg are correct */
2004         unsigned int    reg;
2005         unsigned char   namelen;
2006         unsigned char   name[1];
2007         /* don't handle register tracking */
2008     } s_register32;
2009
2010     struct
2011     {
2012         short int       len;
2013         short int       id;
2014         unsigned int    parent;
2015         unsigned int    end;
2016         unsigned int    length;
2017         unsigned int    offset;
2018         unsigned short  segment;
2019         unsigned char   namelen;
2020         unsigned char   name[1];
2021     } block;
2022
2023     struct
2024     {
2025         short int       len;
2026         short int       id;
2027         unsigned int    offset;
2028         unsigned short  segment;
2029         unsigned char   flags;
2030         unsigned char   namelen;
2031         unsigned char   name[1];
2032     } label;
2033
2034     struct
2035     {
2036         short int       len;
2037         short int       id;
2038         unsigned short  type;
2039         unsigned short  arrlen;         /* numeric leaf */
2040 #if 0
2041         unsigned char   namelen;
2042         unsigned char   name[1];
2043 #endif
2044     } constant;
2045
2046     struct
2047     {
2048         short int       len;
2049         short int       id;
2050         unsigned        type;
2051         unsigned short  arrlen;         /* numeric leaf */
2052 #if 0
2053         unsigned char   namelen;
2054         unsigned char   name[1];
2055 #endif
2056     } constant32;
2057
2058     struct
2059     {
2060         short int       len;
2061         short int       id;
2062         unsigned short  type;
2063         unsigned char   namelen;
2064         unsigned char   name[1];
2065     } udt;
2066
2067     struct
2068     {
2069         short int       len;
2070         short int       id;
2071         unsigned        type;
2072         unsigned char   namelen;
2073         unsigned char   name[1];
2074     } udt32;
2075 };
2076
2077 #define S_COMPILE       0x0001
2078 #define S_REGISTER      0x0002
2079 #define S_CONSTANT      0x0003
2080 #define S_UDT           0x0004
2081 #define S_SSEARCH       0x0005
2082 #define S_END           0x0006
2083 #define S_SKIP          0x0007
2084 #define S_CVRESERVE     0x0008
2085 #define S_OBJNAME       0x0009
2086 #define S_ENDARG        0x000a
2087 #define S_COBOLUDT      0x000b
2088 #define S_MANYREG       0x000c
2089 #define S_RETURN        0x000d
2090 #define S_ENTRYTHIS     0x000e
2091
2092 #define S_BPREL         0x0200
2093 #define S_LDATA         0x0201
2094 #define S_GDATA         0x0202
2095 #define S_PUB           0x0203
2096 #define S_LPROC         0x0204
2097 #define S_GPROC         0x0205
2098 #define S_THUNK         0x0206
2099 #define S_BLOCK         0x0207
2100 #define S_WITH          0x0208
2101 #define S_LABEL         0x0209
2102 #define S_CEXMODEL      0x020a
2103 #define S_VFTPATH       0x020b
2104 #define S_REGREL        0x020c
2105 #define S_LTHREAD       0x020d
2106 #define S_GTHREAD       0x020e
2107
2108 #define S_PROCREF       0x0400
2109 #define S_DATAREF       0x0401
2110 #define S_ALIGN         0x0402
2111 #define S_LPROCREF      0x0403
2112
2113 #define S_REGISTER_32   0x1001 /* Variants with new 32-bit type indices */
2114 #define S_CONSTANT_32   0x1002
2115 #define S_UDT_32        0x1003
2116 #define S_COBOLUDT_32   0x1004
2117 #define S_MANYREG_32    0x1005
2118 #define S_BPREL_32      0x1006
2119 #define S_LDATA_32      0x1007
2120 #define S_GDATA_32      0x1008
2121 #define S_PUB_32        0x1009
2122 #define S_LPROC_32      0x100a
2123 #define S_GPROC_32      0x100b
2124 #define S_VFTTABLE_32   0x100c
2125 #define S_REGREL_32     0x100d
2126 #define S_LTHREAD_32    0x100e
2127 #define S_GTHREAD_32    0x100f
2128
2129 static unsigned int codeview_map_offset(const struct msc_debug_info* msc_dbg,
2130                                         unsigned int offset)
2131 {
2132     int                 nomap = msc_dbg->nomap;
2133     const OMAP_DATA*    omapp = msc_dbg->omapp;
2134     int                 i;
2135
2136     if (!nomap || !omapp) return offset;
2137
2138     /* FIXME: use binary search */
2139     for (i = 0; i < nomap - 1; i++)
2140         if (omapp[i].from <= offset && omapp[i+1].from > offset)
2141             return !omapp[i].to ? 0 : omapp[i].to + (offset - omapp[i].from);
2142
2143     return 0;
2144 }
2145
2146 static const struct codeview_linetab*
2147 codeview_get_linetab(const struct codeview_linetab* linetab,
2148                      unsigned seg, unsigned offset)
2149 {
2150     /*
2151      * Check whether we have line number information
2152      */
2153     if (linetab)
2154     {
2155         for (; linetab->linetab; linetab++)
2156             if (linetab->segno == seg &&
2157                 linetab->start <= offset && linetab->end   >  offset)
2158                 break;
2159         if (!linetab->linetab) linetab = NULL;
2160     }
2161     return linetab;
2162 }
2163
2164 static unsigned codeview_get_address(const struct msc_debug_info* msc_dbg, 
2165                                      unsigned seg, unsigned offset)
2166 {
2167     int                         nsect = msc_dbg->nsect;
2168     const IMAGE_SECTION_HEADER* sectp = msc_dbg->sectp;
2169
2170     if (!seg || seg > nsect) return 0;
2171     return msc_dbg->module->module.BaseOfImage +
2172         codeview_map_offset(msc_dbg, sectp[seg-1].VirtualAddress + offset);
2173 }
2174
2175 static void codeview_add_func_linenum(struct module* module, 
2176                                       struct symt_function* func,
2177                                       const struct codeview_linetab* linetab,
2178                                       unsigned offset, unsigned size)
2179 {
2180     unsigned int        i;
2181
2182     if (!linetab) return;
2183     for (i = 0; i < linetab->nline; i++)
2184     {
2185         if (linetab->offtab[i] >= offset && linetab->offtab[i] < offset + size)
2186         {
2187             symt_add_func_line(module, func, linetab->compiland->source,
2188                                linetab->linetab[i], linetab->offtab[i] - offset);
2189         }
2190     }
2191 }
2192
2193 static int codeview_snarf(const struct msc_debug_info* msc_dbg, const BYTE* root, 
2194                           int offset, int size,
2195                           struct codeview_linetab* linetab)
2196 {
2197     struct symt_function*               curr_func = NULL;
2198     int                                 i, length;
2199     char                                symname[PATH_MAX];
2200     const struct codeview_linetab*      flt;
2201     struct symt_block*                  block = NULL;
2202     struct symt*                        symt;
2203
2204     /*
2205      * Loop over the different types of records and whenever we
2206      * find something we are interested in, record it and move on.
2207      */
2208     for (i = offset; i < size; i += length)
2209     {
2210         const union codeview_symbol* sym = (const union codeview_symbol*)(root + i);
2211         length = sym->generic.len + 2;
2212
2213         switch (sym->generic.id)
2214         {
2215         /*
2216          * Global and local data symbols.  We don't associate these
2217          * with any given source file.
2218          */
2219         case S_GDATA:
2220         case S_LDATA:
2221             memcpy(symname, sym->data.name, sym->data.namelen);
2222             symname[sym->data.namelen] = '\0';
2223             flt = codeview_get_linetab(linetab, sym->data.seg, sym->data.offset);
2224             symt_new_global_variable(msc_dbg->module, 
2225                                      flt ? flt->compiland : NULL,
2226                                      symname, sym->generic.id == S_LDATA,
2227                                      codeview_get_address(msc_dbg, sym->data.seg, sym->data.offset),
2228                                      0,
2229                                      codeview_get_type(sym->data.symtype, FALSE));
2230             break;
2231
2232         case S_GDATA_32:
2233         case S_LDATA_32:
2234             memcpy(symname, sym->data32.name, sym->data32.namelen);
2235             symname[sym->data32.namelen] = '\0';
2236             flt = codeview_get_linetab(linetab, sym->data32.seg, sym->data32.offset);
2237             symt_new_global_variable(msc_dbg->module, flt ? flt->compiland : NULL,
2238                                      symname, sym->generic.id == S_LDATA_32,
2239                                      codeview_get_address(msc_dbg, sym->data32.seg, sym->data32.offset),
2240                                      0,
2241                                      codeview_get_type(sym->data32.symtype, FALSE));
2242             break;
2243
2244         case S_PUB: /* FIXME is this really a 'data' structure ?? */
2245             memcpy(symname, sym->data.name, sym->data.namelen);
2246             symname[sym->data.namelen] = '\0';
2247             flt = codeview_get_linetab(linetab, sym->data.seg, sym->data.offset);
2248             symt_new_public(msc_dbg->module, flt ? flt->compiland : NULL,
2249                             symname, 
2250                             codeview_get_address(msc_dbg, sym->data.seg, sym->data.offset),
2251                             0, TRUE /* FIXME */, TRUE /* FIXME */);
2252             break;
2253
2254         case S_PUB_32: /* FIXME is this really a 'data32' structure ?? */
2255             memcpy(symname, sym->data32.name, sym->data32.namelen);
2256             symname[sym->data32.namelen] = '\0';
2257             flt = codeview_get_linetab(linetab, sym->data32.seg, sym->data32.offset);
2258             symt_new_public(msc_dbg->module, flt ? flt->compiland : NULL,
2259                             symname, 
2260                             codeview_get_address(msc_dbg, sym->data32.seg, sym->data32.offset),
2261                             0, TRUE /* FIXME */, TRUE /* FIXME */);
2262             break;
2263
2264         /*
2265          * Sort of like a global function, but it just points
2266          * to a thunk, which is a stupid name for what amounts to
2267          * a PLT slot in the normal jargon that everyone else uses.
2268          */
2269         case S_THUNK:
2270             memcpy(symname, sym->thunk.name, sym->thunk.namelen);
2271             symname[sym->thunk.namelen] = '\0';
2272             flt = codeview_get_linetab(linetab, sym->thunk.segment, sym->thunk.offset);
2273             symt_new_thunk(msc_dbg->module, flt ? flt->compiland : NULL,
2274                            symname, sym->thunk.thtype,
2275                            codeview_get_address(msc_dbg, sym->thunk.segment, sym->thunk.offset),
2276                            sym->thunk.thunk_len);
2277             break;
2278
2279         /*
2280          * Global and static functions.
2281          */
2282         case S_GPROC:
2283         case S_LPROC:
2284             memcpy(symname, sym->proc.name, sym->proc.namelen);
2285             symname[sym->proc.namelen] = '\0';
2286             flt = codeview_get_linetab(linetab, sym->proc.segment, sym->proc.offset);
2287             curr_func = symt_new_function(msc_dbg->module, 
2288                                           flt ? flt->compiland : NULL, symname, 
2289                                           codeview_get_address(msc_dbg, sym->proc.segment, sym->proc.offset),
2290                                           sym->proc.proc_len,
2291                                           codeview_get_type(sym->proc.proctype, FALSE));
2292             codeview_add_func_linenum(msc_dbg->module, curr_func, flt, 
2293                                       sym->proc.offset, sym->proc.proc_len);
2294             symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, sym->proc.debug_start, NULL);
2295             symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, sym->proc.debug_end, NULL);
2296             break;
2297         case S_GPROC_32:
2298         case S_LPROC_32:
2299             memcpy(symname, sym->proc32.name, sym->proc32.namelen);
2300             symname[sym->proc32.namelen] = '\0';
2301             flt = codeview_get_linetab(linetab, sym->proc32.segment, sym->proc32.offset);
2302             curr_func = symt_new_function(msc_dbg->module,
2303                                           flt ? flt->compiland : NULL, symname, 
2304                                           codeview_get_address(msc_dbg, sym->proc32.segment, sym->proc32.offset),
2305                                           sym->proc32.proc_len,
2306                                           codeview_get_type(sym->proc32.proctype, FALSE));
2307             codeview_add_func_linenum(msc_dbg->module, curr_func, flt, 
2308                                       sym->proc32.offset, sym->proc32.proc_len);
2309             symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugStart, sym->proc32.debug_start, NULL);
2310             symt_add_function_point(msc_dbg->module, curr_func, SymTagFuncDebugEnd, sym->proc32.debug_end, NULL);
2311             break;
2312
2313         /*
2314          * Function parameters and stack variables.
2315          */
2316         case S_BPREL:
2317             memcpy(symname, sym->stack.name, sym->stack.namelen);
2318             symname[sym->stack.namelen] = '\0';
2319             symt_add_func_local(msc_dbg->module, curr_func, 0, sym->stack.offset,
2320                                 block, codeview_get_type(sym->stack.symtype, FALSE),
2321                                 symname);
2322             break;
2323         case S_BPREL_32:
2324             memcpy(symname, sym->stack32.name, sym->stack32.namelen);
2325             symname[sym->stack32.namelen] = '\0';
2326             symt_add_func_local(msc_dbg->module, curr_func, 0, sym->stack32.offset,
2327                                 block, codeview_get_type(sym->stack32.symtype, FALSE),
2328                                 symname);
2329             break;
2330
2331         case S_REGISTER:
2332             memcpy(symname, sym->s_register.name, sym->s_register.namelen);
2333             symname[sym->s_register.namelen] = '\0';
2334             symt_add_func_local(msc_dbg->module, curr_func, 0, sym->s_register.reg,
2335                                 block, codeview_get_type(sym->s_register.type, FALSE),
2336                                 symname);
2337             break;
2338
2339         case S_REGISTER_32:
2340             memcpy(symname, sym->s_register32.name, sym->s_register32.namelen);
2341             symname[sym->s_register32.namelen] = '\0';
2342             symt_add_func_local(msc_dbg->module, curr_func, 0, sym->s_register32.reg,
2343                                 block, codeview_get_type(sym->s_register32.type, FALSE),
2344                                 symname);
2345             break;
2346
2347         case S_BLOCK:
2348             block = symt_open_func_block(msc_dbg->module, curr_func, block, 
2349                                          codeview_get_address(msc_dbg, sym->block.segment, sym->block.offset),
2350                                          sym->block.length);
2351             break;
2352
2353         case S_END:
2354             if (block)
2355             {
2356                 block = symt_close_func_block(msc_dbg->module, curr_func, block, 0);
2357             }
2358             else if (curr_func)
2359             {
2360                 symt_normalize_function(msc_dbg->module, curr_func);
2361                 curr_func = NULL;
2362             }
2363             break;
2364
2365         case S_COMPILE:
2366             TRACE("S-Compile %x %.*s\n", ((const BYTE*)sym)[4], ((const BYTE*)sym)[8], (const BYTE*)sym + 9);
2367             break;
2368
2369         case S_OBJNAME:
2370             TRACE("S-ObjName %.*s\n", ((const BYTE*)sym)[8], (const BYTE*)sym + 9);
2371             break;
2372
2373         case S_LABEL:
2374             memcpy(symname, sym->label.name, sym->label.namelen);
2375             symname[sym->label.namelen] = '\0';
2376             if (curr_func)
2377             {
2378                 symt_add_function_point(msc_dbg->module, curr_func, SymTagLabel, 
2379                                         codeview_get_address(msc_dbg, sym->label.segment, sym->label.offset) - curr_func->address,
2380                                         symname);
2381             }
2382             else FIXME("No current function for label %s\n", symname);
2383             break;
2384
2385 #if 0
2386         case S_CONSTANT_32:
2387             {
2388                 int             val, vlen;
2389                 char*           ptr;
2390                 const char*     x;
2391                 struct symt*    se;
2392
2393                 vlen = numeric_leaf(&val, &sym->constant32.arrlen);
2394                 ptr = (char*)&sym->constant32.arrlen + vlen;
2395                 se = codeview_get_type(sym->constant32.type, FALSE);
2396                 if (!se) x = "---";
2397                 else if (se->tag == SymTagEnum) x = ((struct symt_enum*)se)->name;
2398                 else x = "###";
2399                     
2400                 FIXME("S-Constant %u %.*s %x (%s)\n", 
2401                       val, ptr[0], ptr + 1, sym->constant32.type, x);
2402             }
2403             break;
2404 #endif
2405
2406         case S_UDT:
2407             symt = codeview_get_type(sym->udt.type, FALSE);
2408             if (symt)
2409             {
2410                 memcpy(symname, sym->udt.name, sym->udt.namelen);
2411                 symname[sym->udt.namelen] = '\0';
2412                 symt_new_typedef(msc_dbg->module, symt, symname);
2413             }
2414             else FIXME("S-Udt %.*s: couldn't find type 0x%x\n", 
2415                        sym->udt.namelen, sym->udt.name, sym->udt.type);
2416             break;
2417
2418         case S_UDT_32:
2419             symt = codeview_get_type(sym->udt32.type, FALSE);
2420             if (symt)
2421             {
2422                 memcpy(symname, sym->udt32.name, sym->udt32.namelen);
2423                 symname[sym->udt32.namelen] = '\0';
2424                 symt_new_typedef(msc_dbg->module, symt, symname);
2425             }
2426             else FIXME("S-Udt %.*s: couldn't find type 0x%x\n", 
2427                        sym->udt32.namelen, sym->udt32.name, sym->udt32.type);
2428             break;
2429
2430         /*
2431          * These are special, in that they are always followed by an
2432          * additional length-prefixed string which is *not* included
2433          * into the symbol length count.  We need to skip it.
2434          */
2435         case S_PROCREF:
2436         case S_DATAREF:
2437         case S_LPROCREF:
2438             {
2439                 const BYTE* name = (const BYTE*)sym + length;
2440                 length += (*name + 1 + 3) & ~3;
2441             }
2442             break;
2443         default:
2444             FIXME("Unsupported id %x\n", sym->generic.id);
2445         }
2446     }
2447
2448     if (curr_func) symt_normalize_function(msc_dbg->module, curr_func);
2449
2450     if (linetab) HeapFree(GetProcessHeap(), 0, linetab);
2451     return TRUE;
2452 }
2453
2454 /*========================================================================
2455  * Process PDB file.
2456  */
2457
2458 #pragma pack(1)
2459 typedef struct _PDB_FILE
2460 {
2461     DWORD size;
2462     DWORD unknown;
2463 } PDB_FILE,* PPDB_FILE;
2464
2465 typedef struct _PDB_HEADER
2466 {
2467     CHAR        ident[40];
2468     DWORD       signature;
2469     DWORD       blocksize;
2470     WORD        freelist;
2471     WORD        total_alloc;
2472     PDB_FILE    toc;
2473     WORD        toc_block[1];
2474 } PDB_HEADER, *PPDB_HEADER;
2475
2476 typedef struct _PDB_TOC
2477 {
2478     DWORD       nFiles;
2479     PDB_FILE    file[ 1 ];
2480 } PDB_TOC, *PPDB_TOC;
2481
2482 typedef struct _PDB_ROOT
2483 {
2484     DWORD       version;
2485     DWORD       TimeDateStamp;
2486     DWORD       unknown;
2487     DWORD       cbNames;
2488     CHAR        names[1];
2489 } PDB_ROOT, *PPDB_ROOT;
2490
2491 typedef struct _PDB_TYPES_OLD
2492 {
2493     DWORD       version;
2494     WORD        first_index;
2495     WORD        last_index;
2496     DWORD       type_size;
2497     WORD        file;
2498     WORD        pad;
2499 } PDB_TYPES_OLD, *PPDB_TYPES_OLD;
2500
2501 typedef struct _PDB_TYPES
2502 {
2503     DWORD       version;
2504     DWORD       type_offset;
2505     DWORD       first_index;
2506     DWORD       last_index;
2507     DWORD       type_size;
2508     WORD        file;
2509     WORD        pad;
2510     DWORD       hash_size;
2511     DWORD       hash_base;
2512     DWORD       hash_offset;
2513     DWORD       hash_len;
2514     DWORD       search_offset;
2515     DWORD       search_len;
2516     DWORD       unknown_offset;
2517     DWORD       unknown_len;
2518 } PDB_TYPES, *PPDB_TYPES;
2519
2520 typedef struct _PDB_SYMBOL_RANGE
2521 {
2522     WORD        segment;
2523     WORD        pad1;
2524     DWORD       offset;
2525     DWORD       size;
2526     DWORD       characteristics;
2527     WORD        index;
2528     WORD        pad2;
2529 } PDB_SYMBOL_RANGE, *PPDB_SYMBOL_RANGE;
2530
2531 typedef struct _PDB_SYMBOL_RANGE_EX
2532 {
2533     WORD        segment;
2534     WORD        pad1;
2535     DWORD       offset;
2536     DWORD       size;
2537     DWORD       characteristics;
2538     WORD        index;
2539     WORD        pad2;
2540     DWORD       timestamp;
2541     DWORD       unknown;
2542 } PDB_SYMBOL_RANGE_EX, *PPDB_SYMBOL_RANGE_EX;
2543
2544 typedef struct _PDB_SYMBOL_FILE
2545 {
2546     DWORD       unknown1;
2547     PDB_SYMBOL_RANGE range;
2548     WORD        flag;
2549     WORD        file;
2550     DWORD       symbol_size;
2551     DWORD       lineno_size;
2552     DWORD       unknown2;
2553     DWORD       nSrcFiles;
2554     DWORD       attribute;
2555     CHAR        filename[1];
2556 } PDB_SYMBOL_FILE, *PPDB_SYMBOL_FILE;
2557
2558 typedef struct _PDB_SYMBOL_FILE_EX
2559 {
2560     DWORD       unknown1;
2561     PDB_SYMBOL_RANGE_EX range;
2562     WORD        flag;
2563     WORD        file;
2564     DWORD       symbol_size;
2565     DWORD       lineno_size;
2566     DWORD       unknown2;
2567     DWORD       nSrcFiles;
2568     DWORD       attribute;
2569     DWORD       reserved[2];
2570     CHAR        filename[1];
2571 } PDB_SYMBOL_FILE_EX, *PPDB_SYMBOL_FILE_EX;
2572
2573 typedef struct _PDB_SYMBOL_SOURCE
2574 {
2575     WORD        nModules;
2576     WORD        nSrcFiles;
2577     WORD        table[1];
2578 } PDB_SYMBOL_SOURCE, *PPDB_SYMBOL_SOURCE;
2579
2580 typedef struct _PDB_SYMBOL_IMPORT
2581 {
2582     DWORD       unknown1;
2583     DWORD       unknown2;
2584     DWORD       TimeDateStamp;
2585     DWORD       nRequests;
2586     CHAR        filename[1];
2587 } PDB_SYMBOL_IMPORT, *PPDB_SYMBOL_IMPORT;
2588
2589 typedef struct _PDB_SYMBOLS_OLD
2590 {
2591     WORD        hash1_file;
2592     WORD        hash2_file;
2593     WORD        gsym_file;
2594     WORD        pad;
2595     DWORD       module_size;
2596     DWORD       offset_size;
2597     DWORD       hash_size;
2598     DWORD       srcmodule_size;
2599 } PDB_SYMBOLS_OLD, *PPDB_SYMBOLS_OLD;
2600
2601 typedef struct _PDB_SYMBOLS
2602 {
2603     DWORD       signature;
2604     DWORD       version;
2605     DWORD       unknown;
2606     DWORD       hash1_file;
2607     DWORD       hash2_file;
2608     DWORD       gsym_file;
2609     DWORD       module_size;
2610     DWORD       offset_size;
2611     DWORD       hash_size;
2612     DWORD       srcmodule_size;
2613     DWORD       pdbimport_size;
2614     DWORD       resvd[5];
2615 } PDB_SYMBOLS, *PPDB_SYMBOLS;
2616 #pragma pack()
2617
2618 static void* pdb_read(const BYTE* image, const WORD* block_list, int size)
2619 {
2620     const PDB_HEADER*   pdb = (const PDB_HEADER*)image;
2621     int                 i, nBlocks;
2622     BYTE*               buffer;
2623
2624     if (!size) return NULL;
2625
2626     nBlocks = (size + pdb->blocksize - 1) / pdb->blocksize;
2627     buffer = HeapAlloc(GetProcessHeap(), 0, nBlocks * pdb->blocksize);
2628
2629     for (i = 0; i < nBlocks; i++)
2630         memcpy(buffer + i * pdb->blocksize,
2631                image + block_list[i] * pdb->blocksize, pdb->blocksize);
2632
2633     return buffer;
2634 }
2635
2636 static void* pdb_read_file(const BYTE* image, const PDB_TOC* toc, DWORD fileNr)
2637 {
2638     const PDB_HEADER*   pdb = (const PDB_HEADER*)image;
2639     const WORD*         block_list;
2640     DWORD               i;
2641
2642     if (!toc || fileNr >= toc->nFiles) return NULL;
2643
2644     block_list = (const WORD*) &toc->file[toc->nFiles];
2645     for (i = 0; i < fileNr; i++)
2646         block_list += (toc->file[i].size + pdb->blocksize - 1) / pdb->blocksize;
2647
2648     return pdb_read(image, block_list, toc->file[fileNr].size);
2649 }
2650
2651 static void pdb_free(void* buffer)
2652 {
2653     HeapFree(GetProcessHeap(), 0, buffer);
2654 }
2655
2656 static void pdb_convert_types_header(PDB_TYPES* types, const BYTE* image)
2657 {
2658     memset(types, 0, sizeof(PDB_TYPES));
2659     if (!image) return;
2660
2661     if (*(const DWORD*)image < 19960000)   /* FIXME: correct version? */
2662     {
2663         /* Old version of the types record header */
2664         const PDB_TYPES_OLD*    old = (const PDB_TYPES_OLD*)image;
2665         types->version     = old->version;
2666         types->type_offset = sizeof(PDB_TYPES_OLD);
2667         types->type_size   = old->type_size;
2668         types->first_index = old->first_index;
2669         types->last_index  = old->last_index;
2670         types->file        = old->file;
2671     }
2672     else
2673     {
2674         /* New version of the types record header */
2675         *types = *(const PDB_TYPES*)image;
2676     }
2677 }
2678
2679 static void pdb_convert_symbols_header(PDB_SYMBOLS* symbols,
2680                                        int* header_size, const BYTE* image)
2681 {
2682     memset(symbols, 0, sizeof(PDB_SYMBOLS));
2683     if (!image) return;
2684
2685     if (*(const DWORD*)image != 0xffffffff)
2686     {
2687         /* Old version of the symbols record header */
2688         const PDB_SYMBOLS_OLD*  old = (const PDB_SYMBOLS_OLD*)image;
2689         symbols->version         = 0;
2690         symbols->module_size     = old->module_size;
2691         symbols->offset_size     = old->offset_size;
2692         symbols->hash_size       = old->hash_size;
2693         symbols->srcmodule_size  = old->srcmodule_size;
2694         symbols->pdbimport_size  = 0;
2695         symbols->hash1_file      = old->hash1_file;
2696         symbols->hash2_file      = old->hash2_file;
2697         symbols->gsym_file       = old->gsym_file;
2698
2699         *header_size = sizeof(PDB_SYMBOLS_OLD);
2700     }
2701     else
2702     {
2703         /* New version of the symbols record header */
2704         *symbols = *(const PDB_SYMBOLS*)image;
2705         *header_size = sizeof(PDB_SYMBOLS);
2706     }
2707 }
2708
2709 static BOOL CALLBACK pdb_match(char* file, void* user)
2710 {
2711     /* accept first file */
2712     return FALSE;
2713 }
2714
2715 static HANDLE open_pdb_file(const struct process* pcs, struct module* module,
2716                             const char* filename)
2717 {
2718     HANDLE      h;
2719     char        dbg_file_path[MAX_PATH];
2720
2721     h = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL, 
2722                     OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2723     /* FIXME: should give more bits on the file to look at */
2724     if (h == INVALID_HANDLE_VALUE &&
2725         SymFindFileInPath(pcs->handle, NULL, (char*)filename, NULL, 0, 0, 0,
2726                           dbg_file_path, pdb_match, NULL))
2727     {
2728         h = CreateFileA(dbg_file_path, GENERIC_READ, FILE_SHARE_READ, NULL, 
2729                         OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2730     }
2731     return (h == INVALID_HANDLE_VALUE) ? NULL : h;
2732 }
2733
2734 static BOOL pdb_process_file(const struct process* pcs, 
2735                              const struct msc_debug_info* msc_dbg,
2736                              const char* filename, DWORD timestamp)
2737 {
2738     BOOL        ret = FALSE;
2739     HANDLE      hFile, hMap = NULL;
2740     char*       image = NULL;
2741     PDB_HEADER* pdb = NULL;
2742     PDB_TOC*    toc = NULL;
2743     PDB_ROOT*   root = NULL;
2744     char*       types_image = NULL;
2745     char*       symbols_image = NULL;
2746     PDB_TYPES   types;
2747     PDB_SYMBOLS symbols;
2748     int         header_size = 0;
2749     char*       modimage;
2750     char*       file;
2751
2752     TRACE("Processing PDB file %s\n", filename);
2753
2754     /*
2755     *  Open and map() .PDB file
2756      */
2757     if ((hFile = open_pdb_file(pcs, msc_dbg->module, filename)) == NULL ||
2758         ((hMap = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL)) == NULL) ||
2759         ((image = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0)) == NULL))
2760     {
2761         ERR("-Unable to peruse .PDB file %s\n", filename);
2762         goto leave;
2763     }
2764
2765     /*
2766      * Read in TOC and well-known files
2767      */
2768     pdb = (PPDB_HEADER)image;
2769     toc = pdb_read(image, pdb->toc_block, pdb->toc.size);
2770     root = pdb_read_file(image, toc, 1);
2771     types_image = pdb_read_file(image, toc, 2);
2772     symbols_image = pdb_read_file(image, toc, 3);
2773
2774     pdb_convert_types_header(&types, types_image);
2775     pdb_convert_symbols_header(&symbols, &header_size, symbols_image);
2776
2777     if (!root)
2778     {
2779         ERR("-Unable to get root from .PDB file %s\n", filename);
2780         goto leave;
2781     }
2782
2783     /*
2784      * Check for unknown versions
2785      */
2786
2787     switch (root->version)
2788     {
2789         case 19950623:      /* VC 4.0 */
2790         case 19950814:
2791         case 19960307:      /* VC 5.0 */
2792         case 19970604:      /* VC 6.0 */
2793             break;
2794         default:
2795             ERR("-Unknown root block version %ld\n", root->version);
2796     }
2797
2798     switch (types.version)
2799     {
2800         case 19950410:      /* VC 4.0 */
2801         case 19951122:
2802         case 19961031:      /* VC 5.0 / 6.0 */
2803             break;
2804         default:
2805             ERR("-Unknown type info version %ld\n", types.version);
2806     }
2807
2808     switch (symbols.version)
2809     {
2810         case 0:            /* VC 4.0 */
2811         case 19960307:     /* VC 5.0 */
2812         case 19970606:     /* VC 6.0 */
2813             break;
2814         default:
2815             ERR("-Unknown symbol info version %ld\n", symbols.version);
2816     }
2817
2818
2819     /* Check .PDB time stamp */
2820     if (root->TimeDateStamp != timestamp)
2821     {
2822         ERR("-Wrong time stamp of .PDB file %s (0x%08lx, 0x%08lx)\n",
2823             filename, root->TimeDateStamp, timestamp);
2824     }
2825
2826     /* Read type table */
2827     codeview_parse_type_table(msc_dbg->module, types_image + types.type_offset, types.type_size);
2828
2829     /* Read type-server .PDB imports */
2830     if (symbols.pdbimport_size)
2831     {
2832         /* FIXME */
2833         ERR("-Type server .PDB imports ignored!\n");
2834     }
2835
2836     /* Read global symbol table */
2837     modimage = pdb_read_file(image, toc, symbols.gsym_file);
2838     if (modimage)
2839     {
2840         codeview_snarf(msc_dbg, modimage, 0, toc->file[symbols.gsym_file].size, NULL);
2841         pdb_free(modimage);
2842     }
2843
2844     /* Read per-module symbol / linenumber tables */
2845     file = symbols_image + header_size;
2846     while (file - symbols_image < header_size + symbols.module_size)
2847     {
2848         int file_nr, file_index, symbol_size, lineno_size;
2849         char* file_name;
2850
2851         if (symbols.version < 19970000)
2852         {
2853             PDB_SYMBOL_FILE *sym_file = (PDB_SYMBOL_FILE*) file;
2854             file_nr     = sym_file->file;
2855             file_name   = sym_file->filename;
2856             file_index  = sym_file->range.index;
2857             symbol_size = sym_file->symbol_size;
2858             lineno_size = sym_file->lineno_size;
2859         }
2860         else
2861         {
2862             PDB_SYMBOL_FILE_EX *sym_file = (PDB_SYMBOL_FILE_EX*) file;
2863             file_nr     = sym_file->file;
2864             file_name   = sym_file->filename;
2865             file_index  = sym_file->range.index;
2866             symbol_size = sym_file->symbol_size;
2867             lineno_size = sym_file->lineno_size;
2868         }
2869
2870         modimage = pdb_read_file(image, toc, file_nr);
2871         if (modimage)
2872         {
2873             struct codeview_linetab*    linetab = NULL;
2874
2875             if (lineno_size)
2876                 linetab = codeview_snarf_linetab(msc_dbg->module, modimage + symbol_size, lineno_size);
2877
2878             if (symbol_size)
2879                 codeview_snarf(msc_dbg, modimage, sizeof(DWORD), symbol_size, linetab);
2880
2881             pdb_free(modimage);
2882         }
2883
2884         file_name += strlen(file_name) + 1;
2885         file = (char*)((DWORD)(file_name + strlen(file_name) + 1 + 3) & ~3);
2886     }
2887
2888     msc_dbg->module->module.SymType = SymCv;
2889     ret = TRUE;
2890
2891  leave:
2892
2893     /* Cleanup */
2894     codeview_clear_type_table();
2895
2896     if (symbols_image) pdb_free(symbols_image);
2897     if (types_image) pdb_free(types_image);
2898     if (root) pdb_free(root);
2899     if (toc) pdb_free(toc);
2900
2901     if (image) UnmapViewOfFile(image);
2902     if (hMap) CloseHandle(hMap);
2903     if (hFile) CloseHandle(hFile);
2904
2905     return ret;
2906 }
2907
2908 /*========================================================================
2909  * Process CodeView debug information.
2910  */
2911
2912 #define CODEVIEW_NB09_SIG  ('N' | ('B' << 8) | ('0' << 16) | ('9' << 24))
2913 #define CODEVIEW_NB10_SIG  ('N' | ('B' << 8) | ('1' << 16) | ('0' << 24))
2914 #define CODEVIEW_NB11_SIG  ('N' | ('B' << 8) | ('1' << 16) | ('1' << 24))
2915
2916 typedef struct _CODEVIEW_HEADER
2917 {
2918     DWORD       dwSignature;
2919     DWORD       lfoDirectory;
2920 } CODEVIEW_HEADER,* PCODEVIEW_HEADER;
2921
2922 typedef struct _CODEVIEW_PDB_DATA
2923 {
2924     DWORD       timestamp;
2925     DWORD       unknown;
2926     CHAR        name[1];
2927 } CODEVIEW_PDB_DATA, *PCODEVIEW_PDB_DATA;
2928
2929 typedef struct _CV_DIRECTORY_HEADER
2930 {
2931     WORD        cbDirHeader;
2932     WORD        cbDirEntry;
2933     DWORD       cDir;
2934     DWORD       lfoNextDir;
2935     DWORD       flags;
2936 } CV_DIRECTORY_HEADER, *PCV_DIRECTORY_HEADER;
2937
2938 typedef struct _CV_DIRECTORY_ENTRY
2939 {
2940     WORD        subsection;
2941     WORD        iMod;
2942     DWORD       lfo;
2943     DWORD       cb;
2944 } CV_DIRECTORY_ENTRY, *PCV_DIRECTORY_ENTRY;
2945
2946 #define sstAlignSym             0x125
2947 #define sstSrcModule            0x127
2948
2949 static BOOL codeview_process_info(const struct process* pcs, 
2950                                   const struct msc_debug_info* msc_dbg)
2951 {
2952     const CODEVIEW_HEADER*      cv = (const CODEVIEW_HEADER*)msc_dbg->root;
2953     BOOL                        ret = FALSE;
2954
2955
2956     switch (cv->dwSignature)
2957     {
2958     case CODEVIEW_NB09_SIG:
2959     case CODEVIEW_NB11_SIG:
2960     {
2961         const CV_DIRECTORY_HEADER*      hdr = (const CV_DIRECTORY_HEADER*)(msc_dbg->root + cv->lfoDirectory);
2962         const CV_DIRECTORY_ENTRY*       ent;
2963         const CV_DIRECTORY_ENTRY*       prev;
2964         const CV_DIRECTORY_ENTRY*       next;
2965         unsigned int                    i;
2966
2967         codeview_init_basic_types(msc_dbg->module);
2968         ent = (const CV_DIRECTORY_ENTRY*)((const BYTE*)hdr + hdr->cbDirHeader);
2969         for (i = 0; i < hdr->cDir; i++, ent = next)
2970         {
2971             next = (i == hdr->cDir-1)? NULL :
2972                    (const CV_DIRECTORY_ENTRY*)((const BYTE*)ent + hdr->cbDirEntry);
2973             prev = (i == 0)? NULL :
2974                    (const CV_DIRECTORY_ENTRY*)((const BYTE*)ent - hdr->cbDirEntry);
2975
2976             if (ent->subsection == sstAlignSym)
2977             {
2978                 /*
2979                  * Check the next and previous entry.  If either is a
2980                  * sstSrcModule, it contains the line number info for
2981                  * this file.
2982                  *
2983                  * FIXME: This is not a general solution!
2984                  */
2985                 struct codeview_linetab*        linetab = NULL;
2986
2987                 if (next && next->iMod == ent->iMod && next->subsection == sstSrcModule)
2988                     linetab = codeview_snarf_linetab(msc_dbg->module, msc_dbg->root + next->lfo, next->cb);
2989
2990                 if (prev && prev->iMod == ent->iMod && prev->subsection == sstSrcModule)
2991                     linetab = codeview_snarf_linetab(msc_dbg->module, msc_dbg->root + prev->lfo, prev->cb);
2992
2993                 codeview_snarf(msc_dbg, msc_dbg->root + ent->lfo, sizeof(DWORD),
2994                                ent->cb, linetab);
2995             }
2996         }
2997
2998         msc_dbg->module->module.SymType = SymCv;
2999         ret = TRUE;
3000         break;
3001     }
3002
3003     case CODEVIEW_NB10_SIG:
3004     {
3005         const CODEVIEW_PDB_DATA* pdb = (const CODEVIEW_PDB_DATA*)(cv + 1);
3006
3007         codeview_init_basic_types(msc_dbg->module);
3008         ret = pdb_process_file(pcs, msc_dbg, pdb->name, pdb->timestamp);
3009         break;
3010     }
3011     default:
3012         ERR("Unknown CODEVIEW signature %08lX in module %s\n",
3013             cv->dwSignature, msc_dbg->module->module.ModuleName);
3014         break;
3015     }
3016
3017     return ret;
3018 }
3019
3020 /*========================================================================
3021  * Process debug directory.
3022  */
3023 BOOL pe_load_debug_directory(const struct process* pcs, struct module* module, 
3024                              const BYTE* mapping,
3025                              const IMAGE_SECTION_HEADER* sectp, DWORD nsect,
3026                              const IMAGE_DEBUG_DIRECTORY* dbg, int nDbg)
3027 {
3028     BOOL                        ret;
3029     int                         i;
3030     struct msc_debug_info       msc_dbg;
3031
3032     msc_dbg.module = module;
3033     msc_dbg.nsect  = nsect;
3034     msc_dbg.sectp  = sectp;
3035     msc_dbg.nomap  = 0;
3036     msc_dbg.omapp  = NULL;
3037
3038     __TRY
3039     {
3040         ret = FALSE;
3041
3042         /* First, watch out for OMAP data */
3043         for (i = 0; i < nDbg; i++)
3044         {
3045             if (dbg[i].Type == IMAGE_DEBUG_TYPE_OMAP_FROM_SRC)
3046             {
3047                 msc_dbg.nomap = dbg[i].SizeOfData / sizeof(OMAP_DATA);
3048                 msc_dbg.omapp = (const OMAP_DATA*)(mapping + dbg[i].PointerToRawData);
3049                 break;
3050             }
3051         }
3052   
3053         /* Now, try to parse CodeView debug info */
3054         for (i = 0; i < nDbg; i++)
3055         {
3056             if (dbg[i].Type == IMAGE_DEBUG_TYPE_CODEVIEW)
3057             {
3058                 msc_dbg.root = mapping + dbg[i].PointerToRawData;
3059                 if ((ret = codeview_process_info(pcs, &msc_dbg))) goto done;
3060             }
3061         }
3062     
3063         /* If not found, try to parse COFF debug info */
3064         for (i = 0; i < nDbg; i++)
3065         {
3066             if (dbg[i].Type == IMAGE_DEBUG_TYPE_COFF)
3067             {
3068                 msc_dbg.root = mapping + dbg[i].PointerToRawData;
3069                 if ((ret = coff_process_info(&msc_dbg))) goto done;
3070             }
3071         }
3072     done:
3073 #if 0
3074          /* FIXME: this should be supported... this is the debug information for
3075           * functions compiled without a frame pointer (FPO = frame pointer omission)
3076           * the associated data helps finding out the relevant information
3077           */
3078         for (i = 0; i < nDbg; i++)
3079             if (dbg[i].Type == IMAGE_DEBUG_TYPE_FPO)
3080                 DEBUG_Printf("This guy has FPO information\n");
3081
3082 #define FRAME_FPO   0
3083 #define FRAME_TRAP  1
3084 #define FRAME_TSS   2
3085
3086 typedef struct _FPO_DATA 
3087 {
3088         DWORD       ulOffStart;            /* offset 1st byte of function code */
3089         DWORD       cbProcSize;            /* # bytes in function */
3090         DWORD       cdwLocals;             /* # bytes in locals/4 */
3091         WORD        cdwParams;             /* # bytes in params/4 */
3092
3093         WORD        cbProlog : 8;          /* # bytes in prolog */
3094         WORD        cbRegs   : 3;          /* # regs saved */
3095         WORD        fHasSEH  : 1;          /* TRUE if SEH in func */
3096         WORD        fUseBP   : 1;          /* TRUE if EBP has been allocated */
3097         WORD        reserved : 1;          /* reserved for future use */
3098         WORD        cbFrame  : 2;          /* frame type */
3099 } FPO_DATA;
3100 #else
3101 ;
3102 #endif
3103     }
3104     __EXCEPT(page_fault)
3105     {
3106         ERR("Got a page fault while loading symbols\n");
3107         ret = FALSE;
3108     }
3109     __ENDTRY
3110     return ret;
3111 }