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>
29 #define PATH_MAX _MAX_PATH
39 * This is used so that we have some idea of what we are in fact loading
42 char * DEBUG_curr_module
= NULL
;
45 * This is an index we use to keep track of the debug information
46 * when we have multiple sources. We use the same database to also
47 * allow us to do an 'info shared' type of deal, and we use the index
48 * to eliminate duplicates.
50 static int DEBUG_next_index
= 0;
61 * This is a convenience structure used to map portions of the
71 * This is how we reference the various record types.
87 unsigned short symtype
;
88 unsigned char namelen
;
89 unsigned char name
[1];
100 unsigned short segment
;
101 unsigned short thunk_len
;
102 unsigned char thtype
;
103 unsigned char namelen
;
104 unsigned char name
[1];
110 unsigned int pparent
;
113 unsigned int proc_len
;
114 unsigned int debug_start
;
115 unsigned int debug_end
;
117 unsigned short segment
;
118 unsigned short proctype
;
120 unsigned char namelen
;
121 unsigned char name
[1];
125 short int len
; /* Total length of this entry */
126 short int id
; /* Always S_BPREL32 */
127 unsigned int offset
; /* Stack offset relative to BP */
128 unsigned short symtype
;
129 unsigned char namelen
;
130 unsigned char name
[1];
148 unsigned char variant
[1];
156 unsigned char bitoff
;
166 unsigned char arrlen
;
167 unsigned char namelen
;
168 unsigned char name
[1];
180 unsigned short structlen
;
181 unsigned char namelen
;
182 unsigned char name
[1];
192 unsigned short un_len
;
193 unsigned char namelen
;
194 unsigned char name
[1];
205 unsigned char namelen
;
206 unsigned char name
[1];
213 unsigned short int value
;
214 unsigned char namelen
;
215 unsigned char name
[1];
223 unsigned short int offset
;
224 unsigned char namelen
;
225 unsigned char name
[1];
236 unsigned char namelen
;
237 unsigned char name
[1];
242 #define S_BPREL32 0x200
243 #define S_LDATA32 0x201
244 #define S_GDATA32 0x202
245 #define S_PUB32 0x203
246 #define S_LPROC32 0x204
247 #define S_GPROC32 0x205
248 #define S_THUNK32 0x206
249 #define S_BLOCK32 0x207
250 #define S_WITH32 0x208
251 #define S_LABEL32 0x209
253 #define S_PROCREF 0x400
254 #define S_DATAREF 0x401
255 #define S_ALIGN 0x402
256 #define S_UNKNOWN 0x403
259 * This covers the basic datatypes that VC++ seems to be using these days.
260 * 32 bit mode only. There are additional numbers for the pointers in 16
261 * bit mode. There are many other types listed in the documents, but these
262 * are apparently not used by the compiler, or represent pointer types
265 #define T_NOTYPE 0x0000 /* Notype */
266 #define T_ABS 0x0001 /* Abs */
267 #define T_VOID 0x0003 /* Void */
268 #define T_CHAR 0x0010 /* signed char */
269 #define T_SHORT 0x0011 /* short */
270 #define T_LONG 0x0012 /* long */
271 #define T_QUAD 0x0013 /* long long */
272 #define T_UCHAR 0x0020 /* unsigned char */
273 #define T_USHORT 0x0021 /* unsigned short */
274 #define T_ULONG 0x0022 /* unsigned long */
275 #define T_UQUAD 0x0023 /* unsigned long long */
276 #define T_REAL32 0x0040 /* float */
277 #define T_REAL64 0x0041 /* double */
278 #define T_RCHAR 0x0070 /* real char */
279 #define T_WCHAR 0x0071 /* wide char */
280 #define T_INT4 0x0074 /* int */
281 #define T_UINT4 0x0075 /* unsigned int */
283 #define T_32PVOID 0x0403 /* 32 bit near pointer to void */
284 #define T_32PCHAR 0x0410 /* 16:32 near pointer to signed char */
285 #define T_32PSHORT 0x0411 /* 16:32 near pointer to short */
286 #define T_32PLONG 0x0412 /* 16:32 near pointer to int */
287 #define T_32PQUAD 0x0413 /* 16:32 near pointer to long long */
288 #define T_32PUCHAR 0x0420 /* 16:32 near pointer to unsigned char */
289 #define T_32PUSHORT 0x0421 /* 16:32 near pointer to unsigned short */
290 #define T_32PULONG 0x0422 /* 16:32 near pointer to unsigned int */
291 #define T_32PUQUAD 0x0423 /* 16:32 near pointer to long long */
292 #define T_32PREAL32 0x0440 /* 16:32 near pointer to float */
293 #define T_32PREAL64 0x0441 /* 16:32 near pointer to float */
294 #define T_32PRCHAR 0x0470 /* 16:32 near pointer to real char */
295 #define T_32PWCHAR 0x0471 /* 16:32 near pointer to real char */
296 #define T_32PINT4 0x0474 /* 16:32 near pointer to int */
297 #define T_32PUINT4 0x0475 /* 16:32 near pointer to unsigned int */
299 #define LF_MODIFIER 0x1
300 #define LF_POINTER 0x2
303 #define LF_STRUCTURE 0x5
305 #define LF_ENUMERATION 0x7
306 #define LF_PROCEDURE 0x8
307 #define LF_MFUNCTION 0x9
308 #define LF_VTSHAPE 0xa
309 #define LF_BARRAY 0xd
310 #define LF_DIMARRAY 0x11
311 #define LF_VFTPATH 0x12
313 #define LF_SKIP 0x200
314 #define LF_ARGLIST 0x201
315 #define LF_FIELDLIST 0x204
316 #define LF_DERIVED 0x205
317 #define LF_BITFIELD 0x206
319 #define LF_BCLASS 0x400
320 #define LF_VBCLASS 0x401
321 #define LF_IVBCLASS 0x402
322 #define LF_ENUMERATE 0x403
323 #define LF_FRIENDFCN 0x404
324 #define LF_INDEX 0x405
325 #define LF_MEMBER 0x406
326 #define LF_STMEMBER 0x407
327 #define LF_METHOD 0x408
328 #define LF_NESTEDTYPE 0x409
329 #define LF_VFUNCTAB 0x40a
330 #define LF_FRIENDCLS 0x40b
331 #define LF_ONEMETHOD 0x40c
332 #define LF_FUNCOFF 0x40d
334 #define MAX_BUILTIN_TYPES 0x480
335 static struct datatype
* cv_basic_types
[MAX_BUILTIN_TYPES
];
336 static int num_cv_defined_types
= 0;
337 static struct datatype
**cv_defined_types
= NULL
;
340 * For the type CODEVIEW debug directory entries, the debug directory
341 * points to a structure like this. The cv_name field is the name
342 * of an external .PDB file.
347 unsigned int cv_timestamp
;
353 unsigned int DataType
;
361 * This is the header that the COFF variety of debug header points to.
365 unsigned int SymbolOffset
;
366 unsigned int N_Linenum
;
367 unsigned int LinenumberOffset
;
368 unsigned int Unused
[4];
372 unsigned int VirtualAddr
;
373 unsigned short int Linenum
;
377 unsigned int startaddr
;
378 unsigned int endaddr
;
382 struct name_hash
**entries
;
392 unsigned int NotLong
;
393 unsigned int StrTaboff
;
400 unsigned char NumberOfAuxSymbols
;
403 struct CoffAuxSection
{
405 unsigned short NumberOfRelocations
;
406 unsigned short NumberOfLinenumbers
;
407 unsigned int CheckSum
;
413 * These two structures are used in the directory within a .DBG file
414 * to locate the individual important bits that we might want to see.
417 short unsigned int dhsize
;
418 short unsigned int desize
;
420 unsigned int next_offset
;
425 short unsigned int subsect_number
;
426 short unsigned int module_number
;
432 * These are the values of interest that the subsect_number field takes.
434 #define sstAlignSym 0x125
435 #define sstSrcModule 0x127
437 struct codeview_linetab_hdr
444 unsigned short * linetab
;
445 unsigned int * offtab
;
448 struct codeview_pdb_hdr
451 unsigned int blocksize
; /* Extent size */
452 unsigned short loc_freelist
; /* freelist. */
453 unsigned short alloc_filesize
; /* # extents allocated. */
454 unsigned int toc_len
;
455 unsigned int unknown
;
456 unsigned short toc_ext
[1]; /* array of extent #'s for toc. */
460 * This is our own structure that we use to keep track of the contents
467 short int * extent_list
;
468 unsigned int linetab_offset
;
469 unsigned int linetab_len
;
473 * These are the structures that represent how the file table is set up
474 * within the PDB file.
478 unsigned short tab1_file
;
479 unsigned short tab2_file
;
480 unsigned short gsym_file
;
481 unsigned short padding
;
482 unsigned int ftab_len
;
483 unsigned int fofftab_len
;
484 unsigned int hash_len
;
485 unsigned int strtab_len
;
490 unsigned int reserved1
;
491 unsigned short datasect_segment
;
492 unsigned short reserved2
;
493 unsigned int datasect_offset
;
494 unsigned int datasect_size
;
495 unsigned int datasect_flags
;
496 unsigned short reserved3
;
497 unsigned short index
;
498 unsigned short num6a
;
499 unsigned short file_number
;
500 unsigned int linetab_offset
;
501 unsigned int linetab_len
;
505 unsigned char filename
[1];
509 ********************************************************************
511 struct deferred_debug_info
513 struct deferred_debug_info
* next
;
518 LPIMAGE_DEBUG_DIRECTORY dbgdir
;
520 LPIMAGE_SECTION_HEADER sectp
;
526 struct deferred_debug_info
* dbglist
= NULL
;
529 * A simple macro that tells us whether a given COFF symbol is a
532 #define N_TMASK 0x0030
533 #define IMAGE_SYM_DTYPE_FUNCTION 2
535 #define ISFCN(x) (((x) & N_TMASK) == (IMAGE_SYM_DTYPE_FUNCTION << N_BTSHFT))
539 * This is what we are looking for in the COFF symbols.
541 #define IMAGE_SYM_CLASS_EXTERNAL 0x2
542 #define IMAGE_SYM_CLASS_STATIC 0x3
543 #define IMAGE_SYM_CLASS_FILE 0x67
546 struct datatype
* DEBUG_GetCVType(int typeno
)
548 struct datatype
* dt
= NULL
;
551 * Convert Codeview type numbers into something we can grok internally.
552 * Numbers < 0x1000 are all fixed builtin types. Numbers from 0x1000 and
553 * up are all user defined (structs, etc).
555 if( typeno
< 0x1000 )
557 if( typeno
< MAX_BUILTIN_TYPES
)
559 dt
= cv_basic_types
[typeno
];
564 if( typeno
- 0x1000 < num_cv_defined_types
)
566 dt
= cv_defined_types
[typeno
- 0x1000];
574 DEBUG_ParseTypeTable(char * table
, int len
)
578 enum debug_type fieldtype
;
582 struct datatype
* subtype
;
584 union codeview_type
* type
;
585 union codeview_type
* type2
;
586 struct datatype
* typeptr
;
590 ptr
= (union any_size
) (table
+ 16);
591 while( ptr
.c
- table
< len
)
593 type
= (union codeview_type
*) ptr
.c
;
595 if( curr_type
- 0x1000 >= num_cv_defined_types
)
597 num_cv_defined_types
+= 0x100;
598 cv_defined_types
= (struct datatype
**) realloc(cv_defined_types
,
599 num_cv_defined_types
* sizeof(struct datatype
*));
600 memset(cv_defined_types
+ num_cv_defined_types
- 0x100,
602 0x100 * sizeof(struct datatype
*));
603 if( cv_defined_types
== NULL
)
609 switch(type
->generic
.id
)
612 cv_defined_types
[curr_type
- 0x1000] =
613 DEBUG_FindOrMakePointerType(DEBUG_GetCVType(type
->pointer
.datatype
));
616 if( type
->array
.arrlen
>= 0x8000 )
619 * This is a numeric leaf, I am too lazy to handle this right
622 fprintf(stderr
, "Ignoring large numberic leaf.\n");
625 if( type
->array
.namelen
!= 0 )
627 memset(symname
, 0, sizeof(symname
));
628 memcpy(symname
, type
->array
.name
, type
->array
.namelen
);
629 typeptr
= DEBUG_NewDataType(ARRAY
, symname
);
633 typeptr
= DEBUG_NewDataType(ARRAY
, NULL
);
635 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
637 subtype
= DEBUG_GetCVType(type
->array
.elemtype
);
638 if( (subtype
== NULL
)
639 || (elem_size
= DEBUG_GetObjectSize(subtype
)) == 0 )
645 arr_max
= type
->array
.arrlen
/ DEBUG_GetObjectSize(subtype
);
648 DEBUG_SetArrayParams(typeptr
, 0, arr_max
, subtype
);
652 * This is where the basic list of fields is defined for
653 * structures and classes.
655 * First, we need to look ahead and see whether we are building
656 * a fieldlist for an enum or a struct.
659 type2
= (union codeview_type
*) ptr2
.c
;
660 if( type2
->member
.id
== LF_MEMBER
)
662 typeptr
= DEBUG_NewDataType(STRUCT
, NULL
);
665 else if( type2
->member
.id
== LF_ENUMERATE
)
667 typeptr
= DEBUG_NewDataType(ENUM
, NULL
);
675 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
676 while( ptr2
.c
< (ptr
.c
+ ((type
->generic
.len
+ 3) & ~3)) )
678 type2
= (union codeview_type
*) ptr2
.c
;
679 if( type2
->member
.id
== LF_MEMBER
&& fieldtype
== STRUCT
)
681 memset(symname
, 0, sizeof(symname
));
682 memcpy(symname
, type2
->member
.name
, type2
->member
.namelen
);
684 subtype
= DEBUG_GetCVType(type2
->member
.type
);
686 if( subtype
!= NULL
)
688 elem_size
= DEBUG_GetObjectSize(subtype
);
691 if( type2
->member
.offset
>= 0x8000 )
694 * This is a numeric leaf, I am too lazy to handle this right
697 fprintf(stderr
, "Ignoring large numberic leaf.\n");
701 DEBUG_AddStructElement(typeptr
, symname
, subtype
,
702 type2
->member
.offset
<< 3,
706 else if( type2
->member
.id
== LF_ENUMERATE
&& fieldtype
== ENUM
)
708 memset(symname
, 0, sizeof(symname
));
709 memcpy(symname
, type2
->enumerate
.name
, type2
->enumerate
.namelen
);
711 if( type2
->enumerate
.value
>= 0x8000 )
714 * This is a numeric leaf, I am too lazy to handle this right
717 fprintf(stderr
, "Ignoring large numberic leaf.\n");
721 DEBUG_AddStructElement(typeptr
, symname
, NULL
,
722 type2
->enumerate
.value
, 0);
728 * Something else I have never seen before. Either wrong type of
729 * object in the fieldlist, or some other problem which I wouldn't
730 * really know how to handle until it came up.
732 fprintf(stderr
, "Unexpected entry in fieldlist\n");
737 ptr2
.c
+= ((type2
->member
.namelen
+ 9 + 3) & ~3);
742 if( type
->structure
.structlen
>= 0x8000 )
745 * This is a numeric leaf, I am too lazy to handle this right
748 fprintf(stderr
, "Ignoring large numberic leaf.\n");
751 memset(symname
, 0, sizeof(symname
));
752 memcpy(symname
, type
->structure
.name
, type
->structure
.namelen
);
753 if( strcmp(symname
, "__unnamed") == 0 )
755 typeptr
= DEBUG_NewDataType(STRUCT
, NULL
);
759 typeptr
= DEBUG_NewDataType(STRUCT
, symname
);
761 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
764 * Now copy the relevant bits from the fieldlist that we specified.
766 subtype
= DEBUG_GetCVType(type
->structure
.fieldlist
);
768 if( subtype
!= NULL
)
770 DEBUG_SetStructSize(typeptr
, type
->structure
.structlen
);
771 DEBUG_CopyFieldlist(typeptr
, subtype
);
775 if( type
->t_union
.un_len
>= 0x8000 )
778 * This is a numeric leaf, I am too lazy to handle this right
781 fprintf(stderr
, "Ignoring large numberic leaf.\n");
784 memset(symname
, 0, sizeof(symname
));
785 memcpy(symname
, type
->t_union
.name
, type
->t_union
.namelen
);
787 if( strcmp(symname
, "__unnamed") == 0 )
789 typeptr
= DEBUG_NewDataType(STRUCT
, NULL
);
793 typeptr
= DEBUG_NewDataType(STRUCT
, symname
);
796 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
799 * Now copy the relevant bits from the fieldlist that we specified.
801 subtype
= DEBUG_GetCVType(type
->t_union
.field
);
803 if( subtype
!= NULL
)
805 DEBUG_SetStructSize(typeptr
, type
->t_union
.un_len
);
806 DEBUG_CopyFieldlist(typeptr
, subtype
);
810 typeptr
= DEBUG_NewDataType(BITFIELD
, NULL
);
811 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
812 DEBUG_SetBitfieldParams(typeptr
, type
->bitfield
.bitoff
,
813 type
->bitfield
.nbits
,
814 DEBUG_GetCVType(type
->bitfield
.type
));
817 memset(symname
, 0, sizeof(symname
));
818 memcpy(symname
, type
->enumeration
.name
, type
->enumeration
.namelen
);
819 typeptr
= DEBUG_NewDataType(ENUM
, symname
);
820 cv_defined_types
[curr_type
- 0x1000] = typeptr
;
823 * Now copy the relevant bits from the fieldlist that we specified.
825 subtype
= DEBUG_GetCVType(type
->enumeration
.field
);
827 if( subtype
!= NULL
)
829 DEBUG_CopyFieldlist(typeptr
, subtype
);
837 ptr
.c
+= (type
->generic
.len
+ 3) & ~3;
844 DEBUG_InitCVDataTypes()
847 * These are the common builtin types that are used by VC++.
849 cv_basic_types
[T_NOTYPE
] = NULL
;
850 cv_basic_types
[T_ABS
] = NULL
;
851 cv_basic_types
[T_VOID
] = DEBUG_NewDataType(BASIC
, "void");
852 cv_basic_types
[T_CHAR
] = DEBUG_NewDataType(BASIC
, "char");
853 cv_basic_types
[T_SHORT
] = DEBUG_NewDataType(BASIC
, "short int");
854 cv_basic_types
[T_LONG
] = DEBUG_NewDataType(BASIC
, "long int");
855 cv_basic_types
[T_QUAD
] = DEBUG_NewDataType(BASIC
, "long long int");
856 cv_basic_types
[T_UCHAR
] = DEBUG_NewDataType(BASIC
, "unsigned char");
857 cv_basic_types
[T_USHORT
] = DEBUG_NewDataType(BASIC
, "short unsigned int");
858 cv_basic_types
[T_ULONG
] = DEBUG_NewDataType(BASIC
, "long unsigned int");
859 cv_basic_types
[T_UQUAD
] = DEBUG_NewDataType(BASIC
, "long long unsigned int");
860 cv_basic_types
[T_REAL32
] = DEBUG_NewDataType(BASIC
, "float");
861 cv_basic_types
[T_REAL64
] = DEBUG_NewDataType(BASIC
, "double");
862 cv_basic_types
[T_RCHAR
] = DEBUG_NewDataType(BASIC
, "char");
863 cv_basic_types
[T_WCHAR
] = DEBUG_NewDataType(BASIC
, "short");
864 cv_basic_types
[T_INT4
] = DEBUG_NewDataType(BASIC
, "int");
865 cv_basic_types
[T_UINT4
] = DEBUG_NewDataType(BASIC
, "unsigned int");
867 cv_basic_types
[T_32PVOID
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_VOID
]);
868 cv_basic_types
[T_32PCHAR
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_CHAR
]);
869 cv_basic_types
[T_32PSHORT
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_SHORT
]);
870 cv_basic_types
[T_32PLONG
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_LONG
]);
871 cv_basic_types
[T_32PQUAD
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_QUAD
]);
872 cv_basic_types
[T_32PUCHAR
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_UCHAR
]);
873 cv_basic_types
[T_32PUSHORT
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_USHORT
]);
874 cv_basic_types
[T_32PULONG
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_ULONG
]);
875 cv_basic_types
[T_32PUQUAD
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_UQUAD
]);
876 cv_basic_types
[T_32PREAL32
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_REAL32
]);
877 cv_basic_types
[T_32PREAL64
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_REAL64
]);
878 cv_basic_types
[T_32PRCHAR
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_RCHAR
]);
879 cv_basic_types
[T_32PWCHAR
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_WCHAR
]);
880 cv_basic_types
[T_32PINT4
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_INT4
]);
881 cv_basic_types
[T_32PUINT4
] = DEBUG_FindOrMakePointerType(cv_basic_types
[T_UINT4
]);
885 * In this function, we keep track of deferred debugging information
886 * that we may need later if we were to need to use the internal debugger.
887 * We don't fully process it here for performance reasons.
890 DEBUG_RegisterDebugInfo(int fd
, struct pe_data
* pe
,
891 int load_addr
, u_long v_addr
, u_long size
)
893 int has_codeview
= FALSE
;
896 LPIMAGE_DEBUG_DIRECTORY dbgptr
;
897 struct deferred_debug_info
* deefer
;
900 dbgptr
= (LPIMAGE_DEBUG_DIRECTORY
) (load_addr
+ v_addr
);
901 for(; size
>= sizeof(*dbgptr
); size
-= sizeof(*dbgptr
), dbgptr
++ )
905 case IMAGE_DEBUG_TYPE_CODEVIEW
:
906 case IMAGE_DEBUG_TYPE_MISC
:
913 dbgptr
= (LPIMAGE_DEBUG_DIRECTORY
) (load_addr
+ v_addr
);
914 for(; size
>= sizeof(*dbgptr
); size
-= sizeof(*dbgptr
), dbgptr
++ )
918 case IMAGE_DEBUG_TYPE_COFF
:
920 * If we have both codeview and COFF debug info, ignore the
921 * coff debug info as it would just confuse us, and it is
924 * FIXME - this is broken - if we cannot find the PDB file, then
925 * we end up with no debugging info at all. In this case, we
926 * should use the COFF info as a backup.
932 case IMAGE_DEBUG_TYPE_CODEVIEW
:
933 case IMAGE_DEBUG_TYPE_MISC
:
935 * This is usually an indirection to a .DBG file.
936 * This is similar to (but a slightly older format) from the
939 * First check to see if the image was 'stripped'. If so, it
940 * means that this entry points to a .DBG file. Otherwise,
941 * it just points to itself, and we can ignore this.
943 if( (dbgptr
->Type
== IMAGE_DEBUG_TYPE_MISC
)
944 && (pe
->pe_header
->FileHeader
.Characteristics
& IMAGE_FILE_DEBUG_STRIPPED
) == 0 )
949 deefer
= (struct deferred_debug_info
*) xmalloc(sizeof(*deefer
));
952 deefer
->dbg_info
= NULL
;
953 deefer
->dbg_size
= 0;
956 * Read the important bits. What we do after this depends
957 * upon the type, but this is always enough so we are able
958 * to proceed if we know what we need to do next.
960 deefer
->dbg_size
= dbgptr
->SizeOfData
;
961 deefer
->dbg_info
= (char *) xmalloc(dbgptr
->SizeOfData
);
962 lseek(fd
, dbgptr
->PointerToRawData
, SEEK_SET
);
963 read(fd
, deefer
->dbg_info
, deefer
->dbg_size
);
965 deefer
->load_addr
= (char *) load_addr
;
966 deefer
->dbgdir
= dbgptr
;
967 deefer
->next
= dbglist
;
968 deefer
->loaded
= FALSE
;
969 deefer
->dbg_index
= DEBUG_next_index
;
970 deefer
->module_name
= xstrdup(DEBUG_curr_module
);
972 deefer
->sectp
= pe
->pe_seg
;
973 deefer
->nsect
= pe
->pe_header
->FileHeader
.NumberOfSections
;
988 * ELF modules are also entered into the list - this is so that we
989 * can make 'info shared' types of displays possible.
992 DEBUG_RegisterELFDebugInfo(int load_addr
, u_long size
, char * name
)
994 struct deferred_debug_info
* deefer
;
996 deefer
= (struct deferred_debug_info
*) xmalloc(sizeof(*deefer
));
1000 * Read the important bits. What we do after this depends
1001 * upon the type, but this is always enough so we are able
1002 * to proceed if we know what we need to do next.
1004 deefer
->dbg_size
= size
;
1005 deefer
->dbg_info
= (char *) NULL
;
1007 deefer
->load_addr
= (char *) load_addr
;
1008 deefer
->dbgdir
= NULL
;
1009 deefer
->next
= dbglist
;
1010 deefer
->loaded
= TRUE
;
1011 deefer
->dbg_index
= DEBUG_next_index
;
1012 deefer
->module_name
= xstrdup(name
);
1023 * Process COFF debugging information embedded in a Win32 application.
1028 DEBUG_ProcessCoff(struct deferred_debug_info
* deefer
)
1030 struct CoffAuxSection
* aux
;
1031 struct CoffDebug
* coff
;
1032 struct CoffFiles
* coff_files
= NULL
;
1033 struct CoffLinenum
* coff_linetab
;
1035 struct CoffSymbol
* coff_sym
;
1036 struct CoffSymbol
* coff_symbol
;
1037 struct CoffFiles
* curr_file
= NULL
;
1041 struct CoffLinenum
* linepnt
;
1048 int nfiles_alloc
= 0;
1049 struct CoffFiles orig_file
;
1051 char * this_file
= NULL
;
1053 coff
= (struct CoffDebug
*) deefer
->dbg_info
;
1055 coff_symbol
= (struct CoffSymbol
*) ((unsigned int) coff
+ coff
->SymbolOffset
);
1056 coff_linetab
= (struct CoffLinenum
*) ((unsigned int) coff
+ coff
->LinenumberOffset
);
1057 coff_strtab
= (char *) ((unsigned int) coff_symbol
+ 18*coff
->N_Sym
);
1061 for(i
=0; i
< coff
->N_Sym
; i
++ )
1064 * We do this because some compilers (i.e. gcc) incorrectly
1065 * pad the structure up to a 4 byte boundary. The structure
1066 * is really only 18 bytes long, so we have to manually make sure
1069 * FIXME - there must be a way to have autoconf figure out the
1070 * correct compiler option for this. If it is always gcc, that
1071 * makes life simpler, but I don't want to force this.
1073 coff_sym
= (struct CoffSymbol
*) ((unsigned int) coff_symbol
+ 18*i
);
1074 naux
= coff_sym
->NumberOfAuxSymbols
;
1076 if( coff_sym
->StorageClass
== IMAGE_SYM_CLASS_FILE
)
1078 if( nfiles
+ 1 >= nfiles_alloc
)
1081 coff_files
= (struct CoffFiles
*) realloc( coff_files
,
1082 nfiles_alloc
* sizeof(struct CoffFiles
));
1084 curr_file
= coff_files
+ nfiles
;
1086 curr_file
->startaddr
= 0xffffffff;
1087 curr_file
->endaddr
= 0;
1088 curr_file
->filename
= ((char *) coff_sym
) + 18;
1089 curr_file
->linetab_offset
= -1;
1090 curr_file
->linecnt
= 0;
1091 curr_file
->entries
= NULL
;
1092 curr_file
->neps
= curr_file
->neps_alloc
= 0;
1094 fprintf(stderr
,"New file %s\n", curr_file
->filename
);
1101 * This guy marks the size and location of the text section
1102 * for the current file. We need to keep track of this so
1103 * we can figure out what file the different global functions
1106 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_STATIC
)
1108 && (coff_sym
->Type
== 0)
1109 && (coff_sym
->SectionNumber
== 1) )
1111 aux
= (struct CoffAuxSection
*) ((unsigned int) coff_sym
+ 18);
1113 if( curr_file
->linetab_offset
!= -1 )
1116 fprintf(stderr
, "Duplicating sect from %s: %x %x %x %d %d\n",
1117 curr_file
->filename
,
1119 aux
->NumberOfRelocations
,
1120 aux
->NumberOfLinenumbers
,
1123 fprintf(stderr
, "More sect %d %x %d %d %d\n",
1124 coff_sym
->SectionNumber
,
1127 coff_sym
->StorageClass
,
1128 coff_sym
->NumberOfAuxSymbols
);
1132 * Save this so we can copy bits from it.
1134 orig_file
= *curr_file
;
1137 * Duplicate the file entry. We have no way to describe
1138 * multiple text sections in our current way of handling things.
1140 if( nfiles
+ 1 >= nfiles_alloc
)
1143 coff_files
= (struct CoffFiles
*) realloc( coff_files
,
1144 nfiles_alloc
* sizeof(struct CoffFiles
));
1146 curr_file
= coff_files
+ nfiles
;
1148 curr_file
->startaddr
= 0xffffffff;
1149 curr_file
->endaddr
= 0;
1150 curr_file
->filename
= orig_file
.filename
;
1151 curr_file
->linetab_offset
= -1;
1152 curr_file
->linecnt
= 0;
1153 curr_file
->entries
= NULL
;
1154 curr_file
->neps
= curr_file
->neps_alloc
= 0;
1159 fprintf(stderr
, "New text sect from %s: %x %x %x %d %d\n",
1160 curr_file
->filename
,
1162 aux
->NumberOfRelocations
,
1163 aux
->NumberOfLinenumbers
,
1169 if( curr_file
->startaddr
> coff_sym
->Value
)
1171 curr_file
->startaddr
= coff_sym
->Value
;
1174 if( curr_file
->startaddr
> coff_sym
->Value
)
1176 curr_file
->startaddr
= coff_sym
->Value
;
1179 if( curr_file
->endaddr
< coff_sym
->Value
+ aux
->Length
)
1181 curr_file
->endaddr
= coff_sym
->Value
+ aux
->Length
;
1184 curr_file
->linetab_offset
= linetab_indx
;
1185 curr_file
->linecnt
= aux
->NumberOfLinenumbers
;
1186 linetab_indx
+= aux
->NumberOfLinenumbers
;
1191 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_STATIC
)
1193 && (coff_sym
->SectionNumber
== 1) )
1196 * This is a normal static function when naux == 0.
1197 * Just register it. The current file is the correct
1198 * one in this instance.
1200 if( coff_sym
->N
.Name
.NotLong
)
1202 memcpy(namebuff
, coff_sym
->N
.ShortName
, 8);
1204 nampnt
= &namebuff
[0];
1208 nampnt
= coff_strtab
+ coff_sym
->N
.Name
.StrTaboff
;
1211 if( nampnt
[0] == '_' )
1217 new_addr
.off
= (int) (deefer
->load_addr
+ coff_sym
->Value
);
1219 if( curr_file
->neps
+ 1 >= curr_file
->neps_alloc
)
1221 curr_file
->neps_alloc
+= 10;
1222 curr_file
->entries
= (struct name_hash
**)
1223 realloc( curr_file
->entries
,
1224 curr_file
->neps_alloc
* sizeof(struct name_hash
*));
1227 fprintf(stderr
,"\tAdding static symbol %s\n", nampnt
);
1229 curr_file
->entries
[curr_file
->neps
++] =
1230 DEBUG_AddSymbol( nampnt
, &new_addr
, this_file
, SYM_WIN32
);
1235 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_EXTERNAL
)
1236 && ISFCN(coff_sym
->Type
)
1237 && (coff_sym
->SectionNumber
> 0) )
1239 if( coff_sym
->N
.Name
.NotLong
)
1241 memcpy(namebuff
, coff_sym
->N
.ShortName
, 8);
1243 nampnt
= &namebuff
[0];
1247 nampnt
= coff_strtab
+ coff_sym
->N
.Name
.StrTaboff
;
1251 if( nampnt
[0] == '_' )
1257 new_addr
.off
= (int) (deefer
->load_addr
+ coff_sym
->Value
);
1260 fprintf(stderr
, "%d: %x %s\n", i
, new_addr
.off
, nampnt
);
1262 fprintf(stderr
,"\tAdding global symbol %s\n", nampnt
);
1266 * Now we need to figure out which file this guy belongs to.
1269 for(j
=0; j
< nfiles
; j
++)
1271 if( coff_files
[j
].startaddr
<= coff_sym
->Value
1272 && coff_files
[j
].endaddr
> coff_sym
->Value
)
1274 this_file
= coff_files
[j
].filename
;
1278 if( coff_files
[j
].neps
+ 1 >= coff_files
[j
].neps_alloc
)
1280 coff_files
[j
].neps_alloc
+= 10;
1281 coff_files
[j
].entries
= (struct name_hash
**)
1282 realloc( coff_files
[j
].entries
,
1283 coff_files
[j
].neps_alloc
* sizeof(struct name_hash
*));
1285 coff_files
[j
].entries
[coff_files
[j
].neps
++] =
1286 DEBUG_AddSymbol( nampnt
, &new_addr
, this_file
, SYM_WIN32
);
1291 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_EXTERNAL
)
1292 && (coff_sym
->SectionNumber
> 0) )
1295 * Similar to above, but for the case of data symbols.
1296 * These aren't treated as entrypoints.
1298 if( coff_sym
->N
.Name
.NotLong
)
1300 memcpy(namebuff
, coff_sym
->N
.ShortName
, 8);
1302 nampnt
= &namebuff
[0];
1306 nampnt
= coff_strtab
+ coff_sym
->N
.Name
.StrTaboff
;
1310 if( nampnt
[0] == '_' )
1316 new_addr
.off
= (int) (deefer
->load_addr
+ coff_sym
->Value
);
1319 fprintf(stderr
, "%d: %x %s\n", i
, new_addr
.off
, nampnt
);
1321 fprintf(stderr
,"\tAdding global data symbol %s\n", nampnt
);
1325 * Now we need to figure out which file this guy belongs to.
1327 DEBUG_AddSymbol( nampnt
, &new_addr
, NULL
, SYM_WIN32
);
1332 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_STATIC
)
1336 * Ignore these. They don't have anything to do with
1344 fprintf(stderr
,"Skipping unknown entry %d %d %d\n", coff_sym
->StorageClass
,
1345 coff_sym
->SectionNumber
, naux
);
1349 * For now, skip past the aux entries.
1356 * OK, we now should have a list of files, and we should have a list
1357 * of entrypoints. We need to sort the entrypoints so that we are
1358 * able to tie the line numbers with the given functions within the
1361 if( coff_files
!= NULL
)
1363 for(j
=0; j
< nfiles
; j
++)
1365 if( coff_files
[j
].entries
!= NULL
)
1367 qsort(coff_files
[j
].entries
, coff_files
[j
].neps
,
1368 sizeof(struct name_hash
*), DEBUG_cmp_sym
);
1373 * Now pick apart the line number tables, and attach the entries
1374 * to the given functions.
1376 for(j
=0; j
< nfiles
; j
++)
1379 for(k
=0; k
< coff_files
[j
].linecnt
; k
++)
1382 * Another monstrosity caused by the fact that we are using
1383 * a 6 byte structure, and gcc wants to pad structures to 4 byte
1384 * boundaries. Otherwise we could just index into an array.
1386 linepnt
= (struct CoffLinenum
*)
1387 ((unsigned int) coff_linetab
+
1388 6*(coff_files
[j
].linetab_offset
+ k
));
1390 * If we have spilled onto the next entrypoint, then
1391 * bump the counter..
1395 DEBUG_GetSymbolAddr(coff_files
[j
].entries
[i
+1], &new_addr
);
1396 if( (i
+1 < coff_files
[j
].neps
)
1397 && ( ((unsigned int) deefer
->load_addr
+ linepnt
->VirtualAddr
)
1409 * Add the line number. This is always relative to the
1410 * start of the function, so we need to subtract that offset
1413 DEBUG_GetSymbolAddr(coff_files
[j
].entries
[i
], &new_addr
);
1414 DEBUG_AddLineNumber(coff_files
[j
].entries
[i
],
1416 (unsigned int) deefer
->load_addr
1417 + linepnt
->VirtualAddr
1425 if( coff_files
!= NULL
)
1427 for(j
=0; j
< nfiles
; j
++)
1429 if( coff_files
[j
].entries
!= NULL
)
1431 free(coff_files
[j
].entries
);
1442 * Process a codeview line number table. Digestify the thing so that
1443 * we can easily reference the thing when we process the rest of
1446 static struct codeview_linetab_hdr
*
1447 DEBUG_SnarfLinetab(char * linetab
,
1451 char filename
[PATH_MAX
];
1452 unsigned int * filetab
;
1456 struct codeview_linetab_hdr
* lt_hdr
;
1457 unsigned int * lt_ptr
;
1461 union any_size pnt2
;
1462 struct startend
* start
;
1466 * Now get the important bits.
1468 pnt
= (union any_size
) linetab
;
1472 filetab
= (unsigned int *) pnt
.c
;
1475 * Now count up the number of segments in the file.
1478 for(i
=0; i
<nfile
; i
++)
1480 pnt2
= (union any_size
) (linetab
+ filetab
[i
]);
1485 * Next allocate the header we will be returning.
1486 * There is one header for each segment, so that we can reach in
1487 * and pull bits as required.
1489 lt_hdr
= (struct codeview_linetab_hdr
*)
1490 xmalloc((nseg
+ 1) * sizeof(*lt_hdr
));
1491 if( lt_hdr
== NULL
)
1496 memset(lt_hdr
, 0, sizeof(*lt_hdr
) * (nseg
+1));
1499 * Now fill the header we will be returning, one for each segment.
1500 * Note that this will basically just contain pointers into the existing
1501 * line table, and we do not actually copy any additional information
1502 * or allocate any additional memory.
1506 for(i
=0; i
<nfile
; i
++)
1509 * Get the pointer into the segment information.
1511 pnt2
= (union any_size
) (linetab
+ filetab
[i
]);
1512 file_segcount
= *pnt2
.s
;
1515 lt_ptr
= (unsigned int *) pnt2
.c
;
1516 start
= (struct startend
*) (lt_ptr
+ file_segcount
);
1519 * Now snarf the filename for all of the segments for this file.
1521 fn
= (unsigned char *) (start
+ file_segcount
);
1522 memset(filename
, 0, sizeof(filename
));
1523 memcpy(filename
, fn
+ 1, *fn
);
1524 fn
= strdup(filename
);
1526 for(k
= 0; k
< file_segcount
; k
++, this_seg
++)
1528 pnt2
= (union any_size
) (linetab
+ lt_ptr
[k
]);
1529 lt_hdr
[this_seg
].start
= start
[k
].start
;
1530 lt_hdr
[this_seg
].end
= start
[k
].end
;
1531 lt_hdr
[this_seg
].sourcefile
= fn
;
1532 lt_hdr
[this_seg
].segno
= *pnt2
.s
++;
1533 lt_hdr
[this_seg
].nline
= *pnt2
.s
++;
1534 lt_hdr
[this_seg
].offtab
= pnt2
.ui
;
1535 lt_hdr
[this_seg
].linetab
= (unsigned short *)
1536 (pnt2
.ui
+ lt_hdr
[this_seg
].nline
);
1547 DEBUG_SnarfCodeView( struct deferred_debug_info
* deefer
,
1550 struct codeview_linetab_hdr
* linetab
)
1552 struct name_hash
* curr_func
= NULL
;
1553 struct wine_locals
* curr_sym
= NULL
;
1560 IMAGE_SECTION_HEADER
* sectp
;
1561 union codeview_symbol
* sym
;
1562 char symname
[PATH_MAX
];
1563 struct name_hash
* thunk_sym
= NULL
;
1565 ptr
= (union any_size
) cv_data
;
1566 nsect
= deefer
->nsect
;
1567 sectp
= deefer
->sectp
;
1570 * Skip over the first word. Don't really know what it means, but
1576 * Loop over the different types of records and whenever we
1577 * find something we are interested in, record it and move on.
1579 while( ptr
.c
- cv_data
< size
)
1581 sym
= (union codeview_symbol
*) ptr
.c
;
1583 if( sym
->generic
.len
- sizeof(int) == (ptr
.c
- cv_data
) )
1586 * This happens when we have indirect symbols that VC++ 4.2
1587 * sometimes uses when there isn't a line number table.
1588 * We ignore it - we will process and enter all of the
1589 * symbols in the global symbol table anyways, so there
1590 * isn't much point in keeping track of all of this crap.
1595 memset(symname
, 0, sizeof(symname
));
1596 switch(sym
->generic
.id
)
1602 * First, a couple of sanity checks.
1604 if( sym
->data
.namelen
== 0 )
1609 if( sym
->data
.seg
== 0 || sym
->data
.seg
> nsect
)
1615 * Global and local data symbols. We don't associate these
1616 * with any given source file.
1619 memcpy(symname
, sym
->data
.name
, sym
->data
.namelen
);
1621 new_addr
.type
= DEBUG_GetCVType(sym
->data
.symtype
);
1622 new_addr
.off
= (unsigned int) deefer
->load_addr
+
1623 sectp
[sym
->data
.seg
- 1].VirtualAddress
+
1625 DEBUG_AddSymbol( symname
, &new_addr
, NULL
, SYM_WIN32
| SYM_DATA
);
1629 * Sort of like a global function, but it just points
1630 * to a thunk, which is a stupid name for what amounts to
1631 * a PLT slot in the normal jargon that everyone else uses.
1633 memcpy(symname
, sym
->thunk
.name
, sym
->thunk
.namelen
);
1635 new_addr
.type
= NULL
;
1636 new_addr
.off
= (unsigned int) deefer
->load_addr
+
1637 sectp
[sym
->thunk
.segment
- 1].VirtualAddress
+
1639 thunk_sym
= DEBUG_AddSymbol( symname
, &new_addr
, NULL
,
1640 SYM_WIN32
| SYM_FUNC
);
1641 DEBUG_SetSymbolSize(thunk_sym
, sym
->thunk
.thunk_len
);
1646 * Global and static functions.
1648 memcpy(symname
, sym
->proc
.name
, sym
->proc
.namelen
);
1650 new_addr
.type
= DEBUG_GetCVType(sym
->proc
.proctype
);
1651 new_addr
.off
= (unsigned int) deefer
->load_addr
+
1652 sectp
[sym
->proc
.segment
- 1].VirtualAddress
+
1655 * See if we can find a segment that this goes with. If so,
1656 * it means that we also may have line number information
1657 * for this function.
1659 for(i
=0; linetab
[i
].linetab
!= NULL
; i
++)
1661 if( ((unsigned int) deefer
->load_addr
1662 + sectp
[linetab
[i
].segno
- 1].VirtualAddress
1663 + linetab
[i
].start
<= new_addr
.off
)
1664 && ((unsigned int) deefer
->load_addr
1665 + sectp
[linetab
[i
].segno
- 1].VirtualAddress
1666 + linetab
[i
].end
> new_addr
.off
) )
1672 DEBUG_Normalize(curr_func
);
1673 if( linetab
[i
].linetab
== NULL
)
1675 curr_func
= DEBUG_AddSymbol( symname
, &new_addr
, NULL
,
1676 SYM_WIN32
| SYM_FUNC
);
1681 * First, create the entry. Then dig through the linetab
1682 * and add whatever line numbers are appropriate for this
1685 curr_func
= DEBUG_AddSymbol( symname
, &new_addr
,
1686 linetab
[i
].sourcefile
,
1687 SYM_WIN32
| SYM_FUNC
);
1688 for(j
=0; j
< linetab
[i
].nline
; j
++)
1690 if( linetab
[i
].offtab
[j
] >= sym
->proc
.offset
1691 && linetab
[i
].offtab
[j
] < sym
->proc
.offset
1692 + sym
->proc
.proc_len
)
1694 DEBUG_AddLineNumber(curr_func
, linetab
[i
].linetab
[j
],
1695 linetab
[i
].offtab
[j
] - sym
->proc
.offset
);
1702 * Add information about where we should set breakpoints
1705 DEBUG_SetSymbolBPOff(curr_func
, sym
->proc
.debug_start
);
1706 DEBUG_SetSymbolSize(curr_func
, sym
->proc
.proc_len
);
1710 * Function parameters and stack variables.
1712 memcpy(symname
, sym
->stack
.name
, sym
->stack
.namelen
);
1713 curr_sym
= DEBUG_AddLocal(curr_func
,
1719 DEBUG_SetLocalSymbolType(curr_sym
, DEBUG_GetCVType(sym
->stack
.symtype
));
1727 * Adjust pointer to point to next entry, rounding up to a word
1728 * boundary. MS preserving alignment? Stranger things have
1731 if( sym
->generic
.id
== S_PROCREF
1732 || sym
->generic
.id
== S_DATAREF
1733 || sym
->generic
.id
== S_UNKNOWN
)
1735 len
= (sym
->generic
.len
+ 3) & ~3;
1736 len
+= ptr
.c
[16] + 1;
1737 ptr
.c
+= (len
+ 3) & ~3;
1741 ptr
.c
+= (sym
->generic
.len
+ 3) & ~3;
1745 if( linetab
!= NULL
)
1755 * Process PDB file which contains debug information.
1757 * These are really weird beasts. They are intended to be incrementally
1758 * updated by the incremental linker, and this means that you need to
1759 * be able to remove and add information. Thus the PDB file is sort of
1760 * like a block structured device, with a freelist and lists of extent numbers
1761 * that are used to get the relevant pieces. In all cases seen so far, the
1762 * blocksize is always 0x400 bytes. The header has a field which apparently
1763 * holds the blocksize, so if it ever changes we are safe.
1765 * In general, every time we need to extract something from the pdb file,
1766 * it is easier to copy it into another buffer so we have the information
1767 * in one contiguous block rather than attempt to try and keep track of when
1768 * we need to grab another extent from the pdb file.
1770 * The thing that is a real pain about some MS stuff is that they choose
1771 * data structures which are not representable in C. Thus we have to
1772 * hack around and diddle pointers.
1776 DEBUG_ProcessPDBFile(struct deferred_debug_info
* deefer
, char * full_filename
)
1778 char * addr
= (char *) 0xffffffff;
1779 unsigned int blocksize
;
1780 unsigned int bufflen
= 0;
1781 char * buffer
= NULL
;
1782 unsigned short * extent_table
;
1784 struct file_ent
* fent
;
1786 struct file_list
* filelist
= NULL
;
1787 unsigned int gsym_record
= 0;
1788 char * gsymtab
= NULL
;
1789 struct filetab_hdr
* hd
;
1792 unsigned int last_extent
;
1793 struct codeview_linetab_hdr
* linetab
;
1794 unsigned int nblocks
;
1796 unsigned int offset
;
1797 struct codeview_pdb_hdr
* pdbhdr
;
1799 struct stat statbuf
;
1801 unsigned short * table
;
1803 unsigned int toc_blocks
;
1806 * FIXME - we should use some kind of search path mechanism to locate
1807 * PDB files. Right now we just look in the current working directory,
1808 * which works much of the time, I guess. Ideally we should be able to
1809 * map the filename back using the settings in wine.ini and perhaps
1810 * we could find it there. This bit of coding is left as an exercise
1811 * for the reader. :-).
1813 filename
= strrchr(full_filename
, '\\');
1814 if( filename
== NULL
)
1816 filename
= full_filename
;
1823 status
= stat(filename
, &statbuf
);
1826 fprintf(stderr
, "Unable to open .PDB file %s\n", filename
);
1831 * Now open the file, so that we can mmap() it.
1833 fd
= open(filename
, O_RDONLY
);
1836 fprintf(stderr
, "Unable to open .DBG file %s\n", filename
);
1842 * Now mmap() the file.
1844 addr
= mmap(0, statbuf
.st_size
, PROT_READ
,
1845 MAP_PRIVATE
, fd
, 0);
1846 if( addr
== (char *) 0xffffffff )
1848 fprintf(stderr
, "Unable to mmap .DBG file %s\n", filename
);
1853 * Now that we have the formalities over and done with, we need
1854 * to find the table of contents for the PDB file.
1856 pdbhdr
= (struct codeview_pdb_hdr
*) addr
;
1857 blocksize
= pdbhdr
->blocksize
;
1858 last_extent
= (statbuf
.st_size
+ blocksize
- 1) / blocksize
;
1861 * The TOC itself isn't always contiguous, so we need to extract a few
1862 * extents from the file to form the TOC.
1864 toc_blocks
= (pdbhdr
->toc_len
+ blocksize
- 1) / blocksize
;
1865 toc
= (char *) xmalloc(toc_blocks
* blocksize
);
1866 table
= pdbhdr
->toc_ext
;
1867 for(i
=0; i
< toc_blocks
; i
++)
1869 memcpy(toc
+ blocksize
*i
, addr
+ table
[i
]*blocksize
, blocksize
);
1873 * Next build our own table which will have the size and extent block
1874 * list for each record in the PDB file.
1876 * The TOC starts out with the number of files. Then it is followed by
1877 * (npair * 2*sizeof(int)) bytes of information, which are pairs of ints.
1878 * The first one is the size of the record (in bytes), and the second one
1879 * is something else which I haven't figured out yet.
1881 pnt
= (unsigned int *) toc
;
1883 extent_table
= (unsigned short *) ((unsigned int) toc
+
1884 npair
* 2 * sizeof(int) + sizeof(int));
1889 if( sizeof(int) + 2*sizeof(int)*npair
> pdbhdr
->toc_len
)
1894 filelist
= (struct file_list
*) xmalloc(npair
* sizeof(*filelist
));
1895 if( filelist
== NULL
)
1899 memset(filelist
, 0, npair
* sizeof(*filelist
));
1902 for(i
=0; i
< npair
; i
++)
1904 filelist
[i
].record_len
= pnt
[i
*2];
1905 filelist
[i
].nextents
= (filelist
[i
].record_len
+ blocksize
- 1)
1907 filelist
[i
].extent_list
= extent_table
+ nblocks
;
1908 nblocks
+= filelist
[i
].nextents
;
1911 * These get filled in later when we parse one of the records.
1913 filelist
[i
].linetab_offset
= 0;
1914 filelist
[i
].linetab_len
= 0;
1918 * OK, now walk through the various records and pick out the bits we
1919 * really want to see. Some of the records are extra special, and
1920 * we need to handle these a little bit differently.
1922 for(i
=0; i
< npair
; i
++)
1924 if( filelist
[i
].record_len
== 0xffffffff )
1930 * Make sure our buffer is large enough to hold the record.
1932 if( bufflen
< filelist
[i
].nextents
* blocksize
)
1934 bufflen
= filelist
[i
].nextents
* blocksize
;
1935 buffer
= (char *) realloc(buffer
, bufflen
);
1939 * Do this just for completeness. It makes debugging easier
1940 * if we have a clean indication of where the record ends.
1942 memset(buffer
, 0, filelist
[i
].nextents
* blocksize
);
1945 * Next, build the record using the extent list.
1947 for(j
=0; j
< filelist
[i
].nextents
; j
++)
1949 memcpy(buffer
+ j
* blocksize
,
1950 addr
+ filelist
[i
].extent_list
[j
] * blocksize
,
1954 pnt
= (unsigned int *) buffer
;
1957 * OK, now figure out what to do with it.
1961 * Always ignore the first entry. It seems to contain a backup copy
1962 * of the TOC (the last time the file was modified??)
1970 * The second entry as a id block. It contains a magic number
1971 * to identify the compiler, plus it also contains the timestamp
1972 * which must match the timestamp in the executable.
1977 if( ((*pnt
!= 19950623) && (*pnt
!= 19950814))
1978 || (filelist
[i
].record_len
!= 0x24)
1979 || (pnt
[1] != ((struct CodeViewDebug
*)(deefer
->dbg_info
))->cv_timestamp
) )
1986 * The third entry contains pointers to the global symbol table,
1987 * plus it also contains additional information about each record
1992 hd
= (struct filetab_hdr
*) buffer
;
1994 gsym_record
= hd
->gsym_file
;
1995 gsymtab
= (char *) xmalloc( filelist
[gsym_record
].nextents
1997 memset(gsymtab
, 0, filelist
[gsym_record
].nextents
* blocksize
);
1999 for(j
=0; j
< filelist
[gsym_record
].nextents
; j
++)
2001 memcpy(gsymtab
+ j
* blocksize
,
2002 addr
+ filelist
[gsym_record
].extent_list
[j
] * blocksize
,
2007 * This record also contains information about where in the
2008 * remaining records we will be able to find the start of the
2009 * line number table. We could locate that bit using heuristics,
2010 * but since we have the info handy, we might as well use it.
2012 offset
= sizeof(*hd
);
2015 fent
= (struct file_ent
*) (buffer
+ offset
);
2016 if( offset
> hd
->ftab_len
)
2021 if( fent
->file_number
== 0 || fent
->file_number
>= npair
)
2026 filelist
[fent
->file_number
].linetab_offset
=
2027 fent
->linetab_offset
;
2028 filelist
[fent
->file_number
].linetab_len
=
2031 * Figure out the offset of the next entry.
2032 * There is a fixed part of the record and a variable
2033 * length filename which we must also skip past.
2035 offset
+= ((unsigned int) &fent
->filename
- (unsigned int) fent
)
2036 + strlen(fent
->filename
) + 1;
2037 offset
+= strlen(buffer
+offset
) + 1;
2038 offset
= (offset
+ 3) & ~3;
2044 * Two different magic numbers used as dates.
2045 * These indicate the 'type' table.
2047 if( *pnt
== 19950410
2048 || *pnt
== 19951122 )
2050 DEBUG_ParseTypeTable(buffer
, filelist
[i
].record_len
);
2055 * This is something we really want to look at, since it contains
2056 * real debug info. Anything that doesn't match this can be
2062 * First, snag the line table, if we have one. This always
2063 * occurs at the end of the record, so we take the linetab
2064 * offset as the end of the normal part of the record.
2067 if( filelist
[i
].linetab_len
!= 0 )
2069 linetab
= DEBUG_SnarfLinetab(buffer
+ filelist
[i
].linetab_offset
,
2070 filelist
[i
].linetab_len
);
2071 DEBUG_SnarfCodeView(deefer
, buffer
,
2072 filelist
[i
].linetab_offset
,
2077 DEBUG_SnarfCodeView(deefer
, buffer
,
2078 filelist
[i
].record_len
,
2086 * Finally, process the global symbol table itself. There isn't
2087 * a line number component to this, so we just toss everything
2088 * into the mix and it all should work out.
2090 if( gsym_record
!= 0 )
2092 DEBUG_SnarfCodeView(deefer
, gsymtab
- sizeof(int),
2093 filelist
[gsym_record
].record_len
,
2099 if( gsymtab
!= NULL
)
2105 if( buffer
!= NULL
)
2110 if( filelist
!= NULL
)
2115 if( addr
!= (char *) 0xffffffff )
2117 munmap(addr
, statbuf
.st_size
);
2129 * Process DBG file which contains debug information.
2133 DEBUG_ProcessDBGFile(struct deferred_debug_info
* deefer
, char * filename
)
2135 char * addr
= (char *) 0xffffffff;
2137 struct CV4_DirHead
* codeview_dir
;
2138 struct CV4_DirEnt
* codeview_dent
;
2139 struct PE_Debug_dir
* dbghdr
;
2140 struct deferred_debug_info deefer2
;
2144 struct codeview_linetab_hdr
* linetab
;
2146 LPIMAGE_SEPARATE_DEBUG_HEADER pdbg
= NULL
;
2147 IMAGE_SECTION_HEADER
* sectp
;
2148 struct stat statbuf
;
2151 status
= stat(filename
, &statbuf
);
2154 fprintf(stderr
, "Unable to open .DBG file %s\n", filename
);
2159 * Now open the file, so that we can mmap() it.
2161 fd
= open(filename
, O_RDONLY
);
2164 fprintf(stderr
, "Unable to open .DBG file %s\n", filename
);
2170 * Now mmap() the file.
2172 addr
= mmap(0, statbuf
.st_size
, PROT_READ
,
2173 MAP_PRIVATE
, fd
, 0);
2174 if( addr
== (char *) 0xffffffff )
2176 fprintf(stderr
, "Unable to mmap .DBG file %s\n", filename
);
2180 pdbg
= (LPIMAGE_SEPARATE_DEBUG_HEADER
) addr
;
2182 if( pdbg
->TimeDateStamp
!= deefer
->dbgdir
->TimeDateStamp
)
2184 fprintf(stderr
, "Warning - %s has incorrect internal timestamp\n",
2189 fprintf(stderr
, "Processing symbols from %s...\n", filename
);
2191 dbghdr
= (struct PE_Debug_dir
*) ( addr
+ sizeof(*pdbg
)
2192 + pdbg
->NumberOfSections
* sizeof(IMAGE_SECTION_HEADER
)
2193 + pdbg
->ExportedNamesSize
);
2195 sectp
= (LPIMAGE_SECTION_HEADER
) ((char *) pdbg
+ sizeof(*pdbg
));
2196 nsect
= pdbg
->NumberOfSections
;
2198 for( i
=0; i
< pdbg
->DebugDirectorySize
/ sizeof(*pdbg
); i
++, dbghdr
++ )
2200 switch(dbghdr
->type
)
2202 case IMAGE_DEBUG_TYPE_COFF
:
2204 * Dummy up a deferred debug header to handle the
2205 * COFF stuff embedded within the DBG file.
2207 memset((char *) &deefer2
, 0, sizeof(deefer2
));
2208 deefer2
.dbg_info
= (addr
+ dbghdr
->dbgoff
);
2209 deefer2
.dbg_size
= dbghdr
->dbgsize
;
2210 deefer2
.load_addr
= deefer
->load_addr
;
2212 DEBUG_ProcessCoff(&deefer2
);
2214 case IMAGE_DEBUG_TYPE_CODEVIEW
:
2216 * This is the older format by which codeview stuff is
2217 * stored, known as the 'NB09' format. Newer executables
2218 * and dlls created by VC++ use PDB files instead, which
2219 * have lots of internal similarities, but the overall
2220 * format and structure is quite different.
2222 codeview
= (addr
+ dbghdr
->dbgoff
);
2225 * The first thing in the codeview section should be
2226 * an 'NB09' identifier. As a sanity check, make sure
2229 if( *((unsigned int*) codeview
) != 0x3930424e )
2235 * Next we need to find the directory. This is easy too.
2237 codeview_dir
= (struct CV4_DirHead
*)
2238 (codeview
+ ((unsigned int*) codeview
)[1]);
2241 * Some more sanity checks. Make sure that everything
2242 * is as we expect it.
2244 if( codeview_dir
->next_offset
!= 0
2245 || codeview_dir
->dhsize
!= sizeof(*codeview_dir
)
2246 || codeview_dir
->desize
!= sizeof(*codeview_dent
) )
2250 codeview_dent
= (struct CV4_DirEnt
*) (codeview_dir
+ 1);
2252 for(j
=0; j
< codeview_dir
->ndir
; j
++, codeview_dent
++)
2254 if( codeview_dent
->subsect_number
== sstAlignSym
)
2257 * Check the previous entry. If it is a
2258 * sstSrcModule, it contains the line number
2259 * info for this file.
2262 if( codeview_dent
[1].module_number
== codeview_dent
[0].module_number
2263 && codeview_dent
[1].subsect_number
== sstSrcModule
)
2265 linetab
= DEBUG_SnarfLinetab(
2266 codeview
+ codeview_dent
[1].offset
,
2267 codeview_dent
[1].size
);
2270 if( codeview_dent
[-1].module_number
== codeview_dent
[0].module_number
2271 && codeview_dent
[-1].subsect_number
== sstSrcModule
)
2273 linetab
= DEBUG_SnarfLinetab(
2274 codeview
+ codeview_dent
[-1].offset
,
2275 codeview_dent
[-1].size
);
2278 * Now process the CV stuff.
2280 DEBUG_SnarfCodeView(deefer
,
2281 codeview
+ codeview_dent
->offset
,
2282 codeview_dent
->size
,
2294 if( addr
!= (char *) 0xffffffff )
2296 munmap(addr
, statbuf
.st_size
);
2308 DEBUG_ProcessDeferredDebug()
2310 struct deferred_debug_info
* deefer
;
2311 struct CodeViewDebug
* cvd
;
2312 struct MiscDebug
* misc
;
2316 DEBUG_InitCVDataTypes();
2318 for(deefer
= dbglist
; deefer
; deefer
= deefer
->next
)
2320 if( deefer
->loaded
)
2325 if( last_proc
!= deefer
->dbg_index
)
2327 fprintf(stderr
, "Loading symbols from Win32 file %s...\n",
2328 deefer
->module_name
);
2329 last_proc
= deefer
->dbg_index
;
2332 switch(deefer
->dbgdir
->Type
)
2334 case IMAGE_DEBUG_TYPE_COFF
:
2336 * Standard COFF debug information that VC++ adds when you
2337 * use /debugtype:both with the linker.
2340 fprintf(stderr
, "Processing COFF symbols...\n");
2342 DEBUG_ProcessCoff(deefer
);
2344 case IMAGE_DEBUG_TYPE_CODEVIEW
:
2346 * This is a pointer to a PDB file of some sort.
2348 cvd
= (struct CodeViewDebug
*) deefer
->dbg_info
;
2350 if( strcmp(cvd
->cv_nbtype
, "NB10") != 0 )
2353 * Whatever this is, we don't know how to deal with
2358 DEBUG_ProcessPDBFile(deefer
, cvd
->cv_name
);
2360 fprintf(stderr
, "Processing PDB file %s\n", cvd
->cv_name
);
2363 case IMAGE_DEBUG_TYPE_MISC
:
2365 * A pointer to a .DBG file of some sort. These files
2366 * can contain either CV4 or COFF information. Open
2367 * the file, and try to do the right thing with it.
2369 misc
= (struct MiscDebug
*) deefer
->dbg_info
;
2371 filename
= strrchr((char *) &misc
->Data
, '.');
2374 * Ignore the file if it doesn't have a .DBG extension.
2376 if( (filename
== NULL
)
2377 || ( (strcmp(filename
, ".dbg") != 0)
2378 && (strcmp(filename
, ".DBG") != 0)) )
2383 filename
= (char *) &misc
->Data
;
2386 * Do the dirty deed...
2388 DEBUG_ProcessDBGFile(deefer
, filename
);
2393 * We should never get here...
2402 /***********************************************************************
2405 * Display shared libarary information.
2407 void DEBUG_InfoShare(void)
2409 struct deferred_debug_info
* deefer
;
2411 fprintf(stderr
,"Address\t\tModule\tName\n");
2413 for(deefer
= dbglist
; deefer
; deefer
= deefer
->next
)
2415 if( deefer
->pe
== NULL
)
2417 fprintf(stderr
,"0x%8.8x\t(ELF)\t%s\n",
2418 (unsigned int) deefer
->load_addr
,
2419 deefer
->module_name
);
2423 fprintf(stderr
,"0x%8.8x\t(Win32)\t%s\n",
2424 (unsigned int) deefer
->load_addr
,
2425 deefer
->module_name
);