3 * CVDump - Parses through a Visual Studio .DBG file in CodeView 4 format
4 * and dumps the info to STDOUT in a human-readable format
6 * Copyright 2000 John R. Sheets
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wine/port.h"
33 #ifdef HAVE_SYS_TYPES_H
34 # include <sys/types.h>
36 #ifdef HAVE_SYS_STAT_H
37 # include <sys/stat.h>
39 #ifdef HAVE_SYS_MMAN_H
48 #include "cvinclude.h"
53 * IMAGE_SEPARATE_DEBUG_HEADER
54 * IMAGE_SECTION_HEADER[]
55 * IMAGE_DEBUG_DIRECTORY[]
57 * debug data (typical example)
58 * - IMAGE_DEBUG_TYPE_MISC
59 * - IMAGE_DEBUG_TYPE_FPO
60 * - IMAGE_DEBUG_TYPE_CODEVIEW
68 * (hdr) IMAGE_SEPARATE_DEBUG_HEADER - .DBG-specific file header; holds info that
69 * applies to the file as a whole, including # of COFF sections, file offsets, etc.
70 * (hdr) IMAGE_SECTION_HEADER - list of COFF sections copied verbatim from .EXE;
71 * although this directory contains file offsets, these offsets are meaningless
72 * in the context of the .DBG file, because only the section headers are copied
73 * to the .DBG file...not the binary data it points to.
74 * (hdr) IMAGE_DEBUG_DIRECTORY - list of different formats of debug info contained in file
75 * (see IMAGE_DEBUG_TYPE_* descriptions below); tells where each section starts
76 * (hdr) OMFSignature (CV) - Contains "NBxx" signature, plus file offset telling how far
77 * into the IMAGE_DEBUG_TYPE_CODEVIEW section the OMFDirHeader and OMFDirEntry's sit
78 * (data) IMAGE_DEBUG_TYPE_MISC - usually holds name of original .EXE file
79 * (data) IMAGE_DEBUG_TYPE_FPO - Frame Pointer Optimization data; used for dealing with
80 * optimized stack frames (optional)
81 * (data) IMAGE_DEBUG_TYPE_CODEVIEW - *** THE GOOD STUFF ***
82 * This block of data contains all the symbol tables, line number info, etc.,
83 * that the Visual C++ debugger needs.
84 * (hdr) OMFDirHeader (CV) -
85 * (hdr) OMFDirEntry (CV) - list of subsections within CodeView debug data section
89 * The .DBG file typically has three arrays of directory entries, which tell
90 * the OS or debugger where in the file to look for the actual data
92 * IMAGE_SECTION_HEADER - number of entries determined by:
93 * (IMAGE_SEPARATE_DEBUG_HEADER.NumberOfSections)
95 * IMAGE_DEBUG_DIRECTORY - number of entries determined by:
96 * (IMAGE_SEPARATE_DEBUG_HEADER.DebugDirectorySize / sizeof (IMAGE_DEBUG_DIRECTORY))
98 * OMFDirEntry - number of entries determined by:
102 extern void *PE_base
;
104 extern IMAGE_NT_HEADERS
* PE_nt_headers
;
106 static void* cv_base
/* = 0 */;
108 static int dump_cv_sst_module(OMFDirEntry
* omfde
)
114 module
= PRD(Offset(cv_base
) + omfde
->lfo
, sizeof(OMFModule
));
115 if (!module
) {printf("Can't get the OMF-Module, aborting\n"); return FALSE
;}
117 printf(" olvNumber: %u\n", module
->ovlNumber
);
118 printf(" iLib: %u\n", module
->iLib
);
119 printf(" cSeg: %u\n", module
->cSeg
);
120 printf(" Style: %c%c\n", module
->Style
[0], module
->Style
[1]);
121 printf(" Name: %.*s\n",
122 *(BYTE
*)((char*)(module
+ 1) + sizeof(OMFSegDesc
) * module
->cSeg
),
123 (char*)(module
+ 1) + sizeof(OMFSegDesc
) * module
->cSeg
+ 1);
125 segDesc
= PRD(Offset(module
+ 1), sizeof(OMFSegDesc
) * module
->cSeg
);
126 if (!segDesc
) {printf("Can't get the OMF-SegDesc, aborting\n"); return FALSE
;}
128 for (i
= 0; i
< module
->cSeg
; i
++)
130 printf (" segment #%2d: offset = [0x%8lx], size = [0x%8lx]\n",
131 segDesc
->Seg
, segDesc
->Off
, segDesc
->cbSeg
);
137 static int dump_cv_sst_global_pub(OMFDirEntry
* omfde
)
148 fileoffset
= Offset(cv_base
) + omfde
->lfo
;
149 printf (" GlobalPub section starts at file offset 0x%lx\n", fileoffset
);
150 printf (" Symbol table starts at 0x%lx\n", fileoffset
+ sizeof (OMFSymHash
));
152 printf ("\n ----- Begin Symbol Table -----\n");
153 printf (" (type) (symbol name) (offset) (len) (seg) (ind)\n");
155 header
= PRD(fileoffset
, sizeof(OMFSymHash
));
156 if (!header
) {printf("Can't get OMF-SymHash, aborting\n");return FALSE
;}
158 symbols
= PRD(fileoffset
+ sizeof(OMFSymHash
), header
->cbSymbol
);
159 if (!symbols
) {printf("Can't OMF-SymHash details, aborting\n"); return FALSE
;}
161 /* We don't know how many symbols are in this block of memory...only what
162 * the total size of the block is. Because the symbol's name is tacked
163 * on to the end of the PUBSYM32 struct, each symbol may take up a different
164 * # of bytes. This makes it harder to parse through the symbol table,
165 * since we won't know the exact location of the following symbol until we've
166 * already parsed the current one.
168 for (curpos
= symbols
; curpos
< symbols
+ header
->cbSymbol
; curpos
+= recordlen
)
170 /* Point to the next PUBSYM32 in the table.
172 sym
= (PUBSYM32
*)curpos
;
174 if (sym
->reclen
< sizeof(PUBSYM32
)) break;
176 symlen
= sym
->reclen
- sizeof(PUBSYM32
) + 1;
177 if (symlen
> sizeof(nametmp
)) {printf("\nsqueeze%d\n", symlen
);symlen
= sizeof(nametmp
) - 1;}
179 memcpy(nametmp
, curpos
+ sizeof (PUBSYM32
) + 1, symlen
);
180 nametmp
[symlen
] = '\0';
182 printf (" 0x%04x %-30.30s [0x%8lx] [0x%4x] %d %ld\n",
183 sym
->rectyp
, nametmp
, sym
->off
, sym
->reclen
, sym
->seg
, sym
->typind
);
185 /* The entire record is null-padded to the nearest 4-byte
186 * boundary, so we must do a little extra math to keep things straight.
188 recordlen
= (sym
->reclen
+ 3) & ~3;
194 static int dump_cv_sst_global_sym(OMFDirEntry
* omfde
)
196 /*** NOT YET IMPLEMENTED ***/
200 static int dump_cv_sst_static_sym(OMFDirEntry
* omfde
)
202 /*** NOT YET IMPLEMENTED ***/
206 static int dump_cv_sst_libraries(OMFDirEntry
* omfde
)
208 /*** NOT YET IMPLEMENTED ***/
212 static int dump_cv_sst_global_types(OMFDirEntry
* omfde
)
214 /*** NOT YET IMPLEMENTED ***/
218 static int dump_cv_sst_seg_map(OMFDirEntry
* omfde
)
221 OMFSegMapDesc
* segMapDesc
;
224 segMap
= PRD(Offset(cv_base
) + omfde
->lfo
, sizeof(OMFSegMap
));
225 if (!segMap
) {printf("Can't get SegMap, aborting\n");return FALSE
;}
227 printf(" cSeg: %u\n", segMap
->cSeg
);
228 printf(" cSegLog: %u\n", segMap
->cSegLog
);
230 segMapDesc
= PRD(Offset(segMap
+ 1), segMap
->cSeg
* sizeof(OMFSegDesc
));
231 if (!segMapDesc
) {printf("Can't get SegDescr array, aborting\n");return FALSE
;}
233 for (i
= 0; i
< segMap
->cSeg
; i
++)
235 printf(" SegDescr #%2d\n", i
+ 1);
236 printf(" flags: %04X\n", segMapDesc
[i
].flags
);
237 printf(" ovl: %u\n", segMapDesc
[i
].ovl
);
238 printf(" group: %u\n", segMapDesc
[i
].group
);
239 printf(" frame: %u\n", segMapDesc
[i
].frame
);
240 printf(" iSegName: %u\n", segMapDesc
[i
].iSegName
);
241 printf(" iClassName: %u\n", segMapDesc
[i
].iClassName
);
242 printf(" offset: %lu\n", segMapDesc
[i
].offset
);
243 printf(" cbSeg: %lu\n", segMapDesc
[i
].cbSeg
);
249 static int dump_cv_sst_file_index(OMFDirEntry
* omfde
)
251 /*** NOT YET IMPLEMENTED ***/
255 static int dump_cv_sst_src_module(OMFDirEntry
* omfde
)
259 unsigned long* seg_info_dw
;
260 unsigned short* seg_info_w
;
262 OMFSourceModule
* sourceModule
;
263 OMFSourceFile
* sourceFile
;
264 OMFSourceLine
* sourceLine
;
266 rawdata
= PRD(Offset(cv_base
) + omfde
->lfo
, omfde
->cb
);
267 if (!rawdata
) {printf("Can't get srcModule subsection details, aborting\n");return FALSE
;}
269 /* FIXME: check ptr validity */
270 sourceModule
= (void*)rawdata
;
271 printf (" Module table: Found %d file(s) and %d segment(s)\n",
272 sourceModule
->cFile
, sourceModule
->cSeg
);
273 for (i
= 0; i
< sourceModule
->cFile
; i
++)
275 printf (" File #%2d begins at an offset of 0x%lx in this section\n",
276 i
+ 1, sourceModule
->baseSrcFile
[i
]);
279 /* FIXME: check ptr validity */
280 seg_info_dw
= (void*)((char*)(sourceModule
+ 1) +
281 sizeof(unsigned long) * (sourceModule
->cFile
- 1));
282 seg_info_w
= (unsigned short*)(&seg_info_dw
[sourceModule
->cSeg
* 2]);
283 for (i
= 0; i
< sourceModule
->cSeg
; i
++)
285 printf (" Segment #%2d start = 0x%lx, end = 0x%lx, seg index = %u\n",
286 i
+ 1, seg_info_dw
[i
* 2], seg_info_dw
[(i
* 2) + 1],
289 ofs
= sizeof(OMFSourceModule
) + sizeof(unsigned long) * (sourceModule
->cFile
- 1) +
290 sourceModule
->cSeg
* (2 * sizeof(unsigned long) + sizeof(unsigned short));
291 ofs
= (ofs
+ 3) & ~3;
293 /* the OMFSourceFile is quite unpleasant to use:
295 * unsigned short number of segments
296 * unsigned short reservered
297 * unsigned long baseSrcLn[# segments]
298 * unsigned long offset[2 * #segments]
299 * odd indices are start offsets
300 * even indices are end offsets
301 * unsigned char string length for file name
302 * char file name (length is previous field)
304 /* FIXME: check ptr validity */
305 sourceFile
= (void*)(rawdata
+ ofs
);
306 seg_info_dw
= (void*)((char*)sourceFile
+ 2 * sizeof(unsigned short) +
307 sourceFile
->cSeg
* sizeof(unsigned long));
309 ofs
+= 2 * sizeof(unsigned short) + 3 * sourceFile
->cSeg
* sizeof(unsigned long);
311 printf(" File table: %.*s\n",
312 *(BYTE
*)((char*)sourceModule
+ ofs
), (char*)sourceModule
+ ofs
+ 1);
314 for (i
= 0; i
< sourceFile
->cSeg
; i
++)
316 printf (" Segment #%2d start = 0x%lx, end = 0x%lx, offset = 0x%lx\n",
317 i
+ 1, seg_info_dw
[i
* 2], seg_info_dw
[(i
* 2) + 1], sourceFile
->baseSrcLn
[i
]);
319 /* add file name length */
320 ofs
+= *(BYTE
*)((char*)sourceModule
+ ofs
) + 1;
321 ofs
= (ofs
+ 3) & ~3;
323 for (i
= 0; i
< sourceModule
->cSeg
; i
++)
325 sourceLine
= (void*)(rawdata
+ ofs
);
326 seg_info_dw
= (void*)((char*)sourceLine
+ 2 * sizeof(unsigned short));
327 seg_info_w
= (void*)(&seg_info_dw
[sourceLine
->cLnOff
]);
329 printf (" Line table #%2d: Found %d line numbers for segment index %d\n",
330 i
, sourceLine
->cLnOff
, sourceLine
->Seg
);
332 for (j
= 0; j
< sourceLine
->cLnOff
; j
++)
334 printf (" Pair #%2d: offset = [0x%8lx], linenumber = %d\n",
335 j
+ 1, seg_info_dw
[j
], seg_info_w
[j
]);
337 ofs
+= 2 * sizeof(unsigned short) +
338 sourceLine
->cLnOff
* (sizeof(unsigned long) + sizeof(unsigned short));
339 ofs
= (ofs
+ 3) & ~3;
345 static int dump_cv_sst_align_sym(OMFDirEntry
* omfde
)
347 /*** NOT YET IMPLEMENTED ***/
352 static void dump_codeview_all_modules(OMFDirHeader
*omfdh
)
355 OMFDirEntry
*dirEntry
;
358 if (!omfdh
|| !omfdh
->cDir
) return;
360 dirEntry
= PRD(Offset(omfdh
+ 1), omfdh
->cDir
* sizeof(OMFDirEntry
));
361 if (!dirEntry
) {printf("Can't read DirEntry array, aborting\n"); return;}
363 for (i
= 0; i
< omfdh
->cDir
; i
++)
365 switch (dirEntry
[i
].SubSection
)
367 case sstModule
: str
= "sstModule"; break;
368 case sstAlignSym
: str
= "sstAlignSym"; break;
369 case sstSrcModule
: str
= "sstSrcModule"; break;
370 case sstLibraries
: str
= "sstLibraries"; break;
371 case sstGlobalSym
: str
= "sstGlobalSym"; break;
372 case sstGlobalPub
: str
= "sstGlobalPub"; break;
373 case sstGlobalTypes
: str
= "sstGlobalTypes"; break;
374 case sstSegMap
: str
= "sstSegMap"; break;
375 case sstFileIndex
: str
= "sstFileIndex"; break;
376 case sstStaticSym
: str
= "sstStaticSym"; break;
377 default: str
= "<undefined>"; break;
379 printf("Module #%2d (%p)\n", i
+ 1, &dirEntry
[i
]);
380 printf(" SubSection: %04X (%s)\n", dirEntry
[i
].SubSection
, str
);
381 printf(" iMod: %d\n", dirEntry
[i
].iMod
);
382 printf(" lfo: %ld\n", dirEntry
[i
].lfo
);
383 printf(" cb: %lu\n", dirEntry
[i
].cb
);
385 switch (dirEntry
[i
].SubSection
)
387 case sstModule
: dump_cv_sst_module(&dirEntry
[i
]); break;
388 case sstAlignSym
: dump_cv_sst_align_sym(&dirEntry
[i
]); break;
389 case sstSrcModule
: dump_cv_sst_src_module(&dirEntry
[i
]); break;
390 case sstLibraries
: dump_cv_sst_libraries(&dirEntry
[i
]); break;
391 case sstGlobalSym
: dump_cv_sst_global_sym(&dirEntry
[i
]); break;
392 case sstGlobalPub
: dump_cv_sst_global_pub(&dirEntry
[i
]); break;
393 case sstGlobalTypes
: dump_cv_sst_global_types(&dirEntry
[i
]); break;
394 case sstSegMap
: dump_cv_sst_seg_map(&dirEntry
[i
]); break;
395 case sstFileIndex
: dump_cv_sst_file_index(&dirEntry
[i
]); break;
396 case sstStaticSym
: dump_cv_sst_static_sym(&dirEntry
[i
]); break;
397 default: printf("unsupported type %x\n", dirEntry
[i
].SubSection
); break;
405 static void dump_codeview_headers(unsigned long base
, unsigned long len
)
407 OMFDirHeader
*dirHeader
;
408 OMFSignature
*signature
;
409 OMFDirEntry
*dirEntry
;
411 int modulecount
= 0, alignsymcount
= 0, srcmodulecount
= 0, librariescount
= 0;
412 int globalsymcount
= 0, globalpubcount
= 0, globaltypescount
= 0;
413 int segmapcount
= 0, fileindexcount
= 0, staticsymcount
= 0;
415 cv_base
= PRD(base
, len
);
416 if (!cv_base
) {printf("Can't get full debug content, aborting\n");return;}
420 printf(" CodeView Data\n");
422 printf(" Signature: %.4s\n", signature
->Signature
);
423 printf(" Filepos: 0x%08lX\n", signature
->filepos
);
425 if (memcmp(signature
->Signature
, "NB10", 4) == 0)
427 struct {DWORD TimeStamp
; DWORD Dunno
; char Name
[1];}* pdb_data
;
428 pdb_data
= (void*)(signature
+ 1);
430 printf(" TimeStamp: %08lX (%s)\n",
431 pdb_data
->TimeStamp
, get_time_str(pdb_data
->TimeStamp
));
432 printf(" Dunno: %08lX\n", pdb_data
->Dunno
);
433 printf(" Filename: %s\n", pdb_data
->Name
);
437 if (memcmp(signature
->Signature
, "NB09", 4) != 0 && memcmp(signature
->Signature
, "NB11", 4) != 0)
439 printf("Unsupported signature, aborting\n");
443 dirHeader
= PRD(Offset(cv_base
) + signature
->filepos
, sizeof(OMFDirHeader
));
444 if (!dirHeader
) {printf("Can't get debug header, aborting\n"); return;}
446 printf(" Size of header: 0x%4X\n", dirHeader
->cbDirHeader
);
447 printf(" Size per entry: 0x%4X\n", dirHeader
->cbDirEntry
);
448 printf(" # of entries: 0x%8lX (%ld)\n", dirHeader
->cDir
, dirHeader
->cDir
);
449 printf(" Offset to NextDir: 0x%8lX\n", dirHeader
->lfoNextDir
);
450 printf(" Flags: 0x%8lX\n", dirHeader
->flags
);
452 if (!dirHeader
->cDir
) return;
454 dirEntry
= PRD(Offset(dirHeader
+ 1), sizeof(OMFDirEntry
) * dirHeader
->cDir
);
455 if (!dirEntry
) {printf("Can't get DirEntry array, aborting\n");return;}
457 for (i
= 0; i
< dirHeader
->cDir
; i
++)
459 switch (dirEntry
[i
].SubSection
)
461 case sstModule
: modulecount
++; break;
462 case sstAlignSym
: alignsymcount
++; break;
463 case sstSrcModule
: srcmodulecount
++; break;
464 case sstLibraries
: librariescount
++; break;
465 case sstGlobalSym
: globalsymcount
++; break;
466 case sstGlobalPub
: globalpubcount
++; break;
467 case sstGlobalTypes
: globaltypescount
++; break;
468 case sstSegMap
: segmapcount
++; break;
469 case sstFileIndex
: fileindexcount
++; break;
470 case sstStaticSym
: staticsymcount
++; break;
474 /* This one has to be > 0
476 printf ("\nFound: %d sstModule subsections\n", modulecount
);
478 if (alignsymcount
> 0) printf (" %d sstAlignSym subsections\n", alignsymcount
);
479 if (srcmodulecount
> 0) printf (" %d sstSrcModule subsections\n", srcmodulecount
);
480 if (librariescount
> 0) printf (" %d sstLibraries subsections\n", librariescount
);
481 if (globalsymcount
> 0) printf (" %d sstGlobalSym subsections\n", globalsymcount
);
482 if (globalpubcount
> 0) printf (" %d sstGlobalPub subsections\n", globalpubcount
);
483 if (globaltypescount
> 0) printf (" %d sstGlobalTypes subsections\n", globaltypescount
);
484 if (segmapcount
> 0) printf (" %d sstSegMap subsections\n", segmapcount
);
485 if (fileindexcount
> 0) printf (" %d sstFileIndex subsections\n", fileindexcount
);
486 if (staticsymcount
> 0) printf (" %d sstStaticSym subsections\n", staticsymcount
);
488 dump_codeview_all_modules(dirHeader
);
491 static const char* get_coff_name( PIMAGE_SYMBOL coff_sym
, const char* coff_strtab
)
493 static char namebuff
[9];
496 if( coff_sym
->N
.Name
.Short
)
498 memcpy(namebuff
, coff_sym
->N
.ShortName
, 8);
500 nampnt
= &namebuff
[0];
504 nampnt
= coff_strtab
+ coff_sym
->N
.Name
.Long
;
507 if( nampnt
[0] == '_' )
512 void dump_coff(unsigned long coffbase
, unsigned long len
)
514 PIMAGE_COFF_SYMBOLS_HEADER coff
;
515 PIMAGE_SYMBOL coff_sym
;
516 PIMAGE_SYMBOL coff_symbols
;
517 PIMAGE_LINENUMBER coff_linetab
;
519 IMAGE_SECTION_HEADER
*sectHead
= (IMAGE_SECTION_HEADER
*)((char*)PE_nt_headers
+ sizeof(DWORD
) + sizeof(IMAGE_FILE_HEADER
) + PE_nt_headers
->FileHeader
.SizeOfOptionalHeader
);
524 coff
= (PIMAGE_COFF_SYMBOLS_HEADER
)PRD(coffbase
, len
);
526 coff_symbols
= (PIMAGE_SYMBOL
) ((unsigned int) coff
+ coff
->LvaToFirstSymbol
);
527 coff_linetab
= (PIMAGE_LINENUMBER
) ((unsigned int) coff
+ coff
->LvaToFirstLinenumber
);
528 coff_strtab
= (char *) (coff_symbols
+ coff
->NumberOfSymbols
);
530 printf("\nDebug table: COFF format. modbase %p, coffbase %p\n", PE_base
, coff
);
531 printf(" ID | seg:offs [ abs ] | symbol/function name\n");
532 for(i
=0; i
< coff
->NumberOfSymbols
; i
++ )
534 coff_sym
= coff_symbols
+ i
;
535 naux
= coff_sym
->NumberOfAuxSymbols
;
537 if( coff_sym
->StorageClass
== IMAGE_SYM_CLASS_FILE
)
539 printf("file %s\n", (char *) (coff_sym
+ 1));
544 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_STATIC
)
546 && (coff_sym
->SectionNumber
== 1) )
548 DWORD base
= sectHead
[coff_sym
->SectionNumber
- 1].VirtualAddress
;
550 * This is a normal static function when naux == 0.
551 * Just register it. The current file is the correct
552 * one in this instance.
554 nampnt
= get_coff_name( coff_sym
, coff_strtab
);
556 printf("%05d | %02d:%08lx [%08lx] | %s\n", i
, coff_sym
->SectionNumber
- 1, coff_sym
->Value
- base
, coff_sym
->Value
, nampnt
);
561 if( (coff_sym
->StorageClass
== IMAGE_SYM_CLASS_EXTERNAL
)
562 && ISFCN(coff_sym
->Type
)
563 && (coff_sym
->SectionNumber
> 0) )
565 DWORD base
= sectHead
[coff_sym
->SectionNumber
- 1].VirtualAddress
;
567 nampnt
= get_coff_name( coff_sym
, coff_strtab
);
569 /* FIXME: add code to find out the file this symbol belongs to,
571 printf("%05d | %02d:%08lx [%08lx] | %s\n", i
, coff_sym
->SectionNumber
- 1, coff_sym
->Value
- base
, coff_sym
->Value
, nampnt
);
577 * For now, skip past the aux entries.
584 void dump_codeview(unsigned long base
, unsigned long len
)
586 dump_codeview_headers(base
, len
);
589 void dump_frame_pointer_omission(unsigned long base
, unsigned long len
)
591 /* FPO is used to describe nonstandard stack frames */
592 printf("FIXME: FPO (frame pointer omission) debug symbol dumping not implemented yet.\n");