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>
29 #define PATH_MAX _MAX_PATH
38 *dbg_filename must be at least MAX_PATHNAME_LEN bytes in size
40 static void LocateDebugInfoFile(char *filename, char *dbg_filename)
42 char *str1 = xmalloc(MAX_PATHNAME_LEN*10);
43 char *str2 = xmalloc(MAX_PATHNAME_LEN);
46 DOS_FULL_NAME fullname;
48 file = strrchr(filename, '\\');
49 if( file == NULL ) file = filename; else file++;
51 if (GetEnvironmentVariableA("_NT_SYMBOL_PATH", str1, MAX_PATHNAME_LEN))
52 if (SearchPathA(str1, file, NULL, MAX_PATHNAME_LEN*10, str2, &name_part))
54 if (GetEnvironmentVariableA("_NT_ALT_SYMBOL_PATH", str1, MAX_PATHNAME_LEN))
55 if (SearchPathA(str1, file, NULL, MAX_PATHNAME_LEN*10, str2, &name_part))
57 if (SearchPathA(NULL, file, NULL, MAX_PATHNAME_LEN*10, str2, &name_part))
62 memcpy(dbg_filename, filename, MAX_PATHNAME_LEN);
68 if (DOSFS_GetFullName(str2, TRUE, &fullname))
69 memcpy(dbg_filename, fullname.long_name, MAX_PATHNAME_LEN);
77 * This is an index we use to keep track of the debug information
78 * when we have multiple sources. We use the same database to also
79 * allow us to do an 'info shared' type of deal, and we use the index
80 * to eliminate duplicates.
82 static int DEBUG_next_index = 0;
93 * This is a convenience structure used to map portions of the
103 * This is how we reference the various record types.
105 union codeview_symbol
119 unsigned short symtype;
120 unsigned char namelen;
121 unsigned char name[1];
128 unsigned int pparent;
132 unsigned short segment;
133 unsigned short thunk_len;
134 unsigned char thtype;
135 unsigned char namelen;
136 unsigned char name[1];
142 unsigned int pparent;
145 unsigned int proc_len;
146 unsigned int debug_start;
147 unsigned int debug_end;
149 unsigned short segment;
150 unsigned short proctype;
152 unsigned char namelen;
153 unsigned char name[1];
157 short int len; /* Total length of this entry */
158 short int id; /* Always S_BPREL32 */
159 unsigned int offset; /* Stack offset relative to BP */
160 unsigned short symtype;
161 unsigned char namelen;
162 unsigned char name[1];
180 unsigned char variant[1];
188 unsigned char bitoff;
198 unsigned char arrlen;
199 unsigned char namelen;
200 unsigned char name[1];
212 unsigned short structlen;
213 unsigned char namelen;
214 unsigned char name[1];
224 unsigned short un_len;
225 unsigned char namelen;
226 unsigned char name[1];
237 unsigned char namelen;
238 unsigned char name[1];
245 unsigned short int value;
246 unsigned char namelen;
247 unsigned char name[1];
255 unsigned short int offset;
256 unsigned char namelen;
257 unsigned char name[1];
268 unsigned char namelen;
269 unsigned char name[1];
274 #define S_BPREL 0x200
275 #define S_LDATA 0x201
276 #define S_GDATA 0x202
278 #define S_LPROC 0x204
279 #define S_GPROC 0x205
280 #define S_THUNK 0x206
281 #define S_BLOCK 0x207
283 #define S_LABEL 0x209
285 #define S_PROCREF 0x400
286 #define S_DATAREF 0x401
287 #define S_ALIGN 0x402
288 #define S_UNKNOWN 0x403
291 * This covers the basic datatypes that VC++ seems to be using these days.
292 * 32 bit mode only. There are additional numbers for the pointers in 16
293 * bit mode. There are many other types listed in the documents, but these
294 * are apparently not used by the compiler, or represent pointer types
297 #define T_NOTYPE 0x0000 /* Notype */
298 #define T_ABS 0x0001 /* Abs */
299 #define T_VOID 0x0003 /* Void */
300 #define T_CHAR 0x0010 /* signed char */
301 #define T_SHORT 0x0011 /* short */
302 #define T_LONG 0x0012 /* long */
303 #define T_QUAD 0x0013 /* long long */
304 #define T_UCHAR 0x0020 /* unsigned char */
305 #define T_USHORT 0x0021 /* unsigned short */
306 #define T_ULONG 0x0022 /* unsigned long */
307 #define T_UQUAD 0x0023 /* unsigned long long */
308 #define T_REAL32 0x0040 /* float */
309 #define T_REAL64 0x0041 /* double */
310 #define T_RCHAR 0x0070 /* real char */
311 #define T_WCHAR 0x0071 /* wide char */
312 #define T_INT4 0x0074 /* int */
313 #define T_UINT4 0x0075 /* unsigned int */
315 #define T_32PVOID 0x0403 /* 32 bit near pointer to void */
316 #define T_32PCHAR 0x0410 /* 16:32 near pointer to signed char */
317 #define T_32PSHORT 0x0411 /* 16:32 near pointer to short */
318 #define T_32PLONG 0x0412 /* 16:32 near pointer to int */
319 #define T_32PQUAD 0x0413 /* 16:32 near pointer to long long */
320 #define T_32PUCHAR 0x0420 /* 16:32 near pointer to unsigned char */
321 #define T_32PUSHORT 0x0421 /* 16:32 near pointer to unsigned short */
322 #define T_32PULONG 0x0422 /* 16:32 near pointer to unsigned int */
323 #define T_32PUQUAD 0x0423 /* 16:32 near pointer to long long */
324 #define T_32PREAL32 0x0440 /* 16:32 near pointer to float */
325 #define T_32PREAL64 0x0441 /* 16:32 near pointer to float */
326 #define T_32PRCHAR 0x0470 /* 16:32 near pointer to real char */
327 #define T_32PWCHAR 0x0471 /* 16:32 near pointer to real char */
328 #define T_32PINT4 0x0474 /* 16:32 near pointer to int */
329 #define T_32PUINT4 0x0475 /* 16:32 near pointer to unsigned int */
331 #define LF_MODIFIER 0x1
332 #define LF_POINTER 0x2
335 #define LF_STRUCTURE 0x5
337 #define LF_ENUMERATION 0x7
338 #define LF_PROCEDURE 0x8
339 #define LF_MFUNCTION 0x9
340 #define LF_VTSHAPE 0xa
341 #define LF_BARRAY 0xd
342 #define LF_DIMARRAY 0x11
343 #define LF_VFTPATH 0x12
345 #define LF_SKIP 0x200
346 #define LF_ARGLIST 0x201
347 #define LF_FIELDLIST 0x204
348 #define LF_DERIVED 0x205
349 #define LF_BITFIELD 0x206
351 #define LF_BCLASS 0x400
352 #define LF_VBCLASS 0x401
353 #define LF_IVBCLASS 0x402
354 #define LF_ENUMERATE 0x403
355 #define LF_FRIENDFCN 0x404
356 #define LF_INDEX 0x405
357 #define LF_MEMBER 0x406
358 #define LF_STMEMBER 0x407
359 #define LF_METHOD 0x408
360 #define LF_NESTEDTYPE 0x409
361 #define LF_VFUNCTAB 0x40a
362 #define LF_FRIENDCLS 0x40b
363 #define LF_ONEMETHOD 0x40c
364 #define LF_FUNCOFF 0x40d
366 #define MAX_BUILTIN_TYPES 0x480
367 static struct datatype * cv_basic_types[MAX_BUILTIN_TYPES];
368 static int num_cv_defined_types = 0;
369 static struct datatype **cv_defined_types = NULL;
372 * For the type CODEVIEW debug directory entries, the debug directory
373 * points to a structure like this. The cv_name field is the name
374 * of an external .PDB file.
379 unsigned int cv_timestamp;
385 unsigned int DataType;
393 * This is the header that the COFF variety of debug header points to.
397 unsigned int SymbolOffset;
398 unsigned int N_Linenum;
399 unsigned int LinenumberOffset;
400 unsigned int Unused[4];
404 unsigned int VirtualAddr;
405 unsigned short int Linenum;
409 unsigned int startaddr;
410 unsigned int endaddr;
414 struct name_hash **entries;
424 unsigned int NotLong;
425 unsigned int StrTaboff;
432 unsigned char NumberOfAuxSymbols;
435 struct CoffAuxSection{
437 unsigned short NumberOfRelocations;
438 unsigned short NumberOfLinenumbers;
439 unsigned int CheckSum;
445 * These two structures are used in the directory within a .DBG file
446 * to locate the individual important bits that we might want to see.
449 short unsigned int dhsize;
450 short unsigned int desize;
452 unsigned int next_offset;
457 short unsigned int subsect_number;
458 short unsigned int module_number;
464 * These are the values of interest that the subsect_number field takes.
466 #define sstAlignSym 0x125
467 #define sstSrcModule 0x127
469 struct codeview_linetab_hdr
476 unsigned short * linetab;
477 unsigned int * offtab;
480 struct codeview_pdb_hdr
483 unsigned int blocksize; /* Extent size */
484 unsigned short loc_freelist; /* freelist. */
485 unsigned short alloc_filesize; /* # extents allocated. */
486 unsigned int toc_len;
487 unsigned int unknown;
488 unsigned short toc_ext[1]; /* array of extent #'s for toc. */
492 * This is our own structure that we use to keep track of the contents
499 short int * extent_list;
500 unsigned int linetab_offset;
501 unsigned int linetab_len;
505 * These are the structures that represent how the file table is set up
506 * within the PDB file.
510 unsigned short tab1_file;
511 unsigned short tab2_file;
512 unsigned short gsym_file;
513 unsigned short padding;
514 unsigned int ftab_len;
515 unsigned int fofftab_len;
516 unsigned int hash_len;
517 unsigned int strtab_len;
522 unsigned int reserved1;
523 unsigned short datasect_segment;
524 unsigned short reserved2;
525 unsigned int datasect_offset;
526 unsigned int datasect_size;
527 unsigned int datasect_flags;
528 unsigned short reserved3;
529 unsigned short index;
530 unsigned short num6a;
531 unsigned short file_number;
532 unsigned int linetab_offset;
533 unsigned int linetab_len;
537 unsigned char filename[1];
541 ********************************************************************
543 struct deferred_debug_info
545 struct deferred_debug_info * next;
551 PIMAGE_DEBUG_DIRECTORY dbgdir;
552 PIMAGE_SECTION_HEADER sectp;
558 struct deferred_debug_info * dbglist = NULL;
561 * A simple macro that tells us whether a given COFF symbol is a
564 #define N_TMASK 0x0030
565 #define IMAGE_SYM_DTYPE_FUNCTION 2
567 #define ISFCN(x) (((x) & N_TMASK) == (IMAGE_SYM_DTYPE_FUNCTION << N_BTSHFT))
571 * This is what we are looking for in the COFF symbols.
573 #define IMAGE_SYM_CLASS_EXTERNAL 0x2
574 #define IMAGE_SYM_CLASS_STATIC 0x3
575 #define IMAGE_SYM_CLASS_FILE 0x67
578 struct datatype * DEBUG_GetCVType(int typeno)
580 struct datatype * dt = NULL;
583 * Convert Codeview type numbers into something we can grok internally.
584 * Numbers < 0x1000 are all fixed builtin types. Numbers from 0x1000 and
585 * up are all user defined (structs, etc).
587 if( typeno < 0x1000 )
589 if( typeno < MAX_BUILTIN_TYPES )
591 dt = cv_basic_types[typeno];
596 if( typeno - 0x1000 < num_cv_defined_types )
598 dt = cv_defined_types[typeno - 0x1000];
606 DEBUG_ParseTypeTable(char * table, int len)
610 enum debug_type fieldtype;
614 struct datatype * subtype;
616 union codeview_type * type;
617 union codeview_type * type2;
618 struct datatype * typeptr;
622 ptr = (union any_size) (table + 16);
623 while( ptr.c - table < len )
625 type = (union codeview_type *) ptr.c;
627 if( curr_type - 0x1000 >= num_cv_defined_types )
629 num_cv_defined_types += 0x100;
630 cv_defined_types = (struct datatype **) realloc(cv_defined_types,
631 num_cv_defined_types * sizeof(struct datatype *));
632 memset(cv_defined_types + num_cv_defined_types - 0x100,
634 0x100 * sizeof(struct datatype *));
635 if( cv_defined_types == NULL )
641 switch(type->generic.id)
644 cv_defined_types[curr_type - 0x1000] =
645 DEBUG_FindOrMakePointerType(DEBUG_GetCVType(type->pointer.datatype));
648 if( type->array.arrlen >= 0x8000 )
651 * This is a numeric leaf, I am too lazy to handle this right
654 fprintf(stderr, "Ignoring large numberic leaf.\n");
657 if( type->array.namelen != 0 )
659 memset(symname, 0, sizeof(symname));
660 memcpy(symname, type->array.name, type->array.namelen);
661 typeptr = DEBUG_NewDataType(DT_ARRAY, symname);
665 typeptr = DEBUG_NewDataType(DT_ARRAY, NULL);
667 cv_defined_types[curr_type - 0x1000] = typeptr;
669 subtype = DEBUG_GetCVType(type->array.elemtype);
670 if( (subtype == NULL)
671 || (elem_size = DEBUG_GetObjectSize(subtype)) == 0 )
677 arr_max = type->array.arrlen / DEBUG_GetObjectSize(subtype);
680 DEBUG_SetArrayParams(typeptr, 0, arr_max, subtype);
684 * This is where the basic list of fields is defined for
685 * structures and classes.
687 * First, we need to look ahead and see whether we are building
688 * a fieldlist for an enum or a struct.
691 type2 = (union codeview_type *) ptr2.c;
692 if( type2->member.id == LF_MEMBER )
694 typeptr = DEBUG_NewDataType(DT_STRUCT, NULL);
695 fieldtype = DT_STRUCT;
697 else if( type2->member.id == LF_ENUMERATE )
699 typeptr = DEBUG_NewDataType(DT_ENUM, NULL);
707 cv_defined_types[curr_type - 0x1000] = typeptr;
708 while( ptr2.c < (ptr.c + ((type->generic.len + 3) & ~3)) )
710 type2 = (union codeview_type *) ptr2.c;
711 if( type2->member.id == LF_MEMBER && fieldtype == DT_STRUCT )
713 memset(symname, 0, sizeof(symname));
714 memcpy(symname, type2->member.name, type2->member.namelen);
716 subtype = DEBUG_GetCVType(type2->member.type);
718 if( subtype != NULL )
720 elem_size = DEBUG_GetObjectSize(subtype);
723 if( type2->member.offset >= 0x8000 )
726 * This is a numeric leaf, I am too lazy to handle this right
729 fprintf(stderr, "Ignoring large numberic leaf.\n");
733 DEBUG_AddStructElement(typeptr, symname, subtype,
734 type2->member.offset << 3,
738 else if( type2->member.id == LF_ENUMERATE && fieldtype == DT_ENUM )
740 memset(symname, 0, sizeof(symname));
741 memcpy(symname, type2->enumerate.name, type2->enumerate.namelen);
743 if( type2->enumerate.value >= 0x8000 )
746 * This is a numeric leaf, I am too lazy to handle this right
749 fprintf(stderr, "Ignoring large numberic leaf.\n");
753 DEBUG_AddStructElement(typeptr, symname, NULL,
754 type2->enumerate.value, 0);
760 * Something else I have never seen before. Either wrong type of
761 * object in the fieldlist, or some other problem which I wouldn't
762 * really know how to handle until it came up.
764 fprintf(stderr, "Unexpected entry in fieldlist\n");
769 ptr2.c += ((type2->member.namelen + 9 + 3) & ~3);
774 if( type->structure.structlen >= 0x8000 )
777 * This is a numeric leaf, I am too lazy to handle this right
780 fprintf(stderr, "Ignoring large numberic leaf.\n");
783 memset(symname, 0, sizeof(symname));
784 memcpy(symname, type->structure.name, type->structure.namelen);
785 if( strcmp(symname, "__unnamed") == 0 )
787 typeptr = DEBUG_NewDataType(DT_STRUCT, NULL);
791 typeptr = DEBUG_NewDataType(DT_STRUCT, symname);
793 cv_defined_types[curr_type - 0x1000] = typeptr;
796 * Now copy the relevant bits from the fieldlist that we specified.
798 subtype = DEBUG_GetCVType(type->structure.fieldlist);
800 if( subtype != NULL )
802 DEBUG_SetStructSize(typeptr, type->structure.structlen);
803 DEBUG_CopyFieldlist(typeptr, subtype);
807 if( type->t_union.un_len >= 0x8000 )
810 * This is a numeric leaf, I am too lazy to handle this right
813 fprintf(stderr, "Ignoring large numberic leaf.\n");
816 memset(symname, 0, sizeof(symname));
817 memcpy(symname, type->t_union.name, type->t_union.namelen);
819 if( strcmp(symname, "__unnamed") == 0 )
821 typeptr = DEBUG_NewDataType(DT_STRUCT, NULL);
825 typeptr = DEBUG_NewDataType(DT_STRUCT, symname);
828 cv_defined_types[curr_type - 0x1000] = typeptr;
831 * Now copy the relevant bits from the fieldlist that we specified.
833 subtype = DEBUG_GetCVType(type->t_union.field);
835 if( subtype != NULL )
837 DEBUG_SetStructSize(typeptr, type->t_union.un_len);
838 DEBUG_CopyFieldlist(typeptr, subtype);
842 typeptr = DEBUG_NewDataType(DT_BITFIELD, NULL);
843 cv_defined_types[curr_type - 0x1000] = typeptr;
844 DEBUG_SetBitfieldParams(typeptr, type->bitfield.bitoff,
845 type->bitfield.nbits,
846 DEBUG_GetCVType(type->bitfield.type));
849 memset(symname, 0, sizeof(symname));
850 memcpy(symname, type->enumeration.name, type->enumeration.namelen);
851 typeptr = DEBUG_NewDataType(DT_ENUM, symname);
852 cv_defined_types[curr_type - 0x1000] = typeptr;
855 * Now copy the relevant bits from the fieldlist that we specified.
857 subtype = DEBUG_GetCVType(type->enumeration.field);
859 if( subtype != NULL )
861 DEBUG_CopyFieldlist(typeptr, subtype);
869 ptr.c += (type->generic.len + 3) & ~3;
876 DEBUG_InitCVDataTypes()
879 * These are the common builtin types that are used by VC++.
881 cv_basic_types[T_NOTYPE] = NULL;
882 cv_basic_types[T_ABS] = NULL;
883 cv_basic_types[T_VOID] = DEBUG_NewDataType(DT_BASIC, "void");
884 cv_basic_types[T_CHAR] = DEBUG_NewDataType(DT_BASIC, "char");
885 cv_basic_types[T_SHORT] = DEBUG_NewDataType(DT_BASIC, "short int");
886 cv_basic_types[T_LONG] = DEBUG_NewDataType(DT_BASIC, "long int");
887 cv_basic_types[T_QUAD] = DEBUG_NewDataType(DT_BASIC, "long long int");
888 cv_basic_types[T_UCHAR] = DEBUG_NewDataType(DT_BASIC, "unsigned char");
889 cv_basic_types[T_USHORT] = DEBUG_NewDataType(DT_BASIC, "short unsigned int");
890 cv_basic_types[T_ULONG] = DEBUG_NewDataType(DT_BASIC, "long unsigned int");
891 cv_basic_types[T_UQUAD] = DEBUG_NewDataType(DT_BASIC, "long long unsigned int");
892 cv_basic_types[T_REAL32] = DEBUG_NewDataType(DT_BASIC, "float");
893 cv_basic_types[T_REAL64] = DEBUG_NewDataType(DT_BASIC, "double");
894 cv_basic_types[T_RCHAR] = DEBUG_NewDataType(DT_BASIC, "char");
895 cv_basic_types[T_WCHAR] = DEBUG_NewDataType(DT_BASIC, "short");
896 cv_basic_types[T_INT4] = DEBUG_NewDataType(DT_BASIC, "int");
897 cv_basic_types[T_UINT4] = DEBUG_NewDataType(DT_BASIC, "unsigned int");
899 cv_basic_types[T_32PVOID] = DEBUG_FindOrMakePointerType(cv_basic_types[T_VOID]);
900 cv_basic_types[T_32PCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_CHAR]);
901 cv_basic_types[T_32PSHORT] = DEBUG_FindOrMakePointerType(cv_basic_types[T_SHORT]);
902 cv_basic_types[T_32PLONG] = DEBUG_FindOrMakePointerType(cv_basic_types[T_LONG]);
903 cv_basic_types[T_32PQUAD] = DEBUG_FindOrMakePointerType(cv_basic_types[T_QUAD]);
904 cv_basic_types[T_32PUCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_UCHAR]);
905 cv_basic_types[T_32PUSHORT] = DEBUG_FindOrMakePointerType(cv_basic_types[T_USHORT]);
906 cv_basic_types[T_32PULONG] = DEBUG_FindOrMakePointerType(cv_basic_types[T_ULONG]);
907 cv_basic_types[T_32PUQUAD] = DEBUG_FindOrMakePointerType(cv_basic_types[T_UQUAD]);
908 cv_basic_types[T_32PREAL32] = DEBUG_FindOrMakePointerType(cv_basic_types[T_REAL32]);
909 cv_basic_types[T_32PREAL64] = DEBUG_FindOrMakePointerType(cv_basic_types[T_REAL64]);
910 cv_basic_types[T_32PRCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_RCHAR]);
911 cv_basic_types[T_32PWCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_WCHAR]);
912 cv_basic_types[T_32PINT4] = DEBUG_FindOrMakePointerType(cv_basic_types[T_INT4]);
913 cv_basic_types[T_32PUINT4] = DEBUG_FindOrMakePointerType(cv_basic_types[T_UINT4]);
917 * In this function, we keep track of deferred debugging information
918 * that we may need later if we were to need to use the internal debugger.
919 * We don't fully process it here for performance reasons.
922 DEBUG_RegisterDebugInfo( HMODULE hModule, const char *module_name)
924 int has_codeview = FALSE;
927 PIMAGE_DEBUG_DIRECTORY dbgptr;
929 PIMAGE_NT_HEADERS nth = PE_HEADER(hModule);
931 size = nth->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].Size;
933 v_addr = nth->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG].VirtualAddress;
934 dbgptr = (PIMAGE_DEBUG_DIRECTORY) (hModule + v_addr);
936 for(; size >= sizeof(*dbgptr); size -= sizeof(*dbgptr), dbgptr++ )
940 case IMAGE_DEBUG_TYPE_CODEVIEW:
941 case IMAGE_DEBUG_TYPE_MISC:
948 dbgptr = (PIMAGE_DEBUG_DIRECTORY) (hModule + v_addr);
949 for(; size >= sizeof(*dbgptr); size -= sizeof(*dbgptr), dbgptr++ )
953 case IMAGE_DEBUG_TYPE_COFF:
955 * If we have both codeview and COFF debug info, ignore the
956 * coff debug info as it would just confuse us, and it is
959 * FIXME - this is broken - if we cannot find the PDB file, then
960 * we end up with no debugging info at all. In this case, we
961 * should use the COFF info as a backup.
967 case IMAGE_DEBUG_TYPE_CODEVIEW:
968 case IMAGE_DEBUG_TYPE_MISC:
970 * This is usually an indirection to a .DBG file.
971 * This is similar to (but a slightly older format) from the
974 * First check to see if the image was 'stripped'. If so, it
975 * means that this entry points to a .DBG file. Otherwise,
976 * it just points to itself, and we can ignore this.
984 if( (dbgptr->Type != IMAGE_DEBUG_TYPE_MISC) ||
985 (PE_HEADER(hModule)->FileHeader.Characteristics & IMAGE_FILE_DEBUG_STRIPPED) != 0 )
989 DOS_FULL_NAME full_name;
990 struct deferred_debug_info* deefer = (struct deferred_debug_info *) xmalloc(sizeof(*deefer));
992 deefer->module = hModule;
993 deefer->load_addr = (char *)hModule;
996 * Read the important bits. What we do after this depends
997 * upon the type, but this is always enough so we are able
998 * to proceed if we know what we need to do next.
1000 /* basically, the PE loader maps all sections (data, resources...), but doesn't map
1001 * the DataDirectory array's content. One its entry contains the *beloved*
1002 * debug information. (Note the DataDirectory is mapped, not its content)
1005 if (GetModuleFileNameA(hModule, fn, sizeof(fn)) > 0 &&
1006 DOSFS_GetFullName(fn, TRUE, &full_name) &&
1007 (fd = open(full_name.long_name, O_RDONLY)) > 0)
1009 deefer->dbg_info = mmap(NULL, dbgptr->SizeOfData,
1010 PROT_READ, MAP_PRIVATE, fd, dbgptr->PointerToRawData);
1012 if( deefer->dbg_info == (char *) 0xffffffff )
1021 fprintf(stderr, " (not mapped: fn=%s, lfn=%s, fd=%d)", fn, full_name.long_name, fd);
1024 deefer->dbg_size = dbgptr->SizeOfData;
1025 deefer->dbgdir = dbgptr;
1026 deefer->next = dbglist;
1027 deefer->loaded = FALSE;
1028 deefer->dbg_index = DEBUG_next_index;
1029 deefer->module_name = xstrdup(module_name);
1031 deefer->sectp = PE_SECTIONS(hModule);
1032 deefer->nsect = PE_HEADER(hModule)->FileHeader.NumberOfSections;
1042 /* look for .stabs/.stabstr sections */
1044 PIMAGE_SECTION_HEADER pe_seg = PE_SECTIONS(hModule);
1045 int i,stabsize=0,stabstrsize=0;
1046 unsigned int stabs=0,stabstr=0;
1048 for (i=0;i<nth->FileHeader.NumberOfSections;i++) {
1049 if (!strcasecmp(pe_seg[i].Name,".stab")) {
1050 stabs = pe_seg[i].VirtualAddress;
1051 stabsize = pe_seg[i].SizeOfRawData;
1053 if (!strncasecmp(pe_seg[i].Name,".stabstr",8)) {
1054 stabstr = pe_seg[i].VirtualAddress;
1055 stabstrsize = pe_seg[i].SizeOfRawData;
1058 if (stabstrsize && stabsize) {
1059 #ifdef _WE_SUPPORT_THE_STAB_TYPES_USED_BY_MINGW_TOO
1060 /* Won't work currently, since MINGW32 uses some special typedefs
1061 * which we do not handle yet. Support for them is a bit difficult.
1063 DEBUG_ParseStabs(hModule,0,stabs,stabsize,stabstr,stabstrsize);
1065 fprintf(stderr,"(stabs not loaded)");
1072 * ELF modules are also entered into the list - this is so that we
1073 * can make 'info shared' types of displays possible.
1076 DEBUG_RegisterELFDebugInfo(int load_addr, u_long size, char * name)
1078 struct deferred_debug_info * deefer;
1080 deefer = (struct deferred_debug_info *) xmalloc(sizeof(*deefer));
1084 * Read the important bits. What we do after this depends
1085 * upon the type, but this is always enough so we are able
1086 * to proceed if we know what we need to do next.
1088 deefer->dbg_size = size;
1089 deefer->dbg_info = (char *) NULL;
1091 deefer->load_addr = (char *) load_addr;
1092 deefer->dbgdir = NULL;
1093 deefer->next = dbglist;
1094 deefer->loaded = TRUE;
1095 deefer->dbg_index = DEBUG_next_index;
1096 deefer->module_name = xstrdup(name);
1107 * Process COFF debugging information embedded in a Win32 application.
1112 DEBUG_ProcessCoff(struct deferred_debug_info * deefer)
1114 struct CoffAuxSection * aux;
1115 struct CoffDebug * coff;
1116 struct CoffFiles * coff_files = NULL;
1117 struct CoffLinenum * coff_linetab;
1119 struct CoffSymbol * coff_sym;
1120 struct CoffSymbol * coff_symbol;
1121 struct CoffFiles * curr_file = NULL;
1125 struct CoffLinenum * linepnt;
1132 int nfiles_alloc = 0;
1133 struct CoffFiles orig_file;
1135 char * this_file = NULL;
1137 coff = (struct CoffDebug *) deefer->dbg_info;
1139 coff_symbol = (struct CoffSymbol *) ((unsigned int) coff + coff->SymbolOffset);
1140 coff_linetab = (struct CoffLinenum *) ((unsigned int) coff + coff->LinenumberOffset);
1141 coff_strtab = (char *) ((unsigned int) coff_symbol + 18*coff->N_Sym);
1145 for(i=0; i < coff->N_Sym; i++ )
1148 * We do this because some compilers (i.e. gcc) incorrectly
1149 * pad the structure up to a 4 byte boundary. The structure
1150 * is really only 18 bytes long, so we have to manually make sure
1153 * FIXME - there must be a way to have autoconf figure out the
1154 * correct compiler option for this. If it is always gcc, that
1155 * makes life simpler, but I don't want to force this.
1157 coff_sym = (struct CoffSymbol *) ((unsigned int) coff_symbol + 18*i);
1158 naux = coff_sym->NumberOfAuxSymbols;
1160 if( coff_sym->StorageClass == IMAGE_SYM_CLASS_FILE )
1162 if( nfiles + 1 >= nfiles_alloc )
1165 coff_files = (struct CoffFiles *) realloc( coff_files,
1166 nfiles_alloc * sizeof(struct CoffFiles));
1168 curr_file = coff_files + nfiles;
1170 curr_file->startaddr = 0xffffffff;
1171 curr_file->endaddr = 0;
1172 curr_file->filename = ((char *) coff_sym) + 18;
1173 curr_file->linetab_offset = -1;
1174 curr_file->linecnt = 0;
1175 curr_file->entries = NULL;
1176 curr_file->neps = curr_file->neps_alloc = 0;
1178 fprintf(stderr,"New file %s\n", curr_file->filename);
1185 * This guy marks the size and location of the text section
1186 * for the current file. We need to keep track of this so
1187 * we can figure out what file the different global functions
1190 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC)
1192 && (coff_sym->Type == 0)
1193 && (coff_sym->SectionNumber == 1) )
1195 aux = (struct CoffAuxSection *) ((unsigned int) coff_sym + 18);
1197 if( curr_file->linetab_offset != -1 )
1200 fprintf(stderr, "Duplicating sect from %s: %x %x %x %d %d\n",
1201 curr_file->filename,
1203 aux->NumberOfRelocations,
1204 aux->NumberOfLinenumbers,
1207 fprintf(stderr, "More sect %d %x %d %d %d\n",
1208 coff_sym->SectionNumber,
1211 coff_sym->StorageClass,
1212 coff_sym->NumberOfAuxSymbols);
1216 * Save this so we can copy bits from it.
1218 orig_file = *curr_file;
1221 * Duplicate the file entry. We have no way to describe
1222 * multiple text sections in our current way of handling things.
1224 if( nfiles + 1 >= nfiles_alloc )
1227 coff_files = (struct CoffFiles *) realloc( coff_files,
1228 nfiles_alloc * sizeof(struct CoffFiles));
1230 curr_file = coff_files + nfiles;
1232 curr_file->startaddr = 0xffffffff;
1233 curr_file->endaddr = 0;
1234 curr_file->filename = orig_file.filename;
1235 curr_file->linetab_offset = -1;
1236 curr_file->linecnt = 0;
1237 curr_file->entries = NULL;
1238 curr_file->neps = curr_file->neps_alloc = 0;
1243 fprintf(stderr, "New text sect from %s: %x %x %x %d %d\n",
1244 curr_file->filename,
1246 aux->NumberOfRelocations,
1247 aux->NumberOfLinenumbers,
1253 if( curr_file->startaddr > coff_sym->Value )
1255 curr_file->startaddr = coff_sym->Value;
1258 if( curr_file->startaddr > coff_sym->Value )
1260 curr_file->startaddr = coff_sym->Value;
1263 if( curr_file->endaddr < coff_sym->Value + aux->Length )
1265 curr_file->endaddr = coff_sym->Value + aux->Length;
1268 curr_file->linetab_offset = linetab_indx;
1269 curr_file->linecnt = aux->NumberOfLinenumbers;
1270 linetab_indx += aux->NumberOfLinenumbers;
1275 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC)
1277 && (coff_sym->SectionNumber == 1) )
1280 * This is a normal static function when naux == 0.
1281 * Just register it. The current file is the correct
1282 * one in this instance.
1284 if( coff_sym->N.Name.NotLong )
1286 memcpy(namebuff, coff_sym->N.ShortName, 8);
1288 nampnt = &namebuff[0];
1292 nampnt = coff_strtab + coff_sym->N.Name.StrTaboff;
1295 if( nampnt[0] == '_' )
1301 new_addr.off = (int) (deefer->load_addr + coff_sym->Value);
1303 if( curr_file->neps + 1 >= curr_file->neps_alloc )
1305 curr_file->neps_alloc += 10;
1306 curr_file->entries = (struct name_hash **)
1307 realloc( curr_file->entries,
1308 curr_file->neps_alloc * sizeof(struct name_hash *));
1311 fprintf(stderr,"\tAdding static symbol %s\n", nampnt);
1313 curr_file->entries[curr_file->neps++] =
1314 DEBUG_AddSymbol( nampnt, &new_addr, this_file, SYM_WIN32 );
1319 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL)
1320 && ISFCN(coff_sym->Type)
1321 && (coff_sym->SectionNumber > 0) )
1323 if( coff_sym->N.Name.NotLong )
1325 memcpy(namebuff, coff_sym->N.ShortName, 8);
1327 nampnt = &namebuff[0];
1331 nampnt = coff_strtab + coff_sym->N.Name.StrTaboff;
1335 if( nampnt[0] == '_' )
1341 new_addr.off = (int) (deefer->load_addr + coff_sym->Value);
1344 fprintf(stderr, "%d: %x %s\n", i, new_addr.off, nampnt);
1346 fprintf(stderr,"\tAdding global symbol %s\n", nampnt);
1350 * Now we need to figure out which file this guy belongs to.
1353 for(j=0; j < nfiles; j++)
1355 if( coff_files[j].startaddr <= coff_sym->Value
1356 && coff_files[j].endaddr > coff_sym->Value )
1358 this_file = coff_files[j].filename;
1362 if( coff_files[j].neps + 1 >= coff_files[j].neps_alloc )
1364 coff_files[j].neps_alloc += 10;
1365 coff_files[j].entries = (struct name_hash **)
1366 realloc( coff_files[j].entries,
1367 coff_files[j].neps_alloc * sizeof(struct name_hash *));
1369 coff_files[j].entries[coff_files[j].neps++] =
1370 DEBUG_AddSymbol( nampnt, &new_addr, this_file, SYM_WIN32 );
1375 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL)
1376 && (coff_sym->SectionNumber > 0) )
1379 * Similar to above, but for the case of data symbols.
1380 * These aren't treated as entrypoints.
1382 if( coff_sym->N.Name.NotLong )
1384 memcpy(namebuff, coff_sym->N.ShortName, 8);
1386 nampnt = &namebuff[0];
1390 nampnt = coff_strtab + coff_sym->N.Name.StrTaboff;
1394 if( nampnt[0] == '_' )
1400 new_addr.off = (int) (deefer->load_addr + coff_sym->Value);
1403 fprintf(stderr, "%d: %x %s\n", i, new_addr.off, nampnt);
1405 fprintf(stderr,"\tAdding global data symbol %s\n", nampnt);
1409 * Now we need to figure out which file this guy belongs to.
1411 DEBUG_AddSymbol( nampnt, &new_addr, NULL, SYM_WIN32 );
1416 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC)
1420 * Ignore these. They don't have anything to do with
1428 fprintf(stderr,"Skipping unknown entry %d %d %d\n", coff_sym->StorageClass,
1429 coff_sym->SectionNumber, naux);
1433 * For now, skip past the aux entries.
1440 * OK, we now should have a list of files, and we should have a list
1441 * of entrypoints. We need to sort the entrypoints so that we are
1442 * able to tie the line numbers with the given functions within the
1445 if( coff_files != NULL )
1447 for(j=0; j < nfiles; j++)
1449 if( coff_files[j].entries != NULL )
1451 qsort(coff_files[j].entries, coff_files[j].neps,
1452 sizeof(struct name_hash *), DEBUG_cmp_sym);
1457 * Now pick apart the line number tables, and attach the entries
1458 * to the given functions.
1460 for(j=0; j < nfiles; j++)
1463 if( coff_files[j].neps != 0 )
1464 for(k=0; k < coff_files[j].linecnt; k++)
1467 * Another monstrosity caused by the fact that we are using
1468 * a 6 byte structure, and gcc wants to pad structures to 4 byte
1469 * boundaries. Otherwise we could just index into an array.
1471 linepnt = (struct CoffLinenum *)
1472 ((unsigned int) coff_linetab +
1473 6*(coff_files[j].linetab_offset + k));
1475 * If we have spilled onto the next entrypoint, then
1476 * bump the counter..
1480 if (i+1 >= coff_files[j].neps) break;
1481 DEBUG_GetSymbolAddr(coff_files[j].entries[i+1], &new_addr);
1482 if( (((unsigned int)deefer->load_addr +
1483 linepnt->VirtualAddr) >= new_addr.off) )
1490 * Add the line number. This is always relative to the
1491 * start of the function, so we need to subtract that offset
1494 DEBUG_GetSymbolAddr(coff_files[j].entries[i], &new_addr);
1495 DEBUG_AddLineNumber(coff_files[j].entries[i],
1497 (unsigned int) deefer->load_addr
1498 + linepnt->VirtualAddr
1506 if( coff_files != NULL )
1508 for(j=0; j < nfiles; j++)
1510 if( coff_files[j].entries != NULL )
1512 free(coff_files[j].entries);
1523 * Process a codeview line number table. Digestify the thing so that
1524 * we can easily reference the thing when we process the rest of
1527 static struct codeview_linetab_hdr *
1528 DEBUG_SnarfLinetab(char * linetab,
1532 char filename[PATH_MAX];
1533 unsigned int * filetab;
1537 struct codeview_linetab_hdr * lt_hdr;
1538 unsigned int * lt_ptr;
1542 union any_size pnt2;
1543 struct startend * start;
1547 * Now get the important bits.
1549 pnt = (union any_size) linetab;
1553 filetab = (unsigned int *) pnt.c;
1556 * Now count up the number of segments in the file.
1559 for(i=0; i<nfile; i++)
1561 pnt2 = (union any_size) (linetab + filetab[i]);
1566 * Next allocate the header we will be returning.
1567 * There is one header for each segment, so that we can reach in
1568 * and pull bits as required.
1570 lt_hdr = (struct codeview_linetab_hdr *)
1571 xmalloc((nseg + 1) * sizeof(*lt_hdr));
1572 if( lt_hdr == NULL )
1577 memset(lt_hdr, 0, sizeof(*lt_hdr) * (nseg+1));
1580 * Now fill the header we will be returning, one for each segment.
1581 * Note that this will basically just contain pointers into the existing
1582 * line table, and we do not actually copy any additional information
1583 * or allocate any additional memory.
1587 for(i=0; i<nfile; i++)
1590 * Get the pointer into the segment information.
1592 pnt2 = (union any_size) (linetab + filetab[i]);
1593 file_segcount = *pnt2.s;
1596 lt_ptr = (unsigned int *) pnt2.c;
1597 start = (struct startend *) (lt_ptr + file_segcount);
1600 * Now snarf the filename for all of the segments for this file.
1602 fn = (unsigned char *) (start + file_segcount);
1603 memset(filename, 0, sizeof(filename));
1604 memcpy(filename, fn + 1, *fn);
1605 fn = strdup(filename);
1607 for(k = 0; k < file_segcount; k++, this_seg++)
1609 pnt2 = (union any_size) (linetab + lt_ptr[k]);
1610 lt_hdr[this_seg].start = start[k].start;
1611 lt_hdr[this_seg].end = start[k].end;
1612 lt_hdr[this_seg].sourcefile = fn;
1613 lt_hdr[this_seg].segno = *pnt2.s++;
1614 lt_hdr[this_seg].nline = *pnt2.s++;
1615 lt_hdr[this_seg].offtab = pnt2.ui;
1616 lt_hdr[this_seg].linetab = (unsigned short *)
1617 (pnt2.ui + lt_hdr[this_seg].nline);
1628 DEBUG_SnarfCodeView( struct deferred_debug_info * deefer,
1631 struct codeview_linetab_hdr * linetab)
1633 struct name_hash * curr_func = NULL;
1634 struct wine_locals * curr_sym = NULL;
1641 IMAGE_SECTION_HEADER * sectp;
1642 union codeview_symbol * sym;
1643 char symname[PATH_MAX];
1644 struct name_hash * thunk_sym = NULL;
1646 ptr = (union any_size) cv_data;
1647 nsect = deefer->nsect;
1648 sectp = deefer->sectp;
1651 * Skip over the first word. Don't really know what it means, but
1657 * Loop over the different types of records and whenever we
1658 * find something we are interested in, record it and move on.
1660 while( ptr.c - cv_data < size )
1662 sym = (union codeview_symbol *) ptr.c;
1664 if( sym->generic.len - sizeof(int) == (ptr.c - cv_data) )
1667 * This happens when we have indirect symbols that VC++ 4.2
1668 * sometimes uses when there isn't a line number table.
1669 * We ignore it - we will process and enter all of the
1670 * symbols in the global symbol table anyways, so there
1671 * isn't much point in keeping track of all of this crap.
1676 memset(symname, 0, sizeof(symname));
1677 switch(sym->generic.id)
1683 * First, a couple of sanity checks.
1685 if( sym->data.namelen == 0 )
1690 if( sym->data.seg == 0 || sym->data.seg > nsect )
1696 * Global and local data symbols. We don't associate these
1697 * with any given source file.
1700 memcpy(symname, sym->data.name, sym->data.namelen);
1702 new_addr.type = DEBUG_GetCVType(sym->data.symtype);
1703 new_addr.off = (unsigned int) deefer->load_addr +
1704 sectp[sym->data.seg - 1].VirtualAddress +
1706 DEBUG_AddSymbol( symname, &new_addr, NULL, SYM_WIN32 | SYM_DATA );
1710 * Sort of like a global function, but it just points
1711 * to a thunk, which is a stupid name for what amounts to
1712 * a PLT slot in the normal jargon that everyone else uses.
1714 memcpy(symname, sym->thunk.name, sym->thunk.namelen);
1716 new_addr.type = NULL;
1717 new_addr.off = (unsigned int) deefer->load_addr +
1718 sectp[sym->thunk.segment - 1].VirtualAddress +
1720 thunk_sym = DEBUG_AddSymbol( symname, &new_addr, NULL,
1721 SYM_WIN32 | SYM_FUNC);
1722 DEBUG_SetSymbolSize(thunk_sym, sym->thunk.thunk_len);
1727 * Global and static functions.
1729 memcpy(symname, sym->proc.name, sym->proc.namelen);
1731 new_addr.type = DEBUG_GetCVType(sym->proc.proctype);
1732 new_addr.off = (unsigned int) deefer->load_addr +
1733 sectp[sym->proc.segment - 1].VirtualAddress +
1736 * See if we can find a segment that this goes with. If so,
1737 * it means that we also may have line number information
1738 * for this function.
1740 for(i=0; linetab[i].linetab != NULL; i++)
1742 if( ((unsigned int) deefer->load_addr
1743 + sectp[linetab[i].segno - 1].VirtualAddress
1744 + linetab[i].start <= new_addr.off)
1745 && ((unsigned int) deefer->load_addr
1746 + sectp[linetab[i].segno - 1].VirtualAddress
1747 + linetab[i].end > new_addr.off) )
1753 DEBUG_Normalize(curr_func);
1754 if( linetab[i].linetab == NULL )
1756 curr_func = DEBUG_AddSymbol( symname, &new_addr, NULL,
1757 SYM_WIN32 | SYM_FUNC);
1762 * First, create the entry. Then dig through the linetab
1763 * and add whatever line numbers are appropriate for this
1766 curr_func = DEBUG_AddSymbol( symname, &new_addr,
1767 linetab[i].sourcefile,
1768 SYM_WIN32 | SYM_FUNC);
1769 for(j=0; j < linetab[i].nline; j++)
1771 if( linetab[i].offtab[j] >= sym->proc.offset
1772 && linetab[i].offtab[j] < sym->proc.offset
1773 + sym->proc.proc_len )
1775 DEBUG_AddLineNumber(curr_func, linetab[i].linetab[j],
1776 linetab[i].offtab[j] - sym->proc.offset);
1783 * Add information about where we should set breakpoints
1786 DEBUG_SetSymbolBPOff(curr_func, sym->proc.debug_start);
1787 DEBUG_SetSymbolSize(curr_func, sym->proc.proc_len);
1791 * Function parameters and stack variables.
1793 memcpy(symname, sym->stack.name, sym->stack.namelen);
1794 curr_sym = DEBUG_AddLocal(curr_func,
1800 DEBUG_SetLocalSymbolType(curr_sym, DEBUG_GetCVType(sym->stack.symtype));
1808 * Adjust pointer to point to next entry, rounding up to a word
1809 * boundary. MS preserving alignment? Stranger things have
1812 if( sym->generic.id == S_PROCREF
1813 || sym->generic.id == S_DATAREF
1814 || sym->generic.id == S_UNKNOWN )
1816 len = (sym->generic.len + 3) & ~3;
1817 len += ptr.c[16] + 1;
1818 ptr.c += (len + 3) & ~3;
1822 ptr.c += (sym->generic.len + 3) & ~3;
1826 if( linetab != NULL )
1836 * Process PDB file which contains debug information.
1838 * These are really weird beasts. They are intended to be incrementally
1839 * updated by the incremental linker, and this means that you need to
1840 * be able to remove and add information. Thus the PDB file is sort of
1841 * like a block structured device, with a freelist and lists of extent numbers
1842 * that are used to get the relevant pieces. In all cases seen so far, the
1843 * blocksize is always 0x400 bytes. The header has a field which apparently
1844 * holds the blocksize, so if it ever changes we are safe.
1846 * In general, every time we need to extract something from the pdb file,
1847 * it is easier to copy it into another buffer so we have the information
1848 * in one contiguous block rather than attempt to try and keep track of when
1849 * we need to grab another extent from the pdb file.
1851 * The thing that is a real pain about some MS stuff is that they choose
1852 * data structures which are not representable in C. Thus we have to
1853 * hack around and diddle pointers.
1857 DEBUG_ProcessPDBFile(struct deferred_debug_info * deefer, char * full_filename)
1859 char * addr = (char *) 0xffffffff;
1860 unsigned int blocksize;
1861 unsigned int bufflen = 0;
1862 char * buffer = NULL;
1863 unsigned short * extent_table;
1865 struct file_ent * fent;
1866 char filename[MAX_PATHNAME_LEN];
1867 struct file_list * filelist = NULL;
1868 unsigned int gsym_record = 0;
1869 char * gsymtab = NULL;
1870 struct filetab_hdr * hd;
1873 unsigned int last_extent;
1874 struct codeview_linetab_hdr * linetab;
1875 unsigned int nblocks;
1877 unsigned int offset;
1878 struct codeview_pdb_hdr * pdbhdr;
1880 struct stat statbuf;
1882 unsigned short * table;
1884 unsigned int toc_blocks;
1886 LocateDebugInfoFile(full_filename, filename);
1887 status = stat(filename, &statbuf);
1890 fprintf(stderr, "-Unable to open .PDB file %s\n", filename);
1895 * Now open the file, so that we can mmap() it.
1897 fd = open(filename, O_RDONLY);
1900 fprintf(stderr, "-Unable to open .DBG file %s\n", filename);
1906 * Now mmap() the file.
1908 addr = mmap(0, statbuf.st_size, PROT_READ,
1909 MAP_PRIVATE, fd, 0);
1910 if( addr == (char *) 0xffffffff )
1912 fprintf(stderr, "-Unable to mmap .DBG file %s\n", filename);
1917 * Now that we have the formalities over and done with, we need
1918 * to find the table of contents for the PDB file.
1920 pdbhdr = (struct codeview_pdb_hdr *) addr;
1921 blocksize = pdbhdr->blocksize;
1922 last_extent = (statbuf.st_size + blocksize - 1) / blocksize;
1925 * The TOC itself isn't always contiguous, so we need to extract a few
1926 * extents from the file to form the TOC.
1928 toc_blocks = (pdbhdr->toc_len + blocksize - 1) / blocksize;
1929 toc = (char *) xmalloc(toc_blocks * blocksize);
1930 table = pdbhdr->toc_ext;
1931 for(i=0; i < toc_blocks; i++)
1933 memcpy(toc + blocksize*i, addr + table[i]*blocksize, blocksize);
1937 * Next build our own table which will have the size and extent block
1938 * list for each record in the PDB file.
1940 * The TOC starts out with the number of files. Then it is followed by
1941 * (npair * 2*sizeof(int)) bytes of information, which are pairs of ints.
1942 * The first one is the size of the record (in bytes), and the second one
1943 * is something else which I haven't figured out yet.
1945 pnt = (unsigned int *) toc;
1947 extent_table = (unsigned short *) ((unsigned int) toc +
1948 npair * 2 * sizeof(int) + sizeof(int));
1953 if( sizeof(int) + 2*sizeof(int)*npair > pdbhdr->toc_len )
1958 filelist = (struct file_list *) xmalloc(npair * sizeof(*filelist));
1959 if( filelist == NULL )
1963 memset(filelist, 0, npair * sizeof(*filelist));
1966 for(i=0; i < npair; i++)
1968 filelist[i].record_len = pnt[i*2];
1969 filelist[i].nextents = (filelist[i].record_len + blocksize - 1)
1971 filelist[i].extent_list = extent_table + nblocks;
1972 nblocks += filelist[i].nextents;
1975 * These get filled in later when we parse one of the records.
1977 filelist[i].linetab_offset = 0;
1978 filelist[i].linetab_len = 0;
1982 * OK, now walk through the various records and pick out the bits we
1983 * really want to see. Some of the records are extra special, and
1984 * we need to handle these a little bit differently.
1986 for(i=0; i < npair; i++)
1988 if( filelist[i].record_len == 0xffffffff )
1994 * Make sure our buffer is large enough to hold the record.
1996 if( bufflen < filelist[i].nextents * blocksize )
1998 bufflen = filelist[i].nextents * blocksize;
1999 buffer = (char *) realloc(buffer, bufflen);
2003 * Do this just for completeness. It makes debugging easier
2004 * if we have a clean indication of where the record ends.
2006 memset(buffer, 0, filelist[i].nextents * blocksize);
2009 * Next, build the record using the extent list.
2011 for(j=0; j < filelist[i].nextents; j++)
2013 memcpy(buffer + j * blocksize,
2014 addr + filelist[i].extent_list[j] * blocksize,
2018 pnt = (unsigned int *) buffer;
2021 * OK, now figure out what to do with it.
2025 * Always ignore the first entry. It seems to contain a backup copy
2026 * of the TOC (the last time the file was modified??)
2034 * The second entry as a id block. It contains a magic number
2035 * to identify the compiler, plus it also contains the timestamp
2036 * which must match the timestamp in the executable.
2041 if( ((*pnt != 19950623) && (*pnt != 19950814))
2042 || (filelist[i].record_len != 0x24)
2043 || (pnt[1] != ((struct CodeViewDebug *)(deefer->dbg_info))->cv_timestamp) )
2050 * The third entry contains pointers to the global symbol table,
2051 * plus it also contains additional information about each record
2056 hd = (struct filetab_hdr *) buffer;
2058 gsym_record = hd->gsym_file;
2059 gsymtab = (char *) xmalloc( filelist[gsym_record].nextents
2061 memset(gsymtab, 0, filelist[gsym_record].nextents * blocksize);
2063 for(j=0; j < filelist[gsym_record].nextents; j++)
2065 memcpy(gsymtab + j * blocksize,
2066 addr + filelist[gsym_record].extent_list[j] * blocksize,
2071 * This record also contains information about where in the
2072 * remaining records we will be able to find the start of the
2073 * line number table. We could locate that bit using heuristics,
2074 * but since we have the info handy, we might as well use it.
2076 offset = sizeof(*hd);
2079 fent = (struct file_ent *) (buffer + offset);
2080 if( offset > hd->ftab_len )
2085 if( fent->file_number == 0 || fent->file_number >= npair )
2090 filelist[fent->file_number].linetab_offset =
2091 fent->linetab_offset;
2092 filelist[fent->file_number].linetab_len =
2095 * Figure out the offset of the next entry.
2096 * There is a fixed part of the record and a variable
2097 * length filename which we must also skip past.
2099 offset += ((unsigned int) &fent->filename - (unsigned int) fent)
2100 + strlen(fent->filename) + 1;
2101 offset += strlen(buffer+offset) + 1;
2102 offset = (offset + 3) & ~3;
2108 * Two different magic numbers used as dates.
2109 * These indicate the 'type' table.
2111 if( *pnt == 19950410
2112 || *pnt == 19951122 )
2114 DEBUG_ParseTypeTable(buffer, filelist[i].record_len);
2119 * This is something we really want to look at, since it contains
2120 * real debug info. Anything that doesn't match this can be
2126 * First, snag the line table, if we have one. This always
2127 * occurs at the end of the record, so we take the linetab
2128 * offset as the end of the normal part of the record.
2131 if( filelist[i].linetab_len != 0 )
2133 linetab = DEBUG_SnarfLinetab(buffer + filelist[i].linetab_offset,
2134 filelist[i].linetab_len);
2135 DEBUG_SnarfCodeView(deefer, buffer,
2136 filelist[i].linetab_offset,
2141 DEBUG_SnarfCodeView(deefer, buffer,
2142 filelist[i].record_len,
2150 * Finally, process the global symbol table itself. There isn't
2151 * a line number component to this, so we just toss everything
2152 * into the mix and it all should work out.
2154 if( gsym_record != 0 )
2156 DEBUG_SnarfCodeView(deefer, gsymtab - sizeof(int),
2157 filelist[gsym_record].record_len,
2163 if( gsymtab != NULL )
2169 if( buffer != NULL )
2174 if( filelist != NULL )
2179 if( addr != (char *) 0xffffffff )
2181 munmap(addr, statbuf.st_size);
2193 * Process DBG file which contains debug information.
2197 DEBUG_ProcessDBGFile(struct deferred_debug_info * deefer, char * filename)
2199 char * addr = (char *) 0xffffffff;
2201 struct CV4_DirHead * codeview_dir;
2202 struct CV4_DirEnt * codeview_dent;
2203 PIMAGE_DEBUG_DIRECTORY dbghdr;
2204 struct deferred_debug_info deefer2;
2208 struct codeview_linetab_hdr * linetab;
2210 PIMAGE_SEPARATE_DEBUG_HEADER pdbg = NULL;
2211 IMAGE_SECTION_HEADER * sectp;
2212 struct stat statbuf;
2214 char dbg_file[MAX_PATHNAME_LEN];
2216 LocateDebugInfoFile(filename, dbg_file);
2217 status = stat(dbg_file, &statbuf);
2220 fprintf(stderr, "-Unable to open .DBG file %s\n", dbg_file);
2225 * Now open the file, so that we can mmap() it.
2227 fd = open(dbg_file, O_RDONLY);
2230 fprintf(stderr, "Unable to open .DBG file %s\n", dbg_file);
2236 * Now mmap() the file.
2238 addr = mmap(0, statbuf.st_size, PROT_READ,
2239 MAP_PRIVATE, fd, 0);
2240 if( addr == (char *) 0xffffffff )
2242 fprintf(stderr, "Unable to mmap .DBG file %s\n", dbg_file);
2246 pdbg = (PIMAGE_SEPARATE_DEBUG_HEADER) addr;
2248 if( pdbg->TimeDateStamp != deefer->dbgdir->TimeDateStamp )
2250 fprintf(stderr, "Warning - %s has incorrect internal timestamp\n",
2254 Well, sometimes this happens to DBG files which ARE REALLY the right .DBG
2255 files but nonetheless this check fails. Anyway, WINDBG (debugger for
2256 Windows by Microsoft) loads debug symbols which have incorrect timestamps.
2260 fprintf(stderr, "Processing symbols from %s...\n", dbg_file);
2262 dbghdr = (PIMAGE_DEBUG_DIRECTORY) ( addr + sizeof(*pdbg)
2263 + pdbg->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)
2264 + pdbg->ExportedNamesSize);
2266 sectp = (PIMAGE_SECTION_HEADER) ((char *) pdbg + sizeof(*pdbg));
2267 nsect = pdbg->NumberOfSections;
2269 for( i=0; i < pdbg->DebugDirectorySize / sizeof(*pdbg); i++, dbghdr++ )
2271 switch(dbghdr->Type)
2273 case IMAGE_DEBUG_TYPE_COFF:
2275 * Dummy up a deferred debug header to handle the
2276 * COFF stuff embedded within the DBG file.
2278 memset((char *) &deefer2, 0, sizeof(deefer2));
2279 deefer2.dbg_info = (addr + dbghdr->PointerToRawData);
2280 deefer2.dbg_size = dbghdr->SizeOfData;
2281 deefer2.load_addr = deefer->load_addr;
2283 DEBUG_ProcessCoff(&deefer2);
2285 case IMAGE_DEBUG_TYPE_CODEVIEW:
2287 * This is the older format by which codeview stuff is
2288 * stored, known as the 'NB09' format. Newer executables
2289 * and dlls created by VC++ use PDB files instead, which
2290 * have lots of internal similarities, but the overall
2291 * format and structure is quite different.
2293 codeview = (addr + dbghdr->PointerToRawData);
2296 * The first thing in the codeview section should be
2297 * an 'NB09' identifier. As a sanity check, make sure
2300 if( *((unsigned int*) codeview) != 0x3930424e )
2306 * Next we need to find the directory. This is easy too.
2308 codeview_dir = (struct CV4_DirHead *)
2309 (codeview + ((unsigned int*) codeview)[1]);
2312 * Some more sanity checks. Make sure that everything
2313 * is as we expect it.
2315 if( codeview_dir->next_offset != 0
2316 || codeview_dir->dhsize != sizeof(*codeview_dir)
2317 || codeview_dir->desize != sizeof(*codeview_dent) )
2321 codeview_dent = (struct CV4_DirEnt *) (codeview_dir + 1);
2323 for(j=0; j < codeview_dir->ndir; j++, codeview_dent++)
2325 if( codeview_dent->subsect_number == sstAlignSym )
2328 * Check the previous entry. If it is a
2329 * sstSrcModule, it contains the line number
2330 * info for this file.
2333 if( codeview_dent[1].module_number == codeview_dent[0].module_number
2334 && codeview_dent[1].subsect_number == sstSrcModule )
2336 linetab = DEBUG_SnarfLinetab(
2337 codeview + codeview_dent[1].offset,
2338 codeview_dent[1].size);
2341 if( codeview_dent[-1].module_number == codeview_dent[0].module_number
2342 && codeview_dent[-1].subsect_number == sstSrcModule )
2344 linetab = DEBUG_SnarfLinetab(
2345 codeview + codeview_dent[-1].offset,
2346 codeview_dent[-1].size);
2349 * Now process the CV stuff.
2351 DEBUG_SnarfCodeView(deefer,
2352 codeview + codeview_dent->offset,
2353 codeview_dent->size,
2365 if( addr != (char *) 0xffffffff )
2367 munmap(addr, statbuf.st_size);
2379 DEBUG_ProcessDeferredDebug()
2381 struct deferred_debug_info * deefer;
2382 struct CodeViewDebug * cvd;
2383 struct MiscDebug * misc;
2388 DEBUG_InitCVDataTypes();
2390 for(deefer = dbglist; deefer; deefer = deefer->next)
2392 if( deefer->loaded )
2397 if( last_proc != deefer->dbg_index )
2401 fprintf(stderr, "DeferredDebug for:");
2404 fprintf(stderr, " %s",deefer->module_name);
2405 last_proc = deefer->dbg_index;
2408 switch(deefer->dbgdir->Type)
2410 case IMAGE_DEBUG_TYPE_COFF:
2412 * Standard COFF debug information that VC++ adds when you
2413 * use /debugtype:both with the linker.
2416 fprintf(stderr, "Processing COFF symbols...\n");
2418 DEBUG_ProcessCoff(deefer);
2420 case IMAGE_DEBUG_TYPE_CODEVIEW:
2422 * This is a pointer to a PDB file of some sort.
2424 cvd = (struct CodeViewDebug *) deefer->dbg_info;
2426 if( strcmp(cvd->cv_nbtype, "NB10") != 0 )
2429 * Whatever this is, we don't know how to deal with
2434 DEBUG_ProcessPDBFile(deefer, cvd->cv_name);
2436 fprintf(stderr, "Processing PDB file %s\n", cvd->cv_name);
2439 case IMAGE_DEBUG_TYPE_MISC:
2441 * A pointer to a .DBG file of some sort. These files
2442 * can contain either CV4 or COFF information. Open
2443 * the file, and try to do the right thing with it.
2445 misc = (struct MiscDebug *) deefer->dbg_info;
2447 filename = strrchr((char *) &misc->Data, '.');
2450 * Ignore the file if it doesn't have a .DBG extension.
2452 if( (filename == NULL)
2453 || ( (strcmp(filename, ".dbg") != 0)
2454 && (strcmp(filename, ".DBG") != 0)) )
2459 filename = (char *) &misc->Data;
2462 * Do the dirty deed...
2464 DEBUG_ProcessDBGFile(deefer, filename);
2469 * We should never get here...
2475 fprintf(stderr, "\n");
2480 /***********************************************************************
2483 * Display shared libarary information.
2485 void DEBUG_InfoShare(void)
2487 struct deferred_debug_info * deefer;
2489 fprintf(stderr,"Address\t\tModule\tName\n");
2491 for(deefer = dbglist; deefer; deefer = deefer->next)
2493 fprintf(stderr,"0x%8.8x\t(%s)\t%s\n", (unsigned int) deefer->load_addr,
2494 deefer->module ? "Win32" : "ELF", deefer->module_name);