Added support for MCI_INFO_COPYRIGHT and MCI_INFO_NAME.
[wine.git] / debugger / msc.c
bloba7967ab0759b97f9615991748d43cb950230b2d1
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 <stdio.h>
20 #include <stdlib.h>
22 #include <sys/types.h>
23 #ifdef HAVE_SYS_MMAN_H
24 #include <sys/mman.h>
25 #endif
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #include <limits.h>
29 #include <string.h>
30 #include <unistd.h>
31 #ifndef PATH_MAX
32 #define PATH_MAX _MAX_PATH
33 #endif
34 #include "debugger.h"
35 #include "neexe.h"
36 #include "peexe.h"
37 #include "file.h"
40 *dbg_filename must be at least MAX_PATHNAME_LEN bytes in size
42 static void LocateDebugInfoFile(char *filename, char *dbg_filename)
44 char *str1 = DBG_alloc(MAX_PATHNAME_LEN*10);
45 char *str2 = DBG_alloc(MAX_PATHNAME_LEN);
46 char *file;
47 char *name_part;
48 DOS_FULL_NAME fullname;
50 file = strrchr(filename, '\\');
51 if( file == NULL ) file = filename; else file++;
53 if (GetEnvironmentVariableA("_NT_SYMBOL_PATH", str1, MAX_PATHNAME_LEN))
54 if (SearchPathA(str1, file, NULL, MAX_PATHNAME_LEN*10, str2, &name_part))
55 goto ok;
56 if (GetEnvironmentVariableA("_NT_ALT_SYMBOL_PATH", str1, MAX_PATHNAME_LEN))
57 if (SearchPathA(str1, file, NULL, MAX_PATHNAME_LEN*10, str2, &name_part))
58 goto ok;
59 if (SearchPathA(NULL, file, NULL, MAX_PATHNAME_LEN*10, str2, &name_part))
60 goto ok;
61 else
63 quit:
64 memcpy(dbg_filename, filename, MAX_PATHNAME_LEN);
65 DBG_free(str1);
66 DBG_free(str2);
67 return;
69 ok:
70 if (DOSFS_GetFullName(str2, TRUE, &fullname))
71 memcpy(dbg_filename, fullname.long_name, MAX_PATHNAME_LEN);
72 else
73 goto quit;
74 DBG_free(str1);
75 DBG_free(str2);
76 return;
79 * This is an index we use to keep track of the debug information
80 * when we have multiple sources. We use the same database to also
81 * allow us to do an 'info shared' type of deal, and we use the index
82 * to eliminate duplicates.
84 static int DEBUG_next_index = 0;
86 union any_size
88 char * c;
89 short * s;
90 int * i;
91 unsigned int * ui;
95 * This is a convenience structure used to map portions of the
96 * line number table.
98 struct startend
100 unsigned int start;
101 unsigned int end;
105 * This is how we reference the various record types.
107 union codeview_symbol
109 struct
111 short int len;
112 short int id;
113 } generic;
115 struct
117 short int len;
118 short int id;
119 unsigned int offset;
120 unsigned short seg;
121 unsigned short symtype;
122 unsigned char namelen;
123 unsigned char name[1];
124 } data;
126 struct
128 short int len;
129 short int id;
130 unsigned int symtype;
131 unsigned int offset;
132 unsigned short seg;
133 unsigned char namelen;
134 unsigned char name[1];
135 } data32;
137 struct
139 short int len;
140 short int id;
141 unsigned int pparent;
142 unsigned int pend;
143 unsigned int next;
144 unsigned int offset;
145 unsigned short segment;
146 unsigned short thunk_len;
147 unsigned char thtype;
148 unsigned char namelen;
149 unsigned char name[1];
150 } thunk;
152 struct
154 short int len;
155 short int id;
156 unsigned int pparent;
157 unsigned int pend;
158 unsigned int next;
159 unsigned int proc_len;
160 unsigned int debug_start;
161 unsigned int debug_end;
162 unsigned int offset;
163 unsigned short segment;
164 unsigned short proctype;
165 unsigned char flags;
166 unsigned char namelen;
167 unsigned char name[1];
168 } proc;
170 struct
172 short int len;
173 short int id;
174 unsigned int pparent;
175 unsigned int pend;
176 unsigned int next;
177 unsigned int proc_len;
178 unsigned int debug_start;
179 unsigned int debug_end;
180 unsigned int proctype;
181 unsigned int offset;
182 unsigned short segment;
183 unsigned char flags;
184 unsigned char namelen;
185 unsigned char name[1];
186 } proc32;
188 struct
190 short int len; /* Total length of this entry */
191 short int id; /* Always S_BPREL32 */
192 unsigned int offset; /* Stack offset relative to BP */
193 unsigned short symtype;
194 unsigned char namelen;
195 unsigned char name[1];
196 } stack;
198 struct
200 short int len; /* Total length of this entry */
201 short int id; /* Always S_BPREL32 */
202 unsigned int offset; /* Stack offset relative to BP */
203 unsigned int symtype;
204 unsigned char namelen;
205 unsigned char name[1];
206 } stack32;
210 union codeview_type
212 struct
214 short int len;
215 short int id;
216 } generic;
218 struct
220 short int len;
221 short int id;
222 short int attribute;
223 short int datatype;
224 unsigned char variant[1];
225 } pointer;
227 struct
229 short int len;
230 short int id;
231 unsigned int datatype;
232 unsigned int attribute;
233 unsigned char variant[1];
234 } pointer32;
236 struct
238 short int len;
239 short int id;
240 unsigned char nbits;
241 unsigned char bitoff;
242 unsigned short type;
243 } bitfield;
245 struct
247 short int len;
248 short int id;
249 unsigned int type;
250 unsigned char nbits;
251 unsigned char bitoff;
252 } bitfield32;
254 struct
256 short int len;
257 short int id;
258 short int elemtype;
259 short int idxtype;
260 unsigned char arrlen;
261 unsigned char namelen;
262 unsigned char name[1];
263 } array;
265 struct
267 short int len;
268 short int id;
269 unsigned int elemtype;
270 unsigned int idxtype;
271 unsigned char arrlen;
272 unsigned char namelen;
273 unsigned char name[1];
274 } array32;
276 struct
278 short int len;
279 short int id;
280 short int n_element;
281 short int fieldlist;
282 short int property;
283 short int derived;
284 short int vshape;
285 unsigned short structlen;
286 unsigned char namelen;
287 unsigned char name[1];
288 } structure;
290 struct
292 short int len;
293 short int id;
294 short int n_element;
295 short int property;
296 unsigned int fieldlist;
297 unsigned int derived;
298 unsigned int vshape;
299 unsigned short structlen;
300 unsigned char namelen;
301 unsigned char name[1];
302 } structure32;
304 struct
306 short int len;
307 short int id;
308 short int count;
309 short int field;
310 short int property;
311 unsigned short un_len;
312 unsigned char namelen;
313 unsigned char name[1];
314 } t_union;
316 struct
318 short int len;
319 short int id;
320 short int count;
321 short int property;
322 unsigned int field;
323 unsigned short un_len;
324 unsigned char namelen;
325 unsigned char name[1];
326 } t_union32;
328 struct
330 short int len;
331 short int id;
332 short int count;
333 short int type;
334 short int field;
335 short int property;
336 unsigned char namelen;
337 unsigned char name[1];
338 } enumeration;
340 struct
342 short int len;
343 short int id;
344 short int count;
345 short int property;
346 unsigned int type;
347 unsigned int field;
348 unsigned char namelen;
349 unsigned char name[1];
350 } enumeration32;
352 struct
354 short int id;
355 short int attribute;
356 unsigned short int value;
357 unsigned char namelen;
358 unsigned char name[1];
359 } enumerate;
361 struct
363 short int id;
364 short int type;
365 short int attribute;
366 unsigned short int offset;
367 unsigned char namelen;
368 unsigned char name[1];
369 } member;
371 struct
373 short int id;
374 short int attribute;
375 unsigned int type;
376 unsigned short int offset;
377 unsigned char namelen;
378 unsigned char name[1];
379 } member32;
382 #define S_COMPILE 0x0001
383 #define S_REGISTER 0x0002
384 #define S_CONSTANT 0x0003
385 #define S_UDT 0x0004
386 #define S_SSEARCH 0x0005
387 #define S_END 0x0006
388 #define S_SKIP 0x0007
389 #define S_CVRESERVE 0x0008
390 #define S_OBJNAME 0x0009
391 #define S_ENDARG 0x000a
392 #define S_COBOLUDT 0x000b
393 #define S_MANYREG 0x000c
394 #define S_RETURN 0x000d
395 #define S_ENTRYTHIS 0x000e
397 #define S_BPREL 0x0200
398 #define S_LDATA 0x0201
399 #define S_GDATA 0x0202
400 #define S_PUB 0x0203
401 #define S_LPROC 0x0204
402 #define S_GPROC 0x0205
403 #define S_THUNK 0x0206
404 #define S_BLOCK 0x0207
405 #define S_WITH 0x0208
406 #define S_LABEL 0x0209
407 #define S_CEXMODEL 0x020a
408 #define S_VFTPATH 0x020b
409 #define S_REGREL 0x020c
410 #define S_LTHREAD 0x020d
411 #define S_GTHREAD 0x020e
413 #define S_PROCREF 0x0400
414 #define S_DATAREF 0x0401
415 #define S_ALIGN 0x0402
416 #define S_LPROCREF 0x0403
418 #define S_REGISTER_32 0x1001 /* Variants with new 32-bit type indices */
419 #define S_CONSTANT_32 0x1002
420 #define S_UDT_32 0x1003
421 #define S_COBOLUDT_32 0x1004
422 #define S_MANYREG_32 0x1005
424 #define S_BPREL_32 0x1006
425 #define S_LDATA_32 0x1007
426 #define S_GDATA_32 0x1008
427 #define S_PUB_32 0x1009
428 #define S_LPROC_32 0x100a
429 #define S_GPROC_32 0x100b
430 #define S_VFTTABLE_32 0x100c
431 #define S_REGREL_32 0x100d
432 #define S_LTHREAD_32 0x100e
433 #define S_GTHREAD_32 0x100f
437 * This covers the basic datatypes that VC++ seems to be using these days.
438 * 32 bit mode only. There are additional numbers for the pointers in 16
439 * bit mode. There are many other types listed in the documents, but these
440 * are apparently not used by the compiler, or represent pointer types
441 * that are not used.
443 #define T_NOTYPE 0x0000 /* Notype */
444 #define T_ABS 0x0001 /* Abs */
445 #define T_VOID 0x0003 /* Void */
446 #define T_CHAR 0x0010 /* signed char */
447 #define T_SHORT 0x0011 /* short */
448 #define T_LONG 0x0012 /* long */
449 #define T_QUAD 0x0013 /* long long */
450 #define T_UCHAR 0x0020 /* unsigned char */
451 #define T_USHORT 0x0021 /* unsigned short */
452 #define T_ULONG 0x0022 /* unsigned long */
453 #define T_UQUAD 0x0023 /* unsigned long long */
454 #define T_REAL32 0x0040 /* float */
455 #define T_REAL64 0x0041 /* double */
456 #define T_RCHAR 0x0070 /* real char */
457 #define T_WCHAR 0x0071 /* wide char */
458 #define T_INT4 0x0074 /* int */
459 #define T_UINT4 0x0075 /* unsigned int */
461 #define T_32PVOID 0x0403 /* 32 bit near pointer to void */
462 #define T_32PCHAR 0x0410 /* 16:32 near pointer to signed char */
463 #define T_32PSHORT 0x0411 /* 16:32 near pointer to short */
464 #define T_32PLONG 0x0412 /* 16:32 near pointer to int */
465 #define T_32PQUAD 0x0413 /* 16:32 near pointer to long long */
466 #define T_32PUCHAR 0x0420 /* 16:32 near pointer to unsigned char */
467 #define T_32PUSHORT 0x0421 /* 16:32 near pointer to unsigned short */
468 #define T_32PULONG 0x0422 /* 16:32 near pointer to unsigned int */
469 #define T_32PUQUAD 0x0423 /* 16:32 near pointer to long long */
470 #define T_32PREAL32 0x0440 /* 16:32 near pointer to float */
471 #define T_32PREAL64 0x0441 /* 16:32 near pointer to float */
472 #define T_32PRCHAR 0x0470 /* 16:32 near pointer to real char */
473 #define T_32PWCHAR 0x0471 /* 16:32 near pointer to real char */
474 #define T_32PINT4 0x0474 /* 16:32 near pointer to int */
475 #define T_32PUINT4 0x0475 /* 16:32 near pointer to unsigned int */
477 #define LF_MODIFIER 0x0001
478 #define LF_POINTER 0x0002
479 #define LF_ARRAY 0x0003
480 #define LF_CLASS 0x0004
481 #define LF_STRUCTURE 0x0005
482 #define LF_UNION 0x0006
483 #define LF_ENUM 0x0007
484 #define LF_PROCEDURE 0x0008
485 #define LF_MFUNCTION 0x0009
486 #define LF_VTSHAPE 0x000a
487 #define LF_COBOL0 0x000b
488 #define LF_COBOL1 0x000c
489 #define LF_BARRAY 0x000d
490 #define LF_LABEL 0x000e
491 #define LF_NULL 0x000f
492 #define LF_NOTTRAN 0x0010
493 #define LF_DIMARRAY 0x0011
494 #define LF_VFTPATH 0x0012
495 #define LF_PRECOMP 0x0013
496 #define LF_ENDPRECOMP 0x0014
497 #define LF_OEM 0x0015
498 #define LF_TYPESERVER 0x0016
500 #define LF_MODIFIER_32 0x1001 /* variants with new 32-bit type indices */
501 #define LF_POINTER_32 0x1002
502 #define LF_ARRAY_32 0x1003
503 #define LF_CLASS_32 0x1004
504 #define LF_STRUCTURE_32 0x1005
505 #define LF_UNION_32 0x1006
506 #define LF_ENUM_32 0x1007
507 #define LF_PROCEDURE_32 0x1008
508 #define LF_MFUNCTION_32 0x1009
509 #define LF_COBOL0_32 0x100a
510 #define LF_BARRAY_32 0x100b
511 #define LF_DIMARRAY_32 0x100c
512 #define LF_VFTPATH_32 0x100d
513 #define LF_PRECOMP_32 0x100e
514 #define LF_OEM_32 0x100f
516 #define LF_SKIP 0x0200
517 #define LF_ARGLIST 0x0201
518 #define LF_DEFARG 0x0202
519 #define LF_LIST 0x0203
520 #define LF_FIELDLIST 0x0204
521 #define LF_DERIVED 0x0205
522 #define LF_BITFIELD 0x0206
523 #define LF_METHODLIST 0x0207
524 #define LF_DIMCONU 0x0208
525 #define LF_DIMCONLU 0x0209
526 #define LF_DIMVARU 0x020a
527 #define LF_DIMVARLU 0x020b
528 #define LF_REFSYM 0x020c
530 #define LF_SKIP_32 0x1200 /* variants with new 32-bit type indices */
531 #define LF_ARGLIST_32 0x1201
532 #define LF_DEFARG_32 0x1202
533 #define LF_FIELDLIST_32 0x1203
534 #define LF_DERIVED_32 0x1204
535 #define LF_BITFIELD_32 0x1205
536 #define LF_METHODLIST_32 0x1206
537 #define LF_DIMCONU_32 0x1207
538 #define LF_DIMCONLU_32 0x1208
539 #define LF_DIMVARU_32 0x1209
540 #define LF_DIMVARLU_32 0x120a
542 #define LF_BCLASS 0x0400
543 #define LF_VBCLASS 0x0401
544 #define LF_IVBCLASS 0x0402
545 #define LF_ENUMERATE 0x0403
546 #define LF_FRIENDFCN 0x0404
547 #define LF_INDEX 0x0405
548 #define LF_MEMBER 0x0406
549 #define LF_STMEMBER 0x0407
550 #define LF_METHOD 0x0408
551 #define LF_NESTTYPE 0x0409
552 #define LF_VFUNCTAB 0x040a
553 #define LF_FRIENDCLS 0x040b
554 #define LF_ONEMETHOD 0x040c
555 #define LF_VFUNCOFF 0x040d
556 #define LF_NESTTYPEEX 0x040e
557 #define LF_MEMBERMODIFY 0x040f
559 #define LF_BCLASS_32 0x1400 /* variants with new 32-bit type indices */
560 #define LF_VBCLASS_32 0x1401
561 #define LF_IVBCLASS_32 0x1402
562 #define LF_FRIENDFCN_32 0x1403
563 #define LF_INDEX_32 0x1404
564 #define LF_MEMBER_32 0x1405
565 #define LF_STMEMBER_32 0x1406
566 #define LF_METHOD_32 0x1407
567 #define LF_NESTTYPE_32 0x1408
568 #define LF_VFUNCTAB_32 0x1409
569 #define LF_FRIENDCLS_32 0x140a
570 #define LF_ONEMETHOD_32 0x140b
571 #define LF_VFUNCOFF_32 0x140c
572 #define LF_NESTTYPEEX_32 0x140d
577 #define MAX_BUILTIN_TYPES 0x480
578 static struct datatype * cv_basic_types[MAX_BUILTIN_TYPES];
579 static int num_cv_defined_types = 0;
580 static struct datatype **cv_defined_types = NULL;
583 * For the type CODEVIEW debug directory entries, the debug directory
584 * points to a structure like this. The cv_name field is the name
585 * of an external .PDB file.
587 struct CodeViewDebug
589 char cv_nbtype[8];
590 unsigned int cv_timestamp;
591 char cv_unknown[4];
592 char cv_name[1];
595 struct MiscDebug {
596 unsigned int DataType;
597 unsigned int Length;
598 char Unicode;
599 char Reserved[3];
600 char Data[1];
604 * This is the header that the COFF variety of debug header points to.
606 struct CoffDebug {
607 unsigned int N_Sym;
608 unsigned int SymbolOffset;
609 unsigned int N_Linenum;
610 unsigned int LinenumberOffset;
611 unsigned int Unused[4];
614 struct CoffLinenum {
615 unsigned int VirtualAddr;
616 unsigned short int Linenum;
619 struct CoffFiles {
620 unsigned int startaddr;
621 unsigned int endaddr;
622 char * filename;
623 int linetab_offset;
624 int linecnt;
625 struct name_hash **entries;
626 int neps;
627 int neps_alloc;
631 struct CoffSymbol {
632 union {
633 char ShortName[8];
634 struct {
635 unsigned int NotLong;
636 unsigned int StrTaboff;
637 } Name;
638 } N;
639 unsigned int Value;
640 short SectionNumber;
641 short Type;
642 char StorageClass;
643 unsigned char NumberOfAuxSymbols;
646 struct CoffAuxSection{
647 unsigned int Length;
648 unsigned short NumberOfRelocations;
649 unsigned short NumberOfLinenumbers;
650 unsigned int CheckSum;
651 short Number;
652 char Selection;
653 } Section;
656 * These two structures are used in the directory within a .DBG file
657 * to locate the individual important bits that we might want to see.
659 struct CV4_DirHead {
660 short unsigned int dhsize;
661 short unsigned int desize;
662 unsigned int ndir;
663 unsigned int next_offset;
664 unsigned int flags;
667 struct CV4_DirEnt {
668 short unsigned int subsect_number;
669 short unsigned int module_number;
670 unsigned int offset;
671 unsigned int size;
675 * These are the values of interest that the subsect_number field takes.
677 #define sstAlignSym 0x125
678 #define sstSrcModule 0x127
680 struct codeview_linetab_hdr
682 unsigned int nline;
683 unsigned int segno;
684 unsigned int start;
685 unsigned int end;
686 char * sourcefile;
687 unsigned short * linetab;
688 unsigned int * offtab;
693 ********************************************************************
695 struct deferred_debug_info
697 struct deferred_debug_info * next;
698 char * load_addr;
699 char * module_name;
700 char * dbg_info;
701 int dbg_size;
702 HMODULE module;
703 PIMAGE_DEBUG_DIRECTORY dbgdir;
704 PIMAGE_SECTION_HEADER sectp;
705 int nsect;
706 short int dbg_index;
707 char status;
710 #define DF_STATUS_NEW 0
711 #define DF_STATUS_LOADED 1
712 #define DF_STATUS_ERROR 2
714 struct deferred_debug_info * dbglist = NULL;
717 * A simple macro that tells us whether a given COFF symbol is a
718 * function or not.
720 #define N_TMASK 0x0030
721 #define IMAGE_SYM_DTYPE_FUNCTION 2
722 #define N_BTSHFT 4
723 #define ISFCN(x) (((x) & N_TMASK) == (IMAGE_SYM_DTYPE_FUNCTION << N_BTSHFT))
727 * This is what we are looking for in the COFF symbols.
729 #define IMAGE_SYM_CLASS_EXTERNAL 0x2
730 #define IMAGE_SYM_CLASS_STATIC 0x3
731 #define IMAGE_SYM_CLASS_FILE 0x67
733 static
734 struct datatype * DEBUG_GetCVType(unsigned int typeno)
736 struct datatype * dt = NULL;
739 * Convert Codeview type numbers into something we can grok internally.
740 * Numbers < 0x1000 are all fixed builtin types. Numbers from 0x1000 and
741 * up are all user defined (structs, etc).
743 if( typeno < 0x1000 )
745 if( typeno < MAX_BUILTIN_TYPES )
747 dt = cv_basic_types[typeno];
750 else
752 if( typeno - 0x1000 < num_cv_defined_types )
754 dt = cv_defined_types[typeno - 0x1000];
758 return dt;
761 static int
762 DEBUG_ParseTypeTable(char * table, int len)
764 int arr_max;
765 int curr_type;
766 enum debug_type fieldtype;
767 int elem_size;
768 union any_size ptr;
769 union any_size ptr2;
770 struct datatype * subtype;
771 char symname[256];
772 union codeview_type * type;
773 union codeview_type * type2;
774 struct datatype * typeptr;
776 curr_type = 0x1000;
778 while( ptr.c - table < len )
780 type = (union codeview_type *) ptr.c;
782 if( curr_type - 0x1000 >= num_cv_defined_types )
784 num_cv_defined_types += 0x100;
785 cv_defined_types = (struct datatype **) DBG_realloc(cv_defined_types,
786 num_cv_defined_types * sizeof(struct datatype *));
787 memset(cv_defined_types + num_cv_defined_types - 0x100,
789 0x100 * sizeof(struct datatype *));
790 if( cv_defined_types == NULL )
792 return FALSE;
796 switch(type->generic.id)
798 case LF_POINTER:
799 cv_defined_types[curr_type - 0x1000] =
800 DEBUG_FindOrMakePointerType(DEBUG_GetCVType(type->pointer.datatype));
801 break;
802 case LF_POINTER_32:
803 cv_defined_types[curr_type - 0x1000] =
804 DEBUG_FindOrMakePointerType(DEBUG_GetCVType(type->pointer32.datatype));
805 break;
806 case LF_ARRAY:
807 if( type->array.arrlen >= 0x8000 )
810 * This is a numeric leaf, I am too lazy to handle this right
811 * now.
813 fprintf(stderr, "Ignoring large numberic leaf.\n");
814 break;
816 if( type->array.namelen != 0 )
818 memset(symname, 0, sizeof(symname));
819 memcpy(symname, type->array.name, type->array.namelen);
820 typeptr = DEBUG_NewDataType(DT_ARRAY, symname);
822 else
824 typeptr = DEBUG_NewDataType(DT_ARRAY, NULL);
826 cv_defined_types[curr_type - 0x1000] = typeptr;
828 subtype = DEBUG_GetCVType(type->array.elemtype);
829 if( (subtype == NULL)
830 || (elem_size = DEBUG_GetObjectSize(subtype)) == 0 )
832 arr_max = 0;
834 else
836 arr_max = type->array.arrlen / DEBUG_GetObjectSize(subtype);
839 DEBUG_SetArrayParams(typeptr, 0, arr_max, subtype);
840 break;
841 case LF_ARRAY_32:
842 if( type->array32.arrlen >= 0x8000 )
845 * This is a numeric leaf, I am too lazy to handle this right
846 * now.
848 fprintf(stderr, "Ignoring large numberic leaf.\n");
849 break;
851 if( type->array32.namelen != 0 )
853 memset(symname, 0, sizeof(symname));
854 memcpy(symname, type->array32.name, type->array32.namelen);
855 typeptr = DEBUG_NewDataType(DT_ARRAY, symname);
857 else
859 typeptr = DEBUG_NewDataType(DT_ARRAY, NULL);
861 cv_defined_types[curr_type - 0x1000] = typeptr;
863 subtype = DEBUG_GetCVType(type->array32.elemtype);
864 if( (subtype == NULL)
865 || (elem_size = DEBUG_GetObjectSize(subtype)) == 0 )
867 arr_max = 0;
869 else
871 arr_max = type->array32.arrlen / DEBUG_GetObjectSize(subtype);
874 DEBUG_SetArrayParams(typeptr, 0, arr_max, subtype);
875 break;
876 case LF_FIELDLIST:
878 * This is where the basic list of fields is defined for
879 * structures and classes.
881 * First, we need to look ahead and see whether we are building
882 * a fieldlist for an enum or a struct.
884 ptr2.i = ptr.i + 1;
885 type2 = (union codeview_type *) ptr2.c;
886 if( type2->member.id == LF_MEMBER )
888 typeptr = DEBUG_NewDataType(DT_STRUCT, NULL);
889 fieldtype = DT_STRUCT;
891 else if( type2->member.id == LF_ENUMERATE )
893 typeptr = DEBUG_NewDataType(DT_ENUM, NULL);
894 fieldtype = DT_ENUM;
896 else
898 break;
901 cv_defined_types[curr_type - 0x1000] = typeptr;
902 while( ptr2.c < (ptr.c + ((type->generic.len + 3) & ~3)) )
904 type2 = (union codeview_type *) ptr2.c;
905 if( type2->member.id == LF_MEMBER && fieldtype == DT_STRUCT )
907 memset(symname, 0, sizeof(symname));
908 memcpy(symname, type2->member.name, type2->member.namelen);
910 subtype = DEBUG_GetCVType(type2->member.type);
911 elem_size = 0;
912 if( subtype != NULL )
914 elem_size = DEBUG_GetObjectSize(subtype);
917 if( type2->member.offset >= 0x8000 )
920 * This is a numeric leaf, I am too lazy to handle this right
921 * now.
923 fprintf(stderr, "Ignoring large numberic leaf.\n");
925 else
927 DEBUG_AddStructElement(typeptr, symname, subtype,
928 type2->member.offset << 3,
929 elem_size << 3);
932 else if( type2->member.id == LF_ENUMERATE && fieldtype == DT_ENUM )
934 memset(symname, 0, sizeof(symname));
935 memcpy(symname, type2->enumerate.name, type2->enumerate.namelen);
937 if( type2->enumerate.value >= 0x8000 )
940 * This is a numeric leaf, I am too lazy to handle this right
941 * now.
943 fprintf(stderr, "Ignoring large numberic leaf.\n");
945 else
947 DEBUG_AddStructElement(typeptr, symname, NULL,
948 type2->enumerate.value, 0);
951 else
954 * Something else I have never seen before. Either wrong type of
955 * object in the fieldlist, or some other problem which I wouldn't
956 * really know how to handle until it came up.
958 fprintf(stderr, "Unexpected entry in fieldlist\n");
959 break;
963 ptr2.c += ((type2->member.namelen + 9 + 3) & ~3);
965 break;
966 case LF_FIELDLIST_32:
968 * This is where the basic list of fields is defined for
969 * structures and classes.
971 * First, we need to look ahead and see whether we are building
972 * a fieldlist for an enum or a struct.
974 ptr2.i = ptr.i + 1;
975 type2 = (union codeview_type *) ptr2.c;
976 if( type2->member32.id == LF_MEMBER_32 )
978 typeptr = DEBUG_NewDataType(DT_STRUCT, NULL);
979 fieldtype = DT_STRUCT;
981 else if( type2->member32.id == LF_ENUMERATE )
983 typeptr = DEBUG_NewDataType(DT_ENUM, NULL);
984 fieldtype = DT_ENUM;
986 else
988 break;
991 cv_defined_types[curr_type - 0x1000] = typeptr;
992 while( ptr2.c < (ptr.c + ((type->generic.len + 3) & ~3)) )
994 type2 = (union codeview_type *) ptr2.c;
995 if( type2->member.id == LF_MEMBER_32 && fieldtype == DT_STRUCT )
997 memset(symname, 0, sizeof(symname));
998 memcpy(symname, type2->member32.name, type2->member32.namelen);
1000 subtype = DEBUG_GetCVType(type2->member32.type);
1001 elem_size = 0;
1002 if( subtype != NULL )
1004 elem_size = DEBUG_GetObjectSize(subtype);
1007 if( type2->member32.offset >= 0x8000 )
1010 * This is a numeric leaf, I am too lazy to handle this right
1011 * now.
1013 fprintf(stderr, "Ignoring large numberic leaf.\n");
1015 else
1017 DEBUG_AddStructElement(typeptr, symname, subtype,
1018 type2->member32.offset << 3,
1019 elem_size << 3);
1022 else if( type2->member32.id == LF_ENUMERATE && fieldtype == DT_ENUM )
1024 memset(symname, 0, sizeof(symname));
1025 memcpy(symname, type2->enumerate.name, type2->enumerate.namelen);
1027 if( type2->enumerate.value >= 0x8000 )
1030 * This is a numeric leaf, I am too lazy to handle this right
1031 * now.
1033 fprintf(stderr, "Ignoring large numberic leaf.\n");
1035 else
1037 DEBUG_AddStructElement(typeptr, symname, NULL,
1038 type2->enumerate.value, 0);
1041 else
1044 * Something else I have never seen before. Either wrong type of
1045 * object in the fieldlist, or some other problem which I wouldn't
1046 * really know how to handle until it came up.
1048 fprintf(stderr, "Unexpected entry in fieldlist\n");
1049 break;
1053 ptr2.c += ((type2->member32.namelen + 9 + 3) & ~3);
1055 break;
1056 case LF_STRUCTURE:
1057 case LF_CLASS:
1058 if( type->structure.structlen >= 0x8000 )
1061 * This is a numeric leaf, I am too lazy to handle this right
1062 * now.
1064 fprintf(stderr, "Ignoring large numberic leaf.\n");
1065 break;
1067 memset(symname, 0, sizeof(symname));
1068 memcpy(symname, type->structure.name, type->structure.namelen);
1069 if( strcmp(symname, "__unnamed") == 0 )
1071 typeptr = DEBUG_NewDataType(DT_STRUCT, NULL);
1073 else
1075 typeptr = DEBUG_NewDataType(DT_STRUCT, symname);
1077 cv_defined_types[curr_type - 0x1000] = typeptr;
1080 * Now copy the relevant bits from the fieldlist that we specified.
1082 subtype = DEBUG_GetCVType(type->structure.fieldlist);
1084 if( subtype != NULL )
1086 DEBUG_SetStructSize(typeptr, type->structure.structlen);
1087 DEBUG_CopyFieldlist(typeptr, subtype);
1089 break;
1090 case LF_STRUCTURE_32:
1091 case LF_CLASS_32:
1092 if( type->structure32.structlen >= 0x8000 )
1095 * This is a numeric leaf, I am too lazy to handle this right
1096 * now.
1098 fprintf(stderr, "Ignoring large numberic leaf.\n");
1099 break;
1101 memset(symname, 0, sizeof(symname));
1102 memcpy(symname, type->structure32.name, type->structure32.namelen);
1103 if( strcmp(symname, "__unnamed") == 0 )
1105 typeptr = DEBUG_NewDataType(DT_STRUCT, NULL);
1107 else
1109 typeptr = DEBUG_NewDataType(DT_STRUCT, symname);
1111 cv_defined_types[curr_type - 0x1000] = typeptr;
1114 * Now copy the relevant bits from the fieldlist that we specified.
1116 subtype = DEBUG_GetCVType(type->structure32.fieldlist);
1118 if( subtype != NULL )
1120 DEBUG_SetStructSize(typeptr, type->structure32.structlen);
1121 DEBUG_CopyFieldlist(typeptr, subtype);
1123 break;
1124 case LF_UNION:
1125 if( type->t_union.un_len >= 0x8000 )
1128 * This is a numeric leaf, I am too lazy to handle this right
1129 * now.
1131 fprintf(stderr, "Ignoring large numberic leaf.\n");
1132 break;
1134 memset(symname, 0, sizeof(symname));
1135 memcpy(symname, type->t_union.name, type->t_union.namelen);
1137 if( strcmp(symname, "__unnamed") == 0 )
1139 typeptr = DEBUG_NewDataType(DT_STRUCT, NULL);
1141 else
1143 typeptr = DEBUG_NewDataType(DT_STRUCT, symname);
1146 cv_defined_types[curr_type - 0x1000] = typeptr;
1149 * Now copy the relevant bits from the fieldlist that we specified.
1151 subtype = DEBUG_GetCVType(type->t_union.field);
1153 if( subtype != NULL )
1155 DEBUG_SetStructSize(typeptr, type->t_union.un_len);
1156 DEBUG_CopyFieldlist(typeptr, subtype);
1158 break;
1159 case LF_UNION_32:
1160 if( type->t_union32.un_len >= 0x8000 )
1163 * This is a numeric leaf, I am too lazy to handle this right
1164 * now.
1166 fprintf(stderr, "Ignoring large numberic leaf.\n");
1167 break;
1169 memset(symname, 0, sizeof(symname));
1170 memcpy(symname, type->t_union32.name, type->t_union32.namelen);
1172 if( strcmp(symname, "__unnamed") == 0 )
1174 typeptr = DEBUG_NewDataType(DT_STRUCT, NULL);
1176 else
1178 typeptr = DEBUG_NewDataType(DT_STRUCT, symname);
1181 cv_defined_types[curr_type - 0x1000] = typeptr;
1184 * Now copy the relevant bits from the fieldlist that we specified.
1186 subtype = DEBUG_GetCVType(type->t_union32.field);
1188 if( subtype != NULL )
1190 DEBUG_SetStructSize(typeptr, type->t_union32.un_len);
1191 DEBUG_CopyFieldlist(typeptr, subtype);
1193 break;
1194 case LF_BITFIELD:
1195 typeptr = DEBUG_NewDataType(DT_BITFIELD, NULL);
1196 cv_defined_types[curr_type - 0x1000] = typeptr;
1197 DEBUG_SetBitfieldParams(typeptr, type->bitfield.bitoff,
1198 type->bitfield.nbits,
1199 DEBUG_GetCVType(type->bitfield.type));
1200 break;
1201 case LF_BITFIELD_32:
1202 typeptr = DEBUG_NewDataType(DT_BITFIELD, NULL);
1203 cv_defined_types[curr_type - 0x1000] = typeptr;
1204 DEBUG_SetBitfieldParams(typeptr, type->bitfield32.bitoff,
1205 type->bitfield32.nbits,
1206 DEBUG_GetCVType(type->bitfield32.type));
1207 break;
1208 case LF_ENUM:
1209 memset(symname, 0, sizeof(symname));
1210 memcpy(symname, type->enumeration.name, type->enumeration.namelen);
1211 typeptr = DEBUG_NewDataType(DT_ENUM, symname);
1212 cv_defined_types[curr_type - 0x1000] = typeptr;
1215 * Now copy the relevant bits from the fieldlist that we specified.
1217 subtype = DEBUG_GetCVType(type->enumeration.field);
1219 if( subtype != NULL )
1221 DEBUG_CopyFieldlist(typeptr, subtype);
1223 break;
1224 case LF_ENUM_32:
1225 memset(symname, 0, sizeof(symname));
1226 memcpy(symname, type->enumeration32.name, type->enumeration32.namelen);
1227 typeptr = DEBUG_NewDataType(DT_ENUM, symname);
1228 cv_defined_types[curr_type - 0x1000] = typeptr;
1231 * Now copy the relevant bits from the fieldlist that we specified.
1233 subtype = DEBUG_GetCVType(type->enumeration32.field);
1235 if( subtype != NULL )
1237 DEBUG_CopyFieldlist(typeptr, subtype);
1239 break;
1240 default:
1241 break;
1243 curr_type++;
1244 ptr.c += (type->generic.len + 3) & ~3;
1247 return TRUE;
1250 void
1251 DEBUG_InitCVDataTypes()
1254 * These are the common builtin types that are used by VC++.
1256 cv_basic_types[T_NOTYPE] = NULL;
1257 cv_basic_types[T_ABS] = NULL;
1258 cv_basic_types[T_VOID] = DEBUG_NewDataType(DT_BASIC, "void");
1259 cv_basic_types[T_CHAR] = DEBUG_NewDataType(DT_BASIC, "char");
1260 cv_basic_types[T_SHORT] = DEBUG_NewDataType(DT_BASIC, "short int");
1261 cv_basic_types[T_LONG] = DEBUG_NewDataType(DT_BASIC, "long int");
1262 cv_basic_types[T_QUAD] = DEBUG_NewDataType(DT_BASIC, "long long int");
1263 cv_basic_types[T_UCHAR] = DEBUG_NewDataType(DT_BASIC, "unsigned char");
1264 cv_basic_types[T_USHORT] = DEBUG_NewDataType(DT_BASIC, "short unsigned int");
1265 cv_basic_types[T_ULONG] = DEBUG_NewDataType(DT_BASIC, "long unsigned int");
1266 cv_basic_types[T_UQUAD] = DEBUG_NewDataType(DT_BASIC, "long long unsigned int");
1267 cv_basic_types[T_REAL32] = DEBUG_NewDataType(DT_BASIC, "float");
1268 cv_basic_types[T_REAL64] = DEBUG_NewDataType(DT_BASIC, "double");
1269 cv_basic_types[T_RCHAR] = DEBUG_NewDataType(DT_BASIC, "char");
1270 cv_basic_types[T_WCHAR] = DEBUG_NewDataType(DT_BASIC, "short");
1271 cv_basic_types[T_INT4] = DEBUG_NewDataType(DT_BASIC, "int");
1272 cv_basic_types[T_UINT4] = DEBUG_NewDataType(DT_BASIC, "unsigned int");
1274 cv_basic_types[T_32PVOID] = DEBUG_FindOrMakePointerType(cv_basic_types[T_VOID]);
1275 cv_basic_types[T_32PCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_CHAR]);
1276 cv_basic_types[T_32PSHORT] = DEBUG_FindOrMakePointerType(cv_basic_types[T_SHORT]);
1277 cv_basic_types[T_32PLONG] = DEBUG_FindOrMakePointerType(cv_basic_types[T_LONG]);
1278 cv_basic_types[T_32PQUAD] = DEBUG_FindOrMakePointerType(cv_basic_types[T_QUAD]);
1279 cv_basic_types[T_32PUCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_UCHAR]);
1280 cv_basic_types[T_32PUSHORT] = DEBUG_FindOrMakePointerType(cv_basic_types[T_USHORT]);
1281 cv_basic_types[T_32PULONG] = DEBUG_FindOrMakePointerType(cv_basic_types[T_ULONG]);
1282 cv_basic_types[T_32PUQUAD] = DEBUG_FindOrMakePointerType(cv_basic_types[T_UQUAD]);
1283 cv_basic_types[T_32PREAL32] = DEBUG_FindOrMakePointerType(cv_basic_types[T_REAL32]);
1284 cv_basic_types[T_32PREAL64] = DEBUG_FindOrMakePointerType(cv_basic_types[T_REAL64]);
1285 cv_basic_types[T_32PRCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_RCHAR]);
1286 cv_basic_types[T_32PWCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_WCHAR]);
1287 cv_basic_types[T_32PINT4] = DEBUG_FindOrMakePointerType(cv_basic_types[T_INT4]);
1288 cv_basic_types[T_32PUINT4] = DEBUG_FindOrMakePointerType(cv_basic_types[T_UINT4]);
1292 * In this function, we keep track of deferred debugging information
1293 * that we may need later if we were to need to use the internal debugger.
1294 * We don't fully process it here for performance reasons.
1297 DEBUG_RegisterDebugInfo( HMODULE hModule, const char *module_name)
1299 int has_codeview = FALSE;
1300 int rtn = FALSE;
1301 int orig_size;
1302 PIMAGE_DEBUG_DIRECTORY dbgptr;
1303 u_long v_addr, size;
1304 PIMAGE_NT_HEADERS nth = PE_HEADER(hModule);
1306 size = nth->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].Size;
1307 if (size) {
1308 v_addr = nth->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress;
1309 dbgptr = (PIMAGE_DEBUG_DIRECTORY) (hModule + v_addr);
1310 orig_size = size;
1311 for(; size >= sizeof(*dbgptr); size -= sizeof(*dbgptr), dbgptr++ )
1313 switch(dbgptr->Type)
1315 case IMAGE_DEBUG_TYPE_CODEVIEW:
1316 case IMAGE_DEBUG_TYPE_MISC:
1317 has_codeview = TRUE;
1318 break;
1322 size = orig_size;
1323 dbgptr = (PIMAGE_DEBUG_DIRECTORY) (hModule + v_addr);
1324 for(; size >= sizeof(*dbgptr); size -= sizeof(*dbgptr), dbgptr++ )
1326 switch(dbgptr->Type)
1328 case IMAGE_DEBUG_TYPE_COFF:
1330 * If we have both codeview and COFF debug info, ignore the
1331 * coff debug info as it would just confuse us, and it is
1332 * less complete.
1334 * FIXME - this is broken - if we cannot find the PDB file, then
1335 * we end up with no debugging info at all. In this case, we
1336 * should use the COFF info as a backup.
1338 if( has_codeview )
1340 break;
1342 case IMAGE_DEBUG_TYPE_CODEVIEW:
1343 case IMAGE_DEBUG_TYPE_MISC:
1345 * This is usually an indirection to a .DBG file.
1346 * This is similar to (but a slightly older format) from the
1347 * PDB file.
1349 * First check to see if the image was 'stripped'. If so, it
1350 * means that this entry points to a .DBG file. Otherwise,
1351 * it just points to itself, and we can ignore this.
1359 if( (dbgptr->Type != IMAGE_DEBUG_TYPE_MISC) ||
1360 (PE_HEADER(hModule)->FileHeader.Characteristics & IMAGE_FILE_DEBUG_STRIPPED) != 0 )
1362 char fn[PATH_MAX];
1363 int fd = -1;
1364 DOS_FULL_NAME full_name;
1365 struct deferred_debug_info* deefer = (struct deferred_debug_info *) DBG_alloc(sizeof(*deefer));
1367 deefer->module = hModule;
1368 deefer->load_addr = (char *)hModule;
1371 * Read the important bits. What we do after this depends
1372 * upon the type, but this is always enough so we are able
1373 * to proceed if we know what we need to do next.
1375 /* basically, the PE loader maps all sections (data, resources...), but doesn't map
1376 * the DataDirectory array's content. One its entry contains the *beloved*
1377 * debug information. (Note the DataDirectory is mapped, not its content)
1380 if (GetModuleFileNameA(hModule, fn, sizeof(fn)) > 0 &&
1381 DOSFS_GetFullName(fn, TRUE, &full_name) &&
1382 (fd = open(full_name.long_name, O_RDONLY)) > 0)
1384 deefer->dbg_info = mmap(NULL, dbgptr->SizeOfData,
1385 PROT_READ, MAP_PRIVATE, fd, dbgptr->PointerToRawData);
1386 close(fd);
1387 if( deefer->dbg_info == (char *) 0xffffffff )
1389 DBG_free(deefer);
1390 break;
1393 else
1395 DBG_free(deefer);
1396 fprintf(stderr, " (not mapped: fn=%s, lfn=%s, fd=%d)", fn, full_name.long_name, fd);
1397 break;
1399 deefer->dbg_size = dbgptr->SizeOfData;
1400 deefer->dbgdir = dbgptr;
1401 deefer->next = dbglist;
1402 deefer->status = DF_STATUS_NEW;
1403 deefer->dbg_index = DEBUG_next_index;
1404 deefer->module_name = DBG_strdup(module_name);
1406 deefer->sectp = PE_SECTIONS(hModule);
1407 deefer->nsect = PE_HEADER(hModule)->FileHeader.NumberOfSections;
1409 dbglist = deefer;
1411 break;
1412 #if 0
1413 default:
1414 #endif
1417 DEBUG_next_index++;
1419 /* look for .stabs/.stabstr sections */
1421 PIMAGE_SECTION_HEADER pe_seg = PE_SECTIONS(hModule);
1422 int i,stabsize=0,stabstrsize=0;
1423 unsigned int stabs=0,stabstr=0;
1425 for (i=0;i<nth->FileHeader.NumberOfSections;i++) {
1426 if (!strcasecmp(pe_seg[i].Name,".stab")) {
1427 stabs = pe_seg[i].VirtualAddress;
1428 stabsize = pe_seg[i].SizeOfRawData;
1430 if (!strncasecmp(pe_seg[i].Name,".stabstr",8)) {
1431 stabstr = pe_seg[i].VirtualAddress;
1432 stabstrsize = pe_seg[i].SizeOfRawData;
1435 if (stabstrsize && stabsize) {
1436 #ifdef _WE_SUPPORT_THE_STAB_TYPES_USED_BY_MINGW_TOO
1437 /* Won't work currently, since MINGW32 uses some special typedefs
1438 * which we do not handle yet. Support for them is a bit difficult.
1440 DEBUG_ParseStabs(hModule,0,stabs,stabsize,stabstr,stabstrsize);
1441 #endif
1442 fprintf(stderr,"(stabs not loaded)");
1445 return (rtn);
1449 * ELF modules are also entered into the list - this is so that we
1450 * can make 'info shared' types of displays possible.
1453 DEBUG_RegisterELFDebugInfo(int load_addr, u_long size, char * name)
1455 struct deferred_debug_info * deefer;
1457 deefer = (struct deferred_debug_info *) DBG_alloc(sizeof(*deefer));
1458 deefer->module = 0;
1461 * Read the important bits. What we do after this depends
1462 * upon the type, but this is always enough so we are able
1463 * to proceed if we know what we need to do next.
1465 deefer->dbg_size = size;
1466 deefer->dbg_info = (char *) NULL;
1468 deefer->load_addr = (char *) load_addr;
1469 deefer->dbgdir = NULL;
1470 deefer->next = dbglist;
1471 deefer->status = DF_STATUS_LOADED;
1472 deefer->dbg_index = DEBUG_next_index;
1473 deefer->module_name = DBG_strdup(name);
1474 dbglist = deefer;
1476 DEBUG_next_index++;
1478 return (TRUE);
1484 * Process COFF debugging information embedded in a Win32 application.
1487 static
1489 DEBUG_ProcessCoff(struct deferred_debug_info * deefer)
1491 struct CoffAuxSection * aux;
1492 struct CoffDebug * coff;
1493 struct CoffFiles * coff_files = NULL;
1494 struct CoffLinenum * coff_linetab;
1495 char * coff_strtab;
1496 struct CoffSymbol * coff_sym;
1497 struct CoffSymbol * coff_symbol;
1498 struct CoffFiles * curr_file = NULL;
1499 int i;
1500 int j;
1501 int k;
1502 struct CoffLinenum * linepnt;
1503 int linetab_indx;
1504 char namebuff[9];
1505 char * nampnt;
1506 int naux;
1507 DBG_ADDR new_addr;
1508 int nfiles = 0;
1509 int nfiles_alloc = 0;
1510 struct CoffFiles orig_file;
1511 int rtn = FALSE;
1512 char * this_file = NULL;
1514 coff = (struct CoffDebug *) deefer->dbg_info;
1516 coff_symbol = (struct CoffSymbol *) ((unsigned int) coff + coff->SymbolOffset);
1517 coff_linetab = (struct CoffLinenum *) ((unsigned int) coff + coff->LinenumberOffset);
1518 coff_strtab = (char *) ((unsigned int) coff_symbol + 18*coff->N_Sym);
1520 linetab_indx = 0;
1522 for(i=0; i < coff->N_Sym; i++ )
1525 * We do this because some compilers (i.e. gcc) incorrectly
1526 * pad the structure up to a 4 byte boundary. The structure
1527 * is really only 18 bytes long, so we have to manually make sure
1528 * we get it right.
1530 * FIXME - there must be a way to have autoconf figure out the
1531 * correct compiler option for this. If it is always gcc, that
1532 * makes life simpler, but I don't want to force this.
1534 coff_sym = (struct CoffSymbol *) ((unsigned int) coff_symbol + 18*i);
1535 naux = coff_sym->NumberOfAuxSymbols;
1537 if( coff_sym->StorageClass == IMAGE_SYM_CLASS_FILE )
1539 if( nfiles + 1 >= nfiles_alloc )
1541 nfiles_alloc += 10;
1542 coff_files = (struct CoffFiles *) DBG_realloc(coff_files,
1543 nfiles_alloc * sizeof(struct CoffFiles));
1545 curr_file = coff_files + nfiles;
1546 nfiles++;
1547 curr_file->startaddr = 0xffffffff;
1548 curr_file->endaddr = 0;
1549 curr_file->filename = ((char *) coff_sym) + 18;
1550 curr_file->linetab_offset = -1;
1551 curr_file->linecnt = 0;
1552 curr_file->entries = NULL;
1553 curr_file->neps = curr_file->neps_alloc = 0;
1554 #if 0
1555 fprintf(stderr,"New file %s\n", curr_file->filename);
1556 #endif
1557 i += naux;
1558 continue;
1562 * This guy marks the size and location of the text section
1563 * for the current file. We need to keep track of this so
1564 * we can figure out what file the different global functions
1565 * go with.
1567 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC)
1568 && (naux != 0)
1569 && (coff_sym->Type == 0)
1570 && (coff_sym->SectionNumber == 1) )
1572 aux = (struct CoffAuxSection *) ((unsigned int) coff_sym + 18);
1574 if( curr_file->linetab_offset != -1 )
1576 #if 0
1577 fprintf(stderr, "Duplicating sect from %s: %x %x %x %d %d\n",
1578 curr_file->filename,
1579 aux->Length,
1580 aux->NumberOfRelocations,
1581 aux->NumberOfLinenumbers,
1582 aux->Number,
1583 aux->Selection);
1584 fprintf(stderr, "More sect %d %x %d %d %d\n",
1585 coff_sym->SectionNumber,
1586 coff_sym->Value,
1587 coff_sym->Type,
1588 coff_sym->StorageClass,
1589 coff_sym->NumberOfAuxSymbols);
1590 #endif
1593 * Save this so we can copy bits from it.
1595 orig_file = *curr_file;
1598 * Duplicate the file entry. We have no way to describe
1599 * multiple text sections in our current way of handling things.
1601 if( nfiles + 1 >= nfiles_alloc )
1603 nfiles_alloc += 10;
1604 coff_files = (struct CoffFiles *) DBG_realloc(coff_files,
1605 nfiles_alloc * sizeof(struct CoffFiles));
1607 curr_file = coff_files + nfiles;
1608 nfiles++;
1609 curr_file->startaddr = 0xffffffff;
1610 curr_file->endaddr = 0;
1611 curr_file->filename = orig_file.filename;
1612 curr_file->linetab_offset = -1;
1613 curr_file->linecnt = 0;
1614 curr_file->entries = NULL;
1615 curr_file->neps = curr_file->neps_alloc = 0;
1617 #if 0
1618 else
1620 fprintf(stderr, "New text sect from %s: %x %x %x %d %d\n",
1621 curr_file->filename,
1622 aux->Length,
1623 aux->NumberOfRelocations,
1624 aux->NumberOfLinenumbers,
1625 aux->Number,
1626 aux->Selection);
1628 #endif
1630 if( curr_file->startaddr > coff_sym->Value )
1632 curr_file->startaddr = coff_sym->Value;
1635 if( curr_file->startaddr > coff_sym->Value )
1637 curr_file->startaddr = coff_sym->Value;
1640 if( curr_file->endaddr < coff_sym->Value + aux->Length )
1642 curr_file->endaddr = coff_sym->Value + aux->Length;
1645 curr_file->linetab_offset = linetab_indx;
1646 curr_file->linecnt = aux->NumberOfLinenumbers;
1647 linetab_indx += aux->NumberOfLinenumbers;
1648 i += naux;
1649 continue;
1652 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC)
1653 && (naux == 0)
1654 && (coff_sym->SectionNumber == 1) )
1657 * This is a normal static function when naux == 0.
1658 * Just register it. The current file is the correct
1659 * one in this instance.
1661 if( coff_sym->N.Name.NotLong )
1663 memcpy(namebuff, coff_sym->N.ShortName, 8);
1664 namebuff[8] = '\0';
1665 nampnt = &namebuff[0];
1667 else
1669 nampnt = coff_strtab + coff_sym->N.Name.StrTaboff;
1672 if( nampnt[0] == '_' )
1674 nampnt++;
1677 new_addr.seg = 0;
1678 new_addr.off = (int) (deefer->load_addr + coff_sym->Value);
1680 if( curr_file->neps + 1 >= curr_file->neps_alloc )
1682 curr_file->neps_alloc += 10;
1683 curr_file->entries = (struct name_hash **)
1684 DBG_realloc(curr_file->entries,
1685 curr_file->neps_alloc * sizeof(struct name_hash *));
1687 #if 0
1688 fprintf(stderr,"\tAdding static symbol %s\n", nampnt);
1689 #endif
1690 curr_file->entries[curr_file->neps++] =
1691 DEBUG_AddSymbol( nampnt, &new_addr, this_file, SYM_WIN32 );
1692 i += naux;
1693 continue;
1696 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL)
1697 && ISFCN(coff_sym->Type)
1698 && (coff_sym->SectionNumber > 0) )
1700 if( coff_sym->N.Name.NotLong )
1702 memcpy(namebuff, coff_sym->N.ShortName, 8);
1703 namebuff[8] = '\0';
1704 nampnt = &namebuff[0];
1706 else
1708 nampnt = coff_strtab + coff_sym->N.Name.StrTaboff;
1712 if( nampnt[0] == '_' )
1714 nampnt++;
1717 new_addr.seg = 0;
1718 new_addr.off = (int) (deefer->load_addr + coff_sym->Value);
1720 #if 0
1721 fprintf(stderr, "%d: %x %s\n", i, new_addr.off, nampnt);
1723 fprintf(stderr,"\tAdding global symbol %s\n", nampnt);
1724 #endif
1727 * Now we need to figure out which file this guy belongs to.
1729 this_file = NULL;
1730 for(j=0; j < nfiles; j++)
1732 if( coff_files[j].startaddr <= coff_sym->Value
1733 && coff_files[j].endaddr > coff_sym->Value )
1735 this_file = coff_files[j].filename;
1736 break;
1739 if( coff_files[j].neps + 1 >= coff_files[j].neps_alloc )
1741 coff_files[j].neps_alloc += 10;
1742 coff_files[j].entries = (struct name_hash **)
1743 DBG_realloc(coff_files[j].entries,
1744 coff_files[j].neps_alloc * sizeof(struct name_hash *));
1746 coff_files[j].entries[coff_files[j].neps++] =
1747 DEBUG_AddSymbol( nampnt, &new_addr, this_file, SYM_WIN32 );
1748 i += naux;
1749 continue;
1752 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL)
1753 && (coff_sym->SectionNumber > 0) )
1756 * Similar to above, but for the case of data symbols.
1757 * These aren't treated as entrypoints.
1759 if( coff_sym->N.Name.NotLong )
1761 memcpy(namebuff, coff_sym->N.ShortName, 8);
1762 namebuff[8] = '\0';
1763 nampnt = &namebuff[0];
1765 else
1767 nampnt = coff_strtab + coff_sym->N.Name.StrTaboff;
1771 if( nampnt[0] == '_' )
1773 nampnt++;
1776 new_addr.seg = 0;
1777 new_addr.off = (int) (deefer->load_addr + coff_sym->Value);
1779 #if 0
1780 fprintf(stderr, "%d: %x %s\n", i, new_addr.off, nampnt);
1782 fprintf(stderr,"\tAdding global data symbol %s\n", nampnt);
1783 #endif
1786 * Now we need to figure out which file this guy belongs to.
1788 DEBUG_AddSymbol( nampnt, &new_addr, NULL, SYM_WIN32 );
1789 i += naux;
1790 continue;
1793 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC)
1794 && (naux == 0) )
1797 * Ignore these. They don't have anything to do with
1798 * reality.
1800 i += naux;
1801 continue;
1804 #if 0
1805 fprintf(stderr,"Skipping unknown entry %d %d %d\n", coff_sym->StorageClass,
1806 coff_sym->SectionNumber, naux);
1807 #endif
1810 * For now, skip past the aux entries.
1812 i += naux;
1817 * OK, we now should have a list of files, and we should have a list
1818 * of entrypoints. We need to sort the entrypoints so that we are
1819 * able to tie the line numbers with the given functions within the
1820 * file.
1822 if( coff_files != NULL )
1824 for(j=0; j < nfiles; j++)
1826 if( coff_files[j].entries != NULL )
1828 qsort(coff_files[j].entries, coff_files[j].neps,
1829 sizeof(struct name_hash *), DEBUG_cmp_sym);
1834 * Now pick apart the line number tables, and attach the entries
1835 * to the given functions.
1837 for(j=0; j < nfiles; j++)
1839 i = 0;
1840 if( coff_files[j].neps != 0 )
1841 for(k=0; k < coff_files[j].linecnt; k++)
1844 * Another monstrosity caused by the fact that we are using
1845 * a 6 byte structure, and gcc wants to pad structures to 4 byte
1846 * boundaries. Otherwise we could just index into an array.
1848 linepnt = (struct CoffLinenum *)
1849 ((unsigned int) coff_linetab +
1850 6*(coff_files[j].linetab_offset + k));
1852 * If we have spilled onto the next entrypoint, then
1853 * bump the counter..
1855 while(TRUE)
1857 if (i+1 >= coff_files[j].neps) break;
1858 DEBUG_GetSymbolAddr(coff_files[j].entries[i+1], &new_addr);
1859 if( (((unsigned int)deefer->load_addr +
1860 linepnt->VirtualAddr) >= new_addr.off) )
1862 i++;
1863 } else break;
1867 * Add the line number. This is always relative to the
1868 * start of the function, so we need to subtract that offset
1869 * first.
1871 DEBUG_GetSymbolAddr(coff_files[j].entries[i], &new_addr);
1872 DEBUG_AddLineNumber(coff_files[j].entries[i],
1873 linepnt->Linenum,
1874 (unsigned int) deefer->load_addr
1875 + linepnt->VirtualAddr
1876 - new_addr.off);
1881 rtn = TRUE;
1883 if( coff_files != NULL )
1885 for(j=0; j < nfiles; j++)
1887 if( coff_files[j].entries != NULL )
1889 DBG_free(coff_files[j].entries);
1892 DBG_free(coff_files);
1895 return (rtn);
1900 * Process a codeview line number table. Digestify the thing so that
1901 * we can easily reference the thing when we process the rest of
1902 * the information.
1904 static struct codeview_linetab_hdr *
1905 DEBUG_SnarfLinetab(char * linetab,
1906 int size)
1908 int file_segcount;
1909 char filename[PATH_MAX];
1910 unsigned int * filetab;
1911 char * fn;
1912 int i;
1913 int k;
1914 struct codeview_linetab_hdr * lt_hdr;
1915 unsigned int * lt_ptr;
1916 int nfile;
1917 int nseg;
1918 union any_size pnt;
1919 union any_size pnt2;
1920 struct startend * start;
1921 int this_seg;
1924 * Now get the important bits.
1926 pnt.c = linetab;
1927 nfile = *pnt.s++;
1928 nseg = *pnt.s++;
1930 filetab = (unsigned int *) pnt.c;
1933 * Now count up the number of segments in the file.
1935 nseg = 0;
1936 for(i=0; i<nfile; i++)
1938 pnt2.c = linetab + filetab[i];
1939 nseg += *pnt2.s;
1943 * Next allocate the header we will be returning.
1944 * There is one header for each segment, so that we can reach in
1945 * and pull bits as required.
1947 lt_hdr = (struct codeview_linetab_hdr *)
1948 DBG_alloc((nseg + 1) * sizeof(*lt_hdr));
1949 if( lt_hdr == NULL )
1951 goto leave;
1954 memset(lt_hdr, 0, sizeof(*lt_hdr) * (nseg+1));
1957 * Now fill the header we will be returning, one for each segment.
1958 * Note that this will basically just contain pointers into the existing
1959 * line table, and we do not actually copy any additional information
1960 * or allocate any additional memory.
1963 this_seg = 0;
1964 for(i=0; i<nfile; i++)
1967 * Get the pointer into the segment information.
1969 pnt2.c = linetab + filetab[i];
1970 file_segcount = *pnt2.s;
1972 pnt2.ui++;
1973 lt_ptr = (unsigned int *) pnt2.c;
1974 start = (struct startend *) (lt_ptr + file_segcount);
1977 * Now snarf the filename for all of the segments for this file.
1979 fn = (unsigned char *) (start + file_segcount);
1980 memset(filename, 0, sizeof(filename));
1981 memcpy(filename, fn + 1, *fn);
1982 fn = DBG_strdup(filename);
1984 for(k = 0; k < file_segcount; k++, this_seg++)
1986 pnt2.c = linetab + lt_ptr[k];
1987 lt_hdr[this_seg].start = start[k].start;
1988 lt_hdr[this_seg].end = start[k].end;
1989 lt_hdr[this_seg].sourcefile = fn;
1990 lt_hdr[this_seg].segno = *pnt2.s++;
1991 lt_hdr[this_seg].nline = *pnt2.s++;
1992 lt_hdr[this_seg].offtab = pnt2.ui;
1993 lt_hdr[this_seg].linetab = (unsigned short *)
1994 (pnt2.ui + lt_hdr[this_seg].nline);
1998 leave:
2000 return lt_hdr;
2004 static int
2005 DEBUG_SnarfCodeView( struct deferred_debug_info * deefer,
2006 char * cv_data,
2007 int size,
2008 struct codeview_linetab_hdr * linetab)
2010 struct name_hash * curr_func = NULL;
2011 struct wine_locals * curr_sym = NULL;
2012 int i;
2013 int j;
2014 int len;
2015 DBG_ADDR new_addr;
2016 int nsect;
2017 union any_size ptr;
2018 IMAGE_SECTION_HEADER * sectp;
2019 union codeview_symbol * sym;
2020 char symname[PATH_MAX];
2021 struct name_hash * thunk_sym = NULL;
2023 ptr.c = cv_data;
2024 nsect = deefer->nsect;
2025 sectp = deefer->sectp;
2028 * Loop over the different types of records and whenever we
2029 * find something we are interested in, record it and move on.
2031 while( ptr.c - cv_data < size )
2033 sym = (union codeview_symbol *) ptr.c;
2035 if( sym->generic.len - sizeof(int) == (ptr.c - cv_data) )
2038 * This happens when we have indirect symbols that VC++ 4.2
2039 * sometimes uses when there isn't a line number table.
2040 * We ignore it - we will process and enter all of the
2041 * symbols in the global symbol table anyways, so there
2042 * isn't much point in keeping track of all of this crap.
2044 break;
2047 memset(symname, 0, sizeof(symname));
2048 switch(sym->generic.id)
2050 case S_GDATA:
2051 case S_LDATA:
2052 case S_PUB:
2054 * First, a couple of sanity checks.
2056 if( sym->data.namelen == 0 )
2058 break;
2061 if( sym->data.seg == 0 || sym->data.seg > nsect )
2063 break;
2067 * Global and local data symbols. We don't associate these
2068 * with any given source file.
2071 memcpy(symname, sym->data.name, sym->data.namelen);
2072 new_addr.seg = 0;
2073 new_addr.type = DEBUG_GetCVType(sym->data.symtype);
2074 new_addr.off = (unsigned int) deefer->load_addr +
2075 sectp[sym->data.seg - 1].VirtualAddress +
2076 sym->data.offset;
2077 DEBUG_AddSymbol( symname, &new_addr, NULL, SYM_WIN32 | SYM_DATA );
2078 break;
2079 case S_GDATA_32:
2080 case S_LDATA_32:
2081 case S_PUB_32:
2083 * First, a couple of sanity checks.
2085 if( sym->data32.namelen == 0 )
2087 break;
2090 if( sym->data32.seg == 0 || sym->data32.seg > nsect )
2092 break;
2096 * Global and local data symbols. We don't associate these
2097 * with any given source file.
2100 memcpy(symname, sym->data32.name, sym->data32.namelen);
2101 new_addr.seg = 0;
2102 new_addr.type = DEBUG_GetCVType(sym->data32.symtype);
2103 new_addr.off = (unsigned int) deefer->load_addr +
2104 sectp[sym->data32.seg - 1].VirtualAddress +
2105 sym->data32.offset;
2106 DEBUG_AddSymbol( symname, &new_addr, NULL, SYM_WIN32 | SYM_DATA );
2107 break;
2108 case S_THUNK:
2110 * Sort of like a global function, but it just points
2111 * to a thunk, which is a stupid name for what amounts to
2112 * a PLT slot in the normal jargon that everyone else uses.
2114 memcpy(symname, sym->thunk.name, sym->thunk.namelen);
2115 new_addr.seg = 0;
2116 new_addr.type = NULL;
2117 new_addr.off = (unsigned int) deefer->load_addr +
2118 sectp[sym->thunk.segment - 1].VirtualAddress +
2119 sym->thunk.offset;
2120 thunk_sym = DEBUG_AddSymbol( symname, &new_addr, NULL,
2121 SYM_WIN32 | SYM_FUNC);
2122 DEBUG_SetSymbolSize(thunk_sym, sym->thunk.thunk_len);
2123 break;
2124 case S_GPROC:
2125 case S_LPROC:
2127 * Global and static functions.
2129 memcpy(symname, sym->proc.name, sym->proc.namelen);
2130 new_addr.seg = 0;
2131 new_addr.type = DEBUG_GetCVType(sym->proc.proctype);
2132 new_addr.off = (unsigned int) deefer->load_addr +
2133 sectp[sym->proc.segment - 1].VirtualAddress +
2134 sym->proc.offset;
2136 * See if we can find a segment that this goes with. If so,
2137 * it means that we also may have line number information
2138 * for this function.
2140 for(i=0; linetab[i].linetab != NULL; i++)
2142 if( ((unsigned int) deefer->load_addr
2143 + sectp[linetab[i].segno - 1].VirtualAddress
2144 + linetab[i].start <= new_addr.off)
2145 && ((unsigned int) deefer->load_addr
2146 + sectp[linetab[i].segno - 1].VirtualAddress
2147 + linetab[i].end > new_addr.off) )
2149 break;
2153 DEBUG_Normalize(curr_func);
2154 if( linetab[i].linetab == NULL )
2156 curr_func = DEBUG_AddSymbol( symname, &new_addr, NULL,
2157 SYM_WIN32 | SYM_FUNC);
2159 else
2162 * First, create the entry. Then dig through the linetab
2163 * and add whatever line numbers are appropriate for this
2164 * function.
2166 curr_func = DEBUG_AddSymbol( symname, &new_addr,
2167 linetab[i].sourcefile,
2168 SYM_WIN32 | SYM_FUNC);
2169 for(j=0; j < linetab[i].nline; j++)
2171 if( linetab[i].offtab[j] >= sym->proc.offset
2172 && linetab[i].offtab[j] < sym->proc.offset
2173 + sym->proc.proc_len )
2175 DEBUG_AddLineNumber(curr_func, linetab[i].linetab[j],
2176 linetab[i].offtab[j] - sym->proc.offset);
2183 * Add information about where we should set breakpoints
2184 * in this function.
2186 DEBUG_SetSymbolBPOff(curr_func, sym->proc.debug_start);
2187 DEBUG_SetSymbolSize(curr_func, sym->proc.proc_len);
2188 break;
2189 case S_GPROC_32:
2190 case S_LPROC_32:
2192 * Global and static functions.
2194 memcpy(symname, sym->proc32.name, sym->proc32.namelen);
2195 new_addr.seg = 0;
2196 new_addr.type = DEBUG_GetCVType(sym->proc32.proctype);
2197 new_addr.off = (unsigned int) deefer->load_addr +
2198 sectp[sym->proc32.segment - 1].VirtualAddress +
2199 sym->proc32.offset;
2201 * See if we can find a segment that this goes with. If so,
2202 * it means that we also may have line number information
2203 * for this function.
2205 for(i=0; linetab[i].linetab != NULL; i++)
2207 if( ((unsigned int) deefer->load_addr
2208 + sectp[linetab[i].segno - 1].VirtualAddress
2209 + linetab[i].start <= new_addr.off)
2210 && ((unsigned int) deefer->load_addr
2211 + sectp[linetab[i].segno - 1].VirtualAddress
2212 + linetab[i].end > new_addr.off) )
2214 break;
2218 DEBUG_Normalize(curr_func);
2219 if( linetab[i].linetab == NULL )
2221 curr_func = DEBUG_AddSymbol( symname, &new_addr, NULL,
2222 SYM_WIN32 | SYM_FUNC);
2224 else
2227 * First, create the entry. Then dig through the linetab
2228 * and add whatever line numbers are appropriate for this
2229 * function.
2231 curr_func = DEBUG_AddSymbol( symname, &new_addr,
2232 linetab[i].sourcefile,
2233 SYM_WIN32 | SYM_FUNC);
2234 for(j=0; j < linetab[i].nline; j++)
2236 if( linetab[i].offtab[j] >= sym->proc32.offset
2237 && linetab[i].offtab[j] < sym->proc32.offset
2238 + sym->proc32.proc_len )
2240 DEBUG_AddLineNumber(curr_func, linetab[i].linetab[j],
2241 linetab[i].offtab[j] - sym->proc32.offset);
2248 * Add information about where we should set breakpoints
2249 * in this function.
2251 DEBUG_SetSymbolBPOff(curr_func, sym->proc32.debug_start);
2252 DEBUG_SetSymbolSize(curr_func, sym->proc32.proc_len);
2253 break;
2254 case S_BPREL:
2256 * Function parameters and stack variables.
2258 memcpy(symname, sym->stack.name, sym->stack.namelen);
2259 curr_sym = DEBUG_AddLocal(curr_func,
2261 sym->stack.offset,
2264 symname);
2265 DEBUG_SetLocalSymbolType(curr_sym, DEBUG_GetCVType(sym->stack.symtype));
2267 break;
2268 case S_BPREL_32:
2270 * Function parameters and stack variables.
2272 memcpy(symname, sym->stack32.name, sym->stack32.namelen);
2273 curr_sym = DEBUG_AddLocal(curr_func,
2275 sym->stack32.offset,
2278 symname);
2279 DEBUG_SetLocalSymbolType(curr_sym, DEBUG_GetCVType(sym->stack32.symtype));
2281 break;
2282 default:
2283 break;
2287 * Adjust pointer to point to next entry, rounding up to a word
2288 * boundary. MS preserving alignment? Stranger things have
2289 * happened.
2291 if( sym->generic.id == S_PROCREF
2292 || sym->generic.id == S_DATAREF
2293 || sym->generic.id == S_LPROCREF )
2295 len = (sym->generic.len + 3) & ~3;
2296 len += ptr.c[16] + 1;
2297 ptr.c += (len + 3) & ~3;
2299 else
2301 ptr.c += (sym->generic.len + 3) & ~3;
2305 if( linetab != NULL )
2307 DBG_free(linetab);
2310 return TRUE;
2315 * Process PDB file which contains debug information.
2318 #pragma pack(1)
2319 typedef struct _PDB_FILE
2321 DWORD size;
2322 DWORD unknown;
2324 } PDB_FILE, *PPDB_FILE;
2326 typedef struct _PDB_HEADER
2328 CHAR ident[40];
2329 DWORD signature;
2330 DWORD blocksize;
2331 WORD freelist;
2332 WORD total_alloc;
2333 PDB_FILE toc;
2334 WORD toc_block[ 1 ];
2336 } PDB_HEADER, *PPDB_HEADER;
2338 typedef struct _PDB_TOC
2340 DWORD nFiles;
2341 PDB_FILE file[ 1 ];
2343 } PDB_TOC, *PPDB_TOC;
2345 typedef struct _PDB_ROOT
2347 DWORD version;
2348 DWORD TimeDateStamp;
2349 DWORD unknown;
2350 DWORD cbNames;
2351 CHAR names[ 1 ];
2353 } PDB_ROOT, *PPDB_ROOT;
2355 typedef struct _PDB_TYPES_OLD
2357 DWORD version;
2358 WORD first_index;
2359 WORD last_index;
2360 DWORD type_size;
2361 WORD file;
2362 WORD pad;
2364 } PDB_TYPES_OLD, *PPDB_TYPES_OLD;
2366 typedef struct _PDB_TYPES
2368 DWORD version;
2369 DWORD type_offset;
2370 DWORD first_index;
2371 DWORD last_index;
2372 DWORD type_size;
2373 WORD file;
2374 WORD pad;
2375 DWORD hash_size;
2376 DWORD hash_base;
2377 DWORD hash_offset;
2378 DWORD hash_len;
2379 DWORD search_offset;
2380 DWORD search_len;
2381 DWORD unknown_offset;
2382 DWORD unknown_len;
2384 } PDB_TYPES, *PPDB_TYPES;
2386 typedef struct _PDB_SYMBOL_RANGE
2388 WORD segment;
2389 WORD pad1;
2390 DWORD offset;
2391 DWORD size;
2392 DWORD characteristics;
2393 WORD index;
2394 WORD pad2;
2396 } PDB_SYMBOL_RANGE, *PPDB_SYMBOL_RANGE;
2398 typedef struct _PDB_SYMBOL_RANGE_EX
2400 WORD segment;
2401 WORD pad1;
2402 DWORD offset;
2403 DWORD size;
2404 DWORD characteristics;
2405 WORD index;
2406 WORD pad2;
2407 DWORD timestamp;
2408 DWORD unknown;
2410 } PDB_SYMBOL_RANGE_EX, *PPDB_SYMBOL_RANGE_EX;
2412 typedef struct _PDB_SYMBOL_FILE
2414 DWORD unknown1;
2415 PDB_SYMBOL_RANGE range;
2416 WORD flag;
2417 WORD file;
2418 DWORD symbol_size;
2419 DWORD lineno_size;
2420 DWORD unknown2;
2421 DWORD nSrcFiles;
2422 DWORD attribute;
2423 CHAR filename[ 1 ];
2425 } PDB_SYMBOL_FILE, *PPDB_SYMBOL_FILE;
2427 typedef struct _PDB_SYMBOL_FILE_EX
2429 DWORD unknown1;
2430 PDB_SYMBOL_RANGE_EX range;
2431 WORD flag;
2432 WORD file;
2433 DWORD symbol_size;
2434 DWORD lineno_size;
2435 DWORD unknown2;
2436 DWORD nSrcFiles;
2437 DWORD attribute;
2438 DWORD reserved[ 2 ];
2439 CHAR filename[ 1 ];
2441 } PDB_SYMBOL_FILE_EX, *PPDB_SYMBOL_FILE_EX;
2443 typedef struct _PDB_SYMBOL_SOURCE
2445 WORD nModules;
2446 WORD nSrcFiles;
2447 WORD table[ 1 ];
2449 } PDB_SYMBOL_SOURCE, *PPDB_SYMBOL_SOURCE;
2451 typedef struct _PDB_SYMBOL_IMPORT
2453 DWORD unknown1;
2454 DWORD unknown2;
2455 DWORD TimeDateStamp;
2456 DWORD nRequests;
2457 CHAR filename[ 1 ];
2459 } PDB_SYMBOL_IMPORT, *PPDB_SYMBOL_IMPORT;
2461 typedef struct _PDB_SYMBOLS_OLD
2463 WORD hash1_file;
2464 WORD hash2_file;
2465 WORD gsym_file;
2466 WORD pad;
2467 DWORD module_size;
2468 DWORD offset_size;
2469 DWORD hash_size;
2470 DWORD srcmodule_size;
2472 } PDB_SYMBOLS_OLD, *PPDB_SYMBOLS_OLD;
2474 typedef struct _PDB_SYMBOLS
2476 DWORD signature;
2477 DWORD version;
2478 DWORD extended_format;
2479 DWORD hash1_file;
2480 DWORD hash2_file;
2481 DWORD gsym_file;
2482 DWORD module_size;
2483 DWORD offset_size;
2484 DWORD hash_size;
2485 DWORD srcmodule_size;
2486 DWORD pdbimport_size;
2487 DWORD resvd[ 5 ];
2489 } PDB_SYMBOLS, *PPDB_SYMBOLS;
2490 #pragma pack()
2493 static void *pdb_read( LPBYTE image, WORD *block_list, int size )
2495 PPDB_HEADER pdb = (PPDB_HEADER)image;
2496 int i, nBlocks;
2497 LPBYTE buffer;
2499 if ( !size ) return NULL;
2501 nBlocks = (size + pdb->blocksize-1) / pdb->blocksize;
2502 buffer = DBG_alloc( nBlocks * pdb->blocksize );
2504 for ( i = 0; i < nBlocks; i++ )
2505 memcpy( buffer + i*pdb->blocksize,
2506 image + block_list[i]*pdb->blocksize, pdb->blocksize );
2508 return buffer;
2511 static void *pdb_read_file( LPBYTE image, PPDB_TOC toc, int fileNr )
2513 PPDB_HEADER pdb = (PPDB_HEADER)image;
2514 WORD *block_list;
2515 int i;
2517 if ( !toc || fileNr >= toc->nFiles )
2518 return NULL;
2520 block_list = (WORD *) &toc->file[ toc->nFiles ];
2521 for ( i = 0; i < fileNr; i++ )
2522 block_list += (toc->file[i].size + pdb->blocksize-1) / pdb->blocksize;
2524 return pdb_read( image, block_list, toc->file[fileNr].size );
2527 static void pdb_free( void *buffer )
2529 DBG_free( buffer );
2532 static void pdb_convert_types_header( PDB_TYPES *types, char *image )
2534 memset( types, 0, sizeof(PDB_TYPES) );
2535 if ( !image ) return;
2537 if ( *(DWORD *)image < 19960000 ) /* FIXME: correct version? */
2539 /* Old version of the types record header */
2540 PDB_TYPES_OLD *old = (PDB_TYPES_OLD *)image;
2541 types->version = old->version;
2542 types->type_offset = sizeof(PDB_TYPES_OLD);
2543 types->type_size = old->type_size;
2544 types->first_index = old->first_index;
2545 types->last_index = old->last_index;
2546 types->file = old->file;
2548 else
2550 /* New version of the types record header */
2551 *types = *(PDB_TYPES *)image;
2555 static void pdb_convert_symbols_header( PDB_SYMBOLS *symbols,
2556 int *header_size, char *image )
2558 memset( symbols, 0, sizeof(PDB_SYMBOLS) );
2559 if ( !image ) return;
2561 if ( *(DWORD *)image != 0xffffffff )
2563 /* Old version of the symbols record header */
2564 PDB_SYMBOLS_OLD *old = (PDB_SYMBOLS_OLD *)image;
2565 symbols->version = 0;
2566 symbols->extended_format = 0;
2567 symbols->module_size = old->module_size;
2568 symbols->offset_size = old->offset_size;
2569 symbols->hash_size = old->hash_size;
2570 symbols->srcmodule_size = old->srcmodule_size;
2571 symbols->pdbimport_size = 0;
2572 symbols->hash1_file = old->hash1_file;
2573 symbols->hash2_file = old->hash2_file;
2574 symbols->gsym_file = old->gsym_file;
2576 *header_size = sizeof(PDB_SYMBOLS_OLD);
2578 else
2580 /* New version of the symbols record header */
2581 *symbols = *(PDB_SYMBOLS *)image;
2583 *header_size = sizeof(PDB_SYMBOLS);
2587 int DEBUG_ProcessPDBFile( struct deferred_debug_info *deefer, char *full_filename )
2589 char filename[MAX_PATHNAME_LEN];
2590 struct stat statbuf;
2591 int fd = -1;
2592 char *image = (char *) 0xffffffff;
2593 PDB_HEADER *pdb = NULL;
2594 PDB_TOC *toc = NULL;
2595 PDB_ROOT *root = NULL;
2596 char *types_image = NULL;
2597 char *symbols_image = NULL;
2598 PDB_TYPES types;
2599 PDB_SYMBOLS symbols;
2600 int header_size = 0;
2601 char *modimage, *file;
2605 * Open and mmap() .PDB file
2608 LocateDebugInfoFile( full_filename, filename );
2610 if ( stat( filename, &statbuf ) == -1 )
2612 fprintf( stderr, "-Unable to open .PDB file %s\n", filename );
2613 goto leave;
2616 fd = open(filename, O_RDONLY);
2617 if ( fd == -1 )
2619 fprintf( stderr, "-Unable to open .PDB file %s\n", filename );
2620 goto leave;
2623 image = mmap( 0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0 );
2624 if ( image == (char *) 0xffffffff )
2626 fprintf(stderr, "-Unable to mmap .PDB file %s\n", filename);
2627 goto leave;
2631 * Read in TOC and well-known files
2634 pdb = (PPDB_HEADER)image;
2635 toc = pdb_read( image, pdb->toc_block, pdb->toc.size );
2636 root = pdb_read_file( image, toc, 1 );
2637 types_image = pdb_read_file( image, toc, 2 );
2638 symbols_image = pdb_read_file( image, toc, 3 );
2640 pdb_convert_types_header( &types, types_image );
2641 pdb_convert_symbols_header( &symbols, &header_size, symbols_image );
2644 * Check for unknown versions
2647 switch ( root->version )
2649 case 19950623: /* VC 4.0 */
2650 case 19950814:
2651 case 19960307: /* VC 5.0 */
2652 case 19970604: /* VC 6.0 */
2653 break;
2654 default:
2655 fprintf( stderr, "-Unknown root block version %ld\n", root->version );
2658 switch ( types.version )
2660 case 19950410: /* VC 4.0 */
2661 case 19951122:
2662 case 19961031: /* VC 5.0 / 6.0 */
2663 break;
2664 default:
2665 fprintf( stderr, "-Unknown type info version %ld\n", types.version );
2668 switch ( symbols.version )
2670 case 0: /* VC 4.0 */
2671 case 19960307: /* VC 5.0 */
2672 case 19970606: /* VC 6.0 */
2673 break;
2674 default:
2675 fprintf( stderr, "-Unknown symbol info version %ld\n", symbols.version );
2680 * Check .PDB time stamp
2683 if ( root->TimeDateStamp
2684 != ((struct CodeViewDebug *)deefer->dbg_info)->cv_timestamp )
2686 fprintf(stderr, "-Wrong time stamp of .PDB file %s\n", filename);
2687 goto leave;
2691 * Read type table
2694 DEBUG_ParseTypeTable( types_image + types.type_offset, types.type_size );
2697 * Read type-server .PDB imports
2700 if ( symbols.pdbimport_size )
2702 /* FIXME */
2703 fprintf(stderr, "-Type server .PDB imports ignored!\n" );
2707 * Read global symbol table
2710 modimage = pdb_read_file( image, toc, symbols.gsym_file );
2711 if ( modimage )
2713 DEBUG_SnarfCodeView( deefer, modimage,
2714 toc->file[symbols.gsym_file].size, NULL );
2715 pdb_free( modimage );
2719 * Read per-module symbol / linenumber tables
2722 file = symbols_image + header_size;
2723 while ( file - symbols_image < header_size + symbols.module_size )
2725 int file_nr, file_index, symbol_size, lineno_size;
2726 char *file_name;
2728 if ( !symbols.extended_format )
2730 PDB_SYMBOL_FILE *sym_file = (PDB_SYMBOL_FILE *) file;
2731 file_nr = sym_file->file;
2732 file_name = sym_file->filename;
2733 file_index = sym_file->range.index;
2734 symbol_size = sym_file->symbol_size;
2735 lineno_size = sym_file->lineno_size;
2737 else
2739 PDB_SYMBOL_FILE_EX *sym_file = (PDB_SYMBOL_FILE_EX *) file;
2740 file_nr = sym_file->file;
2741 file_name = sym_file->filename;
2742 file_index = sym_file->range.index;
2743 symbol_size = sym_file->symbol_size;
2744 lineno_size = sym_file->lineno_size;
2747 modimage = pdb_read_file( image, toc, file_nr );
2748 if ( modimage )
2750 struct codeview_linetab_hdr *linetab = NULL;
2752 if ( lineno_size )
2753 linetab = DEBUG_SnarfLinetab( modimage + symbol_size, lineno_size );
2755 if ( symbol_size )
2756 DEBUG_SnarfCodeView( deefer, modimage + sizeof(DWORD),
2757 symbol_size - sizeof(DWORD), linetab );
2759 pdb_free( modimage );
2762 file_name += strlen(file_name) + 1;
2763 file = (char *)( (DWORD)(file_name + strlen(file_name) + 1 + 3) & ~3 );
2767 leave:
2770 * Cleanup
2773 if ( symbols_image ) pdb_free( symbols_image );
2774 if ( types_image ) pdb_free( types_image );
2775 if ( root ) pdb_free( root );
2776 if ( toc ) pdb_free( toc );
2778 if ( image != (char *) 0xffffffff ) munmap( image, statbuf.st_size );
2779 if ( fd != -1 ) close( fd );
2781 return TRUE;
2786 * Process DBG file which contains debug information.
2788 /* static */
2790 DEBUG_ProcessDBGFile(struct deferred_debug_info * deefer, char * filename)
2792 char * addr = (char *) 0xffffffff;
2793 char * codeview;
2794 struct CV4_DirHead * codeview_dir;
2795 struct CV4_DirEnt * codeview_dent;
2796 PIMAGE_DEBUG_DIRECTORY dbghdr;
2797 struct deferred_debug_info deefer2;
2798 int fd = -1;
2799 int i;
2800 int j;
2801 struct codeview_linetab_hdr * linetab;
2802 int nsect;
2803 PIMAGE_SEPARATE_DEBUG_HEADER pdbg = NULL;
2804 IMAGE_SECTION_HEADER * sectp;
2805 struct stat statbuf;
2806 int status;
2807 char dbg_file[MAX_PATHNAME_LEN];
2809 LocateDebugInfoFile(filename, dbg_file);
2810 status = stat(dbg_file, &statbuf);
2811 if( status == -1 )
2813 fprintf(stderr, "-Unable to open .DBG file %s\n", dbg_file);
2814 goto leave;
2818 * Now open the file, so that we can mmap() it.
2820 fd = open(dbg_file, O_RDONLY);
2821 if( fd == -1 )
2823 fprintf(stderr, "Unable to open .DBG file %s\n", dbg_file);
2824 goto leave;
2829 * Now mmap() the file.
2831 addr = mmap(0, statbuf.st_size, PROT_READ,
2832 MAP_PRIVATE, fd, 0);
2833 if( addr == (char *) 0xffffffff )
2835 fprintf(stderr, "Unable to mmap .DBG file %s\n", dbg_file);
2836 goto leave;
2839 pdbg = (PIMAGE_SEPARATE_DEBUG_HEADER) addr;
2841 if( pdbg->TimeDateStamp != deefer->dbgdir->TimeDateStamp )
2843 fprintf(stderr, "Warning - %s has incorrect internal timestamp\n",
2844 dbg_file);
2845 /* goto leave; */
2847 Well, sometimes this happens to DBG files which ARE REALLY the right .DBG
2848 files but nonetheless this check fails. Anyway, WINDBG (debugger for
2849 Windows by Microsoft) loads debug symbols which have incorrect timestamps.
2853 fprintf(stderr, "Processing symbols from %s...\n", dbg_file);
2855 dbghdr = (PIMAGE_DEBUG_DIRECTORY) ( addr + sizeof(*pdbg)
2856 + pdbg->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)
2857 + pdbg->ExportedNamesSize);
2859 sectp = (PIMAGE_SECTION_HEADER) ((char *) pdbg + sizeof(*pdbg));
2860 nsect = pdbg->NumberOfSections;
2862 for( i=0; i < pdbg->DebugDirectorySize / sizeof(*pdbg); i++, dbghdr++ )
2864 switch(dbghdr->Type)
2866 case IMAGE_DEBUG_TYPE_COFF:
2868 * Dummy up a deferred debug header to handle the
2869 * COFF stuff embedded within the DBG file.
2871 memset((char *) &deefer2, 0, sizeof(deefer2));
2872 deefer2.dbg_info = (addr + dbghdr->PointerToRawData);
2873 deefer2.dbg_size = dbghdr->SizeOfData;
2874 deefer2.load_addr = deefer->load_addr;
2876 DEBUG_ProcessCoff(&deefer2);
2877 break;
2878 case IMAGE_DEBUG_TYPE_CODEVIEW:
2880 * This is the older format by which codeview stuff is
2881 * stored, known as the 'NB09' format. Newer executables
2882 * and dlls created by VC++ use PDB files instead, which
2883 * have lots of internal similarities, but the overall
2884 * format and structure is quite different.
2886 codeview = (addr + dbghdr->PointerToRawData);
2889 * The first thing in the codeview section should be
2890 * an 'NB09' identifier. As a sanity check, make sure
2891 * it is there.
2893 if( *((unsigned int*) codeview) != 0x3930424e )
2895 break;
2899 * Next we need to find the directory. This is easy too.
2901 codeview_dir = (struct CV4_DirHead *)
2902 (codeview + ((unsigned int*) codeview)[1]);
2905 * Some more sanity checks. Make sure that everything
2906 * is as we expect it.
2908 if( codeview_dir->next_offset != 0
2909 || codeview_dir->dhsize != sizeof(*codeview_dir)
2910 || codeview_dir->desize != sizeof(*codeview_dent) )
2912 break;
2914 codeview_dent = (struct CV4_DirEnt *) (codeview_dir + 1);
2916 for(j=0; j < codeview_dir->ndir; j++, codeview_dent++)
2918 if( codeview_dent->subsect_number == sstAlignSym )
2921 * Check the previous entry. If it is a
2922 * sstSrcModule, it contains the line number
2923 * info for this file.
2925 linetab = NULL;
2926 if( codeview_dent[1].module_number == codeview_dent[0].module_number
2927 && codeview_dent[1].subsect_number == sstSrcModule )
2929 linetab = DEBUG_SnarfLinetab(
2930 codeview + codeview_dent[1].offset,
2931 codeview_dent[1].size);
2934 if( codeview_dent[-1].module_number == codeview_dent[0].module_number
2935 && codeview_dent[-1].subsect_number == sstSrcModule )
2937 linetab = DEBUG_SnarfLinetab(
2938 codeview + codeview_dent[-1].offset,
2939 codeview_dent[-1].size);
2942 * Now process the CV stuff.
2944 DEBUG_SnarfCodeView(deefer,
2945 codeview + codeview_dent->offset + sizeof(DWORD),
2946 codeview_dent->size - sizeof(DWORD),
2947 linetab);
2951 break;
2952 default:
2953 break;
2956 leave:
2958 if( addr != (char *) 0xffffffff )
2960 munmap(addr, statbuf.st_size);
2963 if( fd != -1 )
2965 close(fd);
2968 return TRUE;
2972 DEBUG_ProcessDeferredDebug()
2974 struct deferred_debug_info * deefer;
2975 struct CodeViewDebug * cvd;
2976 struct MiscDebug * misc;
2977 char * filename;
2978 int last_proc = -1;
2979 int need_print =0;
2980 int sts;
2982 for(deefer = dbglist; deefer; deefer = deefer->next)
2984 if( deefer->status != DF_STATUS_NEW )
2986 continue;
2989 if( last_proc != deefer->dbg_index )
2991 if (!need_print)
2993 fprintf(stderr, "DeferredDebug for:");
2994 need_print=1;
2996 fprintf(stderr, " %s",deefer->module_name);
2997 last_proc = deefer->dbg_index;
3000 switch(deefer->dbgdir->Type)
3002 case IMAGE_DEBUG_TYPE_COFF:
3004 * Standard COFF debug information that VC++ adds when you
3005 * use /debugtype:both with the linker.
3007 #if 0
3008 fprintf(stderr, "Processing COFF symbols...\n");
3009 #endif
3010 sts = DEBUG_ProcessCoff(deefer);
3011 break;
3012 case IMAGE_DEBUG_TYPE_CODEVIEW:
3014 * This is a pointer to a PDB file of some sort.
3016 cvd = (struct CodeViewDebug *) deefer->dbg_info;
3018 if( strcmp(cvd->cv_nbtype, "NB10") != 0 )
3021 * Whatever this is, we don't know how to deal with
3022 * it yet.
3024 sts = FALSE;
3025 break;
3027 sts = DEBUG_ProcessPDBFile(deefer, cvd->cv_name);
3028 #if 0
3029 fprintf(stderr, "Processing PDB file %s\n", cvd->cv_name);
3030 #endif
3031 break;
3032 case IMAGE_DEBUG_TYPE_MISC:
3034 * A pointer to a .DBG file of some sort. These files
3035 * can contain either CV4 or COFF information. Open
3036 * the file, and try to do the right thing with it.
3038 misc = (struct MiscDebug *) deefer->dbg_info;
3040 filename = strrchr((char *) &misc->Data, '.');
3043 * Ignore the file if it doesn't have a .DBG extension.
3045 if( (filename == NULL)
3046 || ( (strcmp(filename, ".dbg") != 0)
3047 && (strcmp(filename, ".DBG") != 0)) )
3049 sts = FALSE;
3050 break;
3053 filename = (char *) &misc->Data;
3056 * Do the dirty deed...
3058 sts = DEBUG_ProcessDBGFile(deefer, filename);
3060 break;
3061 default:
3063 * We should never get here...
3065 sts = FALSE;
3066 break;
3068 deefer->status = (sts) ? DF_STATUS_LOADED : DF_STATUS_ERROR;
3071 if(need_print)
3072 fprintf(stderr, "\n");
3073 return TRUE;
3077 /***********************************************************************
3078 * DEBUG_InfoShare
3080 * Display shared libarary information.
3082 void DEBUG_InfoShare(void)
3084 struct deferred_debug_info * deefer;
3086 fprintf(stderr,"Address\t\tModule\tName\n");
3088 for(deefer = dbglist; deefer; deefer = deefer->next)
3090 fprintf(stderr,"0x%8.8x\t(%s)\t%s\n", (unsigned int) deefer->load_addr,
3091 deefer->module ? "Win32" : "ELF", deefer->module_name);