2 * File msc.c - read VC++ debug information from COFF and eventually
5 * Copyright (C) 1996, Eric Youngdale.
7 * Note - this handles reading debug information for 32 bit applications
8 * that run under Windows-NT for example. I doubt that this would work well
9 * for 16 bit applications, but I don't think it really matters since the
10 * file format is different, and we should never get in here in such cases.
13 * Get 16 bit CV stuff working.
14 * Add symbol size to internal symbol table.
20 #include <sys/types.h>
28 #define PATH_MAX _MAX_PATH
39 * This is an index we use to keep track of the debug information
40 * when we have multiple sources. We use the same database to also
41 * allow us to do an 'info shared' type of deal, and we use the index
42 * to eliminate duplicates.
44 static int DEBUG_next_index = 0;
55 * This is a convenience structure used to map portions of the
65 * This is how we reference the various record types.
81 unsigned short symtype;
82 unsigned char namelen;
83 unsigned char name[1];
94 unsigned short segment;
95 unsigned short thunk_len;
97 unsigned char namelen;
98 unsigned char name[1];
104 unsigned int pparent;
107 unsigned int proc_len;
108 unsigned int debug_start;
109 unsigned int debug_end;
111 unsigned short segment;
112 unsigned short proctype;
114 unsigned char namelen;
115 unsigned char name[1];
119 short int len; /* Total length of this entry */
120 short int id; /* Always S_BPREL32 */
121 unsigned int offset; /* Stack offset relative to BP */
122 unsigned short symtype;
123 unsigned char namelen;
124 unsigned char name[1];
142 unsigned char variant[1];
150 unsigned char bitoff;
160 unsigned char arrlen;
161 unsigned char namelen;
162 unsigned char name[1];
174 unsigned short structlen;
175 unsigned char namelen;
176 unsigned char name[1];
186 unsigned short un_len;
187 unsigned char namelen;
188 unsigned char name[1];
199 unsigned char namelen;
200 unsigned char name[1];
207 unsigned short int value;
208 unsigned char namelen;
209 unsigned char name[1];
217 unsigned short int offset;
218 unsigned char namelen;
219 unsigned char name[1];
230 unsigned char namelen;
231 unsigned char name[1];
236 #define S_BPREL32 0x200
237 #define S_LDATA32 0x201
238 #define S_GDATA32 0x202
239 #define S_PUB32 0x203
240 #define S_LPROC32 0x204
241 #define S_GPROC32 0x205
242 #define S_THUNK32 0x206
243 #define S_BLOCK32 0x207
244 #define S_WITH32 0x208
245 #define S_LABEL32 0x209
247 #define S_PROCREF 0x400
248 #define S_DATAREF 0x401
249 #define S_ALIGN 0x402
250 #define S_UNKNOWN 0x403
253 * This covers the basic datatypes that VC++ seems to be using these days.
254 * 32 bit mode only. There are additional numbers for the pointers in 16
255 * bit mode. There are many other types listed in the documents, but these
256 * are apparently not used by the compiler, or represent pointer types
259 #define T_NOTYPE 0x0000 /* Notype */
260 #define T_ABS 0x0001 /* Abs */
261 #define T_VOID 0x0003 /* Void */
262 #define T_CHAR 0x0010 /* signed char */
263 #define T_SHORT 0x0011 /* short */
264 #define T_LONG 0x0012 /* long */
265 #define T_QUAD 0x0013 /* long long */
266 #define T_UCHAR 0x0020 /* unsigned char */
267 #define T_USHORT 0x0021 /* unsigned short */
268 #define T_ULONG 0x0022 /* unsigned long */
269 #define T_UQUAD 0x0023 /* unsigned long long */
270 #define T_REAL32 0x0040 /* float */
271 #define T_REAL64 0x0041 /* double */
272 #define T_RCHAR 0x0070 /* real char */
273 #define T_WCHAR 0x0071 /* wide char */
274 #define T_INT4 0x0074 /* int */
275 #define T_UINT4 0x0075 /* unsigned int */
277 #define T_32PVOID 0x0403 /* 32 bit near pointer to void */
278 #define T_32PCHAR 0x0410 /* 16:32 near pointer to signed char */
279 #define T_32PSHORT 0x0411 /* 16:32 near pointer to short */
280 #define T_32PLONG 0x0412 /* 16:32 near pointer to int */
281 #define T_32PQUAD 0x0413 /* 16:32 near pointer to long long */
282 #define T_32PUCHAR 0x0420 /* 16:32 near pointer to unsigned char */
283 #define T_32PUSHORT 0x0421 /* 16:32 near pointer to unsigned short */
284 #define T_32PULONG 0x0422 /* 16:32 near pointer to unsigned int */
285 #define T_32PUQUAD 0x0423 /* 16:32 near pointer to long long */
286 #define T_32PREAL32 0x0440 /* 16:32 near pointer to float */
287 #define T_32PREAL64 0x0441 /* 16:32 near pointer to float */
288 #define T_32PRCHAR 0x0470 /* 16:32 near pointer to real char */
289 #define T_32PWCHAR 0x0471 /* 16:32 near pointer to real char */
290 #define T_32PINT4 0x0474 /* 16:32 near pointer to int */
291 #define T_32PUINT4 0x0475 /* 16:32 near pointer to unsigned int */
293 #define LF_MODIFIER 0x1
294 #define LF_POINTER 0x2
297 #define LF_STRUCTURE 0x5
299 #define LF_ENUMERATION 0x7
300 #define LF_PROCEDURE 0x8
301 #define LF_MFUNCTION 0x9
302 #define LF_VTSHAPE 0xa
303 #define LF_BARRAY 0xd
304 #define LF_DIMARRAY 0x11
305 #define LF_VFTPATH 0x12
307 #define LF_SKIP 0x200
308 #define LF_ARGLIST 0x201
309 #define LF_FIELDLIST 0x204
310 #define LF_DERIVED 0x205
311 #define LF_BITFIELD 0x206
313 #define LF_BCLASS 0x400
314 #define LF_VBCLASS 0x401
315 #define LF_IVBCLASS 0x402
316 #define LF_ENUMERATE 0x403
317 #define LF_FRIENDFCN 0x404
318 #define LF_INDEX 0x405
319 #define LF_MEMBER 0x406
320 #define LF_STMEMBER 0x407
321 #define LF_METHOD 0x408
322 #define LF_NESTEDTYPE 0x409
323 #define LF_VFUNCTAB 0x40a
324 #define LF_FRIENDCLS 0x40b
325 #define LF_ONEMETHOD 0x40c
326 #define LF_FUNCOFF 0x40d
328 #define MAX_BUILTIN_TYPES 0x480
329 static struct datatype * cv_basic_types[MAX_BUILTIN_TYPES];
330 static int num_cv_defined_types = 0;
331 static struct datatype **cv_defined_types = NULL;
334 * For the type CODEVIEW debug directory entries, the debug directory
335 * points to a structure like this. The cv_name field is the name
336 * of an external .PDB file.
341 unsigned int cv_timestamp;
347 unsigned int DataType;
355 * This is the header that the COFF variety of debug header points to.
359 unsigned int SymbolOffset;
360 unsigned int N_Linenum;
361 unsigned int LinenumberOffset;
362 unsigned int Unused[4];
366 unsigned int VirtualAddr;
367 unsigned short int Linenum;
371 unsigned int startaddr;
372 unsigned int endaddr;
376 struct name_hash **entries;
386 unsigned int NotLong;
387 unsigned int StrTaboff;
394 unsigned char NumberOfAuxSymbols;
397 struct CoffAuxSection{
399 unsigned short NumberOfRelocations;
400 unsigned short NumberOfLinenumbers;
401 unsigned int CheckSum;
407 * These two structures are used in the directory within a .DBG file
408 * to locate the individual important bits that we might want to see.
411 short unsigned int dhsize;
412 short unsigned int desize;
414 unsigned int next_offset;
419 short unsigned int subsect_number;
420 short unsigned int module_number;
426 * These are the values of interest that the subsect_number field takes.
428 #define sstAlignSym 0x125
429 #define sstSrcModule 0x127
431 struct codeview_linetab_hdr
438 unsigned short * linetab;
439 unsigned int * offtab;
442 struct codeview_pdb_hdr
445 unsigned int blocksize; /* Extent size */
446 unsigned short loc_freelist; /* freelist. */
447 unsigned short alloc_filesize; /* # extents allocated. */
448 unsigned int toc_len;
449 unsigned int unknown;
450 unsigned short toc_ext[1]; /* array of extent #'s for toc. */
454 * This is our own structure that we use to keep track of the contents
461 short int * extent_list;
462 unsigned int linetab_offset;
463 unsigned int linetab_len;
467 * These are the structures that represent how the file table is set up
468 * within the PDB file.
472 unsigned short tab1_file;
473 unsigned short tab2_file;
474 unsigned short gsym_file;
475 unsigned short padding;
476 unsigned int ftab_len;
477 unsigned int fofftab_len;
478 unsigned int hash_len;
479 unsigned int strtab_len;
484 unsigned int reserved1;
485 unsigned short datasect_segment;
486 unsigned short reserved2;
487 unsigned int datasect_offset;
488 unsigned int datasect_size;
489 unsigned int datasect_flags;
490 unsigned short reserved3;
491 unsigned short index;
492 unsigned short num6a;
493 unsigned short file_number;
494 unsigned int linetab_offset;
495 unsigned int linetab_len;
499 unsigned char filename[1];
503 ********************************************************************
505 struct deferred_debug_info
507 struct deferred_debug_info * next;
513 PIMAGE_DEBUG_DIRECTORY dbgdir;
514 PIMAGE_SECTION_HEADER sectp;
520 struct deferred_debug_info * dbglist = NULL;
523 * A simple macro that tells us whether a given COFF symbol is a
526 #define N_TMASK 0x0030
527 #define IMAGE_SYM_DTYPE_FUNCTION 2
529 #define ISFCN(x) (((x) & N_TMASK) == (IMAGE_SYM_DTYPE_FUNCTION << N_BTSHFT))
533 * This is what we are looking for in the COFF symbols.
535 #define IMAGE_SYM_CLASS_EXTERNAL 0x2
536 #define IMAGE_SYM_CLASS_STATIC 0x3
537 #define IMAGE_SYM_CLASS_FILE 0x67
540 struct datatype * DEBUG_GetCVType(int typeno)
542 struct datatype * dt = NULL;
545 * Convert Codeview type numbers into something we can grok internally.
546 * Numbers < 0x1000 are all fixed builtin types. Numbers from 0x1000 and
547 * up are all user defined (structs, etc).
549 if( typeno < 0x1000 )
551 if( typeno < MAX_BUILTIN_TYPES )
553 dt = cv_basic_types[typeno];
558 if( typeno - 0x1000 < num_cv_defined_types )
560 dt = cv_defined_types[typeno - 0x1000];
568 DEBUG_ParseTypeTable(char * table, int len)
572 enum debug_type fieldtype;
576 struct datatype * subtype;
578 union codeview_type * type;
579 union codeview_type * type2;
580 struct datatype * typeptr;
584 ptr = (union any_size) (table + 16);
585 while( ptr.c - table < len )
587 type = (union codeview_type *) ptr.c;
589 if( curr_type - 0x1000 >= num_cv_defined_types )
591 num_cv_defined_types += 0x100;
592 cv_defined_types = (struct datatype **) realloc(cv_defined_types,
593 num_cv_defined_types * sizeof(struct datatype *));
594 memset(cv_defined_types + num_cv_defined_types - 0x100,
596 0x100 * sizeof(struct datatype *));
597 if( cv_defined_types == NULL )
603 switch(type->generic.id)
606 cv_defined_types[curr_type - 0x1000] =
607 DEBUG_FindOrMakePointerType(DEBUG_GetCVType(type->pointer.datatype));
610 if( type->array.arrlen >= 0x8000 )
613 * This is a numeric leaf, I am too lazy to handle this right
616 fprintf(stderr, "Ignoring large numberic leaf.\n");
619 if( type->array.namelen != 0 )
621 memset(symname, 0, sizeof(symname));
622 memcpy(symname, type->array.name, type->array.namelen);
623 typeptr = DEBUG_NewDataType(DT_ARRAY, symname);
627 typeptr = DEBUG_NewDataType(DT_ARRAY, NULL);
629 cv_defined_types[curr_type - 0x1000] = typeptr;
631 subtype = DEBUG_GetCVType(type->array.elemtype);
632 if( (subtype == NULL)
633 || (elem_size = DEBUG_GetObjectSize(subtype)) == 0 )
639 arr_max = type->array.arrlen / DEBUG_GetObjectSize(subtype);
642 DEBUG_SetArrayParams(typeptr, 0, arr_max, subtype);
646 * This is where the basic list of fields is defined for
647 * structures and classes.
649 * First, we need to look ahead and see whether we are building
650 * a fieldlist for an enum or a struct.
653 type2 = (union codeview_type *) ptr2.c;
654 if( type2->member.id == LF_MEMBER )
656 typeptr = DEBUG_NewDataType(DT_STRUCT, NULL);
657 fieldtype = DT_STRUCT;
659 else if( type2->member.id == LF_ENUMERATE )
661 typeptr = DEBUG_NewDataType(DT_ENUM, NULL);
669 cv_defined_types[curr_type - 0x1000] = typeptr;
670 while( ptr2.c < (ptr.c + ((type->generic.len + 3) & ~3)) )
672 type2 = (union codeview_type *) ptr2.c;
673 if( type2->member.id == LF_MEMBER && fieldtype == DT_STRUCT )
675 memset(symname, 0, sizeof(symname));
676 memcpy(symname, type2->member.name, type2->member.namelen);
678 subtype = DEBUG_GetCVType(type2->member.type);
680 if( subtype != NULL )
682 elem_size = DEBUG_GetObjectSize(subtype);
685 if( type2->member.offset >= 0x8000 )
688 * This is a numeric leaf, I am too lazy to handle this right
691 fprintf(stderr, "Ignoring large numberic leaf.\n");
695 DEBUG_AddStructElement(typeptr, symname, subtype,
696 type2->member.offset << 3,
700 else if( type2->member.id == LF_ENUMERATE && fieldtype == DT_ENUM )
702 memset(symname, 0, sizeof(symname));
703 memcpy(symname, type2->enumerate.name, type2->enumerate.namelen);
705 if( type2->enumerate.value >= 0x8000 )
708 * This is a numeric leaf, I am too lazy to handle this right
711 fprintf(stderr, "Ignoring large numberic leaf.\n");
715 DEBUG_AddStructElement(typeptr, symname, NULL,
716 type2->enumerate.value, 0);
722 * Something else I have never seen before. Either wrong type of
723 * object in the fieldlist, or some other problem which I wouldn't
724 * really know how to handle until it came up.
726 fprintf(stderr, "Unexpected entry in fieldlist\n");
731 ptr2.c += ((type2->member.namelen + 9 + 3) & ~3);
736 if( type->structure.structlen >= 0x8000 )
739 * This is a numeric leaf, I am too lazy to handle this right
742 fprintf(stderr, "Ignoring large numberic leaf.\n");
745 memset(symname, 0, sizeof(symname));
746 memcpy(symname, type->structure.name, type->structure.namelen);
747 if( strcmp(symname, "__unnamed") == 0 )
749 typeptr = DEBUG_NewDataType(DT_STRUCT, NULL);
753 typeptr = DEBUG_NewDataType(DT_STRUCT, symname);
755 cv_defined_types[curr_type - 0x1000] = typeptr;
758 * Now copy the relevant bits from the fieldlist that we specified.
760 subtype = DEBUG_GetCVType(type->structure.fieldlist);
762 if( subtype != NULL )
764 DEBUG_SetStructSize(typeptr, type->structure.structlen);
765 DEBUG_CopyFieldlist(typeptr, subtype);
769 if( type->t_union.un_len >= 0x8000 )
772 * This is a numeric leaf, I am too lazy to handle this right
775 fprintf(stderr, "Ignoring large numberic leaf.\n");
778 memset(symname, 0, sizeof(symname));
779 memcpy(symname, type->t_union.name, type->t_union.namelen);
781 if( strcmp(symname, "__unnamed") == 0 )
783 typeptr = DEBUG_NewDataType(DT_STRUCT, NULL);
787 typeptr = DEBUG_NewDataType(DT_STRUCT, symname);
790 cv_defined_types[curr_type - 0x1000] = typeptr;
793 * Now copy the relevant bits from the fieldlist that we specified.
795 subtype = DEBUG_GetCVType(type->t_union.field);
797 if( subtype != NULL )
799 DEBUG_SetStructSize(typeptr, type->t_union.un_len);
800 DEBUG_CopyFieldlist(typeptr, subtype);
804 typeptr = DEBUG_NewDataType(DT_BITFIELD, NULL);
805 cv_defined_types[curr_type - 0x1000] = typeptr;
806 DEBUG_SetBitfieldParams(typeptr, type->bitfield.bitoff,
807 type->bitfield.nbits,
808 DEBUG_GetCVType(type->bitfield.type));
811 memset(symname, 0, sizeof(symname));
812 memcpy(symname, type->enumeration.name, type->enumeration.namelen);
813 typeptr = DEBUG_NewDataType(DT_ENUM, symname);
814 cv_defined_types[curr_type - 0x1000] = typeptr;
817 * Now copy the relevant bits from the fieldlist that we specified.
819 subtype = DEBUG_GetCVType(type->enumeration.field);
821 if( subtype != NULL )
823 DEBUG_CopyFieldlist(typeptr, subtype);
831 ptr.c += (type->generic.len + 3) & ~3;
838 DEBUG_InitCVDataTypes()
841 * These are the common builtin types that are used by VC++.
843 cv_basic_types[T_NOTYPE] = NULL;
844 cv_basic_types[T_ABS] = NULL;
845 cv_basic_types[T_VOID] = DEBUG_NewDataType(DT_BASIC, "void");
846 cv_basic_types[T_CHAR] = DEBUG_NewDataType(DT_BASIC, "char");
847 cv_basic_types[T_SHORT] = DEBUG_NewDataType(DT_BASIC, "short int");
848 cv_basic_types[T_LONG] = DEBUG_NewDataType(DT_BASIC, "long int");
849 cv_basic_types[T_QUAD] = DEBUG_NewDataType(DT_BASIC, "long long int");
850 cv_basic_types[T_UCHAR] = DEBUG_NewDataType(DT_BASIC, "unsigned char");
851 cv_basic_types[T_USHORT] = DEBUG_NewDataType(DT_BASIC, "short unsigned int");
852 cv_basic_types[T_ULONG] = DEBUG_NewDataType(DT_BASIC, "long unsigned int");
853 cv_basic_types[T_UQUAD] = DEBUG_NewDataType(DT_BASIC, "long long unsigned int");
854 cv_basic_types[T_REAL32] = DEBUG_NewDataType(DT_BASIC, "float");
855 cv_basic_types[T_REAL64] = DEBUG_NewDataType(DT_BASIC, "double");
856 cv_basic_types[T_RCHAR] = DEBUG_NewDataType(DT_BASIC, "char");
857 cv_basic_types[T_WCHAR] = DEBUG_NewDataType(DT_BASIC, "short");
858 cv_basic_types[T_INT4] = DEBUG_NewDataType(DT_BASIC, "int");
859 cv_basic_types[T_UINT4] = DEBUG_NewDataType(DT_BASIC, "unsigned int");
861 cv_basic_types[T_32PVOID] = DEBUG_FindOrMakePointerType(cv_basic_types[T_VOID]);
862 cv_basic_types[T_32PCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_CHAR]);
863 cv_basic_types[T_32PSHORT] = DEBUG_FindOrMakePointerType(cv_basic_types[T_SHORT]);
864 cv_basic_types[T_32PLONG] = DEBUG_FindOrMakePointerType(cv_basic_types[T_LONG]);
865 cv_basic_types[T_32PQUAD] = DEBUG_FindOrMakePointerType(cv_basic_types[T_QUAD]);
866 cv_basic_types[T_32PUCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_UCHAR]);
867 cv_basic_types[T_32PUSHORT] = DEBUG_FindOrMakePointerType(cv_basic_types[T_USHORT]);
868 cv_basic_types[T_32PULONG] = DEBUG_FindOrMakePointerType(cv_basic_types[T_ULONG]);
869 cv_basic_types[T_32PUQUAD] = DEBUG_FindOrMakePointerType(cv_basic_types[T_UQUAD]);
870 cv_basic_types[T_32PREAL32] = DEBUG_FindOrMakePointerType(cv_basic_types[T_REAL32]);
871 cv_basic_types[T_32PREAL64] = DEBUG_FindOrMakePointerType(cv_basic_types[T_REAL64]);
872 cv_basic_types[T_32PRCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_RCHAR]);
873 cv_basic_types[T_32PWCHAR] = DEBUG_FindOrMakePointerType(cv_basic_types[T_WCHAR]);
874 cv_basic_types[T_32PINT4] = DEBUG_FindOrMakePointerType(cv_basic_types[T_INT4]);
875 cv_basic_types[T_32PUINT4] = DEBUG_FindOrMakePointerType(cv_basic_types[T_UINT4]);
879 * In this function, we keep track of deferred debugging information
880 * that we may need later if we were to need to use the internal debugger.
881 * We don't fully process it here for performance reasons.
884 DEBUG_RegisterDebugInfo( HMODULE32 hModule, const char *module_name,
885 u_long v_addr, u_long size)
887 int has_codeview = FALSE;
890 PIMAGE_DEBUG_DIRECTORY dbgptr;
893 dbgptr = (PIMAGE_DEBUG_DIRECTORY) (hModule + v_addr);
894 for(; size >= sizeof(*dbgptr); size -= sizeof(*dbgptr), dbgptr++ )
898 case IMAGE_DEBUG_TYPE_CODEVIEW:
899 case IMAGE_DEBUG_TYPE_MISC:
906 dbgptr = (PIMAGE_DEBUG_DIRECTORY) (hModule + v_addr);
907 for(; size >= sizeof(*dbgptr); size -= sizeof(*dbgptr), dbgptr++ )
911 case IMAGE_DEBUG_TYPE_COFF:
913 * If we have both codeview and COFF debug info, ignore the
914 * coff debug info as it would just confuse us, and it is
917 * FIXME - this is broken - if we cannot find the PDB file, then
918 * we end up with no debugging info at all. In this case, we
919 * should use the COFF info as a backup.
925 case IMAGE_DEBUG_TYPE_CODEVIEW:
926 case IMAGE_DEBUG_TYPE_MISC:
928 * This is usually an indirection to a .DBG file.
929 * This is similar to (but a slightly older format) from the
932 * First check to see if the image was 'stripped'. If so, it
933 * means that this entry points to a .DBG file. Otherwise,
934 * it just points to itself, and we can ignore this.
942 if( (dbgptr->Type != IMAGE_DEBUG_TYPE_MISC) ||
943 (PE_HEADER(hModule)->FileHeader.Characteristics & IMAGE_FILE_DEBUG_STRIPPED) != 0 )
945 struct deferred_debug_info* deefer = (struct deferred_debug_info *) xmalloc(sizeof(*deefer));
947 deefer->module = hModule;
948 deefer->load_addr = (char *)hModule;
951 * Read the important bits. What we do after this depends
952 * upon the type, but this is always enough so we are able
953 * to proceed if we know what we need to do next.
955 /* in some cases, debug information has not been mapped, so load it...
956 * basically, the PE loader maps all sections (data, resources...), but doesn't map
957 * the DataDirectory array's content. One its entry contains the *beloved*
958 * debug information. (Note the DataDirectory is mapped, not its content)
960 if (IsBadReadPtr32((void*)hModule, dbgptr->PointerToRawData + dbgptr->SizeOfData))
964 DOS_FULL_NAME full_name;
966 if (GetModuleFileName32A(hModule, fn, sizeof(fn)) > 0 &&
967 DOSFS_GetFullName(fn, TRUE, &full_name) &&
968 (fd = open(full_name.long_name, O_RDONLY)) > 0)
970 deefer->dbg_info = mmap(NULL, dbgptr->SizeOfData,
971 PROT_READ, MAP_PRIVATE, fd, dbgptr->PointerToRawData);
973 if( deefer->dbg_info == (char *) 0xffffffff )
982 fprintf(stderr, " (not mapped: fn=%s, lfn=%s, fd=%d)", fn, full_name.long_name, fd);
988 deefer->dbg_info = (char *)(hModule + dbgptr->PointerToRawData);
990 deefer->dbg_size = dbgptr->SizeOfData;
991 deefer->dbgdir = dbgptr;
992 deefer->next = dbglist;
993 deefer->loaded = FALSE;
994 deefer->dbg_index = DEBUG_next_index;
995 deefer->module_name = xstrdup(module_name);
997 deefer->sectp = PE_SECTIONS(hModule);
998 deefer->nsect = PE_HEADER(hModule)->FileHeader.NumberOfSections;
1014 * ELF modules are also entered into the list - this is so that we
1015 * can make 'info shared' types of displays possible.
1018 DEBUG_RegisterELFDebugInfo(int load_addr, u_long size, char * name)
1020 struct deferred_debug_info * deefer;
1022 deefer = (struct deferred_debug_info *) xmalloc(sizeof(*deefer));
1026 * Read the important bits. What we do after this depends
1027 * upon the type, but this is always enough so we are able
1028 * to proceed if we know what we need to do next.
1030 deefer->dbg_size = size;
1031 deefer->dbg_info = (char *) NULL;
1033 deefer->load_addr = (char *) load_addr;
1034 deefer->dbgdir = NULL;
1035 deefer->next = dbglist;
1036 deefer->loaded = TRUE;
1037 deefer->dbg_index = DEBUG_next_index;
1038 deefer->module_name = xstrdup(name);
1049 * Process COFF debugging information embedded in a Win32 application.
1054 DEBUG_ProcessCoff(struct deferred_debug_info * deefer)
1056 struct CoffAuxSection * aux;
1057 struct CoffDebug * coff;
1058 struct CoffFiles * coff_files = NULL;
1059 struct CoffLinenum * coff_linetab;
1061 struct CoffSymbol * coff_sym;
1062 struct CoffSymbol * coff_symbol;
1063 struct CoffFiles * curr_file = NULL;
1067 struct CoffLinenum * linepnt;
1074 int nfiles_alloc = 0;
1075 struct CoffFiles orig_file;
1077 char * this_file = NULL;
1079 coff = (struct CoffDebug *) deefer->dbg_info;
1081 coff_symbol = (struct CoffSymbol *) ((unsigned int) coff + coff->SymbolOffset);
1082 coff_linetab = (struct CoffLinenum *) ((unsigned int) coff + coff->LinenumberOffset);
1083 coff_strtab = (char *) ((unsigned int) coff_symbol + 18*coff->N_Sym);
1087 for(i=0; i < coff->N_Sym; i++ )
1090 * We do this because some compilers (i.e. gcc) incorrectly
1091 * pad the structure up to a 4 byte boundary. The structure
1092 * is really only 18 bytes long, so we have to manually make sure
1095 * FIXME - there must be a way to have autoconf figure out the
1096 * correct compiler option for this. If it is always gcc, that
1097 * makes life simpler, but I don't want to force this.
1099 coff_sym = (struct CoffSymbol *) ((unsigned int) coff_symbol + 18*i);
1100 naux = coff_sym->NumberOfAuxSymbols;
1102 if( coff_sym->StorageClass == IMAGE_SYM_CLASS_FILE )
1104 if( nfiles + 1 >= nfiles_alloc )
1107 coff_files = (struct CoffFiles *) realloc( coff_files,
1108 nfiles_alloc * sizeof(struct CoffFiles));
1110 curr_file = coff_files + nfiles;
1112 curr_file->startaddr = 0xffffffff;
1113 curr_file->endaddr = 0;
1114 curr_file->filename = ((char *) coff_sym) + 18;
1115 curr_file->linetab_offset = -1;
1116 curr_file->linecnt = 0;
1117 curr_file->entries = NULL;
1118 curr_file->neps = curr_file->neps_alloc = 0;
1120 fprintf(stderr,"New file %s\n", curr_file->filename);
1127 * This guy marks the size and location of the text section
1128 * for the current file. We need to keep track of this so
1129 * we can figure out what file the different global functions
1132 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC)
1134 && (coff_sym->Type == 0)
1135 && (coff_sym->SectionNumber == 1) )
1137 aux = (struct CoffAuxSection *) ((unsigned int) coff_sym + 18);
1139 if( curr_file->linetab_offset != -1 )
1142 fprintf(stderr, "Duplicating sect from %s: %x %x %x %d %d\n",
1143 curr_file->filename,
1145 aux->NumberOfRelocations,
1146 aux->NumberOfLinenumbers,
1149 fprintf(stderr, "More sect %d %x %d %d %d\n",
1150 coff_sym->SectionNumber,
1153 coff_sym->StorageClass,
1154 coff_sym->NumberOfAuxSymbols);
1158 * Save this so we can copy bits from it.
1160 orig_file = *curr_file;
1163 * Duplicate the file entry. We have no way to describe
1164 * multiple text sections in our current way of handling things.
1166 if( nfiles + 1 >= nfiles_alloc )
1169 coff_files = (struct CoffFiles *) realloc( coff_files,
1170 nfiles_alloc * sizeof(struct CoffFiles));
1172 curr_file = coff_files + nfiles;
1174 curr_file->startaddr = 0xffffffff;
1175 curr_file->endaddr = 0;
1176 curr_file->filename = orig_file.filename;
1177 curr_file->linetab_offset = -1;
1178 curr_file->linecnt = 0;
1179 curr_file->entries = NULL;
1180 curr_file->neps = curr_file->neps_alloc = 0;
1185 fprintf(stderr, "New text sect from %s: %x %x %x %d %d\n",
1186 curr_file->filename,
1188 aux->NumberOfRelocations,
1189 aux->NumberOfLinenumbers,
1195 if( curr_file->startaddr > coff_sym->Value )
1197 curr_file->startaddr = coff_sym->Value;
1200 if( curr_file->startaddr > coff_sym->Value )
1202 curr_file->startaddr = coff_sym->Value;
1205 if( curr_file->endaddr < coff_sym->Value + aux->Length )
1207 curr_file->endaddr = coff_sym->Value + aux->Length;
1210 curr_file->linetab_offset = linetab_indx;
1211 curr_file->linecnt = aux->NumberOfLinenumbers;
1212 linetab_indx += aux->NumberOfLinenumbers;
1217 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC)
1219 && (coff_sym->SectionNumber == 1) )
1222 * This is a normal static function when naux == 0.
1223 * Just register it. The current file is the correct
1224 * one in this instance.
1226 if( coff_sym->N.Name.NotLong )
1228 memcpy(namebuff, coff_sym->N.ShortName, 8);
1230 nampnt = &namebuff[0];
1234 nampnt = coff_strtab + coff_sym->N.Name.StrTaboff;
1237 if( nampnt[0] == '_' )
1243 new_addr.off = (int) (deefer->load_addr + coff_sym->Value);
1245 if( curr_file->neps + 1 >= curr_file->neps_alloc )
1247 curr_file->neps_alloc += 10;
1248 curr_file->entries = (struct name_hash **)
1249 realloc( curr_file->entries,
1250 curr_file->neps_alloc * sizeof(struct name_hash *));
1253 fprintf(stderr,"\tAdding static symbol %s\n", nampnt);
1255 curr_file->entries[curr_file->neps++] =
1256 DEBUG_AddSymbol( nampnt, &new_addr, this_file, SYM_WIN32 );
1261 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL)
1262 && ISFCN(coff_sym->Type)
1263 && (coff_sym->SectionNumber > 0) )
1265 if( coff_sym->N.Name.NotLong )
1267 memcpy(namebuff, coff_sym->N.ShortName, 8);
1269 nampnt = &namebuff[0];
1273 nampnt = coff_strtab + coff_sym->N.Name.StrTaboff;
1277 if( nampnt[0] == '_' )
1283 new_addr.off = (int) (deefer->load_addr + coff_sym->Value);
1286 fprintf(stderr, "%d: %x %s\n", i, new_addr.off, nampnt);
1288 fprintf(stderr,"\tAdding global symbol %s\n", nampnt);
1292 * Now we need to figure out which file this guy belongs to.
1295 for(j=0; j < nfiles; j++)
1297 if( coff_files[j].startaddr <= coff_sym->Value
1298 && coff_files[j].endaddr > coff_sym->Value )
1300 this_file = coff_files[j].filename;
1304 if( coff_files[j].neps + 1 >= coff_files[j].neps_alloc )
1306 coff_files[j].neps_alloc += 10;
1307 coff_files[j].entries = (struct name_hash **)
1308 realloc( coff_files[j].entries,
1309 coff_files[j].neps_alloc * sizeof(struct name_hash *));
1311 coff_files[j].entries[coff_files[j].neps++] =
1312 DEBUG_AddSymbol( nampnt, &new_addr, this_file, SYM_WIN32 );
1317 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_EXTERNAL)
1318 && (coff_sym->SectionNumber > 0) )
1321 * Similar to above, but for the case of data symbols.
1322 * These aren't treated as entrypoints.
1324 if( coff_sym->N.Name.NotLong )
1326 memcpy(namebuff, coff_sym->N.ShortName, 8);
1328 nampnt = &namebuff[0];
1332 nampnt = coff_strtab + coff_sym->N.Name.StrTaboff;
1336 if( nampnt[0] == '_' )
1342 new_addr.off = (int) (deefer->load_addr + coff_sym->Value);
1345 fprintf(stderr, "%d: %x %s\n", i, new_addr.off, nampnt);
1347 fprintf(stderr,"\tAdding global data symbol %s\n", nampnt);
1351 * Now we need to figure out which file this guy belongs to.
1353 DEBUG_AddSymbol( nampnt, &new_addr, NULL, SYM_WIN32 );
1358 if( (coff_sym->StorageClass == IMAGE_SYM_CLASS_STATIC)
1362 * Ignore these. They don't have anything to do with
1370 fprintf(stderr,"Skipping unknown entry %d %d %d\n", coff_sym->StorageClass,
1371 coff_sym->SectionNumber, naux);
1375 * For now, skip past the aux entries.
1382 * OK, we now should have a list of files, and we should have a list
1383 * of entrypoints. We need to sort the entrypoints so that we are
1384 * able to tie the line numbers with the given functions within the
1387 if( coff_files != NULL )
1389 for(j=0; j < nfiles; j++)
1391 if( coff_files[j].entries != NULL )
1393 qsort(coff_files[j].entries, coff_files[j].neps,
1394 sizeof(struct name_hash *), DEBUG_cmp_sym);
1399 * Now pick apart the line number tables, and attach the entries
1400 * to the given functions.
1402 for(j=0; j < nfiles; j++)
1405 for(k=0; k < coff_files[j].linecnt; k++)
1408 * Another monstrosity caused by the fact that we are using
1409 * a 6 byte structure, and gcc wants to pad structures to 4 byte
1410 * boundaries. Otherwise we could just index into an array.
1412 linepnt = (struct CoffLinenum *)
1413 ((unsigned int) coff_linetab +
1414 6*(coff_files[j].linetab_offset + k));
1416 * If we have spilled onto the next entrypoint, then
1417 * bump the counter..
1421 DEBUG_GetSymbolAddr(coff_files[j].entries[i+1], &new_addr);
1422 if( (i+1 < coff_files[j].neps)
1423 && ( ((unsigned int) deefer->load_addr + linepnt->VirtualAddr)
1435 * Add the line number. This is always relative to the
1436 * start of the function, so we need to subtract that offset
1439 DEBUG_GetSymbolAddr(coff_files[j].entries[i], &new_addr);
1440 DEBUG_AddLineNumber(coff_files[j].entries[i],
1442 (unsigned int) deefer->load_addr
1443 + linepnt->VirtualAddr
1451 if( coff_files != NULL )
1453 for(j=0; j < nfiles; j++)
1455 if( coff_files[j].entries != NULL )
1457 free(coff_files[j].entries);
1468 * Process a codeview line number table. Digestify the thing so that
1469 * we can easily reference the thing when we process the rest of
1472 static struct codeview_linetab_hdr *
1473 DEBUG_SnarfLinetab(char * linetab,
1477 char filename[PATH_MAX];
1478 unsigned int * filetab;
1482 struct codeview_linetab_hdr * lt_hdr;
1483 unsigned int * lt_ptr;
1487 union any_size pnt2;
1488 struct startend * start;
1492 * Now get the important bits.
1494 pnt = (union any_size) linetab;
1498 filetab = (unsigned int *) pnt.c;
1501 * Now count up the number of segments in the file.
1504 for(i=0; i<nfile; i++)
1506 pnt2 = (union any_size) (linetab + filetab[i]);
1511 * Next allocate the header we will be returning.
1512 * There is one header for each segment, so that we can reach in
1513 * and pull bits as required.
1515 lt_hdr = (struct codeview_linetab_hdr *)
1516 xmalloc((nseg + 1) * sizeof(*lt_hdr));
1517 if( lt_hdr == NULL )
1522 memset(lt_hdr, 0, sizeof(*lt_hdr) * (nseg+1));
1525 * Now fill the header we will be returning, one for each segment.
1526 * Note that this will basically just contain pointers into the existing
1527 * line table, and we do not actually copy any additional information
1528 * or allocate any additional memory.
1532 for(i=0; i<nfile; i++)
1535 * Get the pointer into the segment information.
1537 pnt2 = (union any_size) (linetab + filetab[i]);
1538 file_segcount = *pnt2.s;
1541 lt_ptr = (unsigned int *) pnt2.c;
1542 start = (struct startend *) (lt_ptr + file_segcount);
1545 * Now snarf the filename for all of the segments for this file.
1547 fn = (unsigned char *) (start + file_segcount);
1548 memset(filename, 0, sizeof(filename));
1549 memcpy(filename, fn + 1, *fn);
1550 fn = strdup(filename);
1552 for(k = 0; k < file_segcount; k++, this_seg++)
1554 pnt2 = (union any_size) (linetab + lt_ptr[k]);
1555 lt_hdr[this_seg].start = start[k].start;
1556 lt_hdr[this_seg].end = start[k].end;
1557 lt_hdr[this_seg].sourcefile = fn;
1558 lt_hdr[this_seg].segno = *pnt2.s++;
1559 lt_hdr[this_seg].nline = *pnt2.s++;
1560 lt_hdr[this_seg].offtab = pnt2.ui;
1561 lt_hdr[this_seg].linetab = (unsigned short *)
1562 (pnt2.ui + lt_hdr[this_seg].nline);
1573 DEBUG_SnarfCodeView( struct deferred_debug_info * deefer,
1576 struct codeview_linetab_hdr * linetab)
1578 struct name_hash * curr_func = NULL;
1579 struct wine_locals * curr_sym = NULL;
1586 IMAGE_SECTION_HEADER * sectp;
1587 union codeview_symbol * sym;
1588 char symname[PATH_MAX];
1589 struct name_hash * thunk_sym = NULL;
1591 ptr = (union any_size) cv_data;
1592 nsect = deefer->nsect;
1593 sectp = deefer->sectp;
1596 * Skip over the first word. Don't really know what it means, but
1602 * Loop over the different types of records and whenever we
1603 * find something we are interested in, record it and move on.
1605 while( ptr.c - cv_data < size )
1607 sym = (union codeview_symbol *) ptr.c;
1609 if( sym->generic.len - sizeof(int) == (ptr.c - cv_data) )
1612 * This happens when we have indirect symbols that VC++ 4.2
1613 * sometimes uses when there isn't a line number table.
1614 * We ignore it - we will process and enter all of the
1615 * symbols in the global symbol table anyways, so there
1616 * isn't much point in keeping track of all of this crap.
1621 memset(symname, 0, sizeof(symname));
1622 switch(sym->generic.id)
1628 * First, a couple of sanity checks.
1630 if( sym->data.namelen == 0 )
1635 if( sym->data.seg == 0 || sym->data.seg > nsect )
1641 * Global and local data symbols. We don't associate these
1642 * with any given source file.
1645 memcpy(symname, sym->data.name, sym->data.namelen);
1647 new_addr.type = DEBUG_GetCVType(sym->data.symtype);
1648 new_addr.off = (unsigned int) deefer->load_addr +
1649 sectp[sym->data.seg - 1].VirtualAddress +
1651 DEBUG_AddSymbol( symname, &new_addr, NULL, SYM_WIN32 | SYM_DATA );
1655 * Sort of like a global function, but it just points
1656 * to a thunk, which is a stupid name for what amounts to
1657 * a PLT slot in the normal jargon that everyone else uses.
1659 memcpy(symname, sym->thunk.name, sym->thunk.namelen);
1661 new_addr.type = NULL;
1662 new_addr.off = (unsigned int) deefer->load_addr +
1663 sectp[sym->thunk.segment - 1].VirtualAddress +
1665 thunk_sym = DEBUG_AddSymbol( symname, &new_addr, NULL,
1666 SYM_WIN32 | SYM_FUNC);
1667 DEBUG_SetSymbolSize(thunk_sym, sym->thunk.thunk_len);
1672 * Global and static functions.
1674 memcpy(symname, sym->proc.name, sym->proc.namelen);
1676 new_addr.type = DEBUG_GetCVType(sym->proc.proctype);
1677 new_addr.off = (unsigned int) deefer->load_addr +
1678 sectp[sym->proc.segment - 1].VirtualAddress +
1681 * See if we can find a segment that this goes with. If so,
1682 * it means that we also may have line number information
1683 * for this function.
1685 for(i=0; linetab[i].linetab != NULL; i++)
1687 if( ((unsigned int) deefer->load_addr
1688 + sectp[linetab[i].segno - 1].VirtualAddress
1689 + linetab[i].start <= new_addr.off)
1690 && ((unsigned int) deefer->load_addr
1691 + sectp[linetab[i].segno - 1].VirtualAddress
1692 + linetab[i].end > new_addr.off) )
1698 DEBUG_Normalize(curr_func);
1699 if( linetab[i].linetab == NULL )
1701 curr_func = DEBUG_AddSymbol( symname, &new_addr, NULL,
1702 SYM_WIN32 | SYM_FUNC);
1707 * First, create the entry. Then dig through the linetab
1708 * and add whatever line numbers are appropriate for this
1711 curr_func = DEBUG_AddSymbol( symname, &new_addr,
1712 linetab[i].sourcefile,
1713 SYM_WIN32 | SYM_FUNC);
1714 for(j=0; j < linetab[i].nline; j++)
1716 if( linetab[i].offtab[j] >= sym->proc.offset
1717 && linetab[i].offtab[j] < sym->proc.offset
1718 + sym->proc.proc_len )
1720 DEBUG_AddLineNumber(curr_func, linetab[i].linetab[j],
1721 linetab[i].offtab[j] - sym->proc.offset);
1728 * Add information about where we should set breakpoints
1731 DEBUG_SetSymbolBPOff(curr_func, sym->proc.debug_start);
1732 DEBUG_SetSymbolSize(curr_func, sym->proc.proc_len);
1736 * Function parameters and stack variables.
1738 memcpy(symname, sym->stack.name, sym->stack.namelen);
1739 curr_sym = DEBUG_AddLocal(curr_func,
1745 DEBUG_SetLocalSymbolType(curr_sym, DEBUG_GetCVType(sym->stack.symtype));
1753 * Adjust pointer to point to next entry, rounding up to a word
1754 * boundary. MS preserving alignment? Stranger things have
1757 if( sym->generic.id == S_PROCREF
1758 || sym->generic.id == S_DATAREF
1759 || sym->generic.id == S_UNKNOWN )
1761 len = (sym->generic.len + 3) & ~3;
1762 len += ptr.c[16] + 1;
1763 ptr.c += (len + 3) & ~3;
1767 ptr.c += (sym->generic.len + 3) & ~3;
1771 if( linetab != NULL )
1781 * Process PDB file which contains debug information.
1783 * These are really weird beasts. They are intended to be incrementally
1784 * updated by the incremental linker, and this means that you need to
1785 * be able to remove and add information. Thus the PDB file is sort of
1786 * like a block structured device, with a freelist and lists of extent numbers
1787 * that are used to get the relevant pieces. In all cases seen so far, the
1788 * blocksize is always 0x400 bytes. The header has a field which apparently
1789 * holds the blocksize, so if it ever changes we are safe.
1791 * In general, every time we need to extract something from the pdb file,
1792 * it is easier to copy it into another buffer so we have the information
1793 * in one contiguous block rather than attempt to try and keep track of when
1794 * we need to grab another extent from the pdb file.
1796 * The thing that is a real pain about some MS stuff is that they choose
1797 * data structures which are not representable in C. Thus we have to
1798 * hack around and diddle pointers.
1802 DEBUG_ProcessPDBFile(struct deferred_debug_info * deefer, char * full_filename)
1804 char * addr = (char *) 0xffffffff;
1805 unsigned int blocksize;
1806 unsigned int bufflen = 0;
1807 char * buffer = NULL;
1808 unsigned short * extent_table;
1810 struct file_ent * fent;
1812 struct file_list * filelist = NULL;
1813 unsigned int gsym_record = 0;
1814 char * gsymtab = NULL;
1815 struct filetab_hdr * hd;
1818 unsigned int last_extent;
1819 struct codeview_linetab_hdr * linetab;
1820 unsigned int nblocks;
1822 unsigned int offset;
1823 struct codeview_pdb_hdr * pdbhdr;
1825 struct stat statbuf;
1827 unsigned short * table;
1829 unsigned int toc_blocks;
1832 * FIXME - we should use some kind of search path mechanism to locate
1833 * PDB files. Right now we just look in the current working directory,
1834 * which works much of the time, I guess. Ideally we should be able to
1835 * map the filename back using the settings in wine.ini and perhaps
1836 * we could find it there. This bit of coding is left as an exercise
1837 * for the reader. :-).
1839 filename = strrchr(full_filename, '\\');
1840 if( filename == NULL )
1842 filename = full_filename;
1849 status = stat(filename, &statbuf);
1852 fprintf(stderr, "-Unable to open .PDB file %s\n", filename);
1857 * Now open the file, so that we can mmap() it.
1859 fd = open(filename, O_RDONLY);
1862 fprintf(stderr, "-Unable to open .DBG file %s\n", filename);
1868 * Now mmap() the file.
1870 addr = mmap(0, statbuf.st_size, PROT_READ,
1871 MAP_PRIVATE, fd, 0);
1872 if( addr == (char *) 0xffffffff )
1874 fprintf(stderr, "-Unable to mmap .DBG file %s\n", filename);
1879 * Now that we have the formalities over and done with, we need
1880 * to find the table of contents for the PDB file.
1882 pdbhdr = (struct codeview_pdb_hdr *) addr;
1883 blocksize = pdbhdr->blocksize;
1884 last_extent = (statbuf.st_size + blocksize - 1) / blocksize;
1887 * The TOC itself isn't always contiguous, so we need to extract a few
1888 * extents from the file to form the TOC.
1890 toc_blocks = (pdbhdr->toc_len + blocksize - 1) / blocksize;
1891 toc = (char *) xmalloc(toc_blocks * blocksize);
1892 table = pdbhdr->toc_ext;
1893 for(i=0; i < toc_blocks; i++)
1895 memcpy(toc + blocksize*i, addr + table[i]*blocksize, blocksize);
1899 * Next build our own table which will have the size and extent block
1900 * list for each record in the PDB file.
1902 * The TOC starts out with the number of files. Then it is followed by
1903 * (npair * 2*sizeof(int)) bytes of information, which are pairs of ints.
1904 * The first one is the size of the record (in bytes), and the second one
1905 * is something else which I haven't figured out yet.
1907 pnt = (unsigned int *) toc;
1909 extent_table = (unsigned short *) ((unsigned int) toc +
1910 npair * 2 * sizeof(int) + sizeof(int));
1915 if( sizeof(int) + 2*sizeof(int)*npair > pdbhdr->toc_len )
1920 filelist = (struct file_list *) xmalloc(npair * sizeof(*filelist));
1921 if( filelist == NULL )
1925 memset(filelist, 0, npair * sizeof(*filelist));
1928 for(i=0; i < npair; i++)
1930 filelist[i].record_len = pnt[i*2];
1931 filelist[i].nextents = (filelist[i].record_len + blocksize - 1)
1933 filelist[i].extent_list = extent_table + nblocks;
1934 nblocks += filelist[i].nextents;
1937 * These get filled in later when we parse one of the records.
1939 filelist[i].linetab_offset = 0;
1940 filelist[i].linetab_len = 0;
1944 * OK, now walk through the various records and pick out the bits we
1945 * really want to see. Some of the records are extra special, and
1946 * we need to handle these a little bit differently.
1948 for(i=0; i < npair; i++)
1950 if( filelist[i].record_len == 0xffffffff )
1956 * Make sure our buffer is large enough to hold the record.
1958 if( bufflen < filelist[i].nextents * blocksize )
1960 bufflen = filelist[i].nextents * blocksize;
1961 buffer = (char *) realloc(buffer, bufflen);
1965 * Do this just for completeness. It makes debugging easier
1966 * if we have a clean indication of where the record ends.
1968 memset(buffer, 0, filelist[i].nextents * blocksize);
1971 * Next, build the record using the extent list.
1973 for(j=0; j < filelist[i].nextents; j++)
1975 memcpy(buffer + j * blocksize,
1976 addr + filelist[i].extent_list[j] * blocksize,
1980 pnt = (unsigned int *) buffer;
1983 * OK, now figure out what to do with it.
1987 * Always ignore the first entry. It seems to contain a backup copy
1988 * of the TOC (the last time the file was modified??)
1996 * The second entry as a id block. It contains a magic number
1997 * to identify the compiler, plus it also contains the timestamp
1998 * which must match the timestamp in the executable.
2003 if( ((*pnt != 19950623) && (*pnt != 19950814))
2004 || (filelist[i].record_len != 0x24)
2005 || (pnt[1] != ((struct CodeViewDebug *)(deefer->dbg_info))->cv_timestamp) )
2012 * The third entry contains pointers to the global symbol table,
2013 * plus it also contains additional information about each record
2018 hd = (struct filetab_hdr *) buffer;
2020 gsym_record = hd->gsym_file;
2021 gsymtab = (char *) xmalloc( filelist[gsym_record].nextents
2023 memset(gsymtab, 0, filelist[gsym_record].nextents * blocksize);
2025 for(j=0; j < filelist[gsym_record].nextents; j++)
2027 memcpy(gsymtab + j * blocksize,
2028 addr + filelist[gsym_record].extent_list[j] * blocksize,
2033 * This record also contains information about where in the
2034 * remaining records we will be able to find the start of the
2035 * line number table. We could locate that bit using heuristics,
2036 * but since we have the info handy, we might as well use it.
2038 offset = sizeof(*hd);
2041 fent = (struct file_ent *) (buffer + offset);
2042 if( offset > hd->ftab_len )
2047 if( fent->file_number == 0 || fent->file_number >= npair )
2052 filelist[fent->file_number].linetab_offset =
2053 fent->linetab_offset;
2054 filelist[fent->file_number].linetab_len =
2057 * Figure out the offset of the next entry.
2058 * There is a fixed part of the record and a variable
2059 * length filename which we must also skip past.
2061 offset += ((unsigned int) &fent->filename - (unsigned int) fent)
2062 + strlen(fent->filename) + 1;
2063 offset += strlen(buffer+offset) + 1;
2064 offset = (offset + 3) & ~3;
2070 * Two different magic numbers used as dates.
2071 * These indicate the 'type' table.
2073 if( *pnt == 19950410
2074 || *pnt == 19951122 )
2076 DEBUG_ParseTypeTable(buffer, filelist[i].record_len);
2081 * This is something we really want to look at, since it contains
2082 * real debug info. Anything that doesn't match this can be
2088 * First, snag the line table, if we have one. This always
2089 * occurs at the end of the record, so we take the linetab
2090 * offset as the end of the normal part of the record.
2093 if( filelist[i].linetab_len != 0 )
2095 linetab = DEBUG_SnarfLinetab(buffer + filelist[i].linetab_offset,
2096 filelist[i].linetab_len);
2097 DEBUG_SnarfCodeView(deefer, buffer,
2098 filelist[i].linetab_offset,
2103 DEBUG_SnarfCodeView(deefer, buffer,
2104 filelist[i].record_len,
2112 * Finally, process the global symbol table itself. There isn't
2113 * a line number component to this, so we just toss everything
2114 * into the mix and it all should work out.
2116 if( gsym_record != 0 )
2118 DEBUG_SnarfCodeView(deefer, gsymtab - sizeof(int),
2119 filelist[gsym_record].record_len,
2125 if( gsymtab != NULL )
2131 if( buffer != NULL )
2136 if( filelist != NULL )
2141 if( addr != (char *) 0xffffffff )
2143 munmap(addr, statbuf.st_size);
2155 * Process DBG file which contains debug information.
2159 DEBUG_ProcessDBGFile(struct deferred_debug_info * deefer, char * filename)
2161 char * addr = (char *) 0xffffffff;
2163 struct CV4_DirHead * codeview_dir;
2164 struct CV4_DirEnt * codeview_dent;
2165 PIMAGE_DEBUG_DIRECTORY dbghdr;
2166 struct deferred_debug_info deefer2;
2170 struct codeview_linetab_hdr * linetab;
2172 PIMAGE_SEPARATE_DEBUG_HEADER pdbg = NULL;
2173 IMAGE_SECTION_HEADER * sectp;
2174 struct stat statbuf;
2177 status = stat(filename, &statbuf);
2180 fprintf(stderr, "-Unable to open .DBG file %s\n", filename);
2185 * Now open the file, so that we can mmap() it.
2187 fd = open(filename, O_RDONLY);
2190 fprintf(stderr, "Unable to open .DBG file %s\n", filename);
2196 * Now mmap() the file.
2198 addr = mmap(0, statbuf.st_size, PROT_READ,
2199 MAP_PRIVATE, fd, 0);
2200 if( addr == (char *) 0xffffffff )
2202 fprintf(stderr, "Unable to mmap .DBG file %s\n", filename);
2206 pdbg = (PIMAGE_SEPARATE_DEBUG_HEADER) addr;
2208 if( pdbg->TimeDateStamp != deefer->dbgdir->TimeDateStamp )
2210 fprintf(stderr, "Warning - %s has incorrect internal timestamp\n",
2215 fprintf(stderr, "Processing symbols from %s...\n", filename);
2217 dbghdr = (PIMAGE_DEBUG_DIRECTORY) ( addr + sizeof(*pdbg)
2218 + pdbg->NumberOfSections * sizeof(IMAGE_SECTION_HEADER)
2219 + pdbg->ExportedNamesSize);
2221 sectp = (PIMAGE_SECTION_HEADER) ((char *) pdbg + sizeof(*pdbg));
2222 nsect = pdbg->NumberOfSections;
2224 for( i=0; i < pdbg->DebugDirectorySize / sizeof(*pdbg); i++, dbghdr++ )
2226 switch(dbghdr->Type)
2228 case IMAGE_DEBUG_TYPE_COFF:
2230 * Dummy up a deferred debug header to handle the
2231 * COFF stuff embedded within the DBG file.
2233 memset((char *) &deefer2, 0, sizeof(deefer2));
2234 deefer2.dbg_info = (addr + dbghdr->PointerToRawData);
2235 deefer2.dbg_size = dbghdr->SizeOfData;
2236 deefer2.load_addr = deefer->load_addr;
2238 DEBUG_ProcessCoff(&deefer2);
2240 case IMAGE_DEBUG_TYPE_CODEVIEW:
2242 * This is the older format by which codeview stuff is
2243 * stored, known as the 'NB09' format. Newer executables
2244 * and dlls created by VC++ use PDB files instead, which
2245 * have lots of internal similarities, but the overall
2246 * format and structure is quite different.
2248 codeview = (addr + dbghdr->PointerToRawData);
2251 * The first thing in the codeview section should be
2252 * an 'NB09' identifier. As a sanity check, make sure
2255 if( *((unsigned int*) codeview) != 0x3930424e )
2261 * Next we need to find the directory. This is easy too.
2263 codeview_dir = (struct CV4_DirHead *)
2264 (codeview + ((unsigned int*) codeview)[1]);
2267 * Some more sanity checks. Make sure that everything
2268 * is as we expect it.
2270 if( codeview_dir->next_offset != 0
2271 || codeview_dir->dhsize != sizeof(*codeview_dir)
2272 || codeview_dir->desize != sizeof(*codeview_dent) )
2276 codeview_dent = (struct CV4_DirEnt *) (codeview_dir + 1);
2278 for(j=0; j < codeview_dir->ndir; j++, codeview_dent++)
2280 if( codeview_dent->subsect_number == sstAlignSym )
2283 * Check the previous entry. If it is a
2284 * sstSrcModule, it contains the line number
2285 * info for this file.
2288 if( codeview_dent[1].module_number == codeview_dent[0].module_number
2289 && codeview_dent[1].subsect_number == sstSrcModule )
2291 linetab = DEBUG_SnarfLinetab(
2292 codeview + codeview_dent[1].offset,
2293 codeview_dent[1].size);
2296 if( codeview_dent[-1].module_number == codeview_dent[0].module_number
2297 && codeview_dent[-1].subsect_number == sstSrcModule )
2299 linetab = DEBUG_SnarfLinetab(
2300 codeview + codeview_dent[-1].offset,
2301 codeview_dent[-1].size);
2304 * Now process the CV stuff.
2306 DEBUG_SnarfCodeView(deefer,
2307 codeview + codeview_dent->offset,
2308 codeview_dent->size,
2320 if( addr != (char *) 0xffffffff )
2322 munmap(addr, statbuf.st_size);
2334 DEBUG_ProcessDeferredDebug()
2336 struct deferred_debug_info * deefer;
2337 struct CodeViewDebug * cvd;
2338 struct MiscDebug * misc;
2343 DEBUG_InitCVDataTypes();
2345 for(deefer = dbglist; deefer; deefer = deefer->next)
2347 if( deefer->loaded )
2352 if( last_proc != deefer->dbg_index )
2356 fprintf(stderr, "DeferredDebug for:");
2359 fprintf(stderr, " %s",deefer->module_name);
2360 last_proc = deefer->dbg_index;
2363 switch(deefer->dbgdir->Type)
2365 case IMAGE_DEBUG_TYPE_COFF:
2367 * Standard COFF debug information that VC++ adds when you
2368 * use /debugtype:both with the linker.
2371 fprintf(stderr, "Processing COFF symbols...\n");
2373 DEBUG_ProcessCoff(deefer);
2375 case IMAGE_DEBUG_TYPE_CODEVIEW:
2377 * This is a pointer to a PDB file of some sort.
2379 cvd = (struct CodeViewDebug *) deefer->dbg_info;
2381 if( strcmp(cvd->cv_nbtype, "NB10") != 0 )
2384 * Whatever this is, we don't know how to deal with
2389 DEBUG_ProcessPDBFile(deefer, cvd->cv_name);
2391 fprintf(stderr, "Processing PDB file %s\n", cvd->cv_name);
2394 case IMAGE_DEBUG_TYPE_MISC:
2396 * A pointer to a .DBG file of some sort. These files
2397 * can contain either CV4 or COFF information. Open
2398 * the file, and try to do the right thing with it.
2400 misc = (struct MiscDebug *) deefer->dbg_info;
2402 filename = strrchr((char *) &misc->Data, '.');
2405 * Ignore the file if it doesn't have a .DBG extension.
2407 if( (filename == NULL)
2408 || ( (strcmp(filename, ".dbg") != 0)
2409 && (strcmp(filename, ".DBG") != 0)) )
2414 filename = (char *) &misc->Data;
2417 * Do the dirty deed...
2419 DEBUG_ProcessDBGFile(deefer, filename);
2424 * We should never get here...
2430 fprintf(stderr, "\n");
2435 /***********************************************************************
2438 * Display shared libarary information.
2440 void DEBUG_InfoShare(void)
2442 struct deferred_debug_info * deefer;
2444 fprintf(stderr,"Address\t\tModule\tName\n");
2446 for(deefer = dbglist; deefer; deefer = deefer->next)
2448 fprintf(stderr,"0x%8.8x\t(%s)\t%s\n", (unsigned int) deefer->load_addr,
2449 deefer->module ? "Win32" : "ELF", deefer->module_name);