Use GetProcAddress for mmio* functions to avoid importing winmm.dll.
[wine.git] / debugger / msc.c
blob660181e3a5477512c3da133cd006a939eba33cb2
1 /*
2 * File msc.c - read VC++ debug information from COFF and eventually
3 * from PDB files.
5 * Copyright (C) 1996, Eric Youngdale.
6 * Copyright (C) 1999, Ulrich Weigand.
8 * Note - this handles reading debug information for 32 bit applications
9 * that run under Windows-NT for example. I doubt that this would work well
10 * for 16 bit applications, but I don't think it really matters since the
11 * file format is different, and we should never get in here in such cases.
13 * TODO:
14 * Get 16 bit CV stuff working.
15 * Add symbol size to internal symbol table.
18 #include "config.h"
19 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #ifndef PATH_MAX
24 #define PATH_MAX _MAX_PATH
25 #endif
26 #include "debugger.h"
27 #include "neexe.h"
28 #include "pe_image.h"
29 #include "file.h"
31 typedef struct {
32 IMAGE_DEBUG_DIRECTORY dbgdir;
33 u_long sect_ofs;
34 int nsect;
35 char* dbg_info;
36 int dbg_size;
37 } MSC_DBG_INFO;
39 #define MSC_INFO(module) ((MSC_DBG_INFO*)((module)->extra_info))
41 static int DEBUG_ProcessMSCDebugInfo(DBG_MODULE* module);
44 * dbg_filename must be at least MAX_PATHNAME_LEN bytes in size
46 static void DEBUG_LocateDebugInfoFile(const char *filename, char *dbg_filename)
48 char *str1 = DBG_alloc(MAX_PATHNAME_LEN);
49 char *str2 = DBG_alloc(MAX_PATHNAME_LEN*10);
50 const char *file;
51 char *name_part;
53 file = strrchr(filename, '\\');
54 if( file == NULL ) file = filename; else file++;
56 if ((GetEnvironmentVariable("_NT_SYMBOL_PATH", str1, MAX_PATHNAME_LEN) &&
57 (SearchPath(str1, file, NULL, MAX_PATHNAME_LEN*10, str2, &name_part))) ||
58 (GetEnvironmentVariable("_NT_ALT_SYMBOL_PATH", str1, MAX_PATHNAME_LEN) &&
59 (SearchPath(str1, file, NULL, MAX_PATHNAME_LEN*10, str2, &name_part))) ||
60 (SearchPath(NULL, file, NULL, MAX_PATHNAME_LEN*10, str2, &name_part)))
61 lstrcpyn(dbg_filename, str2, MAX_PATHNAME_LEN);
62 else
63 lstrcpyn(dbg_filename, filename, MAX_PATHNAME_LEN);
64 DBG_free(str1);
65 DBG_free(str2);
68 /***********************************************************************
69 * DEBUG_MapDebugInfoFile
71 static void* DEBUG_MapDebugInfoFile(const char* name, DWORD offset, DWORD size,
72 HANDLE* hFile, HANDLE* hMap)
74 OFSTRUCT ofs;
75 DWORD g_offset; /* offset aligned on map granuality */
76 DWORD g_size; /* size to map, with offset aligned */
77 char* ret;
79 *hMap = 0;
81 if (name != NULL) {
82 char filename[MAX_PATHNAME_LEN];
84 DEBUG_LocateDebugInfoFile(name, filename);
85 if ((*hFile = OpenFile(filename, &ofs, OF_READ)) == HFILE_ERROR)
86 return NULL;
89 if (!size) {
90 DWORD file_size = GetFileSize(*hFile, NULL);
91 if (file_size == (DWORD)-1) return NULL;
92 size = file_size - offset;
95 g_offset = offset & ~0xFFFF; /* FIXME: is granularity portable ? */
96 g_size = offset + size - g_offset;
98 if ((*hMap = CreateFileMapping(*hFile, NULL, PAGE_READONLY, 0, 0, NULL)) == 0)
99 return NULL;
101 if ((ret = MapViewOfFile(*hMap, FILE_MAP_READ, 0, g_offset, g_size)) != NULL)
102 ret += offset - g_offset;
103 return ret;
106 /***********************************************************************
107 * DEBUG_UnmapDebugInfoFile
109 static void DEBUG_UnmapDebugInfoFile(HANDLE hFile, HANDLE hMap, void* addr)
111 if (addr) UnmapViewOfFile(addr);
112 if (hMap) CloseHandle(hMap);
113 if (hFile) CloseHandle(hFile);
116 union any_size
118 char * c;
119 short * s;
120 int * i;
121 unsigned int * ui;
125 * This is a convenience structure used to map portions of the
126 * line number table.
128 struct startend
130 unsigned int start;
131 unsigned int end;
135 * This is how we reference the various record types.
137 union codeview_symbol
139 struct
141 short int len;
142 short int id;
143 } generic;
145 struct
147 short int len;
148 short int id;
149 unsigned int offset;
150 unsigned short seg;
151 unsigned short symtype;
152 unsigned char namelen;
153 unsigned char name[1];
154 } data;
156 struct
158 short int len;
159 short int id;
160 unsigned int symtype;
161 unsigned int offset;
162 unsigned short seg;
163 unsigned char namelen;
164 unsigned char name[1];
165 } data32;
167 struct
169 short int len;
170 short int id;
171 unsigned int pparent;
172 unsigned int pend;
173 unsigned int next;
174 unsigned int offset;
175 unsigned short segment;
176 unsigned short thunk_len;
177 unsigned char thtype;
178 unsigned char namelen;
179 unsigned char name[1];
180 } thunk;
182 struct
184 short int len;
185 short int id;
186 unsigned int pparent;
187 unsigned int pend;
188 unsigned int next;
189 unsigned int proc_len;
190 unsigned int debug_start;
191 unsigned int debug_end;
192 unsigned int offset;
193 unsigned short segment;
194 unsigned short proctype;
195 unsigned char flags;
196 unsigned char namelen;
197 unsigned char name[1];
198 } proc;
200 struct
202 short int len;
203 short int id;
204 unsigned int pparent;
205 unsigned int pend;
206 unsigned int next;
207 unsigned int proc_len;
208 unsigned int debug_start;
209 unsigned int debug_end;
210 unsigned int proctype;
211 unsigned int offset;
212 unsigned short segment;
213 unsigned char flags;
214 unsigned char namelen;
215 unsigned char name[1];
216 } proc32;
218 struct
220 short int len; /* Total length of this entry */
221 short int id; /* Always S_BPREL32 */
222 unsigned int offset; /* Stack offset relative to BP */
223 unsigned short symtype;
224 unsigned char namelen;
225 unsigned char name[1];
226 } stack;
228 struct
230 short int len; /* Total length of this entry */
231 short int id; /* Always S_BPREL32 */
232 unsigned int offset; /* Stack offset relative to BP */
233 unsigned int symtype;
234 unsigned char namelen;
235 unsigned char name[1];
236 } stack32;
240 union codeview_type
242 struct
244 short int len;
245 short int id;
246 } generic;
248 struct
250 short int len;
251 short int id;
252 short int attribute;
253 short int datatype;
254 unsigned char variant[1];
255 } pointer;
257 struct
259 short int len;
260 short int id;
261 unsigned int datatype;
262 unsigned int attribute;
263 unsigned char variant[1];
264 } pointer32;
266 struct
268 short int len;
269 short int id;
270 unsigned char nbits;
271 unsigned char bitoff;
272 unsigned short type;
273 } bitfield;
275 struct
277 short int len;
278 short int id;
279 unsigned int type;
280 unsigned char nbits;
281 unsigned char bitoff;
282 } bitfield32;
284 struct
286 short int len;
287 short int id;
288 short int elemtype;
289 short int idxtype;
290 unsigned char arrlen;
291 unsigned char namelen;
292 unsigned char name[1];
293 } array;
295 struct
297 short int len;
298 short int id;
299 unsigned int elemtype;
300 unsigned int idxtype;
301 unsigned char arrlen;
302 unsigned char namelen;
303 unsigned char name[1];
304 } array32;
306 struct
308 short int len;
309 short int id;
310 short int n_element;
311 short int fieldlist;
312 short int property;
313 short int derived;
314 short int vshape;
315 unsigned short structlen;
316 unsigned char namelen;
317 unsigned char name[1];
318 } structure;
320 struct
322 short int len;
323 short int id;
324 short int n_element;
325 short int property;
326 unsigned int fieldlist;
327 unsigned int derived;
328 unsigned int vshape;
329 unsigned short structlen;
330 unsigned char namelen;
331 unsigned char name[1];
332 } structure32;
334 struct
336 short int len;
337 short int id;
338 short int count;
339 short int field;
340 short int property;
341 unsigned short un_len;
342 unsigned char namelen;
343 unsigned char name[1];
344 } t_union;
346 struct
348 short int len;
349 short int id;
350 short int count;
351 short int property;
352 unsigned int field;
353 unsigned short un_len;
354 unsigned char namelen;
355 unsigned char name[1];
356 } t_union32;
358 struct
360 short int len;
361 short int id;
362 short int count;
363 short int type;
364 short int field;
365 short int property;
366 unsigned char namelen;
367 unsigned char name[1];
368 } enumeration;
370 struct
372 short int len;
373 short int id;
374 short int count;
375 short int property;
376 unsigned int type;
377 unsigned int field;
378 unsigned char namelen;
379 unsigned char name[1];
380 } enumeration32;
382 struct
384 short int id;
385 short int attribute;
386 unsigned short int value;
387 unsigned char namelen;
388 unsigned char name[1];
389 } enumerate;
391 struct
393 short int id;
394 short int type;
395 short int attribute;
396 unsigned short int offset;
397 unsigned char namelen;
398 unsigned char name[1];
399 } member;
401 struct
403 short int id;
404 short int attribute;
405 unsigned int type;
406 unsigned short int offset;
407 unsigned char namelen;
408 unsigned char name[1];
409 } member32;
412 #define S_COMPILE 0x0001
413 #define S_REGISTER 0x0002
414 #define S_CONSTANT 0x0003
415 #define S_UDT 0x0004
416 #define S_SSEARCH 0x0005
417 #define S_END 0x0006
418 #define S_SKIP 0x0007
419 #define S_CVRESERVE 0x0008
420 #define S_OBJNAME 0x0009
421 #define S_ENDARG 0x000a
422 #define S_COBOLUDT 0x000b
423 #define S_MANYREG 0x000c
424 #define S_RETURN 0x000d
425 #define S_ENTRYTHIS 0x000e
427 #define S_BPREL 0x0200
428 #define S_LDATA 0x0201
429 #define S_GDATA 0x0202
430 #define S_PUB 0x0203
431 #define S_LPROC 0x0204
432 #define S_GPROC 0x0205
433 #define S_THUNK 0x0206
434 #define S_BLOCK 0x0207
435 #define S_WITH 0x0208
436 #define S_LABEL 0x0209
437 #define S_CEXMODEL 0x020a
438 #define S_VFTPATH 0x020b
439 #define S_REGREL 0x020c
440 #define S_LTHREAD 0x020d
441 #define S_GTHREAD 0x020e
443 #define S_PROCREF 0x0400
444 #define S_DATAREF 0x0401
445 #define S_ALIGN 0x0402
446 #define S_LPROCREF 0x0403
448 #define S_REGISTER_32 0x1001 /* Variants with new 32-bit type indices */
449 #define S_CONSTANT_32 0x1002
450 #define S_UDT_32 0x1003
451 #define S_COBOLUDT_32 0x1004
452 #define S_MANYREG_32 0x1005
454 #define S_BPREL_32 0x1006
455 #define S_LDATA_32 0x1007
456 #define S_GDATA_32 0x1008
457 #define S_PUB_32 0x1009
458 #define S_LPROC_32 0x100a
459 #define S_GPROC_32 0x100b
460 #define S_VFTTABLE_32 0x100c
461 #define S_REGREL_32 0x100d
462 #define S_LTHREAD_32 0x100e
463 #define S_GTHREAD_32 0x100f
467 * This covers the basic datatypes that VC++ seems to be using these days.
468 * 32 bit mode only. There are additional numbers for the pointers in 16
469 * bit mode. There are many other types listed in the documents, but these
470 * are apparently not used by the compiler, or represent pointer types
471 * that are not used.
473 #define T_NOTYPE 0x0000 /* Notype */
474 #define T_ABS 0x0001 /* Abs */
475 #define T_VOID 0x0003 /* Void */
476 #define T_CHAR 0x0010 /* signed char */
477 #define T_SHORT 0x0011 /* short */
478 #define T_LONG 0x0012 /* long */
479 #define T_QUAD 0x0013 /* long long */
480 #define T_UCHAR 0x0020 /* unsigned char */
481 #define T_USHORT 0x0021 /* unsigned short */
482 #define T_ULONG 0x0022 /* unsigned long */
483 #define T_UQUAD 0x0023 /* unsigned long long */
484 #define T_REAL32 0x0040 /* float */
485 #define T_REAL64 0x0041 /* double */
486 #define T_RCHAR 0x0070 /* real char */
487 #define T_WCHAR 0x0071 /* wide char */
488 #define T_INT4 0x0074 /* int */
489 #define T_UINT4 0x0075 /* unsigned int */
491 #define T_32PVOID 0x0403 /* 32 bit near pointer to void */
492 #define T_32PCHAR 0x0410 /* 16:32 near pointer to signed char */
493 #define T_32PSHORT 0x0411 /* 16:32 near pointer to short */
494 #define T_32PLONG 0x0412 /* 16:32 near pointer to int */
495 #define T_32PQUAD 0x0413 /* 16:32 near pointer to long long */
496 #define T_32PUCHAR 0x0420 /* 16:32 near pointer to unsigned char */
497 #define T_32PUSHORT 0x0421 /* 16:32 near pointer to unsigned short */
498 #define T_32PULONG 0x0422 /* 16:32 near pointer to unsigned int */
499 #define T_32PUQUAD 0x0423 /* 16:32 near pointer to long long */
500 #define T_32PREAL32 0x0440 /* 16:32 near pointer to float */
501 #define T_32PREAL64 0x0441 /* 16:32 near pointer to float */
502 #define T_32PRCHAR 0x0470 /* 16:32 near pointer to real char */
503 #define T_32PWCHAR 0x0471 /* 16:32 near pointer to real char */
504 #define T_32PINT4 0x0474 /* 16:32 near pointer to int */
505 #define T_32PUINT4 0x0475 /* 16:32 near pointer to unsigned int */
507 #define LF_MODIFIER 0x0001
508 #define LF_POINTER 0x0002
509 #define LF_ARRAY 0x0003
510 #define LF_CLASS 0x0004
511 #define LF_STRUCTURE 0x0005
512 #define LF_UNION 0x0006
513 #define LF_ENUM 0x0007
514 #define LF_PROCEDURE 0x0008
515 #define LF_MFUNCTION 0x0009
516 #define LF_VTSHAPE 0x000a
517 #define LF_COBOL0 0x000b
518 #define LF_COBOL1 0x000c
519 #define LF_BARRAY 0x000d
520 #define LF_LABEL 0x000e
521 #define LF_NULL 0x000f
522 #define LF_NOTTRAN 0x0010
523 #define LF_DIMARRAY 0x0011
524 #define LF_VFTPATH 0x0012
525 #define LF_PRECOMP 0x0013
526 #define LF_ENDPRECOMP 0x0014
527 #define LF_OEM 0x0015
528 #define LF_TYPESERVER 0x0016
530 #define LF_MODIFIER_32 0x1001 /* variants with new 32-bit type indices */
531 #define LF_POINTER_32 0x1002
532 #define LF_ARRAY_32 0x1003
533 #define LF_CLASS_32 0x1004
534 #define LF_STRUCTURE_32 0x1005
535 #define LF_UNION_32 0x1006
536 #define LF_ENUM_32 0x1007
537 #define LF_PROCEDURE_32 0x1008
538 #define LF_MFUNCTION_32 0x1009
539 #define LF_COBOL0_32 0x100a
540 #define LF_BARRAY_32 0x100b
541 #define LF_DIMARRAY_32 0x100c
542 #define LF_VFTPATH_32 0x100d
543 #define LF_PRECOMP_32 0x100e
544 #define LF_OEM_32 0x100f
546 #define LF_SKIP 0x0200
547 #define LF_ARGLIST 0x0201
548 #define LF_DEFARG 0x0202
549 #define LF_LIST 0x0203
550 #define LF_FIELDLIST 0x0204
551 #define LF_DERIVED 0x0205
552 #define LF_BITFIELD 0x0206
553 #define LF_METHODLIST 0x0207
554 #define LF_DIMCONU 0x0208
555 #define LF_DIMCONLU 0x0209
556 #define LF_DIMVARU 0x020a
557 #define LF_DIMVARLU 0x020b
558 #define LF_REFSYM 0x020c
560 #define LF_SKIP_32 0x1200 /* variants with new 32-bit type indices */
561 #define LF_ARGLIST_32 0x1201
562 #define LF_DEFARG_32 0x1202
563 #define LF_FIELDLIST_32 0x1203
564 #define LF_DERIVED_32 0x1204
565 #define LF_BITFIELD_32 0x1205
566 #define LF_METHODLIST_32 0x1206
567 #define LF_DIMCONU_32 0x1207
568 #define LF_DIMCONLU_32 0x1208
569 #define LF_DIMVARU_32 0x1209
570 #define LF_DIMVARLU_32 0x120a
572 #define LF_BCLASS 0x0400
573 #define LF_VBCLASS 0x0401
574 #define LF_IVBCLASS 0x0402
575 #define LF_ENUMERATE 0x0403
576 #define LF_FRIENDFCN 0x0404
577 #define LF_INDEX 0x0405
578 #define LF_MEMBER 0x0406
579 #define LF_STMEMBER 0x0407
580 #define LF_METHOD 0x0408
581 #define LF_NESTTYPE 0x0409
582 #define LF_VFUNCTAB 0x040a
583 #define LF_FRIENDCLS 0x040b
584 #define LF_ONEMETHOD 0x040c
585 #define LF_VFUNCOFF 0x040d
586 #define LF_NESTTYPEEX 0x040e
587 #define LF_MEMBERMODIFY 0x040f
589 #define LF_BCLASS_32 0x1400 /* variants with new 32-bit type indices */
590 #define LF_VBCLASS_32 0x1401
591 #define LF_IVBCLASS_32 0x1402
592 #define LF_FRIENDFCN_32 0x1403
593 #define LF_INDEX_32 0x1404
594 #define LF_MEMBER_32 0x1405
595 #define LF_STMEMBER_32 0x1406
596 #define LF_METHOD_32 0x1407
597 #define LF_NESTTYPE_32 0x1408
598 #define LF_VFUNCTAB_32 0x1409
599 #define LF_FRIENDCLS_32 0x140a
600 #define LF_ONEMETHOD_32 0x140b
601 #define LF_VFUNCOFF_32 0x140c
602 #define LF_NESTTYPEEX_32 0x140d
607 #define MAX_BUILTIN_TYPES 0x480
608 static struct datatype * cv_basic_types[MAX_BUILTIN_TYPES];
609 static int num_cv_defined_types = 0;
610 static struct datatype **cv_defined_types = NULL;
613 * For the type CODEVIEW debug directory entries, the debug directory
614 * points to a structure like this. The cv_name field is the name
615 * of an external .PDB file.
617 struct CodeViewDebug
619 char cv_nbtype[8];
620 unsigned int cv_timestamp;
621 char cv_unknown[4];
622 char cv_name[1];
625 struct MiscDebug {
626 unsigned int DataType;
627 unsigned int Length;
628 char Unicode;
629 char Reserved[3];
630 char Data[1];
634 * This is the header that the COFF variety of debug header points to.
636 struct CoffDebug {
637 unsigned int N_Sym;
638 unsigned int SymbolOffset;
639 unsigned int N_Linenum;
640 unsigned int LinenumberOffset;
641 unsigned int Unused[4];
644 struct CoffLinenum {
645 unsigned int VirtualAddr;
646 unsigned short int Linenum;
649 struct CoffFiles {
650 unsigned int startaddr;
651 unsigned int endaddr;
652 char * filename;
653 int linetab_offset;
654 int linecnt;
655 struct name_hash **entries;
656 int neps;
657 int neps_alloc;
661 struct CoffSymbol {
662 union {
663 char ShortName[8];
664 struct {
665 unsigned int NotLong;
666 unsigned int StrTaboff;
667 } Name;
668 } N;
669 unsigned int Value;
670 short SectionNumber;
671 short Type;
672 char StorageClass;
673 unsigned char NumberOfAuxSymbols;
676 struct CoffAuxSection{
677 unsigned int Length;
678 unsigned short NumberOfRelocations;
679 unsigned short NumberOfLinenumbers;
680 unsigned int CheckSum;
681 short Number;
682 char Selection;
683 } Section;
686 * These two structures are used in the directory within a .DBG file
687 * to locate the individual important bits that we might want to see.
689 struct CV4_DirHead {
690 short unsigned int dhsize;
691 short unsigned int desize;
692 unsigned int ndir;
693 unsigned int next_offset;
694 unsigned int flags;
697 struct CV4_DirEnt {
698 short unsigned int subsect_number;
699 short unsigned int module_number;
700 unsigned int offset;
701 unsigned int size;
705 * These are the values of interest that the subsect_number field takes.
707 #define sstAlignSym 0x125
708 #define sstSrcModule 0x127
710 struct codeview_linetab_hdr
712 unsigned int nline;
713 unsigned int segno;
714 unsigned int start;
715 unsigned int end;
716 char * sourcefile;
717 unsigned short * linetab;
718 unsigned int * offtab;
722 static
723 struct datatype * DEBUG_GetCVType(unsigned int typeno)
725 struct datatype * dt = NULL;
728 * Convert Codeview type numbers into something we can grok internally.
729 * Numbers < 0x1000 are all fixed builtin types. Numbers from 0x1000 and
730 * up are all user defined (structs, etc).
732 if( typeno < 0x1000 )
734 if( typeno < MAX_BUILTIN_TYPES )
736 dt = cv_basic_types[typeno];
739 else
741 if( typeno - 0x1000 < num_cv_defined_types )
743 dt = cv_defined_types[typeno - 0x1000];
747 return dt;
750 static int
751 DEBUG_ParseTypeTable(char * table, int len)
753 int arr_max;
754 int curr_type;
755 enum debug_type fieldtype;
756 int elem_size;
757 union any_size ptr;
758 union any_size ptr2;
759 struct datatype * subtype;
760 char symname[256];
761 union codeview_type * type;
762 union codeview_type * type2;
763 struct datatype * typeptr;
765 curr_type = 0x1000;
767 ptr.c = table;
768 while( ptr.c - table < len )
770 type = (union codeview_type *) ptr.c;
772 if( curr_type - 0x1000 >= num_cv_defined_types )
774 num_cv_defined_types += 0x100;
775 cv_defined_types = (struct datatype **) DBG_realloc(cv_defined_types,
776 num_cv_defined_types * sizeof(struct datatype *));
777 memset(cv_defined_types + num_cv_defined_types - 0x100,
779 0x100 * sizeof(struct datatype *));
780 if( cv_defined_types == NULL )
782 return FALSE;
786 switch(type->generic.id)
788 case LF_POINTER:
789 cv_defined_types[curr_type - 0x1000] =
790 DEBUG_FindOrMakePointerType(DEBUG_GetCVType(type->pointer.datatype));
791 break;
792 case LF_POINTER_32:
793 cv_defined_types[curr_type - 0x1000] =
794 DEBUG_FindOrMakePointerType(DEBUG_GetCVType(type->pointer32.datatype));
795 break;
796 case LF_ARRAY:
797 if( type->array.arrlen >= 0x8000 )
800 * This is a numeric leaf, I am too lazy to handle this right
801 * now.
803 DEBUG_Printf(DBG_CHN_MESG, "Ignoring large numberic leaf.\n");
804 break;
806 if( type->array.namelen != 0 )
808 memset(symname, 0, sizeof(symname));
809 memcpy(symname, type->array.name, type->array.namelen);
810 typeptr = DEBUG_NewDataType(DT_ARRAY, symname);
812 else
814 typeptr = DEBUG_NewDataType(DT_ARRAY, NULL);
816 cv_defined_types[curr_type - 0x1000] = typeptr;
818 subtype = DEBUG_GetCVType(type->array.elemtype);
819 if( (subtype == NULL)
820 || (elem_size = DEBUG_GetObjectSize(subtype)) == 0 )
822 arr_max = 0;
824 else
826 arr_max = type->array.arrlen / DEBUG_GetObjectSize(subtype);
829 DEBUG_SetArrayParams(typeptr, 0, arr_max, subtype);
830 break;
831 case LF_ARRAY_32:
832 if( type->array32.arrlen >= 0x8000 )
835 * This is a numeric leaf, I am too lazy to handle this right
836 * now.
838 DEBUG_Printf(DBG_CHN_MESG, "Ignoring large numberic leaf.\n");
839 break;
841 if( type->array32.namelen != 0 )
843 memset(symname, 0, sizeof(symname));
844 memcpy(symname, type->array32.name, type->array32.namelen);
845 typeptr = DEBUG_NewDataType(DT_ARRAY, symname);
847 else
849 typeptr = DEBUG_NewDataType(DT_ARRAY, NULL);
851 cv_defined_types[curr_type - 0x1000] = typeptr;
853 subtype = DEBUG_GetCVType(type->array32.elemtype);
854 if( (subtype == NULL)
855 || (elem_size = DEBUG_GetObjectSize(subtype)) == 0 )
857 arr_max = 0;
859 else
861 arr_max = type->array32.arrlen / DEBUG_GetObjectSize(subtype);
864 DEBUG_SetArrayParams(typeptr, 0, arr_max, subtype);
865 break;
866 case LF_FIELDLIST:
868 * This is where the basic list of fields is defined for
869 * structures and classes.
871 * First, we need to look ahead and see whether we are building
872 * a fieldlist for an enum or a struct.
874 ptr2.i = ptr.i + 1;
875 type2 = (union codeview_type *) ptr2.c;
876 if( type2->member.id == LF_MEMBER )
878 typeptr = DEBUG_NewDataType(DT_STRUCT, NULL);
879 fieldtype = DT_STRUCT;
881 else if( type2->member.id == LF_ENUMERATE )
883 typeptr = DEBUG_NewDataType(DT_ENUM, NULL);
884 fieldtype = DT_ENUM;
886 else
888 break;
891 cv_defined_types[curr_type - 0x1000] = typeptr;
892 while( ptr2.c < (ptr.c + ((type->generic.len + 3) & ~3)) )
894 type2 = (union codeview_type *) ptr2.c;
895 if( type2->member.id == LF_MEMBER && fieldtype == DT_STRUCT )
897 memset(symname, 0, sizeof(symname));
898 memcpy(symname, type2->member.name, type2->member.namelen);
900 subtype = DEBUG_GetCVType(type2->member.type);
901 elem_size = 0;
902 if( subtype != NULL )
904 elem_size = DEBUG_GetObjectSize(subtype);
907 if( type2->member.offset >= 0x8000 )
910 * This is a numeric leaf, I am too lazy to handle this right
911 * now.
913 DEBUG_Printf(DBG_CHN_MESG, "Ignoring large numberic leaf.\n");
915 else
917 DEBUG_AddStructElement(typeptr, symname, subtype,
918 type2->member.offset << 3,
919 elem_size << 3);
922 else if( type2->member.id == LF_ENUMERATE && fieldtype == DT_ENUM )
924 memset(symname, 0, sizeof(symname));
925 memcpy(symname, type2->enumerate.name, type2->enumerate.namelen);
927 if( type2->enumerate.value >= 0x8000 )
930 * This is a numeric leaf, I am too lazy to handle this right
931 * now.
933 DEBUG_Printf(DBG_CHN_MESG, "Ignoring large numberic leaf.\n");
935 else
937 DEBUG_AddStructElement(typeptr, symname, NULL,
938 type2->enumerate.value, 0);
941 else
944 * Something else I have never seen before. Either wrong type of
945 * object in the fieldlist, or some other problem which I wouldn't
946 * really know how to handle until it came up.
948 DEBUG_Printf(DBG_CHN_MESG, "Unexpected entry in fieldlist\n");
949 break;
953 ptr2.c += ((type2->member.namelen + 9 + 3) & ~3);
955 break;
956 case LF_FIELDLIST_32:
958 * This is where the basic list of fields is defined for
959 * structures and classes.
961 * First, we need to look ahead and see whether we are building
962 * a fieldlist for an enum or a struct.
964 ptr2.i = ptr.i + 1;
965 type2 = (union codeview_type *) ptr2.c;
966 if( type2->member32.id == LF_MEMBER_32 )
968 typeptr = DEBUG_NewDataType(DT_STRUCT, NULL);
969 fieldtype = DT_STRUCT;
971 else if( type2->member32.id == LF_ENUMERATE )
973 typeptr = DEBUG_NewDataType(DT_ENUM, NULL);
974 fieldtype = DT_ENUM;
976 else
978 break;
981 cv_defined_types[curr_type - 0x1000] = typeptr;
982 while( ptr2.c < (ptr.c + ((type->generic.len + 3) & ~3)) )
984 type2 = (union codeview_type *) ptr2.c;
985 if( type2->member.id == LF_MEMBER_32 && fieldtype == DT_STRUCT )
987 memset(symname, 0, sizeof(symname));
988 memcpy(symname, type2->member32.name, type2->member32.namelen);
990 subtype = DEBUG_GetCVType(type2->member32.type);
991 elem_size = 0;
992 if( subtype != NULL )
994 elem_size = DEBUG_GetObjectSize(subtype);
997 if( type2->member32.offset >= 0x8000 )
1000 * This is a numeric leaf, I am too lazy to handle this right
1001 * now.
1003 DEBUG_Printf(DBG_CHN_MESG, "Ignoring large numberic leaf.\n");
1005 else
1007 DEBUG_AddStructElement(typeptr, symname, subtype,
1008 type2->member32.offset << 3,
1009 elem_size << 3);
1012 else if( type2->member32.id == LF_ENUMERATE && fieldtype == DT_ENUM )
1014 memset(symname, 0, sizeof(symname));
1015 memcpy(symname, type2->enumerate.name, type2->enumerate.namelen);
1017 if( type2->enumerate.value >= 0x8000 )
1020 * This is a numeric leaf, I am too lazy to handle this right
1021 * now.
1023 DEBUG_Printf(DBG_CHN_MESG, "Ignoring large numberic leaf.\n");
1025 else
1027 DEBUG_AddStructElement(typeptr, symname, NULL,
1028 type2->enumerate.value, 0);
1031 else
1034 * Something else I have never seen before. Either wrong type of
1035 * object in the fieldlist, or some other problem which I wouldn't
1036 * really know how to handle until it came up.
1038 DEBUG_Printf(DBG_CHN_MESG, "Unexpected entry in fieldlist\n");
1039 break;
1043 ptr2.c += ((type2->member32.namelen + 9 + 3) & ~3);
1045 break;
1046 case LF_STRUCTURE:
1047 case LF_CLASS:
1048 if( type->structure.structlen >= 0x8000 )
1051 * This is a numeric leaf, I am too lazy to handle this right
1052 * now.
1054 DEBUG_Printf(DBG_CHN_MESG, "Ignoring large numberic leaf.\n");
1055 break;
1057 memset(symname, 0, sizeof(symname));
1058 memcpy(symname, type->structure.name, type->structure.namelen);
1059 if( strcmp(symname, "__unnamed") == 0 )
1061 typeptr = DEBUG_NewDataType(DT_STRUCT, NULL);
1063 else
1065 typeptr = DEBUG_NewDataType(DT_STRUCT, symname);
1067 cv_defined_types[curr_type - 0x1000] = typeptr;
1070 * Now copy the relevant bits from the fieldlist that we specified.
1072 subtype = DEBUG_GetCVType(type->structure.fieldlist);
1074 if( subtype != NULL )
1076 DEBUG_SetStructSize(typeptr, type->structure.structlen);
1077 DEBUG_CopyFieldlist(typeptr, subtype);
1079 break;
1080 case LF_STRUCTURE_32:
1081 case LF_CLASS_32:
1082 if( type->structure32.structlen >= 0x8000 )
1085 * This is a numeric leaf, I am too lazy to handle this right
1086 * now.
1088 DEBUG_Printf(DBG_CHN_MESG, "Ignoring large numberic leaf.\n");
1089 break;
1091 memset(symname, 0, sizeof(symname));
1092 memcpy(symname, type->structure32.name, type->structure32.namelen);
1093 if( strcmp(symname, "__unnamed") == 0 )
1095 typeptr = DEBUG_NewDataType(DT_STRUCT, NULL);
1097 else
1099 typeptr = DEBUG_NewDataType(DT_STRUCT, symname);
1101 cv_defined_types[curr_type - 0x1000] = typeptr;
1104 * Now copy the relevant bits from the fieldlist that we specified.
1106 subtype = DEBUG_GetCVType(type->structure32.fieldlist);
1108 if( subtype != NULL )
1110 DEBUG_SetStructSize(typeptr, type->structure32.structlen);
1111 DEBUG_CopyFieldlist(typeptr, subtype);
1113 break;
1114 case LF_UNION:
1115 if( type->t_union.un_len >= 0x8000 )
1118 * This is a numeric leaf, I am too lazy to handle this right
1119 * now.
1121 DEBUG_Printf(DBG_CHN_MESG, "Ignoring large numberic leaf.\n");
1122 break;
1124 memset(symname, 0, sizeof(symname));
1125 memcpy(symname, type->t_union.name, type->t_union.namelen);
1127 if( strcmp(symname, "__unnamed") == 0 )
1129 typeptr = DEBUG_NewDataType(DT_STRUCT, NULL);
1131 else
1133 typeptr = DEBUG_NewDataType(DT_STRUCT, symname);
1136 cv_defined_types[curr_type - 0x1000] = typeptr;
1139 * Now copy the relevant bits from the fieldlist that we specified.
1141 subtype = DEBUG_GetCVType(type->t_union.field);
1143 if( subtype != NULL )
1145 DEBUG_SetStructSize(typeptr, type->t_union.un_len);
1146 DEBUG_CopyFieldlist(typeptr, subtype);
1148 break;
1149 case LF_UNION_32:
1150 if( type->t_union32.un_len >= 0x8000 )
1153 * This is a numeric leaf, I am too lazy to handle this right
1154 * now.
1156 DEBUG_Printf(DBG_CHN_MESG, "Ignoring large numberic leaf.\n");
1157 break;
1159 memset(symname, 0, sizeof(symname));
1160 memcpy(symname, type->t_union32.name, type->t_union32.namelen);
1162 if( strcmp(symname, "__unnamed") == 0 )
1164 typeptr = DEBUG_NewDataType(DT_STRUCT, NULL);
1166 else
1168 typeptr = DEBUG_NewDataType(DT_STRUCT, symname);
1171 cv_defined_types[curr_type - 0x1000] = typeptr;
1174 * Now copy the relevant bits from the fieldlist that we specified.
1176 subtype = DEBUG_GetCVType(type->t_union32.field);
1178 if( subtype != NULL )
1180 DEBUG_SetStructSize(typeptr, type->t_union32.un_len);
1181 DEBUG_CopyFieldlist(typeptr, subtype);
1183 break;
1184 case LF_BITFIELD:
1185 typeptr = DEBUG_NewDataType(DT_BITFIELD, NULL);
1186 cv_defined_types[curr_type - 0x1000] = typeptr;
1187 DEBUG_SetBitfieldParams(typeptr, type->bitfield.bitoff,
1188 type->bitfield.nbits,
1189 DEBUG_GetCVType(type->bitfield.type));
1190 break;
1191 case LF_BITFIELD_32:
1192 typeptr = DEBUG_NewDataType(DT_BITFIELD, NULL);
1193 cv_defined_types[curr_type - 0x1000] = typeptr;
1194 DEBUG_SetBitfieldParams(typeptr, type->bitfield32.bitoff,
1195 type->bitfield32.nbits,
1196 DEBUG_GetCVType(type->bitfield32.type));
1197 break;
1198 case LF_ENUM:
1199 memset(symname, 0, sizeof(symname));
1200 memcpy(symname, type->enumeration.name, type->enumeration.namelen);
1201 typeptr = DEBUG_NewDataType(DT_ENUM, symname);
1202 cv_defined_types[curr_type - 0x1000] = typeptr;
1205 * Now copy the relevant bits from the fieldlist that we specified.
1207 subtype = DEBUG_GetCVType(type->enumeration.field);
1209 if( subtype != NULL )
1211 DEBUG_CopyFieldlist(typeptr, subtype);
1213 break;
1214 case LF_ENUM_32:
1215 memset(symname, 0, sizeof(symname));
1216 memcpy(symname, type->enumeration32.name, type->enumeration32.namelen);
1217 typeptr = DEBUG_NewDataType(DT_ENUM, symname);
1218 cv_defined_types[curr_type - 0x1000] = typeptr;
1221 * Now copy the relevant bits from the fieldlist that we specified.
1223 subtype = DEBUG_GetCVType(type->enumeration32.field);
1225 if( subtype != NULL )
1227 DEBUG_CopyFieldlist(typeptr, subtype);
1229 break;
1230 default:
1231 break;
1233 curr_type++;
1234 ptr.c += (type->generic.len + 3) & ~3;
1237 return TRUE;
1240 void
1241 DEBUG_InitCVDataTypes(void)
1244 * These are the common builtin types that are used by VC++.
1246 cv_basic_types[T_NOTYPE] = NULL;
1247 cv_basic_types[T_ABS] = NULL;
1248 cv_basic_types[T_VOID] = DEBUG_NewDataType(DT_BASIC, "void");
1249 cv_basic_types[T_CHAR] = DEBUG_NewDataType(DT_BASIC, "char");
1250 cv_basic_types[T_SHORT] = DEBUG_NewDataType(DT_BASIC, "short int");
1251 cv_basic_types[T_LONG] = DEBUG_NewDataType(DT_BASIC, "long int");
1252 cv_basic_types[T_QUAD] = DEBUG_NewDataType(DT_BASIC, "long long int");
1253 cv_basic_types[T_UCHAR] = DEBUG_NewDataType(DT_BASIC, "unsigned char");
1254 cv_basic_types[T_USHORT] = DEBUG_NewDataType(DT_BASIC, "short unsigned int");
1255 cv_basic_types[T_ULONG] = DEBUG_NewDataType(DT_BASIC, "long unsigned int");
1256 cv_basic_types[T_UQUAD] = DEBUG_NewDataType(DT_BASIC, "long long unsigned int");
1257 cv_basic_types[T_REAL32] = DEBUG_NewDataType(DT_BASIC, "float");
1258 cv_basic_types[T_REAL64] = DEBUG_NewDataType(DT_BASIC, "double");
1259 cv_basic_types[T_RCHAR] = DEBUG_NewDataType(DT_BASIC, "char");
1260 cv_basic_types[T_WCHAR] = DEBUG_NewDataType(DT_BASIC, "short");
1261 cv_basic_types[T_INT4] = DEBUG_NewDataType(DT_BASIC, "int");
1262 cv_basic_types[T_UINT4] = DEBUG_NewDataType(DT_BASIC, "unsigned int");
1264 cv_basic_types[T_32PVOID] = DEBUG_FindOrMakePointerType(cv_basic_types[T_VOID]);
1265 cv_basic_types[T_32PCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_CHAR]);
1266 cv_basic_types[T_32PSHORT] = DEBUG_FindOrMakePointerType(cv_basic_types[T_SHORT]);
1267 cv_basic_types[T_32PLONG] = DEBUG_FindOrMakePointerType(cv_basic_types[T_LONG]);
1268 cv_basic_types[T_32PQUAD] = DEBUG_FindOrMakePointerType(cv_basic_types[T_QUAD]);
1269 cv_basic_types[T_32PUCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_UCHAR]);
1270 cv_basic_types[T_32PUSHORT] = DEBUG_FindOrMakePointerType(cv_basic_types[T_USHORT]);
1271 cv_basic_types[T_32PULONG] = DEBUG_FindOrMakePointerType(cv_basic_types[T_ULONG]);
1272 cv_basic_types[T_32PUQUAD] = DEBUG_FindOrMakePointerType(cv_basic_types[T_UQUAD]);
1273 cv_basic_types[T_32PREAL32] = DEBUG_FindOrMakePointerType(cv_basic_types[T_REAL32]);
1274 cv_basic_types[T_32PREAL64] = DEBUG_FindOrMakePointerType(cv_basic_types[T_REAL64]);
1275 cv_basic_types[T_32PRCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_RCHAR]);
1276 cv_basic_types[T_32PWCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_WCHAR]);
1277 cv_basic_types[T_32PINT4] = DEBUG_FindOrMakePointerType(cv_basic_types[T_INT4]);
1278 cv_basic_types[T_32PUINT4] = DEBUG_FindOrMakePointerType(cv_basic_types[T_UINT4]);
1282 * In this function, we keep track of deferred debugging information
1283 * that we may need later if we were to need to use the internal debugger.
1284 * We don't fully process it here for performance reasons.
1287 DEBUG_RegisterMSCDebugInfo(DBG_MODULE* module, HANDLE hFile, void* _nth, unsigned long nth_ofs)
1289 int has_codeview = FALSE;
1290 int rtn = FALSE;
1291 IMAGE_DEBUG_DIRECTORY dbg;
1292 u_long v_addr, size, orig_size;
1293 PIMAGE_NT_HEADERS nth = (PIMAGE_NT_HEADERS)_nth;
1295 orig_size = nth->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].Size;
1296 if (orig_size) {
1297 v_addr = nth->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress;
1298 for(size = orig_size; size >= sizeof(dbg); size -= sizeof(dbg))
1300 if (!DEBUG_READ_MEM_VERBOSE((void*)((char *) module->load_addr + v_addr), &dbg, sizeof(dbg))) continue;
1302 switch(dbg.Type)
1304 case IMAGE_DEBUG_TYPE_CODEVIEW:
1305 case IMAGE_DEBUG_TYPE_MISC:
1306 has_codeview = TRUE;
1307 break;
1309 v_addr += sizeof(dbg);
1312 v_addr = nth->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress;
1313 for(size = orig_size; size >= sizeof(dbg); size -= sizeof(dbg))
1315 if (!DEBUG_READ_MEM_VERBOSE((void*)((char *)module->load_addr + v_addr), &dbg, sizeof(dbg))) continue;
1317 switch(dbg.Type)
1319 case IMAGE_DEBUG_TYPE_COFF:
1321 * If we have both codeview and COFF debug info, ignore the
1322 * coff debug info as it would just confuse us, and it is
1323 * less complete.
1325 * FIXME - this is broken - if we cannot find the PDB file, then
1326 * we end up with no debugging info at all. In this case, we
1327 * should use the COFF info as a backup.
1329 if( has_codeview )
1331 break;
1333 case IMAGE_DEBUG_TYPE_CODEVIEW:
1334 case IMAGE_DEBUG_TYPE_MISC:
1336 * This is usually an indirection to a .DBG file.
1337 * This is similar to (but a slightly older format) from the
1338 * PDB file.
1340 * First check to see if the image was 'stripped'. If so, it
1341 * means that this entry points to a .DBG file. Otherwise,
1342 * it just points to itself, and we can ignore this.
1345 if( (dbg.Type != IMAGE_DEBUG_TYPE_MISC) ||
1346 (nth->FileHeader.Characteristics & IMAGE_FILE_DEBUG_STRIPPED) != 0 )
1349 * Read the important bits. What we do after this depends
1350 * upon the type, but this is always enough so we are able
1351 * to proceed if we know what we need to do next.
1353 /* basically, the PE loader maps all sections (data, resources...), but doesn't map
1354 * the DataDirectory array's content. One its entry contains the *beloved*
1355 * debug information. (Note the DataDirectory is mapped, not its content)
1357 HANDLE hMap;
1358 char* dbg_info;
1360 DEBUG_Printf(DBG_CHN_TRACE, "PE debugging info at %ld<%ld>\n", dbg.PointerToRawData, dbg.SizeOfData);
1361 dbg_info = DEBUG_MapDebugInfoFile(NULL, dbg.PointerToRawData, dbg.SizeOfData,
1362 &hFile, &hMap);
1364 if (dbg_info != NULL &&
1365 (module->extra_info = DBG_alloc(sizeof(MSC_DBG_INFO))) != NULL) {
1366 MSC_INFO(module)->dbg_info = dbg_info;
1367 MSC_INFO(module)->dbg_size = dbg.SizeOfData;
1368 MSC_INFO(module)->dbgdir = dbg;
1369 MSC_INFO(module)->sect_ofs = nth_ofs + OFFSET_OF(IMAGE_NT_HEADERS, OptionalHeader) +
1370 nth->FileHeader.SizeOfOptionalHeader;
1371 MSC_INFO(module)->nsect = nth->FileHeader.NumberOfSections;
1372 DEBUG_ProcessMSCDebugInfo(module);
1373 DBG_free(MSC_INFO(module));
1374 MSC_INFO(module) = NULL;
1376 DEBUG_UnmapDebugInfoFile(0, hMap, dbg_info);
1378 break;
1379 #if 0
1380 default:
1381 #endif
1383 v_addr += sizeof(dbg);
1385 DEBUG_CurrProcess->next_index++;
1388 return rtn;
1391 /* look for stabs information in PE header (it's how mingw compiler provides its
1392 * debugging information), and also wine PE <-> ELF linking thru .wsolnk sections
1394 int DEBUG_RegisterStabsDebugInfo(DBG_MODULE* module, HANDLE hFile, void* _nth,
1395 unsigned long nth_ofs)
1397 IMAGE_SECTION_HEADER pe_seg;
1398 unsigned long pe_seg_ofs;
1399 int i, stabsize = 0, stabstrsize = 0, xcnlnksize = 0;
1400 unsigned int stabs = 0, stabstr = 0, xcnlnk = 0;
1401 PIMAGE_NT_HEADERS nth = (PIMAGE_NT_HEADERS)_nth;
1403 pe_seg_ofs = nth_ofs + OFFSET_OF(IMAGE_NT_HEADERS, OptionalHeader) +
1404 nth->FileHeader.SizeOfOptionalHeader;
1406 for (i = 0; i < nth->FileHeader.NumberOfSections; i++, pe_seg_ofs += sizeof(pe_seg)) {
1407 if (!DEBUG_READ_MEM_VERBOSE((void*)((char *)module->load_addr + pe_seg_ofs),
1408 &pe_seg, sizeof(pe_seg)))
1409 continue;
1411 if (!strcasecmp(pe_seg.Name, ".stab")) {
1412 stabs = pe_seg.VirtualAddress;
1413 stabsize = pe_seg.SizeOfRawData;
1414 } else if (!strncasecmp(pe_seg.Name, ".stabstr", 8)) {
1415 stabstr = pe_seg.VirtualAddress;
1416 stabstrsize = pe_seg.SizeOfRawData;
1417 } else if (!strncasecmp(pe_seg.Name, ".xcnlnk", 7)) {
1418 xcnlnk = pe_seg.VirtualAddress;
1419 xcnlnksize = pe_seg.SizeOfRawData;
1423 if (stabstrsize && stabsize) {
1424 char* s1 = DBG_alloc(stabsize+stabstrsize);
1426 if (s1) {
1427 if (DEBUG_READ_MEM_VERBOSE((char*)module->load_addr + stabs, s1, stabsize) &&
1428 DEBUG_READ_MEM_VERBOSE((char*)module->load_addr + stabstr,
1429 s1 + stabsize, stabstrsize)) {
1430 DEBUG_ParseStabs(s1, 0, 0, stabsize, stabsize, stabstrsize);
1431 } else {
1432 DEBUG_Printf(DBG_CHN_MESG, "couldn't read data block\n");
1434 DBG_free(s1);
1435 } else {
1436 DEBUG_Printf(DBG_CHN_MESG, "couldn't alloc %d bytes\n",
1437 stabsize + stabstrsize);
1440 if (xcnlnksize) {
1441 DWORD addr;
1442 char bufstr[256];
1444 if (DEBUG_READ_MEM_VERBOSE((char*)module->load_addr + xcnlnk, &addr,
1445 sizeof(addr)) &&
1446 DEBUG_READ_MEM_VERBOSE((char*)addr, bufstr, sizeof(bufstr))) {
1447 bufstr[sizeof(bufstr) - 1] = 0;
1448 DEBUG_Printf(DBG_CHN_TRACE, "Got xcnlnk: argv0 '%s'\n", bufstr);
1449 DEBUG_ReadExecutableDbgInfo(bufstr);
1452 return TRUE;
1456 * Process COFF debugging information embedded in a Win32 application.
1459 static
1461 DEBUG_ProcessCoff(DBG_MODULE* module)
1463 struct CoffAuxSection * aux;
1464 struct CoffDebug * coff;
1465 struct CoffFiles * coff_files = NULL;
1466 struct CoffLinenum * coff_linetab;
1467 char * coff_strtab;
1468 struct CoffSymbol * coff_sym;
1469 struct CoffSymbol * coff_symbol;
1470 struct CoffFiles * curr_file = NULL;
1471 int i;
1472 int j;
1473 int k;
1474 struct CoffLinenum * linepnt;
1475 int linetab_indx;
1476 char namebuff[9];
1477 char * nampnt;
1478 int naux;
1479 DBG_VALUE new_value;
1480 int nfiles = 0;
1481 int nfiles_alloc = 0;
1482 struct CoffFiles orig_file;
1483 int rtn = FALSE;
1484 char * this_file = NULL;
1486 coff = (struct CoffDebug *) MSC_INFO(module)->dbg_info;
1488 coff_symbol = (struct CoffSymbol *) ((unsigned int) coff + coff->SymbolOffset);
1489 coff_linetab = (struct CoffLinenum *) ((unsigned int) coff + coff->LinenumberOffset);
1490 coff_strtab = (char *) ((unsigned int) coff_symbol + 18*coff->N_Sym);
1492 linetab_indx = 0;
1494 new_value.cookie = DV_TARGET;
1495 new_value.type = NULL;
1497 for(i=0; i < coff->N_Sym; i++ )
1500 * We do this because some compilers (i.e. gcc) incorrectly
1501 * pad the structure up to a 4 byte boundary. The structure
1502 * is really only 18 bytes long, so we have to manually make sure
1503 * we get it right.
1505 * FIXME - there must be a way to have autoconf figure out the
1506 * correct compiler option for this. If it is always gcc, that
1507 * makes life simpler, but I don't want to force this.
1509 coff_sym = (struct CoffSymbol *) ((unsigned int) coff_symbol + 18*i);
1510 naux = coff_sym->NumberOfAuxSymbols;
1512 if( coff_sym->StorageClass == IMAGE_SYM_CLASS_FILE )
1514 if( nfiles + 1 >= nfiles_alloc )
1516 nfiles_alloc += 10;
1517 coff_files = (struct CoffFiles *) DBG_realloc(coff_files,
1518 nfiles_alloc * sizeof(struct CoffFiles));
1520 curr_file = coff_files + nfiles;
1521 nfiles++;
1522 curr_file->startaddr = 0xffffffff;
1523 curr_file->endaddr = 0;
1524 curr_file->filename = ((char *) coff_sym) + 18;
1525 curr_file->linetab_offset = -1;
1526 curr_file->linecnt = 0;
1527 curr_file->entries = NULL;
1528 curr_file->neps = curr_file->neps_alloc = 0;
1529 DEBUG_Printf(DBG_CHN_TRACE,"New file %s\n", curr_file->filename);
1530 i += naux;
1531 continue;
1535 * This guy marks the size and location of the text section
1536 * for the current file. We need to keep track of this so
1537 * we can figure out what file the different global functions
1538 * go with.
1540 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC)
1541 && (naux != 0)
1542 && (coff_sym->Type == 0)
1543 && (coff_sym->SectionNumber == 1) )
1545 aux = (struct CoffAuxSection *) ((unsigned int) coff_sym + 18);
1547 if( curr_file->linetab_offset != -1 )
1549 #if 0
1550 DEBUG_Printf(DBG_CHN_TRACE, "Duplicating sect from %s: %x %x %x %d %d\n",
1551 curr_file->filename,
1552 aux->Length,
1553 aux->NumberOfRelocations,
1554 aux->NumberOfLinenumbers,
1555 aux->Number,
1556 aux->Selection);
1557 DEBUG_Printf(DBG_CHN_TRACE, "More sect %d %x %d %d %d\n",
1558 coff_sym->SectionNumber,
1559 coff_sym->Value,
1560 coff_sym->Type,
1561 coff_sym->StorageClass,
1562 coff_sym->NumberOfAuxSymbols);
1563 #endif
1566 * Save this so we can copy bits from it.
1568 orig_file = *curr_file;
1571 * Duplicate the file entry. We have no way to describe
1572 * multiple text sections in our current way of handling things.
1574 if( nfiles + 1 >= nfiles_alloc )
1576 nfiles_alloc += 10;
1577 coff_files = (struct CoffFiles *) DBG_realloc(coff_files,
1578 nfiles_alloc * sizeof(struct CoffFiles));
1580 curr_file = coff_files + nfiles;
1581 nfiles++;
1582 curr_file->startaddr = 0xffffffff;
1583 curr_file->endaddr = 0;
1584 curr_file->filename = orig_file.filename;
1585 curr_file->linetab_offset = -1;
1586 curr_file->linecnt = 0;
1587 curr_file->entries = NULL;
1588 curr_file->neps = curr_file->neps_alloc = 0;
1590 #if 0
1591 else
1593 DEBUG_Printf(DBG_CHN_TRACE, "New text sect from %s: %x %x %x %d %d\n",
1594 curr_file->filename,
1595 aux->Length,
1596 aux->NumberOfRelocations,
1597 aux->NumberOfLinenumbers,
1598 aux->Number,
1599 aux->Selection);
1601 #endif
1603 if( curr_file->startaddr > coff_sym->Value )
1605 curr_file->startaddr = coff_sym->Value;
1608 if( curr_file->startaddr > coff_sym->Value )
1610 curr_file->startaddr = coff_sym->Value;
1613 if( curr_file->endaddr < coff_sym->Value + aux->Length )
1615 curr_file->endaddr = coff_sym->Value + aux->Length;
1618 curr_file->linetab_offset = linetab_indx;
1619 curr_file->linecnt = aux->NumberOfLinenumbers;
1620 linetab_indx += aux->NumberOfLinenumbers;
1621 i += naux;
1622 continue;
1625 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC)
1626 && (naux == 0)
1627 && (coff_sym->SectionNumber == 1) )
1630 * This is a normal static function when naux == 0.
1631 * Just register it. The current file is the correct
1632 * one in this instance.
1634 if( coff_sym->N.Name.NotLong )
1636 memcpy(namebuff, coff_sym->N.ShortName, 8);
1637 namebuff[8] = '\0';
1638 nampnt = &namebuff[0];
1640 else
1642 nampnt = coff_strtab + coff_sym->N.Name.StrTaboff;
1645 if( nampnt[0] == '_' )
1647 nampnt++;
1650 new_value.addr.seg = 0;
1651 new_value.addr.off = (int) ((char *)module->load_addr + coff_sym->Value);
1653 if( curr_file->neps + 1 >= curr_file->neps_alloc )
1655 curr_file->neps_alloc += 10;
1656 curr_file->entries = (struct name_hash **)
1657 DBG_realloc(curr_file->entries,
1658 curr_file->neps_alloc * sizeof(struct name_hash *));
1660 #if 0
1661 DEBUG_Printf(DBG_CHN_TRACE,"\tAdding static symbol %s\n", nampnt);
1662 #endif
1663 curr_file->entries[curr_file->neps++] =
1664 DEBUG_AddSymbol( nampnt, &new_value, this_file, SYM_WIN32 );
1665 i += naux;
1666 continue;
1669 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL)
1670 && ISFCN(coff_sym->Type)
1671 && (coff_sym->SectionNumber > 0) )
1673 if( coff_sym->N.Name.NotLong )
1675 memcpy(namebuff, coff_sym->N.ShortName, 8);
1676 namebuff[8] = '\0';
1677 nampnt = &namebuff[0];
1679 else
1681 nampnt = coff_strtab + coff_sym->N.Name.StrTaboff;
1685 if( nampnt[0] == '_' )
1687 nampnt++;
1690 new_value.addr.seg = 0;
1691 new_value.addr.off = (int) ((char *)module->load_addr + coff_sym->Value);
1693 #if 0
1694 DEBUG_Printf(DBG_CHN_TRACE, "%d: %x %s\n", i, new_value.addr.off, nampnt);
1696 DEBUG_Printf(DBG_CHN_TRACE,"\tAdding global symbol %s\n", nampnt);
1697 #endif
1700 * Now we need to figure out which file this guy belongs to.
1702 this_file = NULL;
1703 for(j=0; j < nfiles; j++)
1705 if( coff_files[j].startaddr <= coff_sym->Value
1706 && coff_files[j].endaddr > coff_sym->Value )
1708 this_file = coff_files[j].filename;
1709 break;
1712 if( coff_files[j].neps + 1 >= coff_files[j].neps_alloc )
1714 coff_files[j].neps_alloc += 10;
1715 coff_files[j].entries = (struct name_hash **)
1716 DBG_realloc(coff_files[j].entries,
1717 coff_files[j].neps_alloc * sizeof(struct name_hash *));
1719 coff_files[j].entries[coff_files[j].neps++] =
1720 DEBUG_AddSymbol( nampnt, &new_value, this_file, SYM_WIN32 );
1721 i += naux;
1722 continue;
1725 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL)
1726 && (coff_sym->SectionNumber > 0) )
1729 * Similar to above, but for the case of data symbols.
1730 * These aren't treated as entrypoints.
1732 if( coff_sym->N.Name.NotLong )
1734 memcpy(namebuff, coff_sym->N.ShortName, 8);
1735 namebuff[8] = '\0';
1736 nampnt = &namebuff[0];
1738 else
1740 nampnt = coff_strtab + coff_sym->N.Name.StrTaboff;
1744 if( nampnt[0] == '_' )
1746 nampnt++;
1749 new_value.addr.seg = 0;
1750 new_value.addr.off = (int) ((char *)module->load_addr + coff_sym->Value);
1752 #if 0
1753 DEBUG_Printf(DBG_CHN_TRACE, "%d: %x %s\n", i, new_value.addr.off, nampnt);
1755 DEBUG_Printf(DBG_CHN_TRACE,"\tAdding global data symbol %s\n", nampnt);
1756 #endif
1759 * Now we need to figure out which file this guy belongs to.
1761 DEBUG_AddSymbol( nampnt, &new_value, NULL, SYM_WIN32 );
1762 i += naux;
1763 continue;
1766 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC)
1767 && (naux == 0) )
1770 * Ignore these. They don't have anything to do with
1771 * reality.
1773 i += naux;
1774 continue;
1777 #if 0
1778 DEBUG_Printf(DBG_CHN_TRACE,"Skipping unknown entry %d %d %d\n", coff_sym->StorageClass,
1779 coff_sym->SectionNumber, naux);
1780 #endif
1783 * For now, skip past the aux entries.
1785 i += naux;
1790 * OK, we now should have a list of files, and we should have a list
1791 * of entrypoints. We need to sort the entrypoints so that we are
1792 * able to tie the line numbers with the given functions within the
1793 * file.
1795 if( coff_files != NULL )
1797 for(j=0; j < nfiles; j++)
1799 if( coff_files[j].entries != NULL )
1801 qsort(coff_files[j].entries, coff_files[j].neps,
1802 sizeof(struct name_hash *), DEBUG_cmp_sym);
1807 * Now pick apart the line number tables, and attach the entries
1808 * to the given functions.
1810 for(j=0; j < nfiles; j++)
1812 i = 0;
1813 if( coff_files[j].neps != 0 )
1814 for(k=0; k < coff_files[j].linecnt; k++)
1817 * Another monstrosity caused by the fact that we are using
1818 * a 6 byte structure, and gcc wants to pad structures to 4 byte
1819 * boundaries. Otherwise we could just index into an array.
1821 linepnt = (struct CoffLinenum *)
1822 ((unsigned int) coff_linetab +
1823 6*(coff_files[j].linetab_offset + k));
1825 * If we have spilled onto the next entrypoint, then
1826 * bump the counter..
1828 while(TRUE)
1830 if (i+1 >= coff_files[j].neps) break;
1831 DEBUG_GetSymbolAddr(coff_files[j].entries[i+1], &new_value.addr);
1832 if( (((unsigned int)module->load_addr +
1833 linepnt->VirtualAddr) >= new_value.addr.off) )
1835 i++;
1836 } else break;
1840 * Add the line number. This is always relative to the
1841 * start of the function, so we need to subtract that offset
1842 * first.
1844 DEBUG_GetSymbolAddr(coff_files[j].entries[i], &new_value.addr);
1845 DEBUG_AddLineNumber(coff_files[j].entries[i],
1846 linepnt->Linenum,
1847 (unsigned int) module->load_addr
1848 + linepnt->VirtualAddr
1849 - new_value.addr.off);
1854 rtn = TRUE;
1856 if( coff_files != NULL )
1858 for(j=0; j < nfiles; j++)
1860 if( coff_files[j].entries != NULL )
1862 DBG_free(coff_files[j].entries);
1865 DBG_free(coff_files);
1868 return (rtn);
1873 * Process a codeview line number table. Digestify the thing so that
1874 * we can easily reference the thing when we process the rest of
1875 * the information.
1877 static struct codeview_linetab_hdr *
1878 DEBUG_SnarfLinetab(char * linetab,
1879 int size)
1881 int file_segcount;
1882 char filename[PATH_MAX];
1883 unsigned int * filetab;
1884 char * fn;
1885 int i;
1886 int k;
1887 struct codeview_linetab_hdr * lt_hdr;
1888 unsigned int * lt_ptr;
1889 int nfile;
1890 int nseg;
1891 union any_size pnt;
1892 union any_size pnt2;
1893 struct startend * start;
1894 int this_seg;
1897 * Now get the important bits.
1899 pnt.c = linetab;
1900 nfile = *pnt.s++;
1901 nseg = *pnt.s++;
1903 filetab = (unsigned int *) pnt.c;
1906 * Now count up the number of segments in the file.
1908 nseg = 0;
1909 for(i=0; i<nfile; i++)
1911 pnt2.c = linetab + filetab[i];
1912 nseg += *pnt2.s;
1916 * Next allocate the header we will be returning.
1917 * There is one header for each segment, so that we can reach in
1918 * and pull bits as required.
1920 lt_hdr = (struct codeview_linetab_hdr *)
1921 DBG_alloc((nseg + 1) * sizeof(*lt_hdr));
1922 if( lt_hdr == NULL )
1924 goto leave;
1927 memset(lt_hdr, 0, sizeof(*lt_hdr) * (nseg+1));
1930 * Now fill the header we will be returning, one for each segment.
1931 * Note that this will basically just contain pointers into the existing
1932 * line table, and we do not actually copy any additional information
1933 * or allocate any additional memory.
1936 this_seg = 0;
1937 for(i=0; i<nfile; i++)
1940 * Get the pointer into the segment information.
1942 pnt2.c = linetab + filetab[i];
1943 file_segcount = *pnt2.s;
1945 pnt2.ui++;
1946 lt_ptr = (unsigned int *) pnt2.c;
1947 start = (struct startend *) (lt_ptr + file_segcount);
1950 * Now snarf the filename for all of the segments for this file.
1952 fn = (unsigned char *) (start + file_segcount);
1953 memset(filename, 0, sizeof(filename));
1954 memcpy(filename, fn + 1, *fn);
1955 fn = DBG_strdup(filename);
1957 for(k = 0; k < file_segcount; k++, this_seg++)
1959 pnt2.c = linetab + lt_ptr[k];
1960 lt_hdr[this_seg].start = start[k].start;
1961 lt_hdr[this_seg].end = start[k].end;
1962 lt_hdr[this_seg].sourcefile = fn;
1963 lt_hdr[this_seg].segno = *pnt2.s++;
1964 lt_hdr[this_seg].nline = *pnt2.s++;
1965 lt_hdr[this_seg].offtab = pnt2.ui;
1966 lt_hdr[this_seg].linetab = (unsigned short *)
1967 (pnt2.ui + lt_hdr[this_seg].nline);
1971 leave:
1973 return lt_hdr;
1977 static int
1978 DEBUG_SnarfCodeView( DBG_MODULE * module,
1979 char * cv_data,
1980 int size,
1981 struct codeview_linetab_hdr * linetab)
1983 struct name_hash * curr_func = NULL;
1984 struct wine_locals * curr_sym = NULL;
1985 int i;
1986 int j;
1987 int len;
1988 DBG_VALUE new_value;
1989 int nsect;
1990 union any_size ptr;
1991 IMAGE_SECTION_HEADER * sectp;
1992 union codeview_symbol * sym;
1993 char symname[PATH_MAX];
1994 struct name_hash * thunk_sym = NULL;
1996 ptr.c = cv_data;
1997 nsect = MSC_INFO(module)->nsect;
1998 sectp = DBG_alloc(sizeof(*sectp) * nsect);
1999 if (!sectp ||
2000 !DEBUG_READ_MEM_VERBOSE((char *)module->load_addr + MSC_INFO(module)->sect_ofs,
2001 sectp, sizeof(*sectp) * nsect))
2002 return FALSE;
2005 * Loop over the different types of records and whenever we
2006 * find something we are interested in, record it and move on.
2008 while( ptr.c - cv_data < size )
2010 sym = (union codeview_symbol *) ptr.c;
2012 if( sym->generic.len - sizeof(int) == (ptr.c - cv_data) )
2015 * This happens when we have indirect symbols that VC++ 4.2
2016 * sometimes uses when there isn't a line number table.
2017 * We ignore it - we will process and enter all of the
2018 * symbols in the global symbol table anyways, so there
2019 * isn't much point in keeping track of all of this crap.
2021 break;
2024 memset(symname, 0, sizeof(symname));
2025 switch(sym->generic.id)
2027 case S_GDATA:
2028 case S_LDATA:
2029 case S_PUB:
2031 * First, a couple of sanity checks.
2033 if( sym->data.namelen == 0 )
2035 break;
2038 if( sym->data.seg == 0 || sym->data.seg > nsect )
2040 break;
2044 * Global and local data symbols. We don't associate these
2045 * with any given source file.
2048 memcpy(symname, sym->data.name, sym->data.namelen);
2049 new_value.addr.seg = 0;
2050 new_value.type = DEBUG_GetCVType(sym->data.symtype);
2051 new_value.addr.off = (unsigned int) module->load_addr +
2052 sectp[sym->data.seg - 1].VirtualAddress +
2053 sym->data.offset;
2054 new_value.cookie = DV_TARGET;
2055 DEBUG_AddSymbol( symname, &new_value, NULL, SYM_WIN32 | SYM_DATA );
2056 break;
2057 case S_GDATA_32:
2058 case S_LDATA_32:
2059 case S_PUB_32:
2061 * First, a couple of sanity checks.
2063 if( sym->data32.namelen == 0 )
2065 break;
2068 if( sym->data32.seg == 0 || sym->data32.seg > nsect )
2070 break;
2074 * Global and local data symbols. We don't associate these
2075 * with any given source file.
2078 memcpy(symname, sym->data32.name, sym->data32.namelen);
2079 new_value.addr.seg = 0;
2080 new_value.type = DEBUG_GetCVType(sym->data32.symtype);
2081 new_value.addr.off = (unsigned int) module->load_addr +
2082 sectp[sym->data32.seg - 1].VirtualAddress +
2083 sym->data32.offset;
2084 new_value.cookie = DV_TARGET;
2085 DEBUG_AddSymbol( symname, &new_value, NULL, SYM_WIN32 | SYM_DATA );
2086 break;
2087 case S_THUNK:
2089 * Sort of like a global function, but it just points
2090 * to a thunk, which is a stupid name for what amounts to
2091 * a PLT slot in the normal jargon that everyone else uses.
2093 memcpy(symname, sym->thunk.name, sym->thunk.namelen);
2094 new_value.addr.seg = 0;
2095 new_value.type = NULL;
2096 new_value.addr.off = (unsigned int) module->load_addr +
2097 sectp[sym->thunk.segment - 1].VirtualAddress +
2098 sym->thunk.offset;
2099 new_value.cookie = DV_TARGET;
2100 thunk_sym = DEBUG_AddSymbol( symname, &new_value, NULL,
2101 SYM_WIN32 | SYM_FUNC);
2102 DEBUG_SetSymbolSize(thunk_sym, sym->thunk.thunk_len);
2103 break;
2104 case S_GPROC:
2105 case S_LPROC:
2107 * Global and static functions.
2109 memcpy(symname, sym->proc.name, sym->proc.namelen);
2110 new_value.addr.seg = 0;
2111 new_value.type = DEBUG_GetCVType(sym->proc.proctype);
2112 new_value.addr.off = (unsigned int) module->load_addr +
2113 sectp[sym->proc.segment - 1].VirtualAddress +
2114 sym->proc.offset;
2115 new_value.cookie = DV_TARGET;
2117 * See if we can find a segment that this goes with. If so,
2118 * it means that we also may have line number information
2119 * for this function.
2121 for(i=0; linetab && linetab[i].linetab != NULL; i++)
2123 if( ((unsigned int) module->load_addr
2124 + sectp[linetab[i].segno - 1].VirtualAddress
2125 + linetab[i].start <= new_value.addr.off)
2126 && ((unsigned int) module->load_addr
2127 + sectp[linetab[i].segno - 1].VirtualAddress
2128 + linetab[i].end > new_value.addr.off) )
2130 break;
2134 DEBUG_Normalize(curr_func);
2135 if( !linetab || linetab[i].linetab == NULL )
2137 curr_func = DEBUG_AddSymbol( symname, &new_value, NULL,
2138 SYM_WIN32 | SYM_FUNC);
2140 else
2143 * First, create the entry. Then dig through the linetab
2144 * and add whatever line numbers are appropriate for this
2145 * function.
2147 curr_func = DEBUG_AddSymbol( symname, &new_value,
2148 linetab[i].sourcefile,
2149 SYM_WIN32 | SYM_FUNC);
2150 for(j=0; j < linetab[i].nline; j++)
2152 if( linetab[i].offtab[j] >= sym->proc.offset
2153 && linetab[i].offtab[j] < sym->proc.offset
2154 + sym->proc.proc_len )
2156 DEBUG_AddLineNumber(curr_func, linetab[i].linetab[j],
2157 linetab[i].offtab[j] - sym->proc.offset);
2164 * Add information about where we should set breakpoints
2165 * in this function.
2167 DEBUG_SetSymbolBPOff(curr_func, sym->proc.debug_start);
2168 DEBUG_SetSymbolSize(curr_func, sym->proc.proc_len);
2169 break;
2170 case S_GPROC_32:
2171 case S_LPROC_32:
2173 * Global and static functions.
2175 memcpy(symname, sym->proc32.name, sym->proc32.namelen);
2176 new_value.addr.seg = 0;
2177 new_value.type = DEBUG_GetCVType(sym->proc32.proctype);
2178 new_value.addr.off = (unsigned int) module->load_addr +
2179 sectp[sym->proc32.segment - 1].VirtualAddress +
2180 sym->proc32.offset;
2181 new_value.cookie = DV_TARGET;
2183 * See if we can find a segment that this goes with. If so,
2184 * it means that we also may have line number information
2185 * for this function.
2187 for(i=0; linetab && linetab[i].linetab != NULL; i++)
2189 if( ((unsigned int) module->load_addr
2190 + sectp[linetab[i].segno - 1].VirtualAddress
2191 + linetab[i].start <= new_value.addr.off)
2192 && ((unsigned int) module->load_addr
2193 + sectp[linetab[i].segno - 1].VirtualAddress
2194 + linetab[i].end > new_value.addr.off) )
2196 break;
2200 DEBUG_Normalize(curr_func);
2201 if( !linetab || linetab[i].linetab == NULL )
2203 curr_func = DEBUG_AddSymbol( symname, &new_value, NULL,
2204 SYM_WIN32 | SYM_FUNC);
2206 else
2209 * First, create the entry. Then dig through the linetab
2210 * and add whatever line numbers are appropriate for this
2211 * function.
2213 curr_func = DEBUG_AddSymbol( symname, &new_value,
2214 linetab[i].sourcefile,
2215 SYM_WIN32 | SYM_FUNC);
2216 for(j=0; j < linetab[i].nline; j++)
2218 if( linetab[i].offtab[j] >= sym->proc32.offset
2219 && linetab[i].offtab[j] < sym->proc32.offset
2220 + sym->proc32.proc_len )
2222 DEBUG_AddLineNumber(curr_func, linetab[i].linetab[j],
2223 linetab[i].offtab[j] - sym->proc32.offset);
2230 * Add information about where we should set breakpoints
2231 * in this function.
2233 DEBUG_SetSymbolBPOff(curr_func, sym->proc32.debug_start);
2234 DEBUG_SetSymbolSize(curr_func, sym->proc32.proc_len);
2235 break;
2236 case S_BPREL:
2238 * Function parameters and stack variables.
2240 memcpy(symname, sym->stack.name, sym->stack.namelen);
2241 curr_sym = DEBUG_AddLocal(curr_func,
2243 sym->stack.offset,
2246 symname);
2247 DEBUG_SetLocalSymbolType(curr_sym, DEBUG_GetCVType(sym->stack.symtype));
2249 break;
2250 case S_BPREL_32:
2252 * Function parameters and stack variables.
2254 memcpy(symname, sym->stack32.name, sym->stack32.namelen);
2255 curr_sym = DEBUG_AddLocal(curr_func,
2257 sym->stack32.offset,
2260 symname);
2261 DEBUG_SetLocalSymbolType(curr_sym, DEBUG_GetCVType(sym->stack32.symtype));
2263 break;
2264 default:
2265 break;
2269 * Adjust pointer to point to next entry, rounding up to a word
2270 * boundary. MS preserving alignment? Stranger things have
2271 * happened.
2273 if( sym->generic.id == S_PROCREF
2274 || sym->generic.id == S_DATAREF
2275 || sym->generic.id == S_LPROCREF )
2277 len = (sym->generic.len + 3) & ~3;
2278 len += ptr.c[16] + 1;
2279 ptr.c += (len + 3) & ~3;
2281 else
2283 ptr.c += (sym->generic.len + 3) & ~3;
2287 if( linetab != NULL )
2289 DBG_free(linetab);
2292 DBG_free(sectp);
2293 return TRUE;
2298 * Process PDB file which contains debug information.
2301 #pragma pack(1)
2302 typedef struct _PDB_FILE
2304 DWORD size;
2305 DWORD unknown;
2307 } PDB_FILE, *PPDB_FILE;
2309 typedef struct _PDB_HEADER
2311 CHAR ident[40];
2312 DWORD signature;
2313 DWORD blocksize;
2314 WORD freelist;
2315 WORD total_alloc;
2316 PDB_FILE toc;
2317 WORD toc_block[ 1 ];
2319 } PDB_HEADER, *PPDB_HEADER;
2321 typedef struct _PDB_TOC
2323 DWORD nFiles;
2324 PDB_FILE file[ 1 ];
2326 } PDB_TOC, *PPDB_TOC;
2328 typedef struct _PDB_ROOT
2330 DWORD version;
2331 DWORD TimeDateStamp;
2332 DWORD unknown;
2333 DWORD cbNames;
2334 CHAR names[ 1 ];
2336 } PDB_ROOT, *PPDB_ROOT;
2338 typedef struct _PDB_TYPES_OLD
2340 DWORD version;
2341 WORD first_index;
2342 WORD last_index;
2343 DWORD type_size;
2344 WORD file;
2345 WORD pad;
2347 } PDB_TYPES_OLD, *PPDB_TYPES_OLD;
2349 typedef struct _PDB_TYPES
2351 DWORD version;
2352 DWORD type_offset;
2353 DWORD first_index;
2354 DWORD last_index;
2355 DWORD type_size;
2356 WORD file;
2357 WORD pad;
2358 DWORD hash_size;
2359 DWORD hash_base;
2360 DWORD hash_offset;
2361 DWORD hash_len;
2362 DWORD search_offset;
2363 DWORD search_len;
2364 DWORD unknown_offset;
2365 DWORD unknown_len;
2367 } PDB_TYPES, *PPDB_TYPES;
2369 typedef struct _PDB_SYMBOL_RANGE
2371 WORD segment;
2372 WORD pad1;
2373 DWORD offset;
2374 DWORD size;
2375 DWORD characteristics;
2376 WORD index;
2377 WORD pad2;
2379 } PDB_SYMBOL_RANGE, *PPDB_SYMBOL_RANGE;
2381 typedef struct _PDB_SYMBOL_RANGE_EX
2383 WORD segment;
2384 WORD pad1;
2385 DWORD offset;
2386 DWORD size;
2387 DWORD characteristics;
2388 WORD index;
2389 WORD pad2;
2390 DWORD timestamp;
2391 DWORD unknown;
2393 } PDB_SYMBOL_RANGE_EX, *PPDB_SYMBOL_RANGE_EX;
2395 typedef struct _PDB_SYMBOL_FILE
2397 DWORD unknown1;
2398 PDB_SYMBOL_RANGE range;
2399 WORD flag;
2400 WORD file;
2401 DWORD symbol_size;
2402 DWORD lineno_size;
2403 DWORD unknown2;
2404 DWORD nSrcFiles;
2405 DWORD attribute;
2406 CHAR filename[ 1 ];
2408 } PDB_SYMBOL_FILE, *PPDB_SYMBOL_FILE;
2410 typedef struct _PDB_SYMBOL_FILE_EX
2412 DWORD unknown1;
2413 PDB_SYMBOL_RANGE_EX range;
2414 WORD flag;
2415 WORD file;
2416 DWORD symbol_size;
2417 DWORD lineno_size;
2418 DWORD unknown2;
2419 DWORD nSrcFiles;
2420 DWORD attribute;
2421 DWORD reserved[ 2 ];
2422 CHAR filename[ 1 ];
2424 } PDB_SYMBOL_FILE_EX, *PPDB_SYMBOL_FILE_EX;
2426 typedef struct _PDB_SYMBOL_SOURCE
2428 WORD nModules;
2429 WORD nSrcFiles;
2430 WORD table[ 1 ];
2432 } PDB_SYMBOL_SOURCE, *PPDB_SYMBOL_SOURCE;
2434 typedef struct _PDB_SYMBOL_IMPORT
2436 DWORD unknown1;
2437 DWORD unknown2;
2438 DWORD TimeDateStamp;
2439 DWORD nRequests;
2440 CHAR filename[ 1 ];
2442 } PDB_SYMBOL_IMPORT, *PPDB_SYMBOL_IMPORT;
2444 typedef struct _PDB_SYMBOLS_OLD
2446 WORD hash1_file;
2447 WORD hash2_file;
2448 WORD gsym_file;
2449 WORD pad;
2450 DWORD module_size;
2451 DWORD offset_size;
2452 DWORD hash_size;
2453 DWORD srcmodule_size;
2455 } PDB_SYMBOLS_OLD, *PPDB_SYMBOLS_OLD;
2457 typedef struct _PDB_SYMBOLS
2459 DWORD signature;
2460 DWORD version;
2461 DWORD extended_format;
2462 DWORD hash1_file;
2463 DWORD hash2_file;
2464 DWORD gsym_file;
2465 DWORD module_size;
2466 DWORD offset_size;
2467 DWORD hash_size;
2468 DWORD srcmodule_size;
2469 DWORD pdbimport_size;
2470 DWORD resvd[ 5 ];
2472 } PDB_SYMBOLS, *PPDB_SYMBOLS;
2473 #pragma pack()
2476 static void *pdb_read( LPBYTE image, WORD *block_list, int size )
2478 PPDB_HEADER pdb = (PPDB_HEADER)image;
2479 int i, nBlocks;
2480 LPBYTE buffer;
2482 if ( !size ) return NULL;
2484 nBlocks = (size + pdb->blocksize-1) / pdb->blocksize;
2485 buffer = DBG_alloc( nBlocks * pdb->blocksize );
2487 for ( i = 0; i < nBlocks; i++ )
2488 memcpy( buffer + i*pdb->blocksize,
2489 image + block_list[i]*pdb->blocksize, pdb->blocksize );
2491 return buffer;
2494 static void *pdb_read_file( LPBYTE image, PPDB_TOC toc, int fileNr )
2496 PPDB_HEADER pdb = (PPDB_HEADER)image;
2497 WORD *block_list;
2498 int i;
2500 if ( !toc || fileNr >= toc->nFiles )
2501 return NULL;
2503 block_list = (WORD *) &toc->file[ toc->nFiles ];
2504 for ( i = 0; i < fileNr; i++ )
2505 block_list += (toc->file[i].size + pdb->blocksize-1) / pdb->blocksize;
2507 return pdb_read( image, block_list, toc->file[fileNr].size );
2510 static void pdb_free( void *buffer )
2512 DBG_free( buffer );
2515 static void pdb_convert_types_header( PDB_TYPES *types, char *image )
2517 memset( types, 0, sizeof(PDB_TYPES) );
2518 if ( !image ) return;
2520 if ( *(DWORD *)image < 19960000 ) /* FIXME: correct version? */
2522 /* Old version of the types record header */
2523 PDB_TYPES_OLD *old = (PDB_TYPES_OLD *)image;
2524 types->version = old->version;
2525 types->type_offset = sizeof(PDB_TYPES_OLD);
2526 types->type_size = old->type_size;
2527 types->first_index = old->first_index;
2528 types->last_index = old->last_index;
2529 types->file = old->file;
2531 else
2533 /* New version of the types record header */
2534 *types = *(PDB_TYPES *)image;
2538 static void pdb_convert_symbols_header( PDB_SYMBOLS *symbols,
2539 int *header_size, char *image )
2541 memset( symbols, 0, sizeof(PDB_SYMBOLS) );
2542 if ( !image ) return;
2544 if ( *(DWORD *)image != 0xffffffff )
2546 /* Old version of the symbols record header */
2547 PDB_SYMBOLS_OLD *old = (PDB_SYMBOLS_OLD *)image;
2548 symbols->version = 0;
2549 symbols->extended_format = 0;
2550 symbols->module_size = old->module_size;
2551 symbols->offset_size = old->offset_size;
2552 symbols->hash_size = old->hash_size;
2553 symbols->srcmodule_size = old->srcmodule_size;
2554 symbols->pdbimport_size = 0;
2555 symbols->hash1_file = old->hash1_file;
2556 symbols->hash2_file = old->hash2_file;
2557 symbols->gsym_file = old->gsym_file;
2559 *header_size = sizeof(PDB_SYMBOLS_OLD);
2561 else
2563 /* New version of the symbols record header */
2564 *symbols = *(PDB_SYMBOLS *)image;
2566 *header_size = sizeof(PDB_SYMBOLS);
2570 static int DEBUG_ProcessPDBFile( DBG_MODULE* module, const char *full_filename )
2572 HANDLE hFile, hMap;
2573 char *image = NULL;
2574 PDB_HEADER *pdb = NULL;
2575 PDB_TOC *toc = NULL;
2576 PDB_ROOT *root = NULL;
2577 char *types_image = NULL;
2578 char *symbols_image = NULL;
2579 PDB_TYPES types;
2580 PDB_SYMBOLS symbols;
2581 int header_size = 0;
2582 char *modimage, *file;
2586 * Open and map() .PDB file
2588 if ((image = DEBUG_MapDebugInfoFile(full_filename, 0, 0, &hFile, &hMap)) == NULL) {
2589 DEBUG_Printf( DBG_CHN_ERR, "-Unable to peruse .PDB file %s\n", full_filename );
2590 goto leave;
2594 * Read in TOC and well-known files
2597 pdb = (PPDB_HEADER)image;
2598 toc = pdb_read( image, pdb->toc_block, pdb->toc.size );
2599 root = pdb_read_file( image, toc, 1 );
2600 types_image = pdb_read_file( image, toc, 2 );
2601 symbols_image = pdb_read_file( image, toc, 3 );
2603 pdb_convert_types_header( &types, types_image );
2604 pdb_convert_symbols_header( &symbols, &header_size, symbols_image );
2607 * Check for unknown versions
2610 switch ( root->version )
2612 case 19950623: /* VC 4.0 */
2613 case 19950814:
2614 case 19960307: /* VC 5.0 */
2615 case 19970604: /* VC 6.0 */
2616 break;
2617 default:
2618 DEBUG_Printf( DBG_CHN_ERR, "-Unknown root block version %ld\n", root->version );
2621 switch ( types.version )
2623 case 19950410: /* VC 4.0 */
2624 case 19951122:
2625 case 19961031: /* VC 5.0 / 6.0 */
2626 break;
2627 default:
2628 DEBUG_Printf( DBG_CHN_ERR, "-Unknown type info version %ld\n", types.version );
2631 switch ( symbols.version )
2633 case 0: /* VC 4.0 */
2634 case 19960307: /* VC 5.0 */
2635 case 19970606: /* VC 6.0 */
2636 break;
2637 default:
2638 DEBUG_Printf( DBG_CHN_ERR, "-Unknown symbol info version %ld\n", symbols.version );
2643 * Check .PDB time stamp
2646 if ( root->TimeDateStamp
2647 != ((struct CodeViewDebug *)MSC_INFO(module)->dbg_info)->cv_timestamp )
2649 DEBUG_Printf(DBG_CHN_ERR, "-Wrong time stamp of .PDB file %s\n", full_filename);
2650 goto leave;
2654 * Read type table
2657 DEBUG_ParseTypeTable( types_image + types.type_offset, types.type_size );
2660 * Read type-server .PDB imports
2663 if ( symbols.pdbimport_size )
2665 /* FIXME */
2666 DEBUG_Printf(DBG_CHN_ERR, "-Type server .PDB imports ignored!\n" );
2670 * Read global symbol table
2673 modimage = pdb_read_file( image, toc, symbols.gsym_file );
2674 if ( modimage )
2676 DEBUG_SnarfCodeView( module, modimage,
2677 toc->file[symbols.gsym_file].size, NULL );
2678 pdb_free( modimage );
2682 * Read per-module symbol / linenumber tables
2685 file = symbols_image + header_size;
2686 while ( file - symbols_image < header_size + symbols.module_size )
2688 int file_nr, file_index, symbol_size, lineno_size;
2689 char *file_name;
2691 if ( !symbols.extended_format )
2693 PDB_SYMBOL_FILE *sym_file = (PDB_SYMBOL_FILE *) file;
2694 file_nr = sym_file->file;
2695 file_name = sym_file->filename;
2696 file_index = sym_file->range.index;
2697 symbol_size = sym_file->symbol_size;
2698 lineno_size = sym_file->lineno_size;
2700 else
2702 PDB_SYMBOL_FILE_EX *sym_file = (PDB_SYMBOL_FILE_EX *) file;
2703 file_nr = sym_file->file;
2704 file_name = sym_file->filename;
2705 file_index = sym_file->range.index;
2706 symbol_size = sym_file->symbol_size;
2707 lineno_size = sym_file->lineno_size;
2710 modimage = pdb_read_file( image, toc, file_nr );
2711 if ( modimage )
2713 struct codeview_linetab_hdr *linetab = NULL;
2715 if ( lineno_size )
2716 linetab = DEBUG_SnarfLinetab( modimage + symbol_size, lineno_size );
2718 if ( symbol_size )
2719 DEBUG_SnarfCodeView( module, modimage + sizeof(DWORD),
2720 symbol_size - sizeof(DWORD), linetab );
2722 pdb_free( modimage );
2725 file_name += strlen(file_name) + 1;
2726 file = (char *)( (DWORD)(file_name + strlen(file_name) + 1 + 3) & ~3 );
2730 leave:
2733 * Cleanup
2736 if ( symbols_image ) pdb_free( symbols_image );
2737 if ( types_image ) pdb_free( types_image );
2738 if ( root ) pdb_free( root );
2739 if ( toc ) pdb_free( toc );
2741 DEBUG_UnmapDebugInfoFile(hFile, hMap, image);
2743 return TRUE;
2748 * Process DBG file which contains debug information.
2750 static
2752 DEBUG_ProcessDBGFile(DBG_MODULE* module, const char* filename)
2754 HANDLE hFile, hMap;
2755 char * addr;
2756 char * codeview;
2757 struct CV4_DirHead * codeview_dir;
2758 struct CV4_DirEnt * codeview_dent;
2759 PIMAGE_DEBUG_DIRECTORY dbghdr;
2760 DBG_MODULE module2;
2761 int i;
2762 int j;
2763 struct codeview_linetab_hdr * linetab;
2764 int nsect;
2765 PIMAGE_SEPARATE_DEBUG_HEADER pdbg = NULL;
2766 IMAGE_SECTION_HEADER * sectp;
2768 if ((addr = DEBUG_MapDebugInfoFile(filename, 0, 0, &hFile, &hMap)) == NULL) {
2769 DEBUG_Printf(DBG_CHN_ERR, "-Unable to peruse .DBG file %s\n", filename);
2770 goto leave;
2773 pdbg = (PIMAGE_SEPARATE_DEBUG_HEADER) addr;
2775 if( pdbg->TimeDateStamp != MSC_INFO(module)->dbgdir.TimeDateStamp )
2777 DEBUG_Printf(DBG_CHN_ERR, "Warning - %s has incorrect internal timestamp\n",
2778 filename);
2779 /* goto leave; */
2781 Well, sometimes this happens to DBG files which ARE REALLY the right .DBG
2782 files but nonetheless this check fails. Anyway, WINDBG (debugger for
2783 Windows by Microsoft) loads debug symbols which have incorrect timestamps.
2787 DEBUG_Printf(DBG_CHN_MESG, "Processing symbols from %s...\n", filename);
2789 dbghdr = (PIMAGE_DEBUG_DIRECTORY) ( addr + sizeof(*pdbg)
2790 + pdbg->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)
2791 + pdbg->ExportedNamesSize);
2793 sectp = (PIMAGE_SECTION_HEADER) ((char *) pdbg + sizeof(*pdbg));
2794 nsect = pdbg->NumberOfSections;
2796 for( i=0; i < pdbg->DebugDirectorySize / sizeof(*pdbg); i++, dbghdr++ )
2798 switch(dbghdr->Type)
2800 case IMAGE_DEBUG_TYPE_COFF:
2802 * Dummy up a deferred debug header to handle the
2803 * COFF stuff embedded within the DBG file.
2805 memset((char *) &module2, 0, sizeof(module2));
2806 MSC_INFO(&module2)->dbg_info = (addr + dbghdr->PointerToRawData);
2807 MSC_INFO(&module2)->dbg_size = dbghdr->SizeOfData;
2808 module2.load_addr = module->load_addr;
2810 DEBUG_ProcessCoff(&module2);
2811 break;
2812 case IMAGE_DEBUG_TYPE_CODEVIEW:
2814 * This is the older format by which codeview stuff is
2815 * stored, known as the 'NB09' format. Newer executables
2816 * and dlls created by VC++ use PDB files instead, which
2817 * have lots of internal similarities, but the overall
2818 * format and structure is quite different.
2820 codeview = (addr + dbghdr->PointerToRawData);
2823 * The first thing in the codeview section should be
2824 * an 'NB09' identifier. As a sanity check, make sure
2825 * it is there.
2827 if( *((unsigned int*) codeview) != 0x3930424e )
2829 break;
2833 * Next we need to find the directory. This is easy too.
2835 codeview_dir = (struct CV4_DirHead *)
2836 (codeview + ((unsigned int*) codeview)[1]);
2839 * Some more sanity checks. Make sure that everything
2840 * is as we expect it.
2842 if( codeview_dir->next_offset != 0
2843 || codeview_dir->dhsize != sizeof(*codeview_dir)
2844 || codeview_dir->desize != sizeof(*codeview_dent) )
2846 break;
2848 codeview_dent = (struct CV4_DirEnt *) (codeview_dir + 1);
2850 for(j=0; j < codeview_dir->ndir; j++, codeview_dent++)
2852 if( codeview_dent->subsect_number == sstAlignSym )
2855 * Check the previous entry. If it is a
2856 * sstSrcModule, it contains the line number
2857 * info for this file.
2859 linetab = NULL;
2860 if( codeview_dent[1].module_number == codeview_dent[0].module_number
2861 && codeview_dent[1].subsect_number == sstSrcModule )
2863 linetab = DEBUG_SnarfLinetab(
2864 codeview + codeview_dent[1].offset,
2865 codeview_dent[1].size);
2868 if( codeview_dent[-1].module_number == codeview_dent[0].module_number
2869 && codeview_dent[-1].subsect_number == sstSrcModule )
2871 linetab = DEBUG_SnarfLinetab(
2872 codeview + codeview_dent[-1].offset,
2873 codeview_dent[-1].size);
2876 * Now process the CV stuff.
2878 DEBUG_SnarfCodeView(module,
2879 codeview + codeview_dent->offset + sizeof(DWORD),
2880 codeview_dent->size - sizeof(DWORD),
2881 linetab);
2885 break;
2886 default:
2887 break;
2890 leave:
2892 DEBUG_UnmapDebugInfoFile(hFile, hMap, addr);
2894 return TRUE;
2897 static int
2898 DEBUG_ProcessMSCDebugInfo(DBG_MODULE* module)
2900 struct CodeViewDebug * cvd;
2901 struct MiscDebug * misc;
2902 char * filename;
2903 int sts;
2905 switch (MSC_INFO(module)->dbgdir.Type)
2907 case IMAGE_DEBUG_TYPE_COFF:
2909 * Standard COFF debug information that VC++ adds when you
2910 * use /debugtype:both with the linker.
2912 DEBUG_Printf(DBG_CHN_TRACE, "Processing COFF symbols...\n");
2913 sts = DEBUG_ProcessCoff(module);
2914 break;
2915 case IMAGE_DEBUG_TYPE_CODEVIEW:
2917 * This is a pointer to a PDB file of some sort.
2919 cvd = (struct CodeViewDebug *) MSC_INFO(module)->dbg_info;
2921 if( strcmp(cvd->cv_nbtype, "NB10") != 0 )
2924 * Whatever this is, we don't know how to deal with
2925 * it yet.
2927 sts = FALSE;
2928 break;
2930 sts = DEBUG_ProcessPDBFile(module, cvd->cv_name);
2931 DEBUG_Printf(DBG_CHN_TRACE, "Processing PDB file %s\n", cvd->cv_name);
2932 break;
2933 case IMAGE_DEBUG_TYPE_MISC:
2935 * A pointer to a .DBG file of some sort. These files
2936 * can contain either CV4 or COFF information. Open
2937 * the file, and try to do the right thing with it.
2939 misc = (struct MiscDebug *) MSC_INFO(module)->dbg_info;
2941 filename = strrchr((char *) &misc->Data, '.');
2944 * Ignore the file if it doesn't have a .DBG extension.
2946 if( (filename == NULL)
2947 || ( (strcmp(filename, ".dbg") != 0)
2948 && (strcmp(filename, ".DBG") != 0)) )
2950 sts = FALSE;
2951 break;
2954 filename = (char *) &misc->Data;
2957 * Do the dirty deed...
2959 sts = DEBUG_ProcessDBGFile(module, filename);
2961 break;
2962 default:
2964 * We should never get here...
2966 sts = FALSE;
2967 break;
2969 module->status = (sts) ? DM_STATUS_LOADED : DM_STATUS_ERROR;
2970 return sts;