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 * This is an index we use to keep track of the debug information
40 * when we have multiple sources. We use the same database to also
41 * allow us to do an 'info shared' type of deal, and we use the index
42 * to eliminate duplicates.
44 static int DEBUG_next_index
= 0;
55 * This is a convenience structure used to map portions of the
65 * This is how we reference the various record types.
81 unsigned short symtype
;
82 unsigned char namelen
;
83 unsigned char name
[1];
94 unsigned short segment
;
95 unsigned short thunk_len
;
97 unsigned char namelen
;
98 unsigned char name
[1];
104 unsigned int pparent
;
107 unsigned int proc_len
;
108 unsigned int debug_start
;
109 unsigned int debug_end
;
111 unsigned short segment
;
112 unsigned short proctype
;
114 unsigned char namelen
;
115 unsigned char name
[1];
119 short int len
; /* Total length of this entry */
120 short int id
; /* Always S_BPREL32 */
121 unsigned int offset
; /* Stack offset relative to BP */
122 unsigned short symtype
;
123 unsigned char namelen
;
124 unsigned char name
[1];
142 unsigned char variant
[1];
150 unsigned char bitoff
;
160 unsigned char arrlen
;
161 unsigned char namelen
;
162 unsigned char name
[1];
174 unsigned short structlen
;
175 unsigned char namelen
;
176 unsigned char name
[1];
186 unsigned short un_len
;
187 unsigned char namelen
;
188 unsigned char name
[1];
199 unsigned char namelen
;
200 unsigned char name
[1];
207 unsigned short int value
;
208 unsigned char namelen
;
209 unsigned char name
[1];
217 unsigned short int offset
;
218 unsigned char namelen
;
219 unsigned char name
[1];
230 unsigned char namelen
;
231 unsigned char name
[1];
236 #define S_BPREL32 0x200
237 #define S_LDATA32 0x201
238 #define S_GDATA32 0x202
239 #define S_PUB32 0x203
240 #define S_LPROC32 0x204
241 #define S_GPROC32 0x205
242 #define S_THUNK32 0x206
243 #define S_BLOCK32 0x207
244 #define S_WITH32 0x208
245 #define S_LABEL32 0x209
247 #define S_PROCREF 0x400
248 #define S_DATAREF 0x401
249 #define S_ALIGN 0x402
250 #define S_UNKNOWN 0x403
253 * This covers the basic datatypes that VC++ seems to be using these days.
254 * 32 bit mode only. There are additional numbers for the pointers in 16
255 * bit mode. There are many other types listed in the documents, but these
256 * are apparently not used by the compiler, or represent pointer types
259 #define T_NOTYPE 0x0000 /* Notype */
260 #define T_ABS 0x0001 /* Abs */
261 #define T_VOID 0x0003 /* Void */
262 #define T_CHAR 0x0010 /* signed char */
263 #define T_SHORT 0x0011 /* short */
264 #define T_LONG 0x0012 /* long */
265 #define T_QUAD 0x0013 /* long long */
266 #define T_UCHAR 0x0020 /* unsigned char */
267 #define T_USHORT 0x0021 /* unsigned short */
268 #define T_ULONG 0x0022 /* unsigned long */
269 #define T_UQUAD 0x0023 /* unsigned long long */
270 #define T_REAL32 0x0040 /* float */
271 #define T_REAL64 0x0041 /* double */
272 #define T_RCHAR 0x0070 /* real char */
273 #define T_WCHAR 0x0071 /* wide char */
274 #define T_INT4 0x0074 /* int */
275 #define T_UINT4 0x0075 /* unsigned int */
277 #define T_32PVOID 0x0403 /* 32 bit near pointer to void */
278 #define T_32PCHAR 0x0410 /* 16:32 near pointer to signed char */
279 #define T_32PSHORT 0x0411 /* 16:32 near pointer to short */
280 #define T_32PLONG 0x0412 /* 16:32 near pointer to int */
281 #define T_32PQUAD 0x0413 /* 16:32 near pointer to long long */
282 #define T_32PUCHAR 0x0420 /* 16:32 near pointer to unsigned char */
283 #define T_32PUSHORT 0x0421 /* 16:32 near pointer to unsigned short */
284 #define T_32PULONG 0x0422 /* 16:32 near pointer to unsigned int */
285 #define T_32PUQUAD 0x0423 /* 16:32 near pointer to long long */
286 #define T_32PREAL32 0x0440 /* 16:32 near pointer to float */
287 #define T_32PREAL64 0x0441 /* 16:32 near pointer to float */
288 #define T_32PRCHAR 0x0470 /* 16:32 near pointer to real char */
289 #define T_32PWCHAR 0x0471 /* 16:32 near pointer to real char */
290 #define T_32PINT4 0x0474 /* 16:32 near pointer to int */
291 #define T_32PUINT4 0x0475 /* 16:32 near pointer to unsigned int */
293 #define LF_MODIFIER 0x1
294 #define LF_POINTER 0x2
297 #define LF_STRUCTURE 0x5
299 #define LF_ENUMERATION 0x7
300 #define LF_PROCEDURE 0x8
301 #define LF_MFUNCTION 0x9
302 #define LF_VTSHAPE 0xa
303 #define LF_BARRAY 0xd
304 #define LF_DIMARRAY 0x11
305 #define LF_VFTPATH 0x12
307 #define LF_SKIP 0x200
308 #define LF_ARGLIST 0x201
309 #define LF_FIELDLIST 0x204
310 #define LF_DERIVED 0x205
311 #define LF_BITFIELD 0x206
313 #define LF_BCLASS 0x400
314 #define LF_VBCLASS 0x401
315 #define LF_IVBCLASS 0x402
316 #define LF_ENUMERATE 0x403
317 #define LF_FRIENDFCN 0x404
318 #define LF_INDEX 0x405
319 #define LF_MEMBER 0x406
320 #define LF_STMEMBER 0x407
321 #define LF_METHOD 0x408
322 #define LF_NESTEDTYPE 0x409
323 #define LF_VFUNCTAB 0x40a
324 #define LF_FRIENDCLS 0x40b
325 #define LF_ONEMETHOD 0x40c
326 #define LF_FUNCOFF 0x40d
328 #define MAX_BUILTIN_TYPES 0x480
329 static struct datatype
* cv_basic_types
[MAX_BUILTIN_TYPES
];
330 static int num_cv_defined_types
= 0;
331 static struct datatype
**cv_defined_types
= NULL
;
334 * For the type CODEVIEW debug directory entries, the debug directory
335 * points to a structure like this. The cv_name field is the name
336 * of an external .PDB file.
341 unsigned int cv_timestamp
;
347 unsigned int DataType
;
355 * This is the header that the COFF variety of debug header points to.
359 unsigned int SymbolOffset
;
360 unsigned int N_Linenum
;
361 unsigned int LinenumberOffset
;
362 unsigned int Unused
[4];
366 unsigned int VirtualAddr
;
367 unsigned short int Linenum
;
371 unsigned int startaddr
;
372 unsigned int endaddr
;
376 struct name_hash
**entries
;
386 unsigned int NotLong
;
387 unsigned int StrTaboff
;
394 unsigned char NumberOfAuxSymbols
;
397 struct CoffAuxSection
{
399 unsigned short NumberOfRelocations
;
400 unsigned short NumberOfLinenumbers
;
401 unsigned int CheckSum
;
407 * These two structures are used in the directory within a .DBG file
408 * to locate the individual important bits that we might want to see.
411 short unsigned int dhsize
;
412 short unsigned int desize
;
414 unsigned int next_offset
;
419 short unsigned int subsect_number
;
420 short unsigned int module_number
;
426 * These are the values of interest that the subsect_number field takes.
428 #define sstAlignSym 0x125
429 #define sstSrcModule 0x127
431 struct codeview_linetab_hdr
438 unsigned short * linetab
;
439 unsigned int * offtab
;
442 struct codeview_pdb_hdr
445 unsigned int blocksize
; /* Extent size */
446 unsigned short loc_freelist
; /* freelist. */
447 unsigned short alloc_filesize
; /* # extents allocated. */
448 unsigned int toc_len
;
449 unsigned int unknown
;
450 unsigned short toc_ext
[1]; /* array of extent #'s for toc. */
454 * This is our own structure that we use to keep track of the contents
461 short int * extent_list
;
462 unsigned int linetab_offset
;
463 unsigned int linetab_len
;
467 * These are the structures that represent how the file table is set up
468 * within the PDB file.
472 unsigned short tab1_file
;
473 unsigned short tab2_file
;
474 unsigned short gsym_file
;
475 unsigned short padding
;
476 unsigned int ftab_len
;
477 unsigned int fofftab_len
;
478 unsigned int hash_len
;
479 unsigned int strtab_len
;
484 unsigned int reserved1
;
485 unsigned short datasect_segment
;
486 unsigned short reserved2
;
487 unsigned int datasect_offset
;
488 unsigned int datasect_size
;
489 unsigned int datasect_flags
;
490 unsigned short reserved3
;
491 unsigned short index
;
492 unsigned short num6a
;
493 unsigned short file_number
;
494 unsigned int linetab_offset
;
495 unsigned int linetab_len
;
499 unsigned char filename
[1];
503 ********************************************************************
505 struct deferred_debug_info
507 struct deferred_debug_info
* next
;
513 PIMAGE_DEBUG_DIRECTORY dbgdir
;
514 PIMAGE_SECTION_HEADER sectp
;
520 struct deferred_debug_info
* dbglist
= NULL
;
523 * A simple macro that tells us whether a given COFF symbol is a
526 #define N_TMASK 0x0030
527 #define IMAGE_SYM_DTYPE_FUNCTION 2
529 #define ISFCN(x) (((x) & N_TMASK) == (IMAGE_SYM_DTYPE_FUNCTION << N_BTSHFT))
533 * This is what we are looking for in the COFF symbols.
535 #define IMAGE_SYM_CLASS_EXTERNAL 0x2
536 #define IMAGE_SYM_CLASS_STATIC 0x3
537 #define IMAGE_SYM_CLASS_FILE 0x67
540 struct datatype
* DEBUG_GetCVType(int typeno
)
542 struct datatype
* dt
= NULL
;
545 * Convert Codeview type numbers into something we can grok internally.
546 * Numbers < 0x1000 are all fixed builtin types. Numbers from 0x1000 and
547 * up are all user defined (structs, etc).
549 if( typeno
< 0x1000 )
551 if( typeno
< MAX_BUILTIN_TYPES
)
553 dt
= cv_basic_types
[typeno
];
558 if( typeno
- 0x1000 < num_cv_defined_types
)
560 dt
= cv_defined_types
[typeno
- 0x1000];
568 DEBUG_ParseTypeTable(char * table
, int len
)
572 enum debug_type fieldtype
;
576 struct datatype
* subtype
;
578 union codeview_type
* type
;
579 union codeview_type
* type2
;
580 struct datatype
* typeptr
;
584 ptr
= (union any_size
) (table
+ 16);
585 while( ptr
.c
- table
< len
)
587 type
= (union codeview_type
*) ptr
.c
;
589 if( curr_type
- 0x1000 >= num_cv_defined_types
)
591 num_cv_defined_types
+= 0x100;
592 cv_defined_types
= (struct datatype
**) realloc(cv_defined_types
,
593 num_cv_defined_types
* sizeof(struct datatype
*));
594 memset(cv_defined_types
+ num_cv_defined_types
- 0x100,
596 0x100 * sizeof(struct datatype
*));
597 if( cv_defined_types
== NULL
)
603 switch(type
->generic
.id
)
606 cv_defined_types
[curr_type
- 0x1000] =
607 DEBUG_FindOrMakePointerType(DEBUG_GetCVType(type
->pointer
.datatype
));
610 if( type
->array
.arrlen
>= 0x8000 )
613 * This is a numeric leaf, I am too lazy to handle this right
616 fprintf(stderr
, "Ignoring large numberic leaf.\n");
619 if( type
->array
.namelen
!= 0 )
621 memset(symname
, 0, sizeof(symname
));
622 memcpy(symname
, type
->array
.name
, type
->array
.namelen
);
623 typeptr
= DEBUG_NewDataType(DT_ARRAY
, symname
);
627 typeptr
= DEBUG_NewDataType(DT_ARRAY
, NULL
);
629 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
631 subtype
= DEBUG_GetCVType(type
->array
.elemtype
);
632 if( (subtype
== NULL
)
633 || (elem_size
= DEBUG_GetObjectSize(subtype
)) == 0 )
639 arr_max
= type
->array
.arrlen
/ DEBUG_GetObjectSize(subtype
);
642 DEBUG_SetArrayParams(typeptr
, 0, arr_max
, subtype
);
646 * This is where the basic list of fields is defined for
647 * structures and classes.
649 * First, we need to look ahead and see whether we are building
650 * a fieldlist for an enum or a struct.
653 type2
= (union codeview_type
*) ptr2
.c
;
654 if( type2
->member
.id
== LF_MEMBER
)
656 typeptr
= DEBUG_NewDataType(DT_STRUCT
, NULL
);
657 fieldtype
= DT_STRUCT
;
659 else if( type2
->member
.id
== LF_ENUMERATE
)
661 typeptr
= DEBUG_NewDataType(DT_ENUM
, NULL
);
669 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
670 while( ptr2
.c
< (ptr
.c
+ ((type
->generic
.len
+ 3) & ~3)) )
672 type2
= (union codeview_type
*) ptr2
.c
;
673 if( type2
->member
.id
== LF_MEMBER
&& fieldtype
== DT_STRUCT
)
675 memset(symname
, 0, sizeof(symname
));
676 memcpy(symname
, type2
->member
.name
, type2
->member
.namelen
);
678 subtype
= DEBUG_GetCVType(type2
->member
.type
);
680 if( subtype
!= NULL
)
682 elem_size
= DEBUG_GetObjectSize(subtype
);
685 if( type2
->member
.offset
>= 0x8000 )
688 * This is a numeric leaf, I am too lazy to handle this right
691 fprintf(stderr
, "Ignoring large numberic leaf.\n");
695 DEBUG_AddStructElement(typeptr
, symname
, subtype
,
696 type2
->member
.offset
<< 3,
700 else if( type2
->member
.id
== LF_ENUMERATE
&& fieldtype
== DT_ENUM
)
702 memset(symname
, 0, sizeof(symname
));
703 memcpy(symname
, type2
->enumerate
.name
, type2
->enumerate
.namelen
);
705 if( type2
->enumerate
.value
>= 0x8000 )
708 * This is a numeric leaf, I am too lazy to handle this right
711 fprintf(stderr
, "Ignoring large numberic leaf.\n");
715 DEBUG_AddStructElement(typeptr
, symname
, NULL
,
716 type2
->enumerate
.value
, 0);
722 * Something else I have never seen before. Either wrong type of
723 * object in the fieldlist, or some other problem which I wouldn't
724 * really know how to handle until it came up.
726 fprintf(stderr
, "Unexpected entry in fieldlist\n");
731 ptr2
.c
+= ((type2
->member
.namelen
+ 9 + 3) & ~3);
736 if( type
->structure
.structlen
>= 0x8000 )
739 * This is a numeric leaf, I am too lazy to handle this right
742 fprintf(stderr
, "Ignoring large numberic leaf.\n");
745 memset(symname
, 0, sizeof(symname
));
746 memcpy(symname
, type
->structure
.name
, type
->structure
.namelen
);
747 if( strcmp(symname
, "__unnamed") == 0 )
749 typeptr
= DEBUG_NewDataType(DT_STRUCT
, NULL
);
753 typeptr
= DEBUG_NewDataType(DT_STRUCT
, symname
);
755 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
758 * Now copy the relevant bits from the fieldlist that we specified.
760 subtype
= DEBUG_GetCVType(type
->structure
.fieldlist
);
762 if( subtype
!= NULL
)
764 DEBUG_SetStructSize(typeptr
, type
->structure
.structlen
);
765 DEBUG_CopyFieldlist(typeptr
, subtype
);
769 if( type
->t_union
.un_len
>= 0x8000 )
772 * This is a numeric leaf, I am too lazy to handle this right
775 fprintf(stderr
, "Ignoring large numberic leaf.\n");
778 memset(symname
, 0, sizeof(symname
));
779 memcpy(symname
, type
->t_union
.name
, type
->t_union
.namelen
);
781 if( strcmp(symname
, "__unnamed") == 0 )
783 typeptr
= DEBUG_NewDataType(DT_STRUCT
, NULL
);
787 typeptr
= DEBUG_NewDataType(DT_STRUCT
, symname
);
790 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
793 * Now copy the relevant bits from the fieldlist that we specified.
795 subtype
= DEBUG_GetCVType(type
->t_union
.field
);
797 if( subtype
!= NULL
)
799 DEBUG_SetStructSize(typeptr
, type
->t_union
.un_len
);
800 DEBUG_CopyFieldlist(typeptr
, subtype
);
804 typeptr
= DEBUG_NewDataType(DT_BITFIELD
, NULL
);
805 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
806 DEBUG_SetBitfieldParams(typeptr
, type
->bitfield
.bitoff
,
807 type
->bitfield
.nbits
,
808 DEBUG_GetCVType(type
->bitfield
.type
));
811 memset(symname
, 0, sizeof(symname
));
812 memcpy(symname
, type
->enumeration
.name
, type
->enumeration
.namelen
);
813 typeptr
= DEBUG_NewDataType(DT_ENUM
, symname
);
814 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
817 * Now copy the relevant bits from the fieldlist that we specified.
819 subtype
= DEBUG_GetCVType(type
->enumeration
.field
);
821 if( subtype
!= NULL
)
823 DEBUG_CopyFieldlist(typeptr
, subtype
);
831 ptr
.c
+= (type
->generic
.len
+ 3) & ~3;
838 DEBUG_InitCVDataTypes()
841 * These are the common builtin types that are used by VC++.
843 cv_basic_types
[T_NOTYPE
] = NULL
;
844 cv_basic_types
[T_ABS
] = NULL
;
845 cv_basic_types
[T_VOID
] = DEBUG_NewDataType(DT_BASIC
, "void");
846 cv_basic_types
[T_CHAR
] = DEBUG_NewDataType(DT_BASIC
, "char");
847 cv_basic_types
[T_SHORT
] = DEBUG_NewDataType(DT_BASIC
, "short int");
848 cv_basic_types
[T_LONG
] = DEBUG_NewDataType(DT_BASIC
, "long int");
849 cv_basic_types
[T_QUAD
] = DEBUG_NewDataType(DT_BASIC
, "long long int");
850 cv_basic_types
[T_UCHAR
] = DEBUG_NewDataType(DT_BASIC
, "unsigned char");
851 cv_basic_types
[T_USHORT
] = DEBUG_NewDataType(DT_BASIC
, "short unsigned int");
852 cv_basic_types
[T_ULONG
] = DEBUG_NewDataType(DT_BASIC
, "long unsigned int");
853 cv_basic_types
[T_UQUAD
] = DEBUG_NewDataType(DT_BASIC
, "long long unsigned int");
854 cv_basic_types
[T_REAL32
] = DEBUG_NewDataType(DT_BASIC
, "float");
855 cv_basic_types
[T_REAL64
] = DEBUG_NewDataType(DT_BASIC
, "double");
856 cv_basic_types
[T_RCHAR
] = DEBUG_NewDataType(DT_BASIC
, "char");
857 cv_basic_types
[T_WCHAR
] = DEBUG_NewDataType(DT_BASIC
, "short");
858 cv_basic_types
[T_INT4
] = DEBUG_NewDataType(DT_BASIC
, "int");
859 cv_basic_types
[T_UINT4
] = DEBUG_NewDataType(DT_BASIC
, "unsigned int");
861 cv_basic_types
[T_32PVOID
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_VOID
]);
862 cv_basic_types
[T_32PCHAR
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_CHAR
]);
863 cv_basic_types
[T_32PSHORT
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_SHORT
]);
864 cv_basic_types
[T_32PLONG
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_LONG
]);
865 cv_basic_types
[T_32PQUAD
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_QUAD
]);
866 cv_basic_types
[T_32PUCHAR
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_UCHAR
]);
867 cv_basic_types
[T_32PUSHORT
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_USHORT
]);
868 cv_basic_types
[T_32PULONG
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_ULONG
]);
869 cv_basic_types
[T_32PUQUAD
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_UQUAD
]);
870 cv_basic_types
[T_32PREAL32
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_REAL32
]);
871 cv_basic_types
[T_32PREAL64
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_REAL64
]);
872 cv_basic_types
[T_32PRCHAR
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_RCHAR
]);
873 cv_basic_types
[T_32PWCHAR
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_WCHAR
]);
874 cv_basic_types
[T_32PINT4
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_INT4
]);
875 cv_basic_types
[T_32PUINT4
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_UINT4
]);
879 * In this function, we keep track of deferred debugging information
880 * that we may need later if we were to need to use the internal debugger.
881 * We don't fully process it here for performance reasons.
884 DEBUG_RegisterDebugInfo( HMODULE32 hModule
, const char *module_name
,
885 u_long v_addr
, u_long size
)
887 int has_codeview
= FALSE
;
890 PIMAGE_DEBUG_DIRECTORY dbgptr
;
891 struct deferred_debug_info
* deefer
;
894 dbgptr
= (PIMAGE_DEBUG_DIRECTORY
) (hModule
+ v_addr
);
895 for(; size
>= sizeof(*dbgptr
); size
-= sizeof(*dbgptr
), dbgptr
++ )
899 case IMAGE_DEBUG_TYPE_CODEVIEW
:
900 case IMAGE_DEBUG_TYPE_MISC
:
907 dbgptr
= (PIMAGE_DEBUG_DIRECTORY
) (hModule
+ v_addr
);
908 for(; size
>= sizeof(*dbgptr
); size
-= sizeof(*dbgptr
), dbgptr
++ )
912 case IMAGE_DEBUG_TYPE_COFF
:
914 * If we have both codeview and COFF debug info, ignore the
915 * coff debug info as it would just confuse us, and it is
918 * FIXME - this is broken - if we cannot find the PDB file, then
919 * we end up with no debugging info at all. In this case, we
920 * should use the COFF info as a backup.
926 case IMAGE_DEBUG_TYPE_CODEVIEW
:
927 case IMAGE_DEBUG_TYPE_MISC
:
929 * This is usually an indirection to a .DBG file.
930 * This is similar to (but a slightly older format) from the
933 * First check to see if the image was 'stripped'. If so, it
934 * means that this entry points to a .DBG file. Otherwise,
935 * it just points to itself, and we can ignore this.
937 if( (dbgptr
->Type
== IMAGE_DEBUG_TYPE_MISC
)
938 && (PE_HEADER(hModule
)->FileHeader
.Characteristics
& IMAGE_FILE_DEBUG_STRIPPED
) == 0 )
943 deefer
= (struct deferred_debug_info
*) xmalloc(sizeof(*deefer
));
944 deefer
->module
= hModule
;
945 deefer
->load_addr
= (char *)hModule
;
946 deefer
->dbg_info
= NULL
;
947 deefer
->dbg_size
= 0;
950 * Read the important bits. What we do after this depends
951 * upon the type, but this is always enough so we are able
952 * to proceed if we know what we need to do next.
954 deefer
->dbg_size
= dbgptr
->SizeOfData
;
955 deefer
->dbg_info
= (char *)(hModule
+ dbgptr
->PointerToRawData
);
956 deefer
->dbgdir
= dbgptr
;
957 deefer
->next
= dbglist
;
958 deefer
->loaded
= FALSE
;
959 deefer
->dbg_index
= DEBUG_next_index
;
960 deefer
->module_name
= xstrdup(module_name
);
962 deefer
->sectp
= PE_SECTIONS(hModule
);
963 deefer
->nsect
= PE_HEADER(hModule
)->FileHeader
.NumberOfSections
;
978 * ELF modules are also entered into the list - this is so that we
979 * can make 'info shared' types of displays possible.
982 DEBUG_RegisterELFDebugInfo(int load_addr
, u_long size
, char * name
)
984 struct deferred_debug_info
* deefer
;
986 deefer
= (struct deferred_debug_info
*) xmalloc(sizeof(*deefer
));
990 * Read the important bits. What we do after this depends
991 * upon the type, but this is always enough so we are able
992 * to proceed if we know what we need to do next.
994 deefer
->dbg_size
= size
;
995 deefer
->dbg_info
= (char *) NULL
;
997 deefer
->load_addr
= (char *) load_addr
;
998 deefer
->dbgdir
= NULL
;
999 deefer
->next
= dbglist
;
1000 deefer
->loaded
= TRUE
;
1001 deefer
->dbg_index
= DEBUG_next_index
;
1002 deefer
->module_name
= xstrdup(name
);
1013 * Process COFF debugging information embedded in a Win32 application.
1018 DEBUG_ProcessCoff(struct deferred_debug_info
* deefer
)
1020 struct CoffAuxSection
* aux
;
1021 struct CoffDebug
* coff
;
1022 struct CoffFiles
* coff_files
= NULL
;
1023 struct CoffLinenum
* coff_linetab
;
1025 struct CoffSymbol
* coff_sym
;
1026 struct CoffSymbol
* coff_symbol
;
1027 struct CoffFiles
* curr_file
= NULL
;
1031 struct CoffLinenum
* linepnt
;
1038 int nfiles_alloc
= 0;
1039 struct CoffFiles orig_file
;
1041 char * this_file
= NULL
;
1043 coff
= (struct CoffDebug
*) deefer
->dbg_info
;
1045 coff_symbol
= (struct CoffSymbol
*) ((unsigned int) coff
+ coff
->SymbolOffset
);
1046 coff_linetab
= (struct CoffLinenum
*) ((unsigned int) coff
+ coff
->LinenumberOffset
);
1047 coff_strtab
= (char *) ((unsigned int) coff_symbol
+ 18*coff
->N_Sym
);
1051 for(i
=0; i
< coff
->N_Sym
; i
++ )
1054 * We do this because some compilers (i.e. gcc) incorrectly
1055 * pad the structure up to a 4 byte boundary. The structure
1056 * is really only 18 bytes long, so we have to manually make sure
1059 * FIXME - there must be a way to have autoconf figure out the
1060 * correct compiler option for this. If it is always gcc, that
1061 * makes life simpler, but I don't want to force this.
1063 coff_sym
= (struct CoffSymbol
*) ((unsigned int) coff_symbol
+ 18*i
);
1064 naux
= coff_sym
->NumberOfAuxSymbols
;
1066 if( coff_sym
->StorageClass
== IMAGE_SYM_CLASS_FILE
)
1068 if( nfiles
+ 1 >= nfiles_alloc
)
1071 coff_files
= (struct CoffFiles
*) realloc( coff_files
,
1072 nfiles_alloc
* sizeof(struct CoffFiles
));
1074 curr_file
= coff_files
+ nfiles
;
1076 curr_file
->startaddr
= 0xffffffff;
1077 curr_file
->endaddr
= 0;
1078 curr_file
->filename
= ((char *) coff_sym
) + 18;
1079 curr_file
->linetab_offset
= -1;
1080 curr_file
->linecnt
= 0;
1081 curr_file
->entries
= NULL
;
1082 curr_file
->neps
= curr_file
->neps_alloc
= 0;
1084 fprintf(stderr
,"New file %s\n", curr_file
->filename
);
1091 * This guy marks the size and location of the text section
1092 * for the current file. We need to keep track of this so
1093 * we can figure out what file the different global functions
1096 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_STATIC
)
1098 && (coff_sym
->Type
== 0)
1099 && (coff_sym
->SectionNumber
== 1) )
1101 aux
= (struct CoffAuxSection
*) ((unsigned int) coff_sym
+ 18);
1103 if( curr_file
->linetab_offset
!= -1 )
1106 fprintf(stderr
, "Duplicating sect from %s: %x %x %x %d %d\n",
1107 curr_file
->filename
,
1109 aux
->NumberOfRelocations
,
1110 aux
->NumberOfLinenumbers
,
1113 fprintf(stderr
, "More sect %d %x %d %d %d\n",
1114 coff_sym
->SectionNumber
,
1117 coff_sym
->StorageClass
,
1118 coff_sym
->NumberOfAuxSymbols
);
1122 * Save this so we can copy bits from it.
1124 orig_file
= *curr_file
;
1127 * Duplicate the file entry. We have no way to describe
1128 * multiple text sections in our current way of handling things.
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
= orig_file
.filename
;
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;
1149 fprintf(stderr
, "New text sect from %s: %x %x %x %d %d\n",
1150 curr_file
->filename
,
1152 aux
->NumberOfRelocations
,
1153 aux
->NumberOfLinenumbers
,
1159 if( curr_file
->startaddr
> coff_sym
->Value
)
1161 curr_file
->startaddr
= coff_sym
->Value
;
1164 if( curr_file
->startaddr
> coff_sym
->Value
)
1166 curr_file
->startaddr
= coff_sym
->Value
;
1169 if( curr_file
->endaddr
< coff_sym
->Value
+ aux
->Length
)
1171 curr_file
->endaddr
= coff_sym
->Value
+ aux
->Length
;
1174 curr_file
->linetab_offset
= linetab_indx
;
1175 curr_file
->linecnt
= aux
->NumberOfLinenumbers
;
1176 linetab_indx
+= aux
->NumberOfLinenumbers
;
1181 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_STATIC
)
1183 && (coff_sym
->SectionNumber
== 1) )
1186 * This is a normal static function when naux == 0.
1187 * Just register it. The current file is the correct
1188 * one in this instance.
1190 if( coff_sym
->N
.Name
.NotLong
)
1192 memcpy(namebuff
, coff_sym
->N
.ShortName
, 8);
1194 nampnt
= &namebuff
[0];
1198 nampnt
= coff_strtab
+ coff_sym
->N
.Name
.StrTaboff
;
1201 if( nampnt
[0] == '_' )
1207 new_addr
.off
= (int) (deefer
->load_addr
+ coff_sym
->Value
);
1209 if( curr_file
->neps
+ 1 >= curr_file
->neps_alloc
)
1211 curr_file
->neps_alloc
+= 10;
1212 curr_file
->entries
= (struct name_hash
**)
1213 realloc( curr_file
->entries
,
1214 curr_file
->neps_alloc
* sizeof(struct name_hash
*));
1217 fprintf(stderr
,"\tAdding static symbol %s\n", nampnt
);
1219 curr_file
->entries
[curr_file
->neps
++] =
1220 DEBUG_AddSymbol( nampnt
, &new_addr
, this_file
, SYM_WIN32
);
1225 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_EXTERNAL
)
1226 && ISFCN(coff_sym
->Type
)
1227 && (coff_sym
->SectionNumber
> 0) )
1229 if( coff_sym
->N
.Name
.NotLong
)
1231 memcpy(namebuff
, coff_sym
->N
.ShortName
, 8);
1233 nampnt
= &namebuff
[0];
1237 nampnt
= coff_strtab
+ coff_sym
->N
.Name
.StrTaboff
;
1241 if( nampnt
[0] == '_' )
1247 new_addr
.off
= (int) (deefer
->load_addr
+ coff_sym
->Value
);
1250 fprintf(stderr
, "%d: %x %s\n", i
, new_addr
.off
, nampnt
);
1252 fprintf(stderr
,"\tAdding global symbol %s\n", nampnt
);
1256 * Now we need to figure out which file this guy belongs to.
1259 for(j
=0; j
< nfiles
; j
++)
1261 if( coff_files
[j
].startaddr
<= coff_sym
->Value
1262 && coff_files
[j
].endaddr
> coff_sym
->Value
)
1264 this_file
= coff_files
[j
].filename
;
1268 if( coff_files
[j
].neps
+ 1 >= coff_files
[j
].neps_alloc
)
1270 coff_files
[j
].neps_alloc
+= 10;
1271 coff_files
[j
].entries
= (struct name_hash
**)
1272 realloc( coff_files
[j
].entries
,
1273 coff_files
[j
].neps_alloc
* sizeof(struct name_hash
*));
1275 coff_files
[j
].entries
[coff_files
[j
].neps
++] =
1276 DEBUG_AddSymbol( nampnt
, &new_addr
, this_file
, SYM_WIN32
);
1281 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_EXTERNAL
)
1282 && (coff_sym
->SectionNumber
> 0) )
1285 * Similar to above, but for the case of data symbols.
1286 * These aren't treated as entrypoints.
1288 if( coff_sym
->N
.Name
.NotLong
)
1290 memcpy(namebuff
, coff_sym
->N
.ShortName
, 8);
1292 nampnt
= &namebuff
[0];
1296 nampnt
= coff_strtab
+ coff_sym
->N
.Name
.StrTaboff
;
1300 if( nampnt
[0] == '_' )
1306 new_addr
.off
= (int) (deefer
->load_addr
+ coff_sym
->Value
);
1309 fprintf(stderr
, "%d: %x %s\n", i
, new_addr
.off
, nampnt
);
1311 fprintf(stderr
,"\tAdding global data symbol %s\n", nampnt
);
1315 * Now we need to figure out which file this guy belongs to.
1317 DEBUG_AddSymbol( nampnt
, &new_addr
, NULL
, SYM_WIN32
);
1322 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_STATIC
)
1326 * Ignore these. They don't have anything to do with
1334 fprintf(stderr
,"Skipping unknown entry %d %d %d\n", coff_sym
->StorageClass
,
1335 coff_sym
->SectionNumber
, naux
);
1339 * For now, skip past the aux entries.
1346 * OK, we now should have a list of files, and we should have a list
1347 * of entrypoints. We need to sort the entrypoints so that we are
1348 * able to tie the line numbers with the given functions within the
1351 if( coff_files
!= NULL
)
1353 for(j
=0; j
< nfiles
; j
++)
1355 if( coff_files
[j
].entries
!= NULL
)
1357 qsort(coff_files
[j
].entries
, coff_files
[j
].neps
,
1358 sizeof(struct name_hash
*), DEBUG_cmp_sym
);
1363 * Now pick apart the line number tables, and attach the entries
1364 * to the given functions.
1366 for(j
=0; j
< nfiles
; j
++)
1369 for(k
=0; k
< coff_files
[j
].linecnt
; k
++)
1372 * Another monstrosity caused by the fact that we are using
1373 * a 6 byte structure, and gcc wants to pad structures to 4 byte
1374 * boundaries. Otherwise we could just index into an array.
1376 linepnt
= (struct CoffLinenum
*)
1377 ((unsigned int) coff_linetab
+
1378 6*(coff_files
[j
].linetab_offset
+ k
));
1380 * If we have spilled onto the next entrypoint, then
1381 * bump the counter..
1385 DEBUG_GetSymbolAddr(coff_files
[j
].entries
[i
+1], &new_addr
);
1386 if( (i
+1 < coff_files
[j
].neps
)
1387 && ( ((unsigned int) deefer
->load_addr
+ linepnt
->VirtualAddr
)
1399 * Add the line number. This is always relative to the
1400 * start of the function, so we need to subtract that offset
1403 DEBUG_GetSymbolAddr(coff_files
[j
].entries
[i
], &new_addr
);
1404 DEBUG_AddLineNumber(coff_files
[j
].entries
[i
],
1406 (unsigned int) deefer
->load_addr
1407 + linepnt
->VirtualAddr
1415 if( coff_files
!= NULL
)
1417 for(j
=0; j
< nfiles
; j
++)
1419 if( coff_files
[j
].entries
!= NULL
)
1421 free(coff_files
[j
].entries
);
1432 * Process a codeview line number table. Digestify the thing so that
1433 * we can easily reference the thing when we process the rest of
1436 static struct codeview_linetab_hdr
*
1437 DEBUG_SnarfLinetab(char * linetab
,
1441 char filename
[PATH_MAX
];
1442 unsigned int * filetab
;
1446 struct codeview_linetab_hdr
* lt_hdr
;
1447 unsigned int * lt_ptr
;
1451 union any_size pnt2
;
1452 struct startend
* start
;
1456 * Now get the important bits.
1458 pnt
= (union any_size
) linetab
;
1462 filetab
= (unsigned int *) pnt
.c
;
1465 * Now count up the number of segments in the file.
1468 for(i
=0; i
<nfile
; i
++)
1470 pnt2
= (union any_size
) (linetab
+ filetab
[i
]);
1475 * Next allocate the header we will be returning.
1476 * There is one header for each segment, so that we can reach in
1477 * and pull bits as required.
1479 lt_hdr
= (struct codeview_linetab_hdr
*)
1480 xmalloc((nseg
+ 1) * sizeof(*lt_hdr
));
1481 if( lt_hdr
== NULL
)
1486 memset(lt_hdr
, 0, sizeof(*lt_hdr
) * (nseg
+1));
1489 * Now fill the header we will be returning, one for each segment.
1490 * Note that this will basically just contain pointers into the existing
1491 * line table, and we do not actually copy any additional information
1492 * or allocate any additional memory.
1496 for(i
=0; i
<nfile
; i
++)
1499 * Get the pointer into the segment information.
1501 pnt2
= (union any_size
) (linetab
+ filetab
[i
]);
1502 file_segcount
= *pnt2
.s
;
1505 lt_ptr
= (unsigned int *) pnt2
.c
;
1506 start
= (struct startend
*) (lt_ptr
+ file_segcount
);
1509 * Now snarf the filename for all of the segments for this file.
1511 fn
= (unsigned char *) (start
+ file_segcount
);
1512 memset(filename
, 0, sizeof(filename
));
1513 memcpy(filename
, fn
+ 1, *fn
);
1514 fn
= strdup(filename
);
1516 for(k
= 0; k
< file_segcount
; k
++, this_seg
++)
1518 pnt2
= (union any_size
) (linetab
+ lt_ptr
[k
]);
1519 lt_hdr
[this_seg
].start
= start
[k
].start
;
1520 lt_hdr
[this_seg
].end
= start
[k
].end
;
1521 lt_hdr
[this_seg
].sourcefile
= fn
;
1522 lt_hdr
[this_seg
].segno
= *pnt2
.s
++;
1523 lt_hdr
[this_seg
].nline
= *pnt2
.s
++;
1524 lt_hdr
[this_seg
].offtab
= pnt2
.ui
;
1525 lt_hdr
[this_seg
].linetab
= (unsigned short *)
1526 (pnt2
.ui
+ lt_hdr
[this_seg
].nline
);
1537 DEBUG_SnarfCodeView( struct deferred_debug_info
* deefer
,
1540 struct codeview_linetab_hdr
* linetab
)
1542 struct name_hash
* curr_func
= NULL
;
1543 struct wine_locals
* curr_sym
= NULL
;
1550 IMAGE_SECTION_HEADER
* sectp
;
1551 union codeview_symbol
* sym
;
1552 char symname
[PATH_MAX
];
1553 struct name_hash
* thunk_sym
= NULL
;
1555 ptr
= (union any_size
) cv_data
;
1556 nsect
= deefer
->nsect
;
1557 sectp
= deefer
->sectp
;
1560 * Skip over the first word. Don't really know what it means, but
1566 * Loop over the different types of records and whenever we
1567 * find something we are interested in, record it and move on.
1569 while( ptr
.c
- cv_data
< size
)
1571 sym
= (union codeview_symbol
*) ptr
.c
;
1573 if( sym
->generic
.len
- sizeof(int) == (ptr
.c
- cv_data
) )
1576 * This happens when we have indirect symbols that VC++ 4.2
1577 * sometimes uses when there isn't a line number table.
1578 * We ignore it - we will process and enter all of the
1579 * symbols in the global symbol table anyways, so there
1580 * isn't much point in keeping track of all of this crap.
1585 memset(symname
, 0, sizeof(symname
));
1586 switch(sym
->generic
.id
)
1592 * First, a couple of sanity checks.
1594 if( sym
->data
.namelen
== 0 )
1599 if( sym
->data
.seg
== 0 || sym
->data
.seg
> nsect
)
1605 * Global and local data symbols. We don't associate these
1606 * with any given source file.
1609 memcpy(symname
, sym
->data
.name
, sym
->data
.namelen
);
1611 new_addr
.type
= DEBUG_GetCVType(sym
->data
.symtype
);
1612 new_addr
.off
= (unsigned int) deefer
->load_addr
+
1613 sectp
[sym
->data
.seg
- 1].VirtualAddress
+
1615 DEBUG_AddSymbol( symname
, &new_addr
, NULL
, SYM_WIN32
| SYM_DATA
);
1619 * Sort of like a global function, but it just points
1620 * to a thunk, which is a stupid name for what amounts to
1621 * a PLT slot in the normal jargon that everyone else uses.
1623 memcpy(symname
, sym
->thunk
.name
, sym
->thunk
.namelen
);
1625 new_addr
.type
= NULL
;
1626 new_addr
.off
= (unsigned int) deefer
->load_addr
+
1627 sectp
[sym
->thunk
.segment
- 1].VirtualAddress
+
1629 thunk_sym
= DEBUG_AddSymbol( symname
, &new_addr
, NULL
,
1630 SYM_WIN32
| SYM_FUNC
);
1631 DEBUG_SetSymbolSize(thunk_sym
, sym
->thunk
.thunk_len
);
1636 * Global and static functions.
1638 memcpy(symname
, sym
->proc
.name
, sym
->proc
.namelen
);
1640 new_addr
.type
= DEBUG_GetCVType(sym
->proc
.proctype
);
1641 new_addr
.off
= (unsigned int) deefer
->load_addr
+
1642 sectp
[sym
->proc
.segment
- 1].VirtualAddress
+
1645 * See if we can find a segment that this goes with. If so,
1646 * it means that we also may have line number information
1647 * for this function.
1649 for(i
=0; linetab
[i
].linetab
!= NULL
; i
++)
1651 if( ((unsigned int) deefer
->load_addr
1652 + sectp
[linetab
[i
].segno
- 1].VirtualAddress
1653 + linetab
[i
].start
<= new_addr
.off
)
1654 && ((unsigned int) deefer
->load_addr
1655 + sectp
[linetab
[i
].segno
- 1].VirtualAddress
1656 + linetab
[i
].end
> new_addr
.off
) )
1662 DEBUG_Normalize(curr_func
);
1663 if( linetab
[i
].linetab
== NULL
)
1665 curr_func
= DEBUG_AddSymbol( symname
, &new_addr
, NULL
,
1666 SYM_WIN32
| SYM_FUNC
);
1671 * First, create the entry. Then dig through the linetab
1672 * and add whatever line numbers are appropriate for this
1675 curr_func
= DEBUG_AddSymbol( symname
, &new_addr
,
1676 linetab
[i
].sourcefile
,
1677 SYM_WIN32
| SYM_FUNC
);
1678 for(j
=0; j
< linetab
[i
].nline
; j
++)
1680 if( linetab
[i
].offtab
[j
] >= sym
->proc
.offset
1681 && linetab
[i
].offtab
[j
] < sym
->proc
.offset
1682 + sym
->proc
.proc_len
)
1684 DEBUG_AddLineNumber(curr_func
, linetab
[i
].linetab
[j
],
1685 linetab
[i
].offtab
[j
] - sym
->proc
.offset
);
1692 * Add information about where we should set breakpoints
1695 DEBUG_SetSymbolBPOff(curr_func
, sym
->proc
.debug_start
);
1696 DEBUG_SetSymbolSize(curr_func
, sym
->proc
.proc_len
);
1700 * Function parameters and stack variables.
1702 memcpy(symname
, sym
->stack
.name
, sym
->stack
.namelen
);
1703 curr_sym
= DEBUG_AddLocal(curr_func
,
1709 DEBUG_SetLocalSymbolType(curr_sym
, DEBUG_GetCVType(sym
->stack
.symtype
));
1717 * Adjust pointer to point to next entry, rounding up to a word
1718 * boundary. MS preserving alignment? Stranger things have
1721 if( sym
->generic
.id
== S_PROCREF
1722 || sym
->generic
.id
== S_DATAREF
1723 || sym
->generic
.id
== S_UNKNOWN
)
1725 len
= (sym
->generic
.len
+ 3) & ~3;
1726 len
+= ptr
.c
[16] + 1;
1727 ptr
.c
+= (len
+ 3) & ~3;
1731 ptr
.c
+= (sym
->generic
.len
+ 3) & ~3;
1735 if( linetab
!= NULL
)
1745 * Process PDB file which contains debug information.
1747 * These are really weird beasts. They are intended to be incrementally
1748 * updated by the incremental linker, and this means that you need to
1749 * be able to remove and add information. Thus the PDB file is sort of
1750 * like a block structured device, with a freelist and lists of extent numbers
1751 * that are used to get the relevant pieces. In all cases seen so far, the
1752 * blocksize is always 0x400 bytes. The header has a field which apparently
1753 * holds the blocksize, so if it ever changes we are safe.
1755 * In general, every time we need to extract something from the pdb file,
1756 * it is easier to copy it into another buffer so we have the information
1757 * in one contiguous block rather than attempt to try and keep track of when
1758 * we need to grab another extent from the pdb file.
1760 * The thing that is a real pain about some MS stuff is that they choose
1761 * data structures which are not representable in C. Thus we have to
1762 * hack around and diddle pointers.
1766 DEBUG_ProcessPDBFile(struct deferred_debug_info
* deefer
, char * full_filename
)
1768 char * addr
= (char *) 0xffffffff;
1769 unsigned int blocksize
;
1770 unsigned int bufflen
= 0;
1771 char * buffer
= NULL
;
1772 unsigned short * extent_table
;
1774 struct file_ent
* fent
;
1776 struct file_list
* filelist
= NULL
;
1777 unsigned int gsym_record
= 0;
1778 char * gsymtab
= NULL
;
1779 struct filetab_hdr
* hd
;
1782 unsigned int last_extent
;
1783 struct codeview_linetab_hdr
* linetab
;
1784 unsigned int nblocks
;
1786 unsigned int offset
;
1787 struct codeview_pdb_hdr
* pdbhdr
;
1789 struct stat statbuf
;
1791 unsigned short * table
;
1793 unsigned int toc_blocks
;
1796 * FIXME - we should use some kind of search path mechanism to locate
1797 * PDB files. Right now we just look in the current working directory,
1798 * which works much of the time, I guess. Ideally we should be able to
1799 * map the filename back using the settings in wine.ini and perhaps
1800 * we could find it there. This bit of coding is left as an exercise
1801 * for the reader. :-).
1803 filename
= strrchr(full_filename
, '\\');
1804 if( filename
== NULL
)
1806 filename
= full_filename
;
1813 status
= stat(filename
, &statbuf
);
1816 fprintf(stderr
, "Unable to open .PDB file %s\n", filename
);
1821 * Now open the file, so that we can mmap() it.
1823 fd
= open(filename
, O_RDONLY
);
1826 fprintf(stderr
, "Unable to open .DBG file %s\n", filename
);
1832 * Now mmap() the file.
1834 addr
= mmap(0, statbuf
.st_size
, PROT_READ
,
1835 MAP_PRIVATE
, fd
, 0);
1836 if( addr
== (char *) 0xffffffff )
1838 fprintf(stderr
, "Unable to mmap .DBG file %s\n", filename
);
1843 * Now that we have the formalities over and done with, we need
1844 * to find the table of contents for the PDB file.
1846 pdbhdr
= (struct codeview_pdb_hdr
*) addr
;
1847 blocksize
= pdbhdr
->blocksize
;
1848 last_extent
= (statbuf
.st_size
+ blocksize
- 1) / blocksize
;
1851 * The TOC itself isn't always contiguous, so we need to extract a few
1852 * extents from the file to form the TOC.
1854 toc_blocks
= (pdbhdr
->toc_len
+ blocksize
- 1) / blocksize
;
1855 toc
= (char *) xmalloc(toc_blocks
* blocksize
);
1856 table
= pdbhdr
->toc_ext
;
1857 for(i
=0; i
< toc_blocks
; i
++)
1859 memcpy(toc
+ blocksize
*i
, addr
+ table
[i
]*blocksize
, blocksize
);
1863 * Next build our own table which will have the size and extent block
1864 * list for each record in the PDB file.
1866 * The TOC starts out with the number of files. Then it is followed by
1867 * (npair * 2*sizeof(int)) bytes of information, which are pairs of ints.
1868 * The first one is the size of the record (in bytes), and the second one
1869 * is something else which I haven't figured out yet.
1871 pnt
= (unsigned int *) toc
;
1873 extent_table
= (unsigned short *) ((unsigned int) toc
+
1874 npair
* 2 * sizeof(int) + sizeof(int));
1879 if( sizeof(int) + 2*sizeof(int)*npair
> pdbhdr
->toc_len
)
1884 filelist
= (struct file_list
*) xmalloc(npair
* sizeof(*filelist
));
1885 if( filelist
== NULL
)
1889 memset(filelist
, 0, npair
* sizeof(*filelist
));
1892 for(i
=0; i
< npair
; i
++)
1894 filelist
[i
].record_len
= pnt
[i
*2];
1895 filelist
[i
].nextents
= (filelist
[i
].record_len
+ blocksize
- 1)
1897 filelist
[i
].extent_list
= extent_table
+ nblocks
;
1898 nblocks
+= filelist
[i
].nextents
;
1901 * These get filled in later when we parse one of the records.
1903 filelist
[i
].linetab_offset
= 0;
1904 filelist
[i
].linetab_len
= 0;
1908 * OK, now walk through the various records and pick out the bits we
1909 * really want to see. Some of the records are extra special, and
1910 * we need to handle these a little bit differently.
1912 for(i
=0; i
< npair
; i
++)
1914 if( filelist
[i
].record_len
== 0xffffffff )
1920 * Make sure our buffer is large enough to hold the record.
1922 if( bufflen
< filelist
[i
].nextents
* blocksize
)
1924 bufflen
= filelist
[i
].nextents
* blocksize
;
1925 buffer
= (char *) realloc(buffer
, bufflen
);
1929 * Do this just for completeness. It makes debugging easier
1930 * if we have a clean indication of where the record ends.
1932 memset(buffer
, 0, filelist
[i
].nextents
* blocksize
);
1935 * Next, build the record using the extent list.
1937 for(j
=0; j
< filelist
[i
].nextents
; j
++)
1939 memcpy(buffer
+ j
* blocksize
,
1940 addr
+ filelist
[i
].extent_list
[j
] * blocksize
,
1944 pnt
= (unsigned int *) buffer
;
1947 * OK, now figure out what to do with it.
1951 * Always ignore the first entry. It seems to contain a backup copy
1952 * of the TOC (the last time the file was modified??)
1960 * The second entry as a id block. It contains a magic number
1961 * to identify the compiler, plus it also contains the timestamp
1962 * which must match the timestamp in the executable.
1967 if( ((*pnt
!= 19950623) && (*pnt
!= 19950814))
1968 || (filelist
[i
].record_len
!= 0x24)
1969 || (pnt
[1] != ((struct CodeViewDebug
*)(deefer
->dbg_info
))->cv_timestamp
) )
1976 * The third entry contains pointers to the global symbol table,
1977 * plus it also contains additional information about each record
1982 hd
= (struct filetab_hdr
*) buffer
;
1984 gsym_record
= hd
->gsym_file
;
1985 gsymtab
= (char *) xmalloc( filelist
[gsym_record
].nextents
1987 memset(gsymtab
, 0, filelist
[gsym_record
].nextents
* blocksize
);
1989 for(j
=0; j
< filelist
[gsym_record
].nextents
; j
++)
1991 memcpy(gsymtab
+ j
* blocksize
,
1992 addr
+ filelist
[gsym_record
].extent_list
[j
] * blocksize
,
1997 * This record also contains information about where in the
1998 * remaining records we will be able to find the start of the
1999 * line number table. We could locate that bit using heuristics,
2000 * but since we have the info handy, we might as well use it.
2002 offset
= sizeof(*hd
);
2005 fent
= (struct file_ent
*) (buffer
+ offset
);
2006 if( offset
> hd
->ftab_len
)
2011 if( fent
->file_number
== 0 || fent
->file_number
>= npair
)
2016 filelist
[fent
->file_number
].linetab_offset
=
2017 fent
->linetab_offset
;
2018 filelist
[fent
->file_number
].linetab_len
=
2021 * Figure out the offset of the next entry.
2022 * There is a fixed part of the record and a variable
2023 * length filename which we must also skip past.
2025 offset
+= ((unsigned int) &fent
->filename
- (unsigned int) fent
)
2026 + strlen(fent
->filename
) + 1;
2027 offset
+= strlen(buffer
+offset
) + 1;
2028 offset
= (offset
+ 3) & ~3;
2034 * Two different magic numbers used as dates.
2035 * These indicate the 'type' table.
2037 if( *pnt
== 19950410
2038 || *pnt
== 19951122 )
2040 DEBUG_ParseTypeTable(buffer
, filelist
[i
].record_len
);
2045 * This is something we really want to look at, since it contains
2046 * real debug info. Anything that doesn't match this can be
2052 * First, snag the line table, if we have one. This always
2053 * occurs at the end of the record, so we take the linetab
2054 * offset as the end of the normal part of the record.
2057 if( filelist
[i
].linetab_len
!= 0 )
2059 linetab
= DEBUG_SnarfLinetab(buffer
+ filelist
[i
].linetab_offset
,
2060 filelist
[i
].linetab_len
);
2061 DEBUG_SnarfCodeView(deefer
, buffer
,
2062 filelist
[i
].linetab_offset
,
2067 DEBUG_SnarfCodeView(deefer
, buffer
,
2068 filelist
[i
].record_len
,
2076 * Finally, process the global symbol table itself. There isn't
2077 * a line number component to this, so we just toss everything
2078 * into the mix and it all should work out.
2080 if( gsym_record
!= 0 )
2082 DEBUG_SnarfCodeView(deefer
, gsymtab
- sizeof(int),
2083 filelist
[gsym_record
].record_len
,
2089 if( gsymtab
!= NULL
)
2095 if( buffer
!= NULL
)
2100 if( filelist
!= NULL
)
2105 if( addr
!= (char *) 0xffffffff )
2107 munmap(addr
, statbuf
.st_size
);
2119 * Process DBG file which contains debug information.
2123 DEBUG_ProcessDBGFile(struct deferred_debug_info
* deefer
, char * filename
)
2125 char * addr
= (char *) 0xffffffff;
2127 struct CV4_DirHead
* codeview_dir
;
2128 struct CV4_DirEnt
* codeview_dent
;
2129 PIMAGE_DEBUG_DIRECTORY dbghdr
;
2130 struct deferred_debug_info deefer2
;
2134 struct codeview_linetab_hdr
* linetab
;
2136 PIMAGE_SEPARATE_DEBUG_HEADER pdbg
= NULL
;
2137 IMAGE_SECTION_HEADER
* sectp
;
2138 struct stat statbuf
;
2141 status
= stat(filename
, &statbuf
);
2144 fprintf(stderr
, "Unable to open .DBG file %s\n", filename
);
2149 * Now open the file, so that we can mmap() it.
2151 fd
= open(filename
, O_RDONLY
);
2154 fprintf(stderr
, "Unable to open .DBG file %s\n", filename
);
2160 * Now mmap() the file.
2162 addr
= mmap(0, statbuf
.st_size
, PROT_READ
,
2163 MAP_PRIVATE
, fd
, 0);
2164 if( addr
== (char *) 0xffffffff )
2166 fprintf(stderr
, "Unable to mmap .DBG file %s\n", filename
);
2170 pdbg
= (PIMAGE_SEPARATE_DEBUG_HEADER
) addr
;
2172 if( pdbg
->TimeDateStamp
!= deefer
->dbgdir
->TimeDateStamp
)
2174 fprintf(stderr
, "Warning - %s has incorrect internal timestamp\n",
2179 fprintf(stderr
, "Processing symbols from %s...\n", filename
);
2181 dbghdr
= (PIMAGE_DEBUG_DIRECTORY
) ( addr
+ sizeof(*pdbg
)
2182 + pdbg
->NumberOfSections
* sizeof(IMAGE_SECTION_HEADER
)
2183 + pdbg
->ExportedNamesSize
);
2185 sectp
= (PIMAGE_SECTION_HEADER
) ((char *) pdbg
+ sizeof(*pdbg
));
2186 nsect
= pdbg
->NumberOfSections
;
2188 for( i
=0; i
< pdbg
->DebugDirectorySize
/ sizeof(*pdbg
); i
++, dbghdr
++ )
2190 switch(dbghdr
->Type
)
2192 case IMAGE_DEBUG_TYPE_COFF
:
2194 * Dummy up a deferred debug header to handle the
2195 * COFF stuff embedded within the DBG file.
2197 memset((char *) &deefer2
, 0, sizeof(deefer2
));
2198 deefer2
.dbg_info
= (addr
+ dbghdr
->PointerToRawData
);
2199 deefer2
.dbg_size
= dbghdr
->SizeOfData
;
2200 deefer2
.load_addr
= deefer
->load_addr
;
2202 DEBUG_ProcessCoff(&deefer2
);
2204 case IMAGE_DEBUG_TYPE_CODEVIEW
:
2206 * This is the older format by which codeview stuff is
2207 * stored, known as the 'NB09' format. Newer executables
2208 * and dlls created by VC++ use PDB files instead, which
2209 * have lots of internal similarities, but the overall
2210 * format and structure is quite different.
2212 codeview
= (addr
+ dbghdr
->PointerToRawData
);
2215 * The first thing in the codeview section should be
2216 * an 'NB09' identifier. As a sanity check, make sure
2219 if( *((unsigned int*) codeview
) != 0x3930424e )
2225 * Next we need to find the directory. This is easy too.
2227 codeview_dir
= (struct CV4_DirHead
*)
2228 (codeview
+ ((unsigned int*) codeview
)[1]);
2231 * Some more sanity checks. Make sure that everything
2232 * is as we expect it.
2234 if( codeview_dir
->next_offset
!= 0
2235 || codeview_dir
->dhsize
!= sizeof(*codeview_dir
)
2236 || codeview_dir
->desize
!= sizeof(*codeview_dent
) )
2240 codeview_dent
= (struct CV4_DirEnt
*) (codeview_dir
+ 1);
2242 for(j
=0; j
< codeview_dir
->ndir
; j
++, codeview_dent
++)
2244 if( codeview_dent
->subsect_number
== sstAlignSym
)
2247 * Check the previous entry. If it is a
2248 * sstSrcModule, it contains the line number
2249 * info for this file.
2252 if( codeview_dent
[1].module_number
== codeview_dent
[0].module_number
2253 && codeview_dent
[1].subsect_number
== sstSrcModule
)
2255 linetab
= DEBUG_SnarfLinetab(
2256 codeview
+ codeview_dent
[1].offset
,
2257 codeview_dent
[1].size
);
2260 if( codeview_dent
[-1].module_number
== codeview_dent
[0].module_number
2261 && codeview_dent
[-1].subsect_number
== sstSrcModule
)
2263 linetab
= DEBUG_SnarfLinetab(
2264 codeview
+ codeview_dent
[-1].offset
,
2265 codeview_dent
[-1].size
);
2268 * Now process the CV stuff.
2270 DEBUG_SnarfCodeView(deefer
,
2271 codeview
+ codeview_dent
->offset
,
2272 codeview_dent
->size
,
2284 if( addr
!= (char *) 0xffffffff )
2286 munmap(addr
, statbuf
.st_size
);
2298 DEBUG_ProcessDeferredDebug()
2300 struct deferred_debug_info
* deefer
;
2301 struct CodeViewDebug
* cvd
;
2302 struct MiscDebug
* misc
;
2307 DEBUG_InitCVDataTypes();
2309 for(deefer
= dbglist
; deefer
; deefer
= deefer
->next
)
2311 if( deefer
->loaded
)
2316 if( last_proc
!= deefer
->dbg_index
)
2320 fprintf(stderr
, "DeferredDebug for:");
2323 fprintf(stderr
, " %s",deefer
->module_name
);
2324 last_proc
= deefer
->dbg_index
;
2327 switch(deefer
->dbgdir
->Type
)
2329 case IMAGE_DEBUG_TYPE_COFF
:
2331 * Standard COFF debug information that VC++ adds when you
2332 * use /debugtype:both with the linker.
2335 fprintf(stderr
, "Processing COFF symbols...\n");
2337 DEBUG_ProcessCoff(deefer
);
2339 case IMAGE_DEBUG_TYPE_CODEVIEW
:
2341 * This is a pointer to a PDB file of some sort.
2343 cvd
= (struct CodeViewDebug
*) deefer
->dbg_info
;
2345 if( strcmp(cvd
->cv_nbtype
, "NB10") != 0 )
2348 * Whatever this is, we don't know how to deal with
2353 DEBUG_ProcessPDBFile(deefer
, cvd
->cv_name
);
2355 fprintf(stderr
, "Processing PDB file %s\n", cvd
->cv_name
);
2358 case IMAGE_DEBUG_TYPE_MISC
:
2360 * A pointer to a .DBG file of some sort. These files
2361 * can contain either CV4 or COFF information. Open
2362 * the file, and try to do the right thing with it.
2364 misc
= (struct MiscDebug
*) deefer
->dbg_info
;
2366 filename
= strrchr((char *) &misc
->Data
, '.');
2369 * Ignore the file if it doesn't have a .DBG extension.
2371 if( (filename
== NULL
)
2372 || ( (strcmp(filename
, ".dbg") != 0)
2373 && (strcmp(filename
, ".DBG") != 0)) )
2378 filename
= (char *) &misc
->Data
;
2381 * Do the dirty deed...
2383 DEBUG_ProcessDBGFile(deefer
, filename
);
2388 * We should never get here...
2394 fprintf(stderr
, "\n");
2399 /***********************************************************************
2402 * Display shared libarary information.
2404 void DEBUG_InfoShare(void)
2406 struct deferred_debug_info
* deefer
;
2408 fprintf(stderr
,"Address\t\tModule\tName\n");
2410 for(deefer
= dbglist
; deefer
; deefer
= deefer
->next
)
2412 fprintf(stderr
,"0x%8.8x\t(%s)\t%s\n", (unsigned int) deefer
->load_addr
,
2413 deefer
->module
? "Win32" : "ELF", deefer
->module_name
);