2 * File msc.c - read VC++ debug information from COFF and eventually
5 * Copyright (C) 1996, Eric Youngdale.
7 * Note - this handles reading debug information for 32 bit applications
8 * that run under Windows-NT for example. I doubt that this would work well
9 * for 16 bit applications, but I don't think it really matters since the
10 * file format is different, and we should never get in here in such cases.
13 * Get 16 bit CV stuff working.
14 * Add symbol size to internal symbol table.
20 #include <sys/types.h>
28 #define PATH_MAX _MAX_PATH
39 *dbg_filename must be at least MAX_PATHNAME_LEN bytes in size
41 static void LocateDebugInfoFile(char *filename
, char *dbg_filename
)
43 char str1
[MAX_PATHNAME_LEN
*10];
44 char str2
[MAX_PATHNAME_LEN
];
47 DOS_FULL_NAME fullname
;
49 file
= strrchr(filename
, '\\');
50 if( file
== NULL
) file
= filename
; else file
++;
52 if (GetEnvironmentVariable32A("_NT_SYMBOL_PATH", str1
, sizeof(str1
)))
53 if (SearchPath32A(str1
, file
, NULL
, sizeof(str2
), str2
, &name_part
))
55 if (GetEnvironmentVariable32A("_NT_ALT_SYMBOL_PATH", str1
, sizeof(str1
)))
56 if (SearchPath32A(str1
, file
, NULL
, sizeof(str2
), str2
, &name_part
))
58 if (SearchPath32A(NULL
, file
, NULL
, sizeof(str2
), str2
, &name_part
))
62 quit
: memcpy(dbg_filename
, filename
, MAX_PATHNAME_LEN
);
66 if (DOSFS_GetFullName(str2
, TRUE
, &fullname
))
67 memcpy(dbg_filename
, fullname
.long_name
, MAX_PATHNAME_LEN
);
73 * This is an index we use to keep track of the debug information
74 * when we have multiple sources. We use the same database to also
75 * allow us to do an 'info shared' type of deal, and we use the index
76 * to eliminate duplicates.
78 static int DEBUG_next_index
= 0;
89 * This is a convenience structure used to map portions of the
99 * This is how we reference the various record types.
101 union codeview_symbol
115 unsigned short symtype
;
116 unsigned char namelen
;
117 unsigned char name
[1];
124 unsigned int pparent
;
128 unsigned short segment
;
129 unsigned short thunk_len
;
130 unsigned char thtype
;
131 unsigned char namelen
;
132 unsigned char name
[1];
138 unsigned int pparent
;
141 unsigned int proc_len
;
142 unsigned int debug_start
;
143 unsigned int debug_end
;
145 unsigned short segment
;
146 unsigned short proctype
;
148 unsigned char namelen
;
149 unsigned char name
[1];
153 short int len
; /* Total length of this entry */
154 short int id
; /* Always S_BPREL32 */
155 unsigned int offset
; /* Stack offset relative to BP */
156 unsigned short symtype
;
157 unsigned char namelen
;
158 unsigned char name
[1];
176 unsigned char variant
[1];
184 unsigned char bitoff
;
194 unsigned char arrlen
;
195 unsigned char namelen
;
196 unsigned char name
[1];
208 unsigned short structlen
;
209 unsigned char namelen
;
210 unsigned char name
[1];
220 unsigned short un_len
;
221 unsigned char namelen
;
222 unsigned char name
[1];
233 unsigned char namelen
;
234 unsigned char name
[1];
241 unsigned short int value
;
242 unsigned char namelen
;
243 unsigned char name
[1];
251 unsigned short int offset
;
252 unsigned char namelen
;
253 unsigned char name
[1];
264 unsigned char namelen
;
265 unsigned char name
[1];
270 #define S_BPREL32 0x200
271 #define S_LDATA32 0x201
272 #define S_GDATA32 0x202
273 #define S_PUB32 0x203
274 #define S_LPROC32 0x204
275 #define S_GPROC32 0x205
276 #define S_THUNK32 0x206
277 #define S_BLOCK32 0x207
278 #define S_WITH32 0x208
279 #define S_LABEL32 0x209
281 #define S_PROCREF 0x400
282 #define S_DATAREF 0x401
283 #define S_ALIGN 0x402
284 #define S_UNKNOWN 0x403
287 * This covers the basic datatypes that VC++ seems to be using these days.
288 * 32 bit mode only. There are additional numbers for the pointers in 16
289 * bit mode. There are many other types listed in the documents, but these
290 * are apparently not used by the compiler, or represent pointer types
293 #define T_NOTYPE 0x0000 /* Notype */
294 #define T_ABS 0x0001 /* Abs */
295 #define T_VOID 0x0003 /* Void */
296 #define T_CHAR 0x0010 /* signed char */
297 #define T_SHORT 0x0011 /* short */
298 #define T_LONG 0x0012 /* long */
299 #define T_QUAD 0x0013 /* long long */
300 #define T_UCHAR 0x0020 /* unsigned char */
301 #define T_USHORT 0x0021 /* unsigned short */
302 #define T_ULONG 0x0022 /* unsigned long */
303 #define T_UQUAD 0x0023 /* unsigned long long */
304 #define T_REAL32 0x0040 /* float */
305 #define T_REAL64 0x0041 /* double */
306 #define T_RCHAR 0x0070 /* real char */
307 #define T_WCHAR 0x0071 /* wide char */
308 #define T_INT4 0x0074 /* int */
309 #define T_UINT4 0x0075 /* unsigned int */
311 #define T_32PVOID 0x0403 /* 32 bit near pointer to void */
312 #define T_32PCHAR 0x0410 /* 16:32 near pointer to signed char */
313 #define T_32PSHORT 0x0411 /* 16:32 near pointer to short */
314 #define T_32PLONG 0x0412 /* 16:32 near pointer to int */
315 #define T_32PQUAD 0x0413 /* 16:32 near pointer to long long */
316 #define T_32PUCHAR 0x0420 /* 16:32 near pointer to unsigned char */
317 #define T_32PUSHORT 0x0421 /* 16:32 near pointer to unsigned short */
318 #define T_32PULONG 0x0422 /* 16:32 near pointer to unsigned int */
319 #define T_32PUQUAD 0x0423 /* 16:32 near pointer to long long */
320 #define T_32PREAL32 0x0440 /* 16:32 near pointer to float */
321 #define T_32PREAL64 0x0441 /* 16:32 near pointer to float */
322 #define T_32PRCHAR 0x0470 /* 16:32 near pointer to real char */
323 #define T_32PWCHAR 0x0471 /* 16:32 near pointer to real char */
324 #define T_32PINT4 0x0474 /* 16:32 near pointer to int */
325 #define T_32PUINT4 0x0475 /* 16:32 near pointer to unsigned int */
327 #define LF_MODIFIER 0x1
328 #define LF_POINTER 0x2
331 #define LF_STRUCTURE 0x5
333 #define LF_ENUMERATION 0x7
334 #define LF_PROCEDURE 0x8
335 #define LF_MFUNCTION 0x9
336 #define LF_VTSHAPE 0xa
337 #define LF_BARRAY 0xd
338 #define LF_DIMARRAY 0x11
339 #define LF_VFTPATH 0x12
341 #define LF_SKIP 0x200
342 #define LF_ARGLIST 0x201
343 #define LF_FIELDLIST 0x204
344 #define LF_DERIVED 0x205
345 #define LF_BITFIELD 0x206
347 #define LF_BCLASS 0x400
348 #define LF_VBCLASS 0x401
349 #define LF_IVBCLASS 0x402
350 #define LF_ENUMERATE 0x403
351 #define LF_FRIENDFCN 0x404
352 #define LF_INDEX 0x405
353 #define LF_MEMBER 0x406
354 #define LF_STMEMBER 0x407
355 #define LF_METHOD 0x408
356 #define LF_NESTEDTYPE 0x409
357 #define LF_VFUNCTAB 0x40a
358 #define LF_FRIENDCLS 0x40b
359 #define LF_ONEMETHOD 0x40c
360 #define LF_FUNCOFF 0x40d
362 #define MAX_BUILTIN_TYPES 0x480
363 static struct datatype
* cv_basic_types
[MAX_BUILTIN_TYPES
];
364 static int num_cv_defined_types
= 0;
365 static struct datatype
**cv_defined_types
= NULL
;
368 * For the type CODEVIEW debug directory entries, the debug directory
369 * points to a structure like this. The cv_name field is the name
370 * of an external .PDB file.
375 unsigned int cv_timestamp
;
381 unsigned int DataType
;
389 * This is the header that the COFF variety of debug header points to.
393 unsigned int SymbolOffset
;
394 unsigned int N_Linenum
;
395 unsigned int LinenumberOffset
;
396 unsigned int Unused
[4];
400 unsigned int VirtualAddr
;
401 unsigned short int Linenum
;
405 unsigned int startaddr
;
406 unsigned int endaddr
;
410 struct name_hash
**entries
;
420 unsigned int NotLong
;
421 unsigned int StrTaboff
;
428 unsigned char NumberOfAuxSymbols
;
431 struct CoffAuxSection
{
433 unsigned short NumberOfRelocations
;
434 unsigned short NumberOfLinenumbers
;
435 unsigned int CheckSum
;
441 * These two structures are used in the directory within a .DBG file
442 * to locate the individual important bits that we might want to see.
445 short unsigned int dhsize
;
446 short unsigned int desize
;
448 unsigned int next_offset
;
453 short unsigned int subsect_number
;
454 short unsigned int module_number
;
460 * These are the values of interest that the subsect_number field takes.
462 #define sstAlignSym 0x125
463 #define sstSrcModule 0x127
465 struct codeview_linetab_hdr
472 unsigned short * linetab
;
473 unsigned int * offtab
;
476 struct codeview_pdb_hdr
479 unsigned int blocksize
; /* Extent size */
480 unsigned short loc_freelist
; /* freelist. */
481 unsigned short alloc_filesize
; /* # extents allocated. */
482 unsigned int toc_len
;
483 unsigned int unknown
;
484 unsigned short toc_ext
[1]; /* array of extent #'s for toc. */
488 * This is our own structure that we use to keep track of the contents
495 short int * extent_list
;
496 unsigned int linetab_offset
;
497 unsigned int linetab_len
;
501 * These are the structures that represent how the file table is set up
502 * within the PDB file.
506 unsigned short tab1_file
;
507 unsigned short tab2_file
;
508 unsigned short gsym_file
;
509 unsigned short padding
;
510 unsigned int ftab_len
;
511 unsigned int fofftab_len
;
512 unsigned int hash_len
;
513 unsigned int strtab_len
;
518 unsigned int reserved1
;
519 unsigned short datasect_segment
;
520 unsigned short reserved2
;
521 unsigned int datasect_offset
;
522 unsigned int datasect_size
;
523 unsigned int datasect_flags
;
524 unsigned short reserved3
;
525 unsigned short index
;
526 unsigned short num6a
;
527 unsigned short file_number
;
528 unsigned int linetab_offset
;
529 unsigned int linetab_len
;
533 unsigned char filename
[1];
537 ********************************************************************
539 struct deferred_debug_info
541 struct deferred_debug_info
* next
;
547 PIMAGE_DEBUG_DIRECTORY dbgdir
;
548 PIMAGE_SECTION_HEADER sectp
;
554 struct deferred_debug_info
* dbglist
= NULL
;
557 * A simple macro that tells us whether a given COFF symbol is a
560 #define N_TMASK 0x0030
561 #define IMAGE_SYM_DTYPE_FUNCTION 2
563 #define ISFCN(x) (((x) & N_TMASK) == (IMAGE_SYM_DTYPE_FUNCTION << N_BTSHFT))
567 * This is what we are looking for in the COFF symbols.
569 #define IMAGE_SYM_CLASS_EXTERNAL 0x2
570 #define IMAGE_SYM_CLASS_STATIC 0x3
571 #define IMAGE_SYM_CLASS_FILE 0x67
574 struct datatype
* DEBUG_GetCVType(int typeno
)
576 struct datatype
* dt
= NULL
;
579 * Convert Codeview type numbers into something we can grok internally.
580 * Numbers < 0x1000 are all fixed builtin types. Numbers from 0x1000 and
581 * up are all user defined (structs, etc).
583 if( typeno
< 0x1000 )
585 if( typeno
< MAX_BUILTIN_TYPES
)
587 dt
= cv_basic_types
[typeno
];
592 if( typeno
- 0x1000 < num_cv_defined_types
)
594 dt
= cv_defined_types
[typeno
- 0x1000];
602 DEBUG_ParseTypeTable(char * table
, int len
)
606 enum debug_type fieldtype
;
610 struct datatype
* subtype
;
612 union codeview_type
* type
;
613 union codeview_type
* type2
;
614 struct datatype
* typeptr
;
618 ptr
= (union any_size
) (table
+ 16);
619 while( ptr
.c
- table
< len
)
621 type
= (union codeview_type
*) ptr
.c
;
623 if( curr_type
- 0x1000 >= num_cv_defined_types
)
625 num_cv_defined_types
+= 0x100;
626 cv_defined_types
= (struct datatype
**) realloc(cv_defined_types
,
627 num_cv_defined_types
* sizeof(struct datatype
*));
628 memset(cv_defined_types
+ num_cv_defined_types
- 0x100,
630 0x100 * sizeof(struct datatype
*));
631 if( cv_defined_types
== NULL
)
637 switch(type
->generic
.id
)
640 cv_defined_types
[curr_type
- 0x1000] =
641 DEBUG_FindOrMakePointerType(DEBUG_GetCVType(type
->pointer
.datatype
));
644 if( type
->array
.arrlen
>= 0x8000 )
647 * This is a numeric leaf, I am too lazy to handle this right
650 fprintf(stderr
, "Ignoring large numberic leaf.\n");
653 if( type
->array
.namelen
!= 0 )
655 memset(symname
, 0, sizeof(symname
));
656 memcpy(symname
, type
->array
.name
, type
->array
.namelen
);
657 typeptr
= DEBUG_NewDataType(DT_ARRAY
, symname
);
661 typeptr
= DEBUG_NewDataType(DT_ARRAY
, NULL
);
663 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
665 subtype
= DEBUG_GetCVType(type
->array
.elemtype
);
666 if( (subtype
== NULL
)
667 || (elem_size
= DEBUG_GetObjectSize(subtype
)) == 0 )
673 arr_max
= type
->array
.arrlen
/ DEBUG_GetObjectSize(subtype
);
676 DEBUG_SetArrayParams(typeptr
, 0, arr_max
, subtype
);
680 * This is where the basic list of fields is defined for
681 * structures and classes.
683 * First, we need to look ahead and see whether we are building
684 * a fieldlist for an enum or a struct.
687 type2
= (union codeview_type
*) ptr2
.c
;
688 if( type2
->member
.id
== LF_MEMBER
)
690 typeptr
= DEBUG_NewDataType(DT_STRUCT
, NULL
);
691 fieldtype
= DT_STRUCT
;
693 else if( type2
->member
.id
== LF_ENUMERATE
)
695 typeptr
= DEBUG_NewDataType(DT_ENUM
, NULL
);
703 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
704 while( ptr2
.c
< (ptr
.c
+ ((type
->generic
.len
+ 3) & ~3)) )
706 type2
= (union codeview_type
*) ptr2
.c
;
707 if( type2
->member
.id
== LF_MEMBER
&& fieldtype
== DT_STRUCT
)
709 memset(symname
, 0, sizeof(symname
));
710 memcpy(symname
, type2
->member
.name
, type2
->member
.namelen
);
712 subtype
= DEBUG_GetCVType(type2
->member
.type
);
714 if( subtype
!= NULL
)
716 elem_size
= DEBUG_GetObjectSize(subtype
);
719 if( type2
->member
.offset
>= 0x8000 )
722 * This is a numeric leaf, I am too lazy to handle this right
725 fprintf(stderr
, "Ignoring large numberic leaf.\n");
729 DEBUG_AddStructElement(typeptr
, symname
, subtype
,
730 type2
->member
.offset
<< 3,
734 else if( type2
->member
.id
== LF_ENUMERATE
&& fieldtype
== DT_ENUM
)
736 memset(symname
, 0, sizeof(symname
));
737 memcpy(symname
, type2
->enumerate
.name
, type2
->enumerate
.namelen
);
739 if( type2
->enumerate
.value
>= 0x8000 )
742 * This is a numeric leaf, I am too lazy to handle this right
745 fprintf(stderr
, "Ignoring large numberic leaf.\n");
749 DEBUG_AddStructElement(typeptr
, symname
, NULL
,
750 type2
->enumerate
.value
, 0);
756 * Something else I have never seen before. Either wrong type of
757 * object in the fieldlist, or some other problem which I wouldn't
758 * really know how to handle until it came up.
760 fprintf(stderr
, "Unexpected entry in fieldlist\n");
765 ptr2
.c
+= ((type2
->member
.namelen
+ 9 + 3) & ~3);
770 if( type
->structure
.structlen
>= 0x8000 )
773 * This is a numeric leaf, I am too lazy to handle this right
776 fprintf(stderr
, "Ignoring large numberic leaf.\n");
779 memset(symname
, 0, sizeof(symname
));
780 memcpy(symname
, type
->structure
.name
, type
->structure
.namelen
);
781 if( strcmp(symname
, "__unnamed") == 0 )
783 typeptr
= DEBUG_NewDataType(DT_STRUCT
, NULL
);
787 typeptr
= DEBUG_NewDataType(DT_STRUCT
, symname
);
789 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
792 * Now copy the relevant bits from the fieldlist that we specified.
794 subtype
= DEBUG_GetCVType(type
->structure
.fieldlist
);
796 if( subtype
!= NULL
)
798 DEBUG_SetStructSize(typeptr
, type
->structure
.structlen
);
799 DEBUG_CopyFieldlist(typeptr
, subtype
);
803 if( type
->t_union
.un_len
>= 0x8000 )
806 * This is a numeric leaf, I am too lazy to handle this right
809 fprintf(stderr
, "Ignoring large numberic leaf.\n");
812 memset(symname
, 0, sizeof(symname
));
813 memcpy(symname
, type
->t_union
.name
, type
->t_union
.namelen
);
815 if( strcmp(symname
, "__unnamed") == 0 )
817 typeptr
= DEBUG_NewDataType(DT_STRUCT
, NULL
);
821 typeptr
= DEBUG_NewDataType(DT_STRUCT
, symname
);
824 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
827 * Now copy the relevant bits from the fieldlist that we specified.
829 subtype
= DEBUG_GetCVType(type
->t_union
.field
);
831 if( subtype
!= NULL
)
833 DEBUG_SetStructSize(typeptr
, type
->t_union
.un_len
);
834 DEBUG_CopyFieldlist(typeptr
, subtype
);
838 typeptr
= DEBUG_NewDataType(DT_BITFIELD
, NULL
);
839 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
840 DEBUG_SetBitfieldParams(typeptr
, type
->bitfield
.bitoff
,
841 type
->bitfield
.nbits
,
842 DEBUG_GetCVType(type
->bitfield
.type
));
845 memset(symname
, 0, sizeof(symname
));
846 memcpy(symname
, type
->enumeration
.name
, type
->enumeration
.namelen
);
847 typeptr
= DEBUG_NewDataType(DT_ENUM
, symname
);
848 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
851 * Now copy the relevant bits from the fieldlist that we specified.
853 subtype
= DEBUG_GetCVType(type
->enumeration
.field
);
855 if( subtype
!= NULL
)
857 DEBUG_CopyFieldlist(typeptr
, subtype
);
865 ptr
.c
+= (type
->generic
.len
+ 3) & ~3;
872 DEBUG_InitCVDataTypes()
875 * These are the common builtin types that are used by VC++.
877 cv_basic_types
[T_NOTYPE
] = NULL
;
878 cv_basic_types
[T_ABS
] = NULL
;
879 cv_basic_types
[T_VOID
] = DEBUG_NewDataType(DT_BASIC
, "void");
880 cv_basic_types
[T_CHAR
] = DEBUG_NewDataType(DT_BASIC
, "char");
881 cv_basic_types
[T_SHORT
] = DEBUG_NewDataType(DT_BASIC
, "short int");
882 cv_basic_types
[T_LONG
] = DEBUG_NewDataType(DT_BASIC
, "long int");
883 cv_basic_types
[T_QUAD
] = DEBUG_NewDataType(DT_BASIC
, "long long int");
884 cv_basic_types
[T_UCHAR
] = DEBUG_NewDataType(DT_BASIC
, "unsigned char");
885 cv_basic_types
[T_USHORT
] = DEBUG_NewDataType(DT_BASIC
, "short unsigned int");
886 cv_basic_types
[T_ULONG
] = DEBUG_NewDataType(DT_BASIC
, "long unsigned int");
887 cv_basic_types
[T_UQUAD
] = DEBUG_NewDataType(DT_BASIC
, "long long unsigned int");
888 cv_basic_types
[T_REAL32
] = DEBUG_NewDataType(DT_BASIC
, "float");
889 cv_basic_types
[T_REAL64
] = DEBUG_NewDataType(DT_BASIC
, "double");
890 cv_basic_types
[T_RCHAR
] = DEBUG_NewDataType(DT_BASIC
, "char");
891 cv_basic_types
[T_WCHAR
] = DEBUG_NewDataType(DT_BASIC
, "short");
892 cv_basic_types
[T_INT4
] = DEBUG_NewDataType(DT_BASIC
, "int");
893 cv_basic_types
[T_UINT4
] = DEBUG_NewDataType(DT_BASIC
, "unsigned int");
895 cv_basic_types
[T_32PVOID
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_VOID
]);
896 cv_basic_types
[T_32PCHAR
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_CHAR
]);
897 cv_basic_types
[T_32PSHORT
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_SHORT
]);
898 cv_basic_types
[T_32PLONG
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_LONG
]);
899 cv_basic_types
[T_32PQUAD
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_QUAD
]);
900 cv_basic_types
[T_32PUCHAR
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_UCHAR
]);
901 cv_basic_types
[T_32PUSHORT
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_USHORT
]);
902 cv_basic_types
[T_32PULONG
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_ULONG
]);
903 cv_basic_types
[T_32PUQUAD
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_UQUAD
]);
904 cv_basic_types
[T_32PREAL32
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_REAL32
]);
905 cv_basic_types
[T_32PREAL64
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_REAL64
]);
906 cv_basic_types
[T_32PRCHAR
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_RCHAR
]);
907 cv_basic_types
[T_32PWCHAR
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_WCHAR
]);
908 cv_basic_types
[T_32PINT4
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_INT4
]);
909 cv_basic_types
[T_32PUINT4
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_UINT4
]);
913 * In this function, we keep track of deferred debugging information
914 * that we may need later if we were to need to use the internal debugger.
915 * We don't fully process it here for performance reasons.
918 DEBUG_RegisterDebugInfo( HMODULE32 hModule
, const char *module_name
,
919 u_long v_addr
, u_long size
)
921 int has_codeview
= FALSE
;
924 PIMAGE_DEBUG_DIRECTORY dbgptr
;
927 dbgptr
= (PIMAGE_DEBUG_DIRECTORY
) (hModule
+ v_addr
);
928 for(; size
>= sizeof(*dbgptr
); size
-= sizeof(*dbgptr
), dbgptr
++ )
932 case IMAGE_DEBUG_TYPE_CODEVIEW
:
933 case IMAGE_DEBUG_TYPE_MISC
:
940 dbgptr
= (PIMAGE_DEBUG_DIRECTORY
) (hModule
+ v_addr
);
941 for(; size
>= sizeof(*dbgptr
); size
-= sizeof(*dbgptr
), dbgptr
++ )
945 case IMAGE_DEBUG_TYPE_COFF
:
947 * If we have both codeview and COFF debug info, ignore the
948 * coff debug info as it would just confuse us, and it is
951 * FIXME - this is broken - if we cannot find the PDB file, then
952 * we end up with no debugging info at all. In this case, we
953 * should use the COFF info as a backup.
959 case IMAGE_DEBUG_TYPE_CODEVIEW
:
960 case IMAGE_DEBUG_TYPE_MISC
:
962 * This is usually an indirection to a .DBG file.
963 * This is similar to (but a slightly older format) from the
966 * First check to see if the image was 'stripped'. If so, it
967 * means that this entry points to a .DBG file. Otherwise,
968 * it just points to itself, and we can ignore this.
976 if( (dbgptr
->Type
!= IMAGE_DEBUG_TYPE_MISC
) ||
977 (PE_HEADER(hModule
)->FileHeader
.Characteristics
& IMAGE_FILE_DEBUG_STRIPPED
) != 0 )
981 DOS_FULL_NAME full_name
;
982 struct deferred_debug_info
* deefer
= (struct deferred_debug_info
*) xmalloc(sizeof(*deefer
));
984 deefer
->module
= hModule
;
985 deefer
->load_addr
= (char *)hModule
;
988 * Read the important bits. What we do after this depends
989 * upon the type, but this is always enough so we are able
990 * to proceed if we know what we need to do next.
992 /* basically, the PE loader maps all sections (data, resources...), but doesn't map
993 * the DataDirectory array's content. One its entry contains the *beloved*
994 * debug information. (Note the DataDirectory is mapped, not its content)
997 if (GetModuleFileName32A(hModule
, fn
, sizeof(fn
)) > 0 &&
998 DOSFS_GetFullName(fn
, TRUE
, &full_name
) &&
999 (fd
= open(full_name
.long_name
, O_RDONLY
)) > 0)
1001 deefer
->dbg_info
= mmap(NULL
, dbgptr
->SizeOfData
,
1002 PROT_READ
, MAP_PRIVATE
, fd
, dbgptr
->PointerToRawData
);
1004 if( deefer
->dbg_info
== (char *) 0xffffffff )
1013 fprintf(stderr
, " (not mapped: fn=%s, lfn=%s, fd=%d)", fn
, full_name
.long_name
, fd
);
1016 deefer
->dbg_size
= dbgptr
->SizeOfData
;
1017 deefer
->dbgdir
= dbgptr
;
1018 deefer
->next
= dbglist
;
1019 deefer
->loaded
= FALSE
;
1020 deefer
->dbg_index
= DEBUG_next_index
;
1021 deefer
->module_name
= xstrdup(module_name
);
1023 deefer
->sectp
= PE_SECTIONS(hModule
);
1024 deefer
->nsect
= PE_HEADER(hModule
)->FileHeader
.NumberOfSections
;
1040 * ELF modules are also entered into the list - this is so that we
1041 * can make 'info shared' types of displays possible.
1044 DEBUG_RegisterELFDebugInfo(int load_addr
, u_long size
, char * name
)
1046 struct deferred_debug_info
* deefer
;
1048 deefer
= (struct deferred_debug_info
*) xmalloc(sizeof(*deefer
));
1052 * Read the important bits. What we do after this depends
1053 * upon the type, but this is always enough so we are able
1054 * to proceed if we know what we need to do next.
1056 deefer
->dbg_size
= size
;
1057 deefer
->dbg_info
= (char *) NULL
;
1059 deefer
->load_addr
= (char *) load_addr
;
1060 deefer
->dbgdir
= NULL
;
1061 deefer
->next
= dbglist
;
1062 deefer
->loaded
= TRUE
;
1063 deefer
->dbg_index
= DEBUG_next_index
;
1064 deefer
->module_name
= xstrdup(name
);
1075 * Process COFF debugging information embedded in a Win32 application.
1080 DEBUG_ProcessCoff(struct deferred_debug_info
* deefer
)
1082 struct CoffAuxSection
* aux
;
1083 struct CoffDebug
* coff
;
1084 struct CoffFiles
* coff_files
= NULL
;
1085 struct CoffLinenum
* coff_linetab
;
1087 struct CoffSymbol
* coff_sym
;
1088 struct CoffSymbol
* coff_symbol
;
1089 struct CoffFiles
* curr_file
= NULL
;
1093 struct CoffLinenum
* linepnt
;
1100 int nfiles_alloc
= 0;
1101 struct CoffFiles orig_file
;
1103 char * this_file
= NULL
;
1105 coff
= (struct CoffDebug
*) deefer
->dbg_info
;
1107 coff_symbol
= (struct CoffSymbol
*) ((unsigned int) coff
+ coff
->SymbolOffset
);
1108 coff_linetab
= (struct CoffLinenum
*) ((unsigned int) coff
+ coff
->LinenumberOffset
);
1109 coff_strtab
= (char *) ((unsigned int) coff_symbol
+ 18*coff
->N_Sym
);
1113 for(i
=0; i
< coff
->N_Sym
; i
++ )
1116 * We do this because some compilers (i.e. gcc) incorrectly
1117 * pad the structure up to a 4 byte boundary. The structure
1118 * is really only 18 bytes long, so we have to manually make sure
1121 * FIXME - there must be a way to have autoconf figure out the
1122 * correct compiler option for this. If it is always gcc, that
1123 * makes life simpler, but I don't want to force this.
1125 coff_sym
= (struct CoffSymbol
*) ((unsigned int) coff_symbol
+ 18*i
);
1126 naux
= coff_sym
->NumberOfAuxSymbols
;
1128 if( coff_sym
->StorageClass
== IMAGE_SYM_CLASS_FILE
)
1130 if( nfiles
+ 1 >= nfiles_alloc
)
1133 coff_files
= (struct CoffFiles
*) realloc( coff_files
,
1134 nfiles_alloc
* sizeof(struct CoffFiles
));
1136 curr_file
= coff_files
+ nfiles
;
1138 curr_file
->startaddr
= 0xffffffff;
1139 curr_file
->endaddr
= 0;
1140 curr_file
->filename
= ((char *) coff_sym
) + 18;
1141 curr_file
->linetab_offset
= -1;
1142 curr_file
->linecnt
= 0;
1143 curr_file
->entries
= NULL
;
1144 curr_file
->neps
= curr_file
->neps_alloc
= 0;
1146 fprintf(stderr
,"New file %s\n", curr_file
->filename
);
1153 * This guy marks the size and location of the text section
1154 * for the current file. We need to keep track of this so
1155 * we can figure out what file the different global functions
1158 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_STATIC
)
1160 && (coff_sym
->Type
== 0)
1161 && (coff_sym
->SectionNumber
== 1) )
1163 aux
= (struct CoffAuxSection
*) ((unsigned int) coff_sym
+ 18);
1165 if( curr_file
->linetab_offset
!= -1 )
1168 fprintf(stderr
, "Duplicating sect from %s: %x %x %x %d %d\n",
1169 curr_file
->filename
,
1171 aux
->NumberOfRelocations
,
1172 aux
->NumberOfLinenumbers
,
1175 fprintf(stderr
, "More sect %d %x %d %d %d\n",
1176 coff_sym
->SectionNumber
,
1179 coff_sym
->StorageClass
,
1180 coff_sym
->NumberOfAuxSymbols
);
1184 * Save this so we can copy bits from it.
1186 orig_file
= *curr_file
;
1189 * Duplicate the file entry. We have no way to describe
1190 * multiple text sections in our current way of handling things.
1192 if( nfiles
+ 1 >= nfiles_alloc
)
1195 coff_files
= (struct CoffFiles
*) realloc( coff_files
,
1196 nfiles_alloc
* sizeof(struct CoffFiles
));
1198 curr_file
= coff_files
+ nfiles
;
1200 curr_file
->startaddr
= 0xffffffff;
1201 curr_file
->endaddr
= 0;
1202 curr_file
->filename
= orig_file
.filename
;
1203 curr_file
->linetab_offset
= -1;
1204 curr_file
->linecnt
= 0;
1205 curr_file
->entries
= NULL
;
1206 curr_file
->neps
= curr_file
->neps_alloc
= 0;
1211 fprintf(stderr
, "New text sect from %s: %x %x %x %d %d\n",
1212 curr_file
->filename
,
1214 aux
->NumberOfRelocations
,
1215 aux
->NumberOfLinenumbers
,
1221 if( curr_file
->startaddr
> coff_sym
->Value
)
1223 curr_file
->startaddr
= coff_sym
->Value
;
1226 if( curr_file
->startaddr
> coff_sym
->Value
)
1228 curr_file
->startaddr
= coff_sym
->Value
;
1231 if( curr_file
->endaddr
< coff_sym
->Value
+ aux
->Length
)
1233 curr_file
->endaddr
= coff_sym
->Value
+ aux
->Length
;
1236 curr_file
->linetab_offset
= linetab_indx
;
1237 curr_file
->linecnt
= aux
->NumberOfLinenumbers
;
1238 linetab_indx
+= aux
->NumberOfLinenumbers
;
1243 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_STATIC
)
1245 && (coff_sym
->SectionNumber
== 1) )
1248 * This is a normal static function when naux == 0.
1249 * Just register it. The current file is the correct
1250 * one in this instance.
1252 if( coff_sym
->N
.Name
.NotLong
)
1254 memcpy(namebuff
, coff_sym
->N
.ShortName
, 8);
1256 nampnt
= &namebuff
[0];
1260 nampnt
= coff_strtab
+ coff_sym
->N
.Name
.StrTaboff
;
1263 if( nampnt
[0] == '_' )
1269 new_addr
.off
= (int) (deefer
->load_addr
+ coff_sym
->Value
);
1271 if( curr_file
->neps
+ 1 >= curr_file
->neps_alloc
)
1273 curr_file
->neps_alloc
+= 10;
1274 curr_file
->entries
= (struct name_hash
**)
1275 realloc( curr_file
->entries
,
1276 curr_file
->neps_alloc
* sizeof(struct name_hash
*));
1279 fprintf(stderr
,"\tAdding static symbol %s\n", nampnt
);
1281 curr_file
->entries
[curr_file
->neps
++] =
1282 DEBUG_AddSymbol( nampnt
, &new_addr
, this_file
, SYM_WIN32
);
1287 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_EXTERNAL
)
1288 && ISFCN(coff_sym
->Type
)
1289 && (coff_sym
->SectionNumber
> 0) )
1291 if( coff_sym
->N
.Name
.NotLong
)
1293 memcpy(namebuff
, coff_sym
->N
.ShortName
, 8);
1295 nampnt
= &namebuff
[0];
1299 nampnt
= coff_strtab
+ coff_sym
->N
.Name
.StrTaboff
;
1303 if( nampnt
[0] == '_' )
1309 new_addr
.off
= (int) (deefer
->load_addr
+ coff_sym
->Value
);
1312 fprintf(stderr
, "%d: %x %s\n", i
, new_addr
.off
, nampnt
);
1314 fprintf(stderr
,"\tAdding global symbol %s\n", nampnt
);
1318 * Now we need to figure out which file this guy belongs to.
1321 for(j
=0; j
< nfiles
; j
++)
1323 if( coff_files
[j
].startaddr
<= coff_sym
->Value
1324 && coff_files
[j
].endaddr
> coff_sym
->Value
)
1326 this_file
= coff_files
[j
].filename
;
1330 if( coff_files
[j
].neps
+ 1 >= coff_files
[j
].neps_alloc
)
1332 coff_files
[j
].neps_alloc
+= 10;
1333 coff_files
[j
].entries
= (struct name_hash
**)
1334 realloc( coff_files
[j
].entries
,
1335 coff_files
[j
].neps_alloc
* sizeof(struct name_hash
*));
1337 coff_files
[j
].entries
[coff_files
[j
].neps
++] =
1338 DEBUG_AddSymbol( nampnt
, &new_addr
, this_file
, SYM_WIN32
);
1343 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_EXTERNAL
)
1344 && (coff_sym
->SectionNumber
> 0) )
1347 * Similar to above, but for the case of data symbols.
1348 * These aren't treated as entrypoints.
1350 if( coff_sym
->N
.Name
.NotLong
)
1352 memcpy(namebuff
, coff_sym
->N
.ShortName
, 8);
1354 nampnt
= &namebuff
[0];
1358 nampnt
= coff_strtab
+ coff_sym
->N
.Name
.StrTaboff
;
1362 if( nampnt
[0] == '_' )
1368 new_addr
.off
= (int) (deefer
->load_addr
+ coff_sym
->Value
);
1371 fprintf(stderr
, "%d: %x %s\n", i
, new_addr
.off
, nampnt
);
1373 fprintf(stderr
,"\tAdding global data symbol %s\n", nampnt
);
1377 * Now we need to figure out which file this guy belongs to.
1379 DEBUG_AddSymbol( nampnt
, &new_addr
, NULL
, SYM_WIN32
);
1384 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_STATIC
)
1388 * Ignore these. They don't have anything to do with
1396 fprintf(stderr
,"Skipping unknown entry %d %d %d\n", coff_sym
->StorageClass
,
1397 coff_sym
->SectionNumber
, naux
);
1401 * For now, skip past the aux entries.
1408 * OK, we now should have a list of files, and we should have a list
1409 * of entrypoints. We need to sort the entrypoints so that we are
1410 * able to tie the line numbers with the given functions within the
1413 if( coff_files
!= NULL
)
1415 for(j
=0; j
< nfiles
; j
++)
1417 if( coff_files
[j
].entries
!= NULL
)
1419 qsort(coff_files
[j
].entries
, coff_files
[j
].neps
,
1420 sizeof(struct name_hash
*), DEBUG_cmp_sym
);
1425 * Now pick apart the line number tables, and attach the entries
1426 * to the given functions.
1428 for(j
=0; j
< nfiles
; j
++)
1431 if( coff_files
[j
].neps
!= 0 )
1432 for(k
=0; k
< coff_files
[j
].linecnt
; k
++)
1435 * Another monstrosity caused by the fact that we are using
1436 * a 6 byte structure, and gcc wants to pad structures to 4 byte
1437 * boundaries. Otherwise we could just index into an array.
1439 linepnt
= (struct CoffLinenum
*)
1440 ((unsigned int) coff_linetab
+
1441 6*(coff_files
[j
].linetab_offset
+ k
));
1443 * If we have spilled onto the next entrypoint, then
1444 * bump the counter..
1448 if (i
+1 >= coff_files
[j
].neps
) break;
1449 DEBUG_GetSymbolAddr(coff_files
[j
].entries
[i
+1], &new_addr
);
1450 if( (((unsigned int)deefer
->load_addr
+
1451 linepnt
->VirtualAddr
) >= new_addr
.off
) )
1458 * Add the line number. This is always relative to the
1459 * start of the function, so we need to subtract that offset
1462 DEBUG_GetSymbolAddr(coff_files
[j
].entries
[i
], &new_addr
);
1463 DEBUG_AddLineNumber(coff_files
[j
].entries
[i
],
1465 (unsigned int) deefer
->load_addr
1466 + linepnt
->VirtualAddr
1474 if( coff_files
!= NULL
)
1476 for(j
=0; j
< nfiles
; j
++)
1478 if( coff_files
[j
].entries
!= NULL
)
1480 free(coff_files
[j
].entries
);
1491 * Process a codeview line number table. Digestify the thing so that
1492 * we can easily reference the thing when we process the rest of
1495 static struct codeview_linetab_hdr
*
1496 DEBUG_SnarfLinetab(char * linetab
,
1500 char filename
[PATH_MAX
];
1501 unsigned int * filetab
;
1505 struct codeview_linetab_hdr
* lt_hdr
;
1506 unsigned int * lt_ptr
;
1510 union any_size pnt2
;
1511 struct startend
* start
;
1515 * Now get the important bits.
1517 pnt
= (union any_size
) linetab
;
1521 filetab
= (unsigned int *) pnt
.c
;
1524 * Now count up the number of segments in the file.
1527 for(i
=0; i
<nfile
; i
++)
1529 pnt2
= (union any_size
) (linetab
+ filetab
[i
]);
1534 * Next allocate the header we will be returning.
1535 * There is one header for each segment, so that we can reach in
1536 * and pull bits as required.
1538 lt_hdr
= (struct codeview_linetab_hdr
*)
1539 xmalloc((nseg
+ 1) * sizeof(*lt_hdr
));
1540 if( lt_hdr
== NULL
)
1545 memset(lt_hdr
, 0, sizeof(*lt_hdr
) * (nseg
+1));
1548 * Now fill the header we will be returning, one for each segment.
1549 * Note that this will basically just contain pointers into the existing
1550 * line table, and we do not actually copy any additional information
1551 * or allocate any additional memory.
1555 for(i
=0; i
<nfile
; i
++)
1558 * Get the pointer into the segment information.
1560 pnt2
= (union any_size
) (linetab
+ filetab
[i
]);
1561 file_segcount
= *pnt2
.s
;
1564 lt_ptr
= (unsigned int *) pnt2
.c
;
1565 start
= (struct startend
*) (lt_ptr
+ file_segcount
);
1568 * Now snarf the filename for all of the segments for this file.
1570 fn
= (unsigned char *) (start
+ file_segcount
);
1571 memset(filename
, 0, sizeof(filename
));
1572 memcpy(filename
, fn
+ 1, *fn
);
1573 fn
= strdup(filename
);
1575 for(k
= 0; k
< file_segcount
; k
++, this_seg
++)
1577 pnt2
= (union any_size
) (linetab
+ lt_ptr
[k
]);
1578 lt_hdr
[this_seg
].start
= start
[k
].start
;
1579 lt_hdr
[this_seg
].end
= start
[k
].end
;
1580 lt_hdr
[this_seg
].sourcefile
= fn
;
1581 lt_hdr
[this_seg
].segno
= *pnt2
.s
++;
1582 lt_hdr
[this_seg
].nline
= *pnt2
.s
++;
1583 lt_hdr
[this_seg
].offtab
= pnt2
.ui
;
1584 lt_hdr
[this_seg
].linetab
= (unsigned short *)
1585 (pnt2
.ui
+ lt_hdr
[this_seg
].nline
);
1596 DEBUG_SnarfCodeView( struct deferred_debug_info
* deefer
,
1599 struct codeview_linetab_hdr
* linetab
)
1601 struct name_hash
* curr_func
= NULL
;
1602 struct wine_locals
* curr_sym
= NULL
;
1609 IMAGE_SECTION_HEADER
* sectp
;
1610 union codeview_symbol
* sym
;
1611 char symname
[PATH_MAX
];
1612 struct name_hash
* thunk_sym
= NULL
;
1614 ptr
= (union any_size
) cv_data
;
1615 nsect
= deefer
->nsect
;
1616 sectp
= deefer
->sectp
;
1619 * Skip over the first word. Don't really know what it means, but
1625 * Loop over the different types of records and whenever we
1626 * find something we are interested in, record it and move on.
1628 while( ptr
.c
- cv_data
< size
)
1630 sym
= (union codeview_symbol
*) ptr
.c
;
1632 if( sym
->generic
.len
- sizeof(int) == (ptr
.c
- cv_data
) )
1635 * This happens when we have indirect symbols that VC++ 4.2
1636 * sometimes uses when there isn't a line number table.
1637 * We ignore it - we will process and enter all of the
1638 * symbols in the global symbol table anyways, so there
1639 * isn't much point in keeping track of all of this crap.
1644 memset(symname
, 0, sizeof(symname
));
1645 switch(sym
->generic
.id
)
1651 * First, a couple of sanity checks.
1653 if( sym
->data
.namelen
== 0 )
1658 if( sym
->data
.seg
== 0 || sym
->data
.seg
> nsect
)
1664 * Global and local data symbols. We don't associate these
1665 * with any given source file.
1668 memcpy(symname
, sym
->data
.name
, sym
->data
.namelen
);
1670 new_addr
.type
= DEBUG_GetCVType(sym
->data
.symtype
);
1671 new_addr
.off
= (unsigned int) deefer
->load_addr
+
1672 sectp
[sym
->data
.seg
- 1].VirtualAddress
+
1674 DEBUG_AddSymbol( symname
, &new_addr
, NULL
, SYM_WIN32
| SYM_DATA
);
1678 * Sort of like a global function, but it just points
1679 * to a thunk, which is a stupid name for what amounts to
1680 * a PLT slot in the normal jargon that everyone else uses.
1682 memcpy(symname
, sym
->thunk
.name
, sym
->thunk
.namelen
);
1684 new_addr
.type
= NULL
;
1685 new_addr
.off
= (unsigned int) deefer
->load_addr
+
1686 sectp
[sym
->thunk
.segment
- 1].VirtualAddress
+
1688 thunk_sym
= DEBUG_AddSymbol( symname
, &new_addr
, NULL
,
1689 SYM_WIN32
| SYM_FUNC
);
1690 DEBUG_SetSymbolSize(thunk_sym
, sym
->thunk
.thunk_len
);
1695 * Global and static functions.
1697 memcpy(symname
, sym
->proc
.name
, sym
->proc
.namelen
);
1699 new_addr
.type
= DEBUG_GetCVType(sym
->proc
.proctype
);
1700 new_addr
.off
= (unsigned int) deefer
->load_addr
+
1701 sectp
[sym
->proc
.segment
- 1].VirtualAddress
+
1704 * See if we can find a segment that this goes with. If so,
1705 * it means that we also may have line number information
1706 * for this function.
1708 for(i
=0; linetab
[i
].linetab
!= NULL
; i
++)
1710 if( ((unsigned int) deefer
->load_addr
1711 + sectp
[linetab
[i
].segno
- 1].VirtualAddress
1712 + linetab
[i
].start
<= new_addr
.off
)
1713 && ((unsigned int) deefer
->load_addr
1714 + sectp
[linetab
[i
].segno
- 1].VirtualAddress
1715 + linetab
[i
].end
> new_addr
.off
) )
1721 DEBUG_Normalize(curr_func
);
1722 if( linetab
[i
].linetab
== NULL
)
1724 curr_func
= DEBUG_AddSymbol( symname
, &new_addr
, NULL
,
1725 SYM_WIN32
| SYM_FUNC
);
1730 * First, create the entry. Then dig through the linetab
1731 * and add whatever line numbers are appropriate for this
1734 curr_func
= DEBUG_AddSymbol( symname
, &new_addr
,
1735 linetab
[i
].sourcefile
,
1736 SYM_WIN32
| SYM_FUNC
);
1737 for(j
=0; j
< linetab
[i
].nline
; j
++)
1739 if( linetab
[i
].offtab
[j
] >= sym
->proc
.offset
1740 && linetab
[i
].offtab
[j
] < sym
->proc
.offset
1741 + sym
->proc
.proc_len
)
1743 DEBUG_AddLineNumber(curr_func
, linetab
[i
].linetab
[j
],
1744 linetab
[i
].offtab
[j
] - sym
->proc
.offset
);
1751 * Add information about where we should set breakpoints
1754 DEBUG_SetSymbolBPOff(curr_func
, sym
->proc
.debug_start
);
1755 DEBUG_SetSymbolSize(curr_func
, sym
->proc
.proc_len
);
1759 * Function parameters and stack variables.
1761 memcpy(symname
, sym
->stack
.name
, sym
->stack
.namelen
);
1762 curr_sym
= DEBUG_AddLocal(curr_func
,
1768 DEBUG_SetLocalSymbolType(curr_sym
, DEBUG_GetCVType(sym
->stack
.symtype
));
1776 * Adjust pointer to point to next entry, rounding up to a word
1777 * boundary. MS preserving alignment? Stranger things have
1780 if( sym
->generic
.id
== S_PROCREF
1781 || sym
->generic
.id
== S_DATAREF
1782 || sym
->generic
.id
== S_UNKNOWN
)
1784 len
= (sym
->generic
.len
+ 3) & ~3;
1785 len
+= ptr
.c
[16] + 1;
1786 ptr
.c
+= (len
+ 3) & ~3;
1790 ptr
.c
+= (sym
->generic
.len
+ 3) & ~3;
1794 if( linetab
!= NULL
)
1804 * Process PDB file which contains debug information.
1806 * These are really weird beasts. They are intended to be incrementally
1807 * updated by the incremental linker, and this means that you need to
1808 * be able to remove and add information. Thus the PDB file is sort of
1809 * like a block structured device, with a freelist and lists of extent numbers
1810 * that are used to get the relevant pieces. In all cases seen so far, the
1811 * blocksize is always 0x400 bytes. The header has a field which apparently
1812 * holds the blocksize, so if it ever changes we are safe.
1814 * In general, every time we need to extract something from the pdb file,
1815 * it is easier to copy it into another buffer so we have the information
1816 * in one contiguous block rather than attempt to try and keep track of when
1817 * we need to grab another extent from the pdb file.
1819 * The thing that is a real pain about some MS stuff is that they choose
1820 * data structures which are not representable in C. Thus we have to
1821 * hack around and diddle pointers.
1825 DEBUG_ProcessPDBFile(struct deferred_debug_info
* deefer
, char * full_filename
)
1827 char * addr
= (char *) 0xffffffff;
1828 unsigned int blocksize
;
1829 unsigned int bufflen
= 0;
1830 char * buffer
= NULL
;
1831 unsigned short * extent_table
;
1833 struct file_ent
* fent
;
1834 char filename
[MAX_PATHNAME_LEN
];
1835 struct file_list
* filelist
= NULL
;
1836 unsigned int gsym_record
= 0;
1837 char * gsymtab
= NULL
;
1838 struct filetab_hdr
* hd
;
1841 unsigned int last_extent
;
1842 struct codeview_linetab_hdr
* linetab
;
1843 unsigned int nblocks
;
1845 unsigned int offset
;
1846 struct codeview_pdb_hdr
* pdbhdr
;
1848 struct stat statbuf
;
1850 unsigned short * table
;
1852 unsigned int toc_blocks
;
1854 LocateDebugInfoFile(full_filename
, filename
);
1855 status
= stat(filename
, &statbuf
);
1858 fprintf(stderr
, "-Unable to open .PDB file %s\n", filename
);
1863 * Now open the file, so that we can mmap() it.
1865 fd
= open(filename
, O_RDONLY
);
1868 fprintf(stderr
, "-Unable to open .DBG file %s\n", filename
);
1874 * Now mmap() the file.
1876 addr
= mmap(0, statbuf
.st_size
, PROT_READ
,
1877 MAP_PRIVATE
, fd
, 0);
1878 if( addr
== (char *) 0xffffffff )
1880 fprintf(stderr
, "-Unable to mmap .DBG file %s\n", filename
);
1885 * Now that we have the formalities over and done with, we need
1886 * to find the table of contents for the PDB file.
1888 pdbhdr
= (struct codeview_pdb_hdr
*) addr
;
1889 blocksize
= pdbhdr
->blocksize
;
1890 last_extent
= (statbuf
.st_size
+ blocksize
- 1) / blocksize
;
1893 * The TOC itself isn't always contiguous, so we need to extract a few
1894 * extents from the file to form the TOC.
1896 toc_blocks
= (pdbhdr
->toc_len
+ blocksize
- 1) / blocksize
;
1897 toc
= (char *) xmalloc(toc_blocks
* blocksize
);
1898 table
= pdbhdr
->toc_ext
;
1899 for(i
=0; i
< toc_blocks
; i
++)
1901 memcpy(toc
+ blocksize
*i
, addr
+ table
[i
]*blocksize
, blocksize
);
1905 * Next build our own table which will have the size and extent block
1906 * list for each record in the PDB file.
1908 * The TOC starts out with the number of files. Then it is followed by
1909 * (npair * 2*sizeof(int)) bytes of information, which are pairs of ints.
1910 * The first one is the size of the record (in bytes), and the second one
1911 * is something else which I haven't figured out yet.
1913 pnt
= (unsigned int *) toc
;
1915 extent_table
= (unsigned short *) ((unsigned int) toc
+
1916 npair
* 2 * sizeof(int) + sizeof(int));
1921 if( sizeof(int) + 2*sizeof(int)*npair
> pdbhdr
->toc_len
)
1926 filelist
= (struct file_list
*) xmalloc(npair
* sizeof(*filelist
));
1927 if( filelist
== NULL
)
1931 memset(filelist
, 0, npair
* sizeof(*filelist
));
1934 for(i
=0; i
< npair
; i
++)
1936 filelist
[i
].record_len
= pnt
[i
*2];
1937 filelist
[i
].nextents
= (filelist
[i
].record_len
+ blocksize
- 1)
1939 filelist
[i
].extent_list
= extent_table
+ nblocks
;
1940 nblocks
+= filelist
[i
].nextents
;
1943 * These get filled in later when we parse one of the records.
1945 filelist
[i
].linetab_offset
= 0;
1946 filelist
[i
].linetab_len
= 0;
1950 * OK, now walk through the various records and pick out the bits we
1951 * really want to see. Some of the records are extra special, and
1952 * we need to handle these a little bit differently.
1954 for(i
=0; i
< npair
; i
++)
1956 if( filelist
[i
].record_len
== 0xffffffff )
1962 * Make sure our buffer is large enough to hold the record.
1964 if( bufflen
< filelist
[i
].nextents
* blocksize
)
1966 bufflen
= filelist
[i
].nextents
* blocksize
;
1967 buffer
= (char *) realloc(buffer
, bufflen
);
1971 * Do this just for completeness. It makes debugging easier
1972 * if we have a clean indication of where the record ends.
1974 memset(buffer
, 0, filelist
[i
].nextents
* blocksize
);
1977 * Next, build the record using the extent list.
1979 for(j
=0; j
< filelist
[i
].nextents
; j
++)
1981 memcpy(buffer
+ j
* blocksize
,
1982 addr
+ filelist
[i
].extent_list
[j
] * blocksize
,
1986 pnt
= (unsigned int *) buffer
;
1989 * OK, now figure out what to do with it.
1993 * Always ignore the first entry. It seems to contain a backup copy
1994 * of the TOC (the last time the file was modified??)
2002 * The second entry as a id block. It contains a magic number
2003 * to identify the compiler, plus it also contains the timestamp
2004 * which must match the timestamp in the executable.
2009 if( ((*pnt
!= 19950623) && (*pnt
!= 19950814))
2010 || (filelist
[i
].record_len
!= 0x24)
2011 || (pnt
[1] != ((struct CodeViewDebug
*)(deefer
->dbg_info
))->cv_timestamp
) )
2018 * The third entry contains pointers to the global symbol table,
2019 * plus it also contains additional information about each record
2024 hd
= (struct filetab_hdr
*) buffer
;
2026 gsym_record
= hd
->gsym_file
;
2027 gsymtab
= (char *) xmalloc( filelist
[gsym_record
].nextents
2029 memset(gsymtab
, 0, filelist
[gsym_record
].nextents
* blocksize
);
2031 for(j
=0; j
< filelist
[gsym_record
].nextents
; j
++)
2033 memcpy(gsymtab
+ j
* blocksize
,
2034 addr
+ filelist
[gsym_record
].extent_list
[j
] * blocksize
,
2039 * This record also contains information about where in the
2040 * remaining records we will be able to find the start of the
2041 * line number table. We could locate that bit using heuristics,
2042 * but since we have the info handy, we might as well use it.
2044 offset
= sizeof(*hd
);
2047 fent
= (struct file_ent
*) (buffer
+ offset
);
2048 if( offset
> hd
->ftab_len
)
2053 if( fent
->file_number
== 0 || fent
->file_number
>= npair
)
2058 filelist
[fent
->file_number
].linetab_offset
=
2059 fent
->linetab_offset
;
2060 filelist
[fent
->file_number
].linetab_len
=
2063 * Figure out the offset of the next entry.
2064 * There is a fixed part of the record and a variable
2065 * length filename which we must also skip past.
2067 offset
+= ((unsigned int) &fent
->filename
- (unsigned int) fent
)
2068 + strlen(fent
->filename
) + 1;
2069 offset
+= strlen(buffer
+offset
) + 1;
2070 offset
= (offset
+ 3) & ~3;
2076 * Two different magic numbers used as dates.
2077 * These indicate the 'type' table.
2079 if( *pnt
== 19950410
2080 || *pnt
== 19951122 )
2082 DEBUG_ParseTypeTable(buffer
, filelist
[i
].record_len
);
2087 * This is something we really want to look at, since it contains
2088 * real debug info. Anything that doesn't match this can be
2094 * First, snag the line table, if we have one. This always
2095 * occurs at the end of the record, so we take the linetab
2096 * offset as the end of the normal part of the record.
2099 if( filelist
[i
].linetab_len
!= 0 )
2101 linetab
= DEBUG_SnarfLinetab(buffer
+ filelist
[i
].linetab_offset
,
2102 filelist
[i
].linetab_len
);
2103 DEBUG_SnarfCodeView(deefer
, buffer
,
2104 filelist
[i
].linetab_offset
,
2109 DEBUG_SnarfCodeView(deefer
, buffer
,
2110 filelist
[i
].record_len
,
2118 * Finally, process the global symbol table itself. There isn't
2119 * a line number component to this, so we just toss everything
2120 * into the mix and it all should work out.
2122 if( gsym_record
!= 0 )
2124 DEBUG_SnarfCodeView(deefer
, gsymtab
- sizeof(int),
2125 filelist
[gsym_record
].record_len
,
2131 if( gsymtab
!= NULL
)
2137 if( buffer
!= NULL
)
2142 if( filelist
!= NULL
)
2147 if( addr
!= (char *) 0xffffffff )
2149 munmap(addr
, statbuf
.st_size
);
2161 * Process DBG file which contains debug information.
2165 DEBUG_ProcessDBGFile(struct deferred_debug_info
* deefer
, char * filename
)
2167 char * addr
= (char *) 0xffffffff;
2169 struct CV4_DirHead
* codeview_dir
;
2170 struct CV4_DirEnt
* codeview_dent
;
2171 PIMAGE_DEBUG_DIRECTORY dbghdr
;
2172 struct deferred_debug_info deefer2
;
2176 struct codeview_linetab_hdr
* linetab
;
2178 PIMAGE_SEPARATE_DEBUG_HEADER pdbg
= NULL
;
2179 IMAGE_SECTION_HEADER
* sectp
;
2180 struct stat statbuf
;
2182 char dbg_file
[MAX_PATHNAME_LEN
];
2184 LocateDebugInfoFile(filename
, dbg_file
);
2185 status
= stat(dbg_file
, &statbuf
);
2188 fprintf(stderr
, "-Unable to open .DBG file %s\n", dbg_file
);
2193 * Now open the file, so that we can mmap() it.
2195 fd
= open(dbg_file
, O_RDONLY
);
2198 fprintf(stderr
, "Unable to open .DBG file %s\n", dbg_file
);
2204 * Now mmap() the file.
2206 addr
= mmap(0, statbuf
.st_size
, PROT_READ
,
2207 MAP_PRIVATE
, fd
, 0);
2208 if( addr
== (char *) 0xffffffff )
2210 fprintf(stderr
, "Unable to mmap .DBG file %s\n", dbg_file
);
2214 pdbg
= (PIMAGE_SEPARATE_DEBUG_HEADER
) addr
;
2216 if( pdbg
->TimeDateStamp
!= deefer
->dbgdir
->TimeDateStamp
)
2218 fprintf(stderr
, "Warning - %s has incorrect internal timestamp\n",
2222 Well, sometimes this happens to DBG files which ARE REALLY the right .DBG
2223 files but nonetheless this check fails. Anyway, WINDBG (debugger for
2224 Windows by Microsoft) loads debug symbols which have incorrect timestamps.
2228 fprintf(stderr
, "Processing symbols from %s...\n", dbg_file
);
2230 dbghdr
= (PIMAGE_DEBUG_DIRECTORY
) ( addr
+ sizeof(*pdbg
)
2231 + pdbg
->NumberOfSections
* sizeof(IMAGE_SECTION_HEADER
)
2232 + pdbg
->ExportedNamesSize
);
2234 sectp
= (PIMAGE_SECTION_HEADER
) ((char *) pdbg
+ sizeof(*pdbg
));
2235 nsect
= pdbg
->NumberOfSections
;
2237 for( i
=0; i
< pdbg
->DebugDirectorySize
/ sizeof(*pdbg
); i
++, dbghdr
++ )
2239 switch(dbghdr
->Type
)
2241 case IMAGE_DEBUG_TYPE_COFF
:
2243 * Dummy up a deferred debug header to handle the
2244 * COFF stuff embedded within the DBG file.
2246 memset((char *) &deefer2
, 0, sizeof(deefer2
));
2247 deefer2
.dbg_info
= (addr
+ dbghdr
->PointerToRawData
);
2248 deefer2
.dbg_size
= dbghdr
->SizeOfData
;
2249 deefer2
.load_addr
= deefer
->load_addr
;
2251 DEBUG_ProcessCoff(&deefer2
);
2253 case IMAGE_DEBUG_TYPE_CODEVIEW
:
2255 * This is the older format by which codeview stuff is
2256 * stored, known as the 'NB09' format. Newer executables
2257 * and dlls created by VC++ use PDB files instead, which
2258 * have lots of internal similarities, but the overall
2259 * format and structure is quite different.
2261 codeview
= (addr
+ dbghdr
->PointerToRawData
);
2264 * The first thing in the codeview section should be
2265 * an 'NB09' identifier. As a sanity check, make sure
2268 if( *((unsigned int*) codeview
) != 0x3930424e )
2274 * Next we need to find the directory. This is easy too.
2276 codeview_dir
= (struct CV4_DirHead
*)
2277 (codeview
+ ((unsigned int*) codeview
)[1]);
2280 * Some more sanity checks. Make sure that everything
2281 * is as we expect it.
2283 if( codeview_dir
->next_offset
!= 0
2284 || codeview_dir
->dhsize
!= sizeof(*codeview_dir
)
2285 || codeview_dir
->desize
!= sizeof(*codeview_dent
) )
2289 codeview_dent
= (struct CV4_DirEnt
*) (codeview_dir
+ 1);
2291 for(j
=0; j
< codeview_dir
->ndir
; j
++, codeview_dent
++)
2293 if( codeview_dent
->subsect_number
== sstAlignSym
)
2296 * Check the previous entry. If it is a
2297 * sstSrcModule, it contains the line number
2298 * info for this file.
2301 if( codeview_dent
[1].module_number
== codeview_dent
[0].module_number
2302 && codeview_dent
[1].subsect_number
== sstSrcModule
)
2304 linetab
= DEBUG_SnarfLinetab(
2305 codeview
+ codeview_dent
[1].offset
,
2306 codeview_dent
[1].size
);
2309 if( codeview_dent
[-1].module_number
== codeview_dent
[0].module_number
2310 && codeview_dent
[-1].subsect_number
== sstSrcModule
)
2312 linetab
= DEBUG_SnarfLinetab(
2313 codeview
+ codeview_dent
[-1].offset
,
2314 codeview_dent
[-1].size
);
2317 * Now process the CV stuff.
2319 DEBUG_SnarfCodeView(deefer
,
2320 codeview
+ codeview_dent
->offset
,
2321 codeview_dent
->size
,
2333 if( addr
!= (char *) 0xffffffff )
2335 munmap(addr
, statbuf
.st_size
);
2347 DEBUG_ProcessDeferredDebug()
2349 struct deferred_debug_info
* deefer
;
2350 struct CodeViewDebug
* cvd
;
2351 struct MiscDebug
* misc
;
2356 DEBUG_InitCVDataTypes();
2358 for(deefer
= dbglist
; deefer
; deefer
= deefer
->next
)
2360 if( deefer
->loaded
)
2365 if( last_proc
!= deefer
->dbg_index
)
2369 fprintf(stderr
, "DeferredDebug for:");
2372 fprintf(stderr
, " %s",deefer
->module_name
);
2373 last_proc
= deefer
->dbg_index
;
2376 switch(deefer
->dbgdir
->Type
)
2378 case IMAGE_DEBUG_TYPE_COFF
:
2380 * Standard COFF debug information that VC++ adds when you
2381 * use /debugtype:both with the linker.
2384 fprintf(stderr
, "Processing COFF symbols...\n");
2386 DEBUG_ProcessCoff(deefer
);
2388 case IMAGE_DEBUG_TYPE_CODEVIEW
:
2390 * This is a pointer to a PDB file of some sort.
2392 cvd
= (struct CodeViewDebug
*) deefer
->dbg_info
;
2394 if( strcmp(cvd
->cv_nbtype
, "NB10") != 0 )
2397 * Whatever this is, we don't know how to deal with
2402 DEBUG_ProcessPDBFile(deefer
, cvd
->cv_name
);
2404 fprintf(stderr
, "Processing PDB file %s\n", cvd
->cv_name
);
2407 case IMAGE_DEBUG_TYPE_MISC
:
2409 * A pointer to a .DBG file of some sort. These files
2410 * can contain either CV4 or COFF information. Open
2411 * the file, and try to do the right thing with it.
2413 misc
= (struct MiscDebug
*) deefer
->dbg_info
;
2415 filename
= strrchr((char *) &misc
->Data
, '.');
2418 * Ignore the file if it doesn't have a .DBG extension.
2420 if( (filename
== NULL
)
2421 || ( (strcmp(filename
, ".dbg") != 0)
2422 && (strcmp(filename
, ".DBG") != 0)) )
2427 filename
= (char *) &misc
->Data
;
2430 * Do the dirty deed...
2432 DEBUG_ProcessDBGFile(deefer
, filename
);
2437 * We should never get here...
2443 fprintf(stderr
, "\n");
2448 /***********************************************************************
2451 * Display shared libarary information.
2453 void DEBUG_InfoShare(void)
2455 struct deferred_debug_info
* deefer
;
2457 fprintf(stderr
,"Address\t\tModule\tName\n");
2459 for(deefer
= dbglist
; deefer
; deefer
= deefer
->next
)
2461 fprintf(stderr
,"0x%8.8x\t(%s)\t%s\n", (unsigned int) deefer
->load_addr
,
2462 deefer
->module
? "Win32" : "ELF", deefer
->module_name
);