2 * File msc.c - read VC++ debug information from COFF and eventually
5 * Copyright (C) 1996, Eric Youngdale.
7 * Note - this handles reading debug information for 32 bit applications
8 * that run under Windows-NT for example. I doubt that this would work well
9 * for 16 bit applications, but I don't think it really matters since the
10 * file format is different, and we should never get in here in such cases.
13 * Get 16 bit CV stuff working.
14 * Add symbol size to internal symbol table.
21 #include <sys/types.h>
22 #ifdef HAVE_SYS_MMAN_H
31 #define PATH_MAX _MAX_PATH
39 *dbg_filename must be at least MAX_PATHNAME_LEN bytes in size
41 static void LocateDebugInfoFile(char *filename, char *dbg_filename)
43 char *str1 = DBG_alloc(MAX_PATHNAME_LEN*10);
44 char *str2 = DBG_alloc(MAX_PATHNAME_LEN);
47 DOS_FULL_NAME fullname;
49 file = strrchr(filename, '\\');
50 if( file == NULL ) file = filename; else file++;
52 if (GetEnvironmentVariableA("_NT_SYMBOL_PATH", str1, MAX_PATHNAME_LEN))
53 if (SearchPathA(str1, file, NULL, MAX_PATHNAME_LEN*10, str2, &name_part))
55 if (GetEnvironmentVariableA("_NT_ALT_SYMBOL_PATH", str1, MAX_PATHNAME_LEN))
56 if (SearchPathA(str1, file, NULL, MAX_PATHNAME_LEN*10, str2, &name_part))
58 if (SearchPathA(NULL, file, NULL, MAX_PATHNAME_LEN*10, str2, &name_part))
63 memcpy(dbg_filename, filename, MAX_PATHNAME_LEN);
69 if (DOSFS_GetFullName(str2, TRUE, &fullname))
70 memcpy(dbg_filename, fullname.long_name, MAX_PATHNAME_LEN);
78 * This is an index we use to keep track of the debug information
79 * when we have multiple sources. We use the same database to also
80 * allow us to do an 'info shared' type of deal, and we use the index
81 * to eliminate duplicates.
83 static int DEBUG_next_index = 0;
94 * This is a convenience structure used to map portions of the
104 * This is how we reference the various record types.
106 union codeview_symbol
120 unsigned short symtype;
121 unsigned char namelen;
122 unsigned char name[1];
129 unsigned int pparent;
133 unsigned short segment;
134 unsigned short thunk_len;
135 unsigned char thtype;
136 unsigned char namelen;
137 unsigned char name[1];
143 unsigned int pparent;
146 unsigned int proc_len;
147 unsigned int debug_start;
148 unsigned int debug_end;
150 unsigned short segment;
151 unsigned short proctype;
153 unsigned char namelen;
154 unsigned char name[1];
158 short int len; /* Total length of this entry */
159 short int id; /* Always S_BPREL32 */
160 unsigned int offset; /* Stack offset relative to BP */
161 unsigned short symtype;
162 unsigned char namelen;
163 unsigned char name[1];
181 unsigned char variant[1];
189 unsigned char bitoff;
199 unsigned char arrlen;
200 unsigned char namelen;
201 unsigned char name[1];
213 unsigned short structlen;
214 unsigned char namelen;
215 unsigned char name[1];
225 unsigned short un_len;
226 unsigned char namelen;
227 unsigned char name[1];
238 unsigned char namelen;
239 unsigned char name[1];
246 unsigned short int value;
247 unsigned char namelen;
248 unsigned char name[1];
256 unsigned short int offset;
257 unsigned char namelen;
258 unsigned char name[1];
269 unsigned char namelen;
270 unsigned char name[1];
275 #define S_BPREL 0x200
276 #define S_LDATA 0x201
277 #define S_GDATA 0x202
279 #define S_LPROC 0x204
280 #define S_GPROC 0x205
281 #define S_THUNK 0x206
282 #define S_BLOCK 0x207
284 #define S_LABEL 0x209
286 #define S_PROCREF 0x400
287 #define S_DATAREF 0x401
288 #define S_ALIGN 0x402
289 #define S_UNKNOWN 0x403
292 * This covers the basic datatypes that VC++ seems to be using these days.
293 * 32 bit mode only. There are additional numbers for the pointers in 16
294 * bit mode. There are many other types listed in the documents, but these
295 * are apparently not used by the compiler, or represent pointer types
298 #define T_NOTYPE 0x0000 /* Notype */
299 #define T_ABS 0x0001 /* Abs */
300 #define T_VOID 0x0003 /* Void */
301 #define T_CHAR 0x0010 /* signed char */
302 #define T_SHORT 0x0011 /* short */
303 #define T_LONG 0x0012 /* long */
304 #define T_QUAD 0x0013 /* long long */
305 #define T_UCHAR 0x0020 /* unsigned char */
306 #define T_USHORT 0x0021 /* unsigned short */
307 #define T_ULONG 0x0022 /* unsigned long */
308 #define T_UQUAD 0x0023 /* unsigned long long */
309 #define T_REAL32 0x0040 /* float */
310 #define T_REAL64 0x0041 /* double */
311 #define T_RCHAR 0x0070 /* real char */
312 #define T_WCHAR 0x0071 /* wide char */
313 #define T_INT4 0x0074 /* int */
314 #define T_UINT4 0x0075 /* unsigned int */
316 #define T_32PVOID 0x0403 /* 32 bit near pointer to void */
317 #define T_32PCHAR 0x0410 /* 16:32 near pointer to signed char */
318 #define T_32PSHORT 0x0411 /* 16:32 near pointer to short */
319 #define T_32PLONG 0x0412 /* 16:32 near pointer to int */
320 #define T_32PQUAD 0x0413 /* 16:32 near pointer to long long */
321 #define T_32PUCHAR 0x0420 /* 16:32 near pointer to unsigned char */
322 #define T_32PUSHORT 0x0421 /* 16:32 near pointer to unsigned short */
323 #define T_32PULONG 0x0422 /* 16:32 near pointer to unsigned int */
324 #define T_32PUQUAD 0x0423 /* 16:32 near pointer to long long */
325 #define T_32PREAL32 0x0440 /* 16:32 near pointer to float */
326 #define T_32PREAL64 0x0441 /* 16:32 near pointer to float */
327 #define T_32PRCHAR 0x0470 /* 16:32 near pointer to real char */
328 #define T_32PWCHAR 0x0471 /* 16:32 near pointer to real char */
329 #define T_32PINT4 0x0474 /* 16:32 near pointer to int */
330 #define T_32PUINT4 0x0475 /* 16:32 near pointer to unsigned int */
332 #define LF_MODIFIER 0x1
333 #define LF_POINTER 0x2
336 #define LF_STRUCTURE 0x5
338 #define LF_ENUMERATION 0x7
339 #define LF_PROCEDURE 0x8
340 #define LF_MFUNCTION 0x9
341 #define LF_VTSHAPE 0xa
342 #define LF_BARRAY 0xd
343 #define LF_DIMARRAY 0x11
344 #define LF_VFTPATH 0x12
346 #define LF_SKIP 0x200
347 #define LF_ARGLIST 0x201
348 #define LF_FIELDLIST 0x204
349 #define LF_DERIVED 0x205
350 #define LF_BITFIELD 0x206
352 #define LF_BCLASS 0x400
353 #define LF_VBCLASS 0x401
354 #define LF_IVBCLASS 0x402
355 #define LF_ENUMERATE 0x403
356 #define LF_FRIENDFCN 0x404
357 #define LF_INDEX 0x405
358 #define LF_MEMBER 0x406
359 #define LF_STMEMBER 0x407
360 #define LF_METHOD 0x408
361 #define LF_NESTEDTYPE 0x409
362 #define LF_VFUNCTAB 0x40a
363 #define LF_FRIENDCLS 0x40b
364 #define LF_ONEMETHOD 0x40c
365 #define LF_FUNCOFF 0x40d
367 #define MAX_BUILTIN_TYPES 0x480
368 static struct datatype * cv_basic_types[MAX_BUILTIN_TYPES];
369 static int num_cv_defined_types = 0;
370 static struct datatype **cv_defined_types = NULL;
373 * For the type CODEVIEW debug directory entries, the debug directory
374 * points to a structure like this. The cv_name field is the name
375 * of an external .PDB file.
380 unsigned int cv_timestamp;
386 unsigned int DataType;
394 * This is the header that the COFF variety of debug header points to.
398 unsigned int SymbolOffset;
399 unsigned int N_Linenum;
400 unsigned int LinenumberOffset;
401 unsigned int Unused[4];
405 unsigned int VirtualAddr;
406 unsigned short int Linenum;
410 unsigned int startaddr;
411 unsigned int endaddr;
415 struct name_hash **entries;
425 unsigned int NotLong;
426 unsigned int StrTaboff;
433 unsigned char NumberOfAuxSymbols;
436 struct CoffAuxSection{
438 unsigned short NumberOfRelocations;
439 unsigned short NumberOfLinenumbers;
440 unsigned int CheckSum;
446 * These two structures are used in the directory within a .DBG file
447 * to locate the individual important bits that we might want to see.
450 short unsigned int dhsize;
451 short unsigned int desize;
453 unsigned int next_offset;
458 short unsigned int subsect_number;
459 short unsigned int module_number;
465 * These are the values of interest that the subsect_number field takes.
467 #define sstAlignSym 0x125
468 #define sstSrcModule 0x127
470 struct codeview_linetab_hdr
477 unsigned short * linetab;
478 unsigned int * offtab;
481 struct codeview_pdb_hdr
484 unsigned int blocksize; /* Extent size */
485 unsigned short loc_freelist; /* freelist. */
486 unsigned short alloc_filesize; /* # extents allocated. */
487 unsigned int toc_len;
488 unsigned int unknown;
489 unsigned short toc_ext[1]; /* array of extent #'s for toc. */
493 * This is our own structure that we use to keep track of the contents
500 short int * extent_list;
501 unsigned int linetab_offset;
502 unsigned int linetab_len;
506 * These are the structures that represent how the file table is set up
507 * within the PDB file.
511 unsigned short tab1_file;
512 unsigned short tab2_file;
513 unsigned short gsym_file;
514 unsigned short padding;
515 unsigned int ftab_len;
516 unsigned int fofftab_len;
517 unsigned int hash_len;
518 unsigned int strtab_len;
523 unsigned int reserved1;
524 unsigned short datasect_segment;
525 unsigned short reserved2;
526 unsigned int datasect_offset;
527 unsigned int datasect_size;
528 unsigned int datasect_flags;
529 unsigned short reserved3;
530 unsigned short index;
531 unsigned short num6a;
532 unsigned short file_number;
533 unsigned int linetab_offset;
534 unsigned int linetab_len;
538 unsigned char filename[1];
542 ********************************************************************
544 struct deferred_debug_info
546 struct deferred_debug_info * next;
552 PIMAGE_DEBUG_DIRECTORY dbgdir;
553 PIMAGE_SECTION_HEADER sectp;
559 struct deferred_debug_info * dbglist = NULL;
562 * A simple macro that tells us whether a given COFF symbol is a
565 #define N_TMASK 0x0030
566 #define IMAGE_SYM_DTYPE_FUNCTION 2
568 #define ISFCN(x) (((x) & N_TMASK) == (IMAGE_SYM_DTYPE_FUNCTION << N_BTSHFT))
572 * This is what we are looking for in the COFF symbols.
574 #define IMAGE_SYM_CLASS_EXTERNAL 0x2
575 #define IMAGE_SYM_CLASS_STATIC 0x3
576 #define IMAGE_SYM_CLASS_FILE 0x67
579 struct datatype * DEBUG_GetCVType(int typeno)
581 struct datatype * dt = NULL;
584 * Convert Codeview type numbers into something we can grok internally.
585 * Numbers < 0x1000 are all fixed builtin types. Numbers from 0x1000 and
586 * up are all user defined (structs, etc).
588 if( typeno < 0x1000 )
590 if( typeno < MAX_BUILTIN_TYPES )
592 dt = cv_basic_types[typeno];
597 if( typeno - 0x1000 < num_cv_defined_types )
599 dt = cv_defined_types[typeno - 0x1000];
607 DEBUG_ParseTypeTable(char * table, int len)
611 enum debug_type fieldtype;
615 struct datatype * subtype;
617 union codeview_type * type;
618 union codeview_type * type2;
619 struct datatype * typeptr;
623 ptr.c = (table + 16);
624 while( ptr.c - table < len )
626 type = (union codeview_type *) ptr.c;
628 if( curr_type - 0x1000 >= num_cv_defined_types )
630 num_cv_defined_types += 0x100;
631 cv_defined_types = (struct datatype **) DBG_realloc(cv_defined_types,
632 num_cv_defined_types * sizeof(struct datatype *));
633 memset(cv_defined_types + num_cv_defined_types - 0x100,
635 0x100 * sizeof(struct datatype *));
636 if( cv_defined_types == NULL )
642 switch(type->generic.id)
645 cv_defined_types[curr_type - 0x1000] =
646 DEBUG_FindOrMakePointerType(DEBUG_GetCVType(type->pointer.datatype));
649 if( type->array.arrlen >= 0x8000 )
652 * This is a numeric leaf, I am too lazy to handle this right
655 fprintf(stderr, "Ignoring large numberic leaf.\n");
658 if( type->array.namelen != 0 )
660 memset(symname, 0, sizeof(symname));
661 memcpy(symname, type->array.name, type->array.namelen);
662 typeptr = DEBUG_NewDataType(DT_ARRAY, symname);
666 typeptr = DEBUG_NewDataType(DT_ARRAY, NULL);
668 cv_defined_types[curr_type - 0x1000] = typeptr;
670 subtype = DEBUG_GetCVType(type->array.elemtype);
671 if( (subtype == NULL)
672 || (elem_size = DEBUG_GetObjectSize(subtype)) == 0 )
678 arr_max = type->array.arrlen / DEBUG_GetObjectSize(subtype);
681 DEBUG_SetArrayParams(typeptr, 0, arr_max, subtype);
685 * This is where the basic list of fields is defined for
686 * structures and classes.
688 * First, we need to look ahead and see whether we are building
689 * a fieldlist for an enum or a struct.
692 type2 = (union codeview_type *) ptr2.c;
693 if( type2->member.id == LF_MEMBER )
695 typeptr = DEBUG_NewDataType(DT_STRUCT, NULL);
696 fieldtype = DT_STRUCT;
698 else if( type2->member.id == LF_ENUMERATE )
700 typeptr = DEBUG_NewDataType(DT_ENUM, NULL);
708 cv_defined_types[curr_type - 0x1000] = typeptr;
709 while( ptr2.c < (ptr.c + ((type->generic.len + 3) & ~3)) )
711 type2 = (union codeview_type *) ptr2.c;
712 if( type2->member.id == LF_MEMBER && fieldtype == DT_STRUCT )
714 memset(symname, 0, sizeof(symname));
715 memcpy(symname, type2->member.name, type2->member.namelen);
717 subtype = DEBUG_GetCVType(type2->member.type);
719 if( subtype != NULL )
721 elem_size = DEBUG_GetObjectSize(subtype);
724 if( type2->member.offset >= 0x8000 )
727 * This is a numeric leaf, I am too lazy to handle this right
730 fprintf(stderr, "Ignoring large numberic leaf.\n");
734 DEBUG_AddStructElement(typeptr, symname, subtype,
735 type2->member.offset << 3,
739 else if( type2->member.id == LF_ENUMERATE && fieldtype == DT_ENUM )
741 memset(symname, 0, sizeof(symname));
742 memcpy(symname, type2->enumerate.name, type2->enumerate.namelen);
744 if( type2->enumerate.value >= 0x8000 )
747 * This is a numeric leaf, I am too lazy to handle this right
750 fprintf(stderr, "Ignoring large numberic leaf.\n");
754 DEBUG_AddStructElement(typeptr, symname, NULL,
755 type2->enumerate.value, 0);
761 * Something else I have never seen before. Either wrong type of
762 * object in the fieldlist, or some other problem which I wouldn't
763 * really know how to handle until it came up.
765 fprintf(stderr, "Unexpected entry in fieldlist\n");
770 ptr2.c += ((type2->member.namelen + 9 + 3) & ~3);
775 if( type->structure.structlen >= 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->structure.name, type->structure.namelen);
786 if( strcmp(symname, "__unnamed") == 0 )
788 typeptr = DEBUG_NewDataType(DT_STRUCT, NULL);
792 typeptr = DEBUG_NewDataType(DT_STRUCT, symname);
794 cv_defined_types[curr_type - 0x1000] = typeptr;
797 * Now copy the relevant bits from the fieldlist that we specified.
799 subtype = DEBUG_GetCVType(type->structure.fieldlist);
801 if( subtype != NULL )
803 DEBUG_SetStructSize(typeptr, type->structure.structlen);
804 DEBUG_CopyFieldlist(typeptr, subtype);
808 if( type->t_union.un_len >= 0x8000 )
811 * This is a numeric leaf, I am too lazy to handle this right
814 fprintf(stderr, "Ignoring large numberic leaf.\n");
817 memset(symname, 0, sizeof(symname));
818 memcpy(symname, type->t_union.name, type->t_union.namelen);
820 if( strcmp(symname, "__unnamed") == 0 )
822 typeptr = DEBUG_NewDataType(DT_STRUCT, NULL);
826 typeptr = DEBUG_NewDataType(DT_STRUCT, symname);
829 cv_defined_types[curr_type - 0x1000] = typeptr;
832 * Now copy the relevant bits from the fieldlist that we specified.
834 subtype = DEBUG_GetCVType(type->t_union.field);
836 if( subtype != NULL )
838 DEBUG_SetStructSize(typeptr, type->t_union.un_len);
839 DEBUG_CopyFieldlist(typeptr, subtype);
843 typeptr = DEBUG_NewDataType(DT_BITFIELD, NULL);
844 cv_defined_types[curr_type - 0x1000] = typeptr;
845 DEBUG_SetBitfieldParams(typeptr, type->bitfield.bitoff,
846 type->bitfield.nbits,
847 DEBUG_GetCVType(type->bitfield.type));
850 memset(symname, 0, sizeof(symname));
851 memcpy(symname, type->enumeration.name, type->enumeration.namelen);
852 typeptr = DEBUG_NewDataType(DT_ENUM, symname);
853 cv_defined_types[curr_type - 0x1000] = typeptr;
856 * Now copy the relevant bits from the fieldlist that we specified.
858 subtype = DEBUG_GetCVType(type->enumeration.field);
860 if( subtype != NULL )
862 DEBUG_CopyFieldlist(typeptr, subtype);
870 ptr.c += (type->generic.len + 3) & ~3;
877 DEBUG_InitCVDataTypes()
880 * These are the common builtin types that are used by VC++.
882 cv_basic_types[T_NOTYPE] = NULL;
883 cv_basic_types[T_ABS] = NULL;
884 cv_basic_types[T_VOID] = DEBUG_NewDataType(DT_BASIC, "void");
885 cv_basic_types[T_CHAR] = DEBUG_NewDataType(DT_BASIC, "char");
886 cv_basic_types[T_SHORT] = DEBUG_NewDataType(DT_BASIC, "short int");
887 cv_basic_types[T_LONG] = DEBUG_NewDataType(DT_BASIC, "long int");
888 cv_basic_types[T_QUAD] = DEBUG_NewDataType(DT_BASIC, "long long int");
889 cv_basic_types[T_UCHAR] = DEBUG_NewDataType(DT_BASIC, "unsigned char");
890 cv_basic_types[T_USHORT] = DEBUG_NewDataType(DT_BASIC, "short unsigned int");
891 cv_basic_types[T_ULONG] = DEBUG_NewDataType(DT_BASIC, "long unsigned int");
892 cv_basic_types[T_UQUAD] = DEBUG_NewDataType(DT_BASIC, "long long unsigned int");
893 cv_basic_types[T_REAL32] = DEBUG_NewDataType(DT_BASIC, "float");
894 cv_basic_types[T_REAL64] = DEBUG_NewDataType(DT_BASIC, "double");
895 cv_basic_types[T_RCHAR] = DEBUG_NewDataType(DT_BASIC, "char");
896 cv_basic_types[T_WCHAR] = DEBUG_NewDataType(DT_BASIC, "short");
897 cv_basic_types[T_INT4] = DEBUG_NewDataType(DT_BASIC, "int");
898 cv_basic_types[T_UINT4] = DEBUG_NewDataType(DT_BASIC, "unsigned int");
900 cv_basic_types[T_32PVOID] = DEBUG_FindOrMakePointerType(cv_basic_types[T_VOID]);
901 cv_basic_types[T_32PCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_CHAR]);
902 cv_basic_types[T_32PSHORT] = DEBUG_FindOrMakePointerType(cv_basic_types[T_SHORT]);
903 cv_basic_types[T_32PLONG] = DEBUG_FindOrMakePointerType(cv_basic_types[T_LONG]);
904 cv_basic_types[T_32PQUAD] = DEBUG_FindOrMakePointerType(cv_basic_types[T_QUAD]);
905 cv_basic_types[T_32PUCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_UCHAR]);
906 cv_basic_types[T_32PUSHORT] = DEBUG_FindOrMakePointerType(cv_basic_types[T_USHORT]);
907 cv_basic_types[T_32PULONG] = DEBUG_FindOrMakePointerType(cv_basic_types[T_ULONG]);
908 cv_basic_types[T_32PUQUAD] = DEBUG_FindOrMakePointerType(cv_basic_types[T_UQUAD]);
909 cv_basic_types[T_32PREAL32] = DEBUG_FindOrMakePointerType(cv_basic_types[T_REAL32]);
910 cv_basic_types[T_32PREAL64] = DEBUG_FindOrMakePointerType(cv_basic_types[T_REAL64]);
911 cv_basic_types[T_32PRCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_RCHAR]);
912 cv_basic_types[T_32PWCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_WCHAR]);
913 cv_basic_types[T_32PINT4] = DEBUG_FindOrMakePointerType(cv_basic_types[T_INT4]);
914 cv_basic_types[T_32PUINT4] = DEBUG_FindOrMakePointerType(cv_basic_types[T_UINT4]);
918 * In this function, we keep track of deferred debugging information
919 * that we may need later if we were to need to use the internal debugger.
920 * We don't fully process it here for performance reasons.
923 DEBUG_RegisterDebugInfo( HMODULE hModule, const char *module_name)
925 int has_codeview = FALSE;
928 PIMAGE_DEBUG_DIRECTORY dbgptr;
930 PIMAGE_NT_HEADERS nth = PE_HEADER(hModule);
932 size = nth->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].Size;
934 v_addr = nth->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress;
935 dbgptr = (PIMAGE_DEBUG_DIRECTORY) (hModule + v_addr);
937 for(; size >= sizeof(*dbgptr); size -= sizeof(*dbgptr), dbgptr++ )
941 case IMAGE_DEBUG_TYPE_CODEVIEW:
942 case IMAGE_DEBUG_TYPE_MISC:
949 dbgptr = (PIMAGE_DEBUG_DIRECTORY) (hModule + v_addr);
950 for(; size >= sizeof(*dbgptr); size -= sizeof(*dbgptr), dbgptr++ )
954 case IMAGE_DEBUG_TYPE_COFF:
956 * If we have both codeview and COFF debug info, ignore the
957 * coff debug info as it would just confuse us, and it is
960 * FIXME - this is broken - if we cannot find the PDB file, then
961 * we end up with no debugging info at all. In this case, we
962 * should use the COFF info as a backup.
968 case IMAGE_DEBUG_TYPE_CODEVIEW:
969 case IMAGE_DEBUG_TYPE_MISC:
971 * This is usually an indirection to a .DBG file.
972 * This is similar to (but a slightly older format) from the
975 * First check to see if the image was 'stripped'. If so, it
976 * means that this entry points to a .DBG file. Otherwise,
977 * it just points to itself, and we can ignore this.
985 if( (dbgptr->Type != IMAGE_DEBUG_TYPE_MISC) ||
986 (PE_HEADER(hModule)->FileHeader.Characteristics & IMAGE_FILE_DEBUG_STRIPPED) != 0 )
990 DOS_FULL_NAME full_name;
991 struct deferred_debug_info* deefer = (struct deferred_debug_info *) DBG_alloc(sizeof(*deefer));
993 deefer->module = hModule;
994 deefer->load_addr = (char *)hModule;
997 * Read the important bits. What we do after this depends
998 * upon the type, but this is always enough so we are able
999 * to proceed if we know what we need to do next.
1001 /* basically, the PE loader maps all sections (data, resources...), but doesn't map
1002 * the DataDirectory array's content. One its entry contains the *beloved*
1003 * debug information. (Note the DataDirectory is mapped, not its content)
1006 if (GetModuleFileNameA(hModule, fn, sizeof(fn)) > 0 &&
1007 DOSFS_GetFullName(fn, TRUE, &full_name) &&
1008 (fd = open(full_name.long_name, O_RDONLY)) > 0)
1010 deefer->dbg_info = mmap(NULL, dbgptr->SizeOfData,
1011 PROT_READ, MAP_PRIVATE, fd, dbgptr->PointerToRawData);
1013 if( deefer->dbg_info == (char *) 0xffffffff )
1022 fprintf(stderr, " (not mapped: fn=%s, lfn=%s, fd=%d)", fn, full_name.long_name, fd);
1025 deefer->dbg_size = dbgptr->SizeOfData;
1026 deefer->dbgdir = dbgptr;
1027 deefer->next = dbglist;
1028 deefer->loaded = FALSE;
1029 deefer->dbg_index = DEBUG_next_index;
1030 deefer->module_name = DBG_strdup(module_name);
1032 deefer->sectp = PE_SECTIONS(hModule);
1033 deefer->nsect = PE_HEADER(hModule)->FileHeader.NumberOfSections;
1045 /* look for .stabs/.stabstr sections */
1047 PIMAGE_SECTION_HEADER pe_seg = PE_SECTIONS(hModule);
1048 int i,stabsize=0,stabstrsize=0;
1049 unsigned int stabs=0,stabstr=0;
1051 for (i=0;i<nth->FileHeader.NumberOfSections;i++) {
1052 if (!strcasecmp(pe_seg[i].Name,".stab")) {
1053 stabs = pe_seg[i].VirtualAddress;
1054 stabsize = pe_seg[i].SizeOfRawData;
1056 if (!strncasecmp(pe_seg[i].Name,".stabstr",8)) {
1057 stabstr = pe_seg[i].VirtualAddress;
1058 stabstrsize = pe_seg[i].SizeOfRawData;
1061 if (stabstrsize && stabsize) {
1062 #ifdef _WE_SUPPORT_THE_STAB_TYPES_USED_BY_MINGW_TOO
1063 /* Won't work currently, since MINGW32 uses some special typedefs
1064 * which we do not handle yet. Support for them is a bit difficult.
1066 DEBUG_ParseStabs(hModule,0,stabs,stabsize,stabstr,stabstrsize);
1068 fprintf(stderr,"(stabs not loaded)");
1075 * ELF modules are also entered into the list - this is so that we
1076 * can make 'info shared' types of displays possible.
1079 DEBUG_RegisterELFDebugInfo(int load_addr, u_long size, char * name)
1081 struct deferred_debug_info * deefer;
1083 deefer = (struct deferred_debug_info *) DBG_alloc(sizeof(*deefer));
1087 * Read the important bits. What we do after this depends
1088 * upon the type, but this is always enough so we are able
1089 * to proceed if we know what we need to do next.
1091 deefer->dbg_size = size;
1092 deefer->dbg_info = (char *) NULL;
1094 deefer->load_addr = (char *) load_addr;
1095 deefer->dbgdir = NULL;
1096 deefer->next = dbglist;
1097 deefer->loaded = TRUE;
1098 deefer->dbg_index = DEBUG_next_index;
1099 deefer->module_name = DBG_strdup(name);
1110 * Process COFF debugging information embedded in a Win32 application.
1115 DEBUG_ProcessCoff(struct deferred_debug_info * deefer)
1117 struct CoffAuxSection * aux;
1118 struct CoffDebug * coff;
1119 struct CoffFiles * coff_files = NULL;
1120 struct CoffLinenum * coff_linetab;
1122 struct CoffSymbol * coff_sym;
1123 struct CoffSymbol * coff_symbol;
1124 struct CoffFiles * curr_file = NULL;
1128 struct CoffLinenum * linepnt;
1135 int nfiles_alloc = 0;
1136 struct CoffFiles orig_file;
1138 char * this_file = NULL;
1140 coff = (struct CoffDebug *) deefer->dbg_info;
1142 coff_symbol = (struct CoffSymbol *) ((unsigned int) coff + coff->SymbolOffset);
1143 coff_linetab = (struct CoffLinenum *) ((unsigned int) coff + coff->LinenumberOffset);
1144 coff_strtab = (char *) ((unsigned int) coff_symbol + 18*coff->N_Sym);
1148 for(i=0; i < coff->N_Sym; i++ )
1151 * We do this because some compilers (i.e. gcc) incorrectly
1152 * pad the structure up to a 4 byte boundary. The structure
1153 * is really only 18 bytes long, so we have to manually make sure
1156 * FIXME - there must be a way to have autoconf figure out the
1157 * correct compiler option for this. If it is always gcc, that
1158 * makes life simpler, but I don't want to force this.
1160 coff_sym = (struct CoffSymbol *) ((unsigned int) coff_symbol + 18*i);
1161 naux = coff_sym->NumberOfAuxSymbols;
1163 if( coff_sym->StorageClass == IMAGE_SYM_CLASS_FILE )
1165 if( nfiles + 1 >= nfiles_alloc )
1168 coff_files = (struct CoffFiles *) DBG_realloc(coff_files,
1169 nfiles_alloc * sizeof(struct CoffFiles));
1171 curr_file = coff_files + nfiles;
1173 curr_file->startaddr = 0xffffffff;
1174 curr_file->endaddr = 0;
1175 curr_file->filename = ((char *) coff_sym) + 18;
1176 curr_file->linetab_offset = -1;
1177 curr_file->linecnt = 0;
1178 curr_file->entries = NULL;
1179 curr_file->neps = curr_file->neps_alloc = 0;
1181 fprintf(stderr,"New file %s\n", curr_file->filename);
1188 * This guy marks the size and location of the text section
1189 * for the current file. We need to keep track of this so
1190 * we can figure out what file the different global functions
1193 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC)
1195 && (coff_sym->Type == 0)
1196 && (coff_sym->SectionNumber == 1) )
1198 aux = (struct CoffAuxSection *) ((unsigned int) coff_sym + 18);
1200 if( curr_file->linetab_offset != -1 )
1203 fprintf(stderr, "Duplicating sect from %s: %x %x %x %d %d\n",
1204 curr_file->filename,
1206 aux->NumberOfRelocations,
1207 aux->NumberOfLinenumbers,
1210 fprintf(stderr, "More sect %d %x %d %d %d\n",
1211 coff_sym->SectionNumber,
1214 coff_sym->StorageClass,
1215 coff_sym->NumberOfAuxSymbols);
1219 * Save this so we can copy bits from it.
1221 orig_file = *curr_file;
1224 * Duplicate the file entry. We have no way to describe
1225 * multiple text sections in our current way of handling things.
1227 if( nfiles + 1 >= nfiles_alloc )
1230 coff_files = (struct CoffFiles *) DBG_realloc(coff_files,
1231 nfiles_alloc * sizeof(struct CoffFiles));
1233 curr_file = coff_files + nfiles;
1235 curr_file->startaddr = 0xffffffff;
1236 curr_file->endaddr = 0;
1237 curr_file->filename = orig_file.filename;
1238 curr_file->linetab_offset = -1;
1239 curr_file->linecnt = 0;
1240 curr_file->entries = NULL;
1241 curr_file->neps = curr_file->neps_alloc = 0;
1246 fprintf(stderr, "New text sect from %s: %x %x %x %d %d\n",
1247 curr_file->filename,
1249 aux->NumberOfRelocations,
1250 aux->NumberOfLinenumbers,
1256 if( curr_file->startaddr > coff_sym->Value )
1258 curr_file->startaddr = coff_sym->Value;
1261 if( curr_file->startaddr > coff_sym->Value )
1263 curr_file->startaddr = coff_sym->Value;
1266 if( curr_file->endaddr < coff_sym->Value + aux->Length )
1268 curr_file->endaddr = coff_sym->Value + aux->Length;
1271 curr_file->linetab_offset = linetab_indx;
1272 curr_file->linecnt = aux->NumberOfLinenumbers;
1273 linetab_indx += aux->NumberOfLinenumbers;
1278 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC)
1280 && (coff_sym->SectionNumber == 1) )
1283 * This is a normal static function when naux == 0.
1284 * Just register it. The current file is the correct
1285 * one in this instance.
1287 if( coff_sym->N.Name.NotLong )
1289 memcpy(namebuff, coff_sym->N.ShortName, 8);
1291 nampnt = &namebuff[0];
1295 nampnt = coff_strtab + coff_sym->N.Name.StrTaboff;
1298 if( nampnt[0] == '_' )
1304 new_addr.off = (int) (deefer->load_addr + coff_sym->Value);
1306 if( curr_file->neps + 1 >= curr_file->neps_alloc )
1308 curr_file->neps_alloc += 10;
1309 curr_file->entries = (struct name_hash **)
1310 DBG_realloc(curr_file->entries,
1311 curr_file->neps_alloc * sizeof(struct name_hash *));
1314 fprintf(stderr,"\tAdding static symbol %s\n", nampnt);
1316 curr_file->entries[curr_file->neps++] =
1317 DEBUG_AddSymbol( nampnt, &new_addr, this_file, SYM_WIN32 );
1322 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL)
1323 && ISFCN(coff_sym->Type)
1324 && (coff_sym->SectionNumber > 0) )
1326 if( coff_sym->N.Name.NotLong )
1328 memcpy(namebuff, coff_sym->N.ShortName, 8);
1330 nampnt = &namebuff[0];
1334 nampnt = coff_strtab + coff_sym->N.Name.StrTaboff;
1338 if( nampnt[0] == '_' )
1344 new_addr.off = (int) (deefer->load_addr + coff_sym->Value);
1347 fprintf(stderr, "%d: %x %s\n", i, new_addr.off, nampnt);
1349 fprintf(stderr,"\tAdding global symbol %s\n", nampnt);
1353 * Now we need to figure out which file this guy belongs to.
1356 for(j=0; j < nfiles; j++)
1358 if( coff_files[j].startaddr <= coff_sym->Value
1359 && coff_files[j].endaddr > coff_sym->Value )
1361 this_file = coff_files[j].filename;
1365 if( coff_files[j].neps + 1 >= coff_files[j].neps_alloc )
1367 coff_files[j].neps_alloc += 10;
1368 coff_files[j].entries = (struct name_hash **)
1369 DBG_realloc(coff_files[j].entries,
1370 coff_files[j].neps_alloc * sizeof(struct name_hash *));
1372 coff_files[j].entries[coff_files[j].neps++] =
1373 DEBUG_AddSymbol( nampnt, &new_addr, this_file, SYM_WIN32 );
1378 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL)
1379 && (coff_sym->SectionNumber > 0) )
1382 * Similar to above, but for the case of data symbols.
1383 * These aren't treated as entrypoints.
1385 if( coff_sym->N.Name.NotLong )
1387 memcpy(namebuff, coff_sym->N.ShortName, 8);
1389 nampnt = &namebuff[0];
1393 nampnt = coff_strtab + coff_sym->N.Name.StrTaboff;
1397 if( nampnt[0] == '_' )
1403 new_addr.off = (int) (deefer->load_addr + coff_sym->Value);
1406 fprintf(stderr, "%d: %x %s\n", i, new_addr.off, nampnt);
1408 fprintf(stderr,"\tAdding global data symbol %s\n", nampnt);
1412 * Now we need to figure out which file this guy belongs to.
1414 DEBUG_AddSymbol( nampnt, &new_addr, NULL, SYM_WIN32 );
1419 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC)
1423 * Ignore these. They don't have anything to do with
1431 fprintf(stderr,"Skipping unknown entry %d %d %d\n", coff_sym->StorageClass,
1432 coff_sym->SectionNumber, naux);
1436 * For now, skip past the aux entries.
1443 * OK, we now should have a list of files, and we should have a list
1444 * of entrypoints. We need to sort the entrypoints so that we are
1445 * able to tie the line numbers with the given functions within the
1448 if( coff_files != NULL )
1450 for(j=0; j < nfiles; j++)
1452 if( coff_files[j].entries != NULL )
1454 qsort(coff_files[j].entries, coff_files[j].neps,
1455 sizeof(struct name_hash *), DEBUG_cmp_sym);
1460 * Now pick apart the line number tables, and attach the entries
1461 * to the given functions.
1463 for(j=0; j < nfiles; j++)
1466 if( coff_files[j].neps != 0 )
1467 for(k=0; k < coff_files[j].linecnt; k++)
1470 * Another monstrosity caused by the fact that we are using
1471 * a 6 byte structure, and gcc wants to pad structures to 4 byte
1472 * boundaries. Otherwise we could just index into an array.
1474 linepnt = (struct CoffLinenum *)
1475 ((unsigned int) coff_linetab +
1476 6*(coff_files[j].linetab_offset + k));
1478 * If we have spilled onto the next entrypoint, then
1479 * bump the counter..
1483 if (i+1 >= coff_files[j].neps) break;
1484 DEBUG_GetSymbolAddr(coff_files[j].entries[i+1], &new_addr);
1485 if( (((unsigned int)deefer->load_addr +
1486 linepnt->VirtualAddr) >= new_addr.off) )
1493 * Add the line number. This is always relative to the
1494 * start of the function, so we need to subtract that offset
1497 DEBUG_GetSymbolAddr(coff_files[j].entries[i], &new_addr);
1498 DEBUG_AddLineNumber(coff_files[j].entries[i],
1500 (unsigned int) deefer->load_addr
1501 + linepnt->VirtualAddr
1509 if( coff_files != NULL )
1511 for(j=0; j < nfiles; j++)
1513 if( coff_files[j].entries != NULL )
1515 DBG_free(coff_files[j].entries);
1518 DBG_free(coff_files);
1526 * Process a codeview line number table. Digestify the thing so that
1527 * we can easily reference the thing when we process the rest of
1530 static struct codeview_linetab_hdr *
1531 DEBUG_SnarfLinetab(char * linetab,
1535 char filename[PATH_MAX];
1536 unsigned int * filetab;
1540 struct codeview_linetab_hdr * lt_hdr;
1541 unsigned int * lt_ptr;
1545 union any_size pnt2;
1546 struct startend * start;
1550 * Now get the important bits.
1556 filetab = (unsigned int *) pnt.c;
1559 * Now count up the number of segments in the file.
1562 for(i=0; i<nfile; i++)
1564 pnt2.c = linetab + filetab[i];
1569 * Next allocate the header we will be returning.
1570 * There is one header for each segment, so that we can reach in
1571 * and pull bits as required.
1573 lt_hdr = (struct codeview_linetab_hdr *)
1574 DBG_alloc((nseg + 1) * sizeof(*lt_hdr));
1575 if( lt_hdr == NULL )
1580 memset(lt_hdr, 0, sizeof(*lt_hdr) * (nseg+1));
1583 * Now fill the header we will be returning, one for each segment.
1584 * Note that this will basically just contain pointers into the existing
1585 * line table, and we do not actually copy any additional information
1586 * or allocate any additional memory.
1590 for(i=0; i<nfile; i++)
1593 * Get the pointer into the segment information.
1595 pnt2.c = linetab + filetab[i];
1596 file_segcount = *pnt2.s;
1599 lt_ptr = (unsigned int *) pnt2.c;
1600 start = (struct startend *) (lt_ptr + file_segcount);
1603 * Now snarf the filename for all of the segments for this file.
1605 fn = (unsigned char *) (start + file_segcount);
1606 memset(filename, 0, sizeof(filename));
1607 memcpy(filename, fn + 1, *fn);
1608 fn = DBG_strdup(filename);
1610 for(k = 0; k < file_segcount; k++, this_seg++)
1612 pnt2.c = linetab + lt_ptr[k];
1613 lt_hdr[this_seg].start = start[k].start;
1614 lt_hdr[this_seg].end = start[k].end;
1615 lt_hdr[this_seg].sourcefile = fn;
1616 lt_hdr[this_seg].segno = *pnt2.s++;
1617 lt_hdr[this_seg].nline = *pnt2.s++;
1618 lt_hdr[this_seg].offtab = pnt2.ui;
1619 lt_hdr[this_seg].linetab = (unsigned short *)
1620 (pnt2.ui + lt_hdr[this_seg].nline);
1631 DEBUG_SnarfCodeView( struct deferred_debug_info * deefer,
1634 struct codeview_linetab_hdr * linetab)
1636 struct name_hash * curr_func = NULL;
1637 struct wine_locals * curr_sym = NULL;
1644 IMAGE_SECTION_HEADER * sectp;
1645 union codeview_symbol * sym;
1646 char symname[PATH_MAX];
1647 struct name_hash * thunk_sym = NULL;
1650 nsect = deefer->nsect;
1651 sectp = deefer->sectp;
1654 * Skip over the first word. Don't really know what it means, but
1660 * Loop over the different types of records and whenever we
1661 * find something we are interested in, record it and move on.
1663 while( ptr.c - cv_data < size )
1665 sym = (union codeview_symbol *) ptr.c;
1667 if( sym->generic.len - sizeof(int) == (ptr.c - cv_data) )
1670 * This happens when we have indirect symbols that VC++ 4.2
1671 * sometimes uses when there isn't a line number table.
1672 * We ignore it - we will process and enter all of the
1673 * symbols in the global symbol table anyways, so there
1674 * isn't much point in keeping track of all of this crap.
1679 memset(symname, 0, sizeof(symname));
1680 switch(sym->generic.id)
1686 * First, a couple of sanity checks.
1688 if( sym->data.namelen == 0 )
1693 if( sym->data.seg == 0 || sym->data.seg > nsect )
1699 * Global and local data symbols. We don't associate these
1700 * with any given source file.
1703 memcpy(symname, sym->data.name, sym->data.namelen);
1705 new_addr.type = DEBUG_GetCVType(sym->data.symtype);
1706 new_addr.off = (unsigned int) deefer->load_addr +
1707 sectp[sym->data.seg - 1].VirtualAddress +
1709 DEBUG_AddSymbol( symname, &new_addr, NULL, SYM_WIN32 | SYM_DATA );
1713 * Sort of like a global function, but it just points
1714 * to a thunk, which is a stupid name for what amounts to
1715 * a PLT slot in the normal jargon that everyone else uses.
1717 memcpy(symname, sym->thunk.name, sym->thunk.namelen);
1719 new_addr.type = NULL;
1720 new_addr.off = (unsigned int) deefer->load_addr +
1721 sectp[sym->thunk.segment - 1].VirtualAddress +
1723 thunk_sym = DEBUG_AddSymbol( symname, &new_addr, NULL,
1724 SYM_WIN32 | SYM_FUNC);
1725 DEBUG_SetSymbolSize(thunk_sym, sym->thunk.thunk_len);
1730 * Global and static functions.
1732 memcpy(symname, sym->proc.name, sym->proc.namelen);
1734 new_addr.type = DEBUG_GetCVType(sym->proc.proctype);
1735 new_addr.off = (unsigned int) deefer->load_addr +
1736 sectp[sym->proc.segment - 1].VirtualAddress +
1739 * See if we can find a segment that this goes with. If so,
1740 * it means that we also may have line number information
1741 * for this function.
1743 for(i=0; linetab[i].linetab != NULL; i++)
1745 if( ((unsigned int) deefer->load_addr
1746 + sectp[linetab[i].segno - 1].VirtualAddress
1747 + linetab[i].start <= new_addr.off)
1748 && ((unsigned int) deefer->load_addr
1749 + sectp[linetab[i].segno - 1].VirtualAddress
1750 + linetab[i].end > new_addr.off) )
1756 DEBUG_Normalize(curr_func);
1757 if( linetab[i].linetab == NULL )
1759 curr_func = DEBUG_AddSymbol( symname, &new_addr, NULL,
1760 SYM_WIN32 | SYM_FUNC);
1765 * First, create the entry. Then dig through the linetab
1766 * and add whatever line numbers are appropriate for this
1769 curr_func = DEBUG_AddSymbol( symname, &new_addr,
1770 linetab[i].sourcefile,
1771 SYM_WIN32 | SYM_FUNC);
1772 for(j=0; j < linetab[i].nline; j++)
1774 if( linetab[i].offtab[j] >= sym->proc.offset
1775 && linetab[i].offtab[j] < sym->proc.offset
1776 + sym->proc.proc_len )
1778 DEBUG_AddLineNumber(curr_func, linetab[i].linetab[j],
1779 linetab[i].offtab[j] - sym->proc.offset);
1786 * Add information about where we should set breakpoints
1789 DEBUG_SetSymbolBPOff(curr_func, sym->proc.debug_start);
1790 DEBUG_SetSymbolSize(curr_func, sym->proc.proc_len);
1794 * Function parameters and stack variables.
1796 memcpy(symname, sym->stack.name, sym->stack.namelen);
1797 curr_sym = DEBUG_AddLocal(curr_func,
1803 DEBUG_SetLocalSymbolType(curr_sym, DEBUG_GetCVType(sym->stack.symtype));
1811 * Adjust pointer to point to next entry, rounding up to a word
1812 * boundary. MS preserving alignment? Stranger things have
1815 if( sym->generic.id == S_PROCREF
1816 || sym->generic.id == S_DATAREF
1817 || sym->generic.id == S_UNKNOWN )
1819 len = (sym->generic.len + 3) & ~3;
1820 len += ptr.c[16] + 1;
1821 ptr.c += (len + 3) & ~3;
1825 ptr.c += (sym->generic.len + 3) & ~3;
1829 if( linetab != NULL )
1839 * Process PDB file which contains debug information.
1841 * These are really weird beasts. They are intended to be incrementally
1842 * updated by the incremental linker, and this means that you need to
1843 * be able to remove and add information. Thus the PDB file is sort of
1844 * like a block structured device, with a freelist and lists of extent numbers
1845 * that are used to get the relevant pieces. In all cases seen so far, the
1846 * blocksize is always 0x400 bytes. The header has a field which apparently
1847 * holds the blocksize, so if it ever changes we are safe.
1849 * In general, every time we need to extract something from the pdb file,
1850 * it is easier to copy it into another buffer so we have the information
1851 * in one contiguous block rather than attempt to try and keep track of when
1852 * we need to grab another extent from the pdb file.
1854 * The thing that is a real pain about some MS stuff is that they choose
1855 * data structures which are not representable in C. Thus we have to
1856 * hack around and diddle pointers.
1860 DEBUG_ProcessPDBFile(struct deferred_debug_info * deefer, char * full_filename)
1862 char * addr = (char *) 0xffffffff;
1863 unsigned int blocksize;
1864 unsigned int bufflen = 0;
1865 char * buffer = NULL;
1866 unsigned short * extent_table;
1868 struct file_ent * fent;
1869 char filename[MAX_PATHNAME_LEN];
1870 struct file_list * filelist = NULL;
1871 unsigned int gsym_record = 0;
1872 char * gsymtab = NULL;
1873 struct filetab_hdr * hd;
1876 unsigned int last_extent;
1877 struct codeview_linetab_hdr * linetab;
1878 unsigned int nblocks;
1880 unsigned int offset;
1881 struct codeview_pdb_hdr * pdbhdr;
1883 struct stat statbuf;
1885 unsigned short * table;
1887 unsigned int toc_blocks;
1889 LocateDebugInfoFile(full_filename, filename);
1890 status = stat(filename, &statbuf);
1893 fprintf(stderr, "-Unable to open .PDB file %s\n", filename);
1898 * Now open the file, so that we can mmap() it.
1900 fd = open(filename, O_RDONLY);
1903 fprintf(stderr, "-Unable to open .DBG file %s\n", filename);
1909 * Now mmap() the file.
1911 addr = mmap(0, statbuf.st_size, PROT_READ,
1912 MAP_PRIVATE, fd, 0);
1913 if( addr == (char *) 0xffffffff )
1915 fprintf(stderr, "-Unable to mmap .DBG file %s\n", filename);
1920 * Now that we have the formalities over and done with, we need
1921 * to find the table of contents for the PDB file.
1923 pdbhdr = (struct codeview_pdb_hdr *) addr;
1924 blocksize = pdbhdr->blocksize;
1925 last_extent = (statbuf.st_size + blocksize - 1) / blocksize;
1928 * The TOC itself isn't always contiguous, so we need to extract a few
1929 * extents from the file to form the TOC.
1931 toc_blocks = (pdbhdr->toc_len + blocksize - 1) / blocksize;
1932 toc = (char *) DBG_alloc(toc_blocks * blocksize);
1933 table = pdbhdr->toc_ext;
1934 for(i=0; i < toc_blocks; i++)
1936 memcpy(toc + blocksize*i, addr + table[i]*blocksize, blocksize);
1940 * Next build our own table which will have the size and extent block
1941 * list for each record in the PDB file.
1943 * The TOC starts out with the number of files. Then it is followed by
1944 * (npair * 2*sizeof(int)) bytes of information, which are pairs of ints.
1945 * The first one is the size of the record (in bytes), and the second one
1946 * is something else which I haven't figured out yet.
1948 pnt = (unsigned int *) toc;
1950 extent_table = (unsigned short *) ((unsigned int) toc +
1951 npair * 2 * sizeof(int) + sizeof(int));
1956 if( sizeof(int) + 2*sizeof(int)*npair > pdbhdr->toc_len )
1961 filelist = (struct file_list *) DBG_alloc(npair * sizeof(*filelist));
1962 if( filelist == NULL )
1966 memset(filelist, 0, npair * sizeof(*filelist));
1969 for(i=0; i < npair; i++)
1971 filelist[i].record_len = pnt[i*2];
1972 filelist[i].nextents = (filelist[i].record_len + blocksize - 1)
1974 filelist[i].extent_list = extent_table + nblocks;
1975 nblocks += filelist[i].nextents;
1978 * These get filled in later when we parse one of the records.
1980 filelist[i].linetab_offset = 0;
1981 filelist[i].linetab_len = 0;
1985 * OK, now walk through the various records and pick out the bits we
1986 * really want to see. Some of the records are extra special, and
1987 * we need to handle these a little bit differently.
1989 for(i=0; i < npair; i++)
1991 if( filelist[i].record_len == 0xffffffff )
1997 * Make sure our buffer is large enough to hold the record.
1999 if( bufflen < filelist[i].nextents * blocksize )
2001 bufflen = filelist[i].nextents * blocksize;
2002 buffer = (char *) DBG_realloc(buffer, bufflen);
2006 * Do this just for completeness. It makes debugging easier
2007 * if we have a clean indication of where the record ends.
2009 memset(buffer, 0, filelist[i].nextents * blocksize);
2012 * Next, build the record using the extent list.
2014 for(j=0; j < filelist[i].nextents; j++)
2016 memcpy(buffer + j * blocksize,
2017 addr + filelist[i].extent_list[j] * blocksize,
2021 pnt = (unsigned int *) buffer;
2024 * OK, now figure out what to do with it.
2028 * Always ignore the first entry. It seems to contain a backup copy
2029 * of the TOC (the last time the file was modified??)
2037 * The second entry as a id block. It contains a magic number
2038 * to identify the compiler, plus it also contains the timestamp
2039 * which must match the timestamp in the executable.
2044 if( ((*pnt != 19950623) && (*pnt != 19950814))
2045 || (filelist[i].record_len != 0x24)
2046 || (pnt[1] != ((struct CodeViewDebug *)(deefer->dbg_info))->cv_timestamp) )
2053 * The third entry contains pointers to the global symbol table,
2054 * plus it also contains additional information about each record
2059 hd = (struct filetab_hdr *) buffer;
2061 gsym_record = hd->gsym_file;
2062 gsymtab = (char *) DBG_alloc(filelist[gsym_record].nextents
2064 memset(gsymtab, 0, filelist[gsym_record].nextents * blocksize);
2066 for(j=0; j < filelist[gsym_record].nextents; j++)
2068 memcpy(gsymtab + j * blocksize,
2069 addr + filelist[gsym_record].extent_list[j] * blocksize,
2074 * This record also contains information about where in the
2075 * remaining records we will be able to find the start of the
2076 * line number table. We could locate that bit using heuristics,
2077 * but since we have the info handy, we might as well use it.
2079 offset = sizeof(*hd);
2082 fent = (struct file_ent *) (buffer + offset);
2083 if( offset > hd->ftab_len )
2088 if( fent->file_number == 0 || fent->file_number >= npair )
2093 filelist[fent->file_number].linetab_offset =
2094 fent->linetab_offset;
2095 filelist[fent->file_number].linetab_len =
2098 * Figure out the offset of the next entry.
2099 * There is a fixed part of the record and a variable
2100 * length filename which we must also skip past.
2102 offset += ((unsigned int) &fent->filename - (unsigned int) fent)
2103 + strlen(fent->filename) + 1;
2104 offset += strlen(buffer+offset) + 1;
2105 offset = (offset + 3) & ~3;
2111 * Two different magic numbers used as dates.
2112 * These indicate the 'type' table.
2114 if( *pnt == 19950410
2115 || *pnt == 19951122 )
2117 DEBUG_ParseTypeTable(buffer, filelist[i].record_len);
2122 * This is something we really want to look at, since it contains
2123 * real debug info. Anything that doesn't match this can be
2129 * First, snag the line table, if we have one. This always
2130 * occurs at the end of the record, so we take the linetab
2131 * offset as the end of the normal part of the record.
2134 if( filelist[i].linetab_len != 0 )
2136 linetab = DEBUG_SnarfLinetab(buffer + filelist[i].linetab_offset,
2137 filelist[i].linetab_len);
2138 DEBUG_SnarfCodeView(deefer, buffer,
2139 filelist[i].linetab_offset,
2144 DEBUG_SnarfCodeView(deefer, buffer,
2145 filelist[i].record_len,
2153 * Finally, process the global symbol table itself. There isn't
2154 * a line number component to this, so we just toss everything
2155 * into the mix and it all should work out.
2157 if( gsym_record != 0 )
2159 DEBUG_SnarfCodeView(deefer, gsymtab - sizeof(int),
2160 filelist[gsym_record].record_len,
2166 if( gsymtab != NULL )
2172 if( buffer != NULL )
2177 if( filelist != NULL )
2182 if( addr != (char *) 0xffffffff )
2184 munmap(addr, statbuf.st_size);
2196 * Process DBG file which contains debug information.
2200 DEBUG_ProcessDBGFile(struct deferred_debug_info * deefer, char * filename)
2202 char * addr = (char *) 0xffffffff;
2204 struct CV4_DirHead * codeview_dir;
2205 struct CV4_DirEnt * codeview_dent;
2206 PIMAGE_DEBUG_DIRECTORY dbghdr;
2207 struct deferred_debug_info deefer2;
2211 struct codeview_linetab_hdr * linetab;
2213 PIMAGE_SEPARATE_DEBUG_HEADER pdbg = NULL;
2214 IMAGE_SECTION_HEADER * sectp;
2215 struct stat statbuf;
2217 char dbg_file[MAX_PATHNAME_LEN];
2219 LocateDebugInfoFile(filename, dbg_file);
2220 status = stat(dbg_file, &statbuf);
2223 fprintf(stderr, "-Unable to open .DBG file %s\n", dbg_file);
2228 * Now open the file, so that we can mmap() it.
2230 fd = open(dbg_file, O_RDONLY);
2233 fprintf(stderr, "Unable to open .DBG file %s\n", dbg_file);
2239 * Now mmap() the file.
2241 addr = mmap(0, statbuf.st_size, PROT_READ,
2242 MAP_PRIVATE, fd, 0);
2243 if( addr == (char *) 0xffffffff )
2245 fprintf(stderr, "Unable to mmap .DBG file %s\n", dbg_file);
2249 pdbg = (PIMAGE_SEPARATE_DEBUG_HEADER) addr;
2251 if( pdbg->TimeDateStamp != deefer->dbgdir->TimeDateStamp )
2253 fprintf(stderr, "Warning - %s has incorrect internal timestamp\n",
2257 Well, sometimes this happens to DBG files which ARE REALLY the right .DBG
2258 files but nonetheless this check fails. Anyway, WINDBG (debugger for
2259 Windows by Microsoft) loads debug symbols which have incorrect timestamps.
2263 fprintf(stderr, "Processing symbols from %s...\n", dbg_file);
2265 dbghdr = (PIMAGE_DEBUG_DIRECTORY) ( addr + sizeof(*pdbg)
2266 + pdbg->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)
2267 + pdbg->ExportedNamesSize);
2269 sectp = (PIMAGE_SECTION_HEADER) ((char *) pdbg + sizeof(*pdbg));
2270 nsect = pdbg->NumberOfSections;
2272 for( i=0; i < pdbg->DebugDirectorySize / sizeof(*pdbg); i++, dbghdr++ )
2274 switch(dbghdr->Type)
2276 case IMAGE_DEBUG_TYPE_COFF:
2278 * Dummy up a deferred debug header to handle the
2279 * COFF stuff embedded within the DBG file.
2281 memset((char *) &deefer2, 0, sizeof(deefer2));
2282 deefer2.dbg_info = (addr + dbghdr->PointerToRawData);
2283 deefer2.dbg_size = dbghdr->SizeOfData;
2284 deefer2.load_addr = deefer->load_addr;
2286 DEBUG_ProcessCoff(&deefer2);
2288 case IMAGE_DEBUG_TYPE_CODEVIEW:
2290 * This is the older format by which codeview stuff is
2291 * stored, known as the 'NB09' format. Newer executables
2292 * and dlls created by VC++ use PDB files instead, which
2293 * have lots of internal similarities, but the overall
2294 * format and structure is quite different.
2296 codeview = (addr + dbghdr->PointerToRawData);
2299 * The first thing in the codeview section should be
2300 * an 'NB09' identifier. As a sanity check, make sure
2303 if( *((unsigned int*) codeview) != 0x3930424e )
2309 * Next we need to find the directory. This is easy too.
2311 codeview_dir = (struct CV4_DirHead *)
2312 (codeview + ((unsigned int*) codeview)[1]);
2315 * Some more sanity checks. Make sure that everything
2316 * is as we expect it.
2318 if( codeview_dir->next_offset != 0
2319 || codeview_dir->dhsize != sizeof(*codeview_dir)
2320 || codeview_dir->desize != sizeof(*codeview_dent) )
2324 codeview_dent = (struct CV4_DirEnt *) (codeview_dir + 1);
2326 for(j=0; j < codeview_dir->ndir; j++, codeview_dent++)
2328 if( codeview_dent->subsect_number == sstAlignSym )
2331 * Check the previous entry. If it is a
2332 * sstSrcModule, it contains the line number
2333 * info for this file.
2336 if( codeview_dent[1].module_number == codeview_dent[0].module_number
2337 && codeview_dent[1].subsect_number == sstSrcModule )
2339 linetab = DEBUG_SnarfLinetab(
2340 codeview + codeview_dent[1].offset,
2341 codeview_dent[1].size);
2344 if( codeview_dent[-1].module_number == codeview_dent[0].module_number
2345 && codeview_dent[-1].subsect_number == sstSrcModule )
2347 linetab = DEBUG_SnarfLinetab(
2348 codeview + codeview_dent[-1].offset,
2349 codeview_dent[-1].size);
2352 * Now process the CV stuff.
2354 DEBUG_SnarfCodeView(deefer,
2355 codeview + codeview_dent->offset,
2356 codeview_dent->size,
2368 if( addr != (char *) 0xffffffff )
2370 munmap(addr, statbuf.st_size);
2382 DEBUG_ProcessDeferredDebug()
2384 struct deferred_debug_info * deefer;
2385 struct CodeViewDebug * cvd;
2386 struct MiscDebug * misc;
2391 DEBUG_InitCVDataTypes();
2393 for(deefer = dbglist; deefer; deefer = deefer->next)
2395 if( deefer->loaded )
2400 if( last_proc != deefer->dbg_index )
2404 fprintf(stderr, "DeferredDebug for:");
2407 fprintf(stderr, " %s",deefer->module_name);
2408 last_proc = deefer->dbg_index;
2411 switch(deefer->dbgdir->Type)
2413 case IMAGE_DEBUG_TYPE_COFF:
2415 * Standard COFF debug information that VC++ adds when you
2416 * use /debugtype:both with the linker.
2419 fprintf(stderr, "Processing COFF symbols...\n");
2421 DEBUG_ProcessCoff(deefer);
2423 case IMAGE_DEBUG_TYPE_CODEVIEW:
2425 * This is a pointer to a PDB file of some sort.
2427 cvd = (struct CodeViewDebug *) deefer->dbg_info;
2429 if( strcmp(cvd->cv_nbtype, "NB10") != 0 )
2432 * Whatever this is, we don't know how to deal with
2437 DEBUG_ProcessPDBFile(deefer, cvd->cv_name);
2439 fprintf(stderr, "Processing PDB file %s\n", cvd->cv_name);
2442 case IMAGE_DEBUG_TYPE_MISC:
2444 * A pointer to a .DBG file of some sort. These files
2445 * can contain either CV4 or COFF information. Open
2446 * the file, and try to do the right thing with it.
2448 misc = (struct MiscDebug *) deefer->dbg_info;
2450 filename = strrchr((char *) &misc->Data, '.');
2453 * Ignore the file if it doesn't have a .DBG extension.
2455 if( (filename == NULL)
2456 || ( (strcmp(filename, ".dbg") != 0)
2457 && (strcmp(filename, ".DBG") != 0)) )
2462 filename = (char *) &misc->Data;
2465 * Do the dirty deed...
2467 DEBUG_ProcessDBGFile(deefer, filename);
2472 * We should never get here...
2478 fprintf(stderr, "\n");
2483 /***********************************************************************
2486 * Display shared libarary information.
2488 void DEBUG_InfoShare(void)
2490 struct deferred_debug_info * deefer;
2492 fprintf(stderr,"Address\t\tModule\tName\n");
2494 for(deefer = dbglist; deefer; deefer = deefer->next)
2496 fprintf(stderr,"0x%8.8x\t(%s)\t%s\n", (unsigned int) deefer->load_addr,
2497 deefer->module ? "Win32" : "ELF", deefer->module_name);