Release 971116
[wine] / debugger / 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  *
7  * Note - this handles reading debug information for 32 bit applications
8  * that run under Windows-NT for example.  I doubt that this would work well
9  * for 16 bit applications, but I don't think it really matters since the
10  * file format is different, and we should never get in here in such cases.
11  *
12  * TODO:
13  *      Get 16 bit CV stuff working.
14  *      Add symbol size to internal symbol table.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19
20 #include <sys/types.h>
21 #include <sys/mman.h>
22 #include <fcntl.h>
23 #include <sys/stat.h>
24 #include <limits.h>
25 #include <string.h>
26 #include <unistd.h>
27 #ifndef PATH_MAX
28 #define PATH_MAX _MAX_PATH
29 #endif
30 #include "win.h"
31 #include "pe_image.h"
32 #include "peexe.h"
33 #include "debugger.h"
34 #include "peexe.h"
35 #include "xmalloc.h"
36
37
38 /*
39  * This is an index we use to keep track of the debug information
40  * when we have multiple sources.  We use the same database to also
41  * allow us to do an 'info shared' type of deal, and we use the index
42  * to eliminate duplicates.
43  */
44 static int DEBUG_next_index = 0;
45
46 union any_size
47 {
48   char           * c;
49   short          * s;
50   int            * i;
51   unsigned int   * ui;
52 };
53
54 /*
55  * This is a convenience structure used to map portions of the
56  * line number table.
57  */
58 struct startend
59 {
60   unsigned int    start;
61   unsigned int    end;
62 };
63
64 /*
65  * This is how we reference the various record types.
66  */
67 union codeview_symbol
68 {
69   struct
70   {
71     short int   len;
72     short int   id;
73   } generic;
74
75   struct
76   {
77         short int       len;
78         short int       id;
79         unsigned int    offset;
80         unsigned short  seg;
81         unsigned short  symtype;
82         unsigned char   namelen;
83         unsigned char   name[1];
84   } data;
85
86   struct
87   {
88         short int       len;
89         short int       id;
90         unsigned int    pparent;
91         unsigned int    pend;
92         unsigned int    next;
93         unsigned int    offset;
94         unsigned short  segment;
95         unsigned short  thunk_len;
96         unsigned char   thtype;
97         unsigned char   namelen;
98         unsigned char   name[1];
99   } thunk;
100   struct
101   {
102         short int       len;
103         short int       id;
104         unsigned int    pparent;
105         unsigned int    pend;
106         unsigned int    next;
107         unsigned int    proc_len;
108         unsigned int    debug_start;
109         unsigned int    debug_end;
110         unsigned int    offset;
111         unsigned short  segment;
112         unsigned short  proctype;
113         unsigned char   flags;
114         unsigned char   namelen;
115         unsigned char   name[1];
116   } proc;
117   struct
118   {
119         short int       len;    /* Total length of this entry */
120         short int       id;             /* Always S_BPREL32 */
121         unsigned int    offset; /* Stack offset relative to BP */
122         unsigned short  symtype;
123         unsigned char   namelen;
124         unsigned char   name[1];
125   } stack;
126 };
127
128 union codeview_type
129 {
130   struct
131   {
132     short int   len;
133     short int   id;
134   } generic;
135
136   struct
137   {
138     short int           len;
139     short int           id;
140     short int           attribute;
141     short int           datatype;
142     unsigned char       variant[1];
143   } pointer;
144
145   struct
146   {
147     short int           len;
148     short int           id;
149     unsigned char       nbits;
150     unsigned char       bitoff;
151     unsigned short      type;
152   } bitfield;
153
154   struct
155   {
156     short int           len;
157     short int           id;
158     short int           elemtype;
159     short int           idxtype;
160     unsigned char       arrlen;
161     unsigned char       namelen;
162     unsigned char       name[1];
163   } array;
164
165   struct
166   {
167     short int           len;
168     short int           id;
169     short int           n_element;
170     short int           fieldlist;
171     short int           property;
172     short int           derived;
173     short int           vshape;
174     unsigned short      structlen;
175     unsigned char       namelen;
176     unsigned char       name[1];
177   } structure;
178
179   struct
180   {
181     short int           len;
182     short int           id;
183     short int           count;
184     short int           field;
185     short int           property;
186     unsigned short      un_len;
187     unsigned char       namelen;
188     unsigned char       name[1];
189   } t_union;
190
191   struct
192   {
193     short int           len;
194     short int           id;
195     short int           count;
196     short int           type;
197     short int           field;
198     short int           property;
199     unsigned char       namelen;
200     unsigned char       name[1];
201   } enumeration;
202
203   struct
204   {
205     short int           id;
206     short int           attribute;
207     unsigned short int  value;
208     unsigned char       namelen;
209     unsigned char       name[1];
210   } enumerate;
211
212   struct
213   {
214     short int           id;
215     short int           type;
216     short int           attribute;
217     unsigned short int  offset;
218     unsigned char       namelen;
219     unsigned char       name[1];
220   } member;
221
222   struct
223   {
224     short int           len;
225     short int           id;
226     short int           count;
227     short int           type;
228     short int           field;
229     short int           property;
230     unsigned char       namelen;
231     unsigned char       name[1];
232   } fieldlist;
233
234 };
235
236 #define S_BPREL32       0x200
237 #define S_LDATA32       0x201
238 #define S_GDATA32       0x202
239 #define S_PUB32         0x203
240 #define S_LPROC32       0x204
241 #define S_GPROC32       0x205
242 #define S_THUNK32       0x206
243 #define S_BLOCK32       0x207
244 #define S_WITH32        0x208
245 #define S_LABEL32       0x209
246
247 #define S_PROCREF       0x400
248 #define S_DATAREF       0x401
249 #define S_ALIGN         0x402
250 #define S_UNKNOWN       0x403
251
252 /*
253  * This covers the basic datatypes that VC++ seems to be using these days.
254  * 32 bit mode only.  There are additional numbers for the pointers in 16
255  * bit mode.  There are many other types listed in the documents, but these
256  * are apparently not used by the compiler, or represent pointer types
257  * that are not used.
258  */
259 #define T_NOTYPE        0x0000  /* Notype */
260 #define T_ABS           0x0001  /* Abs */
261 #define T_VOID          0x0003  /* Void */
262 #define T_CHAR          0x0010  /* signed char */
263 #define T_SHORT         0x0011  /* short */
264 #define T_LONG          0x0012  /* long */
265 #define T_QUAD          0x0013  /* long long */
266 #define T_UCHAR         0x0020  /* unsigned  char */
267 #define T_USHORT        0x0021  /* unsigned short */
268 #define T_ULONG         0x0022  /* unsigned long */
269 #define T_UQUAD         0x0023  /* unsigned long long */
270 #define T_REAL32        0x0040  /* float */
271 #define T_REAL64        0x0041  /* double */
272 #define T_RCHAR         0x0070  /* real char */
273 #define T_WCHAR         0x0071  /* wide char */
274 #define T_INT4          0x0074  /* int */
275 #define T_UINT4         0x0075  /* unsigned int */
276
277 #define T_32PVOID       0x0403  /* 32 bit near pointer to void */
278 #define T_32PCHAR       0x0410  /* 16:32 near pointer to signed char */
279 #define T_32PSHORT      0x0411  /* 16:32 near pointer to short */
280 #define T_32PLONG       0x0412  /* 16:32 near pointer to int */
281 #define T_32PQUAD       0x0413  /* 16:32 near pointer to long long */
282 #define T_32PUCHAR      0x0420  /* 16:32 near pointer to unsigned char */
283 #define T_32PUSHORT     0x0421  /* 16:32 near pointer to unsigned short */
284 #define T_32PULONG      0x0422  /* 16:32 near pointer to unsigned int */
285 #define T_32PUQUAD      0x0423  /* 16:32 near pointer to long long */
286 #define T_32PREAL32     0x0440  /* 16:32 near pointer to float */
287 #define T_32PREAL64     0x0441  /* 16:32 near pointer to float */
288 #define T_32PRCHAR      0x0470  /* 16:32 near pointer to real char */
289 #define T_32PWCHAR      0x0471  /* 16:32 near pointer to real char */
290 #define T_32PINT4       0x0474  /* 16:32 near pointer to int */
291 #define T_32PUINT4      0x0475  /* 16:32 near pointer to unsigned int */
292
293 #define LF_MODIFIER     0x1
294 #define LF_POINTER      0x2
295 #define LF_ARRAY        0x3
296 #define LF_CLASS        0x4
297 #define LF_STRUCTURE    0x5
298 #define LF_UNION        0x6
299 #define LF_ENUMERATION  0x7
300 #define LF_PROCEDURE    0x8
301 #define LF_MFUNCTION    0x9
302 #define LF_VTSHAPE      0xa
303 #define LF_BARRAY       0xd
304 #define LF_DIMARRAY     0x11
305 #define LF_VFTPATH      0x12
306
307 #define LF_SKIP         0x200
308 #define LF_ARGLIST      0x201
309 #define LF_FIELDLIST    0x204
310 #define LF_DERIVED      0x205
311 #define LF_BITFIELD     0x206
312
313 #define LF_BCLASS       0x400
314 #define LF_VBCLASS      0x401
315 #define LF_IVBCLASS     0x402
316 #define LF_ENUMERATE    0x403
317 #define LF_FRIENDFCN    0x404
318 #define LF_INDEX        0x405
319 #define LF_MEMBER       0x406
320 #define LF_STMEMBER     0x407
321 #define LF_METHOD       0x408
322 #define LF_NESTEDTYPE   0x409
323 #define LF_VFUNCTAB     0x40a
324 #define LF_FRIENDCLS    0x40b
325 #define LF_ONEMETHOD    0x40c
326 #define LF_FUNCOFF      0x40d
327
328 #define MAX_BUILTIN_TYPES       0x480
329 static struct datatype * cv_basic_types[MAX_BUILTIN_TYPES];
330 static int num_cv_defined_types = 0;
331 static struct datatype **cv_defined_types = NULL;
332
333 /*
334  * For the type CODEVIEW debug directory entries, the debug directory
335  * points to a structure like this.  The cv_name field is the name
336  * of an external .PDB file.
337  */
338 struct CodeViewDebug
339 {
340         char                cv_nbtype[8];
341         unsigned int        cv_timestamp;
342         char                cv_unknown[4];
343         char                cv_name[1];
344 };
345
346 struct MiscDebug {
347     unsigned int       DataType;
348     unsigned int       Length;
349     char               Unicode;
350     char               Reserved[3];
351     char               Data[1];
352 };
353
354 /*
355  * This is the header that the COFF variety of debug header points to.
356  */
357 struct CoffDebug {
358     unsigned int   N_Sym;
359     unsigned int   SymbolOffset;
360     unsigned int   N_Linenum;
361     unsigned int   LinenumberOffset;
362     unsigned int   Unused[4];
363 };
364
365 struct CoffLinenum {
366         unsigned int            VirtualAddr;
367         unsigned short int      Linenum;
368 };
369
370 struct CoffFiles {
371         unsigned int    startaddr;
372         unsigned int    endaddr;
373         char          * filename;
374         int             linetab_offset;
375         int             linecnt;
376         struct name_hash **entries;
377         int                    neps;
378         int              neps_alloc;
379 };
380
381
382 struct CoffSymbol {
383     union {
384         char    ShortName[8];
385         struct {
386             unsigned int   NotLong;
387             unsigned int   StrTaboff;
388         } Name;
389     } N;
390     unsigned int   Value;
391     short          SectionNumber;
392     short          Type;
393     char           StorageClass;
394     unsigned char  NumberOfAuxSymbols;
395 };
396
397 struct CoffAuxSection{
398   unsigned int   Length;
399   unsigned short NumberOfRelocations;
400   unsigned short NumberOfLinenumbers;
401   unsigned int   CheckSum;
402   short          Number;
403   char           Selection;
404 } Section;
405
406 /*
407  * These two structures are used in the directory within a .DBG file
408  * to locate the individual important bits that we might want to see.
409  */
410 struct CV4_DirHead {
411   short unsigned int       dhsize;
412   short unsigned int       desize;
413   unsigned int             ndir;
414   unsigned int             next_offset;
415   unsigned int             flags;
416 };
417
418 struct CV4_DirEnt {
419   short unsigned int       subsect_number;
420   short unsigned int       module_number;
421   unsigned int             offset;
422   unsigned int             size;
423 };
424
425 /*
426  * These are the values of interest that the subsect_number field takes.
427  */
428 #define sstAlignSym             0x125
429 #define sstSrcModule            0x127
430
431 struct codeview_linetab_hdr
432 {
433   unsigned int             nline;
434   unsigned int             segno;
435   unsigned int             start;
436   unsigned int             end;
437   char                   * sourcefile;
438   unsigned short         * linetab;
439   unsigned int           * offtab;
440 };
441
442 struct codeview_pdb_hdr
443 {
444   char          ident[44];
445   unsigned int  blocksize;         /* Extent size */
446   unsigned short loc_freelist;   /* freelist. */
447   unsigned short alloc_filesize; /* # extents allocated. */
448   unsigned int  toc_len;
449   unsigned int  unknown;
450   unsigned short toc_ext[1];       /* array of extent #'s for toc. */
451 };
452
453 /*
454  * This is our own structure that we use to keep track of the contents
455  * of a PDB file.
456  */
457 struct file_list
458 {
459         int             record_len;
460         int             nextents;
461         short int     * extent_list;
462         unsigned int    linetab_offset;
463         unsigned int    linetab_len;
464 };
465
466 /*
467  * These are the structures that represent how the file table is set up
468  * within the PDB file.
469  */
470 struct filetab_hdr
471 {
472   unsigned short        tab1_file;
473   unsigned short        tab2_file;
474   unsigned short        gsym_file;
475   unsigned short        padding;
476   unsigned int          ftab_len;
477   unsigned int          fofftab_len;
478   unsigned int          hash_len;
479   unsigned int          strtab_len;
480 };
481
482 struct file_ent
483 {
484   unsigned int          reserved1;
485   unsigned short        datasect_segment;
486   unsigned short        reserved2;
487   unsigned int          datasect_offset;
488   unsigned int          datasect_size;
489   unsigned int          datasect_flags;
490   unsigned short        reserved3;
491   unsigned short        index;
492   unsigned short        num6a;
493   unsigned short        file_number;
494   unsigned int          linetab_offset;
495   unsigned int          linetab_len;
496   unsigned int          num9;
497   unsigned int          num10;
498   unsigned int          num11;
499   unsigned char         filename[1];
500 };
501
502 /*
503  ********************************************************************
504  */
505 struct deferred_debug_info
506 {
507         struct deferred_debug_info      * next;
508         char                            * load_addr;
509         char                            * module_name;
510         char                            * dbg_info;
511         int                               dbg_size;
512         LPIMAGE_DEBUG_DIRECTORY           dbgdir;
513         struct pe_data                  * pe;
514         LPIMAGE_SECTION_HEADER            sectp;
515         int                               nsect;
516         short int                         dbg_index;                    
517         char                              loaded;
518 };
519
520 struct deferred_debug_info * dbglist = NULL;
521
522 /*
523  * A simple macro that tells us whether a given COFF symbol is a
524  * function or not.
525  */
526 #define N_TMASK                             0x0030
527 #define IMAGE_SYM_DTYPE_FUNCTION            2
528 #define N_BTSHFT                            4
529 #define ISFCN(x) (((x) & N_TMASK) == (IMAGE_SYM_DTYPE_FUNCTION << N_BTSHFT))
530
531
532 /*
533  * This is what we are looking for in the COFF symbols.
534  */
535 #define IMAGE_SYM_CLASS_EXTERNAL            0x2
536 #define IMAGE_SYM_CLASS_STATIC              0x3
537 #define IMAGE_SYM_CLASS_FILE                0x67
538
539 static 
540 struct datatype * DEBUG_GetCVType(int typeno)
541 {
542   struct datatype * dt = NULL;
543
544   /*
545    * Convert Codeview type numbers into something we can grok internally.
546    * Numbers < 0x1000 are all fixed builtin types.  Numbers from 0x1000 and
547    * up are all user defined (structs, etc).
548    */
549   if( typeno < 0x1000 )
550     {
551       if( typeno < MAX_BUILTIN_TYPES )
552         {
553           dt = cv_basic_types[typeno];
554         }
555     }
556   else
557     {
558       if( typeno - 0x1000 < num_cv_defined_types )
559         {
560           dt = cv_defined_types[typeno - 0x1000];
561         } 
562     }
563
564   return dt;
565 }
566
567 static int
568 DEBUG_ParseTypeTable(char * table, int len)
569 {
570   int                             arr_max;
571   int                             curr_type;
572   enum debug_type                 fieldtype;
573   int                             elem_size;
574   union any_size                  ptr;
575   union any_size                  ptr2;
576   struct datatype               * subtype;
577   char                            symname[256];
578   union codeview_type           * type;
579   union codeview_type           * type2;
580   struct datatype               * typeptr;
581
582   curr_type = 0x1000;
583
584   ptr = (union any_size) (table + 16);
585   while( ptr.c - table < len )
586     {
587       type = (union codeview_type *) ptr.c;
588
589       if( curr_type - 0x1000 >= num_cv_defined_types )
590         {
591           num_cv_defined_types += 0x100;
592           cv_defined_types = (struct datatype **) realloc(cv_defined_types,
593                   num_cv_defined_types * sizeof(struct datatype *));
594           memset(cv_defined_types + num_cv_defined_types - 0x100,
595                  0,
596                  0x100 * sizeof(struct datatype *));
597           if( cv_defined_types == NULL )
598             {
599               return FALSE;
600             }
601         }
602
603       switch(type->generic.id)
604         {
605         case LF_POINTER:
606           cv_defined_types[curr_type - 0x1000] = 
607             DEBUG_FindOrMakePointerType(DEBUG_GetCVType(type->pointer.datatype));
608           break;
609         case LF_ARRAY:
610           if( type->array.arrlen >= 0x8000 )
611             {
612               /*
613                * This is a numeric leaf, I am too lazy to handle this right
614                * now.
615                */
616               fprintf(stderr, "Ignoring large numberic leaf.\n");
617               break;
618             }
619           if( type->array.namelen != 0 )
620             {
621               memset(symname, 0, sizeof(symname));
622               memcpy(symname, type->array.name, type->array.namelen);
623               typeptr = DEBUG_NewDataType(ARRAY, symname);
624             }
625           else
626             {
627               typeptr = DEBUG_NewDataType(ARRAY, NULL);
628             }
629           cv_defined_types[curr_type - 0x1000] = typeptr;
630
631           subtype = DEBUG_GetCVType(type->array.elemtype);
632           if(    (subtype == NULL)
633               || (elem_size = DEBUG_GetObjectSize(subtype)) == 0 )
634             {
635               arr_max = 0;
636             }
637           else
638             {
639               arr_max = type->array.arrlen / DEBUG_GetObjectSize(subtype);
640             }
641
642           DEBUG_SetArrayParams(typeptr, 0, arr_max, subtype);
643           break;
644         case LF_FIELDLIST:
645           /*
646            * This is where the basic list of fields is defined for
647            * structures and classes.
648            *
649            * First, we need to look ahead and see whether we are building
650            * a fieldlist for an enum or a struct.
651            */
652           ptr2.i = ptr.i + 1;
653           type2 = (union codeview_type *) ptr2.c;
654           if( type2->member.id == LF_MEMBER )
655             {
656               typeptr = DEBUG_NewDataType(STRUCT, NULL);
657               fieldtype = STRUCT;
658             }
659           else if( type2->member.id == LF_ENUMERATE )
660             {
661               typeptr = DEBUG_NewDataType(ENUM, NULL);
662               fieldtype = ENUM;
663             }
664           else
665             {
666               break;
667             }
668
669           cv_defined_types[curr_type - 0x1000] = typeptr;
670           while( ptr2.c < (ptr.c + ((type->generic.len + 3) & ~3)) )
671             {
672               type2 = (union codeview_type *) ptr2.c;
673               if( type2->member.id == LF_MEMBER && fieldtype == STRUCT )
674                 {
675                   memset(symname, 0, sizeof(symname));
676                   memcpy(symname, type2->member.name, type2->member.namelen);
677                   
678                   subtype = DEBUG_GetCVType(type2->member.type);
679                   elem_size = 0;
680                   if( subtype != NULL )
681                     {
682                       elem_size = DEBUG_GetObjectSize(subtype);
683                     }
684                   
685                   if( type2->member.offset >= 0x8000 )
686                     {
687                       /*
688                        * This is a numeric leaf, I am too lazy to handle this right
689                        * now.
690                        */
691                       fprintf(stderr, "Ignoring large numberic leaf.\n");
692                     }
693                   else
694                     {
695                       DEBUG_AddStructElement(typeptr, symname, subtype, 
696                                              type2->member.offset << 3,
697                                              elem_size << 3);
698                     }
699                 }
700               else if( type2->member.id == LF_ENUMERATE && fieldtype == ENUM )
701                 {
702                   memset(symname, 0, sizeof(symname));
703                   memcpy(symname, type2->enumerate.name, type2->enumerate.namelen);
704                   
705                   if( type2->enumerate.value >= 0x8000 )
706                     {
707                       /*
708                        * This is a numeric leaf, I am too lazy to handle this right
709                        * now.
710                        */
711                       fprintf(stderr, "Ignoring large numberic leaf.\n");
712                     }
713                   else
714                     {
715                       DEBUG_AddStructElement(typeptr, symname, NULL, 
716                                              type2->enumerate.value, 0);
717                     }
718                 }
719               else
720                 {
721                   /*
722                    * Something else I have never seen before.  Either wrong type of
723                    * object in the fieldlist, or some other problem which I wouldn't
724                    * really know how to handle until it came up.
725                    */
726                   fprintf(stderr, "Unexpected entry in fieldlist\n");
727                   break;
728                 }
729
730
731               ptr2.c += ((type2->member.namelen + 9 + 3) & ~3);
732             }
733           break;
734         case LF_STRUCTURE:
735         case LF_CLASS:
736           if( type->structure.structlen >= 0x8000 )
737             {
738               /*
739                * This is a numeric leaf, I am too lazy to handle this right
740                * now.
741                */
742               fprintf(stderr, "Ignoring large numberic leaf.\n");
743               break;
744             }
745           memset(symname, 0, sizeof(symname));
746           memcpy(symname, type->structure.name, type->structure.namelen);
747           if( strcmp(symname, "__unnamed") == 0 )
748             {
749               typeptr = DEBUG_NewDataType(STRUCT, NULL);
750             }
751           else
752             {
753               typeptr = DEBUG_NewDataType(STRUCT, symname);
754             }
755           cv_defined_types[curr_type - 0x1000] = typeptr;
756
757           /*
758            * Now copy the relevant bits from the fieldlist that we specified.
759            */
760           subtype = DEBUG_GetCVType(type->structure.fieldlist);
761
762           if( subtype != NULL )
763             {
764               DEBUG_SetStructSize(typeptr, type->structure.structlen);
765               DEBUG_CopyFieldlist(typeptr, subtype);
766             }
767           break;
768         case LF_UNION:
769           if( type->t_union.un_len >= 0x8000 )
770             {
771               /*
772                * This is a numeric leaf, I am too lazy to handle this right
773                * now.
774                */
775               fprintf(stderr, "Ignoring large numberic leaf.\n");
776               break;
777             }
778           memset(symname, 0, sizeof(symname));
779           memcpy(symname, type->t_union.name, type->t_union.namelen);
780
781           if( strcmp(symname, "__unnamed") == 0 )
782             {
783               typeptr = DEBUG_NewDataType(STRUCT, NULL);
784             }
785           else
786             {
787               typeptr = DEBUG_NewDataType(STRUCT, symname);
788             }
789
790           cv_defined_types[curr_type - 0x1000] = typeptr;
791
792           /*
793            * Now copy the relevant bits from the fieldlist that we specified.
794            */
795           subtype = DEBUG_GetCVType(type->t_union.field);
796
797           if( subtype != NULL )
798             {
799               DEBUG_SetStructSize(typeptr, type->t_union.un_len);
800               DEBUG_CopyFieldlist(typeptr, subtype);
801             }
802           break;
803         case LF_BITFIELD:
804           typeptr = DEBUG_NewDataType(BITFIELD, NULL);
805           cv_defined_types[curr_type - 0x1000] = typeptr;
806           DEBUG_SetBitfieldParams(typeptr, type->bitfield.bitoff,
807                                   type->bitfield.nbits, 
808                                   DEBUG_GetCVType(type->bitfield.type));
809           break;
810         case LF_ENUMERATION:
811           memset(symname, 0, sizeof(symname));
812           memcpy(symname, type->enumeration.name, type->enumeration.namelen);
813           typeptr = DEBUG_NewDataType(ENUM, symname);
814           cv_defined_types[curr_type - 0x1000] = typeptr;
815
816           /*
817            * Now copy the relevant bits from the fieldlist that we specified.
818            */
819           subtype = DEBUG_GetCVType(type->enumeration.field);
820
821           if( subtype != NULL )
822             {
823               DEBUG_CopyFieldlist(typeptr, subtype);
824             }
825           break;
826         case LF_DIMARRAY:
827         default:
828           break;
829         }
830       curr_type++;
831       ptr.c += (type->generic.len + 3) & ~3;
832     }
833
834   return TRUE;
835 }
836
837 void
838 DEBUG_InitCVDataTypes()
839 {
840   /*
841    * These are the common builtin types that are used by VC++.
842    */
843   cv_basic_types[T_NOTYPE] = NULL;
844   cv_basic_types[T_ABS] = NULL;
845   cv_basic_types[T_VOID] = DEBUG_NewDataType(BASIC, "void");
846   cv_basic_types[T_CHAR] = DEBUG_NewDataType(BASIC, "char");
847   cv_basic_types[T_SHORT] = DEBUG_NewDataType(BASIC, "short int");
848   cv_basic_types[T_LONG] = DEBUG_NewDataType(BASIC, "long int");
849   cv_basic_types[T_QUAD] = DEBUG_NewDataType(BASIC, "long long int");
850   cv_basic_types[T_UCHAR] = DEBUG_NewDataType(BASIC, "unsigned char");
851   cv_basic_types[T_USHORT] = DEBUG_NewDataType(BASIC, "short unsigned int");
852   cv_basic_types[T_ULONG] = DEBUG_NewDataType(BASIC, "long unsigned int");
853   cv_basic_types[T_UQUAD] = DEBUG_NewDataType(BASIC, "long long unsigned int");
854   cv_basic_types[T_REAL32] = DEBUG_NewDataType(BASIC, "float");
855   cv_basic_types[T_REAL64] = DEBUG_NewDataType(BASIC, "double");
856   cv_basic_types[T_RCHAR] = DEBUG_NewDataType(BASIC, "char");
857   cv_basic_types[T_WCHAR] = DEBUG_NewDataType(BASIC, "short");
858   cv_basic_types[T_INT4] = DEBUG_NewDataType(BASIC, "int");
859   cv_basic_types[T_UINT4] = DEBUG_NewDataType(BASIC, "unsigned int");
860
861   cv_basic_types[T_32PVOID] = DEBUG_FindOrMakePointerType(cv_basic_types[T_VOID]);
862   cv_basic_types[T_32PCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_CHAR]);
863   cv_basic_types[T_32PSHORT] = DEBUG_FindOrMakePointerType(cv_basic_types[T_SHORT]);
864   cv_basic_types[T_32PLONG] = DEBUG_FindOrMakePointerType(cv_basic_types[T_LONG]);
865   cv_basic_types[T_32PQUAD] = DEBUG_FindOrMakePointerType(cv_basic_types[T_QUAD]);
866   cv_basic_types[T_32PUCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_UCHAR]);
867   cv_basic_types[T_32PUSHORT] = DEBUG_FindOrMakePointerType(cv_basic_types[T_USHORT]);
868   cv_basic_types[T_32PULONG] = DEBUG_FindOrMakePointerType(cv_basic_types[T_ULONG]);
869   cv_basic_types[T_32PUQUAD] = DEBUG_FindOrMakePointerType(cv_basic_types[T_UQUAD]);
870   cv_basic_types[T_32PREAL32] = DEBUG_FindOrMakePointerType(cv_basic_types[T_REAL32]);
871   cv_basic_types[T_32PREAL64] = DEBUG_FindOrMakePointerType(cv_basic_types[T_REAL64]);
872   cv_basic_types[T_32PRCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_RCHAR]);
873   cv_basic_types[T_32PWCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_WCHAR]);
874   cv_basic_types[T_32PINT4] = DEBUG_FindOrMakePointerType(cv_basic_types[T_INT4]);
875   cv_basic_types[T_32PUINT4] = DEBUG_FindOrMakePointerType(cv_basic_types[T_UINT4]);
876 }
877
878 /*
879  * In this function, we keep track of deferred debugging information
880  * that we may need later if we were to need to use the internal debugger.
881  * We don't fully process it here for performance reasons.
882  */
883 int
884 DEBUG_RegisterDebugInfo(struct pe_data * pe,int load_addr,
885                         const char *module_name, u_long v_addr, u_long size)
886 {
887   int                     has_codeview = FALSE;
888   int                     rtn = FALSE;
889   int                     orig_size;
890   LPIMAGE_DEBUG_DIRECTORY dbgptr;
891   struct deferred_debug_info * deefer;
892
893   orig_size = size;
894   dbgptr = (LPIMAGE_DEBUG_DIRECTORY) (load_addr + v_addr);
895   for(; size >= sizeof(*dbgptr); size -= sizeof(*dbgptr), dbgptr++ )
896     {
897       switch(dbgptr->Type)
898         {
899         case IMAGE_DEBUG_TYPE_CODEVIEW:
900         case IMAGE_DEBUG_TYPE_MISC:
901           has_codeview = TRUE;
902           break;
903         }
904     }
905
906   size = orig_size;
907   dbgptr = (LPIMAGE_DEBUG_DIRECTORY) (load_addr + v_addr);
908   for(; size >= sizeof(*dbgptr); size -= sizeof(*dbgptr), dbgptr++ )
909     {
910       switch(dbgptr->Type)
911         {
912         case IMAGE_DEBUG_TYPE_COFF:
913           /*
914            * If we have both codeview and COFF debug info, ignore the
915            * coff debug info as  it would just confuse us, and it is 
916            * less complete.
917            *
918            * FIXME - this is broken - if we cannot find the PDB file, then
919            * we end up with no debugging info at all.  In this case, we
920            * should use the COFF info as a backup.
921            */
922           if( has_codeview )
923             {
924               break;
925             }
926         case IMAGE_DEBUG_TYPE_CODEVIEW:
927         case IMAGE_DEBUG_TYPE_MISC:
928           /*
929            * This is usually an indirection to a .DBG file.
930            * This is similar to (but a slightly older format) from the
931            * PDB file.
932            *
933            * First check to see if the image was 'stripped'.  If so, it
934            * means that this entry points to a .DBG file.  Otherwise,
935            * it just points to itself, and we can ignore this.
936            */
937           if(    (dbgptr->Type == IMAGE_DEBUG_TYPE_MISC)
938               && (pe->pe_header->FileHeader.Characteristics & IMAGE_FILE_DEBUG_STRIPPED) == 0 )
939             {
940               break;
941             }
942
943           deefer = (struct deferred_debug_info *) xmalloc(sizeof(*deefer));
944           deefer->pe = pe;
945
946           deefer->dbg_info = NULL;
947           deefer->dbg_size = 0;
948
949           /*
950            * Read the important bits.  What we do after this depends
951            * upon the type, but this is always enough so we are able
952            * to proceed if we know what we need to do next.
953            */
954           deefer->dbg_size = dbgptr->SizeOfData;
955           deefer->dbg_info = (char *)(pe->mappeddll+dbgptr->PointerToRawData);
956           deefer->load_addr = (char *) load_addr;
957           deefer->dbgdir = dbgptr;
958           deefer->next = dbglist;
959           deefer->loaded = FALSE;
960           deefer->dbg_index = DEBUG_next_index;
961           deefer->module_name = xstrdup(module_name);
962
963           deefer->sectp = pe->pe_seg;
964           deefer->nsect = pe->pe_header->FileHeader.NumberOfSections;
965
966           dbglist = deefer;
967           break;
968         default:
969         }
970     }
971
972   DEBUG_next_index++;
973
974   return (rtn);
975
976 }
977
978 /*
979  * ELF modules are also entered into the list - this is so that we
980  * can make 'info shared' types of displays possible.
981  */
982 int
983 DEBUG_RegisterELFDebugInfo(int load_addr, u_long size, char * name)
984 {
985   struct deferred_debug_info * deefer;
986
987   deefer = (struct deferred_debug_info *) xmalloc(sizeof(*deefer));
988   deefer->pe = NULL;
989   
990   /*
991    * Read the important bits.  What we do after this depends
992    * upon the type, but this is always enough so we are able
993    * to proceed if we know what we need to do next.
994    */
995   deefer->dbg_size = size;
996   deefer->dbg_info = (char *) NULL;
997   
998   deefer->load_addr = (char *) load_addr;
999   deefer->dbgdir = NULL;
1000   deefer->next = dbglist;
1001   deefer->loaded = TRUE;
1002   deefer->dbg_index = DEBUG_next_index;
1003   deefer->module_name = xstrdup(name);
1004   dbglist = deefer;
1005
1006   DEBUG_next_index++;
1007
1008   return (TRUE);
1009 }
1010
1011
1012
1013 /*
1014  * Process COFF debugging information embedded in a Win32 application.
1015  *
1016  */
1017 static
1018 int
1019 DEBUG_ProcessCoff(struct deferred_debug_info * deefer)
1020 {
1021   struct CoffAuxSection * aux;
1022   struct CoffDebug   * coff;
1023   struct CoffFiles   * coff_files = NULL;
1024   struct CoffLinenum * coff_linetab;
1025   char               * coff_strtab;
1026   struct CoffSymbol  * coff_sym;
1027   struct CoffSymbol  * coff_symbol;
1028   struct CoffFiles   * curr_file = NULL;
1029   int                  i;
1030   int                  j;
1031   int                  k;
1032   struct CoffLinenum * linepnt;
1033   int                  linetab_indx;
1034   char                 namebuff[9];
1035   char               * nampnt;
1036   int                  naux;
1037   DBG_ADDR             new_addr;
1038   int                  nfiles = 0;
1039   int                  nfiles_alloc = 0;
1040   struct CoffFiles     orig_file;
1041   int                  rtn = FALSE;
1042   char               * this_file = NULL;
1043
1044   coff = (struct CoffDebug *) deefer->dbg_info;
1045
1046   coff_symbol = (struct CoffSymbol *) ((unsigned int) coff + coff->SymbolOffset);
1047   coff_linetab = (struct CoffLinenum *) ((unsigned int) coff + coff->LinenumberOffset);
1048   coff_strtab = (char *) ((unsigned int) coff_symbol + 18*coff->N_Sym);
1049
1050   linetab_indx = 0;
1051
1052   for(i=0; i < coff->N_Sym; i++ )
1053     {
1054       /*
1055        * We do this because some compilers (i.e. gcc) incorrectly
1056        * pad the structure up to a 4 byte boundary.  The structure
1057        * is really only 18 bytes long, so we have to manually make sure
1058        * we get it right.
1059        *
1060        * FIXME - there must be a way to have autoconf figure out the
1061        * correct compiler option for this.  If it is always gcc, that
1062        * makes life simpler, but I don't want to force this.
1063        */
1064       coff_sym = (struct CoffSymbol *) ((unsigned int) coff_symbol + 18*i);
1065       naux = coff_sym->NumberOfAuxSymbols;
1066
1067       if( coff_sym->StorageClass == IMAGE_SYM_CLASS_FILE )
1068         {
1069           if( nfiles + 1 >= nfiles_alloc )
1070             {
1071               nfiles_alloc += 10;
1072               coff_files = (struct CoffFiles *) realloc( coff_files,
1073                         nfiles_alloc * sizeof(struct CoffFiles));
1074             }
1075           curr_file = coff_files + nfiles;
1076           nfiles++;
1077           curr_file->startaddr = 0xffffffff;
1078           curr_file->endaddr   = 0;
1079           curr_file->filename =  ((char *) coff_sym) + 18;
1080           curr_file->linetab_offset = -1;
1081           curr_file->linecnt = 0;
1082           curr_file->entries = NULL;
1083           curr_file->neps = curr_file->neps_alloc = 0;
1084 #if 0
1085           fprintf(stderr,"New file %s\n", curr_file->filename);
1086 #endif
1087           i += naux;
1088           continue;
1089         }
1090
1091       /*
1092        * This guy marks the size and location of the text section
1093        * for the current file.  We need to keep track of this so
1094        * we can figure out what file the different global functions
1095        * go with.
1096        */
1097       if(    (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC)
1098           && (naux != 0)
1099           && (coff_sym->Type == 0)
1100           && (coff_sym->SectionNumber == 1) )
1101         {
1102           aux = (struct CoffAuxSection *) ((unsigned int) coff_sym + 18);
1103
1104           if( curr_file->linetab_offset != -1 )
1105             {
1106 #if 0
1107               fprintf(stderr, "Duplicating sect from %s: %x %x %x %d %d\n",
1108                       curr_file->filename,
1109                       aux->Length,
1110                       aux->NumberOfRelocations,
1111                       aux->NumberOfLinenumbers,
1112                       aux->Number,
1113                       aux->Selection);
1114               fprintf(stderr, "More sect %d %x %d %d %d\n", 
1115                       coff_sym->SectionNumber,
1116                       coff_sym->Value,
1117                       coff_sym->Type,
1118                       coff_sym->StorageClass,
1119                       coff_sym->NumberOfAuxSymbols);
1120 #endif
1121
1122               /*
1123                * Save this so we can copy bits from it.
1124                */
1125               orig_file = *curr_file;
1126
1127               /*
1128                * Duplicate the file entry.  We have no way to describe
1129                * multiple text sections in our current way of handling things.
1130                */
1131               if( nfiles + 1 >= nfiles_alloc )
1132                 {
1133                   nfiles_alloc += 10;
1134                   coff_files = (struct CoffFiles *) realloc( coff_files,
1135                                                              nfiles_alloc * sizeof(struct CoffFiles));
1136                 }
1137               curr_file = coff_files + nfiles;
1138               nfiles++;
1139               curr_file->startaddr = 0xffffffff;
1140               curr_file->endaddr   = 0;
1141               curr_file->filename = orig_file.filename;
1142               curr_file->linetab_offset = -1;
1143               curr_file->linecnt = 0;
1144               curr_file->entries = NULL;
1145               curr_file->neps = curr_file->neps_alloc = 0;
1146             }
1147 #if 0
1148           else
1149             {
1150               fprintf(stderr, "New text sect from %s: %x %x %x %d %d\n",
1151                       curr_file->filename,
1152                       aux->Length,
1153                       aux->NumberOfRelocations,
1154                       aux->NumberOfLinenumbers,
1155                       aux->Number,
1156                       aux->Selection);
1157             }
1158 #endif
1159
1160           if( curr_file->startaddr > coff_sym->Value )
1161             {
1162               curr_file->startaddr = coff_sym->Value;
1163             }
1164           
1165           if( curr_file->startaddr > coff_sym->Value )
1166             {
1167               curr_file->startaddr = coff_sym->Value;
1168             }
1169           
1170           if( curr_file->endaddr < coff_sym->Value + aux->Length )
1171             {
1172               curr_file->endaddr = coff_sym->Value + aux->Length;
1173             }
1174           
1175           curr_file->linetab_offset = linetab_indx;
1176           curr_file->linecnt = aux->NumberOfLinenumbers;
1177           linetab_indx += aux->NumberOfLinenumbers;
1178           i += naux;
1179           continue;
1180         }
1181
1182       if(    (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC)
1183           && (naux == 0)
1184           && (coff_sym->SectionNumber == 1) )
1185         {
1186           /*
1187            * This is a normal static function when naux == 0.
1188            * Just register it.  The current file is the correct
1189            * one in this instance.
1190            */
1191           if( coff_sym->N.Name.NotLong )
1192             {
1193               memcpy(namebuff, coff_sym->N.ShortName, 8);
1194               namebuff[8] = '\0';
1195               nampnt = &namebuff[0];
1196             }
1197           else
1198             {
1199               nampnt = coff_strtab + coff_sym->N.Name.StrTaboff;
1200             }
1201
1202           if( nampnt[0] == '_' )
1203             {
1204               nampnt++;
1205             }
1206
1207           new_addr.seg = 0;
1208           new_addr.off = (int) (deefer->load_addr + coff_sym->Value);
1209
1210           if( curr_file->neps + 1 >= curr_file->neps_alloc )
1211             {
1212               curr_file->neps_alloc += 10;
1213               curr_file->entries = (struct name_hash **) 
1214                 realloc( curr_file->entries, 
1215                          curr_file->neps_alloc * sizeof(struct name_hash *));
1216             }
1217 #if 0
1218           fprintf(stderr,"\tAdding static symbol %s\n", nampnt);
1219 #endif
1220           curr_file->entries[curr_file->neps++] =
1221             DEBUG_AddSymbol( nampnt, &new_addr, this_file, SYM_WIN32 );
1222           i += naux;
1223           continue;
1224         }
1225
1226       if(    (coff_sym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL)
1227           && ISFCN(coff_sym->Type)
1228           && (coff_sym->SectionNumber > 0) )
1229         {
1230           if( coff_sym->N.Name.NotLong )
1231             {
1232               memcpy(namebuff, coff_sym->N.ShortName, 8);
1233               namebuff[8] = '\0';
1234               nampnt = &namebuff[0];
1235             }
1236           else
1237             {
1238               nampnt = coff_strtab + coff_sym->N.Name.StrTaboff;
1239             }
1240
1241
1242           if( nampnt[0] == '_' )
1243             {
1244               nampnt++;
1245             }
1246
1247           new_addr.seg = 0;
1248           new_addr.off = (int) (deefer->load_addr + coff_sym->Value);
1249
1250 #if 0
1251           fprintf(stderr, "%d: %x %s\n", i, new_addr.off, nampnt);
1252
1253           fprintf(stderr,"\tAdding global symbol %s\n", nampnt);
1254 #endif
1255
1256           /*
1257            * Now we need to figure out which file this guy belongs to.
1258            */
1259           this_file = NULL;
1260           for(j=0; j < nfiles; j++)
1261             {
1262               if( coff_files[j].startaddr <= coff_sym->Value
1263                   && coff_files[j].endaddr > coff_sym->Value )
1264                 {
1265                   this_file = coff_files[j].filename;
1266                   break;
1267                 }
1268             }
1269           if( coff_files[j].neps + 1 >= coff_files[j].neps_alloc )
1270             {
1271               coff_files[j].neps_alloc += 10;
1272               coff_files[j].entries = (struct name_hash **) 
1273                 realloc( coff_files[j].entries, 
1274                          coff_files[j].neps_alloc * sizeof(struct name_hash *));
1275             }
1276           coff_files[j].entries[coff_files[j].neps++] =
1277             DEBUG_AddSymbol( nampnt, &new_addr, this_file, SYM_WIN32 );
1278           i += naux;
1279           continue;
1280         }
1281
1282       if(    (coff_sym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL)
1283           && (coff_sym->SectionNumber > 0) )
1284         {
1285           /*
1286            * Similar to above, but for the case of data symbols.
1287            * These aren't treated as entrypoints.
1288            */
1289           if( coff_sym->N.Name.NotLong )
1290             {
1291               memcpy(namebuff, coff_sym->N.ShortName, 8);
1292               namebuff[8] = '\0';
1293               nampnt = &namebuff[0];
1294             }
1295           else
1296             {
1297               nampnt = coff_strtab + coff_sym->N.Name.StrTaboff;
1298             }
1299
1300
1301           if( nampnt[0] == '_' )
1302             {
1303               nampnt++;
1304             }
1305
1306           new_addr.seg = 0;
1307           new_addr.off = (int) (deefer->load_addr + coff_sym->Value);
1308
1309 #if 0
1310           fprintf(stderr, "%d: %x %s\n", i, new_addr.off, nampnt);
1311
1312           fprintf(stderr,"\tAdding global data symbol %s\n", nampnt);
1313 #endif
1314
1315           /*
1316            * Now we need to figure out which file this guy belongs to.
1317            */
1318           DEBUG_AddSymbol( nampnt, &new_addr, NULL, SYM_WIN32 );
1319           i += naux;
1320           continue;
1321         }
1322           
1323       if(    (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC)
1324           && (naux == 0) )
1325         {
1326           /*
1327            * Ignore these.  They don't have anything to do with
1328            * reality.
1329            */
1330           i += naux;
1331           continue;
1332         }
1333
1334 #if 0
1335       fprintf(stderr,"Skipping unknown entry %d %d %d\n", coff_sym->StorageClass, 
1336               coff_sym->SectionNumber, naux);
1337 #endif
1338
1339       /*
1340        * For now, skip past the aux entries.
1341        */
1342       i += naux;
1343       
1344     }
1345     
1346   /*
1347    * OK, we now should have a list of files, and we should have a list
1348    * of entrypoints.  We need to sort the entrypoints so that we are
1349    * able to tie the line numbers with the given functions within the
1350    * file.
1351    */
1352   if( coff_files != NULL )
1353     {
1354       for(j=0; j < nfiles; j++)
1355         {
1356           if( coff_files[j].entries != NULL )
1357             {
1358               qsort(coff_files[j].entries, coff_files[j].neps,
1359                     sizeof(struct name_hash *), DEBUG_cmp_sym);
1360             }
1361         }
1362
1363       /*
1364        * Now pick apart the line number tables, and attach the entries
1365        * to the given functions.
1366        */
1367       for(j=0; j < nfiles; j++)
1368         {
1369           i = 0;
1370           for(k=0; k < coff_files[j].linecnt; k++)
1371             {
1372               /*
1373                * Another monstrosity caused by the fact that we are using
1374                * a 6 byte structure, and gcc wants to pad structures to 4 byte
1375                * boundaries.  Otherwise we could just index into an array.
1376                */
1377               linepnt = (struct CoffLinenum *) 
1378                 ((unsigned int) coff_linetab + 
1379                  6*(coff_files[j].linetab_offset + k));
1380               /*
1381                * If we have spilled onto the next entrypoint, then
1382                * bump the counter..
1383                */
1384               while(TRUE)
1385                 {
1386                   DEBUG_GetSymbolAddr(coff_files[j].entries[i+1], &new_addr);
1387                   if(   (i+1 < coff_files[j].neps)
1388                         && (   ((unsigned int) deefer->load_addr + linepnt->VirtualAddr)
1389                                >= new_addr.off) )
1390                     {
1391                       i++;
1392                     }
1393                   else
1394                     {
1395                       break;
1396                     }
1397                 }
1398
1399               /*
1400                * Add the line number.  This is always relative to the
1401                * start of the function, so we need to subtract that offset
1402                * first.
1403                */
1404               DEBUG_GetSymbolAddr(coff_files[j].entries[i], &new_addr);
1405               DEBUG_AddLineNumber(coff_files[j].entries[i], 
1406                                   linepnt->Linenum,
1407                                   (unsigned int) deefer->load_addr 
1408                                   + linepnt->VirtualAddr 
1409                                   - new_addr.off);
1410             }
1411         }
1412     }
1413
1414   rtn = TRUE;
1415
1416   if( coff_files != NULL )
1417     {
1418       for(j=0; j < nfiles; j++)
1419         {
1420           if( coff_files[j].entries != NULL )
1421             {
1422               free(coff_files[j].entries);
1423             }
1424         }
1425       free(coff_files);
1426     }
1427
1428   return (rtn);
1429
1430 }
1431
1432 /*
1433  * Process a codeview line number table.  Digestify the thing so that
1434  * we can easily reference the thing when we process the rest of
1435  * the information.
1436  */
1437 static struct codeview_linetab_hdr *
1438 DEBUG_SnarfLinetab(char                      * linetab,
1439                    int                         size)
1440 {
1441   int                             file_segcount;
1442   char                            filename[PATH_MAX];
1443   unsigned int                  * filetab;
1444   char                          * fn;
1445   int                             i;
1446   int                             k;
1447   struct codeview_linetab_hdr   * lt_hdr;
1448   unsigned int                  * lt_ptr;
1449   int                             nfile;
1450   int                             nseg;
1451   union any_size                  pnt;
1452   union any_size                  pnt2;
1453   struct startend               * start;
1454   int                             this_seg;
1455
1456   /*
1457    * Now get the important bits.
1458    */
1459   pnt = (union any_size) linetab;
1460   nfile = *pnt.s++;
1461   nseg = *pnt.s++;
1462
1463   filetab = (unsigned int *) pnt.c;
1464
1465   /*
1466    * Now count up the number of segments in the file.
1467    */
1468   nseg = 0;
1469   for(i=0; i<nfile; i++)
1470     {
1471       pnt2 = (union any_size) (linetab + filetab[i]);
1472       nseg += *pnt2.s;
1473     }
1474
1475   /*
1476    * Next allocate the header we will be returning.
1477    * There is one header for each segment, so that we can reach in
1478    * and pull bits as required.
1479    */
1480   lt_hdr = (struct codeview_linetab_hdr *) 
1481     xmalloc((nseg + 1) * sizeof(*lt_hdr));
1482   if( lt_hdr == NULL )
1483     {
1484       goto leave;
1485     }
1486
1487   memset(lt_hdr, 0, sizeof(*lt_hdr) * (nseg+1));
1488
1489   /*
1490    * Now fill the header we will be returning, one for each segment.
1491    * Note that this will basically just contain pointers into the existing
1492    * line table, and we do not actually copy any additional information
1493    * or allocate any additional memory.
1494    */
1495
1496   this_seg = 0;
1497   for(i=0; i<nfile; i++)
1498     {
1499       /*
1500        * Get the pointer into the segment information.
1501        */
1502       pnt2 = (union any_size) (linetab + filetab[i]);
1503       file_segcount = *pnt2.s;
1504
1505       pnt2.ui++;
1506       lt_ptr = (unsigned int *) pnt2.c;
1507       start = (struct startend *) (lt_ptr + file_segcount);
1508
1509       /*
1510        * Now snarf the filename for all of the segments for this file.
1511        */
1512       fn = (unsigned char *) (start + file_segcount);
1513       memset(filename, 0, sizeof(filename));
1514       memcpy(filename, fn + 1, *fn);
1515       fn = strdup(filename);
1516
1517       for(k = 0; k < file_segcount; k++, this_seg++)
1518         {
1519           pnt2 = (union any_size) (linetab + lt_ptr[k]);
1520           lt_hdr[this_seg].start      = start[k].start;
1521           lt_hdr[this_seg].end        = start[k].end;
1522           lt_hdr[this_seg].sourcefile = fn;
1523           lt_hdr[this_seg].segno      = *pnt2.s++;
1524           lt_hdr[this_seg].nline      = *pnt2.s++;
1525           lt_hdr[this_seg].offtab     =  pnt2.ui;
1526           lt_hdr[this_seg].linetab    = (unsigned short *) 
1527             (pnt2.ui + lt_hdr[this_seg].nline);
1528         }
1529     }
1530
1531 leave:
1532
1533   return lt_hdr;
1534
1535 }
1536
1537 static int
1538 DEBUG_SnarfCodeView(      struct deferred_debug_info * deefer,
1539                           char                       * cv_data,
1540                           int                          size,
1541                           struct codeview_linetab_hdr * linetab)
1542 {
1543   struct name_hash      * curr_func = NULL;
1544   struct wine_locals    * curr_sym = NULL;
1545   int                     i;
1546   int                     j;
1547   int                     len;
1548   DBG_ADDR                new_addr;
1549   int                     nsect;
1550   union any_size          ptr;
1551   IMAGE_SECTION_HEADER  * sectp;
1552   union codeview_symbol * sym;
1553   char                    symname[PATH_MAX];
1554   struct name_hash      * thunk_sym = NULL;
1555
1556   ptr = (union any_size) cv_data;
1557   nsect = deefer->nsect;
1558   sectp = deefer->sectp;
1559
1560   /*
1561    * Skip over the first word.  Don't really know what it means, but
1562    * it is useless.
1563    */
1564   ptr.ui++;
1565
1566   /*
1567    * Loop over the different types of records and whenever we
1568    * find something we are interested in, record it and move on.
1569    */
1570   while( ptr.c - cv_data < size )
1571     {
1572       sym = (union codeview_symbol *) ptr.c;
1573
1574       if( sym->generic.len - sizeof(int) == (ptr.c - cv_data) )
1575         {
1576           /*
1577            * This happens when we have indirect symbols that VC++ 4.2
1578            * sometimes uses when there isn't a line number table.
1579            * We ignore it - we will process and enter all of the
1580            * symbols in the global symbol table anyways, so there
1581            * isn't much point in keeping track of all of this crap.
1582            */
1583           break;
1584         }
1585
1586       memset(symname, 0, sizeof(symname));
1587       switch(sym->generic.id)
1588         {
1589         case S_GDATA32:
1590         case S_LDATA32:
1591         case S_PUB32:
1592           /*
1593            * First, a couple of sanity checks.
1594            */
1595           if( sym->data.namelen == 0 )
1596             {
1597               break;
1598             }
1599
1600           if( sym->data.seg == 0 || sym->data.seg > nsect )
1601             {
1602               break;
1603             }
1604
1605           /*
1606            * Global and local data symbols.  We don't associate these
1607            * with any given source file.
1608            */
1609
1610           memcpy(symname, sym->data.name, sym->data.namelen);
1611           new_addr.seg = 0;
1612           new_addr.type = DEBUG_GetCVType(sym->data.symtype);
1613           new_addr.off = (unsigned int) deefer->load_addr + 
1614             sectp[sym->data.seg - 1].VirtualAddress + 
1615             sym->data.offset;
1616           DEBUG_AddSymbol( symname, &new_addr, NULL, SYM_WIN32 | SYM_DATA );
1617           break;
1618         case S_THUNK32:
1619           /*
1620            * Sort of like a global function, but it just points
1621            * to a thunk, which is a stupid name for what amounts to
1622            * a PLT slot in the normal jargon that everyone else uses.
1623            */
1624           memcpy(symname, sym->thunk.name, sym->thunk.namelen);
1625           new_addr.seg = 0;
1626           new_addr.type = NULL;
1627           new_addr.off = (unsigned int) deefer->load_addr + 
1628             sectp[sym->thunk.segment - 1].VirtualAddress + 
1629             sym->thunk.offset;
1630           thunk_sym = DEBUG_AddSymbol( symname, &new_addr, NULL, 
1631                                        SYM_WIN32 | SYM_FUNC);
1632           DEBUG_SetSymbolSize(thunk_sym, sym->thunk.thunk_len);
1633           break;
1634         case S_GPROC32:
1635         case S_LPROC32:
1636           /*
1637            * Global and static functions.
1638            */
1639           memcpy(symname, sym->proc.name, sym->proc.namelen);
1640           new_addr.seg = 0;
1641           new_addr.type = DEBUG_GetCVType(sym->proc.proctype);
1642           new_addr.off = (unsigned int) deefer->load_addr + 
1643             sectp[sym->proc.segment - 1].VirtualAddress + 
1644             sym->proc.offset;
1645           /*
1646            * See if we can find a segment that this goes with.  If so,
1647            * it means that we also may have line number information
1648            * for this function.
1649            */
1650           for(i=0; linetab[i].linetab != NULL; i++)
1651             {
1652               if(     ((unsigned int) deefer->load_addr 
1653                        + sectp[linetab[i].segno - 1].VirtualAddress 
1654                        + linetab[i].start <= new_addr.off)
1655                   &&  ((unsigned int) deefer->load_addr 
1656                        + sectp[linetab[i].segno - 1].VirtualAddress 
1657                        + linetab[i].end > new_addr.off) )
1658                 {
1659                   break;
1660                 }
1661             }
1662
1663           DEBUG_Normalize(curr_func);
1664           if( linetab[i].linetab == NULL )
1665             {
1666               curr_func = DEBUG_AddSymbol( symname, &new_addr, NULL,
1667                                            SYM_WIN32 | SYM_FUNC);
1668             }
1669           else
1670             {
1671               /*
1672                * First, create the entry.  Then dig through the linetab
1673                * and add whatever line numbers are appropriate for this
1674                * function.
1675                */
1676               curr_func = DEBUG_AddSymbol( symname, &new_addr, 
1677                                            linetab[i].sourcefile,
1678                                            SYM_WIN32 | SYM_FUNC);
1679               for(j=0; j < linetab[i].nline; j++)
1680                 {
1681                   if( linetab[i].offtab[j] >= sym->proc.offset 
1682                       && linetab[i].offtab[j] < sym->proc.offset 
1683                       + sym->proc.proc_len )
1684                     {
1685                       DEBUG_AddLineNumber(curr_func, linetab[i].linetab[j],
1686                                           linetab[i].offtab[j] - sym->proc.offset);
1687                     }
1688                 }
1689
1690             }
1691
1692           /*
1693            * Add information about where we should set breakpoints
1694            * in this function.
1695            */
1696           DEBUG_SetSymbolBPOff(curr_func, sym->proc.debug_start);
1697           DEBUG_SetSymbolSize(curr_func, sym->proc.proc_len);
1698           break;
1699         case S_BPREL32:
1700           /*
1701            * Function parameters and stack variables.
1702            */
1703           memcpy(symname, sym->stack.name, sym->stack.namelen);
1704           curr_sym = DEBUG_AddLocal(curr_func, 
1705                          0, 
1706                          sym->stack.offset, 
1707                          0, 
1708                          0, 
1709                          symname);
1710           DEBUG_SetLocalSymbolType(curr_sym, DEBUG_GetCVType(sym->stack.symtype));
1711           
1712           break;
1713         default:
1714           break;
1715         }
1716
1717       /*
1718        * Adjust pointer to point to next entry, rounding up to a word
1719        * boundary.  MS preserving alignment?  Stranger things have
1720        * happened.
1721        */
1722       if( sym->generic.id == S_PROCREF
1723           || sym->generic.id == S_DATAREF
1724           || sym->generic.id == S_UNKNOWN )
1725         {
1726           len = (sym->generic.len + 3) & ~3;
1727           len += ptr.c[16] + 1;
1728           ptr.c += (len + 3) & ~3;
1729         }
1730       else
1731         {
1732           ptr.c += (sym->generic.len + 3) & ~3;
1733         }
1734     }
1735
1736   if( linetab != NULL )
1737     {
1738       free(linetab);
1739     }
1740
1741   return TRUE;
1742 }
1743
1744
1745 /*
1746  * Process PDB file which contains debug information.
1747  *
1748  * These are really weird beasts.  They are intended to be incrementally
1749  * updated by the incremental linker, and this means that you need to 
1750  * be able to remove and add information.  Thus the PDB file is sort of
1751  * like a block structured device, with a freelist and lists of extent numbers
1752  * that are used to get the relevant pieces.  In all cases seen so far, the
1753  * blocksize is always 0x400 bytes.  The header has a field which apparently
1754  * holds the blocksize, so if it ever changes we are safe.
1755  *
1756  * In general, every time we need to extract something from the pdb file,
1757  * it is easier to copy it into another buffer so we have the information
1758  * in one contiguous block rather than attempt to try and keep track of when
1759  * we need to grab another extent from the pdb file.
1760  *
1761  * The thing that is a real pain about some MS stuff is that they choose
1762  * data structures which are not representable in C.  Thus we have to
1763  * hack around and diddle pointers.
1764  */
1765 /* static */
1766 int
1767 DEBUG_ProcessPDBFile(struct deferred_debug_info * deefer, char * full_filename)
1768 {
1769   char                        * addr = (char *) 0xffffffff;
1770   unsigned int                  blocksize;
1771   unsigned int                  bufflen = 0;
1772   char                        * buffer = NULL;
1773   unsigned short              * extent_table;
1774   int                           fd = -1;
1775   struct file_ent             * fent;
1776   char                        * filename;
1777   struct file_list            * filelist = NULL;
1778   unsigned int                  gsym_record = 0;
1779   char                        * gsymtab = NULL;
1780   struct filetab_hdr          * hd;
1781   int                           i;
1782   int                           j;
1783   unsigned int                  last_extent;
1784   struct codeview_linetab_hdr * linetab;
1785   unsigned int                  nblocks;
1786   unsigned int                  npair;
1787   unsigned int                  offset;
1788   struct codeview_pdb_hdr     * pdbhdr;
1789   unsigned int                * pnt;
1790   struct stat                   statbuf;
1791   int                           status;
1792   unsigned short              * table;
1793   char                        * toc;
1794   unsigned int                  toc_blocks;
1795   
1796   /*
1797    * FIXME - we should use some kind of search path mechanism to locate
1798    * PDB files.  Right now we just look in the current working directory,
1799    * which works much of the time, I guess.  Ideally we should be able to
1800    * map the filename back using the settings in wine.ini and perhaps
1801    * we could find it there.  This bit of coding is left as an exercise
1802    * for the reader. :-).
1803    */
1804   filename = strrchr(full_filename, '\\');
1805   if( filename == NULL )
1806     {
1807       filename = full_filename;
1808     }
1809   else
1810     {
1811       filename++;
1812     }
1813
1814   status = stat(filename, &statbuf);
1815   if( status == -1 )
1816     {
1817       fprintf(stderr, "Unable to open .PDB file %s\n", filename);
1818       goto leave;
1819     }
1820
1821   /*
1822    * Now open the file, so that we can mmap() it.
1823    */
1824   fd = open(filename, O_RDONLY);
1825   if( fd == -1 )
1826     {
1827       fprintf(stderr, "Unable to open .DBG file %s\n", filename);
1828       goto leave;
1829     }
1830
1831
1832   /*
1833    * Now mmap() the file.
1834    */
1835   addr = mmap(0, statbuf.st_size, PROT_READ, 
1836               MAP_PRIVATE, fd, 0);
1837   if( addr == (char *) 0xffffffff )
1838     {
1839       fprintf(stderr, "Unable to mmap .DBG file %s\n", filename);
1840       goto leave;
1841     }
1842
1843   /*
1844    * Now that we have the formalities over and done with, we need
1845    * to find the table of contents for the PDB file.
1846    */
1847   pdbhdr = (struct codeview_pdb_hdr *) addr;
1848   blocksize = pdbhdr->blocksize;
1849   last_extent = (statbuf.st_size + blocksize - 1) / blocksize;
1850
1851   /*
1852    * The TOC itself isn't always contiguous, so we need to extract a few
1853    * extents from the file to form the TOC.
1854    */
1855   toc_blocks = (pdbhdr->toc_len + blocksize - 1) / blocksize;
1856   toc = (char *) xmalloc(toc_blocks * blocksize);
1857   table = pdbhdr->toc_ext;
1858   for(i=0; i < toc_blocks; i++)
1859     {
1860       memcpy(toc + blocksize*i, addr + table[i]*blocksize, blocksize);
1861     }
1862
1863   /*
1864    * Next build our own table which will have the size and extent block
1865    * list for each record in the PDB file.
1866    *
1867    * The TOC starts out with the number of files.  Then it is followed by
1868    * (npair * 2*sizeof(int)) bytes of information, which are pairs of ints.
1869    * The first one is the size of the record (in bytes), and the second one
1870    * is something else which I haven't figured out yet.
1871    */
1872   pnt = (unsigned int *) toc;
1873   npair = *pnt++;
1874   extent_table = (unsigned short *) ((unsigned int) toc +
1875                                      npair * 2 * sizeof(int) + sizeof(int));
1876
1877   /*
1878    * Sanity check.
1879    */
1880   if( sizeof(int) + 2*sizeof(int)*npair > pdbhdr->toc_len )
1881     {
1882       goto leave;
1883     }
1884   
1885   filelist = (struct file_list *) xmalloc(npair * sizeof(*filelist));
1886   if( filelist == NULL )
1887     {
1888       goto leave;
1889     }
1890   memset(filelist, 0, npair * sizeof(*filelist));
1891
1892   nblocks = 0;
1893   for(i=0; i < npair; i++)
1894     {
1895       filelist[i].record_len = pnt[i*2];
1896       filelist[i].nextents   = (filelist[i].record_len + blocksize - 1) 
1897         / blocksize;
1898       filelist[i].extent_list = extent_table + nblocks;
1899       nblocks += filelist[i].nextents;
1900
1901       /*
1902        * These get filled in later when we parse one of the records.
1903        */
1904       filelist[i].linetab_offset = 0;
1905       filelist[i].linetab_len = 0;
1906     }
1907
1908   /*
1909    * OK, now walk through the various records and pick out the bits we
1910    * really want to see.  Some of the records are extra special, and
1911    * we need to handle these a little bit differently.
1912    */
1913   for(i=0; i < npair; i++)
1914     {
1915       if( filelist[i].record_len == 0xffffffff )
1916         {
1917           continue;
1918         }
1919
1920       /*
1921        * Make sure our buffer is large enough to hold the record.
1922        */
1923       if( bufflen < filelist[i].nextents * blocksize )
1924         {
1925           bufflen = filelist[i].nextents * blocksize;
1926           buffer = (char *) realloc(buffer, bufflen);
1927         }
1928
1929       /*
1930        * Do this just for completeness.  It makes debugging easier
1931        * if we have a clean indication of where the record ends.
1932        */
1933       memset(buffer, 0, filelist[i].nextents * blocksize);
1934
1935       /*
1936        * Next, build the record using the extent list.
1937        */
1938       for(j=0; j < filelist[i].nextents; j++)
1939         {
1940           memcpy(buffer + j * blocksize,
1941                  addr + filelist[i].extent_list[j] * blocksize,
1942                  blocksize);
1943         }
1944
1945       pnt = (unsigned int *) buffer;
1946
1947       /*
1948        * OK, now figure out what to do with it.
1949        */
1950
1951       /*
1952        * Always ignore the first entry.  It seems to contain a backup copy
1953        * of the TOC (the last time the file was modified??)
1954        */
1955       if( i == 0 )
1956         {
1957           continue;
1958         }
1959
1960       /* 
1961        * The second entry as a id block.  It contains a magic number
1962        * to identify the compiler, plus it also contains the timestamp
1963        * which must match the timestamp in the executable.  
1964        */
1965       if( i == 1 )
1966         {
1967
1968           if( ((*pnt != 19950623) && (*pnt != 19950814))
1969               || (filelist[i].record_len != 0x24)
1970               || (pnt[1] != ((struct CodeViewDebug *)(deefer->dbg_info))->cv_timestamp) )
1971             {
1972               goto leave;
1973             }
1974         }
1975
1976       /*
1977        * The third entry contains pointers to the global symbol table,
1978        * plus it also contains additional information about each record
1979        * in the PDB file.
1980        */
1981       if( i == 3 )
1982         {
1983           hd = (struct filetab_hdr *) buffer;
1984
1985           gsym_record = hd->gsym_file;
1986           gsymtab = (char *) xmalloc( filelist[gsym_record].nextents 
1987                                       * blocksize);
1988           memset(gsymtab, 0, filelist[gsym_record].nextents * blocksize);
1989           
1990           for(j=0; j < filelist[gsym_record].nextents; j++)
1991             {
1992               memcpy(gsymtab + j * blocksize,
1993                      addr + filelist[gsym_record].extent_list[j] * blocksize,
1994                      blocksize);
1995             }
1996
1997           /*
1998            * This record also contains information about where in the
1999            * remaining records we will be able to find the start of the
2000            * line number table.  We could locate that bit using heuristics,
2001            * but since we have the info handy, we might as well use it.
2002            */
2003           offset = sizeof(*hd);
2004           while(1==1)
2005             {
2006               fent = (struct file_ent *) (buffer + offset);
2007               if( offset > hd->ftab_len )
2008                 {
2009                   break;
2010                 }
2011
2012               if( fent->file_number == 0 || fent->file_number >= npair )
2013                 {
2014                   break;
2015                 }
2016
2017               filelist[fent->file_number].linetab_offset = 
2018                 fent->linetab_offset;
2019               filelist[fent->file_number].linetab_len = 
2020                 fent->linetab_len;
2021               /*
2022                * Figure out the offset of the next entry.
2023                * There is a fixed part of the record and a variable
2024                * length filename which we must also skip past.
2025                */
2026               offset += ((unsigned int) &fent->filename - (unsigned int) fent)
2027                 + strlen(fent->filename) + 1;
2028               offset += strlen(buffer+offset) + 1;
2029               offset = (offset + 3) & ~3;
2030             }
2031         }
2032       
2033
2034       /*
2035        * Two different magic numbers used as dates.
2036        * These indicate the 'type' table.
2037        */
2038       if(       *pnt == 19950410
2039              || *pnt == 19951122 )
2040         {
2041           DEBUG_ParseTypeTable(buffer, filelist[i].record_len);
2042           continue;
2043         }
2044
2045       /*
2046        * This is something we really want to look at, since it contains
2047        * real debug info.  Anything that doesn't match this can be
2048        * ignored for now.
2049        */
2050       if( *pnt == 1 )
2051         {
2052           /*
2053            * First, snag the line table, if we have one.  This always
2054            * occurs at the end of the record, so we take the linetab
2055            * offset as the end of the normal part of the record.
2056            */
2057           linetab = NULL;
2058           if( filelist[i].linetab_len != 0 )
2059             {
2060               linetab = DEBUG_SnarfLinetab(buffer + filelist[i].linetab_offset,
2061                                            filelist[i].linetab_len);
2062               DEBUG_SnarfCodeView(deefer, buffer,
2063                                   filelist[i].linetab_offset,
2064                                   linetab);
2065             }
2066           else
2067             {
2068               DEBUG_SnarfCodeView(deefer, buffer,
2069                                   filelist[i].record_len,
2070                                   linetab);
2071             }
2072           continue;
2073         }
2074     }
2075
2076   /*
2077    * Finally, process the global symbol table itself.  There isn't
2078    * a line number component to this, so we just toss everything
2079    * into the mix and it all should work out.
2080    */
2081   if( gsym_record != 0 )
2082     {
2083       DEBUG_SnarfCodeView(deefer, gsymtab - sizeof(int), 
2084                           filelist[gsym_record].record_len,
2085                           NULL);
2086     }
2087   
2088 leave:
2089
2090   if( gsymtab != NULL )
2091     {
2092       free(gsymtab);
2093       gsymtab = NULL;
2094     }
2095
2096   if( buffer != NULL )
2097     {
2098       free(buffer);
2099     }
2100
2101   if( filelist != NULL )
2102     {
2103       free(filelist);
2104     }
2105
2106   if( addr != (char *) 0xffffffff )
2107     {
2108       munmap(addr, statbuf.st_size);
2109     }
2110
2111   if( fd != -1 )
2112     {
2113       close(fd);
2114     }
2115
2116   return TRUE;
2117 }
2118
2119 /*
2120  * Process DBG file which contains debug information.
2121  */
2122 /* static */
2123 int
2124 DEBUG_ProcessDBGFile(struct deferred_debug_info * deefer, char * filename)
2125 {
2126   char                        * addr = (char *) 0xffffffff;
2127   char                        * codeview;
2128   struct CV4_DirHead          * codeview_dir;
2129   struct CV4_DirEnt           * codeview_dent;
2130   LPIMAGE_DEBUG_DIRECTORY       dbghdr;
2131   struct deferred_debug_info    deefer2;
2132   int                           fd = -1;
2133   int                           i;
2134   int                           j;
2135   struct codeview_linetab_hdr * linetab;
2136   int                           nsect;
2137   LPIMAGE_SEPARATE_DEBUG_HEADER pdbg = NULL;
2138   IMAGE_SECTION_HEADER        * sectp;
2139   struct stat                   statbuf;
2140   int                           status;
2141   
2142   status = stat(filename, &statbuf);
2143   if( status == -1 )
2144     {
2145       fprintf(stderr, "Unable to open .DBG file %s\n", filename);
2146       goto leave;
2147     }
2148
2149   /*
2150    * Now open the file, so that we can mmap() it.
2151    */
2152   fd = open(filename, O_RDONLY);
2153   if( fd == -1 )
2154     {
2155       fprintf(stderr, "Unable to open .DBG file %s\n", filename);
2156       goto leave;
2157     }
2158
2159
2160   /*
2161    * Now mmap() the file.
2162    */
2163   addr = mmap(0, statbuf.st_size, PROT_READ, 
2164               MAP_PRIVATE, fd, 0);
2165   if( addr == (char *) 0xffffffff )
2166     {
2167       fprintf(stderr, "Unable to mmap .DBG file %s\n", filename);
2168       goto leave;
2169     }
2170
2171   pdbg = (LPIMAGE_SEPARATE_DEBUG_HEADER) addr;
2172
2173   if( pdbg->TimeDateStamp != deefer->dbgdir->TimeDateStamp )
2174     {
2175       fprintf(stderr, "Warning - %s has incorrect internal timestamp\n",
2176               filename);
2177       goto leave;
2178    }
2179
2180   fprintf(stderr, "Processing symbols from %s...\n", filename);
2181
2182   dbghdr = (LPIMAGE_DEBUG_DIRECTORY) (  addr + sizeof(*pdbg) 
2183                  + pdbg->NumberOfSections * sizeof(IMAGE_SECTION_HEADER) 
2184                  + pdbg->ExportedNamesSize);
2185
2186   sectp = (LPIMAGE_SECTION_HEADER) ((char *) pdbg + sizeof(*pdbg));
2187   nsect = pdbg->NumberOfSections;
2188
2189   for( i=0; i < pdbg->DebugDirectorySize / sizeof(*pdbg); i++, dbghdr++ )
2190     {
2191       switch(dbghdr->Type)
2192                 {
2193                 case IMAGE_DEBUG_TYPE_COFF:
2194                   /*
2195                    * Dummy up a deferred debug header to handle the
2196                    * COFF stuff embedded within the DBG file.
2197                    */
2198                   memset((char *) &deefer2, 0, sizeof(deefer2));
2199                   deefer2.dbg_info = (addr + dbghdr->PointerToRawData);
2200                   deefer2.dbg_size = dbghdr->SizeOfData;
2201                   deefer2.load_addr = deefer->load_addr;
2202
2203                   DEBUG_ProcessCoff(&deefer2);
2204                   break;
2205                 case IMAGE_DEBUG_TYPE_CODEVIEW:
2206                   /*
2207                    * This is the older format by which codeview stuff is 
2208                    * stored, known as the 'NB09' format.  Newer executables
2209                    * and dlls created by VC++ use PDB files instead, which
2210                    * have lots of internal similarities, but the overall
2211                    * format and structure is quite different.
2212                    */
2213                   codeview = (addr + dbghdr->PointerToRawData);
2214
2215                   /*
2216                    * The first thing in the codeview section should be
2217                    * an 'NB09' identifier.  As a sanity check, make sure
2218                    * it is there.
2219                    */
2220                   if( *((unsigned int*) codeview) != 0x3930424e )
2221                     {
2222                       break;
2223                     }
2224                   
2225                   /*
2226                    * Next we need to find the directory.  This is easy too.
2227                    */
2228                   codeview_dir = (struct CV4_DirHead *) 
2229                     (codeview + ((unsigned int*) codeview)[1]);
2230
2231                   /*
2232                    * Some more sanity checks.  Make sure that everything
2233                    * is as we expect it.
2234                    */
2235                   if( codeview_dir->next_offset != 0 
2236                       || codeview_dir->dhsize != sizeof(*codeview_dir)
2237                       || codeview_dir->desize != sizeof(*codeview_dent) )
2238                     {
2239                       break;
2240                     }
2241                   codeview_dent = (struct CV4_DirEnt *) (codeview_dir + 1);
2242
2243                   for(j=0; j < codeview_dir->ndir; j++, codeview_dent++)
2244                     {
2245                       if( codeview_dent->subsect_number == sstAlignSym )
2246                         {
2247                           /*
2248                            * Check the previous entry.  If it is a
2249                            * sstSrcModule, it contains the line number
2250                            * info for this file.
2251                            */
2252                           linetab = NULL;
2253                           if( codeview_dent[1].module_number == codeview_dent[0].module_number
2254                               && codeview_dent[1].subsect_number == sstSrcModule )
2255                             {
2256                               linetab = DEBUG_SnarfLinetab(
2257                                            codeview + codeview_dent[1].offset,
2258                                            codeview_dent[1].size);
2259                             }
2260
2261                           if( codeview_dent[-1].module_number == codeview_dent[0].module_number
2262                               && codeview_dent[-1].subsect_number == sstSrcModule )
2263                             {
2264                               linetab = DEBUG_SnarfLinetab(
2265                                            codeview + codeview_dent[-1].offset,
2266                                            codeview_dent[-1].size);
2267                             }
2268                           /*
2269                            * Now process the CV stuff.
2270                            */
2271                           DEBUG_SnarfCodeView(deefer, 
2272                                               codeview + codeview_dent->offset,
2273                                               codeview_dent->size,
2274                                               linetab);
2275                         }
2276                     }
2277
2278                   break;
2279                 default:
2280                   break;
2281                 }
2282     }
2283 leave:
2284
2285   if( addr != (char *) 0xffffffff )
2286     {
2287       munmap(addr, statbuf.st_size);
2288     }
2289
2290   if( fd != -1 )
2291     {
2292       close(fd);
2293     }
2294
2295   return TRUE;
2296 }
2297
2298 int
2299 DEBUG_ProcessDeferredDebug()
2300 {
2301   struct deferred_debug_info * deefer;
2302   struct CodeViewDebug       * cvd;
2303   struct MiscDebug           * misc;
2304   char                       * filename;
2305   int                          last_proc = -1;
2306
2307   DEBUG_InitCVDataTypes();
2308
2309   for(deefer = dbglist; deefer; deefer = deefer->next)
2310     {
2311       if( deefer->loaded )
2312         {
2313           continue;
2314         }
2315
2316       if( last_proc != deefer->dbg_index )
2317         {
2318           fprintf(stderr, " %s",deefer->module_name);
2319           last_proc = deefer->dbg_index;
2320         }
2321
2322       switch(deefer->dbgdir->Type)
2323         {
2324         case IMAGE_DEBUG_TYPE_COFF:
2325           /*
2326            * Standard COFF debug information that VC++ adds when you
2327            * use /debugtype:both with the linker.
2328            */
2329 #if 0
2330           fprintf(stderr, "Processing COFF symbols...\n");
2331 #endif
2332           DEBUG_ProcessCoff(deefer);
2333           break;
2334         case IMAGE_DEBUG_TYPE_CODEVIEW:
2335           /*
2336            * This is a pointer to a PDB file of some sort.
2337            */
2338           cvd = (struct CodeViewDebug *) deefer->dbg_info;
2339
2340           if( strcmp(cvd->cv_nbtype, "NB10") != 0 )
2341             {
2342               /*
2343                * Whatever this is, we don't know how to deal with
2344                * it yet.
2345                */
2346               break;
2347             }
2348           DEBUG_ProcessPDBFile(deefer, cvd->cv_name);
2349 #if 0
2350           fprintf(stderr, "Processing PDB file %s\n", cvd->cv_name);
2351 #endif
2352           break;
2353         case IMAGE_DEBUG_TYPE_MISC:
2354           /*
2355            * A pointer to a .DBG file of some sort.  These files
2356            * can contain either CV4 or COFF information.  Open
2357            * the file, and try to do the right thing with it.
2358            */
2359           misc = (struct MiscDebug *) deefer->dbg_info;
2360
2361           filename = strrchr((char *) &misc->Data, '.');
2362
2363           /*
2364            * Ignore the file if it doesn't have a .DBG extension.
2365            */
2366           if(    (filename == NULL)
2367               || (    (strcmp(filename, ".dbg") != 0)
2368                    && (strcmp(filename, ".DBG") != 0)) )
2369             {
2370               break;
2371             }
2372
2373           filename = (char *) &misc->Data;
2374
2375           /*
2376            * Do the dirty deed...
2377            */
2378           DEBUG_ProcessDBGFile(deefer, filename);
2379       
2380           break;
2381         default:
2382           /*
2383            * We should never get here...
2384            */
2385           break;
2386         }
2387     }
2388   return TRUE;
2389
2390 }
2391
2392 /***********************************************************************
2393  *           DEBUG_InfoShare
2394  *
2395  * Display shared libarary information.
2396  */
2397 void DEBUG_InfoShare(void)
2398 {
2399   struct deferred_debug_info * deefer;
2400
2401   fprintf(stderr,"Address\t\tModule\tName\n");
2402
2403   for(deefer = dbglist; deefer; deefer = deefer->next)
2404     {
2405       if( deefer->pe == NULL )
2406         {
2407           fprintf(stderr,"0x%8.8x\t(ELF)\t%s\n", 
2408                   (unsigned int) deefer->load_addr,
2409                   deefer->module_name);
2410         }
2411       else
2412         {
2413           fprintf(stderr,"0x%8.8x\t(Win32)\t%s\n", 
2414                   (unsigned int) deefer->load_addr,
2415                   deefer->module_name);
2416         }
2417     }
2418 }
2419