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.
21 #include <sys/types.h>
22 #ifdef HAVE_SYS_MMAN_H
31 #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
= DBG_alloc(MAX_PATHNAME_LEN
*10);
44 char *str2
= DBG_alloc(MAX_PATHNAME_LEN
);
47 DOS_FULL_NAME fullname
;
49 file
= strrchr(filename
, '\\');
50 if( file
== NULL
) file
= filename
; else file
++;
52 if (GetEnvironmentVariableA("_NT_SYMBOL_PATH", str1
, MAX_PATHNAME_LEN
))
53 if (SearchPathA(str1
, file
, NULL
, MAX_PATHNAME_LEN
*10, str2
, &name_part
))
55 if (GetEnvironmentVariableA("_NT_ALT_SYMBOL_PATH", str1
, MAX_PATHNAME_LEN
))
56 if (SearchPathA(str1
, file
, NULL
, MAX_PATHNAME_LEN
*10, str2
, &name_part
))
58 if (SearchPathA(NULL
, file
, NULL
, MAX_PATHNAME_LEN
*10, str2
, &name_part
))
63 memcpy(dbg_filename
, filename
, MAX_PATHNAME_LEN
);
69 if (DOSFS_GetFullName(str2
, TRUE
, &fullname
))
70 memcpy(dbg_filename
, fullname
.long_name
, MAX_PATHNAME_LEN
);
78 * This is an index we use to keep track of the debug information
79 * when we have multiple sources. We use the same database to also
80 * allow us to do an 'info shared' type of deal, and we use the index
81 * to eliminate duplicates.
83 static int DEBUG_next_index
= 0;
94 * This is a convenience structure used to map portions of the
104 * This is how we reference the various record types.
106 union codeview_symbol
120 unsigned short symtype
;
121 unsigned char namelen
;
122 unsigned char name
[1];
129 unsigned int pparent
;
133 unsigned short segment
;
134 unsigned short thunk_len
;
135 unsigned char thtype
;
136 unsigned char namelen
;
137 unsigned char name
[1];
143 unsigned int pparent
;
146 unsigned int proc_len
;
147 unsigned int debug_start
;
148 unsigned int debug_end
;
150 unsigned short segment
;
151 unsigned short proctype
;
153 unsigned char namelen
;
154 unsigned char name
[1];
158 short int len
; /* Total length of this entry */
159 short int id
; /* Always S_BPREL32 */
160 unsigned int offset
; /* Stack offset relative to BP */
161 unsigned short symtype
;
162 unsigned char namelen
;
163 unsigned char name
[1];
181 unsigned char variant
[1];
189 unsigned char bitoff
;
199 unsigned char arrlen
;
200 unsigned char namelen
;
201 unsigned char name
[1];
213 unsigned short structlen
;
214 unsigned char namelen
;
215 unsigned char name
[1];
225 unsigned short un_len
;
226 unsigned char namelen
;
227 unsigned char name
[1];
238 unsigned char namelen
;
239 unsigned char name
[1];
246 unsigned short int value
;
247 unsigned char namelen
;
248 unsigned char name
[1];
256 unsigned short int offset
;
257 unsigned char namelen
;
258 unsigned char name
[1];
269 unsigned char namelen
;
270 unsigned char name
[1];
275 #define S_BPREL 0x200
276 #define S_LDATA 0x201
277 #define S_GDATA 0x202
279 #define S_LPROC 0x204
280 #define S_GPROC 0x205
281 #define S_THUNK 0x206
282 #define S_BLOCK 0x207
284 #define S_LABEL 0x209
286 #define S_PROCREF 0x400
287 #define S_DATAREF 0x401
288 #define S_ALIGN 0x402
289 #define S_UNKNOWN 0x403
292 * This covers the basic datatypes that VC++ seems to be using these days.
293 * 32 bit mode only. There are additional numbers for the pointers in 16
294 * bit mode. There are many other types listed in the documents, but these
295 * are apparently not used by the compiler, or represent pointer types
298 #define T_NOTYPE 0x0000 /* Notype */
299 #define T_ABS 0x0001 /* Abs */
300 #define T_VOID 0x0003 /* Void */
301 #define T_CHAR 0x0010 /* signed char */
302 #define T_SHORT 0x0011 /* short */
303 #define T_LONG 0x0012 /* long */
304 #define T_QUAD 0x0013 /* long long */
305 #define T_UCHAR 0x0020 /* unsigned char */
306 #define T_USHORT 0x0021 /* unsigned short */
307 #define T_ULONG 0x0022 /* unsigned long */
308 #define T_UQUAD 0x0023 /* unsigned long long */
309 #define T_REAL32 0x0040 /* float */
310 #define T_REAL64 0x0041 /* double */
311 #define T_RCHAR 0x0070 /* real char */
312 #define T_WCHAR 0x0071 /* wide char */
313 #define T_INT4 0x0074 /* int */
314 #define T_UINT4 0x0075 /* unsigned int */
316 #define T_32PVOID 0x0403 /* 32 bit near pointer to void */
317 #define T_32PCHAR 0x0410 /* 16:32 near pointer to signed char */
318 #define T_32PSHORT 0x0411 /* 16:32 near pointer to short */
319 #define T_32PLONG 0x0412 /* 16:32 near pointer to int */
320 #define T_32PQUAD 0x0413 /* 16:32 near pointer to long long */
321 #define T_32PUCHAR 0x0420 /* 16:32 near pointer to unsigned char */
322 #define T_32PUSHORT 0x0421 /* 16:32 near pointer to unsigned short */
323 #define T_32PULONG 0x0422 /* 16:32 near pointer to unsigned int */
324 #define T_32PUQUAD 0x0423 /* 16:32 near pointer to long long */
325 #define T_32PREAL32 0x0440 /* 16:32 near pointer to float */
326 #define T_32PREAL64 0x0441 /* 16:32 near pointer to float */
327 #define T_32PRCHAR 0x0470 /* 16:32 near pointer to real char */
328 #define T_32PWCHAR 0x0471 /* 16:32 near pointer to real char */
329 #define T_32PINT4 0x0474 /* 16:32 near pointer to int */
330 #define T_32PUINT4 0x0475 /* 16:32 near pointer to unsigned int */
332 #define LF_MODIFIER 0x1
333 #define LF_POINTER 0x2
336 #define LF_STRUCTURE 0x5
338 #define LF_ENUMERATION 0x7
339 #define LF_PROCEDURE 0x8
340 #define LF_MFUNCTION 0x9
341 #define LF_VTSHAPE 0xa
342 #define LF_BARRAY 0xd
343 #define LF_DIMARRAY 0x11
344 #define LF_VFTPATH 0x12
346 #define LF_SKIP 0x200
347 #define LF_ARGLIST 0x201
348 #define LF_FIELDLIST 0x204
349 #define LF_DERIVED 0x205
350 #define LF_BITFIELD 0x206
352 #define LF_BCLASS 0x400
353 #define LF_VBCLASS 0x401
354 #define LF_IVBCLASS 0x402
355 #define LF_ENUMERATE 0x403
356 #define LF_FRIENDFCN 0x404
357 #define LF_INDEX 0x405
358 #define LF_MEMBER 0x406
359 #define LF_STMEMBER 0x407
360 #define LF_METHOD 0x408
361 #define LF_NESTEDTYPE 0x409
362 #define LF_VFUNCTAB 0x40a
363 #define LF_FRIENDCLS 0x40b
364 #define LF_ONEMETHOD 0x40c
365 #define LF_FUNCOFF 0x40d
367 #define MAX_BUILTIN_TYPES 0x480
368 static struct datatype
* cv_basic_types
[MAX_BUILTIN_TYPES
];
369 static int num_cv_defined_types
= 0;
370 static struct datatype
**cv_defined_types
= NULL
;
373 * For the type CODEVIEW debug directory entries, the debug directory
374 * points to a structure like this. The cv_name field is the name
375 * of an external .PDB file.
380 unsigned int cv_timestamp
;
386 unsigned int DataType
;
394 * This is the header that the COFF variety of debug header points to.
398 unsigned int SymbolOffset
;
399 unsigned int N_Linenum
;
400 unsigned int LinenumberOffset
;
401 unsigned int Unused
[4];
405 unsigned int VirtualAddr
;
406 unsigned short int Linenum
;
410 unsigned int startaddr
;
411 unsigned int endaddr
;
415 struct name_hash
**entries
;
425 unsigned int NotLong
;
426 unsigned int StrTaboff
;
433 unsigned char NumberOfAuxSymbols
;
436 struct CoffAuxSection
{
438 unsigned short NumberOfRelocations
;
439 unsigned short NumberOfLinenumbers
;
440 unsigned int CheckSum
;
446 * These two structures are used in the directory within a .DBG file
447 * to locate the individual important bits that we might want to see.
450 short unsigned int dhsize
;
451 short unsigned int desize
;
453 unsigned int next_offset
;
458 short unsigned int subsect_number
;
459 short unsigned int module_number
;
465 * These are the values of interest that the subsect_number field takes.
467 #define sstAlignSym 0x125
468 #define sstSrcModule 0x127
470 struct codeview_linetab_hdr
477 unsigned short * linetab
;
478 unsigned int * offtab
;
481 struct codeview_pdb_hdr
484 unsigned int blocksize
; /* Extent size */
485 unsigned short loc_freelist
; /* freelist. */
486 unsigned short alloc_filesize
; /* # extents allocated. */
487 unsigned int toc_len
;
488 unsigned int unknown
;
489 unsigned short toc_ext
[1]; /* array of extent #'s for toc. */
493 * This is our own structure that we use to keep track of the contents
500 short int * extent_list
;
501 unsigned int linetab_offset
;
502 unsigned int linetab_len
;
506 * These are the structures that represent how the file table is set up
507 * within the PDB file.
511 unsigned short tab1_file
;
512 unsigned short tab2_file
;
513 unsigned short gsym_file
;
514 unsigned short padding
;
515 unsigned int ftab_len
;
516 unsigned int fofftab_len
;
517 unsigned int hash_len
;
518 unsigned int strtab_len
;
523 unsigned int reserved1
;
524 unsigned short datasect_segment
;
525 unsigned short reserved2
;
526 unsigned int datasect_offset
;
527 unsigned int datasect_size
;
528 unsigned int datasect_flags
;
529 unsigned short reserved3
;
530 unsigned short index
;
531 unsigned short num6a
;
532 unsigned short file_number
;
533 unsigned int linetab_offset
;
534 unsigned int linetab_len
;
538 unsigned char filename
[1];
542 ********************************************************************
544 struct deferred_debug_info
546 struct deferred_debug_info
* next
;
552 PIMAGE_DEBUG_DIRECTORY dbgdir
;
553 PIMAGE_SECTION_HEADER sectp
;
559 #define DF_STATUS_NEW 0
560 #define DF_STATUS_LOADED 1
561 #define DF_STATUS_ERROR 2
563 struct deferred_debug_info
* dbglist
= NULL
;
566 * A simple macro that tells us whether a given COFF symbol is a
569 #define N_TMASK 0x0030
570 #define IMAGE_SYM_DTYPE_FUNCTION 2
572 #define ISFCN(x) (((x) & N_TMASK) == (IMAGE_SYM_DTYPE_FUNCTION << N_BTSHFT))
576 * This is what we are looking for in the COFF symbols.
578 #define IMAGE_SYM_CLASS_EXTERNAL 0x2
579 #define IMAGE_SYM_CLASS_STATIC 0x3
580 #define IMAGE_SYM_CLASS_FILE 0x67
583 struct datatype
* DEBUG_GetCVType(int typeno
)
585 struct datatype
* dt
= NULL
;
588 * Convert Codeview type numbers into something we can grok internally.
589 * Numbers < 0x1000 are all fixed builtin types. Numbers from 0x1000 and
590 * up are all user defined (structs, etc).
592 if( typeno
< 0x1000 )
594 if( typeno
< MAX_BUILTIN_TYPES
)
596 dt
= cv_basic_types
[typeno
];
601 if( typeno
- 0x1000 < num_cv_defined_types
)
603 dt
= cv_defined_types
[typeno
- 0x1000];
611 DEBUG_ParseTypeTable(char * table
, int len
)
615 enum debug_type fieldtype
;
619 struct datatype
* subtype
;
621 union codeview_type
* type
;
622 union codeview_type
* type2
;
623 struct datatype
* typeptr
;
627 ptr
.c
= (table
+ 16);
628 while( ptr
.c
- table
< len
)
630 type
= (union codeview_type
*) ptr
.c
;
632 if( curr_type
- 0x1000 >= num_cv_defined_types
)
634 num_cv_defined_types
+= 0x100;
635 cv_defined_types
= (struct datatype
**) DBG_realloc(cv_defined_types
,
636 num_cv_defined_types
* sizeof(struct datatype
*));
637 memset(cv_defined_types
+ num_cv_defined_types
- 0x100,
639 0x100 * sizeof(struct datatype
*));
640 if( cv_defined_types
== NULL
)
646 switch(type
->generic
.id
)
649 cv_defined_types
[curr_type
- 0x1000] =
650 DEBUG_FindOrMakePointerType(DEBUG_GetCVType(type
->pointer
.datatype
));
653 if( type
->array
.arrlen
>= 0x8000 )
656 * This is a numeric leaf, I am too lazy to handle this right
659 fprintf(stderr
, "Ignoring large numberic leaf.\n");
662 if( type
->array
.namelen
!= 0 )
664 memset(symname
, 0, sizeof(symname
));
665 memcpy(symname
, type
->array
.name
, type
->array
.namelen
);
666 typeptr
= DEBUG_NewDataType(DT_ARRAY
, symname
);
670 typeptr
= DEBUG_NewDataType(DT_ARRAY
, NULL
);
672 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
674 subtype
= DEBUG_GetCVType(type
->array
.elemtype
);
675 if( (subtype
== NULL
)
676 || (elem_size
= DEBUG_GetObjectSize(subtype
)) == 0 )
682 arr_max
= type
->array
.arrlen
/ DEBUG_GetObjectSize(subtype
);
685 DEBUG_SetArrayParams(typeptr
, 0, arr_max
, subtype
);
689 * This is where the basic list of fields is defined for
690 * structures and classes.
692 * First, we need to look ahead and see whether we are building
693 * a fieldlist for an enum or a struct.
696 type2
= (union codeview_type
*) ptr2
.c
;
697 if( type2
->member
.id
== LF_MEMBER
)
699 typeptr
= DEBUG_NewDataType(DT_STRUCT
, NULL
);
700 fieldtype
= DT_STRUCT
;
702 else if( type2
->member
.id
== LF_ENUMERATE
)
704 typeptr
= DEBUG_NewDataType(DT_ENUM
, NULL
);
712 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
713 while( ptr2
.c
< (ptr
.c
+ ((type
->generic
.len
+ 3) & ~3)) )
715 type2
= (union codeview_type
*) ptr2
.c
;
716 if( type2
->member
.id
== LF_MEMBER
&& fieldtype
== DT_STRUCT
)
718 memset(symname
, 0, sizeof(symname
));
719 memcpy(symname
, type2
->member
.name
, type2
->member
.namelen
);
721 subtype
= DEBUG_GetCVType(type2
->member
.type
);
723 if( subtype
!= NULL
)
725 elem_size
= DEBUG_GetObjectSize(subtype
);
728 if( type2
->member
.offset
>= 0x8000 )
731 * This is a numeric leaf, I am too lazy to handle this right
734 fprintf(stderr
, "Ignoring large numberic leaf.\n");
738 DEBUG_AddStructElement(typeptr
, symname
, subtype
,
739 type2
->member
.offset
<< 3,
743 else if( type2
->member
.id
== LF_ENUMERATE
&& fieldtype
== DT_ENUM
)
745 memset(symname
, 0, sizeof(symname
));
746 memcpy(symname
, type2
->enumerate
.name
, type2
->enumerate
.namelen
);
748 if( type2
->enumerate
.value
>= 0x8000 )
751 * This is a numeric leaf, I am too lazy to handle this right
754 fprintf(stderr
, "Ignoring large numberic leaf.\n");
758 DEBUG_AddStructElement(typeptr
, symname
, NULL
,
759 type2
->enumerate
.value
, 0);
765 * Something else I have never seen before. Either wrong type of
766 * object in the fieldlist, or some other problem which I wouldn't
767 * really know how to handle until it came up.
769 fprintf(stderr
, "Unexpected entry in fieldlist\n");
774 ptr2
.c
+= ((type2
->member
.namelen
+ 9 + 3) & ~3);
779 if( type
->structure
.structlen
>= 0x8000 )
782 * This is a numeric leaf, I am too lazy to handle this right
785 fprintf(stderr
, "Ignoring large numberic leaf.\n");
788 memset(symname
, 0, sizeof(symname
));
789 memcpy(symname
, type
->structure
.name
, type
->structure
.namelen
);
790 if( strcmp(symname
, "__unnamed") == 0 )
792 typeptr
= DEBUG_NewDataType(DT_STRUCT
, NULL
);
796 typeptr
= DEBUG_NewDataType(DT_STRUCT
, symname
);
798 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
801 * Now copy the relevant bits from the fieldlist that we specified.
803 subtype
= DEBUG_GetCVType(type
->structure
.fieldlist
);
805 if( subtype
!= NULL
)
807 DEBUG_SetStructSize(typeptr
, type
->structure
.structlen
);
808 DEBUG_CopyFieldlist(typeptr
, subtype
);
812 if( type
->t_union
.un_len
>= 0x8000 )
815 * This is a numeric leaf, I am too lazy to handle this right
818 fprintf(stderr
, "Ignoring large numberic leaf.\n");
821 memset(symname
, 0, sizeof(symname
));
822 memcpy(symname
, type
->t_union
.name
, type
->t_union
.namelen
);
824 if( strcmp(symname
, "__unnamed") == 0 )
826 typeptr
= DEBUG_NewDataType(DT_STRUCT
, NULL
);
830 typeptr
= DEBUG_NewDataType(DT_STRUCT
, symname
);
833 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
836 * Now copy the relevant bits from the fieldlist that we specified.
838 subtype
= DEBUG_GetCVType(type
->t_union
.field
);
840 if( subtype
!= NULL
)
842 DEBUG_SetStructSize(typeptr
, type
->t_union
.un_len
);
843 DEBUG_CopyFieldlist(typeptr
, subtype
);
847 typeptr
= DEBUG_NewDataType(DT_BITFIELD
, NULL
);
848 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
849 DEBUG_SetBitfieldParams(typeptr
, type
->bitfield
.bitoff
,
850 type
->bitfield
.nbits
,
851 DEBUG_GetCVType(type
->bitfield
.type
));
854 memset(symname
, 0, sizeof(symname
));
855 memcpy(symname
, type
->enumeration
.name
, type
->enumeration
.namelen
);
856 typeptr
= DEBUG_NewDataType(DT_ENUM
, symname
);
857 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
860 * Now copy the relevant bits from the fieldlist that we specified.
862 subtype
= DEBUG_GetCVType(type
->enumeration
.field
);
864 if( subtype
!= NULL
)
866 DEBUG_CopyFieldlist(typeptr
, subtype
);
874 ptr
.c
+= (type
->generic
.len
+ 3) & ~3;
881 DEBUG_InitCVDataTypes()
884 * These are the common builtin types that are used by VC++.
886 cv_basic_types
[T_NOTYPE
] = NULL
;
887 cv_basic_types
[T_ABS
] = NULL
;
888 cv_basic_types
[T_VOID
] = DEBUG_NewDataType(DT_BASIC
, "void");
889 cv_basic_types
[T_CHAR
] = DEBUG_NewDataType(DT_BASIC
, "char");
890 cv_basic_types
[T_SHORT
] = DEBUG_NewDataType(DT_BASIC
, "short int");
891 cv_basic_types
[T_LONG
] = DEBUG_NewDataType(DT_BASIC
, "long int");
892 cv_basic_types
[T_QUAD
] = DEBUG_NewDataType(DT_BASIC
, "long long int");
893 cv_basic_types
[T_UCHAR
] = DEBUG_NewDataType(DT_BASIC
, "unsigned char");
894 cv_basic_types
[T_USHORT
] = DEBUG_NewDataType(DT_BASIC
, "short unsigned int");
895 cv_basic_types
[T_ULONG
] = DEBUG_NewDataType(DT_BASIC
, "long unsigned int");
896 cv_basic_types
[T_UQUAD
] = DEBUG_NewDataType(DT_BASIC
, "long long unsigned int");
897 cv_basic_types
[T_REAL32
] = DEBUG_NewDataType(DT_BASIC
, "float");
898 cv_basic_types
[T_REAL64
] = DEBUG_NewDataType(DT_BASIC
, "double");
899 cv_basic_types
[T_RCHAR
] = DEBUG_NewDataType(DT_BASIC
, "char");
900 cv_basic_types
[T_WCHAR
] = DEBUG_NewDataType(DT_BASIC
, "short");
901 cv_basic_types
[T_INT4
] = DEBUG_NewDataType(DT_BASIC
, "int");
902 cv_basic_types
[T_UINT4
] = DEBUG_NewDataType(DT_BASIC
, "unsigned int");
904 cv_basic_types
[T_32PVOID
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_VOID
]);
905 cv_basic_types
[T_32PCHAR
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_CHAR
]);
906 cv_basic_types
[T_32PSHORT
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_SHORT
]);
907 cv_basic_types
[T_32PLONG
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_LONG
]);
908 cv_basic_types
[T_32PQUAD
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_QUAD
]);
909 cv_basic_types
[T_32PUCHAR
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_UCHAR
]);
910 cv_basic_types
[T_32PUSHORT
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_USHORT
]);
911 cv_basic_types
[T_32PULONG
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_ULONG
]);
912 cv_basic_types
[T_32PUQUAD
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_UQUAD
]);
913 cv_basic_types
[T_32PREAL32
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_REAL32
]);
914 cv_basic_types
[T_32PREAL64
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_REAL64
]);
915 cv_basic_types
[T_32PRCHAR
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_RCHAR
]);
916 cv_basic_types
[T_32PWCHAR
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_WCHAR
]);
917 cv_basic_types
[T_32PINT4
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_INT4
]);
918 cv_basic_types
[T_32PUINT4
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_UINT4
]);
922 * In this function, we keep track of deferred debugging information
923 * that we may need later if we were to need to use the internal debugger.
924 * We don't fully process it here for performance reasons.
927 DEBUG_RegisterDebugInfo( HMODULE hModule
, const char *module_name
)
929 int has_codeview
= FALSE
;
932 PIMAGE_DEBUG_DIRECTORY dbgptr
;
934 PIMAGE_NT_HEADERS nth
= PE_HEADER(hModule
);
936 size
= nth
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_DEBUG
].Size
;
938 v_addr
= nth
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
;
939 dbgptr
= (PIMAGE_DEBUG_DIRECTORY
) (hModule
+ v_addr
);
941 for(; size
>= sizeof(*dbgptr
); size
-= sizeof(*dbgptr
), dbgptr
++ )
945 case IMAGE_DEBUG_TYPE_CODEVIEW
:
946 case IMAGE_DEBUG_TYPE_MISC
:
953 dbgptr
= (PIMAGE_DEBUG_DIRECTORY
) (hModule
+ v_addr
);
954 for(; size
>= sizeof(*dbgptr
); size
-= sizeof(*dbgptr
), dbgptr
++ )
958 case IMAGE_DEBUG_TYPE_COFF
:
960 * If we have both codeview and COFF debug info, ignore the
961 * coff debug info as it would just confuse us, and it is
964 * FIXME - this is broken - if we cannot find the PDB file, then
965 * we end up with no debugging info at all. In this case, we
966 * should use the COFF info as a backup.
972 case IMAGE_DEBUG_TYPE_CODEVIEW
:
973 case IMAGE_DEBUG_TYPE_MISC
:
975 * This is usually an indirection to a .DBG file.
976 * This is similar to (but a slightly older format) from the
979 * First check to see if the image was 'stripped'. If so, it
980 * means that this entry points to a .DBG file. Otherwise,
981 * it just points to itself, and we can ignore this.
989 if( (dbgptr
->Type
!= IMAGE_DEBUG_TYPE_MISC
) ||
990 (PE_HEADER(hModule
)->FileHeader
.Characteristics
& IMAGE_FILE_DEBUG_STRIPPED
) != 0 )
994 DOS_FULL_NAME full_name
;
995 struct deferred_debug_info
* deefer
= (struct deferred_debug_info
*) DBG_alloc(sizeof(*deefer
));
997 deefer
->module
= hModule
;
998 deefer
->load_addr
= (char *)hModule
;
1001 * Read the important bits. What we do after this depends
1002 * upon the type, but this is always enough so we are able
1003 * to proceed if we know what we need to do next.
1005 /* basically, the PE loader maps all sections (data, resources...), but doesn't map
1006 * the DataDirectory array's content. One its entry contains the *beloved*
1007 * debug information. (Note the DataDirectory is mapped, not its content)
1010 if (GetModuleFileNameA(hModule
, fn
, sizeof(fn
)) > 0 &&
1011 DOSFS_GetFullName(fn
, TRUE
, &full_name
) &&
1012 (fd
= open(full_name
.long_name
, O_RDONLY
)) > 0)
1014 deefer
->dbg_info
= mmap(NULL
, dbgptr
->SizeOfData
,
1015 PROT_READ
, MAP_PRIVATE
, fd
, dbgptr
->PointerToRawData
);
1017 if( deefer
->dbg_info
== (char *) 0xffffffff )
1026 fprintf(stderr
, " (not mapped: fn=%s, lfn=%s, fd=%d)", fn
, full_name
.long_name
, fd
);
1029 deefer
->dbg_size
= dbgptr
->SizeOfData
;
1030 deefer
->dbgdir
= dbgptr
;
1031 deefer
->next
= dbglist
;
1032 deefer
->status
= DF_STATUS_NEW
;
1033 deefer
->dbg_index
= DEBUG_next_index
;
1034 deefer
->module_name
= DBG_strdup(module_name
);
1036 deefer
->sectp
= PE_SECTIONS(hModule
);
1037 deefer
->nsect
= PE_HEADER(hModule
)->FileHeader
.NumberOfSections
;
1049 /* look for .stabs/.stabstr sections */
1051 PIMAGE_SECTION_HEADER pe_seg
= PE_SECTIONS(hModule
);
1052 int i
,stabsize
=0,stabstrsize
=0;
1053 unsigned int stabs
=0,stabstr
=0;
1055 for (i
=0;i
<nth
->FileHeader
.NumberOfSections
;i
++) {
1056 if (!strcasecmp(pe_seg
[i
].Name
,".stab")) {
1057 stabs
= pe_seg
[i
].VirtualAddress
;
1058 stabsize
= pe_seg
[i
].SizeOfRawData
;
1060 if (!strncasecmp(pe_seg
[i
].Name
,".stabstr",8)) {
1061 stabstr
= pe_seg
[i
].VirtualAddress
;
1062 stabstrsize
= pe_seg
[i
].SizeOfRawData
;
1065 if (stabstrsize
&& stabsize
) {
1066 #ifdef _WE_SUPPORT_THE_STAB_TYPES_USED_BY_MINGW_TOO
1067 /* Won't work currently, since MINGW32 uses some special typedefs
1068 * which we do not handle yet. Support for them is a bit difficult.
1070 DEBUG_ParseStabs(hModule
,0,stabs
,stabsize
,stabstr
,stabstrsize
);
1072 fprintf(stderr
,"(stabs not loaded)");
1079 * ELF modules are also entered into the list - this is so that we
1080 * can make 'info shared' types of displays possible.
1083 DEBUG_RegisterELFDebugInfo(int load_addr
, u_long size
, char * name
)
1085 struct deferred_debug_info
* deefer
;
1087 deefer
= (struct deferred_debug_info
*) DBG_alloc(sizeof(*deefer
));
1091 * Read the important bits. What we do after this depends
1092 * upon the type, but this is always enough so we are able
1093 * to proceed if we know what we need to do next.
1095 deefer
->dbg_size
= size
;
1096 deefer
->dbg_info
= (char *) NULL
;
1098 deefer
->load_addr
= (char *) load_addr
;
1099 deefer
->dbgdir
= NULL
;
1100 deefer
->next
= dbglist
;
1101 deefer
->status
= DF_STATUS_LOADED
;
1102 deefer
->dbg_index
= DEBUG_next_index
;
1103 deefer
->module_name
= DBG_strdup(name
);
1114 * Process COFF debugging information embedded in a Win32 application.
1119 DEBUG_ProcessCoff(struct deferred_debug_info
* deefer
)
1121 struct CoffAuxSection
* aux
;
1122 struct CoffDebug
* coff
;
1123 struct CoffFiles
* coff_files
= NULL
;
1124 struct CoffLinenum
* coff_linetab
;
1126 struct CoffSymbol
* coff_sym
;
1127 struct CoffSymbol
* coff_symbol
;
1128 struct CoffFiles
* curr_file
= NULL
;
1132 struct CoffLinenum
* linepnt
;
1139 int nfiles_alloc
= 0;
1140 struct CoffFiles orig_file
;
1142 char * this_file
= NULL
;
1144 coff
= (struct CoffDebug
*) deefer
->dbg_info
;
1146 coff_symbol
= (struct CoffSymbol
*) ((unsigned int) coff
+ coff
->SymbolOffset
);
1147 coff_linetab
= (struct CoffLinenum
*) ((unsigned int) coff
+ coff
->LinenumberOffset
);
1148 coff_strtab
= (char *) ((unsigned int) coff_symbol
+ 18*coff
->N_Sym
);
1152 for(i
=0; i
< coff
->N_Sym
; i
++ )
1155 * We do this because some compilers (i.e. gcc) incorrectly
1156 * pad the structure up to a 4 byte boundary. The structure
1157 * is really only 18 bytes long, so we have to manually make sure
1160 * FIXME - there must be a way to have autoconf figure out the
1161 * correct compiler option for this. If it is always gcc, that
1162 * makes life simpler, but I don't want to force this.
1164 coff_sym
= (struct CoffSymbol
*) ((unsigned int) coff_symbol
+ 18*i
);
1165 naux
= coff_sym
->NumberOfAuxSymbols
;
1167 if( coff_sym
->StorageClass
== IMAGE_SYM_CLASS_FILE
)
1169 if( nfiles
+ 1 >= nfiles_alloc
)
1172 coff_files
= (struct CoffFiles
*) DBG_realloc(coff_files
,
1173 nfiles_alloc
* sizeof(struct CoffFiles
));
1175 curr_file
= coff_files
+ nfiles
;
1177 curr_file
->startaddr
= 0xffffffff;
1178 curr_file
->endaddr
= 0;
1179 curr_file
->filename
= ((char *) coff_sym
) + 18;
1180 curr_file
->linetab_offset
= -1;
1181 curr_file
->linecnt
= 0;
1182 curr_file
->entries
= NULL
;
1183 curr_file
->neps
= curr_file
->neps_alloc
= 0;
1185 fprintf(stderr
,"New file %s\n", curr_file
->filename
);
1192 * This guy marks the size and location of the text section
1193 * for the current file. We need to keep track of this so
1194 * we can figure out what file the different global functions
1197 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_STATIC
)
1199 && (coff_sym
->Type
== 0)
1200 && (coff_sym
->SectionNumber
== 1) )
1202 aux
= (struct CoffAuxSection
*) ((unsigned int) coff_sym
+ 18);
1204 if( curr_file
->linetab_offset
!= -1 )
1207 fprintf(stderr
, "Duplicating sect from %s: %x %x %x %d %d\n",
1208 curr_file
->filename
,
1210 aux
->NumberOfRelocations
,
1211 aux
->NumberOfLinenumbers
,
1214 fprintf(stderr
, "More sect %d %x %d %d %d\n",
1215 coff_sym
->SectionNumber
,
1218 coff_sym
->StorageClass
,
1219 coff_sym
->NumberOfAuxSymbols
);
1223 * Save this so we can copy bits from it.
1225 orig_file
= *curr_file
;
1228 * Duplicate the file entry. We have no way to describe
1229 * multiple text sections in our current way of handling things.
1231 if( nfiles
+ 1 >= nfiles_alloc
)
1234 coff_files
= (struct CoffFiles
*) DBG_realloc(coff_files
,
1235 nfiles_alloc
* sizeof(struct CoffFiles
));
1237 curr_file
= coff_files
+ nfiles
;
1239 curr_file
->startaddr
= 0xffffffff;
1240 curr_file
->endaddr
= 0;
1241 curr_file
->filename
= orig_file
.filename
;
1242 curr_file
->linetab_offset
= -1;
1243 curr_file
->linecnt
= 0;
1244 curr_file
->entries
= NULL
;
1245 curr_file
->neps
= curr_file
->neps_alloc
= 0;
1250 fprintf(stderr
, "New text sect from %s: %x %x %x %d %d\n",
1251 curr_file
->filename
,
1253 aux
->NumberOfRelocations
,
1254 aux
->NumberOfLinenumbers
,
1260 if( curr_file
->startaddr
> coff_sym
->Value
)
1262 curr_file
->startaddr
= coff_sym
->Value
;
1265 if( curr_file
->startaddr
> coff_sym
->Value
)
1267 curr_file
->startaddr
= coff_sym
->Value
;
1270 if( curr_file
->endaddr
< coff_sym
->Value
+ aux
->Length
)
1272 curr_file
->endaddr
= coff_sym
->Value
+ aux
->Length
;
1275 curr_file
->linetab_offset
= linetab_indx
;
1276 curr_file
->linecnt
= aux
->NumberOfLinenumbers
;
1277 linetab_indx
+= aux
->NumberOfLinenumbers
;
1282 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_STATIC
)
1284 && (coff_sym
->SectionNumber
== 1) )
1287 * This is a normal static function when naux == 0.
1288 * Just register it. The current file is the correct
1289 * one in this instance.
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
;
1302 if( nampnt
[0] == '_' )
1308 new_addr
.off
= (int) (deefer
->load_addr
+ coff_sym
->Value
);
1310 if( curr_file
->neps
+ 1 >= curr_file
->neps_alloc
)
1312 curr_file
->neps_alloc
+= 10;
1313 curr_file
->entries
= (struct name_hash
**)
1314 DBG_realloc(curr_file
->entries
,
1315 curr_file
->neps_alloc
* sizeof(struct name_hash
*));
1318 fprintf(stderr
,"\tAdding static symbol %s\n", nampnt
);
1320 curr_file
->entries
[curr_file
->neps
++] =
1321 DEBUG_AddSymbol( nampnt
, &new_addr
, this_file
, SYM_WIN32
);
1326 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_EXTERNAL
)
1327 && ISFCN(coff_sym
->Type
)
1328 && (coff_sym
->SectionNumber
> 0) )
1330 if( coff_sym
->N
.Name
.NotLong
)
1332 memcpy(namebuff
, coff_sym
->N
.ShortName
, 8);
1334 nampnt
= &namebuff
[0];
1338 nampnt
= coff_strtab
+ coff_sym
->N
.Name
.StrTaboff
;
1342 if( nampnt
[0] == '_' )
1348 new_addr
.off
= (int) (deefer
->load_addr
+ coff_sym
->Value
);
1351 fprintf(stderr
, "%d: %x %s\n", i
, new_addr
.off
, nampnt
);
1353 fprintf(stderr
,"\tAdding global symbol %s\n", nampnt
);
1357 * Now we need to figure out which file this guy belongs to.
1360 for(j
=0; j
< nfiles
; j
++)
1362 if( coff_files
[j
].startaddr
<= coff_sym
->Value
1363 && coff_files
[j
].endaddr
> coff_sym
->Value
)
1365 this_file
= coff_files
[j
].filename
;
1369 if( coff_files
[j
].neps
+ 1 >= coff_files
[j
].neps_alloc
)
1371 coff_files
[j
].neps_alloc
+= 10;
1372 coff_files
[j
].entries
= (struct name_hash
**)
1373 DBG_realloc(coff_files
[j
].entries
,
1374 coff_files
[j
].neps_alloc
* sizeof(struct name_hash
*));
1376 coff_files
[j
].entries
[coff_files
[j
].neps
++] =
1377 DEBUG_AddSymbol( nampnt
, &new_addr
, this_file
, SYM_WIN32
);
1382 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_EXTERNAL
)
1383 && (coff_sym
->SectionNumber
> 0) )
1386 * Similar to above, but for the case of data symbols.
1387 * These aren't treated as entrypoints.
1389 if( coff_sym
->N
.Name
.NotLong
)
1391 memcpy(namebuff
, coff_sym
->N
.ShortName
, 8);
1393 nampnt
= &namebuff
[0];
1397 nampnt
= coff_strtab
+ coff_sym
->N
.Name
.StrTaboff
;
1401 if( nampnt
[0] == '_' )
1407 new_addr
.off
= (int) (deefer
->load_addr
+ coff_sym
->Value
);
1410 fprintf(stderr
, "%d: %x %s\n", i
, new_addr
.off
, nampnt
);
1412 fprintf(stderr
,"\tAdding global data symbol %s\n", nampnt
);
1416 * Now we need to figure out which file this guy belongs to.
1418 DEBUG_AddSymbol( nampnt
, &new_addr
, NULL
, SYM_WIN32
);
1423 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_STATIC
)
1427 * Ignore these. They don't have anything to do with
1435 fprintf(stderr
,"Skipping unknown entry %d %d %d\n", coff_sym
->StorageClass
,
1436 coff_sym
->SectionNumber
, naux
);
1440 * For now, skip past the aux entries.
1447 * OK, we now should have a list of files, and we should have a list
1448 * of entrypoints. We need to sort the entrypoints so that we are
1449 * able to tie the line numbers with the given functions within the
1452 if( coff_files
!= NULL
)
1454 for(j
=0; j
< nfiles
; j
++)
1456 if( coff_files
[j
].entries
!= NULL
)
1458 qsort(coff_files
[j
].entries
, coff_files
[j
].neps
,
1459 sizeof(struct name_hash
*), DEBUG_cmp_sym
);
1464 * Now pick apart the line number tables, and attach the entries
1465 * to the given functions.
1467 for(j
=0; j
< nfiles
; j
++)
1470 if( coff_files
[j
].neps
!= 0 )
1471 for(k
=0; k
< coff_files
[j
].linecnt
; k
++)
1474 * Another monstrosity caused by the fact that we are using
1475 * a 6 byte structure, and gcc wants to pad structures to 4 byte
1476 * boundaries. Otherwise we could just index into an array.
1478 linepnt
= (struct CoffLinenum
*)
1479 ((unsigned int) coff_linetab
+
1480 6*(coff_files
[j
].linetab_offset
+ k
));
1482 * If we have spilled onto the next entrypoint, then
1483 * bump the counter..
1487 if (i
+1 >= coff_files
[j
].neps
) break;
1488 DEBUG_GetSymbolAddr(coff_files
[j
].entries
[i
+1], &new_addr
);
1489 if( (((unsigned int)deefer
->load_addr
+
1490 linepnt
->VirtualAddr
) >= new_addr
.off
) )
1497 * Add the line number. This is always relative to the
1498 * start of the function, so we need to subtract that offset
1501 DEBUG_GetSymbolAddr(coff_files
[j
].entries
[i
], &new_addr
);
1502 DEBUG_AddLineNumber(coff_files
[j
].entries
[i
],
1504 (unsigned int) deefer
->load_addr
1505 + linepnt
->VirtualAddr
1513 if( coff_files
!= NULL
)
1515 for(j
=0; j
< nfiles
; j
++)
1517 if( coff_files
[j
].entries
!= NULL
)
1519 DBG_free(coff_files
[j
].entries
);
1522 DBG_free(coff_files
);
1530 * Process a codeview line number table. Digestify the thing so that
1531 * we can easily reference the thing when we process the rest of
1534 static struct codeview_linetab_hdr
*
1535 DEBUG_SnarfLinetab(char * linetab
,
1539 char filename
[PATH_MAX
];
1540 unsigned int * filetab
;
1544 struct codeview_linetab_hdr
* lt_hdr
;
1545 unsigned int * lt_ptr
;
1549 union any_size pnt2
;
1550 struct startend
* start
;
1554 * Now get the important bits.
1560 filetab
= (unsigned int *) pnt
.c
;
1563 * Now count up the number of segments in the file.
1566 for(i
=0; i
<nfile
; i
++)
1568 pnt2
.c
= linetab
+ filetab
[i
];
1573 * Next allocate the header we will be returning.
1574 * There is one header for each segment, so that we can reach in
1575 * and pull bits as required.
1577 lt_hdr
= (struct codeview_linetab_hdr
*)
1578 DBG_alloc((nseg
+ 1) * sizeof(*lt_hdr
));
1579 if( lt_hdr
== NULL
)
1584 memset(lt_hdr
, 0, sizeof(*lt_hdr
) * (nseg
+1));
1587 * Now fill the header we will be returning, one for each segment.
1588 * Note that this will basically just contain pointers into the existing
1589 * line table, and we do not actually copy any additional information
1590 * or allocate any additional memory.
1594 for(i
=0; i
<nfile
; i
++)
1597 * Get the pointer into the segment information.
1599 pnt2
.c
= linetab
+ filetab
[i
];
1600 file_segcount
= *pnt2
.s
;
1603 lt_ptr
= (unsigned int *) pnt2
.c
;
1604 start
= (struct startend
*) (lt_ptr
+ file_segcount
);
1607 * Now snarf the filename for all of the segments for this file.
1609 fn
= (unsigned char *) (start
+ file_segcount
);
1610 memset(filename
, 0, sizeof(filename
));
1611 memcpy(filename
, fn
+ 1, *fn
);
1612 fn
= DBG_strdup(filename
);
1614 for(k
= 0; k
< file_segcount
; k
++, this_seg
++)
1616 pnt2
.c
= linetab
+ lt_ptr
[k
];
1617 lt_hdr
[this_seg
].start
= start
[k
].start
;
1618 lt_hdr
[this_seg
].end
= start
[k
].end
;
1619 lt_hdr
[this_seg
].sourcefile
= fn
;
1620 lt_hdr
[this_seg
].segno
= *pnt2
.s
++;
1621 lt_hdr
[this_seg
].nline
= *pnt2
.s
++;
1622 lt_hdr
[this_seg
].offtab
= pnt2
.ui
;
1623 lt_hdr
[this_seg
].linetab
= (unsigned short *)
1624 (pnt2
.ui
+ lt_hdr
[this_seg
].nline
);
1635 DEBUG_SnarfCodeView( struct deferred_debug_info
* deefer
,
1638 struct codeview_linetab_hdr
* linetab
)
1640 struct name_hash
* curr_func
= NULL
;
1641 struct wine_locals
* curr_sym
= NULL
;
1648 IMAGE_SECTION_HEADER
* sectp
;
1649 union codeview_symbol
* sym
;
1650 char symname
[PATH_MAX
];
1651 struct name_hash
* thunk_sym
= NULL
;
1654 nsect
= deefer
->nsect
;
1655 sectp
= deefer
->sectp
;
1658 * Skip over the first word. Don't really know what it means, but
1664 * Loop over the different types of records and whenever we
1665 * find something we are interested in, record it and move on.
1667 while( ptr
.c
- cv_data
< size
)
1669 sym
= (union codeview_symbol
*) ptr
.c
;
1671 if( sym
->generic
.len
- sizeof(int) == (ptr
.c
- cv_data
) )
1674 * This happens when we have indirect symbols that VC++ 4.2
1675 * sometimes uses when there isn't a line number table.
1676 * We ignore it - we will process and enter all of the
1677 * symbols in the global symbol table anyways, so there
1678 * isn't much point in keeping track of all of this crap.
1683 memset(symname
, 0, sizeof(symname
));
1684 switch(sym
->generic
.id
)
1690 * First, a couple of sanity checks.
1692 if( sym
->data
.namelen
== 0 )
1697 if( sym
->data
.seg
== 0 || sym
->data
.seg
> nsect
)
1703 * Global and local data symbols. We don't associate these
1704 * with any given source file.
1707 memcpy(symname
, sym
->data
.name
, sym
->data
.namelen
);
1709 new_addr
.type
= DEBUG_GetCVType(sym
->data
.symtype
);
1710 new_addr
.off
= (unsigned int) deefer
->load_addr
+
1711 sectp
[sym
->data
.seg
- 1].VirtualAddress
+
1713 DEBUG_AddSymbol( symname
, &new_addr
, NULL
, SYM_WIN32
| SYM_DATA
);
1717 * Sort of like a global function, but it just points
1718 * to a thunk, which is a stupid name for what amounts to
1719 * a PLT slot in the normal jargon that everyone else uses.
1721 memcpy(symname
, sym
->thunk
.name
, sym
->thunk
.namelen
);
1723 new_addr
.type
= NULL
;
1724 new_addr
.off
= (unsigned int) deefer
->load_addr
+
1725 sectp
[sym
->thunk
.segment
- 1].VirtualAddress
+
1727 thunk_sym
= DEBUG_AddSymbol( symname
, &new_addr
, NULL
,
1728 SYM_WIN32
| SYM_FUNC
);
1729 DEBUG_SetSymbolSize(thunk_sym
, sym
->thunk
.thunk_len
);
1734 * Global and static functions.
1736 memcpy(symname
, sym
->proc
.name
, sym
->proc
.namelen
);
1738 new_addr
.type
= DEBUG_GetCVType(sym
->proc
.proctype
);
1739 new_addr
.off
= (unsigned int) deefer
->load_addr
+
1740 sectp
[sym
->proc
.segment
- 1].VirtualAddress
+
1743 * See if we can find a segment that this goes with. If so,
1744 * it means that we also may have line number information
1745 * for this function.
1747 for(i
=0; linetab
[i
].linetab
!= NULL
; i
++)
1749 if( ((unsigned int) deefer
->load_addr
1750 + sectp
[linetab
[i
].segno
- 1].VirtualAddress
1751 + linetab
[i
].start
<= new_addr
.off
)
1752 && ((unsigned int) deefer
->load_addr
1753 + sectp
[linetab
[i
].segno
- 1].VirtualAddress
1754 + linetab
[i
].end
> new_addr
.off
) )
1760 DEBUG_Normalize(curr_func
);
1761 if( linetab
[i
].linetab
== NULL
)
1763 curr_func
= DEBUG_AddSymbol( symname
, &new_addr
, NULL
,
1764 SYM_WIN32
| SYM_FUNC
);
1769 * First, create the entry. Then dig through the linetab
1770 * and add whatever line numbers are appropriate for this
1773 curr_func
= DEBUG_AddSymbol( symname
, &new_addr
,
1774 linetab
[i
].sourcefile
,
1775 SYM_WIN32
| SYM_FUNC
);
1776 for(j
=0; j
< linetab
[i
].nline
; j
++)
1778 if( linetab
[i
].offtab
[j
] >= sym
->proc
.offset
1779 && linetab
[i
].offtab
[j
] < sym
->proc
.offset
1780 + sym
->proc
.proc_len
)
1782 DEBUG_AddLineNumber(curr_func
, linetab
[i
].linetab
[j
],
1783 linetab
[i
].offtab
[j
] - sym
->proc
.offset
);
1790 * Add information about where we should set breakpoints
1793 DEBUG_SetSymbolBPOff(curr_func
, sym
->proc
.debug_start
);
1794 DEBUG_SetSymbolSize(curr_func
, sym
->proc
.proc_len
);
1798 * Function parameters and stack variables.
1800 memcpy(symname
, sym
->stack
.name
, sym
->stack
.namelen
);
1801 curr_sym
= DEBUG_AddLocal(curr_func
,
1807 DEBUG_SetLocalSymbolType(curr_sym
, DEBUG_GetCVType(sym
->stack
.symtype
));
1815 * Adjust pointer to point to next entry, rounding up to a word
1816 * boundary. MS preserving alignment? Stranger things have
1819 if( sym
->generic
.id
== S_PROCREF
1820 || sym
->generic
.id
== S_DATAREF
1821 || sym
->generic
.id
== S_UNKNOWN
)
1823 len
= (sym
->generic
.len
+ 3) & ~3;
1824 len
+= ptr
.c
[16] + 1;
1825 ptr
.c
+= (len
+ 3) & ~3;
1829 ptr
.c
+= (sym
->generic
.len
+ 3) & ~3;
1833 if( linetab
!= NULL
)
1843 * Process PDB file which contains debug information.
1845 * These are really weird beasts. They are intended to be incrementally
1846 * updated by the incremental linker, and this means that you need to
1847 * be able to remove and add information. Thus the PDB file is sort of
1848 * like a block structured device, with a freelist and lists of extent numbers
1849 * that are used to get the relevant pieces. In all cases seen so far, the
1850 * blocksize is always 0x400 bytes. The header has a field which apparently
1851 * holds the blocksize, so if it ever changes we are safe.
1853 * In general, every time we need to extract something from the pdb file,
1854 * it is easier to copy it into another buffer so we have the information
1855 * in one contiguous block rather than attempt to try and keep track of when
1856 * we need to grab another extent from the pdb file.
1858 * The thing that is a real pain about some MS stuff is that they choose
1859 * data structures which are not representable in C. Thus we have to
1860 * hack around and diddle pointers.
1864 DEBUG_ProcessPDBFile(struct deferred_debug_info
* deefer
, char * full_filename
)
1866 char * addr
= (char *) 0xffffffff;
1867 unsigned int blocksize
;
1868 unsigned int bufflen
= 0;
1869 char * buffer
= NULL
;
1870 unsigned short * extent_table
;
1872 struct file_ent
* fent
;
1873 char filename
[MAX_PATHNAME_LEN
];
1874 struct file_list
* filelist
= NULL
;
1875 unsigned int gsym_record
= 0;
1876 char * gsymtab
= NULL
;
1877 struct filetab_hdr
* hd
;
1880 unsigned int last_extent
;
1881 struct codeview_linetab_hdr
* linetab
;
1882 unsigned int nblocks
;
1884 unsigned int offset
;
1885 struct codeview_pdb_hdr
* pdbhdr
;
1887 struct stat statbuf
;
1889 unsigned short * table
;
1891 unsigned int toc_blocks
;
1893 LocateDebugInfoFile(full_filename
, filename
);
1894 status
= stat(filename
, &statbuf
);
1897 fprintf(stderr
, "-Unable to open .PDB file %s\n", filename
);
1902 * Now open the file, so that we can mmap() it.
1904 fd
= open(filename
, O_RDONLY
);
1907 fprintf(stderr
, "-Unable to open .DBG file %s\n", filename
);
1913 * Now mmap() the file.
1915 addr
= mmap(0, statbuf
.st_size
, PROT_READ
,
1916 MAP_PRIVATE
, fd
, 0);
1917 if( addr
== (char *) 0xffffffff )
1919 fprintf(stderr
, "-Unable to mmap .DBG file %s\n", filename
);
1924 * Now that we have the formalities over and done with, we need
1925 * to find the table of contents for the PDB file.
1927 pdbhdr
= (struct codeview_pdb_hdr
*) addr
;
1928 blocksize
= pdbhdr
->blocksize
;
1929 last_extent
= (statbuf
.st_size
+ blocksize
- 1) / blocksize
;
1932 * The TOC itself isn't always contiguous, so we need to extract a few
1933 * extents from the file to form the TOC.
1935 toc_blocks
= (pdbhdr
->toc_len
+ blocksize
- 1) / blocksize
;
1936 toc
= (char *) DBG_alloc(toc_blocks
* blocksize
);
1937 table
= pdbhdr
->toc_ext
;
1938 for(i
=0; i
< toc_blocks
; i
++)
1940 memcpy(toc
+ blocksize
*i
, addr
+ table
[i
]*blocksize
, blocksize
);
1944 * Next build our own table which will have the size and extent block
1945 * list for each record in the PDB file.
1947 * The TOC starts out with the number of files. Then it is followed by
1948 * (npair * 2*sizeof(int)) bytes of information, which are pairs of ints.
1949 * The first one is the size of the record (in bytes), and the second one
1950 * is something else which I haven't figured out yet.
1952 pnt
= (unsigned int *) toc
;
1954 extent_table
= (unsigned short *) ((unsigned int) toc
+
1955 npair
* 2 * sizeof(int) + sizeof(int));
1960 if( sizeof(int) + 2*sizeof(int)*npair
> pdbhdr
->toc_len
)
1965 filelist
= (struct file_list
*) DBG_alloc(npair
* sizeof(*filelist
));
1966 if( filelist
== NULL
)
1970 memset(filelist
, 0, npair
* sizeof(*filelist
));
1973 for(i
=0; i
< npair
; i
++)
1975 filelist
[i
].record_len
= pnt
[i
*2];
1976 filelist
[i
].nextents
= (filelist
[i
].record_len
+ blocksize
- 1)
1978 filelist
[i
].extent_list
= extent_table
+ nblocks
;
1979 nblocks
+= filelist
[i
].nextents
;
1982 * These get filled in later when we parse one of the records.
1984 filelist
[i
].linetab_offset
= 0;
1985 filelist
[i
].linetab_len
= 0;
1989 * OK, now walk through the various records and pick out the bits we
1990 * really want to see. Some of the records are extra special, and
1991 * we need to handle these a little bit differently.
1993 for(i
=0; i
< npair
; i
++)
1995 if( filelist
[i
].record_len
== 0xffffffff )
2001 * Make sure our buffer is large enough to hold the record.
2003 if( bufflen
< filelist
[i
].nextents
* blocksize
)
2005 bufflen
= filelist
[i
].nextents
* blocksize
;
2006 buffer
= (char *) DBG_realloc(buffer
, bufflen
);
2010 * Do this just for completeness. It makes debugging easier
2011 * if we have a clean indication of where the record ends.
2013 memset(buffer
, 0, filelist
[i
].nextents
* blocksize
);
2016 * Next, build the record using the extent list.
2018 for(j
=0; j
< filelist
[i
].nextents
; j
++)
2020 memcpy(buffer
+ j
* blocksize
,
2021 addr
+ filelist
[i
].extent_list
[j
] * blocksize
,
2025 pnt
= (unsigned int *) buffer
;
2028 * OK, now figure out what to do with it.
2032 * Always ignore the first entry. It seems to contain a backup copy
2033 * of the TOC (the last time the file was modified??)
2041 * The second entry as a id block. It contains a magic number
2042 * to identify the compiler, plus it also contains the timestamp
2043 * which must match the timestamp in the executable.
2048 if( ((*pnt
!= 19950623) && (*pnt
!= 19950814))
2049 || (filelist
[i
].record_len
!= 0x24)
2050 || (pnt
[1] != ((struct CodeViewDebug
*)(deefer
->dbg_info
))->cv_timestamp
) )
2057 * The third entry contains pointers to the global symbol table,
2058 * plus it also contains additional information about each record
2063 hd
= (struct filetab_hdr
*) buffer
;
2065 gsym_record
= hd
->gsym_file
;
2066 gsymtab
= (char *) DBG_alloc(filelist
[gsym_record
].nextents
2068 memset(gsymtab
, 0, filelist
[gsym_record
].nextents
* blocksize
);
2070 for(j
=0; j
< filelist
[gsym_record
].nextents
; j
++)
2072 memcpy(gsymtab
+ j
* blocksize
,
2073 addr
+ filelist
[gsym_record
].extent_list
[j
] * blocksize
,
2078 * This record also contains information about where in the
2079 * remaining records we will be able to find the start of the
2080 * line number table. We could locate that bit using heuristics,
2081 * but since we have the info handy, we might as well use it.
2083 offset
= sizeof(*hd
);
2086 fent
= (struct file_ent
*) (buffer
+ offset
);
2087 if( offset
> hd
->ftab_len
)
2092 if( fent
->file_number
== 0 || fent
->file_number
>= npair
)
2097 filelist
[fent
->file_number
].linetab_offset
=
2098 fent
->linetab_offset
;
2099 filelist
[fent
->file_number
].linetab_len
=
2102 * Figure out the offset of the next entry.
2103 * There is a fixed part of the record and a variable
2104 * length filename which we must also skip past.
2106 offset
+= ((unsigned int) &fent
->filename
- (unsigned int) fent
)
2107 + strlen(fent
->filename
) + 1;
2108 offset
+= strlen(buffer
+offset
) + 1;
2109 offset
= (offset
+ 3) & ~3;
2115 * Two different magic numbers used as dates.
2116 * These indicate the 'type' table.
2118 if( *pnt
== 19950410
2119 || *pnt
== 19951122 )
2121 DEBUG_ParseTypeTable(buffer
, filelist
[i
].record_len
);
2126 * This is something we really want to look at, since it contains
2127 * real debug info. Anything that doesn't match this can be
2133 * First, snag the line table, if we have one. This always
2134 * occurs at the end of the record, so we take the linetab
2135 * offset as the end of the normal part of the record.
2138 if( filelist
[i
].linetab_len
!= 0 )
2140 linetab
= DEBUG_SnarfLinetab(buffer
+ filelist
[i
].linetab_offset
,
2141 filelist
[i
].linetab_len
);
2142 DEBUG_SnarfCodeView(deefer
, buffer
,
2143 filelist
[i
].linetab_offset
,
2148 DEBUG_SnarfCodeView(deefer
, buffer
,
2149 filelist
[i
].record_len
,
2157 * Finally, process the global symbol table itself. There isn't
2158 * a line number component to this, so we just toss everything
2159 * into the mix and it all should work out.
2161 if( gsym_record
!= 0 )
2163 DEBUG_SnarfCodeView(deefer
, gsymtab
- sizeof(int),
2164 filelist
[gsym_record
].record_len
,
2170 if( gsymtab
!= NULL
)
2176 if( buffer
!= NULL
)
2181 if( filelist
!= NULL
)
2186 if( addr
!= (char *) 0xffffffff )
2188 munmap(addr
, statbuf
.st_size
);
2200 * Process DBG file which contains debug information.
2204 DEBUG_ProcessDBGFile(struct deferred_debug_info
* deefer
, char * filename
)
2206 char * addr
= (char *) 0xffffffff;
2208 struct CV4_DirHead
* codeview_dir
;
2209 struct CV4_DirEnt
* codeview_dent
;
2210 PIMAGE_DEBUG_DIRECTORY dbghdr
;
2211 struct deferred_debug_info deefer2
;
2215 struct codeview_linetab_hdr
* linetab
;
2217 PIMAGE_SEPARATE_DEBUG_HEADER pdbg
= NULL
;
2218 IMAGE_SECTION_HEADER
* sectp
;
2219 struct stat statbuf
;
2221 char dbg_file
[MAX_PATHNAME_LEN
];
2223 LocateDebugInfoFile(filename
, dbg_file
);
2224 status
= stat(dbg_file
, &statbuf
);
2227 fprintf(stderr
, "-Unable to open .DBG file %s\n", dbg_file
);
2232 * Now open the file, so that we can mmap() it.
2234 fd
= open(dbg_file
, O_RDONLY
);
2237 fprintf(stderr
, "Unable to open .DBG file %s\n", dbg_file
);
2243 * Now mmap() the file.
2245 addr
= mmap(0, statbuf
.st_size
, PROT_READ
,
2246 MAP_PRIVATE
, fd
, 0);
2247 if( addr
== (char *) 0xffffffff )
2249 fprintf(stderr
, "Unable to mmap .DBG file %s\n", dbg_file
);
2253 pdbg
= (PIMAGE_SEPARATE_DEBUG_HEADER
) addr
;
2255 if( pdbg
->TimeDateStamp
!= deefer
->dbgdir
->TimeDateStamp
)
2257 fprintf(stderr
, "Warning - %s has incorrect internal timestamp\n",
2261 Well, sometimes this happens to DBG files which ARE REALLY the right .DBG
2262 files but nonetheless this check fails. Anyway, WINDBG (debugger for
2263 Windows by Microsoft) loads debug symbols which have incorrect timestamps.
2267 fprintf(stderr
, "Processing symbols from %s...\n", dbg_file
);
2269 dbghdr
= (PIMAGE_DEBUG_DIRECTORY
) ( addr
+ sizeof(*pdbg
)
2270 + pdbg
->NumberOfSections
* sizeof(IMAGE_SECTION_HEADER
)
2271 + pdbg
->ExportedNamesSize
);
2273 sectp
= (PIMAGE_SECTION_HEADER
) ((char *) pdbg
+ sizeof(*pdbg
));
2274 nsect
= pdbg
->NumberOfSections
;
2276 for( i
=0; i
< pdbg
->DebugDirectorySize
/ sizeof(*pdbg
); i
++, dbghdr
++ )
2278 switch(dbghdr
->Type
)
2280 case IMAGE_DEBUG_TYPE_COFF
:
2282 * Dummy up a deferred debug header to handle the
2283 * COFF stuff embedded within the DBG file.
2285 memset((char *) &deefer2
, 0, sizeof(deefer2
));
2286 deefer2
.dbg_info
= (addr
+ dbghdr
->PointerToRawData
);
2287 deefer2
.dbg_size
= dbghdr
->SizeOfData
;
2288 deefer2
.load_addr
= deefer
->load_addr
;
2290 DEBUG_ProcessCoff(&deefer2
);
2292 case IMAGE_DEBUG_TYPE_CODEVIEW
:
2294 * This is the older format by which codeview stuff is
2295 * stored, known as the 'NB09' format. Newer executables
2296 * and dlls created by VC++ use PDB files instead, which
2297 * have lots of internal similarities, but the overall
2298 * format and structure is quite different.
2300 codeview
= (addr
+ dbghdr
->PointerToRawData
);
2303 * The first thing in the codeview section should be
2304 * an 'NB09' identifier. As a sanity check, make sure
2307 if( *((unsigned int*) codeview
) != 0x3930424e )
2313 * Next we need to find the directory. This is easy too.
2315 codeview_dir
= (struct CV4_DirHead
*)
2316 (codeview
+ ((unsigned int*) codeview
)[1]);
2319 * Some more sanity checks. Make sure that everything
2320 * is as we expect it.
2322 if( codeview_dir
->next_offset
!= 0
2323 || codeview_dir
->dhsize
!= sizeof(*codeview_dir
)
2324 || codeview_dir
->desize
!= sizeof(*codeview_dent
) )
2328 codeview_dent
= (struct CV4_DirEnt
*) (codeview_dir
+ 1);
2330 for(j
=0; j
< codeview_dir
->ndir
; j
++, codeview_dent
++)
2332 if( codeview_dent
->subsect_number
== sstAlignSym
)
2335 * Check the previous entry. If it is a
2336 * sstSrcModule, it contains the line number
2337 * info for this file.
2340 if( codeview_dent
[1].module_number
== codeview_dent
[0].module_number
2341 && codeview_dent
[1].subsect_number
== sstSrcModule
)
2343 linetab
= DEBUG_SnarfLinetab(
2344 codeview
+ codeview_dent
[1].offset
,
2345 codeview_dent
[1].size
);
2348 if( codeview_dent
[-1].module_number
== codeview_dent
[0].module_number
2349 && codeview_dent
[-1].subsect_number
== sstSrcModule
)
2351 linetab
= DEBUG_SnarfLinetab(
2352 codeview
+ codeview_dent
[-1].offset
,
2353 codeview_dent
[-1].size
);
2356 * Now process the CV stuff.
2358 DEBUG_SnarfCodeView(deefer
,
2359 codeview
+ codeview_dent
->offset
,
2360 codeview_dent
->size
,
2372 if( addr
!= (char *) 0xffffffff )
2374 munmap(addr
, statbuf
.st_size
);
2386 DEBUG_ProcessDeferredDebug()
2388 struct deferred_debug_info
* deefer
;
2389 struct CodeViewDebug
* cvd
;
2390 struct MiscDebug
* misc
;
2396 for(deefer
= dbglist
; deefer
; deefer
= deefer
->next
)
2398 if( deefer
->status
!= DF_STATUS_NEW
)
2403 if( last_proc
!= deefer
->dbg_index
)
2407 fprintf(stderr
, "DeferredDebug for:");
2410 fprintf(stderr
, " %s",deefer
->module_name
);
2411 last_proc
= deefer
->dbg_index
;
2414 switch(deefer
->dbgdir
->Type
)
2416 case IMAGE_DEBUG_TYPE_COFF
:
2418 * Standard COFF debug information that VC++ adds when you
2419 * use /debugtype:both with the linker.
2422 fprintf(stderr
, "Processing COFF symbols...\n");
2424 sts
= DEBUG_ProcessCoff(deefer
);
2426 case IMAGE_DEBUG_TYPE_CODEVIEW
:
2428 * This is a pointer to a PDB file of some sort.
2430 cvd
= (struct CodeViewDebug
*) deefer
->dbg_info
;
2432 if( strcmp(cvd
->cv_nbtype
, "NB10") != 0 )
2435 * Whatever this is, we don't know how to deal with
2441 sts
= DEBUG_ProcessPDBFile(deefer
, cvd
->cv_name
);
2443 fprintf(stderr
, "Processing PDB file %s\n", cvd
->cv_name
);
2446 case IMAGE_DEBUG_TYPE_MISC
:
2448 * A pointer to a .DBG file of some sort. These files
2449 * can contain either CV4 or COFF information. Open
2450 * the file, and try to do the right thing with it.
2452 misc
= (struct MiscDebug
*) deefer
->dbg_info
;
2454 filename
= strrchr((char *) &misc
->Data
, '.');
2457 * Ignore the file if it doesn't have a .DBG extension.
2459 if( (filename
== NULL
)
2460 || ( (strcmp(filename
, ".dbg") != 0)
2461 && (strcmp(filename
, ".DBG") != 0)) )
2467 filename
= (char *) &misc
->Data
;
2470 * Do the dirty deed...
2472 sts
= DEBUG_ProcessDBGFile(deefer
, filename
);
2477 * We should never get here...
2482 deefer
->status
= (sts
) ? DF_STATUS_LOADED
: DF_STATUS_ERROR
;
2486 fprintf(stderr
, "\n");
2491 /***********************************************************************
2494 * Display shared libarary information.
2496 void DEBUG_InfoShare(void)
2498 struct deferred_debug_info
* deefer
;
2500 fprintf(stderr
,"Address\t\tModule\tName\n");
2502 for(deefer
= dbglist
; deefer
; deefer
= deefer
->next
)
2504 fprintf(stderr
,"0x%8.8x\t(%s)\t%s\n", (unsigned int) deefer
->load_addr
,
2505 deefer
->module
? "Win32" : "ELF", deefer
->module_name
);