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 *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
);
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
))
56 if (GetEnvironmentVariableA("_NT_ALT_SYMBOL_PATH", str1
, MAX_PATHNAME_LEN
))
57 if (SearchPathA(str1
, file
, NULL
, MAX_PATHNAME_LEN
*10, str2
, &name_part
))
59 if (SearchPathA(NULL
, file
, NULL
, MAX_PATHNAME_LEN
*10, str2
, &name_part
))
64 memcpy(dbg_filename
, filename
, MAX_PATHNAME_LEN
);
70 if (DOSFS_GetFullName(str2
, TRUE
, &fullname
))
71 memcpy(dbg_filename
, fullname
.long_name
, MAX_PATHNAME_LEN
);
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;
95 * This is a convenience structure used to map portions of the
105 * This is how we reference the various record types.
107 union codeview_symbol
121 unsigned short symtype
;
122 unsigned char namelen
;
123 unsigned char name
[1];
130 unsigned int symtype
;
133 unsigned char namelen
;
134 unsigned char name
[1];
141 unsigned int pparent
;
145 unsigned short segment
;
146 unsigned short thunk_len
;
147 unsigned char thtype
;
148 unsigned char namelen
;
149 unsigned char name
[1];
156 unsigned int pparent
;
159 unsigned int proc_len
;
160 unsigned int debug_start
;
161 unsigned int debug_end
;
163 unsigned short segment
;
164 unsigned short proctype
;
166 unsigned char namelen
;
167 unsigned char name
[1];
174 unsigned int pparent
;
177 unsigned int proc_len
;
178 unsigned int debug_start
;
179 unsigned int debug_end
;
180 unsigned int proctype
;
182 unsigned short segment
;
184 unsigned char namelen
;
185 unsigned char name
[1];
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];
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];
224 unsigned char variant
[1];
231 unsigned int datatype
;
232 unsigned int attribute
;
233 unsigned char variant
[1];
241 unsigned char bitoff
;
251 unsigned char bitoff
;
260 unsigned char arrlen
;
261 unsigned char namelen
;
262 unsigned char name
[1];
269 unsigned int elemtype
;
270 unsigned int idxtype
;
271 unsigned char arrlen
;
272 unsigned char namelen
;
273 unsigned char name
[1];
285 unsigned short structlen
;
286 unsigned char namelen
;
287 unsigned char name
[1];
296 unsigned int fieldlist
;
297 unsigned int derived
;
299 unsigned short structlen
;
300 unsigned char namelen
;
301 unsigned char name
[1];
311 unsigned short un_len
;
312 unsigned char namelen
;
313 unsigned char name
[1];
323 unsigned short un_len
;
324 unsigned char namelen
;
325 unsigned char name
[1];
336 unsigned char namelen
;
337 unsigned char name
[1];
348 unsigned char namelen
;
349 unsigned char name
[1];
356 unsigned short int value
;
357 unsigned char namelen
;
358 unsigned char name
[1];
366 unsigned short int offset
;
367 unsigned char namelen
;
368 unsigned char name
[1];
376 unsigned short int offset
;
377 unsigned char namelen
;
378 unsigned char name
[1];
382 #define S_COMPILE 0x0001
383 #define S_REGISTER 0x0002
384 #define S_CONSTANT 0x0003
386 #define S_SSEARCH 0x0005
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
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
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.
590 unsigned int cv_timestamp
;
596 unsigned int DataType
;
604 * This is the header that the COFF variety of debug header points to.
608 unsigned int SymbolOffset
;
609 unsigned int N_Linenum
;
610 unsigned int LinenumberOffset
;
611 unsigned int Unused
[4];
615 unsigned int VirtualAddr
;
616 unsigned short int Linenum
;
620 unsigned int startaddr
;
621 unsigned int endaddr
;
625 struct name_hash
**entries
;
635 unsigned int NotLong
;
636 unsigned int StrTaboff
;
643 unsigned char NumberOfAuxSymbols
;
646 struct CoffAuxSection
{
648 unsigned short NumberOfRelocations
;
649 unsigned short NumberOfLinenumbers
;
650 unsigned int CheckSum
;
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.
660 short unsigned int dhsize
;
661 short unsigned int desize
;
663 unsigned int next_offset
;
668 short unsigned int subsect_number
;
669 short unsigned int module_number
;
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
687 unsigned short * linetab
;
688 unsigned int * offtab
;
693 ********************************************************************
695 struct deferred_debug_info
697 struct deferred_debug_info
* next
;
703 PIMAGE_DEBUG_DIRECTORY dbgdir
;
704 PIMAGE_SECTION_HEADER sectp
;
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
720 #define N_TMASK 0x0030
721 #define IMAGE_SYM_DTYPE_FUNCTION 2
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
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
];
752 if( typeno
- 0x1000 < num_cv_defined_types
)
754 dt
= cv_defined_types
[typeno
- 0x1000];
762 DEBUG_ParseTypeTable(char * table
, int len
)
766 enum debug_type fieldtype
;
770 struct datatype
* subtype
;
772 union codeview_type
* type
;
773 union codeview_type
* type2
;
774 struct datatype
* typeptr
;
779 while( ptr
.c
- table
< len
)
781 type
= (union codeview_type
*) ptr
.c
;
783 if( curr_type
- 0x1000 >= num_cv_defined_types
)
785 num_cv_defined_types
+= 0x100;
786 cv_defined_types
= (struct datatype
**) DBG_realloc(cv_defined_types
,
787 num_cv_defined_types
* sizeof(struct datatype
*));
788 memset(cv_defined_types
+ num_cv_defined_types
- 0x100,
790 0x100 * sizeof(struct datatype
*));
791 if( cv_defined_types
== NULL
)
797 switch(type
->generic
.id
)
800 cv_defined_types
[curr_type
- 0x1000] =
801 DEBUG_FindOrMakePointerType(DEBUG_GetCVType(type
->pointer
.datatype
));
804 cv_defined_types
[curr_type
- 0x1000] =
805 DEBUG_FindOrMakePointerType(DEBUG_GetCVType(type
->pointer32
.datatype
));
808 if( type
->array
.arrlen
>= 0x8000 )
811 * This is a numeric leaf, I am too lazy to handle this right
814 fprintf(stderr
, "Ignoring large numberic leaf.\n");
817 if( type
->array
.namelen
!= 0 )
819 memset(symname
, 0, sizeof(symname
));
820 memcpy(symname
, type
->array
.name
, type
->array
.namelen
);
821 typeptr
= DEBUG_NewDataType(DT_ARRAY
, symname
);
825 typeptr
= DEBUG_NewDataType(DT_ARRAY
, NULL
);
827 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
829 subtype
= DEBUG_GetCVType(type
->array
.elemtype
);
830 if( (subtype
== NULL
)
831 || (elem_size
= DEBUG_GetObjectSize(subtype
)) == 0 )
837 arr_max
= type
->array
.arrlen
/ DEBUG_GetObjectSize(subtype
);
840 DEBUG_SetArrayParams(typeptr
, 0, arr_max
, subtype
);
843 if( type
->array32
.arrlen
>= 0x8000 )
846 * This is a numeric leaf, I am too lazy to handle this right
849 fprintf(stderr
, "Ignoring large numberic leaf.\n");
852 if( type
->array32
.namelen
!= 0 )
854 memset(symname
, 0, sizeof(symname
));
855 memcpy(symname
, type
->array32
.name
, type
->array32
.namelen
);
856 typeptr
= DEBUG_NewDataType(DT_ARRAY
, symname
);
860 typeptr
= DEBUG_NewDataType(DT_ARRAY
, NULL
);
862 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
864 subtype
= DEBUG_GetCVType(type
->array32
.elemtype
);
865 if( (subtype
== NULL
)
866 || (elem_size
= DEBUG_GetObjectSize(subtype
)) == 0 )
872 arr_max
= type
->array32
.arrlen
/ DEBUG_GetObjectSize(subtype
);
875 DEBUG_SetArrayParams(typeptr
, 0, arr_max
, subtype
);
879 * This is where the basic list of fields is defined for
880 * structures and classes.
882 * First, we need to look ahead and see whether we are building
883 * a fieldlist for an enum or a struct.
886 type2
= (union codeview_type
*) ptr2
.c
;
887 if( type2
->member
.id
== LF_MEMBER
)
889 typeptr
= DEBUG_NewDataType(DT_STRUCT
, NULL
);
890 fieldtype
= DT_STRUCT
;
892 else if( type2
->member
.id
== LF_ENUMERATE
)
894 typeptr
= DEBUG_NewDataType(DT_ENUM
, NULL
);
902 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
903 while( ptr2
.c
< (ptr
.c
+ ((type
->generic
.len
+ 3) & ~3)) )
905 type2
= (union codeview_type
*) ptr2
.c
;
906 if( type2
->member
.id
== LF_MEMBER
&& fieldtype
== DT_STRUCT
)
908 memset(symname
, 0, sizeof(symname
));
909 memcpy(symname
, type2
->member
.name
, type2
->member
.namelen
);
911 subtype
= DEBUG_GetCVType(type2
->member
.type
);
913 if( subtype
!= NULL
)
915 elem_size
= DEBUG_GetObjectSize(subtype
);
918 if( type2
->member
.offset
>= 0x8000 )
921 * This is a numeric leaf, I am too lazy to handle this right
924 fprintf(stderr
, "Ignoring large numberic leaf.\n");
928 DEBUG_AddStructElement(typeptr
, symname
, subtype
,
929 type2
->member
.offset
<< 3,
933 else if( type2
->member
.id
== LF_ENUMERATE
&& fieldtype
== DT_ENUM
)
935 memset(symname
, 0, sizeof(symname
));
936 memcpy(symname
, type2
->enumerate
.name
, type2
->enumerate
.namelen
);
938 if( type2
->enumerate
.value
>= 0x8000 )
941 * This is a numeric leaf, I am too lazy to handle this right
944 fprintf(stderr
, "Ignoring large numberic leaf.\n");
948 DEBUG_AddStructElement(typeptr
, symname
, NULL
,
949 type2
->enumerate
.value
, 0);
955 * Something else I have never seen before. Either wrong type of
956 * object in the fieldlist, or some other problem which I wouldn't
957 * really know how to handle until it came up.
959 fprintf(stderr
, "Unexpected entry in fieldlist\n");
964 ptr2
.c
+= ((type2
->member
.namelen
+ 9 + 3) & ~3);
967 case LF_FIELDLIST_32
:
969 * This is where the basic list of fields is defined for
970 * structures and classes.
972 * First, we need to look ahead and see whether we are building
973 * a fieldlist for an enum or a struct.
976 type2
= (union codeview_type
*) ptr2
.c
;
977 if( type2
->member32
.id
== LF_MEMBER_32
)
979 typeptr
= DEBUG_NewDataType(DT_STRUCT
, NULL
);
980 fieldtype
= DT_STRUCT
;
982 else if( type2
->member32
.id
== LF_ENUMERATE
)
984 typeptr
= DEBUG_NewDataType(DT_ENUM
, NULL
);
992 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
993 while( ptr2
.c
< (ptr
.c
+ ((type
->generic
.len
+ 3) & ~3)) )
995 type2
= (union codeview_type
*) ptr2
.c
;
996 if( type2
->member
.id
== LF_MEMBER_32
&& fieldtype
== DT_STRUCT
)
998 memset(symname
, 0, sizeof(symname
));
999 memcpy(symname
, type2
->member32
.name
, type2
->member32
.namelen
);
1001 subtype
= DEBUG_GetCVType(type2
->member32
.type
);
1003 if( subtype
!= NULL
)
1005 elem_size
= DEBUG_GetObjectSize(subtype
);
1008 if( type2
->member32
.offset
>= 0x8000 )
1011 * This is a numeric leaf, I am too lazy to handle this right
1014 fprintf(stderr
, "Ignoring large numberic leaf.\n");
1018 DEBUG_AddStructElement(typeptr
, symname
, subtype
,
1019 type2
->member32
.offset
<< 3,
1023 else if( type2
->member32
.id
== LF_ENUMERATE
&& fieldtype
== DT_ENUM
)
1025 memset(symname
, 0, sizeof(symname
));
1026 memcpy(symname
, type2
->enumerate
.name
, type2
->enumerate
.namelen
);
1028 if( type2
->enumerate
.value
>= 0x8000 )
1031 * This is a numeric leaf, I am too lazy to handle this right
1034 fprintf(stderr
, "Ignoring large numberic leaf.\n");
1038 DEBUG_AddStructElement(typeptr
, symname
, NULL
,
1039 type2
->enumerate
.value
, 0);
1045 * Something else I have never seen before. Either wrong type of
1046 * object in the fieldlist, or some other problem which I wouldn't
1047 * really know how to handle until it came up.
1049 fprintf(stderr
, "Unexpected entry in fieldlist\n");
1054 ptr2
.c
+= ((type2
->member32
.namelen
+ 9 + 3) & ~3);
1059 if( type
->structure
.structlen
>= 0x8000 )
1062 * This is a numeric leaf, I am too lazy to handle this right
1065 fprintf(stderr
, "Ignoring large numberic leaf.\n");
1068 memset(symname
, 0, sizeof(symname
));
1069 memcpy(symname
, type
->structure
.name
, type
->structure
.namelen
);
1070 if( strcmp(symname
, "__unnamed") == 0 )
1072 typeptr
= DEBUG_NewDataType(DT_STRUCT
, NULL
);
1076 typeptr
= DEBUG_NewDataType(DT_STRUCT
, symname
);
1078 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
1081 * Now copy the relevant bits from the fieldlist that we specified.
1083 subtype
= DEBUG_GetCVType(type
->structure
.fieldlist
);
1085 if( subtype
!= NULL
)
1087 DEBUG_SetStructSize(typeptr
, type
->structure
.structlen
);
1088 DEBUG_CopyFieldlist(typeptr
, subtype
);
1091 case LF_STRUCTURE_32
:
1093 if( type
->structure32
.structlen
>= 0x8000 )
1096 * This is a numeric leaf, I am too lazy to handle this right
1099 fprintf(stderr
, "Ignoring large numberic leaf.\n");
1102 memset(symname
, 0, sizeof(symname
));
1103 memcpy(symname
, type
->structure32
.name
, type
->structure32
.namelen
);
1104 if( strcmp(symname
, "__unnamed") == 0 )
1106 typeptr
= DEBUG_NewDataType(DT_STRUCT
, NULL
);
1110 typeptr
= DEBUG_NewDataType(DT_STRUCT
, symname
);
1112 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
1115 * Now copy the relevant bits from the fieldlist that we specified.
1117 subtype
= DEBUG_GetCVType(type
->structure32
.fieldlist
);
1119 if( subtype
!= NULL
)
1121 DEBUG_SetStructSize(typeptr
, type
->structure32
.structlen
);
1122 DEBUG_CopyFieldlist(typeptr
, subtype
);
1126 if( type
->t_union
.un_len
>= 0x8000 )
1129 * This is a numeric leaf, I am too lazy to handle this right
1132 fprintf(stderr
, "Ignoring large numberic leaf.\n");
1135 memset(symname
, 0, sizeof(symname
));
1136 memcpy(symname
, type
->t_union
.name
, type
->t_union
.namelen
);
1138 if( strcmp(symname
, "__unnamed") == 0 )
1140 typeptr
= DEBUG_NewDataType(DT_STRUCT
, NULL
);
1144 typeptr
= DEBUG_NewDataType(DT_STRUCT
, symname
);
1147 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
1150 * Now copy the relevant bits from the fieldlist that we specified.
1152 subtype
= DEBUG_GetCVType(type
->t_union
.field
);
1154 if( subtype
!= NULL
)
1156 DEBUG_SetStructSize(typeptr
, type
->t_union
.un_len
);
1157 DEBUG_CopyFieldlist(typeptr
, subtype
);
1161 if( type
->t_union32
.un_len
>= 0x8000 )
1164 * This is a numeric leaf, I am too lazy to handle this right
1167 fprintf(stderr
, "Ignoring large numberic leaf.\n");
1170 memset(symname
, 0, sizeof(symname
));
1171 memcpy(symname
, type
->t_union32
.name
, type
->t_union32
.namelen
);
1173 if( strcmp(symname
, "__unnamed") == 0 )
1175 typeptr
= DEBUG_NewDataType(DT_STRUCT
, NULL
);
1179 typeptr
= DEBUG_NewDataType(DT_STRUCT
, symname
);
1182 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
1185 * Now copy the relevant bits from the fieldlist that we specified.
1187 subtype
= DEBUG_GetCVType(type
->t_union32
.field
);
1189 if( subtype
!= NULL
)
1191 DEBUG_SetStructSize(typeptr
, type
->t_union32
.un_len
);
1192 DEBUG_CopyFieldlist(typeptr
, subtype
);
1196 typeptr
= DEBUG_NewDataType(DT_BITFIELD
, NULL
);
1197 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
1198 DEBUG_SetBitfieldParams(typeptr
, type
->bitfield
.bitoff
,
1199 type
->bitfield
.nbits
,
1200 DEBUG_GetCVType(type
->bitfield
.type
));
1202 case LF_BITFIELD_32
:
1203 typeptr
= DEBUG_NewDataType(DT_BITFIELD
, NULL
);
1204 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
1205 DEBUG_SetBitfieldParams(typeptr
, type
->bitfield32
.bitoff
,
1206 type
->bitfield32
.nbits
,
1207 DEBUG_GetCVType(type
->bitfield32
.type
));
1210 memset(symname
, 0, sizeof(symname
));
1211 memcpy(symname
, type
->enumeration
.name
, type
->enumeration
.namelen
);
1212 typeptr
= DEBUG_NewDataType(DT_ENUM
, symname
);
1213 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
1216 * Now copy the relevant bits from the fieldlist that we specified.
1218 subtype
= DEBUG_GetCVType(type
->enumeration
.field
);
1220 if( subtype
!= NULL
)
1222 DEBUG_CopyFieldlist(typeptr
, subtype
);
1226 memset(symname
, 0, sizeof(symname
));
1227 memcpy(symname
, type
->enumeration32
.name
, type
->enumeration32
.namelen
);
1228 typeptr
= DEBUG_NewDataType(DT_ENUM
, symname
);
1229 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
1232 * Now copy the relevant bits from the fieldlist that we specified.
1234 subtype
= DEBUG_GetCVType(type
->enumeration32
.field
);
1236 if( subtype
!= NULL
)
1238 DEBUG_CopyFieldlist(typeptr
, subtype
);
1245 ptr
.c
+= (type
->generic
.len
+ 3) & ~3;
1252 DEBUG_InitCVDataTypes(void)
1255 * These are the common builtin types that are used by VC++.
1257 cv_basic_types
[T_NOTYPE
] = NULL
;
1258 cv_basic_types
[T_ABS
] = NULL
;
1259 cv_basic_types
[T_VOID
] = DEBUG_NewDataType(DT_BASIC
, "void");
1260 cv_basic_types
[T_CHAR
] = DEBUG_NewDataType(DT_BASIC
, "char");
1261 cv_basic_types
[T_SHORT
] = DEBUG_NewDataType(DT_BASIC
, "short int");
1262 cv_basic_types
[T_LONG
] = DEBUG_NewDataType(DT_BASIC
, "long int");
1263 cv_basic_types
[T_QUAD
] = DEBUG_NewDataType(DT_BASIC
, "long long int");
1264 cv_basic_types
[T_UCHAR
] = DEBUG_NewDataType(DT_BASIC
, "unsigned char");
1265 cv_basic_types
[T_USHORT
] = DEBUG_NewDataType(DT_BASIC
, "short unsigned int");
1266 cv_basic_types
[T_ULONG
] = DEBUG_NewDataType(DT_BASIC
, "long unsigned int");
1267 cv_basic_types
[T_UQUAD
] = DEBUG_NewDataType(DT_BASIC
, "long long unsigned int");
1268 cv_basic_types
[T_REAL32
] = DEBUG_NewDataType(DT_BASIC
, "float");
1269 cv_basic_types
[T_REAL64
] = DEBUG_NewDataType(DT_BASIC
, "double");
1270 cv_basic_types
[T_RCHAR
] = DEBUG_NewDataType(DT_BASIC
, "char");
1271 cv_basic_types
[T_WCHAR
] = DEBUG_NewDataType(DT_BASIC
, "short");
1272 cv_basic_types
[T_INT4
] = DEBUG_NewDataType(DT_BASIC
, "int");
1273 cv_basic_types
[T_UINT4
] = DEBUG_NewDataType(DT_BASIC
, "unsigned int");
1275 cv_basic_types
[T_32PVOID
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_VOID
]);
1276 cv_basic_types
[T_32PCHAR
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_CHAR
]);
1277 cv_basic_types
[T_32PSHORT
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_SHORT
]);
1278 cv_basic_types
[T_32PLONG
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_LONG
]);
1279 cv_basic_types
[T_32PQUAD
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_QUAD
]);
1280 cv_basic_types
[T_32PUCHAR
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_UCHAR
]);
1281 cv_basic_types
[T_32PUSHORT
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_USHORT
]);
1282 cv_basic_types
[T_32PULONG
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_ULONG
]);
1283 cv_basic_types
[T_32PUQUAD
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_UQUAD
]);
1284 cv_basic_types
[T_32PREAL32
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_REAL32
]);
1285 cv_basic_types
[T_32PREAL64
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_REAL64
]);
1286 cv_basic_types
[T_32PRCHAR
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_RCHAR
]);
1287 cv_basic_types
[T_32PWCHAR
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_WCHAR
]);
1288 cv_basic_types
[T_32PINT4
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_INT4
]);
1289 cv_basic_types
[T_32PUINT4
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_UINT4
]);
1293 * In this function, we keep track of deferred debugging information
1294 * that we may need later if we were to need to use the internal debugger.
1295 * We don't fully process it here for performance reasons.
1298 DEBUG_RegisterDebugInfo( HMODULE hModule
, const char *module_name
)
1300 int has_codeview
= FALSE
;
1303 PIMAGE_DEBUG_DIRECTORY dbgptr
;
1304 u_long v_addr
, size
;
1305 PIMAGE_NT_HEADERS nth
= PE_HEADER(hModule
);
1307 size
= nth
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_DEBUG
].Size
;
1309 v_addr
= nth
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
;
1310 dbgptr
= (PIMAGE_DEBUG_DIRECTORY
) (hModule
+ v_addr
);
1312 for(; size
>= sizeof(*dbgptr
); size
-= sizeof(*dbgptr
), dbgptr
++ )
1314 switch(dbgptr
->Type
)
1316 case IMAGE_DEBUG_TYPE_CODEVIEW
:
1317 case IMAGE_DEBUG_TYPE_MISC
:
1318 has_codeview
= TRUE
;
1324 dbgptr
= (PIMAGE_DEBUG_DIRECTORY
) (hModule
+ v_addr
);
1325 for(; size
>= sizeof(*dbgptr
); size
-= sizeof(*dbgptr
), dbgptr
++ )
1327 switch(dbgptr
->Type
)
1329 case IMAGE_DEBUG_TYPE_COFF
:
1331 * If we have both codeview and COFF debug info, ignore the
1332 * coff debug info as it would just confuse us, and it is
1335 * FIXME - this is broken - if we cannot find the PDB file, then
1336 * we end up with no debugging info at all. In this case, we
1337 * should use the COFF info as a backup.
1343 case IMAGE_DEBUG_TYPE_CODEVIEW
:
1344 case IMAGE_DEBUG_TYPE_MISC
:
1346 * This is usually an indirection to a .DBG file.
1347 * This is similar to (but a slightly older format) from the
1350 * First check to see if the image was 'stripped'. If so, it
1351 * means that this entry points to a .DBG file. Otherwise,
1352 * it just points to itself, and we can ignore this.
1360 if( (dbgptr
->Type
!= IMAGE_DEBUG_TYPE_MISC
) ||
1361 (PE_HEADER(hModule
)->FileHeader
.Characteristics
& IMAGE_FILE_DEBUG_STRIPPED
) != 0 )
1365 DOS_FULL_NAME full_name
;
1366 struct deferred_debug_info
* deefer
= (struct deferred_debug_info
*) DBG_alloc(sizeof(*deefer
));
1368 deefer
->module
= hModule
;
1369 deefer
->load_addr
= (char *)hModule
;
1372 * Read the important bits. What we do after this depends
1373 * upon the type, but this is always enough so we are able
1374 * to proceed if we know what we need to do next.
1376 /* basically, the PE loader maps all sections (data, resources...), but doesn't map
1377 * the DataDirectory array's content. One its entry contains the *beloved*
1378 * debug information. (Note the DataDirectory is mapped, not its content)
1381 if (GetModuleFileNameA(hModule
, fn
, sizeof(fn
)) > 0 &&
1382 DOSFS_GetFullName(fn
, TRUE
, &full_name
) &&
1383 (fd
= open(full_name
.long_name
, O_RDONLY
)) > 0)
1385 deefer
->dbg_info
= mmap(NULL
, dbgptr
->SizeOfData
,
1386 PROT_READ
, MAP_PRIVATE
, fd
, dbgptr
->PointerToRawData
);
1388 if( deefer
->dbg_info
== (char *) 0xffffffff )
1397 fprintf(stderr
, " (not mapped: fn=%s, lfn=%s, fd=%d)", fn
, full_name
.long_name
, fd
);
1400 deefer
->dbg_size
= dbgptr
->SizeOfData
;
1401 deefer
->dbgdir
= dbgptr
;
1402 deefer
->next
= dbglist
;
1403 deefer
->status
= DF_STATUS_NEW
;
1404 deefer
->dbg_index
= DEBUG_next_index
;
1405 deefer
->module_name
= DBG_strdup(module_name
);
1407 deefer
->sectp
= PE_SECTIONS(hModule
);
1408 deefer
->nsect
= PE_HEADER(hModule
)->FileHeader
.NumberOfSections
;
1420 /* look for .stabs/.stabstr sections */
1422 PIMAGE_SECTION_HEADER pe_seg
= PE_SECTIONS(hModule
);
1423 int i
,stabsize
=0,stabstrsize
=0;
1424 unsigned int stabs
=0,stabstr
=0;
1426 for (i
=0;i
<nth
->FileHeader
.NumberOfSections
;i
++) {
1427 if (!strcasecmp(pe_seg
[i
].Name
,".stab")) {
1428 stabs
= pe_seg
[i
].VirtualAddress
;
1429 stabsize
= pe_seg
[i
].SizeOfRawData
;
1431 if (!strncasecmp(pe_seg
[i
].Name
,".stabstr",8)) {
1432 stabstr
= pe_seg
[i
].VirtualAddress
;
1433 stabstrsize
= pe_seg
[i
].SizeOfRawData
;
1436 if (stabstrsize
&& stabsize
) {
1437 #ifdef _WE_SUPPORT_THE_STAB_TYPES_USED_BY_MINGW_TOO
1438 /* Won't work currently, since MINGW32 uses some special typedefs
1439 * which we do not handle yet. Support for them is a bit difficult.
1441 DEBUG_ParseStabs(hModule
,0,stabs
,stabsize
,stabstr
,stabstrsize
);
1443 fprintf(stderr
,"(stabs not loaded)");
1450 * ELF modules are also entered into the list - this is so that we
1451 * can make 'info shared' types of displays possible.
1454 DEBUG_RegisterELFDebugInfo(int load_addr
, u_long size
, const char * name
)
1456 struct deferred_debug_info
* deefer
;
1458 deefer
= (struct deferred_debug_info
*) DBG_alloc(sizeof(*deefer
));
1462 * Read the important bits. What we do after this depends
1463 * upon the type, but this is always enough so we are able
1464 * to proceed if we know what we need to do next.
1466 deefer
->dbg_size
= size
;
1467 deefer
->dbg_info
= (char *) NULL
;
1469 deefer
->load_addr
= (char *) load_addr
;
1470 deefer
->dbgdir
= NULL
;
1471 deefer
->next
= dbglist
;
1472 deefer
->status
= DF_STATUS_LOADED
;
1473 deefer
->dbg_index
= DEBUG_next_index
;
1474 deefer
->module_name
= DBG_strdup(name
);
1485 * Process COFF debugging information embedded in a Win32 application.
1490 DEBUG_ProcessCoff(struct deferred_debug_info
* deefer
)
1492 struct CoffAuxSection
* aux
;
1493 struct CoffDebug
* coff
;
1494 struct CoffFiles
* coff_files
= NULL
;
1495 struct CoffLinenum
* coff_linetab
;
1497 struct CoffSymbol
* coff_sym
;
1498 struct CoffSymbol
* coff_symbol
;
1499 struct CoffFiles
* curr_file
= NULL
;
1503 struct CoffLinenum
* linepnt
;
1508 DBG_VALUE new_value
;
1510 int nfiles_alloc
= 0;
1511 struct CoffFiles orig_file
;
1513 char * this_file
= NULL
;
1515 coff
= (struct CoffDebug
*) deefer
->dbg_info
;
1517 coff_symbol
= (struct CoffSymbol
*) ((unsigned int) coff
+ coff
->SymbolOffset
);
1518 coff_linetab
= (struct CoffLinenum
*) ((unsigned int) coff
+ coff
->LinenumberOffset
);
1519 coff_strtab
= (char *) ((unsigned int) coff_symbol
+ 18*coff
->N_Sym
);
1523 new_value
.cookie
= DV_TARGET
;
1524 new_value
.type
= NULL
;
1526 for(i
=0; i
< coff
->N_Sym
; i
++ )
1529 * We do this because some compilers (i.e. gcc) incorrectly
1530 * pad the structure up to a 4 byte boundary. The structure
1531 * is really only 18 bytes long, so we have to manually make sure
1534 * FIXME - there must be a way to have autoconf figure out the
1535 * correct compiler option for this. If it is always gcc, that
1536 * makes life simpler, but I don't want to force this.
1538 coff_sym
= (struct CoffSymbol
*) ((unsigned int) coff_symbol
+ 18*i
);
1539 naux
= coff_sym
->NumberOfAuxSymbols
;
1541 if( coff_sym
->StorageClass
== IMAGE_SYM_CLASS_FILE
)
1543 if( nfiles
+ 1 >= nfiles_alloc
)
1546 coff_files
= (struct CoffFiles
*) DBG_realloc(coff_files
,
1547 nfiles_alloc
* sizeof(struct CoffFiles
));
1549 curr_file
= coff_files
+ nfiles
;
1551 curr_file
->startaddr
= 0xffffffff;
1552 curr_file
->endaddr
= 0;
1553 curr_file
->filename
= ((char *) coff_sym
) + 18;
1554 curr_file
->linetab_offset
= -1;
1555 curr_file
->linecnt
= 0;
1556 curr_file
->entries
= NULL
;
1557 curr_file
->neps
= curr_file
->neps_alloc
= 0;
1559 fprintf(stderr
,"New file %s\n", curr_file
->filename
);
1566 * This guy marks the size and location of the text section
1567 * for the current file. We need to keep track of this so
1568 * we can figure out what file the different global functions
1571 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_STATIC
)
1573 && (coff_sym
->Type
== 0)
1574 && (coff_sym
->SectionNumber
== 1) )
1576 aux
= (struct CoffAuxSection
*) ((unsigned int) coff_sym
+ 18);
1578 if( curr_file
->linetab_offset
!= -1 )
1581 fprintf(stderr
, "Duplicating sect from %s: %x %x %x %d %d\n",
1582 curr_file
->filename
,
1584 aux
->NumberOfRelocations
,
1585 aux
->NumberOfLinenumbers
,
1588 fprintf(stderr
, "More sect %d %x %d %d %d\n",
1589 coff_sym
->SectionNumber
,
1592 coff_sym
->StorageClass
,
1593 coff_sym
->NumberOfAuxSymbols
);
1597 * Save this so we can copy bits from it.
1599 orig_file
= *curr_file
;
1602 * Duplicate the file entry. We have no way to describe
1603 * multiple text sections in our current way of handling things.
1605 if( nfiles
+ 1 >= nfiles_alloc
)
1608 coff_files
= (struct CoffFiles
*) DBG_realloc(coff_files
,
1609 nfiles_alloc
* sizeof(struct CoffFiles
));
1611 curr_file
= coff_files
+ nfiles
;
1613 curr_file
->startaddr
= 0xffffffff;
1614 curr_file
->endaddr
= 0;
1615 curr_file
->filename
= orig_file
.filename
;
1616 curr_file
->linetab_offset
= -1;
1617 curr_file
->linecnt
= 0;
1618 curr_file
->entries
= NULL
;
1619 curr_file
->neps
= curr_file
->neps_alloc
= 0;
1624 fprintf(stderr
, "New text sect from %s: %x %x %x %d %d\n",
1625 curr_file
->filename
,
1627 aux
->NumberOfRelocations
,
1628 aux
->NumberOfLinenumbers
,
1634 if( curr_file
->startaddr
> coff_sym
->Value
)
1636 curr_file
->startaddr
= coff_sym
->Value
;
1639 if( curr_file
->startaddr
> coff_sym
->Value
)
1641 curr_file
->startaddr
= coff_sym
->Value
;
1644 if( curr_file
->endaddr
< coff_sym
->Value
+ aux
->Length
)
1646 curr_file
->endaddr
= coff_sym
->Value
+ aux
->Length
;
1649 curr_file
->linetab_offset
= linetab_indx
;
1650 curr_file
->linecnt
= aux
->NumberOfLinenumbers
;
1651 linetab_indx
+= aux
->NumberOfLinenumbers
;
1656 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_STATIC
)
1658 && (coff_sym
->SectionNumber
== 1) )
1661 * This is a normal static function when naux == 0.
1662 * Just register it. The current file is the correct
1663 * one in this instance.
1665 if( coff_sym
->N
.Name
.NotLong
)
1667 memcpy(namebuff
, coff_sym
->N
.ShortName
, 8);
1669 nampnt
= &namebuff
[0];
1673 nampnt
= coff_strtab
+ coff_sym
->N
.Name
.StrTaboff
;
1676 if( nampnt
[0] == '_' )
1681 new_value
.addr
.seg
= 0;
1682 new_value
.addr
.off
= (int) (deefer
->load_addr
+ coff_sym
->Value
);
1684 if( curr_file
->neps
+ 1 >= curr_file
->neps_alloc
)
1686 curr_file
->neps_alloc
+= 10;
1687 curr_file
->entries
= (struct name_hash
**)
1688 DBG_realloc(curr_file
->entries
,
1689 curr_file
->neps_alloc
* sizeof(struct name_hash
*));
1692 fprintf(stderr
,"\tAdding static symbol %s\n", nampnt
);
1694 curr_file
->entries
[curr_file
->neps
++] =
1695 DEBUG_AddSymbol( nampnt
, &new_value
, this_file
, SYM_WIN32
);
1700 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_EXTERNAL
)
1701 && ISFCN(coff_sym
->Type
)
1702 && (coff_sym
->SectionNumber
> 0) )
1704 if( coff_sym
->N
.Name
.NotLong
)
1706 memcpy(namebuff
, coff_sym
->N
.ShortName
, 8);
1708 nampnt
= &namebuff
[0];
1712 nampnt
= coff_strtab
+ coff_sym
->N
.Name
.StrTaboff
;
1716 if( nampnt
[0] == '_' )
1721 new_value
.addr
.seg
= 0;
1722 new_value
.addr
.off
= (int) (deefer
->load_addr
+ coff_sym
->Value
);
1725 fprintf(stderr
, "%d: %x %s\n", i
, new_value
.addr
.off
, nampnt
);
1727 fprintf(stderr
,"\tAdding global symbol %s\n", nampnt
);
1731 * Now we need to figure out which file this guy belongs to.
1734 for(j
=0; j
< nfiles
; j
++)
1736 if( coff_files
[j
].startaddr
<= coff_sym
->Value
1737 && coff_files
[j
].endaddr
> coff_sym
->Value
)
1739 this_file
= coff_files
[j
].filename
;
1743 if( coff_files
[j
].neps
+ 1 >= coff_files
[j
].neps_alloc
)
1745 coff_files
[j
].neps_alloc
+= 10;
1746 coff_files
[j
].entries
= (struct name_hash
**)
1747 DBG_realloc(coff_files
[j
].entries
,
1748 coff_files
[j
].neps_alloc
* sizeof(struct name_hash
*));
1750 coff_files
[j
].entries
[coff_files
[j
].neps
++] =
1751 DEBUG_AddSymbol( nampnt
, &new_value
, this_file
, SYM_WIN32
);
1756 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_EXTERNAL
)
1757 && (coff_sym
->SectionNumber
> 0) )
1760 * Similar to above, but for the case of data symbols.
1761 * These aren't treated as entrypoints.
1763 if( coff_sym
->N
.Name
.NotLong
)
1765 memcpy(namebuff
, coff_sym
->N
.ShortName
, 8);
1767 nampnt
= &namebuff
[0];
1771 nampnt
= coff_strtab
+ coff_sym
->N
.Name
.StrTaboff
;
1775 if( nampnt
[0] == '_' )
1780 new_value
.addr
.seg
= 0;
1781 new_value
.addr
.off
= (int) (deefer
->load_addr
+ coff_sym
->Value
);
1784 fprintf(stderr
, "%d: %x %s\n", i
, new_value
.addr
.off
, nampnt
);
1786 fprintf(stderr
,"\tAdding global data symbol %s\n", nampnt
);
1790 * Now we need to figure out which file this guy belongs to.
1792 DEBUG_AddSymbol( nampnt
, &new_value
, NULL
, SYM_WIN32
);
1797 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_STATIC
)
1801 * Ignore these. They don't have anything to do with
1809 fprintf(stderr
,"Skipping unknown entry %d %d %d\n", coff_sym
->StorageClass
,
1810 coff_sym
->SectionNumber
, naux
);
1814 * For now, skip past the aux entries.
1821 * OK, we now should have a list of files, and we should have a list
1822 * of entrypoints. We need to sort the entrypoints so that we are
1823 * able to tie the line numbers with the given functions within the
1826 if( coff_files
!= NULL
)
1828 for(j
=0; j
< nfiles
; j
++)
1830 if( coff_files
[j
].entries
!= NULL
)
1832 qsort(coff_files
[j
].entries
, coff_files
[j
].neps
,
1833 sizeof(struct name_hash
*), DEBUG_cmp_sym
);
1838 * Now pick apart the line number tables, and attach the entries
1839 * to the given functions.
1841 for(j
=0; j
< nfiles
; j
++)
1844 if( coff_files
[j
].neps
!= 0 )
1845 for(k
=0; k
< coff_files
[j
].linecnt
; k
++)
1848 * Another monstrosity caused by the fact that we are using
1849 * a 6 byte structure, and gcc wants to pad structures to 4 byte
1850 * boundaries. Otherwise we could just index into an array.
1852 linepnt
= (struct CoffLinenum
*)
1853 ((unsigned int) coff_linetab
+
1854 6*(coff_files
[j
].linetab_offset
+ k
));
1856 * If we have spilled onto the next entrypoint, then
1857 * bump the counter..
1861 if (i
+1 >= coff_files
[j
].neps
) break;
1862 DEBUG_GetSymbolAddr(coff_files
[j
].entries
[i
+1], &new_value
.addr
);
1863 if( (((unsigned int)deefer
->load_addr
+
1864 linepnt
->VirtualAddr
) >= new_value
.addr
.off
) )
1871 * Add the line number. This is always relative to the
1872 * start of the function, so we need to subtract that offset
1875 DEBUG_GetSymbolAddr(coff_files
[j
].entries
[i
], &new_value
.addr
);
1876 DEBUG_AddLineNumber(coff_files
[j
].entries
[i
],
1878 (unsigned int) deefer
->load_addr
1879 + linepnt
->VirtualAddr
1880 - new_value
.addr
.off
);
1887 if( coff_files
!= NULL
)
1889 for(j
=0; j
< nfiles
; j
++)
1891 if( coff_files
[j
].entries
!= NULL
)
1893 DBG_free(coff_files
[j
].entries
);
1896 DBG_free(coff_files
);
1904 * Process a codeview line number table. Digestify the thing so that
1905 * we can easily reference the thing when we process the rest of
1908 static struct codeview_linetab_hdr
*
1909 DEBUG_SnarfLinetab(char * linetab
,
1913 char filename
[PATH_MAX
];
1914 unsigned int * filetab
;
1918 struct codeview_linetab_hdr
* lt_hdr
;
1919 unsigned int * lt_ptr
;
1923 union any_size pnt2
;
1924 struct startend
* start
;
1928 * Now get the important bits.
1934 filetab
= (unsigned int *) pnt
.c
;
1937 * Now count up the number of segments in the file.
1940 for(i
=0; i
<nfile
; i
++)
1942 pnt2
.c
= linetab
+ filetab
[i
];
1947 * Next allocate the header we will be returning.
1948 * There is one header for each segment, so that we can reach in
1949 * and pull bits as required.
1951 lt_hdr
= (struct codeview_linetab_hdr
*)
1952 DBG_alloc((nseg
+ 1) * sizeof(*lt_hdr
));
1953 if( lt_hdr
== NULL
)
1958 memset(lt_hdr
, 0, sizeof(*lt_hdr
) * (nseg
+1));
1961 * Now fill the header we will be returning, one for each segment.
1962 * Note that this will basically just contain pointers into the existing
1963 * line table, and we do not actually copy any additional information
1964 * or allocate any additional memory.
1968 for(i
=0; i
<nfile
; i
++)
1971 * Get the pointer into the segment information.
1973 pnt2
.c
= linetab
+ filetab
[i
];
1974 file_segcount
= *pnt2
.s
;
1977 lt_ptr
= (unsigned int *) pnt2
.c
;
1978 start
= (struct startend
*) (lt_ptr
+ file_segcount
);
1981 * Now snarf the filename for all of the segments for this file.
1983 fn
= (unsigned char *) (start
+ file_segcount
);
1984 memset(filename
, 0, sizeof(filename
));
1985 memcpy(filename
, fn
+ 1, *fn
);
1986 fn
= DBG_strdup(filename
);
1988 for(k
= 0; k
< file_segcount
; k
++, this_seg
++)
1990 pnt2
.c
= linetab
+ lt_ptr
[k
];
1991 lt_hdr
[this_seg
].start
= start
[k
].start
;
1992 lt_hdr
[this_seg
].end
= start
[k
].end
;
1993 lt_hdr
[this_seg
].sourcefile
= fn
;
1994 lt_hdr
[this_seg
].segno
= *pnt2
.s
++;
1995 lt_hdr
[this_seg
].nline
= *pnt2
.s
++;
1996 lt_hdr
[this_seg
].offtab
= pnt2
.ui
;
1997 lt_hdr
[this_seg
].linetab
= (unsigned short *)
1998 (pnt2
.ui
+ lt_hdr
[this_seg
].nline
);
2009 DEBUG_SnarfCodeView( struct deferred_debug_info
* deefer
,
2012 struct codeview_linetab_hdr
* linetab
)
2014 struct name_hash
* curr_func
= NULL
;
2015 struct wine_locals
* curr_sym
= NULL
;
2019 DBG_VALUE new_value
;
2022 IMAGE_SECTION_HEADER
* sectp
;
2023 union codeview_symbol
* sym
;
2024 char symname
[PATH_MAX
];
2025 struct name_hash
* thunk_sym
= NULL
;
2028 nsect
= deefer
->nsect
;
2029 sectp
= deefer
->sectp
;
2032 * Loop over the different types of records and whenever we
2033 * find something we are interested in, record it and move on.
2035 while( ptr
.c
- cv_data
< size
)
2037 sym
= (union codeview_symbol
*) ptr
.c
;
2039 if( sym
->generic
.len
- sizeof(int) == (ptr
.c
- cv_data
) )
2042 * This happens when we have indirect symbols that VC++ 4.2
2043 * sometimes uses when there isn't a line number table.
2044 * We ignore it - we will process and enter all of the
2045 * symbols in the global symbol table anyways, so there
2046 * isn't much point in keeping track of all of this crap.
2051 memset(symname
, 0, sizeof(symname
));
2052 switch(sym
->generic
.id
)
2058 * First, a couple of sanity checks.
2060 if( sym
->data
.namelen
== 0 )
2065 if( sym
->data
.seg
== 0 || sym
->data
.seg
> nsect
)
2071 * Global and local data symbols. We don't associate these
2072 * with any given source file.
2075 memcpy(symname
, sym
->data
.name
, sym
->data
.namelen
);
2076 new_value
.addr
.seg
= 0;
2077 new_value
.type
= DEBUG_GetCVType(sym
->data
.symtype
);
2078 new_value
.addr
.off
= (unsigned int) deefer
->load_addr
+
2079 sectp
[sym
->data
.seg
- 1].VirtualAddress
+
2081 new_value
.cookie
= DV_TARGET
;
2082 DEBUG_AddSymbol( symname
, &new_value
, NULL
, SYM_WIN32
| SYM_DATA
);
2088 * First, a couple of sanity checks.
2090 if( sym
->data32
.namelen
== 0 )
2095 if( sym
->data32
.seg
== 0 || sym
->data32
.seg
> nsect
)
2101 * Global and local data symbols. We don't associate these
2102 * with any given source file.
2105 memcpy(symname
, sym
->data32
.name
, sym
->data32
.namelen
);
2106 new_value
.addr
.seg
= 0;
2107 new_value
.type
= DEBUG_GetCVType(sym
->data32
.symtype
);
2108 new_value
.addr
.off
= (unsigned int) deefer
->load_addr
+
2109 sectp
[sym
->data32
.seg
- 1].VirtualAddress
+
2111 new_value
.cookie
= DV_TARGET
;
2112 DEBUG_AddSymbol( symname
, &new_value
, NULL
, SYM_WIN32
| SYM_DATA
);
2116 * Sort of like a global function, but it just points
2117 * to a thunk, which is a stupid name for what amounts to
2118 * a PLT slot in the normal jargon that everyone else uses.
2120 memcpy(symname
, sym
->thunk
.name
, sym
->thunk
.namelen
);
2121 new_value
.addr
.seg
= 0;
2122 new_value
.type
= NULL
;
2123 new_value
.addr
.off
= (unsigned int) deefer
->load_addr
+
2124 sectp
[sym
->thunk
.segment
- 1].VirtualAddress
+
2126 new_value
.cookie
= DV_TARGET
;
2127 thunk_sym
= DEBUG_AddSymbol( symname
, &new_value
, NULL
,
2128 SYM_WIN32
| SYM_FUNC
);
2129 DEBUG_SetSymbolSize(thunk_sym
, sym
->thunk
.thunk_len
);
2134 * Global and static functions.
2136 memcpy(symname
, sym
->proc
.name
, sym
->proc
.namelen
);
2137 new_value
.addr
.seg
= 0;
2138 new_value
.type
= DEBUG_GetCVType(sym
->proc
.proctype
);
2139 new_value
.addr
.off
= (unsigned int) deefer
->load_addr
+
2140 sectp
[sym
->proc
.segment
- 1].VirtualAddress
+
2142 new_value
.cookie
= DV_TARGET
;
2144 * See if we can find a segment that this goes with. If so,
2145 * it means that we also may have line number information
2146 * for this function.
2148 for(i
=0; linetab
&& linetab
[i
].linetab
!= NULL
; i
++)
2150 if( ((unsigned int) deefer
->load_addr
2151 + sectp
[linetab
[i
].segno
- 1].VirtualAddress
2152 + linetab
[i
].start
<= new_value
.addr
.off
)
2153 && ((unsigned int) deefer
->load_addr
2154 + sectp
[linetab
[i
].segno
- 1].VirtualAddress
2155 + linetab
[i
].end
> new_value
.addr
.off
) )
2161 DEBUG_Normalize(curr_func
);
2162 if( !linetab
|| linetab
[i
].linetab
== NULL
)
2164 curr_func
= DEBUG_AddSymbol( symname
, &new_value
, NULL
,
2165 SYM_WIN32
| SYM_FUNC
);
2170 * First, create the entry. Then dig through the linetab
2171 * and add whatever line numbers are appropriate for this
2174 curr_func
= DEBUG_AddSymbol( symname
, &new_value
,
2175 linetab
[i
].sourcefile
,
2176 SYM_WIN32
| SYM_FUNC
);
2177 for(j
=0; j
< linetab
[i
].nline
; j
++)
2179 if( linetab
[i
].offtab
[j
] >= sym
->proc
.offset
2180 && linetab
[i
].offtab
[j
] < sym
->proc
.offset
2181 + sym
->proc
.proc_len
)
2183 DEBUG_AddLineNumber(curr_func
, linetab
[i
].linetab
[j
],
2184 linetab
[i
].offtab
[j
] - sym
->proc
.offset
);
2191 * Add information about where we should set breakpoints
2194 DEBUG_SetSymbolBPOff(curr_func
, sym
->proc
.debug_start
);
2195 DEBUG_SetSymbolSize(curr_func
, sym
->proc
.proc_len
);
2200 * Global and static functions.
2202 memcpy(symname
, sym
->proc32
.name
, sym
->proc32
.namelen
);
2203 new_value
.addr
.seg
= 0;
2204 new_value
.type
= DEBUG_GetCVType(sym
->proc32
.proctype
);
2205 new_value
.addr
.off
= (unsigned int) deefer
->load_addr
+
2206 sectp
[sym
->proc32
.segment
- 1].VirtualAddress
+
2208 new_value
.cookie
= DV_TARGET
;
2210 * See if we can find a segment that this goes with. If so,
2211 * it means that we also may have line number information
2212 * for this function.
2214 for(i
=0; linetab
&& linetab
[i
].linetab
!= NULL
; i
++)
2216 if( ((unsigned int) deefer
->load_addr
2217 + sectp
[linetab
[i
].segno
- 1].VirtualAddress
2218 + linetab
[i
].start
<= new_value
.addr
.off
)
2219 && ((unsigned int) deefer
->load_addr
2220 + sectp
[linetab
[i
].segno
- 1].VirtualAddress
2221 + linetab
[i
].end
> new_value
.addr
.off
) )
2227 DEBUG_Normalize(curr_func
);
2228 if( !linetab
|| linetab
[i
].linetab
== NULL
)
2230 curr_func
= DEBUG_AddSymbol( symname
, &new_value
, NULL
,
2231 SYM_WIN32
| SYM_FUNC
);
2236 * First, create the entry. Then dig through the linetab
2237 * and add whatever line numbers are appropriate for this
2240 curr_func
= DEBUG_AddSymbol( symname
, &new_value
,
2241 linetab
[i
].sourcefile
,
2242 SYM_WIN32
| SYM_FUNC
);
2243 for(j
=0; j
< linetab
[i
].nline
; j
++)
2245 if( linetab
[i
].offtab
[j
] >= sym
->proc32
.offset
2246 && linetab
[i
].offtab
[j
] < sym
->proc32
.offset
2247 + sym
->proc32
.proc_len
)
2249 DEBUG_AddLineNumber(curr_func
, linetab
[i
].linetab
[j
],
2250 linetab
[i
].offtab
[j
] - sym
->proc32
.offset
);
2257 * Add information about where we should set breakpoints
2260 DEBUG_SetSymbolBPOff(curr_func
, sym
->proc32
.debug_start
);
2261 DEBUG_SetSymbolSize(curr_func
, sym
->proc32
.proc_len
);
2265 * Function parameters and stack variables.
2267 memcpy(symname
, sym
->stack
.name
, sym
->stack
.namelen
);
2268 curr_sym
= DEBUG_AddLocal(curr_func
,
2274 DEBUG_SetLocalSymbolType(curr_sym
, DEBUG_GetCVType(sym
->stack
.symtype
));
2279 * Function parameters and stack variables.
2281 memcpy(symname
, sym
->stack32
.name
, sym
->stack32
.namelen
);
2282 curr_sym
= DEBUG_AddLocal(curr_func
,
2284 sym
->stack32
.offset
,
2288 DEBUG_SetLocalSymbolType(curr_sym
, DEBUG_GetCVType(sym
->stack32
.symtype
));
2296 * Adjust pointer to point to next entry, rounding up to a word
2297 * boundary. MS preserving alignment? Stranger things have
2300 if( sym
->generic
.id
== S_PROCREF
2301 || sym
->generic
.id
== S_DATAREF
2302 || sym
->generic
.id
== S_LPROCREF
)
2304 len
= (sym
->generic
.len
+ 3) & ~3;
2305 len
+= ptr
.c
[16] + 1;
2306 ptr
.c
+= (len
+ 3) & ~3;
2310 ptr
.c
+= (sym
->generic
.len
+ 3) & ~3;
2314 if( linetab
!= NULL
)
2324 * Process PDB file which contains debug information.
2328 typedef struct _PDB_FILE
2333 } PDB_FILE
, *PPDB_FILE
;
2335 typedef struct _PDB_HEADER
2343 WORD toc_block
[ 1 ];
2345 } PDB_HEADER
, *PPDB_HEADER
;
2347 typedef struct _PDB_TOC
2352 } PDB_TOC
, *PPDB_TOC
;
2354 typedef struct _PDB_ROOT
2357 DWORD TimeDateStamp
;
2362 } PDB_ROOT
, *PPDB_ROOT
;
2364 typedef struct _PDB_TYPES_OLD
2373 } PDB_TYPES_OLD
, *PPDB_TYPES_OLD
;
2375 typedef struct _PDB_TYPES
2388 DWORD search_offset
;
2390 DWORD unknown_offset
;
2393 } PDB_TYPES
, *PPDB_TYPES
;
2395 typedef struct _PDB_SYMBOL_RANGE
2401 DWORD characteristics
;
2405 } PDB_SYMBOL_RANGE
, *PPDB_SYMBOL_RANGE
;
2407 typedef struct _PDB_SYMBOL_RANGE_EX
2413 DWORD characteristics
;
2419 } PDB_SYMBOL_RANGE_EX
, *PPDB_SYMBOL_RANGE_EX
;
2421 typedef struct _PDB_SYMBOL_FILE
2424 PDB_SYMBOL_RANGE range
;
2434 } PDB_SYMBOL_FILE
, *PPDB_SYMBOL_FILE
;
2436 typedef struct _PDB_SYMBOL_FILE_EX
2439 PDB_SYMBOL_RANGE_EX range
;
2447 DWORD reserved
[ 2 ];
2450 } PDB_SYMBOL_FILE_EX
, *PPDB_SYMBOL_FILE_EX
;
2452 typedef struct _PDB_SYMBOL_SOURCE
2458 } PDB_SYMBOL_SOURCE
, *PPDB_SYMBOL_SOURCE
;
2460 typedef struct _PDB_SYMBOL_IMPORT
2464 DWORD TimeDateStamp
;
2468 } PDB_SYMBOL_IMPORT
, *PPDB_SYMBOL_IMPORT
;
2470 typedef struct _PDB_SYMBOLS_OLD
2479 DWORD srcmodule_size
;
2481 } PDB_SYMBOLS_OLD
, *PPDB_SYMBOLS_OLD
;
2483 typedef struct _PDB_SYMBOLS
2487 DWORD extended_format
;
2494 DWORD srcmodule_size
;
2495 DWORD pdbimport_size
;
2498 } PDB_SYMBOLS
, *PPDB_SYMBOLS
;
2502 static void *pdb_read( LPBYTE image
, WORD
*block_list
, int size
)
2504 PPDB_HEADER pdb
= (PPDB_HEADER
)image
;
2508 if ( !size
) return NULL
;
2510 nBlocks
= (size
+ pdb
->blocksize
-1) / pdb
->blocksize
;
2511 buffer
= DBG_alloc( nBlocks
* pdb
->blocksize
);
2513 for ( i
= 0; i
< nBlocks
; i
++ )
2514 memcpy( buffer
+ i
*pdb
->blocksize
,
2515 image
+ block_list
[i
]*pdb
->blocksize
, pdb
->blocksize
);
2520 static void *pdb_read_file( LPBYTE image
, PPDB_TOC toc
, int fileNr
)
2522 PPDB_HEADER pdb
= (PPDB_HEADER
)image
;
2526 if ( !toc
|| fileNr
>= toc
->nFiles
)
2529 block_list
= (WORD
*) &toc
->file
[ toc
->nFiles
];
2530 for ( i
= 0; i
< fileNr
; i
++ )
2531 block_list
+= (toc
->file
[i
].size
+ pdb
->blocksize
-1) / pdb
->blocksize
;
2533 return pdb_read( image
, block_list
, toc
->file
[fileNr
].size
);
2536 static void pdb_free( void *buffer
)
2541 static void pdb_convert_types_header( PDB_TYPES
*types
, char *image
)
2543 memset( types
, 0, sizeof(PDB_TYPES
) );
2544 if ( !image
) return;
2546 if ( *(DWORD
*)image
< 19960000 ) /* FIXME: correct version? */
2548 /* Old version of the types record header */
2549 PDB_TYPES_OLD
*old
= (PDB_TYPES_OLD
*)image
;
2550 types
->version
= old
->version
;
2551 types
->type_offset
= sizeof(PDB_TYPES_OLD
);
2552 types
->type_size
= old
->type_size
;
2553 types
->first_index
= old
->first_index
;
2554 types
->last_index
= old
->last_index
;
2555 types
->file
= old
->file
;
2559 /* New version of the types record header */
2560 *types
= *(PDB_TYPES
*)image
;
2564 static void pdb_convert_symbols_header( PDB_SYMBOLS
*symbols
,
2565 int *header_size
, char *image
)
2567 memset( symbols
, 0, sizeof(PDB_SYMBOLS
) );
2568 if ( !image
) return;
2570 if ( *(DWORD
*)image
!= 0xffffffff )
2572 /* Old version of the symbols record header */
2573 PDB_SYMBOLS_OLD
*old
= (PDB_SYMBOLS_OLD
*)image
;
2574 symbols
->version
= 0;
2575 symbols
->extended_format
= 0;
2576 symbols
->module_size
= old
->module_size
;
2577 symbols
->offset_size
= old
->offset_size
;
2578 symbols
->hash_size
= old
->hash_size
;
2579 symbols
->srcmodule_size
= old
->srcmodule_size
;
2580 symbols
->pdbimport_size
= 0;
2581 symbols
->hash1_file
= old
->hash1_file
;
2582 symbols
->hash2_file
= old
->hash2_file
;
2583 symbols
->gsym_file
= old
->gsym_file
;
2585 *header_size
= sizeof(PDB_SYMBOLS_OLD
);
2589 /* New version of the symbols record header */
2590 *symbols
= *(PDB_SYMBOLS
*)image
;
2592 *header_size
= sizeof(PDB_SYMBOLS
);
2596 int DEBUG_ProcessPDBFile( struct deferred_debug_info
*deefer
, char *full_filename
)
2598 char filename
[MAX_PATHNAME_LEN
];
2599 struct stat statbuf
;
2601 char *image
= (char *) 0xffffffff;
2602 PDB_HEADER
*pdb
= NULL
;
2603 PDB_TOC
*toc
= NULL
;
2604 PDB_ROOT
*root
= NULL
;
2605 char *types_image
= NULL
;
2606 char *symbols_image
= NULL
;
2608 PDB_SYMBOLS symbols
;
2609 int header_size
= 0;
2610 char *modimage
, *file
;
2614 * Open and mmap() .PDB file
2617 LocateDebugInfoFile( full_filename
, filename
);
2619 if ( stat( filename
, &statbuf
) == -1 )
2621 fprintf( stderr
, "-Unable to open .PDB file %s\n", filename
);
2625 fd
= open(filename
, O_RDONLY
);
2628 fprintf( stderr
, "-Unable to open .PDB file %s\n", filename
);
2632 image
= mmap( 0, statbuf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0 );
2633 if ( image
== (char *) 0xffffffff )
2635 fprintf(stderr
, "-Unable to mmap .PDB file %s\n", filename
);
2640 * Read in TOC and well-known files
2643 pdb
= (PPDB_HEADER
)image
;
2644 toc
= pdb_read( image
, pdb
->toc_block
, pdb
->toc
.size
);
2645 root
= pdb_read_file( image
, toc
, 1 );
2646 types_image
= pdb_read_file( image
, toc
, 2 );
2647 symbols_image
= pdb_read_file( image
, toc
, 3 );
2649 pdb_convert_types_header( &types
, types_image
);
2650 pdb_convert_symbols_header( &symbols
, &header_size
, symbols_image
);
2653 * Check for unknown versions
2656 switch ( root
->version
)
2658 case 19950623: /* VC 4.0 */
2660 case 19960307: /* VC 5.0 */
2661 case 19970604: /* VC 6.0 */
2664 fprintf( stderr
, "-Unknown root block version %ld\n", root
->version
);
2667 switch ( types
.version
)
2669 case 19950410: /* VC 4.0 */
2671 case 19961031: /* VC 5.0 / 6.0 */
2674 fprintf( stderr
, "-Unknown type info version %ld\n", types
.version
);
2677 switch ( symbols
.version
)
2679 case 0: /* VC 4.0 */
2680 case 19960307: /* VC 5.0 */
2681 case 19970606: /* VC 6.0 */
2684 fprintf( stderr
, "-Unknown symbol info version %ld\n", symbols
.version
);
2689 * Check .PDB time stamp
2692 if ( root
->TimeDateStamp
2693 != ((struct CodeViewDebug
*)deefer
->dbg_info
)->cv_timestamp
)
2695 fprintf(stderr
, "-Wrong time stamp of .PDB file %s\n", filename
);
2703 DEBUG_ParseTypeTable( types_image
+ types
.type_offset
, types
.type_size
);
2706 * Read type-server .PDB imports
2709 if ( symbols
.pdbimport_size
)
2712 fprintf(stderr
, "-Type server .PDB imports ignored!\n" );
2716 * Read global symbol table
2719 modimage
= pdb_read_file( image
, toc
, symbols
.gsym_file
);
2722 DEBUG_SnarfCodeView( deefer
, modimage
,
2723 toc
->file
[symbols
.gsym_file
].size
, NULL
);
2724 pdb_free( modimage
);
2728 * Read per-module symbol / linenumber tables
2731 file
= symbols_image
+ header_size
;
2732 while ( file
- symbols_image
< header_size
+ symbols
.module_size
)
2734 int file_nr
, file_index
, symbol_size
, lineno_size
;
2737 if ( !symbols
.extended_format
)
2739 PDB_SYMBOL_FILE
*sym_file
= (PDB_SYMBOL_FILE
*) 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
;
2748 PDB_SYMBOL_FILE_EX
*sym_file
= (PDB_SYMBOL_FILE_EX
*) file
;
2749 file_nr
= sym_file
->file
;
2750 file_name
= sym_file
->filename
;
2751 file_index
= sym_file
->range
.index
;
2752 symbol_size
= sym_file
->symbol_size
;
2753 lineno_size
= sym_file
->lineno_size
;
2756 modimage
= pdb_read_file( image
, toc
, file_nr
);
2759 struct codeview_linetab_hdr
*linetab
= NULL
;
2762 linetab
= DEBUG_SnarfLinetab( modimage
+ symbol_size
, lineno_size
);
2765 DEBUG_SnarfCodeView( deefer
, modimage
+ sizeof(DWORD
),
2766 symbol_size
- sizeof(DWORD
), linetab
);
2768 pdb_free( modimage
);
2771 file_name
+= strlen(file_name
) + 1;
2772 file
= (char *)( (DWORD
)(file_name
+ strlen(file_name
) + 1 + 3) & ~3 );
2782 if ( symbols_image
) pdb_free( symbols_image
);
2783 if ( types_image
) pdb_free( types_image
);
2784 if ( root
) pdb_free( root
);
2785 if ( toc
) pdb_free( toc
);
2787 if ( image
!= (char *) 0xffffffff ) munmap( image
, statbuf
.st_size
);
2788 if ( fd
!= -1 ) close( fd
);
2795 * Process DBG file which contains debug information.
2799 DEBUG_ProcessDBGFile(struct deferred_debug_info
* deefer
, char * filename
)
2801 char * addr
= (char *) 0xffffffff;
2803 struct CV4_DirHead
* codeview_dir
;
2804 struct CV4_DirEnt
* codeview_dent
;
2805 PIMAGE_DEBUG_DIRECTORY dbghdr
;
2806 struct deferred_debug_info deefer2
;
2810 struct codeview_linetab_hdr
* linetab
;
2812 PIMAGE_SEPARATE_DEBUG_HEADER pdbg
= NULL
;
2813 IMAGE_SECTION_HEADER
* sectp
;
2814 struct stat statbuf
;
2816 char dbg_file
[MAX_PATHNAME_LEN
];
2818 LocateDebugInfoFile(filename
, dbg_file
);
2819 status
= stat(dbg_file
, &statbuf
);
2822 fprintf(stderr
, "-Unable to open .DBG file %s\n", dbg_file
);
2827 * Now open the file, so that we can mmap() it.
2829 fd
= open(dbg_file
, O_RDONLY
);
2832 fprintf(stderr
, "Unable to open .DBG file %s\n", dbg_file
);
2838 * Now mmap() the file.
2840 addr
= mmap(0, statbuf
.st_size
, PROT_READ
,
2841 MAP_PRIVATE
, fd
, 0);
2842 if( addr
== (char *) 0xffffffff )
2844 fprintf(stderr
, "Unable to mmap .DBG file %s\n", dbg_file
);
2848 pdbg
= (PIMAGE_SEPARATE_DEBUG_HEADER
) addr
;
2850 if( pdbg
->TimeDateStamp
!= deefer
->dbgdir
->TimeDateStamp
)
2852 fprintf(stderr
, "Warning - %s has incorrect internal timestamp\n",
2856 Well, sometimes this happens to DBG files which ARE REALLY the right .DBG
2857 files but nonetheless this check fails. Anyway, WINDBG (debugger for
2858 Windows by Microsoft) loads debug symbols which have incorrect timestamps.
2862 fprintf(stderr
, "Processing symbols from %s...\n", dbg_file
);
2864 dbghdr
= (PIMAGE_DEBUG_DIRECTORY
) ( addr
+ sizeof(*pdbg
)
2865 + pdbg
->NumberOfSections
* sizeof(IMAGE_SECTION_HEADER
)
2866 + pdbg
->ExportedNamesSize
);
2868 sectp
= (PIMAGE_SECTION_HEADER
) ((char *) pdbg
+ sizeof(*pdbg
));
2869 nsect
= pdbg
->NumberOfSections
;
2871 for( i
=0; i
< pdbg
->DebugDirectorySize
/ sizeof(*pdbg
); i
++, dbghdr
++ )
2873 switch(dbghdr
->Type
)
2875 case IMAGE_DEBUG_TYPE_COFF
:
2877 * Dummy up a deferred debug header to handle the
2878 * COFF stuff embedded within the DBG file.
2880 memset((char *) &deefer2
, 0, sizeof(deefer2
));
2881 deefer2
.dbg_info
= (addr
+ dbghdr
->PointerToRawData
);
2882 deefer2
.dbg_size
= dbghdr
->SizeOfData
;
2883 deefer2
.load_addr
= deefer
->load_addr
;
2885 DEBUG_ProcessCoff(&deefer2
);
2887 case IMAGE_DEBUG_TYPE_CODEVIEW
:
2889 * This is the older format by which codeview stuff is
2890 * stored, known as the 'NB09' format. Newer executables
2891 * and dlls created by VC++ use PDB files instead, which
2892 * have lots of internal similarities, but the overall
2893 * format and structure is quite different.
2895 codeview
= (addr
+ dbghdr
->PointerToRawData
);
2898 * The first thing in the codeview section should be
2899 * an 'NB09' identifier. As a sanity check, make sure
2902 if( *((unsigned int*) codeview
) != 0x3930424e )
2908 * Next we need to find the directory. This is easy too.
2910 codeview_dir
= (struct CV4_DirHead
*)
2911 (codeview
+ ((unsigned int*) codeview
)[1]);
2914 * Some more sanity checks. Make sure that everything
2915 * is as we expect it.
2917 if( codeview_dir
->next_offset
!= 0
2918 || codeview_dir
->dhsize
!= sizeof(*codeview_dir
)
2919 || codeview_dir
->desize
!= sizeof(*codeview_dent
) )
2923 codeview_dent
= (struct CV4_DirEnt
*) (codeview_dir
+ 1);
2925 for(j
=0; j
< codeview_dir
->ndir
; j
++, codeview_dent
++)
2927 if( codeview_dent
->subsect_number
== sstAlignSym
)
2930 * Check the previous entry. If it is a
2931 * sstSrcModule, it contains the line number
2932 * info for this file.
2935 if( codeview_dent
[1].module_number
== codeview_dent
[0].module_number
2936 && codeview_dent
[1].subsect_number
== sstSrcModule
)
2938 linetab
= DEBUG_SnarfLinetab(
2939 codeview
+ codeview_dent
[1].offset
,
2940 codeview_dent
[1].size
);
2943 if( codeview_dent
[-1].module_number
== codeview_dent
[0].module_number
2944 && codeview_dent
[-1].subsect_number
== sstSrcModule
)
2946 linetab
= DEBUG_SnarfLinetab(
2947 codeview
+ codeview_dent
[-1].offset
,
2948 codeview_dent
[-1].size
);
2951 * Now process the CV stuff.
2953 DEBUG_SnarfCodeView(deefer
,
2954 codeview
+ codeview_dent
->offset
+ sizeof(DWORD
),
2955 codeview_dent
->size
- sizeof(DWORD
),
2967 if( addr
!= (char *) 0xffffffff )
2969 munmap(addr
, statbuf
.st_size
);
2981 DEBUG_ProcessDeferredDebug(void)
2983 struct deferred_debug_info
* deefer
;
2984 struct CodeViewDebug
* cvd
;
2985 struct MiscDebug
* misc
;
2991 for(deefer
= dbglist
; deefer
; deefer
= deefer
->next
)
2993 if( deefer
->status
!= DF_STATUS_NEW
)
2998 if( last_proc
!= deefer
->dbg_index
)
3002 fprintf(stderr
, "DeferredDebug for:");
3005 fprintf(stderr
, " %s",deefer
->module_name
);
3006 last_proc
= deefer
->dbg_index
;
3009 switch(deefer
->dbgdir
->Type
)
3011 case IMAGE_DEBUG_TYPE_COFF
:
3013 * Standard COFF debug information that VC++ adds when you
3014 * use /debugtype:both with the linker.
3017 fprintf(stderr
, "Processing COFF symbols...\n");
3019 sts
= DEBUG_ProcessCoff(deefer
);
3021 case IMAGE_DEBUG_TYPE_CODEVIEW
:
3023 * This is a pointer to a PDB file of some sort.
3025 cvd
= (struct CodeViewDebug
*) deefer
->dbg_info
;
3027 if( strcmp(cvd
->cv_nbtype
, "NB10") != 0 )
3030 * Whatever this is, we don't know how to deal with
3036 sts
= DEBUG_ProcessPDBFile(deefer
, cvd
->cv_name
);
3038 fprintf(stderr
, "Processing PDB file %s\n", cvd
->cv_name
);
3041 case IMAGE_DEBUG_TYPE_MISC
:
3043 * A pointer to a .DBG file of some sort. These files
3044 * can contain either CV4 or COFF information. Open
3045 * the file, and try to do the right thing with it.
3047 misc
= (struct MiscDebug
*) deefer
->dbg_info
;
3049 filename
= strrchr((char *) &misc
->Data
, '.');
3052 * Ignore the file if it doesn't have a .DBG extension.
3054 if( (filename
== NULL
)
3055 || ( (strcmp(filename
, ".dbg") != 0)
3056 && (strcmp(filename
, ".DBG") != 0)) )
3062 filename
= (char *) &misc
->Data
;
3065 * Do the dirty deed...
3067 sts
= DEBUG_ProcessDBGFile(deefer
, filename
);
3072 * We should never get here...
3077 deefer
->status
= (sts
) ? DF_STATUS_LOADED
: DF_STATUS_ERROR
;
3081 fprintf(stderr
, "\n");
3086 /***********************************************************************
3089 * Display shared libarary information.
3091 void DEBUG_InfoShare(void)
3093 struct deferred_debug_info
* deefer
;
3095 fprintf(stderr
,"Address\t\tModule\tName\n");
3097 for(deefer
= dbglist
; deefer
; deefer
= deefer
->next
)
3099 fprintf(stderr
,"0x%8.8x\t(%s)\t%s\n", (unsigned int) deefer
->load_addr
,
3100 deefer
->module
? "Win32" : "ELF", deefer
->module_name
);