2 * File msc.c - read VC++ debug information from COFF and eventually
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.
14 * Get 16 bit CV stuff working.
15 * Add symbol size to internal symbol table.
22 #include <sys/types.h>
23 #ifdef HAVE_SYS_MMAN_H
32 #define PATH_MAX _MAX_PATH
40 IMAGE_DEBUG_DIRECTORY dbgdir
;
47 #define MSC_INFO(module) ((MSC_DBG_INFO*)((module)->extra_info))
50 * dbg_filename must be at least MAX_PATHNAME_LEN bytes in size
52 static void LocateDebugInfoFile(char *filename
, char *dbg_filename
)
54 char *str1
= DBG_alloc(MAX_PATHNAME_LEN
*10);
55 char *str2
= DBG_alloc(MAX_PATHNAME_LEN
);
58 DOS_FULL_NAME fullname
;
60 file
= strrchr(filename
, '\\');
61 if( file
== NULL
) file
= filename
; else file
++;
63 if (GetEnvironmentVariableA("_NT_SYMBOL_PATH", str1
, MAX_PATHNAME_LEN
))
64 if (SearchPathA(str1
, file
, NULL
, MAX_PATHNAME_LEN
*10, str2
, &name_part
))
66 if (GetEnvironmentVariableA("_NT_ALT_SYMBOL_PATH", str1
, MAX_PATHNAME_LEN
))
67 if (SearchPathA(str1
, file
, NULL
, MAX_PATHNAME_LEN
*10, str2
, &name_part
))
69 if (SearchPathA(NULL
, file
, NULL
, MAX_PATHNAME_LEN
*10, str2
, &name_part
))
74 memcpy(dbg_filename
, filename
, MAX_PATHNAME_LEN
);
80 if (DOSFS_GetFullName(str2
, TRUE
, &fullname
))
81 memcpy(dbg_filename
, fullname
.long_name
, MAX_PATHNAME_LEN
);
98 * This is a convenience structure used to map portions of the
108 * This is how we reference the various record types.
110 union codeview_symbol
124 unsigned short symtype
;
125 unsigned char namelen
;
126 unsigned char name
[1];
133 unsigned int symtype
;
136 unsigned char namelen
;
137 unsigned char name
[1];
144 unsigned int pparent
;
148 unsigned short segment
;
149 unsigned short thunk_len
;
150 unsigned char thtype
;
151 unsigned char namelen
;
152 unsigned char name
[1];
159 unsigned int pparent
;
162 unsigned int proc_len
;
163 unsigned int debug_start
;
164 unsigned int debug_end
;
166 unsigned short segment
;
167 unsigned short proctype
;
169 unsigned char namelen
;
170 unsigned char name
[1];
177 unsigned int pparent
;
180 unsigned int proc_len
;
181 unsigned int debug_start
;
182 unsigned int debug_end
;
183 unsigned int proctype
;
185 unsigned short segment
;
187 unsigned char namelen
;
188 unsigned char name
[1];
193 short int len
; /* Total length of this entry */
194 short int id
; /* Always S_BPREL32 */
195 unsigned int offset
; /* Stack offset relative to BP */
196 unsigned short symtype
;
197 unsigned char namelen
;
198 unsigned char name
[1];
203 short int len
; /* Total length of this entry */
204 short int id
; /* Always S_BPREL32 */
205 unsigned int offset
; /* Stack offset relative to BP */
206 unsigned int symtype
;
207 unsigned char namelen
;
208 unsigned char name
[1];
227 unsigned char variant
[1];
234 unsigned int datatype
;
235 unsigned int attribute
;
236 unsigned char variant
[1];
244 unsigned char bitoff
;
254 unsigned char bitoff
;
263 unsigned char arrlen
;
264 unsigned char namelen
;
265 unsigned char name
[1];
272 unsigned int elemtype
;
273 unsigned int idxtype
;
274 unsigned char arrlen
;
275 unsigned char namelen
;
276 unsigned char name
[1];
288 unsigned short structlen
;
289 unsigned char namelen
;
290 unsigned char name
[1];
299 unsigned int fieldlist
;
300 unsigned int derived
;
302 unsigned short structlen
;
303 unsigned char namelen
;
304 unsigned char name
[1];
314 unsigned short un_len
;
315 unsigned char namelen
;
316 unsigned char name
[1];
326 unsigned short un_len
;
327 unsigned char namelen
;
328 unsigned char name
[1];
339 unsigned char namelen
;
340 unsigned char name
[1];
351 unsigned char namelen
;
352 unsigned char name
[1];
359 unsigned short int value
;
360 unsigned char namelen
;
361 unsigned char name
[1];
369 unsigned short int offset
;
370 unsigned char namelen
;
371 unsigned char name
[1];
379 unsigned short int offset
;
380 unsigned char namelen
;
381 unsigned char name
[1];
385 #define S_COMPILE 0x0001
386 #define S_REGISTER 0x0002
387 #define S_CONSTANT 0x0003
389 #define S_SSEARCH 0x0005
391 #define S_SKIP 0x0007
392 #define S_CVRESERVE 0x0008
393 #define S_OBJNAME 0x0009
394 #define S_ENDARG 0x000a
395 #define S_COBOLUDT 0x000b
396 #define S_MANYREG 0x000c
397 #define S_RETURN 0x000d
398 #define S_ENTRYTHIS 0x000e
400 #define S_BPREL 0x0200
401 #define S_LDATA 0x0201
402 #define S_GDATA 0x0202
404 #define S_LPROC 0x0204
405 #define S_GPROC 0x0205
406 #define S_THUNK 0x0206
407 #define S_BLOCK 0x0207
408 #define S_WITH 0x0208
409 #define S_LABEL 0x0209
410 #define S_CEXMODEL 0x020a
411 #define S_VFTPATH 0x020b
412 #define S_REGREL 0x020c
413 #define S_LTHREAD 0x020d
414 #define S_GTHREAD 0x020e
416 #define S_PROCREF 0x0400
417 #define S_DATAREF 0x0401
418 #define S_ALIGN 0x0402
419 #define S_LPROCREF 0x0403
421 #define S_REGISTER_32 0x1001 /* Variants with new 32-bit type indices */
422 #define S_CONSTANT_32 0x1002
423 #define S_UDT_32 0x1003
424 #define S_COBOLUDT_32 0x1004
425 #define S_MANYREG_32 0x1005
427 #define S_BPREL_32 0x1006
428 #define S_LDATA_32 0x1007
429 #define S_GDATA_32 0x1008
430 #define S_PUB_32 0x1009
431 #define S_LPROC_32 0x100a
432 #define S_GPROC_32 0x100b
433 #define S_VFTTABLE_32 0x100c
434 #define S_REGREL_32 0x100d
435 #define S_LTHREAD_32 0x100e
436 #define S_GTHREAD_32 0x100f
440 * This covers the basic datatypes that VC++ seems to be using these days.
441 * 32 bit mode only. There are additional numbers for the pointers in 16
442 * bit mode. There are many other types listed in the documents, but these
443 * are apparently not used by the compiler, or represent pointer types
446 #define T_NOTYPE 0x0000 /* Notype */
447 #define T_ABS 0x0001 /* Abs */
448 #define T_VOID 0x0003 /* Void */
449 #define T_CHAR 0x0010 /* signed char */
450 #define T_SHORT 0x0011 /* short */
451 #define T_LONG 0x0012 /* long */
452 #define T_QUAD 0x0013 /* long long */
453 #define T_UCHAR 0x0020 /* unsigned char */
454 #define T_USHORT 0x0021 /* unsigned short */
455 #define T_ULONG 0x0022 /* unsigned long */
456 #define T_UQUAD 0x0023 /* unsigned long long */
457 #define T_REAL32 0x0040 /* float */
458 #define T_REAL64 0x0041 /* double */
459 #define T_RCHAR 0x0070 /* real char */
460 #define T_WCHAR 0x0071 /* wide char */
461 #define T_INT4 0x0074 /* int */
462 #define T_UINT4 0x0075 /* unsigned int */
464 #define T_32PVOID 0x0403 /* 32 bit near pointer to void */
465 #define T_32PCHAR 0x0410 /* 16:32 near pointer to signed char */
466 #define T_32PSHORT 0x0411 /* 16:32 near pointer to short */
467 #define T_32PLONG 0x0412 /* 16:32 near pointer to int */
468 #define T_32PQUAD 0x0413 /* 16:32 near pointer to long long */
469 #define T_32PUCHAR 0x0420 /* 16:32 near pointer to unsigned char */
470 #define T_32PUSHORT 0x0421 /* 16:32 near pointer to unsigned short */
471 #define T_32PULONG 0x0422 /* 16:32 near pointer to unsigned int */
472 #define T_32PUQUAD 0x0423 /* 16:32 near pointer to long long */
473 #define T_32PREAL32 0x0440 /* 16:32 near pointer to float */
474 #define T_32PREAL64 0x0441 /* 16:32 near pointer to float */
475 #define T_32PRCHAR 0x0470 /* 16:32 near pointer to real char */
476 #define T_32PWCHAR 0x0471 /* 16:32 near pointer to real char */
477 #define T_32PINT4 0x0474 /* 16:32 near pointer to int */
478 #define T_32PUINT4 0x0475 /* 16:32 near pointer to unsigned int */
480 #define LF_MODIFIER 0x0001
481 #define LF_POINTER 0x0002
482 #define LF_ARRAY 0x0003
483 #define LF_CLASS 0x0004
484 #define LF_STRUCTURE 0x0005
485 #define LF_UNION 0x0006
486 #define LF_ENUM 0x0007
487 #define LF_PROCEDURE 0x0008
488 #define LF_MFUNCTION 0x0009
489 #define LF_VTSHAPE 0x000a
490 #define LF_COBOL0 0x000b
491 #define LF_COBOL1 0x000c
492 #define LF_BARRAY 0x000d
493 #define LF_LABEL 0x000e
494 #define LF_NULL 0x000f
495 #define LF_NOTTRAN 0x0010
496 #define LF_DIMARRAY 0x0011
497 #define LF_VFTPATH 0x0012
498 #define LF_PRECOMP 0x0013
499 #define LF_ENDPRECOMP 0x0014
500 #define LF_OEM 0x0015
501 #define LF_TYPESERVER 0x0016
503 #define LF_MODIFIER_32 0x1001 /* variants with new 32-bit type indices */
504 #define LF_POINTER_32 0x1002
505 #define LF_ARRAY_32 0x1003
506 #define LF_CLASS_32 0x1004
507 #define LF_STRUCTURE_32 0x1005
508 #define LF_UNION_32 0x1006
509 #define LF_ENUM_32 0x1007
510 #define LF_PROCEDURE_32 0x1008
511 #define LF_MFUNCTION_32 0x1009
512 #define LF_COBOL0_32 0x100a
513 #define LF_BARRAY_32 0x100b
514 #define LF_DIMARRAY_32 0x100c
515 #define LF_VFTPATH_32 0x100d
516 #define LF_PRECOMP_32 0x100e
517 #define LF_OEM_32 0x100f
519 #define LF_SKIP 0x0200
520 #define LF_ARGLIST 0x0201
521 #define LF_DEFARG 0x0202
522 #define LF_LIST 0x0203
523 #define LF_FIELDLIST 0x0204
524 #define LF_DERIVED 0x0205
525 #define LF_BITFIELD 0x0206
526 #define LF_METHODLIST 0x0207
527 #define LF_DIMCONU 0x0208
528 #define LF_DIMCONLU 0x0209
529 #define LF_DIMVARU 0x020a
530 #define LF_DIMVARLU 0x020b
531 #define LF_REFSYM 0x020c
533 #define LF_SKIP_32 0x1200 /* variants with new 32-bit type indices */
534 #define LF_ARGLIST_32 0x1201
535 #define LF_DEFARG_32 0x1202
536 #define LF_FIELDLIST_32 0x1203
537 #define LF_DERIVED_32 0x1204
538 #define LF_BITFIELD_32 0x1205
539 #define LF_METHODLIST_32 0x1206
540 #define LF_DIMCONU_32 0x1207
541 #define LF_DIMCONLU_32 0x1208
542 #define LF_DIMVARU_32 0x1209
543 #define LF_DIMVARLU_32 0x120a
545 #define LF_BCLASS 0x0400
546 #define LF_VBCLASS 0x0401
547 #define LF_IVBCLASS 0x0402
548 #define LF_ENUMERATE 0x0403
549 #define LF_FRIENDFCN 0x0404
550 #define LF_INDEX 0x0405
551 #define LF_MEMBER 0x0406
552 #define LF_STMEMBER 0x0407
553 #define LF_METHOD 0x0408
554 #define LF_NESTTYPE 0x0409
555 #define LF_VFUNCTAB 0x040a
556 #define LF_FRIENDCLS 0x040b
557 #define LF_ONEMETHOD 0x040c
558 #define LF_VFUNCOFF 0x040d
559 #define LF_NESTTYPEEX 0x040e
560 #define LF_MEMBERMODIFY 0x040f
562 #define LF_BCLASS_32 0x1400 /* variants with new 32-bit type indices */
563 #define LF_VBCLASS_32 0x1401
564 #define LF_IVBCLASS_32 0x1402
565 #define LF_FRIENDFCN_32 0x1403
566 #define LF_INDEX_32 0x1404
567 #define LF_MEMBER_32 0x1405
568 #define LF_STMEMBER_32 0x1406
569 #define LF_METHOD_32 0x1407
570 #define LF_NESTTYPE_32 0x1408
571 #define LF_VFUNCTAB_32 0x1409
572 #define LF_FRIENDCLS_32 0x140a
573 #define LF_ONEMETHOD_32 0x140b
574 #define LF_VFUNCOFF_32 0x140c
575 #define LF_NESTTYPEEX_32 0x140d
580 #define MAX_BUILTIN_TYPES 0x480
581 static struct datatype
* cv_basic_types
[MAX_BUILTIN_TYPES
];
582 static int num_cv_defined_types
= 0;
583 static struct datatype
**cv_defined_types
= NULL
;
586 * For the type CODEVIEW debug directory entries, the debug directory
587 * points to a structure like this. The cv_name field is the name
588 * of an external .PDB file.
593 unsigned int cv_timestamp
;
599 unsigned int DataType
;
607 * This is the header that the COFF variety of debug header points to.
611 unsigned int SymbolOffset
;
612 unsigned int N_Linenum
;
613 unsigned int LinenumberOffset
;
614 unsigned int Unused
[4];
618 unsigned int VirtualAddr
;
619 unsigned short int Linenum
;
623 unsigned int startaddr
;
624 unsigned int endaddr
;
628 struct name_hash
**entries
;
638 unsigned int NotLong
;
639 unsigned int StrTaboff
;
646 unsigned char NumberOfAuxSymbols
;
649 struct CoffAuxSection
{
651 unsigned short NumberOfRelocations
;
652 unsigned short NumberOfLinenumbers
;
653 unsigned int CheckSum
;
659 * These two structures are used in the directory within a .DBG file
660 * to locate the individual important bits that we might want to see.
663 short unsigned int dhsize
;
664 short unsigned int desize
;
666 unsigned int next_offset
;
671 short unsigned int subsect_number
;
672 short unsigned int module_number
;
678 * These are the values of interest that the subsect_number field takes.
680 #define sstAlignSym 0x125
681 #define sstSrcModule 0x127
683 struct codeview_linetab_hdr
690 unsigned short * linetab
;
691 unsigned int * offtab
;
696 * A simple macro that tells us whether a given COFF symbol is a
699 #define N_TMASK 0x0030
700 #define IMAGE_SYM_DTYPE_FUNCTION 2
702 #define ISFCN(x) (((x) & N_TMASK) == (IMAGE_SYM_DTYPE_FUNCTION << N_BTSHFT))
706 * This is what we are looking for in the COFF symbols.
708 #define IMAGE_SYM_CLASS_EXTERNAL 0x2
709 #define IMAGE_SYM_CLASS_STATIC 0x3
710 #define IMAGE_SYM_CLASS_FILE 0x67
713 struct datatype
* DEBUG_GetCVType(unsigned int typeno
)
715 struct datatype
* dt
= NULL
;
718 * Convert Codeview type numbers into something we can grok internally.
719 * Numbers < 0x1000 are all fixed builtin types. Numbers from 0x1000 and
720 * up are all user defined (structs, etc).
722 if( typeno
< 0x1000 )
724 if( typeno
< MAX_BUILTIN_TYPES
)
726 dt
= cv_basic_types
[typeno
];
731 if( typeno
- 0x1000 < num_cv_defined_types
)
733 dt
= cv_defined_types
[typeno
- 0x1000];
741 DEBUG_ParseTypeTable(char * table
, int len
)
745 enum debug_type fieldtype
;
749 struct datatype
* subtype
;
751 union codeview_type
* type
;
752 union codeview_type
* type2
;
753 struct datatype
* typeptr
;
758 while( ptr
.c
- table
< len
)
760 type
= (union codeview_type
*) ptr
.c
;
762 if( curr_type
- 0x1000 >= num_cv_defined_types
)
764 num_cv_defined_types
+= 0x100;
765 cv_defined_types
= (struct datatype
**) DBG_realloc(cv_defined_types
,
766 num_cv_defined_types
* sizeof(struct datatype
*));
767 memset(cv_defined_types
+ num_cv_defined_types
- 0x100,
769 0x100 * sizeof(struct datatype
*));
770 if( cv_defined_types
== NULL
)
776 switch(type
->generic
.id
)
779 cv_defined_types
[curr_type
- 0x1000] =
780 DEBUG_FindOrMakePointerType(DEBUG_GetCVType(type
->pointer
.datatype
));
783 cv_defined_types
[curr_type
- 0x1000] =
784 DEBUG_FindOrMakePointerType(DEBUG_GetCVType(type
->pointer32
.datatype
));
787 if( type
->array
.arrlen
>= 0x8000 )
790 * This is a numeric leaf, I am too lazy to handle this right
793 fprintf(stderr
, "Ignoring large numberic leaf.\n");
796 if( type
->array
.namelen
!= 0 )
798 memset(symname
, 0, sizeof(symname
));
799 memcpy(symname
, type
->array
.name
, type
->array
.namelen
);
800 typeptr
= DEBUG_NewDataType(DT_ARRAY
, symname
);
804 typeptr
= DEBUG_NewDataType(DT_ARRAY
, NULL
);
806 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
808 subtype
= DEBUG_GetCVType(type
->array
.elemtype
);
809 if( (subtype
== NULL
)
810 || (elem_size
= DEBUG_GetObjectSize(subtype
)) == 0 )
816 arr_max
= type
->array
.arrlen
/ DEBUG_GetObjectSize(subtype
);
819 DEBUG_SetArrayParams(typeptr
, 0, arr_max
, subtype
);
822 if( type
->array32
.arrlen
>= 0x8000 )
825 * This is a numeric leaf, I am too lazy to handle this right
828 fprintf(stderr
, "Ignoring large numberic leaf.\n");
831 if( type
->array32
.namelen
!= 0 )
833 memset(symname
, 0, sizeof(symname
));
834 memcpy(symname
, type
->array32
.name
, type
->array32
.namelen
);
835 typeptr
= DEBUG_NewDataType(DT_ARRAY
, symname
);
839 typeptr
= DEBUG_NewDataType(DT_ARRAY
, NULL
);
841 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
843 subtype
= DEBUG_GetCVType(type
->array32
.elemtype
);
844 if( (subtype
== NULL
)
845 || (elem_size
= DEBUG_GetObjectSize(subtype
)) == 0 )
851 arr_max
= type
->array32
.arrlen
/ DEBUG_GetObjectSize(subtype
);
854 DEBUG_SetArrayParams(typeptr
, 0, arr_max
, subtype
);
858 * This is where the basic list of fields is defined for
859 * structures and classes.
861 * First, we need to look ahead and see whether we are building
862 * a fieldlist for an enum or a struct.
865 type2
= (union codeview_type
*) ptr2
.c
;
866 if( type2
->member
.id
== LF_MEMBER
)
868 typeptr
= DEBUG_NewDataType(DT_STRUCT
, NULL
);
869 fieldtype
= DT_STRUCT
;
871 else if( type2
->member
.id
== LF_ENUMERATE
)
873 typeptr
= DEBUG_NewDataType(DT_ENUM
, NULL
);
881 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
882 while( ptr2
.c
< (ptr
.c
+ ((type
->generic
.len
+ 3) & ~3)) )
884 type2
= (union codeview_type
*) ptr2
.c
;
885 if( type2
->member
.id
== LF_MEMBER
&& fieldtype
== DT_STRUCT
)
887 memset(symname
, 0, sizeof(symname
));
888 memcpy(symname
, type2
->member
.name
, type2
->member
.namelen
);
890 subtype
= DEBUG_GetCVType(type2
->member
.type
);
892 if( subtype
!= NULL
)
894 elem_size
= DEBUG_GetObjectSize(subtype
);
897 if( type2
->member
.offset
>= 0x8000 )
900 * This is a numeric leaf, I am too lazy to handle this right
903 fprintf(stderr
, "Ignoring large numberic leaf.\n");
907 DEBUG_AddStructElement(typeptr
, symname
, subtype
,
908 type2
->member
.offset
<< 3,
912 else if( type2
->member
.id
== LF_ENUMERATE
&& fieldtype
== DT_ENUM
)
914 memset(symname
, 0, sizeof(symname
));
915 memcpy(symname
, type2
->enumerate
.name
, type2
->enumerate
.namelen
);
917 if( type2
->enumerate
.value
>= 0x8000 )
920 * This is a numeric leaf, I am too lazy to handle this right
923 fprintf(stderr
, "Ignoring large numberic leaf.\n");
927 DEBUG_AddStructElement(typeptr
, symname
, NULL
,
928 type2
->enumerate
.value
, 0);
934 * Something else I have never seen before. Either wrong type of
935 * object in the fieldlist, or some other problem which I wouldn't
936 * really know how to handle until it came up.
938 fprintf(stderr
, "Unexpected entry in fieldlist\n");
943 ptr2
.c
+= ((type2
->member
.namelen
+ 9 + 3) & ~3);
946 case LF_FIELDLIST_32
:
948 * This is where the basic list of fields is defined for
949 * structures and classes.
951 * First, we need to look ahead and see whether we are building
952 * a fieldlist for an enum or a struct.
955 type2
= (union codeview_type
*) ptr2
.c
;
956 if( type2
->member32
.id
== LF_MEMBER_32
)
958 typeptr
= DEBUG_NewDataType(DT_STRUCT
, NULL
);
959 fieldtype
= DT_STRUCT
;
961 else if( type2
->member32
.id
== LF_ENUMERATE
)
963 typeptr
= DEBUG_NewDataType(DT_ENUM
, NULL
);
971 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
972 while( ptr2
.c
< (ptr
.c
+ ((type
->generic
.len
+ 3) & ~3)) )
974 type2
= (union codeview_type
*) ptr2
.c
;
975 if( type2
->member
.id
== LF_MEMBER_32
&& fieldtype
== DT_STRUCT
)
977 memset(symname
, 0, sizeof(symname
));
978 memcpy(symname
, type2
->member32
.name
, type2
->member32
.namelen
);
980 subtype
= DEBUG_GetCVType(type2
->member32
.type
);
982 if( subtype
!= NULL
)
984 elem_size
= DEBUG_GetObjectSize(subtype
);
987 if( type2
->member32
.offset
>= 0x8000 )
990 * This is a numeric leaf, I am too lazy to handle this right
993 fprintf(stderr
, "Ignoring large numberic leaf.\n");
997 DEBUG_AddStructElement(typeptr
, symname
, subtype
,
998 type2
->member32
.offset
<< 3,
1002 else if( type2
->member32
.id
== LF_ENUMERATE
&& fieldtype
== DT_ENUM
)
1004 memset(symname
, 0, sizeof(symname
));
1005 memcpy(symname
, type2
->enumerate
.name
, type2
->enumerate
.namelen
);
1007 if( type2
->enumerate
.value
>= 0x8000 )
1010 * This is a numeric leaf, I am too lazy to handle this right
1013 fprintf(stderr
, "Ignoring large numberic leaf.\n");
1017 DEBUG_AddStructElement(typeptr
, symname
, NULL
,
1018 type2
->enumerate
.value
, 0);
1024 * Something else I have never seen before. Either wrong type of
1025 * object in the fieldlist, or some other problem which I wouldn't
1026 * really know how to handle until it came up.
1028 fprintf(stderr
, "Unexpected entry in fieldlist\n");
1033 ptr2
.c
+= ((type2
->member32
.namelen
+ 9 + 3) & ~3);
1038 if( type
->structure
.structlen
>= 0x8000 )
1041 * This is a numeric leaf, I am too lazy to handle this right
1044 fprintf(stderr
, "Ignoring large numberic leaf.\n");
1047 memset(symname
, 0, sizeof(symname
));
1048 memcpy(symname
, type
->structure
.name
, type
->structure
.namelen
);
1049 if( strcmp(symname
, "__unnamed") == 0 )
1051 typeptr
= DEBUG_NewDataType(DT_STRUCT
, NULL
);
1055 typeptr
= DEBUG_NewDataType(DT_STRUCT
, symname
);
1057 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
1060 * Now copy the relevant bits from the fieldlist that we specified.
1062 subtype
= DEBUG_GetCVType(type
->structure
.fieldlist
);
1064 if( subtype
!= NULL
)
1066 DEBUG_SetStructSize(typeptr
, type
->structure
.structlen
);
1067 DEBUG_CopyFieldlist(typeptr
, subtype
);
1070 case LF_STRUCTURE_32
:
1072 if( type
->structure32
.structlen
>= 0x8000 )
1075 * This is a numeric leaf, I am too lazy to handle this right
1078 fprintf(stderr
, "Ignoring large numberic leaf.\n");
1081 memset(symname
, 0, sizeof(symname
));
1082 memcpy(symname
, type
->structure32
.name
, type
->structure32
.namelen
);
1083 if( strcmp(symname
, "__unnamed") == 0 )
1085 typeptr
= DEBUG_NewDataType(DT_STRUCT
, NULL
);
1089 typeptr
= DEBUG_NewDataType(DT_STRUCT
, symname
);
1091 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
1094 * Now copy the relevant bits from the fieldlist that we specified.
1096 subtype
= DEBUG_GetCVType(type
->structure32
.fieldlist
);
1098 if( subtype
!= NULL
)
1100 DEBUG_SetStructSize(typeptr
, type
->structure32
.structlen
);
1101 DEBUG_CopyFieldlist(typeptr
, subtype
);
1105 if( type
->t_union
.un_len
>= 0x8000 )
1108 * This is a numeric leaf, I am too lazy to handle this right
1111 fprintf(stderr
, "Ignoring large numberic leaf.\n");
1114 memset(symname
, 0, sizeof(symname
));
1115 memcpy(symname
, type
->t_union
.name
, type
->t_union
.namelen
);
1117 if( strcmp(symname
, "__unnamed") == 0 )
1119 typeptr
= DEBUG_NewDataType(DT_STRUCT
, NULL
);
1123 typeptr
= DEBUG_NewDataType(DT_STRUCT
, symname
);
1126 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
1129 * Now copy the relevant bits from the fieldlist that we specified.
1131 subtype
= DEBUG_GetCVType(type
->t_union
.field
);
1133 if( subtype
!= NULL
)
1135 DEBUG_SetStructSize(typeptr
, type
->t_union
.un_len
);
1136 DEBUG_CopyFieldlist(typeptr
, subtype
);
1140 if( type
->t_union32
.un_len
>= 0x8000 )
1143 * This is a numeric leaf, I am too lazy to handle this right
1146 fprintf(stderr
, "Ignoring large numberic leaf.\n");
1149 memset(symname
, 0, sizeof(symname
));
1150 memcpy(symname
, type
->t_union32
.name
, type
->t_union32
.namelen
);
1152 if( strcmp(symname
, "__unnamed") == 0 )
1154 typeptr
= DEBUG_NewDataType(DT_STRUCT
, NULL
);
1158 typeptr
= DEBUG_NewDataType(DT_STRUCT
, symname
);
1161 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
1164 * Now copy the relevant bits from the fieldlist that we specified.
1166 subtype
= DEBUG_GetCVType(type
->t_union32
.field
);
1168 if( subtype
!= NULL
)
1170 DEBUG_SetStructSize(typeptr
, type
->t_union32
.un_len
);
1171 DEBUG_CopyFieldlist(typeptr
, subtype
);
1175 typeptr
= DEBUG_NewDataType(DT_BITFIELD
, NULL
);
1176 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
1177 DEBUG_SetBitfieldParams(typeptr
, type
->bitfield
.bitoff
,
1178 type
->bitfield
.nbits
,
1179 DEBUG_GetCVType(type
->bitfield
.type
));
1181 case LF_BITFIELD_32
:
1182 typeptr
= DEBUG_NewDataType(DT_BITFIELD
, NULL
);
1183 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
1184 DEBUG_SetBitfieldParams(typeptr
, type
->bitfield32
.bitoff
,
1185 type
->bitfield32
.nbits
,
1186 DEBUG_GetCVType(type
->bitfield32
.type
));
1189 memset(symname
, 0, sizeof(symname
));
1190 memcpy(symname
, type
->enumeration
.name
, type
->enumeration
.namelen
);
1191 typeptr
= DEBUG_NewDataType(DT_ENUM
, symname
);
1192 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
1195 * Now copy the relevant bits from the fieldlist that we specified.
1197 subtype
= DEBUG_GetCVType(type
->enumeration
.field
);
1199 if( subtype
!= NULL
)
1201 DEBUG_CopyFieldlist(typeptr
, subtype
);
1205 memset(symname
, 0, sizeof(symname
));
1206 memcpy(symname
, type
->enumeration32
.name
, type
->enumeration32
.namelen
);
1207 typeptr
= DEBUG_NewDataType(DT_ENUM
, symname
);
1208 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
1211 * Now copy the relevant bits from the fieldlist that we specified.
1213 subtype
= DEBUG_GetCVType(type
->enumeration32
.field
);
1215 if( subtype
!= NULL
)
1217 DEBUG_CopyFieldlist(typeptr
, subtype
);
1224 ptr
.c
+= (type
->generic
.len
+ 3) & ~3;
1231 DEBUG_InitCVDataTypes(void)
1234 * These are the common builtin types that are used by VC++.
1236 cv_basic_types
[T_NOTYPE
] = NULL
;
1237 cv_basic_types
[T_ABS
] = NULL
;
1238 cv_basic_types
[T_VOID
] = DEBUG_NewDataType(DT_BASIC
, "void");
1239 cv_basic_types
[T_CHAR
] = DEBUG_NewDataType(DT_BASIC
, "char");
1240 cv_basic_types
[T_SHORT
] = DEBUG_NewDataType(DT_BASIC
, "short int");
1241 cv_basic_types
[T_LONG
] = DEBUG_NewDataType(DT_BASIC
, "long int");
1242 cv_basic_types
[T_QUAD
] = DEBUG_NewDataType(DT_BASIC
, "long long int");
1243 cv_basic_types
[T_UCHAR
] = DEBUG_NewDataType(DT_BASIC
, "unsigned char");
1244 cv_basic_types
[T_USHORT
] = DEBUG_NewDataType(DT_BASIC
, "short unsigned int");
1245 cv_basic_types
[T_ULONG
] = DEBUG_NewDataType(DT_BASIC
, "long unsigned int");
1246 cv_basic_types
[T_UQUAD
] = DEBUG_NewDataType(DT_BASIC
, "long long unsigned int");
1247 cv_basic_types
[T_REAL32
] = DEBUG_NewDataType(DT_BASIC
, "float");
1248 cv_basic_types
[T_REAL64
] = DEBUG_NewDataType(DT_BASIC
, "double");
1249 cv_basic_types
[T_RCHAR
] = DEBUG_NewDataType(DT_BASIC
, "char");
1250 cv_basic_types
[T_WCHAR
] = DEBUG_NewDataType(DT_BASIC
, "short");
1251 cv_basic_types
[T_INT4
] = DEBUG_NewDataType(DT_BASIC
, "int");
1252 cv_basic_types
[T_UINT4
] = DEBUG_NewDataType(DT_BASIC
, "unsigned int");
1254 cv_basic_types
[T_32PVOID
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_VOID
]);
1255 cv_basic_types
[T_32PCHAR
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_CHAR
]);
1256 cv_basic_types
[T_32PSHORT
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_SHORT
]);
1257 cv_basic_types
[T_32PLONG
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_LONG
]);
1258 cv_basic_types
[T_32PQUAD
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_QUAD
]);
1259 cv_basic_types
[T_32PUCHAR
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_UCHAR
]);
1260 cv_basic_types
[T_32PUSHORT
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_USHORT
]);
1261 cv_basic_types
[T_32PULONG
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_ULONG
]);
1262 cv_basic_types
[T_32PUQUAD
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_UQUAD
]);
1263 cv_basic_types
[T_32PREAL32
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_REAL32
]);
1264 cv_basic_types
[T_32PREAL64
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_REAL64
]);
1265 cv_basic_types
[T_32PRCHAR
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_RCHAR
]);
1266 cv_basic_types
[T_32PWCHAR
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_WCHAR
]);
1267 cv_basic_types
[T_32PINT4
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_INT4
]);
1268 cv_basic_types
[T_32PUINT4
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_UINT4
]);
1272 * In this function, we keep track of deferred debugging information
1273 * that we may need later if we were to need to use the internal debugger.
1274 * We don't fully process it here for performance reasons.
1277 DEBUG_RegisterMSCDebugInfo(DBG_MODULE
* module
, void* _nth
, unsigned long nth_ofs
)
1279 int has_codeview
= FALSE
;
1281 IMAGE_DEBUG_DIRECTORY dbg
;
1282 u_long v_addr
, size
, orig_size
;
1283 PIMAGE_NT_HEADERS nth
= (PIMAGE_NT_HEADERS
)_nth
;
1285 orig_size
= nth
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_DEBUG
].Size
;
1287 v_addr
= nth
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
;
1288 for(size
= orig_size
; size
>= sizeof(dbg
); size
-= sizeof(dbg
))
1290 if (!DEBUG_READ_MEM_VERBOSE((void*)(module
->load_addr
+ v_addr
), &dbg
, sizeof(dbg
))) continue;
1294 case IMAGE_DEBUG_TYPE_CODEVIEW
:
1295 case IMAGE_DEBUG_TYPE_MISC
:
1296 has_codeview
= TRUE
;
1299 v_addr
+= sizeof(dbg
);
1302 v_addr
= nth
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
;
1303 for(size
= orig_size
; size
>= sizeof(dbg
); size
-= sizeof(dbg
))
1305 if (!DEBUG_READ_MEM_VERBOSE((void*)(module
->load_addr
+ v_addr
), &dbg
, sizeof(dbg
))) continue;
1309 case IMAGE_DEBUG_TYPE_COFF
:
1311 * If we have both codeview and COFF debug info, ignore the
1312 * coff debug info as it would just confuse us, and it is
1315 * FIXME - this is broken - if we cannot find the PDB file, then
1316 * we end up with no debugging info at all. In this case, we
1317 * should use the COFF info as a backup.
1323 case IMAGE_DEBUG_TYPE_CODEVIEW
:
1324 case IMAGE_DEBUG_TYPE_MISC
:
1326 * This is usually an indirection to a .DBG file.
1327 * This is similar to (but a slightly older format) from the
1330 * First check to see if the image was 'stripped'. If so, it
1331 * means that this entry points to a .DBG file. Otherwise,
1332 * it just points to itself, and we can ignore this.
1335 if( (dbg
.Type
!= IMAGE_DEBUG_TYPE_MISC
) ||
1336 (nth
->FileHeader
.Characteristics
& IMAGE_FILE_DEBUG_STRIPPED
) != 0 )
1340 DOS_FULL_NAME full_name
;
1344 * Read the important bits. What we do after this depends
1345 * upon the type, but this is always enough so we are able
1346 * to proceed if we know what we need to do next.
1348 /* basically, the PE loader maps all sections (data, resources...), but doesn't map
1349 * the DataDirectory array's content. One its entry contains the *beloved*
1350 * debug information. (Note the DataDirectory is mapped, not its content)
1352 /* FIXME: the module->handle value is not usable in the debugger's process */
1353 if (GetModuleFileNameA(module
->handle
, fn
, sizeof(fn
)) > 0 &&
1354 DOSFS_GetFullName(fn
, TRUE
, &full_name
) &&
1355 (fd
= open(full_name
.long_name
, O_RDONLY
)) > 0)
1357 dbg_info
= mmap(NULL
, dbg
.SizeOfData
,
1358 PROT_READ
, MAP_PRIVATE
, fd
, dbg
.PointerToRawData
);
1360 if( dbg_info
== (char *) 0xffffffff ) break;
1364 fprintf(stderr
, " (not mapped: fn='%s', lfn='%s', fd=%d)\n", fn
, full_name
.long_name
, fd
);
1367 if (!(module
->extra_info
= DBG_alloc(sizeof(MSC_DBG_INFO
))))
1370 MSC_INFO(module
)->dbg_info
= dbg_info
;
1371 MSC_INFO(module
)->dbg_size
= dbg
.SizeOfData
;
1372 MSC_INFO(module
)->dbgdir
= dbg
;
1373 MSC_INFO(module
)->sect_ofs
= nth_ofs
+ OFFSET_OF(IMAGE_NT_HEADERS
, OptionalHeader
) +
1374 nth
->FileHeader
.SizeOfOptionalHeader
;
1375 MSC_INFO(module
)->nsect
= nth
->FileHeader
.NumberOfSections
;
1382 v_addr
+= sizeof(dbg
);
1384 DEBUG_CurrProcess
->next_index
++;
1390 /* look for stabs information in PE header (it's how mingw compiler provides its
1391 * debugging information
1393 int DEBUG_RegisterStabsDebugInfo(DBG_MODULE
* module
, void* _nth
, unsigned long nth_ofs
)
1395 IMAGE_SECTION_HEADER pe_seg
;
1396 unsigned long pe_seg_ofs
;
1397 int i
, stabsize
= 0, stabstrsize
= 0;
1398 unsigned int stabs
= 0, stabstr
= 0;
1400 PIMAGE_NT_HEADERS nth
= (PIMAGE_NT_HEADERS
)_nth
;
1402 pe_seg_ofs
= nth_ofs
+ OFFSET_OF(IMAGE_NT_HEADERS
, OptionalHeader
) +
1403 nth
->FileHeader
.SizeOfOptionalHeader
;
1405 for (i
= 0; i
< nth
->FileHeader
.NumberOfSections
; i
++, pe_seg_ofs
+= sizeof(pe_seg
)) {
1406 if (!DEBUG_READ_MEM_VERBOSE((void*)(module
->load_addr
+ pe_seg_ofs
),
1407 &pe_seg
, sizeof(pe_seg
)) ||
1408 !DEBUG_READ_MEM_VERBOSE((void*)pe_seg
.Name
, bufstr
, sizeof(bufstr
)))
1409 {fprintf(stderr
, "err3\n");continue;}
1410 bufstr
[sizeof(bufstr
) - 1] = 0;
1412 if (!strcasecmp(bufstr
, ".stab")) {
1413 stabs
= pe_seg
.VirtualAddress
;
1414 stabsize
= pe_seg
.SizeOfRawData
;
1415 } else if (!strncasecmp(bufstr
, ".stabstr", 8)) {
1416 stabstr
= pe_seg
.VirtualAddress
;
1417 stabstrsize
= pe_seg
.SizeOfRawData
;
1420 if (stabstrsize
&& stabsize
) {
1421 #ifdef _WE_SUPPORT_THE_STAB_TYPES_USED_BY_MINGW_TOO
1422 /* Won't work currently, since MINGW32 uses some special typedefs
1423 * which we do not handle yet. Support for them is a bit difficult.
1425 /* FIXME: load_addr is in a different address space... */
1426 DEBUG_ParseStabs(module
->load_addr
, 0, stabs
, stabsize
, stabstr
, stabstrsize
);
1428 fprintf(stderr
,"(stabs not loaded)");
1434 * Process COFF debugging information embedded in a Win32 application.
1439 DEBUG_ProcessCoff(DBG_MODULE
* module
)
1441 struct CoffAuxSection
* aux
;
1442 struct CoffDebug
* coff
;
1443 struct CoffFiles
* coff_files
= NULL
;
1444 struct CoffLinenum
* coff_linetab
;
1446 struct CoffSymbol
* coff_sym
;
1447 struct CoffSymbol
* coff_symbol
;
1448 struct CoffFiles
* curr_file
= NULL
;
1452 struct CoffLinenum
* linepnt
;
1457 DBG_VALUE new_value
;
1459 int nfiles_alloc
= 0;
1460 struct CoffFiles orig_file
;
1462 char * this_file
= NULL
;
1464 coff
= (struct CoffDebug
*) MSC_INFO(module
)->dbg_info
;
1466 coff_symbol
= (struct CoffSymbol
*) ((unsigned int) coff
+ coff
->SymbolOffset
);
1467 coff_linetab
= (struct CoffLinenum
*) ((unsigned int) coff
+ coff
->LinenumberOffset
);
1468 coff_strtab
= (char *) ((unsigned int) coff_symbol
+ 18*coff
->N_Sym
);
1472 new_value
.cookie
= DV_TARGET
;
1473 new_value
.type
= NULL
;
1475 for(i
=0; i
< coff
->N_Sym
; i
++ )
1478 * We do this because some compilers (i.e. gcc) incorrectly
1479 * pad the structure up to a 4 byte boundary. The structure
1480 * is really only 18 bytes long, so we have to manually make sure
1483 * FIXME - there must be a way to have autoconf figure out the
1484 * correct compiler option for this. If it is always gcc, that
1485 * makes life simpler, but I don't want to force this.
1487 coff_sym
= (struct CoffSymbol
*) ((unsigned int) coff_symbol
+ 18*i
);
1488 naux
= coff_sym
->NumberOfAuxSymbols
;
1490 if( coff_sym
->StorageClass
== IMAGE_SYM_CLASS_FILE
)
1492 if( nfiles
+ 1 >= nfiles_alloc
)
1495 coff_files
= (struct CoffFiles
*) DBG_realloc(coff_files
,
1496 nfiles_alloc
* sizeof(struct CoffFiles
));
1498 curr_file
= coff_files
+ nfiles
;
1500 curr_file
->startaddr
= 0xffffffff;
1501 curr_file
->endaddr
= 0;
1502 curr_file
->filename
= ((char *) coff_sym
) + 18;
1503 curr_file
->linetab_offset
= -1;
1504 curr_file
->linecnt
= 0;
1505 curr_file
->entries
= NULL
;
1506 curr_file
->neps
= curr_file
->neps_alloc
= 0;
1508 fprintf(stderr
,"New file %s\n", curr_file
->filename
);
1515 * This guy marks the size and location of the text section
1516 * for the current file. We need to keep track of this so
1517 * we can figure out what file the different global functions
1520 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_STATIC
)
1522 && (coff_sym
->Type
== 0)
1523 && (coff_sym
->SectionNumber
== 1) )
1525 aux
= (struct CoffAuxSection
*) ((unsigned int) coff_sym
+ 18);
1527 if( curr_file
->linetab_offset
!= -1 )
1530 fprintf(stderr
, "Duplicating sect from %s: %x %x %x %d %d\n",
1531 curr_file
->filename
,
1533 aux
->NumberOfRelocations
,
1534 aux
->NumberOfLinenumbers
,
1537 fprintf(stderr
, "More sect %d %x %d %d %d\n",
1538 coff_sym
->SectionNumber
,
1541 coff_sym
->StorageClass
,
1542 coff_sym
->NumberOfAuxSymbols
);
1546 * Save this so we can copy bits from it.
1548 orig_file
= *curr_file
;
1551 * Duplicate the file entry. We have no way to describe
1552 * multiple text sections in our current way of handling things.
1554 if( nfiles
+ 1 >= nfiles_alloc
)
1557 coff_files
= (struct CoffFiles
*) DBG_realloc(coff_files
,
1558 nfiles_alloc
* sizeof(struct CoffFiles
));
1560 curr_file
= coff_files
+ nfiles
;
1562 curr_file
->startaddr
= 0xffffffff;
1563 curr_file
->endaddr
= 0;
1564 curr_file
->filename
= orig_file
.filename
;
1565 curr_file
->linetab_offset
= -1;
1566 curr_file
->linecnt
= 0;
1567 curr_file
->entries
= NULL
;
1568 curr_file
->neps
= curr_file
->neps_alloc
= 0;
1573 fprintf(stderr
, "New text sect from %s: %x %x %x %d %d\n",
1574 curr_file
->filename
,
1576 aux
->NumberOfRelocations
,
1577 aux
->NumberOfLinenumbers
,
1583 if( curr_file
->startaddr
> coff_sym
->Value
)
1585 curr_file
->startaddr
= coff_sym
->Value
;
1588 if( curr_file
->startaddr
> coff_sym
->Value
)
1590 curr_file
->startaddr
= coff_sym
->Value
;
1593 if( curr_file
->endaddr
< coff_sym
->Value
+ aux
->Length
)
1595 curr_file
->endaddr
= coff_sym
->Value
+ aux
->Length
;
1598 curr_file
->linetab_offset
= linetab_indx
;
1599 curr_file
->linecnt
= aux
->NumberOfLinenumbers
;
1600 linetab_indx
+= aux
->NumberOfLinenumbers
;
1605 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_STATIC
)
1607 && (coff_sym
->SectionNumber
== 1) )
1610 * This is a normal static function when naux == 0.
1611 * Just register it. The current file is the correct
1612 * one in this instance.
1614 if( coff_sym
->N
.Name
.NotLong
)
1616 memcpy(namebuff
, coff_sym
->N
.ShortName
, 8);
1618 nampnt
= &namebuff
[0];
1622 nampnt
= coff_strtab
+ coff_sym
->N
.Name
.StrTaboff
;
1625 if( nampnt
[0] == '_' )
1630 new_value
.addr
.seg
= 0;
1631 new_value
.addr
.off
= (int) (module
->load_addr
+ coff_sym
->Value
);
1633 if( curr_file
->neps
+ 1 >= curr_file
->neps_alloc
)
1635 curr_file
->neps_alloc
+= 10;
1636 curr_file
->entries
= (struct name_hash
**)
1637 DBG_realloc(curr_file
->entries
,
1638 curr_file
->neps_alloc
* sizeof(struct name_hash
*));
1641 fprintf(stderr
,"\tAdding static symbol %s\n", nampnt
);
1643 curr_file
->entries
[curr_file
->neps
++] =
1644 DEBUG_AddSymbol( nampnt
, &new_value
, this_file
, SYM_WIN32
);
1649 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_EXTERNAL
)
1650 && ISFCN(coff_sym
->Type
)
1651 && (coff_sym
->SectionNumber
> 0) )
1653 if( coff_sym
->N
.Name
.NotLong
)
1655 memcpy(namebuff
, coff_sym
->N
.ShortName
, 8);
1657 nampnt
= &namebuff
[0];
1661 nampnt
= coff_strtab
+ coff_sym
->N
.Name
.StrTaboff
;
1665 if( nampnt
[0] == '_' )
1670 new_value
.addr
.seg
= 0;
1671 new_value
.addr
.off
= (int) (module
->load_addr
+ coff_sym
->Value
);
1674 fprintf(stderr
, "%d: %x %s\n", i
, new_value
.addr
.off
, nampnt
);
1676 fprintf(stderr
,"\tAdding global symbol %s\n", nampnt
);
1680 * Now we need to figure out which file this guy belongs to.
1683 for(j
=0; j
< nfiles
; j
++)
1685 if( coff_files
[j
].startaddr
<= coff_sym
->Value
1686 && coff_files
[j
].endaddr
> coff_sym
->Value
)
1688 this_file
= coff_files
[j
].filename
;
1692 if( coff_files
[j
].neps
+ 1 >= coff_files
[j
].neps_alloc
)
1694 coff_files
[j
].neps_alloc
+= 10;
1695 coff_files
[j
].entries
= (struct name_hash
**)
1696 DBG_realloc(coff_files
[j
].entries
,
1697 coff_files
[j
].neps_alloc
* sizeof(struct name_hash
*));
1699 coff_files
[j
].entries
[coff_files
[j
].neps
++] =
1700 DEBUG_AddSymbol( nampnt
, &new_value
, this_file
, SYM_WIN32
);
1705 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_EXTERNAL
)
1706 && (coff_sym
->SectionNumber
> 0) )
1709 * Similar to above, but for the case of data symbols.
1710 * These aren't treated as entrypoints.
1712 if( coff_sym
->N
.Name
.NotLong
)
1714 memcpy(namebuff
, coff_sym
->N
.ShortName
, 8);
1716 nampnt
= &namebuff
[0];
1720 nampnt
= coff_strtab
+ coff_sym
->N
.Name
.StrTaboff
;
1724 if( nampnt
[0] == '_' )
1729 new_value
.addr
.seg
= 0;
1730 new_value
.addr
.off
= (int) (module
->load_addr
+ coff_sym
->Value
);
1733 fprintf(stderr
, "%d: %x %s\n", i
, new_value
.addr
.off
, nampnt
);
1735 fprintf(stderr
,"\tAdding global data symbol %s\n", nampnt
);
1739 * Now we need to figure out which file this guy belongs to.
1741 DEBUG_AddSymbol( nampnt
, &new_value
, NULL
, SYM_WIN32
);
1746 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_STATIC
)
1750 * Ignore these. They don't have anything to do with
1758 fprintf(stderr
,"Skipping unknown entry %d %d %d\n", coff_sym
->StorageClass
,
1759 coff_sym
->SectionNumber
, naux
);
1763 * For now, skip past the aux entries.
1770 * OK, we now should have a list of files, and we should have a list
1771 * of entrypoints. We need to sort the entrypoints so that we are
1772 * able to tie the line numbers with the given functions within the
1775 if( coff_files
!= NULL
)
1777 for(j
=0; j
< nfiles
; j
++)
1779 if( coff_files
[j
].entries
!= NULL
)
1781 qsort(coff_files
[j
].entries
, coff_files
[j
].neps
,
1782 sizeof(struct name_hash
*), DEBUG_cmp_sym
);
1787 * Now pick apart the line number tables, and attach the entries
1788 * to the given functions.
1790 for(j
=0; j
< nfiles
; j
++)
1793 if( coff_files
[j
].neps
!= 0 )
1794 for(k
=0; k
< coff_files
[j
].linecnt
; k
++)
1797 * Another monstrosity caused by the fact that we are using
1798 * a 6 byte structure, and gcc wants to pad structures to 4 byte
1799 * boundaries. Otherwise we could just index into an array.
1801 linepnt
= (struct CoffLinenum
*)
1802 ((unsigned int) coff_linetab
+
1803 6*(coff_files
[j
].linetab_offset
+ k
));
1805 * If we have spilled onto the next entrypoint, then
1806 * bump the counter..
1810 if (i
+1 >= coff_files
[j
].neps
) break;
1811 DEBUG_GetSymbolAddr(coff_files
[j
].entries
[i
+1], &new_value
.addr
);
1812 if( (((unsigned int)module
->load_addr
+
1813 linepnt
->VirtualAddr
) >= new_value
.addr
.off
) )
1820 * Add the line number. This is always relative to the
1821 * start of the function, so we need to subtract that offset
1824 DEBUG_GetSymbolAddr(coff_files
[j
].entries
[i
], &new_value
.addr
);
1825 DEBUG_AddLineNumber(coff_files
[j
].entries
[i
],
1827 (unsigned int) module
->load_addr
1828 + linepnt
->VirtualAddr
1829 - new_value
.addr
.off
);
1836 if( coff_files
!= NULL
)
1838 for(j
=0; j
< nfiles
; j
++)
1840 if( coff_files
[j
].entries
!= NULL
)
1842 DBG_free(coff_files
[j
].entries
);
1845 DBG_free(coff_files
);
1853 * Process a codeview line number table. Digestify the thing so that
1854 * we can easily reference the thing when we process the rest of
1857 static struct codeview_linetab_hdr
*
1858 DEBUG_SnarfLinetab(char * linetab
,
1862 char filename
[PATH_MAX
];
1863 unsigned int * filetab
;
1867 struct codeview_linetab_hdr
* lt_hdr
;
1868 unsigned int * lt_ptr
;
1872 union any_size pnt2
;
1873 struct startend
* start
;
1877 * Now get the important bits.
1883 filetab
= (unsigned int *) pnt
.c
;
1886 * Now count up the number of segments in the file.
1889 for(i
=0; i
<nfile
; i
++)
1891 pnt2
.c
= linetab
+ filetab
[i
];
1896 * Next allocate the header we will be returning.
1897 * There is one header for each segment, so that we can reach in
1898 * and pull bits as required.
1900 lt_hdr
= (struct codeview_linetab_hdr
*)
1901 DBG_alloc((nseg
+ 1) * sizeof(*lt_hdr
));
1902 if( lt_hdr
== NULL
)
1907 memset(lt_hdr
, 0, sizeof(*lt_hdr
) * (nseg
+1));
1910 * Now fill the header we will be returning, one for each segment.
1911 * Note that this will basically just contain pointers into the existing
1912 * line table, and we do not actually copy any additional information
1913 * or allocate any additional memory.
1917 for(i
=0; i
<nfile
; i
++)
1920 * Get the pointer into the segment information.
1922 pnt2
.c
= linetab
+ filetab
[i
];
1923 file_segcount
= *pnt2
.s
;
1926 lt_ptr
= (unsigned int *) pnt2
.c
;
1927 start
= (struct startend
*) (lt_ptr
+ file_segcount
);
1930 * Now snarf the filename for all of the segments for this file.
1932 fn
= (unsigned char *) (start
+ file_segcount
);
1933 memset(filename
, 0, sizeof(filename
));
1934 memcpy(filename
, fn
+ 1, *fn
);
1935 fn
= DBG_strdup(filename
);
1937 for(k
= 0; k
< file_segcount
; k
++, this_seg
++)
1939 pnt2
.c
= linetab
+ lt_ptr
[k
];
1940 lt_hdr
[this_seg
].start
= start
[k
].start
;
1941 lt_hdr
[this_seg
].end
= start
[k
].end
;
1942 lt_hdr
[this_seg
].sourcefile
= fn
;
1943 lt_hdr
[this_seg
].segno
= *pnt2
.s
++;
1944 lt_hdr
[this_seg
].nline
= *pnt2
.s
++;
1945 lt_hdr
[this_seg
].offtab
= pnt2
.ui
;
1946 lt_hdr
[this_seg
].linetab
= (unsigned short *)
1947 (pnt2
.ui
+ lt_hdr
[this_seg
].nline
);
1958 DEBUG_SnarfCodeView( DBG_MODULE
* module
,
1961 struct codeview_linetab_hdr
* linetab
)
1963 struct name_hash
* curr_func
= NULL
;
1964 struct wine_locals
* curr_sym
= NULL
;
1968 DBG_VALUE new_value
;
1971 IMAGE_SECTION_HEADER
* sectp
;
1972 union codeview_symbol
* sym
;
1973 char symname
[PATH_MAX
];
1974 struct name_hash
* thunk_sym
= NULL
;
1977 nsect
= MSC_INFO(module
)->nsect
;
1978 sectp
= DBG_alloc(sizeof(*sectp
) * nsect
);
1980 !DEBUG_READ_MEM_VERBOSE(module
->load_addr
+ MSC_INFO(module
)->sect_ofs
,
1981 sectp
, sizeof(*sectp
) * nsect
))
1985 * Loop over the different types of records and whenever we
1986 * find something we are interested in, record it and move on.
1988 while( ptr
.c
- cv_data
< size
)
1990 sym
= (union codeview_symbol
*) ptr
.c
;
1992 if( sym
->generic
.len
- sizeof(int) == (ptr
.c
- cv_data
) )
1995 * This happens when we have indirect symbols that VC++ 4.2
1996 * sometimes uses when there isn't a line number table.
1997 * We ignore it - we will process and enter all of the
1998 * symbols in the global symbol table anyways, so there
1999 * isn't much point in keeping track of all of this crap.
2004 memset(symname
, 0, sizeof(symname
));
2005 switch(sym
->generic
.id
)
2011 * First, a couple of sanity checks.
2013 if( sym
->data
.namelen
== 0 )
2018 if( sym
->data
.seg
== 0 || sym
->data
.seg
> nsect
)
2024 * Global and local data symbols. We don't associate these
2025 * with any given source file.
2028 memcpy(symname
, sym
->data
.name
, sym
->data
.namelen
);
2029 new_value
.addr
.seg
= 0;
2030 new_value
.type
= DEBUG_GetCVType(sym
->data
.symtype
);
2031 new_value
.addr
.off
= (unsigned int) module
->load_addr
+
2032 sectp
[sym
->data
.seg
- 1].VirtualAddress
+
2034 new_value
.cookie
= DV_TARGET
;
2035 DEBUG_AddSymbol( symname
, &new_value
, NULL
, SYM_WIN32
| SYM_DATA
);
2041 * First, a couple of sanity checks.
2043 if( sym
->data32
.namelen
== 0 )
2048 if( sym
->data32
.seg
== 0 || sym
->data32
.seg
> nsect
)
2054 * Global and local data symbols. We don't associate these
2055 * with any given source file.
2058 memcpy(symname
, sym
->data32
.name
, sym
->data32
.namelen
);
2059 new_value
.addr
.seg
= 0;
2060 new_value
.type
= DEBUG_GetCVType(sym
->data32
.symtype
);
2061 new_value
.addr
.off
= (unsigned int) module
->load_addr
+
2062 sectp
[sym
->data32
.seg
- 1].VirtualAddress
+
2064 new_value
.cookie
= DV_TARGET
;
2065 DEBUG_AddSymbol( symname
, &new_value
, NULL
, SYM_WIN32
| SYM_DATA
);
2069 * Sort of like a global function, but it just points
2070 * to a thunk, which is a stupid name for what amounts to
2071 * a PLT slot in the normal jargon that everyone else uses.
2073 memcpy(symname
, sym
->thunk
.name
, sym
->thunk
.namelen
);
2074 new_value
.addr
.seg
= 0;
2075 new_value
.type
= NULL
;
2076 new_value
.addr
.off
= (unsigned int) module
->load_addr
+
2077 sectp
[sym
->thunk
.segment
- 1].VirtualAddress
+
2079 new_value
.cookie
= DV_TARGET
;
2080 thunk_sym
= DEBUG_AddSymbol( symname
, &new_value
, NULL
,
2081 SYM_WIN32
| SYM_FUNC
);
2082 DEBUG_SetSymbolSize(thunk_sym
, sym
->thunk
.thunk_len
);
2087 * Global and static functions.
2089 memcpy(symname
, sym
->proc
.name
, sym
->proc
.namelen
);
2090 new_value
.addr
.seg
= 0;
2091 new_value
.type
= DEBUG_GetCVType(sym
->proc
.proctype
);
2092 new_value
.addr
.off
= (unsigned int) module
->load_addr
+
2093 sectp
[sym
->proc
.segment
- 1].VirtualAddress
+
2095 new_value
.cookie
= DV_TARGET
;
2097 * See if we can find a segment that this goes with. If so,
2098 * it means that we also may have line number information
2099 * for this function.
2101 for(i
=0; linetab
&& linetab
[i
].linetab
!= NULL
; i
++)
2103 if( ((unsigned int) module
->load_addr
2104 + sectp
[linetab
[i
].segno
- 1].VirtualAddress
2105 + linetab
[i
].start
<= new_value
.addr
.off
)
2106 && ((unsigned int) module
->load_addr
2107 + sectp
[linetab
[i
].segno
- 1].VirtualAddress
2108 + linetab
[i
].end
> new_value
.addr
.off
) )
2114 DEBUG_Normalize(curr_func
);
2115 if( !linetab
|| linetab
[i
].linetab
== NULL
)
2117 curr_func
= DEBUG_AddSymbol( symname
, &new_value
, NULL
,
2118 SYM_WIN32
| SYM_FUNC
);
2123 * First, create the entry. Then dig through the linetab
2124 * and add whatever line numbers are appropriate for this
2127 curr_func
= DEBUG_AddSymbol( symname
, &new_value
,
2128 linetab
[i
].sourcefile
,
2129 SYM_WIN32
| SYM_FUNC
);
2130 for(j
=0; j
< linetab
[i
].nline
; j
++)
2132 if( linetab
[i
].offtab
[j
] >= sym
->proc
.offset
2133 && linetab
[i
].offtab
[j
] < sym
->proc
.offset
2134 + sym
->proc
.proc_len
)
2136 DEBUG_AddLineNumber(curr_func
, linetab
[i
].linetab
[j
],
2137 linetab
[i
].offtab
[j
] - sym
->proc
.offset
);
2144 * Add information about where we should set breakpoints
2147 DEBUG_SetSymbolBPOff(curr_func
, sym
->proc
.debug_start
);
2148 DEBUG_SetSymbolSize(curr_func
, sym
->proc
.proc_len
);
2153 * Global and static functions.
2155 memcpy(symname
, sym
->proc32
.name
, sym
->proc32
.namelen
);
2156 new_value
.addr
.seg
= 0;
2157 new_value
.type
= DEBUG_GetCVType(sym
->proc32
.proctype
);
2158 new_value
.addr
.off
= (unsigned int) module
->load_addr
+
2159 sectp
[sym
->proc32
.segment
- 1].VirtualAddress
+
2161 new_value
.cookie
= DV_TARGET
;
2163 * See if we can find a segment that this goes with. If so,
2164 * it means that we also may have line number information
2165 * for this function.
2167 for(i
=0; linetab
&& linetab
[i
].linetab
!= NULL
; i
++)
2169 if( ((unsigned int) module
->load_addr
2170 + sectp
[linetab
[i
].segno
- 1].VirtualAddress
2171 + linetab
[i
].start
<= new_value
.addr
.off
)
2172 && ((unsigned int) module
->load_addr
2173 + sectp
[linetab
[i
].segno
- 1].VirtualAddress
2174 + linetab
[i
].end
> new_value
.addr
.off
) )
2180 DEBUG_Normalize(curr_func
);
2181 if( !linetab
|| linetab
[i
].linetab
== NULL
)
2183 curr_func
= DEBUG_AddSymbol( symname
, &new_value
, NULL
,
2184 SYM_WIN32
| SYM_FUNC
);
2189 * First, create the entry. Then dig through the linetab
2190 * and add whatever line numbers are appropriate for this
2193 curr_func
= DEBUG_AddSymbol( symname
, &new_value
,
2194 linetab
[i
].sourcefile
,
2195 SYM_WIN32
| SYM_FUNC
);
2196 for(j
=0; j
< linetab
[i
].nline
; j
++)
2198 if( linetab
[i
].offtab
[j
] >= sym
->proc32
.offset
2199 && linetab
[i
].offtab
[j
] < sym
->proc32
.offset
2200 + sym
->proc32
.proc_len
)
2202 DEBUG_AddLineNumber(curr_func
, linetab
[i
].linetab
[j
],
2203 linetab
[i
].offtab
[j
] - sym
->proc32
.offset
);
2210 * Add information about where we should set breakpoints
2213 DEBUG_SetSymbolBPOff(curr_func
, sym
->proc32
.debug_start
);
2214 DEBUG_SetSymbolSize(curr_func
, sym
->proc32
.proc_len
);
2218 * Function parameters and stack variables.
2220 memcpy(symname
, sym
->stack
.name
, sym
->stack
.namelen
);
2221 curr_sym
= DEBUG_AddLocal(curr_func
,
2227 DEBUG_SetLocalSymbolType(curr_sym
, DEBUG_GetCVType(sym
->stack
.symtype
));
2232 * Function parameters and stack variables.
2234 memcpy(symname
, sym
->stack32
.name
, sym
->stack32
.namelen
);
2235 curr_sym
= DEBUG_AddLocal(curr_func
,
2237 sym
->stack32
.offset
,
2241 DEBUG_SetLocalSymbolType(curr_sym
, DEBUG_GetCVType(sym
->stack32
.symtype
));
2249 * Adjust pointer to point to next entry, rounding up to a word
2250 * boundary. MS preserving alignment? Stranger things have
2253 if( sym
->generic
.id
== S_PROCREF
2254 || sym
->generic
.id
== S_DATAREF
2255 || sym
->generic
.id
== S_LPROCREF
)
2257 len
= (sym
->generic
.len
+ 3) & ~3;
2258 len
+= ptr
.c
[16] + 1;
2259 ptr
.c
+= (len
+ 3) & ~3;
2263 ptr
.c
+= (sym
->generic
.len
+ 3) & ~3;
2267 if( linetab
!= NULL
)
2278 * Process PDB file which contains debug information.
2282 typedef struct _PDB_FILE
2287 } PDB_FILE
, *PPDB_FILE
;
2289 typedef struct _PDB_HEADER
2297 WORD toc_block
[ 1 ];
2299 } PDB_HEADER
, *PPDB_HEADER
;
2301 typedef struct _PDB_TOC
2306 } PDB_TOC
, *PPDB_TOC
;
2308 typedef struct _PDB_ROOT
2311 DWORD TimeDateStamp
;
2316 } PDB_ROOT
, *PPDB_ROOT
;
2318 typedef struct _PDB_TYPES_OLD
2327 } PDB_TYPES_OLD
, *PPDB_TYPES_OLD
;
2329 typedef struct _PDB_TYPES
2342 DWORD search_offset
;
2344 DWORD unknown_offset
;
2347 } PDB_TYPES
, *PPDB_TYPES
;
2349 typedef struct _PDB_SYMBOL_RANGE
2355 DWORD characteristics
;
2359 } PDB_SYMBOL_RANGE
, *PPDB_SYMBOL_RANGE
;
2361 typedef struct _PDB_SYMBOL_RANGE_EX
2367 DWORD characteristics
;
2373 } PDB_SYMBOL_RANGE_EX
, *PPDB_SYMBOL_RANGE_EX
;
2375 typedef struct _PDB_SYMBOL_FILE
2378 PDB_SYMBOL_RANGE range
;
2388 } PDB_SYMBOL_FILE
, *PPDB_SYMBOL_FILE
;
2390 typedef struct _PDB_SYMBOL_FILE_EX
2393 PDB_SYMBOL_RANGE_EX range
;
2401 DWORD reserved
[ 2 ];
2404 } PDB_SYMBOL_FILE_EX
, *PPDB_SYMBOL_FILE_EX
;
2406 typedef struct _PDB_SYMBOL_SOURCE
2412 } PDB_SYMBOL_SOURCE
, *PPDB_SYMBOL_SOURCE
;
2414 typedef struct _PDB_SYMBOL_IMPORT
2418 DWORD TimeDateStamp
;
2422 } PDB_SYMBOL_IMPORT
, *PPDB_SYMBOL_IMPORT
;
2424 typedef struct _PDB_SYMBOLS_OLD
2433 DWORD srcmodule_size
;
2435 } PDB_SYMBOLS_OLD
, *PPDB_SYMBOLS_OLD
;
2437 typedef struct _PDB_SYMBOLS
2441 DWORD extended_format
;
2448 DWORD srcmodule_size
;
2449 DWORD pdbimport_size
;
2452 } PDB_SYMBOLS
, *PPDB_SYMBOLS
;
2456 static void *pdb_read( LPBYTE image
, WORD
*block_list
, int size
)
2458 PPDB_HEADER pdb
= (PPDB_HEADER
)image
;
2462 if ( !size
) return NULL
;
2464 nBlocks
= (size
+ pdb
->blocksize
-1) / pdb
->blocksize
;
2465 buffer
= DBG_alloc( nBlocks
* pdb
->blocksize
);
2467 for ( i
= 0; i
< nBlocks
; i
++ )
2468 memcpy( buffer
+ i
*pdb
->blocksize
,
2469 image
+ block_list
[i
]*pdb
->blocksize
, pdb
->blocksize
);
2474 static void *pdb_read_file( LPBYTE image
, PPDB_TOC toc
, int fileNr
)
2476 PPDB_HEADER pdb
= (PPDB_HEADER
)image
;
2480 if ( !toc
|| fileNr
>= toc
->nFiles
)
2483 block_list
= (WORD
*) &toc
->file
[ toc
->nFiles
];
2484 for ( i
= 0; i
< fileNr
; i
++ )
2485 block_list
+= (toc
->file
[i
].size
+ pdb
->blocksize
-1) / pdb
->blocksize
;
2487 return pdb_read( image
, block_list
, toc
->file
[fileNr
].size
);
2490 static void pdb_free( void *buffer
)
2495 static void pdb_convert_types_header( PDB_TYPES
*types
, char *image
)
2497 memset( types
, 0, sizeof(PDB_TYPES
) );
2498 if ( !image
) return;
2500 if ( *(DWORD
*)image
< 19960000 ) /* FIXME: correct version? */
2502 /* Old version of the types record header */
2503 PDB_TYPES_OLD
*old
= (PDB_TYPES_OLD
*)image
;
2504 types
->version
= old
->version
;
2505 types
->type_offset
= sizeof(PDB_TYPES_OLD
);
2506 types
->type_size
= old
->type_size
;
2507 types
->first_index
= old
->first_index
;
2508 types
->last_index
= old
->last_index
;
2509 types
->file
= old
->file
;
2513 /* New version of the types record header */
2514 *types
= *(PDB_TYPES
*)image
;
2518 static void pdb_convert_symbols_header( PDB_SYMBOLS
*symbols
,
2519 int *header_size
, char *image
)
2521 memset( symbols
, 0, sizeof(PDB_SYMBOLS
) );
2522 if ( !image
) return;
2524 if ( *(DWORD
*)image
!= 0xffffffff )
2526 /* Old version of the symbols record header */
2527 PDB_SYMBOLS_OLD
*old
= (PDB_SYMBOLS_OLD
*)image
;
2528 symbols
->version
= 0;
2529 symbols
->extended_format
= 0;
2530 symbols
->module_size
= old
->module_size
;
2531 symbols
->offset_size
= old
->offset_size
;
2532 symbols
->hash_size
= old
->hash_size
;
2533 symbols
->srcmodule_size
= old
->srcmodule_size
;
2534 symbols
->pdbimport_size
= 0;
2535 symbols
->hash1_file
= old
->hash1_file
;
2536 symbols
->hash2_file
= old
->hash2_file
;
2537 symbols
->gsym_file
= old
->gsym_file
;
2539 *header_size
= sizeof(PDB_SYMBOLS_OLD
);
2543 /* New version of the symbols record header */
2544 *symbols
= *(PDB_SYMBOLS
*)image
;
2546 *header_size
= sizeof(PDB_SYMBOLS
);
2550 static int DEBUG_ProcessPDBFile( DBG_MODULE
* module
, char *full_filename
)
2552 char filename
[MAX_PATHNAME_LEN
];
2553 struct stat statbuf
;
2555 char *image
= (char *) 0xffffffff;
2556 PDB_HEADER
*pdb
= NULL
;
2557 PDB_TOC
*toc
= NULL
;
2558 PDB_ROOT
*root
= NULL
;
2559 char *types_image
= NULL
;
2560 char *symbols_image
= NULL
;
2562 PDB_SYMBOLS symbols
;
2563 int header_size
= 0;
2564 char *modimage
, *file
;
2568 * Open and mmap() .PDB file
2571 LocateDebugInfoFile( full_filename
, filename
);
2573 if ( stat( filename
, &statbuf
) == -1 )
2575 fprintf( stderr
, "-Unable to open .PDB file %s\n", filename
);
2579 fd
= open(filename
, O_RDONLY
);
2582 fprintf( stderr
, "-Unable to open .PDB file %s\n", filename
);
2586 image
= mmap( 0, statbuf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0 );
2587 if ( image
== (char *) 0xffffffff )
2589 fprintf(stderr
, "-Unable to mmap .PDB file %s\n", filename
);
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 */
2614 case 19960307: /* VC 5.0 */
2615 case 19970604: /* VC 6.0 */
2618 fprintf( stderr
, "-Unknown root block version %ld\n", root
->version
);
2621 switch ( types
.version
)
2623 case 19950410: /* VC 4.0 */
2625 case 19961031: /* VC 5.0 / 6.0 */
2628 fprintf( stderr
, "-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 */
2638 fprintf( stderr
, "-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 fprintf(stderr
, "-Wrong time stamp of .PDB file %s\n", filename
);
2657 DEBUG_ParseTypeTable( types_image
+ types
.type_offset
, types
.type_size
);
2660 * Read type-server .PDB imports
2663 if ( symbols
.pdbimport_size
)
2666 fprintf(stderr
, "-Type server .PDB imports ignored!\n" );
2670 * Read global symbol table
2673 modimage
= pdb_read_file( image
, toc
, symbols
.gsym_file
);
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
;
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
;
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
);
2713 struct codeview_linetab_hdr
*linetab
= NULL
;
2716 linetab
= DEBUG_SnarfLinetab( modimage
+ symbol_size
, lineno_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 );
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 if ( image
!= (char *) 0xffffffff ) munmap( image
, statbuf
.st_size
);
2742 if ( fd
!= -1 ) close( fd
);
2749 * Process DBG file which contains debug information.
2753 DEBUG_ProcessDBGFile(DBG_MODULE
* module
, char * filename
)
2755 char * addr
= (char *) 0xffffffff;
2757 struct CV4_DirHead
* codeview_dir
;
2758 struct CV4_DirEnt
* codeview_dent
;
2759 PIMAGE_DEBUG_DIRECTORY dbghdr
;
2764 struct codeview_linetab_hdr
* linetab
;
2766 PIMAGE_SEPARATE_DEBUG_HEADER pdbg
= NULL
;
2767 IMAGE_SECTION_HEADER
* sectp
;
2768 struct stat statbuf
;
2770 char dbg_file
[MAX_PATHNAME_LEN
];
2772 LocateDebugInfoFile(filename
, dbg_file
);
2773 status
= stat(dbg_file
, &statbuf
);
2776 fprintf(stderr
, "-Unable to open .DBG file %s\n", dbg_file
);
2781 * Now open the file, so that we can mmap() it.
2783 fd
= open(dbg_file
, O_RDONLY
);
2786 fprintf(stderr
, "Unable to open .DBG file %s\n", dbg_file
);
2792 * Now mmap() the file.
2794 addr
= mmap(0, statbuf
.st_size
, PROT_READ
,
2795 MAP_PRIVATE
, fd
, 0);
2796 if( addr
== (char *) 0xffffffff )
2798 fprintf(stderr
, "Unable to mmap .DBG file %s\n", dbg_file
);
2802 pdbg
= (PIMAGE_SEPARATE_DEBUG_HEADER
) addr
;
2804 if( pdbg
->TimeDateStamp
!= MSC_INFO(module
)->dbgdir
.TimeDateStamp
)
2806 fprintf(stderr
, "Warning - %s has incorrect internal timestamp\n",
2810 Well, sometimes this happens to DBG files which ARE REALLY the right .DBG
2811 files but nonetheless this check fails. Anyway, WINDBG (debugger for
2812 Windows by Microsoft) loads debug symbols which have incorrect timestamps.
2816 fprintf(stderr
, "Processing symbols from %s...\n", dbg_file
);
2818 dbghdr
= (PIMAGE_DEBUG_DIRECTORY
) ( addr
+ sizeof(*pdbg
)
2819 + pdbg
->NumberOfSections
* sizeof(IMAGE_SECTION_HEADER
)
2820 + pdbg
->ExportedNamesSize
);
2822 sectp
= (PIMAGE_SECTION_HEADER
) ((char *) pdbg
+ sizeof(*pdbg
));
2823 nsect
= pdbg
->NumberOfSections
;
2825 for( i
=0; i
< pdbg
->DebugDirectorySize
/ sizeof(*pdbg
); i
++, dbghdr
++ )
2827 switch(dbghdr
->Type
)
2829 case IMAGE_DEBUG_TYPE_COFF
:
2831 * Dummy up a deferred debug header to handle the
2832 * COFF stuff embedded within the DBG file.
2834 memset((char *) &module2
, 0, sizeof(module2
));
2835 MSC_INFO(&module2
)->dbg_info
= (addr
+ dbghdr
->PointerToRawData
);
2836 MSC_INFO(&module2
)->dbg_size
= dbghdr
->SizeOfData
;
2837 module2
.load_addr
= module
->load_addr
;
2839 DEBUG_ProcessCoff(&module2
);
2841 case IMAGE_DEBUG_TYPE_CODEVIEW
:
2843 * This is the older format by which codeview stuff is
2844 * stored, known as the 'NB09' format. Newer executables
2845 * and dlls created by VC++ use PDB files instead, which
2846 * have lots of internal similarities, but the overall
2847 * format and structure is quite different.
2849 codeview
= (addr
+ dbghdr
->PointerToRawData
);
2852 * The first thing in the codeview section should be
2853 * an 'NB09' identifier. As a sanity check, make sure
2856 if( *((unsigned int*) codeview
) != 0x3930424e )
2862 * Next we need to find the directory. This is easy too.
2864 codeview_dir
= (struct CV4_DirHead
*)
2865 (codeview
+ ((unsigned int*) codeview
)[1]);
2868 * Some more sanity checks. Make sure that everything
2869 * is as we expect it.
2871 if( codeview_dir
->next_offset
!= 0
2872 || codeview_dir
->dhsize
!= sizeof(*codeview_dir
)
2873 || codeview_dir
->desize
!= sizeof(*codeview_dent
) )
2877 codeview_dent
= (struct CV4_DirEnt
*) (codeview_dir
+ 1);
2879 for(j
=0; j
< codeview_dir
->ndir
; j
++, codeview_dent
++)
2881 if( codeview_dent
->subsect_number
== sstAlignSym
)
2884 * Check the previous entry. If it is a
2885 * sstSrcModule, it contains the line number
2886 * info for this file.
2889 if( codeview_dent
[1].module_number
== codeview_dent
[0].module_number
2890 && codeview_dent
[1].subsect_number
== sstSrcModule
)
2892 linetab
= DEBUG_SnarfLinetab(
2893 codeview
+ codeview_dent
[1].offset
,
2894 codeview_dent
[1].size
);
2897 if( codeview_dent
[-1].module_number
== codeview_dent
[0].module_number
2898 && codeview_dent
[-1].subsect_number
== sstSrcModule
)
2900 linetab
= DEBUG_SnarfLinetab(
2901 codeview
+ codeview_dent
[-1].offset
,
2902 codeview_dent
[-1].size
);
2905 * Now process the CV stuff.
2907 DEBUG_SnarfCodeView(module
,
2908 codeview
+ codeview_dent
->offset
+ sizeof(DWORD
),
2909 codeview_dent
->size
- sizeof(DWORD
),
2921 if( addr
!= (char *) 0xffffffff )
2923 munmap(addr
, statbuf
.st_size
);
2935 DEBUG_ProcessMSCDebugInfo(DBG_MODULE
* module
)
2937 struct CodeViewDebug
* cvd
;
2938 struct MiscDebug
* misc
;
2942 switch (MSC_INFO(module
)->dbgdir
.Type
)
2944 case IMAGE_DEBUG_TYPE_COFF
:
2946 * Standard COFF debug information that VC++ adds when you
2947 * use /debugtype:both with the linker.
2950 fprintf(stderr
, "Processing COFF symbols...\n");
2952 sts
= DEBUG_ProcessCoff(module
);
2954 case IMAGE_DEBUG_TYPE_CODEVIEW
:
2956 * This is a pointer to a PDB file of some sort.
2958 cvd
= (struct CodeViewDebug
*) MSC_INFO(module
)->dbg_info
;
2960 if( strcmp(cvd
->cv_nbtype
, "NB10") != 0 )
2963 * Whatever this is, we don't know how to deal with
2969 sts
= DEBUG_ProcessPDBFile(module
, cvd
->cv_name
);
2971 fprintf(stderr
, "Processing PDB file %s\n", cvd
->cv_name
);
2974 case IMAGE_DEBUG_TYPE_MISC
:
2976 * A pointer to a .DBG file of some sort. These files
2977 * can contain either CV4 or COFF information. Open
2978 * the file, and try to do the right thing with it.
2980 misc
= (struct MiscDebug
*) MSC_INFO(module
)->dbg_info
;
2982 filename
= strrchr((char *) &misc
->Data
, '.');
2985 * Ignore the file if it doesn't have a .DBG extension.
2987 if( (filename
== NULL
)
2988 || ( (strcmp(filename
, ".dbg") != 0)
2989 && (strcmp(filename
, ".DBG") != 0)) )
2995 filename
= (char *) &misc
->Data
;
2998 * Do the dirty deed...
3000 sts
= DEBUG_ProcessDBGFile(module
, filename
);
3005 * We should never get here...