Release 970509
[wine/multimedia.git] / debugger / msc.c
blob366887765018098658b8523e8859b06a2d698bc0
1 /*
2 * File msc.c - read VC++ debug information from COFF and eventually
3 * from PDB files.
5 * Copyright (C) 1996, Eric Youngdale.
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.
12 * TODO:
13 * Get 16 bit CV stuff working.
14 * Add symbol size to internal symbol table.
17 #include <stdio.h>
18 #include <stdlib.h>
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"
38 * This is used so that we have some idea of what we are in fact loading
39 * at any given time.
41 char * DEBUG_curr_module = NULL;
44 * This is an index we use to keep track of the debug information
45 * when we have multiple sources. We use the same database to also
46 * allow us to do an 'info shared' type of deal, and we use the index
47 * to eliminate duplicates.
49 static int DEBUG_next_index = 0;
51 union any_size
53 char * c;
54 short * s;
55 int * i;
56 unsigned int * ui;
60 * This is a convenience structure used to map portions of the
61 * line number table.
63 struct startend
65 unsigned int start;
66 unsigned int end;
70 * This is how we reference the various record types.
72 union codeview_symbol
74 struct
76 short int len;
77 short int id;
78 } generic;
80 struct
82 short int len;
83 short int id;
84 unsigned int offset;
85 unsigned short seg;
86 unsigned short symtype;
87 unsigned char namelen;
88 unsigned char name[1];
89 } data;
91 struct
93 short int len;
94 short int id;
95 unsigned int pparent;
96 unsigned int pend;
97 unsigned int next;
98 unsigned int offset;
99 unsigned short segment;
100 unsigned short thunk_len;
101 unsigned char thtype;
102 unsigned char namelen;
103 unsigned char name[1];
104 } thunk;
105 struct
107 short int len;
108 short int id;
109 unsigned int pparent;
110 unsigned int pend;
111 unsigned int next;
112 unsigned int proc_len;
113 unsigned int debug_start;
114 unsigned int debug_end;
115 unsigned int offset;
116 unsigned short segment;
117 unsigned short proctype;
118 unsigned char flags;
119 unsigned char namelen;
120 unsigned char name[1];
121 } proc;
122 struct
124 short int len; /* Total length of this entry */
125 short int id; /* Always S_BPREL32 */
126 unsigned int offset; /* Stack offset relative to BP */
127 unsigned short symtype;
128 unsigned char namelen;
129 unsigned char name[1];
130 } stack;
133 union codeview_type
135 struct
137 short int len;
138 short int id;
139 } generic;
141 struct
143 short int len;
144 short int id;
145 short int attribute;
146 short int datatype;
147 unsigned char variant[1];
148 } pointer;
150 struct
152 short int len;
153 short int id;
154 unsigned char nbits;
155 unsigned char bitoff;
156 unsigned short type;
157 } bitfield;
159 struct
161 short int len;
162 short int id;
163 short int elemtype;
164 short int idxtype;
165 unsigned char arrlen;
166 unsigned char namelen;
167 unsigned char name[1];
168 } array;
170 struct
172 short int len;
173 short int id;
174 short int n_element;
175 short int fieldlist;
176 short int property;
177 short int derived;
178 short int vshape;
179 unsigned short structlen;
180 unsigned char namelen;
181 unsigned char name[1];
182 } structure;
184 struct
186 short int len;
187 short int id;
188 short int count;
189 short int field;
190 short int property;
191 unsigned short un_len;
192 unsigned char namelen;
193 unsigned char name[1];
194 } t_union;
196 struct
198 short int len;
199 short int id;
200 short int count;
201 short int type;
202 short int field;
203 short int property;
204 unsigned char namelen;
205 unsigned char name[1];
206 } enumeration;
208 struct
210 short int id;
211 short int attribute;
212 unsigned short int value;
213 unsigned char namelen;
214 unsigned char name[1];
215 } enumerate;
217 struct
219 short int id;
220 short int type;
221 short int attribute;
222 unsigned short int offset;
223 unsigned char namelen;
224 unsigned char name[1];
225 } member;
227 struct
229 short int len;
230 short int id;
231 short int count;
232 short int type;
233 short int field;
234 short int property;
235 unsigned char namelen;
236 unsigned char name[1];
237 } fieldlist;
241 #define S_BPREL32 0x200
242 #define S_LDATA32 0x201
243 #define S_GDATA32 0x202
244 #define S_PUB32 0x203
245 #define S_LPROC32 0x204
246 #define S_GPROC32 0x205
247 #define S_THUNK32 0x206
248 #define S_BLOCK32 0x207
249 #define S_WITH32 0x208
250 #define S_LABEL32 0x209
252 #define S_PROCREF 0x400
253 #define S_DATAREF 0x401
254 #define S_ALIGN 0x402
255 #define S_UNKNOWN 0x403
258 * This covers the basic datatypes that VC++ seems to be using these days.
259 * 32 bit mode only. There are additional numbers for the pointers in 16
260 * bit mode. There are many other types listed in the documents, but these
261 * are apparently not used by the compiler, or represent pointer types
262 * that are not used.
264 #define T_NOTYPE 0x0000 /* Notype */
265 #define T_ABS 0x0001 /* Abs */
266 #define T_VOID 0x0003 /* Void */
267 #define T_CHAR 0x0010 /* signed char */
268 #define T_SHORT 0x0011 /* short */
269 #define T_LONG 0x0012 /* long */
270 #define T_QUAD 0x0013 /* long long */
271 #define T_UCHAR 0x0020 /* unsigned char */
272 #define T_USHORT 0x0021 /* unsigned short */
273 #define T_ULONG 0x0022 /* unsigned long */
274 #define T_UQUAD 0x0023 /* unsigned long long */
275 #define T_REAL32 0x0040 /* float */
276 #define T_REAL64 0x0041 /* double */
277 #define T_RCHAR 0x0070 /* real char */
278 #define T_WCHAR 0x0071 /* wide char */
279 #define T_INT4 0x0074 /* int */
280 #define T_UINT4 0x0075 /* unsigned int */
282 #define T_32PVOID 0x0403 /* 32 bit near pointer to void */
283 #define T_32PCHAR 0x0410 /* 16:32 near pointer to signed char */
284 #define T_32PSHORT 0x0411 /* 16:32 near pointer to short */
285 #define T_32PLONG 0x0412 /* 16:32 near pointer to int */
286 #define T_32PQUAD 0x0413 /* 16:32 near pointer to long long */
287 #define T_32PUCHAR 0x0420 /* 16:32 near pointer to unsigned char */
288 #define T_32PUSHORT 0x0421 /* 16:32 near pointer to unsigned short */
289 #define T_32PULONG 0x0422 /* 16:32 near pointer to unsigned int */
290 #define T_32PUQUAD 0x0423 /* 16:32 near pointer to long long */
291 #define T_32PREAL32 0x0440 /* 16:32 near pointer to float */
292 #define T_32PREAL64 0x0441 /* 16:32 near pointer to float */
293 #define T_32PRCHAR 0x0470 /* 16:32 near pointer to real char */
294 #define T_32PWCHAR 0x0471 /* 16:32 near pointer to real char */
295 #define T_32PINT4 0x0474 /* 16:32 near pointer to int */
296 #define T_32PUINT4 0x0475 /* 16:32 near pointer to unsigned int */
298 #define LF_MODIFIER 0x1
299 #define LF_POINTER 0x2
300 #define LF_ARRAY 0x3
301 #define LF_CLASS 0x4
302 #define LF_STRUCTURE 0x5
303 #define LF_UNION 0x6
304 #define LF_ENUMERATION 0x7
305 #define LF_PROCEDURE 0x8
306 #define LF_MFUNCTION 0x9
307 #define LF_VTSHAPE 0xa
308 #define LF_BARRAY 0xd
309 #define LF_DIMARRAY 0x11
310 #define LF_VFTPATH 0x12
312 #define LF_SKIP 0x200
313 #define LF_ARGLIST 0x201
314 #define LF_FIELDLIST 0x204
315 #define LF_DERIVED 0x205
316 #define LF_BITFIELD 0x206
318 #define LF_BCLASS 0x400
319 #define LF_VBCLASS 0x401
320 #define LF_IVBCLASS 0x402
321 #define LF_ENUMERATE 0x403
322 #define LF_FRIENDFCN 0x404
323 #define LF_INDEX 0x405
324 #define LF_MEMBER 0x406
325 #define LF_STMEMBER 0x407
326 #define LF_METHOD 0x408
327 #define LF_NESTEDTYPE 0x409
328 #define LF_VFUNCTAB 0x40a
329 #define LF_FRIENDCLS 0x40b
330 #define LF_ONEMETHOD 0x40c
331 #define LF_FUNCOFF 0x40d
333 #define MAX_BUILTIN_TYPES 0x480
334 static struct datatype * cv_basic_types[MAX_BUILTIN_TYPES];
335 static int num_cv_defined_types = 0;
336 static struct datatype **cv_defined_types = NULL;
339 * For the type CODEVIEW debug directory entries, the debug directory
340 * points to a structure like this. The cv_name field is the name
341 * of an external .PDB file.
343 struct CodeViewDebug
345 char cv_nbtype[8];
346 unsigned int cv_timestamp;
347 char cv_unknown[4];
348 char cv_name[1];
351 struct MiscDebug {
352 unsigned int DataType;
353 unsigned int Length;
354 char Unicode;
355 char Reserved[3];
356 char Data[1];
360 * This is the header that the COFF variety of debug header points to.
362 struct CoffDebug {
363 unsigned int N_Sym;
364 unsigned int SymbolOffset;
365 unsigned int N_Linenum;
366 unsigned int LinenumberOffset;
367 unsigned int Unused[4];
370 struct CoffLinenum {
371 unsigned int VirtualAddr;
372 unsigned short int Linenum;
375 struct CoffFiles {
376 unsigned int startaddr;
377 unsigned int endaddr;
378 char * filename;
379 int linetab_offset;
380 int linecnt;
381 struct name_hash **entries;
382 int neps;
383 int neps_alloc;
387 struct CoffSymbol {
388 union {
389 char ShortName[8];
390 struct {
391 unsigned int NotLong;
392 unsigned int StrTaboff;
393 } Name;
394 } N;
395 unsigned int Value;
396 short SectionNumber;
397 short Type;
398 char StorageClass;
399 unsigned char NumberOfAuxSymbols;
402 struct CoffAuxSection{
403 unsigned int Length;
404 unsigned short NumberOfRelocations;
405 unsigned short NumberOfLinenumbers;
406 unsigned int CheckSum;
407 short Number;
408 char Selection;
409 } Section;
412 * These two structures are used in the directory within a .DBG file
413 * to locate the individual important bits that we might want to see.
415 struct CV4_DirHead {
416 short unsigned int dhsize;
417 short unsigned int desize;
418 unsigned int ndir;
419 unsigned int next_offset;
420 unsigned int flags;
423 struct CV4_DirEnt {
424 short unsigned int subsect_number;
425 short unsigned int module_number;
426 unsigned int offset;
427 unsigned int size;
431 * These are the values of interest that the subsect_number field takes.
433 #define sstAlignSym 0x125
434 #define sstSrcModule 0x127
436 struct codeview_linetab_hdr
438 unsigned int nline;
439 unsigned int segno;
440 unsigned int start;
441 unsigned int end;
442 char * sourcefile;
443 unsigned short * linetab;
444 unsigned int * offtab;
447 struct codeview_pdb_hdr
449 char ident[44];
450 unsigned int blocksize; /* Extent size */
451 unsigned short loc_freelist; /* freelist. */
452 unsigned short alloc_filesize; /* # extents allocated. */
453 unsigned int toc_len;
454 unsigned int unknown;
455 unsigned short toc_ext[1]; /* array of extent #'s for toc. */
459 * This is our own structure that we use to keep track of the contents
460 * of a PDB file.
462 struct file_list
464 int record_len;
465 int nextents;
466 short int * extent_list;
467 unsigned int linetab_offset;
468 unsigned int linetab_len;
472 * These are the structures that represent how the file table is set up
473 * within the PDB file.
475 struct filetab_hdr
477 unsigned short tab1_file;
478 unsigned short tab2_file;
479 unsigned short gsym_file;
480 unsigned short padding;
481 unsigned int ftab_len;
482 unsigned int fofftab_len;
483 unsigned int hash_len;
484 unsigned int strtab_len;
487 struct file_ent
489 unsigned int reserved1;
490 unsigned short datasect_segment;
491 unsigned short reserved2;
492 unsigned int datasect_offset;
493 unsigned int datasect_size;
494 unsigned int datasect_flags;
495 unsigned short reserved3;
496 unsigned short index;
497 unsigned short num6a;
498 unsigned short file_number;
499 unsigned int linetab_offset;
500 unsigned int linetab_len;
501 unsigned int num9;
502 unsigned int num10;
503 unsigned int num11;
504 unsigned char filename[1];
508 ********************************************************************
510 struct deferred_debug_info
512 struct deferred_debug_info * next;
513 char * load_addr;
514 char * module_name;
515 char * dbg_info;
516 int dbg_size;
517 LPIMAGE_DEBUG_DIRECTORY dbgdir;
518 struct pe_data * pe;
519 LPIMAGE_SECTION_HEADER sectp;
520 int nsect;
521 short int dbg_index;
522 char loaded;
525 struct deferred_debug_info * dbglist = NULL;
528 * A simple macro that tells us whether a given COFF symbol is a
529 * function or not.
531 #define N_TMASK 0x0030
532 #define IMAGE_SYM_DTYPE_FUNCTION 2
533 #define N_BTSHFT 4
534 #define ISFCN(x) (((x) & N_TMASK) == (IMAGE_SYM_DTYPE_FUNCTION << N_BTSHFT))
538 * This is what we are looking for in the COFF symbols.
540 #define IMAGE_SYM_CLASS_EXTERNAL 0x2
541 #define IMAGE_SYM_CLASS_STATIC 0x3
542 #define IMAGE_SYM_CLASS_FILE 0x67
544 static
545 struct datatype * DEBUG_GetCVType(int typeno)
547 struct datatype * dt = NULL;
550 * Convert Codeview type numbers into something we can grok internally.
551 * Numbers < 0x1000 are all fixed builtin types. Numbers from 0x1000 and
552 * up are all user defined (structs, etc).
554 if( typeno < 0x1000 )
556 if( typeno < MAX_BUILTIN_TYPES )
558 dt = cv_basic_types[typeno];
561 else
563 if( typeno - 0x1000 < num_cv_defined_types )
565 dt = cv_defined_types[typeno - 0x1000];
569 return dt;
572 static int
573 DEBUG_ParseTypeTable(char * table, int len)
575 int arr_max;
576 int curr_type;
577 enum debug_type fieldtype;
578 int elem_size;
579 union any_size ptr;
580 union any_size ptr2;
581 struct datatype * subtype;
582 char symname[256];
583 union codeview_type * type;
584 union codeview_type * type2;
585 struct datatype * typeptr;
587 curr_type = 0x1000;
589 ptr = (union any_size) (table + 16);
590 while( ptr.c - table < len )
592 type = (union codeview_type *) ptr.c;
594 if( curr_type - 0x1000 >= num_cv_defined_types )
596 num_cv_defined_types += 0x100;
597 cv_defined_types = (struct datatype **) realloc(cv_defined_types,
598 num_cv_defined_types * sizeof(struct datatype *));
599 memset(cv_defined_types + num_cv_defined_types - 0x100,
601 0x100 * sizeof(struct datatype *));
602 if( cv_defined_types == NULL )
604 return FALSE;
608 switch(type->generic.id)
610 case LF_POINTER:
611 cv_defined_types[curr_type - 0x1000] =
612 DEBUG_FindOrMakePointerType(DEBUG_GetCVType(type->pointer.datatype));
613 break;
614 case LF_ARRAY:
615 if( type->array.arrlen >= 0x8000 )
618 * This is a numeric leaf, I am too lazy to handle this right
619 * now.
621 fprintf(stderr, "Ignoring large numberic leaf.\n");
622 break;
624 if( type->array.namelen != 0 )
626 memset(symname, 0, sizeof(symname));
627 memcpy(symname, type->array.name, type->array.namelen);
628 typeptr = DEBUG_NewDataType(ARRAY, symname);
630 else
632 typeptr = DEBUG_NewDataType(ARRAY, NULL);
634 cv_defined_types[curr_type - 0x1000] = typeptr;
636 subtype = DEBUG_GetCVType(type->array.elemtype);
637 if( (subtype == NULL)
638 || (elem_size = DEBUG_GetObjectSize(subtype)) == 0 )
640 arr_max = 0;
642 else
644 arr_max = type->array.arrlen / DEBUG_GetObjectSize(subtype);
647 DEBUG_SetArrayParams(typeptr, 0, arr_max, subtype);
648 break;
649 case LF_FIELDLIST:
651 * This is where the basic list of fields is defined for
652 * structures and classes.
654 * First, we need to look ahead and see whether we are building
655 * a fieldlist for an enum or a struct.
657 ptr2.i = ptr.i + 1;
658 type2 = (union codeview_type *) ptr2.c;
659 if( type2->member.id == LF_MEMBER )
661 typeptr = DEBUG_NewDataType(STRUCT, NULL);
662 fieldtype = STRUCT;
664 else if( type2->member.id == LF_ENUMERATE )
666 typeptr = DEBUG_NewDataType(ENUM, NULL);
667 fieldtype = ENUM;
669 else
671 break;
674 cv_defined_types[curr_type - 0x1000] = typeptr;
675 while( ptr2.c < (ptr.c + ((type->generic.len + 3) & ~3)) )
677 type2 = (union codeview_type *) ptr2.c;
678 if( type2->member.id == LF_MEMBER && fieldtype == STRUCT )
680 memset(symname, 0, sizeof(symname));
681 memcpy(symname, type2->member.name, type2->member.namelen);
683 subtype = DEBUG_GetCVType(type2->member.type);
684 elem_size = 0;
685 if( subtype != NULL )
687 elem_size = DEBUG_GetObjectSize(subtype);
690 if( type2->member.offset >= 0x8000 )
693 * This is a numeric leaf, I am too lazy to handle this right
694 * now.
696 fprintf(stderr, "Ignoring large numberic leaf.\n");
698 else
700 DEBUG_AddStructElement(typeptr, symname, subtype,
701 type2->member.offset << 3,
702 elem_size << 3);
705 else if( type2->member.id == LF_ENUMERATE && fieldtype == ENUM )
707 memset(symname, 0, sizeof(symname));
708 memcpy(symname, type2->enumerate.name, type2->enumerate.namelen);
710 if( type2->enumerate.value >= 0x8000 )
713 * This is a numeric leaf, I am too lazy to handle this right
714 * now.
716 fprintf(stderr, "Ignoring large numberic leaf.\n");
718 else
720 DEBUG_AddStructElement(typeptr, symname, NULL,
721 type2->enumerate.value, 0);
724 else
727 * Something else I have never seen before. Either wrong type of
728 * object in the fieldlist, or some other problem which I wouldn't
729 * really know how to handle until it came up.
731 fprintf(stderr, "Unexpected entry in fieldlist\n");
732 break;
736 ptr2.c += ((type2->member.namelen + 9 + 3) & ~3);
738 break;
739 case LF_STRUCTURE:
740 case LF_CLASS:
741 if( type->structure.structlen >= 0x8000 )
744 * This is a numeric leaf, I am too lazy to handle this right
745 * now.
747 fprintf(stderr, "Ignoring large numberic leaf.\n");
748 break;
750 memset(symname, 0, sizeof(symname));
751 memcpy(symname, type->structure.name, type->structure.namelen);
752 if( strcmp(symname, "__unnamed") == 0 )
754 typeptr = DEBUG_NewDataType(STRUCT, NULL);
756 else
758 typeptr = DEBUG_NewDataType(STRUCT, symname);
760 cv_defined_types[curr_type - 0x1000] = typeptr;
763 * Now copy the relevant bits from the fieldlist that we specified.
765 subtype = DEBUG_GetCVType(type->structure.fieldlist);
767 if( subtype != NULL )
769 DEBUG_SetStructSize(typeptr, type->structure.structlen);
770 DEBUG_CopyFieldlist(typeptr, subtype);
772 break;
773 case LF_UNION:
774 if( type->t_union.un_len >= 0x8000 )
777 * This is a numeric leaf, I am too lazy to handle this right
778 * now.
780 fprintf(stderr, "Ignoring large numberic leaf.\n");
781 break;
783 memset(symname, 0, sizeof(symname));
784 memcpy(symname, type->t_union.name, type->t_union.namelen);
786 if( strcmp(symname, "__unnamed") == 0 )
788 typeptr = DEBUG_NewDataType(STRUCT, NULL);
790 else
792 typeptr = DEBUG_NewDataType(STRUCT, symname);
795 cv_defined_types[curr_type - 0x1000] = typeptr;
798 * Now copy the relevant bits from the fieldlist that we specified.
800 subtype = DEBUG_GetCVType(type->t_union.field);
802 if( subtype != NULL )
804 DEBUG_SetStructSize(typeptr, type->t_union.un_len);
805 DEBUG_CopyFieldlist(typeptr, subtype);
807 break;
808 case LF_BITFIELD:
809 typeptr = DEBUG_NewDataType(BITFIELD, NULL);
810 cv_defined_types[curr_type - 0x1000] = typeptr;
811 DEBUG_SetBitfieldParams(typeptr, type->bitfield.bitoff,
812 type->bitfield.nbits,
813 DEBUG_GetCVType(type->bitfield.type));
814 break;
815 case LF_ENUMERATION:
816 memset(symname, 0, sizeof(symname));
817 memcpy(symname, type->enumeration.name, type->enumeration.namelen);
818 typeptr = DEBUG_NewDataType(ENUM, symname);
819 cv_defined_types[curr_type - 0x1000] = typeptr;
822 * Now copy the relevant bits from the fieldlist that we specified.
824 subtype = DEBUG_GetCVType(type->enumeration.field);
826 if( subtype != NULL )
828 DEBUG_CopyFieldlist(typeptr, subtype);
830 break;
831 case LF_DIMARRAY:
832 default:
833 break;
835 curr_type++;
836 ptr.c += (type->generic.len + 3) & ~3;
839 return TRUE;
842 void
843 DEBUG_InitCVDataTypes()
846 * These are the common builtin types that are used by VC++.
848 cv_basic_types[T_NOTYPE] = NULL;
849 cv_basic_types[T_ABS] = NULL;
850 cv_basic_types[T_VOID] = DEBUG_NewDataType(BASIC, "void");
851 cv_basic_types[T_CHAR] = DEBUG_NewDataType(BASIC, "char");
852 cv_basic_types[T_SHORT] = DEBUG_NewDataType(BASIC, "short int");
853 cv_basic_types[T_LONG] = DEBUG_NewDataType(BASIC, "long int");
854 cv_basic_types[T_QUAD] = DEBUG_NewDataType(BASIC, "long long int");
855 cv_basic_types[T_UCHAR] = DEBUG_NewDataType(BASIC, "unsigned char");
856 cv_basic_types[T_USHORT] = DEBUG_NewDataType(BASIC, "short unsigned int");
857 cv_basic_types[T_ULONG] = DEBUG_NewDataType(BASIC, "long unsigned int");
858 cv_basic_types[T_UQUAD] = DEBUG_NewDataType(BASIC, "long long unsigned int");
859 cv_basic_types[T_REAL32] = DEBUG_NewDataType(BASIC, "float");
860 cv_basic_types[T_REAL64] = DEBUG_NewDataType(BASIC, "double");
861 cv_basic_types[T_RCHAR] = DEBUG_NewDataType(BASIC, "char");
862 cv_basic_types[T_WCHAR] = DEBUG_NewDataType(BASIC, "short");
863 cv_basic_types[T_INT4] = DEBUG_NewDataType(BASIC, "int");
864 cv_basic_types[T_UINT4] = DEBUG_NewDataType(BASIC, "unsigned int");
866 cv_basic_types[T_32PVOID] = DEBUG_FindOrMakePointerType(cv_basic_types[T_VOID]);
867 cv_basic_types[T_32PCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_CHAR]);
868 cv_basic_types[T_32PSHORT] = DEBUG_FindOrMakePointerType(cv_basic_types[T_SHORT]);
869 cv_basic_types[T_32PLONG] = DEBUG_FindOrMakePointerType(cv_basic_types[T_LONG]);
870 cv_basic_types[T_32PQUAD] = DEBUG_FindOrMakePointerType(cv_basic_types[T_QUAD]);
871 cv_basic_types[T_32PUCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_UCHAR]);
872 cv_basic_types[T_32PUSHORT] = DEBUG_FindOrMakePointerType(cv_basic_types[T_USHORT]);
873 cv_basic_types[T_32PULONG] = DEBUG_FindOrMakePointerType(cv_basic_types[T_ULONG]);
874 cv_basic_types[T_32PUQUAD] = DEBUG_FindOrMakePointerType(cv_basic_types[T_UQUAD]);
875 cv_basic_types[T_32PREAL32] = DEBUG_FindOrMakePointerType(cv_basic_types[T_REAL32]);
876 cv_basic_types[T_32PREAL64] = DEBUG_FindOrMakePointerType(cv_basic_types[T_REAL64]);
877 cv_basic_types[T_32PRCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_RCHAR]);
878 cv_basic_types[T_32PWCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_WCHAR]);
879 cv_basic_types[T_32PINT4] = DEBUG_FindOrMakePointerType(cv_basic_types[T_INT4]);
880 cv_basic_types[T_32PUINT4] = DEBUG_FindOrMakePointerType(cv_basic_types[T_UINT4]);
884 * In this function, we keep track of deferred debugging information
885 * that we may need later if we were to need to use the internal debugger.
886 * We don't fully process it here for performance reasons.
889 DEBUG_RegisterDebugInfo(int fd, struct pe_data * pe,
890 int load_addr, u_long v_addr, u_long size)
892 int has_codeview = FALSE;
893 int rtn = FALSE;
894 int orig_size;
895 LPIMAGE_DEBUG_DIRECTORY dbgptr;
896 struct deferred_debug_info * deefer;
898 orig_size = size;
899 dbgptr = (LPIMAGE_DEBUG_DIRECTORY) (load_addr + v_addr);
900 for(; size >= sizeof(*dbgptr); size -= sizeof(*dbgptr), dbgptr++ )
902 switch(dbgptr->Type)
904 case IMAGE_DEBUG_TYPE_CODEVIEW:
905 case IMAGE_DEBUG_TYPE_MISC:
906 has_codeview = TRUE;
907 break;
911 size = orig_size;
912 dbgptr = (LPIMAGE_DEBUG_DIRECTORY) (load_addr + v_addr);
913 for(; size >= sizeof(*dbgptr); size -= sizeof(*dbgptr), dbgptr++ )
915 switch(dbgptr->Type)
917 case IMAGE_DEBUG_TYPE_COFF:
919 * If we have both codeview and COFF debug info, ignore the
920 * coff debug info as it would just confuse us, and it is
921 * less complete.
923 * FIXME - this is broken - if we cannot find the PDB file, then
924 * we end up with no debugging info at all. In this case, we
925 * should use the COFF info as a backup.
927 if( has_codeview )
929 break;
931 case IMAGE_DEBUG_TYPE_CODEVIEW:
932 case IMAGE_DEBUG_TYPE_MISC:
934 * This is usually an indirection to a .DBG file.
935 * This is similar to (but a slightly older format) from the
936 * PDB file.
938 * First check to see if the image was 'stripped'. If so, it
939 * means that this entry points to a .DBG file. Otherwise,
940 * it just points to itself, and we can ignore this.
942 if( (dbgptr->Type == IMAGE_DEBUG_TYPE_MISC)
943 && (pe->pe_header->FileHeader.Characteristics & IMAGE_FILE_DEBUG_STRIPPED) == 0 )
945 break;
948 deefer = (struct deferred_debug_info *) xmalloc(sizeof(*deefer));
949 deefer->pe = pe;
951 deefer->dbg_info = NULL;
952 deefer->dbg_size = 0;
955 * Read the important bits. What we do after this depends
956 * upon the type, but this is always enough so we are able
957 * to proceed if we know what we need to do next.
959 deefer->dbg_size = dbgptr->SizeOfData;
960 deefer->dbg_info = (char *) xmalloc(dbgptr->SizeOfData);
961 lseek(fd, dbgptr->PointerToRawData, SEEK_SET);
962 read(fd, deefer->dbg_info, deefer->dbg_size);
964 deefer->load_addr = (char *) load_addr;
965 deefer->dbgdir = dbgptr;
966 deefer->next = dbglist;
967 deefer->loaded = FALSE;
968 deefer->dbg_index = DEBUG_next_index;
969 deefer->module_name = xstrdup(DEBUG_curr_module);
971 deefer->sectp = pe->pe_seg;
972 deefer->nsect = pe->pe_header->FileHeader.NumberOfSections;
974 dbglist = deefer;
975 break;
976 default:
980 DEBUG_next_index++;
982 return (rtn);
987 * ELF modules are also entered into the list - this is so that we
988 * can make 'info shared' types of displays possible.
991 DEBUG_RegisterELFDebugInfo(int load_addr, u_long size, char * name)
993 struct deferred_debug_info * deefer;
995 deefer = (struct deferred_debug_info *) xmalloc(sizeof(*deefer));
996 deefer->pe = NULL;
999 * Read the important bits. What we do after this depends
1000 * upon the type, but this is always enough so we are able
1001 * to proceed if we know what we need to do next.
1003 deefer->dbg_size = size;
1004 deefer->dbg_info = (char *) NULL;
1006 deefer->load_addr = (char *) load_addr;
1007 deefer->dbgdir = NULL;
1008 deefer->next = dbglist;
1009 deefer->loaded = TRUE;
1010 deefer->dbg_index = DEBUG_next_index;
1011 deefer->module_name = xstrdup(name);
1012 dbglist = deefer;
1014 DEBUG_next_index++;
1016 return (TRUE);
1022 * Process COFF debugging information embedded in a Win32 application.
1025 static
1027 DEBUG_ProcessCoff(struct deferred_debug_info * deefer)
1029 struct CoffAuxSection * aux;
1030 struct CoffDebug * coff;
1031 struct CoffFiles * coff_files = NULL;
1032 struct CoffLinenum * coff_linetab;
1033 char * coff_strtab;
1034 struct CoffSymbol * coff_sym;
1035 struct CoffSymbol * coff_symbol;
1036 struct CoffFiles * curr_file = NULL;
1037 int i;
1038 int j;
1039 int k;
1040 struct CoffLinenum * linepnt;
1041 int linetab_indx;
1042 char namebuff[9];
1043 char * nampnt;
1044 int naux;
1045 DBG_ADDR new_addr;
1046 int nfiles = 0;
1047 int nfiles_alloc = 0;
1048 struct CoffFiles orig_file;
1049 int rtn = FALSE;
1050 char * this_file = NULL;
1052 coff = (struct CoffDebug *) deefer->dbg_info;
1054 coff_symbol = (struct CoffSymbol *) ((unsigned int) coff + coff->SymbolOffset);
1055 coff_linetab = (struct CoffLinenum *) ((unsigned int) coff + coff->LinenumberOffset);
1056 coff_strtab = (char *) ((unsigned int) coff_symbol + 18*coff->N_Sym);
1058 linetab_indx = 0;
1060 for(i=0; i < coff->N_Sym; i++ )
1063 * We do this because some compilers (i.e. gcc) incorrectly
1064 * pad the structure up to a 4 byte boundary. The structure
1065 * is really only 18 bytes long, so we have to manually make sure
1066 * we get it right.
1068 * FIXME - there must be a way to have autoconf figure out the
1069 * correct compiler option for this. If it is always gcc, that
1070 * makes life simpler, but I don't want to force this.
1072 coff_sym = (struct CoffSymbol *) ((unsigned int) coff_symbol + 18*i);
1073 naux = coff_sym->NumberOfAuxSymbols;
1075 if( coff_sym->StorageClass == IMAGE_SYM_CLASS_FILE )
1077 if( nfiles + 1 >= nfiles_alloc )
1079 nfiles_alloc += 10;
1080 coff_files = (struct CoffFiles *) realloc( coff_files,
1081 nfiles_alloc * sizeof(struct CoffFiles));
1083 curr_file = coff_files + nfiles;
1084 nfiles++;
1085 curr_file->startaddr = 0xffffffff;
1086 curr_file->endaddr = 0;
1087 curr_file->filename = ((char *) coff_sym) + 18;
1088 curr_file->linetab_offset = -1;
1089 curr_file->linecnt = 0;
1090 curr_file->entries = NULL;
1091 curr_file->neps = curr_file->neps_alloc = 0;
1092 #if 0
1093 fprintf(stderr,"New file %s\n", curr_file->filename);
1094 #endif
1095 i += naux;
1096 continue;
1100 * This guy marks the size and location of the text section
1101 * for the current file. We need to keep track of this so
1102 * we can figure out what file the different global functions
1103 * go with.
1105 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC)
1106 && (naux != 0)
1107 && (coff_sym->Type == 0)
1108 && (coff_sym->SectionNumber == 1) )
1110 aux = (struct CoffAuxSection *) ((unsigned int) coff_sym + 18);
1112 if( curr_file->linetab_offset != -1 )
1114 #if 0
1115 fprintf(stderr, "Duplicating sect from %s: %x %x %x %d %d\n",
1116 curr_file->filename,
1117 aux->Length,
1118 aux->NumberOfRelocations,
1119 aux->NumberOfLinenumbers,
1120 aux->Number,
1121 aux->Selection);
1122 fprintf(stderr, "More sect %d %x %d %d %d\n",
1123 coff_sym->SectionNumber,
1124 coff_sym->Value,
1125 coff_sym->Type,
1126 coff_sym->StorageClass,
1127 coff_sym->NumberOfAuxSymbols);
1128 #endif
1131 * Save this so we can copy bits from it.
1133 orig_file = *curr_file;
1136 * Duplicate the file entry. We have no way to describe
1137 * multiple text sections in our current way of handling things.
1139 if( nfiles + 1 >= nfiles_alloc )
1141 nfiles_alloc += 10;
1142 coff_files = (struct CoffFiles *) realloc( coff_files,
1143 nfiles_alloc * sizeof(struct CoffFiles));
1145 curr_file = coff_files + nfiles;
1146 nfiles++;
1147 curr_file->startaddr = 0xffffffff;
1148 curr_file->endaddr = 0;
1149 curr_file->filename = orig_file.filename;
1150 curr_file->linetab_offset = -1;
1151 curr_file->linecnt = 0;
1152 curr_file->entries = NULL;
1153 curr_file->neps = curr_file->neps_alloc = 0;
1155 #if 0
1156 else
1158 fprintf(stderr, "New text sect from %s: %x %x %x %d %d\n",
1159 curr_file->filename,
1160 aux->Length,
1161 aux->NumberOfRelocations,
1162 aux->NumberOfLinenumbers,
1163 aux->Number,
1164 aux->Selection);
1166 #endif
1168 if( curr_file->startaddr > coff_sym->Value )
1170 curr_file->startaddr = coff_sym->Value;
1173 if( curr_file->startaddr > coff_sym->Value )
1175 curr_file->startaddr = coff_sym->Value;
1178 if( curr_file->endaddr < coff_sym->Value + aux->Length )
1180 curr_file->endaddr = coff_sym->Value + aux->Length;
1183 curr_file->linetab_offset = linetab_indx;
1184 curr_file->linecnt = aux->NumberOfLinenumbers;
1185 linetab_indx += aux->NumberOfLinenumbers;
1186 i += naux;
1187 continue;
1190 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC)
1191 && (naux == 0)
1192 && (coff_sym->SectionNumber == 1) )
1195 * This is a normal static function when naux == 0.
1196 * Just register it. The current file is the correct
1197 * one in this instance.
1199 if( coff_sym->N.Name.NotLong )
1201 memcpy(namebuff, coff_sym->N.ShortName, 8);
1202 namebuff[8] = '\0';
1203 nampnt = &namebuff[0];
1205 else
1207 nampnt = coff_strtab + coff_sym->N.Name.StrTaboff;
1210 if( nampnt[0] == '_' )
1212 nampnt++;
1215 new_addr.seg = 0;
1216 new_addr.off = (int) (deefer->load_addr + coff_sym->Value);
1218 if( curr_file->neps + 1 >= curr_file->neps_alloc )
1220 curr_file->neps_alloc += 10;
1221 curr_file->entries = (struct name_hash **)
1222 realloc( curr_file->entries,
1223 curr_file->neps_alloc * sizeof(struct name_hash *));
1225 #if 0
1226 fprintf(stderr,"\tAdding static symbol %s\n", nampnt);
1227 #endif
1228 curr_file->entries[curr_file->neps++] =
1229 DEBUG_AddSymbol( nampnt, &new_addr, this_file, SYM_WIN32 );
1230 i += naux;
1231 continue;
1234 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL)
1235 && ISFCN(coff_sym->Type)
1236 && (coff_sym->SectionNumber > 0) )
1238 if( coff_sym->N.Name.NotLong )
1240 memcpy(namebuff, coff_sym->N.ShortName, 8);
1241 namebuff[8] = '\0';
1242 nampnt = &namebuff[0];
1244 else
1246 nampnt = coff_strtab + coff_sym->N.Name.StrTaboff;
1250 if( nampnt[0] == '_' )
1252 nampnt++;
1255 new_addr.seg = 0;
1256 new_addr.off = (int) (deefer->load_addr + coff_sym->Value);
1258 #if 0
1259 fprintf(stderr, "%d: %x %s\n", i, new_addr.off, nampnt);
1261 fprintf(stderr,"\tAdding global symbol %s\n", nampnt);
1262 #endif
1265 * Now we need to figure out which file this guy belongs to.
1267 this_file = NULL;
1268 for(j=0; j < nfiles; j++)
1270 if( coff_files[j].startaddr <= coff_sym->Value
1271 && coff_files[j].endaddr > coff_sym->Value )
1273 this_file = coff_files[j].filename;
1274 break;
1277 if( coff_files[j].neps + 1 >= coff_files[j].neps_alloc )
1279 coff_files[j].neps_alloc += 10;
1280 coff_files[j].entries = (struct name_hash **)
1281 realloc( coff_files[j].entries,
1282 coff_files[j].neps_alloc * sizeof(struct name_hash *));
1284 coff_files[j].entries[coff_files[j].neps++] =
1285 DEBUG_AddSymbol( nampnt, &new_addr, this_file, SYM_WIN32 );
1286 i += naux;
1287 continue;
1290 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL)
1291 && (coff_sym->SectionNumber > 0) )
1294 * Similar to above, but for the case of data symbols.
1295 * These aren't treated as entrypoints.
1297 if( coff_sym->N.Name.NotLong )
1299 memcpy(namebuff, coff_sym->N.ShortName, 8);
1300 namebuff[8] = '\0';
1301 nampnt = &namebuff[0];
1303 else
1305 nampnt = coff_strtab + coff_sym->N.Name.StrTaboff;
1309 if( nampnt[0] == '_' )
1311 nampnt++;
1314 new_addr.seg = 0;
1315 new_addr.off = (int) (deefer->load_addr + coff_sym->Value);
1317 #if 0
1318 fprintf(stderr, "%d: %x %s\n", i, new_addr.off, nampnt);
1320 fprintf(stderr,"\tAdding global data symbol %s\n", nampnt);
1321 #endif
1324 * Now we need to figure out which file this guy belongs to.
1326 DEBUG_AddSymbol( nampnt, &new_addr, NULL, SYM_WIN32 );
1327 i += naux;
1328 continue;
1331 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC)
1332 && (naux == 0) )
1335 * Ignore these. They don't have anything to do with
1336 * reality.
1338 i += naux;
1339 continue;
1342 #if 0
1343 fprintf(stderr,"Skipping unknown entry %d %d %d\n", coff_sym->StorageClass,
1344 coff_sym->SectionNumber, naux);
1345 #endif
1348 * For now, skip past the aux entries.
1350 i += naux;
1355 * OK, we now should have a list of files, and we should have a list
1356 * of entrypoints. We need to sort the entrypoints so that we are
1357 * able to tie the line numbers with the given functions within the
1358 * file.
1360 if( coff_files != NULL )
1362 for(j=0; j < nfiles; j++)
1364 if( coff_files[j].entries != NULL )
1366 qsort(coff_files[j].entries, coff_files[j].neps,
1367 sizeof(struct name_hash *), DEBUG_cmp_sym);
1372 * Now pick apart the line number tables, and attach the entries
1373 * to the given functions.
1375 for(j=0; j < nfiles; j++)
1377 i = 0;
1378 for(k=0; k < coff_files[j].linecnt; k++)
1381 * Another monstrosity caused by the fact that we are using
1382 * a 6 byte structure, and gcc wants to pad structures to 4 byte
1383 * boundaries. Otherwise we could just index into an array.
1385 linepnt = (struct CoffLinenum *)
1386 ((unsigned int) coff_linetab +
1387 6*(coff_files[j].linetab_offset + k));
1389 * If we have spilled onto the next entrypoint, then
1390 * bump the counter..
1392 while(TRUE)
1394 DEBUG_GetSymbolAddr(coff_files[j].entries[i+1], &new_addr);
1395 if( (i+1 < coff_files[j].neps)
1396 && ( ((unsigned int) deefer->load_addr + linepnt->VirtualAddr)
1397 >= new_addr.off) )
1399 i++;
1401 else
1403 break;
1408 * Add the line number. This is always relative to the
1409 * start of the function, so we need to subtract that offset
1410 * first.
1412 DEBUG_GetSymbolAddr(coff_files[j].entries[i], &new_addr);
1413 DEBUG_AddLineNumber(coff_files[j].entries[i],
1414 linepnt->Linenum,
1415 (unsigned int) deefer->load_addr
1416 + linepnt->VirtualAddr
1417 - new_addr.off);
1422 rtn = TRUE;
1424 if( coff_files != NULL )
1426 for(j=0; j < nfiles; j++)
1428 if( coff_files[j].entries != NULL )
1430 free(coff_files[j].entries);
1433 free(coff_files);
1436 return (rtn);
1441 * Process a codeview line number table. Digestify the thing so that
1442 * we can easily reference the thing when we process the rest of
1443 * the information.
1445 static struct codeview_linetab_hdr *
1446 DEBUG_SnarfLinetab(char * linetab,
1447 int size)
1449 int file_segcount;
1450 char filename[PATH_MAX];
1451 unsigned int * filetab;
1452 char * fn;
1453 int i;
1454 int k;
1455 struct codeview_linetab_hdr * lt_hdr;
1456 unsigned int * lt_ptr;
1457 int nfile;
1458 int nseg;
1459 union any_size pnt;
1460 union any_size pnt2;
1461 struct startend * start;
1462 int this_seg;
1465 * Now get the important bits.
1467 pnt = (union any_size) linetab;
1468 nfile = *pnt.s++;
1469 nseg = *pnt.s++;
1471 filetab = (unsigned int *) pnt.c;
1474 * Now count up the number of segments in the file.
1476 nseg = 0;
1477 for(i=0; i<nfile; i++)
1479 pnt2 = (union any_size) (linetab + filetab[i]);
1480 nseg += *pnt2.s;
1484 * Next allocate the header we will be returning.
1485 * There is one header for each segment, so that we can reach in
1486 * and pull bits as required.
1488 lt_hdr = (struct codeview_linetab_hdr *)
1489 xmalloc((nseg + 1) * sizeof(*lt_hdr));
1490 if( lt_hdr == NULL )
1492 goto leave;
1495 memset(lt_hdr, 0, sizeof(*lt_hdr) * (nseg+1));
1498 * Now fill the header we will be returning, one for each segment.
1499 * Note that this will basically just contain pointers into the existing
1500 * line table, and we do not actually copy any additional information
1501 * or allocate any additional memory.
1504 this_seg = 0;
1505 for(i=0; i<nfile; i++)
1508 * Get the pointer into the segment information.
1510 pnt2 = (union any_size) (linetab + filetab[i]);
1511 file_segcount = *pnt2.s;
1513 pnt2.ui++;
1514 lt_ptr = (unsigned int *) pnt2.c;
1515 start = (struct startend *) (lt_ptr + file_segcount);
1518 * Now snarf the filename for all of the segments for this file.
1520 fn = (unsigned char *) (start + file_segcount);
1521 memset(filename, 0, sizeof(filename));
1522 memcpy(filename, fn + 1, *fn);
1523 fn = strdup(filename);
1525 for(k = 0; k < file_segcount; k++, this_seg++)
1527 pnt2 = (union any_size) (linetab + lt_ptr[k]);
1528 lt_hdr[this_seg].start = start[k].start;
1529 lt_hdr[this_seg].end = start[k].end;
1530 lt_hdr[this_seg].sourcefile = fn;
1531 lt_hdr[this_seg].segno = *pnt2.s++;
1532 lt_hdr[this_seg].nline = *pnt2.s++;
1533 lt_hdr[this_seg].offtab = pnt2.ui;
1534 lt_hdr[this_seg].linetab = (unsigned short *)
1535 (pnt2.ui + lt_hdr[this_seg].nline);
1539 leave:
1541 return lt_hdr;
1545 static int
1546 DEBUG_SnarfCodeView( struct deferred_debug_info * deefer,
1547 char * cv_data,
1548 int size,
1549 struct codeview_linetab_hdr * linetab)
1551 struct name_hash * curr_func = NULL;
1552 struct wine_locals * curr_sym = NULL;
1553 int i;
1554 int j;
1555 int len;
1556 DBG_ADDR new_addr;
1557 int nsect;
1558 union any_size ptr;
1559 IMAGE_SECTION_HEADER * sectp;
1560 union codeview_symbol * sym;
1561 char symname[PATH_MAX];
1562 struct name_hash * thunk_sym = NULL;
1564 ptr = (union any_size) cv_data;
1565 nsect = deefer->nsect;
1566 sectp = deefer->sectp;
1569 * Skip over the first word. Don't really know what it means, but
1570 * it is useless.
1572 ptr.ui++;
1575 * Loop over the different types of records and whenever we
1576 * find something we are interested in, record it and move on.
1578 while( ptr.c - cv_data < size )
1580 sym = (union codeview_symbol *) ptr.c;
1582 if( sym->generic.len - sizeof(int) == (ptr.c - cv_data) )
1585 * This happens when we have indirect symbols that VC++ 4.2
1586 * sometimes uses when there isn't a line number table.
1587 * We ignore it - we will process and enter all of the
1588 * symbols in the global symbol table anyways, so there
1589 * isn't much point in keeping track of all of this crap.
1591 break;
1594 memset(symname, 0, sizeof(symname));
1595 switch(sym->generic.id)
1597 case S_GDATA32:
1598 case S_LDATA32:
1599 case S_PUB32:
1601 * First, a couple of sanity checks.
1603 if( sym->data.namelen == 0 )
1605 break;
1608 if( sym->data.seg == 0 || sym->data.seg > nsect )
1610 break;
1614 * Global and local data symbols. We don't associate these
1615 * with any given source file.
1618 memcpy(symname, sym->data.name, sym->data.namelen);
1619 new_addr.seg = 0;
1620 new_addr.type = DEBUG_GetCVType(sym->data.symtype);
1621 new_addr.off = (unsigned int) deefer->load_addr +
1622 sectp[sym->data.seg - 1].VirtualAddress +
1623 sym->data.offset;
1624 DEBUG_AddSymbol( symname, &new_addr, NULL, SYM_WIN32 | SYM_DATA );
1625 break;
1626 case S_THUNK32:
1628 * Sort of like a global function, but it just points
1629 * to a thunk, which is a stupid name for what amounts to
1630 * a PLT slot in the normal jargon that everyone else uses.
1632 memcpy(symname, sym->thunk.name, sym->thunk.namelen);
1633 new_addr.seg = 0;
1634 new_addr.type = NULL;
1635 new_addr.off = (unsigned int) deefer->load_addr +
1636 sectp[sym->thunk.segment - 1].VirtualAddress +
1637 sym->thunk.offset;
1638 thunk_sym = DEBUG_AddSymbol( symname, &new_addr, NULL,
1639 SYM_WIN32 | SYM_FUNC);
1640 DEBUG_SetSymbolSize(thunk_sym, sym->thunk.thunk_len);
1641 break;
1642 case S_GPROC32:
1643 case S_LPROC32:
1645 * Global and static functions.
1647 memcpy(symname, sym->proc.name, sym->proc.namelen);
1648 new_addr.seg = 0;
1649 new_addr.type = DEBUG_GetCVType(sym->proc.proctype);
1650 new_addr.off = (unsigned int) deefer->load_addr +
1651 sectp[sym->proc.segment - 1].VirtualAddress +
1652 sym->proc.offset;
1654 * See if we can find a segment that this goes with. If so,
1655 * it means that we also may have line number information
1656 * for this function.
1658 for(i=0; linetab[i].linetab != NULL; i++)
1660 if( ((unsigned int) deefer->load_addr
1661 + sectp[linetab[i].segno - 1].VirtualAddress
1662 + linetab[i].start <= new_addr.off)
1663 && ((unsigned int) deefer->load_addr
1664 + sectp[linetab[i].segno - 1].VirtualAddress
1665 + linetab[i].end > new_addr.off) )
1667 break;
1671 DEBUG_Normalize(curr_func);
1672 if( linetab[i].linetab == NULL )
1674 curr_func = DEBUG_AddSymbol( symname, &new_addr, NULL,
1675 SYM_WIN32 | SYM_FUNC);
1677 else
1680 * First, create the entry. Then dig through the linetab
1681 * and add whatever line numbers are appropriate for this
1682 * function.
1684 curr_func = DEBUG_AddSymbol( symname, &new_addr,
1685 linetab[i].sourcefile,
1686 SYM_WIN32 | SYM_FUNC);
1687 for(j=0; j < linetab[i].nline; j++)
1689 if( linetab[i].offtab[j] >= sym->proc.offset
1690 && linetab[i].offtab[j] < sym->proc.offset
1691 + sym->proc.proc_len )
1693 DEBUG_AddLineNumber(curr_func, linetab[i].linetab[j],
1694 linetab[i].offtab[j] - sym->proc.offset);
1701 * Add information about where we should set breakpoints
1702 * in this function.
1704 DEBUG_SetSymbolBPOff(curr_func, sym->proc.debug_start);
1705 DEBUG_SetSymbolSize(curr_func, sym->proc.proc_len);
1706 break;
1707 case S_BPREL32:
1709 * Function parameters and stack variables.
1711 memcpy(symname, sym->stack.name, sym->stack.namelen);
1712 curr_sym = DEBUG_AddLocal(curr_func,
1714 sym->stack.offset,
1717 symname);
1718 DEBUG_SetLocalSymbolType(curr_sym, DEBUG_GetCVType(sym->stack.symtype));
1720 break;
1721 default:
1722 break;
1726 * Adjust pointer to point to next entry, rounding up to a word
1727 * boundary. MS preserving alignment? Stranger things have
1728 * happened.
1730 if( sym->generic.id == S_PROCREF
1731 || sym->generic.id == S_DATAREF
1732 || sym->generic.id == S_UNKNOWN )
1734 len = (sym->generic.len + 3) & ~3;
1735 len += ptr.c[16] + 1;
1736 ptr.c += (len + 3) & ~3;
1738 else
1740 ptr.c += (sym->generic.len + 3) & ~3;
1744 if( linetab != NULL )
1746 free(linetab);
1749 return TRUE;
1754 * Process PDB file which contains debug information.
1756 * These are really weird beasts. They are intended to be incrementally
1757 * updated by the incremental linker, and this means that you need to
1758 * be able to remove and add information. Thus the PDB file is sort of
1759 * like a block structured device, with a freelist and lists of extent numbers
1760 * that are used to get the relevant pieces. In all cases seen so far, the
1761 * blocksize is always 0x400 bytes. The header has a field which apparently
1762 * holds the blocksize, so if it ever changes we are safe.
1764 * In general, every time we need to extract something from the pdb file,
1765 * it is easier to copy it into another buffer so we have the information
1766 * in one contiguous block rather than attempt to try and keep track of when
1767 * we need to grab another extent from the pdb file.
1769 * The thing that is a real pain about some MS stuff is that they choose
1770 * data structures which are not representable in C. Thus we have to
1771 * hack around and diddle pointers.
1773 /* static */
1775 DEBUG_ProcessPDBFile(struct deferred_debug_info * deefer, char * full_filename)
1777 char * addr = (char *) 0xffffffff;
1778 unsigned int blocksize;
1779 unsigned int bufflen = 0;
1780 char * buffer = NULL;
1781 unsigned short * extent_table;
1782 int fd = -1;
1783 struct file_ent * fent;
1784 char * filename;
1785 struct file_list * filelist = NULL;
1786 unsigned int gsym_record = 0;
1787 char * gsymtab = NULL;
1788 struct filetab_hdr * hd;
1789 int i;
1790 int j;
1791 unsigned int last_extent;
1792 struct codeview_linetab_hdr * linetab;
1793 unsigned int nblocks;
1794 unsigned int npair;
1795 unsigned int offset;
1796 struct codeview_pdb_hdr * pdbhdr;
1797 unsigned int * pnt;
1798 struct stat statbuf;
1799 int status;
1800 unsigned short * table;
1801 char * toc;
1802 unsigned int toc_blocks;
1805 * FIXME - we should use some kind of search path mechanism to locate
1806 * PDB files. Right now we just look in the current working directory,
1807 * which works much of the time, I guess. Ideally we should be able to
1808 * map the filename back using the settings in wine.ini and perhaps
1809 * we could find it there. This bit of coding is left as an exercise
1810 * for the reader. :-).
1812 filename = strrchr(full_filename, '\\');
1813 if( filename == NULL )
1815 filename = full_filename;
1817 else
1819 filename++;
1822 status = stat(filename, &statbuf);
1823 if( status == -1 )
1825 fprintf(stderr, "Unable to open .PDB file %s\n", filename);
1826 goto leave;
1830 * Now open the file, so that we can mmap() it.
1832 fd = open(filename, O_RDONLY);
1833 if( fd == -1 )
1835 fprintf(stderr, "Unable to open .DBG file %s\n", filename);
1836 goto leave;
1841 * Now mmap() the file.
1843 addr = mmap(0, statbuf.st_size, PROT_READ,
1844 MAP_PRIVATE, fd, 0);
1845 if( addr == (char *) 0xffffffff )
1847 fprintf(stderr, "Unable to mmap .DBG file %s\n", filename);
1848 goto leave;
1852 * Now that we have the formalities over and done with, we need
1853 * to find the table of contents for the PDB file.
1855 pdbhdr = (struct codeview_pdb_hdr *) addr;
1856 blocksize = pdbhdr->blocksize;
1857 last_extent = (statbuf.st_size + blocksize - 1) / blocksize;
1860 * The TOC itself isn't always contiguous, so we need to extract a few
1861 * extents from the file to form the TOC.
1863 toc_blocks = (pdbhdr->toc_len + blocksize - 1) / blocksize;
1864 toc = (char *) xmalloc(toc_blocks * blocksize);
1865 table = pdbhdr->toc_ext;
1866 for(i=0; i < toc_blocks; i++)
1868 memcpy(toc + blocksize*i, addr + table[i]*blocksize, blocksize);
1872 * Next build our own table which will have the size and extent block
1873 * list for each record in the PDB file.
1875 * The TOC starts out with the number of files. Then it is followed by
1876 * (npair * 2*sizeof(int)) bytes of information, which are pairs of ints.
1877 * The first one is the size of the record (in bytes), and the second one
1878 * is something else which I haven't figured out yet.
1880 pnt = (unsigned int *) toc;
1881 npair = *pnt++;
1882 extent_table = (unsigned short *) ((unsigned int) toc +
1883 npair * 2 * sizeof(int) + sizeof(int));
1886 * Sanity check.
1888 if( sizeof(int) + 2*sizeof(int)*npair > pdbhdr->toc_len )
1890 goto leave;
1893 filelist = (struct file_list *) xmalloc(npair * sizeof(*filelist));
1894 if( filelist == NULL )
1896 goto leave;
1898 memset(filelist, 0, npair * sizeof(*filelist));
1900 nblocks = 0;
1901 for(i=0; i < npair; i++)
1903 filelist[i].record_len = pnt[i*2];
1904 filelist[i].nextents = (filelist[i].record_len + blocksize - 1)
1905 / blocksize;
1906 filelist[i].extent_list = extent_table + nblocks;
1907 nblocks += filelist[i].nextents;
1910 * These get filled in later when we parse one of the records.
1912 filelist[i].linetab_offset = 0;
1913 filelist[i].linetab_len = 0;
1917 * OK, now walk through the various records and pick out the bits we
1918 * really want to see. Some of the records are extra special, and
1919 * we need to handle these a little bit differently.
1921 for(i=0; i < npair; i++)
1923 if( filelist[i].record_len == 0xffffffff )
1925 continue;
1929 * Make sure our buffer is large enough to hold the record.
1931 if( bufflen < filelist[i].nextents * blocksize )
1933 bufflen = filelist[i].nextents * blocksize;
1934 buffer = (char *) realloc(buffer, bufflen);
1938 * Do this just for completeness. It makes debugging easier
1939 * if we have a clean indication of where the record ends.
1941 memset(buffer, 0, filelist[i].nextents * blocksize);
1944 * Next, build the record using the extent list.
1946 for(j=0; j < filelist[i].nextents; j++)
1948 memcpy(buffer + j * blocksize,
1949 addr + filelist[i].extent_list[j] * blocksize,
1950 blocksize);
1953 pnt = (unsigned int *) buffer;
1956 * OK, now figure out what to do with it.
1960 * Always ignore the first entry. It seems to contain a backup copy
1961 * of the TOC (the last time the file was modified??)
1963 if( i == 0 )
1965 continue;
1969 * The second entry as a id block. It contains a magic number
1970 * to identify the compiler, plus it also contains the timestamp
1971 * which must match the timestamp in the executable.
1973 if( i == 1 )
1976 if( ((*pnt != 19950623) && (*pnt != 19950814))
1977 || (filelist[i].record_len != 0x24)
1978 || (pnt[1] != ((struct CodeViewDebug *)(deefer->dbg_info))->cv_timestamp) )
1980 goto leave;
1985 * The third entry contains pointers to the global symbol table,
1986 * plus it also contains additional information about each record
1987 * in the PDB file.
1989 if( i == 3 )
1991 hd = (struct filetab_hdr *) buffer;
1993 gsym_record = hd->gsym_file;
1994 gsymtab = (char *) xmalloc( filelist[gsym_record].nextents
1995 * blocksize);
1996 memset(gsymtab, 0, filelist[gsym_record].nextents * blocksize);
1998 for(j=0; j < filelist[gsym_record].nextents; j++)
2000 memcpy(gsymtab + j * blocksize,
2001 addr + filelist[gsym_record].extent_list[j] * blocksize,
2002 blocksize);
2006 * This record also contains information about where in the
2007 * remaining records we will be able to find the start of the
2008 * line number table. We could locate that bit using heuristics,
2009 * but since we have the info handy, we might as well use it.
2011 offset = sizeof(*hd);
2012 while(1==1)
2014 fent = (struct file_ent *) (buffer + offset);
2015 if( offset > hd->ftab_len )
2017 break;
2020 if( fent->file_number == 0 || fent->file_number >= npair )
2022 break;
2025 filelist[fent->file_number].linetab_offset =
2026 fent->linetab_offset;
2027 filelist[fent->file_number].linetab_len =
2028 fent->linetab_len;
2030 * Figure out the offset of the next entry.
2031 * There is a fixed part of the record and a variable
2032 * length filename which we must also skip past.
2034 offset += ((unsigned int) &fent->filename - (unsigned int) fent)
2035 + strlen(fent->filename) + 1;
2036 offset += strlen(buffer+offset) + 1;
2037 offset = (offset + 3) & ~3;
2043 * Two different magic numbers used as dates.
2044 * These indicate the 'type' table.
2046 if( *pnt == 19950410
2047 || *pnt == 19951122 )
2049 DEBUG_ParseTypeTable(buffer, filelist[i].record_len);
2050 continue;
2054 * This is something we really want to look at, since it contains
2055 * real debug info. Anything that doesn't match this can be
2056 * ignored for now.
2058 if( *pnt == 1 )
2061 * First, snag the line table, if we have one. This always
2062 * occurs at the end of the record, so we take the linetab
2063 * offset as the end of the normal part of the record.
2065 linetab = NULL;
2066 if( filelist[i].linetab_len != 0 )
2068 linetab = DEBUG_SnarfLinetab(buffer + filelist[i].linetab_offset,
2069 filelist[i].linetab_len);
2070 DEBUG_SnarfCodeView(deefer, buffer,
2071 filelist[i].linetab_offset,
2072 linetab);
2074 else
2076 DEBUG_SnarfCodeView(deefer, buffer,
2077 filelist[i].record_len,
2078 linetab);
2080 continue;
2085 * Finally, process the global symbol table itself. There isn't
2086 * a line number component to this, so we just toss everything
2087 * into the mix and it all should work out.
2089 if( gsym_record != 0 )
2091 DEBUG_SnarfCodeView(deefer, gsymtab - sizeof(int),
2092 filelist[gsym_record].record_len,
2093 NULL);
2096 leave:
2098 if( gsymtab != NULL )
2100 free(gsymtab);
2101 gsymtab = NULL;
2104 if( buffer != NULL )
2106 free(buffer);
2109 if( filelist != NULL )
2111 free(filelist);
2114 if( addr != (char *) 0xffffffff )
2116 munmap(addr, statbuf.st_size);
2119 if( fd != -1 )
2121 close(fd);
2124 return TRUE;
2128 * Process DBG file which contains debug information.
2130 /* static */
2132 DEBUG_ProcessDBGFile(struct deferred_debug_info * deefer, char * filename)
2134 char * addr = (char *) 0xffffffff;
2135 char * codeview;
2136 struct CV4_DirHead * codeview_dir;
2137 struct CV4_DirEnt * codeview_dent;
2138 struct PE_Debug_dir * dbghdr;
2139 struct deferred_debug_info deefer2;
2140 int fd = -1;
2141 int i;
2142 int j;
2143 struct codeview_linetab_hdr * linetab;
2144 int nsect;
2145 LPIMAGE_SEPARATE_DEBUG_HEADER pdbg = NULL;
2146 IMAGE_SECTION_HEADER * sectp;
2147 struct stat statbuf;
2148 int status;
2150 status = stat(filename, &statbuf);
2151 if( status == -1 )
2153 fprintf(stderr, "Unable to open .DBG file %s\n", filename);
2154 goto leave;
2158 * Now open the file, so that we can mmap() it.
2160 fd = open(filename, O_RDONLY);
2161 if( fd == -1 )
2163 fprintf(stderr, "Unable to open .DBG file %s\n", filename);
2164 goto leave;
2169 * Now mmap() the file.
2171 addr = mmap(0, statbuf.st_size, PROT_READ,
2172 MAP_PRIVATE, fd, 0);
2173 if( addr == (char *) 0xffffffff )
2175 fprintf(stderr, "Unable to mmap .DBG file %s\n", filename);
2176 goto leave;
2179 pdbg = (LPIMAGE_SEPARATE_DEBUG_HEADER) addr;
2181 if( pdbg->TimeDateStamp != deefer->dbgdir->TimeDateStamp )
2183 fprintf(stderr, "Warning - %s has incorrect internal timestamp\n",
2184 filename);
2185 goto leave;
2188 fprintf(stderr, "Processing symbols from %s...\n", filename);
2190 dbghdr = (struct PE_Debug_dir *) ( addr + sizeof(*pdbg)
2191 + pdbg->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)
2192 + pdbg->ExportedNamesSize);
2194 sectp = (LPIMAGE_SECTION_HEADER) ((char *) pdbg + sizeof(*pdbg));
2195 nsect = pdbg->NumberOfSections;
2197 for( i=0; i < pdbg->DebugDirectorySize / sizeof(*pdbg); i++, dbghdr++ )
2199 switch(dbghdr->type)
2201 case IMAGE_DEBUG_TYPE_COFF:
2203 * Dummy up a deferred debug header to handle the
2204 * COFF stuff embedded within the DBG file.
2206 memset((char *) &deefer2, 0, sizeof(deefer2));
2207 deefer2.dbg_info = (addr + dbghdr->dbgoff);
2208 deefer2.dbg_size = dbghdr->dbgsize;
2209 deefer2.load_addr = deefer->load_addr;
2211 DEBUG_ProcessCoff(&deefer2);
2212 break;
2213 case IMAGE_DEBUG_TYPE_CODEVIEW:
2215 * This is the older format by which codeview stuff is
2216 * stored, known as the 'NB09' format. Newer executables
2217 * and dlls created by VC++ use PDB files instead, which
2218 * have lots of internal similarities, but the overall
2219 * format and structure is quite different.
2221 codeview = (addr + dbghdr->dbgoff);
2224 * The first thing in the codeview section should be
2225 * an 'NB09' identifier. As a sanity check, make sure
2226 * it is there.
2228 if( *((unsigned int*) codeview) != 0x3930424e )
2230 break;
2234 * Next we need to find the directory. This is easy too.
2236 codeview_dir = (struct CV4_DirHead *)
2237 (codeview + ((unsigned int*) codeview)[1]);
2240 * Some more sanity checks. Make sure that everything
2241 * is as we expect it.
2243 if( codeview_dir->next_offset != 0
2244 || codeview_dir->dhsize != sizeof(*codeview_dir)
2245 || codeview_dir->desize != sizeof(*codeview_dent) )
2247 break;
2249 codeview_dent = (struct CV4_DirEnt *) (codeview_dir + 1);
2251 for(j=0; j < codeview_dir->ndir; j++, codeview_dent++)
2253 if( codeview_dent->subsect_number == sstAlignSym )
2256 * Check the previous entry. If it is a
2257 * sstSrcModule, it contains the line number
2258 * info for this file.
2260 linetab = NULL;
2261 if( codeview_dent[1].module_number == codeview_dent[0].module_number
2262 && codeview_dent[1].subsect_number == sstSrcModule )
2264 linetab = DEBUG_SnarfLinetab(
2265 codeview + codeview_dent[1].offset,
2266 codeview_dent[1].size);
2269 if( codeview_dent[-1].module_number == codeview_dent[0].module_number
2270 && codeview_dent[-1].subsect_number == sstSrcModule )
2272 linetab = DEBUG_SnarfLinetab(
2273 codeview + codeview_dent[-1].offset,
2274 codeview_dent[-1].size);
2277 * Now process the CV stuff.
2279 DEBUG_SnarfCodeView(deefer,
2280 codeview + codeview_dent->offset,
2281 codeview_dent->size,
2282 linetab);
2286 break;
2287 default:
2288 break;
2291 leave:
2293 if( addr != (char *) 0xffffffff )
2295 munmap(addr, statbuf.st_size);
2298 if( fd != -1 )
2300 close(fd);
2303 return TRUE;
2307 DEBUG_ProcessDeferredDebug()
2309 struct deferred_debug_info * deefer;
2310 struct CodeViewDebug * cvd;
2311 struct MiscDebug * misc;
2312 char * filename;
2313 int last_proc = -1;
2315 DEBUG_InitCVDataTypes();
2317 for(deefer = dbglist; deefer; deefer = deefer->next)
2319 if( deefer->loaded )
2321 continue;
2324 if( last_proc != deefer->dbg_index )
2326 fprintf(stderr, " %s",deefer->module_name);
2327 last_proc = deefer->dbg_index;
2330 switch(deefer->dbgdir->Type)
2332 case IMAGE_DEBUG_TYPE_COFF:
2334 * Standard COFF debug information that VC++ adds when you
2335 * use /debugtype:both with the linker.
2337 #if 0
2338 fprintf(stderr, "Processing COFF symbols...\n");
2339 #endif
2340 DEBUG_ProcessCoff(deefer);
2341 break;
2342 case IMAGE_DEBUG_TYPE_CODEVIEW:
2344 * This is a pointer to a PDB file of some sort.
2346 cvd = (struct CodeViewDebug *) deefer->dbg_info;
2348 if( strcmp(cvd->cv_nbtype, "NB10") != 0 )
2351 * Whatever this is, we don't know how to deal with
2352 * it yet.
2354 break;
2356 DEBUG_ProcessPDBFile(deefer, cvd->cv_name);
2357 #if 0
2358 fprintf(stderr, "Processing PDB file %s\n", cvd->cv_name);
2359 #endif
2360 break;
2361 case IMAGE_DEBUG_TYPE_MISC:
2363 * A pointer to a .DBG file of some sort. These files
2364 * can contain either CV4 or COFF information. Open
2365 * the file, and try to do the right thing with it.
2367 misc = (struct MiscDebug *) deefer->dbg_info;
2369 filename = strrchr((char *) &misc->Data, '.');
2372 * Ignore the file if it doesn't have a .DBG extension.
2374 if( (filename == NULL)
2375 || ( (strcmp(filename, ".dbg") != 0)
2376 && (strcmp(filename, ".DBG") != 0)) )
2378 break;
2381 filename = (char *) &misc->Data;
2384 * Do the dirty deed...
2386 DEBUG_ProcessDBGFile(deefer, filename);
2388 break;
2389 default:
2391 * We should never get here...
2393 break;
2396 return TRUE;
2400 /***********************************************************************
2401 * DEBUG_InfoShare
2403 * Display shared libarary information.
2405 void DEBUG_InfoShare(void)
2407 struct deferred_debug_info * deefer;
2409 fprintf(stderr,"Address\t\tModule\tName\n");
2411 for(deefer = dbglist; deefer; deefer = deefer->next)
2413 if( deefer->pe == NULL )
2415 fprintf(stderr,"0x%8.8x\t(ELF)\t%s\n",
2416 (unsigned int) deefer->load_addr,
2417 deefer->module_name);
2419 else
2421 fprintf(stderr,"0x%8.8x\t(Win32)\t%s\n",
2422 (unsigned int) deefer->load_addr,
2423 deefer->module_name);